blob: cc092170dfa3c1f78924fed845cbd64388650df2 [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
Linus Torvalds1da177e2005-04-16 15:20:36 -07002712static int tg3_setup_phy(struct tg3 *, int);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713static int tg3_halt_cpu(struct tg3 *, u32);
2714
Matt Carlson0a459aa2008-11-03 16:54:15 -08002715static void tg3_power_down_phy(struct tg3 *tp, bool do_low_power)
Michael Chan15c3b692006-03-22 01:06:52 -08002716{
Matt Carlsonce057f02007-11-12 21:08:03 -08002717 u32 val;
2718
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002719 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Michael Chan51297242007-02-13 12:17:57 -08002720 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
2721 u32 sg_dig_ctrl = tr32(SG_DIG_CTRL);
2722 u32 serdes_cfg = tr32(MAC_SERDES_CFG);
2723
2724 sg_dig_ctrl |=
2725 SG_DIG_USING_HW_AUTONEG | SG_DIG_SOFT_RESET;
2726 tw32(SG_DIG_CTRL, sg_dig_ctrl);
2727 tw32(MAC_SERDES_CFG, serdes_cfg | (1 << 15));
2728 }
Michael Chan3f7045c2006-09-27 16:02:29 -07002729 return;
Michael Chan51297242007-02-13 12:17:57 -08002730 }
Michael Chan3f7045c2006-09-27 16:02:29 -07002731
Michael Chan60189dd2006-12-17 17:08:07 -08002732 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan60189dd2006-12-17 17:08:07 -08002733 tg3_bmcr_reset(tp);
2734 val = tr32(GRC_MISC_CFG);
2735 tw32_f(GRC_MISC_CFG, val | GRC_MISC_CFG_EPHY_IDDQ);
2736 udelay(40);
2737 return;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002738 } else if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Matt Carlson0e5f7842009-11-02 14:26:38 +00002739 u32 phytest;
2740 if (!tg3_readphy(tp, MII_TG3_FET_TEST, &phytest)) {
2741 u32 phy;
2742
2743 tg3_writephy(tp, MII_ADVERTISE, 0);
2744 tg3_writephy(tp, MII_BMCR,
2745 BMCR_ANENABLE | BMCR_ANRESTART);
2746
2747 tg3_writephy(tp, MII_TG3_FET_TEST,
2748 phytest | MII_TG3_FET_SHADOW_EN);
2749 if (!tg3_readphy(tp, MII_TG3_FET_SHDW_AUXMODE4, &phy)) {
2750 phy |= MII_TG3_FET_SHDW_AUXMODE4_SBPD;
2751 tg3_writephy(tp,
2752 MII_TG3_FET_SHDW_AUXMODE4,
2753 phy);
2754 }
2755 tg3_writephy(tp, MII_TG3_FET_TEST, phytest);
2756 }
2757 return;
Matt Carlson0a459aa2008-11-03 16:54:15 -08002758 } else if (do_low_power) {
Michael Chan715116a2006-09-27 16:09:25 -07002759 tg3_writephy(tp, MII_TG3_EXT_CTRL,
2760 MII_TG3_EXT_CTRL_FORCE_LED_OFF);
Matt Carlson0a459aa2008-11-03 16:54:15 -08002761
Matt Carlsonb4bd2922011-04-20 07:57:41 +00002762 val = MII_TG3_AUXCTL_PCTL_100TX_LPWR |
2763 MII_TG3_AUXCTL_PCTL_SPR_ISOLATE |
2764 MII_TG3_AUXCTL_PCTL_VREG_11V;
2765 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_PWRCTL, val);
Michael Chan715116a2006-09-27 16:09:25 -07002766 }
Michael Chan3f7045c2006-09-27 16:02:29 -07002767
Michael Chan15c3b692006-03-22 01:06:52 -08002768 /* The PHY should not be powered down on some chips because
2769 * of bugs.
2770 */
2771 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2772 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
2773 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780 &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002774 (tp->phy_flags & TG3_PHYFLG_MII_SERDES)))
Michael Chan15c3b692006-03-22 01:06:52 -08002775 return;
Matt Carlsonce057f02007-11-12 21:08:03 -08002776
Matt Carlsonbcb37f62008-11-03 16:52:09 -08002777 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX ||
2778 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5761_AX) {
Matt Carlsonce057f02007-11-12 21:08:03 -08002779 val = tr32(TG3_CPMU_LSPD_1000MB_CLK);
2780 val &= ~CPMU_LSPD_1000MB_MACCLK_MASK;
2781 val |= CPMU_LSPD_1000MB_MACCLK_12_5;
2782 tw32_f(TG3_CPMU_LSPD_1000MB_CLK, val);
2783 }
2784
Michael Chan15c3b692006-03-22 01:06:52 -08002785 tg3_writephy(tp, MII_BMCR, BMCR_PDOWN);
2786}
2787
Matt Carlson3f007892008-11-03 16:51:36 -08002788/* tp->lock is held. */
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002789static int tg3_nvram_lock(struct tg3 *tp)
2790{
Joe Perches63c3a662011-04-26 08:12:10 +00002791 if (tg3_flag(tp, NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002792 int i;
2793
2794 if (tp->nvram_lock_cnt == 0) {
2795 tw32(NVRAM_SWARB, SWARB_REQ_SET1);
2796 for (i = 0; i < 8000; i++) {
2797 if (tr32(NVRAM_SWARB) & SWARB_GNT1)
2798 break;
2799 udelay(20);
2800 }
2801 if (i == 8000) {
2802 tw32(NVRAM_SWARB, SWARB_REQ_CLR1);
2803 return -ENODEV;
2804 }
2805 }
2806 tp->nvram_lock_cnt++;
2807 }
2808 return 0;
2809}
2810
2811/* tp->lock is held. */
2812static void tg3_nvram_unlock(struct tg3 *tp)
2813{
Joe Perches63c3a662011-04-26 08:12:10 +00002814 if (tg3_flag(tp, NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002815 if (tp->nvram_lock_cnt > 0)
2816 tp->nvram_lock_cnt--;
2817 if (tp->nvram_lock_cnt == 0)
2818 tw32_f(NVRAM_SWARB, SWARB_REQ_CLR1);
2819 }
2820}
2821
2822/* tp->lock is held. */
2823static void tg3_enable_nvram_access(struct tg3 *tp)
2824{
Joe Perches63c3a662011-04-26 08:12:10 +00002825 if (tg3_flag(tp, 5750_PLUS) && !tg3_flag(tp, PROTECTED_NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002826 u32 nvaccess = tr32(NVRAM_ACCESS);
2827
2828 tw32(NVRAM_ACCESS, nvaccess | ACCESS_ENABLE);
2829 }
2830}
2831
2832/* tp->lock is held. */
2833static void tg3_disable_nvram_access(struct tg3 *tp)
2834{
Joe Perches63c3a662011-04-26 08:12:10 +00002835 if (tg3_flag(tp, 5750_PLUS) && !tg3_flag(tp, PROTECTED_NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002836 u32 nvaccess = tr32(NVRAM_ACCESS);
2837
2838 tw32(NVRAM_ACCESS, nvaccess & ~ACCESS_ENABLE);
2839 }
2840}
2841
2842static int tg3_nvram_read_using_eeprom(struct tg3 *tp,
2843 u32 offset, u32 *val)
2844{
2845 u32 tmp;
2846 int i;
2847
2848 if (offset > EEPROM_ADDR_ADDR_MASK || (offset % 4) != 0)
2849 return -EINVAL;
2850
2851 tmp = tr32(GRC_EEPROM_ADDR) & ~(EEPROM_ADDR_ADDR_MASK |
2852 EEPROM_ADDR_DEVID_MASK |
2853 EEPROM_ADDR_READ);
2854 tw32(GRC_EEPROM_ADDR,
2855 tmp |
2856 (0 << EEPROM_ADDR_DEVID_SHIFT) |
2857 ((offset << EEPROM_ADDR_ADDR_SHIFT) &
2858 EEPROM_ADDR_ADDR_MASK) |
2859 EEPROM_ADDR_READ | EEPROM_ADDR_START);
2860
2861 for (i = 0; i < 1000; i++) {
2862 tmp = tr32(GRC_EEPROM_ADDR);
2863
2864 if (tmp & EEPROM_ADDR_COMPLETE)
2865 break;
2866 msleep(1);
2867 }
2868 if (!(tmp & EEPROM_ADDR_COMPLETE))
2869 return -EBUSY;
2870
Matt Carlson62cedd12009-04-20 14:52:29 -07002871 tmp = tr32(GRC_EEPROM_DATA);
2872
2873 /*
2874 * The data will always be opposite the native endian
2875 * format. Perform a blind byteswap to compensate.
2876 */
2877 *val = swab32(tmp);
2878
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002879 return 0;
2880}
2881
2882#define NVRAM_CMD_TIMEOUT 10000
2883
2884static int tg3_nvram_exec_cmd(struct tg3 *tp, u32 nvram_cmd)
2885{
2886 int i;
2887
2888 tw32(NVRAM_CMD, nvram_cmd);
2889 for (i = 0; i < NVRAM_CMD_TIMEOUT; i++) {
2890 udelay(10);
2891 if (tr32(NVRAM_CMD) & NVRAM_CMD_DONE) {
2892 udelay(10);
2893 break;
2894 }
2895 }
2896
2897 if (i == NVRAM_CMD_TIMEOUT)
2898 return -EBUSY;
2899
2900 return 0;
2901}
2902
2903static u32 tg3_nvram_phys_addr(struct tg3 *tp, u32 addr)
2904{
Joe Perches63c3a662011-04-26 08:12:10 +00002905 if (tg3_flag(tp, NVRAM) &&
2906 tg3_flag(tp, NVRAM_BUFFERED) &&
2907 tg3_flag(tp, FLASH) &&
2908 !tg3_flag(tp, NO_NVRAM_ADDR_TRANS) &&
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002909 (tp->nvram_jedecnum == JEDEC_ATMEL))
2910
2911 addr = ((addr / tp->nvram_pagesize) <<
2912 ATMEL_AT45DB0X1B_PAGE_POS) +
2913 (addr % tp->nvram_pagesize);
2914
2915 return addr;
2916}
2917
2918static u32 tg3_nvram_logical_addr(struct tg3 *tp, u32 addr)
2919{
Joe Perches63c3a662011-04-26 08:12:10 +00002920 if (tg3_flag(tp, NVRAM) &&
2921 tg3_flag(tp, NVRAM_BUFFERED) &&
2922 tg3_flag(tp, FLASH) &&
2923 !tg3_flag(tp, NO_NVRAM_ADDR_TRANS) &&
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002924 (tp->nvram_jedecnum == JEDEC_ATMEL))
2925
2926 addr = ((addr >> ATMEL_AT45DB0X1B_PAGE_POS) *
2927 tp->nvram_pagesize) +
2928 (addr & ((1 << ATMEL_AT45DB0X1B_PAGE_POS) - 1));
2929
2930 return addr;
2931}
2932
Matt Carlsone4f34112009-02-25 14:25:00 +00002933/* NOTE: Data read in from NVRAM is byteswapped according to
2934 * the byteswapping settings for all other register accesses.
2935 * tg3 devices are BE devices, so on a BE machine, the data
2936 * returned will be exactly as it is seen in NVRAM. On a LE
2937 * machine, the 32-bit value will be byteswapped.
2938 */
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002939static int tg3_nvram_read(struct tg3 *tp, u32 offset, u32 *val)
2940{
2941 int ret;
2942
Joe Perches63c3a662011-04-26 08:12:10 +00002943 if (!tg3_flag(tp, NVRAM))
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002944 return tg3_nvram_read_using_eeprom(tp, offset, val);
2945
2946 offset = tg3_nvram_phys_addr(tp, offset);
2947
2948 if (offset > NVRAM_ADDR_MSK)
2949 return -EINVAL;
2950
2951 ret = tg3_nvram_lock(tp);
2952 if (ret)
2953 return ret;
2954
2955 tg3_enable_nvram_access(tp);
2956
2957 tw32(NVRAM_ADDR, offset);
2958 ret = tg3_nvram_exec_cmd(tp, NVRAM_CMD_RD | NVRAM_CMD_GO |
2959 NVRAM_CMD_FIRST | NVRAM_CMD_LAST | NVRAM_CMD_DONE);
2960
2961 if (ret == 0)
Matt Carlsone4f34112009-02-25 14:25:00 +00002962 *val = tr32(NVRAM_RDDATA);
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002963
2964 tg3_disable_nvram_access(tp);
2965
2966 tg3_nvram_unlock(tp);
2967
2968 return ret;
2969}
2970
Matt Carlsona9dc5292009-02-25 14:25:30 +00002971/* Ensures NVRAM data is in bytestream format. */
2972static int tg3_nvram_read_be32(struct tg3 *tp, u32 offset, __be32 *val)
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002973{
2974 u32 v;
Matt Carlsona9dc5292009-02-25 14:25:30 +00002975 int res = tg3_nvram_read(tp, offset, &v);
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002976 if (!res)
Matt Carlsona9dc5292009-02-25 14:25:30 +00002977 *val = cpu_to_be32(v);
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002978 return res;
2979}
2980
Matt Carlsondbe9b922012-02-13 10:20:09 +00002981static int tg3_nvram_write_block_using_eeprom(struct tg3 *tp,
2982 u32 offset, u32 len, u8 *buf)
2983{
2984 int i, j, rc = 0;
2985 u32 val;
2986
2987 for (i = 0; i < len; i += 4) {
2988 u32 addr;
2989 __be32 data;
2990
2991 addr = offset + i;
2992
2993 memcpy(&data, buf + i, 4);
2994
2995 /*
2996 * The SEEPROM interface expects the data to always be opposite
2997 * the native endian format. We accomplish this by reversing
2998 * all the operations that would have been performed on the
2999 * data from a call to tg3_nvram_read_be32().
3000 */
3001 tw32(GRC_EEPROM_DATA, swab32(be32_to_cpu(data)));
3002
3003 val = tr32(GRC_EEPROM_ADDR);
3004 tw32(GRC_EEPROM_ADDR, val | EEPROM_ADDR_COMPLETE);
3005
3006 val &= ~(EEPROM_ADDR_ADDR_MASK | EEPROM_ADDR_DEVID_MASK |
3007 EEPROM_ADDR_READ);
3008 tw32(GRC_EEPROM_ADDR, val |
3009 (0 << EEPROM_ADDR_DEVID_SHIFT) |
3010 (addr & EEPROM_ADDR_ADDR_MASK) |
3011 EEPROM_ADDR_START |
3012 EEPROM_ADDR_WRITE);
3013
3014 for (j = 0; j < 1000; j++) {
3015 val = tr32(GRC_EEPROM_ADDR);
3016
3017 if (val & EEPROM_ADDR_COMPLETE)
3018 break;
3019 msleep(1);
3020 }
3021 if (!(val & EEPROM_ADDR_COMPLETE)) {
3022 rc = -EBUSY;
3023 break;
3024 }
3025 }
3026
3027 return rc;
3028}
3029
3030/* offset and length are dword aligned */
3031static int tg3_nvram_write_block_unbuffered(struct tg3 *tp, u32 offset, u32 len,
3032 u8 *buf)
3033{
3034 int ret = 0;
3035 u32 pagesize = tp->nvram_pagesize;
3036 u32 pagemask = pagesize - 1;
3037 u32 nvram_cmd;
3038 u8 *tmp;
3039
3040 tmp = kmalloc(pagesize, GFP_KERNEL);
3041 if (tmp == NULL)
3042 return -ENOMEM;
3043
3044 while (len) {
3045 int j;
3046 u32 phy_addr, page_off, size;
3047
3048 phy_addr = offset & ~pagemask;
3049
3050 for (j = 0; j < pagesize; j += 4) {
3051 ret = tg3_nvram_read_be32(tp, phy_addr + j,
3052 (__be32 *) (tmp + j));
3053 if (ret)
3054 break;
3055 }
3056 if (ret)
3057 break;
3058
3059 page_off = offset & pagemask;
3060 size = pagesize;
3061 if (len < size)
3062 size = len;
3063
3064 len -= size;
3065
3066 memcpy(tmp + page_off, buf, size);
3067
3068 offset = offset + (pagesize - page_off);
3069
3070 tg3_enable_nvram_access(tp);
3071
3072 /*
3073 * Before we can erase the flash page, we need
3074 * to issue a special "write enable" command.
3075 */
3076 nvram_cmd = NVRAM_CMD_WREN | NVRAM_CMD_GO | NVRAM_CMD_DONE;
3077
3078 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
3079 break;
3080
3081 /* Erase the target page */
3082 tw32(NVRAM_ADDR, phy_addr);
3083
3084 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE | NVRAM_CMD_WR |
3085 NVRAM_CMD_FIRST | NVRAM_CMD_LAST | NVRAM_CMD_ERASE;
3086
3087 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
3088 break;
3089
3090 /* Issue another write enable to start the write. */
3091 nvram_cmd = NVRAM_CMD_WREN | NVRAM_CMD_GO | NVRAM_CMD_DONE;
3092
3093 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
3094 break;
3095
3096 for (j = 0; j < pagesize; j += 4) {
3097 __be32 data;
3098
3099 data = *((__be32 *) (tmp + j));
3100
3101 tw32(NVRAM_WRDATA, be32_to_cpu(data));
3102
3103 tw32(NVRAM_ADDR, phy_addr + j);
3104
3105 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE |
3106 NVRAM_CMD_WR;
3107
3108 if (j == 0)
3109 nvram_cmd |= NVRAM_CMD_FIRST;
3110 else if (j == (pagesize - 4))
3111 nvram_cmd |= NVRAM_CMD_LAST;
3112
3113 ret = tg3_nvram_exec_cmd(tp, nvram_cmd);
3114 if (ret)
3115 break;
3116 }
3117 if (ret)
3118 break;
3119 }
3120
3121 nvram_cmd = NVRAM_CMD_WRDI | NVRAM_CMD_GO | NVRAM_CMD_DONE;
3122 tg3_nvram_exec_cmd(tp, nvram_cmd);
3123
3124 kfree(tmp);
3125
3126 return ret;
3127}
3128
3129/* offset and length are dword aligned */
3130static int tg3_nvram_write_block_buffered(struct tg3 *tp, u32 offset, u32 len,
3131 u8 *buf)
3132{
3133 int i, ret = 0;
3134
3135 for (i = 0; i < len; i += 4, offset += 4) {
3136 u32 page_off, phy_addr, nvram_cmd;
3137 __be32 data;
3138
3139 memcpy(&data, buf + i, 4);
3140 tw32(NVRAM_WRDATA, be32_to_cpu(data));
3141
3142 page_off = offset % tp->nvram_pagesize;
3143
3144 phy_addr = tg3_nvram_phys_addr(tp, offset);
3145
3146 tw32(NVRAM_ADDR, phy_addr);
3147
3148 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE | NVRAM_CMD_WR;
3149
3150 if (page_off == 0 || i == 0)
3151 nvram_cmd |= NVRAM_CMD_FIRST;
3152 if (page_off == (tp->nvram_pagesize - 4))
3153 nvram_cmd |= NVRAM_CMD_LAST;
3154
3155 if (i == (len - 4))
3156 nvram_cmd |= NVRAM_CMD_LAST;
3157
3158 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
3517static int tg3_power_down_prepare(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003518{
3519 u32 misc_host_ctrl;
Matt Carlson0a459aa2008-11-03 16:54:15 -08003520 bool device_should_wake, do_low_power;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003521
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003522 tg3_enable_register_access(tp);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08003523
3524 /* Restore the CLKREQ setting. */
Joe Perches63c3a662011-04-26 08:12:10 +00003525 if (tg3_flag(tp, CLKREQ_BUG)) {
Matt Carlson5e7dfd02008-11-21 17:18:16 -08003526 u16 lnkctl;
3527
3528 pci_read_config_word(tp->pdev,
Jon Mason708ebb32011-06-27 12:56:50 +00003529 pci_pcie_cap(tp->pdev) + PCI_EXP_LNKCTL,
Matt Carlson5e7dfd02008-11-21 17:18:16 -08003530 &lnkctl);
3531 lnkctl |= PCI_EXP_LNKCTL_CLKREQ_EN;
3532 pci_write_config_word(tp->pdev,
Jon Mason708ebb32011-06-27 12:56:50 +00003533 pci_pcie_cap(tp->pdev) + PCI_EXP_LNKCTL,
Matt Carlson5e7dfd02008-11-21 17:18:16 -08003534 lnkctl);
3535 }
3536
Linus Torvalds1da177e2005-04-16 15:20:36 -07003537 misc_host_ctrl = tr32(TG3PCI_MISC_HOST_CTRL);
3538 tw32(TG3PCI_MISC_HOST_CTRL,
3539 misc_host_ctrl | MISC_HOST_CTRL_MASK_PCI_INT);
3540
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003541 device_should_wake = device_may_wakeup(&tp->pdev->dev) &&
Joe Perches63c3a662011-04-26 08:12:10 +00003542 tg3_flag(tp, WOL_ENABLE);
Matt Carlson05ac4cb2008-11-03 16:53:46 -08003543
Joe Perches63c3a662011-04-26 08:12:10 +00003544 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson0a459aa2008-11-03 16:54:15 -08003545 do_low_power = false;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003546 if ((tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) &&
Matt Carlson800960682010-08-02 11:26:06 +00003547 !(tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07003548 struct phy_device *phydev;
Matt Carlson0a459aa2008-11-03 16:54:15 -08003549 u32 phyid, advertising;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07003550
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00003551 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07003552
Matt Carlson800960682010-08-02 11:26:06 +00003553 tp->phy_flags |= TG3_PHYFLG_IS_LOW_POWER;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07003554
3555 tp->link_config.orig_speed = phydev->speed;
3556 tp->link_config.orig_duplex = phydev->duplex;
3557 tp->link_config.orig_autoneg = phydev->autoneg;
3558 tp->link_config.orig_advertising = phydev->advertising;
3559
3560 advertising = ADVERTISED_TP |
3561 ADVERTISED_Pause |
3562 ADVERTISED_Autoneg |
3563 ADVERTISED_10baseT_Half;
3564
Joe Perches63c3a662011-04-26 08:12:10 +00003565 if (tg3_flag(tp, ENABLE_ASF) || device_should_wake) {
3566 if (tg3_flag(tp, WOL_SPEED_100MB))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07003567 advertising |=
3568 ADVERTISED_100baseT_Half |
3569 ADVERTISED_100baseT_Full |
3570 ADVERTISED_10baseT_Full;
3571 else
3572 advertising |= ADVERTISED_10baseT_Full;
3573 }
3574
3575 phydev->advertising = advertising;
3576
3577 phy_start_aneg(phydev);
Matt Carlson0a459aa2008-11-03 16:54:15 -08003578
3579 phyid = phydev->drv->phy_id & phydev->drv->phy_id_mask;
Matt Carlson6a443a02010-02-17 15:17:04 +00003580 if (phyid != PHY_ID_BCMAC131) {
3581 phyid &= PHY_BCM_OUI_MASK;
3582 if (phyid == PHY_BCM_OUI_1 ||
3583 phyid == PHY_BCM_OUI_2 ||
3584 phyid == PHY_BCM_OUI_3)
Matt Carlson0a459aa2008-11-03 16:54:15 -08003585 do_low_power = true;
3586 }
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07003587 }
Matt Carlsondd477002008-05-25 23:45:58 -07003588 } else {
Matt Carlson20232762008-12-21 20:18:56 -08003589 do_low_power = true;
Matt Carlson0a459aa2008-11-03 16:54:15 -08003590
Matt Carlson800960682010-08-02 11:26:06 +00003591 if (!(tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)) {
3592 tp->phy_flags |= TG3_PHYFLG_IS_LOW_POWER;
Matt Carlsondd477002008-05-25 23:45:58 -07003593 tp->link_config.orig_speed = tp->link_config.speed;
3594 tp->link_config.orig_duplex = tp->link_config.duplex;
3595 tp->link_config.orig_autoneg = tp->link_config.autoneg;
3596 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003597
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003598 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES)) {
Matt Carlsondd477002008-05-25 23:45:58 -07003599 tp->link_config.speed = SPEED_10;
3600 tp->link_config.duplex = DUPLEX_HALF;
3601 tp->link_config.autoneg = AUTONEG_ENABLE;
3602 tg3_setup_phy(tp, 0);
3603 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003604 }
3605
Michael Chanb5d37722006-09-27 16:06:21 -07003606 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
3607 u32 val;
3608
3609 val = tr32(GRC_VCPU_EXT_CTRL);
3610 tw32(GRC_VCPU_EXT_CTRL, val | GRC_VCPU_EXT_CTRL_DISABLE_WOL);
Joe Perches63c3a662011-04-26 08:12:10 +00003611 } else if (!tg3_flag(tp, ENABLE_ASF)) {
Michael Chan6921d202005-12-13 21:15:53 -08003612 int i;
3613 u32 val;
3614
3615 for (i = 0; i < 200; i++) {
3616 tg3_read_mem(tp, NIC_SRAM_FW_ASF_STATUS_MBOX, &val);
3617 if (val == ~NIC_SRAM_FIRMWARE_MBOX_MAGIC1)
3618 break;
3619 msleep(1);
3620 }
3621 }
Joe Perches63c3a662011-04-26 08:12:10 +00003622 if (tg3_flag(tp, WOL_CAP))
Gary Zambranoa85feb82007-05-05 11:52:19 -07003623 tg3_write_mem(tp, NIC_SRAM_WOL_MBOX, WOL_SIGNATURE |
3624 WOL_DRV_STATE_SHUTDOWN |
3625 WOL_DRV_WOL |
3626 WOL_SET_MAGIC_PKT);
Michael Chan6921d202005-12-13 21:15:53 -08003627
Matt Carlson05ac4cb2008-11-03 16:53:46 -08003628 if (device_should_wake) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003629 u32 mac_mode;
3630
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003631 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES)) {
Matt Carlsonb4bd2922011-04-20 07:57:41 +00003632 if (do_low_power &&
3633 !(tp->phy_flags & TG3_PHYFLG_IS_FET)) {
3634 tg3_phy_auxctl_write(tp,
3635 MII_TG3_AUXCTL_SHDWSEL_PWRCTL,
3636 MII_TG3_AUXCTL_PCTL_WOL_EN |
3637 MII_TG3_AUXCTL_PCTL_100TX_LPWR |
3638 MII_TG3_AUXCTL_PCTL_CL_AB_TXDAC);
Matt Carlsondd477002008-05-25 23:45:58 -07003639 udelay(40);
3640 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003641
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003642 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
Michael Chan3f7045c2006-09-27 16:02:29 -07003643 mac_mode = MAC_MODE_PORT_MODE_GMII;
3644 else
3645 mac_mode = MAC_MODE_PORT_MODE_MII;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003646
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07003647 mac_mode |= tp->mac_mode & MAC_MODE_LINK_POLARITY;
3648 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
3649 ASIC_REV_5700) {
Joe Perches63c3a662011-04-26 08:12:10 +00003650 u32 speed = tg3_flag(tp, WOL_SPEED_100MB) ?
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07003651 SPEED_100 : SPEED_10;
3652 if (tg3_5700_link_polarity(tp, speed))
3653 mac_mode |= MAC_MODE_LINK_POLARITY;
3654 else
3655 mac_mode &= ~MAC_MODE_LINK_POLARITY;
3656 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003657 } else {
3658 mac_mode = MAC_MODE_PORT_MODE_TBI;
3659 }
3660
Joe Perches63c3a662011-04-26 08:12:10 +00003661 if (!tg3_flag(tp, 5750_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003662 tw32(MAC_LED_CTRL, tp->led_ctrl);
3663
Matt Carlson05ac4cb2008-11-03 16:53:46 -08003664 mac_mode |= MAC_MODE_MAGIC_PKT_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00003665 if ((tg3_flag(tp, 5705_PLUS) && !tg3_flag(tp, 5780_CLASS)) &&
3666 (tg3_flag(tp, ENABLE_ASF) || tg3_flag(tp, ENABLE_APE)))
Matt Carlson05ac4cb2008-11-03 16:53:46 -08003667 mac_mode |= MAC_MODE_KEEP_FRAME_IN_WOL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003668
Joe Perches63c3a662011-04-26 08:12:10 +00003669 if (tg3_flag(tp, ENABLE_APE))
Matt Carlsond2394e6b2010-11-24 08:31:47 +00003670 mac_mode |= MAC_MODE_APE_TX_EN |
3671 MAC_MODE_APE_RX_EN |
3672 MAC_MODE_TDE_ENABLE;
Matt Carlson3bda1252008-08-15 14:08:22 -07003673
Linus Torvalds1da177e2005-04-16 15:20:36 -07003674 tw32_f(MAC_MODE, mac_mode);
3675 udelay(100);
3676
3677 tw32_f(MAC_RX_MODE, RX_MODE_ENABLE);
3678 udelay(10);
3679 }
3680
Joe Perches63c3a662011-04-26 08:12:10 +00003681 if (!tg3_flag(tp, WOL_SPEED_100MB) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003682 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
3683 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)) {
3684 u32 base_val;
3685
3686 base_val = tp->pci_clock_ctrl;
3687 base_val |= (CLOCK_CTRL_RXCLK_DISABLE |
3688 CLOCK_CTRL_TXCLK_DISABLE);
3689
Michael Chanb401e9e2005-12-19 16:27:04 -08003690 tw32_wait_f(TG3PCI_CLOCK_CTRL, base_val | CLOCK_CTRL_ALTCLK |
3691 CLOCK_CTRL_PWRDOWN_PLL133, 40);
Joe Perches63c3a662011-04-26 08:12:10 +00003692 } else if (tg3_flag(tp, 5780_CLASS) ||
3693 tg3_flag(tp, CPMU_PRESENT) ||
Matt Carlson6ff6f812011-05-19 12:12:54 +00003694 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan4cf78e42005-07-25 12:29:19 -07003695 /* do nothing */
Joe Perches63c3a662011-04-26 08:12:10 +00003696 } else if (!(tg3_flag(tp, 5750_PLUS) && tg3_flag(tp, ENABLE_ASF))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003697 u32 newbits1, newbits2;
3698
3699 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
3700 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
3701 newbits1 = (CLOCK_CTRL_RXCLK_DISABLE |
3702 CLOCK_CTRL_TXCLK_DISABLE |
3703 CLOCK_CTRL_ALTCLK);
3704 newbits2 = newbits1 | CLOCK_CTRL_44MHZ_CORE;
Joe Perches63c3a662011-04-26 08:12:10 +00003705 } else if (tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003706 newbits1 = CLOCK_CTRL_625_CORE;
3707 newbits2 = newbits1 | CLOCK_CTRL_ALTCLK;
3708 } else {
3709 newbits1 = CLOCK_CTRL_ALTCLK;
3710 newbits2 = newbits1 | CLOCK_CTRL_44MHZ_CORE;
3711 }
3712
Michael Chanb401e9e2005-12-19 16:27:04 -08003713 tw32_wait_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl | newbits1,
3714 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003715
Michael Chanb401e9e2005-12-19 16:27:04 -08003716 tw32_wait_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl | newbits2,
3717 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003718
Joe Perches63c3a662011-04-26 08:12:10 +00003719 if (!tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003720 u32 newbits3;
3721
3722 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
3723 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
3724 newbits3 = (CLOCK_CTRL_RXCLK_DISABLE |
3725 CLOCK_CTRL_TXCLK_DISABLE |
3726 CLOCK_CTRL_44MHZ_CORE);
3727 } else {
3728 newbits3 = CLOCK_CTRL_44MHZ_CORE;
3729 }
3730
Michael Chanb401e9e2005-12-19 16:27:04 -08003731 tw32_wait_f(TG3PCI_CLOCK_CTRL,
3732 tp->pci_clock_ctrl | newbits3, 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003733 }
3734 }
3735
Joe Perches63c3a662011-04-26 08:12:10 +00003736 if (!(device_should_wake) && !tg3_flag(tp, ENABLE_ASF))
Matt Carlson0a459aa2008-11-03 16:54:15 -08003737 tg3_power_down_phy(tp, do_low_power);
Michael Chan6921d202005-12-13 21:15:53 -08003738
Matt Carlsoncd0d7222011-07-13 09:27:33 +00003739 tg3_frob_aux_power(tp, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003740
3741 /* Workaround for unstable PLL clock */
3742 if ((GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_AX) ||
3743 (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_BX)) {
3744 u32 val = tr32(0x7d00);
3745
3746 val &= ~((1 << 16) | (1 << 4) | (1 << 2) | (1 << 1) | 1);
3747 tw32(0x7d00, val);
Joe Perches63c3a662011-04-26 08:12:10 +00003748 if (!tg3_flag(tp, ENABLE_ASF)) {
Michael Chanec41c7d2006-01-17 02:40:55 -08003749 int err;
3750
3751 err = tg3_nvram_lock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003752 tg3_halt_cpu(tp, RX_CPU_BASE);
Michael Chanec41c7d2006-01-17 02:40:55 -08003753 if (!err)
3754 tg3_nvram_unlock(tp);
Michael Chan6921d202005-12-13 21:15:53 -08003755 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003756 }
3757
Michael Chanbbadf502006-04-06 21:46:34 -07003758 tg3_write_sig_post_reset(tp, RESET_KIND_SHUTDOWN);
3759
Linus Torvalds1da177e2005-04-16 15:20:36 -07003760 return 0;
3761}
3762
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003763static void tg3_power_down(struct tg3 *tp)
3764{
3765 tg3_power_down_prepare(tp);
3766
Joe Perches63c3a662011-04-26 08:12:10 +00003767 pci_wake_from_d3(tp->pdev, tg3_flag(tp, WOL_ENABLE));
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003768 pci_set_power_state(tp->pdev, PCI_D3hot);
3769}
3770
Linus Torvalds1da177e2005-04-16 15:20:36 -07003771static void tg3_aux_stat_to_speed_duplex(struct tg3 *tp, u32 val, u16 *speed, u8 *duplex)
3772{
3773 switch (val & MII_TG3_AUX_STAT_SPDMASK) {
3774 case MII_TG3_AUX_STAT_10HALF:
3775 *speed = SPEED_10;
3776 *duplex = DUPLEX_HALF;
3777 break;
3778
3779 case MII_TG3_AUX_STAT_10FULL:
3780 *speed = SPEED_10;
3781 *duplex = DUPLEX_FULL;
3782 break;
3783
3784 case MII_TG3_AUX_STAT_100HALF:
3785 *speed = SPEED_100;
3786 *duplex = DUPLEX_HALF;
3787 break;
3788
3789 case MII_TG3_AUX_STAT_100FULL:
3790 *speed = SPEED_100;
3791 *duplex = DUPLEX_FULL;
3792 break;
3793
3794 case MII_TG3_AUX_STAT_1000HALF:
3795 *speed = SPEED_1000;
3796 *duplex = DUPLEX_HALF;
3797 break;
3798
3799 case MII_TG3_AUX_STAT_1000FULL:
3800 *speed = SPEED_1000;
3801 *duplex = DUPLEX_FULL;
3802 break;
3803
3804 default:
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003805 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Michael Chan715116a2006-09-27 16:09:25 -07003806 *speed = (val & MII_TG3_AUX_STAT_100) ? SPEED_100 :
3807 SPEED_10;
3808 *duplex = (val & MII_TG3_AUX_STAT_FULL) ? DUPLEX_FULL :
3809 DUPLEX_HALF;
3810 break;
3811 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003812 *speed = SPEED_INVALID;
3813 *duplex = DUPLEX_INVALID;
3814 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07003815 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003816}
3817
Matt Carlson42b64a42011-05-19 12:12:49 +00003818static int tg3_phy_autoneg_cfg(struct tg3 *tp, u32 advertise, u32 flowctrl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003819{
Matt Carlson42b64a42011-05-19 12:12:49 +00003820 int err = 0;
3821 u32 val, new_adv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003822
Matt Carlson42b64a42011-05-19 12:12:49 +00003823 new_adv = ADVERTISE_CSMA;
Hiroaki SHIMODA202ff1c2011-11-22 04:05:41 +00003824 new_adv |= ethtool_adv_to_mii_adv_t(advertise) & ADVERTISE_ALL;
Matt Carlsonf88788f2011-12-14 11:10:00 +00003825 new_adv |= mii_advertise_flowctrl(flowctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003826
Matt Carlson42b64a42011-05-19 12:12:49 +00003827 err = tg3_writephy(tp, MII_ADVERTISE, new_adv);
3828 if (err)
3829 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003830
Matt Carlson4f272092011-12-14 11:09:57 +00003831 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
3832 new_adv = ethtool_adv_to_mii_ctrl1000_t(advertise);
Matt Carlsonba4d07a2007-12-20 20:08:00 -08003833
Matt Carlson4f272092011-12-14 11:09:57 +00003834 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
3835 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0)
3836 new_adv |= CTL1000_AS_MASTER | CTL1000_ENABLE_MASTER;
Matt Carlsonba4d07a2007-12-20 20:08:00 -08003837
Matt Carlson4f272092011-12-14 11:09:57 +00003838 err = tg3_writephy(tp, MII_CTRL1000, new_adv);
3839 if (err)
3840 goto done;
3841 }
Matt Carlsonba4d07a2007-12-20 20:08:00 -08003842
Matt Carlson42b64a42011-05-19 12:12:49 +00003843 if (!(tp->phy_flags & TG3_PHYFLG_EEE_CAP))
3844 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003845
Matt Carlson42b64a42011-05-19 12:12:49 +00003846 tw32(TG3_CPMU_EEE_MODE,
3847 tr32(TG3_CPMU_EEE_MODE) & ~TG3_CPMU_EEEMD_LPI_ENABLE);
Matt Carlsonba4d07a2007-12-20 20:08:00 -08003848
Matt Carlson42b64a42011-05-19 12:12:49 +00003849 err = TG3_PHY_AUXCTL_SMDSP_ENABLE(tp);
3850 if (!err) {
3851 u32 err2;
Matt Carlson52b02d02010-10-14 10:37:41 +00003852
Matt Carlsona6b68da2010-12-06 08:28:52 +00003853 val = 0;
Matt Carlson42b64a42011-05-19 12:12:49 +00003854 /* Advertise 100-BaseTX EEE ability */
3855 if (advertise & ADVERTISED_100baseT_Full)
3856 val |= MDIO_AN_EEE_ADV_100TX;
3857 /* Advertise 1000-BaseT EEE ability */
3858 if (advertise & ADVERTISED_1000baseT_Full)
3859 val |= MDIO_AN_EEE_ADV_1000T;
3860 err = tg3_phy_cl45_write(tp, MDIO_MMD_AN, MDIO_AN_EEE_ADV, val);
Matt Carlsonb715ce92011-07-20 10:20:52 +00003861 if (err)
3862 val = 0;
3863
3864 switch (GET_ASIC_REV(tp->pci_chip_rev_id)) {
3865 case ASIC_REV_5717:
3866 case ASIC_REV_57765:
Matt Carlson55086ad2011-12-14 11:09:59 +00003867 case ASIC_REV_57766:
Matt Carlsonb715ce92011-07-20 10:20:52 +00003868 case ASIC_REV_5719:
3869 /* If we advertised any eee advertisements above... */
3870 if (val)
3871 val = MII_TG3_DSP_TAP26_ALNOKO |
3872 MII_TG3_DSP_TAP26_RMRXSTO |
3873 MII_TG3_DSP_TAP26_OPCSINPT;
3874 tg3_phydsp_write(tp, MII_TG3_DSP_TAP26, val);
3875 /* Fall through */
3876 case ASIC_REV_5720:
3877 if (!tg3_phydsp_read(tp, MII_TG3_DSP_CH34TP2, &val))
3878 tg3_phydsp_write(tp, MII_TG3_DSP_CH34TP2, val |
3879 MII_TG3_DSP_CH34TP2_HIBW01);
3880 }
Matt Carlson52b02d02010-10-14 10:37:41 +00003881
Matt Carlson42b64a42011-05-19 12:12:49 +00003882 err2 = TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
3883 if (!err)
3884 err = err2;
3885 }
3886
3887done:
3888 return err;
3889}
3890
3891static void tg3_phy_copper_begin(struct tg3 *tp)
3892{
3893 u32 new_adv;
3894 int i;
3895
3896 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) {
3897 new_adv = ADVERTISED_10baseT_Half |
3898 ADVERTISED_10baseT_Full;
3899 if (tg3_flag(tp, WOL_SPEED_100MB))
3900 new_adv |= ADVERTISED_100baseT_Half |
3901 ADVERTISED_100baseT_Full;
3902
3903 tg3_phy_autoneg_cfg(tp, new_adv,
3904 FLOW_CTRL_TX | FLOW_CTRL_RX);
3905 } else if (tp->link_config.speed == SPEED_INVALID) {
3906 if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
3907 tp->link_config.advertising &=
3908 ~(ADVERTISED_1000baseT_Half |
3909 ADVERTISED_1000baseT_Full);
3910
3911 tg3_phy_autoneg_cfg(tp, tp->link_config.advertising,
3912 tp->link_config.flowctrl);
3913 } else {
3914 /* Asking for a specific link mode. */
3915 if (tp->link_config.speed == SPEED_1000) {
3916 if (tp->link_config.duplex == DUPLEX_FULL)
3917 new_adv = ADVERTISED_1000baseT_Full;
3918 else
3919 new_adv = ADVERTISED_1000baseT_Half;
3920 } else if (tp->link_config.speed == SPEED_100) {
3921 if (tp->link_config.duplex == DUPLEX_FULL)
3922 new_adv = ADVERTISED_100baseT_Full;
3923 else
3924 new_adv = ADVERTISED_100baseT_Half;
3925 } else {
3926 if (tp->link_config.duplex == DUPLEX_FULL)
3927 new_adv = ADVERTISED_10baseT_Full;
3928 else
3929 new_adv = ADVERTISED_10baseT_Half;
3930 }
3931
3932 tg3_phy_autoneg_cfg(tp, new_adv,
3933 tp->link_config.flowctrl);
Matt Carlson52b02d02010-10-14 10:37:41 +00003934 }
3935
Linus Torvalds1da177e2005-04-16 15:20:36 -07003936 if (tp->link_config.autoneg == AUTONEG_DISABLE &&
3937 tp->link_config.speed != SPEED_INVALID) {
3938 u32 bmcr, orig_bmcr;
3939
3940 tp->link_config.active_speed = tp->link_config.speed;
3941 tp->link_config.active_duplex = tp->link_config.duplex;
3942
3943 bmcr = 0;
3944 switch (tp->link_config.speed) {
3945 default:
3946 case SPEED_10:
3947 break;
3948
3949 case SPEED_100:
3950 bmcr |= BMCR_SPEED100;
3951 break;
3952
3953 case SPEED_1000:
Matt Carlson221c5632011-06-13 13:39:01 +00003954 bmcr |= BMCR_SPEED1000;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003955 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07003956 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003957
3958 if (tp->link_config.duplex == DUPLEX_FULL)
3959 bmcr |= BMCR_FULLDPLX;
3960
3961 if (!tg3_readphy(tp, MII_BMCR, &orig_bmcr) &&
3962 (bmcr != orig_bmcr)) {
3963 tg3_writephy(tp, MII_BMCR, BMCR_LOOPBACK);
3964 for (i = 0; i < 1500; i++) {
3965 u32 tmp;
3966
3967 udelay(10);
3968 if (tg3_readphy(tp, MII_BMSR, &tmp) ||
3969 tg3_readphy(tp, MII_BMSR, &tmp))
3970 continue;
3971 if (!(tmp & BMSR_LSTATUS)) {
3972 udelay(40);
3973 break;
3974 }
3975 }
3976 tg3_writephy(tp, MII_BMCR, bmcr);
3977 udelay(40);
3978 }
3979 } else {
3980 tg3_writephy(tp, MII_BMCR,
3981 BMCR_ANENABLE | BMCR_ANRESTART);
3982 }
3983}
3984
3985static int tg3_init_5401phy_dsp(struct tg3 *tp)
3986{
3987 int err;
3988
3989 /* Turn off tap power management. */
3990 /* Set Extended packet length bit */
Matt Carlsonb4bd2922011-04-20 07:57:41 +00003991 err = tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_AUXCTL, 0x4c20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003992
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00003993 err |= tg3_phydsp_write(tp, 0x0012, 0x1804);
3994 err |= tg3_phydsp_write(tp, 0x0013, 0x1204);
3995 err |= tg3_phydsp_write(tp, 0x8006, 0x0132);
3996 err |= tg3_phydsp_write(tp, 0x8006, 0x0232);
3997 err |= tg3_phydsp_write(tp, 0x201f, 0x0a20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003998
3999 udelay(40);
4000
4001 return err;
4002}
4003
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004004static bool tg3_phy_copper_an_config_ok(struct tg3 *tp, u32 *lcladv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004005{
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004006 u32 advmsk, tgtadv, advertising;
Michael Chan3600d912006-12-07 00:21:48 -08004007
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004008 advertising = tp->link_config.advertising;
4009 tgtadv = ethtool_adv_to_mii_adv_t(advertising) & ADVERTISE_ALL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004010
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004011 advmsk = ADVERTISE_ALL;
4012 if (tp->link_config.active_duplex == DUPLEX_FULL) {
Matt Carlsonf88788f2011-12-14 11:10:00 +00004013 tgtadv |= mii_advertise_flowctrl(tp->link_config.flowctrl);
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004014 advmsk |= ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
4015 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004016
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004017 if (tg3_readphy(tp, MII_ADVERTISE, lcladv))
4018 return false;
4019
4020 if ((*lcladv & advmsk) != tgtadv)
4021 return false;
Matt Carlsonb99d2a52011-08-31 11:44:47 +00004022
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004023 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004024 u32 tg3_ctrl;
4025
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004026 tgtadv = ethtool_adv_to_mii_ctrl1000_t(advertising);
Michael Chan3600d912006-12-07 00:21:48 -08004027
Matt Carlson221c5632011-06-13 13:39:01 +00004028 if (tg3_readphy(tp, MII_CTRL1000, &tg3_ctrl))
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004029 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004030
Matt Carlsonb99d2a52011-08-31 11:44:47 +00004031 tg3_ctrl &= (ADVERTISE_1000HALF | ADVERTISE_1000FULL);
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004032 if (tg3_ctrl != tgtadv)
4033 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004034 }
Matt Carlson93a700a2011-08-31 11:44:54 +00004035
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004036 return true;
Matt Carlsonef167e22007-12-20 20:10:01 -08004037}
4038
Matt Carlson859edb22011-12-08 14:40:16 +00004039static bool tg3_phy_copper_fetch_rmtadv(struct tg3 *tp, u32 *rmtadv)
4040{
4041 u32 lpeth = 0;
4042
4043 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
4044 u32 val;
4045
4046 if (tg3_readphy(tp, MII_STAT1000, &val))
4047 return false;
4048
4049 lpeth = mii_stat1000_to_ethtool_lpa_t(val);
4050 }
4051
4052 if (tg3_readphy(tp, MII_LPA, rmtadv))
4053 return false;
4054
4055 lpeth |= mii_lpa_to_ethtool_lpa_t(*rmtadv);
4056 tp->link_config.rmt_adv = lpeth;
4057
4058 return true;
4059}
4060
Linus Torvalds1da177e2005-04-16 15:20:36 -07004061static int tg3_setup_copper_phy(struct tg3 *tp, int force_reset)
4062{
4063 int current_link_up;
Matt Carlsonf833c4c2010-09-15 09:00:01 +00004064 u32 bmsr, val;
Matt Carlsonef167e22007-12-20 20:10:01 -08004065 u32 lcl_adv, rmt_adv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004066 u16 current_speed;
4067 u8 current_duplex;
4068 int i, err;
4069
4070 tw32(MAC_EVENT, 0);
4071
4072 tw32_f(MAC_STATUS,
4073 (MAC_STATUS_SYNC_CHANGED |
4074 MAC_STATUS_CFG_CHANGED |
4075 MAC_STATUS_MI_COMPLETION |
4076 MAC_STATUS_LNKSTATE_CHANGED));
4077 udelay(40);
4078
Matt Carlson8ef21422008-05-02 16:47:53 -07004079 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
4080 tw32_f(MAC_MI_MODE,
4081 (tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL));
4082 udelay(80);
4083 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004084
Matt Carlsonb4bd2922011-04-20 07:57:41 +00004085 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_PWRCTL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004086
4087 /* Some third-party PHYs need to be reset on link going
4088 * down.
4089 */
4090 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
4091 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
4092 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) &&
4093 netif_carrier_ok(tp->dev)) {
4094 tg3_readphy(tp, MII_BMSR, &bmsr);
4095 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
4096 !(bmsr & BMSR_LSTATUS))
4097 force_reset = 1;
4098 }
4099 if (force_reset)
4100 tg3_phy_reset(tp);
4101
Matt Carlson79eb6902010-02-17 15:17:03 +00004102 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004103 tg3_readphy(tp, MII_BMSR, &bmsr);
4104 if (tg3_readphy(tp, MII_BMSR, &bmsr) ||
Joe Perches63c3a662011-04-26 08:12:10 +00004105 !tg3_flag(tp, INIT_COMPLETE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004106 bmsr = 0;
4107
4108 if (!(bmsr & BMSR_LSTATUS)) {
4109 err = tg3_init_5401phy_dsp(tp);
4110 if (err)
4111 return err;
4112
4113 tg3_readphy(tp, MII_BMSR, &bmsr);
4114 for (i = 0; i < 1000; i++) {
4115 udelay(10);
4116 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
4117 (bmsr & BMSR_LSTATUS)) {
4118 udelay(40);
4119 break;
4120 }
4121 }
4122
Matt Carlson79eb6902010-02-17 15:17:03 +00004123 if ((tp->phy_id & TG3_PHY_ID_REV_MASK) ==
4124 TG3_PHY_REV_BCM5401_B0 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004125 !(bmsr & BMSR_LSTATUS) &&
4126 tp->link_config.active_speed == SPEED_1000) {
4127 err = tg3_phy_reset(tp);
4128 if (!err)
4129 err = tg3_init_5401phy_dsp(tp);
4130 if (err)
4131 return err;
4132 }
4133 }
4134 } else if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
4135 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0) {
4136 /* 5701 {A0,B0} CRC bug workaround */
4137 tg3_writephy(tp, 0x15, 0x0a75);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00004138 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8c68);
4139 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8d68);
4140 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8c68);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004141 }
4142
4143 /* Clear pending interrupts... */
Matt Carlsonf833c4c2010-09-15 09:00:01 +00004144 tg3_readphy(tp, MII_TG3_ISTAT, &val);
4145 tg3_readphy(tp, MII_TG3_ISTAT, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004146
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004147 if (tp->phy_flags & TG3_PHYFLG_USE_MI_INTERRUPT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004148 tg3_writephy(tp, MII_TG3_IMASK, ~MII_TG3_INT_LINKCHG);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004149 else if (!(tp->phy_flags & TG3_PHYFLG_IS_FET))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004150 tg3_writephy(tp, MII_TG3_IMASK, ~0);
4151
4152 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
4153 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
4154 if (tp->led_ctrl == LED_CTRL_MODE_PHY_1)
4155 tg3_writephy(tp, MII_TG3_EXT_CTRL,
4156 MII_TG3_EXT_CTRL_LNK3_LED_MODE);
4157 else
4158 tg3_writephy(tp, MII_TG3_EXT_CTRL, 0);
4159 }
4160
4161 current_link_up = 0;
4162 current_speed = SPEED_INVALID;
4163 current_duplex = DUPLEX_INVALID;
Matt Carlsone348c5e2011-11-21 15:01:20 +00004164 tp->phy_flags &= ~TG3_PHYFLG_MDIX_STATE;
Matt Carlson859edb22011-12-08 14:40:16 +00004165 tp->link_config.rmt_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004166
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004167 if (tp->phy_flags & TG3_PHYFLG_CAPACITIVE_COUPLING) {
Matt Carlson15ee95c2011-04-20 07:57:40 +00004168 err = tg3_phy_auxctl_read(tp,
4169 MII_TG3_AUXCTL_SHDWSEL_MISCTEST,
4170 &val);
4171 if (!err && !(val & (1 << 10))) {
Matt Carlsonb4bd2922011-04-20 07:57:41 +00004172 tg3_phy_auxctl_write(tp,
4173 MII_TG3_AUXCTL_SHDWSEL_MISCTEST,
4174 val | (1 << 10));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004175 goto relink;
4176 }
4177 }
4178
4179 bmsr = 0;
4180 for (i = 0; i < 100; i++) {
4181 tg3_readphy(tp, MII_BMSR, &bmsr);
4182 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
4183 (bmsr & BMSR_LSTATUS))
4184 break;
4185 udelay(40);
4186 }
4187
4188 if (bmsr & BMSR_LSTATUS) {
4189 u32 aux_stat, bmcr;
4190
4191 tg3_readphy(tp, MII_TG3_AUX_STAT, &aux_stat);
4192 for (i = 0; i < 2000; i++) {
4193 udelay(10);
4194 if (!tg3_readphy(tp, MII_TG3_AUX_STAT, &aux_stat) &&
4195 aux_stat)
4196 break;
4197 }
4198
4199 tg3_aux_stat_to_speed_duplex(tp, aux_stat,
4200 &current_speed,
4201 &current_duplex);
4202
4203 bmcr = 0;
4204 for (i = 0; i < 200; i++) {
4205 tg3_readphy(tp, MII_BMCR, &bmcr);
4206 if (tg3_readphy(tp, MII_BMCR, &bmcr))
4207 continue;
4208 if (bmcr && bmcr != 0x7fff)
4209 break;
4210 udelay(10);
4211 }
4212
Matt Carlsonef167e22007-12-20 20:10:01 -08004213 lcl_adv = 0;
4214 rmt_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004215
Matt Carlsonef167e22007-12-20 20:10:01 -08004216 tp->link_config.active_speed = current_speed;
4217 tp->link_config.active_duplex = current_duplex;
4218
4219 if (tp->link_config.autoneg == AUTONEG_ENABLE) {
4220 if ((bmcr & BMCR_ANENABLE) &&
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004221 tg3_phy_copper_an_config_ok(tp, &lcl_adv) &&
Matt Carlson859edb22011-12-08 14:40:16 +00004222 tg3_phy_copper_fetch_rmtadv(tp, &rmt_adv))
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004223 current_link_up = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004224 } else {
4225 if (!(bmcr & BMCR_ANENABLE) &&
4226 tp->link_config.speed == current_speed &&
Matt Carlsonef167e22007-12-20 20:10:01 -08004227 tp->link_config.duplex == current_duplex &&
4228 tp->link_config.flowctrl ==
4229 tp->link_config.active_flowctrl) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004230 current_link_up = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004231 }
4232 }
4233
Matt Carlsonef167e22007-12-20 20:10:01 -08004234 if (current_link_up == 1 &&
Matt Carlsone348c5e2011-11-21 15:01:20 +00004235 tp->link_config.active_duplex == DUPLEX_FULL) {
4236 u32 reg, bit;
4237
4238 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
4239 reg = MII_TG3_FET_GEN_STAT;
4240 bit = MII_TG3_FET_GEN_STAT_MDIXSTAT;
4241 } else {
4242 reg = MII_TG3_EXT_STAT;
4243 bit = MII_TG3_EXT_STAT_MDIX;
4244 }
4245
4246 if (!tg3_readphy(tp, reg, &val) && (val & bit))
4247 tp->phy_flags |= TG3_PHYFLG_MDIX_STATE;
4248
Matt Carlsonef167e22007-12-20 20:10:01 -08004249 tg3_setup_flow_control(tp, lcl_adv, rmt_adv);
Matt Carlsone348c5e2011-11-21 15:01:20 +00004250 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004251 }
4252
Linus Torvalds1da177e2005-04-16 15:20:36 -07004253relink:
Matt Carlson800960682010-08-02 11:26:06 +00004254 if (current_link_up == 0 || (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004255 tg3_phy_copper_begin(tp);
4256
Matt Carlsonf833c4c2010-09-15 09:00:01 +00004257 tg3_readphy(tp, MII_BMSR, &bmsr);
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00004258 if ((!tg3_readphy(tp, MII_BMSR, &bmsr) && (bmsr & BMSR_LSTATUS)) ||
4259 (tp->mac_mode & MAC_MODE_PORT_INT_LPBACK))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004260 current_link_up = 1;
4261 }
4262
4263 tp->mac_mode &= ~MAC_MODE_PORT_MODE_MASK;
4264 if (current_link_up == 1) {
4265 if (tp->link_config.active_speed == SPEED_100 ||
4266 tp->link_config.active_speed == SPEED_10)
4267 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
4268 else
4269 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004270 } else if (tp->phy_flags & TG3_PHYFLG_IS_FET)
Matt Carlson7f97a4b2009-08-25 10:10:03 +00004271 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
4272 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004273 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
4274
4275 tp->mac_mode &= ~MAC_MODE_HALF_DUPLEX;
4276 if (tp->link_config.active_duplex == DUPLEX_HALF)
4277 tp->mac_mode |= MAC_MODE_HALF_DUPLEX;
4278
Linus Torvalds1da177e2005-04-16 15:20:36 -07004279 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700) {
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07004280 if (current_link_up == 1 &&
4281 tg3_5700_link_polarity(tp, tp->link_config.active_speed))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004282 tp->mac_mode |= MAC_MODE_LINK_POLARITY;
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07004283 else
4284 tp->mac_mode &= ~MAC_MODE_LINK_POLARITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004285 }
4286
4287 /* ??? Without this setting Netgear GA302T PHY does not
4288 * ??? send/receive packets...
4289 */
Matt Carlson79eb6902010-02-17 15:17:03 +00004290 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5411 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004291 tp->pci_chip_rev_id == CHIPREV_ID_5700_ALTIMA) {
4292 tp->mi_mode |= MAC_MI_MODE_AUTO_POLL;
4293 tw32_f(MAC_MI_MODE, tp->mi_mode);
4294 udelay(80);
4295 }
4296
4297 tw32_f(MAC_MODE, tp->mac_mode);
4298 udelay(40);
4299
Matt Carlson52b02d02010-10-14 10:37:41 +00004300 tg3_phy_eee_adjust(tp, current_link_up);
4301
Joe Perches63c3a662011-04-26 08:12:10 +00004302 if (tg3_flag(tp, USE_LINKCHG_REG)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004303 /* Polled via timer. */
4304 tw32_f(MAC_EVENT, 0);
4305 } else {
4306 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
4307 }
4308 udelay(40);
4309
4310 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 &&
4311 current_link_up == 1 &&
4312 tp->link_config.active_speed == SPEED_1000 &&
Joe Perches63c3a662011-04-26 08:12:10 +00004313 (tg3_flag(tp, PCIX_MODE) || tg3_flag(tp, PCI_HIGH_SPEED))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004314 udelay(120);
4315 tw32_f(MAC_STATUS,
4316 (MAC_STATUS_SYNC_CHANGED |
4317 MAC_STATUS_CFG_CHANGED));
4318 udelay(40);
4319 tg3_write_mem(tp,
4320 NIC_SRAM_FIRMWARE_MBOX,
4321 NIC_SRAM_FIRMWARE_MBOX_MAGIC2);
4322 }
4323
Matt Carlson5e7dfd02008-11-21 17:18:16 -08004324 /* Prevent send BD corruption. */
Joe Perches63c3a662011-04-26 08:12:10 +00004325 if (tg3_flag(tp, CLKREQ_BUG)) {
Matt Carlson5e7dfd02008-11-21 17:18:16 -08004326 u16 oldlnkctl, newlnkctl;
4327
4328 pci_read_config_word(tp->pdev,
Jon Mason708ebb32011-06-27 12:56:50 +00004329 pci_pcie_cap(tp->pdev) + PCI_EXP_LNKCTL,
Matt Carlson5e7dfd02008-11-21 17:18:16 -08004330 &oldlnkctl);
4331 if (tp->link_config.active_speed == SPEED_100 ||
4332 tp->link_config.active_speed == SPEED_10)
4333 newlnkctl = oldlnkctl & ~PCI_EXP_LNKCTL_CLKREQ_EN;
4334 else
4335 newlnkctl = oldlnkctl | PCI_EXP_LNKCTL_CLKREQ_EN;
4336 if (newlnkctl != oldlnkctl)
4337 pci_write_config_word(tp->pdev,
Matt Carlson93a700a2011-08-31 11:44:54 +00004338 pci_pcie_cap(tp->pdev) +
4339 PCI_EXP_LNKCTL, newlnkctl);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08004340 }
4341
Linus Torvalds1da177e2005-04-16 15:20:36 -07004342 if (current_link_up != netif_carrier_ok(tp->dev)) {
4343 if (current_link_up)
4344 netif_carrier_on(tp->dev);
4345 else
4346 netif_carrier_off(tp->dev);
4347 tg3_link_report(tp);
4348 }
4349
4350 return 0;
4351}
4352
4353struct tg3_fiber_aneginfo {
4354 int state;
4355#define ANEG_STATE_UNKNOWN 0
4356#define ANEG_STATE_AN_ENABLE 1
4357#define ANEG_STATE_RESTART_INIT 2
4358#define ANEG_STATE_RESTART 3
4359#define ANEG_STATE_DISABLE_LINK_OK 4
4360#define ANEG_STATE_ABILITY_DETECT_INIT 5
4361#define ANEG_STATE_ABILITY_DETECT 6
4362#define ANEG_STATE_ACK_DETECT_INIT 7
4363#define ANEG_STATE_ACK_DETECT 8
4364#define ANEG_STATE_COMPLETE_ACK_INIT 9
4365#define ANEG_STATE_COMPLETE_ACK 10
4366#define ANEG_STATE_IDLE_DETECT_INIT 11
4367#define ANEG_STATE_IDLE_DETECT 12
4368#define ANEG_STATE_LINK_OK 13
4369#define ANEG_STATE_NEXT_PAGE_WAIT_INIT 14
4370#define ANEG_STATE_NEXT_PAGE_WAIT 15
4371
4372 u32 flags;
4373#define MR_AN_ENABLE 0x00000001
4374#define MR_RESTART_AN 0x00000002
4375#define MR_AN_COMPLETE 0x00000004
4376#define MR_PAGE_RX 0x00000008
4377#define MR_NP_LOADED 0x00000010
4378#define MR_TOGGLE_TX 0x00000020
4379#define MR_LP_ADV_FULL_DUPLEX 0x00000040
4380#define MR_LP_ADV_HALF_DUPLEX 0x00000080
4381#define MR_LP_ADV_SYM_PAUSE 0x00000100
4382#define MR_LP_ADV_ASYM_PAUSE 0x00000200
4383#define MR_LP_ADV_REMOTE_FAULT1 0x00000400
4384#define MR_LP_ADV_REMOTE_FAULT2 0x00000800
4385#define MR_LP_ADV_NEXT_PAGE 0x00001000
4386#define MR_TOGGLE_RX 0x00002000
4387#define MR_NP_RX 0x00004000
4388
4389#define MR_LINK_OK 0x80000000
4390
4391 unsigned long link_time, cur_time;
4392
4393 u32 ability_match_cfg;
4394 int ability_match_count;
4395
4396 char ability_match, idle_match, ack_match;
4397
4398 u32 txconfig, rxconfig;
4399#define ANEG_CFG_NP 0x00000080
4400#define ANEG_CFG_ACK 0x00000040
4401#define ANEG_CFG_RF2 0x00000020
4402#define ANEG_CFG_RF1 0x00000010
4403#define ANEG_CFG_PS2 0x00000001
4404#define ANEG_CFG_PS1 0x00008000
4405#define ANEG_CFG_HD 0x00004000
4406#define ANEG_CFG_FD 0x00002000
4407#define ANEG_CFG_INVAL 0x00001f06
4408
4409};
4410#define ANEG_OK 0
4411#define ANEG_DONE 1
4412#define ANEG_TIMER_ENAB 2
4413#define ANEG_FAILED -1
4414
4415#define ANEG_STATE_SETTLE_TIME 10000
4416
4417static int tg3_fiber_aneg_smachine(struct tg3 *tp,
4418 struct tg3_fiber_aneginfo *ap)
4419{
Matt Carlson5be73b42007-12-20 20:09:29 -08004420 u16 flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004421 unsigned long delta;
4422 u32 rx_cfg_reg;
4423 int ret;
4424
4425 if (ap->state == ANEG_STATE_UNKNOWN) {
4426 ap->rxconfig = 0;
4427 ap->link_time = 0;
4428 ap->cur_time = 0;
4429 ap->ability_match_cfg = 0;
4430 ap->ability_match_count = 0;
4431 ap->ability_match = 0;
4432 ap->idle_match = 0;
4433 ap->ack_match = 0;
4434 }
4435 ap->cur_time++;
4436
4437 if (tr32(MAC_STATUS) & MAC_STATUS_RCVD_CFG) {
4438 rx_cfg_reg = tr32(MAC_RX_AUTO_NEG);
4439
4440 if (rx_cfg_reg != ap->ability_match_cfg) {
4441 ap->ability_match_cfg = rx_cfg_reg;
4442 ap->ability_match = 0;
4443 ap->ability_match_count = 0;
4444 } else {
4445 if (++ap->ability_match_count > 1) {
4446 ap->ability_match = 1;
4447 ap->ability_match_cfg = rx_cfg_reg;
4448 }
4449 }
4450 if (rx_cfg_reg & ANEG_CFG_ACK)
4451 ap->ack_match = 1;
4452 else
4453 ap->ack_match = 0;
4454
4455 ap->idle_match = 0;
4456 } else {
4457 ap->idle_match = 1;
4458 ap->ability_match_cfg = 0;
4459 ap->ability_match_count = 0;
4460 ap->ability_match = 0;
4461 ap->ack_match = 0;
4462
4463 rx_cfg_reg = 0;
4464 }
4465
4466 ap->rxconfig = rx_cfg_reg;
4467 ret = ANEG_OK;
4468
Matt Carlson33f401a2010-04-05 10:19:27 +00004469 switch (ap->state) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004470 case ANEG_STATE_UNKNOWN:
4471 if (ap->flags & (MR_AN_ENABLE | MR_RESTART_AN))
4472 ap->state = ANEG_STATE_AN_ENABLE;
4473
4474 /* fallthru */
4475 case ANEG_STATE_AN_ENABLE:
4476 ap->flags &= ~(MR_AN_COMPLETE | MR_PAGE_RX);
4477 if (ap->flags & MR_AN_ENABLE) {
4478 ap->link_time = 0;
4479 ap->cur_time = 0;
4480 ap->ability_match_cfg = 0;
4481 ap->ability_match_count = 0;
4482 ap->ability_match = 0;
4483 ap->idle_match = 0;
4484 ap->ack_match = 0;
4485
4486 ap->state = ANEG_STATE_RESTART_INIT;
4487 } else {
4488 ap->state = ANEG_STATE_DISABLE_LINK_OK;
4489 }
4490 break;
4491
4492 case ANEG_STATE_RESTART_INIT:
4493 ap->link_time = ap->cur_time;
4494 ap->flags &= ~(MR_NP_LOADED);
4495 ap->txconfig = 0;
4496 tw32(MAC_TX_AUTO_NEG, 0);
4497 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
4498 tw32_f(MAC_MODE, tp->mac_mode);
4499 udelay(40);
4500
4501 ret = ANEG_TIMER_ENAB;
4502 ap->state = ANEG_STATE_RESTART;
4503
4504 /* fallthru */
4505 case ANEG_STATE_RESTART:
4506 delta = ap->cur_time - ap->link_time;
Matt Carlson859a588792010-04-05 10:19:28 +00004507 if (delta > ANEG_STATE_SETTLE_TIME)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004508 ap->state = ANEG_STATE_ABILITY_DETECT_INIT;
Matt Carlson859a588792010-04-05 10:19:28 +00004509 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004510 ret = ANEG_TIMER_ENAB;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004511 break;
4512
4513 case ANEG_STATE_DISABLE_LINK_OK:
4514 ret = ANEG_DONE;
4515 break;
4516
4517 case ANEG_STATE_ABILITY_DETECT_INIT:
4518 ap->flags &= ~(MR_TOGGLE_TX);
Matt Carlson5be73b42007-12-20 20:09:29 -08004519 ap->txconfig = ANEG_CFG_FD;
4520 flowctrl = tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
4521 if (flowctrl & ADVERTISE_1000XPAUSE)
4522 ap->txconfig |= ANEG_CFG_PS1;
4523 if (flowctrl & ADVERTISE_1000XPSE_ASYM)
4524 ap->txconfig |= ANEG_CFG_PS2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004525 tw32(MAC_TX_AUTO_NEG, ap->txconfig);
4526 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
4527 tw32_f(MAC_MODE, tp->mac_mode);
4528 udelay(40);
4529
4530 ap->state = ANEG_STATE_ABILITY_DETECT;
4531 break;
4532
4533 case ANEG_STATE_ABILITY_DETECT:
Matt Carlson859a588792010-04-05 10:19:28 +00004534 if (ap->ability_match != 0 && ap->rxconfig != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004535 ap->state = ANEG_STATE_ACK_DETECT_INIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004536 break;
4537
4538 case ANEG_STATE_ACK_DETECT_INIT:
4539 ap->txconfig |= ANEG_CFG_ACK;
4540 tw32(MAC_TX_AUTO_NEG, ap->txconfig);
4541 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
4542 tw32_f(MAC_MODE, tp->mac_mode);
4543 udelay(40);
4544
4545 ap->state = ANEG_STATE_ACK_DETECT;
4546
4547 /* fallthru */
4548 case ANEG_STATE_ACK_DETECT:
4549 if (ap->ack_match != 0) {
4550 if ((ap->rxconfig & ~ANEG_CFG_ACK) ==
4551 (ap->ability_match_cfg & ~ANEG_CFG_ACK)) {
4552 ap->state = ANEG_STATE_COMPLETE_ACK_INIT;
4553 } else {
4554 ap->state = ANEG_STATE_AN_ENABLE;
4555 }
4556 } else if (ap->ability_match != 0 &&
4557 ap->rxconfig == 0) {
4558 ap->state = ANEG_STATE_AN_ENABLE;
4559 }
4560 break;
4561
4562 case ANEG_STATE_COMPLETE_ACK_INIT:
4563 if (ap->rxconfig & ANEG_CFG_INVAL) {
4564 ret = ANEG_FAILED;
4565 break;
4566 }
4567 ap->flags &= ~(MR_LP_ADV_FULL_DUPLEX |
4568 MR_LP_ADV_HALF_DUPLEX |
4569 MR_LP_ADV_SYM_PAUSE |
4570 MR_LP_ADV_ASYM_PAUSE |
4571 MR_LP_ADV_REMOTE_FAULT1 |
4572 MR_LP_ADV_REMOTE_FAULT2 |
4573 MR_LP_ADV_NEXT_PAGE |
4574 MR_TOGGLE_RX |
4575 MR_NP_RX);
4576 if (ap->rxconfig & ANEG_CFG_FD)
4577 ap->flags |= MR_LP_ADV_FULL_DUPLEX;
4578 if (ap->rxconfig & ANEG_CFG_HD)
4579 ap->flags |= MR_LP_ADV_HALF_DUPLEX;
4580 if (ap->rxconfig & ANEG_CFG_PS1)
4581 ap->flags |= MR_LP_ADV_SYM_PAUSE;
4582 if (ap->rxconfig & ANEG_CFG_PS2)
4583 ap->flags |= MR_LP_ADV_ASYM_PAUSE;
4584 if (ap->rxconfig & ANEG_CFG_RF1)
4585 ap->flags |= MR_LP_ADV_REMOTE_FAULT1;
4586 if (ap->rxconfig & ANEG_CFG_RF2)
4587 ap->flags |= MR_LP_ADV_REMOTE_FAULT2;
4588 if (ap->rxconfig & ANEG_CFG_NP)
4589 ap->flags |= MR_LP_ADV_NEXT_PAGE;
4590
4591 ap->link_time = ap->cur_time;
4592
4593 ap->flags ^= (MR_TOGGLE_TX);
4594 if (ap->rxconfig & 0x0008)
4595 ap->flags |= MR_TOGGLE_RX;
4596 if (ap->rxconfig & ANEG_CFG_NP)
4597 ap->flags |= MR_NP_RX;
4598 ap->flags |= MR_PAGE_RX;
4599
4600 ap->state = ANEG_STATE_COMPLETE_ACK;
4601 ret = ANEG_TIMER_ENAB;
4602 break;
4603
4604 case ANEG_STATE_COMPLETE_ACK:
4605 if (ap->ability_match != 0 &&
4606 ap->rxconfig == 0) {
4607 ap->state = ANEG_STATE_AN_ENABLE;
4608 break;
4609 }
4610 delta = ap->cur_time - ap->link_time;
4611 if (delta > ANEG_STATE_SETTLE_TIME) {
4612 if (!(ap->flags & (MR_LP_ADV_NEXT_PAGE))) {
4613 ap->state = ANEG_STATE_IDLE_DETECT_INIT;
4614 } else {
4615 if ((ap->txconfig & ANEG_CFG_NP) == 0 &&
4616 !(ap->flags & MR_NP_RX)) {
4617 ap->state = ANEG_STATE_IDLE_DETECT_INIT;
4618 } else {
4619 ret = ANEG_FAILED;
4620 }
4621 }
4622 }
4623 break;
4624
4625 case ANEG_STATE_IDLE_DETECT_INIT:
4626 ap->link_time = ap->cur_time;
4627 tp->mac_mode &= ~MAC_MODE_SEND_CONFIGS;
4628 tw32_f(MAC_MODE, tp->mac_mode);
4629 udelay(40);
4630
4631 ap->state = ANEG_STATE_IDLE_DETECT;
4632 ret = ANEG_TIMER_ENAB;
4633 break;
4634
4635 case ANEG_STATE_IDLE_DETECT:
4636 if (ap->ability_match != 0 &&
4637 ap->rxconfig == 0) {
4638 ap->state = ANEG_STATE_AN_ENABLE;
4639 break;
4640 }
4641 delta = ap->cur_time - ap->link_time;
4642 if (delta > ANEG_STATE_SETTLE_TIME) {
4643 /* XXX another gem from the Broadcom driver :( */
4644 ap->state = ANEG_STATE_LINK_OK;
4645 }
4646 break;
4647
4648 case ANEG_STATE_LINK_OK:
4649 ap->flags |= (MR_AN_COMPLETE | MR_LINK_OK);
4650 ret = ANEG_DONE;
4651 break;
4652
4653 case ANEG_STATE_NEXT_PAGE_WAIT_INIT:
4654 /* ??? unimplemented */
4655 break;
4656
4657 case ANEG_STATE_NEXT_PAGE_WAIT:
4658 /* ??? unimplemented */
4659 break;
4660
4661 default:
4662 ret = ANEG_FAILED;
4663 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07004664 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004665
4666 return ret;
4667}
4668
Matt Carlson5be73b42007-12-20 20:09:29 -08004669static int fiber_autoneg(struct tg3 *tp, u32 *txflags, u32 *rxflags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004670{
4671 int res = 0;
4672 struct tg3_fiber_aneginfo aninfo;
4673 int status = ANEG_FAILED;
4674 unsigned int tick;
4675 u32 tmp;
4676
4677 tw32_f(MAC_TX_AUTO_NEG, 0);
4678
4679 tmp = tp->mac_mode & ~MAC_MODE_PORT_MODE_MASK;
4680 tw32_f(MAC_MODE, tmp | MAC_MODE_PORT_MODE_GMII);
4681 udelay(40);
4682
4683 tw32_f(MAC_MODE, tp->mac_mode | MAC_MODE_SEND_CONFIGS);
4684 udelay(40);
4685
4686 memset(&aninfo, 0, sizeof(aninfo));
4687 aninfo.flags |= MR_AN_ENABLE;
4688 aninfo.state = ANEG_STATE_UNKNOWN;
4689 aninfo.cur_time = 0;
4690 tick = 0;
4691 while (++tick < 195000) {
4692 status = tg3_fiber_aneg_smachine(tp, &aninfo);
4693 if (status == ANEG_DONE || status == ANEG_FAILED)
4694 break;
4695
4696 udelay(1);
4697 }
4698
4699 tp->mac_mode &= ~MAC_MODE_SEND_CONFIGS;
4700 tw32_f(MAC_MODE, tp->mac_mode);
4701 udelay(40);
4702
Matt Carlson5be73b42007-12-20 20:09:29 -08004703 *txflags = aninfo.txconfig;
4704 *rxflags = aninfo.flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004705
4706 if (status == ANEG_DONE &&
4707 (aninfo.flags & (MR_AN_COMPLETE | MR_LINK_OK |
4708 MR_LP_ADV_FULL_DUPLEX)))
4709 res = 1;
4710
4711 return res;
4712}
4713
4714static void tg3_init_bcm8002(struct tg3 *tp)
4715{
4716 u32 mac_status = tr32(MAC_STATUS);
4717 int i;
4718
4719 /* Reset when initting first time or we have a link. */
Joe Perches63c3a662011-04-26 08:12:10 +00004720 if (tg3_flag(tp, INIT_COMPLETE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004721 !(mac_status & MAC_STATUS_PCS_SYNCED))
4722 return;
4723
4724 /* Set PLL lock range. */
4725 tg3_writephy(tp, 0x16, 0x8007);
4726
4727 /* SW reset */
4728 tg3_writephy(tp, MII_BMCR, BMCR_RESET);
4729
4730 /* Wait for reset to complete. */
4731 /* XXX schedule_timeout() ... */
4732 for (i = 0; i < 500; i++)
4733 udelay(10);
4734
4735 /* Config mode; select PMA/Ch 1 regs. */
4736 tg3_writephy(tp, 0x10, 0x8411);
4737
4738 /* Enable auto-lock and comdet, select txclk for tx. */
4739 tg3_writephy(tp, 0x11, 0x0a10);
4740
4741 tg3_writephy(tp, 0x18, 0x00a0);
4742 tg3_writephy(tp, 0x16, 0x41ff);
4743
4744 /* Assert and deassert POR. */
4745 tg3_writephy(tp, 0x13, 0x0400);
4746 udelay(40);
4747 tg3_writephy(tp, 0x13, 0x0000);
4748
4749 tg3_writephy(tp, 0x11, 0x0a50);
4750 udelay(40);
4751 tg3_writephy(tp, 0x11, 0x0a10);
4752
4753 /* Wait for signal to stabilize */
4754 /* XXX schedule_timeout() ... */
4755 for (i = 0; i < 15000; i++)
4756 udelay(10);
4757
4758 /* Deselect the channel register so we can read the PHYID
4759 * later.
4760 */
4761 tg3_writephy(tp, 0x10, 0x8011);
4762}
4763
4764static int tg3_setup_fiber_hw_autoneg(struct tg3 *tp, u32 mac_status)
4765{
Matt Carlson82cd3d12007-12-20 20:09:00 -08004766 u16 flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004767 u32 sg_dig_ctrl, sg_dig_status;
4768 u32 serdes_cfg, expected_sg_dig_ctrl;
4769 int workaround, port_a;
4770 int current_link_up;
4771
4772 serdes_cfg = 0;
4773 expected_sg_dig_ctrl = 0;
4774 workaround = 0;
4775 port_a = 1;
4776 current_link_up = 0;
4777
4778 if (tp->pci_chip_rev_id != CHIPREV_ID_5704_A0 &&
4779 tp->pci_chip_rev_id != CHIPREV_ID_5704_A1) {
4780 workaround = 1;
4781 if (tr32(TG3PCI_DUAL_MAC_CTRL) & DUAL_MAC_CTRL_ID)
4782 port_a = 0;
4783
4784 /* preserve bits 0-11,13,14 for signal pre-emphasis */
4785 /* preserve bits 20-23 for voltage regulator */
4786 serdes_cfg = tr32(MAC_SERDES_CFG) & 0x00f06fff;
4787 }
4788
4789 sg_dig_ctrl = tr32(SG_DIG_CTRL);
4790
4791 if (tp->link_config.autoneg != AUTONEG_ENABLE) {
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004792 if (sg_dig_ctrl & SG_DIG_USING_HW_AUTONEG) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004793 if (workaround) {
4794 u32 val = serdes_cfg;
4795
4796 if (port_a)
4797 val |= 0xc010000;
4798 else
4799 val |= 0x4010000;
4800 tw32_f(MAC_SERDES_CFG, val);
4801 }
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004802
4803 tw32_f(SG_DIG_CTRL, SG_DIG_COMMON_SETUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004804 }
4805 if (mac_status & MAC_STATUS_PCS_SYNCED) {
4806 tg3_setup_flow_control(tp, 0, 0);
4807 current_link_up = 1;
4808 }
4809 goto out;
4810 }
4811
4812 /* Want auto-negotiation. */
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004813 expected_sg_dig_ctrl = SG_DIG_USING_HW_AUTONEG | SG_DIG_COMMON_SETUP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004814
Matt Carlson82cd3d12007-12-20 20:09:00 -08004815 flowctrl = tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
4816 if (flowctrl & ADVERTISE_1000XPAUSE)
4817 expected_sg_dig_ctrl |= SG_DIG_PAUSE_CAP;
4818 if (flowctrl & ADVERTISE_1000XPSE_ASYM)
4819 expected_sg_dig_ctrl |= SG_DIG_ASYM_PAUSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004820
4821 if (sg_dig_ctrl != expected_sg_dig_ctrl) {
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004822 if ((tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT) &&
Michael Chan3d3ebe72006-09-27 15:59:15 -07004823 tp->serdes_counter &&
4824 ((mac_status & (MAC_STATUS_PCS_SYNCED |
4825 MAC_STATUS_RCVD_CFG)) ==
4826 MAC_STATUS_PCS_SYNCED)) {
4827 tp->serdes_counter--;
4828 current_link_up = 1;
4829 goto out;
4830 }
4831restart_autoneg:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004832 if (workaround)
4833 tw32_f(MAC_SERDES_CFG, serdes_cfg | 0xc011000);
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004834 tw32_f(SG_DIG_CTRL, expected_sg_dig_ctrl | SG_DIG_SOFT_RESET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004835 udelay(5);
4836 tw32_f(SG_DIG_CTRL, expected_sg_dig_ctrl);
4837
Michael Chan3d3ebe72006-09-27 15:59:15 -07004838 tp->serdes_counter = SERDES_AN_TIMEOUT_5704S;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004839 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004840 } else if (mac_status & (MAC_STATUS_PCS_SYNCED |
4841 MAC_STATUS_SIGNAL_DET)) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07004842 sg_dig_status = tr32(SG_DIG_STATUS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004843 mac_status = tr32(MAC_STATUS);
4844
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004845 if ((sg_dig_status & SG_DIG_AUTONEG_COMPLETE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004846 (mac_status & MAC_STATUS_PCS_SYNCED)) {
Matt Carlson82cd3d12007-12-20 20:09:00 -08004847 u32 local_adv = 0, remote_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004848
Matt Carlson82cd3d12007-12-20 20:09:00 -08004849 if (sg_dig_ctrl & SG_DIG_PAUSE_CAP)
4850 local_adv |= ADVERTISE_1000XPAUSE;
4851 if (sg_dig_ctrl & SG_DIG_ASYM_PAUSE)
4852 local_adv |= ADVERTISE_1000XPSE_ASYM;
4853
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004854 if (sg_dig_status & SG_DIG_PARTNER_PAUSE_CAPABLE)
Matt Carlson82cd3d12007-12-20 20:09:00 -08004855 remote_adv |= LPA_1000XPAUSE;
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004856 if (sg_dig_status & SG_DIG_PARTNER_ASYM_PAUSE)
Matt Carlson82cd3d12007-12-20 20:09:00 -08004857 remote_adv |= LPA_1000XPAUSE_ASYM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004858
Matt Carlson859edb22011-12-08 14:40:16 +00004859 tp->link_config.rmt_adv =
4860 mii_adv_to_ethtool_adv_x(remote_adv);
4861
Linus Torvalds1da177e2005-04-16 15:20:36 -07004862 tg3_setup_flow_control(tp, local_adv, remote_adv);
4863 current_link_up = 1;
Michael Chan3d3ebe72006-09-27 15:59:15 -07004864 tp->serdes_counter = 0;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004865 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004866 } else if (!(sg_dig_status & SG_DIG_AUTONEG_COMPLETE)) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07004867 if (tp->serdes_counter)
4868 tp->serdes_counter--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004869 else {
4870 if (workaround) {
4871 u32 val = serdes_cfg;
4872
4873 if (port_a)
4874 val |= 0xc010000;
4875 else
4876 val |= 0x4010000;
4877
4878 tw32_f(MAC_SERDES_CFG, val);
4879 }
4880
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004881 tw32_f(SG_DIG_CTRL, SG_DIG_COMMON_SETUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004882 udelay(40);
4883
4884 /* Link parallel detection - link is up */
4885 /* only if we have PCS_SYNC and not */
4886 /* receiving config code words */
4887 mac_status = tr32(MAC_STATUS);
4888 if ((mac_status & MAC_STATUS_PCS_SYNCED) &&
4889 !(mac_status & MAC_STATUS_RCVD_CFG)) {
4890 tg3_setup_flow_control(tp, 0, 0);
4891 current_link_up = 1;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004892 tp->phy_flags |=
4893 TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan3d3ebe72006-09-27 15:59:15 -07004894 tp->serdes_counter =
4895 SERDES_PARALLEL_DET_TIMEOUT;
4896 } else
4897 goto restart_autoneg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004898 }
4899 }
Michael Chan3d3ebe72006-09-27 15:59:15 -07004900 } else {
4901 tp->serdes_counter = SERDES_AN_TIMEOUT_5704S;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004902 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004903 }
4904
4905out:
4906 return current_link_up;
4907}
4908
4909static int tg3_setup_fiber_by_hand(struct tg3 *tp, u32 mac_status)
4910{
4911 int current_link_up = 0;
4912
Michael Chan5cf64b8a2007-05-05 12:11:21 -07004913 if (!(mac_status & MAC_STATUS_PCS_SYNCED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004914 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004915
4916 if (tp->link_config.autoneg == AUTONEG_ENABLE) {
Matt Carlson5be73b42007-12-20 20:09:29 -08004917 u32 txflags, rxflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004918 int i;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04004919
Matt Carlson5be73b42007-12-20 20:09:29 -08004920 if (fiber_autoneg(tp, &txflags, &rxflags)) {
4921 u32 local_adv = 0, remote_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004922
Matt Carlson5be73b42007-12-20 20:09:29 -08004923 if (txflags & ANEG_CFG_PS1)
4924 local_adv |= ADVERTISE_1000XPAUSE;
4925 if (txflags & ANEG_CFG_PS2)
4926 local_adv |= ADVERTISE_1000XPSE_ASYM;
4927
4928 if (rxflags & MR_LP_ADV_SYM_PAUSE)
4929 remote_adv |= LPA_1000XPAUSE;
4930 if (rxflags & MR_LP_ADV_ASYM_PAUSE)
4931 remote_adv |= LPA_1000XPAUSE_ASYM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004932
Matt Carlson859edb22011-12-08 14:40:16 +00004933 tp->link_config.rmt_adv =
4934 mii_adv_to_ethtool_adv_x(remote_adv);
4935
Linus Torvalds1da177e2005-04-16 15:20:36 -07004936 tg3_setup_flow_control(tp, local_adv, remote_adv);
4937
Linus Torvalds1da177e2005-04-16 15:20:36 -07004938 current_link_up = 1;
4939 }
4940 for (i = 0; i < 30; i++) {
4941 udelay(20);
4942 tw32_f(MAC_STATUS,
4943 (MAC_STATUS_SYNC_CHANGED |
4944 MAC_STATUS_CFG_CHANGED));
4945 udelay(40);
4946 if ((tr32(MAC_STATUS) &
4947 (MAC_STATUS_SYNC_CHANGED |
4948 MAC_STATUS_CFG_CHANGED)) == 0)
4949 break;
4950 }
4951
4952 mac_status = tr32(MAC_STATUS);
4953 if (current_link_up == 0 &&
4954 (mac_status & MAC_STATUS_PCS_SYNCED) &&
4955 !(mac_status & MAC_STATUS_RCVD_CFG))
4956 current_link_up = 1;
4957 } else {
Matt Carlson5be73b42007-12-20 20:09:29 -08004958 tg3_setup_flow_control(tp, 0, 0);
4959
Linus Torvalds1da177e2005-04-16 15:20:36 -07004960 /* Forcing 1000FD link up. */
4961 current_link_up = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004962
4963 tw32_f(MAC_MODE, (tp->mac_mode | MAC_MODE_SEND_CONFIGS));
4964 udelay(40);
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07004965
4966 tw32_f(MAC_MODE, tp->mac_mode);
4967 udelay(40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004968 }
4969
4970out:
4971 return current_link_up;
4972}
4973
4974static int tg3_setup_fiber_phy(struct tg3 *tp, int force_reset)
4975{
4976 u32 orig_pause_cfg;
4977 u16 orig_active_speed;
4978 u8 orig_active_duplex;
4979 u32 mac_status;
4980 int current_link_up;
4981 int i;
4982
Matt Carlson8d018622007-12-20 20:05:44 -08004983 orig_pause_cfg = tp->link_config.active_flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004984 orig_active_speed = tp->link_config.active_speed;
4985 orig_active_duplex = tp->link_config.active_duplex;
4986
Joe Perches63c3a662011-04-26 08:12:10 +00004987 if (!tg3_flag(tp, HW_AUTONEG) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004988 netif_carrier_ok(tp->dev) &&
Joe Perches63c3a662011-04-26 08:12:10 +00004989 tg3_flag(tp, INIT_COMPLETE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004990 mac_status = tr32(MAC_STATUS);
4991 mac_status &= (MAC_STATUS_PCS_SYNCED |
4992 MAC_STATUS_SIGNAL_DET |
4993 MAC_STATUS_CFG_CHANGED |
4994 MAC_STATUS_RCVD_CFG);
4995 if (mac_status == (MAC_STATUS_PCS_SYNCED |
4996 MAC_STATUS_SIGNAL_DET)) {
4997 tw32_f(MAC_STATUS, (MAC_STATUS_SYNC_CHANGED |
4998 MAC_STATUS_CFG_CHANGED));
4999 return 0;
5000 }
5001 }
5002
5003 tw32_f(MAC_TX_AUTO_NEG, 0);
5004
5005 tp->mac_mode &= ~(MAC_MODE_PORT_MODE_MASK | MAC_MODE_HALF_DUPLEX);
5006 tp->mac_mode |= MAC_MODE_PORT_MODE_TBI;
5007 tw32_f(MAC_MODE, tp->mac_mode);
5008 udelay(40);
5009
Matt Carlson79eb6902010-02-17 15:17:03 +00005010 if (tp->phy_id == TG3_PHY_ID_BCM8002)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005011 tg3_init_bcm8002(tp);
5012
5013 /* Enable link change event even when serdes polling. */
5014 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
5015 udelay(40);
5016
5017 current_link_up = 0;
Matt Carlson859edb22011-12-08 14:40:16 +00005018 tp->link_config.rmt_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005019 mac_status = tr32(MAC_STATUS);
5020
Joe Perches63c3a662011-04-26 08:12:10 +00005021 if (tg3_flag(tp, HW_AUTONEG))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005022 current_link_up = tg3_setup_fiber_hw_autoneg(tp, mac_status);
5023 else
5024 current_link_up = tg3_setup_fiber_by_hand(tp, mac_status);
5025
Matt Carlson898a56f2009-08-28 14:02:40 +00005026 tp->napi[0].hw_status->status =
Linus Torvalds1da177e2005-04-16 15:20:36 -07005027 (SD_STATUS_UPDATED |
Matt Carlson898a56f2009-08-28 14:02:40 +00005028 (tp->napi[0].hw_status->status & ~SD_STATUS_LINK_CHG));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005029
5030 for (i = 0; i < 100; i++) {
5031 tw32_f(MAC_STATUS, (MAC_STATUS_SYNC_CHANGED |
5032 MAC_STATUS_CFG_CHANGED));
5033 udelay(5);
5034 if ((tr32(MAC_STATUS) & (MAC_STATUS_SYNC_CHANGED |
Michael Chan3d3ebe72006-09-27 15:59:15 -07005035 MAC_STATUS_CFG_CHANGED |
5036 MAC_STATUS_LNKSTATE_CHANGED)) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005037 break;
5038 }
5039
5040 mac_status = tr32(MAC_STATUS);
5041 if ((mac_status & MAC_STATUS_PCS_SYNCED) == 0) {
5042 current_link_up = 0;
Michael Chan3d3ebe72006-09-27 15:59:15 -07005043 if (tp->link_config.autoneg == AUTONEG_ENABLE &&
5044 tp->serdes_counter == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005045 tw32_f(MAC_MODE, (tp->mac_mode |
5046 MAC_MODE_SEND_CONFIGS));
5047 udelay(1);
5048 tw32_f(MAC_MODE, tp->mac_mode);
5049 }
5050 }
5051
5052 if (current_link_up == 1) {
5053 tp->link_config.active_speed = SPEED_1000;
5054 tp->link_config.active_duplex = DUPLEX_FULL;
5055 tw32(MAC_LED_CTRL, (tp->led_ctrl |
5056 LED_CTRL_LNKLED_OVERRIDE |
5057 LED_CTRL_1000MBPS_ON));
5058 } else {
5059 tp->link_config.active_speed = SPEED_INVALID;
5060 tp->link_config.active_duplex = DUPLEX_INVALID;
5061 tw32(MAC_LED_CTRL, (tp->led_ctrl |
5062 LED_CTRL_LNKLED_OVERRIDE |
5063 LED_CTRL_TRAFFIC_OVERRIDE));
5064 }
5065
5066 if (current_link_up != netif_carrier_ok(tp->dev)) {
5067 if (current_link_up)
5068 netif_carrier_on(tp->dev);
5069 else
5070 netif_carrier_off(tp->dev);
5071 tg3_link_report(tp);
5072 } else {
Matt Carlson8d018622007-12-20 20:05:44 -08005073 u32 now_pause_cfg = tp->link_config.active_flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005074 if (orig_pause_cfg != now_pause_cfg ||
5075 orig_active_speed != tp->link_config.active_speed ||
5076 orig_active_duplex != tp->link_config.active_duplex)
5077 tg3_link_report(tp);
5078 }
5079
5080 return 0;
5081}
5082
Michael Chan747e8f82005-07-25 12:33:22 -07005083static int tg3_setup_fiber_mii_phy(struct tg3 *tp, int force_reset)
5084{
5085 int current_link_up, err = 0;
5086 u32 bmsr, bmcr;
5087 u16 current_speed;
5088 u8 current_duplex;
Matt Carlsonef167e22007-12-20 20:10:01 -08005089 u32 local_adv, remote_adv;
Michael Chan747e8f82005-07-25 12:33:22 -07005090
5091 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
5092 tw32_f(MAC_MODE, tp->mac_mode);
5093 udelay(40);
5094
5095 tw32(MAC_EVENT, 0);
5096
5097 tw32_f(MAC_STATUS,
5098 (MAC_STATUS_SYNC_CHANGED |
5099 MAC_STATUS_CFG_CHANGED |
5100 MAC_STATUS_MI_COMPLETION |
5101 MAC_STATUS_LNKSTATE_CHANGED));
5102 udelay(40);
5103
5104 if (force_reset)
5105 tg3_phy_reset(tp);
5106
5107 current_link_up = 0;
5108 current_speed = SPEED_INVALID;
5109 current_duplex = DUPLEX_INVALID;
Matt Carlson859edb22011-12-08 14:40:16 +00005110 tp->link_config.rmt_adv = 0;
Michael Chan747e8f82005-07-25 12:33:22 -07005111
5112 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
5113 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
Michael Chand4d2c552006-03-20 17:47:20 -08005114 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
5115 if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
5116 bmsr |= BMSR_LSTATUS;
5117 else
5118 bmsr &= ~BMSR_LSTATUS;
5119 }
Michael Chan747e8f82005-07-25 12:33:22 -07005120
5121 err |= tg3_readphy(tp, MII_BMCR, &bmcr);
5122
5123 if ((tp->link_config.autoneg == AUTONEG_ENABLE) && !force_reset &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005124 (tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT)) {
Michael Chan747e8f82005-07-25 12:33:22 -07005125 /* do nothing, just check for link up at the end */
5126 } else if (tp->link_config.autoneg == AUTONEG_ENABLE) {
Matt Carlson28011cf2011-11-16 18:36:59 -05005127 u32 adv, newadv;
Michael Chan747e8f82005-07-25 12:33:22 -07005128
5129 err |= tg3_readphy(tp, MII_ADVERTISE, &adv);
Matt Carlson28011cf2011-11-16 18:36:59 -05005130 newadv = adv & ~(ADVERTISE_1000XFULL | ADVERTISE_1000XHALF |
5131 ADVERTISE_1000XPAUSE |
5132 ADVERTISE_1000XPSE_ASYM |
5133 ADVERTISE_SLCT);
Michael Chan747e8f82005-07-25 12:33:22 -07005134
Matt Carlson28011cf2011-11-16 18:36:59 -05005135 newadv |= tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
Matt Carlson37f07022011-11-17 14:30:55 +00005136 newadv |= ethtool_adv_to_mii_adv_x(tp->link_config.advertising);
Michael Chan747e8f82005-07-25 12:33:22 -07005137
Matt Carlson28011cf2011-11-16 18:36:59 -05005138 if ((newadv != adv) || !(bmcr & BMCR_ANENABLE)) {
5139 tg3_writephy(tp, MII_ADVERTISE, newadv);
Michael Chan747e8f82005-07-25 12:33:22 -07005140 bmcr |= BMCR_ANENABLE | BMCR_ANRESTART;
5141 tg3_writephy(tp, MII_BMCR, bmcr);
5142
5143 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
Michael Chan3d3ebe72006-09-27 15:59:15 -07005144 tp->serdes_counter = SERDES_AN_TIMEOUT_5714S;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005145 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07005146
5147 return err;
5148 }
5149 } else {
5150 u32 new_bmcr;
5151
5152 bmcr &= ~BMCR_SPEED1000;
5153 new_bmcr = bmcr & ~(BMCR_ANENABLE | BMCR_FULLDPLX);
5154
5155 if (tp->link_config.duplex == DUPLEX_FULL)
5156 new_bmcr |= BMCR_FULLDPLX;
5157
5158 if (new_bmcr != bmcr) {
5159 /* BMCR_SPEED1000 is a reserved bit that needs
5160 * to be set on write.
5161 */
5162 new_bmcr |= BMCR_SPEED1000;
5163
5164 /* Force a linkdown */
5165 if (netif_carrier_ok(tp->dev)) {
5166 u32 adv;
5167
5168 err |= tg3_readphy(tp, MII_ADVERTISE, &adv);
5169 adv &= ~(ADVERTISE_1000XFULL |
5170 ADVERTISE_1000XHALF |
5171 ADVERTISE_SLCT);
5172 tg3_writephy(tp, MII_ADVERTISE, adv);
5173 tg3_writephy(tp, MII_BMCR, bmcr |
5174 BMCR_ANRESTART |
5175 BMCR_ANENABLE);
5176 udelay(10);
5177 netif_carrier_off(tp->dev);
5178 }
5179 tg3_writephy(tp, MII_BMCR, new_bmcr);
5180 bmcr = new_bmcr;
5181 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
5182 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
Michael Chand4d2c552006-03-20 17:47:20 -08005183 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
5184 ASIC_REV_5714) {
5185 if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
5186 bmsr |= BMSR_LSTATUS;
5187 else
5188 bmsr &= ~BMSR_LSTATUS;
5189 }
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005190 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07005191 }
5192 }
5193
5194 if (bmsr & BMSR_LSTATUS) {
5195 current_speed = SPEED_1000;
5196 current_link_up = 1;
5197 if (bmcr & BMCR_FULLDPLX)
5198 current_duplex = DUPLEX_FULL;
5199 else
5200 current_duplex = DUPLEX_HALF;
5201
Matt Carlsonef167e22007-12-20 20:10:01 -08005202 local_adv = 0;
5203 remote_adv = 0;
5204
Michael Chan747e8f82005-07-25 12:33:22 -07005205 if (bmcr & BMCR_ANENABLE) {
Matt Carlsonef167e22007-12-20 20:10:01 -08005206 u32 common;
Michael Chan747e8f82005-07-25 12:33:22 -07005207
5208 err |= tg3_readphy(tp, MII_ADVERTISE, &local_adv);
5209 err |= tg3_readphy(tp, MII_LPA, &remote_adv);
5210 common = local_adv & remote_adv;
5211 if (common & (ADVERTISE_1000XHALF |
5212 ADVERTISE_1000XFULL)) {
5213 if (common & ADVERTISE_1000XFULL)
5214 current_duplex = DUPLEX_FULL;
5215 else
5216 current_duplex = DUPLEX_HALF;
Matt Carlson859edb22011-12-08 14:40:16 +00005217
5218 tp->link_config.rmt_adv =
5219 mii_adv_to_ethtool_adv_x(remote_adv);
Joe Perches63c3a662011-04-26 08:12:10 +00005220 } else if (!tg3_flag(tp, 5780_CLASS)) {
Matt Carlson57d8b882010-06-05 17:24:35 +00005221 /* Link is up via parallel detect */
Matt Carlson859a588792010-04-05 10:19:28 +00005222 } else {
Michael Chan747e8f82005-07-25 12:33:22 -07005223 current_link_up = 0;
Matt Carlson859a588792010-04-05 10:19:28 +00005224 }
Michael Chan747e8f82005-07-25 12:33:22 -07005225 }
5226 }
5227
Matt Carlsonef167e22007-12-20 20:10:01 -08005228 if (current_link_up == 1 && current_duplex == DUPLEX_FULL)
5229 tg3_setup_flow_control(tp, local_adv, remote_adv);
5230
Michael Chan747e8f82005-07-25 12:33:22 -07005231 tp->mac_mode &= ~MAC_MODE_HALF_DUPLEX;
5232 if (tp->link_config.active_duplex == DUPLEX_HALF)
5233 tp->mac_mode |= MAC_MODE_HALF_DUPLEX;
5234
5235 tw32_f(MAC_MODE, tp->mac_mode);
5236 udelay(40);
5237
5238 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
5239
5240 tp->link_config.active_speed = current_speed;
5241 tp->link_config.active_duplex = current_duplex;
5242
5243 if (current_link_up != netif_carrier_ok(tp->dev)) {
5244 if (current_link_up)
5245 netif_carrier_on(tp->dev);
5246 else {
5247 netif_carrier_off(tp->dev);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005248 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07005249 }
5250 tg3_link_report(tp);
5251 }
5252 return err;
5253}
5254
5255static void tg3_serdes_parallel_detect(struct tg3 *tp)
5256{
Michael Chan3d3ebe72006-09-27 15:59:15 -07005257 if (tp->serdes_counter) {
Michael Chan747e8f82005-07-25 12:33:22 -07005258 /* Give autoneg time to complete. */
Michael Chan3d3ebe72006-09-27 15:59:15 -07005259 tp->serdes_counter--;
Michael Chan747e8f82005-07-25 12:33:22 -07005260 return;
5261 }
Matt Carlsonc6cdf432010-04-05 10:19:26 +00005262
Michael Chan747e8f82005-07-25 12:33:22 -07005263 if (!netif_carrier_ok(tp->dev) &&
5264 (tp->link_config.autoneg == AUTONEG_ENABLE)) {
5265 u32 bmcr;
5266
5267 tg3_readphy(tp, MII_BMCR, &bmcr);
5268 if (bmcr & BMCR_ANENABLE) {
5269 u32 phy1, phy2;
5270
5271 /* Select shadow register 0x1f */
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00005272 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x7c00);
5273 tg3_readphy(tp, MII_TG3_MISC_SHDW, &phy1);
Michael Chan747e8f82005-07-25 12:33:22 -07005274
5275 /* Select expansion interrupt status register */
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00005276 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
5277 MII_TG3_DSP_EXP1_INT_STAT);
5278 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &phy2);
5279 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &phy2);
Michael Chan747e8f82005-07-25 12:33:22 -07005280
5281 if ((phy1 & 0x10) && !(phy2 & 0x20)) {
5282 /* We have signal detect and not receiving
5283 * config code words, link is up by parallel
5284 * detection.
5285 */
5286
5287 bmcr &= ~BMCR_ANENABLE;
5288 bmcr |= BMCR_SPEED1000 | BMCR_FULLDPLX;
5289 tg3_writephy(tp, MII_BMCR, bmcr);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005290 tp->phy_flags |= TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07005291 }
5292 }
Matt Carlson859a588792010-04-05 10:19:28 +00005293 } else if (netif_carrier_ok(tp->dev) &&
5294 (tp->link_config.autoneg == AUTONEG_ENABLE) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005295 (tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT)) {
Michael Chan747e8f82005-07-25 12:33:22 -07005296 u32 phy2;
5297
5298 /* Select expansion interrupt status register */
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00005299 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
5300 MII_TG3_DSP_EXP1_INT_STAT);
5301 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &phy2);
Michael Chan747e8f82005-07-25 12:33:22 -07005302 if (phy2 & 0x20) {
5303 u32 bmcr;
5304
5305 /* Config code words received, turn on autoneg. */
5306 tg3_readphy(tp, MII_BMCR, &bmcr);
5307 tg3_writephy(tp, MII_BMCR, bmcr | BMCR_ANENABLE);
5308
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005309 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07005310
5311 }
5312 }
5313}
5314
Linus Torvalds1da177e2005-04-16 15:20:36 -07005315static int tg3_setup_phy(struct tg3 *tp, int force_reset)
5316{
Matt Carlsonf2096f92011-04-05 14:22:48 +00005317 u32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005318 int err;
5319
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005320 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005321 err = tg3_setup_fiber_phy(tp, force_reset);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005322 else if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
Michael Chan747e8f82005-07-25 12:33:22 -07005323 err = tg3_setup_fiber_mii_phy(tp, force_reset);
Matt Carlson859a588792010-04-05 10:19:28 +00005324 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07005325 err = tg3_setup_copper_phy(tp, force_reset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005326
Matt Carlsonbcb37f62008-11-03 16:52:09 -08005327 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX) {
Matt Carlsonf2096f92011-04-05 14:22:48 +00005328 u32 scale;
Matt Carlsonaa6c91f2007-11-12 21:18:04 -08005329
5330 val = tr32(TG3_CPMU_CLCK_STAT) & CPMU_CLCK_STAT_MAC_CLCK_MASK;
5331 if (val == CPMU_CLCK_STAT_MAC_CLCK_62_5)
5332 scale = 65;
5333 else if (val == CPMU_CLCK_STAT_MAC_CLCK_6_25)
5334 scale = 6;
5335 else
5336 scale = 12;
5337
5338 val = tr32(GRC_MISC_CFG) & ~GRC_MISC_CFG_PRESCALAR_MASK;
5339 val |= (scale << GRC_MISC_CFG_PRESCALAR_SHIFT);
5340 tw32(GRC_MISC_CFG, val);
5341 }
5342
Matt Carlsonf2096f92011-04-05 14:22:48 +00005343 val = (2 << TX_LENGTHS_IPG_CRS_SHIFT) |
5344 (6 << TX_LENGTHS_IPG_SHIFT);
5345 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
5346 val |= tr32(MAC_TX_LENGTHS) &
5347 (TX_LENGTHS_JMB_FRM_LEN_MSK |
5348 TX_LENGTHS_CNT_DWN_VAL_MSK);
5349
Linus Torvalds1da177e2005-04-16 15:20:36 -07005350 if (tp->link_config.active_speed == SPEED_1000 &&
5351 tp->link_config.active_duplex == DUPLEX_HALF)
Matt Carlsonf2096f92011-04-05 14:22:48 +00005352 tw32(MAC_TX_LENGTHS, val |
5353 (0xff << TX_LENGTHS_SLOT_TIME_SHIFT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005354 else
Matt Carlsonf2096f92011-04-05 14:22:48 +00005355 tw32(MAC_TX_LENGTHS, val |
5356 (32 << TX_LENGTHS_SLOT_TIME_SHIFT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005357
Joe Perches63c3a662011-04-26 08:12:10 +00005358 if (!tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005359 if (netif_carrier_ok(tp->dev)) {
5360 tw32(HOSTCC_STAT_COAL_TICKS,
David S. Miller15f98502005-05-18 22:49:26 -07005361 tp->coal.stats_block_coalesce_usecs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005362 } else {
5363 tw32(HOSTCC_STAT_COAL_TICKS, 0);
5364 }
5365 }
5366
Joe Perches63c3a662011-04-26 08:12:10 +00005367 if (tg3_flag(tp, ASPM_WORKAROUND)) {
Matt Carlsonf2096f92011-04-05 14:22:48 +00005368 val = tr32(PCIE_PWR_MGMT_THRESH);
Matt Carlson8ed5d972007-05-07 00:25:49 -07005369 if (!netif_carrier_ok(tp->dev))
5370 val = (val & ~PCIE_PWR_MGMT_L1_THRESH_MSK) |
5371 tp->pwrmgmt_thresh;
5372 else
5373 val |= PCIE_PWR_MGMT_L1_THRESH_MSK;
5374 tw32(PCIE_PWR_MGMT_THRESH, val);
5375 }
5376
Linus Torvalds1da177e2005-04-16 15:20:36 -07005377 return err;
5378}
5379
Matt Carlson66cfd1b2010-09-30 10:34:30 +00005380static inline int tg3_irq_sync(struct tg3 *tp)
5381{
5382 return tp->irq_sync;
5383}
5384
Matt Carlson97bd8e42011-04-13 11:05:04 +00005385static inline void tg3_rd32_loop(struct tg3 *tp, u32 *dst, u32 off, u32 len)
5386{
5387 int i;
5388
5389 dst = (u32 *)((u8 *)dst + off);
5390 for (i = 0; i < len; i += sizeof(u32))
5391 *dst++ = tr32(off + i);
5392}
5393
5394static void tg3_dump_legacy_regs(struct tg3 *tp, u32 *regs)
5395{
5396 tg3_rd32_loop(tp, regs, TG3PCI_VENDOR, 0xb0);
5397 tg3_rd32_loop(tp, regs, MAILBOX_INTERRUPT_0, 0x200);
5398 tg3_rd32_loop(tp, regs, MAC_MODE, 0x4f0);
5399 tg3_rd32_loop(tp, regs, SNDDATAI_MODE, 0xe0);
5400 tg3_rd32_loop(tp, regs, SNDDATAC_MODE, 0x04);
5401 tg3_rd32_loop(tp, regs, SNDBDS_MODE, 0x80);
5402 tg3_rd32_loop(tp, regs, SNDBDI_MODE, 0x48);
5403 tg3_rd32_loop(tp, regs, SNDBDC_MODE, 0x04);
5404 tg3_rd32_loop(tp, regs, RCVLPC_MODE, 0x20);
5405 tg3_rd32_loop(tp, regs, RCVLPC_SELLST_BASE, 0x15c);
5406 tg3_rd32_loop(tp, regs, RCVDBDI_MODE, 0x0c);
5407 tg3_rd32_loop(tp, regs, RCVDBDI_JUMBO_BD, 0x3c);
5408 tg3_rd32_loop(tp, regs, RCVDBDI_BD_PROD_IDX_0, 0x44);
5409 tg3_rd32_loop(tp, regs, RCVDCC_MODE, 0x04);
5410 tg3_rd32_loop(tp, regs, RCVBDI_MODE, 0x20);
5411 tg3_rd32_loop(tp, regs, RCVCC_MODE, 0x14);
5412 tg3_rd32_loop(tp, regs, RCVLSC_MODE, 0x08);
5413 tg3_rd32_loop(tp, regs, MBFREE_MODE, 0x08);
5414 tg3_rd32_loop(tp, regs, HOSTCC_MODE, 0x100);
5415
Joe Perches63c3a662011-04-26 08:12:10 +00005416 if (tg3_flag(tp, SUPPORT_MSIX))
Matt Carlson97bd8e42011-04-13 11:05:04 +00005417 tg3_rd32_loop(tp, regs, HOSTCC_RXCOL_TICKS_VEC1, 0x180);
5418
5419 tg3_rd32_loop(tp, regs, MEMARB_MODE, 0x10);
5420 tg3_rd32_loop(tp, regs, BUFMGR_MODE, 0x58);
5421 tg3_rd32_loop(tp, regs, RDMAC_MODE, 0x08);
5422 tg3_rd32_loop(tp, regs, WDMAC_MODE, 0x08);
5423 tg3_rd32_loop(tp, regs, RX_CPU_MODE, 0x04);
5424 tg3_rd32_loop(tp, regs, RX_CPU_STATE, 0x04);
5425 tg3_rd32_loop(tp, regs, RX_CPU_PGMCTR, 0x04);
5426 tg3_rd32_loop(tp, regs, RX_CPU_HWBKPT, 0x04);
5427
Joe Perches63c3a662011-04-26 08:12:10 +00005428 if (!tg3_flag(tp, 5705_PLUS)) {
Matt Carlson97bd8e42011-04-13 11:05:04 +00005429 tg3_rd32_loop(tp, regs, TX_CPU_MODE, 0x04);
5430 tg3_rd32_loop(tp, regs, TX_CPU_STATE, 0x04);
5431 tg3_rd32_loop(tp, regs, TX_CPU_PGMCTR, 0x04);
5432 }
5433
5434 tg3_rd32_loop(tp, regs, GRCMBOX_INTERRUPT_0, 0x110);
5435 tg3_rd32_loop(tp, regs, FTQ_RESET, 0x120);
5436 tg3_rd32_loop(tp, regs, MSGINT_MODE, 0x0c);
5437 tg3_rd32_loop(tp, regs, DMAC_MODE, 0x04);
5438 tg3_rd32_loop(tp, regs, GRC_MODE, 0x4c);
5439
Joe Perches63c3a662011-04-26 08:12:10 +00005440 if (tg3_flag(tp, NVRAM))
Matt Carlson97bd8e42011-04-13 11:05:04 +00005441 tg3_rd32_loop(tp, regs, NVRAM_CMD, 0x24);
5442}
5443
5444static void tg3_dump_state(struct tg3 *tp)
5445{
5446 int i;
5447 u32 *regs;
5448
5449 regs = kzalloc(TG3_REG_BLK_SIZE, GFP_ATOMIC);
5450 if (!regs) {
5451 netdev_err(tp->dev, "Failed allocating register dump buffer\n");
5452 return;
5453 }
5454
Joe Perches63c3a662011-04-26 08:12:10 +00005455 if (tg3_flag(tp, PCI_EXPRESS)) {
Matt Carlson97bd8e42011-04-13 11:05:04 +00005456 /* Read up to but not including private PCI registers */
5457 for (i = 0; i < TG3_PCIE_TLDLPL_PORT; i += sizeof(u32))
5458 regs[i / sizeof(u32)] = tr32(i);
5459 } else
5460 tg3_dump_legacy_regs(tp, regs);
5461
5462 for (i = 0; i < TG3_REG_BLK_SIZE / sizeof(u32); i += 4) {
5463 if (!regs[i + 0] && !regs[i + 1] &&
5464 !regs[i + 2] && !regs[i + 3])
5465 continue;
5466
5467 netdev_err(tp->dev, "0x%08x: 0x%08x, 0x%08x, 0x%08x, 0x%08x\n",
5468 i * 4,
5469 regs[i + 0], regs[i + 1], regs[i + 2], regs[i + 3]);
5470 }
5471
5472 kfree(regs);
5473
5474 for (i = 0; i < tp->irq_cnt; i++) {
5475 struct tg3_napi *tnapi = &tp->napi[i];
5476
5477 /* SW status block */
5478 netdev_err(tp->dev,
5479 "%d: Host status block [%08x:%08x:(%04x:%04x:%04x):(%04x:%04x)]\n",
5480 i,
5481 tnapi->hw_status->status,
5482 tnapi->hw_status->status_tag,
5483 tnapi->hw_status->rx_jumbo_consumer,
5484 tnapi->hw_status->rx_consumer,
5485 tnapi->hw_status->rx_mini_consumer,
5486 tnapi->hw_status->idx[0].rx_producer,
5487 tnapi->hw_status->idx[0].tx_consumer);
5488
5489 netdev_err(tp->dev,
5490 "%d: NAPI info [%08x:%08x:(%04x:%04x:%04x):%04x:(%04x:%04x:%04x:%04x)]\n",
5491 i,
5492 tnapi->last_tag, tnapi->last_irq_tag,
5493 tnapi->tx_prod, tnapi->tx_cons, tnapi->tx_pending,
5494 tnapi->rx_rcb_ptr,
5495 tnapi->prodring.rx_std_prod_idx,
5496 tnapi->prodring.rx_std_cons_idx,
5497 tnapi->prodring.rx_jmb_prod_idx,
5498 tnapi->prodring.rx_jmb_cons_idx);
5499 }
5500}
5501
Michael Chandf3e6542006-05-26 17:48:07 -07005502/* This is called whenever we suspect that the system chipset is re-
5503 * ordering the sequence of MMIO to the tx send mailbox. The symptom
5504 * is bogus tx completions. We try to recover by setting the
5505 * TG3_FLAG_MBOX_WRITE_REORDER flag and resetting the chip later
5506 * in the workqueue.
5507 */
5508static void tg3_tx_recover(struct tg3 *tp)
5509{
Joe Perches63c3a662011-04-26 08:12:10 +00005510 BUG_ON(tg3_flag(tp, MBOX_WRITE_REORDER) ||
Michael Chandf3e6542006-05-26 17:48:07 -07005511 tp->write32_tx_mbox == tg3_write_indirect_mbox);
5512
Matt Carlson5129c3a2010-04-05 10:19:23 +00005513 netdev_warn(tp->dev,
5514 "The system may be re-ordering memory-mapped I/O "
5515 "cycles to the network device, attempting to recover. "
5516 "Please report the problem to the driver maintainer "
5517 "and include system chipset information.\n");
Michael Chandf3e6542006-05-26 17:48:07 -07005518
5519 spin_lock(&tp->lock);
Joe Perches63c3a662011-04-26 08:12:10 +00005520 tg3_flag_set(tp, TX_RECOVERY_PENDING);
Michael Chandf3e6542006-05-26 17:48:07 -07005521 spin_unlock(&tp->lock);
5522}
5523
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005524static inline u32 tg3_tx_avail(struct tg3_napi *tnapi)
Michael Chan1b2a7202006-08-07 21:46:02 -07005525{
Matt Carlsonf65aac12010-08-02 11:26:03 +00005526 /* Tell compiler to fetch tx indices from memory. */
5527 barrier();
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005528 return tnapi->tx_pending -
5529 ((tnapi->tx_prod - tnapi->tx_cons) & (TG3_TX_RING_SIZE - 1));
Michael Chan1b2a7202006-08-07 21:46:02 -07005530}
5531
Linus Torvalds1da177e2005-04-16 15:20:36 -07005532/* Tigon3 never reports partial packet sends. So we do not
5533 * need special logic to handle SKBs that have not had all
5534 * of their frags sent yet, like SunGEM does.
5535 */
Matt Carlson17375d22009-08-28 14:02:18 +00005536static void tg3_tx(struct tg3_napi *tnapi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005537{
Matt Carlson17375d22009-08-28 14:02:18 +00005538 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00005539 u32 hw_idx = tnapi->hw_status->idx[0].tx_consumer;
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005540 u32 sw_idx = tnapi->tx_cons;
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005541 struct netdev_queue *txq;
5542 int index = tnapi - tp->napi;
Tom Herbert298376d2011-11-28 16:33:30 +00005543 unsigned int pkts_compl = 0, bytes_compl = 0;
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005544
Joe Perches63c3a662011-04-26 08:12:10 +00005545 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005546 index--;
5547
5548 txq = netdev_get_tx_queue(tp->dev, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005549
5550 while (sw_idx != hw_idx) {
Matt Carlsondf8944c2011-07-27 14:20:46 +00005551 struct tg3_tx_ring_info *ri = &tnapi->tx_buffers[sw_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07005552 struct sk_buff *skb = ri->skb;
Michael Chandf3e6542006-05-26 17:48:07 -07005553 int i, tx_bug = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005554
Michael Chandf3e6542006-05-26 17:48:07 -07005555 if (unlikely(skb == NULL)) {
5556 tg3_tx_recover(tp);
5557 return;
5558 }
5559
Alexander Duyckf4188d82009-12-02 16:48:38 +00005560 pci_unmap_single(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005561 dma_unmap_addr(ri, mapping),
Alexander Duyckf4188d82009-12-02 16:48:38 +00005562 skb_headlen(skb),
5563 PCI_DMA_TODEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005564
5565 ri->skb = NULL;
5566
Matt Carlsone01ee142011-07-27 14:20:50 +00005567 while (ri->fragmented) {
5568 ri->fragmented = false;
5569 sw_idx = NEXT_TX(sw_idx);
5570 ri = &tnapi->tx_buffers[sw_idx];
5571 }
5572
Linus Torvalds1da177e2005-04-16 15:20:36 -07005573 sw_idx = NEXT_TX(sw_idx);
5574
5575 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005576 ri = &tnapi->tx_buffers[sw_idx];
Michael Chandf3e6542006-05-26 17:48:07 -07005577 if (unlikely(ri->skb != NULL || sw_idx == hw_idx))
5578 tx_bug = 1;
Alexander Duyckf4188d82009-12-02 16:48:38 +00005579
5580 pci_unmap_page(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005581 dma_unmap_addr(ri, mapping),
Eric Dumazet9e903e02011-10-18 21:00:24 +00005582 skb_frag_size(&skb_shinfo(skb)->frags[i]),
Alexander Duyckf4188d82009-12-02 16:48:38 +00005583 PCI_DMA_TODEVICE);
Matt Carlsone01ee142011-07-27 14:20:50 +00005584
5585 while (ri->fragmented) {
5586 ri->fragmented = false;
5587 sw_idx = NEXT_TX(sw_idx);
5588 ri = &tnapi->tx_buffers[sw_idx];
5589 }
5590
Linus Torvalds1da177e2005-04-16 15:20:36 -07005591 sw_idx = NEXT_TX(sw_idx);
5592 }
5593
Tom Herbert298376d2011-11-28 16:33:30 +00005594 pkts_compl++;
5595 bytes_compl += skb->len;
5596
David S. Millerf47c11e2005-06-24 20:18:35 -07005597 dev_kfree_skb(skb);
Michael Chandf3e6542006-05-26 17:48:07 -07005598
5599 if (unlikely(tx_bug)) {
5600 tg3_tx_recover(tp);
5601 return;
5602 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005603 }
5604
Tom Herbert298376d2011-11-28 16:33:30 +00005605 netdev_completed_queue(tp->dev, pkts_compl, bytes_compl);
5606
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005607 tnapi->tx_cons = sw_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005608
Michael Chan1b2a7202006-08-07 21:46:02 -07005609 /* Need to make the tx_cons update visible to tg3_start_xmit()
5610 * before checking for netif_queue_stopped(). Without the
5611 * memory barrier, there is a small possibility that tg3_start_xmit()
5612 * will miss it and cause the queue to be stopped forever.
5613 */
5614 smp_mb();
5615
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005616 if (unlikely(netif_tx_queue_stopped(txq) &&
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005617 (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi)))) {
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005618 __netif_tx_lock(txq, smp_processor_id());
5619 if (netif_tx_queue_stopped(txq) &&
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005620 (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi)))
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005621 netif_tx_wake_queue(txq);
5622 __netif_tx_unlock(txq);
Michael Chan51b91462005-09-01 17:41:28 -07005623 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005624}
5625
Eric Dumazet9205fd92011-11-18 06:47:01 +00005626static void tg3_rx_data_free(struct tg3 *tp, struct ring_info *ri, u32 map_sz)
Matt Carlson2b2cdb62009-11-13 13:03:48 +00005627{
Eric Dumazet9205fd92011-11-18 06:47:01 +00005628 if (!ri->data)
Matt Carlson2b2cdb62009-11-13 13:03:48 +00005629 return;
5630
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005631 pci_unmap_single(tp->pdev, dma_unmap_addr(ri, mapping),
Matt Carlson2b2cdb62009-11-13 13:03:48 +00005632 map_sz, PCI_DMA_FROMDEVICE);
Eric Dumazet9205fd92011-11-18 06:47:01 +00005633 kfree(ri->data);
5634 ri->data = NULL;
Matt Carlson2b2cdb62009-11-13 13:03:48 +00005635}
5636
Linus Torvalds1da177e2005-04-16 15:20:36 -07005637/* Returns size of skb allocated or < 0 on error.
5638 *
5639 * We only need to fill in the address because the other members
5640 * of the RX descriptor are invariant, see tg3_init_rings.
5641 *
5642 * Note the purposeful assymetry of cpu vs. chip accesses. For
5643 * posting buffers we only dirty the first cache line of the RX
5644 * descriptor (containing the address). Whereas for the RX status
5645 * buffers the cpu only reads the last cacheline of the RX descriptor
5646 * (to fetch the error flags, vlan tag, checksum, and opaque cookie).
5647 */
Eric Dumazet9205fd92011-11-18 06:47:01 +00005648static int tg3_alloc_rx_data(struct tg3 *tp, struct tg3_rx_prodring_set *tpr,
Matt Carlsona3896162009-11-13 13:03:44 +00005649 u32 opaque_key, u32 dest_idx_unmasked)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005650{
5651 struct tg3_rx_buffer_desc *desc;
Matt Carlsonf94e2902010-10-14 10:37:42 +00005652 struct ring_info *map;
Eric Dumazet9205fd92011-11-18 06:47:01 +00005653 u8 *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005654 dma_addr_t mapping;
Eric Dumazet9205fd92011-11-18 06:47:01 +00005655 int skb_size, data_size, dest_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005656
Linus Torvalds1da177e2005-04-16 15:20:36 -07005657 switch (opaque_key) {
5658 case RXD_OPAQUE_RING_STD:
Matt Carlson2c49a442010-09-30 10:34:35 +00005659 dest_idx = dest_idx_unmasked & tp->rx_std_ring_mask;
Matt Carlson21f581a2009-08-28 14:00:25 +00005660 desc = &tpr->rx_std[dest_idx];
5661 map = &tpr->rx_std_buffers[dest_idx];
Eric Dumazet9205fd92011-11-18 06:47:01 +00005662 data_size = tp->rx_pkt_map_sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005663 break;
5664
5665 case RXD_OPAQUE_RING_JUMBO:
Matt Carlson2c49a442010-09-30 10:34:35 +00005666 dest_idx = dest_idx_unmasked & tp->rx_jmb_ring_mask;
Matt Carlson79ed5ac2009-08-28 14:00:55 +00005667 desc = &tpr->rx_jmb[dest_idx].std;
Matt Carlson21f581a2009-08-28 14:00:25 +00005668 map = &tpr->rx_jmb_buffers[dest_idx];
Eric Dumazet9205fd92011-11-18 06:47:01 +00005669 data_size = TG3_RX_JMB_MAP_SZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005670 break;
5671
5672 default:
5673 return -EINVAL;
Stephen Hemminger855e1112008-04-16 16:37:28 -07005674 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005675
5676 /* Do not overwrite any of the map or rp information
5677 * until we are sure we can commit to a new buffer.
5678 *
5679 * Callers depend upon this behavior and assume that
5680 * we leave everything unchanged if we fail.
5681 */
Eric Dumazet9205fd92011-11-18 06:47:01 +00005682 skb_size = SKB_DATA_ALIGN(data_size + TG3_RX_OFFSET(tp)) +
5683 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
5684 data = kmalloc(skb_size, GFP_ATOMIC);
5685 if (!data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005686 return -ENOMEM;
5687
Eric Dumazet9205fd92011-11-18 06:47:01 +00005688 mapping = pci_map_single(tp->pdev,
5689 data + TG3_RX_OFFSET(tp),
5690 data_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005691 PCI_DMA_FROMDEVICE);
Matt Carlsona21771d2009-11-02 14:25:31 +00005692 if (pci_dma_mapping_error(tp->pdev, mapping)) {
Eric Dumazet9205fd92011-11-18 06:47:01 +00005693 kfree(data);
Matt Carlsona21771d2009-11-02 14:25:31 +00005694 return -EIO;
5695 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005696
Eric Dumazet9205fd92011-11-18 06:47:01 +00005697 map->data = data;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005698 dma_unmap_addr_set(map, mapping, mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005699
Linus Torvalds1da177e2005-04-16 15:20:36 -07005700 desc->addr_hi = ((u64)mapping >> 32);
5701 desc->addr_lo = ((u64)mapping & 0xffffffff);
5702
Eric Dumazet9205fd92011-11-18 06:47:01 +00005703 return data_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005704}
5705
5706/* We only need to move over in the address because the other
5707 * members of the RX descriptor are invariant. See notes above
Eric Dumazet9205fd92011-11-18 06:47:01 +00005708 * tg3_alloc_rx_data for full details.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005709 */
Matt Carlsona3896162009-11-13 13:03:44 +00005710static void tg3_recycle_rx(struct tg3_napi *tnapi,
5711 struct tg3_rx_prodring_set *dpr,
5712 u32 opaque_key, int src_idx,
5713 u32 dest_idx_unmasked)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005714{
Matt Carlson17375d22009-08-28 14:02:18 +00005715 struct tg3 *tp = tnapi->tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005716 struct tg3_rx_buffer_desc *src_desc, *dest_desc;
5717 struct ring_info *src_map, *dest_map;
Matt Carlson8fea32b2010-09-15 08:59:58 +00005718 struct tg3_rx_prodring_set *spr = &tp->napi[0].prodring;
Matt Carlsonc6cdf432010-04-05 10:19:26 +00005719 int dest_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005720
5721 switch (opaque_key) {
5722 case RXD_OPAQUE_RING_STD:
Matt Carlson2c49a442010-09-30 10:34:35 +00005723 dest_idx = dest_idx_unmasked & tp->rx_std_ring_mask;
Matt Carlsona3896162009-11-13 13:03:44 +00005724 dest_desc = &dpr->rx_std[dest_idx];
5725 dest_map = &dpr->rx_std_buffers[dest_idx];
5726 src_desc = &spr->rx_std[src_idx];
5727 src_map = &spr->rx_std_buffers[src_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07005728 break;
5729
5730 case RXD_OPAQUE_RING_JUMBO:
Matt Carlson2c49a442010-09-30 10:34:35 +00005731 dest_idx = dest_idx_unmasked & tp->rx_jmb_ring_mask;
Matt Carlsona3896162009-11-13 13:03:44 +00005732 dest_desc = &dpr->rx_jmb[dest_idx].std;
5733 dest_map = &dpr->rx_jmb_buffers[dest_idx];
5734 src_desc = &spr->rx_jmb[src_idx].std;
5735 src_map = &spr->rx_jmb_buffers[src_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07005736 break;
5737
5738 default:
5739 return;
Stephen Hemminger855e1112008-04-16 16:37:28 -07005740 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005741
Eric Dumazet9205fd92011-11-18 06:47:01 +00005742 dest_map->data = src_map->data;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005743 dma_unmap_addr_set(dest_map, mapping,
5744 dma_unmap_addr(src_map, mapping));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005745 dest_desc->addr_hi = src_desc->addr_hi;
5746 dest_desc->addr_lo = src_desc->addr_lo;
Matt Carlsone92967b2010-02-12 14:47:06 +00005747
5748 /* Ensure that the update to the skb happens after the physical
5749 * addresses have been transferred to the new BD location.
5750 */
5751 smp_wmb();
5752
Eric Dumazet9205fd92011-11-18 06:47:01 +00005753 src_map->data = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005754}
5755
Linus Torvalds1da177e2005-04-16 15:20:36 -07005756/* The RX ring scheme is composed of multiple rings which post fresh
5757 * buffers to the chip, and one special ring the chip uses to report
5758 * status back to the host.
5759 *
5760 * The special ring reports the status of received packets to the
5761 * host. The chip does not write into the original descriptor the
5762 * RX buffer was obtained from. The chip simply takes the original
5763 * descriptor as provided by the host, updates the status and length
5764 * field, then writes this into the next status ring entry.
5765 *
5766 * Each ring the host uses to post buffers to the chip is described
5767 * by a TG3_BDINFO entry in the chips SRAM area. When a packet arrives,
5768 * it is first placed into the on-chip ram. When the packet's length
5769 * is known, it walks down the TG3_BDINFO entries to select the ring.
5770 * Each TG3_BDINFO specifies a MAXLEN field and the first TG3_BDINFO
5771 * which is within the range of the new packet's length is chosen.
5772 *
5773 * The "separate ring for rx status" scheme may sound queer, but it makes
5774 * sense from a cache coherency perspective. If only the host writes
5775 * to the buffer post rings, and only the chip writes to the rx status
5776 * rings, then cache lines never move beyond shared-modified state.
5777 * If both the host and chip were to write into the same ring, cache line
5778 * eviction could occur since both entities want it in an exclusive state.
5779 */
Matt Carlson17375d22009-08-28 14:02:18 +00005780static int tg3_rx(struct tg3_napi *tnapi, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005781{
Matt Carlson17375d22009-08-28 14:02:18 +00005782 struct tg3 *tp = tnapi->tp;
Michael Chanf92905d2006-06-29 20:14:29 -07005783 u32 work_mask, rx_std_posted = 0;
Matt Carlson43619352009-11-13 13:03:47 +00005784 u32 std_prod_idx, jmb_prod_idx;
Matt Carlson72334482009-08-28 14:03:01 +00005785 u32 sw_idx = tnapi->rx_rcb_ptr;
Michael Chan483ba502005-04-25 15:14:03 -07005786 u16 hw_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005787 int received;
Matt Carlson8fea32b2010-09-15 08:59:58 +00005788 struct tg3_rx_prodring_set *tpr = &tnapi->prodring;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005789
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00005790 hw_idx = *(tnapi->rx_rcb_prod_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005791 /*
5792 * We need to order the read of hw_idx and the read of
5793 * the opaque cookie.
5794 */
5795 rmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005796 work_mask = 0;
5797 received = 0;
Matt Carlson43619352009-11-13 13:03:47 +00005798 std_prod_idx = tpr->rx_std_prod_idx;
5799 jmb_prod_idx = tpr->rx_jmb_prod_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005800 while (sw_idx != hw_idx && budget > 0) {
Matt Carlsonafc081f2009-11-13 13:03:43 +00005801 struct ring_info *ri;
Matt Carlson72334482009-08-28 14:03:01 +00005802 struct tg3_rx_buffer_desc *desc = &tnapi->rx_rcb[sw_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07005803 unsigned int len;
5804 struct sk_buff *skb;
5805 dma_addr_t dma_addr;
5806 u32 opaque_key, desc_idx, *post_ptr;
Eric Dumazet9205fd92011-11-18 06:47:01 +00005807 u8 *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005808
5809 desc_idx = desc->opaque & RXD_OPAQUE_INDEX_MASK;
5810 opaque_key = desc->opaque & RXD_OPAQUE_RING_MASK;
5811 if (opaque_key == RXD_OPAQUE_RING_STD) {
Matt Carlson8fea32b2010-09-15 08:59:58 +00005812 ri = &tp->napi[0].prodring.rx_std_buffers[desc_idx];
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005813 dma_addr = dma_unmap_addr(ri, mapping);
Eric Dumazet9205fd92011-11-18 06:47:01 +00005814 data = ri->data;
Matt Carlson43619352009-11-13 13:03:47 +00005815 post_ptr = &std_prod_idx;
Michael Chanf92905d2006-06-29 20:14:29 -07005816 rx_std_posted++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005817 } else if (opaque_key == RXD_OPAQUE_RING_JUMBO) {
Matt Carlson8fea32b2010-09-15 08:59:58 +00005818 ri = &tp->napi[0].prodring.rx_jmb_buffers[desc_idx];
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005819 dma_addr = dma_unmap_addr(ri, mapping);
Eric Dumazet9205fd92011-11-18 06:47:01 +00005820 data = ri->data;
Matt Carlson43619352009-11-13 13:03:47 +00005821 post_ptr = &jmb_prod_idx;
Matt Carlson21f581a2009-08-28 14:00:25 +00005822 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07005823 goto next_pkt_nopost;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005824
5825 work_mask |= opaque_key;
5826
5827 if ((desc->err_vlan & RXD_ERR_MASK) != 0 &&
5828 (desc->err_vlan != RXD_ERR_ODD_NIBBLE_RCVD_MII)) {
5829 drop_it:
Matt Carlsona3896162009-11-13 13:03:44 +00005830 tg3_recycle_rx(tnapi, tpr, opaque_key,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005831 desc_idx, *post_ptr);
5832 drop_it_no_recycle:
5833 /* Other statistics kept track of by card. */
Eric Dumazetb0057c52010-10-10 19:55:52 +00005834 tp->rx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005835 goto next_pkt;
5836 }
5837
Eric Dumazet9205fd92011-11-18 06:47:01 +00005838 prefetch(data + TG3_RX_OFFSET(tp));
Matt Carlsonad829262008-11-21 17:16:16 -08005839 len = ((desc->idx_len & RXD_LEN_MASK) >> RXD_LEN_SHIFT) -
5840 ETH_FCS_LEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005841
Matt Carlsond2757fc2010-04-12 06:58:27 +00005842 if (len > TG3_RX_COPY_THRESH(tp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005843 int skb_size;
5844
Eric Dumazet9205fd92011-11-18 06:47:01 +00005845 skb_size = tg3_alloc_rx_data(tp, tpr, opaque_key,
Matt Carlsonafc081f2009-11-13 13:03:43 +00005846 *post_ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005847 if (skb_size < 0)
5848 goto drop_it;
5849
Matt Carlson287be122009-08-28 13:58:46 +00005850 pci_unmap_single(tp->pdev, dma_addr, skb_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005851 PCI_DMA_FROMDEVICE);
5852
Eric Dumazet9205fd92011-11-18 06:47:01 +00005853 skb = build_skb(data);
5854 if (!skb) {
5855 kfree(data);
5856 goto drop_it_no_recycle;
5857 }
5858 skb_reserve(skb, TG3_RX_OFFSET(tp));
5859 /* Ensure that the update to the data happens
Matt Carlson61e800c2010-02-17 15:16:54 +00005860 * after the usage of the old DMA mapping.
5861 */
5862 smp_wmb();
5863
Eric Dumazet9205fd92011-11-18 06:47:01 +00005864 ri->data = NULL;
Matt Carlson61e800c2010-02-17 15:16:54 +00005865
Linus Torvalds1da177e2005-04-16 15:20:36 -07005866 } else {
Matt Carlsona3896162009-11-13 13:03:44 +00005867 tg3_recycle_rx(tnapi, tpr, opaque_key,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005868 desc_idx, *post_ptr);
5869
Eric Dumazet9205fd92011-11-18 06:47:01 +00005870 skb = netdev_alloc_skb(tp->dev,
5871 len + TG3_RAW_IP_ALIGN);
5872 if (skb == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005873 goto drop_it_no_recycle;
5874
Eric Dumazet9205fd92011-11-18 06:47:01 +00005875 skb_reserve(skb, TG3_RAW_IP_ALIGN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005876 pci_dma_sync_single_for_cpu(tp->pdev, dma_addr, len, PCI_DMA_FROMDEVICE);
Eric Dumazet9205fd92011-11-18 06:47:01 +00005877 memcpy(skb->data,
5878 data + TG3_RX_OFFSET(tp),
5879 len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005880 pci_dma_sync_single_for_device(tp->pdev, dma_addr, len, PCI_DMA_FROMDEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005881 }
5882
Eric Dumazet9205fd92011-11-18 06:47:01 +00005883 skb_put(skb, len);
Michał Mirosławdc668912011-04-07 03:35:07 +00005884 if ((tp->dev->features & NETIF_F_RXCSUM) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005885 (desc->type_flags & RXD_FLAG_TCPUDP_CSUM) &&
5886 (((desc->ip_tcp_csum & RXD_TCPCSUM_MASK)
5887 >> RXD_TCPCSUM_SHIFT) == 0xffff))
5888 skb->ip_summed = CHECKSUM_UNNECESSARY;
5889 else
Eric Dumazetbc8acf22010-09-02 13:07:41 -07005890 skb_checksum_none_assert(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005891
5892 skb->protocol = eth_type_trans(skb, tp->dev);
Matt Carlsonf7b493e2009-02-25 14:21:52 +00005893
5894 if (len > (tp->dev->mtu + ETH_HLEN) &&
5895 skb->protocol != htons(ETH_P_8021Q)) {
5896 dev_kfree_skb(skb);
Eric Dumazetb0057c52010-10-10 19:55:52 +00005897 goto drop_it_no_recycle;
Matt Carlsonf7b493e2009-02-25 14:21:52 +00005898 }
5899
Matt Carlson9dc7a112010-04-12 06:58:28 +00005900 if (desc->type_flags & RXD_FLAG_VLAN &&
Matt Carlsonbf933c82011-01-25 15:58:49 +00005901 !(tp->rx_mode & RX_MODE_KEEP_VLAN_TAG))
5902 __vlan_hwaccel_put_tag(skb,
5903 desc->err_vlan & RXD_VLAN_MASK);
Matt Carlson9dc7a112010-04-12 06:58:28 +00005904
Matt Carlsonbf933c82011-01-25 15:58:49 +00005905 napi_gro_receive(&tnapi->napi, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005906
Linus Torvalds1da177e2005-04-16 15:20:36 -07005907 received++;
5908 budget--;
5909
5910next_pkt:
5911 (*post_ptr)++;
Michael Chanf92905d2006-06-29 20:14:29 -07005912
5913 if (unlikely(rx_std_posted >= tp->rx_std_max_post)) {
Matt Carlson2c49a442010-09-30 10:34:35 +00005914 tpr->rx_std_prod_idx = std_prod_idx &
5915 tp->rx_std_ring_mask;
Matt Carlson86cfe4f2010-01-12 10:11:37 +00005916 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
5917 tpr->rx_std_prod_idx);
Michael Chanf92905d2006-06-29 20:14:29 -07005918 work_mask &= ~RXD_OPAQUE_RING_STD;
5919 rx_std_posted = 0;
5920 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005921next_pkt_nopost:
Michael Chan483ba502005-04-25 15:14:03 -07005922 sw_idx++;
Matt Carlson7cb32cf2010-09-30 10:34:36 +00005923 sw_idx &= tp->rx_ret_ring_mask;
Michael Chan52f6d692005-04-25 15:14:32 -07005924
5925 /* Refresh hw_idx to see if there is new work */
5926 if (sw_idx == hw_idx) {
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00005927 hw_idx = *(tnapi->rx_rcb_prod_idx);
Michael Chan52f6d692005-04-25 15:14:32 -07005928 rmb();
5929 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005930 }
5931
5932 /* ACK the status ring. */
Matt Carlson72334482009-08-28 14:03:01 +00005933 tnapi->rx_rcb_ptr = sw_idx;
5934 tw32_rx_mbox(tnapi->consmbox, sw_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005935
5936 /* Refill RX ring(s). */
Joe Perches63c3a662011-04-26 08:12:10 +00005937 if (!tg3_flag(tp, ENABLE_RSS)) {
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005938 if (work_mask & RXD_OPAQUE_RING_STD) {
Matt Carlson2c49a442010-09-30 10:34:35 +00005939 tpr->rx_std_prod_idx = std_prod_idx &
5940 tp->rx_std_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005941 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
5942 tpr->rx_std_prod_idx);
5943 }
5944 if (work_mask & RXD_OPAQUE_RING_JUMBO) {
Matt Carlson2c49a442010-09-30 10:34:35 +00005945 tpr->rx_jmb_prod_idx = jmb_prod_idx &
5946 tp->rx_jmb_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005947 tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG,
5948 tpr->rx_jmb_prod_idx);
5949 }
5950 mmiowb();
5951 } else if (work_mask) {
5952 /* rx_std_buffers[] and rx_jmb_buffers[] entries must be
5953 * updated before the producer indices can be updated.
5954 */
5955 smp_wmb();
5956
Matt Carlson2c49a442010-09-30 10:34:35 +00005957 tpr->rx_std_prod_idx = std_prod_idx & tp->rx_std_ring_mask;
5958 tpr->rx_jmb_prod_idx = jmb_prod_idx & tp->rx_jmb_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005959
Matt Carlsone4af1af2010-02-12 14:47:05 +00005960 if (tnapi != &tp->napi[1])
5961 napi_schedule(&tp->napi[1].napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005962 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005963
5964 return received;
5965}
5966
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005967static void tg3_poll_link(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005968{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005969 /* handle link change and other phy events */
Joe Perches63c3a662011-04-26 08:12:10 +00005970 if (!(tg3_flag(tp, USE_LINKCHG_REG) || tg3_flag(tp, POLL_SERDES))) {
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005971 struct tg3_hw_status *sblk = tp->napi[0].hw_status;
5972
Linus Torvalds1da177e2005-04-16 15:20:36 -07005973 if (sblk->status & SD_STATUS_LINK_CHG) {
5974 sblk->status = SD_STATUS_UPDATED |
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005975 (sblk->status & ~SD_STATUS_LINK_CHG);
David S. Millerf47c11e2005-06-24 20:18:35 -07005976 spin_lock(&tp->lock);
Joe Perches63c3a662011-04-26 08:12:10 +00005977 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlsondd477002008-05-25 23:45:58 -07005978 tw32_f(MAC_STATUS,
5979 (MAC_STATUS_SYNC_CHANGED |
5980 MAC_STATUS_CFG_CHANGED |
5981 MAC_STATUS_MI_COMPLETION |
5982 MAC_STATUS_LNKSTATE_CHANGED));
5983 udelay(40);
5984 } else
5985 tg3_setup_phy(tp, 0);
David S. Millerf47c11e2005-06-24 20:18:35 -07005986 spin_unlock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005987 }
5988 }
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005989}
5990
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005991static int tg3_rx_prodring_xfer(struct tg3 *tp,
5992 struct tg3_rx_prodring_set *dpr,
5993 struct tg3_rx_prodring_set *spr)
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005994{
5995 u32 si, di, cpycnt, src_prod_idx;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005996 int i, err = 0;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005997
5998 while (1) {
5999 src_prod_idx = spr->rx_std_prod_idx;
6000
6001 /* Make sure updates to the rx_std_buffers[] entries and the
6002 * standard producer index are seen in the correct order.
6003 */
6004 smp_rmb();
6005
6006 if (spr->rx_std_cons_idx == src_prod_idx)
6007 break;
6008
6009 if (spr->rx_std_cons_idx < src_prod_idx)
6010 cpycnt = src_prod_idx - spr->rx_std_cons_idx;
6011 else
Matt Carlson2c49a442010-09-30 10:34:35 +00006012 cpycnt = tp->rx_std_ring_mask + 1 -
6013 spr->rx_std_cons_idx;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006014
Matt Carlson2c49a442010-09-30 10:34:35 +00006015 cpycnt = min(cpycnt,
6016 tp->rx_std_ring_mask + 1 - dpr->rx_std_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006017
6018 si = spr->rx_std_cons_idx;
6019 di = dpr->rx_std_prod_idx;
6020
Matt Carlsone92967b2010-02-12 14:47:06 +00006021 for (i = di; i < di + cpycnt; i++) {
Eric Dumazet9205fd92011-11-18 06:47:01 +00006022 if (dpr->rx_std_buffers[i].data) {
Matt Carlsone92967b2010-02-12 14:47:06 +00006023 cpycnt = i - di;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00006024 err = -ENOSPC;
Matt Carlsone92967b2010-02-12 14:47:06 +00006025 break;
6026 }
6027 }
6028
6029 if (!cpycnt)
6030 break;
6031
6032 /* Ensure that updates to the rx_std_buffers ring and the
6033 * shadowed hardware producer ring from tg3_recycle_skb() are
6034 * ordered correctly WRT the skb check above.
6035 */
6036 smp_rmb();
6037
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006038 memcpy(&dpr->rx_std_buffers[di],
6039 &spr->rx_std_buffers[si],
6040 cpycnt * sizeof(struct ring_info));
6041
6042 for (i = 0; i < cpycnt; i++, di++, si++) {
6043 struct tg3_rx_buffer_desc *sbd, *dbd;
6044 sbd = &spr->rx_std[si];
6045 dbd = &dpr->rx_std[di];
6046 dbd->addr_hi = sbd->addr_hi;
6047 dbd->addr_lo = sbd->addr_lo;
6048 }
6049
Matt Carlson2c49a442010-09-30 10:34:35 +00006050 spr->rx_std_cons_idx = (spr->rx_std_cons_idx + cpycnt) &
6051 tp->rx_std_ring_mask;
6052 dpr->rx_std_prod_idx = (dpr->rx_std_prod_idx + cpycnt) &
6053 tp->rx_std_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006054 }
6055
6056 while (1) {
6057 src_prod_idx = spr->rx_jmb_prod_idx;
6058
6059 /* Make sure updates to the rx_jmb_buffers[] entries and
6060 * the jumbo producer index are seen in the correct order.
6061 */
6062 smp_rmb();
6063
6064 if (spr->rx_jmb_cons_idx == src_prod_idx)
6065 break;
6066
6067 if (spr->rx_jmb_cons_idx < src_prod_idx)
6068 cpycnt = src_prod_idx - spr->rx_jmb_cons_idx;
6069 else
Matt Carlson2c49a442010-09-30 10:34:35 +00006070 cpycnt = tp->rx_jmb_ring_mask + 1 -
6071 spr->rx_jmb_cons_idx;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006072
6073 cpycnt = min(cpycnt,
Matt Carlson2c49a442010-09-30 10:34:35 +00006074 tp->rx_jmb_ring_mask + 1 - dpr->rx_jmb_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006075
6076 si = spr->rx_jmb_cons_idx;
6077 di = dpr->rx_jmb_prod_idx;
6078
Matt Carlsone92967b2010-02-12 14:47:06 +00006079 for (i = di; i < di + cpycnt; i++) {
Eric Dumazet9205fd92011-11-18 06:47:01 +00006080 if (dpr->rx_jmb_buffers[i].data) {
Matt Carlsone92967b2010-02-12 14:47:06 +00006081 cpycnt = i - di;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00006082 err = -ENOSPC;
Matt Carlsone92967b2010-02-12 14:47:06 +00006083 break;
6084 }
6085 }
6086
6087 if (!cpycnt)
6088 break;
6089
6090 /* Ensure that updates to the rx_jmb_buffers ring and the
6091 * shadowed hardware producer ring from tg3_recycle_skb() are
6092 * ordered correctly WRT the skb check above.
6093 */
6094 smp_rmb();
6095
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006096 memcpy(&dpr->rx_jmb_buffers[di],
6097 &spr->rx_jmb_buffers[si],
6098 cpycnt * sizeof(struct ring_info));
6099
6100 for (i = 0; i < cpycnt; i++, di++, si++) {
6101 struct tg3_rx_buffer_desc *sbd, *dbd;
6102 sbd = &spr->rx_jmb[si].std;
6103 dbd = &dpr->rx_jmb[di].std;
6104 dbd->addr_hi = sbd->addr_hi;
6105 dbd->addr_lo = sbd->addr_lo;
6106 }
6107
Matt Carlson2c49a442010-09-30 10:34:35 +00006108 spr->rx_jmb_cons_idx = (spr->rx_jmb_cons_idx + cpycnt) &
6109 tp->rx_jmb_ring_mask;
6110 dpr->rx_jmb_prod_idx = (dpr->rx_jmb_prod_idx + cpycnt) &
6111 tp->rx_jmb_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006112 }
Matt Carlsonf89f38b2010-02-12 14:47:07 +00006113
6114 return err;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006115}
6116
Matt Carlson35f2d7d2009-11-13 13:03:41 +00006117static int tg3_poll_work(struct tg3_napi *tnapi, int work_done, int budget)
6118{
6119 struct tg3 *tp = tnapi->tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006120
6121 /* run TX completion thread */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006122 if (tnapi->hw_status->idx[0].tx_consumer != tnapi->tx_cons) {
Matt Carlson17375d22009-08-28 14:02:18 +00006123 tg3_tx(tnapi);
Joe Perches63c3a662011-04-26 08:12:10 +00006124 if (unlikely(tg3_flag(tp, TX_RECOVERY_PENDING)))
Michael Chan4fd7ab52007-10-12 01:39:50 -07006125 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006126 }
6127
Linus Torvalds1da177e2005-04-16 15:20:36 -07006128 /* run RX thread, within the bounds set by NAPI.
6129 * All RX "locking" is done by ensuring outside
Stephen Hemmingerbea33482007-10-03 16:41:36 -07006130 * code synchronizes with tg3->napi.poll()
Linus Torvalds1da177e2005-04-16 15:20:36 -07006131 */
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00006132 if (*(tnapi->rx_rcb_prod_idx) != tnapi->rx_rcb_ptr)
Matt Carlson17375d22009-08-28 14:02:18 +00006133 work_done += tg3_rx(tnapi, budget - work_done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006134
Joe Perches63c3a662011-04-26 08:12:10 +00006135 if (tg3_flag(tp, ENABLE_RSS) && tnapi == &tp->napi[1]) {
Matt Carlson8fea32b2010-09-15 08:59:58 +00006136 struct tg3_rx_prodring_set *dpr = &tp->napi[0].prodring;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00006137 int i, err = 0;
Matt Carlsone4af1af2010-02-12 14:47:05 +00006138 u32 std_prod_idx = dpr->rx_std_prod_idx;
6139 u32 jmb_prod_idx = dpr->rx_jmb_prod_idx;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006140
Matt Carlsone4af1af2010-02-12 14:47:05 +00006141 for (i = 1; i < tp->irq_cnt; i++)
Matt Carlsonf89f38b2010-02-12 14:47:07 +00006142 err |= tg3_rx_prodring_xfer(tp, dpr,
Matt Carlson8fea32b2010-09-15 08:59:58 +00006143 &tp->napi[i].prodring);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006144
6145 wmb();
6146
Matt Carlsone4af1af2010-02-12 14:47:05 +00006147 if (std_prod_idx != dpr->rx_std_prod_idx)
6148 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
6149 dpr->rx_std_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006150
Matt Carlsone4af1af2010-02-12 14:47:05 +00006151 if (jmb_prod_idx != dpr->rx_jmb_prod_idx)
6152 tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG,
6153 dpr->rx_jmb_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006154
6155 mmiowb();
Matt Carlsonf89f38b2010-02-12 14:47:07 +00006156
6157 if (err)
6158 tw32_f(HOSTCC_MODE, tp->coal_now);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006159 }
6160
David S. Miller6f535762007-10-11 18:08:29 -07006161 return work_done;
6162}
David S. Millerf7383c22005-05-18 22:50:53 -07006163
Matt Carlsondb219972011-11-04 09:15:03 +00006164static inline void tg3_reset_task_schedule(struct tg3 *tp)
6165{
6166 if (!test_and_set_bit(TG3_FLAG_RESET_TASK_PENDING, tp->tg3_flags))
6167 schedule_work(&tp->reset_task);
6168}
6169
6170static inline void tg3_reset_task_cancel(struct tg3 *tp)
6171{
6172 cancel_work_sync(&tp->reset_task);
6173 tg3_flag_clear(tp, RESET_TASK_PENDING);
6174}
6175
Matt Carlson35f2d7d2009-11-13 13:03:41 +00006176static int tg3_poll_msix(struct napi_struct *napi, int budget)
6177{
6178 struct tg3_napi *tnapi = container_of(napi, struct tg3_napi, napi);
6179 struct tg3 *tp = tnapi->tp;
6180 int work_done = 0;
6181 struct tg3_hw_status *sblk = tnapi->hw_status;
6182
6183 while (1) {
6184 work_done = tg3_poll_work(tnapi, work_done, budget);
6185
Joe Perches63c3a662011-04-26 08:12:10 +00006186 if (unlikely(tg3_flag(tp, TX_RECOVERY_PENDING)))
Matt Carlson35f2d7d2009-11-13 13:03:41 +00006187 goto tx_recovery;
6188
6189 if (unlikely(work_done >= budget))
6190 break;
6191
Matt Carlsonc6cdf432010-04-05 10:19:26 +00006192 /* tp->last_tag is used in tg3_int_reenable() below
Matt Carlson35f2d7d2009-11-13 13:03:41 +00006193 * to tell the hw how much work has been processed,
6194 * so we must read it before checking for more work.
6195 */
6196 tnapi->last_tag = sblk->status_tag;
6197 tnapi->last_irq_tag = tnapi->last_tag;
6198 rmb();
6199
6200 /* check for RX/TX work to do */
Matt Carlson6d40db72010-04-05 10:19:20 +00006201 if (likely(sblk->idx[0].tx_consumer == tnapi->tx_cons &&
6202 *(tnapi->rx_rcb_prod_idx) == tnapi->rx_rcb_ptr)) {
Matt Carlson35f2d7d2009-11-13 13:03:41 +00006203 napi_complete(napi);
6204 /* Reenable interrupts. */
6205 tw32_mailbox(tnapi->int_mbox, tnapi->last_tag << 24);
6206 mmiowb();
6207 break;
6208 }
6209 }
6210
6211 return work_done;
6212
6213tx_recovery:
6214 /* work_done is guaranteed to be less than budget. */
6215 napi_complete(napi);
Matt Carlsondb219972011-11-04 09:15:03 +00006216 tg3_reset_task_schedule(tp);
Matt Carlson35f2d7d2009-11-13 13:03:41 +00006217 return work_done;
6218}
6219
Matt Carlsone64de4e2011-04-13 11:05:05 +00006220static void tg3_process_error(struct tg3 *tp)
6221{
6222 u32 val;
6223 bool real_error = false;
6224
Joe Perches63c3a662011-04-26 08:12:10 +00006225 if (tg3_flag(tp, ERROR_PROCESSED))
Matt Carlsone64de4e2011-04-13 11:05:05 +00006226 return;
6227
6228 /* Check Flow Attention register */
6229 val = tr32(HOSTCC_FLOW_ATTN);
6230 if (val & ~HOSTCC_FLOW_ATTN_MBUF_LWM) {
6231 netdev_err(tp->dev, "FLOW Attention error. Resetting chip.\n");
6232 real_error = true;
6233 }
6234
6235 if (tr32(MSGINT_STATUS) & ~MSGINT_STATUS_MSI_REQ) {
6236 netdev_err(tp->dev, "MSI Status error. Resetting chip.\n");
6237 real_error = true;
6238 }
6239
6240 if (tr32(RDMAC_STATUS) || tr32(WDMAC_STATUS)) {
6241 netdev_err(tp->dev, "DMA Status error. Resetting chip.\n");
6242 real_error = true;
6243 }
6244
6245 if (!real_error)
6246 return;
6247
6248 tg3_dump_state(tp);
6249
Joe Perches63c3a662011-04-26 08:12:10 +00006250 tg3_flag_set(tp, ERROR_PROCESSED);
Matt Carlsondb219972011-11-04 09:15:03 +00006251 tg3_reset_task_schedule(tp);
Matt Carlsone64de4e2011-04-13 11:05:05 +00006252}
6253
David S. Miller6f535762007-10-11 18:08:29 -07006254static int tg3_poll(struct napi_struct *napi, int budget)
6255{
Matt Carlson8ef04422009-08-28 14:01:37 +00006256 struct tg3_napi *tnapi = container_of(napi, struct tg3_napi, napi);
6257 struct tg3 *tp = tnapi->tp;
David S. Miller6f535762007-10-11 18:08:29 -07006258 int work_done = 0;
Matt Carlson898a56f2009-08-28 14:02:40 +00006259 struct tg3_hw_status *sblk = tnapi->hw_status;
David S. Miller6f535762007-10-11 18:08:29 -07006260
6261 while (1) {
Matt Carlsone64de4e2011-04-13 11:05:05 +00006262 if (sblk->status & SD_STATUS_ERROR)
6263 tg3_process_error(tp);
6264
Matt Carlson35f2d7d2009-11-13 13:03:41 +00006265 tg3_poll_link(tp);
6266
Matt Carlson17375d22009-08-28 14:02:18 +00006267 work_done = tg3_poll_work(tnapi, work_done, budget);
David S. Miller6f535762007-10-11 18:08:29 -07006268
Joe Perches63c3a662011-04-26 08:12:10 +00006269 if (unlikely(tg3_flag(tp, TX_RECOVERY_PENDING)))
David S. Miller6f535762007-10-11 18:08:29 -07006270 goto tx_recovery;
6271
6272 if (unlikely(work_done >= budget))
6273 break;
6274
Joe Perches63c3a662011-04-26 08:12:10 +00006275 if (tg3_flag(tp, TAGGED_STATUS)) {
Matt Carlson17375d22009-08-28 14:02:18 +00006276 /* tp->last_tag is used in tg3_int_reenable() below
Michael Chan4fd7ab52007-10-12 01:39:50 -07006277 * to tell the hw how much work has been processed,
6278 * so we must read it before checking for more work.
6279 */
Matt Carlson898a56f2009-08-28 14:02:40 +00006280 tnapi->last_tag = sblk->status_tag;
6281 tnapi->last_irq_tag = tnapi->last_tag;
Michael Chan4fd7ab52007-10-12 01:39:50 -07006282 rmb();
6283 } else
6284 sblk->status &= ~SD_STATUS_UPDATED;
6285
Matt Carlson17375d22009-08-28 14:02:18 +00006286 if (likely(!tg3_has_work(tnapi))) {
Ben Hutchings288379f2009-01-19 16:43:59 -08006287 napi_complete(napi);
Matt Carlson17375d22009-08-28 14:02:18 +00006288 tg3_int_reenable(tnapi);
David S. Miller6f535762007-10-11 18:08:29 -07006289 break;
6290 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006291 }
6292
Stephen Hemmingerbea33482007-10-03 16:41:36 -07006293 return work_done;
David S. Miller6f535762007-10-11 18:08:29 -07006294
6295tx_recovery:
Michael Chan4fd7ab52007-10-12 01:39:50 -07006296 /* work_done is guaranteed to be less than budget. */
Ben Hutchings288379f2009-01-19 16:43:59 -08006297 napi_complete(napi);
Matt Carlsondb219972011-11-04 09:15:03 +00006298 tg3_reset_task_schedule(tp);
Michael Chan4fd7ab52007-10-12 01:39:50 -07006299 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006300}
6301
Matt Carlson66cfd1b2010-09-30 10:34:30 +00006302static void tg3_napi_disable(struct tg3 *tp)
6303{
6304 int i;
6305
6306 for (i = tp->irq_cnt - 1; i >= 0; i--)
6307 napi_disable(&tp->napi[i].napi);
6308}
6309
6310static void tg3_napi_enable(struct tg3 *tp)
6311{
6312 int i;
6313
6314 for (i = 0; i < tp->irq_cnt; i++)
6315 napi_enable(&tp->napi[i].napi);
6316}
6317
6318static void tg3_napi_init(struct tg3 *tp)
6319{
6320 int i;
6321
6322 netif_napi_add(tp->dev, &tp->napi[0].napi, tg3_poll, 64);
6323 for (i = 1; i < tp->irq_cnt; i++)
6324 netif_napi_add(tp->dev, &tp->napi[i].napi, tg3_poll_msix, 64);
6325}
6326
6327static void tg3_napi_fini(struct tg3 *tp)
6328{
6329 int i;
6330
6331 for (i = 0; i < tp->irq_cnt; i++)
6332 netif_napi_del(&tp->napi[i].napi);
6333}
6334
6335static inline void tg3_netif_stop(struct tg3 *tp)
6336{
6337 tp->dev->trans_start = jiffies; /* prevent tx timeout */
6338 tg3_napi_disable(tp);
6339 netif_tx_disable(tp->dev);
6340}
6341
6342static inline void tg3_netif_start(struct tg3 *tp)
6343{
6344 /* NOTE: unconditional netif_tx_wake_all_queues is only
6345 * appropriate so long as all callers are assured to
6346 * have free tx slots (such as after tg3_init_hw)
6347 */
6348 netif_tx_wake_all_queues(tp->dev);
6349
6350 tg3_napi_enable(tp);
6351 tp->napi[0].hw_status->status |= SD_STATUS_UPDATED;
6352 tg3_enable_ints(tp);
6353}
6354
David S. Millerf47c11e2005-06-24 20:18:35 -07006355static void tg3_irq_quiesce(struct tg3 *tp)
6356{
Matt Carlson4f125f42009-09-01 12:55:02 +00006357 int i;
6358
David S. Millerf47c11e2005-06-24 20:18:35 -07006359 BUG_ON(tp->irq_sync);
6360
6361 tp->irq_sync = 1;
6362 smp_mb();
6363
Matt Carlson4f125f42009-09-01 12:55:02 +00006364 for (i = 0; i < tp->irq_cnt; i++)
6365 synchronize_irq(tp->napi[i].irq_vec);
David S. Millerf47c11e2005-06-24 20:18:35 -07006366}
6367
David S. Millerf47c11e2005-06-24 20:18:35 -07006368/* Fully shutdown all tg3 driver activity elsewhere in the system.
6369 * If irq_sync is non-zero, then the IRQ handler must be synchronized
6370 * with as well. Most of the time, this is not necessary except when
6371 * shutting down the device.
6372 */
6373static inline void tg3_full_lock(struct tg3 *tp, int irq_sync)
6374{
Michael Chan46966542007-07-11 19:47:19 -07006375 spin_lock_bh(&tp->lock);
David S. Millerf47c11e2005-06-24 20:18:35 -07006376 if (irq_sync)
6377 tg3_irq_quiesce(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -07006378}
6379
6380static inline void tg3_full_unlock(struct tg3 *tp)
6381{
David S. Millerf47c11e2005-06-24 20:18:35 -07006382 spin_unlock_bh(&tp->lock);
6383}
6384
Michael Chanfcfa0a32006-03-20 22:28:41 -08006385/* One-shot MSI handler - Chip automatically disables interrupt
6386 * after sending MSI so driver doesn't have to do it.
6387 */
David Howells7d12e782006-10-05 14:55:46 +01006388static irqreturn_t tg3_msi_1shot(int irq, void *dev_id)
Michael Chanfcfa0a32006-03-20 22:28:41 -08006389{
Matt Carlson09943a12009-08-28 14:01:57 +00006390 struct tg3_napi *tnapi = dev_id;
6391 struct tg3 *tp = tnapi->tp;
Michael Chanfcfa0a32006-03-20 22:28:41 -08006392
Matt Carlson898a56f2009-08-28 14:02:40 +00006393 prefetch(tnapi->hw_status);
Matt Carlson0c1d0e22009-09-01 13:16:33 +00006394 if (tnapi->rx_rcb)
6395 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Michael Chanfcfa0a32006-03-20 22:28:41 -08006396
6397 if (likely(!tg3_irq_sync(tp)))
Matt Carlson09943a12009-08-28 14:01:57 +00006398 napi_schedule(&tnapi->napi);
Michael Chanfcfa0a32006-03-20 22:28:41 -08006399
6400 return IRQ_HANDLED;
6401}
6402
Michael Chan88b06bc22005-04-21 17:13:25 -07006403/* MSI ISR - No need to check for interrupt sharing and no need to
6404 * flush status block and interrupt mailbox. PCI ordering rules
6405 * guarantee that MSI will arrive after the status block.
6406 */
David Howells7d12e782006-10-05 14:55:46 +01006407static irqreturn_t tg3_msi(int irq, void *dev_id)
Michael Chan88b06bc22005-04-21 17:13:25 -07006408{
Matt Carlson09943a12009-08-28 14:01:57 +00006409 struct tg3_napi *tnapi = dev_id;
6410 struct tg3 *tp = tnapi->tp;
Michael Chan88b06bc22005-04-21 17:13:25 -07006411
Matt Carlson898a56f2009-08-28 14:02:40 +00006412 prefetch(tnapi->hw_status);
Matt Carlson0c1d0e22009-09-01 13:16:33 +00006413 if (tnapi->rx_rcb)
6414 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Michael Chan88b06bc22005-04-21 17:13:25 -07006415 /*
David S. Millerfac9b832005-05-18 22:46:34 -07006416 * Writing any value to intr-mbox-0 clears PCI INTA# and
Michael Chan88b06bc22005-04-21 17:13:25 -07006417 * chip-internal interrupt pending events.
David S. Millerfac9b832005-05-18 22:46:34 -07006418 * Writing non-zero to intr-mbox-0 additional tells the
Michael Chan88b06bc22005-04-21 17:13:25 -07006419 * NIC to stop sending us irqs, engaging "in-intr-handler"
6420 * event coalescing.
6421 */
Matt Carlson5b39de92011-08-31 11:44:50 +00006422 tw32_mailbox(tnapi->int_mbox, 0x00000001);
Michael Chan61487482005-09-05 17:53:19 -07006423 if (likely(!tg3_irq_sync(tp)))
Matt Carlson09943a12009-08-28 14:01:57 +00006424 napi_schedule(&tnapi->napi);
Michael Chan61487482005-09-05 17:53:19 -07006425
Michael Chan88b06bc22005-04-21 17:13:25 -07006426 return IRQ_RETVAL(1);
6427}
6428
David Howells7d12e782006-10-05 14:55:46 +01006429static irqreturn_t tg3_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006430{
Matt Carlson09943a12009-08-28 14:01:57 +00006431 struct tg3_napi *tnapi = dev_id;
6432 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00006433 struct tg3_hw_status *sblk = tnapi->hw_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006434 unsigned int handled = 1;
6435
Linus Torvalds1da177e2005-04-16 15:20:36 -07006436 /* In INTx mode, it is possible for the interrupt to arrive at
6437 * the CPU before the status block posted prior to the interrupt.
6438 * Reading the PCI State register will confirm whether the
6439 * interrupt is ours and will flush the status block.
6440 */
Michael Chand18edcb2007-03-24 20:57:11 -07006441 if (unlikely(!(sblk->status & SD_STATUS_UPDATED))) {
Joe Perches63c3a662011-04-26 08:12:10 +00006442 if (tg3_flag(tp, CHIP_RESETTING) ||
Michael Chand18edcb2007-03-24 20:57:11 -07006443 (tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
6444 handled = 0;
David S. Millerf47c11e2005-06-24 20:18:35 -07006445 goto out;
David S. Millerfac9b832005-05-18 22:46:34 -07006446 }
Michael Chand18edcb2007-03-24 20:57:11 -07006447 }
6448
6449 /*
6450 * Writing any value to intr-mbox-0 clears PCI INTA# and
6451 * chip-internal interrupt pending events.
6452 * Writing non-zero to intr-mbox-0 additional tells the
6453 * NIC to stop sending us irqs, engaging "in-intr-handler"
6454 * event coalescing.
Michael Chanc04cb342007-05-07 00:26:15 -07006455 *
6456 * Flush the mailbox to de-assert the IRQ immediately to prevent
6457 * spurious interrupts. The flush impacts performance but
6458 * excessive spurious interrupts can be worse in some cases.
Michael Chand18edcb2007-03-24 20:57:11 -07006459 */
Michael Chanc04cb342007-05-07 00:26:15 -07006460 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 0x00000001);
Michael Chand18edcb2007-03-24 20:57:11 -07006461 if (tg3_irq_sync(tp))
6462 goto out;
6463 sblk->status &= ~SD_STATUS_UPDATED;
Matt Carlson17375d22009-08-28 14:02:18 +00006464 if (likely(tg3_has_work(tnapi))) {
Matt Carlson72334482009-08-28 14:03:01 +00006465 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Matt Carlson09943a12009-08-28 14:01:57 +00006466 napi_schedule(&tnapi->napi);
Michael Chand18edcb2007-03-24 20:57:11 -07006467 } else {
6468 /* No work, shared interrupt perhaps? re-enable
6469 * interrupts, and flush that PCI write
6470 */
6471 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW,
6472 0x00000000);
David S. Millerfac9b832005-05-18 22:46:34 -07006473 }
David S. Millerf47c11e2005-06-24 20:18:35 -07006474out:
David S. Millerfac9b832005-05-18 22:46:34 -07006475 return IRQ_RETVAL(handled);
6476}
6477
David Howells7d12e782006-10-05 14:55:46 +01006478static irqreturn_t tg3_interrupt_tagged(int irq, void *dev_id)
David S. Millerfac9b832005-05-18 22:46:34 -07006479{
Matt Carlson09943a12009-08-28 14:01:57 +00006480 struct tg3_napi *tnapi = dev_id;
6481 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00006482 struct tg3_hw_status *sblk = tnapi->hw_status;
David S. Millerfac9b832005-05-18 22:46:34 -07006483 unsigned int handled = 1;
6484
David S. Millerfac9b832005-05-18 22:46:34 -07006485 /* In INTx mode, it is possible for the interrupt to arrive at
6486 * the CPU before the status block posted prior to the interrupt.
6487 * Reading the PCI State register will confirm whether the
6488 * interrupt is ours and will flush the status block.
6489 */
Matt Carlson898a56f2009-08-28 14:02:40 +00006490 if (unlikely(sblk->status_tag == tnapi->last_irq_tag)) {
Joe Perches63c3a662011-04-26 08:12:10 +00006491 if (tg3_flag(tp, CHIP_RESETTING) ||
Michael Chand18edcb2007-03-24 20:57:11 -07006492 (tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
6493 handled = 0;
David S. Millerf47c11e2005-06-24 20:18:35 -07006494 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006495 }
Michael Chand18edcb2007-03-24 20:57:11 -07006496 }
6497
6498 /*
6499 * writing any value to intr-mbox-0 clears PCI INTA# and
6500 * chip-internal interrupt pending events.
6501 * writing non-zero to intr-mbox-0 additional tells the
6502 * NIC to stop sending us irqs, engaging "in-intr-handler"
6503 * event coalescing.
Michael Chanc04cb342007-05-07 00:26:15 -07006504 *
6505 * Flush the mailbox to de-assert the IRQ immediately to prevent
6506 * spurious interrupts. The flush impacts performance but
6507 * excessive spurious interrupts can be worse in some cases.
Michael Chand18edcb2007-03-24 20:57:11 -07006508 */
Michael Chanc04cb342007-05-07 00:26:15 -07006509 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 0x00000001);
Matt Carlson624f8e52009-04-20 06:55:01 +00006510
6511 /*
6512 * In a shared interrupt configuration, sometimes other devices'
6513 * interrupts will scream. We record the current status tag here
6514 * so that the above check can report that the screaming interrupts
6515 * are unhandled. Eventually they will be silenced.
6516 */
Matt Carlson898a56f2009-08-28 14:02:40 +00006517 tnapi->last_irq_tag = sblk->status_tag;
Matt Carlson624f8e52009-04-20 06:55:01 +00006518
Michael Chand18edcb2007-03-24 20:57:11 -07006519 if (tg3_irq_sync(tp))
6520 goto out;
Matt Carlson624f8e52009-04-20 06:55:01 +00006521
Matt Carlson72334482009-08-28 14:03:01 +00006522 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Matt Carlson624f8e52009-04-20 06:55:01 +00006523
Matt Carlson09943a12009-08-28 14:01:57 +00006524 napi_schedule(&tnapi->napi);
Matt Carlson624f8e52009-04-20 06:55:01 +00006525
David S. Millerf47c11e2005-06-24 20:18:35 -07006526out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006527 return IRQ_RETVAL(handled);
6528}
6529
Michael Chan79381092005-04-21 17:13:59 -07006530/* ISR for interrupt test */
David Howells7d12e782006-10-05 14:55:46 +01006531static irqreturn_t tg3_test_isr(int irq, void *dev_id)
Michael Chan79381092005-04-21 17:13:59 -07006532{
Matt Carlson09943a12009-08-28 14:01:57 +00006533 struct tg3_napi *tnapi = dev_id;
6534 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00006535 struct tg3_hw_status *sblk = tnapi->hw_status;
Michael Chan79381092005-04-21 17:13:59 -07006536
Michael Chanf9804dd2005-09-27 12:13:10 -07006537 if ((sblk->status & SD_STATUS_UPDATED) ||
6538 !(tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
Michael Chanb16250e2006-09-27 16:10:14 -07006539 tg3_disable_ints(tp);
Michael Chan79381092005-04-21 17:13:59 -07006540 return IRQ_RETVAL(1);
6541 }
6542 return IRQ_RETVAL(0);
6543}
6544
Linus Torvalds1da177e2005-04-16 15:20:36 -07006545#ifdef CONFIG_NET_POLL_CONTROLLER
6546static void tg3_poll_controller(struct net_device *dev)
6547{
Matt Carlson4f125f42009-09-01 12:55:02 +00006548 int i;
Michael Chan88b06bc22005-04-21 17:13:25 -07006549 struct tg3 *tp = netdev_priv(dev);
6550
Matt Carlson4f125f42009-09-01 12:55:02 +00006551 for (i = 0; i < tp->irq_cnt; i++)
Louis Rillingfe234f02010-03-09 06:14:41 +00006552 tg3_interrupt(tp->napi[i].irq_vec, &tp->napi[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006553}
6554#endif
6555
Linus Torvalds1da177e2005-04-16 15:20:36 -07006556static void tg3_tx_timeout(struct net_device *dev)
6557{
6558 struct tg3 *tp = netdev_priv(dev);
6559
Michael Chanb0408752007-02-13 12:18:30 -08006560 if (netif_msg_tx_err(tp)) {
Joe Perches05dbe002010-02-17 19:44:19 +00006561 netdev_err(dev, "transmit timed out, resetting\n");
Matt Carlson97bd8e42011-04-13 11:05:04 +00006562 tg3_dump_state(tp);
Michael Chanb0408752007-02-13 12:18:30 -08006563 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006564
Matt Carlsondb219972011-11-04 09:15:03 +00006565 tg3_reset_task_schedule(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006566}
6567
Michael Chanc58ec932005-09-17 00:46:27 -07006568/* Test for DMA buffers crossing any 4GB boundaries: 4G, 8G, etc */
6569static inline int tg3_4g_overflow_test(dma_addr_t mapping, int len)
6570{
6571 u32 base = (u32) mapping & 0xffffffff;
6572
Eric Dumazet807540b2010-09-23 05:40:09 +00006573 return (base > 0xffffdcc0) && (base + len + 8 < base);
Michael Chanc58ec932005-09-17 00:46:27 -07006574}
6575
Michael Chan72f2afb2006-03-06 19:28:35 -08006576/* Test for DMA addresses > 40-bit */
6577static inline int tg3_40bit_overflow_test(struct tg3 *tp, dma_addr_t mapping,
6578 int len)
6579{
6580#if defined(CONFIG_HIGHMEM) && (BITS_PER_LONG == 64)
Joe Perches63c3a662011-04-26 08:12:10 +00006581 if (tg3_flag(tp, 40BIT_DMA_BUG))
Eric Dumazet807540b2010-09-23 05:40:09 +00006582 return ((u64) mapping + len) > DMA_BIT_MASK(40);
Michael Chan72f2afb2006-03-06 19:28:35 -08006583 return 0;
6584#else
6585 return 0;
6586#endif
6587}
6588
Matt Carlsond1a3b732011-07-27 14:20:51 +00006589static inline void tg3_tx_set_bd(struct tg3_tx_buffer_desc *txbd,
Matt Carlson92cd3a12011-07-27 14:20:47 +00006590 dma_addr_t mapping, u32 len, u32 flags,
6591 u32 mss, u32 vlan)
Matt Carlson2ffcc982011-05-19 12:12:44 +00006592{
Matt Carlson92cd3a12011-07-27 14:20:47 +00006593 txbd->addr_hi = ((u64) mapping >> 32);
6594 txbd->addr_lo = ((u64) mapping & 0xffffffff);
6595 txbd->len_flags = (len << TXD_LEN_SHIFT) | (flags & 0x0000ffff);
6596 txbd->vlan_tag = (mss << TXD_MSS_SHIFT) | (vlan << TXD_VLAN_TAG_SHIFT);
Matt Carlson2ffcc982011-05-19 12:12:44 +00006597}
Linus Torvalds1da177e2005-04-16 15:20:36 -07006598
Matt Carlson84b67b22011-07-27 14:20:52 +00006599static bool tg3_tx_frag_set(struct tg3_napi *tnapi, u32 *entry, u32 *budget,
Matt Carlsond1a3b732011-07-27 14:20:51 +00006600 dma_addr_t map, u32 len, u32 flags,
6601 u32 mss, u32 vlan)
6602{
6603 struct tg3 *tp = tnapi->tp;
6604 bool hwbug = false;
6605
6606 if (tg3_flag(tp, SHORT_DMA_BUG) && len <= 8)
Rusty Russell3db1cd52011-12-19 13:56:45 +00006607 hwbug = true;
Matt Carlsond1a3b732011-07-27 14:20:51 +00006608
6609 if (tg3_4g_overflow_test(map, len))
Rusty Russell3db1cd52011-12-19 13:56:45 +00006610 hwbug = true;
Matt Carlsond1a3b732011-07-27 14:20:51 +00006611
6612 if (tg3_40bit_overflow_test(tp, map, len))
Rusty Russell3db1cd52011-12-19 13:56:45 +00006613 hwbug = true;
Matt Carlsond1a3b732011-07-27 14:20:51 +00006614
Matt Carlsona4cb4282011-12-14 11:09:58 +00006615 if (tp->dma_limit) {
Matt Carlsonb9e45482011-11-04 09:14:59 +00006616 u32 prvidx = *entry;
Matt Carlsone31aa982011-07-27 14:20:53 +00006617 u32 tmp_flag = flags & ~TXD_FLAG_END;
Matt Carlsona4cb4282011-12-14 11:09:58 +00006618 while (len > tp->dma_limit && *budget) {
6619 u32 frag_len = tp->dma_limit;
6620 len -= tp->dma_limit;
Matt Carlsone31aa982011-07-27 14:20:53 +00006621
Matt Carlsonb9e45482011-11-04 09:14:59 +00006622 /* Avoid the 8byte DMA problem */
6623 if (len <= 8) {
Matt Carlsona4cb4282011-12-14 11:09:58 +00006624 len += tp->dma_limit / 2;
6625 frag_len = tp->dma_limit / 2;
Matt Carlsone31aa982011-07-27 14:20:53 +00006626 }
6627
Matt Carlsonb9e45482011-11-04 09:14:59 +00006628 tnapi->tx_buffers[*entry].fragmented = true;
6629
6630 tg3_tx_set_bd(&tnapi->tx_ring[*entry], map,
6631 frag_len, tmp_flag, mss, vlan);
6632 *budget -= 1;
6633 prvidx = *entry;
6634 *entry = NEXT_TX(*entry);
6635
Matt Carlsone31aa982011-07-27 14:20:53 +00006636 map += frag_len;
6637 }
6638
6639 if (len) {
6640 if (*budget) {
6641 tg3_tx_set_bd(&tnapi->tx_ring[*entry], map,
6642 len, flags, mss, vlan);
Matt Carlsonb9e45482011-11-04 09:14:59 +00006643 *budget -= 1;
Matt Carlsone31aa982011-07-27 14:20:53 +00006644 *entry = NEXT_TX(*entry);
6645 } else {
Rusty Russell3db1cd52011-12-19 13:56:45 +00006646 hwbug = true;
Matt Carlsonb9e45482011-11-04 09:14:59 +00006647 tnapi->tx_buffers[prvidx].fragmented = false;
Matt Carlsone31aa982011-07-27 14:20:53 +00006648 }
6649 }
6650 } else {
Matt Carlson84b67b22011-07-27 14:20:52 +00006651 tg3_tx_set_bd(&tnapi->tx_ring[*entry], map,
6652 len, flags, mss, vlan);
Matt Carlsone31aa982011-07-27 14:20:53 +00006653 *entry = NEXT_TX(*entry);
6654 }
Matt Carlsond1a3b732011-07-27 14:20:51 +00006655
6656 return hwbug;
6657}
6658
Matt Carlson0d681b22011-07-27 14:20:49 +00006659static void tg3_tx_skb_unmap(struct tg3_napi *tnapi, u32 entry, int last)
Matt Carlson432aa7e2011-05-19 12:12:45 +00006660{
6661 int i;
Matt Carlson0d681b22011-07-27 14:20:49 +00006662 struct sk_buff *skb;
Matt Carlsondf8944c2011-07-27 14:20:46 +00006663 struct tg3_tx_ring_info *txb = &tnapi->tx_buffers[entry];
Matt Carlson432aa7e2011-05-19 12:12:45 +00006664
Matt Carlson0d681b22011-07-27 14:20:49 +00006665 skb = txb->skb;
6666 txb->skb = NULL;
6667
Matt Carlson432aa7e2011-05-19 12:12:45 +00006668 pci_unmap_single(tnapi->tp->pdev,
6669 dma_unmap_addr(txb, mapping),
6670 skb_headlen(skb),
6671 PCI_DMA_TODEVICE);
Matt Carlsone01ee142011-07-27 14:20:50 +00006672
6673 while (txb->fragmented) {
6674 txb->fragmented = false;
6675 entry = NEXT_TX(entry);
6676 txb = &tnapi->tx_buffers[entry];
6677 }
6678
Matt Carlsonba1142e2011-11-04 09:15:00 +00006679 for (i = 0; i <= last; i++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00006680 const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
Matt Carlson432aa7e2011-05-19 12:12:45 +00006681
6682 entry = NEXT_TX(entry);
6683 txb = &tnapi->tx_buffers[entry];
6684
6685 pci_unmap_page(tnapi->tp->pdev,
6686 dma_unmap_addr(txb, mapping),
Eric Dumazet9e903e02011-10-18 21:00:24 +00006687 skb_frag_size(frag), PCI_DMA_TODEVICE);
Matt Carlsone01ee142011-07-27 14:20:50 +00006688
6689 while (txb->fragmented) {
6690 txb->fragmented = false;
6691 entry = NEXT_TX(entry);
6692 txb = &tnapi->tx_buffers[entry];
6693 }
Matt Carlson432aa7e2011-05-19 12:12:45 +00006694 }
6695}
6696
Michael Chan72f2afb2006-03-06 19:28:35 -08006697/* Workaround 4GB and 40-bit hardware DMA bugs. */
Matt Carlson24f4efd2009-11-13 13:03:35 +00006698static int tigon3_dma_hwbug_workaround(struct tg3_napi *tnapi,
David S. Miller1805b2f2011-10-24 18:18:09 -04006699 struct sk_buff **pskb,
Matt Carlson84b67b22011-07-27 14:20:52 +00006700 u32 *entry, u32 *budget,
Matt Carlson92cd3a12011-07-27 14:20:47 +00006701 u32 base_flags, u32 mss, u32 vlan)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006702{
Matt Carlson24f4efd2009-11-13 13:03:35 +00006703 struct tg3 *tp = tnapi->tp;
David S. Miller1805b2f2011-10-24 18:18:09 -04006704 struct sk_buff *new_skb, *skb = *pskb;
Michael Chanc58ec932005-09-17 00:46:27 -07006705 dma_addr_t new_addr = 0;
Matt Carlson432aa7e2011-05-19 12:12:45 +00006706 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006707
Matt Carlson41588ba2008-04-19 18:12:33 -07006708 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701)
6709 new_skb = skb_copy(skb, GFP_ATOMIC);
6710 else {
6711 int more_headroom = 4 - ((unsigned long)skb->data & 3);
6712
6713 new_skb = skb_copy_expand(skb,
6714 skb_headroom(skb) + more_headroom,
6715 skb_tailroom(skb), GFP_ATOMIC);
6716 }
6717
Linus Torvalds1da177e2005-04-16 15:20:36 -07006718 if (!new_skb) {
Michael Chanc58ec932005-09-17 00:46:27 -07006719 ret = -1;
6720 } else {
6721 /* New SKB is guaranteed to be linear. */
Alexander Duyckf4188d82009-12-02 16:48:38 +00006722 new_addr = pci_map_single(tp->pdev, new_skb->data, new_skb->len,
6723 PCI_DMA_TODEVICE);
6724 /* Make sure the mapping succeeded */
6725 if (pci_dma_mapping_error(tp->pdev, new_addr)) {
Alexander Duyckf4188d82009-12-02 16:48:38 +00006726 dev_kfree_skb(new_skb);
Michael Chanc58ec932005-09-17 00:46:27 -07006727 ret = -1;
Michael Chanc58ec932005-09-17 00:46:27 -07006728 } else {
Matt Carlsonb9e45482011-11-04 09:14:59 +00006729 u32 save_entry = *entry;
6730
Matt Carlson92cd3a12011-07-27 14:20:47 +00006731 base_flags |= TXD_FLAG_END;
6732
Matt Carlson84b67b22011-07-27 14:20:52 +00006733 tnapi->tx_buffers[*entry].skb = new_skb;
6734 dma_unmap_addr_set(&tnapi->tx_buffers[*entry],
Matt Carlson432aa7e2011-05-19 12:12:45 +00006735 mapping, new_addr);
6736
Matt Carlson84b67b22011-07-27 14:20:52 +00006737 if (tg3_tx_frag_set(tnapi, entry, budget, new_addr,
Matt Carlsond1a3b732011-07-27 14:20:51 +00006738 new_skb->len, base_flags,
6739 mss, vlan)) {
Matt Carlsonba1142e2011-11-04 09:15:00 +00006740 tg3_tx_skb_unmap(tnapi, save_entry, -1);
Matt Carlsond1a3b732011-07-27 14:20:51 +00006741 dev_kfree_skb(new_skb);
6742 ret = -1;
6743 }
Michael Chanc58ec932005-09-17 00:46:27 -07006744 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006745 }
6746
Linus Torvalds1da177e2005-04-16 15:20:36 -07006747 dev_kfree_skb(skb);
David S. Miller1805b2f2011-10-24 18:18:09 -04006748 *pskb = new_skb;
Michael Chanc58ec932005-09-17 00:46:27 -07006749 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006750}
6751
Matt Carlson2ffcc982011-05-19 12:12:44 +00006752static netdev_tx_t tg3_start_xmit(struct sk_buff *, struct net_device *);
Michael Chan52c0fd82006-06-29 20:15:54 -07006753
6754/* Use GSO to workaround a rare TSO bug that may be triggered when the
6755 * TSO header is greater than 80 bytes.
6756 */
6757static int tg3_tso_bug(struct tg3 *tp, struct sk_buff *skb)
6758{
6759 struct sk_buff *segs, *nskb;
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006760 u32 frag_cnt_est = skb_shinfo(skb)->gso_segs * 3;
Michael Chan52c0fd82006-06-29 20:15:54 -07006761
6762 /* Estimate the number of fragments in the worst case */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006763 if (unlikely(tg3_tx_avail(&tp->napi[0]) <= frag_cnt_est)) {
Michael Chan52c0fd82006-06-29 20:15:54 -07006764 netif_stop_queue(tp->dev);
Matt Carlsonf65aac12010-08-02 11:26:03 +00006765
6766 /* netif_tx_stop_queue() must be done before checking
6767 * checking tx index in tg3_tx_avail() below, because in
6768 * tg3_tx(), we update tx index before checking for
6769 * netif_tx_queue_stopped().
6770 */
6771 smp_mb();
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006772 if (tg3_tx_avail(&tp->napi[0]) <= frag_cnt_est)
Michael Chan7f62ad52007-02-20 23:25:40 -08006773 return NETDEV_TX_BUSY;
6774
6775 netif_wake_queue(tp->dev);
Michael Chan52c0fd82006-06-29 20:15:54 -07006776 }
6777
6778 segs = skb_gso_segment(skb, tp->dev->features & ~NETIF_F_TSO);
Hirofumi Nakagawa801678c2008-04-29 01:03:09 -07006779 if (IS_ERR(segs))
Michael Chan52c0fd82006-06-29 20:15:54 -07006780 goto tg3_tso_bug_end;
6781
6782 do {
6783 nskb = segs;
6784 segs = segs->next;
6785 nskb->next = NULL;
Matt Carlson2ffcc982011-05-19 12:12:44 +00006786 tg3_start_xmit(nskb, tp->dev);
Michael Chan52c0fd82006-06-29 20:15:54 -07006787 } while (segs);
6788
6789tg3_tso_bug_end:
6790 dev_kfree_skb(skb);
6791
6792 return NETDEV_TX_OK;
6793}
Michael Chan52c0fd82006-06-29 20:15:54 -07006794
Michael Chan5a6f3072006-03-20 22:28:05 -08006795/* hard_start_xmit for devices that have the 4G bug and/or 40-bit bug and
Joe Perches63c3a662011-04-26 08:12:10 +00006796 * support TG3_FLAG_HW_TSO_1 or firmware TSO only.
Michael Chan5a6f3072006-03-20 22:28:05 -08006797 */
Matt Carlson2ffcc982011-05-19 12:12:44 +00006798static netdev_tx_t tg3_start_xmit(struct sk_buff *skb, struct net_device *dev)
Michael Chan5a6f3072006-03-20 22:28:05 -08006799{
6800 struct tg3 *tp = netdev_priv(dev);
Matt Carlson92cd3a12011-07-27 14:20:47 +00006801 u32 len, entry, base_flags, mss, vlan = 0;
Matt Carlson84b67b22011-07-27 14:20:52 +00006802 u32 budget;
Matt Carlson432aa7e2011-05-19 12:12:45 +00006803 int i = -1, would_hit_hwbug;
David S. Miller90079ce2008-09-11 04:52:51 -07006804 dma_addr_t mapping;
Matt Carlson24f4efd2009-11-13 13:03:35 +00006805 struct tg3_napi *tnapi;
6806 struct netdev_queue *txq;
Matt Carlson432aa7e2011-05-19 12:12:45 +00006807 unsigned int last;
Alexander Duyckf4188d82009-12-02 16:48:38 +00006808
Matt Carlson24f4efd2009-11-13 13:03:35 +00006809 txq = netdev_get_tx_queue(dev, skb_get_queue_mapping(skb));
6810 tnapi = &tp->napi[skb_get_queue_mapping(skb)];
Joe Perches63c3a662011-04-26 08:12:10 +00006811 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlson24f4efd2009-11-13 13:03:35 +00006812 tnapi++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006813
Matt Carlson84b67b22011-07-27 14:20:52 +00006814 budget = tg3_tx_avail(tnapi);
6815
Michael Chan00b70502006-06-17 21:58:45 -07006816 /* We are running in BH disabled context with netif_tx_lock
Stephen Hemmingerbea33482007-10-03 16:41:36 -07006817 * and TX reclaim runs via tp->napi.poll inside of a software
David S. Millerf47c11e2005-06-24 20:18:35 -07006818 * interrupt. Furthermore, IRQ processing runs lockless so we have
6819 * no IRQ context deadlocks to worry about either. Rejoice!
Linus Torvalds1da177e2005-04-16 15:20:36 -07006820 */
Matt Carlson84b67b22011-07-27 14:20:52 +00006821 if (unlikely(budget <= (skb_shinfo(skb)->nr_frags + 1))) {
Matt Carlson24f4efd2009-11-13 13:03:35 +00006822 if (!netif_tx_queue_stopped(txq)) {
6823 netif_tx_stop_queue(txq);
Stephen Hemminger1f064a82005-12-06 17:36:44 -08006824
6825 /* This is a hard error, log it. */
Matt Carlson5129c3a2010-04-05 10:19:23 +00006826 netdev_err(dev,
6827 "BUG! Tx Ring full when queue awake!\n");
Stephen Hemminger1f064a82005-12-06 17:36:44 -08006828 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006829 return NETDEV_TX_BUSY;
6830 }
6831
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006832 entry = tnapi->tx_prod;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006833 base_flags = 0;
Patrick McHardy84fa7932006-08-29 16:44:56 -07006834 if (skb->ip_summed == CHECKSUM_PARTIAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006835 base_flags |= TXD_FLAG_TCPUDP_CSUM;
Matt Carlson24f4efd2009-11-13 13:03:35 +00006836
Matt Carlsonbe98da62010-07-11 09:31:46 +00006837 mss = skb_shinfo(skb)->gso_size;
6838 if (mss) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07006839 struct iphdr *iph;
Matt Carlson34195c32010-07-11 09:31:42 +00006840 u32 tcp_opt_len, hdr_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006841
6842 if (skb_header_cloned(skb) &&
Eric Dumazet48855432011-10-24 07:53:03 +00006843 pskb_expand_head(skb, 0, 0, GFP_ATOMIC))
6844 goto drop;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006845
Matt Carlson34195c32010-07-11 09:31:42 +00006846 iph = ip_hdr(skb);
Arnaldo Carvalho de Meloab6a5bb2007-03-18 17:43:48 -07006847 tcp_opt_len = tcp_optlen(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006848
Eric Dumazeta5a11952012-01-23 01:22:09 +00006849 hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb) - ETH_HLEN;
Matt Carlson34195c32010-07-11 09:31:42 +00006850
Eric Dumazeta5a11952012-01-23 01:22:09 +00006851 if (!skb_is_gso_v6(skb)) {
Matt Carlson34195c32010-07-11 09:31:42 +00006852 iph->check = 0;
6853 iph->tot_len = htons(mss + hdr_len);
6854 }
6855
Michael Chan52c0fd82006-06-29 20:15:54 -07006856 if (unlikely((ETH_HLEN + hdr_len) > 80) &&
Joe Perches63c3a662011-04-26 08:12:10 +00006857 tg3_flag(tp, TSO_BUG))
Matt Carlsonde6f31e2010-04-12 06:58:30 +00006858 return tg3_tso_bug(tp, skb);
Michael Chan52c0fd82006-06-29 20:15:54 -07006859
Linus Torvalds1da177e2005-04-16 15:20:36 -07006860 base_flags |= (TXD_FLAG_CPU_PRE_DMA |
6861 TXD_FLAG_CPU_POST_DMA);
6862
Joe Perches63c3a662011-04-26 08:12:10 +00006863 if (tg3_flag(tp, HW_TSO_1) ||
6864 tg3_flag(tp, HW_TSO_2) ||
6865 tg3_flag(tp, HW_TSO_3)) {
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07006866 tcp_hdr(skb)->check = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006867 base_flags &= ~TXD_FLAG_TCPUDP_CSUM;
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07006868 } else
6869 tcp_hdr(skb)->check = ~csum_tcpudp_magic(iph->saddr,
6870 iph->daddr, 0,
6871 IPPROTO_TCP,
6872 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006873
Joe Perches63c3a662011-04-26 08:12:10 +00006874 if (tg3_flag(tp, HW_TSO_3)) {
Matt Carlson615774f2009-11-13 13:03:39 +00006875 mss |= (hdr_len & 0xc) << 12;
6876 if (hdr_len & 0x10)
6877 base_flags |= 0x00000010;
6878 base_flags |= (hdr_len & 0x3e0) << 5;
Joe Perches63c3a662011-04-26 08:12:10 +00006879 } else if (tg3_flag(tp, HW_TSO_2))
Matt Carlson92c6b8d2009-11-02 14:23:27 +00006880 mss |= hdr_len << 9;
Joe Perches63c3a662011-04-26 08:12:10 +00006881 else if (tg3_flag(tp, HW_TSO_1) ||
Matt Carlson92c6b8d2009-11-02 14:23:27 +00006882 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07006883 if (tcp_opt_len || iph->ihl > 5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006884 int tsflags;
6885
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07006886 tsflags = (iph->ihl - 5) + (tcp_opt_len >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006887 mss |= (tsflags << 11);
6888 }
6889 } else {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07006890 if (tcp_opt_len || iph->ihl > 5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006891 int tsflags;
6892
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07006893 tsflags = (iph->ihl - 5) + (tcp_opt_len >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006894 base_flags |= tsflags << 12;
6895 }
6896 }
6897 }
Matt Carlsonbf933c82011-01-25 15:58:49 +00006898
Matt Carlson93a700a2011-08-31 11:44:54 +00006899 if (tg3_flag(tp, USE_JUMBO_BDFLAG) &&
6900 !mss && skb->len > VLAN_ETH_FRAME_LEN)
6901 base_flags |= TXD_FLAG_JMB_PKT;
6902
Matt Carlson92cd3a12011-07-27 14:20:47 +00006903 if (vlan_tx_tag_present(skb)) {
6904 base_flags |= TXD_FLAG_VLAN;
6905 vlan = vlan_tx_tag_get(skb);
6906 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006907
Alexander Duyckf4188d82009-12-02 16:48:38 +00006908 len = skb_headlen(skb);
6909
6910 mapping = pci_map_single(tp->pdev, skb->data, len, PCI_DMA_TODEVICE);
Eric Dumazet48855432011-10-24 07:53:03 +00006911 if (pci_dma_mapping_error(tp->pdev, mapping))
6912 goto drop;
6913
David S. Miller90079ce2008-09-11 04:52:51 -07006914
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006915 tnapi->tx_buffers[entry].skb = skb;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006916 dma_unmap_addr_set(&tnapi->tx_buffers[entry], mapping, mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006917
6918 would_hit_hwbug = 0;
6919
Joe Perches63c3a662011-04-26 08:12:10 +00006920 if (tg3_flag(tp, 5701_DMA_BUG))
Michael Chanc58ec932005-09-17 00:46:27 -07006921 would_hit_hwbug = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006922
Matt Carlson84b67b22011-07-27 14:20:52 +00006923 if (tg3_tx_frag_set(tnapi, &entry, &budget, mapping, len, base_flags |
Matt Carlsond1a3b732011-07-27 14:20:51 +00006924 ((skb_shinfo(skb)->nr_frags == 0) ? TXD_FLAG_END : 0),
Matt Carlsonba1142e2011-11-04 09:15:00 +00006925 mss, vlan)) {
Matt Carlsond1a3b732011-07-27 14:20:51 +00006926 would_hit_hwbug = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006927 /* Now loop through additional data fragments, and queue them. */
Matt Carlsonba1142e2011-11-04 09:15:00 +00006928 } else if (skb_shinfo(skb)->nr_frags > 0) {
Matt Carlson92cd3a12011-07-27 14:20:47 +00006929 u32 tmp_mss = mss;
6930
6931 if (!tg3_flag(tp, HW_TSO_1) &&
6932 !tg3_flag(tp, HW_TSO_2) &&
6933 !tg3_flag(tp, HW_TSO_3))
6934 tmp_mss = 0;
6935
Linus Torvalds1da177e2005-04-16 15:20:36 -07006936 last = skb_shinfo(skb)->nr_frags - 1;
6937 for (i = 0; i <= last; i++) {
6938 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
6939
Eric Dumazet9e903e02011-10-18 21:00:24 +00006940 len = skb_frag_size(frag);
Ian Campbelldc234d02011-08-24 22:28:11 +00006941 mapping = skb_frag_dma_map(&tp->pdev->dev, frag, 0,
Ian Campbell5d6bcdf2011-10-06 11:10:48 +01006942 len, DMA_TO_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006943
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006944 tnapi->tx_buffers[entry].skb = NULL;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006945 dma_unmap_addr_set(&tnapi->tx_buffers[entry], mapping,
Alexander Duyckf4188d82009-12-02 16:48:38 +00006946 mapping);
Ian Campbell5d6bcdf2011-10-06 11:10:48 +01006947 if (dma_mapping_error(&tp->pdev->dev, mapping))
Alexander Duyckf4188d82009-12-02 16:48:38 +00006948 goto dma_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006949
Matt Carlsonb9e45482011-11-04 09:14:59 +00006950 if (!budget ||
6951 tg3_tx_frag_set(tnapi, &entry, &budget, mapping,
Matt Carlson84b67b22011-07-27 14:20:52 +00006952 len, base_flags |
6953 ((i == last) ? TXD_FLAG_END : 0),
Matt Carlsonb9e45482011-11-04 09:14:59 +00006954 tmp_mss, vlan)) {
Matt Carlson92c6b8d2009-11-02 14:23:27 +00006955 would_hit_hwbug = 1;
Matt Carlsonb9e45482011-11-04 09:14:59 +00006956 break;
6957 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006958 }
6959 }
6960
6961 if (would_hit_hwbug) {
Matt Carlson0d681b22011-07-27 14:20:49 +00006962 tg3_tx_skb_unmap(tnapi, tnapi->tx_prod, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006963
6964 /* If the workaround fails due to memory/mapping
6965 * failure, silently drop this packet.
6966 */
Matt Carlson84b67b22011-07-27 14:20:52 +00006967 entry = tnapi->tx_prod;
6968 budget = tg3_tx_avail(tnapi);
David S. Miller1805b2f2011-10-24 18:18:09 -04006969 if (tigon3_dma_hwbug_workaround(tnapi, &skb, &entry, &budget,
Matt Carlson84b67b22011-07-27 14:20:52 +00006970 base_flags, mss, vlan))
Eric Dumazet48855432011-10-24 07:53:03 +00006971 goto drop_nofree;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006972 }
6973
Richard Cochrand515b452011-06-19 03:31:41 +00006974 skb_tx_timestamp(skb);
Tom Herbert298376d2011-11-28 16:33:30 +00006975 netdev_sent_queue(tp->dev, skb->len);
Richard Cochrand515b452011-06-19 03:31:41 +00006976
Linus Torvalds1da177e2005-04-16 15:20:36 -07006977 /* Packets are ready, update Tx producer idx local and on card. */
Matt Carlson24f4efd2009-11-13 13:03:35 +00006978 tw32_tx_mbox(tnapi->prodmbox, entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006979
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006980 tnapi->tx_prod = entry;
6981 if (unlikely(tg3_tx_avail(tnapi) <= (MAX_SKB_FRAGS + 1))) {
Matt Carlson24f4efd2009-11-13 13:03:35 +00006982 netif_tx_stop_queue(txq);
Matt Carlsonf65aac12010-08-02 11:26:03 +00006983
6984 /* netif_tx_stop_queue() must be done before checking
6985 * checking tx index in tg3_tx_avail() below, because in
6986 * tg3_tx(), we update tx index before checking for
6987 * netif_tx_queue_stopped().
6988 */
6989 smp_mb();
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006990 if (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi))
Matt Carlson24f4efd2009-11-13 13:03:35 +00006991 netif_tx_wake_queue(txq);
Michael Chan51b91462005-09-01 17:41:28 -07006992 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006993
Eric Dumazetcdd0db02009-05-28 00:00:41 +00006994 mmiowb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006995 return NETDEV_TX_OK;
Alexander Duyckf4188d82009-12-02 16:48:38 +00006996
6997dma_error:
Matt Carlsonba1142e2011-11-04 09:15:00 +00006998 tg3_tx_skb_unmap(tnapi, tnapi->tx_prod, --i);
Matt Carlson432aa7e2011-05-19 12:12:45 +00006999 tnapi->tx_buffers[tnapi->tx_prod].skb = NULL;
Eric Dumazet48855432011-10-24 07:53:03 +00007000drop:
7001 dev_kfree_skb(skb);
7002drop_nofree:
7003 tp->tx_dropped++;
Alexander Duyckf4188d82009-12-02 16:48:38 +00007004 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007005}
7006
Matt Carlson6e01b202011-08-19 13:58:20 +00007007static void tg3_mac_loopback(struct tg3 *tp, bool enable)
7008{
7009 if (enable) {
7010 tp->mac_mode &= ~(MAC_MODE_HALF_DUPLEX |
7011 MAC_MODE_PORT_MODE_MASK);
7012
7013 tp->mac_mode |= MAC_MODE_PORT_INT_LPBACK;
7014
7015 if (!tg3_flag(tp, 5705_PLUS))
7016 tp->mac_mode |= MAC_MODE_LINK_POLARITY;
7017
7018 if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
7019 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
7020 else
7021 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
7022 } else {
7023 tp->mac_mode &= ~MAC_MODE_PORT_INT_LPBACK;
7024
7025 if (tg3_flag(tp, 5705_PLUS) ||
7026 (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) ||
7027 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700)
7028 tp->mac_mode &= ~MAC_MODE_LINK_POLARITY;
7029 }
7030
7031 tw32(MAC_MODE, tp->mac_mode);
7032 udelay(40);
7033}
7034
Matt Carlson941ec902011-08-19 13:58:23 +00007035static int tg3_phy_lpbk_set(struct tg3 *tp, u32 speed, bool extlpbk)
Matt Carlson5e5a7f32011-08-19 13:58:21 +00007036{
Matt Carlson941ec902011-08-19 13:58:23 +00007037 u32 val, bmcr, mac_mode, ptest = 0;
Matt Carlson5e5a7f32011-08-19 13:58:21 +00007038
7039 tg3_phy_toggle_apd(tp, false);
7040 tg3_phy_toggle_automdix(tp, 0);
7041
Matt Carlson941ec902011-08-19 13:58:23 +00007042 if (extlpbk && tg3_phy_set_extloopbk(tp))
7043 return -EIO;
7044
7045 bmcr = BMCR_FULLDPLX;
Matt Carlson5e5a7f32011-08-19 13:58:21 +00007046 switch (speed) {
7047 case SPEED_10:
7048 break;
7049 case SPEED_100:
7050 bmcr |= BMCR_SPEED100;
7051 break;
7052 case SPEED_1000:
7053 default:
7054 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
7055 speed = SPEED_100;
7056 bmcr |= BMCR_SPEED100;
7057 } else {
7058 speed = SPEED_1000;
7059 bmcr |= BMCR_SPEED1000;
7060 }
7061 }
7062
Matt Carlson941ec902011-08-19 13:58:23 +00007063 if (extlpbk) {
7064 if (!(tp->phy_flags & TG3_PHYFLG_IS_FET)) {
7065 tg3_readphy(tp, MII_CTRL1000, &val);
7066 val |= CTL1000_AS_MASTER |
7067 CTL1000_ENABLE_MASTER;
7068 tg3_writephy(tp, MII_CTRL1000, val);
7069 } else {
7070 ptest = MII_TG3_FET_PTEST_TRIM_SEL |
7071 MII_TG3_FET_PTEST_TRIM_2;
7072 tg3_writephy(tp, MII_TG3_FET_PTEST, ptest);
7073 }
7074 } else
7075 bmcr |= BMCR_LOOPBACK;
7076
Matt Carlson5e5a7f32011-08-19 13:58:21 +00007077 tg3_writephy(tp, MII_BMCR, bmcr);
7078
7079 /* The write needs to be flushed for the FETs */
7080 if (tp->phy_flags & TG3_PHYFLG_IS_FET)
7081 tg3_readphy(tp, MII_BMCR, &bmcr);
7082
7083 udelay(40);
7084
7085 if ((tp->phy_flags & TG3_PHYFLG_IS_FET) &&
7086 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785) {
Matt Carlson941ec902011-08-19 13:58:23 +00007087 tg3_writephy(tp, MII_TG3_FET_PTEST, ptest |
Matt Carlson5e5a7f32011-08-19 13:58:21 +00007088 MII_TG3_FET_PTEST_FRC_TX_LINK |
7089 MII_TG3_FET_PTEST_FRC_TX_LOCK);
7090
7091 /* The write needs to be flushed for the AC131 */
7092 tg3_readphy(tp, MII_TG3_FET_PTEST, &val);
7093 }
7094
7095 /* Reset to prevent losing 1st rx packet intermittently */
7096 if ((tp->phy_flags & TG3_PHYFLG_MII_SERDES) &&
7097 tg3_flag(tp, 5780_CLASS)) {
7098 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
7099 udelay(10);
7100 tw32_f(MAC_RX_MODE, tp->rx_mode);
7101 }
7102
7103 mac_mode = tp->mac_mode &
7104 ~(MAC_MODE_PORT_MODE_MASK | MAC_MODE_HALF_DUPLEX);
7105 if (speed == SPEED_1000)
7106 mac_mode |= MAC_MODE_PORT_MODE_GMII;
7107 else
7108 mac_mode |= MAC_MODE_PORT_MODE_MII;
7109
7110 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700) {
7111 u32 masked_phy_id = tp->phy_id & TG3_PHY_ID_MASK;
7112
7113 if (masked_phy_id == TG3_PHY_ID_BCM5401)
7114 mac_mode &= ~MAC_MODE_LINK_POLARITY;
7115 else if (masked_phy_id == TG3_PHY_ID_BCM5411)
7116 mac_mode |= MAC_MODE_LINK_POLARITY;
7117
7118 tg3_writephy(tp, MII_TG3_EXT_CTRL,
7119 MII_TG3_EXT_CTRL_LNK3_LED_MODE);
7120 }
7121
7122 tw32(MAC_MODE, mac_mode);
7123 udelay(40);
Matt Carlson941ec902011-08-19 13:58:23 +00007124
7125 return 0;
Matt Carlson5e5a7f32011-08-19 13:58:21 +00007126}
7127
Michał Mirosławc8f44af2011-11-15 15:29:55 +00007128static void tg3_set_loopback(struct net_device *dev, netdev_features_t features)
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00007129{
7130 struct tg3 *tp = netdev_priv(dev);
7131
7132 if (features & NETIF_F_LOOPBACK) {
7133 if (tp->mac_mode & MAC_MODE_PORT_INT_LPBACK)
7134 return;
7135
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00007136 spin_lock_bh(&tp->lock);
Matt Carlson6e01b202011-08-19 13:58:20 +00007137 tg3_mac_loopback(tp, true);
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00007138 netif_carrier_on(tp->dev);
7139 spin_unlock_bh(&tp->lock);
7140 netdev_info(dev, "Internal MAC loopback mode enabled.\n");
7141 } else {
7142 if (!(tp->mac_mode & MAC_MODE_PORT_INT_LPBACK))
7143 return;
7144
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00007145 spin_lock_bh(&tp->lock);
Matt Carlson6e01b202011-08-19 13:58:20 +00007146 tg3_mac_loopback(tp, false);
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00007147 /* Force link status check */
7148 tg3_setup_phy(tp, 1);
7149 spin_unlock_bh(&tp->lock);
7150 netdev_info(dev, "Internal MAC loopback mode disabled.\n");
7151 }
7152}
7153
Michał Mirosławc8f44af2011-11-15 15:29:55 +00007154static netdev_features_t tg3_fix_features(struct net_device *dev,
7155 netdev_features_t features)
Michał Mirosławdc668912011-04-07 03:35:07 +00007156{
7157 struct tg3 *tp = netdev_priv(dev);
7158
Joe Perches63c3a662011-04-26 08:12:10 +00007159 if (dev->mtu > ETH_DATA_LEN && tg3_flag(tp, 5780_CLASS))
Michał Mirosławdc668912011-04-07 03:35:07 +00007160 features &= ~NETIF_F_ALL_TSO;
7161
7162 return features;
7163}
7164
Michał Mirosławc8f44af2011-11-15 15:29:55 +00007165static int tg3_set_features(struct net_device *dev, netdev_features_t features)
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00007166{
Michał Mirosławc8f44af2011-11-15 15:29:55 +00007167 netdev_features_t changed = dev->features ^ features;
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00007168
7169 if ((changed & NETIF_F_LOOPBACK) && netif_running(dev))
7170 tg3_set_loopback(dev, features);
7171
7172 return 0;
7173}
7174
Matt Carlson21f581a2009-08-28 14:00:25 +00007175static void tg3_rx_prodring_free(struct tg3 *tp,
7176 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007177{
Linus Torvalds1da177e2005-04-16 15:20:36 -07007178 int i;
7179
Matt Carlson8fea32b2010-09-15 08:59:58 +00007180 if (tpr != &tp->napi[0].prodring) {
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007181 for (i = tpr->rx_std_cons_idx; i != tpr->rx_std_prod_idx;
Matt Carlson2c49a442010-09-30 10:34:35 +00007182 i = (i + 1) & tp->rx_std_ring_mask)
Eric Dumazet9205fd92011-11-18 06:47:01 +00007183 tg3_rx_data_free(tp, &tpr->rx_std_buffers[i],
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007184 tp->rx_pkt_map_sz);
7185
Joe Perches63c3a662011-04-26 08:12:10 +00007186 if (tg3_flag(tp, JUMBO_CAPABLE)) {
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007187 for (i = tpr->rx_jmb_cons_idx;
7188 i != tpr->rx_jmb_prod_idx;
Matt Carlson2c49a442010-09-30 10:34:35 +00007189 i = (i + 1) & tp->rx_jmb_ring_mask) {
Eric Dumazet9205fd92011-11-18 06:47:01 +00007190 tg3_rx_data_free(tp, &tpr->rx_jmb_buffers[i],
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007191 TG3_RX_JMB_MAP_SZ);
7192 }
7193 }
7194
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007195 return;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007196 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007197
Matt Carlson2c49a442010-09-30 10:34:35 +00007198 for (i = 0; i <= tp->rx_std_ring_mask; i++)
Eric Dumazet9205fd92011-11-18 06:47:01 +00007199 tg3_rx_data_free(tp, &tpr->rx_std_buffers[i],
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007200 tp->rx_pkt_map_sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007201
Joe Perches63c3a662011-04-26 08:12:10 +00007202 if (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS)) {
Matt Carlson2c49a442010-09-30 10:34:35 +00007203 for (i = 0; i <= tp->rx_jmb_ring_mask; i++)
Eric Dumazet9205fd92011-11-18 06:47:01 +00007204 tg3_rx_data_free(tp, &tpr->rx_jmb_buffers[i],
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007205 TG3_RX_JMB_MAP_SZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007206 }
7207}
7208
Matt Carlsonc6cdf432010-04-05 10:19:26 +00007209/* Initialize rx rings for packet processing.
Linus Torvalds1da177e2005-04-16 15:20:36 -07007210 *
7211 * The chip has been shut down and the driver detached from
7212 * the networking, so no interrupts or new tx packets will
7213 * end up in the driver. tp->{tx,}lock are held and thus
7214 * we may not sleep.
7215 */
Matt Carlson21f581a2009-08-28 14:00:25 +00007216static int tg3_rx_prodring_alloc(struct tg3 *tp,
7217 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007218{
Matt Carlson287be122009-08-28 13:58:46 +00007219 u32 i, rx_pkt_dma_sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007220
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007221 tpr->rx_std_cons_idx = 0;
7222 tpr->rx_std_prod_idx = 0;
7223 tpr->rx_jmb_cons_idx = 0;
7224 tpr->rx_jmb_prod_idx = 0;
7225
Matt Carlson8fea32b2010-09-15 08:59:58 +00007226 if (tpr != &tp->napi[0].prodring) {
Matt Carlson2c49a442010-09-30 10:34:35 +00007227 memset(&tpr->rx_std_buffers[0], 0,
7228 TG3_RX_STD_BUFF_RING_SIZE(tp));
Matt Carlson48035722010-10-14 10:37:43 +00007229 if (tpr->rx_jmb_buffers)
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007230 memset(&tpr->rx_jmb_buffers[0], 0,
Matt Carlson2c49a442010-09-30 10:34:35 +00007231 TG3_RX_JMB_BUFF_RING_SIZE(tp));
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007232 goto done;
7233 }
7234
Linus Torvalds1da177e2005-04-16 15:20:36 -07007235 /* Zero out all descriptors. */
Matt Carlson2c49a442010-09-30 10:34:35 +00007236 memset(tpr->rx_std, 0, TG3_RX_STD_RING_BYTES(tp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007237
Matt Carlson287be122009-08-28 13:58:46 +00007238 rx_pkt_dma_sz = TG3_RX_STD_DMA_SZ;
Joe Perches63c3a662011-04-26 08:12:10 +00007239 if (tg3_flag(tp, 5780_CLASS) &&
Matt Carlson287be122009-08-28 13:58:46 +00007240 tp->dev->mtu > ETH_DATA_LEN)
7241 rx_pkt_dma_sz = TG3_RX_JMB_DMA_SZ;
7242 tp->rx_pkt_map_sz = TG3_RX_DMA_TO_MAP_SZ(rx_pkt_dma_sz);
Michael Chan7e72aad2005-07-25 12:31:17 -07007243
Linus Torvalds1da177e2005-04-16 15:20:36 -07007244 /* Initialize invariants of the rings, we only set this
7245 * stuff once. This works because the card does not
7246 * write into the rx buffer posting rings.
7247 */
Matt Carlson2c49a442010-09-30 10:34:35 +00007248 for (i = 0; i <= tp->rx_std_ring_mask; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007249 struct tg3_rx_buffer_desc *rxd;
7250
Matt Carlson21f581a2009-08-28 14:00:25 +00007251 rxd = &tpr->rx_std[i];
Matt Carlson287be122009-08-28 13:58:46 +00007252 rxd->idx_len = rx_pkt_dma_sz << RXD_LEN_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007253 rxd->type_flags = (RXD_FLAG_END << RXD_FLAGS_SHIFT);
7254 rxd->opaque = (RXD_OPAQUE_RING_STD |
7255 (i << RXD_OPAQUE_INDEX_SHIFT));
7256 }
7257
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007258 /* Now allocate fresh SKBs for each rx ring. */
7259 for (i = 0; i < tp->rx_pending; i++) {
Eric Dumazet9205fd92011-11-18 06:47:01 +00007260 if (tg3_alloc_rx_data(tp, tpr, RXD_OPAQUE_RING_STD, i) < 0) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00007261 netdev_warn(tp->dev,
7262 "Using a smaller RX standard ring. Only "
7263 "%d out of %d buffers were allocated "
7264 "successfully\n", i, tp->rx_pending);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007265 if (i == 0)
7266 goto initfail;
7267 tp->rx_pending = i;
7268 break;
7269 }
7270 }
7271
Joe Perches63c3a662011-04-26 08:12:10 +00007272 if (!tg3_flag(tp, JUMBO_CAPABLE) || tg3_flag(tp, 5780_CLASS))
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007273 goto done;
7274
Matt Carlson2c49a442010-09-30 10:34:35 +00007275 memset(tpr->rx_jmb, 0, TG3_RX_JMB_RING_BYTES(tp));
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007276
Joe Perches63c3a662011-04-26 08:12:10 +00007277 if (!tg3_flag(tp, JUMBO_RING_ENABLE))
Matt Carlson0d86df82010-02-17 15:17:00 +00007278 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007279
Matt Carlson2c49a442010-09-30 10:34:35 +00007280 for (i = 0; i <= tp->rx_jmb_ring_mask; i++) {
Matt Carlson0d86df82010-02-17 15:17:00 +00007281 struct tg3_rx_buffer_desc *rxd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007282
Matt Carlson0d86df82010-02-17 15:17:00 +00007283 rxd = &tpr->rx_jmb[i].std;
7284 rxd->idx_len = TG3_RX_JMB_DMA_SZ << RXD_LEN_SHIFT;
7285 rxd->type_flags = (RXD_FLAG_END << RXD_FLAGS_SHIFT) |
7286 RXD_FLAG_JUMBO;
7287 rxd->opaque = (RXD_OPAQUE_RING_JUMBO |
7288 (i << RXD_OPAQUE_INDEX_SHIFT));
7289 }
7290
7291 for (i = 0; i < tp->rx_jumbo_pending; i++) {
Eric Dumazet9205fd92011-11-18 06:47:01 +00007292 if (tg3_alloc_rx_data(tp, tpr, RXD_OPAQUE_RING_JUMBO, i) < 0) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00007293 netdev_warn(tp->dev,
7294 "Using a smaller RX jumbo ring. Only %d "
7295 "out of %d buffers were allocated "
7296 "successfully\n", i, tp->rx_jumbo_pending);
Matt Carlson0d86df82010-02-17 15:17:00 +00007297 if (i == 0)
7298 goto initfail;
7299 tp->rx_jumbo_pending = i;
7300 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007301 }
7302 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007303
7304done:
Michael Chan32d8c572006-07-25 16:38:29 -07007305 return 0;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007306
7307initfail:
Matt Carlson21f581a2009-08-28 14:00:25 +00007308 tg3_rx_prodring_free(tp, tpr);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007309 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007310}
7311
Matt Carlson21f581a2009-08-28 14:00:25 +00007312static void tg3_rx_prodring_fini(struct tg3 *tp,
7313 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007314{
Matt Carlson21f581a2009-08-28 14:00:25 +00007315 kfree(tpr->rx_std_buffers);
7316 tpr->rx_std_buffers = NULL;
7317 kfree(tpr->rx_jmb_buffers);
7318 tpr->rx_jmb_buffers = NULL;
7319 if (tpr->rx_std) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00007320 dma_free_coherent(&tp->pdev->dev, TG3_RX_STD_RING_BYTES(tp),
7321 tpr->rx_std, tpr->rx_std_mapping);
Matt Carlson21f581a2009-08-28 14:00:25 +00007322 tpr->rx_std = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007323 }
Matt Carlson21f581a2009-08-28 14:00:25 +00007324 if (tpr->rx_jmb) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00007325 dma_free_coherent(&tp->pdev->dev, TG3_RX_JMB_RING_BYTES(tp),
7326 tpr->rx_jmb, tpr->rx_jmb_mapping);
Matt Carlson21f581a2009-08-28 14:00:25 +00007327 tpr->rx_jmb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007328 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007329}
7330
Matt Carlson21f581a2009-08-28 14:00:25 +00007331static int tg3_rx_prodring_init(struct tg3 *tp,
7332 struct tg3_rx_prodring_set *tpr)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007333{
Matt Carlson2c49a442010-09-30 10:34:35 +00007334 tpr->rx_std_buffers = kzalloc(TG3_RX_STD_BUFF_RING_SIZE(tp),
7335 GFP_KERNEL);
Matt Carlson21f581a2009-08-28 14:00:25 +00007336 if (!tpr->rx_std_buffers)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007337 return -ENOMEM;
7338
Matt Carlson4bae65c2010-11-24 08:31:52 +00007339 tpr->rx_std = dma_alloc_coherent(&tp->pdev->dev,
7340 TG3_RX_STD_RING_BYTES(tp),
7341 &tpr->rx_std_mapping,
7342 GFP_KERNEL);
Matt Carlson21f581a2009-08-28 14:00:25 +00007343 if (!tpr->rx_std)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007344 goto err_out;
7345
Joe Perches63c3a662011-04-26 08:12:10 +00007346 if (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS)) {
Matt Carlson2c49a442010-09-30 10:34:35 +00007347 tpr->rx_jmb_buffers = kzalloc(TG3_RX_JMB_BUFF_RING_SIZE(tp),
Matt Carlson21f581a2009-08-28 14:00:25 +00007348 GFP_KERNEL);
7349 if (!tpr->rx_jmb_buffers)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007350 goto err_out;
7351
Matt Carlson4bae65c2010-11-24 08:31:52 +00007352 tpr->rx_jmb = dma_alloc_coherent(&tp->pdev->dev,
7353 TG3_RX_JMB_RING_BYTES(tp),
7354 &tpr->rx_jmb_mapping,
7355 GFP_KERNEL);
Matt Carlson21f581a2009-08-28 14:00:25 +00007356 if (!tpr->rx_jmb)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007357 goto err_out;
7358 }
7359
7360 return 0;
7361
7362err_out:
Matt Carlson21f581a2009-08-28 14:00:25 +00007363 tg3_rx_prodring_fini(tp, tpr);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007364 return -ENOMEM;
7365}
7366
7367/* Free up pending packets in all rx/tx rings.
7368 *
7369 * The chip has been shut down and the driver detached from
7370 * the networking, so no interrupts or new tx packets will
7371 * end up in the driver. tp->{tx,}lock is not held and we are not
7372 * in an interrupt context and thus may sleep.
7373 */
7374static void tg3_free_rings(struct tg3 *tp)
7375{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007376 int i, j;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007377
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007378 for (j = 0; j < tp->irq_cnt; j++) {
7379 struct tg3_napi *tnapi = &tp->napi[j];
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007380
Matt Carlson8fea32b2010-09-15 08:59:58 +00007381 tg3_rx_prodring_free(tp, &tnapi->prodring);
Matt Carlsonb28f6422010-06-05 17:24:32 +00007382
Matt Carlson0c1d0e22009-09-01 13:16:33 +00007383 if (!tnapi->tx_buffers)
7384 continue;
7385
Matt Carlson0d681b22011-07-27 14:20:49 +00007386 for (i = 0; i < TG3_TX_RING_SIZE; i++) {
7387 struct sk_buff *skb = tnapi->tx_buffers[i].skb;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007388
Matt Carlson0d681b22011-07-27 14:20:49 +00007389 if (!skb)
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007390 continue;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007391
Matt Carlsonba1142e2011-11-04 09:15:00 +00007392 tg3_tx_skb_unmap(tnapi, i,
7393 skb_shinfo(skb)->nr_frags - 1);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007394
7395 dev_kfree_skb_any(skb);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007396 }
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007397 }
Tom Herbert298376d2011-11-28 16:33:30 +00007398 netdev_reset_queue(tp->dev);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007399}
7400
7401/* Initialize tx/rx rings for packet processing.
7402 *
7403 * The chip has been shut down and the driver detached from
7404 * the networking, so no interrupts or new tx packets will
7405 * end up in the driver. tp->{tx,}lock are held and thus
7406 * we may not sleep.
7407 */
7408static int tg3_init_rings(struct tg3 *tp)
7409{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007410 int i;
Matt Carlson72334482009-08-28 14:03:01 +00007411
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007412 /* Free up all the SKBs. */
7413 tg3_free_rings(tp);
7414
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007415 for (i = 0; i < tp->irq_cnt; i++) {
7416 struct tg3_napi *tnapi = &tp->napi[i];
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007417
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007418 tnapi->last_tag = 0;
7419 tnapi->last_irq_tag = 0;
7420 tnapi->hw_status->status = 0;
7421 tnapi->hw_status->status_tag = 0;
7422 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
7423
7424 tnapi->tx_prod = 0;
7425 tnapi->tx_cons = 0;
Matt Carlson0c1d0e22009-09-01 13:16:33 +00007426 if (tnapi->tx_ring)
7427 memset(tnapi->tx_ring, 0, TG3_TX_RING_BYTES);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007428
7429 tnapi->rx_rcb_ptr = 0;
Matt Carlson0c1d0e22009-09-01 13:16:33 +00007430 if (tnapi->rx_rcb)
7431 memset(tnapi->rx_rcb, 0, TG3_RX_RCB_RING_BYTES(tp));
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007432
Matt Carlson8fea32b2010-09-15 08:59:58 +00007433 if (tg3_rx_prodring_alloc(tp, &tnapi->prodring)) {
Matt Carlsone4af1af2010-02-12 14:47:05 +00007434 tg3_free_rings(tp);
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007435 return -ENOMEM;
Matt Carlsone4af1af2010-02-12 14:47:05 +00007436 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007437 }
Matt Carlson72334482009-08-28 14:03:01 +00007438
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007439 return 0;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007440}
7441
7442/*
7443 * Must not be invoked with interrupt sources disabled and
7444 * the hardware shutdown down.
7445 */
7446static void tg3_free_consistent(struct tg3 *tp)
7447{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007448 int i;
Matt Carlson898a56f2009-08-28 14:02:40 +00007449
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007450 for (i = 0; i < tp->irq_cnt; i++) {
7451 struct tg3_napi *tnapi = &tp->napi[i];
7452
7453 if (tnapi->tx_ring) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00007454 dma_free_coherent(&tp->pdev->dev, TG3_TX_RING_BYTES,
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007455 tnapi->tx_ring, tnapi->tx_desc_mapping);
7456 tnapi->tx_ring = NULL;
7457 }
7458
7459 kfree(tnapi->tx_buffers);
7460 tnapi->tx_buffers = NULL;
7461
7462 if (tnapi->rx_rcb) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00007463 dma_free_coherent(&tp->pdev->dev,
7464 TG3_RX_RCB_RING_BYTES(tp),
7465 tnapi->rx_rcb,
7466 tnapi->rx_rcb_mapping);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007467 tnapi->rx_rcb = NULL;
7468 }
7469
Matt Carlson8fea32b2010-09-15 08:59:58 +00007470 tg3_rx_prodring_fini(tp, &tnapi->prodring);
7471
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007472 if (tnapi->hw_status) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00007473 dma_free_coherent(&tp->pdev->dev, TG3_HW_STATUS_SIZE,
7474 tnapi->hw_status,
7475 tnapi->status_mapping);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007476 tnapi->hw_status = NULL;
7477 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007478 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007479
Linus Torvalds1da177e2005-04-16 15:20:36 -07007480 if (tp->hw_stats) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00007481 dma_free_coherent(&tp->pdev->dev, sizeof(struct tg3_hw_stats),
7482 tp->hw_stats, tp->stats_mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007483 tp->hw_stats = NULL;
7484 }
7485}
7486
7487/*
7488 * Must not be invoked with interrupt sources disabled and
7489 * the hardware shutdown down. Can sleep.
7490 */
7491static int tg3_alloc_consistent(struct tg3 *tp)
7492{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007493 int i;
Matt Carlson898a56f2009-08-28 14:02:40 +00007494
Matt Carlson4bae65c2010-11-24 08:31:52 +00007495 tp->hw_stats = dma_alloc_coherent(&tp->pdev->dev,
7496 sizeof(struct tg3_hw_stats),
7497 &tp->stats_mapping,
7498 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007499 if (!tp->hw_stats)
7500 goto err_out;
7501
Linus Torvalds1da177e2005-04-16 15:20:36 -07007502 memset(tp->hw_stats, 0, sizeof(struct tg3_hw_stats));
7503
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007504 for (i = 0; i < tp->irq_cnt; i++) {
7505 struct tg3_napi *tnapi = &tp->napi[i];
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00007506 struct tg3_hw_status *sblk;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007507
Matt Carlson4bae65c2010-11-24 08:31:52 +00007508 tnapi->hw_status = dma_alloc_coherent(&tp->pdev->dev,
7509 TG3_HW_STATUS_SIZE,
7510 &tnapi->status_mapping,
7511 GFP_KERNEL);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007512 if (!tnapi->hw_status)
7513 goto err_out;
7514
7515 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00007516 sblk = tnapi->hw_status;
7517
Matt Carlson8fea32b2010-09-15 08:59:58 +00007518 if (tg3_rx_prodring_init(tp, &tnapi->prodring))
7519 goto err_out;
7520
Matt Carlson19cfaec2009-12-03 08:36:20 +00007521 /* If multivector TSS is enabled, vector 0 does not handle
7522 * tx interrupts. Don't allocate any resources for it.
7523 */
Joe Perches63c3a662011-04-26 08:12:10 +00007524 if ((!i && !tg3_flag(tp, ENABLE_TSS)) ||
7525 (i && tg3_flag(tp, ENABLE_TSS))) {
Matt Carlsondf8944c2011-07-27 14:20:46 +00007526 tnapi->tx_buffers = kzalloc(
7527 sizeof(struct tg3_tx_ring_info) *
7528 TG3_TX_RING_SIZE, GFP_KERNEL);
Matt Carlson19cfaec2009-12-03 08:36:20 +00007529 if (!tnapi->tx_buffers)
7530 goto err_out;
7531
Matt Carlson4bae65c2010-11-24 08:31:52 +00007532 tnapi->tx_ring = dma_alloc_coherent(&tp->pdev->dev,
7533 TG3_TX_RING_BYTES,
7534 &tnapi->tx_desc_mapping,
7535 GFP_KERNEL);
Matt Carlson19cfaec2009-12-03 08:36:20 +00007536 if (!tnapi->tx_ring)
7537 goto err_out;
7538 }
7539
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00007540 /*
7541 * When RSS is enabled, the status block format changes
7542 * slightly. The "rx_jumbo_consumer", "reserved",
7543 * and "rx_mini_consumer" members get mapped to the
7544 * other three rx return ring producer indexes.
7545 */
7546 switch (i) {
7547 default:
7548 tnapi->rx_rcb_prod_idx = &sblk->idx[0].rx_producer;
7549 break;
7550 case 2:
7551 tnapi->rx_rcb_prod_idx = &sblk->rx_jumbo_consumer;
7552 break;
7553 case 3:
7554 tnapi->rx_rcb_prod_idx = &sblk->reserved;
7555 break;
7556 case 4:
7557 tnapi->rx_rcb_prod_idx = &sblk->rx_mini_consumer;
7558 break;
7559 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007560
Matt Carlson0c1d0e22009-09-01 13:16:33 +00007561 /*
7562 * If multivector RSS is enabled, vector 0 does not handle
7563 * rx or tx interrupts. Don't allocate any resources for it.
7564 */
Joe Perches63c3a662011-04-26 08:12:10 +00007565 if (!i && tg3_flag(tp, ENABLE_RSS))
Matt Carlson0c1d0e22009-09-01 13:16:33 +00007566 continue;
7567
Matt Carlson4bae65c2010-11-24 08:31:52 +00007568 tnapi->rx_rcb = dma_alloc_coherent(&tp->pdev->dev,
7569 TG3_RX_RCB_RING_BYTES(tp),
7570 &tnapi->rx_rcb_mapping,
7571 GFP_KERNEL);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007572 if (!tnapi->rx_rcb)
7573 goto err_out;
7574
7575 memset(tnapi->rx_rcb, 0, TG3_RX_RCB_RING_BYTES(tp));
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007576 }
7577
Linus Torvalds1da177e2005-04-16 15:20:36 -07007578 return 0;
7579
7580err_out:
7581 tg3_free_consistent(tp);
7582 return -ENOMEM;
7583}
7584
7585#define MAX_WAIT_CNT 1000
7586
7587/* To stop a block, clear the enable bit and poll till it
7588 * clears. tp->lock is held.
7589 */
David S. Millerb3b7d6b2005-05-05 14:40:20 -07007590static int tg3_stop_block(struct tg3 *tp, unsigned long ofs, u32 enable_bit, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007591{
7592 unsigned int i;
7593 u32 val;
7594
Joe Perches63c3a662011-04-26 08:12:10 +00007595 if (tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007596 switch (ofs) {
7597 case RCVLSC_MODE:
7598 case DMAC_MODE:
7599 case MBFREE_MODE:
7600 case BUFMGR_MODE:
7601 case MEMARB_MODE:
7602 /* We can't enable/disable these bits of the
7603 * 5705/5750, just say success.
7604 */
7605 return 0;
7606
7607 default:
7608 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07007609 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007610 }
7611
7612 val = tr32(ofs);
7613 val &= ~enable_bit;
7614 tw32_f(ofs, val);
7615
7616 for (i = 0; i < MAX_WAIT_CNT; i++) {
7617 udelay(100);
7618 val = tr32(ofs);
7619 if ((val & enable_bit) == 0)
7620 break;
7621 }
7622
David S. Millerb3b7d6b2005-05-05 14:40:20 -07007623 if (i == MAX_WAIT_CNT && !silent) {
Matt Carlson2445e462010-04-05 10:19:21 +00007624 dev_err(&tp->pdev->dev,
7625 "tg3_stop_block timed out, ofs=%lx enable_bit=%x\n",
7626 ofs, enable_bit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007627 return -ENODEV;
7628 }
7629
7630 return 0;
7631}
7632
7633/* tp->lock is held. */
David S. Millerb3b7d6b2005-05-05 14:40:20 -07007634static int tg3_abort_hw(struct tg3 *tp, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007635{
7636 int i, err;
7637
7638 tg3_disable_ints(tp);
7639
7640 tp->rx_mode &= ~RX_MODE_ENABLE;
7641 tw32_f(MAC_RX_MODE, tp->rx_mode);
7642 udelay(10);
7643
David S. Millerb3b7d6b2005-05-05 14:40:20 -07007644 err = tg3_stop_block(tp, RCVBDI_MODE, RCVBDI_MODE_ENABLE, silent);
7645 err |= tg3_stop_block(tp, RCVLPC_MODE, RCVLPC_MODE_ENABLE, silent);
7646 err |= tg3_stop_block(tp, RCVLSC_MODE, RCVLSC_MODE_ENABLE, silent);
7647 err |= tg3_stop_block(tp, RCVDBDI_MODE, RCVDBDI_MODE_ENABLE, silent);
7648 err |= tg3_stop_block(tp, RCVDCC_MODE, RCVDCC_MODE_ENABLE, silent);
7649 err |= tg3_stop_block(tp, RCVCC_MODE, RCVCC_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007650
David S. Millerb3b7d6b2005-05-05 14:40:20 -07007651 err |= tg3_stop_block(tp, SNDBDS_MODE, SNDBDS_MODE_ENABLE, silent);
7652 err |= tg3_stop_block(tp, SNDBDI_MODE, SNDBDI_MODE_ENABLE, silent);
7653 err |= tg3_stop_block(tp, SNDDATAI_MODE, SNDDATAI_MODE_ENABLE, silent);
7654 err |= tg3_stop_block(tp, RDMAC_MODE, RDMAC_MODE_ENABLE, silent);
7655 err |= tg3_stop_block(tp, SNDDATAC_MODE, SNDDATAC_MODE_ENABLE, silent);
7656 err |= tg3_stop_block(tp, DMAC_MODE, DMAC_MODE_ENABLE, silent);
7657 err |= tg3_stop_block(tp, SNDBDC_MODE, SNDBDC_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007658
7659 tp->mac_mode &= ~MAC_MODE_TDE_ENABLE;
7660 tw32_f(MAC_MODE, tp->mac_mode);
7661 udelay(40);
7662
7663 tp->tx_mode &= ~TX_MODE_ENABLE;
7664 tw32_f(MAC_TX_MODE, tp->tx_mode);
7665
7666 for (i = 0; i < MAX_WAIT_CNT; i++) {
7667 udelay(100);
7668 if (!(tr32(MAC_TX_MODE) & TX_MODE_ENABLE))
7669 break;
7670 }
7671 if (i >= MAX_WAIT_CNT) {
Matt Carlsonab96b242010-04-05 10:19:22 +00007672 dev_err(&tp->pdev->dev,
7673 "%s timed out, TX_MODE_ENABLE will not clear "
7674 "MAC_TX_MODE=%08x\n", __func__, tr32(MAC_TX_MODE));
Michael Chane6de8ad2005-05-05 14:42:41 -07007675 err |= -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007676 }
7677
Michael Chane6de8ad2005-05-05 14:42:41 -07007678 err |= tg3_stop_block(tp, HOSTCC_MODE, HOSTCC_MODE_ENABLE, silent);
David S. Millerb3b7d6b2005-05-05 14:40:20 -07007679 err |= tg3_stop_block(tp, WDMAC_MODE, WDMAC_MODE_ENABLE, silent);
7680 err |= tg3_stop_block(tp, MBFREE_MODE, MBFREE_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007681
7682 tw32(FTQ_RESET, 0xffffffff);
7683 tw32(FTQ_RESET, 0x00000000);
7684
David S. Millerb3b7d6b2005-05-05 14:40:20 -07007685 err |= tg3_stop_block(tp, BUFMGR_MODE, BUFMGR_MODE_ENABLE, silent);
7686 err |= tg3_stop_block(tp, MEMARB_MODE, MEMARB_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007687
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007688 for (i = 0; i < tp->irq_cnt; i++) {
7689 struct tg3_napi *tnapi = &tp->napi[i];
7690 if (tnapi->hw_status)
7691 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
7692 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007693
Linus Torvalds1da177e2005-04-16 15:20:36 -07007694 return err;
7695}
7696
Michael Chanee6a99b2007-07-18 21:49:10 -07007697/* Save PCI command register before chip reset */
7698static void tg3_save_pci_state(struct tg3 *tp)
7699{
Matt Carlson8a6eac92007-10-21 16:17:55 -07007700 pci_read_config_word(tp->pdev, PCI_COMMAND, &tp->pci_cmd);
Michael Chanee6a99b2007-07-18 21:49:10 -07007701}
7702
7703/* Restore PCI state after chip reset */
7704static void tg3_restore_pci_state(struct tg3 *tp)
7705{
7706 u32 val;
7707
7708 /* Re-enable indirect register accesses. */
7709 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
7710 tp->misc_host_ctrl);
7711
7712 /* Set MAX PCI retry to zero. */
7713 val = (PCISTATE_ROM_ENABLE | PCISTATE_ROM_RETRY_ENABLE);
7714 if (tp->pci_chip_rev_id == CHIPREV_ID_5704_A0 &&
Joe Perches63c3a662011-04-26 08:12:10 +00007715 tg3_flag(tp, PCIX_MODE))
Michael Chanee6a99b2007-07-18 21:49:10 -07007716 val |= PCISTATE_RETRY_SAME_DMA;
Matt Carlson0d3031d2007-10-10 18:02:43 -07007717 /* Allow reads and writes to the APE register and memory space. */
Joe Perches63c3a662011-04-26 08:12:10 +00007718 if (tg3_flag(tp, ENABLE_APE))
Matt Carlson0d3031d2007-10-10 18:02:43 -07007719 val |= PCISTATE_ALLOW_APE_CTLSPC_WR |
Matt Carlsonf92d9dc12010-06-05 17:24:30 +00007720 PCISTATE_ALLOW_APE_SHMEM_WR |
7721 PCISTATE_ALLOW_APE_PSPACE_WR;
Michael Chanee6a99b2007-07-18 21:49:10 -07007722 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE, val);
7723
Matt Carlson8a6eac92007-10-21 16:17:55 -07007724 pci_write_config_word(tp->pdev, PCI_COMMAND, tp->pci_cmd);
Michael Chanee6a99b2007-07-18 21:49:10 -07007725
Matt Carlson2c55a3d2011-11-28 09:41:04 +00007726 if (!tg3_flag(tp, PCI_EXPRESS)) {
7727 pci_write_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE,
7728 tp->pci_cacheline_sz);
7729 pci_write_config_byte(tp->pdev, PCI_LATENCY_TIMER,
7730 tp->pci_lat_timer);
Michael Chan114342f2007-10-15 02:12:26 -07007731 }
Matt Carlson5f5c51e2007-11-12 21:19:37 -08007732
Michael Chanee6a99b2007-07-18 21:49:10 -07007733 /* Make sure PCI-X relaxed ordering bit is clear. */
Joe Perches63c3a662011-04-26 08:12:10 +00007734 if (tg3_flag(tp, PCIX_MODE)) {
Matt Carlson9974a352007-10-07 23:27:28 -07007735 u16 pcix_cmd;
7736
7737 pci_read_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
7738 &pcix_cmd);
7739 pcix_cmd &= ~PCI_X_CMD_ERO;
7740 pci_write_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
7741 pcix_cmd);
7742 }
Michael Chanee6a99b2007-07-18 21:49:10 -07007743
Joe Perches63c3a662011-04-26 08:12:10 +00007744 if (tg3_flag(tp, 5780_CLASS)) {
Michael Chanee6a99b2007-07-18 21:49:10 -07007745
7746 /* Chip reset on 5780 will reset MSI enable bit,
7747 * so need to restore it.
7748 */
Joe Perches63c3a662011-04-26 08:12:10 +00007749 if (tg3_flag(tp, USING_MSI)) {
Michael Chanee6a99b2007-07-18 21:49:10 -07007750 u16 ctrl;
7751
7752 pci_read_config_word(tp->pdev,
7753 tp->msi_cap + PCI_MSI_FLAGS,
7754 &ctrl);
7755 pci_write_config_word(tp->pdev,
7756 tp->msi_cap + PCI_MSI_FLAGS,
7757 ctrl | PCI_MSI_FLAGS_ENABLE);
7758 val = tr32(MSGINT_MODE);
7759 tw32(MSGINT_MODE, val | MSGINT_MODE_ENABLE);
7760 }
7761 }
7762}
7763
Linus Torvalds1da177e2005-04-16 15:20:36 -07007764/* tp->lock is held. */
7765static int tg3_chip_reset(struct tg3 *tp)
7766{
7767 u32 val;
Michael Chan1ee582d2005-08-09 20:16:46 -07007768 void (*write_op)(struct tg3 *, u32, u32);
Matt Carlson4f125f42009-09-01 12:55:02 +00007769 int i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007770
David S. Millerf49639e2006-06-09 11:58:36 -07007771 tg3_nvram_lock(tp);
7772
Matt Carlson77b483f2008-08-15 14:07:24 -07007773 tg3_ape_lock(tp, TG3_APE_LOCK_GRC);
7774
David S. Millerf49639e2006-06-09 11:58:36 -07007775 /* No matching tg3_nvram_unlock() after this because
7776 * chip reset below will undo the nvram lock.
7777 */
7778 tp->nvram_lock_cnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007779
Michael Chanee6a99b2007-07-18 21:49:10 -07007780 /* GRC_MISC_CFG core clock reset will clear the memory
7781 * enable bit in PCI register 4 and the MSI enable bit
7782 * on some chips, so we save relevant registers here.
7783 */
7784 tg3_save_pci_state(tp);
7785
Michael Chand9ab5ad2006-03-20 22:27:35 -08007786 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752 ||
Joe Perches63c3a662011-04-26 08:12:10 +00007787 tg3_flag(tp, 5755_PLUS))
Michael Chand9ab5ad2006-03-20 22:27:35 -08007788 tw32(GRC_FASTBOOT_PC, 0);
7789
Linus Torvalds1da177e2005-04-16 15:20:36 -07007790 /*
7791 * We must avoid the readl() that normally takes place.
7792 * It locks machines, causes machine checks, and other
7793 * fun things. So, temporarily disable the 5701
7794 * hardware workaround, while we do the reset.
7795 */
Michael Chan1ee582d2005-08-09 20:16:46 -07007796 write_op = tp->write32;
7797 if (write_op == tg3_write_flush_reg32)
7798 tp->write32 = tg3_write32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007799
Michael Chand18edcb2007-03-24 20:57:11 -07007800 /* Prevent the irq handler from reading or writing PCI registers
7801 * during chip reset when the memory enable bit in the PCI command
7802 * register may be cleared. The chip does not generate interrupt
7803 * at this time, but the irq handler may still be called due to irq
7804 * sharing or irqpoll.
7805 */
Joe Perches63c3a662011-04-26 08:12:10 +00007806 tg3_flag_set(tp, CHIP_RESETTING);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007807 for (i = 0; i < tp->irq_cnt; i++) {
7808 struct tg3_napi *tnapi = &tp->napi[i];
7809 if (tnapi->hw_status) {
7810 tnapi->hw_status->status = 0;
7811 tnapi->hw_status->status_tag = 0;
7812 }
7813 tnapi->last_tag = 0;
7814 tnapi->last_irq_tag = 0;
Michael Chanb8fa2f32007-04-06 17:35:37 -07007815 }
Michael Chand18edcb2007-03-24 20:57:11 -07007816 smp_mb();
Matt Carlson4f125f42009-09-01 12:55:02 +00007817
7818 for (i = 0; i < tp->irq_cnt; i++)
7819 synchronize_irq(tp->napi[i].irq_vec);
Michael Chand18edcb2007-03-24 20:57:11 -07007820
Matt Carlson255ca312009-08-25 10:07:27 +00007821 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780) {
7822 val = tr32(TG3_PCIE_LNKCTL) & ~TG3_PCIE_LNKCTL_L1_PLL_PD_EN;
7823 tw32(TG3_PCIE_LNKCTL, val | TG3_PCIE_LNKCTL_L1_PLL_PD_DIS);
7824 }
7825
Linus Torvalds1da177e2005-04-16 15:20:36 -07007826 /* do the reset */
7827 val = GRC_MISC_CFG_CORECLK_RESET;
7828
Joe Perches63c3a662011-04-26 08:12:10 +00007829 if (tg3_flag(tp, PCI_EXPRESS)) {
Matt Carlson88075d92010-08-02 11:25:58 +00007830 /* Force PCIe 1.0a mode */
7831 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
Joe Perches63c3a662011-04-26 08:12:10 +00007832 !tg3_flag(tp, 57765_PLUS) &&
Matt Carlson88075d92010-08-02 11:25:58 +00007833 tr32(TG3_PCIE_PHY_TSTCTL) ==
7834 (TG3_PCIE_PHY_TSTCTL_PCIE10 | TG3_PCIE_PHY_TSTCTL_PSCRAM))
7835 tw32(TG3_PCIE_PHY_TSTCTL, TG3_PCIE_PHY_TSTCTL_PSCRAM);
7836
Linus Torvalds1da177e2005-04-16 15:20:36 -07007837 if (tp->pci_chip_rev_id != CHIPREV_ID_5750_A0) {
7838 tw32(GRC_MISC_CFG, (1 << 29));
7839 val |= (1 << 29);
7840 }
7841 }
7842
Michael Chanb5d37722006-09-27 16:06:21 -07007843 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
7844 tw32(VCPU_STATUS, tr32(VCPU_STATUS) | VCPU_STATUS_DRV_RESET);
7845 tw32(GRC_VCPU_EXT_CTRL,
7846 tr32(GRC_VCPU_EXT_CTRL) & ~GRC_VCPU_EXT_CTRL_HALT_CPU);
7847 }
7848
Matt Carlsonf37500d2010-08-02 11:25:59 +00007849 /* Manage gphy power for all CPMU absent PCIe devices. */
Joe Perches63c3a662011-04-26 08:12:10 +00007850 if (tg3_flag(tp, 5705_PLUS) && !tg3_flag(tp, CPMU_PRESENT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007851 val |= GRC_MISC_CFG_KEEP_GPHY_POWER;
Matt Carlsonf37500d2010-08-02 11:25:59 +00007852
Linus Torvalds1da177e2005-04-16 15:20:36 -07007853 tw32(GRC_MISC_CFG, val);
7854
Michael Chan1ee582d2005-08-09 20:16:46 -07007855 /* restore 5701 hardware bug workaround write method */
7856 tp->write32 = write_op;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007857
7858 /* Unfortunately, we have to delay before the PCI read back.
7859 * Some 575X chips even will not respond to a PCI cfg access
7860 * when the reset command is given to the chip.
7861 *
7862 * How do these hardware designers expect things to work
7863 * properly if the PCI write is posted for a long period
7864 * of time? It is always necessary to have some method by
7865 * which a register read back can occur to push the write
7866 * out which does the reset.
7867 *
7868 * For most tg3 variants the trick below was working.
7869 * Ho hum...
7870 */
7871 udelay(120);
7872
7873 /* Flush PCI posted writes. The normal MMIO registers
7874 * are inaccessible at this time so this is the only
7875 * way to make this reliably (actually, this is no longer
7876 * the case, see above). I tried to use indirect
7877 * register read/write but this upset some 5701 variants.
7878 */
7879 pci_read_config_dword(tp->pdev, PCI_COMMAND, &val);
7880
7881 udelay(120);
7882
Jon Mason708ebb32011-06-27 12:56:50 +00007883 if (tg3_flag(tp, PCI_EXPRESS) && pci_pcie_cap(tp->pdev)) {
Matt Carlsone7126992009-08-25 10:08:16 +00007884 u16 val16;
7885
Linus Torvalds1da177e2005-04-16 15:20:36 -07007886 if (tp->pci_chip_rev_id == CHIPREV_ID_5750_A0) {
7887 int i;
7888 u32 cfg_val;
7889
7890 /* Wait for link training to complete. */
7891 for (i = 0; i < 5000; i++)
7892 udelay(100);
7893
7894 pci_read_config_dword(tp->pdev, 0xc4, &cfg_val);
7895 pci_write_config_dword(tp->pdev, 0xc4,
7896 cfg_val | (1 << 15));
7897 }
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007898
Matt Carlsone7126992009-08-25 10:08:16 +00007899 /* Clear the "no snoop" and "relaxed ordering" bits. */
7900 pci_read_config_word(tp->pdev,
Jon Mason708ebb32011-06-27 12:56:50 +00007901 pci_pcie_cap(tp->pdev) + PCI_EXP_DEVCTL,
Matt Carlsone7126992009-08-25 10:08:16 +00007902 &val16);
7903 val16 &= ~(PCI_EXP_DEVCTL_RELAX_EN |
7904 PCI_EXP_DEVCTL_NOSNOOP_EN);
7905 /*
7906 * Older PCIe devices only support the 128 byte
7907 * MPS setting. Enforce the restriction.
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007908 */
Joe Perches63c3a662011-04-26 08:12:10 +00007909 if (!tg3_flag(tp, CPMU_PRESENT))
Matt Carlsone7126992009-08-25 10:08:16 +00007910 val16 &= ~PCI_EXP_DEVCTL_PAYLOAD;
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007911 pci_write_config_word(tp->pdev,
Jon Mason708ebb32011-06-27 12:56:50 +00007912 pci_pcie_cap(tp->pdev) + PCI_EXP_DEVCTL,
Matt Carlsone7126992009-08-25 10:08:16 +00007913 val16);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007914
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007915 /* Clear error status */
7916 pci_write_config_word(tp->pdev,
Jon Mason708ebb32011-06-27 12:56:50 +00007917 pci_pcie_cap(tp->pdev) + PCI_EXP_DEVSTA,
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007918 PCI_EXP_DEVSTA_CED |
7919 PCI_EXP_DEVSTA_NFED |
7920 PCI_EXP_DEVSTA_FED |
7921 PCI_EXP_DEVSTA_URD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007922 }
7923
Michael Chanee6a99b2007-07-18 21:49:10 -07007924 tg3_restore_pci_state(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007925
Joe Perches63c3a662011-04-26 08:12:10 +00007926 tg3_flag_clear(tp, CHIP_RESETTING);
7927 tg3_flag_clear(tp, ERROR_PROCESSED);
Michael Chand18edcb2007-03-24 20:57:11 -07007928
Michael Chanee6a99b2007-07-18 21:49:10 -07007929 val = 0;
Joe Perches63c3a662011-04-26 08:12:10 +00007930 if (tg3_flag(tp, 5780_CLASS))
Michael Chan4cf78e42005-07-25 12:29:19 -07007931 val = tr32(MEMARB_MODE);
Michael Chanee6a99b2007-07-18 21:49:10 -07007932 tw32(MEMARB_MODE, val | MEMARB_MODE_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007933
7934 if (tp->pci_chip_rev_id == CHIPREV_ID_5750_A3) {
7935 tg3_stop_fw(tp);
7936 tw32(0x5000, 0x400);
7937 }
7938
7939 tw32(GRC_MODE, tp->grc_mode);
7940
7941 if (tp->pci_chip_rev_id == CHIPREV_ID_5705_A0) {
Andy Gospodarekab0049b2007-09-06 20:42:14 +01007942 val = tr32(0xc4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007943
7944 tw32(0xc4, val | (1 << 15));
7945 }
7946
7947 if ((tp->nic_sram_data_cfg & NIC_SRAM_DATA_CFG_MINI_PCI) != 0 &&
7948 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
7949 tp->pci_clock_ctrl |= CLOCK_CTRL_CLKRUN_OENABLE;
7950 if (tp->pci_chip_rev_id == CHIPREV_ID_5705_A0)
7951 tp->pci_clock_ctrl |= CLOCK_CTRL_FORCE_CLKRUN;
7952 tw32(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl);
7953 }
7954
Matt Carlsonf07e9af2010-08-02 11:26:07 +00007955 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Matt Carlson9e975cc2011-07-20 10:20:50 +00007956 tp->mac_mode = MAC_MODE_PORT_MODE_TBI;
Matt Carlsond2394e6b2010-11-24 08:31:47 +00007957 val = tp->mac_mode;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00007958 } else if (tp->phy_flags & TG3_PHYFLG_MII_SERDES) {
Matt Carlson9e975cc2011-07-20 10:20:50 +00007959 tp->mac_mode = MAC_MODE_PORT_MODE_GMII;
Matt Carlsond2394e6b2010-11-24 08:31:47 +00007960 val = tp->mac_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007961 } else
Matt Carlsond2394e6b2010-11-24 08:31:47 +00007962 val = 0;
7963
7964 tw32_f(MAC_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007965 udelay(40);
7966
Matt Carlson77b483f2008-08-15 14:07:24 -07007967 tg3_ape_unlock(tp, TG3_APE_LOCK_GRC);
7968
Michael Chan7a6f4362006-09-27 16:03:31 -07007969 err = tg3_poll_fw(tp);
7970 if (err)
7971 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007972
Matt Carlson0a9140c2009-08-28 12:27:50 +00007973 tg3_mdio_start(tp);
7974
Joe Perches63c3a662011-04-26 08:12:10 +00007975 if (tg3_flag(tp, PCI_EXPRESS) &&
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00007976 tp->pci_chip_rev_id != CHIPREV_ID_5750_A0 &&
7977 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
Joe Perches63c3a662011-04-26 08:12:10 +00007978 !tg3_flag(tp, 57765_PLUS)) {
Andy Gospodarekab0049b2007-09-06 20:42:14 +01007979 val = tr32(0x7c00);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007980
7981 tw32(0x7c00, val | (1 << 25));
7982 }
7983
Matt Carlsond78b59f2011-04-05 14:22:46 +00007984 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
7985 val = tr32(TG3_CPMU_CLCK_ORIDE);
7986 tw32(TG3_CPMU_CLCK_ORIDE, val & ~CPMU_CLCK_ORIDE_MAC_ORIDE_EN);
7987 }
7988
Linus Torvalds1da177e2005-04-16 15:20:36 -07007989 /* Reprobe ASF enable state. */
Joe Perches63c3a662011-04-26 08:12:10 +00007990 tg3_flag_clear(tp, ENABLE_ASF);
7991 tg3_flag_clear(tp, ASF_NEW_HANDSHAKE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007992 tg3_read_mem(tp, NIC_SRAM_DATA_SIG, &val);
7993 if (val == NIC_SRAM_DATA_SIG_MAGIC) {
7994 u32 nic_cfg;
7995
7996 tg3_read_mem(tp, NIC_SRAM_DATA_CFG, &nic_cfg);
7997 if (nic_cfg & NIC_SRAM_DATA_CFG_ASF_ENABLE) {
Joe Perches63c3a662011-04-26 08:12:10 +00007998 tg3_flag_set(tp, ENABLE_ASF);
Matt Carlson4ba526c2008-08-15 14:10:04 -07007999 tp->last_event_jiffies = jiffies;
Joe Perches63c3a662011-04-26 08:12:10 +00008000 if (tg3_flag(tp, 5750_PLUS))
8001 tg3_flag_set(tp, ASF_NEW_HANDSHAKE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008002 }
8003 }
8004
8005 return 0;
8006}
8007
Matt Carlson92feeab2011-12-08 14:40:14 +00008008static struct rtnl_link_stats64 *tg3_get_stats64(struct net_device *,
8009 struct rtnl_link_stats64 *);
8010static struct tg3_ethtool_stats *tg3_get_estats(struct tg3 *,
8011 struct tg3_ethtool_stats *);
8012
Linus Torvalds1da177e2005-04-16 15:20:36 -07008013/* tp->lock is held. */
Michael Chan944d9802005-05-29 14:57:48 -07008014static int tg3_halt(struct tg3 *tp, int kind, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008015{
8016 int err;
8017
8018 tg3_stop_fw(tp);
8019
Michael Chan944d9802005-05-29 14:57:48 -07008020 tg3_write_sig_pre_reset(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008021
David S. Millerb3b7d6b2005-05-05 14:40:20 -07008022 tg3_abort_hw(tp, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008023 err = tg3_chip_reset(tp);
8024
Matt Carlsondaba2a62009-04-20 06:58:52 +00008025 __tg3_set_mac_addr(tp, 0);
8026
Michael Chan944d9802005-05-29 14:57:48 -07008027 tg3_write_sig_legacy(tp, kind);
8028 tg3_write_sig_post_reset(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008029
Matt Carlson92feeab2011-12-08 14:40:14 +00008030 if (tp->hw_stats) {
8031 /* Save the stats across chip resets... */
8032 tg3_get_stats64(tp->dev, &tp->net_stats_prev),
8033 tg3_get_estats(tp, &tp->estats_prev);
8034
8035 /* And make sure the next sample is new data */
8036 memset(tp->hw_stats, 0, sizeof(struct tg3_hw_stats));
8037 }
8038
Linus Torvalds1da177e2005-04-16 15:20:36 -07008039 if (err)
8040 return err;
8041
8042 return 0;
8043}
8044
Linus Torvalds1da177e2005-04-16 15:20:36 -07008045static int tg3_set_mac_addr(struct net_device *dev, void *p)
8046{
8047 struct tg3 *tp = netdev_priv(dev);
8048 struct sockaddr *addr = p;
Michael Chan986e0ae2007-05-05 12:10:20 -07008049 int err = 0, skip_mac_1 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008050
Michael Chanf9804dd2005-09-27 12:13:10 -07008051 if (!is_valid_ether_addr(addr->sa_data))
8052 return -EINVAL;
8053
Linus Torvalds1da177e2005-04-16 15:20:36 -07008054 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
8055
Michael Chane75f7c92006-03-20 21:33:26 -08008056 if (!netif_running(dev))
8057 return 0;
8058
Joe Perches63c3a662011-04-26 08:12:10 +00008059 if (tg3_flag(tp, ENABLE_ASF)) {
Michael Chan986e0ae2007-05-05 12:10:20 -07008060 u32 addr0_high, addr0_low, addr1_high, addr1_low;
Michael Chan58712ef2006-04-29 18:58:01 -07008061
Michael Chan986e0ae2007-05-05 12:10:20 -07008062 addr0_high = tr32(MAC_ADDR_0_HIGH);
8063 addr0_low = tr32(MAC_ADDR_0_LOW);
8064 addr1_high = tr32(MAC_ADDR_1_HIGH);
8065 addr1_low = tr32(MAC_ADDR_1_LOW);
8066
8067 /* Skip MAC addr 1 if ASF is using it. */
8068 if ((addr0_high != addr1_high || addr0_low != addr1_low) &&
8069 !(addr1_high == 0 && addr1_low == 0))
8070 skip_mac_1 = 1;
Michael Chan58712ef2006-04-29 18:58:01 -07008071 }
Michael Chan986e0ae2007-05-05 12:10:20 -07008072 spin_lock_bh(&tp->lock);
8073 __tg3_set_mac_addr(tp, skip_mac_1);
8074 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008075
Michael Chanb9ec6c12006-07-25 16:37:27 -07008076 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008077}
8078
8079/* tp->lock is held. */
8080static void tg3_set_bdinfo(struct tg3 *tp, u32 bdinfo_addr,
8081 dma_addr_t mapping, u32 maxlen_flags,
8082 u32 nic_addr)
8083{
8084 tg3_write_mem(tp,
8085 (bdinfo_addr + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH),
8086 ((u64) mapping >> 32));
8087 tg3_write_mem(tp,
8088 (bdinfo_addr + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW),
8089 ((u64) mapping & 0xffffffff));
8090 tg3_write_mem(tp,
8091 (bdinfo_addr + TG3_BDINFO_MAXLEN_FLAGS),
8092 maxlen_flags);
8093
Joe Perches63c3a662011-04-26 08:12:10 +00008094 if (!tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008095 tg3_write_mem(tp,
8096 (bdinfo_addr + TG3_BDINFO_NIC_ADDR),
8097 nic_addr);
8098}
8099
Michael Chand244c892005-07-05 14:42:33 -07008100static void __tg3_set_coalesce(struct tg3 *tp, struct ethtool_coalesce *ec)
David S. Miller15f98502005-05-18 22:49:26 -07008101{
Matt Carlsonb6080e12009-09-01 13:12:00 +00008102 int i;
8103
Joe Perches63c3a662011-04-26 08:12:10 +00008104 if (!tg3_flag(tp, ENABLE_TSS)) {
Matt Carlsonb6080e12009-09-01 13:12:00 +00008105 tw32(HOSTCC_TXCOL_TICKS, ec->tx_coalesce_usecs);
8106 tw32(HOSTCC_TXMAX_FRAMES, ec->tx_max_coalesced_frames);
8107 tw32(HOSTCC_TXCOAL_MAXF_INT, ec->tx_max_coalesced_frames_irq);
Matt Carlsonb6080e12009-09-01 13:12:00 +00008108 } else {
8109 tw32(HOSTCC_TXCOL_TICKS, 0);
8110 tw32(HOSTCC_TXMAX_FRAMES, 0);
8111 tw32(HOSTCC_TXCOAL_MAXF_INT, 0);
Matt Carlson19cfaec2009-12-03 08:36:20 +00008112 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00008113
Joe Perches63c3a662011-04-26 08:12:10 +00008114 if (!tg3_flag(tp, ENABLE_RSS)) {
Matt Carlson19cfaec2009-12-03 08:36:20 +00008115 tw32(HOSTCC_RXCOL_TICKS, ec->rx_coalesce_usecs);
8116 tw32(HOSTCC_RXMAX_FRAMES, ec->rx_max_coalesced_frames);
8117 tw32(HOSTCC_RXCOAL_MAXF_INT, ec->rx_max_coalesced_frames_irq);
8118 } else {
Matt Carlsonb6080e12009-09-01 13:12:00 +00008119 tw32(HOSTCC_RXCOL_TICKS, 0);
8120 tw32(HOSTCC_RXMAX_FRAMES, 0);
8121 tw32(HOSTCC_RXCOAL_MAXF_INT, 0);
David S. Miller15f98502005-05-18 22:49:26 -07008122 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00008123
Joe Perches63c3a662011-04-26 08:12:10 +00008124 if (!tg3_flag(tp, 5705_PLUS)) {
David S. Miller15f98502005-05-18 22:49:26 -07008125 u32 val = ec->stats_block_coalesce_usecs;
8126
Matt Carlsonb6080e12009-09-01 13:12:00 +00008127 tw32(HOSTCC_RXCOAL_TICK_INT, ec->rx_coalesce_usecs_irq);
8128 tw32(HOSTCC_TXCOAL_TICK_INT, ec->tx_coalesce_usecs_irq);
8129
David S. Miller15f98502005-05-18 22:49:26 -07008130 if (!netif_carrier_ok(tp->dev))
8131 val = 0;
8132
8133 tw32(HOSTCC_STAT_COAL_TICKS, val);
8134 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00008135
8136 for (i = 0; i < tp->irq_cnt - 1; i++) {
8137 u32 reg;
8138
8139 reg = HOSTCC_RXCOL_TICKS_VEC1 + i * 0x18;
8140 tw32(reg, ec->rx_coalesce_usecs);
Matt Carlsonb6080e12009-09-01 13:12:00 +00008141 reg = HOSTCC_RXMAX_FRAMES_VEC1 + i * 0x18;
8142 tw32(reg, ec->rx_max_coalesced_frames);
Matt Carlsonb6080e12009-09-01 13:12:00 +00008143 reg = HOSTCC_RXCOAL_MAXF_INT_VEC1 + i * 0x18;
8144 tw32(reg, ec->rx_max_coalesced_frames_irq);
Matt Carlson19cfaec2009-12-03 08:36:20 +00008145
Joe Perches63c3a662011-04-26 08:12:10 +00008146 if (tg3_flag(tp, ENABLE_TSS)) {
Matt Carlson19cfaec2009-12-03 08:36:20 +00008147 reg = HOSTCC_TXCOL_TICKS_VEC1 + i * 0x18;
8148 tw32(reg, ec->tx_coalesce_usecs);
8149 reg = HOSTCC_TXMAX_FRAMES_VEC1 + i * 0x18;
8150 tw32(reg, ec->tx_max_coalesced_frames);
8151 reg = HOSTCC_TXCOAL_MAXF_INT_VEC1 + i * 0x18;
8152 tw32(reg, ec->tx_max_coalesced_frames_irq);
8153 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00008154 }
8155
8156 for (; i < tp->irq_max - 1; i++) {
8157 tw32(HOSTCC_RXCOL_TICKS_VEC1 + i * 0x18, 0);
Matt Carlsonb6080e12009-09-01 13:12:00 +00008158 tw32(HOSTCC_RXMAX_FRAMES_VEC1 + i * 0x18, 0);
Matt Carlsonb6080e12009-09-01 13:12:00 +00008159 tw32(HOSTCC_RXCOAL_MAXF_INT_VEC1 + i * 0x18, 0);
Matt Carlson19cfaec2009-12-03 08:36:20 +00008160
Joe Perches63c3a662011-04-26 08:12:10 +00008161 if (tg3_flag(tp, ENABLE_TSS)) {
Matt Carlson19cfaec2009-12-03 08:36:20 +00008162 tw32(HOSTCC_TXCOL_TICKS_VEC1 + i * 0x18, 0);
8163 tw32(HOSTCC_TXMAX_FRAMES_VEC1 + i * 0x18, 0);
8164 tw32(HOSTCC_TXCOAL_MAXF_INT_VEC1 + i * 0x18, 0);
8165 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00008166 }
David S. Miller15f98502005-05-18 22:49:26 -07008167}
Linus Torvalds1da177e2005-04-16 15:20:36 -07008168
8169/* tp->lock is held. */
Matt Carlson2d31eca2009-09-01 12:53:31 +00008170static void tg3_rings_reset(struct tg3 *tp)
8171{
8172 int i;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008173 u32 stblk, txrcb, rxrcb, limit;
Matt Carlson2d31eca2009-09-01 12:53:31 +00008174 struct tg3_napi *tnapi = &tp->napi[0];
8175
8176 /* Disable all transmit rings but the first. */
Joe Perches63c3a662011-04-26 08:12:10 +00008177 if (!tg3_flag(tp, 5705_PLUS))
Matt Carlson2d31eca2009-09-01 12:53:31 +00008178 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 16;
Joe Perches63c3a662011-04-26 08:12:10 +00008179 else if (tg3_flag(tp, 5717_PLUS))
Matt Carlson3d377282010-10-14 10:37:39 +00008180 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 4;
Matt Carlson55086ad2011-12-14 11:09:59 +00008181 else if (tg3_flag(tp, 57765_CLASS))
Matt Carlsonb703df62009-12-03 08:36:21 +00008182 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 2;
Matt Carlson2d31eca2009-09-01 12:53:31 +00008183 else
8184 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE;
8185
8186 for (txrcb = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE;
8187 txrcb < limit; txrcb += TG3_BDINFO_SIZE)
8188 tg3_write_mem(tp, txrcb + TG3_BDINFO_MAXLEN_FLAGS,
8189 BDINFO_FLAGS_DISABLED);
8190
8191
8192 /* Disable all receive return rings but the first. */
Joe Perches63c3a662011-04-26 08:12:10 +00008193 if (tg3_flag(tp, 5717_PLUS))
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00008194 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 17;
Joe Perches63c3a662011-04-26 08:12:10 +00008195 else if (!tg3_flag(tp, 5705_PLUS))
Matt Carlson2d31eca2009-09-01 12:53:31 +00008196 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 16;
Matt Carlsonb703df62009-12-03 08:36:21 +00008197 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
Matt Carlson55086ad2011-12-14 11:09:59 +00008198 tg3_flag(tp, 57765_CLASS))
Matt Carlson2d31eca2009-09-01 12:53:31 +00008199 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 4;
8200 else
8201 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE;
8202
8203 for (rxrcb = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE;
8204 rxrcb < limit; rxrcb += TG3_BDINFO_SIZE)
8205 tg3_write_mem(tp, rxrcb + TG3_BDINFO_MAXLEN_FLAGS,
8206 BDINFO_FLAGS_DISABLED);
8207
8208 /* Disable interrupts */
8209 tw32_mailbox_f(tp->napi[0].int_mbox, 1);
Matt Carlson0e6cf6a2011-06-13 13:38:55 +00008210 tp->napi[0].chk_msi_cnt = 0;
8211 tp->napi[0].last_rx_cons = 0;
8212 tp->napi[0].last_tx_cons = 0;
Matt Carlson2d31eca2009-09-01 12:53:31 +00008213
8214 /* Zero mailbox registers. */
Joe Perches63c3a662011-04-26 08:12:10 +00008215 if (tg3_flag(tp, SUPPORT_MSIX)) {
Matt Carlson6fd45cb2010-09-15 08:59:57 +00008216 for (i = 1; i < tp->irq_max; i++) {
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008217 tp->napi[i].tx_prod = 0;
8218 tp->napi[i].tx_cons = 0;
Joe Perches63c3a662011-04-26 08:12:10 +00008219 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlsonc2353a32010-01-20 16:58:08 +00008220 tw32_mailbox(tp->napi[i].prodmbox, 0);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008221 tw32_rx_mbox(tp->napi[i].consmbox, 0);
8222 tw32_mailbox_f(tp->napi[i].int_mbox, 1);
Matt Carlson7f230732011-08-31 11:44:48 +00008223 tp->napi[i].chk_msi_cnt = 0;
Matt Carlson0e6cf6a2011-06-13 13:38:55 +00008224 tp->napi[i].last_rx_cons = 0;
8225 tp->napi[i].last_tx_cons = 0;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008226 }
Joe Perches63c3a662011-04-26 08:12:10 +00008227 if (!tg3_flag(tp, ENABLE_TSS))
Matt Carlsonc2353a32010-01-20 16:58:08 +00008228 tw32_mailbox(tp->napi[0].prodmbox, 0);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008229 } else {
8230 tp->napi[0].tx_prod = 0;
8231 tp->napi[0].tx_cons = 0;
8232 tw32_mailbox(tp->napi[0].prodmbox, 0);
8233 tw32_rx_mbox(tp->napi[0].consmbox, 0);
8234 }
Matt Carlson2d31eca2009-09-01 12:53:31 +00008235
8236 /* Make sure the NIC-based send BD rings are disabled. */
Joe Perches63c3a662011-04-26 08:12:10 +00008237 if (!tg3_flag(tp, 5705_PLUS)) {
Matt Carlson2d31eca2009-09-01 12:53:31 +00008238 u32 mbox = MAILBOX_SNDNIC_PROD_IDX_0 + TG3_64BIT_REG_LOW;
8239 for (i = 0; i < 16; i++)
8240 tw32_tx_mbox(mbox + i * 8, 0);
8241 }
8242
8243 txrcb = NIC_SRAM_SEND_RCB;
8244 rxrcb = NIC_SRAM_RCV_RET_RCB;
8245
8246 /* Clear status block in ram. */
8247 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
8248
8249 /* Set status block DMA address */
8250 tw32(HOSTCC_STATUS_BLK_HOST_ADDR + TG3_64BIT_REG_HIGH,
8251 ((u64) tnapi->status_mapping >> 32));
8252 tw32(HOSTCC_STATUS_BLK_HOST_ADDR + TG3_64BIT_REG_LOW,
8253 ((u64) tnapi->status_mapping & 0xffffffff));
8254
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008255 if (tnapi->tx_ring) {
8256 tg3_set_bdinfo(tp, txrcb, tnapi->tx_desc_mapping,
8257 (TG3_TX_RING_SIZE <<
8258 BDINFO_FLAGS_MAXLEN_SHIFT),
8259 NIC_SRAM_TX_BUFFER_DESC);
8260 txrcb += TG3_BDINFO_SIZE;
8261 }
Matt Carlson2d31eca2009-09-01 12:53:31 +00008262
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008263 if (tnapi->rx_rcb) {
8264 tg3_set_bdinfo(tp, rxrcb, tnapi->rx_rcb_mapping,
Matt Carlson7cb32cf2010-09-30 10:34:36 +00008265 (tp->rx_ret_ring_mask + 1) <<
8266 BDINFO_FLAGS_MAXLEN_SHIFT, 0);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008267 rxrcb += TG3_BDINFO_SIZE;
8268 }
8269
8270 stblk = HOSTCC_STATBLCK_RING1;
8271
8272 for (i = 1, tnapi++; i < tp->irq_cnt; i++, tnapi++) {
8273 u64 mapping = (u64)tnapi->status_mapping;
8274 tw32(stblk + TG3_64BIT_REG_HIGH, mapping >> 32);
8275 tw32(stblk + TG3_64BIT_REG_LOW, mapping & 0xffffffff);
8276
8277 /* Clear status block in ram. */
8278 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
8279
Matt Carlson19cfaec2009-12-03 08:36:20 +00008280 if (tnapi->tx_ring) {
8281 tg3_set_bdinfo(tp, txrcb, tnapi->tx_desc_mapping,
8282 (TG3_TX_RING_SIZE <<
8283 BDINFO_FLAGS_MAXLEN_SHIFT),
8284 NIC_SRAM_TX_BUFFER_DESC);
8285 txrcb += TG3_BDINFO_SIZE;
8286 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008287
8288 tg3_set_bdinfo(tp, rxrcb, tnapi->rx_rcb_mapping,
Matt Carlson7cb32cf2010-09-30 10:34:36 +00008289 ((tp->rx_ret_ring_mask + 1) <<
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008290 BDINFO_FLAGS_MAXLEN_SHIFT), 0);
8291
8292 stblk += 8;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008293 rxrcb += TG3_BDINFO_SIZE;
8294 }
Matt Carlson2d31eca2009-09-01 12:53:31 +00008295}
8296
Matt Carlsoneb07a942011-04-20 07:57:36 +00008297static void tg3_setup_rxbd_thresholds(struct tg3 *tp)
8298{
8299 u32 val, bdcache_maxcnt, host_rep_thresh, nic_rep_thresh;
8300
Joe Perches63c3a662011-04-26 08:12:10 +00008301 if (!tg3_flag(tp, 5750_PLUS) ||
8302 tg3_flag(tp, 5780_CLASS) ||
Matt Carlsoneb07a942011-04-20 07:57:36 +00008303 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 ||
Matt Carlson513aa6e2011-11-21 15:01:18 +00008304 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752 ||
8305 tg3_flag(tp, 57765_PLUS))
Matt Carlsoneb07a942011-04-20 07:57:36 +00008306 bdcache_maxcnt = TG3_SRAM_RX_STD_BDCACHE_SIZE_5700;
8307 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
8308 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787)
8309 bdcache_maxcnt = TG3_SRAM_RX_STD_BDCACHE_SIZE_5755;
8310 else
8311 bdcache_maxcnt = TG3_SRAM_RX_STD_BDCACHE_SIZE_5906;
8312
8313 nic_rep_thresh = min(bdcache_maxcnt / 2, tp->rx_std_max_post);
8314 host_rep_thresh = max_t(u32, tp->rx_pending / 8, 1);
8315
8316 val = min(nic_rep_thresh, host_rep_thresh);
8317 tw32(RCVBDI_STD_THRESH, val);
8318
Joe Perches63c3a662011-04-26 08:12:10 +00008319 if (tg3_flag(tp, 57765_PLUS))
Matt Carlsoneb07a942011-04-20 07:57:36 +00008320 tw32(STD_REPLENISH_LWM, bdcache_maxcnt);
8321
Joe Perches63c3a662011-04-26 08:12:10 +00008322 if (!tg3_flag(tp, JUMBO_CAPABLE) || tg3_flag(tp, 5780_CLASS))
Matt Carlsoneb07a942011-04-20 07:57:36 +00008323 return;
8324
Matt Carlson513aa6e2011-11-21 15:01:18 +00008325 bdcache_maxcnt = TG3_SRAM_RX_JMB_BDCACHE_SIZE_5700;
Matt Carlsoneb07a942011-04-20 07:57:36 +00008326
8327 host_rep_thresh = max_t(u32, tp->rx_jumbo_pending / 8, 1);
8328
8329 val = min(bdcache_maxcnt / 2, host_rep_thresh);
8330 tw32(RCVBDI_JUMBO_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(JMB_REPLENISH_LWM, bdcache_maxcnt);
8334}
8335
Matt Carlsonccd5ba92012-02-13 10:20:08 +00008336static inline u32 calc_crc(unsigned char *buf, int len)
8337{
8338 u32 reg;
8339 u32 tmp;
8340 int j, k;
8341
8342 reg = 0xffffffff;
8343
8344 for (j = 0; j < len; j++) {
8345 reg ^= buf[j];
8346
8347 for (k = 0; k < 8; k++) {
8348 tmp = reg & 0x01;
8349
8350 reg >>= 1;
8351
8352 if (tmp)
8353 reg ^= 0xedb88320;
8354 }
8355 }
8356
8357 return ~reg;
8358}
8359
8360static void tg3_set_multi(struct tg3 *tp, unsigned int accept_all)
8361{
8362 /* accept or reject all multicast frames */
8363 tw32(MAC_HASH_REG_0, accept_all ? 0xffffffff : 0);
8364 tw32(MAC_HASH_REG_1, accept_all ? 0xffffffff : 0);
8365 tw32(MAC_HASH_REG_2, accept_all ? 0xffffffff : 0);
8366 tw32(MAC_HASH_REG_3, accept_all ? 0xffffffff : 0);
8367}
8368
8369static void __tg3_set_rx_mode(struct net_device *dev)
8370{
8371 struct tg3 *tp = netdev_priv(dev);
8372 u32 rx_mode;
8373
8374 rx_mode = tp->rx_mode & ~(RX_MODE_PROMISC |
8375 RX_MODE_KEEP_VLAN_TAG);
8376
8377#if !defined(CONFIG_VLAN_8021Q) && !defined(CONFIG_VLAN_8021Q_MODULE)
8378 /* When ASF is in use, we always keep the RX_MODE_KEEP_VLAN_TAG
8379 * flag clear.
8380 */
8381 if (!tg3_flag(tp, ENABLE_ASF))
8382 rx_mode |= RX_MODE_KEEP_VLAN_TAG;
8383#endif
8384
8385 if (dev->flags & IFF_PROMISC) {
8386 /* Promiscuous mode. */
8387 rx_mode |= RX_MODE_PROMISC;
8388 } else if (dev->flags & IFF_ALLMULTI) {
8389 /* Accept all multicast. */
8390 tg3_set_multi(tp, 1);
8391 } else if (netdev_mc_empty(dev)) {
8392 /* Reject all multicast. */
8393 tg3_set_multi(tp, 0);
8394 } else {
8395 /* Accept one or more multicast(s). */
8396 struct netdev_hw_addr *ha;
8397 u32 mc_filter[4] = { 0, };
8398 u32 regidx;
8399 u32 bit;
8400 u32 crc;
8401
8402 netdev_for_each_mc_addr(ha, dev) {
8403 crc = calc_crc(ha->addr, ETH_ALEN);
8404 bit = ~crc & 0x7f;
8405 regidx = (bit & 0x60) >> 5;
8406 bit &= 0x1f;
8407 mc_filter[regidx] |= (1 << bit);
8408 }
8409
8410 tw32(MAC_HASH_REG_0, mc_filter[0]);
8411 tw32(MAC_HASH_REG_1, mc_filter[1]);
8412 tw32(MAC_HASH_REG_2, mc_filter[2]);
8413 tw32(MAC_HASH_REG_3, mc_filter[3]);
8414 }
8415
8416 if (rx_mode != tp->rx_mode) {
8417 tp->rx_mode = rx_mode;
8418 tw32_f(MAC_RX_MODE, rx_mode);
8419 udelay(10);
8420 }
8421}
8422
Matt Carlson90415472011-12-16 13:33:23 +00008423static void tg3_rss_init_dflt_indir_tbl(struct tg3 *tp)
8424{
8425 int i;
8426
8427 for (i = 0; i < TG3_RSS_INDIR_TBL_SIZE; i++)
8428 tp->rss_ind_tbl[i] =
8429 ethtool_rxfh_indir_default(i, tp->irq_cnt - 1);
8430}
8431
8432static void tg3_rss_check_indir_tbl(struct tg3 *tp)
Matt Carlsonbcebcc42011-12-14 11:10:01 +00008433{
8434 int i;
8435
8436 if (!tg3_flag(tp, SUPPORT_MSIX))
8437 return;
8438
Matt Carlson90415472011-12-16 13:33:23 +00008439 if (tp->irq_cnt <= 2) {
Matt Carlsonbcebcc42011-12-14 11:10:01 +00008440 memset(&tp->rss_ind_tbl[0], 0, sizeof(tp->rss_ind_tbl));
Matt Carlson90415472011-12-16 13:33:23 +00008441 return;
8442 }
8443
8444 /* Validate table against current IRQ count */
8445 for (i = 0; i < TG3_RSS_INDIR_TBL_SIZE; i++) {
8446 if (tp->rss_ind_tbl[i] >= tp->irq_cnt - 1)
8447 break;
8448 }
8449
8450 if (i != TG3_RSS_INDIR_TBL_SIZE)
8451 tg3_rss_init_dflt_indir_tbl(tp);
Matt Carlsonbcebcc42011-12-14 11:10:01 +00008452}
8453
Matt Carlson90415472011-12-16 13:33:23 +00008454static void tg3_rss_write_indir_tbl(struct tg3 *tp)
Matt Carlsonbcebcc42011-12-14 11:10:01 +00008455{
8456 int i = 0;
8457 u32 reg = MAC_RSS_INDIR_TBL_0;
8458
8459 while (i < TG3_RSS_INDIR_TBL_SIZE) {
8460 u32 val = tp->rss_ind_tbl[i];
8461 i++;
8462 for (; i % 8; i++) {
8463 val <<= 4;
8464 val |= tp->rss_ind_tbl[i];
8465 }
8466 tw32(reg, val);
8467 reg += 4;
8468 }
8469}
8470
Matt Carlson2d31eca2009-09-01 12:53:31 +00008471/* tp->lock is held. */
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07008472static int tg3_reset_hw(struct tg3 *tp, int reset_phy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008473{
8474 u32 val, rdmac_mode;
8475 int i, err, limit;
Matt Carlson8fea32b2010-09-15 08:59:58 +00008476 struct tg3_rx_prodring_set *tpr = &tp->napi[0].prodring;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008477
8478 tg3_disable_ints(tp);
8479
8480 tg3_stop_fw(tp);
8481
8482 tg3_write_sig_pre_reset(tp, RESET_KIND_INIT);
8483
Joe Perches63c3a662011-04-26 08:12:10 +00008484 if (tg3_flag(tp, INIT_COMPLETE))
Michael Chane6de8ad2005-05-05 14:42:41 -07008485 tg3_abort_hw(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008486
Matt Carlson699c0192010-12-06 08:28:51 +00008487 /* Enable MAC control of LPI */
8488 if (tp->phy_flags & TG3_PHYFLG_EEE_CAP) {
8489 tw32_f(TG3_CPMU_EEE_LNKIDL_CTRL,
8490 TG3_CPMU_EEE_LNKIDL_PCIE_NL0 |
8491 TG3_CPMU_EEE_LNKIDL_UART_IDL);
8492
8493 tw32_f(TG3_CPMU_EEE_CTRL,
8494 TG3_CPMU_EEE_CTRL_EXIT_20_1_US);
8495
Matt Carlsona386b902010-12-06 08:28:53 +00008496 val = TG3_CPMU_EEEMD_ERLY_L1_XIT_DET |
8497 TG3_CPMU_EEEMD_LPI_IN_TX |
8498 TG3_CPMU_EEEMD_LPI_IN_RX |
8499 TG3_CPMU_EEEMD_EEE_ENABLE;
8500
8501 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717)
8502 val |= TG3_CPMU_EEEMD_SND_IDX_DET_EN;
8503
Joe Perches63c3a662011-04-26 08:12:10 +00008504 if (tg3_flag(tp, ENABLE_APE))
Matt Carlsona386b902010-12-06 08:28:53 +00008505 val |= TG3_CPMU_EEEMD_APE_TX_DET_EN;
8506
8507 tw32_f(TG3_CPMU_EEE_MODE, val);
8508
8509 tw32_f(TG3_CPMU_EEE_DBTMR1,
8510 TG3_CPMU_DBTMR1_PCIEXIT_2047US |
8511 TG3_CPMU_DBTMR1_LNKIDLE_2047US);
8512
8513 tw32_f(TG3_CPMU_EEE_DBTMR2,
Matt Carlsond7f2ab22011-01-25 15:58:56 +00008514 TG3_CPMU_DBTMR2_APE_TX_2047US |
Matt Carlsona386b902010-12-06 08:28:53 +00008515 TG3_CPMU_DBTMR2_TXIDXEQ_2047US);
Matt Carlson699c0192010-12-06 08:28:51 +00008516 }
8517
Matt Carlson603f1172010-02-12 14:47:10 +00008518 if (reset_phy)
Michael Chand4d2c552006-03-20 17:47:20 -08008519 tg3_phy_reset(tp);
8520
Linus Torvalds1da177e2005-04-16 15:20:36 -07008521 err = tg3_chip_reset(tp);
8522 if (err)
8523 return err;
8524
8525 tg3_write_sig_legacy(tp, RESET_KIND_INIT);
8526
Matt Carlsonbcb37f62008-11-03 16:52:09 -08008527 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX) {
Matt Carlsond30cdd22007-10-07 23:28:35 -07008528 val = tr32(TG3_CPMU_CTRL);
8529 val &= ~(CPMU_CTRL_LINK_AWARE_MODE | CPMU_CTRL_LINK_IDLE_MODE);
8530 tw32(TG3_CPMU_CTRL, val);
Matt Carlson9acb9612007-11-12 21:10:06 -08008531
8532 val = tr32(TG3_CPMU_LSPD_10MB_CLK);
8533 val &= ~CPMU_LSPD_10MB_MACCLK_MASK;
8534 val |= CPMU_LSPD_10MB_MACCLK_6_25;
8535 tw32(TG3_CPMU_LSPD_10MB_CLK, val);
8536
8537 val = tr32(TG3_CPMU_LNK_AWARE_PWRMD);
8538 val &= ~CPMU_LNK_AWARE_MACCLK_MASK;
8539 val |= CPMU_LNK_AWARE_MACCLK_6_25;
8540 tw32(TG3_CPMU_LNK_AWARE_PWRMD, val);
8541
8542 val = tr32(TG3_CPMU_HST_ACC);
8543 val &= ~CPMU_HST_ACC_MACCLK_MASK;
8544 val |= CPMU_HST_ACC_MACCLK_6_25;
8545 tw32(TG3_CPMU_HST_ACC, val);
Matt Carlsond30cdd22007-10-07 23:28:35 -07008546 }
8547
Matt Carlson33466d92009-04-20 06:57:41 +00008548 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780) {
8549 val = tr32(PCIE_PWR_MGMT_THRESH) & ~PCIE_PWR_MGMT_L1_THRESH_MSK;
8550 val |= PCIE_PWR_MGMT_EXT_ASPM_TMR_EN |
8551 PCIE_PWR_MGMT_L1_THRESH_4MS;
8552 tw32(PCIE_PWR_MGMT_THRESH, val);
Matt Carlson521e6b92009-08-25 10:06:01 +00008553
8554 val = tr32(TG3_PCIE_EIDLE_DELAY) & ~TG3_PCIE_EIDLE_DELAY_MASK;
8555 tw32(TG3_PCIE_EIDLE_DELAY, val | TG3_PCIE_EIDLE_DELAY_13_CLKS);
8556
8557 tw32(TG3_CORR_ERR_STAT, TG3_CORR_ERR_STAT_CLEAR);
Matt Carlson33466d92009-04-20 06:57:41 +00008558
Matt Carlsonf40386c2009-11-02 14:24:02 +00008559 val = tr32(TG3_PCIE_LNKCTL) & ~TG3_PCIE_LNKCTL_L1_PLL_PD_EN;
8560 tw32(TG3_PCIE_LNKCTL, val | TG3_PCIE_LNKCTL_L1_PLL_PD_DIS);
Matt Carlson255ca312009-08-25 10:07:27 +00008561 }
8562
Joe Perches63c3a662011-04-26 08:12:10 +00008563 if (tg3_flag(tp, L1PLLPD_EN)) {
Matt Carlson614b0592010-01-20 16:58:02 +00008564 u32 grc_mode = tr32(GRC_MODE);
8565
8566 /* Access the lower 1K of PL PCIE block registers. */
8567 val = grc_mode & ~GRC_MODE_PCIE_PORT_MASK;
8568 tw32(GRC_MODE, val | GRC_MODE_PCIE_PL_SEL);
8569
8570 val = tr32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL1);
8571 tw32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL1,
8572 val | TG3_PCIE_PL_LO_PHYCTL1_L1PLLPD_EN);
8573
8574 tw32(GRC_MODE, grc_mode);
8575 }
8576
Matt Carlson55086ad2011-12-14 11:09:59 +00008577 if (tg3_flag(tp, 57765_CLASS)) {
Matt Carlson5093eed2010-11-24 08:31:45 +00008578 if (tp->pci_chip_rev_id == CHIPREV_ID_57765_A0) {
8579 u32 grc_mode = tr32(GRC_MODE);
Matt Carlsoncea46462010-04-12 06:58:24 +00008580
Matt Carlson5093eed2010-11-24 08:31:45 +00008581 /* Access the lower 1K of PL PCIE block registers. */
8582 val = grc_mode & ~GRC_MODE_PCIE_PORT_MASK;
8583 tw32(GRC_MODE, val | GRC_MODE_PCIE_PL_SEL);
Matt Carlsoncea46462010-04-12 06:58:24 +00008584
Matt Carlson5093eed2010-11-24 08:31:45 +00008585 val = tr32(TG3_PCIE_TLDLPL_PORT +
8586 TG3_PCIE_PL_LO_PHYCTL5);
8587 tw32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL5,
8588 val | TG3_PCIE_PL_LO_PHYCTL5_DIS_L2CLKREQ);
Matt Carlsoncea46462010-04-12 06:58:24 +00008589
Matt Carlson5093eed2010-11-24 08:31:45 +00008590 tw32(GRC_MODE, grc_mode);
8591 }
Matt Carlsona977dbe2010-04-12 06:58:26 +00008592
Matt Carlson1ff30a52011-05-19 12:12:46 +00008593 if (GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_57765_AX) {
8594 u32 grc_mode = tr32(GRC_MODE);
8595
8596 /* Access the lower 1K of DL PCIE block registers. */
8597 val = grc_mode & ~GRC_MODE_PCIE_PORT_MASK;
8598 tw32(GRC_MODE, val | GRC_MODE_PCIE_DL_SEL);
8599
8600 val = tr32(TG3_PCIE_TLDLPL_PORT +
8601 TG3_PCIE_DL_LO_FTSMAX);
8602 val &= ~TG3_PCIE_DL_LO_FTSMAX_MSK;
8603 tw32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_DL_LO_FTSMAX,
8604 val | TG3_PCIE_DL_LO_FTSMAX_VAL);
8605
8606 tw32(GRC_MODE, grc_mode);
8607 }
8608
Matt Carlsona977dbe2010-04-12 06:58:26 +00008609 val = tr32(TG3_CPMU_LSPD_10MB_CLK);
8610 val &= ~CPMU_LSPD_10MB_MACCLK_MASK;
8611 val |= CPMU_LSPD_10MB_MACCLK_6_25;
8612 tw32(TG3_CPMU_LSPD_10MB_CLK, val);
Matt Carlsoncea46462010-04-12 06:58:24 +00008613 }
8614
Linus Torvalds1da177e2005-04-16 15:20:36 -07008615 /* This works around an issue with Athlon chipsets on
8616 * B3 tigon3 silicon. This bit has no effect on any
8617 * other revision. But do not set this on PCI Express
Matt Carlson795d01c2007-10-07 23:28:17 -07008618 * chips and don't even touch the clocks if the CPMU is present.
Linus Torvalds1da177e2005-04-16 15:20:36 -07008619 */
Joe Perches63c3a662011-04-26 08:12:10 +00008620 if (!tg3_flag(tp, CPMU_PRESENT)) {
8621 if (!tg3_flag(tp, PCI_EXPRESS))
Matt Carlson795d01c2007-10-07 23:28:17 -07008622 tp->pci_clock_ctrl |= CLOCK_CTRL_DELAY_PCI_GRANT;
8623 tw32_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl);
8624 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008625
8626 if (tp->pci_chip_rev_id == CHIPREV_ID_5704_A0 &&
Joe Perches63c3a662011-04-26 08:12:10 +00008627 tg3_flag(tp, PCIX_MODE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008628 val = tr32(TG3PCI_PCISTATE);
8629 val |= PCISTATE_RETRY_SAME_DMA;
8630 tw32(TG3PCI_PCISTATE, val);
8631 }
8632
Joe Perches63c3a662011-04-26 08:12:10 +00008633 if (tg3_flag(tp, ENABLE_APE)) {
Matt Carlson0d3031d2007-10-10 18:02:43 -07008634 /* Allow reads and writes to the
8635 * APE register and memory space.
8636 */
8637 val = tr32(TG3PCI_PCISTATE);
8638 val |= PCISTATE_ALLOW_APE_CTLSPC_WR |
Matt Carlsonf92d9dc12010-06-05 17:24:30 +00008639 PCISTATE_ALLOW_APE_SHMEM_WR |
8640 PCISTATE_ALLOW_APE_PSPACE_WR;
Matt Carlson0d3031d2007-10-10 18:02:43 -07008641 tw32(TG3PCI_PCISTATE, val);
8642 }
8643
Linus Torvalds1da177e2005-04-16 15:20:36 -07008644 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5704_BX) {
8645 /* Enable some hw fixes. */
8646 val = tr32(TG3PCI_MSI_DATA);
8647 val |= (1 << 26) | (1 << 28) | (1 << 29);
8648 tw32(TG3PCI_MSI_DATA, val);
8649 }
8650
8651 /* Descriptor ring init may make accesses to the
8652 * NIC SRAM area to setup the TX descriptors, so we
8653 * can only do this after the hardware has been
8654 * successfully reset.
8655 */
Michael Chan32d8c572006-07-25 16:38:29 -07008656 err = tg3_init_rings(tp);
8657 if (err)
8658 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008659
Joe Perches63c3a662011-04-26 08:12:10 +00008660 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlsoncbf9ca62009-11-13 13:03:40 +00008661 val = tr32(TG3PCI_DMA_RW_CTRL) &
8662 ~DMA_RWCTRL_DIS_CACHE_ALIGNMENT;
Matt Carlson1a319022010-04-12 06:58:25 +00008663 if (tp->pci_chip_rev_id == CHIPREV_ID_57765_A0)
8664 val &= ~DMA_RWCTRL_CRDRDR_RDMA_MRRS_MSK;
Matt Carlson55086ad2011-12-14 11:09:59 +00008665 if (!tg3_flag(tp, 57765_CLASS) &&
Matt Carlson0aebff42011-04-25 12:42:45 +00008666 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717)
8667 val |= DMA_RWCTRL_TAGGED_STAT_WA;
Matt Carlsoncbf9ca62009-11-13 13:03:40 +00008668 tw32(TG3PCI_DMA_RW_CTRL, val | tp->dma_rwctrl);
8669 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5784 &&
8670 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5761) {
Matt Carlsond30cdd22007-10-07 23:28:35 -07008671 /* This value is determined during the probe time DMA
8672 * engine test, tg3_test_dma.
8673 */
8674 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
8675 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008676
8677 tp->grc_mode &= ~(GRC_MODE_HOST_SENDBDS |
8678 GRC_MODE_4X_NIC_SEND_RINGS |
8679 GRC_MODE_NO_TX_PHDR_CSUM |
8680 GRC_MODE_NO_RX_PHDR_CSUM);
8681 tp->grc_mode |= GRC_MODE_HOST_SENDBDS;
Michael Chand2d746f2006-04-06 21:45:39 -07008682
8683 /* Pseudo-header checksum is done by hardware logic and not
8684 * the offload processers, so make the chip do the pseudo-
8685 * header checksums on receive. For transmit it is more
8686 * convenient to do the pseudo-header checksum in software
8687 * as Linux does that on transmit for us in all cases.
8688 */
8689 tp->grc_mode |= GRC_MODE_NO_TX_PHDR_CSUM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008690
8691 tw32(GRC_MODE,
8692 tp->grc_mode |
8693 (GRC_MODE_IRQ_ON_MAC_ATTN | GRC_MODE_HOST_STACKUP));
8694
8695 /* Setup the timer prescalar register. Clock is always 66Mhz. */
8696 val = tr32(GRC_MISC_CFG);
8697 val &= ~0xff;
8698 val |= (65 << GRC_MISC_CFG_PRESCALAR_SHIFT);
8699 tw32(GRC_MISC_CFG, val);
8700
8701 /* Initialize MBUF/DESC pool. */
Joe Perches63c3a662011-04-26 08:12:10 +00008702 if (tg3_flag(tp, 5750_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008703 /* Do nothing. */
8704 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5705) {
8705 tw32(BUFMGR_MB_POOL_ADDR, NIC_SRAM_MBUF_POOL_BASE);
8706 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704)
8707 tw32(BUFMGR_MB_POOL_SIZE, NIC_SRAM_MBUF_POOL_SIZE64);
8708 else
8709 tw32(BUFMGR_MB_POOL_SIZE, NIC_SRAM_MBUF_POOL_SIZE96);
8710 tw32(BUFMGR_DMA_DESC_POOL_ADDR, NIC_SRAM_DMA_DESC_POOL_BASE);
8711 tw32(BUFMGR_DMA_DESC_POOL_SIZE, NIC_SRAM_DMA_DESC_POOL_SIZE);
Joe Perches63c3a662011-04-26 08:12:10 +00008712 } else if (tg3_flag(tp, TSO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008713 int fw_len;
8714
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08008715 fw_len = tp->fw_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008716 fw_len = (fw_len + (0x80 - 1)) & ~(0x80 - 1);
8717 tw32(BUFMGR_MB_POOL_ADDR,
8718 NIC_SRAM_MBUF_POOL_BASE5705 + fw_len);
8719 tw32(BUFMGR_MB_POOL_SIZE,
8720 NIC_SRAM_MBUF_POOL_SIZE5705 - fw_len - 0xa00);
8721 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008722
Michael Chan0f893dc2005-07-25 12:30:38 -07008723 if (tp->dev->mtu <= ETH_DATA_LEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008724 tw32(BUFMGR_MB_RDMA_LOW_WATER,
8725 tp->bufmgr_config.mbuf_read_dma_low_water);
8726 tw32(BUFMGR_MB_MACRX_LOW_WATER,
8727 tp->bufmgr_config.mbuf_mac_rx_low_water);
8728 tw32(BUFMGR_MB_HIGH_WATER,
8729 tp->bufmgr_config.mbuf_high_water);
8730 } else {
8731 tw32(BUFMGR_MB_RDMA_LOW_WATER,
8732 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo);
8733 tw32(BUFMGR_MB_MACRX_LOW_WATER,
8734 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo);
8735 tw32(BUFMGR_MB_HIGH_WATER,
8736 tp->bufmgr_config.mbuf_high_water_jumbo);
8737 }
8738 tw32(BUFMGR_DMA_LOW_WATER,
8739 tp->bufmgr_config.dma_low_water);
8740 tw32(BUFMGR_DMA_HIGH_WATER,
8741 tp->bufmgr_config.dma_high_water);
8742
Matt Carlsond309a462010-09-30 10:34:31 +00008743 val = BUFMGR_MODE_ENABLE | BUFMGR_MODE_ATTN_ENABLE;
8744 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
8745 val |= BUFMGR_MODE_NO_TX_UNDERRUN;
Matt Carlson4d958472011-04-20 07:57:35 +00008746 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
8747 tp->pci_chip_rev_id == CHIPREV_ID_5719_A0 ||
8748 tp->pci_chip_rev_id == CHIPREV_ID_5720_A0)
8749 val |= BUFMGR_MODE_MBLOW_ATTN_ENAB;
Matt Carlsond309a462010-09-30 10:34:31 +00008750 tw32(BUFMGR_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008751 for (i = 0; i < 2000; i++) {
8752 if (tr32(BUFMGR_MODE) & BUFMGR_MODE_ENABLE)
8753 break;
8754 udelay(10);
8755 }
8756 if (i >= 2000) {
Joe Perches05dbe002010-02-17 19:44:19 +00008757 netdev_err(tp->dev, "%s cannot enable BUFMGR\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008758 return -ENODEV;
8759 }
8760
Matt Carlsoneb07a942011-04-20 07:57:36 +00008761 if (tp->pci_chip_rev_id == CHIPREV_ID_5906_A1)
8762 tw32(ISO_PKT_TX, (tr32(ISO_PKT_TX) & ~0x3) | 0x2);
Michael Chanb5d37722006-09-27 16:06:21 -07008763
Matt Carlsoneb07a942011-04-20 07:57:36 +00008764 tg3_setup_rxbd_thresholds(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008765
8766 /* Initialize TG3_BDINFO's at:
8767 * RCVDBDI_STD_BD: standard eth size rx ring
8768 * RCVDBDI_JUMBO_BD: jumbo frame rx ring
8769 * RCVDBDI_MINI_BD: small frame rx ring (??? does not work)
8770 *
8771 * like so:
8772 * TG3_BDINFO_HOST_ADDR: high/low parts of DMA address of ring
8773 * TG3_BDINFO_MAXLEN_FLAGS: (rx max buffer size << 16) |
8774 * ring attribute flags
8775 * TG3_BDINFO_NIC_ADDR: location of descriptors in nic SRAM
8776 *
8777 * Standard receive ring @ NIC_SRAM_RX_BUFFER_DESC, 512 entries.
8778 * Jumbo receive ring @ NIC_SRAM_RX_JUMBO_BUFFER_DESC, 256 entries.
8779 *
8780 * The size of each ring is fixed in the firmware, but the location is
8781 * configurable.
8782 */
8783 tw32(RCVDBDI_STD_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH,
Matt Carlson21f581a2009-08-28 14:00:25 +00008784 ((u64) tpr->rx_std_mapping >> 32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07008785 tw32(RCVDBDI_STD_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW,
Matt Carlson21f581a2009-08-28 14:00:25 +00008786 ((u64) tpr->rx_std_mapping & 0xffffffff));
Joe Perches63c3a662011-04-26 08:12:10 +00008787 if (!tg3_flag(tp, 5717_PLUS))
Matt Carlson87668d32009-11-13 13:03:34 +00008788 tw32(RCVDBDI_STD_BD + TG3_BDINFO_NIC_ADDR,
8789 NIC_SRAM_RX_BUFFER_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008790
Matt Carlsonfdb72b32009-08-28 13:57:12 +00008791 /* Disable the mini ring */
Joe Perches63c3a662011-04-26 08:12:10 +00008792 if (!tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008793 tw32(RCVDBDI_MINI_BD + TG3_BDINFO_MAXLEN_FLAGS,
8794 BDINFO_FLAGS_DISABLED);
8795
Matt Carlsonfdb72b32009-08-28 13:57:12 +00008796 /* Program the jumbo buffer descriptor ring control
8797 * blocks on those devices that have them.
8798 */
Matt Carlsona0512942011-07-27 14:20:54 +00008799 if (tp->pci_chip_rev_id == CHIPREV_ID_5719_A0 ||
Joe Perches63c3a662011-04-26 08:12:10 +00008800 (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008801
Joe Perches63c3a662011-04-26 08:12:10 +00008802 if (tg3_flag(tp, JUMBO_RING_ENABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008803 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH,
Matt Carlson21f581a2009-08-28 14:00:25 +00008804 ((u64) tpr->rx_jmb_mapping >> 32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07008805 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW,
Matt Carlson21f581a2009-08-28 14:00:25 +00008806 ((u64) tpr->rx_jmb_mapping & 0xffffffff));
Matt Carlsonde9f5232011-04-05 14:22:43 +00008807 val = TG3_RX_JMB_RING_SIZE(tp) <<
8808 BDINFO_FLAGS_MAXLEN_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008809 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_MAXLEN_FLAGS,
Matt Carlsonde9f5232011-04-05 14:22:43 +00008810 val | BDINFO_FLAGS_USE_EXT_RECV);
Joe Perches63c3a662011-04-26 08:12:10 +00008811 if (!tg3_flag(tp, USE_JUMBO_BDFLAG) ||
Matt Carlson55086ad2011-12-14 11:09:59 +00008812 tg3_flag(tp, 57765_CLASS))
Matt Carlson87668d32009-11-13 13:03:34 +00008813 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_NIC_ADDR,
8814 NIC_SRAM_RX_JUMBO_BUFFER_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008815 } else {
8816 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_MAXLEN_FLAGS,
8817 BDINFO_FLAGS_DISABLED);
8818 }
8819
Joe Perches63c3a662011-04-26 08:12:10 +00008820 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlsonfa6b2aa2011-11-21 15:01:19 +00008821 val = TG3_RX_STD_RING_SIZE(tp);
Matt Carlson7cb32cf2010-09-30 10:34:36 +00008822 val <<= BDINFO_FLAGS_MAXLEN_SHIFT;
8823 val |= (TG3_RX_STD_DMA_SZ << 2);
8824 } else
Matt Carlson04380d42010-04-12 06:58:29 +00008825 val = TG3_RX_STD_DMA_SZ << BDINFO_FLAGS_MAXLEN_SHIFT;
Matt Carlsonfdb72b32009-08-28 13:57:12 +00008826 } else
Matt Carlsonde9f5232011-04-05 14:22:43 +00008827 val = TG3_RX_STD_MAX_SIZE_5700 << BDINFO_FLAGS_MAXLEN_SHIFT;
Matt Carlsonfdb72b32009-08-28 13:57:12 +00008828
8829 tw32(RCVDBDI_STD_BD + TG3_BDINFO_MAXLEN_FLAGS, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008830
Matt Carlson411da642009-11-13 13:03:46 +00008831 tpr->rx_std_prod_idx = tp->rx_pending;
Matt Carlson66711e62009-11-13 13:03:49 +00008832 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG, tpr->rx_std_prod_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008833
Joe Perches63c3a662011-04-26 08:12:10 +00008834 tpr->rx_jmb_prod_idx =
8835 tg3_flag(tp, JUMBO_RING_ENABLE) ? tp->rx_jumbo_pending : 0;
Matt Carlson66711e62009-11-13 13:03:49 +00008836 tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG, tpr->rx_jmb_prod_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008837
Matt Carlson2d31eca2009-09-01 12:53:31 +00008838 tg3_rings_reset(tp);
8839
Linus Torvalds1da177e2005-04-16 15:20:36 -07008840 /* Initialize MAC address and backoff seed. */
Michael Chan986e0ae2007-05-05 12:10:20 -07008841 __tg3_set_mac_addr(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008842
8843 /* MTU + ethernet header + FCS + optional VLAN tag */
Matt Carlsonf7b493e2009-02-25 14:21:52 +00008844 tw32(MAC_RX_MTU_SIZE,
8845 tp->dev->mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008846
8847 /* The slot time is changed by tg3_setup_phy if we
8848 * run at gigabit with half duplex.
8849 */
Matt Carlsonf2096f92011-04-05 14:22:48 +00008850 val = (2 << TX_LENGTHS_IPG_CRS_SHIFT) |
8851 (6 << TX_LENGTHS_IPG_SHIFT) |
8852 (32 << TX_LENGTHS_SLOT_TIME_SHIFT);
8853
8854 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
8855 val |= tr32(MAC_TX_LENGTHS) &
8856 (TX_LENGTHS_JMB_FRM_LEN_MSK |
8857 TX_LENGTHS_CNT_DWN_VAL_MSK);
8858
8859 tw32(MAC_TX_LENGTHS, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008860
8861 /* Receive rules. */
8862 tw32(MAC_RCV_RULE_CFG, RCV_RULE_CFG_DEFAULT_CLASS);
8863 tw32(RCVLPC_CONFIG, 0x0181);
8864
8865 /* Calculate RDMAC_MODE setting early, we need it to determine
8866 * the RCVLPC_STATE_ENABLE mask.
8867 */
8868 rdmac_mode = (RDMAC_MODE_ENABLE | RDMAC_MODE_TGTABORT_ENAB |
8869 RDMAC_MODE_MSTABORT_ENAB | RDMAC_MODE_PARITYERR_ENAB |
8870 RDMAC_MODE_ADDROFLOW_ENAB | RDMAC_MODE_FIFOOFLOW_ENAB |
8871 RDMAC_MODE_FIFOURUN_ENAB | RDMAC_MODE_FIFOOREAD_ENAB |
8872 RDMAC_MODE_LNGREAD_ENAB);
Michael Chan85e94ce2005-04-21 17:05:28 -07008873
Matt Carlsondeabaac2010-11-24 08:31:50 +00008874 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717)
Matt Carlson0339e4e2010-02-12 14:47:09 +00008875 rdmac_mode |= RDMAC_MODE_MULT_DMA_RD_DIS;
8876
Matt Carlson57e69832008-05-25 23:48:31 -07008877 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson321d32a2008-11-21 17:22:19 -08008878 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
8879 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
Matt Carlsond30cdd22007-10-07 23:28:35 -07008880 rdmac_mode |= RDMAC_MODE_BD_SBD_CRPT_ENAB |
8881 RDMAC_MODE_MBUF_RBD_CRPT_ENAB |
8882 RDMAC_MODE_MBUF_SBD_CRPT_ENAB;
8883
Matt Carlsonc5908932011-03-09 16:58:25 +00008884 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
8885 tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) {
Joe Perches63c3a662011-04-26 08:12:10 +00008886 if (tg3_flag(tp, TSO_CAPABLE) &&
Matt Carlsonc13e3712007-05-05 11:50:04 -07008887 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008888 rdmac_mode |= RDMAC_MODE_FIFO_SIZE_128;
8889 } else if (!(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH) &&
Joe Perches63c3a662011-04-26 08:12:10 +00008890 !tg3_flag(tp, IS_5788)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008891 rdmac_mode |= RDMAC_MODE_FIFO_LONG_BURST;
8892 }
8893 }
8894
Joe Perches63c3a662011-04-26 08:12:10 +00008895 if (tg3_flag(tp, PCI_EXPRESS))
Michael Chan85e94ce2005-04-21 17:05:28 -07008896 rdmac_mode |= RDMAC_MODE_FIFO_LONG_BURST;
8897
Matt Carlson55086ad2011-12-14 11:09:59 +00008898 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57766)
8899 rdmac_mode |= RDMAC_MODE_JMB_2K_MMRR;
8900
Joe Perches63c3a662011-04-26 08:12:10 +00008901 if (tg3_flag(tp, HW_TSO_1) ||
8902 tg3_flag(tp, HW_TSO_2) ||
8903 tg3_flag(tp, HW_TSO_3))
Matt Carlson027455a2008-12-21 20:19:30 -08008904 rdmac_mode |= RDMAC_MODE_IPV4_LSO_EN;
8905
Matt Carlson108a6c12011-05-19 12:12:47 +00008906 if (tg3_flag(tp, 57765_PLUS) ||
Matt Carlsone849cdc2009-11-13 13:03:38 +00008907 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Matt Carlson027455a2008-12-21 20:19:30 -08008908 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
8909 rdmac_mode |= RDMAC_MODE_IPV6_LSO_EN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008910
Matt Carlsonf2096f92011-04-05 14:22:48 +00008911 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
8912 rdmac_mode |= tr32(RDMAC_MODE) & RDMAC_MODE_H2BNC_VLAN_DET;
8913
Matt Carlson41a8a7e2010-09-15 08:59:53 +00008914 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
8915 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
8916 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
8917 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
Joe Perches63c3a662011-04-26 08:12:10 +00008918 tg3_flag(tp, 57765_PLUS)) {
Matt Carlson41a8a7e2010-09-15 08:59:53 +00008919 val = tr32(TG3_RDMA_RSRVCTRL_REG);
Matt Carlsond78b59f2011-04-05 14:22:46 +00008920 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
8921 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
Matt Carlsonb4495ed2011-01-25 15:58:47 +00008922 val &= ~(TG3_RDMA_RSRVCTRL_TXMRGN_MASK |
8923 TG3_RDMA_RSRVCTRL_FIFO_LWM_MASK |
8924 TG3_RDMA_RSRVCTRL_FIFO_HWM_MASK);
8925 val |= TG3_RDMA_RSRVCTRL_TXMRGN_320B |
8926 TG3_RDMA_RSRVCTRL_FIFO_LWM_1_5K |
8927 TG3_RDMA_RSRVCTRL_FIFO_HWM_1_5K;
Matt Carlsonb75cc0e2010-11-24 08:31:46 +00008928 }
Matt Carlson41a8a7e2010-09-15 08:59:53 +00008929 tw32(TG3_RDMA_RSRVCTRL_REG,
8930 val | TG3_RDMA_RSRVCTRL_FIFO_OFLW_FIX);
8931 }
8932
Matt Carlsond78b59f2011-04-05 14:22:46 +00008933 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
8934 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
Matt Carlsond309a462010-09-30 10:34:31 +00008935 val = tr32(TG3_LSO_RD_DMA_CRPTEN_CTRL);
8936 tw32(TG3_LSO_RD_DMA_CRPTEN_CTRL, val |
8937 TG3_LSO_RD_DMA_CRPTEN_CTRL_BLEN_BD_4K |
8938 TG3_LSO_RD_DMA_CRPTEN_CTRL_BLEN_LSO_4K);
8939 }
8940
Linus Torvalds1da177e2005-04-16 15:20:36 -07008941 /* Receive/send statistics. */
Joe Perches63c3a662011-04-26 08:12:10 +00008942 if (tg3_flag(tp, 5750_PLUS)) {
Michael Chan16613942006-06-29 20:15:13 -07008943 val = tr32(RCVLPC_STATS_ENABLE);
8944 val &= ~RCVLPC_STATSENAB_DACK_FIX;
8945 tw32(RCVLPC_STATS_ENABLE, val);
8946 } else if ((rdmac_mode & RDMAC_MODE_FIFO_SIZE_128) &&
Joe Perches63c3a662011-04-26 08:12:10 +00008947 tg3_flag(tp, TSO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008948 val = tr32(RCVLPC_STATS_ENABLE);
8949 val &= ~RCVLPC_STATSENAB_LNGBRST_RFIX;
8950 tw32(RCVLPC_STATS_ENABLE, val);
8951 } else {
8952 tw32(RCVLPC_STATS_ENABLE, 0xffffff);
8953 }
8954 tw32(RCVLPC_STATSCTRL, RCVLPC_STATSCTRL_ENABLE);
8955 tw32(SNDDATAI_STATSENAB, 0xffffff);
8956 tw32(SNDDATAI_STATSCTRL,
8957 (SNDDATAI_SCTRL_ENABLE |
8958 SNDDATAI_SCTRL_FASTUPD));
8959
8960 /* Setup host coalescing engine. */
8961 tw32(HOSTCC_MODE, 0);
8962 for (i = 0; i < 2000; i++) {
8963 if (!(tr32(HOSTCC_MODE) & HOSTCC_MODE_ENABLE))
8964 break;
8965 udelay(10);
8966 }
8967
Michael Chand244c892005-07-05 14:42:33 -07008968 __tg3_set_coalesce(tp, &tp->coal);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008969
Joe Perches63c3a662011-04-26 08:12:10 +00008970 if (!tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008971 /* Status/statistics block address. See tg3_timer,
8972 * the tg3_periodic_fetch_stats call there, and
8973 * tg3_get_stats to see how this works for 5705/5750 chips.
8974 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008975 tw32(HOSTCC_STATS_BLK_HOST_ADDR + TG3_64BIT_REG_HIGH,
8976 ((u64) tp->stats_mapping >> 32));
8977 tw32(HOSTCC_STATS_BLK_HOST_ADDR + TG3_64BIT_REG_LOW,
8978 ((u64) tp->stats_mapping & 0xffffffff));
8979 tw32(HOSTCC_STATS_BLK_NIC_ADDR, NIC_SRAM_STATS_BLK);
Matt Carlson2d31eca2009-09-01 12:53:31 +00008980
Linus Torvalds1da177e2005-04-16 15:20:36 -07008981 tw32(HOSTCC_STATUS_BLK_NIC_ADDR, NIC_SRAM_STATUS_BLK);
Matt Carlson2d31eca2009-09-01 12:53:31 +00008982
8983 /* Clear statistics and status block memory areas */
8984 for (i = NIC_SRAM_STATS_BLK;
8985 i < NIC_SRAM_STATUS_BLK + TG3_HW_STATUS_SIZE;
8986 i += sizeof(u32)) {
8987 tg3_write_mem(tp, i, 0);
8988 udelay(40);
8989 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008990 }
8991
8992 tw32(HOSTCC_MODE, HOSTCC_MODE_ENABLE | tp->coalesce_mode);
8993
8994 tw32(RCVCC_MODE, RCVCC_MODE_ENABLE | RCVCC_MODE_ATTN_ENABLE);
8995 tw32(RCVLPC_MODE, RCVLPC_MODE_ENABLE);
Joe Perches63c3a662011-04-26 08:12:10 +00008996 if (!tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008997 tw32(RCVLSC_MODE, RCVLSC_MODE_ENABLE | RCVLSC_MODE_ATTN_ENABLE);
8998
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008999 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES) {
9000 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chanc94e3942005-09-27 12:12:42 -07009001 /* reset to prevent losing 1st rx packet intermittently */
9002 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
9003 udelay(10);
9004 }
9005
Matt Carlson3bda1252008-08-15 14:08:22 -07009006 tp->mac_mode |= MAC_MODE_TXSTAT_ENABLE | MAC_MODE_RXSTAT_ENABLE |
Matt Carlson9e975cc2011-07-20 10:20:50 +00009007 MAC_MODE_TDE_ENABLE | MAC_MODE_RDE_ENABLE |
9008 MAC_MODE_FHDE_ENABLE;
9009 if (tg3_flag(tp, ENABLE_APE))
9010 tp->mac_mode |= MAC_MODE_APE_TX_EN | MAC_MODE_APE_RX_EN;
Joe Perches63c3a662011-04-26 08:12:10 +00009011 if (!tg3_flag(tp, 5705_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009012 !(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07009013 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700)
9014 tp->mac_mode |= MAC_MODE_LINK_POLARITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009015 tw32_f(MAC_MODE, tp->mac_mode | MAC_MODE_RXSTAT_CLEAR | MAC_MODE_TXSTAT_CLEAR);
9016 udelay(40);
9017
Michael Chan314fba32005-04-21 17:07:04 -07009018 /* tp->grc_local_ctrl is partially set up during tg3_get_invariants().
Joe Perches63c3a662011-04-26 08:12:10 +00009019 * If TG3_FLAG_IS_NIC is zero, we should read the
Michael Chan314fba32005-04-21 17:07:04 -07009020 * register to preserve the GPIO settings for LOMs. The GPIOs,
9021 * whether used as inputs or outputs, are set by boot code after
9022 * reset.
9023 */
Joe Perches63c3a662011-04-26 08:12:10 +00009024 if (!tg3_flag(tp, IS_NIC)) {
Michael Chan314fba32005-04-21 17:07:04 -07009025 u32 gpio_mask;
9026
Michael Chan9d26e212006-12-07 00:21:14 -08009027 gpio_mask = GRC_LCLCTRL_GPIO_OE0 | GRC_LCLCTRL_GPIO_OE1 |
9028 GRC_LCLCTRL_GPIO_OE2 | GRC_LCLCTRL_GPIO_OUTPUT0 |
9029 GRC_LCLCTRL_GPIO_OUTPUT1 | GRC_LCLCTRL_GPIO_OUTPUT2;
Michael Chan3e7d83b2005-04-21 17:10:36 -07009030
9031 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752)
9032 gpio_mask |= GRC_LCLCTRL_GPIO_OE3 |
9033 GRC_LCLCTRL_GPIO_OUTPUT3;
9034
Michael Chanaf36e6b2006-03-23 01:28:06 -08009035 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755)
9036 gpio_mask |= GRC_LCLCTRL_GPIO_UART_SEL;
9037
Gary Zambranoaaf84462007-05-05 11:51:45 -07009038 tp->grc_local_ctrl &= ~gpio_mask;
Michael Chan314fba32005-04-21 17:07:04 -07009039 tp->grc_local_ctrl |= tr32(GRC_LOCAL_CTRL) & gpio_mask;
9040
9041 /* GPIO1 must be driven high for eeprom write protect */
Joe Perches63c3a662011-04-26 08:12:10 +00009042 if (tg3_flag(tp, EEPROM_WRITE_PROT))
Michael Chan9d26e212006-12-07 00:21:14 -08009043 tp->grc_local_ctrl |= (GRC_LCLCTRL_GPIO_OE1 |
9044 GRC_LCLCTRL_GPIO_OUTPUT1);
Michael Chan314fba32005-04-21 17:07:04 -07009045 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009046 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
9047 udelay(100);
9048
Matt Carlsonc3b50032012-01-17 15:27:23 +00009049 if (tg3_flag(tp, USING_MSIX)) {
Matt Carlsonbaf8a942009-09-01 13:13:00 +00009050 val = tr32(MSGINT_MODE);
Matt Carlsonc3b50032012-01-17 15:27:23 +00009051 val |= MSGINT_MODE_ENABLE;
9052 if (tp->irq_cnt > 1)
9053 val |= MSGINT_MODE_MULTIVEC_EN;
Matt Carlson5b39de92011-08-31 11:44:50 +00009054 if (!tg3_flag(tp, 1SHOT_MSI))
9055 val |= MSGINT_MODE_ONE_SHOT_DISABLE;
Matt Carlsonbaf8a942009-09-01 13:13:00 +00009056 tw32(MSGINT_MODE, val);
9057 }
9058
Joe Perches63c3a662011-04-26 08:12:10 +00009059 if (!tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009060 tw32_f(DMAC_MODE, DMAC_MODE_ENABLE);
9061 udelay(40);
9062 }
9063
9064 val = (WDMAC_MODE_ENABLE | WDMAC_MODE_TGTABORT_ENAB |
9065 WDMAC_MODE_MSTABORT_ENAB | WDMAC_MODE_PARITYERR_ENAB |
9066 WDMAC_MODE_ADDROFLOW_ENAB | WDMAC_MODE_FIFOOFLOW_ENAB |
9067 WDMAC_MODE_FIFOURUN_ENAB | WDMAC_MODE_FIFOOREAD_ENAB |
9068 WDMAC_MODE_LNGREAD_ENAB);
9069
Matt Carlsonc5908932011-03-09 16:58:25 +00009070 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
9071 tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) {
Joe Perches63c3a662011-04-26 08:12:10 +00009072 if (tg3_flag(tp, TSO_CAPABLE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07009073 (tp->pci_chip_rev_id == CHIPREV_ID_5705_A1 ||
9074 tp->pci_chip_rev_id == CHIPREV_ID_5705_A2)) {
9075 /* nothing */
9076 } else if (!(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH) &&
Joe Perches63c3a662011-04-26 08:12:10 +00009077 !tg3_flag(tp, IS_5788)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009078 val |= WDMAC_MODE_RX_ACCEL;
9079 }
9080 }
9081
Michael Chand9ab5ad2006-03-20 22:27:35 -08009082 /* Enable host coalescing bug fix */
Joe Perches63c3a662011-04-26 08:12:10 +00009083 if (tg3_flag(tp, 5755_PLUS))
Matt Carlsonf51f3562008-05-25 23:45:08 -07009084 val |= WDMAC_MODE_STATUS_TAG_FIX;
Michael Chand9ab5ad2006-03-20 22:27:35 -08009085
Matt Carlson788a0352009-11-02 14:26:03 +00009086 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
9087 val |= WDMAC_MODE_BURST_ALL_DATA;
9088
Linus Torvalds1da177e2005-04-16 15:20:36 -07009089 tw32_f(WDMAC_MODE, val);
9090 udelay(40);
9091
Joe Perches63c3a662011-04-26 08:12:10 +00009092 if (tg3_flag(tp, PCIX_MODE)) {
Matt Carlson9974a352007-10-07 23:27:28 -07009093 u16 pcix_cmd;
9094
9095 pci_read_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
9096 &pcix_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009097 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703) {
Matt Carlson9974a352007-10-07 23:27:28 -07009098 pcix_cmd &= ~PCI_X_CMD_MAX_READ;
9099 pcix_cmd |= PCI_X_CMD_READ_2K;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009100 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
Matt Carlson9974a352007-10-07 23:27:28 -07009101 pcix_cmd &= ~(PCI_X_CMD_MAX_SPLIT | PCI_X_CMD_MAX_READ);
9102 pcix_cmd |= PCI_X_CMD_READ_2K;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009103 }
Matt Carlson9974a352007-10-07 23:27:28 -07009104 pci_write_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
9105 pcix_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009106 }
9107
9108 tw32_f(RDMAC_MODE, rdmac_mode);
9109 udelay(40);
9110
9111 tw32(RCVDCC_MODE, RCVDCC_MODE_ENABLE | RCVDCC_MODE_ATTN_ENABLE);
Joe Perches63c3a662011-04-26 08:12:10 +00009112 if (!tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009113 tw32(MBFREE_MODE, MBFREE_MODE_ENABLE);
Matt Carlson9936bcf2007-10-10 18:03:07 -07009114
9115 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
9116 tw32(SNDDATAC_MODE,
9117 SNDDATAC_MODE_ENABLE | SNDDATAC_MODE_CDELAY);
9118 else
9119 tw32(SNDDATAC_MODE, SNDDATAC_MODE_ENABLE);
9120
Linus Torvalds1da177e2005-04-16 15:20:36 -07009121 tw32(SNDBDC_MODE, SNDBDC_MODE_ENABLE | SNDBDC_MODE_ATTN_ENABLE);
9122 tw32(RCVBDI_MODE, RCVBDI_MODE_ENABLE | RCVBDI_MODE_RCB_ATTN_ENAB);
Matt Carlson7cb32cf2010-09-30 10:34:36 +00009123 val = RCVDBDI_MODE_ENABLE | RCVDBDI_MODE_INV_RING_SZ;
Joe Perches63c3a662011-04-26 08:12:10 +00009124 if (tg3_flag(tp, LRG_PROD_RING_CAP))
Matt Carlson7cb32cf2010-09-30 10:34:36 +00009125 val |= RCVDBDI_MODE_LRG_RING_SZ;
9126 tw32(RCVDBDI_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009127 tw32(SNDDATAI_MODE, SNDDATAI_MODE_ENABLE);
Joe Perches63c3a662011-04-26 08:12:10 +00009128 if (tg3_flag(tp, HW_TSO_1) ||
9129 tg3_flag(tp, HW_TSO_2) ||
9130 tg3_flag(tp, HW_TSO_3))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009131 tw32(SNDDATAI_MODE, SNDDATAI_MODE_ENABLE | 0x8);
Matt Carlsonbaf8a942009-09-01 13:13:00 +00009132 val = SNDBDI_MODE_ENABLE | SNDBDI_MODE_ATTN_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00009133 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlsonbaf8a942009-09-01 13:13:00 +00009134 val |= SNDBDI_MODE_MULTI_TXQ_EN;
9135 tw32(SNDBDI_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009136 tw32(SNDBDS_MODE, SNDBDS_MODE_ENABLE | SNDBDS_MODE_ATTN_ENABLE);
9137
9138 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0) {
9139 err = tg3_load_5701_a0_firmware_fix(tp);
9140 if (err)
9141 return err;
9142 }
9143
Joe Perches63c3a662011-04-26 08:12:10 +00009144 if (tg3_flag(tp, TSO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009145 err = tg3_load_tso_firmware(tp);
9146 if (err)
9147 return err;
9148 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009149
9150 tp->tx_mode = TX_MODE_ENABLE;
Matt Carlsonf2096f92011-04-05 14:22:48 +00009151
Joe Perches63c3a662011-04-26 08:12:10 +00009152 if (tg3_flag(tp, 5755_PLUS) ||
Matt Carlsonb1d05212010-06-05 17:24:31 +00009153 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
9154 tp->tx_mode |= TX_MODE_MBUF_LOCKUP_FIX;
Matt Carlsonf2096f92011-04-05 14:22:48 +00009155
9156 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
9157 val = TX_MODE_JMB_FRM_LEN | TX_MODE_CNT_DN_MODE;
9158 tp->tx_mode &= ~val;
9159 tp->tx_mode |= tr32(MAC_TX_MODE) & val;
9160 }
9161
Linus Torvalds1da177e2005-04-16 15:20:36 -07009162 tw32_f(MAC_TX_MODE, tp->tx_mode);
9163 udelay(100);
9164
Joe Perches63c3a662011-04-26 08:12:10 +00009165 if (tg3_flag(tp, ENABLE_RSS)) {
Matt Carlsonbcebcc42011-12-14 11:10:01 +00009166 tg3_rss_write_indir_tbl(tp);
Matt Carlsonbaf8a942009-09-01 13:13:00 +00009167
9168 /* Setup the "secret" hash key. */
9169 tw32(MAC_RSS_HASH_KEY_0, 0x5f865437);
9170 tw32(MAC_RSS_HASH_KEY_1, 0xe4ac62cc);
9171 tw32(MAC_RSS_HASH_KEY_2, 0x50103a45);
9172 tw32(MAC_RSS_HASH_KEY_3, 0x36621985);
9173 tw32(MAC_RSS_HASH_KEY_4, 0xbf14c0e8);
9174 tw32(MAC_RSS_HASH_KEY_5, 0x1bc27a1e);
9175 tw32(MAC_RSS_HASH_KEY_6, 0x84f4b556);
9176 tw32(MAC_RSS_HASH_KEY_7, 0x094ea6fe);
9177 tw32(MAC_RSS_HASH_KEY_8, 0x7dda01e7);
9178 tw32(MAC_RSS_HASH_KEY_9, 0xc04d7481);
9179 }
9180
Linus Torvalds1da177e2005-04-16 15:20:36 -07009181 tp->rx_mode = RX_MODE_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00009182 if (tg3_flag(tp, 5755_PLUS))
Michael Chanaf36e6b2006-03-23 01:28:06 -08009183 tp->rx_mode |= RX_MODE_IPV6_CSUM_ENABLE;
9184
Joe Perches63c3a662011-04-26 08:12:10 +00009185 if (tg3_flag(tp, ENABLE_RSS))
Matt Carlsonbaf8a942009-09-01 13:13:00 +00009186 tp->rx_mode |= RX_MODE_RSS_ENABLE |
9187 RX_MODE_RSS_ITBL_HASH_BITS_7 |
9188 RX_MODE_RSS_IPV6_HASH_EN |
9189 RX_MODE_RSS_TCP_IPV6_HASH_EN |
9190 RX_MODE_RSS_IPV4_HASH_EN |
9191 RX_MODE_RSS_TCP_IPV4_HASH_EN;
9192
Linus Torvalds1da177e2005-04-16 15:20:36 -07009193 tw32_f(MAC_RX_MODE, tp->rx_mode);
9194 udelay(10);
9195
Linus Torvalds1da177e2005-04-16 15:20:36 -07009196 tw32(MAC_LED_CTRL, tp->led_ctrl);
9197
9198 tw32(MAC_MI_STAT, MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009199 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009200 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
9201 udelay(10);
9202 }
9203 tw32_f(MAC_RX_MODE, tp->rx_mode);
9204 udelay(10);
9205
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009206 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009207 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009208 !(tp->phy_flags & TG3_PHYFLG_SERDES_PREEMPHASIS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009209 /* Set drive transmission level to 1.2V */
9210 /* only if the signal pre-emphasis bit is not set */
9211 val = tr32(MAC_SERDES_CFG);
9212 val &= 0xfffff000;
9213 val |= 0x880;
9214 tw32(MAC_SERDES_CFG, val);
9215 }
9216 if (tp->pci_chip_rev_id == CHIPREV_ID_5703_A1)
9217 tw32(MAC_SERDES_CFG, 0x616000);
9218 }
9219
9220 /* Prevent chip from dropping frames when flow control
9221 * is enabled.
9222 */
Matt Carlson55086ad2011-12-14 11:09:59 +00009223 if (tg3_flag(tp, 57765_CLASS))
Matt Carlson666bc832010-01-20 16:58:03 +00009224 val = 1;
9225 else
9226 val = 2;
9227 tw32_f(MAC_LOW_WMARK_MAX_RX_FRAME, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009228
9229 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009230 (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009231 /* Use hardware link auto-negotiation */
Joe Perches63c3a662011-04-26 08:12:10 +00009232 tg3_flag_set(tp, HW_AUTONEG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009233 }
9234
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009235 if ((tp->phy_flags & TG3_PHYFLG_MII_SERDES) &&
Matt Carlson6ff6f812011-05-19 12:12:54 +00009236 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
Michael Chand4d2c552006-03-20 17:47:20 -08009237 u32 tmp;
9238
9239 tmp = tr32(SERDES_RX_CTRL);
9240 tw32(SERDES_RX_CTRL, tmp | SERDES_RX_SIG_DETECT);
9241 tp->grc_local_ctrl &= ~GRC_LCLCTRL_USE_EXT_SIG_DETECT;
9242 tp->grc_local_ctrl |= GRC_LCLCTRL_USE_SIG_DETECT;
9243 tw32(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
9244 }
9245
Joe Perches63c3a662011-04-26 08:12:10 +00009246 if (!tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson800960682010-08-02 11:26:06 +00009247 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) {
9248 tp->phy_flags &= ~TG3_PHYFLG_IS_LOW_POWER;
Matt Carlsondd477002008-05-25 23:45:58 -07009249 tp->link_config.speed = tp->link_config.orig_speed;
9250 tp->link_config.duplex = tp->link_config.orig_duplex;
9251 tp->link_config.autoneg = tp->link_config.orig_autoneg;
9252 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009253
Matt Carlsondd477002008-05-25 23:45:58 -07009254 err = tg3_setup_phy(tp, 0);
9255 if (err)
9256 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009257
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009258 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
9259 !(tp->phy_flags & TG3_PHYFLG_IS_FET)) {
Matt Carlsondd477002008-05-25 23:45:58 -07009260 u32 tmp;
9261
9262 /* Clear CRC stats. */
9263 if (!tg3_readphy(tp, MII_TG3_TEST1, &tmp)) {
9264 tg3_writephy(tp, MII_TG3_TEST1,
9265 tmp | MII_TG3_TEST1_CRC_EN);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00009266 tg3_readphy(tp, MII_TG3_RXR_COUNTERS, &tmp);
Matt Carlsondd477002008-05-25 23:45:58 -07009267 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009268 }
9269 }
9270
9271 __tg3_set_rx_mode(tp->dev);
9272
9273 /* Initialize receive rules. */
9274 tw32(MAC_RCV_RULE_0, 0xc2000000 & RCV_RULE_DISABLE_MASK);
9275 tw32(MAC_RCV_VALUE_0, 0xffffffff & RCV_RULE_DISABLE_MASK);
9276 tw32(MAC_RCV_RULE_1, 0x86000004 & RCV_RULE_DISABLE_MASK);
9277 tw32(MAC_RCV_VALUE_1, 0xffffffff & RCV_RULE_DISABLE_MASK);
9278
Joe Perches63c3a662011-04-26 08:12:10 +00009279 if (tg3_flag(tp, 5705_PLUS) && !tg3_flag(tp, 5780_CLASS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009280 limit = 8;
9281 else
9282 limit = 16;
Joe Perches63c3a662011-04-26 08:12:10 +00009283 if (tg3_flag(tp, ENABLE_ASF))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009284 limit -= 4;
9285 switch (limit) {
9286 case 16:
9287 tw32(MAC_RCV_RULE_15, 0); tw32(MAC_RCV_VALUE_15, 0);
9288 case 15:
9289 tw32(MAC_RCV_RULE_14, 0); tw32(MAC_RCV_VALUE_14, 0);
9290 case 14:
9291 tw32(MAC_RCV_RULE_13, 0); tw32(MAC_RCV_VALUE_13, 0);
9292 case 13:
9293 tw32(MAC_RCV_RULE_12, 0); tw32(MAC_RCV_VALUE_12, 0);
9294 case 12:
9295 tw32(MAC_RCV_RULE_11, 0); tw32(MAC_RCV_VALUE_11, 0);
9296 case 11:
9297 tw32(MAC_RCV_RULE_10, 0); tw32(MAC_RCV_VALUE_10, 0);
9298 case 10:
9299 tw32(MAC_RCV_RULE_9, 0); tw32(MAC_RCV_VALUE_9, 0);
9300 case 9:
9301 tw32(MAC_RCV_RULE_8, 0); tw32(MAC_RCV_VALUE_8, 0);
9302 case 8:
9303 tw32(MAC_RCV_RULE_7, 0); tw32(MAC_RCV_VALUE_7, 0);
9304 case 7:
9305 tw32(MAC_RCV_RULE_6, 0); tw32(MAC_RCV_VALUE_6, 0);
9306 case 6:
9307 tw32(MAC_RCV_RULE_5, 0); tw32(MAC_RCV_VALUE_5, 0);
9308 case 5:
9309 tw32(MAC_RCV_RULE_4, 0); tw32(MAC_RCV_VALUE_4, 0);
9310 case 4:
9311 /* tw32(MAC_RCV_RULE_3, 0); tw32(MAC_RCV_VALUE_3, 0); */
9312 case 3:
9313 /* tw32(MAC_RCV_RULE_2, 0); tw32(MAC_RCV_VALUE_2, 0); */
9314 case 2:
9315 case 1:
9316
9317 default:
9318 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07009319 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009320
Joe Perches63c3a662011-04-26 08:12:10 +00009321 if (tg3_flag(tp, ENABLE_APE))
Matt Carlson9ce768e2007-10-11 19:49:11 -07009322 /* Write our heartbeat update interval to APE. */
9323 tg3_ape_write32(tp, TG3_APE_HOST_HEARTBEAT_INT_MS,
9324 APE_HOST_HEARTBEAT_INT_DISABLE);
Matt Carlson0d3031d2007-10-10 18:02:43 -07009325
Linus Torvalds1da177e2005-04-16 15:20:36 -07009326 tg3_write_sig_post_reset(tp, RESET_KIND_INIT);
9327
Linus Torvalds1da177e2005-04-16 15:20:36 -07009328 return 0;
9329}
9330
9331/* Called at device open time to get the chip ready for
9332 * packet processing. Invoked with tp->lock held.
9333 */
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07009334static int tg3_init_hw(struct tg3 *tp, int reset_phy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009335{
Linus Torvalds1da177e2005-04-16 15:20:36 -07009336 tg3_switch_clocks(tp);
9337
9338 tw32(TG3PCI_MEM_WIN_BASE_ADDR, 0);
9339
Matt Carlson2f751b62008-08-04 23:17:34 -07009340 return tg3_reset_hw(tp, reset_phy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009341}
9342
Matt Carlsonebf33122012-02-13 10:20:05 +00009343/* Restart hardware after configuration changes, self-test, etc.
9344 * Invoked with tp->lock held.
9345 */
9346static int tg3_restart_hw(struct tg3 *tp, int reset_phy)
9347 __releases(tp->lock)
9348 __acquires(tp->lock)
9349{
9350 int err;
9351
9352 err = tg3_init_hw(tp, reset_phy);
9353 if (err) {
9354 netdev_err(tp->dev,
9355 "Failed to re-initialize device, aborting\n");
9356 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
9357 tg3_full_unlock(tp);
9358 del_timer_sync(&tp->timer);
9359 tp->irq_sync = 0;
9360 tg3_napi_enable(tp);
9361 dev_close(tp->dev);
9362 tg3_full_lock(tp, 0);
9363 }
9364 return err;
9365}
9366
Matt Carlson9a21fb82012-02-13 10:20:06 +00009367static void tg3_reset_task(struct work_struct *work)
9368{
9369 struct tg3 *tp = container_of(work, struct tg3, reset_task);
9370 int err;
9371
9372 tg3_full_lock(tp, 0);
9373
9374 if (!netif_running(tp->dev)) {
9375 tg3_flag_clear(tp, RESET_TASK_PENDING);
9376 tg3_full_unlock(tp);
9377 return;
9378 }
9379
9380 tg3_full_unlock(tp);
9381
9382 tg3_phy_stop(tp);
9383
9384 tg3_netif_stop(tp);
9385
9386 tg3_full_lock(tp, 1);
9387
9388 if (tg3_flag(tp, TX_RECOVERY_PENDING)) {
9389 tp->write32_tx_mbox = tg3_write32_tx_mbox;
9390 tp->write32_rx_mbox = tg3_write_flush_reg32;
9391 tg3_flag_set(tp, MBOX_WRITE_REORDER);
9392 tg3_flag_clear(tp, TX_RECOVERY_PENDING);
9393 }
9394
9395 tg3_halt(tp, RESET_KIND_SHUTDOWN, 0);
9396 err = tg3_init_hw(tp, 1);
9397 if (err)
9398 goto out;
9399
9400 tg3_netif_start(tp);
9401
9402out:
9403 tg3_full_unlock(tp);
9404
9405 if (!err)
9406 tg3_phy_start(tp);
9407
9408 tg3_flag_clear(tp, RESET_TASK_PENDING);
9409}
9410
Linus Torvalds1da177e2005-04-16 15:20:36 -07009411#define TG3_STAT_ADD32(PSTAT, REG) \
9412do { u32 __val = tr32(REG); \
9413 (PSTAT)->low += __val; \
9414 if ((PSTAT)->low < __val) \
9415 (PSTAT)->high += 1; \
9416} while (0)
9417
9418static void tg3_periodic_fetch_stats(struct tg3 *tp)
9419{
9420 struct tg3_hw_stats *sp = tp->hw_stats;
9421
9422 if (!netif_carrier_ok(tp->dev))
9423 return;
9424
9425 TG3_STAT_ADD32(&sp->tx_octets, MAC_TX_STATS_OCTETS);
9426 TG3_STAT_ADD32(&sp->tx_collisions, MAC_TX_STATS_COLLISIONS);
9427 TG3_STAT_ADD32(&sp->tx_xon_sent, MAC_TX_STATS_XON_SENT);
9428 TG3_STAT_ADD32(&sp->tx_xoff_sent, MAC_TX_STATS_XOFF_SENT);
9429 TG3_STAT_ADD32(&sp->tx_mac_errors, MAC_TX_STATS_MAC_ERRORS);
9430 TG3_STAT_ADD32(&sp->tx_single_collisions, MAC_TX_STATS_SINGLE_COLLISIONS);
9431 TG3_STAT_ADD32(&sp->tx_mult_collisions, MAC_TX_STATS_MULT_COLLISIONS);
9432 TG3_STAT_ADD32(&sp->tx_deferred, MAC_TX_STATS_DEFERRED);
9433 TG3_STAT_ADD32(&sp->tx_excessive_collisions, MAC_TX_STATS_EXCESSIVE_COL);
9434 TG3_STAT_ADD32(&sp->tx_late_collisions, MAC_TX_STATS_LATE_COL);
9435 TG3_STAT_ADD32(&sp->tx_ucast_packets, MAC_TX_STATS_UCAST);
9436 TG3_STAT_ADD32(&sp->tx_mcast_packets, MAC_TX_STATS_MCAST);
9437 TG3_STAT_ADD32(&sp->tx_bcast_packets, MAC_TX_STATS_BCAST);
9438
9439 TG3_STAT_ADD32(&sp->rx_octets, MAC_RX_STATS_OCTETS);
9440 TG3_STAT_ADD32(&sp->rx_fragments, MAC_RX_STATS_FRAGMENTS);
9441 TG3_STAT_ADD32(&sp->rx_ucast_packets, MAC_RX_STATS_UCAST);
9442 TG3_STAT_ADD32(&sp->rx_mcast_packets, MAC_RX_STATS_MCAST);
9443 TG3_STAT_ADD32(&sp->rx_bcast_packets, MAC_RX_STATS_BCAST);
9444 TG3_STAT_ADD32(&sp->rx_fcs_errors, MAC_RX_STATS_FCS_ERRORS);
9445 TG3_STAT_ADD32(&sp->rx_align_errors, MAC_RX_STATS_ALIGN_ERRORS);
9446 TG3_STAT_ADD32(&sp->rx_xon_pause_rcvd, MAC_RX_STATS_XON_PAUSE_RECVD);
9447 TG3_STAT_ADD32(&sp->rx_xoff_pause_rcvd, MAC_RX_STATS_XOFF_PAUSE_RECVD);
9448 TG3_STAT_ADD32(&sp->rx_mac_ctrl_rcvd, MAC_RX_STATS_MAC_CTRL_RECVD);
9449 TG3_STAT_ADD32(&sp->rx_xoff_entered, MAC_RX_STATS_XOFF_ENTERED);
9450 TG3_STAT_ADD32(&sp->rx_frame_too_long_errors, MAC_RX_STATS_FRAME_TOO_LONG);
9451 TG3_STAT_ADD32(&sp->rx_jabbers, MAC_RX_STATS_JABBERS);
9452 TG3_STAT_ADD32(&sp->rx_undersize_packets, MAC_RX_STATS_UNDERSIZE);
Michael Chan463d3052006-05-22 16:36:27 -07009453
9454 TG3_STAT_ADD32(&sp->rxbds_empty, RCVLPC_NO_RCV_BD_CNT);
Matt Carlson310050f2011-05-19 12:12:55 +00009455 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717 &&
9456 tp->pci_chip_rev_id != CHIPREV_ID_5719_A0 &&
9457 tp->pci_chip_rev_id != CHIPREV_ID_5720_A0) {
Matt Carlson4d958472011-04-20 07:57:35 +00009458 TG3_STAT_ADD32(&sp->rx_discards, RCVLPC_IN_DISCARDS_CNT);
9459 } else {
9460 u32 val = tr32(HOSTCC_FLOW_ATTN);
9461 val = (val & HOSTCC_FLOW_ATTN_MBUF_LWM) ? 1 : 0;
9462 if (val) {
9463 tw32(HOSTCC_FLOW_ATTN, HOSTCC_FLOW_ATTN_MBUF_LWM);
9464 sp->rx_discards.low += val;
9465 if (sp->rx_discards.low < val)
9466 sp->rx_discards.high += 1;
9467 }
9468 sp->mbuf_lwm_thresh_hit = sp->rx_discards;
9469 }
Michael Chan463d3052006-05-22 16:36:27 -07009470 TG3_STAT_ADD32(&sp->rx_errors, RCVLPC_IN_ERRORS_CNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009471}
9472
Matt Carlson0e6cf6a2011-06-13 13:38:55 +00009473static void tg3_chk_missed_msi(struct tg3 *tp)
9474{
9475 u32 i;
9476
9477 for (i = 0; i < tp->irq_cnt; i++) {
9478 struct tg3_napi *tnapi = &tp->napi[i];
9479
9480 if (tg3_has_work(tnapi)) {
9481 if (tnapi->last_rx_cons == tnapi->rx_rcb_ptr &&
9482 tnapi->last_tx_cons == tnapi->tx_cons) {
9483 if (tnapi->chk_msi_cnt < 1) {
9484 tnapi->chk_msi_cnt++;
9485 return;
9486 }
Matt Carlson7f230732011-08-31 11:44:48 +00009487 tg3_msi(0, tnapi);
Matt Carlson0e6cf6a2011-06-13 13:38:55 +00009488 }
9489 }
9490 tnapi->chk_msi_cnt = 0;
9491 tnapi->last_rx_cons = tnapi->rx_rcb_ptr;
9492 tnapi->last_tx_cons = tnapi->tx_cons;
9493 }
9494}
9495
Linus Torvalds1da177e2005-04-16 15:20:36 -07009496static void tg3_timer(unsigned long __opaque)
9497{
9498 struct tg3 *tp = (struct tg3 *) __opaque;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009499
Matt Carlson5b190622011-11-04 09:15:04 +00009500 if (tp->irq_sync || tg3_flag(tp, RESET_TASK_PENDING))
Michael Chanf475f162006-03-27 23:20:14 -08009501 goto restart_timer;
9502
David S. Millerf47c11e2005-06-24 20:18:35 -07009503 spin_lock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009504
Matt Carlson0e6cf6a2011-06-13 13:38:55 +00009505 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
Matt Carlson55086ad2011-12-14 11:09:59 +00009506 tg3_flag(tp, 57765_CLASS))
Matt Carlson0e6cf6a2011-06-13 13:38:55 +00009507 tg3_chk_missed_msi(tp);
9508
Joe Perches63c3a662011-04-26 08:12:10 +00009509 if (!tg3_flag(tp, TAGGED_STATUS)) {
David S. Millerfac9b832005-05-18 22:46:34 -07009510 /* All of this garbage is because when using non-tagged
9511 * IRQ status the mailbox/status_block protocol the chip
9512 * uses with the cpu is race prone.
9513 */
Matt Carlson898a56f2009-08-28 14:02:40 +00009514 if (tp->napi[0].hw_status->status & SD_STATUS_UPDATED) {
David S. Millerfac9b832005-05-18 22:46:34 -07009515 tw32(GRC_LOCAL_CTRL,
9516 tp->grc_local_ctrl | GRC_LCLCTRL_SETINT);
9517 } else {
9518 tw32(HOSTCC_MODE, tp->coalesce_mode |
Matt Carlsonfd2ce372009-09-01 12:51:13 +00009519 HOSTCC_MODE_ENABLE | HOSTCC_MODE_NOW);
David S. Millerfac9b832005-05-18 22:46:34 -07009520 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009521
David S. Millerfac9b832005-05-18 22:46:34 -07009522 if (!(tr32(WDMAC_MODE) & WDMAC_MODE_ENABLE)) {
David S. Millerf47c11e2005-06-24 20:18:35 -07009523 spin_unlock(&tp->lock);
Matt Carlsondb219972011-11-04 09:15:03 +00009524 tg3_reset_task_schedule(tp);
Matt Carlson5b190622011-11-04 09:15:04 +00009525 goto restart_timer;
David S. Millerfac9b832005-05-18 22:46:34 -07009526 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009527 }
9528
Linus Torvalds1da177e2005-04-16 15:20:36 -07009529 /* This part only runs once per second. */
9530 if (!--tp->timer_counter) {
Joe Perches63c3a662011-04-26 08:12:10 +00009531 if (tg3_flag(tp, 5705_PLUS))
David S. Millerfac9b832005-05-18 22:46:34 -07009532 tg3_periodic_fetch_stats(tp);
9533
Matt Carlsonb0c59432011-05-19 12:12:48 +00009534 if (tp->setlpicnt && !--tp->setlpicnt)
9535 tg3_phy_eee_enable(tp);
Matt Carlson52b02d02010-10-14 10:37:41 +00009536
Joe Perches63c3a662011-04-26 08:12:10 +00009537 if (tg3_flag(tp, USE_LINKCHG_REG)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009538 u32 mac_stat;
9539 int phy_event;
9540
9541 mac_stat = tr32(MAC_STATUS);
9542
9543 phy_event = 0;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009544 if (tp->phy_flags & TG3_PHYFLG_USE_MI_INTERRUPT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009545 if (mac_stat & MAC_STATUS_MI_INTERRUPT)
9546 phy_event = 1;
9547 } else if (mac_stat & MAC_STATUS_LNKSTATE_CHANGED)
9548 phy_event = 1;
9549
9550 if (phy_event)
9551 tg3_setup_phy(tp, 0);
Joe Perches63c3a662011-04-26 08:12:10 +00009552 } else if (tg3_flag(tp, POLL_SERDES)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009553 u32 mac_stat = tr32(MAC_STATUS);
9554 int need_setup = 0;
9555
9556 if (netif_carrier_ok(tp->dev) &&
9557 (mac_stat & MAC_STATUS_LNKSTATE_CHANGED)) {
9558 need_setup = 1;
9559 }
Matt Carlsonbe98da62010-07-11 09:31:46 +00009560 if (!netif_carrier_ok(tp->dev) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07009561 (mac_stat & (MAC_STATUS_PCS_SYNCED |
9562 MAC_STATUS_SIGNAL_DET))) {
9563 need_setup = 1;
9564 }
9565 if (need_setup) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07009566 if (!tp->serdes_counter) {
9567 tw32_f(MAC_MODE,
9568 (tp->mac_mode &
9569 ~MAC_MODE_PORT_MODE_MASK));
9570 udelay(40);
9571 tw32_f(MAC_MODE, tp->mac_mode);
9572 udelay(40);
9573 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009574 tg3_setup_phy(tp, 0);
9575 }
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009576 } else if ((tp->phy_flags & TG3_PHYFLG_MII_SERDES) &&
Joe Perches63c3a662011-04-26 08:12:10 +00009577 tg3_flag(tp, 5780_CLASS)) {
Michael Chan747e8f82005-07-25 12:33:22 -07009578 tg3_serdes_parallel_detect(tp);
Matt Carlson57d8b882010-06-05 17:24:35 +00009579 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009580
9581 tp->timer_counter = tp->timer_multiplier;
9582 }
9583
Michael Chan130b8e42006-09-27 16:00:40 -07009584 /* Heartbeat is only sent once every 2 seconds.
9585 *
9586 * The heartbeat is to tell the ASF firmware that the host
9587 * driver is still alive. In the event that the OS crashes,
9588 * ASF needs to reset the hardware to free up the FIFO space
9589 * that may be filled with rx packets destined for the host.
9590 * If the FIFO is full, ASF will no longer function properly.
9591 *
9592 * Unintended resets have been reported on real time kernels
9593 * where the timer doesn't run on time. Netpoll will also have
9594 * same problem.
9595 *
9596 * The new FWCMD_NICDRV_ALIVE3 command tells the ASF firmware
9597 * to check the ring condition when the heartbeat is expiring
9598 * before doing the reset. This will prevent most unintended
9599 * resets.
9600 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07009601 if (!--tp->asf_counter) {
Joe Perches63c3a662011-04-26 08:12:10 +00009602 if (tg3_flag(tp, ENABLE_ASF) && !tg3_flag(tp, ENABLE_APE)) {
Matt Carlson7c5026a2008-05-02 16:49:29 -07009603 tg3_wait_for_event_ack(tp);
9604
Michael Chanbbadf502006-04-06 21:46:34 -07009605 tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX,
Michael Chan130b8e42006-09-27 16:00:40 -07009606 FWCMD_NICDRV_ALIVE3);
Michael Chanbbadf502006-04-06 21:46:34 -07009607 tg3_write_mem(tp, NIC_SRAM_FW_CMD_LEN_MBOX, 4);
Matt Carlsonc6cdf432010-04-05 10:19:26 +00009608 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX,
9609 TG3_FW_UPDATE_TIMEOUT_SEC);
Matt Carlson4ba526c2008-08-15 14:10:04 -07009610
9611 tg3_generate_fw_event(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009612 }
9613 tp->asf_counter = tp->asf_multiplier;
9614 }
9615
David S. Millerf47c11e2005-06-24 20:18:35 -07009616 spin_unlock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009617
Michael Chanf475f162006-03-27 23:20:14 -08009618restart_timer:
Linus Torvalds1da177e2005-04-16 15:20:36 -07009619 tp->timer.expires = jiffies + tp->timer_offset;
9620 add_timer(&tp->timer);
9621}
9622
Matt Carlson4f125f42009-09-01 12:55:02 +00009623static int tg3_request_irq(struct tg3 *tp, int irq_num)
Michael Chanfcfa0a32006-03-20 22:28:41 -08009624{
David Howells7d12e782006-10-05 14:55:46 +01009625 irq_handler_t fn;
Michael Chanfcfa0a32006-03-20 22:28:41 -08009626 unsigned long flags;
Matt Carlson4f125f42009-09-01 12:55:02 +00009627 char *name;
9628 struct tg3_napi *tnapi = &tp->napi[irq_num];
9629
9630 if (tp->irq_cnt == 1)
9631 name = tp->dev->name;
9632 else {
9633 name = &tnapi->irq_lbl[0];
9634 snprintf(name, IFNAMSIZ, "%s-%d", tp->dev->name, irq_num);
9635 name[IFNAMSIZ-1] = 0;
9636 }
Michael Chanfcfa0a32006-03-20 22:28:41 -08009637
Joe Perches63c3a662011-04-26 08:12:10 +00009638 if (tg3_flag(tp, USING_MSI) || tg3_flag(tp, USING_MSIX)) {
Michael Chanfcfa0a32006-03-20 22:28:41 -08009639 fn = tg3_msi;
Joe Perches63c3a662011-04-26 08:12:10 +00009640 if (tg3_flag(tp, 1SHOT_MSI))
Michael Chanfcfa0a32006-03-20 22:28:41 -08009641 fn = tg3_msi_1shot;
Javier Martinez Canillasab392d22011-03-28 16:27:31 +00009642 flags = 0;
Michael Chanfcfa0a32006-03-20 22:28:41 -08009643 } else {
9644 fn = tg3_interrupt;
Joe Perches63c3a662011-04-26 08:12:10 +00009645 if (tg3_flag(tp, TAGGED_STATUS))
Michael Chanfcfa0a32006-03-20 22:28:41 -08009646 fn = tg3_interrupt_tagged;
Javier Martinez Canillasab392d22011-03-28 16:27:31 +00009647 flags = IRQF_SHARED;
Michael Chanfcfa0a32006-03-20 22:28:41 -08009648 }
Matt Carlson4f125f42009-09-01 12:55:02 +00009649
9650 return request_irq(tnapi->irq_vec, fn, flags, name, tnapi);
Michael Chanfcfa0a32006-03-20 22:28:41 -08009651}
9652
Michael Chan79381092005-04-21 17:13:59 -07009653static int tg3_test_interrupt(struct tg3 *tp)
9654{
Matt Carlson09943a12009-08-28 14:01:57 +00009655 struct tg3_napi *tnapi = &tp->napi[0];
Michael Chan79381092005-04-21 17:13:59 -07009656 struct net_device *dev = tp->dev;
Michael Chanb16250e2006-09-27 16:10:14 -07009657 int err, i, intr_ok = 0;
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009658 u32 val;
Michael Chan79381092005-04-21 17:13:59 -07009659
Michael Chand4bc3922005-05-29 14:59:20 -07009660 if (!netif_running(dev))
9661 return -ENODEV;
9662
Michael Chan79381092005-04-21 17:13:59 -07009663 tg3_disable_ints(tp);
9664
Matt Carlson4f125f42009-09-01 12:55:02 +00009665 free_irq(tnapi->irq_vec, tnapi);
Michael Chan79381092005-04-21 17:13:59 -07009666
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009667 /*
9668 * Turn off MSI one shot mode. Otherwise this test has no
9669 * observable way to know whether the interrupt was delivered.
9670 */
Matt Carlson3aa1cdf2011-07-20 10:20:55 +00009671 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009672 val = tr32(MSGINT_MODE) | MSGINT_MODE_ONE_SHOT_DISABLE;
9673 tw32(MSGINT_MODE, val);
9674 }
9675
Matt Carlson4f125f42009-09-01 12:55:02 +00009676 err = request_irq(tnapi->irq_vec, tg3_test_isr,
Matt Carlson09943a12009-08-28 14:01:57 +00009677 IRQF_SHARED | IRQF_SAMPLE_RANDOM, dev->name, tnapi);
Michael Chan79381092005-04-21 17:13:59 -07009678 if (err)
9679 return err;
9680
Matt Carlson898a56f2009-08-28 14:02:40 +00009681 tnapi->hw_status->status &= ~SD_STATUS_UPDATED;
Michael Chan79381092005-04-21 17:13:59 -07009682 tg3_enable_ints(tp);
9683
9684 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +00009685 tnapi->coal_now);
Michael Chan79381092005-04-21 17:13:59 -07009686
9687 for (i = 0; i < 5; i++) {
Michael Chanb16250e2006-09-27 16:10:14 -07009688 u32 int_mbox, misc_host_ctrl;
9689
Matt Carlson898a56f2009-08-28 14:02:40 +00009690 int_mbox = tr32_mailbox(tnapi->int_mbox);
Michael Chanb16250e2006-09-27 16:10:14 -07009691 misc_host_ctrl = tr32(TG3PCI_MISC_HOST_CTRL);
9692
9693 if ((int_mbox != 0) ||
9694 (misc_host_ctrl & MISC_HOST_CTRL_MASK_PCI_INT)) {
9695 intr_ok = 1;
Michael Chan79381092005-04-21 17:13:59 -07009696 break;
Michael Chanb16250e2006-09-27 16:10:14 -07009697 }
9698
Matt Carlson3aa1cdf2011-07-20 10:20:55 +00009699 if (tg3_flag(tp, 57765_PLUS) &&
9700 tnapi->hw_status->status_tag != tnapi->last_tag)
9701 tw32_mailbox_f(tnapi->int_mbox, tnapi->last_tag << 24);
9702
Michael Chan79381092005-04-21 17:13:59 -07009703 msleep(10);
9704 }
9705
9706 tg3_disable_ints(tp);
9707
Matt Carlson4f125f42009-09-01 12:55:02 +00009708 free_irq(tnapi->irq_vec, tnapi);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009709
Matt Carlson4f125f42009-09-01 12:55:02 +00009710 err = tg3_request_irq(tp, 0);
Michael Chan79381092005-04-21 17:13:59 -07009711
9712 if (err)
9713 return err;
9714
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009715 if (intr_ok) {
9716 /* Reenable MSI one shot mode. */
Matt Carlson5b39de92011-08-31 11:44:50 +00009717 if (tg3_flag(tp, 57765_PLUS) && tg3_flag(tp, 1SHOT_MSI)) {
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009718 val = tr32(MSGINT_MODE) & ~MSGINT_MODE_ONE_SHOT_DISABLE;
9719 tw32(MSGINT_MODE, val);
9720 }
Michael Chan79381092005-04-21 17:13:59 -07009721 return 0;
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009722 }
Michael Chan79381092005-04-21 17:13:59 -07009723
9724 return -EIO;
9725}
9726
9727/* Returns 0 if MSI test succeeds or MSI test fails and INTx mode is
9728 * successfully restored
9729 */
9730static int tg3_test_msi(struct tg3 *tp)
9731{
Michael Chan79381092005-04-21 17:13:59 -07009732 int err;
9733 u16 pci_cmd;
9734
Joe Perches63c3a662011-04-26 08:12:10 +00009735 if (!tg3_flag(tp, USING_MSI))
Michael Chan79381092005-04-21 17:13:59 -07009736 return 0;
9737
9738 /* Turn off SERR reporting in case MSI terminates with Master
9739 * Abort.
9740 */
9741 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
9742 pci_write_config_word(tp->pdev, PCI_COMMAND,
9743 pci_cmd & ~PCI_COMMAND_SERR);
9744
9745 err = tg3_test_interrupt(tp);
9746
9747 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
9748
9749 if (!err)
9750 return 0;
9751
9752 /* other failures */
9753 if (err != -EIO)
9754 return err;
9755
9756 /* MSI test failed, go back to INTx mode */
Matt Carlson5129c3a2010-04-05 10:19:23 +00009757 netdev_warn(tp->dev, "No interrupt was generated using MSI. Switching "
9758 "to INTx mode. Please report this failure to the PCI "
9759 "maintainer and include system chipset information\n");
Michael Chan79381092005-04-21 17:13:59 -07009760
Matt Carlson4f125f42009-09-01 12:55:02 +00009761 free_irq(tp->napi[0].irq_vec, &tp->napi[0]);
Matt Carlson09943a12009-08-28 14:01:57 +00009762
Michael Chan79381092005-04-21 17:13:59 -07009763 pci_disable_msi(tp->pdev);
9764
Joe Perches63c3a662011-04-26 08:12:10 +00009765 tg3_flag_clear(tp, USING_MSI);
Andre Detschdc8bf1b2010-04-26 07:27:07 +00009766 tp->napi[0].irq_vec = tp->pdev->irq;
Michael Chan79381092005-04-21 17:13:59 -07009767
Matt Carlson4f125f42009-09-01 12:55:02 +00009768 err = tg3_request_irq(tp, 0);
Michael Chan79381092005-04-21 17:13:59 -07009769 if (err)
9770 return err;
9771
9772 /* Need to reset the chip because the MSI cycle may have terminated
9773 * with Master Abort.
9774 */
David S. Millerf47c11e2005-06-24 20:18:35 -07009775 tg3_full_lock(tp, 1);
Michael Chan79381092005-04-21 17:13:59 -07009776
Michael Chan944d9802005-05-29 14:57:48 -07009777 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07009778 err = tg3_init_hw(tp, 1);
Michael Chan79381092005-04-21 17:13:59 -07009779
David S. Millerf47c11e2005-06-24 20:18:35 -07009780 tg3_full_unlock(tp);
Michael Chan79381092005-04-21 17:13:59 -07009781
9782 if (err)
Matt Carlson4f125f42009-09-01 12:55:02 +00009783 free_irq(tp->napi[0].irq_vec, &tp->napi[0]);
Michael Chan79381092005-04-21 17:13:59 -07009784
9785 return err;
9786}
9787
Matt Carlson9e9fd122009-01-19 16:57:45 -08009788static int tg3_request_firmware(struct tg3 *tp)
9789{
9790 const __be32 *fw_data;
9791
9792 if (request_firmware(&tp->fw, tp->fw_needed, &tp->pdev->dev)) {
Joe Perches05dbe002010-02-17 19:44:19 +00009793 netdev_err(tp->dev, "Failed to load firmware \"%s\"\n",
9794 tp->fw_needed);
Matt Carlson9e9fd122009-01-19 16:57:45 -08009795 return -ENOENT;
9796 }
9797
9798 fw_data = (void *)tp->fw->data;
9799
9800 /* Firmware blob starts with version numbers, followed by
9801 * start address and _full_ length including BSS sections
9802 * (which must be longer than the actual data, of course
9803 */
9804
9805 tp->fw_len = be32_to_cpu(fw_data[2]); /* includes bss */
9806 if (tp->fw_len < (tp->fw->size - 12)) {
Joe Perches05dbe002010-02-17 19:44:19 +00009807 netdev_err(tp->dev, "bogus length %d in \"%s\"\n",
9808 tp->fw_len, tp->fw_needed);
Matt Carlson9e9fd122009-01-19 16:57:45 -08009809 release_firmware(tp->fw);
9810 tp->fw = NULL;
9811 return -EINVAL;
9812 }
9813
9814 /* We no longer need firmware; we have it. */
9815 tp->fw_needed = NULL;
9816 return 0;
9817}
9818
Matt Carlson679563f2009-09-01 12:55:46 +00009819static bool tg3_enable_msix(struct tg3 *tp)
9820{
Matt Carlsonc3b50032012-01-17 15:27:23 +00009821 int i, rc;
Matt Carlson679563f2009-09-01 12:55:46 +00009822 struct msix_entry msix_ent[tp->irq_max];
9823
Matt Carlsonc3b50032012-01-17 15:27:23 +00009824 tp->irq_cnt = num_online_cpus();
9825 if (tp->irq_cnt > 1) {
9826 /* We want as many rx rings enabled as there are cpus.
9827 * In multiqueue MSI-X mode, the first MSI-X vector
9828 * only deals with link interrupts, etc, so we add
9829 * one to the number of vectors we are requesting.
9830 */
9831 tp->irq_cnt = min_t(unsigned, tp->irq_cnt + 1, tp->irq_max);
9832 }
Matt Carlson679563f2009-09-01 12:55:46 +00009833
9834 for (i = 0; i < tp->irq_max; i++) {
9835 msix_ent[i].entry = i;
9836 msix_ent[i].vector = 0;
9837 }
9838
9839 rc = pci_enable_msix(tp->pdev, msix_ent, tp->irq_cnt);
Matt Carlson2430b032010-06-05 17:24:34 +00009840 if (rc < 0) {
9841 return false;
9842 } else if (rc != 0) {
Matt Carlson679563f2009-09-01 12:55:46 +00009843 if (pci_enable_msix(tp->pdev, msix_ent, rc))
9844 return false;
Joe Perches05dbe002010-02-17 19:44:19 +00009845 netdev_notice(tp->dev, "Requested %d MSI-X vectors, received %d\n",
9846 tp->irq_cnt, rc);
Matt Carlson679563f2009-09-01 12:55:46 +00009847 tp->irq_cnt = rc;
9848 }
9849
9850 for (i = 0; i < tp->irq_max; i++)
9851 tp->napi[i].irq_vec = msix_ent[i].vector;
9852
Ben Hutchings2ddaad32010-09-27 22:11:51 -07009853 netif_set_real_num_tx_queues(tp->dev, 1);
9854 rc = tp->irq_cnt > 1 ? tp->irq_cnt - 1 : 1;
9855 if (netif_set_real_num_rx_queues(tp->dev, rc)) {
9856 pci_disable_msix(tp->pdev);
9857 return false;
9858 }
Matt Carlsonb92b9042010-11-24 08:31:51 +00009859
9860 if (tp->irq_cnt > 1) {
Joe Perches63c3a662011-04-26 08:12:10 +00009861 tg3_flag_set(tp, ENABLE_RSS);
Matt Carlsond78b59f2011-04-05 14:22:46 +00009862
9863 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
9864 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
Joe Perches63c3a662011-04-26 08:12:10 +00009865 tg3_flag_set(tp, ENABLE_TSS);
Matt Carlsonb92b9042010-11-24 08:31:51 +00009866 netif_set_real_num_tx_queues(tp->dev, tp->irq_cnt - 1);
9867 }
9868 }
Matt Carlson2430b032010-06-05 17:24:34 +00009869
Matt Carlson679563f2009-09-01 12:55:46 +00009870 return true;
9871}
9872
Matt Carlson07b01732009-08-28 14:01:15 +00009873static void tg3_ints_init(struct tg3 *tp)
9874{
Joe Perches63c3a662011-04-26 08:12:10 +00009875 if ((tg3_flag(tp, SUPPORT_MSI) || tg3_flag(tp, SUPPORT_MSIX)) &&
9876 !tg3_flag(tp, TAGGED_STATUS)) {
Matt Carlson07b01732009-08-28 14:01:15 +00009877 /* All MSI supporting chips should support tagged
9878 * status. Assert that this is the case.
9879 */
Matt Carlson5129c3a2010-04-05 10:19:23 +00009880 netdev_warn(tp->dev,
9881 "MSI without TAGGED_STATUS? Not using MSI\n");
Matt Carlson679563f2009-09-01 12:55:46 +00009882 goto defcfg;
Matt Carlson07b01732009-08-28 14:01:15 +00009883 }
Matt Carlson4f125f42009-09-01 12:55:02 +00009884
Joe Perches63c3a662011-04-26 08:12:10 +00009885 if (tg3_flag(tp, SUPPORT_MSIX) && tg3_enable_msix(tp))
9886 tg3_flag_set(tp, USING_MSIX);
9887 else if (tg3_flag(tp, SUPPORT_MSI) && pci_enable_msi(tp->pdev) == 0)
9888 tg3_flag_set(tp, USING_MSI);
Matt Carlson679563f2009-09-01 12:55:46 +00009889
Joe Perches63c3a662011-04-26 08:12:10 +00009890 if (tg3_flag(tp, USING_MSI) || tg3_flag(tp, USING_MSIX)) {
Matt Carlson679563f2009-09-01 12:55:46 +00009891 u32 msi_mode = tr32(MSGINT_MODE);
Joe Perches63c3a662011-04-26 08:12:10 +00009892 if (tg3_flag(tp, USING_MSIX) && tp->irq_cnt > 1)
Matt Carlsonbaf8a942009-09-01 13:13:00 +00009893 msi_mode |= MSGINT_MODE_MULTIVEC_EN;
Matt Carlson5b39de92011-08-31 11:44:50 +00009894 if (!tg3_flag(tp, 1SHOT_MSI))
9895 msi_mode |= MSGINT_MODE_ONE_SHOT_DISABLE;
Matt Carlson679563f2009-09-01 12:55:46 +00009896 tw32(MSGINT_MODE, msi_mode | MSGINT_MODE_ENABLE);
9897 }
9898defcfg:
Joe Perches63c3a662011-04-26 08:12:10 +00009899 if (!tg3_flag(tp, USING_MSIX)) {
Matt Carlson679563f2009-09-01 12:55:46 +00009900 tp->irq_cnt = 1;
9901 tp->napi[0].irq_vec = tp->pdev->irq;
Ben Hutchings2ddaad32010-09-27 22:11:51 -07009902 netif_set_real_num_tx_queues(tp->dev, 1);
Matt Carlson85407882010-10-06 13:40:58 -07009903 netif_set_real_num_rx_queues(tp->dev, 1);
Matt Carlson679563f2009-09-01 12:55:46 +00009904 }
Matt Carlson07b01732009-08-28 14:01:15 +00009905}
9906
9907static void tg3_ints_fini(struct tg3 *tp)
9908{
Joe Perches63c3a662011-04-26 08:12:10 +00009909 if (tg3_flag(tp, USING_MSIX))
Matt Carlson679563f2009-09-01 12:55:46 +00009910 pci_disable_msix(tp->pdev);
Joe Perches63c3a662011-04-26 08:12:10 +00009911 else if (tg3_flag(tp, USING_MSI))
Matt Carlson679563f2009-09-01 12:55:46 +00009912 pci_disable_msi(tp->pdev);
Joe Perches63c3a662011-04-26 08:12:10 +00009913 tg3_flag_clear(tp, USING_MSI);
9914 tg3_flag_clear(tp, USING_MSIX);
9915 tg3_flag_clear(tp, ENABLE_RSS);
9916 tg3_flag_clear(tp, ENABLE_TSS);
Matt Carlson07b01732009-08-28 14:01:15 +00009917}
9918
Linus Torvalds1da177e2005-04-16 15:20:36 -07009919static int tg3_open(struct net_device *dev)
9920{
9921 struct tg3 *tp = netdev_priv(dev);
Matt Carlson4f125f42009-09-01 12:55:02 +00009922 int i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009923
Matt Carlson9e9fd122009-01-19 16:57:45 -08009924 if (tp->fw_needed) {
9925 err = tg3_request_firmware(tp);
9926 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0) {
9927 if (err)
9928 return err;
9929 } else if (err) {
Joe Perches05dbe002010-02-17 19:44:19 +00009930 netdev_warn(tp->dev, "TSO capability disabled\n");
Joe Perches63c3a662011-04-26 08:12:10 +00009931 tg3_flag_clear(tp, TSO_CAPABLE);
9932 } else if (!tg3_flag(tp, TSO_CAPABLE)) {
Joe Perches05dbe002010-02-17 19:44:19 +00009933 netdev_notice(tp->dev, "TSO capability restored\n");
Joe Perches63c3a662011-04-26 08:12:10 +00009934 tg3_flag_set(tp, TSO_CAPABLE);
Matt Carlson9e9fd122009-01-19 16:57:45 -08009935 }
9936 }
9937
Michael Chanc49a1562006-12-17 17:07:29 -08009938 netif_carrier_off(tp->dev);
9939
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00009940 err = tg3_power_up(tp);
Matt Carlson2f751b62008-08-04 23:17:34 -07009941 if (err)
Michael Chanbc1c7562006-03-20 17:48:03 -08009942 return err;
Matt Carlson2f751b62008-08-04 23:17:34 -07009943
9944 tg3_full_lock(tp, 0);
Michael Chanbc1c7562006-03-20 17:48:03 -08009945
Linus Torvalds1da177e2005-04-16 15:20:36 -07009946 tg3_disable_ints(tp);
Joe Perches63c3a662011-04-26 08:12:10 +00009947 tg3_flag_clear(tp, INIT_COMPLETE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009948
David S. Millerf47c11e2005-06-24 20:18:35 -07009949 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009950
Matt Carlson679563f2009-09-01 12:55:46 +00009951 /*
9952 * Setup interrupts first so we know how
9953 * many NAPI resources to allocate
9954 */
9955 tg3_ints_init(tp);
9956
Matt Carlson90415472011-12-16 13:33:23 +00009957 tg3_rss_check_indir_tbl(tp);
Matt Carlsonbcebcc42011-12-14 11:10:01 +00009958
Linus Torvalds1da177e2005-04-16 15:20:36 -07009959 /* The placement of this call is tied
9960 * to the setup and use of Host TX descriptors.
9961 */
9962 err = tg3_alloc_consistent(tp);
9963 if (err)
Matt Carlson679563f2009-09-01 12:55:46 +00009964 goto err_out1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009965
Matt Carlson66cfd1b2010-09-30 10:34:30 +00009966 tg3_napi_init(tp);
9967
Matt Carlsonfed97812009-09-01 13:10:19 +00009968 tg3_napi_enable(tp);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07009969
Matt Carlson4f125f42009-09-01 12:55:02 +00009970 for (i = 0; i < tp->irq_cnt; i++) {
9971 struct tg3_napi *tnapi = &tp->napi[i];
9972 err = tg3_request_irq(tp, i);
9973 if (err) {
Matt Carlson5bc09182011-11-04 09:15:01 +00009974 for (i--; i >= 0; i--) {
9975 tnapi = &tp->napi[i];
Matt Carlson4f125f42009-09-01 12:55:02 +00009976 free_irq(tnapi->irq_vec, tnapi);
Matt Carlson5bc09182011-11-04 09:15:01 +00009977 }
9978 goto err_out2;
Matt Carlson4f125f42009-09-01 12:55:02 +00009979 }
9980 }
Matt Carlson07b01732009-08-28 14:01:15 +00009981
David S. Millerf47c11e2005-06-24 20:18:35 -07009982 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009983
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07009984 err = tg3_init_hw(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009985 if (err) {
Michael Chan944d9802005-05-29 14:57:48 -07009986 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009987 tg3_free_rings(tp);
9988 } else {
Matt Carlson0e6cf6a2011-06-13 13:38:55 +00009989 if (tg3_flag(tp, TAGGED_STATUS) &&
Matt Carlson55086ad2011-12-14 11:09:59 +00009990 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717 &&
9991 !tg3_flag(tp, 57765_CLASS))
David S. Millerfac9b832005-05-18 22:46:34 -07009992 tp->timer_offset = HZ;
9993 else
9994 tp->timer_offset = HZ / 10;
9995
9996 BUG_ON(tp->timer_offset > HZ);
9997 tp->timer_counter = tp->timer_multiplier =
9998 (HZ / tp->timer_offset);
9999 tp->asf_counter = tp->asf_multiplier =
Michael Chan28fbef72005-10-26 15:48:35 -070010000 ((HZ / tp->timer_offset) * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010001
10002 init_timer(&tp->timer);
10003 tp->timer.expires = jiffies + tp->timer_offset;
10004 tp->timer.data = (unsigned long) tp;
10005 tp->timer.function = tg3_timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010006 }
10007
David S. Millerf47c11e2005-06-24 20:18:35 -070010008 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010009
Matt Carlson07b01732009-08-28 14:01:15 +000010010 if (err)
Matt Carlson679563f2009-09-01 12:55:46 +000010011 goto err_out3;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010012
Joe Perches63c3a662011-04-26 08:12:10 +000010013 if (tg3_flag(tp, USING_MSI)) {
Michael Chan79381092005-04-21 17:13:59 -070010014 err = tg3_test_msi(tp);
David S. Millerfac9b832005-05-18 22:46:34 -070010015
Michael Chan79381092005-04-21 17:13:59 -070010016 if (err) {
David S. Millerf47c11e2005-06-24 20:18:35 -070010017 tg3_full_lock(tp, 0);
Michael Chan944d9802005-05-29 14:57:48 -070010018 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Michael Chan79381092005-04-21 17:13:59 -070010019 tg3_free_rings(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -070010020 tg3_full_unlock(tp);
Michael Chan79381092005-04-21 17:13:59 -070010021
Matt Carlson679563f2009-09-01 12:55:46 +000010022 goto err_out2;
Michael Chan79381092005-04-21 17:13:59 -070010023 }
Michael Chanfcfa0a32006-03-20 22:28:41 -080010024
Joe Perches63c3a662011-04-26 08:12:10 +000010025 if (!tg3_flag(tp, 57765_PLUS) && tg3_flag(tp, USING_MSI)) {
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000010026 u32 val = tr32(PCIE_TRANSACTION_CFG);
Michael Chanfcfa0a32006-03-20 22:28:41 -080010027
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000010028 tw32(PCIE_TRANSACTION_CFG,
10029 val | PCIE_TRANS_CFG_1SHOT_MSI);
Michael Chanfcfa0a32006-03-20 22:28:41 -080010030 }
Michael Chan79381092005-04-21 17:13:59 -070010031 }
10032
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010033 tg3_phy_start(tp);
10034
David S. Millerf47c11e2005-06-24 20:18:35 -070010035 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010036
Michael Chan79381092005-04-21 17:13:59 -070010037 add_timer(&tp->timer);
Joe Perches63c3a662011-04-26 08:12:10 +000010038 tg3_flag_set(tp, INIT_COMPLETE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010039 tg3_enable_ints(tp);
10040
David S. Millerf47c11e2005-06-24 20:18:35 -070010041 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010042
Matt Carlsonfe5f5782009-09-01 13:09:39 +000010043 netif_tx_start_all_queues(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010044
Mahesh Bandewar06c03c02011-05-08 06:51:48 +000010045 /*
10046 * Reset loopback feature if it was turned on while the device was down
10047 * make sure that it's installed properly now.
10048 */
10049 if (dev->features & NETIF_F_LOOPBACK)
10050 tg3_set_loopback(dev, dev->features);
10051
Linus Torvalds1da177e2005-04-16 15:20:36 -070010052 return 0;
Matt Carlson07b01732009-08-28 14:01:15 +000010053
Matt Carlson679563f2009-09-01 12:55:46 +000010054err_out3:
Matt Carlson4f125f42009-09-01 12:55:02 +000010055 for (i = tp->irq_cnt - 1; i >= 0; i--) {
10056 struct tg3_napi *tnapi = &tp->napi[i];
10057 free_irq(tnapi->irq_vec, tnapi);
10058 }
Matt Carlson07b01732009-08-28 14:01:15 +000010059
Matt Carlson679563f2009-09-01 12:55:46 +000010060err_out2:
Matt Carlsonfed97812009-09-01 13:10:19 +000010061 tg3_napi_disable(tp);
Matt Carlson66cfd1b2010-09-30 10:34:30 +000010062 tg3_napi_fini(tp);
Matt Carlson07b01732009-08-28 14:01:15 +000010063 tg3_free_consistent(tp);
Matt Carlson679563f2009-09-01 12:55:46 +000010064
10065err_out1:
10066 tg3_ints_fini(tp);
Matt Carlsoncd0d7222011-07-13 09:27:33 +000010067 tg3_frob_aux_power(tp, false);
10068 pci_set_power_state(tp->pdev, PCI_D3hot);
Matt Carlson07b01732009-08-28 14:01:15 +000010069 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010070}
10071
Linus Torvalds1da177e2005-04-16 15:20:36 -070010072static int tg3_close(struct net_device *dev)
10073{
Matt Carlson4f125f42009-09-01 12:55:02 +000010074 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010075 struct tg3 *tp = netdev_priv(dev);
10076
Matt Carlsonfed97812009-09-01 13:10:19 +000010077 tg3_napi_disable(tp);
Matt Carlsondb219972011-11-04 09:15:03 +000010078 tg3_reset_task_cancel(tp);
Michael Chan7faa0062006-02-02 17:29:28 -080010079
Matt Carlsonfe5f5782009-09-01 13:09:39 +000010080 netif_tx_stop_all_queues(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010081
10082 del_timer_sync(&tp->timer);
10083
Matt Carlson24bb4fb2009-10-05 17:55:29 +000010084 tg3_phy_stop(tp);
10085
David S. Millerf47c11e2005-06-24 20:18:35 -070010086 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010087
10088 tg3_disable_ints(tp);
10089
Michael Chan944d9802005-05-29 14:57:48 -070010090 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010091 tg3_free_rings(tp);
Joe Perches63c3a662011-04-26 08:12:10 +000010092 tg3_flag_clear(tp, INIT_COMPLETE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010093
David S. Millerf47c11e2005-06-24 20:18:35 -070010094 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010095
Matt Carlson4f125f42009-09-01 12:55:02 +000010096 for (i = tp->irq_cnt - 1; i >= 0; i--) {
10097 struct tg3_napi *tnapi = &tp->napi[i];
10098 free_irq(tnapi->irq_vec, tnapi);
10099 }
Matt Carlson07b01732009-08-28 14:01:15 +000010100
10101 tg3_ints_fini(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010102
Matt Carlson92feeab2011-12-08 14:40:14 +000010103 /* Clear stats across close / open calls */
10104 memset(&tp->net_stats_prev, 0, sizeof(tp->net_stats_prev));
10105 memset(&tp->estats_prev, 0, sizeof(tp->estats_prev));
Linus Torvalds1da177e2005-04-16 15:20:36 -070010106
Matt Carlson66cfd1b2010-09-30 10:34:30 +000010107 tg3_napi_fini(tp);
10108
Linus Torvalds1da177e2005-04-16 15:20:36 -070010109 tg3_free_consistent(tp);
10110
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000010111 tg3_power_down(tp);
Michael Chanbc1c7562006-03-20 17:48:03 -080010112
10113 netif_carrier_off(tp->dev);
10114
Linus Torvalds1da177e2005-04-16 15:20:36 -070010115 return 0;
10116}
10117
Eric Dumazet511d2222010-07-07 20:44:24 +000010118static inline u64 get_stat64(tg3_stat64_t *val)
Stefan Buehler816f8b82008-08-15 14:10:54 -070010119{
10120 return ((u64)val->high << 32) | ((u64)val->low);
10121}
10122
Eric Dumazet511d2222010-07-07 20:44:24 +000010123static u64 calc_crc_errors(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010124{
10125 struct tg3_hw_stats *hw_stats = tp->hw_stats;
10126
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010127 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070010128 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
10129 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010130 u32 val;
10131
David S. Millerf47c11e2005-06-24 20:18:35 -070010132 spin_lock_bh(&tp->lock);
Michael Chan569a5df2007-02-13 12:18:15 -080010133 if (!tg3_readphy(tp, MII_TG3_TEST1, &val)) {
10134 tg3_writephy(tp, MII_TG3_TEST1,
10135 val | MII_TG3_TEST1_CRC_EN);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +000010136 tg3_readphy(tp, MII_TG3_RXR_COUNTERS, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010137 } else
10138 val = 0;
David S. Millerf47c11e2005-06-24 20:18:35 -070010139 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010140
10141 tp->phy_crc_errors += val;
10142
10143 return tp->phy_crc_errors;
10144 }
10145
10146 return get_stat64(&hw_stats->rx_fcs_errors);
10147}
10148
10149#define ESTAT_ADD(member) \
10150 estats->member = old_estats->member + \
Eric Dumazet511d2222010-07-07 20:44:24 +000010151 get_stat64(&hw_stats->member)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010152
Matt Carlson0e6c9da2011-12-08 14:40:13 +000010153static struct tg3_ethtool_stats *tg3_get_estats(struct tg3 *tp,
10154 struct tg3_ethtool_stats *estats)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010155{
Linus Torvalds1da177e2005-04-16 15:20:36 -070010156 struct tg3_ethtool_stats *old_estats = &tp->estats_prev;
10157 struct tg3_hw_stats *hw_stats = tp->hw_stats;
10158
10159 if (!hw_stats)
10160 return old_estats;
10161
10162 ESTAT_ADD(rx_octets);
10163 ESTAT_ADD(rx_fragments);
10164 ESTAT_ADD(rx_ucast_packets);
10165 ESTAT_ADD(rx_mcast_packets);
10166 ESTAT_ADD(rx_bcast_packets);
10167 ESTAT_ADD(rx_fcs_errors);
10168 ESTAT_ADD(rx_align_errors);
10169 ESTAT_ADD(rx_xon_pause_rcvd);
10170 ESTAT_ADD(rx_xoff_pause_rcvd);
10171 ESTAT_ADD(rx_mac_ctrl_rcvd);
10172 ESTAT_ADD(rx_xoff_entered);
10173 ESTAT_ADD(rx_frame_too_long_errors);
10174 ESTAT_ADD(rx_jabbers);
10175 ESTAT_ADD(rx_undersize_packets);
10176 ESTAT_ADD(rx_in_length_errors);
10177 ESTAT_ADD(rx_out_length_errors);
10178 ESTAT_ADD(rx_64_or_less_octet_packets);
10179 ESTAT_ADD(rx_65_to_127_octet_packets);
10180 ESTAT_ADD(rx_128_to_255_octet_packets);
10181 ESTAT_ADD(rx_256_to_511_octet_packets);
10182 ESTAT_ADD(rx_512_to_1023_octet_packets);
10183 ESTAT_ADD(rx_1024_to_1522_octet_packets);
10184 ESTAT_ADD(rx_1523_to_2047_octet_packets);
10185 ESTAT_ADD(rx_2048_to_4095_octet_packets);
10186 ESTAT_ADD(rx_4096_to_8191_octet_packets);
10187 ESTAT_ADD(rx_8192_to_9022_octet_packets);
10188
10189 ESTAT_ADD(tx_octets);
10190 ESTAT_ADD(tx_collisions);
10191 ESTAT_ADD(tx_xon_sent);
10192 ESTAT_ADD(tx_xoff_sent);
10193 ESTAT_ADD(tx_flow_control);
10194 ESTAT_ADD(tx_mac_errors);
10195 ESTAT_ADD(tx_single_collisions);
10196 ESTAT_ADD(tx_mult_collisions);
10197 ESTAT_ADD(tx_deferred);
10198 ESTAT_ADD(tx_excessive_collisions);
10199 ESTAT_ADD(tx_late_collisions);
10200 ESTAT_ADD(tx_collide_2times);
10201 ESTAT_ADD(tx_collide_3times);
10202 ESTAT_ADD(tx_collide_4times);
10203 ESTAT_ADD(tx_collide_5times);
10204 ESTAT_ADD(tx_collide_6times);
10205 ESTAT_ADD(tx_collide_7times);
10206 ESTAT_ADD(tx_collide_8times);
10207 ESTAT_ADD(tx_collide_9times);
10208 ESTAT_ADD(tx_collide_10times);
10209 ESTAT_ADD(tx_collide_11times);
10210 ESTAT_ADD(tx_collide_12times);
10211 ESTAT_ADD(tx_collide_13times);
10212 ESTAT_ADD(tx_collide_14times);
10213 ESTAT_ADD(tx_collide_15times);
10214 ESTAT_ADD(tx_ucast_packets);
10215 ESTAT_ADD(tx_mcast_packets);
10216 ESTAT_ADD(tx_bcast_packets);
10217 ESTAT_ADD(tx_carrier_sense_errors);
10218 ESTAT_ADD(tx_discards);
10219 ESTAT_ADD(tx_errors);
10220
10221 ESTAT_ADD(dma_writeq_full);
10222 ESTAT_ADD(dma_write_prioq_full);
10223 ESTAT_ADD(rxbds_empty);
10224 ESTAT_ADD(rx_discards);
10225 ESTAT_ADD(rx_errors);
10226 ESTAT_ADD(rx_threshold_hit);
10227
10228 ESTAT_ADD(dma_readq_full);
10229 ESTAT_ADD(dma_read_prioq_full);
10230 ESTAT_ADD(tx_comp_queue_full);
10231
10232 ESTAT_ADD(ring_set_send_prod_index);
10233 ESTAT_ADD(ring_status_update);
10234 ESTAT_ADD(nic_irqs);
10235 ESTAT_ADD(nic_avoided_irqs);
10236 ESTAT_ADD(nic_tx_threshold_hit);
10237
Matt Carlson4452d092011-05-19 12:12:51 +000010238 ESTAT_ADD(mbuf_lwm_thresh_hit);
10239
Linus Torvalds1da177e2005-04-16 15:20:36 -070010240 return estats;
10241}
10242
Eric Dumazet511d2222010-07-07 20:44:24 +000010243static struct rtnl_link_stats64 *tg3_get_stats64(struct net_device *dev,
10244 struct rtnl_link_stats64 *stats)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010245{
10246 struct tg3 *tp = netdev_priv(dev);
Eric Dumazet511d2222010-07-07 20:44:24 +000010247 struct rtnl_link_stats64 *old_stats = &tp->net_stats_prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010248 struct tg3_hw_stats *hw_stats = tp->hw_stats;
10249
10250 if (!hw_stats)
10251 return old_stats;
10252
10253 stats->rx_packets = old_stats->rx_packets +
10254 get_stat64(&hw_stats->rx_ucast_packets) +
10255 get_stat64(&hw_stats->rx_mcast_packets) +
10256 get_stat64(&hw_stats->rx_bcast_packets);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010257
Linus Torvalds1da177e2005-04-16 15:20:36 -070010258 stats->tx_packets = old_stats->tx_packets +
10259 get_stat64(&hw_stats->tx_ucast_packets) +
10260 get_stat64(&hw_stats->tx_mcast_packets) +
10261 get_stat64(&hw_stats->tx_bcast_packets);
10262
10263 stats->rx_bytes = old_stats->rx_bytes +
10264 get_stat64(&hw_stats->rx_octets);
10265 stats->tx_bytes = old_stats->tx_bytes +
10266 get_stat64(&hw_stats->tx_octets);
10267
10268 stats->rx_errors = old_stats->rx_errors +
John W. Linville4f63b872005-09-12 14:43:18 -070010269 get_stat64(&hw_stats->rx_errors);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010270 stats->tx_errors = old_stats->tx_errors +
10271 get_stat64(&hw_stats->tx_errors) +
10272 get_stat64(&hw_stats->tx_mac_errors) +
10273 get_stat64(&hw_stats->tx_carrier_sense_errors) +
10274 get_stat64(&hw_stats->tx_discards);
10275
10276 stats->multicast = old_stats->multicast +
10277 get_stat64(&hw_stats->rx_mcast_packets);
10278 stats->collisions = old_stats->collisions +
10279 get_stat64(&hw_stats->tx_collisions);
10280
10281 stats->rx_length_errors = old_stats->rx_length_errors +
10282 get_stat64(&hw_stats->rx_frame_too_long_errors) +
10283 get_stat64(&hw_stats->rx_undersize_packets);
10284
10285 stats->rx_over_errors = old_stats->rx_over_errors +
10286 get_stat64(&hw_stats->rxbds_empty);
10287 stats->rx_frame_errors = old_stats->rx_frame_errors +
10288 get_stat64(&hw_stats->rx_align_errors);
10289 stats->tx_aborted_errors = old_stats->tx_aborted_errors +
10290 get_stat64(&hw_stats->tx_discards);
10291 stats->tx_carrier_errors = old_stats->tx_carrier_errors +
10292 get_stat64(&hw_stats->tx_carrier_sense_errors);
10293
10294 stats->rx_crc_errors = old_stats->rx_crc_errors +
10295 calc_crc_errors(tp);
10296
John W. Linville4f63b872005-09-12 14:43:18 -070010297 stats->rx_missed_errors = old_stats->rx_missed_errors +
10298 get_stat64(&hw_stats->rx_discards);
10299
Eric Dumazetb0057c52010-10-10 19:55:52 +000010300 stats->rx_dropped = tp->rx_dropped;
Eric Dumazet48855432011-10-24 07:53:03 +000010301 stats->tx_dropped = tp->tx_dropped;
Eric Dumazetb0057c52010-10-10 19:55:52 +000010302
Linus Torvalds1da177e2005-04-16 15:20:36 -070010303 return stats;
10304}
10305
Linus Torvalds1da177e2005-04-16 15:20:36 -070010306static int tg3_get_regs_len(struct net_device *dev)
10307{
Matt Carlson97bd8e42011-04-13 11:05:04 +000010308 return TG3_REG_BLK_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010309}
10310
10311static void tg3_get_regs(struct net_device *dev,
10312 struct ethtool_regs *regs, void *_p)
10313{
Linus Torvalds1da177e2005-04-16 15:20:36 -070010314 struct tg3 *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010315
10316 regs->version = 0;
10317
Matt Carlson97bd8e42011-04-13 11:05:04 +000010318 memset(_p, 0, TG3_REG_BLK_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010319
Matt Carlson800960682010-08-02 11:26:06 +000010320 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Michael Chanbc1c7562006-03-20 17:48:03 -080010321 return;
10322
David S. Millerf47c11e2005-06-24 20:18:35 -070010323 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010324
Matt Carlson97bd8e42011-04-13 11:05:04 +000010325 tg3_dump_legacy_regs(tp, (u32 *)_p);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010326
David S. Millerf47c11e2005-06-24 20:18:35 -070010327 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010328}
10329
10330static int tg3_get_eeprom_len(struct net_device *dev)
10331{
10332 struct tg3 *tp = netdev_priv(dev);
10333
10334 return tp->nvram_size;
10335}
10336
Linus Torvalds1da177e2005-04-16 15:20:36 -070010337static int tg3_get_eeprom(struct net_device *dev, struct ethtool_eeprom *eeprom, u8 *data)
10338{
10339 struct tg3 *tp = netdev_priv(dev);
10340 int ret;
10341 u8 *pd;
Al Virob9fc7dc2007-12-17 22:59:57 -080010342 u32 i, offset, len, b_offset, b_count;
Matt Carlsona9dc5292009-02-25 14:25:30 +000010343 __be32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010344
Joe Perches63c3a662011-04-26 08:12:10 +000010345 if (tg3_flag(tp, NO_NVRAM))
Matt Carlsondf259d82009-04-20 06:57:14 +000010346 return -EINVAL;
10347
Matt Carlson800960682010-08-02 11:26:06 +000010348 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Michael Chanbc1c7562006-03-20 17:48:03 -080010349 return -EAGAIN;
10350
Linus Torvalds1da177e2005-04-16 15:20:36 -070010351 offset = eeprom->offset;
10352 len = eeprom->len;
10353 eeprom->len = 0;
10354
10355 eeprom->magic = TG3_EEPROM_MAGIC;
10356
10357 if (offset & 3) {
10358 /* adjustments to start on required 4 byte boundary */
10359 b_offset = offset & 3;
10360 b_count = 4 - b_offset;
10361 if (b_count > len) {
10362 /* i.e. offset=1 len=2 */
10363 b_count = len;
10364 }
Matt Carlsona9dc5292009-02-25 14:25:30 +000010365 ret = tg3_nvram_read_be32(tp, offset-b_offset, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010366 if (ret)
10367 return ret;
Matt Carlsonbe98da62010-07-11 09:31:46 +000010368 memcpy(data, ((char *)&val) + b_offset, b_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010369 len -= b_count;
10370 offset += b_count;
Matt Carlsonc6cdf432010-04-05 10:19:26 +000010371 eeprom->len += b_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010372 }
10373
Lucas De Marchi25985ed2011-03-30 22:57:33 -030010374 /* read bytes up to the last 4 byte boundary */
Linus Torvalds1da177e2005-04-16 15:20:36 -070010375 pd = &data[eeprom->len];
10376 for (i = 0; i < (len - (len & 3)); i += 4) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000010377 ret = tg3_nvram_read_be32(tp, offset + i, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010378 if (ret) {
10379 eeprom->len += i;
10380 return ret;
10381 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010382 memcpy(pd + i, &val, 4);
10383 }
10384 eeprom->len += i;
10385
10386 if (len & 3) {
10387 /* read last bytes not ending on 4 byte boundary */
10388 pd = &data[eeprom->len];
10389 b_count = len & 3;
10390 b_offset = offset + len - b_count;
Matt Carlsona9dc5292009-02-25 14:25:30 +000010391 ret = tg3_nvram_read_be32(tp, b_offset, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010392 if (ret)
10393 return ret;
Al Virob9fc7dc2007-12-17 22:59:57 -080010394 memcpy(pd, &val, b_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010395 eeprom->len += b_count;
10396 }
10397 return 0;
10398}
10399
Linus Torvalds1da177e2005-04-16 15:20:36 -070010400static int tg3_set_eeprom(struct net_device *dev, struct ethtool_eeprom *eeprom, u8 *data)
10401{
10402 struct tg3 *tp = netdev_priv(dev);
10403 int ret;
Al Virob9fc7dc2007-12-17 22:59:57 -080010404 u32 offset, len, b_offset, odd_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010405 u8 *buf;
Matt Carlsona9dc5292009-02-25 14:25:30 +000010406 __be32 start, end;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010407
Matt Carlson800960682010-08-02 11:26:06 +000010408 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Michael Chanbc1c7562006-03-20 17:48:03 -080010409 return -EAGAIN;
10410
Joe Perches63c3a662011-04-26 08:12:10 +000010411 if (tg3_flag(tp, NO_NVRAM) ||
Matt Carlsondf259d82009-04-20 06:57:14 +000010412 eeprom->magic != TG3_EEPROM_MAGIC)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010413 return -EINVAL;
10414
10415 offset = eeprom->offset;
10416 len = eeprom->len;
10417
10418 if ((b_offset = (offset & 3))) {
10419 /* adjustments to start on required 4 byte boundary */
Matt Carlsona9dc5292009-02-25 14:25:30 +000010420 ret = tg3_nvram_read_be32(tp, offset-b_offset, &start);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010421 if (ret)
10422 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010423 len += b_offset;
10424 offset &= ~3;
Michael Chan1c8594b2005-04-21 17:12:46 -070010425 if (len < 4)
10426 len = 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010427 }
10428
10429 odd_len = 0;
Michael Chan1c8594b2005-04-21 17:12:46 -070010430 if (len & 3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010431 /* adjustments to end on required 4 byte boundary */
10432 odd_len = 1;
10433 len = (len + 3) & ~3;
Matt Carlsona9dc5292009-02-25 14:25:30 +000010434 ret = tg3_nvram_read_be32(tp, offset+len-4, &end);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010435 if (ret)
10436 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010437 }
10438
10439 buf = data;
10440 if (b_offset || odd_len) {
10441 buf = kmalloc(len, GFP_KERNEL);
Andy Gospodarekab0049b2007-09-06 20:42:14 +010010442 if (!buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010443 return -ENOMEM;
10444 if (b_offset)
10445 memcpy(buf, &start, 4);
10446 if (odd_len)
10447 memcpy(buf+len-4, &end, 4);
10448 memcpy(buf + b_offset, data, eeprom->len);
10449 }
10450
10451 ret = tg3_nvram_write_block(tp, offset, len, buf);
10452
10453 if (buf != data)
10454 kfree(buf);
10455
10456 return ret;
10457}
10458
10459static int tg3_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
10460{
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010461 struct tg3 *tp = netdev_priv(dev);
10462
Joe Perches63c3a662011-04-26 08:12:10 +000010463 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000010464 struct phy_device *phydev;
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010465 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010466 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000010467 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
10468 return phy_ethtool_gset(phydev, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010469 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010470
Linus Torvalds1da177e2005-04-16 15:20:36 -070010471 cmd->supported = (SUPPORTED_Autoneg);
10472
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010473 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010474 cmd->supported |= (SUPPORTED_1000baseT_Half |
10475 SUPPORTED_1000baseT_Full);
10476
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010477 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010478 cmd->supported |= (SUPPORTED_100baseT_Half |
10479 SUPPORTED_100baseT_Full |
10480 SUPPORTED_10baseT_Half |
10481 SUPPORTED_10baseT_Full |
Matt Carlson3bebab52007-11-12 21:22:40 -080010482 SUPPORTED_TP);
Karsten Keilef348142006-05-12 12:49:08 -070010483 cmd->port = PORT_TP;
10484 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010485 cmd->supported |= SUPPORTED_FIBRE;
Karsten Keilef348142006-05-12 12:49:08 -070010486 cmd->port = PORT_FIBRE;
10487 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010488
Linus Torvalds1da177e2005-04-16 15:20:36 -070010489 cmd->advertising = tp->link_config.advertising;
Matt Carlson5bb09772011-06-13 13:39:00 +000010490 if (tg3_flag(tp, PAUSE_AUTONEG)) {
10491 if (tp->link_config.flowctrl & FLOW_CTRL_RX) {
10492 if (tp->link_config.flowctrl & FLOW_CTRL_TX) {
10493 cmd->advertising |= ADVERTISED_Pause;
10494 } else {
10495 cmd->advertising |= ADVERTISED_Pause |
10496 ADVERTISED_Asym_Pause;
10497 }
10498 } else if (tp->link_config.flowctrl & FLOW_CTRL_TX) {
10499 cmd->advertising |= ADVERTISED_Asym_Pause;
10500 }
10501 }
Matt Carlson859edb22011-12-08 14:40:16 +000010502 if (netif_running(dev) && netif_carrier_ok(dev)) {
David Decotigny70739492011-04-27 18:32:40 +000010503 ethtool_cmd_speed_set(cmd, tp->link_config.active_speed);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010504 cmd->duplex = tp->link_config.active_duplex;
Matt Carlson859edb22011-12-08 14:40:16 +000010505 cmd->lp_advertising = tp->link_config.rmt_adv;
Matt Carlsone348c5e2011-11-21 15:01:20 +000010506 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES)) {
10507 if (tp->phy_flags & TG3_PHYFLG_MDIX_STATE)
10508 cmd->eth_tp_mdix = ETH_TP_MDI_X;
10509 else
10510 cmd->eth_tp_mdix = ETH_TP_MDI;
10511 }
Matt Carlson64c22182010-10-14 10:37:44 +000010512 } else {
David Decotigny70739492011-04-27 18:32:40 +000010513 ethtool_cmd_speed_set(cmd, SPEED_INVALID);
Matt Carlson64c22182010-10-14 10:37:44 +000010514 cmd->duplex = DUPLEX_INVALID;
Matt Carlsone348c5e2011-11-21 15:01:20 +000010515 cmd->eth_tp_mdix = ETH_TP_MDI_INVALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010516 }
Matt Carlson882e9792009-09-01 13:21:36 +000010517 cmd->phy_address = tp->phy_addr;
Matt Carlson7e5856b2009-02-25 14:23:01 +000010518 cmd->transceiver = XCVR_INTERNAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010519 cmd->autoneg = tp->link_config.autoneg;
10520 cmd->maxtxpkt = 0;
10521 cmd->maxrxpkt = 0;
10522 return 0;
10523}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010524
Linus Torvalds1da177e2005-04-16 15:20:36 -070010525static int tg3_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
10526{
10527 struct tg3 *tp = netdev_priv(dev);
David Decotigny25db0332011-04-27 18:32:39 +000010528 u32 speed = ethtool_cmd_speed(cmd);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010529
Joe Perches63c3a662011-04-26 08:12:10 +000010530 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000010531 struct phy_device *phydev;
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010532 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010533 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000010534 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
10535 return phy_ethtool_sset(phydev, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010536 }
10537
Matt Carlson7e5856b2009-02-25 14:23:01 +000010538 if (cmd->autoneg != AUTONEG_ENABLE &&
10539 cmd->autoneg != AUTONEG_DISABLE)
Michael Chan37ff2382005-10-26 15:49:51 -070010540 return -EINVAL;
Matt Carlson7e5856b2009-02-25 14:23:01 +000010541
10542 if (cmd->autoneg == AUTONEG_DISABLE &&
10543 cmd->duplex != DUPLEX_FULL &&
10544 cmd->duplex != DUPLEX_HALF)
Michael Chan37ff2382005-10-26 15:49:51 -070010545 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010546
Matt Carlson7e5856b2009-02-25 14:23:01 +000010547 if (cmd->autoneg == AUTONEG_ENABLE) {
10548 u32 mask = ADVERTISED_Autoneg |
10549 ADVERTISED_Pause |
10550 ADVERTISED_Asym_Pause;
10551
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010552 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
Matt Carlson7e5856b2009-02-25 14:23:01 +000010553 mask |= ADVERTISED_1000baseT_Half |
10554 ADVERTISED_1000baseT_Full;
10555
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010556 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
Matt Carlson7e5856b2009-02-25 14:23:01 +000010557 mask |= ADVERTISED_100baseT_Half |
10558 ADVERTISED_100baseT_Full |
10559 ADVERTISED_10baseT_Half |
10560 ADVERTISED_10baseT_Full |
10561 ADVERTISED_TP;
10562 else
10563 mask |= ADVERTISED_FIBRE;
10564
10565 if (cmd->advertising & ~mask)
10566 return -EINVAL;
10567
10568 mask &= (ADVERTISED_1000baseT_Half |
10569 ADVERTISED_1000baseT_Full |
10570 ADVERTISED_100baseT_Half |
10571 ADVERTISED_100baseT_Full |
10572 ADVERTISED_10baseT_Half |
10573 ADVERTISED_10baseT_Full);
10574
10575 cmd->advertising &= mask;
10576 } else {
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010577 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES) {
David Decotigny25db0332011-04-27 18:32:39 +000010578 if (speed != SPEED_1000)
Matt Carlson7e5856b2009-02-25 14:23:01 +000010579 return -EINVAL;
10580
10581 if (cmd->duplex != DUPLEX_FULL)
10582 return -EINVAL;
10583 } else {
David Decotigny25db0332011-04-27 18:32:39 +000010584 if (speed != SPEED_100 &&
10585 speed != SPEED_10)
Matt Carlson7e5856b2009-02-25 14:23:01 +000010586 return -EINVAL;
10587 }
10588 }
10589
David S. Millerf47c11e2005-06-24 20:18:35 -070010590 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010591
10592 tp->link_config.autoneg = cmd->autoneg;
10593 if (cmd->autoneg == AUTONEG_ENABLE) {
Andy Gospodarek405d8e52007-10-08 01:08:47 -070010594 tp->link_config.advertising = (cmd->advertising |
10595 ADVERTISED_Autoneg);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010596 tp->link_config.speed = SPEED_INVALID;
10597 tp->link_config.duplex = DUPLEX_INVALID;
10598 } else {
10599 tp->link_config.advertising = 0;
David Decotigny25db0332011-04-27 18:32:39 +000010600 tp->link_config.speed = speed;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010601 tp->link_config.duplex = cmd->duplex;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010602 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010603
Michael Chan24fcad62006-12-17 17:06:46 -080010604 tp->link_config.orig_speed = tp->link_config.speed;
10605 tp->link_config.orig_duplex = tp->link_config.duplex;
10606 tp->link_config.orig_autoneg = tp->link_config.autoneg;
10607
Linus Torvalds1da177e2005-04-16 15:20:36 -070010608 if (netif_running(dev))
10609 tg3_setup_phy(tp, 1);
10610
David S. Millerf47c11e2005-06-24 20:18:35 -070010611 tg3_full_unlock(tp);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010612
Linus Torvalds1da177e2005-04-16 15:20:36 -070010613 return 0;
10614}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010615
Linus Torvalds1da177e2005-04-16 15:20:36 -070010616static void tg3_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
10617{
10618 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010619
Rick Jones68aad782011-11-07 13:29:27 +000010620 strlcpy(info->driver, DRV_MODULE_NAME, sizeof(info->driver));
10621 strlcpy(info->version, DRV_MODULE_VERSION, sizeof(info->version));
10622 strlcpy(info->fw_version, tp->fw_ver, sizeof(info->fw_version));
10623 strlcpy(info->bus_info, pci_name(tp->pdev), sizeof(info->bus_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -070010624}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010625
Linus Torvalds1da177e2005-04-16 15:20:36 -070010626static void tg3_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
10627{
10628 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010629
Joe Perches63c3a662011-04-26 08:12:10 +000010630 if (tg3_flag(tp, WOL_CAP) && device_can_wakeup(&tp->pdev->dev))
Gary Zambranoa85feb82007-05-05 11:52:19 -070010631 wol->supported = WAKE_MAGIC;
10632 else
10633 wol->supported = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010634 wol->wolopts = 0;
Joe Perches63c3a662011-04-26 08:12:10 +000010635 if (tg3_flag(tp, WOL_ENABLE) && device_can_wakeup(&tp->pdev->dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010636 wol->wolopts = WAKE_MAGIC;
10637 memset(&wol->sopass, 0, sizeof(wol->sopass));
10638}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010639
Linus Torvalds1da177e2005-04-16 15:20:36 -070010640static int tg3_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
10641{
10642 struct tg3 *tp = netdev_priv(dev);
Rafael J. Wysocki12dac072008-07-30 16:37:33 -070010643 struct device *dp = &tp->pdev->dev;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010644
Linus Torvalds1da177e2005-04-16 15:20:36 -070010645 if (wol->wolopts & ~WAKE_MAGIC)
10646 return -EINVAL;
10647 if ((wol->wolopts & WAKE_MAGIC) &&
Joe Perches63c3a662011-04-26 08:12:10 +000010648 !(tg3_flag(tp, WOL_CAP) && device_can_wakeup(dp)))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010649 return -EINVAL;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010650
Rafael J. Wysockif2dc0d12010-10-25 13:01:55 +000010651 device_set_wakeup_enable(dp, wol->wolopts & WAKE_MAGIC);
10652
David S. Millerf47c11e2005-06-24 20:18:35 -070010653 spin_lock_bh(&tp->lock);
Rafael J. Wysockif2dc0d12010-10-25 13:01:55 +000010654 if (device_may_wakeup(dp))
Joe Perches63c3a662011-04-26 08:12:10 +000010655 tg3_flag_set(tp, WOL_ENABLE);
Rafael J. Wysockif2dc0d12010-10-25 13:01:55 +000010656 else
Joe Perches63c3a662011-04-26 08:12:10 +000010657 tg3_flag_clear(tp, WOL_ENABLE);
David S. Millerf47c11e2005-06-24 20:18:35 -070010658 spin_unlock_bh(&tp->lock);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010659
Linus Torvalds1da177e2005-04-16 15:20:36 -070010660 return 0;
10661}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010662
Linus Torvalds1da177e2005-04-16 15:20:36 -070010663static u32 tg3_get_msglevel(struct net_device *dev)
10664{
10665 struct tg3 *tp = netdev_priv(dev);
10666 return tp->msg_enable;
10667}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010668
Linus Torvalds1da177e2005-04-16 15:20:36 -070010669static void tg3_set_msglevel(struct net_device *dev, u32 value)
10670{
10671 struct tg3 *tp = netdev_priv(dev);
10672 tp->msg_enable = value;
10673}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010674
Linus Torvalds1da177e2005-04-16 15:20:36 -070010675static int tg3_nway_reset(struct net_device *dev)
10676{
10677 struct tg3 *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010678 int r;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010679
Linus Torvalds1da177e2005-04-16 15:20:36 -070010680 if (!netif_running(dev))
10681 return -EAGAIN;
10682
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010683 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Michael Chanc94e3942005-09-27 12:12:42 -070010684 return -EINVAL;
10685
Joe Perches63c3a662011-04-26 08:12:10 +000010686 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010687 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010688 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000010689 r = phy_start_aneg(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010690 } else {
10691 u32 bmcr;
10692
10693 spin_lock_bh(&tp->lock);
10694 r = -EINVAL;
10695 tg3_readphy(tp, MII_BMCR, &bmcr);
10696 if (!tg3_readphy(tp, MII_BMCR, &bmcr) &&
10697 ((bmcr & BMCR_ANENABLE) ||
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010698 (tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT))) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010699 tg3_writephy(tp, MII_BMCR, bmcr | BMCR_ANRESTART |
10700 BMCR_ANENABLE);
10701 r = 0;
10702 }
10703 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010704 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010705
Linus Torvalds1da177e2005-04-16 15:20:36 -070010706 return r;
10707}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010708
Linus Torvalds1da177e2005-04-16 15:20:36 -070010709static void tg3_get_ringparam(struct net_device *dev, struct ethtool_ringparam *ering)
10710{
10711 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010712
Matt Carlson2c49a442010-09-30 10:34:35 +000010713 ering->rx_max_pending = tp->rx_std_ring_mask;
Joe Perches63c3a662011-04-26 08:12:10 +000010714 if (tg3_flag(tp, JUMBO_RING_ENABLE))
Matt Carlson2c49a442010-09-30 10:34:35 +000010715 ering->rx_jumbo_max_pending = tp->rx_jmb_ring_mask;
Michael Chan4f81c322006-03-20 21:33:42 -080010716 else
10717 ering->rx_jumbo_max_pending = 0;
10718
10719 ering->tx_max_pending = TG3_TX_RING_SIZE - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010720
10721 ering->rx_pending = tp->rx_pending;
Joe Perches63c3a662011-04-26 08:12:10 +000010722 if (tg3_flag(tp, JUMBO_RING_ENABLE))
Michael Chan4f81c322006-03-20 21:33:42 -080010723 ering->rx_jumbo_pending = tp->rx_jumbo_pending;
10724 else
10725 ering->rx_jumbo_pending = 0;
10726
Matt Carlsonf3f3f272009-08-28 14:03:21 +000010727 ering->tx_pending = tp->napi[0].tx_pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010728}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010729
Linus Torvalds1da177e2005-04-16 15:20:36 -070010730static int tg3_set_ringparam(struct net_device *dev, struct ethtool_ringparam *ering)
10731{
10732 struct tg3 *tp = netdev_priv(dev);
Matt Carlson646c9ed2009-09-01 12:58:41 +000010733 int i, irq_sync = 0, err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010734
Matt Carlson2c49a442010-09-30 10:34:35 +000010735 if ((ering->rx_pending > tp->rx_std_ring_mask) ||
10736 (ering->rx_jumbo_pending > tp->rx_jmb_ring_mask) ||
Michael Chanbc3a9252006-10-18 20:55:18 -070010737 (ering->tx_pending > TG3_TX_RING_SIZE - 1) ||
10738 (ering->tx_pending <= MAX_SKB_FRAGS) ||
Joe Perches63c3a662011-04-26 08:12:10 +000010739 (tg3_flag(tp, TSO_BUG) &&
Michael Chanbc3a9252006-10-18 20:55:18 -070010740 (ering->tx_pending <= (MAX_SKB_FRAGS * 3))))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010741 return -EINVAL;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010742
Michael Chanbbe832c2005-06-24 20:20:04 -070010743 if (netif_running(dev)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010744 tg3_phy_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010745 tg3_netif_stop(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -070010746 irq_sync = 1;
10747 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010748
Michael Chanbbe832c2005-06-24 20:20:04 -070010749 tg3_full_lock(tp, irq_sync);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010750
Linus Torvalds1da177e2005-04-16 15:20:36 -070010751 tp->rx_pending = ering->rx_pending;
10752
Joe Perches63c3a662011-04-26 08:12:10 +000010753 if (tg3_flag(tp, MAX_RXPEND_64) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070010754 tp->rx_pending > 63)
10755 tp->rx_pending = 63;
10756 tp->rx_jumbo_pending = ering->rx_jumbo_pending;
Matt Carlson646c9ed2009-09-01 12:58:41 +000010757
Matt Carlson6fd45cb2010-09-15 08:59:57 +000010758 for (i = 0; i < tp->irq_max; i++)
Matt Carlson646c9ed2009-09-01 12:58:41 +000010759 tp->napi[i].tx_pending = ering->tx_pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010760
10761 if (netif_running(dev)) {
Michael Chan944d9802005-05-29 14:57:48 -070010762 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Michael Chanb9ec6c12006-07-25 16:37:27 -070010763 err = tg3_restart_hw(tp, 1);
10764 if (!err)
10765 tg3_netif_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010766 }
10767
David S. Millerf47c11e2005-06-24 20:18:35 -070010768 tg3_full_unlock(tp);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010769
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010770 if (irq_sync && !err)
10771 tg3_phy_start(tp);
10772
Michael Chanb9ec6c12006-07-25 16:37:27 -070010773 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010774}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010775
Linus Torvalds1da177e2005-04-16 15:20:36 -070010776static void tg3_get_pauseparam(struct net_device *dev, struct ethtool_pauseparam *epause)
10777{
10778 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010779
Joe Perches63c3a662011-04-26 08:12:10 +000010780 epause->autoneg = !!tg3_flag(tp, PAUSE_AUTONEG);
Matt Carlson8d018622007-12-20 20:05:44 -080010781
Matt Carlson4a2db502011-12-08 14:40:17 +000010782 if (tp->link_config.flowctrl & FLOW_CTRL_RX)
Matt Carlson8d018622007-12-20 20:05:44 -080010783 epause->rx_pause = 1;
10784 else
10785 epause->rx_pause = 0;
10786
Matt Carlson4a2db502011-12-08 14:40:17 +000010787 if (tp->link_config.flowctrl & FLOW_CTRL_TX)
Matt Carlson8d018622007-12-20 20:05:44 -080010788 epause->tx_pause = 1;
10789 else
10790 epause->tx_pause = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010791}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010792
Linus Torvalds1da177e2005-04-16 15:20:36 -070010793static int tg3_set_pauseparam(struct net_device *dev, struct ethtool_pauseparam *epause)
10794{
10795 struct tg3 *tp = netdev_priv(dev);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010796 int err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010797
Joe Perches63c3a662011-04-26 08:12:10 +000010798 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson27121682010-02-17 15:16:57 +000010799 u32 newadv;
10800 struct phy_device *phydev;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010801
Matt Carlson27121682010-02-17 15:16:57 +000010802 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010803
Matt Carlson27121682010-02-17 15:16:57 +000010804 if (!(phydev->supported & SUPPORTED_Pause) ||
10805 (!(phydev->supported & SUPPORTED_Asym_Pause) &&
Nicolas Kaiser2259dca2010-10-07 23:29:27 +000010806 (epause->rx_pause != epause->tx_pause)))
Matt Carlson27121682010-02-17 15:16:57 +000010807 return -EINVAL;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010808
Matt Carlson27121682010-02-17 15:16:57 +000010809 tp->link_config.flowctrl = 0;
10810 if (epause->rx_pause) {
10811 tp->link_config.flowctrl |= FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010812
Matt Carlson27121682010-02-17 15:16:57 +000010813 if (epause->tx_pause) {
Steve Glendinninge18ce342008-12-16 02:00:00 -080010814 tp->link_config.flowctrl |= FLOW_CTRL_TX;
Matt Carlson27121682010-02-17 15:16:57 +000010815 newadv = ADVERTISED_Pause;
10816 } else
10817 newadv = ADVERTISED_Pause |
10818 ADVERTISED_Asym_Pause;
10819 } else if (epause->tx_pause) {
10820 tp->link_config.flowctrl |= FLOW_CTRL_TX;
10821 newadv = ADVERTISED_Asym_Pause;
10822 } else
10823 newadv = 0;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010824
Matt Carlson27121682010-02-17 15:16:57 +000010825 if (epause->autoneg)
Joe Perches63c3a662011-04-26 08:12:10 +000010826 tg3_flag_set(tp, PAUSE_AUTONEG);
Matt Carlson27121682010-02-17 15:16:57 +000010827 else
Joe Perches63c3a662011-04-26 08:12:10 +000010828 tg3_flag_clear(tp, PAUSE_AUTONEG);
Matt Carlson27121682010-02-17 15:16:57 +000010829
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010830 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) {
Matt Carlson27121682010-02-17 15:16:57 +000010831 u32 oldadv = phydev->advertising &
10832 (ADVERTISED_Pause | ADVERTISED_Asym_Pause);
10833 if (oldadv != newadv) {
10834 phydev->advertising &=
10835 ~(ADVERTISED_Pause |
10836 ADVERTISED_Asym_Pause);
10837 phydev->advertising |= newadv;
10838 if (phydev->autoneg) {
10839 /*
10840 * Always renegotiate the link to
10841 * inform our link partner of our
10842 * flow control settings, even if the
10843 * flow control is forced. Let
10844 * tg3_adjust_link() do the final
10845 * flow control setup.
10846 */
10847 return phy_start_aneg(phydev);
10848 }
10849 }
10850
10851 if (!epause->autoneg)
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010852 tg3_setup_flow_control(tp, 0, 0);
Matt Carlson27121682010-02-17 15:16:57 +000010853 } else {
10854 tp->link_config.orig_advertising &=
10855 ~(ADVERTISED_Pause |
10856 ADVERTISED_Asym_Pause);
10857 tp->link_config.orig_advertising |= newadv;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010858 }
10859 } else {
10860 int irq_sync = 0;
10861
10862 if (netif_running(dev)) {
10863 tg3_netif_stop(tp);
10864 irq_sync = 1;
10865 }
10866
10867 tg3_full_lock(tp, irq_sync);
10868
10869 if (epause->autoneg)
Joe Perches63c3a662011-04-26 08:12:10 +000010870 tg3_flag_set(tp, PAUSE_AUTONEG);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010871 else
Joe Perches63c3a662011-04-26 08:12:10 +000010872 tg3_flag_clear(tp, PAUSE_AUTONEG);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010873 if (epause->rx_pause)
Steve Glendinninge18ce342008-12-16 02:00:00 -080010874 tp->link_config.flowctrl |= FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010875 else
Steve Glendinninge18ce342008-12-16 02:00:00 -080010876 tp->link_config.flowctrl &= ~FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010877 if (epause->tx_pause)
Steve Glendinninge18ce342008-12-16 02:00:00 -080010878 tp->link_config.flowctrl |= FLOW_CTRL_TX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010879 else
Steve Glendinninge18ce342008-12-16 02:00:00 -080010880 tp->link_config.flowctrl &= ~FLOW_CTRL_TX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010881
10882 if (netif_running(dev)) {
10883 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
10884 err = tg3_restart_hw(tp, 1);
10885 if (!err)
10886 tg3_netif_start(tp);
10887 }
10888
10889 tg3_full_unlock(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -070010890 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010891
Michael Chanb9ec6c12006-07-25 16:37:27 -070010892 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010893}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010894
Matt Carlsonde6f31e2010-04-12 06:58:30 +000010895static int tg3_get_sset_count(struct net_device *dev, int sset)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010896{
Jeff Garzikb9f2c042007-10-03 18:07:32 -070010897 switch (sset) {
10898 case ETH_SS_TEST:
10899 return TG3_NUM_TEST;
10900 case ETH_SS_STATS:
10901 return TG3_NUM_STATS;
10902 default:
10903 return -EOPNOTSUPP;
10904 }
Michael Chan4cafd3f2005-05-29 14:56:34 -070010905}
10906
Matt Carlson90415472011-12-16 13:33:23 +000010907static int tg3_get_rxnfc(struct net_device *dev, struct ethtool_rxnfc *info,
10908 u32 *rules __always_unused)
10909{
10910 struct tg3 *tp = netdev_priv(dev);
10911
10912 if (!tg3_flag(tp, SUPPORT_MSIX))
10913 return -EOPNOTSUPP;
10914
10915 switch (info->cmd) {
10916 case ETHTOOL_GRXRINGS:
10917 if (netif_running(tp->dev))
10918 info->data = tp->irq_cnt;
10919 else {
10920 info->data = num_online_cpus();
10921 if (info->data > TG3_IRQ_MAX_VECS_RSS)
10922 info->data = TG3_IRQ_MAX_VECS_RSS;
10923 }
10924
10925 /* The first interrupt vector only
10926 * handles link interrupts.
10927 */
10928 info->data -= 1;
10929 return 0;
10930
10931 default:
10932 return -EOPNOTSUPP;
10933 }
10934}
10935
10936static u32 tg3_get_rxfh_indir_size(struct net_device *dev)
10937{
10938 u32 size = 0;
10939 struct tg3 *tp = netdev_priv(dev);
10940
10941 if (tg3_flag(tp, SUPPORT_MSIX))
10942 size = TG3_RSS_INDIR_TBL_SIZE;
10943
10944 return size;
10945}
10946
10947static int tg3_get_rxfh_indir(struct net_device *dev, u32 *indir)
10948{
10949 struct tg3 *tp = netdev_priv(dev);
10950 int i;
10951
10952 for (i = 0; i < TG3_RSS_INDIR_TBL_SIZE; i++)
10953 indir[i] = tp->rss_ind_tbl[i];
10954
10955 return 0;
10956}
10957
10958static int tg3_set_rxfh_indir(struct net_device *dev, const u32 *indir)
10959{
10960 struct tg3 *tp = netdev_priv(dev);
10961 size_t i;
10962
10963 for (i = 0; i < TG3_RSS_INDIR_TBL_SIZE; i++)
10964 tp->rss_ind_tbl[i] = indir[i];
10965
10966 if (!netif_running(dev) || !tg3_flag(tp, ENABLE_RSS))
10967 return 0;
10968
10969 /* It is legal to write the indirection
10970 * table while the device is running.
10971 */
10972 tg3_full_lock(tp, 0);
10973 tg3_rss_write_indir_tbl(tp);
10974 tg3_full_unlock(tp);
10975
10976 return 0;
10977}
10978
Matt Carlsonde6f31e2010-04-12 06:58:30 +000010979static void tg3_get_strings(struct net_device *dev, u32 stringset, u8 *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010980{
10981 switch (stringset) {
10982 case ETH_SS_STATS:
10983 memcpy(buf, &ethtool_stats_keys, sizeof(ethtool_stats_keys));
10984 break;
Michael Chan4cafd3f2005-05-29 14:56:34 -070010985 case ETH_SS_TEST:
10986 memcpy(buf, &ethtool_test_keys, sizeof(ethtool_test_keys));
10987 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010988 default:
10989 WARN_ON(1); /* we need a WARN() */
10990 break;
10991 }
10992}
10993
stephen hemminger81b87092011-04-04 08:43:50 +000010994static int tg3_set_phys_id(struct net_device *dev,
10995 enum ethtool_phys_id_state state)
Michael Chan4009a932005-09-05 17:52:54 -070010996{
10997 struct tg3 *tp = netdev_priv(dev);
Michael Chan4009a932005-09-05 17:52:54 -070010998
10999 if (!netif_running(tp->dev))
11000 return -EAGAIN;
11001
stephen hemminger81b87092011-04-04 08:43:50 +000011002 switch (state) {
11003 case ETHTOOL_ID_ACTIVE:
Allan, Bruce Wfce55922011-04-13 13:09:10 +000011004 return 1; /* cycle on/off once per second */
Michael Chan4009a932005-09-05 17:52:54 -070011005
stephen hemminger81b87092011-04-04 08:43:50 +000011006 case ETHTOOL_ID_ON:
11007 tw32(MAC_LED_CTRL, LED_CTRL_LNKLED_OVERRIDE |
11008 LED_CTRL_1000MBPS_ON |
11009 LED_CTRL_100MBPS_ON |
11010 LED_CTRL_10MBPS_ON |
11011 LED_CTRL_TRAFFIC_OVERRIDE |
11012 LED_CTRL_TRAFFIC_BLINK |
11013 LED_CTRL_TRAFFIC_LED);
11014 break;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011015
stephen hemminger81b87092011-04-04 08:43:50 +000011016 case ETHTOOL_ID_OFF:
11017 tw32(MAC_LED_CTRL, LED_CTRL_LNKLED_OVERRIDE |
11018 LED_CTRL_TRAFFIC_OVERRIDE);
11019 break;
Michael Chan4009a932005-09-05 17:52:54 -070011020
stephen hemminger81b87092011-04-04 08:43:50 +000011021 case ETHTOOL_ID_INACTIVE:
11022 tw32(MAC_LED_CTRL, tp->led_ctrl);
11023 break;
Michael Chan4009a932005-09-05 17:52:54 -070011024 }
stephen hemminger81b87092011-04-04 08:43:50 +000011025
Michael Chan4009a932005-09-05 17:52:54 -070011026 return 0;
11027}
11028
Matt Carlsonde6f31e2010-04-12 06:58:30 +000011029static void tg3_get_ethtool_stats(struct net_device *dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -070011030 struct ethtool_stats *estats, u64 *tmp_stats)
11031{
11032 struct tg3 *tp = netdev_priv(dev);
Matt Carlson0e6c9da2011-12-08 14:40:13 +000011033
11034 tg3_get_estats(tp, (struct tg3_ethtool_stats *)tmp_stats);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011035}
11036
Matt Carlson535a4902011-07-20 10:20:56 +000011037static __be32 *tg3_vpd_readblock(struct tg3 *tp, u32 *vpdlen)
Matt Carlsonc3e94502011-04-13 11:05:08 +000011038{
11039 int i;
11040 __be32 *buf;
11041 u32 offset = 0, len = 0;
11042 u32 magic, val;
11043
Joe Perches63c3a662011-04-26 08:12:10 +000011044 if (tg3_flag(tp, NO_NVRAM) || tg3_nvram_read(tp, 0, &magic))
Matt Carlsonc3e94502011-04-13 11:05:08 +000011045 return NULL;
11046
11047 if (magic == TG3_EEPROM_MAGIC) {
11048 for (offset = TG3_NVM_DIR_START;
11049 offset < TG3_NVM_DIR_END;
11050 offset += TG3_NVM_DIRENT_SIZE) {
11051 if (tg3_nvram_read(tp, offset, &val))
11052 return NULL;
11053
11054 if ((val >> TG3_NVM_DIRTYPE_SHIFT) ==
11055 TG3_NVM_DIRTYPE_EXTVPD)
11056 break;
11057 }
11058
11059 if (offset != TG3_NVM_DIR_END) {
11060 len = (val & TG3_NVM_DIRTYPE_LENMSK) * 4;
11061 if (tg3_nvram_read(tp, offset + 4, &offset))
11062 return NULL;
11063
11064 offset = tg3_nvram_logical_addr(tp, offset);
11065 }
11066 }
11067
11068 if (!offset || !len) {
11069 offset = TG3_NVM_VPD_OFF;
11070 len = TG3_NVM_VPD_LEN;
11071 }
11072
11073 buf = kmalloc(len, GFP_KERNEL);
11074 if (buf == NULL)
11075 return NULL;
11076
11077 if (magic == TG3_EEPROM_MAGIC) {
11078 for (i = 0; i < len; i += 4) {
11079 /* The data is in little-endian format in NVRAM.
11080 * Use the big-endian read routines to preserve
11081 * the byte order as it exists in NVRAM.
11082 */
11083 if (tg3_nvram_read_be32(tp, offset + i, &buf[i/4]))
11084 goto error;
11085 }
11086 } else {
11087 u8 *ptr;
11088 ssize_t cnt;
11089 unsigned int pos = 0;
11090
11091 ptr = (u8 *)&buf[0];
11092 for (i = 0; pos < len && i < 3; i++, pos += cnt, ptr += cnt) {
11093 cnt = pci_read_vpd(tp->pdev, pos,
11094 len - pos, ptr);
11095 if (cnt == -ETIMEDOUT || cnt == -EINTR)
11096 cnt = 0;
11097 else if (cnt < 0)
11098 goto error;
11099 }
11100 if (pos != len)
11101 goto error;
11102 }
11103
Matt Carlson535a4902011-07-20 10:20:56 +000011104 *vpdlen = len;
11105
Matt Carlsonc3e94502011-04-13 11:05:08 +000011106 return buf;
11107
11108error:
11109 kfree(buf);
11110 return NULL;
11111}
11112
Michael Chan566f86a2005-05-29 14:56:58 -070011113#define NVRAM_TEST_SIZE 0x100
Matt Carlsona5767de2007-11-12 21:10:58 -080011114#define NVRAM_SELFBOOT_FORMAT1_0_SIZE 0x14
11115#define NVRAM_SELFBOOT_FORMAT1_2_SIZE 0x18
11116#define NVRAM_SELFBOOT_FORMAT1_3_SIZE 0x1c
Matt Carlson727a6d92011-06-13 13:38:58 +000011117#define NVRAM_SELFBOOT_FORMAT1_4_SIZE 0x20
11118#define NVRAM_SELFBOOT_FORMAT1_5_SIZE 0x24
Matt Carlsonbda18fa2011-07-20 10:20:57 +000011119#define NVRAM_SELFBOOT_FORMAT1_6_SIZE 0x50
Michael Chanb16250e2006-09-27 16:10:14 -070011120#define NVRAM_SELFBOOT_HW_SIZE 0x20
11121#define NVRAM_SELFBOOT_DATA_SIZE 0x1c
Michael Chan566f86a2005-05-29 14:56:58 -070011122
11123static int tg3_test_nvram(struct tg3 *tp)
11124{
Matt Carlson535a4902011-07-20 10:20:56 +000011125 u32 csum, magic, len;
Matt Carlsona9dc5292009-02-25 14:25:30 +000011126 __be32 *buf;
Andy Gospodarekab0049b2007-09-06 20:42:14 +010011127 int i, j, k, err = 0, size;
Michael Chan566f86a2005-05-29 14:56:58 -070011128
Joe Perches63c3a662011-04-26 08:12:10 +000011129 if (tg3_flag(tp, NO_NVRAM))
Matt Carlsondf259d82009-04-20 06:57:14 +000011130 return 0;
11131
Matt Carlsone4f34112009-02-25 14:25:00 +000011132 if (tg3_nvram_read(tp, 0, &magic) != 0)
Michael Chan1b277772006-03-20 22:27:48 -080011133 return -EIO;
11134
Michael Chan1b277772006-03-20 22:27:48 -080011135 if (magic == TG3_EEPROM_MAGIC)
11136 size = NVRAM_TEST_SIZE;
Michael Chanb16250e2006-09-27 16:10:14 -070011137 else if ((magic & TG3_EEPROM_MAGIC_FW_MSK) == TG3_EEPROM_MAGIC_FW) {
Matt Carlsona5767de2007-11-12 21:10:58 -080011138 if ((magic & TG3_EEPROM_SB_FORMAT_MASK) ==
11139 TG3_EEPROM_SB_FORMAT_1) {
11140 switch (magic & TG3_EEPROM_SB_REVISION_MASK) {
11141 case TG3_EEPROM_SB_REVISION_0:
11142 size = NVRAM_SELFBOOT_FORMAT1_0_SIZE;
11143 break;
11144 case TG3_EEPROM_SB_REVISION_2:
11145 size = NVRAM_SELFBOOT_FORMAT1_2_SIZE;
11146 break;
11147 case TG3_EEPROM_SB_REVISION_3:
11148 size = NVRAM_SELFBOOT_FORMAT1_3_SIZE;
11149 break;
Matt Carlson727a6d92011-06-13 13:38:58 +000011150 case TG3_EEPROM_SB_REVISION_4:
11151 size = NVRAM_SELFBOOT_FORMAT1_4_SIZE;
11152 break;
11153 case TG3_EEPROM_SB_REVISION_5:
11154 size = NVRAM_SELFBOOT_FORMAT1_5_SIZE;
11155 break;
11156 case TG3_EEPROM_SB_REVISION_6:
11157 size = NVRAM_SELFBOOT_FORMAT1_6_SIZE;
11158 break;
Matt Carlsona5767de2007-11-12 21:10:58 -080011159 default:
Matt Carlson727a6d92011-06-13 13:38:58 +000011160 return -EIO;
Matt Carlsona5767de2007-11-12 21:10:58 -080011161 }
11162 } else
Michael Chan1b277772006-03-20 22:27:48 -080011163 return 0;
Michael Chanb16250e2006-09-27 16:10:14 -070011164 } else if ((magic & TG3_EEPROM_MAGIC_HW_MSK) == TG3_EEPROM_MAGIC_HW)
11165 size = NVRAM_SELFBOOT_HW_SIZE;
11166 else
Michael Chan1b277772006-03-20 22:27:48 -080011167 return -EIO;
11168
11169 buf = kmalloc(size, GFP_KERNEL);
Michael Chan566f86a2005-05-29 14:56:58 -070011170 if (buf == NULL)
11171 return -ENOMEM;
11172
Michael Chan1b277772006-03-20 22:27:48 -080011173 err = -EIO;
11174 for (i = 0, j = 0; i < size; i += 4, j++) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000011175 err = tg3_nvram_read_be32(tp, i, &buf[j]);
11176 if (err)
Michael Chan566f86a2005-05-29 14:56:58 -070011177 break;
Michael Chan566f86a2005-05-29 14:56:58 -070011178 }
Michael Chan1b277772006-03-20 22:27:48 -080011179 if (i < size)
Michael Chan566f86a2005-05-29 14:56:58 -070011180 goto out;
11181
Michael Chan1b277772006-03-20 22:27:48 -080011182 /* Selfboot format */
Matt Carlsona9dc5292009-02-25 14:25:30 +000011183 magic = be32_to_cpu(buf[0]);
Al Virob9fc7dc2007-12-17 22:59:57 -080011184 if ((magic & TG3_EEPROM_MAGIC_FW_MSK) ==
Michael Chanb16250e2006-09-27 16:10:14 -070011185 TG3_EEPROM_MAGIC_FW) {
Michael Chan1b277772006-03-20 22:27:48 -080011186 u8 *buf8 = (u8 *) buf, csum8 = 0;
11187
Al Virob9fc7dc2007-12-17 22:59:57 -080011188 if ((magic & TG3_EEPROM_SB_REVISION_MASK) ==
Matt Carlsona5767de2007-11-12 21:10:58 -080011189 TG3_EEPROM_SB_REVISION_2) {
11190 /* For rev 2, the csum doesn't include the MBA. */
11191 for (i = 0; i < TG3_EEPROM_SB_F1R2_MBA_OFF; i++)
11192 csum8 += buf8[i];
11193 for (i = TG3_EEPROM_SB_F1R2_MBA_OFF + 4; i < size; i++)
11194 csum8 += buf8[i];
11195 } else {
11196 for (i = 0; i < size; i++)
11197 csum8 += buf8[i];
11198 }
Michael Chan1b277772006-03-20 22:27:48 -080011199
Adrian Bunkad96b482006-04-05 22:21:04 -070011200 if (csum8 == 0) {
11201 err = 0;
11202 goto out;
11203 }
11204
11205 err = -EIO;
11206 goto out;
Michael Chan1b277772006-03-20 22:27:48 -080011207 }
Michael Chan566f86a2005-05-29 14:56:58 -070011208
Al Virob9fc7dc2007-12-17 22:59:57 -080011209 if ((magic & TG3_EEPROM_MAGIC_HW_MSK) ==
Michael Chanb16250e2006-09-27 16:10:14 -070011210 TG3_EEPROM_MAGIC_HW) {
11211 u8 data[NVRAM_SELFBOOT_DATA_SIZE];
Matt Carlsona9dc5292009-02-25 14:25:30 +000011212 u8 parity[NVRAM_SELFBOOT_DATA_SIZE];
Michael Chanb16250e2006-09-27 16:10:14 -070011213 u8 *buf8 = (u8 *) buf;
Michael Chanb16250e2006-09-27 16:10:14 -070011214
11215 /* Separate the parity bits and the data bytes. */
11216 for (i = 0, j = 0, k = 0; i < NVRAM_SELFBOOT_HW_SIZE; i++) {
11217 if ((i == 0) || (i == 8)) {
11218 int l;
11219 u8 msk;
11220
11221 for (l = 0, msk = 0x80; l < 7; l++, msk >>= 1)
11222 parity[k++] = buf8[i] & msk;
11223 i++;
Matt Carlson859a588792010-04-05 10:19:28 +000011224 } else if (i == 16) {
Michael Chanb16250e2006-09-27 16:10:14 -070011225 int l;
11226 u8 msk;
11227
11228 for (l = 0, msk = 0x20; l < 6; l++, msk >>= 1)
11229 parity[k++] = buf8[i] & msk;
11230 i++;
11231
11232 for (l = 0, msk = 0x80; l < 8; l++, msk >>= 1)
11233 parity[k++] = buf8[i] & msk;
11234 i++;
11235 }
11236 data[j++] = buf8[i];
11237 }
11238
11239 err = -EIO;
11240 for (i = 0; i < NVRAM_SELFBOOT_DATA_SIZE; i++) {
11241 u8 hw8 = hweight8(data[i]);
11242
11243 if ((hw8 & 0x1) && parity[i])
11244 goto out;
11245 else if (!(hw8 & 0x1) && !parity[i])
11246 goto out;
11247 }
11248 err = 0;
11249 goto out;
11250 }
11251
Matt Carlson01c3a392011-03-09 16:58:20 +000011252 err = -EIO;
11253
Michael Chan566f86a2005-05-29 14:56:58 -070011254 /* Bootstrap checksum at offset 0x10 */
11255 csum = calc_crc((unsigned char *) buf, 0x10);
Matt Carlson01c3a392011-03-09 16:58:20 +000011256 if (csum != le32_to_cpu(buf[0x10/4]))
Michael Chan566f86a2005-05-29 14:56:58 -070011257 goto out;
11258
11259 /* Manufacturing block starts at offset 0x74, checksum at 0xfc */
11260 csum = calc_crc((unsigned char *) &buf[0x74/4], 0x88);
Matt Carlson01c3a392011-03-09 16:58:20 +000011261 if (csum != le32_to_cpu(buf[0xfc/4]))
Matt Carlsona9dc5292009-02-25 14:25:30 +000011262 goto out;
Michael Chan566f86a2005-05-29 14:56:58 -070011263
Matt Carlsonc3e94502011-04-13 11:05:08 +000011264 kfree(buf);
11265
Matt Carlson535a4902011-07-20 10:20:56 +000011266 buf = tg3_vpd_readblock(tp, &len);
Matt Carlsonc3e94502011-04-13 11:05:08 +000011267 if (!buf)
11268 return -ENOMEM;
Matt Carlsond4894f32011-03-09 16:58:21 +000011269
Matt Carlson535a4902011-07-20 10:20:56 +000011270 i = pci_vpd_find_tag((u8 *)buf, 0, len, PCI_VPD_LRDT_RO_DATA);
Matt Carlsond4894f32011-03-09 16:58:21 +000011271 if (i > 0) {
11272 j = pci_vpd_lrdt_size(&((u8 *)buf)[i]);
11273 if (j < 0)
11274 goto out;
11275
Matt Carlson535a4902011-07-20 10:20:56 +000011276 if (i + PCI_VPD_LRDT_TAG_SIZE + j > len)
Matt Carlsond4894f32011-03-09 16:58:21 +000011277 goto out;
11278
11279 i += PCI_VPD_LRDT_TAG_SIZE;
11280 j = pci_vpd_find_info_keyword((u8 *)buf, i, j,
11281 PCI_VPD_RO_KEYWORD_CHKSUM);
11282 if (j > 0) {
11283 u8 csum8 = 0;
11284
11285 j += PCI_VPD_INFO_FLD_HDR_SIZE;
11286
11287 for (i = 0; i <= j; i++)
11288 csum8 += ((u8 *)buf)[i];
11289
11290 if (csum8)
11291 goto out;
11292 }
11293 }
11294
Michael Chan566f86a2005-05-29 14:56:58 -070011295 err = 0;
11296
11297out:
11298 kfree(buf);
11299 return err;
11300}
11301
Michael Chanca430072005-05-29 14:57:23 -070011302#define TG3_SERDES_TIMEOUT_SEC 2
11303#define TG3_COPPER_TIMEOUT_SEC 6
11304
11305static int tg3_test_link(struct tg3 *tp)
11306{
11307 int i, max;
11308
11309 if (!netif_running(tp->dev))
11310 return -ENODEV;
11311
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011312 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
Michael Chanca430072005-05-29 14:57:23 -070011313 max = TG3_SERDES_TIMEOUT_SEC;
11314 else
11315 max = TG3_COPPER_TIMEOUT_SEC;
11316
11317 for (i = 0; i < max; i++) {
11318 if (netif_carrier_ok(tp->dev))
11319 return 0;
11320
11321 if (msleep_interruptible(1000))
11322 break;
11323 }
11324
11325 return -EIO;
11326}
11327
Michael Chana71116d2005-05-29 14:58:11 -070011328/* Only test the commonly used registers */
David S. Miller30ca3e32006-03-20 23:02:36 -080011329static int tg3_test_registers(struct tg3 *tp)
Michael Chana71116d2005-05-29 14:58:11 -070011330{
Michael Chanb16250e2006-09-27 16:10:14 -070011331 int i, is_5705, is_5750;
Michael Chana71116d2005-05-29 14:58:11 -070011332 u32 offset, read_mask, write_mask, val, save_val, read_val;
11333 static struct {
11334 u16 offset;
11335 u16 flags;
11336#define TG3_FL_5705 0x1
11337#define TG3_FL_NOT_5705 0x2
11338#define TG3_FL_NOT_5788 0x4
Michael Chanb16250e2006-09-27 16:10:14 -070011339#define TG3_FL_NOT_5750 0x8
Michael Chana71116d2005-05-29 14:58:11 -070011340 u32 read_mask;
11341 u32 write_mask;
11342 } reg_tbl[] = {
11343 /* MAC Control Registers */
11344 { MAC_MODE, TG3_FL_NOT_5705,
11345 0x00000000, 0x00ef6f8c },
11346 { MAC_MODE, TG3_FL_5705,
11347 0x00000000, 0x01ef6b8c },
11348 { MAC_STATUS, TG3_FL_NOT_5705,
11349 0x03800107, 0x00000000 },
11350 { MAC_STATUS, TG3_FL_5705,
11351 0x03800100, 0x00000000 },
11352 { MAC_ADDR_0_HIGH, 0x0000,
11353 0x00000000, 0x0000ffff },
11354 { MAC_ADDR_0_LOW, 0x0000,
Matt Carlsonc6cdf432010-04-05 10:19:26 +000011355 0x00000000, 0xffffffff },
Michael Chana71116d2005-05-29 14:58:11 -070011356 { MAC_RX_MTU_SIZE, 0x0000,
11357 0x00000000, 0x0000ffff },
11358 { MAC_TX_MODE, 0x0000,
11359 0x00000000, 0x00000070 },
11360 { MAC_TX_LENGTHS, 0x0000,
11361 0x00000000, 0x00003fff },
11362 { MAC_RX_MODE, TG3_FL_NOT_5705,
11363 0x00000000, 0x000007fc },
11364 { MAC_RX_MODE, TG3_FL_5705,
11365 0x00000000, 0x000007dc },
11366 { MAC_HASH_REG_0, 0x0000,
11367 0x00000000, 0xffffffff },
11368 { MAC_HASH_REG_1, 0x0000,
11369 0x00000000, 0xffffffff },
11370 { MAC_HASH_REG_2, 0x0000,
11371 0x00000000, 0xffffffff },
11372 { MAC_HASH_REG_3, 0x0000,
11373 0x00000000, 0xffffffff },
11374
11375 /* Receive Data and Receive BD Initiator Control Registers. */
11376 { RCVDBDI_JUMBO_BD+0, TG3_FL_NOT_5705,
11377 0x00000000, 0xffffffff },
11378 { RCVDBDI_JUMBO_BD+4, TG3_FL_NOT_5705,
11379 0x00000000, 0xffffffff },
11380 { RCVDBDI_JUMBO_BD+8, TG3_FL_NOT_5705,
11381 0x00000000, 0x00000003 },
11382 { RCVDBDI_JUMBO_BD+0xc, TG3_FL_NOT_5705,
11383 0x00000000, 0xffffffff },
11384 { RCVDBDI_STD_BD+0, 0x0000,
11385 0x00000000, 0xffffffff },
11386 { RCVDBDI_STD_BD+4, 0x0000,
11387 0x00000000, 0xffffffff },
11388 { RCVDBDI_STD_BD+8, 0x0000,
11389 0x00000000, 0xffff0002 },
11390 { RCVDBDI_STD_BD+0xc, 0x0000,
11391 0x00000000, 0xffffffff },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011392
Michael Chana71116d2005-05-29 14:58:11 -070011393 /* Receive BD Initiator Control Registers. */
11394 { RCVBDI_STD_THRESH, TG3_FL_NOT_5705,
11395 0x00000000, 0xffffffff },
11396 { RCVBDI_STD_THRESH, TG3_FL_5705,
11397 0x00000000, 0x000003ff },
11398 { RCVBDI_JUMBO_THRESH, TG3_FL_NOT_5705,
11399 0x00000000, 0xffffffff },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011400
Michael Chana71116d2005-05-29 14:58:11 -070011401 /* Host Coalescing Control Registers. */
11402 { HOSTCC_MODE, TG3_FL_NOT_5705,
11403 0x00000000, 0x00000004 },
11404 { HOSTCC_MODE, TG3_FL_5705,
11405 0x00000000, 0x000000f6 },
11406 { HOSTCC_RXCOL_TICKS, TG3_FL_NOT_5705,
11407 0x00000000, 0xffffffff },
11408 { HOSTCC_RXCOL_TICKS, TG3_FL_5705,
11409 0x00000000, 0x000003ff },
11410 { HOSTCC_TXCOL_TICKS, TG3_FL_NOT_5705,
11411 0x00000000, 0xffffffff },
11412 { HOSTCC_TXCOL_TICKS, TG3_FL_5705,
11413 0x00000000, 0x000003ff },
11414 { HOSTCC_RXMAX_FRAMES, TG3_FL_NOT_5705,
11415 0x00000000, 0xffffffff },
11416 { HOSTCC_RXMAX_FRAMES, TG3_FL_5705 | TG3_FL_NOT_5788,
11417 0x00000000, 0x000000ff },
11418 { HOSTCC_TXMAX_FRAMES, TG3_FL_NOT_5705,
11419 0x00000000, 0xffffffff },
11420 { HOSTCC_TXMAX_FRAMES, TG3_FL_5705 | TG3_FL_NOT_5788,
11421 0x00000000, 0x000000ff },
11422 { HOSTCC_RXCOAL_TICK_INT, TG3_FL_NOT_5705,
11423 0x00000000, 0xffffffff },
11424 { HOSTCC_TXCOAL_TICK_INT, TG3_FL_NOT_5705,
11425 0x00000000, 0xffffffff },
11426 { HOSTCC_RXCOAL_MAXF_INT, TG3_FL_NOT_5705,
11427 0x00000000, 0xffffffff },
11428 { HOSTCC_RXCOAL_MAXF_INT, TG3_FL_5705 | TG3_FL_NOT_5788,
11429 0x00000000, 0x000000ff },
11430 { HOSTCC_TXCOAL_MAXF_INT, TG3_FL_NOT_5705,
11431 0x00000000, 0xffffffff },
11432 { HOSTCC_TXCOAL_MAXF_INT, TG3_FL_5705 | TG3_FL_NOT_5788,
11433 0x00000000, 0x000000ff },
11434 { HOSTCC_STAT_COAL_TICKS, TG3_FL_NOT_5705,
11435 0x00000000, 0xffffffff },
11436 { HOSTCC_STATS_BLK_HOST_ADDR, TG3_FL_NOT_5705,
11437 0x00000000, 0xffffffff },
11438 { HOSTCC_STATS_BLK_HOST_ADDR+4, TG3_FL_NOT_5705,
11439 0x00000000, 0xffffffff },
11440 { HOSTCC_STATUS_BLK_HOST_ADDR, 0x0000,
11441 0x00000000, 0xffffffff },
11442 { HOSTCC_STATUS_BLK_HOST_ADDR+4, 0x0000,
11443 0x00000000, 0xffffffff },
11444 { HOSTCC_STATS_BLK_NIC_ADDR, 0x0000,
11445 0xffffffff, 0x00000000 },
11446 { HOSTCC_STATUS_BLK_NIC_ADDR, 0x0000,
11447 0xffffffff, 0x00000000 },
11448
11449 /* Buffer Manager Control Registers. */
Michael Chanb16250e2006-09-27 16:10:14 -070011450 { BUFMGR_MB_POOL_ADDR, TG3_FL_NOT_5750,
Michael Chana71116d2005-05-29 14:58:11 -070011451 0x00000000, 0x007fff80 },
Michael Chanb16250e2006-09-27 16:10:14 -070011452 { BUFMGR_MB_POOL_SIZE, TG3_FL_NOT_5750,
Michael Chana71116d2005-05-29 14:58:11 -070011453 0x00000000, 0x007fffff },
11454 { BUFMGR_MB_RDMA_LOW_WATER, 0x0000,
11455 0x00000000, 0x0000003f },
11456 { BUFMGR_MB_MACRX_LOW_WATER, 0x0000,
11457 0x00000000, 0x000001ff },
11458 { BUFMGR_MB_HIGH_WATER, 0x0000,
11459 0x00000000, 0x000001ff },
11460 { BUFMGR_DMA_DESC_POOL_ADDR, TG3_FL_NOT_5705,
11461 0xffffffff, 0x00000000 },
11462 { BUFMGR_DMA_DESC_POOL_SIZE, TG3_FL_NOT_5705,
11463 0xffffffff, 0x00000000 },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011464
Michael Chana71116d2005-05-29 14:58:11 -070011465 /* Mailbox Registers */
11466 { GRCMBOX_RCVSTD_PROD_IDX+4, 0x0000,
11467 0x00000000, 0x000001ff },
11468 { GRCMBOX_RCVJUMBO_PROD_IDX+4, TG3_FL_NOT_5705,
11469 0x00000000, 0x000001ff },
11470 { GRCMBOX_RCVRET_CON_IDX_0+4, 0x0000,
11471 0x00000000, 0x000007ff },
11472 { GRCMBOX_SNDHOST_PROD_IDX_0+4, 0x0000,
11473 0x00000000, 0x000001ff },
11474
11475 { 0xffff, 0x0000, 0x00000000, 0x00000000 },
11476 };
11477
Michael Chanb16250e2006-09-27 16:10:14 -070011478 is_5705 = is_5750 = 0;
Joe Perches63c3a662011-04-26 08:12:10 +000011479 if (tg3_flag(tp, 5705_PLUS)) {
Michael Chana71116d2005-05-29 14:58:11 -070011480 is_5705 = 1;
Joe Perches63c3a662011-04-26 08:12:10 +000011481 if (tg3_flag(tp, 5750_PLUS))
Michael Chanb16250e2006-09-27 16:10:14 -070011482 is_5750 = 1;
11483 }
Michael Chana71116d2005-05-29 14:58:11 -070011484
11485 for (i = 0; reg_tbl[i].offset != 0xffff; i++) {
11486 if (is_5705 && (reg_tbl[i].flags & TG3_FL_NOT_5705))
11487 continue;
11488
11489 if (!is_5705 && (reg_tbl[i].flags & TG3_FL_5705))
11490 continue;
11491
Joe Perches63c3a662011-04-26 08:12:10 +000011492 if (tg3_flag(tp, IS_5788) &&
Michael Chana71116d2005-05-29 14:58:11 -070011493 (reg_tbl[i].flags & TG3_FL_NOT_5788))
11494 continue;
11495
Michael Chanb16250e2006-09-27 16:10:14 -070011496 if (is_5750 && (reg_tbl[i].flags & TG3_FL_NOT_5750))
11497 continue;
11498
Michael Chana71116d2005-05-29 14:58:11 -070011499 offset = (u32) reg_tbl[i].offset;
11500 read_mask = reg_tbl[i].read_mask;
11501 write_mask = reg_tbl[i].write_mask;
11502
11503 /* Save the original register content */
11504 save_val = tr32(offset);
11505
11506 /* Determine the read-only value. */
11507 read_val = save_val & read_mask;
11508
11509 /* Write zero to the register, then make sure the read-only bits
11510 * are not changed and the read/write bits are all zeros.
11511 */
11512 tw32(offset, 0);
11513
11514 val = tr32(offset);
11515
11516 /* Test the read-only and read/write bits. */
11517 if (((val & read_mask) != read_val) || (val & write_mask))
11518 goto out;
11519
11520 /* Write ones to all the bits defined by RdMask and WrMask, then
11521 * make sure the read-only bits are not changed and the
11522 * read/write bits are all ones.
11523 */
11524 tw32(offset, read_mask | write_mask);
11525
11526 val = tr32(offset);
11527
11528 /* Test the read-only bits. */
11529 if ((val & read_mask) != read_val)
11530 goto out;
11531
11532 /* Test the read/write bits. */
11533 if ((val & write_mask) != write_mask)
11534 goto out;
11535
11536 tw32(offset, save_val);
11537 }
11538
11539 return 0;
11540
11541out:
Michael Chan9f88f292006-12-07 00:22:54 -080011542 if (netif_msg_hw(tp))
Matt Carlson2445e462010-04-05 10:19:21 +000011543 netdev_err(tp->dev,
11544 "Register test failed at offset %x\n", offset);
Michael Chana71116d2005-05-29 14:58:11 -070011545 tw32(offset, save_val);
11546 return -EIO;
11547}
11548
Michael Chan7942e1d2005-05-29 14:58:36 -070011549static int tg3_do_mem_test(struct tg3 *tp, u32 offset, u32 len)
11550{
Arjan van de Venf71e1302006-03-03 21:33:57 -050011551 static const u32 test_pattern[] = { 0x00000000, 0xffffffff, 0xaa55a55a };
Michael Chan7942e1d2005-05-29 14:58:36 -070011552 int i;
11553 u32 j;
11554
Alejandro Martinez Ruize9edda62007-10-15 03:37:43 +020011555 for (i = 0; i < ARRAY_SIZE(test_pattern); i++) {
Michael Chan7942e1d2005-05-29 14:58:36 -070011556 for (j = 0; j < len; j += 4) {
11557 u32 val;
11558
11559 tg3_write_mem(tp, offset + j, test_pattern[i]);
11560 tg3_read_mem(tp, offset + j, &val);
11561 if (val != test_pattern[i])
11562 return -EIO;
11563 }
11564 }
11565 return 0;
11566}
11567
11568static int tg3_test_memory(struct tg3 *tp)
11569{
11570 static struct mem_entry {
11571 u32 offset;
11572 u32 len;
11573 } mem_tbl_570x[] = {
Michael Chan38690192005-12-19 16:27:28 -080011574 { 0x00000000, 0x00b50},
Michael Chan7942e1d2005-05-29 14:58:36 -070011575 { 0x00002000, 0x1c000},
11576 { 0xffffffff, 0x00000}
11577 }, mem_tbl_5705[] = {
11578 { 0x00000100, 0x0000c},
11579 { 0x00000200, 0x00008},
Michael Chan7942e1d2005-05-29 14:58:36 -070011580 { 0x00004000, 0x00800},
11581 { 0x00006000, 0x01000},
11582 { 0x00008000, 0x02000},
11583 { 0x00010000, 0x0e000},
11584 { 0xffffffff, 0x00000}
Michael Chan79f4d132006-03-20 22:28:57 -080011585 }, mem_tbl_5755[] = {
11586 { 0x00000200, 0x00008},
11587 { 0x00004000, 0x00800},
11588 { 0x00006000, 0x00800},
11589 { 0x00008000, 0x02000},
11590 { 0x00010000, 0x0c000},
11591 { 0xffffffff, 0x00000}
Michael Chanb16250e2006-09-27 16:10:14 -070011592 }, mem_tbl_5906[] = {
11593 { 0x00000200, 0x00008},
11594 { 0x00004000, 0x00400},
11595 { 0x00006000, 0x00400},
11596 { 0x00008000, 0x01000},
11597 { 0x00010000, 0x01000},
11598 { 0xffffffff, 0x00000}
Matt Carlson8b5a6c42010-01-20 16:58:06 +000011599 }, mem_tbl_5717[] = {
11600 { 0x00000200, 0x00008},
11601 { 0x00010000, 0x0a000},
11602 { 0x00020000, 0x13c00},
11603 { 0xffffffff, 0x00000}
11604 }, mem_tbl_57765[] = {
11605 { 0x00000200, 0x00008},
11606 { 0x00004000, 0x00800},
11607 { 0x00006000, 0x09800},
11608 { 0x00010000, 0x0a000},
11609 { 0xffffffff, 0x00000}
Michael Chan7942e1d2005-05-29 14:58:36 -070011610 };
11611 struct mem_entry *mem_tbl;
11612 int err = 0;
11613 int i;
11614
Joe Perches63c3a662011-04-26 08:12:10 +000011615 if (tg3_flag(tp, 5717_PLUS))
Matt Carlson8b5a6c42010-01-20 16:58:06 +000011616 mem_tbl = mem_tbl_5717;
Matt Carlson55086ad2011-12-14 11:09:59 +000011617 else if (tg3_flag(tp, 57765_CLASS))
Matt Carlson8b5a6c42010-01-20 16:58:06 +000011618 mem_tbl = mem_tbl_57765;
Joe Perches63c3a662011-04-26 08:12:10 +000011619 else if (tg3_flag(tp, 5755_PLUS))
Matt Carlson321d32a2008-11-21 17:22:19 -080011620 mem_tbl = mem_tbl_5755;
11621 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
11622 mem_tbl = mem_tbl_5906;
Joe Perches63c3a662011-04-26 08:12:10 +000011623 else if (tg3_flag(tp, 5705_PLUS))
Matt Carlson321d32a2008-11-21 17:22:19 -080011624 mem_tbl = mem_tbl_5705;
11625 else
Michael Chan7942e1d2005-05-29 14:58:36 -070011626 mem_tbl = mem_tbl_570x;
11627
11628 for (i = 0; mem_tbl[i].offset != 0xffffffff; i++) {
Matt Carlsonbe98da62010-07-11 09:31:46 +000011629 err = tg3_do_mem_test(tp, mem_tbl[i].offset, mem_tbl[i].len);
11630 if (err)
Michael Chan7942e1d2005-05-29 14:58:36 -070011631 break;
11632 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011633
Michael Chan7942e1d2005-05-29 14:58:36 -070011634 return err;
11635}
11636
Matt Carlsonbb158d62011-04-25 12:42:47 +000011637#define TG3_TSO_MSS 500
11638
11639#define TG3_TSO_IP_HDR_LEN 20
11640#define TG3_TSO_TCP_HDR_LEN 20
11641#define TG3_TSO_TCP_OPT_LEN 12
11642
11643static const u8 tg3_tso_header[] = {
116440x08, 0x00,
116450x45, 0x00, 0x00, 0x00,
116460x00, 0x00, 0x40, 0x00,
116470x40, 0x06, 0x00, 0x00,
116480x0a, 0x00, 0x00, 0x01,
116490x0a, 0x00, 0x00, 0x02,
116500x0d, 0x00, 0xe0, 0x00,
116510x00, 0x00, 0x01, 0x00,
116520x00, 0x00, 0x02, 0x00,
116530x80, 0x10, 0x10, 0x00,
116540x14, 0x09, 0x00, 0x00,
116550x01, 0x01, 0x08, 0x0a,
116560x11, 0x11, 0x11, 0x11,
116570x11, 0x11, 0x11, 0x11,
11658};
Michael Chan9f40dea2005-09-05 17:53:06 -070011659
Matt Carlson28a45952011-08-19 13:58:22 +000011660static int tg3_run_loopback(struct tg3 *tp, u32 pktsz, bool tso_loopback)
Michael Chanc76949a2005-05-29 14:58:59 -070011661{
Matt Carlson5e5a7f32011-08-19 13:58:21 +000011662 u32 rx_start_idx, rx_idx, tx_idx, opaque_key;
Matt Carlsonbb158d62011-04-25 12:42:47 +000011663 u32 base_flags = 0, mss = 0, desc_idx, coal_now, data_off, val;
Matt Carlson84b67b22011-07-27 14:20:52 +000011664 u32 budget;
Eric Dumazet9205fd92011-11-18 06:47:01 +000011665 struct sk_buff *skb;
11666 u8 *tx_data, *rx_data;
Michael Chanc76949a2005-05-29 14:58:59 -070011667 dma_addr_t map;
11668 int num_pkts, tx_len, rx_len, i, err;
11669 struct tg3_rx_buffer_desc *desc;
Matt Carlson898a56f2009-08-28 14:02:40 +000011670 struct tg3_napi *tnapi, *rnapi;
Matt Carlson8fea32b2010-09-15 08:59:58 +000011671 struct tg3_rx_prodring_set *tpr = &tp->napi[0].prodring;
Michael Chanc76949a2005-05-29 14:58:59 -070011672
Matt Carlsonc8873402010-02-12 14:47:11 +000011673 tnapi = &tp->napi[0];
11674 rnapi = &tp->napi[0];
Matt Carlson0c1d0e22009-09-01 13:16:33 +000011675 if (tp->irq_cnt > 1) {
Joe Perches63c3a662011-04-26 08:12:10 +000011676 if (tg3_flag(tp, ENABLE_RSS))
Matt Carlson1da85aa2010-09-30 10:34:34 +000011677 rnapi = &tp->napi[1];
Joe Perches63c3a662011-04-26 08:12:10 +000011678 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlsonc8873402010-02-12 14:47:11 +000011679 tnapi = &tp->napi[1];
Matt Carlson0c1d0e22009-09-01 13:16:33 +000011680 }
Matt Carlsonfd2ce372009-09-01 12:51:13 +000011681 coal_now = tnapi->coal_now | rnapi->coal_now;
Matt Carlson898a56f2009-08-28 14:02:40 +000011682
Michael Chanc76949a2005-05-29 14:58:59 -070011683 err = -EIO;
11684
Matt Carlson4852a862011-04-13 11:05:07 +000011685 tx_len = pktsz;
David S. Millera20e9c62006-07-31 22:38:16 -070011686 skb = netdev_alloc_skb(tp->dev, tx_len);
Jesper Juhla50bb7b2006-05-09 23:14:35 -070011687 if (!skb)
11688 return -ENOMEM;
11689
Michael Chanc76949a2005-05-29 14:58:59 -070011690 tx_data = skb_put(skb, tx_len);
11691 memcpy(tx_data, tp->dev->dev_addr, 6);
11692 memset(tx_data + 6, 0x0, 8);
11693
Matt Carlson4852a862011-04-13 11:05:07 +000011694 tw32(MAC_RX_MTU_SIZE, tx_len + ETH_FCS_LEN);
Michael Chanc76949a2005-05-29 14:58:59 -070011695
Matt Carlson28a45952011-08-19 13:58:22 +000011696 if (tso_loopback) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000011697 struct iphdr *iph = (struct iphdr *)&tx_data[ETH_HLEN];
11698
11699 u32 hdr_len = TG3_TSO_IP_HDR_LEN + TG3_TSO_TCP_HDR_LEN +
11700 TG3_TSO_TCP_OPT_LEN;
11701
11702 memcpy(tx_data + ETH_ALEN * 2, tg3_tso_header,
11703 sizeof(tg3_tso_header));
11704 mss = TG3_TSO_MSS;
11705
11706 val = tx_len - ETH_ALEN * 2 - sizeof(tg3_tso_header);
11707 num_pkts = DIV_ROUND_UP(val, TG3_TSO_MSS);
11708
11709 /* Set the total length field in the IP header */
11710 iph->tot_len = htons((u16)(mss + hdr_len));
11711
11712 base_flags = (TXD_FLAG_CPU_PRE_DMA |
11713 TXD_FLAG_CPU_POST_DMA);
11714
Joe Perches63c3a662011-04-26 08:12:10 +000011715 if (tg3_flag(tp, HW_TSO_1) ||
11716 tg3_flag(tp, HW_TSO_2) ||
11717 tg3_flag(tp, HW_TSO_3)) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000011718 struct tcphdr *th;
11719 val = ETH_HLEN + TG3_TSO_IP_HDR_LEN;
11720 th = (struct tcphdr *)&tx_data[val];
11721 th->check = 0;
11722 } else
11723 base_flags |= TXD_FLAG_TCPUDP_CSUM;
11724
Joe Perches63c3a662011-04-26 08:12:10 +000011725 if (tg3_flag(tp, HW_TSO_3)) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000011726 mss |= (hdr_len & 0xc) << 12;
11727 if (hdr_len & 0x10)
11728 base_flags |= 0x00000010;
11729 base_flags |= (hdr_len & 0x3e0) << 5;
Joe Perches63c3a662011-04-26 08:12:10 +000011730 } else if (tg3_flag(tp, HW_TSO_2))
Matt Carlsonbb158d62011-04-25 12:42:47 +000011731 mss |= hdr_len << 9;
Joe Perches63c3a662011-04-26 08:12:10 +000011732 else if (tg3_flag(tp, HW_TSO_1) ||
Matt Carlsonbb158d62011-04-25 12:42:47 +000011733 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
11734 mss |= (TG3_TSO_TCP_OPT_LEN << 9);
11735 } else {
11736 base_flags |= (TG3_TSO_TCP_OPT_LEN << 10);
11737 }
11738
11739 data_off = ETH_ALEN * 2 + sizeof(tg3_tso_header);
11740 } else {
11741 num_pkts = 1;
11742 data_off = ETH_HLEN;
11743 }
11744
11745 for (i = data_off; i < tx_len; i++)
Michael Chanc76949a2005-05-29 14:58:59 -070011746 tx_data[i] = (u8) (i & 0xff);
11747
Alexander Duyckf4188d82009-12-02 16:48:38 +000011748 map = pci_map_single(tp->pdev, skb->data, tx_len, PCI_DMA_TODEVICE);
11749 if (pci_dma_mapping_error(tp->pdev, map)) {
Matt Carlsona21771d2009-11-02 14:25:31 +000011750 dev_kfree_skb(skb);
11751 return -EIO;
11752 }
Michael Chanc76949a2005-05-29 14:58:59 -070011753
Matt Carlson0d681b22011-07-27 14:20:49 +000011754 val = tnapi->tx_prod;
11755 tnapi->tx_buffers[val].skb = skb;
11756 dma_unmap_addr_set(&tnapi->tx_buffers[val], mapping, map);
11757
Michael Chanc76949a2005-05-29 14:58:59 -070011758 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +000011759 rnapi->coal_now);
Michael Chanc76949a2005-05-29 14:58:59 -070011760
11761 udelay(10);
11762
Matt Carlson898a56f2009-08-28 14:02:40 +000011763 rx_start_idx = rnapi->hw_status->idx[0].rx_producer;
Michael Chanc76949a2005-05-29 14:58:59 -070011764
Matt Carlson84b67b22011-07-27 14:20:52 +000011765 budget = tg3_tx_avail(tnapi);
11766 if (tg3_tx_frag_set(tnapi, &val, &budget, map, tx_len,
Matt Carlsond1a3b732011-07-27 14:20:51 +000011767 base_flags | TXD_FLAG_END, mss, 0)) {
11768 tnapi->tx_buffers[val].skb = NULL;
11769 dev_kfree_skb(skb);
11770 return -EIO;
11771 }
Michael Chanc76949a2005-05-29 14:58:59 -070011772
Matt Carlsonf3f3f272009-08-28 14:03:21 +000011773 tnapi->tx_prod++;
Michael Chanc76949a2005-05-29 14:58:59 -070011774
Matt Carlsonf3f3f272009-08-28 14:03:21 +000011775 tw32_tx_mbox(tnapi->prodmbox, tnapi->tx_prod);
11776 tr32_mailbox(tnapi->prodmbox);
Michael Chanc76949a2005-05-29 14:58:59 -070011777
11778 udelay(10);
11779
Matt Carlson303fc922009-11-02 14:27:34 +000011780 /* 350 usec to allow enough time on some 10/100 Mbps devices. */
11781 for (i = 0; i < 35; i++) {
Michael Chanc76949a2005-05-29 14:58:59 -070011782 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +000011783 coal_now);
Michael Chanc76949a2005-05-29 14:58:59 -070011784
11785 udelay(10);
11786
Matt Carlson898a56f2009-08-28 14:02:40 +000011787 tx_idx = tnapi->hw_status->idx[0].tx_consumer;
11788 rx_idx = rnapi->hw_status->idx[0].rx_producer;
Matt Carlsonf3f3f272009-08-28 14:03:21 +000011789 if ((tx_idx == tnapi->tx_prod) &&
Michael Chanc76949a2005-05-29 14:58:59 -070011790 (rx_idx == (rx_start_idx + num_pkts)))
11791 break;
11792 }
11793
Matt Carlsonba1142e2011-11-04 09:15:00 +000011794 tg3_tx_skb_unmap(tnapi, tnapi->tx_prod - 1, -1);
Michael Chanc76949a2005-05-29 14:58:59 -070011795 dev_kfree_skb(skb);
11796
Matt Carlsonf3f3f272009-08-28 14:03:21 +000011797 if (tx_idx != tnapi->tx_prod)
Michael Chanc76949a2005-05-29 14:58:59 -070011798 goto out;
11799
11800 if (rx_idx != rx_start_idx + num_pkts)
11801 goto out;
11802
Matt Carlsonbb158d62011-04-25 12:42:47 +000011803 val = data_off;
11804 while (rx_idx != rx_start_idx) {
11805 desc = &rnapi->rx_rcb[rx_start_idx++];
11806 desc_idx = desc->opaque & RXD_OPAQUE_INDEX_MASK;
11807 opaque_key = desc->opaque & RXD_OPAQUE_RING_MASK;
Michael Chanc76949a2005-05-29 14:58:59 -070011808
Matt Carlsonbb158d62011-04-25 12:42:47 +000011809 if ((desc->err_vlan & RXD_ERR_MASK) != 0 &&
11810 (desc->err_vlan != RXD_ERR_ODD_NIBBLE_RCVD_MII))
Matt Carlson4852a862011-04-13 11:05:07 +000011811 goto out;
Michael Chanc76949a2005-05-29 14:58:59 -070011812
Matt Carlsonbb158d62011-04-25 12:42:47 +000011813 rx_len = ((desc->idx_len & RXD_LEN_MASK) >> RXD_LEN_SHIFT)
11814 - ETH_FCS_LEN;
11815
Matt Carlson28a45952011-08-19 13:58:22 +000011816 if (!tso_loopback) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000011817 if (rx_len != tx_len)
11818 goto out;
11819
11820 if (pktsz <= TG3_RX_STD_DMA_SZ - ETH_FCS_LEN) {
11821 if (opaque_key != RXD_OPAQUE_RING_STD)
11822 goto out;
11823 } else {
11824 if (opaque_key != RXD_OPAQUE_RING_JUMBO)
11825 goto out;
11826 }
11827 } else if ((desc->type_flags & RXD_FLAG_TCPUDP_CSUM) &&
11828 (desc->ip_tcp_csum & RXD_TCPCSUM_MASK)
Matt Carlson54e0a672011-05-19 12:12:50 +000011829 >> RXD_TCPCSUM_SHIFT != 0xffff) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000011830 goto out;
11831 }
11832
11833 if (opaque_key == RXD_OPAQUE_RING_STD) {
Eric Dumazet9205fd92011-11-18 06:47:01 +000011834 rx_data = tpr->rx_std_buffers[desc_idx].data;
Matt Carlsonbb158d62011-04-25 12:42:47 +000011835 map = dma_unmap_addr(&tpr->rx_std_buffers[desc_idx],
11836 mapping);
11837 } else if (opaque_key == RXD_OPAQUE_RING_JUMBO) {
Eric Dumazet9205fd92011-11-18 06:47:01 +000011838 rx_data = tpr->rx_jmb_buffers[desc_idx].data;
Matt Carlsonbb158d62011-04-25 12:42:47 +000011839 map = dma_unmap_addr(&tpr->rx_jmb_buffers[desc_idx],
11840 mapping);
11841 } else
Matt Carlson4852a862011-04-13 11:05:07 +000011842 goto out;
11843
Matt Carlsonbb158d62011-04-25 12:42:47 +000011844 pci_dma_sync_single_for_cpu(tp->pdev, map, rx_len,
11845 PCI_DMA_FROMDEVICE);
11846
Eric Dumazet9205fd92011-11-18 06:47:01 +000011847 rx_data += TG3_RX_OFFSET(tp);
Matt Carlsonbb158d62011-04-25 12:42:47 +000011848 for (i = data_off; i < rx_len; i++, val++) {
Eric Dumazet9205fd92011-11-18 06:47:01 +000011849 if (*(rx_data + i) != (u8) (val & 0xff))
Matt Carlsonbb158d62011-04-25 12:42:47 +000011850 goto out;
11851 }
Matt Carlson4852a862011-04-13 11:05:07 +000011852 }
11853
Michael Chanc76949a2005-05-29 14:58:59 -070011854 err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011855
Eric Dumazet9205fd92011-11-18 06:47:01 +000011856 /* tg3_free_rings will unmap and free the rx_data */
Michael Chanc76949a2005-05-29 14:58:59 -070011857out:
11858 return err;
11859}
11860
Matt Carlson00c266b2011-04-25 12:42:46 +000011861#define TG3_STD_LOOPBACK_FAILED 1
11862#define TG3_JMB_LOOPBACK_FAILED 2
Matt Carlsonbb158d62011-04-25 12:42:47 +000011863#define TG3_TSO_LOOPBACK_FAILED 4
Matt Carlson28a45952011-08-19 13:58:22 +000011864#define TG3_LOOPBACK_FAILED \
11865 (TG3_STD_LOOPBACK_FAILED | \
11866 TG3_JMB_LOOPBACK_FAILED | \
11867 TG3_TSO_LOOPBACK_FAILED)
Matt Carlson00c266b2011-04-25 12:42:46 +000011868
Matt Carlson941ec902011-08-19 13:58:23 +000011869static int tg3_test_loopback(struct tg3 *tp, u64 *data, bool do_extlpbk)
Michael Chan9f40dea2005-09-05 17:53:06 -070011870{
Matt Carlson28a45952011-08-19 13:58:22 +000011871 int err = -EIO;
Matt Carlson2215e242011-08-19 13:58:19 +000011872 u32 eee_cap;
Michael Chan9f40dea2005-09-05 17:53:06 -070011873
Matt Carlsonab789042011-01-25 15:58:54 +000011874 eee_cap = tp->phy_flags & TG3_PHYFLG_EEE_CAP;
11875 tp->phy_flags &= ~TG3_PHYFLG_EEE_CAP;
11876
Matt Carlson28a45952011-08-19 13:58:22 +000011877 if (!netif_running(tp->dev)) {
11878 data[0] = TG3_LOOPBACK_FAILED;
11879 data[1] = TG3_LOOPBACK_FAILED;
Matt Carlson941ec902011-08-19 13:58:23 +000011880 if (do_extlpbk)
11881 data[2] = TG3_LOOPBACK_FAILED;
Matt Carlson28a45952011-08-19 13:58:22 +000011882 goto done;
11883 }
11884
Michael Chanb9ec6c12006-07-25 16:37:27 -070011885 err = tg3_reset_hw(tp, 1);
Matt Carlsonab789042011-01-25 15:58:54 +000011886 if (err) {
Matt Carlson28a45952011-08-19 13:58:22 +000011887 data[0] = TG3_LOOPBACK_FAILED;
11888 data[1] = TG3_LOOPBACK_FAILED;
Matt Carlson941ec902011-08-19 13:58:23 +000011889 if (do_extlpbk)
11890 data[2] = TG3_LOOPBACK_FAILED;
Matt Carlsonab789042011-01-25 15:58:54 +000011891 goto done;
11892 }
Michael Chan9f40dea2005-09-05 17:53:06 -070011893
Joe Perches63c3a662011-04-26 08:12:10 +000011894 if (tg3_flag(tp, ENABLE_RSS)) {
Matt Carlson4a85f092011-04-20 07:57:37 +000011895 int i;
11896
11897 /* Reroute all rx packets to the 1st queue */
11898 for (i = MAC_RSS_INDIR_TBL_0;
11899 i < MAC_RSS_INDIR_TBL_0 + TG3_RSS_INDIR_TBL_SIZE; i += 4)
11900 tw32(i, 0x0);
11901 }
11902
Matt Carlson6e01b202011-08-19 13:58:20 +000011903 /* HW errata - mac loopback fails in some cases on 5780.
11904 * Normal traffic and PHY loopback are not affected by
11905 * errata. Also, the MAC loopback test is deprecated for
11906 * all newer ASIC revisions.
11907 */
11908 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5780 &&
11909 !tg3_flag(tp, CPMU_PRESENT)) {
11910 tg3_mac_loopback(tp, true);
Matt Carlson9936bcf2007-10-10 18:03:07 -070011911
Matt Carlson28a45952011-08-19 13:58:22 +000011912 if (tg3_run_loopback(tp, ETH_FRAME_LEN, false))
11913 data[0] |= TG3_STD_LOOPBACK_FAILED;
Matt Carlson6e01b202011-08-19 13:58:20 +000011914
11915 if (tg3_flag(tp, JUMBO_RING_ENABLE) &&
Matt Carlson28a45952011-08-19 13:58:22 +000011916 tg3_run_loopback(tp, 9000 + ETH_HLEN, false))
11917 data[0] |= TG3_JMB_LOOPBACK_FAILED;
Matt Carlson6e01b202011-08-19 13:58:20 +000011918
11919 tg3_mac_loopback(tp, false);
11920 }
Matt Carlson4852a862011-04-13 11:05:07 +000011921
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011922 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
Joe Perches63c3a662011-04-26 08:12:10 +000011923 !tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson5e5a7f32011-08-19 13:58:21 +000011924 int i;
11925
Matt Carlson941ec902011-08-19 13:58:23 +000011926 tg3_phy_lpbk_set(tp, 0, false);
Matt Carlson5e5a7f32011-08-19 13:58:21 +000011927
11928 /* Wait for link */
11929 for (i = 0; i < 100; i++) {
11930 if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
11931 break;
11932 mdelay(1);
11933 }
11934
Matt Carlson28a45952011-08-19 13:58:22 +000011935 if (tg3_run_loopback(tp, ETH_FRAME_LEN, false))
11936 data[1] |= TG3_STD_LOOPBACK_FAILED;
Joe Perches63c3a662011-04-26 08:12:10 +000011937 if (tg3_flag(tp, TSO_CAPABLE) &&
Matt Carlson28a45952011-08-19 13:58:22 +000011938 tg3_run_loopback(tp, ETH_FRAME_LEN, true))
11939 data[1] |= TG3_TSO_LOOPBACK_FAILED;
Joe Perches63c3a662011-04-26 08:12:10 +000011940 if (tg3_flag(tp, JUMBO_RING_ENABLE) &&
Matt Carlson28a45952011-08-19 13:58:22 +000011941 tg3_run_loopback(tp, 9000 + ETH_HLEN, false))
11942 data[1] |= TG3_JMB_LOOPBACK_FAILED;
Michael Chan9f40dea2005-09-05 17:53:06 -070011943
Matt Carlson941ec902011-08-19 13:58:23 +000011944 if (do_extlpbk) {
11945 tg3_phy_lpbk_set(tp, 0, true);
11946
11947 /* All link indications report up, but the hardware
11948 * isn't really ready for about 20 msec. Double it
11949 * to be sure.
11950 */
11951 mdelay(40);
11952
11953 if (tg3_run_loopback(tp, ETH_FRAME_LEN, false))
11954 data[2] |= TG3_STD_LOOPBACK_FAILED;
11955 if (tg3_flag(tp, TSO_CAPABLE) &&
11956 tg3_run_loopback(tp, ETH_FRAME_LEN, true))
11957 data[2] |= TG3_TSO_LOOPBACK_FAILED;
11958 if (tg3_flag(tp, JUMBO_RING_ENABLE) &&
11959 tg3_run_loopback(tp, 9000 + ETH_HLEN, false))
11960 data[2] |= TG3_JMB_LOOPBACK_FAILED;
11961 }
11962
Matt Carlson5e5a7f32011-08-19 13:58:21 +000011963 /* Re-enable gphy autopowerdown. */
11964 if (tp->phy_flags & TG3_PHYFLG_ENABLE_APD)
11965 tg3_phy_toggle_apd(tp, true);
11966 }
Matt Carlson6833c042008-11-21 17:18:59 -080011967
Matt Carlson941ec902011-08-19 13:58:23 +000011968 err = (data[0] | data[1] | data[2]) ? -EIO : 0;
Matt Carlson28a45952011-08-19 13:58:22 +000011969
Matt Carlsonab789042011-01-25 15:58:54 +000011970done:
11971 tp->phy_flags |= eee_cap;
11972
Michael Chan9f40dea2005-09-05 17:53:06 -070011973 return err;
11974}
11975
Michael Chan4cafd3f2005-05-29 14:56:34 -070011976static void tg3_self_test(struct net_device *dev, struct ethtool_test *etest,
11977 u64 *data)
11978{
Michael Chan566f86a2005-05-29 14:56:58 -070011979 struct tg3 *tp = netdev_priv(dev);
Matt Carlson941ec902011-08-19 13:58:23 +000011980 bool doextlpbk = etest->flags & ETH_TEST_FL_EXTERNAL_LB;
Michael Chan566f86a2005-05-29 14:56:58 -070011981
Matt Carlsonbed98292011-07-13 09:27:29 +000011982 if ((tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) &&
11983 tg3_power_up(tp)) {
11984 etest->flags |= ETH_TEST_FL_FAILED;
11985 memset(data, 1, sizeof(u64) * TG3_NUM_TEST);
11986 return;
11987 }
Michael Chanbc1c7562006-03-20 17:48:03 -080011988
Michael Chan566f86a2005-05-29 14:56:58 -070011989 memset(data, 0, sizeof(u64) * TG3_NUM_TEST);
11990
11991 if (tg3_test_nvram(tp) != 0) {
11992 etest->flags |= ETH_TEST_FL_FAILED;
11993 data[0] = 1;
11994 }
Matt Carlson941ec902011-08-19 13:58:23 +000011995 if (!doextlpbk && tg3_test_link(tp)) {
Michael Chanca430072005-05-29 14:57:23 -070011996 etest->flags |= ETH_TEST_FL_FAILED;
11997 data[1] = 1;
11998 }
Michael Chana71116d2005-05-29 14:58:11 -070011999 if (etest->flags & ETH_TEST_FL_OFFLINE) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012000 int err, err2 = 0, irq_sync = 0;
Michael Chana71116d2005-05-29 14:58:11 -070012001
Michael Chanbbe832c2005-06-24 20:20:04 -070012002 if (netif_running(dev)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012003 tg3_phy_stop(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -070012004 tg3_netif_stop(tp);
12005 irq_sync = 1;
12006 }
12007
12008 tg3_full_lock(tp, irq_sync);
Michael Chana71116d2005-05-29 14:58:11 -070012009
12010 tg3_halt(tp, RESET_KIND_SUSPEND, 1);
Michael Chanec41c7d2006-01-17 02:40:55 -080012011 err = tg3_nvram_lock(tp);
Michael Chana71116d2005-05-29 14:58:11 -070012012 tg3_halt_cpu(tp, RX_CPU_BASE);
Joe Perches63c3a662011-04-26 08:12:10 +000012013 if (!tg3_flag(tp, 5705_PLUS))
Michael Chana71116d2005-05-29 14:58:11 -070012014 tg3_halt_cpu(tp, TX_CPU_BASE);
Michael Chanec41c7d2006-01-17 02:40:55 -080012015 if (!err)
12016 tg3_nvram_unlock(tp);
Michael Chana71116d2005-05-29 14:58:11 -070012017
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012018 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
Michael Chand9ab5ad2006-03-20 22:27:35 -080012019 tg3_phy_reset(tp);
12020
Michael Chana71116d2005-05-29 14:58:11 -070012021 if (tg3_test_registers(tp) != 0) {
12022 etest->flags |= ETH_TEST_FL_FAILED;
12023 data[2] = 1;
12024 }
Matt Carlson28a45952011-08-19 13:58:22 +000012025
Michael Chan7942e1d2005-05-29 14:58:36 -070012026 if (tg3_test_memory(tp) != 0) {
12027 etest->flags |= ETH_TEST_FL_FAILED;
12028 data[3] = 1;
12029 }
Matt Carlson28a45952011-08-19 13:58:22 +000012030
Matt Carlson941ec902011-08-19 13:58:23 +000012031 if (doextlpbk)
12032 etest->flags |= ETH_TEST_FL_EXTERNAL_LB_DONE;
12033
12034 if (tg3_test_loopback(tp, &data[4], doextlpbk))
Michael Chanc76949a2005-05-29 14:58:59 -070012035 etest->flags |= ETH_TEST_FL_FAILED;
Michael Chana71116d2005-05-29 14:58:11 -070012036
David S. Millerf47c11e2005-06-24 20:18:35 -070012037 tg3_full_unlock(tp);
12038
Michael Chand4bc3922005-05-29 14:59:20 -070012039 if (tg3_test_interrupt(tp) != 0) {
12040 etest->flags |= ETH_TEST_FL_FAILED;
Matt Carlson941ec902011-08-19 13:58:23 +000012041 data[7] = 1;
Michael Chand4bc3922005-05-29 14:59:20 -070012042 }
David S. Millerf47c11e2005-06-24 20:18:35 -070012043
12044 tg3_full_lock(tp, 0);
Michael Chand4bc3922005-05-29 14:59:20 -070012045
Michael Chana71116d2005-05-29 14:58:11 -070012046 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
12047 if (netif_running(dev)) {
Joe Perches63c3a662011-04-26 08:12:10 +000012048 tg3_flag_set(tp, INIT_COMPLETE);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012049 err2 = tg3_restart_hw(tp, 1);
12050 if (!err2)
Michael Chanb9ec6c12006-07-25 16:37:27 -070012051 tg3_netif_start(tp);
Michael Chana71116d2005-05-29 14:58:11 -070012052 }
David S. Millerf47c11e2005-06-24 20:18:35 -070012053
12054 tg3_full_unlock(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012055
12056 if (irq_sync && !err2)
12057 tg3_phy_start(tp);
Michael Chana71116d2005-05-29 14:58:11 -070012058 }
Matt Carlson800960682010-08-02 11:26:06 +000012059 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000012060 tg3_power_down(tp);
Michael Chanbc1c7562006-03-20 17:48:03 -080012061
Michael Chan4cafd3f2005-05-29 14:56:34 -070012062}
12063
Linus Torvalds1da177e2005-04-16 15:20:36 -070012064static int tg3_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
12065{
12066 struct mii_ioctl_data *data = if_mii(ifr);
12067 struct tg3 *tp = netdev_priv(dev);
12068 int err;
12069
Joe Perches63c3a662011-04-26 08:12:10 +000012070 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000012071 struct phy_device *phydev;
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012072 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012073 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000012074 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Richard Cochran28b04112010-07-17 08:48:55 +000012075 return phy_mii_ioctl(phydev, ifr, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012076 }
12077
Matt Carlson33f401a2010-04-05 10:19:27 +000012078 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012079 case SIOCGMIIPHY:
Matt Carlson882e9792009-09-01 13:21:36 +000012080 data->phy_id = tp->phy_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012081
12082 /* fallthru */
12083 case SIOCGMIIREG: {
12084 u32 mii_regval;
12085
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012086 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012087 break; /* We have no PHY */
12088
Matt Carlson34eea5a2011-04-20 07:57:38 +000012089 if (!netif_running(dev))
Michael Chanbc1c7562006-03-20 17:48:03 -080012090 return -EAGAIN;
12091
David S. Millerf47c11e2005-06-24 20:18:35 -070012092 spin_lock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012093 err = tg3_readphy(tp, data->reg_num & 0x1f, &mii_regval);
David S. Millerf47c11e2005-06-24 20:18:35 -070012094 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012095
12096 data->val_out = mii_regval;
12097
12098 return err;
12099 }
12100
12101 case SIOCSMIIREG:
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012102 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012103 break; /* We have no PHY */
12104
Matt Carlson34eea5a2011-04-20 07:57:38 +000012105 if (!netif_running(dev))
Michael Chanbc1c7562006-03-20 17:48:03 -080012106 return -EAGAIN;
12107
David S. Millerf47c11e2005-06-24 20:18:35 -070012108 spin_lock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012109 err = tg3_writephy(tp, data->reg_num & 0x1f, data->val_in);
David S. Millerf47c11e2005-06-24 20:18:35 -070012110 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012111
12112 return err;
12113
12114 default:
12115 /* do nothing */
12116 break;
12117 }
12118 return -EOPNOTSUPP;
12119}
12120
David S. Miller15f98502005-05-18 22:49:26 -070012121static int tg3_get_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
12122{
12123 struct tg3 *tp = netdev_priv(dev);
12124
12125 memcpy(ec, &tp->coal, sizeof(*ec));
12126 return 0;
12127}
12128
Michael Chand244c892005-07-05 14:42:33 -070012129static int tg3_set_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
12130{
12131 struct tg3 *tp = netdev_priv(dev);
12132 u32 max_rxcoal_tick_int = 0, max_txcoal_tick_int = 0;
12133 u32 max_stat_coal_ticks = 0, min_stat_coal_ticks = 0;
12134
Joe Perches63c3a662011-04-26 08:12:10 +000012135 if (!tg3_flag(tp, 5705_PLUS)) {
Michael Chand244c892005-07-05 14:42:33 -070012136 max_rxcoal_tick_int = MAX_RXCOAL_TICK_INT;
12137 max_txcoal_tick_int = MAX_TXCOAL_TICK_INT;
12138 max_stat_coal_ticks = MAX_STAT_COAL_TICKS;
12139 min_stat_coal_ticks = MIN_STAT_COAL_TICKS;
12140 }
12141
12142 if ((ec->rx_coalesce_usecs > MAX_RXCOL_TICKS) ||
12143 (ec->tx_coalesce_usecs > MAX_TXCOL_TICKS) ||
12144 (ec->rx_max_coalesced_frames > MAX_RXMAX_FRAMES) ||
12145 (ec->tx_max_coalesced_frames > MAX_TXMAX_FRAMES) ||
12146 (ec->rx_coalesce_usecs_irq > max_rxcoal_tick_int) ||
12147 (ec->tx_coalesce_usecs_irq > max_txcoal_tick_int) ||
12148 (ec->rx_max_coalesced_frames_irq > MAX_RXCOAL_MAXF_INT) ||
12149 (ec->tx_max_coalesced_frames_irq > MAX_TXCOAL_MAXF_INT) ||
12150 (ec->stats_block_coalesce_usecs > max_stat_coal_ticks) ||
12151 (ec->stats_block_coalesce_usecs < min_stat_coal_ticks))
12152 return -EINVAL;
12153
12154 /* No rx interrupts will be generated if both are zero */
12155 if ((ec->rx_coalesce_usecs == 0) &&
12156 (ec->rx_max_coalesced_frames == 0))
12157 return -EINVAL;
12158
12159 /* No tx interrupts will be generated if both are zero */
12160 if ((ec->tx_coalesce_usecs == 0) &&
12161 (ec->tx_max_coalesced_frames == 0))
12162 return -EINVAL;
12163
12164 /* Only copy relevant parameters, ignore all others. */
12165 tp->coal.rx_coalesce_usecs = ec->rx_coalesce_usecs;
12166 tp->coal.tx_coalesce_usecs = ec->tx_coalesce_usecs;
12167 tp->coal.rx_max_coalesced_frames = ec->rx_max_coalesced_frames;
12168 tp->coal.tx_max_coalesced_frames = ec->tx_max_coalesced_frames;
12169 tp->coal.rx_coalesce_usecs_irq = ec->rx_coalesce_usecs_irq;
12170 tp->coal.tx_coalesce_usecs_irq = ec->tx_coalesce_usecs_irq;
12171 tp->coal.rx_max_coalesced_frames_irq = ec->rx_max_coalesced_frames_irq;
12172 tp->coal.tx_max_coalesced_frames_irq = ec->tx_max_coalesced_frames_irq;
12173 tp->coal.stats_block_coalesce_usecs = ec->stats_block_coalesce_usecs;
12174
12175 if (netif_running(dev)) {
12176 tg3_full_lock(tp, 0);
12177 __tg3_set_coalesce(tp, &tp->coal);
12178 tg3_full_unlock(tp);
12179 }
12180 return 0;
12181}
12182
Jeff Garzik7282d492006-09-13 14:30:00 -040012183static const struct ethtool_ops tg3_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012184 .get_settings = tg3_get_settings,
12185 .set_settings = tg3_set_settings,
12186 .get_drvinfo = tg3_get_drvinfo,
12187 .get_regs_len = tg3_get_regs_len,
12188 .get_regs = tg3_get_regs,
12189 .get_wol = tg3_get_wol,
12190 .set_wol = tg3_set_wol,
12191 .get_msglevel = tg3_get_msglevel,
12192 .set_msglevel = tg3_set_msglevel,
12193 .nway_reset = tg3_nway_reset,
12194 .get_link = ethtool_op_get_link,
12195 .get_eeprom_len = tg3_get_eeprom_len,
12196 .get_eeprom = tg3_get_eeprom,
12197 .set_eeprom = tg3_set_eeprom,
12198 .get_ringparam = tg3_get_ringparam,
12199 .set_ringparam = tg3_set_ringparam,
12200 .get_pauseparam = tg3_get_pauseparam,
12201 .set_pauseparam = tg3_set_pauseparam,
Michael Chan4cafd3f2005-05-29 14:56:34 -070012202 .self_test = tg3_self_test,
Linus Torvalds1da177e2005-04-16 15:20:36 -070012203 .get_strings = tg3_get_strings,
stephen hemminger81b87092011-04-04 08:43:50 +000012204 .set_phys_id = tg3_set_phys_id,
Linus Torvalds1da177e2005-04-16 15:20:36 -070012205 .get_ethtool_stats = tg3_get_ethtool_stats,
David S. Miller15f98502005-05-18 22:49:26 -070012206 .get_coalesce = tg3_get_coalesce,
Michael Chand244c892005-07-05 14:42:33 -070012207 .set_coalesce = tg3_set_coalesce,
Jeff Garzikb9f2c042007-10-03 18:07:32 -070012208 .get_sset_count = tg3_get_sset_count,
Matt Carlson90415472011-12-16 13:33:23 +000012209 .get_rxnfc = tg3_get_rxnfc,
12210 .get_rxfh_indir_size = tg3_get_rxfh_indir_size,
12211 .get_rxfh_indir = tg3_get_rxfh_indir,
12212 .set_rxfh_indir = tg3_set_rxfh_indir,
Linus Torvalds1da177e2005-04-16 15:20:36 -070012213};
12214
Matt Carlsonccd5ba92012-02-13 10:20:08 +000012215static void tg3_set_rx_mode(struct net_device *dev)
12216{
12217 struct tg3 *tp = netdev_priv(dev);
12218
12219 if (!netif_running(dev))
12220 return;
12221
12222 tg3_full_lock(tp, 0);
12223 __tg3_set_rx_mode(dev);
12224 tg3_full_unlock(tp);
12225}
12226
Matt Carlsonfaf16272012-02-13 10:20:07 +000012227static inline void tg3_set_mtu(struct net_device *dev, struct tg3 *tp,
12228 int new_mtu)
12229{
12230 dev->mtu = new_mtu;
12231
12232 if (new_mtu > ETH_DATA_LEN) {
12233 if (tg3_flag(tp, 5780_CLASS)) {
12234 netdev_update_features(dev);
12235 tg3_flag_clear(tp, TSO_CAPABLE);
12236 } else {
12237 tg3_flag_set(tp, JUMBO_RING_ENABLE);
12238 }
12239 } else {
12240 if (tg3_flag(tp, 5780_CLASS)) {
12241 tg3_flag_set(tp, TSO_CAPABLE);
12242 netdev_update_features(dev);
12243 }
12244 tg3_flag_clear(tp, JUMBO_RING_ENABLE);
12245 }
12246}
12247
12248static int tg3_change_mtu(struct net_device *dev, int new_mtu)
12249{
12250 struct tg3 *tp = netdev_priv(dev);
12251 int err;
12252
12253 if (new_mtu < TG3_MIN_MTU || new_mtu > TG3_MAX_MTU(tp))
12254 return -EINVAL;
12255
12256 if (!netif_running(dev)) {
12257 /* We'll just catch it later when the
12258 * device is up'd.
12259 */
12260 tg3_set_mtu(dev, tp, new_mtu);
12261 return 0;
12262 }
12263
12264 tg3_phy_stop(tp);
12265
12266 tg3_netif_stop(tp);
12267
12268 tg3_full_lock(tp, 1);
12269
12270 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
12271
12272 tg3_set_mtu(dev, tp, new_mtu);
12273
12274 err = tg3_restart_hw(tp, 0);
12275
12276 if (!err)
12277 tg3_netif_start(tp);
12278
12279 tg3_full_unlock(tp);
12280
12281 if (!err)
12282 tg3_phy_start(tp);
12283
12284 return err;
12285}
12286
12287static const struct net_device_ops tg3_netdev_ops = {
12288 .ndo_open = tg3_open,
12289 .ndo_stop = tg3_close,
12290 .ndo_start_xmit = tg3_start_xmit,
12291 .ndo_get_stats64 = tg3_get_stats64,
12292 .ndo_validate_addr = eth_validate_addr,
12293 .ndo_set_rx_mode = tg3_set_rx_mode,
12294 .ndo_set_mac_address = tg3_set_mac_addr,
12295 .ndo_do_ioctl = tg3_ioctl,
12296 .ndo_tx_timeout = tg3_tx_timeout,
12297 .ndo_change_mtu = tg3_change_mtu,
12298 .ndo_fix_features = tg3_fix_features,
12299 .ndo_set_features = tg3_set_features,
12300#ifdef CONFIG_NET_POLL_CONTROLLER
12301 .ndo_poll_controller = tg3_poll_controller,
12302#endif
12303};
12304
Linus Torvalds1da177e2005-04-16 15:20:36 -070012305static void __devinit tg3_get_eeprom_size(struct tg3 *tp)
12306{
Michael Chan1b277772006-03-20 22:27:48 -080012307 u32 cursize, val, magic;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012308
12309 tp->nvram_size = EEPROM_CHIP_SIZE;
12310
Matt Carlsone4f34112009-02-25 14:25:00 +000012311 if (tg3_nvram_read(tp, 0, &magic) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012312 return;
12313
Michael Chanb16250e2006-09-27 16:10:14 -070012314 if ((magic != TG3_EEPROM_MAGIC) &&
12315 ((magic & TG3_EEPROM_MAGIC_FW_MSK) != TG3_EEPROM_MAGIC_FW) &&
12316 ((magic & TG3_EEPROM_MAGIC_HW_MSK) != TG3_EEPROM_MAGIC_HW))
Linus Torvalds1da177e2005-04-16 15:20:36 -070012317 return;
12318
12319 /*
12320 * Size the chip by reading offsets at increasing powers of two.
12321 * When we encounter our validation signature, we know the addressing
12322 * has wrapped around, and thus have our chip size.
12323 */
Michael Chan1b277772006-03-20 22:27:48 -080012324 cursize = 0x10;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012325
12326 while (cursize < tp->nvram_size) {
Matt Carlsone4f34112009-02-25 14:25:00 +000012327 if (tg3_nvram_read(tp, cursize, &val) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012328 return;
12329
Michael Chan18201802006-03-20 22:29:15 -080012330 if (val == magic)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012331 break;
12332
12333 cursize <<= 1;
12334 }
12335
12336 tp->nvram_size = cursize;
12337}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012338
Linus Torvalds1da177e2005-04-16 15:20:36 -070012339static void __devinit tg3_get_nvram_size(struct tg3 *tp)
12340{
12341 u32 val;
12342
Joe Perches63c3a662011-04-26 08:12:10 +000012343 if (tg3_flag(tp, NO_NVRAM) || tg3_nvram_read(tp, 0, &val) != 0)
Michael Chan1b277772006-03-20 22:27:48 -080012344 return;
12345
12346 /* Selfboot format */
Michael Chan18201802006-03-20 22:29:15 -080012347 if (val != TG3_EEPROM_MAGIC) {
Michael Chan1b277772006-03-20 22:27:48 -080012348 tg3_get_eeprom_size(tp);
12349 return;
12350 }
12351
Matt Carlson6d348f22009-02-25 14:25:52 +000012352 if (tg3_nvram_read(tp, 0xf0, &val) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012353 if (val != 0) {
Matt Carlson6d348f22009-02-25 14:25:52 +000012354 /* This is confusing. We want to operate on the
12355 * 16-bit value at offset 0xf2. The tg3_nvram_read()
12356 * call will read from NVRAM and byteswap the data
12357 * according to the byteswapping settings for all
12358 * other register accesses. This ensures the data we
12359 * want will always reside in the lower 16-bits.
12360 * However, the data in NVRAM is in LE format, which
12361 * means the data from the NVRAM read will always be
12362 * opposite the endianness of the CPU. The 16-bit
12363 * byteswap then brings the data to CPU endianness.
12364 */
12365 tp->nvram_size = swab16((u16)(val & 0x0000ffff)) * 1024;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012366 return;
12367 }
12368 }
Matt Carlsonfd1122a2008-05-02 16:48:36 -070012369 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012370}
12371
12372static void __devinit tg3_get_nvram_info(struct tg3 *tp)
12373{
12374 u32 nvcfg1;
12375
12376 nvcfg1 = tr32(NVRAM_CFG1);
12377 if (nvcfg1 & NVRAM_CFG1_FLASHIF_ENAB) {
Joe Perches63c3a662011-04-26 08:12:10 +000012378 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000012379 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012380 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
12381 tw32(NVRAM_CFG1, nvcfg1);
12382 }
12383
Matt Carlson6ff6f812011-05-19 12:12:54 +000012384 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 ||
Joe Perches63c3a662011-04-26 08:12:10 +000012385 tg3_flag(tp, 5780_CLASS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012386 switch (nvcfg1 & NVRAM_CFG1_VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000012387 case FLASH_VENDOR_ATMEL_FLASH_BUFFERED:
12388 tp->nvram_jedecnum = JEDEC_ATMEL;
12389 tp->nvram_pagesize = ATMEL_AT45DB0X1B_PAGE_SIZE;
Joe Perches63c3a662011-04-26 08:12:10 +000012390 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000012391 break;
12392 case FLASH_VENDOR_ATMEL_FLASH_UNBUFFERED:
12393 tp->nvram_jedecnum = JEDEC_ATMEL;
12394 tp->nvram_pagesize = ATMEL_AT25F512_PAGE_SIZE;
12395 break;
12396 case FLASH_VENDOR_ATMEL_EEPROM:
12397 tp->nvram_jedecnum = JEDEC_ATMEL;
12398 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
Joe Perches63c3a662011-04-26 08:12:10 +000012399 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000012400 break;
12401 case FLASH_VENDOR_ST:
12402 tp->nvram_jedecnum = JEDEC_ST;
12403 tp->nvram_pagesize = ST_M45PEX0_PAGE_SIZE;
Joe Perches63c3a662011-04-26 08:12:10 +000012404 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000012405 break;
12406 case FLASH_VENDOR_SAIFUN:
12407 tp->nvram_jedecnum = JEDEC_SAIFUN;
12408 tp->nvram_pagesize = SAIFUN_SA25F0XX_PAGE_SIZE;
12409 break;
12410 case FLASH_VENDOR_SST_SMALL:
12411 case FLASH_VENDOR_SST_LARGE:
12412 tp->nvram_jedecnum = JEDEC_SST;
12413 tp->nvram_pagesize = SST_25VF0X0_PAGE_SIZE;
12414 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012415 }
Matt Carlson8590a602009-08-28 12:29:16 +000012416 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012417 tp->nvram_jedecnum = JEDEC_ATMEL;
12418 tp->nvram_pagesize = ATMEL_AT45DB0X1B_PAGE_SIZE;
Joe Perches63c3a662011-04-26 08:12:10 +000012419 tg3_flag_set(tp, NVRAM_BUFFERED);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012420 }
12421}
12422
Matt Carlsona1b950d2009-09-01 13:20:17 +000012423static void __devinit tg3_nvram_get_pagesize(struct tg3 *tp, u32 nvmcfg1)
12424{
12425 switch (nvmcfg1 & NVRAM_CFG1_5752PAGE_SIZE_MASK) {
12426 case FLASH_5752PAGE_SIZE_256:
12427 tp->nvram_pagesize = 256;
12428 break;
12429 case FLASH_5752PAGE_SIZE_512:
12430 tp->nvram_pagesize = 512;
12431 break;
12432 case FLASH_5752PAGE_SIZE_1K:
12433 tp->nvram_pagesize = 1024;
12434 break;
12435 case FLASH_5752PAGE_SIZE_2K:
12436 tp->nvram_pagesize = 2048;
12437 break;
12438 case FLASH_5752PAGE_SIZE_4K:
12439 tp->nvram_pagesize = 4096;
12440 break;
12441 case FLASH_5752PAGE_SIZE_264:
12442 tp->nvram_pagesize = 264;
12443 break;
12444 case FLASH_5752PAGE_SIZE_528:
12445 tp->nvram_pagesize = 528;
12446 break;
12447 }
12448}
12449
Michael Chan361b4ac2005-04-21 17:11:21 -070012450static void __devinit tg3_get_5752_nvram_info(struct tg3 *tp)
12451{
12452 u32 nvcfg1;
12453
12454 nvcfg1 = tr32(NVRAM_CFG1);
12455
Michael Chane6af3012005-04-21 17:12:05 -070012456 /* NVRAM protection for TPM */
12457 if (nvcfg1 & (1 << 27))
Joe Perches63c3a662011-04-26 08:12:10 +000012458 tg3_flag_set(tp, PROTECTED_NVRAM);
Michael Chane6af3012005-04-21 17:12:05 -070012459
Michael Chan361b4ac2005-04-21 17:11:21 -070012460 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000012461 case FLASH_5752VENDOR_ATMEL_EEPROM_64KHZ:
12462 case FLASH_5752VENDOR_ATMEL_EEPROM_376KHZ:
12463 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012464 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000012465 break;
12466 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
12467 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012468 tg3_flag_set(tp, NVRAM_BUFFERED);
12469 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000012470 break;
12471 case FLASH_5752VENDOR_ST_M45PE10:
12472 case FLASH_5752VENDOR_ST_M45PE20:
12473 case FLASH_5752VENDOR_ST_M45PE40:
12474 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000012475 tg3_flag_set(tp, NVRAM_BUFFERED);
12476 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000012477 break;
Michael Chan361b4ac2005-04-21 17:11:21 -070012478 }
12479
Joe Perches63c3a662011-04-26 08:12:10 +000012480 if (tg3_flag(tp, FLASH)) {
Matt Carlsona1b950d2009-09-01 13:20:17 +000012481 tg3_nvram_get_pagesize(tp, nvcfg1);
Matt Carlson8590a602009-08-28 12:29:16 +000012482 } else {
Michael Chan361b4ac2005-04-21 17:11:21 -070012483 /* For eeprom, set pagesize to maximum eeprom size */
12484 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
12485
12486 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
12487 tw32(NVRAM_CFG1, nvcfg1);
12488 }
12489}
12490
Michael Chand3c7b882006-03-23 01:28:25 -080012491static void __devinit tg3_get_5755_nvram_info(struct tg3 *tp)
12492{
Matt Carlson989a9d22007-05-05 11:51:05 -070012493 u32 nvcfg1, protect = 0;
Michael Chand3c7b882006-03-23 01:28:25 -080012494
12495 nvcfg1 = tr32(NVRAM_CFG1);
12496
12497 /* NVRAM protection for TPM */
Matt Carlson989a9d22007-05-05 11:51:05 -070012498 if (nvcfg1 & (1 << 27)) {
Joe Perches63c3a662011-04-26 08:12:10 +000012499 tg3_flag_set(tp, PROTECTED_NVRAM);
Matt Carlson989a9d22007-05-05 11:51:05 -070012500 protect = 1;
12501 }
Michael Chand3c7b882006-03-23 01:28:25 -080012502
Matt Carlson989a9d22007-05-05 11:51:05 -070012503 nvcfg1 &= NVRAM_CFG1_5752VENDOR_MASK;
12504 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000012505 case FLASH_5755VENDOR_ATMEL_FLASH_1:
12506 case FLASH_5755VENDOR_ATMEL_FLASH_2:
12507 case FLASH_5755VENDOR_ATMEL_FLASH_3:
12508 case FLASH_5755VENDOR_ATMEL_FLASH_5:
12509 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012510 tg3_flag_set(tp, NVRAM_BUFFERED);
12511 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000012512 tp->nvram_pagesize = 264;
12513 if (nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_1 ||
12514 nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_5)
12515 tp->nvram_size = (protect ? 0x3e200 :
12516 TG3_NVRAM_SIZE_512KB);
12517 else if (nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_2)
12518 tp->nvram_size = (protect ? 0x1f200 :
12519 TG3_NVRAM_SIZE_256KB);
12520 else
12521 tp->nvram_size = (protect ? 0x1f200 :
12522 TG3_NVRAM_SIZE_128KB);
12523 break;
12524 case FLASH_5752VENDOR_ST_M45PE10:
12525 case FLASH_5752VENDOR_ST_M45PE20:
12526 case FLASH_5752VENDOR_ST_M45PE40:
12527 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000012528 tg3_flag_set(tp, NVRAM_BUFFERED);
12529 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000012530 tp->nvram_pagesize = 256;
12531 if (nvcfg1 == FLASH_5752VENDOR_ST_M45PE10)
12532 tp->nvram_size = (protect ?
12533 TG3_NVRAM_SIZE_64KB :
12534 TG3_NVRAM_SIZE_128KB);
12535 else if (nvcfg1 == FLASH_5752VENDOR_ST_M45PE20)
12536 tp->nvram_size = (protect ?
12537 TG3_NVRAM_SIZE_64KB :
12538 TG3_NVRAM_SIZE_256KB);
12539 else
12540 tp->nvram_size = (protect ?
12541 TG3_NVRAM_SIZE_128KB :
12542 TG3_NVRAM_SIZE_512KB);
12543 break;
Michael Chand3c7b882006-03-23 01:28:25 -080012544 }
12545}
12546
Michael Chan1b277772006-03-20 22:27:48 -080012547static void __devinit tg3_get_5787_nvram_info(struct tg3 *tp)
12548{
12549 u32 nvcfg1;
12550
12551 nvcfg1 = tr32(NVRAM_CFG1);
12552
12553 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000012554 case FLASH_5787VENDOR_ATMEL_EEPROM_64KHZ:
12555 case FLASH_5787VENDOR_ATMEL_EEPROM_376KHZ:
12556 case FLASH_5787VENDOR_MICRO_EEPROM_64KHZ:
12557 case FLASH_5787VENDOR_MICRO_EEPROM_376KHZ:
12558 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012559 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000012560 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
Michael Chan1b277772006-03-20 22:27:48 -080012561
Matt Carlson8590a602009-08-28 12:29:16 +000012562 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
12563 tw32(NVRAM_CFG1, nvcfg1);
12564 break;
12565 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
12566 case FLASH_5755VENDOR_ATMEL_FLASH_1:
12567 case FLASH_5755VENDOR_ATMEL_FLASH_2:
12568 case FLASH_5755VENDOR_ATMEL_FLASH_3:
12569 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012570 tg3_flag_set(tp, NVRAM_BUFFERED);
12571 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000012572 tp->nvram_pagesize = 264;
12573 break;
12574 case FLASH_5752VENDOR_ST_M45PE10:
12575 case FLASH_5752VENDOR_ST_M45PE20:
12576 case FLASH_5752VENDOR_ST_M45PE40:
12577 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000012578 tg3_flag_set(tp, NVRAM_BUFFERED);
12579 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000012580 tp->nvram_pagesize = 256;
12581 break;
Michael Chan1b277772006-03-20 22:27:48 -080012582 }
12583}
12584
Matt Carlson6b91fa02007-10-10 18:01:09 -070012585static void __devinit tg3_get_5761_nvram_info(struct tg3 *tp)
12586{
12587 u32 nvcfg1, protect = 0;
12588
12589 nvcfg1 = tr32(NVRAM_CFG1);
12590
12591 /* NVRAM protection for TPM */
12592 if (nvcfg1 & (1 << 27)) {
Joe Perches63c3a662011-04-26 08:12:10 +000012593 tg3_flag_set(tp, PROTECTED_NVRAM);
Matt Carlson6b91fa02007-10-10 18:01:09 -070012594 protect = 1;
12595 }
12596
12597 nvcfg1 &= NVRAM_CFG1_5752VENDOR_MASK;
12598 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000012599 case FLASH_5761VENDOR_ATMEL_ADB021D:
12600 case FLASH_5761VENDOR_ATMEL_ADB041D:
12601 case FLASH_5761VENDOR_ATMEL_ADB081D:
12602 case FLASH_5761VENDOR_ATMEL_ADB161D:
12603 case FLASH_5761VENDOR_ATMEL_MDB021D:
12604 case FLASH_5761VENDOR_ATMEL_MDB041D:
12605 case FLASH_5761VENDOR_ATMEL_MDB081D:
12606 case FLASH_5761VENDOR_ATMEL_MDB161D:
12607 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012608 tg3_flag_set(tp, NVRAM_BUFFERED);
12609 tg3_flag_set(tp, FLASH);
12610 tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
Matt Carlson8590a602009-08-28 12:29:16 +000012611 tp->nvram_pagesize = 256;
12612 break;
12613 case FLASH_5761VENDOR_ST_A_M45PE20:
12614 case FLASH_5761VENDOR_ST_A_M45PE40:
12615 case FLASH_5761VENDOR_ST_A_M45PE80:
12616 case FLASH_5761VENDOR_ST_A_M45PE16:
12617 case FLASH_5761VENDOR_ST_M_M45PE20:
12618 case FLASH_5761VENDOR_ST_M_M45PE40:
12619 case FLASH_5761VENDOR_ST_M_M45PE80:
12620 case FLASH_5761VENDOR_ST_M_M45PE16:
12621 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000012622 tg3_flag_set(tp, NVRAM_BUFFERED);
12623 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000012624 tp->nvram_pagesize = 256;
12625 break;
Matt Carlson6b91fa02007-10-10 18:01:09 -070012626 }
12627
12628 if (protect) {
12629 tp->nvram_size = tr32(NVRAM_ADDR_LOCKOUT);
12630 } else {
12631 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000012632 case FLASH_5761VENDOR_ATMEL_ADB161D:
12633 case FLASH_5761VENDOR_ATMEL_MDB161D:
12634 case FLASH_5761VENDOR_ST_A_M45PE16:
12635 case FLASH_5761VENDOR_ST_M_M45PE16:
12636 tp->nvram_size = TG3_NVRAM_SIZE_2MB;
12637 break;
12638 case FLASH_5761VENDOR_ATMEL_ADB081D:
12639 case FLASH_5761VENDOR_ATMEL_MDB081D:
12640 case FLASH_5761VENDOR_ST_A_M45PE80:
12641 case FLASH_5761VENDOR_ST_M_M45PE80:
12642 tp->nvram_size = TG3_NVRAM_SIZE_1MB;
12643 break;
12644 case FLASH_5761VENDOR_ATMEL_ADB041D:
12645 case FLASH_5761VENDOR_ATMEL_MDB041D:
12646 case FLASH_5761VENDOR_ST_A_M45PE40:
12647 case FLASH_5761VENDOR_ST_M_M45PE40:
12648 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
12649 break;
12650 case FLASH_5761VENDOR_ATMEL_ADB021D:
12651 case FLASH_5761VENDOR_ATMEL_MDB021D:
12652 case FLASH_5761VENDOR_ST_A_M45PE20:
12653 case FLASH_5761VENDOR_ST_M_M45PE20:
12654 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
12655 break;
Matt Carlson6b91fa02007-10-10 18:01:09 -070012656 }
12657 }
12658}
12659
Michael Chanb5d37722006-09-27 16:06:21 -070012660static void __devinit tg3_get_5906_nvram_info(struct tg3 *tp)
12661{
12662 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012663 tg3_flag_set(tp, NVRAM_BUFFERED);
Michael Chanb5d37722006-09-27 16:06:21 -070012664 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
12665}
12666
Matt Carlson321d32a2008-11-21 17:22:19 -080012667static void __devinit tg3_get_57780_nvram_info(struct tg3 *tp)
12668{
12669 u32 nvcfg1;
12670
12671 nvcfg1 = tr32(NVRAM_CFG1);
12672
12673 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
12674 case FLASH_5787VENDOR_ATMEL_EEPROM_376KHZ:
12675 case FLASH_5787VENDOR_MICRO_EEPROM_376KHZ:
12676 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012677 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson321d32a2008-11-21 17:22:19 -080012678 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
12679
12680 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
12681 tw32(NVRAM_CFG1, nvcfg1);
12682 return;
12683 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
12684 case FLASH_57780VENDOR_ATMEL_AT45DB011D:
12685 case FLASH_57780VENDOR_ATMEL_AT45DB011B:
12686 case FLASH_57780VENDOR_ATMEL_AT45DB021D:
12687 case FLASH_57780VENDOR_ATMEL_AT45DB021B:
12688 case FLASH_57780VENDOR_ATMEL_AT45DB041D:
12689 case FLASH_57780VENDOR_ATMEL_AT45DB041B:
12690 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012691 tg3_flag_set(tp, NVRAM_BUFFERED);
12692 tg3_flag_set(tp, FLASH);
Matt Carlson321d32a2008-11-21 17:22:19 -080012693
12694 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
12695 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
12696 case FLASH_57780VENDOR_ATMEL_AT45DB011D:
12697 case FLASH_57780VENDOR_ATMEL_AT45DB011B:
12698 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
12699 break;
12700 case FLASH_57780VENDOR_ATMEL_AT45DB021D:
12701 case FLASH_57780VENDOR_ATMEL_AT45DB021B:
12702 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
12703 break;
12704 case FLASH_57780VENDOR_ATMEL_AT45DB041D:
12705 case FLASH_57780VENDOR_ATMEL_AT45DB041B:
12706 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
12707 break;
12708 }
12709 break;
12710 case FLASH_5752VENDOR_ST_M45PE10:
12711 case FLASH_5752VENDOR_ST_M45PE20:
12712 case FLASH_5752VENDOR_ST_M45PE40:
12713 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000012714 tg3_flag_set(tp, NVRAM_BUFFERED);
12715 tg3_flag_set(tp, FLASH);
Matt Carlson321d32a2008-11-21 17:22:19 -080012716
12717 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
12718 case FLASH_5752VENDOR_ST_M45PE10:
12719 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
12720 break;
12721 case FLASH_5752VENDOR_ST_M45PE20:
12722 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
12723 break;
12724 case FLASH_5752VENDOR_ST_M45PE40:
12725 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
12726 break;
12727 }
12728 break;
12729 default:
Joe Perches63c3a662011-04-26 08:12:10 +000012730 tg3_flag_set(tp, NO_NVRAM);
Matt Carlson321d32a2008-11-21 17:22:19 -080012731 return;
12732 }
12733
Matt Carlsona1b950d2009-09-01 13:20:17 +000012734 tg3_nvram_get_pagesize(tp, nvcfg1);
12735 if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
Joe Perches63c3a662011-04-26 08:12:10 +000012736 tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
Matt Carlsona1b950d2009-09-01 13:20:17 +000012737}
12738
12739
12740static void __devinit tg3_get_5717_nvram_info(struct tg3 *tp)
12741{
12742 u32 nvcfg1;
12743
12744 nvcfg1 = tr32(NVRAM_CFG1);
12745
12746 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
12747 case FLASH_5717VENDOR_ATMEL_EEPROM:
12748 case FLASH_5717VENDOR_MICRO_EEPROM:
12749 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012750 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlsona1b950d2009-09-01 13:20:17 +000012751 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
12752
12753 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
12754 tw32(NVRAM_CFG1, nvcfg1);
12755 return;
12756 case FLASH_5717VENDOR_ATMEL_MDB011D:
12757 case FLASH_5717VENDOR_ATMEL_ADB011B:
12758 case FLASH_5717VENDOR_ATMEL_ADB011D:
12759 case FLASH_5717VENDOR_ATMEL_MDB021D:
12760 case FLASH_5717VENDOR_ATMEL_ADB021B:
12761 case FLASH_5717VENDOR_ATMEL_ADB021D:
12762 case FLASH_5717VENDOR_ATMEL_45USPT:
12763 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012764 tg3_flag_set(tp, NVRAM_BUFFERED);
12765 tg3_flag_set(tp, FLASH);
Matt Carlsona1b950d2009-09-01 13:20:17 +000012766
12767 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
12768 case FLASH_5717VENDOR_ATMEL_MDB021D:
Matt Carlson66ee33b2011-04-05 14:22:51 +000012769 /* Detect size with tg3_nvram_get_size() */
12770 break;
Matt Carlsona1b950d2009-09-01 13:20:17 +000012771 case FLASH_5717VENDOR_ATMEL_ADB021B:
12772 case FLASH_5717VENDOR_ATMEL_ADB021D:
12773 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
12774 break;
12775 default:
12776 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
12777 break;
12778 }
Matt Carlson321d32a2008-11-21 17:22:19 -080012779 break;
Matt Carlsona1b950d2009-09-01 13:20:17 +000012780 case FLASH_5717VENDOR_ST_M_M25PE10:
12781 case FLASH_5717VENDOR_ST_A_M25PE10:
12782 case FLASH_5717VENDOR_ST_M_M45PE10:
12783 case FLASH_5717VENDOR_ST_A_M45PE10:
12784 case FLASH_5717VENDOR_ST_M_M25PE20:
12785 case FLASH_5717VENDOR_ST_A_M25PE20:
12786 case FLASH_5717VENDOR_ST_M_M45PE20:
12787 case FLASH_5717VENDOR_ST_A_M45PE20:
12788 case FLASH_5717VENDOR_ST_25USPT:
12789 case FLASH_5717VENDOR_ST_45USPT:
12790 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000012791 tg3_flag_set(tp, NVRAM_BUFFERED);
12792 tg3_flag_set(tp, FLASH);
Matt Carlsona1b950d2009-09-01 13:20:17 +000012793
12794 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
12795 case FLASH_5717VENDOR_ST_M_M25PE20:
Matt Carlsona1b950d2009-09-01 13:20:17 +000012796 case FLASH_5717VENDOR_ST_M_M45PE20:
Matt Carlson66ee33b2011-04-05 14:22:51 +000012797 /* Detect size with tg3_nvram_get_size() */
12798 break;
12799 case FLASH_5717VENDOR_ST_A_M25PE20:
Matt Carlsona1b950d2009-09-01 13:20:17 +000012800 case FLASH_5717VENDOR_ST_A_M45PE20:
12801 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
12802 break;
12803 default:
12804 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
12805 break;
12806 }
Matt Carlson321d32a2008-11-21 17:22:19 -080012807 break;
Matt Carlsona1b950d2009-09-01 13:20:17 +000012808 default:
Joe Perches63c3a662011-04-26 08:12:10 +000012809 tg3_flag_set(tp, NO_NVRAM);
Matt Carlsona1b950d2009-09-01 13:20:17 +000012810 return;
Matt Carlson321d32a2008-11-21 17:22:19 -080012811 }
Matt Carlsona1b950d2009-09-01 13:20:17 +000012812
12813 tg3_nvram_get_pagesize(tp, nvcfg1);
12814 if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
Joe Perches63c3a662011-04-26 08:12:10 +000012815 tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
Matt Carlson321d32a2008-11-21 17:22:19 -080012816}
12817
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012818static void __devinit tg3_get_5720_nvram_info(struct tg3 *tp)
12819{
12820 u32 nvcfg1, nvmpinstrp;
12821
12822 nvcfg1 = tr32(NVRAM_CFG1);
12823 nvmpinstrp = nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK;
12824
12825 switch (nvmpinstrp) {
12826 case FLASH_5720_EEPROM_HD:
12827 case FLASH_5720_EEPROM_LD:
12828 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012829 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012830
12831 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
12832 tw32(NVRAM_CFG1, nvcfg1);
12833 if (nvmpinstrp == FLASH_5720_EEPROM_HD)
12834 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
12835 else
12836 tp->nvram_pagesize = ATMEL_AT24C02_CHIP_SIZE;
12837 return;
12838 case FLASH_5720VENDOR_M_ATMEL_DB011D:
12839 case FLASH_5720VENDOR_A_ATMEL_DB011B:
12840 case FLASH_5720VENDOR_A_ATMEL_DB011D:
12841 case FLASH_5720VENDOR_M_ATMEL_DB021D:
12842 case FLASH_5720VENDOR_A_ATMEL_DB021B:
12843 case FLASH_5720VENDOR_A_ATMEL_DB021D:
12844 case FLASH_5720VENDOR_M_ATMEL_DB041D:
12845 case FLASH_5720VENDOR_A_ATMEL_DB041B:
12846 case FLASH_5720VENDOR_A_ATMEL_DB041D:
12847 case FLASH_5720VENDOR_M_ATMEL_DB081D:
12848 case FLASH_5720VENDOR_A_ATMEL_DB081D:
12849 case FLASH_5720VENDOR_ATMEL_45USPT:
12850 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012851 tg3_flag_set(tp, NVRAM_BUFFERED);
12852 tg3_flag_set(tp, FLASH);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012853
12854 switch (nvmpinstrp) {
12855 case FLASH_5720VENDOR_M_ATMEL_DB021D:
12856 case FLASH_5720VENDOR_A_ATMEL_DB021B:
12857 case FLASH_5720VENDOR_A_ATMEL_DB021D:
12858 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
12859 break;
12860 case FLASH_5720VENDOR_M_ATMEL_DB041D:
12861 case FLASH_5720VENDOR_A_ATMEL_DB041B:
12862 case FLASH_5720VENDOR_A_ATMEL_DB041D:
12863 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
12864 break;
12865 case FLASH_5720VENDOR_M_ATMEL_DB081D:
12866 case FLASH_5720VENDOR_A_ATMEL_DB081D:
12867 tp->nvram_size = TG3_NVRAM_SIZE_1MB;
12868 break;
12869 default:
12870 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
12871 break;
12872 }
12873 break;
12874 case FLASH_5720VENDOR_M_ST_M25PE10:
12875 case FLASH_5720VENDOR_M_ST_M45PE10:
12876 case FLASH_5720VENDOR_A_ST_M25PE10:
12877 case FLASH_5720VENDOR_A_ST_M45PE10:
12878 case FLASH_5720VENDOR_M_ST_M25PE20:
12879 case FLASH_5720VENDOR_M_ST_M45PE20:
12880 case FLASH_5720VENDOR_A_ST_M25PE20:
12881 case FLASH_5720VENDOR_A_ST_M45PE20:
12882 case FLASH_5720VENDOR_M_ST_M25PE40:
12883 case FLASH_5720VENDOR_M_ST_M45PE40:
12884 case FLASH_5720VENDOR_A_ST_M25PE40:
12885 case FLASH_5720VENDOR_A_ST_M45PE40:
12886 case FLASH_5720VENDOR_M_ST_M25PE80:
12887 case FLASH_5720VENDOR_M_ST_M45PE80:
12888 case FLASH_5720VENDOR_A_ST_M25PE80:
12889 case FLASH_5720VENDOR_A_ST_M45PE80:
12890 case FLASH_5720VENDOR_ST_25USPT:
12891 case FLASH_5720VENDOR_ST_45USPT:
12892 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000012893 tg3_flag_set(tp, NVRAM_BUFFERED);
12894 tg3_flag_set(tp, FLASH);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012895
12896 switch (nvmpinstrp) {
12897 case FLASH_5720VENDOR_M_ST_M25PE20:
12898 case FLASH_5720VENDOR_M_ST_M45PE20:
12899 case FLASH_5720VENDOR_A_ST_M25PE20:
12900 case FLASH_5720VENDOR_A_ST_M45PE20:
12901 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
12902 break;
12903 case FLASH_5720VENDOR_M_ST_M25PE40:
12904 case FLASH_5720VENDOR_M_ST_M45PE40:
12905 case FLASH_5720VENDOR_A_ST_M25PE40:
12906 case FLASH_5720VENDOR_A_ST_M45PE40:
12907 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
12908 break;
12909 case FLASH_5720VENDOR_M_ST_M25PE80:
12910 case FLASH_5720VENDOR_M_ST_M45PE80:
12911 case FLASH_5720VENDOR_A_ST_M25PE80:
12912 case FLASH_5720VENDOR_A_ST_M45PE80:
12913 tp->nvram_size = TG3_NVRAM_SIZE_1MB;
12914 break;
12915 default:
12916 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
12917 break;
12918 }
12919 break;
12920 default:
Joe Perches63c3a662011-04-26 08:12:10 +000012921 tg3_flag_set(tp, NO_NVRAM);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012922 return;
12923 }
12924
12925 tg3_nvram_get_pagesize(tp, nvcfg1);
12926 if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
Joe Perches63c3a662011-04-26 08:12:10 +000012927 tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012928}
12929
Linus Torvalds1da177e2005-04-16 15:20:36 -070012930/* Chips other than 5700/5701 use the NVRAM for fetching info. */
12931static void __devinit tg3_nvram_init(struct tg3 *tp)
12932{
Linus Torvalds1da177e2005-04-16 15:20:36 -070012933 tw32_f(GRC_EEPROM_ADDR,
12934 (EEPROM_ADDR_FSM_RESET |
12935 (EEPROM_DEFAULT_CLOCK_PERIOD <<
12936 EEPROM_ADDR_CLKPERD_SHIFT)));
12937
Michael Chan9d57f012006-12-07 00:23:25 -080012938 msleep(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012939
12940 /* Enable seeprom accesses. */
12941 tw32_f(GRC_LOCAL_CTRL,
12942 tr32(GRC_LOCAL_CTRL) | GRC_LCLCTRL_AUTO_SEEPROM);
12943 udelay(100);
12944
12945 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
12946 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701) {
Joe Perches63c3a662011-04-26 08:12:10 +000012947 tg3_flag_set(tp, NVRAM);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012948
Michael Chanec41c7d2006-01-17 02:40:55 -080012949 if (tg3_nvram_lock(tp)) {
Matt Carlson5129c3a2010-04-05 10:19:23 +000012950 netdev_warn(tp->dev,
12951 "Cannot get nvram lock, %s failed\n",
Joe Perches05dbe002010-02-17 19:44:19 +000012952 __func__);
Michael Chanec41c7d2006-01-17 02:40:55 -080012953 return;
12954 }
Michael Chane6af3012005-04-21 17:12:05 -070012955 tg3_enable_nvram_access(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012956
Matt Carlson989a9d22007-05-05 11:51:05 -070012957 tp->nvram_size = 0;
12958
Michael Chan361b4ac2005-04-21 17:11:21 -070012959 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752)
12960 tg3_get_5752_nvram_info(tp);
Michael Chand3c7b882006-03-23 01:28:25 -080012961 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755)
12962 tg3_get_5755_nvram_info(tp);
Matt Carlsond30cdd22007-10-07 23:28:35 -070012963 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787 ||
Matt Carlson57e69832008-05-25 23:48:31 -070012964 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
12965 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
Michael Chan1b277772006-03-20 22:27:48 -080012966 tg3_get_5787_nvram_info(tp);
Matt Carlson6b91fa02007-10-10 18:01:09 -070012967 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
12968 tg3_get_5761_nvram_info(tp);
Michael Chanb5d37722006-09-27 16:06:21 -070012969 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
12970 tg3_get_5906_nvram_info(tp);
Matt Carlsonb703df62009-12-03 08:36:21 +000012971 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
Matt Carlson55086ad2011-12-14 11:09:59 +000012972 tg3_flag(tp, 57765_CLASS))
Matt Carlson321d32a2008-11-21 17:22:19 -080012973 tg3_get_57780_nvram_info(tp);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012974 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
12975 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
Matt Carlsona1b950d2009-09-01 13:20:17 +000012976 tg3_get_5717_nvram_info(tp);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012977 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
12978 tg3_get_5720_nvram_info(tp);
Michael Chan361b4ac2005-04-21 17:11:21 -070012979 else
12980 tg3_get_nvram_info(tp);
12981
Matt Carlson989a9d22007-05-05 11:51:05 -070012982 if (tp->nvram_size == 0)
12983 tg3_get_nvram_size(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012984
Michael Chane6af3012005-04-21 17:12:05 -070012985 tg3_disable_nvram_access(tp);
Michael Chan381291b2005-12-13 21:08:21 -080012986 tg3_nvram_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012987
12988 } else {
Joe Perches63c3a662011-04-26 08:12:10 +000012989 tg3_flag_clear(tp, NVRAM);
12990 tg3_flag_clear(tp, NVRAM_BUFFERED);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012991
12992 tg3_get_eeprom_size(tp);
12993 }
12994}
12995
Linus Torvalds1da177e2005-04-16 15:20:36 -070012996struct subsys_tbl_ent {
12997 u16 subsys_vendor, subsys_devid;
12998 u32 phy_id;
12999};
13000
Matt Carlson24daf2b2010-02-17 15:17:02 +000013001static struct subsys_tbl_ent subsys_id_to_phy_id[] __devinitdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070013002 /* Broadcom boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000013003 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000013004 TG3PCI_SUBDEVICE_ID_BROADCOM_95700A6, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013005 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000013006 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A5, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013007 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000013008 TG3PCI_SUBDEVICE_ID_BROADCOM_95700T6, TG3_PHY_ID_BCM8002 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013009 { TG3PCI_SUBVENDOR_ID_BROADCOM,
13010 TG3PCI_SUBDEVICE_ID_BROADCOM_95700A9, 0 },
13011 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000013012 TG3PCI_SUBDEVICE_ID_BROADCOM_95701T1, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013013 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000013014 TG3PCI_SUBDEVICE_ID_BROADCOM_95701T8, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013015 { TG3PCI_SUBVENDOR_ID_BROADCOM,
13016 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A7, 0 },
13017 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000013018 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A10, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013019 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000013020 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A12, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013021 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000013022 TG3PCI_SUBDEVICE_ID_BROADCOM_95703AX1, TG3_PHY_ID_BCM5703 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013023 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000013024 TG3PCI_SUBDEVICE_ID_BROADCOM_95703AX2, TG3_PHY_ID_BCM5703 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070013025
13026 /* 3com boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000013027 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000013028 TG3PCI_SUBDEVICE_ID_3COM_3C996T, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013029 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000013030 TG3PCI_SUBDEVICE_ID_3COM_3C996BT, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013031 { TG3PCI_SUBVENDOR_ID_3COM,
13032 TG3PCI_SUBDEVICE_ID_3COM_3C996SX, 0 },
13033 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000013034 TG3PCI_SUBDEVICE_ID_3COM_3C1000T, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013035 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000013036 TG3PCI_SUBDEVICE_ID_3COM_3C940BR01, TG3_PHY_ID_BCM5701 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070013037
13038 /* DELL boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000013039 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000013040 TG3PCI_SUBDEVICE_ID_DELL_VIPER, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013041 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000013042 TG3PCI_SUBDEVICE_ID_DELL_JAGUAR, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013043 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000013044 TG3PCI_SUBDEVICE_ID_DELL_MERLOT, TG3_PHY_ID_BCM5411 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013045 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000013046 TG3PCI_SUBDEVICE_ID_DELL_SLIM_MERLOT, TG3_PHY_ID_BCM5411 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070013047
13048 /* Compaq boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000013049 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000013050 TG3PCI_SUBDEVICE_ID_COMPAQ_BANSHEE, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013051 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000013052 TG3PCI_SUBDEVICE_ID_COMPAQ_BANSHEE_2, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013053 { TG3PCI_SUBVENDOR_ID_COMPAQ,
13054 TG3PCI_SUBDEVICE_ID_COMPAQ_CHANGELING, 0 },
13055 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000013056 TG3PCI_SUBDEVICE_ID_COMPAQ_NC7780, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013057 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000013058 TG3PCI_SUBDEVICE_ID_COMPAQ_NC7780_2, TG3_PHY_ID_BCM5701 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070013059
13060 /* IBM boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000013061 { TG3PCI_SUBVENDOR_ID_IBM,
13062 TG3PCI_SUBDEVICE_ID_IBM_5703SAX2, 0 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013063};
13064
Matt Carlson24daf2b2010-02-17 15:17:02 +000013065static struct subsys_tbl_ent * __devinit tg3_lookup_by_subsys(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013066{
13067 int i;
13068
13069 for (i = 0; i < ARRAY_SIZE(subsys_id_to_phy_id); i++) {
13070 if ((subsys_id_to_phy_id[i].subsys_vendor ==
13071 tp->pdev->subsystem_vendor) &&
13072 (subsys_id_to_phy_id[i].subsys_devid ==
13073 tp->pdev->subsystem_device))
13074 return &subsys_id_to_phy_id[i];
13075 }
13076 return NULL;
13077}
13078
Michael Chan7d0c41e2005-04-21 17:06:20 -070013079static void __devinit tg3_get_eeprom_hw_cfg(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013080{
Linus Torvalds1da177e2005-04-16 15:20:36 -070013081 u32 val;
David S. Millerf49639e2006-06-09 11:58:36 -070013082
Matt Carlson79eb6902010-02-17 15:17:03 +000013083 tp->phy_id = TG3_PHY_ID_INVALID;
Michael Chan7d0c41e2005-04-21 17:06:20 -070013084 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
13085
Gary Zambranoa85feb82007-05-05 11:52:19 -070013086 /* Assume an onboard device and WOL capable by default. */
Joe Perches63c3a662011-04-26 08:12:10 +000013087 tg3_flag_set(tp, EEPROM_WRITE_PROT);
13088 tg3_flag_set(tp, WOL_CAP);
David S. Miller72b845e2006-03-14 14:11:48 -080013089
Michael Chanb5d37722006-09-27 16:06:21 -070013090 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan9d26e212006-12-07 00:21:14 -080013091 if (!(tr32(PCIE_TRANSACTION_CFG) & PCIE_TRANS_CFG_LOM)) {
Joe Perches63c3a662011-04-26 08:12:10 +000013092 tg3_flag_clear(tp, EEPROM_WRITE_PROT);
13093 tg3_flag_set(tp, IS_NIC);
Michael Chan9d26e212006-12-07 00:21:14 -080013094 }
Matt Carlson0527ba32007-10-10 18:03:30 -070013095 val = tr32(VCPU_CFGSHDW);
13096 if (val & VCPU_CFGSHDW_ASPM_DBNC)
Joe Perches63c3a662011-04-26 08:12:10 +000013097 tg3_flag_set(tp, ASPM_WORKAROUND);
Matt Carlson0527ba32007-10-10 18:03:30 -070013098 if ((val & VCPU_CFGSHDW_WOL_ENABLE) &&
Rafael J. Wysocki6fdbab92011-04-28 11:02:15 +000013099 (val & VCPU_CFGSHDW_WOL_MAGPKT)) {
Joe Perches63c3a662011-04-26 08:12:10 +000013100 tg3_flag_set(tp, WOL_ENABLE);
Rafael J. Wysocki6fdbab92011-04-28 11:02:15 +000013101 device_set_wakeup_enable(&tp->pdev->dev, true);
13102 }
Matt Carlson05ac4cb2008-11-03 16:53:46 -080013103 goto done;
Michael Chanb5d37722006-09-27 16:06:21 -070013104 }
13105
Linus Torvalds1da177e2005-04-16 15:20:36 -070013106 tg3_read_mem(tp, NIC_SRAM_DATA_SIG, &val);
13107 if (val == NIC_SRAM_DATA_SIG_MAGIC) {
13108 u32 nic_cfg, led_cfg;
Matt Carlsona9daf362008-05-25 23:49:44 -070013109 u32 nic_phy_id, ver, cfg2 = 0, cfg4 = 0, eeprom_phy_id;
Michael Chan7d0c41e2005-04-21 17:06:20 -070013110 int eeprom_phy_serdes = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013111
13112 tg3_read_mem(tp, NIC_SRAM_DATA_CFG, &nic_cfg);
13113 tp->nic_sram_data_cfg = nic_cfg;
13114
13115 tg3_read_mem(tp, NIC_SRAM_DATA_VER, &ver);
13116 ver >>= NIC_SRAM_DATA_VER_SHIFT;
Matt Carlson6ff6f812011-05-19 12:12:54 +000013117 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
13118 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701 &&
13119 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5703 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070013120 (ver > 0) && (ver < 0x100))
13121 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_2, &cfg2);
13122
Matt Carlsona9daf362008-05-25 23:49:44 -070013123 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
13124 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_4, &cfg4);
13125
Linus Torvalds1da177e2005-04-16 15:20:36 -070013126 if ((nic_cfg & NIC_SRAM_DATA_CFG_PHY_TYPE_MASK) ==
13127 NIC_SRAM_DATA_CFG_PHY_TYPE_FIBER)
13128 eeprom_phy_serdes = 1;
13129
13130 tg3_read_mem(tp, NIC_SRAM_DATA_PHY_ID, &nic_phy_id);
13131 if (nic_phy_id != 0) {
13132 u32 id1 = nic_phy_id & NIC_SRAM_DATA_PHY_ID1_MASK;
13133 u32 id2 = nic_phy_id & NIC_SRAM_DATA_PHY_ID2_MASK;
13134
13135 eeprom_phy_id = (id1 >> 16) << 10;
13136 eeprom_phy_id |= (id2 & 0xfc00) << 16;
13137 eeprom_phy_id |= (id2 & 0x03ff) << 0;
13138 } else
13139 eeprom_phy_id = 0;
13140
Michael Chan7d0c41e2005-04-21 17:06:20 -070013141 tp->phy_id = eeprom_phy_id;
Michael Chan747e8f82005-07-25 12:33:22 -070013142 if (eeprom_phy_serdes) {
Joe Perches63c3a662011-04-26 08:12:10 +000013143 if (!tg3_flag(tp, 5705_PLUS))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013144 tp->phy_flags |= TG3_PHYFLG_PHY_SERDES;
Matt Carlsona50d0792010-06-05 17:24:37 +000013145 else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013146 tp->phy_flags |= TG3_PHYFLG_MII_SERDES;
Michael Chan747e8f82005-07-25 12:33:22 -070013147 }
Michael Chan7d0c41e2005-04-21 17:06:20 -070013148
Joe Perches63c3a662011-04-26 08:12:10 +000013149 if (tg3_flag(tp, 5750_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -070013150 led_cfg = cfg2 & (NIC_SRAM_DATA_CFG_LED_MODE_MASK |
13151 SHASTA_EXT_LED_MODE_MASK);
John W. Linvillecbf46852005-04-21 17:01:29 -070013152 else
Linus Torvalds1da177e2005-04-16 15:20:36 -070013153 led_cfg = nic_cfg & NIC_SRAM_DATA_CFG_LED_MODE_MASK;
13154
13155 switch (led_cfg) {
13156 default:
13157 case NIC_SRAM_DATA_CFG_LED_MODE_PHY_1:
13158 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
13159 break;
13160
13161 case NIC_SRAM_DATA_CFG_LED_MODE_PHY_2:
13162 tp->led_ctrl = LED_CTRL_MODE_PHY_2;
13163 break;
13164
13165 case NIC_SRAM_DATA_CFG_LED_MODE_MAC:
13166 tp->led_ctrl = LED_CTRL_MODE_MAC;
Michael Chan9ba27792005-06-06 15:16:20 -070013167
13168 /* Default to PHY_1_MODE if 0 (MAC_MODE) is
13169 * read on some older 5700/5701 bootcode.
13170 */
13171 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
13172 ASIC_REV_5700 ||
13173 GET_ASIC_REV(tp->pci_chip_rev_id) ==
13174 ASIC_REV_5701)
13175 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
13176
Linus Torvalds1da177e2005-04-16 15:20:36 -070013177 break;
13178
13179 case SHASTA_EXT_LED_SHARED:
13180 tp->led_ctrl = LED_CTRL_MODE_SHARED;
13181 if (tp->pci_chip_rev_id != CHIPREV_ID_5750_A0 &&
13182 tp->pci_chip_rev_id != CHIPREV_ID_5750_A1)
13183 tp->led_ctrl |= (LED_CTRL_MODE_PHY_1 |
13184 LED_CTRL_MODE_PHY_2);
13185 break;
13186
13187 case SHASTA_EXT_LED_MAC:
13188 tp->led_ctrl = LED_CTRL_MODE_SHASTA_MAC;
13189 break;
13190
13191 case SHASTA_EXT_LED_COMBO:
13192 tp->led_ctrl = LED_CTRL_MODE_COMBO;
13193 if (tp->pci_chip_rev_id != CHIPREV_ID_5750_A0)
13194 tp->led_ctrl |= (LED_CTRL_MODE_PHY_1 |
13195 LED_CTRL_MODE_PHY_2);
13196 break;
13197
Stephen Hemminger855e1112008-04-16 16:37:28 -070013198 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013199
13200 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
13201 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) &&
13202 tp->pdev->subsystem_vendor == PCI_VENDOR_ID_DELL)
13203 tp->led_ctrl = LED_CTRL_MODE_PHY_2;
13204
Matt Carlsonb2a5c192008-04-03 21:44:44 -070013205 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX)
13206 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
Matt Carlson5f608912007-11-12 21:17:07 -080013207
Michael Chan9d26e212006-12-07 00:21:14 -080013208 if (nic_cfg & NIC_SRAM_DATA_CFG_EEPROM_WP) {
Joe Perches63c3a662011-04-26 08:12:10 +000013209 tg3_flag_set(tp, EEPROM_WRITE_PROT);
Michael Chan9d26e212006-12-07 00:21:14 -080013210 if ((tp->pdev->subsystem_vendor ==
13211 PCI_VENDOR_ID_ARIMA) &&
13212 (tp->pdev->subsystem_device == 0x205a ||
13213 tp->pdev->subsystem_device == 0x2063))
Joe Perches63c3a662011-04-26 08:12:10 +000013214 tg3_flag_clear(tp, EEPROM_WRITE_PROT);
Michael Chan9d26e212006-12-07 00:21:14 -080013215 } else {
Joe Perches63c3a662011-04-26 08:12:10 +000013216 tg3_flag_clear(tp, EEPROM_WRITE_PROT);
13217 tg3_flag_set(tp, IS_NIC);
Michael Chan9d26e212006-12-07 00:21:14 -080013218 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013219
13220 if (nic_cfg & NIC_SRAM_DATA_CFG_ASF_ENABLE) {
Joe Perches63c3a662011-04-26 08:12:10 +000013221 tg3_flag_set(tp, ENABLE_ASF);
13222 if (tg3_flag(tp, 5750_PLUS))
13223 tg3_flag_set(tp, ASF_NEW_HANDSHAKE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013224 }
Matt Carlsonb2b98d42008-11-03 16:52:32 -080013225
13226 if ((nic_cfg & NIC_SRAM_DATA_CFG_APE_ENABLE) &&
Joe Perches63c3a662011-04-26 08:12:10 +000013227 tg3_flag(tp, 5750_PLUS))
13228 tg3_flag_set(tp, ENABLE_APE);
Matt Carlsonb2b98d42008-11-03 16:52:32 -080013229
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013230 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES &&
Gary Zambranoa85feb82007-05-05 11:52:19 -070013231 !(nic_cfg & NIC_SRAM_DATA_CFG_FIBER_WOL))
Joe Perches63c3a662011-04-26 08:12:10 +000013232 tg3_flag_clear(tp, WOL_CAP);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013233
Joe Perches63c3a662011-04-26 08:12:10 +000013234 if (tg3_flag(tp, WOL_CAP) &&
Rafael J. Wysocki6fdbab92011-04-28 11:02:15 +000013235 (nic_cfg & NIC_SRAM_DATA_CFG_WOL_ENABLE)) {
Joe Perches63c3a662011-04-26 08:12:10 +000013236 tg3_flag_set(tp, WOL_ENABLE);
Rafael J. Wysocki6fdbab92011-04-28 11:02:15 +000013237 device_set_wakeup_enable(&tp->pdev->dev, true);
13238 }
Matt Carlson0527ba32007-10-10 18:03:30 -070013239
Linus Torvalds1da177e2005-04-16 15:20:36 -070013240 if (cfg2 & (1 << 17))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013241 tp->phy_flags |= TG3_PHYFLG_CAPACITIVE_COUPLING;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013242
13243 /* serdes signal pre-emphasis in register 0x590 set by */
13244 /* bootcode if bit 18 is set */
13245 if (cfg2 & (1 << 18))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013246 tp->phy_flags |= TG3_PHYFLG_SERDES_PREEMPHASIS;
Matt Carlson8ed5d972007-05-07 00:25:49 -070013247
Joe Perches63c3a662011-04-26 08:12:10 +000013248 if ((tg3_flag(tp, 57765_PLUS) ||
13249 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
13250 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX)) &&
Matt Carlson6833c042008-11-21 17:18:59 -080013251 (cfg2 & NIC_SRAM_DATA_CFG_2_APD_EN))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013252 tp->phy_flags |= TG3_PHYFLG_ENABLE_APD;
Matt Carlson6833c042008-11-21 17:18:59 -080013253
Joe Perches63c3a662011-04-26 08:12:10 +000013254 if (tg3_flag(tp, PCI_EXPRESS) &&
Matt Carlson8c69b1e2010-08-02 11:26:00 +000013255 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
Joe Perches63c3a662011-04-26 08:12:10 +000013256 !tg3_flag(tp, 57765_PLUS)) {
Matt Carlson8ed5d972007-05-07 00:25:49 -070013257 u32 cfg3;
13258
13259 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_3, &cfg3);
13260 if (cfg3 & NIC_SRAM_ASPM_DEBOUNCE)
Joe Perches63c3a662011-04-26 08:12:10 +000013261 tg3_flag_set(tp, ASPM_WORKAROUND);
Matt Carlson8ed5d972007-05-07 00:25:49 -070013262 }
Matt Carlsona9daf362008-05-25 23:49:44 -070013263
Matt Carlson14417062010-02-17 15:16:59 +000013264 if (cfg4 & NIC_SRAM_RGMII_INBAND_DISABLE)
Joe Perches63c3a662011-04-26 08:12:10 +000013265 tg3_flag_set(tp, RGMII_INBAND_DISABLE);
Matt Carlsona9daf362008-05-25 23:49:44 -070013266 if (cfg4 & NIC_SRAM_RGMII_EXT_IBND_RX_EN)
Joe Perches63c3a662011-04-26 08:12:10 +000013267 tg3_flag_set(tp, RGMII_EXT_IBND_RX_EN);
Matt Carlsona9daf362008-05-25 23:49:44 -070013268 if (cfg4 & NIC_SRAM_RGMII_EXT_IBND_TX_EN)
Joe Perches63c3a662011-04-26 08:12:10 +000013269 tg3_flag_set(tp, RGMII_EXT_IBND_TX_EN);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013270 }
Matt Carlson05ac4cb2008-11-03 16:53:46 -080013271done:
Joe Perches63c3a662011-04-26 08:12:10 +000013272 if (tg3_flag(tp, WOL_CAP))
Rafael J. Wysocki43067ed2011-02-10 06:53:09 +000013273 device_set_wakeup_enable(&tp->pdev->dev,
Joe Perches63c3a662011-04-26 08:12:10 +000013274 tg3_flag(tp, WOL_ENABLE));
Rafael J. Wysocki43067ed2011-02-10 06:53:09 +000013275 else
13276 device_set_wakeup_capable(&tp->pdev->dev, false);
Michael Chan7d0c41e2005-04-21 17:06:20 -070013277}
13278
Matt Carlsonb2a5c192008-04-03 21:44:44 -070013279static int __devinit tg3_issue_otp_command(struct tg3 *tp, u32 cmd)
13280{
13281 int i;
13282 u32 val;
13283
13284 tw32(OTP_CTRL, cmd | OTP_CTRL_OTP_CMD_START);
13285 tw32(OTP_CTRL, cmd);
13286
13287 /* Wait for up to 1 ms for command to execute. */
13288 for (i = 0; i < 100; i++) {
13289 val = tr32(OTP_STATUS);
13290 if (val & OTP_STATUS_CMD_DONE)
13291 break;
13292 udelay(10);
13293 }
13294
13295 return (val & OTP_STATUS_CMD_DONE) ? 0 : -EBUSY;
13296}
13297
13298/* Read the gphy configuration from the OTP region of the chip. The gphy
13299 * configuration is a 32-bit value that straddles the alignment boundary.
13300 * We do two 32-bit reads and then shift and merge the results.
13301 */
13302static u32 __devinit tg3_read_otp_phycfg(struct tg3 *tp)
13303{
13304 u32 bhalf_otp, thalf_otp;
13305
13306 tw32(OTP_MODE, OTP_MODE_OTP_THRU_GRC);
13307
13308 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_INIT))
13309 return 0;
13310
13311 tw32(OTP_ADDRESS, OTP_ADDRESS_MAGIC1);
13312
13313 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_READ))
13314 return 0;
13315
13316 thalf_otp = tr32(OTP_READ_DATA);
13317
13318 tw32(OTP_ADDRESS, OTP_ADDRESS_MAGIC2);
13319
13320 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_READ))
13321 return 0;
13322
13323 bhalf_otp = tr32(OTP_READ_DATA);
13324
13325 return ((thalf_otp & 0x0000ffff) << 16) | (bhalf_otp >> 16);
13326}
13327
Matt Carlsone256f8a2011-03-09 16:58:24 +000013328static void __devinit tg3_phy_init_link_config(struct tg3 *tp)
13329{
Hiroaki SHIMODA202ff1c2011-11-22 04:05:41 +000013330 u32 adv = ADVERTISED_Autoneg;
Matt Carlsone256f8a2011-03-09 16:58:24 +000013331
13332 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
13333 adv |= ADVERTISED_1000baseT_Half |
13334 ADVERTISED_1000baseT_Full;
13335
13336 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
13337 adv |= ADVERTISED_100baseT_Half |
13338 ADVERTISED_100baseT_Full |
13339 ADVERTISED_10baseT_Half |
13340 ADVERTISED_10baseT_Full |
13341 ADVERTISED_TP;
13342 else
13343 adv |= ADVERTISED_FIBRE;
13344
13345 tp->link_config.advertising = adv;
13346 tp->link_config.speed = SPEED_INVALID;
13347 tp->link_config.duplex = DUPLEX_INVALID;
13348 tp->link_config.autoneg = AUTONEG_ENABLE;
13349 tp->link_config.active_speed = SPEED_INVALID;
13350 tp->link_config.active_duplex = DUPLEX_INVALID;
13351 tp->link_config.orig_speed = SPEED_INVALID;
13352 tp->link_config.orig_duplex = DUPLEX_INVALID;
13353 tp->link_config.orig_autoneg = AUTONEG_INVALID;
13354}
13355
Michael Chan7d0c41e2005-04-21 17:06:20 -070013356static int __devinit tg3_phy_probe(struct tg3 *tp)
13357{
13358 u32 hw_phy_id_1, hw_phy_id_2;
13359 u32 hw_phy_id, hw_phy_id_masked;
13360 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013361
Matt Carlsone256f8a2011-03-09 16:58:24 +000013362 /* flow control autonegotiation is default behavior */
Joe Perches63c3a662011-04-26 08:12:10 +000013363 tg3_flag_set(tp, PAUSE_AUTONEG);
Matt Carlsone256f8a2011-03-09 16:58:24 +000013364 tp->link_config.flowctrl = FLOW_CTRL_TX | FLOW_CTRL_RX;
13365
Joe Perches63c3a662011-04-26 08:12:10 +000013366 if (tg3_flag(tp, USE_PHYLIB))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070013367 return tg3_phy_init(tp);
13368
Linus Torvalds1da177e2005-04-16 15:20:36 -070013369 /* Reading the PHY ID register can conflict with ASF
Nick Andrew877d0312009-01-26 11:06:57 +010013370 * firmware access to the PHY hardware.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013371 */
13372 err = 0;
Joe Perches63c3a662011-04-26 08:12:10 +000013373 if (tg3_flag(tp, ENABLE_ASF) || tg3_flag(tp, ENABLE_APE)) {
Matt Carlson79eb6902010-02-17 15:17:03 +000013374 hw_phy_id = hw_phy_id_masked = TG3_PHY_ID_INVALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013375 } else {
13376 /* Now read the physical PHY_ID from the chip and verify
13377 * that it is sane. If it doesn't look good, we fall back
13378 * to either the hard-coded table based PHY_ID and failing
13379 * that the value found in the eeprom area.
13380 */
13381 err |= tg3_readphy(tp, MII_PHYSID1, &hw_phy_id_1);
13382 err |= tg3_readphy(tp, MII_PHYSID2, &hw_phy_id_2);
13383
13384 hw_phy_id = (hw_phy_id_1 & 0xffff) << 10;
13385 hw_phy_id |= (hw_phy_id_2 & 0xfc00) << 16;
13386 hw_phy_id |= (hw_phy_id_2 & 0x03ff) << 0;
13387
Matt Carlson79eb6902010-02-17 15:17:03 +000013388 hw_phy_id_masked = hw_phy_id & TG3_PHY_ID_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013389 }
13390
Matt Carlson79eb6902010-02-17 15:17:03 +000013391 if (!err && TG3_KNOWN_PHY_ID(hw_phy_id_masked)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070013392 tp->phy_id = hw_phy_id;
Matt Carlson79eb6902010-02-17 15:17:03 +000013393 if (hw_phy_id_masked == TG3_PHY_ID_BCM8002)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013394 tp->phy_flags |= TG3_PHYFLG_PHY_SERDES;
Michael Chanda6b2d02005-08-19 12:54:29 -070013395 else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013396 tp->phy_flags &= ~TG3_PHYFLG_PHY_SERDES;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013397 } else {
Matt Carlson79eb6902010-02-17 15:17:03 +000013398 if (tp->phy_id != TG3_PHY_ID_INVALID) {
Michael Chan7d0c41e2005-04-21 17:06:20 -070013399 /* Do nothing, phy ID already set up in
13400 * tg3_get_eeprom_hw_cfg().
13401 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070013402 } else {
13403 struct subsys_tbl_ent *p;
13404
13405 /* No eeprom signature? Try the hardcoded
13406 * subsys device table.
13407 */
Matt Carlson24daf2b2010-02-17 15:17:02 +000013408 p = tg3_lookup_by_subsys(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013409 if (!p)
13410 return -ENODEV;
13411
13412 tp->phy_id = p->phy_id;
13413 if (!tp->phy_id ||
Matt Carlson79eb6902010-02-17 15:17:03 +000013414 tp->phy_id == TG3_PHY_ID_BCM8002)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013415 tp->phy_flags |= TG3_PHYFLG_PHY_SERDES;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013416 }
13417 }
13418
Matt Carlsona6b68da2010-12-06 08:28:52 +000013419 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES) &&
Matt Carlson5baa5e92011-07-20 10:20:53 +000013420 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
13421 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720 ||
13422 (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718 &&
Matt Carlsona6b68da2010-12-06 08:28:52 +000013423 tp->pci_chip_rev_id != CHIPREV_ID_5717_A0) ||
13424 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765 &&
13425 tp->pci_chip_rev_id != CHIPREV_ID_57765_A0)))
Matt Carlson52b02d02010-10-14 10:37:41 +000013426 tp->phy_flags |= TG3_PHYFLG_EEE_CAP;
13427
Matt Carlsone256f8a2011-03-09 16:58:24 +000013428 tg3_phy_init_link_config(tp);
13429
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013430 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES) &&
Joe Perches63c3a662011-04-26 08:12:10 +000013431 !tg3_flag(tp, ENABLE_APE) &&
13432 !tg3_flag(tp, ENABLE_ASF)) {
Matt Carlsone2bf73e2011-12-08 14:40:15 +000013433 u32 bmsr, dummy;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013434
13435 tg3_readphy(tp, MII_BMSR, &bmsr);
13436 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
13437 (bmsr & BMSR_LSTATUS))
13438 goto skip_phy_reset;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040013439
Linus Torvalds1da177e2005-04-16 15:20:36 -070013440 err = tg3_phy_reset(tp);
13441 if (err)
13442 return err;
13443
Matt Carlson42b64a42011-05-19 12:12:49 +000013444 tg3_phy_set_wirespeed(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013445
Matt Carlsone2bf73e2011-12-08 14:40:15 +000013446 if (!tg3_phy_copper_an_config_ok(tp, &dummy)) {
Matt Carlson42b64a42011-05-19 12:12:49 +000013447 tg3_phy_autoneg_cfg(tp, tp->link_config.advertising,
13448 tp->link_config.flowctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013449
13450 tg3_writephy(tp, MII_BMCR,
13451 BMCR_ANENABLE | BMCR_ANRESTART);
13452 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013453 }
13454
13455skip_phy_reset:
Matt Carlson79eb6902010-02-17 15:17:03 +000013456 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070013457 err = tg3_init_5401phy_dsp(tp);
13458 if (err)
13459 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013460
Linus Torvalds1da177e2005-04-16 15:20:36 -070013461 err = tg3_init_5401phy_dsp(tp);
13462 }
13463
Linus Torvalds1da177e2005-04-16 15:20:36 -070013464 return err;
13465}
13466
Matt Carlson184b8902010-04-05 10:19:25 +000013467static void __devinit tg3_read_vpd(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013468{
Matt Carlsona4a8bb12010-09-15 09:00:00 +000013469 u8 *vpd_data;
Matt Carlson4181b2c2010-02-26 14:04:45 +000013470 unsigned int block_end, rosize, len;
Matt Carlson535a4902011-07-20 10:20:56 +000013471 u32 vpdlen;
Matt Carlson184b8902010-04-05 10:19:25 +000013472 int j, i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013473
Matt Carlson535a4902011-07-20 10:20:56 +000013474 vpd_data = (u8 *)tg3_vpd_readblock(tp, &vpdlen);
Matt Carlsona4a8bb12010-09-15 09:00:00 +000013475 if (!vpd_data)
13476 goto out_no_vpd;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013477
Matt Carlson535a4902011-07-20 10:20:56 +000013478 i = pci_vpd_find_tag(vpd_data, 0, vpdlen, PCI_VPD_LRDT_RO_DATA);
Matt Carlson4181b2c2010-02-26 14:04:45 +000013479 if (i < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013480 goto out_not_found;
Matt Carlson4181b2c2010-02-26 14:04:45 +000013481
13482 rosize = pci_vpd_lrdt_size(&vpd_data[i]);
13483 block_end = i + PCI_VPD_LRDT_TAG_SIZE + rosize;
13484 i += PCI_VPD_LRDT_TAG_SIZE;
13485
Matt Carlson535a4902011-07-20 10:20:56 +000013486 if (block_end > vpdlen)
Matt Carlson4181b2c2010-02-26 14:04:45 +000013487 goto out_not_found;
13488
Matt Carlson184b8902010-04-05 10:19:25 +000013489 j = pci_vpd_find_info_keyword(vpd_data, i, rosize,
13490 PCI_VPD_RO_KEYWORD_MFR_ID);
13491 if (j > 0) {
13492 len = pci_vpd_info_field_size(&vpd_data[j]);
13493
13494 j += PCI_VPD_INFO_FLD_HDR_SIZE;
13495 if (j + len > block_end || len != 4 ||
13496 memcmp(&vpd_data[j], "1028", 4))
13497 goto partno;
13498
13499 j = pci_vpd_find_info_keyword(vpd_data, i, rosize,
13500 PCI_VPD_RO_KEYWORD_VENDOR0);
13501 if (j < 0)
13502 goto partno;
13503
13504 len = pci_vpd_info_field_size(&vpd_data[j]);
13505
13506 j += PCI_VPD_INFO_FLD_HDR_SIZE;
13507 if (j + len > block_end)
13508 goto partno;
13509
13510 memcpy(tp->fw_ver, &vpd_data[j], len);
Matt Carlson535a4902011-07-20 10:20:56 +000013511 strncat(tp->fw_ver, " bc ", vpdlen - len - 1);
Matt Carlson184b8902010-04-05 10:19:25 +000013512 }
13513
13514partno:
Matt Carlson4181b2c2010-02-26 14:04:45 +000013515 i = pci_vpd_find_info_keyword(vpd_data, i, rosize,
13516 PCI_VPD_RO_KEYWORD_PARTNO);
13517 if (i < 0)
13518 goto out_not_found;
13519
13520 len = pci_vpd_info_field_size(&vpd_data[i]);
13521
13522 i += PCI_VPD_INFO_FLD_HDR_SIZE;
13523 if (len > TG3_BPN_SIZE ||
Matt Carlson535a4902011-07-20 10:20:56 +000013524 (len + i) > vpdlen)
Matt Carlson4181b2c2010-02-26 14:04:45 +000013525 goto out_not_found;
13526
13527 memcpy(tp->board_part_number, &vpd_data[i], len);
13528
Linus Torvalds1da177e2005-04-16 15:20:36 -070013529out_not_found:
Matt Carlsona4a8bb12010-09-15 09:00:00 +000013530 kfree(vpd_data);
Matt Carlson37a949c2010-09-30 10:34:33 +000013531 if (tp->board_part_number[0])
Matt Carlsona4a8bb12010-09-15 09:00:00 +000013532 return;
13533
13534out_no_vpd:
Matt Carlson37a949c2010-09-30 10:34:33 +000013535 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717) {
13536 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717)
13537 strcpy(tp->board_part_number, "BCM5717");
13538 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718)
13539 strcpy(tp->board_part_number, "BCM5718");
13540 else
13541 goto nomatch;
13542 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780) {
13543 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57780)
13544 strcpy(tp->board_part_number, "BCM57780");
13545 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57760)
13546 strcpy(tp->board_part_number, "BCM57760");
13547 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57790)
13548 strcpy(tp->board_part_number, "BCM57790");
13549 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57788)
13550 strcpy(tp->board_part_number, "BCM57788");
13551 else
13552 goto nomatch;
13553 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765) {
13554 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57761)
13555 strcpy(tp->board_part_number, "BCM57761");
13556 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57765)
13557 strcpy(tp->board_part_number, "BCM57765");
13558 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57781)
13559 strcpy(tp->board_part_number, "BCM57781");
13560 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57785)
13561 strcpy(tp->board_part_number, "BCM57785");
13562 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57791)
13563 strcpy(tp->board_part_number, "BCM57791");
13564 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57795)
13565 strcpy(tp->board_part_number, "BCM57795");
13566 else
13567 goto nomatch;
Matt Carlson55086ad2011-12-14 11:09:59 +000013568 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57766) {
13569 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57762)
13570 strcpy(tp->board_part_number, "BCM57762");
13571 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57766)
13572 strcpy(tp->board_part_number, "BCM57766");
13573 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57782)
13574 strcpy(tp->board_part_number, "BCM57782");
13575 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57786)
13576 strcpy(tp->board_part_number, "BCM57786");
13577 else
13578 goto nomatch;
Matt Carlson37a949c2010-09-30 10:34:33 +000013579 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chanb5d37722006-09-27 16:06:21 -070013580 strcpy(tp->board_part_number, "BCM95906");
Matt Carlson37a949c2010-09-30 10:34:33 +000013581 } else {
13582nomatch:
Michael Chanb5d37722006-09-27 16:06:21 -070013583 strcpy(tp->board_part_number, "none");
Matt Carlson37a949c2010-09-30 10:34:33 +000013584 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013585}
13586
Matt Carlson9c8a6202007-10-21 16:16:08 -070013587static int __devinit tg3_fw_img_is_valid(struct tg3 *tp, u32 offset)
13588{
13589 u32 val;
13590
Matt Carlsone4f34112009-02-25 14:25:00 +000013591 if (tg3_nvram_read(tp, offset, &val) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070013592 (val & 0xfc000000) != 0x0c000000 ||
Matt Carlsone4f34112009-02-25 14:25:00 +000013593 tg3_nvram_read(tp, offset + 4, &val) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070013594 val != 0)
13595 return 0;
13596
13597 return 1;
13598}
13599
Matt Carlsonacd9c112009-02-25 14:26:33 +000013600static void __devinit tg3_read_bc_ver(struct tg3 *tp)
13601{
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013602 u32 val, offset, start, ver_offset;
Matt Carlson75f99362010-04-05 10:19:24 +000013603 int i, dst_off;
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013604 bool newver = false;
Matt Carlsonacd9c112009-02-25 14:26:33 +000013605
13606 if (tg3_nvram_read(tp, 0xc, &offset) ||
13607 tg3_nvram_read(tp, 0x4, &start))
13608 return;
13609
13610 offset = tg3_nvram_logical_addr(tp, offset);
13611
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013612 if (tg3_nvram_read(tp, offset, &val))
Matt Carlsonacd9c112009-02-25 14:26:33 +000013613 return;
13614
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013615 if ((val & 0xfc000000) == 0x0c000000) {
13616 if (tg3_nvram_read(tp, offset + 4, &val))
Matt Carlsonacd9c112009-02-25 14:26:33 +000013617 return;
13618
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013619 if (val == 0)
13620 newver = true;
13621 }
13622
Matt Carlson75f99362010-04-05 10:19:24 +000013623 dst_off = strlen(tp->fw_ver);
13624
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013625 if (newver) {
Matt Carlson75f99362010-04-05 10:19:24 +000013626 if (TG3_VER_SIZE - dst_off < 16 ||
13627 tg3_nvram_read(tp, offset + 8, &ver_offset))
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013628 return;
13629
13630 offset = offset + ver_offset - start;
13631 for (i = 0; i < 16; i += 4) {
13632 __be32 v;
13633 if (tg3_nvram_read_be32(tp, offset + i, &v))
13634 return;
13635
Matt Carlson75f99362010-04-05 10:19:24 +000013636 memcpy(tp->fw_ver + dst_off + i, &v, sizeof(v));
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013637 }
13638 } else {
13639 u32 major, minor;
13640
13641 if (tg3_nvram_read(tp, TG3_NVM_PTREV_BCVER, &ver_offset))
13642 return;
13643
13644 major = (ver_offset & TG3_NVM_BCVER_MAJMSK) >>
13645 TG3_NVM_BCVER_MAJSFT;
13646 minor = ver_offset & TG3_NVM_BCVER_MINMSK;
Matt Carlson75f99362010-04-05 10:19:24 +000013647 snprintf(&tp->fw_ver[dst_off], TG3_VER_SIZE - dst_off,
13648 "v%d.%02d", major, minor);
Matt Carlsonacd9c112009-02-25 14:26:33 +000013649 }
13650}
13651
Matt Carlsona6f6cb12009-02-25 14:27:43 +000013652static void __devinit tg3_read_hwsb_ver(struct tg3 *tp)
13653{
13654 u32 val, major, minor;
13655
13656 /* Use native endian representation */
13657 if (tg3_nvram_read(tp, TG3_NVM_HWSB_CFG1, &val))
13658 return;
13659
13660 major = (val & TG3_NVM_HWSB_CFG1_MAJMSK) >>
13661 TG3_NVM_HWSB_CFG1_MAJSFT;
13662 minor = (val & TG3_NVM_HWSB_CFG1_MINMSK) >>
13663 TG3_NVM_HWSB_CFG1_MINSFT;
13664
13665 snprintf(&tp->fw_ver[0], 32, "sb v%d.%02d", major, minor);
13666}
13667
Matt Carlsondfe00d72008-11-21 17:19:41 -080013668static void __devinit tg3_read_sb_ver(struct tg3 *tp, u32 val)
13669{
13670 u32 offset, major, minor, build;
13671
Matt Carlson75f99362010-04-05 10:19:24 +000013672 strncat(tp->fw_ver, "sb", TG3_VER_SIZE - strlen(tp->fw_ver) - 1);
Matt Carlsondfe00d72008-11-21 17:19:41 -080013673
13674 if ((val & TG3_EEPROM_SB_FORMAT_MASK) != TG3_EEPROM_SB_FORMAT_1)
13675 return;
13676
13677 switch (val & TG3_EEPROM_SB_REVISION_MASK) {
13678 case TG3_EEPROM_SB_REVISION_0:
13679 offset = TG3_EEPROM_SB_F1R0_EDH_OFF;
13680 break;
13681 case TG3_EEPROM_SB_REVISION_2:
13682 offset = TG3_EEPROM_SB_F1R2_EDH_OFF;
13683 break;
13684 case TG3_EEPROM_SB_REVISION_3:
13685 offset = TG3_EEPROM_SB_F1R3_EDH_OFF;
13686 break;
Matt Carlsona4153d42010-02-17 15:16:56 +000013687 case TG3_EEPROM_SB_REVISION_4:
13688 offset = TG3_EEPROM_SB_F1R4_EDH_OFF;
13689 break;
13690 case TG3_EEPROM_SB_REVISION_5:
13691 offset = TG3_EEPROM_SB_F1R5_EDH_OFF;
13692 break;
Matt Carlsonbba226a2010-10-14 10:37:38 +000013693 case TG3_EEPROM_SB_REVISION_6:
13694 offset = TG3_EEPROM_SB_F1R6_EDH_OFF;
13695 break;
Matt Carlsondfe00d72008-11-21 17:19:41 -080013696 default:
13697 return;
13698 }
13699
Matt Carlsone4f34112009-02-25 14:25:00 +000013700 if (tg3_nvram_read(tp, offset, &val))
Matt Carlsondfe00d72008-11-21 17:19:41 -080013701 return;
13702
13703 build = (val & TG3_EEPROM_SB_EDH_BLD_MASK) >>
13704 TG3_EEPROM_SB_EDH_BLD_SHFT;
13705 major = (val & TG3_EEPROM_SB_EDH_MAJ_MASK) >>
13706 TG3_EEPROM_SB_EDH_MAJ_SHFT;
13707 minor = val & TG3_EEPROM_SB_EDH_MIN_MASK;
13708
13709 if (minor > 99 || build > 26)
13710 return;
13711
Matt Carlson75f99362010-04-05 10:19:24 +000013712 offset = strlen(tp->fw_ver);
13713 snprintf(&tp->fw_ver[offset], TG3_VER_SIZE - offset,
13714 " v%d.%02d", major, minor);
Matt Carlsondfe00d72008-11-21 17:19:41 -080013715
13716 if (build > 0) {
Matt Carlson75f99362010-04-05 10:19:24 +000013717 offset = strlen(tp->fw_ver);
13718 if (offset < TG3_VER_SIZE - 1)
13719 tp->fw_ver[offset] = 'a' + build - 1;
Matt Carlsondfe00d72008-11-21 17:19:41 -080013720 }
13721}
13722
Matt Carlsonacd9c112009-02-25 14:26:33 +000013723static void __devinit tg3_read_mgmtfw_ver(struct tg3 *tp)
Michael Chanc4e65752006-03-20 22:29:32 -080013724{
13725 u32 val, offset, start;
Matt Carlsonacd9c112009-02-25 14:26:33 +000013726 int i, vlen;
Matt Carlson9c8a6202007-10-21 16:16:08 -070013727
13728 for (offset = TG3_NVM_DIR_START;
13729 offset < TG3_NVM_DIR_END;
13730 offset += TG3_NVM_DIRENT_SIZE) {
Matt Carlsone4f34112009-02-25 14:25:00 +000013731 if (tg3_nvram_read(tp, offset, &val))
Matt Carlson9c8a6202007-10-21 16:16:08 -070013732 return;
13733
13734 if ((val >> TG3_NVM_DIRTYPE_SHIFT) == TG3_NVM_DIRTYPE_ASFINI)
13735 break;
13736 }
13737
13738 if (offset == TG3_NVM_DIR_END)
13739 return;
13740
Joe Perches63c3a662011-04-26 08:12:10 +000013741 if (!tg3_flag(tp, 5705_PLUS))
Matt Carlson9c8a6202007-10-21 16:16:08 -070013742 start = 0x08000000;
Matt Carlsone4f34112009-02-25 14:25:00 +000013743 else if (tg3_nvram_read(tp, offset - 4, &start))
Matt Carlson9c8a6202007-10-21 16:16:08 -070013744 return;
13745
Matt Carlsone4f34112009-02-25 14:25:00 +000013746 if (tg3_nvram_read(tp, offset + 4, &offset) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070013747 !tg3_fw_img_is_valid(tp, offset) ||
Matt Carlsone4f34112009-02-25 14:25:00 +000013748 tg3_nvram_read(tp, offset + 8, &val))
Matt Carlson9c8a6202007-10-21 16:16:08 -070013749 return;
13750
13751 offset += val - start;
13752
Matt Carlsonacd9c112009-02-25 14:26:33 +000013753 vlen = strlen(tp->fw_ver);
Matt Carlson9c8a6202007-10-21 16:16:08 -070013754
Matt Carlsonacd9c112009-02-25 14:26:33 +000013755 tp->fw_ver[vlen++] = ',';
13756 tp->fw_ver[vlen++] = ' ';
Matt Carlson9c8a6202007-10-21 16:16:08 -070013757
13758 for (i = 0; i < 4; i++) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000013759 __be32 v;
13760 if (tg3_nvram_read_be32(tp, offset, &v))
Matt Carlson9c8a6202007-10-21 16:16:08 -070013761 return;
13762
Al Virob9fc7dc2007-12-17 22:59:57 -080013763 offset += sizeof(v);
Matt Carlson9c8a6202007-10-21 16:16:08 -070013764
Matt Carlsonacd9c112009-02-25 14:26:33 +000013765 if (vlen > TG3_VER_SIZE - sizeof(v)) {
13766 memcpy(&tp->fw_ver[vlen], &v, TG3_VER_SIZE - vlen);
Matt Carlson9c8a6202007-10-21 16:16:08 -070013767 break;
13768 }
13769
Matt Carlsonacd9c112009-02-25 14:26:33 +000013770 memcpy(&tp->fw_ver[vlen], &v, sizeof(v));
13771 vlen += sizeof(v);
Matt Carlson9c8a6202007-10-21 16:16:08 -070013772 }
Matt Carlsonacd9c112009-02-25 14:26:33 +000013773}
13774
Matt Carlson7fd76442009-02-25 14:27:20 +000013775static void __devinit tg3_read_dash_ver(struct tg3 *tp)
13776{
13777 int vlen;
13778 u32 apedata;
Matt Carlsonecc79642010-08-02 11:26:01 +000013779 char *fwtype;
Matt Carlson7fd76442009-02-25 14:27:20 +000013780
Joe Perches63c3a662011-04-26 08:12:10 +000013781 if (!tg3_flag(tp, ENABLE_APE) || !tg3_flag(tp, ENABLE_ASF))
Matt Carlson7fd76442009-02-25 14:27:20 +000013782 return;
13783
13784 apedata = tg3_ape_read32(tp, TG3_APE_SEG_SIG);
13785 if (apedata != APE_SEG_SIG_MAGIC)
13786 return;
13787
13788 apedata = tg3_ape_read32(tp, TG3_APE_FW_STATUS);
13789 if (!(apedata & APE_FW_STATUS_READY))
13790 return;
13791
13792 apedata = tg3_ape_read32(tp, TG3_APE_FW_VERSION);
13793
Matt Carlsondc6d0742010-09-15 08:59:55 +000013794 if (tg3_ape_read32(tp, TG3_APE_FW_FEATURES) & TG3_APE_FW_FEATURE_NCSI) {
Joe Perches63c3a662011-04-26 08:12:10 +000013795 tg3_flag_set(tp, APE_HAS_NCSI);
Matt Carlsonecc79642010-08-02 11:26:01 +000013796 fwtype = "NCSI";
Matt Carlsondc6d0742010-09-15 08:59:55 +000013797 } else {
Matt Carlsonecc79642010-08-02 11:26:01 +000013798 fwtype = "DASH";
Matt Carlsondc6d0742010-09-15 08:59:55 +000013799 }
Matt Carlsonecc79642010-08-02 11:26:01 +000013800
Matt Carlson7fd76442009-02-25 14:27:20 +000013801 vlen = strlen(tp->fw_ver);
13802
Matt Carlsonecc79642010-08-02 11:26:01 +000013803 snprintf(&tp->fw_ver[vlen], TG3_VER_SIZE - vlen, " %s v%d.%d.%d.%d",
13804 fwtype,
Matt Carlson7fd76442009-02-25 14:27:20 +000013805 (apedata & APE_FW_VERSION_MAJMSK) >> APE_FW_VERSION_MAJSFT,
13806 (apedata & APE_FW_VERSION_MINMSK) >> APE_FW_VERSION_MINSFT,
13807 (apedata & APE_FW_VERSION_REVMSK) >> APE_FW_VERSION_REVSFT,
13808 (apedata & APE_FW_VERSION_BLDMSK));
13809}
13810
Matt Carlsonacd9c112009-02-25 14:26:33 +000013811static void __devinit tg3_read_fw_ver(struct tg3 *tp)
13812{
13813 u32 val;
Matt Carlson75f99362010-04-05 10:19:24 +000013814 bool vpd_vers = false;
13815
13816 if (tp->fw_ver[0] != 0)
13817 vpd_vers = true;
Matt Carlsonacd9c112009-02-25 14:26:33 +000013818
Joe Perches63c3a662011-04-26 08:12:10 +000013819 if (tg3_flag(tp, NO_NVRAM)) {
Matt Carlson75f99362010-04-05 10:19:24 +000013820 strcat(tp->fw_ver, "sb");
Matt Carlsondf259d82009-04-20 06:57:14 +000013821 return;
13822 }
13823
Matt Carlsonacd9c112009-02-25 14:26:33 +000013824 if (tg3_nvram_read(tp, 0, &val))
13825 return;
13826
13827 if (val == TG3_EEPROM_MAGIC)
13828 tg3_read_bc_ver(tp);
13829 else if ((val & TG3_EEPROM_MAGIC_FW_MSK) == TG3_EEPROM_MAGIC_FW)
13830 tg3_read_sb_ver(tp, val);
Matt Carlsona6f6cb12009-02-25 14:27:43 +000013831 else if ((val & TG3_EEPROM_MAGIC_HW_MSK) == TG3_EEPROM_MAGIC_HW)
13832 tg3_read_hwsb_ver(tp);
Matt Carlsonacd9c112009-02-25 14:26:33 +000013833 else
13834 return;
13835
Matt Carlsonc9cab242011-07-13 09:27:27 +000013836 if (vpd_vers)
Matt Carlson75f99362010-04-05 10:19:24 +000013837 goto done;
Matt Carlsonacd9c112009-02-25 14:26:33 +000013838
Matt Carlsonc9cab242011-07-13 09:27:27 +000013839 if (tg3_flag(tp, ENABLE_APE)) {
13840 if (tg3_flag(tp, ENABLE_ASF))
13841 tg3_read_dash_ver(tp);
13842 } else if (tg3_flag(tp, ENABLE_ASF)) {
13843 tg3_read_mgmtfw_ver(tp);
13844 }
Matt Carlson9c8a6202007-10-21 16:16:08 -070013845
Matt Carlson75f99362010-04-05 10:19:24 +000013846done:
Matt Carlson9c8a6202007-10-21 16:16:08 -070013847 tp->fw_ver[TG3_VER_SIZE - 1] = 0;
Michael Chanc4e65752006-03-20 22:29:32 -080013848}
13849
Matt Carlson7cb32cf2010-09-30 10:34:36 +000013850static inline u32 tg3_rx_ret_ring_size(struct tg3 *tp)
13851{
Joe Perches63c3a662011-04-26 08:12:10 +000013852 if (tg3_flag(tp, LRG_PROD_RING_CAP))
Matt Carlsonde9f5232011-04-05 14:22:43 +000013853 return TG3_RX_RET_MAX_SIZE_5717;
Joe Perches63c3a662011-04-26 08:12:10 +000013854 else if (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS))
Matt Carlsonde9f5232011-04-05 14:22:43 +000013855 return TG3_RX_RET_MAX_SIZE_5700;
Matt Carlson7cb32cf2010-09-30 10:34:36 +000013856 else
Matt Carlsonde9f5232011-04-05 14:22:43 +000013857 return TG3_RX_RET_MAX_SIZE_5705;
Matt Carlson7cb32cf2010-09-30 10:34:36 +000013858}
13859
Matt Carlson41434702011-03-09 16:58:22 +000013860static DEFINE_PCI_DEVICE_TABLE(tg3_write_reorder_chipsets) = {
Joe Perches895950c2010-12-21 02:16:08 -080013861 { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_FE_GATE_700C) },
13862 { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_8131_BRIDGE) },
13863 { PCI_DEVICE(PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_8385_0) },
13864 { },
13865};
13866
Matt Carlson16c7fa72012-02-13 10:20:10 +000013867static struct pci_dev * __devinit tg3_find_peer(struct tg3 *tp)
13868{
13869 struct pci_dev *peer;
13870 unsigned int func, devnr = tp->pdev->devfn & ~7;
13871
13872 for (func = 0; func < 8; func++) {
13873 peer = pci_get_slot(tp->pdev->bus, devnr | func);
13874 if (peer && peer != tp->pdev)
13875 break;
13876 pci_dev_put(peer);
13877 }
13878 /* 5704 can be configured in single-port mode, set peer to
13879 * tp->pdev in that case.
13880 */
13881 if (!peer) {
13882 peer = tp->pdev;
13883 return peer;
13884 }
13885
13886 /*
13887 * We don't need to keep the refcount elevated; there's no way
13888 * to remove one half of this device without removing the other
13889 */
13890 pci_dev_put(peer);
13891
13892 return peer;
13893}
13894
Linus Torvalds1da177e2005-04-16 15:20:36 -070013895static int __devinit tg3_get_invariants(struct tg3 *tp)
13896{
Linus Torvalds1da177e2005-04-16 15:20:36 -070013897 u32 misc_ctrl_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013898 u32 pci_state_reg, grc_misc_cfg;
13899 u32 val;
13900 u16 pci_cmd;
Matt Carlson5e7dfd02008-11-21 17:18:16 -080013901 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013902
Linus Torvalds1da177e2005-04-16 15:20:36 -070013903 /* Force memory write invalidate off. If we leave it on,
13904 * then on 5700_BX chips we have to enable a workaround.
13905 * The workaround is to set the TG3PCI_DMA_RW_CTRL boundary
13906 * to match the cacheline size. The Broadcom driver have this
13907 * workaround but turns MWI off all the times so never uses
13908 * it. This seems to suggest that the workaround is insufficient.
13909 */
13910 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
13911 pci_cmd &= ~PCI_COMMAND_INVALIDATE;
13912 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
13913
Matt Carlson16821282011-07-13 09:27:28 +000013914 /* Important! -- Make sure register accesses are byteswapped
13915 * correctly. Also, for those chips that require it, make
13916 * sure that indirect register accesses are enabled before
13917 * the first operation.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013918 */
13919 pci_read_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
13920 &misc_ctrl_reg);
Matt Carlson16821282011-07-13 09:27:28 +000013921 tp->misc_host_ctrl |= (misc_ctrl_reg &
13922 MISC_HOST_CTRL_CHIPREV);
13923 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
13924 tp->misc_host_ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013925
13926 tp->pci_chip_rev_id = (misc_ctrl_reg >>
13927 MISC_HOST_CTRL_CHIPREV_SHIFT);
Matt Carlson795d01c2007-10-07 23:28:17 -070013928 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_USE_PROD_ID_REG) {
13929 u32 prod_id_asic_rev;
13930
Matt Carlson5001e2f2009-11-13 13:03:51 +000013931 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717 ||
13932 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718 ||
Matt Carlsond78b59f2011-04-05 14:22:46 +000013933 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5719 ||
13934 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5720)
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013935 pci_read_config_dword(tp->pdev,
13936 TG3PCI_GEN2_PRODID_ASICREV,
13937 &prod_id_asic_rev);
Matt Carlsonb703df62009-12-03 08:36:21 +000013938 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57781 ||
13939 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57785 ||
13940 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57761 ||
13941 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57765 ||
13942 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57791 ||
Matt Carlson55086ad2011-12-14 11:09:59 +000013943 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57795 ||
13944 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57762 ||
13945 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57766 ||
13946 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57782 ||
13947 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57786)
Matt Carlsonb703df62009-12-03 08:36:21 +000013948 pci_read_config_dword(tp->pdev,
13949 TG3PCI_GEN15_PRODID_ASICREV,
13950 &prod_id_asic_rev);
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013951 else
13952 pci_read_config_dword(tp->pdev, TG3PCI_PRODID_ASICREV,
13953 &prod_id_asic_rev);
13954
Matt Carlson321d32a2008-11-21 17:22:19 -080013955 tp->pci_chip_rev_id = prod_id_asic_rev;
Matt Carlson795d01c2007-10-07 23:28:17 -070013956 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013957
Michael Chanff645be2005-04-21 17:09:53 -070013958 /* Wrong chip ID in 5752 A0. This code can be removed later
13959 * as A0 is not in production.
13960 */
13961 if (tp->pci_chip_rev_id == CHIPREV_ID_5752_A0_HW)
13962 tp->pci_chip_rev_id = CHIPREV_ID_5752_A0;
13963
Michael Chan68929142005-08-09 20:17:14 -070013964 /* If we have 5702/03 A1 or A2 on certain ICH chipsets,
13965 * we need to disable memory and use config. cycles
13966 * only to access all registers. The 5702/03 chips
13967 * can mistakenly decode the special cycles from the
13968 * ICH chipsets as memory write cycles, causing corruption
13969 * of register and memory space. Only certain ICH bridges
13970 * will drive special cycles with non-zero data during the
13971 * address phase which can fall within the 5703's address
13972 * range. This is not an ICH bug as the PCI spec allows
13973 * non-zero address during special cycles. However, only
13974 * these ICH bridges are known to drive non-zero addresses
13975 * during special cycles.
13976 *
13977 * Since special cycles do not cross PCI bridges, we only
13978 * enable this workaround if the 5703 is on the secondary
13979 * bus of these ICH bridges.
13980 */
13981 if ((tp->pci_chip_rev_id == CHIPREV_ID_5703_A1) ||
13982 (tp->pci_chip_rev_id == CHIPREV_ID_5703_A2)) {
13983 static struct tg3_dev_id {
13984 u32 vendor;
13985 u32 device;
13986 u32 rev;
13987 } ich_chipsets[] = {
13988 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AA_8,
13989 PCI_ANY_ID },
13990 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AB_8,
13991 PCI_ANY_ID },
13992 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_11,
13993 0xa },
13994 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_6,
13995 PCI_ANY_ID },
13996 { },
13997 };
13998 struct tg3_dev_id *pci_id = &ich_chipsets[0];
13999 struct pci_dev *bridge = NULL;
14000
14001 while (pci_id->vendor != 0) {
14002 bridge = pci_get_device(pci_id->vendor, pci_id->device,
14003 bridge);
14004 if (!bridge) {
14005 pci_id++;
14006 continue;
14007 }
14008 if (pci_id->rev != PCI_ANY_ID) {
Auke Kok44c10132007-06-08 15:46:36 -070014009 if (bridge->revision > pci_id->rev)
Michael Chan68929142005-08-09 20:17:14 -070014010 continue;
14011 }
14012 if (bridge->subordinate &&
14013 (bridge->subordinate->number ==
14014 tp->pdev->bus->number)) {
Joe Perches63c3a662011-04-26 08:12:10 +000014015 tg3_flag_set(tp, ICH_WORKAROUND);
Michael Chan68929142005-08-09 20:17:14 -070014016 pci_dev_put(bridge);
14017 break;
14018 }
14019 }
14020 }
14021
Matt Carlson6ff6f812011-05-19 12:12:54 +000014022 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
Matt Carlson41588ba2008-04-19 18:12:33 -070014023 static struct tg3_dev_id {
14024 u32 vendor;
14025 u32 device;
14026 } bridge_chipsets[] = {
14027 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PXH_0 },
14028 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PXH_1 },
14029 { },
14030 };
14031 struct tg3_dev_id *pci_id = &bridge_chipsets[0];
14032 struct pci_dev *bridge = NULL;
14033
14034 while (pci_id->vendor != 0) {
14035 bridge = pci_get_device(pci_id->vendor,
14036 pci_id->device,
14037 bridge);
14038 if (!bridge) {
14039 pci_id++;
14040 continue;
14041 }
14042 if (bridge->subordinate &&
14043 (bridge->subordinate->number <=
14044 tp->pdev->bus->number) &&
14045 (bridge->subordinate->subordinate >=
14046 tp->pdev->bus->number)) {
Joe Perches63c3a662011-04-26 08:12:10 +000014047 tg3_flag_set(tp, 5701_DMA_BUG);
Matt Carlson41588ba2008-04-19 18:12:33 -070014048 pci_dev_put(bridge);
14049 break;
14050 }
14051 }
14052 }
14053
Michael Chan4a29cc22006-03-19 13:21:12 -080014054 /* The EPB bridge inside 5714, 5715, and 5780 cannot support
14055 * DMA addresses > 40-bit. This bridge may have other additional
14056 * 57xx devices behind it in some 4-port NIC designs for example.
14057 * Any tg3 device found behind the bridge will also need the 40-bit
14058 * DMA workaround.
14059 */
Michael Chana4e2b342005-10-26 15:46:52 -070014060 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780 ||
14061 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
Joe Perches63c3a662011-04-26 08:12:10 +000014062 tg3_flag_set(tp, 5780_CLASS);
14063 tg3_flag_set(tp, 40BIT_DMA_BUG);
Michael Chan4cf78e42005-07-25 12:29:19 -070014064 tp->msi_cap = pci_find_capability(tp->pdev, PCI_CAP_ID_MSI);
Matt Carlson859a588792010-04-05 10:19:28 +000014065 } else {
Michael Chan4a29cc22006-03-19 13:21:12 -080014066 struct pci_dev *bridge = NULL;
14067
14068 do {
14069 bridge = pci_get_device(PCI_VENDOR_ID_SERVERWORKS,
14070 PCI_DEVICE_ID_SERVERWORKS_EPB,
14071 bridge);
14072 if (bridge && bridge->subordinate &&
14073 (bridge->subordinate->number <=
14074 tp->pdev->bus->number) &&
14075 (bridge->subordinate->subordinate >=
14076 tp->pdev->bus->number)) {
Joe Perches63c3a662011-04-26 08:12:10 +000014077 tg3_flag_set(tp, 40BIT_DMA_BUG);
Michael Chan4a29cc22006-03-19 13:21:12 -080014078 pci_dev_put(bridge);
14079 break;
14080 }
14081 } while (bridge);
14082 }
Michael Chan4cf78e42005-07-25 12:29:19 -070014083
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000014084 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
Matt Carlson3a1e19d2011-07-13 09:27:32 +000014085 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714)
Michael Chan7544b092007-05-05 13:08:32 -070014086 tp->pdev_peer = tg3_find_peer(tp);
14087
Matt Carlsonc885e822010-08-02 11:25:57 +000014088 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
Matt Carlsond78b59f2011-04-05 14:22:46 +000014089 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
14090 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
Joe Perches63c3a662011-04-26 08:12:10 +000014091 tg3_flag_set(tp, 5717_PLUS);
Matt Carlson0a58d662011-04-05 14:22:45 +000014092
14093 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765 ||
Matt Carlson55086ad2011-12-14 11:09:59 +000014094 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57766)
14095 tg3_flag_set(tp, 57765_CLASS);
14096
14097 if (tg3_flag(tp, 57765_CLASS) || tg3_flag(tp, 5717_PLUS))
Joe Perches63c3a662011-04-26 08:12:10 +000014098 tg3_flag_set(tp, 57765_PLUS);
Matt Carlsonc885e822010-08-02 11:25:57 +000014099
Matt Carlson321d32a2008-11-21 17:22:19 -080014100 /* Intentionally exclude ASIC_REV_5906 */
14101 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
Michael Chand9ab5ad2006-03-20 22:27:35 -080014102 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787 ||
Matt Carlsond30cdd22007-10-07 23:28:35 -070014103 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson9936bcf2007-10-10 18:03:07 -070014104 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson57e69832008-05-25 23:48:31 -070014105 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000014106 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
Joe Perches63c3a662011-04-26 08:12:10 +000014107 tg3_flag(tp, 57765_PLUS))
14108 tg3_flag_set(tp, 5755_PLUS);
Matt Carlson321d32a2008-11-21 17:22:19 -080014109
14110 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 ||
14111 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752 ||
Michael Chanb5d37722006-09-27 16:06:21 -070014112 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906 ||
Joe Perches63c3a662011-04-26 08:12:10 +000014113 tg3_flag(tp, 5755_PLUS) ||
14114 tg3_flag(tp, 5780_CLASS))
14115 tg3_flag_set(tp, 5750_PLUS);
John W. Linville6708e5c2005-04-21 17:00:52 -070014116
Matt Carlson6ff6f812011-05-19 12:12:54 +000014117 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 ||
Joe Perches63c3a662011-04-26 08:12:10 +000014118 tg3_flag(tp, 5750_PLUS))
14119 tg3_flag_set(tp, 5705_PLUS);
John W. Linville1b440c562005-04-21 17:03:18 -070014120
Matt Carlson507399f2009-11-13 13:03:37 +000014121 /* Determine TSO capabilities */
Matt Carlsona0512942011-07-27 14:20:54 +000014122 if (tp->pci_chip_rev_id == CHIPREV_ID_5719_A0)
Matt Carlson4d163b72011-01-25 15:58:48 +000014123 ; /* Do nothing. HW bug. */
Joe Perches63c3a662011-04-26 08:12:10 +000014124 else if (tg3_flag(tp, 57765_PLUS))
14125 tg3_flag_set(tp, HW_TSO_3);
14126 else if (tg3_flag(tp, 5755_PLUS) ||
Matt Carlsone849cdc2009-11-13 13:03:38 +000014127 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
Joe Perches63c3a662011-04-26 08:12:10 +000014128 tg3_flag_set(tp, HW_TSO_2);
14129 else if (tg3_flag(tp, 5750_PLUS)) {
14130 tg3_flag_set(tp, HW_TSO_1);
14131 tg3_flag_set(tp, TSO_BUG);
Matt Carlson507399f2009-11-13 13:03:37 +000014132 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 &&
14133 tp->pci_chip_rev_id >= CHIPREV_ID_5750_C2)
Joe Perches63c3a662011-04-26 08:12:10 +000014134 tg3_flag_clear(tp, TSO_BUG);
Matt Carlson507399f2009-11-13 13:03:37 +000014135 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
14136 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701 &&
14137 tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) {
Joe Perches63c3a662011-04-26 08:12:10 +000014138 tg3_flag_set(tp, TSO_BUG);
Matt Carlson507399f2009-11-13 13:03:37 +000014139 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705)
14140 tp->fw_needed = FIRMWARE_TG3TSO5;
14141 else
14142 tp->fw_needed = FIRMWARE_TG3TSO;
14143 }
14144
Matt Carlsondabc5c62011-05-19 12:12:52 +000014145 /* Selectively allow TSO based on operating conditions */
Matt Carlson6ff6f812011-05-19 12:12:54 +000014146 if (tg3_flag(tp, HW_TSO_1) ||
14147 tg3_flag(tp, HW_TSO_2) ||
14148 tg3_flag(tp, HW_TSO_3) ||
Matt Carlsoncf9ecf42011-11-28 09:41:03 +000014149 tp->fw_needed) {
14150 /* For firmware TSO, assume ASF is disabled.
14151 * We'll disable TSO later if we discover ASF
14152 * is enabled in tg3_get_eeprom_hw_cfg().
14153 */
Matt Carlsondabc5c62011-05-19 12:12:52 +000014154 tg3_flag_set(tp, TSO_CAPABLE);
Matt Carlsoncf9ecf42011-11-28 09:41:03 +000014155 } else {
Matt Carlsondabc5c62011-05-19 12:12:52 +000014156 tg3_flag_clear(tp, TSO_CAPABLE);
14157 tg3_flag_clear(tp, TSO_BUG);
14158 tp->fw_needed = NULL;
14159 }
14160
14161 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0)
14162 tp->fw_needed = FIRMWARE_TG3;
14163
Matt Carlson507399f2009-11-13 13:03:37 +000014164 tp->irq_max = 1;
14165
Joe Perches63c3a662011-04-26 08:12:10 +000014166 if (tg3_flag(tp, 5750_PLUS)) {
14167 tg3_flag_set(tp, SUPPORT_MSI);
Michael Chan7544b092007-05-05 13:08:32 -070014168 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_AX ||
14169 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_BX ||
14170 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714 &&
14171 tp->pci_chip_rev_id <= CHIPREV_ID_5714_A2 &&
14172 tp->pdev_peer == tp->pdev))
Joe Perches63c3a662011-04-26 08:12:10 +000014173 tg3_flag_clear(tp, SUPPORT_MSI);
Michael Chan7544b092007-05-05 13:08:32 -070014174
Joe Perches63c3a662011-04-26 08:12:10 +000014175 if (tg3_flag(tp, 5755_PLUS) ||
Michael Chanb5d37722006-09-27 16:06:21 -070014176 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Joe Perches63c3a662011-04-26 08:12:10 +000014177 tg3_flag_set(tp, 1SHOT_MSI);
Michael Chan52c0fd82006-06-29 20:15:54 -070014178 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014179
Joe Perches63c3a662011-04-26 08:12:10 +000014180 if (tg3_flag(tp, 57765_PLUS)) {
14181 tg3_flag_set(tp, SUPPORT_MSIX);
Matt Carlson507399f2009-11-13 13:03:37 +000014182 tp->irq_max = TG3_IRQ_MAX_VECS;
Matt Carlson90415472011-12-16 13:33:23 +000014183 tg3_rss_init_dflt_indir_tbl(tp);
Matt Carlson507399f2009-11-13 13:03:37 +000014184 }
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000014185 }
Matt Carlson0e1406d2009-11-02 12:33:33 +000014186
Matt Carlson2ffcc982011-05-19 12:12:44 +000014187 if (tg3_flag(tp, 5755_PLUS))
Joe Perches63c3a662011-04-26 08:12:10 +000014188 tg3_flag_set(tp, SHORT_DMA_BUG);
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000014189
Matt Carlsone31aa982011-07-27 14:20:53 +000014190 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
Matt Carlsona4cb4282011-12-14 11:09:58 +000014191 tp->dma_limit = TG3_TX_BD_DMA_MAX_4K;
Matt Carlson55086ad2011-12-14 11:09:59 +000014192 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57766)
14193 tp->dma_limit = TG3_TX_BD_DMA_MAX_2K;
Matt Carlsone31aa982011-07-27 14:20:53 +000014194
Matt Carlsonfa6b2aa2011-11-21 15:01:19 +000014195 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
14196 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
14197 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
Joe Perches63c3a662011-04-26 08:12:10 +000014198 tg3_flag_set(tp, LRG_PROD_RING_CAP);
Matt Carlsonde9f5232011-04-05 14:22:43 +000014199
Joe Perches63c3a662011-04-26 08:12:10 +000014200 if (tg3_flag(tp, 57765_PLUS) &&
Matt Carlsona0512942011-07-27 14:20:54 +000014201 tp->pci_chip_rev_id != CHIPREV_ID_5719_A0)
Joe Perches63c3a662011-04-26 08:12:10 +000014202 tg3_flag_set(tp, USE_JUMBO_BDFLAG);
Matt Carlsonb703df62009-12-03 08:36:21 +000014203
Joe Perches63c3a662011-04-26 08:12:10 +000014204 if (!tg3_flag(tp, 5705_PLUS) ||
14205 tg3_flag(tp, 5780_CLASS) ||
14206 tg3_flag(tp, USE_JUMBO_BDFLAG))
14207 tg3_flag_set(tp, JUMBO_CAPABLE);
Michael Chan0f893dc2005-07-25 12:30:38 -070014208
Matt Carlson52f44902008-11-21 17:17:04 -080014209 pci_read_config_dword(tp->pdev, TG3PCI_PCISTATE,
14210 &pci_state_reg);
14211
Jon Mason708ebb32011-06-27 12:56:50 +000014212 if (pci_is_pcie(tp->pdev)) {
Matt Carlson5e7dfd02008-11-21 17:18:16 -080014213 u16 lnkctl;
14214
Joe Perches63c3a662011-04-26 08:12:10 +000014215 tg3_flag_set(tp, PCI_EXPRESS);
Matt Carlson5f5c51e2007-11-12 21:19:37 -080014216
Matt Carlson2c55a3d2011-11-28 09:41:04 +000014217 if (tp->pci_chip_rev_id == CHIPREV_ID_5719_A0) {
14218 int readrq = pcie_get_readrq(tp->pdev);
14219 if (readrq > 2048)
14220 pcie_set_readrq(tp->pdev, 2048);
14221 }
Matt Carlson5f5c51e2007-11-12 21:19:37 -080014222
Matt Carlson5e7dfd02008-11-21 17:18:16 -080014223 pci_read_config_word(tp->pdev,
Jon Mason708ebb32011-06-27 12:56:50 +000014224 pci_pcie_cap(tp->pdev) + PCI_EXP_LNKCTL,
Matt Carlson5e7dfd02008-11-21 17:18:16 -080014225 &lnkctl);
14226 if (lnkctl & PCI_EXP_LNKCTL_CLKREQ_EN) {
Matt Carlson7196cd62011-05-19 16:02:44 +000014227 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
14228 ASIC_REV_5906) {
Joe Perches63c3a662011-04-26 08:12:10 +000014229 tg3_flag_clear(tp, HW_TSO_2);
Matt Carlsondabc5c62011-05-19 12:12:52 +000014230 tg3_flag_clear(tp, TSO_CAPABLE);
Matt Carlson7196cd62011-05-19 16:02:44 +000014231 }
Matt Carlson5e7dfd02008-11-21 17:18:16 -080014232 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson321d32a2008-11-21 17:22:19 -080014233 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson9cf74eb2009-04-20 06:58:27 +000014234 tp->pci_chip_rev_id == CHIPREV_ID_57780_A0 ||
14235 tp->pci_chip_rev_id == CHIPREV_ID_57780_A1)
Joe Perches63c3a662011-04-26 08:12:10 +000014236 tg3_flag_set(tp, CLKREQ_BUG);
Matt Carlson614b0592010-01-20 16:58:02 +000014237 } else if (tp->pci_chip_rev_id == CHIPREV_ID_5717_A0) {
Joe Perches63c3a662011-04-26 08:12:10 +000014238 tg3_flag_set(tp, L1PLLPD_EN);
Michael Chanc7835a72006-11-15 21:14:42 -080014239 }
Matt Carlson52f44902008-11-21 17:17:04 -080014240 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785) {
Jon Mason708ebb32011-06-27 12:56:50 +000014241 /* BCM5785 devices are effectively PCIe devices, and should
14242 * follow PCIe codepaths, but do not have a PCIe capabilities
14243 * section.
Matt Carlson93a700a2011-08-31 11:44:54 +000014244 */
Joe Perches63c3a662011-04-26 08:12:10 +000014245 tg3_flag_set(tp, PCI_EXPRESS);
14246 } else if (!tg3_flag(tp, 5705_PLUS) ||
14247 tg3_flag(tp, 5780_CLASS)) {
Matt Carlson52f44902008-11-21 17:17:04 -080014248 tp->pcix_cap = pci_find_capability(tp->pdev, PCI_CAP_ID_PCIX);
14249 if (!tp->pcix_cap) {
Matt Carlson2445e462010-04-05 10:19:21 +000014250 dev_err(&tp->pdev->dev,
14251 "Cannot find PCI-X capability, aborting\n");
Matt Carlson52f44902008-11-21 17:17:04 -080014252 return -EIO;
14253 }
14254
14255 if (!(pci_state_reg & PCISTATE_CONV_PCI_MODE))
Joe Perches63c3a662011-04-26 08:12:10 +000014256 tg3_flag_set(tp, PCIX_MODE);
Matt Carlson52f44902008-11-21 17:17:04 -080014257 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014258
Michael Chan399de502005-10-03 14:02:39 -070014259 /* If we have an AMD 762 or VIA K8T800 chipset, write
14260 * reordering to the mailbox registers done by the host
14261 * controller can cause major troubles. We read back from
14262 * every mailbox register write to force the writes to be
14263 * posted to the chip in order.
14264 */
Matt Carlson41434702011-03-09 16:58:22 +000014265 if (pci_dev_present(tg3_write_reorder_chipsets) &&
Joe Perches63c3a662011-04-26 08:12:10 +000014266 !tg3_flag(tp, PCI_EXPRESS))
14267 tg3_flag_set(tp, MBOX_WRITE_REORDER);
Michael Chan399de502005-10-03 14:02:39 -070014268
Matt Carlson69fc4052008-12-21 20:19:57 -080014269 pci_read_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE,
14270 &tp->pci_cacheline_sz);
14271 pci_read_config_byte(tp->pdev, PCI_LATENCY_TIMER,
14272 &tp->pci_lat_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014273 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 &&
14274 tp->pci_lat_timer < 64) {
14275 tp->pci_lat_timer = 64;
Matt Carlson69fc4052008-12-21 20:19:57 -080014276 pci_write_config_byte(tp->pdev, PCI_LATENCY_TIMER,
14277 tp->pci_lat_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014278 }
14279
Matt Carlson16821282011-07-13 09:27:28 +000014280 /* Important! -- It is critical that the PCI-X hw workaround
14281 * situation is decided before the first MMIO register access.
14282 */
Matt Carlson52f44902008-11-21 17:17:04 -080014283 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5700_BX) {
14284 /* 5700 BX chips need to have their TX producer index
14285 * mailboxes written twice to workaround a bug.
14286 */
Joe Perches63c3a662011-04-26 08:12:10 +000014287 tg3_flag_set(tp, TXD_MBOX_HWBUG);
Matt Carlson9974a352007-10-07 23:27:28 -070014288
Matt Carlson52f44902008-11-21 17:17:04 -080014289 /* If we are in PCI-X mode, enable register write workaround.
Linus Torvalds1da177e2005-04-16 15:20:36 -070014290 *
14291 * The workaround is to use indirect register accesses
14292 * for all chip writes not to mailbox registers.
14293 */
Joe Perches63c3a662011-04-26 08:12:10 +000014294 if (tg3_flag(tp, PCIX_MODE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070014295 u32 pm_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014296
Joe Perches63c3a662011-04-26 08:12:10 +000014297 tg3_flag_set(tp, PCIX_TARGET_HWBUG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014298
14299 /* The chip can have it's power management PCI config
14300 * space registers clobbered due to this bug.
14301 * So explicitly force the chip into D0 here.
14302 */
Matt Carlson9974a352007-10-07 23:27:28 -070014303 pci_read_config_dword(tp->pdev,
14304 tp->pm_cap + PCI_PM_CTRL,
Linus Torvalds1da177e2005-04-16 15:20:36 -070014305 &pm_reg);
14306 pm_reg &= ~PCI_PM_CTRL_STATE_MASK;
14307 pm_reg |= PCI_PM_CTRL_PME_ENABLE | 0 /* D0 */;
Matt Carlson9974a352007-10-07 23:27:28 -070014308 pci_write_config_dword(tp->pdev,
14309 tp->pm_cap + PCI_PM_CTRL,
Linus Torvalds1da177e2005-04-16 15:20:36 -070014310 pm_reg);
14311
14312 /* Also, force SERR#/PERR# in PCI command. */
14313 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
14314 pci_cmd |= PCI_COMMAND_PARITY | PCI_COMMAND_SERR;
14315 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
14316 }
14317 }
14318
Linus Torvalds1da177e2005-04-16 15:20:36 -070014319 if ((pci_state_reg & PCISTATE_BUS_SPEED_HIGH) != 0)
Joe Perches63c3a662011-04-26 08:12:10 +000014320 tg3_flag_set(tp, PCI_HIGH_SPEED);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014321 if ((pci_state_reg & PCISTATE_BUS_32BIT) != 0)
Joe Perches63c3a662011-04-26 08:12:10 +000014322 tg3_flag_set(tp, PCI_32BIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014323
14324 /* Chip-specific fixup from Broadcom driver */
14325 if ((tp->pci_chip_rev_id == CHIPREV_ID_5704_A0) &&
14326 (!(pci_state_reg & PCISTATE_RETRY_SAME_DMA))) {
14327 pci_state_reg |= PCISTATE_RETRY_SAME_DMA;
14328 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE, pci_state_reg);
14329 }
14330
Michael Chan1ee582d2005-08-09 20:16:46 -070014331 /* Default fast path register access methods */
Michael Chan20094932005-08-09 20:16:32 -070014332 tp->read32 = tg3_read32;
Michael Chan1ee582d2005-08-09 20:16:46 -070014333 tp->write32 = tg3_write32;
Michael Chan09ee9292005-08-09 20:17:00 -070014334 tp->read32_mbox = tg3_read32;
Michael Chan20094932005-08-09 20:16:32 -070014335 tp->write32_mbox = tg3_write32;
Michael Chan1ee582d2005-08-09 20:16:46 -070014336 tp->write32_tx_mbox = tg3_write32;
14337 tp->write32_rx_mbox = tg3_write32;
14338
14339 /* Various workaround register access methods */
Joe Perches63c3a662011-04-26 08:12:10 +000014340 if (tg3_flag(tp, PCIX_TARGET_HWBUG))
Michael Chan1ee582d2005-08-09 20:16:46 -070014341 tp->write32 = tg3_write_indirect_reg32;
Matt Carlson98efd8a2007-05-05 12:47:25 -070014342 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701 ||
Joe Perches63c3a662011-04-26 08:12:10 +000014343 (tg3_flag(tp, PCI_EXPRESS) &&
Matt Carlson98efd8a2007-05-05 12:47:25 -070014344 tp->pci_chip_rev_id == CHIPREV_ID_5750_A0)) {
14345 /*
14346 * Back to back register writes can cause problems on these
14347 * chips, the workaround is to read back all reg writes
14348 * except those to mailbox regs.
14349 *
14350 * See tg3_write_indirect_reg32().
14351 */
Michael Chan1ee582d2005-08-09 20:16:46 -070014352 tp->write32 = tg3_write_flush_reg32;
Matt Carlson98efd8a2007-05-05 12:47:25 -070014353 }
14354
Joe Perches63c3a662011-04-26 08:12:10 +000014355 if (tg3_flag(tp, TXD_MBOX_HWBUG) || tg3_flag(tp, MBOX_WRITE_REORDER)) {
Michael Chan1ee582d2005-08-09 20:16:46 -070014356 tp->write32_tx_mbox = tg3_write32_tx_mbox;
Joe Perches63c3a662011-04-26 08:12:10 +000014357 if (tg3_flag(tp, MBOX_WRITE_REORDER))
Michael Chan1ee582d2005-08-09 20:16:46 -070014358 tp->write32_rx_mbox = tg3_write_flush_reg32;
14359 }
Michael Chan20094932005-08-09 20:16:32 -070014360
Joe Perches63c3a662011-04-26 08:12:10 +000014361 if (tg3_flag(tp, ICH_WORKAROUND)) {
Michael Chan68929142005-08-09 20:17:14 -070014362 tp->read32 = tg3_read_indirect_reg32;
14363 tp->write32 = tg3_write_indirect_reg32;
14364 tp->read32_mbox = tg3_read_indirect_mbox;
14365 tp->write32_mbox = tg3_write_indirect_mbox;
14366 tp->write32_tx_mbox = tg3_write_indirect_mbox;
14367 tp->write32_rx_mbox = tg3_write_indirect_mbox;
14368
14369 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070014370 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070014371
14372 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
14373 pci_cmd &= ~PCI_COMMAND_MEMORY;
14374 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
14375 }
Michael Chanb5d37722006-09-27 16:06:21 -070014376 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
14377 tp->read32_mbox = tg3_read32_mbox_5906;
14378 tp->write32_mbox = tg3_write32_mbox_5906;
14379 tp->write32_tx_mbox = tg3_write32_mbox_5906;
14380 tp->write32_rx_mbox = tg3_write32_mbox_5906;
14381 }
Michael Chan68929142005-08-09 20:17:14 -070014382
Michael Chanbbadf502006-04-06 21:46:34 -070014383 if (tp->write32 == tg3_write_indirect_reg32 ||
Joe Perches63c3a662011-04-26 08:12:10 +000014384 (tg3_flag(tp, PCIX_MODE) &&
Michael Chanbbadf502006-04-06 21:46:34 -070014385 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
David S. Millerf49639e2006-06-09 11:58:36 -070014386 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)))
Joe Perches63c3a662011-04-26 08:12:10 +000014387 tg3_flag_set(tp, SRAM_USE_CONFIG);
Michael Chanbbadf502006-04-06 21:46:34 -070014388
Matt Carlson16821282011-07-13 09:27:28 +000014389 /* The memory arbiter has to be enabled in order for SRAM accesses
14390 * to succeed. Normally on powerup the tg3 chip firmware will make
14391 * sure it is enabled, but other entities such as system netboot
14392 * code might disable it.
14393 */
14394 val = tr32(MEMARB_MODE);
14395 tw32(MEMARB_MODE, val | MEMARB_MODE_ENABLE);
14396
Matt Carlson9dc5e342011-11-04 09:15:02 +000014397 tp->pci_fn = PCI_FUNC(tp->pdev->devfn) & 3;
14398 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
14399 tg3_flag(tp, 5780_CLASS)) {
14400 if (tg3_flag(tp, PCIX_MODE)) {
14401 pci_read_config_dword(tp->pdev,
14402 tp->pcix_cap + PCI_X_STATUS,
14403 &val);
14404 tp->pci_fn = val & 0x7;
14405 }
14406 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717) {
14407 tg3_read_mem(tp, NIC_SRAM_CPMU_STATUS, &val);
14408 if ((val & NIC_SRAM_CPMUSTAT_SIG_MSK) ==
14409 NIC_SRAM_CPMUSTAT_SIG) {
14410 tp->pci_fn = val & TG3_CPMU_STATUS_FMSK_5717;
14411 tp->pci_fn = tp->pci_fn ? 1 : 0;
14412 }
14413 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
14414 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
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_5719) >>
14419 TG3_CPMU_STATUS_FSHFT_5719;
14420 }
Matt Carlson69f11c92011-07-13 09:27:30 +000014421 }
14422
Michael Chan7d0c41e2005-04-21 17:06:20 -070014423 /* Get eeprom hw config before calling tg3_set_power_state().
Joe Perches63c3a662011-04-26 08:12:10 +000014424 * In particular, the TG3_FLAG_IS_NIC flag must be
Michael Chan7d0c41e2005-04-21 17:06:20 -070014425 * determined before calling tg3_set_power_state() so that
14426 * we know whether or not to switch out of Vaux power.
14427 * When the flag is set, it means that GPIO1 is used for eeprom
14428 * write protect and also implies that it is a LOM where GPIOs
14429 * are not used to switch power.
Jeff Garzik6aa20a22006-09-13 13:24:59 -040014430 */
Michael Chan7d0c41e2005-04-21 17:06:20 -070014431 tg3_get_eeprom_hw_cfg(tp);
14432
Matt Carlsoncf9ecf42011-11-28 09:41:03 +000014433 if (tp->fw_needed && tg3_flag(tp, ENABLE_ASF)) {
14434 tg3_flag_clear(tp, TSO_CAPABLE);
14435 tg3_flag_clear(tp, TSO_BUG);
14436 tp->fw_needed = NULL;
14437 }
14438
Joe Perches63c3a662011-04-26 08:12:10 +000014439 if (tg3_flag(tp, ENABLE_APE)) {
Matt Carlson0d3031d2007-10-10 18:02:43 -070014440 /* Allow reads and writes to the
14441 * APE register and memory space.
14442 */
14443 pci_state_reg |= PCISTATE_ALLOW_APE_CTLSPC_WR |
Matt Carlsonf92d9dc12010-06-05 17:24:30 +000014444 PCISTATE_ALLOW_APE_SHMEM_WR |
14445 PCISTATE_ALLOW_APE_PSPACE_WR;
Matt Carlson0d3031d2007-10-10 18:02:43 -070014446 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE,
14447 pci_state_reg);
Matt Carlsonc9cab242011-07-13 09:27:27 +000014448
14449 tg3_ape_lock_init(tp);
Matt Carlson0d3031d2007-10-10 18:02:43 -070014450 }
14451
Matt Carlson9936bcf2007-10-10 18:03:07 -070014452 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson57e69832008-05-25 23:48:31 -070014453 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson321d32a2008-11-21 17:22:19 -080014454 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000014455 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
Joe Perches63c3a662011-04-26 08:12:10 +000014456 tg3_flag(tp, 57765_PLUS))
14457 tg3_flag_set(tp, CPMU_PRESENT);
Matt Carlsond30cdd22007-10-07 23:28:35 -070014458
Matt Carlson16821282011-07-13 09:27:28 +000014459 /* Set up tp->grc_local_ctrl before calling
14460 * tg3_pwrsrc_switch_to_vmain(). GPIO1 driven high
14461 * will bring 5700's external PHY out of reset.
Michael Chan314fba32005-04-21 17:07:04 -070014462 * It is also used as eeprom write protect on LOMs.
14463 */
14464 tp->grc_local_ctrl = GRC_LCLCTRL_INT_ON_ATTN | GRC_LCLCTRL_AUTO_SEEPROM;
Matt Carlson6ff6f812011-05-19 12:12:54 +000014465 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
Joe Perches63c3a662011-04-26 08:12:10 +000014466 tg3_flag(tp, EEPROM_WRITE_PROT))
Michael Chan314fba32005-04-21 17:07:04 -070014467 tp->grc_local_ctrl |= (GRC_LCLCTRL_GPIO_OE1 |
14468 GRC_LCLCTRL_GPIO_OUTPUT1);
Michael Chan3e7d83b2005-04-21 17:10:36 -070014469 /* Unused GPIO3 must be driven as output on 5752 because there
14470 * are no pull-up resistors on unused GPIO pins.
14471 */
14472 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752)
14473 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE3;
Michael Chan314fba32005-04-21 17:07:04 -070014474
Matt Carlson321d32a2008-11-21 17:22:19 -080014475 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
Matt Carlsoncb4ed1f2010-01-20 16:58:09 +000014476 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
Matt Carlson55086ad2011-12-14 11:09:59 +000014477 tg3_flag(tp, 57765_CLASS))
Michael Chanaf36e6b2006-03-23 01:28:06 -080014478 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_UART_SEL;
14479
Matt Carlson8d519ab2009-04-20 06:58:01 +000014480 if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761 ||
14481 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761S) {
Matt Carlson5f0c4a32008-06-09 15:41:12 -070014482 /* Turn off the debug UART. */
14483 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_UART_SEL;
Joe Perches63c3a662011-04-26 08:12:10 +000014484 if (tg3_flag(tp, IS_NIC))
Matt Carlson5f0c4a32008-06-09 15:41:12 -070014485 /* Keep VMain power. */
14486 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE0 |
14487 GRC_LCLCTRL_GPIO_OUTPUT0;
14488 }
14489
Matt Carlson16821282011-07-13 09:27:28 +000014490 /* Switch out of Vaux if it is a NIC */
14491 tg3_pwrsrc_switch_to_vmain(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014492
Linus Torvalds1da177e2005-04-16 15:20:36 -070014493 /* Derive initial jumbo mode from MTU assigned in
14494 * ether_setup() via the alloc_etherdev() call
14495 */
Joe Perches63c3a662011-04-26 08:12:10 +000014496 if (tp->dev->mtu > ETH_DATA_LEN && !tg3_flag(tp, 5780_CLASS))
14497 tg3_flag_set(tp, JUMBO_RING_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014498
14499 /* Determine WakeOnLan speed to use. */
14500 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
14501 tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
14502 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0 ||
14503 tp->pci_chip_rev_id == CHIPREV_ID_5701_B2) {
Joe Perches63c3a662011-04-26 08:12:10 +000014504 tg3_flag_clear(tp, WOL_SPEED_100MB);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014505 } else {
Joe Perches63c3a662011-04-26 08:12:10 +000014506 tg3_flag_set(tp, WOL_SPEED_100MB);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014507 }
14508
Matt Carlson7f97a4b2009-08-25 10:10:03 +000014509 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014510 tp->phy_flags |= TG3_PHYFLG_IS_FET;
Matt Carlson7f97a4b2009-08-25 10:10:03 +000014511
Linus Torvalds1da177e2005-04-16 15:20:36 -070014512 /* A few boards don't want Ethernet@WireSpeed phy feature */
Matt Carlson6ff6f812011-05-19 12:12:54 +000014513 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
14514 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070014515 (tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) &&
Michael Chan747e8f82005-07-25 12:33:22 -070014516 (tp->pci_chip_rev_id != CHIPREV_ID_5705_A1)) ||
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014517 (tp->phy_flags & TG3_PHYFLG_IS_FET) ||
14518 (tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
14519 tp->phy_flags |= TG3_PHYFLG_NO_ETH_WIRE_SPEED;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014520
14521 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5703_AX ||
14522 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5704_AX)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014523 tp->phy_flags |= TG3_PHYFLG_ADC_BUG;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014524 if (tp->pci_chip_rev_id == CHIPREV_ID_5704_A0)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014525 tp->phy_flags |= TG3_PHYFLG_5704_A0_BUG;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014526
Joe Perches63c3a662011-04-26 08:12:10 +000014527 if (tg3_flag(tp, 5705_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014528 !(tp->phy_flags & TG3_PHYFLG_IS_FET) &&
Matt Carlson321d32a2008-11-21 17:22:19 -080014529 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000014530 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_57780 &&
Joe Perches63c3a662011-04-26 08:12:10 +000014531 !tg3_flag(tp, 57765_PLUS)) {
Michael Chanc424cb22006-04-29 18:56:34 -070014532 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
Matt Carlsond30cdd22007-10-07 23:28:35 -070014533 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787 ||
Matt Carlson9936bcf2007-10-10 18:03:07 -070014534 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
14535 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761) {
Michael Chand4011ad2007-02-13 12:17:25 -080014536 if (tp->pdev->device != PCI_DEVICE_ID_TIGON3_5756 &&
14537 tp->pdev->device != PCI_DEVICE_ID_TIGON3_5722)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014538 tp->phy_flags |= TG3_PHYFLG_JITTER_BUG;
Michael Chanc1d2a192007-01-08 19:57:20 -080014539 if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5755M)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014540 tp->phy_flags |= TG3_PHYFLG_ADJUST_TRIM;
Matt Carlson321d32a2008-11-21 17:22:19 -080014541 } else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014542 tp->phy_flags |= TG3_PHYFLG_BER_BUG;
Michael Chanc424cb22006-04-29 18:56:34 -070014543 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014544
Matt Carlsonb2a5c192008-04-03 21:44:44 -070014545 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
14546 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX) {
14547 tp->phy_otp = tg3_read_otp_phycfg(tp);
14548 if (tp->phy_otp == 0)
14549 tp->phy_otp = TG3_OTP_DEFAULT;
14550 }
14551
Joe Perches63c3a662011-04-26 08:12:10 +000014552 if (tg3_flag(tp, CPMU_PRESENT))
Matt Carlson8ef21422008-05-02 16:47:53 -070014553 tp->mi_mode = MAC_MI_MODE_500KHZ_CONST;
14554 else
14555 tp->mi_mode = MAC_MI_MODE_BASE;
14556
Linus Torvalds1da177e2005-04-16 15:20:36 -070014557 tp->coalesce_mode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014558 if (GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5700_AX &&
14559 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5700_BX)
14560 tp->coalesce_mode |= HOSTCC_MODE_32BYTE;
14561
Matt Carlson4d958472011-04-20 07:57:35 +000014562 /* Set these bits to enable statistics workaround. */
14563 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
14564 tp->pci_chip_rev_id == CHIPREV_ID_5719_A0 ||
14565 tp->pci_chip_rev_id == CHIPREV_ID_5720_A0) {
14566 tp->coalesce_mode |= HOSTCC_MODE_ATTN;
14567 tp->grc_mode |= GRC_MODE_IRQ_ON_FLOW_ATTN;
14568 }
14569
Matt Carlson321d32a2008-11-21 17:22:19 -080014570 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
14571 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
Joe Perches63c3a662011-04-26 08:12:10 +000014572 tg3_flag_set(tp, USE_PHYLIB);
Matt Carlson57e69832008-05-25 23:48:31 -070014573
Matt Carlson158d7ab2008-05-29 01:37:54 -070014574 err = tg3_mdio_init(tp);
14575 if (err)
14576 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014577
14578 /* Initialize data/descriptor byte/word swapping. */
14579 val = tr32(GRC_MODE);
Matt Carlsonf2096f92011-04-05 14:22:48 +000014580 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
14581 val &= (GRC_MODE_BYTE_SWAP_B2HRX_DATA |
14582 GRC_MODE_WORD_SWAP_B2HRX_DATA |
14583 GRC_MODE_B2HRX_ENABLE |
14584 GRC_MODE_HTX2B_ENABLE |
14585 GRC_MODE_HOST_STACKUP);
14586 else
14587 val &= GRC_MODE_HOST_STACKUP;
14588
Linus Torvalds1da177e2005-04-16 15:20:36 -070014589 tw32(GRC_MODE, val | tp->grc_mode);
14590
14591 tg3_switch_clocks(tp);
14592
14593 /* Clear this out for sanity. */
14594 tw32(TG3PCI_MEM_WIN_BASE_ADDR, 0);
14595
14596 pci_read_config_dword(tp->pdev, TG3PCI_PCISTATE,
14597 &pci_state_reg);
14598 if ((pci_state_reg & PCISTATE_CONV_PCI_MODE) == 0 &&
Joe Perches63c3a662011-04-26 08:12:10 +000014599 !tg3_flag(tp, PCIX_TARGET_HWBUG)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070014600 u32 chiprevid = GET_CHIP_REV_ID(tp->misc_host_ctrl);
14601
14602 if (chiprevid == CHIPREV_ID_5701_A0 ||
14603 chiprevid == CHIPREV_ID_5701_B0 ||
14604 chiprevid == CHIPREV_ID_5701_B2 ||
14605 chiprevid == CHIPREV_ID_5701_B5) {
14606 void __iomem *sram_base;
14607
14608 /* Write some dummy words into the SRAM status block
14609 * area, see if it reads back correctly. If the return
14610 * value is bad, force enable the PCIX workaround.
14611 */
14612 sram_base = tp->regs + NIC_SRAM_WIN_BASE + NIC_SRAM_STATS_BLK;
14613
14614 writel(0x00000000, sram_base);
14615 writel(0x00000000, sram_base + 4);
14616 writel(0xffffffff, sram_base + 4);
14617 if (readl(sram_base) != 0x00000000)
Joe Perches63c3a662011-04-26 08:12:10 +000014618 tg3_flag_set(tp, PCIX_TARGET_HWBUG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014619 }
14620 }
14621
14622 udelay(50);
14623 tg3_nvram_init(tp);
14624
14625 grc_misc_cfg = tr32(GRC_MISC_CFG);
14626 grc_misc_cfg &= GRC_MISC_CFG_BOARD_ID_MASK;
14627
Linus Torvalds1da177e2005-04-16 15:20:36 -070014628 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
14629 (grc_misc_cfg == GRC_MISC_CFG_BOARD_ID_5788 ||
14630 grc_misc_cfg == GRC_MISC_CFG_BOARD_ID_5788M))
Joe Perches63c3a662011-04-26 08:12:10 +000014631 tg3_flag_set(tp, IS_5788);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014632
Joe Perches63c3a662011-04-26 08:12:10 +000014633 if (!tg3_flag(tp, IS_5788) &&
Matt Carlson6ff6f812011-05-19 12:12:54 +000014634 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700)
Joe Perches63c3a662011-04-26 08:12:10 +000014635 tg3_flag_set(tp, TAGGED_STATUS);
14636 if (tg3_flag(tp, TAGGED_STATUS)) {
David S. Millerfac9b832005-05-18 22:46:34 -070014637 tp->coalesce_mode |= (HOSTCC_MODE_CLRTICK_RXBD |
14638 HOSTCC_MODE_CLRTICK_TXBD);
14639
14640 tp->misc_host_ctrl |= MISC_HOST_CTRL_TAGGED_STATUS;
14641 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
14642 tp->misc_host_ctrl);
14643 }
14644
Matt Carlson3bda1252008-08-15 14:08:22 -070014645 /* Preserve the APE MAC_MODE bits */
Joe Perches63c3a662011-04-26 08:12:10 +000014646 if (tg3_flag(tp, ENABLE_APE))
Matt Carlsond2394e6b2010-11-24 08:31:47 +000014647 tp->mac_mode = MAC_MODE_APE_TX_EN | MAC_MODE_APE_RX_EN;
Matt Carlson3bda1252008-08-15 14:08:22 -070014648 else
Matt Carlson6e01b202011-08-19 13:58:20 +000014649 tp->mac_mode = 0;
Matt Carlson3bda1252008-08-15 14:08:22 -070014650
Linus Torvalds1da177e2005-04-16 15:20:36 -070014651 /* these are limited to 10/100 only */
14652 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 &&
14653 (grc_misc_cfg == 0x8000 || grc_misc_cfg == 0x4000)) ||
14654 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
14655 tp->pdev->vendor == PCI_VENDOR_ID_BROADCOM &&
14656 (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5901 ||
14657 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5901_2 ||
14658 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5705F)) ||
14659 (tp->pdev->vendor == PCI_VENDOR_ID_BROADCOM &&
14660 (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5751F ||
Michael Chan676917d2006-12-07 00:20:22 -080014661 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5753F ||
14662 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5787F)) ||
Matt Carlson321d32a2008-11-21 17:22:19 -080014663 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57790 ||
Matt Carlsond1101142010-02-17 15:16:55 +000014664 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57791 ||
14665 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57795 ||
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014666 (tp->phy_flags & TG3_PHYFLG_IS_FET))
14667 tp->phy_flags |= TG3_PHYFLG_10_100_ONLY;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014668
14669 err = tg3_phy_probe(tp);
14670 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000014671 dev_err(&tp->pdev->dev, "phy probe failed, err %d\n", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014672 /* ... but do not return immediately ... */
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070014673 tg3_mdio_fini(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014674 }
14675
Matt Carlson184b8902010-04-05 10:19:25 +000014676 tg3_read_vpd(tp);
Michael Chanc4e65752006-03-20 22:29:32 -080014677 tg3_read_fw_ver(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014678
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014679 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
14680 tp->phy_flags &= ~TG3_PHYFLG_USE_MI_INTERRUPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014681 } else {
14682 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014683 tp->phy_flags |= TG3_PHYFLG_USE_MI_INTERRUPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014684 else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014685 tp->phy_flags &= ~TG3_PHYFLG_USE_MI_INTERRUPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014686 }
14687
14688 /* 5700 {AX,BX} chips have a broken status block link
14689 * change bit implementation, so we must use the
14690 * status register in those cases.
14691 */
14692 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700)
Joe Perches63c3a662011-04-26 08:12:10 +000014693 tg3_flag_set(tp, USE_LINKCHG_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014694 else
Joe Perches63c3a662011-04-26 08:12:10 +000014695 tg3_flag_clear(tp, USE_LINKCHG_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014696
14697 /* The led_ctrl is set during tg3_phy_probe, here we might
14698 * have to force the link status polling mechanism based
14699 * upon subsystem IDs.
14700 */
14701 if (tp->pdev->subsystem_vendor == PCI_VENDOR_ID_DELL &&
Michael Chan007a880d2007-05-31 14:49:51 -070014702 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701 &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014703 !(tp->phy_flags & TG3_PHYFLG_PHY_SERDES)) {
14704 tp->phy_flags |= TG3_PHYFLG_USE_MI_INTERRUPT;
Joe Perches63c3a662011-04-26 08:12:10 +000014705 tg3_flag_set(tp, USE_LINKCHG_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014706 }
14707
14708 /* For all SERDES we poll the MAC status register. */
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014709 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Joe Perches63c3a662011-04-26 08:12:10 +000014710 tg3_flag_set(tp, POLL_SERDES);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014711 else
Joe Perches63c3a662011-04-26 08:12:10 +000014712 tg3_flag_clear(tp, POLL_SERDES);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014713
Eric Dumazet9205fd92011-11-18 06:47:01 +000014714 tp->rx_offset = NET_SKB_PAD + NET_IP_ALIGN;
Matt Carlsond2757fc2010-04-12 06:58:27 +000014715 tp->rx_copy_thresh = TG3_RX_COPY_THRESHOLD;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014716 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701 &&
Joe Perches63c3a662011-04-26 08:12:10 +000014717 tg3_flag(tp, PCIX_MODE)) {
Eric Dumazet9205fd92011-11-18 06:47:01 +000014718 tp->rx_offset = NET_SKB_PAD;
Matt Carlsond2757fc2010-04-12 06:58:27 +000014719#ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
Matt Carlson9dc7a112010-04-12 06:58:28 +000014720 tp->rx_copy_thresh = ~(u16)0;
Matt Carlsond2757fc2010-04-12 06:58:27 +000014721#endif
14722 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014723
Matt Carlson2c49a442010-09-30 10:34:35 +000014724 tp->rx_std_ring_mask = TG3_RX_STD_RING_SIZE(tp) - 1;
14725 tp->rx_jmb_ring_mask = TG3_RX_JMB_RING_SIZE(tp) - 1;
Matt Carlson7cb32cf2010-09-30 10:34:36 +000014726 tp->rx_ret_ring_mask = tg3_rx_ret_ring_size(tp) - 1;
14727
Matt Carlson2c49a442010-09-30 10:34:35 +000014728 tp->rx_std_max_post = tp->rx_std_ring_mask + 1;
Michael Chanf92905d2006-06-29 20:14:29 -070014729
14730 /* Increment the rx prod index on the rx std ring by at most
14731 * 8 for these chips to workaround hw errata.
14732 */
14733 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 ||
14734 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752 ||
14735 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755)
14736 tp->rx_std_max_post = 8;
14737
Joe Perches63c3a662011-04-26 08:12:10 +000014738 if (tg3_flag(tp, ASPM_WORKAROUND))
Matt Carlson8ed5d972007-05-07 00:25:49 -070014739 tp->pwrmgmt_thresh = tr32(PCIE_PWR_MGMT_THRESH) &
14740 PCIE_PWR_MGMT_L1_THRESH_MSK;
14741
Linus Torvalds1da177e2005-04-16 15:20:36 -070014742 return err;
14743}
14744
David S. Miller49b6e95f2007-03-29 01:38:42 -070014745#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070014746static int __devinit tg3_get_macaddr_sparc(struct tg3 *tp)
14747{
14748 struct net_device *dev = tp->dev;
14749 struct pci_dev *pdev = tp->pdev;
David S. Miller49b6e95f2007-03-29 01:38:42 -070014750 struct device_node *dp = pci_device_to_OF_node(pdev);
David S. Miller374d4ca2007-03-29 01:57:57 -070014751 const unsigned char *addr;
David S. Miller49b6e95f2007-03-29 01:38:42 -070014752 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014753
David S. Miller49b6e95f2007-03-29 01:38:42 -070014754 addr = of_get_property(dp, "local-mac-address", &len);
14755 if (addr && len == 6) {
14756 memcpy(dev->dev_addr, addr, 6);
14757 memcpy(dev->perm_addr, dev->dev_addr, 6);
14758 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014759 }
14760 return -ENODEV;
14761}
14762
14763static int __devinit tg3_get_default_macaddr_sparc(struct tg3 *tp)
14764{
14765 struct net_device *dev = tp->dev;
14766
14767 memcpy(dev->dev_addr, idprom->id_ethaddr, 6);
John W. Linville2ff43692005-09-12 14:44:20 -070014768 memcpy(dev->perm_addr, idprom->id_ethaddr, 6);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014769 return 0;
14770}
14771#endif
14772
14773static int __devinit tg3_get_device_address(struct tg3 *tp)
14774{
14775 struct net_device *dev = tp->dev;
14776 u32 hi, lo, mac_offset;
Michael Chan008652b2006-03-27 23:14:53 -080014777 int addr_ok = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014778
David S. Miller49b6e95f2007-03-29 01:38:42 -070014779#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070014780 if (!tg3_get_macaddr_sparc(tp))
14781 return 0;
14782#endif
14783
14784 mac_offset = 0x7c;
Matt Carlson6ff6f812011-05-19 12:12:54 +000014785 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
Joe Perches63c3a662011-04-26 08:12:10 +000014786 tg3_flag(tp, 5780_CLASS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070014787 if (tr32(TG3PCI_DUAL_MAC_CTRL) & DUAL_MAC_CTRL_ID)
14788 mac_offset = 0xcc;
14789 if (tg3_nvram_lock(tp))
14790 tw32_f(NVRAM_CMD, NVRAM_CMD_RESET);
14791 else
14792 tg3_nvram_unlock(tp);
Joe Perches63c3a662011-04-26 08:12:10 +000014793 } else if (tg3_flag(tp, 5717_PLUS)) {
Matt Carlson69f11c92011-07-13 09:27:30 +000014794 if (tp->pci_fn & 1)
Matt Carlsona1b950d2009-09-01 13:20:17 +000014795 mac_offset = 0xcc;
Matt Carlson69f11c92011-07-13 09:27:30 +000014796 if (tp->pci_fn > 1)
Matt Carlsona50d0792010-06-05 17:24:37 +000014797 mac_offset += 0x18c;
Matt Carlsona1b950d2009-09-01 13:20:17 +000014798 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
Michael Chanb5d37722006-09-27 16:06:21 -070014799 mac_offset = 0x10;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014800
14801 /* First try to get it from MAC address mailbox. */
14802 tg3_read_mem(tp, NIC_SRAM_MAC_ADDR_HIGH_MBOX, &hi);
14803 if ((hi >> 16) == 0x484b) {
14804 dev->dev_addr[0] = (hi >> 8) & 0xff;
14805 dev->dev_addr[1] = (hi >> 0) & 0xff;
14806
14807 tg3_read_mem(tp, NIC_SRAM_MAC_ADDR_LOW_MBOX, &lo);
14808 dev->dev_addr[2] = (lo >> 24) & 0xff;
14809 dev->dev_addr[3] = (lo >> 16) & 0xff;
14810 dev->dev_addr[4] = (lo >> 8) & 0xff;
14811 dev->dev_addr[5] = (lo >> 0) & 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014812
Michael Chan008652b2006-03-27 23:14:53 -080014813 /* Some old bootcode may report a 0 MAC address in SRAM */
14814 addr_ok = is_valid_ether_addr(&dev->dev_addr[0]);
14815 }
14816 if (!addr_ok) {
14817 /* Next, try NVRAM. */
Joe Perches63c3a662011-04-26 08:12:10 +000014818 if (!tg3_flag(tp, NO_NVRAM) &&
Matt Carlsondf259d82009-04-20 06:57:14 +000014819 !tg3_nvram_read_be32(tp, mac_offset + 0, &hi) &&
Matt Carlson6d348f22009-02-25 14:25:52 +000014820 !tg3_nvram_read_be32(tp, mac_offset + 4, &lo)) {
Matt Carlson62cedd12009-04-20 14:52:29 -070014821 memcpy(&dev->dev_addr[0], ((char *)&hi) + 2, 2);
14822 memcpy(&dev->dev_addr[2], (char *)&lo, sizeof(lo));
Michael Chan008652b2006-03-27 23:14:53 -080014823 }
14824 /* Finally just fetch it out of the MAC control regs. */
14825 else {
14826 hi = tr32(MAC_ADDR_0_HIGH);
14827 lo = tr32(MAC_ADDR_0_LOW);
14828
14829 dev->dev_addr[5] = lo & 0xff;
14830 dev->dev_addr[4] = (lo >> 8) & 0xff;
14831 dev->dev_addr[3] = (lo >> 16) & 0xff;
14832 dev->dev_addr[2] = (lo >> 24) & 0xff;
14833 dev->dev_addr[1] = hi & 0xff;
14834 dev->dev_addr[0] = (hi >> 8) & 0xff;
14835 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014836 }
14837
14838 if (!is_valid_ether_addr(&dev->dev_addr[0])) {
David S. Miller7582a332008-03-20 15:53:15 -070014839#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070014840 if (!tg3_get_default_macaddr_sparc(tp))
14841 return 0;
14842#endif
14843 return -EINVAL;
14844 }
John W. Linville2ff43692005-09-12 14:44:20 -070014845 memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014846 return 0;
14847}
14848
David S. Miller59e6b432005-05-18 22:50:10 -070014849#define BOUNDARY_SINGLE_CACHELINE 1
14850#define BOUNDARY_MULTI_CACHELINE 2
14851
14852static u32 __devinit tg3_calc_dma_bndry(struct tg3 *tp, u32 val)
14853{
14854 int cacheline_size;
14855 u8 byte;
14856 int goal;
14857
14858 pci_read_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE, &byte);
14859 if (byte == 0)
14860 cacheline_size = 1024;
14861 else
14862 cacheline_size = (int) byte * 4;
14863
14864 /* On 5703 and later chips, the boundary bits have no
14865 * effect.
14866 */
14867 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
14868 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701 &&
Joe Perches63c3a662011-04-26 08:12:10 +000014869 !tg3_flag(tp, PCI_EXPRESS))
David S. Miller59e6b432005-05-18 22:50:10 -070014870 goto out;
14871
14872#if defined(CONFIG_PPC64) || defined(CONFIG_IA64) || defined(CONFIG_PARISC)
14873 goal = BOUNDARY_MULTI_CACHELINE;
14874#else
14875#if defined(CONFIG_SPARC64) || defined(CONFIG_ALPHA)
14876 goal = BOUNDARY_SINGLE_CACHELINE;
14877#else
14878 goal = 0;
14879#endif
14880#endif
14881
Joe Perches63c3a662011-04-26 08:12:10 +000014882 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000014883 val = goal ? 0 : DMA_RWCTRL_DIS_CACHE_ALIGNMENT;
14884 goto out;
14885 }
14886
David S. Miller59e6b432005-05-18 22:50:10 -070014887 if (!goal)
14888 goto out;
14889
14890 /* PCI controllers on most RISC systems tend to disconnect
14891 * when a device tries to burst across a cache-line boundary.
14892 * Therefore, letting tg3 do so just wastes PCI bandwidth.
14893 *
14894 * Unfortunately, for PCI-E there are only limited
14895 * write-side controls for this, and thus for reads
14896 * we will still get the disconnects. We'll also waste
14897 * these PCI cycles for both read and write for chips
14898 * other than 5700 and 5701 which do not implement the
14899 * boundary bits.
14900 */
Joe Perches63c3a662011-04-26 08:12:10 +000014901 if (tg3_flag(tp, PCIX_MODE) && !tg3_flag(tp, PCI_EXPRESS)) {
David S. Miller59e6b432005-05-18 22:50:10 -070014902 switch (cacheline_size) {
14903 case 16:
14904 case 32:
14905 case 64:
14906 case 128:
14907 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14908 val |= (DMA_RWCTRL_READ_BNDRY_128_PCIX |
14909 DMA_RWCTRL_WRITE_BNDRY_128_PCIX);
14910 } else {
14911 val |= (DMA_RWCTRL_READ_BNDRY_384_PCIX |
14912 DMA_RWCTRL_WRITE_BNDRY_384_PCIX);
14913 }
14914 break;
14915
14916 case 256:
14917 val |= (DMA_RWCTRL_READ_BNDRY_256_PCIX |
14918 DMA_RWCTRL_WRITE_BNDRY_256_PCIX);
14919 break;
14920
14921 default:
14922 val |= (DMA_RWCTRL_READ_BNDRY_384_PCIX |
14923 DMA_RWCTRL_WRITE_BNDRY_384_PCIX);
14924 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070014925 }
Joe Perches63c3a662011-04-26 08:12:10 +000014926 } else if (tg3_flag(tp, PCI_EXPRESS)) {
David S. Miller59e6b432005-05-18 22:50:10 -070014927 switch (cacheline_size) {
14928 case 16:
14929 case 32:
14930 case 64:
14931 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14932 val &= ~DMA_RWCTRL_WRITE_BNDRY_DISAB_PCIE;
14933 val |= DMA_RWCTRL_WRITE_BNDRY_64_PCIE;
14934 break;
14935 }
14936 /* fallthrough */
14937 case 128:
14938 default:
14939 val &= ~DMA_RWCTRL_WRITE_BNDRY_DISAB_PCIE;
14940 val |= DMA_RWCTRL_WRITE_BNDRY_128_PCIE;
14941 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070014942 }
David S. Miller59e6b432005-05-18 22:50:10 -070014943 } else {
14944 switch (cacheline_size) {
14945 case 16:
14946 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14947 val |= (DMA_RWCTRL_READ_BNDRY_16 |
14948 DMA_RWCTRL_WRITE_BNDRY_16);
14949 break;
14950 }
14951 /* fallthrough */
14952 case 32:
14953 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14954 val |= (DMA_RWCTRL_READ_BNDRY_32 |
14955 DMA_RWCTRL_WRITE_BNDRY_32);
14956 break;
14957 }
14958 /* fallthrough */
14959 case 64:
14960 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14961 val |= (DMA_RWCTRL_READ_BNDRY_64 |
14962 DMA_RWCTRL_WRITE_BNDRY_64);
14963 break;
14964 }
14965 /* fallthrough */
14966 case 128:
14967 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14968 val |= (DMA_RWCTRL_READ_BNDRY_128 |
14969 DMA_RWCTRL_WRITE_BNDRY_128);
14970 break;
14971 }
14972 /* fallthrough */
14973 case 256:
14974 val |= (DMA_RWCTRL_READ_BNDRY_256 |
14975 DMA_RWCTRL_WRITE_BNDRY_256);
14976 break;
14977 case 512:
14978 val |= (DMA_RWCTRL_READ_BNDRY_512 |
14979 DMA_RWCTRL_WRITE_BNDRY_512);
14980 break;
14981 case 1024:
14982 default:
14983 val |= (DMA_RWCTRL_READ_BNDRY_1024 |
14984 DMA_RWCTRL_WRITE_BNDRY_1024);
14985 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070014986 }
David S. Miller59e6b432005-05-18 22:50:10 -070014987 }
14988
14989out:
14990 return val;
14991}
14992
Linus Torvalds1da177e2005-04-16 15:20:36 -070014993static int __devinit tg3_do_test_dma(struct tg3 *tp, u32 *buf, dma_addr_t buf_dma, int size, int to_device)
14994{
14995 struct tg3_internal_buffer_desc test_desc;
14996 u32 sram_dma_descs;
14997 int i, ret;
14998
14999 sram_dma_descs = NIC_SRAM_DMA_DESC_POOL_BASE;
15000
15001 tw32(FTQ_RCVBD_COMP_FIFO_ENQDEQ, 0);
15002 tw32(FTQ_RCVDATA_COMP_FIFO_ENQDEQ, 0);
15003 tw32(RDMAC_STATUS, 0);
15004 tw32(WDMAC_STATUS, 0);
15005
15006 tw32(BUFMGR_MODE, 0);
15007 tw32(FTQ_RESET, 0);
15008
15009 test_desc.addr_hi = ((u64) buf_dma) >> 32;
15010 test_desc.addr_lo = buf_dma & 0xffffffff;
15011 test_desc.nic_mbuf = 0x00002100;
15012 test_desc.len = size;
15013
15014 /*
15015 * HP ZX1 was seeing test failures for 5701 cards running at 33Mhz
15016 * the *second* time the tg3 driver was getting loaded after an
15017 * initial scan.
15018 *
15019 * Broadcom tells me:
15020 * ...the DMA engine is connected to the GRC block and a DMA
15021 * reset may affect the GRC block in some unpredictable way...
15022 * The behavior of resets to individual blocks has not been tested.
15023 *
15024 * Broadcom noted the GRC reset will also reset all sub-components.
15025 */
15026 if (to_device) {
15027 test_desc.cqid_sqid = (13 << 8) | 2;
15028
15029 tw32_f(RDMAC_MODE, RDMAC_MODE_ENABLE);
15030 udelay(40);
15031 } else {
15032 test_desc.cqid_sqid = (16 << 8) | 7;
15033
15034 tw32_f(WDMAC_MODE, WDMAC_MODE_ENABLE);
15035 udelay(40);
15036 }
15037 test_desc.flags = 0x00000005;
15038
15039 for (i = 0; i < (sizeof(test_desc) / sizeof(u32)); i++) {
15040 u32 val;
15041
15042 val = *(((u32 *)&test_desc) + i);
15043 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR,
15044 sram_dma_descs + (i * sizeof(u32)));
15045 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_DATA, val);
15046 }
15047 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, 0);
15048
Matt Carlson859a588792010-04-05 10:19:28 +000015049 if (to_device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015050 tw32(FTQ_DMA_HIGH_READ_FIFO_ENQDEQ, sram_dma_descs);
Matt Carlson859a588792010-04-05 10:19:28 +000015051 else
Linus Torvalds1da177e2005-04-16 15:20:36 -070015052 tw32(FTQ_DMA_HIGH_WRITE_FIFO_ENQDEQ, sram_dma_descs);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015053
15054 ret = -ENODEV;
15055 for (i = 0; i < 40; i++) {
15056 u32 val;
15057
15058 if (to_device)
15059 val = tr32(FTQ_RCVBD_COMP_FIFO_ENQDEQ);
15060 else
15061 val = tr32(FTQ_RCVDATA_COMP_FIFO_ENQDEQ);
15062 if ((val & 0xffff) == sram_dma_descs) {
15063 ret = 0;
15064 break;
15065 }
15066
15067 udelay(100);
15068 }
15069
15070 return ret;
15071}
15072
David S. Millerded73402005-05-23 13:59:47 -070015073#define TEST_BUFFER_SIZE 0x2000
Linus Torvalds1da177e2005-04-16 15:20:36 -070015074
Matt Carlson41434702011-03-09 16:58:22 +000015075static DEFINE_PCI_DEVICE_TABLE(tg3_dma_wait_state_chipsets) = {
Joe Perches895950c2010-12-21 02:16:08 -080015076 { PCI_DEVICE(PCI_VENDOR_ID_APPLE, PCI_DEVICE_ID_APPLE_UNI_N_PCI15) },
15077 { },
15078};
15079
Linus Torvalds1da177e2005-04-16 15:20:36 -070015080static int __devinit tg3_test_dma(struct tg3 *tp)
15081{
15082 dma_addr_t buf_dma;
David S. Miller59e6b432005-05-18 22:50:10 -070015083 u32 *buf, saved_dma_rwctrl;
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000015084 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015085
Matt Carlson4bae65c2010-11-24 08:31:52 +000015086 buf = dma_alloc_coherent(&tp->pdev->dev, TEST_BUFFER_SIZE,
15087 &buf_dma, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015088 if (!buf) {
15089 ret = -ENOMEM;
15090 goto out_nofree;
15091 }
15092
15093 tp->dma_rwctrl = ((0x7 << DMA_RWCTRL_PCI_WRITE_CMD_SHIFT) |
15094 (0x6 << DMA_RWCTRL_PCI_READ_CMD_SHIFT));
15095
David S. Miller59e6b432005-05-18 22:50:10 -070015096 tp->dma_rwctrl = tg3_calc_dma_bndry(tp, tp->dma_rwctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015097
Joe Perches63c3a662011-04-26 08:12:10 +000015098 if (tg3_flag(tp, 57765_PLUS))
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000015099 goto out;
15100
Joe Perches63c3a662011-04-26 08:12:10 +000015101 if (tg3_flag(tp, PCI_EXPRESS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070015102 /* DMA read watermark not used on PCIE */
15103 tp->dma_rwctrl |= 0x00180000;
Joe Perches63c3a662011-04-26 08:12:10 +000015104 } else if (!tg3_flag(tp, PCIX_MODE)) {
Michael Chan85e94ce2005-04-21 17:05:28 -070015105 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 ||
15106 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015107 tp->dma_rwctrl |= 0x003f0000;
15108 else
15109 tp->dma_rwctrl |= 0x003f000f;
15110 } else {
15111 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
15112 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
15113 u32 ccval = (tr32(TG3PCI_CLOCK_CTRL) & 0x1f);
Michael Chan49afdeb2007-02-13 12:17:03 -080015114 u32 read_water = 0x7;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015115
Michael Chan4a29cc22006-03-19 13:21:12 -080015116 /* If the 5704 is behind the EPB bridge, we can
15117 * do the less restrictive ONE_DMA workaround for
15118 * better performance.
15119 */
Joe Perches63c3a662011-04-26 08:12:10 +000015120 if (tg3_flag(tp, 40BIT_DMA_BUG) &&
Michael Chan4a29cc22006-03-19 13:21:12 -080015121 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704)
15122 tp->dma_rwctrl |= 0x8000;
15123 else if (ccval == 0x6 || ccval == 0x7)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015124 tp->dma_rwctrl |= DMA_RWCTRL_ONE_DMA;
15125
Michael Chan49afdeb2007-02-13 12:17:03 -080015126 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703)
15127 read_water = 4;
David S. Miller59e6b432005-05-18 22:50:10 -070015128 /* Set bit 23 to enable PCIX hw bug fix */
Michael Chan49afdeb2007-02-13 12:17:03 -080015129 tp->dma_rwctrl |=
15130 (read_water << DMA_RWCTRL_READ_WATER_SHIFT) |
15131 (0x3 << DMA_RWCTRL_WRITE_WATER_SHIFT) |
15132 (1 << 23);
Michael Chan4cf78e42005-07-25 12:29:19 -070015133 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780) {
15134 /* 5780 always in PCIX mode */
15135 tp->dma_rwctrl |= 0x00144000;
Michael Chana4e2b342005-10-26 15:46:52 -070015136 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
15137 /* 5714 always in PCIX mode */
15138 tp->dma_rwctrl |= 0x00148000;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015139 } else {
15140 tp->dma_rwctrl |= 0x001b000f;
15141 }
15142 }
15143
15144 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
15145 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704)
15146 tp->dma_rwctrl &= 0xfffffff0;
15147
15148 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
15149 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
15150 /* Remove this if it causes problems for some boards. */
15151 tp->dma_rwctrl |= DMA_RWCTRL_USE_MEM_READ_MULT;
15152
15153 /* On 5700/5701 chips, we need to set this bit.
15154 * Otherwise the chip will issue cacheline transactions
15155 * to streamable DMA memory with not all the byte
15156 * enables turned on. This is an error on several
15157 * RISC PCI controllers, in particular sparc64.
15158 *
15159 * On 5703/5704 chips, this bit has been reassigned
15160 * a different meaning. In particular, it is used
15161 * on those chips to enable a PCI-X workaround.
15162 */
15163 tp->dma_rwctrl |= DMA_RWCTRL_ASSERT_ALL_BE;
15164 }
15165
15166 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
15167
15168#if 0
15169 /* Unneeded, already done by tg3_get_invariants. */
15170 tg3_switch_clocks(tp);
15171#endif
15172
Linus Torvalds1da177e2005-04-16 15:20:36 -070015173 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
15174 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701)
15175 goto out;
15176
David S. Miller59e6b432005-05-18 22:50:10 -070015177 /* It is best to perform DMA test with maximum write burst size
15178 * to expose the 5700/5701 write DMA bug.
15179 */
15180 saved_dma_rwctrl = tp->dma_rwctrl;
15181 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
15182 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
15183
Linus Torvalds1da177e2005-04-16 15:20:36 -070015184 while (1) {
15185 u32 *p = buf, i;
15186
15187 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++)
15188 p[i] = i;
15189
15190 /* Send the buffer to the chip. */
15191 ret = tg3_do_test_dma(tp, buf, buf_dma, TEST_BUFFER_SIZE, 1);
15192 if (ret) {
Matt Carlson2445e462010-04-05 10:19:21 +000015193 dev_err(&tp->pdev->dev,
15194 "%s: Buffer write failed. err = %d\n",
15195 __func__, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015196 break;
15197 }
15198
15199#if 0
15200 /* validate data reached card RAM correctly. */
15201 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++) {
15202 u32 val;
15203 tg3_read_mem(tp, 0x2100 + (i*4), &val);
15204 if (le32_to_cpu(val) != p[i]) {
Matt Carlson2445e462010-04-05 10:19:21 +000015205 dev_err(&tp->pdev->dev,
15206 "%s: Buffer corrupted on device! "
15207 "(%d != %d)\n", __func__, val, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015208 /* ret = -ENODEV here? */
15209 }
15210 p[i] = 0;
15211 }
15212#endif
15213 /* Now read it back. */
15214 ret = tg3_do_test_dma(tp, buf, buf_dma, TEST_BUFFER_SIZE, 0);
15215 if (ret) {
Matt Carlson5129c3a2010-04-05 10:19:23 +000015216 dev_err(&tp->pdev->dev, "%s: Buffer read failed. "
15217 "err = %d\n", __func__, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015218 break;
15219 }
15220
15221 /* Verify it. */
15222 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++) {
15223 if (p[i] == i)
15224 continue;
15225
David S. Miller59e6b432005-05-18 22:50:10 -070015226 if ((tp->dma_rwctrl & DMA_RWCTRL_WRITE_BNDRY_MASK) !=
15227 DMA_RWCTRL_WRITE_BNDRY_16) {
15228 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015229 tp->dma_rwctrl |= DMA_RWCTRL_WRITE_BNDRY_16;
15230 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
15231 break;
15232 } else {
Matt Carlson2445e462010-04-05 10:19:21 +000015233 dev_err(&tp->pdev->dev,
15234 "%s: Buffer corrupted on read back! "
15235 "(%d != %d)\n", __func__, p[i], i);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015236 ret = -ENODEV;
15237 goto out;
15238 }
15239 }
15240
15241 if (i == (TEST_BUFFER_SIZE / sizeof(u32))) {
15242 /* Success. */
15243 ret = 0;
15244 break;
15245 }
15246 }
David S. Miller59e6b432005-05-18 22:50:10 -070015247 if ((tp->dma_rwctrl & DMA_RWCTRL_WRITE_BNDRY_MASK) !=
15248 DMA_RWCTRL_WRITE_BNDRY_16) {
15249 /* DMA test passed without adjusting DMA boundary,
Michael Chan6d1cfba2005-06-08 14:13:14 -070015250 * now look for chipsets that are known to expose the
15251 * DMA bug without failing the test.
David S. Miller59e6b432005-05-18 22:50:10 -070015252 */
Matt Carlson41434702011-03-09 16:58:22 +000015253 if (pci_dev_present(tg3_dma_wait_state_chipsets)) {
Michael Chan6d1cfba2005-06-08 14:13:14 -070015254 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
15255 tp->dma_rwctrl |= DMA_RWCTRL_WRITE_BNDRY_16;
Matt Carlson859a588792010-04-05 10:19:28 +000015256 } else {
Michael Chan6d1cfba2005-06-08 14:13:14 -070015257 /* Safe to use the calculated DMA boundary. */
15258 tp->dma_rwctrl = saved_dma_rwctrl;
Matt Carlson859a588792010-04-05 10:19:28 +000015259 }
Michael Chan6d1cfba2005-06-08 14:13:14 -070015260
David S. Miller59e6b432005-05-18 22:50:10 -070015261 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
15262 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015263
15264out:
Matt Carlson4bae65c2010-11-24 08:31:52 +000015265 dma_free_coherent(&tp->pdev->dev, TEST_BUFFER_SIZE, buf, buf_dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015266out_nofree:
15267 return ret;
15268}
15269
Linus Torvalds1da177e2005-04-16 15:20:36 -070015270static void __devinit tg3_init_bufmgr_config(struct tg3 *tp)
15271{
Joe Perches63c3a662011-04-26 08:12:10 +000015272 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlson666bc832010-01-20 16:58:03 +000015273 tp->bufmgr_config.mbuf_read_dma_low_water =
15274 DEFAULT_MB_RDMA_LOW_WATER_5705;
15275 tp->bufmgr_config.mbuf_mac_rx_low_water =
15276 DEFAULT_MB_MACRX_LOW_WATER_57765;
15277 tp->bufmgr_config.mbuf_high_water =
15278 DEFAULT_MB_HIGH_WATER_57765;
15279
15280 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
15281 DEFAULT_MB_RDMA_LOW_WATER_5705;
15282 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
15283 DEFAULT_MB_MACRX_LOW_WATER_JUMBO_57765;
15284 tp->bufmgr_config.mbuf_high_water_jumbo =
15285 DEFAULT_MB_HIGH_WATER_JUMBO_57765;
Joe Perches63c3a662011-04-26 08:12:10 +000015286 } else if (tg3_flag(tp, 5705_PLUS)) {
Michael Chanfdfec1722005-07-25 12:31:48 -070015287 tp->bufmgr_config.mbuf_read_dma_low_water =
15288 DEFAULT_MB_RDMA_LOW_WATER_5705;
15289 tp->bufmgr_config.mbuf_mac_rx_low_water =
15290 DEFAULT_MB_MACRX_LOW_WATER_5705;
15291 tp->bufmgr_config.mbuf_high_water =
15292 DEFAULT_MB_HIGH_WATER_5705;
Michael Chanb5d37722006-09-27 16:06:21 -070015293 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
15294 tp->bufmgr_config.mbuf_mac_rx_low_water =
15295 DEFAULT_MB_MACRX_LOW_WATER_5906;
15296 tp->bufmgr_config.mbuf_high_water =
15297 DEFAULT_MB_HIGH_WATER_5906;
15298 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015299
Michael Chanfdfec1722005-07-25 12:31:48 -070015300 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
15301 DEFAULT_MB_RDMA_LOW_WATER_JUMBO_5780;
15302 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
15303 DEFAULT_MB_MACRX_LOW_WATER_JUMBO_5780;
15304 tp->bufmgr_config.mbuf_high_water_jumbo =
15305 DEFAULT_MB_HIGH_WATER_JUMBO_5780;
15306 } else {
15307 tp->bufmgr_config.mbuf_read_dma_low_water =
15308 DEFAULT_MB_RDMA_LOW_WATER;
15309 tp->bufmgr_config.mbuf_mac_rx_low_water =
15310 DEFAULT_MB_MACRX_LOW_WATER;
15311 tp->bufmgr_config.mbuf_high_water =
15312 DEFAULT_MB_HIGH_WATER;
15313
15314 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
15315 DEFAULT_MB_RDMA_LOW_WATER_JUMBO;
15316 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
15317 DEFAULT_MB_MACRX_LOW_WATER_JUMBO;
15318 tp->bufmgr_config.mbuf_high_water_jumbo =
15319 DEFAULT_MB_HIGH_WATER_JUMBO;
15320 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015321
15322 tp->bufmgr_config.dma_low_water = DEFAULT_DMA_LOW_WATER;
15323 tp->bufmgr_config.dma_high_water = DEFAULT_DMA_HIGH_WATER;
15324}
15325
15326static char * __devinit tg3_phy_string(struct tg3 *tp)
15327{
Matt Carlson79eb6902010-02-17 15:17:03 +000015328 switch (tp->phy_id & TG3_PHY_ID_MASK) {
15329 case TG3_PHY_ID_BCM5400: return "5400";
15330 case TG3_PHY_ID_BCM5401: return "5401";
15331 case TG3_PHY_ID_BCM5411: return "5411";
15332 case TG3_PHY_ID_BCM5701: return "5701";
15333 case TG3_PHY_ID_BCM5703: return "5703";
15334 case TG3_PHY_ID_BCM5704: return "5704";
15335 case TG3_PHY_ID_BCM5705: return "5705";
15336 case TG3_PHY_ID_BCM5750: return "5750";
15337 case TG3_PHY_ID_BCM5752: return "5752";
15338 case TG3_PHY_ID_BCM5714: return "5714";
15339 case TG3_PHY_ID_BCM5780: return "5780";
15340 case TG3_PHY_ID_BCM5755: return "5755";
15341 case TG3_PHY_ID_BCM5787: return "5787";
15342 case TG3_PHY_ID_BCM5784: return "5784";
15343 case TG3_PHY_ID_BCM5756: return "5722/5756";
15344 case TG3_PHY_ID_BCM5906: return "5906";
15345 case TG3_PHY_ID_BCM5761: return "5761";
15346 case TG3_PHY_ID_BCM5718C: return "5718C";
15347 case TG3_PHY_ID_BCM5718S: return "5718S";
15348 case TG3_PHY_ID_BCM57765: return "57765";
Matt Carlson302b5002010-06-05 17:24:38 +000015349 case TG3_PHY_ID_BCM5719C: return "5719C";
Matt Carlson6418f2c2011-04-05 14:22:49 +000015350 case TG3_PHY_ID_BCM5720C: return "5720C";
Matt Carlson79eb6902010-02-17 15:17:03 +000015351 case TG3_PHY_ID_BCM8002: return "8002/serdes";
Linus Torvalds1da177e2005-04-16 15:20:36 -070015352 case 0: return "serdes";
15353 default: return "unknown";
Stephen Hemminger855e1112008-04-16 16:37:28 -070015354 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015355}
15356
Michael Chanf9804dd2005-09-27 12:13:10 -070015357static char * __devinit tg3_bus_string(struct tg3 *tp, char *str)
15358{
Joe Perches63c3a662011-04-26 08:12:10 +000015359 if (tg3_flag(tp, PCI_EXPRESS)) {
Michael Chanf9804dd2005-09-27 12:13:10 -070015360 strcpy(str, "PCI Express");
15361 return str;
Joe Perches63c3a662011-04-26 08:12:10 +000015362 } else if (tg3_flag(tp, PCIX_MODE)) {
Michael Chanf9804dd2005-09-27 12:13:10 -070015363 u32 clock_ctrl = tr32(TG3PCI_CLOCK_CTRL) & 0x1f;
15364
15365 strcpy(str, "PCIX:");
15366
15367 if ((clock_ctrl == 7) ||
15368 ((tr32(GRC_MISC_CFG) & GRC_MISC_CFG_BOARD_ID_MASK) ==
15369 GRC_MISC_CFG_BOARD_ID_5704CIOBE))
15370 strcat(str, "133MHz");
15371 else if (clock_ctrl == 0)
15372 strcat(str, "33MHz");
15373 else if (clock_ctrl == 2)
15374 strcat(str, "50MHz");
15375 else if (clock_ctrl == 4)
15376 strcat(str, "66MHz");
15377 else if (clock_ctrl == 6)
15378 strcat(str, "100MHz");
Michael Chanf9804dd2005-09-27 12:13:10 -070015379 } else {
15380 strcpy(str, "PCI:");
Joe Perches63c3a662011-04-26 08:12:10 +000015381 if (tg3_flag(tp, PCI_HIGH_SPEED))
Michael Chanf9804dd2005-09-27 12:13:10 -070015382 strcat(str, "66MHz");
15383 else
15384 strcat(str, "33MHz");
15385 }
Joe Perches63c3a662011-04-26 08:12:10 +000015386 if (tg3_flag(tp, PCI_32BIT))
Michael Chanf9804dd2005-09-27 12:13:10 -070015387 strcat(str, ":32-bit");
15388 else
15389 strcat(str, ":64-bit");
15390 return str;
15391}
15392
David S. Miller15f98502005-05-18 22:49:26 -070015393static void __devinit tg3_init_coal(struct tg3 *tp)
15394{
15395 struct ethtool_coalesce *ec = &tp->coal;
15396
15397 memset(ec, 0, sizeof(*ec));
15398 ec->cmd = ETHTOOL_GCOALESCE;
15399 ec->rx_coalesce_usecs = LOW_RXCOL_TICKS;
15400 ec->tx_coalesce_usecs = LOW_TXCOL_TICKS;
15401 ec->rx_max_coalesced_frames = LOW_RXMAX_FRAMES;
15402 ec->tx_max_coalesced_frames = LOW_TXMAX_FRAMES;
15403 ec->rx_coalesce_usecs_irq = DEFAULT_RXCOAL_TICK_INT;
15404 ec->tx_coalesce_usecs_irq = DEFAULT_TXCOAL_TICK_INT;
15405 ec->rx_max_coalesced_frames_irq = DEFAULT_RXCOAL_MAXF_INT;
15406 ec->tx_max_coalesced_frames_irq = DEFAULT_TXCOAL_MAXF_INT;
15407 ec->stats_block_coalesce_usecs = DEFAULT_STAT_COAL_TICKS;
15408
15409 if (tp->coalesce_mode & (HOSTCC_MODE_CLRTICK_RXBD |
15410 HOSTCC_MODE_CLRTICK_TXBD)) {
15411 ec->rx_coalesce_usecs = LOW_RXCOL_TICKS_CLRTCKS;
15412 ec->rx_coalesce_usecs_irq = DEFAULT_RXCOAL_TICK_INT_CLRTCKS;
15413 ec->tx_coalesce_usecs = LOW_TXCOL_TICKS_CLRTCKS;
15414 ec->tx_coalesce_usecs_irq = DEFAULT_TXCOAL_TICK_INT_CLRTCKS;
15415 }
Michael Chand244c892005-07-05 14:42:33 -070015416
Joe Perches63c3a662011-04-26 08:12:10 +000015417 if (tg3_flag(tp, 5705_PLUS)) {
Michael Chand244c892005-07-05 14:42:33 -070015418 ec->rx_coalesce_usecs_irq = 0;
15419 ec->tx_coalesce_usecs_irq = 0;
15420 ec->stats_block_coalesce_usecs = 0;
15421 }
David S. Miller15f98502005-05-18 22:49:26 -070015422}
15423
Linus Torvalds1da177e2005-04-16 15:20:36 -070015424static int __devinit tg3_init_one(struct pci_dev *pdev,
15425 const struct pci_device_id *ent)
15426{
Linus Torvalds1da177e2005-04-16 15:20:36 -070015427 struct net_device *dev;
15428 struct tg3 *tp;
Matt Carlson646c9ed2009-09-01 12:58:41 +000015429 int i, err, pm_cap;
15430 u32 sndmbx, rcvmbx, intmbx;
Michael Chanf9804dd2005-09-27 12:13:10 -070015431 char str[40];
Michael Chan72f2afb2006-03-06 19:28:35 -080015432 u64 dma_mask, persist_dma_mask;
Michał Mirosławc8f44af2011-11-15 15:29:55 +000015433 netdev_features_t features = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015434
Joe Perches05dbe002010-02-17 19:44:19 +000015435 printk_once(KERN_INFO "%s\n", version);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015436
15437 err = pci_enable_device(pdev);
15438 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000015439 dev_err(&pdev->dev, "Cannot enable PCI device, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070015440 return err;
15441 }
15442
Linus Torvalds1da177e2005-04-16 15:20:36 -070015443 err = pci_request_regions(pdev, DRV_MODULE_NAME);
15444 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000015445 dev_err(&pdev->dev, "Cannot obtain PCI resources, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070015446 goto err_out_disable_pdev;
15447 }
15448
15449 pci_set_master(pdev);
15450
15451 /* Find power-management capability. */
15452 pm_cap = pci_find_capability(pdev, PCI_CAP_ID_PM);
15453 if (pm_cap == 0) {
Matt Carlson2445e462010-04-05 10:19:21 +000015454 dev_err(&pdev->dev,
15455 "Cannot find Power Management capability, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070015456 err = -EIO;
15457 goto err_out_free_res;
15458 }
15459
Matt Carlson16821282011-07-13 09:27:28 +000015460 err = pci_set_power_state(pdev, PCI_D0);
15461 if (err) {
15462 dev_err(&pdev->dev, "Transition to D0 failed, aborting\n");
15463 goto err_out_free_res;
15464 }
15465
Matt Carlsonfe5f5782009-09-01 13:09:39 +000015466 dev = alloc_etherdev_mq(sizeof(*tp), TG3_IRQ_MAX_VECS);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015467 if (!dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070015468 err = -ENOMEM;
Matt Carlson16821282011-07-13 09:27:28 +000015469 goto err_out_power_down;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015470 }
15471
Linus Torvalds1da177e2005-04-16 15:20:36 -070015472 SET_NETDEV_DEV(dev, &pdev->dev);
15473
Linus Torvalds1da177e2005-04-16 15:20:36 -070015474 tp = netdev_priv(dev);
15475 tp->pdev = pdev;
15476 tp->dev = dev;
15477 tp->pm_cap = pm_cap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015478 tp->rx_mode = TG3_DEF_RX_MODE;
15479 tp->tx_mode = TG3_DEF_TX_MODE;
Matt Carlson8ef21422008-05-02 16:47:53 -070015480
Linus Torvalds1da177e2005-04-16 15:20:36 -070015481 if (tg3_debug > 0)
15482 tp->msg_enable = tg3_debug;
15483 else
15484 tp->msg_enable = TG3_DEF_MSG_ENABLE;
15485
15486 /* The word/byte swap controls here control register access byte
15487 * swapping. DMA data byte swapping is controlled in the GRC_MODE
15488 * setting below.
15489 */
15490 tp->misc_host_ctrl =
15491 MISC_HOST_CTRL_MASK_PCI_INT |
15492 MISC_HOST_CTRL_WORD_SWAP |
15493 MISC_HOST_CTRL_INDIR_ACCESS |
15494 MISC_HOST_CTRL_PCISTATE_RW;
15495
15496 /* The NONFRM (non-frame) byte/word swap controls take effect
15497 * on descriptor entries, anything which isn't packet data.
15498 *
15499 * The StrongARM chips on the board (one for tx, one for rx)
15500 * are running in big-endian mode.
15501 */
15502 tp->grc_mode = (GRC_MODE_WSWAP_DATA | GRC_MODE_BSWAP_DATA |
15503 GRC_MODE_WSWAP_NONFRM_DATA);
15504#ifdef __BIG_ENDIAN
15505 tp->grc_mode |= GRC_MODE_BSWAP_NONFRM_DATA;
15506#endif
15507 spin_lock_init(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015508 spin_lock_init(&tp->indirect_lock);
David Howellsc4028952006-11-22 14:57:56 +000015509 INIT_WORK(&tp->reset_task, tg3_reset_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015510
Matt Carlsond5fe4882008-11-21 17:20:32 -080015511 tp->regs = pci_ioremap_bar(pdev, BAR_0);
Andy Gospodarekab0049b2007-09-06 20:42:14 +010015512 if (!tp->regs) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015513 dev_err(&pdev->dev, "Cannot map device registers, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070015514 err = -ENOMEM;
15515 goto err_out_free_dev;
15516 }
15517
Matt Carlsonc9cab242011-07-13 09:27:27 +000015518 if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761 ||
15519 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761E ||
15520 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761S ||
15521 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761SE ||
15522 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717 ||
15523 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718 ||
15524 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5719 ||
15525 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5720) {
15526 tg3_flag_set(tp, ENABLE_APE);
15527 tp->aperegs = pci_ioremap_bar(pdev, BAR_2);
15528 if (!tp->aperegs) {
15529 dev_err(&pdev->dev,
15530 "Cannot map APE registers, aborting\n");
15531 err = -ENOMEM;
15532 goto err_out_iounmap;
15533 }
15534 }
15535
Linus Torvalds1da177e2005-04-16 15:20:36 -070015536 tp->rx_pending = TG3_DEF_RX_RING_PENDING;
15537 tp->rx_jumbo_pending = TG3_DEF_RX_JUMBO_RING_PENDING;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015538
Linus Torvalds1da177e2005-04-16 15:20:36 -070015539 dev->ethtool_ops = &tg3_ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015540 dev->watchdog_timeo = TG3_TX_TIMEOUT;
Matt Carlson2ffcc982011-05-19 12:12:44 +000015541 dev->netdev_ops = &tg3_netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015542 dev->irq = pdev->irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015543
15544 err = tg3_get_invariants(tp);
15545 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015546 dev_err(&pdev->dev,
15547 "Problem fetching invariants of chip, aborting\n");
Matt Carlsonc9cab242011-07-13 09:27:27 +000015548 goto err_out_apeunmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015549 }
15550
Michael Chan4a29cc22006-03-19 13:21:12 -080015551 /* The EPB bridge inside 5714, 5715, and 5780 and any
15552 * device behind the EPB cannot support DMA addresses > 40-bit.
Michael Chan72f2afb2006-03-06 19:28:35 -080015553 * On 64-bit systems with IOMMU, use 40-bit dma_mask.
15554 * On 64-bit systems without IOMMU, use 64-bit dma_mask and
15555 * do DMA address check in tg3_start_xmit().
15556 */
Joe Perches63c3a662011-04-26 08:12:10 +000015557 if (tg3_flag(tp, IS_5788))
Yang Hongyang284901a2009-04-06 19:01:15 -070015558 persist_dma_mask = dma_mask = DMA_BIT_MASK(32);
Joe Perches63c3a662011-04-26 08:12:10 +000015559 else if (tg3_flag(tp, 40BIT_DMA_BUG)) {
Yang Hongyang50cf1562009-04-06 19:01:14 -070015560 persist_dma_mask = dma_mask = DMA_BIT_MASK(40);
Michael Chan72f2afb2006-03-06 19:28:35 -080015561#ifdef CONFIG_HIGHMEM
Yang Hongyang6a355282009-04-06 19:01:13 -070015562 dma_mask = DMA_BIT_MASK(64);
Michael Chan72f2afb2006-03-06 19:28:35 -080015563#endif
Michael Chan4a29cc22006-03-19 13:21:12 -080015564 } else
Yang Hongyang6a355282009-04-06 19:01:13 -070015565 persist_dma_mask = dma_mask = DMA_BIT_MASK(64);
Michael Chan72f2afb2006-03-06 19:28:35 -080015566
15567 /* Configure DMA attributes. */
Yang Hongyang284901a2009-04-06 19:01:15 -070015568 if (dma_mask > DMA_BIT_MASK(32)) {
Michael Chan72f2afb2006-03-06 19:28:35 -080015569 err = pci_set_dma_mask(pdev, dma_mask);
15570 if (!err) {
Matt Carlson0da06062011-05-19 12:12:53 +000015571 features |= NETIF_F_HIGHDMA;
Michael Chan72f2afb2006-03-06 19:28:35 -080015572 err = pci_set_consistent_dma_mask(pdev,
15573 persist_dma_mask);
15574 if (err < 0) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015575 dev_err(&pdev->dev, "Unable to obtain 64 bit "
15576 "DMA for consistent allocations\n");
Matt Carlsonc9cab242011-07-13 09:27:27 +000015577 goto err_out_apeunmap;
Michael Chan72f2afb2006-03-06 19:28:35 -080015578 }
15579 }
15580 }
Yang Hongyang284901a2009-04-06 19:01:15 -070015581 if (err || dma_mask == DMA_BIT_MASK(32)) {
15582 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
Michael Chan72f2afb2006-03-06 19:28:35 -080015583 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015584 dev_err(&pdev->dev,
15585 "No usable DMA configuration, aborting\n");
Matt Carlsonc9cab242011-07-13 09:27:27 +000015586 goto err_out_apeunmap;
Michael Chan72f2afb2006-03-06 19:28:35 -080015587 }
15588 }
15589
Michael Chanfdfec1722005-07-25 12:31:48 -070015590 tg3_init_bufmgr_config(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015591
Matt Carlson0da06062011-05-19 12:12:53 +000015592 features |= NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
15593
15594 /* 5700 B0 chips do not support checksumming correctly due
15595 * to hardware bugs.
15596 */
15597 if (tp->pci_chip_rev_id != CHIPREV_ID_5700_B0) {
15598 features |= NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_RXCSUM;
15599
15600 if (tg3_flag(tp, 5755_PLUS))
15601 features |= NETIF_F_IPV6_CSUM;
15602 }
15603
Michael Chan4e3a7aa2006-03-20 17:47:44 -080015604 /* TSO is on by default on chips that support hardware TSO.
15605 * Firmware TSO on older chips gives lower performance, so it
15606 * is off by default, but can be enabled using ethtool.
15607 */
Joe Perches63c3a662011-04-26 08:12:10 +000015608 if ((tg3_flag(tp, HW_TSO_1) ||
15609 tg3_flag(tp, HW_TSO_2) ||
15610 tg3_flag(tp, HW_TSO_3)) &&
Matt Carlson0da06062011-05-19 12:12:53 +000015611 (features & NETIF_F_IP_CSUM))
15612 features |= NETIF_F_TSO;
Joe Perches63c3a662011-04-26 08:12:10 +000015613 if (tg3_flag(tp, HW_TSO_2) || tg3_flag(tp, HW_TSO_3)) {
Matt Carlson0da06062011-05-19 12:12:53 +000015614 if (features & NETIF_F_IPV6_CSUM)
15615 features |= NETIF_F_TSO6;
Joe Perches63c3a662011-04-26 08:12:10 +000015616 if (tg3_flag(tp, HW_TSO_3) ||
Matt Carlsone849cdc2009-11-13 13:03:38 +000015617 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson57e69832008-05-25 23:48:31 -070015618 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
15619 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX) ||
Joe Perches63c3a662011-04-26 08:12:10 +000015620 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Michał Mirosławdc668912011-04-07 03:35:07 +000015621 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
Matt Carlson0da06062011-05-19 12:12:53 +000015622 features |= NETIF_F_TSO_ECN;
Michael Chanb0026622006-07-03 19:42:14 -070015623 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015624
Matt Carlsond542fe22011-05-19 16:02:43 +000015625 dev->features |= features;
15626 dev->vlan_features |= features;
15627
Mahesh Bandewar06c03c02011-05-08 06:51:48 +000015628 /*
15629 * Add loopback capability only for a subset of devices that support
15630 * MAC-LOOPBACK. Eventually this need to be enhanced to allow INT-PHY
15631 * loopback for the remaining devices.
15632 */
15633 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5780 &&
15634 !tg3_flag(tp, CPMU_PRESENT))
15635 /* Add the loopback capability */
Matt Carlson0da06062011-05-19 12:12:53 +000015636 features |= NETIF_F_LOOPBACK;
15637
Matt Carlson0da06062011-05-19 12:12:53 +000015638 dev->hw_features |= features;
Mahesh Bandewar06c03c02011-05-08 06:51:48 +000015639
Linus Torvalds1da177e2005-04-16 15:20:36 -070015640 if (tp->pci_chip_rev_id == CHIPREV_ID_5705_A1 &&
Joe Perches63c3a662011-04-26 08:12:10 +000015641 !tg3_flag(tp, TSO_CAPABLE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070015642 !(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH)) {
Joe Perches63c3a662011-04-26 08:12:10 +000015643 tg3_flag_set(tp, MAX_RXPEND_64);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015644 tp->rx_pending = 63;
15645 }
15646
Linus Torvalds1da177e2005-04-16 15:20:36 -070015647 err = tg3_get_device_address(tp);
15648 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015649 dev_err(&pdev->dev,
15650 "Could not obtain valid ethernet address, aborting\n");
Matt Carlsonc9cab242011-07-13 09:27:27 +000015651 goto err_out_apeunmap;
Matt Carlson0d3031d2007-10-10 18:02:43 -070015652 }
15653
Matt Carlsonc88864d2007-11-12 21:07:01 -080015654 /*
15655 * Reset chip in case UNDI or EFI driver did not shutdown
15656 * DMA self test will enable WDMAC and we'll see (spurious)
15657 * pending DMA on the PCI bus at that point.
15658 */
15659 if ((tr32(HOSTCC_MODE) & HOSTCC_MODE_ENABLE) ||
15660 (tr32(WDMAC_MODE) & WDMAC_MODE_ENABLE)) {
15661 tw32(MEMARB_MODE, MEMARB_MODE_ENABLE);
15662 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
15663 }
15664
15665 err = tg3_test_dma(tp);
15666 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015667 dev_err(&pdev->dev, "DMA engine test failed, aborting\n");
Matt Carlsonc88864d2007-11-12 21:07:01 -080015668 goto err_out_apeunmap;
15669 }
15670
Matt Carlson78f90dc2009-11-13 13:03:42 +000015671 intmbx = MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW;
15672 rcvmbx = MAILBOX_RCVRET_CON_IDX_0 + TG3_64BIT_REG_LOW;
15673 sndmbx = MAILBOX_SNDHOST_PROD_IDX_0 + TG3_64BIT_REG_LOW;
Matt Carlson6fd45cb2010-09-15 08:59:57 +000015674 for (i = 0; i < tp->irq_max; i++) {
Matt Carlson78f90dc2009-11-13 13:03:42 +000015675 struct tg3_napi *tnapi = &tp->napi[i];
15676
15677 tnapi->tp = tp;
15678 tnapi->tx_pending = TG3_DEF_TX_RING_PENDING;
15679
15680 tnapi->int_mbox = intmbx;
Matt Carlson93a700a2011-08-31 11:44:54 +000015681 if (i <= 4)
Matt Carlson78f90dc2009-11-13 13:03:42 +000015682 intmbx += 0x8;
15683 else
15684 intmbx += 0x4;
15685
15686 tnapi->consmbox = rcvmbx;
15687 tnapi->prodmbox = sndmbx;
15688
Matt Carlson66cfd1b2010-09-30 10:34:30 +000015689 if (i)
Matt Carlson78f90dc2009-11-13 13:03:42 +000015690 tnapi->coal_now = HOSTCC_MODE_COAL_VEC1_NOW << (i - 1);
Matt Carlson66cfd1b2010-09-30 10:34:30 +000015691 else
Matt Carlson78f90dc2009-11-13 13:03:42 +000015692 tnapi->coal_now = HOSTCC_MODE_NOW;
Matt Carlson78f90dc2009-11-13 13:03:42 +000015693
Joe Perches63c3a662011-04-26 08:12:10 +000015694 if (!tg3_flag(tp, SUPPORT_MSIX))
Matt Carlson78f90dc2009-11-13 13:03:42 +000015695 break;
15696
15697 /*
15698 * If we support MSIX, we'll be using RSS. If we're using
15699 * RSS, the first vector only handles link interrupts and the
15700 * remaining vectors handle rx and tx interrupts. Reuse the
15701 * mailbox values for the next iteration. The values we setup
15702 * above are still useful for the single vectored mode.
15703 */
15704 if (!i)
15705 continue;
15706
15707 rcvmbx += 0x8;
15708
15709 if (sndmbx & 0x4)
15710 sndmbx -= 0x4;
15711 else
15712 sndmbx += 0xc;
15713 }
15714
Matt Carlsonc88864d2007-11-12 21:07:01 -080015715 tg3_init_coal(tp);
15716
Michael Chanc49a1562006-12-17 17:07:29 -080015717 pci_set_drvdata(pdev, dev);
15718
Matt Carlsoncd0d7222011-07-13 09:27:33 +000015719 if (tg3_flag(tp, 5717_PLUS)) {
15720 /* Resume a low-power mode */
15721 tg3_frob_aux_power(tp, false);
15722 }
15723
Linus Torvalds1da177e2005-04-16 15:20:36 -070015724 err = register_netdev(dev);
15725 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015726 dev_err(&pdev->dev, "Cannot register net device, aborting\n");
Matt Carlson0d3031d2007-10-10 18:02:43 -070015727 goto err_out_apeunmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015728 }
15729
Joe Perches05dbe002010-02-17 19:44:19 +000015730 netdev_info(dev, "Tigon3 [partno(%s) rev %04x] (%s) MAC address %pM\n",
15731 tp->board_part_number,
15732 tp->pci_chip_rev_id,
15733 tg3_bus_string(tp, str),
15734 dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015735
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015736 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000015737 struct phy_device *phydev;
15738 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlson5129c3a2010-04-05 10:19:23 +000015739 netdev_info(dev,
15740 "attached PHY driver [%s] (mii_bus:phy_addr=%s)\n",
Joe Perches05dbe002010-02-17 19:44:19 +000015741 phydev->drv->name, dev_name(&phydev->dev));
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015742 } else {
15743 char *ethtype;
15744
15745 if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
15746 ethtype = "10/100Base-TX";
15747 else if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
15748 ethtype = "1000Base-SX";
15749 else
15750 ethtype = "10/100/1000Base-T";
15751
Matt Carlson5129c3a2010-04-05 10:19:23 +000015752 netdev_info(dev, "attached PHY is %s (%s Ethernet) "
Matt Carlson47007832011-04-20 07:57:43 +000015753 "(WireSpeed[%d], EEE[%d])\n",
15754 tg3_phy_string(tp), ethtype,
15755 (tp->phy_flags & TG3_PHYFLG_NO_ETH_WIRE_SPEED) == 0,
15756 (tp->phy_flags & TG3_PHYFLG_EEE_CAP) != 0);
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015757 }
Matt Carlsondf59c942008-11-03 16:52:56 -080015758
Joe Perches05dbe002010-02-17 19:44:19 +000015759 netdev_info(dev, "RXcsums[%d] LinkChgREG[%d] MIirq[%d] ASF[%d] TSOcap[%d]\n",
Michał Mirosławdc668912011-04-07 03:35:07 +000015760 (dev->features & NETIF_F_RXCSUM) != 0,
Joe Perches63c3a662011-04-26 08:12:10 +000015761 tg3_flag(tp, USE_LINKCHG_REG) != 0,
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015762 (tp->phy_flags & TG3_PHYFLG_USE_MI_INTERRUPT) != 0,
Joe Perches63c3a662011-04-26 08:12:10 +000015763 tg3_flag(tp, ENABLE_ASF) != 0,
15764 tg3_flag(tp, TSO_CAPABLE) != 0);
Joe Perches05dbe002010-02-17 19:44:19 +000015765 netdev_info(dev, "dma_rwctrl[%08x] dma_mask[%d-bit]\n",
15766 tp->dma_rwctrl,
15767 pdev->dma_mask == DMA_BIT_MASK(32) ? 32 :
15768 ((u64)pdev->dma_mask) == DMA_BIT_MASK(40) ? 40 : 64);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015769
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000015770 pci_save_state(pdev);
15771
Linus Torvalds1da177e2005-04-16 15:20:36 -070015772 return 0;
15773
Matt Carlson0d3031d2007-10-10 18:02:43 -070015774err_out_apeunmap:
15775 if (tp->aperegs) {
15776 iounmap(tp->aperegs);
15777 tp->aperegs = NULL;
15778 }
15779
Linus Torvalds1da177e2005-04-16 15:20:36 -070015780err_out_iounmap:
Michael Chan68929142005-08-09 20:17:14 -070015781 if (tp->regs) {
15782 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070015783 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070015784 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015785
15786err_out_free_dev:
15787 free_netdev(dev);
15788
Matt Carlson16821282011-07-13 09:27:28 +000015789err_out_power_down:
15790 pci_set_power_state(pdev, PCI_D3hot);
15791
Linus Torvalds1da177e2005-04-16 15:20:36 -070015792err_out_free_res:
15793 pci_release_regions(pdev);
15794
15795err_out_disable_pdev:
15796 pci_disable_device(pdev);
15797 pci_set_drvdata(pdev, NULL);
15798 return err;
15799}
15800
15801static void __devexit tg3_remove_one(struct pci_dev *pdev)
15802{
15803 struct net_device *dev = pci_get_drvdata(pdev);
15804
15805 if (dev) {
15806 struct tg3 *tp = netdev_priv(dev);
15807
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -080015808 if (tp->fw)
15809 release_firmware(tp->fw);
15810
Matt Carlsondb219972011-11-04 09:15:03 +000015811 tg3_reset_task_cancel(tp);
Matt Carlson158d7ab2008-05-29 01:37:54 -070015812
David S. Miller1805b2f2011-10-24 18:18:09 -040015813 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015814 tg3_phy_fini(tp);
Matt Carlson158d7ab2008-05-29 01:37:54 -070015815 tg3_mdio_fini(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015816 }
Matt Carlson158d7ab2008-05-29 01:37:54 -070015817
Linus Torvalds1da177e2005-04-16 15:20:36 -070015818 unregister_netdev(dev);
Matt Carlson0d3031d2007-10-10 18:02:43 -070015819 if (tp->aperegs) {
15820 iounmap(tp->aperegs);
15821 tp->aperegs = NULL;
15822 }
Michael Chan68929142005-08-09 20:17:14 -070015823 if (tp->regs) {
15824 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070015825 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070015826 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015827 free_netdev(dev);
15828 pci_release_regions(pdev);
15829 pci_disable_device(pdev);
15830 pci_set_drvdata(pdev, NULL);
15831 }
15832}
15833
Eric Dumazetaa6027c2011-01-01 05:22:46 +000015834#ifdef CONFIG_PM_SLEEP
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015835static int tg3_suspend(struct device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015836{
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015837 struct pci_dev *pdev = to_pci_dev(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015838 struct net_device *dev = pci_get_drvdata(pdev);
15839 struct tg3 *tp = netdev_priv(dev);
15840 int err;
15841
15842 if (!netif_running(dev))
15843 return 0;
15844
Matt Carlsondb219972011-11-04 09:15:03 +000015845 tg3_reset_task_cancel(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015846 tg3_phy_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015847 tg3_netif_stop(tp);
15848
15849 del_timer_sync(&tp->timer);
15850
David S. Millerf47c11e2005-06-24 20:18:35 -070015851 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015852 tg3_disable_ints(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -070015853 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015854
15855 netif_device_detach(dev);
15856
David S. Millerf47c11e2005-06-24 20:18:35 -070015857 tg3_full_lock(tp, 0);
Michael Chan944d9802005-05-29 14:57:48 -070015858 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Joe Perches63c3a662011-04-26 08:12:10 +000015859 tg3_flag_clear(tp, INIT_COMPLETE);
David S. Millerf47c11e2005-06-24 20:18:35 -070015860 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015861
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015862 err = tg3_power_down_prepare(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015863 if (err) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015864 int err2;
15865
David S. Millerf47c11e2005-06-24 20:18:35 -070015866 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015867
Joe Perches63c3a662011-04-26 08:12:10 +000015868 tg3_flag_set(tp, INIT_COMPLETE);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015869 err2 = tg3_restart_hw(tp, 1);
15870 if (err2)
Michael Chanb9ec6c12006-07-25 16:37:27 -070015871 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015872
15873 tp->timer.expires = jiffies + tp->timer_offset;
15874 add_timer(&tp->timer);
15875
15876 netif_device_attach(dev);
15877 tg3_netif_start(tp);
15878
Michael Chanb9ec6c12006-07-25 16:37:27 -070015879out:
David S. Millerf47c11e2005-06-24 20:18:35 -070015880 tg3_full_unlock(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015881
15882 if (!err2)
15883 tg3_phy_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015884 }
15885
15886 return err;
15887}
15888
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015889static int tg3_resume(struct device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015890{
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015891 struct pci_dev *pdev = to_pci_dev(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015892 struct net_device *dev = pci_get_drvdata(pdev);
15893 struct tg3 *tp = netdev_priv(dev);
15894 int err;
15895
15896 if (!netif_running(dev))
15897 return 0;
15898
Linus Torvalds1da177e2005-04-16 15:20:36 -070015899 netif_device_attach(dev);
15900
David S. Millerf47c11e2005-06-24 20:18:35 -070015901 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015902
Joe Perches63c3a662011-04-26 08:12:10 +000015903 tg3_flag_set(tp, INIT_COMPLETE);
Michael Chanb9ec6c12006-07-25 16:37:27 -070015904 err = tg3_restart_hw(tp, 1);
15905 if (err)
15906 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015907
15908 tp->timer.expires = jiffies + tp->timer_offset;
15909 add_timer(&tp->timer);
15910
Linus Torvalds1da177e2005-04-16 15:20:36 -070015911 tg3_netif_start(tp);
15912
Michael Chanb9ec6c12006-07-25 16:37:27 -070015913out:
David S. Millerf47c11e2005-06-24 20:18:35 -070015914 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015915
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015916 if (!err)
15917 tg3_phy_start(tp);
15918
Michael Chanb9ec6c12006-07-25 16:37:27 -070015919 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015920}
15921
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015922static SIMPLE_DEV_PM_OPS(tg3_pm_ops, tg3_suspend, tg3_resume);
Eric Dumazetaa6027c2011-01-01 05:22:46 +000015923#define TG3_PM_OPS (&tg3_pm_ops)
15924
15925#else
15926
15927#define TG3_PM_OPS NULL
15928
15929#endif /* CONFIG_PM_SLEEP */
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015930
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000015931/**
15932 * tg3_io_error_detected - called when PCI error is detected
15933 * @pdev: Pointer to PCI device
15934 * @state: The current pci connection state
15935 *
15936 * This function is called after a PCI bus error affecting
15937 * this device has been detected.
15938 */
15939static pci_ers_result_t tg3_io_error_detected(struct pci_dev *pdev,
15940 pci_channel_state_t state)
15941{
15942 struct net_device *netdev = pci_get_drvdata(pdev);
15943 struct tg3 *tp = netdev_priv(netdev);
15944 pci_ers_result_t err = PCI_ERS_RESULT_NEED_RESET;
15945
15946 netdev_info(netdev, "PCI I/O error detected\n");
15947
15948 rtnl_lock();
15949
15950 if (!netif_running(netdev))
15951 goto done;
15952
15953 tg3_phy_stop(tp);
15954
15955 tg3_netif_stop(tp);
15956
15957 del_timer_sync(&tp->timer);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000015958
15959 /* Want to make sure that the reset task doesn't run */
Matt Carlsondb219972011-11-04 09:15:03 +000015960 tg3_reset_task_cancel(tp);
Joe Perches63c3a662011-04-26 08:12:10 +000015961 tg3_flag_clear(tp, TX_RECOVERY_PENDING);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000015962
15963 netif_device_detach(netdev);
15964
15965 /* Clean up software state, even if MMIO is blocked */
15966 tg3_full_lock(tp, 0);
15967 tg3_halt(tp, RESET_KIND_SHUTDOWN, 0);
15968 tg3_full_unlock(tp);
15969
15970done:
15971 if (state == pci_channel_io_perm_failure)
15972 err = PCI_ERS_RESULT_DISCONNECT;
15973 else
15974 pci_disable_device(pdev);
15975
15976 rtnl_unlock();
15977
15978 return err;
15979}
15980
15981/**
15982 * tg3_io_slot_reset - called after the pci bus has been reset.
15983 * @pdev: Pointer to PCI device
15984 *
15985 * Restart the card from scratch, as if from a cold-boot.
15986 * At this point, the card has exprienced a hard reset,
15987 * followed by fixups by BIOS, and has its config space
15988 * set up identically to what it was at cold boot.
15989 */
15990static pci_ers_result_t tg3_io_slot_reset(struct pci_dev *pdev)
15991{
15992 struct net_device *netdev = pci_get_drvdata(pdev);
15993 struct tg3 *tp = netdev_priv(netdev);
15994 pci_ers_result_t rc = PCI_ERS_RESULT_DISCONNECT;
15995 int err;
15996
15997 rtnl_lock();
15998
15999 if (pci_enable_device(pdev)) {
16000 netdev_err(netdev, "Cannot re-enable PCI device after reset.\n");
16001 goto done;
16002 }
16003
16004 pci_set_master(pdev);
16005 pci_restore_state(pdev);
16006 pci_save_state(pdev);
16007
16008 if (!netif_running(netdev)) {
16009 rc = PCI_ERS_RESULT_RECOVERED;
16010 goto done;
16011 }
16012
16013 err = tg3_power_up(tp);
Matt Carlsonbed98292011-07-13 09:27:29 +000016014 if (err)
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000016015 goto done;
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000016016
16017 rc = PCI_ERS_RESULT_RECOVERED;
16018
16019done:
16020 rtnl_unlock();
16021
16022 return rc;
16023}
16024
16025/**
16026 * tg3_io_resume - called when traffic can start flowing again.
16027 * @pdev: Pointer to PCI device
16028 *
16029 * This callback is called when the error recovery driver tells
16030 * us that its OK to resume normal operation.
16031 */
16032static void tg3_io_resume(struct pci_dev *pdev)
16033{
16034 struct net_device *netdev = pci_get_drvdata(pdev);
16035 struct tg3 *tp = netdev_priv(netdev);
16036 int err;
16037
16038 rtnl_lock();
16039
16040 if (!netif_running(netdev))
16041 goto done;
16042
16043 tg3_full_lock(tp, 0);
Joe Perches63c3a662011-04-26 08:12:10 +000016044 tg3_flag_set(tp, INIT_COMPLETE);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000016045 err = tg3_restart_hw(tp, 1);
16046 tg3_full_unlock(tp);
16047 if (err) {
16048 netdev_err(netdev, "Cannot restart hardware after reset.\n");
16049 goto done;
16050 }
16051
16052 netif_device_attach(netdev);
16053
16054 tp->timer.expires = jiffies + tp->timer_offset;
16055 add_timer(&tp->timer);
16056
16057 tg3_netif_start(tp);
16058
16059 tg3_phy_start(tp);
16060
16061done:
16062 rtnl_unlock();
16063}
16064
16065static struct pci_error_handlers tg3_err_handler = {
16066 .error_detected = tg3_io_error_detected,
16067 .slot_reset = tg3_io_slot_reset,
16068 .resume = tg3_io_resume
16069};
16070
Linus Torvalds1da177e2005-04-16 15:20:36 -070016071static struct pci_driver tg3_driver = {
16072 .name = DRV_MODULE_NAME,
16073 .id_table = tg3_pci_tbl,
16074 .probe = tg3_init_one,
16075 .remove = __devexit_p(tg3_remove_one),
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000016076 .err_handler = &tg3_err_handler,
Eric Dumazetaa6027c2011-01-01 05:22:46 +000016077 .driver.pm = TG3_PM_OPS,
Linus Torvalds1da177e2005-04-16 15:20:36 -070016078};
16079
16080static int __init tg3_init(void)
16081{
Jeff Garzik29917622006-08-19 17:48:59 -040016082 return pci_register_driver(&tg3_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016083}
16084
16085static void __exit tg3_cleanup(void)
16086{
16087 pci_unregister_driver(&tg3_driver);
16088}
16089
16090module_init(tg3_init);
16091module_exit(tg3_cleanup);