blob: 67b6d61abfe1bf20b92fe91047ae1cf40ab23ec9 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * tg3.c: Broadcom Tigon3 ethernet driver.
3 *
4 * Copyright (C) 2001, 2002, 2003, 2004 David S. Miller (davem@redhat.com)
5 * Copyright (C) 2001, 2002, 2003 Jeff Garzik (jgarzik@pobox.com)
6 * Copyright (C) 2004 Sun Microsystems Inc.
Matt Carlsonb86fb2c2011-01-25 15:58:57 +00007 * Copyright (C) 2005-2011 Broadcom Corporation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 *
9 * Firmware is:
Michael Chan49cabf42005-06-06 15:15:17 -070010 * Derived from proprietary unpublished source code,
11 * Copyright (C) 2000-2003 Broadcom Corporation.
12 *
13 * Permission is hereby granted for the distribution of this firmware
14 * data in hexadecimal or equivalent format, provided this copyright
15 * notice is accompanying it.
Linus Torvalds1da177e2005-04-16 15:20:36 -070016 */
17
Linus Torvalds1da177e2005-04-16 15:20:36 -070018
19#include <linux/module.h>
20#include <linux/moduleparam.h>
Matt Carlson6867c842010-07-11 09:31:44 +000021#include <linux/stringify.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/kernel.h>
23#include <linux/types.h>
24#include <linux/compiler.h>
25#include <linux/slab.h>
26#include <linux/delay.h>
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020027#include <linux/in.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <linux/init.h>
Alexey Dobriyana6b7a402011-06-06 10:43:46 +000029#include <linux/interrupt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <linux/ioport.h>
31#include <linux/pci.h>
32#include <linux/netdevice.h>
33#include <linux/etherdevice.h>
34#include <linux/skbuff.h>
35#include <linux/ethtool.h>
Matt Carlson3110f5f52010-12-06 08:28:50 +000036#include <linux/mdio.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include <linux/mii.h>
Matt Carlson158d7ab2008-05-29 01:37:54 -070038#include <linux/phy.h>
Matt Carlsona9daf362008-05-25 23:49:44 -070039#include <linux/brcmphy.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include <linux/if_vlan.h>
41#include <linux/ip.h>
42#include <linux/tcp.h>
43#include <linux/workqueue.h>
Michael Chan61487482005-09-05 17:53:19 -070044#include <linux/prefetch.h>
Tobias Klauserf9a5f7d2005-10-29 15:09:26 +020045#include <linux/dma-mapping.h>
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -080046#include <linux/firmware.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
48#include <net/checksum.h>
Arnaldo Carvalho de Meloc9bdd4b2007-03-12 20:09:15 -030049#include <net/ip.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
51#include <asm/system.h>
Javier Martinez Canillas27fd9de2011-03-26 16:42:31 +000052#include <linux/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070053#include <asm/byteorder.h>
Javier Martinez Canillas27fd9de2011-03-26 16:42:31 +000054#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
David S. Miller49b6e95f2007-03-29 01:38:42 -070056#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070057#include <asm/idprom.h>
David S. Miller49b6e95f2007-03-29 01:38:42 -070058#include <asm/prom.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070059#endif
60
Matt Carlson63532392008-11-03 16:49:57 -080061#define BAR_0 0
62#define BAR_2 2
63
Linus Torvalds1da177e2005-04-16 15:20:36 -070064#include "tg3.h"
65
Joe Perches63c3a662011-04-26 08:12:10 +000066/* Functions & macros to verify TG3_FLAGS types */
67
68static inline int _tg3_flag(enum TG3_FLAGS flag, unsigned long *bits)
69{
70 return test_bit(flag, bits);
71}
72
73static inline void _tg3_flag_set(enum TG3_FLAGS flag, unsigned long *bits)
74{
75 set_bit(flag, bits);
76}
77
78static inline void _tg3_flag_clear(enum TG3_FLAGS flag, unsigned long *bits)
79{
80 clear_bit(flag, bits);
81}
82
83#define tg3_flag(tp, flag) \
84 _tg3_flag(TG3_FLAG_##flag, (tp)->tg3_flags)
85#define tg3_flag_set(tp, flag) \
86 _tg3_flag_set(TG3_FLAG_##flag, (tp)->tg3_flags)
87#define tg3_flag_clear(tp, flag) \
88 _tg3_flag_clear(TG3_FLAG_##flag, (tp)->tg3_flags)
89
Linus Torvalds1da177e2005-04-16 15:20:36 -070090#define DRV_MODULE_NAME "tg3"
Matt Carlson6867c842010-07-11 09:31:44 +000091#define TG3_MAJ_NUM 3
Matt Carlsonefab79c2011-12-08 14:40:18 +000092#define TG3_MIN_NUM 122
Matt Carlson6867c842010-07-11 09:31:44 +000093#define DRV_MODULE_VERSION \
94 __stringify(TG3_MAJ_NUM) "." __stringify(TG3_MIN_NUM)
Matt Carlsonefab79c2011-12-08 14:40:18 +000095#define DRV_MODULE_RELDATE "December 7, 2011"
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
Matt Carlsonfd6d3f02011-08-31 11:44:52 +000097#define RESET_KIND_SHUTDOWN 0
98#define RESET_KIND_INIT 1
99#define RESET_KIND_SUSPEND 2
100
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101#define TG3_DEF_RX_MODE 0
102#define TG3_DEF_TX_MODE 0
103#define TG3_DEF_MSG_ENABLE \
104 (NETIF_MSG_DRV | \
105 NETIF_MSG_PROBE | \
106 NETIF_MSG_LINK | \
107 NETIF_MSG_TIMER | \
108 NETIF_MSG_IFDOWN | \
109 NETIF_MSG_IFUP | \
110 NETIF_MSG_RX_ERR | \
111 NETIF_MSG_TX_ERR)
112
Matt Carlson520b2752011-06-13 13:39:02 +0000113#define TG3_GRC_LCLCTL_PWRSW_DELAY 100
114
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115/* length of time before we decide the hardware is borked,
116 * and dev->tx_timeout() should be called to fix the problem
117 */
Joe Perches63c3a662011-04-26 08:12:10 +0000118
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119#define TG3_TX_TIMEOUT (5 * HZ)
120
121/* hardware minimum and maximum for a single frame's data payload */
122#define TG3_MIN_MTU 60
123#define TG3_MAX_MTU(tp) \
Joe Perches63c3a662011-04-26 08:12:10 +0000124 (tg3_flag(tp, JUMBO_CAPABLE) ? 9000 : 1500)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125
126/* These numbers seem to be hard coded in the NIC firmware somehow.
127 * You can't change the ring sizes, but you can change where you place
128 * them in the NIC onboard memory.
129 */
Matt Carlson7cb32cf2010-09-30 10:34:36 +0000130#define TG3_RX_STD_RING_SIZE(tp) \
Joe Perches63c3a662011-04-26 08:12:10 +0000131 (tg3_flag(tp, LRG_PROD_RING_CAP) ? \
Matt Carlsonde9f5232011-04-05 14:22:43 +0000132 TG3_RX_STD_MAX_SIZE_5717 : TG3_RX_STD_MAX_SIZE_5700)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133#define TG3_DEF_RX_RING_PENDING 200
Matt Carlson7cb32cf2010-09-30 10:34:36 +0000134#define TG3_RX_JMB_RING_SIZE(tp) \
Joe Perches63c3a662011-04-26 08:12:10 +0000135 (tg3_flag(tp, LRG_PROD_RING_CAP) ? \
Matt Carlsonde9f5232011-04-05 14:22:43 +0000136 TG3_RX_JMB_MAX_SIZE_5717 : TG3_RX_JMB_MAX_SIZE_5700)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137#define TG3_DEF_RX_JUMBO_RING_PENDING 100
138
139/* Do not place this n-ring entries value into the tp struct itself,
140 * we really want to expose these constants to GCC so that modulo et
141 * al. operations are done with shifts and masks instead of with
142 * hw multiply/modulo instructions. Another solution would be to
143 * replace things like '% foo' with '& (foo - 1)'.
144 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
146#define TG3_TX_RING_SIZE 512
147#define TG3_DEF_TX_RING_PENDING (TG3_TX_RING_SIZE - 1)
148
Matt Carlson2c49a442010-09-30 10:34:35 +0000149#define TG3_RX_STD_RING_BYTES(tp) \
150 (sizeof(struct tg3_rx_buffer_desc) * TG3_RX_STD_RING_SIZE(tp))
151#define TG3_RX_JMB_RING_BYTES(tp) \
152 (sizeof(struct tg3_ext_rx_buffer_desc) * TG3_RX_JMB_RING_SIZE(tp))
153#define TG3_RX_RCB_RING_BYTES(tp) \
Matt Carlson7cb32cf2010-09-30 10:34:36 +0000154 (sizeof(struct tg3_rx_buffer_desc) * (tp->rx_ret_ring_mask + 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155#define TG3_TX_RING_BYTES (sizeof(struct tg3_tx_buffer_desc) * \
156 TG3_TX_RING_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157#define NEXT_TX(N) (((N) + 1) & (TG3_TX_RING_SIZE - 1))
158
Matt Carlson287be122009-08-28 13:58:46 +0000159#define TG3_DMA_BYTE_ENAB 64
160
161#define TG3_RX_STD_DMA_SZ 1536
162#define TG3_RX_JMB_DMA_SZ 9046
163
164#define TG3_RX_DMA_TO_MAP_SZ(x) ((x) + TG3_DMA_BYTE_ENAB)
165
166#define TG3_RX_STD_MAP_SZ TG3_RX_DMA_TO_MAP_SZ(TG3_RX_STD_DMA_SZ)
167#define TG3_RX_JMB_MAP_SZ TG3_RX_DMA_TO_MAP_SZ(TG3_RX_JMB_DMA_SZ)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168
Matt Carlson2c49a442010-09-30 10:34:35 +0000169#define TG3_RX_STD_BUFF_RING_SIZE(tp) \
170 (sizeof(struct ring_info) * TG3_RX_STD_RING_SIZE(tp))
Matt Carlson2b2cdb62009-11-13 13:03:48 +0000171
Matt Carlson2c49a442010-09-30 10:34:35 +0000172#define TG3_RX_JMB_BUFF_RING_SIZE(tp) \
173 (sizeof(struct ring_info) * TG3_RX_JMB_RING_SIZE(tp))
Matt Carlson2b2cdb62009-11-13 13:03:48 +0000174
Matt Carlsond2757fc2010-04-12 06:58:27 +0000175/* Due to a hardware bug, the 5701 can only DMA to memory addresses
176 * that are at least dword aligned when used in PCIX mode. The driver
177 * works around this bug by double copying the packet. This workaround
178 * is built into the normal double copy length check for efficiency.
179 *
180 * However, the double copy is only necessary on those architectures
181 * where unaligned memory accesses are inefficient. For those architectures
182 * where unaligned memory accesses incur little penalty, we can reintegrate
183 * the 5701 in the normal rx path. Doing so saves a device structure
184 * dereference by hardcoding the double copy threshold in place.
185 */
186#define TG3_RX_COPY_THRESHOLD 256
187#if NET_IP_ALIGN == 0 || defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)
188 #define TG3_RX_COPY_THRESH(tp) TG3_RX_COPY_THRESHOLD
189#else
190 #define TG3_RX_COPY_THRESH(tp) ((tp)->rx_copy_thresh)
191#endif
192
Matt Carlson81389f52011-08-31 11:44:49 +0000193#if (NET_IP_ALIGN != 0)
194#define TG3_RX_OFFSET(tp) ((tp)->rx_offset)
195#else
Eric Dumazet9205fd92011-11-18 06:47:01 +0000196#define TG3_RX_OFFSET(tp) (NET_SKB_PAD)
Matt Carlson81389f52011-08-31 11:44:49 +0000197#endif
198
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199/* minimum number of free TX descriptors required to wake up TX process */
Matt Carlsonf3f3f272009-08-28 14:03:21 +0000200#define TG3_TX_WAKEUP_THRESH(tnapi) ((tnapi)->tx_pending / 4)
Matt Carlson55086ad2011-12-14 11:09:59 +0000201#define TG3_TX_BD_DMA_MAX_2K 2048
Matt Carlsona4cb4282011-12-14 11:09:58 +0000202#define TG3_TX_BD_DMA_MAX_4K 4096
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203
Matt Carlsonad829262008-11-21 17:16:16 -0800204#define TG3_RAW_IP_ALIGN 2
205
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000206#define TG3_FW_UPDATE_TIMEOUT_SEC 5
207
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -0800208#define FIRMWARE_TG3 "tigon/tg3.bin"
209#define FIRMWARE_TG3TSO "tigon/tg3_tso.bin"
210#define FIRMWARE_TG3TSO5 "tigon/tg3_tso5.bin"
211
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212static char version[] __devinitdata =
Joe Perches05dbe002010-02-17 19:44:19 +0000213 DRV_MODULE_NAME ".c:v" DRV_MODULE_VERSION " (" DRV_MODULE_RELDATE ")";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214
215MODULE_AUTHOR("David S. Miller (davem@redhat.com) and Jeff Garzik (jgarzik@pobox.com)");
216MODULE_DESCRIPTION("Broadcom Tigon3 ethernet driver");
217MODULE_LICENSE("GPL");
218MODULE_VERSION(DRV_MODULE_VERSION);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -0800219MODULE_FIRMWARE(FIRMWARE_TG3);
220MODULE_FIRMWARE(FIRMWARE_TG3TSO);
221MODULE_FIRMWARE(FIRMWARE_TG3TSO5);
222
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223static int tg3_debug = -1; /* -1 == use TG3_DEF_MSG_ENABLE as value */
224module_param(tg3_debug, int, 0);
225MODULE_PARM_DESC(tg3_debug, "Tigon3 bitmapped debugging message enable value");
226
Alexey Dobriyana3aa1882010-01-07 11:58:11 +0000227static DEFINE_PCI_DEVICE_TABLE(tg3_pci_tbl) = {
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700228 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5700)},
229 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5701)},
230 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5702)},
231 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5703)},
232 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5704)},
233 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5702FE)},
234 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705)},
235 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705_2)},
236 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705M)},
237 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705M_2)},
238 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5702X)},
239 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5703X)},
240 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5704S)},
241 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5702A3)},
242 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5703A3)},
243 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5782)},
244 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5788)},
245 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5789)},
246 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5901)},
247 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5901_2)},
248 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5704S_2)},
249 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705F)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700250 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5721)},
Michael Chan126a3362006-09-27 16:03:07 -0700251 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5722)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700252 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5751)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700253 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5751M)},
254 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5751F)},
255 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5752)},
256 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5752M)},
257 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5753)},
258 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5753M)},
259 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5753F)},
260 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5754)},
261 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5754M)},
262 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5755)},
263 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5755M)},
Michael Chan126a3362006-09-27 16:03:07 -0700264 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5756)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700265 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5786)},
266 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5787)},
267 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5787M)},
Michael Chan676917d2006-12-07 00:20:22 -0800268 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5787F)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700269 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5714)},
270 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5714S)},
271 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5715)},
272 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5715S)},
273 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5780)},
274 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5780S)},
275 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5781)},
Michael Chanb5d37722006-09-27 16:06:21 -0700276 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5906)},
277 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5906M)},
Matt Carlsond30cdd22007-10-07 23:28:35 -0700278 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5784)},
279 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5764)},
Matt Carlson6c7af272007-10-21 16:12:02 -0700280 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5723)},
Matt Carlson9936bcf2007-10-10 18:03:07 -0700281 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5761)},
282 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5761E)},
Matt Carlsonc88e6682008-11-03 16:49:18 -0800283 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5761S)},
284 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5761SE)},
Matt Carlson2befdce2009-08-28 12:28:45 +0000285 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5785_G)},
286 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5785_F)},
Matt Carlson321d32a2008-11-21 17:22:19 -0800287 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57780)},
288 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57760)},
289 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57790)},
Matt Carlson5e7ccf22009-08-25 10:08:42 +0000290 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57788)},
Matt Carlson5001e2f2009-11-13 13:03:51 +0000291 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5717)},
292 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5718)},
Matt Carlsonb0f75222010-01-20 16:58:11 +0000293 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57781)},
294 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57785)},
295 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57761)},
296 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57765)},
297 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57791)},
298 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57795)},
Matt Carlson302b5002010-06-05 17:24:38 +0000299 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5719)},
Matt Carlsonba1f3c72011-04-05 14:22:50 +0000300 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5720)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700301 {PCI_DEVICE(PCI_VENDOR_ID_SYSKONNECT, PCI_DEVICE_ID_SYSKONNECT_9DXX)},
302 {PCI_DEVICE(PCI_VENDOR_ID_SYSKONNECT, PCI_DEVICE_ID_SYSKONNECT_9MXX)},
303 {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC1000)},
304 {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC1001)},
305 {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC1003)},
306 {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC9100)},
307 {PCI_DEVICE(PCI_VENDOR_ID_APPLE, PCI_DEVICE_ID_APPLE_TIGON3)},
Meelis Roos1dcb14d2011-05-25 05:43:47 +0000308 {PCI_DEVICE(0x10cf, 0x11a2)}, /* Fujitsu 1000base-SX with BCM5703SKHB */
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700309 {}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310};
311
312MODULE_DEVICE_TABLE(pci, tg3_pci_tbl);
313
Andreas Mohr50da8592006-08-14 23:54:30 -0700314static const struct {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 const char string[ETH_GSTRING_LEN];
Matt Carlson48fa55a2011-04-13 11:05:06 +0000316} ethtool_stats_keys[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 { "rx_octets" },
318 { "rx_fragments" },
319 { "rx_ucast_packets" },
320 { "rx_mcast_packets" },
321 { "rx_bcast_packets" },
322 { "rx_fcs_errors" },
323 { "rx_align_errors" },
324 { "rx_xon_pause_rcvd" },
325 { "rx_xoff_pause_rcvd" },
326 { "rx_mac_ctrl_rcvd" },
327 { "rx_xoff_entered" },
328 { "rx_frame_too_long_errors" },
329 { "rx_jabbers" },
330 { "rx_undersize_packets" },
331 { "rx_in_length_errors" },
332 { "rx_out_length_errors" },
333 { "rx_64_or_less_octet_packets" },
334 { "rx_65_to_127_octet_packets" },
335 { "rx_128_to_255_octet_packets" },
336 { "rx_256_to_511_octet_packets" },
337 { "rx_512_to_1023_octet_packets" },
338 { "rx_1024_to_1522_octet_packets" },
339 { "rx_1523_to_2047_octet_packets" },
340 { "rx_2048_to_4095_octet_packets" },
341 { "rx_4096_to_8191_octet_packets" },
342 { "rx_8192_to_9022_octet_packets" },
343
344 { "tx_octets" },
345 { "tx_collisions" },
346
347 { "tx_xon_sent" },
348 { "tx_xoff_sent" },
349 { "tx_flow_control" },
350 { "tx_mac_errors" },
351 { "tx_single_collisions" },
352 { "tx_mult_collisions" },
353 { "tx_deferred" },
354 { "tx_excessive_collisions" },
355 { "tx_late_collisions" },
356 { "tx_collide_2times" },
357 { "tx_collide_3times" },
358 { "tx_collide_4times" },
359 { "tx_collide_5times" },
360 { "tx_collide_6times" },
361 { "tx_collide_7times" },
362 { "tx_collide_8times" },
363 { "tx_collide_9times" },
364 { "tx_collide_10times" },
365 { "tx_collide_11times" },
366 { "tx_collide_12times" },
367 { "tx_collide_13times" },
368 { "tx_collide_14times" },
369 { "tx_collide_15times" },
370 { "tx_ucast_packets" },
371 { "tx_mcast_packets" },
372 { "tx_bcast_packets" },
373 { "tx_carrier_sense_errors" },
374 { "tx_discards" },
375 { "tx_errors" },
376
377 { "dma_writeq_full" },
378 { "dma_write_prioq_full" },
379 { "rxbds_empty" },
380 { "rx_discards" },
381 { "rx_errors" },
382 { "rx_threshold_hit" },
383
384 { "dma_readq_full" },
385 { "dma_read_prioq_full" },
386 { "tx_comp_queue_full" },
387
388 { "ring_set_send_prod_index" },
389 { "ring_status_update" },
390 { "nic_irqs" },
391 { "nic_avoided_irqs" },
Matt Carlson4452d092011-05-19 12:12:51 +0000392 { "nic_tx_threshold_hit" },
393
394 { "mbuf_lwm_thresh_hit" },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395};
396
Matt Carlson48fa55a2011-04-13 11:05:06 +0000397#define TG3_NUM_STATS ARRAY_SIZE(ethtool_stats_keys)
398
399
Andreas Mohr50da8592006-08-14 23:54:30 -0700400static const struct {
Michael Chan4cafd3f2005-05-29 14:56:34 -0700401 const char string[ETH_GSTRING_LEN];
Matt Carlson48fa55a2011-04-13 11:05:06 +0000402} ethtool_test_keys[] = {
Matt Carlson28a45952011-08-19 13:58:22 +0000403 { "nvram test (online) " },
404 { "link test (online) " },
405 { "register test (offline)" },
406 { "memory test (offline)" },
407 { "mac loopback test (offline)" },
408 { "phy loopback test (offline)" },
Matt Carlson941ec902011-08-19 13:58:23 +0000409 { "ext loopback test (offline)" },
Matt Carlson28a45952011-08-19 13:58:22 +0000410 { "interrupt test (offline)" },
Michael Chan4cafd3f2005-05-29 14:56:34 -0700411};
412
Matt Carlson48fa55a2011-04-13 11:05:06 +0000413#define TG3_NUM_TEST ARRAY_SIZE(ethtool_test_keys)
414
415
Michael Chanb401e9e2005-12-19 16:27:04 -0800416static void tg3_write32(struct tg3 *tp, u32 off, u32 val)
417{
418 writel(val, tp->regs + off);
419}
420
421static u32 tg3_read32(struct tg3 *tp, u32 off)
422{
Matt Carlsonde6f31e2010-04-12 06:58:30 +0000423 return readl(tp->regs + off);
Michael Chanb401e9e2005-12-19 16:27:04 -0800424}
425
Matt Carlson0d3031d2007-10-10 18:02:43 -0700426static void tg3_ape_write32(struct tg3 *tp, u32 off, u32 val)
427{
428 writel(val, tp->aperegs + off);
429}
430
431static u32 tg3_ape_read32(struct tg3 *tp, u32 off)
432{
Matt Carlsonde6f31e2010-04-12 06:58:30 +0000433 return readl(tp->aperegs + off);
Matt Carlson0d3031d2007-10-10 18:02:43 -0700434}
435
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436static void tg3_write_indirect_reg32(struct tg3 *tp, u32 off, u32 val)
437{
Michael Chan68929142005-08-09 20:17:14 -0700438 unsigned long flags;
439
440 spin_lock_irqsave(&tp->indirect_lock, flags);
Michael Chan1ee582d2005-08-09 20:16:46 -0700441 pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off);
442 pci_write_config_dword(tp->pdev, TG3PCI_REG_DATA, val);
Michael Chan68929142005-08-09 20:17:14 -0700443 spin_unlock_irqrestore(&tp->indirect_lock, flags);
Michael Chan1ee582d2005-08-09 20:16:46 -0700444}
445
446static void tg3_write_flush_reg32(struct tg3 *tp, u32 off, u32 val)
447{
448 writel(val, tp->regs + off);
449 readl(tp->regs + off);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450}
451
Michael Chan68929142005-08-09 20:17:14 -0700452static u32 tg3_read_indirect_reg32(struct tg3 *tp, u32 off)
453{
454 unsigned long flags;
455 u32 val;
456
457 spin_lock_irqsave(&tp->indirect_lock, flags);
458 pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off);
459 pci_read_config_dword(tp->pdev, TG3PCI_REG_DATA, &val);
460 spin_unlock_irqrestore(&tp->indirect_lock, flags);
461 return val;
462}
463
464static void tg3_write_indirect_mbox(struct tg3 *tp, u32 off, u32 val)
465{
466 unsigned long flags;
467
468 if (off == (MAILBOX_RCVRET_CON_IDX_0 + TG3_64BIT_REG_LOW)) {
469 pci_write_config_dword(tp->pdev, TG3PCI_RCV_RET_RING_CON_IDX +
470 TG3_64BIT_REG_LOW, val);
471 return;
472 }
Matt Carlson66711e62009-11-13 13:03:49 +0000473 if (off == TG3_RX_STD_PROD_IDX_REG) {
Michael Chan68929142005-08-09 20:17:14 -0700474 pci_write_config_dword(tp->pdev, TG3PCI_STD_RING_PROD_IDX +
475 TG3_64BIT_REG_LOW, val);
476 return;
477 }
478
479 spin_lock_irqsave(&tp->indirect_lock, flags);
480 pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off + 0x5600);
481 pci_write_config_dword(tp->pdev, TG3PCI_REG_DATA, val);
482 spin_unlock_irqrestore(&tp->indirect_lock, flags);
483
484 /* In indirect mode when disabling interrupts, we also need
485 * to clear the interrupt bit in the GRC local ctrl register.
486 */
487 if ((off == (MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW)) &&
488 (val == 0x1)) {
489 pci_write_config_dword(tp->pdev, TG3PCI_MISC_LOCAL_CTRL,
490 tp->grc_local_ctrl|GRC_LCLCTRL_CLEARINT);
491 }
492}
493
494static u32 tg3_read_indirect_mbox(struct tg3 *tp, u32 off)
495{
496 unsigned long flags;
497 u32 val;
498
499 spin_lock_irqsave(&tp->indirect_lock, flags);
500 pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off + 0x5600);
501 pci_read_config_dword(tp->pdev, TG3PCI_REG_DATA, &val);
502 spin_unlock_irqrestore(&tp->indirect_lock, flags);
503 return val;
504}
505
Michael Chanb401e9e2005-12-19 16:27:04 -0800506/* usec_wait specifies the wait time in usec when writing to certain registers
507 * where it is unsafe to read back the register without some delay.
508 * GRC_LOCAL_CTRL is one example if the GPIOs are toggled to switch power.
509 * TG3PCI_CLOCK_CTRL is another example if the clock frequencies are changed.
510 */
511static void _tw32_flush(struct tg3 *tp, u32 off, u32 val, u32 usec_wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512{
Joe Perches63c3a662011-04-26 08:12:10 +0000513 if (tg3_flag(tp, PCIX_TARGET_HWBUG) || tg3_flag(tp, ICH_WORKAROUND))
Michael Chanb401e9e2005-12-19 16:27:04 -0800514 /* Non-posted methods */
515 tp->write32(tp, off, val);
516 else {
517 /* Posted method */
518 tg3_write32(tp, off, val);
519 if (usec_wait)
520 udelay(usec_wait);
521 tp->read32(tp, off);
522 }
523 /* Wait again after the read for the posted method to guarantee that
524 * the wait time is met.
525 */
526 if (usec_wait)
527 udelay(usec_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528}
529
Michael Chan09ee9292005-08-09 20:17:00 -0700530static inline void tw32_mailbox_flush(struct tg3 *tp, u32 off, u32 val)
531{
532 tp->write32_mbox(tp, off, val);
Joe Perches63c3a662011-04-26 08:12:10 +0000533 if (!tg3_flag(tp, MBOX_WRITE_REORDER) && !tg3_flag(tp, ICH_WORKAROUND))
Michael Chan68929142005-08-09 20:17:14 -0700534 tp->read32_mbox(tp, off);
Michael Chan09ee9292005-08-09 20:17:00 -0700535}
536
Michael Chan20094932005-08-09 20:16:32 -0700537static void tg3_write32_tx_mbox(struct tg3 *tp, u32 off, u32 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538{
539 void __iomem *mbox = tp->regs + off;
540 writel(val, mbox);
Joe Perches63c3a662011-04-26 08:12:10 +0000541 if (tg3_flag(tp, TXD_MBOX_HWBUG))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 writel(val, mbox);
Joe Perches63c3a662011-04-26 08:12:10 +0000543 if (tg3_flag(tp, MBOX_WRITE_REORDER))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 readl(mbox);
545}
546
Michael Chanb5d37722006-09-27 16:06:21 -0700547static u32 tg3_read32_mbox_5906(struct tg3 *tp, u32 off)
548{
Matt Carlsonde6f31e2010-04-12 06:58:30 +0000549 return readl(tp->regs + off + GRCMBOX_BASE);
Michael Chanb5d37722006-09-27 16:06:21 -0700550}
551
552static void tg3_write32_mbox_5906(struct tg3 *tp, u32 off, u32 val)
553{
554 writel(val, tp->regs + off + GRCMBOX_BASE);
555}
556
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000557#define tw32_mailbox(reg, val) tp->write32_mbox(tp, reg, val)
Michael Chan09ee9292005-08-09 20:17:00 -0700558#define tw32_mailbox_f(reg, val) tw32_mailbox_flush(tp, (reg), (val))
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000559#define tw32_rx_mbox(reg, val) tp->write32_rx_mbox(tp, reg, val)
560#define tw32_tx_mbox(reg, val) tp->write32_tx_mbox(tp, reg, val)
561#define tr32_mailbox(reg) tp->read32_mbox(tp, reg)
Michael Chan20094932005-08-09 20:16:32 -0700562
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000563#define tw32(reg, val) tp->write32(tp, reg, val)
564#define tw32_f(reg, val) _tw32_flush(tp, (reg), (val), 0)
565#define tw32_wait_f(reg, val, us) _tw32_flush(tp, (reg), (val), (us))
566#define tr32(reg) tp->read32(tp, reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567
568static void tg3_write_mem(struct tg3 *tp, u32 off, u32 val)
569{
Michael Chan68929142005-08-09 20:17:14 -0700570 unsigned long flags;
571
Matt Carlson6ff6f812011-05-19 12:12:54 +0000572 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906 &&
Michael Chanb5d37722006-09-27 16:06:21 -0700573 (off >= NIC_SRAM_STATS_BLK) && (off < NIC_SRAM_TX_BUFFER_DESC))
574 return;
575
Michael Chan68929142005-08-09 20:17:14 -0700576 spin_lock_irqsave(&tp->indirect_lock, flags);
Joe Perches63c3a662011-04-26 08:12:10 +0000577 if (tg3_flag(tp, SRAM_USE_CONFIG)) {
Michael Chanbbadf502006-04-06 21:46:34 -0700578 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, off);
579 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_DATA, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580
Michael Chanbbadf502006-04-06 21:46:34 -0700581 /* Always leave this as zero. */
582 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, 0);
583 } else {
584 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, off);
585 tw32_f(TG3PCI_MEM_WIN_DATA, val);
586
587 /* Always leave this as zero. */
588 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, 0);
589 }
Michael Chan68929142005-08-09 20:17:14 -0700590 spin_unlock_irqrestore(&tp->indirect_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591}
592
593static void tg3_read_mem(struct tg3 *tp, u32 off, u32 *val)
594{
Michael Chan68929142005-08-09 20:17:14 -0700595 unsigned long flags;
596
Matt Carlson6ff6f812011-05-19 12:12:54 +0000597 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906 &&
Michael Chanb5d37722006-09-27 16:06:21 -0700598 (off >= NIC_SRAM_STATS_BLK) && (off < NIC_SRAM_TX_BUFFER_DESC)) {
599 *val = 0;
600 return;
601 }
602
Michael Chan68929142005-08-09 20:17:14 -0700603 spin_lock_irqsave(&tp->indirect_lock, flags);
Joe Perches63c3a662011-04-26 08:12:10 +0000604 if (tg3_flag(tp, SRAM_USE_CONFIG)) {
Michael Chanbbadf502006-04-06 21:46:34 -0700605 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, off);
606 pci_read_config_dword(tp->pdev, TG3PCI_MEM_WIN_DATA, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607
Michael Chanbbadf502006-04-06 21:46:34 -0700608 /* Always leave this as zero. */
609 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, 0);
610 } else {
611 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, off);
612 *val = tr32(TG3PCI_MEM_WIN_DATA);
613
614 /* Always leave this as zero. */
615 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, 0);
616 }
Michael Chan68929142005-08-09 20:17:14 -0700617 spin_unlock_irqrestore(&tp->indirect_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618}
619
Matt Carlson0d3031d2007-10-10 18:02:43 -0700620static void tg3_ape_lock_init(struct tg3 *tp)
621{
622 int i;
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000623 u32 regbase, bit;
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000624
625 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
626 regbase = TG3_APE_LOCK_GRANT;
627 else
628 regbase = TG3_APE_PER_LOCK_GRANT;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700629
630 /* Make sure the driver hasn't any stale locks. */
Matt Carlson78f94dc2011-11-04 09:14:58 +0000631 for (i = TG3_APE_LOCK_PHY0; i <= TG3_APE_LOCK_GPIO; i++) {
632 switch (i) {
633 case TG3_APE_LOCK_PHY0:
634 case TG3_APE_LOCK_PHY1:
635 case TG3_APE_LOCK_PHY2:
636 case TG3_APE_LOCK_PHY3:
637 bit = APE_LOCK_GRANT_DRIVER;
638 break;
639 default:
640 if (!tp->pci_fn)
641 bit = APE_LOCK_GRANT_DRIVER;
642 else
643 bit = 1 << tp->pci_fn;
644 }
645 tg3_ape_write32(tp, regbase + 4 * i, bit);
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000646 }
647
Matt Carlson0d3031d2007-10-10 18:02:43 -0700648}
649
650static int tg3_ape_lock(struct tg3 *tp, int locknum)
651{
652 int i, off;
653 int ret = 0;
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000654 u32 status, req, gnt, bit;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700655
Joe Perches63c3a662011-04-26 08:12:10 +0000656 if (!tg3_flag(tp, ENABLE_APE))
Matt Carlson0d3031d2007-10-10 18:02:43 -0700657 return 0;
658
659 switch (locknum) {
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000660 case TG3_APE_LOCK_GPIO:
661 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
662 return 0;
Matt Carlson33f401a2010-04-05 10:19:27 +0000663 case TG3_APE_LOCK_GRC:
664 case TG3_APE_LOCK_MEM:
Matt Carlson78f94dc2011-11-04 09:14:58 +0000665 if (!tp->pci_fn)
666 bit = APE_LOCK_REQ_DRIVER;
667 else
668 bit = 1 << tp->pci_fn;
Matt Carlson33f401a2010-04-05 10:19:27 +0000669 break;
670 default:
671 return -EINVAL;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700672 }
673
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000674 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761) {
675 req = TG3_APE_LOCK_REQ;
676 gnt = TG3_APE_LOCK_GRANT;
677 } else {
678 req = TG3_APE_PER_LOCK_REQ;
679 gnt = TG3_APE_PER_LOCK_GRANT;
680 }
681
Matt Carlson0d3031d2007-10-10 18:02:43 -0700682 off = 4 * locknum;
683
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000684 tg3_ape_write32(tp, req + off, bit);
Matt Carlson0d3031d2007-10-10 18:02:43 -0700685
686 /* Wait for up to 1 millisecond to acquire lock. */
687 for (i = 0; i < 100; i++) {
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000688 status = tg3_ape_read32(tp, gnt + off);
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000689 if (status == bit)
Matt Carlson0d3031d2007-10-10 18:02:43 -0700690 break;
691 udelay(10);
692 }
693
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000694 if (status != bit) {
Matt Carlson0d3031d2007-10-10 18:02:43 -0700695 /* Revoke the lock request. */
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000696 tg3_ape_write32(tp, gnt + off, bit);
Matt Carlson0d3031d2007-10-10 18:02:43 -0700697 ret = -EBUSY;
698 }
699
700 return ret;
701}
702
703static void tg3_ape_unlock(struct tg3 *tp, int locknum)
704{
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000705 u32 gnt, bit;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700706
Joe Perches63c3a662011-04-26 08:12:10 +0000707 if (!tg3_flag(tp, ENABLE_APE))
Matt Carlson0d3031d2007-10-10 18:02:43 -0700708 return;
709
710 switch (locknum) {
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000711 case TG3_APE_LOCK_GPIO:
712 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
713 return;
Matt Carlson33f401a2010-04-05 10:19:27 +0000714 case TG3_APE_LOCK_GRC:
715 case TG3_APE_LOCK_MEM:
Matt Carlson78f94dc2011-11-04 09:14:58 +0000716 if (!tp->pci_fn)
717 bit = APE_LOCK_GRANT_DRIVER;
718 else
719 bit = 1 << tp->pci_fn;
Matt Carlson33f401a2010-04-05 10:19:27 +0000720 break;
721 default:
722 return;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700723 }
724
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000725 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
726 gnt = TG3_APE_LOCK_GRANT;
727 else
728 gnt = TG3_APE_PER_LOCK_GRANT;
729
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000730 tg3_ape_write32(tp, gnt + 4 * locknum, bit);
Matt Carlson0d3031d2007-10-10 18:02:43 -0700731}
732
Matt Carlsonfd6d3f02011-08-31 11:44:52 +0000733static void tg3_ape_send_event(struct tg3 *tp, u32 event)
734{
735 int i;
736 u32 apedata;
737
738 /* NCSI does not support APE events */
739 if (tg3_flag(tp, APE_HAS_NCSI))
740 return;
741
742 apedata = tg3_ape_read32(tp, TG3_APE_SEG_SIG);
743 if (apedata != APE_SEG_SIG_MAGIC)
744 return;
745
746 apedata = tg3_ape_read32(tp, TG3_APE_FW_STATUS);
747 if (!(apedata & APE_FW_STATUS_READY))
748 return;
749
750 /* Wait for up to 1 millisecond for APE to service previous event. */
751 for (i = 0; i < 10; i++) {
752 if (tg3_ape_lock(tp, TG3_APE_LOCK_MEM))
753 return;
754
755 apedata = tg3_ape_read32(tp, TG3_APE_EVENT_STATUS);
756
757 if (!(apedata & APE_EVENT_STATUS_EVENT_PENDING))
758 tg3_ape_write32(tp, TG3_APE_EVENT_STATUS,
759 event | APE_EVENT_STATUS_EVENT_PENDING);
760
761 tg3_ape_unlock(tp, TG3_APE_LOCK_MEM);
762
763 if (!(apedata & APE_EVENT_STATUS_EVENT_PENDING))
764 break;
765
766 udelay(100);
767 }
768
769 if (!(apedata & APE_EVENT_STATUS_EVENT_PENDING))
770 tg3_ape_write32(tp, TG3_APE_EVENT, APE_EVENT_1);
771}
772
773static void tg3_ape_driver_state_change(struct tg3 *tp, int kind)
774{
775 u32 event;
776 u32 apedata;
777
778 if (!tg3_flag(tp, ENABLE_APE))
779 return;
780
781 switch (kind) {
782 case RESET_KIND_INIT:
783 tg3_ape_write32(tp, TG3_APE_HOST_SEG_SIG,
784 APE_HOST_SEG_SIG_MAGIC);
785 tg3_ape_write32(tp, TG3_APE_HOST_SEG_LEN,
786 APE_HOST_SEG_LEN_MAGIC);
787 apedata = tg3_ape_read32(tp, TG3_APE_HOST_INIT_COUNT);
788 tg3_ape_write32(tp, TG3_APE_HOST_INIT_COUNT, ++apedata);
789 tg3_ape_write32(tp, TG3_APE_HOST_DRIVER_ID,
790 APE_HOST_DRIVER_ID_MAGIC(TG3_MAJ_NUM, TG3_MIN_NUM));
791 tg3_ape_write32(tp, TG3_APE_HOST_BEHAVIOR,
792 APE_HOST_BEHAV_NO_PHYLOCK);
793 tg3_ape_write32(tp, TG3_APE_HOST_DRVR_STATE,
794 TG3_APE_HOST_DRVR_STATE_START);
795
796 event = APE_EVENT_STATUS_STATE_START;
797 break;
798 case RESET_KIND_SHUTDOWN:
799 /* With the interface we are currently using,
800 * APE does not track driver state. Wiping
801 * out the HOST SEGMENT SIGNATURE forces
802 * the APE to assume OS absent status.
803 */
804 tg3_ape_write32(tp, TG3_APE_HOST_SEG_SIG, 0x0);
805
806 if (device_may_wakeup(&tp->pdev->dev) &&
807 tg3_flag(tp, WOL_ENABLE)) {
808 tg3_ape_write32(tp, TG3_APE_HOST_WOL_SPEED,
809 TG3_APE_HOST_WOL_SPEED_AUTO);
810 apedata = TG3_APE_HOST_DRVR_STATE_WOL;
811 } else
812 apedata = TG3_APE_HOST_DRVR_STATE_UNLOAD;
813
814 tg3_ape_write32(tp, TG3_APE_HOST_DRVR_STATE, apedata);
815
816 event = APE_EVENT_STATUS_STATE_UNLOAD;
817 break;
818 case RESET_KIND_SUSPEND:
819 event = APE_EVENT_STATUS_STATE_SUSPEND;
820 break;
821 default:
822 return;
823 }
824
825 event |= APE_EVENT_STATUS_DRIVER_EVNT | APE_EVENT_STATUS_STATE_CHNGE;
826
827 tg3_ape_send_event(tp, event);
828}
829
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830static void tg3_disable_ints(struct tg3 *tp)
831{
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000832 int i;
833
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 tw32(TG3PCI_MISC_HOST_CTRL,
835 (tp->misc_host_ctrl | MISC_HOST_CTRL_MASK_PCI_INT));
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000836 for (i = 0; i < tp->irq_max; i++)
837 tw32_mailbox_f(tp->napi[i].int_mbox, 0x00000001);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838}
839
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840static void tg3_enable_ints(struct tg3 *tp)
841{
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000842 int i;
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000843
Michael Chanbbe832c2005-06-24 20:20:04 -0700844 tp->irq_sync = 0;
845 wmb();
846
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 tw32(TG3PCI_MISC_HOST_CTRL,
848 (tp->misc_host_ctrl & ~MISC_HOST_CTRL_MASK_PCI_INT));
Matt Carlsonf19af9c2009-09-01 12:47:49 +0000849
Matt Carlsonf89f38b2010-02-12 14:47:07 +0000850 tp->coal_now = tp->coalesce_mode | HOSTCC_MODE_ENABLE;
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000851 for (i = 0; i < tp->irq_cnt; i++) {
852 struct tg3_napi *tnapi = &tp->napi[i];
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000853
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000854 tw32_mailbox_f(tnapi->int_mbox, tnapi->last_tag << 24);
Joe Perches63c3a662011-04-26 08:12:10 +0000855 if (tg3_flag(tp, 1SHOT_MSI))
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000856 tw32_mailbox_f(tnapi->int_mbox, tnapi->last_tag << 24);
857
Matt Carlsonf89f38b2010-02-12 14:47:07 +0000858 tp->coal_now |= tnapi->coal_now;
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000859 }
Matt Carlsonf19af9c2009-09-01 12:47:49 +0000860
861 /* Force an initial interrupt */
Joe Perches63c3a662011-04-26 08:12:10 +0000862 if (!tg3_flag(tp, TAGGED_STATUS) &&
Matt Carlsonf19af9c2009-09-01 12:47:49 +0000863 (tp->napi[0].hw_status->status & SD_STATUS_UPDATED))
864 tw32(GRC_LOCAL_CTRL, tp->grc_local_ctrl | GRC_LCLCTRL_SETINT);
865 else
Matt Carlsonf89f38b2010-02-12 14:47:07 +0000866 tw32(HOSTCC_MODE, tp->coal_now);
867
868 tp->coal_now &= ~(tp->napi[0].coal_now | tp->napi[1].coal_now);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869}
870
Matt Carlson17375d22009-08-28 14:02:18 +0000871static inline unsigned int tg3_has_work(struct tg3_napi *tnapi)
Michael Chan04237dd2005-04-25 15:17:17 -0700872{
Matt Carlson17375d22009-08-28 14:02:18 +0000873 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +0000874 struct tg3_hw_status *sblk = tnapi->hw_status;
Michael Chan04237dd2005-04-25 15:17:17 -0700875 unsigned int work_exists = 0;
876
877 /* check for phy events */
Joe Perches63c3a662011-04-26 08:12:10 +0000878 if (!(tg3_flag(tp, USE_LINKCHG_REG) || tg3_flag(tp, POLL_SERDES))) {
Michael Chan04237dd2005-04-25 15:17:17 -0700879 if (sblk->status & SD_STATUS_LINK_CHG)
880 work_exists = 1;
881 }
882 /* check for RX/TX work to do */
Matt Carlsonf3f3f272009-08-28 14:03:21 +0000883 if (sblk->idx[0].tx_consumer != tnapi->tx_cons ||
Matt Carlson8d9d7cf2009-09-01 13:19:05 +0000884 *(tnapi->rx_rcb_prod_idx) != tnapi->rx_rcb_ptr)
Michael Chan04237dd2005-04-25 15:17:17 -0700885 work_exists = 1;
886
887 return work_exists;
888}
889
Matt Carlson17375d22009-08-28 14:02:18 +0000890/* tg3_int_reenable
Michael Chan04237dd2005-04-25 15:17:17 -0700891 * similar to tg3_enable_ints, but it accurately determines whether there
892 * is new work pending and can return without flushing the PIO write
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400893 * which reenables interrupts
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 */
Matt Carlson17375d22009-08-28 14:02:18 +0000895static void tg3_int_reenable(struct tg3_napi *tnapi)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896{
Matt Carlson17375d22009-08-28 14:02:18 +0000897 struct tg3 *tp = tnapi->tp;
898
Matt Carlson898a56f2009-08-28 14:02:40 +0000899 tw32_mailbox(tnapi->int_mbox, tnapi->last_tag << 24);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 mmiowb();
901
David S. Millerfac9b832005-05-18 22:46:34 -0700902 /* When doing tagged status, this work check is unnecessary.
903 * The last_tag we write above tells the chip which piece of
904 * work we've completed.
905 */
Joe Perches63c3a662011-04-26 08:12:10 +0000906 if (!tg3_flag(tp, TAGGED_STATUS) && tg3_has_work(tnapi))
Michael Chan04237dd2005-04-25 15:17:17 -0700907 tw32(HOSTCC_MODE, tp->coalesce_mode |
Matt Carlsonfd2ce372009-09-01 12:51:13 +0000908 HOSTCC_MODE_ENABLE | tnapi->coal_now);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909}
910
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911static void tg3_switch_clocks(struct tg3 *tp)
912{
Matt Carlsonf6eb9b12009-09-01 13:19:53 +0000913 u32 clock_ctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 u32 orig_clock_ctrl;
915
Joe Perches63c3a662011-04-26 08:12:10 +0000916 if (tg3_flag(tp, CPMU_PRESENT) || tg3_flag(tp, 5780_CLASS))
Michael Chan4cf78e42005-07-25 12:29:19 -0700917 return;
918
Matt Carlsonf6eb9b12009-09-01 13:19:53 +0000919 clock_ctrl = tr32(TG3PCI_CLOCK_CTRL);
920
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 orig_clock_ctrl = clock_ctrl;
922 clock_ctrl &= (CLOCK_CTRL_FORCE_CLKRUN |
923 CLOCK_CTRL_CLKRUN_OENABLE |
924 0x1f);
925 tp->pci_clock_ctrl = clock_ctrl;
926
Joe Perches63c3a662011-04-26 08:12:10 +0000927 if (tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 if (orig_clock_ctrl & CLOCK_CTRL_625_CORE) {
Michael Chanb401e9e2005-12-19 16:27:04 -0800929 tw32_wait_f(TG3PCI_CLOCK_CTRL,
930 clock_ctrl | CLOCK_CTRL_625_CORE, 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 }
932 } else if ((orig_clock_ctrl & CLOCK_CTRL_44MHZ_CORE) != 0) {
Michael Chanb401e9e2005-12-19 16:27:04 -0800933 tw32_wait_f(TG3PCI_CLOCK_CTRL,
934 clock_ctrl |
935 (CLOCK_CTRL_44MHZ_CORE | CLOCK_CTRL_ALTCLK),
936 40);
937 tw32_wait_f(TG3PCI_CLOCK_CTRL,
938 clock_ctrl | (CLOCK_CTRL_ALTCLK),
939 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 }
Michael Chanb401e9e2005-12-19 16:27:04 -0800941 tw32_wait_f(TG3PCI_CLOCK_CTRL, clock_ctrl, 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942}
943
944#define PHY_BUSY_LOOPS 5000
945
946static int tg3_readphy(struct tg3 *tp, int reg, u32 *val)
947{
948 u32 frame_val;
949 unsigned int loops;
950 int ret;
951
952 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
953 tw32_f(MAC_MI_MODE,
954 (tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL));
955 udelay(80);
956 }
957
958 *val = 0x0;
959
Matt Carlson882e9792009-09-01 13:21:36 +0000960 frame_val = ((tp->phy_addr << MI_COM_PHY_ADDR_SHIFT) &
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 MI_COM_PHY_ADDR_MASK);
962 frame_val |= ((reg << MI_COM_REG_ADDR_SHIFT) &
963 MI_COM_REG_ADDR_MASK);
964 frame_val |= (MI_COM_CMD_READ | MI_COM_START);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400965
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 tw32_f(MAC_MI_COM, frame_val);
967
968 loops = PHY_BUSY_LOOPS;
969 while (loops != 0) {
970 udelay(10);
971 frame_val = tr32(MAC_MI_COM);
972
973 if ((frame_val & MI_COM_BUSY) == 0) {
974 udelay(5);
975 frame_val = tr32(MAC_MI_COM);
976 break;
977 }
978 loops -= 1;
979 }
980
981 ret = -EBUSY;
982 if (loops != 0) {
983 *val = frame_val & MI_COM_DATA_MASK;
984 ret = 0;
985 }
986
987 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
988 tw32_f(MAC_MI_MODE, tp->mi_mode);
989 udelay(80);
990 }
991
992 return ret;
993}
994
995static int tg3_writephy(struct tg3 *tp, int reg, u32 val)
996{
997 u32 frame_val;
998 unsigned int loops;
999 int ret;
1000
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001001 if ((tp->phy_flags & TG3_PHYFLG_IS_FET) &&
Matt Carlson221c5632011-06-13 13:39:01 +00001002 (reg == MII_CTRL1000 || reg == MII_TG3_AUX_CTRL))
Michael Chanb5d37722006-09-27 16:06:21 -07001003 return 0;
1004
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
1006 tw32_f(MAC_MI_MODE,
1007 (tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL));
1008 udelay(80);
1009 }
1010
Matt Carlson882e9792009-09-01 13:21:36 +00001011 frame_val = ((tp->phy_addr << MI_COM_PHY_ADDR_SHIFT) &
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 MI_COM_PHY_ADDR_MASK);
1013 frame_val |= ((reg << MI_COM_REG_ADDR_SHIFT) &
1014 MI_COM_REG_ADDR_MASK);
1015 frame_val |= (val & MI_COM_DATA_MASK);
1016 frame_val |= (MI_COM_CMD_WRITE | MI_COM_START);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001017
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 tw32_f(MAC_MI_COM, frame_val);
1019
1020 loops = PHY_BUSY_LOOPS;
1021 while (loops != 0) {
1022 udelay(10);
1023 frame_val = tr32(MAC_MI_COM);
1024 if ((frame_val & MI_COM_BUSY) == 0) {
1025 udelay(5);
1026 frame_val = tr32(MAC_MI_COM);
1027 break;
1028 }
1029 loops -= 1;
1030 }
1031
1032 ret = -EBUSY;
1033 if (loops != 0)
1034 ret = 0;
1035
1036 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
1037 tw32_f(MAC_MI_MODE, tp->mi_mode);
1038 udelay(80);
1039 }
1040
1041 return ret;
1042}
1043
Matt Carlsonb0988c12011-04-20 07:57:39 +00001044static int tg3_phy_cl45_write(struct tg3 *tp, u32 devad, u32 addr, u32 val)
1045{
1046 int err;
1047
1048 err = tg3_writephy(tp, MII_TG3_MMD_CTRL, devad);
1049 if (err)
1050 goto done;
1051
1052 err = tg3_writephy(tp, MII_TG3_MMD_ADDRESS, addr);
1053 if (err)
1054 goto done;
1055
1056 err = tg3_writephy(tp, MII_TG3_MMD_CTRL,
1057 MII_TG3_MMD_CTRL_DATA_NOINC | devad);
1058 if (err)
1059 goto done;
1060
1061 err = tg3_writephy(tp, MII_TG3_MMD_ADDRESS, val);
1062
1063done:
1064 return err;
1065}
1066
1067static int tg3_phy_cl45_read(struct tg3 *tp, u32 devad, u32 addr, u32 *val)
1068{
1069 int err;
1070
1071 err = tg3_writephy(tp, MII_TG3_MMD_CTRL, devad);
1072 if (err)
1073 goto done;
1074
1075 err = tg3_writephy(tp, MII_TG3_MMD_ADDRESS, addr);
1076 if (err)
1077 goto done;
1078
1079 err = tg3_writephy(tp, MII_TG3_MMD_CTRL,
1080 MII_TG3_MMD_CTRL_DATA_NOINC | devad);
1081 if (err)
1082 goto done;
1083
1084 err = tg3_readphy(tp, MII_TG3_MMD_ADDRESS, val);
1085
1086done:
1087 return err;
1088}
1089
1090static int tg3_phydsp_read(struct tg3 *tp, u32 reg, u32 *val)
1091{
1092 int err;
1093
1094 err = tg3_writephy(tp, MII_TG3_DSP_ADDRESS, reg);
1095 if (!err)
1096 err = tg3_readphy(tp, MII_TG3_DSP_RW_PORT, val);
1097
1098 return err;
1099}
1100
1101static int tg3_phydsp_write(struct tg3 *tp, u32 reg, u32 val)
1102{
1103 int err;
1104
1105 err = tg3_writephy(tp, MII_TG3_DSP_ADDRESS, reg);
1106 if (!err)
1107 err = tg3_writephy(tp, MII_TG3_DSP_RW_PORT, val);
1108
1109 return err;
1110}
1111
Matt Carlson15ee95c2011-04-20 07:57:40 +00001112static int tg3_phy_auxctl_read(struct tg3 *tp, int reg, u32 *val)
1113{
1114 int err;
1115
1116 err = tg3_writephy(tp, MII_TG3_AUX_CTRL,
1117 (reg << MII_TG3_AUXCTL_MISC_RDSEL_SHIFT) |
1118 MII_TG3_AUXCTL_SHDWSEL_MISC);
1119 if (!err)
1120 err = tg3_readphy(tp, MII_TG3_AUX_CTRL, val);
1121
1122 return err;
1123}
1124
Matt Carlsonb4bd2922011-04-20 07:57:41 +00001125static int tg3_phy_auxctl_write(struct tg3 *tp, int reg, u32 set)
1126{
1127 if (reg == MII_TG3_AUXCTL_SHDWSEL_MISC)
1128 set |= MII_TG3_AUXCTL_MISC_WREN;
1129
1130 return tg3_writephy(tp, MII_TG3_AUX_CTRL, set | reg);
1131}
1132
Matt Carlson1d36ba42011-04-20 07:57:42 +00001133#define TG3_PHY_AUXCTL_SMDSP_ENABLE(tp) \
1134 tg3_phy_auxctl_write((tp), MII_TG3_AUXCTL_SHDWSEL_AUXCTL, \
1135 MII_TG3_AUXCTL_ACTL_SMDSP_ENA | \
1136 MII_TG3_AUXCTL_ACTL_TX_6DB)
1137
1138#define TG3_PHY_AUXCTL_SMDSP_DISABLE(tp) \
1139 tg3_phy_auxctl_write((tp), MII_TG3_AUXCTL_SHDWSEL_AUXCTL, \
1140 MII_TG3_AUXCTL_ACTL_TX_6DB);
1141
Matt Carlson95e28692008-05-25 23:44:14 -07001142static int tg3_bmcr_reset(struct tg3 *tp)
1143{
1144 u32 phy_control;
1145 int limit, err;
1146
1147 /* OK, reset it, and poll the BMCR_RESET bit until it
1148 * clears or we time out.
1149 */
1150 phy_control = BMCR_RESET;
1151 err = tg3_writephy(tp, MII_BMCR, phy_control);
1152 if (err != 0)
1153 return -EBUSY;
1154
1155 limit = 5000;
1156 while (limit--) {
1157 err = tg3_readphy(tp, MII_BMCR, &phy_control);
1158 if (err != 0)
1159 return -EBUSY;
1160
1161 if ((phy_control & BMCR_RESET) == 0) {
1162 udelay(40);
1163 break;
1164 }
1165 udelay(10);
1166 }
Roel Kluind4675b52009-02-12 16:33:27 -08001167 if (limit < 0)
Matt Carlson95e28692008-05-25 23:44:14 -07001168 return -EBUSY;
1169
1170 return 0;
1171}
1172
Matt Carlson158d7ab2008-05-29 01:37:54 -07001173static int tg3_mdio_read(struct mii_bus *bp, int mii_id, int reg)
1174{
Francois Romieu3d165432009-01-19 16:56:50 -08001175 struct tg3 *tp = bp->priv;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001176 u32 val;
1177
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001178 spin_lock_bh(&tp->lock);
Matt Carlson158d7ab2008-05-29 01:37:54 -07001179
1180 if (tg3_readphy(tp, reg, &val))
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001181 val = -EIO;
1182
1183 spin_unlock_bh(&tp->lock);
Matt Carlson158d7ab2008-05-29 01:37:54 -07001184
1185 return val;
1186}
1187
1188static int tg3_mdio_write(struct mii_bus *bp, int mii_id, int reg, u16 val)
1189{
Francois Romieu3d165432009-01-19 16:56:50 -08001190 struct tg3 *tp = bp->priv;
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001191 u32 ret = 0;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001192
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001193 spin_lock_bh(&tp->lock);
Matt Carlson158d7ab2008-05-29 01:37:54 -07001194
1195 if (tg3_writephy(tp, reg, val))
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001196 ret = -EIO;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001197
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001198 spin_unlock_bh(&tp->lock);
1199
1200 return ret;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001201}
1202
1203static int tg3_mdio_reset(struct mii_bus *bp)
1204{
1205 return 0;
1206}
1207
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001208static void tg3_mdio_config_5785(struct tg3 *tp)
Matt Carlsona9daf362008-05-25 23:49:44 -07001209{
1210 u32 val;
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001211 struct phy_device *phydev;
Matt Carlsona9daf362008-05-25 23:49:44 -07001212
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001213 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001214 switch (phydev->drv->phy_id & phydev->drv->phy_id_mask) {
Matt Carlson6a443a02010-02-17 15:17:04 +00001215 case PHY_ID_BCM50610:
1216 case PHY_ID_BCM50610M:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001217 val = MAC_PHYCFG2_50610_LED_MODES;
1218 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001219 case PHY_ID_BCMAC131:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001220 val = MAC_PHYCFG2_AC131_LED_MODES;
1221 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001222 case PHY_ID_RTL8211C:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001223 val = MAC_PHYCFG2_RTL8211C_LED_MODES;
1224 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001225 case PHY_ID_RTL8201E:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001226 val = MAC_PHYCFG2_RTL8201E_LED_MODES;
1227 break;
1228 default:
Matt Carlsona9daf362008-05-25 23:49:44 -07001229 return;
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001230 }
1231
1232 if (phydev->interface != PHY_INTERFACE_MODE_RGMII) {
1233 tw32(MAC_PHYCFG2, val);
1234
1235 val = tr32(MAC_PHYCFG1);
Matt Carlsonbb85fbb2009-08-25 10:09:07 +00001236 val &= ~(MAC_PHYCFG1_RGMII_INT |
1237 MAC_PHYCFG1_RXCLK_TO_MASK | MAC_PHYCFG1_TXCLK_TO_MASK);
1238 val |= MAC_PHYCFG1_RXCLK_TIMEOUT | MAC_PHYCFG1_TXCLK_TIMEOUT;
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001239 tw32(MAC_PHYCFG1, val);
1240
1241 return;
1242 }
1243
Joe Perches63c3a662011-04-26 08:12:10 +00001244 if (!tg3_flag(tp, RGMII_INBAND_DISABLE))
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001245 val |= MAC_PHYCFG2_EMODE_MASK_MASK |
1246 MAC_PHYCFG2_FMODE_MASK_MASK |
1247 MAC_PHYCFG2_GMODE_MASK_MASK |
1248 MAC_PHYCFG2_ACT_MASK_MASK |
1249 MAC_PHYCFG2_QUAL_MASK_MASK |
1250 MAC_PHYCFG2_INBAND_ENABLE;
1251
1252 tw32(MAC_PHYCFG2, val);
Matt Carlsona9daf362008-05-25 23:49:44 -07001253
Matt Carlsonbb85fbb2009-08-25 10:09:07 +00001254 val = tr32(MAC_PHYCFG1);
1255 val &= ~(MAC_PHYCFG1_RXCLK_TO_MASK | MAC_PHYCFG1_TXCLK_TO_MASK |
1256 MAC_PHYCFG1_RGMII_EXT_RX_DEC | MAC_PHYCFG1_RGMII_SND_STAT_EN);
Joe Perches63c3a662011-04-26 08:12:10 +00001257 if (!tg3_flag(tp, RGMII_INBAND_DISABLE)) {
1258 if (tg3_flag(tp, RGMII_EXT_IBND_RX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001259 val |= MAC_PHYCFG1_RGMII_EXT_RX_DEC;
Joe Perches63c3a662011-04-26 08:12:10 +00001260 if (tg3_flag(tp, RGMII_EXT_IBND_TX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001261 val |= MAC_PHYCFG1_RGMII_SND_STAT_EN;
1262 }
Matt Carlsonbb85fbb2009-08-25 10:09:07 +00001263 val |= MAC_PHYCFG1_RXCLK_TIMEOUT | MAC_PHYCFG1_TXCLK_TIMEOUT |
1264 MAC_PHYCFG1_RGMII_INT | MAC_PHYCFG1_TXC_DRV;
1265 tw32(MAC_PHYCFG1, val);
Matt Carlsona9daf362008-05-25 23:49:44 -07001266
Matt Carlsona9daf362008-05-25 23:49:44 -07001267 val = tr32(MAC_EXT_RGMII_MODE);
1268 val &= ~(MAC_RGMII_MODE_RX_INT_B |
1269 MAC_RGMII_MODE_RX_QUALITY |
1270 MAC_RGMII_MODE_RX_ACTIVITY |
1271 MAC_RGMII_MODE_RX_ENG_DET |
1272 MAC_RGMII_MODE_TX_ENABLE |
1273 MAC_RGMII_MODE_TX_LOWPWR |
1274 MAC_RGMII_MODE_TX_RESET);
Joe Perches63c3a662011-04-26 08:12:10 +00001275 if (!tg3_flag(tp, RGMII_INBAND_DISABLE)) {
1276 if (tg3_flag(tp, RGMII_EXT_IBND_RX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001277 val |= MAC_RGMII_MODE_RX_INT_B |
1278 MAC_RGMII_MODE_RX_QUALITY |
1279 MAC_RGMII_MODE_RX_ACTIVITY |
1280 MAC_RGMII_MODE_RX_ENG_DET;
Joe Perches63c3a662011-04-26 08:12:10 +00001281 if (tg3_flag(tp, RGMII_EXT_IBND_TX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001282 val |= MAC_RGMII_MODE_TX_ENABLE |
1283 MAC_RGMII_MODE_TX_LOWPWR |
1284 MAC_RGMII_MODE_TX_RESET;
1285 }
1286 tw32(MAC_EXT_RGMII_MODE, val);
1287}
1288
Matt Carlson158d7ab2008-05-29 01:37:54 -07001289static void tg3_mdio_start(struct tg3 *tp)
1290{
Matt Carlson158d7ab2008-05-29 01:37:54 -07001291 tp->mi_mode &= ~MAC_MI_MODE_AUTO_POLL;
1292 tw32_f(MAC_MI_MODE, tp->mi_mode);
1293 udelay(80);
Matt Carlsona9daf362008-05-25 23:49:44 -07001294
Joe Perches63c3a662011-04-26 08:12:10 +00001295 if (tg3_flag(tp, MDIOBUS_INITED) &&
Matt Carlson9ea48182010-02-17 15:17:01 +00001296 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
1297 tg3_mdio_config_5785(tp);
1298}
1299
1300static int tg3_mdio_init(struct tg3 *tp)
1301{
1302 int i;
1303 u32 reg;
1304 struct phy_device *phydev;
1305
Joe Perches63c3a662011-04-26 08:12:10 +00001306 if (tg3_flag(tp, 5717_PLUS)) {
Matt Carlson9c7df912010-06-05 17:24:36 +00001307 u32 is_serdes;
Matt Carlson882e9792009-09-01 13:21:36 +00001308
Matt Carlson69f11c92011-07-13 09:27:30 +00001309 tp->phy_addr = tp->pci_fn + 1;
Matt Carlson882e9792009-09-01 13:21:36 +00001310
Matt Carlsond1ec96a2010-01-12 10:11:38 +00001311 if (tp->pci_chip_rev_id != CHIPREV_ID_5717_A0)
1312 is_serdes = tr32(SG_DIG_STATUS) & SG_DIG_IS_SERDES;
1313 else
1314 is_serdes = tr32(TG3_CPMU_PHY_STRAP) &
1315 TG3_CPMU_PHY_STRAP_IS_SERDES;
Matt Carlson882e9792009-09-01 13:21:36 +00001316 if (is_serdes)
1317 tp->phy_addr += 7;
1318 } else
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001319 tp->phy_addr = TG3_PHY_MII_ADDR;
Matt Carlson882e9792009-09-01 13:21:36 +00001320
Matt Carlson158d7ab2008-05-29 01:37:54 -07001321 tg3_mdio_start(tp);
1322
Joe Perches63c3a662011-04-26 08:12:10 +00001323 if (!tg3_flag(tp, USE_PHYLIB) || tg3_flag(tp, MDIOBUS_INITED))
Matt Carlson158d7ab2008-05-29 01:37:54 -07001324 return 0;
1325
Lennert Buytenhek298cf9be2008-10-08 16:29:57 -07001326 tp->mdio_bus = mdiobus_alloc();
1327 if (tp->mdio_bus == NULL)
1328 return -ENOMEM;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001329
Lennert Buytenhek298cf9be2008-10-08 16:29:57 -07001330 tp->mdio_bus->name = "tg3 mdio bus";
1331 snprintf(tp->mdio_bus->id, MII_BUS_ID_SIZE, "%x",
Matt Carlson158d7ab2008-05-29 01:37:54 -07001332 (tp->pdev->bus->number << 8) | tp->pdev->devfn);
Lennert Buytenhek298cf9be2008-10-08 16:29:57 -07001333 tp->mdio_bus->priv = tp;
1334 tp->mdio_bus->parent = &tp->pdev->dev;
1335 tp->mdio_bus->read = &tg3_mdio_read;
1336 tp->mdio_bus->write = &tg3_mdio_write;
1337 tp->mdio_bus->reset = &tg3_mdio_reset;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001338 tp->mdio_bus->phy_mask = ~(1 << TG3_PHY_MII_ADDR);
Lennert Buytenhek298cf9be2008-10-08 16:29:57 -07001339 tp->mdio_bus->irq = &tp->mdio_irq[0];
Matt Carlson158d7ab2008-05-29 01:37:54 -07001340
1341 for (i = 0; i < PHY_MAX_ADDR; i++)
Lennert Buytenhek298cf9be2008-10-08 16:29:57 -07001342 tp->mdio_bus->irq[i] = PHY_POLL;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001343
1344 /* The bus registration will look for all the PHYs on the mdio bus.
1345 * Unfortunately, it does not ensure the PHY is powered up before
1346 * accessing the PHY ID registers. A chip reset is the
1347 * quickest way to bring the device back to an operational state..
1348 */
1349 if (tg3_readphy(tp, MII_BMCR, &reg) || (reg & BMCR_PDOWN))
1350 tg3_bmcr_reset(tp);
1351
Lennert Buytenhek298cf9be2008-10-08 16:29:57 -07001352 i = mdiobus_register(tp->mdio_bus);
Matt Carlsona9daf362008-05-25 23:49:44 -07001353 if (i) {
Matt Carlsonab96b242010-04-05 10:19:22 +00001354 dev_warn(&tp->pdev->dev, "mdiobus_reg failed (0x%x)\n", i);
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001355 mdiobus_free(tp->mdio_bus);
Matt Carlsona9daf362008-05-25 23:49:44 -07001356 return i;
1357 }
Matt Carlson158d7ab2008-05-29 01:37:54 -07001358
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001359 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsona9daf362008-05-25 23:49:44 -07001360
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001361 if (!phydev || !phydev->drv) {
Matt Carlsonab96b242010-04-05 10:19:22 +00001362 dev_warn(&tp->pdev->dev, "No PHY devices\n");
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001363 mdiobus_unregister(tp->mdio_bus);
1364 mdiobus_free(tp->mdio_bus);
1365 return -ENODEV;
1366 }
1367
1368 switch (phydev->drv->phy_id & phydev->drv->phy_id_mask) {
Matt Carlson6a443a02010-02-17 15:17:04 +00001369 case PHY_ID_BCM57780:
Matt Carlson321d32a2008-11-21 17:22:19 -08001370 phydev->interface = PHY_INTERFACE_MODE_GMII;
Matt Carlsonc704dc22009-11-02 14:32:12 +00001371 phydev->dev_flags |= PHY_BRCM_AUTO_PWRDWN_ENABLE;
Matt Carlson321d32a2008-11-21 17:22:19 -08001372 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001373 case PHY_ID_BCM50610:
1374 case PHY_ID_BCM50610M:
Matt Carlson32e5a8d2009-11-02 14:31:39 +00001375 phydev->dev_flags |= PHY_BRCM_CLEAR_RGMII_MODE |
Matt Carlsonc704dc22009-11-02 14:32:12 +00001376 PHY_BRCM_RX_REFCLK_UNUSED |
Matt Carlson52fae082009-11-02 14:32:38 +00001377 PHY_BRCM_DIS_TXCRXC_NOENRGY |
Matt Carlsonc704dc22009-11-02 14:32:12 +00001378 PHY_BRCM_AUTO_PWRDWN_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00001379 if (tg3_flag(tp, RGMII_INBAND_DISABLE))
Matt Carlsona9daf362008-05-25 23:49:44 -07001380 phydev->dev_flags |= PHY_BRCM_STD_IBND_DISABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00001381 if (tg3_flag(tp, RGMII_EXT_IBND_RX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001382 phydev->dev_flags |= PHY_BRCM_EXT_IBND_RX_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00001383 if (tg3_flag(tp, RGMII_EXT_IBND_TX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001384 phydev->dev_flags |= PHY_BRCM_EXT_IBND_TX_ENABLE;
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001385 /* fallthru */
Matt Carlson6a443a02010-02-17 15:17:04 +00001386 case PHY_ID_RTL8211C:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001387 phydev->interface = PHY_INTERFACE_MODE_RGMII;
Matt Carlsona9daf362008-05-25 23:49:44 -07001388 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001389 case PHY_ID_RTL8201E:
1390 case PHY_ID_BCMAC131:
Matt Carlsona9daf362008-05-25 23:49:44 -07001391 phydev->interface = PHY_INTERFACE_MODE_MII;
Matt Carlsoncdd4e09d2009-11-02 14:31:11 +00001392 phydev->dev_flags |= PHY_BRCM_AUTO_PWRDWN_ENABLE;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001393 tp->phy_flags |= TG3_PHYFLG_IS_FET;
Matt Carlsona9daf362008-05-25 23:49:44 -07001394 break;
1395 }
1396
Joe Perches63c3a662011-04-26 08:12:10 +00001397 tg3_flag_set(tp, MDIOBUS_INITED);
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001398
1399 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
1400 tg3_mdio_config_5785(tp);
Matt Carlsona9daf362008-05-25 23:49:44 -07001401
1402 return 0;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001403}
1404
1405static void tg3_mdio_fini(struct tg3 *tp)
1406{
Joe Perches63c3a662011-04-26 08:12:10 +00001407 if (tg3_flag(tp, MDIOBUS_INITED)) {
1408 tg3_flag_clear(tp, MDIOBUS_INITED);
Lennert Buytenhek298cf9be2008-10-08 16:29:57 -07001409 mdiobus_unregister(tp->mdio_bus);
1410 mdiobus_free(tp->mdio_bus);
Matt Carlson158d7ab2008-05-29 01:37:54 -07001411 }
1412}
1413
Matt Carlson95e28692008-05-25 23:44:14 -07001414/* tp->lock is held. */
Matt Carlson4ba526c2008-08-15 14:10:04 -07001415static inline void tg3_generate_fw_event(struct tg3 *tp)
1416{
1417 u32 val;
1418
1419 val = tr32(GRC_RX_CPU_EVENT);
1420 val |= GRC_RX_CPU_DRIVER_EVENT;
1421 tw32_f(GRC_RX_CPU_EVENT, val);
1422
1423 tp->last_event_jiffies = jiffies;
1424}
1425
1426#define TG3_FW_EVENT_TIMEOUT_USEC 2500
1427
1428/* tp->lock is held. */
Matt Carlson95e28692008-05-25 23:44:14 -07001429static void tg3_wait_for_event_ack(struct tg3 *tp)
1430{
1431 int i;
Matt Carlson4ba526c2008-08-15 14:10:04 -07001432 unsigned int delay_cnt;
1433 long time_remain;
Matt Carlson95e28692008-05-25 23:44:14 -07001434
Matt Carlson4ba526c2008-08-15 14:10:04 -07001435 /* If enough time has passed, no wait is necessary. */
1436 time_remain = (long)(tp->last_event_jiffies + 1 +
1437 usecs_to_jiffies(TG3_FW_EVENT_TIMEOUT_USEC)) -
1438 (long)jiffies;
1439 if (time_remain < 0)
1440 return;
1441
1442 /* Check if we can shorten the wait time. */
1443 delay_cnt = jiffies_to_usecs(time_remain);
1444 if (delay_cnt > TG3_FW_EVENT_TIMEOUT_USEC)
1445 delay_cnt = TG3_FW_EVENT_TIMEOUT_USEC;
1446 delay_cnt = (delay_cnt >> 3) + 1;
1447
1448 for (i = 0; i < delay_cnt; i++) {
Matt Carlson95e28692008-05-25 23:44:14 -07001449 if (!(tr32(GRC_RX_CPU_EVENT) & GRC_RX_CPU_DRIVER_EVENT))
1450 break;
Matt Carlson4ba526c2008-08-15 14:10:04 -07001451 udelay(8);
Matt Carlson95e28692008-05-25 23:44:14 -07001452 }
1453}
1454
1455/* tp->lock is held. */
1456static void tg3_ump_link_report(struct tg3 *tp)
1457{
1458 u32 reg;
1459 u32 val;
1460
Joe Perches63c3a662011-04-26 08:12:10 +00001461 if (!tg3_flag(tp, 5780_CLASS) || !tg3_flag(tp, ENABLE_ASF))
Matt Carlson95e28692008-05-25 23:44:14 -07001462 return;
1463
1464 tg3_wait_for_event_ack(tp);
1465
1466 tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX, FWCMD_NICDRV_LINK_UPDATE);
1467
1468 tg3_write_mem(tp, NIC_SRAM_FW_CMD_LEN_MBOX, 14);
1469
1470 val = 0;
1471 if (!tg3_readphy(tp, MII_BMCR, &reg))
1472 val = reg << 16;
1473 if (!tg3_readphy(tp, MII_BMSR, &reg))
1474 val |= (reg & 0xffff);
1475 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX, val);
1476
1477 val = 0;
1478 if (!tg3_readphy(tp, MII_ADVERTISE, &reg))
1479 val = reg << 16;
1480 if (!tg3_readphy(tp, MII_LPA, &reg))
1481 val |= (reg & 0xffff);
1482 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 4, val);
1483
1484 val = 0;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001485 if (!(tp->phy_flags & TG3_PHYFLG_MII_SERDES)) {
Matt Carlson95e28692008-05-25 23:44:14 -07001486 if (!tg3_readphy(tp, MII_CTRL1000, &reg))
1487 val = reg << 16;
1488 if (!tg3_readphy(tp, MII_STAT1000, &reg))
1489 val |= (reg & 0xffff);
1490 }
1491 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 8, val);
1492
1493 if (!tg3_readphy(tp, MII_PHYADDR, &reg))
1494 val = reg << 16;
1495 else
1496 val = 0;
1497 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 12, val);
1498
Matt Carlson4ba526c2008-08-15 14:10:04 -07001499 tg3_generate_fw_event(tp);
Matt Carlson95e28692008-05-25 23:44:14 -07001500}
1501
Matt Carlson8d5a89b2011-08-31 11:44:51 +00001502/* tp->lock is held. */
1503static void tg3_stop_fw(struct tg3 *tp)
1504{
1505 if (tg3_flag(tp, ENABLE_ASF) && !tg3_flag(tp, ENABLE_APE)) {
1506 /* Wait for RX cpu to ACK the previous event. */
1507 tg3_wait_for_event_ack(tp);
1508
1509 tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX, FWCMD_NICDRV_PAUSE_FW);
1510
1511 tg3_generate_fw_event(tp);
1512
1513 /* Wait for RX cpu to ACK this event. */
1514 tg3_wait_for_event_ack(tp);
1515 }
1516}
1517
Matt Carlsonfd6d3f02011-08-31 11:44:52 +00001518/* tp->lock is held. */
1519static void tg3_write_sig_pre_reset(struct tg3 *tp, int kind)
1520{
1521 tg3_write_mem(tp, NIC_SRAM_FIRMWARE_MBOX,
1522 NIC_SRAM_FIRMWARE_MBOX_MAGIC1);
1523
1524 if (tg3_flag(tp, ASF_NEW_HANDSHAKE)) {
1525 switch (kind) {
1526 case RESET_KIND_INIT:
1527 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1528 DRV_STATE_START);
1529 break;
1530
1531 case RESET_KIND_SHUTDOWN:
1532 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1533 DRV_STATE_UNLOAD);
1534 break;
1535
1536 case RESET_KIND_SUSPEND:
1537 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1538 DRV_STATE_SUSPEND);
1539 break;
1540
1541 default:
1542 break;
1543 }
1544 }
1545
1546 if (kind == RESET_KIND_INIT ||
1547 kind == RESET_KIND_SUSPEND)
1548 tg3_ape_driver_state_change(tp, kind);
1549}
1550
1551/* tp->lock is held. */
1552static void tg3_write_sig_post_reset(struct tg3 *tp, int kind)
1553{
1554 if (tg3_flag(tp, ASF_NEW_HANDSHAKE)) {
1555 switch (kind) {
1556 case RESET_KIND_INIT:
1557 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1558 DRV_STATE_START_DONE);
1559 break;
1560
1561 case RESET_KIND_SHUTDOWN:
1562 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1563 DRV_STATE_UNLOAD_DONE);
1564 break;
1565
1566 default:
1567 break;
1568 }
1569 }
1570
1571 if (kind == RESET_KIND_SHUTDOWN)
1572 tg3_ape_driver_state_change(tp, kind);
1573}
1574
1575/* tp->lock is held. */
1576static void tg3_write_sig_legacy(struct tg3 *tp, int kind)
1577{
1578 if (tg3_flag(tp, ENABLE_ASF)) {
1579 switch (kind) {
1580 case RESET_KIND_INIT:
1581 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1582 DRV_STATE_START);
1583 break;
1584
1585 case RESET_KIND_SHUTDOWN:
1586 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1587 DRV_STATE_UNLOAD);
1588 break;
1589
1590 case RESET_KIND_SUSPEND:
1591 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1592 DRV_STATE_SUSPEND);
1593 break;
1594
1595 default:
1596 break;
1597 }
1598 }
1599}
1600
1601static int tg3_poll_fw(struct tg3 *tp)
1602{
1603 int i;
1604 u32 val;
1605
1606 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
1607 /* Wait up to 20ms for init done. */
1608 for (i = 0; i < 200; i++) {
1609 if (tr32(VCPU_STATUS) & VCPU_STATUS_INIT_DONE)
1610 return 0;
1611 udelay(100);
1612 }
1613 return -ENODEV;
1614 }
1615
1616 /* Wait for firmware initialization to complete. */
1617 for (i = 0; i < 100000; i++) {
1618 tg3_read_mem(tp, NIC_SRAM_FIRMWARE_MBOX, &val);
1619 if (val == ~NIC_SRAM_FIRMWARE_MBOX_MAGIC1)
1620 break;
1621 udelay(10);
1622 }
1623
1624 /* Chip might not be fitted with firmware. Some Sun onboard
1625 * parts are configured like that. So don't signal the timeout
1626 * of the above loop as an error, but do report the lack of
1627 * running firmware once.
1628 */
1629 if (i >= 100000 && !tg3_flag(tp, NO_FWARE_REPORTED)) {
1630 tg3_flag_set(tp, NO_FWARE_REPORTED);
1631
1632 netdev_info(tp->dev, "No firmware running\n");
1633 }
1634
1635 if (tp->pci_chip_rev_id == CHIPREV_ID_57765_A0) {
1636 /* The 57765 A0 needs a little more
1637 * time to do some important work.
1638 */
1639 mdelay(10);
1640 }
1641
1642 return 0;
1643}
1644
Matt Carlson95e28692008-05-25 23:44:14 -07001645static void tg3_link_report(struct tg3 *tp)
1646{
1647 if (!netif_carrier_ok(tp->dev)) {
Joe Perches05dbe002010-02-17 19:44:19 +00001648 netif_info(tp, link, tp->dev, "Link is down\n");
Matt Carlson95e28692008-05-25 23:44:14 -07001649 tg3_ump_link_report(tp);
1650 } else if (netif_msg_link(tp)) {
Joe Perches05dbe002010-02-17 19:44:19 +00001651 netdev_info(tp->dev, "Link is up at %d Mbps, %s duplex\n",
1652 (tp->link_config.active_speed == SPEED_1000 ?
1653 1000 :
1654 (tp->link_config.active_speed == SPEED_100 ?
1655 100 : 10)),
1656 (tp->link_config.active_duplex == DUPLEX_FULL ?
1657 "full" : "half"));
Matt Carlson95e28692008-05-25 23:44:14 -07001658
Joe Perches05dbe002010-02-17 19:44:19 +00001659 netdev_info(tp->dev, "Flow control is %s for TX and %s for RX\n",
1660 (tp->link_config.active_flowctrl & FLOW_CTRL_TX) ?
1661 "on" : "off",
1662 (tp->link_config.active_flowctrl & FLOW_CTRL_RX) ?
1663 "on" : "off");
Matt Carlson47007832011-04-20 07:57:43 +00001664
1665 if (tp->phy_flags & TG3_PHYFLG_EEE_CAP)
1666 netdev_info(tp->dev, "EEE is %s\n",
1667 tp->setlpicnt ? "enabled" : "disabled");
1668
Matt Carlson95e28692008-05-25 23:44:14 -07001669 tg3_ump_link_report(tp);
1670 }
1671}
1672
Matt Carlson95e28692008-05-25 23:44:14 -07001673static u16 tg3_advert_flowctrl_1000X(u8 flow_ctrl)
1674{
1675 u16 miireg;
1676
Steve Glendinninge18ce342008-12-16 02:00:00 -08001677 if ((flow_ctrl & FLOW_CTRL_TX) && (flow_ctrl & FLOW_CTRL_RX))
Matt Carlson95e28692008-05-25 23:44:14 -07001678 miireg = ADVERTISE_1000XPAUSE;
Steve Glendinninge18ce342008-12-16 02:00:00 -08001679 else if (flow_ctrl & FLOW_CTRL_TX)
Matt Carlson95e28692008-05-25 23:44:14 -07001680 miireg = ADVERTISE_1000XPSE_ASYM;
Steve Glendinninge18ce342008-12-16 02:00:00 -08001681 else if (flow_ctrl & FLOW_CTRL_RX)
Matt Carlson95e28692008-05-25 23:44:14 -07001682 miireg = ADVERTISE_1000XPAUSE | ADVERTISE_1000XPSE_ASYM;
1683 else
1684 miireg = 0;
1685
1686 return miireg;
1687}
1688
Matt Carlson95e28692008-05-25 23:44:14 -07001689static u8 tg3_resolve_flowctrl_1000X(u16 lcladv, u16 rmtadv)
1690{
1691 u8 cap = 0;
1692
Matt Carlsonf3791cd2011-11-21 15:01:17 +00001693 if (lcladv & rmtadv & ADVERTISE_1000XPAUSE) {
1694 cap = FLOW_CTRL_TX | FLOW_CTRL_RX;
1695 } else if (lcladv & rmtadv & ADVERTISE_1000XPSE_ASYM) {
1696 if (lcladv & ADVERTISE_1000XPAUSE)
1697 cap = FLOW_CTRL_RX;
1698 if (rmtadv & ADVERTISE_1000XPAUSE)
Steve Glendinninge18ce342008-12-16 02:00:00 -08001699 cap = FLOW_CTRL_TX;
Matt Carlson95e28692008-05-25 23:44:14 -07001700 }
1701
1702 return cap;
1703}
1704
Matt Carlsonf51f3562008-05-25 23:45:08 -07001705static void tg3_setup_flow_control(struct tg3 *tp, u32 lcladv, u32 rmtadv)
Matt Carlson95e28692008-05-25 23:44:14 -07001706{
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001707 u8 autoneg;
Matt Carlsonf51f3562008-05-25 23:45:08 -07001708 u8 flowctrl = 0;
Matt Carlson95e28692008-05-25 23:44:14 -07001709 u32 old_rx_mode = tp->rx_mode;
1710 u32 old_tx_mode = tp->tx_mode;
1711
Joe Perches63c3a662011-04-26 08:12:10 +00001712 if (tg3_flag(tp, USE_PHYLIB))
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001713 autoneg = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]->autoneg;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001714 else
1715 autoneg = tp->link_config.autoneg;
1716
Joe Perches63c3a662011-04-26 08:12:10 +00001717 if (autoneg == AUTONEG_ENABLE && tg3_flag(tp, PAUSE_AUTONEG)) {
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001718 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
Matt Carlsonf51f3562008-05-25 23:45:08 -07001719 flowctrl = tg3_resolve_flowctrl_1000X(lcladv, rmtadv);
Matt Carlson95e28692008-05-25 23:44:14 -07001720 else
Steve Glendinningbc02ff92008-12-16 02:00:48 -08001721 flowctrl = mii_resolve_flowctrl_fdx(lcladv, rmtadv);
Matt Carlsonf51f3562008-05-25 23:45:08 -07001722 } else
1723 flowctrl = tp->link_config.flowctrl;
Matt Carlson95e28692008-05-25 23:44:14 -07001724
Matt Carlsonf51f3562008-05-25 23:45:08 -07001725 tp->link_config.active_flowctrl = flowctrl;
Matt Carlson95e28692008-05-25 23:44:14 -07001726
Steve Glendinninge18ce342008-12-16 02:00:00 -08001727 if (flowctrl & FLOW_CTRL_RX)
Matt Carlson95e28692008-05-25 23:44:14 -07001728 tp->rx_mode |= RX_MODE_FLOW_CTRL_ENABLE;
1729 else
1730 tp->rx_mode &= ~RX_MODE_FLOW_CTRL_ENABLE;
1731
Matt Carlsonf51f3562008-05-25 23:45:08 -07001732 if (old_rx_mode != tp->rx_mode)
Matt Carlson95e28692008-05-25 23:44:14 -07001733 tw32_f(MAC_RX_MODE, tp->rx_mode);
Matt Carlson95e28692008-05-25 23:44:14 -07001734
Steve Glendinninge18ce342008-12-16 02:00:00 -08001735 if (flowctrl & FLOW_CTRL_TX)
Matt Carlson95e28692008-05-25 23:44:14 -07001736 tp->tx_mode |= TX_MODE_FLOW_CTRL_ENABLE;
1737 else
1738 tp->tx_mode &= ~TX_MODE_FLOW_CTRL_ENABLE;
1739
Matt Carlsonf51f3562008-05-25 23:45:08 -07001740 if (old_tx_mode != tp->tx_mode)
Matt Carlson95e28692008-05-25 23:44:14 -07001741 tw32_f(MAC_TX_MODE, tp->tx_mode);
Matt Carlson95e28692008-05-25 23:44:14 -07001742}
1743
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001744static void tg3_adjust_link(struct net_device *dev)
1745{
1746 u8 oldflowctrl, linkmesg = 0;
1747 u32 mac_mode, lcl_adv, rmt_adv;
1748 struct tg3 *tp = netdev_priv(dev);
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001749 struct phy_device *phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001750
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001751 spin_lock_bh(&tp->lock);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001752
1753 mac_mode = tp->mac_mode & ~(MAC_MODE_PORT_MODE_MASK |
1754 MAC_MODE_HALF_DUPLEX);
1755
1756 oldflowctrl = tp->link_config.active_flowctrl;
1757
1758 if (phydev->link) {
1759 lcl_adv = 0;
1760 rmt_adv = 0;
1761
1762 if (phydev->speed == SPEED_100 || phydev->speed == SPEED_10)
1763 mac_mode |= MAC_MODE_PORT_MODE_MII;
Matt Carlsonc3df0742009-11-02 14:27:02 +00001764 else if (phydev->speed == SPEED_1000 ||
1765 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785)
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001766 mac_mode |= MAC_MODE_PORT_MODE_GMII;
Matt Carlsonc3df0742009-11-02 14:27:02 +00001767 else
1768 mac_mode |= MAC_MODE_PORT_MODE_MII;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001769
1770 if (phydev->duplex == DUPLEX_HALF)
1771 mac_mode |= MAC_MODE_HALF_DUPLEX;
1772 else {
Matt Carlsonf88788f2011-12-14 11:10:00 +00001773 lcl_adv = mii_advertise_flowctrl(
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001774 tp->link_config.flowctrl);
1775
1776 if (phydev->pause)
1777 rmt_adv = LPA_PAUSE_CAP;
1778 if (phydev->asym_pause)
1779 rmt_adv |= LPA_PAUSE_ASYM;
1780 }
1781
1782 tg3_setup_flow_control(tp, lcl_adv, rmt_adv);
1783 } else
1784 mac_mode |= MAC_MODE_PORT_MODE_GMII;
1785
1786 if (mac_mode != tp->mac_mode) {
1787 tp->mac_mode = mac_mode;
1788 tw32_f(MAC_MODE, tp->mac_mode);
1789 udelay(40);
1790 }
1791
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001792 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785) {
1793 if (phydev->speed == SPEED_10)
1794 tw32(MAC_MI_STAT,
1795 MAC_MI_STAT_10MBPS_MODE |
1796 MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
1797 else
1798 tw32(MAC_MI_STAT, MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
1799 }
1800
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001801 if (phydev->speed == SPEED_1000 && phydev->duplex == DUPLEX_HALF)
1802 tw32(MAC_TX_LENGTHS,
1803 ((2 << TX_LENGTHS_IPG_CRS_SHIFT) |
1804 (6 << TX_LENGTHS_IPG_SHIFT) |
1805 (0xff << TX_LENGTHS_SLOT_TIME_SHIFT)));
1806 else
1807 tw32(MAC_TX_LENGTHS,
1808 ((2 << TX_LENGTHS_IPG_CRS_SHIFT) |
1809 (6 << TX_LENGTHS_IPG_SHIFT) |
1810 (32 << TX_LENGTHS_SLOT_TIME_SHIFT)));
1811
1812 if ((phydev->link && tp->link_config.active_speed == SPEED_INVALID) ||
1813 (!phydev->link && tp->link_config.active_speed != SPEED_INVALID) ||
1814 phydev->speed != tp->link_config.active_speed ||
1815 phydev->duplex != tp->link_config.active_duplex ||
1816 oldflowctrl != tp->link_config.active_flowctrl)
Matt Carlsonc6cdf432010-04-05 10:19:26 +00001817 linkmesg = 1;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001818
1819 tp->link_config.active_speed = phydev->speed;
1820 tp->link_config.active_duplex = phydev->duplex;
1821
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001822 spin_unlock_bh(&tp->lock);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001823
1824 if (linkmesg)
1825 tg3_link_report(tp);
1826}
1827
1828static int tg3_phy_init(struct tg3 *tp)
1829{
1830 struct phy_device *phydev;
1831
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001832 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED)
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001833 return 0;
1834
1835 /* Bring the PHY back to a known state. */
1836 tg3_bmcr_reset(tp);
1837
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001838 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001839
1840 /* Attach the MAC to the PHY. */
Kay Sieversfb28ad32008-11-10 13:55:14 -08001841 phydev = phy_connect(tp->dev, dev_name(&phydev->dev), tg3_adjust_link,
Matt Carlsona9daf362008-05-25 23:49:44 -07001842 phydev->dev_flags, phydev->interface);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001843 if (IS_ERR(phydev)) {
Matt Carlsonab96b242010-04-05 10:19:22 +00001844 dev_err(&tp->pdev->dev, "Could not attach to PHY\n");
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001845 return PTR_ERR(phydev);
1846 }
1847
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001848 /* Mask with MAC supported features. */
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001849 switch (phydev->interface) {
1850 case PHY_INTERFACE_MODE_GMII:
1851 case PHY_INTERFACE_MODE_RGMII:
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001852 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
Matt Carlson321d32a2008-11-21 17:22:19 -08001853 phydev->supported &= (PHY_GBIT_FEATURES |
1854 SUPPORTED_Pause |
1855 SUPPORTED_Asym_Pause);
1856 break;
1857 }
1858 /* fallthru */
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001859 case PHY_INTERFACE_MODE_MII:
1860 phydev->supported &= (PHY_BASIC_FEATURES |
1861 SUPPORTED_Pause |
1862 SUPPORTED_Asym_Pause);
1863 break;
1864 default:
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001865 phy_disconnect(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001866 return -EINVAL;
1867 }
1868
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001869 tp->phy_flags |= TG3_PHYFLG_IS_CONNECTED;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001870
1871 phydev->advertising = phydev->supported;
1872
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001873 return 0;
1874}
1875
1876static void tg3_phy_start(struct tg3 *tp)
1877{
1878 struct phy_device *phydev;
1879
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001880 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001881 return;
1882
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001883 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001884
Matt Carlson800960682010-08-02 11:26:06 +00001885 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) {
1886 tp->phy_flags &= ~TG3_PHYFLG_IS_LOW_POWER;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001887 phydev->speed = tp->link_config.orig_speed;
1888 phydev->duplex = tp->link_config.orig_duplex;
1889 phydev->autoneg = tp->link_config.orig_autoneg;
1890 phydev->advertising = tp->link_config.orig_advertising;
1891 }
1892
1893 phy_start(phydev);
1894
1895 phy_start_aneg(phydev);
1896}
1897
1898static void tg3_phy_stop(struct tg3 *tp)
1899{
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001900 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001901 return;
1902
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001903 phy_stop(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001904}
1905
1906static void tg3_phy_fini(struct tg3 *tp)
1907{
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001908 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001909 phy_disconnect(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001910 tp->phy_flags &= ~TG3_PHYFLG_IS_CONNECTED;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001911 }
1912}
1913
Matt Carlson941ec902011-08-19 13:58:23 +00001914static int tg3_phy_set_extloopbk(struct tg3 *tp)
1915{
1916 int err;
1917 u32 val;
1918
1919 if (tp->phy_flags & TG3_PHYFLG_IS_FET)
1920 return 0;
1921
1922 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
1923 /* Cannot do read-modify-write on 5401 */
1924 err = tg3_phy_auxctl_write(tp,
1925 MII_TG3_AUXCTL_SHDWSEL_AUXCTL,
1926 MII_TG3_AUXCTL_ACTL_EXTLOOPBK |
1927 0x4c20);
1928 goto done;
1929 }
1930
1931 err = tg3_phy_auxctl_read(tp,
1932 MII_TG3_AUXCTL_SHDWSEL_AUXCTL, &val);
1933 if (err)
1934 return err;
1935
1936 val |= MII_TG3_AUXCTL_ACTL_EXTLOOPBK;
1937 err = tg3_phy_auxctl_write(tp,
1938 MII_TG3_AUXCTL_SHDWSEL_AUXCTL, val);
1939
1940done:
1941 return err;
1942}
1943
Matt Carlson7f97a4b2009-08-25 10:10:03 +00001944static void tg3_phy_fet_toggle_apd(struct tg3 *tp, bool enable)
1945{
1946 u32 phytest;
1947
1948 if (!tg3_readphy(tp, MII_TG3_FET_TEST, &phytest)) {
1949 u32 phy;
1950
1951 tg3_writephy(tp, MII_TG3_FET_TEST,
1952 phytest | MII_TG3_FET_SHADOW_EN);
1953 if (!tg3_readphy(tp, MII_TG3_FET_SHDW_AUXSTAT2, &phy)) {
1954 if (enable)
1955 phy |= MII_TG3_FET_SHDW_AUXSTAT2_APD;
1956 else
1957 phy &= ~MII_TG3_FET_SHDW_AUXSTAT2_APD;
1958 tg3_writephy(tp, MII_TG3_FET_SHDW_AUXSTAT2, phy);
1959 }
1960 tg3_writephy(tp, MII_TG3_FET_TEST, phytest);
1961 }
1962}
1963
Matt Carlson6833c042008-11-21 17:18:59 -08001964static void tg3_phy_toggle_apd(struct tg3 *tp, bool enable)
1965{
1966 u32 reg;
1967
Joe Perches63c3a662011-04-26 08:12:10 +00001968 if (!tg3_flag(tp, 5705_PLUS) ||
1969 (tg3_flag(tp, 5717_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001970 (tp->phy_flags & TG3_PHYFLG_MII_SERDES)))
Matt Carlson6833c042008-11-21 17:18:59 -08001971 return;
1972
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001973 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Matt Carlson7f97a4b2009-08-25 10:10:03 +00001974 tg3_phy_fet_toggle_apd(tp, enable);
1975 return;
1976 }
1977
Matt Carlson6833c042008-11-21 17:18:59 -08001978 reg = MII_TG3_MISC_SHDW_WREN |
1979 MII_TG3_MISC_SHDW_SCR5_SEL |
1980 MII_TG3_MISC_SHDW_SCR5_LPED |
1981 MII_TG3_MISC_SHDW_SCR5_DLPTLM |
1982 MII_TG3_MISC_SHDW_SCR5_SDTL |
1983 MII_TG3_MISC_SHDW_SCR5_C125OE;
1984 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5784 || !enable)
1985 reg |= MII_TG3_MISC_SHDW_SCR5_DLLAPD;
1986
1987 tg3_writephy(tp, MII_TG3_MISC_SHDW, reg);
1988
1989
1990 reg = MII_TG3_MISC_SHDW_WREN |
1991 MII_TG3_MISC_SHDW_APD_SEL |
1992 MII_TG3_MISC_SHDW_APD_WKTM_84MS;
1993 if (enable)
1994 reg |= MII_TG3_MISC_SHDW_APD_ENABLE;
1995
1996 tg3_writephy(tp, MII_TG3_MISC_SHDW, reg);
1997}
1998
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001999static void tg3_phy_toggle_automdix(struct tg3 *tp, int enable)
2000{
2001 u32 phy;
2002
Joe Perches63c3a662011-04-26 08:12:10 +00002003 if (!tg3_flag(tp, 5705_PLUS) ||
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002004 (tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002005 return;
2006
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002007 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002008 u32 ephy;
2009
Matt Carlson535ef6e2009-08-25 10:09:36 +00002010 if (!tg3_readphy(tp, MII_TG3_FET_TEST, &ephy)) {
2011 u32 reg = MII_TG3_FET_SHDW_MISCCTRL;
2012
2013 tg3_writephy(tp, MII_TG3_FET_TEST,
2014 ephy | MII_TG3_FET_SHADOW_EN);
2015 if (!tg3_readphy(tp, reg, &phy)) {
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002016 if (enable)
Matt Carlson535ef6e2009-08-25 10:09:36 +00002017 phy |= MII_TG3_FET_SHDW_MISCCTRL_MDIX;
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002018 else
Matt Carlson535ef6e2009-08-25 10:09:36 +00002019 phy &= ~MII_TG3_FET_SHDW_MISCCTRL_MDIX;
2020 tg3_writephy(tp, reg, phy);
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002021 }
Matt Carlson535ef6e2009-08-25 10:09:36 +00002022 tg3_writephy(tp, MII_TG3_FET_TEST, ephy);
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002023 }
2024 } else {
Matt Carlson15ee95c2011-04-20 07:57:40 +00002025 int ret;
2026
2027 ret = tg3_phy_auxctl_read(tp,
2028 MII_TG3_AUXCTL_SHDWSEL_MISC, &phy);
2029 if (!ret) {
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002030 if (enable)
2031 phy |= MII_TG3_AUXCTL_MISC_FORCE_AMDIX;
2032 else
2033 phy &= ~MII_TG3_AUXCTL_MISC_FORCE_AMDIX;
Matt Carlsonb4bd2922011-04-20 07:57:41 +00002034 tg3_phy_auxctl_write(tp,
2035 MII_TG3_AUXCTL_SHDWSEL_MISC, phy);
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002036 }
2037 }
2038}
2039
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040static void tg3_phy_set_wirespeed(struct tg3 *tp)
2041{
Matt Carlson15ee95c2011-04-20 07:57:40 +00002042 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043 u32 val;
2044
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002045 if (tp->phy_flags & TG3_PHYFLG_NO_ETH_WIRE_SPEED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046 return;
2047
Matt Carlson15ee95c2011-04-20 07:57:40 +00002048 ret = tg3_phy_auxctl_read(tp, MII_TG3_AUXCTL_SHDWSEL_MISC, &val);
2049 if (!ret)
Matt Carlsonb4bd2922011-04-20 07:57:41 +00002050 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_MISC,
2051 val | MII_TG3_AUXCTL_MISC_WIRESPD_EN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052}
2053
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002054static void tg3_phy_apply_otp(struct tg3 *tp)
2055{
2056 u32 otp, phy;
2057
2058 if (!tp->phy_otp)
2059 return;
2060
2061 otp = tp->phy_otp;
2062
Matt Carlson1d36ba42011-04-20 07:57:42 +00002063 if (TG3_PHY_AUXCTL_SMDSP_ENABLE(tp))
2064 return;
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002065
2066 phy = ((otp & TG3_OTP_AGCTGT_MASK) >> TG3_OTP_AGCTGT_SHIFT);
2067 phy |= MII_TG3_DSP_TAP1_AGCTGT_DFLT;
2068 tg3_phydsp_write(tp, MII_TG3_DSP_TAP1, phy);
2069
2070 phy = ((otp & TG3_OTP_HPFFLTR_MASK) >> TG3_OTP_HPFFLTR_SHIFT) |
2071 ((otp & TG3_OTP_HPFOVER_MASK) >> TG3_OTP_HPFOVER_SHIFT);
2072 tg3_phydsp_write(tp, MII_TG3_DSP_AADJ1CH0, phy);
2073
2074 phy = ((otp & TG3_OTP_LPFDIS_MASK) >> TG3_OTP_LPFDIS_SHIFT);
2075 phy |= MII_TG3_DSP_AADJ1CH3_ADCCKADJ;
2076 tg3_phydsp_write(tp, MII_TG3_DSP_AADJ1CH3, phy);
2077
2078 phy = ((otp & TG3_OTP_VDAC_MASK) >> TG3_OTP_VDAC_SHIFT);
2079 tg3_phydsp_write(tp, MII_TG3_DSP_EXP75, phy);
2080
2081 phy = ((otp & TG3_OTP_10BTAMP_MASK) >> TG3_OTP_10BTAMP_SHIFT);
2082 tg3_phydsp_write(tp, MII_TG3_DSP_EXP96, phy);
2083
2084 phy = ((otp & TG3_OTP_ROFF_MASK) >> TG3_OTP_ROFF_SHIFT) |
2085 ((otp & TG3_OTP_RCOFF_MASK) >> TG3_OTP_RCOFF_SHIFT);
2086 tg3_phydsp_write(tp, MII_TG3_DSP_EXP97, phy);
2087
Matt Carlson1d36ba42011-04-20 07:57:42 +00002088 TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002089}
2090
Matt Carlson52b02d02010-10-14 10:37:41 +00002091static void tg3_phy_eee_adjust(struct tg3 *tp, u32 current_link_up)
2092{
2093 u32 val;
2094
2095 if (!(tp->phy_flags & TG3_PHYFLG_EEE_CAP))
2096 return;
2097
2098 tp->setlpicnt = 0;
2099
2100 if (tp->link_config.autoneg == AUTONEG_ENABLE &&
2101 current_link_up == 1 &&
Matt Carlsona6b68da2010-12-06 08:28:52 +00002102 tp->link_config.active_duplex == DUPLEX_FULL &&
2103 (tp->link_config.active_speed == SPEED_100 ||
2104 tp->link_config.active_speed == SPEED_1000)) {
Matt Carlson52b02d02010-10-14 10:37:41 +00002105 u32 eeectl;
2106
2107 if (tp->link_config.active_speed == SPEED_1000)
2108 eeectl = TG3_CPMU_EEE_CTRL_EXIT_16_5_US;
2109 else
2110 eeectl = TG3_CPMU_EEE_CTRL_EXIT_36_US;
2111
2112 tw32(TG3_CPMU_EEE_CTRL, eeectl);
2113
Matt Carlson3110f5f52010-12-06 08:28:50 +00002114 tg3_phy_cl45_read(tp, MDIO_MMD_AN,
2115 TG3_CL45_D7_EEERES_STAT, &val);
Matt Carlson52b02d02010-10-14 10:37:41 +00002116
Matt Carlsonb0c59432011-05-19 12:12:48 +00002117 if (val == TG3_CL45_D7_EEERES_STAT_LP_1000T ||
2118 val == TG3_CL45_D7_EEERES_STAT_LP_100TX)
Matt Carlson52b02d02010-10-14 10:37:41 +00002119 tp->setlpicnt = 2;
2120 }
2121
2122 if (!tp->setlpicnt) {
Matt Carlsonb715ce92011-07-20 10:20:52 +00002123 if (current_link_up == 1 &&
2124 !TG3_PHY_AUXCTL_SMDSP_ENABLE(tp)) {
2125 tg3_phydsp_write(tp, MII_TG3_DSP_TAP26, 0x0000);
2126 TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
2127 }
2128
Matt Carlson52b02d02010-10-14 10:37:41 +00002129 val = tr32(TG3_CPMU_EEE_MODE);
2130 tw32(TG3_CPMU_EEE_MODE, val & ~TG3_CPMU_EEEMD_LPI_ENABLE);
2131 }
2132}
2133
Matt Carlsonb0c59432011-05-19 12:12:48 +00002134static void tg3_phy_eee_enable(struct tg3 *tp)
2135{
2136 u32 val;
2137
2138 if (tp->link_config.active_speed == SPEED_1000 &&
2139 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
2140 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
Matt Carlson55086ad2011-12-14 11:09:59 +00002141 tg3_flag(tp, 57765_CLASS)) &&
Matt Carlsonb0c59432011-05-19 12:12:48 +00002142 !TG3_PHY_AUXCTL_SMDSP_ENABLE(tp)) {
Matt Carlsonb715ce92011-07-20 10:20:52 +00002143 val = MII_TG3_DSP_TAP26_ALNOKO |
2144 MII_TG3_DSP_TAP26_RMRXSTO;
2145 tg3_phydsp_write(tp, MII_TG3_DSP_TAP26, val);
Matt Carlsonb0c59432011-05-19 12:12:48 +00002146 TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
2147 }
2148
2149 val = tr32(TG3_CPMU_EEE_MODE);
2150 tw32(TG3_CPMU_EEE_MODE, val | TG3_CPMU_EEEMD_LPI_ENABLE);
2151}
2152
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153static int tg3_wait_macro_done(struct tg3 *tp)
2154{
2155 int limit = 100;
2156
2157 while (limit--) {
2158 u32 tmp32;
2159
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002160 if (!tg3_readphy(tp, MII_TG3_DSP_CONTROL, &tmp32)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161 if ((tmp32 & 0x1000) == 0)
2162 break;
2163 }
2164 }
Roel Kluind4675b52009-02-12 16:33:27 -08002165 if (limit < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166 return -EBUSY;
2167
2168 return 0;
2169}
2170
2171static int tg3_phy_write_and_check_testpat(struct tg3 *tp, int *resetp)
2172{
2173 static const u32 test_pat[4][6] = {
2174 { 0x00005555, 0x00000005, 0x00002aaa, 0x0000000a, 0x00003456, 0x00000003 },
2175 { 0x00002aaa, 0x0000000a, 0x00003333, 0x00000003, 0x0000789a, 0x00000005 },
2176 { 0x00005a5a, 0x00000005, 0x00002a6a, 0x0000000a, 0x00001bcd, 0x00000003 },
2177 { 0x00002a5a, 0x0000000a, 0x000033c3, 0x00000003, 0x00002ef1, 0x00000005 }
2178 };
2179 int chan;
2180
2181 for (chan = 0; chan < 4; chan++) {
2182 int i;
2183
2184 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
2185 (chan * 0x2000) | 0x0200);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002186 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0002);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187
2188 for (i = 0; i < 6; i++)
2189 tg3_writephy(tp, MII_TG3_DSP_RW_PORT,
2190 test_pat[chan][i]);
2191
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002192 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0202);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193 if (tg3_wait_macro_done(tp)) {
2194 *resetp = 1;
2195 return -EBUSY;
2196 }
2197
2198 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
2199 (chan * 0x2000) | 0x0200);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002200 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0082);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201 if (tg3_wait_macro_done(tp)) {
2202 *resetp = 1;
2203 return -EBUSY;
2204 }
2205
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002206 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0802);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207 if (tg3_wait_macro_done(tp)) {
2208 *resetp = 1;
2209 return -EBUSY;
2210 }
2211
2212 for (i = 0; i < 6; i += 2) {
2213 u32 low, high;
2214
2215 if (tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &low) ||
2216 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &high) ||
2217 tg3_wait_macro_done(tp)) {
2218 *resetp = 1;
2219 return -EBUSY;
2220 }
2221 low &= 0x7fff;
2222 high &= 0x000f;
2223 if (low != test_pat[chan][i] ||
2224 high != test_pat[chan][i+1]) {
2225 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x000b);
2226 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x4001);
2227 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x4005);
2228
2229 return -EBUSY;
2230 }
2231 }
2232 }
2233
2234 return 0;
2235}
2236
2237static int tg3_phy_reset_chanpat(struct tg3 *tp)
2238{
2239 int chan;
2240
2241 for (chan = 0; chan < 4; chan++) {
2242 int i;
2243
2244 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
2245 (chan * 0x2000) | 0x0200);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002246 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0002);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247 for (i = 0; i < 6; i++)
2248 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x000);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002249 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0202);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250 if (tg3_wait_macro_done(tp))
2251 return -EBUSY;
2252 }
2253
2254 return 0;
2255}
2256
2257static int tg3_phy_reset_5703_4_5(struct tg3 *tp)
2258{
2259 u32 reg32, phy9_orig;
2260 int retries, do_phy_reset, err;
2261
2262 retries = 10;
2263 do_phy_reset = 1;
2264 do {
2265 if (do_phy_reset) {
2266 err = tg3_bmcr_reset(tp);
2267 if (err)
2268 return err;
2269 do_phy_reset = 0;
2270 }
2271
2272 /* Disable transmitter and interrupt. */
2273 if (tg3_readphy(tp, MII_TG3_EXT_CTRL, &reg32))
2274 continue;
2275
2276 reg32 |= 0x3000;
2277 tg3_writephy(tp, MII_TG3_EXT_CTRL, reg32);
2278
2279 /* Set full-duplex, 1000 mbps. */
2280 tg3_writephy(tp, MII_BMCR,
Matt Carlson221c5632011-06-13 13:39:01 +00002281 BMCR_FULLDPLX | BMCR_SPEED1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282
2283 /* Set to master mode. */
Matt Carlson221c5632011-06-13 13:39:01 +00002284 if (tg3_readphy(tp, MII_CTRL1000, &phy9_orig))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285 continue;
2286
Matt Carlson221c5632011-06-13 13:39:01 +00002287 tg3_writephy(tp, MII_CTRL1000,
2288 CTL1000_AS_MASTER | CTL1000_ENABLE_MASTER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289
Matt Carlson1d36ba42011-04-20 07:57:42 +00002290 err = TG3_PHY_AUXCTL_SMDSP_ENABLE(tp);
2291 if (err)
2292 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293
2294 /* Block the PHY control access. */
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00002295 tg3_phydsp_write(tp, 0x8005, 0x0800);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296
2297 err = tg3_phy_write_and_check_testpat(tp, &do_phy_reset);
2298 if (!err)
2299 break;
2300 } while (--retries);
2301
2302 err = tg3_phy_reset_chanpat(tp);
2303 if (err)
2304 return err;
2305
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00002306 tg3_phydsp_write(tp, 0x8005, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307
2308 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x8200);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002309 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310
Matt Carlson1d36ba42011-04-20 07:57:42 +00002311 TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312
Matt Carlson221c5632011-06-13 13:39:01 +00002313 tg3_writephy(tp, MII_CTRL1000, phy9_orig);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314
2315 if (!tg3_readphy(tp, MII_TG3_EXT_CTRL, &reg32)) {
2316 reg32 &= ~0x3000;
2317 tg3_writephy(tp, MII_TG3_EXT_CTRL, reg32);
2318 } else if (!err)
2319 err = -EBUSY;
2320
2321 return err;
2322}
2323
2324/* This will reset the tigon3 PHY if there is no valid
2325 * link unless the FORCE argument is non-zero.
2326 */
2327static int tg3_phy_reset(struct tg3 *tp)
2328{
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002329 u32 val, cpmuctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330 int err;
2331
Michael Chan60189dd2006-12-17 17:08:07 -08002332 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan60189dd2006-12-17 17:08:07 -08002333 val = tr32(GRC_MISC_CFG);
2334 tw32_f(GRC_MISC_CFG, val & ~GRC_MISC_CFG_EPHY_IDDQ);
2335 udelay(40);
2336 }
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002337 err = tg3_readphy(tp, MII_BMSR, &val);
2338 err |= tg3_readphy(tp, MII_BMSR, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339 if (err != 0)
2340 return -EBUSY;
2341
Michael Chanc8e1e822006-04-29 18:55:17 -07002342 if (netif_running(tp->dev) && netif_carrier_ok(tp->dev)) {
2343 netif_carrier_off(tp->dev);
2344 tg3_link_report(tp);
2345 }
2346
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
2348 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
2349 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
2350 err = tg3_phy_reset_5703_4_5(tp);
2351 if (err)
2352 return err;
2353 goto out;
2354 }
2355
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002356 cpmuctrl = 0;
2357 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
2358 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX) {
2359 cpmuctrl = tr32(TG3_CPMU_CTRL);
2360 if (cpmuctrl & CPMU_CTRL_GPHY_10MB_RXONLY)
2361 tw32(TG3_CPMU_CTRL,
2362 cpmuctrl & ~CPMU_CTRL_GPHY_10MB_RXONLY);
2363 }
2364
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365 err = tg3_bmcr_reset(tp);
2366 if (err)
2367 return err;
2368
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002369 if (cpmuctrl & CPMU_CTRL_GPHY_10MB_RXONLY) {
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002370 val = MII_TG3_DSP_EXP8_AEDW | MII_TG3_DSP_EXP8_REJ2MHz;
2371 tg3_phydsp_write(tp, MII_TG3_DSP_EXP8, val);
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002372
2373 tw32(TG3_CPMU_CTRL, cpmuctrl);
2374 }
2375
Matt Carlsonbcb37f62008-11-03 16:52:09 -08002376 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX ||
2377 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5761_AX) {
Matt Carlsonce057f02007-11-12 21:08:03 -08002378 val = tr32(TG3_CPMU_LSPD_1000MB_CLK);
2379 if ((val & CPMU_LSPD_1000MB_MACCLK_MASK) ==
2380 CPMU_LSPD_1000MB_MACCLK_12_5) {
2381 val &= ~CPMU_LSPD_1000MB_MACCLK_MASK;
2382 udelay(40);
2383 tw32_f(TG3_CPMU_LSPD_1000MB_CLK, val);
2384 }
2385 }
2386
Joe Perches63c3a662011-04-26 08:12:10 +00002387 if (tg3_flag(tp, 5717_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002388 (tp->phy_flags & TG3_PHYFLG_MII_SERDES))
Matt Carlsonecf14102010-01-20 16:58:05 +00002389 return 0;
2390
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002391 tg3_phy_apply_otp(tp);
2392
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002393 if (tp->phy_flags & TG3_PHYFLG_ENABLE_APD)
Matt Carlson6833c042008-11-21 17:18:59 -08002394 tg3_phy_toggle_apd(tp, true);
2395 else
2396 tg3_phy_toggle_apd(tp, false);
2397
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398out:
Matt Carlson1d36ba42011-04-20 07:57:42 +00002399 if ((tp->phy_flags & TG3_PHYFLG_ADC_BUG) &&
2400 !TG3_PHY_AUXCTL_SMDSP_ENABLE(tp)) {
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00002401 tg3_phydsp_write(tp, 0x201f, 0x2aaa);
2402 tg3_phydsp_write(tp, 0x000a, 0x0323);
Matt Carlson1d36ba42011-04-20 07:57:42 +00002403 TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404 }
Matt Carlson1d36ba42011-04-20 07:57:42 +00002405
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002406 if (tp->phy_flags & TG3_PHYFLG_5704_A0_BUG) {
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002407 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8d68);
2408 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8d68);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409 }
Matt Carlson1d36ba42011-04-20 07:57:42 +00002410
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002411 if (tp->phy_flags & TG3_PHYFLG_BER_BUG) {
Matt Carlson1d36ba42011-04-20 07:57:42 +00002412 if (!TG3_PHY_AUXCTL_SMDSP_ENABLE(tp)) {
2413 tg3_phydsp_write(tp, 0x000a, 0x310b);
2414 tg3_phydsp_write(tp, 0x201f, 0x9506);
2415 tg3_phydsp_write(tp, 0x401f, 0x14e2);
2416 TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
2417 }
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002418 } else if (tp->phy_flags & TG3_PHYFLG_JITTER_BUG) {
Matt Carlson1d36ba42011-04-20 07:57:42 +00002419 if (!TG3_PHY_AUXCTL_SMDSP_ENABLE(tp)) {
2420 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x000a);
2421 if (tp->phy_flags & TG3_PHYFLG_ADJUST_TRIM) {
2422 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x110b);
2423 tg3_writephy(tp, MII_TG3_TEST1,
2424 MII_TG3_TEST1_TRIM_EN | 0x4);
2425 } else
2426 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x010b);
2427
2428 TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
2429 }
Michael Chanc424cb22006-04-29 18:56:34 -07002430 }
Matt Carlson1d36ba42011-04-20 07:57:42 +00002431
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432 /* Set Extended packet length bit (bit 14) on all chips that */
2433 /* support jumbo frames */
Matt Carlson79eb6902010-02-17 15:17:03 +00002434 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435 /* Cannot do read-modify-write on 5401 */
Matt Carlsonb4bd2922011-04-20 07:57:41 +00002436 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_AUXCTL, 0x4c20);
Joe Perches63c3a662011-04-26 08:12:10 +00002437 } else if (tg3_flag(tp, JUMBO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438 /* Set bit 14 with read-modify-write to preserve other bits */
Matt Carlson15ee95c2011-04-20 07:57:40 +00002439 err = tg3_phy_auxctl_read(tp,
2440 MII_TG3_AUXCTL_SHDWSEL_AUXCTL, &val);
2441 if (!err)
Matt Carlsonb4bd2922011-04-20 07:57:41 +00002442 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_AUXCTL,
2443 val | MII_TG3_AUXCTL_ACTL_EXTPKTLEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444 }
2445
2446 /* Set phy register 0x10 bit 0 to high fifo elasticity to support
2447 * jumbo frames transmission.
2448 */
Joe Perches63c3a662011-04-26 08:12:10 +00002449 if (tg3_flag(tp, JUMBO_CAPABLE)) {
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002450 if (!tg3_readphy(tp, MII_TG3_EXT_CTRL, &val))
Matt Carlsonc6cdf432010-04-05 10:19:26 +00002451 tg3_writephy(tp, MII_TG3_EXT_CTRL,
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002452 val | MII_TG3_EXT_CTRL_FIFO_ELASTIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453 }
2454
Michael Chan715116a2006-09-27 16:09:25 -07002455 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan715116a2006-09-27 16:09:25 -07002456 /* adjust output voltage */
Matt Carlson535ef6e2009-08-25 10:09:36 +00002457 tg3_writephy(tp, MII_TG3_FET_PTEST, 0x12);
Michael Chan715116a2006-09-27 16:09:25 -07002458 }
2459
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002460 tg3_phy_toggle_automdix(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461 tg3_phy_set_wirespeed(tp);
2462 return 0;
2463}
2464
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002465#define TG3_GPIO_MSG_DRVR_PRES 0x00000001
2466#define TG3_GPIO_MSG_NEED_VAUX 0x00000002
2467#define TG3_GPIO_MSG_MASK (TG3_GPIO_MSG_DRVR_PRES | \
2468 TG3_GPIO_MSG_NEED_VAUX)
2469#define TG3_GPIO_MSG_ALL_DRVR_PRES_MASK \
2470 ((TG3_GPIO_MSG_DRVR_PRES << 0) | \
2471 (TG3_GPIO_MSG_DRVR_PRES << 4) | \
2472 (TG3_GPIO_MSG_DRVR_PRES << 8) | \
2473 (TG3_GPIO_MSG_DRVR_PRES << 12))
2474
2475#define TG3_GPIO_MSG_ALL_NEED_VAUX_MASK \
2476 ((TG3_GPIO_MSG_NEED_VAUX << 0) | \
2477 (TG3_GPIO_MSG_NEED_VAUX << 4) | \
2478 (TG3_GPIO_MSG_NEED_VAUX << 8) | \
2479 (TG3_GPIO_MSG_NEED_VAUX << 12))
2480
2481static inline u32 tg3_set_function_status(struct tg3 *tp, u32 newstat)
2482{
2483 u32 status, shift;
2484
2485 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
2486 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
2487 status = tg3_ape_read32(tp, TG3_APE_GPIO_MSG);
2488 else
2489 status = tr32(TG3_CPMU_DRV_STATUS);
2490
2491 shift = TG3_APE_GPIO_MSG_SHIFT + 4 * tp->pci_fn;
2492 status &= ~(TG3_GPIO_MSG_MASK << shift);
2493 status |= (newstat << shift);
2494
2495 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
2496 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
2497 tg3_ape_write32(tp, TG3_APE_GPIO_MSG, status);
2498 else
2499 tw32(TG3_CPMU_DRV_STATUS, status);
2500
2501 return status >> TG3_APE_GPIO_MSG_SHIFT;
2502}
2503
Matt Carlson520b2752011-06-13 13:39:02 +00002504static inline int tg3_pwrsrc_switch_to_vmain(struct tg3 *tp)
2505{
2506 if (!tg3_flag(tp, IS_NIC))
2507 return 0;
2508
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002509 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
2510 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
2511 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
2512 if (tg3_ape_lock(tp, TG3_APE_LOCK_GPIO))
2513 return -EIO;
Matt Carlson520b2752011-06-13 13:39:02 +00002514
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002515 tg3_set_function_status(tp, TG3_GPIO_MSG_DRVR_PRES);
2516
2517 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl,
2518 TG3_GRC_LCLCTL_PWRSW_DELAY);
2519
2520 tg3_ape_unlock(tp, TG3_APE_LOCK_GPIO);
2521 } else {
2522 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl,
2523 TG3_GRC_LCLCTL_PWRSW_DELAY);
2524 }
Matt Carlson6f5c8f832011-07-13 09:27:31 +00002525
Matt Carlson520b2752011-06-13 13:39:02 +00002526 return 0;
2527}
2528
2529static void tg3_pwrsrc_die_with_vmain(struct tg3 *tp)
2530{
2531 u32 grc_local_ctrl;
2532
2533 if (!tg3_flag(tp, IS_NIC) ||
2534 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2535 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)
2536 return;
2537
2538 grc_local_ctrl = tp->grc_local_ctrl | GRC_LCLCTRL_GPIO_OE1;
2539
2540 tw32_wait_f(GRC_LOCAL_CTRL,
2541 grc_local_ctrl | GRC_LCLCTRL_GPIO_OUTPUT1,
2542 TG3_GRC_LCLCTL_PWRSW_DELAY);
2543
2544 tw32_wait_f(GRC_LOCAL_CTRL,
2545 grc_local_ctrl,
2546 TG3_GRC_LCLCTL_PWRSW_DELAY);
2547
2548 tw32_wait_f(GRC_LOCAL_CTRL,
2549 grc_local_ctrl | GRC_LCLCTRL_GPIO_OUTPUT1,
2550 TG3_GRC_LCLCTL_PWRSW_DELAY);
2551}
2552
2553static void tg3_pwrsrc_switch_to_vaux(struct tg3 *tp)
2554{
2555 if (!tg3_flag(tp, IS_NIC))
2556 return;
2557
2558 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2559 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
2560 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2561 (GRC_LCLCTRL_GPIO_OE0 |
2562 GRC_LCLCTRL_GPIO_OE1 |
2563 GRC_LCLCTRL_GPIO_OE2 |
2564 GRC_LCLCTRL_GPIO_OUTPUT0 |
2565 GRC_LCLCTRL_GPIO_OUTPUT1),
2566 TG3_GRC_LCLCTL_PWRSW_DELAY);
2567 } else if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761 ||
2568 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761S) {
2569 /* The 5761 non-e device swaps GPIO 0 and GPIO 2. */
2570 u32 grc_local_ctrl = GRC_LCLCTRL_GPIO_OE0 |
2571 GRC_LCLCTRL_GPIO_OE1 |
2572 GRC_LCLCTRL_GPIO_OE2 |
2573 GRC_LCLCTRL_GPIO_OUTPUT0 |
2574 GRC_LCLCTRL_GPIO_OUTPUT1 |
2575 tp->grc_local_ctrl;
2576 tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl,
2577 TG3_GRC_LCLCTL_PWRSW_DELAY);
2578
2579 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OUTPUT2;
2580 tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl,
2581 TG3_GRC_LCLCTL_PWRSW_DELAY);
2582
2583 grc_local_ctrl &= ~GRC_LCLCTRL_GPIO_OUTPUT0;
2584 tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl,
2585 TG3_GRC_LCLCTL_PWRSW_DELAY);
2586 } else {
2587 u32 no_gpio2;
2588 u32 grc_local_ctrl = 0;
2589
2590 /* Workaround to prevent overdrawing Amps. */
2591 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
2592 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE3;
2593 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2594 grc_local_ctrl,
2595 TG3_GRC_LCLCTL_PWRSW_DELAY);
2596 }
2597
2598 /* On 5753 and variants, GPIO2 cannot be used. */
2599 no_gpio2 = tp->nic_sram_data_cfg &
2600 NIC_SRAM_DATA_CFG_NO_GPIO2;
2601
2602 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE0 |
2603 GRC_LCLCTRL_GPIO_OE1 |
2604 GRC_LCLCTRL_GPIO_OE2 |
2605 GRC_LCLCTRL_GPIO_OUTPUT1 |
2606 GRC_LCLCTRL_GPIO_OUTPUT2;
2607 if (no_gpio2) {
2608 grc_local_ctrl &= ~(GRC_LCLCTRL_GPIO_OE2 |
2609 GRC_LCLCTRL_GPIO_OUTPUT2);
2610 }
2611 tw32_wait_f(GRC_LOCAL_CTRL,
2612 tp->grc_local_ctrl | grc_local_ctrl,
2613 TG3_GRC_LCLCTL_PWRSW_DELAY);
2614
2615 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OUTPUT0;
2616
2617 tw32_wait_f(GRC_LOCAL_CTRL,
2618 tp->grc_local_ctrl | grc_local_ctrl,
2619 TG3_GRC_LCLCTL_PWRSW_DELAY);
2620
2621 if (!no_gpio2) {
2622 grc_local_ctrl &= ~GRC_LCLCTRL_GPIO_OUTPUT2;
2623 tw32_wait_f(GRC_LOCAL_CTRL,
2624 tp->grc_local_ctrl | grc_local_ctrl,
2625 TG3_GRC_LCLCTL_PWRSW_DELAY);
2626 }
2627 }
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002628}
Matt Carlson6f5c8f832011-07-13 09:27:31 +00002629
Matt Carlsoncd0d7222011-07-13 09:27:33 +00002630static void tg3_frob_aux_power_5717(struct tg3 *tp, bool wol_enable)
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002631{
2632 u32 msg = 0;
2633
2634 /* Serialize power state transitions */
2635 if (tg3_ape_lock(tp, TG3_APE_LOCK_GPIO))
2636 return;
2637
Matt Carlsoncd0d7222011-07-13 09:27:33 +00002638 if (tg3_flag(tp, ENABLE_ASF) || tg3_flag(tp, ENABLE_APE) || wol_enable)
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002639 msg = TG3_GPIO_MSG_NEED_VAUX;
2640
2641 msg = tg3_set_function_status(tp, msg);
2642
2643 if (msg & TG3_GPIO_MSG_ALL_DRVR_PRES_MASK)
2644 goto done;
2645
2646 if (msg & TG3_GPIO_MSG_ALL_NEED_VAUX_MASK)
2647 tg3_pwrsrc_switch_to_vaux(tp);
2648 else
2649 tg3_pwrsrc_die_with_vmain(tp);
2650
2651done:
Matt Carlson6f5c8f832011-07-13 09:27:31 +00002652 tg3_ape_unlock(tp, TG3_APE_LOCK_GPIO);
Matt Carlson520b2752011-06-13 13:39:02 +00002653}
2654
Matt Carlsoncd0d7222011-07-13 09:27:33 +00002655static void tg3_frob_aux_power(struct tg3 *tp, bool include_wol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656{
Matt Carlson683644b2011-03-09 16:58:23 +00002657 bool need_vaux = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658
Matt Carlson334355a2010-01-20 16:58:10 +00002659 /* The GPIOs do something completely different on 57765. */
Matt Carlson55086ad2011-12-14 11:09:59 +00002660 if (!tg3_flag(tp, IS_NIC) || tg3_flag(tp, 57765_CLASS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002661 return;
2662
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002663 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
2664 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
2665 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
Matt Carlsoncd0d7222011-07-13 09:27:33 +00002666 tg3_frob_aux_power_5717(tp, include_wol ?
2667 tg3_flag(tp, WOL_ENABLE) != 0 : 0);
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002668 return;
2669 }
2670
2671 if (tp->pdev_peer && tp->pdev_peer != tp->pdev) {
Michael Chan8c2dc7e2005-12-19 16:26:02 -08002672 struct net_device *dev_peer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002673
Michael Chan8c2dc7e2005-12-19 16:26:02 -08002674 dev_peer = pci_get_drvdata(tp->pdev_peer);
Matt Carlson683644b2011-03-09 16:58:23 +00002675
Michael Chanbc1c7562006-03-20 17:48:03 -08002676 /* remove_one() may have been run on the peer. */
Matt Carlson683644b2011-03-09 16:58:23 +00002677 if (dev_peer) {
2678 struct tg3 *tp_peer = netdev_priv(dev_peer);
2679
Joe Perches63c3a662011-04-26 08:12:10 +00002680 if (tg3_flag(tp_peer, INIT_COMPLETE))
Matt Carlson683644b2011-03-09 16:58:23 +00002681 return;
2682
Matt Carlsoncd0d7222011-07-13 09:27:33 +00002683 if ((include_wol && tg3_flag(tp_peer, WOL_ENABLE)) ||
Joe Perches63c3a662011-04-26 08:12:10 +00002684 tg3_flag(tp_peer, ENABLE_ASF))
Matt Carlson683644b2011-03-09 16:58:23 +00002685 need_vaux = true;
2686 }
Michael Chan8c2dc7e2005-12-19 16:26:02 -08002687 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002688
Matt Carlsoncd0d7222011-07-13 09:27:33 +00002689 if ((include_wol && tg3_flag(tp, WOL_ENABLE)) ||
2690 tg3_flag(tp, ENABLE_ASF))
Matt Carlson683644b2011-03-09 16:58:23 +00002691 need_vaux = true;
2692
Matt Carlson520b2752011-06-13 13:39:02 +00002693 if (need_vaux)
2694 tg3_pwrsrc_switch_to_vaux(tp);
2695 else
2696 tg3_pwrsrc_die_with_vmain(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002697}
2698
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07002699static int tg3_5700_link_polarity(struct tg3 *tp, u32 speed)
2700{
2701 if (tp->led_ctrl == LED_CTRL_MODE_PHY_2)
2702 return 1;
Matt Carlson79eb6902010-02-17 15:17:03 +00002703 else if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5411) {
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07002704 if (speed != SPEED_10)
2705 return 1;
2706 } else if (speed == SPEED_10)
2707 return 1;
2708
2709 return 0;
2710}
2711
Matt Carlson0a459aa2008-11-03 16:54:15 -08002712static void tg3_power_down_phy(struct tg3 *tp, bool do_low_power)
Michael Chan15c3b692006-03-22 01:06:52 -08002713{
Matt Carlsonce057f02007-11-12 21:08:03 -08002714 u32 val;
2715
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002716 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Michael Chan51297242007-02-13 12:17:57 -08002717 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
2718 u32 sg_dig_ctrl = tr32(SG_DIG_CTRL);
2719 u32 serdes_cfg = tr32(MAC_SERDES_CFG);
2720
2721 sg_dig_ctrl |=
2722 SG_DIG_USING_HW_AUTONEG | SG_DIG_SOFT_RESET;
2723 tw32(SG_DIG_CTRL, sg_dig_ctrl);
2724 tw32(MAC_SERDES_CFG, serdes_cfg | (1 << 15));
2725 }
Michael Chan3f7045c2006-09-27 16:02:29 -07002726 return;
Michael Chan51297242007-02-13 12:17:57 -08002727 }
Michael Chan3f7045c2006-09-27 16:02:29 -07002728
Michael Chan60189dd2006-12-17 17:08:07 -08002729 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan60189dd2006-12-17 17:08:07 -08002730 tg3_bmcr_reset(tp);
2731 val = tr32(GRC_MISC_CFG);
2732 tw32_f(GRC_MISC_CFG, val | GRC_MISC_CFG_EPHY_IDDQ);
2733 udelay(40);
2734 return;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002735 } else if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Matt Carlson0e5f7842009-11-02 14:26:38 +00002736 u32 phytest;
2737 if (!tg3_readphy(tp, MII_TG3_FET_TEST, &phytest)) {
2738 u32 phy;
2739
2740 tg3_writephy(tp, MII_ADVERTISE, 0);
2741 tg3_writephy(tp, MII_BMCR,
2742 BMCR_ANENABLE | BMCR_ANRESTART);
2743
2744 tg3_writephy(tp, MII_TG3_FET_TEST,
2745 phytest | MII_TG3_FET_SHADOW_EN);
2746 if (!tg3_readphy(tp, MII_TG3_FET_SHDW_AUXMODE4, &phy)) {
2747 phy |= MII_TG3_FET_SHDW_AUXMODE4_SBPD;
2748 tg3_writephy(tp,
2749 MII_TG3_FET_SHDW_AUXMODE4,
2750 phy);
2751 }
2752 tg3_writephy(tp, MII_TG3_FET_TEST, phytest);
2753 }
2754 return;
Matt Carlson0a459aa2008-11-03 16:54:15 -08002755 } else if (do_low_power) {
Michael Chan715116a2006-09-27 16:09:25 -07002756 tg3_writephy(tp, MII_TG3_EXT_CTRL,
2757 MII_TG3_EXT_CTRL_FORCE_LED_OFF);
Matt Carlson0a459aa2008-11-03 16:54:15 -08002758
Matt Carlsonb4bd2922011-04-20 07:57:41 +00002759 val = MII_TG3_AUXCTL_PCTL_100TX_LPWR |
2760 MII_TG3_AUXCTL_PCTL_SPR_ISOLATE |
2761 MII_TG3_AUXCTL_PCTL_VREG_11V;
2762 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_PWRCTL, val);
Michael Chan715116a2006-09-27 16:09:25 -07002763 }
Michael Chan3f7045c2006-09-27 16:02:29 -07002764
Michael Chan15c3b692006-03-22 01:06:52 -08002765 /* The PHY should not be powered down on some chips because
2766 * of bugs.
2767 */
2768 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2769 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
2770 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780 &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002771 (tp->phy_flags & TG3_PHYFLG_MII_SERDES)))
Michael Chan15c3b692006-03-22 01:06:52 -08002772 return;
Matt Carlsonce057f02007-11-12 21:08:03 -08002773
Matt Carlsonbcb37f62008-11-03 16:52:09 -08002774 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX ||
2775 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5761_AX) {
Matt Carlsonce057f02007-11-12 21:08:03 -08002776 val = tr32(TG3_CPMU_LSPD_1000MB_CLK);
2777 val &= ~CPMU_LSPD_1000MB_MACCLK_MASK;
2778 val |= CPMU_LSPD_1000MB_MACCLK_12_5;
2779 tw32_f(TG3_CPMU_LSPD_1000MB_CLK, val);
2780 }
2781
Michael Chan15c3b692006-03-22 01:06:52 -08002782 tg3_writephy(tp, MII_BMCR, BMCR_PDOWN);
2783}
2784
Matt Carlson3f007892008-11-03 16:51:36 -08002785/* tp->lock is held. */
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002786static int tg3_nvram_lock(struct tg3 *tp)
2787{
Joe Perches63c3a662011-04-26 08:12:10 +00002788 if (tg3_flag(tp, NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002789 int i;
2790
2791 if (tp->nvram_lock_cnt == 0) {
2792 tw32(NVRAM_SWARB, SWARB_REQ_SET1);
2793 for (i = 0; i < 8000; i++) {
2794 if (tr32(NVRAM_SWARB) & SWARB_GNT1)
2795 break;
2796 udelay(20);
2797 }
2798 if (i == 8000) {
2799 tw32(NVRAM_SWARB, SWARB_REQ_CLR1);
2800 return -ENODEV;
2801 }
2802 }
2803 tp->nvram_lock_cnt++;
2804 }
2805 return 0;
2806}
2807
2808/* tp->lock is held. */
2809static void tg3_nvram_unlock(struct tg3 *tp)
2810{
Joe Perches63c3a662011-04-26 08:12:10 +00002811 if (tg3_flag(tp, NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002812 if (tp->nvram_lock_cnt > 0)
2813 tp->nvram_lock_cnt--;
2814 if (tp->nvram_lock_cnt == 0)
2815 tw32_f(NVRAM_SWARB, SWARB_REQ_CLR1);
2816 }
2817}
2818
2819/* tp->lock is held. */
2820static void tg3_enable_nvram_access(struct tg3 *tp)
2821{
Joe Perches63c3a662011-04-26 08:12:10 +00002822 if (tg3_flag(tp, 5750_PLUS) && !tg3_flag(tp, PROTECTED_NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002823 u32 nvaccess = tr32(NVRAM_ACCESS);
2824
2825 tw32(NVRAM_ACCESS, nvaccess | ACCESS_ENABLE);
2826 }
2827}
2828
2829/* tp->lock is held. */
2830static void tg3_disable_nvram_access(struct tg3 *tp)
2831{
Joe Perches63c3a662011-04-26 08:12:10 +00002832 if (tg3_flag(tp, 5750_PLUS) && !tg3_flag(tp, PROTECTED_NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002833 u32 nvaccess = tr32(NVRAM_ACCESS);
2834
2835 tw32(NVRAM_ACCESS, nvaccess & ~ACCESS_ENABLE);
2836 }
2837}
2838
2839static int tg3_nvram_read_using_eeprom(struct tg3 *tp,
2840 u32 offset, u32 *val)
2841{
2842 u32 tmp;
2843 int i;
2844
2845 if (offset > EEPROM_ADDR_ADDR_MASK || (offset % 4) != 0)
2846 return -EINVAL;
2847
2848 tmp = tr32(GRC_EEPROM_ADDR) & ~(EEPROM_ADDR_ADDR_MASK |
2849 EEPROM_ADDR_DEVID_MASK |
2850 EEPROM_ADDR_READ);
2851 tw32(GRC_EEPROM_ADDR,
2852 tmp |
2853 (0 << EEPROM_ADDR_DEVID_SHIFT) |
2854 ((offset << EEPROM_ADDR_ADDR_SHIFT) &
2855 EEPROM_ADDR_ADDR_MASK) |
2856 EEPROM_ADDR_READ | EEPROM_ADDR_START);
2857
2858 for (i = 0; i < 1000; i++) {
2859 tmp = tr32(GRC_EEPROM_ADDR);
2860
2861 if (tmp & EEPROM_ADDR_COMPLETE)
2862 break;
2863 msleep(1);
2864 }
2865 if (!(tmp & EEPROM_ADDR_COMPLETE))
2866 return -EBUSY;
2867
Matt Carlson62cedd12009-04-20 14:52:29 -07002868 tmp = tr32(GRC_EEPROM_DATA);
2869
2870 /*
2871 * The data will always be opposite the native endian
2872 * format. Perform a blind byteswap to compensate.
2873 */
2874 *val = swab32(tmp);
2875
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002876 return 0;
2877}
2878
2879#define NVRAM_CMD_TIMEOUT 10000
2880
2881static int tg3_nvram_exec_cmd(struct tg3 *tp, u32 nvram_cmd)
2882{
2883 int i;
2884
2885 tw32(NVRAM_CMD, nvram_cmd);
2886 for (i = 0; i < NVRAM_CMD_TIMEOUT; i++) {
2887 udelay(10);
2888 if (tr32(NVRAM_CMD) & NVRAM_CMD_DONE) {
2889 udelay(10);
2890 break;
2891 }
2892 }
2893
2894 if (i == NVRAM_CMD_TIMEOUT)
2895 return -EBUSY;
2896
2897 return 0;
2898}
2899
2900static u32 tg3_nvram_phys_addr(struct tg3 *tp, u32 addr)
2901{
Joe Perches63c3a662011-04-26 08:12:10 +00002902 if (tg3_flag(tp, NVRAM) &&
2903 tg3_flag(tp, NVRAM_BUFFERED) &&
2904 tg3_flag(tp, FLASH) &&
2905 !tg3_flag(tp, NO_NVRAM_ADDR_TRANS) &&
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002906 (tp->nvram_jedecnum == JEDEC_ATMEL))
2907
2908 addr = ((addr / tp->nvram_pagesize) <<
2909 ATMEL_AT45DB0X1B_PAGE_POS) +
2910 (addr % tp->nvram_pagesize);
2911
2912 return addr;
2913}
2914
2915static u32 tg3_nvram_logical_addr(struct tg3 *tp, u32 addr)
2916{
Joe Perches63c3a662011-04-26 08:12:10 +00002917 if (tg3_flag(tp, NVRAM) &&
2918 tg3_flag(tp, NVRAM_BUFFERED) &&
2919 tg3_flag(tp, FLASH) &&
2920 !tg3_flag(tp, NO_NVRAM_ADDR_TRANS) &&
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002921 (tp->nvram_jedecnum == JEDEC_ATMEL))
2922
2923 addr = ((addr >> ATMEL_AT45DB0X1B_PAGE_POS) *
2924 tp->nvram_pagesize) +
2925 (addr & ((1 << ATMEL_AT45DB0X1B_PAGE_POS) - 1));
2926
2927 return addr;
2928}
2929
Matt Carlsone4f34112009-02-25 14:25:00 +00002930/* NOTE: Data read in from NVRAM is byteswapped according to
2931 * the byteswapping settings for all other register accesses.
2932 * tg3 devices are BE devices, so on a BE machine, the data
2933 * returned will be exactly as it is seen in NVRAM. On a LE
2934 * machine, the 32-bit value will be byteswapped.
2935 */
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002936static int tg3_nvram_read(struct tg3 *tp, u32 offset, u32 *val)
2937{
2938 int ret;
2939
Joe Perches63c3a662011-04-26 08:12:10 +00002940 if (!tg3_flag(tp, NVRAM))
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002941 return tg3_nvram_read_using_eeprom(tp, offset, val);
2942
2943 offset = tg3_nvram_phys_addr(tp, offset);
2944
2945 if (offset > NVRAM_ADDR_MSK)
2946 return -EINVAL;
2947
2948 ret = tg3_nvram_lock(tp);
2949 if (ret)
2950 return ret;
2951
2952 tg3_enable_nvram_access(tp);
2953
2954 tw32(NVRAM_ADDR, offset);
2955 ret = tg3_nvram_exec_cmd(tp, NVRAM_CMD_RD | NVRAM_CMD_GO |
2956 NVRAM_CMD_FIRST | NVRAM_CMD_LAST | NVRAM_CMD_DONE);
2957
2958 if (ret == 0)
Matt Carlsone4f34112009-02-25 14:25:00 +00002959 *val = tr32(NVRAM_RDDATA);
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002960
2961 tg3_disable_nvram_access(tp);
2962
2963 tg3_nvram_unlock(tp);
2964
2965 return ret;
2966}
2967
Matt Carlsona9dc5292009-02-25 14:25:30 +00002968/* Ensures NVRAM data is in bytestream format. */
2969static int tg3_nvram_read_be32(struct tg3 *tp, u32 offset, __be32 *val)
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002970{
2971 u32 v;
Matt Carlsona9dc5292009-02-25 14:25:30 +00002972 int res = tg3_nvram_read(tp, offset, &v);
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002973 if (!res)
Matt Carlsona9dc5292009-02-25 14:25:30 +00002974 *val = cpu_to_be32(v);
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002975 return res;
2976}
2977
Matt Carlsondbe9b922012-02-13 10:20:09 +00002978static int tg3_nvram_write_block_using_eeprom(struct tg3 *tp,
2979 u32 offset, u32 len, u8 *buf)
2980{
2981 int i, j, rc = 0;
2982 u32 val;
2983
2984 for (i = 0; i < len; i += 4) {
2985 u32 addr;
2986 __be32 data;
2987
2988 addr = offset + i;
2989
2990 memcpy(&data, buf + i, 4);
2991
2992 /*
2993 * The SEEPROM interface expects the data to always be opposite
2994 * the native endian format. We accomplish this by reversing
2995 * all the operations that would have been performed on the
2996 * data from a call to tg3_nvram_read_be32().
2997 */
2998 tw32(GRC_EEPROM_DATA, swab32(be32_to_cpu(data)));
2999
3000 val = tr32(GRC_EEPROM_ADDR);
3001 tw32(GRC_EEPROM_ADDR, val | EEPROM_ADDR_COMPLETE);
3002
3003 val &= ~(EEPROM_ADDR_ADDR_MASK | EEPROM_ADDR_DEVID_MASK |
3004 EEPROM_ADDR_READ);
3005 tw32(GRC_EEPROM_ADDR, val |
3006 (0 << EEPROM_ADDR_DEVID_SHIFT) |
3007 (addr & EEPROM_ADDR_ADDR_MASK) |
3008 EEPROM_ADDR_START |
3009 EEPROM_ADDR_WRITE);
3010
3011 for (j = 0; j < 1000; j++) {
3012 val = tr32(GRC_EEPROM_ADDR);
3013
3014 if (val & EEPROM_ADDR_COMPLETE)
3015 break;
3016 msleep(1);
3017 }
3018 if (!(val & EEPROM_ADDR_COMPLETE)) {
3019 rc = -EBUSY;
3020 break;
3021 }
3022 }
3023
3024 return rc;
3025}
3026
3027/* offset and length are dword aligned */
3028static int tg3_nvram_write_block_unbuffered(struct tg3 *tp, u32 offset, u32 len,
3029 u8 *buf)
3030{
3031 int ret = 0;
3032 u32 pagesize = tp->nvram_pagesize;
3033 u32 pagemask = pagesize - 1;
3034 u32 nvram_cmd;
3035 u8 *tmp;
3036
3037 tmp = kmalloc(pagesize, GFP_KERNEL);
3038 if (tmp == NULL)
3039 return -ENOMEM;
3040
3041 while (len) {
3042 int j;
3043 u32 phy_addr, page_off, size;
3044
3045 phy_addr = offset & ~pagemask;
3046
3047 for (j = 0; j < pagesize; j += 4) {
3048 ret = tg3_nvram_read_be32(tp, phy_addr + j,
3049 (__be32 *) (tmp + j));
3050 if (ret)
3051 break;
3052 }
3053 if (ret)
3054 break;
3055
3056 page_off = offset & pagemask;
3057 size = pagesize;
3058 if (len < size)
3059 size = len;
3060
3061 len -= size;
3062
3063 memcpy(tmp + page_off, buf, size);
3064
3065 offset = offset + (pagesize - page_off);
3066
3067 tg3_enable_nvram_access(tp);
3068
3069 /*
3070 * Before we can erase the flash page, we need
3071 * to issue a special "write enable" command.
3072 */
3073 nvram_cmd = NVRAM_CMD_WREN | NVRAM_CMD_GO | NVRAM_CMD_DONE;
3074
3075 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
3076 break;
3077
3078 /* Erase the target page */
3079 tw32(NVRAM_ADDR, phy_addr);
3080
3081 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE | NVRAM_CMD_WR |
3082 NVRAM_CMD_FIRST | NVRAM_CMD_LAST | NVRAM_CMD_ERASE;
3083
3084 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
3085 break;
3086
3087 /* Issue another write enable to start the write. */
3088 nvram_cmd = NVRAM_CMD_WREN | NVRAM_CMD_GO | NVRAM_CMD_DONE;
3089
3090 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
3091 break;
3092
3093 for (j = 0; j < pagesize; j += 4) {
3094 __be32 data;
3095
3096 data = *((__be32 *) (tmp + j));
3097
3098 tw32(NVRAM_WRDATA, be32_to_cpu(data));
3099
3100 tw32(NVRAM_ADDR, phy_addr + j);
3101
3102 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE |
3103 NVRAM_CMD_WR;
3104
3105 if (j == 0)
3106 nvram_cmd |= NVRAM_CMD_FIRST;
3107 else if (j == (pagesize - 4))
3108 nvram_cmd |= NVRAM_CMD_LAST;
3109
3110 ret = tg3_nvram_exec_cmd(tp, nvram_cmd);
3111 if (ret)
3112 break;
3113 }
3114 if (ret)
3115 break;
3116 }
3117
3118 nvram_cmd = NVRAM_CMD_WRDI | NVRAM_CMD_GO | NVRAM_CMD_DONE;
3119 tg3_nvram_exec_cmd(tp, nvram_cmd);
3120
3121 kfree(tmp);
3122
3123 return ret;
3124}
3125
3126/* offset and length are dword aligned */
3127static int tg3_nvram_write_block_buffered(struct tg3 *tp, u32 offset, u32 len,
3128 u8 *buf)
3129{
3130 int i, ret = 0;
3131
3132 for (i = 0; i < len; i += 4, offset += 4) {
3133 u32 page_off, phy_addr, nvram_cmd;
3134 __be32 data;
3135
3136 memcpy(&data, buf + i, 4);
3137 tw32(NVRAM_WRDATA, be32_to_cpu(data));
3138
3139 page_off = offset % tp->nvram_pagesize;
3140
3141 phy_addr = tg3_nvram_phys_addr(tp, offset);
3142
Matt Carlsondbe9b922012-02-13 10:20:09 +00003143 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE | NVRAM_CMD_WR;
3144
3145 if (page_off == 0 || i == 0)
3146 nvram_cmd |= NVRAM_CMD_FIRST;
3147 if (page_off == (tp->nvram_pagesize - 4))
3148 nvram_cmd |= NVRAM_CMD_LAST;
3149
3150 if (i == (len - 4))
3151 nvram_cmd |= NVRAM_CMD_LAST;
3152
Matt Carlson42278222012-02-13 15:20:11 +00003153 if ((nvram_cmd & NVRAM_CMD_FIRST) ||
3154 !tg3_flag(tp, FLASH) ||
3155 !tg3_flag(tp, 57765_PLUS))
3156 tw32(NVRAM_ADDR, phy_addr);
3157
Matt Carlsondbe9b922012-02-13 10:20:09 +00003158 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5752 &&
3159 !tg3_flag(tp, 5755_PLUS) &&
3160 (tp->nvram_jedecnum == JEDEC_ST) &&
3161 (nvram_cmd & NVRAM_CMD_FIRST)) {
3162 u32 cmd;
3163
3164 cmd = NVRAM_CMD_WREN | NVRAM_CMD_GO | NVRAM_CMD_DONE;
3165 ret = tg3_nvram_exec_cmd(tp, cmd);
3166 if (ret)
3167 break;
3168 }
3169 if (!tg3_flag(tp, FLASH)) {
3170 /* We always do complete word writes to eeprom. */
3171 nvram_cmd |= (NVRAM_CMD_FIRST | NVRAM_CMD_LAST);
3172 }
3173
3174 ret = tg3_nvram_exec_cmd(tp, nvram_cmd);
3175 if (ret)
3176 break;
3177 }
3178 return ret;
3179}
3180
3181/* offset and length are dword aligned */
3182static int tg3_nvram_write_block(struct tg3 *tp, u32 offset, u32 len, u8 *buf)
3183{
3184 int ret;
3185
3186 if (tg3_flag(tp, EEPROM_WRITE_PROT)) {
3187 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl &
3188 ~GRC_LCLCTRL_GPIO_OUTPUT1);
3189 udelay(40);
3190 }
3191
3192 if (!tg3_flag(tp, NVRAM)) {
3193 ret = tg3_nvram_write_block_using_eeprom(tp, offset, len, buf);
3194 } else {
3195 u32 grc_mode;
3196
3197 ret = tg3_nvram_lock(tp);
3198 if (ret)
3199 return ret;
3200
3201 tg3_enable_nvram_access(tp);
3202 if (tg3_flag(tp, 5750_PLUS) && !tg3_flag(tp, PROTECTED_NVRAM))
3203 tw32(NVRAM_WRITE1, 0x406);
3204
3205 grc_mode = tr32(GRC_MODE);
3206 tw32(GRC_MODE, grc_mode | GRC_MODE_NVRAM_WR_ENABLE);
3207
3208 if (tg3_flag(tp, NVRAM_BUFFERED) || !tg3_flag(tp, FLASH)) {
3209 ret = tg3_nvram_write_block_buffered(tp, offset, len,
3210 buf);
3211 } else {
3212 ret = tg3_nvram_write_block_unbuffered(tp, offset, len,
3213 buf);
3214 }
3215
3216 grc_mode = tr32(GRC_MODE);
3217 tw32(GRC_MODE, grc_mode & ~GRC_MODE_NVRAM_WR_ENABLE);
3218
3219 tg3_disable_nvram_access(tp);
3220 tg3_nvram_unlock(tp);
3221 }
3222
3223 if (tg3_flag(tp, EEPROM_WRITE_PROT)) {
3224 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
3225 udelay(40);
3226 }
3227
3228 return ret;
3229}
3230
Matt Carlson997b4f12011-08-31 11:44:53 +00003231#define RX_CPU_SCRATCH_BASE 0x30000
3232#define RX_CPU_SCRATCH_SIZE 0x04000
3233#define TX_CPU_SCRATCH_BASE 0x34000
3234#define TX_CPU_SCRATCH_SIZE 0x04000
3235
3236/* tp->lock is held. */
3237static int tg3_halt_cpu(struct tg3 *tp, u32 offset)
3238{
3239 int i;
3240
3241 BUG_ON(offset == TX_CPU_BASE && tg3_flag(tp, 5705_PLUS));
3242
3243 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
3244 u32 val = tr32(GRC_VCPU_EXT_CTRL);
3245
3246 tw32(GRC_VCPU_EXT_CTRL, val | GRC_VCPU_EXT_CTRL_HALT_CPU);
3247 return 0;
3248 }
3249 if (offset == RX_CPU_BASE) {
3250 for (i = 0; i < 10000; i++) {
3251 tw32(offset + CPU_STATE, 0xffffffff);
3252 tw32(offset + CPU_MODE, CPU_MODE_HALT);
3253 if (tr32(offset + CPU_MODE) & CPU_MODE_HALT)
3254 break;
3255 }
3256
3257 tw32(offset + CPU_STATE, 0xffffffff);
3258 tw32_f(offset + CPU_MODE, CPU_MODE_HALT);
3259 udelay(10);
3260 } else {
3261 for (i = 0; i < 10000; i++) {
3262 tw32(offset + CPU_STATE, 0xffffffff);
3263 tw32(offset + CPU_MODE, CPU_MODE_HALT);
3264 if (tr32(offset + CPU_MODE) & CPU_MODE_HALT)
3265 break;
3266 }
3267 }
3268
3269 if (i >= 10000) {
3270 netdev_err(tp->dev, "%s timed out, %s CPU\n",
3271 __func__, offset == RX_CPU_BASE ? "RX" : "TX");
3272 return -ENODEV;
3273 }
3274
3275 /* Clear firmware's nvram arbitration. */
3276 if (tg3_flag(tp, NVRAM))
3277 tw32(NVRAM_SWARB, SWARB_REQ_CLR0);
3278 return 0;
3279}
3280
3281struct fw_info {
3282 unsigned int fw_base;
3283 unsigned int fw_len;
3284 const __be32 *fw_data;
3285};
3286
3287/* tp->lock is held. */
3288static int tg3_load_firmware_cpu(struct tg3 *tp, u32 cpu_base,
3289 u32 cpu_scratch_base, int cpu_scratch_size,
3290 struct fw_info *info)
3291{
3292 int err, lock_err, i;
3293 void (*write_op)(struct tg3 *, u32, u32);
3294
3295 if (cpu_base == TX_CPU_BASE && tg3_flag(tp, 5705_PLUS)) {
3296 netdev_err(tp->dev,
3297 "%s: Trying to load TX cpu firmware which is 5705\n",
3298 __func__);
3299 return -EINVAL;
3300 }
3301
3302 if (tg3_flag(tp, 5705_PLUS))
3303 write_op = tg3_write_mem;
3304 else
3305 write_op = tg3_write_indirect_reg32;
3306
3307 /* It is possible that bootcode is still loading at this point.
3308 * Get the nvram lock first before halting the cpu.
3309 */
3310 lock_err = tg3_nvram_lock(tp);
3311 err = tg3_halt_cpu(tp, cpu_base);
3312 if (!lock_err)
3313 tg3_nvram_unlock(tp);
3314 if (err)
3315 goto out;
3316
3317 for (i = 0; i < cpu_scratch_size; i += sizeof(u32))
3318 write_op(tp, cpu_scratch_base + i, 0);
3319 tw32(cpu_base + CPU_STATE, 0xffffffff);
3320 tw32(cpu_base + CPU_MODE, tr32(cpu_base+CPU_MODE)|CPU_MODE_HALT);
3321 for (i = 0; i < (info->fw_len / sizeof(u32)); i++)
3322 write_op(tp, (cpu_scratch_base +
3323 (info->fw_base & 0xffff) +
3324 (i * sizeof(u32))),
3325 be32_to_cpu(info->fw_data[i]));
3326
3327 err = 0;
3328
3329out:
3330 return err;
3331}
3332
3333/* tp->lock is held. */
3334static int tg3_load_5701_a0_firmware_fix(struct tg3 *tp)
3335{
3336 struct fw_info info;
3337 const __be32 *fw_data;
3338 int err, i;
3339
3340 fw_data = (void *)tp->fw->data;
3341
3342 /* Firmware blob starts with version numbers, followed by
3343 start address and length. We are setting complete length.
3344 length = end_address_of_bss - start_address_of_text.
3345 Remainder is the blob to be loaded contiguously
3346 from start address. */
3347
3348 info.fw_base = be32_to_cpu(fw_data[1]);
3349 info.fw_len = tp->fw->size - 12;
3350 info.fw_data = &fw_data[3];
3351
3352 err = tg3_load_firmware_cpu(tp, RX_CPU_BASE,
3353 RX_CPU_SCRATCH_BASE, RX_CPU_SCRATCH_SIZE,
3354 &info);
3355 if (err)
3356 return err;
3357
3358 err = tg3_load_firmware_cpu(tp, TX_CPU_BASE,
3359 TX_CPU_SCRATCH_BASE, TX_CPU_SCRATCH_SIZE,
3360 &info);
3361 if (err)
3362 return err;
3363
3364 /* Now startup only the RX cpu. */
3365 tw32(RX_CPU_BASE + CPU_STATE, 0xffffffff);
3366 tw32_f(RX_CPU_BASE + CPU_PC, info.fw_base);
3367
3368 for (i = 0; i < 5; i++) {
3369 if (tr32(RX_CPU_BASE + CPU_PC) == info.fw_base)
3370 break;
3371 tw32(RX_CPU_BASE + CPU_STATE, 0xffffffff);
3372 tw32(RX_CPU_BASE + CPU_MODE, CPU_MODE_HALT);
3373 tw32_f(RX_CPU_BASE + CPU_PC, info.fw_base);
3374 udelay(1000);
3375 }
3376 if (i >= 5) {
3377 netdev_err(tp->dev, "%s fails to set RX CPU PC, is %08x "
3378 "should be %08x\n", __func__,
3379 tr32(RX_CPU_BASE + CPU_PC), info.fw_base);
3380 return -ENODEV;
3381 }
3382 tw32(RX_CPU_BASE + CPU_STATE, 0xffffffff);
3383 tw32_f(RX_CPU_BASE + CPU_MODE, 0x00000000);
3384
3385 return 0;
3386}
3387
3388/* tp->lock is held. */
3389static int tg3_load_tso_firmware(struct tg3 *tp)
3390{
3391 struct fw_info info;
3392 const __be32 *fw_data;
3393 unsigned long cpu_base, cpu_scratch_base, cpu_scratch_size;
3394 int err, i;
3395
3396 if (tg3_flag(tp, HW_TSO_1) ||
3397 tg3_flag(tp, HW_TSO_2) ||
3398 tg3_flag(tp, HW_TSO_3))
3399 return 0;
3400
3401 fw_data = (void *)tp->fw->data;
3402
3403 /* Firmware blob starts with version numbers, followed by
3404 start address and length. We are setting complete length.
3405 length = end_address_of_bss - start_address_of_text.
3406 Remainder is the blob to be loaded contiguously
3407 from start address. */
3408
3409 info.fw_base = be32_to_cpu(fw_data[1]);
3410 cpu_scratch_size = tp->fw_len;
3411 info.fw_len = tp->fw->size - 12;
3412 info.fw_data = &fw_data[3];
3413
3414 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
3415 cpu_base = RX_CPU_BASE;
3416 cpu_scratch_base = NIC_SRAM_MBUF_POOL_BASE5705;
3417 } else {
3418 cpu_base = TX_CPU_BASE;
3419 cpu_scratch_base = TX_CPU_SCRATCH_BASE;
3420 cpu_scratch_size = TX_CPU_SCRATCH_SIZE;
3421 }
3422
3423 err = tg3_load_firmware_cpu(tp, cpu_base,
3424 cpu_scratch_base, cpu_scratch_size,
3425 &info);
3426 if (err)
3427 return err;
3428
3429 /* Now startup the cpu. */
3430 tw32(cpu_base + CPU_STATE, 0xffffffff);
3431 tw32_f(cpu_base + CPU_PC, info.fw_base);
3432
3433 for (i = 0; i < 5; i++) {
3434 if (tr32(cpu_base + CPU_PC) == info.fw_base)
3435 break;
3436 tw32(cpu_base + CPU_STATE, 0xffffffff);
3437 tw32(cpu_base + CPU_MODE, CPU_MODE_HALT);
3438 tw32_f(cpu_base + CPU_PC, info.fw_base);
3439 udelay(1000);
3440 }
3441 if (i >= 5) {
3442 netdev_err(tp->dev,
3443 "%s fails to set CPU PC, is %08x should be %08x\n",
3444 __func__, tr32(cpu_base + CPU_PC), info.fw_base);
3445 return -ENODEV;
3446 }
3447 tw32(cpu_base + CPU_STATE, 0xffffffff);
3448 tw32_f(cpu_base + CPU_MODE, 0x00000000);
3449 return 0;
3450}
3451
3452
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003453/* tp->lock is held. */
Matt Carlson3f007892008-11-03 16:51:36 -08003454static void __tg3_set_mac_addr(struct tg3 *tp, int skip_mac_1)
3455{
3456 u32 addr_high, addr_low;
3457 int i;
3458
3459 addr_high = ((tp->dev->dev_addr[0] << 8) |
3460 tp->dev->dev_addr[1]);
3461 addr_low = ((tp->dev->dev_addr[2] << 24) |
3462 (tp->dev->dev_addr[3] << 16) |
3463 (tp->dev->dev_addr[4] << 8) |
3464 (tp->dev->dev_addr[5] << 0));
3465 for (i = 0; i < 4; i++) {
3466 if (i == 1 && skip_mac_1)
3467 continue;
3468 tw32(MAC_ADDR_0_HIGH + (i * 8), addr_high);
3469 tw32(MAC_ADDR_0_LOW + (i * 8), addr_low);
3470 }
3471
3472 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
3473 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
3474 for (i = 0; i < 12; i++) {
3475 tw32(MAC_EXTADDR_0_HIGH + (i * 8), addr_high);
3476 tw32(MAC_EXTADDR_0_LOW + (i * 8), addr_low);
3477 }
3478 }
3479
3480 addr_high = (tp->dev->dev_addr[0] +
3481 tp->dev->dev_addr[1] +
3482 tp->dev->dev_addr[2] +
3483 tp->dev->dev_addr[3] +
3484 tp->dev->dev_addr[4] +
3485 tp->dev->dev_addr[5]) &
3486 TX_BACKOFF_SEED_MASK;
3487 tw32(MAC_TX_BACKOFF_SEED, addr_high);
3488}
3489
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003490static void tg3_enable_register_access(struct tg3 *tp)
3491{
3492 /*
3493 * Make sure register accesses (indirect or otherwise) will function
3494 * correctly.
3495 */
3496 pci_write_config_dword(tp->pdev,
3497 TG3PCI_MISC_HOST_CTRL, tp->misc_host_ctrl);
3498}
3499
3500static int tg3_power_up(struct tg3 *tp)
3501{
Matt Carlsonbed98292011-07-13 09:27:29 +00003502 int err;
3503
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003504 tg3_enable_register_access(tp);
3505
Matt Carlsonbed98292011-07-13 09:27:29 +00003506 err = pci_set_power_state(tp->pdev, PCI_D0);
3507 if (!err) {
3508 /* Switch out of Vaux if it is a NIC */
3509 tg3_pwrsrc_switch_to_vmain(tp);
3510 } else {
3511 netdev_err(tp->dev, "Transition to D0 failed\n");
3512 }
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003513
Matt Carlsonbed98292011-07-13 09:27:29 +00003514 return err;
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003515}
3516
Matt Carlson4b409522012-02-13 10:20:11 +00003517static int tg3_setup_phy(struct tg3 *, int);
3518
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003519static int tg3_power_down_prepare(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003520{
3521 u32 misc_host_ctrl;
Matt Carlson0a459aa2008-11-03 16:54:15 -08003522 bool device_should_wake, do_low_power;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003523
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003524 tg3_enable_register_access(tp);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08003525
3526 /* Restore the CLKREQ setting. */
Joe Perches63c3a662011-04-26 08:12:10 +00003527 if (tg3_flag(tp, CLKREQ_BUG)) {
Matt Carlson5e7dfd02008-11-21 17:18:16 -08003528 u16 lnkctl;
3529
3530 pci_read_config_word(tp->pdev,
Jon Mason708ebb32011-06-27 12:56:50 +00003531 pci_pcie_cap(tp->pdev) + PCI_EXP_LNKCTL,
Matt Carlson5e7dfd02008-11-21 17:18:16 -08003532 &lnkctl);
3533 lnkctl |= PCI_EXP_LNKCTL_CLKREQ_EN;
3534 pci_write_config_word(tp->pdev,
Jon Mason708ebb32011-06-27 12:56:50 +00003535 pci_pcie_cap(tp->pdev) + PCI_EXP_LNKCTL,
Matt Carlson5e7dfd02008-11-21 17:18:16 -08003536 lnkctl);
3537 }
3538
Linus Torvalds1da177e2005-04-16 15:20:36 -07003539 misc_host_ctrl = tr32(TG3PCI_MISC_HOST_CTRL);
3540 tw32(TG3PCI_MISC_HOST_CTRL,
3541 misc_host_ctrl | MISC_HOST_CTRL_MASK_PCI_INT);
3542
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003543 device_should_wake = device_may_wakeup(&tp->pdev->dev) &&
Joe Perches63c3a662011-04-26 08:12:10 +00003544 tg3_flag(tp, WOL_ENABLE);
Matt Carlson05ac4cb2008-11-03 16:53:46 -08003545
Joe Perches63c3a662011-04-26 08:12:10 +00003546 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson0a459aa2008-11-03 16:54:15 -08003547 do_low_power = false;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003548 if ((tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) &&
Matt Carlson800960682010-08-02 11:26:06 +00003549 !(tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07003550 struct phy_device *phydev;
Matt Carlson0a459aa2008-11-03 16:54:15 -08003551 u32 phyid, advertising;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07003552
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00003553 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07003554
Matt Carlson800960682010-08-02 11:26:06 +00003555 tp->phy_flags |= TG3_PHYFLG_IS_LOW_POWER;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07003556
3557 tp->link_config.orig_speed = phydev->speed;
3558 tp->link_config.orig_duplex = phydev->duplex;
3559 tp->link_config.orig_autoneg = phydev->autoneg;
3560 tp->link_config.orig_advertising = phydev->advertising;
3561
3562 advertising = ADVERTISED_TP |
3563 ADVERTISED_Pause |
3564 ADVERTISED_Autoneg |
3565 ADVERTISED_10baseT_Half;
3566
Joe Perches63c3a662011-04-26 08:12:10 +00003567 if (tg3_flag(tp, ENABLE_ASF) || device_should_wake) {
3568 if (tg3_flag(tp, WOL_SPEED_100MB))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07003569 advertising |=
3570 ADVERTISED_100baseT_Half |
3571 ADVERTISED_100baseT_Full |
3572 ADVERTISED_10baseT_Full;
3573 else
3574 advertising |= ADVERTISED_10baseT_Full;
3575 }
3576
3577 phydev->advertising = advertising;
3578
3579 phy_start_aneg(phydev);
Matt Carlson0a459aa2008-11-03 16:54:15 -08003580
3581 phyid = phydev->drv->phy_id & phydev->drv->phy_id_mask;
Matt Carlson6a443a02010-02-17 15:17:04 +00003582 if (phyid != PHY_ID_BCMAC131) {
3583 phyid &= PHY_BCM_OUI_MASK;
3584 if (phyid == PHY_BCM_OUI_1 ||
3585 phyid == PHY_BCM_OUI_2 ||
3586 phyid == PHY_BCM_OUI_3)
Matt Carlson0a459aa2008-11-03 16:54:15 -08003587 do_low_power = true;
3588 }
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07003589 }
Matt Carlsondd477002008-05-25 23:45:58 -07003590 } else {
Matt Carlson20232762008-12-21 20:18:56 -08003591 do_low_power = true;
Matt Carlson0a459aa2008-11-03 16:54:15 -08003592
Matt Carlson800960682010-08-02 11:26:06 +00003593 if (!(tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)) {
3594 tp->phy_flags |= TG3_PHYFLG_IS_LOW_POWER;
Matt Carlsondd477002008-05-25 23:45:58 -07003595 tp->link_config.orig_speed = tp->link_config.speed;
3596 tp->link_config.orig_duplex = tp->link_config.duplex;
3597 tp->link_config.orig_autoneg = tp->link_config.autoneg;
3598 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003599
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003600 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES)) {
Matt Carlsondd477002008-05-25 23:45:58 -07003601 tp->link_config.speed = SPEED_10;
3602 tp->link_config.duplex = DUPLEX_HALF;
3603 tp->link_config.autoneg = AUTONEG_ENABLE;
3604 tg3_setup_phy(tp, 0);
3605 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003606 }
3607
Michael Chanb5d37722006-09-27 16:06:21 -07003608 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
3609 u32 val;
3610
3611 val = tr32(GRC_VCPU_EXT_CTRL);
3612 tw32(GRC_VCPU_EXT_CTRL, val | GRC_VCPU_EXT_CTRL_DISABLE_WOL);
Joe Perches63c3a662011-04-26 08:12:10 +00003613 } else if (!tg3_flag(tp, ENABLE_ASF)) {
Michael Chan6921d202005-12-13 21:15:53 -08003614 int i;
3615 u32 val;
3616
3617 for (i = 0; i < 200; i++) {
3618 tg3_read_mem(tp, NIC_SRAM_FW_ASF_STATUS_MBOX, &val);
3619 if (val == ~NIC_SRAM_FIRMWARE_MBOX_MAGIC1)
3620 break;
3621 msleep(1);
3622 }
3623 }
Joe Perches63c3a662011-04-26 08:12:10 +00003624 if (tg3_flag(tp, WOL_CAP))
Gary Zambranoa85feb82007-05-05 11:52:19 -07003625 tg3_write_mem(tp, NIC_SRAM_WOL_MBOX, WOL_SIGNATURE |
3626 WOL_DRV_STATE_SHUTDOWN |
3627 WOL_DRV_WOL |
3628 WOL_SET_MAGIC_PKT);
Michael Chan6921d202005-12-13 21:15:53 -08003629
Matt Carlson05ac4cb2008-11-03 16:53:46 -08003630 if (device_should_wake) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003631 u32 mac_mode;
3632
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003633 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES)) {
Matt Carlsonb4bd2922011-04-20 07:57:41 +00003634 if (do_low_power &&
3635 !(tp->phy_flags & TG3_PHYFLG_IS_FET)) {
3636 tg3_phy_auxctl_write(tp,
3637 MII_TG3_AUXCTL_SHDWSEL_PWRCTL,
3638 MII_TG3_AUXCTL_PCTL_WOL_EN |
3639 MII_TG3_AUXCTL_PCTL_100TX_LPWR |
3640 MII_TG3_AUXCTL_PCTL_CL_AB_TXDAC);
Matt Carlsondd477002008-05-25 23:45:58 -07003641 udelay(40);
3642 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003643
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003644 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
Michael Chan3f7045c2006-09-27 16:02:29 -07003645 mac_mode = MAC_MODE_PORT_MODE_GMII;
3646 else
3647 mac_mode = MAC_MODE_PORT_MODE_MII;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003648
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07003649 mac_mode |= tp->mac_mode & MAC_MODE_LINK_POLARITY;
3650 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
3651 ASIC_REV_5700) {
Joe Perches63c3a662011-04-26 08:12:10 +00003652 u32 speed = tg3_flag(tp, WOL_SPEED_100MB) ?
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07003653 SPEED_100 : SPEED_10;
3654 if (tg3_5700_link_polarity(tp, speed))
3655 mac_mode |= MAC_MODE_LINK_POLARITY;
3656 else
3657 mac_mode &= ~MAC_MODE_LINK_POLARITY;
3658 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003659 } else {
3660 mac_mode = MAC_MODE_PORT_MODE_TBI;
3661 }
3662
Joe Perches63c3a662011-04-26 08:12:10 +00003663 if (!tg3_flag(tp, 5750_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003664 tw32(MAC_LED_CTRL, tp->led_ctrl);
3665
Matt Carlson05ac4cb2008-11-03 16:53:46 -08003666 mac_mode |= MAC_MODE_MAGIC_PKT_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00003667 if ((tg3_flag(tp, 5705_PLUS) && !tg3_flag(tp, 5780_CLASS)) &&
3668 (tg3_flag(tp, ENABLE_ASF) || tg3_flag(tp, ENABLE_APE)))
Matt Carlson05ac4cb2008-11-03 16:53:46 -08003669 mac_mode |= MAC_MODE_KEEP_FRAME_IN_WOL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003670
Joe Perches63c3a662011-04-26 08:12:10 +00003671 if (tg3_flag(tp, ENABLE_APE))
Matt Carlsond2394e6b2010-11-24 08:31:47 +00003672 mac_mode |= MAC_MODE_APE_TX_EN |
3673 MAC_MODE_APE_RX_EN |
3674 MAC_MODE_TDE_ENABLE;
Matt Carlson3bda1252008-08-15 14:08:22 -07003675
Linus Torvalds1da177e2005-04-16 15:20:36 -07003676 tw32_f(MAC_MODE, mac_mode);
3677 udelay(100);
3678
3679 tw32_f(MAC_RX_MODE, RX_MODE_ENABLE);
3680 udelay(10);
3681 }
3682
Joe Perches63c3a662011-04-26 08:12:10 +00003683 if (!tg3_flag(tp, WOL_SPEED_100MB) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003684 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
3685 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)) {
3686 u32 base_val;
3687
3688 base_val = tp->pci_clock_ctrl;
3689 base_val |= (CLOCK_CTRL_RXCLK_DISABLE |
3690 CLOCK_CTRL_TXCLK_DISABLE);
3691
Michael Chanb401e9e2005-12-19 16:27:04 -08003692 tw32_wait_f(TG3PCI_CLOCK_CTRL, base_val | CLOCK_CTRL_ALTCLK |
3693 CLOCK_CTRL_PWRDOWN_PLL133, 40);
Joe Perches63c3a662011-04-26 08:12:10 +00003694 } else if (tg3_flag(tp, 5780_CLASS) ||
3695 tg3_flag(tp, CPMU_PRESENT) ||
Matt Carlson6ff6f812011-05-19 12:12:54 +00003696 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan4cf78e42005-07-25 12:29:19 -07003697 /* do nothing */
Joe Perches63c3a662011-04-26 08:12:10 +00003698 } else if (!(tg3_flag(tp, 5750_PLUS) && tg3_flag(tp, ENABLE_ASF))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003699 u32 newbits1, newbits2;
3700
3701 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
3702 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
3703 newbits1 = (CLOCK_CTRL_RXCLK_DISABLE |
3704 CLOCK_CTRL_TXCLK_DISABLE |
3705 CLOCK_CTRL_ALTCLK);
3706 newbits2 = newbits1 | CLOCK_CTRL_44MHZ_CORE;
Joe Perches63c3a662011-04-26 08:12:10 +00003707 } else if (tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003708 newbits1 = CLOCK_CTRL_625_CORE;
3709 newbits2 = newbits1 | CLOCK_CTRL_ALTCLK;
3710 } else {
3711 newbits1 = CLOCK_CTRL_ALTCLK;
3712 newbits2 = newbits1 | CLOCK_CTRL_44MHZ_CORE;
3713 }
3714
Michael Chanb401e9e2005-12-19 16:27:04 -08003715 tw32_wait_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl | newbits1,
3716 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003717
Michael Chanb401e9e2005-12-19 16:27:04 -08003718 tw32_wait_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl | newbits2,
3719 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003720
Joe Perches63c3a662011-04-26 08:12:10 +00003721 if (!tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003722 u32 newbits3;
3723
3724 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
3725 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
3726 newbits3 = (CLOCK_CTRL_RXCLK_DISABLE |
3727 CLOCK_CTRL_TXCLK_DISABLE |
3728 CLOCK_CTRL_44MHZ_CORE);
3729 } else {
3730 newbits3 = CLOCK_CTRL_44MHZ_CORE;
3731 }
3732
Michael Chanb401e9e2005-12-19 16:27:04 -08003733 tw32_wait_f(TG3PCI_CLOCK_CTRL,
3734 tp->pci_clock_ctrl | newbits3, 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003735 }
3736 }
3737
Joe Perches63c3a662011-04-26 08:12:10 +00003738 if (!(device_should_wake) && !tg3_flag(tp, ENABLE_ASF))
Matt Carlson0a459aa2008-11-03 16:54:15 -08003739 tg3_power_down_phy(tp, do_low_power);
Michael Chan6921d202005-12-13 21:15:53 -08003740
Matt Carlsoncd0d7222011-07-13 09:27:33 +00003741 tg3_frob_aux_power(tp, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003742
3743 /* Workaround for unstable PLL clock */
3744 if ((GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_AX) ||
3745 (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_BX)) {
3746 u32 val = tr32(0x7d00);
3747
3748 val &= ~((1 << 16) | (1 << 4) | (1 << 2) | (1 << 1) | 1);
3749 tw32(0x7d00, val);
Joe Perches63c3a662011-04-26 08:12:10 +00003750 if (!tg3_flag(tp, ENABLE_ASF)) {
Michael Chanec41c7d2006-01-17 02:40:55 -08003751 int err;
3752
3753 err = tg3_nvram_lock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003754 tg3_halt_cpu(tp, RX_CPU_BASE);
Michael Chanec41c7d2006-01-17 02:40:55 -08003755 if (!err)
3756 tg3_nvram_unlock(tp);
Michael Chan6921d202005-12-13 21:15:53 -08003757 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003758 }
3759
Michael Chanbbadf502006-04-06 21:46:34 -07003760 tg3_write_sig_post_reset(tp, RESET_KIND_SHUTDOWN);
3761
Linus Torvalds1da177e2005-04-16 15:20:36 -07003762 return 0;
3763}
3764
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003765static void tg3_power_down(struct tg3 *tp)
3766{
3767 tg3_power_down_prepare(tp);
3768
Joe Perches63c3a662011-04-26 08:12:10 +00003769 pci_wake_from_d3(tp->pdev, tg3_flag(tp, WOL_ENABLE));
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003770 pci_set_power_state(tp->pdev, PCI_D3hot);
3771}
3772
Linus Torvalds1da177e2005-04-16 15:20:36 -07003773static void tg3_aux_stat_to_speed_duplex(struct tg3 *tp, u32 val, u16 *speed, u8 *duplex)
3774{
3775 switch (val & MII_TG3_AUX_STAT_SPDMASK) {
3776 case MII_TG3_AUX_STAT_10HALF:
3777 *speed = SPEED_10;
3778 *duplex = DUPLEX_HALF;
3779 break;
3780
3781 case MII_TG3_AUX_STAT_10FULL:
3782 *speed = SPEED_10;
3783 *duplex = DUPLEX_FULL;
3784 break;
3785
3786 case MII_TG3_AUX_STAT_100HALF:
3787 *speed = SPEED_100;
3788 *duplex = DUPLEX_HALF;
3789 break;
3790
3791 case MII_TG3_AUX_STAT_100FULL:
3792 *speed = SPEED_100;
3793 *duplex = DUPLEX_FULL;
3794 break;
3795
3796 case MII_TG3_AUX_STAT_1000HALF:
3797 *speed = SPEED_1000;
3798 *duplex = DUPLEX_HALF;
3799 break;
3800
3801 case MII_TG3_AUX_STAT_1000FULL:
3802 *speed = SPEED_1000;
3803 *duplex = DUPLEX_FULL;
3804 break;
3805
3806 default:
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003807 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Michael Chan715116a2006-09-27 16:09:25 -07003808 *speed = (val & MII_TG3_AUX_STAT_100) ? SPEED_100 :
3809 SPEED_10;
3810 *duplex = (val & MII_TG3_AUX_STAT_FULL) ? DUPLEX_FULL :
3811 DUPLEX_HALF;
3812 break;
3813 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003814 *speed = SPEED_INVALID;
3815 *duplex = DUPLEX_INVALID;
3816 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07003817 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003818}
3819
Matt Carlson42b64a42011-05-19 12:12:49 +00003820static int tg3_phy_autoneg_cfg(struct tg3 *tp, u32 advertise, u32 flowctrl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003821{
Matt Carlson42b64a42011-05-19 12:12:49 +00003822 int err = 0;
3823 u32 val, new_adv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003824
Matt Carlson42b64a42011-05-19 12:12:49 +00003825 new_adv = ADVERTISE_CSMA;
Hiroaki SHIMODA202ff1c2011-11-22 04:05:41 +00003826 new_adv |= ethtool_adv_to_mii_adv_t(advertise) & ADVERTISE_ALL;
Matt Carlsonf88788f2011-12-14 11:10:00 +00003827 new_adv |= mii_advertise_flowctrl(flowctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003828
Matt Carlson42b64a42011-05-19 12:12:49 +00003829 err = tg3_writephy(tp, MII_ADVERTISE, new_adv);
3830 if (err)
3831 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003832
Matt Carlson4f272092011-12-14 11:09:57 +00003833 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
3834 new_adv = ethtool_adv_to_mii_ctrl1000_t(advertise);
Matt Carlsonba4d07a2007-12-20 20:08:00 -08003835
Matt Carlson4f272092011-12-14 11:09:57 +00003836 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
3837 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0)
3838 new_adv |= CTL1000_AS_MASTER | CTL1000_ENABLE_MASTER;
Matt Carlsonba4d07a2007-12-20 20:08:00 -08003839
Matt Carlson4f272092011-12-14 11:09:57 +00003840 err = tg3_writephy(tp, MII_CTRL1000, new_adv);
3841 if (err)
3842 goto done;
3843 }
Matt Carlsonba4d07a2007-12-20 20:08:00 -08003844
Matt Carlson42b64a42011-05-19 12:12:49 +00003845 if (!(tp->phy_flags & TG3_PHYFLG_EEE_CAP))
3846 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003847
Matt Carlson42b64a42011-05-19 12:12:49 +00003848 tw32(TG3_CPMU_EEE_MODE,
3849 tr32(TG3_CPMU_EEE_MODE) & ~TG3_CPMU_EEEMD_LPI_ENABLE);
Matt Carlsonba4d07a2007-12-20 20:08:00 -08003850
Matt Carlson42b64a42011-05-19 12:12:49 +00003851 err = TG3_PHY_AUXCTL_SMDSP_ENABLE(tp);
3852 if (!err) {
3853 u32 err2;
Matt Carlson52b02d02010-10-14 10:37:41 +00003854
Matt Carlsona6b68da2010-12-06 08:28:52 +00003855 val = 0;
Matt Carlson42b64a42011-05-19 12:12:49 +00003856 /* Advertise 100-BaseTX EEE ability */
3857 if (advertise & ADVERTISED_100baseT_Full)
3858 val |= MDIO_AN_EEE_ADV_100TX;
3859 /* Advertise 1000-BaseT EEE ability */
3860 if (advertise & ADVERTISED_1000baseT_Full)
3861 val |= MDIO_AN_EEE_ADV_1000T;
3862 err = tg3_phy_cl45_write(tp, MDIO_MMD_AN, MDIO_AN_EEE_ADV, val);
Matt Carlsonb715ce92011-07-20 10:20:52 +00003863 if (err)
3864 val = 0;
3865
3866 switch (GET_ASIC_REV(tp->pci_chip_rev_id)) {
3867 case ASIC_REV_5717:
3868 case ASIC_REV_57765:
Matt Carlson55086ad2011-12-14 11:09:59 +00003869 case ASIC_REV_57766:
Matt Carlsonb715ce92011-07-20 10:20:52 +00003870 case ASIC_REV_5719:
3871 /* If we advertised any eee advertisements above... */
3872 if (val)
3873 val = MII_TG3_DSP_TAP26_ALNOKO |
3874 MII_TG3_DSP_TAP26_RMRXSTO |
3875 MII_TG3_DSP_TAP26_OPCSINPT;
3876 tg3_phydsp_write(tp, MII_TG3_DSP_TAP26, val);
3877 /* Fall through */
3878 case ASIC_REV_5720:
3879 if (!tg3_phydsp_read(tp, MII_TG3_DSP_CH34TP2, &val))
3880 tg3_phydsp_write(tp, MII_TG3_DSP_CH34TP2, val |
3881 MII_TG3_DSP_CH34TP2_HIBW01);
3882 }
Matt Carlson52b02d02010-10-14 10:37:41 +00003883
Matt Carlson42b64a42011-05-19 12:12:49 +00003884 err2 = TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
3885 if (!err)
3886 err = err2;
3887 }
3888
3889done:
3890 return err;
3891}
3892
3893static void tg3_phy_copper_begin(struct tg3 *tp)
3894{
3895 u32 new_adv;
3896 int i;
3897
3898 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) {
3899 new_adv = ADVERTISED_10baseT_Half |
3900 ADVERTISED_10baseT_Full;
3901 if (tg3_flag(tp, WOL_SPEED_100MB))
3902 new_adv |= ADVERTISED_100baseT_Half |
3903 ADVERTISED_100baseT_Full;
3904
3905 tg3_phy_autoneg_cfg(tp, new_adv,
3906 FLOW_CTRL_TX | FLOW_CTRL_RX);
3907 } else if (tp->link_config.speed == SPEED_INVALID) {
3908 if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
3909 tp->link_config.advertising &=
3910 ~(ADVERTISED_1000baseT_Half |
3911 ADVERTISED_1000baseT_Full);
3912
3913 tg3_phy_autoneg_cfg(tp, tp->link_config.advertising,
3914 tp->link_config.flowctrl);
3915 } else {
3916 /* Asking for a specific link mode. */
3917 if (tp->link_config.speed == SPEED_1000) {
3918 if (tp->link_config.duplex == DUPLEX_FULL)
3919 new_adv = ADVERTISED_1000baseT_Full;
3920 else
3921 new_adv = ADVERTISED_1000baseT_Half;
3922 } else if (tp->link_config.speed == SPEED_100) {
3923 if (tp->link_config.duplex == DUPLEX_FULL)
3924 new_adv = ADVERTISED_100baseT_Full;
3925 else
3926 new_adv = ADVERTISED_100baseT_Half;
3927 } else {
3928 if (tp->link_config.duplex == DUPLEX_FULL)
3929 new_adv = ADVERTISED_10baseT_Full;
3930 else
3931 new_adv = ADVERTISED_10baseT_Half;
3932 }
3933
3934 tg3_phy_autoneg_cfg(tp, new_adv,
3935 tp->link_config.flowctrl);
Matt Carlson52b02d02010-10-14 10:37:41 +00003936 }
3937
Linus Torvalds1da177e2005-04-16 15:20:36 -07003938 if (tp->link_config.autoneg == AUTONEG_DISABLE &&
3939 tp->link_config.speed != SPEED_INVALID) {
3940 u32 bmcr, orig_bmcr;
3941
3942 tp->link_config.active_speed = tp->link_config.speed;
3943 tp->link_config.active_duplex = tp->link_config.duplex;
3944
3945 bmcr = 0;
3946 switch (tp->link_config.speed) {
3947 default:
3948 case SPEED_10:
3949 break;
3950
3951 case SPEED_100:
3952 bmcr |= BMCR_SPEED100;
3953 break;
3954
3955 case SPEED_1000:
Matt Carlson221c5632011-06-13 13:39:01 +00003956 bmcr |= BMCR_SPEED1000;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003957 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07003958 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003959
3960 if (tp->link_config.duplex == DUPLEX_FULL)
3961 bmcr |= BMCR_FULLDPLX;
3962
3963 if (!tg3_readphy(tp, MII_BMCR, &orig_bmcr) &&
3964 (bmcr != orig_bmcr)) {
3965 tg3_writephy(tp, MII_BMCR, BMCR_LOOPBACK);
3966 for (i = 0; i < 1500; i++) {
3967 u32 tmp;
3968
3969 udelay(10);
3970 if (tg3_readphy(tp, MII_BMSR, &tmp) ||
3971 tg3_readphy(tp, MII_BMSR, &tmp))
3972 continue;
3973 if (!(tmp & BMSR_LSTATUS)) {
3974 udelay(40);
3975 break;
3976 }
3977 }
3978 tg3_writephy(tp, MII_BMCR, bmcr);
3979 udelay(40);
3980 }
3981 } else {
3982 tg3_writephy(tp, MII_BMCR,
3983 BMCR_ANENABLE | BMCR_ANRESTART);
3984 }
3985}
3986
3987static int tg3_init_5401phy_dsp(struct tg3 *tp)
3988{
3989 int err;
3990
3991 /* Turn off tap power management. */
3992 /* Set Extended packet length bit */
Matt Carlsonb4bd2922011-04-20 07:57:41 +00003993 err = tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_AUXCTL, 0x4c20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003994
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00003995 err |= tg3_phydsp_write(tp, 0x0012, 0x1804);
3996 err |= tg3_phydsp_write(tp, 0x0013, 0x1204);
3997 err |= tg3_phydsp_write(tp, 0x8006, 0x0132);
3998 err |= tg3_phydsp_write(tp, 0x8006, 0x0232);
3999 err |= tg3_phydsp_write(tp, 0x201f, 0x0a20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004000
4001 udelay(40);
4002
4003 return err;
4004}
4005
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004006static bool tg3_phy_copper_an_config_ok(struct tg3 *tp, u32 *lcladv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004007{
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004008 u32 advmsk, tgtadv, advertising;
Michael Chan3600d912006-12-07 00:21:48 -08004009
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004010 advertising = tp->link_config.advertising;
4011 tgtadv = ethtool_adv_to_mii_adv_t(advertising) & ADVERTISE_ALL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004012
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004013 advmsk = ADVERTISE_ALL;
4014 if (tp->link_config.active_duplex == DUPLEX_FULL) {
Matt Carlsonf88788f2011-12-14 11:10:00 +00004015 tgtadv |= mii_advertise_flowctrl(tp->link_config.flowctrl);
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004016 advmsk |= ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
4017 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004018
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004019 if (tg3_readphy(tp, MII_ADVERTISE, lcladv))
4020 return false;
4021
4022 if ((*lcladv & advmsk) != tgtadv)
4023 return false;
Matt Carlsonb99d2a52011-08-31 11:44:47 +00004024
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004025 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004026 u32 tg3_ctrl;
4027
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004028 tgtadv = ethtool_adv_to_mii_ctrl1000_t(advertising);
Michael Chan3600d912006-12-07 00:21:48 -08004029
Matt Carlson221c5632011-06-13 13:39:01 +00004030 if (tg3_readphy(tp, MII_CTRL1000, &tg3_ctrl))
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004031 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004032
Matt Carlson3198e072012-02-13 15:20:10 +00004033 if (tgtadv &&
4034 (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
4035 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0)) {
4036 tgtadv |= CTL1000_AS_MASTER | CTL1000_ENABLE_MASTER;
4037 tg3_ctrl &= (ADVERTISE_1000HALF | ADVERTISE_1000FULL |
4038 CTL1000_AS_MASTER | CTL1000_ENABLE_MASTER);
4039 } else {
4040 tg3_ctrl &= (ADVERTISE_1000HALF | ADVERTISE_1000FULL);
4041 }
4042
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004043 if (tg3_ctrl != tgtadv)
4044 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004045 }
Matt Carlson93a700a2011-08-31 11:44:54 +00004046
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004047 return true;
Matt Carlsonef167e22007-12-20 20:10:01 -08004048}
4049
Matt Carlson859edb22011-12-08 14:40:16 +00004050static bool tg3_phy_copper_fetch_rmtadv(struct tg3 *tp, u32 *rmtadv)
4051{
4052 u32 lpeth = 0;
4053
4054 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
4055 u32 val;
4056
4057 if (tg3_readphy(tp, MII_STAT1000, &val))
4058 return false;
4059
4060 lpeth = mii_stat1000_to_ethtool_lpa_t(val);
4061 }
4062
4063 if (tg3_readphy(tp, MII_LPA, rmtadv))
4064 return false;
4065
4066 lpeth |= mii_lpa_to_ethtool_lpa_t(*rmtadv);
4067 tp->link_config.rmt_adv = lpeth;
4068
4069 return true;
4070}
4071
Linus Torvalds1da177e2005-04-16 15:20:36 -07004072static int tg3_setup_copper_phy(struct tg3 *tp, int force_reset)
4073{
4074 int current_link_up;
Matt Carlsonf833c4c2010-09-15 09:00:01 +00004075 u32 bmsr, val;
Matt Carlsonef167e22007-12-20 20:10:01 -08004076 u32 lcl_adv, rmt_adv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004077 u16 current_speed;
4078 u8 current_duplex;
4079 int i, err;
4080
4081 tw32(MAC_EVENT, 0);
4082
4083 tw32_f(MAC_STATUS,
4084 (MAC_STATUS_SYNC_CHANGED |
4085 MAC_STATUS_CFG_CHANGED |
4086 MAC_STATUS_MI_COMPLETION |
4087 MAC_STATUS_LNKSTATE_CHANGED));
4088 udelay(40);
4089
Matt Carlson8ef21422008-05-02 16:47:53 -07004090 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
4091 tw32_f(MAC_MI_MODE,
4092 (tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL));
4093 udelay(80);
4094 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004095
Matt Carlsonb4bd2922011-04-20 07:57:41 +00004096 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_PWRCTL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004097
4098 /* Some third-party PHYs need to be reset on link going
4099 * down.
4100 */
4101 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
4102 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
4103 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) &&
4104 netif_carrier_ok(tp->dev)) {
4105 tg3_readphy(tp, MII_BMSR, &bmsr);
4106 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
4107 !(bmsr & BMSR_LSTATUS))
4108 force_reset = 1;
4109 }
4110 if (force_reset)
4111 tg3_phy_reset(tp);
4112
Matt Carlson79eb6902010-02-17 15:17:03 +00004113 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004114 tg3_readphy(tp, MII_BMSR, &bmsr);
4115 if (tg3_readphy(tp, MII_BMSR, &bmsr) ||
Joe Perches63c3a662011-04-26 08:12:10 +00004116 !tg3_flag(tp, INIT_COMPLETE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004117 bmsr = 0;
4118
4119 if (!(bmsr & BMSR_LSTATUS)) {
4120 err = tg3_init_5401phy_dsp(tp);
4121 if (err)
4122 return err;
4123
4124 tg3_readphy(tp, MII_BMSR, &bmsr);
4125 for (i = 0; i < 1000; i++) {
4126 udelay(10);
4127 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
4128 (bmsr & BMSR_LSTATUS)) {
4129 udelay(40);
4130 break;
4131 }
4132 }
4133
Matt Carlson79eb6902010-02-17 15:17:03 +00004134 if ((tp->phy_id & TG3_PHY_ID_REV_MASK) ==
4135 TG3_PHY_REV_BCM5401_B0 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004136 !(bmsr & BMSR_LSTATUS) &&
4137 tp->link_config.active_speed == SPEED_1000) {
4138 err = tg3_phy_reset(tp);
4139 if (!err)
4140 err = tg3_init_5401phy_dsp(tp);
4141 if (err)
4142 return err;
4143 }
4144 }
4145 } else if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
4146 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0) {
4147 /* 5701 {A0,B0} CRC bug workaround */
4148 tg3_writephy(tp, 0x15, 0x0a75);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00004149 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8c68);
4150 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8d68);
4151 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8c68);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004152 }
4153
4154 /* Clear pending interrupts... */
Matt Carlsonf833c4c2010-09-15 09:00:01 +00004155 tg3_readphy(tp, MII_TG3_ISTAT, &val);
4156 tg3_readphy(tp, MII_TG3_ISTAT, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004157
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004158 if (tp->phy_flags & TG3_PHYFLG_USE_MI_INTERRUPT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004159 tg3_writephy(tp, MII_TG3_IMASK, ~MII_TG3_INT_LINKCHG);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004160 else if (!(tp->phy_flags & TG3_PHYFLG_IS_FET))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004161 tg3_writephy(tp, MII_TG3_IMASK, ~0);
4162
4163 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
4164 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
4165 if (tp->led_ctrl == LED_CTRL_MODE_PHY_1)
4166 tg3_writephy(tp, MII_TG3_EXT_CTRL,
4167 MII_TG3_EXT_CTRL_LNK3_LED_MODE);
4168 else
4169 tg3_writephy(tp, MII_TG3_EXT_CTRL, 0);
4170 }
4171
4172 current_link_up = 0;
4173 current_speed = SPEED_INVALID;
4174 current_duplex = DUPLEX_INVALID;
Matt Carlsone348c5e2011-11-21 15:01:20 +00004175 tp->phy_flags &= ~TG3_PHYFLG_MDIX_STATE;
Matt Carlson859edb22011-12-08 14:40:16 +00004176 tp->link_config.rmt_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004177
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004178 if (tp->phy_flags & TG3_PHYFLG_CAPACITIVE_COUPLING) {
Matt Carlson15ee95c2011-04-20 07:57:40 +00004179 err = tg3_phy_auxctl_read(tp,
4180 MII_TG3_AUXCTL_SHDWSEL_MISCTEST,
4181 &val);
4182 if (!err && !(val & (1 << 10))) {
Matt Carlsonb4bd2922011-04-20 07:57:41 +00004183 tg3_phy_auxctl_write(tp,
4184 MII_TG3_AUXCTL_SHDWSEL_MISCTEST,
4185 val | (1 << 10));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004186 goto relink;
4187 }
4188 }
4189
4190 bmsr = 0;
4191 for (i = 0; i < 100; i++) {
4192 tg3_readphy(tp, MII_BMSR, &bmsr);
4193 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
4194 (bmsr & BMSR_LSTATUS))
4195 break;
4196 udelay(40);
4197 }
4198
4199 if (bmsr & BMSR_LSTATUS) {
4200 u32 aux_stat, bmcr;
4201
4202 tg3_readphy(tp, MII_TG3_AUX_STAT, &aux_stat);
4203 for (i = 0; i < 2000; i++) {
4204 udelay(10);
4205 if (!tg3_readphy(tp, MII_TG3_AUX_STAT, &aux_stat) &&
4206 aux_stat)
4207 break;
4208 }
4209
4210 tg3_aux_stat_to_speed_duplex(tp, aux_stat,
4211 &current_speed,
4212 &current_duplex);
4213
4214 bmcr = 0;
4215 for (i = 0; i < 200; i++) {
4216 tg3_readphy(tp, MII_BMCR, &bmcr);
4217 if (tg3_readphy(tp, MII_BMCR, &bmcr))
4218 continue;
4219 if (bmcr && bmcr != 0x7fff)
4220 break;
4221 udelay(10);
4222 }
4223
Matt Carlsonef167e22007-12-20 20:10:01 -08004224 lcl_adv = 0;
4225 rmt_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004226
Matt Carlsonef167e22007-12-20 20:10:01 -08004227 tp->link_config.active_speed = current_speed;
4228 tp->link_config.active_duplex = current_duplex;
4229
4230 if (tp->link_config.autoneg == AUTONEG_ENABLE) {
4231 if ((bmcr & BMCR_ANENABLE) &&
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004232 tg3_phy_copper_an_config_ok(tp, &lcl_adv) &&
Matt Carlson859edb22011-12-08 14:40:16 +00004233 tg3_phy_copper_fetch_rmtadv(tp, &rmt_adv))
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004234 current_link_up = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004235 } else {
4236 if (!(bmcr & BMCR_ANENABLE) &&
4237 tp->link_config.speed == current_speed &&
Matt Carlsonef167e22007-12-20 20:10:01 -08004238 tp->link_config.duplex == current_duplex &&
4239 tp->link_config.flowctrl ==
4240 tp->link_config.active_flowctrl) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004241 current_link_up = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004242 }
4243 }
4244
Matt Carlsonef167e22007-12-20 20:10:01 -08004245 if (current_link_up == 1 &&
Matt Carlsone348c5e2011-11-21 15:01:20 +00004246 tp->link_config.active_duplex == DUPLEX_FULL) {
4247 u32 reg, bit;
4248
4249 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
4250 reg = MII_TG3_FET_GEN_STAT;
4251 bit = MII_TG3_FET_GEN_STAT_MDIXSTAT;
4252 } else {
4253 reg = MII_TG3_EXT_STAT;
4254 bit = MII_TG3_EXT_STAT_MDIX;
4255 }
4256
4257 if (!tg3_readphy(tp, reg, &val) && (val & bit))
4258 tp->phy_flags |= TG3_PHYFLG_MDIX_STATE;
4259
Matt Carlsonef167e22007-12-20 20:10:01 -08004260 tg3_setup_flow_control(tp, lcl_adv, rmt_adv);
Matt Carlsone348c5e2011-11-21 15:01:20 +00004261 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004262 }
4263
Linus Torvalds1da177e2005-04-16 15:20:36 -07004264relink:
Matt Carlson800960682010-08-02 11:26:06 +00004265 if (current_link_up == 0 || (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004266 tg3_phy_copper_begin(tp);
4267
Matt Carlsonf833c4c2010-09-15 09:00:01 +00004268 tg3_readphy(tp, MII_BMSR, &bmsr);
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00004269 if ((!tg3_readphy(tp, MII_BMSR, &bmsr) && (bmsr & BMSR_LSTATUS)) ||
4270 (tp->mac_mode & MAC_MODE_PORT_INT_LPBACK))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004271 current_link_up = 1;
4272 }
4273
4274 tp->mac_mode &= ~MAC_MODE_PORT_MODE_MASK;
4275 if (current_link_up == 1) {
4276 if (tp->link_config.active_speed == SPEED_100 ||
4277 tp->link_config.active_speed == SPEED_10)
4278 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
4279 else
4280 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004281 } else if (tp->phy_flags & TG3_PHYFLG_IS_FET)
Matt Carlson7f97a4b2009-08-25 10:10:03 +00004282 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
4283 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004284 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
4285
4286 tp->mac_mode &= ~MAC_MODE_HALF_DUPLEX;
4287 if (tp->link_config.active_duplex == DUPLEX_HALF)
4288 tp->mac_mode |= MAC_MODE_HALF_DUPLEX;
4289
Linus Torvalds1da177e2005-04-16 15:20:36 -07004290 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700) {
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07004291 if (current_link_up == 1 &&
4292 tg3_5700_link_polarity(tp, tp->link_config.active_speed))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004293 tp->mac_mode |= MAC_MODE_LINK_POLARITY;
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07004294 else
4295 tp->mac_mode &= ~MAC_MODE_LINK_POLARITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004296 }
4297
4298 /* ??? Without this setting Netgear GA302T PHY does not
4299 * ??? send/receive packets...
4300 */
Matt Carlson79eb6902010-02-17 15:17:03 +00004301 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5411 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004302 tp->pci_chip_rev_id == CHIPREV_ID_5700_ALTIMA) {
4303 tp->mi_mode |= MAC_MI_MODE_AUTO_POLL;
4304 tw32_f(MAC_MI_MODE, tp->mi_mode);
4305 udelay(80);
4306 }
4307
4308 tw32_f(MAC_MODE, tp->mac_mode);
4309 udelay(40);
4310
Matt Carlson52b02d02010-10-14 10:37:41 +00004311 tg3_phy_eee_adjust(tp, current_link_up);
4312
Joe Perches63c3a662011-04-26 08:12:10 +00004313 if (tg3_flag(tp, USE_LINKCHG_REG)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004314 /* Polled via timer. */
4315 tw32_f(MAC_EVENT, 0);
4316 } else {
4317 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
4318 }
4319 udelay(40);
4320
4321 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 &&
4322 current_link_up == 1 &&
4323 tp->link_config.active_speed == SPEED_1000 &&
Joe Perches63c3a662011-04-26 08:12:10 +00004324 (tg3_flag(tp, PCIX_MODE) || tg3_flag(tp, PCI_HIGH_SPEED))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004325 udelay(120);
4326 tw32_f(MAC_STATUS,
4327 (MAC_STATUS_SYNC_CHANGED |
4328 MAC_STATUS_CFG_CHANGED));
4329 udelay(40);
4330 tg3_write_mem(tp,
4331 NIC_SRAM_FIRMWARE_MBOX,
4332 NIC_SRAM_FIRMWARE_MBOX_MAGIC2);
4333 }
4334
Matt Carlson5e7dfd02008-11-21 17:18:16 -08004335 /* Prevent send BD corruption. */
Joe Perches63c3a662011-04-26 08:12:10 +00004336 if (tg3_flag(tp, CLKREQ_BUG)) {
Matt Carlson5e7dfd02008-11-21 17:18:16 -08004337 u16 oldlnkctl, newlnkctl;
4338
4339 pci_read_config_word(tp->pdev,
Jon Mason708ebb32011-06-27 12:56:50 +00004340 pci_pcie_cap(tp->pdev) + PCI_EXP_LNKCTL,
Matt Carlson5e7dfd02008-11-21 17:18:16 -08004341 &oldlnkctl);
4342 if (tp->link_config.active_speed == SPEED_100 ||
4343 tp->link_config.active_speed == SPEED_10)
4344 newlnkctl = oldlnkctl & ~PCI_EXP_LNKCTL_CLKREQ_EN;
4345 else
4346 newlnkctl = oldlnkctl | PCI_EXP_LNKCTL_CLKREQ_EN;
4347 if (newlnkctl != oldlnkctl)
4348 pci_write_config_word(tp->pdev,
Matt Carlson93a700a2011-08-31 11:44:54 +00004349 pci_pcie_cap(tp->pdev) +
4350 PCI_EXP_LNKCTL, newlnkctl);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08004351 }
4352
Linus Torvalds1da177e2005-04-16 15:20:36 -07004353 if (current_link_up != netif_carrier_ok(tp->dev)) {
4354 if (current_link_up)
4355 netif_carrier_on(tp->dev);
4356 else
4357 netif_carrier_off(tp->dev);
4358 tg3_link_report(tp);
4359 }
4360
4361 return 0;
4362}
4363
4364struct tg3_fiber_aneginfo {
4365 int state;
4366#define ANEG_STATE_UNKNOWN 0
4367#define ANEG_STATE_AN_ENABLE 1
4368#define ANEG_STATE_RESTART_INIT 2
4369#define ANEG_STATE_RESTART 3
4370#define ANEG_STATE_DISABLE_LINK_OK 4
4371#define ANEG_STATE_ABILITY_DETECT_INIT 5
4372#define ANEG_STATE_ABILITY_DETECT 6
4373#define ANEG_STATE_ACK_DETECT_INIT 7
4374#define ANEG_STATE_ACK_DETECT 8
4375#define ANEG_STATE_COMPLETE_ACK_INIT 9
4376#define ANEG_STATE_COMPLETE_ACK 10
4377#define ANEG_STATE_IDLE_DETECT_INIT 11
4378#define ANEG_STATE_IDLE_DETECT 12
4379#define ANEG_STATE_LINK_OK 13
4380#define ANEG_STATE_NEXT_PAGE_WAIT_INIT 14
4381#define ANEG_STATE_NEXT_PAGE_WAIT 15
4382
4383 u32 flags;
4384#define MR_AN_ENABLE 0x00000001
4385#define MR_RESTART_AN 0x00000002
4386#define MR_AN_COMPLETE 0x00000004
4387#define MR_PAGE_RX 0x00000008
4388#define MR_NP_LOADED 0x00000010
4389#define MR_TOGGLE_TX 0x00000020
4390#define MR_LP_ADV_FULL_DUPLEX 0x00000040
4391#define MR_LP_ADV_HALF_DUPLEX 0x00000080
4392#define MR_LP_ADV_SYM_PAUSE 0x00000100
4393#define MR_LP_ADV_ASYM_PAUSE 0x00000200
4394#define MR_LP_ADV_REMOTE_FAULT1 0x00000400
4395#define MR_LP_ADV_REMOTE_FAULT2 0x00000800
4396#define MR_LP_ADV_NEXT_PAGE 0x00001000
4397#define MR_TOGGLE_RX 0x00002000
4398#define MR_NP_RX 0x00004000
4399
4400#define MR_LINK_OK 0x80000000
4401
4402 unsigned long link_time, cur_time;
4403
4404 u32 ability_match_cfg;
4405 int ability_match_count;
4406
4407 char ability_match, idle_match, ack_match;
4408
4409 u32 txconfig, rxconfig;
4410#define ANEG_CFG_NP 0x00000080
4411#define ANEG_CFG_ACK 0x00000040
4412#define ANEG_CFG_RF2 0x00000020
4413#define ANEG_CFG_RF1 0x00000010
4414#define ANEG_CFG_PS2 0x00000001
4415#define ANEG_CFG_PS1 0x00008000
4416#define ANEG_CFG_HD 0x00004000
4417#define ANEG_CFG_FD 0x00002000
4418#define ANEG_CFG_INVAL 0x00001f06
4419
4420};
4421#define ANEG_OK 0
4422#define ANEG_DONE 1
4423#define ANEG_TIMER_ENAB 2
4424#define ANEG_FAILED -1
4425
4426#define ANEG_STATE_SETTLE_TIME 10000
4427
4428static int tg3_fiber_aneg_smachine(struct tg3 *tp,
4429 struct tg3_fiber_aneginfo *ap)
4430{
Matt Carlson5be73b42007-12-20 20:09:29 -08004431 u16 flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004432 unsigned long delta;
4433 u32 rx_cfg_reg;
4434 int ret;
4435
4436 if (ap->state == ANEG_STATE_UNKNOWN) {
4437 ap->rxconfig = 0;
4438 ap->link_time = 0;
4439 ap->cur_time = 0;
4440 ap->ability_match_cfg = 0;
4441 ap->ability_match_count = 0;
4442 ap->ability_match = 0;
4443 ap->idle_match = 0;
4444 ap->ack_match = 0;
4445 }
4446 ap->cur_time++;
4447
4448 if (tr32(MAC_STATUS) & MAC_STATUS_RCVD_CFG) {
4449 rx_cfg_reg = tr32(MAC_RX_AUTO_NEG);
4450
4451 if (rx_cfg_reg != ap->ability_match_cfg) {
4452 ap->ability_match_cfg = rx_cfg_reg;
4453 ap->ability_match = 0;
4454 ap->ability_match_count = 0;
4455 } else {
4456 if (++ap->ability_match_count > 1) {
4457 ap->ability_match = 1;
4458 ap->ability_match_cfg = rx_cfg_reg;
4459 }
4460 }
4461 if (rx_cfg_reg & ANEG_CFG_ACK)
4462 ap->ack_match = 1;
4463 else
4464 ap->ack_match = 0;
4465
4466 ap->idle_match = 0;
4467 } else {
4468 ap->idle_match = 1;
4469 ap->ability_match_cfg = 0;
4470 ap->ability_match_count = 0;
4471 ap->ability_match = 0;
4472 ap->ack_match = 0;
4473
4474 rx_cfg_reg = 0;
4475 }
4476
4477 ap->rxconfig = rx_cfg_reg;
4478 ret = ANEG_OK;
4479
Matt Carlson33f401a2010-04-05 10:19:27 +00004480 switch (ap->state) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004481 case ANEG_STATE_UNKNOWN:
4482 if (ap->flags & (MR_AN_ENABLE | MR_RESTART_AN))
4483 ap->state = ANEG_STATE_AN_ENABLE;
4484
4485 /* fallthru */
4486 case ANEG_STATE_AN_ENABLE:
4487 ap->flags &= ~(MR_AN_COMPLETE | MR_PAGE_RX);
4488 if (ap->flags & MR_AN_ENABLE) {
4489 ap->link_time = 0;
4490 ap->cur_time = 0;
4491 ap->ability_match_cfg = 0;
4492 ap->ability_match_count = 0;
4493 ap->ability_match = 0;
4494 ap->idle_match = 0;
4495 ap->ack_match = 0;
4496
4497 ap->state = ANEG_STATE_RESTART_INIT;
4498 } else {
4499 ap->state = ANEG_STATE_DISABLE_LINK_OK;
4500 }
4501 break;
4502
4503 case ANEG_STATE_RESTART_INIT:
4504 ap->link_time = ap->cur_time;
4505 ap->flags &= ~(MR_NP_LOADED);
4506 ap->txconfig = 0;
4507 tw32(MAC_TX_AUTO_NEG, 0);
4508 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
4509 tw32_f(MAC_MODE, tp->mac_mode);
4510 udelay(40);
4511
4512 ret = ANEG_TIMER_ENAB;
4513 ap->state = ANEG_STATE_RESTART;
4514
4515 /* fallthru */
4516 case ANEG_STATE_RESTART:
4517 delta = ap->cur_time - ap->link_time;
Matt Carlson859a588792010-04-05 10:19:28 +00004518 if (delta > ANEG_STATE_SETTLE_TIME)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004519 ap->state = ANEG_STATE_ABILITY_DETECT_INIT;
Matt Carlson859a588792010-04-05 10:19:28 +00004520 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004521 ret = ANEG_TIMER_ENAB;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004522 break;
4523
4524 case ANEG_STATE_DISABLE_LINK_OK:
4525 ret = ANEG_DONE;
4526 break;
4527
4528 case ANEG_STATE_ABILITY_DETECT_INIT:
4529 ap->flags &= ~(MR_TOGGLE_TX);
Matt Carlson5be73b42007-12-20 20:09:29 -08004530 ap->txconfig = ANEG_CFG_FD;
4531 flowctrl = tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
4532 if (flowctrl & ADVERTISE_1000XPAUSE)
4533 ap->txconfig |= ANEG_CFG_PS1;
4534 if (flowctrl & ADVERTISE_1000XPSE_ASYM)
4535 ap->txconfig |= ANEG_CFG_PS2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004536 tw32(MAC_TX_AUTO_NEG, ap->txconfig);
4537 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
4538 tw32_f(MAC_MODE, tp->mac_mode);
4539 udelay(40);
4540
4541 ap->state = ANEG_STATE_ABILITY_DETECT;
4542 break;
4543
4544 case ANEG_STATE_ABILITY_DETECT:
Matt Carlson859a588792010-04-05 10:19:28 +00004545 if (ap->ability_match != 0 && ap->rxconfig != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004546 ap->state = ANEG_STATE_ACK_DETECT_INIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004547 break;
4548
4549 case ANEG_STATE_ACK_DETECT_INIT:
4550 ap->txconfig |= ANEG_CFG_ACK;
4551 tw32(MAC_TX_AUTO_NEG, ap->txconfig);
4552 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
4553 tw32_f(MAC_MODE, tp->mac_mode);
4554 udelay(40);
4555
4556 ap->state = ANEG_STATE_ACK_DETECT;
4557
4558 /* fallthru */
4559 case ANEG_STATE_ACK_DETECT:
4560 if (ap->ack_match != 0) {
4561 if ((ap->rxconfig & ~ANEG_CFG_ACK) ==
4562 (ap->ability_match_cfg & ~ANEG_CFG_ACK)) {
4563 ap->state = ANEG_STATE_COMPLETE_ACK_INIT;
4564 } else {
4565 ap->state = ANEG_STATE_AN_ENABLE;
4566 }
4567 } else if (ap->ability_match != 0 &&
4568 ap->rxconfig == 0) {
4569 ap->state = ANEG_STATE_AN_ENABLE;
4570 }
4571 break;
4572
4573 case ANEG_STATE_COMPLETE_ACK_INIT:
4574 if (ap->rxconfig & ANEG_CFG_INVAL) {
4575 ret = ANEG_FAILED;
4576 break;
4577 }
4578 ap->flags &= ~(MR_LP_ADV_FULL_DUPLEX |
4579 MR_LP_ADV_HALF_DUPLEX |
4580 MR_LP_ADV_SYM_PAUSE |
4581 MR_LP_ADV_ASYM_PAUSE |
4582 MR_LP_ADV_REMOTE_FAULT1 |
4583 MR_LP_ADV_REMOTE_FAULT2 |
4584 MR_LP_ADV_NEXT_PAGE |
4585 MR_TOGGLE_RX |
4586 MR_NP_RX);
4587 if (ap->rxconfig & ANEG_CFG_FD)
4588 ap->flags |= MR_LP_ADV_FULL_DUPLEX;
4589 if (ap->rxconfig & ANEG_CFG_HD)
4590 ap->flags |= MR_LP_ADV_HALF_DUPLEX;
4591 if (ap->rxconfig & ANEG_CFG_PS1)
4592 ap->flags |= MR_LP_ADV_SYM_PAUSE;
4593 if (ap->rxconfig & ANEG_CFG_PS2)
4594 ap->flags |= MR_LP_ADV_ASYM_PAUSE;
4595 if (ap->rxconfig & ANEG_CFG_RF1)
4596 ap->flags |= MR_LP_ADV_REMOTE_FAULT1;
4597 if (ap->rxconfig & ANEG_CFG_RF2)
4598 ap->flags |= MR_LP_ADV_REMOTE_FAULT2;
4599 if (ap->rxconfig & ANEG_CFG_NP)
4600 ap->flags |= MR_LP_ADV_NEXT_PAGE;
4601
4602 ap->link_time = ap->cur_time;
4603
4604 ap->flags ^= (MR_TOGGLE_TX);
4605 if (ap->rxconfig & 0x0008)
4606 ap->flags |= MR_TOGGLE_RX;
4607 if (ap->rxconfig & ANEG_CFG_NP)
4608 ap->flags |= MR_NP_RX;
4609 ap->flags |= MR_PAGE_RX;
4610
4611 ap->state = ANEG_STATE_COMPLETE_ACK;
4612 ret = ANEG_TIMER_ENAB;
4613 break;
4614
4615 case ANEG_STATE_COMPLETE_ACK:
4616 if (ap->ability_match != 0 &&
4617 ap->rxconfig == 0) {
4618 ap->state = ANEG_STATE_AN_ENABLE;
4619 break;
4620 }
4621 delta = ap->cur_time - ap->link_time;
4622 if (delta > ANEG_STATE_SETTLE_TIME) {
4623 if (!(ap->flags & (MR_LP_ADV_NEXT_PAGE))) {
4624 ap->state = ANEG_STATE_IDLE_DETECT_INIT;
4625 } else {
4626 if ((ap->txconfig & ANEG_CFG_NP) == 0 &&
4627 !(ap->flags & MR_NP_RX)) {
4628 ap->state = ANEG_STATE_IDLE_DETECT_INIT;
4629 } else {
4630 ret = ANEG_FAILED;
4631 }
4632 }
4633 }
4634 break;
4635
4636 case ANEG_STATE_IDLE_DETECT_INIT:
4637 ap->link_time = ap->cur_time;
4638 tp->mac_mode &= ~MAC_MODE_SEND_CONFIGS;
4639 tw32_f(MAC_MODE, tp->mac_mode);
4640 udelay(40);
4641
4642 ap->state = ANEG_STATE_IDLE_DETECT;
4643 ret = ANEG_TIMER_ENAB;
4644 break;
4645
4646 case ANEG_STATE_IDLE_DETECT:
4647 if (ap->ability_match != 0 &&
4648 ap->rxconfig == 0) {
4649 ap->state = ANEG_STATE_AN_ENABLE;
4650 break;
4651 }
4652 delta = ap->cur_time - ap->link_time;
4653 if (delta > ANEG_STATE_SETTLE_TIME) {
4654 /* XXX another gem from the Broadcom driver :( */
4655 ap->state = ANEG_STATE_LINK_OK;
4656 }
4657 break;
4658
4659 case ANEG_STATE_LINK_OK:
4660 ap->flags |= (MR_AN_COMPLETE | MR_LINK_OK);
4661 ret = ANEG_DONE;
4662 break;
4663
4664 case ANEG_STATE_NEXT_PAGE_WAIT_INIT:
4665 /* ??? unimplemented */
4666 break;
4667
4668 case ANEG_STATE_NEXT_PAGE_WAIT:
4669 /* ??? unimplemented */
4670 break;
4671
4672 default:
4673 ret = ANEG_FAILED;
4674 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07004675 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004676
4677 return ret;
4678}
4679
Matt Carlson5be73b42007-12-20 20:09:29 -08004680static int fiber_autoneg(struct tg3 *tp, u32 *txflags, u32 *rxflags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004681{
4682 int res = 0;
4683 struct tg3_fiber_aneginfo aninfo;
4684 int status = ANEG_FAILED;
4685 unsigned int tick;
4686 u32 tmp;
4687
4688 tw32_f(MAC_TX_AUTO_NEG, 0);
4689
4690 tmp = tp->mac_mode & ~MAC_MODE_PORT_MODE_MASK;
4691 tw32_f(MAC_MODE, tmp | MAC_MODE_PORT_MODE_GMII);
4692 udelay(40);
4693
4694 tw32_f(MAC_MODE, tp->mac_mode | MAC_MODE_SEND_CONFIGS);
4695 udelay(40);
4696
4697 memset(&aninfo, 0, sizeof(aninfo));
4698 aninfo.flags |= MR_AN_ENABLE;
4699 aninfo.state = ANEG_STATE_UNKNOWN;
4700 aninfo.cur_time = 0;
4701 tick = 0;
4702 while (++tick < 195000) {
4703 status = tg3_fiber_aneg_smachine(tp, &aninfo);
4704 if (status == ANEG_DONE || status == ANEG_FAILED)
4705 break;
4706
4707 udelay(1);
4708 }
4709
4710 tp->mac_mode &= ~MAC_MODE_SEND_CONFIGS;
4711 tw32_f(MAC_MODE, tp->mac_mode);
4712 udelay(40);
4713
Matt Carlson5be73b42007-12-20 20:09:29 -08004714 *txflags = aninfo.txconfig;
4715 *rxflags = aninfo.flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004716
4717 if (status == ANEG_DONE &&
4718 (aninfo.flags & (MR_AN_COMPLETE | MR_LINK_OK |
4719 MR_LP_ADV_FULL_DUPLEX)))
4720 res = 1;
4721
4722 return res;
4723}
4724
4725static void tg3_init_bcm8002(struct tg3 *tp)
4726{
4727 u32 mac_status = tr32(MAC_STATUS);
4728 int i;
4729
4730 /* Reset when initting first time or we have a link. */
Joe Perches63c3a662011-04-26 08:12:10 +00004731 if (tg3_flag(tp, INIT_COMPLETE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004732 !(mac_status & MAC_STATUS_PCS_SYNCED))
4733 return;
4734
4735 /* Set PLL lock range. */
4736 tg3_writephy(tp, 0x16, 0x8007);
4737
4738 /* SW reset */
4739 tg3_writephy(tp, MII_BMCR, BMCR_RESET);
4740
4741 /* Wait for reset to complete. */
4742 /* XXX schedule_timeout() ... */
4743 for (i = 0; i < 500; i++)
4744 udelay(10);
4745
4746 /* Config mode; select PMA/Ch 1 regs. */
4747 tg3_writephy(tp, 0x10, 0x8411);
4748
4749 /* Enable auto-lock and comdet, select txclk for tx. */
4750 tg3_writephy(tp, 0x11, 0x0a10);
4751
4752 tg3_writephy(tp, 0x18, 0x00a0);
4753 tg3_writephy(tp, 0x16, 0x41ff);
4754
4755 /* Assert and deassert POR. */
4756 tg3_writephy(tp, 0x13, 0x0400);
4757 udelay(40);
4758 tg3_writephy(tp, 0x13, 0x0000);
4759
4760 tg3_writephy(tp, 0x11, 0x0a50);
4761 udelay(40);
4762 tg3_writephy(tp, 0x11, 0x0a10);
4763
4764 /* Wait for signal to stabilize */
4765 /* XXX schedule_timeout() ... */
4766 for (i = 0; i < 15000; i++)
4767 udelay(10);
4768
4769 /* Deselect the channel register so we can read the PHYID
4770 * later.
4771 */
4772 tg3_writephy(tp, 0x10, 0x8011);
4773}
4774
4775static int tg3_setup_fiber_hw_autoneg(struct tg3 *tp, u32 mac_status)
4776{
Matt Carlson82cd3d12007-12-20 20:09:00 -08004777 u16 flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004778 u32 sg_dig_ctrl, sg_dig_status;
4779 u32 serdes_cfg, expected_sg_dig_ctrl;
4780 int workaround, port_a;
4781 int current_link_up;
4782
4783 serdes_cfg = 0;
4784 expected_sg_dig_ctrl = 0;
4785 workaround = 0;
4786 port_a = 1;
4787 current_link_up = 0;
4788
4789 if (tp->pci_chip_rev_id != CHIPREV_ID_5704_A0 &&
4790 tp->pci_chip_rev_id != CHIPREV_ID_5704_A1) {
4791 workaround = 1;
4792 if (tr32(TG3PCI_DUAL_MAC_CTRL) & DUAL_MAC_CTRL_ID)
4793 port_a = 0;
4794
4795 /* preserve bits 0-11,13,14 for signal pre-emphasis */
4796 /* preserve bits 20-23 for voltage regulator */
4797 serdes_cfg = tr32(MAC_SERDES_CFG) & 0x00f06fff;
4798 }
4799
4800 sg_dig_ctrl = tr32(SG_DIG_CTRL);
4801
4802 if (tp->link_config.autoneg != AUTONEG_ENABLE) {
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004803 if (sg_dig_ctrl & SG_DIG_USING_HW_AUTONEG) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004804 if (workaround) {
4805 u32 val = serdes_cfg;
4806
4807 if (port_a)
4808 val |= 0xc010000;
4809 else
4810 val |= 0x4010000;
4811 tw32_f(MAC_SERDES_CFG, val);
4812 }
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004813
4814 tw32_f(SG_DIG_CTRL, SG_DIG_COMMON_SETUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004815 }
4816 if (mac_status & MAC_STATUS_PCS_SYNCED) {
4817 tg3_setup_flow_control(tp, 0, 0);
4818 current_link_up = 1;
4819 }
4820 goto out;
4821 }
4822
4823 /* Want auto-negotiation. */
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004824 expected_sg_dig_ctrl = SG_DIG_USING_HW_AUTONEG | SG_DIG_COMMON_SETUP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004825
Matt Carlson82cd3d12007-12-20 20:09:00 -08004826 flowctrl = tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
4827 if (flowctrl & ADVERTISE_1000XPAUSE)
4828 expected_sg_dig_ctrl |= SG_DIG_PAUSE_CAP;
4829 if (flowctrl & ADVERTISE_1000XPSE_ASYM)
4830 expected_sg_dig_ctrl |= SG_DIG_ASYM_PAUSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004831
4832 if (sg_dig_ctrl != expected_sg_dig_ctrl) {
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004833 if ((tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT) &&
Michael Chan3d3ebe72006-09-27 15:59:15 -07004834 tp->serdes_counter &&
4835 ((mac_status & (MAC_STATUS_PCS_SYNCED |
4836 MAC_STATUS_RCVD_CFG)) ==
4837 MAC_STATUS_PCS_SYNCED)) {
4838 tp->serdes_counter--;
4839 current_link_up = 1;
4840 goto out;
4841 }
4842restart_autoneg:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004843 if (workaround)
4844 tw32_f(MAC_SERDES_CFG, serdes_cfg | 0xc011000);
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004845 tw32_f(SG_DIG_CTRL, expected_sg_dig_ctrl | SG_DIG_SOFT_RESET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004846 udelay(5);
4847 tw32_f(SG_DIG_CTRL, expected_sg_dig_ctrl);
4848
Michael Chan3d3ebe72006-09-27 15:59:15 -07004849 tp->serdes_counter = SERDES_AN_TIMEOUT_5704S;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004850 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004851 } else if (mac_status & (MAC_STATUS_PCS_SYNCED |
4852 MAC_STATUS_SIGNAL_DET)) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07004853 sg_dig_status = tr32(SG_DIG_STATUS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004854 mac_status = tr32(MAC_STATUS);
4855
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004856 if ((sg_dig_status & SG_DIG_AUTONEG_COMPLETE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004857 (mac_status & MAC_STATUS_PCS_SYNCED)) {
Matt Carlson82cd3d12007-12-20 20:09:00 -08004858 u32 local_adv = 0, remote_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004859
Matt Carlson82cd3d12007-12-20 20:09:00 -08004860 if (sg_dig_ctrl & SG_DIG_PAUSE_CAP)
4861 local_adv |= ADVERTISE_1000XPAUSE;
4862 if (sg_dig_ctrl & SG_DIG_ASYM_PAUSE)
4863 local_adv |= ADVERTISE_1000XPSE_ASYM;
4864
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004865 if (sg_dig_status & SG_DIG_PARTNER_PAUSE_CAPABLE)
Matt Carlson82cd3d12007-12-20 20:09:00 -08004866 remote_adv |= LPA_1000XPAUSE;
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004867 if (sg_dig_status & SG_DIG_PARTNER_ASYM_PAUSE)
Matt Carlson82cd3d12007-12-20 20:09:00 -08004868 remote_adv |= LPA_1000XPAUSE_ASYM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004869
Matt Carlson859edb22011-12-08 14:40:16 +00004870 tp->link_config.rmt_adv =
4871 mii_adv_to_ethtool_adv_x(remote_adv);
4872
Linus Torvalds1da177e2005-04-16 15:20:36 -07004873 tg3_setup_flow_control(tp, local_adv, remote_adv);
4874 current_link_up = 1;
Michael Chan3d3ebe72006-09-27 15:59:15 -07004875 tp->serdes_counter = 0;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004876 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004877 } else if (!(sg_dig_status & SG_DIG_AUTONEG_COMPLETE)) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07004878 if (tp->serdes_counter)
4879 tp->serdes_counter--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004880 else {
4881 if (workaround) {
4882 u32 val = serdes_cfg;
4883
4884 if (port_a)
4885 val |= 0xc010000;
4886 else
4887 val |= 0x4010000;
4888
4889 tw32_f(MAC_SERDES_CFG, val);
4890 }
4891
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004892 tw32_f(SG_DIG_CTRL, SG_DIG_COMMON_SETUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004893 udelay(40);
4894
4895 /* Link parallel detection - link is up */
4896 /* only if we have PCS_SYNC and not */
4897 /* receiving config code words */
4898 mac_status = tr32(MAC_STATUS);
4899 if ((mac_status & MAC_STATUS_PCS_SYNCED) &&
4900 !(mac_status & MAC_STATUS_RCVD_CFG)) {
4901 tg3_setup_flow_control(tp, 0, 0);
4902 current_link_up = 1;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004903 tp->phy_flags |=
4904 TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan3d3ebe72006-09-27 15:59:15 -07004905 tp->serdes_counter =
4906 SERDES_PARALLEL_DET_TIMEOUT;
4907 } else
4908 goto restart_autoneg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004909 }
4910 }
Michael Chan3d3ebe72006-09-27 15:59:15 -07004911 } else {
4912 tp->serdes_counter = SERDES_AN_TIMEOUT_5704S;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004913 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004914 }
4915
4916out:
4917 return current_link_up;
4918}
4919
4920static int tg3_setup_fiber_by_hand(struct tg3 *tp, u32 mac_status)
4921{
4922 int current_link_up = 0;
4923
Michael Chan5cf64b8a2007-05-05 12:11:21 -07004924 if (!(mac_status & MAC_STATUS_PCS_SYNCED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004925 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004926
4927 if (tp->link_config.autoneg == AUTONEG_ENABLE) {
Matt Carlson5be73b42007-12-20 20:09:29 -08004928 u32 txflags, rxflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004929 int i;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04004930
Matt Carlson5be73b42007-12-20 20:09:29 -08004931 if (fiber_autoneg(tp, &txflags, &rxflags)) {
4932 u32 local_adv = 0, remote_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004933
Matt Carlson5be73b42007-12-20 20:09:29 -08004934 if (txflags & ANEG_CFG_PS1)
4935 local_adv |= ADVERTISE_1000XPAUSE;
4936 if (txflags & ANEG_CFG_PS2)
4937 local_adv |= ADVERTISE_1000XPSE_ASYM;
4938
4939 if (rxflags & MR_LP_ADV_SYM_PAUSE)
4940 remote_adv |= LPA_1000XPAUSE;
4941 if (rxflags & MR_LP_ADV_ASYM_PAUSE)
4942 remote_adv |= LPA_1000XPAUSE_ASYM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004943
Matt Carlson859edb22011-12-08 14:40:16 +00004944 tp->link_config.rmt_adv =
4945 mii_adv_to_ethtool_adv_x(remote_adv);
4946
Linus Torvalds1da177e2005-04-16 15:20:36 -07004947 tg3_setup_flow_control(tp, local_adv, remote_adv);
4948
Linus Torvalds1da177e2005-04-16 15:20:36 -07004949 current_link_up = 1;
4950 }
4951 for (i = 0; i < 30; i++) {
4952 udelay(20);
4953 tw32_f(MAC_STATUS,
4954 (MAC_STATUS_SYNC_CHANGED |
4955 MAC_STATUS_CFG_CHANGED));
4956 udelay(40);
4957 if ((tr32(MAC_STATUS) &
4958 (MAC_STATUS_SYNC_CHANGED |
4959 MAC_STATUS_CFG_CHANGED)) == 0)
4960 break;
4961 }
4962
4963 mac_status = tr32(MAC_STATUS);
4964 if (current_link_up == 0 &&
4965 (mac_status & MAC_STATUS_PCS_SYNCED) &&
4966 !(mac_status & MAC_STATUS_RCVD_CFG))
4967 current_link_up = 1;
4968 } else {
Matt Carlson5be73b42007-12-20 20:09:29 -08004969 tg3_setup_flow_control(tp, 0, 0);
4970
Linus Torvalds1da177e2005-04-16 15:20:36 -07004971 /* Forcing 1000FD link up. */
4972 current_link_up = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004973
4974 tw32_f(MAC_MODE, (tp->mac_mode | MAC_MODE_SEND_CONFIGS));
4975 udelay(40);
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07004976
4977 tw32_f(MAC_MODE, tp->mac_mode);
4978 udelay(40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004979 }
4980
4981out:
4982 return current_link_up;
4983}
4984
4985static int tg3_setup_fiber_phy(struct tg3 *tp, int force_reset)
4986{
4987 u32 orig_pause_cfg;
4988 u16 orig_active_speed;
4989 u8 orig_active_duplex;
4990 u32 mac_status;
4991 int current_link_up;
4992 int i;
4993
Matt Carlson8d018622007-12-20 20:05:44 -08004994 orig_pause_cfg = tp->link_config.active_flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004995 orig_active_speed = tp->link_config.active_speed;
4996 orig_active_duplex = tp->link_config.active_duplex;
4997
Joe Perches63c3a662011-04-26 08:12:10 +00004998 if (!tg3_flag(tp, HW_AUTONEG) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004999 netif_carrier_ok(tp->dev) &&
Joe Perches63c3a662011-04-26 08:12:10 +00005000 tg3_flag(tp, INIT_COMPLETE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005001 mac_status = tr32(MAC_STATUS);
5002 mac_status &= (MAC_STATUS_PCS_SYNCED |
5003 MAC_STATUS_SIGNAL_DET |
5004 MAC_STATUS_CFG_CHANGED |
5005 MAC_STATUS_RCVD_CFG);
5006 if (mac_status == (MAC_STATUS_PCS_SYNCED |
5007 MAC_STATUS_SIGNAL_DET)) {
5008 tw32_f(MAC_STATUS, (MAC_STATUS_SYNC_CHANGED |
5009 MAC_STATUS_CFG_CHANGED));
5010 return 0;
5011 }
5012 }
5013
5014 tw32_f(MAC_TX_AUTO_NEG, 0);
5015
5016 tp->mac_mode &= ~(MAC_MODE_PORT_MODE_MASK | MAC_MODE_HALF_DUPLEX);
5017 tp->mac_mode |= MAC_MODE_PORT_MODE_TBI;
5018 tw32_f(MAC_MODE, tp->mac_mode);
5019 udelay(40);
5020
Matt Carlson79eb6902010-02-17 15:17:03 +00005021 if (tp->phy_id == TG3_PHY_ID_BCM8002)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005022 tg3_init_bcm8002(tp);
5023
5024 /* Enable link change event even when serdes polling. */
5025 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
5026 udelay(40);
5027
5028 current_link_up = 0;
Matt Carlson859edb22011-12-08 14:40:16 +00005029 tp->link_config.rmt_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005030 mac_status = tr32(MAC_STATUS);
5031
Joe Perches63c3a662011-04-26 08:12:10 +00005032 if (tg3_flag(tp, HW_AUTONEG))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005033 current_link_up = tg3_setup_fiber_hw_autoneg(tp, mac_status);
5034 else
5035 current_link_up = tg3_setup_fiber_by_hand(tp, mac_status);
5036
Matt Carlson898a56f2009-08-28 14:02:40 +00005037 tp->napi[0].hw_status->status =
Linus Torvalds1da177e2005-04-16 15:20:36 -07005038 (SD_STATUS_UPDATED |
Matt Carlson898a56f2009-08-28 14:02:40 +00005039 (tp->napi[0].hw_status->status & ~SD_STATUS_LINK_CHG));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005040
5041 for (i = 0; i < 100; i++) {
5042 tw32_f(MAC_STATUS, (MAC_STATUS_SYNC_CHANGED |
5043 MAC_STATUS_CFG_CHANGED));
5044 udelay(5);
5045 if ((tr32(MAC_STATUS) & (MAC_STATUS_SYNC_CHANGED |
Michael Chan3d3ebe72006-09-27 15:59:15 -07005046 MAC_STATUS_CFG_CHANGED |
5047 MAC_STATUS_LNKSTATE_CHANGED)) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005048 break;
5049 }
5050
5051 mac_status = tr32(MAC_STATUS);
5052 if ((mac_status & MAC_STATUS_PCS_SYNCED) == 0) {
5053 current_link_up = 0;
Michael Chan3d3ebe72006-09-27 15:59:15 -07005054 if (tp->link_config.autoneg == AUTONEG_ENABLE &&
5055 tp->serdes_counter == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005056 tw32_f(MAC_MODE, (tp->mac_mode |
5057 MAC_MODE_SEND_CONFIGS));
5058 udelay(1);
5059 tw32_f(MAC_MODE, tp->mac_mode);
5060 }
5061 }
5062
5063 if (current_link_up == 1) {
5064 tp->link_config.active_speed = SPEED_1000;
5065 tp->link_config.active_duplex = DUPLEX_FULL;
5066 tw32(MAC_LED_CTRL, (tp->led_ctrl |
5067 LED_CTRL_LNKLED_OVERRIDE |
5068 LED_CTRL_1000MBPS_ON));
5069 } else {
5070 tp->link_config.active_speed = SPEED_INVALID;
5071 tp->link_config.active_duplex = DUPLEX_INVALID;
5072 tw32(MAC_LED_CTRL, (tp->led_ctrl |
5073 LED_CTRL_LNKLED_OVERRIDE |
5074 LED_CTRL_TRAFFIC_OVERRIDE));
5075 }
5076
5077 if (current_link_up != netif_carrier_ok(tp->dev)) {
5078 if (current_link_up)
5079 netif_carrier_on(tp->dev);
5080 else
5081 netif_carrier_off(tp->dev);
5082 tg3_link_report(tp);
5083 } else {
Matt Carlson8d018622007-12-20 20:05:44 -08005084 u32 now_pause_cfg = tp->link_config.active_flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005085 if (orig_pause_cfg != now_pause_cfg ||
5086 orig_active_speed != tp->link_config.active_speed ||
5087 orig_active_duplex != tp->link_config.active_duplex)
5088 tg3_link_report(tp);
5089 }
5090
5091 return 0;
5092}
5093
Michael Chan747e8f82005-07-25 12:33:22 -07005094static int tg3_setup_fiber_mii_phy(struct tg3 *tp, int force_reset)
5095{
5096 int current_link_up, err = 0;
5097 u32 bmsr, bmcr;
5098 u16 current_speed;
5099 u8 current_duplex;
Matt Carlsonef167e22007-12-20 20:10:01 -08005100 u32 local_adv, remote_adv;
Michael Chan747e8f82005-07-25 12:33:22 -07005101
5102 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
5103 tw32_f(MAC_MODE, tp->mac_mode);
5104 udelay(40);
5105
5106 tw32(MAC_EVENT, 0);
5107
5108 tw32_f(MAC_STATUS,
5109 (MAC_STATUS_SYNC_CHANGED |
5110 MAC_STATUS_CFG_CHANGED |
5111 MAC_STATUS_MI_COMPLETION |
5112 MAC_STATUS_LNKSTATE_CHANGED));
5113 udelay(40);
5114
5115 if (force_reset)
5116 tg3_phy_reset(tp);
5117
5118 current_link_up = 0;
5119 current_speed = SPEED_INVALID;
5120 current_duplex = DUPLEX_INVALID;
Matt Carlson859edb22011-12-08 14:40:16 +00005121 tp->link_config.rmt_adv = 0;
Michael Chan747e8f82005-07-25 12:33:22 -07005122
5123 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
5124 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
Michael Chand4d2c552006-03-20 17:47:20 -08005125 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
5126 if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
5127 bmsr |= BMSR_LSTATUS;
5128 else
5129 bmsr &= ~BMSR_LSTATUS;
5130 }
Michael Chan747e8f82005-07-25 12:33:22 -07005131
5132 err |= tg3_readphy(tp, MII_BMCR, &bmcr);
5133
5134 if ((tp->link_config.autoneg == AUTONEG_ENABLE) && !force_reset &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005135 (tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT)) {
Michael Chan747e8f82005-07-25 12:33:22 -07005136 /* do nothing, just check for link up at the end */
5137 } else if (tp->link_config.autoneg == AUTONEG_ENABLE) {
Matt Carlson28011cf2011-11-16 18:36:59 -05005138 u32 adv, newadv;
Michael Chan747e8f82005-07-25 12:33:22 -07005139
5140 err |= tg3_readphy(tp, MII_ADVERTISE, &adv);
Matt Carlson28011cf2011-11-16 18:36:59 -05005141 newadv = adv & ~(ADVERTISE_1000XFULL | ADVERTISE_1000XHALF |
5142 ADVERTISE_1000XPAUSE |
5143 ADVERTISE_1000XPSE_ASYM |
5144 ADVERTISE_SLCT);
Michael Chan747e8f82005-07-25 12:33:22 -07005145
Matt Carlson28011cf2011-11-16 18:36:59 -05005146 newadv |= tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
Matt Carlson37f07022011-11-17 14:30:55 +00005147 newadv |= ethtool_adv_to_mii_adv_x(tp->link_config.advertising);
Michael Chan747e8f82005-07-25 12:33:22 -07005148
Matt Carlson28011cf2011-11-16 18:36:59 -05005149 if ((newadv != adv) || !(bmcr & BMCR_ANENABLE)) {
5150 tg3_writephy(tp, MII_ADVERTISE, newadv);
Michael Chan747e8f82005-07-25 12:33:22 -07005151 bmcr |= BMCR_ANENABLE | BMCR_ANRESTART;
5152 tg3_writephy(tp, MII_BMCR, bmcr);
5153
5154 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
Michael Chan3d3ebe72006-09-27 15:59:15 -07005155 tp->serdes_counter = SERDES_AN_TIMEOUT_5714S;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005156 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07005157
5158 return err;
5159 }
5160 } else {
5161 u32 new_bmcr;
5162
5163 bmcr &= ~BMCR_SPEED1000;
5164 new_bmcr = bmcr & ~(BMCR_ANENABLE | BMCR_FULLDPLX);
5165
5166 if (tp->link_config.duplex == DUPLEX_FULL)
5167 new_bmcr |= BMCR_FULLDPLX;
5168
5169 if (new_bmcr != bmcr) {
5170 /* BMCR_SPEED1000 is a reserved bit that needs
5171 * to be set on write.
5172 */
5173 new_bmcr |= BMCR_SPEED1000;
5174
5175 /* Force a linkdown */
5176 if (netif_carrier_ok(tp->dev)) {
5177 u32 adv;
5178
5179 err |= tg3_readphy(tp, MII_ADVERTISE, &adv);
5180 adv &= ~(ADVERTISE_1000XFULL |
5181 ADVERTISE_1000XHALF |
5182 ADVERTISE_SLCT);
5183 tg3_writephy(tp, MII_ADVERTISE, adv);
5184 tg3_writephy(tp, MII_BMCR, bmcr |
5185 BMCR_ANRESTART |
5186 BMCR_ANENABLE);
5187 udelay(10);
5188 netif_carrier_off(tp->dev);
5189 }
5190 tg3_writephy(tp, MII_BMCR, new_bmcr);
5191 bmcr = new_bmcr;
5192 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
5193 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
Michael Chand4d2c552006-03-20 17:47:20 -08005194 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
5195 ASIC_REV_5714) {
5196 if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
5197 bmsr |= BMSR_LSTATUS;
5198 else
5199 bmsr &= ~BMSR_LSTATUS;
5200 }
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005201 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07005202 }
5203 }
5204
5205 if (bmsr & BMSR_LSTATUS) {
5206 current_speed = SPEED_1000;
5207 current_link_up = 1;
5208 if (bmcr & BMCR_FULLDPLX)
5209 current_duplex = DUPLEX_FULL;
5210 else
5211 current_duplex = DUPLEX_HALF;
5212
Matt Carlsonef167e22007-12-20 20:10:01 -08005213 local_adv = 0;
5214 remote_adv = 0;
5215
Michael Chan747e8f82005-07-25 12:33:22 -07005216 if (bmcr & BMCR_ANENABLE) {
Matt Carlsonef167e22007-12-20 20:10:01 -08005217 u32 common;
Michael Chan747e8f82005-07-25 12:33:22 -07005218
5219 err |= tg3_readphy(tp, MII_ADVERTISE, &local_adv);
5220 err |= tg3_readphy(tp, MII_LPA, &remote_adv);
5221 common = local_adv & remote_adv;
5222 if (common & (ADVERTISE_1000XHALF |
5223 ADVERTISE_1000XFULL)) {
5224 if (common & ADVERTISE_1000XFULL)
5225 current_duplex = DUPLEX_FULL;
5226 else
5227 current_duplex = DUPLEX_HALF;
Matt Carlson859edb22011-12-08 14:40:16 +00005228
5229 tp->link_config.rmt_adv =
5230 mii_adv_to_ethtool_adv_x(remote_adv);
Joe Perches63c3a662011-04-26 08:12:10 +00005231 } else if (!tg3_flag(tp, 5780_CLASS)) {
Matt Carlson57d8b882010-06-05 17:24:35 +00005232 /* Link is up via parallel detect */
Matt Carlson859a588792010-04-05 10:19:28 +00005233 } else {
Michael Chan747e8f82005-07-25 12:33:22 -07005234 current_link_up = 0;
Matt Carlson859a588792010-04-05 10:19:28 +00005235 }
Michael Chan747e8f82005-07-25 12:33:22 -07005236 }
5237 }
5238
Matt Carlsonef167e22007-12-20 20:10:01 -08005239 if (current_link_up == 1 && current_duplex == DUPLEX_FULL)
5240 tg3_setup_flow_control(tp, local_adv, remote_adv);
5241
Michael Chan747e8f82005-07-25 12:33:22 -07005242 tp->mac_mode &= ~MAC_MODE_HALF_DUPLEX;
5243 if (tp->link_config.active_duplex == DUPLEX_HALF)
5244 tp->mac_mode |= MAC_MODE_HALF_DUPLEX;
5245
5246 tw32_f(MAC_MODE, tp->mac_mode);
5247 udelay(40);
5248
5249 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
5250
5251 tp->link_config.active_speed = current_speed;
5252 tp->link_config.active_duplex = current_duplex;
5253
5254 if (current_link_up != netif_carrier_ok(tp->dev)) {
5255 if (current_link_up)
5256 netif_carrier_on(tp->dev);
5257 else {
5258 netif_carrier_off(tp->dev);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005259 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07005260 }
5261 tg3_link_report(tp);
5262 }
5263 return err;
5264}
5265
5266static void tg3_serdes_parallel_detect(struct tg3 *tp)
5267{
Michael Chan3d3ebe72006-09-27 15:59:15 -07005268 if (tp->serdes_counter) {
Michael Chan747e8f82005-07-25 12:33:22 -07005269 /* Give autoneg time to complete. */
Michael Chan3d3ebe72006-09-27 15:59:15 -07005270 tp->serdes_counter--;
Michael Chan747e8f82005-07-25 12:33:22 -07005271 return;
5272 }
Matt Carlsonc6cdf432010-04-05 10:19:26 +00005273
Michael Chan747e8f82005-07-25 12:33:22 -07005274 if (!netif_carrier_ok(tp->dev) &&
5275 (tp->link_config.autoneg == AUTONEG_ENABLE)) {
5276 u32 bmcr;
5277
5278 tg3_readphy(tp, MII_BMCR, &bmcr);
5279 if (bmcr & BMCR_ANENABLE) {
5280 u32 phy1, phy2;
5281
5282 /* Select shadow register 0x1f */
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00005283 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x7c00);
5284 tg3_readphy(tp, MII_TG3_MISC_SHDW, &phy1);
Michael Chan747e8f82005-07-25 12:33:22 -07005285
5286 /* Select expansion interrupt status register */
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00005287 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
5288 MII_TG3_DSP_EXP1_INT_STAT);
5289 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &phy2);
5290 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &phy2);
Michael Chan747e8f82005-07-25 12:33:22 -07005291
5292 if ((phy1 & 0x10) && !(phy2 & 0x20)) {
5293 /* We have signal detect and not receiving
5294 * config code words, link is up by parallel
5295 * detection.
5296 */
5297
5298 bmcr &= ~BMCR_ANENABLE;
5299 bmcr |= BMCR_SPEED1000 | BMCR_FULLDPLX;
5300 tg3_writephy(tp, MII_BMCR, bmcr);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005301 tp->phy_flags |= TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07005302 }
5303 }
Matt Carlson859a588792010-04-05 10:19:28 +00005304 } else if (netif_carrier_ok(tp->dev) &&
5305 (tp->link_config.autoneg == AUTONEG_ENABLE) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005306 (tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT)) {
Michael Chan747e8f82005-07-25 12:33:22 -07005307 u32 phy2;
5308
5309 /* Select expansion interrupt status register */
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00005310 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
5311 MII_TG3_DSP_EXP1_INT_STAT);
5312 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &phy2);
Michael Chan747e8f82005-07-25 12:33:22 -07005313 if (phy2 & 0x20) {
5314 u32 bmcr;
5315
5316 /* Config code words received, turn on autoneg. */
5317 tg3_readphy(tp, MII_BMCR, &bmcr);
5318 tg3_writephy(tp, MII_BMCR, bmcr | BMCR_ANENABLE);
5319
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005320 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07005321
5322 }
5323 }
5324}
5325
Linus Torvalds1da177e2005-04-16 15:20:36 -07005326static int tg3_setup_phy(struct tg3 *tp, int force_reset)
5327{
Matt Carlsonf2096f92011-04-05 14:22:48 +00005328 u32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005329 int err;
5330
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005331 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005332 err = tg3_setup_fiber_phy(tp, force_reset);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005333 else if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
Michael Chan747e8f82005-07-25 12:33:22 -07005334 err = tg3_setup_fiber_mii_phy(tp, force_reset);
Matt Carlson859a588792010-04-05 10:19:28 +00005335 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07005336 err = tg3_setup_copper_phy(tp, force_reset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005337
Matt Carlsonbcb37f62008-11-03 16:52:09 -08005338 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX) {
Matt Carlsonf2096f92011-04-05 14:22:48 +00005339 u32 scale;
Matt Carlsonaa6c91f2007-11-12 21:18:04 -08005340
5341 val = tr32(TG3_CPMU_CLCK_STAT) & CPMU_CLCK_STAT_MAC_CLCK_MASK;
5342 if (val == CPMU_CLCK_STAT_MAC_CLCK_62_5)
5343 scale = 65;
5344 else if (val == CPMU_CLCK_STAT_MAC_CLCK_6_25)
5345 scale = 6;
5346 else
5347 scale = 12;
5348
5349 val = tr32(GRC_MISC_CFG) & ~GRC_MISC_CFG_PRESCALAR_MASK;
5350 val |= (scale << GRC_MISC_CFG_PRESCALAR_SHIFT);
5351 tw32(GRC_MISC_CFG, val);
5352 }
5353
Matt Carlsonf2096f92011-04-05 14:22:48 +00005354 val = (2 << TX_LENGTHS_IPG_CRS_SHIFT) |
5355 (6 << TX_LENGTHS_IPG_SHIFT);
5356 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
5357 val |= tr32(MAC_TX_LENGTHS) &
5358 (TX_LENGTHS_JMB_FRM_LEN_MSK |
5359 TX_LENGTHS_CNT_DWN_VAL_MSK);
5360
Linus Torvalds1da177e2005-04-16 15:20:36 -07005361 if (tp->link_config.active_speed == SPEED_1000 &&
5362 tp->link_config.active_duplex == DUPLEX_HALF)
Matt Carlsonf2096f92011-04-05 14:22:48 +00005363 tw32(MAC_TX_LENGTHS, val |
5364 (0xff << TX_LENGTHS_SLOT_TIME_SHIFT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005365 else
Matt Carlsonf2096f92011-04-05 14:22:48 +00005366 tw32(MAC_TX_LENGTHS, val |
5367 (32 << TX_LENGTHS_SLOT_TIME_SHIFT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005368
Joe Perches63c3a662011-04-26 08:12:10 +00005369 if (!tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005370 if (netif_carrier_ok(tp->dev)) {
5371 tw32(HOSTCC_STAT_COAL_TICKS,
David S. Miller15f98502005-05-18 22:49:26 -07005372 tp->coal.stats_block_coalesce_usecs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005373 } else {
5374 tw32(HOSTCC_STAT_COAL_TICKS, 0);
5375 }
5376 }
5377
Joe Perches63c3a662011-04-26 08:12:10 +00005378 if (tg3_flag(tp, ASPM_WORKAROUND)) {
Matt Carlsonf2096f92011-04-05 14:22:48 +00005379 val = tr32(PCIE_PWR_MGMT_THRESH);
Matt Carlson8ed5d972007-05-07 00:25:49 -07005380 if (!netif_carrier_ok(tp->dev))
5381 val = (val & ~PCIE_PWR_MGMT_L1_THRESH_MSK) |
5382 tp->pwrmgmt_thresh;
5383 else
5384 val |= PCIE_PWR_MGMT_L1_THRESH_MSK;
5385 tw32(PCIE_PWR_MGMT_THRESH, val);
5386 }
5387
Linus Torvalds1da177e2005-04-16 15:20:36 -07005388 return err;
5389}
5390
Matt Carlson66cfd1b2010-09-30 10:34:30 +00005391static inline int tg3_irq_sync(struct tg3 *tp)
5392{
5393 return tp->irq_sync;
5394}
5395
Matt Carlson97bd8e42011-04-13 11:05:04 +00005396static inline void tg3_rd32_loop(struct tg3 *tp, u32 *dst, u32 off, u32 len)
5397{
5398 int i;
5399
5400 dst = (u32 *)((u8 *)dst + off);
5401 for (i = 0; i < len; i += sizeof(u32))
5402 *dst++ = tr32(off + i);
5403}
5404
5405static void tg3_dump_legacy_regs(struct tg3 *tp, u32 *regs)
5406{
5407 tg3_rd32_loop(tp, regs, TG3PCI_VENDOR, 0xb0);
5408 tg3_rd32_loop(tp, regs, MAILBOX_INTERRUPT_0, 0x200);
5409 tg3_rd32_loop(tp, regs, MAC_MODE, 0x4f0);
5410 tg3_rd32_loop(tp, regs, SNDDATAI_MODE, 0xe0);
5411 tg3_rd32_loop(tp, regs, SNDDATAC_MODE, 0x04);
5412 tg3_rd32_loop(tp, regs, SNDBDS_MODE, 0x80);
5413 tg3_rd32_loop(tp, regs, SNDBDI_MODE, 0x48);
5414 tg3_rd32_loop(tp, regs, SNDBDC_MODE, 0x04);
5415 tg3_rd32_loop(tp, regs, RCVLPC_MODE, 0x20);
5416 tg3_rd32_loop(tp, regs, RCVLPC_SELLST_BASE, 0x15c);
5417 tg3_rd32_loop(tp, regs, RCVDBDI_MODE, 0x0c);
5418 tg3_rd32_loop(tp, regs, RCVDBDI_JUMBO_BD, 0x3c);
5419 tg3_rd32_loop(tp, regs, RCVDBDI_BD_PROD_IDX_0, 0x44);
5420 tg3_rd32_loop(tp, regs, RCVDCC_MODE, 0x04);
5421 tg3_rd32_loop(tp, regs, RCVBDI_MODE, 0x20);
5422 tg3_rd32_loop(tp, regs, RCVCC_MODE, 0x14);
5423 tg3_rd32_loop(tp, regs, RCVLSC_MODE, 0x08);
5424 tg3_rd32_loop(tp, regs, MBFREE_MODE, 0x08);
5425 tg3_rd32_loop(tp, regs, HOSTCC_MODE, 0x100);
5426
Joe Perches63c3a662011-04-26 08:12:10 +00005427 if (tg3_flag(tp, SUPPORT_MSIX))
Matt Carlson97bd8e42011-04-13 11:05:04 +00005428 tg3_rd32_loop(tp, regs, HOSTCC_RXCOL_TICKS_VEC1, 0x180);
5429
5430 tg3_rd32_loop(tp, regs, MEMARB_MODE, 0x10);
5431 tg3_rd32_loop(tp, regs, BUFMGR_MODE, 0x58);
5432 tg3_rd32_loop(tp, regs, RDMAC_MODE, 0x08);
5433 tg3_rd32_loop(tp, regs, WDMAC_MODE, 0x08);
5434 tg3_rd32_loop(tp, regs, RX_CPU_MODE, 0x04);
5435 tg3_rd32_loop(tp, regs, RX_CPU_STATE, 0x04);
5436 tg3_rd32_loop(tp, regs, RX_CPU_PGMCTR, 0x04);
5437 tg3_rd32_loop(tp, regs, RX_CPU_HWBKPT, 0x04);
5438
Joe Perches63c3a662011-04-26 08:12:10 +00005439 if (!tg3_flag(tp, 5705_PLUS)) {
Matt Carlson97bd8e42011-04-13 11:05:04 +00005440 tg3_rd32_loop(tp, regs, TX_CPU_MODE, 0x04);
5441 tg3_rd32_loop(tp, regs, TX_CPU_STATE, 0x04);
5442 tg3_rd32_loop(tp, regs, TX_CPU_PGMCTR, 0x04);
5443 }
5444
5445 tg3_rd32_loop(tp, regs, GRCMBOX_INTERRUPT_0, 0x110);
5446 tg3_rd32_loop(tp, regs, FTQ_RESET, 0x120);
5447 tg3_rd32_loop(tp, regs, MSGINT_MODE, 0x0c);
5448 tg3_rd32_loop(tp, regs, DMAC_MODE, 0x04);
5449 tg3_rd32_loop(tp, regs, GRC_MODE, 0x4c);
5450
Joe Perches63c3a662011-04-26 08:12:10 +00005451 if (tg3_flag(tp, NVRAM))
Matt Carlson97bd8e42011-04-13 11:05:04 +00005452 tg3_rd32_loop(tp, regs, NVRAM_CMD, 0x24);
5453}
5454
5455static void tg3_dump_state(struct tg3 *tp)
5456{
5457 int i;
5458 u32 *regs;
5459
5460 regs = kzalloc(TG3_REG_BLK_SIZE, GFP_ATOMIC);
5461 if (!regs) {
5462 netdev_err(tp->dev, "Failed allocating register dump buffer\n");
5463 return;
5464 }
5465
Joe Perches63c3a662011-04-26 08:12:10 +00005466 if (tg3_flag(tp, PCI_EXPRESS)) {
Matt Carlson97bd8e42011-04-13 11:05:04 +00005467 /* Read up to but not including private PCI registers */
5468 for (i = 0; i < TG3_PCIE_TLDLPL_PORT; i += sizeof(u32))
5469 regs[i / sizeof(u32)] = tr32(i);
5470 } else
5471 tg3_dump_legacy_regs(tp, regs);
5472
5473 for (i = 0; i < TG3_REG_BLK_SIZE / sizeof(u32); i += 4) {
5474 if (!regs[i + 0] && !regs[i + 1] &&
5475 !regs[i + 2] && !regs[i + 3])
5476 continue;
5477
5478 netdev_err(tp->dev, "0x%08x: 0x%08x, 0x%08x, 0x%08x, 0x%08x\n",
5479 i * 4,
5480 regs[i + 0], regs[i + 1], regs[i + 2], regs[i + 3]);
5481 }
5482
5483 kfree(regs);
5484
5485 for (i = 0; i < tp->irq_cnt; i++) {
5486 struct tg3_napi *tnapi = &tp->napi[i];
5487
5488 /* SW status block */
5489 netdev_err(tp->dev,
5490 "%d: Host status block [%08x:%08x:(%04x:%04x:%04x):(%04x:%04x)]\n",
5491 i,
5492 tnapi->hw_status->status,
5493 tnapi->hw_status->status_tag,
5494 tnapi->hw_status->rx_jumbo_consumer,
5495 tnapi->hw_status->rx_consumer,
5496 tnapi->hw_status->rx_mini_consumer,
5497 tnapi->hw_status->idx[0].rx_producer,
5498 tnapi->hw_status->idx[0].tx_consumer);
5499
5500 netdev_err(tp->dev,
5501 "%d: NAPI info [%08x:%08x:(%04x:%04x:%04x):%04x:(%04x:%04x:%04x:%04x)]\n",
5502 i,
5503 tnapi->last_tag, tnapi->last_irq_tag,
5504 tnapi->tx_prod, tnapi->tx_cons, tnapi->tx_pending,
5505 tnapi->rx_rcb_ptr,
5506 tnapi->prodring.rx_std_prod_idx,
5507 tnapi->prodring.rx_std_cons_idx,
5508 tnapi->prodring.rx_jmb_prod_idx,
5509 tnapi->prodring.rx_jmb_cons_idx);
5510 }
5511}
5512
Michael Chandf3e6542006-05-26 17:48:07 -07005513/* This is called whenever we suspect that the system chipset is re-
5514 * ordering the sequence of MMIO to the tx send mailbox. The symptom
5515 * is bogus tx completions. We try to recover by setting the
5516 * TG3_FLAG_MBOX_WRITE_REORDER flag and resetting the chip later
5517 * in the workqueue.
5518 */
5519static void tg3_tx_recover(struct tg3 *tp)
5520{
Joe Perches63c3a662011-04-26 08:12:10 +00005521 BUG_ON(tg3_flag(tp, MBOX_WRITE_REORDER) ||
Michael Chandf3e6542006-05-26 17:48:07 -07005522 tp->write32_tx_mbox == tg3_write_indirect_mbox);
5523
Matt Carlson5129c3a2010-04-05 10:19:23 +00005524 netdev_warn(tp->dev,
5525 "The system may be re-ordering memory-mapped I/O "
5526 "cycles to the network device, attempting to recover. "
5527 "Please report the problem to the driver maintainer "
5528 "and include system chipset information.\n");
Michael Chandf3e6542006-05-26 17:48:07 -07005529
5530 spin_lock(&tp->lock);
Joe Perches63c3a662011-04-26 08:12:10 +00005531 tg3_flag_set(tp, TX_RECOVERY_PENDING);
Michael Chandf3e6542006-05-26 17:48:07 -07005532 spin_unlock(&tp->lock);
5533}
5534
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005535static inline u32 tg3_tx_avail(struct tg3_napi *tnapi)
Michael Chan1b2a7202006-08-07 21:46:02 -07005536{
Matt Carlsonf65aac12010-08-02 11:26:03 +00005537 /* Tell compiler to fetch tx indices from memory. */
5538 barrier();
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005539 return tnapi->tx_pending -
5540 ((tnapi->tx_prod - tnapi->tx_cons) & (TG3_TX_RING_SIZE - 1));
Michael Chan1b2a7202006-08-07 21:46:02 -07005541}
5542
Linus Torvalds1da177e2005-04-16 15:20:36 -07005543/* Tigon3 never reports partial packet sends. So we do not
5544 * need special logic to handle SKBs that have not had all
5545 * of their frags sent yet, like SunGEM does.
5546 */
Matt Carlson17375d22009-08-28 14:02:18 +00005547static void tg3_tx(struct tg3_napi *tnapi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005548{
Matt Carlson17375d22009-08-28 14:02:18 +00005549 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00005550 u32 hw_idx = tnapi->hw_status->idx[0].tx_consumer;
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005551 u32 sw_idx = tnapi->tx_cons;
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005552 struct netdev_queue *txq;
5553 int index = tnapi - tp->napi;
Tom Herbert298376d2011-11-28 16:33:30 +00005554 unsigned int pkts_compl = 0, bytes_compl = 0;
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005555
Joe Perches63c3a662011-04-26 08:12:10 +00005556 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005557 index--;
5558
5559 txq = netdev_get_tx_queue(tp->dev, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005560
5561 while (sw_idx != hw_idx) {
Matt Carlsondf8944c2011-07-27 14:20:46 +00005562 struct tg3_tx_ring_info *ri = &tnapi->tx_buffers[sw_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07005563 struct sk_buff *skb = ri->skb;
Michael Chandf3e6542006-05-26 17:48:07 -07005564 int i, tx_bug = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005565
Michael Chandf3e6542006-05-26 17:48:07 -07005566 if (unlikely(skb == NULL)) {
5567 tg3_tx_recover(tp);
5568 return;
5569 }
5570
Alexander Duyckf4188d82009-12-02 16:48:38 +00005571 pci_unmap_single(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005572 dma_unmap_addr(ri, mapping),
Alexander Duyckf4188d82009-12-02 16:48:38 +00005573 skb_headlen(skb),
5574 PCI_DMA_TODEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005575
5576 ri->skb = NULL;
5577
Matt Carlsone01ee142011-07-27 14:20:50 +00005578 while (ri->fragmented) {
5579 ri->fragmented = false;
5580 sw_idx = NEXT_TX(sw_idx);
5581 ri = &tnapi->tx_buffers[sw_idx];
5582 }
5583
Linus Torvalds1da177e2005-04-16 15:20:36 -07005584 sw_idx = NEXT_TX(sw_idx);
5585
5586 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005587 ri = &tnapi->tx_buffers[sw_idx];
Michael Chandf3e6542006-05-26 17:48:07 -07005588 if (unlikely(ri->skb != NULL || sw_idx == hw_idx))
5589 tx_bug = 1;
Alexander Duyckf4188d82009-12-02 16:48:38 +00005590
5591 pci_unmap_page(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005592 dma_unmap_addr(ri, mapping),
Eric Dumazet9e903e02011-10-18 21:00:24 +00005593 skb_frag_size(&skb_shinfo(skb)->frags[i]),
Alexander Duyckf4188d82009-12-02 16:48:38 +00005594 PCI_DMA_TODEVICE);
Matt Carlsone01ee142011-07-27 14:20:50 +00005595
5596 while (ri->fragmented) {
5597 ri->fragmented = false;
5598 sw_idx = NEXT_TX(sw_idx);
5599 ri = &tnapi->tx_buffers[sw_idx];
5600 }
5601
Linus Torvalds1da177e2005-04-16 15:20:36 -07005602 sw_idx = NEXT_TX(sw_idx);
5603 }
5604
Tom Herbert298376d2011-11-28 16:33:30 +00005605 pkts_compl++;
5606 bytes_compl += skb->len;
5607
David S. Millerf47c11e2005-06-24 20:18:35 -07005608 dev_kfree_skb(skb);
Michael Chandf3e6542006-05-26 17:48:07 -07005609
5610 if (unlikely(tx_bug)) {
5611 tg3_tx_recover(tp);
5612 return;
5613 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005614 }
5615
Tom Herbert298376d2011-11-28 16:33:30 +00005616 netdev_completed_queue(tp->dev, pkts_compl, bytes_compl);
5617
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005618 tnapi->tx_cons = sw_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005619
Michael Chan1b2a7202006-08-07 21:46:02 -07005620 /* Need to make the tx_cons update visible to tg3_start_xmit()
5621 * before checking for netif_queue_stopped(). Without the
5622 * memory barrier, there is a small possibility that tg3_start_xmit()
5623 * will miss it and cause the queue to be stopped forever.
5624 */
5625 smp_mb();
5626
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005627 if (unlikely(netif_tx_queue_stopped(txq) &&
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005628 (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi)))) {
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005629 __netif_tx_lock(txq, smp_processor_id());
5630 if (netif_tx_queue_stopped(txq) &&
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005631 (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi)))
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005632 netif_tx_wake_queue(txq);
5633 __netif_tx_unlock(txq);
Michael Chan51b91462005-09-01 17:41:28 -07005634 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005635}
5636
Eric Dumazet9205fd92011-11-18 06:47:01 +00005637static void tg3_rx_data_free(struct tg3 *tp, struct ring_info *ri, u32 map_sz)
Matt Carlson2b2cdb62009-11-13 13:03:48 +00005638{
Eric Dumazet9205fd92011-11-18 06:47:01 +00005639 if (!ri->data)
Matt Carlson2b2cdb62009-11-13 13:03:48 +00005640 return;
5641
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005642 pci_unmap_single(tp->pdev, dma_unmap_addr(ri, mapping),
Matt Carlson2b2cdb62009-11-13 13:03:48 +00005643 map_sz, PCI_DMA_FROMDEVICE);
Eric Dumazet9205fd92011-11-18 06:47:01 +00005644 kfree(ri->data);
5645 ri->data = NULL;
Matt Carlson2b2cdb62009-11-13 13:03:48 +00005646}
5647
Linus Torvalds1da177e2005-04-16 15:20:36 -07005648/* Returns size of skb allocated or < 0 on error.
5649 *
5650 * We only need to fill in the address because the other members
5651 * of the RX descriptor are invariant, see tg3_init_rings.
5652 *
5653 * Note the purposeful assymetry of cpu vs. chip accesses. For
5654 * posting buffers we only dirty the first cache line of the RX
5655 * descriptor (containing the address). Whereas for the RX status
5656 * buffers the cpu only reads the last cacheline of the RX descriptor
5657 * (to fetch the error flags, vlan tag, checksum, and opaque cookie).
5658 */
Eric Dumazet9205fd92011-11-18 06:47:01 +00005659static int tg3_alloc_rx_data(struct tg3 *tp, struct tg3_rx_prodring_set *tpr,
Matt Carlsona3896162009-11-13 13:03:44 +00005660 u32 opaque_key, u32 dest_idx_unmasked)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005661{
5662 struct tg3_rx_buffer_desc *desc;
Matt Carlsonf94e2902010-10-14 10:37:42 +00005663 struct ring_info *map;
Eric Dumazet9205fd92011-11-18 06:47:01 +00005664 u8 *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005665 dma_addr_t mapping;
Eric Dumazet9205fd92011-11-18 06:47:01 +00005666 int skb_size, data_size, dest_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005667
Linus Torvalds1da177e2005-04-16 15:20:36 -07005668 switch (opaque_key) {
5669 case RXD_OPAQUE_RING_STD:
Matt Carlson2c49a442010-09-30 10:34:35 +00005670 dest_idx = dest_idx_unmasked & tp->rx_std_ring_mask;
Matt Carlson21f581a2009-08-28 14:00:25 +00005671 desc = &tpr->rx_std[dest_idx];
5672 map = &tpr->rx_std_buffers[dest_idx];
Eric Dumazet9205fd92011-11-18 06:47:01 +00005673 data_size = tp->rx_pkt_map_sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005674 break;
5675
5676 case RXD_OPAQUE_RING_JUMBO:
Matt Carlson2c49a442010-09-30 10:34:35 +00005677 dest_idx = dest_idx_unmasked & tp->rx_jmb_ring_mask;
Matt Carlson79ed5ac2009-08-28 14:00:55 +00005678 desc = &tpr->rx_jmb[dest_idx].std;
Matt Carlson21f581a2009-08-28 14:00:25 +00005679 map = &tpr->rx_jmb_buffers[dest_idx];
Eric Dumazet9205fd92011-11-18 06:47:01 +00005680 data_size = TG3_RX_JMB_MAP_SZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005681 break;
5682
5683 default:
5684 return -EINVAL;
Stephen Hemminger855e1112008-04-16 16:37:28 -07005685 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005686
5687 /* Do not overwrite any of the map or rp information
5688 * until we are sure we can commit to a new buffer.
5689 *
5690 * Callers depend upon this behavior and assume that
5691 * we leave everything unchanged if we fail.
5692 */
Eric Dumazet9205fd92011-11-18 06:47:01 +00005693 skb_size = SKB_DATA_ALIGN(data_size + TG3_RX_OFFSET(tp)) +
5694 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
5695 data = kmalloc(skb_size, GFP_ATOMIC);
5696 if (!data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005697 return -ENOMEM;
5698
Eric Dumazet9205fd92011-11-18 06:47:01 +00005699 mapping = pci_map_single(tp->pdev,
5700 data + TG3_RX_OFFSET(tp),
5701 data_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005702 PCI_DMA_FROMDEVICE);
Matt Carlsona21771d2009-11-02 14:25:31 +00005703 if (pci_dma_mapping_error(tp->pdev, mapping)) {
Eric Dumazet9205fd92011-11-18 06:47:01 +00005704 kfree(data);
Matt Carlsona21771d2009-11-02 14:25:31 +00005705 return -EIO;
5706 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005707
Eric Dumazet9205fd92011-11-18 06:47:01 +00005708 map->data = data;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005709 dma_unmap_addr_set(map, mapping, mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005710
Linus Torvalds1da177e2005-04-16 15:20:36 -07005711 desc->addr_hi = ((u64)mapping >> 32);
5712 desc->addr_lo = ((u64)mapping & 0xffffffff);
5713
Eric Dumazet9205fd92011-11-18 06:47:01 +00005714 return data_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005715}
5716
5717/* We only need to move over in the address because the other
5718 * members of the RX descriptor are invariant. See notes above
Eric Dumazet9205fd92011-11-18 06:47:01 +00005719 * tg3_alloc_rx_data for full details.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005720 */
Matt Carlsona3896162009-11-13 13:03:44 +00005721static void tg3_recycle_rx(struct tg3_napi *tnapi,
5722 struct tg3_rx_prodring_set *dpr,
5723 u32 opaque_key, int src_idx,
5724 u32 dest_idx_unmasked)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005725{
Matt Carlson17375d22009-08-28 14:02:18 +00005726 struct tg3 *tp = tnapi->tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005727 struct tg3_rx_buffer_desc *src_desc, *dest_desc;
5728 struct ring_info *src_map, *dest_map;
Matt Carlson8fea32b2010-09-15 08:59:58 +00005729 struct tg3_rx_prodring_set *spr = &tp->napi[0].prodring;
Matt Carlsonc6cdf432010-04-05 10:19:26 +00005730 int dest_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005731
5732 switch (opaque_key) {
5733 case RXD_OPAQUE_RING_STD:
Matt Carlson2c49a442010-09-30 10:34:35 +00005734 dest_idx = dest_idx_unmasked & tp->rx_std_ring_mask;
Matt Carlsona3896162009-11-13 13:03:44 +00005735 dest_desc = &dpr->rx_std[dest_idx];
5736 dest_map = &dpr->rx_std_buffers[dest_idx];
5737 src_desc = &spr->rx_std[src_idx];
5738 src_map = &spr->rx_std_buffers[src_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07005739 break;
5740
5741 case RXD_OPAQUE_RING_JUMBO:
Matt Carlson2c49a442010-09-30 10:34:35 +00005742 dest_idx = dest_idx_unmasked & tp->rx_jmb_ring_mask;
Matt Carlsona3896162009-11-13 13:03:44 +00005743 dest_desc = &dpr->rx_jmb[dest_idx].std;
5744 dest_map = &dpr->rx_jmb_buffers[dest_idx];
5745 src_desc = &spr->rx_jmb[src_idx].std;
5746 src_map = &spr->rx_jmb_buffers[src_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07005747 break;
5748
5749 default:
5750 return;
Stephen Hemminger855e1112008-04-16 16:37:28 -07005751 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005752
Eric Dumazet9205fd92011-11-18 06:47:01 +00005753 dest_map->data = src_map->data;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005754 dma_unmap_addr_set(dest_map, mapping,
5755 dma_unmap_addr(src_map, mapping));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005756 dest_desc->addr_hi = src_desc->addr_hi;
5757 dest_desc->addr_lo = src_desc->addr_lo;
Matt Carlsone92967b2010-02-12 14:47:06 +00005758
5759 /* Ensure that the update to the skb happens after the physical
5760 * addresses have been transferred to the new BD location.
5761 */
5762 smp_wmb();
5763
Eric Dumazet9205fd92011-11-18 06:47:01 +00005764 src_map->data = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005765}
5766
Linus Torvalds1da177e2005-04-16 15:20:36 -07005767/* The RX ring scheme is composed of multiple rings which post fresh
5768 * buffers to the chip, and one special ring the chip uses to report
5769 * status back to the host.
5770 *
5771 * The special ring reports the status of received packets to the
5772 * host. The chip does not write into the original descriptor the
5773 * RX buffer was obtained from. The chip simply takes the original
5774 * descriptor as provided by the host, updates the status and length
5775 * field, then writes this into the next status ring entry.
5776 *
5777 * Each ring the host uses to post buffers to the chip is described
5778 * by a TG3_BDINFO entry in the chips SRAM area. When a packet arrives,
5779 * it is first placed into the on-chip ram. When the packet's length
5780 * is known, it walks down the TG3_BDINFO entries to select the ring.
5781 * Each TG3_BDINFO specifies a MAXLEN field and the first TG3_BDINFO
5782 * which is within the range of the new packet's length is chosen.
5783 *
5784 * The "separate ring for rx status" scheme may sound queer, but it makes
5785 * sense from a cache coherency perspective. If only the host writes
5786 * to the buffer post rings, and only the chip writes to the rx status
5787 * rings, then cache lines never move beyond shared-modified state.
5788 * If both the host and chip were to write into the same ring, cache line
5789 * eviction could occur since both entities want it in an exclusive state.
5790 */
Matt Carlson17375d22009-08-28 14:02:18 +00005791static int tg3_rx(struct tg3_napi *tnapi, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005792{
Matt Carlson17375d22009-08-28 14:02:18 +00005793 struct tg3 *tp = tnapi->tp;
Michael Chanf92905d2006-06-29 20:14:29 -07005794 u32 work_mask, rx_std_posted = 0;
Matt Carlson43619352009-11-13 13:03:47 +00005795 u32 std_prod_idx, jmb_prod_idx;
Matt Carlson72334482009-08-28 14:03:01 +00005796 u32 sw_idx = tnapi->rx_rcb_ptr;
Michael Chan483ba502005-04-25 15:14:03 -07005797 u16 hw_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005798 int received;
Matt Carlson8fea32b2010-09-15 08:59:58 +00005799 struct tg3_rx_prodring_set *tpr = &tnapi->prodring;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005800
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00005801 hw_idx = *(tnapi->rx_rcb_prod_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005802 /*
5803 * We need to order the read of hw_idx and the read of
5804 * the opaque cookie.
5805 */
5806 rmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005807 work_mask = 0;
5808 received = 0;
Matt Carlson43619352009-11-13 13:03:47 +00005809 std_prod_idx = tpr->rx_std_prod_idx;
5810 jmb_prod_idx = tpr->rx_jmb_prod_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005811 while (sw_idx != hw_idx && budget > 0) {
Matt Carlsonafc081f2009-11-13 13:03:43 +00005812 struct ring_info *ri;
Matt Carlson72334482009-08-28 14:03:01 +00005813 struct tg3_rx_buffer_desc *desc = &tnapi->rx_rcb[sw_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07005814 unsigned int len;
5815 struct sk_buff *skb;
5816 dma_addr_t dma_addr;
5817 u32 opaque_key, desc_idx, *post_ptr;
Eric Dumazet9205fd92011-11-18 06:47:01 +00005818 u8 *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005819
5820 desc_idx = desc->opaque & RXD_OPAQUE_INDEX_MASK;
5821 opaque_key = desc->opaque & RXD_OPAQUE_RING_MASK;
5822 if (opaque_key == RXD_OPAQUE_RING_STD) {
Matt Carlson8fea32b2010-09-15 08:59:58 +00005823 ri = &tp->napi[0].prodring.rx_std_buffers[desc_idx];
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005824 dma_addr = dma_unmap_addr(ri, mapping);
Eric Dumazet9205fd92011-11-18 06:47:01 +00005825 data = ri->data;
Matt Carlson43619352009-11-13 13:03:47 +00005826 post_ptr = &std_prod_idx;
Michael Chanf92905d2006-06-29 20:14:29 -07005827 rx_std_posted++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005828 } else if (opaque_key == RXD_OPAQUE_RING_JUMBO) {
Matt Carlson8fea32b2010-09-15 08:59:58 +00005829 ri = &tp->napi[0].prodring.rx_jmb_buffers[desc_idx];
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005830 dma_addr = dma_unmap_addr(ri, mapping);
Eric Dumazet9205fd92011-11-18 06:47:01 +00005831 data = ri->data;
Matt Carlson43619352009-11-13 13:03:47 +00005832 post_ptr = &jmb_prod_idx;
Matt Carlson21f581a2009-08-28 14:00:25 +00005833 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07005834 goto next_pkt_nopost;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005835
5836 work_mask |= opaque_key;
5837
5838 if ((desc->err_vlan & RXD_ERR_MASK) != 0 &&
5839 (desc->err_vlan != RXD_ERR_ODD_NIBBLE_RCVD_MII)) {
5840 drop_it:
Matt Carlsona3896162009-11-13 13:03:44 +00005841 tg3_recycle_rx(tnapi, tpr, opaque_key,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005842 desc_idx, *post_ptr);
5843 drop_it_no_recycle:
5844 /* Other statistics kept track of by card. */
Eric Dumazetb0057c52010-10-10 19:55:52 +00005845 tp->rx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005846 goto next_pkt;
5847 }
5848
Eric Dumazet9205fd92011-11-18 06:47:01 +00005849 prefetch(data + TG3_RX_OFFSET(tp));
Matt Carlsonad829262008-11-21 17:16:16 -08005850 len = ((desc->idx_len & RXD_LEN_MASK) >> RXD_LEN_SHIFT) -
5851 ETH_FCS_LEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005852
Matt Carlsond2757fc2010-04-12 06:58:27 +00005853 if (len > TG3_RX_COPY_THRESH(tp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005854 int skb_size;
5855
Eric Dumazet9205fd92011-11-18 06:47:01 +00005856 skb_size = tg3_alloc_rx_data(tp, tpr, opaque_key,
Matt Carlsonafc081f2009-11-13 13:03:43 +00005857 *post_ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005858 if (skb_size < 0)
5859 goto drop_it;
5860
Matt Carlson287be122009-08-28 13:58:46 +00005861 pci_unmap_single(tp->pdev, dma_addr, skb_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005862 PCI_DMA_FROMDEVICE);
5863
Eric Dumazet9205fd92011-11-18 06:47:01 +00005864 skb = build_skb(data);
5865 if (!skb) {
5866 kfree(data);
5867 goto drop_it_no_recycle;
5868 }
5869 skb_reserve(skb, TG3_RX_OFFSET(tp));
5870 /* Ensure that the update to the data happens
Matt Carlson61e800c2010-02-17 15:16:54 +00005871 * after the usage of the old DMA mapping.
5872 */
5873 smp_wmb();
5874
Eric Dumazet9205fd92011-11-18 06:47:01 +00005875 ri->data = NULL;
Matt Carlson61e800c2010-02-17 15:16:54 +00005876
Linus Torvalds1da177e2005-04-16 15:20:36 -07005877 } else {
Matt Carlsona3896162009-11-13 13:03:44 +00005878 tg3_recycle_rx(tnapi, tpr, opaque_key,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005879 desc_idx, *post_ptr);
5880
Eric Dumazet9205fd92011-11-18 06:47:01 +00005881 skb = netdev_alloc_skb(tp->dev,
5882 len + TG3_RAW_IP_ALIGN);
5883 if (skb == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005884 goto drop_it_no_recycle;
5885
Eric Dumazet9205fd92011-11-18 06:47:01 +00005886 skb_reserve(skb, TG3_RAW_IP_ALIGN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005887 pci_dma_sync_single_for_cpu(tp->pdev, dma_addr, len, PCI_DMA_FROMDEVICE);
Eric Dumazet9205fd92011-11-18 06:47:01 +00005888 memcpy(skb->data,
5889 data + TG3_RX_OFFSET(tp),
5890 len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005891 pci_dma_sync_single_for_device(tp->pdev, dma_addr, len, PCI_DMA_FROMDEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005892 }
5893
Eric Dumazet9205fd92011-11-18 06:47:01 +00005894 skb_put(skb, len);
Michał Mirosławdc668912011-04-07 03:35:07 +00005895 if ((tp->dev->features & NETIF_F_RXCSUM) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005896 (desc->type_flags & RXD_FLAG_TCPUDP_CSUM) &&
5897 (((desc->ip_tcp_csum & RXD_TCPCSUM_MASK)
5898 >> RXD_TCPCSUM_SHIFT) == 0xffff))
5899 skb->ip_summed = CHECKSUM_UNNECESSARY;
5900 else
Eric Dumazetbc8acf22010-09-02 13:07:41 -07005901 skb_checksum_none_assert(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005902
5903 skb->protocol = eth_type_trans(skb, tp->dev);
Matt Carlsonf7b493e2009-02-25 14:21:52 +00005904
5905 if (len > (tp->dev->mtu + ETH_HLEN) &&
5906 skb->protocol != htons(ETH_P_8021Q)) {
5907 dev_kfree_skb(skb);
Eric Dumazetb0057c52010-10-10 19:55:52 +00005908 goto drop_it_no_recycle;
Matt Carlsonf7b493e2009-02-25 14:21:52 +00005909 }
5910
Matt Carlson9dc7a112010-04-12 06:58:28 +00005911 if (desc->type_flags & RXD_FLAG_VLAN &&
Matt Carlsonbf933c82011-01-25 15:58:49 +00005912 !(tp->rx_mode & RX_MODE_KEEP_VLAN_TAG))
5913 __vlan_hwaccel_put_tag(skb,
5914 desc->err_vlan & RXD_VLAN_MASK);
Matt Carlson9dc7a112010-04-12 06:58:28 +00005915
Matt Carlsonbf933c82011-01-25 15:58:49 +00005916 napi_gro_receive(&tnapi->napi, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005917
Linus Torvalds1da177e2005-04-16 15:20:36 -07005918 received++;
5919 budget--;
5920
5921next_pkt:
5922 (*post_ptr)++;
Michael Chanf92905d2006-06-29 20:14:29 -07005923
5924 if (unlikely(rx_std_posted >= tp->rx_std_max_post)) {
Matt Carlson2c49a442010-09-30 10:34:35 +00005925 tpr->rx_std_prod_idx = std_prod_idx &
5926 tp->rx_std_ring_mask;
Matt Carlson86cfe4f2010-01-12 10:11:37 +00005927 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
5928 tpr->rx_std_prod_idx);
Michael Chanf92905d2006-06-29 20:14:29 -07005929 work_mask &= ~RXD_OPAQUE_RING_STD;
5930 rx_std_posted = 0;
5931 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005932next_pkt_nopost:
Michael Chan483ba502005-04-25 15:14:03 -07005933 sw_idx++;
Matt Carlson7cb32cf2010-09-30 10:34:36 +00005934 sw_idx &= tp->rx_ret_ring_mask;
Michael Chan52f6d692005-04-25 15:14:32 -07005935
5936 /* Refresh hw_idx to see if there is new work */
5937 if (sw_idx == hw_idx) {
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00005938 hw_idx = *(tnapi->rx_rcb_prod_idx);
Michael Chan52f6d692005-04-25 15:14:32 -07005939 rmb();
5940 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005941 }
5942
5943 /* ACK the status ring. */
Matt Carlson72334482009-08-28 14:03:01 +00005944 tnapi->rx_rcb_ptr = sw_idx;
5945 tw32_rx_mbox(tnapi->consmbox, sw_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005946
5947 /* Refill RX ring(s). */
Joe Perches63c3a662011-04-26 08:12:10 +00005948 if (!tg3_flag(tp, ENABLE_RSS)) {
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005949 if (work_mask & RXD_OPAQUE_RING_STD) {
Matt Carlson2c49a442010-09-30 10:34:35 +00005950 tpr->rx_std_prod_idx = std_prod_idx &
5951 tp->rx_std_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005952 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
5953 tpr->rx_std_prod_idx);
5954 }
5955 if (work_mask & RXD_OPAQUE_RING_JUMBO) {
Matt Carlson2c49a442010-09-30 10:34:35 +00005956 tpr->rx_jmb_prod_idx = jmb_prod_idx &
5957 tp->rx_jmb_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005958 tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG,
5959 tpr->rx_jmb_prod_idx);
5960 }
5961 mmiowb();
5962 } else if (work_mask) {
5963 /* rx_std_buffers[] and rx_jmb_buffers[] entries must be
5964 * updated before the producer indices can be updated.
5965 */
5966 smp_wmb();
5967
Matt Carlson2c49a442010-09-30 10:34:35 +00005968 tpr->rx_std_prod_idx = std_prod_idx & tp->rx_std_ring_mask;
5969 tpr->rx_jmb_prod_idx = jmb_prod_idx & tp->rx_jmb_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005970
Matt Carlsone4af1af2010-02-12 14:47:05 +00005971 if (tnapi != &tp->napi[1])
5972 napi_schedule(&tp->napi[1].napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005973 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005974
5975 return received;
5976}
5977
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005978static void tg3_poll_link(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005979{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005980 /* handle link change and other phy events */
Joe Perches63c3a662011-04-26 08:12:10 +00005981 if (!(tg3_flag(tp, USE_LINKCHG_REG) || tg3_flag(tp, POLL_SERDES))) {
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005982 struct tg3_hw_status *sblk = tp->napi[0].hw_status;
5983
Linus Torvalds1da177e2005-04-16 15:20:36 -07005984 if (sblk->status & SD_STATUS_LINK_CHG) {
5985 sblk->status = SD_STATUS_UPDATED |
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005986 (sblk->status & ~SD_STATUS_LINK_CHG);
David S. Millerf47c11e2005-06-24 20:18:35 -07005987 spin_lock(&tp->lock);
Joe Perches63c3a662011-04-26 08:12:10 +00005988 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlsondd477002008-05-25 23:45:58 -07005989 tw32_f(MAC_STATUS,
5990 (MAC_STATUS_SYNC_CHANGED |
5991 MAC_STATUS_CFG_CHANGED |
5992 MAC_STATUS_MI_COMPLETION |
5993 MAC_STATUS_LNKSTATE_CHANGED));
5994 udelay(40);
5995 } else
5996 tg3_setup_phy(tp, 0);
David S. Millerf47c11e2005-06-24 20:18:35 -07005997 spin_unlock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005998 }
5999 }
Matt Carlson35f2d7d2009-11-13 13:03:41 +00006000}
6001
Matt Carlsonf89f38b2010-02-12 14:47:07 +00006002static int tg3_rx_prodring_xfer(struct tg3 *tp,
6003 struct tg3_rx_prodring_set *dpr,
6004 struct tg3_rx_prodring_set *spr)
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006005{
6006 u32 si, di, cpycnt, src_prod_idx;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00006007 int i, err = 0;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006008
6009 while (1) {
6010 src_prod_idx = spr->rx_std_prod_idx;
6011
6012 /* Make sure updates to the rx_std_buffers[] entries and the
6013 * standard producer index are seen in the correct order.
6014 */
6015 smp_rmb();
6016
6017 if (spr->rx_std_cons_idx == src_prod_idx)
6018 break;
6019
6020 if (spr->rx_std_cons_idx < src_prod_idx)
6021 cpycnt = src_prod_idx - spr->rx_std_cons_idx;
6022 else
Matt Carlson2c49a442010-09-30 10:34:35 +00006023 cpycnt = tp->rx_std_ring_mask + 1 -
6024 spr->rx_std_cons_idx;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006025
Matt Carlson2c49a442010-09-30 10:34:35 +00006026 cpycnt = min(cpycnt,
6027 tp->rx_std_ring_mask + 1 - dpr->rx_std_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006028
6029 si = spr->rx_std_cons_idx;
6030 di = dpr->rx_std_prod_idx;
6031
Matt Carlsone92967b2010-02-12 14:47:06 +00006032 for (i = di; i < di + cpycnt; i++) {
Eric Dumazet9205fd92011-11-18 06:47:01 +00006033 if (dpr->rx_std_buffers[i].data) {
Matt Carlsone92967b2010-02-12 14:47:06 +00006034 cpycnt = i - di;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00006035 err = -ENOSPC;
Matt Carlsone92967b2010-02-12 14:47:06 +00006036 break;
6037 }
6038 }
6039
6040 if (!cpycnt)
6041 break;
6042
6043 /* Ensure that updates to the rx_std_buffers ring and the
6044 * shadowed hardware producer ring from tg3_recycle_skb() are
6045 * ordered correctly WRT the skb check above.
6046 */
6047 smp_rmb();
6048
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006049 memcpy(&dpr->rx_std_buffers[di],
6050 &spr->rx_std_buffers[si],
6051 cpycnt * sizeof(struct ring_info));
6052
6053 for (i = 0; i < cpycnt; i++, di++, si++) {
6054 struct tg3_rx_buffer_desc *sbd, *dbd;
6055 sbd = &spr->rx_std[si];
6056 dbd = &dpr->rx_std[di];
6057 dbd->addr_hi = sbd->addr_hi;
6058 dbd->addr_lo = sbd->addr_lo;
6059 }
6060
Matt Carlson2c49a442010-09-30 10:34:35 +00006061 spr->rx_std_cons_idx = (spr->rx_std_cons_idx + cpycnt) &
6062 tp->rx_std_ring_mask;
6063 dpr->rx_std_prod_idx = (dpr->rx_std_prod_idx + cpycnt) &
6064 tp->rx_std_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006065 }
6066
6067 while (1) {
6068 src_prod_idx = spr->rx_jmb_prod_idx;
6069
6070 /* Make sure updates to the rx_jmb_buffers[] entries and
6071 * the jumbo producer index are seen in the correct order.
6072 */
6073 smp_rmb();
6074
6075 if (spr->rx_jmb_cons_idx == src_prod_idx)
6076 break;
6077
6078 if (spr->rx_jmb_cons_idx < src_prod_idx)
6079 cpycnt = src_prod_idx - spr->rx_jmb_cons_idx;
6080 else
Matt Carlson2c49a442010-09-30 10:34:35 +00006081 cpycnt = tp->rx_jmb_ring_mask + 1 -
6082 spr->rx_jmb_cons_idx;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006083
6084 cpycnt = min(cpycnt,
Matt Carlson2c49a442010-09-30 10:34:35 +00006085 tp->rx_jmb_ring_mask + 1 - dpr->rx_jmb_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006086
6087 si = spr->rx_jmb_cons_idx;
6088 di = dpr->rx_jmb_prod_idx;
6089
Matt Carlsone92967b2010-02-12 14:47:06 +00006090 for (i = di; i < di + cpycnt; i++) {
Eric Dumazet9205fd92011-11-18 06:47:01 +00006091 if (dpr->rx_jmb_buffers[i].data) {
Matt Carlsone92967b2010-02-12 14:47:06 +00006092 cpycnt = i - di;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00006093 err = -ENOSPC;
Matt Carlsone92967b2010-02-12 14:47:06 +00006094 break;
6095 }
6096 }
6097
6098 if (!cpycnt)
6099 break;
6100
6101 /* Ensure that updates to the rx_jmb_buffers ring and the
6102 * shadowed hardware producer ring from tg3_recycle_skb() are
6103 * ordered correctly WRT the skb check above.
6104 */
6105 smp_rmb();
6106
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006107 memcpy(&dpr->rx_jmb_buffers[di],
6108 &spr->rx_jmb_buffers[si],
6109 cpycnt * sizeof(struct ring_info));
6110
6111 for (i = 0; i < cpycnt; i++, di++, si++) {
6112 struct tg3_rx_buffer_desc *sbd, *dbd;
6113 sbd = &spr->rx_jmb[si].std;
6114 dbd = &dpr->rx_jmb[di].std;
6115 dbd->addr_hi = sbd->addr_hi;
6116 dbd->addr_lo = sbd->addr_lo;
6117 }
6118
Matt Carlson2c49a442010-09-30 10:34:35 +00006119 spr->rx_jmb_cons_idx = (spr->rx_jmb_cons_idx + cpycnt) &
6120 tp->rx_jmb_ring_mask;
6121 dpr->rx_jmb_prod_idx = (dpr->rx_jmb_prod_idx + cpycnt) &
6122 tp->rx_jmb_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006123 }
Matt Carlsonf89f38b2010-02-12 14:47:07 +00006124
6125 return err;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006126}
6127
Matt Carlson35f2d7d2009-11-13 13:03:41 +00006128static int tg3_poll_work(struct tg3_napi *tnapi, int work_done, int budget)
6129{
6130 struct tg3 *tp = tnapi->tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006131
6132 /* run TX completion thread */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006133 if (tnapi->hw_status->idx[0].tx_consumer != tnapi->tx_cons) {
Matt Carlson17375d22009-08-28 14:02:18 +00006134 tg3_tx(tnapi);
Joe Perches63c3a662011-04-26 08:12:10 +00006135 if (unlikely(tg3_flag(tp, TX_RECOVERY_PENDING)))
Michael Chan4fd7ab52007-10-12 01:39:50 -07006136 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006137 }
6138
Linus Torvalds1da177e2005-04-16 15:20:36 -07006139 /* run RX thread, within the bounds set by NAPI.
6140 * All RX "locking" is done by ensuring outside
Stephen Hemmingerbea33482007-10-03 16:41:36 -07006141 * code synchronizes with tg3->napi.poll()
Linus Torvalds1da177e2005-04-16 15:20:36 -07006142 */
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00006143 if (*(tnapi->rx_rcb_prod_idx) != tnapi->rx_rcb_ptr)
Matt Carlson17375d22009-08-28 14:02:18 +00006144 work_done += tg3_rx(tnapi, budget - work_done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006145
Joe Perches63c3a662011-04-26 08:12:10 +00006146 if (tg3_flag(tp, ENABLE_RSS) && tnapi == &tp->napi[1]) {
Matt Carlson8fea32b2010-09-15 08:59:58 +00006147 struct tg3_rx_prodring_set *dpr = &tp->napi[0].prodring;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00006148 int i, err = 0;
Matt Carlsone4af1af2010-02-12 14:47:05 +00006149 u32 std_prod_idx = dpr->rx_std_prod_idx;
6150 u32 jmb_prod_idx = dpr->rx_jmb_prod_idx;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006151
Matt Carlsone4af1af2010-02-12 14:47:05 +00006152 for (i = 1; i < tp->irq_cnt; i++)
Matt Carlsonf89f38b2010-02-12 14:47:07 +00006153 err |= tg3_rx_prodring_xfer(tp, dpr,
Matt Carlson8fea32b2010-09-15 08:59:58 +00006154 &tp->napi[i].prodring);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006155
6156 wmb();
6157
Matt Carlsone4af1af2010-02-12 14:47:05 +00006158 if (std_prod_idx != dpr->rx_std_prod_idx)
6159 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
6160 dpr->rx_std_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006161
Matt Carlsone4af1af2010-02-12 14:47:05 +00006162 if (jmb_prod_idx != dpr->rx_jmb_prod_idx)
6163 tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG,
6164 dpr->rx_jmb_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006165
6166 mmiowb();
Matt Carlsonf89f38b2010-02-12 14:47:07 +00006167
6168 if (err)
6169 tw32_f(HOSTCC_MODE, tp->coal_now);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006170 }
6171
David S. Miller6f535762007-10-11 18:08:29 -07006172 return work_done;
6173}
David S. Millerf7383c22005-05-18 22:50:53 -07006174
Matt Carlsondb219972011-11-04 09:15:03 +00006175static inline void tg3_reset_task_schedule(struct tg3 *tp)
6176{
6177 if (!test_and_set_bit(TG3_FLAG_RESET_TASK_PENDING, tp->tg3_flags))
6178 schedule_work(&tp->reset_task);
6179}
6180
6181static inline void tg3_reset_task_cancel(struct tg3 *tp)
6182{
6183 cancel_work_sync(&tp->reset_task);
6184 tg3_flag_clear(tp, RESET_TASK_PENDING);
6185}
6186
Matt Carlson35f2d7d2009-11-13 13:03:41 +00006187static int tg3_poll_msix(struct napi_struct *napi, int budget)
6188{
6189 struct tg3_napi *tnapi = container_of(napi, struct tg3_napi, napi);
6190 struct tg3 *tp = tnapi->tp;
6191 int work_done = 0;
6192 struct tg3_hw_status *sblk = tnapi->hw_status;
6193
6194 while (1) {
6195 work_done = tg3_poll_work(tnapi, work_done, budget);
6196
Joe Perches63c3a662011-04-26 08:12:10 +00006197 if (unlikely(tg3_flag(tp, TX_RECOVERY_PENDING)))
Matt Carlson35f2d7d2009-11-13 13:03:41 +00006198 goto tx_recovery;
6199
6200 if (unlikely(work_done >= budget))
6201 break;
6202
Matt Carlsonc6cdf432010-04-05 10:19:26 +00006203 /* tp->last_tag is used in tg3_int_reenable() below
Matt Carlson35f2d7d2009-11-13 13:03:41 +00006204 * to tell the hw how much work has been processed,
6205 * so we must read it before checking for more work.
6206 */
6207 tnapi->last_tag = sblk->status_tag;
6208 tnapi->last_irq_tag = tnapi->last_tag;
6209 rmb();
6210
6211 /* check for RX/TX work to do */
Matt Carlson6d40db72010-04-05 10:19:20 +00006212 if (likely(sblk->idx[0].tx_consumer == tnapi->tx_cons &&
6213 *(tnapi->rx_rcb_prod_idx) == tnapi->rx_rcb_ptr)) {
Matt Carlson35f2d7d2009-11-13 13:03:41 +00006214 napi_complete(napi);
6215 /* Reenable interrupts. */
6216 tw32_mailbox(tnapi->int_mbox, tnapi->last_tag << 24);
6217 mmiowb();
6218 break;
6219 }
6220 }
6221
6222 return work_done;
6223
6224tx_recovery:
6225 /* work_done is guaranteed to be less than budget. */
6226 napi_complete(napi);
Matt Carlsondb219972011-11-04 09:15:03 +00006227 tg3_reset_task_schedule(tp);
Matt Carlson35f2d7d2009-11-13 13:03:41 +00006228 return work_done;
6229}
6230
Matt Carlsone64de4e2011-04-13 11:05:05 +00006231static void tg3_process_error(struct tg3 *tp)
6232{
6233 u32 val;
6234 bool real_error = false;
6235
Joe Perches63c3a662011-04-26 08:12:10 +00006236 if (tg3_flag(tp, ERROR_PROCESSED))
Matt Carlsone64de4e2011-04-13 11:05:05 +00006237 return;
6238
6239 /* Check Flow Attention register */
6240 val = tr32(HOSTCC_FLOW_ATTN);
6241 if (val & ~HOSTCC_FLOW_ATTN_MBUF_LWM) {
6242 netdev_err(tp->dev, "FLOW Attention error. Resetting chip.\n");
6243 real_error = true;
6244 }
6245
6246 if (tr32(MSGINT_STATUS) & ~MSGINT_STATUS_MSI_REQ) {
6247 netdev_err(tp->dev, "MSI Status error. Resetting chip.\n");
6248 real_error = true;
6249 }
6250
6251 if (tr32(RDMAC_STATUS) || tr32(WDMAC_STATUS)) {
6252 netdev_err(tp->dev, "DMA Status error. Resetting chip.\n");
6253 real_error = true;
6254 }
6255
6256 if (!real_error)
6257 return;
6258
6259 tg3_dump_state(tp);
6260
Joe Perches63c3a662011-04-26 08:12:10 +00006261 tg3_flag_set(tp, ERROR_PROCESSED);
Matt Carlsondb219972011-11-04 09:15:03 +00006262 tg3_reset_task_schedule(tp);
Matt Carlsone64de4e2011-04-13 11:05:05 +00006263}
6264
David S. Miller6f535762007-10-11 18:08:29 -07006265static int tg3_poll(struct napi_struct *napi, int budget)
6266{
Matt Carlson8ef04422009-08-28 14:01:37 +00006267 struct tg3_napi *tnapi = container_of(napi, struct tg3_napi, napi);
6268 struct tg3 *tp = tnapi->tp;
David S. Miller6f535762007-10-11 18:08:29 -07006269 int work_done = 0;
Matt Carlson898a56f2009-08-28 14:02:40 +00006270 struct tg3_hw_status *sblk = tnapi->hw_status;
David S. Miller6f535762007-10-11 18:08:29 -07006271
6272 while (1) {
Matt Carlsone64de4e2011-04-13 11:05:05 +00006273 if (sblk->status & SD_STATUS_ERROR)
6274 tg3_process_error(tp);
6275
Matt Carlson35f2d7d2009-11-13 13:03:41 +00006276 tg3_poll_link(tp);
6277
Matt Carlson17375d22009-08-28 14:02:18 +00006278 work_done = tg3_poll_work(tnapi, work_done, budget);
David S. Miller6f535762007-10-11 18:08:29 -07006279
Joe Perches63c3a662011-04-26 08:12:10 +00006280 if (unlikely(tg3_flag(tp, TX_RECOVERY_PENDING)))
David S. Miller6f535762007-10-11 18:08:29 -07006281 goto tx_recovery;
6282
6283 if (unlikely(work_done >= budget))
6284 break;
6285
Joe Perches63c3a662011-04-26 08:12:10 +00006286 if (tg3_flag(tp, TAGGED_STATUS)) {
Matt Carlson17375d22009-08-28 14:02:18 +00006287 /* tp->last_tag is used in tg3_int_reenable() below
Michael Chan4fd7ab52007-10-12 01:39:50 -07006288 * to tell the hw how much work has been processed,
6289 * so we must read it before checking for more work.
6290 */
Matt Carlson898a56f2009-08-28 14:02:40 +00006291 tnapi->last_tag = sblk->status_tag;
6292 tnapi->last_irq_tag = tnapi->last_tag;
Michael Chan4fd7ab52007-10-12 01:39:50 -07006293 rmb();
6294 } else
6295 sblk->status &= ~SD_STATUS_UPDATED;
6296
Matt Carlson17375d22009-08-28 14:02:18 +00006297 if (likely(!tg3_has_work(tnapi))) {
Ben Hutchings288379f2009-01-19 16:43:59 -08006298 napi_complete(napi);
Matt Carlson17375d22009-08-28 14:02:18 +00006299 tg3_int_reenable(tnapi);
David S. Miller6f535762007-10-11 18:08:29 -07006300 break;
6301 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006302 }
6303
Stephen Hemmingerbea33482007-10-03 16:41:36 -07006304 return work_done;
David S. Miller6f535762007-10-11 18:08:29 -07006305
6306tx_recovery:
Michael Chan4fd7ab52007-10-12 01:39:50 -07006307 /* work_done is guaranteed to be less than budget. */
Ben Hutchings288379f2009-01-19 16:43:59 -08006308 napi_complete(napi);
Matt Carlsondb219972011-11-04 09:15:03 +00006309 tg3_reset_task_schedule(tp);
Michael Chan4fd7ab52007-10-12 01:39:50 -07006310 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006311}
6312
Matt Carlson66cfd1b2010-09-30 10:34:30 +00006313static void tg3_napi_disable(struct tg3 *tp)
6314{
6315 int i;
6316
6317 for (i = tp->irq_cnt - 1; i >= 0; i--)
6318 napi_disable(&tp->napi[i].napi);
6319}
6320
6321static void tg3_napi_enable(struct tg3 *tp)
6322{
6323 int i;
6324
6325 for (i = 0; i < tp->irq_cnt; i++)
6326 napi_enable(&tp->napi[i].napi);
6327}
6328
6329static void tg3_napi_init(struct tg3 *tp)
6330{
6331 int i;
6332
6333 netif_napi_add(tp->dev, &tp->napi[0].napi, tg3_poll, 64);
6334 for (i = 1; i < tp->irq_cnt; i++)
6335 netif_napi_add(tp->dev, &tp->napi[i].napi, tg3_poll_msix, 64);
6336}
6337
6338static void tg3_napi_fini(struct tg3 *tp)
6339{
6340 int i;
6341
6342 for (i = 0; i < tp->irq_cnt; i++)
6343 netif_napi_del(&tp->napi[i].napi);
6344}
6345
6346static inline void tg3_netif_stop(struct tg3 *tp)
6347{
6348 tp->dev->trans_start = jiffies; /* prevent tx timeout */
6349 tg3_napi_disable(tp);
6350 netif_tx_disable(tp->dev);
6351}
6352
6353static inline void tg3_netif_start(struct tg3 *tp)
6354{
6355 /* NOTE: unconditional netif_tx_wake_all_queues is only
6356 * appropriate so long as all callers are assured to
6357 * have free tx slots (such as after tg3_init_hw)
6358 */
6359 netif_tx_wake_all_queues(tp->dev);
6360
6361 tg3_napi_enable(tp);
6362 tp->napi[0].hw_status->status |= SD_STATUS_UPDATED;
6363 tg3_enable_ints(tp);
6364}
6365
David S. Millerf47c11e2005-06-24 20:18:35 -07006366static void tg3_irq_quiesce(struct tg3 *tp)
6367{
Matt Carlson4f125f42009-09-01 12:55:02 +00006368 int i;
6369
David S. Millerf47c11e2005-06-24 20:18:35 -07006370 BUG_ON(tp->irq_sync);
6371
6372 tp->irq_sync = 1;
6373 smp_mb();
6374
Matt Carlson4f125f42009-09-01 12:55:02 +00006375 for (i = 0; i < tp->irq_cnt; i++)
6376 synchronize_irq(tp->napi[i].irq_vec);
David S. Millerf47c11e2005-06-24 20:18:35 -07006377}
6378
David S. Millerf47c11e2005-06-24 20:18:35 -07006379/* Fully shutdown all tg3 driver activity elsewhere in the system.
6380 * If irq_sync is non-zero, then the IRQ handler must be synchronized
6381 * with as well. Most of the time, this is not necessary except when
6382 * shutting down the device.
6383 */
6384static inline void tg3_full_lock(struct tg3 *tp, int irq_sync)
6385{
Michael Chan46966542007-07-11 19:47:19 -07006386 spin_lock_bh(&tp->lock);
David S. Millerf47c11e2005-06-24 20:18:35 -07006387 if (irq_sync)
6388 tg3_irq_quiesce(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -07006389}
6390
6391static inline void tg3_full_unlock(struct tg3 *tp)
6392{
David S. Millerf47c11e2005-06-24 20:18:35 -07006393 spin_unlock_bh(&tp->lock);
6394}
6395
Michael Chanfcfa0a32006-03-20 22:28:41 -08006396/* One-shot MSI handler - Chip automatically disables interrupt
6397 * after sending MSI so driver doesn't have to do it.
6398 */
David Howells7d12e782006-10-05 14:55:46 +01006399static irqreturn_t tg3_msi_1shot(int irq, void *dev_id)
Michael Chanfcfa0a32006-03-20 22:28:41 -08006400{
Matt Carlson09943a12009-08-28 14:01:57 +00006401 struct tg3_napi *tnapi = dev_id;
6402 struct tg3 *tp = tnapi->tp;
Michael Chanfcfa0a32006-03-20 22:28:41 -08006403
Matt Carlson898a56f2009-08-28 14:02:40 +00006404 prefetch(tnapi->hw_status);
Matt Carlson0c1d0e22009-09-01 13:16:33 +00006405 if (tnapi->rx_rcb)
6406 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Michael Chanfcfa0a32006-03-20 22:28:41 -08006407
6408 if (likely(!tg3_irq_sync(tp)))
Matt Carlson09943a12009-08-28 14:01:57 +00006409 napi_schedule(&tnapi->napi);
Michael Chanfcfa0a32006-03-20 22:28:41 -08006410
6411 return IRQ_HANDLED;
6412}
6413
Michael Chan88b06bc22005-04-21 17:13:25 -07006414/* MSI ISR - No need to check for interrupt sharing and no need to
6415 * flush status block and interrupt mailbox. PCI ordering rules
6416 * guarantee that MSI will arrive after the status block.
6417 */
David Howells7d12e782006-10-05 14:55:46 +01006418static irqreturn_t tg3_msi(int irq, void *dev_id)
Michael Chan88b06bc22005-04-21 17:13:25 -07006419{
Matt Carlson09943a12009-08-28 14:01:57 +00006420 struct tg3_napi *tnapi = dev_id;
6421 struct tg3 *tp = tnapi->tp;
Michael Chan88b06bc22005-04-21 17:13:25 -07006422
Matt Carlson898a56f2009-08-28 14:02:40 +00006423 prefetch(tnapi->hw_status);
Matt Carlson0c1d0e22009-09-01 13:16:33 +00006424 if (tnapi->rx_rcb)
6425 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Michael Chan88b06bc22005-04-21 17:13:25 -07006426 /*
David S. Millerfac9b832005-05-18 22:46:34 -07006427 * Writing any value to intr-mbox-0 clears PCI INTA# and
Michael Chan88b06bc22005-04-21 17:13:25 -07006428 * chip-internal interrupt pending events.
David S. Millerfac9b832005-05-18 22:46:34 -07006429 * Writing non-zero to intr-mbox-0 additional tells the
Michael Chan88b06bc22005-04-21 17:13:25 -07006430 * NIC to stop sending us irqs, engaging "in-intr-handler"
6431 * event coalescing.
6432 */
Matt Carlson5b39de92011-08-31 11:44:50 +00006433 tw32_mailbox(tnapi->int_mbox, 0x00000001);
Michael Chan61487482005-09-05 17:53:19 -07006434 if (likely(!tg3_irq_sync(tp)))
Matt Carlson09943a12009-08-28 14:01:57 +00006435 napi_schedule(&tnapi->napi);
Michael Chan61487482005-09-05 17:53:19 -07006436
Michael Chan88b06bc22005-04-21 17:13:25 -07006437 return IRQ_RETVAL(1);
6438}
6439
David Howells7d12e782006-10-05 14:55:46 +01006440static irqreturn_t tg3_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006441{
Matt Carlson09943a12009-08-28 14:01:57 +00006442 struct tg3_napi *tnapi = dev_id;
6443 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00006444 struct tg3_hw_status *sblk = tnapi->hw_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006445 unsigned int handled = 1;
6446
Linus Torvalds1da177e2005-04-16 15:20:36 -07006447 /* In INTx mode, it is possible for the interrupt to arrive at
6448 * the CPU before the status block posted prior to the interrupt.
6449 * Reading the PCI State register will confirm whether the
6450 * interrupt is ours and will flush the status block.
6451 */
Michael Chand18edcb2007-03-24 20:57:11 -07006452 if (unlikely(!(sblk->status & SD_STATUS_UPDATED))) {
Joe Perches63c3a662011-04-26 08:12:10 +00006453 if (tg3_flag(tp, CHIP_RESETTING) ||
Michael Chand18edcb2007-03-24 20:57:11 -07006454 (tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
6455 handled = 0;
David S. Millerf47c11e2005-06-24 20:18:35 -07006456 goto out;
David S. Millerfac9b832005-05-18 22:46:34 -07006457 }
Michael Chand18edcb2007-03-24 20:57:11 -07006458 }
6459
6460 /*
6461 * Writing any value to intr-mbox-0 clears PCI INTA# and
6462 * chip-internal interrupt pending events.
6463 * Writing non-zero to intr-mbox-0 additional tells the
6464 * NIC to stop sending us irqs, engaging "in-intr-handler"
6465 * event coalescing.
Michael Chanc04cb342007-05-07 00:26:15 -07006466 *
6467 * Flush the mailbox to de-assert the IRQ immediately to prevent
6468 * spurious interrupts. The flush impacts performance but
6469 * excessive spurious interrupts can be worse in some cases.
Michael Chand18edcb2007-03-24 20:57:11 -07006470 */
Michael Chanc04cb342007-05-07 00:26:15 -07006471 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 0x00000001);
Michael Chand18edcb2007-03-24 20:57:11 -07006472 if (tg3_irq_sync(tp))
6473 goto out;
6474 sblk->status &= ~SD_STATUS_UPDATED;
Matt Carlson17375d22009-08-28 14:02:18 +00006475 if (likely(tg3_has_work(tnapi))) {
Matt Carlson72334482009-08-28 14:03:01 +00006476 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Matt Carlson09943a12009-08-28 14:01:57 +00006477 napi_schedule(&tnapi->napi);
Michael Chand18edcb2007-03-24 20:57:11 -07006478 } else {
6479 /* No work, shared interrupt perhaps? re-enable
6480 * interrupts, and flush that PCI write
6481 */
6482 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW,
6483 0x00000000);
David S. Millerfac9b832005-05-18 22:46:34 -07006484 }
David S. Millerf47c11e2005-06-24 20:18:35 -07006485out:
David S. Millerfac9b832005-05-18 22:46:34 -07006486 return IRQ_RETVAL(handled);
6487}
6488
David Howells7d12e782006-10-05 14:55:46 +01006489static irqreturn_t tg3_interrupt_tagged(int irq, void *dev_id)
David S. Millerfac9b832005-05-18 22:46:34 -07006490{
Matt Carlson09943a12009-08-28 14:01:57 +00006491 struct tg3_napi *tnapi = dev_id;
6492 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00006493 struct tg3_hw_status *sblk = tnapi->hw_status;
David S. Millerfac9b832005-05-18 22:46:34 -07006494 unsigned int handled = 1;
6495
David S. Millerfac9b832005-05-18 22:46:34 -07006496 /* In INTx mode, it is possible for the interrupt to arrive at
6497 * the CPU before the status block posted prior to the interrupt.
6498 * Reading the PCI State register will confirm whether the
6499 * interrupt is ours and will flush the status block.
6500 */
Matt Carlson898a56f2009-08-28 14:02:40 +00006501 if (unlikely(sblk->status_tag == tnapi->last_irq_tag)) {
Joe Perches63c3a662011-04-26 08:12:10 +00006502 if (tg3_flag(tp, CHIP_RESETTING) ||
Michael Chand18edcb2007-03-24 20:57:11 -07006503 (tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
6504 handled = 0;
David S. Millerf47c11e2005-06-24 20:18:35 -07006505 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006506 }
Michael Chand18edcb2007-03-24 20:57:11 -07006507 }
6508
6509 /*
6510 * writing any value to intr-mbox-0 clears PCI INTA# and
6511 * chip-internal interrupt pending events.
6512 * writing non-zero to intr-mbox-0 additional tells the
6513 * NIC to stop sending us irqs, engaging "in-intr-handler"
6514 * event coalescing.
Michael Chanc04cb342007-05-07 00:26:15 -07006515 *
6516 * Flush the mailbox to de-assert the IRQ immediately to prevent
6517 * spurious interrupts. The flush impacts performance but
6518 * excessive spurious interrupts can be worse in some cases.
Michael Chand18edcb2007-03-24 20:57:11 -07006519 */
Michael Chanc04cb342007-05-07 00:26:15 -07006520 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 0x00000001);
Matt Carlson624f8e52009-04-20 06:55:01 +00006521
6522 /*
6523 * In a shared interrupt configuration, sometimes other devices'
6524 * interrupts will scream. We record the current status tag here
6525 * so that the above check can report that the screaming interrupts
6526 * are unhandled. Eventually they will be silenced.
6527 */
Matt Carlson898a56f2009-08-28 14:02:40 +00006528 tnapi->last_irq_tag = sblk->status_tag;
Matt Carlson624f8e52009-04-20 06:55:01 +00006529
Michael Chand18edcb2007-03-24 20:57:11 -07006530 if (tg3_irq_sync(tp))
6531 goto out;
Matt Carlson624f8e52009-04-20 06:55:01 +00006532
Matt Carlson72334482009-08-28 14:03:01 +00006533 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Matt Carlson624f8e52009-04-20 06:55:01 +00006534
Matt Carlson09943a12009-08-28 14:01:57 +00006535 napi_schedule(&tnapi->napi);
Matt Carlson624f8e52009-04-20 06:55:01 +00006536
David S. Millerf47c11e2005-06-24 20:18:35 -07006537out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006538 return IRQ_RETVAL(handled);
6539}
6540
Michael Chan79381092005-04-21 17:13:59 -07006541/* ISR for interrupt test */
David Howells7d12e782006-10-05 14:55:46 +01006542static irqreturn_t tg3_test_isr(int irq, void *dev_id)
Michael Chan79381092005-04-21 17:13:59 -07006543{
Matt Carlson09943a12009-08-28 14:01:57 +00006544 struct tg3_napi *tnapi = dev_id;
6545 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00006546 struct tg3_hw_status *sblk = tnapi->hw_status;
Michael Chan79381092005-04-21 17:13:59 -07006547
Michael Chanf9804dd2005-09-27 12:13:10 -07006548 if ((sblk->status & SD_STATUS_UPDATED) ||
6549 !(tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
Michael Chanb16250e2006-09-27 16:10:14 -07006550 tg3_disable_ints(tp);
Michael Chan79381092005-04-21 17:13:59 -07006551 return IRQ_RETVAL(1);
6552 }
6553 return IRQ_RETVAL(0);
6554}
6555
Linus Torvalds1da177e2005-04-16 15:20:36 -07006556#ifdef CONFIG_NET_POLL_CONTROLLER
6557static void tg3_poll_controller(struct net_device *dev)
6558{
Matt Carlson4f125f42009-09-01 12:55:02 +00006559 int i;
Michael Chan88b06bc22005-04-21 17:13:25 -07006560 struct tg3 *tp = netdev_priv(dev);
6561
Matt Carlson4f125f42009-09-01 12:55:02 +00006562 for (i = 0; i < tp->irq_cnt; i++)
Louis Rillingfe234f02010-03-09 06:14:41 +00006563 tg3_interrupt(tp->napi[i].irq_vec, &tp->napi[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006564}
6565#endif
6566
Linus Torvalds1da177e2005-04-16 15:20:36 -07006567static void tg3_tx_timeout(struct net_device *dev)
6568{
6569 struct tg3 *tp = netdev_priv(dev);
6570
Michael Chanb0408752007-02-13 12:18:30 -08006571 if (netif_msg_tx_err(tp)) {
Joe Perches05dbe002010-02-17 19:44:19 +00006572 netdev_err(dev, "transmit timed out, resetting\n");
Matt Carlson97bd8e42011-04-13 11:05:04 +00006573 tg3_dump_state(tp);
Michael Chanb0408752007-02-13 12:18:30 -08006574 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006575
Matt Carlsondb219972011-11-04 09:15:03 +00006576 tg3_reset_task_schedule(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006577}
6578
Michael Chanc58ec932005-09-17 00:46:27 -07006579/* Test for DMA buffers crossing any 4GB boundaries: 4G, 8G, etc */
6580static inline int tg3_4g_overflow_test(dma_addr_t mapping, int len)
6581{
6582 u32 base = (u32) mapping & 0xffffffff;
6583
Eric Dumazet807540b2010-09-23 05:40:09 +00006584 return (base > 0xffffdcc0) && (base + len + 8 < base);
Michael Chanc58ec932005-09-17 00:46:27 -07006585}
6586
Michael Chan72f2afb2006-03-06 19:28:35 -08006587/* Test for DMA addresses > 40-bit */
6588static inline int tg3_40bit_overflow_test(struct tg3 *tp, dma_addr_t mapping,
6589 int len)
6590{
6591#if defined(CONFIG_HIGHMEM) && (BITS_PER_LONG == 64)
Joe Perches63c3a662011-04-26 08:12:10 +00006592 if (tg3_flag(tp, 40BIT_DMA_BUG))
Eric Dumazet807540b2010-09-23 05:40:09 +00006593 return ((u64) mapping + len) > DMA_BIT_MASK(40);
Michael Chan72f2afb2006-03-06 19:28:35 -08006594 return 0;
6595#else
6596 return 0;
6597#endif
6598}
6599
Matt Carlsond1a3b732011-07-27 14:20:51 +00006600static inline void tg3_tx_set_bd(struct tg3_tx_buffer_desc *txbd,
Matt Carlson92cd3a12011-07-27 14:20:47 +00006601 dma_addr_t mapping, u32 len, u32 flags,
6602 u32 mss, u32 vlan)
Matt Carlson2ffcc982011-05-19 12:12:44 +00006603{
Matt Carlson92cd3a12011-07-27 14:20:47 +00006604 txbd->addr_hi = ((u64) mapping >> 32);
6605 txbd->addr_lo = ((u64) mapping & 0xffffffff);
6606 txbd->len_flags = (len << TXD_LEN_SHIFT) | (flags & 0x0000ffff);
6607 txbd->vlan_tag = (mss << TXD_MSS_SHIFT) | (vlan << TXD_VLAN_TAG_SHIFT);
Matt Carlson2ffcc982011-05-19 12:12:44 +00006608}
Linus Torvalds1da177e2005-04-16 15:20:36 -07006609
Matt Carlson84b67b22011-07-27 14:20:52 +00006610static bool tg3_tx_frag_set(struct tg3_napi *tnapi, u32 *entry, u32 *budget,
Matt Carlsond1a3b732011-07-27 14:20:51 +00006611 dma_addr_t map, u32 len, u32 flags,
6612 u32 mss, u32 vlan)
6613{
6614 struct tg3 *tp = tnapi->tp;
6615 bool hwbug = false;
6616
6617 if (tg3_flag(tp, SHORT_DMA_BUG) && len <= 8)
Rusty Russell3db1cd52011-12-19 13:56:45 +00006618 hwbug = true;
Matt Carlsond1a3b732011-07-27 14:20:51 +00006619
6620 if (tg3_4g_overflow_test(map, len))
Rusty Russell3db1cd52011-12-19 13:56:45 +00006621 hwbug = true;
Matt Carlsond1a3b732011-07-27 14:20:51 +00006622
6623 if (tg3_40bit_overflow_test(tp, map, len))
Rusty Russell3db1cd52011-12-19 13:56:45 +00006624 hwbug = true;
Matt Carlsond1a3b732011-07-27 14:20:51 +00006625
Matt Carlsona4cb4282011-12-14 11:09:58 +00006626 if (tp->dma_limit) {
Matt Carlsonb9e45482011-11-04 09:14:59 +00006627 u32 prvidx = *entry;
Matt Carlsone31aa982011-07-27 14:20:53 +00006628 u32 tmp_flag = flags & ~TXD_FLAG_END;
Matt Carlsona4cb4282011-12-14 11:09:58 +00006629 while (len > tp->dma_limit && *budget) {
6630 u32 frag_len = tp->dma_limit;
6631 len -= tp->dma_limit;
Matt Carlsone31aa982011-07-27 14:20:53 +00006632
Matt Carlsonb9e45482011-11-04 09:14:59 +00006633 /* Avoid the 8byte DMA problem */
6634 if (len <= 8) {
Matt Carlsona4cb4282011-12-14 11:09:58 +00006635 len += tp->dma_limit / 2;
6636 frag_len = tp->dma_limit / 2;
Matt Carlsone31aa982011-07-27 14:20:53 +00006637 }
6638
Matt Carlsonb9e45482011-11-04 09:14:59 +00006639 tnapi->tx_buffers[*entry].fragmented = true;
6640
6641 tg3_tx_set_bd(&tnapi->tx_ring[*entry], map,
6642 frag_len, tmp_flag, mss, vlan);
6643 *budget -= 1;
6644 prvidx = *entry;
6645 *entry = NEXT_TX(*entry);
6646
Matt Carlsone31aa982011-07-27 14:20:53 +00006647 map += frag_len;
6648 }
6649
6650 if (len) {
6651 if (*budget) {
6652 tg3_tx_set_bd(&tnapi->tx_ring[*entry], map,
6653 len, flags, mss, vlan);
Matt Carlsonb9e45482011-11-04 09:14:59 +00006654 *budget -= 1;
Matt Carlsone31aa982011-07-27 14:20:53 +00006655 *entry = NEXT_TX(*entry);
6656 } else {
Rusty Russell3db1cd52011-12-19 13:56:45 +00006657 hwbug = true;
Matt Carlsonb9e45482011-11-04 09:14:59 +00006658 tnapi->tx_buffers[prvidx].fragmented = false;
Matt Carlsone31aa982011-07-27 14:20:53 +00006659 }
6660 }
6661 } else {
Matt Carlson84b67b22011-07-27 14:20:52 +00006662 tg3_tx_set_bd(&tnapi->tx_ring[*entry], map,
6663 len, flags, mss, vlan);
Matt Carlsone31aa982011-07-27 14:20:53 +00006664 *entry = NEXT_TX(*entry);
6665 }
Matt Carlsond1a3b732011-07-27 14:20:51 +00006666
6667 return hwbug;
6668}
6669
Matt Carlson0d681b22011-07-27 14:20:49 +00006670static void tg3_tx_skb_unmap(struct tg3_napi *tnapi, u32 entry, int last)
Matt Carlson432aa7e2011-05-19 12:12:45 +00006671{
6672 int i;
Matt Carlson0d681b22011-07-27 14:20:49 +00006673 struct sk_buff *skb;
Matt Carlsondf8944c2011-07-27 14:20:46 +00006674 struct tg3_tx_ring_info *txb = &tnapi->tx_buffers[entry];
Matt Carlson432aa7e2011-05-19 12:12:45 +00006675
Matt Carlson0d681b22011-07-27 14:20:49 +00006676 skb = txb->skb;
6677 txb->skb = NULL;
6678
Matt Carlson432aa7e2011-05-19 12:12:45 +00006679 pci_unmap_single(tnapi->tp->pdev,
6680 dma_unmap_addr(txb, mapping),
6681 skb_headlen(skb),
6682 PCI_DMA_TODEVICE);
Matt Carlsone01ee142011-07-27 14:20:50 +00006683
6684 while (txb->fragmented) {
6685 txb->fragmented = false;
6686 entry = NEXT_TX(entry);
6687 txb = &tnapi->tx_buffers[entry];
6688 }
6689
Matt Carlsonba1142e2011-11-04 09:15:00 +00006690 for (i = 0; i <= last; i++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00006691 const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
Matt Carlson432aa7e2011-05-19 12:12:45 +00006692
6693 entry = NEXT_TX(entry);
6694 txb = &tnapi->tx_buffers[entry];
6695
6696 pci_unmap_page(tnapi->tp->pdev,
6697 dma_unmap_addr(txb, mapping),
Eric Dumazet9e903e02011-10-18 21:00:24 +00006698 skb_frag_size(frag), PCI_DMA_TODEVICE);
Matt Carlsone01ee142011-07-27 14:20:50 +00006699
6700 while (txb->fragmented) {
6701 txb->fragmented = false;
6702 entry = NEXT_TX(entry);
6703 txb = &tnapi->tx_buffers[entry];
6704 }
Matt Carlson432aa7e2011-05-19 12:12:45 +00006705 }
6706}
6707
Michael Chan72f2afb2006-03-06 19:28:35 -08006708/* Workaround 4GB and 40-bit hardware DMA bugs. */
Matt Carlson24f4efd2009-11-13 13:03:35 +00006709static int tigon3_dma_hwbug_workaround(struct tg3_napi *tnapi,
David S. Miller1805b2f2011-10-24 18:18:09 -04006710 struct sk_buff **pskb,
Matt Carlson84b67b22011-07-27 14:20:52 +00006711 u32 *entry, u32 *budget,
Matt Carlson92cd3a12011-07-27 14:20:47 +00006712 u32 base_flags, u32 mss, u32 vlan)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006713{
Matt Carlson24f4efd2009-11-13 13:03:35 +00006714 struct tg3 *tp = tnapi->tp;
David S. Miller1805b2f2011-10-24 18:18:09 -04006715 struct sk_buff *new_skb, *skb = *pskb;
Michael Chanc58ec932005-09-17 00:46:27 -07006716 dma_addr_t new_addr = 0;
Matt Carlson432aa7e2011-05-19 12:12:45 +00006717 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006718
Matt Carlson41588ba2008-04-19 18:12:33 -07006719 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701)
6720 new_skb = skb_copy(skb, GFP_ATOMIC);
6721 else {
6722 int more_headroom = 4 - ((unsigned long)skb->data & 3);
6723
6724 new_skb = skb_copy_expand(skb,
6725 skb_headroom(skb) + more_headroom,
6726 skb_tailroom(skb), GFP_ATOMIC);
6727 }
6728
Linus Torvalds1da177e2005-04-16 15:20:36 -07006729 if (!new_skb) {
Michael Chanc58ec932005-09-17 00:46:27 -07006730 ret = -1;
6731 } else {
6732 /* New SKB is guaranteed to be linear. */
Alexander Duyckf4188d82009-12-02 16:48:38 +00006733 new_addr = pci_map_single(tp->pdev, new_skb->data, new_skb->len,
6734 PCI_DMA_TODEVICE);
6735 /* Make sure the mapping succeeded */
6736 if (pci_dma_mapping_error(tp->pdev, new_addr)) {
Alexander Duyckf4188d82009-12-02 16:48:38 +00006737 dev_kfree_skb(new_skb);
Michael Chanc58ec932005-09-17 00:46:27 -07006738 ret = -1;
Michael Chanc58ec932005-09-17 00:46:27 -07006739 } else {
Matt Carlsonb9e45482011-11-04 09:14:59 +00006740 u32 save_entry = *entry;
6741
Matt Carlson92cd3a12011-07-27 14:20:47 +00006742 base_flags |= TXD_FLAG_END;
6743
Matt Carlson84b67b22011-07-27 14:20:52 +00006744 tnapi->tx_buffers[*entry].skb = new_skb;
6745 dma_unmap_addr_set(&tnapi->tx_buffers[*entry],
Matt Carlson432aa7e2011-05-19 12:12:45 +00006746 mapping, new_addr);
6747
Matt Carlson84b67b22011-07-27 14:20:52 +00006748 if (tg3_tx_frag_set(tnapi, entry, budget, new_addr,
Matt Carlsond1a3b732011-07-27 14:20:51 +00006749 new_skb->len, base_flags,
6750 mss, vlan)) {
Matt Carlsonba1142e2011-11-04 09:15:00 +00006751 tg3_tx_skb_unmap(tnapi, save_entry, -1);
Matt Carlsond1a3b732011-07-27 14:20:51 +00006752 dev_kfree_skb(new_skb);
6753 ret = -1;
6754 }
Michael Chanc58ec932005-09-17 00:46:27 -07006755 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006756 }
6757
Linus Torvalds1da177e2005-04-16 15:20:36 -07006758 dev_kfree_skb(skb);
David S. Miller1805b2f2011-10-24 18:18:09 -04006759 *pskb = new_skb;
Michael Chanc58ec932005-09-17 00:46:27 -07006760 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006761}
6762
Matt Carlson2ffcc982011-05-19 12:12:44 +00006763static netdev_tx_t tg3_start_xmit(struct sk_buff *, struct net_device *);
Michael Chan52c0fd82006-06-29 20:15:54 -07006764
6765/* Use GSO to workaround a rare TSO bug that may be triggered when the
6766 * TSO header is greater than 80 bytes.
6767 */
6768static int tg3_tso_bug(struct tg3 *tp, struct sk_buff *skb)
6769{
6770 struct sk_buff *segs, *nskb;
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006771 u32 frag_cnt_est = skb_shinfo(skb)->gso_segs * 3;
Michael Chan52c0fd82006-06-29 20:15:54 -07006772
6773 /* Estimate the number of fragments in the worst case */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006774 if (unlikely(tg3_tx_avail(&tp->napi[0]) <= frag_cnt_est)) {
Michael Chan52c0fd82006-06-29 20:15:54 -07006775 netif_stop_queue(tp->dev);
Matt Carlsonf65aac12010-08-02 11:26:03 +00006776
6777 /* netif_tx_stop_queue() must be done before checking
6778 * checking tx index in tg3_tx_avail() below, because in
6779 * tg3_tx(), we update tx index before checking for
6780 * netif_tx_queue_stopped().
6781 */
6782 smp_mb();
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006783 if (tg3_tx_avail(&tp->napi[0]) <= frag_cnt_est)
Michael Chan7f62ad52007-02-20 23:25:40 -08006784 return NETDEV_TX_BUSY;
6785
6786 netif_wake_queue(tp->dev);
Michael Chan52c0fd82006-06-29 20:15:54 -07006787 }
6788
6789 segs = skb_gso_segment(skb, tp->dev->features & ~NETIF_F_TSO);
Hirofumi Nakagawa801678c2008-04-29 01:03:09 -07006790 if (IS_ERR(segs))
Michael Chan52c0fd82006-06-29 20:15:54 -07006791 goto tg3_tso_bug_end;
6792
6793 do {
6794 nskb = segs;
6795 segs = segs->next;
6796 nskb->next = NULL;
Matt Carlson2ffcc982011-05-19 12:12:44 +00006797 tg3_start_xmit(nskb, tp->dev);
Michael Chan52c0fd82006-06-29 20:15:54 -07006798 } while (segs);
6799
6800tg3_tso_bug_end:
6801 dev_kfree_skb(skb);
6802
6803 return NETDEV_TX_OK;
6804}
Michael Chan52c0fd82006-06-29 20:15:54 -07006805
Michael Chan5a6f3072006-03-20 22:28:05 -08006806/* hard_start_xmit for devices that have the 4G bug and/or 40-bit bug and
Joe Perches63c3a662011-04-26 08:12:10 +00006807 * support TG3_FLAG_HW_TSO_1 or firmware TSO only.
Michael Chan5a6f3072006-03-20 22:28:05 -08006808 */
Matt Carlson2ffcc982011-05-19 12:12:44 +00006809static netdev_tx_t tg3_start_xmit(struct sk_buff *skb, struct net_device *dev)
Michael Chan5a6f3072006-03-20 22:28:05 -08006810{
6811 struct tg3 *tp = netdev_priv(dev);
Matt Carlson92cd3a12011-07-27 14:20:47 +00006812 u32 len, entry, base_flags, mss, vlan = 0;
Matt Carlson84b67b22011-07-27 14:20:52 +00006813 u32 budget;
Matt Carlson432aa7e2011-05-19 12:12:45 +00006814 int i = -1, would_hit_hwbug;
David S. Miller90079ce2008-09-11 04:52:51 -07006815 dma_addr_t mapping;
Matt Carlson24f4efd2009-11-13 13:03:35 +00006816 struct tg3_napi *tnapi;
6817 struct netdev_queue *txq;
Matt Carlson432aa7e2011-05-19 12:12:45 +00006818 unsigned int last;
Alexander Duyckf4188d82009-12-02 16:48:38 +00006819
Matt Carlson24f4efd2009-11-13 13:03:35 +00006820 txq = netdev_get_tx_queue(dev, skb_get_queue_mapping(skb));
6821 tnapi = &tp->napi[skb_get_queue_mapping(skb)];
Joe Perches63c3a662011-04-26 08:12:10 +00006822 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlson24f4efd2009-11-13 13:03:35 +00006823 tnapi++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006824
Matt Carlson84b67b22011-07-27 14:20:52 +00006825 budget = tg3_tx_avail(tnapi);
6826
Michael Chan00b70502006-06-17 21:58:45 -07006827 /* We are running in BH disabled context with netif_tx_lock
Stephen Hemmingerbea33482007-10-03 16:41:36 -07006828 * and TX reclaim runs via tp->napi.poll inside of a software
David S. Millerf47c11e2005-06-24 20:18:35 -07006829 * interrupt. Furthermore, IRQ processing runs lockless so we have
6830 * no IRQ context deadlocks to worry about either. Rejoice!
Linus Torvalds1da177e2005-04-16 15:20:36 -07006831 */
Matt Carlson84b67b22011-07-27 14:20:52 +00006832 if (unlikely(budget <= (skb_shinfo(skb)->nr_frags + 1))) {
Matt Carlson24f4efd2009-11-13 13:03:35 +00006833 if (!netif_tx_queue_stopped(txq)) {
6834 netif_tx_stop_queue(txq);
Stephen Hemminger1f064a82005-12-06 17:36:44 -08006835
6836 /* This is a hard error, log it. */
Matt Carlson5129c3a2010-04-05 10:19:23 +00006837 netdev_err(dev,
6838 "BUG! Tx Ring full when queue awake!\n");
Stephen Hemminger1f064a82005-12-06 17:36:44 -08006839 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006840 return NETDEV_TX_BUSY;
6841 }
6842
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006843 entry = tnapi->tx_prod;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006844 base_flags = 0;
Patrick McHardy84fa7932006-08-29 16:44:56 -07006845 if (skb->ip_summed == CHECKSUM_PARTIAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006846 base_flags |= TXD_FLAG_TCPUDP_CSUM;
Matt Carlson24f4efd2009-11-13 13:03:35 +00006847
Matt Carlsonbe98da62010-07-11 09:31:46 +00006848 mss = skb_shinfo(skb)->gso_size;
6849 if (mss) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07006850 struct iphdr *iph;
Matt Carlson34195c32010-07-11 09:31:42 +00006851 u32 tcp_opt_len, hdr_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006852
6853 if (skb_header_cloned(skb) &&
Eric Dumazet48855432011-10-24 07:53:03 +00006854 pskb_expand_head(skb, 0, 0, GFP_ATOMIC))
6855 goto drop;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006856
Matt Carlson34195c32010-07-11 09:31:42 +00006857 iph = ip_hdr(skb);
Arnaldo Carvalho de Meloab6a5bb2007-03-18 17:43:48 -07006858 tcp_opt_len = tcp_optlen(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006859
Eric Dumazeta5a11952012-01-23 01:22:09 +00006860 hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb) - ETH_HLEN;
Matt Carlson34195c32010-07-11 09:31:42 +00006861
Eric Dumazeta5a11952012-01-23 01:22:09 +00006862 if (!skb_is_gso_v6(skb)) {
Matt Carlson34195c32010-07-11 09:31:42 +00006863 iph->check = 0;
6864 iph->tot_len = htons(mss + hdr_len);
6865 }
6866
Michael Chan52c0fd82006-06-29 20:15:54 -07006867 if (unlikely((ETH_HLEN + hdr_len) > 80) &&
Joe Perches63c3a662011-04-26 08:12:10 +00006868 tg3_flag(tp, TSO_BUG))
Matt Carlsonde6f31e2010-04-12 06:58:30 +00006869 return tg3_tso_bug(tp, skb);
Michael Chan52c0fd82006-06-29 20:15:54 -07006870
Linus Torvalds1da177e2005-04-16 15:20:36 -07006871 base_flags |= (TXD_FLAG_CPU_PRE_DMA |
6872 TXD_FLAG_CPU_POST_DMA);
6873
Joe Perches63c3a662011-04-26 08:12:10 +00006874 if (tg3_flag(tp, HW_TSO_1) ||
6875 tg3_flag(tp, HW_TSO_2) ||
6876 tg3_flag(tp, HW_TSO_3)) {
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07006877 tcp_hdr(skb)->check = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006878 base_flags &= ~TXD_FLAG_TCPUDP_CSUM;
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07006879 } else
6880 tcp_hdr(skb)->check = ~csum_tcpudp_magic(iph->saddr,
6881 iph->daddr, 0,
6882 IPPROTO_TCP,
6883 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006884
Joe Perches63c3a662011-04-26 08:12:10 +00006885 if (tg3_flag(tp, HW_TSO_3)) {
Matt Carlson615774f2009-11-13 13:03:39 +00006886 mss |= (hdr_len & 0xc) << 12;
6887 if (hdr_len & 0x10)
6888 base_flags |= 0x00000010;
6889 base_flags |= (hdr_len & 0x3e0) << 5;
Joe Perches63c3a662011-04-26 08:12:10 +00006890 } else if (tg3_flag(tp, HW_TSO_2))
Matt Carlson92c6b8d2009-11-02 14:23:27 +00006891 mss |= hdr_len << 9;
Joe Perches63c3a662011-04-26 08:12:10 +00006892 else if (tg3_flag(tp, HW_TSO_1) ||
Matt Carlson92c6b8d2009-11-02 14:23:27 +00006893 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07006894 if (tcp_opt_len || iph->ihl > 5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006895 int tsflags;
6896
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07006897 tsflags = (iph->ihl - 5) + (tcp_opt_len >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006898 mss |= (tsflags << 11);
6899 }
6900 } else {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07006901 if (tcp_opt_len || iph->ihl > 5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006902 int tsflags;
6903
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07006904 tsflags = (iph->ihl - 5) + (tcp_opt_len >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006905 base_flags |= tsflags << 12;
6906 }
6907 }
6908 }
Matt Carlsonbf933c82011-01-25 15:58:49 +00006909
Matt Carlson93a700a2011-08-31 11:44:54 +00006910 if (tg3_flag(tp, USE_JUMBO_BDFLAG) &&
6911 !mss && skb->len > VLAN_ETH_FRAME_LEN)
6912 base_flags |= TXD_FLAG_JMB_PKT;
6913
Matt Carlson92cd3a12011-07-27 14:20:47 +00006914 if (vlan_tx_tag_present(skb)) {
6915 base_flags |= TXD_FLAG_VLAN;
6916 vlan = vlan_tx_tag_get(skb);
6917 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006918
Alexander Duyckf4188d82009-12-02 16:48:38 +00006919 len = skb_headlen(skb);
6920
6921 mapping = pci_map_single(tp->pdev, skb->data, len, PCI_DMA_TODEVICE);
Eric Dumazet48855432011-10-24 07:53:03 +00006922 if (pci_dma_mapping_error(tp->pdev, mapping))
6923 goto drop;
6924
David S. Miller90079ce2008-09-11 04:52:51 -07006925
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006926 tnapi->tx_buffers[entry].skb = skb;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006927 dma_unmap_addr_set(&tnapi->tx_buffers[entry], mapping, mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006928
6929 would_hit_hwbug = 0;
6930
Joe Perches63c3a662011-04-26 08:12:10 +00006931 if (tg3_flag(tp, 5701_DMA_BUG))
Michael Chanc58ec932005-09-17 00:46:27 -07006932 would_hit_hwbug = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006933
Matt Carlson84b67b22011-07-27 14:20:52 +00006934 if (tg3_tx_frag_set(tnapi, &entry, &budget, mapping, len, base_flags |
Matt Carlsond1a3b732011-07-27 14:20:51 +00006935 ((skb_shinfo(skb)->nr_frags == 0) ? TXD_FLAG_END : 0),
Matt Carlsonba1142e2011-11-04 09:15:00 +00006936 mss, vlan)) {
Matt Carlsond1a3b732011-07-27 14:20:51 +00006937 would_hit_hwbug = 1;
Matt Carlsonba1142e2011-11-04 09:15:00 +00006938 } else if (skb_shinfo(skb)->nr_frags > 0) {
Matt Carlson92cd3a12011-07-27 14:20:47 +00006939 u32 tmp_mss = mss;
6940
6941 if (!tg3_flag(tp, HW_TSO_1) &&
6942 !tg3_flag(tp, HW_TSO_2) &&
6943 !tg3_flag(tp, HW_TSO_3))
6944 tmp_mss = 0;
6945
Matt Carlsonc5665a52012-02-13 10:20:12 +00006946 /* Now loop through additional data
6947 * fragments, and queue them.
6948 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006949 last = skb_shinfo(skb)->nr_frags - 1;
6950 for (i = 0; i <= last; i++) {
6951 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
6952
Eric Dumazet9e903e02011-10-18 21:00:24 +00006953 len = skb_frag_size(frag);
Ian Campbelldc234d02011-08-24 22:28:11 +00006954 mapping = skb_frag_dma_map(&tp->pdev->dev, frag, 0,
Ian Campbell5d6bcdf2011-10-06 11:10:48 +01006955 len, DMA_TO_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006956
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006957 tnapi->tx_buffers[entry].skb = NULL;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006958 dma_unmap_addr_set(&tnapi->tx_buffers[entry], mapping,
Alexander Duyckf4188d82009-12-02 16:48:38 +00006959 mapping);
Ian Campbell5d6bcdf2011-10-06 11:10:48 +01006960 if (dma_mapping_error(&tp->pdev->dev, mapping))
Alexander Duyckf4188d82009-12-02 16:48:38 +00006961 goto dma_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006962
Matt Carlsonb9e45482011-11-04 09:14:59 +00006963 if (!budget ||
6964 tg3_tx_frag_set(tnapi, &entry, &budget, mapping,
Matt Carlson84b67b22011-07-27 14:20:52 +00006965 len, base_flags |
6966 ((i == last) ? TXD_FLAG_END : 0),
Matt Carlsonb9e45482011-11-04 09:14:59 +00006967 tmp_mss, vlan)) {
Matt Carlson92c6b8d2009-11-02 14:23:27 +00006968 would_hit_hwbug = 1;
Matt Carlsonb9e45482011-11-04 09:14:59 +00006969 break;
6970 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006971 }
6972 }
6973
6974 if (would_hit_hwbug) {
Matt Carlson0d681b22011-07-27 14:20:49 +00006975 tg3_tx_skb_unmap(tnapi, tnapi->tx_prod, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006976
6977 /* If the workaround fails due to memory/mapping
6978 * failure, silently drop this packet.
6979 */
Matt Carlson84b67b22011-07-27 14:20:52 +00006980 entry = tnapi->tx_prod;
6981 budget = tg3_tx_avail(tnapi);
David S. Miller1805b2f2011-10-24 18:18:09 -04006982 if (tigon3_dma_hwbug_workaround(tnapi, &skb, &entry, &budget,
Matt Carlson84b67b22011-07-27 14:20:52 +00006983 base_flags, mss, vlan))
Eric Dumazet48855432011-10-24 07:53:03 +00006984 goto drop_nofree;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006985 }
6986
Richard Cochrand515b452011-06-19 03:31:41 +00006987 skb_tx_timestamp(skb);
Tom Herbert298376d2011-11-28 16:33:30 +00006988 netdev_sent_queue(tp->dev, skb->len);
Richard Cochrand515b452011-06-19 03:31:41 +00006989
Linus Torvalds1da177e2005-04-16 15:20:36 -07006990 /* Packets are ready, update Tx producer idx local and on card. */
Matt Carlson24f4efd2009-11-13 13:03:35 +00006991 tw32_tx_mbox(tnapi->prodmbox, entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006992
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006993 tnapi->tx_prod = entry;
6994 if (unlikely(tg3_tx_avail(tnapi) <= (MAX_SKB_FRAGS + 1))) {
Matt Carlson24f4efd2009-11-13 13:03:35 +00006995 netif_tx_stop_queue(txq);
Matt Carlsonf65aac12010-08-02 11:26:03 +00006996
6997 /* netif_tx_stop_queue() must be done before checking
6998 * checking tx index in tg3_tx_avail() below, because in
6999 * tg3_tx(), we update tx index before checking for
7000 * netif_tx_queue_stopped().
7001 */
7002 smp_mb();
Matt Carlsonf3f3f272009-08-28 14:03:21 +00007003 if (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi))
Matt Carlson24f4efd2009-11-13 13:03:35 +00007004 netif_tx_wake_queue(txq);
Michael Chan51b91462005-09-01 17:41:28 -07007005 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007006
Eric Dumazetcdd0db02009-05-28 00:00:41 +00007007 mmiowb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007008 return NETDEV_TX_OK;
Alexander Duyckf4188d82009-12-02 16:48:38 +00007009
7010dma_error:
Matt Carlsonba1142e2011-11-04 09:15:00 +00007011 tg3_tx_skb_unmap(tnapi, tnapi->tx_prod, --i);
Matt Carlson432aa7e2011-05-19 12:12:45 +00007012 tnapi->tx_buffers[tnapi->tx_prod].skb = NULL;
Eric Dumazet48855432011-10-24 07:53:03 +00007013drop:
7014 dev_kfree_skb(skb);
7015drop_nofree:
7016 tp->tx_dropped++;
Alexander Duyckf4188d82009-12-02 16:48:38 +00007017 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007018}
7019
Matt Carlson6e01b202011-08-19 13:58:20 +00007020static void tg3_mac_loopback(struct tg3 *tp, bool enable)
7021{
7022 if (enable) {
7023 tp->mac_mode &= ~(MAC_MODE_HALF_DUPLEX |
7024 MAC_MODE_PORT_MODE_MASK);
7025
7026 tp->mac_mode |= MAC_MODE_PORT_INT_LPBACK;
7027
7028 if (!tg3_flag(tp, 5705_PLUS))
7029 tp->mac_mode |= MAC_MODE_LINK_POLARITY;
7030
7031 if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
7032 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
7033 else
7034 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
7035 } else {
7036 tp->mac_mode &= ~MAC_MODE_PORT_INT_LPBACK;
7037
7038 if (tg3_flag(tp, 5705_PLUS) ||
7039 (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) ||
7040 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700)
7041 tp->mac_mode &= ~MAC_MODE_LINK_POLARITY;
7042 }
7043
7044 tw32(MAC_MODE, tp->mac_mode);
7045 udelay(40);
7046}
7047
Matt Carlson941ec902011-08-19 13:58:23 +00007048static int tg3_phy_lpbk_set(struct tg3 *tp, u32 speed, bool extlpbk)
Matt Carlson5e5a7f32011-08-19 13:58:21 +00007049{
Matt Carlson941ec902011-08-19 13:58:23 +00007050 u32 val, bmcr, mac_mode, ptest = 0;
Matt Carlson5e5a7f32011-08-19 13:58:21 +00007051
7052 tg3_phy_toggle_apd(tp, false);
7053 tg3_phy_toggle_automdix(tp, 0);
7054
Matt Carlson941ec902011-08-19 13:58:23 +00007055 if (extlpbk && tg3_phy_set_extloopbk(tp))
7056 return -EIO;
7057
7058 bmcr = BMCR_FULLDPLX;
Matt Carlson5e5a7f32011-08-19 13:58:21 +00007059 switch (speed) {
7060 case SPEED_10:
7061 break;
7062 case SPEED_100:
7063 bmcr |= BMCR_SPEED100;
7064 break;
7065 case SPEED_1000:
7066 default:
7067 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
7068 speed = SPEED_100;
7069 bmcr |= BMCR_SPEED100;
7070 } else {
7071 speed = SPEED_1000;
7072 bmcr |= BMCR_SPEED1000;
7073 }
7074 }
7075
Matt Carlson941ec902011-08-19 13:58:23 +00007076 if (extlpbk) {
7077 if (!(tp->phy_flags & TG3_PHYFLG_IS_FET)) {
7078 tg3_readphy(tp, MII_CTRL1000, &val);
7079 val |= CTL1000_AS_MASTER |
7080 CTL1000_ENABLE_MASTER;
7081 tg3_writephy(tp, MII_CTRL1000, val);
7082 } else {
7083 ptest = MII_TG3_FET_PTEST_TRIM_SEL |
7084 MII_TG3_FET_PTEST_TRIM_2;
7085 tg3_writephy(tp, MII_TG3_FET_PTEST, ptest);
7086 }
7087 } else
7088 bmcr |= BMCR_LOOPBACK;
7089
Matt Carlson5e5a7f32011-08-19 13:58:21 +00007090 tg3_writephy(tp, MII_BMCR, bmcr);
7091
7092 /* The write needs to be flushed for the FETs */
7093 if (tp->phy_flags & TG3_PHYFLG_IS_FET)
7094 tg3_readphy(tp, MII_BMCR, &bmcr);
7095
7096 udelay(40);
7097
7098 if ((tp->phy_flags & TG3_PHYFLG_IS_FET) &&
7099 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785) {
Matt Carlson941ec902011-08-19 13:58:23 +00007100 tg3_writephy(tp, MII_TG3_FET_PTEST, ptest |
Matt Carlson5e5a7f32011-08-19 13:58:21 +00007101 MII_TG3_FET_PTEST_FRC_TX_LINK |
7102 MII_TG3_FET_PTEST_FRC_TX_LOCK);
7103
7104 /* The write needs to be flushed for the AC131 */
7105 tg3_readphy(tp, MII_TG3_FET_PTEST, &val);
7106 }
7107
7108 /* Reset to prevent losing 1st rx packet intermittently */
7109 if ((tp->phy_flags & TG3_PHYFLG_MII_SERDES) &&
7110 tg3_flag(tp, 5780_CLASS)) {
7111 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
7112 udelay(10);
7113 tw32_f(MAC_RX_MODE, tp->rx_mode);
7114 }
7115
7116 mac_mode = tp->mac_mode &
7117 ~(MAC_MODE_PORT_MODE_MASK | MAC_MODE_HALF_DUPLEX);
7118 if (speed == SPEED_1000)
7119 mac_mode |= MAC_MODE_PORT_MODE_GMII;
7120 else
7121 mac_mode |= MAC_MODE_PORT_MODE_MII;
7122
7123 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700) {
7124 u32 masked_phy_id = tp->phy_id & TG3_PHY_ID_MASK;
7125
7126 if (masked_phy_id == TG3_PHY_ID_BCM5401)
7127 mac_mode &= ~MAC_MODE_LINK_POLARITY;
7128 else if (masked_phy_id == TG3_PHY_ID_BCM5411)
7129 mac_mode |= MAC_MODE_LINK_POLARITY;
7130
7131 tg3_writephy(tp, MII_TG3_EXT_CTRL,
7132 MII_TG3_EXT_CTRL_LNK3_LED_MODE);
7133 }
7134
7135 tw32(MAC_MODE, mac_mode);
7136 udelay(40);
Matt Carlson941ec902011-08-19 13:58:23 +00007137
7138 return 0;
Matt Carlson5e5a7f32011-08-19 13:58:21 +00007139}
7140
Michał Mirosławc8f44af2011-11-15 15:29:55 +00007141static void tg3_set_loopback(struct net_device *dev, netdev_features_t features)
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00007142{
7143 struct tg3 *tp = netdev_priv(dev);
7144
7145 if (features & NETIF_F_LOOPBACK) {
7146 if (tp->mac_mode & MAC_MODE_PORT_INT_LPBACK)
7147 return;
7148
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00007149 spin_lock_bh(&tp->lock);
Matt Carlson6e01b202011-08-19 13:58:20 +00007150 tg3_mac_loopback(tp, true);
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00007151 netif_carrier_on(tp->dev);
7152 spin_unlock_bh(&tp->lock);
7153 netdev_info(dev, "Internal MAC loopback mode enabled.\n");
7154 } else {
7155 if (!(tp->mac_mode & MAC_MODE_PORT_INT_LPBACK))
7156 return;
7157
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00007158 spin_lock_bh(&tp->lock);
Matt Carlson6e01b202011-08-19 13:58:20 +00007159 tg3_mac_loopback(tp, false);
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00007160 /* Force link status check */
7161 tg3_setup_phy(tp, 1);
7162 spin_unlock_bh(&tp->lock);
7163 netdev_info(dev, "Internal MAC loopback mode disabled.\n");
7164 }
7165}
7166
Michał Mirosławc8f44af2011-11-15 15:29:55 +00007167static netdev_features_t tg3_fix_features(struct net_device *dev,
7168 netdev_features_t features)
Michał Mirosławdc668912011-04-07 03:35:07 +00007169{
7170 struct tg3 *tp = netdev_priv(dev);
7171
Joe Perches63c3a662011-04-26 08:12:10 +00007172 if (dev->mtu > ETH_DATA_LEN && tg3_flag(tp, 5780_CLASS))
Michał Mirosławdc668912011-04-07 03:35:07 +00007173 features &= ~NETIF_F_ALL_TSO;
7174
7175 return features;
7176}
7177
Michał Mirosławc8f44af2011-11-15 15:29:55 +00007178static int tg3_set_features(struct net_device *dev, netdev_features_t features)
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00007179{
Michał Mirosławc8f44af2011-11-15 15:29:55 +00007180 netdev_features_t changed = dev->features ^ features;
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00007181
7182 if ((changed & NETIF_F_LOOPBACK) && netif_running(dev))
7183 tg3_set_loopback(dev, features);
7184
7185 return 0;
7186}
7187
Matt Carlson21f581a2009-08-28 14:00:25 +00007188static void tg3_rx_prodring_free(struct tg3 *tp,
7189 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007190{
Linus Torvalds1da177e2005-04-16 15:20:36 -07007191 int i;
7192
Matt Carlson8fea32b2010-09-15 08:59:58 +00007193 if (tpr != &tp->napi[0].prodring) {
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007194 for (i = tpr->rx_std_cons_idx; i != tpr->rx_std_prod_idx;
Matt Carlson2c49a442010-09-30 10:34:35 +00007195 i = (i + 1) & tp->rx_std_ring_mask)
Eric Dumazet9205fd92011-11-18 06:47:01 +00007196 tg3_rx_data_free(tp, &tpr->rx_std_buffers[i],
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007197 tp->rx_pkt_map_sz);
7198
Joe Perches63c3a662011-04-26 08:12:10 +00007199 if (tg3_flag(tp, JUMBO_CAPABLE)) {
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007200 for (i = tpr->rx_jmb_cons_idx;
7201 i != tpr->rx_jmb_prod_idx;
Matt Carlson2c49a442010-09-30 10:34:35 +00007202 i = (i + 1) & tp->rx_jmb_ring_mask) {
Eric Dumazet9205fd92011-11-18 06:47:01 +00007203 tg3_rx_data_free(tp, &tpr->rx_jmb_buffers[i],
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007204 TG3_RX_JMB_MAP_SZ);
7205 }
7206 }
7207
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007208 return;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007209 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007210
Matt Carlson2c49a442010-09-30 10:34:35 +00007211 for (i = 0; i <= tp->rx_std_ring_mask; i++)
Eric Dumazet9205fd92011-11-18 06:47:01 +00007212 tg3_rx_data_free(tp, &tpr->rx_std_buffers[i],
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007213 tp->rx_pkt_map_sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007214
Joe Perches63c3a662011-04-26 08:12:10 +00007215 if (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS)) {
Matt Carlson2c49a442010-09-30 10:34:35 +00007216 for (i = 0; i <= tp->rx_jmb_ring_mask; i++)
Eric Dumazet9205fd92011-11-18 06:47:01 +00007217 tg3_rx_data_free(tp, &tpr->rx_jmb_buffers[i],
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007218 TG3_RX_JMB_MAP_SZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007219 }
7220}
7221
Matt Carlsonc6cdf432010-04-05 10:19:26 +00007222/* Initialize rx rings for packet processing.
Linus Torvalds1da177e2005-04-16 15:20:36 -07007223 *
7224 * The chip has been shut down and the driver detached from
7225 * the networking, so no interrupts or new tx packets will
7226 * end up in the driver. tp->{tx,}lock are held and thus
7227 * we may not sleep.
7228 */
Matt Carlson21f581a2009-08-28 14:00:25 +00007229static int tg3_rx_prodring_alloc(struct tg3 *tp,
7230 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007231{
Matt Carlson287be122009-08-28 13:58:46 +00007232 u32 i, rx_pkt_dma_sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007233
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007234 tpr->rx_std_cons_idx = 0;
7235 tpr->rx_std_prod_idx = 0;
7236 tpr->rx_jmb_cons_idx = 0;
7237 tpr->rx_jmb_prod_idx = 0;
7238
Matt Carlson8fea32b2010-09-15 08:59:58 +00007239 if (tpr != &tp->napi[0].prodring) {
Matt Carlson2c49a442010-09-30 10:34:35 +00007240 memset(&tpr->rx_std_buffers[0], 0,
7241 TG3_RX_STD_BUFF_RING_SIZE(tp));
Matt Carlson48035722010-10-14 10:37:43 +00007242 if (tpr->rx_jmb_buffers)
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007243 memset(&tpr->rx_jmb_buffers[0], 0,
Matt Carlson2c49a442010-09-30 10:34:35 +00007244 TG3_RX_JMB_BUFF_RING_SIZE(tp));
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007245 goto done;
7246 }
7247
Linus Torvalds1da177e2005-04-16 15:20:36 -07007248 /* Zero out all descriptors. */
Matt Carlson2c49a442010-09-30 10:34:35 +00007249 memset(tpr->rx_std, 0, TG3_RX_STD_RING_BYTES(tp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007250
Matt Carlson287be122009-08-28 13:58:46 +00007251 rx_pkt_dma_sz = TG3_RX_STD_DMA_SZ;
Joe Perches63c3a662011-04-26 08:12:10 +00007252 if (tg3_flag(tp, 5780_CLASS) &&
Matt Carlson287be122009-08-28 13:58:46 +00007253 tp->dev->mtu > ETH_DATA_LEN)
7254 rx_pkt_dma_sz = TG3_RX_JMB_DMA_SZ;
7255 tp->rx_pkt_map_sz = TG3_RX_DMA_TO_MAP_SZ(rx_pkt_dma_sz);
Michael Chan7e72aad2005-07-25 12:31:17 -07007256
Linus Torvalds1da177e2005-04-16 15:20:36 -07007257 /* Initialize invariants of the rings, we only set this
7258 * stuff once. This works because the card does not
7259 * write into the rx buffer posting rings.
7260 */
Matt Carlson2c49a442010-09-30 10:34:35 +00007261 for (i = 0; i <= tp->rx_std_ring_mask; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007262 struct tg3_rx_buffer_desc *rxd;
7263
Matt Carlson21f581a2009-08-28 14:00:25 +00007264 rxd = &tpr->rx_std[i];
Matt Carlson287be122009-08-28 13:58:46 +00007265 rxd->idx_len = rx_pkt_dma_sz << RXD_LEN_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007266 rxd->type_flags = (RXD_FLAG_END << RXD_FLAGS_SHIFT);
7267 rxd->opaque = (RXD_OPAQUE_RING_STD |
7268 (i << RXD_OPAQUE_INDEX_SHIFT));
7269 }
7270
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007271 /* Now allocate fresh SKBs for each rx ring. */
7272 for (i = 0; i < tp->rx_pending; i++) {
Eric Dumazet9205fd92011-11-18 06:47:01 +00007273 if (tg3_alloc_rx_data(tp, tpr, RXD_OPAQUE_RING_STD, i) < 0) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00007274 netdev_warn(tp->dev,
7275 "Using a smaller RX standard ring. Only "
7276 "%d out of %d buffers were allocated "
7277 "successfully\n", i, tp->rx_pending);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007278 if (i == 0)
7279 goto initfail;
7280 tp->rx_pending = i;
7281 break;
7282 }
7283 }
7284
Joe Perches63c3a662011-04-26 08:12:10 +00007285 if (!tg3_flag(tp, JUMBO_CAPABLE) || tg3_flag(tp, 5780_CLASS))
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007286 goto done;
7287
Matt Carlson2c49a442010-09-30 10:34:35 +00007288 memset(tpr->rx_jmb, 0, TG3_RX_JMB_RING_BYTES(tp));
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007289
Joe Perches63c3a662011-04-26 08:12:10 +00007290 if (!tg3_flag(tp, JUMBO_RING_ENABLE))
Matt Carlson0d86df82010-02-17 15:17:00 +00007291 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007292
Matt Carlson2c49a442010-09-30 10:34:35 +00007293 for (i = 0; i <= tp->rx_jmb_ring_mask; i++) {
Matt Carlson0d86df82010-02-17 15:17:00 +00007294 struct tg3_rx_buffer_desc *rxd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007295
Matt Carlson0d86df82010-02-17 15:17:00 +00007296 rxd = &tpr->rx_jmb[i].std;
7297 rxd->idx_len = TG3_RX_JMB_DMA_SZ << RXD_LEN_SHIFT;
7298 rxd->type_flags = (RXD_FLAG_END << RXD_FLAGS_SHIFT) |
7299 RXD_FLAG_JUMBO;
7300 rxd->opaque = (RXD_OPAQUE_RING_JUMBO |
7301 (i << RXD_OPAQUE_INDEX_SHIFT));
7302 }
7303
7304 for (i = 0; i < tp->rx_jumbo_pending; i++) {
Eric Dumazet9205fd92011-11-18 06:47:01 +00007305 if (tg3_alloc_rx_data(tp, tpr, RXD_OPAQUE_RING_JUMBO, i) < 0) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00007306 netdev_warn(tp->dev,
7307 "Using a smaller RX jumbo ring. Only %d "
7308 "out of %d buffers were allocated "
7309 "successfully\n", i, tp->rx_jumbo_pending);
Matt Carlson0d86df82010-02-17 15:17:00 +00007310 if (i == 0)
7311 goto initfail;
7312 tp->rx_jumbo_pending = i;
7313 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007314 }
7315 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007316
7317done:
Michael Chan32d8c572006-07-25 16:38:29 -07007318 return 0;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007319
7320initfail:
Matt Carlson21f581a2009-08-28 14:00:25 +00007321 tg3_rx_prodring_free(tp, tpr);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007322 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007323}
7324
Matt Carlson21f581a2009-08-28 14:00:25 +00007325static void tg3_rx_prodring_fini(struct tg3 *tp,
7326 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007327{
Matt Carlson21f581a2009-08-28 14:00:25 +00007328 kfree(tpr->rx_std_buffers);
7329 tpr->rx_std_buffers = NULL;
7330 kfree(tpr->rx_jmb_buffers);
7331 tpr->rx_jmb_buffers = NULL;
7332 if (tpr->rx_std) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00007333 dma_free_coherent(&tp->pdev->dev, TG3_RX_STD_RING_BYTES(tp),
7334 tpr->rx_std, tpr->rx_std_mapping);
Matt Carlson21f581a2009-08-28 14:00:25 +00007335 tpr->rx_std = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007336 }
Matt Carlson21f581a2009-08-28 14:00:25 +00007337 if (tpr->rx_jmb) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00007338 dma_free_coherent(&tp->pdev->dev, TG3_RX_JMB_RING_BYTES(tp),
7339 tpr->rx_jmb, tpr->rx_jmb_mapping);
Matt Carlson21f581a2009-08-28 14:00:25 +00007340 tpr->rx_jmb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007341 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007342}
7343
Matt Carlson21f581a2009-08-28 14:00:25 +00007344static int tg3_rx_prodring_init(struct tg3 *tp,
7345 struct tg3_rx_prodring_set *tpr)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007346{
Matt Carlson2c49a442010-09-30 10:34:35 +00007347 tpr->rx_std_buffers = kzalloc(TG3_RX_STD_BUFF_RING_SIZE(tp),
7348 GFP_KERNEL);
Matt Carlson21f581a2009-08-28 14:00:25 +00007349 if (!tpr->rx_std_buffers)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007350 return -ENOMEM;
7351
Matt Carlson4bae65c2010-11-24 08:31:52 +00007352 tpr->rx_std = dma_alloc_coherent(&tp->pdev->dev,
7353 TG3_RX_STD_RING_BYTES(tp),
7354 &tpr->rx_std_mapping,
7355 GFP_KERNEL);
Matt Carlson21f581a2009-08-28 14:00:25 +00007356 if (!tpr->rx_std)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007357 goto err_out;
7358
Joe Perches63c3a662011-04-26 08:12:10 +00007359 if (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS)) {
Matt Carlson2c49a442010-09-30 10:34:35 +00007360 tpr->rx_jmb_buffers = kzalloc(TG3_RX_JMB_BUFF_RING_SIZE(tp),
Matt Carlson21f581a2009-08-28 14:00:25 +00007361 GFP_KERNEL);
7362 if (!tpr->rx_jmb_buffers)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007363 goto err_out;
7364
Matt Carlson4bae65c2010-11-24 08:31:52 +00007365 tpr->rx_jmb = dma_alloc_coherent(&tp->pdev->dev,
7366 TG3_RX_JMB_RING_BYTES(tp),
7367 &tpr->rx_jmb_mapping,
7368 GFP_KERNEL);
Matt Carlson21f581a2009-08-28 14:00:25 +00007369 if (!tpr->rx_jmb)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007370 goto err_out;
7371 }
7372
7373 return 0;
7374
7375err_out:
Matt Carlson21f581a2009-08-28 14:00:25 +00007376 tg3_rx_prodring_fini(tp, tpr);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007377 return -ENOMEM;
7378}
7379
7380/* Free up pending packets in all rx/tx rings.
7381 *
7382 * The chip has been shut down and the driver detached from
7383 * the networking, so no interrupts or new tx packets will
7384 * end up in the driver. tp->{tx,}lock is not held and we are not
7385 * in an interrupt context and thus may sleep.
7386 */
7387static void tg3_free_rings(struct tg3 *tp)
7388{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007389 int i, j;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007390
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007391 for (j = 0; j < tp->irq_cnt; j++) {
7392 struct tg3_napi *tnapi = &tp->napi[j];
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007393
Matt Carlson8fea32b2010-09-15 08:59:58 +00007394 tg3_rx_prodring_free(tp, &tnapi->prodring);
Matt Carlsonb28f6422010-06-05 17:24:32 +00007395
Matt Carlson0c1d0e22009-09-01 13:16:33 +00007396 if (!tnapi->tx_buffers)
7397 continue;
7398
Matt Carlson0d681b22011-07-27 14:20:49 +00007399 for (i = 0; i < TG3_TX_RING_SIZE; i++) {
7400 struct sk_buff *skb = tnapi->tx_buffers[i].skb;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007401
Matt Carlson0d681b22011-07-27 14:20:49 +00007402 if (!skb)
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007403 continue;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007404
Matt Carlsonba1142e2011-11-04 09:15:00 +00007405 tg3_tx_skb_unmap(tnapi, i,
7406 skb_shinfo(skb)->nr_frags - 1);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007407
7408 dev_kfree_skb_any(skb);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007409 }
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007410 }
Tom Herbert298376d2011-11-28 16:33:30 +00007411 netdev_reset_queue(tp->dev);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007412}
7413
7414/* Initialize tx/rx rings for packet processing.
7415 *
7416 * The chip has been shut down and the driver detached from
7417 * the networking, so no interrupts or new tx packets will
7418 * end up in the driver. tp->{tx,}lock are held and thus
7419 * we may not sleep.
7420 */
7421static int tg3_init_rings(struct tg3 *tp)
7422{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007423 int i;
Matt Carlson72334482009-08-28 14:03:01 +00007424
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007425 /* Free up all the SKBs. */
7426 tg3_free_rings(tp);
7427
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007428 for (i = 0; i < tp->irq_cnt; i++) {
7429 struct tg3_napi *tnapi = &tp->napi[i];
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007430
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007431 tnapi->last_tag = 0;
7432 tnapi->last_irq_tag = 0;
7433 tnapi->hw_status->status = 0;
7434 tnapi->hw_status->status_tag = 0;
7435 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
7436
7437 tnapi->tx_prod = 0;
7438 tnapi->tx_cons = 0;
Matt Carlson0c1d0e22009-09-01 13:16:33 +00007439 if (tnapi->tx_ring)
7440 memset(tnapi->tx_ring, 0, TG3_TX_RING_BYTES);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007441
7442 tnapi->rx_rcb_ptr = 0;
Matt Carlson0c1d0e22009-09-01 13:16:33 +00007443 if (tnapi->rx_rcb)
7444 memset(tnapi->rx_rcb, 0, TG3_RX_RCB_RING_BYTES(tp));
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007445
Matt Carlson8fea32b2010-09-15 08:59:58 +00007446 if (tg3_rx_prodring_alloc(tp, &tnapi->prodring)) {
Matt Carlsone4af1af2010-02-12 14:47:05 +00007447 tg3_free_rings(tp);
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007448 return -ENOMEM;
Matt Carlsone4af1af2010-02-12 14:47:05 +00007449 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007450 }
Matt Carlson72334482009-08-28 14:03:01 +00007451
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007452 return 0;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007453}
7454
7455/*
7456 * Must not be invoked with interrupt sources disabled and
7457 * the hardware shutdown down.
7458 */
7459static void tg3_free_consistent(struct tg3 *tp)
7460{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007461 int i;
Matt Carlson898a56f2009-08-28 14:02:40 +00007462
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007463 for (i = 0; i < tp->irq_cnt; i++) {
7464 struct tg3_napi *tnapi = &tp->napi[i];
7465
7466 if (tnapi->tx_ring) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00007467 dma_free_coherent(&tp->pdev->dev, TG3_TX_RING_BYTES,
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007468 tnapi->tx_ring, tnapi->tx_desc_mapping);
7469 tnapi->tx_ring = NULL;
7470 }
7471
7472 kfree(tnapi->tx_buffers);
7473 tnapi->tx_buffers = NULL;
7474
7475 if (tnapi->rx_rcb) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00007476 dma_free_coherent(&tp->pdev->dev,
7477 TG3_RX_RCB_RING_BYTES(tp),
7478 tnapi->rx_rcb,
7479 tnapi->rx_rcb_mapping);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007480 tnapi->rx_rcb = NULL;
7481 }
7482
Matt Carlson8fea32b2010-09-15 08:59:58 +00007483 tg3_rx_prodring_fini(tp, &tnapi->prodring);
7484
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007485 if (tnapi->hw_status) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00007486 dma_free_coherent(&tp->pdev->dev, TG3_HW_STATUS_SIZE,
7487 tnapi->hw_status,
7488 tnapi->status_mapping);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007489 tnapi->hw_status = NULL;
7490 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007491 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007492
Linus Torvalds1da177e2005-04-16 15:20:36 -07007493 if (tp->hw_stats) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00007494 dma_free_coherent(&tp->pdev->dev, sizeof(struct tg3_hw_stats),
7495 tp->hw_stats, tp->stats_mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007496 tp->hw_stats = NULL;
7497 }
7498}
7499
7500/*
7501 * Must not be invoked with interrupt sources disabled and
7502 * the hardware shutdown down. Can sleep.
7503 */
7504static int tg3_alloc_consistent(struct tg3 *tp)
7505{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007506 int i;
Matt Carlson898a56f2009-08-28 14:02:40 +00007507
Matt Carlson4bae65c2010-11-24 08:31:52 +00007508 tp->hw_stats = dma_alloc_coherent(&tp->pdev->dev,
7509 sizeof(struct tg3_hw_stats),
7510 &tp->stats_mapping,
7511 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007512 if (!tp->hw_stats)
7513 goto err_out;
7514
Linus Torvalds1da177e2005-04-16 15:20:36 -07007515 memset(tp->hw_stats, 0, sizeof(struct tg3_hw_stats));
7516
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007517 for (i = 0; i < tp->irq_cnt; i++) {
7518 struct tg3_napi *tnapi = &tp->napi[i];
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00007519 struct tg3_hw_status *sblk;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007520
Matt Carlson4bae65c2010-11-24 08:31:52 +00007521 tnapi->hw_status = dma_alloc_coherent(&tp->pdev->dev,
7522 TG3_HW_STATUS_SIZE,
7523 &tnapi->status_mapping,
7524 GFP_KERNEL);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007525 if (!tnapi->hw_status)
7526 goto err_out;
7527
7528 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00007529 sblk = tnapi->hw_status;
7530
Matt Carlson8fea32b2010-09-15 08:59:58 +00007531 if (tg3_rx_prodring_init(tp, &tnapi->prodring))
7532 goto err_out;
7533
Matt Carlson19cfaec2009-12-03 08:36:20 +00007534 /* If multivector TSS is enabled, vector 0 does not handle
7535 * tx interrupts. Don't allocate any resources for it.
7536 */
Joe Perches63c3a662011-04-26 08:12:10 +00007537 if ((!i && !tg3_flag(tp, ENABLE_TSS)) ||
7538 (i && tg3_flag(tp, ENABLE_TSS))) {
Matt Carlsondf8944c2011-07-27 14:20:46 +00007539 tnapi->tx_buffers = kzalloc(
7540 sizeof(struct tg3_tx_ring_info) *
7541 TG3_TX_RING_SIZE, GFP_KERNEL);
Matt Carlson19cfaec2009-12-03 08:36:20 +00007542 if (!tnapi->tx_buffers)
7543 goto err_out;
7544
Matt Carlson4bae65c2010-11-24 08:31:52 +00007545 tnapi->tx_ring = dma_alloc_coherent(&tp->pdev->dev,
7546 TG3_TX_RING_BYTES,
7547 &tnapi->tx_desc_mapping,
7548 GFP_KERNEL);
Matt Carlson19cfaec2009-12-03 08:36:20 +00007549 if (!tnapi->tx_ring)
7550 goto err_out;
7551 }
7552
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00007553 /*
7554 * When RSS is enabled, the status block format changes
7555 * slightly. The "rx_jumbo_consumer", "reserved",
7556 * and "rx_mini_consumer" members get mapped to the
7557 * other three rx return ring producer indexes.
7558 */
7559 switch (i) {
7560 default:
7561 tnapi->rx_rcb_prod_idx = &sblk->idx[0].rx_producer;
7562 break;
7563 case 2:
7564 tnapi->rx_rcb_prod_idx = &sblk->rx_jumbo_consumer;
7565 break;
7566 case 3:
7567 tnapi->rx_rcb_prod_idx = &sblk->reserved;
7568 break;
7569 case 4:
7570 tnapi->rx_rcb_prod_idx = &sblk->rx_mini_consumer;
7571 break;
7572 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007573
Matt Carlson0c1d0e22009-09-01 13:16:33 +00007574 /*
7575 * If multivector RSS is enabled, vector 0 does not handle
7576 * rx or tx interrupts. Don't allocate any resources for it.
7577 */
Joe Perches63c3a662011-04-26 08:12:10 +00007578 if (!i && tg3_flag(tp, ENABLE_RSS))
Matt Carlson0c1d0e22009-09-01 13:16:33 +00007579 continue;
7580
Matt Carlson4bae65c2010-11-24 08:31:52 +00007581 tnapi->rx_rcb = dma_alloc_coherent(&tp->pdev->dev,
7582 TG3_RX_RCB_RING_BYTES(tp),
7583 &tnapi->rx_rcb_mapping,
7584 GFP_KERNEL);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007585 if (!tnapi->rx_rcb)
7586 goto err_out;
7587
7588 memset(tnapi->rx_rcb, 0, TG3_RX_RCB_RING_BYTES(tp));
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007589 }
7590
Linus Torvalds1da177e2005-04-16 15:20:36 -07007591 return 0;
7592
7593err_out:
7594 tg3_free_consistent(tp);
7595 return -ENOMEM;
7596}
7597
7598#define MAX_WAIT_CNT 1000
7599
7600/* To stop a block, clear the enable bit and poll till it
7601 * clears. tp->lock is held.
7602 */
David S. Millerb3b7d6b2005-05-05 14:40:20 -07007603static int tg3_stop_block(struct tg3 *tp, unsigned long ofs, u32 enable_bit, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007604{
7605 unsigned int i;
7606 u32 val;
7607
Joe Perches63c3a662011-04-26 08:12:10 +00007608 if (tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007609 switch (ofs) {
7610 case RCVLSC_MODE:
7611 case DMAC_MODE:
7612 case MBFREE_MODE:
7613 case BUFMGR_MODE:
7614 case MEMARB_MODE:
7615 /* We can't enable/disable these bits of the
7616 * 5705/5750, just say success.
7617 */
7618 return 0;
7619
7620 default:
7621 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07007622 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007623 }
7624
7625 val = tr32(ofs);
7626 val &= ~enable_bit;
7627 tw32_f(ofs, val);
7628
7629 for (i = 0; i < MAX_WAIT_CNT; i++) {
7630 udelay(100);
7631 val = tr32(ofs);
7632 if ((val & enable_bit) == 0)
7633 break;
7634 }
7635
David S. Millerb3b7d6b2005-05-05 14:40:20 -07007636 if (i == MAX_WAIT_CNT && !silent) {
Matt Carlson2445e462010-04-05 10:19:21 +00007637 dev_err(&tp->pdev->dev,
7638 "tg3_stop_block timed out, ofs=%lx enable_bit=%x\n",
7639 ofs, enable_bit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007640 return -ENODEV;
7641 }
7642
7643 return 0;
7644}
7645
7646/* tp->lock is held. */
David S. Millerb3b7d6b2005-05-05 14:40:20 -07007647static int tg3_abort_hw(struct tg3 *tp, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007648{
7649 int i, err;
7650
7651 tg3_disable_ints(tp);
7652
7653 tp->rx_mode &= ~RX_MODE_ENABLE;
7654 tw32_f(MAC_RX_MODE, tp->rx_mode);
7655 udelay(10);
7656
David S. Millerb3b7d6b2005-05-05 14:40:20 -07007657 err = tg3_stop_block(tp, RCVBDI_MODE, RCVBDI_MODE_ENABLE, silent);
7658 err |= tg3_stop_block(tp, RCVLPC_MODE, RCVLPC_MODE_ENABLE, silent);
7659 err |= tg3_stop_block(tp, RCVLSC_MODE, RCVLSC_MODE_ENABLE, silent);
7660 err |= tg3_stop_block(tp, RCVDBDI_MODE, RCVDBDI_MODE_ENABLE, silent);
7661 err |= tg3_stop_block(tp, RCVDCC_MODE, RCVDCC_MODE_ENABLE, silent);
7662 err |= tg3_stop_block(tp, RCVCC_MODE, RCVCC_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007663
David S. Millerb3b7d6b2005-05-05 14:40:20 -07007664 err |= tg3_stop_block(tp, SNDBDS_MODE, SNDBDS_MODE_ENABLE, silent);
7665 err |= tg3_stop_block(tp, SNDBDI_MODE, SNDBDI_MODE_ENABLE, silent);
7666 err |= tg3_stop_block(tp, SNDDATAI_MODE, SNDDATAI_MODE_ENABLE, silent);
7667 err |= tg3_stop_block(tp, RDMAC_MODE, RDMAC_MODE_ENABLE, silent);
7668 err |= tg3_stop_block(tp, SNDDATAC_MODE, SNDDATAC_MODE_ENABLE, silent);
7669 err |= tg3_stop_block(tp, DMAC_MODE, DMAC_MODE_ENABLE, silent);
7670 err |= tg3_stop_block(tp, SNDBDC_MODE, SNDBDC_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007671
7672 tp->mac_mode &= ~MAC_MODE_TDE_ENABLE;
7673 tw32_f(MAC_MODE, tp->mac_mode);
7674 udelay(40);
7675
7676 tp->tx_mode &= ~TX_MODE_ENABLE;
7677 tw32_f(MAC_TX_MODE, tp->tx_mode);
7678
7679 for (i = 0; i < MAX_WAIT_CNT; i++) {
7680 udelay(100);
7681 if (!(tr32(MAC_TX_MODE) & TX_MODE_ENABLE))
7682 break;
7683 }
7684 if (i >= MAX_WAIT_CNT) {
Matt Carlsonab96b242010-04-05 10:19:22 +00007685 dev_err(&tp->pdev->dev,
7686 "%s timed out, TX_MODE_ENABLE will not clear "
7687 "MAC_TX_MODE=%08x\n", __func__, tr32(MAC_TX_MODE));
Michael Chane6de8ad2005-05-05 14:42:41 -07007688 err |= -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007689 }
7690
Michael Chane6de8ad2005-05-05 14:42:41 -07007691 err |= tg3_stop_block(tp, HOSTCC_MODE, HOSTCC_MODE_ENABLE, silent);
David S. Millerb3b7d6b2005-05-05 14:40:20 -07007692 err |= tg3_stop_block(tp, WDMAC_MODE, WDMAC_MODE_ENABLE, silent);
7693 err |= tg3_stop_block(tp, MBFREE_MODE, MBFREE_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007694
7695 tw32(FTQ_RESET, 0xffffffff);
7696 tw32(FTQ_RESET, 0x00000000);
7697
David S. Millerb3b7d6b2005-05-05 14:40:20 -07007698 err |= tg3_stop_block(tp, BUFMGR_MODE, BUFMGR_MODE_ENABLE, silent);
7699 err |= tg3_stop_block(tp, MEMARB_MODE, MEMARB_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007700
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007701 for (i = 0; i < tp->irq_cnt; i++) {
7702 struct tg3_napi *tnapi = &tp->napi[i];
7703 if (tnapi->hw_status)
7704 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
7705 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007706
Linus Torvalds1da177e2005-04-16 15:20:36 -07007707 return err;
7708}
7709
Michael Chanee6a99b2007-07-18 21:49:10 -07007710/* Save PCI command register before chip reset */
7711static void tg3_save_pci_state(struct tg3 *tp)
7712{
Matt Carlson8a6eac92007-10-21 16:17:55 -07007713 pci_read_config_word(tp->pdev, PCI_COMMAND, &tp->pci_cmd);
Michael Chanee6a99b2007-07-18 21:49:10 -07007714}
7715
7716/* Restore PCI state after chip reset */
7717static void tg3_restore_pci_state(struct tg3 *tp)
7718{
7719 u32 val;
7720
7721 /* Re-enable indirect register accesses. */
7722 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
7723 tp->misc_host_ctrl);
7724
7725 /* Set MAX PCI retry to zero. */
7726 val = (PCISTATE_ROM_ENABLE | PCISTATE_ROM_RETRY_ENABLE);
7727 if (tp->pci_chip_rev_id == CHIPREV_ID_5704_A0 &&
Joe Perches63c3a662011-04-26 08:12:10 +00007728 tg3_flag(tp, PCIX_MODE))
Michael Chanee6a99b2007-07-18 21:49:10 -07007729 val |= PCISTATE_RETRY_SAME_DMA;
Matt Carlson0d3031d2007-10-10 18:02:43 -07007730 /* Allow reads and writes to the APE register and memory space. */
Joe Perches63c3a662011-04-26 08:12:10 +00007731 if (tg3_flag(tp, ENABLE_APE))
Matt Carlson0d3031d2007-10-10 18:02:43 -07007732 val |= PCISTATE_ALLOW_APE_CTLSPC_WR |
Matt Carlsonf92d9dc12010-06-05 17:24:30 +00007733 PCISTATE_ALLOW_APE_SHMEM_WR |
7734 PCISTATE_ALLOW_APE_PSPACE_WR;
Michael Chanee6a99b2007-07-18 21:49:10 -07007735 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE, val);
7736
Matt Carlson8a6eac92007-10-21 16:17:55 -07007737 pci_write_config_word(tp->pdev, PCI_COMMAND, tp->pci_cmd);
Michael Chanee6a99b2007-07-18 21:49:10 -07007738
Matt Carlson2c55a3d2011-11-28 09:41:04 +00007739 if (!tg3_flag(tp, PCI_EXPRESS)) {
7740 pci_write_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE,
7741 tp->pci_cacheline_sz);
7742 pci_write_config_byte(tp->pdev, PCI_LATENCY_TIMER,
7743 tp->pci_lat_timer);
Michael Chan114342f2007-10-15 02:12:26 -07007744 }
Matt Carlson5f5c51e2007-11-12 21:19:37 -08007745
Michael Chanee6a99b2007-07-18 21:49:10 -07007746 /* Make sure PCI-X relaxed ordering bit is clear. */
Joe Perches63c3a662011-04-26 08:12:10 +00007747 if (tg3_flag(tp, PCIX_MODE)) {
Matt Carlson9974a352007-10-07 23:27:28 -07007748 u16 pcix_cmd;
7749
7750 pci_read_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
7751 &pcix_cmd);
7752 pcix_cmd &= ~PCI_X_CMD_ERO;
7753 pci_write_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
7754 pcix_cmd);
7755 }
Michael Chanee6a99b2007-07-18 21:49:10 -07007756
Joe Perches63c3a662011-04-26 08:12:10 +00007757 if (tg3_flag(tp, 5780_CLASS)) {
Michael Chanee6a99b2007-07-18 21:49:10 -07007758
7759 /* Chip reset on 5780 will reset MSI enable bit,
7760 * so need to restore it.
7761 */
Joe Perches63c3a662011-04-26 08:12:10 +00007762 if (tg3_flag(tp, USING_MSI)) {
Michael Chanee6a99b2007-07-18 21:49:10 -07007763 u16 ctrl;
7764
7765 pci_read_config_word(tp->pdev,
7766 tp->msi_cap + PCI_MSI_FLAGS,
7767 &ctrl);
7768 pci_write_config_word(tp->pdev,
7769 tp->msi_cap + PCI_MSI_FLAGS,
7770 ctrl | PCI_MSI_FLAGS_ENABLE);
7771 val = tr32(MSGINT_MODE);
7772 tw32(MSGINT_MODE, val | MSGINT_MODE_ENABLE);
7773 }
7774 }
7775}
7776
Linus Torvalds1da177e2005-04-16 15:20:36 -07007777/* tp->lock is held. */
7778static int tg3_chip_reset(struct tg3 *tp)
7779{
7780 u32 val;
Michael Chan1ee582d2005-08-09 20:16:46 -07007781 void (*write_op)(struct tg3 *, u32, u32);
Matt Carlson4f125f42009-09-01 12:55:02 +00007782 int i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007783
David S. Millerf49639e2006-06-09 11:58:36 -07007784 tg3_nvram_lock(tp);
7785
Matt Carlson77b483f2008-08-15 14:07:24 -07007786 tg3_ape_lock(tp, TG3_APE_LOCK_GRC);
7787
David S. Millerf49639e2006-06-09 11:58:36 -07007788 /* No matching tg3_nvram_unlock() after this because
7789 * chip reset below will undo the nvram lock.
7790 */
7791 tp->nvram_lock_cnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007792
Michael Chanee6a99b2007-07-18 21:49:10 -07007793 /* GRC_MISC_CFG core clock reset will clear the memory
7794 * enable bit in PCI register 4 and the MSI enable bit
7795 * on some chips, so we save relevant registers here.
7796 */
7797 tg3_save_pci_state(tp);
7798
Michael Chand9ab5ad2006-03-20 22:27:35 -08007799 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752 ||
Joe Perches63c3a662011-04-26 08:12:10 +00007800 tg3_flag(tp, 5755_PLUS))
Michael Chand9ab5ad2006-03-20 22:27:35 -08007801 tw32(GRC_FASTBOOT_PC, 0);
7802
Linus Torvalds1da177e2005-04-16 15:20:36 -07007803 /*
7804 * We must avoid the readl() that normally takes place.
7805 * It locks machines, causes machine checks, and other
7806 * fun things. So, temporarily disable the 5701
7807 * hardware workaround, while we do the reset.
7808 */
Michael Chan1ee582d2005-08-09 20:16:46 -07007809 write_op = tp->write32;
7810 if (write_op == tg3_write_flush_reg32)
7811 tp->write32 = tg3_write32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007812
Michael Chand18edcb2007-03-24 20:57:11 -07007813 /* Prevent the irq handler from reading or writing PCI registers
7814 * during chip reset when the memory enable bit in the PCI command
7815 * register may be cleared. The chip does not generate interrupt
7816 * at this time, but the irq handler may still be called due to irq
7817 * sharing or irqpoll.
7818 */
Joe Perches63c3a662011-04-26 08:12:10 +00007819 tg3_flag_set(tp, CHIP_RESETTING);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007820 for (i = 0; i < tp->irq_cnt; i++) {
7821 struct tg3_napi *tnapi = &tp->napi[i];
7822 if (tnapi->hw_status) {
7823 tnapi->hw_status->status = 0;
7824 tnapi->hw_status->status_tag = 0;
7825 }
7826 tnapi->last_tag = 0;
7827 tnapi->last_irq_tag = 0;
Michael Chanb8fa2f32007-04-06 17:35:37 -07007828 }
Michael Chand18edcb2007-03-24 20:57:11 -07007829 smp_mb();
Matt Carlson4f125f42009-09-01 12:55:02 +00007830
7831 for (i = 0; i < tp->irq_cnt; i++)
7832 synchronize_irq(tp->napi[i].irq_vec);
Michael Chand18edcb2007-03-24 20:57:11 -07007833
Matt Carlson255ca312009-08-25 10:07:27 +00007834 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780) {
7835 val = tr32(TG3_PCIE_LNKCTL) & ~TG3_PCIE_LNKCTL_L1_PLL_PD_EN;
7836 tw32(TG3_PCIE_LNKCTL, val | TG3_PCIE_LNKCTL_L1_PLL_PD_DIS);
7837 }
7838
Linus Torvalds1da177e2005-04-16 15:20:36 -07007839 /* do the reset */
7840 val = GRC_MISC_CFG_CORECLK_RESET;
7841
Joe Perches63c3a662011-04-26 08:12:10 +00007842 if (tg3_flag(tp, PCI_EXPRESS)) {
Matt Carlson88075d92010-08-02 11:25:58 +00007843 /* Force PCIe 1.0a mode */
7844 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
Joe Perches63c3a662011-04-26 08:12:10 +00007845 !tg3_flag(tp, 57765_PLUS) &&
Matt Carlson88075d92010-08-02 11:25:58 +00007846 tr32(TG3_PCIE_PHY_TSTCTL) ==
7847 (TG3_PCIE_PHY_TSTCTL_PCIE10 | TG3_PCIE_PHY_TSTCTL_PSCRAM))
7848 tw32(TG3_PCIE_PHY_TSTCTL, TG3_PCIE_PHY_TSTCTL_PSCRAM);
7849
Linus Torvalds1da177e2005-04-16 15:20:36 -07007850 if (tp->pci_chip_rev_id != CHIPREV_ID_5750_A0) {
7851 tw32(GRC_MISC_CFG, (1 << 29));
7852 val |= (1 << 29);
7853 }
7854 }
7855
Michael Chanb5d37722006-09-27 16:06:21 -07007856 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
7857 tw32(VCPU_STATUS, tr32(VCPU_STATUS) | VCPU_STATUS_DRV_RESET);
7858 tw32(GRC_VCPU_EXT_CTRL,
7859 tr32(GRC_VCPU_EXT_CTRL) & ~GRC_VCPU_EXT_CTRL_HALT_CPU);
7860 }
7861
Matt Carlsonf37500d2010-08-02 11:25:59 +00007862 /* Manage gphy power for all CPMU absent PCIe devices. */
Joe Perches63c3a662011-04-26 08:12:10 +00007863 if (tg3_flag(tp, 5705_PLUS) && !tg3_flag(tp, CPMU_PRESENT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007864 val |= GRC_MISC_CFG_KEEP_GPHY_POWER;
Matt Carlsonf37500d2010-08-02 11:25:59 +00007865
Linus Torvalds1da177e2005-04-16 15:20:36 -07007866 tw32(GRC_MISC_CFG, val);
7867
Michael Chan1ee582d2005-08-09 20:16:46 -07007868 /* restore 5701 hardware bug workaround write method */
7869 tp->write32 = write_op;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007870
7871 /* Unfortunately, we have to delay before the PCI read back.
7872 * Some 575X chips even will not respond to a PCI cfg access
7873 * when the reset command is given to the chip.
7874 *
7875 * How do these hardware designers expect things to work
7876 * properly if the PCI write is posted for a long period
7877 * of time? It is always necessary to have some method by
7878 * which a register read back can occur to push the write
7879 * out which does the reset.
7880 *
7881 * For most tg3 variants the trick below was working.
7882 * Ho hum...
7883 */
7884 udelay(120);
7885
7886 /* Flush PCI posted writes. The normal MMIO registers
7887 * are inaccessible at this time so this is the only
7888 * way to make this reliably (actually, this is no longer
7889 * the case, see above). I tried to use indirect
7890 * register read/write but this upset some 5701 variants.
7891 */
7892 pci_read_config_dword(tp->pdev, PCI_COMMAND, &val);
7893
7894 udelay(120);
7895
Jon Mason708ebb32011-06-27 12:56:50 +00007896 if (tg3_flag(tp, PCI_EXPRESS) && pci_pcie_cap(tp->pdev)) {
Matt Carlsone7126992009-08-25 10:08:16 +00007897 u16 val16;
7898
Linus Torvalds1da177e2005-04-16 15:20:36 -07007899 if (tp->pci_chip_rev_id == CHIPREV_ID_5750_A0) {
7900 int i;
7901 u32 cfg_val;
7902
7903 /* Wait for link training to complete. */
7904 for (i = 0; i < 5000; i++)
7905 udelay(100);
7906
7907 pci_read_config_dword(tp->pdev, 0xc4, &cfg_val);
7908 pci_write_config_dword(tp->pdev, 0xc4,
7909 cfg_val | (1 << 15));
7910 }
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007911
Matt Carlsone7126992009-08-25 10:08:16 +00007912 /* Clear the "no snoop" and "relaxed ordering" bits. */
7913 pci_read_config_word(tp->pdev,
Jon Mason708ebb32011-06-27 12:56:50 +00007914 pci_pcie_cap(tp->pdev) + PCI_EXP_DEVCTL,
Matt Carlsone7126992009-08-25 10:08:16 +00007915 &val16);
7916 val16 &= ~(PCI_EXP_DEVCTL_RELAX_EN |
7917 PCI_EXP_DEVCTL_NOSNOOP_EN);
7918 /*
7919 * Older PCIe devices only support the 128 byte
7920 * MPS setting. Enforce the restriction.
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007921 */
Joe Perches63c3a662011-04-26 08:12:10 +00007922 if (!tg3_flag(tp, CPMU_PRESENT))
Matt Carlsone7126992009-08-25 10:08:16 +00007923 val16 &= ~PCI_EXP_DEVCTL_PAYLOAD;
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007924 pci_write_config_word(tp->pdev,
Jon Mason708ebb32011-06-27 12:56:50 +00007925 pci_pcie_cap(tp->pdev) + PCI_EXP_DEVCTL,
Matt Carlsone7126992009-08-25 10:08:16 +00007926 val16);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007927
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007928 /* Clear error status */
7929 pci_write_config_word(tp->pdev,
Jon Mason708ebb32011-06-27 12:56:50 +00007930 pci_pcie_cap(tp->pdev) + PCI_EXP_DEVSTA,
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007931 PCI_EXP_DEVSTA_CED |
7932 PCI_EXP_DEVSTA_NFED |
7933 PCI_EXP_DEVSTA_FED |
7934 PCI_EXP_DEVSTA_URD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007935 }
7936
Michael Chanee6a99b2007-07-18 21:49:10 -07007937 tg3_restore_pci_state(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007938
Joe Perches63c3a662011-04-26 08:12:10 +00007939 tg3_flag_clear(tp, CHIP_RESETTING);
7940 tg3_flag_clear(tp, ERROR_PROCESSED);
Michael Chand18edcb2007-03-24 20:57:11 -07007941
Michael Chanee6a99b2007-07-18 21:49:10 -07007942 val = 0;
Joe Perches63c3a662011-04-26 08:12:10 +00007943 if (tg3_flag(tp, 5780_CLASS))
Michael Chan4cf78e42005-07-25 12:29:19 -07007944 val = tr32(MEMARB_MODE);
Michael Chanee6a99b2007-07-18 21:49:10 -07007945 tw32(MEMARB_MODE, val | MEMARB_MODE_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007946
7947 if (tp->pci_chip_rev_id == CHIPREV_ID_5750_A3) {
7948 tg3_stop_fw(tp);
7949 tw32(0x5000, 0x400);
7950 }
7951
7952 tw32(GRC_MODE, tp->grc_mode);
7953
7954 if (tp->pci_chip_rev_id == CHIPREV_ID_5705_A0) {
Andy Gospodarekab0049b2007-09-06 20:42:14 +01007955 val = tr32(0xc4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007956
7957 tw32(0xc4, val | (1 << 15));
7958 }
7959
7960 if ((tp->nic_sram_data_cfg & NIC_SRAM_DATA_CFG_MINI_PCI) != 0 &&
7961 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
7962 tp->pci_clock_ctrl |= CLOCK_CTRL_CLKRUN_OENABLE;
7963 if (tp->pci_chip_rev_id == CHIPREV_ID_5705_A0)
7964 tp->pci_clock_ctrl |= CLOCK_CTRL_FORCE_CLKRUN;
7965 tw32(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl);
7966 }
7967
Matt Carlsonf07e9af2010-08-02 11:26:07 +00007968 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Matt Carlson9e975cc2011-07-20 10:20:50 +00007969 tp->mac_mode = MAC_MODE_PORT_MODE_TBI;
Matt Carlsond2394e6b2010-11-24 08:31:47 +00007970 val = tp->mac_mode;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00007971 } else if (tp->phy_flags & TG3_PHYFLG_MII_SERDES) {
Matt Carlson9e975cc2011-07-20 10:20:50 +00007972 tp->mac_mode = MAC_MODE_PORT_MODE_GMII;
Matt Carlsond2394e6b2010-11-24 08:31:47 +00007973 val = tp->mac_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007974 } else
Matt Carlsond2394e6b2010-11-24 08:31:47 +00007975 val = 0;
7976
7977 tw32_f(MAC_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007978 udelay(40);
7979
Matt Carlson77b483f2008-08-15 14:07:24 -07007980 tg3_ape_unlock(tp, TG3_APE_LOCK_GRC);
7981
Michael Chan7a6f4362006-09-27 16:03:31 -07007982 err = tg3_poll_fw(tp);
7983 if (err)
7984 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007985
Matt Carlson0a9140c2009-08-28 12:27:50 +00007986 tg3_mdio_start(tp);
7987
Joe Perches63c3a662011-04-26 08:12:10 +00007988 if (tg3_flag(tp, PCI_EXPRESS) &&
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00007989 tp->pci_chip_rev_id != CHIPREV_ID_5750_A0 &&
7990 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
Joe Perches63c3a662011-04-26 08:12:10 +00007991 !tg3_flag(tp, 57765_PLUS)) {
Andy Gospodarekab0049b2007-09-06 20:42:14 +01007992 val = tr32(0x7c00);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007993
7994 tw32(0x7c00, val | (1 << 25));
7995 }
7996
Matt Carlsond78b59f2011-04-05 14:22:46 +00007997 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
7998 val = tr32(TG3_CPMU_CLCK_ORIDE);
7999 tw32(TG3_CPMU_CLCK_ORIDE, val & ~CPMU_CLCK_ORIDE_MAC_ORIDE_EN);
8000 }
8001
Linus Torvalds1da177e2005-04-16 15:20:36 -07008002 /* Reprobe ASF enable state. */
Joe Perches63c3a662011-04-26 08:12:10 +00008003 tg3_flag_clear(tp, ENABLE_ASF);
8004 tg3_flag_clear(tp, ASF_NEW_HANDSHAKE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008005 tg3_read_mem(tp, NIC_SRAM_DATA_SIG, &val);
8006 if (val == NIC_SRAM_DATA_SIG_MAGIC) {
8007 u32 nic_cfg;
8008
8009 tg3_read_mem(tp, NIC_SRAM_DATA_CFG, &nic_cfg);
8010 if (nic_cfg & NIC_SRAM_DATA_CFG_ASF_ENABLE) {
Joe Perches63c3a662011-04-26 08:12:10 +00008011 tg3_flag_set(tp, ENABLE_ASF);
Matt Carlson4ba526c2008-08-15 14:10:04 -07008012 tp->last_event_jiffies = jiffies;
Joe Perches63c3a662011-04-26 08:12:10 +00008013 if (tg3_flag(tp, 5750_PLUS))
8014 tg3_flag_set(tp, ASF_NEW_HANDSHAKE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008015 }
8016 }
8017
8018 return 0;
8019}
8020
Matt Carlson92feeab2011-12-08 14:40:14 +00008021static struct rtnl_link_stats64 *tg3_get_stats64(struct net_device *,
8022 struct rtnl_link_stats64 *);
8023static struct tg3_ethtool_stats *tg3_get_estats(struct tg3 *,
8024 struct tg3_ethtool_stats *);
8025
Linus Torvalds1da177e2005-04-16 15:20:36 -07008026/* tp->lock is held. */
Michael Chan944d9802005-05-29 14:57:48 -07008027static int tg3_halt(struct tg3 *tp, int kind, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008028{
8029 int err;
8030
8031 tg3_stop_fw(tp);
8032
Michael Chan944d9802005-05-29 14:57:48 -07008033 tg3_write_sig_pre_reset(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008034
David S. Millerb3b7d6b2005-05-05 14:40:20 -07008035 tg3_abort_hw(tp, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008036 err = tg3_chip_reset(tp);
8037
Matt Carlsondaba2a62009-04-20 06:58:52 +00008038 __tg3_set_mac_addr(tp, 0);
8039
Michael Chan944d9802005-05-29 14:57:48 -07008040 tg3_write_sig_legacy(tp, kind);
8041 tg3_write_sig_post_reset(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008042
Matt Carlson92feeab2011-12-08 14:40:14 +00008043 if (tp->hw_stats) {
8044 /* Save the stats across chip resets... */
8045 tg3_get_stats64(tp->dev, &tp->net_stats_prev),
8046 tg3_get_estats(tp, &tp->estats_prev);
8047
8048 /* And make sure the next sample is new data */
8049 memset(tp->hw_stats, 0, sizeof(struct tg3_hw_stats));
8050 }
8051
Linus Torvalds1da177e2005-04-16 15:20:36 -07008052 if (err)
8053 return err;
8054
8055 return 0;
8056}
8057
Linus Torvalds1da177e2005-04-16 15:20:36 -07008058static int tg3_set_mac_addr(struct net_device *dev, void *p)
8059{
8060 struct tg3 *tp = netdev_priv(dev);
8061 struct sockaddr *addr = p;
Michael Chan986e0ae2007-05-05 12:10:20 -07008062 int err = 0, skip_mac_1 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008063
Michael Chanf9804dd2005-09-27 12:13:10 -07008064 if (!is_valid_ether_addr(addr->sa_data))
8065 return -EINVAL;
8066
Linus Torvalds1da177e2005-04-16 15:20:36 -07008067 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
8068
Michael Chane75f7c92006-03-20 21:33:26 -08008069 if (!netif_running(dev))
8070 return 0;
8071
Joe Perches63c3a662011-04-26 08:12:10 +00008072 if (tg3_flag(tp, ENABLE_ASF)) {
Michael Chan986e0ae2007-05-05 12:10:20 -07008073 u32 addr0_high, addr0_low, addr1_high, addr1_low;
Michael Chan58712ef2006-04-29 18:58:01 -07008074
Michael Chan986e0ae2007-05-05 12:10:20 -07008075 addr0_high = tr32(MAC_ADDR_0_HIGH);
8076 addr0_low = tr32(MAC_ADDR_0_LOW);
8077 addr1_high = tr32(MAC_ADDR_1_HIGH);
8078 addr1_low = tr32(MAC_ADDR_1_LOW);
8079
8080 /* Skip MAC addr 1 if ASF is using it. */
8081 if ((addr0_high != addr1_high || addr0_low != addr1_low) &&
8082 !(addr1_high == 0 && addr1_low == 0))
8083 skip_mac_1 = 1;
Michael Chan58712ef2006-04-29 18:58:01 -07008084 }
Michael Chan986e0ae2007-05-05 12:10:20 -07008085 spin_lock_bh(&tp->lock);
8086 __tg3_set_mac_addr(tp, skip_mac_1);
8087 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008088
Michael Chanb9ec6c12006-07-25 16:37:27 -07008089 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008090}
8091
8092/* tp->lock is held. */
8093static void tg3_set_bdinfo(struct tg3 *tp, u32 bdinfo_addr,
8094 dma_addr_t mapping, u32 maxlen_flags,
8095 u32 nic_addr)
8096{
8097 tg3_write_mem(tp,
8098 (bdinfo_addr + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH),
8099 ((u64) mapping >> 32));
8100 tg3_write_mem(tp,
8101 (bdinfo_addr + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW),
8102 ((u64) mapping & 0xffffffff));
8103 tg3_write_mem(tp,
8104 (bdinfo_addr + TG3_BDINFO_MAXLEN_FLAGS),
8105 maxlen_flags);
8106
Joe Perches63c3a662011-04-26 08:12:10 +00008107 if (!tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008108 tg3_write_mem(tp,
8109 (bdinfo_addr + TG3_BDINFO_NIC_ADDR),
8110 nic_addr);
8111}
8112
Michael Chand244c892005-07-05 14:42:33 -07008113static void __tg3_set_coalesce(struct tg3 *tp, struct ethtool_coalesce *ec)
David S. Miller15f98502005-05-18 22:49:26 -07008114{
Matt Carlsonb6080e12009-09-01 13:12:00 +00008115 int i;
8116
Joe Perches63c3a662011-04-26 08:12:10 +00008117 if (!tg3_flag(tp, ENABLE_TSS)) {
Matt Carlsonb6080e12009-09-01 13:12:00 +00008118 tw32(HOSTCC_TXCOL_TICKS, ec->tx_coalesce_usecs);
8119 tw32(HOSTCC_TXMAX_FRAMES, ec->tx_max_coalesced_frames);
8120 tw32(HOSTCC_TXCOAL_MAXF_INT, ec->tx_max_coalesced_frames_irq);
Matt Carlsonb6080e12009-09-01 13:12:00 +00008121 } else {
8122 tw32(HOSTCC_TXCOL_TICKS, 0);
8123 tw32(HOSTCC_TXMAX_FRAMES, 0);
8124 tw32(HOSTCC_TXCOAL_MAXF_INT, 0);
Matt Carlson19cfaec2009-12-03 08:36:20 +00008125 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00008126
Joe Perches63c3a662011-04-26 08:12:10 +00008127 if (!tg3_flag(tp, ENABLE_RSS)) {
Matt Carlson19cfaec2009-12-03 08:36:20 +00008128 tw32(HOSTCC_RXCOL_TICKS, ec->rx_coalesce_usecs);
8129 tw32(HOSTCC_RXMAX_FRAMES, ec->rx_max_coalesced_frames);
8130 tw32(HOSTCC_RXCOAL_MAXF_INT, ec->rx_max_coalesced_frames_irq);
8131 } else {
Matt Carlsonb6080e12009-09-01 13:12:00 +00008132 tw32(HOSTCC_RXCOL_TICKS, 0);
8133 tw32(HOSTCC_RXMAX_FRAMES, 0);
8134 tw32(HOSTCC_RXCOAL_MAXF_INT, 0);
David S. Miller15f98502005-05-18 22:49:26 -07008135 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00008136
Joe Perches63c3a662011-04-26 08:12:10 +00008137 if (!tg3_flag(tp, 5705_PLUS)) {
David S. Miller15f98502005-05-18 22:49:26 -07008138 u32 val = ec->stats_block_coalesce_usecs;
8139
Matt Carlsonb6080e12009-09-01 13:12:00 +00008140 tw32(HOSTCC_RXCOAL_TICK_INT, ec->rx_coalesce_usecs_irq);
8141 tw32(HOSTCC_TXCOAL_TICK_INT, ec->tx_coalesce_usecs_irq);
8142
David S. Miller15f98502005-05-18 22:49:26 -07008143 if (!netif_carrier_ok(tp->dev))
8144 val = 0;
8145
8146 tw32(HOSTCC_STAT_COAL_TICKS, val);
8147 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00008148
8149 for (i = 0; i < tp->irq_cnt - 1; i++) {
8150 u32 reg;
8151
8152 reg = HOSTCC_RXCOL_TICKS_VEC1 + i * 0x18;
8153 tw32(reg, ec->rx_coalesce_usecs);
Matt Carlsonb6080e12009-09-01 13:12:00 +00008154 reg = HOSTCC_RXMAX_FRAMES_VEC1 + i * 0x18;
8155 tw32(reg, ec->rx_max_coalesced_frames);
Matt Carlsonb6080e12009-09-01 13:12:00 +00008156 reg = HOSTCC_RXCOAL_MAXF_INT_VEC1 + i * 0x18;
8157 tw32(reg, ec->rx_max_coalesced_frames_irq);
Matt Carlson19cfaec2009-12-03 08:36:20 +00008158
Joe Perches63c3a662011-04-26 08:12:10 +00008159 if (tg3_flag(tp, ENABLE_TSS)) {
Matt Carlson19cfaec2009-12-03 08:36:20 +00008160 reg = HOSTCC_TXCOL_TICKS_VEC1 + i * 0x18;
8161 tw32(reg, ec->tx_coalesce_usecs);
8162 reg = HOSTCC_TXMAX_FRAMES_VEC1 + i * 0x18;
8163 tw32(reg, ec->tx_max_coalesced_frames);
8164 reg = HOSTCC_TXCOAL_MAXF_INT_VEC1 + i * 0x18;
8165 tw32(reg, ec->tx_max_coalesced_frames_irq);
8166 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00008167 }
8168
8169 for (; i < tp->irq_max - 1; i++) {
8170 tw32(HOSTCC_RXCOL_TICKS_VEC1 + i * 0x18, 0);
Matt Carlsonb6080e12009-09-01 13:12:00 +00008171 tw32(HOSTCC_RXMAX_FRAMES_VEC1 + i * 0x18, 0);
Matt Carlsonb6080e12009-09-01 13:12:00 +00008172 tw32(HOSTCC_RXCOAL_MAXF_INT_VEC1 + i * 0x18, 0);
Matt Carlson19cfaec2009-12-03 08:36:20 +00008173
Joe Perches63c3a662011-04-26 08:12:10 +00008174 if (tg3_flag(tp, ENABLE_TSS)) {
Matt Carlson19cfaec2009-12-03 08:36:20 +00008175 tw32(HOSTCC_TXCOL_TICKS_VEC1 + i * 0x18, 0);
8176 tw32(HOSTCC_TXMAX_FRAMES_VEC1 + i * 0x18, 0);
8177 tw32(HOSTCC_TXCOAL_MAXF_INT_VEC1 + i * 0x18, 0);
8178 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00008179 }
David S. Miller15f98502005-05-18 22:49:26 -07008180}
Linus Torvalds1da177e2005-04-16 15:20:36 -07008181
8182/* tp->lock is held. */
Matt Carlson2d31eca2009-09-01 12:53:31 +00008183static void tg3_rings_reset(struct tg3 *tp)
8184{
8185 int i;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008186 u32 stblk, txrcb, rxrcb, limit;
Matt Carlson2d31eca2009-09-01 12:53:31 +00008187 struct tg3_napi *tnapi = &tp->napi[0];
8188
8189 /* Disable all transmit rings but the first. */
Joe Perches63c3a662011-04-26 08:12:10 +00008190 if (!tg3_flag(tp, 5705_PLUS))
Matt Carlson2d31eca2009-09-01 12:53:31 +00008191 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 16;
Joe Perches63c3a662011-04-26 08:12:10 +00008192 else if (tg3_flag(tp, 5717_PLUS))
Matt Carlson3d377282010-10-14 10:37:39 +00008193 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 4;
Matt Carlson55086ad2011-12-14 11:09:59 +00008194 else if (tg3_flag(tp, 57765_CLASS))
Matt Carlsonb703df62009-12-03 08:36:21 +00008195 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 2;
Matt Carlson2d31eca2009-09-01 12:53:31 +00008196 else
8197 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE;
8198
8199 for (txrcb = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE;
8200 txrcb < limit; txrcb += TG3_BDINFO_SIZE)
8201 tg3_write_mem(tp, txrcb + TG3_BDINFO_MAXLEN_FLAGS,
8202 BDINFO_FLAGS_DISABLED);
8203
8204
8205 /* Disable all receive return rings but the first. */
Joe Perches63c3a662011-04-26 08:12:10 +00008206 if (tg3_flag(tp, 5717_PLUS))
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00008207 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 17;
Joe Perches63c3a662011-04-26 08:12:10 +00008208 else if (!tg3_flag(tp, 5705_PLUS))
Matt Carlson2d31eca2009-09-01 12:53:31 +00008209 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 16;
Matt Carlsonb703df62009-12-03 08:36:21 +00008210 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
Matt Carlson55086ad2011-12-14 11:09:59 +00008211 tg3_flag(tp, 57765_CLASS))
Matt Carlson2d31eca2009-09-01 12:53:31 +00008212 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 4;
8213 else
8214 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE;
8215
8216 for (rxrcb = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE;
8217 rxrcb < limit; rxrcb += TG3_BDINFO_SIZE)
8218 tg3_write_mem(tp, rxrcb + TG3_BDINFO_MAXLEN_FLAGS,
8219 BDINFO_FLAGS_DISABLED);
8220
8221 /* Disable interrupts */
8222 tw32_mailbox_f(tp->napi[0].int_mbox, 1);
Matt Carlson0e6cf6a2011-06-13 13:38:55 +00008223 tp->napi[0].chk_msi_cnt = 0;
8224 tp->napi[0].last_rx_cons = 0;
8225 tp->napi[0].last_tx_cons = 0;
Matt Carlson2d31eca2009-09-01 12:53:31 +00008226
8227 /* Zero mailbox registers. */
Joe Perches63c3a662011-04-26 08:12:10 +00008228 if (tg3_flag(tp, SUPPORT_MSIX)) {
Matt Carlson6fd45cb2010-09-15 08:59:57 +00008229 for (i = 1; i < tp->irq_max; i++) {
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008230 tp->napi[i].tx_prod = 0;
8231 tp->napi[i].tx_cons = 0;
Joe Perches63c3a662011-04-26 08:12:10 +00008232 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlsonc2353a32010-01-20 16:58:08 +00008233 tw32_mailbox(tp->napi[i].prodmbox, 0);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008234 tw32_rx_mbox(tp->napi[i].consmbox, 0);
8235 tw32_mailbox_f(tp->napi[i].int_mbox, 1);
Matt Carlson7f230732011-08-31 11:44:48 +00008236 tp->napi[i].chk_msi_cnt = 0;
Matt Carlson0e6cf6a2011-06-13 13:38:55 +00008237 tp->napi[i].last_rx_cons = 0;
8238 tp->napi[i].last_tx_cons = 0;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008239 }
Joe Perches63c3a662011-04-26 08:12:10 +00008240 if (!tg3_flag(tp, ENABLE_TSS))
Matt Carlsonc2353a32010-01-20 16:58:08 +00008241 tw32_mailbox(tp->napi[0].prodmbox, 0);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008242 } else {
8243 tp->napi[0].tx_prod = 0;
8244 tp->napi[0].tx_cons = 0;
8245 tw32_mailbox(tp->napi[0].prodmbox, 0);
8246 tw32_rx_mbox(tp->napi[0].consmbox, 0);
8247 }
Matt Carlson2d31eca2009-09-01 12:53:31 +00008248
8249 /* Make sure the NIC-based send BD rings are disabled. */
Joe Perches63c3a662011-04-26 08:12:10 +00008250 if (!tg3_flag(tp, 5705_PLUS)) {
Matt Carlson2d31eca2009-09-01 12:53:31 +00008251 u32 mbox = MAILBOX_SNDNIC_PROD_IDX_0 + TG3_64BIT_REG_LOW;
8252 for (i = 0; i < 16; i++)
8253 tw32_tx_mbox(mbox + i * 8, 0);
8254 }
8255
8256 txrcb = NIC_SRAM_SEND_RCB;
8257 rxrcb = NIC_SRAM_RCV_RET_RCB;
8258
8259 /* Clear status block in ram. */
8260 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
8261
8262 /* Set status block DMA address */
8263 tw32(HOSTCC_STATUS_BLK_HOST_ADDR + TG3_64BIT_REG_HIGH,
8264 ((u64) tnapi->status_mapping >> 32));
8265 tw32(HOSTCC_STATUS_BLK_HOST_ADDR + TG3_64BIT_REG_LOW,
8266 ((u64) tnapi->status_mapping & 0xffffffff));
8267
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008268 if (tnapi->tx_ring) {
8269 tg3_set_bdinfo(tp, txrcb, tnapi->tx_desc_mapping,
8270 (TG3_TX_RING_SIZE <<
8271 BDINFO_FLAGS_MAXLEN_SHIFT),
8272 NIC_SRAM_TX_BUFFER_DESC);
8273 txrcb += TG3_BDINFO_SIZE;
8274 }
Matt Carlson2d31eca2009-09-01 12:53:31 +00008275
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008276 if (tnapi->rx_rcb) {
8277 tg3_set_bdinfo(tp, rxrcb, tnapi->rx_rcb_mapping,
Matt Carlson7cb32cf2010-09-30 10:34:36 +00008278 (tp->rx_ret_ring_mask + 1) <<
8279 BDINFO_FLAGS_MAXLEN_SHIFT, 0);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008280 rxrcb += TG3_BDINFO_SIZE;
8281 }
8282
8283 stblk = HOSTCC_STATBLCK_RING1;
8284
8285 for (i = 1, tnapi++; i < tp->irq_cnt; i++, tnapi++) {
8286 u64 mapping = (u64)tnapi->status_mapping;
8287 tw32(stblk + TG3_64BIT_REG_HIGH, mapping >> 32);
8288 tw32(stblk + TG3_64BIT_REG_LOW, mapping & 0xffffffff);
8289
8290 /* Clear status block in ram. */
8291 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
8292
Matt Carlson19cfaec2009-12-03 08:36:20 +00008293 if (tnapi->tx_ring) {
8294 tg3_set_bdinfo(tp, txrcb, tnapi->tx_desc_mapping,
8295 (TG3_TX_RING_SIZE <<
8296 BDINFO_FLAGS_MAXLEN_SHIFT),
8297 NIC_SRAM_TX_BUFFER_DESC);
8298 txrcb += TG3_BDINFO_SIZE;
8299 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008300
8301 tg3_set_bdinfo(tp, rxrcb, tnapi->rx_rcb_mapping,
Matt Carlson7cb32cf2010-09-30 10:34:36 +00008302 ((tp->rx_ret_ring_mask + 1) <<
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008303 BDINFO_FLAGS_MAXLEN_SHIFT), 0);
8304
8305 stblk += 8;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008306 rxrcb += TG3_BDINFO_SIZE;
8307 }
Matt Carlson2d31eca2009-09-01 12:53:31 +00008308}
8309
Matt Carlsoneb07a942011-04-20 07:57:36 +00008310static void tg3_setup_rxbd_thresholds(struct tg3 *tp)
8311{
8312 u32 val, bdcache_maxcnt, host_rep_thresh, nic_rep_thresh;
8313
Joe Perches63c3a662011-04-26 08:12:10 +00008314 if (!tg3_flag(tp, 5750_PLUS) ||
8315 tg3_flag(tp, 5780_CLASS) ||
Matt Carlsoneb07a942011-04-20 07:57:36 +00008316 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 ||
Matt Carlson513aa6e2011-11-21 15:01:18 +00008317 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752 ||
8318 tg3_flag(tp, 57765_PLUS))
Matt Carlsoneb07a942011-04-20 07:57:36 +00008319 bdcache_maxcnt = TG3_SRAM_RX_STD_BDCACHE_SIZE_5700;
8320 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
8321 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787)
8322 bdcache_maxcnt = TG3_SRAM_RX_STD_BDCACHE_SIZE_5755;
8323 else
8324 bdcache_maxcnt = TG3_SRAM_RX_STD_BDCACHE_SIZE_5906;
8325
8326 nic_rep_thresh = min(bdcache_maxcnt / 2, tp->rx_std_max_post);
8327 host_rep_thresh = max_t(u32, tp->rx_pending / 8, 1);
8328
8329 val = min(nic_rep_thresh, host_rep_thresh);
8330 tw32(RCVBDI_STD_THRESH, val);
8331
Joe Perches63c3a662011-04-26 08:12:10 +00008332 if (tg3_flag(tp, 57765_PLUS))
Matt Carlsoneb07a942011-04-20 07:57:36 +00008333 tw32(STD_REPLENISH_LWM, bdcache_maxcnt);
8334
Joe Perches63c3a662011-04-26 08:12:10 +00008335 if (!tg3_flag(tp, JUMBO_CAPABLE) || tg3_flag(tp, 5780_CLASS))
Matt Carlsoneb07a942011-04-20 07:57:36 +00008336 return;
8337
Matt Carlson513aa6e2011-11-21 15:01:18 +00008338 bdcache_maxcnt = TG3_SRAM_RX_JMB_BDCACHE_SIZE_5700;
Matt Carlsoneb07a942011-04-20 07:57:36 +00008339
8340 host_rep_thresh = max_t(u32, tp->rx_jumbo_pending / 8, 1);
8341
8342 val = min(bdcache_maxcnt / 2, host_rep_thresh);
8343 tw32(RCVBDI_JUMBO_THRESH, val);
8344
Joe Perches63c3a662011-04-26 08:12:10 +00008345 if (tg3_flag(tp, 57765_PLUS))
Matt Carlsoneb07a942011-04-20 07:57:36 +00008346 tw32(JMB_REPLENISH_LWM, bdcache_maxcnt);
8347}
8348
Matt Carlsonccd5ba92012-02-13 10:20:08 +00008349static inline u32 calc_crc(unsigned char *buf, int len)
8350{
8351 u32 reg;
8352 u32 tmp;
8353 int j, k;
8354
8355 reg = 0xffffffff;
8356
8357 for (j = 0; j < len; j++) {
8358 reg ^= buf[j];
8359
8360 for (k = 0; k < 8; k++) {
8361 tmp = reg & 0x01;
8362
8363 reg >>= 1;
8364
8365 if (tmp)
8366 reg ^= 0xedb88320;
8367 }
8368 }
8369
8370 return ~reg;
8371}
8372
8373static void tg3_set_multi(struct tg3 *tp, unsigned int accept_all)
8374{
8375 /* accept or reject all multicast frames */
8376 tw32(MAC_HASH_REG_0, accept_all ? 0xffffffff : 0);
8377 tw32(MAC_HASH_REG_1, accept_all ? 0xffffffff : 0);
8378 tw32(MAC_HASH_REG_2, accept_all ? 0xffffffff : 0);
8379 tw32(MAC_HASH_REG_3, accept_all ? 0xffffffff : 0);
8380}
8381
8382static void __tg3_set_rx_mode(struct net_device *dev)
8383{
8384 struct tg3 *tp = netdev_priv(dev);
8385 u32 rx_mode;
8386
8387 rx_mode = tp->rx_mode & ~(RX_MODE_PROMISC |
8388 RX_MODE_KEEP_VLAN_TAG);
8389
8390#if !defined(CONFIG_VLAN_8021Q) && !defined(CONFIG_VLAN_8021Q_MODULE)
8391 /* When ASF is in use, we always keep the RX_MODE_KEEP_VLAN_TAG
8392 * flag clear.
8393 */
8394 if (!tg3_flag(tp, ENABLE_ASF))
8395 rx_mode |= RX_MODE_KEEP_VLAN_TAG;
8396#endif
8397
8398 if (dev->flags & IFF_PROMISC) {
8399 /* Promiscuous mode. */
8400 rx_mode |= RX_MODE_PROMISC;
8401 } else if (dev->flags & IFF_ALLMULTI) {
8402 /* Accept all multicast. */
8403 tg3_set_multi(tp, 1);
8404 } else if (netdev_mc_empty(dev)) {
8405 /* Reject all multicast. */
8406 tg3_set_multi(tp, 0);
8407 } else {
8408 /* Accept one or more multicast(s). */
8409 struct netdev_hw_addr *ha;
8410 u32 mc_filter[4] = { 0, };
8411 u32 regidx;
8412 u32 bit;
8413 u32 crc;
8414
8415 netdev_for_each_mc_addr(ha, dev) {
8416 crc = calc_crc(ha->addr, ETH_ALEN);
8417 bit = ~crc & 0x7f;
8418 regidx = (bit & 0x60) >> 5;
8419 bit &= 0x1f;
8420 mc_filter[regidx] |= (1 << bit);
8421 }
8422
8423 tw32(MAC_HASH_REG_0, mc_filter[0]);
8424 tw32(MAC_HASH_REG_1, mc_filter[1]);
8425 tw32(MAC_HASH_REG_2, mc_filter[2]);
8426 tw32(MAC_HASH_REG_3, mc_filter[3]);
8427 }
8428
8429 if (rx_mode != tp->rx_mode) {
8430 tp->rx_mode = rx_mode;
8431 tw32_f(MAC_RX_MODE, rx_mode);
8432 udelay(10);
8433 }
8434}
8435
Matt Carlson90415472011-12-16 13:33:23 +00008436static void tg3_rss_init_dflt_indir_tbl(struct tg3 *tp)
8437{
8438 int i;
8439
8440 for (i = 0; i < TG3_RSS_INDIR_TBL_SIZE; i++)
8441 tp->rss_ind_tbl[i] =
8442 ethtool_rxfh_indir_default(i, tp->irq_cnt - 1);
8443}
8444
8445static void tg3_rss_check_indir_tbl(struct tg3 *tp)
Matt Carlsonbcebcc42011-12-14 11:10:01 +00008446{
8447 int i;
8448
8449 if (!tg3_flag(tp, SUPPORT_MSIX))
8450 return;
8451
Matt Carlson90415472011-12-16 13:33:23 +00008452 if (tp->irq_cnt <= 2) {
Matt Carlsonbcebcc42011-12-14 11:10:01 +00008453 memset(&tp->rss_ind_tbl[0], 0, sizeof(tp->rss_ind_tbl));
Matt Carlson90415472011-12-16 13:33:23 +00008454 return;
8455 }
8456
8457 /* Validate table against current IRQ count */
8458 for (i = 0; i < TG3_RSS_INDIR_TBL_SIZE; i++) {
8459 if (tp->rss_ind_tbl[i] >= tp->irq_cnt - 1)
8460 break;
8461 }
8462
8463 if (i != TG3_RSS_INDIR_TBL_SIZE)
8464 tg3_rss_init_dflt_indir_tbl(tp);
Matt Carlsonbcebcc42011-12-14 11:10:01 +00008465}
8466
Matt Carlson90415472011-12-16 13:33:23 +00008467static void tg3_rss_write_indir_tbl(struct tg3 *tp)
Matt Carlsonbcebcc42011-12-14 11:10:01 +00008468{
8469 int i = 0;
8470 u32 reg = MAC_RSS_INDIR_TBL_0;
8471
8472 while (i < TG3_RSS_INDIR_TBL_SIZE) {
8473 u32 val = tp->rss_ind_tbl[i];
8474 i++;
8475 for (; i % 8; i++) {
8476 val <<= 4;
8477 val |= tp->rss_ind_tbl[i];
8478 }
8479 tw32(reg, val);
8480 reg += 4;
8481 }
8482}
8483
Matt Carlson2d31eca2009-09-01 12:53:31 +00008484/* tp->lock is held. */
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07008485static int tg3_reset_hw(struct tg3 *tp, int reset_phy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008486{
8487 u32 val, rdmac_mode;
8488 int i, err, limit;
Matt Carlson8fea32b2010-09-15 08:59:58 +00008489 struct tg3_rx_prodring_set *tpr = &tp->napi[0].prodring;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008490
8491 tg3_disable_ints(tp);
8492
8493 tg3_stop_fw(tp);
8494
8495 tg3_write_sig_pre_reset(tp, RESET_KIND_INIT);
8496
Joe Perches63c3a662011-04-26 08:12:10 +00008497 if (tg3_flag(tp, INIT_COMPLETE))
Michael Chane6de8ad2005-05-05 14:42:41 -07008498 tg3_abort_hw(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008499
Matt Carlson699c0192010-12-06 08:28:51 +00008500 /* Enable MAC control of LPI */
8501 if (tp->phy_flags & TG3_PHYFLG_EEE_CAP) {
8502 tw32_f(TG3_CPMU_EEE_LNKIDL_CTRL,
8503 TG3_CPMU_EEE_LNKIDL_PCIE_NL0 |
8504 TG3_CPMU_EEE_LNKIDL_UART_IDL);
8505
8506 tw32_f(TG3_CPMU_EEE_CTRL,
8507 TG3_CPMU_EEE_CTRL_EXIT_20_1_US);
8508
Matt Carlsona386b902010-12-06 08:28:53 +00008509 val = TG3_CPMU_EEEMD_ERLY_L1_XIT_DET |
8510 TG3_CPMU_EEEMD_LPI_IN_TX |
8511 TG3_CPMU_EEEMD_LPI_IN_RX |
8512 TG3_CPMU_EEEMD_EEE_ENABLE;
8513
8514 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717)
8515 val |= TG3_CPMU_EEEMD_SND_IDX_DET_EN;
8516
Joe Perches63c3a662011-04-26 08:12:10 +00008517 if (tg3_flag(tp, ENABLE_APE))
Matt Carlsona386b902010-12-06 08:28:53 +00008518 val |= TG3_CPMU_EEEMD_APE_TX_DET_EN;
8519
8520 tw32_f(TG3_CPMU_EEE_MODE, val);
8521
8522 tw32_f(TG3_CPMU_EEE_DBTMR1,
8523 TG3_CPMU_DBTMR1_PCIEXIT_2047US |
8524 TG3_CPMU_DBTMR1_LNKIDLE_2047US);
8525
8526 tw32_f(TG3_CPMU_EEE_DBTMR2,
Matt Carlsond7f2ab22011-01-25 15:58:56 +00008527 TG3_CPMU_DBTMR2_APE_TX_2047US |
Matt Carlsona386b902010-12-06 08:28:53 +00008528 TG3_CPMU_DBTMR2_TXIDXEQ_2047US);
Matt Carlson699c0192010-12-06 08:28:51 +00008529 }
8530
Matt Carlson603f1172010-02-12 14:47:10 +00008531 if (reset_phy)
Michael Chand4d2c552006-03-20 17:47:20 -08008532 tg3_phy_reset(tp);
8533
Linus Torvalds1da177e2005-04-16 15:20:36 -07008534 err = tg3_chip_reset(tp);
8535 if (err)
8536 return err;
8537
8538 tg3_write_sig_legacy(tp, RESET_KIND_INIT);
8539
Matt Carlsonbcb37f62008-11-03 16:52:09 -08008540 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX) {
Matt Carlsond30cdd22007-10-07 23:28:35 -07008541 val = tr32(TG3_CPMU_CTRL);
8542 val &= ~(CPMU_CTRL_LINK_AWARE_MODE | CPMU_CTRL_LINK_IDLE_MODE);
8543 tw32(TG3_CPMU_CTRL, val);
Matt Carlson9acb9612007-11-12 21:10:06 -08008544
8545 val = tr32(TG3_CPMU_LSPD_10MB_CLK);
8546 val &= ~CPMU_LSPD_10MB_MACCLK_MASK;
8547 val |= CPMU_LSPD_10MB_MACCLK_6_25;
8548 tw32(TG3_CPMU_LSPD_10MB_CLK, val);
8549
8550 val = tr32(TG3_CPMU_LNK_AWARE_PWRMD);
8551 val &= ~CPMU_LNK_AWARE_MACCLK_MASK;
8552 val |= CPMU_LNK_AWARE_MACCLK_6_25;
8553 tw32(TG3_CPMU_LNK_AWARE_PWRMD, val);
8554
8555 val = tr32(TG3_CPMU_HST_ACC);
8556 val &= ~CPMU_HST_ACC_MACCLK_MASK;
8557 val |= CPMU_HST_ACC_MACCLK_6_25;
8558 tw32(TG3_CPMU_HST_ACC, val);
Matt Carlsond30cdd22007-10-07 23:28:35 -07008559 }
8560
Matt Carlson33466d92009-04-20 06:57:41 +00008561 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780) {
8562 val = tr32(PCIE_PWR_MGMT_THRESH) & ~PCIE_PWR_MGMT_L1_THRESH_MSK;
8563 val |= PCIE_PWR_MGMT_EXT_ASPM_TMR_EN |
8564 PCIE_PWR_MGMT_L1_THRESH_4MS;
8565 tw32(PCIE_PWR_MGMT_THRESH, val);
Matt Carlson521e6b92009-08-25 10:06:01 +00008566
8567 val = tr32(TG3_PCIE_EIDLE_DELAY) & ~TG3_PCIE_EIDLE_DELAY_MASK;
8568 tw32(TG3_PCIE_EIDLE_DELAY, val | TG3_PCIE_EIDLE_DELAY_13_CLKS);
8569
8570 tw32(TG3_CORR_ERR_STAT, TG3_CORR_ERR_STAT_CLEAR);
Matt Carlson33466d92009-04-20 06:57:41 +00008571
Matt Carlsonf40386c2009-11-02 14:24:02 +00008572 val = tr32(TG3_PCIE_LNKCTL) & ~TG3_PCIE_LNKCTL_L1_PLL_PD_EN;
8573 tw32(TG3_PCIE_LNKCTL, val | TG3_PCIE_LNKCTL_L1_PLL_PD_DIS);
Matt Carlson255ca312009-08-25 10:07:27 +00008574 }
8575
Joe Perches63c3a662011-04-26 08:12:10 +00008576 if (tg3_flag(tp, L1PLLPD_EN)) {
Matt Carlson614b0592010-01-20 16:58:02 +00008577 u32 grc_mode = tr32(GRC_MODE);
8578
8579 /* Access the lower 1K of PL PCIE block registers. */
8580 val = grc_mode & ~GRC_MODE_PCIE_PORT_MASK;
8581 tw32(GRC_MODE, val | GRC_MODE_PCIE_PL_SEL);
8582
8583 val = tr32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL1);
8584 tw32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL1,
8585 val | TG3_PCIE_PL_LO_PHYCTL1_L1PLLPD_EN);
8586
8587 tw32(GRC_MODE, grc_mode);
8588 }
8589
Matt Carlson55086ad2011-12-14 11:09:59 +00008590 if (tg3_flag(tp, 57765_CLASS)) {
Matt Carlson5093eed2010-11-24 08:31:45 +00008591 if (tp->pci_chip_rev_id == CHIPREV_ID_57765_A0) {
8592 u32 grc_mode = tr32(GRC_MODE);
Matt Carlsoncea46462010-04-12 06:58:24 +00008593
Matt Carlson5093eed2010-11-24 08:31:45 +00008594 /* Access the lower 1K of PL PCIE block registers. */
8595 val = grc_mode & ~GRC_MODE_PCIE_PORT_MASK;
8596 tw32(GRC_MODE, val | GRC_MODE_PCIE_PL_SEL);
Matt Carlsoncea46462010-04-12 06:58:24 +00008597
Matt Carlson5093eed2010-11-24 08:31:45 +00008598 val = tr32(TG3_PCIE_TLDLPL_PORT +
8599 TG3_PCIE_PL_LO_PHYCTL5);
8600 tw32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL5,
8601 val | TG3_PCIE_PL_LO_PHYCTL5_DIS_L2CLKREQ);
Matt Carlsoncea46462010-04-12 06:58:24 +00008602
Matt Carlson5093eed2010-11-24 08:31:45 +00008603 tw32(GRC_MODE, grc_mode);
8604 }
Matt Carlsona977dbe2010-04-12 06:58:26 +00008605
Matt Carlson1ff30a52011-05-19 12:12:46 +00008606 if (GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_57765_AX) {
8607 u32 grc_mode = tr32(GRC_MODE);
8608
8609 /* Access the lower 1K of DL PCIE block registers. */
8610 val = grc_mode & ~GRC_MODE_PCIE_PORT_MASK;
8611 tw32(GRC_MODE, val | GRC_MODE_PCIE_DL_SEL);
8612
8613 val = tr32(TG3_PCIE_TLDLPL_PORT +
8614 TG3_PCIE_DL_LO_FTSMAX);
8615 val &= ~TG3_PCIE_DL_LO_FTSMAX_MSK;
8616 tw32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_DL_LO_FTSMAX,
8617 val | TG3_PCIE_DL_LO_FTSMAX_VAL);
8618
8619 tw32(GRC_MODE, grc_mode);
8620 }
8621
Matt Carlsona977dbe2010-04-12 06:58:26 +00008622 val = tr32(TG3_CPMU_LSPD_10MB_CLK);
8623 val &= ~CPMU_LSPD_10MB_MACCLK_MASK;
8624 val |= CPMU_LSPD_10MB_MACCLK_6_25;
8625 tw32(TG3_CPMU_LSPD_10MB_CLK, val);
Matt Carlsoncea46462010-04-12 06:58:24 +00008626 }
8627
Linus Torvalds1da177e2005-04-16 15:20:36 -07008628 /* This works around an issue with Athlon chipsets on
8629 * B3 tigon3 silicon. This bit has no effect on any
8630 * other revision. But do not set this on PCI Express
Matt Carlson795d01c2007-10-07 23:28:17 -07008631 * chips and don't even touch the clocks if the CPMU is present.
Linus Torvalds1da177e2005-04-16 15:20:36 -07008632 */
Joe Perches63c3a662011-04-26 08:12:10 +00008633 if (!tg3_flag(tp, CPMU_PRESENT)) {
8634 if (!tg3_flag(tp, PCI_EXPRESS))
Matt Carlson795d01c2007-10-07 23:28:17 -07008635 tp->pci_clock_ctrl |= CLOCK_CTRL_DELAY_PCI_GRANT;
8636 tw32_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl);
8637 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008638
8639 if (tp->pci_chip_rev_id == CHIPREV_ID_5704_A0 &&
Joe Perches63c3a662011-04-26 08:12:10 +00008640 tg3_flag(tp, PCIX_MODE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008641 val = tr32(TG3PCI_PCISTATE);
8642 val |= PCISTATE_RETRY_SAME_DMA;
8643 tw32(TG3PCI_PCISTATE, val);
8644 }
8645
Joe Perches63c3a662011-04-26 08:12:10 +00008646 if (tg3_flag(tp, ENABLE_APE)) {
Matt Carlson0d3031d2007-10-10 18:02:43 -07008647 /* Allow reads and writes to the
8648 * APE register and memory space.
8649 */
8650 val = tr32(TG3PCI_PCISTATE);
8651 val |= PCISTATE_ALLOW_APE_CTLSPC_WR |
Matt Carlsonf92d9dc12010-06-05 17:24:30 +00008652 PCISTATE_ALLOW_APE_SHMEM_WR |
8653 PCISTATE_ALLOW_APE_PSPACE_WR;
Matt Carlson0d3031d2007-10-10 18:02:43 -07008654 tw32(TG3PCI_PCISTATE, val);
8655 }
8656
Linus Torvalds1da177e2005-04-16 15:20:36 -07008657 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5704_BX) {
8658 /* Enable some hw fixes. */
8659 val = tr32(TG3PCI_MSI_DATA);
8660 val |= (1 << 26) | (1 << 28) | (1 << 29);
8661 tw32(TG3PCI_MSI_DATA, val);
8662 }
8663
8664 /* Descriptor ring init may make accesses to the
8665 * NIC SRAM area to setup the TX descriptors, so we
8666 * can only do this after the hardware has been
8667 * successfully reset.
8668 */
Michael Chan32d8c572006-07-25 16:38:29 -07008669 err = tg3_init_rings(tp);
8670 if (err)
8671 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008672
Joe Perches63c3a662011-04-26 08:12:10 +00008673 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlsoncbf9ca62009-11-13 13:03:40 +00008674 val = tr32(TG3PCI_DMA_RW_CTRL) &
8675 ~DMA_RWCTRL_DIS_CACHE_ALIGNMENT;
Matt Carlson1a319022010-04-12 06:58:25 +00008676 if (tp->pci_chip_rev_id == CHIPREV_ID_57765_A0)
8677 val &= ~DMA_RWCTRL_CRDRDR_RDMA_MRRS_MSK;
Matt Carlson55086ad2011-12-14 11:09:59 +00008678 if (!tg3_flag(tp, 57765_CLASS) &&
Matt Carlson0aebff42011-04-25 12:42:45 +00008679 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717)
8680 val |= DMA_RWCTRL_TAGGED_STAT_WA;
Matt Carlsoncbf9ca62009-11-13 13:03:40 +00008681 tw32(TG3PCI_DMA_RW_CTRL, val | tp->dma_rwctrl);
8682 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5784 &&
8683 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5761) {
Matt Carlsond30cdd22007-10-07 23:28:35 -07008684 /* This value is determined during the probe time DMA
8685 * engine test, tg3_test_dma.
8686 */
8687 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
8688 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008689
8690 tp->grc_mode &= ~(GRC_MODE_HOST_SENDBDS |
8691 GRC_MODE_4X_NIC_SEND_RINGS |
8692 GRC_MODE_NO_TX_PHDR_CSUM |
8693 GRC_MODE_NO_RX_PHDR_CSUM);
8694 tp->grc_mode |= GRC_MODE_HOST_SENDBDS;
Michael Chand2d746f2006-04-06 21:45:39 -07008695
8696 /* Pseudo-header checksum is done by hardware logic and not
8697 * the offload processers, so make the chip do the pseudo-
8698 * header checksums on receive. For transmit it is more
8699 * convenient to do the pseudo-header checksum in software
8700 * as Linux does that on transmit for us in all cases.
8701 */
8702 tp->grc_mode |= GRC_MODE_NO_TX_PHDR_CSUM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008703
8704 tw32(GRC_MODE,
8705 tp->grc_mode |
8706 (GRC_MODE_IRQ_ON_MAC_ATTN | GRC_MODE_HOST_STACKUP));
8707
8708 /* Setup the timer prescalar register. Clock is always 66Mhz. */
8709 val = tr32(GRC_MISC_CFG);
8710 val &= ~0xff;
8711 val |= (65 << GRC_MISC_CFG_PRESCALAR_SHIFT);
8712 tw32(GRC_MISC_CFG, val);
8713
8714 /* Initialize MBUF/DESC pool. */
Joe Perches63c3a662011-04-26 08:12:10 +00008715 if (tg3_flag(tp, 5750_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008716 /* Do nothing. */
8717 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5705) {
8718 tw32(BUFMGR_MB_POOL_ADDR, NIC_SRAM_MBUF_POOL_BASE);
8719 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704)
8720 tw32(BUFMGR_MB_POOL_SIZE, NIC_SRAM_MBUF_POOL_SIZE64);
8721 else
8722 tw32(BUFMGR_MB_POOL_SIZE, NIC_SRAM_MBUF_POOL_SIZE96);
8723 tw32(BUFMGR_DMA_DESC_POOL_ADDR, NIC_SRAM_DMA_DESC_POOL_BASE);
8724 tw32(BUFMGR_DMA_DESC_POOL_SIZE, NIC_SRAM_DMA_DESC_POOL_SIZE);
Joe Perches63c3a662011-04-26 08:12:10 +00008725 } else if (tg3_flag(tp, TSO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008726 int fw_len;
8727
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08008728 fw_len = tp->fw_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008729 fw_len = (fw_len + (0x80 - 1)) & ~(0x80 - 1);
8730 tw32(BUFMGR_MB_POOL_ADDR,
8731 NIC_SRAM_MBUF_POOL_BASE5705 + fw_len);
8732 tw32(BUFMGR_MB_POOL_SIZE,
8733 NIC_SRAM_MBUF_POOL_SIZE5705 - fw_len - 0xa00);
8734 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008735
Michael Chan0f893dc2005-07-25 12:30:38 -07008736 if (tp->dev->mtu <= ETH_DATA_LEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008737 tw32(BUFMGR_MB_RDMA_LOW_WATER,
8738 tp->bufmgr_config.mbuf_read_dma_low_water);
8739 tw32(BUFMGR_MB_MACRX_LOW_WATER,
8740 tp->bufmgr_config.mbuf_mac_rx_low_water);
8741 tw32(BUFMGR_MB_HIGH_WATER,
8742 tp->bufmgr_config.mbuf_high_water);
8743 } else {
8744 tw32(BUFMGR_MB_RDMA_LOW_WATER,
8745 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo);
8746 tw32(BUFMGR_MB_MACRX_LOW_WATER,
8747 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo);
8748 tw32(BUFMGR_MB_HIGH_WATER,
8749 tp->bufmgr_config.mbuf_high_water_jumbo);
8750 }
8751 tw32(BUFMGR_DMA_LOW_WATER,
8752 tp->bufmgr_config.dma_low_water);
8753 tw32(BUFMGR_DMA_HIGH_WATER,
8754 tp->bufmgr_config.dma_high_water);
8755
Matt Carlsond309a462010-09-30 10:34:31 +00008756 val = BUFMGR_MODE_ENABLE | BUFMGR_MODE_ATTN_ENABLE;
8757 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
8758 val |= BUFMGR_MODE_NO_TX_UNDERRUN;
Matt Carlson4d958472011-04-20 07:57:35 +00008759 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
8760 tp->pci_chip_rev_id == CHIPREV_ID_5719_A0 ||
8761 tp->pci_chip_rev_id == CHIPREV_ID_5720_A0)
8762 val |= BUFMGR_MODE_MBLOW_ATTN_ENAB;
Matt Carlsond309a462010-09-30 10:34:31 +00008763 tw32(BUFMGR_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008764 for (i = 0; i < 2000; i++) {
8765 if (tr32(BUFMGR_MODE) & BUFMGR_MODE_ENABLE)
8766 break;
8767 udelay(10);
8768 }
8769 if (i >= 2000) {
Joe Perches05dbe002010-02-17 19:44:19 +00008770 netdev_err(tp->dev, "%s cannot enable BUFMGR\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008771 return -ENODEV;
8772 }
8773
Matt Carlsoneb07a942011-04-20 07:57:36 +00008774 if (tp->pci_chip_rev_id == CHIPREV_ID_5906_A1)
8775 tw32(ISO_PKT_TX, (tr32(ISO_PKT_TX) & ~0x3) | 0x2);
Michael Chanb5d37722006-09-27 16:06:21 -07008776
Matt Carlsoneb07a942011-04-20 07:57:36 +00008777 tg3_setup_rxbd_thresholds(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008778
8779 /* Initialize TG3_BDINFO's at:
8780 * RCVDBDI_STD_BD: standard eth size rx ring
8781 * RCVDBDI_JUMBO_BD: jumbo frame rx ring
8782 * RCVDBDI_MINI_BD: small frame rx ring (??? does not work)
8783 *
8784 * like so:
8785 * TG3_BDINFO_HOST_ADDR: high/low parts of DMA address of ring
8786 * TG3_BDINFO_MAXLEN_FLAGS: (rx max buffer size << 16) |
8787 * ring attribute flags
8788 * TG3_BDINFO_NIC_ADDR: location of descriptors in nic SRAM
8789 *
8790 * Standard receive ring @ NIC_SRAM_RX_BUFFER_DESC, 512 entries.
8791 * Jumbo receive ring @ NIC_SRAM_RX_JUMBO_BUFFER_DESC, 256 entries.
8792 *
8793 * The size of each ring is fixed in the firmware, but the location is
8794 * configurable.
8795 */
8796 tw32(RCVDBDI_STD_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH,
Matt Carlson21f581a2009-08-28 14:00:25 +00008797 ((u64) tpr->rx_std_mapping >> 32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07008798 tw32(RCVDBDI_STD_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW,
Matt Carlson21f581a2009-08-28 14:00:25 +00008799 ((u64) tpr->rx_std_mapping & 0xffffffff));
Joe Perches63c3a662011-04-26 08:12:10 +00008800 if (!tg3_flag(tp, 5717_PLUS))
Matt Carlson87668d32009-11-13 13:03:34 +00008801 tw32(RCVDBDI_STD_BD + TG3_BDINFO_NIC_ADDR,
8802 NIC_SRAM_RX_BUFFER_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008803
Matt Carlsonfdb72b32009-08-28 13:57:12 +00008804 /* Disable the mini ring */
Joe Perches63c3a662011-04-26 08:12:10 +00008805 if (!tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008806 tw32(RCVDBDI_MINI_BD + TG3_BDINFO_MAXLEN_FLAGS,
8807 BDINFO_FLAGS_DISABLED);
8808
Matt Carlsonfdb72b32009-08-28 13:57:12 +00008809 /* Program the jumbo buffer descriptor ring control
8810 * blocks on those devices that have them.
8811 */
Matt Carlsona0512942011-07-27 14:20:54 +00008812 if (tp->pci_chip_rev_id == CHIPREV_ID_5719_A0 ||
Joe Perches63c3a662011-04-26 08:12:10 +00008813 (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008814
Joe Perches63c3a662011-04-26 08:12:10 +00008815 if (tg3_flag(tp, JUMBO_RING_ENABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008816 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH,
Matt Carlson21f581a2009-08-28 14:00:25 +00008817 ((u64) tpr->rx_jmb_mapping >> 32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07008818 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW,
Matt Carlson21f581a2009-08-28 14:00:25 +00008819 ((u64) tpr->rx_jmb_mapping & 0xffffffff));
Matt Carlsonde9f5232011-04-05 14:22:43 +00008820 val = TG3_RX_JMB_RING_SIZE(tp) <<
8821 BDINFO_FLAGS_MAXLEN_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008822 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_MAXLEN_FLAGS,
Matt Carlsonde9f5232011-04-05 14:22:43 +00008823 val | BDINFO_FLAGS_USE_EXT_RECV);
Joe Perches63c3a662011-04-26 08:12:10 +00008824 if (!tg3_flag(tp, USE_JUMBO_BDFLAG) ||
Matt Carlson55086ad2011-12-14 11:09:59 +00008825 tg3_flag(tp, 57765_CLASS))
Matt Carlson87668d32009-11-13 13:03:34 +00008826 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_NIC_ADDR,
8827 NIC_SRAM_RX_JUMBO_BUFFER_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008828 } else {
8829 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_MAXLEN_FLAGS,
8830 BDINFO_FLAGS_DISABLED);
8831 }
8832
Joe Perches63c3a662011-04-26 08:12:10 +00008833 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlsonfa6b2aa2011-11-21 15:01:19 +00008834 val = TG3_RX_STD_RING_SIZE(tp);
Matt Carlson7cb32cf2010-09-30 10:34:36 +00008835 val <<= BDINFO_FLAGS_MAXLEN_SHIFT;
8836 val |= (TG3_RX_STD_DMA_SZ << 2);
8837 } else
Matt Carlson04380d42010-04-12 06:58:29 +00008838 val = TG3_RX_STD_DMA_SZ << BDINFO_FLAGS_MAXLEN_SHIFT;
Matt Carlsonfdb72b32009-08-28 13:57:12 +00008839 } else
Matt Carlsonde9f5232011-04-05 14:22:43 +00008840 val = TG3_RX_STD_MAX_SIZE_5700 << BDINFO_FLAGS_MAXLEN_SHIFT;
Matt Carlsonfdb72b32009-08-28 13:57:12 +00008841
8842 tw32(RCVDBDI_STD_BD + TG3_BDINFO_MAXLEN_FLAGS, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008843
Matt Carlson411da642009-11-13 13:03:46 +00008844 tpr->rx_std_prod_idx = tp->rx_pending;
Matt Carlson66711e62009-11-13 13:03:49 +00008845 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG, tpr->rx_std_prod_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008846
Joe Perches63c3a662011-04-26 08:12:10 +00008847 tpr->rx_jmb_prod_idx =
8848 tg3_flag(tp, JUMBO_RING_ENABLE) ? tp->rx_jumbo_pending : 0;
Matt Carlson66711e62009-11-13 13:03:49 +00008849 tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG, tpr->rx_jmb_prod_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008850
Matt Carlson2d31eca2009-09-01 12:53:31 +00008851 tg3_rings_reset(tp);
8852
Linus Torvalds1da177e2005-04-16 15:20:36 -07008853 /* Initialize MAC address and backoff seed. */
Michael Chan986e0ae2007-05-05 12:10:20 -07008854 __tg3_set_mac_addr(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008855
8856 /* MTU + ethernet header + FCS + optional VLAN tag */
Matt Carlsonf7b493e2009-02-25 14:21:52 +00008857 tw32(MAC_RX_MTU_SIZE,
8858 tp->dev->mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008859
8860 /* The slot time is changed by tg3_setup_phy if we
8861 * run at gigabit with half duplex.
8862 */
Matt Carlsonf2096f92011-04-05 14:22:48 +00008863 val = (2 << TX_LENGTHS_IPG_CRS_SHIFT) |
8864 (6 << TX_LENGTHS_IPG_SHIFT) |
8865 (32 << TX_LENGTHS_SLOT_TIME_SHIFT);
8866
8867 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
8868 val |= tr32(MAC_TX_LENGTHS) &
8869 (TX_LENGTHS_JMB_FRM_LEN_MSK |
8870 TX_LENGTHS_CNT_DWN_VAL_MSK);
8871
8872 tw32(MAC_TX_LENGTHS, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008873
8874 /* Receive rules. */
8875 tw32(MAC_RCV_RULE_CFG, RCV_RULE_CFG_DEFAULT_CLASS);
8876 tw32(RCVLPC_CONFIG, 0x0181);
8877
8878 /* Calculate RDMAC_MODE setting early, we need it to determine
8879 * the RCVLPC_STATE_ENABLE mask.
8880 */
8881 rdmac_mode = (RDMAC_MODE_ENABLE | RDMAC_MODE_TGTABORT_ENAB |
8882 RDMAC_MODE_MSTABORT_ENAB | RDMAC_MODE_PARITYERR_ENAB |
8883 RDMAC_MODE_ADDROFLOW_ENAB | RDMAC_MODE_FIFOOFLOW_ENAB |
8884 RDMAC_MODE_FIFOURUN_ENAB | RDMAC_MODE_FIFOOREAD_ENAB |
8885 RDMAC_MODE_LNGREAD_ENAB);
Michael Chan85e94ce2005-04-21 17:05:28 -07008886
Matt Carlsondeabaac2010-11-24 08:31:50 +00008887 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717)
Matt Carlson0339e4e2010-02-12 14:47:09 +00008888 rdmac_mode |= RDMAC_MODE_MULT_DMA_RD_DIS;
8889
Matt Carlson57e69832008-05-25 23:48:31 -07008890 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson321d32a2008-11-21 17:22:19 -08008891 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
8892 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
Matt Carlsond30cdd22007-10-07 23:28:35 -07008893 rdmac_mode |= RDMAC_MODE_BD_SBD_CRPT_ENAB |
8894 RDMAC_MODE_MBUF_RBD_CRPT_ENAB |
8895 RDMAC_MODE_MBUF_SBD_CRPT_ENAB;
8896
Matt Carlsonc5908932011-03-09 16:58:25 +00008897 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
8898 tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) {
Joe Perches63c3a662011-04-26 08:12:10 +00008899 if (tg3_flag(tp, TSO_CAPABLE) &&
Matt Carlsonc13e3712007-05-05 11:50:04 -07008900 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008901 rdmac_mode |= RDMAC_MODE_FIFO_SIZE_128;
8902 } else if (!(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH) &&
Joe Perches63c3a662011-04-26 08:12:10 +00008903 !tg3_flag(tp, IS_5788)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008904 rdmac_mode |= RDMAC_MODE_FIFO_LONG_BURST;
8905 }
8906 }
8907
Joe Perches63c3a662011-04-26 08:12:10 +00008908 if (tg3_flag(tp, PCI_EXPRESS))
Michael Chan85e94ce2005-04-21 17:05:28 -07008909 rdmac_mode |= RDMAC_MODE_FIFO_LONG_BURST;
8910
Joe Perches63c3a662011-04-26 08:12:10 +00008911 if (tg3_flag(tp, HW_TSO_1) ||
8912 tg3_flag(tp, HW_TSO_2) ||
8913 tg3_flag(tp, HW_TSO_3))
Matt Carlson027455a2008-12-21 20:19:30 -08008914 rdmac_mode |= RDMAC_MODE_IPV4_LSO_EN;
8915
Matt Carlson108a6c12011-05-19 12:12:47 +00008916 if (tg3_flag(tp, 57765_PLUS) ||
Matt Carlsone849cdc2009-11-13 13:03:38 +00008917 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Matt Carlson027455a2008-12-21 20:19:30 -08008918 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
8919 rdmac_mode |= RDMAC_MODE_IPV6_LSO_EN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008920
Matt Carlsonf2096f92011-04-05 14:22:48 +00008921 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
8922 rdmac_mode |= tr32(RDMAC_MODE) & RDMAC_MODE_H2BNC_VLAN_DET;
8923
Matt Carlson41a8a7e2010-09-15 08:59:53 +00008924 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
8925 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
8926 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
8927 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
Joe Perches63c3a662011-04-26 08:12:10 +00008928 tg3_flag(tp, 57765_PLUS)) {
Matt Carlson41a8a7e2010-09-15 08:59:53 +00008929 val = tr32(TG3_RDMA_RSRVCTRL_REG);
Matt Carlsond78b59f2011-04-05 14:22:46 +00008930 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
8931 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
Matt Carlsonb4495ed2011-01-25 15:58:47 +00008932 val &= ~(TG3_RDMA_RSRVCTRL_TXMRGN_MASK |
8933 TG3_RDMA_RSRVCTRL_FIFO_LWM_MASK |
8934 TG3_RDMA_RSRVCTRL_FIFO_HWM_MASK);
8935 val |= TG3_RDMA_RSRVCTRL_TXMRGN_320B |
8936 TG3_RDMA_RSRVCTRL_FIFO_LWM_1_5K |
8937 TG3_RDMA_RSRVCTRL_FIFO_HWM_1_5K;
Matt Carlsonb75cc0e2010-11-24 08:31:46 +00008938 }
Matt Carlson41a8a7e2010-09-15 08:59:53 +00008939 tw32(TG3_RDMA_RSRVCTRL_REG,
8940 val | TG3_RDMA_RSRVCTRL_FIFO_OFLW_FIX);
8941 }
8942
Matt Carlsond78b59f2011-04-05 14:22:46 +00008943 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
8944 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
Matt Carlsond309a462010-09-30 10:34:31 +00008945 val = tr32(TG3_LSO_RD_DMA_CRPTEN_CTRL);
8946 tw32(TG3_LSO_RD_DMA_CRPTEN_CTRL, val |
8947 TG3_LSO_RD_DMA_CRPTEN_CTRL_BLEN_BD_4K |
8948 TG3_LSO_RD_DMA_CRPTEN_CTRL_BLEN_LSO_4K);
8949 }
8950
Linus Torvalds1da177e2005-04-16 15:20:36 -07008951 /* Receive/send statistics. */
Joe Perches63c3a662011-04-26 08:12:10 +00008952 if (tg3_flag(tp, 5750_PLUS)) {
Michael Chan16613942006-06-29 20:15:13 -07008953 val = tr32(RCVLPC_STATS_ENABLE);
8954 val &= ~RCVLPC_STATSENAB_DACK_FIX;
8955 tw32(RCVLPC_STATS_ENABLE, val);
8956 } else if ((rdmac_mode & RDMAC_MODE_FIFO_SIZE_128) &&
Joe Perches63c3a662011-04-26 08:12:10 +00008957 tg3_flag(tp, TSO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008958 val = tr32(RCVLPC_STATS_ENABLE);
8959 val &= ~RCVLPC_STATSENAB_LNGBRST_RFIX;
8960 tw32(RCVLPC_STATS_ENABLE, val);
8961 } else {
8962 tw32(RCVLPC_STATS_ENABLE, 0xffffff);
8963 }
8964 tw32(RCVLPC_STATSCTRL, RCVLPC_STATSCTRL_ENABLE);
8965 tw32(SNDDATAI_STATSENAB, 0xffffff);
8966 tw32(SNDDATAI_STATSCTRL,
8967 (SNDDATAI_SCTRL_ENABLE |
8968 SNDDATAI_SCTRL_FASTUPD));
8969
8970 /* Setup host coalescing engine. */
8971 tw32(HOSTCC_MODE, 0);
8972 for (i = 0; i < 2000; i++) {
8973 if (!(tr32(HOSTCC_MODE) & HOSTCC_MODE_ENABLE))
8974 break;
8975 udelay(10);
8976 }
8977
Michael Chand244c892005-07-05 14:42:33 -07008978 __tg3_set_coalesce(tp, &tp->coal);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008979
Joe Perches63c3a662011-04-26 08:12:10 +00008980 if (!tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008981 /* Status/statistics block address. See tg3_timer,
8982 * the tg3_periodic_fetch_stats call there, and
8983 * tg3_get_stats to see how this works for 5705/5750 chips.
8984 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008985 tw32(HOSTCC_STATS_BLK_HOST_ADDR + TG3_64BIT_REG_HIGH,
8986 ((u64) tp->stats_mapping >> 32));
8987 tw32(HOSTCC_STATS_BLK_HOST_ADDR + TG3_64BIT_REG_LOW,
8988 ((u64) tp->stats_mapping & 0xffffffff));
8989 tw32(HOSTCC_STATS_BLK_NIC_ADDR, NIC_SRAM_STATS_BLK);
Matt Carlson2d31eca2009-09-01 12:53:31 +00008990
Linus Torvalds1da177e2005-04-16 15:20:36 -07008991 tw32(HOSTCC_STATUS_BLK_NIC_ADDR, NIC_SRAM_STATUS_BLK);
Matt Carlson2d31eca2009-09-01 12:53:31 +00008992
8993 /* Clear statistics and status block memory areas */
8994 for (i = NIC_SRAM_STATS_BLK;
8995 i < NIC_SRAM_STATUS_BLK + TG3_HW_STATUS_SIZE;
8996 i += sizeof(u32)) {
8997 tg3_write_mem(tp, i, 0);
8998 udelay(40);
8999 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009000 }
9001
9002 tw32(HOSTCC_MODE, HOSTCC_MODE_ENABLE | tp->coalesce_mode);
9003
9004 tw32(RCVCC_MODE, RCVCC_MODE_ENABLE | RCVCC_MODE_ATTN_ENABLE);
9005 tw32(RCVLPC_MODE, RCVLPC_MODE_ENABLE);
Joe Perches63c3a662011-04-26 08:12:10 +00009006 if (!tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009007 tw32(RCVLSC_MODE, RCVLSC_MODE_ENABLE | RCVLSC_MODE_ATTN_ENABLE);
9008
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009009 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES) {
9010 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chanc94e3942005-09-27 12:12:42 -07009011 /* reset to prevent losing 1st rx packet intermittently */
9012 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
9013 udelay(10);
9014 }
9015
Matt Carlson3bda1252008-08-15 14:08:22 -07009016 tp->mac_mode |= MAC_MODE_TXSTAT_ENABLE | MAC_MODE_RXSTAT_ENABLE |
Matt Carlson9e975cc2011-07-20 10:20:50 +00009017 MAC_MODE_TDE_ENABLE | MAC_MODE_RDE_ENABLE |
9018 MAC_MODE_FHDE_ENABLE;
9019 if (tg3_flag(tp, ENABLE_APE))
9020 tp->mac_mode |= MAC_MODE_APE_TX_EN | MAC_MODE_APE_RX_EN;
Joe Perches63c3a662011-04-26 08:12:10 +00009021 if (!tg3_flag(tp, 5705_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009022 !(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07009023 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700)
9024 tp->mac_mode |= MAC_MODE_LINK_POLARITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009025 tw32_f(MAC_MODE, tp->mac_mode | MAC_MODE_RXSTAT_CLEAR | MAC_MODE_TXSTAT_CLEAR);
9026 udelay(40);
9027
Michael Chan314fba32005-04-21 17:07:04 -07009028 /* tp->grc_local_ctrl is partially set up during tg3_get_invariants().
Joe Perches63c3a662011-04-26 08:12:10 +00009029 * If TG3_FLAG_IS_NIC is zero, we should read the
Michael Chan314fba32005-04-21 17:07:04 -07009030 * register to preserve the GPIO settings for LOMs. The GPIOs,
9031 * whether used as inputs or outputs, are set by boot code after
9032 * reset.
9033 */
Joe Perches63c3a662011-04-26 08:12:10 +00009034 if (!tg3_flag(tp, IS_NIC)) {
Michael Chan314fba32005-04-21 17:07:04 -07009035 u32 gpio_mask;
9036
Michael Chan9d26e212006-12-07 00:21:14 -08009037 gpio_mask = GRC_LCLCTRL_GPIO_OE0 | GRC_LCLCTRL_GPIO_OE1 |
9038 GRC_LCLCTRL_GPIO_OE2 | GRC_LCLCTRL_GPIO_OUTPUT0 |
9039 GRC_LCLCTRL_GPIO_OUTPUT1 | GRC_LCLCTRL_GPIO_OUTPUT2;
Michael Chan3e7d83b2005-04-21 17:10:36 -07009040
9041 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752)
9042 gpio_mask |= GRC_LCLCTRL_GPIO_OE3 |
9043 GRC_LCLCTRL_GPIO_OUTPUT3;
9044
Michael Chanaf36e6b2006-03-23 01:28:06 -08009045 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755)
9046 gpio_mask |= GRC_LCLCTRL_GPIO_UART_SEL;
9047
Gary Zambranoaaf84462007-05-05 11:51:45 -07009048 tp->grc_local_ctrl &= ~gpio_mask;
Michael Chan314fba32005-04-21 17:07:04 -07009049 tp->grc_local_ctrl |= tr32(GRC_LOCAL_CTRL) & gpio_mask;
9050
9051 /* GPIO1 must be driven high for eeprom write protect */
Joe Perches63c3a662011-04-26 08:12:10 +00009052 if (tg3_flag(tp, EEPROM_WRITE_PROT))
Michael Chan9d26e212006-12-07 00:21:14 -08009053 tp->grc_local_ctrl |= (GRC_LCLCTRL_GPIO_OE1 |
9054 GRC_LCLCTRL_GPIO_OUTPUT1);
Michael Chan314fba32005-04-21 17:07:04 -07009055 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009056 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
9057 udelay(100);
9058
Matt Carlsonc3b50032012-01-17 15:27:23 +00009059 if (tg3_flag(tp, USING_MSIX)) {
Matt Carlsonbaf8a942009-09-01 13:13:00 +00009060 val = tr32(MSGINT_MODE);
Matt Carlsonc3b50032012-01-17 15:27:23 +00009061 val |= MSGINT_MODE_ENABLE;
9062 if (tp->irq_cnt > 1)
9063 val |= MSGINT_MODE_MULTIVEC_EN;
Matt Carlson5b39de92011-08-31 11:44:50 +00009064 if (!tg3_flag(tp, 1SHOT_MSI))
9065 val |= MSGINT_MODE_ONE_SHOT_DISABLE;
Matt Carlsonbaf8a942009-09-01 13:13:00 +00009066 tw32(MSGINT_MODE, val);
9067 }
9068
Joe Perches63c3a662011-04-26 08:12:10 +00009069 if (!tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009070 tw32_f(DMAC_MODE, DMAC_MODE_ENABLE);
9071 udelay(40);
9072 }
9073
9074 val = (WDMAC_MODE_ENABLE | WDMAC_MODE_TGTABORT_ENAB |
9075 WDMAC_MODE_MSTABORT_ENAB | WDMAC_MODE_PARITYERR_ENAB |
9076 WDMAC_MODE_ADDROFLOW_ENAB | WDMAC_MODE_FIFOOFLOW_ENAB |
9077 WDMAC_MODE_FIFOURUN_ENAB | WDMAC_MODE_FIFOOREAD_ENAB |
9078 WDMAC_MODE_LNGREAD_ENAB);
9079
Matt Carlsonc5908932011-03-09 16:58:25 +00009080 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
9081 tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) {
Joe Perches63c3a662011-04-26 08:12:10 +00009082 if (tg3_flag(tp, TSO_CAPABLE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07009083 (tp->pci_chip_rev_id == CHIPREV_ID_5705_A1 ||
9084 tp->pci_chip_rev_id == CHIPREV_ID_5705_A2)) {
9085 /* nothing */
9086 } else if (!(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH) &&
Joe Perches63c3a662011-04-26 08:12:10 +00009087 !tg3_flag(tp, IS_5788)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009088 val |= WDMAC_MODE_RX_ACCEL;
9089 }
9090 }
9091
Michael Chand9ab5ad2006-03-20 22:27:35 -08009092 /* Enable host coalescing bug fix */
Joe Perches63c3a662011-04-26 08:12:10 +00009093 if (tg3_flag(tp, 5755_PLUS))
Matt Carlsonf51f3562008-05-25 23:45:08 -07009094 val |= WDMAC_MODE_STATUS_TAG_FIX;
Michael Chand9ab5ad2006-03-20 22:27:35 -08009095
Matt Carlson788a0352009-11-02 14:26:03 +00009096 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
9097 val |= WDMAC_MODE_BURST_ALL_DATA;
9098
Linus Torvalds1da177e2005-04-16 15:20:36 -07009099 tw32_f(WDMAC_MODE, val);
9100 udelay(40);
9101
Joe Perches63c3a662011-04-26 08:12:10 +00009102 if (tg3_flag(tp, PCIX_MODE)) {
Matt Carlson9974a352007-10-07 23:27:28 -07009103 u16 pcix_cmd;
9104
9105 pci_read_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
9106 &pcix_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009107 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703) {
Matt Carlson9974a352007-10-07 23:27:28 -07009108 pcix_cmd &= ~PCI_X_CMD_MAX_READ;
9109 pcix_cmd |= PCI_X_CMD_READ_2K;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009110 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
Matt Carlson9974a352007-10-07 23:27:28 -07009111 pcix_cmd &= ~(PCI_X_CMD_MAX_SPLIT | PCI_X_CMD_MAX_READ);
9112 pcix_cmd |= PCI_X_CMD_READ_2K;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009113 }
Matt Carlson9974a352007-10-07 23:27:28 -07009114 pci_write_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
9115 pcix_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009116 }
9117
9118 tw32_f(RDMAC_MODE, rdmac_mode);
9119 udelay(40);
9120
9121 tw32(RCVDCC_MODE, RCVDCC_MODE_ENABLE | RCVDCC_MODE_ATTN_ENABLE);
Joe Perches63c3a662011-04-26 08:12:10 +00009122 if (!tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009123 tw32(MBFREE_MODE, MBFREE_MODE_ENABLE);
Matt Carlson9936bcf2007-10-10 18:03:07 -07009124
9125 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
9126 tw32(SNDDATAC_MODE,
9127 SNDDATAC_MODE_ENABLE | SNDDATAC_MODE_CDELAY);
9128 else
9129 tw32(SNDDATAC_MODE, SNDDATAC_MODE_ENABLE);
9130
Linus Torvalds1da177e2005-04-16 15:20:36 -07009131 tw32(SNDBDC_MODE, SNDBDC_MODE_ENABLE | SNDBDC_MODE_ATTN_ENABLE);
9132 tw32(RCVBDI_MODE, RCVBDI_MODE_ENABLE | RCVBDI_MODE_RCB_ATTN_ENAB);
Matt Carlson7cb32cf2010-09-30 10:34:36 +00009133 val = RCVDBDI_MODE_ENABLE | RCVDBDI_MODE_INV_RING_SZ;
Joe Perches63c3a662011-04-26 08:12:10 +00009134 if (tg3_flag(tp, LRG_PROD_RING_CAP))
Matt Carlson7cb32cf2010-09-30 10:34:36 +00009135 val |= RCVDBDI_MODE_LRG_RING_SZ;
9136 tw32(RCVDBDI_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009137 tw32(SNDDATAI_MODE, SNDDATAI_MODE_ENABLE);
Joe Perches63c3a662011-04-26 08:12:10 +00009138 if (tg3_flag(tp, HW_TSO_1) ||
9139 tg3_flag(tp, HW_TSO_2) ||
9140 tg3_flag(tp, HW_TSO_3))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009141 tw32(SNDDATAI_MODE, SNDDATAI_MODE_ENABLE | 0x8);
Matt Carlsonbaf8a942009-09-01 13:13:00 +00009142 val = SNDBDI_MODE_ENABLE | SNDBDI_MODE_ATTN_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00009143 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlsonbaf8a942009-09-01 13:13:00 +00009144 val |= SNDBDI_MODE_MULTI_TXQ_EN;
9145 tw32(SNDBDI_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009146 tw32(SNDBDS_MODE, SNDBDS_MODE_ENABLE | SNDBDS_MODE_ATTN_ENABLE);
9147
9148 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0) {
9149 err = tg3_load_5701_a0_firmware_fix(tp);
9150 if (err)
9151 return err;
9152 }
9153
Joe Perches63c3a662011-04-26 08:12:10 +00009154 if (tg3_flag(tp, TSO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009155 err = tg3_load_tso_firmware(tp);
9156 if (err)
9157 return err;
9158 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009159
9160 tp->tx_mode = TX_MODE_ENABLE;
Matt Carlsonf2096f92011-04-05 14:22:48 +00009161
Joe Perches63c3a662011-04-26 08:12:10 +00009162 if (tg3_flag(tp, 5755_PLUS) ||
Matt Carlsonb1d05212010-06-05 17:24:31 +00009163 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
9164 tp->tx_mode |= TX_MODE_MBUF_LOCKUP_FIX;
Matt Carlsonf2096f92011-04-05 14:22:48 +00009165
9166 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
9167 val = TX_MODE_JMB_FRM_LEN | TX_MODE_CNT_DN_MODE;
9168 tp->tx_mode &= ~val;
9169 tp->tx_mode |= tr32(MAC_TX_MODE) & val;
9170 }
9171
Linus Torvalds1da177e2005-04-16 15:20:36 -07009172 tw32_f(MAC_TX_MODE, tp->tx_mode);
9173 udelay(100);
9174
Joe Perches63c3a662011-04-26 08:12:10 +00009175 if (tg3_flag(tp, ENABLE_RSS)) {
Matt Carlsonbcebcc42011-12-14 11:10:01 +00009176 tg3_rss_write_indir_tbl(tp);
Matt Carlsonbaf8a942009-09-01 13:13:00 +00009177
9178 /* Setup the "secret" hash key. */
9179 tw32(MAC_RSS_HASH_KEY_0, 0x5f865437);
9180 tw32(MAC_RSS_HASH_KEY_1, 0xe4ac62cc);
9181 tw32(MAC_RSS_HASH_KEY_2, 0x50103a45);
9182 tw32(MAC_RSS_HASH_KEY_3, 0x36621985);
9183 tw32(MAC_RSS_HASH_KEY_4, 0xbf14c0e8);
9184 tw32(MAC_RSS_HASH_KEY_5, 0x1bc27a1e);
9185 tw32(MAC_RSS_HASH_KEY_6, 0x84f4b556);
9186 tw32(MAC_RSS_HASH_KEY_7, 0x094ea6fe);
9187 tw32(MAC_RSS_HASH_KEY_8, 0x7dda01e7);
9188 tw32(MAC_RSS_HASH_KEY_9, 0xc04d7481);
9189 }
9190
Linus Torvalds1da177e2005-04-16 15:20:36 -07009191 tp->rx_mode = RX_MODE_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00009192 if (tg3_flag(tp, 5755_PLUS))
Michael Chanaf36e6b2006-03-23 01:28:06 -08009193 tp->rx_mode |= RX_MODE_IPV6_CSUM_ENABLE;
9194
Joe Perches63c3a662011-04-26 08:12:10 +00009195 if (tg3_flag(tp, ENABLE_RSS))
Matt Carlsonbaf8a942009-09-01 13:13:00 +00009196 tp->rx_mode |= RX_MODE_RSS_ENABLE |
9197 RX_MODE_RSS_ITBL_HASH_BITS_7 |
9198 RX_MODE_RSS_IPV6_HASH_EN |
9199 RX_MODE_RSS_TCP_IPV6_HASH_EN |
9200 RX_MODE_RSS_IPV4_HASH_EN |
9201 RX_MODE_RSS_TCP_IPV4_HASH_EN;
9202
Linus Torvalds1da177e2005-04-16 15:20:36 -07009203 tw32_f(MAC_RX_MODE, tp->rx_mode);
9204 udelay(10);
9205
Linus Torvalds1da177e2005-04-16 15:20:36 -07009206 tw32(MAC_LED_CTRL, tp->led_ctrl);
9207
9208 tw32(MAC_MI_STAT, MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009209 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009210 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
9211 udelay(10);
9212 }
9213 tw32_f(MAC_RX_MODE, tp->rx_mode);
9214 udelay(10);
9215
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009216 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009217 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009218 !(tp->phy_flags & TG3_PHYFLG_SERDES_PREEMPHASIS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009219 /* Set drive transmission level to 1.2V */
9220 /* only if the signal pre-emphasis bit is not set */
9221 val = tr32(MAC_SERDES_CFG);
9222 val &= 0xfffff000;
9223 val |= 0x880;
9224 tw32(MAC_SERDES_CFG, val);
9225 }
9226 if (tp->pci_chip_rev_id == CHIPREV_ID_5703_A1)
9227 tw32(MAC_SERDES_CFG, 0x616000);
9228 }
9229
9230 /* Prevent chip from dropping frames when flow control
9231 * is enabled.
9232 */
Matt Carlson55086ad2011-12-14 11:09:59 +00009233 if (tg3_flag(tp, 57765_CLASS))
Matt Carlson666bc832010-01-20 16:58:03 +00009234 val = 1;
9235 else
9236 val = 2;
9237 tw32_f(MAC_LOW_WMARK_MAX_RX_FRAME, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009238
9239 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009240 (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009241 /* Use hardware link auto-negotiation */
Joe Perches63c3a662011-04-26 08:12:10 +00009242 tg3_flag_set(tp, HW_AUTONEG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009243 }
9244
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009245 if ((tp->phy_flags & TG3_PHYFLG_MII_SERDES) &&
Matt Carlson6ff6f812011-05-19 12:12:54 +00009246 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
Michael Chand4d2c552006-03-20 17:47:20 -08009247 u32 tmp;
9248
9249 tmp = tr32(SERDES_RX_CTRL);
9250 tw32(SERDES_RX_CTRL, tmp | SERDES_RX_SIG_DETECT);
9251 tp->grc_local_ctrl &= ~GRC_LCLCTRL_USE_EXT_SIG_DETECT;
9252 tp->grc_local_ctrl |= GRC_LCLCTRL_USE_SIG_DETECT;
9253 tw32(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
9254 }
9255
Joe Perches63c3a662011-04-26 08:12:10 +00009256 if (!tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson800960682010-08-02 11:26:06 +00009257 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) {
9258 tp->phy_flags &= ~TG3_PHYFLG_IS_LOW_POWER;
Matt Carlsondd477002008-05-25 23:45:58 -07009259 tp->link_config.speed = tp->link_config.orig_speed;
9260 tp->link_config.duplex = tp->link_config.orig_duplex;
9261 tp->link_config.autoneg = tp->link_config.orig_autoneg;
9262 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009263
Matt Carlsondd477002008-05-25 23:45:58 -07009264 err = tg3_setup_phy(tp, 0);
9265 if (err)
9266 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009267
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009268 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
9269 !(tp->phy_flags & TG3_PHYFLG_IS_FET)) {
Matt Carlsondd477002008-05-25 23:45:58 -07009270 u32 tmp;
9271
9272 /* Clear CRC stats. */
9273 if (!tg3_readphy(tp, MII_TG3_TEST1, &tmp)) {
9274 tg3_writephy(tp, MII_TG3_TEST1,
9275 tmp | MII_TG3_TEST1_CRC_EN);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00009276 tg3_readphy(tp, MII_TG3_RXR_COUNTERS, &tmp);
Matt Carlsondd477002008-05-25 23:45:58 -07009277 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009278 }
9279 }
9280
9281 __tg3_set_rx_mode(tp->dev);
9282
9283 /* Initialize receive rules. */
9284 tw32(MAC_RCV_RULE_0, 0xc2000000 & RCV_RULE_DISABLE_MASK);
9285 tw32(MAC_RCV_VALUE_0, 0xffffffff & RCV_RULE_DISABLE_MASK);
9286 tw32(MAC_RCV_RULE_1, 0x86000004 & RCV_RULE_DISABLE_MASK);
9287 tw32(MAC_RCV_VALUE_1, 0xffffffff & RCV_RULE_DISABLE_MASK);
9288
Joe Perches63c3a662011-04-26 08:12:10 +00009289 if (tg3_flag(tp, 5705_PLUS) && !tg3_flag(tp, 5780_CLASS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009290 limit = 8;
9291 else
9292 limit = 16;
Joe Perches63c3a662011-04-26 08:12:10 +00009293 if (tg3_flag(tp, ENABLE_ASF))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009294 limit -= 4;
9295 switch (limit) {
9296 case 16:
9297 tw32(MAC_RCV_RULE_15, 0); tw32(MAC_RCV_VALUE_15, 0);
9298 case 15:
9299 tw32(MAC_RCV_RULE_14, 0); tw32(MAC_RCV_VALUE_14, 0);
9300 case 14:
9301 tw32(MAC_RCV_RULE_13, 0); tw32(MAC_RCV_VALUE_13, 0);
9302 case 13:
9303 tw32(MAC_RCV_RULE_12, 0); tw32(MAC_RCV_VALUE_12, 0);
9304 case 12:
9305 tw32(MAC_RCV_RULE_11, 0); tw32(MAC_RCV_VALUE_11, 0);
9306 case 11:
9307 tw32(MAC_RCV_RULE_10, 0); tw32(MAC_RCV_VALUE_10, 0);
9308 case 10:
9309 tw32(MAC_RCV_RULE_9, 0); tw32(MAC_RCV_VALUE_9, 0);
9310 case 9:
9311 tw32(MAC_RCV_RULE_8, 0); tw32(MAC_RCV_VALUE_8, 0);
9312 case 8:
9313 tw32(MAC_RCV_RULE_7, 0); tw32(MAC_RCV_VALUE_7, 0);
9314 case 7:
9315 tw32(MAC_RCV_RULE_6, 0); tw32(MAC_RCV_VALUE_6, 0);
9316 case 6:
9317 tw32(MAC_RCV_RULE_5, 0); tw32(MAC_RCV_VALUE_5, 0);
9318 case 5:
9319 tw32(MAC_RCV_RULE_4, 0); tw32(MAC_RCV_VALUE_4, 0);
9320 case 4:
9321 /* tw32(MAC_RCV_RULE_3, 0); tw32(MAC_RCV_VALUE_3, 0); */
9322 case 3:
9323 /* tw32(MAC_RCV_RULE_2, 0); tw32(MAC_RCV_VALUE_2, 0); */
9324 case 2:
9325 case 1:
9326
9327 default:
9328 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07009329 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009330
Joe Perches63c3a662011-04-26 08:12:10 +00009331 if (tg3_flag(tp, ENABLE_APE))
Matt Carlson9ce768e2007-10-11 19:49:11 -07009332 /* Write our heartbeat update interval to APE. */
9333 tg3_ape_write32(tp, TG3_APE_HOST_HEARTBEAT_INT_MS,
9334 APE_HOST_HEARTBEAT_INT_DISABLE);
Matt Carlson0d3031d2007-10-10 18:02:43 -07009335
Linus Torvalds1da177e2005-04-16 15:20:36 -07009336 tg3_write_sig_post_reset(tp, RESET_KIND_INIT);
9337
Linus Torvalds1da177e2005-04-16 15:20:36 -07009338 return 0;
9339}
9340
9341/* Called at device open time to get the chip ready for
9342 * packet processing. Invoked with tp->lock held.
9343 */
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07009344static int tg3_init_hw(struct tg3 *tp, int reset_phy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009345{
Linus Torvalds1da177e2005-04-16 15:20:36 -07009346 tg3_switch_clocks(tp);
9347
9348 tw32(TG3PCI_MEM_WIN_BASE_ADDR, 0);
9349
Matt Carlson2f751b62008-08-04 23:17:34 -07009350 return tg3_reset_hw(tp, reset_phy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009351}
9352
Matt Carlsonebf33122012-02-13 10:20:05 +00009353/* Restart hardware after configuration changes, self-test, etc.
9354 * Invoked with tp->lock held.
9355 */
9356static int tg3_restart_hw(struct tg3 *tp, int reset_phy)
9357 __releases(tp->lock)
9358 __acquires(tp->lock)
9359{
9360 int err;
9361
9362 err = tg3_init_hw(tp, reset_phy);
9363 if (err) {
9364 netdev_err(tp->dev,
9365 "Failed to re-initialize device, aborting\n");
9366 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
9367 tg3_full_unlock(tp);
9368 del_timer_sync(&tp->timer);
9369 tp->irq_sync = 0;
9370 tg3_napi_enable(tp);
9371 dev_close(tp->dev);
9372 tg3_full_lock(tp, 0);
9373 }
9374 return err;
9375}
9376
Matt Carlson9a21fb82012-02-13 10:20:06 +00009377static void tg3_reset_task(struct work_struct *work)
9378{
9379 struct tg3 *tp = container_of(work, struct tg3, reset_task);
9380 int err;
9381
9382 tg3_full_lock(tp, 0);
9383
9384 if (!netif_running(tp->dev)) {
9385 tg3_flag_clear(tp, RESET_TASK_PENDING);
9386 tg3_full_unlock(tp);
9387 return;
9388 }
9389
9390 tg3_full_unlock(tp);
9391
9392 tg3_phy_stop(tp);
9393
9394 tg3_netif_stop(tp);
9395
9396 tg3_full_lock(tp, 1);
9397
9398 if (tg3_flag(tp, TX_RECOVERY_PENDING)) {
9399 tp->write32_tx_mbox = tg3_write32_tx_mbox;
9400 tp->write32_rx_mbox = tg3_write_flush_reg32;
9401 tg3_flag_set(tp, MBOX_WRITE_REORDER);
9402 tg3_flag_clear(tp, TX_RECOVERY_PENDING);
9403 }
9404
9405 tg3_halt(tp, RESET_KIND_SHUTDOWN, 0);
9406 err = tg3_init_hw(tp, 1);
9407 if (err)
9408 goto out;
9409
9410 tg3_netif_start(tp);
9411
9412out:
9413 tg3_full_unlock(tp);
9414
9415 if (!err)
9416 tg3_phy_start(tp);
9417
9418 tg3_flag_clear(tp, RESET_TASK_PENDING);
9419}
9420
Linus Torvalds1da177e2005-04-16 15:20:36 -07009421#define TG3_STAT_ADD32(PSTAT, REG) \
9422do { u32 __val = tr32(REG); \
9423 (PSTAT)->low += __val; \
9424 if ((PSTAT)->low < __val) \
9425 (PSTAT)->high += 1; \
9426} while (0)
9427
9428static void tg3_periodic_fetch_stats(struct tg3 *tp)
9429{
9430 struct tg3_hw_stats *sp = tp->hw_stats;
9431
9432 if (!netif_carrier_ok(tp->dev))
9433 return;
9434
9435 TG3_STAT_ADD32(&sp->tx_octets, MAC_TX_STATS_OCTETS);
9436 TG3_STAT_ADD32(&sp->tx_collisions, MAC_TX_STATS_COLLISIONS);
9437 TG3_STAT_ADD32(&sp->tx_xon_sent, MAC_TX_STATS_XON_SENT);
9438 TG3_STAT_ADD32(&sp->tx_xoff_sent, MAC_TX_STATS_XOFF_SENT);
9439 TG3_STAT_ADD32(&sp->tx_mac_errors, MAC_TX_STATS_MAC_ERRORS);
9440 TG3_STAT_ADD32(&sp->tx_single_collisions, MAC_TX_STATS_SINGLE_COLLISIONS);
9441 TG3_STAT_ADD32(&sp->tx_mult_collisions, MAC_TX_STATS_MULT_COLLISIONS);
9442 TG3_STAT_ADD32(&sp->tx_deferred, MAC_TX_STATS_DEFERRED);
9443 TG3_STAT_ADD32(&sp->tx_excessive_collisions, MAC_TX_STATS_EXCESSIVE_COL);
9444 TG3_STAT_ADD32(&sp->tx_late_collisions, MAC_TX_STATS_LATE_COL);
9445 TG3_STAT_ADD32(&sp->tx_ucast_packets, MAC_TX_STATS_UCAST);
9446 TG3_STAT_ADD32(&sp->tx_mcast_packets, MAC_TX_STATS_MCAST);
9447 TG3_STAT_ADD32(&sp->tx_bcast_packets, MAC_TX_STATS_BCAST);
9448
9449 TG3_STAT_ADD32(&sp->rx_octets, MAC_RX_STATS_OCTETS);
9450 TG3_STAT_ADD32(&sp->rx_fragments, MAC_RX_STATS_FRAGMENTS);
9451 TG3_STAT_ADD32(&sp->rx_ucast_packets, MAC_RX_STATS_UCAST);
9452 TG3_STAT_ADD32(&sp->rx_mcast_packets, MAC_RX_STATS_MCAST);
9453 TG3_STAT_ADD32(&sp->rx_bcast_packets, MAC_RX_STATS_BCAST);
9454 TG3_STAT_ADD32(&sp->rx_fcs_errors, MAC_RX_STATS_FCS_ERRORS);
9455 TG3_STAT_ADD32(&sp->rx_align_errors, MAC_RX_STATS_ALIGN_ERRORS);
9456 TG3_STAT_ADD32(&sp->rx_xon_pause_rcvd, MAC_RX_STATS_XON_PAUSE_RECVD);
9457 TG3_STAT_ADD32(&sp->rx_xoff_pause_rcvd, MAC_RX_STATS_XOFF_PAUSE_RECVD);
9458 TG3_STAT_ADD32(&sp->rx_mac_ctrl_rcvd, MAC_RX_STATS_MAC_CTRL_RECVD);
9459 TG3_STAT_ADD32(&sp->rx_xoff_entered, MAC_RX_STATS_XOFF_ENTERED);
9460 TG3_STAT_ADD32(&sp->rx_frame_too_long_errors, MAC_RX_STATS_FRAME_TOO_LONG);
9461 TG3_STAT_ADD32(&sp->rx_jabbers, MAC_RX_STATS_JABBERS);
9462 TG3_STAT_ADD32(&sp->rx_undersize_packets, MAC_RX_STATS_UNDERSIZE);
Michael Chan463d3052006-05-22 16:36:27 -07009463
9464 TG3_STAT_ADD32(&sp->rxbds_empty, RCVLPC_NO_RCV_BD_CNT);
Matt Carlson310050f2011-05-19 12:12:55 +00009465 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717 &&
9466 tp->pci_chip_rev_id != CHIPREV_ID_5719_A0 &&
9467 tp->pci_chip_rev_id != CHIPREV_ID_5720_A0) {
Matt Carlson4d958472011-04-20 07:57:35 +00009468 TG3_STAT_ADD32(&sp->rx_discards, RCVLPC_IN_DISCARDS_CNT);
9469 } else {
9470 u32 val = tr32(HOSTCC_FLOW_ATTN);
9471 val = (val & HOSTCC_FLOW_ATTN_MBUF_LWM) ? 1 : 0;
9472 if (val) {
9473 tw32(HOSTCC_FLOW_ATTN, HOSTCC_FLOW_ATTN_MBUF_LWM);
9474 sp->rx_discards.low += val;
9475 if (sp->rx_discards.low < val)
9476 sp->rx_discards.high += 1;
9477 }
9478 sp->mbuf_lwm_thresh_hit = sp->rx_discards;
9479 }
Michael Chan463d3052006-05-22 16:36:27 -07009480 TG3_STAT_ADD32(&sp->rx_errors, RCVLPC_IN_ERRORS_CNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009481}
9482
Matt Carlson0e6cf6a2011-06-13 13:38:55 +00009483static void tg3_chk_missed_msi(struct tg3 *tp)
9484{
9485 u32 i;
9486
9487 for (i = 0; i < tp->irq_cnt; i++) {
9488 struct tg3_napi *tnapi = &tp->napi[i];
9489
9490 if (tg3_has_work(tnapi)) {
9491 if (tnapi->last_rx_cons == tnapi->rx_rcb_ptr &&
9492 tnapi->last_tx_cons == tnapi->tx_cons) {
9493 if (tnapi->chk_msi_cnt < 1) {
9494 tnapi->chk_msi_cnt++;
9495 return;
9496 }
Matt Carlson7f230732011-08-31 11:44:48 +00009497 tg3_msi(0, tnapi);
Matt Carlson0e6cf6a2011-06-13 13:38:55 +00009498 }
9499 }
9500 tnapi->chk_msi_cnt = 0;
9501 tnapi->last_rx_cons = tnapi->rx_rcb_ptr;
9502 tnapi->last_tx_cons = tnapi->tx_cons;
9503 }
9504}
9505
Linus Torvalds1da177e2005-04-16 15:20:36 -07009506static void tg3_timer(unsigned long __opaque)
9507{
9508 struct tg3 *tp = (struct tg3 *) __opaque;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009509
Matt Carlson5b190622011-11-04 09:15:04 +00009510 if (tp->irq_sync || tg3_flag(tp, RESET_TASK_PENDING))
Michael Chanf475f162006-03-27 23:20:14 -08009511 goto restart_timer;
9512
David S. Millerf47c11e2005-06-24 20:18:35 -07009513 spin_lock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009514
Matt Carlson0e6cf6a2011-06-13 13:38:55 +00009515 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
Matt Carlson55086ad2011-12-14 11:09:59 +00009516 tg3_flag(tp, 57765_CLASS))
Matt Carlson0e6cf6a2011-06-13 13:38:55 +00009517 tg3_chk_missed_msi(tp);
9518
Joe Perches63c3a662011-04-26 08:12:10 +00009519 if (!tg3_flag(tp, TAGGED_STATUS)) {
David S. Millerfac9b832005-05-18 22:46:34 -07009520 /* All of this garbage is because when using non-tagged
9521 * IRQ status the mailbox/status_block protocol the chip
9522 * uses with the cpu is race prone.
9523 */
Matt Carlson898a56f2009-08-28 14:02:40 +00009524 if (tp->napi[0].hw_status->status & SD_STATUS_UPDATED) {
David S. Millerfac9b832005-05-18 22:46:34 -07009525 tw32(GRC_LOCAL_CTRL,
9526 tp->grc_local_ctrl | GRC_LCLCTRL_SETINT);
9527 } else {
9528 tw32(HOSTCC_MODE, tp->coalesce_mode |
Matt Carlsonfd2ce372009-09-01 12:51:13 +00009529 HOSTCC_MODE_ENABLE | HOSTCC_MODE_NOW);
David S. Millerfac9b832005-05-18 22:46:34 -07009530 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009531
David S. Millerfac9b832005-05-18 22:46:34 -07009532 if (!(tr32(WDMAC_MODE) & WDMAC_MODE_ENABLE)) {
David S. Millerf47c11e2005-06-24 20:18:35 -07009533 spin_unlock(&tp->lock);
Matt Carlsondb219972011-11-04 09:15:03 +00009534 tg3_reset_task_schedule(tp);
Matt Carlson5b190622011-11-04 09:15:04 +00009535 goto restart_timer;
David S. Millerfac9b832005-05-18 22:46:34 -07009536 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009537 }
9538
Linus Torvalds1da177e2005-04-16 15:20:36 -07009539 /* This part only runs once per second. */
9540 if (!--tp->timer_counter) {
Joe Perches63c3a662011-04-26 08:12:10 +00009541 if (tg3_flag(tp, 5705_PLUS))
David S. Millerfac9b832005-05-18 22:46:34 -07009542 tg3_periodic_fetch_stats(tp);
9543
Matt Carlsonb0c59432011-05-19 12:12:48 +00009544 if (tp->setlpicnt && !--tp->setlpicnt)
9545 tg3_phy_eee_enable(tp);
Matt Carlson52b02d02010-10-14 10:37:41 +00009546
Joe Perches63c3a662011-04-26 08:12:10 +00009547 if (tg3_flag(tp, USE_LINKCHG_REG)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009548 u32 mac_stat;
9549 int phy_event;
9550
9551 mac_stat = tr32(MAC_STATUS);
9552
9553 phy_event = 0;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009554 if (tp->phy_flags & TG3_PHYFLG_USE_MI_INTERRUPT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009555 if (mac_stat & MAC_STATUS_MI_INTERRUPT)
9556 phy_event = 1;
9557 } else if (mac_stat & MAC_STATUS_LNKSTATE_CHANGED)
9558 phy_event = 1;
9559
9560 if (phy_event)
9561 tg3_setup_phy(tp, 0);
Joe Perches63c3a662011-04-26 08:12:10 +00009562 } else if (tg3_flag(tp, POLL_SERDES)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009563 u32 mac_stat = tr32(MAC_STATUS);
9564 int need_setup = 0;
9565
9566 if (netif_carrier_ok(tp->dev) &&
9567 (mac_stat & MAC_STATUS_LNKSTATE_CHANGED)) {
9568 need_setup = 1;
9569 }
Matt Carlsonbe98da62010-07-11 09:31:46 +00009570 if (!netif_carrier_ok(tp->dev) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07009571 (mac_stat & (MAC_STATUS_PCS_SYNCED |
9572 MAC_STATUS_SIGNAL_DET))) {
9573 need_setup = 1;
9574 }
9575 if (need_setup) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07009576 if (!tp->serdes_counter) {
9577 tw32_f(MAC_MODE,
9578 (tp->mac_mode &
9579 ~MAC_MODE_PORT_MODE_MASK));
9580 udelay(40);
9581 tw32_f(MAC_MODE, tp->mac_mode);
9582 udelay(40);
9583 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009584 tg3_setup_phy(tp, 0);
9585 }
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009586 } else if ((tp->phy_flags & TG3_PHYFLG_MII_SERDES) &&
Joe Perches63c3a662011-04-26 08:12:10 +00009587 tg3_flag(tp, 5780_CLASS)) {
Michael Chan747e8f82005-07-25 12:33:22 -07009588 tg3_serdes_parallel_detect(tp);
Matt Carlson57d8b882010-06-05 17:24:35 +00009589 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009590
9591 tp->timer_counter = tp->timer_multiplier;
9592 }
9593
Michael Chan130b8e42006-09-27 16:00:40 -07009594 /* Heartbeat is only sent once every 2 seconds.
9595 *
9596 * The heartbeat is to tell the ASF firmware that the host
9597 * driver is still alive. In the event that the OS crashes,
9598 * ASF needs to reset the hardware to free up the FIFO space
9599 * that may be filled with rx packets destined for the host.
9600 * If the FIFO is full, ASF will no longer function properly.
9601 *
9602 * Unintended resets have been reported on real time kernels
9603 * where the timer doesn't run on time. Netpoll will also have
9604 * same problem.
9605 *
9606 * The new FWCMD_NICDRV_ALIVE3 command tells the ASF firmware
9607 * to check the ring condition when the heartbeat is expiring
9608 * before doing the reset. This will prevent most unintended
9609 * resets.
9610 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07009611 if (!--tp->asf_counter) {
Joe Perches63c3a662011-04-26 08:12:10 +00009612 if (tg3_flag(tp, ENABLE_ASF) && !tg3_flag(tp, ENABLE_APE)) {
Matt Carlson7c5026a2008-05-02 16:49:29 -07009613 tg3_wait_for_event_ack(tp);
9614
Michael Chanbbadf502006-04-06 21:46:34 -07009615 tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX,
Michael Chan130b8e42006-09-27 16:00:40 -07009616 FWCMD_NICDRV_ALIVE3);
Michael Chanbbadf502006-04-06 21:46:34 -07009617 tg3_write_mem(tp, NIC_SRAM_FW_CMD_LEN_MBOX, 4);
Matt Carlsonc6cdf432010-04-05 10:19:26 +00009618 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX,
9619 TG3_FW_UPDATE_TIMEOUT_SEC);
Matt Carlson4ba526c2008-08-15 14:10:04 -07009620
9621 tg3_generate_fw_event(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009622 }
9623 tp->asf_counter = tp->asf_multiplier;
9624 }
9625
David S. Millerf47c11e2005-06-24 20:18:35 -07009626 spin_unlock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009627
Michael Chanf475f162006-03-27 23:20:14 -08009628restart_timer:
Linus Torvalds1da177e2005-04-16 15:20:36 -07009629 tp->timer.expires = jiffies + tp->timer_offset;
9630 add_timer(&tp->timer);
9631}
9632
Matt Carlson4f125f42009-09-01 12:55:02 +00009633static int tg3_request_irq(struct tg3 *tp, int irq_num)
Michael Chanfcfa0a32006-03-20 22:28:41 -08009634{
David Howells7d12e782006-10-05 14:55:46 +01009635 irq_handler_t fn;
Michael Chanfcfa0a32006-03-20 22:28:41 -08009636 unsigned long flags;
Matt Carlson4f125f42009-09-01 12:55:02 +00009637 char *name;
9638 struct tg3_napi *tnapi = &tp->napi[irq_num];
9639
9640 if (tp->irq_cnt == 1)
9641 name = tp->dev->name;
9642 else {
9643 name = &tnapi->irq_lbl[0];
9644 snprintf(name, IFNAMSIZ, "%s-%d", tp->dev->name, irq_num);
9645 name[IFNAMSIZ-1] = 0;
9646 }
Michael Chanfcfa0a32006-03-20 22:28:41 -08009647
Joe Perches63c3a662011-04-26 08:12:10 +00009648 if (tg3_flag(tp, USING_MSI) || tg3_flag(tp, USING_MSIX)) {
Michael Chanfcfa0a32006-03-20 22:28:41 -08009649 fn = tg3_msi;
Joe Perches63c3a662011-04-26 08:12:10 +00009650 if (tg3_flag(tp, 1SHOT_MSI))
Michael Chanfcfa0a32006-03-20 22:28:41 -08009651 fn = tg3_msi_1shot;
Javier Martinez Canillasab392d22011-03-28 16:27:31 +00009652 flags = 0;
Michael Chanfcfa0a32006-03-20 22:28:41 -08009653 } else {
9654 fn = tg3_interrupt;
Joe Perches63c3a662011-04-26 08:12:10 +00009655 if (tg3_flag(tp, TAGGED_STATUS))
Michael Chanfcfa0a32006-03-20 22:28:41 -08009656 fn = tg3_interrupt_tagged;
Javier Martinez Canillasab392d22011-03-28 16:27:31 +00009657 flags = IRQF_SHARED;
Michael Chanfcfa0a32006-03-20 22:28:41 -08009658 }
Matt Carlson4f125f42009-09-01 12:55:02 +00009659
9660 return request_irq(tnapi->irq_vec, fn, flags, name, tnapi);
Michael Chanfcfa0a32006-03-20 22:28:41 -08009661}
9662
Michael Chan79381092005-04-21 17:13:59 -07009663static int tg3_test_interrupt(struct tg3 *tp)
9664{
Matt Carlson09943a12009-08-28 14:01:57 +00009665 struct tg3_napi *tnapi = &tp->napi[0];
Michael Chan79381092005-04-21 17:13:59 -07009666 struct net_device *dev = tp->dev;
Michael Chanb16250e2006-09-27 16:10:14 -07009667 int err, i, intr_ok = 0;
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009668 u32 val;
Michael Chan79381092005-04-21 17:13:59 -07009669
Michael Chand4bc3922005-05-29 14:59:20 -07009670 if (!netif_running(dev))
9671 return -ENODEV;
9672
Michael Chan79381092005-04-21 17:13:59 -07009673 tg3_disable_ints(tp);
9674
Matt Carlson4f125f42009-09-01 12:55:02 +00009675 free_irq(tnapi->irq_vec, tnapi);
Michael Chan79381092005-04-21 17:13:59 -07009676
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009677 /*
9678 * Turn off MSI one shot mode. Otherwise this test has no
9679 * observable way to know whether the interrupt was delivered.
9680 */
Matt Carlson3aa1cdf2011-07-20 10:20:55 +00009681 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009682 val = tr32(MSGINT_MODE) | MSGINT_MODE_ONE_SHOT_DISABLE;
9683 tw32(MSGINT_MODE, val);
9684 }
9685
Matt Carlson4f125f42009-09-01 12:55:02 +00009686 err = request_irq(tnapi->irq_vec, tg3_test_isr,
Matt Carlson09943a12009-08-28 14:01:57 +00009687 IRQF_SHARED | IRQF_SAMPLE_RANDOM, dev->name, tnapi);
Michael Chan79381092005-04-21 17:13:59 -07009688 if (err)
9689 return err;
9690
Matt Carlson898a56f2009-08-28 14:02:40 +00009691 tnapi->hw_status->status &= ~SD_STATUS_UPDATED;
Michael Chan79381092005-04-21 17:13:59 -07009692 tg3_enable_ints(tp);
9693
9694 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +00009695 tnapi->coal_now);
Michael Chan79381092005-04-21 17:13:59 -07009696
9697 for (i = 0; i < 5; i++) {
Michael Chanb16250e2006-09-27 16:10:14 -07009698 u32 int_mbox, misc_host_ctrl;
9699
Matt Carlson898a56f2009-08-28 14:02:40 +00009700 int_mbox = tr32_mailbox(tnapi->int_mbox);
Michael Chanb16250e2006-09-27 16:10:14 -07009701 misc_host_ctrl = tr32(TG3PCI_MISC_HOST_CTRL);
9702
9703 if ((int_mbox != 0) ||
9704 (misc_host_ctrl & MISC_HOST_CTRL_MASK_PCI_INT)) {
9705 intr_ok = 1;
Michael Chan79381092005-04-21 17:13:59 -07009706 break;
Michael Chanb16250e2006-09-27 16:10:14 -07009707 }
9708
Matt Carlson3aa1cdf2011-07-20 10:20:55 +00009709 if (tg3_flag(tp, 57765_PLUS) &&
9710 tnapi->hw_status->status_tag != tnapi->last_tag)
9711 tw32_mailbox_f(tnapi->int_mbox, tnapi->last_tag << 24);
9712
Michael Chan79381092005-04-21 17:13:59 -07009713 msleep(10);
9714 }
9715
9716 tg3_disable_ints(tp);
9717
Matt Carlson4f125f42009-09-01 12:55:02 +00009718 free_irq(tnapi->irq_vec, tnapi);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009719
Matt Carlson4f125f42009-09-01 12:55:02 +00009720 err = tg3_request_irq(tp, 0);
Michael Chan79381092005-04-21 17:13:59 -07009721
9722 if (err)
9723 return err;
9724
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009725 if (intr_ok) {
9726 /* Reenable MSI one shot mode. */
Matt Carlson5b39de92011-08-31 11:44:50 +00009727 if (tg3_flag(tp, 57765_PLUS) && tg3_flag(tp, 1SHOT_MSI)) {
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009728 val = tr32(MSGINT_MODE) & ~MSGINT_MODE_ONE_SHOT_DISABLE;
9729 tw32(MSGINT_MODE, val);
9730 }
Michael Chan79381092005-04-21 17:13:59 -07009731 return 0;
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009732 }
Michael Chan79381092005-04-21 17:13:59 -07009733
9734 return -EIO;
9735}
9736
9737/* Returns 0 if MSI test succeeds or MSI test fails and INTx mode is
9738 * successfully restored
9739 */
9740static int tg3_test_msi(struct tg3 *tp)
9741{
Michael Chan79381092005-04-21 17:13:59 -07009742 int err;
9743 u16 pci_cmd;
9744
Joe Perches63c3a662011-04-26 08:12:10 +00009745 if (!tg3_flag(tp, USING_MSI))
Michael Chan79381092005-04-21 17:13:59 -07009746 return 0;
9747
9748 /* Turn off SERR reporting in case MSI terminates with Master
9749 * Abort.
9750 */
9751 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
9752 pci_write_config_word(tp->pdev, PCI_COMMAND,
9753 pci_cmd & ~PCI_COMMAND_SERR);
9754
9755 err = tg3_test_interrupt(tp);
9756
9757 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
9758
9759 if (!err)
9760 return 0;
9761
9762 /* other failures */
9763 if (err != -EIO)
9764 return err;
9765
9766 /* MSI test failed, go back to INTx mode */
Matt Carlson5129c3a2010-04-05 10:19:23 +00009767 netdev_warn(tp->dev, "No interrupt was generated using MSI. Switching "
9768 "to INTx mode. Please report this failure to the PCI "
9769 "maintainer and include system chipset information\n");
Michael Chan79381092005-04-21 17:13:59 -07009770
Matt Carlson4f125f42009-09-01 12:55:02 +00009771 free_irq(tp->napi[0].irq_vec, &tp->napi[0]);
Matt Carlson09943a12009-08-28 14:01:57 +00009772
Michael Chan79381092005-04-21 17:13:59 -07009773 pci_disable_msi(tp->pdev);
9774
Joe Perches63c3a662011-04-26 08:12:10 +00009775 tg3_flag_clear(tp, USING_MSI);
Andre Detschdc8bf1b2010-04-26 07:27:07 +00009776 tp->napi[0].irq_vec = tp->pdev->irq;
Michael Chan79381092005-04-21 17:13:59 -07009777
Matt Carlson4f125f42009-09-01 12:55:02 +00009778 err = tg3_request_irq(tp, 0);
Michael Chan79381092005-04-21 17:13:59 -07009779 if (err)
9780 return err;
9781
9782 /* Need to reset the chip because the MSI cycle may have terminated
9783 * with Master Abort.
9784 */
David S. Millerf47c11e2005-06-24 20:18:35 -07009785 tg3_full_lock(tp, 1);
Michael Chan79381092005-04-21 17:13:59 -07009786
Michael Chan944d9802005-05-29 14:57:48 -07009787 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07009788 err = tg3_init_hw(tp, 1);
Michael Chan79381092005-04-21 17:13:59 -07009789
David S. Millerf47c11e2005-06-24 20:18:35 -07009790 tg3_full_unlock(tp);
Michael Chan79381092005-04-21 17:13:59 -07009791
9792 if (err)
Matt Carlson4f125f42009-09-01 12:55:02 +00009793 free_irq(tp->napi[0].irq_vec, &tp->napi[0]);
Michael Chan79381092005-04-21 17:13:59 -07009794
9795 return err;
9796}
9797
Matt Carlson9e9fd122009-01-19 16:57:45 -08009798static int tg3_request_firmware(struct tg3 *tp)
9799{
9800 const __be32 *fw_data;
9801
9802 if (request_firmware(&tp->fw, tp->fw_needed, &tp->pdev->dev)) {
Joe Perches05dbe002010-02-17 19:44:19 +00009803 netdev_err(tp->dev, "Failed to load firmware \"%s\"\n",
9804 tp->fw_needed);
Matt Carlson9e9fd122009-01-19 16:57:45 -08009805 return -ENOENT;
9806 }
9807
9808 fw_data = (void *)tp->fw->data;
9809
9810 /* Firmware blob starts with version numbers, followed by
9811 * start address and _full_ length including BSS sections
9812 * (which must be longer than the actual data, of course
9813 */
9814
9815 tp->fw_len = be32_to_cpu(fw_data[2]); /* includes bss */
9816 if (tp->fw_len < (tp->fw->size - 12)) {
Joe Perches05dbe002010-02-17 19:44:19 +00009817 netdev_err(tp->dev, "bogus length %d in \"%s\"\n",
9818 tp->fw_len, tp->fw_needed);
Matt Carlson9e9fd122009-01-19 16:57:45 -08009819 release_firmware(tp->fw);
9820 tp->fw = NULL;
9821 return -EINVAL;
9822 }
9823
9824 /* We no longer need firmware; we have it. */
9825 tp->fw_needed = NULL;
9826 return 0;
9827}
9828
Matt Carlson679563f2009-09-01 12:55:46 +00009829static bool tg3_enable_msix(struct tg3 *tp)
9830{
Matt Carlsonc3b50032012-01-17 15:27:23 +00009831 int i, rc;
Matt Carlson679563f2009-09-01 12:55:46 +00009832 struct msix_entry msix_ent[tp->irq_max];
9833
Matt Carlsonc3b50032012-01-17 15:27:23 +00009834 tp->irq_cnt = num_online_cpus();
9835 if (tp->irq_cnt > 1) {
9836 /* We want as many rx rings enabled as there are cpus.
9837 * In multiqueue MSI-X mode, the first MSI-X vector
9838 * only deals with link interrupts, etc, so we add
9839 * one to the number of vectors we are requesting.
9840 */
9841 tp->irq_cnt = min_t(unsigned, tp->irq_cnt + 1, tp->irq_max);
9842 }
Matt Carlson679563f2009-09-01 12:55:46 +00009843
9844 for (i = 0; i < tp->irq_max; i++) {
9845 msix_ent[i].entry = i;
9846 msix_ent[i].vector = 0;
9847 }
9848
9849 rc = pci_enable_msix(tp->pdev, msix_ent, tp->irq_cnt);
Matt Carlson2430b032010-06-05 17:24:34 +00009850 if (rc < 0) {
9851 return false;
9852 } else if (rc != 0) {
Matt Carlson679563f2009-09-01 12:55:46 +00009853 if (pci_enable_msix(tp->pdev, msix_ent, rc))
9854 return false;
Joe Perches05dbe002010-02-17 19:44:19 +00009855 netdev_notice(tp->dev, "Requested %d MSI-X vectors, received %d\n",
9856 tp->irq_cnt, rc);
Matt Carlson679563f2009-09-01 12:55:46 +00009857 tp->irq_cnt = rc;
9858 }
9859
9860 for (i = 0; i < tp->irq_max; i++)
9861 tp->napi[i].irq_vec = msix_ent[i].vector;
9862
Ben Hutchings2ddaad32010-09-27 22:11:51 -07009863 netif_set_real_num_tx_queues(tp->dev, 1);
9864 rc = tp->irq_cnt > 1 ? tp->irq_cnt - 1 : 1;
9865 if (netif_set_real_num_rx_queues(tp->dev, rc)) {
9866 pci_disable_msix(tp->pdev);
9867 return false;
9868 }
Matt Carlsonb92b9042010-11-24 08:31:51 +00009869
9870 if (tp->irq_cnt > 1) {
Joe Perches63c3a662011-04-26 08:12:10 +00009871 tg3_flag_set(tp, ENABLE_RSS);
Matt Carlsond78b59f2011-04-05 14:22:46 +00009872
9873 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
9874 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
Joe Perches63c3a662011-04-26 08:12:10 +00009875 tg3_flag_set(tp, ENABLE_TSS);
Matt Carlsonb92b9042010-11-24 08:31:51 +00009876 netif_set_real_num_tx_queues(tp->dev, tp->irq_cnt - 1);
9877 }
9878 }
Matt Carlson2430b032010-06-05 17:24:34 +00009879
Matt Carlson679563f2009-09-01 12:55:46 +00009880 return true;
9881}
9882
Matt Carlson07b01732009-08-28 14:01:15 +00009883static void tg3_ints_init(struct tg3 *tp)
9884{
Joe Perches63c3a662011-04-26 08:12:10 +00009885 if ((tg3_flag(tp, SUPPORT_MSI) || tg3_flag(tp, SUPPORT_MSIX)) &&
9886 !tg3_flag(tp, TAGGED_STATUS)) {
Matt Carlson07b01732009-08-28 14:01:15 +00009887 /* All MSI supporting chips should support tagged
9888 * status. Assert that this is the case.
9889 */
Matt Carlson5129c3a2010-04-05 10:19:23 +00009890 netdev_warn(tp->dev,
9891 "MSI without TAGGED_STATUS? Not using MSI\n");
Matt Carlson679563f2009-09-01 12:55:46 +00009892 goto defcfg;
Matt Carlson07b01732009-08-28 14:01:15 +00009893 }
Matt Carlson4f125f42009-09-01 12:55:02 +00009894
Joe Perches63c3a662011-04-26 08:12:10 +00009895 if (tg3_flag(tp, SUPPORT_MSIX) && tg3_enable_msix(tp))
9896 tg3_flag_set(tp, USING_MSIX);
9897 else if (tg3_flag(tp, SUPPORT_MSI) && pci_enable_msi(tp->pdev) == 0)
9898 tg3_flag_set(tp, USING_MSI);
Matt Carlson679563f2009-09-01 12:55:46 +00009899
Joe Perches63c3a662011-04-26 08:12:10 +00009900 if (tg3_flag(tp, USING_MSI) || tg3_flag(tp, USING_MSIX)) {
Matt Carlson679563f2009-09-01 12:55:46 +00009901 u32 msi_mode = tr32(MSGINT_MODE);
Joe Perches63c3a662011-04-26 08:12:10 +00009902 if (tg3_flag(tp, USING_MSIX) && tp->irq_cnt > 1)
Matt Carlsonbaf8a942009-09-01 13:13:00 +00009903 msi_mode |= MSGINT_MODE_MULTIVEC_EN;
Matt Carlson5b39de92011-08-31 11:44:50 +00009904 if (!tg3_flag(tp, 1SHOT_MSI))
9905 msi_mode |= MSGINT_MODE_ONE_SHOT_DISABLE;
Matt Carlson679563f2009-09-01 12:55:46 +00009906 tw32(MSGINT_MODE, msi_mode | MSGINT_MODE_ENABLE);
9907 }
9908defcfg:
Joe Perches63c3a662011-04-26 08:12:10 +00009909 if (!tg3_flag(tp, USING_MSIX)) {
Matt Carlson679563f2009-09-01 12:55:46 +00009910 tp->irq_cnt = 1;
9911 tp->napi[0].irq_vec = tp->pdev->irq;
Ben Hutchings2ddaad32010-09-27 22:11:51 -07009912 netif_set_real_num_tx_queues(tp->dev, 1);
Matt Carlson85407882010-10-06 13:40:58 -07009913 netif_set_real_num_rx_queues(tp->dev, 1);
Matt Carlson679563f2009-09-01 12:55:46 +00009914 }
Matt Carlson07b01732009-08-28 14:01:15 +00009915}
9916
9917static void tg3_ints_fini(struct tg3 *tp)
9918{
Joe Perches63c3a662011-04-26 08:12:10 +00009919 if (tg3_flag(tp, USING_MSIX))
Matt Carlson679563f2009-09-01 12:55:46 +00009920 pci_disable_msix(tp->pdev);
Joe Perches63c3a662011-04-26 08:12:10 +00009921 else if (tg3_flag(tp, USING_MSI))
Matt Carlson679563f2009-09-01 12:55:46 +00009922 pci_disable_msi(tp->pdev);
Joe Perches63c3a662011-04-26 08:12:10 +00009923 tg3_flag_clear(tp, USING_MSI);
9924 tg3_flag_clear(tp, USING_MSIX);
9925 tg3_flag_clear(tp, ENABLE_RSS);
9926 tg3_flag_clear(tp, ENABLE_TSS);
Matt Carlson07b01732009-08-28 14:01:15 +00009927}
9928
Linus Torvalds1da177e2005-04-16 15:20:36 -07009929static int tg3_open(struct net_device *dev)
9930{
9931 struct tg3 *tp = netdev_priv(dev);
Matt Carlson4f125f42009-09-01 12:55:02 +00009932 int i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009933
Matt Carlson9e9fd122009-01-19 16:57:45 -08009934 if (tp->fw_needed) {
9935 err = tg3_request_firmware(tp);
9936 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0) {
9937 if (err)
9938 return err;
9939 } else if (err) {
Joe Perches05dbe002010-02-17 19:44:19 +00009940 netdev_warn(tp->dev, "TSO capability disabled\n");
Joe Perches63c3a662011-04-26 08:12:10 +00009941 tg3_flag_clear(tp, TSO_CAPABLE);
9942 } else if (!tg3_flag(tp, TSO_CAPABLE)) {
Joe Perches05dbe002010-02-17 19:44:19 +00009943 netdev_notice(tp->dev, "TSO capability restored\n");
Joe Perches63c3a662011-04-26 08:12:10 +00009944 tg3_flag_set(tp, TSO_CAPABLE);
Matt Carlson9e9fd122009-01-19 16:57:45 -08009945 }
9946 }
9947
Michael Chanc49a1562006-12-17 17:07:29 -08009948 netif_carrier_off(tp->dev);
9949
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00009950 err = tg3_power_up(tp);
Matt Carlson2f751b62008-08-04 23:17:34 -07009951 if (err)
Michael Chanbc1c7562006-03-20 17:48:03 -08009952 return err;
Matt Carlson2f751b62008-08-04 23:17:34 -07009953
9954 tg3_full_lock(tp, 0);
Michael Chanbc1c7562006-03-20 17:48:03 -08009955
Linus Torvalds1da177e2005-04-16 15:20:36 -07009956 tg3_disable_ints(tp);
Joe Perches63c3a662011-04-26 08:12:10 +00009957 tg3_flag_clear(tp, INIT_COMPLETE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009958
David S. Millerf47c11e2005-06-24 20:18:35 -07009959 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009960
Matt Carlson679563f2009-09-01 12:55:46 +00009961 /*
9962 * Setup interrupts first so we know how
9963 * many NAPI resources to allocate
9964 */
9965 tg3_ints_init(tp);
9966
Matt Carlson90415472011-12-16 13:33:23 +00009967 tg3_rss_check_indir_tbl(tp);
Matt Carlsonbcebcc42011-12-14 11:10:01 +00009968
Linus Torvalds1da177e2005-04-16 15:20:36 -07009969 /* The placement of this call is tied
9970 * to the setup and use of Host TX descriptors.
9971 */
9972 err = tg3_alloc_consistent(tp);
9973 if (err)
Matt Carlson679563f2009-09-01 12:55:46 +00009974 goto err_out1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009975
Matt Carlson66cfd1b2010-09-30 10:34:30 +00009976 tg3_napi_init(tp);
9977
Matt Carlsonfed97812009-09-01 13:10:19 +00009978 tg3_napi_enable(tp);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07009979
Matt Carlson4f125f42009-09-01 12:55:02 +00009980 for (i = 0; i < tp->irq_cnt; i++) {
9981 struct tg3_napi *tnapi = &tp->napi[i];
9982 err = tg3_request_irq(tp, i);
9983 if (err) {
Matt Carlson5bc09182011-11-04 09:15:01 +00009984 for (i--; i >= 0; i--) {
9985 tnapi = &tp->napi[i];
Matt Carlson4f125f42009-09-01 12:55:02 +00009986 free_irq(tnapi->irq_vec, tnapi);
Matt Carlson5bc09182011-11-04 09:15:01 +00009987 }
9988 goto err_out2;
Matt Carlson4f125f42009-09-01 12:55:02 +00009989 }
9990 }
Matt Carlson07b01732009-08-28 14:01:15 +00009991
David S. Millerf47c11e2005-06-24 20:18:35 -07009992 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009993
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07009994 err = tg3_init_hw(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009995 if (err) {
Michael Chan944d9802005-05-29 14:57:48 -07009996 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009997 tg3_free_rings(tp);
9998 } else {
Matt Carlson0e6cf6a2011-06-13 13:38:55 +00009999 if (tg3_flag(tp, TAGGED_STATUS) &&
Matt Carlson55086ad2011-12-14 11:09:59 +000010000 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717 &&
10001 !tg3_flag(tp, 57765_CLASS))
David S. Millerfac9b832005-05-18 22:46:34 -070010002 tp->timer_offset = HZ;
10003 else
10004 tp->timer_offset = HZ / 10;
10005
10006 BUG_ON(tp->timer_offset > HZ);
10007 tp->timer_counter = tp->timer_multiplier =
10008 (HZ / tp->timer_offset);
10009 tp->asf_counter = tp->asf_multiplier =
Michael Chan28fbef72005-10-26 15:48:35 -070010010 ((HZ / tp->timer_offset) * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010011
10012 init_timer(&tp->timer);
10013 tp->timer.expires = jiffies + tp->timer_offset;
10014 tp->timer.data = (unsigned long) tp;
10015 tp->timer.function = tg3_timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010016 }
10017
David S. Millerf47c11e2005-06-24 20:18:35 -070010018 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010019
Matt Carlson07b01732009-08-28 14:01:15 +000010020 if (err)
Matt Carlson679563f2009-09-01 12:55:46 +000010021 goto err_out3;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010022
Joe Perches63c3a662011-04-26 08:12:10 +000010023 if (tg3_flag(tp, USING_MSI)) {
Michael Chan79381092005-04-21 17:13:59 -070010024 err = tg3_test_msi(tp);
David S. Millerfac9b832005-05-18 22:46:34 -070010025
Michael Chan79381092005-04-21 17:13:59 -070010026 if (err) {
David S. Millerf47c11e2005-06-24 20:18:35 -070010027 tg3_full_lock(tp, 0);
Michael Chan944d9802005-05-29 14:57:48 -070010028 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Michael Chan79381092005-04-21 17:13:59 -070010029 tg3_free_rings(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -070010030 tg3_full_unlock(tp);
Michael Chan79381092005-04-21 17:13:59 -070010031
Matt Carlson679563f2009-09-01 12:55:46 +000010032 goto err_out2;
Michael Chan79381092005-04-21 17:13:59 -070010033 }
Michael Chanfcfa0a32006-03-20 22:28:41 -080010034
Joe Perches63c3a662011-04-26 08:12:10 +000010035 if (!tg3_flag(tp, 57765_PLUS) && tg3_flag(tp, USING_MSI)) {
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000010036 u32 val = tr32(PCIE_TRANSACTION_CFG);
Michael Chanfcfa0a32006-03-20 22:28:41 -080010037
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000010038 tw32(PCIE_TRANSACTION_CFG,
10039 val | PCIE_TRANS_CFG_1SHOT_MSI);
Michael Chanfcfa0a32006-03-20 22:28:41 -080010040 }
Michael Chan79381092005-04-21 17:13:59 -070010041 }
10042
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010043 tg3_phy_start(tp);
10044
David S. Millerf47c11e2005-06-24 20:18:35 -070010045 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010046
Michael Chan79381092005-04-21 17:13:59 -070010047 add_timer(&tp->timer);
Joe Perches63c3a662011-04-26 08:12:10 +000010048 tg3_flag_set(tp, INIT_COMPLETE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010049 tg3_enable_ints(tp);
10050
David S. Millerf47c11e2005-06-24 20:18:35 -070010051 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010052
Matt Carlsonfe5f5782009-09-01 13:09:39 +000010053 netif_tx_start_all_queues(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010054
Mahesh Bandewar06c03c02011-05-08 06:51:48 +000010055 /*
10056 * Reset loopback feature if it was turned on while the device was down
10057 * make sure that it's installed properly now.
10058 */
10059 if (dev->features & NETIF_F_LOOPBACK)
10060 tg3_set_loopback(dev, dev->features);
10061
Linus Torvalds1da177e2005-04-16 15:20:36 -070010062 return 0;
Matt Carlson07b01732009-08-28 14:01:15 +000010063
Matt Carlson679563f2009-09-01 12:55:46 +000010064err_out3:
Matt Carlson4f125f42009-09-01 12:55:02 +000010065 for (i = tp->irq_cnt - 1; i >= 0; i--) {
10066 struct tg3_napi *tnapi = &tp->napi[i];
10067 free_irq(tnapi->irq_vec, tnapi);
10068 }
Matt Carlson07b01732009-08-28 14:01:15 +000010069
Matt Carlson679563f2009-09-01 12:55:46 +000010070err_out2:
Matt Carlsonfed97812009-09-01 13:10:19 +000010071 tg3_napi_disable(tp);
Matt Carlson66cfd1b2010-09-30 10:34:30 +000010072 tg3_napi_fini(tp);
Matt Carlson07b01732009-08-28 14:01:15 +000010073 tg3_free_consistent(tp);
Matt Carlson679563f2009-09-01 12:55:46 +000010074
10075err_out1:
10076 tg3_ints_fini(tp);
Matt Carlsoncd0d7222011-07-13 09:27:33 +000010077 tg3_frob_aux_power(tp, false);
10078 pci_set_power_state(tp->pdev, PCI_D3hot);
Matt Carlson07b01732009-08-28 14:01:15 +000010079 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010080}
10081
Linus Torvalds1da177e2005-04-16 15:20:36 -070010082static int tg3_close(struct net_device *dev)
10083{
Matt Carlson4f125f42009-09-01 12:55:02 +000010084 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010085 struct tg3 *tp = netdev_priv(dev);
10086
Matt Carlsonfed97812009-09-01 13:10:19 +000010087 tg3_napi_disable(tp);
Matt Carlsondb219972011-11-04 09:15:03 +000010088 tg3_reset_task_cancel(tp);
Michael Chan7faa0062006-02-02 17:29:28 -080010089
Matt Carlsonfe5f5782009-09-01 13:09:39 +000010090 netif_tx_stop_all_queues(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010091
10092 del_timer_sync(&tp->timer);
10093
Matt Carlson24bb4fb2009-10-05 17:55:29 +000010094 tg3_phy_stop(tp);
10095
David S. Millerf47c11e2005-06-24 20:18:35 -070010096 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010097
10098 tg3_disable_ints(tp);
10099
Michael Chan944d9802005-05-29 14:57:48 -070010100 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010101 tg3_free_rings(tp);
Joe Perches63c3a662011-04-26 08:12:10 +000010102 tg3_flag_clear(tp, INIT_COMPLETE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010103
David S. Millerf47c11e2005-06-24 20:18:35 -070010104 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010105
Matt Carlson4f125f42009-09-01 12:55:02 +000010106 for (i = tp->irq_cnt - 1; i >= 0; i--) {
10107 struct tg3_napi *tnapi = &tp->napi[i];
10108 free_irq(tnapi->irq_vec, tnapi);
10109 }
Matt Carlson07b01732009-08-28 14:01:15 +000010110
10111 tg3_ints_fini(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010112
Matt Carlson92feeab2011-12-08 14:40:14 +000010113 /* Clear stats across close / open calls */
10114 memset(&tp->net_stats_prev, 0, sizeof(tp->net_stats_prev));
10115 memset(&tp->estats_prev, 0, sizeof(tp->estats_prev));
Linus Torvalds1da177e2005-04-16 15:20:36 -070010116
Matt Carlson66cfd1b2010-09-30 10:34:30 +000010117 tg3_napi_fini(tp);
10118
Linus Torvalds1da177e2005-04-16 15:20:36 -070010119 tg3_free_consistent(tp);
10120
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000010121 tg3_power_down(tp);
Michael Chanbc1c7562006-03-20 17:48:03 -080010122
10123 netif_carrier_off(tp->dev);
10124
Linus Torvalds1da177e2005-04-16 15:20:36 -070010125 return 0;
10126}
10127
Eric Dumazet511d2222010-07-07 20:44:24 +000010128static inline u64 get_stat64(tg3_stat64_t *val)
Stefan Buehler816f8b82008-08-15 14:10:54 -070010129{
10130 return ((u64)val->high << 32) | ((u64)val->low);
10131}
10132
Eric Dumazet511d2222010-07-07 20:44:24 +000010133static u64 calc_crc_errors(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010134{
10135 struct tg3_hw_stats *hw_stats = tp->hw_stats;
10136
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010137 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070010138 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
10139 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010140 u32 val;
10141
David S. Millerf47c11e2005-06-24 20:18:35 -070010142 spin_lock_bh(&tp->lock);
Michael Chan569a5df2007-02-13 12:18:15 -080010143 if (!tg3_readphy(tp, MII_TG3_TEST1, &val)) {
10144 tg3_writephy(tp, MII_TG3_TEST1,
10145 val | MII_TG3_TEST1_CRC_EN);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +000010146 tg3_readphy(tp, MII_TG3_RXR_COUNTERS, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010147 } else
10148 val = 0;
David S. Millerf47c11e2005-06-24 20:18:35 -070010149 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010150
10151 tp->phy_crc_errors += val;
10152
10153 return tp->phy_crc_errors;
10154 }
10155
10156 return get_stat64(&hw_stats->rx_fcs_errors);
10157}
10158
10159#define ESTAT_ADD(member) \
10160 estats->member = old_estats->member + \
Eric Dumazet511d2222010-07-07 20:44:24 +000010161 get_stat64(&hw_stats->member)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010162
Matt Carlson0e6c9da2011-12-08 14:40:13 +000010163static struct tg3_ethtool_stats *tg3_get_estats(struct tg3 *tp,
10164 struct tg3_ethtool_stats *estats)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010165{
Linus Torvalds1da177e2005-04-16 15:20:36 -070010166 struct tg3_ethtool_stats *old_estats = &tp->estats_prev;
10167 struct tg3_hw_stats *hw_stats = tp->hw_stats;
10168
Linus Torvalds1da177e2005-04-16 15:20:36 -070010169 ESTAT_ADD(rx_octets);
10170 ESTAT_ADD(rx_fragments);
10171 ESTAT_ADD(rx_ucast_packets);
10172 ESTAT_ADD(rx_mcast_packets);
10173 ESTAT_ADD(rx_bcast_packets);
10174 ESTAT_ADD(rx_fcs_errors);
10175 ESTAT_ADD(rx_align_errors);
10176 ESTAT_ADD(rx_xon_pause_rcvd);
10177 ESTAT_ADD(rx_xoff_pause_rcvd);
10178 ESTAT_ADD(rx_mac_ctrl_rcvd);
10179 ESTAT_ADD(rx_xoff_entered);
10180 ESTAT_ADD(rx_frame_too_long_errors);
10181 ESTAT_ADD(rx_jabbers);
10182 ESTAT_ADD(rx_undersize_packets);
10183 ESTAT_ADD(rx_in_length_errors);
10184 ESTAT_ADD(rx_out_length_errors);
10185 ESTAT_ADD(rx_64_or_less_octet_packets);
10186 ESTAT_ADD(rx_65_to_127_octet_packets);
10187 ESTAT_ADD(rx_128_to_255_octet_packets);
10188 ESTAT_ADD(rx_256_to_511_octet_packets);
10189 ESTAT_ADD(rx_512_to_1023_octet_packets);
10190 ESTAT_ADD(rx_1024_to_1522_octet_packets);
10191 ESTAT_ADD(rx_1523_to_2047_octet_packets);
10192 ESTAT_ADD(rx_2048_to_4095_octet_packets);
10193 ESTAT_ADD(rx_4096_to_8191_octet_packets);
10194 ESTAT_ADD(rx_8192_to_9022_octet_packets);
10195
10196 ESTAT_ADD(tx_octets);
10197 ESTAT_ADD(tx_collisions);
10198 ESTAT_ADD(tx_xon_sent);
10199 ESTAT_ADD(tx_xoff_sent);
10200 ESTAT_ADD(tx_flow_control);
10201 ESTAT_ADD(tx_mac_errors);
10202 ESTAT_ADD(tx_single_collisions);
10203 ESTAT_ADD(tx_mult_collisions);
10204 ESTAT_ADD(tx_deferred);
10205 ESTAT_ADD(tx_excessive_collisions);
10206 ESTAT_ADD(tx_late_collisions);
10207 ESTAT_ADD(tx_collide_2times);
10208 ESTAT_ADD(tx_collide_3times);
10209 ESTAT_ADD(tx_collide_4times);
10210 ESTAT_ADD(tx_collide_5times);
10211 ESTAT_ADD(tx_collide_6times);
10212 ESTAT_ADD(tx_collide_7times);
10213 ESTAT_ADD(tx_collide_8times);
10214 ESTAT_ADD(tx_collide_9times);
10215 ESTAT_ADD(tx_collide_10times);
10216 ESTAT_ADD(tx_collide_11times);
10217 ESTAT_ADD(tx_collide_12times);
10218 ESTAT_ADD(tx_collide_13times);
10219 ESTAT_ADD(tx_collide_14times);
10220 ESTAT_ADD(tx_collide_15times);
10221 ESTAT_ADD(tx_ucast_packets);
10222 ESTAT_ADD(tx_mcast_packets);
10223 ESTAT_ADD(tx_bcast_packets);
10224 ESTAT_ADD(tx_carrier_sense_errors);
10225 ESTAT_ADD(tx_discards);
10226 ESTAT_ADD(tx_errors);
10227
10228 ESTAT_ADD(dma_writeq_full);
10229 ESTAT_ADD(dma_write_prioq_full);
10230 ESTAT_ADD(rxbds_empty);
10231 ESTAT_ADD(rx_discards);
10232 ESTAT_ADD(rx_errors);
10233 ESTAT_ADD(rx_threshold_hit);
10234
10235 ESTAT_ADD(dma_readq_full);
10236 ESTAT_ADD(dma_read_prioq_full);
10237 ESTAT_ADD(tx_comp_queue_full);
10238
10239 ESTAT_ADD(ring_set_send_prod_index);
10240 ESTAT_ADD(ring_status_update);
10241 ESTAT_ADD(nic_irqs);
10242 ESTAT_ADD(nic_avoided_irqs);
10243 ESTAT_ADD(nic_tx_threshold_hit);
10244
Matt Carlson4452d092011-05-19 12:12:51 +000010245 ESTAT_ADD(mbuf_lwm_thresh_hit);
10246
Linus Torvalds1da177e2005-04-16 15:20:36 -070010247 return estats;
10248}
10249
Eric Dumazet511d2222010-07-07 20:44:24 +000010250static struct rtnl_link_stats64 *tg3_get_stats64(struct net_device *dev,
10251 struct rtnl_link_stats64 *stats)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010252{
10253 struct tg3 *tp = netdev_priv(dev);
Eric Dumazet511d2222010-07-07 20:44:24 +000010254 struct rtnl_link_stats64 *old_stats = &tp->net_stats_prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010255 struct tg3_hw_stats *hw_stats = tp->hw_stats;
10256
10257 if (!hw_stats)
10258 return old_stats;
10259
10260 stats->rx_packets = old_stats->rx_packets +
10261 get_stat64(&hw_stats->rx_ucast_packets) +
10262 get_stat64(&hw_stats->rx_mcast_packets) +
10263 get_stat64(&hw_stats->rx_bcast_packets);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010264
Linus Torvalds1da177e2005-04-16 15:20:36 -070010265 stats->tx_packets = old_stats->tx_packets +
10266 get_stat64(&hw_stats->tx_ucast_packets) +
10267 get_stat64(&hw_stats->tx_mcast_packets) +
10268 get_stat64(&hw_stats->tx_bcast_packets);
10269
10270 stats->rx_bytes = old_stats->rx_bytes +
10271 get_stat64(&hw_stats->rx_octets);
10272 stats->tx_bytes = old_stats->tx_bytes +
10273 get_stat64(&hw_stats->tx_octets);
10274
10275 stats->rx_errors = old_stats->rx_errors +
John W. Linville4f63b872005-09-12 14:43:18 -070010276 get_stat64(&hw_stats->rx_errors);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010277 stats->tx_errors = old_stats->tx_errors +
10278 get_stat64(&hw_stats->tx_errors) +
10279 get_stat64(&hw_stats->tx_mac_errors) +
10280 get_stat64(&hw_stats->tx_carrier_sense_errors) +
10281 get_stat64(&hw_stats->tx_discards);
10282
10283 stats->multicast = old_stats->multicast +
10284 get_stat64(&hw_stats->rx_mcast_packets);
10285 stats->collisions = old_stats->collisions +
10286 get_stat64(&hw_stats->tx_collisions);
10287
10288 stats->rx_length_errors = old_stats->rx_length_errors +
10289 get_stat64(&hw_stats->rx_frame_too_long_errors) +
10290 get_stat64(&hw_stats->rx_undersize_packets);
10291
10292 stats->rx_over_errors = old_stats->rx_over_errors +
10293 get_stat64(&hw_stats->rxbds_empty);
10294 stats->rx_frame_errors = old_stats->rx_frame_errors +
10295 get_stat64(&hw_stats->rx_align_errors);
10296 stats->tx_aborted_errors = old_stats->tx_aborted_errors +
10297 get_stat64(&hw_stats->tx_discards);
10298 stats->tx_carrier_errors = old_stats->tx_carrier_errors +
10299 get_stat64(&hw_stats->tx_carrier_sense_errors);
10300
10301 stats->rx_crc_errors = old_stats->rx_crc_errors +
10302 calc_crc_errors(tp);
10303
John W. Linville4f63b872005-09-12 14:43:18 -070010304 stats->rx_missed_errors = old_stats->rx_missed_errors +
10305 get_stat64(&hw_stats->rx_discards);
10306
Eric Dumazetb0057c52010-10-10 19:55:52 +000010307 stats->rx_dropped = tp->rx_dropped;
Eric Dumazet48855432011-10-24 07:53:03 +000010308 stats->tx_dropped = tp->tx_dropped;
Eric Dumazetb0057c52010-10-10 19:55:52 +000010309
Linus Torvalds1da177e2005-04-16 15:20:36 -070010310 return stats;
10311}
10312
Linus Torvalds1da177e2005-04-16 15:20:36 -070010313static int tg3_get_regs_len(struct net_device *dev)
10314{
Matt Carlson97bd8e42011-04-13 11:05:04 +000010315 return TG3_REG_BLK_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010316}
10317
10318static void tg3_get_regs(struct net_device *dev,
10319 struct ethtool_regs *regs, void *_p)
10320{
Linus Torvalds1da177e2005-04-16 15:20:36 -070010321 struct tg3 *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010322
10323 regs->version = 0;
10324
Matt Carlson97bd8e42011-04-13 11:05:04 +000010325 memset(_p, 0, TG3_REG_BLK_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010326
Matt Carlson800960682010-08-02 11:26:06 +000010327 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Michael Chanbc1c7562006-03-20 17:48:03 -080010328 return;
10329
David S. Millerf47c11e2005-06-24 20:18:35 -070010330 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010331
Matt Carlson97bd8e42011-04-13 11:05:04 +000010332 tg3_dump_legacy_regs(tp, (u32 *)_p);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010333
David S. Millerf47c11e2005-06-24 20:18:35 -070010334 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010335}
10336
10337static int tg3_get_eeprom_len(struct net_device *dev)
10338{
10339 struct tg3 *tp = netdev_priv(dev);
10340
10341 return tp->nvram_size;
10342}
10343
Linus Torvalds1da177e2005-04-16 15:20:36 -070010344static int tg3_get_eeprom(struct net_device *dev, struct ethtool_eeprom *eeprom, u8 *data)
10345{
10346 struct tg3 *tp = netdev_priv(dev);
10347 int ret;
10348 u8 *pd;
Al Virob9fc7dc2007-12-17 22:59:57 -080010349 u32 i, offset, len, b_offset, b_count;
Matt Carlsona9dc5292009-02-25 14:25:30 +000010350 __be32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010351
Joe Perches63c3a662011-04-26 08:12:10 +000010352 if (tg3_flag(tp, NO_NVRAM))
Matt Carlsondf259d82009-04-20 06:57:14 +000010353 return -EINVAL;
10354
Matt Carlson800960682010-08-02 11:26:06 +000010355 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Michael Chanbc1c7562006-03-20 17:48:03 -080010356 return -EAGAIN;
10357
Linus Torvalds1da177e2005-04-16 15:20:36 -070010358 offset = eeprom->offset;
10359 len = eeprom->len;
10360 eeprom->len = 0;
10361
10362 eeprom->magic = TG3_EEPROM_MAGIC;
10363
10364 if (offset & 3) {
10365 /* adjustments to start on required 4 byte boundary */
10366 b_offset = offset & 3;
10367 b_count = 4 - b_offset;
10368 if (b_count > len) {
10369 /* i.e. offset=1 len=2 */
10370 b_count = len;
10371 }
Matt Carlsona9dc5292009-02-25 14:25:30 +000010372 ret = tg3_nvram_read_be32(tp, offset-b_offset, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010373 if (ret)
10374 return ret;
Matt Carlsonbe98da62010-07-11 09:31:46 +000010375 memcpy(data, ((char *)&val) + b_offset, b_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010376 len -= b_count;
10377 offset += b_count;
Matt Carlsonc6cdf432010-04-05 10:19:26 +000010378 eeprom->len += b_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010379 }
10380
Lucas De Marchi25985ed2011-03-30 22:57:33 -030010381 /* read bytes up to the last 4 byte boundary */
Linus Torvalds1da177e2005-04-16 15:20:36 -070010382 pd = &data[eeprom->len];
10383 for (i = 0; i < (len - (len & 3)); i += 4) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000010384 ret = tg3_nvram_read_be32(tp, offset + i, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010385 if (ret) {
10386 eeprom->len += i;
10387 return ret;
10388 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010389 memcpy(pd + i, &val, 4);
10390 }
10391 eeprom->len += i;
10392
10393 if (len & 3) {
10394 /* read last bytes not ending on 4 byte boundary */
10395 pd = &data[eeprom->len];
10396 b_count = len & 3;
10397 b_offset = offset + len - b_count;
Matt Carlsona9dc5292009-02-25 14:25:30 +000010398 ret = tg3_nvram_read_be32(tp, b_offset, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010399 if (ret)
10400 return ret;
Al Virob9fc7dc2007-12-17 22:59:57 -080010401 memcpy(pd, &val, b_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010402 eeprom->len += b_count;
10403 }
10404 return 0;
10405}
10406
Linus Torvalds1da177e2005-04-16 15:20:36 -070010407static int tg3_set_eeprom(struct net_device *dev, struct ethtool_eeprom *eeprom, u8 *data)
10408{
10409 struct tg3 *tp = netdev_priv(dev);
10410 int ret;
Al Virob9fc7dc2007-12-17 22:59:57 -080010411 u32 offset, len, b_offset, odd_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010412 u8 *buf;
Matt Carlsona9dc5292009-02-25 14:25:30 +000010413 __be32 start, end;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010414
Matt Carlson800960682010-08-02 11:26:06 +000010415 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Michael Chanbc1c7562006-03-20 17:48:03 -080010416 return -EAGAIN;
10417
Joe Perches63c3a662011-04-26 08:12:10 +000010418 if (tg3_flag(tp, NO_NVRAM) ||
Matt Carlsondf259d82009-04-20 06:57:14 +000010419 eeprom->magic != TG3_EEPROM_MAGIC)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010420 return -EINVAL;
10421
10422 offset = eeprom->offset;
10423 len = eeprom->len;
10424
10425 if ((b_offset = (offset & 3))) {
10426 /* adjustments to start on required 4 byte boundary */
Matt Carlsona9dc5292009-02-25 14:25:30 +000010427 ret = tg3_nvram_read_be32(tp, offset-b_offset, &start);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010428 if (ret)
10429 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010430 len += b_offset;
10431 offset &= ~3;
Michael Chan1c8594b2005-04-21 17:12:46 -070010432 if (len < 4)
10433 len = 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010434 }
10435
10436 odd_len = 0;
Michael Chan1c8594b2005-04-21 17:12:46 -070010437 if (len & 3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010438 /* adjustments to end on required 4 byte boundary */
10439 odd_len = 1;
10440 len = (len + 3) & ~3;
Matt Carlsona9dc5292009-02-25 14:25:30 +000010441 ret = tg3_nvram_read_be32(tp, offset+len-4, &end);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010442 if (ret)
10443 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010444 }
10445
10446 buf = data;
10447 if (b_offset || odd_len) {
10448 buf = kmalloc(len, GFP_KERNEL);
Andy Gospodarekab0049b2007-09-06 20:42:14 +010010449 if (!buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010450 return -ENOMEM;
10451 if (b_offset)
10452 memcpy(buf, &start, 4);
10453 if (odd_len)
10454 memcpy(buf+len-4, &end, 4);
10455 memcpy(buf + b_offset, data, eeprom->len);
10456 }
10457
10458 ret = tg3_nvram_write_block(tp, offset, len, buf);
10459
10460 if (buf != data)
10461 kfree(buf);
10462
10463 return ret;
10464}
10465
10466static int tg3_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
10467{
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010468 struct tg3 *tp = netdev_priv(dev);
10469
Joe Perches63c3a662011-04-26 08:12:10 +000010470 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000010471 struct phy_device *phydev;
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010472 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010473 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000010474 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
10475 return phy_ethtool_gset(phydev, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010476 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010477
Linus Torvalds1da177e2005-04-16 15:20:36 -070010478 cmd->supported = (SUPPORTED_Autoneg);
10479
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010480 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010481 cmd->supported |= (SUPPORTED_1000baseT_Half |
10482 SUPPORTED_1000baseT_Full);
10483
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010484 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010485 cmd->supported |= (SUPPORTED_100baseT_Half |
10486 SUPPORTED_100baseT_Full |
10487 SUPPORTED_10baseT_Half |
10488 SUPPORTED_10baseT_Full |
Matt Carlson3bebab52007-11-12 21:22:40 -080010489 SUPPORTED_TP);
Karsten Keilef348142006-05-12 12:49:08 -070010490 cmd->port = PORT_TP;
10491 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010492 cmd->supported |= SUPPORTED_FIBRE;
Karsten Keilef348142006-05-12 12:49:08 -070010493 cmd->port = PORT_FIBRE;
10494 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010495
Linus Torvalds1da177e2005-04-16 15:20:36 -070010496 cmd->advertising = tp->link_config.advertising;
Matt Carlson5bb09772011-06-13 13:39:00 +000010497 if (tg3_flag(tp, PAUSE_AUTONEG)) {
10498 if (tp->link_config.flowctrl & FLOW_CTRL_RX) {
10499 if (tp->link_config.flowctrl & FLOW_CTRL_TX) {
10500 cmd->advertising |= ADVERTISED_Pause;
10501 } else {
10502 cmd->advertising |= ADVERTISED_Pause |
10503 ADVERTISED_Asym_Pause;
10504 }
10505 } else if (tp->link_config.flowctrl & FLOW_CTRL_TX) {
10506 cmd->advertising |= ADVERTISED_Asym_Pause;
10507 }
10508 }
Matt Carlson859edb22011-12-08 14:40:16 +000010509 if (netif_running(dev) && netif_carrier_ok(dev)) {
David Decotigny70739492011-04-27 18:32:40 +000010510 ethtool_cmd_speed_set(cmd, tp->link_config.active_speed);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010511 cmd->duplex = tp->link_config.active_duplex;
Matt Carlson859edb22011-12-08 14:40:16 +000010512 cmd->lp_advertising = tp->link_config.rmt_adv;
Matt Carlsone348c5e2011-11-21 15:01:20 +000010513 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES)) {
10514 if (tp->phy_flags & TG3_PHYFLG_MDIX_STATE)
10515 cmd->eth_tp_mdix = ETH_TP_MDI_X;
10516 else
10517 cmd->eth_tp_mdix = ETH_TP_MDI;
10518 }
Matt Carlson64c22182010-10-14 10:37:44 +000010519 } else {
David Decotigny70739492011-04-27 18:32:40 +000010520 ethtool_cmd_speed_set(cmd, SPEED_INVALID);
Matt Carlson64c22182010-10-14 10:37:44 +000010521 cmd->duplex = DUPLEX_INVALID;
Matt Carlsone348c5e2011-11-21 15:01:20 +000010522 cmd->eth_tp_mdix = ETH_TP_MDI_INVALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010523 }
Matt Carlson882e9792009-09-01 13:21:36 +000010524 cmd->phy_address = tp->phy_addr;
Matt Carlson7e5856b2009-02-25 14:23:01 +000010525 cmd->transceiver = XCVR_INTERNAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010526 cmd->autoneg = tp->link_config.autoneg;
10527 cmd->maxtxpkt = 0;
10528 cmd->maxrxpkt = 0;
10529 return 0;
10530}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010531
Linus Torvalds1da177e2005-04-16 15:20:36 -070010532static int tg3_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
10533{
10534 struct tg3 *tp = netdev_priv(dev);
David Decotigny25db0332011-04-27 18:32:39 +000010535 u32 speed = ethtool_cmd_speed(cmd);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010536
Joe Perches63c3a662011-04-26 08:12:10 +000010537 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000010538 struct phy_device *phydev;
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010539 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010540 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000010541 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
10542 return phy_ethtool_sset(phydev, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010543 }
10544
Matt Carlson7e5856b2009-02-25 14:23:01 +000010545 if (cmd->autoneg != AUTONEG_ENABLE &&
10546 cmd->autoneg != AUTONEG_DISABLE)
Michael Chan37ff2382005-10-26 15:49:51 -070010547 return -EINVAL;
Matt Carlson7e5856b2009-02-25 14:23:01 +000010548
10549 if (cmd->autoneg == AUTONEG_DISABLE &&
10550 cmd->duplex != DUPLEX_FULL &&
10551 cmd->duplex != DUPLEX_HALF)
Michael Chan37ff2382005-10-26 15:49:51 -070010552 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010553
Matt Carlson7e5856b2009-02-25 14:23:01 +000010554 if (cmd->autoneg == AUTONEG_ENABLE) {
10555 u32 mask = ADVERTISED_Autoneg |
10556 ADVERTISED_Pause |
10557 ADVERTISED_Asym_Pause;
10558
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010559 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
Matt Carlson7e5856b2009-02-25 14:23:01 +000010560 mask |= ADVERTISED_1000baseT_Half |
10561 ADVERTISED_1000baseT_Full;
10562
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010563 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
Matt Carlson7e5856b2009-02-25 14:23:01 +000010564 mask |= ADVERTISED_100baseT_Half |
10565 ADVERTISED_100baseT_Full |
10566 ADVERTISED_10baseT_Half |
10567 ADVERTISED_10baseT_Full |
10568 ADVERTISED_TP;
10569 else
10570 mask |= ADVERTISED_FIBRE;
10571
10572 if (cmd->advertising & ~mask)
10573 return -EINVAL;
10574
10575 mask &= (ADVERTISED_1000baseT_Half |
10576 ADVERTISED_1000baseT_Full |
10577 ADVERTISED_100baseT_Half |
10578 ADVERTISED_100baseT_Full |
10579 ADVERTISED_10baseT_Half |
10580 ADVERTISED_10baseT_Full);
10581
10582 cmd->advertising &= mask;
10583 } else {
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010584 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES) {
David Decotigny25db0332011-04-27 18:32:39 +000010585 if (speed != SPEED_1000)
Matt Carlson7e5856b2009-02-25 14:23:01 +000010586 return -EINVAL;
10587
10588 if (cmd->duplex != DUPLEX_FULL)
10589 return -EINVAL;
10590 } else {
David Decotigny25db0332011-04-27 18:32:39 +000010591 if (speed != SPEED_100 &&
10592 speed != SPEED_10)
Matt Carlson7e5856b2009-02-25 14:23:01 +000010593 return -EINVAL;
10594 }
10595 }
10596
David S. Millerf47c11e2005-06-24 20:18:35 -070010597 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010598
10599 tp->link_config.autoneg = cmd->autoneg;
10600 if (cmd->autoneg == AUTONEG_ENABLE) {
Andy Gospodarek405d8e52007-10-08 01:08:47 -070010601 tp->link_config.advertising = (cmd->advertising |
10602 ADVERTISED_Autoneg);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010603 tp->link_config.speed = SPEED_INVALID;
10604 tp->link_config.duplex = DUPLEX_INVALID;
10605 } else {
10606 tp->link_config.advertising = 0;
David Decotigny25db0332011-04-27 18:32:39 +000010607 tp->link_config.speed = speed;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010608 tp->link_config.duplex = cmd->duplex;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010609 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010610
Michael Chan24fcad62006-12-17 17:06:46 -080010611 tp->link_config.orig_speed = tp->link_config.speed;
10612 tp->link_config.orig_duplex = tp->link_config.duplex;
10613 tp->link_config.orig_autoneg = tp->link_config.autoneg;
10614
Linus Torvalds1da177e2005-04-16 15:20:36 -070010615 if (netif_running(dev))
10616 tg3_setup_phy(tp, 1);
10617
David S. Millerf47c11e2005-06-24 20:18:35 -070010618 tg3_full_unlock(tp);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010619
Linus Torvalds1da177e2005-04-16 15:20:36 -070010620 return 0;
10621}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010622
Linus Torvalds1da177e2005-04-16 15:20:36 -070010623static void tg3_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
10624{
10625 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010626
Rick Jones68aad782011-11-07 13:29:27 +000010627 strlcpy(info->driver, DRV_MODULE_NAME, sizeof(info->driver));
10628 strlcpy(info->version, DRV_MODULE_VERSION, sizeof(info->version));
10629 strlcpy(info->fw_version, tp->fw_ver, sizeof(info->fw_version));
10630 strlcpy(info->bus_info, pci_name(tp->pdev), sizeof(info->bus_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -070010631}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010632
Linus Torvalds1da177e2005-04-16 15:20:36 -070010633static void tg3_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
10634{
10635 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010636
Joe Perches63c3a662011-04-26 08:12:10 +000010637 if (tg3_flag(tp, WOL_CAP) && device_can_wakeup(&tp->pdev->dev))
Gary Zambranoa85feb82007-05-05 11:52:19 -070010638 wol->supported = WAKE_MAGIC;
10639 else
10640 wol->supported = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010641 wol->wolopts = 0;
Joe Perches63c3a662011-04-26 08:12:10 +000010642 if (tg3_flag(tp, WOL_ENABLE) && device_can_wakeup(&tp->pdev->dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010643 wol->wolopts = WAKE_MAGIC;
10644 memset(&wol->sopass, 0, sizeof(wol->sopass));
10645}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010646
Linus Torvalds1da177e2005-04-16 15:20:36 -070010647static int tg3_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
10648{
10649 struct tg3 *tp = netdev_priv(dev);
Rafael J. Wysocki12dac072008-07-30 16:37:33 -070010650 struct device *dp = &tp->pdev->dev;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010651
Linus Torvalds1da177e2005-04-16 15:20:36 -070010652 if (wol->wolopts & ~WAKE_MAGIC)
10653 return -EINVAL;
10654 if ((wol->wolopts & WAKE_MAGIC) &&
Joe Perches63c3a662011-04-26 08:12:10 +000010655 !(tg3_flag(tp, WOL_CAP) && device_can_wakeup(dp)))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010656 return -EINVAL;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010657
Rafael J. Wysockif2dc0d12010-10-25 13:01:55 +000010658 device_set_wakeup_enable(dp, wol->wolopts & WAKE_MAGIC);
10659
David S. Millerf47c11e2005-06-24 20:18:35 -070010660 spin_lock_bh(&tp->lock);
Rafael J. Wysockif2dc0d12010-10-25 13:01:55 +000010661 if (device_may_wakeup(dp))
Joe Perches63c3a662011-04-26 08:12:10 +000010662 tg3_flag_set(tp, WOL_ENABLE);
Rafael J. Wysockif2dc0d12010-10-25 13:01:55 +000010663 else
Joe Perches63c3a662011-04-26 08:12:10 +000010664 tg3_flag_clear(tp, WOL_ENABLE);
David S. Millerf47c11e2005-06-24 20:18:35 -070010665 spin_unlock_bh(&tp->lock);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010666
Linus Torvalds1da177e2005-04-16 15:20:36 -070010667 return 0;
10668}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010669
Linus Torvalds1da177e2005-04-16 15:20:36 -070010670static u32 tg3_get_msglevel(struct net_device *dev)
10671{
10672 struct tg3 *tp = netdev_priv(dev);
10673 return tp->msg_enable;
10674}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010675
Linus Torvalds1da177e2005-04-16 15:20:36 -070010676static void tg3_set_msglevel(struct net_device *dev, u32 value)
10677{
10678 struct tg3 *tp = netdev_priv(dev);
10679 tp->msg_enable = value;
10680}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010681
Linus Torvalds1da177e2005-04-16 15:20:36 -070010682static int tg3_nway_reset(struct net_device *dev)
10683{
10684 struct tg3 *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010685 int r;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010686
Linus Torvalds1da177e2005-04-16 15:20:36 -070010687 if (!netif_running(dev))
10688 return -EAGAIN;
10689
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010690 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Michael Chanc94e3942005-09-27 12:12:42 -070010691 return -EINVAL;
10692
Joe Perches63c3a662011-04-26 08:12:10 +000010693 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010694 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010695 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000010696 r = phy_start_aneg(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010697 } else {
10698 u32 bmcr;
10699
10700 spin_lock_bh(&tp->lock);
10701 r = -EINVAL;
10702 tg3_readphy(tp, MII_BMCR, &bmcr);
10703 if (!tg3_readphy(tp, MII_BMCR, &bmcr) &&
10704 ((bmcr & BMCR_ANENABLE) ||
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010705 (tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT))) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010706 tg3_writephy(tp, MII_BMCR, bmcr | BMCR_ANRESTART |
10707 BMCR_ANENABLE);
10708 r = 0;
10709 }
10710 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010711 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010712
Linus Torvalds1da177e2005-04-16 15:20:36 -070010713 return r;
10714}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010715
Linus Torvalds1da177e2005-04-16 15:20:36 -070010716static void tg3_get_ringparam(struct net_device *dev, struct ethtool_ringparam *ering)
10717{
10718 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010719
Matt Carlson2c49a442010-09-30 10:34:35 +000010720 ering->rx_max_pending = tp->rx_std_ring_mask;
Joe Perches63c3a662011-04-26 08:12:10 +000010721 if (tg3_flag(tp, JUMBO_RING_ENABLE))
Matt Carlson2c49a442010-09-30 10:34:35 +000010722 ering->rx_jumbo_max_pending = tp->rx_jmb_ring_mask;
Michael Chan4f81c322006-03-20 21:33:42 -080010723 else
10724 ering->rx_jumbo_max_pending = 0;
10725
10726 ering->tx_max_pending = TG3_TX_RING_SIZE - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010727
10728 ering->rx_pending = tp->rx_pending;
Joe Perches63c3a662011-04-26 08:12:10 +000010729 if (tg3_flag(tp, JUMBO_RING_ENABLE))
Michael Chan4f81c322006-03-20 21:33:42 -080010730 ering->rx_jumbo_pending = tp->rx_jumbo_pending;
10731 else
10732 ering->rx_jumbo_pending = 0;
10733
Matt Carlsonf3f3f272009-08-28 14:03:21 +000010734 ering->tx_pending = tp->napi[0].tx_pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010735}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010736
Linus Torvalds1da177e2005-04-16 15:20:36 -070010737static int tg3_set_ringparam(struct net_device *dev, struct ethtool_ringparam *ering)
10738{
10739 struct tg3 *tp = netdev_priv(dev);
Matt Carlson646c9ed2009-09-01 12:58:41 +000010740 int i, irq_sync = 0, err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010741
Matt Carlson2c49a442010-09-30 10:34:35 +000010742 if ((ering->rx_pending > tp->rx_std_ring_mask) ||
10743 (ering->rx_jumbo_pending > tp->rx_jmb_ring_mask) ||
Michael Chanbc3a9252006-10-18 20:55:18 -070010744 (ering->tx_pending > TG3_TX_RING_SIZE - 1) ||
10745 (ering->tx_pending <= MAX_SKB_FRAGS) ||
Joe Perches63c3a662011-04-26 08:12:10 +000010746 (tg3_flag(tp, TSO_BUG) &&
Michael Chanbc3a9252006-10-18 20:55:18 -070010747 (ering->tx_pending <= (MAX_SKB_FRAGS * 3))))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010748 return -EINVAL;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010749
Michael Chanbbe832c2005-06-24 20:20:04 -070010750 if (netif_running(dev)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010751 tg3_phy_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010752 tg3_netif_stop(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -070010753 irq_sync = 1;
10754 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010755
Michael Chanbbe832c2005-06-24 20:20:04 -070010756 tg3_full_lock(tp, irq_sync);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010757
Linus Torvalds1da177e2005-04-16 15:20:36 -070010758 tp->rx_pending = ering->rx_pending;
10759
Joe Perches63c3a662011-04-26 08:12:10 +000010760 if (tg3_flag(tp, MAX_RXPEND_64) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070010761 tp->rx_pending > 63)
10762 tp->rx_pending = 63;
10763 tp->rx_jumbo_pending = ering->rx_jumbo_pending;
Matt Carlson646c9ed2009-09-01 12:58:41 +000010764
Matt Carlson6fd45cb2010-09-15 08:59:57 +000010765 for (i = 0; i < tp->irq_max; i++)
Matt Carlson646c9ed2009-09-01 12:58:41 +000010766 tp->napi[i].tx_pending = ering->tx_pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010767
10768 if (netif_running(dev)) {
Michael Chan944d9802005-05-29 14:57:48 -070010769 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Michael Chanb9ec6c12006-07-25 16:37:27 -070010770 err = tg3_restart_hw(tp, 1);
10771 if (!err)
10772 tg3_netif_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010773 }
10774
David S. Millerf47c11e2005-06-24 20:18:35 -070010775 tg3_full_unlock(tp);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010776
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010777 if (irq_sync && !err)
10778 tg3_phy_start(tp);
10779
Michael Chanb9ec6c12006-07-25 16:37:27 -070010780 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010781}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010782
Linus Torvalds1da177e2005-04-16 15:20:36 -070010783static void tg3_get_pauseparam(struct net_device *dev, struct ethtool_pauseparam *epause)
10784{
10785 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010786
Joe Perches63c3a662011-04-26 08:12:10 +000010787 epause->autoneg = !!tg3_flag(tp, PAUSE_AUTONEG);
Matt Carlson8d018622007-12-20 20:05:44 -080010788
Matt Carlson4a2db502011-12-08 14:40:17 +000010789 if (tp->link_config.flowctrl & FLOW_CTRL_RX)
Matt Carlson8d018622007-12-20 20:05:44 -080010790 epause->rx_pause = 1;
10791 else
10792 epause->rx_pause = 0;
10793
Matt Carlson4a2db502011-12-08 14:40:17 +000010794 if (tp->link_config.flowctrl & FLOW_CTRL_TX)
Matt Carlson8d018622007-12-20 20:05:44 -080010795 epause->tx_pause = 1;
10796 else
10797 epause->tx_pause = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010798}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010799
Linus Torvalds1da177e2005-04-16 15:20:36 -070010800static int tg3_set_pauseparam(struct net_device *dev, struct ethtool_pauseparam *epause)
10801{
10802 struct tg3 *tp = netdev_priv(dev);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010803 int err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010804
Joe Perches63c3a662011-04-26 08:12:10 +000010805 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson27121682010-02-17 15:16:57 +000010806 u32 newadv;
10807 struct phy_device *phydev;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010808
Matt Carlson27121682010-02-17 15:16:57 +000010809 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010810
Matt Carlson27121682010-02-17 15:16:57 +000010811 if (!(phydev->supported & SUPPORTED_Pause) ||
10812 (!(phydev->supported & SUPPORTED_Asym_Pause) &&
Nicolas Kaiser2259dca2010-10-07 23:29:27 +000010813 (epause->rx_pause != epause->tx_pause)))
Matt Carlson27121682010-02-17 15:16:57 +000010814 return -EINVAL;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010815
Matt Carlson27121682010-02-17 15:16:57 +000010816 tp->link_config.flowctrl = 0;
10817 if (epause->rx_pause) {
10818 tp->link_config.flowctrl |= FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010819
Matt Carlson27121682010-02-17 15:16:57 +000010820 if (epause->tx_pause) {
Steve Glendinninge18ce342008-12-16 02:00:00 -080010821 tp->link_config.flowctrl |= FLOW_CTRL_TX;
Matt Carlson27121682010-02-17 15:16:57 +000010822 newadv = ADVERTISED_Pause;
10823 } else
10824 newadv = ADVERTISED_Pause |
10825 ADVERTISED_Asym_Pause;
10826 } else if (epause->tx_pause) {
10827 tp->link_config.flowctrl |= FLOW_CTRL_TX;
10828 newadv = ADVERTISED_Asym_Pause;
10829 } else
10830 newadv = 0;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010831
Matt Carlson27121682010-02-17 15:16:57 +000010832 if (epause->autoneg)
Joe Perches63c3a662011-04-26 08:12:10 +000010833 tg3_flag_set(tp, PAUSE_AUTONEG);
Matt Carlson27121682010-02-17 15:16:57 +000010834 else
Joe Perches63c3a662011-04-26 08:12:10 +000010835 tg3_flag_clear(tp, PAUSE_AUTONEG);
Matt Carlson27121682010-02-17 15:16:57 +000010836
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010837 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) {
Matt Carlson27121682010-02-17 15:16:57 +000010838 u32 oldadv = phydev->advertising &
10839 (ADVERTISED_Pause | ADVERTISED_Asym_Pause);
10840 if (oldadv != newadv) {
10841 phydev->advertising &=
10842 ~(ADVERTISED_Pause |
10843 ADVERTISED_Asym_Pause);
10844 phydev->advertising |= newadv;
10845 if (phydev->autoneg) {
10846 /*
10847 * Always renegotiate the link to
10848 * inform our link partner of our
10849 * flow control settings, even if the
10850 * flow control is forced. Let
10851 * tg3_adjust_link() do the final
10852 * flow control setup.
10853 */
10854 return phy_start_aneg(phydev);
10855 }
10856 }
10857
10858 if (!epause->autoneg)
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010859 tg3_setup_flow_control(tp, 0, 0);
Matt Carlson27121682010-02-17 15:16:57 +000010860 } else {
10861 tp->link_config.orig_advertising &=
10862 ~(ADVERTISED_Pause |
10863 ADVERTISED_Asym_Pause);
10864 tp->link_config.orig_advertising |= newadv;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010865 }
10866 } else {
10867 int irq_sync = 0;
10868
10869 if (netif_running(dev)) {
10870 tg3_netif_stop(tp);
10871 irq_sync = 1;
10872 }
10873
10874 tg3_full_lock(tp, irq_sync);
10875
10876 if (epause->autoneg)
Joe Perches63c3a662011-04-26 08:12:10 +000010877 tg3_flag_set(tp, PAUSE_AUTONEG);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010878 else
Joe Perches63c3a662011-04-26 08:12:10 +000010879 tg3_flag_clear(tp, PAUSE_AUTONEG);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010880 if (epause->rx_pause)
Steve Glendinninge18ce342008-12-16 02:00:00 -080010881 tp->link_config.flowctrl |= FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010882 else
Steve Glendinninge18ce342008-12-16 02:00:00 -080010883 tp->link_config.flowctrl &= ~FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010884 if (epause->tx_pause)
Steve Glendinninge18ce342008-12-16 02:00:00 -080010885 tp->link_config.flowctrl |= FLOW_CTRL_TX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010886 else
Steve Glendinninge18ce342008-12-16 02:00:00 -080010887 tp->link_config.flowctrl &= ~FLOW_CTRL_TX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010888
10889 if (netif_running(dev)) {
10890 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
10891 err = tg3_restart_hw(tp, 1);
10892 if (!err)
10893 tg3_netif_start(tp);
10894 }
10895
10896 tg3_full_unlock(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -070010897 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010898
Michael Chanb9ec6c12006-07-25 16:37:27 -070010899 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010900}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010901
Matt Carlsonde6f31e2010-04-12 06:58:30 +000010902static int tg3_get_sset_count(struct net_device *dev, int sset)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010903{
Jeff Garzikb9f2c042007-10-03 18:07:32 -070010904 switch (sset) {
10905 case ETH_SS_TEST:
10906 return TG3_NUM_TEST;
10907 case ETH_SS_STATS:
10908 return TG3_NUM_STATS;
10909 default:
10910 return -EOPNOTSUPP;
10911 }
Michael Chan4cafd3f2005-05-29 14:56:34 -070010912}
10913
Matt Carlson90415472011-12-16 13:33:23 +000010914static int tg3_get_rxnfc(struct net_device *dev, struct ethtool_rxnfc *info,
10915 u32 *rules __always_unused)
10916{
10917 struct tg3 *tp = netdev_priv(dev);
10918
10919 if (!tg3_flag(tp, SUPPORT_MSIX))
10920 return -EOPNOTSUPP;
10921
10922 switch (info->cmd) {
10923 case ETHTOOL_GRXRINGS:
10924 if (netif_running(tp->dev))
10925 info->data = tp->irq_cnt;
10926 else {
10927 info->data = num_online_cpus();
10928 if (info->data > TG3_IRQ_MAX_VECS_RSS)
10929 info->data = TG3_IRQ_MAX_VECS_RSS;
10930 }
10931
10932 /* The first interrupt vector only
10933 * handles link interrupts.
10934 */
10935 info->data -= 1;
10936 return 0;
10937
10938 default:
10939 return -EOPNOTSUPP;
10940 }
10941}
10942
10943static u32 tg3_get_rxfh_indir_size(struct net_device *dev)
10944{
10945 u32 size = 0;
10946 struct tg3 *tp = netdev_priv(dev);
10947
10948 if (tg3_flag(tp, SUPPORT_MSIX))
10949 size = TG3_RSS_INDIR_TBL_SIZE;
10950
10951 return size;
10952}
10953
10954static int tg3_get_rxfh_indir(struct net_device *dev, u32 *indir)
10955{
10956 struct tg3 *tp = netdev_priv(dev);
10957 int i;
10958
10959 for (i = 0; i < TG3_RSS_INDIR_TBL_SIZE; i++)
10960 indir[i] = tp->rss_ind_tbl[i];
10961
10962 return 0;
10963}
10964
10965static int tg3_set_rxfh_indir(struct net_device *dev, const u32 *indir)
10966{
10967 struct tg3 *tp = netdev_priv(dev);
10968 size_t i;
10969
10970 for (i = 0; i < TG3_RSS_INDIR_TBL_SIZE; i++)
10971 tp->rss_ind_tbl[i] = indir[i];
10972
10973 if (!netif_running(dev) || !tg3_flag(tp, ENABLE_RSS))
10974 return 0;
10975
10976 /* It is legal to write the indirection
10977 * table while the device is running.
10978 */
10979 tg3_full_lock(tp, 0);
10980 tg3_rss_write_indir_tbl(tp);
10981 tg3_full_unlock(tp);
10982
10983 return 0;
10984}
10985
Matt Carlsonde6f31e2010-04-12 06:58:30 +000010986static void tg3_get_strings(struct net_device *dev, u32 stringset, u8 *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010987{
10988 switch (stringset) {
10989 case ETH_SS_STATS:
10990 memcpy(buf, &ethtool_stats_keys, sizeof(ethtool_stats_keys));
10991 break;
Michael Chan4cafd3f2005-05-29 14:56:34 -070010992 case ETH_SS_TEST:
10993 memcpy(buf, &ethtool_test_keys, sizeof(ethtool_test_keys));
10994 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010995 default:
10996 WARN_ON(1); /* we need a WARN() */
10997 break;
10998 }
10999}
11000
stephen hemminger81b87092011-04-04 08:43:50 +000011001static int tg3_set_phys_id(struct net_device *dev,
11002 enum ethtool_phys_id_state state)
Michael Chan4009a932005-09-05 17:52:54 -070011003{
11004 struct tg3 *tp = netdev_priv(dev);
Michael Chan4009a932005-09-05 17:52:54 -070011005
11006 if (!netif_running(tp->dev))
11007 return -EAGAIN;
11008
stephen hemminger81b87092011-04-04 08:43:50 +000011009 switch (state) {
11010 case ETHTOOL_ID_ACTIVE:
Allan, Bruce Wfce55922011-04-13 13:09:10 +000011011 return 1; /* cycle on/off once per second */
Michael Chan4009a932005-09-05 17:52:54 -070011012
stephen hemminger81b87092011-04-04 08:43:50 +000011013 case ETHTOOL_ID_ON:
11014 tw32(MAC_LED_CTRL, LED_CTRL_LNKLED_OVERRIDE |
11015 LED_CTRL_1000MBPS_ON |
11016 LED_CTRL_100MBPS_ON |
11017 LED_CTRL_10MBPS_ON |
11018 LED_CTRL_TRAFFIC_OVERRIDE |
11019 LED_CTRL_TRAFFIC_BLINK |
11020 LED_CTRL_TRAFFIC_LED);
11021 break;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011022
stephen hemminger81b87092011-04-04 08:43:50 +000011023 case ETHTOOL_ID_OFF:
11024 tw32(MAC_LED_CTRL, LED_CTRL_LNKLED_OVERRIDE |
11025 LED_CTRL_TRAFFIC_OVERRIDE);
11026 break;
Michael Chan4009a932005-09-05 17:52:54 -070011027
stephen hemminger81b87092011-04-04 08:43:50 +000011028 case ETHTOOL_ID_INACTIVE:
11029 tw32(MAC_LED_CTRL, tp->led_ctrl);
11030 break;
Michael Chan4009a932005-09-05 17:52:54 -070011031 }
stephen hemminger81b87092011-04-04 08:43:50 +000011032
Michael Chan4009a932005-09-05 17:52:54 -070011033 return 0;
11034}
11035
Matt Carlsonde6f31e2010-04-12 06:58:30 +000011036static void tg3_get_ethtool_stats(struct net_device *dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -070011037 struct ethtool_stats *estats, u64 *tmp_stats)
11038{
11039 struct tg3 *tp = netdev_priv(dev);
Matt Carlson0e6c9da2011-12-08 14:40:13 +000011040
Matt Carlsonb546e462012-02-13 15:20:09 +000011041 if (tp->hw_stats)
11042 tg3_get_estats(tp, (struct tg3_ethtool_stats *)tmp_stats);
11043 else
11044 memset(tmp_stats, 0, sizeof(struct tg3_ethtool_stats));
Linus Torvalds1da177e2005-04-16 15:20:36 -070011045}
11046
Matt Carlson535a4902011-07-20 10:20:56 +000011047static __be32 *tg3_vpd_readblock(struct tg3 *tp, u32 *vpdlen)
Matt Carlsonc3e94502011-04-13 11:05:08 +000011048{
11049 int i;
11050 __be32 *buf;
11051 u32 offset = 0, len = 0;
11052 u32 magic, val;
11053
Joe Perches63c3a662011-04-26 08:12:10 +000011054 if (tg3_flag(tp, NO_NVRAM) || tg3_nvram_read(tp, 0, &magic))
Matt Carlsonc3e94502011-04-13 11:05:08 +000011055 return NULL;
11056
11057 if (magic == TG3_EEPROM_MAGIC) {
11058 for (offset = TG3_NVM_DIR_START;
11059 offset < TG3_NVM_DIR_END;
11060 offset += TG3_NVM_DIRENT_SIZE) {
11061 if (tg3_nvram_read(tp, offset, &val))
11062 return NULL;
11063
11064 if ((val >> TG3_NVM_DIRTYPE_SHIFT) ==
11065 TG3_NVM_DIRTYPE_EXTVPD)
11066 break;
11067 }
11068
11069 if (offset != TG3_NVM_DIR_END) {
11070 len = (val & TG3_NVM_DIRTYPE_LENMSK) * 4;
11071 if (tg3_nvram_read(tp, offset + 4, &offset))
11072 return NULL;
11073
11074 offset = tg3_nvram_logical_addr(tp, offset);
11075 }
11076 }
11077
11078 if (!offset || !len) {
11079 offset = TG3_NVM_VPD_OFF;
11080 len = TG3_NVM_VPD_LEN;
11081 }
11082
11083 buf = kmalloc(len, GFP_KERNEL);
11084 if (buf == NULL)
11085 return NULL;
11086
11087 if (magic == TG3_EEPROM_MAGIC) {
11088 for (i = 0; i < len; i += 4) {
11089 /* The data is in little-endian format in NVRAM.
11090 * Use the big-endian read routines to preserve
11091 * the byte order as it exists in NVRAM.
11092 */
11093 if (tg3_nvram_read_be32(tp, offset + i, &buf[i/4]))
11094 goto error;
11095 }
11096 } else {
11097 u8 *ptr;
11098 ssize_t cnt;
11099 unsigned int pos = 0;
11100
11101 ptr = (u8 *)&buf[0];
11102 for (i = 0; pos < len && i < 3; i++, pos += cnt, ptr += cnt) {
11103 cnt = pci_read_vpd(tp->pdev, pos,
11104 len - pos, ptr);
11105 if (cnt == -ETIMEDOUT || cnt == -EINTR)
11106 cnt = 0;
11107 else if (cnt < 0)
11108 goto error;
11109 }
11110 if (pos != len)
11111 goto error;
11112 }
11113
Matt Carlson535a4902011-07-20 10:20:56 +000011114 *vpdlen = len;
11115
Matt Carlsonc3e94502011-04-13 11:05:08 +000011116 return buf;
11117
11118error:
11119 kfree(buf);
11120 return NULL;
11121}
11122
Michael Chan566f86a2005-05-29 14:56:58 -070011123#define NVRAM_TEST_SIZE 0x100
Matt Carlsona5767de2007-11-12 21:10:58 -080011124#define NVRAM_SELFBOOT_FORMAT1_0_SIZE 0x14
11125#define NVRAM_SELFBOOT_FORMAT1_2_SIZE 0x18
11126#define NVRAM_SELFBOOT_FORMAT1_3_SIZE 0x1c
Matt Carlson727a6d92011-06-13 13:38:58 +000011127#define NVRAM_SELFBOOT_FORMAT1_4_SIZE 0x20
11128#define NVRAM_SELFBOOT_FORMAT1_5_SIZE 0x24
Matt Carlsonbda18fa2011-07-20 10:20:57 +000011129#define NVRAM_SELFBOOT_FORMAT1_6_SIZE 0x50
Michael Chanb16250e2006-09-27 16:10:14 -070011130#define NVRAM_SELFBOOT_HW_SIZE 0x20
11131#define NVRAM_SELFBOOT_DATA_SIZE 0x1c
Michael Chan566f86a2005-05-29 14:56:58 -070011132
11133static int tg3_test_nvram(struct tg3 *tp)
11134{
Matt Carlson535a4902011-07-20 10:20:56 +000011135 u32 csum, magic, len;
Matt Carlsona9dc5292009-02-25 14:25:30 +000011136 __be32 *buf;
Andy Gospodarekab0049b2007-09-06 20:42:14 +010011137 int i, j, k, err = 0, size;
Michael Chan566f86a2005-05-29 14:56:58 -070011138
Joe Perches63c3a662011-04-26 08:12:10 +000011139 if (tg3_flag(tp, NO_NVRAM))
Matt Carlsondf259d82009-04-20 06:57:14 +000011140 return 0;
11141
Matt Carlsone4f34112009-02-25 14:25:00 +000011142 if (tg3_nvram_read(tp, 0, &magic) != 0)
Michael Chan1b277772006-03-20 22:27:48 -080011143 return -EIO;
11144
Michael Chan1b277772006-03-20 22:27:48 -080011145 if (magic == TG3_EEPROM_MAGIC)
11146 size = NVRAM_TEST_SIZE;
Michael Chanb16250e2006-09-27 16:10:14 -070011147 else if ((magic & TG3_EEPROM_MAGIC_FW_MSK) == TG3_EEPROM_MAGIC_FW) {
Matt Carlsona5767de2007-11-12 21:10:58 -080011148 if ((magic & TG3_EEPROM_SB_FORMAT_MASK) ==
11149 TG3_EEPROM_SB_FORMAT_1) {
11150 switch (magic & TG3_EEPROM_SB_REVISION_MASK) {
11151 case TG3_EEPROM_SB_REVISION_0:
11152 size = NVRAM_SELFBOOT_FORMAT1_0_SIZE;
11153 break;
11154 case TG3_EEPROM_SB_REVISION_2:
11155 size = NVRAM_SELFBOOT_FORMAT1_2_SIZE;
11156 break;
11157 case TG3_EEPROM_SB_REVISION_3:
11158 size = NVRAM_SELFBOOT_FORMAT1_3_SIZE;
11159 break;
Matt Carlson727a6d92011-06-13 13:38:58 +000011160 case TG3_EEPROM_SB_REVISION_4:
11161 size = NVRAM_SELFBOOT_FORMAT1_4_SIZE;
11162 break;
11163 case TG3_EEPROM_SB_REVISION_5:
11164 size = NVRAM_SELFBOOT_FORMAT1_5_SIZE;
11165 break;
11166 case TG3_EEPROM_SB_REVISION_6:
11167 size = NVRAM_SELFBOOT_FORMAT1_6_SIZE;
11168 break;
Matt Carlsona5767de2007-11-12 21:10:58 -080011169 default:
Matt Carlson727a6d92011-06-13 13:38:58 +000011170 return -EIO;
Matt Carlsona5767de2007-11-12 21:10:58 -080011171 }
11172 } else
Michael Chan1b277772006-03-20 22:27:48 -080011173 return 0;
Michael Chanb16250e2006-09-27 16:10:14 -070011174 } else if ((magic & TG3_EEPROM_MAGIC_HW_MSK) == TG3_EEPROM_MAGIC_HW)
11175 size = NVRAM_SELFBOOT_HW_SIZE;
11176 else
Michael Chan1b277772006-03-20 22:27:48 -080011177 return -EIO;
11178
11179 buf = kmalloc(size, GFP_KERNEL);
Michael Chan566f86a2005-05-29 14:56:58 -070011180 if (buf == NULL)
11181 return -ENOMEM;
11182
Michael Chan1b277772006-03-20 22:27:48 -080011183 err = -EIO;
11184 for (i = 0, j = 0; i < size; i += 4, j++) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000011185 err = tg3_nvram_read_be32(tp, i, &buf[j]);
11186 if (err)
Michael Chan566f86a2005-05-29 14:56:58 -070011187 break;
Michael Chan566f86a2005-05-29 14:56:58 -070011188 }
Michael Chan1b277772006-03-20 22:27:48 -080011189 if (i < size)
Michael Chan566f86a2005-05-29 14:56:58 -070011190 goto out;
11191
Michael Chan1b277772006-03-20 22:27:48 -080011192 /* Selfboot format */
Matt Carlsona9dc5292009-02-25 14:25:30 +000011193 magic = be32_to_cpu(buf[0]);
Al Virob9fc7dc2007-12-17 22:59:57 -080011194 if ((magic & TG3_EEPROM_MAGIC_FW_MSK) ==
Michael Chanb16250e2006-09-27 16:10:14 -070011195 TG3_EEPROM_MAGIC_FW) {
Michael Chan1b277772006-03-20 22:27:48 -080011196 u8 *buf8 = (u8 *) buf, csum8 = 0;
11197
Al Virob9fc7dc2007-12-17 22:59:57 -080011198 if ((magic & TG3_EEPROM_SB_REVISION_MASK) ==
Matt Carlsona5767de2007-11-12 21:10:58 -080011199 TG3_EEPROM_SB_REVISION_2) {
11200 /* For rev 2, the csum doesn't include the MBA. */
11201 for (i = 0; i < TG3_EEPROM_SB_F1R2_MBA_OFF; i++)
11202 csum8 += buf8[i];
11203 for (i = TG3_EEPROM_SB_F1R2_MBA_OFF + 4; i < size; i++)
11204 csum8 += buf8[i];
11205 } else {
11206 for (i = 0; i < size; i++)
11207 csum8 += buf8[i];
11208 }
Michael Chan1b277772006-03-20 22:27:48 -080011209
Adrian Bunkad96b482006-04-05 22:21:04 -070011210 if (csum8 == 0) {
11211 err = 0;
11212 goto out;
11213 }
11214
11215 err = -EIO;
11216 goto out;
Michael Chan1b277772006-03-20 22:27:48 -080011217 }
Michael Chan566f86a2005-05-29 14:56:58 -070011218
Al Virob9fc7dc2007-12-17 22:59:57 -080011219 if ((magic & TG3_EEPROM_MAGIC_HW_MSK) ==
Michael Chanb16250e2006-09-27 16:10:14 -070011220 TG3_EEPROM_MAGIC_HW) {
11221 u8 data[NVRAM_SELFBOOT_DATA_SIZE];
Matt Carlsona9dc5292009-02-25 14:25:30 +000011222 u8 parity[NVRAM_SELFBOOT_DATA_SIZE];
Michael Chanb16250e2006-09-27 16:10:14 -070011223 u8 *buf8 = (u8 *) buf;
Michael Chanb16250e2006-09-27 16:10:14 -070011224
11225 /* Separate the parity bits and the data bytes. */
11226 for (i = 0, j = 0, k = 0; i < NVRAM_SELFBOOT_HW_SIZE; i++) {
11227 if ((i == 0) || (i == 8)) {
11228 int l;
11229 u8 msk;
11230
11231 for (l = 0, msk = 0x80; l < 7; l++, msk >>= 1)
11232 parity[k++] = buf8[i] & msk;
11233 i++;
Matt Carlson859a588792010-04-05 10:19:28 +000011234 } else if (i == 16) {
Michael Chanb16250e2006-09-27 16:10:14 -070011235 int l;
11236 u8 msk;
11237
11238 for (l = 0, msk = 0x20; l < 6; l++, msk >>= 1)
11239 parity[k++] = buf8[i] & msk;
11240 i++;
11241
11242 for (l = 0, msk = 0x80; l < 8; l++, msk >>= 1)
11243 parity[k++] = buf8[i] & msk;
11244 i++;
11245 }
11246 data[j++] = buf8[i];
11247 }
11248
11249 err = -EIO;
11250 for (i = 0; i < NVRAM_SELFBOOT_DATA_SIZE; i++) {
11251 u8 hw8 = hweight8(data[i]);
11252
11253 if ((hw8 & 0x1) && parity[i])
11254 goto out;
11255 else if (!(hw8 & 0x1) && !parity[i])
11256 goto out;
11257 }
11258 err = 0;
11259 goto out;
11260 }
11261
Matt Carlson01c3a392011-03-09 16:58:20 +000011262 err = -EIO;
11263
Michael Chan566f86a2005-05-29 14:56:58 -070011264 /* Bootstrap checksum at offset 0x10 */
11265 csum = calc_crc((unsigned char *) buf, 0x10);
Matt Carlson01c3a392011-03-09 16:58:20 +000011266 if (csum != le32_to_cpu(buf[0x10/4]))
Michael Chan566f86a2005-05-29 14:56:58 -070011267 goto out;
11268
11269 /* Manufacturing block starts at offset 0x74, checksum at 0xfc */
11270 csum = calc_crc((unsigned char *) &buf[0x74/4], 0x88);
Matt Carlson01c3a392011-03-09 16:58:20 +000011271 if (csum != le32_to_cpu(buf[0xfc/4]))
Matt Carlsona9dc5292009-02-25 14:25:30 +000011272 goto out;
Michael Chan566f86a2005-05-29 14:56:58 -070011273
Matt Carlsonc3e94502011-04-13 11:05:08 +000011274 kfree(buf);
11275
Matt Carlson535a4902011-07-20 10:20:56 +000011276 buf = tg3_vpd_readblock(tp, &len);
Matt Carlsonc3e94502011-04-13 11:05:08 +000011277 if (!buf)
11278 return -ENOMEM;
Matt Carlsond4894f32011-03-09 16:58:21 +000011279
Matt Carlson535a4902011-07-20 10:20:56 +000011280 i = pci_vpd_find_tag((u8 *)buf, 0, len, PCI_VPD_LRDT_RO_DATA);
Matt Carlsond4894f32011-03-09 16:58:21 +000011281 if (i > 0) {
11282 j = pci_vpd_lrdt_size(&((u8 *)buf)[i]);
11283 if (j < 0)
11284 goto out;
11285
Matt Carlson535a4902011-07-20 10:20:56 +000011286 if (i + PCI_VPD_LRDT_TAG_SIZE + j > len)
Matt Carlsond4894f32011-03-09 16:58:21 +000011287 goto out;
11288
11289 i += PCI_VPD_LRDT_TAG_SIZE;
11290 j = pci_vpd_find_info_keyword((u8 *)buf, i, j,
11291 PCI_VPD_RO_KEYWORD_CHKSUM);
11292 if (j > 0) {
11293 u8 csum8 = 0;
11294
11295 j += PCI_VPD_INFO_FLD_HDR_SIZE;
11296
11297 for (i = 0; i <= j; i++)
11298 csum8 += ((u8 *)buf)[i];
11299
11300 if (csum8)
11301 goto out;
11302 }
11303 }
11304
Michael Chan566f86a2005-05-29 14:56:58 -070011305 err = 0;
11306
11307out:
11308 kfree(buf);
11309 return err;
11310}
11311
Michael Chanca430072005-05-29 14:57:23 -070011312#define TG3_SERDES_TIMEOUT_SEC 2
11313#define TG3_COPPER_TIMEOUT_SEC 6
11314
11315static int tg3_test_link(struct tg3 *tp)
11316{
11317 int i, max;
11318
11319 if (!netif_running(tp->dev))
11320 return -ENODEV;
11321
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011322 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
Michael Chanca430072005-05-29 14:57:23 -070011323 max = TG3_SERDES_TIMEOUT_SEC;
11324 else
11325 max = TG3_COPPER_TIMEOUT_SEC;
11326
11327 for (i = 0; i < max; i++) {
11328 if (netif_carrier_ok(tp->dev))
11329 return 0;
11330
11331 if (msleep_interruptible(1000))
11332 break;
11333 }
11334
11335 return -EIO;
11336}
11337
Michael Chana71116d2005-05-29 14:58:11 -070011338/* Only test the commonly used registers */
David S. Miller30ca3e32006-03-20 23:02:36 -080011339static int tg3_test_registers(struct tg3 *tp)
Michael Chana71116d2005-05-29 14:58:11 -070011340{
Michael Chanb16250e2006-09-27 16:10:14 -070011341 int i, is_5705, is_5750;
Michael Chana71116d2005-05-29 14:58:11 -070011342 u32 offset, read_mask, write_mask, val, save_val, read_val;
11343 static struct {
11344 u16 offset;
11345 u16 flags;
11346#define TG3_FL_5705 0x1
11347#define TG3_FL_NOT_5705 0x2
11348#define TG3_FL_NOT_5788 0x4
Michael Chanb16250e2006-09-27 16:10:14 -070011349#define TG3_FL_NOT_5750 0x8
Michael Chana71116d2005-05-29 14:58:11 -070011350 u32 read_mask;
11351 u32 write_mask;
11352 } reg_tbl[] = {
11353 /* MAC Control Registers */
11354 { MAC_MODE, TG3_FL_NOT_5705,
11355 0x00000000, 0x00ef6f8c },
11356 { MAC_MODE, TG3_FL_5705,
11357 0x00000000, 0x01ef6b8c },
11358 { MAC_STATUS, TG3_FL_NOT_5705,
11359 0x03800107, 0x00000000 },
11360 { MAC_STATUS, TG3_FL_5705,
11361 0x03800100, 0x00000000 },
11362 { MAC_ADDR_0_HIGH, 0x0000,
11363 0x00000000, 0x0000ffff },
11364 { MAC_ADDR_0_LOW, 0x0000,
Matt Carlsonc6cdf432010-04-05 10:19:26 +000011365 0x00000000, 0xffffffff },
Michael Chana71116d2005-05-29 14:58:11 -070011366 { MAC_RX_MTU_SIZE, 0x0000,
11367 0x00000000, 0x0000ffff },
11368 { MAC_TX_MODE, 0x0000,
11369 0x00000000, 0x00000070 },
11370 { MAC_TX_LENGTHS, 0x0000,
11371 0x00000000, 0x00003fff },
11372 { MAC_RX_MODE, TG3_FL_NOT_5705,
11373 0x00000000, 0x000007fc },
11374 { MAC_RX_MODE, TG3_FL_5705,
11375 0x00000000, 0x000007dc },
11376 { MAC_HASH_REG_0, 0x0000,
11377 0x00000000, 0xffffffff },
11378 { MAC_HASH_REG_1, 0x0000,
11379 0x00000000, 0xffffffff },
11380 { MAC_HASH_REG_2, 0x0000,
11381 0x00000000, 0xffffffff },
11382 { MAC_HASH_REG_3, 0x0000,
11383 0x00000000, 0xffffffff },
11384
11385 /* Receive Data and Receive BD Initiator Control Registers. */
11386 { RCVDBDI_JUMBO_BD+0, TG3_FL_NOT_5705,
11387 0x00000000, 0xffffffff },
11388 { RCVDBDI_JUMBO_BD+4, TG3_FL_NOT_5705,
11389 0x00000000, 0xffffffff },
11390 { RCVDBDI_JUMBO_BD+8, TG3_FL_NOT_5705,
11391 0x00000000, 0x00000003 },
11392 { RCVDBDI_JUMBO_BD+0xc, TG3_FL_NOT_5705,
11393 0x00000000, 0xffffffff },
11394 { RCVDBDI_STD_BD+0, 0x0000,
11395 0x00000000, 0xffffffff },
11396 { RCVDBDI_STD_BD+4, 0x0000,
11397 0x00000000, 0xffffffff },
11398 { RCVDBDI_STD_BD+8, 0x0000,
11399 0x00000000, 0xffff0002 },
11400 { RCVDBDI_STD_BD+0xc, 0x0000,
11401 0x00000000, 0xffffffff },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011402
Michael Chana71116d2005-05-29 14:58:11 -070011403 /* Receive BD Initiator Control Registers. */
11404 { RCVBDI_STD_THRESH, TG3_FL_NOT_5705,
11405 0x00000000, 0xffffffff },
11406 { RCVBDI_STD_THRESH, TG3_FL_5705,
11407 0x00000000, 0x000003ff },
11408 { RCVBDI_JUMBO_THRESH, TG3_FL_NOT_5705,
11409 0x00000000, 0xffffffff },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011410
Michael Chana71116d2005-05-29 14:58:11 -070011411 /* Host Coalescing Control Registers. */
11412 { HOSTCC_MODE, TG3_FL_NOT_5705,
11413 0x00000000, 0x00000004 },
11414 { HOSTCC_MODE, TG3_FL_5705,
11415 0x00000000, 0x000000f6 },
11416 { HOSTCC_RXCOL_TICKS, TG3_FL_NOT_5705,
11417 0x00000000, 0xffffffff },
11418 { HOSTCC_RXCOL_TICKS, TG3_FL_5705,
11419 0x00000000, 0x000003ff },
11420 { HOSTCC_TXCOL_TICKS, TG3_FL_NOT_5705,
11421 0x00000000, 0xffffffff },
11422 { HOSTCC_TXCOL_TICKS, TG3_FL_5705,
11423 0x00000000, 0x000003ff },
11424 { HOSTCC_RXMAX_FRAMES, TG3_FL_NOT_5705,
11425 0x00000000, 0xffffffff },
11426 { HOSTCC_RXMAX_FRAMES, TG3_FL_5705 | TG3_FL_NOT_5788,
11427 0x00000000, 0x000000ff },
11428 { HOSTCC_TXMAX_FRAMES, TG3_FL_NOT_5705,
11429 0x00000000, 0xffffffff },
11430 { HOSTCC_TXMAX_FRAMES, TG3_FL_5705 | TG3_FL_NOT_5788,
11431 0x00000000, 0x000000ff },
11432 { HOSTCC_RXCOAL_TICK_INT, TG3_FL_NOT_5705,
11433 0x00000000, 0xffffffff },
11434 { HOSTCC_TXCOAL_TICK_INT, TG3_FL_NOT_5705,
11435 0x00000000, 0xffffffff },
11436 { HOSTCC_RXCOAL_MAXF_INT, TG3_FL_NOT_5705,
11437 0x00000000, 0xffffffff },
11438 { HOSTCC_RXCOAL_MAXF_INT, TG3_FL_5705 | TG3_FL_NOT_5788,
11439 0x00000000, 0x000000ff },
11440 { HOSTCC_TXCOAL_MAXF_INT, TG3_FL_NOT_5705,
11441 0x00000000, 0xffffffff },
11442 { HOSTCC_TXCOAL_MAXF_INT, TG3_FL_5705 | TG3_FL_NOT_5788,
11443 0x00000000, 0x000000ff },
11444 { HOSTCC_STAT_COAL_TICKS, TG3_FL_NOT_5705,
11445 0x00000000, 0xffffffff },
11446 { HOSTCC_STATS_BLK_HOST_ADDR, TG3_FL_NOT_5705,
11447 0x00000000, 0xffffffff },
11448 { HOSTCC_STATS_BLK_HOST_ADDR+4, TG3_FL_NOT_5705,
11449 0x00000000, 0xffffffff },
11450 { HOSTCC_STATUS_BLK_HOST_ADDR, 0x0000,
11451 0x00000000, 0xffffffff },
11452 { HOSTCC_STATUS_BLK_HOST_ADDR+4, 0x0000,
11453 0x00000000, 0xffffffff },
11454 { HOSTCC_STATS_BLK_NIC_ADDR, 0x0000,
11455 0xffffffff, 0x00000000 },
11456 { HOSTCC_STATUS_BLK_NIC_ADDR, 0x0000,
11457 0xffffffff, 0x00000000 },
11458
11459 /* Buffer Manager Control Registers. */
Michael Chanb16250e2006-09-27 16:10:14 -070011460 { BUFMGR_MB_POOL_ADDR, TG3_FL_NOT_5750,
Michael Chana71116d2005-05-29 14:58:11 -070011461 0x00000000, 0x007fff80 },
Michael Chanb16250e2006-09-27 16:10:14 -070011462 { BUFMGR_MB_POOL_SIZE, TG3_FL_NOT_5750,
Michael Chana71116d2005-05-29 14:58:11 -070011463 0x00000000, 0x007fffff },
11464 { BUFMGR_MB_RDMA_LOW_WATER, 0x0000,
11465 0x00000000, 0x0000003f },
11466 { BUFMGR_MB_MACRX_LOW_WATER, 0x0000,
11467 0x00000000, 0x000001ff },
11468 { BUFMGR_MB_HIGH_WATER, 0x0000,
11469 0x00000000, 0x000001ff },
11470 { BUFMGR_DMA_DESC_POOL_ADDR, TG3_FL_NOT_5705,
11471 0xffffffff, 0x00000000 },
11472 { BUFMGR_DMA_DESC_POOL_SIZE, TG3_FL_NOT_5705,
11473 0xffffffff, 0x00000000 },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011474
Michael Chana71116d2005-05-29 14:58:11 -070011475 /* Mailbox Registers */
11476 { GRCMBOX_RCVSTD_PROD_IDX+4, 0x0000,
11477 0x00000000, 0x000001ff },
11478 { GRCMBOX_RCVJUMBO_PROD_IDX+4, TG3_FL_NOT_5705,
11479 0x00000000, 0x000001ff },
11480 { GRCMBOX_RCVRET_CON_IDX_0+4, 0x0000,
11481 0x00000000, 0x000007ff },
11482 { GRCMBOX_SNDHOST_PROD_IDX_0+4, 0x0000,
11483 0x00000000, 0x000001ff },
11484
11485 { 0xffff, 0x0000, 0x00000000, 0x00000000 },
11486 };
11487
Michael Chanb16250e2006-09-27 16:10:14 -070011488 is_5705 = is_5750 = 0;
Joe Perches63c3a662011-04-26 08:12:10 +000011489 if (tg3_flag(tp, 5705_PLUS)) {
Michael Chana71116d2005-05-29 14:58:11 -070011490 is_5705 = 1;
Joe Perches63c3a662011-04-26 08:12:10 +000011491 if (tg3_flag(tp, 5750_PLUS))
Michael Chanb16250e2006-09-27 16:10:14 -070011492 is_5750 = 1;
11493 }
Michael Chana71116d2005-05-29 14:58:11 -070011494
11495 for (i = 0; reg_tbl[i].offset != 0xffff; i++) {
11496 if (is_5705 && (reg_tbl[i].flags & TG3_FL_NOT_5705))
11497 continue;
11498
11499 if (!is_5705 && (reg_tbl[i].flags & TG3_FL_5705))
11500 continue;
11501
Joe Perches63c3a662011-04-26 08:12:10 +000011502 if (tg3_flag(tp, IS_5788) &&
Michael Chana71116d2005-05-29 14:58:11 -070011503 (reg_tbl[i].flags & TG3_FL_NOT_5788))
11504 continue;
11505
Michael Chanb16250e2006-09-27 16:10:14 -070011506 if (is_5750 && (reg_tbl[i].flags & TG3_FL_NOT_5750))
11507 continue;
11508
Michael Chana71116d2005-05-29 14:58:11 -070011509 offset = (u32) reg_tbl[i].offset;
11510 read_mask = reg_tbl[i].read_mask;
11511 write_mask = reg_tbl[i].write_mask;
11512
11513 /* Save the original register content */
11514 save_val = tr32(offset);
11515
11516 /* Determine the read-only value. */
11517 read_val = save_val & read_mask;
11518
11519 /* Write zero to the register, then make sure the read-only bits
11520 * are not changed and the read/write bits are all zeros.
11521 */
11522 tw32(offset, 0);
11523
11524 val = tr32(offset);
11525
11526 /* Test the read-only and read/write bits. */
11527 if (((val & read_mask) != read_val) || (val & write_mask))
11528 goto out;
11529
11530 /* Write ones to all the bits defined by RdMask and WrMask, then
11531 * make sure the read-only bits are not changed and the
11532 * read/write bits are all ones.
11533 */
11534 tw32(offset, read_mask | write_mask);
11535
11536 val = tr32(offset);
11537
11538 /* Test the read-only bits. */
11539 if ((val & read_mask) != read_val)
11540 goto out;
11541
11542 /* Test the read/write bits. */
11543 if ((val & write_mask) != write_mask)
11544 goto out;
11545
11546 tw32(offset, save_val);
11547 }
11548
11549 return 0;
11550
11551out:
Michael Chan9f88f292006-12-07 00:22:54 -080011552 if (netif_msg_hw(tp))
Matt Carlson2445e462010-04-05 10:19:21 +000011553 netdev_err(tp->dev,
11554 "Register test failed at offset %x\n", offset);
Michael Chana71116d2005-05-29 14:58:11 -070011555 tw32(offset, save_val);
11556 return -EIO;
11557}
11558
Michael Chan7942e1d2005-05-29 14:58:36 -070011559static int tg3_do_mem_test(struct tg3 *tp, u32 offset, u32 len)
11560{
Arjan van de Venf71e1302006-03-03 21:33:57 -050011561 static const u32 test_pattern[] = { 0x00000000, 0xffffffff, 0xaa55a55a };
Michael Chan7942e1d2005-05-29 14:58:36 -070011562 int i;
11563 u32 j;
11564
Alejandro Martinez Ruize9edda62007-10-15 03:37:43 +020011565 for (i = 0; i < ARRAY_SIZE(test_pattern); i++) {
Michael Chan7942e1d2005-05-29 14:58:36 -070011566 for (j = 0; j < len; j += 4) {
11567 u32 val;
11568
11569 tg3_write_mem(tp, offset + j, test_pattern[i]);
11570 tg3_read_mem(tp, offset + j, &val);
11571 if (val != test_pattern[i])
11572 return -EIO;
11573 }
11574 }
11575 return 0;
11576}
11577
11578static int tg3_test_memory(struct tg3 *tp)
11579{
11580 static struct mem_entry {
11581 u32 offset;
11582 u32 len;
11583 } mem_tbl_570x[] = {
Michael Chan38690192005-12-19 16:27:28 -080011584 { 0x00000000, 0x00b50},
Michael Chan7942e1d2005-05-29 14:58:36 -070011585 { 0x00002000, 0x1c000},
11586 { 0xffffffff, 0x00000}
11587 }, mem_tbl_5705[] = {
11588 { 0x00000100, 0x0000c},
11589 { 0x00000200, 0x00008},
Michael Chan7942e1d2005-05-29 14:58:36 -070011590 { 0x00004000, 0x00800},
11591 { 0x00006000, 0x01000},
11592 { 0x00008000, 0x02000},
11593 { 0x00010000, 0x0e000},
11594 { 0xffffffff, 0x00000}
Michael Chan79f4d132006-03-20 22:28:57 -080011595 }, mem_tbl_5755[] = {
11596 { 0x00000200, 0x00008},
11597 { 0x00004000, 0x00800},
11598 { 0x00006000, 0x00800},
11599 { 0x00008000, 0x02000},
11600 { 0x00010000, 0x0c000},
11601 { 0xffffffff, 0x00000}
Michael Chanb16250e2006-09-27 16:10:14 -070011602 }, mem_tbl_5906[] = {
11603 { 0x00000200, 0x00008},
11604 { 0x00004000, 0x00400},
11605 { 0x00006000, 0x00400},
11606 { 0x00008000, 0x01000},
11607 { 0x00010000, 0x01000},
11608 { 0xffffffff, 0x00000}
Matt Carlson8b5a6c42010-01-20 16:58:06 +000011609 }, mem_tbl_5717[] = {
11610 { 0x00000200, 0x00008},
11611 { 0x00010000, 0x0a000},
11612 { 0x00020000, 0x13c00},
11613 { 0xffffffff, 0x00000}
11614 }, mem_tbl_57765[] = {
11615 { 0x00000200, 0x00008},
11616 { 0x00004000, 0x00800},
11617 { 0x00006000, 0x09800},
11618 { 0x00010000, 0x0a000},
11619 { 0xffffffff, 0x00000}
Michael Chan7942e1d2005-05-29 14:58:36 -070011620 };
11621 struct mem_entry *mem_tbl;
11622 int err = 0;
11623 int i;
11624
Joe Perches63c3a662011-04-26 08:12:10 +000011625 if (tg3_flag(tp, 5717_PLUS))
Matt Carlson8b5a6c42010-01-20 16:58:06 +000011626 mem_tbl = mem_tbl_5717;
Matt Carlson55086ad2011-12-14 11:09:59 +000011627 else if (tg3_flag(tp, 57765_CLASS))
Matt Carlson8b5a6c42010-01-20 16:58:06 +000011628 mem_tbl = mem_tbl_57765;
Joe Perches63c3a662011-04-26 08:12:10 +000011629 else if (tg3_flag(tp, 5755_PLUS))
Matt Carlson321d32a2008-11-21 17:22:19 -080011630 mem_tbl = mem_tbl_5755;
11631 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
11632 mem_tbl = mem_tbl_5906;
Joe Perches63c3a662011-04-26 08:12:10 +000011633 else if (tg3_flag(tp, 5705_PLUS))
Matt Carlson321d32a2008-11-21 17:22:19 -080011634 mem_tbl = mem_tbl_5705;
11635 else
Michael Chan7942e1d2005-05-29 14:58:36 -070011636 mem_tbl = mem_tbl_570x;
11637
11638 for (i = 0; mem_tbl[i].offset != 0xffffffff; i++) {
Matt Carlsonbe98da62010-07-11 09:31:46 +000011639 err = tg3_do_mem_test(tp, mem_tbl[i].offset, mem_tbl[i].len);
11640 if (err)
Michael Chan7942e1d2005-05-29 14:58:36 -070011641 break;
11642 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011643
Michael Chan7942e1d2005-05-29 14:58:36 -070011644 return err;
11645}
11646
Matt Carlsonbb158d62011-04-25 12:42:47 +000011647#define TG3_TSO_MSS 500
11648
11649#define TG3_TSO_IP_HDR_LEN 20
11650#define TG3_TSO_TCP_HDR_LEN 20
11651#define TG3_TSO_TCP_OPT_LEN 12
11652
11653static const u8 tg3_tso_header[] = {
116540x08, 0x00,
116550x45, 0x00, 0x00, 0x00,
116560x00, 0x00, 0x40, 0x00,
116570x40, 0x06, 0x00, 0x00,
116580x0a, 0x00, 0x00, 0x01,
116590x0a, 0x00, 0x00, 0x02,
116600x0d, 0x00, 0xe0, 0x00,
116610x00, 0x00, 0x01, 0x00,
116620x00, 0x00, 0x02, 0x00,
116630x80, 0x10, 0x10, 0x00,
116640x14, 0x09, 0x00, 0x00,
116650x01, 0x01, 0x08, 0x0a,
116660x11, 0x11, 0x11, 0x11,
116670x11, 0x11, 0x11, 0x11,
11668};
Michael Chan9f40dea2005-09-05 17:53:06 -070011669
Matt Carlson28a45952011-08-19 13:58:22 +000011670static int tg3_run_loopback(struct tg3 *tp, u32 pktsz, bool tso_loopback)
Michael Chanc76949a2005-05-29 14:58:59 -070011671{
Matt Carlson5e5a7f32011-08-19 13:58:21 +000011672 u32 rx_start_idx, rx_idx, tx_idx, opaque_key;
Matt Carlsonbb158d62011-04-25 12:42:47 +000011673 u32 base_flags = 0, mss = 0, desc_idx, coal_now, data_off, val;
Matt Carlson84b67b22011-07-27 14:20:52 +000011674 u32 budget;
Eric Dumazet9205fd92011-11-18 06:47:01 +000011675 struct sk_buff *skb;
11676 u8 *tx_data, *rx_data;
Michael Chanc76949a2005-05-29 14:58:59 -070011677 dma_addr_t map;
11678 int num_pkts, tx_len, rx_len, i, err;
11679 struct tg3_rx_buffer_desc *desc;
Matt Carlson898a56f2009-08-28 14:02:40 +000011680 struct tg3_napi *tnapi, *rnapi;
Matt Carlson8fea32b2010-09-15 08:59:58 +000011681 struct tg3_rx_prodring_set *tpr = &tp->napi[0].prodring;
Michael Chanc76949a2005-05-29 14:58:59 -070011682
Matt Carlsonc8873402010-02-12 14:47:11 +000011683 tnapi = &tp->napi[0];
11684 rnapi = &tp->napi[0];
Matt Carlson0c1d0e22009-09-01 13:16:33 +000011685 if (tp->irq_cnt > 1) {
Joe Perches63c3a662011-04-26 08:12:10 +000011686 if (tg3_flag(tp, ENABLE_RSS))
Matt Carlson1da85aa2010-09-30 10:34:34 +000011687 rnapi = &tp->napi[1];
Joe Perches63c3a662011-04-26 08:12:10 +000011688 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlsonc8873402010-02-12 14:47:11 +000011689 tnapi = &tp->napi[1];
Matt Carlson0c1d0e22009-09-01 13:16:33 +000011690 }
Matt Carlsonfd2ce372009-09-01 12:51:13 +000011691 coal_now = tnapi->coal_now | rnapi->coal_now;
Matt Carlson898a56f2009-08-28 14:02:40 +000011692
Michael Chanc76949a2005-05-29 14:58:59 -070011693 err = -EIO;
11694
Matt Carlson4852a862011-04-13 11:05:07 +000011695 tx_len = pktsz;
David S. Millera20e9c62006-07-31 22:38:16 -070011696 skb = netdev_alloc_skb(tp->dev, tx_len);
Jesper Juhla50bb7b2006-05-09 23:14:35 -070011697 if (!skb)
11698 return -ENOMEM;
11699
Michael Chanc76949a2005-05-29 14:58:59 -070011700 tx_data = skb_put(skb, tx_len);
11701 memcpy(tx_data, tp->dev->dev_addr, 6);
11702 memset(tx_data + 6, 0x0, 8);
11703
Matt Carlson4852a862011-04-13 11:05:07 +000011704 tw32(MAC_RX_MTU_SIZE, tx_len + ETH_FCS_LEN);
Michael Chanc76949a2005-05-29 14:58:59 -070011705
Matt Carlson28a45952011-08-19 13:58:22 +000011706 if (tso_loopback) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000011707 struct iphdr *iph = (struct iphdr *)&tx_data[ETH_HLEN];
11708
11709 u32 hdr_len = TG3_TSO_IP_HDR_LEN + TG3_TSO_TCP_HDR_LEN +
11710 TG3_TSO_TCP_OPT_LEN;
11711
11712 memcpy(tx_data + ETH_ALEN * 2, tg3_tso_header,
11713 sizeof(tg3_tso_header));
11714 mss = TG3_TSO_MSS;
11715
11716 val = tx_len - ETH_ALEN * 2 - sizeof(tg3_tso_header);
11717 num_pkts = DIV_ROUND_UP(val, TG3_TSO_MSS);
11718
11719 /* Set the total length field in the IP header */
11720 iph->tot_len = htons((u16)(mss + hdr_len));
11721
11722 base_flags = (TXD_FLAG_CPU_PRE_DMA |
11723 TXD_FLAG_CPU_POST_DMA);
11724
Joe Perches63c3a662011-04-26 08:12:10 +000011725 if (tg3_flag(tp, HW_TSO_1) ||
11726 tg3_flag(tp, HW_TSO_2) ||
11727 tg3_flag(tp, HW_TSO_3)) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000011728 struct tcphdr *th;
11729 val = ETH_HLEN + TG3_TSO_IP_HDR_LEN;
11730 th = (struct tcphdr *)&tx_data[val];
11731 th->check = 0;
11732 } else
11733 base_flags |= TXD_FLAG_TCPUDP_CSUM;
11734
Joe Perches63c3a662011-04-26 08:12:10 +000011735 if (tg3_flag(tp, HW_TSO_3)) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000011736 mss |= (hdr_len & 0xc) << 12;
11737 if (hdr_len & 0x10)
11738 base_flags |= 0x00000010;
11739 base_flags |= (hdr_len & 0x3e0) << 5;
Joe Perches63c3a662011-04-26 08:12:10 +000011740 } else if (tg3_flag(tp, HW_TSO_2))
Matt Carlsonbb158d62011-04-25 12:42:47 +000011741 mss |= hdr_len << 9;
Joe Perches63c3a662011-04-26 08:12:10 +000011742 else if (tg3_flag(tp, HW_TSO_1) ||
Matt Carlsonbb158d62011-04-25 12:42:47 +000011743 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
11744 mss |= (TG3_TSO_TCP_OPT_LEN << 9);
11745 } else {
11746 base_flags |= (TG3_TSO_TCP_OPT_LEN << 10);
11747 }
11748
11749 data_off = ETH_ALEN * 2 + sizeof(tg3_tso_header);
11750 } else {
11751 num_pkts = 1;
11752 data_off = ETH_HLEN;
11753 }
11754
11755 for (i = data_off; i < tx_len; i++)
Michael Chanc76949a2005-05-29 14:58:59 -070011756 tx_data[i] = (u8) (i & 0xff);
11757
Alexander Duyckf4188d82009-12-02 16:48:38 +000011758 map = pci_map_single(tp->pdev, skb->data, tx_len, PCI_DMA_TODEVICE);
11759 if (pci_dma_mapping_error(tp->pdev, map)) {
Matt Carlsona21771d2009-11-02 14:25:31 +000011760 dev_kfree_skb(skb);
11761 return -EIO;
11762 }
Michael Chanc76949a2005-05-29 14:58:59 -070011763
Matt Carlson0d681b22011-07-27 14:20:49 +000011764 val = tnapi->tx_prod;
11765 tnapi->tx_buffers[val].skb = skb;
11766 dma_unmap_addr_set(&tnapi->tx_buffers[val], mapping, map);
11767
Michael Chanc76949a2005-05-29 14:58:59 -070011768 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +000011769 rnapi->coal_now);
Michael Chanc76949a2005-05-29 14:58:59 -070011770
11771 udelay(10);
11772
Matt Carlson898a56f2009-08-28 14:02:40 +000011773 rx_start_idx = rnapi->hw_status->idx[0].rx_producer;
Michael Chanc76949a2005-05-29 14:58:59 -070011774
Matt Carlson84b67b22011-07-27 14:20:52 +000011775 budget = tg3_tx_avail(tnapi);
11776 if (tg3_tx_frag_set(tnapi, &val, &budget, map, tx_len,
Matt Carlsond1a3b732011-07-27 14:20:51 +000011777 base_flags | TXD_FLAG_END, mss, 0)) {
11778 tnapi->tx_buffers[val].skb = NULL;
11779 dev_kfree_skb(skb);
11780 return -EIO;
11781 }
Michael Chanc76949a2005-05-29 14:58:59 -070011782
Matt Carlsonf3f3f272009-08-28 14:03:21 +000011783 tnapi->tx_prod++;
Michael Chanc76949a2005-05-29 14:58:59 -070011784
Matt Carlsonf3f3f272009-08-28 14:03:21 +000011785 tw32_tx_mbox(tnapi->prodmbox, tnapi->tx_prod);
11786 tr32_mailbox(tnapi->prodmbox);
Michael Chanc76949a2005-05-29 14:58:59 -070011787
11788 udelay(10);
11789
Matt Carlson303fc922009-11-02 14:27:34 +000011790 /* 350 usec to allow enough time on some 10/100 Mbps devices. */
11791 for (i = 0; i < 35; i++) {
Michael Chanc76949a2005-05-29 14:58:59 -070011792 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +000011793 coal_now);
Michael Chanc76949a2005-05-29 14:58:59 -070011794
11795 udelay(10);
11796
Matt Carlson898a56f2009-08-28 14:02:40 +000011797 tx_idx = tnapi->hw_status->idx[0].tx_consumer;
11798 rx_idx = rnapi->hw_status->idx[0].rx_producer;
Matt Carlsonf3f3f272009-08-28 14:03:21 +000011799 if ((tx_idx == tnapi->tx_prod) &&
Michael Chanc76949a2005-05-29 14:58:59 -070011800 (rx_idx == (rx_start_idx + num_pkts)))
11801 break;
11802 }
11803
Matt Carlsonba1142e2011-11-04 09:15:00 +000011804 tg3_tx_skb_unmap(tnapi, tnapi->tx_prod - 1, -1);
Michael Chanc76949a2005-05-29 14:58:59 -070011805 dev_kfree_skb(skb);
11806
Matt Carlsonf3f3f272009-08-28 14:03:21 +000011807 if (tx_idx != tnapi->tx_prod)
Michael Chanc76949a2005-05-29 14:58:59 -070011808 goto out;
11809
11810 if (rx_idx != rx_start_idx + num_pkts)
11811 goto out;
11812
Matt Carlsonbb158d62011-04-25 12:42:47 +000011813 val = data_off;
11814 while (rx_idx != rx_start_idx) {
11815 desc = &rnapi->rx_rcb[rx_start_idx++];
11816 desc_idx = desc->opaque & RXD_OPAQUE_INDEX_MASK;
11817 opaque_key = desc->opaque & RXD_OPAQUE_RING_MASK;
Michael Chanc76949a2005-05-29 14:58:59 -070011818
Matt Carlsonbb158d62011-04-25 12:42:47 +000011819 if ((desc->err_vlan & RXD_ERR_MASK) != 0 &&
11820 (desc->err_vlan != RXD_ERR_ODD_NIBBLE_RCVD_MII))
Matt Carlson4852a862011-04-13 11:05:07 +000011821 goto out;
Michael Chanc76949a2005-05-29 14:58:59 -070011822
Matt Carlsonbb158d62011-04-25 12:42:47 +000011823 rx_len = ((desc->idx_len & RXD_LEN_MASK) >> RXD_LEN_SHIFT)
11824 - ETH_FCS_LEN;
11825
Matt Carlson28a45952011-08-19 13:58:22 +000011826 if (!tso_loopback) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000011827 if (rx_len != tx_len)
11828 goto out;
11829
11830 if (pktsz <= TG3_RX_STD_DMA_SZ - ETH_FCS_LEN) {
11831 if (opaque_key != RXD_OPAQUE_RING_STD)
11832 goto out;
11833 } else {
11834 if (opaque_key != RXD_OPAQUE_RING_JUMBO)
11835 goto out;
11836 }
11837 } else if ((desc->type_flags & RXD_FLAG_TCPUDP_CSUM) &&
11838 (desc->ip_tcp_csum & RXD_TCPCSUM_MASK)
Matt Carlson54e0a672011-05-19 12:12:50 +000011839 >> RXD_TCPCSUM_SHIFT != 0xffff) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000011840 goto out;
11841 }
11842
11843 if (opaque_key == RXD_OPAQUE_RING_STD) {
Eric Dumazet9205fd92011-11-18 06:47:01 +000011844 rx_data = tpr->rx_std_buffers[desc_idx].data;
Matt Carlsonbb158d62011-04-25 12:42:47 +000011845 map = dma_unmap_addr(&tpr->rx_std_buffers[desc_idx],
11846 mapping);
11847 } else if (opaque_key == RXD_OPAQUE_RING_JUMBO) {
Eric Dumazet9205fd92011-11-18 06:47:01 +000011848 rx_data = tpr->rx_jmb_buffers[desc_idx].data;
Matt Carlsonbb158d62011-04-25 12:42:47 +000011849 map = dma_unmap_addr(&tpr->rx_jmb_buffers[desc_idx],
11850 mapping);
11851 } else
Matt Carlson4852a862011-04-13 11:05:07 +000011852 goto out;
11853
Matt Carlsonbb158d62011-04-25 12:42:47 +000011854 pci_dma_sync_single_for_cpu(tp->pdev, map, rx_len,
11855 PCI_DMA_FROMDEVICE);
11856
Eric Dumazet9205fd92011-11-18 06:47:01 +000011857 rx_data += TG3_RX_OFFSET(tp);
Matt Carlsonbb158d62011-04-25 12:42:47 +000011858 for (i = data_off; i < rx_len; i++, val++) {
Eric Dumazet9205fd92011-11-18 06:47:01 +000011859 if (*(rx_data + i) != (u8) (val & 0xff))
Matt Carlsonbb158d62011-04-25 12:42:47 +000011860 goto out;
11861 }
Matt Carlson4852a862011-04-13 11:05:07 +000011862 }
11863
Michael Chanc76949a2005-05-29 14:58:59 -070011864 err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011865
Eric Dumazet9205fd92011-11-18 06:47:01 +000011866 /* tg3_free_rings will unmap and free the rx_data */
Michael Chanc76949a2005-05-29 14:58:59 -070011867out:
11868 return err;
11869}
11870
Matt Carlson00c266b2011-04-25 12:42:46 +000011871#define TG3_STD_LOOPBACK_FAILED 1
11872#define TG3_JMB_LOOPBACK_FAILED 2
Matt Carlsonbb158d62011-04-25 12:42:47 +000011873#define TG3_TSO_LOOPBACK_FAILED 4
Matt Carlson28a45952011-08-19 13:58:22 +000011874#define TG3_LOOPBACK_FAILED \
11875 (TG3_STD_LOOPBACK_FAILED | \
11876 TG3_JMB_LOOPBACK_FAILED | \
11877 TG3_TSO_LOOPBACK_FAILED)
Matt Carlson00c266b2011-04-25 12:42:46 +000011878
Matt Carlson941ec902011-08-19 13:58:23 +000011879static int tg3_test_loopback(struct tg3 *tp, u64 *data, bool do_extlpbk)
Michael Chan9f40dea2005-09-05 17:53:06 -070011880{
Matt Carlson28a45952011-08-19 13:58:22 +000011881 int err = -EIO;
Matt Carlson2215e242011-08-19 13:58:19 +000011882 u32 eee_cap;
Michael Chan9f40dea2005-09-05 17:53:06 -070011883
Matt Carlsonab789042011-01-25 15:58:54 +000011884 eee_cap = tp->phy_flags & TG3_PHYFLG_EEE_CAP;
11885 tp->phy_flags &= ~TG3_PHYFLG_EEE_CAP;
11886
Matt Carlson28a45952011-08-19 13:58:22 +000011887 if (!netif_running(tp->dev)) {
11888 data[0] = TG3_LOOPBACK_FAILED;
11889 data[1] = TG3_LOOPBACK_FAILED;
Matt Carlson941ec902011-08-19 13:58:23 +000011890 if (do_extlpbk)
11891 data[2] = TG3_LOOPBACK_FAILED;
Matt Carlson28a45952011-08-19 13:58:22 +000011892 goto done;
11893 }
11894
Michael Chanb9ec6c12006-07-25 16:37:27 -070011895 err = tg3_reset_hw(tp, 1);
Matt Carlsonab789042011-01-25 15:58:54 +000011896 if (err) {
Matt Carlson28a45952011-08-19 13:58:22 +000011897 data[0] = TG3_LOOPBACK_FAILED;
11898 data[1] = TG3_LOOPBACK_FAILED;
Matt Carlson941ec902011-08-19 13:58:23 +000011899 if (do_extlpbk)
11900 data[2] = TG3_LOOPBACK_FAILED;
Matt Carlsonab789042011-01-25 15:58:54 +000011901 goto done;
11902 }
Michael Chan9f40dea2005-09-05 17:53:06 -070011903
Joe Perches63c3a662011-04-26 08:12:10 +000011904 if (tg3_flag(tp, ENABLE_RSS)) {
Matt Carlson4a85f092011-04-20 07:57:37 +000011905 int i;
11906
11907 /* Reroute all rx packets to the 1st queue */
11908 for (i = MAC_RSS_INDIR_TBL_0;
11909 i < MAC_RSS_INDIR_TBL_0 + TG3_RSS_INDIR_TBL_SIZE; i += 4)
11910 tw32(i, 0x0);
11911 }
11912
Matt Carlson6e01b202011-08-19 13:58:20 +000011913 /* HW errata - mac loopback fails in some cases on 5780.
11914 * Normal traffic and PHY loopback are not affected by
11915 * errata. Also, the MAC loopback test is deprecated for
11916 * all newer ASIC revisions.
11917 */
11918 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5780 &&
11919 !tg3_flag(tp, CPMU_PRESENT)) {
11920 tg3_mac_loopback(tp, true);
Matt Carlson9936bcf2007-10-10 18:03:07 -070011921
Matt Carlson28a45952011-08-19 13:58:22 +000011922 if (tg3_run_loopback(tp, ETH_FRAME_LEN, false))
11923 data[0] |= TG3_STD_LOOPBACK_FAILED;
Matt Carlson6e01b202011-08-19 13:58:20 +000011924
11925 if (tg3_flag(tp, JUMBO_RING_ENABLE) &&
Matt Carlson28a45952011-08-19 13:58:22 +000011926 tg3_run_loopback(tp, 9000 + ETH_HLEN, false))
11927 data[0] |= TG3_JMB_LOOPBACK_FAILED;
Matt Carlson6e01b202011-08-19 13:58:20 +000011928
11929 tg3_mac_loopback(tp, false);
11930 }
Matt Carlson4852a862011-04-13 11:05:07 +000011931
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011932 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
Joe Perches63c3a662011-04-26 08:12:10 +000011933 !tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson5e5a7f32011-08-19 13:58:21 +000011934 int i;
11935
Matt Carlson941ec902011-08-19 13:58:23 +000011936 tg3_phy_lpbk_set(tp, 0, false);
Matt Carlson5e5a7f32011-08-19 13:58:21 +000011937
11938 /* Wait for link */
11939 for (i = 0; i < 100; i++) {
11940 if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
11941 break;
11942 mdelay(1);
11943 }
11944
Matt Carlson28a45952011-08-19 13:58:22 +000011945 if (tg3_run_loopback(tp, ETH_FRAME_LEN, false))
11946 data[1] |= TG3_STD_LOOPBACK_FAILED;
Joe Perches63c3a662011-04-26 08:12:10 +000011947 if (tg3_flag(tp, TSO_CAPABLE) &&
Matt Carlson28a45952011-08-19 13:58:22 +000011948 tg3_run_loopback(tp, ETH_FRAME_LEN, true))
11949 data[1] |= TG3_TSO_LOOPBACK_FAILED;
Joe Perches63c3a662011-04-26 08:12:10 +000011950 if (tg3_flag(tp, JUMBO_RING_ENABLE) &&
Matt Carlson28a45952011-08-19 13:58:22 +000011951 tg3_run_loopback(tp, 9000 + ETH_HLEN, false))
11952 data[1] |= TG3_JMB_LOOPBACK_FAILED;
Michael Chan9f40dea2005-09-05 17:53:06 -070011953
Matt Carlson941ec902011-08-19 13:58:23 +000011954 if (do_extlpbk) {
11955 tg3_phy_lpbk_set(tp, 0, true);
11956
11957 /* All link indications report up, but the hardware
11958 * isn't really ready for about 20 msec. Double it
11959 * to be sure.
11960 */
11961 mdelay(40);
11962
11963 if (tg3_run_loopback(tp, ETH_FRAME_LEN, false))
11964 data[2] |= TG3_STD_LOOPBACK_FAILED;
11965 if (tg3_flag(tp, TSO_CAPABLE) &&
11966 tg3_run_loopback(tp, ETH_FRAME_LEN, true))
11967 data[2] |= TG3_TSO_LOOPBACK_FAILED;
11968 if (tg3_flag(tp, JUMBO_RING_ENABLE) &&
11969 tg3_run_loopback(tp, 9000 + ETH_HLEN, false))
11970 data[2] |= TG3_JMB_LOOPBACK_FAILED;
11971 }
11972
Matt Carlson5e5a7f32011-08-19 13:58:21 +000011973 /* Re-enable gphy autopowerdown. */
11974 if (tp->phy_flags & TG3_PHYFLG_ENABLE_APD)
11975 tg3_phy_toggle_apd(tp, true);
11976 }
Matt Carlson6833c042008-11-21 17:18:59 -080011977
Matt Carlson941ec902011-08-19 13:58:23 +000011978 err = (data[0] | data[1] | data[2]) ? -EIO : 0;
Matt Carlson28a45952011-08-19 13:58:22 +000011979
Matt Carlsonab789042011-01-25 15:58:54 +000011980done:
11981 tp->phy_flags |= eee_cap;
11982
Michael Chan9f40dea2005-09-05 17:53:06 -070011983 return err;
11984}
11985
Michael Chan4cafd3f2005-05-29 14:56:34 -070011986static void tg3_self_test(struct net_device *dev, struct ethtool_test *etest,
11987 u64 *data)
11988{
Michael Chan566f86a2005-05-29 14:56:58 -070011989 struct tg3 *tp = netdev_priv(dev);
Matt Carlson941ec902011-08-19 13:58:23 +000011990 bool doextlpbk = etest->flags & ETH_TEST_FL_EXTERNAL_LB;
Michael Chan566f86a2005-05-29 14:56:58 -070011991
Matt Carlsonbed98292011-07-13 09:27:29 +000011992 if ((tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) &&
11993 tg3_power_up(tp)) {
11994 etest->flags |= ETH_TEST_FL_FAILED;
11995 memset(data, 1, sizeof(u64) * TG3_NUM_TEST);
11996 return;
11997 }
Michael Chanbc1c7562006-03-20 17:48:03 -080011998
Michael Chan566f86a2005-05-29 14:56:58 -070011999 memset(data, 0, sizeof(u64) * TG3_NUM_TEST);
12000
12001 if (tg3_test_nvram(tp) != 0) {
12002 etest->flags |= ETH_TEST_FL_FAILED;
12003 data[0] = 1;
12004 }
Matt Carlson941ec902011-08-19 13:58:23 +000012005 if (!doextlpbk && tg3_test_link(tp)) {
Michael Chanca430072005-05-29 14:57:23 -070012006 etest->flags |= ETH_TEST_FL_FAILED;
12007 data[1] = 1;
12008 }
Michael Chana71116d2005-05-29 14:58:11 -070012009 if (etest->flags & ETH_TEST_FL_OFFLINE) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012010 int err, err2 = 0, irq_sync = 0;
Michael Chana71116d2005-05-29 14:58:11 -070012011
Michael Chanbbe832c2005-06-24 20:20:04 -070012012 if (netif_running(dev)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012013 tg3_phy_stop(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -070012014 tg3_netif_stop(tp);
12015 irq_sync = 1;
12016 }
12017
12018 tg3_full_lock(tp, irq_sync);
Michael Chana71116d2005-05-29 14:58:11 -070012019
12020 tg3_halt(tp, RESET_KIND_SUSPEND, 1);
Michael Chanec41c7d2006-01-17 02:40:55 -080012021 err = tg3_nvram_lock(tp);
Michael Chana71116d2005-05-29 14:58:11 -070012022 tg3_halt_cpu(tp, RX_CPU_BASE);
Joe Perches63c3a662011-04-26 08:12:10 +000012023 if (!tg3_flag(tp, 5705_PLUS))
Michael Chana71116d2005-05-29 14:58:11 -070012024 tg3_halt_cpu(tp, TX_CPU_BASE);
Michael Chanec41c7d2006-01-17 02:40:55 -080012025 if (!err)
12026 tg3_nvram_unlock(tp);
Michael Chana71116d2005-05-29 14:58:11 -070012027
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012028 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
Michael Chand9ab5ad2006-03-20 22:27:35 -080012029 tg3_phy_reset(tp);
12030
Michael Chana71116d2005-05-29 14:58:11 -070012031 if (tg3_test_registers(tp) != 0) {
12032 etest->flags |= ETH_TEST_FL_FAILED;
12033 data[2] = 1;
12034 }
Matt Carlson28a45952011-08-19 13:58:22 +000012035
Michael Chan7942e1d2005-05-29 14:58:36 -070012036 if (tg3_test_memory(tp) != 0) {
12037 etest->flags |= ETH_TEST_FL_FAILED;
12038 data[3] = 1;
12039 }
Matt Carlson28a45952011-08-19 13:58:22 +000012040
Matt Carlson941ec902011-08-19 13:58:23 +000012041 if (doextlpbk)
12042 etest->flags |= ETH_TEST_FL_EXTERNAL_LB_DONE;
12043
12044 if (tg3_test_loopback(tp, &data[4], doextlpbk))
Michael Chanc76949a2005-05-29 14:58:59 -070012045 etest->flags |= ETH_TEST_FL_FAILED;
Michael Chana71116d2005-05-29 14:58:11 -070012046
David S. Millerf47c11e2005-06-24 20:18:35 -070012047 tg3_full_unlock(tp);
12048
Michael Chand4bc3922005-05-29 14:59:20 -070012049 if (tg3_test_interrupt(tp) != 0) {
12050 etest->flags |= ETH_TEST_FL_FAILED;
Matt Carlson941ec902011-08-19 13:58:23 +000012051 data[7] = 1;
Michael Chand4bc3922005-05-29 14:59:20 -070012052 }
David S. Millerf47c11e2005-06-24 20:18:35 -070012053
12054 tg3_full_lock(tp, 0);
Michael Chand4bc3922005-05-29 14:59:20 -070012055
Michael Chana71116d2005-05-29 14:58:11 -070012056 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
12057 if (netif_running(dev)) {
Joe Perches63c3a662011-04-26 08:12:10 +000012058 tg3_flag_set(tp, INIT_COMPLETE);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012059 err2 = tg3_restart_hw(tp, 1);
12060 if (!err2)
Michael Chanb9ec6c12006-07-25 16:37:27 -070012061 tg3_netif_start(tp);
Michael Chana71116d2005-05-29 14:58:11 -070012062 }
David S. Millerf47c11e2005-06-24 20:18:35 -070012063
12064 tg3_full_unlock(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012065
12066 if (irq_sync && !err2)
12067 tg3_phy_start(tp);
Michael Chana71116d2005-05-29 14:58:11 -070012068 }
Matt Carlson800960682010-08-02 11:26:06 +000012069 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000012070 tg3_power_down(tp);
Michael Chanbc1c7562006-03-20 17:48:03 -080012071
Michael Chan4cafd3f2005-05-29 14:56:34 -070012072}
12073
Linus Torvalds1da177e2005-04-16 15:20:36 -070012074static int tg3_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
12075{
12076 struct mii_ioctl_data *data = if_mii(ifr);
12077 struct tg3 *tp = netdev_priv(dev);
12078 int err;
12079
Joe Perches63c3a662011-04-26 08:12:10 +000012080 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000012081 struct phy_device *phydev;
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012082 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012083 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000012084 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Richard Cochran28b04112010-07-17 08:48:55 +000012085 return phy_mii_ioctl(phydev, ifr, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012086 }
12087
Matt Carlson33f401a2010-04-05 10:19:27 +000012088 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012089 case SIOCGMIIPHY:
Matt Carlson882e9792009-09-01 13:21:36 +000012090 data->phy_id = tp->phy_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012091
12092 /* fallthru */
12093 case SIOCGMIIREG: {
12094 u32 mii_regval;
12095
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012096 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012097 break; /* We have no PHY */
12098
Matt Carlson34eea5a2011-04-20 07:57:38 +000012099 if (!netif_running(dev))
Michael Chanbc1c7562006-03-20 17:48:03 -080012100 return -EAGAIN;
12101
David S. Millerf47c11e2005-06-24 20:18:35 -070012102 spin_lock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012103 err = tg3_readphy(tp, data->reg_num & 0x1f, &mii_regval);
David S. Millerf47c11e2005-06-24 20:18:35 -070012104 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012105
12106 data->val_out = mii_regval;
12107
12108 return err;
12109 }
12110
12111 case SIOCSMIIREG:
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012112 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012113 break; /* We have no PHY */
12114
Matt Carlson34eea5a2011-04-20 07:57:38 +000012115 if (!netif_running(dev))
Michael Chanbc1c7562006-03-20 17:48:03 -080012116 return -EAGAIN;
12117
David S. Millerf47c11e2005-06-24 20:18:35 -070012118 spin_lock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012119 err = tg3_writephy(tp, data->reg_num & 0x1f, data->val_in);
David S. Millerf47c11e2005-06-24 20:18:35 -070012120 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012121
12122 return err;
12123
12124 default:
12125 /* do nothing */
12126 break;
12127 }
12128 return -EOPNOTSUPP;
12129}
12130
David S. Miller15f98502005-05-18 22:49:26 -070012131static int tg3_get_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
12132{
12133 struct tg3 *tp = netdev_priv(dev);
12134
12135 memcpy(ec, &tp->coal, sizeof(*ec));
12136 return 0;
12137}
12138
Michael Chand244c892005-07-05 14:42:33 -070012139static int tg3_set_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
12140{
12141 struct tg3 *tp = netdev_priv(dev);
12142 u32 max_rxcoal_tick_int = 0, max_txcoal_tick_int = 0;
12143 u32 max_stat_coal_ticks = 0, min_stat_coal_ticks = 0;
12144
Joe Perches63c3a662011-04-26 08:12:10 +000012145 if (!tg3_flag(tp, 5705_PLUS)) {
Michael Chand244c892005-07-05 14:42:33 -070012146 max_rxcoal_tick_int = MAX_RXCOAL_TICK_INT;
12147 max_txcoal_tick_int = MAX_TXCOAL_TICK_INT;
12148 max_stat_coal_ticks = MAX_STAT_COAL_TICKS;
12149 min_stat_coal_ticks = MIN_STAT_COAL_TICKS;
12150 }
12151
12152 if ((ec->rx_coalesce_usecs > MAX_RXCOL_TICKS) ||
12153 (ec->tx_coalesce_usecs > MAX_TXCOL_TICKS) ||
12154 (ec->rx_max_coalesced_frames > MAX_RXMAX_FRAMES) ||
12155 (ec->tx_max_coalesced_frames > MAX_TXMAX_FRAMES) ||
12156 (ec->rx_coalesce_usecs_irq > max_rxcoal_tick_int) ||
12157 (ec->tx_coalesce_usecs_irq > max_txcoal_tick_int) ||
12158 (ec->rx_max_coalesced_frames_irq > MAX_RXCOAL_MAXF_INT) ||
12159 (ec->tx_max_coalesced_frames_irq > MAX_TXCOAL_MAXF_INT) ||
12160 (ec->stats_block_coalesce_usecs > max_stat_coal_ticks) ||
12161 (ec->stats_block_coalesce_usecs < min_stat_coal_ticks))
12162 return -EINVAL;
12163
12164 /* No rx interrupts will be generated if both are zero */
12165 if ((ec->rx_coalesce_usecs == 0) &&
12166 (ec->rx_max_coalesced_frames == 0))
12167 return -EINVAL;
12168
12169 /* No tx interrupts will be generated if both are zero */
12170 if ((ec->tx_coalesce_usecs == 0) &&
12171 (ec->tx_max_coalesced_frames == 0))
12172 return -EINVAL;
12173
12174 /* Only copy relevant parameters, ignore all others. */
12175 tp->coal.rx_coalesce_usecs = ec->rx_coalesce_usecs;
12176 tp->coal.tx_coalesce_usecs = ec->tx_coalesce_usecs;
12177 tp->coal.rx_max_coalesced_frames = ec->rx_max_coalesced_frames;
12178 tp->coal.tx_max_coalesced_frames = ec->tx_max_coalesced_frames;
12179 tp->coal.rx_coalesce_usecs_irq = ec->rx_coalesce_usecs_irq;
12180 tp->coal.tx_coalesce_usecs_irq = ec->tx_coalesce_usecs_irq;
12181 tp->coal.rx_max_coalesced_frames_irq = ec->rx_max_coalesced_frames_irq;
12182 tp->coal.tx_max_coalesced_frames_irq = ec->tx_max_coalesced_frames_irq;
12183 tp->coal.stats_block_coalesce_usecs = ec->stats_block_coalesce_usecs;
12184
12185 if (netif_running(dev)) {
12186 tg3_full_lock(tp, 0);
12187 __tg3_set_coalesce(tp, &tp->coal);
12188 tg3_full_unlock(tp);
12189 }
12190 return 0;
12191}
12192
Jeff Garzik7282d492006-09-13 14:30:00 -040012193static const struct ethtool_ops tg3_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012194 .get_settings = tg3_get_settings,
12195 .set_settings = tg3_set_settings,
12196 .get_drvinfo = tg3_get_drvinfo,
12197 .get_regs_len = tg3_get_regs_len,
12198 .get_regs = tg3_get_regs,
12199 .get_wol = tg3_get_wol,
12200 .set_wol = tg3_set_wol,
12201 .get_msglevel = tg3_get_msglevel,
12202 .set_msglevel = tg3_set_msglevel,
12203 .nway_reset = tg3_nway_reset,
12204 .get_link = ethtool_op_get_link,
12205 .get_eeprom_len = tg3_get_eeprom_len,
12206 .get_eeprom = tg3_get_eeprom,
12207 .set_eeprom = tg3_set_eeprom,
12208 .get_ringparam = tg3_get_ringparam,
12209 .set_ringparam = tg3_set_ringparam,
12210 .get_pauseparam = tg3_get_pauseparam,
12211 .set_pauseparam = tg3_set_pauseparam,
Michael Chan4cafd3f2005-05-29 14:56:34 -070012212 .self_test = tg3_self_test,
Linus Torvalds1da177e2005-04-16 15:20:36 -070012213 .get_strings = tg3_get_strings,
stephen hemminger81b87092011-04-04 08:43:50 +000012214 .set_phys_id = tg3_set_phys_id,
Linus Torvalds1da177e2005-04-16 15:20:36 -070012215 .get_ethtool_stats = tg3_get_ethtool_stats,
David S. Miller15f98502005-05-18 22:49:26 -070012216 .get_coalesce = tg3_get_coalesce,
Michael Chand244c892005-07-05 14:42:33 -070012217 .set_coalesce = tg3_set_coalesce,
Jeff Garzikb9f2c042007-10-03 18:07:32 -070012218 .get_sset_count = tg3_get_sset_count,
Matt Carlson90415472011-12-16 13:33:23 +000012219 .get_rxnfc = tg3_get_rxnfc,
12220 .get_rxfh_indir_size = tg3_get_rxfh_indir_size,
12221 .get_rxfh_indir = tg3_get_rxfh_indir,
12222 .set_rxfh_indir = tg3_set_rxfh_indir,
Linus Torvalds1da177e2005-04-16 15:20:36 -070012223};
12224
Matt Carlsonccd5ba92012-02-13 10:20:08 +000012225static void tg3_set_rx_mode(struct net_device *dev)
12226{
12227 struct tg3 *tp = netdev_priv(dev);
12228
12229 if (!netif_running(dev))
12230 return;
12231
12232 tg3_full_lock(tp, 0);
12233 __tg3_set_rx_mode(dev);
12234 tg3_full_unlock(tp);
12235}
12236
Matt Carlsonfaf16272012-02-13 10:20:07 +000012237static inline void tg3_set_mtu(struct net_device *dev, struct tg3 *tp,
12238 int new_mtu)
12239{
12240 dev->mtu = new_mtu;
12241
12242 if (new_mtu > ETH_DATA_LEN) {
12243 if (tg3_flag(tp, 5780_CLASS)) {
12244 netdev_update_features(dev);
12245 tg3_flag_clear(tp, TSO_CAPABLE);
12246 } else {
12247 tg3_flag_set(tp, JUMBO_RING_ENABLE);
12248 }
12249 } else {
12250 if (tg3_flag(tp, 5780_CLASS)) {
12251 tg3_flag_set(tp, TSO_CAPABLE);
12252 netdev_update_features(dev);
12253 }
12254 tg3_flag_clear(tp, JUMBO_RING_ENABLE);
12255 }
12256}
12257
12258static int tg3_change_mtu(struct net_device *dev, int new_mtu)
12259{
12260 struct tg3 *tp = netdev_priv(dev);
12261 int err;
12262
12263 if (new_mtu < TG3_MIN_MTU || new_mtu > TG3_MAX_MTU(tp))
12264 return -EINVAL;
12265
12266 if (!netif_running(dev)) {
12267 /* We'll just catch it later when the
12268 * device is up'd.
12269 */
12270 tg3_set_mtu(dev, tp, new_mtu);
12271 return 0;
12272 }
12273
12274 tg3_phy_stop(tp);
12275
12276 tg3_netif_stop(tp);
12277
12278 tg3_full_lock(tp, 1);
12279
12280 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
12281
12282 tg3_set_mtu(dev, tp, new_mtu);
12283
12284 err = tg3_restart_hw(tp, 0);
12285
12286 if (!err)
12287 tg3_netif_start(tp);
12288
12289 tg3_full_unlock(tp);
12290
12291 if (!err)
12292 tg3_phy_start(tp);
12293
12294 return err;
12295}
12296
12297static const struct net_device_ops tg3_netdev_ops = {
12298 .ndo_open = tg3_open,
12299 .ndo_stop = tg3_close,
12300 .ndo_start_xmit = tg3_start_xmit,
12301 .ndo_get_stats64 = tg3_get_stats64,
12302 .ndo_validate_addr = eth_validate_addr,
12303 .ndo_set_rx_mode = tg3_set_rx_mode,
12304 .ndo_set_mac_address = tg3_set_mac_addr,
12305 .ndo_do_ioctl = tg3_ioctl,
12306 .ndo_tx_timeout = tg3_tx_timeout,
12307 .ndo_change_mtu = tg3_change_mtu,
12308 .ndo_fix_features = tg3_fix_features,
12309 .ndo_set_features = tg3_set_features,
12310#ifdef CONFIG_NET_POLL_CONTROLLER
12311 .ndo_poll_controller = tg3_poll_controller,
12312#endif
12313};
12314
Linus Torvalds1da177e2005-04-16 15:20:36 -070012315static void __devinit tg3_get_eeprom_size(struct tg3 *tp)
12316{
Michael Chan1b277772006-03-20 22:27:48 -080012317 u32 cursize, val, magic;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012318
12319 tp->nvram_size = EEPROM_CHIP_SIZE;
12320
Matt Carlsone4f34112009-02-25 14:25:00 +000012321 if (tg3_nvram_read(tp, 0, &magic) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012322 return;
12323
Michael Chanb16250e2006-09-27 16:10:14 -070012324 if ((magic != TG3_EEPROM_MAGIC) &&
12325 ((magic & TG3_EEPROM_MAGIC_FW_MSK) != TG3_EEPROM_MAGIC_FW) &&
12326 ((magic & TG3_EEPROM_MAGIC_HW_MSK) != TG3_EEPROM_MAGIC_HW))
Linus Torvalds1da177e2005-04-16 15:20:36 -070012327 return;
12328
12329 /*
12330 * Size the chip by reading offsets at increasing powers of two.
12331 * When we encounter our validation signature, we know the addressing
12332 * has wrapped around, and thus have our chip size.
12333 */
Michael Chan1b277772006-03-20 22:27:48 -080012334 cursize = 0x10;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012335
12336 while (cursize < tp->nvram_size) {
Matt Carlsone4f34112009-02-25 14:25:00 +000012337 if (tg3_nvram_read(tp, cursize, &val) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012338 return;
12339
Michael Chan18201802006-03-20 22:29:15 -080012340 if (val == magic)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012341 break;
12342
12343 cursize <<= 1;
12344 }
12345
12346 tp->nvram_size = cursize;
12347}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012348
Linus Torvalds1da177e2005-04-16 15:20:36 -070012349static void __devinit tg3_get_nvram_size(struct tg3 *tp)
12350{
12351 u32 val;
12352
Joe Perches63c3a662011-04-26 08:12:10 +000012353 if (tg3_flag(tp, NO_NVRAM) || tg3_nvram_read(tp, 0, &val) != 0)
Michael Chan1b277772006-03-20 22:27:48 -080012354 return;
12355
12356 /* Selfboot format */
Michael Chan18201802006-03-20 22:29:15 -080012357 if (val != TG3_EEPROM_MAGIC) {
Michael Chan1b277772006-03-20 22:27:48 -080012358 tg3_get_eeprom_size(tp);
12359 return;
12360 }
12361
Matt Carlson6d348f22009-02-25 14:25:52 +000012362 if (tg3_nvram_read(tp, 0xf0, &val) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012363 if (val != 0) {
Matt Carlson6d348f22009-02-25 14:25:52 +000012364 /* This is confusing. We want to operate on the
12365 * 16-bit value at offset 0xf2. The tg3_nvram_read()
12366 * call will read from NVRAM and byteswap the data
12367 * according to the byteswapping settings for all
12368 * other register accesses. This ensures the data we
12369 * want will always reside in the lower 16-bits.
12370 * However, the data in NVRAM is in LE format, which
12371 * means the data from the NVRAM read will always be
12372 * opposite the endianness of the CPU. The 16-bit
12373 * byteswap then brings the data to CPU endianness.
12374 */
12375 tp->nvram_size = swab16((u16)(val & 0x0000ffff)) * 1024;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012376 return;
12377 }
12378 }
Matt Carlsonfd1122a2008-05-02 16:48:36 -070012379 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012380}
12381
12382static void __devinit tg3_get_nvram_info(struct tg3 *tp)
12383{
12384 u32 nvcfg1;
12385
12386 nvcfg1 = tr32(NVRAM_CFG1);
12387 if (nvcfg1 & NVRAM_CFG1_FLASHIF_ENAB) {
Joe Perches63c3a662011-04-26 08:12:10 +000012388 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000012389 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012390 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
12391 tw32(NVRAM_CFG1, nvcfg1);
12392 }
12393
Matt Carlson6ff6f812011-05-19 12:12:54 +000012394 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 ||
Joe Perches63c3a662011-04-26 08:12:10 +000012395 tg3_flag(tp, 5780_CLASS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012396 switch (nvcfg1 & NVRAM_CFG1_VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000012397 case FLASH_VENDOR_ATMEL_FLASH_BUFFERED:
12398 tp->nvram_jedecnum = JEDEC_ATMEL;
12399 tp->nvram_pagesize = ATMEL_AT45DB0X1B_PAGE_SIZE;
Joe Perches63c3a662011-04-26 08:12:10 +000012400 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000012401 break;
12402 case FLASH_VENDOR_ATMEL_FLASH_UNBUFFERED:
12403 tp->nvram_jedecnum = JEDEC_ATMEL;
12404 tp->nvram_pagesize = ATMEL_AT25F512_PAGE_SIZE;
12405 break;
12406 case FLASH_VENDOR_ATMEL_EEPROM:
12407 tp->nvram_jedecnum = JEDEC_ATMEL;
12408 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
Joe Perches63c3a662011-04-26 08:12:10 +000012409 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000012410 break;
12411 case FLASH_VENDOR_ST:
12412 tp->nvram_jedecnum = JEDEC_ST;
12413 tp->nvram_pagesize = ST_M45PEX0_PAGE_SIZE;
Joe Perches63c3a662011-04-26 08:12:10 +000012414 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000012415 break;
12416 case FLASH_VENDOR_SAIFUN:
12417 tp->nvram_jedecnum = JEDEC_SAIFUN;
12418 tp->nvram_pagesize = SAIFUN_SA25F0XX_PAGE_SIZE;
12419 break;
12420 case FLASH_VENDOR_SST_SMALL:
12421 case FLASH_VENDOR_SST_LARGE:
12422 tp->nvram_jedecnum = JEDEC_SST;
12423 tp->nvram_pagesize = SST_25VF0X0_PAGE_SIZE;
12424 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012425 }
Matt Carlson8590a602009-08-28 12:29:16 +000012426 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012427 tp->nvram_jedecnum = JEDEC_ATMEL;
12428 tp->nvram_pagesize = ATMEL_AT45DB0X1B_PAGE_SIZE;
Joe Perches63c3a662011-04-26 08:12:10 +000012429 tg3_flag_set(tp, NVRAM_BUFFERED);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012430 }
12431}
12432
Matt Carlsona1b950d2009-09-01 13:20:17 +000012433static void __devinit tg3_nvram_get_pagesize(struct tg3 *tp, u32 nvmcfg1)
12434{
12435 switch (nvmcfg1 & NVRAM_CFG1_5752PAGE_SIZE_MASK) {
12436 case FLASH_5752PAGE_SIZE_256:
12437 tp->nvram_pagesize = 256;
12438 break;
12439 case FLASH_5752PAGE_SIZE_512:
12440 tp->nvram_pagesize = 512;
12441 break;
12442 case FLASH_5752PAGE_SIZE_1K:
12443 tp->nvram_pagesize = 1024;
12444 break;
12445 case FLASH_5752PAGE_SIZE_2K:
12446 tp->nvram_pagesize = 2048;
12447 break;
12448 case FLASH_5752PAGE_SIZE_4K:
12449 tp->nvram_pagesize = 4096;
12450 break;
12451 case FLASH_5752PAGE_SIZE_264:
12452 tp->nvram_pagesize = 264;
12453 break;
12454 case FLASH_5752PAGE_SIZE_528:
12455 tp->nvram_pagesize = 528;
12456 break;
12457 }
12458}
12459
Michael Chan361b4ac2005-04-21 17:11:21 -070012460static void __devinit tg3_get_5752_nvram_info(struct tg3 *tp)
12461{
12462 u32 nvcfg1;
12463
12464 nvcfg1 = tr32(NVRAM_CFG1);
12465
Michael Chane6af3012005-04-21 17:12:05 -070012466 /* NVRAM protection for TPM */
12467 if (nvcfg1 & (1 << 27))
Joe Perches63c3a662011-04-26 08:12:10 +000012468 tg3_flag_set(tp, PROTECTED_NVRAM);
Michael Chane6af3012005-04-21 17:12:05 -070012469
Michael Chan361b4ac2005-04-21 17:11:21 -070012470 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000012471 case FLASH_5752VENDOR_ATMEL_EEPROM_64KHZ:
12472 case FLASH_5752VENDOR_ATMEL_EEPROM_376KHZ:
12473 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012474 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000012475 break;
12476 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
12477 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012478 tg3_flag_set(tp, NVRAM_BUFFERED);
12479 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000012480 break;
12481 case FLASH_5752VENDOR_ST_M45PE10:
12482 case FLASH_5752VENDOR_ST_M45PE20:
12483 case FLASH_5752VENDOR_ST_M45PE40:
12484 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000012485 tg3_flag_set(tp, NVRAM_BUFFERED);
12486 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000012487 break;
Michael Chan361b4ac2005-04-21 17:11:21 -070012488 }
12489
Joe Perches63c3a662011-04-26 08:12:10 +000012490 if (tg3_flag(tp, FLASH)) {
Matt Carlsona1b950d2009-09-01 13:20:17 +000012491 tg3_nvram_get_pagesize(tp, nvcfg1);
Matt Carlson8590a602009-08-28 12:29:16 +000012492 } else {
Michael Chan361b4ac2005-04-21 17:11:21 -070012493 /* For eeprom, set pagesize to maximum eeprom size */
12494 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
12495
12496 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
12497 tw32(NVRAM_CFG1, nvcfg1);
12498 }
12499}
12500
Michael Chand3c7b882006-03-23 01:28:25 -080012501static void __devinit tg3_get_5755_nvram_info(struct tg3 *tp)
12502{
Matt Carlson989a9d22007-05-05 11:51:05 -070012503 u32 nvcfg1, protect = 0;
Michael Chand3c7b882006-03-23 01:28:25 -080012504
12505 nvcfg1 = tr32(NVRAM_CFG1);
12506
12507 /* NVRAM protection for TPM */
Matt Carlson989a9d22007-05-05 11:51:05 -070012508 if (nvcfg1 & (1 << 27)) {
Joe Perches63c3a662011-04-26 08:12:10 +000012509 tg3_flag_set(tp, PROTECTED_NVRAM);
Matt Carlson989a9d22007-05-05 11:51:05 -070012510 protect = 1;
12511 }
Michael Chand3c7b882006-03-23 01:28:25 -080012512
Matt Carlson989a9d22007-05-05 11:51:05 -070012513 nvcfg1 &= NVRAM_CFG1_5752VENDOR_MASK;
12514 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000012515 case FLASH_5755VENDOR_ATMEL_FLASH_1:
12516 case FLASH_5755VENDOR_ATMEL_FLASH_2:
12517 case FLASH_5755VENDOR_ATMEL_FLASH_3:
12518 case FLASH_5755VENDOR_ATMEL_FLASH_5:
12519 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012520 tg3_flag_set(tp, NVRAM_BUFFERED);
12521 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000012522 tp->nvram_pagesize = 264;
12523 if (nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_1 ||
12524 nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_5)
12525 tp->nvram_size = (protect ? 0x3e200 :
12526 TG3_NVRAM_SIZE_512KB);
12527 else if (nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_2)
12528 tp->nvram_size = (protect ? 0x1f200 :
12529 TG3_NVRAM_SIZE_256KB);
12530 else
12531 tp->nvram_size = (protect ? 0x1f200 :
12532 TG3_NVRAM_SIZE_128KB);
12533 break;
12534 case FLASH_5752VENDOR_ST_M45PE10:
12535 case FLASH_5752VENDOR_ST_M45PE20:
12536 case FLASH_5752VENDOR_ST_M45PE40:
12537 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000012538 tg3_flag_set(tp, NVRAM_BUFFERED);
12539 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000012540 tp->nvram_pagesize = 256;
12541 if (nvcfg1 == FLASH_5752VENDOR_ST_M45PE10)
12542 tp->nvram_size = (protect ?
12543 TG3_NVRAM_SIZE_64KB :
12544 TG3_NVRAM_SIZE_128KB);
12545 else if (nvcfg1 == FLASH_5752VENDOR_ST_M45PE20)
12546 tp->nvram_size = (protect ?
12547 TG3_NVRAM_SIZE_64KB :
12548 TG3_NVRAM_SIZE_256KB);
12549 else
12550 tp->nvram_size = (protect ?
12551 TG3_NVRAM_SIZE_128KB :
12552 TG3_NVRAM_SIZE_512KB);
12553 break;
Michael Chand3c7b882006-03-23 01:28:25 -080012554 }
12555}
12556
Michael Chan1b277772006-03-20 22:27:48 -080012557static void __devinit tg3_get_5787_nvram_info(struct tg3 *tp)
12558{
12559 u32 nvcfg1;
12560
12561 nvcfg1 = tr32(NVRAM_CFG1);
12562
12563 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000012564 case FLASH_5787VENDOR_ATMEL_EEPROM_64KHZ:
12565 case FLASH_5787VENDOR_ATMEL_EEPROM_376KHZ:
12566 case FLASH_5787VENDOR_MICRO_EEPROM_64KHZ:
12567 case FLASH_5787VENDOR_MICRO_EEPROM_376KHZ:
12568 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012569 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000012570 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
Michael Chan1b277772006-03-20 22:27:48 -080012571
Matt Carlson8590a602009-08-28 12:29:16 +000012572 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
12573 tw32(NVRAM_CFG1, nvcfg1);
12574 break;
12575 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
12576 case FLASH_5755VENDOR_ATMEL_FLASH_1:
12577 case FLASH_5755VENDOR_ATMEL_FLASH_2:
12578 case FLASH_5755VENDOR_ATMEL_FLASH_3:
12579 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012580 tg3_flag_set(tp, NVRAM_BUFFERED);
12581 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000012582 tp->nvram_pagesize = 264;
12583 break;
12584 case FLASH_5752VENDOR_ST_M45PE10:
12585 case FLASH_5752VENDOR_ST_M45PE20:
12586 case FLASH_5752VENDOR_ST_M45PE40:
12587 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000012588 tg3_flag_set(tp, NVRAM_BUFFERED);
12589 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000012590 tp->nvram_pagesize = 256;
12591 break;
Michael Chan1b277772006-03-20 22:27:48 -080012592 }
12593}
12594
Matt Carlson6b91fa02007-10-10 18:01:09 -070012595static void __devinit tg3_get_5761_nvram_info(struct tg3 *tp)
12596{
12597 u32 nvcfg1, protect = 0;
12598
12599 nvcfg1 = tr32(NVRAM_CFG1);
12600
12601 /* NVRAM protection for TPM */
12602 if (nvcfg1 & (1 << 27)) {
Joe Perches63c3a662011-04-26 08:12:10 +000012603 tg3_flag_set(tp, PROTECTED_NVRAM);
Matt Carlson6b91fa02007-10-10 18:01:09 -070012604 protect = 1;
12605 }
12606
12607 nvcfg1 &= NVRAM_CFG1_5752VENDOR_MASK;
12608 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000012609 case FLASH_5761VENDOR_ATMEL_ADB021D:
12610 case FLASH_5761VENDOR_ATMEL_ADB041D:
12611 case FLASH_5761VENDOR_ATMEL_ADB081D:
12612 case FLASH_5761VENDOR_ATMEL_ADB161D:
12613 case FLASH_5761VENDOR_ATMEL_MDB021D:
12614 case FLASH_5761VENDOR_ATMEL_MDB041D:
12615 case FLASH_5761VENDOR_ATMEL_MDB081D:
12616 case FLASH_5761VENDOR_ATMEL_MDB161D:
12617 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012618 tg3_flag_set(tp, NVRAM_BUFFERED);
12619 tg3_flag_set(tp, FLASH);
12620 tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
Matt Carlson8590a602009-08-28 12:29:16 +000012621 tp->nvram_pagesize = 256;
12622 break;
12623 case FLASH_5761VENDOR_ST_A_M45PE20:
12624 case FLASH_5761VENDOR_ST_A_M45PE40:
12625 case FLASH_5761VENDOR_ST_A_M45PE80:
12626 case FLASH_5761VENDOR_ST_A_M45PE16:
12627 case FLASH_5761VENDOR_ST_M_M45PE20:
12628 case FLASH_5761VENDOR_ST_M_M45PE40:
12629 case FLASH_5761VENDOR_ST_M_M45PE80:
12630 case FLASH_5761VENDOR_ST_M_M45PE16:
12631 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000012632 tg3_flag_set(tp, NVRAM_BUFFERED);
12633 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000012634 tp->nvram_pagesize = 256;
12635 break;
Matt Carlson6b91fa02007-10-10 18:01:09 -070012636 }
12637
12638 if (protect) {
12639 tp->nvram_size = tr32(NVRAM_ADDR_LOCKOUT);
12640 } else {
12641 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000012642 case FLASH_5761VENDOR_ATMEL_ADB161D:
12643 case FLASH_5761VENDOR_ATMEL_MDB161D:
12644 case FLASH_5761VENDOR_ST_A_M45PE16:
12645 case FLASH_5761VENDOR_ST_M_M45PE16:
12646 tp->nvram_size = TG3_NVRAM_SIZE_2MB;
12647 break;
12648 case FLASH_5761VENDOR_ATMEL_ADB081D:
12649 case FLASH_5761VENDOR_ATMEL_MDB081D:
12650 case FLASH_5761VENDOR_ST_A_M45PE80:
12651 case FLASH_5761VENDOR_ST_M_M45PE80:
12652 tp->nvram_size = TG3_NVRAM_SIZE_1MB;
12653 break;
12654 case FLASH_5761VENDOR_ATMEL_ADB041D:
12655 case FLASH_5761VENDOR_ATMEL_MDB041D:
12656 case FLASH_5761VENDOR_ST_A_M45PE40:
12657 case FLASH_5761VENDOR_ST_M_M45PE40:
12658 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
12659 break;
12660 case FLASH_5761VENDOR_ATMEL_ADB021D:
12661 case FLASH_5761VENDOR_ATMEL_MDB021D:
12662 case FLASH_5761VENDOR_ST_A_M45PE20:
12663 case FLASH_5761VENDOR_ST_M_M45PE20:
12664 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
12665 break;
Matt Carlson6b91fa02007-10-10 18:01:09 -070012666 }
12667 }
12668}
12669
Michael Chanb5d37722006-09-27 16:06:21 -070012670static void __devinit tg3_get_5906_nvram_info(struct tg3 *tp)
12671{
12672 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012673 tg3_flag_set(tp, NVRAM_BUFFERED);
Michael Chanb5d37722006-09-27 16:06:21 -070012674 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
12675}
12676
Matt Carlson321d32a2008-11-21 17:22:19 -080012677static void __devinit tg3_get_57780_nvram_info(struct tg3 *tp)
12678{
12679 u32 nvcfg1;
12680
12681 nvcfg1 = tr32(NVRAM_CFG1);
12682
12683 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
12684 case FLASH_5787VENDOR_ATMEL_EEPROM_376KHZ:
12685 case FLASH_5787VENDOR_MICRO_EEPROM_376KHZ:
12686 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012687 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson321d32a2008-11-21 17:22:19 -080012688 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
12689
12690 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
12691 tw32(NVRAM_CFG1, nvcfg1);
12692 return;
12693 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
12694 case FLASH_57780VENDOR_ATMEL_AT45DB011D:
12695 case FLASH_57780VENDOR_ATMEL_AT45DB011B:
12696 case FLASH_57780VENDOR_ATMEL_AT45DB021D:
12697 case FLASH_57780VENDOR_ATMEL_AT45DB021B:
12698 case FLASH_57780VENDOR_ATMEL_AT45DB041D:
12699 case FLASH_57780VENDOR_ATMEL_AT45DB041B:
12700 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012701 tg3_flag_set(tp, NVRAM_BUFFERED);
12702 tg3_flag_set(tp, FLASH);
Matt Carlson321d32a2008-11-21 17:22:19 -080012703
12704 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
12705 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
12706 case FLASH_57780VENDOR_ATMEL_AT45DB011D:
12707 case FLASH_57780VENDOR_ATMEL_AT45DB011B:
12708 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
12709 break;
12710 case FLASH_57780VENDOR_ATMEL_AT45DB021D:
12711 case FLASH_57780VENDOR_ATMEL_AT45DB021B:
12712 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
12713 break;
12714 case FLASH_57780VENDOR_ATMEL_AT45DB041D:
12715 case FLASH_57780VENDOR_ATMEL_AT45DB041B:
12716 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
12717 break;
12718 }
12719 break;
12720 case FLASH_5752VENDOR_ST_M45PE10:
12721 case FLASH_5752VENDOR_ST_M45PE20:
12722 case FLASH_5752VENDOR_ST_M45PE40:
12723 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000012724 tg3_flag_set(tp, NVRAM_BUFFERED);
12725 tg3_flag_set(tp, FLASH);
Matt Carlson321d32a2008-11-21 17:22:19 -080012726
12727 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
12728 case FLASH_5752VENDOR_ST_M45PE10:
12729 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
12730 break;
12731 case FLASH_5752VENDOR_ST_M45PE20:
12732 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
12733 break;
12734 case FLASH_5752VENDOR_ST_M45PE40:
12735 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
12736 break;
12737 }
12738 break;
12739 default:
Joe Perches63c3a662011-04-26 08:12:10 +000012740 tg3_flag_set(tp, NO_NVRAM);
Matt Carlson321d32a2008-11-21 17:22:19 -080012741 return;
12742 }
12743
Matt Carlsona1b950d2009-09-01 13:20:17 +000012744 tg3_nvram_get_pagesize(tp, nvcfg1);
12745 if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
Joe Perches63c3a662011-04-26 08:12:10 +000012746 tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
Matt Carlsona1b950d2009-09-01 13:20:17 +000012747}
12748
12749
12750static void __devinit tg3_get_5717_nvram_info(struct tg3 *tp)
12751{
12752 u32 nvcfg1;
12753
12754 nvcfg1 = tr32(NVRAM_CFG1);
12755
12756 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
12757 case FLASH_5717VENDOR_ATMEL_EEPROM:
12758 case FLASH_5717VENDOR_MICRO_EEPROM:
12759 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012760 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlsona1b950d2009-09-01 13:20:17 +000012761 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
12762
12763 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
12764 tw32(NVRAM_CFG1, nvcfg1);
12765 return;
12766 case FLASH_5717VENDOR_ATMEL_MDB011D:
12767 case FLASH_5717VENDOR_ATMEL_ADB011B:
12768 case FLASH_5717VENDOR_ATMEL_ADB011D:
12769 case FLASH_5717VENDOR_ATMEL_MDB021D:
12770 case FLASH_5717VENDOR_ATMEL_ADB021B:
12771 case FLASH_5717VENDOR_ATMEL_ADB021D:
12772 case FLASH_5717VENDOR_ATMEL_45USPT:
12773 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012774 tg3_flag_set(tp, NVRAM_BUFFERED);
12775 tg3_flag_set(tp, FLASH);
Matt Carlsona1b950d2009-09-01 13:20:17 +000012776
12777 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
12778 case FLASH_5717VENDOR_ATMEL_MDB021D:
Matt Carlson66ee33b2011-04-05 14:22:51 +000012779 /* Detect size with tg3_nvram_get_size() */
12780 break;
Matt Carlsona1b950d2009-09-01 13:20:17 +000012781 case FLASH_5717VENDOR_ATMEL_ADB021B:
12782 case FLASH_5717VENDOR_ATMEL_ADB021D:
12783 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
12784 break;
12785 default:
12786 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
12787 break;
12788 }
Matt Carlson321d32a2008-11-21 17:22:19 -080012789 break;
Matt Carlsona1b950d2009-09-01 13:20:17 +000012790 case FLASH_5717VENDOR_ST_M_M25PE10:
12791 case FLASH_5717VENDOR_ST_A_M25PE10:
12792 case FLASH_5717VENDOR_ST_M_M45PE10:
12793 case FLASH_5717VENDOR_ST_A_M45PE10:
12794 case FLASH_5717VENDOR_ST_M_M25PE20:
12795 case FLASH_5717VENDOR_ST_A_M25PE20:
12796 case FLASH_5717VENDOR_ST_M_M45PE20:
12797 case FLASH_5717VENDOR_ST_A_M45PE20:
12798 case FLASH_5717VENDOR_ST_25USPT:
12799 case FLASH_5717VENDOR_ST_45USPT:
12800 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000012801 tg3_flag_set(tp, NVRAM_BUFFERED);
12802 tg3_flag_set(tp, FLASH);
Matt Carlsona1b950d2009-09-01 13:20:17 +000012803
12804 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
12805 case FLASH_5717VENDOR_ST_M_M25PE20:
Matt Carlsona1b950d2009-09-01 13:20:17 +000012806 case FLASH_5717VENDOR_ST_M_M45PE20:
Matt Carlson66ee33b2011-04-05 14:22:51 +000012807 /* Detect size with tg3_nvram_get_size() */
12808 break;
12809 case FLASH_5717VENDOR_ST_A_M25PE20:
Matt Carlsona1b950d2009-09-01 13:20:17 +000012810 case FLASH_5717VENDOR_ST_A_M45PE20:
12811 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
12812 break;
12813 default:
12814 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
12815 break;
12816 }
Matt Carlson321d32a2008-11-21 17:22:19 -080012817 break;
Matt Carlsona1b950d2009-09-01 13:20:17 +000012818 default:
Joe Perches63c3a662011-04-26 08:12:10 +000012819 tg3_flag_set(tp, NO_NVRAM);
Matt Carlsona1b950d2009-09-01 13:20:17 +000012820 return;
Matt Carlson321d32a2008-11-21 17:22:19 -080012821 }
Matt Carlsona1b950d2009-09-01 13:20:17 +000012822
12823 tg3_nvram_get_pagesize(tp, nvcfg1);
12824 if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
Joe Perches63c3a662011-04-26 08:12:10 +000012825 tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
Matt Carlson321d32a2008-11-21 17:22:19 -080012826}
12827
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012828static void __devinit tg3_get_5720_nvram_info(struct tg3 *tp)
12829{
12830 u32 nvcfg1, nvmpinstrp;
12831
12832 nvcfg1 = tr32(NVRAM_CFG1);
12833 nvmpinstrp = nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK;
12834
12835 switch (nvmpinstrp) {
12836 case FLASH_5720_EEPROM_HD:
12837 case FLASH_5720_EEPROM_LD:
12838 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012839 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012840
12841 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
12842 tw32(NVRAM_CFG1, nvcfg1);
12843 if (nvmpinstrp == FLASH_5720_EEPROM_HD)
12844 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
12845 else
12846 tp->nvram_pagesize = ATMEL_AT24C02_CHIP_SIZE;
12847 return;
12848 case FLASH_5720VENDOR_M_ATMEL_DB011D:
12849 case FLASH_5720VENDOR_A_ATMEL_DB011B:
12850 case FLASH_5720VENDOR_A_ATMEL_DB011D:
12851 case FLASH_5720VENDOR_M_ATMEL_DB021D:
12852 case FLASH_5720VENDOR_A_ATMEL_DB021B:
12853 case FLASH_5720VENDOR_A_ATMEL_DB021D:
12854 case FLASH_5720VENDOR_M_ATMEL_DB041D:
12855 case FLASH_5720VENDOR_A_ATMEL_DB041B:
12856 case FLASH_5720VENDOR_A_ATMEL_DB041D:
12857 case FLASH_5720VENDOR_M_ATMEL_DB081D:
12858 case FLASH_5720VENDOR_A_ATMEL_DB081D:
12859 case FLASH_5720VENDOR_ATMEL_45USPT:
12860 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012861 tg3_flag_set(tp, NVRAM_BUFFERED);
12862 tg3_flag_set(tp, FLASH);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012863
12864 switch (nvmpinstrp) {
12865 case FLASH_5720VENDOR_M_ATMEL_DB021D:
12866 case FLASH_5720VENDOR_A_ATMEL_DB021B:
12867 case FLASH_5720VENDOR_A_ATMEL_DB021D:
12868 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
12869 break;
12870 case FLASH_5720VENDOR_M_ATMEL_DB041D:
12871 case FLASH_5720VENDOR_A_ATMEL_DB041B:
12872 case FLASH_5720VENDOR_A_ATMEL_DB041D:
12873 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
12874 break;
12875 case FLASH_5720VENDOR_M_ATMEL_DB081D:
12876 case FLASH_5720VENDOR_A_ATMEL_DB081D:
12877 tp->nvram_size = TG3_NVRAM_SIZE_1MB;
12878 break;
12879 default:
12880 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
12881 break;
12882 }
12883 break;
12884 case FLASH_5720VENDOR_M_ST_M25PE10:
12885 case FLASH_5720VENDOR_M_ST_M45PE10:
12886 case FLASH_5720VENDOR_A_ST_M25PE10:
12887 case FLASH_5720VENDOR_A_ST_M45PE10:
12888 case FLASH_5720VENDOR_M_ST_M25PE20:
12889 case FLASH_5720VENDOR_M_ST_M45PE20:
12890 case FLASH_5720VENDOR_A_ST_M25PE20:
12891 case FLASH_5720VENDOR_A_ST_M45PE20:
12892 case FLASH_5720VENDOR_M_ST_M25PE40:
12893 case FLASH_5720VENDOR_M_ST_M45PE40:
12894 case FLASH_5720VENDOR_A_ST_M25PE40:
12895 case FLASH_5720VENDOR_A_ST_M45PE40:
12896 case FLASH_5720VENDOR_M_ST_M25PE80:
12897 case FLASH_5720VENDOR_M_ST_M45PE80:
12898 case FLASH_5720VENDOR_A_ST_M25PE80:
12899 case FLASH_5720VENDOR_A_ST_M45PE80:
12900 case FLASH_5720VENDOR_ST_25USPT:
12901 case FLASH_5720VENDOR_ST_45USPT:
12902 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000012903 tg3_flag_set(tp, NVRAM_BUFFERED);
12904 tg3_flag_set(tp, FLASH);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012905
12906 switch (nvmpinstrp) {
12907 case FLASH_5720VENDOR_M_ST_M25PE20:
12908 case FLASH_5720VENDOR_M_ST_M45PE20:
12909 case FLASH_5720VENDOR_A_ST_M25PE20:
12910 case FLASH_5720VENDOR_A_ST_M45PE20:
12911 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
12912 break;
12913 case FLASH_5720VENDOR_M_ST_M25PE40:
12914 case FLASH_5720VENDOR_M_ST_M45PE40:
12915 case FLASH_5720VENDOR_A_ST_M25PE40:
12916 case FLASH_5720VENDOR_A_ST_M45PE40:
12917 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
12918 break;
12919 case FLASH_5720VENDOR_M_ST_M25PE80:
12920 case FLASH_5720VENDOR_M_ST_M45PE80:
12921 case FLASH_5720VENDOR_A_ST_M25PE80:
12922 case FLASH_5720VENDOR_A_ST_M45PE80:
12923 tp->nvram_size = TG3_NVRAM_SIZE_1MB;
12924 break;
12925 default:
12926 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
12927 break;
12928 }
12929 break;
12930 default:
Joe Perches63c3a662011-04-26 08:12:10 +000012931 tg3_flag_set(tp, NO_NVRAM);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012932 return;
12933 }
12934
12935 tg3_nvram_get_pagesize(tp, nvcfg1);
12936 if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
Joe Perches63c3a662011-04-26 08:12:10 +000012937 tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012938}
12939
Linus Torvalds1da177e2005-04-16 15:20:36 -070012940/* Chips other than 5700/5701 use the NVRAM for fetching info. */
12941static void __devinit tg3_nvram_init(struct tg3 *tp)
12942{
Linus Torvalds1da177e2005-04-16 15:20:36 -070012943 tw32_f(GRC_EEPROM_ADDR,
12944 (EEPROM_ADDR_FSM_RESET |
12945 (EEPROM_DEFAULT_CLOCK_PERIOD <<
12946 EEPROM_ADDR_CLKPERD_SHIFT)));
12947
Michael Chan9d57f012006-12-07 00:23:25 -080012948 msleep(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012949
12950 /* Enable seeprom accesses. */
12951 tw32_f(GRC_LOCAL_CTRL,
12952 tr32(GRC_LOCAL_CTRL) | GRC_LCLCTRL_AUTO_SEEPROM);
12953 udelay(100);
12954
12955 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
12956 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701) {
Joe Perches63c3a662011-04-26 08:12:10 +000012957 tg3_flag_set(tp, NVRAM);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012958
Michael Chanec41c7d2006-01-17 02:40:55 -080012959 if (tg3_nvram_lock(tp)) {
Matt Carlson5129c3a2010-04-05 10:19:23 +000012960 netdev_warn(tp->dev,
12961 "Cannot get nvram lock, %s failed\n",
Joe Perches05dbe002010-02-17 19:44:19 +000012962 __func__);
Michael Chanec41c7d2006-01-17 02:40:55 -080012963 return;
12964 }
Michael Chane6af3012005-04-21 17:12:05 -070012965 tg3_enable_nvram_access(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012966
Matt Carlson989a9d22007-05-05 11:51:05 -070012967 tp->nvram_size = 0;
12968
Michael Chan361b4ac2005-04-21 17:11:21 -070012969 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752)
12970 tg3_get_5752_nvram_info(tp);
Michael Chand3c7b882006-03-23 01:28:25 -080012971 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755)
12972 tg3_get_5755_nvram_info(tp);
Matt Carlsond30cdd22007-10-07 23:28:35 -070012973 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787 ||
Matt Carlson57e69832008-05-25 23:48:31 -070012974 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
12975 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
Michael Chan1b277772006-03-20 22:27:48 -080012976 tg3_get_5787_nvram_info(tp);
Matt Carlson6b91fa02007-10-10 18:01:09 -070012977 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
12978 tg3_get_5761_nvram_info(tp);
Michael Chanb5d37722006-09-27 16:06:21 -070012979 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
12980 tg3_get_5906_nvram_info(tp);
Matt Carlsonb703df62009-12-03 08:36:21 +000012981 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
Matt Carlson55086ad2011-12-14 11:09:59 +000012982 tg3_flag(tp, 57765_CLASS))
Matt Carlson321d32a2008-11-21 17:22:19 -080012983 tg3_get_57780_nvram_info(tp);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012984 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
12985 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
Matt Carlsona1b950d2009-09-01 13:20:17 +000012986 tg3_get_5717_nvram_info(tp);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012987 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
12988 tg3_get_5720_nvram_info(tp);
Michael Chan361b4ac2005-04-21 17:11:21 -070012989 else
12990 tg3_get_nvram_info(tp);
12991
Matt Carlson989a9d22007-05-05 11:51:05 -070012992 if (tp->nvram_size == 0)
12993 tg3_get_nvram_size(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012994
Michael Chane6af3012005-04-21 17:12:05 -070012995 tg3_disable_nvram_access(tp);
Michael Chan381291b2005-12-13 21:08:21 -080012996 tg3_nvram_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012997
12998 } else {
Joe Perches63c3a662011-04-26 08:12:10 +000012999 tg3_flag_clear(tp, NVRAM);
13000 tg3_flag_clear(tp, NVRAM_BUFFERED);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013001
13002 tg3_get_eeprom_size(tp);
13003 }
13004}
13005
Linus Torvalds1da177e2005-04-16 15:20:36 -070013006struct subsys_tbl_ent {
13007 u16 subsys_vendor, subsys_devid;
13008 u32 phy_id;
13009};
13010
Matt Carlson24daf2b2010-02-17 15:17:02 +000013011static struct subsys_tbl_ent subsys_id_to_phy_id[] __devinitdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070013012 /* Broadcom boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000013013 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000013014 TG3PCI_SUBDEVICE_ID_BROADCOM_95700A6, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013015 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000013016 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A5, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013017 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000013018 TG3PCI_SUBDEVICE_ID_BROADCOM_95700T6, TG3_PHY_ID_BCM8002 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013019 { TG3PCI_SUBVENDOR_ID_BROADCOM,
13020 TG3PCI_SUBDEVICE_ID_BROADCOM_95700A9, 0 },
13021 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000013022 TG3PCI_SUBDEVICE_ID_BROADCOM_95701T1, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013023 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000013024 TG3PCI_SUBDEVICE_ID_BROADCOM_95701T8, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013025 { TG3PCI_SUBVENDOR_ID_BROADCOM,
13026 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A7, 0 },
13027 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000013028 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A10, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013029 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000013030 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A12, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013031 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000013032 TG3PCI_SUBDEVICE_ID_BROADCOM_95703AX1, TG3_PHY_ID_BCM5703 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013033 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000013034 TG3PCI_SUBDEVICE_ID_BROADCOM_95703AX2, TG3_PHY_ID_BCM5703 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070013035
13036 /* 3com boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000013037 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000013038 TG3PCI_SUBDEVICE_ID_3COM_3C996T, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013039 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000013040 TG3PCI_SUBDEVICE_ID_3COM_3C996BT, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013041 { TG3PCI_SUBVENDOR_ID_3COM,
13042 TG3PCI_SUBDEVICE_ID_3COM_3C996SX, 0 },
13043 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000013044 TG3PCI_SUBDEVICE_ID_3COM_3C1000T, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013045 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000013046 TG3PCI_SUBDEVICE_ID_3COM_3C940BR01, TG3_PHY_ID_BCM5701 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070013047
13048 /* DELL boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000013049 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000013050 TG3PCI_SUBDEVICE_ID_DELL_VIPER, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013051 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000013052 TG3PCI_SUBDEVICE_ID_DELL_JAGUAR, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013053 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000013054 TG3PCI_SUBDEVICE_ID_DELL_MERLOT, TG3_PHY_ID_BCM5411 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013055 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000013056 TG3PCI_SUBDEVICE_ID_DELL_SLIM_MERLOT, TG3_PHY_ID_BCM5411 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070013057
13058 /* Compaq boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000013059 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000013060 TG3PCI_SUBDEVICE_ID_COMPAQ_BANSHEE, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013061 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000013062 TG3PCI_SUBDEVICE_ID_COMPAQ_BANSHEE_2, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013063 { TG3PCI_SUBVENDOR_ID_COMPAQ,
13064 TG3PCI_SUBDEVICE_ID_COMPAQ_CHANGELING, 0 },
13065 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000013066 TG3PCI_SUBDEVICE_ID_COMPAQ_NC7780, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013067 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000013068 TG3PCI_SUBDEVICE_ID_COMPAQ_NC7780_2, TG3_PHY_ID_BCM5701 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070013069
13070 /* IBM boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000013071 { TG3PCI_SUBVENDOR_ID_IBM,
13072 TG3PCI_SUBDEVICE_ID_IBM_5703SAX2, 0 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013073};
13074
Matt Carlson24daf2b2010-02-17 15:17:02 +000013075static struct subsys_tbl_ent * __devinit tg3_lookup_by_subsys(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013076{
13077 int i;
13078
13079 for (i = 0; i < ARRAY_SIZE(subsys_id_to_phy_id); i++) {
13080 if ((subsys_id_to_phy_id[i].subsys_vendor ==
13081 tp->pdev->subsystem_vendor) &&
13082 (subsys_id_to_phy_id[i].subsys_devid ==
13083 tp->pdev->subsystem_device))
13084 return &subsys_id_to_phy_id[i];
13085 }
13086 return NULL;
13087}
13088
Michael Chan7d0c41e2005-04-21 17:06:20 -070013089static void __devinit tg3_get_eeprom_hw_cfg(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013090{
Linus Torvalds1da177e2005-04-16 15:20:36 -070013091 u32 val;
David S. Millerf49639e2006-06-09 11:58:36 -070013092
Matt Carlson79eb6902010-02-17 15:17:03 +000013093 tp->phy_id = TG3_PHY_ID_INVALID;
Michael Chan7d0c41e2005-04-21 17:06:20 -070013094 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
13095
Gary Zambranoa85feb82007-05-05 11:52:19 -070013096 /* Assume an onboard device and WOL capable by default. */
Joe Perches63c3a662011-04-26 08:12:10 +000013097 tg3_flag_set(tp, EEPROM_WRITE_PROT);
13098 tg3_flag_set(tp, WOL_CAP);
David S. Miller72b845e2006-03-14 14:11:48 -080013099
Michael Chanb5d37722006-09-27 16:06:21 -070013100 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan9d26e212006-12-07 00:21:14 -080013101 if (!(tr32(PCIE_TRANSACTION_CFG) & PCIE_TRANS_CFG_LOM)) {
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 }
Matt Carlson0527ba32007-10-10 18:03:30 -070013105 val = tr32(VCPU_CFGSHDW);
13106 if (val & VCPU_CFGSHDW_ASPM_DBNC)
Joe Perches63c3a662011-04-26 08:12:10 +000013107 tg3_flag_set(tp, ASPM_WORKAROUND);
Matt Carlson0527ba32007-10-10 18:03:30 -070013108 if ((val & VCPU_CFGSHDW_WOL_ENABLE) &&
Rafael J. Wysocki6fdbab92011-04-28 11:02:15 +000013109 (val & VCPU_CFGSHDW_WOL_MAGPKT)) {
Joe Perches63c3a662011-04-26 08:12:10 +000013110 tg3_flag_set(tp, WOL_ENABLE);
Rafael J. Wysocki6fdbab92011-04-28 11:02:15 +000013111 device_set_wakeup_enable(&tp->pdev->dev, true);
13112 }
Matt Carlson05ac4cb2008-11-03 16:53:46 -080013113 goto done;
Michael Chanb5d37722006-09-27 16:06:21 -070013114 }
13115
Linus Torvalds1da177e2005-04-16 15:20:36 -070013116 tg3_read_mem(tp, NIC_SRAM_DATA_SIG, &val);
13117 if (val == NIC_SRAM_DATA_SIG_MAGIC) {
13118 u32 nic_cfg, led_cfg;
Matt Carlsona9daf362008-05-25 23:49:44 -070013119 u32 nic_phy_id, ver, cfg2 = 0, cfg4 = 0, eeprom_phy_id;
Michael Chan7d0c41e2005-04-21 17:06:20 -070013120 int eeprom_phy_serdes = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013121
13122 tg3_read_mem(tp, NIC_SRAM_DATA_CFG, &nic_cfg);
13123 tp->nic_sram_data_cfg = nic_cfg;
13124
13125 tg3_read_mem(tp, NIC_SRAM_DATA_VER, &ver);
13126 ver >>= NIC_SRAM_DATA_VER_SHIFT;
Matt Carlson6ff6f812011-05-19 12:12:54 +000013127 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
13128 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701 &&
13129 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5703 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070013130 (ver > 0) && (ver < 0x100))
13131 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_2, &cfg2);
13132
Matt Carlsona9daf362008-05-25 23:49:44 -070013133 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
13134 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_4, &cfg4);
13135
Linus Torvalds1da177e2005-04-16 15:20:36 -070013136 if ((nic_cfg & NIC_SRAM_DATA_CFG_PHY_TYPE_MASK) ==
13137 NIC_SRAM_DATA_CFG_PHY_TYPE_FIBER)
13138 eeprom_phy_serdes = 1;
13139
13140 tg3_read_mem(tp, NIC_SRAM_DATA_PHY_ID, &nic_phy_id);
13141 if (nic_phy_id != 0) {
13142 u32 id1 = nic_phy_id & NIC_SRAM_DATA_PHY_ID1_MASK;
13143 u32 id2 = nic_phy_id & NIC_SRAM_DATA_PHY_ID2_MASK;
13144
13145 eeprom_phy_id = (id1 >> 16) << 10;
13146 eeprom_phy_id |= (id2 & 0xfc00) << 16;
13147 eeprom_phy_id |= (id2 & 0x03ff) << 0;
13148 } else
13149 eeprom_phy_id = 0;
13150
Michael Chan7d0c41e2005-04-21 17:06:20 -070013151 tp->phy_id = eeprom_phy_id;
Michael Chan747e8f82005-07-25 12:33:22 -070013152 if (eeprom_phy_serdes) {
Joe Perches63c3a662011-04-26 08:12:10 +000013153 if (!tg3_flag(tp, 5705_PLUS))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013154 tp->phy_flags |= TG3_PHYFLG_PHY_SERDES;
Matt Carlsona50d0792010-06-05 17:24:37 +000013155 else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013156 tp->phy_flags |= TG3_PHYFLG_MII_SERDES;
Michael Chan747e8f82005-07-25 12:33:22 -070013157 }
Michael Chan7d0c41e2005-04-21 17:06:20 -070013158
Joe Perches63c3a662011-04-26 08:12:10 +000013159 if (tg3_flag(tp, 5750_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -070013160 led_cfg = cfg2 & (NIC_SRAM_DATA_CFG_LED_MODE_MASK |
13161 SHASTA_EXT_LED_MODE_MASK);
John W. Linvillecbf46852005-04-21 17:01:29 -070013162 else
Linus Torvalds1da177e2005-04-16 15:20:36 -070013163 led_cfg = nic_cfg & NIC_SRAM_DATA_CFG_LED_MODE_MASK;
13164
13165 switch (led_cfg) {
13166 default:
13167 case NIC_SRAM_DATA_CFG_LED_MODE_PHY_1:
13168 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
13169 break;
13170
13171 case NIC_SRAM_DATA_CFG_LED_MODE_PHY_2:
13172 tp->led_ctrl = LED_CTRL_MODE_PHY_2;
13173 break;
13174
13175 case NIC_SRAM_DATA_CFG_LED_MODE_MAC:
13176 tp->led_ctrl = LED_CTRL_MODE_MAC;
Michael Chan9ba27792005-06-06 15:16:20 -070013177
13178 /* Default to PHY_1_MODE if 0 (MAC_MODE) is
13179 * read on some older 5700/5701 bootcode.
13180 */
13181 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
13182 ASIC_REV_5700 ||
13183 GET_ASIC_REV(tp->pci_chip_rev_id) ==
13184 ASIC_REV_5701)
13185 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
13186
Linus Torvalds1da177e2005-04-16 15:20:36 -070013187 break;
13188
13189 case SHASTA_EXT_LED_SHARED:
13190 tp->led_ctrl = LED_CTRL_MODE_SHARED;
13191 if (tp->pci_chip_rev_id != CHIPREV_ID_5750_A0 &&
13192 tp->pci_chip_rev_id != CHIPREV_ID_5750_A1)
13193 tp->led_ctrl |= (LED_CTRL_MODE_PHY_1 |
13194 LED_CTRL_MODE_PHY_2);
13195 break;
13196
13197 case SHASTA_EXT_LED_MAC:
13198 tp->led_ctrl = LED_CTRL_MODE_SHASTA_MAC;
13199 break;
13200
13201 case SHASTA_EXT_LED_COMBO:
13202 tp->led_ctrl = LED_CTRL_MODE_COMBO;
13203 if (tp->pci_chip_rev_id != CHIPREV_ID_5750_A0)
13204 tp->led_ctrl |= (LED_CTRL_MODE_PHY_1 |
13205 LED_CTRL_MODE_PHY_2);
13206 break;
13207
Stephen Hemminger855e1112008-04-16 16:37:28 -070013208 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013209
13210 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
13211 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) &&
13212 tp->pdev->subsystem_vendor == PCI_VENDOR_ID_DELL)
13213 tp->led_ctrl = LED_CTRL_MODE_PHY_2;
13214
Matt Carlsonb2a5c192008-04-03 21:44:44 -070013215 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX)
13216 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
Matt Carlson5f608912007-11-12 21:17:07 -080013217
Michael Chan9d26e212006-12-07 00:21:14 -080013218 if (nic_cfg & NIC_SRAM_DATA_CFG_EEPROM_WP) {
Joe Perches63c3a662011-04-26 08:12:10 +000013219 tg3_flag_set(tp, EEPROM_WRITE_PROT);
Michael Chan9d26e212006-12-07 00:21:14 -080013220 if ((tp->pdev->subsystem_vendor ==
13221 PCI_VENDOR_ID_ARIMA) &&
13222 (tp->pdev->subsystem_device == 0x205a ||
13223 tp->pdev->subsystem_device == 0x2063))
Joe Perches63c3a662011-04-26 08:12:10 +000013224 tg3_flag_clear(tp, EEPROM_WRITE_PROT);
Michael Chan9d26e212006-12-07 00:21:14 -080013225 } else {
Joe Perches63c3a662011-04-26 08:12:10 +000013226 tg3_flag_clear(tp, EEPROM_WRITE_PROT);
13227 tg3_flag_set(tp, IS_NIC);
Michael Chan9d26e212006-12-07 00:21:14 -080013228 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013229
13230 if (nic_cfg & NIC_SRAM_DATA_CFG_ASF_ENABLE) {
Joe Perches63c3a662011-04-26 08:12:10 +000013231 tg3_flag_set(tp, ENABLE_ASF);
13232 if (tg3_flag(tp, 5750_PLUS))
13233 tg3_flag_set(tp, ASF_NEW_HANDSHAKE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013234 }
Matt Carlsonb2b98d42008-11-03 16:52:32 -080013235
13236 if ((nic_cfg & NIC_SRAM_DATA_CFG_APE_ENABLE) &&
Joe Perches63c3a662011-04-26 08:12:10 +000013237 tg3_flag(tp, 5750_PLUS))
13238 tg3_flag_set(tp, ENABLE_APE);
Matt Carlsonb2b98d42008-11-03 16:52:32 -080013239
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013240 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES &&
Gary Zambranoa85feb82007-05-05 11:52:19 -070013241 !(nic_cfg & NIC_SRAM_DATA_CFG_FIBER_WOL))
Joe Perches63c3a662011-04-26 08:12:10 +000013242 tg3_flag_clear(tp, WOL_CAP);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013243
Joe Perches63c3a662011-04-26 08:12:10 +000013244 if (tg3_flag(tp, WOL_CAP) &&
Rafael J. Wysocki6fdbab92011-04-28 11:02:15 +000013245 (nic_cfg & NIC_SRAM_DATA_CFG_WOL_ENABLE)) {
Joe Perches63c3a662011-04-26 08:12:10 +000013246 tg3_flag_set(tp, WOL_ENABLE);
Rafael J. Wysocki6fdbab92011-04-28 11:02:15 +000013247 device_set_wakeup_enable(&tp->pdev->dev, true);
13248 }
Matt Carlson0527ba32007-10-10 18:03:30 -070013249
Linus Torvalds1da177e2005-04-16 15:20:36 -070013250 if (cfg2 & (1 << 17))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013251 tp->phy_flags |= TG3_PHYFLG_CAPACITIVE_COUPLING;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013252
13253 /* serdes signal pre-emphasis in register 0x590 set by */
13254 /* bootcode if bit 18 is set */
13255 if (cfg2 & (1 << 18))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013256 tp->phy_flags |= TG3_PHYFLG_SERDES_PREEMPHASIS;
Matt Carlson8ed5d972007-05-07 00:25:49 -070013257
Joe Perches63c3a662011-04-26 08:12:10 +000013258 if ((tg3_flag(tp, 57765_PLUS) ||
13259 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
13260 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX)) &&
Matt Carlson6833c042008-11-21 17:18:59 -080013261 (cfg2 & NIC_SRAM_DATA_CFG_2_APD_EN))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013262 tp->phy_flags |= TG3_PHYFLG_ENABLE_APD;
Matt Carlson6833c042008-11-21 17:18:59 -080013263
Joe Perches63c3a662011-04-26 08:12:10 +000013264 if (tg3_flag(tp, PCI_EXPRESS) &&
Matt Carlson8c69b1e2010-08-02 11:26:00 +000013265 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
Joe Perches63c3a662011-04-26 08:12:10 +000013266 !tg3_flag(tp, 57765_PLUS)) {
Matt Carlson8ed5d972007-05-07 00:25:49 -070013267 u32 cfg3;
13268
13269 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_3, &cfg3);
13270 if (cfg3 & NIC_SRAM_ASPM_DEBOUNCE)
Joe Perches63c3a662011-04-26 08:12:10 +000013271 tg3_flag_set(tp, ASPM_WORKAROUND);
Matt Carlson8ed5d972007-05-07 00:25:49 -070013272 }
Matt Carlsona9daf362008-05-25 23:49:44 -070013273
Matt Carlson14417062010-02-17 15:16:59 +000013274 if (cfg4 & NIC_SRAM_RGMII_INBAND_DISABLE)
Joe Perches63c3a662011-04-26 08:12:10 +000013275 tg3_flag_set(tp, RGMII_INBAND_DISABLE);
Matt Carlsona9daf362008-05-25 23:49:44 -070013276 if (cfg4 & NIC_SRAM_RGMII_EXT_IBND_RX_EN)
Joe Perches63c3a662011-04-26 08:12:10 +000013277 tg3_flag_set(tp, RGMII_EXT_IBND_RX_EN);
Matt Carlsona9daf362008-05-25 23:49:44 -070013278 if (cfg4 & NIC_SRAM_RGMII_EXT_IBND_TX_EN)
Joe Perches63c3a662011-04-26 08:12:10 +000013279 tg3_flag_set(tp, RGMII_EXT_IBND_TX_EN);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013280 }
Matt Carlson05ac4cb2008-11-03 16:53:46 -080013281done:
Joe Perches63c3a662011-04-26 08:12:10 +000013282 if (tg3_flag(tp, WOL_CAP))
Rafael J. Wysocki43067ed2011-02-10 06:53:09 +000013283 device_set_wakeup_enable(&tp->pdev->dev,
Joe Perches63c3a662011-04-26 08:12:10 +000013284 tg3_flag(tp, WOL_ENABLE));
Rafael J. Wysocki43067ed2011-02-10 06:53:09 +000013285 else
13286 device_set_wakeup_capable(&tp->pdev->dev, false);
Michael Chan7d0c41e2005-04-21 17:06:20 -070013287}
13288
Matt Carlsonb2a5c192008-04-03 21:44:44 -070013289static int __devinit tg3_issue_otp_command(struct tg3 *tp, u32 cmd)
13290{
13291 int i;
13292 u32 val;
13293
13294 tw32(OTP_CTRL, cmd | OTP_CTRL_OTP_CMD_START);
13295 tw32(OTP_CTRL, cmd);
13296
13297 /* Wait for up to 1 ms for command to execute. */
13298 for (i = 0; i < 100; i++) {
13299 val = tr32(OTP_STATUS);
13300 if (val & OTP_STATUS_CMD_DONE)
13301 break;
13302 udelay(10);
13303 }
13304
13305 return (val & OTP_STATUS_CMD_DONE) ? 0 : -EBUSY;
13306}
13307
13308/* Read the gphy configuration from the OTP region of the chip. The gphy
13309 * configuration is a 32-bit value that straddles the alignment boundary.
13310 * We do two 32-bit reads and then shift and merge the results.
13311 */
13312static u32 __devinit tg3_read_otp_phycfg(struct tg3 *tp)
13313{
13314 u32 bhalf_otp, thalf_otp;
13315
13316 tw32(OTP_MODE, OTP_MODE_OTP_THRU_GRC);
13317
13318 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_INIT))
13319 return 0;
13320
13321 tw32(OTP_ADDRESS, OTP_ADDRESS_MAGIC1);
13322
13323 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_READ))
13324 return 0;
13325
13326 thalf_otp = tr32(OTP_READ_DATA);
13327
13328 tw32(OTP_ADDRESS, OTP_ADDRESS_MAGIC2);
13329
13330 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_READ))
13331 return 0;
13332
13333 bhalf_otp = tr32(OTP_READ_DATA);
13334
13335 return ((thalf_otp & 0x0000ffff) << 16) | (bhalf_otp >> 16);
13336}
13337
Matt Carlsone256f8a2011-03-09 16:58:24 +000013338static void __devinit tg3_phy_init_link_config(struct tg3 *tp)
13339{
Hiroaki SHIMODA202ff1c2011-11-22 04:05:41 +000013340 u32 adv = ADVERTISED_Autoneg;
Matt Carlsone256f8a2011-03-09 16:58:24 +000013341
13342 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
13343 adv |= ADVERTISED_1000baseT_Half |
13344 ADVERTISED_1000baseT_Full;
13345
13346 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
13347 adv |= ADVERTISED_100baseT_Half |
13348 ADVERTISED_100baseT_Full |
13349 ADVERTISED_10baseT_Half |
13350 ADVERTISED_10baseT_Full |
13351 ADVERTISED_TP;
13352 else
13353 adv |= ADVERTISED_FIBRE;
13354
13355 tp->link_config.advertising = adv;
13356 tp->link_config.speed = SPEED_INVALID;
13357 tp->link_config.duplex = DUPLEX_INVALID;
13358 tp->link_config.autoneg = AUTONEG_ENABLE;
13359 tp->link_config.active_speed = SPEED_INVALID;
13360 tp->link_config.active_duplex = DUPLEX_INVALID;
13361 tp->link_config.orig_speed = SPEED_INVALID;
13362 tp->link_config.orig_duplex = DUPLEX_INVALID;
13363 tp->link_config.orig_autoneg = AUTONEG_INVALID;
13364}
13365
Michael Chan7d0c41e2005-04-21 17:06:20 -070013366static int __devinit tg3_phy_probe(struct tg3 *tp)
13367{
13368 u32 hw_phy_id_1, hw_phy_id_2;
13369 u32 hw_phy_id, hw_phy_id_masked;
13370 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013371
Matt Carlsone256f8a2011-03-09 16:58:24 +000013372 /* flow control autonegotiation is default behavior */
Joe Perches63c3a662011-04-26 08:12:10 +000013373 tg3_flag_set(tp, PAUSE_AUTONEG);
Matt Carlsone256f8a2011-03-09 16:58:24 +000013374 tp->link_config.flowctrl = FLOW_CTRL_TX | FLOW_CTRL_RX;
13375
Joe Perches63c3a662011-04-26 08:12:10 +000013376 if (tg3_flag(tp, USE_PHYLIB))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070013377 return tg3_phy_init(tp);
13378
Linus Torvalds1da177e2005-04-16 15:20:36 -070013379 /* Reading the PHY ID register can conflict with ASF
Nick Andrew877d0312009-01-26 11:06:57 +010013380 * firmware access to the PHY hardware.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013381 */
13382 err = 0;
Joe Perches63c3a662011-04-26 08:12:10 +000013383 if (tg3_flag(tp, ENABLE_ASF) || tg3_flag(tp, ENABLE_APE)) {
Matt Carlson79eb6902010-02-17 15:17:03 +000013384 hw_phy_id = hw_phy_id_masked = TG3_PHY_ID_INVALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013385 } else {
13386 /* Now read the physical PHY_ID from the chip and verify
13387 * that it is sane. If it doesn't look good, we fall back
13388 * to either the hard-coded table based PHY_ID and failing
13389 * that the value found in the eeprom area.
13390 */
13391 err |= tg3_readphy(tp, MII_PHYSID1, &hw_phy_id_1);
13392 err |= tg3_readphy(tp, MII_PHYSID2, &hw_phy_id_2);
13393
13394 hw_phy_id = (hw_phy_id_1 & 0xffff) << 10;
13395 hw_phy_id |= (hw_phy_id_2 & 0xfc00) << 16;
13396 hw_phy_id |= (hw_phy_id_2 & 0x03ff) << 0;
13397
Matt Carlson79eb6902010-02-17 15:17:03 +000013398 hw_phy_id_masked = hw_phy_id & TG3_PHY_ID_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013399 }
13400
Matt Carlson79eb6902010-02-17 15:17:03 +000013401 if (!err && TG3_KNOWN_PHY_ID(hw_phy_id_masked)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070013402 tp->phy_id = hw_phy_id;
Matt Carlson79eb6902010-02-17 15:17:03 +000013403 if (hw_phy_id_masked == TG3_PHY_ID_BCM8002)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013404 tp->phy_flags |= TG3_PHYFLG_PHY_SERDES;
Michael Chanda6b2d02005-08-19 12:54:29 -070013405 else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013406 tp->phy_flags &= ~TG3_PHYFLG_PHY_SERDES;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013407 } else {
Matt Carlson79eb6902010-02-17 15:17:03 +000013408 if (tp->phy_id != TG3_PHY_ID_INVALID) {
Michael Chan7d0c41e2005-04-21 17:06:20 -070013409 /* Do nothing, phy ID already set up in
13410 * tg3_get_eeprom_hw_cfg().
13411 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070013412 } else {
13413 struct subsys_tbl_ent *p;
13414
13415 /* No eeprom signature? Try the hardcoded
13416 * subsys device table.
13417 */
Matt Carlson24daf2b2010-02-17 15:17:02 +000013418 p = tg3_lookup_by_subsys(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013419 if (!p)
13420 return -ENODEV;
13421
13422 tp->phy_id = p->phy_id;
13423 if (!tp->phy_id ||
Matt Carlson79eb6902010-02-17 15:17:03 +000013424 tp->phy_id == TG3_PHY_ID_BCM8002)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013425 tp->phy_flags |= TG3_PHYFLG_PHY_SERDES;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013426 }
13427 }
13428
Matt Carlsona6b68da2010-12-06 08:28:52 +000013429 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES) &&
Matt Carlson5baa5e92011-07-20 10:20:53 +000013430 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
13431 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720 ||
13432 (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718 &&
Matt Carlsona6b68da2010-12-06 08:28:52 +000013433 tp->pci_chip_rev_id != CHIPREV_ID_5717_A0) ||
13434 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765 &&
13435 tp->pci_chip_rev_id != CHIPREV_ID_57765_A0)))
Matt Carlson52b02d02010-10-14 10:37:41 +000013436 tp->phy_flags |= TG3_PHYFLG_EEE_CAP;
13437
Matt Carlsone256f8a2011-03-09 16:58:24 +000013438 tg3_phy_init_link_config(tp);
13439
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013440 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES) &&
Joe Perches63c3a662011-04-26 08:12:10 +000013441 !tg3_flag(tp, ENABLE_APE) &&
13442 !tg3_flag(tp, ENABLE_ASF)) {
Matt Carlsone2bf73e2011-12-08 14:40:15 +000013443 u32 bmsr, dummy;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013444
13445 tg3_readphy(tp, MII_BMSR, &bmsr);
13446 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
13447 (bmsr & BMSR_LSTATUS))
13448 goto skip_phy_reset;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040013449
Linus Torvalds1da177e2005-04-16 15:20:36 -070013450 err = tg3_phy_reset(tp);
13451 if (err)
13452 return err;
13453
Matt Carlson42b64a42011-05-19 12:12:49 +000013454 tg3_phy_set_wirespeed(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013455
Matt Carlsone2bf73e2011-12-08 14:40:15 +000013456 if (!tg3_phy_copper_an_config_ok(tp, &dummy)) {
Matt Carlson42b64a42011-05-19 12:12:49 +000013457 tg3_phy_autoneg_cfg(tp, tp->link_config.advertising,
13458 tp->link_config.flowctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013459
13460 tg3_writephy(tp, MII_BMCR,
13461 BMCR_ANENABLE | BMCR_ANRESTART);
13462 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013463 }
13464
13465skip_phy_reset:
Matt Carlson79eb6902010-02-17 15:17:03 +000013466 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070013467 err = tg3_init_5401phy_dsp(tp);
13468 if (err)
13469 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013470
Linus Torvalds1da177e2005-04-16 15:20:36 -070013471 err = tg3_init_5401phy_dsp(tp);
13472 }
13473
Linus Torvalds1da177e2005-04-16 15:20:36 -070013474 return err;
13475}
13476
Matt Carlson184b8902010-04-05 10:19:25 +000013477static void __devinit tg3_read_vpd(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013478{
Matt Carlsona4a8bb12010-09-15 09:00:00 +000013479 u8 *vpd_data;
Matt Carlson4181b2c2010-02-26 14:04:45 +000013480 unsigned int block_end, rosize, len;
Matt Carlson535a4902011-07-20 10:20:56 +000013481 u32 vpdlen;
Matt Carlson184b8902010-04-05 10:19:25 +000013482 int j, i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013483
Matt Carlson535a4902011-07-20 10:20:56 +000013484 vpd_data = (u8 *)tg3_vpd_readblock(tp, &vpdlen);
Matt Carlsona4a8bb12010-09-15 09:00:00 +000013485 if (!vpd_data)
13486 goto out_no_vpd;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013487
Matt Carlson535a4902011-07-20 10:20:56 +000013488 i = pci_vpd_find_tag(vpd_data, 0, vpdlen, PCI_VPD_LRDT_RO_DATA);
Matt Carlson4181b2c2010-02-26 14:04:45 +000013489 if (i < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013490 goto out_not_found;
Matt Carlson4181b2c2010-02-26 14:04:45 +000013491
13492 rosize = pci_vpd_lrdt_size(&vpd_data[i]);
13493 block_end = i + PCI_VPD_LRDT_TAG_SIZE + rosize;
13494 i += PCI_VPD_LRDT_TAG_SIZE;
13495
Matt Carlson535a4902011-07-20 10:20:56 +000013496 if (block_end > vpdlen)
Matt Carlson4181b2c2010-02-26 14:04:45 +000013497 goto out_not_found;
13498
Matt Carlson184b8902010-04-05 10:19:25 +000013499 j = pci_vpd_find_info_keyword(vpd_data, i, rosize,
13500 PCI_VPD_RO_KEYWORD_MFR_ID);
13501 if (j > 0) {
13502 len = pci_vpd_info_field_size(&vpd_data[j]);
13503
13504 j += PCI_VPD_INFO_FLD_HDR_SIZE;
13505 if (j + len > block_end || len != 4 ||
13506 memcmp(&vpd_data[j], "1028", 4))
13507 goto partno;
13508
13509 j = pci_vpd_find_info_keyword(vpd_data, i, rosize,
13510 PCI_VPD_RO_KEYWORD_VENDOR0);
13511 if (j < 0)
13512 goto partno;
13513
13514 len = pci_vpd_info_field_size(&vpd_data[j]);
13515
13516 j += PCI_VPD_INFO_FLD_HDR_SIZE;
13517 if (j + len > block_end)
13518 goto partno;
13519
13520 memcpy(tp->fw_ver, &vpd_data[j], len);
Matt Carlson535a4902011-07-20 10:20:56 +000013521 strncat(tp->fw_ver, " bc ", vpdlen - len - 1);
Matt Carlson184b8902010-04-05 10:19:25 +000013522 }
13523
13524partno:
Matt Carlson4181b2c2010-02-26 14:04:45 +000013525 i = pci_vpd_find_info_keyword(vpd_data, i, rosize,
13526 PCI_VPD_RO_KEYWORD_PARTNO);
13527 if (i < 0)
13528 goto out_not_found;
13529
13530 len = pci_vpd_info_field_size(&vpd_data[i]);
13531
13532 i += PCI_VPD_INFO_FLD_HDR_SIZE;
13533 if (len > TG3_BPN_SIZE ||
Matt Carlson535a4902011-07-20 10:20:56 +000013534 (len + i) > vpdlen)
Matt Carlson4181b2c2010-02-26 14:04:45 +000013535 goto out_not_found;
13536
13537 memcpy(tp->board_part_number, &vpd_data[i], len);
13538
Linus Torvalds1da177e2005-04-16 15:20:36 -070013539out_not_found:
Matt Carlsona4a8bb12010-09-15 09:00:00 +000013540 kfree(vpd_data);
Matt Carlson37a949c2010-09-30 10:34:33 +000013541 if (tp->board_part_number[0])
Matt Carlsona4a8bb12010-09-15 09:00:00 +000013542 return;
13543
13544out_no_vpd:
Matt Carlson37a949c2010-09-30 10:34:33 +000013545 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717) {
13546 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717)
13547 strcpy(tp->board_part_number, "BCM5717");
13548 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718)
13549 strcpy(tp->board_part_number, "BCM5718");
13550 else
13551 goto nomatch;
13552 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780) {
13553 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57780)
13554 strcpy(tp->board_part_number, "BCM57780");
13555 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57760)
13556 strcpy(tp->board_part_number, "BCM57760");
13557 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57790)
13558 strcpy(tp->board_part_number, "BCM57790");
13559 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57788)
13560 strcpy(tp->board_part_number, "BCM57788");
13561 else
13562 goto nomatch;
13563 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765) {
13564 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57761)
13565 strcpy(tp->board_part_number, "BCM57761");
13566 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57765)
13567 strcpy(tp->board_part_number, "BCM57765");
13568 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57781)
13569 strcpy(tp->board_part_number, "BCM57781");
13570 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57785)
13571 strcpy(tp->board_part_number, "BCM57785");
13572 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57791)
13573 strcpy(tp->board_part_number, "BCM57791");
13574 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57795)
13575 strcpy(tp->board_part_number, "BCM57795");
13576 else
13577 goto nomatch;
Matt Carlson55086ad2011-12-14 11:09:59 +000013578 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57766) {
13579 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57762)
13580 strcpy(tp->board_part_number, "BCM57762");
13581 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57766)
13582 strcpy(tp->board_part_number, "BCM57766");
13583 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57782)
13584 strcpy(tp->board_part_number, "BCM57782");
13585 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57786)
13586 strcpy(tp->board_part_number, "BCM57786");
13587 else
13588 goto nomatch;
Matt Carlson37a949c2010-09-30 10:34:33 +000013589 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chanb5d37722006-09-27 16:06:21 -070013590 strcpy(tp->board_part_number, "BCM95906");
Matt Carlson37a949c2010-09-30 10:34:33 +000013591 } else {
13592nomatch:
Michael Chanb5d37722006-09-27 16:06:21 -070013593 strcpy(tp->board_part_number, "none");
Matt Carlson37a949c2010-09-30 10:34:33 +000013594 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013595}
13596
Matt Carlson9c8a6202007-10-21 16:16:08 -070013597static int __devinit tg3_fw_img_is_valid(struct tg3 *tp, u32 offset)
13598{
13599 u32 val;
13600
Matt Carlsone4f34112009-02-25 14:25:00 +000013601 if (tg3_nvram_read(tp, offset, &val) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070013602 (val & 0xfc000000) != 0x0c000000 ||
Matt Carlsone4f34112009-02-25 14:25:00 +000013603 tg3_nvram_read(tp, offset + 4, &val) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070013604 val != 0)
13605 return 0;
13606
13607 return 1;
13608}
13609
Matt Carlsonacd9c112009-02-25 14:26:33 +000013610static void __devinit tg3_read_bc_ver(struct tg3 *tp)
13611{
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013612 u32 val, offset, start, ver_offset;
Matt Carlson75f99362010-04-05 10:19:24 +000013613 int i, dst_off;
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013614 bool newver = false;
Matt Carlsonacd9c112009-02-25 14:26:33 +000013615
13616 if (tg3_nvram_read(tp, 0xc, &offset) ||
13617 tg3_nvram_read(tp, 0x4, &start))
13618 return;
13619
13620 offset = tg3_nvram_logical_addr(tp, offset);
13621
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013622 if (tg3_nvram_read(tp, offset, &val))
Matt Carlsonacd9c112009-02-25 14:26:33 +000013623 return;
13624
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013625 if ((val & 0xfc000000) == 0x0c000000) {
13626 if (tg3_nvram_read(tp, offset + 4, &val))
Matt Carlsonacd9c112009-02-25 14:26:33 +000013627 return;
13628
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013629 if (val == 0)
13630 newver = true;
13631 }
13632
Matt Carlson75f99362010-04-05 10:19:24 +000013633 dst_off = strlen(tp->fw_ver);
13634
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013635 if (newver) {
Matt Carlson75f99362010-04-05 10:19:24 +000013636 if (TG3_VER_SIZE - dst_off < 16 ||
13637 tg3_nvram_read(tp, offset + 8, &ver_offset))
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013638 return;
13639
13640 offset = offset + ver_offset - start;
13641 for (i = 0; i < 16; i += 4) {
13642 __be32 v;
13643 if (tg3_nvram_read_be32(tp, offset + i, &v))
13644 return;
13645
Matt Carlson75f99362010-04-05 10:19:24 +000013646 memcpy(tp->fw_ver + dst_off + i, &v, sizeof(v));
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013647 }
13648 } else {
13649 u32 major, minor;
13650
13651 if (tg3_nvram_read(tp, TG3_NVM_PTREV_BCVER, &ver_offset))
13652 return;
13653
13654 major = (ver_offset & TG3_NVM_BCVER_MAJMSK) >>
13655 TG3_NVM_BCVER_MAJSFT;
13656 minor = ver_offset & TG3_NVM_BCVER_MINMSK;
Matt Carlson75f99362010-04-05 10:19:24 +000013657 snprintf(&tp->fw_ver[dst_off], TG3_VER_SIZE - dst_off,
13658 "v%d.%02d", major, minor);
Matt Carlsonacd9c112009-02-25 14:26:33 +000013659 }
13660}
13661
Matt Carlsona6f6cb12009-02-25 14:27:43 +000013662static void __devinit tg3_read_hwsb_ver(struct tg3 *tp)
13663{
13664 u32 val, major, minor;
13665
13666 /* Use native endian representation */
13667 if (tg3_nvram_read(tp, TG3_NVM_HWSB_CFG1, &val))
13668 return;
13669
13670 major = (val & TG3_NVM_HWSB_CFG1_MAJMSK) >>
13671 TG3_NVM_HWSB_CFG1_MAJSFT;
13672 minor = (val & TG3_NVM_HWSB_CFG1_MINMSK) >>
13673 TG3_NVM_HWSB_CFG1_MINSFT;
13674
13675 snprintf(&tp->fw_ver[0], 32, "sb v%d.%02d", major, minor);
13676}
13677
Matt Carlsondfe00d72008-11-21 17:19:41 -080013678static void __devinit tg3_read_sb_ver(struct tg3 *tp, u32 val)
13679{
13680 u32 offset, major, minor, build;
13681
Matt Carlson75f99362010-04-05 10:19:24 +000013682 strncat(tp->fw_ver, "sb", TG3_VER_SIZE - strlen(tp->fw_ver) - 1);
Matt Carlsondfe00d72008-11-21 17:19:41 -080013683
13684 if ((val & TG3_EEPROM_SB_FORMAT_MASK) != TG3_EEPROM_SB_FORMAT_1)
13685 return;
13686
13687 switch (val & TG3_EEPROM_SB_REVISION_MASK) {
13688 case TG3_EEPROM_SB_REVISION_0:
13689 offset = TG3_EEPROM_SB_F1R0_EDH_OFF;
13690 break;
13691 case TG3_EEPROM_SB_REVISION_2:
13692 offset = TG3_EEPROM_SB_F1R2_EDH_OFF;
13693 break;
13694 case TG3_EEPROM_SB_REVISION_3:
13695 offset = TG3_EEPROM_SB_F1R3_EDH_OFF;
13696 break;
Matt Carlsona4153d42010-02-17 15:16:56 +000013697 case TG3_EEPROM_SB_REVISION_4:
13698 offset = TG3_EEPROM_SB_F1R4_EDH_OFF;
13699 break;
13700 case TG3_EEPROM_SB_REVISION_5:
13701 offset = TG3_EEPROM_SB_F1R5_EDH_OFF;
13702 break;
Matt Carlsonbba226a2010-10-14 10:37:38 +000013703 case TG3_EEPROM_SB_REVISION_6:
13704 offset = TG3_EEPROM_SB_F1R6_EDH_OFF;
13705 break;
Matt Carlsondfe00d72008-11-21 17:19:41 -080013706 default:
13707 return;
13708 }
13709
Matt Carlsone4f34112009-02-25 14:25:00 +000013710 if (tg3_nvram_read(tp, offset, &val))
Matt Carlsondfe00d72008-11-21 17:19:41 -080013711 return;
13712
13713 build = (val & TG3_EEPROM_SB_EDH_BLD_MASK) >>
13714 TG3_EEPROM_SB_EDH_BLD_SHFT;
13715 major = (val & TG3_EEPROM_SB_EDH_MAJ_MASK) >>
13716 TG3_EEPROM_SB_EDH_MAJ_SHFT;
13717 minor = val & TG3_EEPROM_SB_EDH_MIN_MASK;
13718
13719 if (minor > 99 || build > 26)
13720 return;
13721
Matt Carlson75f99362010-04-05 10:19:24 +000013722 offset = strlen(tp->fw_ver);
13723 snprintf(&tp->fw_ver[offset], TG3_VER_SIZE - offset,
13724 " v%d.%02d", major, minor);
Matt Carlsondfe00d72008-11-21 17:19:41 -080013725
13726 if (build > 0) {
Matt Carlson75f99362010-04-05 10:19:24 +000013727 offset = strlen(tp->fw_ver);
13728 if (offset < TG3_VER_SIZE - 1)
13729 tp->fw_ver[offset] = 'a' + build - 1;
Matt Carlsondfe00d72008-11-21 17:19:41 -080013730 }
13731}
13732
Matt Carlsonacd9c112009-02-25 14:26:33 +000013733static void __devinit tg3_read_mgmtfw_ver(struct tg3 *tp)
Michael Chanc4e65752006-03-20 22:29:32 -080013734{
13735 u32 val, offset, start;
Matt Carlsonacd9c112009-02-25 14:26:33 +000013736 int i, vlen;
Matt Carlson9c8a6202007-10-21 16:16:08 -070013737
13738 for (offset = TG3_NVM_DIR_START;
13739 offset < TG3_NVM_DIR_END;
13740 offset += TG3_NVM_DIRENT_SIZE) {
Matt Carlsone4f34112009-02-25 14:25:00 +000013741 if (tg3_nvram_read(tp, offset, &val))
Matt Carlson9c8a6202007-10-21 16:16:08 -070013742 return;
13743
13744 if ((val >> TG3_NVM_DIRTYPE_SHIFT) == TG3_NVM_DIRTYPE_ASFINI)
13745 break;
13746 }
13747
13748 if (offset == TG3_NVM_DIR_END)
13749 return;
13750
Joe Perches63c3a662011-04-26 08:12:10 +000013751 if (!tg3_flag(tp, 5705_PLUS))
Matt Carlson9c8a6202007-10-21 16:16:08 -070013752 start = 0x08000000;
Matt Carlsone4f34112009-02-25 14:25:00 +000013753 else if (tg3_nvram_read(tp, offset - 4, &start))
Matt Carlson9c8a6202007-10-21 16:16:08 -070013754 return;
13755
Matt Carlsone4f34112009-02-25 14:25:00 +000013756 if (tg3_nvram_read(tp, offset + 4, &offset) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070013757 !tg3_fw_img_is_valid(tp, offset) ||
Matt Carlsone4f34112009-02-25 14:25:00 +000013758 tg3_nvram_read(tp, offset + 8, &val))
Matt Carlson9c8a6202007-10-21 16:16:08 -070013759 return;
13760
13761 offset += val - start;
13762
Matt Carlsonacd9c112009-02-25 14:26:33 +000013763 vlen = strlen(tp->fw_ver);
Matt Carlson9c8a6202007-10-21 16:16:08 -070013764
Matt Carlsonacd9c112009-02-25 14:26:33 +000013765 tp->fw_ver[vlen++] = ',';
13766 tp->fw_ver[vlen++] = ' ';
Matt Carlson9c8a6202007-10-21 16:16:08 -070013767
13768 for (i = 0; i < 4; i++) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000013769 __be32 v;
13770 if (tg3_nvram_read_be32(tp, offset, &v))
Matt Carlson9c8a6202007-10-21 16:16:08 -070013771 return;
13772
Al Virob9fc7dc2007-12-17 22:59:57 -080013773 offset += sizeof(v);
Matt Carlson9c8a6202007-10-21 16:16:08 -070013774
Matt Carlsonacd9c112009-02-25 14:26:33 +000013775 if (vlen > TG3_VER_SIZE - sizeof(v)) {
13776 memcpy(&tp->fw_ver[vlen], &v, TG3_VER_SIZE - vlen);
Matt Carlson9c8a6202007-10-21 16:16:08 -070013777 break;
13778 }
13779
Matt Carlsonacd9c112009-02-25 14:26:33 +000013780 memcpy(&tp->fw_ver[vlen], &v, sizeof(v));
13781 vlen += sizeof(v);
Matt Carlson9c8a6202007-10-21 16:16:08 -070013782 }
Matt Carlsonacd9c112009-02-25 14:26:33 +000013783}
13784
Matt Carlson7fd76442009-02-25 14:27:20 +000013785static void __devinit tg3_read_dash_ver(struct tg3 *tp)
13786{
13787 int vlen;
13788 u32 apedata;
Matt Carlsonecc79642010-08-02 11:26:01 +000013789 char *fwtype;
Matt Carlson7fd76442009-02-25 14:27:20 +000013790
Joe Perches63c3a662011-04-26 08:12:10 +000013791 if (!tg3_flag(tp, ENABLE_APE) || !tg3_flag(tp, ENABLE_ASF))
Matt Carlson7fd76442009-02-25 14:27:20 +000013792 return;
13793
13794 apedata = tg3_ape_read32(tp, TG3_APE_SEG_SIG);
13795 if (apedata != APE_SEG_SIG_MAGIC)
13796 return;
13797
13798 apedata = tg3_ape_read32(tp, TG3_APE_FW_STATUS);
13799 if (!(apedata & APE_FW_STATUS_READY))
13800 return;
13801
13802 apedata = tg3_ape_read32(tp, TG3_APE_FW_VERSION);
13803
Matt Carlsondc6d0742010-09-15 08:59:55 +000013804 if (tg3_ape_read32(tp, TG3_APE_FW_FEATURES) & TG3_APE_FW_FEATURE_NCSI) {
Joe Perches63c3a662011-04-26 08:12:10 +000013805 tg3_flag_set(tp, APE_HAS_NCSI);
Matt Carlsonecc79642010-08-02 11:26:01 +000013806 fwtype = "NCSI";
Matt Carlsondc6d0742010-09-15 08:59:55 +000013807 } else {
Matt Carlsonecc79642010-08-02 11:26:01 +000013808 fwtype = "DASH";
Matt Carlsondc6d0742010-09-15 08:59:55 +000013809 }
Matt Carlsonecc79642010-08-02 11:26:01 +000013810
Matt Carlson7fd76442009-02-25 14:27:20 +000013811 vlen = strlen(tp->fw_ver);
13812
Matt Carlsonecc79642010-08-02 11:26:01 +000013813 snprintf(&tp->fw_ver[vlen], TG3_VER_SIZE - vlen, " %s v%d.%d.%d.%d",
13814 fwtype,
Matt Carlson7fd76442009-02-25 14:27:20 +000013815 (apedata & APE_FW_VERSION_MAJMSK) >> APE_FW_VERSION_MAJSFT,
13816 (apedata & APE_FW_VERSION_MINMSK) >> APE_FW_VERSION_MINSFT,
13817 (apedata & APE_FW_VERSION_REVMSK) >> APE_FW_VERSION_REVSFT,
13818 (apedata & APE_FW_VERSION_BLDMSK));
13819}
13820
Matt Carlsonacd9c112009-02-25 14:26:33 +000013821static void __devinit tg3_read_fw_ver(struct tg3 *tp)
13822{
13823 u32 val;
Matt Carlson75f99362010-04-05 10:19:24 +000013824 bool vpd_vers = false;
13825
13826 if (tp->fw_ver[0] != 0)
13827 vpd_vers = true;
Matt Carlsonacd9c112009-02-25 14:26:33 +000013828
Joe Perches63c3a662011-04-26 08:12:10 +000013829 if (tg3_flag(tp, NO_NVRAM)) {
Matt Carlson75f99362010-04-05 10:19:24 +000013830 strcat(tp->fw_ver, "sb");
Matt Carlsondf259d82009-04-20 06:57:14 +000013831 return;
13832 }
13833
Matt Carlsonacd9c112009-02-25 14:26:33 +000013834 if (tg3_nvram_read(tp, 0, &val))
13835 return;
13836
13837 if (val == TG3_EEPROM_MAGIC)
13838 tg3_read_bc_ver(tp);
13839 else if ((val & TG3_EEPROM_MAGIC_FW_MSK) == TG3_EEPROM_MAGIC_FW)
13840 tg3_read_sb_ver(tp, val);
Matt Carlsona6f6cb12009-02-25 14:27:43 +000013841 else if ((val & TG3_EEPROM_MAGIC_HW_MSK) == TG3_EEPROM_MAGIC_HW)
13842 tg3_read_hwsb_ver(tp);
Matt Carlsonacd9c112009-02-25 14:26:33 +000013843 else
13844 return;
13845
Matt Carlsonc9cab242011-07-13 09:27:27 +000013846 if (vpd_vers)
Matt Carlson75f99362010-04-05 10:19:24 +000013847 goto done;
Matt Carlsonacd9c112009-02-25 14:26:33 +000013848
Matt Carlsonc9cab242011-07-13 09:27:27 +000013849 if (tg3_flag(tp, ENABLE_APE)) {
13850 if (tg3_flag(tp, ENABLE_ASF))
13851 tg3_read_dash_ver(tp);
13852 } else if (tg3_flag(tp, ENABLE_ASF)) {
13853 tg3_read_mgmtfw_ver(tp);
13854 }
Matt Carlson9c8a6202007-10-21 16:16:08 -070013855
Matt Carlson75f99362010-04-05 10:19:24 +000013856done:
Matt Carlson9c8a6202007-10-21 16:16:08 -070013857 tp->fw_ver[TG3_VER_SIZE - 1] = 0;
Michael Chanc4e65752006-03-20 22:29:32 -080013858}
13859
Matt Carlson7cb32cf2010-09-30 10:34:36 +000013860static inline u32 tg3_rx_ret_ring_size(struct tg3 *tp)
13861{
Joe Perches63c3a662011-04-26 08:12:10 +000013862 if (tg3_flag(tp, LRG_PROD_RING_CAP))
Matt Carlsonde9f5232011-04-05 14:22:43 +000013863 return TG3_RX_RET_MAX_SIZE_5717;
Joe Perches63c3a662011-04-26 08:12:10 +000013864 else if (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS))
Matt Carlsonde9f5232011-04-05 14:22:43 +000013865 return TG3_RX_RET_MAX_SIZE_5700;
Matt Carlson7cb32cf2010-09-30 10:34:36 +000013866 else
Matt Carlsonde9f5232011-04-05 14:22:43 +000013867 return TG3_RX_RET_MAX_SIZE_5705;
Matt Carlson7cb32cf2010-09-30 10:34:36 +000013868}
13869
Matt Carlson41434702011-03-09 16:58:22 +000013870static DEFINE_PCI_DEVICE_TABLE(tg3_write_reorder_chipsets) = {
Joe Perches895950c2010-12-21 02:16:08 -080013871 { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_FE_GATE_700C) },
13872 { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_8131_BRIDGE) },
13873 { PCI_DEVICE(PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_8385_0) },
13874 { },
13875};
13876
Matt Carlson16c7fa72012-02-13 10:20:10 +000013877static struct pci_dev * __devinit tg3_find_peer(struct tg3 *tp)
13878{
13879 struct pci_dev *peer;
13880 unsigned int func, devnr = tp->pdev->devfn & ~7;
13881
13882 for (func = 0; func < 8; func++) {
13883 peer = pci_get_slot(tp->pdev->bus, devnr | func);
13884 if (peer && peer != tp->pdev)
13885 break;
13886 pci_dev_put(peer);
13887 }
13888 /* 5704 can be configured in single-port mode, set peer to
13889 * tp->pdev in that case.
13890 */
13891 if (!peer) {
13892 peer = tp->pdev;
13893 return peer;
13894 }
13895
13896 /*
13897 * We don't need to keep the refcount elevated; there's no way
13898 * to remove one half of this device without removing the other
13899 */
13900 pci_dev_put(peer);
13901
13902 return peer;
13903}
13904
Linus Torvalds1da177e2005-04-16 15:20:36 -070013905static int __devinit tg3_get_invariants(struct tg3 *tp)
13906{
Linus Torvalds1da177e2005-04-16 15:20:36 -070013907 u32 misc_ctrl_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013908 u32 pci_state_reg, grc_misc_cfg;
13909 u32 val;
13910 u16 pci_cmd;
Matt Carlson5e7dfd02008-11-21 17:18:16 -080013911 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013912
Linus Torvalds1da177e2005-04-16 15:20:36 -070013913 /* Force memory write invalidate off. If we leave it on,
13914 * then on 5700_BX chips we have to enable a workaround.
13915 * The workaround is to set the TG3PCI_DMA_RW_CTRL boundary
13916 * to match the cacheline size. The Broadcom driver have this
13917 * workaround but turns MWI off all the times so never uses
13918 * it. This seems to suggest that the workaround is insufficient.
13919 */
13920 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
13921 pci_cmd &= ~PCI_COMMAND_INVALIDATE;
13922 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
13923
Matt Carlson16821282011-07-13 09:27:28 +000013924 /* Important! -- Make sure register accesses are byteswapped
13925 * correctly. Also, for those chips that require it, make
13926 * sure that indirect register accesses are enabled before
13927 * the first operation.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013928 */
13929 pci_read_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
13930 &misc_ctrl_reg);
Matt Carlson16821282011-07-13 09:27:28 +000013931 tp->misc_host_ctrl |= (misc_ctrl_reg &
13932 MISC_HOST_CTRL_CHIPREV);
13933 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
13934 tp->misc_host_ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013935
13936 tp->pci_chip_rev_id = (misc_ctrl_reg >>
13937 MISC_HOST_CTRL_CHIPREV_SHIFT);
Matt Carlson795d01c2007-10-07 23:28:17 -070013938 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_USE_PROD_ID_REG) {
13939 u32 prod_id_asic_rev;
13940
Matt Carlson5001e2f2009-11-13 13:03:51 +000013941 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717 ||
13942 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718 ||
Matt Carlsond78b59f2011-04-05 14:22:46 +000013943 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5719 ||
13944 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5720)
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013945 pci_read_config_dword(tp->pdev,
13946 TG3PCI_GEN2_PRODID_ASICREV,
13947 &prod_id_asic_rev);
Matt Carlsonb703df62009-12-03 08:36:21 +000013948 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57781 ||
13949 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57785 ||
13950 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57761 ||
13951 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57765 ||
13952 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57791 ||
Matt Carlson55086ad2011-12-14 11:09:59 +000013953 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57795 ||
13954 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57762 ||
13955 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57766 ||
13956 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57782 ||
13957 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57786)
Matt Carlsonb703df62009-12-03 08:36:21 +000013958 pci_read_config_dword(tp->pdev,
13959 TG3PCI_GEN15_PRODID_ASICREV,
13960 &prod_id_asic_rev);
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013961 else
13962 pci_read_config_dword(tp->pdev, TG3PCI_PRODID_ASICREV,
13963 &prod_id_asic_rev);
13964
Matt Carlson321d32a2008-11-21 17:22:19 -080013965 tp->pci_chip_rev_id = prod_id_asic_rev;
Matt Carlson795d01c2007-10-07 23:28:17 -070013966 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013967
Michael Chanff645be2005-04-21 17:09:53 -070013968 /* Wrong chip ID in 5752 A0. This code can be removed later
13969 * as A0 is not in production.
13970 */
13971 if (tp->pci_chip_rev_id == CHIPREV_ID_5752_A0_HW)
13972 tp->pci_chip_rev_id = CHIPREV_ID_5752_A0;
13973
Michael Chan68929142005-08-09 20:17:14 -070013974 /* If we have 5702/03 A1 or A2 on certain ICH chipsets,
13975 * we need to disable memory and use config. cycles
13976 * only to access all registers. The 5702/03 chips
13977 * can mistakenly decode the special cycles from the
13978 * ICH chipsets as memory write cycles, causing corruption
13979 * of register and memory space. Only certain ICH bridges
13980 * will drive special cycles with non-zero data during the
13981 * address phase which can fall within the 5703's address
13982 * range. This is not an ICH bug as the PCI spec allows
13983 * non-zero address during special cycles. However, only
13984 * these ICH bridges are known to drive non-zero addresses
13985 * during special cycles.
13986 *
13987 * Since special cycles do not cross PCI bridges, we only
13988 * enable this workaround if the 5703 is on the secondary
13989 * bus of these ICH bridges.
13990 */
13991 if ((tp->pci_chip_rev_id == CHIPREV_ID_5703_A1) ||
13992 (tp->pci_chip_rev_id == CHIPREV_ID_5703_A2)) {
13993 static struct tg3_dev_id {
13994 u32 vendor;
13995 u32 device;
13996 u32 rev;
13997 } ich_chipsets[] = {
13998 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AA_8,
13999 PCI_ANY_ID },
14000 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AB_8,
14001 PCI_ANY_ID },
14002 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_11,
14003 0xa },
14004 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_6,
14005 PCI_ANY_ID },
14006 { },
14007 };
14008 struct tg3_dev_id *pci_id = &ich_chipsets[0];
14009 struct pci_dev *bridge = NULL;
14010
14011 while (pci_id->vendor != 0) {
14012 bridge = pci_get_device(pci_id->vendor, pci_id->device,
14013 bridge);
14014 if (!bridge) {
14015 pci_id++;
14016 continue;
14017 }
14018 if (pci_id->rev != PCI_ANY_ID) {
Auke Kok44c10132007-06-08 15:46:36 -070014019 if (bridge->revision > pci_id->rev)
Michael Chan68929142005-08-09 20:17:14 -070014020 continue;
14021 }
14022 if (bridge->subordinate &&
14023 (bridge->subordinate->number ==
14024 tp->pdev->bus->number)) {
Joe Perches63c3a662011-04-26 08:12:10 +000014025 tg3_flag_set(tp, ICH_WORKAROUND);
Michael Chan68929142005-08-09 20:17:14 -070014026 pci_dev_put(bridge);
14027 break;
14028 }
14029 }
14030 }
14031
Matt Carlson6ff6f812011-05-19 12:12:54 +000014032 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
Matt Carlson41588ba2008-04-19 18:12:33 -070014033 static struct tg3_dev_id {
14034 u32 vendor;
14035 u32 device;
14036 } bridge_chipsets[] = {
14037 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PXH_0 },
14038 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PXH_1 },
14039 { },
14040 };
14041 struct tg3_dev_id *pci_id = &bridge_chipsets[0];
14042 struct pci_dev *bridge = NULL;
14043
14044 while (pci_id->vendor != 0) {
14045 bridge = pci_get_device(pci_id->vendor,
14046 pci_id->device,
14047 bridge);
14048 if (!bridge) {
14049 pci_id++;
14050 continue;
14051 }
14052 if (bridge->subordinate &&
14053 (bridge->subordinate->number <=
14054 tp->pdev->bus->number) &&
14055 (bridge->subordinate->subordinate >=
14056 tp->pdev->bus->number)) {
Joe Perches63c3a662011-04-26 08:12:10 +000014057 tg3_flag_set(tp, 5701_DMA_BUG);
Matt Carlson41588ba2008-04-19 18:12:33 -070014058 pci_dev_put(bridge);
14059 break;
14060 }
14061 }
14062 }
14063
Michael Chan4a29cc22006-03-19 13:21:12 -080014064 /* The EPB bridge inside 5714, 5715, and 5780 cannot support
14065 * DMA addresses > 40-bit. This bridge may have other additional
14066 * 57xx devices behind it in some 4-port NIC designs for example.
14067 * Any tg3 device found behind the bridge will also need the 40-bit
14068 * DMA workaround.
14069 */
Michael Chana4e2b342005-10-26 15:46:52 -070014070 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780 ||
14071 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
Joe Perches63c3a662011-04-26 08:12:10 +000014072 tg3_flag_set(tp, 5780_CLASS);
14073 tg3_flag_set(tp, 40BIT_DMA_BUG);
Michael Chan4cf78e42005-07-25 12:29:19 -070014074 tp->msi_cap = pci_find_capability(tp->pdev, PCI_CAP_ID_MSI);
Matt Carlson859a588792010-04-05 10:19:28 +000014075 } else {
Michael Chan4a29cc22006-03-19 13:21:12 -080014076 struct pci_dev *bridge = NULL;
14077
14078 do {
14079 bridge = pci_get_device(PCI_VENDOR_ID_SERVERWORKS,
14080 PCI_DEVICE_ID_SERVERWORKS_EPB,
14081 bridge);
14082 if (bridge && bridge->subordinate &&
14083 (bridge->subordinate->number <=
14084 tp->pdev->bus->number) &&
14085 (bridge->subordinate->subordinate >=
14086 tp->pdev->bus->number)) {
Joe Perches63c3a662011-04-26 08:12:10 +000014087 tg3_flag_set(tp, 40BIT_DMA_BUG);
Michael Chan4a29cc22006-03-19 13:21:12 -080014088 pci_dev_put(bridge);
14089 break;
14090 }
14091 } while (bridge);
14092 }
Michael Chan4cf78e42005-07-25 12:29:19 -070014093
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000014094 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
Matt Carlson3a1e19d2011-07-13 09:27:32 +000014095 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714)
Michael Chan7544b092007-05-05 13:08:32 -070014096 tp->pdev_peer = tg3_find_peer(tp);
14097
Matt Carlsonc885e822010-08-02 11:25:57 +000014098 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
Matt Carlsond78b59f2011-04-05 14:22:46 +000014099 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
14100 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
Joe Perches63c3a662011-04-26 08:12:10 +000014101 tg3_flag_set(tp, 5717_PLUS);
Matt Carlson0a58d662011-04-05 14:22:45 +000014102
14103 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765 ||
Matt Carlson55086ad2011-12-14 11:09:59 +000014104 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57766)
14105 tg3_flag_set(tp, 57765_CLASS);
14106
14107 if (tg3_flag(tp, 57765_CLASS) || tg3_flag(tp, 5717_PLUS))
Joe Perches63c3a662011-04-26 08:12:10 +000014108 tg3_flag_set(tp, 57765_PLUS);
Matt Carlsonc885e822010-08-02 11:25:57 +000014109
Matt Carlson321d32a2008-11-21 17:22:19 -080014110 /* Intentionally exclude ASIC_REV_5906 */
14111 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
Michael Chand9ab5ad2006-03-20 22:27:35 -080014112 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787 ||
Matt Carlsond30cdd22007-10-07 23:28:35 -070014113 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson9936bcf2007-10-10 18:03:07 -070014114 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson57e69832008-05-25 23:48:31 -070014115 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000014116 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
Joe Perches63c3a662011-04-26 08:12:10 +000014117 tg3_flag(tp, 57765_PLUS))
14118 tg3_flag_set(tp, 5755_PLUS);
Matt Carlson321d32a2008-11-21 17:22:19 -080014119
14120 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 ||
14121 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752 ||
Michael Chanb5d37722006-09-27 16:06:21 -070014122 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906 ||
Joe Perches63c3a662011-04-26 08:12:10 +000014123 tg3_flag(tp, 5755_PLUS) ||
14124 tg3_flag(tp, 5780_CLASS))
14125 tg3_flag_set(tp, 5750_PLUS);
John W. Linville6708e5c2005-04-21 17:00:52 -070014126
Matt Carlson6ff6f812011-05-19 12:12:54 +000014127 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 ||
Joe Perches63c3a662011-04-26 08:12:10 +000014128 tg3_flag(tp, 5750_PLUS))
14129 tg3_flag_set(tp, 5705_PLUS);
John W. Linville1b440c562005-04-21 17:03:18 -070014130
Matt Carlson507399f2009-11-13 13:03:37 +000014131 /* Determine TSO capabilities */
Matt Carlsona0512942011-07-27 14:20:54 +000014132 if (tp->pci_chip_rev_id == CHIPREV_ID_5719_A0)
Matt Carlson4d163b72011-01-25 15:58:48 +000014133 ; /* Do nothing. HW bug. */
Joe Perches63c3a662011-04-26 08:12:10 +000014134 else if (tg3_flag(tp, 57765_PLUS))
14135 tg3_flag_set(tp, HW_TSO_3);
14136 else if (tg3_flag(tp, 5755_PLUS) ||
Matt Carlsone849cdc2009-11-13 13:03:38 +000014137 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
Joe Perches63c3a662011-04-26 08:12:10 +000014138 tg3_flag_set(tp, HW_TSO_2);
14139 else if (tg3_flag(tp, 5750_PLUS)) {
14140 tg3_flag_set(tp, HW_TSO_1);
14141 tg3_flag_set(tp, TSO_BUG);
Matt Carlson507399f2009-11-13 13:03:37 +000014142 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 &&
14143 tp->pci_chip_rev_id >= CHIPREV_ID_5750_C2)
Joe Perches63c3a662011-04-26 08:12:10 +000014144 tg3_flag_clear(tp, TSO_BUG);
Matt Carlson507399f2009-11-13 13:03:37 +000014145 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
14146 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701 &&
14147 tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) {
Joe Perches63c3a662011-04-26 08:12:10 +000014148 tg3_flag_set(tp, TSO_BUG);
Matt Carlson507399f2009-11-13 13:03:37 +000014149 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705)
14150 tp->fw_needed = FIRMWARE_TG3TSO5;
14151 else
14152 tp->fw_needed = FIRMWARE_TG3TSO;
14153 }
14154
Matt Carlsondabc5c62011-05-19 12:12:52 +000014155 /* Selectively allow TSO based on operating conditions */
Matt Carlson6ff6f812011-05-19 12:12:54 +000014156 if (tg3_flag(tp, HW_TSO_1) ||
14157 tg3_flag(tp, HW_TSO_2) ||
14158 tg3_flag(tp, HW_TSO_3) ||
Matt Carlsoncf9ecf42011-11-28 09:41:03 +000014159 tp->fw_needed) {
14160 /* For firmware TSO, assume ASF is disabled.
14161 * We'll disable TSO later if we discover ASF
14162 * is enabled in tg3_get_eeprom_hw_cfg().
14163 */
Matt Carlsondabc5c62011-05-19 12:12:52 +000014164 tg3_flag_set(tp, TSO_CAPABLE);
Matt Carlsoncf9ecf42011-11-28 09:41:03 +000014165 } else {
Matt Carlsondabc5c62011-05-19 12:12:52 +000014166 tg3_flag_clear(tp, TSO_CAPABLE);
14167 tg3_flag_clear(tp, TSO_BUG);
14168 tp->fw_needed = NULL;
14169 }
14170
14171 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0)
14172 tp->fw_needed = FIRMWARE_TG3;
14173
Matt Carlson507399f2009-11-13 13:03:37 +000014174 tp->irq_max = 1;
14175
Joe Perches63c3a662011-04-26 08:12:10 +000014176 if (tg3_flag(tp, 5750_PLUS)) {
14177 tg3_flag_set(tp, SUPPORT_MSI);
Michael Chan7544b092007-05-05 13:08:32 -070014178 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_AX ||
14179 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_BX ||
14180 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714 &&
14181 tp->pci_chip_rev_id <= CHIPREV_ID_5714_A2 &&
14182 tp->pdev_peer == tp->pdev))
Joe Perches63c3a662011-04-26 08:12:10 +000014183 tg3_flag_clear(tp, SUPPORT_MSI);
Michael Chan7544b092007-05-05 13:08:32 -070014184
Joe Perches63c3a662011-04-26 08:12:10 +000014185 if (tg3_flag(tp, 5755_PLUS) ||
Michael Chanb5d37722006-09-27 16:06:21 -070014186 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Joe Perches63c3a662011-04-26 08:12:10 +000014187 tg3_flag_set(tp, 1SHOT_MSI);
Michael Chan52c0fd82006-06-29 20:15:54 -070014188 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014189
Joe Perches63c3a662011-04-26 08:12:10 +000014190 if (tg3_flag(tp, 57765_PLUS)) {
14191 tg3_flag_set(tp, SUPPORT_MSIX);
Matt Carlson507399f2009-11-13 13:03:37 +000014192 tp->irq_max = TG3_IRQ_MAX_VECS;
Matt Carlson90415472011-12-16 13:33:23 +000014193 tg3_rss_init_dflt_indir_tbl(tp);
Matt Carlson507399f2009-11-13 13:03:37 +000014194 }
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000014195 }
Matt Carlson0e1406d2009-11-02 12:33:33 +000014196
Matt Carlson2ffcc982011-05-19 12:12:44 +000014197 if (tg3_flag(tp, 5755_PLUS))
Joe Perches63c3a662011-04-26 08:12:10 +000014198 tg3_flag_set(tp, SHORT_DMA_BUG);
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000014199
Matt Carlsone31aa982011-07-27 14:20:53 +000014200 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
Matt Carlsona4cb4282011-12-14 11:09:58 +000014201 tp->dma_limit = TG3_TX_BD_DMA_MAX_4K;
Matt Carlsone31aa982011-07-27 14:20:53 +000014202
Matt Carlsonfa6b2aa2011-11-21 15:01:19 +000014203 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
14204 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
14205 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
Joe Perches63c3a662011-04-26 08:12:10 +000014206 tg3_flag_set(tp, LRG_PROD_RING_CAP);
Matt Carlsonde9f5232011-04-05 14:22:43 +000014207
Joe Perches63c3a662011-04-26 08:12:10 +000014208 if (tg3_flag(tp, 57765_PLUS) &&
Matt Carlsona0512942011-07-27 14:20:54 +000014209 tp->pci_chip_rev_id != CHIPREV_ID_5719_A0)
Joe Perches63c3a662011-04-26 08:12:10 +000014210 tg3_flag_set(tp, USE_JUMBO_BDFLAG);
Matt Carlsonb703df62009-12-03 08:36:21 +000014211
Joe Perches63c3a662011-04-26 08:12:10 +000014212 if (!tg3_flag(tp, 5705_PLUS) ||
14213 tg3_flag(tp, 5780_CLASS) ||
14214 tg3_flag(tp, USE_JUMBO_BDFLAG))
14215 tg3_flag_set(tp, JUMBO_CAPABLE);
Michael Chan0f893dc2005-07-25 12:30:38 -070014216
Matt Carlson52f44902008-11-21 17:17:04 -080014217 pci_read_config_dword(tp->pdev, TG3PCI_PCISTATE,
14218 &pci_state_reg);
14219
Jon Mason708ebb32011-06-27 12:56:50 +000014220 if (pci_is_pcie(tp->pdev)) {
Matt Carlson5e7dfd02008-11-21 17:18:16 -080014221 u16 lnkctl;
14222
Joe Perches63c3a662011-04-26 08:12:10 +000014223 tg3_flag_set(tp, PCI_EXPRESS);
Matt Carlson5f5c51e2007-11-12 21:19:37 -080014224
Matt Carlson2c55a3d2011-11-28 09:41:04 +000014225 if (tp->pci_chip_rev_id == CHIPREV_ID_5719_A0) {
14226 int readrq = pcie_get_readrq(tp->pdev);
14227 if (readrq > 2048)
14228 pcie_set_readrq(tp->pdev, 2048);
14229 }
Matt Carlson5f5c51e2007-11-12 21:19:37 -080014230
Matt Carlson5e7dfd02008-11-21 17:18:16 -080014231 pci_read_config_word(tp->pdev,
Jon Mason708ebb32011-06-27 12:56:50 +000014232 pci_pcie_cap(tp->pdev) + PCI_EXP_LNKCTL,
Matt Carlson5e7dfd02008-11-21 17:18:16 -080014233 &lnkctl);
14234 if (lnkctl & PCI_EXP_LNKCTL_CLKREQ_EN) {
Matt Carlson7196cd62011-05-19 16:02:44 +000014235 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
14236 ASIC_REV_5906) {
Joe Perches63c3a662011-04-26 08:12:10 +000014237 tg3_flag_clear(tp, HW_TSO_2);
Matt Carlsondabc5c62011-05-19 12:12:52 +000014238 tg3_flag_clear(tp, TSO_CAPABLE);
Matt Carlson7196cd62011-05-19 16:02:44 +000014239 }
Matt Carlson5e7dfd02008-11-21 17:18:16 -080014240 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson321d32a2008-11-21 17:22:19 -080014241 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson9cf74eb2009-04-20 06:58:27 +000014242 tp->pci_chip_rev_id == CHIPREV_ID_57780_A0 ||
14243 tp->pci_chip_rev_id == CHIPREV_ID_57780_A1)
Joe Perches63c3a662011-04-26 08:12:10 +000014244 tg3_flag_set(tp, CLKREQ_BUG);
Matt Carlson614b0592010-01-20 16:58:02 +000014245 } else if (tp->pci_chip_rev_id == CHIPREV_ID_5717_A0) {
Joe Perches63c3a662011-04-26 08:12:10 +000014246 tg3_flag_set(tp, L1PLLPD_EN);
Michael Chanc7835a72006-11-15 21:14:42 -080014247 }
Matt Carlson52f44902008-11-21 17:17:04 -080014248 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785) {
Jon Mason708ebb32011-06-27 12:56:50 +000014249 /* BCM5785 devices are effectively PCIe devices, and should
14250 * follow PCIe codepaths, but do not have a PCIe capabilities
14251 * section.
Matt Carlson93a700a2011-08-31 11:44:54 +000014252 */
Joe Perches63c3a662011-04-26 08:12:10 +000014253 tg3_flag_set(tp, PCI_EXPRESS);
14254 } else if (!tg3_flag(tp, 5705_PLUS) ||
14255 tg3_flag(tp, 5780_CLASS)) {
Matt Carlson52f44902008-11-21 17:17:04 -080014256 tp->pcix_cap = pci_find_capability(tp->pdev, PCI_CAP_ID_PCIX);
14257 if (!tp->pcix_cap) {
Matt Carlson2445e462010-04-05 10:19:21 +000014258 dev_err(&tp->pdev->dev,
14259 "Cannot find PCI-X capability, aborting\n");
Matt Carlson52f44902008-11-21 17:17:04 -080014260 return -EIO;
14261 }
14262
14263 if (!(pci_state_reg & PCISTATE_CONV_PCI_MODE))
Joe Perches63c3a662011-04-26 08:12:10 +000014264 tg3_flag_set(tp, PCIX_MODE);
Matt Carlson52f44902008-11-21 17:17:04 -080014265 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014266
Michael Chan399de502005-10-03 14:02:39 -070014267 /* If we have an AMD 762 or VIA K8T800 chipset, write
14268 * reordering to the mailbox registers done by the host
14269 * controller can cause major troubles. We read back from
14270 * every mailbox register write to force the writes to be
14271 * posted to the chip in order.
14272 */
Matt Carlson41434702011-03-09 16:58:22 +000014273 if (pci_dev_present(tg3_write_reorder_chipsets) &&
Joe Perches63c3a662011-04-26 08:12:10 +000014274 !tg3_flag(tp, PCI_EXPRESS))
14275 tg3_flag_set(tp, MBOX_WRITE_REORDER);
Michael Chan399de502005-10-03 14:02:39 -070014276
Matt Carlson69fc4052008-12-21 20:19:57 -080014277 pci_read_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE,
14278 &tp->pci_cacheline_sz);
14279 pci_read_config_byte(tp->pdev, PCI_LATENCY_TIMER,
14280 &tp->pci_lat_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014281 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 &&
14282 tp->pci_lat_timer < 64) {
14283 tp->pci_lat_timer = 64;
Matt Carlson69fc4052008-12-21 20:19:57 -080014284 pci_write_config_byte(tp->pdev, PCI_LATENCY_TIMER,
14285 tp->pci_lat_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014286 }
14287
Matt Carlson16821282011-07-13 09:27:28 +000014288 /* Important! -- It is critical that the PCI-X hw workaround
14289 * situation is decided before the first MMIO register access.
14290 */
Matt Carlson52f44902008-11-21 17:17:04 -080014291 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5700_BX) {
14292 /* 5700 BX chips need to have their TX producer index
14293 * mailboxes written twice to workaround a bug.
14294 */
Joe Perches63c3a662011-04-26 08:12:10 +000014295 tg3_flag_set(tp, TXD_MBOX_HWBUG);
Matt Carlson9974a352007-10-07 23:27:28 -070014296
Matt Carlson52f44902008-11-21 17:17:04 -080014297 /* If we are in PCI-X mode, enable register write workaround.
Linus Torvalds1da177e2005-04-16 15:20:36 -070014298 *
14299 * The workaround is to use indirect register accesses
14300 * for all chip writes not to mailbox registers.
14301 */
Joe Perches63c3a662011-04-26 08:12:10 +000014302 if (tg3_flag(tp, PCIX_MODE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070014303 u32 pm_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014304
Joe Perches63c3a662011-04-26 08:12:10 +000014305 tg3_flag_set(tp, PCIX_TARGET_HWBUG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014306
14307 /* The chip can have it's power management PCI config
14308 * space registers clobbered due to this bug.
14309 * So explicitly force the chip into D0 here.
14310 */
Matt Carlson9974a352007-10-07 23:27:28 -070014311 pci_read_config_dword(tp->pdev,
14312 tp->pm_cap + PCI_PM_CTRL,
Linus Torvalds1da177e2005-04-16 15:20:36 -070014313 &pm_reg);
14314 pm_reg &= ~PCI_PM_CTRL_STATE_MASK;
14315 pm_reg |= PCI_PM_CTRL_PME_ENABLE | 0 /* D0 */;
Matt Carlson9974a352007-10-07 23:27:28 -070014316 pci_write_config_dword(tp->pdev,
14317 tp->pm_cap + PCI_PM_CTRL,
Linus Torvalds1da177e2005-04-16 15:20:36 -070014318 pm_reg);
14319
14320 /* Also, force SERR#/PERR# in PCI command. */
14321 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
14322 pci_cmd |= PCI_COMMAND_PARITY | PCI_COMMAND_SERR;
14323 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
14324 }
14325 }
14326
Linus Torvalds1da177e2005-04-16 15:20:36 -070014327 if ((pci_state_reg & PCISTATE_BUS_SPEED_HIGH) != 0)
Joe Perches63c3a662011-04-26 08:12:10 +000014328 tg3_flag_set(tp, PCI_HIGH_SPEED);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014329 if ((pci_state_reg & PCISTATE_BUS_32BIT) != 0)
Joe Perches63c3a662011-04-26 08:12:10 +000014330 tg3_flag_set(tp, PCI_32BIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014331
14332 /* Chip-specific fixup from Broadcom driver */
14333 if ((tp->pci_chip_rev_id == CHIPREV_ID_5704_A0) &&
14334 (!(pci_state_reg & PCISTATE_RETRY_SAME_DMA))) {
14335 pci_state_reg |= PCISTATE_RETRY_SAME_DMA;
14336 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE, pci_state_reg);
14337 }
14338
Michael Chan1ee582d2005-08-09 20:16:46 -070014339 /* Default fast path register access methods */
Michael Chan20094932005-08-09 20:16:32 -070014340 tp->read32 = tg3_read32;
Michael Chan1ee582d2005-08-09 20:16:46 -070014341 tp->write32 = tg3_write32;
Michael Chan09ee9292005-08-09 20:17:00 -070014342 tp->read32_mbox = tg3_read32;
Michael Chan20094932005-08-09 20:16:32 -070014343 tp->write32_mbox = tg3_write32;
Michael Chan1ee582d2005-08-09 20:16:46 -070014344 tp->write32_tx_mbox = tg3_write32;
14345 tp->write32_rx_mbox = tg3_write32;
14346
14347 /* Various workaround register access methods */
Joe Perches63c3a662011-04-26 08:12:10 +000014348 if (tg3_flag(tp, PCIX_TARGET_HWBUG))
Michael Chan1ee582d2005-08-09 20:16:46 -070014349 tp->write32 = tg3_write_indirect_reg32;
Matt Carlson98efd8a2007-05-05 12:47:25 -070014350 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701 ||
Joe Perches63c3a662011-04-26 08:12:10 +000014351 (tg3_flag(tp, PCI_EXPRESS) &&
Matt Carlson98efd8a2007-05-05 12:47:25 -070014352 tp->pci_chip_rev_id == CHIPREV_ID_5750_A0)) {
14353 /*
14354 * Back to back register writes can cause problems on these
14355 * chips, the workaround is to read back all reg writes
14356 * except those to mailbox regs.
14357 *
14358 * See tg3_write_indirect_reg32().
14359 */
Michael Chan1ee582d2005-08-09 20:16:46 -070014360 tp->write32 = tg3_write_flush_reg32;
Matt Carlson98efd8a2007-05-05 12:47:25 -070014361 }
14362
Joe Perches63c3a662011-04-26 08:12:10 +000014363 if (tg3_flag(tp, TXD_MBOX_HWBUG) || tg3_flag(tp, MBOX_WRITE_REORDER)) {
Michael Chan1ee582d2005-08-09 20:16:46 -070014364 tp->write32_tx_mbox = tg3_write32_tx_mbox;
Joe Perches63c3a662011-04-26 08:12:10 +000014365 if (tg3_flag(tp, MBOX_WRITE_REORDER))
Michael Chan1ee582d2005-08-09 20:16:46 -070014366 tp->write32_rx_mbox = tg3_write_flush_reg32;
14367 }
Michael Chan20094932005-08-09 20:16:32 -070014368
Joe Perches63c3a662011-04-26 08:12:10 +000014369 if (tg3_flag(tp, ICH_WORKAROUND)) {
Michael Chan68929142005-08-09 20:17:14 -070014370 tp->read32 = tg3_read_indirect_reg32;
14371 tp->write32 = tg3_write_indirect_reg32;
14372 tp->read32_mbox = tg3_read_indirect_mbox;
14373 tp->write32_mbox = tg3_write_indirect_mbox;
14374 tp->write32_tx_mbox = tg3_write_indirect_mbox;
14375 tp->write32_rx_mbox = tg3_write_indirect_mbox;
14376
14377 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070014378 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070014379
14380 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
14381 pci_cmd &= ~PCI_COMMAND_MEMORY;
14382 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
14383 }
Michael Chanb5d37722006-09-27 16:06:21 -070014384 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
14385 tp->read32_mbox = tg3_read32_mbox_5906;
14386 tp->write32_mbox = tg3_write32_mbox_5906;
14387 tp->write32_tx_mbox = tg3_write32_mbox_5906;
14388 tp->write32_rx_mbox = tg3_write32_mbox_5906;
14389 }
Michael Chan68929142005-08-09 20:17:14 -070014390
Michael Chanbbadf502006-04-06 21:46:34 -070014391 if (tp->write32 == tg3_write_indirect_reg32 ||
Joe Perches63c3a662011-04-26 08:12:10 +000014392 (tg3_flag(tp, PCIX_MODE) &&
Michael Chanbbadf502006-04-06 21:46:34 -070014393 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
David S. Millerf49639e2006-06-09 11:58:36 -070014394 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)))
Joe Perches63c3a662011-04-26 08:12:10 +000014395 tg3_flag_set(tp, SRAM_USE_CONFIG);
Michael Chanbbadf502006-04-06 21:46:34 -070014396
Matt Carlson16821282011-07-13 09:27:28 +000014397 /* The memory arbiter has to be enabled in order for SRAM accesses
14398 * to succeed. Normally on powerup the tg3 chip firmware will make
14399 * sure it is enabled, but other entities such as system netboot
14400 * code might disable it.
14401 */
14402 val = tr32(MEMARB_MODE);
14403 tw32(MEMARB_MODE, val | MEMARB_MODE_ENABLE);
14404
Matt Carlson9dc5e342011-11-04 09:15:02 +000014405 tp->pci_fn = PCI_FUNC(tp->pdev->devfn) & 3;
14406 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
14407 tg3_flag(tp, 5780_CLASS)) {
14408 if (tg3_flag(tp, PCIX_MODE)) {
14409 pci_read_config_dword(tp->pdev,
14410 tp->pcix_cap + PCI_X_STATUS,
14411 &val);
14412 tp->pci_fn = val & 0x7;
14413 }
14414 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717) {
14415 tg3_read_mem(tp, NIC_SRAM_CPMU_STATUS, &val);
14416 if ((val & NIC_SRAM_CPMUSTAT_SIG_MSK) ==
14417 NIC_SRAM_CPMUSTAT_SIG) {
14418 tp->pci_fn = val & TG3_CPMU_STATUS_FMSK_5717;
14419 tp->pci_fn = tp->pci_fn ? 1 : 0;
14420 }
14421 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
14422 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
14423 tg3_read_mem(tp, NIC_SRAM_CPMU_STATUS, &val);
14424 if ((val & NIC_SRAM_CPMUSTAT_SIG_MSK) ==
14425 NIC_SRAM_CPMUSTAT_SIG) {
14426 tp->pci_fn = (val & TG3_CPMU_STATUS_FMSK_5719) >>
14427 TG3_CPMU_STATUS_FSHFT_5719;
14428 }
Matt Carlson69f11c92011-07-13 09:27:30 +000014429 }
14430
Michael Chan7d0c41e2005-04-21 17:06:20 -070014431 /* Get eeprom hw config before calling tg3_set_power_state().
Joe Perches63c3a662011-04-26 08:12:10 +000014432 * In particular, the TG3_FLAG_IS_NIC flag must be
Michael Chan7d0c41e2005-04-21 17:06:20 -070014433 * determined before calling tg3_set_power_state() so that
14434 * we know whether or not to switch out of Vaux power.
14435 * When the flag is set, it means that GPIO1 is used for eeprom
14436 * write protect and also implies that it is a LOM where GPIOs
14437 * are not used to switch power.
Jeff Garzik6aa20a22006-09-13 13:24:59 -040014438 */
Michael Chan7d0c41e2005-04-21 17:06:20 -070014439 tg3_get_eeprom_hw_cfg(tp);
14440
Matt Carlsoncf9ecf42011-11-28 09:41:03 +000014441 if (tp->fw_needed && tg3_flag(tp, ENABLE_ASF)) {
14442 tg3_flag_clear(tp, TSO_CAPABLE);
14443 tg3_flag_clear(tp, TSO_BUG);
14444 tp->fw_needed = NULL;
14445 }
14446
Joe Perches63c3a662011-04-26 08:12:10 +000014447 if (tg3_flag(tp, ENABLE_APE)) {
Matt Carlson0d3031d2007-10-10 18:02:43 -070014448 /* Allow reads and writes to the
14449 * APE register and memory space.
14450 */
14451 pci_state_reg |= PCISTATE_ALLOW_APE_CTLSPC_WR |
Matt Carlsonf92d9dc12010-06-05 17:24:30 +000014452 PCISTATE_ALLOW_APE_SHMEM_WR |
14453 PCISTATE_ALLOW_APE_PSPACE_WR;
Matt Carlson0d3031d2007-10-10 18:02:43 -070014454 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE,
14455 pci_state_reg);
Matt Carlsonc9cab242011-07-13 09:27:27 +000014456
14457 tg3_ape_lock_init(tp);
Matt Carlson0d3031d2007-10-10 18:02:43 -070014458 }
14459
Matt Carlson9936bcf2007-10-10 18:03:07 -070014460 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson57e69832008-05-25 23:48:31 -070014461 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson321d32a2008-11-21 17:22:19 -080014462 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000014463 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
Joe Perches63c3a662011-04-26 08:12:10 +000014464 tg3_flag(tp, 57765_PLUS))
14465 tg3_flag_set(tp, CPMU_PRESENT);
Matt Carlsond30cdd22007-10-07 23:28:35 -070014466
Matt Carlson16821282011-07-13 09:27:28 +000014467 /* Set up tp->grc_local_ctrl before calling
14468 * tg3_pwrsrc_switch_to_vmain(). GPIO1 driven high
14469 * will bring 5700's external PHY out of reset.
Michael Chan314fba32005-04-21 17:07:04 -070014470 * It is also used as eeprom write protect on LOMs.
14471 */
14472 tp->grc_local_ctrl = GRC_LCLCTRL_INT_ON_ATTN | GRC_LCLCTRL_AUTO_SEEPROM;
Matt Carlson6ff6f812011-05-19 12:12:54 +000014473 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
Joe Perches63c3a662011-04-26 08:12:10 +000014474 tg3_flag(tp, EEPROM_WRITE_PROT))
Michael Chan314fba32005-04-21 17:07:04 -070014475 tp->grc_local_ctrl |= (GRC_LCLCTRL_GPIO_OE1 |
14476 GRC_LCLCTRL_GPIO_OUTPUT1);
Michael Chan3e7d83b2005-04-21 17:10:36 -070014477 /* Unused GPIO3 must be driven as output on 5752 because there
14478 * are no pull-up resistors on unused GPIO pins.
14479 */
14480 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752)
14481 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE3;
Michael Chan314fba32005-04-21 17:07:04 -070014482
Matt Carlson321d32a2008-11-21 17:22:19 -080014483 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
Matt Carlsoncb4ed1f2010-01-20 16:58:09 +000014484 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
Matt Carlson55086ad2011-12-14 11:09:59 +000014485 tg3_flag(tp, 57765_CLASS))
Michael Chanaf36e6b2006-03-23 01:28:06 -080014486 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_UART_SEL;
14487
Matt Carlson8d519ab2009-04-20 06:58:01 +000014488 if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761 ||
14489 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761S) {
Matt Carlson5f0c4a32008-06-09 15:41:12 -070014490 /* Turn off the debug UART. */
14491 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_UART_SEL;
Joe Perches63c3a662011-04-26 08:12:10 +000014492 if (tg3_flag(tp, IS_NIC))
Matt Carlson5f0c4a32008-06-09 15:41:12 -070014493 /* Keep VMain power. */
14494 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE0 |
14495 GRC_LCLCTRL_GPIO_OUTPUT0;
14496 }
14497
Matt Carlson16821282011-07-13 09:27:28 +000014498 /* Switch out of Vaux if it is a NIC */
14499 tg3_pwrsrc_switch_to_vmain(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014500
Linus Torvalds1da177e2005-04-16 15:20:36 -070014501 /* Derive initial jumbo mode from MTU assigned in
14502 * ether_setup() via the alloc_etherdev() call
14503 */
Joe Perches63c3a662011-04-26 08:12:10 +000014504 if (tp->dev->mtu > ETH_DATA_LEN && !tg3_flag(tp, 5780_CLASS))
14505 tg3_flag_set(tp, JUMBO_RING_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014506
14507 /* Determine WakeOnLan speed to use. */
14508 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
14509 tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
14510 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0 ||
14511 tp->pci_chip_rev_id == CHIPREV_ID_5701_B2) {
Joe Perches63c3a662011-04-26 08:12:10 +000014512 tg3_flag_clear(tp, WOL_SPEED_100MB);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014513 } else {
Joe Perches63c3a662011-04-26 08:12:10 +000014514 tg3_flag_set(tp, WOL_SPEED_100MB);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014515 }
14516
Matt Carlson7f97a4b2009-08-25 10:10:03 +000014517 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014518 tp->phy_flags |= TG3_PHYFLG_IS_FET;
Matt Carlson7f97a4b2009-08-25 10:10:03 +000014519
Linus Torvalds1da177e2005-04-16 15:20:36 -070014520 /* A few boards don't want Ethernet@WireSpeed phy feature */
Matt Carlson6ff6f812011-05-19 12:12:54 +000014521 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
14522 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070014523 (tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) &&
Michael Chan747e8f82005-07-25 12:33:22 -070014524 (tp->pci_chip_rev_id != CHIPREV_ID_5705_A1)) ||
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014525 (tp->phy_flags & TG3_PHYFLG_IS_FET) ||
14526 (tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
14527 tp->phy_flags |= TG3_PHYFLG_NO_ETH_WIRE_SPEED;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014528
14529 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5703_AX ||
14530 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5704_AX)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014531 tp->phy_flags |= TG3_PHYFLG_ADC_BUG;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014532 if (tp->pci_chip_rev_id == CHIPREV_ID_5704_A0)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014533 tp->phy_flags |= TG3_PHYFLG_5704_A0_BUG;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014534
Joe Perches63c3a662011-04-26 08:12:10 +000014535 if (tg3_flag(tp, 5705_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014536 !(tp->phy_flags & TG3_PHYFLG_IS_FET) &&
Matt Carlson321d32a2008-11-21 17:22:19 -080014537 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000014538 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_57780 &&
Joe Perches63c3a662011-04-26 08:12:10 +000014539 !tg3_flag(tp, 57765_PLUS)) {
Michael Chanc424cb22006-04-29 18:56:34 -070014540 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
Matt Carlsond30cdd22007-10-07 23:28:35 -070014541 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787 ||
Matt Carlson9936bcf2007-10-10 18:03:07 -070014542 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
14543 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761) {
Michael Chand4011ad2007-02-13 12:17:25 -080014544 if (tp->pdev->device != PCI_DEVICE_ID_TIGON3_5756 &&
14545 tp->pdev->device != PCI_DEVICE_ID_TIGON3_5722)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014546 tp->phy_flags |= TG3_PHYFLG_JITTER_BUG;
Michael Chanc1d2a192007-01-08 19:57:20 -080014547 if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5755M)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014548 tp->phy_flags |= TG3_PHYFLG_ADJUST_TRIM;
Matt Carlson321d32a2008-11-21 17:22:19 -080014549 } else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014550 tp->phy_flags |= TG3_PHYFLG_BER_BUG;
Michael Chanc424cb22006-04-29 18:56:34 -070014551 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014552
Matt Carlsonb2a5c192008-04-03 21:44:44 -070014553 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
14554 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX) {
14555 tp->phy_otp = tg3_read_otp_phycfg(tp);
14556 if (tp->phy_otp == 0)
14557 tp->phy_otp = TG3_OTP_DEFAULT;
14558 }
14559
Joe Perches63c3a662011-04-26 08:12:10 +000014560 if (tg3_flag(tp, CPMU_PRESENT))
Matt Carlson8ef21422008-05-02 16:47:53 -070014561 tp->mi_mode = MAC_MI_MODE_500KHZ_CONST;
14562 else
14563 tp->mi_mode = MAC_MI_MODE_BASE;
14564
Linus Torvalds1da177e2005-04-16 15:20:36 -070014565 tp->coalesce_mode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014566 if (GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5700_AX &&
14567 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5700_BX)
14568 tp->coalesce_mode |= HOSTCC_MODE_32BYTE;
14569
Matt Carlson4d958472011-04-20 07:57:35 +000014570 /* Set these bits to enable statistics workaround. */
14571 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
14572 tp->pci_chip_rev_id == CHIPREV_ID_5719_A0 ||
14573 tp->pci_chip_rev_id == CHIPREV_ID_5720_A0) {
14574 tp->coalesce_mode |= HOSTCC_MODE_ATTN;
14575 tp->grc_mode |= GRC_MODE_IRQ_ON_FLOW_ATTN;
14576 }
14577
Matt Carlson321d32a2008-11-21 17:22:19 -080014578 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
14579 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
Joe Perches63c3a662011-04-26 08:12:10 +000014580 tg3_flag_set(tp, USE_PHYLIB);
Matt Carlson57e69832008-05-25 23:48:31 -070014581
Matt Carlson158d7ab2008-05-29 01:37:54 -070014582 err = tg3_mdio_init(tp);
14583 if (err)
14584 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014585
14586 /* Initialize data/descriptor byte/word swapping. */
14587 val = tr32(GRC_MODE);
Matt Carlsonf2096f92011-04-05 14:22:48 +000014588 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
14589 val &= (GRC_MODE_BYTE_SWAP_B2HRX_DATA |
14590 GRC_MODE_WORD_SWAP_B2HRX_DATA |
14591 GRC_MODE_B2HRX_ENABLE |
14592 GRC_MODE_HTX2B_ENABLE |
14593 GRC_MODE_HOST_STACKUP);
14594 else
14595 val &= GRC_MODE_HOST_STACKUP;
14596
Linus Torvalds1da177e2005-04-16 15:20:36 -070014597 tw32(GRC_MODE, val | tp->grc_mode);
14598
14599 tg3_switch_clocks(tp);
14600
14601 /* Clear this out for sanity. */
14602 tw32(TG3PCI_MEM_WIN_BASE_ADDR, 0);
14603
14604 pci_read_config_dword(tp->pdev, TG3PCI_PCISTATE,
14605 &pci_state_reg);
14606 if ((pci_state_reg & PCISTATE_CONV_PCI_MODE) == 0 &&
Joe Perches63c3a662011-04-26 08:12:10 +000014607 !tg3_flag(tp, PCIX_TARGET_HWBUG)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070014608 u32 chiprevid = GET_CHIP_REV_ID(tp->misc_host_ctrl);
14609
14610 if (chiprevid == CHIPREV_ID_5701_A0 ||
14611 chiprevid == CHIPREV_ID_5701_B0 ||
14612 chiprevid == CHIPREV_ID_5701_B2 ||
14613 chiprevid == CHIPREV_ID_5701_B5) {
14614 void __iomem *sram_base;
14615
14616 /* Write some dummy words into the SRAM status block
14617 * area, see if it reads back correctly. If the return
14618 * value is bad, force enable the PCIX workaround.
14619 */
14620 sram_base = tp->regs + NIC_SRAM_WIN_BASE + NIC_SRAM_STATS_BLK;
14621
14622 writel(0x00000000, sram_base);
14623 writel(0x00000000, sram_base + 4);
14624 writel(0xffffffff, sram_base + 4);
14625 if (readl(sram_base) != 0x00000000)
Joe Perches63c3a662011-04-26 08:12:10 +000014626 tg3_flag_set(tp, PCIX_TARGET_HWBUG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014627 }
14628 }
14629
14630 udelay(50);
14631 tg3_nvram_init(tp);
14632
14633 grc_misc_cfg = tr32(GRC_MISC_CFG);
14634 grc_misc_cfg &= GRC_MISC_CFG_BOARD_ID_MASK;
14635
Linus Torvalds1da177e2005-04-16 15:20:36 -070014636 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
14637 (grc_misc_cfg == GRC_MISC_CFG_BOARD_ID_5788 ||
14638 grc_misc_cfg == GRC_MISC_CFG_BOARD_ID_5788M))
Joe Perches63c3a662011-04-26 08:12:10 +000014639 tg3_flag_set(tp, IS_5788);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014640
Joe Perches63c3a662011-04-26 08:12:10 +000014641 if (!tg3_flag(tp, IS_5788) &&
Matt Carlson6ff6f812011-05-19 12:12:54 +000014642 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700)
Joe Perches63c3a662011-04-26 08:12:10 +000014643 tg3_flag_set(tp, TAGGED_STATUS);
14644 if (tg3_flag(tp, TAGGED_STATUS)) {
David S. Millerfac9b832005-05-18 22:46:34 -070014645 tp->coalesce_mode |= (HOSTCC_MODE_CLRTICK_RXBD |
14646 HOSTCC_MODE_CLRTICK_TXBD);
14647
14648 tp->misc_host_ctrl |= MISC_HOST_CTRL_TAGGED_STATUS;
14649 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
14650 tp->misc_host_ctrl);
14651 }
14652
Matt Carlson3bda1252008-08-15 14:08:22 -070014653 /* Preserve the APE MAC_MODE bits */
Joe Perches63c3a662011-04-26 08:12:10 +000014654 if (tg3_flag(tp, ENABLE_APE))
Matt Carlsond2394e6b2010-11-24 08:31:47 +000014655 tp->mac_mode = MAC_MODE_APE_TX_EN | MAC_MODE_APE_RX_EN;
Matt Carlson3bda1252008-08-15 14:08:22 -070014656 else
Matt Carlson6e01b202011-08-19 13:58:20 +000014657 tp->mac_mode = 0;
Matt Carlson3bda1252008-08-15 14:08:22 -070014658
Linus Torvalds1da177e2005-04-16 15:20:36 -070014659 /* these are limited to 10/100 only */
14660 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 &&
14661 (grc_misc_cfg == 0x8000 || grc_misc_cfg == 0x4000)) ||
14662 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
14663 tp->pdev->vendor == PCI_VENDOR_ID_BROADCOM &&
14664 (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5901 ||
14665 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5901_2 ||
14666 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5705F)) ||
14667 (tp->pdev->vendor == PCI_VENDOR_ID_BROADCOM &&
14668 (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5751F ||
Michael Chan676917d2006-12-07 00:20:22 -080014669 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5753F ||
14670 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5787F)) ||
Matt Carlson321d32a2008-11-21 17:22:19 -080014671 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57790 ||
Matt Carlsond1101142010-02-17 15:16:55 +000014672 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57791 ||
14673 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57795 ||
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014674 (tp->phy_flags & TG3_PHYFLG_IS_FET))
14675 tp->phy_flags |= TG3_PHYFLG_10_100_ONLY;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014676
14677 err = tg3_phy_probe(tp);
14678 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000014679 dev_err(&tp->pdev->dev, "phy probe failed, err %d\n", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014680 /* ... but do not return immediately ... */
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070014681 tg3_mdio_fini(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014682 }
14683
Matt Carlson184b8902010-04-05 10:19:25 +000014684 tg3_read_vpd(tp);
Michael Chanc4e65752006-03-20 22:29:32 -080014685 tg3_read_fw_ver(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014686
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014687 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
14688 tp->phy_flags &= ~TG3_PHYFLG_USE_MI_INTERRUPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014689 } else {
14690 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014691 tp->phy_flags |= TG3_PHYFLG_USE_MI_INTERRUPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014692 else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014693 tp->phy_flags &= ~TG3_PHYFLG_USE_MI_INTERRUPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014694 }
14695
14696 /* 5700 {AX,BX} chips have a broken status block link
14697 * change bit implementation, so we must use the
14698 * status register in those cases.
14699 */
14700 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700)
Joe Perches63c3a662011-04-26 08:12:10 +000014701 tg3_flag_set(tp, USE_LINKCHG_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014702 else
Joe Perches63c3a662011-04-26 08:12:10 +000014703 tg3_flag_clear(tp, USE_LINKCHG_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014704
14705 /* The led_ctrl is set during tg3_phy_probe, here we might
14706 * have to force the link status polling mechanism based
14707 * upon subsystem IDs.
14708 */
14709 if (tp->pdev->subsystem_vendor == PCI_VENDOR_ID_DELL &&
Michael Chan007a880d2007-05-31 14:49:51 -070014710 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701 &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014711 !(tp->phy_flags & TG3_PHYFLG_PHY_SERDES)) {
14712 tp->phy_flags |= TG3_PHYFLG_USE_MI_INTERRUPT;
Joe Perches63c3a662011-04-26 08:12:10 +000014713 tg3_flag_set(tp, USE_LINKCHG_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014714 }
14715
14716 /* For all SERDES we poll the MAC status register. */
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014717 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Joe Perches63c3a662011-04-26 08:12:10 +000014718 tg3_flag_set(tp, POLL_SERDES);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014719 else
Joe Perches63c3a662011-04-26 08:12:10 +000014720 tg3_flag_clear(tp, POLL_SERDES);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014721
Eric Dumazet9205fd92011-11-18 06:47:01 +000014722 tp->rx_offset = NET_SKB_PAD + NET_IP_ALIGN;
Matt Carlsond2757fc2010-04-12 06:58:27 +000014723 tp->rx_copy_thresh = TG3_RX_COPY_THRESHOLD;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014724 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701 &&
Joe Perches63c3a662011-04-26 08:12:10 +000014725 tg3_flag(tp, PCIX_MODE)) {
Eric Dumazet9205fd92011-11-18 06:47:01 +000014726 tp->rx_offset = NET_SKB_PAD;
Matt Carlsond2757fc2010-04-12 06:58:27 +000014727#ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
Matt Carlson9dc7a112010-04-12 06:58:28 +000014728 tp->rx_copy_thresh = ~(u16)0;
Matt Carlsond2757fc2010-04-12 06:58:27 +000014729#endif
14730 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014731
Matt Carlson2c49a442010-09-30 10:34:35 +000014732 tp->rx_std_ring_mask = TG3_RX_STD_RING_SIZE(tp) - 1;
14733 tp->rx_jmb_ring_mask = TG3_RX_JMB_RING_SIZE(tp) - 1;
Matt Carlson7cb32cf2010-09-30 10:34:36 +000014734 tp->rx_ret_ring_mask = tg3_rx_ret_ring_size(tp) - 1;
14735
Matt Carlson2c49a442010-09-30 10:34:35 +000014736 tp->rx_std_max_post = tp->rx_std_ring_mask + 1;
Michael Chanf92905d2006-06-29 20:14:29 -070014737
14738 /* Increment the rx prod index on the rx std ring by at most
14739 * 8 for these chips to workaround hw errata.
14740 */
14741 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 ||
14742 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752 ||
14743 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755)
14744 tp->rx_std_max_post = 8;
14745
Joe Perches63c3a662011-04-26 08:12:10 +000014746 if (tg3_flag(tp, ASPM_WORKAROUND))
Matt Carlson8ed5d972007-05-07 00:25:49 -070014747 tp->pwrmgmt_thresh = tr32(PCIE_PWR_MGMT_THRESH) &
14748 PCIE_PWR_MGMT_L1_THRESH_MSK;
14749
Linus Torvalds1da177e2005-04-16 15:20:36 -070014750 return err;
14751}
14752
David S. Miller49b6e95f2007-03-29 01:38:42 -070014753#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070014754static int __devinit tg3_get_macaddr_sparc(struct tg3 *tp)
14755{
14756 struct net_device *dev = tp->dev;
14757 struct pci_dev *pdev = tp->pdev;
David S. Miller49b6e95f2007-03-29 01:38:42 -070014758 struct device_node *dp = pci_device_to_OF_node(pdev);
David S. Miller374d4ca2007-03-29 01:57:57 -070014759 const unsigned char *addr;
David S. Miller49b6e95f2007-03-29 01:38:42 -070014760 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014761
David S. Miller49b6e95f2007-03-29 01:38:42 -070014762 addr = of_get_property(dp, "local-mac-address", &len);
14763 if (addr && len == 6) {
14764 memcpy(dev->dev_addr, addr, 6);
14765 memcpy(dev->perm_addr, dev->dev_addr, 6);
14766 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014767 }
14768 return -ENODEV;
14769}
14770
14771static int __devinit tg3_get_default_macaddr_sparc(struct tg3 *tp)
14772{
14773 struct net_device *dev = tp->dev;
14774
14775 memcpy(dev->dev_addr, idprom->id_ethaddr, 6);
John W. Linville2ff43692005-09-12 14:44:20 -070014776 memcpy(dev->perm_addr, idprom->id_ethaddr, 6);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014777 return 0;
14778}
14779#endif
14780
14781static int __devinit tg3_get_device_address(struct tg3 *tp)
14782{
14783 struct net_device *dev = tp->dev;
14784 u32 hi, lo, mac_offset;
Michael Chan008652b2006-03-27 23:14:53 -080014785 int addr_ok = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014786
David S. Miller49b6e95f2007-03-29 01:38:42 -070014787#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070014788 if (!tg3_get_macaddr_sparc(tp))
14789 return 0;
14790#endif
14791
14792 mac_offset = 0x7c;
Matt Carlson6ff6f812011-05-19 12:12:54 +000014793 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
Joe Perches63c3a662011-04-26 08:12:10 +000014794 tg3_flag(tp, 5780_CLASS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070014795 if (tr32(TG3PCI_DUAL_MAC_CTRL) & DUAL_MAC_CTRL_ID)
14796 mac_offset = 0xcc;
14797 if (tg3_nvram_lock(tp))
14798 tw32_f(NVRAM_CMD, NVRAM_CMD_RESET);
14799 else
14800 tg3_nvram_unlock(tp);
Joe Perches63c3a662011-04-26 08:12:10 +000014801 } else if (tg3_flag(tp, 5717_PLUS)) {
Matt Carlson69f11c92011-07-13 09:27:30 +000014802 if (tp->pci_fn & 1)
Matt Carlsona1b950d2009-09-01 13:20:17 +000014803 mac_offset = 0xcc;
Matt Carlson69f11c92011-07-13 09:27:30 +000014804 if (tp->pci_fn > 1)
Matt Carlsona50d0792010-06-05 17:24:37 +000014805 mac_offset += 0x18c;
Matt Carlsona1b950d2009-09-01 13:20:17 +000014806 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
Michael Chanb5d37722006-09-27 16:06:21 -070014807 mac_offset = 0x10;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014808
14809 /* First try to get it from MAC address mailbox. */
14810 tg3_read_mem(tp, NIC_SRAM_MAC_ADDR_HIGH_MBOX, &hi);
14811 if ((hi >> 16) == 0x484b) {
14812 dev->dev_addr[0] = (hi >> 8) & 0xff;
14813 dev->dev_addr[1] = (hi >> 0) & 0xff;
14814
14815 tg3_read_mem(tp, NIC_SRAM_MAC_ADDR_LOW_MBOX, &lo);
14816 dev->dev_addr[2] = (lo >> 24) & 0xff;
14817 dev->dev_addr[3] = (lo >> 16) & 0xff;
14818 dev->dev_addr[4] = (lo >> 8) & 0xff;
14819 dev->dev_addr[5] = (lo >> 0) & 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014820
Michael Chan008652b2006-03-27 23:14:53 -080014821 /* Some old bootcode may report a 0 MAC address in SRAM */
14822 addr_ok = is_valid_ether_addr(&dev->dev_addr[0]);
14823 }
14824 if (!addr_ok) {
14825 /* Next, try NVRAM. */
Joe Perches63c3a662011-04-26 08:12:10 +000014826 if (!tg3_flag(tp, NO_NVRAM) &&
Matt Carlsondf259d82009-04-20 06:57:14 +000014827 !tg3_nvram_read_be32(tp, mac_offset + 0, &hi) &&
Matt Carlson6d348f22009-02-25 14:25:52 +000014828 !tg3_nvram_read_be32(tp, mac_offset + 4, &lo)) {
Matt Carlson62cedd12009-04-20 14:52:29 -070014829 memcpy(&dev->dev_addr[0], ((char *)&hi) + 2, 2);
14830 memcpy(&dev->dev_addr[2], (char *)&lo, sizeof(lo));
Michael Chan008652b2006-03-27 23:14:53 -080014831 }
14832 /* Finally just fetch it out of the MAC control regs. */
14833 else {
14834 hi = tr32(MAC_ADDR_0_HIGH);
14835 lo = tr32(MAC_ADDR_0_LOW);
14836
14837 dev->dev_addr[5] = lo & 0xff;
14838 dev->dev_addr[4] = (lo >> 8) & 0xff;
14839 dev->dev_addr[3] = (lo >> 16) & 0xff;
14840 dev->dev_addr[2] = (lo >> 24) & 0xff;
14841 dev->dev_addr[1] = hi & 0xff;
14842 dev->dev_addr[0] = (hi >> 8) & 0xff;
14843 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014844 }
14845
14846 if (!is_valid_ether_addr(&dev->dev_addr[0])) {
David S. Miller7582a332008-03-20 15:53:15 -070014847#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070014848 if (!tg3_get_default_macaddr_sparc(tp))
14849 return 0;
14850#endif
14851 return -EINVAL;
14852 }
John W. Linville2ff43692005-09-12 14:44:20 -070014853 memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014854 return 0;
14855}
14856
David S. Miller59e6b432005-05-18 22:50:10 -070014857#define BOUNDARY_SINGLE_CACHELINE 1
14858#define BOUNDARY_MULTI_CACHELINE 2
14859
14860static u32 __devinit tg3_calc_dma_bndry(struct tg3 *tp, u32 val)
14861{
14862 int cacheline_size;
14863 u8 byte;
14864 int goal;
14865
14866 pci_read_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE, &byte);
14867 if (byte == 0)
14868 cacheline_size = 1024;
14869 else
14870 cacheline_size = (int) byte * 4;
14871
14872 /* On 5703 and later chips, the boundary bits have no
14873 * effect.
14874 */
14875 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
14876 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701 &&
Joe Perches63c3a662011-04-26 08:12:10 +000014877 !tg3_flag(tp, PCI_EXPRESS))
David S. Miller59e6b432005-05-18 22:50:10 -070014878 goto out;
14879
14880#if defined(CONFIG_PPC64) || defined(CONFIG_IA64) || defined(CONFIG_PARISC)
14881 goal = BOUNDARY_MULTI_CACHELINE;
14882#else
14883#if defined(CONFIG_SPARC64) || defined(CONFIG_ALPHA)
14884 goal = BOUNDARY_SINGLE_CACHELINE;
14885#else
14886 goal = 0;
14887#endif
14888#endif
14889
Joe Perches63c3a662011-04-26 08:12:10 +000014890 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000014891 val = goal ? 0 : DMA_RWCTRL_DIS_CACHE_ALIGNMENT;
14892 goto out;
14893 }
14894
David S. Miller59e6b432005-05-18 22:50:10 -070014895 if (!goal)
14896 goto out;
14897
14898 /* PCI controllers on most RISC systems tend to disconnect
14899 * when a device tries to burst across a cache-line boundary.
14900 * Therefore, letting tg3 do so just wastes PCI bandwidth.
14901 *
14902 * Unfortunately, for PCI-E there are only limited
14903 * write-side controls for this, and thus for reads
14904 * we will still get the disconnects. We'll also waste
14905 * these PCI cycles for both read and write for chips
14906 * other than 5700 and 5701 which do not implement the
14907 * boundary bits.
14908 */
Joe Perches63c3a662011-04-26 08:12:10 +000014909 if (tg3_flag(tp, PCIX_MODE) && !tg3_flag(tp, PCI_EXPRESS)) {
David S. Miller59e6b432005-05-18 22:50:10 -070014910 switch (cacheline_size) {
14911 case 16:
14912 case 32:
14913 case 64:
14914 case 128:
14915 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14916 val |= (DMA_RWCTRL_READ_BNDRY_128_PCIX |
14917 DMA_RWCTRL_WRITE_BNDRY_128_PCIX);
14918 } else {
14919 val |= (DMA_RWCTRL_READ_BNDRY_384_PCIX |
14920 DMA_RWCTRL_WRITE_BNDRY_384_PCIX);
14921 }
14922 break;
14923
14924 case 256:
14925 val |= (DMA_RWCTRL_READ_BNDRY_256_PCIX |
14926 DMA_RWCTRL_WRITE_BNDRY_256_PCIX);
14927 break;
14928
14929 default:
14930 val |= (DMA_RWCTRL_READ_BNDRY_384_PCIX |
14931 DMA_RWCTRL_WRITE_BNDRY_384_PCIX);
14932 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070014933 }
Joe Perches63c3a662011-04-26 08:12:10 +000014934 } else if (tg3_flag(tp, PCI_EXPRESS)) {
David S. Miller59e6b432005-05-18 22:50:10 -070014935 switch (cacheline_size) {
14936 case 16:
14937 case 32:
14938 case 64:
14939 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14940 val &= ~DMA_RWCTRL_WRITE_BNDRY_DISAB_PCIE;
14941 val |= DMA_RWCTRL_WRITE_BNDRY_64_PCIE;
14942 break;
14943 }
14944 /* fallthrough */
14945 case 128:
14946 default:
14947 val &= ~DMA_RWCTRL_WRITE_BNDRY_DISAB_PCIE;
14948 val |= DMA_RWCTRL_WRITE_BNDRY_128_PCIE;
14949 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070014950 }
David S. Miller59e6b432005-05-18 22:50:10 -070014951 } else {
14952 switch (cacheline_size) {
14953 case 16:
14954 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14955 val |= (DMA_RWCTRL_READ_BNDRY_16 |
14956 DMA_RWCTRL_WRITE_BNDRY_16);
14957 break;
14958 }
14959 /* fallthrough */
14960 case 32:
14961 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14962 val |= (DMA_RWCTRL_READ_BNDRY_32 |
14963 DMA_RWCTRL_WRITE_BNDRY_32);
14964 break;
14965 }
14966 /* fallthrough */
14967 case 64:
14968 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14969 val |= (DMA_RWCTRL_READ_BNDRY_64 |
14970 DMA_RWCTRL_WRITE_BNDRY_64);
14971 break;
14972 }
14973 /* fallthrough */
14974 case 128:
14975 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14976 val |= (DMA_RWCTRL_READ_BNDRY_128 |
14977 DMA_RWCTRL_WRITE_BNDRY_128);
14978 break;
14979 }
14980 /* fallthrough */
14981 case 256:
14982 val |= (DMA_RWCTRL_READ_BNDRY_256 |
14983 DMA_RWCTRL_WRITE_BNDRY_256);
14984 break;
14985 case 512:
14986 val |= (DMA_RWCTRL_READ_BNDRY_512 |
14987 DMA_RWCTRL_WRITE_BNDRY_512);
14988 break;
14989 case 1024:
14990 default:
14991 val |= (DMA_RWCTRL_READ_BNDRY_1024 |
14992 DMA_RWCTRL_WRITE_BNDRY_1024);
14993 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070014994 }
David S. Miller59e6b432005-05-18 22:50:10 -070014995 }
14996
14997out:
14998 return val;
14999}
15000
Linus Torvalds1da177e2005-04-16 15:20:36 -070015001static int __devinit tg3_do_test_dma(struct tg3 *tp, u32 *buf, dma_addr_t buf_dma, int size, int to_device)
15002{
15003 struct tg3_internal_buffer_desc test_desc;
15004 u32 sram_dma_descs;
15005 int i, ret;
15006
15007 sram_dma_descs = NIC_SRAM_DMA_DESC_POOL_BASE;
15008
15009 tw32(FTQ_RCVBD_COMP_FIFO_ENQDEQ, 0);
15010 tw32(FTQ_RCVDATA_COMP_FIFO_ENQDEQ, 0);
15011 tw32(RDMAC_STATUS, 0);
15012 tw32(WDMAC_STATUS, 0);
15013
15014 tw32(BUFMGR_MODE, 0);
15015 tw32(FTQ_RESET, 0);
15016
15017 test_desc.addr_hi = ((u64) buf_dma) >> 32;
15018 test_desc.addr_lo = buf_dma & 0xffffffff;
15019 test_desc.nic_mbuf = 0x00002100;
15020 test_desc.len = size;
15021
15022 /*
15023 * HP ZX1 was seeing test failures for 5701 cards running at 33Mhz
15024 * the *second* time the tg3 driver was getting loaded after an
15025 * initial scan.
15026 *
15027 * Broadcom tells me:
15028 * ...the DMA engine is connected to the GRC block and a DMA
15029 * reset may affect the GRC block in some unpredictable way...
15030 * The behavior of resets to individual blocks has not been tested.
15031 *
15032 * Broadcom noted the GRC reset will also reset all sub-components.
15033 */
15034 if (to_device) {
15035 test_desc.cqid_sqid = (13 << 8) | 2;
15036
15037 tw32_f(RDMAC_MODE, RDMAC_MODE_ENABLE);
15038 udelay(40);
15039 } else {
15040 test_desc.cqid_sqid = (16 << 8) | 7;
15041
15042 tw32_f(WDMAC_MODE, WDMAC_MODE_ENABLE);
15043 udelay(40);
15044 }
15045 test_desc.flags = 0x00000005;
15046
15047 for (i = 0; i < (sizeof(test_desc) / sizeof(u32)); i++) {
15048 u32 val;
15049
15050 val = *(((u32 *)&test_desc) + i);
15051 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR,
15052 sram_dma_descs + (i * sizeof(u32)));
15053 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_DATA, val);
15054 }
15055 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, 0);
15056
Matt Carlson859a588792010-04-05 10:19:28 +000015057 if (to_device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015058 tw32(FTQ_DMA_HIGH_READ_FIFO_ENQDEQ, sram_dma_descs);
Matt Carlson859a588792010-04-05 10:19:28 +000015059 else
Linus Torvalds1da177e2005-04-16 15:20:36 -070015060 tw32(FTQ_DMA_HIGH_WRITE_FIFO_ENQDEQ, sram_dma_descs);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015061
15062 ret = -ENODEV;
15063 for (i = 0; i < 40; i++) {
15064 u32 val;
15065
15066 if (to_device)
15067 val = tr32(FTQ_RCVBD_COMP_FIFO_ENQDEQ);
15068 else
15069 val = tr32(FTQ_RCVDATA_COMP_FIFO_ENQDEQ);
15070 if ((val & 0xffff) == sram_dma_descs) {
15071 ret = 0;
15072 break;
15073 }
15074
15075 udelay(100);
15076 }
15077
15078 return ret;
15079}
15080
David S. Millerded73402005-05-23 13:59:47 -070015081#define TEST_BUFFER_SIZE 0x2000
Linus Torvalds1da177e2005-04-16 15:20:36 -070015082
Matt Carlson41434702011-03-09 16:58:22 +000015083static DEFINE_PCI_DEVICE_TABLE(tg3_dma_wait_state_chipsets) = {
Joe Perches895950c2010-12-21 02:16:08 -080015084 { PCI_DEVICE(PCI_VENDOR_ID_APPLE, PCI_DEVICE_ID_APPLE_UNI_N_PCI15) },
15085 { },
15086};
15087
Linus Torvalds1da177e2005-04-16 15:20:36 -070015088static int __devinit tg3_test_dma(struct tg3 *tp)
15089{
15090 dma_addr_t buf_dma;
David S. Miller59e6b432005-05-18 22:50:10 -070015091 u32 *buf, saved_dma_rwctrl;
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000015092 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015093
Matt Carlson4bae65c2010-11-24 08:31:52 +000015094 buf = dma_alloc_coherent(&tp->pdev->dev, TEST_BUFFER_SIZE,
15095 &buf_dma, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015096 if (!buf) {
15097 ret = -ENOMEM;
15098 goto out_nofree;
15099 }
15100
15101 tp->dma_rwctrl = ((0x7 << DMA_RWCTRL_PCI_WRITE_CMD_SHIFT) |
15102 (0x6 << DMA_RWCTRL_PCI_READ_CMD_SHIFT));
15103
David S. Miller59e6b432005-05-18 22:50:10 -070015104 tp->dma_rwctrl = tg3_calc_dma_bndry(tp, tp->dma_rwctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015105
Joe Perches63c3a662011-04-26 08:12:10 +000015106 if (tg3_flag(tp, 57765_PLUS))
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000015107 goto out;
15108
Joe Perches63c3a662011-04-26 08:12:10 +000015109 if (tg3_flag(tp, PCI_EXPRESS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070015110 /* DMA read watermark not used on PCIE */
15111 tp->dma_rwctrl |= 0x00180000;
Joe Perches63c3a662011-04-26 08:12:10 +000015112 } else if (!tg3_flag(tp, PCIX_MODE)) {
Michael Chan85e94ce2005-04-21 17:05:28 -070015113 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 ||
15114 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015115 tp->dma_rwctrl |= 0x003f0000;
15116 else
15117 tp->dma_rwctrl |= 0x003f000f;
15118 } else {
15119 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
15120 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
15121 u32 ccval = (tr32(TG3PCI_CLOCK_CTRL) & 0x1f);
Michael Chan49afdeb2007-02-13 12:17:03 -080015122 u32 read_water = 0x7;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015123
Michael Chan4a29cc22006-03-19 13:21:12 -080015124 /* If the 5704 is behind the EPB bridge, we can
15125 * do the less restrictive ONE_DMA workaround for
15126 * better performance.
15127 */
Joe Perches63c3a662011-04-26 08:12:10 +000015128 if (tg3_flag(tp, 40BIT_DMA_BUG) &&
Michael Chan4a29cc22006-03-19 13:21:12 -080015129 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704)
15130 tp->dma_rwctrl |= 0x8000;
15131 else if (ccval == 0x6 || ccval == 0x7)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015132 tp->dma_rwctrl |= DMA_RWCTRL_ONE_DMA;
15133
Michael Chan49afdeb2007-02-13 12:17:03 -080015134 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703)
15135 read_water = 4;
David S. Miller59e6b432005-05-18 22:50:10 -070015136 /* Set bit 23 to enable PCIX hw bug fix */
Michael Chan49afdeb2007-02-13 12:17:03 -080015137 tp->dma_rwctrl |=
15138 (read_water << DMA_RWCTRL_READ_WATER_SHIFT) |
15139 (0x3 << DMA_RWCTRL_WRITE_WATER_SHIFT) |
15140 (1 << 23);
Michael Chan4cf78e42005-07-25 12:29:19 -070015141 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780) {
15142 /* 5780 always in PCIX mode */
15143 tp->dma_rwctrl |= 0x00144000;
Michael Chana4e2b342005-10-26 15:46:52 -070015144 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
15145 /* 5714 always in PCIX mode */
15146 tp->dma_rwctrl |= 0x00148000;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015147 } else {
15148 tp->dma_rwctrl |= 0x001b000f;
15149 }
15150 }
15151
15152 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
15153 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704)
15154 tp->dma_rwctrl &= 0xfffffff0;
15155
15156 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
15157 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
15158 /* Remove this if it causes problems for some boards. */
15159 tp->dma_rwctrl |= DMA_RWCTRL_USE_MEM_READ_MULT;
15160
15161 /* On 5700/5701 chips, we need to set this bit.
15162 * Otherwise the chip will issue cacheline transactions
15163 * to streamable DMA memory with not all the byte
15164 * enables turned on. This is an error on several
15165 * RISC PCI controllers, in particular sparc64.
15166 *
15167 * On 5703/5704 chips, this bit has been reassigned
15168 * a different meaning. In particular, it is used
15169 * on those chips to enable a PCI-X workaround.
15170 */
15171 tp->dma_rwctrl |= DMA_RWCTRL_ASSERT_ALL_BE;
15172 }
15173
15174 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
15175
15176#if 0
15177 /* Unneeded, already done by tg3_get_invariants. */
15178 tg3_switch_clocks(tp);
15179#endif
15180
Linus Torvalds1da177e2005-04-16 15:20:36 -070015181 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
15182 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701)
15183 goto out;
15184
David S. Miller59e6b432005-05-18 22:50:10 -070015185 /* It is best to perform DMA test with maximum write burst size
15186 * to expose the 5700/5701 write DMA bug.
15187 */
15188 saved_dma_rwctrl = tp->dma_rwctrl;
15189 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
15190 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
15191
Linus Torvalds1da177e2005-04-16 15:20:36 -070015192 while (1) {
15193 u32 *p = buf, i;
15194
15195 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++)
15196 p[i] = i;
15197
15198 /* Send the buffer to the chip. */
15199 ret = tg3_do_test_dma(tp, buf, buf_dma, TEST_BUFFER_SIZE, 1);
15200 if (ret) {
Matt Carlson2445e462010-04-05 10:19:21 +000015201 dev_err(&tp->pdev->dev,
15202 "%s: Buffer write failed. err = %d\n",
15203 __func__, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015204 break;
15205 }
15206
15207#if 0
15208 /* validate data reached card RAM correctly. */
15209 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++) {
15210 u32 val;
15211 tg3_read_mem(tp, 0x2100 + (i*4), &val);
15212 if (le32_to_cpu(val) != p[i]) {
Matt Carlson2445e462010-04-05 10:19:21 +000015213 dev_err(&tp->pdev->dev,
15214 "%s: Buffer corrupted on device! "
15215 "(%d != %d)\n", __func__, val, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015216 /* ret = -ENODEV here? */
15217 }
15218 p[i] = 0;
15219 }
15220#endif
15221 /* Now read it back. */
15222 ret = tg3_do_test_dma(tp, buf, buf_dma, TEST_BUFFER_SIZE, 0);
15223 if (ret) {
Matt Carlson5129c3a2010-04-05 10:19:23 +000015224 dev_err(&tp->pdev->dev, "%s: Buffer read failed. "
15225 "err = %d\n", __func__, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015226 break;
15227 }
15228
15229 /* Verify it. */
15230 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++) {
15231 if (p[i] == i)
15232 continue;
15233
David S. Miller59e6b432005-05-18 22:50:10 -070015234 if ((tp->dma_rwctrl & DMA_RWCTRL_WRITE_BNDRY_MASK) !=
15235 DMA_RWCTRL_WRITE_BNDRY_16) {
15236 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015237 tp->dma_rwctrl |= DMA_RWCTRL_WRITE_BNDRY_16;
15238 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
15239 break;
15240 } else {
Matt Carlson2445e462010-04-05 10:19:21 +000015241 dev_err(&tp->pdev->dev,
15242 "%s: Buffer corrupted on read back! "
15243 "(%d != %d)\n", __func__, p[i], i);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015244 ret = -ENODEV;
15245 goto out;
15246 }
15247 }
15248
15249 if (i == (TEST_BUFFER_SIZE / sizeof(u32))) {
15250 /* Success. */
15251 ret = 0;
15252 break;
15253 }
15254 }
David S. Miller59e6b432005-05-18 22:50:10 -070015255 if ((tp->dma_rwctrl & DMA_RWCTRL_WRITE_BNDRY_MASK) !=
15256 DMA_RWCTRL_WRITE_BNDRY_16) {
15257 /* DMA test passed without adjusting DMA boundary,
Michael Chan6d1cfba2005-06-08 14:13:14 -070015258 * now look for chipsets that are known to expose the
15259 * DMA bug without failing the test.
David S. Miller59e6b432005-05-18 22:50:10 -070015260 */
Matt Carlson41434702011-03-09 16:58:22 +000015261 if (pci_dev_present(tg3_dma_wait_state_chipsets)) {
Michael Chan6d1cfba2005-06-08 14:13:14 -070015262 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
15263 tp->dma_rwctrl |= DMA_RWCTRL_WRITE_BNDRY_16;
Matt Carlson859a588792010-04-05 10:19:28 +000015264 } else {
Michael Chan6d1cfba2005-06-08 14:13:14 -070015265 /* Safe to use the calculated DMA boundary. */
15266 tp->dma_rwctrl = saved_dma_rwctrl;
Matt Carlson859a588792010-04-05 10:19:28 +000015267 }
Michael Chan6d1cfba2005-06-08 14:13:14 -070015268
David S. Miller59e6b432005-05-18 22:50:10 -070015269 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
15270 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015271
15272out:
Matt Carlson4bae65c2010-11-24 08:31:52 +000015273 dma_free_coherent(&tp->pdev->dev, TEST_BUFFER_SIZE, buf, buf_dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015274out_nofree:
15275 return ret;
15276}
15277
Linus Torvalds1da177e2005-04-16 15:20:36 -070015278static void __devinit tg3_init_bufmgr_config(struct tg3 *tp)
15279{
Joe Perches63c3a662011-04-26 08:12:10 +000015280 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlson666bc832010-01-20 16:58:03 +000015281 tp->bufmgr_config.mbuf_read_dma_low_water =
15282 DEFAULT_MB_RDMA_LOW_WATER_5705;
15283 tp->bufmgr_config.mbuf_mac_rx_low_water =
15284 DEFAULT_MB_MACRX_LOW_WATER_57765;
15285 tp->bufmgr_config.mbuf_high_water =
15286 DEFAULT_MB_HIGH_WATER_57765;
15287
15288 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
15289 DEFAULT_MB_RDMA_LOW_WATER_5705;
15290 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
15291 DEFAULT_MB_MACRX_LOW_WATER_JUMBO_57765;
15292 tp->bufmgr_config.mbuf_high_water_jumbo =
15293 DEFAULT_MB_HIGH_WATER_JUMBO_57765;
Joe Perches63c3a662011-04-26 08:12:10 +000015294 } else if (tg3_flag(tp, 5705_PLUS)) {
Michael Chanfdfec1722005-07-25 12:31:48 -070015295 tp->bufmgr_config.mbuf_read_dma_low_water =
15296 DEFAULT_MB_RDMA_LOW_WATER_5705;
15297 tp->bufmgr_config.mbuf_mac_rx_low_water =
15298 DEFAULT_MB_MACRX_LOW_WATER_5705;
15299 tp->bufmgr_config.mbuf_high_water =
15300 DEFAULT_MB_HIGH_WATER_5705;
Michael Chanb5d37722006-09-27 16:06:21 -070015301 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
15302 tp->bufmgr_config.mbuf_mac_rx_low_water =
15303 DEFAULT_MB_MACRX_LOW_WATER_5906;
15304 tp->bufmgr_config.mbuf_high_water =
15305 DEFAULT_MB_HIGH_WATER_5906;
15306 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015307
Michael Chanfdfec1722005-07-25 12:31:48 -070015308 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
15309 DEFAULT_MB_RDMA_LOW_WATER_JUMBO_5780;
15310 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
15311 DEFAULT_MB_MACRX_LOW_WATER_JUMBO_5780;
15312 tp->bufmgr_config.mbuf_high_water_jumbo =
15313 DEFAULT_MB_HIGH_WATER_JUMBO_5780;
15314 } else {
15315 tp->bufmgr_config.mbuf_read_dma_low_water =
15316 DEFAULT_MB_RDMA_LOW_WATER;
15317 tp->bufmgr_config.mbuf_mac_rx_low_water =
15318 DEFAULT_MB_MACRX_LOW_WATER;
15319 tp->bufmgr_config.mbuf_high_water =
15320 DEFAULT_MB_HIGH_WATER;
15321
15322 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
15323 DEFAULT_MB_RDMA_LOW_WATER_JUMBO;
15324 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
15325 DEFAULT_MB_MACRX_LOW_WATER_JUMBO;
15326 tp->bufmgr_config.mbuf_high_water_jumbo =
15327 DEFAULT_MB_HIGH_WATER_JUMBO;
15328 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015329
15330 tp->bufmgr_config.dma_low_water = DEFAULT_DMA_LOW_WATER;
15331 tp->bufmgr_config.dma_high_water = DEFAULT_DMA_HIGH_WATER;
15332}
15333
15334static char * __devinit tg3_phy_string(struct tg3 *tp)
15335{
Matt Carlson79eb6902010-02-17 15:17:03 +000015336 switch (tp->phy_id & TG3_PHY_ID_MASK) {
15337 case TG3_PHY_ID_BCM5400: return "5400";
15338 case TG3_PHY_ID_BCM5401: return "5401";
15339 case TG3_PHY_ID_BCM5411: return "5411";
15340 case TG3_PHY_ID_BCM5701: return "5701";
15341 case TG3_PHY_ID_BCM5703: return "5703";
15342 case TG3_PHY_ID_BCM5704: return "5704";
15343 case TG3_PHY_ID_BCM5705: return "5705";
15344 case TG3_PHY_ID_BCM5750: return "5750";
15345 case TG3_PHY_ID_BCM5752: return "5752";
15346 case TG3_PHY_ID_BCM5714: return "5714";
15347 case TG3_PHY_ID_BCM5780: return "5780";
15348 case TG3_PHY_ID_BCM5755: return "5755";
15349 case TG3_PHY_ID_BCM5787: return "5787";
15350 case TG3_PHY_ID_BCM5784: return "5784";
15351 case TG3_PHY_ID_BCM5756: return "5722/5756";
15352 case TG3_PHY_ID_BCM5906: return "5906";
15353 case TG3_PHY_ID_BCM5761: return "5761";
15354 case TG3_PHY_ID_BCM5718C: return "5718C";
15355 case TG3_PHY_ID_BCM5718S: return "5718S";
15356 case TG3_PHY_ID_BCM57765: return "57765";
Matt Carlson302b5002010-06-05 17:24:38 +000015357 case TG3_PHY_ID_BCM5719C: return "5719C";
Matt Carlson6418f2c2011-04-05 14:22:49 +000015358 case TG3_PHY_ID_BCM5720C: return "5720C";
Matt Carlson79eb6902010-02-17 15:17:03 +000015359 case TG3_PHY_ID_BCM8002: return "8002/serdes";
Linus Torvalds1da177e2005-04-16 15:20:36 -070015360 case 0: return "serdes";
15361 default: return "unknown";
Stephen Hemminger855e1112008-04-16 16:37:28 -070015362 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015363}
15364
Michael Chanf9804dd2005-09-27 12:13:10 -070015365static char * __devinit tg3_bus_string(struct tg3 *tp, char *str)
15366{
Joe Perches63c3a662011-04-26 08:12:10 +000015367 if (tg3_flag(tp, PCI_EXPRESS)) {
Michael Chanf9804dd2005-09-27 12:13:10 -070015368 strcpy(str, "PCI Express");
15369 return str;
Joe Perches63c3a662011-04-26 08:12:10 +000015370 } else if (tg3_flag(tp, PCIX_MODE)) {
Michael Chanf9804dd2005-09-27 12:13:10 -070015371 u32 clock_ctrl = tr32(TG3PCI_CLOCK_CTRL) & 0x1f;
15372
15373 strcpy(str, "PCIX:");
15374
15375 if ((clock_ctrl == 7) ||
15376 ((tr32(GRC_MISC_CFG) & GRC_MISC_CFG_BOARD_ID_MASK) ==
15377 GRC_MISC_CFG_BOARD_ID_5704CIOBE))
15378 strcat(str, "133MHz");
15379 else if (clock_ctrl == 0)
15380 strcat(str, "33MHz");
15381 else if (clock_ctrl == 2)
15382 strcat(str, "50MHz");
15383 else if (clock_ctrl == 4)
15384 strcat(str, "66MHz");
15385 else if (clock_ctrl == 6)
15386 strcat(str, "100MHz");
Michael Chanf9804dd2005-09-27 12:13:10 -070015387 } else {
15388 strcpy(str, "PCI:");
Joe Perches63c3a662011-04-26 08:12:10 +000015389 if (tg3_flag(tp, PCI_HIGH_SPEED))
Michael Chanf9804dd2005-09-27 12:13:10 -070015390 strcat(str, "66MHz");
15391 else
15392 strcat(str, "33MHz");
15393 }
Joe Perches63c3a662011-04-26 08:12:10 +000015394 if (tg3_flag(tp, PCI_32BIT))
Michael Chanf9804dd2005-09-27 12:13:10 -070015395 strcat(str, ":32-bit");
15396 else
15397 strcat(str, ":64-bit");
15398 return str;
15399}
15400
David S. Miller15f98502005-05-18 22:49:26 -070015401static void __devinit tg3_init_coal(struct tg3 *tp)
15402{
15403 struct ethtool_coalesce *ec = &tp->coal;
15404
15405 memset(ec, 0, sizeof(*ec));
15406 ec->cmd = ETHTOOL_GCOALESCE;
15407 ec->rx_coalesce_usecs = LOW_RXCOL_TICKS;
15408 ec->tx_coalesce_usecs = LOW_TXCOL_TICKS;
15409 ec->rx_max_coalesced_frames = LOW_RXMAX_FRAMES;
15410 ec->tx_max_coalesced_frames = LOW_TXMAX_FRAMES;
15411 ec->rx_coalesce_usecs_irq = DEFAULT_RXCOAL_TICK_INT;
15412 ec->tx_coalesce_usecs_irq = DEFAULT_TXCOAL_TICK_INT;
15413 ec->rx_max_coalesced_frames_irq = DEFAULT_RXCOAL_MAXF_INT;
15414 ec->tx_max_coalesced_frames_irq = DEFAULT_TXCOAL_MAXF_INT;
15415 ec->stats_block_coalesce_usecs = DEFAULT_STAT_COAL_TICKS;
15416
15417 if (tp->coalesce_mode & (HOSTCC_MODE_CLRTICK_RXBD |
15418 HOSTCC_MODE_CLRTICK_TXBD)) {
15419 ec->rx_coalesce_usecs = LOW_RXCOL_TICKS_CLRTCKS;
15420 ec->rx_coalesce_usecs_irq = DEFAULT_RXCOAL_TICK_INT_CLRTCKS;
15421 ec->tx_coalesce_usecs = LOW_TXCOL_TICKS_CLRTCKS;
15422 ec->tx_coalesce_usecs_irq = DEFAULT_TXCOAL_TICK_INT_CLRTCKS;
15423 }
Michael Chand244c892005-07-05 14:42:33 -070015424
Joe Perches63c3a662011-04-26 08:12:10 +000015425 if (tg3_flag(tp, 5705_PLUS)) {
Michael Chand244c892005-07-05 14:42:33 -070015426 ec->rx_coalesce_usecs_irq = 0;
15427 ec->tx_coalesce_usecs_irq = 0;
15428 ec->stats_block_coalesce_usecs = 0;
15429 }
David S. Miller15f98502005-05-18 22:49:26 -070015430}
15431
Linus Torvalds1da177e2005-04-16 15:20:36 -070015432static int __devinit tg3_init_one(struct pci_dev *pdev,
15433 const struct pci_device_id *ent)
15434{
Linus Torvalds1da177e2005-04-16 15:20:36 -070015435 struct net_device *dev;
15436 struct tg3 *tp;
Matt Carlson646c9ed2009-09-01 12:58:41 +000015437 int i, err, pm_cap;
15438 u32 sndmbx, rcvmbx, intmbx;
Michael Chanf9804dd2005-09-27 12:13:10 -070015439 char str[40];
Michael Chan72f2afb2006-03-06 19:28:35 -080015440 u64 dma_mask, persist_dma_mask;
Michał Mirosławc8f44af2011-11-15 15:29:55 +000015441 netdev_features_t features = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015442
Joe Perches05dbe002010-02-17 19:44:19 +000015443 printk_once(KERN_INFO "%s\n", version);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015444
15445 err = pci_enable_device(pdev);
15446 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000015447 dev_err(&pdev->dev, "Cannot enable PCI device, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070015448 return err;
15449 }
15450
Linus Torvalds1da177e2005-04-16 15:20:36 -070015451 err = pci_request_regions(pdev, DRV_MODULE_NAME);
15452 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000015453 dev_err(&pdev->dev, "Cannot obtain PCI resources, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070015454 goto err_out_disable_pdev;
15455 }
15456
15457 pci_set_master(pdev);
15458
15459 /* Find power-management capability. */
15460 pm_cap = pci_find_capability(pdev, PCI_CAP_ID_PM);
15461 if (pm_cap == 0) {
Matt Carlson2445e462010-04-05 10:19:21 +000015462 dev_err(&pdev->dev,
15463 "Cannot find Power Management capability, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070015464 err = -EIO;
15465 goto err_out_free_res;
15466 }
15467
Matt Carlson16821282011-07-13 09:27:28 +000015468 err = pci_set_power_state(pdev, PCI_D0);
15469 if (err) {
15470 dev_err(&pdev->dev, "Transition to D0 failed, aborting\n");
15471 goto err_out_free_res;
15472 }
15473
Matt Carlsonfe5f5782009-09-01 13:09:39 +000015474 dev = alloc_etherdev_mq(sizeof(*tp), TG3_IRQ_MAX_VECS);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015475 if (!dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070015476 err = -ENOMEM;
Matt Carlson16821282011-07-13 09:27:28 +000015477 goto err_out_power_down;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015478 }
15479
Linus Torvalds1da177e2005-04-16 15:20:36 -070015480 SET_NETDEV_DEV(dev, &pdev->dev);
15481
Linus Torvalds1da177e2005-04-16 15:20:36 -070015482 tp = netdev_priv(dev);
15483 tp->pdev = pdev;
15484 tp->dev = dev;
15485 tp->pm_cap = pm_cap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015486 tp->rx_mode = TG3_DEF_RX_MODE;
15487 tp->tx_mode = TG3_DEF_TX_MODE;
Matt Carlson8ef21422008-05-02 16:47:53 -070015488
Linus Torvalds1da177e2005-04-16 15:20:36 -070015489 if (tg3_debug > 0)
15490 tp->msg_enable = tg3_debug;
15491 else
15492 tp->msg_enable = TG3_DEF_MSG_ENABLE;
15493
15494 /* The word/byte swap controls here control register access byte
15495 * swapping. DMA data byte swapping is controlled in the GRC_MODE
15496 * setting below.
15497 */
15498 tp->misc_host_ctrl =
15499 MISC_HOST_CTRL_MASK_PCI_INT |
15500 MISC_HOST_CTRL_WORD_SWAP |
15501 MISC_HOST_CTRL_INDIR_ACCESS |
15502 MISC_HOST_CTRL_PCISTATE_RW;
15503
15504 /* The NONFRM (non-frame) byte/word swap controls take effect
15505 * on descriptor entries, anything which isn't packet data.
15506 *
15507 * The StrongARM chips on the board (one for tx, one for rx)
15508 * are running in big-endian mode.
15509 */
15510 tp->grc_mode = (GRC_MODE_WSWAP_DATA | GRC_MODE_BSWAP_DATA |
15511 GRC_MODE_WSWAP_NONFRM_DATA);
15512#ifdef __BIG_ENDIAN
15513 tp->grc_mode |= GRC_MODE_BSWAP_NONFRM_DATA;
15514#endif
15515 spin_lock_init(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015516 spin_lock_init(&tp->indirect_lock);
David Howellsc4028952006-11-22 14:57:56 +000015517 INIT_WORK(&tp->reset_task, tg3_reset_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015518
Matt Carlsond5fe4882008-11-21 17:20:32 -080015519 tp->regs = pci_ioremap_bar(pdev, BAR_0);
Andy Gospodarekab0049b2007-09-06 20:42:14 +010015520 if (!tp->regs) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015521 dev_err(&pdev->dev, "Cannot map device registers, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070015522 err = -ENOMEM;
15523 goto err_out_free_dev;
15524 }
15525
Matt Carlsonc9cab242011-07-13 09:27:27 +000015526 if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761 ||
15527 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761E ||
15528 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761S ||
15529 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761SE ||
15530 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717 ||
15531 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718 ||
15532 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5719 ||
15533 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5720) {
15534 tg3_flag_set(tp, ENABLE_APE);
15535 tp->aperegs = pci_ioremap_bar(pdev, BAR_2);
15536 if (!tp->aperegs) {
15537 dev_err(&pdev->dev,
15538 "Cannot map APE registers, aborting\n");
15539 err = -ENOMEM;
15540 goto err_out_iounmap;
15541 }
15542 }
15543
Linus Torvalds1da177e2005-04-16 15:20:36 -070015544 tp->rx_pending = TG3_DEF_RX_RING_PENDING;
15545 tp->rx_jumbo_pending = TG3_DEF_RX_JUMBO_RING_PENDING;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015546
Linus Torvalds1da177e2005-04-16 15:20:36 -070015547 dev->ethtool_ops = &tg3_ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015548 dev->watchdog_timeo = TG3_TX_TIMEOUT;
Matt Carlson2ffcc982011-05-19 12:12:44 +000015549 dev->netdev_ops = &tg3_netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015550 dev->irq = pdev->irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015551
15552 err = tg3_get_invariants(tp);
15553 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015554 dev_err(&pdev->dev,
15555 "Problem fetching invariants of chip, aborting\n");
Matt Carlsonc9cab242011-07-13 09:27:27 +000015556 goto err_out_apeunmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015557 }
15558
Michael Chan4a29cc22006-03-19 13:21:12 -080015559 /* The EPB bridge inside 5714, 5715, and 5780 and any
15560 * device behind the EPB cannot support DMA addresses > 40-bit.
Michael Chan72f2afb2006-03-06 19:28:35 -080015561 * On 64-bit systems with IOMMU, use 40-bit dma_mask.
15562 * On 64-bit systems without IOMMU, use 64-bit dma_mask and
15563 * do DMA address check in tg3_start_xmit().
15564 */
Joe Perches63c3a662011-04-26 08:12:10 +000015565 if (tg3_flag(tp, IS_5788))
Yang Hongyang284901a2009-04-06 19:01:15 -070015566 persist_dma_mask = dma_mask = DMA_BIT_MASK(32);
Joe Perches63c3a662011-04-26 08:12:10 +000015567 else if (tg3_flag(tp, 40BIT_DMA_BUG)) {
Yang Hongyang50cf1562009-04-06 19:01:14 -070015568 persist_dma_mask = dma_mask = DMA_BIT_MASK(40);
Michael Chan72f2afb2006-03-06 19:28:35 -080015569#ifdef CONFIG_HIGHMEM
Yang Hongyang6a355282009-04-06 19:01:13 -070015570 dma_mask = DMA_BIT_MASK(64);
Michael Chan72f2afb2006-03-06 19:28:35 -080015571#endif
Michael Chan4a29cc22006-03-19 13:21:12 -080015572 } else
Yang Hongyang6a355282009-04-06 19:01:13 -070015573 persist_dma_mask = dma_mask = DMA_BIT_MASK(64);
Michael Chan72f2afb2006-03-06 19:28:35 -080015574
15575 /* Configure DMA attributes. */
Yang Hongyang284901a2009-04-06 19:01:15 -070015576 if (dma_mask > DMA_BIT_MASK(32)) {
Michael Chan72f2afb2006-03-06 19:28:35 -080015577 err = pci_set_dma_mask(pdev, dma_mask);
15578 if (!err) {
Matt Carlson0da06062011-05-19 12:12:53 +000015579 features |= NETIF_F_HIGHDMA;
Michael Chan72f2afb2006-03-06 19:28:35 -080015580 err = pci_set_consistent_dma_mask(pdev,
15581 persist_dma_mask);
15582 if (err < 0) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015583 dev_err(&pdev->dev, "Unable to obtain 64 bit "
15584 "DMA for consistent allocations\n");
Matt Carlsonc9cab242011-07-13 09:27:27 +000015585 goto err_out_apeunmap;
Michael Chan72f2afb2006-03-06 19:28:35 -080015586 }
15587 }
15588 }
Yang Hongyang284901a2009-04-06 19:01:15 -070015589 if (err || dma_mask == DMA_BIT_MASK(32)) {
15590 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
Michael Chan72f2afb2006-03-06 19:28:35 -080015591 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015592 dev_err(&pdev->dev,
15593 "No usable DMA configuration, aborting\n");
Matt Carlsonc9cab242011-07-13 09:27:27 +000015594 goto err_out_apeunmap;
Michael Chan72f2afb2006-03-06 19:28:35 -080015595 }
15596 }
15597
Michael Chanfdfec1722005-07-25 12:31:48 -070015598 tg3_init_bufmgr_config(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015599
Matt Carlson0da06062011-05-19 12:12:53 +000015600 features |= NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
15601
15602 /* 5700 B0 chips do not support checksumming correctly due
15603 * to hardware bugs.
15604 */
15605 if (tp->pci_chip_rev_id != CHIPREV_ID_5700_B0) {
15606 features |= NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_RXCSUM;
15607
15608 if (tg3_flag(tp, 5755_PLUS))
15609 features |= NETIF_F_IPV6_CSUM;
15610 }
15611
Michael Chan4e3a7aa2006-03-20 17:47:44 -080015612 /* TSO is on by default on chips that support hardware TSO.
15613 * Firmware TSO on older chips gives lower performance, so it
15614 * is off by default, but can be enabled using ethtool.
15615 */
Joe Perches63c3a662011-04-26 08:12:10 +000015616 if ((tg3_flag(tp, HW_TSO_1) ||
15617 tg3_flag(tp, HW_TSO_2) ||
15618 tg3_flag(tp, HW_TSO_3)) &&
Matt Carlson0da06062011-05-19 12:12:53 +000015619 (features & NETIF_F_IP_CSUM))
15620 features |= NETIF_F_TSO;
Joe Perches63c3a662011-04-26 08:12:10 +000015621 if (tg3_flag(tp, HW_TSO_2) || tg3_flag(tp, HW_TSO_3)) {
Matt Carlson0da06062011-05-19 12:12:53 +000015622 if (features & NETIF_F_IPV6_CSUM)
15623 features |= NETIF_F_TSO6;
Joe Perches63c3a662011-04-26 08:12:10 +000015624 if (tg3_flag(tp, HW_TSO_3) ||
Matt Carlsone849cdc2009-11-13 13:03:38 +000015625 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson57e69832008-05-25 23:48:31 -070015626 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
15627 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX) ||
Joe Perches63c3a662011-04-26 08:12:10 +000015628 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Michał Mirosławdc668912011-04-07 03:35:07 +000015629 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
Matt Carlson0da06062011-05-19 12:12:53 +000015630 features |= NETIF_F_TSO_ECN;
Michael Chanb0026622006-07-03 19:42:14 -070015631 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015632
Matt Carlsond542fe22011-05-19 16:02:43 +000015633 dev->features |= features;
15634 dev->vlan_features |= features;
15635
Mahesh Bandewar06c03c02011-05-08 06:51:48 +000015636 /*
15637 * Add loopback capability only for a subset of devices that support
15638 * MAC-LOOPBACK. Eventually this need to be enhanced to allow INT-PHY
15639 * loopback for the remaining devices.
15640 */
15641 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5780 &&
15642 !tg3_flag(tp, CPMU_PRESENT))
15643 /* Add the loopback capability */
Matt Carlson0da06062011-05-19 12:12:53 +000015644 features |= NETIF_F_LOOPBACK;
15645
Matt Carlson0da06062011-05-19 12:12:53 +000015646 dev->hw_features |= features;
Mahesh Bandewar06c03c02011-05-08 06:51:48 +000015647
Linus Torvalds1da177e2005-04-16 15:20:36 -070015648 if (tp->pci_chip_rev_id == CHIPREV_ID_5705_A1 &&
Joe Perches63c3a662011-04-26 08:12:10 +000015649 !tg3_flag(tp, TSO_CAPABLE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070015650 !(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH)) {
Joe Perches63c3a662011-04-26 08:12:10 +000015651 tg3_flag_set(tp, MAX_RXPEND_64);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015652 tp->rx_pending = 63;
15653 }
15654
Linus Torvalds1da177e2005-04-16 15:20:36 -070015655 err = tg3_get_device_address(tp);
15656 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015657 dev_err(&pdev->dev,
15658 "Could not obtain valid ethernet address, aborting\n");
Matt Carlsonc9cab242011-07-13 09:27:27 +000015659 goto err_out_apeunmap;
Matt Carlson0d3031d2007-10-10 18:02:43 -070015660 }
15661
Matt Carlsonc88864d2007-11-12 21:07:01 -080015662 /*
15663 * Reset chip in case UNDI or EFI driver did not shutdown
15664 * DMA self test will enable WDMAC and we'll see (spurious)
15665 * pending DMA on the PCI bus at that point.
15666 */
15667 if ((tr32(HOSTCC_MODE) & HOSTCC_MODE_ENABLE) ||
15668 (tr32(WDMAC_MODE) & WDMAC_MODE_ENABLE)) {
15669 tw32(MEMARB_MODE, MEMARB_MODE_ENABLE);
15670 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
15671 }
15672
15673 err = tg3_test_dma(tp);
15674 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015675 dev_err(&pdev->dev, "DMA engine test failed, aborting\n");
Matt Carlsonc88864d2007-11-12 21:07:01 -080015676 goto err_out_apeunmap;
15677 }
15678
Matt Carlson78f90dc2009-11-13 13:03:42 +000015679 intmbx = MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW;
15680 rcvmbx = MAILBOX_RCVRET_CON_IDX_0 + TG3_64BIT_REG_LOW;
15681 sndmbx = MAILBOX_SNDHOST_PROD_IDX_0 + TG3_64BIT_REG_LOW;
Matt Carlson6fd45cb2010-09-15 08:59:57 +000015682 for (i = 0; i < tp->irq_max; i++) {
Matt Carlson78f90dc2009-11-13 13:03:42 +000015683 struct tg3_napi *tnapi = &tp->napi[i];
15684
15685 tnapi->tp = tp;
15686 tnapi->tx_pending = TG3_DEF_TX_RING_PENDING;
15687
15688 tnapi->int_mbox = intmbx;
Matt Carlson93a700a2011-08-31 11:44:54 +000015689 if (i <= 4)
Matt Carlson78f90dc2009-11-13 13:03:42 +000015690 intmbx += 0x8;
15691 else
15692 intmbx += 0x4;
15693
15694 tnapi->consmbox = rcvmbx;
15695 tnapi->prodmbox = sndmbx;
15696
Matt Carlson66cfd1b2010-09-30 10:34:30 +000015697 if (i)
Matt Carlson78f90dc2009-11-13 13:03:42 +000015698 tnapi->coal_now = HOSTCC_MODE_COAL_VEC1_NOW << (i - 1);
Matt Carlson66cfd1b2010-09-30 10:34:30 +000015699 else
Matt Carlson78f90dc2009-11-13 13:03:42 +000015700 tnapi->coal_now = HOSTCC_MODE_NOW;
Matt Carlson78f90dc2009-11-13 13:03:42 +000015701
Joe Perches63c3a662011-04-26 08:12:10 +000015702 if (!tg3_flag(tp, SUPPORT_MSIX))
Matt Carlson78f90dc2009-11-13 13:03:42 +000015703 break;
15704
15705 /*
15706 * If we support MSIX, we'll be using RSS. If we're using
15707 * RSS, the first vector only handles link interrupts and the
15708 * remaining vectors handle rx and tx interrupts. Reuse the
15709 * mailbox values for the next iteration. The values we setup
15710 * above are still useful for the single vectored mode.
15711 */
15712 if (!i)
15713 continue;
15714
15715 rcvmbx += 0x8;
15716
15717 if (sndmbx & 0x4)
15718 sndmbx -= 0x4;
15719 else
15720 sndmbx += 0xc;
15721 }
15722
Matt Carlsonc88864d2007-11-12 21:07:01 -080015723 tg3_init_coal(tp);
15724
Michael Chanc49a1562006-12-17 17:07:29 -080015725 pci_set_drvdata(pdev, dev);
15726
Matt Carlsoncd0d7222011-07-13 09:27:33 +000015727 if (tg3_flag(tp, 5717_PLUS)) {
15728 /* Resume a low-power mode */
15729 tg3_frob_aux_power(tp, false);
15730 }
15731
Linus Torvalds1da177e2005-04-16 15:20:36 -070015732 err = register_netdev(dev);
15733 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015734 dev_err(&pdev->dev, "Cannot register net device, aborting\n");
Matt Carlson0d3031d2007-10-10 18:02:43 -070015735 goto err_out_apeunmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015736 }
15737
Joe Perches05dbe002010-02-17 19:44:19 +000015738 netdev_info(dev, "Tigon3 [partno(%s) rev %04x] (%s) MAC address %pM\n",
15739 tp->board_part_number,
15740 tp->pci_chip_rev_id,
15741 tg3_bus_string(tp, str),
15742 dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015743
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015744 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000015745 struct phy_device *phydev;
15746 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlson5129c3a2010-04-05 10:19:23 +000015747 netdev_info(dev,
15748 "attached PHY driver [%s] (mii_bus:phy_addr=%s)\n",
Joe Perches05dbe002010-02-17 19:44:19 +000015749 phydev->drv->name, dev_name(&phydev->dev));
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015750 } else {
15751 char *ethtype;
15752
15753 if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
15754 ethtype = "10/100Base-TX";
15755 else if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
15756 ethtype = "1000Base-SX";
15757 else
15758 ethtype = "10/100/1000Base-T";
15759
Matt Carlson5129c3a2010-04-05 10:19:23 +000015760 netdev_info(dev, "attached PHY is %s (%s Ethernet) "
Matt Carlson47007832011-04-20 07:57:43 +000015761 "(WireSpeed[%d], EEE[%d])\n",
15762 tg3_phy_string(tp), ethtype,
15763 (tp->phy_flags & TG3_PHYFLG_NO_ETH_WIRE_SPEED) == 0,
15764 (tp->phy_flags & TG3_PHYFLG_EEE_CAP) != 0);
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015765 }
Matt Carlsondf59c942008-11-03 16:52:56 -080015766
Joe Perches05dbe002010-02-17 19:44:19 +000015767 netdev_info(dev, "RXcsums[%d] LinkChgREG[%d] MIirq[%d] ASF[%d] TSOcap[%d]\n",
Michał Mirosławdc668912011-04-07 03:35:07 +000015768 (dev->features & NETIF_F_RXCSUM) != 0,
Joe Perches63c3a662011-04-26 08:12:10 +000015769 tg3_flag(tp, USE_LINKCHG_REG) != 0,
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015770 (tp->phy_flags & TG3_PHYFLG_USE_MI_INTERRUPT) != 0,
Joe Perches63c3a662011-04-26 08:12:10 +000015771 tg3_flag(tp, ENABLE_ASF) != 0,
15772 tg3_flag(tp, TSO_CAPABLE) != 0);
Joe Perches05dbe002010-02-17 19:44:19 +000015773 netdev_info(dev, "dma_rwctrl[%08x] dma_mask[%d-bit]\n",
15774 tp->dma_rwctrl,
15775 pdev->dma_mask == DMA_BIT_MASK(32) ? 32 :
15776 ((u64)pdev->dma_mask) == DMA_BIT_MASK(40) ? 40 : 64);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015777
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000015778 pci_save_state(pdev);
15779
Linus Torvalds1da177e2005-04-16 15:20:36 -070015780 return 0;
15781
Matt Carlson0d3031d2007-10-10 18:02:43 -070015782err_out_apeunmap:
15783 if (tp->aperegs) {
15784 iounmap(tp->aperegs);
15785 tp->aperegs = NULL;
15786 }
15787
Linus Torvalds1da177e2005-04-16 15:20:36 -070015788err_out_iounmap:
Michael Chan68929142005-08-09 20:17:14 -070015789 if (tp->regs) {
15790 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070015791 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070015792 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015793
15794err_out_free_dev:
15795 free_netdev(dev);
15796
Matt Carlson16821282011-07-13 09:27:28 +000015797err_out_power_down:
15798 pci_set_power_state(pdev, PCI_D3hot);
15799
Linus Torvalds1da177e2005-04-16 15:20:36 -070015800err_out_free_res:
15801 pci_release_regions(pdev);
15802
15803err_out_disable_pdev:
15804 pci_disable_device(pdev);
15805 pci_set_drvdata(pdev, NULL);
15806 return err;
15807}
15808
15809static void __devexit tg3_remove_one(struct pci_dev *pdev)
15810{
15811 struct net_device *dev = pci_get_drvdata(pdev);
15812
15813 if (dev) {
15814 struct tg3 *tp = netdev_priv(dev);
15815
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -080015816 if (tp->fw)
15817 release_firmware(tp->fw);
15818
Matt Carlsondb219972011-11-04 09:15:03 +000015819 tg3_reset_task_cancel(tp);
Matt Carlson158d7ab2008-05-29 01:37:54 -070015820
David S. Miller1805b2f2011-10-24 18:18:09 -040015821 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015822 tg3_phy_fini(tp);
Matt Carlson158d7ab2008-05-29 01:37:54 -070015823 tg3_mdio_fini(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015824 }
Matt Carlson158d7ab2008-05-29 01:37:54 -070015825
Linus Torvalds1da177e2005-04-16 15:20:36 -070015826 unregister_netdev(dev);
Matt Carlson0d3031d2007-10-10 18:02:43 -070015827 if (tp->aperegs) {
15828 iounmap(tp->aperegs);
15829 tp->aperegs = NULL;
15830 }
Michael Chan68929142005-08-09 20:17:14 -070015831 if (tp->regs) {
15832 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070015833 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070015834 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015835 free_netdev(dev);
15836 pci_release_regions(pdev);
15837 pci_disable_device(pdev);
15838 pci_set_drvdata(pdev, NULL);
15839 }
15840}
15841
Eric Dumazetaa6027c2011-01-01 05:22:46 +000015842#ifdef CONFIG_PM_SLEEP
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015843static int tg3_suspend(struct device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015844{
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015845 struct pci_dev *pdev = to_pci_dev(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015846 struct net_device *dev = pci_get_drvdata(pdev);
15847 struct tg3 *tp = netdev_priv(dev);
15848 int err;
15849
15850 if (!netif_running(dev))
15851 return 0;
15852
Matt Carlsondb219972011-11-04 09:15:03 +000015853 tg3_reset_task_cancel(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015854 tg3_phy_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015855 tg3_netif_stop(tp);
15856
15857 del_timer_sync(&tp->timer);
15858
David S. Millerf47c11e2005-06-24 20:18:35 -070015859 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015860 tg3_disable_ints(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -070015861 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015862
15863 netif_device_detach(dev);
15864
David S. Millerf47c11e2005-06-24 20:18:35 -070015865 tg3_full_lock(tp, 0);
Michael Chan944d9802005-05-29 14:57:48 -070015866 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Joe Perches63c3a662011-04-26 08:12:10 +000015867 tg3_flag_clear(tp, INIT_COMPLETE);
David S. Millerf47c11e2005-06-24 20:18:35 -070015868 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015869
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015870 err = tg3_power_down_prepare(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015871 if (err) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015872 int err2;
15873
David S. Millerf47c11e2005-06-24 20:18:35 -070015874 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015875
Joe Perches63c3a662011-04-26 08:12:10 +000015876 tg3_flag_set(tp, INIT_COMPLETE);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015877 err2 = tg3_restart_hw(tp, 1);
15878 if (err2)
Michael Chanb9ec6c12006-07-25 16:37:27 -070015879 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015880
15881 tp->timer.expires = jiffies + tp->timer_offset;
15882 add_timer(&tp->timer);
15883
15884 netif_device_attach(dev);
15885 tg3_netif_start(tp);
15886
Michael Chanb9ec6c12006-07-25 16:37:27 -070015887out:
David S. Millerf47c11e2005-06-24 20:18:35 -070015888 tg3_full_unlock(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015889
15890 if (!err2)
15891 tg3_phy_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015892 }
15893
15894 return err;
15895}
15896
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015897static int tg3_resume(struct device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015898{
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015899 struct pci_dev *pdev = to_pci_dev(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015900 struct net_device *dev = pci_get_drvdata(pdev);
15901 struct tg3 *tp = netdev_priv(dev);
15902 int err;
15903
15904 if (!netif_running(dev))
15905 return 0;
15906
Linus Torvalds1da177e2005-04-16 15:20:36 -070015907 netif_device_attach(dev);
15908
David S. Millerf47c11e2005-06-24 20:18:35 -070015909 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015910
Joe Perches63c3a662011-04-26 08:12:10 +000015911 tg3_flag_set(tp, INIT_COMPLETE);
Michael Chanb9ec6c12006-07-25 16:37:27 -070015912 err = tg3_restart_hw(tp, 1);
15913 if (err)
15914 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015915
15916 tp->timer.expires = jiffies + tp->timer_offset;
15917 add_timer(&tp->timer);
15918
Linus Torvalds1da177e2005-04-16 15:20:36 -070015919 tg3_netif_start(tp);
15920
Michael Chanb9ec6c12006-07-25 16:37:27 -070015921out:
David S. Millerf47c11e2005-06-24 20:18:35 -070015922 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015923
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015924 if (!err)
15925 tg3_phy_start(tp);
15926
Michael Chanb9ec6c12006-07-25 16:37:27 -070015927 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015928}
15929
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015930static SIMPLE_DEV_PM_OPS(tg3_pm_ops, tg3_suspend, tg3_resume);
Eric Dumazetaa6027c2011-01-01 05:22:46 +000015931#define TG3_PM_OPS (&tg3_pm_ops)
15932
15933#else
15934
15935#define TG3_PM_OPS NULL
15936
15937#endif /* CONFIG_PM_SLEEP */
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015938
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000015939/**
15940 * tg3_io_error_detected - called when PCI error is detected
15941 * @pdev: Pointer to PCI device
15942 * @state: The current pci connection state
15943 *
15944 * This function is called after a PCI bus error affecting
15945 * this device has been detected.
15946 */
15947static pci_ers_result_t tg3_io_error_detected(struct pci_dev *pdev,
15948 pci_channel_state_t state)
15949{
15950 struct net_device *netdev = pci_get_drvdata(pdev);
15951 struct tg3 *tp = netdev_priv(netdev);
15952 pci_ers_result_t err = PCI_ERS_RESULT_NEED_RESET;
15953
15954 netdev_info(netdev, "PCI I/O error detected\n");
15955
15956 rtnl_lock();
15957
15958 if (!netif_running(netdev))
15959 goto done;
15960
15961 tg3_phy_stop(tp);
15962
15963 tg3_netif_stop(tp);
15964
15965 del_timer_sync(&tp->timer);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000015966
15967 /* Want to make sure that the reset task doesn't run */
Matt Carlsondb219972011-11-04 09:15:03 +000015968 tg3_reset_task_cancel(tp);
Joe Perches63c3a662011-04-26 08:12:10 +000015969 tg3_flag_clear(tp, TX_RECOVERY_PENDING);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000015970
15971 netif_device_detach(netdev);
15972
15973 /* Clean up software state, even if MMIO is blocked */
15974 tg3_full_lock(tp, 0);
15975 tg3_halt(tp, RESET_KIND_SHUTDOWN, 0);
15976 tg3_full_unlock(tp);
15977
15978done:
15979 if (state == pci_channel_io_perm_failure)
15980 err = PCI_ERS_RESULT_DISCONNECT;
15981 else
15982 pci_disable_device(pdev);
15983
15984 rtnl_unlock();
15985
15986 return err;
15987}
15988
15989/**
15990 * tg3_io_slot_reset - called after the pci bus has been reset.
15991 * @pdev: Pointer to PCI device
15992 *
15993 * Restart the card from scratch, as if from a cold-boot.
15994 * At this point, the card has exprienced a hard reset,
15995 * followed by fixups by BIOS, and has its config space
15996 * set up identically to what it was at cold boot.
15997 */
15998static pci_ers_result_t tg3_io_slot_reset(struct pci_dev *pdev)
15999{
16000 struct net_device *netdev = pci_get_drvdata(pdev);
16001 struct tg3 *tp = netdev_priv(netdev);
16002 pci_ers_result_t rc = PCI_ERS_RESULT_DISCONNECT;
16003 int err;
16004
16005 rtnl_lock();
16006
16007 if (pci_enable_device(pdev)) {
16008 netdev_err(netdev, "Cannot re-enable PCI device after reset.\n");
16009 goto done;
16010 }
16011
16012 pci_set_master(pdev);
16013 pci_restore_state(pdev);
16014 pci_save_state(pdev);
16015
16016 if (!netif_running(netdev)) {
16017 rc = PCI_ERS_RESULT_RECOVERED;
16018 goto done;
16019 }
16020
16021 err = tg3_power_up(tp);
Matt Carlsonbed98292011-07-13 09:27:29 +000016022 if (err)
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000016023 goto done;
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000016024
16025 rc = PCI_ERS_RESULT_RECOVERED;
16026
16027done:
16028 rtnl_unlock();
16029
16030 return rc;
16031}
16032
16033/**
16034 * tg3_io_resume - called when traffic can start flowing again.
16035 * @pdev: Pointer to PCI device
16036 *
16037 * This callback is called when the error recovery driver tells
16038 * us that its OK to resume normal operation.
16039 */
16040static void tg3_io_resume(struct pci_dev *pdev)
16041{
16042 struct net_device *netdev = pci_get_drvdata(pdev);
16043 struct tg3 *tp = netdev_priv(netdev);
16044 int err;
16045
16046 rtnl_lock();
16047
16048 if (!netif_running(netdev))
16049 goto done;
16050
16051 tg3_full_lock(tp, 0);
Joe Perches63c3a662011-04-26 08:12:10 +000016052 tg3_flag_set(tp, INIT_COMPLETE);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000016053 err = tg3_restart_hw(tp, 1);
16054 tg3_full_unlock(tp);
16055 if (err) {
16056 netdev_err(netdev, "Cannot restart hardware after reset.\n");
16057 goto done;
16058 }
16059
16060 netif_device_attach(netdev);
16061
16062 tp->timer.expires = jiffies + tp->timer_offset;
16063 add_timer(&tp->timer);
16064
16065 tg3_netif_start(tp);
16066
16067 tg3_phy_start(tp);
16068
16069done:
16070 rtnl_unlock();
16071}
16072
16073static struct pci_error_handlers tg3_err_handler = {
16074 .error_detected = tg3_io_error_detected,
16075 .slot_reset = tg3_io_slot_reset,
16076 .resume = tg3_io_resume
16077};
16078
Linus Torvalds1da177e2005-04-16 15:20:36 -070016079static struct pci_driver tg3_driver = {
16080 .name = DRV_MODULE_NAME,
16081 .id_table = tg3_pci_tbl,
16082 .probe = tg3_init_one,
16083 .remove = __devexit_p(tg3_remove_one),
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000016084 .err_handler = &tg3_err_handler,
Eric Dumazetaa6027c2011-01-01 05:22:46 +000016085 .driver.pm = TG3_PM_OPS,
Linus Torvalds1da177e2005-04-16 15:20:36 -070016086};
16087
16088static int __init tg3_init(void)
16089{
Jeff Garzik29917622006-08-19 17:48:59 -040016090 return pci_register_driver(&tg3_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016091}
16092
16093static void __exit tg3_cleanup(void)
16094{
16095 pci_unregister_driver(&tg3_driver);
16096}
16097
16098module_init(tg3_init);
16099module_exit(tg3_cleanup);