blob: 39b92f5ed7dd618f0152b1e5df1dac0945712f8d [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 Carlson9e056c02012-02-13 15:20:17 +00007 * Copyright (C) 2005-2012 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
Javier Martinez Canillas27fd9de2011-03-26 16:42:31 +000051#include <linux/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#include <asm/byteorder.h>
Javier Martinez Canillas27fd9de2011-03-26 16:42:31 +000053#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
David S. Miller49b6e95f2007-03-29 01:38:42 -070055#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070056#include <asm/idprom.h>
David S. Miller49b6e95f2007-03-29 01:38:42 -070057#include <asm/prom.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070058#endif
59
Matt Carlson63532392008-11-03 16:49:57 -080060#define BAR_0 0
61#define BAR_2 2
62
Linus Torvalds1da177e2005-04-16 15:20:36 -070063#include "tg3.h"
64
Joe Perches63c3a662011-04-26 08:12:10 +000065/* Functions & macros to verify TG3_FLAGS types */
66
67static inline int _tg3_flag(enum TG3_FLAGS flag, unsigned long *bits)
68{
69 return test_bit(flag, bits);
70}
71
72static inline void _tg3_flag_set(enum TG3_FLAGS flag, unsigned long *bits)
73{
74 set_bit(flag, bits);
75}
76
77static inline void _tg3_flag_clear(enum TG3_FLAGS flag, unsigned long *bits)
78{
79 clear_bit(flag, bits);
80}
81
82#define tg3_flag(tp, flag) \
83 _tg3_flag(TG3_FLAG_##flag, (tp)->tg3_flags)
84#define tg3_flag_set(tp, flag) \
85 _tg3_flag_set(TG3_FLAG_##flag, (tp)->tg3_flags)
86#define tg3_flag_clear(tp, flag) \
87 _tg3_flag_clear(TG3_FLAG_##flag, (tp)->tg3_flags)
88
Linus Torvalds1da177e2005-04-16 15:20:36 -070089#define DRV_MODULE_NAME "tg3"
Matt Carlson6867c842010-07-11 09:31:44 +000090#define TG3_MAJ_NUM 3
Michael Chan7ae52892012-03-21 15:38:33 +000091#define TG3_MIN_NUM 123
Matt Carlson6867c842010-07-11 09:31:44 +000092#define DRV_MODULE_VERSION \
93 __stringify(TG3_MAJ_NUM) "." __stringify(TG3_MIN_NUM)
Michael Chan7ae52892012-03-21 15:38:33 +000094#define DRV_MODULE_RELDATE "March 21, 2012"
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
Matt Carlsonfd6d3f02011-08-31 11:44:52 +000096#define RESET_KIND_SHUTDOWN 0
97#define RESET_KIND_INIT 1
98#define RESET_KIND_SUSPEND 2
99
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100#define TG3_DEF_RX_MODE 0
101#define TG3_DEF_TX_MODE 0
102#define TG3_DEF_MSG_ENABLE \
103 (NETIF_MSG_DRV | \
104 NETIF_MSG_PROBE | \
105 NETIF_MSG_LINK | \
106 NETIF_MSG_TIMER | \
107 NETIF_MSG_IFDOWN | \
108 NETIF_MSG_IFUP | \
109 NETIF_MSG_RX_ERR | \
110 NETIF_MSG_TX_ERR)
111
Matt Carlson520b2752011-06-13 13:39:02 +0000112#define TG3_GRC_LCLCTL_PWRSW_DELAY 100
113
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114/* length of time before we decide the hardware is borked,
115 * and dev->tx_timeout() should be called to fix the problem
116 */
Joe Perches63c3a662011-04-26 08:12:10 +0000117
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118#define TG3_TX_TIMEOUT (5 * HZ)
119
120/* hardware minimum and maximum for a single frame's data payload */
121#define TG3_MIN_MTU 60
122#define TG3_MAX_MTU(tp) \
Joe Perches63c3a662011-04-26 08:12:10 +0000123 (tg3_flag(tp, JUMBO_CAPABLE) ? 9000 : 1500)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124
125/* These numbers seem to be hard coded in the NIC firmware somehow.
126 * You can't change the ring sizes, but you can change where you place
127 * them in the NIC onboard memory.
128 */
Matt Carlson7cb32cf2010-09-30 10:34:36 +0000129#define TG3_RX_STD_RING_SIZE(tp) \
Joe Perches63c3a662011-04-26 08:12:10 +0000130 (tg3_flag(tp, LRG_PROD_RING_CAP) ? \
Matt Carlsonde9f5232011-04-05 14:22:43 +0000131 TG3_RX_STD_MAX_SIZE_5717 : TG3_RX_STD_MAX_SIZE_5700)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132#define TG3_DEF_RX_RING_PENDING 200
Matt Carlson7cb32cf2010-09-30 10:34:36 +0000133#define TG3_RX_JMB_RING_SIZE(tp) \
Joe Perches63c3a662011-04-26 08:12:10 +0000134 (tg3_flag(tp, LRG_PROD_RING_CAP) ? \
Matt Carlsonde9f5232011-04-05 14:22:43 +0000135 TG3_RX_JMB_MAX_SIZE_5717 : TG3_RX_JMB_MAX_SIZE_5700)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136#define TG3_DEF_RX_JUMBO_RING_PENDING 100
137
138/* Do not place this n-ring entries value into the tp struct itself,
139 * we really want to expose these constants to GCC so that modulo et
140 * al. operations are done with shifts and masks instead of with
141 * hw multiply/modulo instructions. Another solution would be to
142 * replace things like '% foo' with '& (foo - 1)'.
143 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
145#define TG3_TX_RING_SIZE 512
146#define TG3_DEF_TX_RING_PENDING (TG3_TX_RING_SIZE - 1)
147
Matt Carlson2c49a442010-09-30 10:34:35 +0000148#define TG3_RX_STD_RING_BYTES(tp) \
149 (sizeof(struct tg3_rx_buffer_desc) * TG3_RX_STD_RING_SIZE(tp))
150#define TG3_RX_JMB_RING_BYTES(tp) \
151 (sizeof(struct tg3_ext_rx_buffer_desc) * TG3_RX_JMB_RING_SIZE(tp))
152#define TG3_RX_RCB_RING_BYTES(tp) \
Matt Carlson7cb32cf2010-09-30 10:34:36 +0000153 (sizeof(struct tg3_rx_buffer_desc) * (tp->rx_ret_ring_mask + 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154#define TG3_TX_RING_BYTES (sizeof(struct tg3_tx_buffer_desc) * \
155 TG3_TX_RING_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156#define NEXT_TX(N) (((N) + 1) & (TG3_TX_RING_SIZE - 1))
157
Matt Carlson287be122009-08-28 13:58:46 +0000158#define TG3_DMA_BYTE_ENAB 64
159
160#define TG3_RX_STD_DMA_SZ 1536
161#define TG3_RX_JMB_DMA_SZ 9046
162
163#define TG3_RX_DMA_TO_MAP_SZ(x) ((x) + TG3_DMA_BYTE_ENAB)
164
165#define TG3_RX_STD_MAP_SZ TG3_RX_DMA_TO_MAP_SZ(TG3_RX_STD_DMA_SZ)
166#define TG3_RX_JMB_MAP_SZ TG3_RX_DMA_TO_MAP_SZ(TG3_RX_JMB_DMA_SZ)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167
Matt Carlson2c49a442010-09-30 10:34:35 +0000168#define TG3_RX_STD_BUFF_RING_SIZE(tp) \
169 (sizeof(struct ring_info) * TG3_RX_STD_RING_SIZE(tp))
Matt Carlson2b2cdb62009-11-13 13:03:48 +0000170
Matt Carlson2c49a442010-09-30 10:34:35 +0000171#define TG3_RX_JMB_BUFF_RING_SIZE(tp) \
172 (sizeof(struct ring_info) * TG3_RX_JMB_RING_SIZE(tp))
Matt Carlson2b2cdb62009-11-13 13:03:48 +0000173
Matt Carlsond2757fc2010-04-12 06:58:27 +0000174/* Due to a hardware bug, the 5701 can only DMA to memory addresses
175 * that are at least dword aligned when used in PCIX mode. The driver
176 * works around this bug by double copying the packet. This workaround
177 * is built into the normal double copy length check for efficiency.
178 *
179 * However, the double copy is only necessary on those architectures
180 * where unaligned memory accesses are inefficient. For those architectures
181 * where unaligned memory accesses incur little penalty, we can reintegrate
182 * the 5701 in the normal rx path. Doing so saves a device structure
183 * dereference by hardcoding the double copy threshold in place.
184 */
185#define TG3_RX_COPY_THRESHOLD 256
186#if NET_IP_ALIGN == 0 || defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)
187 #define TG3_RX_COPY_THRESH(tp) TG3_RX_COPY_THRESHOLD
188#else
189 #define TG3_RX_COPY_THRESH(tp) ((tp)->rx_copy_thresh)
190#endif
191
Matt Carlson81389f52011-08-31 11:44:49 +0000192#if (NET_IP_ALIGN != 0)
193#define TG3_RX_OFFSET(tp) ((tp)->rx_offset)
194#else
Eric Dumazet9205fd92011-11-18 06:47:01 +0000195#define TG3_RX_OFFSET(tp) (NET_SKB_PAD)
Matt Carlson81389f52011-08-31 11:44:49 +0000196#endif
197
Eric Dumazet8d4057a2012-04-27 00:34:49 +0000198/* This driver uses the new build_skb() API providing a frag as skb->head
199 * This strategy permits better GRO aggregation, better TCP coalescing, and
200 * better splice() implementation (avoids a copy from head to a page), at
201 * minimal memory cost.
202 * In this 2048 bytes block, we have enough room to store the MTU=1500 frame
203 * and the struct skb_shared_info.
204 */
205#define TG3_FRAGSIZE 2048
206
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207/* minimum number of free TX descriptors required to wake up TX process */
Matt Carlsonf3f3f272009-08-28 14:03:21 +0000208#define TG3_TX_WAKEUP_THRESH(tnapi) ((tnapi)->tx_pending / 4)
Matt Carlson55086ad2011-12-14 11:09:59 +0000209#define TG3_TX_BD_DMA_MAX_2K 2048
Matt Carlsona4cb4282011-12-14 11:09:58 +0000210#define TG3_TX_BD_DMA_MAX_4K 4096
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
Matt Carlsonad829262008-11-21 17:16:16 -0800212#define TG3_RAW_IP_ALIGN 2
213
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000214#define TG3_FW_UPDATE_TIMEOUT_SEC 5
Matt Carlson21f76382012-02-22 12:35:21 +0000215#define TG3_FW_UPDATE_FREQ_SEC (TG3_FW_UPDATE_TIMEOUT_SEC / 2)
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000216
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -0800217#define FIRMWARE_TG3 "tigon/tg3.bin"
218#define FIRMWARE_TG3TSO "tigon/tg3_tso.bin"
219#define FIRMWARE_TG3TSO5 "tigon/tg3_tso5.bin"
220
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221static char version[] __devinitdata =
Joe Perches05dbe002010-02-17 19:44:19 +0000222 DRV_MODULE_NAME ".c:v" DRV_MODULE_VERSION " (" DRV_MODULE_RELDATE ")";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
224MODULE_AUTHOR("David S. Miller (davem@redhat.com) and Jeff Garzik (jgarzik@pobox.com)");
225MODULE_DESCRIPTION("Broadcom Tigon3 ethernet driver");
226MODULE_LICENSE("GPL");
227MODULE_VERSION(DRV_MODULE_VERSION);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -0800228MODULE_FIRMWARE(FIRMWARE_TG3);
229MODULE_FIRMWARE(FIRMWARE_TG3TSO);
230MODULE_FIRMWARE(FIRMWARE_TG3TSO5);
231
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232static int tg3_debug = -1; /* -1 == use TG3_DEF_MSG_ENABLE as value */
233module_param(tg3_debug, int, 0);
234MODULE_PARM_DESC(tg3_debug, "Tigon3 bitmapped debugging message enable value");
235
Alexey Dobriyana3aa1882010-01-07 11:58:11 +0000236static DEFINE_PCI_DEVICE_TABLE(tg3_pci_tbl) = {
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700237 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5700)},
238 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5701)},
239 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5702)},
240 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5703)},
241 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5704)},
242 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5702FE)},
243 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705)},
244 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705_2)},
245 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705M)},
246 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705M_2)},
247 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5702X)},
248 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5703X)},
249 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5704S)},
250 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5702A3)},
251 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5703A3)},
252 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5782)},
253 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5788)},
254 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5789)},
255 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5901)},
256 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5901_2)},
257 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5704S_2)},
258 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705F)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700259 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5721)},
Michael Chan126a3362006-09-27 16:03:07 -0700260 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5722)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700261 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5751)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700262 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5751M)},
263 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5751F)},
264 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5752)},
265 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5752M)},
266 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5753)},
267 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5753M)},
268 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5753F)},
269 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5754)},
270 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5754M)},
271 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5755)},
272 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5755M)},
Michael Chan126a3362006-09-27 16:03:07 -0700273 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5756)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700274 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5786)},
275 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5787)},
276 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5787M)},
Michael Chan676917d2006-12-07 00:20:22 -0800277 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5787F)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700278 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5714)},
279 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5714S)},
280 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5715)},
281 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5715S)},
282 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5780)},
283 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5780S)},
284 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5781)},
Michael Chanb5d37722006-09-27 16:06:21 -0700285 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5906)},
286 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5906M)},
Matt Carlsond30cdd22007-10-07 23:28:35 -0700287 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5784)},
288 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5764)},
Matt Carlson6c7af272007-10-21 16:12:02 -0700289 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5723)},
Matt Carlson9936bcf2007-10-10 18:03:07 -0700290 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5761)},
291 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5761E)},
Matt Carlsonc88e6682008-11-03 16:49:18 -0800292 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5761S)},
293 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5761SE)},
Matt Carlson2befdce2009-08-28 12:28:45 +0000294 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5785_G)},
295 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5785_F)},
Matt Carlson321d32a2008-11-21 17:22:19 -0800296 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57780)},
297 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57760)},
298 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57790)},
Matt Carlson5e7ccf22009-08-25 10:08:42 +0000299 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57788)},
Matt Carlson5001e2f2009-11-13 13:03:51 +0000300 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5717)},
301 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5718)},
Matt Carlsonb0f75222010-01-20 16:58:11 +0000302 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57781)},
303 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57785)},
304 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57761)},
305 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57765)},
306 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57791)},
307 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57795)},
Matt Carlson302b5002010-06-05 17:24:38 +0000308 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5719)},
Matt Carlsonba1f3c72011-04-05 14:22:50 +0000309 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5720)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700310 {PCI_DEVICE(PCI_VENDOR_ID_SYSKONNECT, PCI_DEVICE_ID_SYSKONNECT_9DXX)},
311 {PCI_DEVICE(PCI_VENDOR_ID_SYSKONNECT, PCI_DEVICE_ID_SYSKONNECT_9MXX)},
312 {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC1000)},
313 {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC1001)},
314 {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC1003)},
315 {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC9100)},
316 {PCI_DEVICE(PCI_VENDOR_ID_APPLE, PCI_DEVICE_ID_APPLE_TIGON3)},
Meelis Roos1dcb14d2011-05-25 05:43:47 +0000317 {PCI_DEVICE(0x10cf, 0x11a2)}, /* Fujitsu 1000base-SX with BCM5703SKHB */
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700318 {}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319};
320
321MODULE_DEVICE_TABLE(pci, tg3_pci_tbl);
322
Andreas Mohr50da8592006-08-14 23:54:30 -0700323static const struct {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 const char string[ETH_GSTRING_LEN];
Matt Carlson48fa55a2011-04-13 11:05:06 +0000325} ethtool_stats_keys[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 { "rx_octets" },
327 { "rx_fragments" },
328 { "rx_ucast_packets" },
329 { "rx_mcast_packets" },
330 { "rx_bcast_packets" },
331 { "rx_fcs_errors" },
332 { "rx_align_errors" },
333 { "rx_xon_pause_rcvd" },
334 { "rx_xoff_pause_rcvd" },
335 { "rx_mac_ctrl_rcvd" },
336 { "rx_xoff_entered" },
337 { "rx_frame_too_long_errors" },
338 { "rx_jabbers" },
339 { "rx_undersize_packets" },
340 { "rx_in_length_errors" },
341 { "rx_out_length_errors" },
342 { "rx_64_or_less_octet_packets" },
343 { "rx_65_to_127_octet_packets" },
344 { "rx_128_to_255_octet_packets" },
345 { "rx_256_to_511_octet_packets" },
346 { "rx_512_to_1023_octet_packets" },
347 { "rx_1024_to_1522_octet_packets" },
348 { "rx_1523_to_2047_octet_packets" },
349 { "rx_2048_to_4095_octet_packets" },
350 { "rx_4096_to_8191_octet_packets" },
351 { "rx_8192_to_9022_octet_packets" },
352
353 { "tx_octets" },
354 { "tx_collisions" },
355
356 { "tx_xon_sent" },
357 { "tx_xoff_sent" },
358 { "tx_flow_control" },
359 { "tx_mac_errors" },
360 { "tx_single_collisions" },
361 { "tx_mult_collisions" },
362 { "tx_deferred" },
363 { "tx_excessive_collisions" },
364 { "tx_late_collisions" },
365 { "tx_collide_2times" },
366 { "tx_collide_3times" },
367 { "tx_collide_4times" },
368 { "tx_collide_5times" },
369 { "tx_collide_6times" },
370 { "tx_collide_7times" },
371 { "tx_collide_8times" },
372 { "tx_collide_9times" },
373 { "tx_collide_10times" },
374 { "tx_collide_11times" },
375 { "tx_collide_12times" },
376 { "tx_collide_13times" },
377 { "tx_collide_14times" },
378 { "tx_collide_15times" },
379 { "tx_ucast_packets" },
380 { "tx_mcast_packets" },
381 { "tx_bcast_packets" },
382 { "tx_carrier_sense_errors" },
383 { "tx_discards" },
384 { "tx_errors" },
385
386 { "dma_writeq_full" },
387 { "dma_write_prioq_full" },
388 { "rxbds_empty" },
389 { "rx_discards" },
390 { "rx_errors" },
391 { "rx_threshold_hit" },
392
393 { "dma_readq_full" },
394 { "dma_read_prioq_full" },
395 { "tx_comp_queue_full" },
396
397 { "ring_set_send_prod_index" },
398 { "ring_status_update" },
399 { "nic_irqs" },
400 { "nic_avoided_irqs" },
Matt Carlson4452d092011-05-19 12:12:51 +0000401 { "nic_tx_threshold_hit" },
402
403 { "mbuf_lwm_thresh_hit" },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404};
405
Matt Carlson48fa55a2011-04-13 11:05:06 +0000406#define TG3_NUM_STATS ARRAY_SIZE(ethtool_stats_keys)
407
408
Andreas Mohr50da8592006-08-14 23:54:30 -0700409static const struct {
Michael Chan4cafd3f2005-05-29 14:56:34 -0700410 const char string[ETH_GSTRING_LEN];
Matt Carlson48fa55a2011-04-13 11:05:06 +0000411} ethtool_test_keys[] = {
Matt Carlson28a45952011-08-19 13:58:22 +0000412 { "nvram test (online) " },
413 { "link test (online) " },
414 { "register test (offline)" },
415 { "memory test (offline)" },
416 { "mac loopback test (offline)" },
417 { "phy loopback test (offline)" },
Matt Carlson941ec902011-08-19 13:58:23 +0000418 { "ext loopback test (offline)" },
Matt Carlson28a45952011-08-19 13:58:22 +0000419 { "interrupt test (offline)" },
Michael Chan4cafd3f2005-05-29 14:56:34 -0700420};
421
Matt Carlson48fa55a2011-04-13 11:05:06 +0000422#define TG3_NUM_TEST ARRAY_SIZE(ethtool_test_keys)
423
424
Michael Chanb401e9e2005-12-19 16:27:04 -0800425static void tg3_write32(struct tg3 *tp, u32 off, u32 val)
426{
427 writel(val, tp->regs + off);
428}
429
430static u32 tg3_read32(struct tg3 *tp, u32 off)
431{
Matt Carlsonde6f31e2010-04-12 06:58:30 +0000432 return readl(tp->regs + off);
Michael Chanb401e9e2005-12-19 16:27:04 -0800433}
434
Matt Carlson0d3031d2007-10-10 18:02:43 -0700435static void tg3_ape_write32(struct tg3 *tp, u32 off, u32 val)
436{
437 writel(val, tp->aperegs + off);
438}
439
440static u32 tg3_ape_read32(struct tg3 *tp, u32 off)
441{
Matt Carlsonde6f31e2010-04-12 06:58:30 +0000442 return readl(tp->aperegs + off);
Matt Carlson0d3031d2007-10-10 18:02:43 -0700443}
444
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445static void tg3_write_indirect_reg32(struct tg3 *tp, u32 off, u32 val)
446{
Michael Chan68929142005-08-09 20:17:14 -0700447 unsigned long flags;
448
449 spin_lock_irqsave(&tp->indirect_lock, flags);
Michael Chan1ee582d2005-08-09 20:16:46 -0700450 pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off);
451 pci_write_config_dword(tp->pdev, TG3PCI_REG_DATA, val);
Michael Chan68929142005-08-09 20:17:14 -0700452 spin_unlock_irqrestore(&tp->indirect_lock, flags);
Michael Chan1ee582d2005-08-09 20:16:46 -0700453}
454
455static void tg3_write_flush_reg32(struct tg3 *tp, u32 off, u32 val)
456{
457 writel(val, tp->regs + off);
458 readl(tp->regs + off);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459}
460
Michael Chan68929142005-08-09 20:17:14 -0700461static u32 tg3_read_indirect_reg32(struct tg3 *tp, u32 off)
462{
463 unsigned long flags;
464 u32 val;
465
466 spin_lock_irqsave(&tp->indirect_lock, flags);
467 pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off);
468 pci_read_config_dword(tp->pdev, TG3PCI_REG_DATA, &val);
469 spin_unlock_irqrestore(&tp->indirect_lock, flags);
470 return val;
471}
472
473static void tg3_write_indirect_mbox(struct tg3 *tp, u32 off, u32 val)
474{
475 unsigned long flags;
476
477 if (off == (MAILBOX_RCVRET_CON_IDX_0 + TG3_64BIT_REG_LOW)) {
478 pci_write_config_dword(tp->pdev, TG3PCI_RCV_RET_RING_CON_IDX +
479 TG3_64BIT_REG_LOW, val);
480 return;
481 }
Matt Carlson66711e62009-11-13 13:03:49 +0000482 if (off == TG3_RX_STD_PROD_IDX_REG) {
Michael Chan68929142005-08-09 20:17:14 -0700483 pci_write_config_dword(tp->pdev, TG3PCI_STD_RING_PROD_IDX +
484 TG3_64BIT_REG_LOW, val);
485 return;
486 }
487
488 spin_lock_irqsave(&tp->indirect_lock, flags);
489 pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off + 0x5600);
490 pci_write_config_dword(tp->pdev, TG3PCI_REG_DATA, val);
491 spin_unlock_irqrestore(&tp->indirect_lock, flags);
492
493 /* In indirect mode when disabling interrupts, we also need
494 * to clear the interrupt bit in the GRC local ctrl register.
495 */
496 if ((off == (MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW)) &&
497 (val == 0x1)) {
498 pci_write_config_dword(tp->pdev, TG3PCI_MISC_LOCAL_CTRL,
499 tp->grc_local_ctrl|GRC_LCLCTRL_CLEARINT);
500 }
501}
502
503static u32 tg3_read_indirect_mbox(struct tg3 *tp, u32 off)
504{
505 unsigned long flags;
506 u32 val;
507
508 spin_lock_irqsave(&tp->indirect_lock, flags);
509 pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off + 0x5600);
510 pci_read_config_dword(tp->pdev, TG3PCI_REG_DATA, &val);
511 spin_unlock_irqrestore(&tp->indirect_lock, flags);
512 return val;
513}
514
Michael Chanb401e9e2005-12-19 16:27:04 -0800515/* usec_wait specifies the wait time in usec when writing to certain registers
516 * where it is unsafe to read back the register without some delay.
517 * GRC_LOCAL_CTRL is one example if the GPIOs are toggled to switch power.
518 * TG3PCI_CLOCK_CTRL is another example if the clock frequencies are changed.
519 */
520static void _tw32_flush(struct tg3 *tp, u32 off, u32 val, u32 usec_wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521{
Joe Perches63c3a662011-04-26 08:12:10 +0000522 if (tg3_flag(tp, PCIX_TARGET_HWBUG) || tg3_flag(tp, ICH_WORKAROUND))
Michael Chanb401e9e2005-12-19 16:27:04 -0800523 /* Non-posted methods */
524 tp->write32(tp, off, val);
525 else {
526 /* Posted method */
527 tg3_write32(tp, off, val);
528 if (usec_wait)
529 udelay(usec_wait);
530 tp->read32(tp, off);
531 }
532 /* Wait again after the read for the posted method to guarantee that
533 * the wait time is met.
534 */
535 if (usec_wait)
536 udelay(usec_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537}
538
Michael Chan09ee9292005-08-09 20:17:00 -0700539static inline void tw32_mailbox_flush(struct tg3 *tp, u32 off, u32 val)
540{
541 tp->write32_mbox(tp, off, val);
Joe Perches63c3a662011-04-26 08:12:10 +0000542 if (!tg3_flag(tp, MBOX_WRITE_REORDER) && !tg3_flag(tp, ICH_WORKAROUND))
Michael Chan68929142005-08-09 20:17:14 -0700543 tp->read32_mbox(tp, off);
Michael Chan09ee9292005-08-09 20:17:00 -0700544}
545
Michael Chan20094932005-08-09 20:16:32 -0700546static void tg3_write32_tx_mbox(struct tg3 *tp, u32 off, u32 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547{
548 void __iomem *mbox = tp->regs + off;
549 writel(val, mbox);
Joe Perches63c3a662011-04-26 08:12:10 +0000550 if (tg3_flag(tp, TXD_MBOX_HWBUG))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 writel(val, mbox);
Joe Perches63c3a662011-04-26 08:12:10 +0000552 if (tg3_flag(tp, MBOX_WRITE_REORDER))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 readl(mbox);
554}
555
Michael Chanb5d37722006-09-27 16:06:21 -0700556static u32 tg3_read32_mbox_5906(struct tg3 *tp, u32 off)
557{
Matt Carlsonde6f31e2010-04-12 06:58:30 +0000558 return readl(tp->regs + off + GRCMBOX_BASE);
Michael Chanb5d37722006-09-27 16:06:21 -0700559}
560
561static void tg3_write32_mbox_5906(struct tg3 *tp, u32 off, u32 val)
562{
563 writel(val, tp->regs + off + GRCMBOX_BASE);
564}
565
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000566#define tw32_mailbox(reg, val) tp->write32_mbox(tp, reg, val)
Michael Chan09ee9292005-08-09 20:17:00 -0700567#define tw32_mailbox_f(reg, val) tw32_mailbox_flush(tp, (reg), (val))
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000568#define tw32_rx_mbox(reg, val) tp->write32_rx_mbox(tp, reg, val)
569#define tw32_tx_mbox(reg, val) tp->write32_tx_mbox(tp, reg, val)
570#define tr32_mailbox(reg) tp->read32_mbox(tp, reg)
Michael Chan20094932005-08-09 20:16:32 -0700571
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000572#define tw32(reg, val) tp->write32(tp, reg, val)
573#define tw32_f(reg, val) _tw32_flush(tp, (reg), (val), 0)
574#define tw32_wait_f(reg, val, us) _tw32_flush(tp, (reg), (val), (us))
575#define tr32(reg) tp->read32(tp, reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576
577static void tg3_write_mem(struct tg3 *tp, u32 off, u32 val)
578{
Michael Chan68929142005-08-09 20:17:14 -0700579 unsigned long flags;
580
Matt Carlson6ff6f812011-05-19 12:12:54 +0000581 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906 &&
Michael Chanb5d37722006-09-27 16:06:21 -0700582 (off >= NIC_SRAM_STATS_BLK) && (off < NIC_SRAM_TX_BUFFER_DESC))
583 return;
584
Michael Chan68929142005-08-09 20:17:14 -0700585 spin_lock_irqsave(&tp->indirect_lock, flags);
Joe Perches63c3a662011-04-26 08:12:10 +0000586 if (tg3_flag(tp, SRAM_USE_CONFIG)) {
Michael Chanbbadf502006-04-06 21:46:34 -0700587 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, off);
588 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_DATA, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589
Michael Chanbbadf502006-04-06 21:46:34 -0700590 /* Always leave this as zero. */
591 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, 0);
592 } else {
593 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, off);
594 tw32_f(TG3PCI_MEM_WIN_DATA, val);
595
596 /* Always leave this as zero. */
597 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, 0);
598 }
Michael Chan68929142005-08-09 20:17:14 -0700599 spin_unlock_irqrestore(&tp->indirect_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600}
601
602static void tg3_read_mem(struct tg3 *tp, u32 off, u32 *val)
603{
Michael Chan68929142005-08-09 20:17:14 -0700604 unsigned long flags;
605
Matt Carlson6ff6f812011-05-19 12:12:54 +0000606 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906 &&
Michael Chanb5d37722006-09-27 16:06:21 -0700607 (off >= NIC_SRAM_STATS_BLK) && (off < NIC_SRAM_TX_BUFFER_DESC)) {
608 *val = 0;
609 return;
610 }
611
Michael Chan68929142005-08-09 20:17:14 -0700612 spin_lock_irqsave(&tp->indirect_lock, flags);
Joe Perches63c3a662011-04-26 08:12:10 +0000613 if (tg3_flag(tp, SRAM_USE_CONFIG)) {
Michael Chanbbadf502006-04-06 21:46:34 -0700614 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, off);
615 pci_read_config_dword(tp->pdev, TG3PCI_MEM_WIN_DATA, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616
Michael Chanbbadf502006-04-06 21:46:34 -0700617 /* Always leave this as zero. */
618 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, 0);
619 } else {
620 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, off);
621 *val = tr32(TG3PCI_MEM_WIN_DATA);
622
623 /* Always leave this as zero. */
624 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, 0);
625 }
Michael Chan68929142005-08-09 20:17:14 -0700626 spin_unlock_irqrestore(&tp->indirect_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627}
628
Matt Carlson0d3031d2007-10-10 18:02:43 -0700629static void tg3_ape_lock_init(struct tg3 *tp)
630{
631 int i;
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000632 u32 regbase, bit;
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000633
634 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
635 regbase = TG3_APE_LOCK_GRANT;
636 else
637 regbase = TG3_APE_PER_LOCK_GRANT;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700638
639 /* Make sure the driver hasn't any stale locks. */
Matt Carlson78f94dc2011-11-04 09:14:58 +0000640 for (i = TG3_APE_LOCK_PHY0; i <= TG3_APE_LOCK_GPIO; i++) {
641 switch (i) {
642 case TG3_APE_LOCK_PHY0:
643 case TG3_APE_LOCK_PHY1:
644 case TG3_APE_LOCK_PHY2:
645 case TG3_APE_LOCK_PHY3:
646 bit = APE_LOCK_GRANT_DRIVER;
647 break;
648 default:
649 if (!tp->pci_fn)
650 bit = APE_LOCK_GRANT_DRIVER;
651 else
652 bit = 1 << tp->pci_fn;
653 }
654 tg3_ape_write32(tp, regbase + 4 * i, bit);
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000655 }
656
Matt Carlson0d3031d2007-10-10 18:02:43 -0700657}
658
659static int tg3_ape_lock(struct tg3 *tp, int locknum)
660{
661 int i, off;
662 int ret = 0;
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000663 u32 status, req, gnt, bit;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700664
Joe Perches63c3a662011-04-26 08:12:10 +0000665 if (!tg3_flag(tp, ENABLE_APE))
Matt Carlson0d3031d2007-10-10 18:02:43 -0700666 return 0;
667
668 switch (locknum) {
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000669 case TG3_APE_LOCK_GPIO:
670 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
671 return 0;
Matt Carlson33f401a2010-04-05 10:19:27 +0000672 case TG3_APE_LOCK_GRC:
673 case TG3_APE_LOCK_MEM:
Matt Carlson78f94dc2011-11-04 09:14:58 +0000674 if (!tp->pci_fn)
675 bit = APE_LOCK_REQ_DRIVER;
676 else
677 bit = 1 << tp->pci_fn;
Matt Carlson33f401a2010-04-05 10:19:27 +0000678 break;
679 default:
680 return -EINVAL;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700681 }
682
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000683 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761) {
684 req = TG3_APE_LOCK_REQ;
685 gnt = TG3_APE_LOCK_GRANT;
686 } else {
687 req = TG3_APE_PER_LOCK_REQ;
688 gnt = TG3_APE_PER_LOCK_GRANT;
689 }
690
Matt Carlson0d3031d2007-10-10 18:02:43 -0700691 off = 4 * locknum;
692
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000693 tg3_ape_write32(tp, req + off, bit);
Matt Carlson0d3031d2007-10-10 18:02:43 -0700694
695 /* Wait for up to 1 millisecond to acquire lock. */
696 for (i = 0; i < 100; i++) {
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000697 status = tg3_ape_read32(tp, gnt + off);
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000698 if (status == bit)
Matt Carlson0d3031d2007-10-10 18:02:43 -0700699 break;
700 udelay(10);
701 }
702
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000703 if (status != bit) {
Matt Carlson0d3031d2007-10-10 18:02:43 -0700704 /* Revoke the lock request. */
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000705 tg3_ape_write32(tp, gnt + off, bit);
Matt Carlson0d3031d2007-10-10 18:02:43 -0700706 ret = -EBUSY;
707 }
708
709 return ret;
710}
711
712static void tg3_ape_unlock(struct tg3 *tp, int locknum)
713{
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000714 u32 gnt, bit;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700715
Joe Perches63c3a662011-04-26 08:12:10 +0000716 if (!tg3_flag(tp, ENABLE_APE))
Matt Carlson0d3031d2007-10-10 18:02:43 -0700717 return;
718
719 switch (locknum) {
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000720 case TG3_APE_LOCK_GPIO:
721 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
722 return;
Matt Carlson33f401a2010-04-05 10:19:27 +0000723 case TG3_APE_LOCK_GRC:
724 case TG3_APE_LOCK_MEM:
Matt Carlson78f94dc2011-11-04 09:14:58 +0000725 if (!tp->pci_fn)
726 bit = APE_LOCK_GRANT_DRIVER;
727 else
728 bit = 1 << tp->pci_fn;
Matt Carlson33f401a2010-04-05 10:19:27 +0000729 break;
730 default:
731 return;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700732 }
733
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000734 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
735 gnt = TG3_APE_LOCK_GRANT;
736 else
737 gnt = TG3_APE_PER_LOCK_GRANT;
738
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000739 tg3_ape_write32(tp, gnt + 4 * locknum, bit);
Matt Carlson0d3031d2007-10-10 18:02:43 -0700740}
741
Matt Carlsonfd6d3f02011-08-31 11:44:52 +0000742static void tg3_ape_send_event(struct tg3 *tp, u32 event)
743{
744 int i;
745 u32 apedata;
746
747 /* NCSI does not support APE events */
748 if (tg3_flag(tp, APE_HAS_NCSI))
749 return;
750
751 apedata = tg3_ape_read32(tp, TG3_APE_SEG_SIG);
752 if (apedata != APE_SEG_SIG_MAGIC)
753 return;
754
755 apedata = tg3_ape_read32(tp, TG3_APE_FW_STATUS);
756 if (!(apedata & APE_FW_STATUS_READY))
757 return;
758
759 /* Wait for up to 1 millisecond for APE to service previous event. */
760 for (i = 0; i < 10; i++) {
761 if (tg3_ape_lock(tp, TG3_APE_LOCK_MEM))
762 return;
763
764 apedata = tg3_ape_read32(tp, TG3_APE_EVENT_STATUS);
765
766 if (!(apedata & APE_EVENT_STATUS_EVENT_PENDING))
767 tg3_ape_write32(tp, TG3_APE_EVENT_STATUS,
768 event | APE_EVENT_STATUS_EVENT_PENDING);
769
770 tg3_ape_unlock(tp, TG3_APE_LOCK_MEM);
771
772 if (!(apedata & APE_EVENT_STATUS_EVENT_PENDING))
773 break;
774
775 udelay(100);
776 }
777
778 if (!(apedata & APE_EVENT_STATUS_EVENT_PENDING))
779 tg3_ape_write32(tp, TG3_APE_EVENT, APE_EVENT_1);
780}
781
782static void tg3_ape_driver_state_change(struct tg3 *tp, int kind)
783{
784 u32 event;
785 u32 apedata;
786
787 if (!tg3_flag(tp, ENABLE_APE))
788 return;
789
790 switch (kind) {
791 case RESET_KIND_INIT:
792 tg3_ape_write32(tp, TG3_APE_HOST_SEG_SIG,
793 APE_HOST_SEG_SIG_MAGIC);
794 tg3_ape_write32(tp, TG3_APE_HOST_SEG_LEN,
795 APE_HOST_SEG_LEN_MAGIC);
796 apedata = tg3_ape_read32(tp, TG3_APE_HOST_INIT_COUNT);
797 tg3_ape_write32(tp, TG3_APE_HOST_INIT_COUNT, ++apedata);
798 tg3_ape_write32(tp, TG3_APE_HOST_DRIVER_ID,
799 APE_HOST_DRIVER_ID_MAGIC(TG3_MAJ_NUM, TG3_MIN_NUM));
800 tg3_ape_write32(tp, TG3_APE_HOST_BEHAVIOR,
801 APE_HOST_BEHAV_NO_PHYLOCK);
802 tg3_ape_write32(tp, TG3_APE_HOST_DRVR_STATE,
803 TG3_APE_HOST_DRVR_STATE_START);
804
805 event = APE_EVENT_STATUS_STATE_START;
806 break;
807 case RESET_KIND_SHUTDOWN:
808 /* With the interface we are currently using,
809 * APE does not track driver state. Wiping
810 * out the HOST SEGMENT SIGNATURE forces
811 * the APE to assume OS absent status.
812 */
813 tg3_ape_write32(tp, TG3_APE_HOST_SEG_SIG, 0x0);
814
815 if (device_may_wakeup(&tp->pdev->dev) &&
816 tg3_flag(tp, WOL_ENABLE)) {
817 tg3_ape_write32(tp, TG3_APE_HOST_WOL_SPEED,
818 TG3_APE_HOST_WOL_SPEED_AUTO);
819 apedata = TG3_APE_HOST_DRVR_STATE_WOL;
820 } else
821 apedata = TG3_APE_HOST_DRVR_STATE_UNLOAD;
822
823 tg3_ape_write32(tp, TG3_APE_HOST_DRVR_STATE, apedata);
824
825 event = APE_EVENT_STATUS_STATE_UNLOAD;
826 break;
827 case RESET_KIND_SUSPEND:
828 event = APE_EVENT_STATUS_STATE_SUSPEND;
829 break;
830 default:
831 return;
832 }
833
834 event |= APE_EVENT_STATUS_DRIVER_EVNT | APE_EVENT_STATUS_STATE_CHNGE;
835
836 tg3_ape_send_event(tp, event);
837}
838
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839static void tg3_disable_ints(struct tg3 *tp)
840{
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000841 int i;
842
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 tw32(TG3PCI_MISC_HOST_CTRL,
844 (tp->misc_host_ctrl | MISC_HOST_CTRL_MASK_PCI_INT));
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000845 for (i = 0; i < tp->irq_max; i++)
846 tw32_mailbox_f(tp->napi[i].int_mbox, 0x00000001);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847}
848
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849static void tg3_enable_ints(struct tg3 *tp)
850{
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000851 int i;
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000852
Michael Chanbbe832c2005-06-24 20:20:04 -0700853 tp->irq_sync = 0;
854 wmb();
855
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 tw32(TG3PCI_MISC_HOST_CTRL,
857 (tp->misc_host_ctrl & ~MISC_HOST_CTRL_MASK_PCI_INT));
Matt Carlsonf19af9c2009-09-01 12:47:49 +0000858
Matt Carlsonf89f38b2010-02-12 14:47:07 +0000859 tp->coal_now = tp->coalesce_mode | HOSTCC_MODE_ENABLE;
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000860 for (i = 0; i < tp->irq_cnt; i++) {
861 struct tg3_napi *tnapi = &tp->napi[i];
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000862
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000863 tw32_mailbox_f(tnapi->int_mbox, tnapi->last_tag << 24);
Joe Perches63c3a662011-04-26 08:12:10 +0000864 if (tg3_flag(tp, 1SHOT_MSI))
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000865 tw32_mailbox_f(tnapi->int_mbox, tnapi->last_tag << 24);
866
Matt Carlsonf89f38b2010-02-12 14:47:07 +0000867 tp->coal_now |= tnapi->coal_now;
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000868 }
Matt Carlsonf19af9c2009-09-01 12:47:49 +0000869
870 /* Force an initial interrupt */
Joe Perches63c3a662011-04-26 08:12:10 +0000871 if (!tg3_flag(tp, TAGGED_STATUS) &&
Matt Carlsonf19af9c2009-09-01 12:47:49 +0000872 (tp->napi[0].hw_status->status & SD_STATUS_UPDATED))
873 tw32(GRC_LOCAL_CTRL, tp->grc_local_ctrl | GRC_LCLCTRL_SETINT);
874 else
Matt Carlsonf89f38b2010-02-12 14:47:07 +0000875 tw32(HOSTCC_MODE, tp->coal_now);
876
877 tp->coal_now &= ~(tp->napi[0].coal_now | tp->napi[1].coal_now);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878}
879
Matt Carlson17375d22009-08-28 14:02:18 +0000880static inline unsigned int tg3_has_work(struct tg3_napi *tnapi)
Michael Chan04237dd2005-04-25 15:17:17 -0700881{
Matt Carlson17375d22009-08-28 14:02:18 +0000882 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +0000883 struct tg3_hw_status *sblk = tnapi->hw_status;
Michael Chan04237dd2005-04-25 15:17:17 -0700884 unsigned int work_exists = 0;
885
886 /* check for phy events */
Joe Perches63c3a662011-04-26 08:12:10 +0000887 if (!(tg3_flag(tp, USE_LINKCHG_REG) || tg3_flag(tp, POLL_SERDES))) {
Michael Chan04237dd2005-04-25 15:17:17 -0700888 if (sblk->status & SD_STATUS_LINK_CHG)
889 work_exists = 1;
890 }
Matt Carlsonf891ea12012-04-24 13:37:01 +0000891
892 /* check for TX work to do */
893 if (sblk->idx[0].tx_consumer != tnapi->tx_cons)
894 work_exists = 1;
895
896 /* check for RX work to do */
897 if (tnapi->rx_rcb_prod_idx &&
Matt Carlson8d9d7cf2009-09-01 13:19:05 +0000898 *(tnapi->rx_rcb_prod_idx) != tnapi->rx_rcb_ptr)
Michael Chan04237dd2005-04-25 15:17:17 -0700899 work_exists = 1;
900
901 return work_exists;
902}
903
Matt Carlson17375d22009-08-28 14:02:18 +0000904/* tg3_int_reenable
Michael Chan04237dd2005-04-25 15:17:17 -0700905 * similar to tg3_enable_ints, but it accurately determines whether there
906 * is new work pending and can return without flushing the PIO write
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400907 * which reenables interrupts
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 */
Matt Carlson17375d22009-08-28 14:02:18 +0000909static void tg3_int_reenable(struct tg3_napi *tnapi)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910{
Matt Carlson17375d22009-08-28 14:02:18 +0000911 struct tg3 *tp = tnapi->tp;
912
Matt Carlson898a56f2009-08-28 14:02:40 +0000913 tw32_mailbox(tnapi->int_mbox, tnapi->last_tag << 24);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 mmiowb();
915
David S. Millerfac9b832005-05-18 22:46:34 -0700916 /* When doing tagged status, this work check is unnecessary.
917 * The last_tag we write above tells the chip which piece of
918 * work we've completed.
919 */
Joe Perches63c3a662011-04-26 08:12:10 +0000920 if (!tg3_flag(tp, TAGGED_STATUS) && tg3_has_work(tnapi))
Michael Chan04237dd2005-04-25 15:17:17 -0700921 tw32(HOSTCC_MODE, tp->coalesce_mode |
Matt Carlsonfd2ce372009-09-01 12:51:13 +0000922 HOSTCC_MODE_ENABLE | tnapi->coal_now);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923}
924
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925static void tg3_switch_clocks(struct tg3 *tp)
926{
Matt Carlsonf6eb9b12009-09-01 13:19:53 +0000927 u32 clock_ctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 u32 orig_clock_ctrl;
929
Joe Perches63c3a662011-04-26 08:12:10 +0000930 if (tg3_flag(tp, CPMU_PRESENT) || tg3_flag(tp, 5780_CLASS))
Michael Chan4cf78e42005-07-25 12:29:19 -0700931 return;
932
Matt Carlsonf6eb9b12009-09-01 13:19:53 +0000933 clock_ctrl = tr32(TG3PCI_CLOCK_CTRL);
934
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 orig_clock_ctrl = clock_ctrl;
936 clock_ctrl &= (CLOCK_CTRL_FORCE_CLKRUN |
937 CLOCK_CTRL_CLKRUN_OENABLE |
938 0x1f);
939 tp->pci_clock_ctrl = clock_ctrl;
940
Joe Perches63c3a662011-04-26 08:12:10 +0000941 if (tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 if (orig_clock_ctrl & CLOCK_CTRL_625_CORE) {
Michael Chanb401e9e2005-12-19 16:27:04 -0800943 tw32_wait_f(TG3PCI_CLOCK_CTRL,
944 clock_ctrl | CLOCK_CTRL_625_CORE, 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 }
946 } else if ((orig_clock_ctrl & CLOCK_CTRL_44MHZ_CORE) != 0) {
Michael Chanb401e9e2005-12-19 16:27:04 -0800947 tw32_wait_f(TG3PCI_CLOCK_CTRL,
948 clock_ctrl |
949 (CLOCK_CTRL_44MHZ_CORE | CLOCK_CTRL_ALTCLK),
950 40);
951 tw32_wait_f(TG3PCI_CLOCK_CTRL,
952 clock_ctrl | (CLOCK_CTRL_ALTCLK),
953 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 }
Michael Chanb401e9e2005-12-19 16:27:04 -0800955 tw32_wait_f(TG3PCI_CLOCK_CTRL, clock_ctrl, 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956}
957
958#define PHY_BUSY_LOOPS 5000
959
960static int tg3_readphy(struct tg3 *tp, int reg, u32 *val)
961{
962 u32 frame_val;
963 unsigned int loops;
964 int ret;
965
966 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
967 tw32_f(MAC_MI_MODE,
968 (tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL));
969 udelay(80);
970 }
971
972 *val = 0x0;
973
Matt Carlson882e9792009-09-01 13:21:36 +0000974 frame_val = ((tp->phy_addr << MI_COM_PHY_ADDR_SHIFT) &
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 MI_COM_PHY_ADDR_MASK);
976 frame_val |= ((reg << MI_COM_REG_ADDR_SHIFT) &
977 MI_COM_REG_ADDR_MASK);
978 frame_val |= (MI_COM_CMD_READ | MI_COM_START);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400979
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 tw32_f(MAC_MI_COM, frame_val);
981
982 loops = PHY_BUSY_LOOPS;
983 while (loops != 0) {
984 udelay(10);
985 frame_val = tr32(MAC_MI_COM);
986
987 if ((frame_val & MI_COM_BUSY) == 0) {
988 udelay(5);
989 frame_val = tr32(MAC_MI_COM);
990 break;
991 }
992 loops -= 1;
993 }
994
995 ret = -EBUSY;
996 if (loops != 0) {
997 *val = frame_val & MI_COM_DATA_MASK;
998 ret = 0;
999 }
1000
1001 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
1002 tw32_f(MAC_MI_MODE, tp->mi_mode);
1003 udelay(80);
1004 }
1005
1006 return ret;
1007}
1008
1009static int tg3_writephy(struct tg3 *tp, int reg, u32 val)
1010{
1011 u32 frame_val;
1012 unsigned int loops;
1013 int ret;
1014
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001015 if ((tp->phy_flags & TG3_PHYFLG_IS_FET) &&
Matt Carlson221c5632011-06-13 13:39:01 +00001016 (reg == MII_CTRL1000 || reg == MII_TG3_AUX_CTRL))
Michael Chanb5d37722006-09-27 16:06:21 -07001017 return 0;
1018
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
1020 tw32_f(MAC_MI_MODE,
1021 (tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL));
1022 udelay(80);
1023 }
1024
Matt Carlson882e9792009-09-01 13:21:36 +00001025 frame_val = ((tp->phy_addr << MI_COM_PHY_ADDR_SHIFT) &
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 MI_COM_PHY_ADDR_MASK);
1027 frame_val |= ((reg << MI_COM_REG_ADDR_SHIFT) &
1028 MI_COM_REG_ADDR_MASK);
1029 frame_val |= (val & MI_COM_DATA_MASK);
1030 frame_val |= (MI_COM_CMD_WRITE | MI_COM_START);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001031
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 tw32_f(MAC_MI_COM, frame_val);
1033
1034 loops = PHY_BUSY_LOOPS;
1035 while (loops != 0) {
1036 udelay(10);
1037 frame_val = tr32(MAC_MI_COM);
1038 if ((frame_val & MI_COM_BUSY) == 0) {
1039 udelay(5);
1040 frame_val = tr32(MAC_MI_COM);
1041 break;
1042 }
1043 loops -= 1;
1044 }
1045
1046 ret = -EBUSY;
1047 if (loops != 0)
1048 ret = 0;
1049
1050 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
1051 tw32_f(MAC_MI_MODE, tp->mi_mode);
1052 udelay(80);
1053 }
1054
1055 return ret;
1056}
1057
Matt Carlsonb0988c12011-04-20 07:57:39 +00001058static int tg3_phy_cl45_write(struct tg3 *tp, u32 devad, u32 addr, u32 val)
1059{
1060 int err;
1061
1062 err = tg3_writephy(tp, MII_TG3_MMD_CTRL, devad);
1063 if (err)
1064 goto done;
1065
1066 err = tg3_writephy(tp, MII_TG3_MMD_ADDRESS, addr);
1067 if (err)
1068 goto done;
1069
1070 err = tg3_writephy(tp, MII_TG3_MMD_CTRL,
1071 MII_TG3_MMD_CTRL_DATA_NOINC | devad);
1072 if (err)
1073 goto done;
1074
1075 err = tg3_writephy(tp, MII_TG3_MMD_ADDRESS, val);
1076
1077done:
1078 return err;
1079}
1080
1081static int tg3_phy_cl45_read(struct tg3 *tp, u32 devad, u32 addr, u32 *val)
1082{
1083 int err;
1084
1085 err = tg3_writephy(tp, MII_TG3_MMD_CTRL, devad);
1086 if (err)
1087 goto done;
1088
1089 err = tg3_writephy(tp, MII_TG3_MMD_ADDRESS, addr);
1090 if (err)
1091 goto done;
1092
1093 err = tg3_writephy(tp, MII_TG3_MMD_CTRL,
1094 MII_TG3_MMD_CTRL_DATA_NOINC | devad);
1095 if (err)
1096 goto done;
1097
1098 err = tg3_readphy(tp, MII_TG3_MMD_ADDRESS, val);
1099
1100done:
1101 return err;
1102}
1103
1104static int tg3_phydsp_read(struct tg3 *tp, u32 reg, u32 *val)
1105{
1106 int err;
1107
1108 err = tg3_writephy(tp, MII_TG3_DSP_ADDRESS, reg);
1109 if (!err)
1110 err = tg3_readphy(tp, MII_TG3_DSP_RW_PORT, val);
1111
1112 return err;
1113}
1114
1115static int tg3_phydsp_write(struct tg3 *tp, u32 reg, u32 val)
1116{
1117 int err;
1118
1119 err = tg3_writephy(tp, MII_TG3_DSP_ADDRESS, reg);
1120 if (!err)
1121 err = tg3_writephy(tp, MII_TG3_DSP_RW_PORT, val);
1122
1123 return err;
1124}
1125
Matt Carlson15ee95c2011-04-20 07:57:40 +00001126static int tg3_phy_auxctl_read(struct tg3 *tp, int reg, u32 *val)
1127{
1128 int err;
1129
1130 err = tg3_writephy(tp, MII_TG3_AUX_CTRL,
1131 (reg << MII_TG3_AUXCTL_MISC_RDSEL_SHIFT) |
1132 MII_TG3_AUXCTL_SHDWSEL_MISC);
1133 if (!err)
1134 err = tg3_readphy(tp, MII_TG3_AUX_CTRL, val);
1135
1136 return err;
1137}
1138
Matt Carlsonb4bd2922011-04-20 07:57:41 +00001139static int tg3_phy_auxctl_write(struct tg3 *tp, int reg, u32 set)
1140{
1141 if (reg == MII_TG3_AUXCTL_SHDWSEL_MISC)
1142 set |= MII_TG3_AUXCTL_MISC_WREN;
1143
1144 return tg3_writephy(tp, MII_TG3_AUX_CTRL, set | reg);
1145}
1146
Matt Carlson1d36ba42011-04-20 07:57:42 +00001147#define TG3_PHY_AUXCTL_SMDSP_ENABLE(tp) \
1148 tg3_phy_auxctl_write((tp), MII_TG3_AUXCTL_SHDWSEL_AUXCTL, \
1149 MII_TG3_AUXCTL_ACTL_SMDSP_ENA | \
1150 MII_TG3_AUXCTL_ACTL_TX_6DB)
1151
1152#define TG3_PHY_AUXCTL_SMDSP_DISABLE(tp) \
1153 tg3_phy_auxctl_write((tp), MII_TG3_AUXCTL_SHDWSEL_AUXCTL, \
1154 MII_TG3_AUXCTL_ACTL_TX_6DB);
1155
Matt Carlson95e28692008-05-25 23:44:14 -07001156static int tg3_bmcr_reset(struct tg3 *tp)
1157{
1158 u32 phy_control;
1159 int limit, err;
1160
1161 /* OK, reset it, and poll the BMCR_RESET bit until it
1162 * clears or we time out.
1163 */
1164 phy_control = BMCR_RESET;
1165 err = tg3_writephy(tp, MII_BMCR, phy_control);
1166 if (err != 0)
1167 return -EBUSY;
1168
1169 limit = 5000;
1170 while (limit--) {
1171 err = tg3_readphy(tp, MII_BMCR, &phy_control);
1172 if (err != 0)
1173 return -EBUSY;
1174
1175 if ((phy_control & BMCR_RESET) == 0) {
1176 udelay(40);
1177 break;
1178 }
1179 udelay(10);
1180 }
Roel Kluind4675b52009-02-12 16:33:27 -08001181 if (limit < 0)
Matt Carlson95e28692008-05-25 23:44:14 -07001182 return -EBUSY;
1183
1184 return 0;
1185}
1186
Matt Carlson158d7ab2008-05-29 01:37:54 -07001187static int tg3_mdio_read(struct mii_bus *bp, int mii_id, int reg)
1188{
Francois Romieu3d165432009-01-19 16:56:50 -08001189 struct tg3 *tp = bp->priv;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001190 u32 val;
1191
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001192 spin_lock_bh(&tp->lock);
Matt Carlson158d7ab2008-05-29 01:37:54 -07001193
1194 if (tg3_readphy(tp, reg, &val))
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001195 val = -EIO;
1196
1197 spin_unlock_bh(&tp->lock);
Matt Carlson158d7ab2008-05-29 01:37:54 -07001198
1199 return val;
1200}
1201
1202static int tg3_mdio_write(struct mii_bus *bp, int mii_id, int reg, u16 val)
1203{
Francois Romieu3d165432009-01-19 16:56:50 -08001204 struct tg3 *tp = bp->priv;
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001205 u32 ret = 0;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001206
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001207 spin_lock_bh(&tp->lock);
Matt Carlson158d7ab2008-05-29 01:37:54 -07001208
1209 if (tg3_writephy(tp, reg, val))
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001210 ret = -EIO;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001211
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001212 spin_unlock_bh(&tp->lock);
1213
1214 return ret;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001215}
1216
1217static int tg3_mdio_reset(struct mii_bus *bp)
1218{
1219 return 0;
1220}
1221
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001222static void tg3_mdio_config_5785(struct tg3 *tp)
Matt Carlsona9daf362008-05-25 23:49:44 -07001223{
1224 u32 val;
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001225 struct phy_device *phydev;
Matt Carlsona9daf362008-05-25 23:49:44 -07001226
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001227 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001228 switch (phydev->drv->phy_id & phydev->drv->phy_id_mask) {
Matt Carlson6a443a02010-02-17 15:17:04 +00001229 case PHY_ID_BCM50610:
1230 case PHY_ID_BCM50610M:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001231 val = MAC_PHYCFG2_50610_LED_MODES;
1232 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001233 case PHY_ID_BCMAC131:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001234 val = MAC_PHYCFG2_AC131_LED_MODES;
1235 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001236 case PHY_ID_RTL8211C:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001237 val = MAC_PHYCFG2_RTL8211C_LED_MODES;
1238 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001239 case PHY_ID_RTL8201E:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001240 val = MAC_PHYCFG2_RTL8201E_LED_MODES;
1241 break;
1242 default:
Matt Carlsona9daf362008-05-25 23:49:44 -07001243 return;
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001244 }
1245
1246 if (phydev->interface != PHY_INTERFACE_MODE_RGMII) {
1247 tw32(MAC_PHYCFG2, val);
1248
1249 val = tr32(MAC_PHYCFG1);
Matt Carlsonbb85fbb2009-08-25 10:09:07 +00001250 val &= ~(MAC_PHYCFG1_RGMII_INT |
1251 MAC_PHYCFG1_RXCLK_TO_MASK | MAC_PHYCFG1_TXCLK_TO_MASK);
1252 val |= MAC_PHYCFG1_RXCLK_TIMEOUT | MAC_PHYCFG1_TXCLK_TIMEOUT;
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001253 tw32(MAC_PHYCFG1, val);
1254
1255 return;
1256 }
1257
Joe Perches63c3a662011-04-26 08:12:10 +00001258 if (!tg3_flag(tp, RGMII_INBAND_DISABLE))
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001259 val |= MAC_PHYCFG2_EMODE_MASK_MASK |
1260 MAC_PHYCFG2_FMODE_MASK_MASK |
1261 MAC_PHYCFG2_GMODE_MASK_MASK |
1262 MAC_PHYCFG2_ACT_MASK_MASK |
1263 MAC_PHYCFG2_QUAL_MASK_MASK |
1264 MAC_PHYCFG2_INBAND_ENABLE;
1265
1266 tw32(MAC_PHYCFG2, val);
Matt Carlsona9daf362008-05-25 23:49:44 -07001267
Matt Carlsonbb85fbb2009-08-25 10:09:07 +00001268 val = tr32(MAC_PHYCFG1);
1269 val &= ~(MAC_PHYCFG1_RXCLK_TO_MASK | MAC_PHYCFG1_TXCLK_TO_MASK |
1270 MAC_PHYCFG1_RGMII_EXT_RX_DEC | MAC_PHYCFG1_RGMII_SND_STAT_EN);
Joe Perches63c3a662011-04-26 08:12:10 +00001271 if (!tg3_flag(tp, RGMII_INBAND_DISABLE)) {
1272 if (tg3_flag(tp, RGMII_EXT_IBND_RX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001273 val |= MAC_PHYCFG1_RGMII_EXT_RX_DEC;
Joe Perches63c3a662011-04-26 08:12:10 +00001274 if (tg3_flag(tp, RGMII_EXT_IBND_TX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001275 val |= MAC_PHYCFG1_RGMII_SND_STAT_EN;
1276 }
Matt Carlsonbb85fbb2009-08-25 10:09:07 +00001277 val |= MAC_PHYCFG1_RXCLK_TIMEOUT | MAC_PHYCFG1_TXCLK_TIMEOUT |
1278 MAC_PHYCFG1_RGMII_INT | MAC_PHYCFG1_TXC_DRV;
1279 tw32(MAC_PHYCFG1, val);
Matt Carlsona9daf362008-05-25 23:49:44 -07001280
Matt Carlsona9daf362008-05-25 23:49:44 -07001281 val = tr32(MAC_EXT_RGMII_MODE);
1282 val &= ~(MAC_RGMII_MODE_RX_INT_B |
1283 MAC_RGMII_MODE_RX_QUALITY |
1284 MAC_RGMII_MODE_RX_ACTIVITY |
1285 MAC_RGMII_MODE_RX_ENG_DET |
1286 MAC_RGMII_MODE_TX_ENABLE |
1287 MAC_RGMII_MODE_TX_LOWPWR |
1288 MAC_RGMII_MODE_TX_RESET);
Joe Perches63c3a662011-04-26 08:12:10 +00001289 if (!tg3_flag(tp, RGMII_INBAND_DISABLE)) {
1290 if (tg3_flag(tp, RGMII_EXT_IBND_RX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001291 val |= MAC_RGMII_MODE_RX_INT_B |
1292 MAC_RGMII_MODE_RX_QUALITY |
1293 MAC_RGMII_MODE_RX_ACTIVITY |
1294 MAC_RGMII_MODE_RX_ENG_DET;
Joe Perches63c3a662011-04-26 08:12:10 +00001295 if (tg3_flag(tp, RGMII_EXT_IBND_TX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001296 val |= MAC_RGMII_MODE_TX_ENABLE |
1297 MAC_RGMII_MODE_TX_LOWPWR |
1298 MAC_RGMII_MODE_TX_RESET;
1299 }
1300 tw32(MAC_EXT_RGMII_MODE, val);
1301}
1302
Matt Carlson158d7ab2008-05-29 01:37:54 -07001303static void tg3_mdio_start(struct tg3 *tp)
1304{
Matt Carlson158d7ab2008-05-29 01:37:54 -07001305 tp->mi_mode &= ~MAC_MI_MODE_AUTO_POLL;
1306 tw32_f(MAC_MI_MODE, tp->mi_mode);
1307 udelay(80);
Matt Carlsona9daf362008-05-25 23:49:44 -07001308
Joe Perches63c3a662011-04-26 08:12:10 +00001309 if (tg3_flag(tp, MDIOBUS_INITED) &&
Matt Carlson9ea48182010-02-17 15:17:01 +00001310 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
1311 tg3_mdio_config_5785(tp);
1312}
1313
1314static int tg3_mdio_init(struct tg3 *tp)
1315{
1316 int i;
1317 u32 reg;
1318 struct phy_device *phydev;
1319
Joe Perches63c3a662011-04-26 08:12:10 +00001320 if (tg3_flag(tp, 5717_PLUS)) {
Matt Carlson9c7df912010-06-05 17:24:36 +00001321 u32 is_serdes;
Matt Carlson882e9792009-09-01 13:21:36 +00001322
Matt Carlson69f11c92011-07-13 09:27:30 +00001323 tp->phy_addr = tp->pci_fn + 1;
Matt Carlson882e9792009-09-01 13:21:36 +00001324
Matt Carlsond1ec96a2010-01-12 10:11:38 +00001325 if (tp->pci_chip_rev_id != CHIPREV_ID_5717_A0)
1326 is_serdes = tr32(SG_DIG_STATUS) & SG_DIG_IS_SERDES;
1327 else
1328 is_serdes = tr32(TG3_CPMU_PHY_STRAP) &
1329 TG3_CPMU_PHY_STRAP_IS_SERDES;
Matt Carlson882e9792009-09-01 13:21:36 +00001330 if (is_serdes)
1331 tp->phy_addr += 7;
1332 } else
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001333 tp->phy_addr = TG3_PHY_MII_ADDR;
Matt Carlson882e9792009-09-01 13:21:36 +00001334
Matt Carlson158d7ab2008-05-29 01:37:54 -07001335 tg3_mdio_start(tp);
1336
Joe Perches63c3a662011-04-26 08:12:10 +00001337 if (!tg3_flag(tp, USE_PHYLIB) || tg3_flag(tp, MDIOBUS_INITED))
Matt Carlson158d7ab2008-05-29 01:37:54 -07001338 return 0;
1339
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001340 tp->mdio_bus = mdiobus_alloc();
1341 if (tp->mdio_bus == NULL)
1342 return -ENOMEM;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001343
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001344 tp->mdio_bus->name = "tg3 mdio bus";
1345 snprintf(tp->mdio_bus->id, MII_BUS_ID_SIZE, "%x",
Matt Carlson158d7ab2008-05-29 01:37:54 -07001346 (tp->pdev->bus->number << 8) | tp->pdev->devfn);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001347 tp->mdio_bus->priv = tp;
1348 tp->mdio_bus->parent = &tp->pdev->dev;
1349 tp->mdio_bus->read = &tg3_mdio_read;
1350 tp->mdio_bus->write = &tg3_mdio_write;
1351 tp->mdio_bus->reset = &tg3_mdio_reset;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001352 tp->mdio_bus->phy_mask = ~(1 << TG3_PHY_MII_ADDR);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001353 tp->mdio_bus->irq = &tp->mdio_irq[0];
Matt Carlson158d7ab2008-05-29 01:37:54 -07001354
1355 for (i = 0; i < PHY_MAX_ADDR; i++)
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001356 tp->mdio_bus->irq[i] = PHY_POLL;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001357
1358 /* The bus registration will look for all the PHYs on the mdio bus.
1359 * Unfortunately, it does not ensure the PHY is powered up before
1360 * accessing the PHY ID registers. A chip reset is the
1361 * quickest way to bring the device back to an operational state..
1362 */
1363 if (tg3_readphy(tp, MII_BMCR, &reg) || (reg & BMCR_PDOWN))
1364 tg3_bmcr_reset(tp);
1365
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001366 i = mdiobus_register(tp->mdio_bus);
Matt Carlsona9daf362008-05-25 23:49:44 -07001367 if (i) {
Matt Carlsonab96b242010-04-05 10:19:22 +00001368 dev_warn(&tp->pdev->dev, "mdiobus_reg failed (0x%x)\n", i);
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001369 mdiobus_free(tp->mdio_bus);
Matt Carlsona9daf362008-05-25 23:49:44 -07001370 return i;
1371 }
Matt Carlson158d7ab2008-05-29 01:37:54 -07001372
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001373 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsona9daf362008-05-25 23:49:44 -07001374
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001375 if (!phydev || !phydev->drv) {
Matt Carlsonab96b242010-04-05 10:19:22 +00001376 dev_warn(&tp->pdev->dev, "No PHY devices\n");
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001377 mdiobus_unregister(tp->mdio_bus);
1378 mdiobus_free(tp->mdio_bus);
1379 return -ENODEV;
1380 }
1381
1382 switch (phydev->drv->phy_id & phydev->drv->phy_id_mask) {
Matt Carlson6a443a02010-02-17 15:17:04 +00001383 case PHY_ID_BCM57780:
Matt Carlson321d32a2008-11-21 17:22:19 -08001384 phydev->interface = PHY_INTERFACE_MODE_GMII;
Matt Carlsonc704dc22009-11-02 14:32:12 +00001385 phydev->dev_flags |= PHY_BRCM_AUTO_PWRDWN_ENABLE;
Matt Carlson321d32a2008-11-21 17:22:19 -08001386 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001387 case PHY_ID_BCM50610:
1388 case PHY_ID_BCM50610M:
Matt Carlson32e5a8d2009-11-02 14:31:39 +00001389 phydev->dev_flags |= PHY_BRCM_CLEAR_RGMII_MODE |
Matt Carlsonc704dc22009-11-02 14:32:12 +00001390 PHY_BRCM_RX_REFCLK_UNUSED |
Matt Carlson52fae082009-11-02 14:32:38 +00001391 PHY_BRCM_DIS_TXCRXC_NOENRGY |
Matt Carlsonc704dc22009-11-02 14:32:12 +00001392 PHY_BRCM_AUTO_PWRDWN_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00001393 if (tg3_flag(tp, RGMII_INBAND_DISABLE))
Matt Carlsona9daf362008-05-25 23:49:44 -07001394 phydev->dev_flags |= PHY_BRCM_STD_IBND_DISABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00001395 if (tg3_flag(tp, RGMII_EXT_IBND_RX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001396 phydev->dev_flags |= PHY_BRCM_EXT_IBND_RX_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00001397 if (tg3_flag(tp, RGMII_EXT_IBND_TX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001398 phydev->dev_flags |= PHY_BRCM_EXT_IBND_TX_ENABLE;
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001399 /* fallthru */
Matt Carlson6a443a02010-02-17 15:17:04 +00001400 case PHY_ID_RTL8211C:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001401 phydev->interface = PHY_INTERFACE_MODE_RGMII;
Matt Carlsona9daf362008-05-25 23:49:44 -07001402 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001403 case PHY_ID_RTL8201E:
1404 case PHY_ID_BCMAC131:
Matt Carlsona9daf362008-05-25 23:49:44 -07001405 phydev->interface = PHY_INTERFACE_MODE_MII;
Matt Carlsoncdd4e09d2009-11-02 14:31:11 +00001406 phydev->dev_flags |= PHY_BRCM_AUTO_PWRDWN_ENABLE;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001407 tp->phy_flags |= TG3_PHYFLG_IS_FET;
Matt Carlsona9daf362008-05-25 23:49:44 -07001408 break;
1409 }
1410
Joe Perches63c3a662011-04-26 08:12:10 +00001411 tg3_flag_set(tp, MDIOBUS_INITED);
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001412
1413 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
1414 tg3_mdio_config_5785(tp);
Matt Carlsona9daf362008-05-25 23:49:44 -07001415
1416 return 0;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001417}
1418
1419static void tg3_mdio_fini(struct tg3 *tp)
1420{
Joe Perches63c3a662011-04-26 08:12:10 +00001421 if (tg3_flag(tp, MDIOBUS_INITED)) {
1422 tg3_flag_clear(tp, MDIOBUS_INITED);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001423 mdiobus_unregister(tp->mdio_bus);
1424 mdiobus_free(tp->mdio_bus);
Matt Carlson158d7ab2008-05-29 01:37:54 -07001425 }
1426}
1427
Matt Carlson95e28692008-05-25 23:44:14 -07001428/* tp->lock is held. */
Matt Carlson4ba526c2008-08-15 14:10:04 -07001429static inline void tg3_generate_fw_event(struct tg3 *tp)
1430{
1431 u32 val;
1432
1433 val = tr32(GRC_RX_CPU_EVENT);
1434 val |= GRC_RX_CPU_DRIVER_EVENT;
1435 tw32_f(GRC_RX_CPU_EVENT, val);
1436
1437 tp->last_event_jiffies = jiffies;
1438}
1439
1440#define TG3_FW_EVENT_TIMEOUT_USEC 2500
1441
1442/* tp->lock is held. */
Matt Carlson95e28692008-05-25 23:44:14 -07001443static void tg3_wait_for_event_ack(struct tg3 *tp)
1444{
1445 int i;
Matt Carlson4ba526c2008-08-15 14:10:04 -07001446 unsigned int delay_cnt;
1447 long time_remain;
Matt Carlson95e28692008-05-25 23:44:14 -07001448
Matt Carlson4ba526c2008-08-15 14:10:04 -07001449 /* If enough time has passed, no wait is necessary. */
1450 time_remain = (long)(tp->last_event_jiffies + 1 +
1451 usecs_to_jiffies(TG3_FW_EVENT_TIMEOUT_USEC)) -
1452 (long)jiffies;
1453 if (time_remain < 0)
1454 return;
1455
1456 /* Check if we can shorten the wait time. */
1457 delay_cnt = jiffies_to_usecs(time_remain);
1458 if (delay_cnt > TG3_FW_EVENT_TIMEOUT_USEC)
1459 delay_cnt = TG3_FW_EVENT_TIMEOUT_USEC;
1460 delay_cnt = (delay_cnt >> 3) + 1;
1461
1462 for (i = 0; i < delay_cnt; i++) {
Matt Carlson95e28692008-05-25 23:44:14 -07001463 if (!(tr32(GRC_RX_CPU_EVENT) & GRC_RX_CPU_DRIVER_EVENT))
1464 break;
Matt Carlson4ba526c2008-08-15 14:10:04 -07001465 udelay(8);
Matt Carlson95e28692008-05-25 23:44:14 -07001466 }
1467}
1468
1469/* tp->lock is held. */
Matt Carlsonb28f3892012-02-13 15:20:12 +00001470static void tg3_phy_gather_ump_data(struct tg3 *tp, u32 *data)
Matt Carlson95e28692008-05-25 23:44:14 -07001471{
Matt Carlsonb28f3892012-02-13 15:20:12 +00001472 u32 reg, val;
Matt Carlson95e28692008-05-25 23:44:14 -07001473
1474 val = 0;
1475 if (!tg3_readphy(tp, MII_BMCR, &reg))
1476 val = reg << 16;
1477 if (!tg3_readphy(tp, MII_BMSR, &reg))
1478 val |= (reg & 0xffff);
Matt Carlsonb28f3892012-02-13 15:20:12 +00001479 *data++ = val;
Matt Carlson95e28692008-05-25 23:44:14 -07001480
1481 val = 0;
1482 if (!tg3_readphy(tp, MII_ADVERTISE, &reg))
1483 val = reg << 16;
1484 if (!tg3_readphy(tp, MII_LPA, &reg))
1485 val |= (reg & 0xffff);
Matt Carlsonb28f3892012-02-13 15:20:12 +00001486 *data++ = val;
Matt Carlson95e28692008-05-25 23:44:14 -07001487
1488 val = 0;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001489 if (!(tp->phy_flags & TG3_PHYFLG_MII_SERDES)) {
Matt Carlson95e28692008-05-25 23:44:14 -07001490 if (!tg3_readphy(tp, MII_CTRL1000, &reg))
1491 val = reg << 16;
1492 if (!tg3_readphy(tp, MII_STAT1000, &reg))
1493 val |= (reg & 0xffff);
1494 }
Matt Carlsonb28f3892012-02-13 15:20:12 +00001495 *data++ = val;
Matt Carlson95e28692008-05-25 23:44:14 -07001496
1497 if (!tg3_readphy(tp, MII_PHYADDR, &reg))
1498 val = reg << 16;
1499 else
1500 val = 0;
Matt Carlsonb28f3892012-02-13 15:20:12 +00001501 *data++ = val;
1502}
1503
1504/* tp->lock is held. */
1505static void tg3_ump_link_report(struct tg3 *tp)
1506{
1507 u32 data[4];
1508
1509 if (!tg3_flag(tp, 5780_CLASS) || !tg3_flag(tp, ENABLE_ASF))
1510 return;
1511
1512 tg3_phy_gather_ump_data(tp, data);
1513
1514 tg3_wait_for_event_ack(tp);
1515
1516 tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX, FWCMD_NICDRV_LINK_UPDATE);
1517 tg3_write_mem(tp, NIC_SRAM_FW_CMD_LEN_MBOX, 14);
1518 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 0x0, data[0]);
1519 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 0x4, data[1]);
1520 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 0x8, data[2]);
1521 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 0xc, data[3]);
Matt Carlson95e28692008-05-25 23:44:14 -07001522
Matt Carlson4ba526c2008-08-15 14:10:04 -07001523 tg3_generate_fw_event(tp);
Matt Carlson95e28692008-05-25 23:44:14 -07001524}
1525
Matt Carlson8d5a89b2011-08-31 11:44:51 +00001526/* tp->lock is held. */
1527static void tg3_stop_fw(struct tg3 *tp)
1528{
1529 if (tg3_flag(tp, ENABLE_ASF) && !tg3_flag(tp, ENABLE_APE)) {
1530 /* Wait for RX cpu to ACK the previous event. */
1531 tg3_wait_for_event_ack(tp);
1532
1533 tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX, FWCMD_NICDRV_PAUSE_FW);
1534
1535 tg3_generate_fw_event(tp);
1536
1537 /* Wait for RX cpu to ACK this event. */
1538 tg3_wait_for_event_ack(tp);
1539 }
1540}
1541
Matt Carlsonfd6d3f02011-08-31 11:44:52 +00001542/* tp->lock is held. */
1543static void tg3_write_sig_pre_reset(struct tg3 *tp, int kind)
1544{
1545 tg3_write_mem(tp, NIC_SRAM_FIRMWARE_MBOX,
1546 NIC_SRAM_FIRMWARE_MBOX_MAGIC1);
1547
1548 if (tg3_flag(tp, ASF_NEW_HANDSHAKE)) {
1549 switch (kind) {
1550 case RESET_KIND_INIT:
1551 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1552 DRV_STATE_START);
1553 break;
1554
1555 case RESET_KIND_SHUTDOWN:
1556 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1557 DRV_STATE_UNLOAD);
1558 break;
1559
1560 case RESET_KIND_SUSPEND:
1561 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1562 DRV_STATE_SUSPEND);
1563 break;
1564
1565 default:
1566 break;
1567 }
1568 }
1569
1570 if (kind == RESET_KIND_INIT ||
1571 kind == RESET_KIND_SUSPEND)
1572 tg3_ape_driver_state_change(tp, kind);
1573}
1574
1575/* tp->lock is held. */
1576static void tg3_write_sig_post_reset(struct tg3 *tp, int kind)
1577{
1578 if (tg3_flag(tp, ASF_NEW_HANDSHAKE)) {
1579 switch (kind) {
1580 case RESET_KIND_INIT:
1581 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1582 DRV_STATE_START_DONE);
1583 break;
1584
1585 case RESET_KIND_SHUTDOWN:
1586 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1587 DRV_STATE_UNLOAD_DONE);
1588 break;
1589
1590 default:
1591 break;
1592 }
1593 }
1594
1595 if (kind == RESET_KIND_SHUTDOWN)
1596 tg3_ape_driver_state_change(tp, kind);
1597}
1598
1599/* tp->lock is held. */
1600static void tg3_write_sig_legacy(struct tg3 *tp, int kind)
1601{
1602 if (tg3_flag(tp, ENABLE_ASF)) {
1603 switch (kind) {
1604 case RESET_KIND_INIT:
1605 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1606 DRV_STATE_START);
1607 break;
1608
1609 case RESET_KIND_SHUTDOWN:
1610 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1611 DRV_STATE_UNLOAD);
1612 break;
1613
1614 case RESET_KIND_SUSPEND:
1615 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1616 DRV_STATE_SUSPEND);
1617 break;
1618
1619 default:
1620 break;
1621 }
1622 }
1623}
1624
1625static int tg3_poll_fw(struct tg3 *tp)
1626{
1627 int i;
1628 u32 val;
1629
1630 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
1631 /* Wait up to 20ms for init done. */
1632 for (i = 0; i < 200; i++) {
1633 if (tr32(VCPU_STATUS) & VCPU_STATUS_INIT_DONE)
1634 return 0;
1635 udelay(100);
1636 }
1637 return -ENODEV;
1638 }
1639
1640 /* Wait for firmware initialization to complete. */
1641 for (i = 0; i < 100000; i++) {
1642 tg3_read_mem(tp, NIC_SRAM_FIRMWARE_MBOX, &val);
1643 if (val == ~NIC_SRAM_FIRMWARE_MBOX_MAGIC1)
1644 break;
1645 udelay(10);
1646 }
1647
1648 /* Chip might not be fitted with firmware. Some Sun onboard
1649 * parts are configured like that. So don't signal the timeout
1650 * of the above loop as an error, but do report the lack of
1651 * running firmware once.
1652 */
1653 if (i >= 100000 && !tg3_flag(tp, NO_FWARE_REPORTED)) {
1654 tg3_flag_set(tp, NO_FWARE_REPORTED);
1655
1656 netdev_info(tp->dev, "No firmware running\n");
1657 }
1658
1659 if (tp->pci_chip_rev_id == CHIPREV_ID_57765_A0) {
1660 /* The 57765 A0 needs a little more
1661 * time to do some important work.
1662 */
1663 mdelay(10);
1664 }
1665
1666 return 0;
1667}
1668
Matt Carlson95e28692008-05-25 23:44:14 -07001669static void tg3_link_report(struct tg3 *tp)
1670{
1671 if (!netif_carrier_ok(tp->dev)) {
Joe Perches05dbe002010-02-17 19:44:19 +00001672 netif_info(tp, link, tp->dev, "Link is down\n");
Matt Carlson95e28692008-05-25 23:44:14 -07001673 tg3_ump_link_report(tp);
1674 } else if (netif_msg_link(tp)) {
Joe Perches05dbe002010-02-17 19:44:19 +00001675 netdev_info(tp->dev, "Link is up at %d Mbps, %s duplex\n",
1676 (tp->link_config.active_speed == SPEED_1000 ?
1677 1000 :
1678 (tp->link_config.active_speed == SPEED_100 ?
1679 100 : 10)),
1680 (tp->link_config.active_duplex == DUPLEX_FULL ?
1681 "full" : "half"));
Matt Carlson95e28692008-05-25 23:44:14 -07001682
Joe Perches05dbe002010-02-17 19:44:19 +00001683 netdev_info(tp->dev, "Flow control is %s for TX and %s for RX\n",
1684 (tp->link_config.active_flowctrl & FLOW_CTRL_TX) ?
1685 "on" : "off",
1686 (tp->link_config.active_flowctrl & FLOW_CTRL_RX) ?
1687 "on" : "off");
Matt Carlson47007832011-04-20 07:57:43 +00001688
1689 if (tp->phy_flags & TG3_PHYFLG_EEE_CAP)
1690 netdev_info(tp->dev, "EEE is %s\n",
1691 tp->setlpicnt ? "enabled" : "disabled");
1692
Matt Carlson95e28692008-05-25 23:44:14 -07001693 tg3_ump_link_report(tp);
1694 }
1695}
1696
Matt Carlson95e28692008-05-25 23:44:14 -07001697static u16 tg3_advert_flowctrl_1000X(u8 flow_ctrl)
1698{
1699 u16 miireg;
1700
Steve Glendinninge18ce342008-12-16 02:00:00 -08001701 if ((flow_ctrl & FLOW_CTRL_TX) && (flow_ctrl & FLOW_CTRL_RX))
Matt Carlson95e28692008-05-25 23:44:14 -07001702 miireg = ADVERTISE_1000XPAUSE;
Steve Glendinninge18ce342008-12-16 02:00:00 -08001703 else if (flow_ctrl & FLOW_CTRL_TX)
Matt Carlson95e28692008-05-25 23:44:14 -07001704 miireg = ADVERTISE_1000XPSE_ASYM;
Steve Glendinninge18ce342008-12-16 02:00:00 -08001705 else if (flow_ctrl & FLOW_CTRL_RX)
Matt Carlson95e28692008-05-25 23:44:14 -07001706 miireg = ADVERTISE_1000XPAUSE | ADVERTISE_1000XPSE_ASYM;
1707 else
1708 miireg = 0;
1709
1710 return miireg;
1711}
1712
Matt Carlson95e28692008-05-25 23:44:14 -07001713static u8 tg3_resolve_flowctrl_1000X(u16 lcladv, u16 rmtadv)
1714{
1715 u8 cap = 0;
1716
Matt Carlsonf3791cd2011-11-21 15:01:17 +00001717 if (lcladv & rmtadv & ADVERTISE_1000XPAUSE) {
1718 cap = FLOW_CTRL_TX | FLOW_CTRL_RX;
1719 } else if (lcladv & rmtadv & ADVERTISE_1000XPSE_ASYM) {
1720 if (lcladv & ADVERTISE_1000XPAUSE)
1721 cap = FLOW_CTRL_RX;
1722 if (rmtadv & ADVERTISE_1000XPAUSE)
Steve Glendinninge18ce342008-12-16 02:00:00 -08001723 cap = FLOW_CTRL_TX;
Matt Carlson95e28692008-05-25 23:44:14 -07001724 }
1725
1726 return cap;
1727}
1728
Matt Carlsonf51f3562008-05-25 23:45:08 -07001729static void tg3_setup_flow_control(struct tg3 *tp, u32 lcladv, u32 rmtadv)
Matt Carlson95e28692008-05-25 23:44:14 -07001730{
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001731 u8 autoneg;
Matt Carlsonf51f3562008-05-25 23:45:08 -07001732 u8 flowctrl = 0;
Matt Carlson95e28692008-05-25 23:44:14 -07001733 u32 old_rx_mode = tp->rx_mode;
1734 u32 old_tx_mode = tp->tx_mode;
1735
Joe Perches63c3a662011-04-26 08:12:10 +00001736 if (tg3_flag(tp, USE_PHYLIB))
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001737 autoneg = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]->autoneg;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001738 else
1739 autoneg = tp->link_config.autoneg;
1740
Joe Perches63c3a662011-04-26 08:12:10 +00001741 if (autoneg == AUTONEG_ENABLE && tg3_flag(tp, PAUSE_AUTONEG)) {
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001742 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
Matt Carlsonf51f3562008-05-25 23:45:08 -07001743 flowctrl = tg3_resolve_flowctrl_1000X(lcladv, rmtadv);
Matt Carlson95e28692008-05-25 23:44:14 -07001744 else
Steve Glendinningbc02ff92008-12-16 02:00:48 -08001745 flowctrl = mii_resolve_flowctrl_fdx(lcladv, rmtadv);
Matt Carlsonf51f3562008-05-25 23:45:08 -07001746 } else
1747 flowctrl = tp->link_config.flowctrl;
Matt Carlson95e28692008-05-25 23:44:14 -07001748
Matt Carlsonf51f3562008-05-25 23:45:08 -07001749 tp->link_config.active_flowctrl = flowctrl;
Matt Carlson95e28692008-05-25 23:44:14 -07001750
Steve Glendinninge18ce342008-12-16 02:00:00 -08001751 if (flowctrl & FLOW_CTRL_RX)
Matt Carlson95e28692008-05-25 23:44:14 -07001752 tp->rx_mode |= RX_MODE_FLOW_CTRL_ENABLE;
1753 else
1754 tp->rx_mode &= ~RX_MODE_FLOW_CTRL_ENABLE;
1755
Matt Carlsonf51f3562008-05-25 23:45:08 -07001756 if (old_rx_mode != tp->rx_mode)
Matt Carlson95e28692008-05-25 23:44:14 -07001757 tw32_f(MAC_RX_MODE, tp->rx_mode);
Matt Carlson95e28692008-05-25 23:44:14 -07001758
Steve Glendinninge18ce342008-12-16 02:00:00 -08001759 if (flowctrl & FLOW_CTRL_TX)
Matt Carlson95e28692008-05-25 23:44:14 -07001760 tp->tx_mode |= TX_MODE_FLOW_CTRL_ENABLE;
1761 else
1762 tp->tx_mode &= ~TX_MODE_FLOW_CTRL_ENABLE;
1763
Matt Carlsonf51f3562008-05-25 23:45:08 -07001764 if (old_tx_mode != tp->tx_mode)
Matt Carlson95e28692008-05-25 23:44:14 -07001765 tw32_f(MAC_TX_MODE, tp->tx_mode);
Matt Carlson95e28692008-05-25 23:44:14 -07001766}
1767
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001768static void tg3_adjust_link(struct net_device *dev)
1769{
1770 u8 oldflowctrl, linkmesg = 0;
1771 u32 mac_mode, lcl_adv, rmt_adv;
1772 struct tg3 *tp = netdev_priv(dev);
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001773 struct phy_device *phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001774
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001775 spin_lock_bh(&tp->lock);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001776
1777 mac_mode = tp->mac_mode & ~(MAC_MODE_PORT_MODE_MASK |
1778 MAC_MODE_HALF_DUPLEX);
1779
1780 oldflowctrl = tp->link_config.active_flowctrl;
1781
1782 if (phydev->link) {
1783 lcl_adv = 0;
1784 rmt_adv = 0;
1785
1786 if (phydev->speed == SPEED_100 || phydev->speed == SPEED_10)
1787 mac_mode |= MAC_MODE_PORT_MODE_MII;
Matt Carlsonc3df0742009-11-02 14:27:02 +00001788 else if (phydev->speed == SPEED_1000 ||
1789 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785)
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001790 mac_mode |= MAC_MODE_PORT_MODE_GMII;
Matt Carlsonc3df0742009-11-02 14:27:02 +00001791 else
1792 mac_mode |= MAC_MODE_PORT_MODE_MII;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001793
1794 if (phydev->duplex == DUPLEX_HALF)
1795 mac_mode |= MAC_MODE_HALF_DUPLEX;
1796 else {
Matt Carlsonf88788f2011-12-14 11:10:00 +00001797 lcl_adv = mii_advertise_flowctrl(
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001798 tp->link_config.flowctrl);
1799
1800 if (phydev->pause)
1801 rmt_adv = LPA_PAUSE_CAP;
1802 if (phydev->asym_pause)
1803 rmt_adv |= LPA_PAUSE_ASYM;
1804 }
1805
1806 tg3_setup_flow_control(tp, lcl_adv, rmt_adv);
1807 } else
1808 mac_mode |= MAC_MODE_PORT_MODE_GMII;
1809
1810 if (mac_mode != tp->mac_mode) {
1811 tp->mac_mode = mac_mode;
1812 tw32_f(MAC_MODE, tp->mac_mode);
1813 udelay(40);
1814 }
1815
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001816 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785) {
1817 if (phydev->speed == SPEED_10)
1818 tw32(MAC_MI_STAT,
1819 MAC_MI_STAT_10MBPS_MODE |
1820 MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
1821 else
1822 tw32(MAC_MI_STAT, MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
1823 }
1824
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001825 if (phydev->speed == SPEED_1000 && phydev->duplex == DUPLEX_HALF)
1826 tw32(MAC_TX_LENGTHS,
1827 ((2 << TX_LENGTHS_IPG_CRS_SHIFT) |
1828 (6 << TX_LENGTHS_IPG_SHIFT) |
1829 (0xff << TX_LENGTHS_SLOT_TIME_SHIFT)));
1830 else
1831 tw32(MAC_TX_LENGTHS,
1832 ((2 << TX_LENGTHS_IPG_CRS_SHIFT) |
1833 (6 << TX_LENGTHS_IPG_SHIFT) |
1834 (32 << TX_LENGTHS_SLOT_TIME_SHIFT)));
1835
Matt Carlson34655ad2012-02-22 12:35:18 +00001836 if (phydev->link != tp->old_link ||
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001837 phydev->speed != tp->link_config.active_speed ||
1838 phydev->duplex != tp->link_config.active_duplex ||
1839 oldflowctrl != tp->link_config.active_flowctrl)
Matt Carlsonc6cdf432010-04-05 10:19:26 +00001840 linkmesg = 1;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001841
Matt Carlson34655ad2012-02-22 12:35:18 +00001842 tp->old_link = phydev->link;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001843 tp->link_config.active_speed = phydev->speed;
1844 tp->link_config.active_duplex = phydev->duplex;
1845
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001846 spin_unlock_bh(&tp->lock);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001847
1848 if (linkmesg)
1849 tg3_link_report(tp);
1850}
1851
1852static int tg3_phy_init(struct tg3 *tp)
1853{
1854 struct phy_device *phydev;
1855
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001856 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED)
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001857 return 0;
1858
1859 /* Bring the PHY back to a known state. */
1860 tg3_bmcr_reset(tp);
1861
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001862 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001863
1864 /* Attach the MAC to the PHY. */
Kay Sieversfb28ad352008-11-10 13:55:14 -08001865 phydev = phy_connect(tp->dev, dev_name(&phydev->dev), tg3_adjust_link,
Matt Carlsona9daf362008-05-25 23:49:44 -07001866 phydev->dev_flags, phydev->interface);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001867 if (IS_ERR(phydev)) {
Matt Carlsonab96b242010-04-05 10:19:22 +00001868 dev_err(&tp->pdev->dev, "Could not attach to PHY\n");
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001869 return PTR_ERR(phydev);
1870 }
1871
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001872 /* Mask with MAC supported features. */
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001873 switch (phydev->interface) {
1874 case PHY_INTERFACE_MODE_GMII:
1875 case PHY_INTERFACE_MODE_RGMII:
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001876 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
Matt Carlson321d32a2008-11-21 17:22:19 -08001877 phydev->supported &= (PHY_GBIT_FEATURES |
1878 SUPPORTED_Pause |
1879 SUPPORTED_Asym_Pause);
1880 break;
1881 }
1882 /* fallthru */
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001883 case PHY_INTERFACE_MODE_MII:
1884 phydev->supported &= (PHY_BASIC_FEATURES |
1885 SUPPORTED_Pause |
1886 SUPPORTED_Asym_Pause);
1887 break;
1888 default:
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001889 phy_disconnect(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001890 return -EINVAL;
1891 }
1892
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001893 tp->phy_flags |= TG3_PHYFLG_IS_CONNECTED;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001894
1895 phydev->advertising = phydev->supported;
1896
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001897 return 0;
1898}
1899
1900static void tg3_phy_start(struct tg3 *tp)
1901{
1902 struct phy_device *phydev;
1903
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001904 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001905 return;
1906
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001907 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001908
Matt Carlson80096062010-08-02 11:26:06 +00001909 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) {
1910 tp->phy_flags &= ~TG3_PHYFLG_IS_LOW_POWER;
Matt Carlsonc6700ce2012-02-13 15:20:15 +00001911 phydev->speed = tp->link_config.speed;
1912 phydev->duplex = tp->link_config.duplex;
1913 phydev->autoneg = tp->link_config.autoneg;
1914 phydev->advertising = tp->link_config.advertising;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001915 }
1916
1917 phy_start(phydev);
1918
1919 phy_start_aneg(phydev);
1920}
1921
1922static void tg3_phy_stop(struct tg3 *tp)
1923{
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001924 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001925 return;
1926
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001927 phy_stop(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001928}
1929
1930static void tg3_phy_fini(struct tg3 *tp)
1931{
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001932 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001933 phy_disconnect(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001934 tp->phy_flags &= ~TG3_PHYFLG_IS_CONNECTED;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001935 }
1936}
1937
Matt Carlson941ec902011-08-19 13:58:23 +00001938static int tg3_phy_set_extloopbk(struct tg3 *tp)
1939{
1940 int err;
1941 u32 val;
1942
1943 if (tp->phy_flags & TG3_PHYFLG_IS_FET)
1944 return 0;
1945
1946 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
1947 /* Cannot do read-modify-write on 5401 */
1948 err = tg3_phy_auxctl_write(tp,
1949 MII_TG3_AUXCTL_SHDWSEL_AUXCTL,
1950 MII_TG3_AUXCTL_ACTL_EXTLOOPBK |
1951 0x4c20);
1952 goto done;
1953 }
1954
1955 err = tg3_phy_auxctl_read(tp,
1956 MII_TG3_AUXCTL_SHDWSEL_AUXCTL, &val);
1957 if (err)
1958 return err;
1959
1960 val |= MII_TG3_AUXCTL_ACTL_EXTLOOPBK;
1961 err = tg3_phy_auxctl_write(tp,
1962 MII_TG3_AUXCTL_SHDWSEL_AUXCTL, val);
1963
1964done:
1965 return err;
1966}
1967
Matt Carlson7f97a4b2009-08-25 10:10:03 +00001968static void tg3_phy_fet_toggle_apd(struct tg3 *tp, bool enable)
1969{
1970 u32 phytest;
1971
1972 if (!tg3_readphy(tp, MII_TG3_FET_TEST, &phytest)) {
1973 u32 phy;
1974
1975 tg3_writephy(tp, MII_TG3_FET_TEST,
1976 phytest | MII_TG3_FET_SHADOW_EN);
1977 if (!tg3_readphy(tp, MII_TG3_FET_SHDW_AUXSTAT2, &phy)) {
1978 if (enable)
1979 phy |= MII_TG3_FET_SHDW_AUXSTAT2_APD;
1980 else
1981 phy &= ~MII_TG3_FET_SHDW_AUXSTAT2_APD;
1982 tg3_writephy(tp, MII_TG3_FET_SHDW_AUXSTAT2, phy);
1983 }
1984 tg3_writephy(tp, MII_TG3_FET_TEST, phytest);
1985 }
1986}
1987
Matt Carlson6833c042008-11-21 17:18:59 -08001988static void tg3_phy_toggle_apd(struct tg3 *tp, bool enable)
1989{
1990 u32 reg;
1991
Joe Perches63c3a662011-04-26 08:12:10 +00001992 if (!tg3_flag(tp, 5705_PLUS) ||
1993 (tg3_flag(tp, 5717_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001994 (tp->phy_flags & TG3_PHYFLG_MII_SERDES)))
Matt Carlson6833c042008-11-21 17:18:59 -08001995 return;
1996
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001997 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Matt Carlson7f97a4b2009-08-25 10:10:03 +00001998 tg3_phy_fet_toggle_apd(tp, enable);
1999 return;
2000 }
2001
Matt Carlson6833c042008-11-21 17:18:59 -08002002 reg = MII_TG3_MISC_SHDW_WREN |
2003 MII_TG3_MISC_SHDW_SCR5_SEL |
2004 MII_TG3_MISC_SHDW_SCR5_LPED |
2005 MII_TG3_MISC_SHDW_SCR5_DLPTLM |
2006 MII_TG3_MISC_SHDW_SCR5_SDTL |
2007 MII_TG3_MISC_SHDW_SCR5_C125OE;
2008 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5784 || !enable)
2009 reg |= MII_TG3_MISC_SHDW_SCR5_DLLAPD;
2010
2011 tg3_writephy(tp, MII_TG3_MISC_SHDW, reg);
2012
2013
2014 reg = MII_TG3_MISC_SHDW_WREN |
2015 MII_TG3_MISC_SHDW_APD_SEL |
2016 MII_TG3_MISC_SHDW_APD_WKTM_84MS;
2017 if (enable)
2018 reg |= MII_TG3_MISC_SHDW_APD_ENABLE;
2019
2020 tg3_writephy(tp, MII_TG3_MISC_SHDW, reg);
2021}
2022
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002023static void tg3_phy_toggle_automdix(struct tg3 *tp, int enable)
2024{
2025 u32 phy;
2026
Joe Perches63c3a662011-04-26 08:12:10 +00002027 if (!tg3_flag(tp, 5705_PLUS) ||
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002028 (tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002029 return;
2030
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002031 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002032 u32 ephy;
2033
Matt Carlson535ef6e2009-08-25 10:09:36 +00002034 if (!tg3_readphy(tp, MII_TG3_FET_TEST, &ephy)) {
2035 u32 reg = MII_TG3_FET_SHDW_MISCCTRL;
2036
2037 tg3_writephy(tp, MII_TG3_FET_TEST,
2038 ephy | MII_TG3_FET_SHADOW_EN);
2039 if (!tg3_readphy(tp, reg, &phy)) {
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002040 if (enable)
Matt Carlson535ef6e2009-08-25 10:09:36 +00002041 phy |= MII_TG3_FET_SHDW_MISCCTRL_MDIX;
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002042 else
Matt Carlson535ef6e2009-08-25 10:09:36 +00002043 phy &= ~MII_TG3_FET_SHDW_MISCCTRL_MDIX;
2044 tg3_writephy(tp, reg, phy);
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002045 }
Matt Carlson535ef6e2009-08-25 10:09:36 +00002046 tg3_writephy(tp, MII_TG3_FET_TEST, ephy);
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002047 }
2048 } else {
Matt Carlson15ee95c2011-04-20 07:57:40 +00002049 int ret;
2050
2051 ret = tg3_phy_auxctl_read(tp,
2052 MII_TG3_AUXCTL_SHDWSEL_MISC, &phy);
2053 if (!ret) {
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002054 if (enable)
2055 phy |= MII_TG3_AUXCTL_MISC_FORCE_AMDIX;
2056 else
2057 phy &= ~MII_TG3_AUXCTL_MISC_FORCE_AMDIX;
Matt Carlsonb4bd2922011-04-20 07:57:41 +00002058 tg3_phy_auxctl_write(tp,
2059 MII_TG3_AUXCTL_SHDWSEL_MISC, phy);
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002060 }
2061 }
2062}
2063
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064static void tg3_phy_set_wirespeed(struct tg3 *tp)
2065{
Matt Carlson15ee95c2011-04-20 07:57:40 +00002066 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067 u32 val;
2068
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002069 if (tp->phy_flags & TG3_PHYFLG_NO_ETH_WIRE_SPEED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070 return;
2071
Matt Carlson15ee95c2011-04-20 07:57:40 +00002072 ret = tg3_phy_auxctl_read(tp, MII_TG3_AUXCTL_SHDWSEL_MISC, &val);
2073 if (!ret)
Matt Carlsonb4bd2922011-04-20 07:57:41 +00002074 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_MISC,
2075 val | MII_TG3_AUXCTL_MISC_WIRESPD_EN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076}
2077
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002078static void tg3_phy_apply_otp(struct tg3 *tp)
2079{
2080 u32 otp, phy;
2081
2082 if (!tp->phy_otp)
2083 return;
2084
2085 otp = tp->phy_otp;
2086
Matt Carlson1d36ba42011-04-20 07:57:42 +00002087 if (TG3_PHY_AUXCTL_SMDSP_ENABLE(tp))
2088 return;
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002089
2090 phy = ((otp & TG3_OTP_AGCTGT_MASK) >> TG3_OTP_AGCTGT_SHIFT);
2091 phy |= MII_TG3_DSP_TAP1_AGCTGT_DFLT;
2092 tg3_phydsp_write(tp, MII_TG3_DSP_TAP1, phy);
2093
2094 phy = ((otp & TG3_OTP_HPFFLTR_MASK) >> TG3_OTP_HPFFLTR_SHIFT) |
2095 ((otp & TG3_OTP_HPFOVER_MASK) >> TG3_OTP_HPFOVER_SHIFT);
2096 tg3_phydsp_write(tp, MII_TG3_DSP_AADJ1CH0, phy);
2097
2098 phy = ((otp & TG3_OTP_LPFDIS_MASK) >> TG3_OTP_LPFDIS_SHIFT);
2099 phy |= MII_TG3_DSP_AADJ1CH3_ADCCKADJ;
2100 tg3_phydsp_write(tp, MII_TG3_DSP_AADJ1CH3, phy);
2101
2102 phy = ((otp & TG3_OTP_VDAC_MASK) >> TG3_OTP_VDAC_SHIFT);
2103 tg3_phydsp_write(tp, MII_TG3_DSP_EXP75, phy);
2104
2105 phy = ((otp & TG3_OTP_10BTAMP_MASK) >> TG3_OTP_10BTAMP_SHIFT);
2106 tg3_phydsp_write(tp, MII_TG3_DSP_EXP96, phy);
2107
2108 phy = ((otp & TG3_OTP_ROFF_MASK) >> TG3_OTP_ROFF_SHIFT) |
2109 ((otp & TG3_OTP_RCOFF_MASK) >> TG3_OTP_RCOFF_SHIFT);
2110 tg3_phydsp_write(tp, MII_TG3_DSP_EXP97, phy);
2111
Matt Carlson1d36ba42011-04-20 07:57:42 +00002112 TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002113}
2114
Matt Carlson52b02d02010-10-14 10:37:41 +00002115static void tg3_phy_eee_adjust(struct tg3 *tp, u32 current_link_up)
2116{
2117 u32 val;
2118
2119 if (!(tp->phy_flags & TG3_PHYFLG_EEE_CAP))
2120 return;
2121
2122 tp->setlpicnt = 0;
2123
2124 if (tp->link_config.autoneg == AUTONEG_ENABLE &&
2125 current_link_up == 1 &&
Matt Carlsona6b68da2010-12-06 08:28:52 +00002126 tp->link_config.active_duplex == DUPLEX_FULL &&
2127 (tp->link_config.active_speed == SPEED_100 ||
2128 tp->link_config.active_speed == SPEED_1000)) {
Matt Carlson52b02d02010-10-14 10:37:41 +00002129 u32 eeectl;
2130
2131 if (tp->link_config.active_speed == SPEED_1000)
2132 eeectl = TG3_CPMU_EEE_CTRL_EXIT_16_5_US;
2133 else
2134 eeectl = TG3_CPMU_EEE_CTRL_EXIT_36_US;
2135
2136 tw32(TG3_CPMU_EEE_CTRL, eeectl);
2137
Matt Carlson3110f5f52010-12-06 08:28:50 +00002138 tg3_phy_cl45_read(tp, MDIO_MMD_AN,
2139 TG3_CL45_D7_EEERES_STAT, &val);
Matt Carlson52b02d02010-10-14 10:37:41 +00002140
Matt Carlsonb0c59432011-05-19 12:12:48 +00002141 if (val == TG3_CL45_D7_EEERES_STAT_LP_1000T ||
2142 val == TG3_CL45_D7_EEERES_STAT_LP_100TX)
Matt Carlson52b02d02010-10-14 10:37:41 +00002143 tp->setlpicnt = 2;
2144 }
2145
2146 if (!tp->setlpicnt) {
Matt Carlsonb715ce92011-07-20 10:20:52 +00002147 if (current_link_up == 1 &&
2148 !TG3_PHY_AUXCTL_SMDSP_ENABLE(tp)) {
2149 tg3_phydsp_write(tp, MII_TG3_DSP_TAP26, 0x0000);
2150 TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
2151 }
2152
Matt Carlson52b02d02010-10-14 10:37:41 +00002153 val = tr32(TG3_CPMU_EEE_MODE);
2154 tw32(TG3_CPMU_EEE_MODE, val & ~TG3_CPMU_EEEMD_LPI_ENABLE);
2155 }
2156}
2157
Matt Carlsonb0c59432011-05-19 12:12:48 +00002158static void tg3_phy_eee_enable(struct tg3 *tp)
2159{
2160 u32 val;
2161
2162 if (tp->link_config.active_speed == SPEED_1000 &&
2163 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
2164 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
Matt Carlson55086ad2011-12-14 11:09:59 +00002165 tg3_flag(tp, 57765_CLASS)) &&
Matt Carlsonb0c59432011-05-19 12:12:48 +00002166 !TG3_PHY_AUXCTL_SMDSP_ENABLE(tp)) {
Matt Carlsonb715ce92011-07-20 10:20:52 +00002167 val = MII_TG3_DSP_TAP26_ALNOKO |
2168 MII_TG3_DSP_TAP26_RMRXSTO;
2169 tg3_phydsp_write(tp, MII_TG3_DSP_TAP26, val);
Matt Carlsonb0c59432011-05-19 12:12:48 +00002170 TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
2171 }
2172
2173 val = tr32(TG3_CPMU_EEE_MODE);
2174 tw32(TG3_CPMU_EEE_MODE, val | TG3_CPMU_EEEMD_LPI_ENABLE);
2175}
2176
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177static int tg3_wait_macro_done(struct tg3 *tp)
2178{
2179 int limit = 100;
2180
2181 while (limit--) {
2182 u32 tmp32;
2183
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002184 if (!tg3_readphy(tp, MII_TG3_DSP_CONTROL, &tmp32)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185 if ((tmp32 & 0x1000) == 0)
2186 break;
2187 }
2188 }
Roel Kluind4675b52009-02-12 16:33:27 -08002189 if (limit < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190 return -EBUSY;
2191
2192 return 0;
2193}
2194
2195static int tg3_phy_write_and_check_testpat(struct tg3 *tp, int *resetp)
2196{
2197 static const u32 test_pat[4][6] = {
2198 { 0x00005555, 0x00000005, 0x00002aaa, 0x0000000a, 0x00003456, 0x00000003 },
2199 { 0x00002aaa, 0x0000000a, 0x00003333, 0x00000003, 0x0000789a, 0x00000005 },
2200 { 0x00005a5a, 0x00000005, 0x00002a6a, 0x0000000a, 0x00001bcd, 0x00000003 },
2201 { 0x00002a5a, 0x0000000a, 0x000033c3, 0x00000003, 0x00002ef1, 0x00000005 }
2202 };
2203 int chan;
2204
2205 for (chan = 0; chan < 4; chan++) {
2206 int i;
2207
2208 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
2209 (chan * 0x2000) | 0x0200);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002210 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0002);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211
2212 for (i = 0; i < 6; i++)
2213 tg3_writephy(tp, MII_TG3_DSP_RW_PORT,
2214 test_pat[chan][i]);
2215
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002216 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0202);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217 if (tg3_wait_macro_done(tp)) {
2218 *resetp = 1;
2219 return -EBUSY;
2220 }
2221
2222 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
2223 (chan * 0x2000) | 0x0200);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002224 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0082);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225 if (tg3_wait_macro_done(tp)) {
2226 *resetp = 1;
2227 return -EBUSY;
2228 }
2229
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002230 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0802);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231 if (tg3_wait_macro_done(tp)) {
2232 *resetp = 1;
2233 return -EBUSY;
2234 }
2235
2236 for (i = 0; i < 6; i += 2) {
2237 u32 low, high;
2238
2239 if (tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &low) ||
2240 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &high) ||
2241 tg3_wait_macro_done(tp)) {
2242 *resetp = 1;
2243 return -EBUSY;
2244 }
2245 low &= 0x7fff;
2246 high &= 0x000f;
2247 if (low != test_pat[chan][i] ||
2248 high != test_pat[chan][i+1]) {
2249 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x000b);
2250 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x4001);
2251 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x4005);
2252
2253 return -EBUSY;
2254 }
2255 }
2256 }
2257
2258 return 0;
2259}
2260
2261static int tg3_phy_reset_chanpat(struct tg3 *tp)
2262{
2263 int chan;
2264
2265 for (chan = 0; chan < 4; chan++) {
2266 int i;
2267
2268 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
2269 (chan * 0x2000) | 0x0200);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002270 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0002);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271 for (i = 0; i < 6; i++)
2272 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x000);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002273 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0202);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274 if (tg3_wait_macro_done(tp))
2275 return -EBUSY;
2276 }
2277
2278 return 0;
2279}
2280
2281static int tg3_phy_reset_5703_4_5(struct tg3 *tp)
2282{
2283 u32 reg32, phy9_orig;
2284 int retries, do_phy_reset, err;
2285
2286 retries = 10;
2287 do_phy_reset = 1;
2288 do {
2289 if (do_phy_reset) {
2290 err = tg3_bmcr_reset(tp);
2291 if (err)
2292 return err;
2293 do_phy_reset = 0;
2294 }
2295
2296 /* Disable transmitter and interrupt. */
2297 if (tg3_readphy(tp, MII_TG3_EXT_CTRL, &reg32))
2298 continue;
2299
2300 reg32 |= 0x3000;
2301 tg3_writephy(tp, MII_TG3_EXT_CTRL, reg32);
2302
2303 /* Set full-duplex, 1000 mbps. */
2304 tg3_writephy(tp, MII_BMCR,
Matt Carlson221c5632011-06-13 13:39:01 +00002305 BMCR_FULLDPLX | BMCR_SPEED1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306
2307 /* Set to master mode. */
Matt Carlson221c5632011-06-13 13:39:01 +00002308 if (tg3_readphy(tp, MII_CTRL1000, &phy9_orig))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309 continue;
2310
Matt Carlson221c5632011-06-13 13:39:01 +00002311 tg3_writephy(tp, MII_CTRL1000,
2312 CTL1000_AS_MASTER | CTL1000_ENABLE_MASTER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313
Matt Carlson1d36ba42011-04-20 07:57:42 +00002314 err = TG3_PHY_AUXCTL_SMDSP_ENABLE(tp);
2315 if (err)
2316 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317
2318 /* Block the PHY control access. */
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00002319 tg3_phydsp_write(tp, 0x8005, 0x0800);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320
2321 err = tg3_phy_write_and_check_testpat(tp, &do_phy_reset);
2322 if (!err)
2323 break;
2324 } while (--retries);
2325
2326 err = tg3_phy_reset_chanpat(tp);
2327 if (err)
2328 return err;
2329
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00002330 tg3_phydsp_write(tp, 0x8005, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331
2332 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x8200);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002333 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334
Matt Carlson1d36ba42011-04-20 07:57:42 +00002335 TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336
Matt Carlson221c5632011-06-13 13:39:01 +00002337 tg3_writephy(tp, MII_CTRL1000, phy9_orig);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338
2339 if (!tg3_readphy(tp, MII_TG3_EXT_CTRL, &reg32)) {
2340 reg32 &= ~0x3000;
2341 tg3_writephy(tp, MII_TG3_EXT_CTRL, reg32);
2342 } else if (!err)
2343 err = -EBUSY;
2344
2345 return err;
2346}
2347
2348/* This will reset the tigon3 PHY if there is no valid
2349 * link unless the FORCE argument is non-zero.
2350 */
2351static int tg3_phy_reset(struct tg3 *tp)
2352{
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002353 u32 val, cpmuctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354 int err;
2355
Michael Chan60189dd2006-12-17 17:08:07 -08002356 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan60189dd2006-12-17 17:08:07 -08002357 val = tr32(GRC_MISC_CFG);
2358 tw32_f(GRC_MISC_CFG, val & ~GRC_MISC_CFG_EPHY_IDDQ);
2359 udelay(40);
2360 }
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002361 err = tg3_readphy(tp, MII_BMSR, &val);
2362 err |= tg3_readphy(tp, MII_BMSR, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363 if (err != 0)
2364 return -EBUSY;
2365
Michael Chanc8e1e822006-04-29 18:55:17 -07002366 if (netif_running(tp->dev) && netif_carrier_ok(tp->dev)) {
2367 netif_carrier_off(tp->dev);
2368 tg3_link_report(tp);
2369 }
2370
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
2372 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
2373 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
2374 err = tg3_phy_reset_5703_4_5(tp);
2375 if (err)
2376 return err;
2377 goto out;
2378 }
2379
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002380 cpmuctrl = 0;
2381 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
2382 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX) {
2383 cpmuctrl = tr32(TG3_CPMU_CTRL);
2384 if (cpmuctrl & CPMU_CTRL_GPHY_10MB_RXONLY)
2385 tw32(TG3_CPMU_CTRL,
2386 cpmuctrl & ~CPMU_CTRL_GPHY_10MB_RXONLY);
2387 }
2388
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389 err = tg3_bmcr_reset(tp);
2390 if (err)
2391 return err;
2392
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002393 if (cpmuctrl & CPMU_CTRL_GPHY_10MB_RXONLY) {
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002394 val = MII_TG3_DSP_EXP8_AEDW | MII_TG3_DSP_EXP8_REJ2MHz;
2395 tg3_phydsp_write(tp, MII_TG3_DSP_EXP8, val);
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002396
2397 tw32(TG3_CPMU_CTRL, cpmuctrl);
2398 }
2399
Matt Carlsonbcb37f62008-11-03 16:52:09 -08002400 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX ||
2401 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5761_AX) {
Matt Carlsonce057f02007-11-12 21:08:03 -08002402 val = tr32(TG3_CPMU_LSPD_1000MB_CLK);
2403 if ((val & CPMU_LSPD_1000MB_MACCLK_MASK) ==
2404 CPMU_LSPD_1000MB_MACCLK_12_5) {
2405 val &= ~CPMU_LSPD_1000MB_MACCLK_MASK;
2406 udelay(40);
2407 tw32_f(TG3_CPMU_LSPD_1000MB_CLK, val);
2408 }
2409 }
2410
Joe Perches63c3a662011-04-26 08:12:10 +00002411 if (tg3_flag(tp, 5717_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002412 (tp->phy_flags & TG3_PHYFLG_MII_SERDES))
Matt Carlsonecf14102010-01-20 16:58:05 +00002413 return 0;
2414
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002415 tg3_phy_apply_otp(tp);
2416
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002417 if (tp->phy_flags & TG3_PHYFLG_ENABLE_APD)
Matt Carlson6833c042008-11-21 17:18:59 -08002418 tg3_phy_toggle_apd(tp, true);
2419 else
2420 tg3_phy_toggle_apd(tp, false);
2421
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422out:
Matt Carlson1d36ba42011-04-20 07:57:42 +00002423 if ((tp->phy_flags & TG3_PHYFLG_ADC_BUG) &&
2424 !TG3_PHY_AUXCTL_SMDSP_ENABLE(tp)) {
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00002425 tg3_phydsp_write(tp, 0x201f, 0x2aaa);
2426 tg3_phydsp_write(tp, 0x000a, 0x0323);
Matt Carlson1d36ba42011-04-20 07:57:42 +00002427 TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428 }
Matt Carlson1d36ba42011-04-20 07:57:42 +00002429
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002430 if (tp->phy_flags & TG3_PHYFLG_5704_A0_BUG) {
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002431 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8d68);
2432 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8d68);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433 }
Matt Carlson1d36ba42011-04-20 07:57:42 +00002434
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002435 if (tp->phy_flags & TG3_PHYFLG_BER_BUG) {
Matt Carlson1d36ba42011-04-20 07:57:42 +00002436 if (!TG3_PHY_AUXCTL_SMDSP_ENABLE(tp)) {
2437 tg3_phydsp_write(tp, 0x000a, 0x310b);
2438 tg3_phydsp_write(tp, 0x201f, 0x9506);
2439 tg3_phydsp_write(tp, 0x401f, 0x14e2);
2440 TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
2441 }
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002442 } else if (tp->phy_flags & TG3_PHYFLG_JITTER_BUG) {
Matt Carlson1d36ba42011-04-20 07:57:42 +00002443 if (!TG3_PHY_AUXCTL_SMDSP_ENABLE(tp)) {
2444 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x000a);
2445 if (tp->phy_flags & TG3_PHYFLG_ADJUST_TRIM) {
2446 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x110b);
2447 tg3_writephy(tp, MII_TG3_TEST1,
2448 MII_TG3_TEST1_TRIM_EN | 0x4);
2449 } else
2450 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x010b);
2451
2452 TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
2453 }
Michael Chanc424cb22006-04-29 18:56:34 -07002454 }
Matt Carlson1d36ba42011-04-20 07:57:42 +00002455
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456 /* Set Extended packet length bit (bit 14) on all chips that */
2457 /* support jumbo frames */
Matt Carlson79eb6902010-02-17 15:17:03 +00002458 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459 /* Cannot do read-modify-write on 5401 */
Matt Carlsonb4bd2922011-04-20 07:57:41 +00002460 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_AUXCTL, 0x4c20);
Joe Perches63c3a662011-04-26 08:12:10 +00002461 } else if (tg3_flag(tp, JUMBO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462 /* Set bit 14 with read-modify-write to preserve other bits */
Matt Carlson15ee95c2011-04-20 07:57:40 +00002463 err = tg3_phy_auxctl_read(tp,
2464 MII_TG3_AUXCTL_SHDWSEL_AUXCTL, &val);
2465 if (!err)
Matt Carlsonb4bd2922011-04-20 07:57:41 +00002466 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_AUXCTL,
2467 val | MII_TG3_AUXCTL_ACTL_EXTPKTLEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468 }
2469
2470 /* Set phy register 0x10 bit 0 to high fifo elasticity to support
2471 * jumbo frames transmission.
2472 */
Joe Perches63c3a662011-04-26 08:12:10 +00002473 if (tg3_flag(tp, JUMBO_CAPABLE)) {
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002474 if (!tg3_readphy(tp, MII_TG3_EXT_CTRL, &val))
Matt Carlsonc6cdf432010-04-05 10:19:26 +00002475 tg3_writephy(tp, MII_TG3_EXT_CTRL,
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002476 val | MII_TG3_EXT_CTRL_FIFO_ELASTIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477 }
2478
Michael Chan715116a2006-09-27 16:09:25 -07002479 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan715116a2006-09-27 16:09:25 -07002480 /* adjust output voltage */
Matt Carlson535ef6e2009-08-25 10:09:36 +00002481 tg3_writephy(tp, MII_TG3_FET_PTEST, 0x12);
Michael Chan715116a2006-09-27 16:09:25 -07002482 }
2483
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002484 tg3_phy_toggle_automdix(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485 tg3_phy_set_wirespeed(tp);
2486 return 0;
2487}
2488
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002489#define TG3_GPIO_MSG_DRVR_PRES 0x00000001
2490#define TG3_GPIO_MSG_NEED_VAUX 0x00000002
2491#define TG3_GPIO_MSG_MASK (TG3_GPIO_MSG_DRVR_PRES | \
2492 TG3_GPIO_MSG_NEED_VAUX)
2493#define TG3_GPIO_MSG_ALL_DRVR_PRES_MASK \
2494 ((TG3_GPIO_MSG_DRVR_PRES << 0) | \
2495 (TG3_GPIO_MSG_DRVR_PRES << 4) | \
2496 (TG3_GPIO_MSG_DRVR_PRES << 8) | \
2497 (TG3_GPIO_MSG_DRVR_PRES << 12))
2498
2499#define TG3_GPIO_MSG_ALL_NEED_VAUX_MASK \
2500 ((TG3_GPIO_MSG_NEED_VAUX << 0) | \
2501 (TG3_GPIO_MSG_NEED_VAUX << 4) | \
2502 (TG3_GPIO_MSG_NEED_VAUX << 8) | \
2503 (TG3_GPIO_MSG_NEED_VAUX << 12))
2504
2505static inline u32 tg3_set_function_status(struct tg3 *tp, u32 newstat)
2506{
2507 u32 status, shift;
2508
2509 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 status = tg3_ape_read32(tp, TG3_APE_GPIO_MSG);
2512 else
2513 status = tr32(TG3_CPMU_DRV_STATUS);
2514
2515 shift = TG3_APE_GPIO_MSG_SHIFT + 4 * tp->pci_fn;
2516 status &= ~(TG3_GPIO_MSG_MASK << shift);
2517 status |= (newstat << shift);
2518
2519 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
2520 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
2521 tg3_ape_write32(tp, TG3_APE_GPIO_MSG, status);
2522 else
2523 tw32(TG3_CPMU_DRV_STATUS, status);
2524
2525 return status >> TG3_APE_GPIO_MSG_SHIFT;
2526}
2527
Matt Carlson520b2752011-06-13 13:39:02 +00002528static inline int tg3_pwrsrc_switch_to_vmain(struct tg3 *tp)
2529{
2530 if (!tg3_flag(tp, IS_NIC))
2531 return 0;
2532
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002533 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
2534 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
2535 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
2536 if (tg3_ape_lock(tp, TG3_APE_LOCK_GPIO))
2537 return -EIO;
Matt Carlson520b2752011-06-13 13:39:02 +00002538
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002539 tg3_set_function_status(tp, TG3_GPIO_MSG_DRVR_PRES);
2540
2541 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl,
2542 TG3_GRC_LCLCTL_PWRSW_DELAY);
2543
2544 tg3_ape_unlock(tp, TG3_APE_LOCK_GPIO);
2545 } else {
2546 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl,
2547 TG3_GRC_LCLCTL_PWRSW_DELAY);
2548 }
Matt Carlson6f5c8f832011-07-13 09:27:31 +00002549
Matt Carlson520b2752011-06-13 13:39:02 +00002550 return 0;
2551}
2552
2553static void tg3_pwrsrc_die_with_vmain(struct tg3 *tp)
2554{
2555 u32 grc_local_ctrl;
2556
2557 if (!tg3_flag(tp, IS_NIC) ||
2558 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2559 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)
2560 return;
2561
2562 grc_local_ctrl = tp->grc_local_ctrl | GRC_LCLCTRL_GPIO_OE1;
2563
2564 tw32_wait_f(GRC_LOCAL_CTRL,
2565 grc_local_ctrl | GRC_LCLCTRL_GPIO_OUTPUT1,
2566 TG3_GRC_LCLCTL_PWRSW_DELAY);
2567
2568 tw32_wait_f(GRC_LOCAL_CTRL,
2569 grc_local_ctrl,
2570 TG3_GRC_LCLCTL_PWRSW_DELAY);
2571
2572 tw32_wait_f(GRC_LOCAL_CTRL,
2573 grc_local_ctrl | GRC_LCLCTRL_GPIO_OUTPUT1,
2574 TG3_GRC_LCLCTL_PWRSW_DELAY);
2575}
2576
2577static void tg3_pwrsrc_switch_to_vaux(struct tg3 *tp)
2578{
2579 if (!tg3_flag(tp, IS_NIC))
2580 return;
2581
2582 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2583 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
2584 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2585 (GRC_LCLCTRL_GPIO_OE0 |
2586 GRC_LCLCTRL_GPIO_OE1 |
2587 GRC_LCLCTRL_GPIO_OE2 |
2588 GRC_LCLCTRL_GPIO_OUTPUT0 |
2589 GRC_LCLCTRL_GPIO_OUTPUT1),
2590 TG3_GRC_LCLCTL_PWRSW_DELAY);
2591 } else if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761 ||
2592 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761S) {
2593 /* The 5761 non-e device swaps GPIO 0 and GPIO 2. */
2594 u32 grc_local_ctrl = GRC_LCLCTRL_GPIO_OE0 |
2595 GRC_LCLCTRL_GPIO_OE1 |
2596 GRC_LCLCTRL_GPIO_OE2 |
2597 GRC_LCLCTRL_GPIO_OUTPUT0 |
2598 GRC_LCLCTRL_GPIO_OUTPUT1 |
2599 tp->grc_local_ctrl;
2600 tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl,
2601 TG3_GRC_LCLCTL_PWRSW_DELAY);
2602
2603 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OUTPUT2;
2604 tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl,
2605 TG3_GRC_LCLCTL_PWRSW_DELAY);
2606
2607 grc_local_ctrl &= ~GRC_LCLCTRL_GPIO_OUTPUT0;
2608 tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl,
2609 TG3_GRC_LCLCTL_PWRSW_DELAY);
2610 } else {
2611 u32 no_gpio2;
2612 u32 grc_local_ctrl = 0;
2613
2614 /* Workaround to prevent overdrawing Amps. */
2615 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
2616 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE3;
2617 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2618 grc_local_ctrl,
2619 TG3_GRC_LCLCTL_PWRSW_DELAY);
2620 }
2621
2622 /* On 5753 and variants, GPIO2 cannot be used. */
2623 no_gpio2 = tp->nic_sram_data_cfg &
2624 NIC_SRAM_DATA_CFG_NO_GPIO2;
2625
2626 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE0 |
2627 GRC_LCLCTRL_GPIO_OE1 |
2628 GRC_LCLCTRL_GPIO_OE2 |
2629 GRC_LCLCTRL_GPIO_OUTPUT1 |
2630 GRC_LCLCTRL_GPIO_OUTPUT2;
2631 if (no_gpio2) {
2632 grc_local_ctrl &= ~(GRC_LCLCTRL_GPIO_OE2 |
2633 GRC_LCLCTRL_GPIO_OUTPUT2);
2634 }
2635 tw32_wait_f(GRC_LOCAL_CTRL,
2636 tp->grc_local_ctrl | grc_local_ctrl,
2637 TG3_GRC_LCLCTL_PWRSW_DELAY);
2638
2639 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OUTPUT0;
2640
2641 tw32_wait_f(GRC_LOCAL_CTRL,
2642 tp->grc_local_ctrl | grc_local_ctrl,
2643 TG3_GRC_LCLCTL_PWRSW_DELAY);
2644
2645 if (!no_gpio2) {
2646 grc_local_ctrl &= ~GRC_LCLCTRL_GPIO_OUTPUT2;
2647 tw32_wait_f(GRC_LOCAL_CTRL,
2648 tp->grc_local_ctrl | grc_local_ctrl,
2649 TG3_GRC_LCLCTL_PWRSW_DELAY);
2650 }
2651 }
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002652}
Matt Carlson6f5c8f832011-07-13 09:27:31 +00002653
Matt Carlsoncd0d7222011-07-13 09:27:33 +00002654static void tg3_frob_aux_power_5717(struct tg3 *tp, bool wol_enable)
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002655{
2656 u32 msg = 0;
2657
2658 /* Serialize power state transitions */
2659 if (tg3_ape_lock(tp, TG3_APE_LOCK_GPIO))
2660 return;
2661
Matt Carlsoncd0d7222011-07-13 09:27:33 +00002662 if (tg3_flag(tp, ENABLE_ASF) || tg3_flag(tp, ENABLE_APE) || wol_enable)
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002663 msg = TG3_GPIO_MSG_NEED_VAUX;
2664
2665 msg = tg3_set_function_status(tp, msg);
2666
2667 if (msg & TG3_GPIO_MSG_ALL_DRVR_PRES_MASK)
2668 goto done;
2669
2670 if (msg & TG3_GPIO_MSG_ALL_NEED_VAUX_MASK)
2671 tg3_pwrsrc_switch_to_vaux(tp);
2672 else
2673 tg3_pwrsrc_die_with_vmain(tp);
2674
2675done:
Matt Carlson6f5c8f832011-07-13 09:27:31 +00002676 tg3_ape_unlock(tp, TG3_APE_LOCK_GPIO);
Matt Carlson520b2752011-06-13 13:39:02 +00002677}
2678
Matt Carlsoncd0d7222011-07-13 09:27:33 +00002679static void tg3_frob_aux_power(struct tg3 *tp, bool include_wol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680{
Matt Carlson683644b2011-03-09 16:58:23 +00002681 bool need_vaux = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002682
Matt Carlson334355a2010-01-20 16:58:10 +00002683 /* The GPIOs do something completely different on 57765. */
Matt Carlson55086ad2011-12-14 11:09:59 +00002684 if (!tg3_flag(tp, IS_NIC) || tg3_flag(tp, 57765_CLASS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002685 return;
2686
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002687 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
2688 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
2689 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
Matt Carlsoncd0d7222011-07-13 09:27:33 +00002690 tg3_frob_aux_power_5717(tp, include_wol ?
2691 tg3_flag(tp, WOL_ENABLE) != 0 : 0);
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002692 return;
2693 }
2694
2695 if (tp->pdev_peer && tp->pdev_peer != tp->pdev) {
Michael Chan8c2dc7e2005-12-19 16:26:02 -08002696 struct net_device *dev_peer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002697
Michael Chan8c2dc7e2005-12-19 16:26:02 -08002698 dev_peer = pci_get_drvdata(tp->pdev_peer);
Matt Carlson683644b2011-03-09 16:58:23 +00002699
Michael Chanbc1c7562006-03-20 17:48:03 -08002700 /* remove_one() may have been run on the peer. */
Matt Carlson683644b2011-03-09 16:58:23 +00002701 if (dev_peer) {
2702 struct tg3 *tp_peer = netdev_priv(dev_peer);
2703
Joe Perches63c3a662011-04-26 08:12:10 +00002704 if (tg3_flag(tp_peer, INIT_COMPLETE))
Matt Carlson683644b2011-03-09 16:58:23 +00002705 return;
2706
Matt Carlsoncd0d7222011-07-13 09:27:33 +00002707 if ((include_wol && tg3_flag(tp_peer, WOL_ENABLE)) ||
Joe Perches63c3a662011-04-26 08:12:10 +00002708 tg3_flag(tp_peer, ENABLE_ASF))
Matt Carlson683644b2011-03-09 16:58:23 +00002709 need_vaux = true;
2710 }
Michael Chan8c2dc7e2005-12-19 16:26:02 -08002711 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002712
Matt Carlsoncd0d7222011-07-13 09:27:33 +00002713 if ((include_wol && tg3_flag(tp, WOL_ENABLE)) ||
2714 tg3_flag(tp, ENABLE_ASF))
Matt Carlson683644b2011-03-09 16:58:23 +00002715 need_vaux = true;
2716
Matt Carlson520b2752011-06-13 13:39:02 +00002717 if (need_vaux)
2718 tg3_pwrsrc_switch_to_vaux(tp);
2719 else
2720 tg3_pwrsrc_die_with_vmain(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721}
2722
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07002723static int tg3_5700_link_polarity(struct tg3 *tp, u32 speed)
2724{
2725 if (tp->led_ctrl == LED_CTRL_MODE_PHY_2)
2726 return 1;
Matt Carlson79eb6902010-02-17 15:17:03 +00002727 else if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5411) {
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07002728 if (speed != SPEED_10)
2729 return 1;
2730 } else if (speed == SPEED_10)
2731 return 1;
2732
2733 return 0;
2734}
2735
Matt Carlson0a459aa2008-11-03 16:54:15 -08002736static void tg3_power_down_phy(struct tg3 *tp, bool do_low_power)
Michael Chan15c3b692006-03-22 01:06:52 -08002737{
Matt Carlsonce057f02007-11-12 21:08:03 -08002738 u32 val;
2739
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002740 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Michael Chan51297242007-02-13 12:17:57 -08002741 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
2742 u32 sg_dig_ctrl = tr32(SG_DIG_CTRL);
2743 u32 serdes_cfg = tr32(MAC_SERDES_CFG);
2744
2745 sg_dig_ctrl |=
2746 SG_DIG_USING_HW_AUTONEG | SG_DIG_SOFT_RESET;
2747 tw32(SG_DIG_CTRL, sg_dig_ctrl);
2748 tw32(MAC_SERDES_CFG, serdes_cfg | (1 << 15));
2749 }
Michael Chan3f7045c2006-09-27 16:02:29 -07002750 return;
Michael Chan51297242007-02-13 12:17:57 -08002751 }
Michael Chan3f7045c2006-09-27 16:02:29 -07002752
Michael Chan60189dd2006-12-17 17:08:07 -08002753 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan60189dd2006-12-17 17:08:07 -08002754 tg3_bmcr_reset(tp);
2755 val = tr32(GRC_MISC_CFG);
2756 tw32_f(GRC_MISC_CFG, val | GRC_MISC_CFG_EPHY_IDDQ);
2757 udelay(40);
2758 return;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002759 } else if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Matt Carlson0e5f7842009-11-02 14:26:38 +00002760 u32 phytest;
2761 if (!tg3_readphy(tp, MII_TG3_FET_TEST, &phytest)) {
2762 u32 phy;
2763
2764 tg3_writephy(tp, MII_ADVERTISE, 0);
2765 tg3_writephy(tp, MII_BMCR,
2766 BMCR_ANENABLE | BMCR_ANRESTART);
2767
2768 tg3_writephy(tp, MII_TG3_FET_TEST,
2769 phytest | MII_TG3_FET_SHADOW_EN);
2770 if (!tg3_readphy(tp, MII_TG3_FET_SHDW_AUXMODE4, &phy)) {
2771 phy |= MII_TG3_FET_SHDW_AUXMODE4_SBPD;
2772 tg3_writephy(tp,
2773 MII_TG3_FET_SHDW_AUXMODE4,
2774 phy);
2775 }
2776 tg3_writephy(tp, MII_TG3_FET_TEST, phytest);
2777 }
2778 return;
Matt Carlson0a459aa2008-11-03 16:54:15 -08002779 } else if (do_low_power) {
Michael Chan715116a2006-09-27 16:09:25 -07002780 tg3_writephy(tp, MII_TG3_EXT_CTRL,
2781 MII_TG3_EXT_CTRL_FORCE_LED_OFF);
Matt Carlson0a459aa2008-11-03 16:54:15 -08002782
Matt Carlsonb4bd2922011-04-20 07:57:41 +00002783 val = MII_TG3_AUXCTL_PCTL_100TX_LPWR |
2784 MII_TG3_AUXCTL_PCTL_SPR_ISOLATE |
2785 MII_TG3_AUXCTL_PCTL_VREG_11V;
2786 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_PWRCTL, val);
Michael Chan715116a2006-09-27 16:09:25 -07002787 }
Michael Chan3f7045c2006-09-27 16:02:29 -07002788
Michael Chan15c3b692006-03-22 01:06:52 -08002789 /* The PHY should not be powered down on some chips because
2790 * of bugs.
2791 */
2792 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2793 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
2794 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780 &&
Matt Carlson085f1af2012-04-02 09:01:40 +00002795 (tp->phy_flags & TG3_PHYFLG_MII_SERDES)) ||
2796 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 &&
2797 !tp->pci_fn))
Michael Chan15c3b692006-03-22 01:06:52 -08002798 return;
Matt Carlsonce057f02007-11-12 21:08:03 -08002799
Matt Carlsonbcb37f62008-11-03 16:52:09 -08002800 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX ||
2801 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5761_AX) {
Matt Carlsonce057f02007-11-12 21:08:03 -08002802 val = tr32(TG3_CPMU_LSPD_1000MB_CLK);
2803 val &= ~CPMU_LSPD_1000MB_MACCLK_MASK;
2804 val |= CPMU_LSPD_1000MB_MACCLK_12_5;
2805 tw32_f(TG3_CPMU_LSPD_1000MB_CLK, val);
2806 }
2807
Michael Chan15c3b692006-03-22 01:06:52 -08002808 tg3_writephy(tp, MII_BMCR, BMCR_PDOWN);
2809}
2810
Matt Carlson3f007892008-11-03 16:51:36 -08002811/* tp->lock is held. */
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002812static int tg3_nvram_lock(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 int i;
2816
2817 if (tp->nvram_lock_cnt == 0) {
2818 tw32(NVRAM_SWARB, SWARB_REQ_SET1);
2819 for (i = 0; i < 8000; i++) {
2820 if (tr32(NVRAM_SWARB) & SWARB_GNT1)
2821 break;
2822 udelay(20);
2823 }
2824 if (i == 8000) {
2825 tw32(NVRAM_SWARB, SWARB_REQ_CLR1);
2826 return -ENODEV;
2827 }
2828 }
2829 tp->nvram_lock_cnt++;
2830 }
2831 return 0;
2832}
2833
2834/* tp->lock is held. */
2835static void tg3_nvram_unlock(struct tg3 *tp)
2836{
Joe Perches63c3a662011-04-26 08:12:10 +00002837 if (tg3_flag(tp, NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002838 if (tp->nvram_lock_cnt > 0)
2839 tp->nvram_lock_cnt--;
2840 if (tp->nvram_lock_cnt == 0)
2841 tw32_f(NVRAM_SWARB, SWARB_REQ_CLR1);
2842 }
2843}
2844
2845/* tp->lock is held. */
2846static void tg3_enable_nvram_access(struct tg3 *tp)
2847{
Joe Perches63c3a662011-04-26 08:12:10 +00002848 if (tg3_flag(tp, 5750_PLUS) && !tg3_flag(tp, PROTECTED_NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002849 u32 nvaccess = tr32(NVRAM_ACCESS);
2850
2851 tw32(NVRAM_ACCESS, nvaccess | ACCESS_ENABLE);
2852 }
2853}
2854
2855/* tp->lock is held. */
2856static void tg3_disable_nvram_access(struct tg3 *tp)
2857{
Joe Perches63c3a662011-04-26 08:12:10 +00002858 if (tg3_flag(tp, 5750_PLUS) && !tg3_flag(tp, PROTECTED_NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002859 u32 nvaccess = tr32(NVRAM_ACCESS);
2860
2861 tw32(NVRAM_ACCESS, nvaccess & ~ACCESS_ENABLE);
2862 }
2863}
2864
2865static int tg3_nvram_read_using_eeprom(struct tg3 *tp,
2866 u32 offset, u32 *val)
2867{
2868 u32 tmp;
2869 int i;
2870
2871 if (offset > EEPROM_ADDR_ADDR_MASK || (offset % 4) != 0)
2872 return -EINVAL;
2873
2874 tmp = tr32(GRC_EEPROM_ADDR) & ~(EEPROM_ADDR_ADDR_MASK |
2875 EEPROM_ADDR_DEVID_MASK |
2876 EEPROM_ADDR_READ);
2877 tw32(GRC_EEPROM_ADDR,
2878 tmp |
2879 (0 << EEPROM_ADDR_DEVID_SHIFT) |
2880 ((offset << EEPROM_ADDR_ADDR_SHIFT) &
2881 EEPROM_ADDR_ADDR_MASK) |
2882 EEPROM_ADDR_READ | EEPROM_ADDR_START);
2883
2884 for (i = 0; i < 1000; i++) {
2885 tmp = tr32(GRC_EEPROM_ADDR);
2886
2887 if (tmp & EEPROM_ADDR_COMPLETE)
2888 break;
2889 msleep(1);
2890 }
2891 if (!(tmp & EEPROM_ADDR_COMPLETE))
2892 return -EBUSY;
2893
Matt Carlson62cedd12009-04-20 14:52:29 -07002894 tmp = tr32(GRC_EEPROM_DATA);
2895
2896 /*
2897 * The data will always be opposite the native endian
2898 * format. Perform a blind byteswap to compensate.
2899 */
2900 *val = swab32(tmp);
2901
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002902 return 0;
2903}
2904
2905#define NVRAM_CMD_TIMEOUT 10000
2906
2907static int tg3_nvram_exec_cmd(struct tg3 *tp, u32 nvram_cmd)
2908{
2909 int i;
2910
2911 tw32(NVRAM_CMD, nvram_cmd);
2912 for (i = 0; i < NVRAM_CMD_TIMEOUT; i++) {
2913 udelay(10);
2914 if (tr32(NVRAM_CMD) & NVRAM_CMD_DONE) {
2915 udelay(10);
2916 break;
2917 }
2918 }
2919
2920 if (i == NVRAM_CMD_TIMEOUT)
2921 return -EBUSY;
2922
2923 return 0;
2924}
2925
2926static u32 tg3_nvram_phys_addr(struct tg3 *tp, u32 addr)
2927{
Joe Perches63c3a662011-04-26 08:12:10 +00002928 if (tg3_flag(tp, NVRAM) &&
2929 tg3_flag(tp, NVRAM_BUFFERED) &&
2930 tg3_flag(tp, FLASH) &&
2931 !tg3_flag(tp, NO_NVRAM_ADDR_TRANS) &&
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002932 (tp->nvram_jedecnum == JEDEC_ATMEL))
2933
2934 addr = ((addr / tp->nvram_pagesize) <<
2935 ATMEL_AT45DB0X1B_PAGE_POS) +
2936 (addr % tp->nvram_pagesize);
2937
2938 return addr;
2939}
2940
2941static u32 tg3_nvram_logical_addr(struct tg3 *tp, u32 addr)
2942{
Joe Perches63c3a662011-04-26 08:12:10 +00002943 if (tg3_flag(tp, NVRAM) &&
2944 tg3_flag(tp, NVRAM_BUFFERED) &&
2945 tg3_flag(tp, FLASH) &&
2946 !tg3_flag(tp, NO_NVRAM_ADDR_TRANS) &&
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002947 (tp->nvram_jedecnum == JEDEC_ATMEL))
2948
2949 addr = ((addr >> ATMEL_AT45DB0X1B_PAGE_POS) *
2950 tp->nvram_pagesize) +
2951 (addr & ((1 << ATMEL_AT45DB0X1B_PAGE_POS) - 1));
2952
2953 return addr;
2954}
2955
Matt Carlsone4f34112009-02-25 14:25:00 +00002956/* NOTE: Data read in from NVRAM is byteswapped according to
2957 * the byteswapping settings for all other register accesses.
2958 * tg3 devices are BE devices, so on a BE machine, the data
2959 * returned will be exactly as it is seen in NVRAM. On a LE
2960 * machine, the 32-bit value will be byteswapped.
2961 */
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002962static int tg3_nvram_read(struct tg3 *tp, u32 offset, u32 *val)
2963{
2964 int ret;
2965
Joe Perches63c3a662011-04-26 08:12:10 +00002966 if (!tg3_flag(tp, NVRAM))
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002967 return tg3_nvram_read_using_eeprom(tp, offset, val);
2968
2969 offset = tg3_nvram_phys_addr(tp, offset);
2970
2971 if (offset > NVRAM_ADDR_MSK)
2972 return -EINVAL;
2973
2974 ret = tg3_nvram_lock(tp);
2975 if (ret)
2976 return ret;
2977
2978 tg3_enable_nvram_access(tp);
2979
2980 tw32(NVRAM_ADDR, offset);
2981 ret = tg3_nvram_exec_cmd(tp, NVRAM_CMD_RD | NVRAM_CMD_GO |
2982 NVRAM_CMD_FIRST | NVRAM_CMD_LAST | NVRAM_CMD_DONE);
2983
2984 if (ret == 0)
Matt Carlsone4f34112009-02-25 14:25:00 +00002985 *val = tr32(NVRAM_RDDATA);
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002986
2987 tg3_disable_nvram_access(tp);
2988
2989 tg3_nvram_unlock(tp);
2990
2991 return ret;
2992}
2993
Matt Carlsona9dc5292009-02-25 14:25:30 +00002994/* Ensures NVRAM data is in bytestream format. */
2995static int tg3_nvram_read_be32(struct tg3 *tp, u32 offset, __be32 *val)
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002996{
2997 u32 v;
Matt Carlsona9dc5292009-02-25 14:25:30 +00002998 int res = tg3_nvram_read(tp, offset, &v);
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002999 if (!res)
Matt Carlsona9dc5292009-02-25 14:25:30 +00003000 *val = cpu_to_be32(v);
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003001 return res;
3002}
3003
Matt Carlsondbe9b922012-02-13 10:20:09 +00003004static int tg3_nvram_write_block_using_eeprom(struct tg3 *tp,
3005 u32 offset, u32 len, u8 *buf)
3006{
3007 int i, j, rc = 0;
3008 u32 val;
3009
3010 for (i = 0; i < len; i += 4) {
3011 u32 addr;
3012 __be32 data;
3013
3014 addr = offset + i;
3015
3016 memcpy(&data, buf + i, 4);
3017
3018 /*
3019 * The SEEPROM interface expects the data to always be opposite
3020 * the native endian format. We accomplish this by reversing
3021 * all the operations that would have been performed on the
3022 * data from a call to tg3_nvram_read_be32().
3023 */
3024 tw32(GRC_EEPROM_DATA, swab32(be32_to_cpu(data)));
3025
3026 val = tr32(GRC_EEPROM_ADDR);
3027 tw32(GRC_EEPROM_ADDR, val | EEPROM_ADDR_COMPLETE);
3028
3029 val &= ~(EEPROM_ADDR_ADDR_MASK | EEPROM_ADDR_DEVID_MASK |
3030 EEPROM_ADDR_READ);
3031 tw32(GRC_EEPROM_ADDR, val |
3032 (0 << EEPROM_ADDR_DEVID_SHIFT) |
3033 (addr & EEPROM_ADDR_ADDR_MASK) |
3034 EEPROM_ADDR_START |
3035 EEPROM_ADDR_WRITE);
3036
3037 for (j = 0; j < 1000; j++) {
3038 val = tr32(GRC_EEPROM_ADDR);
3039
3040 if (val & EEPROM_ADDR_COMPLETE)
3041 break;
3042 msleep(1);
3043 }
3044 if (!(val & EEPROM_ADDR_COMPLETE)) {
3045 rc = -EBUSY;
3046 break;
3047 }
3048 }
3049
3050 return rc;
3051}
3052
3053/* offset and length are dword aligned */
3054static int tg3_nvram_write_block_unbuffered(struct tg3 *tp, u32 offset, u32 len,
3055 u8 *buf)
3056{
3057 int ret = 0;
3058 u32 pagesize = tp->nvram_pagesize;
3059 u32 pagemask = pagesize - 1;
3060 u32 nvram_cmd;
3061 u8 *tmp;
3062
3063 tmp = kmalloc(pagesize, GFP_KERNEL);
3064 if (tmp == NULL)
3065 return -ENOMEM;
3066
3067 while (len) {
3068 int j;
3069 u32 phy_addr, page_off, size;
3070
3071 phy_addr = offset & ~pagemask;
3072
3073 for (j = 0; j < pagesize; j += 4) {
3074 ret = tg3_nvram_read_be32(tp, phy_addr + j,
3075 (__be32 *) (tmp + j));
3076 if (ret)
3077 break;
3078 }
3079 if (ret)
3080 break;
3081
3082 page_off = offset & pagemask;
3083 size = pagesize;
3084 if (len < size)
3085 size = len;
3086
3087 len -= size;
3088
3089 memcpy(tmp + page_off, buf, size);
3090
3091 offset = offset + (pagesize - page_off);
3092
3093 tg3_enable_nvram_access(tp);
3094
3095 /*
3096 * Before we can erase the flash page, we need
3097 * to issue a special "write enable" command.
3098 */
3099 nvram_cmd = NVRAM_CMD_WREN | NVRAM_CMD_GO | NVRAM_CMD_DONE;
3100
3101 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
3102 break;
3103
3104 /* Erase the target page */
3105 tw32(NVRAM_ADDR, phy_addr);
3106
3107 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE | NVRAM_CMD_WR |
3108 NVRAM_CMD_FIRST | NVRAM_CMD_LAST | NVRAM_CMD_ERASE;
3109
3110 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
3111 break;
3112
3113 /* Issue another write enable to start the write. */
3114 nvram_cmd = NVRAM_CMD_WREN | NVRAM_CMD_GO | NVRAM_CMD_DONE;
3115
3116 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
3117 break;
3118
3119 for (j = 0; j < pagesize; j += 4) {
3120 __be32 data;
3121
3122 data = *((__be32 *) (tmp + j));
3123
3124 tw32(NVRAM_WRDATA, be32_to_cpu(data));
3125
3126 tw32(NVRAM_ADDR, phy_addr + j);
3127
3128 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE |
3129 NVRAM_CMD_WR;
3130
3131 if (j == 0)
3132 nvram_cmd |= NVRAM_CMD_FIRST;
3133 else if (j == (pagesize - 4))
3134 nvram_cmd |= NVRAM_CMD_LAST;
3135
3136 ret = tg3_nvram_exec_cmd(tp, nvram_cmd);
3137 if (ret)
3138 break;
3139 }
3140 if (ret)
3141 break;
3142 }
3143
3144 nvram_cmd = NVRAM_CMD_WRDI | NVRAM_CMD_GO | NVRAM_CMD_DONE;
3145 tg3_nvram_exec_cmd(tp, nvram_cmd);
3146
3147 kfree(tmp);
3148
3149 return ret;
3150}
3151
3152/* offset and length are dword aligned */
3153static int tg3_nvram_write_block_buffered(struct tg3 *tp, u32 offset, u32 len,
3154 u8 *buf)
3155{
3156 int i, ret = 0;
3157
3158 for (i = 0; i < len; i += 4, offset += 4) {
3159 u32 page_off, phy_addr, nvram_cmd;
3160 __be32 data;
3161
3162 memcpy(&data, buf + i, 4);
3163 tw32(NVRAM_WRDATA, be32_to_cpu(data));
3164
3165 page_off = offset % tp->nvram_pagesize;
3166
3167 phy_addr = tg3_nvram_phys_addr(tp, offset);
3168
Matt Carlsondbe9b922012-02-13 10:20:09 +00003169 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE | NVRAM_CMD_WR;
3170
3171 if (page_off == 0 || i == 0)
3172 nvram_cmd |= NVRAM_CMD_FIRST;
3173 if (page_off == (tp->nvram_pagesize - 4))
3174 nvram_cmd |= NVRAM_CMD_LAST;
3175
3176 if (i == (len - 4))
3177 nvram_cmd |= NVRAM_CMD_LAST;
3178
Matt Carlson42278222012-02-13 15:20:11 +00003179 if ((nvram_cmd & NVRAM_CMD_FIRST) ||
3180 !tg3_flag(tp, FLASH) ||
3181 !tg3_flag(tp, 57765_PLUS))
3182 tw32(NVRAM_ADDR, phy_addr);
3183
Matt Carlsondbe9b922012-02-13 10:20:09 +00003184 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5752 &&
3185 !tg3_flag(tp, 5755_PLUS) &&
3186 (tp->nvram_jedecnum == JEDEC_ST) &&
3187 (nvram_cmd & NVRAM_CMD_FIRST)) {
3188 u32 cmd;
3189
3190 cmd = NVRAM_CMD_WREN | NVRAM_CMD_GO | NVRAM_CMD_DONE;
3191 ret = tg3_nvram_exec_cmd(tp, cmd);
3192 if (ret)
3193 break;
3194 }
3195 if (!tg3_flag(tp, FLASH)) {
3196 /* We always do complete word writes to eeprom. */
3197 nvram_cmd |= (NVRAM_CMD_FIRST | NVRAM_CMD_LAST);
3198 }
3199
3200 ret = tg3_nvram_exec_cmd(tp, nvram_cmd);
3201 if (ret)
3202 break;
3203 }
3204 return ret;
3205}
3206
3207/* offset and length are dword aligned */
3208static int tg3_nvram_write_block(struct tg3 *tp, u32 offset, u32 len, u8 *buf)
3209{
3210 int ret;
3211
3212 if (tg3_flag(tp, EEPROM_WRITE_PROT)) {
3213 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl &
3214 ~GRC_LCLCTRL_GPIO_OUTPUT1);
3215 udelay(40);
3216 }
3217
3218 if (!tg3_flag(tp, NVRAM)) {
3219 ret = tg3_nvram_write_block_using_eeprom(tp, offset, len, buf);
3220 } else {
3221 u32 grc_mode;
3222
3223 ret = tg3_nvram_lock(tp);
3224 if (ret)
3225 return ret;
3226
3227 tg3_enable_nvram_access(tp);
3228 if (tg3_flag(tp, 5750_PLUS) && !tg3_flag(tp, PROTECTED_NVRAM))
3229 tw32(NVRAM_WRITE1, 0x406);
3230
3231 grc_mode = tr32(GRC_MODE);
3232 tw32(GRC_MODE, grc_mode | GRC_MODE_NVRAM_WR_ENABLE);
3233
3234 if (tg3_flag(tp, NVRAM_BUFFERED) || !tg3_flag(tp, FLASH)) {
3235 ret = tg3_nvram_write_block_buffered(tp, offset, len,
3236 buf);
3237 } else {
3238 ret = tg3_nvram_write_block_unbuffered(tp, offset, len,
3239 buf);
3240 }
3241
3242 grc_mode = tr32(GRC_MODE);
3243 tw32(GRC_MODE, grc_mode & ~GRC_MODE_NVRAM_WR_ENABLE);
3244
3245 tg3_disable_nvram_access(tp);
3246 tg3_nvram_unlock(tp);
3247 }
3248
3249 if (tg3_flag(tp, EEPROM_WRITE_PROT)) {
3250 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
3251 udelay(40);
3252 }
3253
3254 return ret;
3255}
3256
Matt Carlson997b4f12011-08-31 11:44:53 +00003257#define RX_CPU_SCRATCH_BASE 0x30000
3258#define RX_CPU_SCRATCH_SIZE 0x04000
3259#define TX_CPU_SCRATCH_BASE 0x34000
3260#define TX_CPU_SCRATCH_SIZE 0x04000
3261
3262/* tp->lock is held. */
3263static int tg3_halt_cpu(struct tg3 *tp, u32 offset)
3264{
3265 int i;
3266
3267 BUG_ON(offset == TX_CPU_BASE && tg3_flag(tp, 5705_PLUS));
3268
3269 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
3270 u32 val = tr32(GRC_VCPU_EXT_CTRL);
3271
3272 tw32(GRC_VCPU_EXT_CTRL, val | GRC_VCPU_EXT_CTRL_HALT_CPU);
3273 return 0;
3274 }
3275 if (offset == RX_CPU_BASE) {
3276 for (i = 0; i < 10000; i++) {
3277 tw32(offset + CPU_STATE, 0xffffffff);
3278 tw32(offset + CPU_MODE, CPU_MODE_HALT);
3279 if (tr32(offset + CPU_MODE) & CPU_MODE_HALT)
3280 break;
3281 }
3282
3283 tw32(offset + CPU_STATE, 0xffffffff);
3284 tw32_f(offset + CPU_MODE, CPU_MODE_HALT);
3285 udelay(10);
3286 } else {
3287 for (i = 0; i < 10000; i++) {
3288 tw32(offset + CPU_STATE, 0xffffffff);
3289 tw32(offset + CPU_MODE, CPU_MODE_HALT);
3290 if (tr32(offset + CPU_MODE) & CPU_MODE_HALT)
3291 break;
3292 }
3293 }
3294
3295 if (i >= 10000) {
3296 netdev_err(tp->dev, "%s timed out, %s CPU\n",
3297 __func__, offset == RX_CPU_BASE ? "RX" : "TX");
3298 return -ENODEV;
3299 }
3300
3301 /* Clear firmware's nvram arbitration. */
3302 if (tg3_flag(tp, NVRAM))
3303 tw32(NVRAM_SWARB, SWARB_REQ_CLR0);
3304 return 0;
3305}
3306
3307struct fw_info {
3308 unsigned int fw_base;
3309 unsigned int fw_len;
3310 const __be32 *fw_data;
3311};
3312
3313/* tp->lock is held. */
3314static int tg3_load_firmware_cpu(struct tg3 *tp, u32 cpu_base,
3315 u32 cpu_scratch_base, int cpu_scratch_size,
3316 struct fw_info *info)
3317{
3318 int err, lock_err, i;
3319 void (*write_op)(struct tg3 *, u32, u32);
3320
3321 if (cpu_base == TX_CPU_BASE && tg3_flag(tp, 5705_PLUS)) {
3322 netdev_err(tp->dev,
3323 "%s: Trying to load TX cpu firmware which is 5705\n",
3324 __func__);
3325 return -EINVAL;
3326 }
3327
3328 if (tg3_flag(tp, 5705_PLUS))
3329 write_op = tg3_write_mem;
3330 else
3331 write_op = tg3_write_indirect_reg32;
3332
3333 /* It is possible that bootcode is still loading at this point.
3334 * Get the nvram lock first before halting the cpu.
3335 */
3336 lock_err = tg3_nvram_lock(tp);
3337 err = tg3_halt_cpu(tp, cpu_base);
3338 if (!lock_err)
3339 tg3_nvram_unlock(tp);
3340 if (err)
3341 goto out;
3342
3343 for (i = 0; i < cpu_scratch_size; i += sizeof(u32))
3344 write_op(tp, cpu_scratch_base + i, 0);
3345 tw32(cpu_base + CPU_STATE, 0xffffffff);
3346 tw32(cpu_base + CPU_MODE, tr32(cpu_base+CPU_MODE)|CPU_MODE_HALT);
3347 for (i = 0; i < (info->fw_len / sizeof(u32)); i++)
3348 write_op(tp, (cpu_scratch_base +
3349 (info->fw_base & 0xffff) +
3350 (i * sizeof(u32))),
3351 be32_to_cpu(info->fw_data[i]));
3352
3353 err = 0;
3354
3355out:
3356 return err;
3357}
3358
3359/* tp->lock is held. */
3360static int tg3_load_5701_a0_firmware_fix(struct tg3 *tp)
3361{
3362 struct fw_info info;
3363 const __be32 *fw_data;
3364 int err, i;
3365
3366 fw_data = (void *)tp->fw->data;
3367
3368 /* Firmware blob starts with version numbers, followed by
3369 start address and length. We are setting complete length.
3370 length = end_address_of_bss - start_address_of_text.
3371 Remainder is the blob to be loaded contiguously
3372 from start address. */
3373
3374 info.fw_base = be32_to_cpu(fw_data[1]);
3375 info.fw_len = tp->fw->size - 12;
3376 info.fw_data = &fw_data[3];
3377
3378 err = tg3_load_firmware_cpu(tp, RX_CPU_BASE,
3379 RX_CPU_SCRATCH_BASE, RX_CPU_SCRATCH_SIZE,
3380 &info);
3381 if (err)
3382 return err;
3383
3384 err = tg3_load_firmware_cpu(tp, TX_CPU_BASE,
3385 TX_CPU_SCRATCH_BASE, TX_CPU_SCRATCH_SIZE,
3386 &info);
3387 if (err)
3388 return err;
3389
3390 /* Now startup only the RX cpu. */
3391 tw32(RX_CPU_BASE + CPU_STATE, 0xffffffff);
3392 tw32_f(RX_CPU_BASE + CPU_PC, info.fw_base);
3393
3394 for (i = 0; i < 5; i++) {
3395 if (tr32(RX_CPU_BASE + CPU_PC) == info.fw_base)
3396 break;
3397 tw32(RX_CPU_BASE + CPU_STATE, 0xffffffff);
3398 tw32(RX_CPU_BASE + CPU_MODE, CPU_MODE_HALT);
3399 tw32_f(RX_CPU_BASE + CPU_PC, info.fw_base);
3400 udelay(1000);
3401 }
3402 if (i >= 5) {
3403 netdev_err(tp->dev, "%s fails to set RX CPU PC, is %08x "
3404 "should be %08x\n", __func__,
3405 tr32(RX_CPU_BASE + CPU_PC), info.fw_base);
3406 return -ENODEV;
3407 }
3408 tw32(RX_CPU_BASE + CPU_STATE, 0xffffffff);
3409 tw32_f(RX_CPU_BASE + CPU_MODE, 0x00000000);
3410
3411 return 0;
3412}
3413
3414/* tp->lock is held. */
3415static int tg3_load_tso_firmware(struct tg3 *tp)
3416{
3417 struct fw_info info;
3418 const __be32 *fw_data;
3419 unsigned long cpu_base, cpu_scratch_base, cpu_scratch_size;
3420 int err, i;
3421
3422 if (tg3_flag(tp, HW_TSO_1) ||
3423 tg3_flag(tp, HW_TSO_2) ||
3424 tg3_flag(tp, HW_TSO_3))
3425 return 0;
3426
3427 fw_data = (void *)tp->fw->data;
3428
3429 /* Firmware blob starts with version numbers, followed by
3430 start address and length. We are setting complete length.
3431 length = end_address_of_bss - start_address_of_text.
3432 Remainder is the blob to be loaded contiguously
3433 from start address. */
3434
3435 info.fw_base = be32_to_cpu(fw_data[1]);
3436 cpu_scratch_size = tp->fw_len;
3437 info.fw_len = tp->fw->size - 12;
3438 info.fw_data = &fw_data[3];
3439
3440 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
3441 cpu_base = RX_CPU_BASE;
3442 cpu_scratch_base = NIC_SRAM_MBUF_POOL_BASE5705;
3443 } else {
3444 cpu_base = TX_CPU_BASE;
3445 cpu_scratch_base = TX_CPU_SCRATCH_BASE;
3446 cpu_scratch_size = TX_CPU_SCRATCH_SIZE;
3447 }
3448
3449 err = tg3_load_firmware_cpu(tp, cpu_base,
3450 cpu_scratch_base, cpu_scratch_size,
3451 &info);
3452 if (err)
3453 return err;
3454
3455 /* Now startup the cpu. */
3456 tw32(cpu_base + CPU_STATE, 0xffffffff);
3457 tw32_f(cpu_base + CPU_PC, info.fw_base);
3458
3459 for (i = 0; i < 5; i++) {
3460 if (tr32(cpu_base + CPU_PC) == info.fw_base)
3461 break;
3462 tw32(cpu_base + CPU_STATE, 0xffffffff);
3463 tw32(cpu_base + CPU_MODE, CPU_MODE_HALT);
3464 tw32_f(cpu_base + CPU_PC, info.fw_base);
3465 udelay(1000);
3466 }
3467 if (i >= 5) {
3468 netdev_err(tp->dev,
3469 "%s fails to set CPU PC, is %08x should be %08x\n",
3470 __func__, tr32(cpu_base + CPU_PC), info.fw_base);
3471 return -ENODEV;
3472 }
3473 tw32(cpu_base + CPU_STATE, 0xffffffff);
3474 tw32_f(cpu_base + CPU_MODE, 0x00000000);
3475 return 0;
3476}
3477
3478
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003479/* tp->lock is held. */
Matt Carlson3f007892008-11-03 16:51:36 -08003480static void __tg3_set_mac_addr(struct tg3 *tp, int skip_mac_1)
3481{
3482 u32 addr_high, addr_low;
3483 int i;
3484
3485 addr_high = ((tp->dev->dev_addr[0] << 8) |
3486 tp->dev->dev_addr[1]);
3487 addr_low = ((tp->dev->dev_addr[2] << 24) |
3488 (tp->dev->dev_addr[3] << 16) |
3489 (tp->dev->dev_addr[4] << 8) |
3490 (tp->dev->dev_addr[5] << 0));
3491 for (i = 0; i < 4; i++) {
3492 if (i == 1 && skip_mac_1)
3493 continue;
3494 tw32(MAC_ADDR_0_HIGH + (i * 8), addr_high);
3495 tw32(MAC_ADDR_0_LOW + (i * 8), addr_low);
3496 }
3497
3498 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
3499 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
3500 for (i = 0; i < 12; i++) {
3501 tw32(MAC_EXTADDR_0_HIGH + (i * 8), addr_high);
3502 tw32(MAC_EXTADDR_0_LOW + (i * 8), addr_low);
3503 }
3504 }
3505
3506 addr_high = (tp->dev->dev_addr[0] +
3507 tp->dev->dev_addr[1] +
3508 tp->dev->dev_addr[2] +
3509 tp->dev->dev_addr[3] +
3510 tp->dev->dev_addr[4] +
3511 tp->dev->dev_addr[5]) &
3512 TX_BACKOFF_SEED_MASK;
3513 tw32(MAC_TX_BACKOFF_SEED, addr_high);
3514}
3515
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003516static void tg3_enable_register_access(struct tg3 *tp)
3517{
3518 /*
3519 * Make sure register accesses (indirect or otherwise) will function
3520 * correctly.
3521 */
3522 pci_write_config_dword(tp->pdev,
3523 TG3PCI_MISC_HOST_CTRL, tp->misc_host_ctrl);
3524}
3525
3526static int tg3_power_up(struct tg3 *tp)
3527{
Matt Carlsonbed98292011-07-13 09:27:29 +00003528 int err;
3529
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003530 tg3_enable_register_access(tp);
3531
Matt Carlsonbed98292011-07-13 09:27:29 +00003532 err = pci_set_power_state(tp->pdev, PCI_D0);
3533 if (!err) {
3534 /* Switch out of Vaux if it is a NIC */
3535 tg3_pwrsrc_switch_to_vmain(tp);
3536 } else {
3537 netdev_err(tp->dev, "Transition to D0 failed\n");
3538 }
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003539
Matt Carlsonbed98292011-07-13 09:27:29 +00003540 return err;
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003541}
3542
Matt Carlson4b409522012-02-13 10:20:11 +00003543static int tg3_setup_phy(struct tg3 *, int);
3544
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003545static int tg3_power_down_prepare(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003546{
3547 u32 misc_host_ctrl;
Matt Carlson0a459aa2008-11-03 16:54:15 -08003548 bool device_should_wake, do_low_power;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003549
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003550 tg3_enable_register_access(tp);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08003551
3552 /* Restore the CLKREQ setting. */
Joe Perches63c3a662011-04-26 08:12:10 +00003553 if (tg3_flag(tp, CLKREQ_BUG)) {
Matt Carlson5e7dfd02008-11-21 17:18:16 -08003554 u16 lnkctl;
3555
3556 pci_read_config_word(tp->pdev,
Jon Mason708ebb3a2011-06-27 12:56:50 +00003557 pci_pcie_cap(tp->pdev) + PCI_EXP_LNKCTL,
Matt Carlson5e7dfd02008-11-21 17:18:16 -08003558 &lnkctl);
3559 lnkctl |= PCI_EXP_LNKCTL_CLKREQ_EN;
3560 pci_write_config_word(tp->pdev,
Jon Mason708ebb3a2011-06-27 12:56:50 +00003561 pci_pcie_cap(tp->pdev) + PCI_EXP_LNKCTL,
Matt Carlson5e7dfd02008-11-21 17:18:16 -08003562 lnkctl);
3563 }
3564
Linus Torvalds1da177e2005-04-16 15:20:36 -07003565 misc_host_ctrl = tr32(TG3PCI_MISC_HOST_CTRL);
3566 tw32(TG3PCI_MISC_HOST_CTRL,
3567 misc_host_ctrl | MISC_HOST_CTRL_MASK_PCI_INT);
3568
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003569 device_should_wake = device_may_wakeup(&tp->pdev->dev) &&
Joe Perches63c3a662011-04-26 08:12:10 +00003570 tg3_flag(tp, WOL_ENABLE);
Matt Carlson05ac4cb2008-11-03 16:53:46 -08003571
Joe Perches63c3a662011-04-26 08:12:10 +00003572 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson0a459aa2008-11-03 16:54:15 -08003573 do_low_power = false;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003574 if ((tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) &&
Matt Carlson80096062010-08-02 11:26:06 +00003575 !(tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07003576 struct phy_device *phydev;
Matt Carlson0a459aa2008-11-03 16:54:15 -08003577 u32 phyid, advertising;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07003578
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00003579 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07003580
Matt Carlson80096062010-08-02 11:26:06 +00003581 tp->phy_flags |= TG3_PHYFLG_IS_LOW_POWER;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07003582
Matt Carlsonc6700ce2012-02-13 15:20:15 +00003583 tp->link_config.speed = phydev->speed;
3584 tp->link_config.duplex = phydev->duplex;
3585 tp->link_config.autoneg = phydev->autoneg;
3586 tp->link_config.advertising = phydev->advertising;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07003587
3588 advertising = ADVERTISED_TP |
3589 ADVERTISED_Pause |
3590 ADVERTISED_Autoneg |
3591 ADVERTISED_10baseT_Half;
3592
Joe Perches63c3a662011-04-26 08:12:10 +00003593 if (tg3_flag(tp, ENABLE_ASF) || device_should_wake) {
3594 if (tg3_flag(tp, WOL_SPEED_100MB))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07003595 advertising |=
3596 ADVERTISED_100baseT_Half |
3597 ADVERTISED_100baseT_Full |
3598 ADVERTISED_10baseT_Full;
3599 else
3600 advertising |= ADVERTISED_10baseT_Full;
3601 }
3602
3603 phydev->advertising = advertising;
3604
3605 phy_start_aneg(phydev);
Matt Carlson0a459aa2008-11-03 16:54:15 -08003606
3607 phyid = phydev->drv->phy_id & phydev->drv->phy_id_mask;
Matt Carlson6a443a02010-02-17 15:17:04 +00003608 if (phyid != PHY_ID_BCMAC131) {
3609 phyid &= PHY_BCM_OUI_MASK;
3610 if (phyid == PHY_BCM_OUI_1 ||
3611 phyid == PHY_BCM_OUI_2 ||
3612 phyid == PHY_BCM_OUI_3)
Matt Carlson0a459aa2008-11-03 16:54:15 -08003613 do_low_power = true;
3614 }
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07003615 }
Matt Carlsondd477002008-05-25 23:45:58 -07003616 } else {
Matt Carlson20232762008-12-21 20:18:56 -08003617 do_low_power = true;
Matt Carlson0a459aa2008-11-03 16:54:15 -08003618
Matt Carlsonc6700ce2012-02-13 15:20:15 +00003619 if (!(tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER))
Matt Carlson80096062010-08-02 11:26:06 +00003620 tp->phy_flags |= TG3_PHYFLG_IS_LOW_POWER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003621
Matt Carlson2855b9f2012-02-13 15:20:14 +00003622 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
Matt Carlsondd477002008-05-25 23:45:58 -07003623 tg3_setup_phy(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003624 }
3625
Michael Chanb5d37722006-09-27 16:06:21 -07003626 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
3627 u32 val;
3628
3629 val = tr32(GRC_VCPU_EXT_CTRL);
3630 tw32(GRC_VCPU_EXT_CTRL, val | GRC_VCPU_EXT_CTRL_DISABLE_WOL);
Joe Perches63c3a662011-04-26 08:12:10 +00003631 } else if (!tg3_flag(tp, ENABLE_ASF)) {
Michael Chan6921d202005-12-13 21:15:53 -08003632 int i;
3633 u32 val;
3634
3635 for (i = 0; i < 200; i++) {
3636 tg3_read_mem(tp, NIC_SRAM_FW_ASF_STATUS_MBOX, &val);
3637 if (val == ~NIC_SRAM_FIRMWARE_MBOX_MAGIC1)
3638 break;
3639 msleep(1);
3640 }
3641 }
Joe Perches63c3a662011-04-26 08:12:10 +00003642 if (tg3_flag(tp, WOL_CAP))
Gary Zambranoa85feb82007-05-05 11:52:19 -07003643 tg3_write_mem(tp, NIC_SRAM_WOL_MBOX, WOL_SIGNATURE |
3644 WOL_DRV_STATE_SHUTDOWN |
3645 WOL_DRV_WOL |
3646 WOL_SET_MAGIC_PKT);
Michael Chan6921d202005-12-13 21:15:53 -08003647
Matt Carlson05ac4cb2008-11-03 16:53:46 -08003648 if (device_should_wake) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003649 u32 mac_mode;
3650
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003651 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES)) {
Matt Carlsonb4bd2922011-04-20 07:57:41 +00003652 if (do_low_power &&
3653 !(tp->phy_flags & TG3_PHYFLG_IS_FET)) {
3654 tg3_phy_auxctl_write(tp,
3655 MII_TG3_AUXCTL_SHDWSEL_PWRCTL,
3656 MII_TG3_AUXCTL_PCTL_WOL_EN |
3657 MII_TG3_AUXCTL_PCTL_100TX_LPWR |
3658 MII_TG3_AUXCTL_PCTL_CL_AB_TXDAC);
Matt Carlsondd477002008-05-25 23:45:58 -07003659 udelay(40);
3660 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003661
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003662 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
Michael Chan3f7045c2006-09-27 16:02:29 -07003663 mac_mode = MAC_MODE_PORT_MODE_GMII;
3664 else
3665 mac_mode = MAC_MODE_PORT_MODE_MII;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003666
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07003667 mac_mode |= tp->mac_mode & MAC_MODE_LINK_POLARITY;
3668 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
3669 ASIC_REV_5700) {
Joe Perches63c3a662011-04-26 08:12:10 +00003670 u32 speed = tg3_flag(tp, WOL_SPEED_100MB) ?
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07003671 SPEED_100 : SPEED_10;
3672 if (tg3_5700_link_polarity(tp, speed))
3673 mac_mode |= MAC_MODE_LINK_POLARITY;
3674 else
3675 mac_mode &= ~MAC_MODE_LINK_POLARITY;
3676 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003677 } else {
3678 mac_mode = MAC_MODE_PORT_MODE_TBI;
3679 }
3680
Joe Perches63c3a662011-04-26 08:12:10 +00003681 if (!tg3_flag(tp, 5750_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003682 tw32(MAC_LED_CTRL, tp->led_ctrl);
3683
Matt Carlson05ac4cb2008-11-03 16:53:46 -08003684 mac_mode |= MAC_MODE_MAGIC_PKT_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00003685 if ((tg3_flag(tp, 5705_PLUS) && !tg3_flag(tp, 5780_CLASS)) &&
3686 (tg3_flag(tp, ENABLE_ASF) || tg3_flag(tp, ENABLE_APE)))
Matt Carlson05ac4cb2008-11-03 16:53:46 -08003687 mac_mode |= MAC_MODE_KEEP_FRAME_IN_WOL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003688
Joe Perches63c3a662011-04-26 08:12:10 +00003689 if (tg3_flag(tp, ENABLE_APE))
Matt Carlsond2394e6b2010-11-24 08:31:47 +00003690 mac_mode |= MAC_MODE_APE_TX_EN |
3691 MAC_MODE_APE_RX_EN |
3692 MAC_MODE_TDE_ENABLE;
Matt Carlson3bda1252008-08-15 14:08:22 -07003693
Linus Torvalds1da177e2005-04-16 15:20:36 -07003694 tw32_f(MAC_MODE, mac_mode);
3695 udelay(100);
3696
3697 tw32_f(MAC_RX_MODE, RX_MODE_ENABLE);
3698 udelay(10);
3699 }
3700
Joe Perches63c3a662011-04-26 08:12:10 +00003701 if (!tg3_flag(tp, WOL_SPEED_100MB) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003702 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
3703 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)) {
3704 u32 base_val;
3705
3706 base_val = tp->pci_clock_ctrl;
3707 base_val |= (CLOCK_CTRL_RXCLK_DISABLE |
3708 CLOCK_CTRL_TXCLK_DISABLE);
3709
Michael Chanb401e9e2005-12-19 16:27:04 -08003710 tw32_wait_f(TG3PCI_CLOCK_CTRL, base_val | CLOCK_CTRL_ALTCLK |
3711 CLOCK_CTRL_PWRDOWN_PLL133, 40);
Joe Perches63c3a662011-04-26 08:12:10 +00003712 } else if (tg3_flag(tp, 5780_CLASS) ||
3713 tg3_flag(tp, CPMU_PRESENT) ||
Matt Carlson6ff6f812011-05-19 12:12:54 +00003714 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan4cf78e42005-07-25 12:29:19 -07003715 /* do nothing */
Joe Perches63c3a662011-04-26 08:12:10 +00003716 } else if (!(tg3_flag(tp, 5750_PLUS) && tg3_flag(tp, ENABLE_ASF))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003717 u32 newbits1, newbits2;
3718
3719 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
3720 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
3721 newbits1 = (CLOCK_CTRL_RXCLK_DISABLE |
3722 CLOCK_CTRL_TXCLK_DISABLE |
3723 CLOCK_CTRL_ALTCLK);
3724 newbits2 = newbits1 | CLOCK_CTRL_44MHZ_CORE;
Joe Perches63c3a662011-04-26 08:12:10 +00003725 } else if (tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003726 newbits1 = CLOCK_CTRL_625_CORE;
3727 newbits2 = newbits1 | CLOCK_CTRL_ALTCLK;
3728 } else {
3729 newbits1 = CLOCK_CTRL_ALTCLK;
3730 newbits2 = newbits1 | CLOCK_CTRL_44MHZ_CORE;
3731 }
3732
Michael Chanb401e9e2005-12-19 16:27:04 -08003733 tw32_wait_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl | newbits1,
3734 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003735
Michael Chanb401e9e2005-12-19 16:27:04 -08003736 tw32_wait_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl | newbits2,
3737 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003738
Joe Perches63c3a662011-04-26 08:12:10 +00003739 if (!tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003740 u32 newbits3;
3741
3742 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
3743 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
3744 newbits3 = (CLOCK_CTRL_RXCLK_DISABLE |
3745 CLOCK_CTRL_TXCLK_DISABLE |
3746 CLOCK_CTRL_44MHZ_CORE);
3747 } else {
3748 newbits3 = CLOCK_CTRL_44MHZ_CORE;
3749 }
3750
Michael Chanb401e9e2005-12-19 16:27:04 -08003751 tw32_wait_f(TG3PCI_CLOCK_CTRL,
3752 tp->pci_clock_ctrl | newbits3, 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003753 }
3754 }
3755
Joe Perches63c3a662011-04-26 08:12:10 +00003756 if (!(device_should_wake) && !tg3_flag(tp, ENABLE_ASF))
Matt Carlson0a459aa2008-11-03 16:54:15 -08003757 tg3_power_down_phy(tp, do_low_power);
Michael Chan6921d202005-12-13 21:15:53 -08003758
Matt Carlsoncd0d7222011-07-13 09:27:33 +00003759 tg3_frob_aux_power(tp, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003760
3761 /* Workaround for unstable PLL clock */
3762 if ((GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_AX) ||
3763 (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_BX)) {
3764 u32 val = tr32(0x7d00);
3765
3766 val &= ~((1 << 16) | (1 << 4) | (1 << 2) | (1 << 1) | 1);
3767 tw32(0x7d00, val);
Joe Perches63c3a662011-04-26 08:12:10 +00003768 if (!tg3_flag(tp, ENABLE_ASF)) {
Michael Chanec41c7d2006-01-17 02:40:55 -08003769 int err;
3770
3771 err = tg3_nvram_lock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003772 tg3_halt_cpu(tp, RX_CPU_BASE);
Michael Chanec41c7d2006-01-17 02:40:55 -08003773 if (!err)
3774 tg3_nvram_unlock(tp);
Michael Chan6921d202005-12-13 21:15:53 -08003775 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003776 }
3777
Michael Chanbbadf502006-04-06 21:46:34 -07003778 tg3_write_sig_post_reset(tp, RESET_KIND_SHUTDOWN);
3779
Linus Torvalds1da177e2005-04-16 15:20:36 -07003780 return 0;
3781}
3782
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003783static void tg3_power_down(struct tg3 *tp)
3784{
3785 tg3_power_down_prepare(tp);
3786
Joe Perches63c3a662011-04-26 08:12:10 +00003787 pci_wake_from_d3(tp->pdev, tg3_flag(tp, WOL_ENABLE));
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003788 pci_set_power_state(tp->pdev, PCI_D3hot);
3789}
3790
Linus Torvalds1da177e2005-04-16 15:20:36 -07003791static void tg3_aux_stat_to_speed_duplex(struct tg3 *tp, u32 val, u16 *speed, u8 *duplex)
3792{
3793 switch (val & MII_TG3_AUX_STAT_SPDMASK) {
3794 case MII_TG3_AUX_STAT_10HALF:
3795 *speed = SPEED_10;
3796 *duplex = DUPLEX_HALF;
3797 break;
3798
3799 case MII_TG3_AUX_STAT_10FULL:
3800 *speed = SPEED_10;
3801 *duplex = DUPLEX_FULL;
3802 break;
3803
3804 case MII_TG3_AUX_STAT_100HALF:
3805 *speed = SPEED_100;
3806 *duplex = DUPLEX_HALF;
3807 break;
3808
3809 case MII_TG3_AUX_STAT_100FULL:
3810 *speed = SPEED_100;
3811 *duplex = DUPLEX_FULL;
3812 break;
3813
3814 case MII_TG3_AUX_STAT_1000HALF:
3815 *speed = SPEED_1000;
3816 *duplex = DUPLEX_HALF;
3817 break;
3818
3819 case MII_TG3_AUX_STAT_1000FULL:
3820 *speed = SPEED_1000;
3821 *duplex = DUPLEX_FULL;
3822 break;
3823
3824 default:
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003825 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Michael Chan715116a2006-09-27 16:09:25 -07003826 *speed = (val & MII_TG3_AUX_STAT_100) ? SPEED_100 :
3827 SPEED_10;
3828 *duplex = (val & MII_TG3_AUX_STAT_FULL) ? DUPLEX_FULL :
3829 DUPLEX_HALF;
3830 break;
3831 }
Matt Carlsone7405222012-02-13 15:20:16 +00003832 *speed = SPEED_UNKNOWN;
3833 *duplex = DUPLEX_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003834 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07003835 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003836}
3837
Matt Carlson42b64a42011-05-19 12:12:49 +00003838static int tg3_phy_autoneg_cfg(struct tg3 *tp, u32 advertise, u32 flowctrl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003839{
Matt Carlson42b64a42011-05-19 12:12:49 +00003840 int err = 0;
3841 u32 val, new_adv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003842
Matt Carlson42b64a42011-05-19 12:12:49 +00003843 new_adv = ADVERTISE_CSMA;
Hiroaki SHIMODA202ff1c2011-11-22 04:05:41 +00003844 new_adv |= ethtool_adv_to_mii_adv_t(advertise) & ADVERTISE_ALL;
Matt Carlsonf88788f2011-12-14 11:10:00 +00003845 new_adv |= mii_advertise_flowctrl(flowctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003846
Matt Carlson42b64a42011-05-19 12:12:49 +00003847 err = tg3_writephy(tp, MII_ADVERTISE, new_adv);
3848 if (err)
3849 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003850
Matt Carlson4f272092011-12-14 11:09:57 +00003851 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
3852 new_adv = ethtool_adv_to_mii_ctrl1000_t(advertise);
Matt Carlsonba4d07a2007-12-20 20:08:00 -08003853
Matt Carlson4f272092011-12-14 11:09:57 +00003854 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
3855 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0)
3856 new_adv |= CTL1000_AS_MASTER | CTL1000_ENABLE_MASTER;
Matt Carlsonba4d07a2007-12-20 20:08:00 -08003857
Matt Carlson4f272092011-12-14 11:09:57 +00003858 err = tg3_writephy(tp, MII_CTRL1000, new_adv);
3859 if (err)
3860 goto done;
3861 }
Matt Carlsonba4d07a2007-12-20 20:08:00 -08003862
Matt Carlson42b64a42011-05-19 12:12:49 +00003863 if (!(tp->phy_flags & TG3_PHYFLG_EEE_CAP))
3864 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003865
Matt Carlson42b64a42011-05-19 12:12:49 +00003866 tw32(TG3_CPMU_EEE_MODE,
3867 tr32(TG3_CPMU_EEE_MODE) & ~TG3_CPMU_EEEMD_LPI_ENABLE);
Matt Carlsonba4d07a2007-12-20 20:08:00 -08003868
Matt Carlson42b64a42011-05-19 12:12:49 +00003869 err = TG3_PHY_AUXCTL_SMDSP_ENABLE(tp);
3870 if (!err) {
3871 u32 err2;
Matt Carlson52b02d02010-10-14 10:37:41 +00003872
Matt Carlsona6b68da2010-12-06 08:28:52 +00003873 val = 0;
Matt Carlson42b64a42011-05-19 12:12:49 +00003874 /* Advertise 100-BaseTX EEE ability */
3875 if (advertise & ADVERTISED_100baseT_Full)
3876 val |= MDIO_AN_EEE_ADV_100TX;
3877 /* Advertise 1000-BaseT EEE ability */
3878 if (advertise & ADVERTISED_1000baseT_Full)
3879 val |= MDIO_AN_EEE_ADV_1000T;
3880 err = tg3_phy_cl45_write(tp, MDIO_MMD_AN, MDIO_AN_EEE_ADV, val);
Matt Carlsonb715ce92011-07-20 10:20:52 +00003881 if (err)
3882 val = 0;
3883
3884 switch (GET_ASIC_REV(tp->pci_chip_rev_id)) {
3885 case ASIC_REV_5717:
3886 case ASIC_REV_57765:
Matt Carlson55086ad2011-12-14 11:09:59 +00003887 case ASIC_REV_57766:
Matt Carlsonb715ce92011-07-20 10:20:52 +00003888 case ASIC_REV_5719:
3889 /* If we advertised any eee advertisements above... */
3890 if (val)
3891 val = MII_TG3_DSP_TAP26_ALNOKO |
3892 MII_TG3_DSP_TAP26_RMRXSTO |
3893 MII_TG3_DSP_TAP26_OPCSINPT;
3894 tg3_phydsp_write(tp, MII_TG3_DSP_TAP26, val);
3895 /* Fall through */
3896 case ASIC_REV_5720:
3897 if (!tg3_phydsp_read(tp, MII_TG3_DSP_CH34TP2, &val))
3898 tg3_phydsp_write(tp, MII_TG3_DSP_CH34TP2, val |
3899 MII_TG3_DSP_CH34TP2_HIBW01);
3900 }
Matt Carlson52b02d02010-10-14 10:37:41 +00003901
Matt Carlson42b64a42011-05-19 12:12:49 +00003902 err2 = TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
3903 if (!err)
3904 err = err2;
3905 }
3906
3907done:
3908 return err;
3909}
3910
3911static void tg3_phy_copper_begin(struct tg3 *tp)
3912{
Matt Carlsond13ba512012-02-22 12:35:19 +00003913 if (tp->link_config.autoneg == AUTONEG_ENABLE ||
3914 (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)) {
3915 u32 adv, fc;
Matt Carlson42b64a42011-05-19 12:12:49 +00003916
Matt Carlsond13ba512012-02-22 12:35:19 +00003917 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) {
3918 adv = ADVERTISED_10baseT_Half |
3919 ADVERTISED_10baseT_Full;
3920 if (tg3_flag(tp, WOL_SPEED_100MB))
3921 adv |= ADVERTISED_100baseT_Half |
3922 ADVERTISED_100baseT_Full;
Matt Carlson42b64a42011-05-19 12:12:49 +00003923
Matt Carlsond13ba512012-02-22 12:35:19 +00003924 fc = FLOW_CTRL_TX | FLOW_CTRL_RX;
Matt Carlson42b64a42011-05-19 12:12:49 +00003925 } else {
Matt Carlsond13ba512012-02-22 12:35:19 +00003926 adv = tp->link_config.advertising;
3927 if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
3928 adv &= ~(ADVERTISED_1000baseT_Half |
3929 ADVERTISED_1000baseT_Full);
3930
3931 fc = tp->link_config.flowctrl;
Matt Carlson42b64a42011-05-19 12:12:49 +00003932 }
3933
Matt Carlsond13ba512012-02-22 12:35:19 +00003934 tg3_phy_autoneg_cfg(tp, adv, fc);
Matt Carlson52b02d02010-10-14 10:37:41 +00003935
Matt Carlsond13ba512012-02-22 12:35:19 +00003936 tg3_writephy(tp, MII_BMCR,
3937 BMCR_ANENABLE | BMCR_ANRESTART);
3938 } else {
3939 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003940 u32 bmcr, orig_bmcr;
3941
3942 tp->link_config.active_speed = tp->link_config.speed;
3943 tp->link_config.active_duplex = tp->link_config.duplex;
3944
3945 bmcr = 0;
3946 switch (tp->link_config.speed) {
3947 default:
3948 case SPEED_10:
3949 break;
3950
3951 case SPEED_100:
3952 bmcr |= BMCR_SPEED100;
3953 break;
3954
3955 case SPEED_1000:
Matt Carlson221c5632011-06-13 13:39:01 +00003956 bmcr |= BMCR_SPEED1000;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003957 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07003958 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003959
3960 if (tp->link_config.duplex == DUPLEX_FULL)
3961 bmcr |= BMCR_FULLDPLX;
3962
3963 if (!tg3_readphy(tp, MII_BMCR, &orig_bmcr) &&
3964 (bmcr != orig_bmcr)) {
3965 tg3_writephy(tp, MII_BMCR, BMCR_LOOPBACK);
3966 for (i = 0; i < 1500; i++) {
3967 u32 tmp;
3968
3969 udelay(10);
3970 if (tg3_readphy(tp, MII_BMSR, &tmp) ||
3971 tg3_readphy(tp, MII_BMSR, &tmp))
3972 continue;
3973 if (!(tmp & BMSR_LSTATUS)) {
3974 udelay(40);
3975 break;
3976 }
3977 }
3978 tg3_writephy(tp, MII_BMCR, bmcr);
3979 udelay(40);
3980 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003981 }
3982}
3983
3984static int tg3_init_5401phy_dsp(struct tg3 *tp)
3985{
3986 int err;
3987
3988 /* Turn off tap power management. */
3989 /* Set Extended packet length bit */
Matt Carlsonb4bd2922011-04-20 07:57:41 +00003990 err = tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_AUXCTL, 0x4c20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003991
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00003992 err |= tg3_phydsp_write(tp, 0x0012, 0x1804);
3993 err |= tg3_phydsp_write(tp, 0x0013, 0x1204);
3994 err |= tg3_phydsp_write(tp, 0x8006, 0x0132);
3995 err |= tg3_phydsp_write(tp, 0x8006, 0x0232);
3996 err |= tg3_phydsp_write(tp, 0x201f, 0x0a20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003997
3998 udelay(40);
3999
4000 return err;
4001}
4002
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004003static bool tg3_phy_copper_an_config_ok(struct tg3 *tp, u32 *lcladv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004004{
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004005 u32 advmsk, tgtadv, advertising;
Michael Chan3600d912006-12-07 00:21:48 -08004006
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004007 advertising = tp->link_config.advertising;
4008 tgtadv = ethtool_adv_to_mii_adv_t(advertising) & ADVERTISE_ALL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004009
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004010 advmsk = ADVERTISE_ALL;
4011 if (tp->link_config.active_duplex == DUPLEX_FULL) {
Matt Carlsonf88788f2011-12-14 11:10:00 +00004012 tgtadv |= mii_advertise_flowctrl(tp->link_config.flowctrl);
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004013 advmsk |= ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
4014 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004015
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004016 if (tg3_readphy(tp, MII_ADVERTISE, lcladv))
4017 return false;
4018
4019 if ((*lcladv & advmsk) != tgtadv)
4020 return false;
Matt Carlsonb99d2a52011-08-31 11:44:47 +00004021
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004022 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004023 u32 tg3_ctrl;
4024
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004025 tgtadv = ethtool_adv_to_mii_ctrl1000_t(advertising);
Michael Chan3600d912006-12-07 00:21:48 -08004026
Matt Carlson221c5632011-06-13 13:39:01 +00004027 if (tg3_readphy(tp, MII_CTRL1000, &tg3_ctrl))
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004028 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004029
Matt Carlson3198e072012-02-13 15:20:10 +00004030 if (tgtadv &&
4031 (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
4032 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0)) {
4033 tgtadv |= CTL1000_AS_MASTER | CTL1000_ENABLE_MASTER;
4034 tg3_ctrl &= (ADVERTISE_1000HALF | ADVERTISE_1000FULL |
4035 CTL1000_AS_MASTER | CTL1000_ENABLE_MASTER);
4036 } else {
4037 tg3_ctrl &= (ADVERTISE_1000HALF | ADVERTISE_1000FULL);
4038 }
4039
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004040 if (tg3_ctrl != tgtadv)
4041 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004042 }
Matt Carlson93a700a2011-08-31 11:44:54 +00004043
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004044 return true;
Matt Carlsonef167e22007-12-20 20:10:01 -08004045}
4046
Matt Carlson859edb22011-12-08 14:40:16 +00004047static bool tg3_phy_copper_fetch_rmtadv(struct tg3 *tp, u32 *rmtadv)
4048{
4049 u32 lpeth = 0;
4050
4051 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
4052 u32 val;
4053
4054 if (tg3_readphy(tp, MII_STAT1000, &val))
4055 return false;
4056
4057 lpeth = mii_stat1000_to_ethtool_lpa_t(val);
4058 }
4059
4060 if (tg3_readphy(tp, MII_LPA, rmtadv))
4061 return false;
4062
4063 lpeth |= mii_lpa_to_ethtool_lpa_t(*rmtadv);
4064 tp->link_config.rmt_adv = lpeth;
4065
4066 return true;
4067}
4068
Linus Torvalds1da177e2005-04-16 15:20:36 -07004069static int tg3_setup_copper_phy(struct tg3 *tp, int force_reset)
4070{
4071 int current_link_up;
Matt Carlsonf833c4c2010-09-15 09:00:01 +00004072 u32 bmsr, val;
Matt Carlsonef167e22007-12-20 20:10:01 -08004073 u32 lcl_adv, rmt_adv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004074 u16 current_speed;
4075 u8 current_duplex;
4076 int i, err;
4077
4078 tw32(MAC_EVENT, 0);
4079
4080 tw32_f(MAC_STATUS,
4081 (MAC_STATUS_SYNC_CHANGED |
4082 MAC_STATUS_CFG_CHANGED |
4083 MAC_STATUS_MI_COMPLETION |
4084 MAC_STATUS_LNKSTATE_CHANGED));
4085 udelay(40);
4086
Matt Carlson8ef21422008-05-02 16:47:53 -07004087 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
4088 tw32_f(MAC_MI_MODE,
4089 (tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL));
4090 udelay(80);
4091 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004092
Matt Carlsonb4bd2922011-04-20 07:57:41 +00004093 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_PWRCTL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004094
4095 /* Some third-party PHYs need to be reset on link going
4096 * down.
4097 */
4098 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
4099 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
4100 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) &&
4101 netif_carrier_ok(tp->dev)) {
4102 tg3_readphy(tp, MII_BMSR, &bmsr);
4103 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
4104 !(bmsr & BMSR_LSTATUS))
4105 force_reset = 1;
4106 }
4107 if (force_reset)
4108 tg3_phy_reset(tp);
4109
Matt Carlson79eb6902010-02-17 15:17:03 +00004110 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004111 tg3_readphy(tp, MII_BMSR, &bmsr);
4112 if (tg3_readphy(tp, MII_BMSR, &bmsr) ||
Joe Perches63c3a662011-04-26 08:12:10 +00004113 !tg3_flag(tp, INIT_COMPLETE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004114 bmsr = 0;
4115
4116 if (!(bmsr & BMSR_LSTATUS)) {
4117 err = tg3_init_5401phy_dsp(tp);
4118 if (err)
4119 return err;
4120
4121 tg3_readphy(tp, MII_BMSR, &bmsr);
4122 for (i = 0; i < 1000; i++) {
4123 udelay(10);
4124 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
4125 (bmsr & BMSR_LSTATUS)) {
4126 udelay(40);
4127 break;
4128 }
4129 }
4130
Matt Carlson79eb6902010-02-17 15:17:03 +00004131 if ((tp->phy_id & TG3_PHY_ID_REV_MASK) ==
4132 TG3_PHY_REV_BCM5401_B0 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004133 !(bmsr & BMSR_LSTATUS) &&
4134 tp->link_config.active_speed == SPEED_1000) {
4135 err = tg3_phy_reset(tp);
4136 if (!err)
4137 err = tg3_init_5401phy_dsp(tp);
4138 if (err)
4139 return err;
4140 }
4141 }
4142 } else if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
4143 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0) {
4144 /* 5701 {A0,B0} CRC bug workaround */
4145 tg3_writephy(tp, 0x15, 0x0a75);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00004146 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8c68);
4147 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8d68);
4148 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8c68);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004149 }
4150
4151 /* Clear pending interrupts... */
Matt Carlsonf833c4c2010-09-15 09:00:01 +00004152 tg3_readphy(tp, MII_TG3_ISTAT, &val);
4153 tg3_readphy(tp, MII_TG3_ISTAT, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004154
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004155 if (tp->phy_flags & TG3_PHYFLG_USE_MI_INTERRUPT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004156 tg3_writephy(tp, MII_TG3_IMASK, ~MII_TG3_INT_LINKCHG);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004157 else if (!(tp->phy_flags & TG3_PHYFLG_IS_FET))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004158 tg3_writephy(tp, MII_TG3_IMASK, ~0);
4159
4160 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
4161 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
4162 if (tp->led_ctrl == LED_CTRL_MODE_PHY_1)
4163 tg3_writephy(tp, MII_TG3_EXT_CTRL,
4164 MII_TG3_EXT_CTRL_LNK3_LED_MODE);
4165 else
4166 tg3_writephy(tp, MII_TG3_EXT_CTRL, 0);
4167 }
4168
4169 current_link_up = 0;
Matt Carlsone7405222012-02-13 15:20:16 +00004170 current_speed = SPEED_UNKNOWN;
4171 current_duplex = DUPLEX_UNKNOWN;
Matt Carlsone348c5e2011-11-21 15:01:20 +00004172 tp->phy_flags &= ~TG3_PHYFLG_MDIX_STATE;
Matt Carlson859edb22011-12-08 14:40:16 +00004173 tp->link_config.rmt_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004174
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004175 if (tp->phy_flags & TG3_PHYFLG_CAPACITIVE_COUPLING) {
Matt Carlson15ee95c2011-04-20 07:57:40 +00004176 err = tg3_phy_auxctl_read(tp,
4177 MII_TG3_AUXCTL_SHDWSEL_MISCTEST,
4178 &val);
4179 if (!err && !(val & (1 << 10))) {
Matt Carlsonb4bd2922011-04-20 07:57:41 +00004180 tg3_phy_auxctl_write(tp,
4181 MII_TG3_AUXCTL_SHDWSEL_MISCTEST,
4182 val | (1 << 10));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004183 goto relink;
4184 }
4185 }
4186
4187 bmsr = 0;
4188 for (i = 0; i < 100; i++) {
4189 tg3_readphy(tp, MII_BMSR, &bmsr);
4190 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
4191 (bmsr & BMSR_LSTATUS))
4192 break;
4193 udelay(40);
4194 }
4195
4196 if (bmsr & BMSR_LSTATUS) {
4197 u32 aux_stat, bmcr;
4198
4199 tg3_readphy(tp, MII_TG3_AUX_STAT, &aux_stat);
4200 for (i = 0; i < 2000; i++) {
4201 udelay(10);
4202 if (!tg3_readphy(tp, MII_TG3_AUX_STAT, &aux_stat) &&
4203 aux_stat)
4204 break;
4205 }
4206
4207 tg3_aux_stat_to_speed_duplex(tp, aux_stat,
4208 &current_speed,
4209 &current_duplex);
4210
4211 bmcr = 0;
4212 for (i = 0; i < 200; i++) {
4213 tg3_readphy(tp, MII_BMCR, &bmcr);
4214 if (tg3_readphy(tp, MII_BMCR, &bmcr))
4215 continue;
4216 if (bmcr && bmcr != 0x7fff)
4217 break;
4218 udelay(10);
4219 }
4220
Matt Carlsonef167e22007-12-20 20:10:01 -08004221 lcl_adv = 0;
4222 rmt_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004223
Matt Carlsonef167e22007-12-20 20:10:01 -08004224 tp->link_config.active_speed = current_speed;
4225 tp->link_config.active_duplex = current_duplex;
4226
4227 if (tp->link_config.autoneg == AUTONEG_ENABLE) {
4228 if ((bmcr & BMCR_ANENABLE) &&
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004229 tg3_phy_copper_an_config_ok(tp, &lcl_adv) &&
Matt Carlson859edb22011-12-08 14:40:16 +00004230 tg3_phy_copper_fetch_rmtadv(tp, &rmt_adv))
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004231 current_link_up = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004232 } else {
4233 if (!(bmcr & BMCR_ANENABLE) &&
4234 tp->link_config.speed == current_speed &&
Matt Carlsonef167e22007-12-20 20:10:01 -08004235 tp->link_config.duplex == current_duplex &&
4236 tp->link_config.flowctrl ==
4237 tp->link_config.active_flowctrl) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004238 current_link_up = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004239 }
4240 }
4241
Matt Carlsonef167e22007-12-20 20:10:01 -08004242 if (current_link_up == 1 &&
Matt Carlsone348c5e2011-11-21 15:01:20 +00004243 tp->link_config.active_duplex == DUPLEX_FULL) {
4244 u32 reg, bit;
4245
4246 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
4247 reg = MII_TG3_FET_GEN_STAT;
4248 bit = MII_TG3_FET_GEN_STAT_MDIXSTAT;
4249 } else {
4250 reg = MII_TG3_EXT_STAT;
4251 bit = MII_TG3_EXT_STAT_MDIX;
4252 }
4253
4254 if (!tg3_readphy(tp, reg, &val) && (val & bit))
4255 tp->phy_flags |= TG3_PHYFLG_MDIX_STATE;
4256
Matt Carlsonef167e22007-12-20 20:10:01 -08004257 tg3_setup_flow_control(tp, lcl_adv, rmt_adv);
Matt Carlsone348c5e2011-11-21 15:01:20 +00004258 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004259 }
4260
Linus Torvalds1da177e2005-04-16 15:20:36 -07004261relink:
Matt Carlson80096062010-08-02 11:26:06 +00004262 if (current_link_up == 0 || (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004263 tg3_phy_copper_begin(tp);
4264
Matt Carlsonf833c4c2010-09-15 09:00:01 +00004265 tg3_readphy(tp, MII_BMSR, &bmsr);
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00004266 if ((!tg3_readphy(tp, MII_BMSR, &bmsr) && (bmsr & BMSR_LSTATUS)) ||
4267 (tp->mac_mode & MAC_MODE_PORT_INT_LPBACK))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004268 current_link_up = 1;
4269 }
4270
4271 tp->mac_mode &= ~MAC_MODE_PORT_MODE_MASK;
4272 if (current_link_up == 1) {
4273 if (tp->link_config.active_speed == SPEED_100 ||
4274 tp->link_config.active_speed == SPEED_10)
4275 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
4276 else
4277 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004278 } else if (tp->phy_flags & TG3_PHYFLG_IS_FET)
Matt Carlson7f97a4b2009-08-25 10:10:03 +00004279 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
4280 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004281 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
4282
4283 tp->mac_mode &= ~MAC_MODE_HALF_DUPLEX;
4284 if (tp->link_config.active_duplex == DUPLEX_HALF)
4285 tp->mac_mode |= MAC_MODE_HALF_DUPLEX;
4286
Linus Torvalds1da177e2005-04-16 15:20:36 -07004287 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700) {
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07004288 if (current_link_up == 1 &&
4289 tg3_5700_link_polarity(tp, tp->link_config.active_speed))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004290 tp->mac_mode |= MAC_MODE_LINK_POLARITY;
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07004291 else
4292 tp->mac_mode &= ~MAC_MODE_LINK_POLARITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004293 }
4294
4295 /* ??? Without this setting Netgear GA302T PHY does not
4296 * ??? send/receive packets...
4297 */
Matt Carlson79eb6902010-02-17 15:17:03 +00004298 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5411 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004299 tp->pci_chip_rev_id == CHIPREV_ID_5700_ALTIMA) {
4300 tp->mi_mode |= MAC_MI_MODE_AUTO_POLL;
4301 tw32_f(MAC_MI_MODE, tp->mi_mode);
4302 udelay(80);
4303 }
4304
4305 tw32_f(MAC_MODE, tp->mac_mode);
4306 udelay(40);
4307
Matt Carlson52b02d02010-10-14 10:37:41 +00004308 tg3_phy_eee_adjust(tp, current_link_up);
4309
Joe Perches63c3a662011-04-26 08:12:10 +00004310 if (tg3_flag(tp, USE_LINKCHG_REG)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004311 /* Polled via timer. */
4312 tw32_f(MAC_EVENT, 0);
4313 } else {
4314 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
4315 }
4316 udelay(40);
4317
4318 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 &&
4319 current_link_up == 1 &&
4320 tp->link_config.active_speed == SPEED_1000 &&
Joe Perches63c3a662011-04-26 08:12:10 +00004321 (tg3_flag(tp, PCIX_MODE) || tg3_flag(tp, PCI_HIGH_SPEED))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004322 udelay(120);
4323 tw32_f(MAC_STATUS,
4324 (MAC_STATUS_SYNC_CHANGED |
4325 MAC_STATUS_CFG_CHANGED));
4326 udelay(40);
4327 tg3_write_mem(tp,
4328 NIC_SRAM_FIRMWARE_MBOX,
4329 NIC_SRAM_FIRMWARE_MBOX_MAGIC2);
4330 }
4331
Matt Carlson5e7dfd02008-11-21 17:18:16 -08004332 /* Prevent send BD corruption. */
Joe Perches63c3a662011-04-26 08:12:10 +00004333 if (tg3_flag(tp, CLKREQ_BUG)) {
Matt Carlson5e7dfd02008-11-21 17:18:16 -08004334 u16 oldlnkctl, newlnkctl;
4335
4336 pci_read_config_word(tp->pdev,
Jon Mason708ebb3a2011-06-27 12:56:50 +00004337 pci_pcie_cap(tp->pdev) + PCI_EXP_LNKCTL,
Matt Carlson5e7dfd02008-11-21 17:18:16 -08004338 &oldlnkctl);
4339 if (tp->link_config.active_speed == SPEED_100 ||
4340 tp->link_config.active_speed == SPEED_10)
4341 newlnkctl = oldlnkctl & ~PCI_EXP_LNKCTL_CLKREQ_EN;
4342 else
4343 newlnkctl = oldlnkctl | PCI_EXP_LNKCTL_CLKREQ_EN;
4344 if (newlnkctl != oldlnkctl)
4345 pci_write_config_word(tp->pdev,
Matt Carlson93a700a2011-08-31 11:44:54 +00004346 pci_pcie_cap(tp->pdev) +
4347 PCI_EXP_LNKCTL, newlnkctl);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08004348 }
4349
Linus Torvalds1da177e2005-04-16 15:20:36 -07004350 if (current_link_up != netif_carrier_ok(tp->dev)) {
4351 if (current_link_up)
4352 netif_carrier_on(tp->dev);
4353 else
4354 netif_carrier_off(tp->dev);
4355 tg3_link_report(tp);
4356 }
4357
4358 return 0;
4359}
4360
4361struct tg3_fiber_aneginfo {
4362 int state;
4363#define ANEG_STATE_UNKNOWN 0
4364#define ANEG_STATE_AN_ENABLE 1
4365#define ANEG_STATE_RESTART_INIT 2
4366#define ANEG_STATE_RESTART 3
4367#define ANEG_STATE_DISABLE_LINK_OK 4
4368#define ANEG_STATE_ABILITY_DETECT_INIT 5
4369#define ANEG_STATE_ABILITY_DETECT 6
4370#define ANEG_STATE_ACK_DETECT_INIT 7
4371#define ANEG_STATE_ACK_DETECT 8
4372#define ANEG_STATE_COMPLETE_ACK_INIT 9
4373#define ANEG_STATE_COMPLETE_ACK 10
4374#define ANEG_STATE_IDLE_DETECT_INIT 11
4375#define ANEG_STATE_IDLE_DETECT 12
4376#define ANEG_STATE_LINK_OK 13
4377#define ANEG_STATE_NEXT_PAGE_WAIT_INIT 14
4378#define ANEG_STATE_NEXT_PAGE_WAIT 15
4379
4380 u32 flags;
4381#define MR_AN_ENABLE 0x00000001
4382#define MR_RESTART_AN 0x00000002
4383#define MR_AN_COMPLETE 0x00000004
4384#define MR_PAGE_RX 0x00000008
4385#define MR_NP_LOADED 0x00000010
4386#define MR_TOGGLE_TX 0x00000020
4387#define MR_LP_ADV_FULL_DUPLEX 0x00000040
4388#define MR_LP_ADV_HALF_DUPLEX 0x00000080
4389#define MR_LP_ADV_SYM_PAUSE 0x00000100
4390#define MR_LP_ADV_ASYM_PAUSE 0x00000200
4391#define MR_LP_ADV_REMOTE_FAULT1 0x00000400
4392#define MR_LP_ADV_REMOTE_FAULT2 0x00000800
4393#define MR_LP_ADV_NEXT_PAGE 0x00001000
4394#define MR_TOGGLE_RX 0x00002000
4395#define MR_NP_RX 0x00004000
4396
4397#define MR_LINK_OK 0x80000000
4398
4399 unsigned long link_time, cur_time;
4400
4401 u32 ability_match_cfg;
4402 int ability_match_count;
4403
4404 char ability_match, idle_match, ack_match;
4405
4406 u32 txconfig, rxconfig;
4407#define ANEG_CFG_NP 0x00000080
4408#define ANEG_CFG_ACK 0x00000040
4409#define ANEG_CFG_RF2 0x00000020
4410#define ANEG_CFG_RF1 0x00000010
4411#define ANEG_CFG_PS2 0x00000001
4412#define ANEG_CFG_PS1 0x00008000
4413#define ANEG_CFG_HD 0x00004000
4414#define ANEG_CFG_FD 0x00002000
4415#define ANEG_CFG_INVAL 0x00001f06
4416
4417};
4418#define ANEG_OK 0
4419#define ANEG_DONE 1
4420#define ANEG_TIMER_ENAB 2
4421#define ANEG_FAILED -1
4422
4423#define ANEG_STATE_SETTLE_TIME 10000
4424
4425static int tg3_fiber_aneg_smachine(struct tg3 *tp,
4426 struct tg3_fiber_aneginfo *ap)
4427{
Matt Carlson5be73b42007-12-20 20:09:29 -08004428 u16 flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004429 unsigned long delta;
4430 u32 rx_cfg_reg;
4431 int ret;
4432
4433 if (ap->state == ANEG_STATE_UNKNOWN) {
4434 ap->rxconfig = 0;
4435 ap->link_time = 0;
4436 ap->cur_time = 0;
4437 ap->ability_match_cfg = 0;
4438 ap->ability_match_count = 0;
4439 ap->ability_match = 0;
4440 ap->idle_match = 0;
4441 ap->ack_match = 0;
4442 }
4443 ap->cur_time++;
4444
4445 if (tr32(MAC_STATUS) & MAC_STATUS_RCVD_CFG) {
4446 rx_cfg_reg = tr32(MAC_RX_AUTO_NEG);
4447
4448 if (rx_cfg_reg != ap->ability_match_cfg) {
4449 ap->ability_match_cfg = rx_cfg_reg;
4450 ap->ability_match = 0;
4451 ap->ability_match_count = 0;
4452 } else {
4453 if (++ap->ability_match_count > 1) {
4454 ap->ability_match = 1;
4455 ap->ability_match_cfg = rx_cfg_reg;
4456 }
4457 }
4458 if (rx_cfg_reg & ANEG_CFG_ACK)
4459 ap->ack_match = 1;
4460 else
4461 ap->ack_match = 0;
4462
4463 ap->idle_match = 0;
4464 } else {
4465 ap->idle_match = 1;
4466 ap->ability_match_cfg = 0;
4467 ap->ability_match_count = 0;
4468 ap->ability_match = 0;
4469 ap->ack_match = 0;
4470
4471 rx_cfg_reg = 0;
4472 }
4473
4474 ap->rxconfig = rx_cfg_reg;
4475 ret = ANEG_OK;
4476
Matt Carlson33f401a2010-04-05 10:19:27 +00004477 switch (ap->state) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004478 case ANEG_STATE_UNKNOWN:
4479 if (ap->flags & (MR_AN_ENABLE | MR_RESTART_AN))
4480 ap->state = ANEG_STATE_AN_ENABLE;
4481
4482 /* fallthru */
4483 case ANEG_STATE_AN_ENABLE:
4484 ap->flags &= ~(MR_AN_COMPLETE | MR_PAGE_RX);
4485 if (ap->flags & MR_AN_ENABLE) {
4486 ap->link_time = 0;
4487 ap->cur_time = 0;
4488 ap->ability_match_cfg = 0;
4489 ap->ability_match_count = 0;
4490 ap->ability_match = 0;
4491 ap->idle_match = 0;
4492 ap->ack_match = 0;
4493
4494 ap->state = ANEG_STATE_RESTART_INIT;
4495 } else {
4496 ap->state = ANEG_STATE_DISABLE_LINK_OK;
4497 }
4498 break;
4499
4500 case ANEG_STATE_RESTART_INIT:
4501 ap->link_time = ap->cur_time;
4502 ap->flags &= ~(MR_NP_LOADED);
4503 ap->txconfig = 0;
4504 tw32(MAC_TX_AUTO_NEG, 0);
4505 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
4506 tw32_f(MAC_MODE, tp->mac_mode);
4507 udelay(40);
4508
4509 ret = ANEG_TIMER_ENAB;
4510 ap->state = ANEG_STATE_RESTART;
4511
4512 /* fallthru */
4513 case ANEG_STATE_RESTART:
4514 delta = ap->cur_time - ap->link_time;
Matt Carlson859a588792010-04-05 10:19:28 +00004515 if (delta > ANEG_STATE_SETTLE_TIME)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004516 ap->state = ANEG_STATE_ABILITY_DETECT_INIT;
Matt Carlson859a588792010-04-05 10:19:28 +00004517 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004518 ret = ANEG_TIMER_ENAB;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004519 break;
4520
4521 case ANEG_STATE_DISABLE_LINK_OK:
4522 ret = ANEG_DONE;
4523 break;
4524
4525 case ANEG_STATE_ABILITY_DETECT_INIT:
4526 ap->flags &= ~(MR_TOGGLE_TX);
Matt Carlson5be73b42007-12-20 20:09:29 -08004527 ap->txconfig = ANEG_CFG_FD;
4528 flowctrl = tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
4529 if (flowctrl & ADVERTISE_1000XPAUSE)
4530 ap->txconfig |= ANEG_CFG_PS1;
4531 if (flowctrl & ADVERTISE_1000XPSE_ASYM)
4532 ap->txconfig |= ANEG_CFG_PS2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004533 tw32(MAC_TX_AUTO_NEG, ap->txconfig);
4534 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
4535 tw32_f(MAC_MODE, tp->mac_mode);
4536 udelay(40);
4537
4538 ap->state = ANEG_STATE_ABILITY_DETECT;
4539 break;
4540
4541 case ANEG_STATE_ABILITY_DETECT:
Matt Carlson859a588792010-04-05 10:19:28 +00004542 if (ap->ability_match != 0 && ap->rxconfig != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004543 ap->state = ANEG_STATE_ACK_DETECT_INIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004544 break;
4545
4546 case ANEG_STATE_ACK_DETECT_INIT:
4547 ap->txconfig |= ANEG_CFG_ACK;
4548 tw32(MAC_TX_AUTO_NEG, ap->txconfig);
4549 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
4550 tw32_f(MAC_MODE, tp->mac_mode);
4551 udelay(40);
4552
4553 ap->state = ANEG_STATE_ACK_DETECT;
4554
4555 /* fallthru */
4556 case ANEG_STATE_ACK_DETECT:
4557 if (ap->ack_match != 0) {
4558 if ((ap->rxconfig & ~ANEG_CFG_ACK) ==
4559 (ap->ability_match_cfg & ~ANEG_CFG_ACK)) {
4560 ap->state = ANEG_STATE_COMPLETE_ACK_INIT;
4561 } else {
4562 ap->state = ANEG_STATE_AN_ENABLE;
4563 }
4564 } else if (ap->ability_match != 0 &&
4565 ap->rxconfig == 0) {
4566 ap->state = ANEG_STATE_AN_ENABLE;
4567 }
4568 break;
4569
4570 case ANEG_STATE_COMPLETE_ACK_INIT:
4571 if (ap->rxconfig & ANEG_CFG_INVAL) {
4572 ret = ANEG_FAILED;
4573 break;
4574 }
4575 ap->flags &= ~(MR_LP_ADV_FULL_DUPLEX |
4576 MR_LP_ADV_HALF_DUPLEX |
4577 MR_LP_ADV_SYM_PAUSE |
4578 MR_LP_ADV_ASYM_PAUSE |
4579 MR_LP_ADV_REMOTE_FAULT1 |
4580 MR_LP_ADV_REMOTE_FAULT2 |
4581 MR_LP_ADV_NEXT_PAGE |
4582 MR_TOGGLE_RX |
4583 MR_NP_RX);
4584 if (ap->rxconfig & ANEG_CFG_FD)
4585 ap->flags |= MR_LP_ADV_FULL_DUPLEX;
4586 if (ap->rxconfig & ANEG_CFG_HD)
4587 ap->flags |= MR_LP_ADV_HALF_DUPLEX;
4588 if (ap->rxconfig & ANEG_CFG_PS1)
4589 ap->flags |= MR_LP_ADV_SYM_PAUSE;
4590 if (ap->rxconfig & ANEG_CFG_PS2)
4591 ap->flags |= MR_LP_ADV_ASYM_PAUSE;
4592 if (ap->rxconfig & ANEG_CFG_RF1)
4593 ap->flags |= MR_LP_ADV_REMOTE_FAULT1;
4594 if (ap->rxconfig & ANEG_CFG_RF2)
4595 ap->flags |= MR_LP_ADV_REMOTE_FAULT2;
4596 if (ap->rxconfig & ANEG_CFG_NP)
4597 ap->flags |= MR_LP_ADV_NEXT_PAGE;
4598
4599 ap->link_time = ap->cur_time;
4600
4601 ap->flags ^= (MR_TOGGLE_TX);
4602 if (ap->rxconfig & 0x0008)
4603 ap->flags |= MR_TOGGLE_RX;
4604 if (ap->rxconfig & ANEG_CFG_NP)
4605 ap->flags |= MR_NP_RX;
4606 ap->flags |= MR_PAGE_RX;
4607
4608 ap->state = ANEG_STATE_COMPLETE_ACK;
4609 ret = ANEG_TIMER_ENAB;
4610 break;
4611
4612 case ANEG_STATE_COMPLETE_ACK:
4613 if (ap->ability_match != 0 &&
4614 ap->rxconfig == 0) {
4615 ap->state = ANEG_STATE_AN_ENABLE;
4616 break;
4617 }
4618 delta = ap->cur_time - ap->link_time;
4619 if (delta > ANEG_STATE_SETTLE_TIME) {
4620 if (!(ap->flags & (MR_LP_ADV_NEXT_PAGE))) {
4621 ap->state = ANEG_STATE_IDLE_DETECT_INIT;
4622 } else {
4623 if ((ap->txconfig & ANEG_CFG_NP) == 0 &&
4624 !(ap->flags & MR_NP_RX)) {
4625 ap->state = ANEG_STATE_IDLE_DETECT_INIT;
4626 } else {
4627 ret = ANEG_FAILED;
4628 }
4629 }
4630 }
4631 break;
4632
4633 case ANEG_STATE_IDLE_DETECT_INIT:
4634 ap->link_time = ap->cur_time;
4635 tp->mac_mode &= ~MAC_MODE_SEND_CONFIGS;
4636 tw32_f(MAC_MODE, tp->mac_mode);
4637 udelay(40);
4638
4639 ap->state = ANEG_STATE_IDLE_DETECT;
4640 ret = ANEG_TIMER_ENAB;
4641 break;
4642
4643 case ANEG_STATE_IDLE_DETECT:
4644 if (ap->ability_match != 0 &&
4645 ap->rxconfig == 0) {
4646 ap->state = ANEG_STATE_AN_ENABLE;
4647 break;
4648 }
4649 delta = ap->cur_time - ap->link_time;
4650 if (delta > ANEG_STATE_SETTLE_TIME) {
4651 /* XXX another gem from the Broadcom driver :( */
4652 ap->state = ANEG_STATE_LINK_OK;
4653 }
4654 break;
4655
4656 case ANEG_STATE_LINK_OK:
4657 ap->flags |= (MR_AN_COMPLETE | MR_LINK_OK);
4658 ret = ANEG_DONE;
4659 break;
4660
4661 case ANEG_STATE_NEXT_PAGE_WAIT_INIT:
4662 /* ??? unimplemented */
4663 break;
4664
4665 case ANEG_STATE_NEXT_PAGE_WAIT:
4666 /* ??? unimplemented */
4667 break;
4668
4669 default:
4670 ret = ANEG_FAILED;
4671 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07004672 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004673
4674 return ret;
4675}
4676
Matt Carlson5be73b42007-12-20 20:09:29 -08004677static int fiber_autoneg(struct tg3 *tp, u32 *txflags, u32 *rxflags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004678{
4679 int res = 0;
4680 struct tg3_fiber_aneginfo aninfo;
4681 int status = ANEG_FAILED;
4682 unsigned int tick;
4683 u32 tmp;
4684
4685 tw32_f(MAC_TX_AUTO_NEG, 0);
4686
4687 tmp = tp->mac_mode & ~MAC_MODE_PORT_MODE_MASK;
4688 tw32_f(MAC_MODE, tmp | MAC_MODE_PORT_MODE_GMII);
4689 udelay(40);
4690
4691 tw32_f(MAC_MODE, tp->mac_mode | MAC_MODE_SEND_CONFIGS);
4692 udelay(40);
4693
4694 memset(&aninfo, 0, sizeof(aninfo));
4695 aninfo.flags |= MR_AN_ENABLE;
4696 aninfo.state = ANEG_STATE_UNKNOWN;
4697 aninfo.cur_time = 0;
4698 tick = 0;
4699 while (++tick < 195000) {
4700 status = tg3_fiber_aneg_smachine(tp, &aninfo);
4701 if (status == ANEG_DONE || status == ANEG_FAILED)
4702 break;
4703
4704 udelay(1);
4705 }
4706
4707 tp->mac_mode &= ~MAC_MODE_SEND_CONFIGS;
4708 tw32_f(MAC_MODE, tp->mac_mode);
4709 udelay(40);
4710
Matt Carlson5be73b42007-12-20 20:09:29 -08004711 *txflags = aninfo.txconfig;
4712 *rxflags = aninfo.flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004713
4714 if (status == ANEG_DONE &&
4715 (aninfo.flags & (MR_AN_COMPLETE | MR_LINK_OK |
4716 MR_LP_ADV_FULL_DUPLEX)))
4717 res = 1;
4718
4719 return res;
4720}
4721
4722static void tg3_init_bcm8002(struct tg3 *tp)
4723{
4724 u32 mac_status = tr32(MAC_STATUS);
4725 int i;
4726
4727 /* Reset when initting first time or we have a link. */
Joe Perches63c3a662011-04-26 08:12:10 +00004728 if (tg3_flag(tp, INIT_COMPLETE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004729 !(mac_status & MAC_STATUS_PCS_SYNCED))
4730 return;
4731
4732 /* Set PLL lock range. */
4733 tg3_writephy(tp, 0x16, 0x8007);
4734
4735 /* SW reset */
4736 tg3_writephy(tp, MII_BMCR, BMCR_RESET);
4737
4738 /* Wait for reset to complete. */
4739 /* XXX schedule_timeout() ... */
4740 for (i = 0; i < 500; i++)
4741 udelay(10);
4742
4743 /* Config mode; select PMA/Ch 1 regs. */
4744 tg3_writephy(tp, 0x10, 0x8411);
4745
4746 /* Enable auto-lock and comdet, select txclk for tx. */
4747 tg3_writephy(tp, 0x11, 0x0a10);
4748
4749 tg3_writephy(tp, 0x18, 0x00a0);
4750 tg3_writephy(tp, 0x16, 0x41ff);
4751
4752 /* Assert and deassert POR. */
4753 tg3_writephy(tp, 0x13, 0x0400);
4754 udelay(40);
4755 tg3_writephy(tp, 0x13, 0x0000);
4756
4757 tg3_writephy(tp, 0x11, 0x0a50);
4758 udelay(40);
4759 tg3_writephy(tp, 0x11, 0x0a10);
4760
4761 /* Wait for signal to stabilize */
4762 /* XXX schedule_timeout() ... */
4763 for (i = 0; i < 15000; i++)
4764 udelay(10);
4765
4766 /* Deselect the channel register so we can read the PHYID
4767 * later.
4768 */
4769 tg3_writephy(tp, 0x10, 0x8011);
4770}
4771
4772static int tg3_setup_fiber_hw_autoneg(struct tg3 *tp, u32 mac_status)
4773{
Matt Carlson82cd3d12007-12-20 20:09:00 -08004774 u16 flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004775 u32 sg_dig_ctrl, sg_dig_status;
4776 u32 serdes_cfg, expected_sg_dig_ctrl;
4777 int workaround, port_a;
4778 int current_link_up;
4779
4780 serdes_cfg = 0;
4781 expected_sg_dig_ctrl = 0;
4782 workaround = 0;
4783 port_a = 1;
4784 current_link_up = 0;
4785
4786 if (tp->pci_chip_rev_id != CHIPREV_ID_5704_A0 &&
4787 tp->pci_chip_rev_id != CHIPREV_ID_5704_A1) {
4788 workaround = 1;
4789 if (tr32(TG3PCI_DUAL_MAC_CTRL) & DUAL_MAC_CTRL_ID)
4790 port_a = 0;
4791
4792 /* preserve bits 0-11,13,14 for signal pre-emphasis */
4793 /* preserve bits 20-23 for voltage regulator */
4794 serdes_cfg = tr32(MAC_SERDES_CFG) & 0x00f06fff;
4795 }
4796
4797 sg_dig_ctrl = tr32(SG_DIG_CTRL);
4798
4799 if (tp->link_config.autoneg != AUTONEG_ENABLE) {
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004800 if (sg_dig_ctrl & SG_DIG_USING_HW_AUTONEG) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004801 if (workaround) {
4802 u32 val = serdes_cfg;
4803
4804 if (port_a)
4805 val |= 0xc010000;
4806 else
4807 val |= 0x4010000;
4808 tw32_f(MAC_SERDES_CFG, val);
4809 }
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004810
4811 tw32_f(SG_DIG_CTRL, SG_DIG_COMMON_SETUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004812 }
4813 if (mac_status & MAC_STATUS_PCS_SYNCED) {
4814 tg3_setup_flow_control(tp, 0, 0);
4815 current_link_up = 1;
4816 }
4817 goto out;
4818 }
4819
4820 /* Want auto-negotiation. */
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004821 expected_sg_dig_ctrl = SG_DIG_USING_HW_AUTONEG | SG_DIG_COMMON_SETUP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004822
Matt Carlson82cd3d12007-12-20 20:09:00 -08004823 flowctrl = tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
4824 if (flowctrl & ADVERTISE_1000XPAUSE)
4825 expected_sg_dig_ctrl |= SG_DIG_PAUSE_CAP;
4826 if (flowctrl & ADVERTISE_1000XPSE_ASYM)
4827 expected_sg_dig_ctrl |= SG_DIG_ASYM_PAUSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004828
4829 if (sg_dig_ctrl != expected_sg_dig_ctrl) {
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004830 if ((tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT) &&
Michael Chan3d3ebe72006-09-27 15:59:15 -07004831 tp->serdes_counter &&
4832 ((mac_status & (MAC_STATUS_PCS_SYNCED |
4833 MAC_STATUS_RCVD_CFG)) ==
4834 MAC_STATUS_PCS_SYNCED)) {
4835 tp->serdes_counter--;
4836 current_link_up = 1;
4837 goto out;
4838 }
4839restart_autoneg:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004840 if (workaround)
4841 tw32_f(MAC_SERDES_CFG, serdes_cfg | 0xc011000);
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004842 tw32_f(SG_DIG_CTRL, expected_sg_dig_ctrl | SG_DIG_SOFT_RESET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004843 udelay(5);
4844 tw32_f(SG_DIG_CTRL, expected_sg_dig_ctrl);
4845
Michael Chan3d3ebe72006-09-27 15:59:15 -07004846 tp->serdes_counter = SERDES_AN_TIMEOUT_5704S;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004847 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004848 } else if (mac_status & (MAC_STATUS_PCS_SYNCED |
4849 MAC_STATUS_SIGNAL_DET)) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07004850 sg_dig_status = tr32(SG_DIG_STATUS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004851 mac_status = tr32(MAC_STATUS);
4852
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004853 if ((sg_dig_status & SG_DIG_AUTONEG_COMPLETE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004854 (mac_status & MAC_STATUS_PCS_SYNCED)) {
Matt Carlson82cd3d12007-12-20 20:09:00 -08004855 u32 local_adv = 0, remote_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004856
Matt Carlson82cd3d12007-12-20 20:09:00 -08004857 if (sg_dig_ctrl & SG_DIG_PAUSE_CAP)
4858 local_adv |= ADVERTISE_1000XPAUSE;
4859 if (sg_dig_ctrl & SG_DIG_ASYM_PAUSE)
4860 local_adv |= ADVERTISE_1000XPSE_ASYM;
4861
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004862 if (sg_dig_status & SG_DIG_PARTNER_PAUSE_CAPABLE)
Matt Carlson82cd3d12007-12-20 20:09:00 -08004863 remote_adv |= LPA_1000XPAUSE;
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004864 if (sg_dig_status & SG_DIG_PARTNER_ASYM_PAUSE)
Matt Carlson82cd3d12007-12-20 20:09:00 -08004865 remote_adv |= LPA_1000XPAUSE_ASYM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004866
Matt Carlson859edb22011-12-08 14:40:16 +00004867 tp->link_config.rmt_adv =
4868 mii_adv_to_ethtool_adv_x(remote_adv);
4869
Linus Torvalds1da177e2005-04-16 15:20:36 -07004870 tg3_setup_flow_control(tp, local_adv, remote_adv);
4871 current_link_up = 1;
Michael Chan3d3ebe72006-09-27 15:59:15 -07004872 tp->serdes_counter = 0;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004873 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004874 } else if (!(sg_dig_status & SG_DIG_AUTONEG_COMPLETE)) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07004875 if (tp->serdes_counter)
4876 tp->serdes_counter--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004877 else {
4878 if (workaround) {
4879 u32 val = serdes_cfg;
4880
4881 if (port_a)
4882 val |= 0xc010000;
4883 else
4884 val |= 0x4010000;
4885
4886 tw32_f(MAC_SERDES_CFG, val);
4887 }
4888
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004889 tw32_f(SG_DIG_CTRL, SG_DIG_COMMON_SETUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004890 udelay(40);
4891
4892 /* Link parallel detection - link is up */
4893 /* only if we have PCS_SYNC and not */
4894 /* receiving config code words */
4895 mac_status = tr32(MAC_STATUS);
4896 if ((mac_status & MAC_STATUS_PCS_SYNCED) &&
4897 !(mac_status & MAC_STATUS_RCVD_CFG)) {
4898 tg3_setup_flow_control(tp, 0, 0);
4899 current_link_up = 1;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004900 tp->phy_flags |=
4901 TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan3d3ebe72006-09-27 15:59:15 -07004902 tp->serdes_counter =
4903 SERDES_PARALLEL_DET_TIMEOUT;
4904 } else
4905 goto restart_autoneg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004906 }
4907 }
Michael Chan3d3ebe72006-09-27 15:59:15 -07004908 } else {
4909 tp->serdes_counter = SERDES_AN_TIMEOUT_5704S;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004910 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004911 }
4912
4913out:
4914 return current_link_up;
4915}
4916
4917static int tg3_setup_fiber_by_hand(struct tg3 *tp, u32 mac_status)
4918{
4919 int current_link_up = 0;
4920
Michael Chan5cf64b8a2007-05-05 12:11:21 -07004921 if (!(mac_status & MAC_STATUS_PCS_SYNCED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004922 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004923
4924 if (tp->link_config.autoneg == AUTONEG_ENABLE) {
Matt Carlson5be73b42007-12-20 20:09:29 -08004925 u32 txflags, rxflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004926 int i;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04004927
Matt Carlson5be73b42007-12-20 20:09:29 -08004928 if (fiber_autoneg(tp, &txflags, &rxflags)) {
4929 u32 local_adv = 0, remote_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004930
Matt Carlson5be73b42007-12-20 20:09:29 -08004931 if (txflags & ANEG_CFG_PS1)
4932 local_adv |= ADVERTISE_1000XPAUSE;
4933 if (txflags & ANEG_CFG_PS2)
4934 local_adv |= ADVERTISE_1000XPSE_ASYM;
4935
4936 if (rxflags & MR_LP_ADV_SYM_PAUSE)
4937 remote_adv |= LPA_1000XPAUSE;
4938 if (rxflags & MR_LP_ADV_ASYM_PAUSE)
4939 remote_adv |= LPA_1000XPAUSE_ASYM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004940
Matt Carlson859edb22011-12-08 14:40:16 +00004941 tp->link_config.rmt_adv =
4942 mii_adv_to_ethtool_adv_x(remote_adv);
4943
Linus Torvalds1da177e2005-04-16 15:20:36 -07004944 tg3_setup_flow_control(tp, local_adv, remote_adv);
4945
Linus Torvalds1da177e2005-04-16 15:20:36 -07004946 current_link_up = 1;
4947 }
4948 for (i = 0; i < 30; i++) {
4949 udelay(20);
4950 tw32_f(MAC_STATUS,
4951 (MAC_STATUS_SYNC_CHANGED |
4952 MAC_STATUS_CFG_CHANGED));
4953 udelay(40);
4954 if ((tr32(MAC_STATUS) &
4955 (MAC_STATUS_SYNC_CHANGED |
4956 MAC_STATUS_CFG_CHANGED)) == 0)
4957 break;
4958 }
4959
4960 mac_status = tr32(MAC_STATUS);
4961 if (current_link_up == 0 &&
4962 (mac_status & MAC_STATUS_PCS_SYNCED) &&
4963 !(mac_status & MAC_STATUS_RCVD_CFG))
4964 current_link_up = 1;
4965 } else {
Matt Carlson5be73b42007-12-20 20:09:29 -08004966 tg3_setup_flow_control(tp, 0, 0);
4967
Linus Torvalds1da177e2005-04-16 15:20:36 -07004968 /* Forcing 1000FD link up. */
4969 current_link_up = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004970
4971 tw32_f(MAC_MODE, (tp->mac_mode | MAC_MODE_SEND_CONFIGS));
4972 udelay(40);
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07004973
4974 tw32_f(MAC_MODE, tp->mac_mode);
4975 udelay(40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004976 }
4977
4978out:
4979 return current_link_up;
4980}
4981
4982static int tg3_setup_fiber_phy(struct tg3 *tp, int force_reset)
4983{
4984 u32 orig_pause_cfg;
4985 u16 orig_active_speed;
4986 u8 orig_active_duplex;
4987 u32 mac_status;
4988 int current_link_up;
4989 int i;
4990
Matt Carlson8d018622007-12-20 20:05:44 -08004991 orig_pause_cfg = tp->link_config.active_flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004992 orig_active_speed = tp->link_config.active_speed;
4993 orig_active_duplex = tp->link_config.active_duplex;
4994
Joe Perches63c3a662011-04-26 08:12:10 +00004995 if (!tg3_flag(tp, HW_AUTONEG) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004996 netif_carrier_ok(tp->dev) &&
Joe Perches63c3a662011-04-26 08:12:10 +00004997 tg3_flag(tp, INIT_COMPLETE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004998 mac_status = tr32(MAC_STATUS);
4999 mac_status &= (MAC_STATUS_PCS_SYNCED |
5000 MAC_STATUS_SIGNAL_DET |
5001 MAC_STATUS_CFG_CHANGED |
5002 MAC_STATUS_RCVD_CFG);
5003 if (mac_status == (MAC_STATUS_PCS_SYNCED |
5004 MAC_STATUS_SIGNAL_DET)) {
5005 tw32_f(MAC_STATUS, (MAC_STATUS_SYNC_CHANGED |
5006 MAC_STATUS_CFG_CHANGED));
5007 return 0;
5008 }
5009 }
5010
5011 tw32_f(MAC_TX_AUTO_NEG, 0);
5012
5013 tp->mac_mode &= ~(MAC_MODE_PORT_MODE_MASK | MAC_MODE_HALF_DUPLEX);
5014 tp->mac_mode |= MAC_MODE_PORT_MODE_TBI;
5015 tw32_f(MAC_MODE, tp->mac_mode);
5016 udelay(40);
5017
Matt Carlson79eb6902010-02-17 15:17:03 +00005018 if (tp->phy_id == TG3_PHY_ID_BCM8002)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005019 tg3_init_bcm8002(tp);
5020
5021 /* Enable link change event even when serdes polling. */
5022 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
5023 udelay(40);
5024
5025 current_link_up = 0;
Matt Carlson859edb22011-12-08 14:40:16 +00005026 tp->link_config.rmt_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005027 mac_status = tr32(MAC_STATUS);
5028
Joe Perches63c3a662011-04-26 08:12:10 +00005029 if (tg3_flag(tp, HW_AUTONEG))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005030 current_link_up = tg3_setup_fiber_hw_autoneg(tp, mac_status);
5031 else
5032 current_link_up = tg3_setup_fiber_by_hand(tp, mac_status);
5033
Matt Carlson898a56f2009-08-28 14:02:40 +00005034 tp->napi[0].hw_status->status =
Linus Torvalds1da177e2005-04-16 15:20:36 -07005035 (SD_STATUS_UPDATED |
Matt Carlson898a56f2009-08-28 14:02:40 +00005036 (tp->napi[0].hw_status->status & ~SD_STATUS_LINK_CHG));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005037
5038 for (i = 0; i < 100; i++) {
5039 tw32_f(MAC_STATUS, (MAC_STATUS_SYNC_CHANGED |
5040 MAC_STATUS_CFG_CHANGED));
5041 udelay(5);
5042 if ((tr32(MAC_STATUS) & (MAC_STATUS_SYNC_CHANGED |
Michael Chan3d3ebe72006-09-27 15:59:15 -07005043 MAC_STATUS_CFG_CHANGED |
5044 MAC_STATUS_LNKSTATE_CHANGED)) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005045 break;
5046 }
5047
5048 mac_status = tr32(MAC_STATUS);
5049 if ((mac_status & MAC_STATUS_PCS_SYNCED) == 0) {
5050 current_link_up = 0;
Michael Chan3d3ebe72006-09-27 15:59:15 -07005051 if (tp->link_config.autoneg == AUTONEG_ENABLE &&
5052 tp->serdes_counter == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005053 tw32_f(MAC_MODE, (tp->mac_mode |
5054 MAC_MODE_SEND_CONFIGS));
5055 udelay(1);
5056 tw32_f(MAC_MODE, tp->mac_mode);
5057 }
5058 }
5059
5060 if (current_link_up == 1) {
5061 tp->link_config.active_speed = SPEED_1000;
5062 tp->link_config.active_duplex = DUPLEX_FULL;
5063 tw32(MAC_LED_CTRL, (tp->led_ctrl |
5064 LED_CTRL_LNKLED_OVERRIDE |
5065 LED_CTRL_1000MBPS_ON));
5066 } else {
Matt Carlsone7405222012-02-13 15:20:16 +00005067 tp->link_config.active_speed = SPEED_UNKNOWN;
5068 tp->link_config.active_duplex = DUPLEX_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005069 tw32(MAC_LED_CTRL, (tp->led_ctrl |
5070 LED_CTRL_LNKLED_OVERRIDE |
5071 LED_CTRL_TRAFFIC_OVERRIDE));
5072 }
5073
5074 if (current_link_up != netif_carrier_ok(tp->dev)) {
5075 if (current_link_up)
5076 netif_carrier_on(tp->dev);
5077 else
5078 netif_carrier_off(tp->dev);
5079 tg3_link_report(tp);
5080 } else {
Matt Carlson8d018622007-12-20 20:05:44 -08005081 u32 now_pause_cfg = tp->link_config.active_flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005082 if (orig_pause_cfg != now_pause_cfg ||
5083 orig_active_speed != tp->link_config.active_speed ||
5084 orig_active_duplex != tp->link_config.active_duplex)
5085 tg3_link_report(tp);
5086 }
5087
5088 return 0;
5089}
5090
Michael Chan747e8f82005-07-25 12:33:22 -07005091static int tg3_setup_fiber_mii_phy(struct tg3 *tp, int force_reset)
5092{
5093 int current_link_up, err = 0;
5094 u32 bmsr, bmcr;
5095 u16 current_speed;
5096 u8 current_duplex;
Matt Carlsonef167e22007-12-20 20:10:01 -08005097 u32 local_adv, remote_adv;
Michael Chan747e8f82005-07-25 12:33:22 -07005098
5099 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
5100 tw32_f(MAC_MODE, tp->mac_mode);
5101 udelay(40);
5102
5103 tw32(MAC_EVENT, 0);
5104
5105 tw32_f(MAC_STATUS,
5106 (MAC_STATUS_SYNC_CHANGED |
5107 MAC_STATUS_CFG_CHANGED |
5108 MAC_STATUS_MI_COMPLETION |
5109 MAC_STATUS_LNKSTATE_CHANGED));
5110 udelay(40);
5111
5112 if (force_reset)
5113 tg3_phy_reset(tp);
5114
5115 current_link_up = 0;
Matt Carlsone7405222012-02-13 15:20:16 +00005116 current_speed = SPEED_UNKNOWN;
5117 current_duplex = DUPLEX_UNKNOWN;
Matt Carlson859edb22011-12-08 14:40:16 +00005118 tp->link_config.rmt_adv = 0;
Michael Chan747e8f82005-07-25 12:33:22 -07005119
5120 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
5121 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
Michael Chand4d2c552006-03-20 17:47:20 -08005122 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
5123 if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
5124 bmsr |= BMSR_LSTATUS;
5125 else
5126 bmsr &= ~BMSR_LSTATUS;
5127 }
Michael Chan747e8f82005-07-25 12:33:22 -07005128
5129 err |= tg3_readphy(tp, MII_BMCR, &bmcr);
5130
5131 if ((tp->link_config.autoneg == AUTONEG_ENABLE) && !force_reset &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005132 (tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT)) {
Michael Chan747e8f82005-07-25 12:33:22 -07005133 /* do nothing, just check for link up at the end */
5134 } else if (tp->link_config.autoneg == AUTONEG_ENABLE) {
Matt Carlson28011cf2011-11-16 18:36:59 -05005135 u32 adv, newadv;
Michael Chan747e8f82005-07-25 12:33:22 -07005136
5137 err |= tg3_readphy(tp, MII_ADVERTISE, &adv);
Matt Carlson28011cf2011-11-16 18:36:59 -05005138 newadv = adv & ~(ADVERTISE_1000XFULL | ADVERTISE_1000XHALF |
5139 ADVERTISE_1000XPAUSE |
5140 ADVERTISE_1000XPSE_ASYM |
5141 ADVERTISE_SLCT);
Michael Chan747e8f82005-07-25 12:33:22 -07005142
Matt Carlson28011cf2011-11-16 18:36:59 -05005143 newadv |= tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
Matt Carlson37f07022011-11-17 14:30:55 +00005144 newadv |= ethtool_adv_to_mii_adv_x(tp->link_config.advertising);
Michael Chan747e8f82005-07-25 12:33:22 -07005145
Matt Carlson28011cf2011-11-16 18:36:59 -05005146 if ((newadv != adv) || !(bmcr & BMCR_ANENABLE)) {
5147 tg3_writephy(tp, MII_ADVERTISE, newadv);
Michael Chan747e8f82005-07-25 12:33:22 -07005148 bmcr |= BMCR_ANENABLE | BMCR_ANRESTART;
5149 tg3_writephy(tp, MII_BMCR, bmcr);
5150
5151 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
Michael Chan3d3ebe72006-09-27 15:59:15 -07005152 tp->serdes_counter = SERDES_AN_TIMEOUT_5714S;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005153 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07005154
5155 return err;
5156 }
5157 } else {
5158 u32 new_bmcr;
5159
5160 bmcr &= ~BMCR_SPEED1000;
5161 new_bmcr = bmcr & ~(BMCR_ANENABLE | BMCR_FULLDPLX);
5162
5163 if (tp->link_config.duplex == DUPLEX_FULL)
5164 new_bmcr |= BMCR_FULLDPLX;
5165
5166 if (new_bmcr != bmcr) {
5167 /* BMCR_SPEED1000 is a reserved bit that needs
5168 * to be set on write.
5169 */
5170 new_bmcr |= BMCR_SPEED1000;
5171
5172 /* Force a linkdown */
5173 if (netif_carrier_ok(tp->dev)) {
5174 u32 adv;
5175
5176 err |= tg3_readphy(tp, MII_ADVERTISE, &adv);
5177 adv &= ~(ADVERTISE_1000XFULL |
5178 ADVERTISE_1000XHALF |
5179 ADVERTISE_SLCT);
5180 tg3_writephy(tp, MII_ADVERTISE, adv);
5181 tg3_writephy(tp, MII_BMCR, bmcr |
5182 BMCR_ANRESTART |
5183 BMCR_ANENABLE);
5184 udelay(10);
5185 netif_carrier_off(tp->dev);
5186 }
5187 tg3_writephy(tp, MII_BMCR, new_bmcr);
5188 bmcr = new_bmcr;
5189 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
5190 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
Michael Chand4d2c552006-03-20 17:47:20 -08005191 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
5192 ASIC_REV_5714) {
5193 if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
5194 bmsr |= BMSR_LSTATUS;
5195 else
5196 bmsr &= ~BMSR_LSTATUS;
5197 }
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005198 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07005199 }
5200 }
5201
5202 if (bmsr & BMSR_LSTATUS) {
5203 current_speed = SPEED_1000;
5204 current_link_up = 1;
5205 if (bmcr & BMCR_FULLDPLX)
5206 current_duplex = DUPLEX_FULL;
5207 else
5208 current_duplex = DUPLEX_HALF;
5209
Matt Carlsonef167e22007-12-20 20:10:01 -08005210 local_adv = 0;
5211 remote_adv = 0;
5212
Michael Chan747e8f82005-07-25 12:33:22 -07005213 if (bmcr & BMCR_ANENABLE) {
Matt Carlsonef167e22007-12-20 20:10:01 -08005214 u32 common;
Michael Chan747e8f82005-07-25 12:33:22 -07005215
5216 err |= tg3_readphy(tp, MII_ADVERTISE, &local_adv);
5217 err |= tg3_readphy(tp, MII_LPA, &remote_adv);
5218 common = local_adv & remote_adv;
5219 if (common & (ADVERTISE_1000XHALF |
5220 ADVERTISE_1000XFULL)) {
5221 if (common & ADVERTISE_1000XFULL)
5222 current_duplex = DUPLEX_FULL;
5223 else
5224 current_duplex = DUPLEX_HALF;
Matt Carlson859edb22011-12-08 14:40:16 +00005225
5226 tp->link_config.rmt_adv =
5227 mii_adv_to_ethtool_adv_x(remote_adv);
Joe Perches63c3a662011-04-26 08:12:10 +00005228 } else if (!tg3_flag(tp, 5780_CLASS)) {
Matt Carlson57d8b882010-06-05 17:24:35 +00005229 /* Link is up via parallel detect */
Matt Carlson859a588792010-04-05 10:19:28 +00005230 } else {
Michael Chan747e8f82005-07-25 12:33:22 -07005231 current_link_up = 0;
Matt Carlson859a588792010-04-05 10:19:28 +00005232 }
Michael Chan747e8f82005-07-25 12:33:22 -07005233 }
5234 }
5235
Matt Carlsonef167e22007-12-20 20:10:01 -08005236 if (current_link_up == 1 && current_duplex == DUPLEX_FULL)
5237 tg3_setup_flow_control(tp, local_adv, remote_adv);
5238
Michael Chan747e8f82005-07-25 12:33:22 -07005239 tp->mac_mode &= ~MAC_MODE_HALF_DUPLEX;
5240 if (tp->link_config.active_duplex == DUPLEX_HALF)
5241 tp->mac_mode |= MAC_MODE_HALF_DUPLEX;
5242
5243 tw32_f(MAC_MODE, tp->mac_mode);
5244 udelay(40);
5245
5246 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
5247
5248 tp->link_config.active_speed = current_speed;
5249 tp->link_config.active_duplex = current_duplex;
5250
5251 if (current_link_up != netif_carrier_ok(tp->dev)) {
5252 if (current_link_up)
5253 netif_carrier_on(tp->dev);
5254 else {
5255 netif_carrier_off(tp->dev);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005256 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07005257 }
5258 tg3_link_report(tp);
5259 }
5260 return err;
5261}
5262
5263static void tg3_serdes_parallel_detect(struct tg3 *tp)
5264{
Michael Chan3d3ebe72006-09-27 15:59:15 -07005265 if (tp->serdes_counter) {
Michael Chan747e8f82005-07-25 12:33:22 -07005266 /* Give autoneg time to complete. */
Michael Chan3d3ebe72006-09-27 15:59:15 -07005267 tp->serdes_counter--;
Michael Chan747e8f82005-07-25 12:33:22 -07005268 return;
5269 }
Matt Carlsonc6cdf432010-04-05 10:19:26 +00005270
Michael Chan747e8f82005-07-25 12:33:22 -07005271 if (!netif_carrier_ok(tp->dev) &&
5272 (tp->link_config.autoneg == AUTONEG_ENABLE)) {
5273 u32 bmcr;
5274
5275 tg3_readphy(tp, MII_BMCR, &bmcr);
5276 if (bmcr & BMCR_ANENABLE) {
5277 u32 phy1, phy2;
5278
5279 /* Select shadow register 0x1f */
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00005280 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x7c00);
5281 tg3_readphy(tp, MII_TG3_MISC_SHDW, &phy1);
Michael Chan747e8f82005-07-25 12:33:22 -07005282
5283 /* Select expansion interrupt status register */
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00005284 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
5285 MII_TG3_DSP_EXP1_INT_STAT);
5286 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &phy2);
5287 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &phy2);
Michael Chan747e8f82005-07-25 12:33:22 -07005288
5289 if ((phy1 & 0x10) && !(phy2 & 0x20)) {
5290 /* We have signal detect and not receiving
5291 * config code words, link is up by parallel
5292 * detection.
5293 */
5294
5295 bmcr &= ~BMCR_ANENABLE;
5296 bmcr |= BMCR_SPEED1000 | BMCR_FULLDPLX;
5297 tg3_writephy(tp, MII_BMCR, bmcr);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005298 tp->phy_flags |= TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07005299 }
5300 }
Matt Carlson859a588792010-04-05 10:19:28 +00005301 } else if (netif_carrier_ok(tp->dev) &&
5302 (tp->link_config.autoneg == AUTONEG_ENABLE) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005303 (tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT)) {
Michael Chan747e8f82005-07-25 12:33:22 -07005304 u32 phy2;
5305
5306 /* Select expansion interrupt status register */
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00005307 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
5308 MII_TG3_DSP_EXP1_INT_STAT);
5309 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &phy2);
Michael Chan747e8f82005-07-25 12:33:22 -07005310 if (phy2 & 0x20) {
5311 u32 bmcr;
5312
5313 /* Config code words received, turn on autoneg. */
5314 tg3_readphy(tp, MII_BMCR, &bmcr);
5315 tg3_writephy(tp, MII_BMCR, bmcr | BMCR_ANENABLE);
5316
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005317 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07005318
5319 }
5320 }
5321}
5322
Linus Torvalds1da177e2005-04-16 15:20:36 -07005323static int tg3_setup_phy(struct tg3 *tp, int force_reset)
5324{
Matt Carlsonf2096f92011-04-05 14:22:48 +00005325 u32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005326 int err;
5327
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005328 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005329 err = tg3_setup_fiber_phy(tp, force_reset);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005330 else if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
Michael Chan747e8f82005-07-25 12:33:22 -07005331 err = tg3_setup_fiber_mii_phy(tp, force_reset);
Matt Carlson859a588792010-04-05 10:19:28 +00005332 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07005333 err = tg3_setup_copper_phy(tp, force_reset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005334
Matt Carlsonbcb37f62008-11-03 16:52:09 -08005335 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX) {
Matt Carlsonf2096f92011-04-05 14:22:48 +00005336 u32 scale;
Matt Carlsonaa6c91f2007-11-12 21:18:04 -08005337
5338 val = tr32(TG3_CPMU_CLCK_STAT) & CPMU_CLCK_STAT_MAC_CLCK_MASK;
5339 if (val == CPMU_CLCK_STAT_MAC_CLCK_62_5)
5340 scale = 65;
5341 else if (val == CPMU_CLCK_STAT_MAC_CLCK_6_25)
5342 scale = 6;
5343 else
5344 scale = 12;
5345
5346 val = tr32(GRC_MISC_CFG) & ~GRC_MISC_CFG_PRESCALAR_MASK;
5347 val |= (scale << GRC_MISC_CFG_PRESCALAR_SHIFT);
5348 tw32(GRC_MISC_CFG, val);
5349 }
5350
Matt Carlsonf2096f92011-04-05 14:22:48 +00005351 val = (2 << TX_LENGTHS_IPG_CRS_SHIFT) |
5352 (6 << TX_LENGTHS_IPG_SHIFT);
5353 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
5354 val |= tr32(MAC_TX_LENGTHS) &
5355 (TX_LENGTHS_JMB_FRM_LEN_MSK |
5356 TX_LENGTHS_CNT_DWN_VAL_MSK);
5357
Linus Torvalds1da177e2005-04-16 15:20:36 -07005358 if (tp->link_config.active_speed == SPEED_1000 &&
5359 tp->link_config.active_duplex == DUPLEX_HALF)
Matt Carlsonf2096f92011-04-05 14:22:48 +00005360 tw32(MAC_TX_LENGTHS, val |
5361 (0xff << TX_LENGTHS_SLOT_TIME_SHIFT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005362 else
Matt Carlsonf2096f92011-04-05 14:22:48 +00005363 tw32(MAC_TX_LENGTHS, val |
5364 (32 << TX_LENGTHS_SLOT_TIME_SHIFT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005365
Joe Perches63c3a662011-04-26 08:12:10 +00005366 if (!tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005367 if (netif_carrier_ok(tp->dev)) {
5368 tw32(HOSTCC_STAT_COAL_TICKS,
David S. Miller15f98502005-05-18 22:49:26 -07005369 tp->coal.stats_block_coalesce_usecs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005370 } else {
5371 tw32(HOSTCC_STAT_COAL_TICKS, 0);
5372 }
5373 }
5374
Joe Perches63c3a662011-04-26 08:12:10 +00005375 if (tg3_flag(tp, ASPM_WORKAROUND)) {
Matt Carlsonf2096f92011-04-05 14:22:48 +00005376 val = tr32(PCIE_PWR_MGMT_THRESH);
Matt Carlson8ed5d972007-05-07 00:25:49 -07005377 if (!netif_carrier_ok(tp->dev))
5378 val = (val & ~PCIE_PWR_MGMT_L1_THRESH_MSK) |
5379 tp->pwrmgmt_thresh;
5380 else
5381 val |= PCIE_PWR_MGMT_L1_THRESH_MSK;
5382 tw32(PCIE_PWR_MGMT_THRESH, val);
5383 }
5384
Linus Torvalds1da177e2005-04-16 15:20:36 -07005385 return err;
5386}
5387
Matt Carlson66cfd1b2010-09-30 10:34:30 +00005388static inline int tg3_irq_sync(struct tg3 *tp)
5389{
5390 return tp->irq_sync;
5391}
5392
Matt Carlson97bd8e42011-04-13 11:05:04 +00005393static inline void tg3_rd32_loop(struct tg3 *tp, u32 *dst, u32 off, u32 len)
5394{
5395 int i;
5396
5397 dst = (u32 *)((u8 *)dst + off);
5398 for (i = 0; i < len; i += sizeof(u32))
5399 *dst++ = tr32(off + i);
5400}
5401
5402static void tg3_dump_legacy_regs(struct tg3 *tp, u32 *regs)
5403{
5404 tg3_rd32_loop(tp, regs, TG3PCI_VENDOR, 0xb0);
5405 tg3_rd32_loop(tp, regs, MAILBOX_INTERRUPT_0, 0x200);
5406 tg3_rd32_loop(tp, regs, MAC_MODE, 0x4f0);
5407 tg3_rd32_loop(tp, regs, SNDDATAI_MODE, 0xe0);
5408 tg3_rd32_loop(tp, regs, SNDDATAC_MODE, 0x04);
5409 tg3_rd32_loop(tp, regs, SNDBDS_MODE, 0x80);
5410 tg3_rd32_loop(tp, regs, SNDBDI_MODE, 0x48);
5411 tg3_rd32_loop(tp, regs, SNDBDC_MODE, 0x04);
5412 tg3_rd32_loop(tp, regs, RCVLPC_MODE, 0x20);
5413 tg3_rd32_loop(tp, regs, RCVLPC_SELLST_BASE, 0x15c);
5414 tg3_rd32_loop(tp, regs, RCVDBDI_MODE, 0x0c);
5415 tg3_rd32_loop(tp, regs, RCVDBDI_JUMBO_BD, 0x3c);
5416 tg3_rd32_loop(tp, regs, RCVDBDI_BD_PROD_IDX_0, 0x44);
5417 tg3_rd32_loop(tp, regs, RCVDCC_MODE, 0x04);
5418 tg3_rd32_loop(tp, regs, RCVBDI_MODE, 0x20);
5419 tg3_rd32_loop(tp, regs, RCVCC_MODE, 0x14);
5420 tg3_rd32_loop(tp, regs, RCVLSC_MODE, 0x08);
5421 tg3_rd32_loop(tp, regs, MBFREE_MODE, 0x08);
5422 tg3_rd32_loop(tp, regs, HOSTCC_MODE, 0x100);
5423
Joe Perches63c3a662011-04-26 08:12:10 +00005424 if (tg3_flag(tp, SUPPORT_MSIX))
Matt Carlson97bd8e42011-04-13 11:05:04 +00005425 tg3_rd32_loop(tp, regs, HOSTCC_RXCOL_TICKS_VEC1, 0x180);
5426
5427 tg3_rd32_loop(tp, regs, MEMARB_MODE, 0x10);
5428 tg3_rd32_loop(tp, regs, BUFMGR_MODE, 0x58);
5429 tg3_rd32_loop(tp, regs, RDMAC_MODE, 0x08);
5430 tg3_rd32_loop(tp, regs, WDMAC_MODE, 0x08);
5431 tg3_rd32_loop(tp, regs, RX_CPU_MODE, 0x04);
5432 tg3_rd32_loop(tp, regs, RX_CPU_STATE, 0x04);
5433 tg3_rd32_loop(tp, regs, RX_CPU_PGMCTR, 0x04);
5434 tg3_rd32_loop(tp, regs, RX_CPU_HWBKPT, 0x04);
5435
Joe Perches63c3a662011-04-26 08:12:10 +00005436 if (!tg3_flag(tp, 5705_PLUS)) {
Matt Carlson97bd8e42011-04-13 11:05:04 +00005437 tg3_rd32_loop(tp, regs, TX_CPU_MODE, 0x04);
5438 tg3_rd32_loop(tp, regs, TX_CPU_STATE, 0x04);
5439 tg3_rd32_loop(tp, regs, TX_CPU_PGMCTR, 0x04);
5440 }
5441
5442 tg3_rd32_loop(tp, regs, GRCMBOX_INTERRUPT_0, 0x110);
5443 tg3_rd32_loop(tp, regs, FTQ_RESET, 0x120);
5444 tg3_rd32_loop(tp, regs, MSGINT_MODE, 0x0c);
5445 tg3_rd32_loop(tp, regs, DMAC_MODE, 0x04);
5446 tg3_rd32_loop(tp, regs, GRC_MODE, 0x4c);
5447
Joe Perches63c3a662011-04-26 08:12:10 +00005448 if (tg3_flag(tp, NVRAM))
Matt Carlson97bd8e42011-04-13 11:05:04 +00005449 tg3_rd32_loop(tp, regs, NVRAM_CMD, 0x24);
5450}
5451
5452static void tg3_dump_state(struct tg3 *tp)
5453{
5454 int i;
5455 u32 *regs;
5456
5457 regs = kzalloc(TG3_REG_BLK_SIZE, GFP_ATOMIC);
5458 if (!regs) {
5459 netdev_err(tp->dev, "Failed allocating register dump buffer\n");
5460 return;
5461 }
5462
Joe Perches63c3a662011-04-26 08:12:10 +00005463 if (tg3_flag(tp, PCI_EXPRESS)) {
Matt Carlson97bd8e42011-04-13 11:05:04 +00005464 /* Read up to but not including private PCI registers */
5465 for (i = 0; i < TG3_PCIE_TLDLPL_PORT; i += sizeof(u32))
5466 regs[i / sizeof(u32)] = tr32(i);
5467 } else
5468 tg3_dump_legacy_regs(tp, regs);
5469
5470 for (i = 0; i < TG3_REG_BLK_SIZE / sizeof(u32); i += 4) {
5471 if (!regs[i + 0] && !regs[i + 1] &&
5472 !regs[i + 2] && !regs[i + 3])
5473 continue;
5474
5475 netdev_err(tp->dev, "0x%08x: 0x%08x, 0x%08x, 0x%08x, 0x%08x\n",
5476 i * 4,
5477 regs[i + 0], regs[i + 1], regs[i + 2], regs[i + 3]);
5478 }
5479
5480 kfree(regs);
5481
5482 for (i = 0; i < tp->irq_cnt; i++) {
5483 struct tg3_napi *tnapi = &tp->napi[i];
5484
5485 /* SW status block */
5486 netdev_err(tp->dev,
5487 "%d: Host status block [%08x:%08x:(%04x:%04x:%04x):(%04x:%04x)]\n",
5488 i,
5489 tnapi->hw_status->status,
5490 tnapi->hw_status->status_tag,
5491 tnapi->hw_status->rx_jumbo_consumer,
5492 tnapi->hw_status->rx_consumer,
5493 tnapi->hw_status->rx_mini_consumer,
5494 tnapi->hw_status->idx[0].rx_producer,
5495 tnapi->hw_status->idx[0].tx_consumer);
5496
5497 netdev_err(tp->dev,
5498 "%d: NAPI info [%08x:%08x:(%04x:%04x:%04x):%04x:(%04x:%04x:%04x:%04x)]\n",
5499 i,
5500 tnapi->last_tag, tnapi->last_irq_tag,
5501 tnapi->tx_prod, tnapi->tx_cons, tnapi->tx_pending,
5502 tnapi->rx_rcb_ptr,
5503 tnapi->prodring.rx_std_prod_idx,
5504 tnapi->prodring.rx_std_cons_idx,
5505 tnapi->prodring.rx_jmb_prod_idx,
5506 tnapi->prodring.rx_jmb_cons_idx);
5507 }
5508}
5509
Michael Chandf3e6542006-05-26 17:48:07 -07005510/* This is called whenever we suspect that the system chipset is re-
5511 * ordering the sequence of MMIO to the tx send mailbox. The symptom
5512 * is bogus tx completions. We try to recover by setting the
5513 * TG3_FLAG_MBOX_WRITE_REORDER flag and resetting the chip later
5514 * in the workqueue.
5515 */
5516static void tg3_tx_recover(struct tg3 *tp)
5517{
Joe Perches63c3a662011-04-26 08:12:10 +00005518 BUG_ON(tg3_flag(tp, MBOX_WRITE_REORDER) ||
Michael Chandf3e6542006-05-26 17:48:07 -07005519 tp->write32_tx_mbox == tg3_write_indirect_mbox);
5520
Matt Carlson5129c3a2010-04-05 10:19:23 +00005521 netdev_warn(tp->dev,
5522 "The system may be re-ordering memory-mapped I/O "
5523 "cycles to the network device, attempting to recover. "
5524 "Please report the problem to the driver maintainer "
5525 "and include system chipset information.\n");
Michael Chandf3e6542006-05-26 17:48:07 -07005526
5527 spin_lock(&tp->lock);
Joe Perches63c3a662011-04-26 08:12:10 +00005528 tg3_flag_set(tp, TX_RECOVERY_PENDING);
Michael Chandf3e6542006-05-26 17:48:07 -07005529 spin_unlock(&tp->lock);
5530}
5531
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005532static inline u32 tg3_tx_avail(struct tg3_napi *tnapi)
Michael Chan1b2a7202006-08-07 21:46:02 -07005533{
Matt Carlsonf65aac12010-08-02 11:26:03 +00005534 /* Tell compiler to fetch tx indices from memory. */
5535 barrier();
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005536 return tnapi->tx_pending -
5537 ((tnapi->tx_prod - tnapi->tx_cons) & (TG3_TX_RING_SIZE - 1));
Michael Chan1b2a7202006-08-07 21:46:02 -07005538}
5539
Linus Torvalds1da177e2005-04-16 15:20:36 -07005540/* Tigon3 never reports partial packet sends. So we do not
5541 * need special logic to handle SKBs that have not had all
5542 * of their frags sent yet, like SunGEM does.
5543 */
Matt Carlson17375d22009-08-28 14:02:18 +00005544static void tg3_tx(struct tg3_napi *tnapi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005545{
Matt Carlson17375d22009-08-28 14:02:18 +00005546 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00005547 u32 hw_idx = tnapi->hw_status->idx[0].tx_consumer;
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005548 u32 sw_idx = tnapi->tx_cons;
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005549 struct netdev_queue *txq;
5550 int index = tnapi - tp->napi;
Tom Herbert298376d2011-11-28 16:33:30 +00005551 unsigned int pkts_compl = 0, bytes_compl = 0;
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005552
Joe Perches63c3a662011-04-26 08:12:10 +00005553 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005554 index--;
5555
5556 txq = netdev_get_tx_queue(tp->dev, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005557
5558 while (sw_idx != hw_idx) {
Matt Carlsondf8944c2011-07-27 14:20:46 +00005559 struct tg3_tx_ring_info *ri = &tnapi->tx_buffers[sw_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07005560 struct sk_buff *skb = ri->skb;
Michael Chandf3e6542006-05-26 17:48:07 -07005561 int i, tx_bug = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005562
Michael Chandf3e6542006-05-26 17:48:07 -07005563 if (unlikely(skb == NULL)) {
5564 tg3_tx_recover(tp);
5565 return;
5566 }
5567
Alexander Duyckf4188d82009-12-02 16:48:38 +00005568 pci_unmap_single(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005569 dma_unmap_addr(ri, mapping),
Alexander Duyckf4188d82009-12-02 16:48:38 +00005570 skb_headlen(skb),
5571 PCI_DMA_TODEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005572
5573 ri->skb = NULL;
5574
Matt Carlsone01ee142011-07-27 14:20:50 +00005575 while (ri->fragmented) {
5576 ri->fragmented = false;
5577 sw_idx = NEXT_TX(sw_idx);
5578 ri = &tnapi->tx_buffers[sw_idx];
5579 }
5580
Linus Torvalds1da177e2005-04-16 15:20:36 -07005581 sw_idx = NEXT_TX(sw_idx);
5582
5583 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005584 ri = &tnapi->tx_buffers[sw_idx];
Michael Chandf3e6542006-05-26 17:48:07 -07005585 if (unlikely(ri->skb != NULL || sw_idx == hw_idx))
5586 tx_bug = 1;
Alexander Duyckf4188d82009-12-02 16:48:38 +00005587
5588 pci_unmap_page(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005589 dma_unmap_addr(ri, mapping),
Eric Dumazet9e903e02011-10-18 21:00:24 +00005590 skb_frag_size(&skb_shinfo(skb)->frags[i]),
Alexander Duyckf4188d82009-12-02 16:48:38 +00005591 PCI_DMA_TODEVICE);
Matt Carlsone01ee142011-07-27 14:20:50 +00005592
5593 while (ri->fragmented) {
5594 ri->fragmented = false;
5595 sw_idx = NEXT_TX(sw_idx);
5596 ri = &tnapi->tx_buffers[sw_idx];
5597 }
5598
Linus Torvalds1da177e2005-04-16 15:20:36 -07005599 sw_idx = NEXT_TX(sw_idx);
5600 }
5601
Tom Herbert298376d2011-11-28 16:33:30 +00005602 pkts_compl++;
5603 bytes_compl += skb->len;
5604
David S. Millerf47c11e2005-06-24 20:18:35 -07005605 dev_kfree_skb(skb);
Michael Chandf3e6542006-05-26 17:48:07 -07005606
5607 if (unlikely(tx_bug)) {
5608 tg3_tx_recover(tp);
5609 return;
5610 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005611 }
5612
Tom Herbert5cb917b2012-03-05 19:53:50 +00005613 netdev_tx_completed_queue(txq, pkts_compl, bytes_compl);
Tom Herbert298376d2011-11-28 16:33:30 +00005614
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005615 tnapi->tx_cons = sw_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005616
Michael Chan1b2a7202006-08-07 21:46:02 -07005617 /* Need to make the tx_cons update visible to tg3_start_xmit()
5618 * before checking for netif_queue_stopped(). Without the
5619 * memory barrier, there is a small possibility that tg3_start_xmit()
5620 * will miss it and cause the queue to be stopped forever.
5621 */
5622 smp_mb();
5623
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005624 if (unlikely(netif_tx_queue_stopped(txq) &&
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005625 (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi)))) {
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005626 __netif_tx_lock(txq, smp_processor_id());
5627 if (netif_tx_queue_stopped(txq) &&
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005628 (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi)))
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005629 netif_tx_wake_queue(txq);
5630 __netif_tx_unlock(txq);
Michael Chan51b91462005-09-01 17:41:28 -07005631 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005632}
5633
Eric Dumazet8d4057a2012-04-27 00:34:49 +00005634static void *tg3_frag_alloc(struct tg3_rx_prodring_set *tpr)
5635{
5636 void *data;
5637
5638 if (tpr->rx_page_size < TG3_FRAGSIZE) {
5639 struct page *page = alloc_page(GFP_ATOMIC);
5640
5641 if (!page)
5642 return NULL;
5643 atomic_add((PAGE_SIZE / TG3_FRAGSIZE) - 1, &page->_count);
5644 tpr->rx_page_addr = page_address(page);
5645 tpr->rx_page_size = PAGE_SIZE;
5646 }
5647 data = tpr->rx_page_addr;
5648 tpr->rx_page_addr += TG3_FRAGSIZE;
5649 tpr->rx_page_size -= TG3_FRAGSIZE;
5650 return data;
5651}
5652
5653static void tg3_frag_free(bool is_frag, void *data)
5654{
5655 if (is_frag)
5656 put_page(virt_to_head_page(data));
5657 else
5658 kfree(data);
5659}
5660
Eric Dumazet9205fd92011-11-18 06:47:01 +00005661static void tg3_rx_data_free(struct tg3 *tp, struct ring_info *ri, u32 map_sz)
Matt Carlson2b2cdb62009-11-13 13:03:48 +00005662{
Eric Dumazet8d4057a2012-04-27 00:34:49 +00005663 unsigned int skb_size = SKB_DATA_ALIGN(map_sz + TG3_RX_OFFSET(tp)) +
5664 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
5665
Eric Dumazet9205fd92011-11-18 06:47:01 +00005666 if (!ri->data)
Matt Carlson2b2cdb62009-11-13 13:03:48 +00005667 return;
5668
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005669 pci_unmap_single(tp->pdev, dma_unmap_addr(ri, mapping),
Matt Carlson2b2cdb62009-11-13 13:03:48 +00005670 map_sz, PCI_DMA_FROMDEVICE);
Eric Dumazet8d4057a2012-04-27 00:34:49 +00005671 tg3_frag_free(skb_size <= TG3_FRAGSIZE, ri->data);
Eric Dumazet9205fd92011-11-18 06:47:01 +00005672 ri->data = NULL;
Matt Carlson2b2cdb62009-11-13 13:03:48 +00005673}
5674
Eric Dumazet8d4057a2012-04-27 00:34:49 +00005675
Linus Torvalds1da177e2005-04-16 15:20:36 -07005676/* Returns size of skb allocated or < 0 on error.
5677 *
5678 * We only need to fill in the address because the other members
5679 * of the RX descriptor are invariant, see tg3_init_rings.
5680 *
5681 * Note the purposeful assymetry of cpu vs. chip accesses. For
5682 * posting buffers we only dirty the first cache line of the RX
5683 * descriptor (containing the address). Whereas for the RX status
5684 * buffers the cpu only reads the last cacheline of the RX descriptor
5685 * (to fetch the error flags, vlan tag, checksum, and opaque cookie).
5686 */
Eric Dumazet9205fd92011-11-18 06:47:01 +00005687static int tg3_alloc_rx_data(struct tg3 *tp, struct tg3_rx_prodring_set *tpr,
Eric Dumazet8d4057a2012-04-27 00:34:49 +00005688 u32 opaque_key, u32 dest_idx_unmasked,
5689 unsigned int *frag_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005690{
5691 struct tg3_rx_buffer_desc *desc;
Matt Carlsonf94e2902010-10-14 10:37:42 +00005692 struct ring_info *map;
Eric Dumazet9205fd92011-11-18 06:47:01 +00005693 u8 *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005694 dma_addr_t mapping;
Eric Dumazet9205fd92011-11-18 06:47:01 +00005695 int skb_size, data_size, dest_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005696
Linus Torvalds1da177e2005-04-16 15:20:36 -07005697 switch (opaque_key) {
5698 case RXD_OPAQUE_RING_STD:
Matt Carlson2c49a442010-09-30 10:34:35 +00005699 dest_idx = dest_idx_unmasked & tp->rx_std_ring_mask;
Matt Carlson21f581a2009-08-28 14:00:25 +00005700 desc = &tpr->rx_std[dest_idx];
5701 map = &tpr->rx_std_buffers[dest_idx];
Eric Dumazet9205fd92011-11-18 06:47:01 +00005702 data_size = tp->rx_pkt_map_sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005703 break;
5704
5705 case RXD_OPAQUE_RING_JUMBO:
Matt Carlson2c49a442010-09-30 10:34:35 +00005706 dest_idx = dest_idx_unmasked & tp->rx_jmb_ring_mask;
Matt Carlson79ed5ac2009-08-28 14:00:55 +00005707 desc = &tpr->rx_jmb[dest_idx].std;
Matt Carlson21f581a2009-08-28 14:00:25 +00005708 map = &tpr->rx_jmb_buffers[dest_idx];
Eric Dumazet9205fd92011-11-18 06:47:01 +00005709 data_size = TG3_RX_JMB_MAP_SZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005710 break;
5711
5712 default:
5713 return -EINVAL;
Stephen Hemminger855e1112008-04-16 16:37:28 -07005714 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005715
5716 /* Do not overwrite any of the map or rp information
5717 * until we are sure we can commit to a new buffer.
5718 *
5719 * Callers depend upon this behavior and assume that
5720 * we leave everything unchanged if we fail.
5721 */
Eric Dumazet9205fd92011-11-18 06:47:01 +00005722 skb_size = SKB_DATA_ALIGN(data_size + TG3_RX_OFFSET(tp)) +
5723 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
Eric Dumazet8d4057a2012-04-27 00:34:49 +00005724 if (skb_size <= TG3_FRAGSIZE) {
5725 data = tg3_frag_alloc(tpr);
5726 *frag_size = TG3_FRAGSIZE;
5727 } else {
5728 data = kmalloc(skb_size, GFP_ATOMIC);
5729 *frag_size = 0;
5730 }
Eric Dumazet9205fd92011-11-18 06:47:01 +00005731 if (!data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005732 return -ENOMEM;
5733
Eric Dumazet9205fd92011-11-18 06:47:01 +00005734 mapping = pci_map_single(tp->pdev,
5735 data + TG3_RX_OFFSET(tp),
5736 data_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005737 PCI_DMA_FROMDEVICE);
Eric Dumazet8d4057a2012-04-27 00:34:49 +00005738 if (unlikely(pci_dma_mapping_error(tp->pdev, mapping))) {
5739 tg3_frag_free(skb_size <= TG3_FRAGSIZE, data);
Matt Carlsona21771d2009-11-02 14:25:31 +00005740 return -EIO;
5741 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005742
Eric Dumazet9205fd92011-11-18 06:47:01 +00005743 map->data = data;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005744 dma_unmap_addr_set(map, mapping, mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005745
Linus Torvalds1da177e2005-04-16 15:20:36 -07005746 desc->addr_hi = ((u64)mapping >> 32);
5747 desc->addr_lo = ((u64)mapping & 0xffffffff);
5748
Eric Dumazet9205fd92011-11-18 06:47:01 +00005749 return data_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005750}
5751
5752/* We only need to move over in the address because the other
5753 * members of the RX descriptor are invariant. See notes above
Eric Dumazet9205fd92011-11-18 06:47:01 +00005754 * tg3_alloc_rx_data for full details.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005755 */
Matt Carlsona3896162009-11-13 13:03:44 +00005756static void tg3_recycle_rx(struct tg3_napi *tnapi,
5757 struct tg3_rx_prodring_set *dpr,
5758 u32 opaque_key, int src_idx,
5759 u32 dest_idx_unmasked)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005760{
Matt Carlson17375d22009-08-28 14:02:18 +00005761 struct tg3 *tp = tnapi->tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005762 struct tg3_rx_buffer_desc *src_desc, *dest_desc;
5763 struct ring_info *src_map, *dest_map;
Matt Carlson8fea32b2010-09-15 08:59:58 +00005764 struct tg3_rx_prodring_set *spr = &tp->napi[0].prodring;
Matt Carlsonc6cdf432010-04-05 10:19:26 +00005765 int dest_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005766
5767 switch (opaque_key) {
5768 case RXD_OPAQUE_RING_STD:
Matt Carlson2c49a442010-09-30 10:34:35 +00005769 dest_idx = dest_idx_unmasked & tp->rx_std_ring_mask;
Matt Carlsona3896162009-11-13 13:03:44 +00005770 dest_desc = &dpr->rx_std[dest_idx];
5771 dest_map = &dpr->rx_std_buffers[dest_idx];
5772 src_desc = &spr->rx_std[src_idx];
5773 src_map = &spr->rx_std_buffers[src_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07005774 break;
5775
5776 case RXD_OPAQUE_RING_JUMBO:
Matt Carlson2c49a442010-09-30 10:34:35 +00005777 dest_idx = dest_idx_unmasked & tp->rx_jmb_ring_mask;
Matt Carlsona3896162009-11-13 13:03:44 +00005778 dest_desc = &dpr->rx_jmb[dest_idx].std;
5779 dest_map = &dpr->rx_jmb_buffers[dest_idx];
5780 src_desc = &spr->rx_jmb[src_idx].std;
5781 src_map = &spr->rx_jmb_buffers[src_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07005782 break;
5783
5784 default:
5785 return;
Stephen Hemminger855e1112008-04-16 16:37:28 -07005786 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005787
Eric Dumazet9205fd92011-11-18 06:47:01 +00005788 dest_map->data = src_map->data;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005789 dma_unmap_addr_set(dest_map, mapping,
5790 dma_unmap_addr(src_map, mapping));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005791 dest_desc->addr_hi = src_desc->addr_hi;
5792 dest_desc->addr_lo = src_desc->addr_lo;
Matt Carlsone92967b2010-02-12 14:47:06 +00005793
5794 /* Ensure that the update to the skb happens after the physical
5795 * addresses have been transferred to the new BD location.
5796 */
5797 smp_wmb();
5798
Eric Dumazet9205fd92011-11-18 06:47:01 +00005799 src_map->data = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005800}
5801
Linus Torvalds1da177e2005-04-16 15:20:36 -07005802/* The RX ring scheme is composed of multiple rings which post fresh
5803 * buffers to the chip, and one special ring the chip uses to report
5804 * status back to the host.
5805 *
5806 * The special ring reports the status of received packets to the
5807 * host. The chip does not write into the original descriptor the
5808 * RX buffer was obtained from. The chip simply takes the original
5809 * descriptor as provided by the host, updates the status and length
5810 * field, then writes this into the next status ring entry.
5811 *
5812 * Each ring the host uses to post buffers to the chip is described
5813 * by a TG3_BDINFO entry in the chips SRAM area. When a packet arrives,
5814 * it is first placed into the on-chip ram. When the packet's length
5815 * is known, it walks down the TG3_BDINFO entries to select the ring.
5816 * Each TG3_BDINFO specifies a MAXLEN field and the first TG3_BDINFO
5817 * which is within the range of the new packet's length is chosen.
5818 *
5819 * The "separate ring for rx status" scheme may sound queer, but it makes
5820 * sense from a cache coherency perspective. If only the host writes
5821 * to the buffer post rings, and only the chip writes to the rx status
5822 * rings, then cache lines never move beyond shared-modified state.
5823 * If both the host and chip were to write into the same ring, cache line
5824 * eviction could occur since both entities want it in an exclusive state.
5825 */
Matt Carlson17375d22009-08-28 14:02:18 +00005826static int tg3_rx(struct tg3_napi *tnapi, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005827{
Matt Carlson17375d22009-08-28 14:02:18 +00005828 struct tg3 *tp = tnapi->tp;
Michael Chanf92905d2006-06-29 20:14:29 -07005829 u32 work_mask, rx_std_posted = 0;
Matt Carlson43619352009-11-13 13:03:47 +00005830 u32 std_prod_idx, jmb_prod_idx;
Matt Carlson72334482009-08-28 14:03:01 +00005831 u32 sw_idx = tnapi->rx_rcb_ptr;
Michael Chan483ba502005-04-25 15:14:03 -07005832 u16 hw_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005833 int received;
Matt Carlson8fea32b2010-09-15 08:59:58 +00005834 struct tg3_rx_prodring_set *tpr = &tnapi->prodring;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005835
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00005836 hw_idx = *(tnapi->rx_rcb_prod_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005837 /*
5838 * We need to order the read of hw_idx and the read of
5839 * the opaque cookie.
5840 */
5841 rmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005842 work_mask = 0;
5843 received = 0;
Matt Carlson43619352009-11-13 13:03:47 +00005844 std_prod_idx = tpr->rx_std_prod_idx;
5845 jmb_prod_idx = tpr->rx_jmb_prod_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005846 while (sw_idx != hw_idx && budget > 0) {
Matt Carlsonafc081f2009-11-13 13:03:43 +00005847 struct ring_info *ri;
Matt Carlson72334482009-08-28 14:03:01 +00005848 struct tg3_rx_buffer_desc *desc = &tnapi->rx_rcb[sw_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07005849 unsigned int len;
5850 struct sk_buff *skb;
5851 dma_addr_t dma_addr;
5852 u32 opaque_key, desc_idx, *post_ptr;
Eric Dumazet9205fd92011-11-18 06:47:01 +00005853 u8 *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005854
5855 desc_idx = desc->opaque & RXD_OPAQUE_INDEX_MASK;
5856 opaque_key = desc->opaque & RXD_OPAQUE_RING_MASK;
5857 if (opaque_key == RXD_OPAQUE_RING_STD) {
Matt Carlson8fea32b2010-09-15 08:59:58 +00005858 ri = &tp->napi[0].prodring.rx_std_buffers[desc_idx];
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005859 dma_addr = dma_unmap_addr(ri, mapping);
Eric Dumazet9205fd92011-11-18 06:47:01 +00005860 data = ri->data;
Matt Carlson43619352009-11-13 13:03:47 +00005861 post_ptr = &std_prod_idx;
Michael Chanf92905d2006-06-29 20:14:29 -07005862 rx_std_posted++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005863 } else if (opaque_key == RXD_OPAQUE_RING_JUMBO) {
Matt Carlson8fea32b2010-09-15 08:59:58 +00005864 ri = &tp->napi[0].prodring.rx_jmb_buffers[desc_idx];
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005865 dma_addr = dma_unmap_addr(ri, mapping);
Eric Dumazet9205fd92011-11-18 06:47:01 +00005866 data = ri->data;
Matt Carlson43619352009-11-13 13:03:47 +00005867 post_ptr = &jmb_prod_idx;
Matt Carlson21f581a2009-08-28 14:00:25 +00005868 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07005869 goto next_pkt_nopost;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005870
5871 work_mask |= opaque_key;
5872
5873 if ((desc->err_vlan & RXD_ERR_MASK) != 0 &&
5874 (desc->err_vlan != RXD_ERR_ODD_NIBBLE_RCVD_MII)) {
5875 drop_it:
Matt Carlsona3896162009-11-13 13:03:44 +00005876 tg3_recycle_rx(tnapi, tpr, opaque_key,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005877 desc_idx, *post_ptr);
5878 drop_it_no_recycle:
5879 /* Other statistics kept track of by card. */
Eric Dumazetb0057c52010-10-10 19:55:52 +00005880 tp->rx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005881 goto next_pkt;
5882 }
5883
Eric Dumazet9205fd92011-11-18 06:47:01 +00005884 prefetch(data + TG3_RX_OFFSET(tp));
Matt Carlsonad829262008-11-21 17:16:16 -08005885 len = ((desc->idx_len & RXD_LEN_MASK) >> RXD_LEN_SHIFT) -
5886 ETH_FCS_LEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005887
Matt Carlsond2757fc2010-04-12 06:58:27 +00005888 if (len > TG3_RX_COPY_THRESH(tp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005889 int skb_size;
Eric Dumazet8d4057a2012-04-27 00:34:49 +00005890 unsigned int frag_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005891
Eric Dumazet9205fd92011-11-18 06:47:01 +00005892 skb_size = tg3_alloc_rx_data(tp, tpr, opaque_key,
Eric Dumazet8d4057a2012-04-27 00:34:49 +00005893 *post_ptr, &frag_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005894 if (skb_size < 0)
5895 goto drop_it;
5896
Matt Carlson287be122009-08-28 13:58:46 +00005897 pci_unmap_single(tp->pdev, dma_addr, skb_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005898 PCI_DMA_FROMDEVICE);
5899
Eric Dumazet8d4057a2012-04-27 00:34:49 +00005900 skb = build_skb(data, frag_size);
Eric Dumazet9205fd92011-11-18 06:47:01 +00005901 if (!skb) {
Eric Dumazet8d4057a2012-04-27 00:34:49 +00005902 tg3_frag_free(frag_size != 0, data);
Eric Dumazet9205fd92011-11-18 06:47:01 +00005903 goto drop_it_no_recycle;
5904 }
5905 skb_reserve(skb, TG3_RX_OFFSET(tp));
5906 /* Ensure that the update to the data happens
Matt Carlson61e800c2010-02-17 15:16:54 +00005907 * after the usage of the old DMA mapping.
5908 */
5909 smp_wmb();
5910
Eric Dumazet9205fd92011-11-18 06:47:01 +00005911 ri->data = NULL;
Matt Carlson61e800c2010-02-17 15:16:54 +00005912
Linus Torvalds1da177e2005-04-16 15:20:36 -07005913 } else {
Matt Carlsona3896162009-11-13 13:03:44 +00005914 tg3_recycle_rx(tnapi, tpr, opaque_key,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005915 desc_idx, *post_ptr);
5916
Eric Dumazet9205fd92011-11-18 06:47:01 +00005917 skb = netdev_alloc_skb(tp->dev,
5918 len + TG3_RAW_IP_ALIGN);
5919 if (skb == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005920 goto drop_it_no_recycle;
5921
Eric Dumazet9205fd92011-11-18 06:47:01 +00005922 skb_reserve(skb, TG3_RAW_IP_ALIGN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005923 pci_dma_sync_single_for_cpu(tp->pdev, dma_addr, len, PCI_DMA_FROMDEVICE);
Eric Dumazet9205fd92011-11-18 06:47:01 +00005924 memcpy(skb->data,
5925 data + TG3_RX_OFFSET(tp),
5926 len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005927 pci_dma_sync_single_for_device(tp->pdev, dma_addr, len, PCI_DMA_FROMDEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005928 }
5929
Eric Dumazet9205fd92011-11-18 06:47:01 +00005930 skb_put(skb, len);
Michał Mirosławdc668912011-04-07 03:35:07 +00005931 if ((tp->dev->features & NETIF_F_RXCSUM) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005932 (desc->type_flags & RXD_FLAG_TCPUDP_CSUM) &&
5933 (((desc->ip_tcp_csum & RXD_TCPCSUM_MASK)
5934 >> RXD_TCPCSUM_SHIFT) == 0xffff))
5935 skb->ip_summed = CHECKSUM_UNNECESSARY;
5936 else
Eric Dumazetbc8acf22010-09-02 13:07:41 -07005937 skb_checksum_none_assert(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005938
5939 skb->protocol = eth_type_trans(skb, tp->dev);
Matt Carlsonf7b493e2009-02-25 14:21:52 +00005940
5941 if (len > (tp->dev->mtu + ETH_HLEN) &&
5942 skb->protocol != htons(ETH_P_8021Q)) {
5943 dev_kfree_skb(skb);
Eric Dumazetb0057c52010-10-10 19:55:52 +00005944 goto drop_it_no_recycle;
Matt Carlsonf7b493e2009-02-25 14:21:52 +00005945 }
5946
Matt Carlson9dc7a112010-04-12 06:58:28 +00005947 if (desc->type_flags & RXD_FLAG_VLAN &&
Matt Carlsonbf933c82011-01-25 15:58:49 +00005948 !(tp->rx_mode & RX_MODE_KEEP_VLAN_TAG))
5949 __vlan_hwaccel_put_tag(skb,
5950 desc->err_vlan & RXD_VLAN_MASK);
Matt Carlson9dc7a112010-04-12 06:58:28 +00005951
Matt Carlsonbf933c82011-01-25 15:58:49 +00005952 napi_gro_receive(&tnapi->napi, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005953
Linus Torvalds1da177e2005-04-16 15:20:36 -07005954 received++;
5955 budget--;
5956
5957next_pkt:
5958 (*post_ptr)++;
Michael Chanf92905d2006-06-29 20:14:29 -07005959
5960 if (unlikely(rx_std_posted >= tp->rx_std_max_post)) {
Matt Carlson2c49a442010-09-30 10:34:35 +00005961 tpr->rx_std_prod_idx = std_prod_idx &
5962 tp->rx_std_ring_mask;
Matt Carlson86cfe4f2010-01-12 10:11:37 +00005963 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
5964 tpr->rx_std_prod_idx);
Michael Chanf92905d2006-06-29 20:14:29 -07005965 work_mask &= ~RXD_OPAQUE_RING_STD;
5966 rx_std_posted = 0;
5967 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005968next_pkt_nopost:
Michael Chan483ba502005-04-25 15:14:03 -07005969 sw_idx++;
Matt Carlson7cb32cf2010-09-30 10:34:36 +00005970 sw_idx &= tp->rx_ret_ring_mask;
Michael Chan52f6d692005-04-25 15:14:32 -07005971
5972 /* Refresh hw_idx to see if there is new work */
5973 if (sw_idx == hw_idx) {
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00005974 hw_idx = *(tnapi->rx_rcb_prod_idx);
Michael Chan52f6d692005-04-25 15:14:32 -07005975 rmb();
5976 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005977 }
5978
5979 /* ACK the status ring. */
Matt Carlson72334482009-08-28 14:03:01 +00005980 tnapi->rx_rcb_ptr = sw_idx;
5981 tw32_rx_mbox(tnapi->consmbox, sw_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005982
5983 /* Refill RX ring(s). */
Joe Perches63c3a662011-04-26 08:12:10 +00005984 if (!tg3_flag(tp, ENABLE_RSS)) {
Michael Chan6541b802012-03-04 14:48:14 +00005985 /* Sync BD data before updating mailbox */
5986 wmb();
5987
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005988 if (work_mask & RXD_OPAQUE_RING_STD) {
Matt Carlson2c49a442010-09-30 10:34:35 +00005989 tpr->rx_std_prod_idx = std_prod_idx &
5990 tp->rx_std_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005991 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
5992 tpr->rx_std_prod_idx);
5993 }
5994 if (work_mask & RXD_OPAQUE_RING_JUMBO) {
Matt Carlson2c49a442010-09-30 10:34:35 +00005995 tpr->rx_jmb_prod_idx = jmb_prod_idx &
5996 tp->rx_jmb_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005997 tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG,
5998 tpr->rx_jmb_prod_idx);
5999 }
6000 mmiowb();
6001 } else if (work_mask) {
6002 /* rx_std_buffers[] and rx_jmb_buffers[] entries must be
6003 * updated before the producer indices can be updated.
6004 */
6005 smp_wmb();
6006
Matt Carlson2c49a442010-09-30 10:34:35 +00006007 tpr->rx_std_prod_idx = std_prod_idx & tp->rx_std_ring_mask;
6008 tpr->rx_jmb_prod_idx = jmb_prod_idx & tp->rx_jmb_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006009
Michael Chan7ae52892012-03-21 15:38:33 +00006010 if (tnapi != &tp->napi[1]) {
6011 tp->rx_refill = true;
Matt Carlsone4af1af2010-02-12 14:47:05 +00006012 napi_schedule(&tp->napi[1].napi);
Michael Chan7ae52892012-03-21 15:38:33 +00006013 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006014 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006015
6016 return received;
6017}
6018
Matt Carlson35f2d7d2009-11-13 13:03:41 +00006019static void tg3_poll_link(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006020{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006021 /* handle link change and other phy events */
Joe Perches63c3a662011-04-26 08:12:10 +00006022 if (!(tg3_flag(tp, USE_LINKCHG_REG) || tg3_flag(tp, POLL_SERDES))) {
Matt Carlson35f2d7d2009-11-13 13:03:41 +00006023 struct tg3_hw_status *sblk = tp->napi[0].hw_status;
6024
Linus Torvalds1da177e2005-04-16 15:20:36 -07006025 if (sblk->status & SD_STATUS_LINK_CHG) {
6026 sblk->status = SD_STATUS_UPDATED |
Matt Carlson35f2d7d2009-11-13 13:03:41 +00006027 (sblk->status & ~SD_STATUS_LINK_CHG);
David S. Millerf47c11e2005-06-24 20:18:35 -07006028 spin_lock(&tp->lock);
Joe Perches63c3a662011-04-26 08:12:10 +00006029 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlsondd477002008-05-25 23:45:58 -07006030 tw32_f(MAC_STATUS,
6031 (MAC_STATUS_SYNC_CHANGED |
6032 MAC_STATUS_CFG_CHANGED |
6033 MAC_STATUS_MI_COMPLETION |
6034 MAC_STATUS_LNKSTATE_CHANGED));
6035 udelay(40);
6036 } else
6037 tg3_setup_phy(tp, 0);
David S. Millerf47c11e2005-06-24 20:18:35 -07006038 spin_unlock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006039 }
6040 }
Matt Carlson35f2d7d2009-11-13 13:03:41 +00006041}
6042
Matt Carlsonf89f38b2010-02-12 14:47:07 +00006043static int tg3_rx_prodring_xfer(struct tg3 *tp,
6044 struct tg3_rx_prodring_set *dpr,
6045 struct tg3_rx_prodring_set *spr)
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006046{
6047 u32 si, di, cpycnt, src_prod_idx;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00006048 int i, err = 0;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006049
6050 while (1) {
6051 src_prod_idx = spr->rx_std_prod_idx;
6052
6053 /* Make sure updates to the rx_std_buffers[] entries and the
6054 * standard producer index are seen in the correct order.
6055 */
6056 smp_rmb();
6057
6058 if (spr->rx_std_cons_idx == src_prod_idx)
6059 break;
6060
6061 if (spr->rx_std_cons_idx < src_prod_idx)
6062 cpycnt = src_prod_idx - spr->rx_std_cons_idx;
6063 else
Matt Carlson2c49a442010-09-30 10:34:35 +00006064 cpycnt = tp->rx_std_ring_mask + 1 -
6065 spr->rx_std_cons_idx;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006066
Matt Carlson2c49a442010-09-30 10:34:35 +00006067 cpycnt = min(cpycnt,
6068 tp->rx_std_ring_mask + 1 - dpr->rx_std_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006069
6070 si = spr->rx_std_cons_idx;
6071 di = dpr->rx_std_prod_idx;
6072
Matt Carlsone92967b2010-02-12 14:47:06 +00006073 for (i = di; i < di + cpycnt; i++) {
Eric Dumazet9205fd92011-11-18 06:47:01 +00006074 if (dpr->rx_std_buffers[i].data) {
Matt Carlsone92967b2010-02-12 14:47:06 +00006075 cpycnt = i - di;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00006076 err = -ENOSPC;
Matt Carlsone92967b2010-02-12 14:47:06 +00006077 break;
6078 }
6079 }
6080
6081 if (!cpycnt)
6082 break;
6083
6084 /* Ensure that updates to the rx_std_buffers ring and the
6085 * shadowed hardware producer ring from tg3_recycle_skb() are
6086 * ordered correctly WRT the skb check above.
6087 */
6088 smp_rmb();
6089
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006090 memcpy(&dpr->rx_std_buffers[di],
6091 &spr->rx_std_buffers[si],
6092 cpycnt * sizeof(struct ring_info));
6093
6094 for (i = 0; i < cpycnt; i++, di++, si++) {
6095 struct tg3_rx_buffer_desc *sbd, *dbd;
6096 sbd = &spr->rx_std[si];
6097 dbd = &dpr->rx_std[di];
6098 dbd->addr_hi = sbd->addr_hi;
6099 dbd->addr_lo = sbd->addr_lo;
6100 }
6101
Matt Carlson2c49a442010-09-30 10:34:35 +00006102 spr->rx_std_cons_idx = (spr->rx_std_cons_idx + cpycnt) &
6103 tp->rx_std_ring_mask;
6104 dpr->rx_std_prod_idx = (dpr->rx_std_prod_idx + cpycnt) &
6105 tp->rx_std_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006106 }
6107
6108 while (1) {
6109 src_prod_idx = spr->rx_jmb_prod_idx;
6110
6111 /* Make sure updates to the rx_jmb_buffers[] entries and
6112 * the jumbo producer index are seen in the correct order.
6113 */
6114 smp_rmb();
6115
6116 if (spr->rx_jmb_cons_idx == src_prod_idx)
6117 break;
6118
6119 if (spr->rx_jmb_cons_idx < src_prod_idx)
6120 cpycnt = src_prod_idx - spr->rx_jmb_cons_idx;
6121 else
Matt Carlson2c49a442010-09-30 10:34:35 +00006122 cpycnt = tp->rx_jmb_ring_mask + 1 -
6123 spr->rx_jmb_cons_idx;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006124
6125 cpycnt = min(cpycnt,
Matt Carlson2c49a442010-09-30 10:34:35 +00006126 tp->rx_jmb_ring_mask + 1 - dpr->rx_jmb_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006127
6128 si = spr->rx_jmb_cons_idx;
6129 di = dpr->rx_jmb_prod_idx;
6130
Matt Carlsone92967b2010-02-12 14:47:06 +00006131 for (i = di; i < di + cpycnt; i++) {
Eric Dumazet9205fd92011-11-18 06:47:01 +00006132 if (dpr->rx_jmb_buffers[i].data) {
Matt Carlsone92967b2010-02-12 14:47:06 +00006133 cpycnt = i - di;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00006134 err = -ENOSPC;
Matt Carlsone92967b2010-02-12 14:47:06 +00006135 break;
6136 }
6137 }
6138
6139 if (!cpycnt)
6140 break;
6141
6142 /* Ensure that updates to the rx_jmb_buffers ring and the
6143 * shadowed hardware producer ring from tg3_recycle_skb() are
6144 * ordered correctly WRT the skb check above.
6145 */
6146 smp_rmb();
6147
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006148 memcpy(&dpr->rx_jmb_buffers[di],
6149 &spr->rx_jmb_buffers[si],
6150 cpycnt * sizeof(struct ring_info));
6151
6152 for (i = 0; i < cpycnt; i++, di++, si++) {
6153 struct tg3_rx_buffer_desc *sbd, *dbd;
6154 sbd = &spr->rx_jmb[si].std;
6155 dbd = &dpr->rx_jmb[di].std;
6156 dbd->addr_hi = sbd->addr_hi;
6157 dbd->addr_lo = sbd->addr_lo;
6158 }
6159
Matt Carlson2c49a442010-09-30 10:34:35 +00006160 spr->rx_jmb_cons_idx = (spr->rx_jmb_cons_idx + cpycnt) &
6161 tp->rx_jmb_ring_mask;
6162 dpr->rx_jmb_prod_idx = (dpr->rx_jmb_prod_idx + cpycnt) &
6163 tp->rx_jmb_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006164 }
Matt Carlsonf89f38b2010-02-12 14:47:07 +00006165
6166 return err;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006167}
6168
Matt Carlson35f2d7d2009-11-13 13:03:41 +00006169static int tg3_poll_work(struct tg3_napi *tnapi, int work_done, int budget)
6170{
6171 struct tg3 *tp = tnapi->tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006172
6173 /* run TX completion thread */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006174 if (tnapi->hw_status->idx[0].tx_consumer != tnapi->tx_cons) {
Matt Carlson17375d22009-08-28 14:02:18 +00006175 tg3_tx(tnapi);
Joe Perches63c3a662011-04-26 08:12:10 +00006176 if (unlikely(tg3_flag(tp, TX_RECOVERY_PENDING)))
Michael Chan4fd7ab52007-10-12 01:39:50 -07006177 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006178 }
6179
Matt Carlsonf891ea12012-04-24 13:37:01 +00006180 if (!tnapi->rx_rcb_prod_idx)
6181 return work_done;
6182
Linus Torvalds1da177e2005-04-16 15:20:36 -07006183 /* run RX thread, within the bounds set by NAPI.
6184 * All RX "locking" is done by ensuring outside
Stephen Hemmingerbea33482007-10-03 16:41:36 -07006185 * code synchronizes with tg3->napi.poll()
Linus Torvalds1da177e2005-04-16 15:20:36 -07006186 */
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00006187 if (*(tnapi->rx_rcb_prod_idx) != tnapi->rx_rcb_ptr)
Matt Carlson17375d22009-08-28 14:02:18 +00006188 work_done += tg3_rx(tnapi, budget - work_done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006189
Joe Perches63c3a662011-04-26 08:12:10 +00006190 if (tg3_flag(tp, ENABLE_RSS) && tnapi == &tp->napi[1]) {
Matt Carlson8fea32b2010-09-15 08:59:58 +00006191 struct tg3_rx_prodring_set *dpr = &tp->napi[0].prodring;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00006192 int i, err = 0;
Matt Carlsone4af1af2010-02-12 14:47:05 +00006193 u32 std_prod_idx = dpr->rx_std_prod_idx;
6194 u32 jmb_prod_idx = dpr->rx_jmb_prod_idx;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006195
Michael Chan7ae52892012-03-21 15:38:33 +00006196 tp->rx_refill = false;
Matt Carlsone4af1af2010-02-12 14:47:05 +00006197 for (i = 1; i < tp->irq_cnt; i++)
Matt Carlsonf89f38b2010-02-12 14:47:07 +00006198 err |= tg3_rx_prodring_xfer(tp, dpr,
Matt Carlson8fea32b2010-09-15 08:59:58 +00006199 &tp->napi[i].prodring);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006200
6201 wmb();
6202
Matt Carlsone4af1af2010-02-12 14:47:05 +00006203 if (std_prod_idx != dpr->rx_std_prod_idx)
6204 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
6205 dpr->rx_std_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006206
Matt Carlsone4af1af2010-02-12 14:47:05 +00006207 if (jmb_prod_idx != dpr->rx_jmb_prod_idx)
6208 tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG,
6209 dpr->rx_jmb_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006210
6211 mmiowb();
Matt Carlsonf89f38b2010-02-12 14:47:07 +00006212
6213 if (err)
6214 tw32_f(HOSTCC_MODE, tp->coal_now);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006215 }
6216
David S. Miller6f535762007-10-11 18:08:29 -07006217 return work_done;
6218}
David S. Millerf7383c22005-05-18 22:50:53 -07006219
Matt Carlsondb219972011-11-04 09:15:03 +00006220static inline void tg3_reset_task_schedule(struct tg3 *tp)
6221{
6222 if (!test_and_set_bit(TG3_FLAG_RESET_TASK_PENDING, tp->tg3_flags))
6223 schedule_work(&tp->reset_task);
6224}
6225
6226static inline void tg3_reset_task_cancel(struct tg3 *tp)
6227{
6228 cancel_work_sync(&tp->reset_task);
6229 tg3_flag_clear(tp, RESET_TASK_PENDING);
Matt Carlsonc7101352012-02-22 12:35:20 +00006230 tg3_flag_clear(tp, TX_RECOVERY_PENDING);
Matt Carlsondb219972011-11-04 09:15:03 +00006231}
6232
Matt Carlson35f2d7d2009-11-13 13:03:41 +00006233static int tg3_poll_msix(struct napi_struct *napi, int budget)
6234{
6235 struct tg3_napi *tnapi = container_of(napi, struct tg3_napi, napi);
6236 struct tg3 *tp = tnapi->tp;
6237 int work_done = 0;
6238 struct tg3_hw_status *sblk = tnapi->hw_status;
6239
6240 while (1) {
6241 work_done = tg3_poll_work(tnapi, work_done, budget);
6242
Joe Perches63c3a662011-04-26 08:12:10 +00006243 if (unlikely(tg3_flag(tp, TX_RECOVERY_PENDING)))
Matt Carlson35f2d7d2009-11-13 13:03:41 +00006244 goto tx_recovery;
6245
6246 if (unlikely(work_done >= budget))
6247 break;
6248
Matt Carlsonc6cdf432010-04-05 10:19:26 +00006249 /* tp->last_tag is used in tg3_int_reenable() below
Matt Carlson35f2d7d2009-11-13 13:03:41 +00006250 * to tell the hw how much work has been processed,
6251 * so we must read it before checking for more work.
6252 */
6253 tnapi->last_tag = sblk->status_tag;
6254 tnapi->last_irq_tag = tnapi->last_tag;
6255 rmb();
6256
6257 /* check for RX/TX work to do */
Matt Carlson6d40db72010-04-05 10:19:20 +00006258 if (likely(sblk->idx[0].tx_consumer == tnapi->tx_cons &&
6259 *(tnapi->rx_rcb_prod_idx) == tnapi->rx_rcb_ptr)) {
Michael Chan7ae52892012-03-21 15:38:33 +00006260
6261 /* This test here is not race free, but will reduce
6262 * the number of interrupts by looping again.
6263 */
6264 if (tnapi == &tp->napi[1] && tp->rx_refill)
6265 continue;
6266
Matt Carlson35f2d7d2009-11-13 13:03:41 +00006267 napi_complete(napi);
6268 /* Reenable interrupts. */
6269 tw32_mailbox(tnapi->int_mbox, tnapi->last_tag << 24);
Michael Chan7ae52892012-03-21 15:38:33 +00006270
6271 /* This test here is synchronized by napi_schedule()
6272 * and napi_complete() to close the race condition.
6273 */
6274 if (unlikely(tnapi == &tp->napi[1] && tp->rx_refill)) {
6275 tw32(HOSTCC_MODE, tp->coalesce_mode |
6276 HOSTCC_MODE_ENABLE |
6277 tnapi->coal_now);
6278 }
Matt Carlson35f2d7d2009-11-13 13:03:41 +00006279 mmiowb();
6280 break;
6281 }
6282 }
6283
6284 return work_done;
6285
6286tx_recovery:
6287 /* work_done is guaranteed to be less than budget. */
6288 napi_complete(napi);
Matt Carlsondb219972011-11-04 09:15:03 +00006289 tg3_reset_task_schedule(tp);
Matt Carlson35f2d7d2009-11-13 13:03:41 +00006290 return work_done;
6291}
6292
Matt Carlsone64de4e2011-04-13 11:05:05 +00006293static void tg3_process_error(struct tg3 *tp)
6294{
6295 u32 val;
6296 bool real_error = false;
6297
Joe Perches63c3a662011-04-26 08:12:10 +00006298 if (tg3_flag(tp, ERROR_PROCESSED))
Matt Carlsone64de4e2011-04-13 11:05:05 +00006299 return;
6300
6301 /* Check Flow Attention register */
6302 val = tr32(HOSTCC_FLOW_ATTN);
6303 if (val & ~HOSTCC_FLOW_ATTN_MBUF_LWM) {
6304 netdev_err(tp->dev, "FLOW Attention error. Resetting chip.\n");
6305 real_error = true;
6306 }
6307
6308 if (tr32(MSGINT_STATUS) & ~MSGINT_STATUS_MSI_REQ) {
6309 netdev_err(tp->dev, "MSI Status error. Resetting chip.\n");
6310 real_error = true;
6311 }
6312
6313 if (tr32(RDMAC_STATUS) || tr32(WDMAC_STATUS)) {
6314 netdev_err(tp->dev, "DMA Status error. Resetting chip.\n");
6315 real_error = true;
6316 }
6317
6318 if (!real_error)
6319 return;
6320
6321 tg3_dump_state(tp);
6322
Joe Perches63c3a662011-04-26 08:12:10 +00006323 tg3_flag_set(tp, ERROR_PROCESSED);
Matt Carlsondb219972011-11-04 09:15:03 +00006324 tg3_reset_task_schedule(tp);
Matt Carlsone64de4e2011-04-13 11:05:05 +00006325}
6326
David S. Miller6f535762007-10-11 18:08:29 -07006327static int tg3_poll(struct napi_struct *napi, int budget)
6328{
Matt Carlson8ef04422009-08-28 14:01:37 +00006329 struct tg3_napi *tnapi = container_of(napi, struct tg3_napi, napi);
6330 struct tg3 *tp = tnapi->tp;
David S. Miller6f535762007-10-11 18:08:29 -07006331 int work_done = 0;
Matt Carlson898a56f2009-08-28 14:02:40 +00006332 struct tg3_hw_status *sblk = tnapi->hw_status;
David S. Miller6f535762007-10-11 18:08:29 -07006333
6334 while (1) {
Matt Carlsone64de4e2011-04-13 11:05:05 +00006335 if (sblk->status & SD_STATUS_ERROR)
6336 tg3_process_error(tp);
6337
Matt Carlson35f2d7d2009-11-13 13:03:41 +00006338 tg3_poll_link(tp);
6339
Matt Carlson17375d22009-08-28 14:02:18 +00006340 work_done = tg3_poll_work(tnapi, work_done, budget);
David S. Miller6f535762007-10-11 18:08:29 -07006341
Joe Perches63c3a662011-04-26 08:12:10 +00006342 if (unlikely(tg3_flag(tp, TX_RECOVERY_PENDING)))
David S. Miller6f535762007-10-11 18:08:29 -07006343 goto tx_recovery;
6344
6345 if (unlikely(work_done >= budget))
6346 break;
6347
Joe Perches63c3a662011-04-26 08:12:10 +00006348 if (tg3_flag(tp, TAGGED_STATUS)) {
Matt Carlson17375d22009-08-28 14:02:18 +00006349 /* tp->last_tag is used in tg3_int_reenable() below
Michael Chan4fd7ab52007-10-12 01:39:50 -07006350 * to tell the hw how much work has been processed,
6351 * so we must read it before checking for more work.
6352 */
Matt Carlson898a56f2009-08-28 14:02:40 +00006353 tnapi->last_tag = sblk->status_tag;
6354 tnapi->last_irq_tag = tnapi->last_tag;
Michael Chan4fd7ab52007-10-12 01:39:50 -07006355 rmb();
6356 } else
6357 sblk->status &= ~SD_STATUS_UPDATED;
6358
Matt Carlson17375d22009-08-28 14:02:18 +00006359 if (likely(!tg3_has_work(tnapi))) {
Ben Hutchings288379f2009-01-19 16:43:59 -08006360 napi_complete(napi);
Matt Carlson17375d22009-08-28 14:02:18 +00006361 tg3_int_reenable(tnapi);
David S. Miller6f535762007-10-11 18:08:29 -07006362 break;
6363 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006364 }
6365
Stephen Hemmingerbea33482007-10-03 16:41:36 -07006366 return work_done;
David S. Miller6f535762007-10-11 18:08:29 -07006367
6368tx_recovery:
Michael Chan4fd7ab52007-10-12 01:39:50 -07006369 /* work_done is guaranteed to be less than budget. */
Ben Hutchings288379f2009-01-19 16:43:59 -08006370 napi_complete(napi);
Matt Carlsondb219972011-11-04 09:15:03 +00006371 tg3_reset_task_schedule(tp);
Michael Chan4fd7ab52007-10-12 01:39:50 -07006372 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006373}
6374
Matt Carlson66cfd1b2010-09-30 10:34:30 +00006375static void tg3_napi_disable(struct tg3 *tp)
6376{
6377 int i;
6378
6379 for (i = tp->irq_cnt - 1; i >= 0; i--)
6380 napi_disable(&tp->napi[i].napi);
6381}
6382
6383static void tg3_napi_enable(struct tg3 *tp)
6384{
6385 int i;
6386
6387 for (i = 0; i < tp->irq_cnt; i++)
6388 napi_enable(&tp->napi[i].napi);
6389}
6390
6391static void tg3_napi_init(struct tg3 *tp)
6392{
6393 int i;
6394
6395 netif_napi_add(tp->dev, &tp->napi[0].napi, tg3_poll, 64);
6396 for (i = 1; i < tp->irq_cnt; i++)
6397 netif_napi_add(tp->dev, &tp->napi[i].napi, tg3_poll_msix, 64);
6398}
6399
6400static void tg3_napi_fini(struct tg3 *tp)
6401{
6402 int i;
6403
6404 for (i = 0; i < tp->irq_cnt; i++)
6405 netif_napi_del(&tp->napi[i].napi);
6406}
6407
6408static inline void tg3_netif_stop(struct tg3 *tp)
6409{
6410 tp->dev->trans_start = jiffies; /* prevent tx timeout */
6411 tg3_napi_disable(tp);
6412 netif_tx_disable(tp->dev);
6413}
6414
6415static inline void tg3_netif_start(struct tg3 *tp)
6416{
6417 /* NOTE: unconditional netif_tx_wake_all_queues is only
6418 * appropriate so long as all callers are assured to
6419 * have free tx slots (such as after tg3_init_hw)
6420 */
6421 netif_tx_wake_all_queues(tp->dev);
6422
6423 tg3_napi_enable(tp);
6424 tp->napi[0].hw_status->status |= SD_STATUS_UPDATED;
6425 tg3_enable_ints(tp);
6426}
6427
David S. Millerf47c11e2005-06-24 20:18:35 -07006428static void tg3_irq_quiesce(struct tg3 *tp)
6429{
Matt Carlson4f125f42009-09-01 12:55:02 +00006430 int i;
6431
David S. Millerf47c11e2005-06-24 20:18:35 -07006432 BUG_ON(tp->irq_sync);
6433
6434 tp->irq_sync = 1;
6435 smp_mb();
6436
Matt Carlson4f125f42009-09-01 12:55:02 +00006437 for (i = 0; i < tp->irq_cnt; i++)
6438 synchronize_irq(tp->napi[i].irq_vec);
David S. Millerf47c11e2005-06-24 20:18:35 -07006439}
6440
David S. Millerf47c11e2005-06-24 20:18:35 -07006441/* Fully shutdown all tg3 driver activity elsewhere in the system.
6442 * If irq_sync is non-zero, then the IRQ handler must be synchronized
6443 * with as well. Most of the time, this is not necessary except when
6444 * shutting down the device.
6445 */
6446static inline void tg3_full_lock(struct tg3 *tp, int irq_sync)
6447{
Michael Chan46966542007-07-11 19:47:19 -07006448 spin_lock_bh(&tp->lock);
David S. Millerf47c11e2005-06-24 20:18:35 -07006449 if (irq_sync)
6450 tg3_irq_quiesce(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -07006451}
6452
6453static inline void tg3_full_unlock(struct tg3 *tp)
6454{
David S. Millerf47c11e2005-06-24 20:18:35 -07006455 spin_unlock_bh(&tp->lock);
6456}
6457
Michael Chanfcfa0a32006-03-20 22:28:41 -08006458/* One-shot MSI handler - Chip automatically disables interrupt
6459 * after sending MSI so driver doesn't have to do it.
6460 */
David Howells7d12e782006-10-05 14:55:46 +01006461static irqreturn_t tg3_msi_1shot(int irq, void *dev_id)
Michael Chanfcfa0a32006-03-20 22:28:41 -08006462{
Matt Carlson09943a12009-08-28 14:01:57 +00006463 struct tg3_napi *tnapi = dev_id;
6464 struct tg3 *tp = tnapi->tp;
Michael Chanfcfa0a32006-03-20 22:28:41 -08006465
Matt Carlson898a56f2009-08-28 14:02:40 +00006466 prefetch(tnapi->hw_status);
Matt Carlson0c1d0e22009-09-01 13:16:33 +00006467 if (tnapi->rx_rcb)
6468 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Michael Chanfcfa0a32006-03-20 22:28:41 -08006469
6470 if (likely(!tg3_irq_sync(tp)))
Matt Carlson09943a12009-08-28 14:01:57 +00006471 napi_schedule(&tnapi->napi);
Michael Chanfcfa0a32006-03-20 22:28:41 -08006472
6473 return IRQ_HANDLED;
6474}
6475
Michael Chan88b06bc22005-04-21 17:13:25 -07006476/* MSI ISR - No need to check for interrupt sharing and no need to
6477 * flush status block and interrupt mailbox. PCI ordering rules
6478 * guarantee that MSI will arrive after the status block.
6479 */
David Howells7d12e782006-10-05 14:55:46 +01006480static irqreturn_t tg3_msi(int irq, void *dev_id)
Michael Chan88b06bc22005-04-21 17:13:25 -07006481{
Matt Carlson09943a12009-08-28 14:01:57 +00006482 struct tg3_napi *tnapi = dev_id;
6483 struct tg3 *tp = tnapi->tp;
Michael Chan88b06bc22005-04-21 17:13:25 -07006484
Matt Carlson898a56f2009-08-28 14:02:40 +00006485 prefetch(tnapi->hw_status);
Matt Carlson0c1d0e22009-09-01 13:16:33 +00006486 if (tnapi->rx_rcb)
6487 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Michael Chan88b06bc22005-04-21 17:13:25 -07006488 /*
David S. Millerfac9b832005-05-18 22:46:34 -07006489 * Writing any value to intr-mbox-0 clears PCI INTA# and
Michael Chan88b06bc22005-04-21 17:13:25 -07006490 * chip-internal interrupt pending events.
David S. Millerfac9b832005-05-18 22:46:34 -07006491 * Writing non-zero to intr-mbox-0 additional tells the
Michael Chan88b06bc22005-04-21 17:13:25 -07006492 * NIC to stop sending us irqs, engaging "in-intr-handler"
6493 * event coalescing.
6494 */
Matt Carlson5b39de92011-08-31 11:44:50 +00006495 tw32_mailbox(tnapi->int_mbox, 0x00000001);
Michael Chan61487482005-09-05 17:53:19 -07006496 if (likely(!tg3_irq_sync(tp)))
Matt Carlson09943a12009-08-28 14:01:57 +00006497 napi_schedule(&tnapi->napi);
Michael Chan61487482005-09-05 17:53:19 -07006498
Michael Chan88b06bc22005-04-21 17:13:25 -07006499 return IRQ_RETVAL(1);
6500}
6501
David Howells7d12e782006-10-05 14:55:46 +01006502static irqreturn_t tg3_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006503{
Matt Carlson09943a12009-08-28 14:01:57 +00006504 struct tg3_napi *tnapi = dev_id;
6505 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00006506 struct tg3_hw_status *sblk = tnapi->hw_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006507 unsigned int handled = 1;
6508
Linus Torvalds1da177e2005-04-16 15:20:36 -07006509 /* In INTx mode, it is possible for the interrupt to arrive at
6510 * the CPU before the status block posted prior to the interrupt.
6511 * Reading the PCI State register will confirm whether the
6512 * interrupt is ours and will flush the status block.
6513 */
Michael Chand18edcb2007-03-24 20:57:11 -07006514 if (unlikely(!(sblk->status & SD_STATUS_UPDATED))) {
Joe Perches63c3a662011-04-26 08:12:10 +00006515 if (tg3_flag(tp, CHIP_RESETTING) ||
Michael Chand18edcb2007-03-24 20:57:11 -07006516 (tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
6517 handled = 0;
David S. Millerf47c11e2005-06-24 20:18:35 -07006518 goto out;
David S. Millerfac9b832005-05-18 22:46:34 -07006519 }
Michael Chand18edcb2007-03-24 20:57:11 -07006520 }
6521
6522 /*
6523 * Writing any value to intr-mbox-0 clears PCI INTA# and
6524 * chip-internal interrupt pending events.
6525 * Writing non-zero to intr-mbox-0 additional tells the
6526 * NIC to stop sending us irqs, engaging "in-intr-handler"
6527 * event coalescing.
Michael Chanc04cb342007-05-07 00:26:15 -07006528 *
6529 * Flush the mailbox to de-assert the IRQ immediately to prevent
6530 * spurious interrupts. The flush impacts performance but
6531 * excessive spurious interrupts can be worse in some cases.
Michael Chand18edcb2007-03-24 20:57:11 -07006532 */
Michael Chanc04cb342007-05-07 00:26:15 -07006533 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 0x00000001);
Michael Chand18edcb2007-03-24 20:57:11 -07006534 if (tg3_irq_sync(tp))
6535 goto out;
6536 sblk->status &= ~SD_STATUS_UPDATED;
Matt Carlson17375d22009-08-28 14:02:18 +00006537 if (likely(tg3_has_work(tnapi))) {
Matt Carlson72334482009-08-28 14:03:01 +00006538 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Matt Carlson09943a12009-08-28 14:01:57 +00006539 napi_schedule(&tnapi->napi);
Michael Chand18edcb2007-03-24 20:57:11 -07006540 } else {
6541 /* No work, shared interrupt perhaps? re-enable
6542 * interrupts, and flush that PCI write
6543 */
6544 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW,
6545 0x00000000);
David S. Millerfac9b832005-05-18 22:46:34 -07006546 }
David S. Millerf47c11e2005-06-24 20:18:35 -07006547out:
David S. Millerfac9b832005-05-18 22:46:34 -07006548 return IRQ_RETVAL(handled);
6549}
6550
David Howells7d12e782006-10-05 14:55:46 +01006551static irqreturn_t tg3_interrupt_tagged(int irq, void *dev_id)
David S. Millerfac9b832005-05-18 22:46:34 -07006552{
Matt Carlson09943a12009-08-28 14:01:57 +00006553 struct tg3_napi *tnapi = dev_id;
6554 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00006555 struct tg3_hw_status *sblk = tnapi->hw_status;
David S. Millerfac9b832005-05-18 22:46:34 -07006556 unsigned int handled = 1;
6557
David S. Millerfac9b832005-05-18 22:46:34 -07006558 /* In INTx mode, it is possible for the interrupt to arrive at
6559 * the CPU before the status block posted prior to the interrupt.
6560 * Reading the PCI State register will confirm whether the
6561 * interrupt is ours and will flush the status block.
6562 */
Matt Carlson898a56f2009-08-28 14:02:40 +00006563 if (unlikely(sblk->status_tag == tnapi->last_irq_tag)) {
Joe Perches63c3a662011-04-26 08:12:10 +00006564 if (tg3_flag(tp, CHIP_RESETTING) ||
Michael Chand18edcb2007-03-24 20:57:11 -07006565 (tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
6566 handled = 0;
David S. Millerf47c11e2005-06-24 20:18:35 -07006567 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006568 }
Michael Chand18edcb2007-03-24 20:57:11 -07006569 }
6570
6571 /*
6572 * writing any value to intr-mbox-0 clears PCI INTA# and
6573 * chip-internal interrupt pending events.
6574 * writing non-zero to intr-mbox-0 additional tells the
6575 * NIC to stop sending us irqs, engaging "in-intr-handler"
6576 * event coalescing.
Michael Chanc04cb342007-05-07 00:26:15 -07006577 *
6578 * Flush the mailbox to de-assert the IRQ immediately to prevent
6579 * spurious interrupts. The flush impacts performance but
6580 * excessive spurious interrupts can be worse in some cases.
Michael Chand18edcb2007-03-24 20:57:11 -07006581 */
Michael Chanc04cb342007-05-07 00:26:15 -07006582 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 0x00000001);
Matt Carlson624f8e52009-04-20 06:55:01 +00006583
6584 /*
6585 * In a shared interrupt configuration, sometimes other devices'
6586 * interrupts will scream. We record the current status tag here
6587 * so that the above check can report that the screaming interrupts
6588 * are unhandled. Eventually they will be silenced.
6589 */
Matt Carlson898a56f2009-08-28 14:02:40 +00006590 tnapi->last_irq_tag = sblk->status_tag;
Matt Carlson624f8e52009-04-20 06:55:01 +00006591
Michael Chand18edcb2007-03-24 20:57:11 -07006592 if (tg3_irq_sync(tp))
6593 goto out;
Matt Carlson624f8e52009-04-20 06:55:01 +00006594
Matt Carlson72334482009-08-28 14:03:01 +00006595 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Matt Carlson624f8e52009-04-20 06:55:01 +00006596
Matt Carlson09943a12009-08-28 14:01:57 +00006597 napi_schedule(&tnapi->napi);
Matt Carlson624f8e52009-04-20 06:55:01 +00006598
David S. Millerf47c11e2005-06-24 20:18:35 -07006599out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006600 return IRQ_RETVAL(handled);
6601}
6602
Michael Chan79381092005-04-21 17:13:59 -07006603/* ISR for interrupt test */
David Howells7d12e782006-10-05 14:55:46 +01006604static irqreturn_t tg3_test_isr(int irq, void *dev_id)
Michael Chan79381092005-04-21 17:13:59 -07006605{
Matt Carlson09943a12009-08-28 14:01:57 +00006606 struct tg3_napi *tnapi = dev_id;
6607 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00006608 struct tg3_hw_status *sblk = tnapi->hw_status;
Michael Chan79381092005-04-21 17:13:59 -07006609
Michael Chanf9804dd2005-09-27 12:13:10 -07006610 if ((sblk->status & SD_STATUS_UPDATED) ||
6611 !(tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
Michael Chanb16250e2006-09-27 16:10:14 -07006612 tg3_disable_ints(tp);
Michael Chan79381092005-04-21 17:13:59 -07006613 return IRQ_RETVAL(1);
6614 }
6615 return IRQ_RETVAL(0);
6616}
6617
Linus Torvalds1da177e2005-04-16 15:20:36 -07006618#ifdef CONFIG_NET_POLL_CONTROLLER
6619static void tg3_poll_controller(struct net_device *dev)
6620{
Matt Carlson4f125f42009-09-01 12:55:02 +00006621 int i;
Michael Chan88b06bc22005-04-21 17:13:25 -07006622 struct tg3 *tp = netdev_priv(dev);
6623
Matt Carlson4f125f42009-09-01 12:55:02 +00006624 for (i = 0; i < tp->irq_cnt; i++)
Louis Rillingfe234f02010-03-09 06:14:41 +00006625 tg3_interrupt(tp->napi[i].irq_vec, &tp->napi[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006626}
6627#endif
6628
Linus Torvalds1da177e2005-04-16 15:20:36 -07006629static void tg3_tx_timeout(struct net_device *dev)
6630{
6631 struct tg3 *tp = netdev_priv(dev);
6632
Michael Chanb0408752007-02-13 12:18:30 -08006633 if (netif_msg_tx_err(tp)) {
Joe Perches05dbe002010-02-17 19:44:19 +00006634 netdev_err(dev, "transmit timed out, resetting\n");
Matt Carlson97bd8e42011-04-13 11:05:04 +00006635 tg3_dump_state(tp);
Michael Chanb0408752007-02-13 12:18:30 -08006636 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006637
Matt Carlsondb219972011-11-04 09:15:03 +00006638 tg3_reset_task_schedule(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006639}
6640
Michael Chanc58ec932005-09-17 00:46:27 -07006641/* Test for DMA buffers crossing any 4GB boundaries: 4G, 8G, etc */
6642static inline int tg3_4g_overflow_test(dma_addr_t mapping, int len)
6643{
6644 u32 base = (u32) mapping & 0xffffffff;
6645
Eric Dumazet807540b2010-09-23 05:40:09 +00006646 return (base > 0xffffdcc0) && (base + len + 8 < base);
Michael Chanc58ec932005-09-17 00:46:27 -07006647}
6648
Michael Chan72f2afb2006-03-06 19:28:35 -08006649/* Test for DMA addresses > 40-bit */
6650static inline int tg3_40bit_overflow_test(struct tg3 *tp, dma_addr_t mapping,
6651 int len)
6652{
6653#if defined(CONFIG_HIGHMEM) && (BITS_PER_LONG == 64)
Joe Perches63c3a662011-04-26 08:12:10 +00006654 if (tg3_flag(tp, 40BIT_DMA_BUG))
Eric Dumazet807540b2010-09-23 05:40:09 +00006655 return ((u64) mapping + len) > DMA_BIT_MASK(40);
Michael Chan72f2afb2006-03-06 19:28:35 -08006656 return 0;
6657#else
6658 return 0;
6659#endif
6660}
6661
Matt Carlsond1a3b732011-07-27 14:20:51 +00006662static inline void tg3_tx_set_bd(struct tg3_tx_buffer_desc *txbd,
Matt Carlson92cd3a12011-07-27 14:20:47 +00006663 dma_addr_t mapping, u32 len, u32 flags,
6664 u32 mss, u32 vlan)
Matt Carlson2ffcc982011-05-19 12:12:44 +00006665{
Matt Carlson92cd3a12011-07-27 14:20:47 +00006666 txbd->addr_hi = ((u64) mapping >> 32);
6667 txbd->addr_lo = ((u64) mapping & 0xffffffff);
6668 txbd->len_flags = (len << TXD_LEN_SHIFT) | (flags & 0x0000ffff);
6669 txbd->vlan_tag = (mss << TXD_MSS_SHIFT) | (vlan << TXD_VLAN_TAG_SHIFT);
Matt Carlson2ffcc982011-05-19 12:12:44 +00006670}
Linus Torvalds1da177e2005-04-16 15:20:36 -07006671
Matt Carlson84b67b22011-07-27 14:20:52 +00006672static bool tg3_tx_frag_set(struct tg3_napi *tnapi, u32 *entry, u32 *budget,
Matt Carlsond1a3b732011-07-27 14:20:51 +00006673 dma_addr_t map, u32 len, u32 flags,
6674 u32 mss, u32 vlan)
6675{
6676 struct tg3 *tp = tnapi->tp;
6677 bool hwbug = false;
6678
6679 if (tg3_flag(tp, SHORT_DMA_BUG) && len <= 8)
Rusty Russell3db1cd52011-12-19 13:56:45 +00006680 hwbug = true;
Matt Carlsond1a3b732011-07-27 14:20:51 +00006681
6682 if (tg3_4g_overflow_test(map, len))
Rusty Russell3db1cd52011-12-19 13:56:45 +00006683 hwbug = true;
Matt Carlsond1a3b732011-07-27 14:20:51 +00006684
6685 if (tg3_40bit_overflow_test(tp, map, len))
Rusty Russell3db1cd52011-12-19 13:56:45 +00006686 hwbug = true;
Matt Carlsond1a3b732011-07-27 14:20:51 +00006687
Matt Carlsona4cb4282011-12-14 11:09:58 +00006688 if (tp->dma_limit) {
Matt Carlsonb9e45482011-11-04 09:14:59 +00006689 u32 prvidx = *entry;
Matt Carlsone31aa982011-07-27 14:20:53 +00006690 u32 tmp_flag = flags & ~TXD_FLAG_END;
Matt Carlsona4cb4282011-12-14 11:09:58 +00006691 while (len > tp->dma_limit && *budget) {
6692 u32 frag_len = tp->dma_limit;
6693 len -= tp->dma_limit;
Matt Carlsone31aa982011-07-27 14:20:53 +00006694
Matt Carlsonb9e45482011-11-04 09:14:59 +00006695 /* Avoid the 8byte DMA problem */
6696 if (len <= 8) {
Matt Carlsona4cb4282011-12-14 11:09:58 +00006697 len += tp->dma_limit / 2;
6698 frag_len = tp->dma_limit / 2;
Matt Carlsone31aa982011-07-27 14:20:53 +00006699 }
6700
Matt Carlsonb9e45482011-11-04 09:14:59 +00006701 tnapi->tx_buffers[*entry].fragmented = true;
6702
6703 tg3_tx_set_bd(&tnapi->tx_ring[*entry], map,
6704 frag_len, tmp_flag, mss, vlan);
6705 *budget -= 1;
6706 prvidx = *entry;
6707 *entry = NEXT_TX(*entry);
6708
Matt Carlsone31aa982011-07-27 14:20:53 +00006709 map += frag_len;
6710 }
6711
6712 if (len) {
6713 if (*budget) {
6714 tg3_tx_set_bd(&tnapi->tx_ring[*entry], map,
6715 len, flags, mss, vlan);
Matt Carlsonb9e45482011-11-04 09:14:59 +00006716 *budget -= 1;
Matt Carlsone31aa982011-07-27 14:20:53 +00006717 *entry = NEXT_TX(*entry);
6718 } else {
Rusty Russell3db1cd52011-12-19 13:56:45 +00006719 hwbug = true;
Matt Carlsonb9e45482011-11-04 09:14:59 +00006720 tnapi->tx_buffers[prvidx].fragmented = false;
Matt Carlsone31aa982011-07-27 14:20:53 +00006721 }
6722 }
6723 } else {
Matt Carlson84b67b22011-07-27 14:20:52 +00006724 tg3_tx_set_bd(&tnapi->tx_ring[*entry], map,
6725 len, flags, mss, vlan);
Matt Carlsone31aa982011-07-27 14:20:53 +00006726 *entry = NEXT_TX(*entry);
6727 }
Matt Carlsond1a3b732011-07-27 14:20:51 +00006728
6729 return hwbug;
6730}
6731
Matt Carlson0d681b22011-07-27 14:20:49 +00006732static void tg3_tx_skb_unmap(struct tg3_napi *tnapi, u32 entry, int last)
Matt Carlson432aa7e2011-05-19 12:12:45 +00006733{
6734 int i;
Matt Carlson0d681b22011-07-27 14:20:49 +00006735 struct sk_buff *skb;
Matt Carlsondf8944c2011-07-27 14:20:46 +00006736 struct tg3_tx_ring_info *txb = &tnapi->tx_buffers[entry];
Matt Carlson432aa7e2011-05-19 12:12:45 +00006737
Matt Carlson0d681b22011-07-27 14:20:49 +00006738 skb = txb->skb;
6739 txb->skb = NULL;
6740
Matt Carlson432aa7e2011-05-19 12:12:45 +00006741 pci_unmap_single(tnapi->tp->pdev,
6742 dma_unmap_addr(txb, mapping),
6743 skb_headlen(skb),
6744 PCI_DMA_TODEVICE);
Matt Carlsone01ee142011-07-27 14:20:50 +00006745
6746 while (txb->fragmented) {
6747 txb->fragmented = false;
6748 entry = NEXT_TX(entry);
6749 txb = &tnapi->tx_buffers[entry];
6750 }
6751
Matt Carlsonba1142e2011-11-04 09:15:00 +00006752 for (i = 0; i <= last; i++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00006753 const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
Matt Carlson432aa7e2011-05-19 12:12:45 +00006754
6755 entry = NEXT_TX(entry);
6756 txb = &tnapi->tx_buffers[entry];
6757
6758 pci_unmap_page(tnapi->tp->pdev,
6759 dma_unmap_addr(txb, mapping),
Eric Dumazet9e903e02011-10-18 21:00:24 +00006760 skb_frag_size(frag), PCI_DMA_TODEVICE);
Matt Carlsone01ee142011-07-27 14:20:50 +00006761
6762 while (txb->fragmented) {
6763 txb->fragmented = false;
6764 entry = NEXT_TX(entry);
6765 txb = &tnapi->tx_buffers[entry];
6766 }
Matt Carlson432aa7e2011-05-19 12:12:45 +00006767 }
6768}
6769
Michael Chan72f2afb2006-03-06 19:28:35 -08006770/* Workaround 4GB and 40-bit hardware DMA bugs. */
Matt Carlson24f4efd2009-11-13 13:03:35 +00006771static int tigon3_dma_hwbug_workaround(struct tg3_napi *tnapi,
David S. Miller1805b2f2011-10-24 18:18:09 -04006772 struct sk_buff **pskb,
Matt Carlson84b67b22011-07-27 14:20:52 +00006773 u32 *entry, u32 *budget,
Matt Carlson92cd3a12011-07-27 14:20:47 +00006774 u32 base_flags, u32 mss, u32 vlan)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006775{
Matt Carlson24f4efd2009-11-13 13:03:35 +00006776 struct tg3 *tp = tnapi->tp;
David S. Miller1805b2f2011-10-24 18:18:09 -04006777 struct sk_buff *new_skb, *skb = *pskb;
Michael Chanc58ec932005-09-17 00:46:27 -07006778 dma_addr_t new_addr = 0;
Matt Carlson432aa7e2011-05-19 12:12:45 +00006779 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006780
Matt Carlson41588ba2008-04-19 18:12:33 -07006781 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701)
6782 new_skb = skb_copy(skb, GFP_ATOMIC);
6783 else {
6784 int more_headroom = 4 - ((unsigned long)skb->data & 3);
6785
6786 new_skb = skb_copy_expand(skb,
6787 skb_headroom(skb) + more_headroom,
6788 skb_tailroom(skb), GFP_ATOMIC);
6789 }
6790
Linus Torvalds1da177e2005-04-16 15:20:36 -07006791 if (!new_skb) {
Michael Chanc58ec932005-09-17 00:46:27 -07006792 ret = -1;
6793 } else {
6794 /* New SKB is guaranteed to be linear. */
Alexander Duyckf4188d82009-12-02 16:48:38 +00006795 new_addr = pci_map_single(tp->pdev, new_skb->data, new_skb->len,
6796 PCI_DMA_TODEVICE);
6797 /* Make sure the mapping succeeded */
6798 if (pci_dma_mapping_error(tp->pdev, new_addr)) {
Alexander Duyckf4188d82009-12-02 16:48:38 +00006799 dev_kfree_skb(new_skb);
Michael Chanc58ec932005-09-17 00:46:27 -07006800 ret = -1;
Michael Chanc58ec932005-09-17 00:46:27 -07006801 } else {
Matt Carlsonb9e45482011-11-04 09:14:59 +00006802 u32 save_entry = *entry;
6803
Matt Carlson92cd3a12011-07-27 14:20:47 +00006804 base_flags |= TXD_FLAG_END;
6805
Matt Carlson84b67b22011-07-27 14:20:52 +00006806 tnapi->tx_buffers[*entry].skb = new_skb;
6807 dma_unmap_addr_set(&tnapi->tx_buffers[*entry],
Matt Carlson432aa7e2011-05-19 12:12:45 +00006808 mapping, new_addr);
6809
Matt Carlson84b67b22011-07-27 14:20:52 +00006810 if (tg3_tx_frag_set(tnapi, entry, budget, new_addr,
Matt Carlsond1a3b732011-07-27 14:20:51 +00006811 new_skb->len, base_flags,
6812 mss, vlan)) {
Matt Carlsonba1142e2011-11-04 09:15:00 +00006813 tg3_tx_skb_unmap(tnapi, save_entry, -1);
Matt Carlsond1a3b732011-07-27 14:20:51 +00006814 dev_kfree_skb(new_skb);
6815 ret = -1;
6816 }
Michael Chanc58ec932005-09-17 00:46:27 -07006817 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006818 }
6819
Linus Torvalds1da177e2005-04-16 15:20:36 -07006820 dev_kfree_skb(skb);
David S. Miller1805b2f2011-10-24 18:18:09 -04006821 *pskb = new_skb;
Michael Chanc58ec932005-09-17 00:46:27 -07006822 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006823}
6824
Matt Carlson2ffcc982011-05-19 12:12:44 +00006825static netdev_tx_t tg3_start_xmit(struct sk_buff *, struct net_device *);
Michael Chan52c0fd82006-06-29 20:15:54 -07006826
6827/* Use GSO to workaround a rare TSO bug that may be triggered when the
6828 * TSO header is greater than 80 bytes.
6829 */
6830static int tg3_tso_bug(struct tg3 *tp, struct sk_buff *skb)
6831{
6832 struct sk_buff *segs, *nskb;
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006833 u32 frag_cnt_est = skb_shinfo(skb)->gso_segs * 3;
Michael Chan52c0fd82006-06-29 20:15:54 -07006834
6835 /* Estimate the number of fragments in the worst case */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006836 if (unlikely(tg3_tx_avail(&tp->napi[0]) <= frag_cnt_est)) {
Michael Chan52c0fd82006-06-29 20:15:54 -07006837 netif_stop_queue(tp->dev);
Matt Carlsonf65aac12010-08-02 11:26:03 +00006838
6839 /* netif_tx_stop_queue() must be done before checking
6840 * checking tx index in tg3_tx_avail() below, because in
6841 * tg3_tx(), we update tx index before checking for
6842 * netif_tx_queue_stopped().
6843 */
6844 smp_mb();
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006845 if (tg3_tx_avail(&tp->napi[0]) <= frag_cnt_est)
Michael Chan7f62ad52007-02-20 23:25:40 -08006846 return NETDEV_TX_BUSY;
6847
6848 netif_wake_queue(tp->dev);
Michael Chan52c0fd82006-06-29 20:15:54 -07006849 }
6850
6851 segs = skb_gso_segment(skb, tp->dev->features & ~NETIF_F_TSO);
Hirofumi Nakagawa801678c2008-04-29 01:03:09 -07006852 if (IS_ERR(segs))
Michael Chan52c0fd82006-06-29 20:15:54 -07006853 goto tg3_tso_bug_end;
6854
6855 do {
6856 nskb = segs;
6857 segs = segs->next;
6858 nskb->next = NULL;
Matt Carlson2ffcc982011-05-19 12:12:44 +00006859 tg3_start_xmit(nskb, tp->dev);
Michael Chan52c0fd82006-06-29 20:15:54 -07006860 } while (segs);
6861
6862tg3_tso_bug_end:
6863 dev_kfree_skb(skb);
6864
6865 return NETDEV_TX_OK;
6866}
Michael Chan52c0fd82006-06-29 20:15:54 -07006867
Michael Chan5a6f3072006-03-20 22:28:05 -08006868/* hard_start_xmit for devices that have the 4G bug and/or 40-bit bug and
Joe Perches63c3a662011-04-26 08:12:10 +00006869 * support TG3_FLAG_HW_TSO_1 or firmware TSO only.
Michael Chan5a6f3072006-03-20 22:28:05 -08006870 */
Matt Carlson2ffcc982011-05-19 12:12:44 +00006871static netdev_tx_t tg3_start_xmit(struct sk_buff *skb, struct net_device *dev)
Michael Chan5a6f3072006-03-20 22:28:05 -08006872{
6873 struct tg3 *tp = netdev_priv(dev);
Matt Carlson92cd3a12011-07-27 14:20:47 +00006874 u32 len, entry, base_flags, mss, vlan = 0;
Matt Carlson84b67b22011-07-27 14:20:52 +00006875 u32 budget;
Matt Carlson432aa7e2011-05-19 12:12:45 +00006876 int i = -1, would_hit_hwbug;
David S. Miller90079ce2008-09-11 04:52:51 -07006877 dma_addr_t mapping;
Matt Carlson24f4efd2009-11-13 13:03:35 +00006878 struct tg3_napi *tnapi;
6879 struct netdev_queue *txq;
Matt Carlson432aa7e2011-05-19 12:12:45 +00006880 unsigned int last;
Alexander Duyckf4188d82009-12-02 16:48:38 +00006881
Matt Carlson24f4efd2009-11-13 13:03:35 +00006882 txq = netdev_get_tx_queue(dev, skb_get_queue_mapping(skb));
6883 tnapi = &tp->napi[skb_get_queue_mapping(skb)];
Joe Perches63c3a662011-04-26 08:12:10 +00006884 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlson24f4efd2009-11-13 13:03:35 +00006885 tnapi++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006886
Matt Carlson84b67b22011-07-27 14:20:52 +00006887 budget = tg3_tx_avail(tnapi);
6888
Michael Chan00b70502006-06-17 21:58:45 -07006889 /* We are running in BH disabled context with netif_tx_lock
Stephen Hemmingerbea33482007-10-03 16:41:36 -07006890 * and TX reclaim runs via tp->napi.poll inside of a software
David S. Millerf47c11e2005-06-24 20:18:35 -07006891 * interrupt. Furthermore, IRQ processing runs lockless so we have
6892 * no IRQ context deadlocks to worry about either. Rejoice!
Linus Torvalds1da177e2005-04-16 15:20:36 -07006893 */
Matt Carlson84b67b22011-07-27 14:20:52 +00006894 if (unlikely(budget <= (skb_shinfo(skb)->nr_frags + 1))) {
Matt Carlson24f4efd2009-11-13 13:03:35 +00006895 if (!netif_tx_queue_stopped(txq)) {
6896 netif_tx_stop_queue(txq);
Stephen Hemminger1f064a82005-12-06 17:36:44 -08006897
6898 /* This is a hard error, log it. */
Matt Carlson5129c3a2010-04-05 10:19:23 +00006899 netdev_err(dev,
6900 "BUG! Tx Ring full when queue awake!\n");
Stephen Hemminger1f064a82005-12-06 17:36:44 -08006901 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006902 return NETDEV_TX_BUSY;
6903 }
6904
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006905 entry = tnapi->tx_prod;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006906 base_flags = 0;
Patrick McHardy84fa7932006-08-29 16:44:56 -07006907 if (skb->ip_summed == CHECKSUM_PARTIAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006908 base_flags |= TXD_FLAG_TCPUDP_CSUM;
Matt Carlson24f4efd2009-11-13 13:03:35 +00006909
Matt Carlsonbe98da62010-07-11 09:31:46 +00006910 mss = skb_shinfo(skb)->gso_size;
6911 if (mss) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07006912 struct iphdr *iph;
Matt Carlson34195c32010-07-11 09:31:42 +00006913 u32 tcp_opt_len, hdr_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006914
6915 if (skb_header_cloned(skb) &&
Eric Dumazet48855432011-10-24 07:53:03 +00006916 pskb_expand_head(skb, 0, 0, GFP_ATOMIC))
6917 goto drop;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006918
Matt Carlson34195c32010-07-11 09:31:42 +00006919 iph = ip_hdr(skb);
Arnaldo Carvalho de Meloab6a5bb2007-03-18 17:43:48 -07006920 tcp_opt_len = tcp_optlen(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006921
Eric Dumazeta5a11952012-01-23 01:22:09 +00006922 hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb) - ETH_HLEN;
Matt Carlson34195c32010-07-11 09:31:42 +00006923
Eric Dumazeta5a11952012-01-23 01:22:09 +00006924 if (!skb_is_gso_v6(skb)) {
Matt Carlson34195c32010-07-11 09:31:42 +00006925 iph->check = 0;
6926 iph->tot_len = htons(mss + hdr_len);
6927 }
6928
Michael Chan52c0fd82006-06-29 20:15:54 -07006929 if (unlikely((ETH_HLEN + hdr_len) > 80) &&
Joe Perches63c3a662011-04-26 08:12:10 +00006930 tg3_flag(tp, TSO_BUG))
Matt Carlsonde6f31e2010-04-12 06:58:30 +00006931 return tg3_tso_bug(tp, skb);
Michael Chan52c0fd82006-06-29 20:15:54 -07006932
Linus Torvalds1da177e2005-04-16 15:20:36 -07006933 base_flags |= (TXD_FLAG_CPU_PRE_DMA |
6934 TXD_FLAG_CPU_POST_DMA);
6935
Joe Perches63c3a662011-04-26 08:12:10 +00006936 if (tg3_flag(tp, HW_TSO_1) ||
6937 tg3_flag(tp, HW_TSO_2) ||
6938 tg3_flag(tp, HW_TSO_3)) {
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07006939 tcp_hdr(skb)->check = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006940 base_flags &= ~TXD_FLAG_TCPUDP_CSUM;
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07006941 } else
6942 tcp_hdr(skb)->check = ~csum_tcpudp_magic(iph->saddr,
6943 iph->daddr, 0,
6944 IPPROTO_TCP,
6945 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006946
Joe Perches63c3a662011-04-26 08:12:10 +00006947 if (tg3_flag(tp, HW_TSO_3)) {
Matt Carlson615774f2009-11-13 13:03:39 +00006948 mss |= (hdr_len & 0xc) << 12;
6949 if (hdr_len & 0x10)
6950 base_flags |= 0x00000010;
6951 base_flags |= (hdr_len & 0x3e0) << 5;
Joe Perches63c3a662011-04-26 08:12:10 +00006952 } else if (tg3_flag(tp, HW_TSO_2))
Matt Carlson92c6b8d2009-11-02 14:23:27 +00006953 mss |= hdr_len << 9;
Joe Perches63c3a662011-04-26 08:12:10 +00006954 else if (tg3_flag(tp, HW_TSO_1) ||
Matt Carlson92c6b8d2009-11-02 14:23:27 +00006955 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07006956 if (tcp_opt_len || iph->ihl > 5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006957 int tsflags;
6958
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07006959 tsflags = (iph->ihl - 5) + (tcp_opt_len >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006960 mss |= (tsflags << 11);
6961 }
6962 } else {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07006963 if (tcp_opt_len || iph->ihl > 5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006964 int tsflags;
6965
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07006966 tsflags = (iph->ihl - 5) + (tcp_opt_len >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006967 base_flags |= tsflags << 12;
6968 }
6969 }
6970 }
Matt Carlsonbf933c82011-01-25 15:58:49 +00006971
Matt Carlson93a700a2011-08-31 11:44:54 +00006972 if (tg3_flag(tp, USE_JUMBO_BDFLAG) &&
6973 !mss && skb->len > VLAN_ETH_FRAME_LEN)
6974 base_flags |= TXD_FLAG_JMB_PKT;
6975
Matt Carlson92cd3a12011-07-27 14:20:47 +00006976 if (vlan_tx_tag_present(skb)) {
6977 base_flags |= TXD_FLAG_VLAN;
6978 vlan = vlan_tx_tag_get(skb);
6979 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006980
Alexander Duyckf4188d82009-12-02 16:48:38 +00006981 len = skb_headlen(skb);
6982
6983 mapping = pci_map_single(tp->pdev, skb->data, len, PCI_DMA_TODEVICE);
Eric Dumazet48855432011-10-24 07:53:03 +00006984 if (pci_dma_mapping_error(tp->pdev, mapping))
6985 goto drop;
6986
David S. Miller90079ce2008-09-11 04:52:51 -07006987
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006988 tnapi->tx_buffers[entry].skb = skb;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006989 dma_unmap_addr_set(&tnapi->tx_buffers[entry], mapping, mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006990
6991 would_hit_hwbug = 0;
6992
Joe Perches63c3a662011-04-26 08:12:10 +00006993 if (tg3_flag(tp, 5701_DMA_BUG))
Michael Chanc58ec932005-09-17 00:46:27 -07006994 would_hit_hwbug = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006995
Matt Carlson84b67b22011-07-27 14:20:52 +00006996 if (tg3_tx_frag_set(tnapi, &entry, &budget, mapping, len, base_flags |
Matt Carlsond1a3b732011-07-27 14:20:51 +00006997 ((skb_shinfo(skb)->nr_frags == 0) ? TXD_FLAG_END : 0),
Matt Carlsonba1142e2011-11-04 09:15:00 +00006998 mss, vlan)) {
Matt Carlsond1a3b732011-07-27 14:20:51 +00006999 would_hit_hwbug = 1;
Matt Carlsonba1142e2011-11-04 09:15:00 +00007000 } else if (skb_shinfo(skb)->nr_frags > 0) {
Matt Carlson92cd3a12011-07-27 14:20:47 +00007001 u32 tmp_mss = mss;
7002
7003 if (!tg3_flag(tp, HW_TSO_1) &&
7004 !tg3_flag(tp, HW_TSO_2) &&
7005 !tg3_flag(tp, HW_TSO_3))
7006 tmp_mss = 0;
7007
Matt Carlsonc5665a52012-02-13 10:20:12 +00007008 /* Now loop through additional data
7009 * fragments, and queue them.
7010 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007011 last = skb_shinfo(skb)->nr_frags - 1;
7012 for (i = 0; i <= last; i++) {
7013 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
7014
Eric Dumazet9e903e02011-10-18 21:00:24 +00007015 len = skb_frag_size(frag);
Ian Campbelldc234d02011-08-24 22:28:11 +00007016 mapping = skb_frag_dma_map(&tp->pdev->dev, frag, 0,
Ian Campbell5d6bcdf2011-10-06 11:10:48 +01007017 len, DMA_TO_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007018
Matt Carlsonf3f3f272009-08-28 14:03:21 +00007019 tnapi->tx_buffers[entry].skb = NULL;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00007020 dma_unmap_addr_set(&tnapi->tx_buffers[entry], mapping,
Alexander Duyckf4188d82009-12-02 16:48:38 +00007021 mapping);
Ian Campbell5d6bcdf2011-10-06 11:10:48 +01007022 if (dma_mapping_error(&tp->pdev->dev, mapping))
Alexander Duyckf4188d82009-12-02 16:48:38 +00007023 goto dma_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007024
Matt Carlsonb9e45482011-11-04 09:14:59 +00007025 if (!budget ||
7026 tg3_tx_frag_set(tnapi, &entry, &budget, mapping,
Matt Carlson84b67b22011-07-27 14:20:52 +00007027 len, base_flags |
7028 ((i == last) ? TXD_FLAG_END : 0),
Matt Carlsonb9e45482011-11-04 09:14:59 +00007029 tmp_mss, vlan)) {
Matt Carlson92c6b8d2009-11-02 14:23:27 +00007030 would_hit_hwbug = 1;
Matt Carlsonb9e45482011-11-04 09:14:59 +00007031 break;
7032 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007033 }
7034 }
7035
7036 if (would_hit_hwbug) {
Matt Carlson0d681b22011-07-27 14:20:49 +00007037 tg3_tx_skb_unmap(tnapi, tnapi->tx_prod, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007038
7039 /* If the workaround fails due to memory/mapping
7040 * failure, silently drop this packet.
7041 */
Matt Carlson84b67b22011-07-27 14:20:52 +00007042 entry = tnapi->tx_prod;
7043 budget = tg3_tx_avail(tnapi);
David S. Miller1805b2f2011-10-24 18:18:09 -04007044 if (tigon3_dma_hwbug_workaround(tnapi, &skb, &entry, &budget,
Matt Carlson84b67b22011-07-27 14:20:52 +00007045 base_flags, mss, vlan))
Eric Dumazet48855432011-10-24 07:53:03 +00007046 goto drop_nofree;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007047 }
7048
Richard Cochrand515b452011-06-19 03:31:41 +00007049 skb_tx_timestamp(skb);
Tom Herbert5cb917b2012-03-05 19:53:50 +00007050 netdev_tx_sent_queue(txq, skb->len);
Richard Cochrand515b452011-06-19 03:31:41 +00007051
Michael Chan6541b802012-03-04 14:48:14 +00007052 /* Sync BD data before updating mailbox */
7053 wmb();
7054
Linus Torvalds1da177e2005-04-16 15:20:36 -07007055 /* Packets are ready, update Tx producer idx local and on card. */
Matt Carlson24f4efd2009-11-13 13:03:35 +00007056 tw32_tx_mbox(tnapi->prodmbox, entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007057
Matt Carlsonf3f3f272009-08-28 14:03:21 +00007058 tnapi->tx_prod = entry;
7059 if (unlikely(tg3_tx_avail(tnapi) <= (MAX_SKB_FRAGS + 1))) {
Matt Carlson24f4efd2009-11-13 13:03:35 +00007060 netif_tx_stop_queue(txq);
Matt Carlsonf65aac12010-08-02 11:26:03 +00007061
7062 /* netif_tx_stop_queue() must be done before checking
7063 * checking tx index in tg3_tx_avail() below, because in
7064 * tg3_tx(), we update tx index before checking for
7065 * netif_tx_queue_stopped().
7066 */
7067 smp_mb();
Matt Carlsonf3f3f272009-08-28 14:03:21 +00007068 if (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi))
Matt Carlson24f4efd2009-11-13 13:03:35 +00007069 netif_tx_wake_queue(txq);
Michael Chan51b91462005-09-01 17:41:28 -07007070 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007071
Eric Dumazetcdd0db02009-05-28 00:00:41 +00007072 mmiowb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007073 return NETDEV_TX_OK;
Alexander Duyckf4188d82009-12-02 16:48:38 +00007074
7075dma_error:
Matt Carlsonba1142e2011-11-04 09:15:00 +00007076 tg3_tx_skb_unmap(tnapi, tnapi->tx_prod, --i);
Matt Carlson432aa7e2011-05-19 12:12:45 +00007077 tnapi->tx_buffers[tnapi->tx_prod].skb = NULL;
Eric Dumazet48855432011-10-24 07:53:03 +00007078drop:
7079 dev_kfree_skb(skb);
7080drop_nofree:
7081 tp->tx_dropped++;
Alexander Duyckf4188d82009-12-02 16:48:38 +00007082 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007083}
7084
Matt Carlson6e01b202011-08-19 13:58:20 +00007085static void tg3_mac_loopback(struct tg3 *tp, bool enable)
7086{
7087 if (enable) {
7088 tp->mac_mode &= ~(MAC_MODE_HALF_DUPLEX |
7089 MAC_MODE_PORT_MODE_MASK);
7090
7091 tp->mac_mode |= MAC_MODE_PORT_INT_LPBACK;
7092
7093 if (!tg3_flag(tp, 5705_PLUS))
7094 tp->mac_mode |= MAC_MODE_LINK_POLARITY;
7095
7096 if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
7097 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
7098 else
7099 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
7100 } else {
7101 tp->mac_mode &= ~MAC_MODE_PORT_INT_LPBACK;
7102
7103 if (tg3_flag(tp, 5705_PLUS) ||
7104 (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) ||
7105 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700)
7106 tp->mac_mode &= ~MAC_MODE_LINK_POLARITY;
7107 }
7108
7109 tw32(MAC_MODE, tp->mac_mode);
7110 udelay(40);
7111}
7112
Matt Carlson941ec902011-08-19 13:58:23 +00007113static int tg3_phy_lpbk_set(struct tg3 *tp, u32 speed, bool extlpbk)
Matt Carlson5e5a7f32011-08-19 13:58:21 +00007114{
Matt Carlson941ec902011-08-19 13:58:23 +00007115 u32 val, bmcr, mac_mode, ptest = 0;
Matt Carlson5e5a7f32011-08-19 13:58:21 +00007116
7117 tg3_phy_toggle_apd(tp, false);
7118 tg3_phy_toggle_automdix(tp, 0);
7119
Matt Carlson941ec902011-08-19 13:58:23 +00007120 if (extlpbk && tg3_phy_set_extloopbk(tp))
7121 return -EIO;
7122
7123 bmcr = BMCR_FULLDPLX;
Matt Carlson5e5a7f32011-08-19 13:58:21 +00007124 switch (speed) {
7125 case SPEED_10:
7126 break;
7127 case SPEED_100:
7128 bmcr |= BMCR_SPEED100;
7129 break;
7130 case SPEED_1000:
7131 default:
7132 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
7133 speed = SPEED_100;
7134 bmcr |= BMCR_SPEED100;
7135 } else {
7136 speed = SPEED_1000;
7137 bmcr |= BMCR_SPEED1000;
7138 }
7139 }
7140
Matt Carlson941ec902011-08-19 13:58:23 +00007141 if (extlpbk) {
7142 if (!(tp->phy_flags & TG3_PHYFLG_IS_FET)) {
7143 tg3_readphy(tp, MII_CTRL1000, &val);
7144 val |= CTL1000_AS_MASTER |
7145 CTL1000_ENABLE_MASTER;
7146 tg3_writephy(tp, MII_CTRL1000, val);
7147 } else {
7148 ptest = MII_TG3_FET_PTEST_TRIM_SEL |
7149 MII_TG3_FET_PTEST_TRIM_2;
7150 tg3_writephy(tp, MII_TG3_FET_PTEST, ptest);
7151 }
7152 } else
7153 bmcr |= BMCR_LOOPBACK;
7154
Matt Carlson5e5a7f32011-08-19 13:58:21 +00007155 tg3_writephy(tp, MII_BMCR, bmcr);
7156
7157 /* The write needs to be flushed for the FETs */
7158 if (tp->phy_flags & TG3_PHYFLG_IS_FET)
7159 tg3_readphy(tp, MII_BMCR, &bmcr);
7160
7161 udelay(40);
7162
7163 if ((tp->phy_flags & TG3_PHYFLG_IS_FET) &&
7164 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785) {
Matt Carlson941ec902011-08-19 13:58:23 +00007165 tg3_writephy(tp, MII_TG3_FET_PTEST, ptest |
Matt Carlson5e5a7f32011-08-19 13:58:21 +00007166 MII_TG3_FET_PTEST_FRC_TX_LINK |
7167 MII_TG3_FET_PTEST_FRC_TX_LOCK);
7168
7169 /* The write needs to be flushed for the AC131 */
7170 tg3_readphy(tp, MII_TG3_FET_PTEST, &val);
7171 }
7172
7173 /* Reset to prevent losing 1st rx packet intermittently */
7174 if ((tp->phy_flags & TG3_PHYFLG_MII_SERDES) &&
7175 tg3_flag(tp, 5780_CLASS)) {
7176 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
7177 udelay(10);
7178 tw32_f(MAC_RX_MODE, tp->rx_mode);
7179 }
7180
7181 mac_mode = tp->mac_mode &
7182 ~(MAC_MODE_PORT_MODE_MASK | MAC_MODE_HALF_DUPLEX);
7183 if (speed == SPEED_1000)
7184 mac_mode |= MAC_MODE_PORT_MODE_GMII;
7185 else
7186 mac_mode |= MAC_MODE_PORT_MODE_MII;
7187
7188 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700) {
7189 u32 masked_phy_id = tp->phy_id & TG3_PHY_ID_MASK;
7190
7191 if (masked_phy_id == TG3_PHY_ID_BCM5401)
7192 mac_mode &= ~MAC_MODE_LINK_POLARITY;
7193 else if (masked_phy_id == TG3_PHY_ID_BCM5411)
7194 mac_mode |= MAC_MODE_LINK_POLARITY;
7195
7196 tg3_writephy(tp, MII_TG3_EXT_CTRL,
7197 MII_TG3_EXT_CTRL_LNK3_LED_MODE);
7198 }
7199
7200 tw32(MAC_MODE, mac_mode);
7201 udelay(40);
Matt Carlson941ec902011-08-19 13:58:23 +00007202
7203 return 0;
Matt Carlson5e5a7f32011-08-19 13:58:21 +00007204}
7205
Michał Mirosławc8f44af2011-11-15 15:29:55 +00007206static void tg3_set_loopback(struct net_device *dev, netdev_features_t features)
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00007207{
7208 struct tg3 *tp = netdev_priv(dev);
7209
7210 if (features & NETIF_F_LOOPBACK) {
7211 if (tp->mac_mode & MAC_MODE_PORT_INT_LPBACK)
7212 return;
7213
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00007214 spin_lock_bh(&tp->lock);
Matt Carlson6e01b202011-08-19 13:58:20 +00007215 tg3_mac_loopback(tp, true);
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00007216 netif_carrier_on(tp->dev);
7217 spin_unlock_bh(&tp->lock);
7218 netdev_info(dev, "Internal MAC loopback mode enabled.\n");
7219 } else {
7220 if (!(tp->mac_mode & MAC_MODE_PORT_INT_LPBACK))
7221 return;
7222
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00007223 spin_lock_bh(&tp->lock);
Matt Carlson6e01b202011-08-19 13:58:20 +00007224 tg3_mac_loopback(tp, false);
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00007225 /* Force link status check */
7226 tg3_setup_phy(tp, 1);
7227 spin_unlock_bh(&tp->lock);
7228 netdev_info(dev, "Internal MAC loopback mode disabled.\n");
7229 }
7230}
7231
Michał Mirosławc8f44af2011-11-15 15:29:55 +00007232static netdev_features_t tg3_fix_features(struct net_device *dev,
7233 netdev_features_t features)
Michał Mirosławdc668912011-04-07 03:35:07 +00007234{
7235 struct tg3 *tp = netdev_priv(dev);
7236
Joe Perches63c3a662011-04-26 08:12:10 +00007237 if (dev->mtu > ETH_DATA_LEN && tg3_flag(tp, 5780_CLASS))
Michał Mirosławdc668912011-04-07 03:35:07 +00007238 features &= ~NETIF_F_ALL_TSO;
7239
7240 return features;
7241}
7242
Michał Mirosławc8f44af2011-11-15 15:29:55 +00007243static int tg3_set_features(struct net_device *dev, netdev_features_t features)
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00007244{
Michał Mirosławc8f44af2011-11-15 15:29:55 +00007245 netdev_features_t changed = dev->features ^ features;
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00007246
7247 if ((changed & NETIF_F_LOOPBACK) && netif_running(dev))
7248 tg3_set_loopback(dev, features);
7249
7250 return 0;
7251}
7252
Matt Carlson21f581a2009-08-28 14:00:25 +00007253static void tg3_rx_prodring_free(struct tg3 *tp,
7254 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007255{
Linus Torvalds1da177e2005-04-16 15:20:36 -07007256 int i;
7257
Matt Carlson8fea32b2010-09-15 08:59:58 +00007258 if (tpr != &tp->napi[0].prodring) {
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007259 for (i = tpr->rx_std_cons_idx; i != tpr->rx_std_prod_idx;
Matt Carlson2c49a442010-09-30 10:34:35 +00007260 i = (i + 1) & tp->rx_std_ring_mask)
Eric Dumazet9205fd92011-11-18 06:47:01 +00007261 tg3_rx_data_free(tp, &tpr->rx_std_buffers[i],
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007262 tp->rx_pkt_map_sz);
7263
Joe Perches63c3a662011-04-26 08:12:10 +00007264 if (tg3_flag(tp, JUMBO_CAPABLE)) {
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007265 for (i = tpr->rx_jmb_cons_idx;
7266 i != tpr->rx_jmb_prod_idx;
Matt Carlson2c49a442010-09-30 10:34:35 +00007267 i = (i + 1) & tp->rx_jmb_ring_mask) {
Eric Dumazet9205fd92011-11-18 06:47:01 +00007268 tg3_rx_data_free(tp, &tpr->rx_jmb_buffers[i],
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007269 TG3_RX_JMB_MAP_SZ);
7270 }
7271 }
7272
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007273 return;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007274 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007275
Matt Carlson2c49a442010-09-30 10:34:35 +00007276 for (i = 0; i <= tp->rx_std_ring_mask; i++)
Eric Dumazet9205fd92011-11-18 06:47:01 +00007277 tg3_rx_data_free(tp, &tpr->rx_std_buffers[i],
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007278 tp->rx_pkt_map_sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007279
Joe Perches63c3a662011-04-26 08:12:10 +00007280 if (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS)) {
Matt Carlson2c49a442010-09-30 10:34:35 +00007281 for (i = 0; i <= tp->rx_jmb_ring_mask; i++)
Eric Dumazet9205fd92011-11-18 06:47:01 +00007282 tg3_rx_data_free(tp, &tpr->rx_jmb_buffers[i],
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007283 TG3_RX_JMB_MAP_SZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007284 }
7285}
7286
Matt Carlsonc6cdf432010-04-05 10:19:26 +00007287/* Initialize rx rings for packet processing.
Linus Torvalds1da177e2005-04-16 15:20:36 -07007288 *
7289 * The chip has been shut down and the driver detached from
7290 * the networking, so no interrupts or new tx packets will
7291 * end up in the driver. tp->{tx,}lock are held and thus
7292 * we may not sleep.
7293 */
Matt Carlson21f581a2009-08-28 14:00:25 +00007294static int tg3_rx_prodring_alloc(struct tg3 *tp,
7295 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007296{
Matt Carlson287be122009-08-28 13:58:46 +00007297 u32 i, rx_pkt_dma_sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007298
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007299 tpr->rx_std_cons_idx = 0;
7300 tpr->rx_std_prod_idx = 0;
7301 tpr->rx_jmb_cons_idx = 0;
7302 tpr->rx_jmb_prod_idx = 0;
7303
Matt Carlson8fea32b2010-09-15 08:59:58 +00007304 if (tpr != &tp->napi[0].prodring) {
Matt Carlson2c49a442010-09-30 10:34:35 +00007305 memset(&tpr->rx_std_buffers[0], 0,
7306 TG3_RX_STD_BUFF_RING_SIZE(tp));
Matt Carlson48035722010-10-14 10:37:43 +00007307 if (tpr->rx_jmb_buffers)
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007308 memset(&tpr->rx_jmb_buffers[0], 0,
Matt Carlson2c49a442010-09-30 10:34:35 +00007309 TG3_RX_JMB_BUFF_RING_SIZE(tp));
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007310 goto done;
7311 }
7312
Linus Torvalds1da177e2005-04-16 15:20:36 -07007313 /* Zero out all descriptors. */
Matt Carlson2c49a442010-09-30 10:34:35 +00007314 memset(tpr->rx_std, 0, TG3_RX_STD_RING_BYTES(tp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007315
Matt Carlson287be122009-08-28 13:58:46 +00007316 rx_pkt_dma_sz = TG3_RX_STD_DMA_SZ;
Joe Perches63c3a662011-04-26 08:12:10 +00007317 if (tg3_flag(tp, 5780_CLASS) &&
Matt Carlson287be122009-08-28 13:58:46 +00007318 tp->dev->mtu > ETH_DATA_LEN)
7319 rx_pkt_dma_sz = TG3_RX_JMB_DMA_SZ;
7320 tp->rx_pkt_map_sz = TG3_RX_DMA_TO_MAP_SZ(rx_pkt_dma_sz);
Michael Chan7e72aad2005-07-25 12:31:17 -07007321
Linus Torvalds1da177e2005-04-16 15:20:36 -07007322 /* Initialize invariants of the rings, we only set this
7323 * stuff once. This works because the card does not
7324 * write into the rx buffer posting rings.
7325 */
Matt Carlson2c49a442010-09-30 10:34:35 +00007326 for (i = 0; i <= tp->rx_std_ring_mask; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007327 struct tg3_rx_buffer_desc *rxd;
7328
Matt Carlson21f581a2009-08-28 14:00:25 +00007329 rxd = &tpr->rx_std[i];
Matt Carlson287be122009-08-28 13:58:46 +00007330 rxd->idx_len = rx_pkt_dma_sz << RXD_LEN_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007331 rxd->type_flags = (RXD_FLAG_END << RXD_FLAGS_SHIFT);
7332 rxd->opaque = (RXD_OPAQUE_RING_STD |
7333 (i << RXD_OPAQUE_INDEX_SHIFT));
7334 }
7335
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007336 /* Now allocate fresh SKBs for each rx ring. */
7337 for (i = 0; i < tp->rx_pending; i++) {
Eric Dumazet8d4057a2012-04-27 00:34:49 +00007338 unsigned int frag_size;
7339
7340 if (tg3_alloc_rx_data(tp, tpr, RXD_OPAQUE_RING_STD, i,
7341 &frag_size) < 0) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00007342 netdev_warn(tp->dev,
7343 "Using a smaller RX standard ring. Only "
7344 "%d out of %d buffers were allocated "
7345 "successfully\n", i, tp->rx_pending);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007346 if (i == 0)
7347 goto initfail;
7348 tp->rx_pending = i;
7349 break;
7350 }
7351 }
7352
Joe Perches63c3a662011-04-26 08:12:10 +00007353 if (!tg3_flag(tp, JUMBO_CAPABLE) || tg3_flag(tp, 5780_CLASS))
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007354 goto done;
7355
Matt Carlson2c49a442010-09-30 10:34:35 +00007356 memset(tpr->rx_jmb, 0, TG3_RX_JMB_RING_BYTES(tp));
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007357
Joe Perches63c3a662011-04-26 08:12:10 +00007358 if (!tg3_flag(tp, JUMBO_RING_ENABLE))
Matt Carlson0d86df82010-02-17 15:17:00 +00007359 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007360
Matt Carlson2c49a442010-09-30 10:34:35 +00007361 for (i = 0; i <= tp->rx_jmb_ring_mask; i++) {
Matt Carlson0d86df82010-02-17 15:17:00 +00007362 struct tg3_rx_buffer_desc *rxd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007363
Matt Carlson0d86df82010-02-17 15:17:00 +00007364 rxd = &tpr->rx_jmb[i].std;
7365 rxd->idx_len = TG3_RX_JMB_DMA_SZ << RXD_LEN_SHIFT;
7366 rxd->type_flags = (RXD_FLAG_END << RXD_FLAGS_SHIFT) |
7367 RXD_FLAG_JUMBO;
7368 rxd->opaque = (RXD_OPAQUE_RING_JUMBO |
7369 (i << RXD_OPAQUE_INDEX_SHIFT));
7370 }
7371
7372 for (i = 0; i < tp->rx_jumbo_pending; i++) {
Eric Dumazet8d4057a2012-04-27 00:34:49 +00007373 unsigned int frag_size;
7374
7375 if (tg3_alloc_rx_data(tp, tpr, RXD_OPAQUE_RING_JUMBO, i,
7376 &frag_size) < 0) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00007377 netdev_warn(tp->dev,
7378 "Using a smaller RX jumbo ring. Only %d "
7379 "out of %d buffers were allocated "
7380 "successfully\n", i, tp->rx_jumbo_pending);
Matt Carlson0d86df82010-02-17 15:17:00 +00007381 if (i == 0)
7382 goto initfail;
7383 tp->rx_jumbo_pending = i;
7384 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007385 }
7386 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007387
7388done:
Michael Chan32d8c572006-07-25 16:38:29 -07007389 return 0;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007390
7391initfail:
Matt Carlson21f581a2009-08-28 14:00:25 +00007392 tg3_rx_prodring_free(tp, tpr);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007393 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007394}
7395
Matt Carlson21f581a2009-08-28 14:00:25 +00007396static void tg3_rx_prodring_fini(struct tg3 *tp,
7397 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007398{
Matt Carlson21f581a2009-08-28 14:00:25 +00007399 kfree(tpr->rx_std_buffers);
7400 tpr->rx_std_buffers = NULL;
7401 kfree(tpr->rx_jmb_buffers);
7402 tpr->rx_jmb_buffers = NULL;
7403 if (tpr->rx_std) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00007404 dma_free_coherent(&tp->pdev->dev, TG3_RX_STD_RING_BYTES(tp),
7405 tpr->rx_std, tpr->rx_std_mapping);
Matt Carlson21f581a2009-08-28 14:00:25 +00007406 tpr->rx_std = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007407 }
Matt Carlson21f581a2009-08-28 14:00:25 +00007408 if (tpr->rx_jmb) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00007409 dma_free_coherent(&tp->pdev->dev, TG3_RX_JMB_RING_BYTES(tp),
7410 tpr->rx_jmb, tpr->rx_jmb_mapping);
Matt Carlson21f581a2009-08-28 14:00:25 +00007411 tpr->rx_jmb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007412 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007413}
7414
Matt Carlson21f581a2009-08-28 14:00:25 +00007415static int tg3_rx_prodring_init(struct tg3 *tp,
7416 struct tg3_rx_prodring_set *tpr)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007417{
Matt Carlson2c49a442010-09-30 10:34:35 +00007418 tpr->rx_std_buffers = kzalloc(TG3_RX_STD_BUFF_RING_SIZE(tp),
7419 GFP_KERNEL);
Matt Carlson21f581a2009-08-28 14:00:25 +00007420 if (!tpr->rx_std_buffers)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007421 return -ENOMEM;
7422
Matt Carlson4bae65c2010-11-24 08:31:52 +00007423 tpr->rx_std = dma_alloc_coherent(&tp->pdev->dev,
7424 TG3_RX_STD_RING_BYTES(tp),
7425 &tpr->rx_std_mapping,
7426 GFP_KERNEL);
Matt Carlson21f581a2009-08-28 14:00:25 +00007427 if (!tpr->rx_std)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007428 goto err_out;
7429
Joe Perches63c3a662011-04-26 08:12:10 +00007430 if (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS)) {
Matt Carlson2c49a442010-09-30 10:34:35 +00007431 tpr->rx_jmb_buffers = kzalloc(TG3_RX_JMB_BUFF_RING_SIZE(tp),
Matt Carlson21f581a2009-08-28 14:00:25 +00007432 GFP_KERNEL);
7433 if (!tpr->rx_jmb_buffers)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007434 goto err_out;
7435
Matt Carlson4bae65c2010-11-24 08:31:52 +00007436 tpr->rx_jmb = dma_alloc_coherent(&tp->pdev->dev,
7437 TG3_RX_JMB_RING_BYTES(tp),
7438 &tpr->rx_jmb_mapping,
7439 GFP_KERNEL);
Matt Carlson21f581a2009-08-28 14:00:25 +00007440 if (!tpr->rx_jmb)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007441 goto err_out;
7442 }
7443
7444 return 0;
7445
7446err_out:
Matt Carlson21f581a2009-08-28 14:00:25 +00007447 tg3_rx_prodring_fini(tp, tpr);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007448 return -ENOMEM;
7449}
7450
7451/* Free up pending packets in all rx/tx rings.
7452 *
7453 * The chip has been shut down and the driver detached from
7454 * the networking, so no interrupts or new tx packets will
7455 * end up in the driver. tp->{tx,}lock is not held and we are not
7456 * in an interrupt context and thus may sleep.
7457 */
7458static void tg3_free_rings(struct tg3 *tp)
7459{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007460 int i, j;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007461
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007462 for (j = 0; j < tp->irq_cnt; j++) {
7463 struct tg3_napi *tnapi = &tp->napi[j];
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007464
Matt Carlson8fea32b2010-09-15 08:59:58 +00007465 tg3_rx_prodring_free(tp, &tnapi->prodring);
Matt Carlsonb28f6422010-06-05 17:24:32 +00007466
Matt Carlson0c1d0e22009-09-01 13:16:33 +00007467 if (!tnapi->tx_buffers)
7468 continue;
7469
Matt Carlson0d681b22011-07-27 14:20:49 +00007470 for (i = 0; i < TG3_TX_RING_SIZE; i++) {
7471 struct sk_buff *skb = tnapi->tx_buffers[i].skb;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007472
Matt Carlson0d681b22011-07-27 14:20:49 +00007473 if (!skb)
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007474 continue;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007475
Matt Carlsonba1142e2011-11-04 09:15:00 +00007476 tg3_tx_skb_unmap(tnapi, i,
7477 skb_shinfo(skb)->nr_frags - 1);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007478
7479 dev_kfree_skb_any(skb);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007480 }
Tom Herbert5cb917b2012-03-05 19:53:50 +00007481 netdev_tx_reset_queue(netdev_get_tx_queue(tp->dev, j));
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007482 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007483}
7484
7485/* Initialize tx/rx rings for packet processing.
7486 *
7487 * The chip has been shut down and the driver detached from
7488 * the networking, so no interrupts or new tx packets will
7489 * end up in the driver. tp->{tx,}lock are held and thus
7490 * we may not sleep.
7491 */
7492static int tg3_init_rings(struct tg3 *tp)
7493{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007494 int i;
Matt Carlson72334482009-08-28 14:03:01 +00007495
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007496 /* Free up all the SKBs. */
7497 tg3_free_rings(tp);
7498
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007499 for (i = 0; i < tp->irq_cnt; i++) {
7500 struct tg3_napi *tnapi = &tp->napi[i];
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007501
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007502 tnapi->last_tag = 0;
7503 tnapi->last_irq_tag = 0;
7504 tnapi->hw_status->status = 0;
7505 tnapi->hw_status->status_tag = 0;
7506 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
7507
7508 tnapi->tx_prod = 0;
7509 tnapi->tx_cons = 0;
Matt Carlson0c1d0e22009-09-01 13:16:33 +00007510 if (tnapi->tx_ring)
7511 memset(tnapi->tx_ring, 0, TG3_TX_RING_BYTES);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007512
7513 tnapi->rx_rcb_ptr = 0;
Matt Carlson0c1d0e22009-09-01 13:16:33 +00007514 if (tnapi->rx_rcb)
7515 memset(tnapi->rx_rcb, 0, TG3_RX_RCB_RING_BYTES(tp));
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007516
Matt Carlson8fea32b2010-09-15 08:59:58 +00007517 if (tg3_rx_prodring_alloc(tp, &tnapi->prodring)) {
Matt Carlsone4af1af2010-02-12 14:47:05 +00007518 tg3_free_rings(tp);
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007519 return -ENOMEM;
Matt Carlsone4af1af2010-02-12 14:47:05 +00007520 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007521 }
Matt Carlson72334482009-08-28 14:03:01 +00007522
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007523 return 0;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007524}
7525
7526/*
7527 * Must not be invoked with interrupt sources disabled and
7528 * the hardware shutdown down.
7529 */
7530static void tg3_free_consistent(struct tg3 *tp)
7531{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007532 int i;
Matt Carlson898a56f2009-08-28 14:02:40 +00007533
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007534 for (i = 0; i < tp->irq_cnt; i++) {
7535 struct tg3_napi *tnapi = &tp->napi[i];
7536
7537 if (tnapi->tx_ring) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00007538 dma_free_coherent(&tp->pdev->dev, TG3_TX_RING_BYTES,
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007539 tnapi->tx_ring, tnapi->tx_desc_mapping);
7540 tnapi->tx_ring = NULL;
7541 }
7542
7543 kfree(tnapi->tx_buffers);
7544 tnapi->tx_buffers = NULL;
7545
7546 if (tnapi->rx_rcb) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00007547 dma_free_coherent(&tp->pdev->dev,
7548 TG3_RX_RCB_RING_BYTES(tp),
7549 tnapi->rx_rcb,
7550 tnapi->rx_rcb_mapping);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007551 tnapi->rx_rcb = NULL;
7552 }
7553
Matt Carlson8fea32b2010-09-15 08:59:58 +00007554 tg3_rx_prodring_fini(tp, &tnapi->prodring);
7555
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007556 if (tnapi->hw_status) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00007557 dma_free_coherent(&tp->pdev->dev, TG3_HW_STATUS_SIZE,
7558 tnapi->hw_status,
7559 tnapi->status_mapping);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007560 tnapi->hw_status = NULL;
7561 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007562 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007563
Linus Torvalds1da177e2005-04-16 15:20:36 -07007564 if (tp->hw_stats) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00007565 dma_free_coherent(&tp->pdev->dev, sizeof(struct tg3_hw_stats),
7566 tp->hw_stats, tp->stats_mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007567 tp->hw_stats = NULL;
7568 }
7569}
7570
7571/*
7572 * Must not be invoked with interrupt sources disabled and
7573 * the hardware shutdown down. Can sleep.
7574 */
7575static int tg3_alloc_consistent(struct tg3 *tp)
7576{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007577 int i;
Matt Carlson898a56f2009-08-28 14:02:40 +00007578
Matt Carlson4bae65c2010-11-24 08:31:52 +00007579 tp->hw_stats = dma_alloc_coherent(&tp->pdev->dev,
7580 sizeof(struct tg3_hw_stats),
7581 &tp->stats_mapping,
7582 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007583 if (!tp->hw_stats)
7584 goto err_out;
7585
Linus Torvalds1da177e2005-04-16 15:20:36 -07007586 memset(tp->hw_stats, 0, sizeof(struct tg3_hw_stats));
7587
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007588 for (i = 0; i < tp->irq_cnt; i++) {
7589 struct tg3_napi *tnapi = &tp->napi[i];
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00007590 struct tg3_hw_status *sblk;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007591
Matt Carlson4bae65c2010-11-24 08:31:52 +00007592 tnapi->hw_status = dma_alloc_coherent(&tp->pdev->dev,
7593 TG3_HW_STATUS_SIZE,
7594 &tnapi->status_mapping,
7595 GFP_KERNEL);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007596 if (!tnapi->hw_status)
7597 goto err_out;
7598
7599 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00007600 sblk = tnapi->hw_status;
7601
Matt Carlson8fea32b2010-09-15 08:59:58 +00007602 if (tg3_rx_prodring_init(tp, &tnapi->prodring))
7603 goto err_out;
7604
Matt Carlson19cfaec2009-12-03 08:36:20 +00007605 /* If multivector TSS is enabled, vector 0 does not handle
7606 * tx interrupts. Don't allocate any resources for it.
7607 */
Joe Perches63c3a662011-04-26 08:12:10 +00007608 if ((!i && !tg3_flag(tp, ENABLE_TSS)) ||
7609 (i && tg3_flag(tp, ENABLE_TSS))) {
Matt Carlsondf8944c2011-07-27 14:20:46 +00007610 tnapi->tx_buffers = kzalloc(
7611 sizeof(struct tg3_tx_ring_info) *
7612 TG3_TX_RING_SIZE, GFP_KERNEL);
Matt Carlson19cfaec2009-12-03 08:36:20 +00007613 if (!tnapi->tx_buffers)
7614 goto err_out;
7615
Matt Carlson4bae65c2010-11-24 08:31:52 +00007616 tnapi->tx_ring = dma_alloc_coherent(&tp->pdev->dev,
7617 TG3_TX_RING_BYTES,
7618 &tnapi->tx_desc_mapping,
7619 GFP_KERNEL);
Matt Carlson19cfaec2009-12-03 08:36:20 +00007620 if (!tnapi->tx_ring)
7621 goto err_out;
7622 }
7623
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00007624 /*
7625 * When RSS is enabled, the status block format changes
7626 * slightly. The "rx_jumbo_consumer", "reserved",
7627 * and "rx_mini_consumer" members get mapped to the
7628 * other three rx return ring producer indexes.
7629 */
7630 switch (i) {
7631 default:
Matt Carlsonf891ea12012-04-24 13:37:01 +00007632 if (tg3_flag(tp, ENABLE_RSS)) {
7633 tnapi->rx_rcb_prod_idx = NULL;
7634 break;
7635 }
7636 /* Fall through */
7637 case 1:
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00007638 tnapi->rx_rcb_prod_idx = &sblk->idx[0].rx_producer;
7639 break;
7640 case 2:
7641 tnapi->rx_rcb_prod_idx = &sblk->rx_jumbo_consumer;
7642 break;
7643 case 3:
7644 tnapi->rx_rcb_prod_idx = &sblk->reserved;
7645 break;
7646 case 4:
7647 tnapi->rx_rcb_prod_idx = &sblk->rx_mini_consumer;
7648 break;
7649 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007650
Matt Carlson0c1d0e22009-09-01 13:16:33 +00007651 /*
7652 * If multivector RSS is enabled, vector 0 does not handle
7653 * rx or tx interrupts. Don't allocate any resources for it.
7654 */
Joe Perches63c3a662011-04-26 08:12:10 +00007655 if (!i && tg3_flag(tp, ENABLE_RSS))
Matt Carlson0c1d0e22009-09-01 13:16:33 +00007656 continue;
7657
Matt Carlson4bae65c2010-11-24 08:31:52 +00007658 tnapi->rx_rcb = dma_alloc_coherent(&tp->pdev->dev,
7659 TG3_RX_RCB_RING_BYTES(tp),
7660 &tnapi->rx_rcb_mapping,
7661 GFP_KERNEL);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007662 if (!tnapi->rx_rcb)
7663 goto err_out;
7664
7665 memset(tnapi->rx_rcb, 0, TG3_RX_RCB_RING_BYTES(tp));
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007666 }
7667
Linus Torvalds1da177e2005-04-16 15:20:36 -07007668 return 0;
7669
7670err_out:
7671 tg3_free_consistent(tp);
7672 return -ENOMEM;
7673}
7674
7675#define MAX_WAIT_CNT 1000
7676
7677/* To stop a block, clear the enable bit and poll till it
7678 * clears. tp->lock is held.
7679 */
David S. Millerb3b7d6b2005-05-05 14:40:20 -07007680static int tg3_stop_block(struct tg3 *tp, unsigned long ofs, u32 enable_bit, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007681{
7682 unsigned int i;
7683 u32 val;
7684
Joe Perches63c3a662011-04-26 08:12:10 +00007685 if (tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007686 switch (ofs) {
7687 case RCVLSC_MODE:
7688 case DMAC_MODE:
7689 case MBFREE_MODE:
7690 case BUFMGR_MODE:
7691 case MEMARB_MODE:
7692 /* We can't enable/disable these bits of the
7693 * 5705/5750, just say success.
7694 */
7695 return 0;
7696
7697 default:
7698 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07007699 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007700 }
7701
7702 val = tr32(ofs);
7703 val &= ~enable_bit;
7704 tw32_f(ofs, val);
7705
7706 for (i = 0; i < MAX_WAIT_CNT; i++) {
7707 udelay(100);
7708 val = tr32(ofs);
7709 if ((val & enable_bit) == 0)
7710 break;
7711 }
7712
David S. Millerb3b7d6b2005-05-05 14:40:20 -07007713 if (i == MAX_WAIT_CNT && !silent) {
Matt Carlson2445e462010-04-05 10:19:21 +00007714 dev_err(&tp->pdev->dev,
7715 "tg3_stop_block timed out, ofs=%lx enable_bit=%x\n",
7716 ofs, enable_bit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007717 return -ENODEV;
7718 }
7719
7720 return 0;
7721}
7722
7723/* tp->lock is held. */
David S. Millerb3b7d6b2005-05-05 14:40:20 -07007724static int tg3_abort_hw(struct tg3 *tp, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007725{
7726 int i, err;
7727
7728 tg3_disable_ints(tp);
7729
7730 tp->rx_mode &= ~RX_MODE_ENABLE;
7731 tw32_f(MAC_RX_MODE, tp->rx_mode);
7732 udelay(10);
7733
David S. Millerb3b7d6b2005-05-05 14:40:20 -07007734 err = tg3_stop_block(tp, RCVBDI_MODE, RCVBDI_MODE_ENABLE, silent);
7735 err |= tg3_stop_block(tp, RCVLPC_MODE, RCVLPC_MODE_ENABLE, silent);
7736 err |= tg3_stop_block(tp, RCVLSC_MODE, RCVLSC_MODE_ENABLE, silent);
7737 err |= tg3_stop_block(tp, RCVDBDI_MODE, RCVDBDI_MODE_ENABLE, silent);
7738 err |= tg3_stop_block(tp, RCVDCC_MODE, RCVDCC_MODE_ENABLE, silent);
7739 err |= tg3_stop_block(tp, RCVCC_MODE, RCVCC_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007740
David S. Millerb3b7d6b2005-05-05 14:40:20 -07007741 err |= tg3_stop_block(tp, SNDBDS_MODE, SNDBDS_MODE_ENABLE, silent);
7742 err |= tg3_stop_block(tp, SNDBDI_MODE, SNDBDI_MODE_ENABLE, silent);
7743 err |= tg3_stop_block(tp, SNDDATAI_MODE, SNDDATAI_MODE_ENABLE, silent);
7744 err |= tg3_stop_block(tp, RDMAC_MODE, RDMAC_MODE_ENABLE, silent);
7745 err |= tg3_stop_block(tp, SNDDATAC_MODE, SNDDATAC_MODE_ENABLE, silent);
7746 err |= tg3_stop_block(tp, DMAC_MODE, DMAC_MODE_ENABLE, silent);
7747 err |= tg3_stop_block(tp, SNDBDC_MODE, SNDBDC_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007748
7749 tp->mac_mode &= ~MAC_MODE_TDE_ENABLE;
7750 tw32_f(MAC_MODE, tp->mac_mode);
7751 udelay(40);
7752
7753 tp->tx_mode &= ~TX_MODE_ENABLE;
7754 tw32_f(MAC_TX_MODE, tp->tx_mode);
7755
7756 for (i = 0; i < MAX_WAIT_CNT; i++) {
7757 udelay(100);
7758 if (!(tr32(MAC_TX_MODE) & TX_MODE_ENABLE))
7759 break;
7760 }
7761 if (i >= MAX_WAIT_CNT) {
Matt Carlsonab96b242010-04-05 10:19:22 +00007762 dev_err(&tp->pdev->dev,
7763 "%s timed out, TX_MODE_ENABLE will not clear "
7764 "MAC_TX_MODE=%08x\n", __func__, tr32(MAC_TX_MODE));
Michael Chane6de8ad2005-05-05 14:42:41 -07007765 err |= -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007766 }
7767
Michael Chane6de8ad2005-05-05 14:42:41 -07007768 err |= tg3_stop_block(tp, HOSTCC_MODE, HOSTCC_MODE_ENABLE, silent);
David S. Millerb3b7d6b2005-05-05 14:40:20 -07007769 err |= tg3_stop_block(tp, WDMAC_MODE, WDMAC_MODE_ENABLE, silent);
7770 err |= tg3_stop_block(tp, MBFREE_MODE, MBFREE_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007771
7772 tw32(FTQ_RESET, 0xffffffff);
7773 tw32(FTQ_RESET, 0x00000000);
7774
David S. Millerb3b7d6b2005-05-05 14:40:20 -07007775 err |= tg3_stop_block(tp, BUFMGR_MODE, BUFMGR_MODE_ENABLE, silent);
7776 err |= tg3_stop_block(tp, MEMARB_MODE, MEMARB_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007777
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007778 for (i = 0; i < tp->irq_cnt; i++) {
7779 struct tg3_napi *tnapi = &tp->napi[i];
7780 if (tnapi->hw_status)
7781 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
7782 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007783
Linus Torvalds1da177e2005-04-16 15:20:36 -07007784 return err;
7785}
7786
Michael Chanee6a99b2007-07-18 21:49:10 -07007787/* Save PCI command register before chip reset */
7788static void tg3_save_pci_state(struct tg3 *tp)
7789{
Matt Carlson8a6eac92007-10-21 16:17:55 -07007790 pci_read_config_word(tp->pdev, PCI_COMMAND, &tp->pci_cmd);
Michael Chanee6a99b2007-07-18 21:49:10 -07007791}
7792
7793/* Restore PCI state after chip reset */
7794static void tg3_restore_pci_state(struct tg3 *tp)
7795{
7796 u32 val;
7797
7798 /* Re-enable indirect register accesses. */
7799 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
7800 tp->misc_host_ctrl);
7801
7802 /* Set MAX PCI retry to zero. */
7803 val = (PCISTATE_ROM_ENABLE | PCISTATE_ROM_RETRY_ENABLE);
7804 if (tp->pci_chip_rev_id == CHIPREV_ID_5704_A0 &&
Joe Perches63c3a662011-04-26 08:12:10 +00007805 tg3_flag(tp, PCIX_MODE))
Michael Chanee6a99b2007-07-18 21:49:10 -07007806 val |= PCISTATE_RETRY_SAME_DMA;
Matt Carlson0d3031d2007-10-10 18:02:43 -07007807 /* Allow reads and writes to the APE register and memory space. */
Joe Perches63c3a662011-04-26 08:12:10 +00007808 if (tg3_flag(tp, ENABLE_APE))
Matt Carlson0d3031d2007-10-10 18:02:43 -07007809 val |= PCISTATE_ALLOW_APE_CTLSPC_WR |
Matt Carlsonf92d9dc12010-06-05 17:24:30 +00007810 PCISTATE_ALLOW_APE_SHMEM_WR |
7811 PCISTATE_ALLOW_APE_PSPACE_WR;
Michael Chanee6a99b2007-07-18 21:49:10 -07007812 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE, val);
7813
Matt Carlson8a6eac92007-10-21 16:17:55 -07007814 pci_write_config_word(tp->pdev, PCI_COMMAND, tp->pci_cmd);
Michael Chanee6a99b2007-07-18 21:49:10 -07007815
Matt Carlson2c55a3d2011-11-28 09:41:04 +00007816 if (!tg3_flag(tp, PCI_EXPRESS)) {
7817 pci_write_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE,
7818 tp->pci_cacheline_sz);
7819 pci_write_config_byte(tp->pdev, PCI_LATENCY_TIMER,
7820 tp->pci_lat_timer);
Michael Chan114342f2007-10-15 02:12:26 -07007821 }
Matt Carlson5f5c51e2007-11-12 21:19:37 -08007822
Michael Chanee6a99b2007-07-18 21:49:10 -07007823 /* Make sure PCI-X relaxed ordering bit is clear. */
Joe Perches63c3a662011-04-26 08:12:10 +00007824 if (tg3_flag(tp, PCIX_MODE)) {
Matt Carlson9974a352007-10-07 23:27:28 -07007825 u16 pcix_cmd;
7826
7827 pci_read_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
7828 &pcix_cmd);
7829 pcix_cmd &= ~PCI_X_CMD_ERO;
7830 pci_write_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
7831 pcix_cmd);
7832 }
Michael Chanee6a99b2007-07-18 21:49:10 -07007833
Joe Perches63c3a662011-04-26 08:12:10 +00007834 if (tg3_flag(tp, 5780_CLASS)) {
Michael Chanee6a99b2007-07-18 21:49:10 -07007835
7836 /* Chip reset on 5780 will reset MSI enable bit,
7837 * so need to restore it.
7838 */
Joe Perches63c3a662011-04-26 08:12:10 +00007839 if (tg3_flag(tp, USING_MSI)) {
Michael Chanee6a99b2007-07-18 21:49:10 -07007840 u16 ctrl;
7841
7842 pci_read_config_word(tp->pdev,
7843 tp->msi_cap + PCI_MSI_FLAGS,
7844 &ctrl);
7845 pci_write_config_word(tp->pdev,
7846 tp->msi_cap + PCI_MSI_FLAGS,
7847 ctrl | PCI_MSI_FLAGS_ENABLE);
7848 val = tr32(MSGINT_MODE);
7849 tw32(MSGINT_MODE, val | MSGINT_MODE_ENABLE);
7850 }
7851 }
7852}
7853
Linus Torvalds1da177e2005-04-16 15:20:36 -07007854/* tp->lock is held. */
7855static int tg3_chip_reset(struct tg3 *tp)
7856{
7857 u32 val;
Michael Chan1ee582d2005-08-09 20:16:46 -07007858 void (*write_op)(struct tg3 *, u32, u32);
Matt Carlson4f125f42009-09-01 12:55:02 +00007859 int i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007860
David S. Millerf49639e2006-06-09 11:58:36 -07007861 tg3_nvram_lock(tp);
7862
Matt Carlson77b483f2008-08-15 14:07:24 -07007863 tg3_ape_lock(tp, TG3_APE_LOCK_GRC);
7864
David S. Millerf49639e2006-06-09 11:58:36 -07007865 /* No matching tg3_nvram_unlock() after this because
7866 * chip reset below will undo the nvram lock.
7867 */
7868 tp->nvram_lock_cnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007869
Michael Chanee6a99b2007-07-18 21:49:10 -07007870 /* GRC_MISC_CFG core clock reset will clear the memory
7871 * enable bit in PCI register 4 and the MSI enable bit
7872 * on some chips, so we save relevant registers here.
7873 */
7874 tg3_save_pci_state(tp);
7875
Michael Chand9ab5ad12006-03-20 22:27:35 -08007876 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752 ||
Joe Perches63c3a662011-04-26 08:12:10 +00007877 tg3_flag(tp, 5755_PLUS))
Michael Chand9ab5ad12006-03-20 22:27:35 -08007878 tw32(GRC_FASTBOOT_PC, 0);
7879
Linus Torvalds1da177e2005-04-16 15:20:36 -07007880 /*
7881 * We must avoid the readl() that normally takes place.
7882 * It locks machines, causes machine checks, and other
7883 * fun things. So, temporarily disable the 5701
7884 * hardware workaround, while we do the reset.
7885 */
Michael Chan1ee582d2005-08-09 20:16:46 -07007886 write_op = tp->write32;
7887 if (write_op == tg3_write_flush_reg32)
7888 tp->write32 = tg3_write32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007889
Michael Chand18edcb2007-03-24 20:57:11 -07007890 /* Prevent the irq handler from reading or writing PCI registers
7891 * during chip reset when the memory enable bit in the PCI command
7892 * register may be cleared. The chip does not generate interrupt
7893 * at this time, but the irq handler may still be called due to irq
7894 * sharing or irqpoll.
7895 */
Joe Perches63c3a662011-04-26 08:12:10 +00007896 tg3_flag_set(tp, CHIP_RESETTING);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007897 for (i = 0; i < tp->irq_cnt; i++) {
7898 struct tg3_napi *tnapi = &tp->napi[i];
7899 if (tnapi->hw_status) {
7900 tnapi->hw_status->status = 0;
7901 tnapi->hw_status->status_tag = 0;
7902 }
7903 tnapi->last_tag = 0;
7904 tnapi->last_irq_tag = 0;
Michael Chanb8fa2f32007-04-06 17:35:37 -07007905 }
Michael Chand18edcb2007-03-24 20:57:11 -07007906 smp_mb();
Matt Carlson4f125f42009-09-01 12:55:02 +00007907
7908 for (i = 0; i < tp->irq_cnt; i++)
7909 synchronize_irq(tp->napi[i].irq_vec);
Michael Chand18edcb2007-03-24 20:57:11 -07007910
Matt Carlson255ca312009-08-25 10:07:27 +00007911 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780) {
7912 val = tr32(TG3_PCIE_LNKCTL) & ~TG3_PCIE_LNKCTL_L1_PLL_PD_EN;
7913 tw32(TG3_PCIE_LNKCTL, val | TG3_PCIE_LNKCTL_L1_PLL_PD_DIS);
7914 }
7915
Linus Torvalds1da177e2005-04-16 15:20:36 -07007916 /* do the reset */
7917 val = GRC_MISC_CFG_CORECLK_RESET;
7918
Joe Perches63c3a662011-04-26 08:12:10 +00007919 if (tg3_flag(tp, PCI_EXPRESS)) {
Matt Carlson88075d92010-08-02 11:25:58 +00007920 /* Force PCIe 1.0a mode */
7921 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
Joe Perches63c3a662011-04-26 08:12:10 +00007922 !tg3_flag(tp, 57765_PLUS) &&
Matt Carlson88075d92010-08-02 11:25:58 +00007923 tr32(TG3_PCIE_PHY_TSTCTL) ==
7924 (TG3_PCIE_PHY_TSTCTL_PCIE10 | TG3_PCIE_PHY_TSTCTL_PSCRAM))
7925 tw32(TG3_PCIE_PHY_TSTCTL, TG3_PCIE_PHY_TSTCTL_PSCRAM);
7926
Linus Torvalds1da177e2005-04-16 15:20:36 -07007927 if (tp->pci_chip_rev_id != CHIPREV_ID_5750_A0) {
7928 tw32(GRC_MISC_CFG, (1 << 29));
7929 val |= (1 << 29);
7930 }
7931 }
7932
Michael Chanb5d37722006-09-27 16:06:21 -07007933 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
7934 tw32(VCPU_STATUS, tr32(VCPU_STATUS) | VCPU_STATUS_DRV_RESET);
7935 tw32(GRC_VCPU_EXT_CTRL,
7936 tr32(GRC_VCPU_EXT_CTRL) & ~GRC_VCPU_EXT_CTRL_HALT_CPU);
7937 }
7938
Matt Carlsonf37500d2010-08-02 11:25:59 +00007939 /* Manage gphy power for all CPMU absent PCIe devices. */
Joe Perches63c3a662011-04-26 08:12:10 +00007940 if (tg3_flag(tp, 5705_PLUS) && !tg3_flag(tp, CPMU_PRESENT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007941 val |= GRC_MISC_CFG_KEEP_GPHY_POWER;
Matt Carlsonf37500d2010-08-02 11:25:59 +00007942
Linus Torvalds1da177e2005-04-16 15:20:36 -07007943 tw32(GRC_MISC_CFG, val);
7944
Michael Chan1ee582d2005-08-09 20:16:46 -07007945 /* restore 5701 hardware bug workaround write method */
7946 tp->write32 = write_op;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007947
7948 /* Unfortunately, we have to delay before the PCI read back.
7949 * Some 575X chips even will not respond to a PCI cfg access
7950 * when the reset command is given to the chip.
7951 *
7952 * How do these hardware designers expect things to work
7953 * properly if the PCI write is posted for a long period
7954 * of time? It is always necessary to have some method by
7955 * which a register read back can occur to push the write
7956 * out which does the reset.
7957 *
7958 * For most tg3 variants the trick below was working.
7959 * Ho hum...
7960 */
7961 udelay(120);
7962
7963 /* Flush PCI posted writes. The normal MMIO registers
7964 * are inaccessible at this time so this is the only
7965 * way to make this reliably (actually, this is no longer
7966 * the case, see above). I tried to use indirect
7967 * register read/write but this upset some 5701 variants.
7968 */
7969 pci_read_config_dword(tp->pdev, PCI_COMMAND, &val);
7970
7971 udelay(120);
7972
Jon Mason708ebb3a2011-06-27 12:56:50 +00007973 if (tg3_flag(tp, PCI_EXPRESS) && pci_pcie_cap(tp->pdev)) {
Matt Carlsone7126992009-08-25 10:08:16 +00007974 u16 val16;
7975
Linus Torvalds1da177e2005-04-16 15:20:36 -07007976 if (tp->pci_chip_rev_id == CHIPREV_ID_5750_A0) {
7977 int i;
7978 u32 cfg_val;
7979
7980 /* Wait for link training to complete. */
7981 for (i = 0; i < 5000; i++)
7982 udelay(100);
7983
7984 pci_read_config_dword(tp->pdev, 0xc4, &cfg_val);
7985 pci_write_config_dword(tp->pdev, 0xc4,
7986 cfg_val | (1 << 15));
7987 }
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007988
Matt Carlsone7126992009-08-25 10:08:16 +00007989 /* Clear the "no snoop" and "relaxed ordering" bits. */
7990 pci_read_config_word(tp->pdev,
Jon Mason708ebb3a2011-06-27 12:56:50 +00007991 pci_pcie_cap(tp->pdev) + PCI_EXP_DEVCTL,
Matt Carlsone7126992009-08-25 10:08:16 +00007992 &val16);
7993 val16 &= ~(PCI_EXP_DEVCTL_RELAX_EN |
7994 PCI_EXP_DEVCTL_NOSNOOP_EN);
7995 /*
7996 * Older PCIe devices only support the 128 byte
7997 * MPS setting. Enforce the restriction.
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007998 */
Joe Perches63c3a662011-04-26 08:12:10 +00007999 if (!tg3_flag(tp, CPMU_PRESENT))
Matt Carlsone7126992009-08-25 10:08:16 +00008000 val16 &= ~PCI_EXP_DEVCTL_PAYLOAD;
Matt Carlson5e7dfd02008-11-21 17:18:16 -08008001 pci_write_config_word(tp->pdev,
Jon Mason708ebb3a2011-06-27 12:56:50 +00008002 pci_pcie_cap(tp->pdev) + PCI_EXP_DEVCTL,
Matt Carlsone7126992009-08-25 10:08:16 +00008003 val16);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08008004
Matt Carlson5e7dfd02008-11-21 17:18:16 -08008005 /* Clear error status */
8006 pci_write_config_word(tp->pdev,
Jon Mason708ebb3a2011-06-27 12:56:50 +00008007 pci_pcie_cap(tp->pdev) + PCI_EXP_DEVSTA,
Matt Carlson5e7dfd02008-11-21 17:18:16 -08008008 PCI_EXP_DEVSTA_CED |
8009 PCI_EXP_DEVSTA_NFED |
8010 PCI_EXP_DEVSTA_FED |
8011 PCI_EXP_DEVSTA_URD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008012 }
8013
Michael Chanee6a99b2007-07-18 21:49:10 -07008014 tg3_restore_pci_state(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008015
Joe Perches63c3a662011-04-26 08:12:10 +00008016 tg3_flag_clear(tp, CHIP_RESETTING);
8017 tg3_flag_clear(tp, ERROR_PROCESSED);
Michael Chand18edcb2007-03-24 20:57:11 -07008018
Michael Chanee6a99b2007-07-18 21:49:10 -07008019 val = 0;
Joe Perches63c3a662011-04-26 08:12:10 +00008020 if (tg3_flag(tp, 5780_CLASS))
Michael Chan4cf78e42005-07-25 12:29:19 -07008021 val = tr32(MEMARB_MODE);
Michael Chanee6a99b2007-07-18 21:49:10 -07008022 tw32(MEMARB_MODE, val | MEMARB_MODE_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008023
8024 if (tp->pci_chip_rev_id == CHIPREV_ID_5750_A3) {
8025 tg3_stop_fw(tp);
8026 tw32(0x5000, 0x400);
8027 }
8028
8029 tw32(GRC_MODE, tp->grc_mode);
8030
8031 if (tp->pci_chip_rev_id == CHIPREV_ID_5705_A0) {
Andy Gospodarekab0049b2007-09-06 20:42:14 +01008032 val = tr32(0xc4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008033
8034 tw32(0xc4, val | (1 << 15));
8035 }
8036
8037 if ((tp->nic_sram_data_cfg & NIC_SRAM_DATA_CFG_MINI_PCI) != 0 &&
8038 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
8039 tp->pci_clock_ctrl |= CLOCK_CTRL_CLKRUN_OENABLE;
8040 if (tp->pci_chip_rev_id == CHIPREV_ID_5705_A0)
8041 tp->pci_clock_ctrl |= CLOCK_CTRL_FORCE_CLKRUN;
8042 tw32(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl);
8043 }
8044
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008045 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Matt Carlson9e975cc2011-07-20 10:20:50 +00008046 tp->mac_mode = MAC_MODE_PORT_MODE_TBI;
Matt Carlsond2394e6b2010-11-24 08:31:47 +00008047 val = tp->mac_mode;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008048 } else if (tp->phy_flags & TG3_PHYFLG_MII_SERDES) {
Matt Carlson9e975cc2011-07-20 10:20:50 +00008049 tp->mac_mode = MAC_MODE_PORT_MODE_GMII;
Matt Carlsond2394e6b2010-11-24 08:31:47 +00008050 val = tp->mac_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008051 } else
Matt Carlsond2394e6b2010-11-24 08:31:47 +00008052 val = 0;
8053
8054 tw32_f(MAC_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008055 udelay(40);
8056
Matt Carlson77b483f2008-08-15 14:07:24 -07008057 tg3_ape_unlock(tp, TG3_APE_LOCK_GRC);
8058
Michael Chan7a6f4362006-09-27 16:03:31 -07008059 err = tg3_poll_fw(tp);
8060 if (err)
8061 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008062
Matt Carlson0a9140c2009-08-28 12:27:50 +00008063 tg3_mdio_start(tp);
8064
Joe Perches63c3a662011-04-26 08:12:10 +00008065 if (tg3_flag(tp, PCI_EXPRESS) &&
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00008066 tp->pci_chip_rev_id != CHIPREV_ID_5750_A0 &&
8067 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
Joe Perches63c3a662011-04-26 08:12:10 +00008068 !tg3_flag(tp, 57765_PLUS)) {
Andy Gospodarekab0049b2007-09-06 20:42:14 +01008069 val = tr32(0x7c00);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008070
8071 tw32(0x7c00, val | (1 << 25));
8072 }
8073
Matt Carlsond78b59f2011-04-05 14:22:46 +00008074 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
8075 val = tr32(TG3_CPMU_CLCK_ORIDE);
8076 tw32(TG3_CPMU_CLCK_ORIDE, val & ~CPMU_CLCK_ORIDE_MAC_ORIDE_EN);
8077 }
8078
Linus Torvalds1da177e2005-04-16 15:20:36 -07008079 /* Reprobe ASF enable state. */
Joe Perches63c3a662011-04-26 08:12:10 +00008080 tg3_flag_clear(tp, ENABLE_ASF);
8081 tg3_flag_clear(tp, ASF_NEW_HANDSHAKE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008082 tg3_read_mem(tp, NIC_SRAM_DATA_SIG, &val);
8083 if (val == NIC_SRAM_DATA_SIG_MAGIC) {
8084 u32 nic_cfg;
8085
8086 tg3_read_mem(tp, NIC_SRAM_DATA_CFG, &nic_cfg);
8087 if (nic_cfg & NIC_SRAM_DATA_CFG_ASF_ENABLE) {
Joe Perches63c3a662011-04-26 08:12:10 +00008088 tg3_flag_set(tp, ENABLE_ASF);
Matt Carlson4ba526c2008-08-15 14:10:04 -07008089 tp->last_event_jiffies = jiffies;
Joe Perches63c3a662011-04-26 08:12:10 +00008090 if (tg3_flag(tp, 5750_PLUS))
8091 tg3_flag_set(tp, ASF_NEW_HANDSHAKE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008092 }
8093 }
8094
8095 return 0;
8096}
8097
Matt Carlson65ec6982012-02-28 23:33:37 +00008098static void tg3_get_nstats(struct tg3 *, struct rtnl_link_stats64 *);
8099static void tg3_get_estats(struct tg3 *, struct tg3_ethtool_stats *);
Matt Carlson92feeab2011-12-08 14:40:14 +00008100
Linus Torvalds1da177e2005-04-16 15:20:36 -07008101/* tp->lock is held. */
Michael Chan944d9802005-05-29 14:57:48 -07008102static int tg3_halt(struct tg3 *tp, int kind, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008103{
8104 int err;
8105
8106 tg3_stop_fw(tp);
8107
Michael Chan944d9802005-05-29 14:57:48 -07008108 tg3_write_sig_pre_reset(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008109
David S. Millerb3b7d6b2005-05-05 14:40:20 -07008110 tg3_abort_hw(tp, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008111 err = tg3_chip_reset(tp);
8112
Matt Carlsondaba2a62009-04-20 06:58:52 +00008113 __tg3_set_mac_addr(tp, 0);
8114
Michael Chan944d9802005-05-29 14:57:48 -07008115 tg3_write_sig_legacy(tp, kind);
8116 tg3_write_sig_post_reset(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008117
Matt Carlson92feeab2011-12-08 14:40:14 +00008118 if (tp->hw_stats) {
8119 /* Save the stats across chip resets... */
David S. Millerb4017c52012-03-01 17:57:40 -05008120 tg3_get_nstats(tp, &tp->net_stats_prev);
Matt Carlson92feeab2011-12-08 14:40:14 +00008121 tg3_get_estats(tp, &tp->estats_prev);
8122
8123 /* And make sure the next sample is new data */
8124 memset(tp->hw_stats, 0, sizeof(struct tg3_hw_stats));
8125 }
8126
Linus Torvalds1da177e2005-04-16 15:20:36 -07008127 if (err)
8128 return err;
8129
8130 return 0;
8131}
8132
Linus Torvalds1da177e2005-04-16 15:20:36 -07008133static int tg3_set_mac_addr(struct net_device *dev, void *p)
8134{
8135 struct tg3 *tp = netdev_priv(dev);
8136 struct sockaddr *addr = p;
Michael Chan986e0ae2007-05-05 12:10:20 -07008137 int err = 0, skip_mac_1 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008138
Michael Chanf9804dd2005-09-27 12:13:10 -07008139 if (!is_valid_ether_addr(addr->sa_data))
Danny Kukawka504f9b52012-02-21 02:07:49 +00008140 return -EADDRNOTAVAIL;
Michael Chanf9804dd2005-09-27 12:13:10 -07008141
Linus Torvalds1da177e2005-04-16 15:20:36 -07008142 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
8143
Michael Chane75f7c92006-03-20 21:33:26 -08008144 if (!netif_running(dev))
8145 return 0;
8146
Joe Perches63c3a662011-04-26 08:12:10 +00008147 if (tg3_flag(tp, ENABLE_ASF)) {
Michael Chan986e0ae2007-05-05 12:10:20 -07008148 u32 addr0_high, addr0_low, addr1_high, addr1_low;
Michael Chan58712ef2006-04-29 18:58:01 -07008149
Michael Chan986e0ae2007-05-05 12:10:20 -07008150 addr0_high = tr32(MAC_ADDR_0_HIGH);
8151 addr0_low = tr32(MAC_ADDR_0_LOW);
8152 addr1_high = tr32(MAC_ADDR_1_HIGH);
8153 addr1_low = tr32(MAC_ADDR_1_LOW);
8154
8155 /* Skip MAC addr 1 if ASF is using it. */
8156 if ((addr0_high != addr1_high || addr0_low != addr1_low) &&
8157 !(addr1_high == 0 && addr1_low == 0))
8158 skip_mac_1 = 1;
Michael Chan58712ef2006-04-29 18:58:01 -07008159 }
Michael Chan986e0ae2007-05-05 12:10:20 -07008160 spin_lock_bh(&tp->lock);
8161 __tg3_set_mac_addr(tp, skip_mac_1);
8162 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008163
Michael Chanb9ec6c12006-07-25 16:37:27 -07008164 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008165}
8166
8167/* tp->lock is held. */
8168static void tg3_set_bdinfo(struct tg3 *tp, u32 bdinfo_addr,
8169 dma_addr_t mapping, u32 maxlen_flags,
8170 u32 nic_addr)
8171{
8172 tg3_write_mem(tp,
8173 (bdinfo_addr + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH),
8174 ((u64) mapping >> 32));
8175 tg3_write_mem(tp,
8176 (bdinfo_addr + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW),
8177 ((u64) mapping & 0xffffffff));
8178 tg3_write_mem(tp,
8179 (bdinfo_addr + TG3_BDINFO_MAXLEN_FLAGS),
8180 maxlen_flags);
8181
Joe Perches63c3a662011-04-26 08:12:10 +00008182 if (!tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008183 tg3_write_mem(tp,
8184 (bdinfo_addr + TG3_BDINFO_NIC_ADDR),
8185 nic_addr);
8186}
8187
Michael Chand244c892005-07-05 14:42:33 -07008188static void __tg3_set_coalesce(struct tg3 *tp, struct ethtool_coalesce *ec)
David S. Miller15f98502005-05-18 22:49:26 -07008189{
Matt Carlsonb6080e12009-09-01 13:12:00 +00008190 int i;
8191
Joe Perches63c3a662011-04-26 08:12:10 +00008192 if (!tg3_flag(tp, ENABLE_TSS)) {
Matt Carlsonb6080e12009-09-01 13:12:00 +00008193 tw32(HOSTCC_TXCOL_TICKS, ec->tx_coalesce_usecs);
8194 tw32(HOSTCC_TXMAX_FRAMES, ec->tx_max_coalesced_frames);
8195 tw32(HOSTCC_TXCOAL_MAXF_INT, ec->tx_max_coalesced_frames_irq);
Matt Carlsonb6080e12009-09-01 13:12:00 +00008196 } else {
8197 tw32(HOSTCC_TXCOL_TICKS, 0);
8198 tw32(HOSTCC_TXMAX_FRAMES, 0);
8199 tw32(HOSTCC_TXCOAL_MAXF_INT, 0);
Matt Carlson19cfaec2009-12-03 08:36:20 +00008200 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00008201
Joe Perches63c3a662011-04-26 08:12:10 +00008202 if (!tg3_flag(tp, ENABLE_RSS)) {
Matt Carlson19cfaec2009-12-03 08:36:20 +00008203 tw32(HOSTCC_RXCOL_TICKS, ec->rx_coalesce_usecs);
8204 tw32(HOSTCC_RXMAX_FRAMES, ec->rx_max_coalesced_frames);
8205 tw32(HOSTCC_RXCOAL_MAXF_INT, ec->rx_max_coalesced_frames_irq);
8206 } else {
Matt Carlsonb6080e12009-09-01 13:12:00 +00008207 tw32(HOSTCC_RXCOL_TICKS, 0);
8208 tw32(HOSTCC_RXMAX_FRAMES, 0);
8209 tw32(HOSTCC_RXCOAL_MAXF_INT, 0);
David S. Miller15f98502005-05-18 22:49:26 -07008210 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00008211
Joe Perches63c3a662011-04-26 08:12:10 +00008212 if (!tg3_flag(tp, 5705_PLUS)) {
David S. Miller15f98502005-05-18 22:49:26 -07008213 u32 val = ec->stats_block_coalesce_usecs;
8214
Matt Carlsonb6080e12009-09-01 13:12:00 +00008215 tw32(HOSTCC_RXCOAL_TICK_INT, ec->rx_coalesce_usecs_irq);
8216 tw32(HOSTCC_TXCOAL_TICK_INT, ec->tx_coalesce_usecs_irq);
8217
David S. Miller15f98502005-05-18 22:49:26 -07008218 if (!netif_carrier_ok(tp->dev))
8219 val = 0;
8220
8221 tw32(HOSTCC_STAT_COAL_TICKS, val);
8222 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00008223
8224 for (i = 0; i < tp->irq_cnt - 1; i++) {
8225 u32 reg;
8226
8227 reg = HOSTCC_RXCOL_TICKS_VEC1 + i * 0x18;
8228 tw32(reg, ec->rx_coalesce_usecs);
Matt Carlsonb6080e12009-09-01 13:12:00 +00008229 reg = HOSTCC_RXMAX_FRAMES_VEC1 + i * 0x18;
8230 tw32(reg, ec->rx_max_coalesced_frames);
Matt Carlsonb6080e12009-09-01 13:12:00 +00008231 reg = HOSTCC_RXCOAL_MAXF_INT_VEC1 + i * 0x18;
8232 tw32(reg, ec->rx_max_coalesced_frames_irq);
Matt Carlson19cfaec2009-12-03 08:36:20 +00008233
Joe Perches63c3a662011-04-26 08:12:10 +00008234 if (tg3_flag(tp, ENABLE_TSS)) {
Matt Carlson19cfaec2009-12-03 08:36:20 +00008235 reg = HOSTCC_TXCOL_TICKS_VEC1 + i * 0x18;
8236 tw32(reg, ec->tx_coalesce_usecs);
8237 reg = HOSTCC_TXMAX_FRAMES_VEC1 + i * 0x18;
8238 tw32(reg, ec->tx_max_coalesced_frames);
8239 reg = HOSTCC_TXCOAL_MAXF_INT_VEC1 + i * 0x18;
8240 tw32(reg, ec->tx_max_coalesced_frames_irq);
8241 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00008242 }
8243
8244 for (; i < tp->irq_max - 1; i++) {
8245 tw32(HOSTCC_RXCOL_TICKS_VEC1 + i * 0x18, 0);
Matt Carlsonb6080e12009-09-01 13:12:00 +00008246 tw32(HOSTCC_RXMAX_FRAMES_VEC1 + i * 0x18, 0);
Matt Carlsonb6080e12009-09-01 13:12:00 +00008247 tw32(HOSTCC_RXCOAL_MAXF_INT_VEC1 + i * 0x18, 0);
Matt Carlson19cfaec2009-12-03 08:36:20 +00008248
Joe Perches63c3a662011-04-26 08:12:10 +00008249 if (tg3_flag(tp, ENABLE_TSS)) {
Matt Carlson19cfaec2009-12-03 08:36:20 +00008250 tw32(HOSTCC_TXCOL_TICKS_VEC1 + i * 0x18, 0);
8251 tw32(HOSTCC_TXMAX_FRAMES_VEC1 + i * 0x18, 0);
8252 tw32(HOSTCC_TXCOAL_MAXF_INT_VEC1 + i * 0x18, 0);
8253 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00008254 }
David S. Miller15f98502005-05-18 22:49:26 -07008255}
Linus Torvalds1da177e2005-04-16 15:20:36 -07008256
8257/* tp->lock is held. */
Matt Carlson2d31eca2009-09-01 12:53:31 +00008258static void tg3_rings_reset(struct tg3 *tp)
8259{
8260 int i;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008261 u32 stblk, txrcb, rxrcb, limit;
Matt Carlson2d31eca2009-09-01 12:53:31 +00008262 struct tg3_napi *tnapi = &tp->napi[0];
8263
8264 /* Disable all transmit rings but the first. */
Joe Perches63c3a662011-04-26 08:12:10 +00008265 if (!tg3_flag(tp, 5705_PLUS))
Matt Carlson2d31eca2009-09-01 12:53:31 +00008266 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 16;
Joe Perches63c3a662011-04-26 08:12:10 +00008267 else if (tg3_flag(tp, 5717_PLUS))
Matt Carlson3d377282010-10-14 10:37:39 +00008268 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 4;
Matt Carlson55086ad2011-12-14 11:09:59 +00008269 else if (tg3_flag(tp, 57765_CLASS))
Matt Carlsonb703df62009-12-03 08:36:21 +00008270 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 2;
Matt Carlson2d31eca2009-09-01 12:53:31 +00008271 else
8272 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE;
8273
8274 for (txrcb = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE;
8275 txrcb < limit; txrcb += TG3_BDINFO_SIZE)
8276 tg3_write_mem(tp, txrcb + TG3_BDINFO_MAXLEN_FLAGS,
8277 BDINFO_FLAGS_DISABLED);
8278
8279
8280 /* Disable all receive return rings but the first. */
Joe Perches63c3a662011-04-26 08:12:10 +00008281 if (tg3_flag(tp, 5717_PLUS))
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00008282 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 17;
Joe Perches63c3a662011-04-26 08:12:10 +00008283 else if (!tg3_flag(tp, 5705_PLUS))
Matt Carlson2d31eca2009-09-01 12:53:31 +00008284 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 16;
Matt Carlsonb703df62009-12-03 08:36:21 +00008285 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
Matt Carlson55086ad2011-12-14 11:09:59 +00008286 tg3_flag(tp, 57765_CLASS))
Matt Carlson2d31eca2009-09-01 12:53:31 +00008287 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 4;
8288 else
8289 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE;
8290
8291 for (rxrcb = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE;
8292 rxrcb < limit; rxrcb += TG3_BDINFO_SIZE)
8293 tg3_write_mem(tp, rxrcb + TG3_BDINFO_MAXLEN_FLAGS,
8294 BDINFO_FLAGS_DISABLED);
8295
8296 /* Disable interrupts */
8297 tw32_mailbox_f(tp->napi[0].int_mbox, 1);
Matt Carlson0e6cf6a2011-06-13 13:38:55 +00008298 tp->napi[0].chk_msi_cnt = 0;
8299 tp->napi[0].last_rx_cons = 0;
8300 tp->napi[0].last_tx_cons = 0;
Matt Carlson2d31eca2009-09-01 12:53:31 +00008301
8302 /* Zero mailbox registers. */
Joe Perches63c3a662011-04-26 08:12:10 +00008303 if (tg3_flag(tp, SUPPORT_MSIX)) {
Matt Carlson6fd45cb2010-09-15 08:59:57 +00008304 for (i = 1; i < tp->irq_max; i++) {
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008305 tp->napi[i].tx_prod = 0;
8306 tp->napi[i].tx_cons = 0;
Joe Perches63c3a662011-04-26 08:12:10 +00008307 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlsonc2353a32010-01-20 16:58:08 +00008308 tw32_mailbox(tp->napi[i].prodmbox, 0);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008309 tw32_rx_mbox(tp->napi[i].consmbox, 0);
8310 tw32_mailbox_f(tp->napi[i].int_mbox, 1);
Matt Carlson7f230732011-08-31 11:44:48 +00008311 tp->napi[i].chk_msi_cnt = 0;
Matt Carlson0e6cf6a2011-06-13 13:38:55 +00008312 tp->napi[i].last_rx_cons = 0;
8313 tp->napi[i].last_tx_cons = 0;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008314 }
Joe Perches63c3a662011-04-26 08:12:10 +00008315 if (!tg3_flag(tp, ENABLE_TSS))
Matt Carlsonc2353a32010-01-20 16:58:08 +00008316 tw32_mailbox(tp->napi[0].prodmbox, 0);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008317 } else {
8318 tp->napi[0].tx_prod = 0;
8319 tp->napi[0].tx_cons = 0;
8320 tw32_mailbox(tp->napi[0].prodmbox, 0);
8321 tw32_rx_mbox(tp->napi[0].consmbox, 0);
8322 }
Matt Carlson2d31eca2009-09-01 12:53:31 +00008323
8324 /* Make sure the NIC-based send BD rings are disabled. */
Joe Perches63c3a662011-04-26 08:12:10 +00008325 if (!tg3_flag(tp, 5705_PLUS)) {
Matt Carlson2d31eca2009-09-01 12:53:31 +00008326 u32 mbox = MAILBOX_SNDNIC_PROD_IDX_0 + TG3_64BIT_REG_LOW;
8327 for (i = 0; i < 16; i++)
8328 tw32_tx_mbox(mbox + i * 8, 0);
8329 }
8330
8331 txrcb = NIC_SRAM_SEND_RCB;
8332 rxrcb = NIC_SRAM_RCV_RET_RCB;
8333
8334 /* Clear status block in ram. */
8335 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
8336
8337 /* Set status block DMA address */
8338 tw32(HOSTCC_STATUS_BLK_HOST_ADDR + TG3_64BIT_REG_HIGH,
8339 ((u64) tnapi->status_mapping >> 32));
8340 tw32(HOSTCC_STATUS_BLK_HOST_ADDR + TG3_64BIT_REG_LOW,
8341 ((u64) tnapi->status_mapping & 0xffffffff));
8342
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008343 if (tnapi->tx_ring) {
8344 tg3_set_bdinfo(tp, txrcb, tnapi->tx_desc_mapping,
8345 (TG3_TX_RING_SIZE <<
8346 BDINFO_FLAGS_MAXLEN_SHIFT),
8347 NIC_SRAM_TX_BUFFER_DESC);
8348 txrcb += TG3_BDINFO_SIZE;
8349 }
Matt Carlson2d31eca2009-09-01 12:53:31 +00008350
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008351 if (tnapi->rx_rcb) {
8352 tg3_set_bdinfo(tp, rxrcb, tnapi->rx_rcb_mapping,
Matt Carlson7cb32cf2010-09-30 10:34:36 +00008353 (tp->rx_ret_ring_mask + 1) <<
8354 BDINFO_FLAGS_MAXLEN_SHIFT, 0);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008355 rxrcb += TG3_BDINFO_SIZE;
8356 }
8357
8358 stblk = HOSTCC_STATBLCK_RING1;
8359
8360 for (i = 1, tnapi++; i < tp->irq_cnt; i++, tnapi++) {
8361 u64 mapping = (u64)tnapi->status_mapping;
8362 tw32(stblk + TG3_64BIT_REG_HIGH, mapping >> 32);
8363 tw32(stblk + TG3_64BIT_REG_LOW, mapping & 0xffffffff);
8364
8365 /* Clear status block in ram. */
8366 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
8367
Matt Carlson19cfaec2009-12-03 08:36:20 +00008368 if (tnapi->tx_ring) {
8369 tg3_set_bdinfo(tp, txrcb, tnapi->tx_desc_mapping,
8370 (TG3_TX_RING_SIZE <<
8371 BDINFO_FLAGS_MAXLEN_SHIFT),
8372 NIC_SRAM_TX_BUFFER_DESC);
8373 txrcb += TG3_BDINFO_SIZE;
8374 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008375
8376 tg3_set_bdinfo(tp, rxrcb, tnapi->rx_rcb_mapping,
Matt Carlson7cb32cf2010-09-30 10:34:36 +00008377 ((tp->rx_ret_ring_mask + 1) <<
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008378 BDINFO_FLAGS_MAXLEN_SHIFT), 0);
8379
8380 stblk += 8;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008381 rxrcb += TG3_BDINFO_SIZE;
8382 }
Matt Carlson2d31eca2009-09-01 12:53:31 +00008383}
8384
Matt Carlsoneb07a942011-04-20 07:57:36 +00008385static void tg3_setup_rxbd_thresholds(struct tg3 *tp)
8386{
8387 u32 val, bdcache_maxcnt, host_rep_thresh, nic_rep_thresh;
8388
Joe Perches63c3a662011-04-26 08:12:10 +00008389 if (!tg3_flag(tp, 5750_PLUS) ||
8390 tg3_flag(tp, 5780_CLASS) ||
Matt Carlsoneb07a942011-04-20 07:57:36 +00008391 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 ||
Matt Carlson513aa6e2011-11-21 15:01:18 +00008392 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752 ||
8393 tg3_flag(tp, 57765_PLUS))
Matt Carlsoneb07a942011-04-20 07:57:36 +00008394 bdcache_maxcnt = TG3_SRAM_RX_STD_BDCACHE_SIZE_5700;
8395 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
8396 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787)
8397 bdcache_maxcnt = TG3_SRAM_RX_STD_BDCACHE_SIZE_5755;
8398 else
8399 bdcache_maxcnt = TG3_SRAM_RX_STD_BDCACHE_SIZE_5906;
8400
8401 nic_rep_thresh = min(bdcache_maxcnt / 2, tp->rx_std_max_post);
8402 host_rep_thresh = max_t(u32, tp->rx_pending / 8, 1);
8403
8404 val = min(nic_rep_thresh, host_rep_thresh);
8405 tw32(RCVBDI_STD_THRESH, val);
8406
Joe Perches63c3a662011-04-26 08:12:10 +00008407 if (tg3_flag(tp, 57765_PLUS))
Matt Carlsoneb07a942011-04-20 07:57:36 +00008408 tw32(STD_REPLENISH_LWM, bdcache_maxcnt);
8409
Joe Perches63c3a662011-04-26 08:12:10 +00008410 if (!tg3_flag(tp, JUMBO_CAPABLE) || tg3_flag(tp, 5780_CLASS))
Matt Carlsoneb07a942011-04-20 07:57:36 +00008411 return;
8412
Matt Carlson513aa6e2011-11-21 15:01:18 +00008413 bdcache_maxcnt = TG3_SRAM_RX_JMB_BDCACHE_SIZE_5700;
Matt Carlsoneb07a942011-04-20 07:57:36 +00008414
8415 host_rep_thresh = max_t(u32, tp->rx_jumbo_pending / 8, 1);
8416
8417 val = min(bdcache_maxcnt / 2, host_rep_thresh);
8418 tw32(RCVBDI_JUMBO_THRESH, val);
8419
Joe Perches63c3a662011-04-26 08:12:10 +00008420 if (tg3_flag(tp, 57765_PLUS))
Matt Carlsoneb07a942011-04-20 07:57:36 +00008421 tw32(JMB_REPLENISH_LWM, bdcache_maxcnt);
8422}
8423
Matt Carlsonccd5ba92012-02-13 10:20:08 +00008424static inline u32 calc_crc(unsigned char *buf, int len)
8425{
8426 u32 reg;
8427 u32 tmp;
8428 int j, k;
8429
8430 reg = 0xffffffff;
8431
8432 for (j = 0; j < len; j++) {
8433 reg ^= buf[j];
8434
8435 for (k = 0; k < 8; k++) {
8436 tmp = reg & 0x01;
8437
8438 reg >>= 1;
8439
8440 if (tmp)
8441 reg ^= 0xedb88320;
8442 }
8443 }
8444
8445 return ~reg;
8446}
8447
8448static void tg3_set_multi(struct tg3 *tp, unsigned int accept_all)
8449{
8450 /* accept or reject all multicast frames */
8451 tw32(MAC_HASH_REG_0, accept_all ? 0xffffffff : 0);
8452 tw32(MAC_HASH_REG_1, accept_all ? 0xffffffff : 0);
8453 tw32(MAC_HASH_REG_2, accept_all ? 0xffffffff : 0);
8454 tw32(MAC_HASH_REG_3, accept_all ? 0xffffffff : 0);
8455}
8456
8457static void __tg3_set_rx_mode(struct net_device *dev)
8458{
8459 struct tg3 *tp = netdev_priv(dev);
8460 u32 rx_mode;
8461
8462 rx_mode = tp->rx_mode & ~(RX_MODE_PROMISC |
8463 RX_MODE_KEEP_VLAN_TAG);
8464
8465#if !defined(CONFIG_VLAN_8021Q) && !defined(CONFIG_VLAN_8021Q_MODULE)
8466 /* When ASF is in use, we always keep the RX_MODE_KEEP_VLAN_TAG
8467 * flag clear.
8468 */
8469 if (!tg3_flag(tp, ENABLE_ASF))
8470 rx_mode |= RX_MODE_KEEP_VLAN_TAG;
8471#endif
8472
8473 if (dev->flags & IFF_PROMISC) {
8474 /* Promiscuous mode. */
8475 rx_mode |= RX_MODE_PROMISC;
8476 } else if (dev->flags & IFF_ALLMULTI) {
8477 /* Accept all multicast. */
8478 tg3_set_multi(tp, 1);
8479 } else if (netdev_mc_empty(dev)) {
8480 /* Reject all multicast. */
8481 tg3_set_multi(tp, 0);
8482 } else {
8483 /* Accept one or more multicast(s). */
8484 struct netdev_hw_addr *ha;
8485 u32 mc_filter[4] = { 0, };
8486 u32 regidx;
8487 u32 bit;
8488 u32 crc;
8489
8490 netdev_for_each_mc_addr(ha, dev) {
8491 crc = calc_crc(ha->addr, ETH_ALEN);
8492 bit = ~crc & 0x7f;
8493 regidx = (bit & 0x60) >> 5;
8494 bit &= 0x1f;
8495 mc_filter[regidx] |= (1 << bit);
8496 }
8497
8498 tw32(MAC_HASH_REG_0, mc_filter[0]);
8499 tw32(MAC_HASH_REG_1, mc_filter[1]);
8500 tw32(MAC_HASH_REG_2, mc_filter[2]);
8501 tw32(MAC_HASH_REG_3, mc_filter[3]);
8502 }
8503
8504 if (rx_mode != tp->rx_mode) {
8505 tp->rx_mode = rx_mode;
8506 tw32_f(MAC_RX_MODE, rx_mode);
8507 udelay(10);
8508 }
8509}
8510
Matt Carlson90415472011-12-16 13:33:23 +00008511static void tg3_rss_init_dflt_indir_tbl(struct tg3 *tp)
8512{
8513 int i;
8514
8515 for (i = 0; i < TG3_RSS_INDIR_TBL_SIZE; i++)
8516 tp->rss_ind_tbl[i] =
8517 ethtool_rxfh_indir_default(i, tp->irq_cnt - 1);
8518}
8519
8520static void tg3_rss_check_indir_tbl(struct tg3 *tp)
Matt Carlsonbcebcc42011-12-14 11:10:01 +00008521{
8522 int i;
8523
8524 if (!tg3_flag(tp, SUPPORT_MSIX))
8525 return;
8526
Matt Carlson90415472011-12-16 13:33:23 +00008527 if (tp->irq_cnt <= 2) {
Matt Carlsonbcebcc42011-12-14 11:10:01 +00008528 memset(&tp->rss_ind_tbl[0], 0, sizeof(tp->rss_ind_tbl));
Matt Carlson90415472011-12-16 13:33:23 +00008529 return;
8530 }
8531
8532 /* Validate table against current IRQ count */
8533 for (i = 0; i < TG3_RSS_INDIR_TBL_SIZE; i++) {
8534 if (tp->rss_ind_tbl[i] >= tp->irq_cnt - 1)
8535 break;
8536 }
8537
8538 if (i != TG3_RSS_INDIR_TBL_SIZE)
8539 tg3_rss_init_dflt_indir_tbl(tp);
Matt Carlsonbcebcc42011-12-14 11:10:01 +00008540}
8541
Matt Carlson90415472011-12-16 13:33:23 +00008542static void tg3_rss_write_indir_tbl(struct tg3 *tp)
Matt Carlsonbcebcc42011-12-14 11:10:01 +00008543{
8544 int i = 0;
8545 u32 reg = MAC_RSS_INDIR_TBL_0;
8546
8547 while (i < TG3_RSS_INDIR_TBL_SIZE) {
8548 u32 val = tp->rss_ind_tbl[i];
8549 i++;
8550 for (; i % 8; i++) {
8551 val <<= 4;
8552 val |= tp->rss_ind_tbl[i];
8553 }
8554 tw32(reg, val);
8555 reg += 4;
8556 }
8557}
8558
Matt Carlson2d31eca2009-09-01 12:53:31 +00008559/* tp->lock is held. */
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07008560static int tg3_reset_hw(struct tg3 *tp, int reset_phy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008561{
8562 u32 val, rdmac_mode;
8563 int i, err, limit;
Matt Carlson8fea32b2010-09-15 08:59:58 +00008564 struct tg3_rx_prodring_set *tpr = &tp->napi[0].prodring;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008565
8566 tg3_disable_ints(tp);
8567
8568 tg3_stop_fw(tp);
8569
8570 tg3_write_sig_pre_reset(tp, RESET_KIND_INIT);
8571
Joe Perches63c3a662011-04-26 08:12:10 +00008572 if (tg3_flag(tp, INIT_COMPLETE))
Michael Chane6de8ad2005-05-05 14:42:41 -07008573 tg3_abort_hw(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008574
Matt Carlson699c0192010-12-06 08:28:51 +00008575 /* Enable MAC control of LPI */
8576 if (tp->phy_flags & TG3_PHYFLG_EEE_CAP) {
8577 tw32_f(TG3_CPMU_EEE_LNKIDL_CTRL,
8578 TG3_CPMU_EEE_LNKIDL_PCIE_NL0 |
8579 TG3_CPMU_EEE_LNKIDL_UART_IDL);
8580
8581 tw32_f(TG3_CPMU_EEE_CTRL,
8582 TG3_CPMU_EEE_CTRL_EXIT_20_1_US);
8583
Matt Carlsona386b902010-12-06 08:28:53 +00008584 val = TG3_CPMU_EEEMD_ERLY_L1_XIT_DET |
8585 TG3_CPMU_EEEMD_LPI_IN_TX |
8586 TG3_CPMU_EEEMD_LPI_IN_RX |
8587 TG3_CPMU_EEEMD_EEE_ENABLE;
8588
8589 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717)
8590 val |= TG3_CPMU_EEEMD_SND_IDX_DET_EN;
8591
Joe Perches63c3a662011-04-26 08:12:10 +00008592 if (tg3_flag(tp, ENABLE_APE))
Matt Carlsona386b902010-12-06 08:28:53 +00008593 val |= TG3_CPMU_EEEMD_APE_TX_DET_EN;
8594
8595 tw32_f(TG3_CPMU_EEE_MODE, val);
8596
8597 tw32_f(TG3_CPMU_EEE_DBTMR1,
8598 TG3_CPMU_DBTMR1_PCIEXIT_2047US |
8599 TG3_CPMU_DBTMR1_LNKIDLE_2047US);
8600
8601 tw32_f(TG3_CPMU_EEE_DBTMR2,
Matt Carlsond7f2ab22011-01-25 15:58:56 +00008602 TG3_CPMU_DBTMR2_APE_TX_2047US |
Matt Carlsona386b902010-12-06 08:28:53 +00008603 TG3_CPMU_DBTMR2_TXIDXEQ_2047US);
Matt Carlson699c0192010-12-06 08:28:51 +00008604 }
8605
Matt Carlson603f1172010-02-12 14:47:10 +00008606 if (reset_phy)
Michael Chand4d2c552006-03-20 17:47:20 -08008607 tg3_phy_reset(tp);
8608
Linus Torvalds1da177e2005-04-16 15:20:36 -07008609 err = tg3_chip_reset(tp);
8610 if (err)
8611 return err;
8612
8613 tg3_write_sig_legacy(tp, RESET_KIND_INIT);
8614
Matt Carlsonbcb37f62008-11-03 16:52:09 -08008615 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX) {
Matt Carlsond30cdd22007-10-07 23:28:35 -07008616 val = tr32(TG3_CPMU_CTRL);
8617 val &= ~(CPMU_CTRL_LINK_AWARE_MODE | CPMU_CTRL_LINK_IDLE_MODE);
8618 tw32(TG3_CPMU_CTRL, val);
Matt Carlson9acb9612007-11-12 21:10:06 -08008619
8620 val = tr32(TG3_CPMU_LSPD_10MB_CLK);
8621 val &= ~CPMU_LSPD_10MB_MACCLK_MASK;
8622 val |= CPMU_LSPD_10MB_MACCLK_6_25;
8623 tw32(TG3_CPMU_LSPD_10MB_CLK, val);
8624
8625 val = tr32(TG3_CPMU_LNK_AWARE_PWRMD);
8626 val &= ~CPMU_LNK_AWARE_MACCLK_MASK;
8627 val |= CPMU_LNK_AWARE_MACCLK_6_25;
8628 tw32(TG3_CPMU_LNK_AWARE_PWRMD, val);
8629
8630 val = tr32(TG3_CPMU_HST_ACC);
8631 val &= ~CPMU_HST_ACC_MACCLK_MASK;
8632 val |= CPMU_HST_ACC_MACCLK_6_25;
8633 tw32(TG3_CPMU_HST_ACC, val);
Matt Carlsond30cdd22007-10-07 23:28:35 -07008634 }
8635
Matt Carlson33466d92009-04-20 06:57:41 +00008636 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780) {
8637 val = tr32(PCIE_PWR_MGMT_THRESH) & ~PCIE_PWR_MGMT_L1_THRESH_MSK;
8638 val |= PCIE_PWR_MGMT_EXT_ASPM_TMR_EN |
8639 PCIE_PWR_MGMT_L1_THRESH_4MS;
8640 tw32(PCIE_PWR_MGMT_THRESH, val);
Matt Carlson521e6b92009-08-25 10:06:01 +00008641
8642 val = tr32(TG3_PCIE_EIDLE_DELAY) & ~TG3_PCIE_EIDLE_DELAY_MASK;
8643 tw32(TG3_PCIE_EIDLE_DELAY, val | TG3_PCIE_EIDLE_DELAY_13_CLKS);
8644
8645 tw32(TG3_CORR_ERR_STAT, TG3_CORR_ERR_STAT_CLEAR);
Matt Carlson33466d92009-04-20 06:57:41 +00008646
Matt Carlsonf40386c2009-11-02 14:24:02 +00008647 val = tr32(TG3_PCIE_LNKCTL) & ~TG3_PCIE_LNKCTL_L1_PLL_PD_EN;
8648 tw32(TG3_PCIE_LNKCTL, val | TG3_PCIE_LNKCTL_L1_PLL_PD_DIS);
Matt Carlson255ca312009-08-25 10:07:27 +00008649 }
8650
Joe Perches63c3a662011-04-26 08:12:10 +00008651 if (tg3_flag(tp, L1PLLPD_EN)) {
Matt Carlson614b0592010-01-20 16:58:02 +00008652 u32 grc_mode = tr32(GRC_MODE);
8653
8654 /* Access the lower 1K of PL PCIE block registers. */
8655 val = grc_mode & ~GRC_MODE_PCIE_PORT_MASK;
8656 tw32(GRC_MODE, val | GRC_MODE_PCIE_PL_SEL);
8657
8658 val = tr32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL1);
8659 tw32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL1,
8660 val | TG3_PCIE_PL_LO_PHYCTL1_L1PLLPD_EN);
8661
8662 tw32(GRC_MODE, grc_mode);
8663 }
8664
Matt Carlson55086ad2011-12-14 11:09:59 +00008665 if (tg3_flag(tp, 57765_CLASS)) {
Matt Carlson5093eed2010-11-24 08:31:45 +00008666 if (tp->pci_chip_rev_id == CHIPREV_ID_57765_A0) {
8667 u32 grc_mode = tr32(GRC_MODE);
Matt Carlsoncea46462010-04-12 06:58:24 +00008668
Matt Carlson5093eed2010-11-24 08:31:45 +00008669 /* Access the lower 1K of PL PCIE block registers. */
8670 val = grc_mode & ~GRC_MODE_PCIE_PORT_MASK;
8671 tw32(GRC_MODE, val | GRC_MODE_PCIE_PL_SEL);
Matt Carlsoncea46462010-04-12 06:58:24 +00008672
Matt Carlson5093eed2010-11-24 08:31:45 +00008673 val = tr32(TG3_PCIE_TLDLPL_PORT +
8674 TG3_PCIE_PL_LO_PHYCTL5);
8675 tw32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL5,
8676 val | TG3_PCIE_PL_LO_PHYCTL5_DIS_L2CLKREQ);
Matt Carlsoncea46462010-04-12 06:58:24 +00008677
Matt Carlson5093eed2010-11-24 08:31:45 +00008678 tw32(GRC_MODE, grc_mode);
8679 }
Matt Carlsona977dbe2010-04-12 06:58:26 +00008680
Matt Carlson1ff30a52011-05-19 12:12:46 +00008681 if (GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_57765_AX) {
8682 u32 grc_mode = tr32(GRC_MODE);
8683
8684 /* Access the lower 1K of DL PCIE block registers. */
8685 val = grc_mode & ~GRC_MODE_PCIE_PORT_MASK;
8686 tw32(GRC_MODE, val | GRC_MODE_PCIE_DL_SEL);
8687
8688 val = tr32(TG3_PCIE_TLDLPL_PORT +
8689 TG3_PCIE_DL_LO_FTSMAX);
8690 val &= ~TG3_PCIE_DL_LO_FTSMAX_MSK;
8691 tw32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_DL_LO_FTSMAX,
8692 val | TG3_PCIE_DL_LO_FTSMAX_VAL);
8693
8694 tw32(GRC_MODE, grc_mode);
8695 }
8696
Matt Carlsona977dbe2010-04-12 06:58:26 +00008697 val = tr32(TG3_CPMU_LSPD_10MB_CLK);
8698 val &= ~CPMU_LSPD_10MB_MACCLK_MASK;
8699 val |= CPMU_LSPD_10MB_MACCLK_6_25;
8700 tw32(TG3_CPMU_LSPD_10MB_CLK, val);
Matt Carlsoncea46462010-04-12 06:58:24 +00008701 }
8702
Linus Torvalds1da177e2005-04-16 15:20:36 -07008703 /* This works around an issue with Athlon chipsets on
8704 * B3 tigon3 silicon. This bit has no effect on any
8705 * other revision. But do not set this on PCI Express
Matt Carlson795d01c2007-10-07 23:28:17 -07008706 * chips and don't even touch the clocks if the CPMU is present.
Linus Torvalds1da177e2005-04-16 15:20:36 -07008707 */
Joe Perches63c3a662011-04-26 08:12:10 +00008708 if (!tg3_flag(tp, CPMU_PRESENT)) {
8709 if (!tg3_flag(tp, PCI_EXPRESS))
Matt Carlson795d01c2007-10-07 23:28:17 -07008710 tp->pci_clock_ctrl |= CLOCK_CTRL_DELAY_PCI_GRANT;
8711 tw32_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl);
8712 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008713
8714 if (tp->pci_chip_rev_id == CHIPREV_ID_5704_A0 &&
Joe Perches63c3a662011-04-26 08:12:10 +00008715 tg3_flag(tp, PCIX_MODE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008716 val = tr32(TG3PCI_PCISTATE);
8717 val |= PCISTATE_RETRY_SAME_DMA;
8718 tw32(TG3PCI_PCISTATE, val);
8719 }
8720
Joe Perches63c3a662011-04-26 08:12:10 +00008721 if (tg3_flag(tp, ENABLE_APE)) {
Matt Carlson0d3031d2007-10-10 18:02:43 -07008722 /* Allow reads and writes to the
8723 * APE register and memory space.
8724 */
8725 val = tr32(TG3PCI_PCISTATE);
8726 val |= PCISTATE_ALLOW_APE_CTLSPC_WR |
Matt Carlsonf92d9dc12010-06-05 17:24:30 +00008727 PCISTATE_ALLOW_APE_SHMEM_WR |
8728 PCISTATE_ALLOW_APE_PSPACE_WR;
Matt Carlson0d3031d2007-10-10 18:02:43 -07008729 tw32(TG3PCI_PCISTATE, val);
8730 }
8731
Linus Torvalds1da177e2005-04-16 15:20:36 -07008732 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5704_BX) {
8733 /* Enable some hw fixes. */
8734 val = tr32(TG3PCI_MSI_DATA);
8735 val |= (1 << 26) | (1 << 28) | (1 << 29);
8736 tw32(TG3PCI_MSI_DATA, val);
8737 }
8738
8739 /* Descriptor ring init may make accesses to the
8740 * NIC SRAM area to setup the TX descriptors, so we
8741 * can only do this after the hardware has been
8742 * successfully reset.
8743 */
Michael Chan32d8c572006-07-25 16:38:29 -07008744 err = tg3_init_rings(tp);
8745 if (err)
8746 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008747
Joe Perches63c3a662011-04-26 08:12:10 +00008748 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlsoncbf9ca62009-11-13 13:03:40 +00008749 val = tr32(TG3PCI_DMA_RW_CTRL) &
8750 ~DMA_RWCTRL_DIS_CACHE_ALIGNMENT;
Matt Carlson1a319022010-04-12 06:58:25 +00008751 if (tp->pci_chip_rev_id == CHIPREV_ID_57765_A0)
8752 val &= ~DMA_RWCTRL_CRDRDR_RDMA_MRRS_MSK;
Matt Carlson55086ad2011-12-14 11:09:59 +00008753 if (!tg3_flag(tp, 57765_CLASS) &&
Matt Carlson0aebff42011-04-25 12:42:45 +00008754 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717)
8755 val |= DMA_RWCTRL_TAGGED_STAT_WA;
Matt Carlsoncbf9ca62009-11-13 13:03:40 +00008756 tw32(TG3PCI_DMA_RW_CTRL, val | tp->dma_rwctrl);
8757 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5784 &&
8758 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5761) {
Matt Carlsond30cdd22007-10-07 23:28:35 -07008759 /* This value is determined during the probe time DMA
8760 * engine test, tg3_test_dma.
8761 */
8762 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
8763 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008764
8765 tp->grc_mode &= ~(GRC_MODE_HOST_SENDBDS |
8766 GRC_MODE_4X_NIC_SEND_RINGS |
8767 GRC_MODE_NO_TX_PHDR_CSUM |
8768 GRC_MODE_NO_RX_PHDR_CSUM);
8769 tp->grc_mode |= GRC_MODE_HOST_SENDBDS;
Michael Chand2d746f2006-04-06 21:45:39 -07008770
8771 /* Pseudo-header checksum is done by hardware logic and not
8772 * the offload processers, so make the chip do the pseudo-
8773 * header checksums on receive. For transmit it is more
8774 * convenient to do the pseudo-header checksum in software
8775 * as Linux does that on transmit for us in all cases.
8776 */
8777 tp->grc_mode |= GRC_MODE_NO_TX_PHDR_CSUM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008778
8779 tw32(GRC_MODE,
8780 tp->grc_mode |
8781 (GRC_MODE_IRQ_ON_MAC_ATTN | GRC_MODE_HOST_STACKUP));
8782
8783 /* Setup the timer prescalar register. Clock is always 66Mhz. */
8784 val = tr32(GRC_MISC_CFG);
8785 val &= ~0xff;
8786 val |= (65 << GRC_MISC_CFG_PRESCALAR_SHIFT);
8787 tw32(GRC_MISC_CFG, val);
8788
8789 /* Initialize MBUF/DESC pool. */
Joe Perches63c3a662011-04-26 08:12:10 +00008790 if (tg3_flag(tp, 5750_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008791 /* Do nothing. */
8792 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5705) {
8793 tw32(BUFMGR_MB_POOL_ADDR, NIC_SRAM_MBUF_POOL_BASE);
8794 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704)
8795 tw32(BUFMGR_MB_POOL_SIZE, NIC_SRAM_MBUF_POOL_SIZE64);
8796 else
8797 tw32(BUFMGR_MB_POOL_SIZE, NIC_SRAM_MBUF_POOL_SIZE96);
8798 tw32(BUFMGR_DMA_DESC_POOL_ADDR, NIC_SRAM_DMA_DESC_POOL_BASE);
8799 tw32(BUFMGR_DMA_DESC_POOL_SIZE, NIC_SRAM_DMA_DESC_POOL_SIZE);
Joe Perches63c3a662011-04-26 08:12:10 +00008800 } else if (tg3_flag(tp, TSO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008801 int fw_len;
8802
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08008803 fw_len = tp->fw_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008804 fw_len = (fw_len + (0x80 - 1)) & ~(0x80 - 1);
8805 tw32(BUFMGR_MB_POOL_ADDR,
8806 NIC_SRAM_MBUF_POOL_BASE5705 + fw_len);
8807 tw32(BUFMGR_MB_POOL_SIZE,
8808 NIC_SRAM_MBUF_POOL_SIZE5705 - fw_len - 0xa00);
8809 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008810
Michael Chan0f893dc2005-07-25 12:30:38 -07008811 if (tp->dev->mtu <= ETH_DATA_LEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008812 tw32(BUFMGR_MB_RDMA_LOW_WATER,
8813 tp->bufmgr_config.mbuf_read_dma_low_water);
8814 tw32(BUFMGR_MB_MACRX_LOW_WATER,
8815 tp->bufmgr_config.mbuf_mac_rx_low_water);
8816 tw32(BUFMGR_MB_HIGH_WATER,
8817 tp->bufmgr_config.mbuf_high_water);
8818 } else {
8819 tw32(BUFMGR_MB_RDMA_LOW_WATER,
8820 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo);
8821 tw32(BUFMGR_MB_MACRX_LOW_WATER,
8822 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo);
8823 tw32(BUFMGR_MB_HIGH_WATER,
8824 tp->bufmgr_config.mbuf_high_water_jumbo);
8825 }
8826 tw32(BUFMGR_DMA_LOW_WATER,
8827 tp->bufmgr_config.dma_low_water);
8828 tw32(BUFMGR_DMA_HIGH_WATER,
8829 tp->bufmgr_config.dma_high_water);
8830
Matt Carlsond309a462010-09-30 10:34:31 +00008831 val = BUFMGR_MODE_ENABLE | BUFMGR_MODE_ATTN_ENABLE;
8832 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
8833 val |= BUFMGR_MODE_NO_TX_UNDERRUN;
Matt Carlson4d958472011-04-20 07:57:35 +00008834 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
8835 tp->pci_chip_rev_id == CHIPREV_ID_5719_A0 ||
8836 tp->pci_chip_rev_id == CHIPREV_ID_5720_A0)
8837 val |= BUFMGR_MODE_MBLOW_ATTN_ENAB;
Matt Carlsond309a462010-09-30 10:34:31 +00008838 tw32(BUFMGR_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008839 for (i = 0; i < 2000; i++) {
8840 if (tr32(BUFMGR_MODE) & BUFMGR_MODE_ENABLE)
8841 break;
8842 udelay(10);
8843 }
8844 if (i >= 2000) {
Joe Perches05dbe002010-02-17 19:44:19 +00008845 netdev_err(tp->dev, "%s cannot enable BUFMGR\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008846 return -ENODEV;
8847 }
8848
Matt Carlsoneb07a942011-04-20 07:57:36 +00008849 if (tp->pci_chip_rev_id == CHIPREV_ID_5906_A1)
8850 tw32(ISO_PKT_TX, (tr32(ISO_PKT_TX) & ~0x3) | 0x2);
Michael Chanb5d37722006-09-27 16:06:21 -07008851
Matt Carlsoneb07a942011-04-20 07:57:36 +00008852 tg3_setup_rxbd_thresholds(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008853
8854 /* Initialize TG3_BDINFO's at:
8855 * RCVDBDI_STD_BD: standard eth size rx ring
8856 * RCVDBDI_JUMBO_BD: jumbo frame rx ring
8857 * RCVDBDI_MINI_BD: small frame rx ring (??? does not work)
8858 *
8859 * like so:
8860 * TG3_BDINFO_HOST_ADDR: high/low parts of DMA address of ring
8861 * TG3_BDINFO_MAXLEN_FLAGS: (rx max buffer size << 16) |
8862 * ring attribute flags
8863 * TG3_BDINFO_NIC_ADDR: location of descriptors in nic SRAM
8864 *
8865 * Standard receive ring @ NIC_SRAM_RX_BUFFER_DESC, 512 entries.
8866 * Jumbo receive ring @ NIC_SRAM_RX_JUMBO_BUFFER_DESC, 256 entries.
8867 *
8868 * The size of each ring is fixed in the firmware, but the location is
8869 * configurable.
8870 */
8871 tw32(RCVDBDI_STD_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH,
Matt Carlson21f581a2009-08-28 14:00:25 +00008872 ((u64) tpr->rx_std_mapping >> 32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07008873 tw32(RCVDBDI_STD_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW,
Matt Carlson21f581a2009-08-28 14:00:25 +00008874 ((u64) tpr->rx_std_mapping & 0xffffffff));
Joe Perches63c3a662011-04-26 08:12:10 +00008875 if (!tg3_flag(tp, 5717_PLUS))
Matt Carlson87668d32009-11-13 13:03:34 +00008876 tw32(RCVDBDI_STD_BD + TG3_BDINFO_NIC_ADDR,
8877 NIC_SRAM_RX_BUFFER_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008878
Matt Carlsonfdb72b32009-08-28 13:57:12 +00008879 /* Disable the mini ring */
Joe Perches63c3a662011-04-26 08:12:10 +00008880 if (!tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008881 tw32(RCVDBDI_MINI_BD + TG3_BDINFO_MAXLEN_FLAGS,
8882 BDINFO_FLAGS_DISABLED);
8883
Matt Carlsonfdb72b32009-08-28 13:57:12 +00008884 /* Program the jumbo buffer descriptor ring control
8885 * blocks on those devices that have them.
8886 */
Matt Carlsona0512942011-07-27 14:20:54 +00008887 if (tp->pci_chip_rev_id == CHIPREV_ID_5719_A0 ||
Joe Perches63c3a662011-04-26 08:12:10 +00008888 (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008889
Joe Perches63c3a662011-04-26 08:12:10 +00008890 if (tg3_flag(tp, JUMBO_RING_ENABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008891 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH,
Matt Carlson21f581a2009-08-28 14:00:25 +00008892 ((u64) tpr->rx_jmb_mapping >> 32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07008893 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW,
Matt Carlson21f581a2009-08-28 14:00:25 +00008894 ((u64) tpr->rx_jmb_mapping & 0xffffffff));
Matt Carlsonde9f5232011-04-05 14:22:43 +00008895 val = TG3_RX_JMB_RING_SIZE(tp) <<
8896 BDINFO_FLAGS_MAXLEN_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008897 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_MAXLEN_FLAGS,
Matt Carlsonde9f5232011-04-05 14:22:43 +00008898 val | BDINFO_FLAGS_USE_EXT_RECV);
Joe Perches63c3a662011-04-26 08:12:10 +00008899 if (!tg3_flag(tp, USE_JUMBO_BDFLAG) ||
Matt Carlson55086ad2011-12-14 11:09:59 +00008900 tg3_flag(tp, 57765_CLASS))
Matt Carlson87668d32009-11-13 13:03:34 +00008901 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_NIC_ADDR,
8902 NIC_SRAM_RX_JUMBO_BUFFER_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008903 } else {
8904 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_MAXLEN_FLAGS,
8905 BDINFO_FLAGS_DISABLED);
8906 }
8907
Joe Perches63c3a662011-04-26 08:12:10 +00008908 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlsonfa6b2aa2011-11-21 15:01:19 +00008909 val = TG3_RX_STD_RING_SIZE(tp);
Matt Carlson7cb32cf2010-09-30 10:34:36 +00008910 val <<= BDINFO_FLAGS_MAXLEN_SHIFT;
8911 val |= (TG3_RX_STD_DMA_SZ << 2);
8912 } else
Matt Carlson04380d42010-04-12 06:58:29 +00008913 val = TG3_RX_STD_DMA_SZ << BDINFO_FLAGS_MAXLEN_SHIFT;
Matt Carlsonfdb72b32009-08-28 13:57:12 +00008914 } else
Matt Carlsonde9f5232011-04-05 14:22:43 +00008915 val = TG3_RX_STD_MAX_SIZE_5700 << BDINFO_FLAGS_MAXLEN_SHIFT;
Matt Carlsonfdb72b32009-08-28 13:57:12 +00008916
8917 tw32(RCVDBDI_STD_BD + TG3_BDINFO_MAXLEN_FLAGS, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008918
Matt Carlson411da642009-11-13 13:03:46 +00008919 tpr->rx_std_prod_idx = tp->rx_pending;
Matt Carlson66711e62009-11-13 13:03:49 +00008920 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG, tpr->rx_std_prod_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008921
Joe Perches63c3a662011-04-26 08:12:10 +00008922 tpr->rx_jmb_prod_idx =
8923 tg3_flag(tp, JUMBO_RING_ENABLE) ? tp->rx_jumbo_pending : 0;
Matt Carlson66711e62009-11-13 13:03:49 +00008924 tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG, tpr->rx_jmb_prod_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008925
Matt Carlson2d31eca2009-09-01 12:53:31 +00008926 tg3_rings_reset(tp);
8927
Linus Torvalds1da177e2005-04-16 15:20:36 -07008928 /* Initialize MAC address and backoff seed. */
Michael Chan986e0ae2007-05-05 12:10:20 -07008929 __tg3_set_mac_addr(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008930
8931 /* MTU + ethernet header + FCS + optional VLAN tag */
Matt Carlsonf7b493e2009-02-25 14:21:52 +00008932 tw32(MAC_RX_MTU_SIZE,
8933 tp->dev->mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008934
8935 /* The slot time is changed by tg3_setup_phy if we
8936 * run at gigabit with half duplex.
8937 */
Matt Carlsonf2096f92011-04-05 14:22:48 +00008938 val = (2 << TX_LENGTHS_IPG_CRS_SHIFT) |
8939 (6 << TX_LENGTHS_IPG_SHIFT) |
8940 (32 << TX_LENGTHS_SLOT_TIME_SHIFT);
8941
8942 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
8943 val |= tr32(MAC_TX_LENGTHS) &
8944 (TX_LENGTHS_JMB_FRM_LEN_MSK |
8945 TX_LENGTHS_CNT_DWN_VAL_MSK);
8946
8947 tw32(MAC_TX_LENGTHS, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008948
8949 /* Receive rules. */
8950 tw32(MAC_RCV_RULE_CFG, RCV_RULE_CFG_DEFAULT_CLASS);
8951 tw32(RCVLPC_CONFIG, 0x0181);
8952
8953 /* Calculate RDMAC_MODE setting early, we need it to determine
8954 * the RCVLPC_STATE_ENABLE mask.
8955 */
8956 rdmac_mode = (RDMAC_MODE_ENABLE | RDMAC_MODE_TGTABORT_ENAB |
8957 RDMAC_MODE_MSTABORT_ENAB | RDMAC_MODE_PARITYERR_ENAB |
8958 RDMAC_MODE_ADDROFLOW_ENAB | RDMAC_MODE_FIFOOFLOW_ENAB |
8959 RDMAC_MODE_FIFOURUN_ENAB | RDMAC_MODE_FIFOOREAD_ENAB |
8960 RDMAC_MODE_LNGREAD_ENAB);
Michael Chan85e94ce2005-04-21 17:05:28 -07008961
Matt Carlsondeabaac2010-11-24 08:31:50 +00008962 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717)
Matt Carlson0339e4e2010-02-12 14:47:09 +00008963 rdmac_mode |= RDMAC_MODE_MULT_DMA_RD_DIS;
8964
Matt Carlson57e69832008-05-25 23:48:31 -07008965 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson321d32a2008-11-21 17:22:19 -08008966 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
8967 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
Matt Carlsond30cdd22007-10-07 23:28:35 -07008968 rdmac_mode |= RDMAC_MODE_BD_SBD_CRPT_ENAB |
8969 RDMAC_MODE_MBUF_RBD_CRPT_ENAB |
8970 RDMAC_MODE_MBUF_SBD_CRPT_ENAB;
8971
Matt Carlsonc5908932011-03-09 16:58:25 +00008972 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
8973 tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) {
Joe Perches63c3a662011-04-26 08:12:10 +00008974 if (tg3_flag(tp, TSO_CAPABLE) &&
Matt Carlsonc13e3712007-05-05 11:50:04 -07008975 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008976 rdmac_mode |= RDMAC_MODE_FIFO_SIZE_128;
8977 } else if (!(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH) &&
Joe Perches63c3a662011-04-26 08:12:10 +00008978 !tg3_flag(tp, IS_5788)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008979 rdmac_mode |= RDMAC_MODE_FIFO_LONG_BURST;
8980 }
8981 }
8982
Joe Perches63c3a662011-04-26 08:12:10 +00008983 if (tg3_flag(tp, PCI_EXPRESS))
Michael Chan85e94ce2005-04-21 17:05:28 -07008984 rdmac_mode |= RDMAC_MODE_FIFO_LONG_BURST;
8985
Joe Perches63c3a662011-04-26 08:12:10 +00008986 if (tg3_flag(tp, HW_TSO_1) ||
8987 tg3_flag(tp, HW_TSO_2) ||
8988 tg3_flag(tp, HW_TSO_3))
Matt Carlson027455a2008-12-21 20:19:30 -08008989 rdmac_mode |= RDMAC_MODE_IPV4_LSO_EN;
8990
Matt Carlson108a6c12011-05-19 12:12:47 +00008991 if (tg3_flag(tp, 57765_PLUS) ||
Matt Carlsone849cdc2009-11-13 13:03:38 +00008992 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Matt Carlson027455a2008-12-21 20:19:30 -08008993 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
8994 rdmac_mode |= RDMAC_MODE_IPV6_LSO_EN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008995
Matt Carlsonf2096f92011-04-05 14:22:48 +00008996 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
8997 rdmac_mode |= tr32(RDMAC_MODE) & RDMAC_MODE_H2BNC_VLAN_DET;
8998
Matt Carlson41a8a7e2010-09-15 08:59:53 +00008999 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
9000 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
9001 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
9002 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
Joe Perches63c3a662011-04-26 08:12:10 +00009003 tg3_flag(tp, 57765_PLUS)) {
Matt Carlson41a8a7e2010-09-15 08:59:53 +00009004 val = tr32(TG3_RDMA_RSRVCTRL_REG);
Matt Carlsond78b59f2011-04-05 14:22:46 +00009005 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
9006 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
Matt Carlsonb4495ed2011-01-25 15:58:47 +00009007 val &= ~(TG3_RDMA_RSRVCTRL_TXMRGN_MASK |
9008 TG3_RDMA_RSRVCTRL_FIFO_LWM_MASK |
9009 TG3_RDMA_RSRVCTRL_FIFO_HWM_MASK);
9010 val |= TG3_RDMA_RSRVCTRL_TXMRGN_320B |
9011 TG3_RDMA_RSRVCTRL_FIFO_LWM_1_5K |
9012 TG3_RDMA_RSRVCTRL_FIFO_HWM_1_5K;
Matt Carlsonb75cc0e2010-11-24 08:31:46 +00009013 }
Matt Carlson41a8a7e2010-09-15 08:59:53 +00009014 tw32(TG3_RDMA_RSRVCTRL_REG,
9015 val | TG3_RDMA_RSRVCTRL_FIFO_OFLW_FIX);
9016 }
9017
Matt Carlsond78b59f2011-04-05 14:22:46 +00009018 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
9019 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
Matt Carlsond309a462010-09-30 10:34:31 +00009020 val = tr32(TG3_LSO_RD_DMA_CRPTEN_CTRL);
9021 tw32(TG3_LSO_RD_DMA_CRPTEN_CTRL, val |
9022 TG3_LSO_RD_DMA_CRPTEN_CTRL_BLEN_BD_4K |
9023 TG3_LSO_RD_DMA_CRPTEN_CTRL_BLEN_LSO_4K);
9024 }
9025
Linus Torvalds1da177e2005-04-16 15:20:36 -07009026 /* Receive/send statistics. */
Joe Perches63c3a662011-04-26 08:12:10 +00009027 if (tg3_flag(tp, 5750_PLUS)) {
Michael Chan16613942006-06-29 20:15:13 -07009028 val = tr32(RCVLPC_STATS_ENABLE);
9029 val &= ~RCVLPC_STATSENAB_DACK_FIX;
9030 tw32(RCVLPC_STATS_ENABLE, val);
9031 } else if ((rdmac_mode & RDMAC_MODE_FIFO_SIZE_128) &&
Joe Perches63c3a662011-04-26 08:12:10 +00009032 tg3_flag(tp, TSO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009033 val = tr32(RCVLPC_STATS_ENABLE);
9034 val &= ~RCVLPC_STATSENAB_LNGBRST_RFIX;
9035 tw32(RCVLPC_STATS_ENABLE, val);
9036 } else {
9037 tw32(RCVLPC_STATS_ENABLE, 0xffffff);
9038 }
9039 tw32(RCVLPC_STATSCTRL, RCVLPC_STATSCTRL_ENABLE);
9040 tw32(SNDDATAI_STATSENAB, 0xffffff);
9041 tw32(SNDDATAI_STATSCTRL,
9042 (SNDDATAI_SCTRL_ENABLE |
9043 SNDDATAI_SCTRL_FASTUPD));
9044
9045 /* Setup host coalescing engine. */
9046 tw32(HOSTCC_MODE, 0);
9047 for (i = 0; i < 2000; i++) {
9048 if (!(tr32(HOSTCC_MODE) & HOSTCC_MODE_ENABLE))
9049 break;
9050 udelay(10);
9051 }
9052
Michael Chand244c892005-07-05 14:42:33 -07009053 __tg3_set_coalesce(tp, &tp->coal);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009054
Joe Perches63c3a662011-04-26 08:12:10 +00009055 if (!tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009056 /* Status/statistics block address. See tg3_timer,
9057 * the tg3_periodic_fetch_stats call there, and
9058 * tg3_get_stats to see how this works for 5705/5750 chips.
9059 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07009060 tw32(HOSTCC_STATS_BLK_HOST_ADDR + TG3_64BIT_REG_HIGH,
9061 ((u64) tp->stats_mapping >> 32));
9062 tw32(HOSTCC_STATS_BLK_HOST_ADDR + TG3_64BIT_REG_LOW,
9063 ((u64) tp->stats_mapping & 0xffffffff));
9064 tw32(HOSTCC_STATS_BLK_NIC_ADDR, NIC_SRAM_STATS_BLK);
Matt Carlson2d31eca2009-09-01 12:53:31 +00009065
Linus Torvalds1da177e2005-04-16 15:20:36 -07009066 tw32(HOSTCC_STATUS_BLK_NIC_ADDR, NIC_SRAM_STATUS_BLK);
Matt Carlson2d31eca2009-09-01 12:53:31 +00009067
9068 /* Clear statistics and status block memory areas */
9069 for (i = NIC_SRAM_STATS_BLK;
9070 i < NIC_SRAM_STATUS_BLK + TG3_HW_STATUS_SIZE;
9071 i += sizeof(u32)) {
9072 tg3_write_mem(tp, i, 0);
9073 udelay(40);
9074 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009075 }
9076
9077 tw32(HOSTCC_MODE, HOSTCC_MODE_ENABLE | tp->coalesce_mode);
9078
9079 tw32(RCVCC_MODE, RCVCC_MODE_ENABLE | RCVCC_MODE_ATTN_ENABLE);
9080 tw32(RCVLPC_MODE, RCVLPC_MODE_ENABLE);
Joe Perches63c3a662011-04-26 08:12:10 +00009081 if (!tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009082 tw32(RCVLSC_MODE, RCVLSC_MODE_ENABLE | RCVLSC_MODE_ATTN_ENABLE);
9083
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009084 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES) {
9085 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chanc94e3942005-09-27 12:12:42 -07009086 /* reset to prevent losing 1st rx packet intermittently */
9087 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
9088 udelay(10);
9089 }
9090
Matt Carlson3bda1252008-08-15 14:08:22 -07009091 tp->mac_mode |= MAC_MODE_TXSTAT_ENABLE | MAC_MODE_RXSTAT_ENABLE |
Matt Carlson9e975cc2011-07-20 10:20:50 +00009092 MAC_MODE_TDE_ENABLE | MAC_MODE_RDE_ENABLE |
9093 MAC_MODE_FHDE_ENABLE;
9094 if (tg3_flag(tp, ENABLE_APE))
9095 tp->mac_mode |= MAC_MODE_APE_TX_EN | MAC_MODE_APE_RX_EN;
Joe Perches63c3a662011-04-26 08:12:10 +00009096 if (!tg3_flag(tp, 5705_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009097 !(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07009098 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700)
9099 tp->mac_mode |= MAC_MODE_LINK_POLARITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009100 tw32_f(MAC_MODE, tp->mac_mode | MAC_MODE_RXSTAT_CLEAR | MAC_MODE_TXSTAT_CLEAR);
9101 udelay(40);
9102
Michael Chan314fba32005-04-21 17:07:04 -07009103 /* tp->grc_local_ctrl is partially set up during tg3_get_invariants().
Joe Perches63c3a662011-04-26 08:12:10 +00009104 * If TG3_FLAG_IS_NIC is zero, we should read the
Michael Chan314fba32005-04-21 17:07:04 -07009105 * register to preserve the GPIO settings for LOMs. The GPIOs,
9106 * whether used as inputs or outputs, are set by boot code after
9107 * reset.
9108 */
Joe Perches63c3a662011-04-26 08:12:10 +00009109 if (!tg3_flag(tp, IS_NIC)) {
Michael Chan314fba32005-04-21 17:07:04 -07009110 u32 gpio_mask;
9111
Michael Chan9d26e212006-12-07 00:21:14 -08009112 gpio_mask = GRC_LCLCTRL_GPIO_OE0 | GRC_LCLCTRL_GPIO_OE1 |
9113 GRC_LCLCTRL_GPIO_OE2 | GRC_LCLCTRL_GPIO_OUTPUT0 |
9114 GRC_LCLCTRL_GPIO_OUTPUT1 | GRC_LCLCTRL_GPIO_OUTPUT2;
Michael Chan3e7d83b2005-04-21 17:10:36 -07009115
9116 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752)
9117 gpio_mask |= GRC_LCLCTRL_GPIO_OE3 |
9118 GRC_LCLCTRL_GPIO_OUTPUT3;
9119
Michael Chanaf36e6b2006-03-23 01:28:06 -08009120 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755)
9121 gpio_mask |= GRC_LCLCTRL_GPIO_UART_SEL;
9122
Gary Zambranoaaf84462007-05-05 11:51:45 -07009123 tp->grc_local_ctrl &= ~gpio_mask;
Michael Chan314fba32005-04-21 17:07:04 -07009124 tp->grc_local_ctrl |= tr32(GRC_LOCAL_CTRL) & gpio_mask;
9125
9126 /* GPIO1 must be driven high for eeprom write protect */
Joe Perches63c3a662011-04-26 08:12:10 +00009127 if (tg3_flag(tp, EEPROM_WRITE_PROT))
Michael Chan9d26e212006-12-07 00:21:14 -08009128 tp->grc_local_ctrl |= (GRC_LCLCTRL_GPIO_OE1 |
9129 GRC_LCLCTRL_GPIO_OUTPUT1);
Michael Chan314fba32005-04-21 17:07:04 -07009130 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009131 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
9132 udelay(100);
9133
Matt Carlsonc3b50032012-01-17 15:27:23 +00009134 if (tg3_flag(tp, USING_MSIX)) {
Matt Carlsonbaf8a942009-09-01 13:13:00 +00009135 val = tr32(MSGINT_MODE);
Matt Carlsonc3b50032012-01-17 15:27:23 +00009136 val |= MSGINT_MODE_ENABLE;
9137 if (tp->irq_cnt > 1)
9138 val |= MSGINT_MODE_MULTIVEC_EN;
Matt Carlson5b39de92011-08-31 11:44:50 +00009139 if (!tg3_flag(tp, 1SHOT_MSI))
9140 val |= MSGINT_MODE_ONE_SHOT_DISABLE;
Matt Carlsonbaf8a942009-09-01 13:13:00 +00009141 tw32(MSGINT_MODE, val);
9142 }
9143
Joe Perches63c3a662011-04-26 08:12:10 +00009144 if (!tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009145 tw32_f(DMAC_MODE, DMAC_MODE_ENABLE);
9146 udelay(40);
9147 }
9148
9149 val = (WDMAC_MODE_ENABLE | WDMAC_MODE_TGTABORT_ENAB |
9150 WDMAC_MODE_MSTABORT_ENAB | WDMAC_MODE_PARITYERR_ENAB |
9151 WDMAC_MODE_ADDROFLOW_ENAB | WDMAC_MODE_FIFOOFLOW_ENAB |
9152 WDMAC_MODE_FIFOURUN_ENAB | WDMAC_MODE_FIFOOREAD_ENAB |
9153 WDMAC_MODE_LNGREAD_ENAB);
9154
Matt Carlsonc5908932011-03-09 16:58:25 +00009155 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
9156 tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) {
Joe Perches63c3a662011-04-26 08:12:10 +00009157 if (tg3_flag(tp, TSO_CAPABLE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07009158 (tp->pci_chip_rev_id == CHIPREV_ID_5705_A1 ||
9159 tp->pci_chip_rev_id == CHIPREV_ID_5705_A2)) {
9160 /* nothing */
9161 } else if (!(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH) &&
Joe Perches63c3a662011-04-26 08:12:10 +00009162 !tg3_flag(tp, IS_5788)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009163 val |= WDMAC_MODE_RX_ACCEL;
9164 }
9165 }
9166
Michael Chand9ab5ad12006-03-20 22:27:35 -08009167 /* Enable host coalescing bug fix */
Joe Perches63c3a662011-04-26 08:12:10 +00009168 if (tg3_flag(tp, 5755_PLUS))
Matt Carlsonf51f3562008-05-25 23:45:08 -07009169 val |= WDMAC_MODE_STATUS_TAG_FIX;
Michael Chand9ab5ad12006-03-20 22:27:35 -08009170
Matt Carlson788a0352009-11-02 14:26:03 +00009171 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
9172 val |= WDMAC_MODE_BURST_ALL_DATA;
9173
Linus Torvalds1da177e2005-04-16 15:20:36 -07009174 tw32_f(WDMAC_MODE, val);
9175 udelay(40);
9176
Joe Perches63c3a662011-04-26 08:12:10 +00009177 if (tg3_flag(tp, PCIX_MODE)) {
Matt Carlson9974a352007-10-07 23:27:28 -07009178 u16 pcix_cmd;
9179
9180 pci_read_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
9181 &pcix_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009182 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703) {
Matt Carlson9974a352007-10-07 23:27:28 -07009183 pcix_cmd &= ~PCI_X_CMD_MAX_READ;
9184 pcix_cmd |= PCI_X_CMD_READ_2K;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009185 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
Matt Carlson9974a352007-10-07 23:27:28 -07009186 pcix_cmd &= ~(PCI_X_CMD_MAX_SPLIT | PCI_X_CMD_MAX_READ);
9187 pcix_cmd |= PCI_X_CMD_READ_2K;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009188 }
Matt Carlson9974a352007-10-07 23:27:28 -07009189 pci_write_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
9190 pcix_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009191 }
9192
9193 tw32_f(RDMAC_MODE, rdmac_mode);
9194 udelay(40);
9195
9196 tw32(RCVDCC_MODE, RCVDCC_MODE_ENABLE | RCVDCC_MODE_ATTN_ENABLE);
Joe Perches63c3a662011-04-26 08:12:10 +00009197 if (!tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009198 tw32(MBFREE_MODE, MBFREE_MODE_ENABLE);
Matt Carlson9936bcf2007-10-10 18:03:07 -07009199
9200 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
9201 tw32(SNDDATAC_MODE,
9202 SNDDATAC_MODE_ENABLE | SNDDATAC_MODE_CDELAY);
9203 else
9204 tw32(SNDDATAC_MODE, SNDDATAC_MODE_ENABLE);
9205
Linus Torvalds1da177e2005-04-16 15:20:36 -07009206 tw32(SNDBDC_MODE, SNDBDC_MODE_ENABLE | SNDBDC_MODE_ATTN_ENABLE);
9207 tw32(RCVBDI_MODE, RCVBDI_MODE_ENABLE | RCVBDI_MODE_RCB_ATTN_ENAB);
Matt Carlson7cb32cf2010-09-30 10:34:36 +00009208 val = RCVDBDI_MODE_ENABLE | RCVDBDI_MODE_INV_RING_SZ;
Joe Perches63c3a662011-04-26 08:12:10 +00009209 if (tg3_flag(tp, LRG_PROD_RING_CAP))
Matt Carlson7cb32cf2010-09-30 10:34:36 +00009210 val |= RCVDBDI_MODE_LRG_RING_SZ;
9211 tw32(RCVDBDI_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009212 tw32(SNDDATAI_MODE, SNDDATAI_MODE_ENABLE);
Joe Perches63c3a662011-04-26 08:12:10 +00009213 if (tg3_flag(tp, HW_TSO_1) ||
9214 tg3_flag(tp, HW_TSO_2) ||
9215 tg3_flag(tp, HW_TSO_3))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009216 tw32(SNDDATAI_MODE, SNDDATAI_MODE_ENABLE | 0x8);
Matt Carlsonbaf8a942009-09-01 13:13:00 +00009217 val = SNDBDI_MODE_ENABLE | SNDBDI_MODE_ATTN_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00009218 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlsonbaf8a942009-09-01 13:13:00 +00009219 val |= SNDBDI_MODE_MULTI_TXQ_EN;
9220 tw32(SNDBDI_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009221 tw32(SNDBDS_MODE, SNDBDS_MODE_ENABLE | SNDBDS_MODE_ATTN_ENABLE);
9222
9223 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0) {
9224 err = tg3_load_5701_a0_firmware_fix(tp);
9225 if (err)
9226 return err;
9227 }
9228
Joe Perches63c3a662011-04-26 08:12:10 +00009229 if (tg3_flag(tp, TSO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009230 err = tg3_load_tso_firmware(tp);
9231 if (err)
9232 return err;
9233 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009234
9235 tp->tx_mode = TX_MODE_ENABLE;
Matt Carlsonf2096f92011-04-05 14:22:48 +00009236
Joe Perches63c3a662011-04-26 08:12:10 +00009237 if (tg3_flag(tp, 5755_PLUS) ||
Matt Carlsonb1d05212010-06-05 17:24:31 +00009238 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
9239 tp->tx_mode |= TX_MODE_MBUF_LOCKUP_FIX;
Matt Carlsonf2096f92011-04-05 14:22:48 +00009240
9241 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
9242 val = TX_MODE_JMB_FRM_LEN | TX_MODE_CNT_DN_MODE;
9243 tp->tx_mode &= ~val;
9244 tp->tx_mode |= tr32(MAC_TX_MODE) & val;
9245 }
9246
Linus Torvalds1da177e2005-04-16 15:20:36 -07009247 tw32_f(MAC_TX_MODE, tp->tx_mode);
9248 udelay(100);
9249
Joe Perches63c3a662011-04-26 08:12:10 +00009250 if (tg3_flag(tp, ENABLE_RSS)) {
Matt Carlsonbcebcc42011-12-14 11:10:01 +00009251 tg3_rss_write_indir_tbl(tp);
Matt Carlsonbaf8a942009-09-01 13:13:00 +00009252
9253 /* Setup the "secret" hash key. */
9254 tw32(MAC_RSS_HASH_KEY_0, 0x5f865437);
9255 tw32(MAC_RSS_HASH_KEY_1, 0xe4ac62cc);
9256 tw32(MAC_RSS_HASH_KEY_2, 0x50103a45);
9257 tw32(MAC_RSS_HASH_KEY_3, 0x36621985);
9258 tw32(MAC_RSS_HASH_KEY_4, 0xbf14c0e8);
9259 tw32(MAC_RSS_HASH_KEY_5, 0x1bc27a1e);
9260 tw32(MAC_RSS_HASH_KEY_6, 0x84f4b556);
9261 tw32(MAC_RSS_HASH_KEY_7, 0x094ea6fe);
9262 tw32(MAC_RSS_HASH_KEY_8, 0x7dda01e7);
9263 tw32(MAC_RSS_HASH_KEY_9, 0xc04d7481);
9264 }
9265
Linus Torvalds1da177e2005-04-16 15:20:36 -07009266 tp->rx_mode = RX_MODE_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00009267 if (tg3_flag(tp, 5755_PLUS))
Michael Chanaf36e6b2006-03-23 01:28:06 -08009268 tp->rx_mode |= RX_MODE_IPV6_CSUM_ENABLE;
9269
Joe Perches63c3a662011-04-26 08:12:10 +00009270 if (tg3_flag(tp, ENABLE_RSS))
Matt Carlsonbaf8a942009-09-01 13:13:00 +00009271 tp->rx_mode |= RX_MODE_RSS_ENABLE |
9272 RX_MODE_RSS_ITBL_HASH_BITS_7 |
9273 RX_MODE_RSS_IPV6_HASH_EN |
9274 RX_MODE_RSS_TCP_IPV6_HASH_EN |
9275 RX_MODE_RSS_IPV4_HASH_EN |
9276 RX_MODE_RSS_TCP_IPV4_HASH_EN;
9277
Linus Torvalds1da177e2005-04-16 15:20:36 -07009278 tw32_f(MAC_RX_MODE, tp->rx_mode);
9279 udelay(10);
9280
Linus Torvalds1da177e2005-04-16 15:20:36 -07009281 tw32(MAC_LED_CTRL, tp->led_ctrl);
9282
9283 tw32(MAC_MI_STAT, MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009284 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009285 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
9286 udelay(10);
9287 }
9288 tw32_f(MAC_RX_MODE, tp->rx_mode);
9289 udelay(10);
9290
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009291 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009292 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009293 !(tp->phy_flags & TG3_PHYFLG_SERDES_PREEMPHASIS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009294 /* Set drive transmission level to 1.2V */
9295 /* only if the signal pre-emphasis bit is not set */
9296 val = tr32(MAC_SERDES_CFG);
9297 val &= 0xfffff000;
9298 val |= 0x880;
9299 tw32(MAC_SERDES_CFG, val);
9300 }
9301 if (tp->pci_chip_rev_id == CHIPREV_ID_5703_A1)
9302 tw32(MAC_SERDES_CFG, 0x616000);
9303 }
9304
9305 /* Prevent chip from dropping frames when flow control
9306 * is enabled.
9307 */
Matt Carlson55086ad2011-12-14 11:09:59 +00009308 if (tg3_flag(tp, 57765_CLASS))
Matt Carlson666bc832010-01-20 16:58:03 +00009309 val = 1;
9310 else
9311 val = 2;
9312 tw32_f(MAC_LOW_WMARK_MAX_RX_FRAME, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009313
9314 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009315 (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009316 /* Use hardware link auto-negotiation */
Joe Perches63c3a662011-04-26 08:12:10 +00009317 tg3_flag_set(tp, HW_AUTONEG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009318 }
9319
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009320 if ((tp->phy_flags & TG3_PHYFLG_MII_SERDES) &&
Matt Carlson6ff6f812011-05-19 12:12:54 +00009321 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
Michael Chand4d2c552006-03-20 17:47:20 -08009322 u32 tmp;
9323
9324 tmp = tr32(SERDES_RX_CTRL);
9325 tw32(SERDES_RX_CTRL, tmp | SERDES_RX_SIG_DETECT);
9326 tp->grc_local_ctrl &= ~GRC_LCLCTRL_USE_EXT_SIG_DETECT;
9327 tp->grc_local_ctrl |= GRC_LCLCTRL_USE_SIG_DETECT;
9328 tw32(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
9329 }
9330
Joe Perches63c3a662011-04-26 08:12:10 +00009331 if (!tg3_flag(tp, USE_PHYLIB)) {
Matt Carlsonc6700ce2012-02-13 15:20:15 +00009332 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Matt Carlson80096062010-08-02 11:26:06 +00009333 tp->phy_flags &= ~TG3_PHYFLG_IS_LOW_POWER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009334
Matt Carlsondd477002008-05-25 23:45:58 -07009335 err = tg3_setup_phy(tp, 0);
9336 if (err)
9337 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009338
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009339 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
9340 !(tp->phy_flags & TG3_PHYFLG_IS_FET)) {
Matt Carlsondd477002008-05-25 23:45:58 -07009341 u32 tmp;
9342
9343 /* Clear CRC stats. */
9344 if (!tg3_readphy(tp, MII_TG3_TEST1, &tmp)) {
9345 tg3_writephy(tp, MII_TG3_TEST1,
9346 tmp | MII_TG3_TEST1_CRC_EN);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00009347 tg3_readphy(tp, MII_TG3_RXR_COUNTERS, &tmp);
Matt Carlsondd477002008-05-25 23:45:58 -07009348 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009349 }
9350 }
9351
9352 __tg3_set_rx_mode(tp->dev);
9353
9354 /* Initialize receive rules. */
9355 tw32(MAC_RCV_RULE_0, 0xc2000000 & RCV_RULE_DISABLE_MASK);
9356 tw32(MAC_RCV_VALUE_0, 0xffffffff & RCV_RULE_DISABLE_MASK);
9357 tw32(MAC_RCV_RULE_1, 0x86000004 & RCV_RULE_DISABLE_MASK);
9358 tw32(MAC_RCV_VALUE_1, 0xffffffff & RCV_RULE_DISABLE_MASK);
9359
Joe Perches63c3a662011-04-26 08:12:10 +00009360 if (tg3_flag(tp, 5705_PLUS) && !tg3_flag(tp, 5780_CLASS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009361 limit = 8;
9362 else
9363 limit = 16;
Joe Perches63c3a662011-04-26 08:12:10 +00009364 if (tg3_flag(tp, ENABLE_ASF))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009365 limit -= 4;
9366 switch (limit) {
9367 case 16:
9368 tw32(MAC_RCV_RULE_15, 0); tw32(MAC_RCV_VALUE_15, 0);
9369 case 15:
9370 tw32(MAC_RCV_RULE_14, 0); tw32(MAC_RCV_VALUE_14, 0);
9371 case 14:
9372 tw32(MAC_RCV_RULE_13, 0); tw32(MAC_RCV_VALUE_13, 0);
9373 case 13:
9374 tw32(MAC_RCV_RULE_12, 0); tw32(MAC_RCV_VALUE_12, 0);
9375 case 12:
9376 tw32(MAC_RCV_RULE_11, 0); tw32(MAC_RCV_VALUE_11, 0);
9377 case 11:
9378 tw32(MAC_RCV_RULE_10, 0); tw32(MAC_RCV_VALUE_10, 0);
9379 case 10:
9380 tw32(MAC_RCV_RULE_9, 0); tw32(MAC_RCV_VALUE_9, 0);
9381 case 9:
9382 tw32(MAC_RCV_RULE_8, 0); tw32(MAC_RCV_VALUE_8, 0);
9383 case 8:
9384 tw32(MAC_RCV_RULE_7, 0); tw32(MAC_RCV_VALUE_7, 0);
9385 case 7:
9386 tw32(MAC_RCV_RULE_6, 0); tw32(MAC_RCV_VALUE_6, 0);
9387 case 6:
9388 tw32(MAC_RCV_RULE_5, 0); tw32(MAC_RCV_VALUE_5, 0);
9389 case 5:
9390 tw32(MAC_RCV_RULE_4, 0); tw32(MAC_RCV_VALUE_4, 0);
9391 case 4:
9392 /* tw32(MAC_RCV_RULE_3, 0); tw32(MAC_RCV_VALUE_3, 0); */
9393 case 3:
9394 /* tw32(MAC_RCV_RULE_2, 0); tw32(MAC_RCV_VALUE_2, 0); */
9395 case 2:
9396 case 1:
9397
9398 default:
9399 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07009400 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009401
Joe Perches63c3a662011-04-26 08:12:10 +00009402 if (tg3_flag(tp, ENABLE_APE))
Matt Carlson9ce768e2007-10-11 19:49:11 -07009403 /* Write our heartbeat update interval to APE. */
9404 tg3_ape_write32(tp, TG3_APE_HOST_HEARTBEAT_INT_MS,
9405 APE_HOST_HEARTBEAT_INT_DISABLE);
Matt Carlson0d3031d2007-10-10 18:02:43 -07009406
Linus Torvalds1da177e2005-04-16 15:20:36 -07009407 tg3_write_sig_post_reset(tp, RESET_KIND_INIT);
9408
Linus Torvalds1da177e2005-04-16 15:20:36 -07009409 return 0;
9410}
9411
9412/* Called at device open time to get the chip ready for
9413 * packet processing. Invoked with tp->lock held.
9414 */
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07009415static int tg3_init_hw(struct tg3 *tp, int reset_phy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009416{
Linus Torvalds1da177e2005-04-16 15:20:36 -07009417 tg3_switch_clocks(tp);
9418
9419 tw32(TG3PCI_MEM_WIN_BASE_ADDR, 0);
9420
Matt Carlson2f751b62008-08-04 23:17:34 -07009421 return tg3_reset_hw(tp, reset_phy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009422}
9423
9424#define TG3_STAT_ADD32(PSTAT, REG) \
9425do { u32 __val = tr32(REG); \
9426 (PSTAT)->low += __val; \
9427 if ((PSTAT)->low < __val) \
9428 (PSTAT)->high += 1; \
9429} while (0)
9430
9431static void tg3_periodic_fetch_stats(struct tg3 *tp)
9432{
9433 struct tg3_hw_stats *sp = tp->hw_stats;
9434
9435 if (!netif_carrier_ok(tp->dev))
9436 return;
9437
9438 TG3_STAT_ADD32(&sp->tx_octets, MAC_TX_STATS_OCTETS);
9439 TG3_STAT_ADD32(&sp->tx_collisions, MAC_TX_STATS_COLLISIONS);
9440 TG3_STAT_ADD32(&sp->tx_xon_sent, MAC_TX_STATS_XON_SENT);
9441 TG3_STAT_ADD32(&sp->tx_xoff_sent, MAC_TX_STATS_XOFF_SENT);
9442 TG3_STAT_ADD32(&sp->tx_mac_errors, MAC_TX_STATS_MAC_ERRORS);
9443 TG3_STAT_ADD32(&sp->tx_single_collisions, MAC_TX_STATS_SINGLE_COLLISIONS);
9444 TG3_STAT_ADD32(&sp->tx_mult_collisions, MAC_TX_STATS_MULT_COLLISIONS);
9445 TG3_STAT_ADD32(&sp->tx_deferred, MAC_TX_STATS_DEFERRED);
9446 TG3_STAT_ADD32(&sp->tx_excessive_collisions, MAC_TX_STATS_EXCESSIVE_COL);
9447 TG3_STAT_ADD32(&sp->tx_late_collisions, MAC_TX_STATS_LATE_COL);
9448 TG3_STAT_ADD32(&sp->tx_ucast_packets, MAC_TX_STATS_UCAST);
9449 TG3_STAT_ADD32(&sp->tx_mcast_packets, MAC_TX_STATS_MCAST);
9450 TG3_STAT_ADD32(&sp->tx_bcast_packets, MAC_TX_STATS_BCAST);
9451
9452 TG3_STAT_ADD32(&sp->rx_octets, MAC_RX_STATS_OCTETS);
9453 TG3_STAT_ADD32(&sp->rx_fragments, MAC_RX_STATS_FRAGMENTS);
9454 TG3_STAT_ADD32(&sp->rx_ucast_packets, MAC_RX_STATS_UCAST);
9455 TG3_STAT_ADD32(&sp->rx_mcast_packets, MAC_RX_STATS_MCAST);
9456 TG3_STAT_ADD32(&sp->rx_bcast_packets, MAC_RX_STATS_BCAST);
9457 TG3_STAT_ADD32(&sp->rx_fcs_errors, MAC_RX_STATS_FCS_ERRORS);
9458 TG3_STAT_ADD32(&sp->rx_align_errors, MAC_RX_STATS_ALIGN_ERRORS);
9459 TG3_STAT_ADD32(&sp->rx_xon_pause_rcvd, MAC_RX_STATS_XON_PAUSE_RECVD);
9460 TG3_STAT_ADD32(&sp->rx_xoff_pause_rcvd, MAC_RX_STATS_XOFF_PAUSE_RECVD);
9461 TG3_STAT_ADD32(&sp->rx_mac_ctrl_rcvd, MAC_RX_STATS_MAC_CTRL_RECVD);
9462 TG3_STAT_ADD32(&sp->rx_xoff_entered, MAC_RX_STATS_XOFF_ENTERED);
9463 TG3_STAT_ADD32(&sp->rx_frame_too_long_errors, MAC_RX_STATS_FRAME_TOO_LONG);
9464 TG3_STAT_ADD32(&sp->rx_jabbers, MAC_RX_STATS_JABBERS);
9465 TG3_STAT_ADD32(&sp->rx_undersize_packets, MAC_RX_STATS_UNDERSIZE);
Michael Chan463d3052006-05-22 16:36:27 -07009466
9467 TG3_STAT_ADD32(&sp->rxbds_empty, RCVLPC_NO_RCV_BD_CNT);
Matt Carlson310050f2011-05-19 12:12:55 +00009468 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717 &&
9469 tp->pci_chip_rev_id != CHIPREV_ID_5719_A0 &&
9470 tp->pci_chip_rev_id != CHIPREV_ID_5720_A0) {
Matt Carlson4d958472011-04-20 07:57:35 +00009471 TG3_STAT_ADD32(&sp->rx_discards, RCVLPC_IN_DISCARDS_CNT);
9472 } else {
9473 u32 val = tr32(HOSTCC_FLOW_ATTN);
9474 val = (val & HOSTCC_FLOW_ATTN_MBUF_LWM) ? 1 : 0;
9475 if (val) {
9476 tw32(HOSTCC_FLOW_ATTN, HOSTCC_FLOW_ATTN_MBUF_LWM);
9477 sp->rx_discards.low += val;
9478 if (sp->rx_discards.low < val)
9479 sp->rx_discards.high += 1;
9480 }
9481 sp->mbuf_lwm_thresh_hit = sp->rx_discards;
9482 }
Michael Chan463d3052006-05-22 16:36:27 -07009483 TG3_STAT_ADD32(&sp->rx_errors, RCVLPC_IN_ERRORS_CNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009484}
9485
Matt Carlson0e6cf6a2011-06-13 13:38:55 +00009486static void tg3_chk_missed_msi(struct tg3 *tp)
9487{
9488 u32 i;
9489
9490 for (i = 0; i < tp->irq_cnt; i++) {
9491 struct tg3_napi *tnapi = &tp->napi[i];
9492
9493 if (tg3_has_work(tnapi)) {
9494 if (tnapi->last_rx_cons == tnapi->rx_rcb_ptr &&
9495 tnapi->last_tx_cons == tnapi->tx_cons) {
9496 if (tnapi->chk_msi_cnt < 1) {
9497 tnapi->chk_msi_cnt++;
9498 return;
9499 }
Matt Carlson7f230732011-08-31 11:44:48 +00009500 tg3_msi(0, tnapi);
Matt Carlson0e6cf6a2011-06-13 13:38:55 +00009501 }
9502 }
9503 tnapi->chk_msi_cnt = 0;
9504 tnapi->last_rx_cons = tnapi->rx_rcb_ptr;
9505 tnapi->last_tx_cons = tnapi->tx_cons;
9506 }
9507}
9508
Linus Torvalds1da177e2005-04-16 15:20:36 -07009509static void tg3_timer(unsigned long __opaque)
9510{
9511 struct tg3 *tp = (struct tg3 *) __opaque;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009512
Matt Carlson5b190622011-11-04 09:15:04 +00009513 if (tp->irq_sync || tg3_flag(tp, RESET_TASK_PENDING))
Michael Chanf475f162006-03-27 23:20:14 -08009514 goto restart_timer;
9515
David S. Millerf47c11e2005-06-24 20:18:35 -07009516 spin_lock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009517
Matt Carlson0e6cf6a2011-06-13 13:38:55 +00009518 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
Matt Carlson55086ad2011-12-14 11:09:59 +00009519 tg3_flag(tp, 57765_CLASS))
Matt Carlson0e6cf6a2011-06-13 13:38:55 +00009520 tg3_chk_missed_msi(tp);
9521
Joe Perches63c3a662011-04-26 08:12:10 +00009522 if (!tg3_flag(tp, TAGGED_STATUS)) {
David S. Millerfac9b832005-05-18 22:46:34 -07009523 /* All of this garbage is because when using non-tagged
9524 * IRQ status the mailbox/status_block protocol the chip
9525 * uses with the cpu is race prone.
9526 */
Matt Carlson898a56f2009-08-28 14:02:40 +00009527 if (tp->napi[0].hw_status->status & SD_STATUS_UPDATED) {
David S. Millerfac9b832005-05-18 22:46:34 -07009528 tw32(GRC_LOCAL_CTRL,
9529 tp->grc_local_ctrl | GRC_LCLCTRL_SETINT);
9530 } else {
9531 tw32(HOSTCC_MODE, tp->coalesce_mode |
Matt Carlsonfd2ce372009-09-01 12:51:13 +00009532 HOSTCC_MODE_ENABLE | HOSTCC_MODE_NOW);
David S. Millerfac9b832005-05-18 22:46:34 -07009533 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009534
David S. Millerfac9b832005-05-18 22:46:34 -07009535 if (!(tr32(WDMAC_MODE) & WDMAC_MODE_ENABLE)) {
David S. Millerf47c11e2005-06-24 20:18:35 -07009536 spin_unlock(&tp->lock);
Matt Carlsondb219972011-11-04 09:15:03 +00009537 tg3_reset_task_schedule(tp);
Matt Carlson5b190622011-11-04 09:15:04 +00009538 goto restart_timer;
David S. Millerfac9b832005-05-18 22:46:34 -07009539 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009540 }
9541
Linus Torvalds1da177e2005-04-16 15:20:36 -07009542 /* This part only runs once per second. */
9543 if (!--tp->timer_counter) {
Joe Perches63c3a662011-04-26 08:12:10 +00009544 if (tg3_flag(tp, 5705_PLUS))
David S. Millerfac9b832005-05-18 22:46:34 -07009545 tg3_periodic_fetch_stats(tp);
9546
Matt Carlsonb0c59432011-05-19 12:12:48 +00009547 if (tp->setlpicnt && !--tp->setlpicnt)
9548 tg3_phy_eee_enable(tp);
Matt Carlson52b02d02010-10-14 10:37:41 +00009549
Joe Perches63c3a662011-04-26 08:12:10 +00009550 if (tg3_flag(tp, USE_LINKCHG_REG)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009551 u32 mac_stat;
9552 int phy_event;
9553
9554 mac_stat = tr32(MAC_STATUS);
9555
9556 phy_event = 0;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009557 if (tp->phy_flags & TG3_PHYFLG_USE_MI_INTERRUPT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009558 if (mac_stat & MAC_STATUS_MI_INTERRUPT)
9559 phy_event = 1;
9560 } else if (mac_stat & MAC_STATUS_LNKSTATE_CHANGED)
9561 phy_event = 1;
9562
9563 if (phy_event)
9564 tg3_setup_phy(tp, 0);
Joe Perches63c3a662011-04-26 08:12:10 +00009565 } else if (tg3_flag(tp, POLL_SERDES)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009566 u32 mac_stat = tr32(MAC_STATUS);
9567 int need_setup = 0;
9568
9569 if (netif_carrier_ok(tp->dev) &&
9570 (mac_stat & MAC_STATUS_LNKSTATE_CHANGED)) {
9571 need_setup = 1;
9572 }
Matt Carlsonbe98da62010-07-11 09:31:46 +00009573 if (!netif_carrier_ok(tp->dev) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07009574 (mac_stat & (MAC_STATUS_PCS_SYNCED |
9575 MAC_STATUS_SIGNAL_DET))) {
9576 need_setup = 1;
9577 }
9578 if (need_setup) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07009579 if (!tp->serdes_counter) {
9580 tw32_f(MAC_MODE,
9581 (tp->mac_mode &
9582 ~MAC_MODE_PORT_MODE_MASK));
9583 udelay(40);
9584 tw32_f(MAC_MODE, tp->mac_mode);
9585 udelay(40);
9586 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009587 tg3_setup_phy(tp, 0);
9588 }
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009589 } else if ((tp->phy_flags & TG3_PHYFLG_MII_SERDES) &&
Joe Perches63c3a662011-04-26 08:12:10 +00009590 tg3_flag(tp, 5780_CLASS)) {
Michael Chan747e8f82005-07-25 12:33:22 -07009591 tg3_serdes_parallel_detect(tp);
Matt Carlson57d8b882010-06-05 17:24:35 +00009592 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009593
9594 tp->timer_counter = tp->timer_multiplier;
9595 }
9596
Michael Chan130b8e42006-09-27 16:00:40 -07009597 /* Heartbeat is only sent once every 2 seconds.
9598 *
9599 * The heartbeat is to tell the ASF firmware that the host
9600 * driver is still alive. In the event that the OS crashes,
9601 * ASF needs to reset the hardware to free up the FIFO space
9602 * that may be filled with rx packets destined for the host.
9603 * If the FIFO is full, ASF will no longer function properly.
9604 *
9605 * Unintended resets have been reported on real time kernels
9606 * where the timer doesn't run on time. Netpoll will also have
9607 * same problem.
9608 *
9609 * The new FWCMD_NICDRV_ALIVE3 command tells the ASF firmware
9610 * to check the ring condition when the heartbeat is expiring
9611 * before doing the reset. This will prevent most unintended
9612 * resets.
9613 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07009614 if (!--tp->asf_counter) {
Joe Perches63c3a662011-04-26 08:12:10 +00009615 if (tg3_flag(tp, ENABLE_ASF) && !tg3_flag(tp, ENABLE_APE)) {
Matt Carlson7c5026a2008-05-02 16:49:29 -07009616 tg3_wait_for_event_ack(tp);
9617
Michael Chanbbadf502006-04-06 21:46:34 -07009618 tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX,
Michael Chan130b8e42006-09-27 16:00:40 -07009619 FWCMD_NICDRV_ALIVE3);
Michael Chanbbadf502006-04-06 21:46:34 -07009620 tg3_write_mem(tp, NIC_SRAM_FW_CMD_LEN_MBOX, 4);
Matt Carlsonc6cdf432010-04-05 10:19:26 +00009621 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX,
9622 TG3_FW_UPDATE_TIMEOUT_SEC);
Matt Carlson4ba526c2008-08-15 14:10:04 -07009623
9624 tg3_generate_fw_event(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009625 }
9626 tp->asf_counter = tp->asf_multiplier;
9627 }
9628
David S. Millerf47c11e2005-06-24 20:18:35 -07009629 spin_unlock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009630
Michael Chanf475f162006-03-27 23:20:14 -08009631restart_timer:
Linus Torvalds1da177e2005-04-16 15:20:36 -07009632 tp->timer.expires = jiffies + tp->timer_offset;
9633 add_timer(&tp->timer);
9634}
9635
Matt Carlson21f76382012-02-22 12:35:21 +00009636static void __devinit tg3_timer_init(struct tg3 *tp)
9637{
9638 if (tg3_flag(tp, TAGGED_STATUS) &&
9639 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717 &&
9640 !tg3_flag(tp, 57765_CLASS))
9641 tp->timer_offset = HZ;
9642 else
9643 tp->timer_offset = HZ / 10;
9644
9645 BUG_ON(tp->timer_offset > HZ);
9646
9647 tp->timer_multiplier = (HZ / tp->timer_offset);
9648 tp->asf_multiplier = (HZ / tp->timer_offset) *
9649 TG3_FW_UPDATE_FREQ_SEC;
9650
9651 init_timer(&tp->timer);
9652 tp->timer.data = (unsigned long) tp;
9653 tp->timer.function = tg3_timer;
9654}
9655
9656static void tg3_timer_start(struct tg3 *tp)
9657{
9658 tp->asf_counter = tp->asf_multiplier;
9659 tp->timer_counter = tp->timer_multiplier;
9660
9661 tp->timer.expires = jiffies + tp->timer_offset;
9662 add_timer(&tp->timer);
9663}
9664
9665static void tg3_timer_stop(struct tg3 *tp)
9666{
9667 del_timer_sync(&tp->timer);
9668}
9669
9670/* Restart hardware after configuration changes, self-test, etc.
9671 * Invoked with tp->lock held.
9672 */
9673static int tg3_restart_hw(struct tg3 *tp, int reset_phy)
9674 __releases(tp->lock)
9675 __acquires(tp->lock)
9676{
9677 int err;
9678
9679 err = tg3_init_hw(tp, reset_phy);
9680 if (err) {
9681 netdev_err(tp->dev,
9682 "Failed to re-initialize device, aborting\n");
9683 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
9684 tg3_full_unlock(tp);
9685 tg3_timer_stop(tp);
9686 tp->irq_sync = 0;
9687 tg3_napi_enable(tp);
9688 dev_close(tp->dev);
9689 tg3_full_lock(tp, 0);
9690 }
9691 return err;
9692}
9693
9694static void tg3_reset_task(struct work_struct *work)
9695{
9696 struct tg3 *tp = container_of(work, struct tg3, reset_task);
9697 int err;
9698
9699 tg3_full_lock(tp, 0);
9700
9701 if (!netif_running(tp->dev)) {
9702 tg3_flag_clear(tp, RESET_TASK_PENDING);
9703 tg3_full_unlock(tp);
9704 return;
9705 }
9706
9707 tg3_full_unlock(tp);
9708
9709 tg3_phy_stop(tp);
9710
9711 tg3_netif_stop(tp);
9712
9713 tg3_full_lock(tp, 1);
9714
9715 if (tg3_flag(tp, TX_RECOVERY_PENDING)) {
9716 tp->write32_tx_mbox = tg3_write32_tx_mbox;
9717 tp->write32_rx_mbox = tg3_write_flush_reg32;
9718 tg3_flag_set(tp, MBOX_WRITE_REORDER);
9719 tg3_flag_clear(tp, TX_RECOVERY_PENDING);
9720 }
9721
9722 tg3_halt(tp, RESET_KIND_SHUTDOWN, 0);
9723 err = tg3_init_hw(tp, 1);
9724 if (err)
9725 goto out;
9726
9727 tg3_netif_start(tp);
9728
9729out:
9730 tg3_full_unlock(tp);
9731
9732 if (!err)
9733 tg3_phy_start(tp);
9734
9735 tg3_flag_clear(tp, RESET_TASK_PENDING);
9736}
9737
Matt Carlson4f125f42009-09-01 12:55:02 +00009738static int tg3_request_irq(struct tg3 *tp, int irq_num)
Michael Chanfcfa0a32006-03-20 22:28:41 -08009739{
David Howells7d12e782006-10-05 14:55:46 +01009740 irq_handler_t fn;
Michael Chanfcfa0a32006-03-20 22:28:41 -08009741 unsigned long flags;
Matt Carlson4f125f42009-09-01 12:55:02 +00009742 char *name;
9743 struct tg3_napi *tnapi = &tp->napi[irq_num];
9744
9745 if (tp->irq_cnt == 1)
9746 name = tp->dev->name;
9747 else {
9748 name = &tnapi->irq_lbl[0];
9749 snprintf(name, IFNAMSIZ, "%s-%d", tp->dev->name, irq_num);
9750 name[IFNAMSIZ-1] = 0;
9751 }
Michael Chanfcfa0a32006-03-20 22:28:41 -08009752
Joe Perches63c3a662011-04-26 08:12:10 +00009753 if (tg3_flag(tp, USING_MSI) || tg3_flag(tp, USING_MSIX)) {
Michael Chanfcfa0a32006-03-20 22:28:41 -08009754 fn = tg3_msi;
Joe Perches63c3a662011-04-26 08:12:10 +00009755 if (tg3_flag(tp, 1SHOT_MSI))
Michael Chanfcfa0a32006-03-20 22:28:41 -08009756 fn = tg3_msi_1shot;
Javier Martinez Canillasab392d22011-03-28 16:27:31 +00009757 flags = 0;
Michael Chanfcfa0a32006-03-20 22:28:41 -08009758 } else {
9759 fn = tg3_interrupt;
Joe Perches63c3a662011-04-26 08:12:10 +00009760 if (tg3_flag(tp, TAGGED_STATUS))
Michael Chanfcfa0a32006-03-20 22:28:41 -08009761 fn = tg3_interrupt_tagged;
Javier Martinez Canillasab392d22011-03-28 16:27:31 +00009762 flags = IRQF_SHARED;
Michael Chanfcfa0a32006-03-20 22:28:41 -08009763 }
Matt Carlson4f125f42009-09-01 12:55:02 +00009764
9765 return request_irq(tnapi->irq_vec, fn, flags, name, tnapi);
Michael Chanfcfa0a32006-03-20 22:28:41 -08009766}
9767
Michael Chan79381092005-04-21 17:13:59 -07009768static int tg3_test_interrupt(struct tg3 *tp)
9769{
Matt Carlson09943a12009-08-28 14:01:57 +00009770 struct tg3_napi *tnapi = &tp->napi[0];
Michael Chan79381092005-04-21 17:13:59 -07009771 struct net_device *dev = tp->dev;
Michael Chanb16250e2006-09-27 16:10:14 -07009772 int err, i, intr_ok = 0;
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009773 u32 val;
Michael Chan79381092005-04-21 17:13:59 -07009774
Michael Chand4bc3922005-05-29 14:59:20 -07009775 if (!netif_running(dev))
9776 return -ENODEV;
9777
Michael Chan79381092005-04-21 17:13:59 -07009778 tg3_disable_ints(tp);
9779
Matt Carlson4f125f42009-09-01 12:55:02 +00009780 free_irq(tnapi->irq_vec, tnapi);
Michael Chan79381092005-04-21 17:13:59 -07009781
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009782 /*
9783 * Turn off MSI one shot mode. Otherwise this test has no
9784 * observable way to know whether the interrupt was delivered.
9785 */
Matt Carlson3aa1cdf2011-07-20 10:20:55 +00009786 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009787 val = tr32(MSGINT_MODE) | MSGINT_MODE_ONE_SHOT_DISABLE;
9788 tw32(MSGINT_MODE, val);
9789 }
9790
Matt Carlson4f125f42009-09-01 12:55:02 +00009791 err = request_irq(tnapi->irq_vec, tg3_test_isr,
Davidlohr Buesof274fd92012-02-22 03:06:54 +00009792 IRQF_SHARED, dev->name, tnapi);
Michael Chan79381092005-04-21 17:13:59 -07009793 if (err)
9794 return err;
9795
Matt Carlson898a56f2009-08-28 14:02:40 +00009796 tnapi->hw_status->status &= ~SD_STATUS_UPDATED;
Michael Chan79381092005-04-21 17:13:59 -07009797 tg3_enable_ints(tp);
9798
9799 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +00009800 tnapi->coal_now);
Michael Chan79381092005-04-21 17:13:59 -07009801
9802 for (i = 0; i < 5; i++) {
Michael Chanb16250e2006-09-27 16:10:14 -07009803 u32 int_mbox, misc_host_ctrl;
9804
Matt Carlson898a56f2009-08-28 14:02:40 +00009805 int_mbox = tr32_mailbox(tnapi->int_mbox);
Michael Chanb16250e2006-09-27 16:10:14 -07009806 misc_host_ctrl = tr32(TG3PCI_MISC_HOST_CTRL);
9807
9808 if ((int_mbox != 0) ||
9809 (misc_host_ctrl & MISC_HOST_CTRL_MASK_PCI_INT)) {
9810 intr_ok = 1;
Michael Chan79381092005-04-21 17:13:59 -07009811 break;
Michael Chanb16250e2006-09-27 16:10:14 -07009812 }
9813
Matt Carlson3aa1cdf2011-07-20 10:20:55 +00009814 if (tg3_flag(tp, 57765_PLUS) &&
9815 tnapi->hw_status->status_tag != tnapi->last_tag)
9816 tw32_mailbox_f(tnapi->int_mbox, tnapi->last_tag << 24);
9817
Michael Chan79381092005-04-21 17:13:59 -07009818 msleep(10);
9819 }
9820
9821 tg3_disable_ints(tp);
9822
Matt Carlson4f125f42009-09-01 12:55:02 +00009823 free_irq(tnapi->irq_vec, tnapi);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009824
Matt Carlson4f125f42009-09-01 12:55:02 +00009825 err = tg3_request_irq(tp, 0);
Michael Chan79381092005-04-21 17:13:59 -07009826
9827 if (err)
9828 return err;
9829
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009830 if (intr_ok) {
9831 /* Reenable MSI one shot mode. */
Matt Carlson5b39de92011-08-31 11:44:50 +00009832 if (tg3_flag(tp, 57765_PLUS) && tg3_flag(tp, 1SHOT_MSI)) {
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009833 val = tr32(MSGINT_MODE) & ~MSGINT_MODE_ONE_SHOT_DISABLE;
9834 tw32(MSGINT_MODE, val);
9835 }
Michael Chan79381092005-04-21 17:13:59 -07009836 return 0;
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009837 }
Michael Chan79381092005-04-21 17:13:59 -07009838
9839 return -EIO;
9840}
9841
9842/* Returns 0 if MSI test succeeds or MSI test fails and INTx mode is
9843 * successfully restored
9844 */
9845static int tg3_test_msi(struct tg3 *tp)
9846{
Michael Chan79381092005-04-21 17:13:59 -07009847 int err;
9848 u16 pci_cmd;
9849
Joe Perches63c3a662011-04-26 08:12:10 +00009850 if (!tg3_flag(tp, USING_MSI))
Michael Chan79381092005-04-21 17:13:59 -07009851 return 0;
9852
9853 /* Turn off SERR reporting in case MSI terminates with Master
9854 * Abort.
9855 */
9856 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
9857 pci_write_config_word(tp->pdev, PCI_COMMAND,
9858 pci_cmd & ~PCI_COMMAND_SERR);
9859
9860 err = tg3_test_interrupt(tp);
9861
9862 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
9863
9864 if (!err)
9865 return 0;
9866
9867 /* other failures */
9868 if (err != -EIO)
9869 return err;
9870
9871 /* MSI test failed, go back to INTx mode */
Matt Carlson5129c3a2010-04-05 10:19:23 +00009872 netdev_warn(tp->dev, "No interrupt was generated using MSI. Switching "
9873 "to INTx mode. Please report this failure to the PCI "
9874 "maintainer and include system chipset information\n");
Michael Chan79381092005-04-21 17:13:59 -07009875
Matt Carlson4f125f42009-09-01 12:55:02 +00009876 free_irq(tp->napi[0].irq_vec, &tp->napi[0]);
Matt Carlson09943a12009-08-28 14:01:57 +00009877
Michael Chan79381092005-04-21 17:13:59 -07009878 pci_disable_msi(tp->pdev);
9879
Joe Perches63c3a662011-04-26 08:12:10 +00009880 tg3_flag_clear(tp, USING_MSI);
Andre Detschdc8bf1b2010-04-26 07:27:07 +00009881 tp->napi[0].irq_vec = tp->pdev->irq;
Michael Chan79381092005-04-21 17:13:59 -07009882
Matt Carlson4f125f42009-09-01 12:55:02 +00009883 err = tg3_request_irq(tp, 0);
Michael Chan79381092005-04-21 17:13:59 -07009884 if (err)
9885 return err;
9886
9887 /* Need to reset the chip because the MSI cycle may have terminated
9888 * with Master Abort.
9889 */
David S. Millerf47c11e2005-06-24 20:18:35 -07009890 tg3_full_lock(tp, 1);
Michael Chan79381092005-04-21 17:13:59 -07009891
Michael Chan944d9802005-05-29 14:57:48 -07009892 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07009893 err = tg3_init_hw(tp, 1);
Michael Chan79381092005-04-21 17:13:59 -07009894
David S. Millerf47c11e2005-06-24 20:18:35 -07009895 tg3_full_unlock(tp);
Michael Chan79381092005-04-21 17:13:59 -07009896
9897 if (err)
Matt Carlson4f125f42009-09-01 12:55:02 +00009898 free_irq(tp->napi[0].irq_vec, &tp->napi[0]);
Michael Chan79381092005-04-21 17:13:59 -07009899
9900 return err;
9901}
9902
Matt Carlson9e9fd122009-01-19 16:57:45 -08009903static int tg3_request_firmware(struct tg3 *tp)
9904{
9905 const __be32 *fw_data;
9906
9907 if (request_firmware(&tp->fw, tp->fw_needed, &tp->pdev->dev)) {
Joe Perches05dbe002010-02-17 19:44:19 +00009908 netdev_err(tp->dev, "Failed to load firmware \"%s\"\n",
9909 tp->fw_needed);
Matt Carlson9e9fd122009-01-19 16:57:45 -08009910 return -ENOENT;
9911 }
9912
9913 fw_data = (void *)tp->fw->data;
9914
9915 /* Firmware blob starts with version numbers, followed by
9916 * start address and _full_ length including BSS sections
9917 * (which must be longer than the actual data, of course
9918 */
9919
9920 tp->fw_len = be32_to_cpu(fw_data[2]); /* includes bss */
9921 if (tp->fw_len < (tp->fw->size - 12)) {
Joe Perches05dbe002010-02-17 19:44:19 +00009922 netdev_err(tp->dev, "bogus length %d in \"%s\"\n",
9923 tp->fw_len, tp->fw_needed);
Matt Carlson9e9fd122009-01-19 16:57:45 -08009924 release_firmware(tp->fw);
9925 tp->fw = NULL;
9926 return -EINVAL;
9927 }
9928
9929 /* We no longer need firmware; we have it. */
9930 tp->fw_needed = NULL;
9931 return 0;
9932}
9933
Matt Carlson679563f2009-09-01 12:55:46 +00009934static bool tg3_enable_msix(struct tg3 *tp)
9935{
Matt Carlsonc3b50032012-01-17 15:27:23 +00009936 int i, rc;
Matt Carlson679563f2009-09-01 12:55:46 +00009937 struct msix_entry msix_ent[tp->irq_max];
9938
Matt Carlsonc3b50032012-01-17 15:27:23 +00009939 tp->irq_cnt = num_online_cpus();
9940 if (tp->irq_cnt > 1) {
9941 /* We want as many rx rings enabled as there are cpus.
9942 * In multiqueue MSI-X mode, the first MSI-X vector
9943 * only deals with link interrupts, etc, so we add
9944 * one to the number of vectors we are requesting.
9945 */
9946 tp->irq_cnt = min_t(unsigned, tp->irq_cnt + 1, tp->irq_max);
9947 }
Matt Carlson679563f2009-09-01 12:55:46 +00009948
9949 for (i = 0; i < tp->irq_max; i++) {
9950 msix_ent[i].entry = i;
9951 msix_ent[i].vector = 0;
9952 }
9953
9954 rc = pci_enable_msix(tp->pdev, msix_ent, tp->irq_cnt);
Matt Carlson2430b032010-06-05 17:24:34 +00009955 if (rc < 0) {
9956 return false;
9957 } else if (rc != 0) {
Matt Carlson679563f2009-09-01 12:55:46 +00009958 if (pci_enable_msix(tp->pdev, msix_ent, rc))
9959 return false;
Joe Perches05dbe002010-02-17 19:44:19 +00009960 netdev_notice(tp->dev, "Requested %d MSI-X vectors, received %d\n",
9961 tp->irq_cnt, rc);
Matt Carlson679563f2009-09-01 12:55:46 +00009962 tp->irq_cnt = rc;
9963 }
9964
9965 for (i = 0; i < tp->irq_max; i++)
9966 tp->napi[i].irq_vec = msix_ent[i].vector;
9967
Ben Hutchings2ddaad32010-09-27 22:11:51 -07009968 netif_set_real_num_tx_queues(tp->dev, 1);
9969 rc = tp->irq_cnt > 1 ? tp->irq_cnt - 1 : 1;
9970 if (netif_set_real_num_rx_queues(tp->dev, rc)) {
9971 pci_disable_msix(tp->pdev);
9972 return false;
9973 }
Matt Carlsonb92b9042010-11-24 08:31:51 +00009974
9975 if (tp->irq_cnt > 1) {
Joe Perches63c3a662011-04-26 08:12:10 +00009976 tg3_flag_set(tp, ENABLE_RSS);
Matt Carlsond78b59f2011-04-05 14:22:46 +00009977
9978 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
9979 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
Joe Perches63c3a662011-04-26 08:12:10 +00009980 tg3_flag_set(tp, ENABLE_TSS);
Matt Carlsonb92b9042010-11-24 08:31:51 +00009981 netif_set_real_num_tx_queues(tp->dev, tp->irq_cnt - 1);
9982 }
9983 }
Matt Carlson2430b032010-06-05 17:24:34 +00009984
Matt Carlson679563f2009-09-01 12:55:46 +00009985 return true;
9986}
9987
Matt Carlson07b01732009-08-28 14:01:15 +00009988static void tg3_ints_init(struct tg3 *tp)
9989{
Joe Perches63c3a662011-04-26 08:12:10 +00009990 if ((tg3_flag(tp, SUPPORT_MSI) || tg3_flag(tp, SUPPORT_MSIX)) &&
9991 !tg3_flag(tp, TAGGED_STATUS)) {
Matt Carlson07b01732009-08-28 14:01:15 +00009992 /* All MSI supporting chips should support tagged
9993 * status. Assert that this is the case.
9994 */
Matt Carlson5129c3a2010-04-05 10:19:23 +00009995 netdev_warn(tp->dev,
9996 "MSI without TAGGED_STATUS? Not using MSI\n");
Matt Carlson679563f2009-09-01 12:55:46 +00009997 goto defcfg;
Matt Carlson07b01732009-08-28 14:01:15 +00009998 }
Matt Carlson4f125f42009-09-01 12:55:02 +00009999
Joe Perches63c3a662011-04-26 08:12:10 +000010000 if (tg3_flag(tp, SUPPORT_MSIX) && tg3_enable_msix(tp))
10001 tg3_flag_set(tp, USING_MSIX);
10002 else if (tg3_flag(tp, SUPPORT_MSI) && pci_enable_msi(tp->pdev) == 0)
10003 tg3_flag_set(tp, USING_MSI);
Matt Carlson679563f2009-09-01 12:55:46 +000010004
Joe Perches63c3a662011-04-26 08:12:10 +000010005 if (tg3_flag(tp, USING_MSI) || tg3_flag(tp, USING_MSIX)) {
Matt Carlson679563f2009-09-01 12:55:46 +000010006 u32 msi_mode = tr32(MSGINT_MODE);
Joe Perches63c3a662011-04-26 08:12:10 +000010007 if (tg3_flag(tp, USING_MSIX) && tp->irq_cnt > 1)
Matt Carlsonbaf8a942009-09-01 13:13:00 +000010008 msi_mode |= MSGINT_MODE_MULTIVEC_EN;
Matt Carlson5b39de92011-08-31 11:44:50 +000010009 if (!tg3_flag(tp, 1SHOT_MSI))
10010 msi_mode |= MSGINT_MODE_ONE_SHOT_DISABLE;
Matt Carlson679563f2009-09-01 12:55:46 +000010011 tw32(MSGINT_MODE, msi_mode | MSGINT_MODE_ENABLE);
10012 }
10013defcfg:
Joe Perches63c3a662011-04-26 08:12:10 +000010014 if (!tg3_flag(tp, USING_MSIX)) {
Matt Carlson679563f2009-09-01 12:55:46 +000010015 tp->irq_cnt = 1;
10016 tp->napi[0].irq_vec = tp->pdev->irq;
Ben Hutchings2ddaad32010-09-27 22:11:51 -070010017 netif_set_real_num_tx_queues(tp->dev, 1);
Matt Carlson85407882010-10-06 13:40:58 -070010018 netif_set_real_num_rx_queues(tp->dev, 1);
Matt Carlson679563f2009-09-01 12:55:46 +000010019 }
Matt Carlson07b01732009-08-28 14:01:15 +000010020}
10021
10022static void tg3_ints_fini(struct tg3 *tp)
10023{
Joe Perches63c3a662011-04-26 08:12:10 +000010024 if (tg3_flag(tp, USING_MSIX))
Matt Carlson679563f2009-09-01 12:55:46 +000010025 pci_disable_msix(tp->pdev);
Joe Perches63c3a662011-04-26 08:12:10 +000010026 else if (tg3_flag(tp, USING_MSI))
Matt Carlson679563f2009-09-01 12:55:46 +000010027 pci_disable_msi(tp->pdev);
Joe Perches63c3a662011-04-26 08:12:10 +000010028 tg3_flag_clear(tp, USING_MSI);
10029 tg3_flag_clear(tp, USING_MSIX);
10030 tg3_flag_clear(tp, ENABLE_RSS);
10031 tg3_flag_clear(tp, ENABLE_TSS);
Matt Carlson07b01732009-08-28 14:01:15 +000010032}
10033
Linus Torvalds1da177e2005-04-16 15:20:36 -070010034static int tg3_open(struct net_device *dev)
10035{
10036 struct tg3 *tp = netdev_priv(dev);
Matt Carlson4f125f42009-09-01 12:55:02 +000010037 int i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010038
Matt Carlson9e9fd122009-01-19 16:57:45 -080010039 if (tp->fw_needed) {
10040 err = tg3_request_firmware(tp);
10041 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0) {
10042 if (err)
10043 return err;
10044 } else if (err) {
Joe Perches05dbe002010-02-17 19:44:19 +000010045 netdev_warn(tp->dev, "TSO capability disabled\n");
Joe Perches63c3a662011-04-26 08:12:10 +000010046 tg3_flag_clear(tp, TSO_CAPABLE);
10047 } else if (!tg3_flag(tp, TSO_CAPABLE)) {
Joe Perches05dbe002010-02-17 19:44:19 +000010048 netdev_notice(tp->dev, "TSO capability restored\n");
Joe Perches63c3a662011-04-26 08:12:10 +000010049 tg3_flag_set(tp, TSO_CAPABLE);
Matt Carlson9e9fd122009-01-19 16:57:45 -080010050 }
10051 }
10052
Michael Chanc49a1562006-12-17 17:07:29 -080010053 netif_carrier_off(tp->dev);
10054
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000010055 err = tg3_power_up(tp);
Matt Carlson2f751b62008-08-04 23:17:34 -070010056 if (err)
Michael Chanbc1c7562006-03-20 17:48:03 -080010057 return err;
Matt Carlson2f751b62008-08-04 23:17:34 -070010058
10059 tg3_full_lock(tp, 0);
Michael Chanbc1c7562006-03-20 17:48:03 -080010060
Linus Torvalds1da177e2005-04-16 15:20:36 -070010061 tg3_disable_ints(tp);
Joe Perches63c3a662011-04-26 08:12:10 +000010062 tg3_flag_clear(tp, INIT_COMPLETE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010063
David S. Millerf47c11e2005-06-24 20:18:35 -070010064 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010065
Matt Carlson679563f2009-09-01 12:55:46 +000010066 /*
10067 * Setup interrupts first so we know how
10068 * many NAPI resources to allocate
10069 */
10070 tg3_ints_init(tp);
10071
Matt Carlson90415472011-12-16 13:33:23 +000010072 tg3_rss_check_indir_tbl(tp);
Matt Carlsonbcebcc42011-12-14 11:10:01 +000010073
Linus Torvalds1da177e2005-04-16 15:20:36 -070010074 /* The placement of this call is tied
10075 * to the setup and use of Host TX descriptors.
10076 */
10077 err = tg3_alloc_consistent(tp);
10078 if (err)
Matt Carlson679563f2009-09-01 12:55:46 +000010079 goto err_out1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010080
Matt Carlson66cfd1b2010-09-30 10:34:30 +000010081 tg3_napi_init(tp);
10082
Matt Carlsonfed97812009-09-01 13:10:19 +000010083 tg3_napi_enable(tp);
Stephen Hemmingerbea33482007-10-03 16:41:36 -070010084
Matt Carlson4f125f42009-09-01 12:55:02 +000010085 for (i = 0; i < tp->irq_cnt; i++) {
10086 struct tg3_napi *tnapi = &tp->napi[i];
10087 err = tg3_request_irq(tp, i);
10088 if (err) {
Matt Carlson5bc09182011-11-04 09:15:01 +000010089 for (i--; i >= 0; i--) {
10090 tnapi = &tp->napi[i];
Matt Carlson4f125f42009-09-01 12:55:02 +000010091 free_irq(tnapi->irq_vec, tnapi);
Matt Carlson5bc09182011-11-04 09:15:01 +000010092 }
10093 goto err_out2;
Matt Carlson4f125f42009-09-01 12:55:02 +000010094 }
10095 }
Matt Carlson07b01732009-08-28 14:01:15 +000010096
David S. Millerf47c11e2005-06-24 20:18:35 -070010097 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010098
Gary Zambrano8e7a22e2006-04-29 18:59:13 -070010099 err = tg3_init_hw(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010100 if (err) {
Michael Chan944d9802005-05-29 14:57:48 -070010101 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010102 tg3_free_rings(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010103 }
10104
David S. Millerf47c11e2005-06-24 20:18:35 -070010105 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010106
Matt Carlson07b01732009-08-28 14:01:15 +000010107 if (err)
Matt Carlson679563f2009-09-01 12:55:46 +000010108 goto err_out3;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010109
Joe Perches63c3a662011-04-26 08:12:10 +000010110 if (tg3_flag(tp, USING_MSI)) {
Michael Chan79381092005-04-21 17:13:59 -070010111 err = tg3_test_msi(tp);
David S. Millerfac9b832005-05-18 22:46:34 -070010112
Michael Chan79381092005-04-21 17:13:59 -070010113 if (err) {
David S. Millerf47c11e2005-06-24 20:18:35 -070010114 tg3_full_lock(tp, 0);
Michael Chan944d9802005-05-29 14:57:48 -070010115 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Michael Chan79381092005-04-21 17:13:59 -070010116 tg3_free_rings(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -070010117 tg3_full_unlock(tp);
Michael Chan79381092005-04-21 17:13:59 -070010118
Matt Carlson679563f2009-09-01 12:55:46 +000010119 goto err_out2;
Michael Chan79381092005-04-21 17:13:59 -070010120 }
Michael Chanfcfa0a32006-03-20 22:28:41 -080010121
Joe Perches63c3a662011-04-26 08:12:10 +000010122 if (!tg3_flag(tp, 57765_PLUS) && tg3_flag(tp, USING_MSI)) {
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000010123 u32 val = tr32(PCIE_TRANSACTION_CFG);
Michael Chanfcfa0a32006-03-20 22:28:41 -080010124
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000010125 tw32(PCIE_TRANSACTION_CFG,
10126 val | PCIE_TRANS_CFG_1SHOT_MSI);
Michael Chanfcfa0a32006-03-20 22:28:41 -080010127 }
Michael Chan79381092005-04-21 17:13:59 -070010128 }
10129
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010130 tg3_phy_start(tp);
10131
David S. Millerf47c11e2005-06-24 20:18:35 -070010132 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010133
Matt Carlson21f76382012-02-22 12:35:21 +000010134 tg3_timer_start(tp);
Joe Perches63c3a662011-04-26 08:12:10 +000010135 tg3_flag_set(tp, INIT_COMPLETE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010136 tg3_enable_ints(tp);
10137
David S. Millerf47c11e2005-06-24 20:18:35 -070010138 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010139
Matt Carlsonfe5f5782009-09-01 13:09:39 +000010140 netif_tx_start_all_queues(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010141
Mahesh Bandewar06c03c02011-05-08 06:51:48 +000010142 /*
10143 * Reset loopback feature if it was turned on while the device was down
10144 * make sure that it's installed properly now.
10145 */
10146 if (dev->features & NETIF_F_LOOPBACK)
10147 tg3_set_loopback(dev, dev->features);
10148
Linus Torvalds1da177e2005-04-16 15:20:36 -070010149 return 0;
Matt Carlson07b01732009-08-28 14:01:15 +000010150
Matt Carlson679563f2009-09-01 12:55:46 +000010151err_out3:
Matt Carlson4f125f42009-09-01 12:55:02 +000010152 for (i = tp->irq_cnt - 1; i >= 0; i--) {
10153 struct tg3_napi *tnapi = &tp->napi[i];
10154 free_irq(tnapi->irq_vec, tnapi);
10155 }
Matt Carlson07b01732009-08-28 14:01:15 +000010156
Matt Carlson679563f2009-09-01 12:55:46 +000010157err_out2:
Matt Carlsonfed97812009-09-01 13:10:19 +000010158 tg3_napi_disable(tp);
Matt Carlson66cfd1b2010-09-30 10:34:30 +000010159 tg3_napi_fini(tp);
Matt Carlson07b01732009-08-28 14:01:15 +000010160 tg3_free_consistent(tp);
Matt Carlson679563f2009-09-01 12:55:46 +000010161
10162err_out1:
10163 tg3_ints_fini(tp);
Matt Carlsoncd0d7222011-07-13 09:27:33 +000010164 tg3_frob_aux_power(tp, false);
10165 pci_set_power_state(tp->pdev, PCI_D3hot);
Matt Carlson07b01732009-08-28 14:01:15 +000010166 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010167}
10168
Linus Torvalds1da177e2005-04-16 15:20:36 -070010169static int tg3_close(struct net_device *dev)
10170{
Matt Carlson4f125f42009-09-01 12:55:02 +000010171 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010172 struct tg3 *tp = netdev_priv(dev);
10173
Matt Carlsonfed97812009-09-01 13:10:19 +000010174 tg3_napi_disable(tp);
Matt Carlsondb219972011-11-04 09:15:03 +000010175 tg3_reset_task_cancel(tp);
Michael Chan7faa0062006-02-02 17:29:28 -080010176
Matt Carlsonfe5f5782009-09-01 13:09:39 +000010177 netif_tx_stop_all_queues(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010178
Matt Carlson21f76382012-02-22 12:35:21 +000010179 tg3_timer_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010180
Matt Carlson24bb4fb2009-10-05 17:55:29 +000010181 tg3_phy_stop(tp);
10182
David S. Millerf47c11e2005-06-24 20:18:35 -070010183 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010184
10185 tg3_disable_ints(tp);
10186
Michael Chan944d9802005-05-29 14:57:48 -070010187 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010188 tg3_free_rings(tp);
Joe Perches63c3a662011-04-26 08:12:10 +000010189 tg3_flag_clear(tp, INIT_COMPLETE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010190
David S. Millerf47c11e2005-06-24 20:18:35 -070010191 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010192
Matt Carlson4f125f42009-09-01 12:55:02 +000010193 for (i = tp->irq_cnt - 1; i >= 0; i--) {
10194 struct tg3_napi *tnapi = &tp->napi[i];
10195 free_irq(tnapi->irq_vec, tnapi);
10196 }
Matt Carlson07b01732009-08-28 14:01:15 +000010197
10198 tg3_ints_fini(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010199
Matt Carlson92feeab2011-12-08 14:40:14 +000010200 /* Clear stats across close / open calls */
10201 memset(&tp->net_stats_prev, 0, sizeof(tp->net_stats_prev));
10202 memset(&tp->estats_prev, 0, sizeof(tp->estats_prev));
Linus Torvalds1da177e2005-04-16 15:20:36 -070010203
Matt Carlson66cfd1b2010-09-30 10:34:30 +000010204 tg3_napi_fini(tp);
10205
Linus Torvalds1da177e2005-04-16 15:20:36 -070010206 tg3_free_consistent(tp);
10207
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000010208 tg3_power_down(tp);
Michael Chanbc1c7562006-03-20 17:48:03 -080010209
10210 netif_carrier_off(tp->dev);
10211
Linus Torvalds1da177e2005-04-16 15:20:36 -070010212 return 0;
10213}
10214
Eric Dumazet511d2222010-07-07 20:44:24 +000010215static inline u64 get_stat64(tg3_stat64_t *val)
Stefan Buehler816f8b82008-08-15 14:10:54 -070010216{
10217 return ((u64)val->high << 32) | ((u64)val->low);
10218}
10219
Matt Carlson65ec6982012-02-28 23:33:37 +000010220static u64 tg3_calc_crc_errors(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010221{
10222 struct tg3_hw_stats *hw_stats = tp->hw_stats;
10223
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010224 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070010225 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
10226 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010227 u32 val;
10228
Michael Chan569a5df2007-02-13 12:18:15 -080010229 if (!tg3_readphy(tp, MII_TG3_TEST1, &val)) {
10230 tg3_writephy(tp, MII_TG3_TEST1,
10231 val | MII_TG3_TEST1_CRC_EN);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +000010232 tg3_readphy(tp, MII_TG3_RXR_COUNTERS, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010233 } else
10234 val = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010235
10236 tp->phy_crc_errors += val;
10237
10238 return tp->phy_crc_errors;
10239 }
10240
10241 return get_stat64(&hw_stats->rx_fcs_errors);
10242}
10243
10244#define ESTAT_ADD(member) \
10245 estats->member = old_estats->member + \
Eric Dumazet511d2222010-07-07 20:44:24 +000010246 get_stat64(&hw_stats->member)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010247
Matt Carlson65ec6982012-02-28 23:33:37 +000010248static void tg3_get_estats(struct tg3 *tp, struct tg3_ethtool_stats *estats)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010249{
Linus Torvalds1da177e2005-04-16 15:20:36 -070010250 struct tg3_ethtool_stats *old_estats = &tp->estats_prev;
10251 struct tg3_hw_stats *hw_stats = tp->hw_stats;
10252
Linus Torvalds1da177e2005-04-16 15:20:36 -070010253 ESTAT_ADD(rx_octets);
10254 ESTAT_ADD(rx_fragments);
10255 ESTAT_ADD(rx_ucast_packets);
10256 ESTAT_ADD(rx_mcast_packets);
10257 ESTAT_ADD(rx_bcast_packets);
10258 ESTAT_ADD(rx_fcs_errors);
10259 ESTAT_ADD(rx_align_errors);
10260 ESTAT_ADD(rx_xon_pause_rcvd);
10261 ESTAT_ADD(rx_xoff_pause_rcvd);
10262 ESTAT_ADD(rx_mac_ctrl_rcvd);
10263 ESTAT_ADD(rx_xoff_entered);
10264 ESTAT_ADD(rx_frame_too_long_errors);
10265 ESTAT_ADD(rx_jabbers);
10266 ESTAT_ADD(rx_undersize_packets);
10267 ESTAT_ADD(rx_in_length_errors);
10268 ESTAT_ADD(rx_out_length_errors);
10269 ESTAT_ADD(rx_64_or_less_octet_packets);
10270 ESTAT_ADD(rx_65_to_127_octet_packets);
10271 ESTAT_ADD(rx_128_to_255_octet_packets);
10272 ESTAT_ADD(rx_256_to_511_octet_packets);
10273 ESTAT_ADD(rx_512_to_1023_octet_packets);
10274 ESTAT_ADD(rx_1024_to_1522_octet_packets);
10275 ESTAT_ADD(rx_1523_to_2047_octet_packets);
10276 ESTAT_ADD(rx_2048_to_4095_octet_packets);
10277 ESTAT_ADD(rx_4096_to_8191_octet_packets);
10278 ESTAT_ADD(rx_8192_to_9022_octet_packets);
10279
10280 ESTAT_ADD(tx_octets);
10281 ESTAT_ADD(tx_collisions);
10282 ESTAT_ADD(tx_xon_sent);
10283 ESTAT_ADD(tx_xoff_sent);
10284 ESTAT_ADD(tx_flow_control);
10285 ESTAT_ADD(tx_mac_errors);
10286 ESTAT_ADD(tx_single_collisions);
10287 ESTAT_ADD(tx_mult_collisions);
10288 ESTAT_ADD(tx_deferred);
10289 ESTAT_ADD(tx_excessive_collisions);
10290 ESTAT_ADD(tx_late_collisions);
10291 ESTAT_ADD(tx_collide_2times);
10292 ESTAT_ADD(tx_collide_3times);
10293 ESTAT_ADD(tx_collide_4times);
10294 ESTAT_ADD(tx_collide_5times);
10295 ESTAT_ADD(tx_collide_6times);
10296 ESTAT_ADD(tx_collide_7times);
10297 ESTAT_ADD(tx_collide_8times);
10298 ESTAT_ADD(tx_collide_9times);
10299 ESTAT_ADD(tx_collide_10times);
10300 ESTAT_ADD(tx_collide_11times);
10301 ESTAT_ADD(tx_collide_12times);
10302 ESTAT_ADD(tx_collide_13times);
10303 ESTAT_ADD(tx_collide_14times);
10304 ESTAT_ADD(tx_collide_15times);
10305 ESTAT_ADD(tx_ucast_packets);
10306 ESTAT_ADD(tx_mcast_packets);
10307 ESTAT_ADD(tx_bcast_packets);
10308 ESTAT_ADD(tx_carrier_sense_errors);
10309 ESTAT_ADD(tx_discards);
10310 ESTAT_ADD(tx_errors);
10311
10312 ESTAT_ADD(dma_writeq_full);
10313 ESTAT_ADD(dma_write_prioq_full);
10314 ESTAT_ADD(rxbds_empty);
10315 ESTAT_ADD(rx_discards);
10316 ESTAT_ADD(rx_errors);
10317 ESTAT_ADD(rx_threshold_hit);
10318
10319 ESTAT_ADD(dma_readq_full);
10320 ESTAT_ADD(dma_read_prioq_full);
10321 ESTAT_ADD(tx_comp_queue_full);
10322
10323 ESTAT_ADD(ring_set_send_prod_index);
10324 ESTAT_ADD(ring_status_update);
10325 ESTAT_ADD(nic_irqs);
10326 ESTAT_ADD(nic_avoided_irqs);
10327 ESTAT_ADD(nic_tx_threshold_hit);
10328
Matt Carlson4452d092011-05-19 12:12:51 +000010329 ESTAT_ADD(mbuf_lwm_thresh_hit);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010330}
10331
Matt Carlson65ec6982012-02-28 23:33:37 +000010332static void tg3_get_nstats(struct tg3 *tp, struct rtnl_link_stats64 *stats)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010333{
Eric Dumazet511d2222010-07-07 20:44:24 +000010334 struct rtnl_link_stats64 *old_stats = &tp->net_stats_prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010335 struct tg3_hw_stats *hw_stats = tp->hw_stats;
10336
Linus Torvalds1da177e2005-04-16 15:20:36 -070010337 stats->rx_packets = old_stats->rx_packets +
10338 get_stat64(&hw_stats->rx_ucast_packets) +
10339 get_stat64(&hw_stats->rx_mcast_packets) +
10340 get_stat64(&hw_stats->rx_bcast_packets);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010341
Linus Torvalds1da177e2005-04-16 15:20:36 -070010342 stats->tx_packets = old_stats->tx_packets +
10343 get_stat64(&hw_stats->tx_ucast_packets) +
10344 get_stat64(&hw_stats->tx_mcast_packets) +
10345 get_stat64(&hw_stats->tx_bcast_packets);
10346
10347 stats->rx_bytes = old_stats->rx_bytes +
10348 get_stat64(&hw_stats->rx_octets);
10349 stats->tx_bytes = old_stats->tx_bytes +
10350 get_stat64(&hw_stats->tx_octets);
10351
10352 stats->rx_errors = old_stats->rx_errors +
John W. Linville4f63b872005-09-12 14:43:18 -070010353 get_stat64(&hw_stats->rx_errors);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010354 stats->tx_errors = old_stats->tx_errors +
10355 get_stat64(&hw_stats->tx_errors) +
10356 get_stat64(&hw_stats->tx_mac_errors) +
10357 get_stat64(&hw_stats->tx_carrier_sense_errors) +
10358 get_stat64(&hw_stats->tx_discards);
10359
10360 stats->multicast = old_stats->multicast +
10361 get_stat64(&hw_stats->rx_mcast_packets);
10362 stats->collisions = old_stats->collisions +
10363 get_stat64(&hw_stats->tx_collisions);
10364
10365 stats->rx_length_errors = old_stats->rx_length_errors +
10366 get_stat64(&hw_stats->rx_frame_too_long_errors) +
10367 get_stat64(&hw_stats->rx_undersize_packets);
10368
10369 stats->rx_over_errors = old_stats->rx_over_errors +
10370 get_stat64(&hw_stats->rxbds_empty);
10371 stats->rx_frame_errors = old_stats->rx_frame_errors +
10372 get_stat64(&hw_stats->rx_align_errors);
10373 stats->tx_aborted_errors = old_stats->tx_aborted_errors +
10374 get_stat64(&hw_stats->tx_discards);
10375 stats->tx_carrier_errors = old_stats->tx_carrier_errors +
10376 get_stat64(&hw_stats->tx_carrier_sense_errors);
10377
10378 stats->rx_crc_errors = old_stats->rx_crc_errors +
Matt Carlson65ec6982012-02-28 23:33:37 +000010379 tg3_calc_crc_errors(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010380
John W. Linville4f63b872005-09-12 14:43:18 -070010381 stats->rx_missed_errors = old_stats->rx_missed_errors +
10382 get_stat64(&hw_stats->rx_discards);
10383
Eric Dumazetb0057c52010-10-10 19:55:52 +000010384 stats->rx_dropped = tp->rx_dropped;
Eric Dumazet48855432011-10-24 07:53:03 +000010385 stats->tx_dropped = tp->tx_dropped;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010386}
10387
Linus Torvalds1da177e2005-04-16 15:20:36 -070010388static int tg3_get_regs_len(struct net_device *dev)
10389{
Matt Carlson97bd8e42011-04-13 11:05:04 +000010390 return TG3_REG_BLK_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010391}
10392
10393static void tg3_get_regs(struct net_device *dev,
10394 struct ethtool_regs *regs, void *_p)
10395{
Linus Torvalds1da177e2005-04-16 15:20:36 -070010396 struct tg3 *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010397
10398 regs->version = 0;
10399
Matt Carlson97bd8e42011-04-13 11:05:04 +000010400 memset(_p, 0, TG3_REG_BLK_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010401
Matt Carlson80096062010-08-02 11:26:06 +000010402 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Michael Chanbc1c7562006-03-20 17:48:03 -080010403 return;
10404
David S. Millerf47c11e2005-06-24 20:18:35 -070010405 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010406
Matt Carlson97bd8e42011-04-13 11:05:04 +000010407 tg3_dump_legacy_regs(tp, (u32 *)_p);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010408
David S. Millerf47c11e2005-06-24 20:18:35 -070010409 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010410}
10411
10412static int tg3_get_eeprom_len(struct net_device *dev)
10413{
10414 struct tg3 *tp = netdev_priv(dev);
10415
10416 return tp->nvram_size;
10417}
10418
Linus Torvalds1da177e2005-04-16 15:20:36 -070010419static int tg3_get_eeprom(struct net_device *dev, struct ethtool_eeprom *eeprom, u8 *data)
10420{
10421 struct tg3 *tp = netdev_priv(dev);
10422 int ret;
10423 u8 *pd;
Al Virob9fc7dc2007-12-17 22:59:57 -080010424 u32 i, offset, len, b_offset, b_count;
Matt Carlsona9dc5292009-02-25 14:25:30 +000010425 __be32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010426
Joe Perches63c3a662011-04-26 08:12:10 +000010427 if (tg3_flag(tp, NO_NVRAM))
Matt Carlsondf259d82009-04-20 06:57:14 +000010428 return -EINVAL;
10429
Matt Carlson80096062010-08-02 11:26:06 +000010430 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Michael Chanbc1c7562006-03-20 17:48:03 -080010431 return -EAGAIN;
10432
Linus Torvalds1da177e2005-04-16 15:20:36 -070010433 offset = eeprom->offset;
10434 len = eeprom->len;
10435 eeprom->len = 0;
10436
10437 eeprom->magic = TG3_EEPROM_MAGIC;
10438
10439 if (offset & 3) {
10440 /* adjustments to start on required 4 byte boundary */
10441 b_offset = offset & 3;
10442 b_count = 4 - b_offset;
10443 if (b_count > len) {
10444 /* i.e. offset=1 len=2 */
10445 b_count = len;
10446 }
Matt Carlsona9dc5292009-02-25 14:25:30 +000010447 ret = tg3_nvram_read_be32(tp, offset-b_offset, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010448 if (ret)
10449 return ret;
Matt Carlsonbe98da62010-07-11 09:31:46 +000010450 memcpy(data, ((char *)&val) + b_offset, b_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010451 len -= b_count;
10452 offset += b_count;
Matt Carlsonc6cdf432010-04-05 10:19:26 +000010453 eeprom->len += b_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010454 }
10455
Lucas De Marchi25985ed2011-03-30 22:57:33 -030010456 /* read bytes up to the last 4 byte boundary */
Linus Torvalds1da177e2005-04-16 15:20:36 -070010457 pd = &data[eeprom->len];
10458 for (i = 0; i < (len - (len & 3)); i += 4) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000010459 ret = tg3_nvram_read_be32(tp, offset + i, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010460 if (ret) {
10461 eeprom->len += i;
10462 return ret;
10463 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010464 memcpy(pd + i, &val, 4);
10465 }
10466 eeprom->len += i;
10467
10468 if (len & 3) {
10469 /* read last bytes not ending on 4 byte boundary */
10470 pd = &data[eeprom->len];
10471 b_count = len & 3;
10472 b_offset = offset + len - b_count;
Matt Carlsona9dc5292009-02-25 14:25:30 +000010473 ret = tg3_nvram_read_be32(tp, b_offset, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010474 if (ret)
10475 return ret;
Al Virob9fc7dc2007-12-17 22:59:57 -080010476 memcpy(pd, &val, b_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010477 eeprom->len += b_count;
10478 }
10479 return 0;
10480}
10481
Linus Torvalds1da177e2005-04-16 15:20:36 -070010482static int tg3_set_eeprom(struct net_device *dev, struct ethtool_eeprom *eeprom, u8 *data)
10483{
10484 struct tg3 *tp = netdev_priv(dev);
10485 int ret;
Al Virob9fc7dc2007-12-17 22:59:57 -080010486 u32 offset, len, b_offset, odd_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010487 u8 *buf;
Matt Carlsona9dc5292009-02-25 14:25:30 +000010488 __be32 start, end;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010489
Matt Carlson80096062010-08-02 11:26:06 +000010490 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Michael Chanbc1c7562006-03-20 17:48:03 -080010491 return -EAGAIN;
10492
Joe Perches63c3a662011-04-26 08:12:10 +000010493 if (tg3_flag(tp, NO_NVRAM) ||
Matt Carlsondf259d82009-04-20 06:57:14 +000010494 eeprom->magic != TG3_EEPROM_MAGIC)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010495 return -EINVAL;
10496
10497 offset = eeprom->offset;
10498 len = eeprom->len;
10499
10500 if ((b_offset = (offset & 3))) {
10501 /* adjustments to start on required 4 byte boundary */
Matt Carlsona9dc5292009-02-25 14:25:30 +000010502 ret = tg3_nvram_read_be32(tp, offset-b_offset, &start);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010503 if (ret)
10504 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010505 len += b_offset;
10506 offset &= ~3;
Michael Chan1c8594b2005-04-21 17:12:46 -070010507 if (len < 4)
10508 len = 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010509 }
10510
10511 odd_len = 0;
Michael Chan1c8594b2005-04-21 17:12:46 -070010512 if (len & 3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010513 /* adjustments to end on required 4 byte boundary */
10514 odd_len = 1;
10515 len = (len + 3) & ~3;
Matt Carlsona9dc5292009-02-25 14:25:30 +000010516 ret = tg3_nvram_read_be32(tp, offset+len-4, &end);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010517 if (ret)
10518 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010519 }
10520
10521 buf = data;
10522 if (b_offset || odd_len) {
10523 buf = kmalloc(len, GFP_KERNEL);
Andy Gospodarekab0049b2007-09-06 20:42:14 +010010524 if (!buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010525 return -ENOMEM;
10526 if (b_offset)
10527 memcpy(buf, &start, 4);
10528 if (odd_len)
10529 memcpy(buf+len-4, &end, 4);
10530 memcpy(buf + b_offset, data, eeprom->len);
10531 }
10532
10533 ret = tg3_nvram_write_block(tp, offset, len, buf);
10534
10535 if (buf != data)
10536 kfree(buf);
10537
10538 return ret;
10539}
10540
10541static int tg3_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
10542{
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010543 struct tg3 *tp = netdev_priv(dev);
10544
Joe Perches63c3a662011-04-26 08:12:10 +000010545 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000010546 struct phy_device *phydev;
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010547 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010548 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000010549 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
10550 return phy_ethtool_gset(phydev, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010551 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010552
Linus Torvalds1da177e2005-04-16 15:20:36 -070010553 cmd->supported = (SUPPORTED_Autoneg);
10554
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010555 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010556 cmd->supported |= (SUPPORTED_1000baseT_Half |
10557 SUPPORTED_1000baseT_Full);
10558
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010559 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010560 cmd->supported |= (SUPPORTED_100baseT_Half |
10561 SUPPORTED_100baseT_Full |
10562 SUPPORTED_10baseT_Half |
10563 SUPPORTED_10baseT_Full |
Matt Carlson3bebab52007-11-12 21:22:40 -080010564 SUPPORTED_TP);
Karsten Keilef348142006-05-12 12:49:08 -070010565 cmd->port = PORT_TP;
10566 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010567 cmd->supported |= SUPPORTED_FIBRE;
Karsten Keilef348142006-05-12 12:49:08 -070010568 cmd->port = PORT_FIBRE;
10569 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010570
Linus Torvalds1da177e2005-04-16 15:20:36 -070010571 cmd->advertising = tp->link_config.advertising;
Matt Carlson5bb09772011-06-13 13:39:00 +000010572 if (tg3_flag(tp, PAUSE_AUTONEG)) {
10573 if (tp->link_config.flowctrl & FLOW_CTRL_RX) {
10574 if (tp->link_config.flowctrl & FLOW_CTRL_TX) {
10575 cmd->advertising |= ADVERTISED_Pause;
10576 } else {
10577 cmd->advertising |= ADVERTISED_Pause |
10578 ADVERTISED_Asym_Pause;
10579 }
10580 } else if (tp->link_config.flowctrl & FLOW_CTRL_TX) {
10581 cmd->advertising |= ADVERTISED_Asym_Pause;
10582 }
10583 }
Matt Carlson859edb22011-12-08 14:40:16 +000010584 if (netif_running(dev) && netif_carrier_ok(dev)) {
David Decotigny70739492011-04-27 18:32:40 +000010585 ethtool_cmd_speed_set(cmd, tp->link_config.active_speed);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010586 cmd->duplex = tp->link_config.active_duplex;
Matt Carlson859edb22011-12-08 14:40:16 +000010587 cmd->lp_advertising = tp->link_config.rmt_adv;
Matt Carlsone348c5e2011-11-21 15:01:20 +000010588 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES)) {
10589 if (tp->phy_flags & TG3_PHYFLG_MDIX_STATE)
10590 cmd->eth_tp_mdix = ETH_TP_MDI_X;
10591 else
10592 cmd->eth_tp_mdix = ETH_TP_MDI;
10593 }
Matt Carlson64c22182010-10-14 10:37:44 +000010594 } else {
Matt Carlsone7405222012-02-13 15:20:16 +000010595 ethtool_cmd_speed_set(cmd, SPEED_UNKNOWN);
10596 cmd->duplex = DUPLEX_UNKNOWN;
Matt Carlsone348c5e2011-11-21 15:01:20 +000010597 cmd->eth_tp_mdix = ETH_TP_MDI_INVALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010598 }
Matt Carlson882e9792009-09-01 13:21:36 +000010599 cmd->phy_address = tp->phy_addr;
Matt Carlson7e5856b2009-02-25 14:23:01 +000010600 cmd->transceiver = XCVR_INTERNAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010601 cmd->autoneg = tp->link_config.autoneg;
10602 cmd->maxtxpkt = 0;
10603 cmd->maxrxpkt = 0;
10604 return 0;
10605}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010606
Linus Torvalds1da177e2005-04-16 15:20:36 -070010607static int tg3_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
10608{
10609 struct tg3 *tp = netdev_priv(dev);
David Decotigny25db0332011-04-27 18:32:39 +000010610 u32 speed = ethtool_cmd_speed(cmd);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010611
Joe Perches63c3a662011-04-26 08:12:10 +000010612 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000010613 struct phy_device *phydev;
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010614 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010615 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000010616 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
10617 return phy_ethtool_sset(phydev, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010618 }
10619
Matt Carlson7e5856b2009-02-25 14:23:01 +000010620 if (cmd->autoneg != AUTONEG_ENABLE &&
10621 cmd->autoneg != AUTONEG_DISABLE)
Michael Chan37ff2382005-10-26 15:49:51 -070010622 return -EINVAL;
Matt Carlson7e5856b2009-02-25 14:23:01 +000010623
10624 if (cmd->autoneg == AUTONEG_DISABLE &&
10625 cmd->duplex != DUPLEX_FULL &&
10626 cmd->duplex != DUPLEX_HALF)
Michael Chan37ff2382005-10-26 15:49:51 -070010627 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010628
Matt Carlson7e5856b2009-02-25 14:23:01 +000010629 if (cmd->autoneg == AUTONEG_ENABLE) {
10630 u32 mask = ADVERTISED_Autoneg |
10631 ADVERTISED_Pause |
10632 ADVERTISED_Asym_Pause;
10633
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010634 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
Matt Carlson7e5856b2009-02-25 14:23:01 +000010635 mask |= ADVERTISED_1000baseT_Half |
10636 ADVERTISED_1000baseT_Full;
10637
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010638 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
Matt Carlson7e5856b2009-02-25 14:23:01 +000010639 mask |= ADVERTISED_100baseT_Half |
10640 ADVERTISED_100baseT_Full |
10641 ADVERTISED_10baseT_Half |
10642 ADVERTISED_10baseT_Full |
10643 ADVERTISED_TP;
10644 else
10645 mask |= ADVERTISED_FIBRE;
10646
10647 if (cmd->advertising & ~mask)
10648 return -EINVAL;
10649
10650 mask &= (ADVERTISED_1000baseT_Half |
10651 ADVERTISED_1000baseT_Full |
10652 ADVERTISED_100baseT_Half |
10653 ADVERTISED_100baseT_Full |
10654 ADVERTISED_10baseT_Half |
10655 ADVERTISED_10baseT_Full);
10656
10657 cmd->advertising &= mask;
10658 } else {
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010659 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES) {
David Decotigny25db0332011-04-27 18:32:39 +000010660 if (speed != SPEED_1000)
Matt Carlson7e5856b2009-02-25 14:23:01 +000010661 return -EINVAL;
10662
10663 if (cmd->duplex != DUPLEX_FULL)
10664 return -EINVAL;
10665 } else {
David Decotigny25db0332011-04-27 18:32:39 +000010666 if (speed != SPEED_100 &&
10667 speed != SPEED_10)
Matt Carlson7e5856b2009-02-25 14:23:01 +000010668 return -EINVAL;
10669 }
10670 }
10671
David S. Millerf47c11e2005-06-24 20:18:35 -070010672 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010673
10674 tp->link_config.autoneg = cmd->autoneg;
10675 if (cmd->autoneg == AUTONEG_ENABLE) {
Andy Gospodarek405d8e52007-10-08 01:08:47 -070010676 tp->link_config.advertising = (cmd->advertising |
10677 ADVERTISED_Autoneg);
Matt Carlsone7405222012-02-13 15:20:16 +000010678 tp->link_config.speed = SPEED_UNKNOWN;
10679 tp->link_config.duplex = DUPLEX_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010680 } else {
10681 tp->link_config.advertising = 0;
David Decotigny25db0332011-04-27 18:32:39 +000010682 tp->link_config.speed = speed;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010683 tp->link_config.duplex = cmd->duplex;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010684 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010685
Linus Torvalds1da177e2005-04-16 15:20:36 -070010686 if (netif_running(dev))
10687 tg3_setup_phy(tp, 1);
10688
David S. Millerf47c11e2005-06-24 20:18:35 -070010689 tg3_full_unlock(tp);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010690
Linus Torvalds1da177e2005-04-16 15:20:36 -070010691 return 0;
10692}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010693
Linus Torvalds1da177e2005-04-16 15:20:36 -070010694static void tg3_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
10695{
10696 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010697
Rick Jones68aad782011-11-07 13:29:27 +000010698 strlcpy(info->driver, DRV_MODULE_NAME, sizeof(info->driver));
10699 strlcpy(info->version, DRV_MODULE_VERSION, sizeof(info->version));
10700 strlcpy(info->fw_version, tp->fw_ver, sizeof(info->fw_version));
10701 strlcpy(info->bus_info, pci_name(tp->pdev), sizeof(info->bus_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -070010702}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010703
Linus Torvalds1da177e2005-04-16 15:20:36 -070010704static void tg3_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
10705{
10706 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010707
Joe Perches63c3a662011-04-26 08:12:10 +000010708 if (tg3_flag(tp, WOL_CAP) && device_can_wakeup(&tp->pdev->dev))
Gary Zambranoa85feb82007-05-05 11:52:19 -070010709 wol->supported = WAKE_MAGIC;
10710 else
10711 wol->supported = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010712 wol->wolopts = 0;
Joe Perches63c3a662011-04-26 08:12:10 +000010713 if (tg3_flag(tp, WOL_ENABLE) && device_can_wakeup(&tp->pdev->dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010714 wol->wolopts = WAKE_MAGIC;
10715 memset(&wol->sopass, 0, sizeof(wol->sopass));
10716}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010717
Linus Torvalds1da177e2005-04-16 15:20:36 -070010718static int tg3_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
10719{
10720 struct tg3 *tp = netdev_priv(dev);
Rafael J. Wysocki12dac072008-07-30 16:37:33 -070010721 struct device *dp = &tp->pdev->dev;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010722
Linus Torvalds1da177e2005-04-16 15:20:36 -070010723 if (wol->wolopts & ~WAKE_MAGIC)
10724 return -EINVAL;
10725 if ((wol->wolopts & WAKE_MAGIC) &&
Joe Perches63c3a662011-04-26 08:12:10 +000010726 !(tg3_flag(tp, WOL_CAP) && device_can_wakeup(dp)))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010727 return -EINVAL;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010728
Rafael J. Wysockif2dc0d12010-10-25 13:01:55 +000010729 device_set_wakeup_enable(dp, wol->wolopts & WAKE_MAGIC);
10730
David S. Millerf47c11e2005-06-24 20:18:35 -070010731 spin_lock_bh(&tp->lock);
Rafael J. Wysockif2dc0d12010-10-25 13:01:55 +000010732 if (device_may_wakeup(dp))
Joe Perches63c3a662011-04-26 08:12:10 +000010733 tg3_flag_set(tp, WOL_ENABLE);
Rafael J. Wysockif2dc0d12010-10-25 13:01:55 +000010734 else
Joe Perches63c3a662011-04-26 08:12:10 +000010735 tg3_flag_clear(tp, WOL_ENABLE);
David S. Millerf47c11e2005-06-24 20:18:35 -070010736 spin_unlock_bh(&tp->lock);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010737
Linus Torvalds1da177e2005-04-16 15:20:36 -070010738 return 0;
10739}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010740
Linus Torvalds1da177e2005-04-16 15:20:36 -070010741static u32 tg3_get_msglevel(struct net_device *dev)
10742{
10743 struct tg3 *tp = netdev_priv(dev);
10744 return tp->msg_enable;
10745}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010746
Linus Torvalds1da177e2005-04-16 15:20:36 -070010747static void tg3_set_msglevel(struct net_device *dev, u32 value)
10748{
10749 struct tg3 *tp = netdev_priv(dev);
10750 tp->msg_enable = value;
10751}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010752
Linus Torvalds1da177e2005-04-16 15:20:36 -070010753static int tg3_nway_reset(struct net_device *dev)
10754{
10755 struct tg3 *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010756 int r;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010757
Linus Torvalds1da177e2005-04-16 15:20:36 -070010758 if (!netif_running(dev))
10759 return -EAGAIN;
10760
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010761 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Michael Chanc94e3942005-09-27 12:12:42 -070010762 return -EINVAL;
10763
Joe Perches63c3a662011-04-26 08:12:10 +000010764 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010765 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010766 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000010767 r = phy_start_aneg(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010768 } else {
10769 u32 bmcr;
10770
10771 spin_lock_bh(&tp->lock);
10772 r = -EINVAL;
10773 tg3_readphy(tp, MII_BMCR, &bmcr);
10774 if (!tg3_readphy(tp, MII_BMCR, &bmcr) &&
10775 ((bmcr & BMCR_ANENABLE) ||
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010776 (tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT))) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010777 tg3_writephy(tp, MII_BMCR, bmcr | BMCR_ANRESTART |
10778 BMCR_ANENABLE);
10779 r = 0;
10780 }
10781 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010782 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010783
Linus Torvalds1da177e2005-04-16 15:20:36 -070010784 return r;
10785}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010786
Linus Torvalds1da177e2005-04-16 15:20:36 -070010787static void tg3_get_ringparam(struct net_device *dev, struct ethtool_ringparam *ering)
10788{
10789 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010790
Matt Carlson2c49a442010-09-30 10:34:35 +000010791 ering->rx_max_pending = tp->rx_std_ring_mask;
Joe Perches63c3a662011-04-26 08:12:10 +000010792 if (tg3_flag(tp, JUMBO_RING_ENABLE))
Matt Carlson2c49a442010-09-30 10:34:35 +000010793 ering->rx_jumbo_max_pending = tp->rx_jmb_ring_mask;
Michael Chan4f81c322006-03-20 21:33:42 -080010794 else
10795 ering->rx_jumbo_max_pending = 0;
10796
10797 ering->tx_max_pending = TG3_TX_RING_SIZE - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010798
10799 ering->rx_pending = tp->rx_pending;
Joe Perches63c3a662011-04-26 08:12:10 +000010800 if (tg3_flag(tp, JUMBO_RING_ENABLE))
Michael Chan4f81c322006-03-20 21:33:42 -080010801 ering->rx_jumbo_pending = tp->rx_jumbo_pending;
10802 else
10803 ering->rx_jumbo_pending = 0;
10804
Matt Carlsonf3f3f272009-08-28 14:03:21 +000010805 ering->tx_pending = tp->napi[0].tx_pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010806}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010807
Linus Torvalds1da177e2005-04-16 15:20:36 -070010808static int tg3_set_ringparam(struct net_device *dev, struct ethtool_ringparam *ering)
10809{
10810 struct tg3 *tp = netdev_priv(dev);
Matt Carlson646c9ed2009-09-01 12:58:41 +000010811 int i, irq_sync = 0, err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010812
Matt Carlson2c49a442010-09-30 10:34:35 +000010813 if ((ering->rx_pending > tp->rx_std_ring_mask) ||
10814 (ering->rx_jumbo_pending > tp->rx_jmb_ring_mask) ||
Michael Chanbc3a9252006-10-18 20:55:18 -070010815 (ering->tx_pending > TG3_TX_RING_SIZE - 1) ||
10816 (ering->tx_pending <= MAX_SKB_FRAGS) ||
Joe Perches63c3a662011-04-26 08:12:10 +000010817 (tg3_flag(tp, TSO_BUG) &&
Michael Chanbc3a9252006-10-18 20:55:18 -070010818 (ering->tx_pending <= (MAX_SKB_FRAGS * 3))))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010819 return -EINVAL;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010820
Michael Chanbbe832c2005-06-24 20:20:04 -070010821 if (netif_running(dev)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010822 tg3_phy_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010823 tg3_netif_stop(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -070010824 irq_sync = 1;
10825 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010826
Michael Chanbbe832c2005-06-24 20:20:04 -070010827 tg3_full_lock(tp, irq_sync);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010828
Linus Torvalds1da177e2005-04-16 15:20:36 -070010829 tp->rx_pending = ering->rx_pending;
10830
Joe Perches63c3a662011-04-26 08:12:10 +000010831 if (tg3_flag(tp, MAX_RXPEND_64) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070010832 tp->rx_pending > 63)
10833 tp->rx_pending = 63;
10834 tp->rx_jumbo_pending = ering->rx_jumbo_pending;
Matt Carlson646c9ed2009-09-01 12:58:41 +000010835
Matt Carlson6fd45cb2010-09-15 08:59:57 +000010836 for (i = 0; i < tp->irq_max; i++)
Matt Carlson646c9ed2009-09-01 12:58:41 +000010837 tp->napi[i].tx_pending = ering->tx_pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010838
10839 if (netif_running(dev)) {
Michael Chan944d9802005-05-29 14:57:48 -070010840 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Michael Chanb9ec6c12006-07-25 16:37:27 -070010841 err = tg3_restart_hw(tp, 1);
10842 if (!err)
10843 tg3_netif_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010844 }
10845
David S. Millerf47c11e2005-06-24 20:18:35 -070010846 tg3_full_unlock(tp);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010847
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010848 if (irq_sync && !err)
10849 tg3_phy_start(tp);
10850
Michael Chanb9ec6c12006-07-25 16:37:27 -070010851 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010852}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010853
Linus Torvalds1da177e2005-04-16 15:20:36 -070010854static void tg3_get_pauseparam(struct net_device *dev, struct ethtool_pauseparam *epause)
10855{
10856 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010857
Joe Perches63c3a662011-04-26 08:12:10 +000010858 epause->autoneg = !!tg3_flag(tp, PAUSE_AUTONEG);
Matt Carlson8d018622007-12-20 20:05:44 -080010859
Matt Carlson4a2db502011-12-08 14:40:17 +000010860 if (tp->link_config.flowctrl & FLOW_CTRL_RX)
Matt Carlson8d018622007-12-20 20:05:44 -080010861 epause->rx_pause = 1;
10862 else
10863 epause->rx_pause = 0;
10864
Matt Carlson4a2db502011-12-08 14:40:17 +000010865 if (tp->link_config.flowctrl & FLOW_CTRL_TX)
Matt Carlson8d018622007-12-20 20:05:44 -080010866 epause->tx_pause = 1;
10867 else
10868 epause->tx_pause = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010869}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010870
Linus Torvalds1da177e2005-04-16 15:20:36 -070010871static int tg3_set_pauseparam(struct net_device *dev, struct ethtool_pauseparam *epause)
10872{
10873 struct tg3 *tp = netdev_priv(dev);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010874 int err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010875
Joe Perches63c3a662011-04-26 08:12:10 +000010876 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson27121682010-02-17 15:16:57 +000010877 u32 newadv;
10878 struct phy_device *phydev;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010879
Matt Carlson27121682010-02-17 15:16:57 +000010880 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010881
Matt Carlson27121682010-02-17 15:16:57 +000010882 if (!(phydev->supported & SUPPORTED_Pause) ||
10883 (!(phydev->supported & SUPPORTED_Asym_Pause) &&
Nicolas Kaiser2259dca2010-10-07 23:29:27 +000010884 (epause->rx_pause != epause->tx_pause)))
Matt Carlson27121682010-02-17 15:16:57 +000010885 return -EINVAL;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010886
Matt Carlson27121682010-02-17 15:16:57 +000010887 tp->link_config.flowctrl = 0;
10888 if (epause->rx_pause) {
10889 tp->link_config.flowctrl |= FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010890
Matt Carlson27121682010-02-17 15:16:57 +000010891 if (epause->tx_pause) {
Steve Glendinninge18ce342008-12-16 02:00:00 -080010892 tp->link_config.flowctrl |= FLOW_CTRL_TX;
Matt Carlson27121682010-02-17 15:16:57 +000010893 newadv = ADVERTISED_Pause;
10894 } else
10895 newadv = ADVERTISED_Pause |
10896 ADVERTISED_Asym_Pause;
10897 } else if (epause->tx_pause) {
10898 tp->link_config.flowctrl |= FLOW_CTRL_TX;
10899 newadv = ADVERTISED_Asym_Pause;
10900 } else
10901 newadv = 0;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010902
Matt Carlson27121682010-02-17 15:16:57 +000010903 if (epause->autoneg)
Joe Perches63c3a662011-04-26 08:12:10 +000010904 tg3_flag_set(tp, PAUSE_AUTONEG);
Matt Carlson27121682010-02-17 15:16:57 +000010905 else
Joe Perches63c3a662011-04-26 08:12:10 +000010906 tg3_flag_clear(tp, PAUSE_AUTONEG);
Matt Carlson27121682010-02-17 15:16:57 +000010907
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010908 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) {
Matt Carlson27121682010-02-17 15:16:57 +000010909 u32 oldadv = phydev->advertising &
10910 (ADVERTISED_Pause | ADVERTISED_Asym_Pause);
10911 if (oldadv != newadv) {
10912 phydev->advertising &=
10913 ~(ADVERTISED_Pause |
10914 ADVERTISED_Asym_Pause);
10915 phydev->advertising |= newadv;
10916 if (phydev->autoneg) {
10917 /*
10918 * Always renegotiate the link to
10919 * inform our link partner of our
10920 * flow control settings, even if the
10921 * flow control is forced. Let
10922 * tg3_adjust_link() do the final
10923 * flow control setup.
10924 */
10925 return phy_start_aneg(phydev);
10926 }
10927 }
10928
10929 if (!epause->autoneg)
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010930 tg3_setup_flow_control(tp, 0, 0);
Matt Carlson27121682010-02-17 15:16:57 +000010931 } else {
Matt Carlsonc6700ce2012-02-13 15:20:15 +000010932 tp->link_config.advertising &=
Matt Carlson27121682010-02-17 15:16:57 +000010933 ~(ADVERTISED_Pause |
10934 ADVERTISED_Asym_Pause);
Matt Carlsonc6700ce2012-02-13 15:20:15 +000010935 tp->link_config.advertising |= newadv;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010936 }
10937 } else {
10938 int irq_sync = 0;
10939
10940 if (netif_running(dev)) {
10941 tg3_netif_stop(tp);
10942 irq_sync = 1;
10943 }
10944
10945 tg3_full_lock(tp, irq_sync);
10946
10947 if (epause->autoneg)
Joe Perches63c3a662011-04-26 08:12:10 +000010948 tg3_flag_set(tp, PAUSE_AUTONEG);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010949 else
Joe Perches63c3a662011-04-26 08:12:10 +000010950 tg3_flag_clear(tp, PAUSE_AUTONEG);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010951 if (epause->rx_pause)
Steve Glendinninge18ce342008-12-16 02:00:00 -080010952 tp->link_config.flowctrl |= FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010953 else
Steve Glendinninge18ce342008-12-16 02:00:00 -080010954 tp->link_config.flowctrl &= ~FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010955 if (epause->tx_pause)
Steve Glendinninge18ce342008-12-16 02:00:00 -080010956 tp->link_config.flowctrl |= FLOW_CTRL_TX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010957 else
Steve Glendinninge18ce342008-12-16 02:00:00 -080010958 tp->link_config.flowctrl &= ~FLOW_CTRL_TX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010959
10960 if (netif_running(dev)) {
10961 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
10962 err = tg3_restart_hw(tp, 1);
10963 if (!err)
10964 tg3_netif_start(tp);
10965 }
10966
10967 tg3_full_unlock(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -070010968 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010969
Michael Chanb9ec6c12006-07-25 16:37:27 -070010970 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010971}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010972
Matt Carlsonde6f31e2010-04-12 06:58:30 +000010973static int tg3_get_sset_count(struct net_device *dev, int sset)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010974{
Jeff Garzikb9f2c042007-10-03 18:07:32 -070010975 switch (sset) {
10976 case ETH_SS_TEST:
10977 return TG3_NUM_TEST;
10978 case ETH_SS_STATS:
10979 return TG3_NUM_STATS;
10980 default:
10981 return -EOPNOTSUPP;
10982 }
Michael Chan4cafd3f2005-05-29 14:56:34 -070010983}
10984
Matt Carlson90415472011-12-16 13:33:23 +000010985static int tg3_get_rxnfc(struct net_device *dev, struct ethtool_rxnfc *info,
10986 u32 *rules __always_unused)
10987{
10988 struct tg3 *tp = netdev_priv(dev);
10989
10990 if (!tg3_flag(tp, SUPPORT_MSIX))
10991 return -EOPNOTSUPP;
10992
10993 switch (info->cmd) {
10994 case ETHTOOL_GRXRINGS:
10995 if (netif_running(tp->dev))
10996 info->data = tp->irq_cnt;
10997 else {
10998 info->data = num_online_cpus();
10999 if (info->data > TG3_IRQ_MAX_VECS_RSS)
11000 info->data = TG3_IRQ_MAX_VECS_RSS;
11001 }
11002
11003 /* The first interrupt vector only
11004 * handles link interrupts.
11005 */
11006 info->data -= 1;
11007 return 0;
11008
11009 default:
11010 return -EOPNOTSUPP;
11011 }
11012}
11013
11014static u32 tg3_get_rxfh_indir_size(struct net_device *dev)
11015{
11016 u32 size = 0;
11017 struct tg3 *tp = netdev_priv(dev);
11018
11019 if (tg3_flag(tp, SUPPORT_MSIX))
11020 size = TG3_RSS_INDIR_TBL_SIZE;
11021
11022 return size;
11023}
11024
11025static int tg3_get_rxfh_indir(struct net_device *dev, u32 *indir)
11026{
11027 struct tg3 *tp = netdev_priv(dev);
11028 int i;
11029
11030 for (i = 0; i < TG3_RSS_INDIR_TBL_SIZE; i++)
11031 indir[i] = tp->rss_ind_tbl[i];
11032
11033 return 0;
11034}
11035
11036static int tg3_set_rxfh_indir(struct net_device *dev, const u32 *indir)
11037{
11038 struct tg3 *tp = netdev_priv(dev);
11039 size_t i;
11040
11041 for (i = 0; i < TG3_RSS_INDIR_TBL_SIZE; i++)
11042 tp->rss_ind_tbl[i] = indir[i];
11043
11044 if (!netif_running(dev) || !tg3_flag(tp, ENABLE_RSS))
11045 return 0;
11046
11047 /* It is legal to write the indirection
11048 * table while the device is running.
11049 */
11050 tg3_full_lock(tp, 0);
11051 tg3_rss_write_indir_tbl(tp);
11052 tg3_full_unlock(tp);
11053
11054 return 0;
11055}
11056
Matt Carlsonde6f31e2010-04-12 06:58:30 +000011057static void tg3_get_strings(struct net_device *dev, u32 stringset, u8 *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011058{
11059 switch (stringset) {
11060 case ETH_SS_STATS:
11061 memcpy(buf, &ethtool_stats_keys, sizeof(ethtool_stats_keys));
11062 break;
Michael Chan4cafd3f2005-05-29 14:56:34 -070011063 case ETH_SS_TEST:
11064 memcpy(buf, &ethtool_test_keys, sizeof(ethtool_test_keys));
11065 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011066 default:
11067 WARN_ON(1); /* we need a WARN() */
11068 break;
11069 }
11070}
11071
stephen hemminger81b87092011-04-04 08:43:50 +000011072static int tg3_set_phys_id(struct net_device *dev,
11073 enum ethtool_phys_id_state state)
Michael Chan4009a932005-09-05 17:52:54 -070011074{
11075 struct tg3 *tp = netdev_priv(dev);
Michael Chan4009a932005-09-05 17:52:54 -070011076
11077 if (!netif_running(tp->dev))
11078 return -EAGAIN;
11079
stephen hemminger81b87092011-04-04 08:43:50 +000011080 switch (state) {
11081 case ETHTOOL_ID_ACTIVE:
Allan, Bruce Wfce55922011-04-13 13:09:10 +000011082 return 1; /* cycle on/off once per second */
Michael Chan4009a932005-09-05 17:52:54 -070011083
stephen hemminger81b87092011-04-04 08:43:50 +000011084 case ETHTOOL_ID_ON:
11085 tw32(MAC_LED_CTRL, LED_CTRL_LNKLED_OVERRIDE |
11086 LED_CTRL_1000MBPS_ON |
11087 LED_CTRL_100MBPS_ON |
11088 LED_CTRL_10MBPS_ON |
11089 LED_CTRL_TRAFFIC_OVERRIDE |
11090 LED_CTRL_TRAFFIC_BLINK |
11091 LED_CTRL_TRAFFIC_LED);
11092 break;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011093
stephen hemminger81b87092011-04-04 08:43:50 +000011094 case ETHTOOL_ID_OFF:
11095 tw32(MAC_LED_CTRL, LED_CTRL_LNKLED_OVERRIDE |
11096 LED_CTRL_TRAFFIC_OVERRIDE);
11097 break;
Michael Chan4009a932005-09-05 17:52:54 -070011098
stephen hemminger81b87092011-04-04 08:43:50 +000011099 case ETHTOOL_ID_INACTIVE:
11100 tw32(MAC_LED_CTRL, tp->led_ctrl);
11101 break;
Michael Chan4009a932005-09-05 17:52:54 -070011102 }
stephen hemminger81b87092011-04-04 08:43:50 +000011103
Michael Chan4009a932005-09-05 17:52:54 -070011104 return 0;
11105}
11106
Matt Carlsonde6f31e2010-04-12 06:58:30 +000011107static void tg3_get_ethtool_stats(struct net_device *dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -070011108 struct ethtool_stats *estats, u64 *tmp_stats)
11109{
11110 struct tg3 *tp = netdev_priv(dev);
Matt Carlson0e6c9da2011-12-08 14:40:13 +000011111
Matt Carlsonb546e462012-02-13 15:20:09 +000011112 if (tp->hw_stats)
11113 tg3_get_estats(tp, (struct tg3_ethtool_stats *)tmp_stats);
11114 else
11115 memset(tmp_stats, 0, sizeof(struct tg3_ethtool_stats));
Linus Torvalds1da177e2005-04-16 15:20:36 -070011116}
11117
Matt Carlson535a4902011-07-20 10:20:56 +000011118static __be32 *tg3_vpd_readblock(struct tg3 *tp, u32 *vpdlen)
Matt Carlsonc3e94502011-04-13 11:05:08 +000011119{
11120 int i;
11121 __be32 *buf;
11122 u32 offset = 0, len = 0;
11123 u32 magic, val;
11124
Joe Perches63c3a662011-04-26 08:12:10 +000011125 if (tg3_flag(tp, NO_NVRAM) || tg3_nvram_read(tp, 0, &magic))
Matt Carlsonc3e94502011-04-13 11:05:08 +000011126 return NULL;
11127
11128 if (magic == TG3_EEPROM_MAGIC) {
11129 for (offset = TG3_NVM_DIR_START;
11130 offset < TG3_NVM_DIR_END;
11131 offset += TG3_NVM_DIRENT_SIZE) {
11132 if (tg3_nvram_read(tp, offset, &val))
11133 return NULL;
11134
11135 if ((val >> TG3_NVM_DIRTYPE_SHIFT) ==
11136 TG3_NVM_DIRTYPE_EXTVPD)
11137 break;
11138 }
11139
11140 if (offset != TG3_NVM_DIR_END) {
11141 len = (val & TG3_NVM_DIRTYPE_LENMSK) * 4;
11142 if (tg3_nvram_read(tp, offset + 4, &offset))
11143 return NULL;
11144
11145 offset = tg3_nvram_logical_addr(tp, offset);
11146 }
11147 }
11148
11149 if (!offset || !len) {
11150 offset = TG3_NVM_VPD_OFF;
11151 len = TG3_NVM_VPD_LEN;
11152 }
11153
11154 buf = kmalloc(len, GFP_KERNEL);
11155 if (buf == NULL)
11156 return NULL;
11157
11158 if (magic == TG3_EEPROM_MAGIC) {
11159 for (i = 0; i < len; i += 4) {
11160 /* The data is in little-endian format in NVRAM.
11161 * Use the big-endian read routines to preserve
11162 * the byte order as it exists in NVRAM.
11163 */
11164 if (tg3_nvram_read_be32(tp, offset + i, &buf[i/4]))
11165 goto error;
11166 }
11167 } else {
11168 u8 *ptr;
11169 ssize_t cnt;
11170 unsigned int pos = 0;
11171
11172 ptr = (u8 *)&buf[0];
11173 for (i = 0; pos < len && i < 3; i++, pos += cnt, ptr += cnt) {
11174 cnt = pci_read_vpd(tp->pdev, pos,
11175 len - pos, ptr);
11176 if (cnt == -ETIMEDOUT || cnt == -EINTR)
11177 cnt = 0;
11178 else if (cnt < 0)
11179 goto error;
11180 }
11181 if (pos != len)
11182 goto error;
11183 }
11184
Matt Carlson535a4902011-07-20 10:20:56 +000011185 *vpdlen = len;
11186
Matt Carlsonc3e94502011-04-13 11:05:08 +000011187 return buf;
11188
11189error:
11190 kfree(buf);
11191 return NULL;
11192}
11193
Michael Chan566f86a2005-05-29 14:56:58 -070011194#define NVRAM_TEST_SIZE 0x100
Matt Carlsona5767de2007-11-12 21:10:58 -080011195#define NVRAM_SELFBOOT_FORMAT1_0_SIZE 0x14
11196#define NVRAM_SELFBOOT_FORMAT1_2_SIZE 0x18
11197#define NVRAM_SELFBOOT_FORMAT1_3_SIZE 0x1c
Matt Carlson727a6d92011-06-13 13:38:58 +000011198#define NVRAM_SELFBOOT_FORMAT1_4_SIZE 0x20
11199#define NVRAM_SELFBOOT_FORMAT1_5_SIZE 0x24
Matt Carlsonbda18fa2011-07-20 10:20:57 +000011200#define NVRAM_SELFBOOT_FORMAT1_6_SIZE 0x50
Michael Chanb16250e2006-09-27 16:10:14 -070011201#define NVRAM_SELFBOOT_HW_SIZE 0x20
11202#define NVRAM_SELFBOOT_DATA_SIZE 0x1c
Michael Chan566f86a2005-05-29 14:56:58 -070011203
11204static int tg3_test_nvram(struct tg3 *tp)
11205{
Matt Carlson535a4902011-07-20 10:20:56 +000011206 u32 csum, magic, len;
Matt Carlsona9dc5292009-02-25 14:25:30 +000011207 __be32 *buf;
Andy Gospodarekab0049b2007-09-06 20:42:14 +010011208 int i, j, k, err = 0, size;
Michael Chan566f86a2005-05-29 14:56:58 -070011209
Joe Perches63c3a662011-04-26 08:12:10 +000011210 if (tg3_flag(tp, NO_NVRAM))
Matt Carlsondf259d82009-04-20 06:57:14 +000011211 return 0;
11212
Matt Carlsone4f34112009-02-25 14:25:00 +000011213 if (tg3_nvram_read(tp, 0, &magic) != 0)
Michael Chan1b277772006-03-20 22:27:48 -080011214 return -EIO;
11215
Michael Chan1b277772006-03-20 22:27:48 -080011216 if (magic == TG3_EEPROM_MAGIC)
11217 size = NVRAM_TEST_SIZE;
Michael Chanb16250e2006-09-27 16:10:14 -070011218 else if ((magic & TG3_EEPROM_MAGIC_FW_MSK) == TG3_EEPROM_MAGIC_FW) {
Matt Carlsona5767de2007-11-12 21:10:58 -080011219 if ((magic & TG3_EEPROM_SB_FORMAT_MASK) ==
11220 TG3_EEPROM_SB_FORMAT_1) {
11221 switch (magic & TG3_EEPROM_SB_REVISION_MASK) {
11222 case TG3_EEPROM_SB_REVISION_0:
11223 size = NVRAM_SELFBOOT_FORMAT1_0_SIZE;
11224 break;
11225 case TG3_EEPROM_SB_REVISION_2:
11226 size = NVRAM_SELFBOOT_FORMAT1_2_SIZE;
11227 break;
11228 case TG3_EEPROM_SB_REVISION_3:
11229 size = NVRAM_SELFBOOT_FORMAT1_3_SIZE;
11230 break;
Matt Carlson727a6d92011-06-13 13:38:58 +000011231 case TG3_EEPROM_SB_REVISION_4:
11232 size = NVRAM_SELFBOOT_FORMAT1_4_SIZE;
11233 break;
11234 case TG3_EEPROM_SB_REVISION_5:
11235 size = NVRAM_SELFBOOT_FORMAT1_5_SIZE;
11236 break;
11237 case TG3_EEPROM_SB_REVISION_6:
11238 size = NVRAM_SELFBOOT_FORMAT1_6_SIZE;
11239 break;
Matt Carlsona5767de2007-11-12 21:10:58 -080011240 default:
Matt Carlson727a6d92011-06-13 13:38:58 +000011241 return -EIO;
Matt Carlsona5767de2007-11-12 21:10:58 -080011242 }
11243 } else
Michael Chan1b277772006-03-20 22:27:48 -080011244 return 0;
Michael Chanb16250e2006-09-27 16:10:14 -070011245 } else if ((magic & TG3_EEPROM_MAGIC_HW_MSK) == TG3_EEPROM_MAGIC_HW)
11246 size = NVRAM_SELFBOOT_HW_SIZE;
11247 else
Michael Chan1b277772006-03-20 22:27:48 -080011248 return -EIO;
11249
11250 buf = kmalloc(size, GFP_KERNEL);
Michael Chan566f86a2005-05-29 14:56:58 -070011251 if (buf == NULL)
11252 return -ENOMEM;
11253
Michael Chan1b277772006-03-20 22:27:48 -080011254 err = -EIO;
11255 for (i = 0, j = 0; i < size; i += 4, j++) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000011256 err = tg3_nvram_read_be32(tp, i, &buf[j]);
11257 if (err)
Michael Chan566f86a2005-05-29 14:56:58 -070011258 break;
Michael Chan566f86a2005-05-29 14:56:58 -070011259 }
Michael Chan1b277772006-03-20 22:27:48 -080011260 if (i < size)
Michael Chan566f86a2005-05-29 14:56:58 -070011261 goto out;
11262
Michael Chan1b277772006-03-20 22:27:48 -080011263 /* Selfboot format */
Matt Carlsona9dc5292009-02-25 14:25:30 +000011264 magic = be32_to_cpu(buf[0]);
Al Virob9fc7dc2007-12-17 22:59:57 -080011265 if ((magic & TG3_EEPROM_MAGIC_FW_MSK) ==
Michael Chanb16250e2006-09-27 16:10:14 -070011266 TG3_EEPROM_MAGIC_FW) {
Michael Chan1b277772006-03-20 22:27:48 -080011267 u8 *buf8 = (u8 *) buf, csum8 = 0;
11268
Al Virob9fc7dc2007-12-17 22:59:57 -080011269 if ((magic & TG3_EEPROM_SB_REVISION_MASK) ==
Matt Carlsona5767de2007-11-12 21:10:58 -080011270 TG3_EEPROM_SB_REVISION_2) {
11271 /* For rev 2, the csum doesn't include the MBA. */
11272 for (i = 0; i < TG3_EEPROM_SB_F1R2_MBA_OFF; i++)
11273 csum8 += buf8[i];
11274 for (i = TG3_EEPROM_SB_F1R2_MBA_OFF + 4; i < size; i++)
11275 csum8 += buf8[i];
11276 } else {
11277 for (i = 0; i < size; i++)
11278 csum8 += buf8[i];
11279 }
Michael Chan1b277772006-03-20 22:27:48 -080011280
Adrian Bunkad96b482006-04-05 22:21:04 -070011281 if (csum8 == 0) {
11282 err = 0;
11283 goto out;
11284 }
11285
11286 err = -EIO;
11287 goto out;
Michael Chan1b277772006-03-20 22:27:48 -080011288 }
Michael Chan566f86a2005-05-29 14:56:58 -070011289
Al Virob9fc7dc2007-12-17 22:59:57 -080011290 if ((magic & TG3_EEPROM_MAGIC_HW_MSK) ==
Michael Chanb16250e2006-09-27 16:10:14 -070011291 TG3_EEPROM_MAGIC_HW) {
11292 u8 data[NVRAM_SELFBOOT_DATA_SIZE];
Matt Carlsona9dc5292009-02-25 14:25:30 +000011293 u8 parity[NVRAM_SELFBOOT_DATA_SIZE];
Michael Chanb16250e2006-09-27 16:10:14 -070011294 u8 *buf8 = (u8 *) buf;
Michael Chanb16250e2006-09-27 16:10:14 -070011295
11296 /* Separate the parity bits and the data bytes. */
11297 for (i = 0, j = 0, k = 0; i < NVRAM_SELFBOOT_HW_SIZE; i++) {
11298 if ((i == 0) || (i == 8)) {
11299 int l;
11300 u8 msk;
11301
11302 for (l = 0, msk = 0x80; l < 7; l++, msk >>= 1)
11303 parity[k++] = buf8[i] & msk;
11304 i++;
Matt Carlson859a588792010-04-05 10:19:28 +000011305 } else if (i == 16) {
Michael Chanb16250e2006-09-27 16:10:14 -070011306 int l;
11307 u8 msk;
11308
11309 for (l = 0, msk = 0x20; l < 6; l++, msk >>= 1)
11310 parity[k++] = buf8[i] & msk;
11311 i++;
11312
11313 for (l = 0, msk = 0x80; l < 8; l++, msk >>= 1)
11314 parity[k++] = buf8[i] & msk;
11315 i++;
11316 }
11317 data[j++] = buf8[i];
11318 }
11319
11320 err = -EIO;
11321 for (i = 0; i < NVRAM_SELFBOOT_DATA_SIZE; i++) {
11322 u8 hw8 = hweight8(data[i]);
11323
11324 if ((hw8 & 0x1) && parity[i])
11325 goto out;
11326 else if (!(hw8 & 0x1) && !parity[i])
11327 goto out;
11328 }
11329 err = 0;
11330 goto out;
11331 }
11332
Matt Carlson01c3a392011-03-09 16:58:20 +000011333 err = -EIO;
11334
Michael Chan566f86a2005-05-29 14:56:58 -070011335 /* Bootstrap checksum at offset 0x10 */
11336 csum = calc_crc((unsigned char *) buf, 0x10);
Matt Carlson01c3a392011-03-09 16:58:20 +000011337 if (csum != le32_to_cpu(buf[0x10/4]))
Michael Chan566f86a2005-05-29 14:56:58 -070011338 goto out;
11339
11340 /* Manufacturing block starts at offset 0x74, checksum at 0xfc */
11341 csum = calc_crc((unsigned char *) &buf[0x74/4], 0x88);
Matt Carlson01c3a392011-03-09 16:58:20 +000011342 if (csum != le32_to_cpu(buf[0xfc/4]))
Matt Carlsona9dc5292009-02-25 14:25:30 +000011343 goto out;
Michael Chan566f86a2005-05-29 14:56:58 -070011344
Matt Carlsonc3e94502011-04-13 11:05:08 +000011345 kfree(buf);
11346
Matt Carlson535a4902011-07-20 10:20:56 +000011347 buf = tg3_vpd_readblock(tp, &len);
Matt Carlsonc3e94502011-04-13 11:05:08 +000011348 if (!buf)
11349 return -ENOMEM;
Matt Carlsond4894f32011-03-09 16:58:21 +000011350
Matt Carlson535a4902011-07-20 10:20:56 +000011351 i = pci_vpd_find_tag((u8 *)buf, 0, len, PCI_VPD_LRDT_RO_DATA);
Matt Carlsond4894f32011-03-09 16:58:21 +000011352 if (i > 0) {
11353 j = pci_vpd_lrdt_size(&((u8 *)buf)[i]);
11354 if (j < 0)
11355 goto out;
11356
Matt Carlson535a4902011-07-20 10:20:56 +000011357 if (i + PCI_VPD_LRDT_TAG_SIZE + j > len)
Matt Carlsond4894f32011-03-09 16:58:21 +000011358 goto out;
11359
11360 i += PCI_VPD_LRDT_TAG_SIZE;
11361 j = pci_vpd_find_info_keyword((u8 *)buf, i, j,
11362 PCI_VPD_RO_KEYWORD_CHKSUM);
11363 if (j > 0) {
11364 u8 csum8 = 0;
11365
11366 j += PCI_VPD_INFO_FLD_HDR_SIZE;
11367
11368 for (i = 0; i <= j; i++)
11369 csum8 += ((u8 *)buf)[i];
11370
11371 if (csum8)
11372 goto out;
11373 }
11374 }
11375
Michael Chan566f86a2005-05-29 14:56:58 -070011376 err = 0;
11377
11378out:
11379 kfree(buf);
11380 return err;
11381}
11382
Michael Chanca430072005-05-29 14:57:23 -070011383#define TG3_SERDES_TIMEOUT_SEC 2
11384#define TG3_COPPER_TIMEOUT_SEC 6
11385
11386static int tg3_test_link(struct tg3 *tp)
11387{
11388 int i, max;
11389
11390 if (!netif_running(tp->dev))
11391 return -ENODEV;
11392
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011393 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
Michael Chanca430072005-05-29 14:57:23 -070011394 max = TG3_SERDES_TIMEOUT_SEC;
11395 else
11396 max = TG3_COPPER_TIMEOUT_SEC;
11397
11398 for (i = 0; i < max; i++) {
11399 if (netif_carrier_ok(tp->dev))
11400 return 0;
11401
11402 if (msleep_interruptible(1000))
11403 break;
11404 }
11405
11406 return -EIO;
11407}
11408
Michael Chana71116d2005-05-29 14:58:11 -070011409/* Only test the commonly used registers */
David S. Miller30ca3e32006-03-20 23:02:36 -080011410static int tg3_test_registers(struct tg3 *tp)
Michael Chana71116d2005-05-29 14:58:11 -070011411{
Michael Chanb16250e2006-09-27 16:10:14 -070011412 int i, is_5705, is_5750;
Michael Chana71116d2005-05-29 14:58:11 -070011413 u32 offset, read_mask, write_mask, val, save_val, read_val;
11414 static struct {
11415 u16 offset;
11416 u16 flags;
11417#define TG3_FL_5705 0x1
11418#define TG3_FL_NOT_5705 0x2
11419#define TG3_FL_NOT_5788 0x4
Michael Chanb16250e2006-09-27 16:10:14 -070011420#define TG3_FL_NOT_5750 0x8
Michael Chana71116d2005-05-29 14:58:11 -070011421 u32 read_mask;
11422 u32 write_mask;
11423 } reg_tbl[] = {
11424 /* MAC Control Registers */
11425 { MAC_MODE, TG3_FL_NOT_5705,
11426 0x00000000, 0x00ef6f8c },
11427 { MAC_MODE, TG3_FL_5705,
11428 0x00000000, 0x01ef6b8c },
11429 { MAC_STATUS, TG3_FL_NOT_5705,
11430 0x03800107, 0x00000000 },
11431 { MAC_STATUS, TG3_FL_5705,
11432 0x03800100, 0x00000000 },
11433 { MAC_ADDR_0_HIGH, 0x0000,
11434 0x00000000, 0x0000ffff },
11435 { MAC_ADDR_0_LOW, 0x0000,
Matt Carlsonc6cdf432010-04-05 10:19:26 +000011436 0x00000000, 0xffffffff },
Michael Chana71116d2005-05-29 14:58:11 -070011437 { MAC_RX_MTU_SIZE, 0x0000,
11438 0x00000000, 0x0000ffff },
11439 { MAC_TX_MODE, 0x0000,
11440 0x00000000, 0x00000070 },
11441 { MAC_TX_LENGTHS, 0x0000,
11442 0x00000000, 0x00003fff },
11443 { MAC_RX_MODE, TG3_FL_NOT_5705,
11444 0x00000000, 0x000007fc },
11445 { MAC_RX_MODE, TG3_FL_5705,
11446 0x00000000, 0x000007dc },
11447 { MAC_HASH_REG_0, 0x0000,
11448 0x00000000, 0xffffffff },
11449 { MAC_HASH_REG_1, 0x0000,
11450 0x00000000, 0xffffffff },
11451 { MAC_HASH_REG_2, 0x0000,
11452 0x00000000, 0xffffffff },
11453 { MAC_HASH_REG_3, 0x0000,
11454 0x00000000, 0xffffffff },
11455
11456 /* Receive Data and Receive BD Initiator Control Registers. */
11457 { RCVDBDI_JUMBO_BD+0, TG3_FL_NOT_5705,
11458 0x00000000, 0xffffffff },
11459 { RCVDBDI_JUMBO_BD+4, TG3_FL_NOT_5705,
11460 0x00000000, 0xffffffff },
11461 { RCVDBDI_JUMBO_BD+8, TG3_FL_NOT_5705,
11462 0x00000000, 0x00000003 },
11463 { RCVDBDI_JUMBO_BD+0xc, TG3_FL_NOT_5705,
11464 0x00000000, 0xffffffff },
11465 { RCVDBDI_STD_BD+0, 0x0000,
11466 0x00000000, 0xffffffff },
11467 { RCVDBDI_STD_BD+4, 0x0000,
11468 0x00000000, 0xffffffff },
11469 { RCVDBDI_STD_BD+8, 0x0000,
11470 0x00000000, 0xffff0002 },
11471 { RCVDBDI_STD_BD+0xc, 0x0000,
11472 0x00000000, 0xffffffff },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011473
Michael Chana71116d2005-05-29 14:58:11 -070011474 /* Receive BD Initiator Control Registers. */
11475 { RCVBDI_STD_THRESH, TG3_FL_NOT_5705,
11476 0x00000000, 0xffffffff },
11477 { RCVBDI_STD_THRESH, TG3_FL_5705,
11478 0x00000000, 0x000003ff },
11479 { RCVBDI_JUMBO_THRESH, TG3_FL_NOT_5705,
11480 0x00000000, 0xffffffff },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011481
Michael Chana71116d2005-05-29 14:58:11 -070011482 /* Host Coalescing Control Registers. */
11483 { HOSTCC_MODE, TG3_FL_NOT_5705,
11484 0x00000000, 0x00000004 },
11485 { HOSTCC_MODE, TG3_FL_5705,
11486 0x00000000, 0x000000f6 },
11487 { HOSTCC_RXCOL_TICKS, TG3_FL_NOT_5705,
11488 0x00000000, 0xffffffff },
11489 { HOSTCC_RXCOL_TICKS, TG3_FL_5705,
11490 0x00000000, 0x000003ff },
11491 { HOSTCC_TXCOL_TICKS, TG3_FL_NOT_5705,
11492 0x00000000, 0xffffffff },
11493 { HOSTCC_TXCOL_TICKS, TG3_FL_5705,
11494 0x00000000, 0x000003ff },
11495 { HOSTCC_RXMAX_FRAMES, TG3_FL_NOT_5705,
11496 0x00000000, 0xffffffff },
11497 { HOSTCC_RXMAX_FRAMES, TG3_FL_5705 | TG3_FL_NOT_5788,
11498 0x00000000, 0x000000ff },
11499 { HOSTCC_TXMAX_FRAMES, TG3_FL_NOT_5705,
11500 0x00000000, 0xffffffff },
11501 { HOSTCC_TXMAX_FRAMES, TG3_FL_5705 | TG3_FL_NOT_5788,
11502 0x00000000, 0x000000ff },
11503 { HOSTCC_RXCOAL_TICK_INT, TG3_FL_NOT_5705,
11504 0x00000000, 0xffffffff },
11505 { HOSTCC_TXCOAL_TICK_INT, TG3_FL_NOT_5705,
11506 0x00000000, 0xffffffff },
11507 { HOSTCC_RXCOAL_MAXF_INT, TG3_FL_NOT_5705,
11508 0x00000000, 0xffffffff },
11509 { HOSTCC_RXCOAL_MAXF_INT, TG3_FL_5705 | TG3_FL_NOT_5788,
11510 0x00000000, 0x000000ff },
11511 { HOSTCC_TXCOAL_MAXF_INT, TG3_FL_NOT_5705,
11512 0x00000000, 0xffffffff },
11513 { HOSTCC_TXCOAL_MAXF_INT, TG3_FL_5705 | TG3_FL_NOT_5788,
11514 0x00000000, 0x000000ff },
11515 { HOSTCC_STAT_COAL_TICKS, TG3_FL_NOT_5705,
11516 0x00000000, 0xffffffff },
11517 { HOSTCC_STATS_BLK_HOST_ADDR, TG3_FL_NOT_5705,
11518 0x00000000, 0xffffffff },
11519 { HOSTCC_STATS_BLK_HOST_ADDR+4, TG3_FL_NOT_5705,
11520 0x00000000, 0xffffffff },
11521 { HOSTCC_STATUS_BLK_HOST_ADDR, 0x0000,
11522 0x00000000, 0xffffffff },
11523 { HOSTCC_STATUS_BLK_HOST_ADDR+4, 0x0000,
11524 0x00000000, 0xffffffff },
11525 { HOSTCC_STATS_BLK_NIC_ADDR, 0x0000,
11526 0xffffffff, 0x00000000 },
11527 { HOSTCC_STATUS_BLK_NIC_ADDR, 0x0000,
11528 0xffffffff, 0x00000000 },
11529
11530 /* Buffer Manager Control Registers. */
Michael Chanb16250e2006-09-27 16:10:14 -070011531 { BUFMGR_MB_POOL_ADDR, TG3_FL_NOT_5750,
Michael Chana71116d2005-05-29 14:58:11 -070011532 0x00000000, 0x007fff80 },
Michael Chanb16250e2006-09-27 16:10:14 -070011533 { BUFMGR_MB_POOL_SIZE, TG3_FL_NOT_5750,
Michael Chana71116d2005-05-29 14:58:11 -070011534 0x00000000, 0x007fffff },
11535 { BUFMGR_MB_RDMA_LOW_WATER, 0x0000,
11536 0x00000000, 0x0000003f },
11537 { BUFMGR_MB_MACRX_LOW_WATER, 0x0000,
11538 0x00000000, 0x000001ff },
11539 { BUFMGR_MB_HIGH_WATER, 0x0000,
11540 0x00000000, 0x000001ff },
11541 { BUFMGR_DMA_DESC_POOL_ADDR, TG3_FL_NOT_5705,
11542 0xffffffff, 0x00000000 },
11543 { BUFMGR_DMA_DESC_POOL_SIZE, TG3_FL_NOT_5705,
11544 0xffffffff, 0x00000000 },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011545
Michael Chana71116d2005-05-29 14:58:11 -070011546 /* Mailbox Registers */
11547 { GRCMBOX_RCVSTD_PROD_IDX+4, 0x0000,
11548 0x00000000, 0x000001ff },
11549 { GRCMBOX_RCVJUMBO_PROD_IDX+4, TG3_FL_NOT_5705,
11550 0x00000000, 0x000001ff },
11551 { GRCMBOX_RCVRET_CON_IDX_0+4, 0x0000,
11552 0x00000000, 0x000007ff },
11553 { GRCMBOX_SNDHOST_PROD_IDX_0+4, 0x0000,
11554 0x00000000, 0x000001ff },
11555
11556 { 0xffff, 0x0000, 0x00000000, 0x00000000 },
11557 };
11558
Michael Chanb16250e2006-09-27 16:10:14 -070011559 is_5705 = is_5750 = 0;
Joe Perches63c3a662011-04-26 08:12:10 +000011560 if (tg3_flag(tp, 5705_PLUS)) {
Michael Chana71116d2005-05-29 14:58:11 -070011561 is_5705 = 1;
Joe Perches63c3a662011-04-26 08:12:10 +000011562 if (tg3_flag(tp, 5750_PLUS))
Michael Chanb16250e2006-09-27 16:10:14 -070011563 is_5750 = 1;
11564 }
Michael Chana71116d2005-05-29 14:58:11 -070011565
11566 for (i = 0; reg_tbl[i].offset != 0xffff; i++) {
11567 if (is_5705 && (reg_tbl[i].flags & TG3_FL_NOT_5705))
11568 continue;
11569
11570 if (!is_5705 && (reg_tbl[i].flags & TG3_FL_5705))
11571 continue;
11572
Joe Perches63c3a662011-04-26 08:12:10 +000011573 if (tg3_flag(tp, IS_5788) &&
Michael Chana71116d2005-05-29 14:58:11 -070011574 (reg_tbl[i].flags & TG3_FL_NOT_5788))
11575 continue;
11576
Michael Chanb16250e2006-09-27 16:10:14 -070011577 if (is_5750 && (reg_tbl[i].flags & TG3_FL_NOT_5750))
11578 continue;
11579
Michael Chana71116d2005-05-29 14:58:11 -070011580 offset = (u32) reg_tbl[i].offset;
11581 read_mask = reg_tbl[i].read_mask;
11582 write_mask = reg_tbl[i].write_mask;
11583
11584 /* Save the original register content */
11585 save_val = tr32(offset);
11586
11587 /* Determine the read-only value. */
11588 read_val = save_val & read_mask;
11589
11590 /* Write zero to the register, then make sure the read-only bits
11591 * are not changed and the read/write bits are all zeros.
11592 */
11593 tw32(offset, 0);
11594
11595 val = tr32(offset);
11596
11597 /* Test the read-only and read/write bits. */
11598 if (((val & read_mask) != read_val) || (val & write_mask))
11599 goto out;
11600
11601 /* Write ones to all the bits defined by RdMask and WrMask, then
11602 * make sure the read-only bits are not changed and the
11603 * read/write bits are all ones.
11604 */
11605 tw32(offset, read_mask | write_mask);
11606
11607 val = tr32(offset);
11608
11609 /* Test the read-only bits. */
11610 if ((val & read_mask) != read_val)
11611 goto out;
11612
11613 /* Test the read/write bits. */
11614 if ((val & write_mask) != write_mask)
11615 goto out;
11616
11617 tw32(offset, save_val);
11618 }
11619
11620 return 0;
11621
11622out:
Michael Chan9f88f292006-12-07 00:22:54 -080011623 if (netif_msg_hw(tp))
Matt Carlson2445e462010-04-05 10:19:21 +000011624 netdev_err(tp->dev,
11625 "Register test failed at offset %x\n", offset);
Michael Chana71116d2005-05-29 14:58:11 -070011626 tw32(offset, save_val);
11627 return -EIO;
11628}
11629
Michael Chan7942e1d2005-05-29 14:58:36 -070011630static int tg3_do_mem_test(struct tg3 *tp, u32 offset, u32 len)
11631{
Arjan van de Venf71e1302006-03-03 21:33:57 -050011632 static const u32 test_pattern[] = { 0x00000000, 0xffffffff, 0xaa55a55a };
Michael Chan7942e1d2005-05-29 14:58:36 -070011633 int i;
11634 u32 j;
11635
Alejandro Martinez Ruize9edda62007-10-15 03:37:43 +020011636 for (i = 0; i < ARRAY_SIZE(test_pattern); i++) {
Michael Chan7942e1d2005-05-29 14:58:36 -070011637 for (j = 0; j < len; j += 4) {
11638 u32 val;
11639
11640 tg3_write_mem(tp, offset + j, test_pattern[i]);
11641 tg3_read_mem(tp, offset + j, &val);
11642 if (val != test_pattern[i])
11643 return -EIO;
11644 }
11645 }
11646 return 0;
11647}
11648
11649static int tg3_test_memory(struct tg3 *tp)
11650{
11651 static struct mem_entry {
11652 u32 offset;
11653 u32 len;
11654 } mem_tbl_570x[] = {
Michael Chan38690192005-12-19 16:27:28 -080011655 { 0x00000000, 0x00b50},
Michael Chan7942e1d2005-05-29 14:58:36 -070011656 { 0x00002000, 0x1c000},
11657 { 0xffffffff, 0x00000}
11658 }, mem_tbl_5705[] = {
11659 { 0x00000100, 0x0000c},
11660 { 0x00000200, 0x00008},
Michael Chan7942e1d2005-05-29 14:58:36 -070011661 { 0x00004000, 0x00800},
11662 { 0x00006000, 0x01000},
11663 { 0x00008000, 0x02000},
11664 { 0x00010000, 0x0e000},
11665 { 0xffffffff, 0x00000}
Michael Chan79f4d132006-03-20 22:28:57 -080011666 }, mem_tbl_5755[] = {
11667 { 0x00000200, 0x00008},
11668 { 0x00004000, 0x00800},
11669 { 0x00006000, 0x00800},
11670 { 0x00008000, 0x02000},
11671 { 0x00010000, 0x0c000},
11672 { 0xffffffff, 0x00000}
Michael Chanb16250e2006-09-27 16:10:14 -070011673 }, mem_tbl_5906[] = {
11674 { 0x00000200, 0x00008},
11675 { 0x00004000, 0x00400},
11676 { 0x00006000, 0x00400},
11677 { 0x00008000, 0x01000},
11678 { 0x00010000, 0x01000},
11679 { 0xffffffff, 0x00000}
Matt Carlson8b5a6c42010-01-20 16:58:06 +000011680 }, mem_tbl_5717[] = {
11681 { 0x00000200, 0x00008},
11682 { 0x00010000, 0x0a000},
11683 { 0x00020000, 0x13c00},
11684 { 0xffffffff, 0x00000}
11685 }, mem_tbl_57765[] = {
11686 { 0x00000200, 0x00008},
11687 { 0x00004000, 0x00800},
11688 { 0x00006000, 0x09800},
11689 { 0x00010000, 0x0a000},
11690 { 0xffffffff, 0x00000}
Michael Chan7942e1d2005-05-29 14:58:36 -070011691 };
11692 struct mem_entry *mem_tbl;
11693 int err = 0;
11694 int i;
11695
Joe Perches63c3a662011-04-26 08:12:10 +000011696 if (tg3_flag(tp, 5717_PLUS))
Matt Carlson8b5a6c42010-01-20 16:58:06 +000011697 mem_tbl = mem_tbl_5717;
Matt Carlson55086ad2011-12-14 11:09:59 +000011698 else if (tg3_flag(tp, 57765_CLASS))
Matt Carlson8b5a6c42010-01-20 16:58:06 +000011699 mem_tbl = mem_tbl_57765;
Joe Perches63c3a662011-04-26 08:12:10 +000011700 else if (tg3_flag(tp, 5755_PLUS))
Matt Carlson321d32a2008-11-21 17:22:19 -080011701 mem_tbl = mem_tbl_5755;
11702 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
11703 mem_tbl = mem_tbl_5906;
Joe Perches63c3a662011-04-26 08:12:10 +000011704 else if (tg3_flag(tp, 5705_PLUS))
Matt Carlson321d32a2008-11-21 17:22:19 -080011705 mem_tbl = mem_tbl_5705;
11706 else
Michael Chan7942e1d2005-05-29 14:58:36 -070011707 mem_tbl = mem_tbl_570x;
11708
11709 for (i = 0; mem_tbl[i].offset != 0xffffffff; i++) {
Matt Carlsonbe98da62010-07-11 09:31:46 +000011710 err = tg3_do_mem_test(tp, mem_tbl[i].offset, mem_tbl[i].len);
11711 if (err)
Michael Chan7942e1d2005-05-29 14:58:36 -070011712 break;
11713 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011714
Michael Chan7942e1d2005-05-29 14:58:36 -070011715 return err;
11716}
11717
Matt Carlsonbb158d62011-04-25 12:42:47 +000011718#define TG3_TSO_MSS 500
11719
11720#define TG3_TSO_IP_HDR_LEN 20
11721#define TG3_TSO_TCP_HDR_LEN 20
11722#define TG3_TSO_TCP_OPT_LEN 12
11723
11724static const u8 tg3_tso_header[] = {
117250x08, 0x00,
117260x45, 0x00, 0x00, 0x00,
117270x00, 0x00, 0x40, 0x00,
117280x40, 0x06, 0x00, 0x00,
117290x0a, 0x00, 0x00, 0x01,
117300x0a, 0x00, 0x00, 0x02,
117310x0d, 0x00, 0xe0, 0x00,
117320x00, 0x00, 0x01, 0x00,
117330x00, 0x00, 0x02, 0x00,
117340x80, 0x10, 0x10, 0x00,
117350x14, 0x09, 0x00, 0x00,
117360x01, 0x01, 0x08, 0x0a,
117370x11, 0x11, 0x11, 0x11,
117380x11, 0x11, 0x11, 0x11,
11739};
Michael Chan9f40dea2005-09-05 17:53:06 -070011740
Matt Carlson28a45952011-08-19 13:58:22 +000011741static int tg3_run_loopback(struct tg3 *tp, u32 pktsz, bool tso_loopback)
Michael Chanc76949a2005-05-29 14:58:59 -070011742{
Matt Carlson5e5a7f32011-08-19 13:58:21 +000011743 u32 rx_start_idx, rx_idx, tx_idx, opaque_key;
Matt Carlsonbb158d62011-04-25 12:42:47 +000011744 u32 base_flags = 0, mss = 0, desc_idx, coal_now, data_off, val;
Matt Carlson84b67b22011-07-27 14:20:52 +000011745 u32 budget;
Eric Dumazet9205fd92011-11-18 06:47:01 +000011746 struct sk_buff *skb;
11747 u8 *tx_data, *rx_data;
Michael Chanc76949a2005-05-29 14:58:59 -070011748 dma_addr_t map;
11749 int num_pkts, tx_len, rx_len, i, err;
11750 struct tg3_rx_buffer_desc *desc;
Matt Carlson898a56f2009-08-28 14:02:40 +000011751 struct tg3_napi *tnapi, *rnapi;
Matt Carlson8fea32b2010-09-15 08:59:58 +000011752 struct tg3_rx_prodring_set *tpr = &tp->napi[0].prodring;
Michael Chanc76949a2005-05-29 14:58:59 -070011753
Matt Carlsonc8873402010-02-12 14:47:11 +000011754 tnapi = &tp->napi[0];
11755 rnapi = &tp->napi[0];
Matt Carlson0c1d0e22009-09-01 13:16:33 +000011756 if (tp->irq_cnt > 1) {
Joe Perches63c3a662011-04-26 08:12:10 +000011757 if (tg3_flag(tp, ENABLE_RSS))
Matt Carlson1da85aa2010-09-30 10:34:34 +000011758 rnapi = &tp->napi[1];
Joe Perches63c3a662011-04-26 08:12:10 +000011759 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlsonc8873402010-02-12 14:47:11 +000011760 tnapi = &tp->napi[1];
Matt Carlson0c1d0e22009-09-01 13:16:33 +000011761 }
Matt Carlsonfd2ce372009-09-01 12:51:13 +000011762 coal_now = tnapi->coal_now | rnapi->coal_now;
Matt Carlson898a56f2009-08-28 14:02:40 +000011763
Michael Chanc76949a2005-05-29 14:58:59 -070011764 err = -EIO;
11765
Matt Carlson4852a862011-04-13 11:05:07 +000011766 tx_len = pktsz;
David S. Millera20e9c62006-07-31 22:38:16 -070011767 skb = netdev_alloc_skb(tp->dev, tx_len);
Jesper Juhla50bb7b2006-05-09 23:14:35 -070011768 if (!skb)
11769 return -ENOMEM;
11770
Michael Chanc76949a2005-05-29 14:58:59 -070011771 tx_data = skb_put(skb, tx_len);
11772 memcpy(tx_data, tp->dev->dev_addr, 6);
11773 memset(tx_data + 6, 0x0, 8);
11774
Matt Carlson4852a862011-04-13 11:05:07 +000011775 tw32(MAC_RX_MTU_SIZE, tx_len + ETH_FCS_LEN);
Michael Chanc76949a2005-05-29 14:58:59 -070011776
Matt Carlson28a45952011-08-19 13:58:22 +000011777 if (tso_loopback) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000011778 struct iphdr *iph = (struct iphdr *)&tx_data[ETH_HLEN];
11779
11780 u32 hdr_len = TG3_TSO_IP_HDR_LEN + TG3_TSO_TCP_HDR_LEN +
11781 TG3_TSO_TCP_OPT_LEN;
11782
11783 memcpy(tx_data + ETH_ALEN * 2, tg3_tso_header,
11784 sizeof(tg3_tso_header));
11785 mss = TG3_TSO_MSS;
11786
11787 val = tx_len - ETH_ALEN * 2 - sizeof(tg3_tso_header);
11788 num_pkts = DIV_ROUND_UP(val, TG3_TSO_MSS);
11789
11790 /* Set the total length field in the IP header */
11791 iph->tot_len = htons((u16)(mss + hdr_len));
11792
11793 base_flags = (TXD_FLAG_CPU_PRE_DMA |
11794 TXD_FLAG_CPU_POST_DMA);
11795
Joe Perches63c3a662011-04-26 08:12:10 +000011796 if (tg3_flag(tp, HW_TSO_1) ||
11797 tg3_flag(tp, HW_TSO_2) ||
11798 tg3_flag(tp, HW_TSO_3)) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000011799 struct tcphdr *th;
11800 val = ETH_HLEN + TG3_TSO_IP_HDR_LEN;
11801 th = (struct tcphdr *)&tx_data[val];
11802 th->check = 0;
11803 } else
11804 base_flags |= TXD_FLAG_TCPUDP_CSUM;
11805
Joe Perches63c3a662011-04-26 08:12:10 +000011806 if (tg3_flag(tp, HW_TSO_3)) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000011807 mss |= (hdr_len & 0xc) << 12;
11808 if (hdr_len & 0x10)
11809 base_flags |= 0x00000010;
11810 base_flags |= (hdr_len & 0x3e0) << 5;
Joe Perches63c3a662011-04-26 08:12:10 +000011811 } else if (tg3_flag(tp, HW_TSO_2))
Matt Carlsonbb158d62011-04-25 12:42:47 +000011812 mss |= hdr_len << 9;
Joe Perches63c3a662011-04-26 08:12:10 +000011813 else if (tg3_flag(tp, HW_TSO_1) ||
Matt Carlsonbb158d62011-04-25 12:42:47 +000011814 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
11815 mss |= (TG3_TSO_TCP_OPT_LEN << 9);
11816 } else {
11817 base_flags |= (TG3_TSO_TCP_OPT_LEN << 10);
11818 }
11819
11820 data_off = ETH_ALEN * 2 + sizeof(tg3_tso_header);
11821 } else {
11822 num_pkts = 1;
11823 data_off = ETH_HLEN;
Michael Chanc441b452012-03-04 14:48:13 +000011824
11825 if (tg3_flag(tp, USE_JUMBO_BDFLAG) &&
11826 tx_len > VLAN_ETH_FRAME_LEN)
11827 base_flags |= TXD_FLAG_JMB_PKT;
Matt Carlsonbb158d62011-04-25 12:42:47 +000011828 }
11829
11830 for (i = data_off; i < tx_len; i++)
Michael Chanc76949a2005-05-29 14:58:59 -070011831 tx_data[i] = (u8) (i & 0xff);
11832
Alexander Duyckf4188d82009-12-02 16:48:38 +000011833 map = pci_map_single(tp->pdev, skb->data, tx_len, PCI_DMA_TODEVICE);
11834 if (pci_dma_mapping_error(tp->pdev, map)) {
Matt Carlsona21771d2009-11-02 14:25:31 +000011835 dev_kfree_skb(skb);
11836 return -EIO;
11837 }
Michael Chanc76949a2005-05-29 14:58:59 -070011838
Matt Carlson0d681b22011-07-27 14:20:49 +000011839 val = tnapi->tx_prod;
11840 tnapi->tx_buffers[val].skb = skb;
11841 dma_unmap_addr_set(&tnapi->tx_buffers[val], mapping, map);
11842
Michael Chanc76949a2005-05-29 14:58:59 -070011843 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +000011844 rnapi->coal_now);
Michael Chanc76949a2005-05-29 14:58:59 -070011845
11846 udelay(10);
11847
Matt Carlson898a56f2009-08-28 14:02:40 +000011848 rx_start_idx = rnapi->hw_status->idx[0].rx_producer;
Michael Chanc76949a2005-05-29 14:58:59 -070011849
Matt Carlson84b67b22011-07-27 14:20:52 +000011850 budget = tg3_tx_avail(tnapi);
11851 if (tg3_tx_frag_set(tnapi, &val, &budget, map, tx_len,
Matt Carlsond1a3b732011-07-27 14:20:51 +000011852 base_flags | TXD_FLAG_END, mss, 0)) {
11853 tnapi->tx_buffers[val].skb = NULL;
11854 dev_kfree_skb(skb);
11855 return -EIO;
11856 }
Michael Chanc76949a2005-05-29 14:58:59 -070011857
Matt Carlsonf3f3f272009-08-28 14:03:21 +000011858 tnapi->tx_prod++;
Michael Chanc76949a2005-05-29 14:58:59 -070011859
Michael Chan6541b802012-03-04 14:48:14 +000011860 /* Sync BD data before updating mailbox */
11861 wmb();
11862
Matt Carlsonf3f3f272009-08-28 14:03:21 +000011863 tw32_tx_mbox(tnapi->prodmbox, tnapi->tx_prod);
11864 tr32_mailbox(tnapi->prodmbox);
Michael Chanc76949a2005-05-29 14:58:59 -070011865
11866 udelay(10);
11867
Matt Carlson303fc922009-11-02 14:27:34 +000011868 /* 350 usec to allow enough time on some 10/100 Mbps devices. */
11869 for (i = 0; i < 35; i++) {
Michael Chanc76949a2005-05-29 14:58:59 -070011870 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +000011871 coal_now);
Michael Chanc76949a2005-05-29 14:58:59 -070011872
11873 udelay(10);
11874
Matt Carlson898a56f2009-08-28 14:02:40 +000011875 tx_idx = tnapi->hw_status->idx[0].tx_consumer;
11876 rx_idx = rnapi->hw_status->idx[0].rx_producer;
Matt Carlsonf3f3f272009-08-28 14:03:21 +000011877 if ((tx_idx == tnapi->tx_prod) &&
Michael Chanc76949a2005-05-29 14:58:59 -070011878 (rx_idx == (rx_start_idx + num_pkts)))
11879 break;
11880 }
11881
Matt Carlsonba1142e2011-11-04 09:15:00 +000011882 tg3_tx_skb_unmap(tnapi, tnapi->tx_prod - 1, -1);
Michael Chanc76949a2005-05-29 14:58:59 -070011883 dev_kfree_skb(skb);
11884
Matt Carlsonf3f3f272009-08-28 14:03:21 +000011885 if (tx_idx != tnapi->tx_prod)
Michael Chanc76949a2005-05-29 14:58:59 -070011886 goto out;
11887
11888 if (rx_idx != rx_start_idx + num_pkts)
11889 goto out;
11890
Matt Carlsonbb158d62011-04-25 12:42:47 +000011891 val = data_off;
11892 while (rx_idx != rx_start_idx) {
11893 desc = &rnapi->rx_rcb[rx_start_idx++];
11894 desc_idx = desc->opaque & RXD_OPAQUE_INDEX_MASK;
11895 opaque_key = desc->opaque & RXD_OPAQUE_RING_MASK;
Michael Chanc76949a2005-05-29 14:58:59 -070011896
Matt Carlsonbb158d62011-04-25 12:42:47 +000011897 if ((desc->err_vlan & RXD_ERR_MASK) != 0 &&
11898 (desc->err_vlan != RXD_ERR_ODD_NIBBLE_RCVD_MII))
Matt Carlson4852a862011-04-13 11:05:07 +000011899 goto out;
Michael Chanc76949a2005-05-29 14:58:59 -070011900
Matt Carlsonbb158d62011-04-25 12:42:47 +000011901 rx_len = ((desc->idx_len & RXD_LEN_MASK) >> RXD_LEN_SHIFT)
11902 - ETH_FCS_LEN;
11903
Matt Carlson28a45952011-08-19 13:58:22 +000011904 if (!tso_loopback) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000011905 if (rx_len != tx_len)
11906 goto out;
11907
11908 if (pktsz <= TG3_RX_STD_DMA_SZ - ETH_FCS_LEN) {
11909 if (opaque_key != RXD_OPAQUE_RING_STD)
11910 goto out;
11911 } else {
11912 if (opaque_key != RXD_OPAQUE_RING_JUMBO)
11913 goto out;
11914 }
11915 } else if ((desc->type_flags & RXD_FLAG_TCPUDP_CSUM) &&
11916 (desc->ip_tcp_csum & RXD_TCPCSUM_MASK)
Matt Carlson54e0a672011-05-19 12:12:50 +000011917 >> RXD_TCPCSUM_SHIFT != 0xffff) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000011918 goto out;
11919 }
11920
11921 if (opaque_key == RXD_OPAQUE_RING_STD) {
Eric Dumazet9205fd92011-11-18 06:47:01 +000011922 rx_data = tpr->rx_std_buffers[desc_idx].data;
Matt Carlsonbb158d62011-04-25 12:42:47 +000011923 map = dma_unmap_addr(&tpr->rx_std_buffers[desc_idx],
11924 mapping);
11925 } else if (opaque_key == RXD_OPAQUE_RING_JUMBO) {
Eric Dumazet9205fd92011-11-18 06:47:01 +000011926 rx_data = tpr->rx_jmb_buffers[desc_idx].data;
Matt Carlsonbb158d62011-04-25 12:42:47 +000011927 map = dma_unmap_addr(&tpr->rx_jmb_buffers[desc_idx],
11928 mapping);
11929 } else
Matt Carlson4852a862011-04-13 11:05:07 +000011930 goto out;
11931
Matt Carlsonbb158d62011-04-25 12:42:47 +000011932 pci_dma_sync_single_for_cpu(tp->pdev, map, rx_len,
11933 PCI_DMA_FROMDEVICE);
11934
Eric Dumazet9205fd92011-11-18 06:47:01 +000011935 rx_data += TG3_RX_OFFSET(tp);
Matt Carlsonbb158d62011-04-25 12:42:47 +000011936 for (i = data_off; i < rx_len; i++, val++) {
Eric Dumazet9205fd92011-11-18 06:47:01 +000011937 if (*(rx_data + i) != (u8) (val & 0xff))
Matt Carlsonbb158d62011-04-25 12:42:47 +000011938 goto out;
11939 }
Matt Carlson4852a862011-04-13 11:05:07 +000011940 }
11941
Michael Chanc76949a2005-05-29 14:58:59 -070011942 err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011943
Eric Dumazet9205fd92011-11-18 06:47:01 +000011944 /* tg3_free_rings will unmap and free the rx_data */
Michael Chanc76949a2005-05-29 14:58:59 -070011945out:
11946 return err;
11947}
11948
Matt Carlson00c266b2011-04-25 12:42:46 +000011949#define TG3_STD_LOOPBACK_FAILED 1
11950#define TG3_JMB_LOOPBACK_FAILED 2
Matt Carlsonbb158d62011-04-25 12:42:47 +000011951#define TG3_TSO_LOOPBACK_FAILED 4
Matt Carlson28a45952011-08-19 13:58:22 +000011952#define TG3_LOOPBACK_FAILED \
11953 (TG3_STD_LOOPBACK_FAILED | \
11954 TG3_JMB_LOOPBACK_FAILED | \
11955 TG3_TSO_LOOPBACK_FAILED)
Matt Carlson00c266b2011-04-25 12:42:46 +000011956
Matt Carlson941ec902011-08-19 13:58:23 +000011957static int tg3_test_loopback(struct tg3 *tp, u64 *data, bool do_extlpbk)
Michael Chan9f40dea2005-09-05 17:53:06 -070011958{
Matt Carlson28a45952011-08-19 13:58:22 +000011959 int err = -EIO;
Matt Carlson2215e242011-08-19 13:58:19 +000011960 u32 eee_cap;
Michael Chanc441b452012-03-04 14:48:13 +000011961 u32 jmb_pkt_sz = 9000;
11962
11963 if (tp->dma_limit)
11964 jmb_pkt_sz = tp->dma_limit - ETH_HLEN;
Michael Chan9f40dea2005-09-05 17:53:06 -070011965
Matt Carlsonab789042011-01-25 15:58:54 +000011966 eee_cap = tp->phy_flags & TG3_PHYFLG_EEE_CAP;
11967 tp->phy_flags &= ~TG3_PHYFLG_EEE_CAP;
11968
Matt Carlson28a45952011-08-19 13:58:22 +000011969 if (!netif_running(tp->dev)) {
11970 data[0] = TG3_LOOPBACK_FAILED;
11971 data[1] = TG3_LOOPBACK_FAILED;
Matt Carlson941ec902011-08-19 13:58:23 +000011972 if (do_extlpbk)
11973 data[2] = TG3_LOOPBACK_FAILED;
Matt Carlson28a45952011-08-19 13:58:22 +000011974 goto done;
11975 }
11976
Michael Chanb9ec6c12006-07-25 16:37:27 -070011977 err = tg3_reset_hw(tp, 1);
Matt Carlsonab789042011-01-25 15:58:54 +000011978 if (err) {
Matt Carlson28a45952011-08-19 13:58:22 +000011979 data[0] = TG3_LOOPBACK_FAILED;
11980 data[1] = TG3_LOOPBACK_FAILED;
Matt Carlson941ec902011-08-19 13:58:23 +000011981 if (do_extlpbk)
11982 data[2] = TG3_LOOPBACK_FAILED;
Matt Carlsonab789042011-01-25 15:58:54 +000011983 goto done;
11984 }
Michael Chan9f40dea2005-09-05 17:53:06 -070011985
Joe Perches63c3a662011-04-26 08:12:10 +000011986 if (tg3_flag(tp, ENABLE_RSS)) {
Matt Carlson4a85f092011-04-20 07:57:37 +000011987 int i;
11988
11989 /* Reroute all rx packets to the 1st queue */
11990 for (i = MAC_RSS_INDIR_TBL_0;
11991 i < MAC_RSS_INDIR_TBL_0 + TG3_RSS_INDIR_TBL_SIZE; i += 4)
11992 tw32(i, 0x0);
11993 }
11994
Matt Carlson6e01b202011-08-19 13:58:20 +000011995 /* HW errata - mac loopback fails in some cases on 5780.
11996 * Normal traffic and PHY loopback are not affected by
11997 * errata. Also, the MAC loopback test is deprecated for
11998 * all newer ASIC revisions.
11999 */
12000 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5780 &&
12001 !tg3_flag(tp, CPMU_PRESENT)) {
12002 tg3_mac_loopback(tp, true);
Matt Carlson9936bcf2007-10-10 18:03:07 -070012003
Matt Carlson28a45952011-08-19 13:58:22 +000012004 if (tg3_run_loopback(tp, ETH_FRAME_LEN, false))
12005 data[0] |= TG3_STD_LOOPBACK_FAILED;
Matt Carlson6e01b202011-08-19 13:58:20 +000012006
12007 if (tg3_flag(tp, JUMBO_RING_ENABLE) &&
Michael Chanc441b452012-03-04 14:48:13 +000012008 tg3_run_loopback(tp, jmb_pkt_sz + ETH_HLEN, false))
Matt Carlson28a45952011-08-19 13:58:22 +000012009 data[0] |= TG3_JMB_LOOPBACK_FAILED;
Matt Carlson6e01b202011-08-19 13:58:20 +000012010
12011 tg3_mac_loopback(tp, false);
12012 }
Matt Carlson4852a862011-04-13 11:05:07 +000012013
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012014 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
Joe Perches63c3a662011-04-26 08:12:10 +000012015 !tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson5e5a7f32011-08-19 13:58:21 +000012016 int i;
12017
Matt Carlson941ec902011-08-19 13:58:23 +000012018 tg3_phy_lpbk_set(tp, 0, false);
Matt Carlson5e5a7f32011-08-19 13:58:21 +000012019
12020 /* Wait for link */
12021 for (i = 0; i < 100; i++) {
12022 if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
12023 break;
12024 mdelay(1);
12025 }
12026
Matt Carlson28a45952011-08-19 13:58:22 +000012027 if (tg3_run_loopback(tp, ETH_FRAME_LEN, false))
12028 data[1] |= TG3_STD_LOOPBACK_FAILED;
Joe Perches63c3a662011-04-26 08:12:10 +000012029 if (tg3_flag(tp, TSO_CAPABLE) &&
Matt Carlson28a45952011-08-19 13:58:22 +000012030 tg3_run_loopback(tp, ETH_FRAME_LEN, true))
12031 data[1] |= TG3_TSO_LOOPBACK_FAILED;
Joe Perches63c3a662011-04-26 08:12:10 +000012032 if (tg3_flag(tp, JUMBO_RING_ENABLE) &&
Michael Chanc441b452012-03-04 14:48:13 +000012033 tg3_run_loopback(tp, jmb_pkt_sz + ETH_HLEN, false))
Matt Carlson28a45952011-08-19 13:58:22 +000012034 data[1] |= TG3_JMB_LOOPBACK_FAILED;
Michael Chan9f40dea2005-09-05 17:53:06 -070012035
Matt Carlson941ec902011-08-19 13:58:23 +000012036 if (do_extlpbk) {
12037 tg3_phy_lpbk_set(tp, 0, true);
12038
12039 /* All link indications report up, but the hardware
12040 * isn't really ready for about 20 msec. Double it
12041 * to be sure.
12042 */
12043 mdelay(40);
12044
12045 if (tg3_run_loopback(tp, ETH_FRAME_LEN, false))
12046 data[2] |= TG3_STD_LOOPBACK_FAILED;
12047 if (tg3_flag(tp, TSO_CAPABLE) &&
12048 tg3_run_loopback(tp, ETH_FRAME_LEN, true))
12049 data[2] |= TG3_TSO_LOOPBACK_FAILED;
12050 if (tg3_flag(tp, JUMBO_RING_ENABLE) &&
Michael Chanc441b452012-03-04 14:48:13 +000012051 tg3_run_loopback(tp, jmb_pkt_sz + ETH_HLEN, false))
Matt Carlson941ec902011-08-19 13:58:23 +000012052 data[2] |= TG3_JMB_LOOPBACK_FAILED;
12053 }
12054
Matt Carlson5e5a7f32011-08-19 13:58:21 +000012055 /* Re-enable gphy autopowerdown. */
12056 if (tp->phy_flags & TG3_PHYFLG_ENABLE_APD)
12057 tg3_phy_toggle_apd(tp, true);
12058 }
Matt Carlson6833c042008-11-21 17:18:59 -080012059
Matt Carlson941ec902011-08-19 13:58:23 +000012060 err = (data[0] | data[1] | data[2]) ? -EIO : 0;
Matt Carlson28a45952011-08-19 13:58:22 +000012061
Matt Carlsonab789042011-01-25 15:58:54 +000012062done:
12063 tp->phy_flags |= eee_cap;
12064
Michael Chan9f40dea2005-09-05 17:53:06 -070012065 return err;
12066}
12067
Michael Chan4cafd3f2005-05-29 14:56:34 -070012068static void tg3_self_test(struct net_device *dev, struct ethtool_test *etest,
12069 u64 *data)
12070{
Michael Chan566f86a2005-05-29 14:56:58 -070012071 struct tg3 *tp = netdev_priv(dev);
Matt Carlson941ec902011-08-19 13:58:23 +000012072 bool doextlpbk = etest->flags & ETH_TEST_FL_EXTERNAL_LB;
Michael Chan566f86a2005-05-29 14:56:58 -070012073
Matt Carlsonbed98292011-07-13 09:27:29 +000012074 if ((tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) &&
12075 tg3_power_up(tp)) {
12076 etest->flags |= ETH_TEST_FL_FAILED;
12077 memset(data, 1, sizeof(u64) * TG3_NUM_TEST);
12078 return;
12079 }
Michael Chanbc1c7562006-03-20 17:48:03 -080012080
Michael Chan566f86a2005-05-29 14:56:58 -070012081 memset(data, 0, sizeof(u64) * TG3_NUM_TEST);
12082
12083 if (tg3_test_nvram(tp) != 0) {
12084 etest->flags |= ETH_TEST_FL_FAILED;
12085 data[0] = 1;
12086 }
Matt Carlson941ec902011-08-19 13:58:23 +000012087 if (!doextlpbk && tg3_test_link(tp)) {
Michael Chanca430072005-05-29 14:57:23 -070012088 etest->flags |= ETH_TEST_FL_FAILED;
12089 data[1] = 1;
12090 }
Michael Chana71116d2005-05-29 14:58:11 -070012091 if (etest->flags & ETH_TEST_FL_OFFLINE) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012092 int err, err2 = 0, irq_sync = 0;
Michael Chana71116d2005-05-29 14:58:11 -070012093
Michael Chanbbe832c2005-06-24 20:20:04 -070012094 if (netif_running(dev)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012095 tg3_phy_stop(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -070012096 tg3_netif_stop(tp);
12097 irq_sync = 1;
12098 }
12099
12100 tg3_full_lock(tp, irq_sync);
Michael Chana71116d2005-05-29 14:58:11 -070012101
12102 tg3_halt(tp, RESET_KIND_SUSPEND, 1);
Michael Chanec41c7d2006-01-17 02:40:55 -080012103 err = tg3_nvram_lock(tp);
Michael Chana71116d2005-05-29 14:58:11 -070012104 tg3_halt_cpu(tp, RX_CPU_BASE);
Joe Perches63c3a662011-04-26 08:12:10 +000012105 if (!tg3_flag(tp, 5705_PLUS))
Michael Chana71116d2005-05-29 14:58:11 -070012106 tg3_halt_cpu(tp, TX_CPU_BASE);
Michael Chanec41c7d2006-01-17 02:40:55 -080012107 if (!err)
12108 tg3_nvram_unlock(tp);
Michael Chana71116d2005-05-29 14:58:11 -070012109
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012110 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
Michael Chand9ab5ad12006-03-20 22:27:35 -080012111 tg3_phy_reset(tp);
12112
Michael Chana71116d2005-05-29 14:58:11 -070012113 if (tg3_test_registers(tp) != 0) {
12114 etest->flags |= ETH_TEST_FL_FAILED;
12115 data[2] = 1;
12116 }
Matt Carlson28a45952011-08-19 13:58:22 +000012117
Michael Chan7942e1d2005-05-29 14:58:36 -070012118 if (tg3_test_memory(tp) != 0) {
12119 etest->flags |= ETH_TEST_FL_FAILED;
12120 data[3] = 1;
12121 }
Matt Carlson28a45952011-08-19 13:58:22 +000012122
Matt Carlson941ec902011-08-19 13:58:23 +000012123 if (doextlpbk)
12124 etest->flags |= ETH_TEST_FL_EXTERNAL_LB_DONE;
12125
12126 if (tg3_test_loopback(tp, &data[4], doextlpbk))
Michael Chanc76949a2005-05-29 14:58:59 -070012127 etest->flags |= ETH_TEST_FL_FAILED;
Michael Chana71116d2005-05-29 14:58:11 -070012128
David S. Millerf47c11e2005-06-24 20:18:35 -070012129 tg3_full_unlock(tp);
12130
Michael Chand4bc3922005-05-29 14:59:20 -070012131 if (tg3_test_interrupt(tp) != 0) {
12132 etest->flags |= ETH_TEST_FL_FAILED;
Matt Carlson941ec902011-08-19 13:58:23 +000012133 data[7] = 1;
Michael Chand4bc3922005-05-29 14:59:20 -070012134 }
David S. Millerf47c11e2005-06-24 20:18:35 -070012135
12136 tg3_full_lock(tp, 0);
Michael Chand4bc3922005-05-29 14:59:20 -070012137
Michael Chana71116d2005-05-29 14:58:11 -070012138 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
12139 if (netif_running(dev)) {
Joe Perches63c3a662011-04-26 08:12:10 +000012140 tg3_flag_set(tp, INIT_COMPLETE);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012141 err2 = tg3_restart_hw(tp, 1);
12142 if (!err2)
Michael Chanb9ec6c12006-07-25 16:37:27 -070012143 tg3_netif_start(tp);
Michael Chana71116d2005-05-29 14:58:11 -070012144 }
David S. Millerf47c11e2005-06-24 20:18:35 -070012145
12146 tg3_full_unlock(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012147
12148 if (irq_sync && !err2)
12149 tg3_phy_start(tp);
Michael Chana71116d2005-05-29 14:58:11 -070012150 }
Matt Carlson80096062010-08-02 11:26:06 +000012151 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000012152 tg3_power_down(tp);
Michael Chanbc1c7562006-03-20 17:48:03 -080012153
Michael Chan4cafd3f2005-05-29 14:56:34 -070012154}
12155
Linus Torvalds1da177e2005-04-16 15:20:36 -070012156static int tg3_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
12157{
12158 struct mii_ioctl_data *data = if_mii(ifr);
12159 struct tg3 *tp = netdev_priv(dev);
12160 int err;
12161
Joe Perches63c3a662011-04-26 08:12:10 +000012162 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000012163 struct phy_device *phydev;
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012164 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012165 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000012166 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Richard Cochran28b04112010-07-17 08:48:55 +000012167 return phy_mii_ioctl(phydev, ifr, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012168 }
12169
Matt Carlson33f401a2010-04-05 10:19:27 +000012170 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012171 case SIOCGMIIPHY:
Matt Carlson882e9792009-09-01 13:21:36 +000012172 data->phy_id = tp->phy_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012173
12174 /* fallthru */
12175 case SIOCGMIIREG: {
12176 u32 mii_regval;
12177
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012178 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012179 break; /* We have no PHY */
12180
Matt Carlson34eea5a2011-04-20 07:57:38 +000012181 if (!netif_running(dev))
Michael Chanbc1c7562006-03-20 17:48:03 -080012182 return -EAGAIN;
12183
David S. Millerf47c11e2005-06-24 20:18:35 -070012184 spin_lock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012185 err = tg3_readphy(tp, data->reg_num & 0x1f, &mii_regval);
David S. Millerf47c11e2005-06-24 20:18:35 -070012186 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012187
12188 data->val_out = mii_regval;
12189
12190 return err;
12191 }
12192
12193 case SIOCSMIIREG:
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012194 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012195 break; /* We have no PHY */
12196
Matt Carlson34eea5a2011-04-20 07:57:38 +000012197 if (!netif_running(dev))
Michael Chanbc1c7562006-03-20 17:48:03 -080012198 return -EAGAIN;
12199
David S. Millerf47c11e2005-06-24 20:18:35 -070012200 spin_lock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012201 err = tg3_writephy(tp, data->reg_num & 0x1f, data->val_in);
David S. Millerf47c11e2005-06-24 20:18:35 -070012202 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012203
12204 return err;
12205
12206 default:
12207 /* do nothing */
12208 break;
12209 }
12210 return -EOPNOTSUPP;
12211}
12212
David S. Miller15f98502005-05-18 22:49:26 -070012213static int tg3_get_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
12214{
12215 struct tg3 *tp = netdev_priv(dev);
12216
12217 memcpy(ec, &tp->coal, sizeof(*ec));
12218 return 0;
12219}
12220
Michael Chand244c892005-07-05 14:42:33 -070012221static int tg3_set_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
12222{
12223 struct tg3 *tp = netdev_priv(dev);
12224 u32 max_rxcoal_tick_int = 0, max_txcoal_tick_int = 0;
12225 u32 max_stat_coal_ticks = 0, min_stat_coal_ticks = 0;
12226
Joe Perches63c3a662011-04-26 08:12:10 +000012227 if (!tg3_flag(tp, 5705_PLUS)) {
Michael Chand244c892005-07-05 14:42:33 -070012228 max_rxcoal_tick_int = MAX_RXCOAL_TICK_INT;
12229 max_txcoal_tick_int = MAX_TXCOAL_TICK_INT;
12230 max_stat_coal_ticks = MAX_STAT_COAL_TICKS;
12231 min_stat_coal_ticks = MIN_STAT_COAL_TICKS;
12232 }
12233
12234 if ((ec->rx_coalesce_usecs > MAX_RXCOL_TICKS) ||
12235 (ec->tx_coalesce_usecs > MAX_TXCOL_TICKS) ||
12236 (ec->rx_max_coalesced_frames > MAX_RXMAX_FRAMES) ||
12237 (ec->tx_max_coalesced_frames > MAX_TXMAX_FRAMES) ||
12238 (ec->rx_coalesce_usecs_irq > max_rxcoal_tick_int) ||
12239 (ec->tx_coalesce_usecs_irq > max_txcoal_tick_int) ||
12240 (ec->rx_max_coalesced_frames_irq > MAX_RXCOAL_MAXF_INT) ||
12241 (ec->tx_max_coalesced_frames_irq > MAX_TXCOAL_MAXF_INT) ||
12242 (ec->stats_block_coalesce_usecs > max_stat_coal_ticks) ||
12243 (ec->stats_block_coalesce_usecs < min_stat_coal_ticks))
12244 return -EINVAL;
12245
12246 /* No rx interrupts will be generated if both are zero */
12247 if ((ec->rx_coalesce_usecs == 0) &&
12248 (ec->rx_max_coalesced_frames == 0))
12249 return -EINVAL;
12250
12251 /* No tx interrupts will be generated if both are zero */
12252 if ((ec->tx_coalesce_usecs == 0) &&
12253 (ec->tx_max_coalesced_frames == 0))
12254 return -EINVAL;
12255
12256 /* Only copy relevant parameters, ignore all others. */
12257 tp->coal.rx_coalesce_usecs = ec->rx_coalesce_usecs;
12258 tp->coal.tx_coalesce_usecs = ec->tx_coalesce_usecs;
12259 tp->coal.rx_max_coalesced_frames = ec->rx_max_coalesced_frames;
12260 tp->coal.tx_max_coalesced_frames = ec->tx_max_coalesced_frames;
12261 tp->coal.rx_coalesce_usecs_irq = ec->rx_coalesce_usecs_irq;
12262 tp->coal.tx_coalesce_usecs_irq = ec->tx_coalesce_usecs_irq;
12263 tp->coal.rx_max_coalesced_frames_irq = ec->rx_max_coalesced_frames_irq;
12264 tp->coal.tx_max_coalesced_frames_irq = ec->tx_max_coalesced_frames_irq;
12265 tp->coal.stats_block_coalesce_usecs = ec->stats_block_coalesce_usecs;
12266
12267 if (netif_running(dev)) {
12268 tg3_full_lock(tp, 0);
12269 __tg3_set_coalesce(tp, &tp->coal);
12270 tg3_full_unlock(tp);
12271 }
12272 return 0;
12273}
12274
Jeff Garzik7282d492006-09-13 14:30:00 -040012275static const struct ethtool_ops tg3_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012276 .get_settings = tg3_get_settings,
12277 .set_settings = tg3_set_settings,
12278 .get_drvinfo = tg3_get_drvinfo,
12279 .get_regs_len = tg3_get_regs_len,
12280 .get_regs = tg3_get_regs,
12281 .get_wol = tg3_get_wol,
12282 .set_wol = tg3_set_wol,
12283 .get_msglevel = tg3_get_msglevel,
12284 .set_msglevel = tg3_set_msglevel,
12285 .nway_reset = tg3_nway_reset,
12286 .get_link = ethtool_op_get_link,
12287 .get_eeprom_len = tg3_get_eeprom_len,
12288 .get_eeprom = tg3_get_eeprom,
12289 .set_eeprom = tg3_set_eeprom,
12290 .get_ringparam = tg3_get_ringparam,
12291 .set_ringparam = tg3_set_ringparam,
12292 .get_pauseparam = tg3_get_pauseparam,
12293 .set_pauseparam = tg3_set_pauseparam,
Michael Chan4cafd3f2005-05-29 14:56:34 -070012294 .self_test = tg3_self_test,
Linus Torvalds1da177e2005-04-16 15:20:36 -070012295 .get_strings = tg3_get_strings,
stephen hemminger81b87092011-04-04 08:43:50 +000012296 .set_phys_id = tg3_set_phys_id,
Linus Torvalds1da177e2005-04-16 15:20:36 -070012297 .get_ethtool_stats = tg3_get_ethtool_stats,
David S. Miller15f98502005-05-18 22:49:26 -070012298 .get_coalesce = tg3_get_coalesce,
Michael Chand244c892005-07-05 14:42:33 -070012299 .set_coalesce = tg3_set_coalesce,
Jeff Garzikb9f2c042007-10-03 18:07:32 -070012300 .get_sset_count = tg3_get_sset_count,
Matt Carlson90415472011-12-16 13:33:23 +000012301 .get_rxnfc = tg3_get_rxnfc,
12302 .get_rxfh_indir_size = tg3_get_rxfh_indir_size,
12303 .get_rxfh_indir = tg3_get_rxfh_indir,
12304 .set_rxfh_indir = tg3_set_rxfh_indir,
Richard Cochran3f847492012-04-03 22:59:39 +000012305 .get_ts_info = ethtool_op_get_ts_info,
Linus Torvalds1da177e2005-04-16 15:20:36 -070012306};
12307
David S. Millerb4017c52012-03-01 17:57:40 -050012308static struct rtnl_link_stats64 *tg3_get_stats64(struct net_device *dev,
12309 struct rtnl_link_stats64 *stats)
12310{
12311 struct tg3 *tp = netdev_priv(dev);
12312
12313 if (!tp->hw_stats)
12314 return &tp->net_stats_prev;
12315
12316 spin_lock_bh(&tp->lock);
12317 tg3_get_nstats(tp, stats);
12318 spin_unlock_bh(&tp->lock);
12319
12320 return stats;
12321}
12322
Matt Carlsonccd5ba92012-02-13 10:20:08 +000012323static void tg3_set_rx_mode(struct net_device *dev)
12324{
12325 struct tg3 *tp = netdev_priv(dev);
12326
12327 if (!netif_running(dev))
12328 return;
12329
12330 tg3_full_lock(tp, 0);
12331 __tg3_set_rx_mode(dev);
12332 tg3_full_unlock(tp);
12333}
12334
Matt Carlsonfaf16272012-02-13 10:20:07 +000012335static inline void tg3_set_mtu(struct net_device *dev, struct tg3 *tp,
12336 int new_mtu)
12337{
12338 dev->mtu = new_mtu;
12339
12340 if (new_mtu > ETH_DATA_LEN) {
12341 if (tg3_flag(tp, 5780_CLASS)) {
12342 netdev_update_features(dev);
12343 tg3_flag_clear(tp, TSO_CAPABLE);
12344 } else {
12345 tg3_flag_set(tp, JUMBO_RING_ENABLE);
12346 }
12347 } else {
12348 if (tg3_flag(tp, 5780_CLASS)) {
12349 tg3_flag_set(tp, TSO_CAPABLE);
12350 netdev_update_features(dev);
12351 }
12352 tg3_flag_clear(tp, JUMBO_RING_ENABLE);
12353 }
12354}
12355
12356static int tg3_change_mtu(struct net_device *dev, int new_mtu)
12357{
12358 struct tg3 *tp = netdev_priv(dev);
Michael Chan2fae5e32012-03-04 14:48:15 +000012359 int err, reset_phy = 0;
Matt Carlsonfaf16272012-02-13 10:20:07 +000012360
12361 if (new_mtu < TG3_MIN_MTU || new_mtu > TG3_MAX_MTU(tp))
12362 return -EINVAL;
12363
12364 if (!netif_running(dev)) {
12365 /* We'll just catch it later when the
12366 * device is up'd.
12367 */
12368 tg3_set_mtu(dev, tp, new_mtu);
12369 return 0;
12370 }
12371
12372 tg3_phy_stop(tp);
12373
12374 tg3_netif_stop(tp);
12375
12376 tg3_full_lock(tp, 1);
12377
12378 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
12379
12380 tg3_set_mtu(dev, tp, new_mtu);
12381
Michael Chan2fae5e32012-03-04 14:48:15 +000012382 /* Reset PHY, otherwise the read DMA engine will be in a mode that
12383 * breaks all requests to 256 bytes.
12384 */
12385 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57766)
12386 reset_phy = 1;
12387
12388 err = tg3_restart_hw(tp, reset_phy);
Matt Carlsonfaf16272012-02-13 10:20:07 +000012389
12390 if (!err)
12391 tg3_netif_start(tp);
12392
12393 tg3_full_unlock(tp);
12394
12395 if (!err)
12396 tg3_phy_start(tp);
12397
12398 return err;
12399}
12400
12401static const struct net_device_ops tg3_netdev_ops = {
12402 .ndo_open = tg3_open,
12403 .ndo_stop = tg3_close,
12404 .ndo_start_xmit = tg3_start_xmit,
12405 .ndo_get_stats64 = tg3_get_stats64,
12406 .ndo_validate_addr = eth_validate_addr,
12407 .ndo_set_rx_mode = tg3_set_rx_mode,
12408 .ndo_set_mac_address = tg3_set_mac_addr,
12409 .ndo_do_ioctl = tg3_ioctl,
12410 .ndo_tx_timeout = tg3_tx_timeout,
12411 .ndo_change_mtu = tg3_change_mtu,
12412 .ndo_fix_features = tg3_fix_features,
12413 .ndo_set_features = tg3_set_features,
12414#ifdef CONFIG_NET_POLL_CONTROLLER
12415 .ndo_poll_controller = tg3_poll_controller,
12416#endif
12417};
12418
Linus Torvalds1da177e2005-04-16 15:20:36 -070012419static void __devinit tg3_get_eeprom_size(struct tg3 *tp)
12420{
Michael Chan1b277772006-03-20 22:27:48 -080012421 u32 cursize, val, magic;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012422
12423 tp->nvram_size = EEPROM_CHIP_SIZE;
12424
Matt Carlsone4f34112009-02-25 14:25:00 +000012425 if (tg3_nvram_read(tp, 0, &magic) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012426 return;
12427
Michael Chanb16250e2006-09-27 16:10:14 -070012428 if ((magic != TG3_EEPROM_MAGIC) &&
12429 ((magic & TG3_EEPROM_MAGIC_FW_MSK) != TG3_EEPROM_MAGIC_FW) &&
12430 ((magic & TG3_EEPROM_MAGIC_HW_MSK) != TG3_EEPROM_MAGIC_HW))
Linus Torvalds1da177e2005-04-16 15:20:36 -070012431 return;
12432
12433 /*
12434 * Size the chip by reading offsets at increasing powers of two.
12435 * When we encounter our validation signature, we know the addressing
12436 * has wrapped around, and thus have our chip size.
12437 */
Michael Chan1b277772006-03-20 22:27:48 -080012438 cursize = 0x10;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012439
12440 while (cursize < tp->nvram_size) {
Matt Carlsone4f34112009-02-25 14:25:00 +000012441 if (tg3_nvram_read(tp, cursize, &val) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012442 return;
12443
Michael Chan18201802006-03-20 22:29:15 -080012444 if (val == magic)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012445 break;
12446
12447 cursize <<= 1;
12448 }
12449
12450 tp->nvram_size = cursize;
12451}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012452
Linus Torvalds1da177e2005-04-16 15:20:36 -070012453static void __devinit tg3_get_nvram_size(struct tg3 *tp)
12454{
12455 u32 val;
12456
Joe Perches63c3a662011-04-26 08:12:10 +000012457 if (tg3_flag(tp, NO_NVRAM) || tg3_nvram_read(tp, 0, &val) != 0)
Michael Chan1b277772006-03-20 22:27:48 -080012458 return;
12459
12460 /* Selfboot format */
Michael Chan18201802006-03-20 22:29:15 -080012461 if (val != TG3_EEPROM_MAGIC) {
Michael Chan1b277772006-03-20 22:27:48 -080012462 tg3_get_eeprom_size(tp);
12463 return;
12464 }
12465
Matt Carlson6d348f22009-02-25 14:25:52 +000012466 if (tg3_nvram_read(tp, 0xf0, &val) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012467 if (val != 0) {
Matt Carlson6d348f22009-02-25 14:25:52 +000012468 /* This is confusing. We want to operate on the
12469 * 16-bit value at offset 0xf2. The tg3_nvram_read()
12470 * call will read from NVRAM and byteswap the data
12471 * according to the byteswapping settings for all
12472 * other register accesses. This ensures the data we
12473 * want will always reside in the lower 16-bits.
12474 * However, the data in NVRAM is in LE format, which
12475 * means the data from the NVRAM read will always be
12476 * opposite the endianness of the CPU. The 16-bit
12477 * byteswap then brings the data to CPU endianness.
12478 */
12479 tp->nvram_size = swab16((u16)(val & 0x0000ffff)) * 1024;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012480 return;
12481 }
12482 }
Matt Carlsonfd1122a2008-05-02 16:48:36 -070012483 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012484}
12485
12486static void __devinit tg3_get_nvram_info(struct tg3 *tp)
12487{
12488 u32 nvcfg1;
12489
12490 nvcfg1 = tr32(NVRAM_CFG1);
12491 if (nvcfg1 & NVRAM_CFG1_FLASHIF_ENAB) {
Joe Perches63c3a662011-04-26 08:12:10 +000012492 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000012493 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012494 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
12495 tw32(NVRAM_CFG1, nvcfg1);
12496 }
12497
Matt Carlson6ff6f812011-05-19 12:12:54 +000012498 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 ||
Joe Perches63c3a662011-04-26 08:12:10 +000012499 tg3_flag(tp, 5780_CLASS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012500 switch (nvcfg1 & NVRAM_CFG1_VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000012501 case FLASH_VENDOR_ATMEL_FLASH_BUFFERED:
12502 tp->nvram_jedecnum = JEDEC_ATMEL;
12503 tp->nvram_pagesize = ATMEL_AT45DB0X1B_PAGE_SIZE;
Joe Perches63c3a662011-04-26 08:12:10 +000012504 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000012505 break;
12506 case FLASH_VENDOR_ATMEL_FLASH_UNBUFFERED:
12507 tp->nvram_jedecnum = JEDEC_ATMEL;
12508 tp->nvram_pagesize = ATMEL_AT25F512_PAGE_SIZE;
12509 break;
12510 case FLASH_VENDOR_ATMEL_EEPROM:
12511 tp->nvram_jedecnum = JEDEC_ATMEL;
12512 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
Joe Perches63c3a662011-04-26 08:12:10 +000012513 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000012514 break;
12515 case FLASH_VENDOR_ST:
12516 tp->nvram_jedecnum = JEDEC_ST;
12517 tp->nvram_pagesize = ST_M45PEX0_PAGE_SIZE;
Joe Perches63c3a662011-04-26 08:12:10 +000012518 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000012519 break;
12520 case FLASH_VENDOR_SAIFUN:
12521 tp->nvram_jedecnum = JEDEC_SAIFUN;
12522 tp->nvram_pagesize = SAIFUN_SA25F0XX_PAGE_SIZE;
12523 break;
12524 case FLASH_VENDOR_SST_SMALL:
12525 case FLASH_VENDOR_SST_LARGE:
12526 tp->nvram_jedecnum = JEDEC_SST;
12527 tp->nvram_pagesize = SST_25VF0X0_PAGE_SIZE;
12528 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012529 }
Matt Carlson8590a602009-08-28 12:29:16 +000012530 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012531 tp->nvram_jedecnum = JEDEC_ATMEL;
12532 tp->nvram_pagesize = ATMEL_AT45DB0X1B_PAGE_SIZE;
Joe Perches63c3a662011-04-26 08:12:10 +000012533 tg3_flag_set(tp, NVRAM_BUFFERED);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012534 }
12535}
12536
Matt Carlsona1b950d2009-09-01 13:20:17 +000012537static void __devinit tg3_nvram_get_pagesize(struct tg3 *tp, u32 nvmcfg1)
12538{
12539 switch (nvmcfg1 & NVRAM_CFG1_5752PAGE_SIZE_MASK) {
12540 case FLASH_5752PAGE_SIZE_256:
12541 tp->nvram_pagesize = 256;
12542 break;
12543 case FLASH_5752PAGE_SIZE_512:
12544 tp->nvram_pagesize = 512;
12545 break;
12546 case FLASH_5752PAGE_SIZE_1K:
12547 tp->nvram_pagesize = 1024;
12548 break;
12549 case FLASH_5752PAGE_SIZE_2K:
12550 tp->nvram_pagesize = 2048;
12551 break;
12552 case FLASH_5752PAGE_SIZE_4K:
12553 tp->nvram_pagesize = 4096;
12554 break;
12555 case FLASH_5752PAGE_SIZE_264:
12556 tp->nvram_pagesize = 264;
12557 break;
12558 case FLASH_5752PAGE_SIZE_528:
12559 tp->nvram_pagesize = 528;
12560 break;
12561 }
12562}
12563
Michael Chan361b4ac2005-04-21 17:11:21 -070012564static void __devinit tg3_get_5752_nvram_info(struct tg3 *tp)
12565{
12566 u32 nvcfg1;
12567
12568 nvcfg1 = tr32(NVRAM_CFG1);
12569
Michael Chane6af3012005-04-21 17:12:05 -070012570 /* NVRAM protection for TPM */
12571 if (nvcfg1 & (1 << 27))
Joe Perches63c3a662011-04-26 08:12:10 +000012572 tg3_flag_set(tp, PROTECTED_NVRAM);
Michael Chane6af3012005-04-21 17:12:05 -070012573
Michael Chan361b4ac2005-04-21 17:11:21 -070012574 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000012575 case FLASH_5752VENDOR_ATMEL_EEPROM_64KHZ:
12576 case FLASH_5752VENDOR_ATMEL_EEPROM_376KHZ:
12577 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012578 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000012579 break;
12580 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
12581 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012582 tg3_flag_set(tp, NVRAM_BUFFERED);
12583 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000012584 break;
12585 case FLASH_5752VENDOR_ST_M45PE10:
12586 case FLASH_5752VENDOR_ST_M45PE20:
12587 case FLASH_5752VENDOR_ST_M45PE40:
12588 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000012589 tg3_flag_set(tp, NVRAM_BUFFERED);
12590 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000012591 break;
Michael Chan361b4ac2005-04-21 17:11:21 -070012592 }
12593
Joe Perches63c3a662011-04-26 08:12:10 +000012594 if (tg3_flag(tp, FLASH)) {
Matt Carlsona1b950d2009-09-01 13:20:17 +000012595 tg3_nvram_get_pagesize(tp, nvcfg1);
Matt Carlson8590a602009-08-28 12:29:16 +000012596 } else {
Michael Chan361b4ac2005-04-21 17:11:21 -070012597 /* For eeprom, set pagesize to maximum eeprom size */
12598 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
12599
12600 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
12601 tw32(NVRAM_CFG1, nvcfg1);
12602 }
12603}
12604
Michael Chand3c7b882006-03-23 01:28:25 -080012605static void __devinit tg3_get_5755_nvram_info(struct tg3 *tp)
12606{
Matt Carlson989a9d22007-05-05 11:51:05 -070012607 u32 nvcfg1, protect = 0;
Michael Chand3c7b882006-03-23 01:28:25 -080012608
12609 nvcfg1 = tr32(NVRAM_CFG1);
12610
12611 /* NVRAM protection for TPM */
Matt Carlson989a9d22007-05-05 11:51:05 -070012612 if (nvcfg1 & (1 << 27)) {
Joe Perches63c3a662011-04-26 08:12:10 +000012613 tg3_flag_set(tp, PROTECTED_NVRAM);
Matt Carlson989a9d22007-05-05 11:51:05 -070012614 protect = 1;
12615 }
Michael Chand3c7b882006-03-23 01:28:25 -080012616
Matt Carlson989a9d22007-05-05 11:51:05 -070012617 nvcfg1 &= NVRAM_CFG1_5752VENDOR_MASK;
12618 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000012619 case FLASH_5755VENDOR_ATMEL_FLASH_1:
12620 case FLASH_5755VENDOR_ATMEL_FLASH_2:
12621 case FLASH_5755VENDOR_ATMEL_FLASH_3:
12622 case FLASH_5755VENDOR_ATMEL_FLASH_5:
12623 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012624 tg3_flag_set(tp, NVRAM_BUFFERED);
12625 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000012626 tp->nvram_pagesize = 264;
12627 if (nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_1 ||
12628 nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_5)
12629 tp->nvram_size = (protect ? 0x3e200 :
12630 TG3_NVRAM_SIZE_512KB);
12631 else if (nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_2)
12632 tp->nvram_size = (protect ? 0x1f200 :
12633 TG3_NVRAM_SIZE_256KB);
12634 else
12635 tp->nvram_size = (protect ? 0x1f200 :
12636 TG3_NVRAM_SIZE_128KB);
12637 break;
12638 case FLASH_5752VENDOR_ST_M45PE10:
12639 case FLASH_5752VENDOR_ST_M45PE20:
12640 case FLASH_5752VENDOR_ST_M45PE40:
12641 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000012642 tg3_flag_set(tp, NVRAM_BUFFERED);
12643 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000012644 tp->nvram_pagesize = 256;
12645 if (nvcfg1 == FLASH_5752VENDOR_ST_M45PE10)
12646 tp->nvram_size = (protect ?
12647 TG3_NVRAM_SIZE_64KB :
12648 TG3_NVRAM_SIZE_128KB);
12649 else if (nvcfg1 == FLASH_5752VENDOR_ST_M45PE20)
12650 tp->nvram_size = (protect ?
12651 TG3_NVRAM_SIZE_64KB :
12652 TG3_NVRAM_SIZE_256KB);
12653 else
12654 tp->nvram_size = (protect ?
12655 TG3_NVRAM_SIZE_128KB :
12656 TG3_NVRAM_SIZE_512KB);
12657 break;
Michael Chand3c7b882006-03-23 01:28:25 -080012658 }
12659}
12660
Michael Chan1b277772006-03-20 22:27:48 -080012661static void __devinit tg3_get_5787_nvram_info(struct tg3 *tp)
12662{
12663 u32 nvcfg1;
12664
12665 nvcfg1 = tr32(NVRAM_CFG1);
12666
12667 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000012668 case FLASH_5787VENDOR_ATMEL_EEPROM_64KHZ:
12669 case FLASH_5787VENDOR_ATMEL_EEPROM_376KHZ:
12670 case FLASH_5787VENDOR_MICRO_EEPROM_64KHZ:
12671 case FLASH_5787VENDOR_MICRO_EEPROM_376KHZ:
12672 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012673 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000012674 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
Michael Chan1b277772006-03-20 22:27:48 -080012675
Matt Carlson8590a602009-08-28 12:29:16 +000012676 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
12677 tw32(NVRAM_CFG1, nvcfg1);
12678 break;
12679 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
12680 case FLASH_5755VENDOR_ATMEL_FLASH_1:
12681 case FLASH_5755VENDOR_ATMEL_FLASH_2:
12682 case FLASH_5755VENDOR_ATMEL_FLASH_3:
12683 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012684 tg3_flag_set(tp, NVRAM_BUFFERED);
12685 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000012686 tp->nvram_pagesize = 264;
12687 break;
12688 case FLASH_5752VENDOR_ST_M45PE10:
12689 case FLASH_5752VENDOR_ST_M45PE20:
12690 case FLASH_5752VENDOR_ST_M45PE40:
12691 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000012692 tg3_flag_set(tp, NVRAM_BUFFERED);
12693 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000012694 tp->nvram_pagesize = 256;
12695 break;
Michael Chan1b277772006-03-20 22:27:48 -080012696 }
12697}
12698
Matt Carlson6b91fa02007-10-10 18:01:09 -070012699static void __devinit tg3_get_5761_nvram_info(struct tg3 *tp)
12700{
12701 u32 nvcfg1, protect = 0;
12702
12703 nvcfg1 = tr32(NVRAM_CFG1);
12704
12705 /* NVRAM protection for TPM */
12706 if (nvcfg1 & (1 << 27)) {
Joe Perches63c3a662011-04-26 08:12:10 +000012707 tg3_flag_set(tp, PROTECTED_NVRAM);
Matt Carlson6b91fa02007-10-10 18:01:09 -070012708 protect = 1;
12709 }
12710
12711 nvcfg1 &= NVRAM_CFG1_5752VENDOR_MASK;
12712 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000012713 case FLASH_5761VENDOR_ATMEL_ADB021D:
12714 case FLASH_5761VENDOR_ATMEL_ADB041D:
12715 case FLASH_5761VENDOR_ATMEL_ADB081D:
12716 case FLASH_5761VENDOR_ATMEL_ADB161D:
12717 case FLASH_5761VENDOR_ATMEL_MDB021D:
12718 case FLASH_5761VENDOR_ATMEL_MDB041D:
12719 case FLASH_5761VENDOR_ATMEL_MDB081D:
12720 case FLASH_5761VENDOR_ATMEL_MDB161D:
12721 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012722 tg3_flag_set(tp, NVRAM_BUFFERED);
12723 tg3_flag_set(tp, FLASH);
12724 tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
Matt Carlson8590a602009-08-28 12:29:16 +000012725 tp->nvram_pagesize = 256;
12726 break;
12727 case FLASH_5761VENDOR_ST_A_M45PE20:
12728 case FLASH_5761VENDOR_ST_A_M45PE40:
12729 case FLASH_5761VENDOR_ST_A_M45PE80:
12730 case FLASH_5761VENDOR_ST_A_M45PE16:
12731 case FLASH_5761VENDOR_ST_M_M45PE20:
12732 case FLASH_5761VENDOR_ST_M_M45PE40:
12733 case FLASH_5761VENDOR_ST_M_M45PE80:
12734 case FLASH_5761VENDOR_ST_M_M45PE16:
12735 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000012736 tg3_flag_set(tp, NVRAM_BUFFERED);
12737 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000012738 tp->nvram_pagesize = 256;
12739 break;
Matt Carlson6b91fa02007-10-10 18:01:09 -070012740 }
12741
12742 if (protect) {
12743 tp->nvram_size = tr32(NVRAM_ADDR_LOCKOUT);
12744 } else {
12745 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000012746 case FLASH_5761VENDOR_ATMEL_ADB161D:
12747 case FLASH_5761VENDOR_ATMEL_MDB161D:
12748 case FLASH_5761VENDOR_ST_A_M45PE16:
12749 case FLASH_5761VENDOR_ST_M_M45PE16:
12750 tp->nvram_size = TG3_NVRAM_SIZE_2MB;
12751 break;
12752 case FLASH_5761VENDOR_ATMEL_ADB081D:
12753 case FLASH_5761VENDOR_ATMEL_MDB081D:
12754 case FLASH_5761VENDOR_ST_A_M45PE80:
12755 case FLASH_5761VENDOR_ST_M_M45PE80:
12756 tp->nvram_size = TG3_NVRAM_SIZE_1MB;
12757 break;
12758 case FLASH_5761VENDOR_ATMEL_ADB041D:
12759 case FLASH_5761VENDOR_ATMEL_MDB041D:
12760 case FLASH_5761VENDOR_ST_A_M45PE40:
12761 case FLASH_5761VENDOR_ST_M_M45PE40:
12762 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
12763 break;
12764 case FLASH_5761VENDOR_ATMEL_ADB021D:
12765 case FLASH_5761VENDOR_ATMEL_MDB021D:
12766 case FLASH_5761VENDOR_ST_A_M45PE20:
12767 case FLASH_5761VENDOR_ST_M_M45PE20:
12768 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
12769 break;
Matt Carlson6b91fa02007-10-10 18:01:09 -070012770 }
12771 }
12772}
12773
Michael Chanb5d37722006-09-27 16:06:21 -070012774static void __devinit tg3_get_5906_nvram_info(struct tg3 *tp)
12775{
12776 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012777 tg3_flag_set(tp, NVRAM_BUFFERED);
Michael Chanb5d37722006-09-27 16:06:21 -070012778 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
12779}
12780
Matt Carlson321d32a2008-11-21 17:22:19 -080012781static void __devinit tg3_get_57780_nvram_info(struct tg3 *tp)
12782{
12783 u32 nvcfg1;
12784
12785 nvcfg1 = tr32(NVRAM_CFG1);
12786
12787 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
12788 case FLASH_5787VENDOR_ATMEL_EEPROM_376KHZ:
12789 case FLASH_5787VENDOR_MICRO_EEPROM_376KHZ:
12790 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012791 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson321d32a2008-11-21 17:22:19 -080012792 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
12793
12794 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
12795 tw32(NVRAM_CFG1, nvcfg1);
12796 return;
12797 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
12798 case FLASH_57780VENDOR_ATMEL_AT45DB011D:
12799 case FLASH_57780VENDOR_ATMEL_AT45DB011B:
12800 case FLASH_57780VENDOR_ATMEL_AT45DB021D:
12801 case FLASH_57780VENDOR_ATMEL_AT45DB021B:
12802 case FLASH_57780VENDOR_ATMEL_AT45DB041D:
12803 case FLASH_57780VENDOR_ATMEL_AT45DB041B:
12804 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012805 tg3_flag_set(tp, NVRAM_BUFFERED);
12806 tg3_flag_set(tp, FLASH);
Matt Carlson321d32a2008-11-21 17:22:19 -080012807
12808 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
12809 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
12810 case FLASH_57780VENDOR_ATMEL_AT45DB011D:
12811 case FLASH_57780VENDOR_ATMEL_AT45DB011B:
12812 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
12813 break;
12814 case FLASH_57780VENDOR_ATMEL_AT45DB021D:
12815 case FLASH_57780VENDOR_ATMEL_AT45DB021B:
12816 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
12817 break;
12818 case FLASH_57780VENDOR_ATMEL_AT45DB041D:
12819 case FLASH_57780VENDOR_ATMEL_AT45DB041B:
12820 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
12821 break;
12822 }
12823 break;
12824 case FLASH_5752VENDOR_ST_M45PE10:
12825 case FLASH_5752VENDOR_ST_M45PE20:
12826 case FLASH_5752VENDOR_ST_M45PE40:
12827 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000012828 tg3_flag_set(tp, NVRAM_BUFFERED);
12829 tg3_flag_set(tp, FLASH);
Matt Carlson321d32a2008-11-21 17:22:19 -080012830
12831 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
12832 case FLASH_5752VENDOR_ST_M45PE10:
12833 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
12834 break;
12835 case FLASH_5752VENDOR_ST_M45PE20:
12836 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
12837 break;
12838 case FLASH_5752VENDOR_ST_M45PE40:
12839 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
12840 break;
12841 }
12842 break;
12843 default:
Joe Perches63c3a662011-04-26 08:12:10 +000012844 tg3_flag_set(tp, NO_NVRAM);
Matt Carlson321d32a2008-11-21 17:22:19 -080012845 return;
12846 }
12847
Matt Carlsona1b950d2009-09-01 13:20:17 +000012848 tg3_nvram_get_pagesize(tp, nvcfg1);
12849 if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
Joe Perches63c3a662011-04-26 08:12:10 +000012850 tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
Matt Carlsona1b950d2009-09-01 13:20:17 +000012851}
12852
12853
12854static void __devinit tg3_get_5717_nvram_info(struct tg3 *tp)
12855{
12856 u32 nvcfg1;
12857
12858 nvcfg1 = tr32(NVRAM_CFG1);
12859
12860 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
12861 case FLASH_5717VENDOR_ATMEL_EEPROM:
12862 case FLASH_5717VENDOR_MICRO_EEPROM:
12863 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012864 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlsona1b950d2009-09-01 13:20:17 +000012865 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
12866
12867 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
12868 tw32(NVRAM_CFG1, nvcfg1);
12869 return;
12870 case FLASH_5717VENDOR_ATMEL_MDB011D:
12871 case FLASH_5717VENDOR_ATMEL_ADB011B:
12872 case FLASH_5717VENDOR_ATMEL_ADB011D:
12873 case FLASH_5717VENDOR_ATMEL_MDB021D:
12874 case FLASH_5717VENDOR_ATMEL_ADB021B:
12875 case FLASH_5717VENDOR_ATMEL_ADB021D:
12876 case FLASH_5717VENDOR_ATMEL_45USPT:
12877 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012878 tg3_flag_set(tp, NVRAM_BUFFERED);
12879 tg3_flag_set(tp, FLASH);
Matt Carlsona1b950d2009-09-01 13:20:17 +000012880
12881 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
12882 case FLASH_5717VENDOR_ATMEL_MDB021D:
Matt Carlson66ee33b2011-04-05 14:22:51 +000012883 /* Detect size with tg3_nvram_get_size() */
12884 break;
Matt Carlsona1b950d2009-09-01 13:20:17 +000012885 case FLASH_5717VENDOR_ATMEL_ADB021B:
12886 case FLASH_5717VENDOR_ATMEL_ADB021D:
12887 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
12888 break;
12889 default:
12890 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
12891 break;
12892 }
Matt Carlson321d32a2008-11-21 17:22:19 -080012893 break;
Matt Carlsona1b950d2009-09-01 13:20:17 +000012894 case FLASH_5717VENDOR_ST_M_M25PE10:
12895 case FLASH_5717VENDOR_ST_A_M25PE10:
12896 case FLASH_5717VENDOR_ST_M_M45PE10:
12897 case FLASH_5717VENDOR_ST_A_M45PE10:
12898 case FLASH_5717VENDOR_ST_M_M25PE20:
12899 case FLASH_5717VENDOR_ST_A_M25PE20:
12900 case FLASH_5717VENDOR_ST_M_M45PE20:
12901 case FLASH_5717VENDOR_ST_A_M45PE20:
12902 case FLASH_5717VENDOR_ST_25USPT:
12903 case FLASH_5717VENDOR_ST_45USPT:
12904 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000012905 tg3_flag_set(tp, NVRAM_BUFFERED);
12906 tg3_flag_set(tp, FLASH);
Matt Carlsona1b950d2009-09-01 13:20:17 +000012907
12908 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
12909 case FLASH_5717VENDOR_ST_M_M25PE20:
Matt Carlsona1b950d2009-09-01 13:20:17 +000012910 case FLASH_5717VENDOR_ST_M_M45PE20:
Matt Carlson66ee33b2011-04-05 14:22:51 +000012911 /* Detect size with tg3_nvram_get_size() */
12912 break;
12913 case FLASH_5717VENDOR_ST_A_M25PE20:
Matt Carlsona1b950d2009-09-01 13:20:17 +000012914 case FLASH_5717VENDOR_ST_A_M45PE20:
12915 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
12916 break;
12917 default:
12918 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
12919 break;
12920 }
Matt Carlson321d32a2008-11-21 17:22:19 -080012921 break;
Matt Carlsona1b950d2009-09-01 13:20:17 +000012922 default:
Joe Perches63c3a662011-04-26 08:12:10 +000012923 tg3_flag_set(tp, NO_NVRAM);
Matt Carlsona1b950d2009-09-01 13:20:17 +000012924 return;
Matt Carlson321d32a2008-11-21 17:22:19 -080012925 }
Matt Carlsona1b950d2009-09-01 13:20:17 +000012926
12927 tg3_nvram_get_pagesize(tp, nvcfg1);
12928 if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
Joe Perches63c3a662011-04-26 08:12:10 +000012929 tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
Matt Carlson321d32a2008-11-21 17:22:19 -080012930}
12931
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012932static void __devinit tg3_get_5720_nvram_info(struct tg3 *tp)
12933{
12934 u32 nvcfg1, nvmpinstrp;
12935
12936 nvcfg1 = tr32(NVRAM_CFG1);
12937 nvmpinstrp = nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK;
12938
12939 switch (nvmpinstrp) {
12940 case FLASH_5720_EEPROM_HD:
12941 case FLASH_5720_EEPROM_LD:
12942 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012943 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012944
12945 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
12946 tw32(NVRAM_CFG1, nvcfg1);
12947 if (nvmpinstrp == FLASH_5720_EEPROM_HD)
12948 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
12949 else
12950 tp->nvram_pagesize = ATMEL_AT24C02_CHIP_SIZE;
12951 return;
12952 case FLASH_5720VENDOR_M_ATMEL_DB011D:
12953 case FLASH_5720VENDOR_A_ATMEL_DB011B:
12954 case FLASH_5720VENDOR_A_ATMEL_DB011D:
12955 case FLASH_5720VENDOR_M_ATMEL_DB021D:
12956 case FLASH_5720VENDOR_A_ATMEL_DB021B:
12957 case FLASH_5720VENDOR_A_ATMEL_DB021D:
12958 case FLASH_5720VENDOR_M_ATMEL_DB041D:
12959 case FLASH_5720VENDOR_A_ATMEL_DB041B:
12960 case FLASH_5720VENDOR_A_ATMEL_DB041D:
12961 case FLASH_5720VENDOR_M_ATMEL_DB081D:
12962 case FLASH_5720VENDOR_A_ATMEL_DB081D:
12963 case FLASH_5720VENDOR_ATMEL_45USPT:
12964 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012965 tg3_flag_set(tp, NVRAM_BUFFERED);
12966 tg3_flag_set(tp, FLASH);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012967
12968 switch (nvmpinstrp) {
12969 case FLASH_5720VENDOR_M_ATMEL_DB021D:
12970 case FLASH_5720VENDOR_A_ATMEL_DB021B:
12971 case FLASH_5720VENDOR_A_ATMEL_DB021D:
12972 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
12973 break;
12974 case FLASH_5720VENDOR_M_ATMEL_DB041D:
12975 case FLASH_5720VENDOR_A_ATMEL_DB041B:
12976 case FLASH_5720VENDOR_A_ATMEL_DB041D:
12977 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
12978 break;
12979 case FLASH_5720VENDOR_M_ATMEL_DB081D:
12980 case FLASH_5720VENDOR_A_ATMEL_DB081D:
12981 tp->nvram_size = TG3_NVRAM_SIZE_1MB;
12982 break;
12983 default:
12984 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
12985 break;
12986 }
12987 break;
12988 case FLASH_5720VENDOR_M_ST_M25PE10:
12989 case FLASH_5720VENDOR_M_ST_M45PE10:
12990 case FLASH_5720VENDOR_A_ST_M25PE10:
12991 case FLASH_5720VENDOR_A_ST_M45PE10:
12992 case FLASH_5720VENDOR_M_ST_M25PE20:
12993 case FLASH_5720VENDOR_M_ST_M45PE20:
12994 case FLASH_5720VENDOR_A_ST_M25PE20:
12995 case FLASH_5720VENDOR_A_ST_M45PE20:
12996 case FLASH_5720VENDOR_M_ST_M25PE40:
12997 case FLASH_5720VENDOR_M_ST_M45PE40:
12998 case FLASH_5720VENDOR_A_ST_M25PE40:
12999 case FLASH_5720VENDOR_A_ST_M45PE40:
13000 case FLASH_5720VENDOR_M_ST_M25PE80:
13001 case FLASH_5720VENDOR_M_ST_M45PE80:
13002 case FLASH_5720VENDOR_A_ST_M25PE80:
13003 case FLASH_5720VENDOR_A_ST_M45PE80:
13004 case FLASH_5720VENDOR_ST_25USPT:
13005 case FLASH_5720VENDOR_ST_45USPT:
13006 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000013007 tg3_flag_set(tp, NVRAM_BUFFERED);
13008 tg3_flag_set(tp, FLASH);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000013009
13010 switch (nvmpinstrp) {
13011 case FLASH_5720VENDOR_M_ST_M25PE20:
13012 case FLASH_5720VENDOR_M_ST_M45PE20:
13013 case FLASH_5720VENDOR_A_ST_M25PE20:
13014 case FLASH_5720VENDOR_A_ST_M45PE20:
13015 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
13016 break;
13017 case FLASH_5720VENDOR_M_ST_M25PE40:
13018 case FLASH_5720VENDOR_M_ST_M45PE40:
13019 case FLASH_5720VENDOR_A_ST_M25PE40:
13020 case FLASH_5720VENDOR_A_ST_M45PE40:
13021 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
13022 break;
13023 case FLASH_5720VENDOR_M_ST_M25PE80:
13024 case FLASH_5720VENDOR_M_ST_M45PE80:
13025 case FLASH_5720VENDOR_A_ST_M25PE80:
13026 case FLASH_5720VENDOR_A_ST_M45PE80:
13027 tp->nvram_size = TG3_NVRAM_SIZE_1MB;
13028 break;
13029 default:
13030 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
13031 break;
13032 }
13033 break;
13034 default:
Joe Perches63c3a662011-04-26 08:12:10 +000013035 tg3_flag_set(tp, NO_NVRAM);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000013036 return;
13037 }
13038
13039 tg3_nvram_get_pagesize(tp, nvcfg1);
13040 if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
Joe Perches63c3a662011-04-26 08:12:10 +000013041 tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000013042}
13043
Linus Torvalds1da177e2005-04-16 15:20:36 -070013044/* Chips other than 5700/5701 use the NVRAM for fetching info. */
13045static void __devinit tg3_nvram_init(struct tg3 *tp)
13046{
Linus Torvalds1da177e2005-04-16 15:20:36 -070013047 tw32_f(GRC_EEPROM_ADDR,
13048 (EEPROM_ADDR_FSM_RESET |
13049 (EEPROM_DEFAULT_CLOCK_PERIOD <<
13050 EEPROM_ADDR_CLKPERD_SHIFT)));
13051
Michael Chan9d57f012006-12-07 00:23:25 -080013052 msleep(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013053
13054 /* Enable seeprom accesses. */
13055 tw32_f(GRC_LOCAL_CTRL,
13056 tr32(GRC_LOCAL_CTRL) | GRC_LCLCTRL_AUTO_SEEPROM);
13057 udelay(100);
13058
13059 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
13060 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701) {
Joe Perches63c3a662011-04-26 08:12:10 +000013061 tg3_flag_set(tp, NVRAM);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013062
Michael Chanec41c7d2006-01-17 02:40:55 -080013063 if (tg3_nvram_lock(tp)) {
Matt Carlson5129c3a2010-04-05 10:19:23 +000013064 netdev_warn(tp->dev,
13065 "Cannot get nvram lock, %s failed\n",
Joe Perches05dbe002010-02-17 19:44:19 +000013066 __func__);
Michael Chanec41c7d2006-01-17 02:40:55 -080013067 return;
13068 }
Michael Chane6af3012005-04-21 17:12:05 -070013069 tg3_enable_nvram_access(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013070
Matt Carlson989a9d22007-05-05 11:51:05 -070013071 tp->nvram_size = 0;
13072
Michael Chan361b4ac2005-04-21 17:11:21 -070013073 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752)
13074 tg3_get_5752_nvram_info(tp);
Michael Chand3c7b882006-03-23 01:28:25 -080013075 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755)
13076 tg3_get_5755_nvram_info(tp);
Matt Carlsond30cdd22007-10-07 23:28:35 -070013077 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787 ||
Matt Carlson57e69832008-05-25 23:48:31 -070013078 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
13079 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
Michael Chan1b277772006-03-20 22:27:48 -080013080 tg3_get_5787_nvram_info(tp);
Matt Carlson6b91fa02007-10-10 18:01:09 -070013081 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
13082 tg3_get_5761_nvram_info(tp);
Michael Chanb5d37722006-09-27 16:06:21 -070013083 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
13084 tg3_get_5906_nvram_info(tp);
Matt Carlsonb703df62009-12-03 08:36:21 +000013085 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
Matt Carlson55086ad2011-12-14 11:09:59 +000013086 tg3_flag(tp, 57765_CLASS))
Matt Carlson321d32a2008-11-21 17:22:19 -080013087 tg3_get_57780_nvram_info(tp);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000013088 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
13089 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
Matt Carlsona1b950d2009-09-01 13:20:17 +000013090 tg3_get_5717_nvram_info(tp);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000013091 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
13092 tg3_get_5720_nvram_info(tp);
Michael Chan361b4ac2005-04-21 17:11:21 -070013093 else
13094 tg3_get_nvram_info(tp);
13095
Matt Carlson989a9d22007-05-05 11:51:05 -070013096 if (tp->nvram_size == 0)
13097 tg3_get_nvram_size(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013098
Michael Chane6af3012005-04-21 17:12:05 -070013099 tg3_disable_nvram_access(tp);
Michael Chan381291b2005-12-13 21:08:21 -080013100 tg3_nvram_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013101
13102 } else {
Joe Perches63c3a662011-04-26 08:12:10 +000013103 tg3_flag_clear(tp, NVRAM);
13104 tg3_flag_clear(tp, NVRAM_BUFFERED);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013105
13106 tg3_get_eeprom_size(tp);
13107 }
13108}
13109
Linus Torvalds1da177e2005-04-16 15:20:36 -070013110struct subsys_tbl_ent {
13111 u16 subsys_vendor, subsys_devid;
13112 u32 phy_id;
13113};
13114
Matt Carlson24daf2b2010-02-17 15:17:02 +000013115static struct subsys_tbl_ent subsys_id_to_phy_id[] __devinitdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070013116 /* Broadcom boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000013117 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000013118 TG3PCI_SUBDEVICE_ID_BROADCOM_95700A6, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013119 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000013120 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A5, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013121 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000013122 TG3PCI_SUBDEVICE_ID_BROADCOM_95700T6, TG3_PHY_ID_BCM8002 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013123 { TG3PCI_SUBVENDOR_ID_BROADCOM,
13124 TG3PCI_SUBDEVICE_ID_BROADCOM_95700A9, 0 },
13125 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000013126 TG3PCI_SUBDEVICE_ID_BROADCOM_95701T1, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013127 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000013128 TG3PCI_SUBDEVICE_ID_BROADCOM_95701T8, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013129 { TG3PCI_SUBVENDOR_ID_BROADCOM,
13130 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A7, 0 },
13131 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000013132 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A10, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013133 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000013134 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A12, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013135 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000013136 TG3PCI_SUBDEVICE_ID_BROADCOM_95703AX1, TG3_PHY_ID_BCM5703 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013137 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000013138 TG3PCI_SUBDEVICE_ID_BROADCOM_95703AX2, TG3_PHY_ID_BCM5703 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070013139
13140 /* 3com boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000013141 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000013142 TG3PCI_SUBDEVICE_ID_3COM_3C996T, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013143 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000013144 TG3PCI_SUBDEVICE_ID_3COM_3C996BT, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013145 { TG3PCI_SUBVENDOR_ID_3COM,
13146 TG3PCI_SUBDEVICE_ID_3COM_3C996SX, 0 },
13147 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000013148 TG3PCI_SUBDEVICE_ID_3COM_3C1000T, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013149 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000013150 TG3PCI_SUBDEVICE_ID_3COM_3C940BR01, TG3_PHY_ID_BCM5701 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070013151
13152 /* DELL boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000013153 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000013154 TG3PCI_SUBDEVICE_ID_DELL_VIPER, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013155 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000013156 TG3PCI_SUBDEVICE_ID_DELL_JAGUAR, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013157 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000013158 TG3PCI_SUBDEVICE_ID_DELL_MERLOT, TG3_PHY_ID_BCM5411 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013159 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000013160 TG3PCI_SUBDEVICE_ID_DELL_SLIM_MERLOT, TG3_PHY_ID_BCM5411 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070013161
13162 /* Compaq boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000013163 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000013164 TG3PCI_SUBDEVICE_ID_COMPAQ_BANSHEE, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013165 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000013166 TG3PCI_SUBDEVICE_ID_COMPAQ_BANSHEE_2, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013167 { TG3PCI_SUBVENDOR_ID_COMPAQ,
13168 TG3PCI_SUBDEVICE_ID_COMPAQ_CHANGELING, 0 },
13169 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000013170 TG3PCI_SUBDEVICE_ID_COMPAQ_NC7780, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013171 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000013172 TG3PCI_SUBDEVICE_ID_COMPAQ_NC7780_2, TG3_PHY_ID_BCM5701 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070013173
13174 /* IBM boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000013175 { TG3PCI_SUBVENDOR_ID_IBM,
13176 TG3PCI_SUBDEVICE_ID_IBM_5703SAX2, 0 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013177};
13178
Matt Carlson24daf2b2010-02-17 15:17:02 +000013179static struct subsys_tbl_ent * __devinit tg3_lookup_by_subsys(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013180{
13181 int i;
13182
13183 for (i = 0; i < ARRAY_SIZE(subsys_id_to_phy_id); i++) {
13184 if ((subsys_id_to_phy_id[i].subsys_vendor ==
13185 tp->pdev->subsystem_vendor) &&
13186 (subsys_id_to_phy_id[i].subsys_devid ==
13187 tp->pdev->subsystem_device))
13188 return &subsys_id_to_phy_id[i];
13189 }
13190 return NULL;
13191}
13192
Michael Chan7d0c41e2005-04-21 17:06:20 -070013193static void __devinit tg3_get_eeprom_hw_cfg(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013194{
Linus Torvalds1da177e2005-04-16 15:20:36 -070013195 u32 val;
David S. Millerf49639e2006-06-09 11:58:36 -070013196
Matt Carlson79eb6902010-02-17 15:17:03 +000013197 tp->phy_id = TG3_PHY_ID_INVALID;
Michael Chan7d0c41e2005-04-21 17:06:20 -070013198 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
13199
Gary Zambranoa85feb82007-05-05 11:52:19 -070013200 /* Assume an onboard device and WOL capable by default. */
Joe Perches63c3a662011-04-26 08:12:10 +000013201 tg3_flag_set(tp, EEPROM_WRITE_PROT);
13202 tg3_flag_set(tp, WOL_CAP);
David S. Miller72b845e2006-03-14 14:11:48 -080013203
Michael Chanb5d37722006-09-27 16:06:21 -070013204 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan9d26e212006-12-07 00:21:14 -080013205 if (!(tr32(PCIE_TRANSACTION_CFG) & PCIE_TRANS_CFG_LOM)) {
Joe Perches63c3a662011-04-26 08:12:10 +000013206 tg3_flag_clear(tp, EEPROM_WRITE_PROT);
13207 tg3_flag_set(tp, IS_NIC);
Michael Chan9d26e212006-12-07 00:21:14 -080013208 }
Matt Carlson0527ba32007-10-10 18:03:30 -070013209 val = tr32(VCPU_CFGSHDW);
13210 if (val & VCPU_CFGSHDW_ASPM_DBNC)
Joe Perches63c3a662011-04-26 08:12:10 +000013211 tg3_flag_set(tp, ASPM_WORKAROUND);
Matt Carlson0527ba32007-10-10 18:03:30 -070013212 if ((val & VCPU_CFGSHDW_WOL_ENABLE) &&
Rafael J. Wysocki6fdbab92011-04-28 11:02:15 +000013213 (val & VCPU_CFGSHDW_WOL_MAGPKT)) {
Joe Perches63c3a662011-04-26 08:12:10 +000013214 tg3_flag_set(tp, WOL_ENABLE);
Rafael J. Wysocki6fdbab92011-04-28 11:02:15 +000013215 device_set_wakeup_enable(&tp->pdev->dev, true);
13216 }
Matt Carlson05ac4cb2008-11-03 16:53:46 -080013217 goto done;
Michael Chanb5d37722006-09-27 16:06:21 -070013218 }
13219
Linus Torvalds1da177e2005-04-16 15:20:36 -070013220 tg3_read_mem(tp, NIC_SRAM_DATA_SIG, &val);
13221 if (val == NIC_SRAM_DATA_SIG_MAGIC) {
13222 u32 nic_cfg, led_cfg;
Matt Carlsona9daf362008-05-25 23:49:44 -070013223 u32 nic_phy_id, ver, cfg2 = 0, cfg4 = 0, eeprom_phy_id;
Michael Chan7d0c41e2005-04-21 17:06:20 -070013224 int eeprom_phy_serdes = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013225
13226 tg3_read_mem(tp, NIC_SRAM_DATA_CFG, &nic_cfg);
13227 tp->nic_sram_data_cfg = nic_cfg;
13228
13229 tg3_read_mem(tp, NIC_SRAM_DATA_VER, &ver);
13230 ver >>= NIC_SRAM_DATA_VER_SHIFT;
Matt Carlson6ff6f812011-05-19 12:12:54 +000013231 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
13232 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701 &&
13233 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5703 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070013234 (ver > 0) && (ver < 0x100))
13235 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_2, &cfg2);
13236
Matt Carlsona9daf362008-05-25 23:49:44 -070013237 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
13238 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_4, &cfg4);
13239
Linus Torvalds1da177e2005-04-16 15:20:36 -070013240 if ((nic_cfg & NIC_SRAM_DATA_CFG_PHY_TYPE_MASK) ==
13241 NIC_SRAM_DATA_CFG_PHY_TYPE_FIBER)
13242 eeprom_phy_serdes = 1;
13243
13244 tg3_read_mem(tp, NIC_SRAM_DATA_PHY_ID, &nic_phy_id);
13245 if (nic_phy_id != 0) {
13246 u32 id1 = nic_phy_id & NIC_SRAM_DATA_PHY_ID1_MASK;
13247 u32 id2 = nic_phy_id & NIC_SRAM_DATA_PHY_ID2_MASK;
13248
13249 eeprom_phy_id = (id1 >> 16) << 10;
13250 eeprom_phy_id |= (id2 & 0xfc00) << 16;
13251 eeprom_phy_id |= (id2 & 0x03ff) << 0;
13252 } else
13253 eeprom_phy_id = 0;
13254
Michael Chan7d0c41e2005-04-21 17:06:20 -070013255 tp->phy_id = eeprom_phy_id;
Michael Chan747e8f82005-07-25 12:33:22 -070013256 if (eeprom_phy_serdes) {
Joe Perches63c3a662011-04-26 08:12:10 +000013257 if (!tg3_flag(tp, 5705_PLUS))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013258 tp->phy_flags |= TG3_PHYFLG_PHY_SERDES;
Matt Carlsona50d0792010-06-05 17:24:37 +000013259 else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013260 tp->phy_flags |= TG3_PHYFLG_MII_SERDES;
Michael Chan747e8f82005-07-25 12:33:22 -070013261 }
Michael Chan7d0c41e2005-04-21 17:06:20 -070013262
Joe Perches63c3a662011-04-26 08:12:10 +000013263 if (tg3_flag(tp, 5750_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -070013264 led_cfg = cfg2 & (NIC_SRAM_DATA_CFG_LED_MODE_MASK |
13265 SHASTA_EXT_LED_MODE_MASK);
John W. Linvillecbf46852005-04-21 17:01:29 -070013266 else
Linus Torvalds1da177e2005-04-16 15:20:36 -070013267 led_cfg = nic_cfg & NIC_SRAM_DATA_CFG_LED_MODE_MASK;
13268
13269 switch (led_cfg) {
13270 default:
13271 case NIC_SRAM_DATA_CFG_LED_MODE_PHY_1:
13272 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
13273 break;
13274
13275 case NIC_SRAM_DATA_CFG_LED_MODE_PHY_2:
13276 tp->led_ctrl = LED_CTRL_MODE_PHY_2;
13277 break;
13278
13279 case NIC_SRAM_DATA_CFG_LED_MODE_MAC:
13280 tp->led_ctrl = LED_CTRL_MODE_MAC;
Michael Chan9ba27792005-06-06 15:16:20 -070013281
13282 /* Default to PHY_1_MODE if 0 (MAC_MODE) is
13283 * read on some older 5700/5701 bootcode.
13284 */
13285 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
13286 ASIC_REV_5700 ||
13287 GET_ASIC_REV(tp->pci_chip_rev_id) ==
13288 ASIC_REV_5701)
13289 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
13290
Linus Torvalds1da177e2005-04-16 15:20:36 -070013291 break;
13292
13293 case SHASTA_EXT_LED_SHARED:
13294 tp->led_ctrl = LED_CTRL_MODE_SHARED;
13295 if (tp->pci_chip_rev_id != CHIPREV_ID_5750_A0 &&
13296 tp->pci_chip_rev_id != CHIPREV_ID_5750_A1)
13297 tp->led_ctrl |= (LED_CTRL_MODE_PHY_1 |
13298 LED_CTRL_MODE_PHY_2);
13299 break;
13300
13301 case SHASTA_EXT_LED_MAC:
13302 tp->led_ctrl = LED_CTRL_MODE_SHASTA_MAC;
13303 break;
13304
13305 case SHASTA_EXT_LED_COMBO:
13306 tp->led_ctrl = LED_CTRL_MODE_COMBO;
13307 if (tp->pci_chip_rev_id != CHIPREV_ID_5750_A0)
13308 tp->led_ctrl |= (LED_CTRL_MODE_PHY_1 |
13309 LED_CTRL_MODE_PHY_2);
13310 break;
13311
Stephen Hemminger855e1112008-04-16 16:37:28 -070013312 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013313
13314 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
13315 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) &&
13316 tp->pdev->subsystem_vendor == PCI_VENDOR_ID_DELL)
13317 tp->led_ctrl = LED_CTRL_MODE_PHY_2;
13318
Matt Carlsonb2a5c192008-04-03 21:44:44 -070013319 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX)
13320 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
Matt Carlson5f608912007-11-12 21:17:07 -080013321
Michael Chan9d26e212006-12-07 00:21:14 -080013322 if (nic_cfg & NIC_SRAM_DATA_CFG_EEPROM_WP) {
Joe Perches63c3a662011-04-26 08:12:10 +000013323 tg3_flag_set(tp, EEPROM_WRITE_PROT);
Michael Chan9d26e212006-12-07 00:21:14 -080013324 if ((tp->pdev->subsystem_vendor ==
13325 PCI_VENDOR_ID_ARIMA) &&
13326 (tp->pdev->subsystem_device == 0x205a ||
13327 tp->pdev->subsystem_device == 0x2063))
Joe Perches63c3a662011-04-26 08:12:10 +000013328 tg3_flag_clear(tp, EEPROM_WRITE_PROT);
Michael Chan9d26e212006-12-07 00:21:14 -080013329 } else {
Joe Perches63c3a662011-04-26 08:12:10 +000013330 tg3_flag_clear(tp, EEPROM_WRITE_PROT);
13331 tg3_flag_set(tp, IS_NIC);
Michael Chan9d26e212006-12-07 00:21:14 -080013332 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013333
13334 if (nic_cfg & NIC_SRAM_DATA_CFG_ASF_ENABLE) {
Joe Perches63c3a662011-04-26 08:12:10 +000013335 tg3_flag_set(tp, ENABLE_ASF);
13336 if (tg3_flag(tp, 5750_PLUS))
13337 tg3_flag_set(tp, ASF_NEW_HANDSHAKE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013338 }
Matt Carlsonb2b98d42008-11-03 16:52:32 -080013339
13340 if ((nic_cfg & NIC_SRAM_DATA_CFG_APE_ENABLE) &&
Joe Perches63c3a662011-04-26 08:12:10 +000013341 tg3_flag(tp, 5750_PLUS))
13342 tg3_flag_set(tp, ENABLE_APE);
Matt Carlsonb2b98d42008-11-03 16:52:32 -080013343
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013344 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES &&
Gary Zambranoa85feb82007-05-05 11:52:19 -070013345 !(nic_cfg & NIC_SRAM_DATA_CFG_FIBER_WOL))
Joe Perches63c3a662011-04-26 08:12:10 +000013346 tg3_flag_clear(tp, WOL_CAP);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013347
Joe Perches63c3a662011-04-26 08:12:10 +000013348 if (tg3_flag(tp, WOL_CAP) &&
Rafael J. Wysocki6fdbab92011-04-28 11:02:15 +000013349 (nic_cfg & NIC_SRAM_DATA_CFG_WOL_ENABLE)) {
Joe Perches63c3a662011-04-26 08:12:10 +000013350 tg3_flag_set(tp, WOL_ENABLE);
Rafael J. Wysocki6fdbab92011-04-28 11:02:15 +000013351 device_set_wakeup_enable(&tp->pdev->dev, true);
13352 }
Matt Carlson0527ba32007-10-10 18:03:30 -070013353
Linus Torvalds1da177e2005-04-16 15:20:36 -070013354 if (cfg2 & (1 << 17))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013355 tp->phy_flags |= TG3_PHYFLG_CAPACITIVE_COUPLING;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013356
13357 /* serdes signal pre-emphasis in register 0x590 set by */
13358 /* bootcode if bit 18 is set */
13359 if (cfg2 & (1 << 18))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013360 tp->phy_flags |= TG3_PHYFLG_SERDES_PREEMPHASIS;
Matt Carlson8ed5d972007-05-07 00:25:49 -070013361
Joe Perches63c3a662011-04-26 08:12:10 +000013362 if ((tg3_flag(tp, 57765_PLUS) ||
13363 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
13364 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX)) &&
Matt Carlson6833c042008-11-21 17:18:59 -080013365 (cfg2 & NIC_SRAM_DATA_CFG_2_APD_EN))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013366 tp->phy_flags |= TG3_PHYFLG_ENABLE_APD;
Matt Carlson6833c042008-11-21 17:18:59 -080013367
Joe Perches63c3a662011-04-26 08:12:10 +000013368 if (tg3_flag(tp, PCI_EXPRESS) &&
Matt Carlson8c69b1e2010-08-02 11:26:00 +000013369 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
Joe Perches63c3a662011-04-26 08:12:10 +000013370 !tg3_flag(tp, 57765_PLUS)) {
Matt Carlson8ed5d972007-05-07 00:25:49 -070013371 u32 cfg3;
13372
13373 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_3, &cfg3);
13374 if (cfg3 & NIC_SRAM_ASPM_DEBOUNCE)
Joe Perches63c3a662011-04-26 08:12:10 +000013375 tg3_flag_set(tp, ASPM_WORKAROUND);
Matt Carlson8ed5d972007-05-07 00:25:49 -070013376 }
Matt Carlsona9daf362008-05-25 23:49:44 -070013377
Matt Carlson14417062010-02-17 15:16:59 +000013378 if (cfg4 & NIC_SRAM_RGMII_INBAND_DISABLE)
Joe Perches63c3a662011-04-26 08:12:10 +000013379 tg3_flag_set(tp, RGMII_INBAND_DISABLE);
Matt Carlsona9daf362008-05-25 23:49:44 -070013380 if (cfg4 & NIC_SRAM_RGMII_EXT_IBND_RX_EN)
Joe Perches63c3a662011-04-26 08:12:10 +000013381 tg3_flag_set(tp, RGMII_EXT_IBND_RX_EN);
Matt Carlsona9daf362008-05-25 23:49:44 -070013382 if (cfg4 & NIC_SRAM_RGMII_EXT_IBND_TX_EN)
Joe Perches63c3a662011-04-26 08:12:10 +000013383 tg3_flag_set(tp, RGMII_EXT_IBND_TX_EN);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013384 }
Matt Carlson05ac4cb2008-11-03 16:53:46 -080013385done:
Joe Perches63c3a662011-04-26 08:12:10 +000013386 if (tg3_flag(tp, WOL_CAP))
Rafael J. Wysocki43067ed2011-02-10 06:53:09 +000013387 device_set_wakeup_enable(&tp->pdev->dev,
Joe Perches63c3a662011-04-26 08:12:10 +000013388 tg3_flag(tp, WOL_ENABLE));
Rafael J. Wysocki43067ed2011-02-10 06:53:09 +000013389 else
13390 device_set_wakeup_capable(&tp->pdev->dev, false);
Michael Chan7d0c41e2005-04-21 17:06:20 -070013391}
13392
Matt Carlsonb2a5c192008-04-03 21:44:44 -070013393static int __devinit tg3_issue_otp_command(struct tg3 *tp, u32 cmd)
13394{
13395 int i;
13396 u32 val;
13397
13398 tw32(OTP_CTRL, cmd | OTP_CTRL_OTP_CMD_START);
13399 tw32(OTP_CTRL, cmd);
13400
13401 /* Wait for up to 1 ms for command to execute. */
13402 for (i = 0; i < 100; i++) {
13403 val = tr32(OTP_STATUS);
13404 if (val & OTP_STATUS_CMD_DONE)
13405 break;
13406 udelay(10);
13407 }
13408
13409 return (val & OTP_STATUS_CMD_DONE) ? 0 : -EBUSY;
13410}
13411
13412/* Read the gphy configuration from the OTP region of the chip. The gphy
13413 * configuration is a 32-bit value that straddles the alignment boundary.
13414 * We do two 32-bit reads and then shift and merge the results.
13415 */
13416static u32 __devinit tg3_read_otp_phycfg(struct tg3 *tp)
13417{
13418 u32 bhalf_otp, thalf_otp;
13419
13420 tw32(OTP_MODE, OTP_MODE_OTP_THRU_GRC);
13421
13422 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_INIT))
13423 return 0;
13424
13425 tw32(OTP_ADDRESS, OTP_ADDRESS_MAGIC1);
13426
13427 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_READ))
13428 return 0;
13429
13430 thalf_otp = tr32(OTP_READ_DATA);
13431
13432 tw32(OTP_ADDRESS, OTP_ADDRESS_MAGIC2);
13433
13434 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_READ))
13435 return 0;
13436
13437 bhalf_otp = tr32(OTP_READ_DATA);
13438
13439 return ((thalf_otp & 0x0000ffff) << 16) | (bhalf_otp >> 16);
13440}
13441
Matt Carlsone256f8a2011-03-09 16:58:24 +000013442static void __devinit tg3_phy_init_link_config(struct tg3 *tp)
13443{
Hiroaki SHIMODA202ff1c2011-11-22 04:05:41 +000013444 u32 adv = ADVERTISED_Autoneg;
Matt Carlsone256f8a2011-03-09 16:58:24 +000013445
13446 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
13447 adv |= ADVERTISED_1000baseT_Half |
13448 ADVERTISED_1000baseT_Full;
13449
13450 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
13451 adv |= ADVERTISED_100baseT_Half |
13452 ADVERTISED_100baseT_Full |
13453 ADVERTISED_10baseT_Half |
13454 ADVERTISED_10baseT_Full |
13455 ADVERTISED_TP;
13456 else
13457 adv |= ADVERTISED_FIBRE;
13458
13459 tp->link_config.advertising = adv;
Matt Carlsone7405222012-02-13 15:20:16 +000013460 tp->link_config.speed = SPEED_UNKNOWN;
13461 tp->link_config.duplex = DUPLEX_UNKNOWN;
Matt Carlsone256f8a2011-03-09 16:58:24 +000013462 tp->link_config.autoneg = AUTONEG_ENABLE;
Matt Carlsone7405222012-02-13 15:20:16 +000013463 tp->link_config.active_speed = SPEED_UNKNOWN;
13464 tp->link_config.active_duplex = DUPLEX_UNKNOWN;
Matt Carlson34655ad2012-02-22 12:35:18 +000013465
13466 tp->old_link = -1;
Matt Carlsone256f8a2011-03-09 16:58:24 +000013467}
13468
Michael Chan7d0c41e2005-04-21 17:06:20 -070013469static int __devinit tg3_phy_probe(struct tg3 *tp)
13470{
13471 u32 hw_phy_id_1, hw_phy_id_2;
13472 u32 hw_phy_id, hw_phy_id_masked;
13473 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013474
Matt Carlsone256f8a2011-03-09 16:58:24 +000013475 /* flow control autonegotiation is default behavior */
Joe Perches63c3a662011-04-26 08:12:10 +000013476 tg3_flag_set(tp, PAUSE_AUTONEG);
Matt Carlsone256f8a2011-03-09 16:58:24 +000013477 tp->link_config.flowctrl = FLOW_CTRL_TX | FLOW_CTRL_RX;
13478
Joe Perches63c3a662011-04-26 08:12:10 +000013479 if (tg3_flag(tp, USE_PHYLIB))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070013480 return tg3_phy_init(tp);
13481
Linus Torvalds1da177e2005-04-16 15:20:36 -070013482 /* Reading the PHY ID register can conflict with ASF
Nick Andrew877d0312009-01-26 11:06:57 +010013483 * firmware access to the PHY hardware.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013484 */
13485 err = 0;
Joe Perches63c3a662011-04-26 08:12:10 +000013486 if (tg3_flag(tp, ENABLE_ASF) || tg3_flag(tp, ENABLE_APE)) {
Matt Carlson79eb6902010-02-17 15:17:03 +000013487 hw_phy_id = hw_phy_id_masked = TG3_PHY_ID_INVALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013488 } else {
13489 /* Now read the physical PHY_ID from the chip and verify
13490 * that it is sane. If it doesn't look good, we fall back
13491 * to either the hard-coded table based PHY_ID and failing
13492 * that the value found in the eeprom area.
13493 */
13494 err |= tg3_readphy(tp, MII_PHYSID1, &hw_phy_id_1);
13495 err |= tg3_readphy(tp, MII_PHYSID2, &hw_phy_id_2);
13496
13497 hw_phy_id = (hw_phy_id_1 & 0xffff) << 10;
13498 hw_phy_id |= (hw_phy_id_2 & 0xfc00) << 16;
13499 hw_phy_id |= (hw_phy_id_2 & 0x03ff) << 0;
13500
Matt Carlson79eb6902010-02-17 15:17:03 +000013501 hw_phy_id_masked = hw_phy_id & TG3_PHY_ID_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013502 }
13503
Matt Carlson79eb6902010-02-17 15:17:03 +000013504 if (!err && TG3_KNOWN_PHY_ID(hw_phy_id_masked)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070013505 tp->phy_id = hw_phy_id;
Matt Carlson79eb6902010-02-17 15:17:03 +000013506 if (hw_phy_id_masked == TG3_PHY_ID_BCM8002)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013507 tp->phy_flags |= TG3_PHYFLG_PHY_SERDES;
Michael Chanda6b2d02005-08-19 12:54:29 -070013508 else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013509 tp->phy_flags &= ~TG3_PHYFLG_PHY_SERDES;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013510 } else {
Matt Carlson79eb6902010-02-17 15:17:03 +000013511 if (tp->phy_id != TG3_PHY_ID_INVALID) {
Michael Chan7d0c41e2005-04-21 17:06:20 -070013512 /* Do nothing, phy ID already set up in
13513 * tg3_get_eeprom_hw_cfg().
13514 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070013515 } else {
13516 struct subsys_tbl_ent *p;
13517
13518 /* No eeprom signature? Try the hardcoded
13519 * subsys device table.
13520 */
Matt Carlson24daf2b2010-02-17 15:17:02 +000013521 p = tg3_lookup_by_subsys(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013522 if (!p)
13523 return -ENODEV;
13524
13525 tp->phy_id = p->phy_id;
13526 if (!tp->phy_id ||
Matt Carlson79eb6902010-02-17 15:17:03 +000013527 tp->phy_id == TG3_PHY_ID_BCM8002)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013528 tp->phy_flags |= TG3_PHYFLG_PHY_SERDES;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013529 }
13530 }
13531
Matt Carlsona6b68da2010-12-06 08:28:52 +000013532 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES) &&
Matt Carlson5baa5e92011-07-20 10:20:53 +000013533 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
13534 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720 ||
13535 (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718 &&
Matt Carlsona6b68da2010-12-06 08:28:52 +000013536 tp->pci_chip_rev_id != CHIPREV_ID_5717_A0) ||
13537 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765 &&
13538 tp->pci_chip_rev_id != CHIPREV_ID_57765_A0)))
Matt Carlson52b02d02010-10-14 10:37:41 +000013539 tp->phy_flags |= TG3_PHYFLG_EEE_CAP;
13540
Matt Carlsone256f8a2011-03-09 16:58:24 +000013541 tg3_phy_init_link_config(tp);
13542
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013543 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES) &&
Joe Perches63c3a662011-04-26 08:12:10 +000013544 !tg3_flag(tp, ENABLE_APE) &&
13545 !tg3_flag(tp, ENABLE_ASF)) {
Matt Carlsone2bf73e2011-12-08 14:40:15 +000013546 u32 bmsr, dummy;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013547
13548 tg3_readphy(tp, MII_BMSR, &bmsr);
13549 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
13550 (bmsr & BMSR_LSTATUS))
13551 goto skip_phy_reset;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040013552
Linus Torvalds1da177e2005-04-16 15:20:36 -070013553 err = tg3_phy_reset(tp);
13554 if (err)
13555 return err;
13556
Matt Carlson42b64a42011-05-19 12:12:49 +000013557 tg3_phy_set_wirespeed(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013558
Matt Carlsone2bf73e2011-12-08 14:40:15 +000013559 if (!tg3_phy_copper_an_config_ok(tp, &dummy)) {
Matt Carlson42b64a42011-05-19 12:12:49 +000013560 tg3_phy_autoneg_cfg(tp, tp->link_config.advertising,
13561 tp->link_config.flowctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013562
13563 tg3_writephy(tp, MII_BMCR,
13564 BMCR_ANENABLE | BMCR_ANRESTART);
13565 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013566 }
13567
13568skip_phy_reset:
Matt Carlson79eb6902010-02-17 15:17:03 +000013569 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070013570 err = tg3_init_5401phy_dsp(tp);
13571 if (err)
13572 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013573
Linus Torvalds1da177e2005-04-16 15:20:36 -070013574 err = tg3_init_5401phy_dsp(tp);
13575 }
13576
Linus Torvalds1da177e2005-04-16 15:20:36 -070013577 return err;
13578}
13579
Matt Carlson184b8902010-04-05 10:19:25 +000013580static void __devinit tg3_read_vpd(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013581{
Matt Carlsona4a8bb12010-09-15 09:00:00 +000013582 u8 *vpd_data;
Matt Carlson4181b2c2010-02-26 14:04:45 +000013583 unsigned int block_end, rosize, len;
Matt Carlson535a4902011-07-20 10:20:56 +000013584 u32 vpdlen;
Matt Carlson184b8902010-04-05 10:19:25 +000013585 int j, i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013586
Matt Carlson535a4902011-07-20 10:20:56 +000013587 vpd_data = (u8 *)tg3_vpd_readblock(tp, &vpdlen);
Matt Carlsona4a8bb12010-09-15 09:00:00 +000013588 if (!vpd_data)
13589 goto out_no_vpd;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013590
Matt Carlson535a4902011-07-20 10:20:56 +000013591 i = pci_vpd_find_tag(vpd_data, 0, vpdlen, PCI_VPD_LRDT_RO_DATA);
Matt Carlson4181b2c2010-02-26 14:04:45 +000013592 if (i < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013593 goto out_not_found;
Matt Carlson4181b2c2010-02-26 14:04:45 +000013594
13595 rosize = pci_vpd_lrdt_size(&vpd_data[i]);
13596 block_end = i + PCI_VPD_LRDT_TAG_SIZE + rosize;
13597 i += PCI_VPD_LRDT_TAG_SIZE;
13598
Matt Carlson535a4902011-07-20 10:20:56 +000013599 if (block_end > vpdlen)
Matt Carlson4181b2c2010-02-26 14:04:45 +000013600 goto out_not_found;
13601
Matt Carlson184b8902010-04-05 10:19:25 +000013602 j = pci_vpd_find_info_keyword(vpd_data, i, rosize,
13603 PCI_VPD_RO_KEYWORD_MFR_ID);
13604 if (j > 0) {
13605 len = pci_vpd_info_field_size(&vpd_data[j]);
13606
13607 j += PCI_VPD_INFO_FLD_HDR_SIZE;
13608 if (j + len > block_end || len != 4 ||
13609 memcmp(&vpd_data[j], "1028", 4))
13610 goto partno;
13611
13612 j = pci_vpd_find_info_keyword(vpd_data, i, rosize,
13613 PCI_VPD_RO_KEYWORD_VENDOR0);
13614 if (j < 0)
13615 goto partno;
13616
13617 len = pci_vpd_info_field_size(&vpd_data[j]);
13618
13619 j += PCI_VPD_INFO_FLD_HDR_SIZE;
13620 if (j + len > block_end)
13621 goto partno;
13622
13623 memcpy(tp->fw_ver, &vpd_data[j], len);
Matt Carlson535a4902011-07-20 10:20:56 +000013624 strncat(tp->fw_ver, " bc ", vpdlen - len - 1);
Matt Carlson184b8902010-04-05 10:19:25 +000013625 }
13626
13627partno:
Matt Carlson4181b2c2010-02-26 14:04:45 +000013628 i = pci_vpd_find_info_keyword(vpd_data, i, rosize,
13629 PCI_VPD_RO_KEYWORD_PARTNO);
13630 if (i < 0)
13631 goto out_not_found;
13632
13633 len = pci_vpd_info_field_size(&vpd_data[i]);
13634
13635 i += PCI_VPD_INFO_FLD_HDR_SIZE;
13636 if (len > TG3_BPN_SIZE ||
Matt Carlson535a4902011-07-20 10:20:56 +000013637 (len + i) > vpdlen)
Matt Carlson4181b2c2010-02-26 14:04:45 +000013638 goto out_not_found;
13639
13640 memcpy(tp->board_part_number, &vpd_data[i], len);
13641
Linus Torvalds1da177e2005-04-16 15:20:36 -070013642out_not_found:
Matt Carlsona4a8bb12010-09-15 09:00:00 +000013643 kfree(vpd_data);
Matt Carlson37a949c2010-09-30 10:34:33 +000013644 if (tp->board_part_number[0])
Matt Carlsona4a8bb12010-09-15 09:00:00 +000013645 return;
13646
13647out_no_vpd:
Matt Carlson37a949c2010-09-30 10:34:33 +000013648 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717) {
13649 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717)
13650 strcpy(tp->board_part_number, "BCM5717");
13651 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718)
13652 strcpy(tp->board_part_number, "BCM5718");
13653 else
13654 goto nomatch;
13655 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780) {
13656 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57780)
13657 strcpy(tp->board_part_number, "BCM57780");
13658 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57760)
13659 strcpy(tp->board_part_number, "BCM57760");
13660 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57790)
13661 strcpy(tp->board_part_number, "BCM57790");
13662 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57788)
13663 strcpy(tp->board_part_number, "BCM57788");
13664 else
13665 goto nomatch;
13666 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765) {
13667 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57761)
13668 strcpy(tp->board_part_number, "BCM57761");
13669 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57765)
13670 strcpy(tp->board_part_number, "BCM57765");
13671 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57781)
13672 strcpy(tp->board_part_number, "BCM57781");
13673 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57785)
13674 strcpy(tp->board_part_number, "BCM57785");
13675 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57791)
13676 strcpy(tp->board_part_number, "BCM57791");
13677 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57795)
13678 strcpy(tp->board_part_number, "BCM57795");
13679 else
13680 goto nomatch;
Matt Carlson55086ad2011-12-14 11:09:59 +000013681 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57766) {
13682 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57762)
13683 strcpy(tp->board_part_number, "BCM57762");
13684 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57766)
13685 strcpy(tp->board_part_number, "BCM57766");
13686 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57782)
13687 strcpy(tp->board_part_number, "BCM57782");
13688 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57786)
13689 strcpy(tp->board_part_number, "BCM57786");
13690 else
13691 goto nomatch;
Matt Carlson37a949c2010-09-30 10:34:33 +000013692 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chanb5d37722006-09-27 16:06:21 -070013693 strcpy(tp->board_part_number, "BCM95906");
Matt Carlson37a949c2010-09-30 10:34:33 +000013694 } else {
13695nomatch:
Michael Chanb5d37722006-09-27 16:06:21 -070013696 strcpy(tp->board_part_number, "none");
Matt Carlson37a949c2010-09-30 10:34:33 +000013697 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013698}
13699
Matt Carlson9c8a6202007-10-21 16:16:08 -070013700static int __devinit tg3_fw_img_is_valid(struct tg3 *tp, u32 offset)
13701{
13702 u32 val;
13703
Matt Carlsone4f34112009-02-25 14:25:00 +000013704 if (tg3_nvram_read(tp, offset, &val) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070013705 (val & 0xfc000000) != 0x0c000000 ||
Matt Carlsone4f34112009-02-25 14:25:00 +000013706 tg3_nvram_read(tp, offset + 4, &val) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070013707 val != 0)
13708 return 0;
13709
13710 return 1;
13711}
13712
Matt Carlsonacd9c112009-02-25 14:26:33 +000013713static void __devinit tg3_read_bc_ver(struct tg3 *tp)
13714{
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013715 u32 val, offset, start, ver_offset;
Matt Carlson75f99362010-04-05 10:19:24 +000013716 int i, dst_off;
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013717 bool newver = false;
Matt Carlsonacd9c112009-02-25 14:26:33 +000013718
13719 if (tg3_nvram_read(tp, 0xc, &offset) ||
13720 tg3_nvram_read(tp, 0x4, &start))
13721 return;
13722
13723 offset = tg3_nvram_logical_addr(tp, offset);
13724
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013725 if (tg3_nvram_read(tp, offset, &val))
Matt Carlsonacd9c112009-02-25 14:26:33 +000013726 return;
13727
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013728 if ((val & 0xfc000000) == 0x0c000000) {
13729 if (tg3_nvram_read(tp, offset + 4, &val))
Matt Carlsonacd9c112009-02-25 14:26:33 +000013730 return;
13731
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013732 if (val == 0)
13733 newver = true;
13734 }
13735
Matt Carlson75f99362010-04-05 10:19:24 +000013736 dst_off = strlen(tp->fw_ver);
13737
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013738 if (newver) {
Matt Carlson75f99362010-04-05 10:19:24 +000013739 if (TG3_VER_SIZE - dst_off < 16 ||
13740 tg3_nvram_read(tp, offset + 8, &ver_offset))
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013741 return;
13742
13743 offset = offset + ver_offset - start;
13744 for (i = 0; i < 16; i += 4) {
13745 __be32 v;
13746 if (tg3_nvram_read_be32(tp, offset + i, &v))
13747 return;
13748
Matt Carlson75f99362010-04-05 10:19:24 +000013749 memcpy(tp->fw_ver + dst_off + i, &v, sizeof(v));
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013750 }
13751 } else {
13752 u32 major, minor;
13753
13754 if (tg3_nvram_read(tp, TG3_NVM_PTREV_BCVER, &ver_offset))
13755 return;
13756
13757 major = (ver_offset & TG3_NVM_BCVER_MAJMSK) >>
13758 TG3_NVM_BCVER_MAJSFT;
13759 minor = ver_offset & TG3_NVM_BCVER_MINMSK;
Matt Carlson75f99362010-04-05 10:19:24 +000013760 snprintf(&tp->fw_ver[dst_off], TG3_VER_SIZE - dst_off,
13761 "v%d.%02d", major, minor);
Matt Carlsonacd9c112009-02-25 14:26:33 +000013762 }
13763}
13764
Matt Carlsona6f6cb12009-02-25 14:27:43 +000013765static void __devinit tg3_read_hwsb_ver(struct tg3 *tp)
13766{
13767 u32 val, major, minor;
13768
13769 /* Use native endian representation */
13770 if (tg3_nvram_read(tp, TG3_NVM_HWSB_CFG1, &val))
13771 return;
13772
13773 major = (val & TG3_NVM_HWSB_CFG1_MAJMSK) >>
13774 TG3_NVM_HWSB_CFG1_MAJSFT;
13775 minor = (val & TG3_NVM_HWSB_CFG1_MINMSK) >>
13776 TG3_NVM_HWSB_CFG1_MINSFT;
13777
13778 snprintf(&tp->fw_ver[0], 32, "sb v%d.%02d", major, minor);
13779}
13780
Matt Carlsondfe00d72008-11-21 17:19:41 -080013781static void __devinit tg3_read_sb_ver(struct tg3 *tp, u32 val)
13782{
13783 u32 offset, major, minor, build;
13784
Matt Carlson75f99362010-04-05 10:19:24 +000013785 strncat(tp->fw_ver, "sb", TG3_VER_SIZE - strlen(tp->fw_ver) - 1);
Matt Carlsondfe00d72008-11-21 17:19:41 -080013786
13787 if ((val & TG3_EEPROM_SB_FORMAT_MASK) != TG3_EEPROM_SB_FORMAT_1)
13788 return;
13789
13790 switch (val & TG3_EEPROM_SB_REVISION_MASK) {
13791 case TG3_EEPROM_SB_REVISION_0:
13792 offset = TG3_EEPROM_SB_F1R0_EDH_OFF;
13793 break;
13794 case TG3_EEPROM_SB_REVISION_2:
13795 offset = TG3_EEPROM_SB_F1R2_EDH_OFF;
13796 break;
13797 case TG3_EEPROM_SB_REVISION_3:
13798 offset = TG3_EEPROM_SB_F1R3_EDH_OFF;
13799 break;
Matt Carlsona4153d42010-02-17 15:16:56 +000013800 case TG3_EEPROM_SB_REVISION_4:
13801 offset = TG3_EEPROM_SB_F1R4_EDH_OFF;
13802 break;
13803 case TG3_EEPROM_SB_REVISION_5:
13804 offset = TG3_EEPROM_SB_F1R5_EDH_OFF;
13805 break;
Matt Carlsonbba226a2010-10-14 10:37:38 +000013806 case TG3_EEPROM_SB_REVISION_6:
13807 offset = TG3_EEPROM_SB_F1R6_EDH_OFF;
13808 break;
Matt Carlsondfe00d72008-11-21 17:19:41 -080013809 default:
13810 return;
13811 }
13812
Matt Carlsone4f34112009-02-25 14:25:00 +000013813 if (tg3_nvram_read(tp, offset, &val))
Matt Carlsondfe00d72008-11-21 17:19:41 -080013814 return;
13815
13816 build = (val & TG3_EEPROM_SB_EDH_BLD_MASK) >>
13817 TG3_EEPROM_SB_EDH_BLD_SHFT;
13818 major = (val & TG3_EEPROM_SB_EDH_MAJ_MASK) >>
13819 TG3_EEPROM_SB_EDH_MAJ_SHFT;
13820 minor = val & TG3_EEPROM_SB_EDH_MIN_MASK;
13821
13822 if (minor > 99 || build > 26)
13823 return;
13824
Matt Carlson75f99362010-04-05 10:19:24 +000013825 offset = strlen(tp->fw_ver);
13826 snprintf(&tp->fw_ver[offset], TG3_VER_SIZE - offset,
13827 " v%d.%02d", major, minor);
Matt Carlsondfe00d72008-11-21 17:19:41 -080013828
13829 if (build > 0) {
Matt Carlson75f99362010-04-05 10:19:24 +000013830 offset = strlen(tp->fw_ver);
13831 if (offset < TG3_VER_SIZE - 1)
13832 tp->fw_ver[offset] = 'a' + build - 1;
Matt Carlsondfe00d72008-11-21 17:19:41 -080013833 }
13834}
13835
Matt Carlsonacd9c112009-02-25 14:26:33 +000013836static void __devinit tg3_read_mgmtfw_ver(struct tg3 *tp)
Michael Chanc4e65752006-03-20 22:29:32 -080013837{
13838 u32 val, offset, start;
Matt Carlsonacd9c112009-02-25 14:26:33 +000013839 int i, vlen;
Matt Carlson9c8a6202007-10-21 16:16:08 -070013840
13841 for (offset = TG3_NVM_DIR_START;
13842 offset < TG3_NVM_DIR_END;
13843 offset += TG3_NVM_DIRENT_SIZE) {
Matt Carlsone4f34112009-02-25 14:25:00 +000013844 if (tg3_nvram_read(tp, offset, &val))
Matt Carlson9c8a6202007-10-21 16:16:08 -070013845 return;
13846
13847 if ((val >> TG3_NVM_DIRTYPE_SHIFT) == TG3_NVM_DIRTYPE_ASFINI)
13848 break;
13849 }
13850
13851 if (offset == TG3_NVM_DIR_END)
13852 return;
13853
Joe Perches63c3a662011-04-26 08:12:10 +000013854 if (!tg3_flag(tp, 5705_PLUS))
Matt Carlson9c8a6202007-10-21 16:16:08 -070013855 start = 0x08000000;
Matt Carlsone4f34112009-02-25 14:25:00 +000013856 else if (tg3_nvram_read(tp, offset - 4, &start))
Matt Carlson9c8a6202007-10-21 16:16:08 -070013857 return;
13858
Matt Carlsone4f34112009-02-25 14:25:00 +000013859 if (tg3_nvram_read(tp, offset + 4, &offset) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070013860 !tg3_fw_img_is_valid(tp, offset) ||
Matt Carlsone4f34112009-02-25 14:25:00 +000013861 tg3_nvram_read(tp, offset + 8, &val))
Matt Carlson9c8a6202007-10-21 16:16:08 -070013862 return;
13863
13864 offset += val - start;
13865
Matt Carlsonacd9c112009-02-25 14:26:33 +000013866 vlen = strlen(tp->fw_ver);
Matt Carlson9c8a6202007-10-21 16:16:08 -070013867
Matt Carlsonacd9c112009-02-25 14:26:33 +000013868 tp->fw_ver[vlen++] = ',';
13869 tp->fw_ver[vlen++] = ' ';
Matt Carlson9c8a6202007-10-21 16:16:08 -070013870
13871 for (i = 0; i < 4; i++) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000013872 __be32 v;
13873 if (tg3_nvram_read_be32(tp, offset, &v))
Matt Carlson9c8a6202007-10-21 16:16:08 -070013874 return;
13875
Al Virob9fc7dc2007-12-17 22:59:57 -080013876 offset += sizeof(v);
Matt Carlson9c8a6202007-10-21 16:16:08 -070013877
Matt Carlsonacd9c112009-02-25 14:26:33 +000013878 if (vlen > TG3_VER_SIZE - sizeof(v)) {
13879 memcpy(&tp->fw_ver[vlen], &v, TG3_VER_SIZE - vlen);
Matt Carlson9c8a6202007-10-21 16:16:08 -070013880 break;
13881 }
13882
Matt Carlsonacd9c112009-02-25 14:26:33 +000013883 memcpy(&tp->fw_ver[vlen], &v, sizeof(v));
13884 vlen += sizeof(v);
Matt Carlson9c8a6202007-10-21 16:16:08 -070013885 }
Matt Carlsonacd9c112009-02-25 14:26:33 +000013886}
13887
Matt Carlson7fd76442009-02-25 14:27:20 +000013888static void __devinit tg3_read_dash_ver(struct tg3 *tp)
13889{
13890 int vlen;
13891 u32 apedata;
Matt Carlsonecc79642010-08-02 11:26:01 +000013892 char *fwtype;
Matt Carlson7fd76442009-02-25 14:27:20 +000013893
Joe Perches63c3a662011-04-26 08:12:10 +000013894 if (!tg3_flag(tp, ENABLE_APE) || !tg3_flag(tp, ENABLE_ASF))
Matt Carlson7fd76442009-02-25 14:27:20 +000013895 return;
13896
13897 apedata = tg3_ape_read32(tp, TG3_APE_SEG_SIG);
13898 if (apedata != APE_SEG_SIG_MAGIC)
13899 return;
13900
13901 apedata = tg3_ape_read32(tp, TG3_APE_FW_STATUS);
13902 if (!(apedata & APE_FW_STATUS_READY))
13903 return;
13904
13905 apedata = tg3_ape_read32(tp, TG3_APE_FW_VERSION);
13906
Matt Carlsondc6d0742010-09-15 08:59:55 +000013907 if (tg3_ape_read32(tp, TG3_APE_FW_FEATURES) & TG3_APE_FW_FEATURE_NCSI) {
Joe Perches63c3a662011-04-26 08:12:10 +000013908 tg3_flag_set(tp, APE_HAS_NCSI);
Matt Carlsonecc79642010-08-02 11:26:01 +000013909 fwtype = "NCSI";
Matt Carlsondc6d0742010-09-15 08:59:55 +000013910 } else {
Matt Carlsonecc79642010-08-02 11:26:01 +000013911 fwtype = "DASH";
Matt Carlsondc6d0742010-09-15 08:59:55 +000013912 }
Matt Carlsonecc79642010-08-02 11:26:01 +000013913
Matt Carlson7fd76442009-02-25 14:27:20 +000013914 vlen = strlen(tp->fw_ver);
13915
Matt Carlsonecc79642010-08-02 11:26:01 +000013916 snprintf(&tp->fw_ver[vlen], TG3_VER_SIZE - vlen, " %s v%d.%d.%d.%d",
13917 fwtype,
Matt Carlson7fd76442009-02-25 14:27:20 +000013918 (apedata & APE_FW_VERSION_MAJMSK) >> APE_FW_VERSION_MAJSFT,
13919 (apedata & APE_FW_VERSION_MINMSK) >> APE_FW_VERSION_MINSFT,
13920 (apedata & APE_FW_VERSION_REVMSK) >> APE_FW_VERSION_REVSFT,
13921 (apedata & APE_FW_VERSION_BLDMSK));
13922}
13923
Matt Carlsonacd9c112009-02-25 14:26:33 +000013924static void __devinit tg3_read_fw_ver(struct tg3 *tp)
13925{
13926 u32 val;
Matt Carlson75f99362010-04-05 10:19:24 +000013927 bool vpd_vers = false;
13928
13929 if (tp->fw_ver[0] != 0)
13930 vpd_vers = true;
Matt Carlsonacd9c112009-02-25 14:26:33 +000013931
Joe Perches63c3a662011-04-26 08:12:10 +000013932 if (tg3_flag(tp, NO_NVRAM)) {
Matt Carlson75f99362010-04-05 10:19:24 +000013933 strcat(tp->fw_ver, "sb");
Matt Carlsondf259d82009-04-20 06:57:14 +000013934 return;
13935 }
13936
Matt Carlsonacd9c112009-02-25 14:26:33 +000013937 if (tg3_nvram_read(tp, 0, &val))
13938 return;
13939
13940 if (val == TG3_EEPROM_MAGIC)
13941 tg3_read_bc_ver(tp);
13942 else if ((val & TG3_EEPROM_MAGIC_FW_MSK) == TG3_EEPROM_MAGIC_FW)
13943 tg3_read_sb_ver(tp, val);
Matt Carlsona6f6cb12009-02-25 14:27:43 +000013944 else if ((val & TG3_EEPROM_MAGIC_HW_MSK) == TG3_EEPROM_MAGIC_HW)
13945 tg3_read_hwsb_ver(tp);
Matt Carlsonacd9c112009-02-25 14:26:33 +000013946 else
13947 return;
13948
Matt Carlsonc9cab242011-07-13 09:27:27 +000013949 if (vpd_vers)
Matt Carlson75f99362010-04-05 10:19:24 +000013950 goto done;
Matt Carlsonacd9c112009-02-25 14:26:33 +000013951
Matt Carlsonc9cab242011-07-13 09:27:27 +000013952 if (tg3_flag(tp, ENABLE_APE)) {
13953 if (tg3_flag(tp, ENABLE_ASF))
13954 tg3_read_dash_ver(tp);
13955 } else if (tg3_flag(tp, ENABLE_ASF)) {
13956 tg3_read_mgmtfw_ver(tp);
13957 }
Matt Carlson9c8a6202007-10-21 16:16:08 -070013958
Matt Carlson75f99362010-04-05 10:19:24 +000013959done:
Matt Carlson9c8a6202007-10-21 16:16:08 -070013960 tp->fw_ver[TG3_VER_SIZE - 1] = 0;
Michael Chanc4e65752006-03-20 22:29:32 -080013961}
13962
Matt Carlson7cb32cf2010-09-30 10:34:36 +000013963static inline u32 tg3_rx_ret_ring_size(struct tg3 *tp)
13964{
Joe Perches63c3a662011-04-26 08:12:10 +000013965 if (tg3_flag(tp, LRG_PROD_RING_CAP))
Matt Carlsonde9f5232011-04-05 14:22:43 +000013966 return TG3_RX_RET_MAX_SIZE_5717;
Joe Perches63c3a662011-04-26 08:12:10 +000013967 else if (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS))
Matt Carlsonde9f5232011-04-05 14:22:43 +000013968 return TG3_RX_RET_MAX_SIZE_5700;
Matt Carlson7cb32cf2010-09-30 10:34:36 +000013969 else
Matt Carlsonde9f5232011-04-05 14:22:43 +000013970 return TG3_RX_RET_MAX_SIZE_5705;
Matt Carlson7cb32cf2010-09-30 10:34:36 +000013971}
13972
Matt Carlson41434702011-03-09 16:58:22 +000013973static DEFINE_PCI_DEVICE_TABLE(tg3_write_reorder_chipsets) = {
Joe Perches895950c2010-12-21 02:16:08 -080013974 { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_FE_GATE_700C) },
13975 { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_8131_BRIDGE) },
13976 { PCI_DEVICE(PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_8385_0) },
13977 { },
13978};
13979
Matt Carlson16c7fa72012-02-13 10:20:10 +000013980static struct pci_dev * __devinit tg3_find_peer(struct tg3 *tp)
13981{
13982 struct pci_dev *peer;
13983 unsigned int func, devnr = tp->pdev->devfn & ~7;
13984
13985 for (func = 0; func < 8; func++) {
13986 peer = pci_get_slot(tp->pdev->bus, devnr | func);
13987 if (peer && peer != tp->pdev)
13988 break;
13989 pci_dev_put(peer);
13990 }
13991 /* 5704 can be configured in single-port mode, set peer to
13992 * tp->pdev in that case.
13993 */
13994 if (!peer) {
13995 peer = tp->pdev;
13996 return peer;
13997 }
13998
13999 /*
14000 * We don't need to keep the refcount elevated; there's no way
14001 * to remove one half of this device without removing the other
14002 */
14003 pci_dev_put(peer);
14004
14005 return peer;
14006}
14007
Matt Carlson42b123b2012-02-13 15:20:13 +000014008static void __devinit tg3_detect_asic_rev(struct tg3 *tp, u32 misc_ctrl_reg)
14009{
14010 tp->pci_chip_rev_id = misc_ctrl_reg >> MISC_HOST_CTRL_CHIPREV_SHIFT;
14011 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_USE_PROD_ID_REG) {
14012 u32 reg;
14013
14014 /* All devices that use the alternate
14015 * ASIC REV location have a CPMU.
14016 */
14017 tg3_flag_set(tp, CPMU_PRESENT);
14018
14019 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717 ||
14020 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718 ||
14021 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5719 ||
14022 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5720)
14023 reg = TG3PCI_GEN2_PRODID_ASICREV;
14024 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57781 ||
14025 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57785 ||
14026 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57761 ||
14027 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57765 ||
14028 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57791 ||
14029 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57795 ||
14030 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57762 ||
14031 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57766 ||
14032 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57782 ||
14033 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57786)
14034 reg = TG3PCI_GEN15_PRODID_ASICREV;
14035 else
14036 reg = TG3PCI_PRODID_ASICREV;
14037
14038 pci_read_config_dword(tp->pdev, reg, &tp->pci_chip_rev_id);
14039 }
14040
14041 /* Wrong chip ID in 5752 A0. This code can be removed later
14042 * as A0 is not in production.
14043 */
14044 if (tp->pci_chip_rev_id == CHIPREV_ID_5752_A0_HW)
14045 tp->pci_chip_rev_id = CHIPREV_ID_5752_A0;
14046
14047 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
14048 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
14049 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
14050 tg3_flag_set(tp, 5717_PLUS);
14051
14052 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765 ||
14053 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57766)
14054 tg3_flag_set(tp, 57765_CLASS);
14055
14056 if (tg3_flag(tp, 57765_CLASS) || tg3_flag(tp, 5717_PLUS))
14057 tg3_flag_set(tp, 57765_PLUS);
14058
14059 /* Intentionally exclude ASIC_REV_5906 */
14060 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
14061 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787 ||
14062 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
14063 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
14064 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
14065 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
14066 tg3_flag(tp, 57765_PLUS))
14067 tg3_flag_set(tp, 5755_PLUS);
14068
14069 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780 ||
14070 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714)
14071 tg3_flag_set(tp, 5780_CLASS);
14072
14073 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 ||
14074 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752 ||
14075 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906 ||
14076 tg3_flag(tp, 5755_PLUS) ||
14077 tg3_flag(tp, 5780_CLASS))
14078 tg3_flag_set(tp, 5750_PLUS);
14079
14080 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 ||
14081 tg3_flag(tp, 5750_PLUS))
14082 tg3_flag_set(tp, 5705_PLUS);
14083}
14084
Linus Torvalds1da177e2005-04-16 15:20:36 -070014085static int __devinit tg3_get_invariants(struct tg3 *tp)
14086{
Linus Torvalds1da177e2005-04-16 15:20:36 -070014087 u32 misc_ctrl_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014088 u32 pci_state_reg, grc_misc_cfg;
14089 u32 val;
14090 u16 pci_cmd;
Matt Carlson5e7dfd02008-11-21 17:18:16 -080014091 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014092
Linus Torvalds1da177e2005-04-16 15:20:36 -070014093 /* Force memory write invalidate off. If we leave it on,
14094 * then on 5700_BX chips we have to enable a workaround.
14095 * The workaround is to set the TG3PCI_DMA_RW_CTRL boundary
14096 * to match the cacheline size. The Broadcom driver have this
14097 * workaround but turns MWI off all the times so never uses
14098 * it. This seems to suggest that the workaround is insufficient.
14099 */
14100 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
14101 pci_cmd &= ~PCI_COMMAND_INVALIDATE;
14102 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
14103
Matt Carlson16821282011-07-13 09:27:28 +000014104 /* Important! -- Make sure register accesses are byteswapped
14105 * correctly. Also, for those chips that require it, make
14106 * sure that indirect register accesses are enabled before
14107 * the first operation.
Linus Torvalds1da177e2005-04-16 15:20:36 -070014108 */
14109 pci_read_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
14110 &misc_ctrl_reg);
Matt Carlson16821282011-07-13 09:27:28 +000014111 tp->misc_host_ctrl |= (misc_ctrl_reg &
14112 MISC_HOST_CTRL_CHIPREV);
14113 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
14114 tp->misc_host_ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014115
Matt Carlson42b123b2012-02-13 15:20:13 +000014116 tg3_detect_asic_rev(tp, misc_ctrl_reg);
Michael Chanff645be2005-04-21 17:09:53 -070014117
Michael Chan68929142005-08-09 20:17:14 -070014118 /* If we have 5702/03 A1 or A2 on certain ICH chipsets,
14119 * we need to disable memory and use config. cycles
14120 * only to access all registers. The 5702/03 chips
14121 * can mistakenly decode the special cycles from the
14122 * ICH chipsets as memory write cycles, causing corruption
14123 * of register and memory space. Only certain ICH bridges
14124 * will drive special cycles with non-zero data during the
14125 * address phase which can fall within the 5703's address
14126 * range. This is not an ICH bug as the PCI spec allows
14127 * non-zero address during special cycles. However, only
14128 * these ICH bridges are known to drive non-zero addresses
14129 * during special cycles.
14130 *
14131 * Since special cycles do not cross PCI bridges, we only
14132 * enable this workaround if the 5703 is on the secondary
14133 * bus of these ICH bridges.
14134 */
14135 if ((tp->pci_chip_rev_id == CHIPREV_ID_5703_A1) ||
14136 (tp->pci_chip_rev_id == CHIPREV_ID_5703_A2)) {
14137 static struct tg3_dev_id {
14138 u32 vendor;
14139 u32 device;
14140 u32 rev;
14141 } ich_chipsets[] = {
14142 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AA_8,
14143 PCI_ANY_ID },
14144 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AB_8,
14145 PCI_ANY_ID },
14146 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_11,
14147 0xa },
14148 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_6,
14149 PCI_ANY_ID },
14150 { },
14151 };
14152 struct tg3_dev_id *pci_id = &ich_chipsets[0];
14153 struct pci_dev *bridge = NULL;
14154
14155 while (pci_id->vendor != 0) {
14156 bridge = pci_get_device(pci_id->vendor, pci_id->device,
14157 bridge);
14158 if (!bridge) {
14159 pci_id++;
14160 continue;
14161 }
14162 if (pci_id->rev != PCI_ANY_ID) {
Auke Kok44c10132007-06-08 15:46:36 -070014163 if (bridge->revision > pci_id->rev)
Michael Chan68929142005-08-09 20:17:14 -070014164 continue;
14165 }
14166 if (bridge->subordinate &&
14167 (bridge->subordinate->number ==
14168 tp->pdev->bus->number)) {
Joe Perches63c3a662011-04-26 08:12:10 +000014169 tg3_flag_set(tp, ICH_WORKAROUND);
Michael Chan68929142005-08-09 20:17:14 -070014170 pci_dev_put(bridge);
14171 break;
14172 }
14173 }
14174 }
14175
Matt Carlson6ff6f812011-05-19 12:12:54 +000014176 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
Matt Carlson41588ba2008-04-19 18:12:33 -070014177 static struct tg3_dev_id {
14178 u32 vendor;
14179 u32 device;
14180 } bridge_chipsets[] = {
14181 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PXH_0 },
14182 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PXH_1 },
14183 { },
14184 };
14185 struct tg3_dev_id *pci_id = &bridge_chipsets[0];
14186 struct pci_dev *bridge = NULL;
14187
14188 while (pci_id->vendor != 0) {
14189 bridge = pci_get_device(pci_id->vendor,
14190 pci_id->device,
14191 bridge);
14192 if (!bridge) {
14193 pci_id++;
14194 continue;
14195 }
14196 if (bridge->subordinate &&
14197 (bridge->subordinate->number <=
14198 tp->pdev->bus->number) &&
14199 (bridge->subordinate->subordinate >=
14200 tp->pdev->bus->number)) {
Joe Perches63c3a662011-04-26 08:12:10 +000014201 tg3_flag_set(tp, 5701_DMA_BUG);
Matt Carlson41588ba2008-04-19 18:12:33 -070014202 pci_dev_put(bridge);
14203 break;
14204 }
14205 }
14206 }
14207
Michael Chan4a29cc22006-03-19 13:21:12 -080014208 /* The EPB bridge inside 5714, 5715, and 5780 cannot support
14209 * DMA addresses > 40-bit. This bridge may have other additional
14210 * 57xx devices behind it in some 4-port NIC designs for example.
14211 * Any tg3 device found behind the bridge will also need the 40-bit
14212 * DMA workaround.
14213 */
Matt Carlson42b123b2012-02-13 15:20:13 +000014214 if (tg3_flag(tp, 5780_CLASS)) {
Joe Perches63c3a662011-04-26 08:12:10 +000014215 tg3_flag_set(tp, 40BIT_DMA_BUG);
Michael Chan4cf78e42005-07-25 12:29:19 -070014216 tp->msi_cap = pci_find_capability(tp->pdev, PCI_CAP_ID_MSI);
Matt Carlson859a588792010-04-05 10:19:28 +000014217 } else {
Michael Chan4a29cc22006-03-19 13:21:12 -080014218 struct pci_dev *bridge = NULL;
14219
14220 do {
14221 bridge = pci_get_device(PCI_VENDOR_ID_SERVERWORKS,
14222 PCI_DEVICE_ID_SERVERWORKS_EPB,
14223 bridge);
14224 if (bridge && bridge->subordinate &&
14225 (bridge->subordinate->number <=
14226 tp->pdev->bus->number) &&
14227 (bridge->subordinate->subordinate >=
14228 tp->pdev->bus->number)) {
Joe Perches63c3a662011-04-26 08:12:10 +000014229 tg3_flag_set(tp, 40BIT_DMA_BUG);
Michael Chan4a29cc22006-03-19 13:21:12 -080014230 pci_dev_put(bridge);
14231 break;
14232 }
14233 } while (bridge);
14234 }
Michael Chan4cf78e42005-07-25 12:29:19 -070014235
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000014236 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
Matt Carlson3a1e19d2011-07-13 09:27:32 +000014237 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714)
Michael Chan7544b092007-05-05 13:08:32 -070014238 tp->pdev_peer = tg3_find_peer(tp);
14239
Matt Carlson507399f2009-11-13 13:03:37 +000014240 /* Determine TSO capabilities */
Matt Carlsona0512942011-07-27 14:20:54 +000014241 if (tp->pci_chip_rev_id == CHIPREV_ID_5719_A0)
Matt Carlson4d163b72011-01-25 15:58:48 +000014242 ; /* Do nothing. HW bug. */
Joe Perches63c3a662011-04-26 08:12:10 +000014243 else if (tg3_flag(tp, 57765_PLUS))
14244 tg3_flag_set(tp, HW_TSO_3);
14245 else if (tg3_flag(tp, 5755_PLUS) ||
Matt Carlsone849cdc2009-11-13 13:03:38 +000014246 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
Joe Perches63c3a662011-04-26 08:12:10 +000014247 tg3_flag_set(tp, HW_TSO_2);
14248 else if (tg3_flag(tp, 5750_PLUS)) {
14249 tg3_flag_set(tp, HW_TSO_1);
14250 tg3_flag_set(tp, TSO_BUG);
Matt Carlson507399f2009-11-13 13:03:37 +000014251 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 &&
14252 tp->pci_chip_rev_id >= CHIPREV_ID_5750_C2)
Joe Perches63c3a662011-04-26 08:12:10 +000014253 tg3_flag_clear(tp, TSO_BUG);
Matt Carlson507399f2009-11-13 13:03:37 +000014254 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
14255 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701 &&
14256 tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) {
Joe Perches63c3a662011-04-26 08:12:10 +000014257 tg3_flag_set(tp, TSO_BUG);
Matt Carlson507399f2009-11-13 13:03:37 +000014258 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705)
14259 tp->fw_needed = FIRMWARE_TG3TSO5;
14260 else
14261 tp->fw_needed = FIRMWARE_TG3TSO;
14262 }
14263
Matt Carlsondabc5c62011-05-19 12:12:52 +000014264 /* Selectively allow TSO based on operating conditions */
Matt Carlson6ff6f812011-05-19 12:12:54 +000014265 if (tg3_flag(tp, HW_TSO_1) ||
14266 tg3_flag(tp, HW_TSO_2) ||
14267 tg3_flag(tp, HW_TSO_3) ||
Matt Carlsoncf9ecf42011-11-28 09:41:03 +000014268 tp->fw_needed) {
14269 /* For firmware TSO, assume ASF is disabled.
14270 * We'll disable TSO later if we discover ASF
14271 * is enabled in tg3_get_eeprom_hw_cfg().
14272 */
Matt Carlsondabc5c62011-05-19 12:12:52 +000014273 tg3_flag_set(tp, TSO_CAPABLE);
Matt Carlsoncf9ecf42011-11-28 09:41:03 +000014274 } else {
Matt Carlsondabc5c62011-05-19 12:12:52 +000014275 tg3_flag_clear(tp, TSO_CAPABLE);
14276 tg3_flag_clear(tp, TSO_BUG);
14277 tp->fw_needed = NULL;
14278 }
14279
14280 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0)
14281 tp->fw_needed = FIRMWARE_TG3;
14282
Matt Carlson507399f2009-11-13 13:03:37 +000014283 tp->irq_max = 1;
14284
Joe Perches63c3a662011-04-26 08:12:10 +000014285 if (tg3_flag(tp, 5750_PLUS)) {
14286 tg3_flag_set(tp, SUPPORT_MSI);
Michael Chan7544b092007-05-05 13:08:32 -070014287 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_AX ||
14288 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_BX ||
14289 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714 &&
14290 tp->pci_chip_rev_id <= CHIPREV_ID_5714_A2 &&
14291 tp->pdev_peer == tp->pdev))
Joe Perches63c3a662011-04-26 08:12:10 +000014292 tg3_flag_clear(tp, SUPPORT_MSI);
Michael Chan7544b092007-05-05 13:08:32 -070014293
Joe Perches63c3a662011-04-26 08:12:10 +000014294 if (tg3_flag(tp, 5755_PLUS) ||
Michael Chanb5d37722006-09-27 16:06:21 -070014295 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Joe Perches63c3a662011-04-26 08:12:10 +000014296 tg3_flag_set(tp, 1SHOT_MSI);
Michael Chan52c0fd82006-06-29 20:15:54 -070014297 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014298
Joe Perches63c3a662011-04-26 08:12:10 +000014299 if (tg3_flag(tp, 57765_PLUS)) {
14300 tg3_flag_set(tp, SUPPORT_MSIX);
Matt Carlson507399f2009-11-13 13:03:37 +000014301 tp->irq_max = TG3_IRQ_MAX_VECS;
Matt Carlson90415472011-12-16 13:33:23 +000014302 tg3_rss_init_dflt_indir_tbl(tp);
Matt Carlson507399f2009-11-13 13:03:37 +000014303 }
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000014304 }
Matt Carlson0e1406d2009-11-02 12:33:33 +000014305
Matt Carlson2ffcc982011-05-19 12:12:44 +000014306 if (tg3_flag(tp, 5755_PLUS))
Joe Perches63c3a662011-04-26 08:12:10 +000014307 tg3_flag_set(tp, SHORT_DMA_BUG);
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000014308
Matt Carlsone31aa982011-07-27 14:20:53 +000014309 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
Matt Carlsona4cb4282011-12-14 11:09:58 +000014310 tp->dma_limit = TG3_TX_BD_DMA_MAX_4K;
Matt Carlsone31aa982011-07-27 14:20:53 +000014311
Matt Carlsonfa6b2aa2011-11-21 15:01:19 +000014312 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
14313 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
14314 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
Joe Perches63c3a662011-04-26 08:12:10 +000014315 tg3_flag_set(tp, LRG_PROD_RING_CAP);
Matt Carlsonde9f5232011-04-05 14:22:43 +000014316
Joe Perches63c3a662011-04-26 08:12:10 +000014317 if (tg3_flag(tp, 57765_PLUS) &&
Matt Carlsona0512942011-07-27 14:20:54 +000014318 tp->pci_chip_rev_id != CHIPREV_ID_5719_A0)
Joe Perches63c3a662011-04-26 08:12:10 +000014319 tg3_flag_set(tp, USE_JUMBO_BDFLAG);
Matt Carlsonb703df62009-12-03 08:36:21 +000014320
Joe Perches63c3a662011-04-26 08:12:10 +000014321 if (!tg3_flag(tp, 5705_PLUS) ||
14322 tg3_flag(tp, 5780_CLASS) ||
14323 tg3_flag(tp, USE_JUMBO_BDFLAG))
14324 tg3_flag_set(tp, JUMBO_CAPABLE);
Michael Chan0f893dc2005-07-25 12:30:38 -070014325
Matt Carlson52f44902008-11-21 17:17:04 -080014326 pci_read_config_dword(tp->pdev, TG3PCI_PCISTATE,
14327 &pci_state_reg);
14328
Jon Mason708ebb3a2011-06-27 12:56:50 +000014329 if (pci_is_pcie(tp->pdev)) {
Matt Carlson5e7dfd02008-11-21 17:18:16 -080014330 u16 lnkctl;
14331
Joe Perches63c3a662011-04-26 08:12:10 +000014332 tg3_flag_set(tp, PCI_EXPRESS);
Matt Carlson5f5c51e2007-11-12 21:19:37 -080014333
Matt Carlson5e7dfd02008-11-21 17:18:16 -080014334 pci_read_config_word(tp->pdev,
Jon Mason708ebb3a2011-06-27 12:56:50 +000014335 pci_pcie_cap(tp->pdev) + PCI_EXP_LNKCTL,
Matt Carlson5e7dfd02008-11-21 17:18:16 -080014336 &lnkctl);
14337 if (lnkctl & PCI_EXP_LNKCTL_CLKREQ_EN) {
Matt Carlson7196cd62011-05-19 16:02:44 +000014338 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
14339 ASIC_REV_5906) {
Joe Perches63c3a662011-04-26 08:12:10 +000014340 tg3_flag_clear(tp, HW_TSO_2);
Matt Carlsondabc5c62011-05-19 12:12:52 +000014341 tg3_flag_clear(tp, TSO_CAPABLE);
Matt Carlson7196cd62011-05-19 16:02:44 +000014342 }
Matt Carlson5e7dfd02008-11-21 17:18:16 -080014343 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson321d32a2008-11-21 17:22:19 -080014344 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson9cf74eb2009-04-20 06:58:27 +000014345 tp->pci_chip_rev_id == CHIPREV_ID_57780_A0 ||
14346 tp->pci_chip_rev_id == CHIPREV_ID_57780_A1)
Joe Perches63c3a662011-04-26 08:12:10 +000014347 tg3_flag_set(tp, CLKREQ_BUG);
Matt Carlson614b0592010-01-20 16:58:02 +000014348 } else if (tp->pci_chip_rev_id == CHIPREV_ID_5717_A0) {
Joe Perches63c3a662011-04-26 08:12:10 +000014349 tg3_flag_set(tp, L1PLLPD_EN);
Michael Chanc7835a72006-11-15 21:14:42 -080014350 }
Matt Carlson52f44902008-11-21 17:17:04 -080014351 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785) {
Jon Mason708ebb3a2011-06-27 12:56:50 +000014352 /* BCM5785 devices are effectively PCIe devices, and should
14353 * follow PCIe codepaths, but do not have a PCIe capabilities
14354 * section.
Matt Carlson93a700a2011-08-31 11:44:54 +000014355 */
Joe Perches63c3a662011-04-26 08:12:10 +000014356 tg3_flag_set(tp, PCI_EXPRESS);
14357 } else if (!tg3_flag(tp, 5705_PLUS) ||
14358 tg3_flag(tp, 5780_CLASS)) {
Matt Carlson52f44902008-11-21 17:17:04 -080014359 tp->pcix_cap = pci_find_capability(tp->pdev, PCI_CAP_ID_PCIX);
14360 if (!tp->pcix_cap) {
Matt Carlson2445e462010-04-05 10:19:21 +000014361 dev_err(&tp->pdev->dev,
14362 "Cannot find PCI-X capability, aborting\n");
Matt Carlson52f44902008-11-21 17:17:04 -080014363 return -EIO;
14364 }
14365
14366 if (!(pci_state_reg & PCISTATE_CONV_PCI_MODE))
Joe Perches63c3a662011-04-26 08:12:10 +000014367 tg3_flag_set(tp, PCIX_MODE);
Matt Carlson52f44902008-11-21 17:17:04 -080014368 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014369
Michael Chan399de502005-10-03 14:02:39 -070014370 /* If we have an AMD 762 or VIA K8T800 chipset, write
14371 * reordering to the mailbox registers done by the host
14372 * controller can cause major troubles. We read back from
14373 * every mailbox register write to force the writes to be
14374 * posted to the chip in order.
14375 */
Matt Carlson41434702011-03-09 16:58:22 +000014376 if (pci_dev_present(tg3_write_reorder_chipsets) &&
Joe Perches63c3a662011-04-26 08:12:10 +000014377 !tg3_flag(tp, PCI_EXPRESS))
14378 tg3_flag_set(tp, MBOX_WRITE_REORDER);
Michael Chan399de502005-10-03 14:02:39 -070014379
Matt Carlson69fc4052008-12-21 20:19:57 -080014380 pci_read_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE,
14381 &tp->pci_cacheline_sz);
14382 pci_read_config_byte(tp->pdev, PCI_LATENCY_TIMER,
14383 &tp->pci_lat_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014384 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 &&
14385 tp->pci_lat_timer < 64) {
14386 tp->pci_lat_timer = 64;
Matt Carlson69fc4052008-12-21 20:19:57 -080014387 pci_write_config_byte(tp->pdev, PCI_LATENCY_TIMER,
14388 tp->pci_lat_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014389 }
14390
Matt Carlson16821282011-07-13 09:27:28 +000014391 /* Important! -- It is critical that the PCI-X hw workaround
14392 * situation is decided before the first MMIO register access.
14393 */
Matt Carlson52f44902008-11-21 17:17:04 -080014394 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5700_BX) {
14395 /* 5700 BX chips need to have their TX producer index
14396 * mailboxes written twice to workaround a bug.
14397 */
Joe Perches63c3a662011-04-26 08:12:10 +000014398 tg3_flag_set(tp, TXD_MBOX_HWBUG);
Matt Carlson9974a352007-10-07 23:27:28 -070014399
Matt Carlson52f44902008-11-21 17:17:04 -080014400 /* If we are in PCI-X mode, enable register write workaround.
Linus Torvalds1da177e2005-04-16 15:20:36 -070014401 *
14402 * The workaround is to use indirect register accesses
14403 * for all chip writes not to mailbox registers.
14404 */
Joe Perches63c3a662011-04-26 08:12:10 +000014405 if (tg3_flag(tp, PCIX_MODE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070014406 u32 pm_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014407
Joe Perches63c3a662011-04-26 08:12:10 +000014408 tg3_flag_set(tp, PCIX_TARGET_HWBUG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014409
14410 /* The chip can have it's power management PCI config
14411 * space registers clobbered due to this bug.
14412 * So explicitly force the chip into D0 here.
14413 */
Matt Carlson9974a352007-10-07 23:27:28 -070014414 pci_read_config_dword(tp->pdev,
14415 tp->pm_cap + PCI_PM_CTRL,
Linus Torvalds1da177e2005-04-16 15:20:36 -070014416 &pm_reg);
14417 pm_reg &= ~PCI_PM_CTRL_STATE_MASK;
14418 pm_reg |= PCI_PM_CTRL_PME_ENABLE | 0 /* D0 */;
Matt Carlson9974a352007-10-07 23:27:28 -070014419 pci_write_config_dword(tp->pdev,
14420 tp->pm_cap + PCI_PM_CTRL,
Linus Torvalds1da177e2005-04-16 15:20:36 -070014421 pm_reg);
14422
14423 /* Also, force SERR#/PERR# in PCI command. */
14424 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
14425 pci_cmd |= PCI_COMMAND_PARITY | PCI_COMMAND_SERR;
14426 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
14427 }
14428 }
14429
Linus Torvalds1da177e2005-04-16 15:20:36 -070014430 if ((pci_state_reg & PCISTATE_BUS_SPEED_HIGH) != 0)
Joe Perches63c3a662011-04-26 08:12:10 +000014431 tg3_flag_set(tp, PCI_HIGH_SPEED);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014432 if ((pci_state_reg & PCISTATE_BUS_32BIT) != 0)
Joe Perches63c3a662011-04-26 08:12:10 +000014433 tg3_flag_set(tp, PCI_32BIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014434
14435 /* Chip-specific fixup from Broadcom driver */
14436 if ((tp->pci_chip_rev_id == CHIPREV_ID_5704_A0) &&
14437 (!(pci_state_reg & PCISTATE_RETRY_SAME_DMA))) {
14438 pci_state_reg |= PCISTATE_RETRY_SAME_DMA;
14439 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE, pci_state_reg);
14440 }
14441
Michael Chan1ee582d2005-08-09 20:16:46 -070014442 /* Default fast path register access methods */
Michael Chan20094932005-08-09 20:16:32 -070014443 tp->read32 = tg3_read32;
Michael Chan1ee582d2005-08-09 20:16:46 -070014444 tp->write32 = tg3_write32;
Michael Chan09ee9292005-08-09 20:17:00 -070014445 tp->read32_mbox = tg3_read32;
Michael Chan20094932005-08-09 20:16:32 -070014446 tp->write32_mbox = tg3_write32;
Michael Chan1ee582d2005-08-09 20:16:46 -070014447 tp->write32_tx_mbox = tg3_write32;
14448 tp->write32_rx_mbox = tg3_write32;
14449
14450 /* Various workaround register access methods */
Joe Perches63c3a662011-04-26 08:12:10 +000014451 if (tg3_flag(tp, PCIX_TARGET_HWBUG))
Michael Chan1ee582d2005-08-09 20:16:46 -070014452 tp->write32 = tg3_write_indirect_reg32;
Matt Carlson98efd8a2007-05-05 12:47:25 -070014453 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701 ||
Joe Perches63c3a662011-04-26 08:12:10 +000014454 (tg3_flag(tp, PCI_EXPRESS) &&
Matt Carlson98efd8a2007-05-05 12:47:25 -070014455 tp->pci_chip_rev_id == CHIPREV_ID_5750_A0)) {
14456 /*
14457 * Back to back register writes can cause problems on these
14458 * chips, the workaround is to read back all reg writes
14459 * except those to mailbox regs.
14460 *
14461 * See tg3_write_indirect_reg32().
14462 */
Michael Chan1ee582d2005-08-09 20:16:46 -070014463 tp->write32 = tg3_write_flush_reg32;
Matt Carlson98efd8a2007-05-05 12:47:25 -070014464 }
14465
Joe Perches63c3a662011-04-26 08:12:10 +000014466 if (tg3_flag(tp, TXD_MBOX_HWBUG) || tg3_flag(tp, MBOX_WRITE_REORDER)) {
Michael Chan1ee582d2005-08-09 20:16:46 -070014467 tp->write32_tx_mbox = tg3_write32_tx_mbox;
Joe Perches63c3a662011-04-26 08:12:10 +000014468 if (tg3_flag(tp, MBOX_WRITE_REORDER))
Michael Chan1ee582d2005-08-09 20:16:46 -070014469 tp->write32_rx_mbox = tg3_write_flush_reg32;
14470 }
Michael Chan20094932005-08-09 20:16:32 -070014471
Joe Perches63c3a662011-04-26 08:12:10 +000014472 if (tg3_flag(tp, ICH_WORKAROUND)) {
Michael Chan68929142005-08-09 20:17:14 -070014473 tp->read32 = tg3_read_indirect_reg32;
14474 tp->write32 = tg3_write_indirect_reg32;
14475 tp->read32_mbox = tg3_read_indirect_mbox;
14476 tp->write32_mbox = tg3_write_indirect_mbox;
14477 tp->write32_tx_mbox = tg3_write_indirect_mbox;
14478 tp->write32_rx_mbox = tg3_write_indirect_mbox;
14479
14480 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070014481 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070014482
14483 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
14484 pci_cmd &= ~PCI_COMMAND_MEMORY;
14485 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
14486 }
Michael Chanb5d37722006-09-27 16:06:21 -070014487 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
14488 tp->read32_mbox = tg3_read32_mbox_5906;
14489 tp->write32_mbox = tg3_write32_mbox_5906;
14490 tp->write32_tx_mbox = tg3_write32_mbox_5906;
14491 tp->write32_rx_mbox = tg3_write32_mbox_5906;
14492 }
Michael Chan68929142005-08-09 20:17:14 -070014493
Michael Chanbbadf502006-04-06 21:46:34 -070014494 if (tp->write32 == tg3_write_indirect_reg32 ||
Joe Perches63c3a662011-04-26 08:12:10 +000014495 (tg3_flag(tp, PCIX_MODE) &&
Michael Chanbbadf502006-04-06 21:46:34 -070014496 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
David S. Millerf49639e2006-06-09 11:58:36 -070014497 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)))
Joe Perches63c3a662011-04-26 08:12:10 +000014498 tg3_flag_set(tp, SRAM_USE_CONFIG);
Michael Chanbbadf502006-04-06 21:46:34 -070014499
Matt Carlson16821282011-07-13 09:27:28 +000014500 /* The memory arbiter has to be enabled in order for SRAM accesses
14501 * to succeed. Normally on powerup the tg3 chip firmware will make
14502 * sure it is enabled, but other entities such as system netboot
14503 * code might disable it.
14504 */
14505 val = tr32(MEMARB_MODE);
14506 tw32(MEMARB_MODE, val | MEMARB_MODE_ENABLE);
14507
Matt Carlson9dc5e342011-11-04 09:15:02 +000014508 tp->pci_fn = PCI_FUNC(tp->pdev->devfn) & 3;
14509 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
14510 tg3_flag(tp, 5780_CLASS)) {
14511 if (tg3_flag(tp, PCIX_MODE)) {
14512 pci_read_config_dword(tp->pdev,
14513 tp->pcix_cap + PCI_X_STATUS,
14514 &val);
14515 tp->pci_fn = val & 0x7;
14516 }
14517 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717) {
14518 tg3_read_mem(tp, NIC_SRAM_CPMU_STATUS, &val);
14519 if ((val & NIC_SRAM_CPMUSTAT_SIG_MSK) ==
14520 NIC_SRAM_CPMUSTAT_SIG) {
14521 tp->pci_fn = val & TG3_CPMU_STATUS_FMSK_5717;
14522 tp->pci_fn = tp->pci_fn ? 1 : 0;
14523 }
14524 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
14525 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
14526 tg3_read_mem(tp, NIC_SRAM_CPMU_STATUS, &val);
14527 if ((val & NIC_SRAM_CPMUSTAT_SIG_MSK) ==
14528 NIC_SRAM_CPMUSTAT_SIG) {
14529 tp->pci_fn = (val & TG3_CPMU_STATUS_FMSK_5719) >>
14530 TG3_CPMU_STATUS_FSHFT_5719;
14531 }
Matt Carlson69f11c92011-07-13 09:27:30 +000014532 }
14533
Michael Chan7d0c41e2005-04-21 17:06:20 -070014534 /* Get eeprom hw config before calling tg3_set_power_state().
Joe Perches63c3a662011-04-26 08:12:10 +000014535 * In particular, the TG3_FLAG_IS_NIC flag must be
Michael Chan7d0c41e2005-04-21 17:06:20 -070014536 * determined before calling tg3_set_power_state() so that
14537 * we know whether or not to switch out of Vaux power.
14538 * When the flag is set, it means that GPIO1 is used for eeprom
14539 * write protect and also implies that it is a LOM where GPIOs
14540 * are not used to switch power.
Jeff Garzik6aa20a22006-09-13 13:24:59 -040014541 */
Michael Chan7d0c41e2005-04-21 17:06:20 -070014542 tg3_get_eeprom_hw_cfg(tp);
14543
Matt Carlsoncf9ecf42011-11-28 09:41:03 +000014544 if (tp->fw_needed && tg3_flag(tp, ENABLE_ASF)) {
14545 tg3_flag_clear(tp, TSO_CAPABLE);
14546 tg3_flag_clear(tp, TSO_BUG);
14547 tp->fw_needed = NULL;
14548 }
14549
Joe Perches63c3a662011-04-26 08:12:10 +000014550 if (tg3_flag(tp, ENABLE_APE)) {
Matt Carlson0d3031d2007-10-10 18:02:43 -070014551 /* Allow reads and writes to the
14552 * APE register and memory space.
14553 */
14554 pci_state_reg |= PCISTATE_ALLOW_APE_CTLSPC_WR |
Matt Carlsonf92d9dc12010-06-05 17:24:30 +000014555 PCISTATE_ALLOW_APE_SHMEM_WR |
14556 PCISTATE_ALLOW_APE_PSPACE_WR;
Matt Carlson0d3031d2007-10-10 18:02:43 -070014557 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE,
14558 pci_state_reg);
Matt Carlsonc9cab242011-07-13 09:27:27 +000014559
14560 tg3_ape_lock_init(tp);
Matt Carlson0d3031d2007-10-10 18:02:43 -070014561 }
14562
Matt Carlson16821282011-07-13 09:27:28 +000014563 /* Set up tp->grc_local_ctrl before calling
14564 * tg3_pwrsrc_switch_to_vmain(). GPIO1 driven high
14565 * will bring 5700's external PHY out of reset.
Michael Chan314fba32005-04-21 17:07:04 -070014566 * It is also used as eeprom write protect on LOMs.
14567 */
14568 tp->grc_local_ctrl = GRC_LCLCTRL_INT_ON_ATTN | GRC_LCLCTRL_AUTO_SEEPROM;
Matt Carlson6ff6f812011-05-19 12:12:54 +000014569 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
Joe Perches63c3a662011-04-26 08:12:10 +000014570 tg3_flag(tp, EEPROM_WRITE_PROT))
Michael Chan314fba32005-04-21 17:07:04 -070014571 tp->grc_local_ctrl |= (GRC_LCLCTRL_GPIO_OE1 |
14572 GRC_LCLCTRL_GPIO_OUTPUT1);
Michael Chan3e7d83b2005-04-21 17:10:36 -070014573 /* Unused GPIO3 must be driven as output on 5752 because there
14574 * are no pull-up resistors on unused GPIO pins.
14575 */
14576 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752)
14577 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE3;
Michael Chan314fba32005-04-21 17:07:04 -070014578
Matt Carlson321d32a2008-11-21 17:22:19 -080014579 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
Matt Carlsoncb4ed1f2010-01-20 16:58:09 +000014580 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
Matt Carlson55086ad2011-12-14 11:09:59 +000014581 tg3_flag(tp, 57765_CLASS))
Michael Chanaf36e6b2006-03-23 01:28:06 -080014582 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_UART_SEL;
14583
Matt Carlson8d519ab2009-04-20 06:58:01 +000014584 if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761 ||
14585 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761S) {
Matt Carlson5f0c4a32008-06-09 15:41:12 -070014586 /* Turn off the debug UART. */
14587 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_UART_SEL;
Joe Perches63c3a662011-04-26 08:12:10 +000014588 if (tg3_flag(tp, IS_NIC))
Matt Carlson5f0c4a32008-06-09 15:41:12 -070014589 /* Keep VMain power. */
14590 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE0 |
14591 GRC_LCLCTRL_GPIO_OUTPUT0;
14592 }
14593
Matt Carlson16821282011-07-13 09:27:28 +000014594 /* Switch out of Vaux if it is a NIC */
14595 tg3_pwrsrc_switch_to_vmain(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014596
Linus Torvalds1da177e2005-04-16 15:20:36 -070014597 /* Derive initial jumbo mode from MTU assigned in
14598 * ether_setup() via the alloc_etherdev() call
14599 */
Joe Perches63c3a662011-04-26 08:12:10 +000014600 if (tp->dev->mtu > ETH_DATA_LEN && !tg3_flag(tp, 5780_CLASS))
14601 tg3_flag_set(tp, JUMBO_RING_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014602
14603 /* Determine WakeOnLan speed to use. */
14604 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
14605 tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
14606 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0 ||
14607 tp->pci_chip_rev_id == CHIPREV_ID_5701_B2) {
Joe Perches63c3a662011-04-26 08:12:10 +000014608 tg3_flag_clear(tp, WOL_SPEED_100MB);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014609 } else {
Joe Perches63c3a662011-04-26 08:12:10 +000014610 tg3_flag_set(tp, WOL_SPEED_100MB);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014611 }
14612
Matt Carlson7f97a4b2009-08-25 10:10:03 +000014613 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014614 tp->phy_flags |= TG3_PHYFLG_IS_FET;
Matt Carlson7f97a4b2009-08-25 10:10:03 +000014615
Linus Torvalds1da177e2005-04-16 15:20:36 -070014616 /* A few boards don't want Ethernet@WireSpeed phy feature */
Matt Carlson6ff6f812011-05-19 12:12:54 +000014617 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
14618 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070014619 (tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) &&
Michael Chan747e8f82005-07-25 12:33:22 -070014620 (tp->pci_chip_rev_id != CHIPREV_ID_5705_A1)) ||
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014621 (tp->phy_flags & TG3_PHYFLG_IS_FET) ||
14622 (tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
14623 tp->phy_flags |= TG3_PHYFLG_NO_ETH_WIRE_SPEED;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014624
14625 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5703_AX ||
14626 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5704_AX)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014627 tp->phy_flags |= TG3_PHYFLG_ADC_BUG;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014628 if (tp->pci_chip_rev_id == CHIPREV_ID_5704_A0)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014629 tp->phy_flags |= TG3_PHYFLG_5704_A0_BUG;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014630
Joe Perches63c3a662011-04-26 08:12:10 +000014631 if (tg3_flag(tp, 5705_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014632 !(tp->phy_flags & TG3_PHYFLG_IS_FET) &&
Matt Carlson321d32a2008-11-21 17:22:19 -080014633 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000014634 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_57780 &&
Joe Perches63c3a662011-04-26 08:12:10 +000014635 !tg3_flag(tp, 57765_PLUS)) {
Michael Chanc424cb22006-04-29 18:56:34 -070014636 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
Matt Carlsond30cdd22007-10-07 23:28:35 -070014637 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787 ||
Matt Carlson9936bcf2007-10-10 18:03:07 -070014638 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
14639 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761) {
Michael Chand4011ad2007-02-13 12:17:25 -080014640 if (tp->pdev->device != PCI_DEVICE_ID_TIGON3_5756 &&
14641 tp->pdev->device != PCI_DEVICE_ID_TIGON3_5722)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014642 tp->phy_flags |= TG3_PHYFLG_JITTER_BUG;
Michael Chanc1d2a192007-01-08 19:57:20 -080014643 if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5755M)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014644 tp->phy_flags |= TG3_PHYFLG_ADJUST_TRIM;
Matt Carlson321d32a2008-11-21 17:22:19 -080014645 } else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014646 tp->phy_flags |= TG3_PHYFLG_BER_BUG;
Michael Chanc424cb22006-04-29 18:56:34 -070014647 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014648
Matt Carlsonb2a5c192008-04-03 21:44:44 -070014649 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
14650 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX) {
14651 tp->phy_otp = tg3_read_otp_phycfg(tp);
14652 if (tp->phy_otp == 0)
14653 tp->phy_otp = TG3_OTP_DEFAULT;
14654 }
14655
Joe Perches63c3a662011-04-26 08:12:10 +000014656 if (tg3_flag(tp, CPMU_PRESENT))
Matt Carlson8ef21422008-05-02 16:47:53 -070014657 tp->mi_mode = MAC_MI_MODE_500KHZ_CONST;
14658 else
14659 tp->mi_mode = MAC_MI_MODE_BASE;
14660
Linus Torvalds1da177e2005-04-16 15:20:36 -070014661 tp->coalesce_mode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014662 if (GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5700_AX &&
14663 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5700_BX)
14664 tp->coalesce_mode |= HOSTCC_MODE_32BYTE;
14665
Matt Carlson4d958472011-04-20 07:57:35 +000014666 /* Set these bits to enable statistics workaround. */
14667 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
14668 tp->pci_chip_rev_id == CHIPREV_ID_5719_A0 ||
14669 tp->pci_chip_rev_id == CHIPREV_ID_5720_A0) {
14670 tp->coalesce_mode |= HOSTCC_MODE_ATTN;
14671 tp->grc_mode |= GRC_MODE_IRQ_ON_FLOW_ATTN;
14672 }
14673
Matt Carlson321d32a2008-11-21 17:22:19 -080014674 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
14675 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
Joe Perches63c3a662011-04-26 08:12:10 +000014676 tg3_flag_set(tp, USE_PHYLIB);
Matt Carlson57e69832008-05-25 23:48:31 -070014677
Matt Carlson158d7ab2008-05-29 01:37:54 -070014678 err = tg3_mdio_init(tp);
14679 if (err)
14680 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014681
14682 /* Initialize data/descriptor byte/word swapping. */
14683 val = tr32(GRC_MODE);
Matt Carlsonf2096f92011-04-05 14:22:48 +000014684 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
14685 val &= (GRC_MODE_BYTE_SWAP_B2HRX_DATA |
14686 GRC_MODE_WORD_SWAP_B2HRX_DATA |
14687 GRC_MODE_B2HRX_ENABLE |
14688 GRC_MODE_HTX2B_ENABLE |
14689 GRC_MODE_HOST_STACKUP);
14690 else
14691 val &= GRC_MODE_HOST_STACKUP;
14692
Linus Torvalds1da177e2005-04-16 15:20:36 -070014693 tw32(GRC_MODE, val | tp->grc_mode);
14694
14695 tg3_switch_clocks(tp);
14696
14697 /* Clear this out for sanity. */
14698 tw32(TG3PCI_MEM_WIN_BASE_ADDR, 0);
14699
14700 pci_read_config_dword(tp->pdev, TG3PCI_PCISTATE,
14701 &pci_state_reg);
14702 if ((pci_state_reg & PCISTATE_CONV_PCI_MODE) == 0 &&
Joe Perches63c3a662011-04-26 08:12:10 +000014703 !tg3_flag(tp, PCIX_TARGET_HWBUG)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070014704 u32 chiprevid = GET_CHIP_REV_ID(tp->misc_host_ctrl);
14705
14706 if (chiprevid == CHIPREV_ID_5701_A0 ||
14707 chiprevid == CHIPREV_ID_5701_B0 ||
14708 chiprevid == CHIPREV_ID_5701_B2 ||
14709 chiprevid == CHIPREV_ID_5701_B5) {
14710 void __iomem *sram_base;
14711
14712 /* Write some dummy words into the SRAM status block
14713 * area, see if it reads back correctly. If the return
14714 * value is bad, force enable the PCIX workaround.
14715 */
14716 sram_base = tp->regs + NIC_SRAM_WIN_BASE + NIC_SRAM_STATS_BLK;
14717
14718 writel(0x00000000, sram_base);
14719 writel(0x00000000, sram_base + 4);
14720 writel(0xffffffff, sram_base + 4);
14721 if (readl(sram_base) != 0x00000000)
Joe Perches63c3a662011-04-26 08:12:10 +000014722 tg3_flag_set(tp, PCIX_TARGET_HWBUG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014723 }
14724 }
14725
14726 udelay(50);
14727 tg3_nvram_init(tp);
14728
14729 grc_misc_cfg = tr32(GRC_MISC_CFG);
14730 grc_misc_cfg &= GRC_MISC_CFG_BOARD_ID_MASK;
14731
Linus Torvalds1da177e2005-04-16 15:20:36 -070014732 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
14733 (grc_misc_cfg == GRC_MISC_CFG_BOARD_ID_5788 ||
14734 grc_misc_cfg == GRC_MISC_CFG_BOARD_ID_5788M))
Joe Perches63c3a662011-04-26 08:12:10 +000014735 tg3_flag_set(tp, IS_5788);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014736
Joe Perches63c3a662011-04-26 08:12:10 +000014737 if (!tg3_flag(tp, IS_5788) &&
Matt Carlson6ff6f812011-05-19 12:12:54 +000014738 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700)
Joe Perches63c3a662011-04-26 08:12:10 +000014739 tg3_flag_set(tp, TAGGED_STATUS);
14740 if (tg3_flag(tp, TAGGED_STATUS)) {
David S. Millerfac9b832005-05-18 22:46:34 -070014741 tp->coalesce_mode |= (HOSTCC_MODE_CLRTICK_RXBD |
14742 HOSTCC_MODE_CLRTICK_TXBD);
14743
14744 tp->misc_host_ctrl |= MISC_HOST_CTRL_TAGGED_STATUS;
14745 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
14746 tp->misc_host_ctrl);
14747 }
14748
Matt Carlson3bda1252008-08-15 14:08:22 -070014749 /* Preserve the APE MAC_MODE bits */
Joe Perches63c3a662011-04-26 08:12:10 +000014750 if (tg3_flag(tp, ENABLE_APE))
Matt Carlsond2394e6b2010-11-24 08:31:47 +000014751 tp->mac_mode = MAC_MODE_APE_TX_EN | MAC_MODE_APE_RX_EN;
Matt Carlson3bda1252008-08-15 14:08:22 -070014752 else
Matt Carlson6e01b202011-08-19 13:58:20 +000014753 tp->mac_mode = 0;
Matt Carlson3bda1252008-08-15 14:08:22 -070014754
Linus Torvalds1da177e2005-04-16 15:20:36 -070014755 /* these are limited to 10/100 only */
14756 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 &&
14757 (grc_misc_cfg == 0x8000 || grc_misc_cfg == 0x4000)) ||
14758 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
14759 tp->pdev->vendor == PCI_VENDOR_ID_BROADCOM &&
14760 (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5901 ||
14761 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5901_2 ||
14762 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5705F)) ||
14763 (tp->pdev->vendor == PCI_VENDOR_ID_BROADCOM &&
14764 (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5751F ||
Michael Chan676917d2006-12-07 00:20:22 -080014765 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5753F ||
14766 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5787F)) ||
Matt Carlson321d32a2008-11-21 17:22:19 -080014767 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57790 ||
Matt Carlsond1101142010-02-17 15:16:55 +000014768 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57791 ||
14769 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57795 ||
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014770 (tp->phy_flags & TG3_PHYFLG_IS_FET))
14771 tp->phy_flags |= TG3_PHYFLG_10_100_ONLY;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014772
14773 err = tg3_phy_probe(tp);
14774 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000014775 dev_err(&tp->pdev->dev, "phy probe failed, err %d\n", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014776 /* ... but do not return immediately ... */
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070014777 tg3_mdio_fini(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014778 }
14779
Matt Carlson184b8902010-04-05 10:19:25 +000014780 tg3_read_vpd(tp);
Michael Chanc4e65752006-03-20 22:29:32 -080014781 tg3_read_fw_ver(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014782
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014783 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
14784 tp->phy_flags &= ~TG3_PHYFLG_USE_MI_INTERRUPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014785 } else {
14786 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014787 tp->phy_flags |= TG3_PHYFLG_USE_MI_INTERRUPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014788 else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014789 tp->phy_flags &= ~TG3_PHYFLG_USE_MI_INTERRUPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014790 }
14791
14792 /* 5700 {AX,BX} chips have a broken status block link
14793 * change bit implementation, so we must use the
14794 * status register in those cases.
14795 */
14796 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700)
Joe Perches63c3a662011-04-26 08:12:10 +000014797 tg3_flag_set(tp, USE_LINKCHG_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014798 else
Joe Perches63c3a662011-04-26 08:12:10 +000014799 tg3_flag_clear(tp, USE_LINKCHG_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014800
14801 /* The led_ctrl is set during tg3_phy_probe, here we might
14802 * have to force the link status polling mechanism based
14803 * upon subsystem IDs.
14804 */
14805 if (tp->pdev->subsystem_vendor == PCI_VENDOR_ID_DELL &&
Michael Chan007a880d2007-05-31 14:49:51 -070014806 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701 &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014807 !(tp->phy_flags & TG3_PHYFLG_PHY_SERDES)) {
14808 tp->phy_flags |= TG3_PHYFLG_USE_MI_INTERRUPT;
Joe Perches63c3a662011-04-26 08:12:10 +000014809 tg3_flag_set(tp, USE_LINKCHG_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014810 }
14811
14812 /* For all SERDES we poll the MAC status register. */
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014813 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Joe Perches63c3a662011-04-26 08:12:10 +000014814 tg3_flag_set(tp, POLL_SERDES);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014815 else
Joe Perches63c3a662011-04-26 08:12:10 +000014816 tg3_flag_clear(tp, POLL_SERDES);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014817
Eric Dumazet9205fd92011-11-18 06:47:01 +000014818 tp->rx_offset = NET_SKB_PAD + NET_IP_ALIGN;
Matt Carlsond2757fc2010-04-12 06:58:27 +000014819 tp->rx_copy_thresh = TG3_RX_COPY_THRESHOLD;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014820 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701 &&
Joe Perches63c3a662011-04-26 08:12:10 +000014821 tg3_flag(tp, PCIX_MODE)) {
Eric Dumazet9205fd92011-11-18 06:47:01 +000014822 tp->rx_offset = NET_SKB_PAD;
Matt Carlsond2757fc2010-04-12 06:58:27 +000014823#ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
Matt Carlson9dc7a112010-04-12 06:58:28 +000014824 tp->rx_copy_thresh = ~(u16)0;
Matt Carlsond2757fc2010-04-12 06:58:27 +000014825#endif
14826 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014827
Matt Carlson2c49a442010-09-30 10:34:35 +000014828 tp->rx_std_ring_mask = TG3_RX_STD_RING_SIZE(tp) - 1;
14829 tp->rx_jmb_ring_mask = TG3_RX_JMB_RING_SIZE(tp) - 1;
Matt Carlson7cb32cf2010-09-30 10:34:36 +000014830 tp->rx_ret_ring_mask = tg3_rx_ret_ring_size(tp) - 1;
14831
Matt Carlson2c49a442010-09-30 10:34:35 +000014832 tp->rx_std_max_post = tp->rx_std_ring_mask + 1;
Michael Chanf92905d2006-06-29 20:14:29 -070014833
14834 /* Increment the rx prod index on the rx std ring by at most
14835 * 8 for these chips to workaround hw errata.
14836 */
14837 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 ||
14838 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752 ||
14839 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755)
14840 tp->rx_std_max_post = 8;
14841
Joe Perches63c3a662011-04-26 08:12:10 +000014842 if (tg3_flag(tp, ASPM_WORKAROUND))
Matt Carlson8ed5d972007-05-07 00:25:49 -070014843 tp->pwrmgmt_thresh = tr32(PCIE_PWR_MGMT_THRESH) &
14844 PCIE_PWR_MGMT_L1_THRESH_MSK;
14845
Linus Torvalds1da177e2005-04-16 15:20:36 -070014846 return err;
14847}
14848
David S. Miller49b6e95f2007-03-29 01:38:42 -070014849#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070014850static int __devinit tg3_get_macaddr_sparc(struct tg3 *tp)
14851{
14852 struct net_device *dev = tp->dev;
14853 struct pci_dev *pdev = tp->pdev;
David S. Miller49b6e95f2007-03-29 01:38:42 -070014854 struct device_node *dp = pci_device_to_OF_node(pdev);
David S. Miller374d4ca2007-03-29 01:57:57 -070014855 const unsigned char *addr;
David S. Miller49b6e95f2007-03-29 01:38:42 -070014856 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014857
David S. Miller49b6e95f2007-03-29 01:38:42 -070014858 addr = of_get_property(dp, "local-mac-address", &len);
14859 if (addr && len == 6) {
14860 memcpy(dev->dev_addr, addr, 6);
14861 memcpy(dev->perm_addr, dev->dev_addr, 6);
14862 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014863 }
14864 return -ENODEV;
14865}
14866
14867static int __devinit tg3_get_default_macaddr_sparc(struct tg3 *tp)
14868{
14869 struct net_device *dev = tp->dev;
14870
14871 memcpy(dev->dev_addr, idprom->id_ethaddr, 6);
John W. Linville2ff43692005-09-12 14:44:20 -070014872 memcpy(dev->perm_addr, idprom->id_ethaddr, 6);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014873 return 0;
14874}
14875#endif
14876
14877static int __devinit tg3_get_device_address(struct tg3 *tp)
14878{
14879 struct net_device *dev = tp->dev;
14880 u32 hi, lo, mac_offset;
Michael Chan008652b2006-03-27 23:14:53 -080014881 int addr_ok = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014882
David S. Miller49b6e95f2007-03-29 01:38:42 -070014883#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070014884 if (!tg3_get_macaddr_sparc(tp))
14885 return 0;
14886#endif
14887
14888 mac_offset = 0x7c;
Matt Carlson6ff6f812011-05-19 12:12:54 +000014889 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
Joe Perches63c3a662011-04-26 08:12:10 +000014890 tg3_flag(tp, 5780_CLASS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070014891 if (tr32(TG3PCI_DUAL_MAC_CTRL) & DUAL_MAC_CTRL_ID)
14892 mac_offset = 0xcc;
14893 if (tg3_nvram_lock(tp))
14894 tw32_f(NVRAM_CMD, NVRAM_CMD_RESET);
14895 else
14896 tg3_nvram_unlock(tp);
Joe Perches63c3a662011-04-26 08:12:10 +000014897 } else if (tg3_flag(tp, 5717_PLUS)) {
Matt Carlson69f11c92011-07-13 09:27:30 +000014898 if (tp->pci_fn & 1)
Matt Carlsona1b950d2009-09-01 13:20:17 +000014899 mac_offset = 0xcc;
Matt Carlson69f11c92011-07-13 09:27:30 +000014900 if (tp->pci_fn > 1)
Matt Carlsona50d0792010-06-05 17:24:37 +000014901 mac_offset += 0x18c;
Matt Carlsona1b950d2009-09-01 13:20:17 +000014902 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
Michael Chanb5d37722006-09-27 16:06:21 -070014903 mac_offset = 0x10;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014904
14905 /* First try to get it from MAC address mailbox. */
14906 tg3_read_mem(tp, NIC_SRAM_MAC_ADDR_HIGH_MBOX, &hi);
14907 if ((hi >> 16) == 0x484b) {
14908 dev->dev_addr[0] = (hi >> 8) & 0xff;
14909 dev->dev_addr[1] = (hi >> 0) & 0xff;
14910
14911 tg3_read_mem(tp, NIC_SRAM_MAC_ADDR_LOW_MBOX, &lo);
14912 dev->dev_addr[2] = (lo >> 24) & 0xff;
14913 dev->dev_addr[3] = (lo >> 16) & 0xff;
14914 dev->dev_addr[4] = (lo >> 8) & 0xff;
14915 dev->dev_addr[5] = (lo >> 0) & 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014916
Michael Chan008652b2006-03-27 23:14:53 -080014917 /* Some old bootcode may report a 0 MAC address in SRAM */
14918 addr_ok = is_valid_ether_addr(&dev->dev_addr[0]);
14919 }
14920 if (!addr_ok) {
14921 /* Next, try NVRAM. */
Joe Perches63c3a662011-04-26 08:12:10 +000014922 if (!tg3_flag(tp, NO_NVRAM) &&
Matt Carlsondf259d82009-04-20 06:57:14 +000014923 !tg3_nvram_read_be32(tp, mac_offset + 0, &hi) &&
Matt Carlson6d348f22009-02-25 14:25:52 +000014924 !tg3_nvram_read_be32(tp, mac_offset + 4, &lo)) {
Matt Carlson62cedd12009-04-20 14:52:29 -070014925 memcpy(&dev->dev_addr[0], ((char *)&hi) + 2, 2);
14926 memcpy(&dev->dev_addr[2], (char *)&lo, sizeof(lo));
Michael Chan008652b2006-03-27 23:14:53 -080014927 }
14928 /* Finally just fetch it out of the MAC control regs. */
14929 else {
14930 hi = tr32(MAC_ADDR_0_HIGH);
14931 lo = tr32(MAC_ADDR_0_LOW);
14932
14933 dev->dev_addr[5] = lo & 0xff;
14934 dev->dev_addr[4] = (lo >> 8) & 0xff;
14935 dev->dev_addr[3] = (lo >> 16) & 0xff;
14936 dev->dev_addr[2] = (lo >> 24) & 0xff;
14937 dev->dev_addr[1] = hi & 0xff;
14938 dev->dev_addr[0] = (hi >> 8) & 0xff;
14939 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014940 }
14941
14942 if (!is_valid_ether_addr(&dev->dev_addr[0])) {
David S. Miller7582a332008-03-20 15:53:15 -070014943#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070014944 if (!tg3_get_default_macaddr_sparc(tp))
14945 return 0;
14946#endif
14947 return -EINVAL;
14948 }
John W. Linville2ff43692005-09-12 14:44:20 -070014949 memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014950 return 0;
14951}
14952
David S. Miller59e6b432005-05-18 22:50:10 -070014953#define BOUNDARY_SINGLE_CACHELINE 1
14954#define BOUNDARY_MULTI_CACHELINE 2
14955
14956static u32 __devinit tg3_calc_dma_bndry(struct tg3 *tp, u32 val)
14957{
14958 int cacheline_size;
14959 u8 byte;
14960 int goal;
14961
14962 pci_read_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE, &byte);
14963 if (byte == 0)
14964 cacheline_size = 1024;
14965 else
14966 cacheline_size = (int) byte * 4;
14967
14968 /* On 5703 and later chips, the boundary bits have no
14969 * effect.
14970 */
14971 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
14972 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701 &&
Joe Perches63c3a662011-04-26 08:12:10 +000014973 !tg3_flag(tp, PCI_EXPRESS))
David S. Miller59e6b432005-05-18 22:50:10 -070014974 goto out;
14975
14976#if defined(CONFIG_PPC64) || defined(CONFIG_IA64) || defined(CONFIG_PARISC)
14977 goal = BOUNDARY_MULTI_CACHELINE;
14978#else
14979#if defined(CONFIG_SPARC64) || defined(CONFIG_ALPHA)
14980 goal = BOUNDARY_SINGLE_CACHELINE;
14981#else
14982 goal = 0;
14983#endif
14984#endif
14985
Joe Perches63c3a662011-04-26 08:12:10 +000014986 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000014987 val = goal ? 0 : DMA_RWCTRL_DIS_CACHE_ALIGNMENT;
14988 goto out;
14989 }
14990
David S. Miller59e6b432005-05-18 22:50:10 -070014991 if (!goal)
14992 goto out;
14993
14994 /* PCI controllers on most RISC systems tend to disconnect
14995 * when a device tries to burst across a cache-line boundary.
14996 * Therefore, letting tg3 do so just wastes PCI bandwidth.
14997 *
14998 * Unfortunately, for PCI-E there are only limited
14999 * write-side controls for this, and thus for reads
15000 * we will still get the disconnects. We'll also waste
15001 * these PCI cycles for both read and write for chips
15002 * other than 5700 and 5701 which do not implement the
15003 * boundary bits.
15004 */
Joe Perches63c3a662011-04-26 08:12:10 +000015005 if (tg3_flag(tp, PCIX_MODE) && !tg3_flag(tp, PCI_EXPRESS)) {
David S. Miller59e6b432005-05-18 22:50:10 -070015006 switch (cacheline_size) {
15007 case 16:
15008 case 32:
15009 case 64:
15010 case 128:
15011 if (goal == BOUNDARY_SINGLE_CACHELINE) {
15012 val |= (DMA_RWCTRL_READ_BNDRY_128_PCIX |
15013 DMA_RWCTRL_WRITE_BNDRY_128_PCIX);
15014 } else {
15015 val |= (DMA_RWCTRL_READ_BNDRY_384_PCIX |
15016 DMA_RWCTRL_WRITE_BNDRY_384_PCIX);
15017 }
15018 break;
15019
15020 case 256:
15021 val |= (DMA_RWCTRL_READ_BNDRY_256_PCIX |
15022 DMA_RWCTRL_WRITE_BNDRY_256_PCIX);
15023 break;
15024
15025 default:
15026 val |= (DMA_RWCTRL_READ_BNDRY_384_PCIX |
15027 DMA_RWCTRL_WRITE_BNDRY_384_PCIX);
15028 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070015029 }
Joe Perches63c3a662011-04-26 08:12:10 +000015030 } else if (tg3_flag(tp, PCI_EXPRESS)) {
David S. Miller59e6b432005-05-18 22:50:10 -070015031 switch (cacheline_size) {
15032 case 16:
15033 case 32:
15034 case 64:
15035 if (goal == BOUNDARY_SINGLE_CACHELINE) {
15036 val &= ~DMA_RWCTRL_WRITE_BNDRY_DISAB_PCIE;
15037 val |= DMA_RWCTRL_WRITE_BNDRY_64_PCIE;
15038 break;
15039 }
15040 /* fallthrough */
15041 case 128:
15042 default:
15043 val &= ~DMA_RWCTRL_WRITE_BNDRY_DISAB_PCIE;
15044 val |= DMA_RWCTRL_WRITE_BNDRY_128_PCIE;
15045 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070015046 }
David S. Miller59e6b432005-05-18 22:50:10 -070015047 } else {
15048 switch (cacheline_size) {
15049 case 16:
15050 if (goal == BOUNDARY_SINGLE_CACHELINE) {
15051 val |= (DMA_RWCTRL_READ_BNDRY_16 |
15052 DMA_RWCTRL_WRITE_BNDRY_16);
15053 break;
15054 }
15055 /* fallthrough */
15056 case 32:
15057 if (goal == BOUNDARY_SINGLE_CACHELINE) {
15058 val |= (DMA_RWCTRL_READ_BNDRY_32 |
15059 DMA_RWCTRL_WRITE_BNDRY_32);
15060 break;
15061 }
15062 /* fallthrough */
15063 case 64:
15064 if (goal == BOUNDARY_SINGLE_CACHELINE) {
15065 val |= (DMA_RWCTRL_READ_BNDRY_64 |
15066 DMA_RWCTRL_WRITE_BNDRY_64);
15067 break;
15068 }
15069 /* fallthrough */
15070 case 128:
15071 if (goal == BOUNDARY_SINGLE_CACHELINE) {
15072 val |= (DMA_RWCTRL_READ_BNDRY_128 |
15073 DMA_RWCTRL_WRITE_BNDRY_128);
15074 break;
15075 }
15076 /* fallthrough */
15077 case 256:
15078 val |= (DMA_RWCTRL_READ_BNDRY_256 |
15079 DMA_RWCTRL_WRITE_BNDRY_256);
15080 break;
15081 case 512:
15082 val |= (DMA_RWCTRL_READ_BNDRY_512 |
15083 DMA_RWCTRL_WRITE_BNDRY_512);
15084 break;
15085 case 1024:
15086 default:
15087 val |= (DMA_RWCTRL_READ_BNDRY_1024 |
15088 DMA_RWCTRL_WRITE_BNDRY_1024);
15089 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070015090 }
David S. Miller59e6b432005-05-18 22:50:10 -070015091 }
15092
15093out:
15094 return val;
15095}
15096
Linus Torvalds1da177e2005-04-16 15:20:36 -070015097static int __devinit tg3_do_test_dma(struct tg3 *tp, u32 *buf, dma_addr_t buf_dma, int size, int to_device)
15098{
15099 struct tg3_internal_buffer_desc test_desc;
15100 u32 sram_dma_descs;
15101 int i, ret;
15102
15103 sram_dma_descs = NIC_SRAM_DMA_DESC_POOL_BASE;
15104
15105 tw32(FTQ_RCVBD_COMP_FIFO_ENQDEQ, 0);
15106 tw32(FTQ_RCVDATA_COMP_FIFO_ENQDEQ, 0);
15107 tw32(RDMAC_STATUS, 0);
15108 tw32(WDMAC_STATUS, 0);
15109
15110 tw32(BUFMGR_MODE, 0);
15111 tw32(FTQ_RESET, 0);
15112
15113 test_desc.addr_hi = ((u64) buf_dma) >> 32;
15114 test_desc.addr_lo = buf_dma & 0xffffffff;
15115 test_desc.nic_mbuf = 0x00002100;
15116 test_desc.len = size;
15117
15118 /*
15119 * HP ZX1 was seeing test failures for 5701 cards running at 33Mhz
15120 * the *second* time the tg3 driver was getting loaded after an
15121 * initial scan.
15122 *
15123 * Broadcom tells me:
15124 * ...the DMA engine is connected to the GRC block and a DMA
15125 * reset may affect the GRC block in some unpredictable way...
15126 * The behavior of resets to individual blocks has not been tested.
15127 *
15128 * Broadcom noted the GRC reset will also reset all sub-components.
15129 */
15130 if (to_device) {
15131 test_desc.cqid_sqid = (13 << 8) | 2;
15132
15133 tw32_f(RDMAC_MODE, RDMAC_MODE_ENABLE);
15134 udelay(40);
15135 } else {
15136 test_desc.cqid_sqid = (16 << 8) | 7;
15137
15138 tw32_f(WDMAC_MODE, WDMAC_MODE_ENABLE);
15139 udelay(40);
15140 }
15141 test_desc.flags = 0x00000005;
15142
15143 for (i = 0; i < (sizeof(test_desc) / sizeof(u32)); i++) {
15144 u32 val;
15145
15146 val = *(((u32 *)&test_desc) + i);
15147 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR,
15148 sram_dma_descs + (i * sizeof(u32)));
15149 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_DATA, val);
15150 }
15151 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, 0);
15152
Matt Carlson859a588792010-04-05 10:19:28 +000015153 if (to_device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015154 tw32(FTQ_DMA_HIGH_READ_FIFO_ENQDEQ, sram_dma_descs);
Matt Carlson859a588792010-04-05 10:19:28 +000015155 else
Linus Torvalds1da177e2005-04-16 15:20:36 -070015156 tw32(FTQ_DMA_HIGH_WRITE_FIFO_ENQDEQ, sram_dma_descs);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015157
15158 ret = -ENODEV;
15159 for (i = 0; i < 40; i++) {
15160 u32 val;
15161
15162 if (to_device)
15163 val = tr32(FTQ_RCVBD_COMP_FIFO_ENQDEQ);
15164 else
15165 val = tr32(FTQ_RCVDATA_COMP_FIFO_ENQDEQ);
15166 if ((val & 0xffff) == sram_dma_descs) {
15167 ret = 0;
15168 break;
15169 }
15170
15171 udelay(100);
15172 }
15173
15174 return ret;
15175}
15176
David S. Millerded73402005-05-23 13:59:47 -070015177#define TEST_BUFFER_SIZE 0x2000
Linus Torvalds1da177e2005-04-16 15:20:36 -070015178
Matt Carlson41434702011-03-09 16:58:22 +000015179static DEFINE_PCI_DEVICE_TABLE(tg3_dma_wait_state_chipsets) = {
Joe Perches895950c2010-12-21 02:16:08 -080015180 { PCI_DEVICE(PCI_VENDOR_ID_APPLE, PCI_DEVICE_ID_APPLE_UNI_N_PCI15) },
15181 { },
15182};
15183
Linus Torvalds1da177e2005-04-16 15:20:36 -070015184static int __devinit tg3_test_dma(struct tg3 *tp)
15185{
15186 dma_addr_t buf_dma;
David S. Miller59e6b432005-05-18 22:50:10 -070015187 u32 *buf, saved_dma_rwctrl;
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000015188 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015189
Matt Carlson4bae65c2010-11-24 08:31:52 +000015190 buf = dma_alloc_coherent(&tp->pdev->dev, TEST_BUFFER_SIZE,
15191 &buf_dma, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015192 if (!buf) {
15193 ret = -ENOMEM;
15194 goto out_nofree;
15195 }
15196
15197 tp->dma_rwctrl = ((0x7 << DMA_RWCTRL_PCI_WRITE_CMD_SHIFT) |
15198 (0x6 << DMA_RWCTRL_PCI_READ_CMD_SHIFT));
15199
David S. Miller59e6b432005-05-18 22:50:10 -070015200 tp->dma_rwctrl = tg3_calc_dma_bndry(tp, tp->dma_rwctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015201
Joe Perches63c3a662011-04-26 08:12:10 +000015202 if (tg3_flag(tp, 57765_PLUS))
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000015203 goto out;
15204
Joe Perches63c3a662011-04-26 08:12:10 +000015205 if (tg3_flag(tp, PCI_EXPRESS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070015206 /* DMA read watermark not used on PCIE */
15207 tp->dma_rwctrl |= 0x00180000;
Joe Perches63c3a662011-04-26 08:12:10 +000015208 } else if (!tg3_flag(tp, PCIX_MODE)) {
Michael Chan85e94ce2005-04-21 17:05:28 -070015209 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 ||
15210 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015211 tp->dma_rwctrl |= 0x003f0000;
15212 else
15213 tp->dma_rwctrl |= 0x003f000f;
15214 } else {
15215 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
15216 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
15217 u32 ccval = (tr32(TG3PCI_CLOCK_CTRL) & 0x1f);
Michael Chan49afdeb2007-02-13 12:17:03 -080015218 u32 read_water = 0x7;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015219
Michael Chan4a29cc22006-03-19 13:21:12 -080015220 /* If the 5704 is behind the EPB bridge, we can
15221 * do the less restrictive ONE_DMA workaround for
15222 * better performance.
15223 */
Joe Perches63c3a662011-04-26 08:12:10 +000015224 if (tg3_flag(tp, 40BIT_DMA_BUG) &&
Michael Chan4a29cc22006-03-19 13:21:12 -080015225 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704)
15226 tp->dma_rwctrl |= 0x8000;
15227 else if (ccval == 0x6 || ccval == 0x7)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015228 tp->dma_rwctrl |= DMA_RWCTRL_ONE_DMA;
15229
Michael Chan49afdeb2007-02-13 12:17:03 -080015230 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703)
15231 read_water = 4;
David S. Miller59e6b432005-05-18 22:50:10 -070015232 /* Set bit 23 to enable PCIX hw bug fix */
Michael Chan49afdeb2007-02-13 12:17:03 -080015233 tp->dma_rwctrl |=
15234 (read_water << DMA_RWCTRL_READ_WATER_SHIFT) |
15235 (0x3 << DMA_RWCTRL_WRITE_WATER_SHIFT) |
15236 (1 << 23);
Michael Chan4cf78e42005-07-25 12:29:19 -070015237 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780) {
15238 /* 5780 always in PCIX mode */
15239 tp->dma_rwctrl |= 0x00144000;
Michael Chana4e2b342005-10-26 15:46:52 -070015240 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
15241 /* 5714 always in PCIX mode */
15242 tp->dma_rwctrl |= 0x00148000;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015243 } else {
15244 tp->dma_rwctrl |= 0x001b000f;
15245 }
15246 }
15247
15248 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
15249 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704)
15250 tp->dma_rwctrl &= 0xfffffff0;
15251
15252 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
15253 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
15254 /* Remove this if it causes problems for some boards. */
15255 tp->dma_rwctrl |= DMA_RWCTRL_USE_MEM_READ_MULT;
15256
15257 /* On 5700/5701 chips, we need to set this bit.
15258 * Otherwise the chip will issue cacheline transactions
15259 * to streamable DMA memory with not all the byte
15260 * enables turned on. This is an error on several
15261 * RISC PCI controllers, in particular sparc64.
15262 *
15263 * On 5703/5704 chips, this bit has been reassigned
15264 * a different meaning. In particular, it is used
15265 * on those chips to enable a PCI-X workaround.
15266 */
15267 tp->dma_rwctrl |= DMA_RWCTRL_ASSERT_ALL_BE;
15268 }
15269
15270 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
15271
15272#if 0
15273 /* Unneeded, already done by tg3_get_invariants. */
15274 tg3_switch_clocks(tp);
15275#endif
15276
Linus Torvalds1da177e2005-04-16 15:20:36 -070015277 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
15278 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701)
15279 goto out;
15280
David S. Miller59e6b432005-05-18 22:50:10 -070015281 /* It is best to perform DMA test with maximum write burst size
15282 * to expose the 5700/5701 write DMA bug.
15283 */
15284 saved_dma_rwctrl = tp->dma_rwctrl;
15285 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
15286 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
15287
Linus Torvalds1da177e2005-04-16 15:20:36 -070015288 while (1) {
15289 u32 *p = buf, i;
15290
15291 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++)
15292 p[i] = i;
15293
15294 /* Send the buffer to the chip. */
15295 ret = tg3_do_test_dma(tp, buf, buf_dma, TEST_BUFFER_SIZE, 1);
15296 if (ret) {
Matt Carlson2445e462010-04-05 10:19:21 +000015297 dev_err(&tp->pdev->dev,
15298 "%s: Buffer write failed. err = %d\n",
15299 __func__, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015300 break;
15301 }
15302
15303#if 0
15304 /* validate data reached card RAM correctly. */
15305 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++) {
15306 u32 val;
15307 tg3_read_mem(tp, 0x2100 + (i*4), &val);
15308 if (le32_to_cpu(val) != p[i]) {
Matt Carlson2445e462010-04-05 10:19:21 +000015309 dev_err(&tp->pdev->dev,
15310 "%s: Buffer corrupted on device! "
15311 "(%d != %d)\n", __func__, val, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015312 /* ret = -ENODEV here? */
15313 }
15314 p[i] = 0;
15315 }
15316#endif
15317 /* Now read it back. */
15318 ret = tg3_do_test_dma(tp, buf, buf_dma, TEST_BUFFER_SIZE, 0);
15319 if (ret) {
Matt Carlson5129c3a2010-04-05 10:19:23 +000015320 dev_err(&tp->pdev->dev, "%s: Buffer read failed. "
15321 "err = %d\n", __func__, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015322 break;
15323 }
15324
15325 /* Verify it. */
15326 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++) {
15327 if (p[i] == i)
15328 continue;
15329
David S. Miller59e6b432005-05-18 22:50:10 -070015330 if ((tp->dma_rwctrl & DMA_RWCTRL_WRITE_BNDRY_MASK) !=
15331 DMA_RWCTRL_WRITE_BNDRY_16) {
15332 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015333 tp->dma_rwctrl |= DMA_RWCTRL_WRITE_BNDRY_16;
15334 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
15335 break;
15336 } else {
Matt Carlson2445e462010-04-05 10:19:21 +000015337 dev_err(&tp->pdev->dev,
15338 "%s: Buffer corrupted on read back! "
15339 "(%d != %d)\n", __func__, p[i], i);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015340 ret = -ENODEV;
15341 goto out;
15342 }
15343 }
15344
15345 if (i == (TEST_BUFFER_SIZE / sizeof(u32))) {
15346 /* Success. */
15347 ret = 0;
15348 break;
15349 }
15350 }
David S. Miller59e6b432005-05-18 22:50:10 -070015351 if ((tp->dma_rwctrl & DMA_RWCTRL_WRITE_BNDRY_MASK) !=
15352 DMA_RWCTRL_WRITE_BNDRY_16) {
15353 /* DMA test passed without adjusting DMA boundary,
Michael Chan6d1cfba2005-06-08 14:13:14 -070015354 * now look for chipsets that are known to expose the
15355 * DMA bug without failing the test.
David S. Miller59e6b432005-05-18 22:50:10 -070015356 */
Matt Carlson41434702011-03-09 16:58:22 +000015357 if (pci_dev_present(tg3_dma_wait_state_chipsets)) {
Michael Chan6d1cfba2005-06-08 14:13:14 -070015358 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
15359 tp->dma_rwctrl |= DMA_RWCTRL_WRITE_BNDRY_16;
Matt Carlson859a588792010-04-05 10:19:28 +000015360 } else {
Michael Chan6d1cfba2005-06-08 14:13:14 -070015361 /* Safe to use the calculated DMA boundary. */
15362 tp->dma_rwctrl = saved_dma_rwctrl;
Matt Carlson859a588792010-04-05 10:19:28 +000015363 }
Michael Chan6d1cfba2005-06-08 14:13:14 -070015364
David S. Miller59e6b432005-05-18 22:50:10 -070015365 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
15366 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015367
15368out:
Matt Carlson4bae65c2010-11-24 08:31:52 +000015369 dma_free_coherent(&tp->pdev->dev, TEST_BUFFER_SIZE, buf, buf_dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015370out_nofree:
15371 return ret;
15372}
15373
Linus Torvalds1da177e2005-04-16 15:20:36 -070015374static void __devinit tg3_init_bufmgr_config(struct tg3 *tp)
15375{
Joe Perches63c3a662011-04-26 08:12:10 +000015376 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlson666bc832010-01-20 16:58:03 +000015377 tp->bufmgr_config.mbuf_read_dma_low_water =
15378 DEFAULT_MB_RDMA_LOW_WATER_5705;
15379 tp->bufmgr_config.mbuf_mac_rx_low_water =
15380 DEFAULT_MB_MACRX_LOW_WATER_57765;
15381 tp->bufmgr_config.mbuf_high_water =
15382 DEFAULT_MB_HIGH_WATER_57765;
15383
15384 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
15385 DEFAULT_MB_RDMA_LOW_WATER_5705;
15386 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
15387 DEFAULT_MB_MACRX_LOW_WATER_JUMBO_57765;
15388 tp->bufmgr_config.mbuf_high_water_jumbo =
15389 DEFAULT_MB_HIGH_WATER_JUMBO_57765;
Joe Perches63c3a662011-04-26 08:12:10 +000015390 } else if (tg3_flag(tp, 5705_PLUS)) {
Michael Chanfdfec1722005-07-25 12:31:48 -070015391 tp->bufmgr_config.mbuf_read_dma_low_water =
15392 DEFAULT_MB_RDMA_LOW_WATER_5705;
15393 tp->bufmgr_config.mbuf_mac_rx_low_water =
15394 DEFAULT_MB_MACRX_LOW_WATER_5705;
15395 tp->bufmgr_config.mbuf_high_water =
15396 DEFAULT_MB_HIGH_WATER_5705;
Michael Chanb5d37722006-09-27 16:06:21 -070015397 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
15398 tp->bufmgr_config.mbuf_mac_rx_low_water =
15399 DEFAULT_MB_MACRX_LOW_WATER_5906;
15400 tp->bufmgr_config.mbuf_high_water =
15401 DEFAULT_MB_HIGH_WATER_5906;
15402 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015403
Michael Chanfdfec1722005-07-25 12:31:48 -070015404 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
15405 DEFAULT_MB_RDMA_LOW_WATER_JUMBO_5780;
15406 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
15407 DEFAULT_MB_MACRX_LOW_WATER_JUMBO_5780;
15408 tp->bufmgr_config.mbuf_high_water_jumbo =
15409 DEFAULT_MB_HIGH_WATER_JUMBO_5780;
15410 } else {
15411 tp->bufmgr_config.mbuf_read_dma_low_water =
15412 DEFAULT_MB_RDMA_LOW_WATER;
15413 tp->bufmgr_config.mbuf_mac_rx_low_water =
15414 DEFAULT_MB_MACRX_LOW_WATER;
15415 tp->bufmgr_config.mbuf_high_water =
15416 DEFAULT_MB_HIGH_WATER;
15417
15418 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
15419 DEFAULT_MB_RDMA_LOW_WATER_JUMBO;
15420 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
15421 DEFAULT_MB_MACRX_LOW_WATER_JUMBO;
15422 tp->bufmgr_config.mbuf_high_water_jumbo =
15423 DEFAULT_MB_HIGH_WATER_JUMBO;
15424 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015425
15426 tp->bufmgr_config.dma_low_water = DEFAULT_DMA_LOW_WATER;
15427 tp->bufmgr_config.dma_high_water = DEFAULT_DMA_HIGH_WATER;
15428}
15429
15430static char * __devinit tg3_phy_string(struct tg3 *tp)
15431{
Matt Carlson79eb6902010-02-17 15:17:03 +000015432 switch (tp->phy_id & TG3_PHY_ID_MASK) {
15433 case TG3_PHY_ID_BCM5400: return "5400";
15434 case TG3_PHY_ID_BCM5401: return "5401";
15435 case TG3_PHY_ID_BCM5411: return "5411";
15436 case TG3_PHY_ID_BCM5701: return "5701";
15437 case TG3_PHY_ID_BCM5703: return "5703";
15438 case TG3_PHY_ID_BCM5704: return "5704";
15439 case TG3_PHY_ID_BCM5705: return "5705";
15440 case TG3_PHY_ID_BCM5750: return "5750";
15441 case TG3_PHY_ID_BCM5752: return "5752";
15442 case TG3_PHY_ID_BCM5714: return "5714";
15443 case TG3_PHY_ID_BCM5780: return "5780";
15444 case TG3_PHY_ID_BCM5755: return "5755";
15445 case TG3_PHY_ID_BCM5787: return "5787";
15446 case TG3_PHY_ID_BCM5784: return "5784";
15447 case TG3_PHY_ID_BCM5756: return "5722/5756";
15448 case TG3_PHY_ID_BCM5906: return "5906";
15449 case TG3_PHY_ID_BCM5761: return "5761";
15450 case TG3_PHY_ID_BCM5718C: return "5718C";
15451 case TG3_PHY_ID_BCM5718S: return "5718S";
15452 case TG3_PHY_ID_BCM57765: return "57765";
Matt Carlson302b5002010-06-05 17:24:38 +000015453 case TG3_PHY_ID_BCM5719C: return "5719C";
Matt Carlson6418f2c2011-04-05 14:22:49 +000015454 case TG3_PHY_ID_BCM5720C: return "5720C";
Matt Carlson79eb6902010-02-17 15:17:03 +000015455 case TG3_PHY_ID_BCM8002: return "8002/serdes";
Linus Torvalds1da177e2005-04-16 15:20:36 -070015456 case 0: return "serdes";
15457 default: return "unknown";
Stephen Hemminger855e1112008-04-16 16:37:28 -070015458 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015459}
15460
Michael Chanf9804dd2005-09-27 12:13:10 -070015461static char * __devinit tg3_bus_string(struct tg3 *tp, char *str)
15462{
Joe Perches63c3a662011-04-26 08:12:10 +000015463 if (tg3_flag(tp, PCI_EXPRESS)) {
Michael Chanf9804dd2005-09-27 12:13:10 -070015464 strcpy(str, "PCI Express");
15465 return str;
Joe Perches63c3a662011-04-26 08:12:10 +000015466 } else if (tg3_flag(tp, PCIX_MODE)) {
Michael Chanf9804dd2005-09-27 12:13:10 -070015467 u32 clock_ctrl = tr32(TG3PCI_CLOCK_CTRL) & 0x1f;
15468
15469 strcpy(str, "PCIX:");
15470
15471 if ((clock_ctrl == 7) ||
15472 ((tr32(GRC_MISC_CFG) & GRC_MISC_CFG_BOARD_ID_MASK) ==
15473 GRC_MISC_CFG_BOARD_ID_5704CIOBE))
15474 strcat(str, "133MHz");
15475 else if (clock_ctrl == 0)
15476 strcat(str, "33MHz");
15477 else if (clock_ctrl == 2)
15478 strcat(str, "50MHz");
15479 else if (clock_ctrl == 4)
15480 strcat(str, "66MHz");
15481 else if (clock_ctrl == 6)
15482 strcat(str, "100MHz");
Michael Chanf9804dd2005-09-27 12:13:10 -070015483 } else {
15484 strcpy(str, "PCI:");
Joe Perches63c3a662011-04-26 08:12:10 +000015485 if (tg3_flag(tp, PCI_HIGH_SPEED))
Michael Chanf9804dd2005-09-27 12:13:10 -070015486 strcat(str, "66MHz");
15487 else
15488 strcat(str, "33MHz");
15489 }
Joe Perches63c3a662011-04-26 08:12:10 +000015490 if (tg3_flag(tp, PCI_32BIT))
Michael Chanf9804dd2005-09-27 12:13:10 -070015491 strcat(str, ":32-bit");
15492 else
15493 strcat(str, ":64-bit");
15494 return str;
15495}
15496
David S. Miller15f98502005-05-18 22:49:26 -070015497static void __devinit tg3_init_coal(struct tg3 *tp)
15498{
15499 struct ethtool_coalesce *ec = &tp->coal;
15500
15501 memset(ec, 0, sizeof(*ec));
15502 ec->cmd = ETHTOOL_GCOALESCE;
15503 ec->rx_coalesce_usecs = LOW_RXCOL_TICKS;
15504 ec->tx_coalesce_usecs = LOW_TXCOL_TICKS;
15505 ec->rx_max_coalesced_frames = LOW_RXMAX_FRAMES;
15506 ec->tx_max_coalesced_frames = LOW_TXMAX_FRAMES;
15507 ec->rx_coalesce_usecs_irq = DEFAULT_RXCOAL_TICK_INT;
15508 ec->tx_coalesce_usecs_irq = DEFAULT_TXCOAL_TICK_INT;
15509 ec->rx_max_coalesced_frames_irq = DEFAULT_RXCOAL_MAXF_INT;
15510 ec->tx_max_coalesced_frames_irq = DEFAULT_TXCOAL_MAXF_INT;
15511 ec->stats_block_coalesce_usecs = DEFAULT_STAT_COAL_TICKS;
15512
15513 if (tp->coalesce_mode & (HOSTCC_MODE_CLRTICK_RXBD |
15514 HOSTCC_MODE_CLRTICK_TXBD)) {
15515 ec->rx_coalesce_usecs = LOW_RXCOL_TICKS_CLRTCKS;
15516 ec->rx_coalesce_usecs_irq = DEFAULT_RXCOAL_TICK_INT_CLRTCKS;
15517 ec->tx_coalesce_usecs = LOW_TXCOL_TICKS_CLRTCKS;
15518 ec->tx_coalesce_usecs_irq = DEFAULT_TXCOAL_TICK_INT_CLRTCKS;
15519 }
Michael Chand244c892005-07-05 14:42:33 -070015520
Joe Perches63c3a662011-04-26 08:12:10 +000015521 if (tg3_flag(tp, 5705_PLUS)) {
Michael Chand244c892005-07-05 14:42:33 -070015522 ec->rx_coalesce_usecs_irq = 0;
15523 ec->tx_coalesce_usecs_irq = 0;
15524 ec->stats_block_coalesce_usecs = 0;
15525 }
David S. Miller15f98502005-05-18 22:49:26 -070015526}
15527
Linus Torvalds1da177e2005-04-16 15:20:36 -070015528static int __devinit tg3_init_one(struct pci_dev *pdev,
15529 const struct pci_device_id *ent)
15530{
Linus Torvalds1da177e2005-04-16 15:20:36 -070015531 struct net_device *dev;
15532 struct tg3 *tp;
Matt Carlson646c9ed2009-09-01 12:58:41 +000015533 int i, err, pm_cap;
15534 u32 sndmbx, rcvmbx, intmbx;
Michael Chanf9804dd2005-09-27 12:13:10 -070015535 char str[40];
Michael Chan72f2afb2006-03-06 19:28:35 -080015536 u64 dma_mask, persist_dma_mask;
Michał Mirosławc8f44af2011-11-15 15:29:55 +000015537 netdev_features_t features = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015538
Joe Perches05dbe002010-02-17 19:44:19 +000015539 printk_once(KERN_INFO "%s\n", version);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015540
15541 err = pci_enable_device(pdev);
15542 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000015543 dev_err(&pdev->dev, "Cannot enable PCI device, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070015544 return err;
15545 }
15546
Linus Torvalds1da177e2005-04-16 15:20:36 -070015547 err = pci_request_regions(pdev, DRV_MODULE_NAME);
15548 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000015549 dev_err(&pdev->dev, "Cannot obtain PCI resources, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070015550 goto err_out_disable_pdev;
15551 }
15552
15553 pci_set_master(pdev);
15554
15555 /* Find power-management capability. */
15556 pm_cap = pci_find_capability(pdev, PCI_CAP_ID_PM);
15557 if (pm_cap == 0) {
Matt Carlson2445e462010-04-05 10:19:21 +000015558 dev_err(&pdev->dev,
15559 "Cannot find Power Management capability, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070015560 err = -EIO;
15561 goto err_out_free_res;
15562 }
15563
Matt Carlson16821282011-07-13 09:27:28 +000015564 err = pci_set_power_state(pdev, PCI_D0);
15565 if (err) {
15566 dev_err(&pdev->dev, "Transition to D0 failed, aborting\n");
15567 goto err_out_free_res;
15568 }
15569
Matt Carlsonfe5f5782009-09-01 13:09:39 +000015570 dev = alloc_etherdev_mq(sizeof(*tp), TG3_IRQ_MAX_VECS);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015571 if (!dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070015572 err = -ENOMEM;
Matt Carlson16821282011-07-13 09:27:28 +000015573 goto err_out_power_down;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015574 }
15575
Linus Torvalds1da177e2005-04-16 15:20:36 -070015576 SET_NETDEV_DEV(dev, &pdev->dev);
15577
Linus Torvalds1da177e2005-04-16 15:20:36 -070015578 tp = netdev_priv(dev);
15579 tp->pdev = pdev;
15580 tp->dev = dev;
15581 tp->pm_cap = pm_cap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015582 tp->rx_mode = TG3_DEF_RX_MODE;
15583 tp->tx_mode = TG3_DEF_TX_MODE;
Matt Carlson8ef21422008-05-02 16:47:53 -070015584
Linus Torvalds1da177e2005-04-16 15:20:36 -070015585 if (tg3_debug > 0)
15586 tp->msg_enable = tg3_debug;
15587 else
15588 tp->msg_enable = TG3_DEF_MSG_ENABLE;
15589
15590 /* The word/byte swap controls here control register access byte
15591 * swapping. DMA data byte swapping is controlled in the GRC_MODE
15592 * setting below.
15593 */
15594 tp->misc_host_ctrl =
15595 MISC_HOST_CTRL_MASK_PCI_INT |
15596 MISC_HOST_CTRL_WORD_SWAP |
15597 MISC_HOST_CTRL_INDIR_ACCESS |
15598 MISC_HOST_CTRL_PCISTATE_RW;
15599
15600 /* The NONFRM (non-frame) byte/word swap controls take effect
15601 * on descriptor entries, anything which isn't packet data.
15602 *
15603 * The StrongARM chips on the board (one for tx, one for rx)
15604 * are running in big-endian mode.
15605 */
15606 tp->grc_mode = (GRC_MODE_WSWAP_DATA | GRC_MODE_BSWAP_DATA |
15607 GRC_MODE_WSWAP_NONFRM_DATA);
15608#ifdef __BIG_ENDIAN
15609 tp->grc_mode |= GRC_MODE_BSWAP_NONFRM_DATA;
15610#endif
15611 spin_lock_init(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015612 spin_lock_init(&tp->indirect_lock);
David Howellsc4028952006-11-22 14:57:56 +000015613 INIT_WORK(&tp->reset_task, tg3_reset_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015614
Matt Carlsond5fe4882008-11-21 17:20:32 -080015615 tp->regs = pci_ioremap_bar(pdev, BAR_0);
Andy Gospodarekab0049b2007-09-06 20:42:14 +010015616 if (!tp->regs) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015617 dev_err(&pdev->dev, "Cannot map device registers, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070015618 err = -ENOMEM;
15619 goto err_out_free_dev;
15620 }
15621
Matt Carlsonc9cab242011-07-13 09:27:27 +000015622 if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761 ||
15623 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761E ||
15624 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761S ||
15625 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761SE ||
15626 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717 ||
15627 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718 ||
15628 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5719 ||
15629 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5720) {
15630 tg3_flag_set(tp, ENABLE_APE);
15631 tp->aperegs = pci_ioremap_bar(pdev, BAR_2);
15632 if (!tp->aperegs) {
15633 dev_err(&pdev->dev,
15634 "Cannot map APE registers, aborting\n");
15635 err = -ENOMEM;
15636 goto err_out_iounmap;
15637 }
15638 }
15639
Linus Torvalds1da177e2005-04-16 15:20:36 -070015640 tp->rx_pending = TG3_DEF_RX_RING_PENDING;
15641 tp->rx_jumbo_pending = TG3_DEF_RX_JUMBO_RING_PENDING;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015642
Linus Torvalds1da177e2005-04-16 15:20:36 -070015643 dev->ethtool_ops = &tg3_ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015644 dev->watchdog_timeo = TG3_TX_TIMEOUT;
Matt Carlson2ffcc982011-05-19 12:12:44 +000015645 dev->netdev_ops = &tg3_netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015646 dev->irq = pdev->irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015647
15648 err = tg3_get_invariants(tp);
15649 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015650 dev_err(&pdev->dev,
15651 "Problem fetching invariants of chip, aborting\n");
Matt Carlsonc9cab242011-07-13 09:27:27 +000015652 goto err_out_apeunmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015653 }
15654
Michael Chan4a29cc22006-03-19 13:21:12 -080015655 /* The EPB bridge inside 5714, 5715, and 5780 and any
15656 * device behind the EPB cannot support DMA addresses > 40-bit.
Michael Chan72f2afb2006-03-06 19:28:35 -080015657 * On 64-bit systems with IOMMU, use 40-bit dma_mask.
15658 * On 64-bit systems without IOMMU, use 64-bit dma_mask and
15659 * do DMA address check in tg3_start_xmit().
15660 */
Joe Perches63c3a662011-04-26 08:12:10 +000015661 if (tg3_flag(tp, IS_5788))
Yang Hongyang284901a2009-04-06 19:01:15 -070015662 persist_dma_mask = dma_mask = DMA_BIT_MASK(32);
Joe Perches63c3a662011-04-26 08:12:10 +000015663 else if (tg3_flag(tp, 40BIT_DMA_BUG)) {
Yang Hongyang50cf1562009-04-06 19:01:14 -070015664 persist_dma_mask = dma_mask = DMA_BIT_MASK(40);
Michael Chan72f2afb2006-03-06 19:28:35 -080015665#ifdef CONFIG_HIGHMEM
Yang Hongyang6a355282009-04-06 19:01:13 -070015666 dma_mask = DMA_BIT_MASK(64);
Michael Chan72f2afb2006-03-06 19:28:35 -080015667#endif
Michael Chan4a29cc22006-03-19 13:21:12 -080015668 } else
Yang Hongyang6a355282009-04-06 19:01:13 -070015669 persist_dma_mask = dma_mask = DMA_BIT_MASK(64);
Michael Chan72f2afb2006-03-06 19:28:35 -080015670
15671 /* Configure DMA attributes. */
Yang Hongyang284901a2009-04-06 19:01:15 -070015672 if (dma_mask > DMA_BIT_MASK(32)) {
Michael Chan72f2afb2006-03-06 19:28:35 -080015673 err = pci_set_dma_mask(pdev, dma_mask);
15674 if (!err) {
Matt Carlson0da06062011-05-19 12:12:53 +000015675 features |= NETIF_F_HIGHDMA;
Michael Chan72f2afb2006-03-06 19:28:35 -080015676 err = pci_set_consistent_dma_mask(pdev,
15677 persist_dma_mask);
15678 if (err < 0) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015679 dev_err(&pdev->dev, "Unable to obtain 64 bit "
15680 "DMA for consistent allocations\n");
Matt Carlsonc9cab242011-07-13 09:27:27 +000015681 goto err_out_apeunmap;
Michael Chan72f2afb2006-03-06 19:28:35 -080015682 }
15683 }
15684 }
Yang Hongyang284901a2009-04-06 19:01:15 -070015685 if (err || dma_mask == DMA_BIT_MASK(32)) {
15686 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
Michael Chan72f2afb2006-03-06 19:28:35 -080015687 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015688 dev_err(&pdev->dev,
15689 "No usable DMA configuration, aborting\n");
Matt Carlsonc9cab242011-07-13 09:27:27 +000015690 goto err_out_apeunmap;
Michael Chan72f2afb2006-03-06 19:28:35 -080015691 }
15692 }
15693
Michael Chanfdfec1722005-07-25 12:31:48 -070015694 tg3_init_bufmgr_config(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015695
Matt Carlson0da06062011-05-19 12:12:53 +000015696 features |= NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
15697
15698 /* 5700 B0 chips do not support checksumming correctly due
15699 * to hardware bugs.
15700 */
15701 if (tp->pci_chip_rev_id != CHIPREV_ID_5700_B0) {
15702 features |= NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_RXCSUM;
15703
15704 if (tg3_flag(tp, 5755_PLUS))
15705 features |= NETIF_F_IPV6_CSUM;
15706 }
15707
Michael Chan4e3a7aa2006-03-20 17:47:44 -080015708 /* TSO is on by default on chips that support hardware TSO.
15709 * Firmware TSO on older chips gives lower performance, so it
15710 * is off by default, but can be enabled using ethtool.
15711 */
Joe Perches63c3a662011-04-26 08:12:10 +000015712 if ((tg3_flag(tp, HW_TSO_1) ||
15713 tg3_flag(tp, HW_TSO_2) ||
15714 tg3_flag(tp, HW_TSO_3)) &&
Matt Carlson0da06062011-05-19 12:12:53 +000015715 (features & NETIF_F_IP_CSUM))
15716 features |= NETIF_F_TSO;
Joe Perches63c3a662011-04-26 08:12:10 +000015717 if (tg3_flag(tp, HW_TSO_2) || tg3_flag(tp, HW_TSO_3)) {
Matt Carlson0da06062011-05-19 12:12:53 +000015718 if (features & NETIF_F_IPV6_CSUM)
15719 features |= NETIF_F_TSO6;
Joe Perches63c3a662011-04-26 08:12:10 +000015720 if (tg3_flag(tp, HW_TSO_3) ||
Matt Carlsone849cdc2009-11-13 13:03:38 +000015721 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson57e69832008-05-25 23:48:31 -070015722 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
15723 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX) ||
Joe Perches63c3a662011-04-26 08:12:10 +000015724 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Michał Mirosławdc668912011-04-07 03:35:07 +000015725 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
Matt Carlson0da06062011-05-19 12:12:53 +000015726 features |= NETIF_F_TSO_ECN;
Michael Chanb0026622006-07-03 19:42:14 -070015727 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015728
Matt Carlsond542fe22011-05-19 16:02:43 +000015729 dev->features |= features;
15730 dev->vlan_features |= features;
15731
Mahesh Bandewar06c03c02011-05-08 06:51:48 +000015732 /*
15733 * Add loopback capability only for a subset of devices that support
15734 * MAC-LOOPBACK. Eventually this need to be enhanced to allow INT-PHY
15735 * loopback for the remaining devices.
15736 */
15737 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5780 &&
15738 !tg3_flag(tp, CPMU_PRESENT))
15739 /* Add the loopback capability */
Matt Carlson0da06062011-05-19 12:12:53 +000015740 features |= NETIF_F_LOOPBACK;
15741
Matt Carlson0da06062011-05-19 12:12:53 +000015742 dev->hw_features |= features;
Mahesh Bandewar06c03c02011-05-08 06:51:48 +000015743
Linus Torvalds1da177e2005-04-16 15:20:36 -070015744 if (tp->pci_chip_rev_id == CHIPREV_ID_5705_A1 &&
Joe Perches63c3a662011-04-26 08:12:10 +000015745 !tg3_flag(tp, TSO_CAPABLE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070015746 !(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH)) {
Joe Perches63c3a662011-04-26 08:12:10 +000015747 tg3_flag_set(tp, MAX_RXPEND_64);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015748 tp->rx_pending = 63;
15749 }
15750
Linus Torvalds1da177e2005-04-16 15:20:36 -070015751 err = tg3_get_device_address(tp);
15752 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015753 dev_err(&pdev->dev,
15754 "Could not obtain valid ethernet address, aborting\n");
Matt Carlsonc9cab242011-07-13 09:27:27 +000015755 goto err_out_apeunmap;
Matt Carlson0d3031d2007-10-10 18:02:43 -070015756 }
15757
Matt Carlsonc88864d2007-11-12 21:07:01 -080015758 /*
15759 * Reset chip in case UNDI or EFI driver did not shutdown
15760 * DMA self test will enable WDMAC and we'll see (spurious)
15761 * pending DMA on the PCI bus at that point.
15762 */
15763 if ((tr32(HOSTCC_MODE) & HOSTCC_MODE_ENABLE) ||
15764 (tr32(WDMAC_MODE) & WDMAC_MODE_ENABLE)) {
15765 tw32(MEMARB_MODE, MEMARB_MODE_ENABLE);
15766 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
15767 }
15768
15769 err = tg3_test_dma(tp);
15770 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015771 dev_err(&pdev->dev, "DMA engine test failed, aborting\n");
Matt Carlsonc88864d2007-11-12 21:07:01 -080015772 goto err_out_apeunmap;
15773 }
15774
Matt Carlson78f90dc2009-11-13 13:03:42 +000015775 intmbx = MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW;
15776 rcvmbx = MAILBOX_RCVRET_CON_IDX_0 + TG3_64BIT_REG_LOW;
15777 sndmbx = MAILBOX_SNDHOST_PROD_IDX_0 + TG3_64BIT_REG_LOW;
Matt Carlson6fd45cb2010-09-15 08:59:57 +000015778 for (i = 0; i < tp->irq_max; i++) {
Matt Carlson78f90dc2009-11-13 13:03:42 +000015779 struct tg3_napi *tnapi = &tp->napi[i];
15780
15781 tnapi->tp = tp;
15782 tnapi->tx_pending = TG3_DEF_TX_RING_PENDING;
15783
15784 tnapi->int_mbox = intmbx;
Matt Carlson93a700a2011-08-31 11:44:54 +000015785 if (i <= 4)
Matt Carlson78f90dc2009-11-13 13:03:42 +000015786 intmbx += 0x8;
15787 else
15788 intmbx += 0x4;
15789
15790 tnapi->consmbox = rcvmbx;
15791 tnapi->prodmbox = sndmbx;
15792
Matt Carlson66cfd1b2010-09-30 10:34:30 +000015793 if (i)
Matt Carlson78f90dc2009-11-13 13:03:42 +000015794 tnapi->coal_now = HOSTCC_MODE_COAL_VEC1_NOW << (i - 1);
Matt Carlson66cfd1b2010-09-30 10:34:30 +000015795 else
Matt Carlson78f90dc2009-11-13 13:03:42 +000015796 tnapi->coal_now = HOSTCC_MODE_NOW;
Matt Carlson78f90dc2009-11-13 13:03:42 +000015797
Joe Perches63c3a662011-04-26 08:12:10 +000015798 if (!tg3_flag(tp, SUPPORT_MSIX))
Matt Carlson78f90dc2009-11-13 13:03:42 +000015799 break;
15800
15801 /*
15802 * If we support MSIX, we'll be using RSS. If we're using
15803 * RSS, the first vector only handles link interrupts and the
15804 * remaining vectors handle rx and tx interrupts. Reuse the
15805 * mailbox values for the next iteration. The values we setup
15806 * above are still useful for the single vectored mode.
15807 */
15808 if (!i)
15809 continue;
15810
15811 rcvmbx += 0x8;
15812
15813 if (sndmbx & 0x4)
15814 sndmbx -= 0x4;
15815 else
15816 sndmbx += 0xc;
15817 }
15818
Matt Carlsonc88864d2007-11-12 21:07:01 -080015819 tg3_init_coal(tp);
15820
Michael Chanc49a1562006-12-17 17:07:29 -080015821 pci_set_drvdata(pdev, dev);
15822
Matt Carlsoncd0d7222011-07-13 09:27:33 +000015823 if (tg3_flag(tp, 5717_PLUS)) {
15824 /* Resume a low-power mode */
15825 tg3_frob_aux_power(tp, false);
15826 }
15827
Matt Carlson21f76382012-02-22 12:35:21 +000015828 tg3_timer_init(tp);
15829
Linus Torvalds1da177e2005-04-16 15:20:36 -070015830 err = register_netdev(dev);
15831 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015832 dev_err(&pdev->dev, "Cannot register net device, aborting\n");
Matt Carlson0d3031d2007-10-10 18:02:43 -070015833 goto err_out_apeunmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015834 }
15835
Joe Perches05dbe002010-02-17 19:44:19 +000015836 netdev_info(dev, "Tigon3 [partno(%s) rev %04x] (%s) MAC address %pM\n",
15837 tp->board_part_number,
15838 tp->pci_chip_rev_id,
15839 tg3_bus_string(tp, str),
15840 dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015841
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015842 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000015843 struct phy_device *phydev;
15844 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlson5129c3a2010-04-05 10:19:23 +000015845 netdev_info(dev,
15846 "attached PHY driver [%s] (mii_bus:phy_addr=%s)\n",
Joe Perches05dbe002010-02-17 19:44:19 +000015847 phydev->drv->name, dev_name(&phydev->dev));
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015848 } else {
15849 char *ethtype;
15850
15851 if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
15852 ethtype = "10/100Base-TX";
15853 else if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
15854 ethtype = "1000Base-SX";
15855 else
15856 ethtype = "10/100/1000Base-T";
15857
Matt Carlson5129c3a2010-04-05 10:19:23 +000015858 netdev_info(dev, "attached PHY is %s (%s Ethernet) "
Matt Carlson47007832011-04-20 07:57:43 +000015859 "(WireSpeed[%d], EEE[%d])\n",
15860 tg3_phy_string(tp), ethtype,
15861 (tp->phy_flags & TG3_PHYFLG_NO_ETH_WIRE_SPEED) == 0,
15862 (tp->phy_flags & TG3_PHYFLG_EEE_CAP) != 0);
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015863 }
Matt Carlsondf59c942008-11-03 16:52:56 -080015864
Joe Perches05dbe002010-02-17 19:44:19 +000015865 netdev_info(dev, "RXcsums[%d] LinkChgREG[%d] MIirq[%d] ASF[%d] TSOcap[%d]\n",
Michał Mirosławdc668912011-04-07 03:35:07 +000015866 (dev->features & NETIF_F_RXCSUM) != 0,
Joe Perches63c3a662011-04-26 08:12:10 +000015867 tg3_flag(tp, USE_LINKCHG_REG) != 0,
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015868 (tp->phy_flags & TG3_PHYFLG_USE_MI_INTERRUPT) != 0,
Joe Perches63c3a662011-04-26 08:12:10 +000015869 tg3_flag(tp, ENABLE_ASF) != 0,
15870 tg3_flag(tp, TSO_CAPABLE) != 0);
Joe Perches05dbe002010-02-17 19:44:19 +000015871 netdev_info(dev, "dma_rwctrl[%08x] dma_mask[%d-bit]\n",
15872 tp->dma_rwctrl,
15873 pdev->dma_mask == DMA_BIT_MASK(32) ? 32 :
15874 ((u64)pdev->dma_mask) == DMA_BIT_MASK(40) ? 40 : 64);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015875
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000015876 pci_save_state(pdev);
15877
Linus Torvalds1da177e2005-04-16 15:20:36 -070015878 return 0;
15879
Matt Carlson0d3031d2007-10-10 18:02:43 -070015880err_out_apeunmap:
15881 if (tp->aperegs) {
15882 iounmap(tp->aperegs);
15883 tp->aperegs = NULL;
15884 }
15885
Linus Torvalds1da177e2005-04-16 15:20:36 -070015886err_out_iounmap:
Michael Chan68929142005-08-09 20:17:14 -070015887 if (tp->regs) {
15888 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070015889 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070015890 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015891
15892err_out_free_dev:
15893 free_netdev(dev);
15894
Matt Carlson16821282011-07-13 09:27:28 +000015895err_out_power_down:
15896 pci_set_power_state(pdev, PCI_D3hot);
15897
Linus Torvalds1da177e2005-04-16 15:20:36 -070015898err_out_free_res:
15899 pci_release_regions(pdev);
15900
15901err_out_disable_pdev:
15902 pci_disable_device(pdev);
15903 pci_set_drvdata(pdev, NULL);
15904 return err;
15905}
15906
15907static void __devexit tg3_remove_one(struct pci_dev *pdev)
15908{
15909 struct net_device *dev = pci_get_drvdata(pdev);
15910
15911 if (dev) {
15912 struct tg3 *tp = netdev_priv(dev);
15913
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -080015914 if (tp->fw)
15915 release_firmware(tp->fw);
15916
Matt Carlsondb219972011-11-04 09:15:03 +000015917 tg3_reset_task_cancel(tp);
Matt Carlson158d7ab2008-05-29 01:37:54 -070015918
David S. Miller1805b2f2011-10-24 18:18:09 -040015919 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015920 tg3_phy_fini(tp);
Matt Carlson158d7ab2008-05-29 01:37:54 -070015921 tg3_mdio_fini(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015922 }
Matt Carlson158d7ab2008-05-29 01:37:54 -070015923
Linus Torvalds1da177e2005-04-16 15:20:36 -070015924 unregister_netdev(dev);
Matt Carlson0d3031d2007-10-10 18:02:43 -070015925 if (tp->aperegs) {
15926 iounmap(tp->aperegs);
15927 tp->aperegs = NULL;
15928 }
Michael Chan68929142005-08-09 20:17:14 -070015929 if (tp->regs) {
15930 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070015931 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070015932 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015933 free_netdev(dev);
15934 pci_release_regions(pdev);
15935 pci_disable_device(pdev);
15936 pci_set_drvdata(pdev, NULL);
15937 }
15938}
15939
Eric Dumazetaa6027c2011-01-01 05:22:46 +000015940#ifdef CONFIG_PM_SLEEP
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015941static int tg3_suspend(struct device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015942{
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015943 struct pci_dev *pdev = to_pci_dev(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015944 struct net_device *dev = pci_get_drvdata(pdev);
15945 struct tg3 *tp = netdev_priv(dev);
15946 int err;
15947
15948 if (!netif_running(dev))
15949 return 0;
15950
Matt Carlsondb219972011-11-04 09:15:03 +000015951 tg3_reset_task_cancel(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015952 tg3_phy_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015953 tg3_netif_stop(tp);
15954
Matt Carlson21f76382012-02-22 12:35:21 +000015955 tg3_timer_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015956
David S. Millerf47c11e2005-06-24 20:18:35 -070015957 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015958 tg3_disable_ints(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -070015959 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015960
15961 netif_device_detach(dev);
15962
David S. Millerf47c11e2005-06-24 20:18:35 -070015963 tg3_full_lock(tp, 0);
Michael Chan944d9802005-05-29 14:57:48 -070015964 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Joe Perches63c3a662011-04-26 08:12:10 +000015965 tg3_flag_clear(tp, INIT_COMPLETE);
David S. Millerf47c11e2005-06-24 20:18:35 -070015966 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015967
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015968 err = tg3_power_down_prepare(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015969 if (err) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015970 int err2;
15971
David S. Millerf47c11e2005-06-24 20:18:35 -070015972 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015973
Joe Perches63c3a662011-04-26 08:12:10 +000015974 tg3_flag_set(tp, INIT_COMPLETE);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015975 err2 = tg3_restart_hw(tp, 1);
15976 if (err2)
Michael Chanb9ec6c12006-07-25 16:37:27 -070015977 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015978
Matt Carlson21f76382012-02-22 12:35:21 +000015979 tg3_timer_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015980
15981 netif_device_attach(dev);
15982 tg3_netif_start(tp);
15983
Michael Chanb9ec6c12006-07-25 16:37:27 -070015984out:
David S. Millerf47c11e2005-06-24 20:18:35 -070015985 tg3_full_unlock(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015986
15987 if (!err2)
15988 tg3_phy_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015989 }
15990
15991 return err;
15992}
15993
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015994static int tg3_resume(struct device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015995{
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015996 struct pci_dev *pdev = to_pci_dev(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015997 struct net_device *dev = pci_get_drvdata(pdev);
15998 struct tg3 *tp = netdev_priv(dev);
15999 int err;
16000
16001 if (!netif_running(dev))
16002 return 0;
16003
Linus Torvalds1da177e2005-04-16 15:20:36 -070016004 netif_device_attach(dev);
16005
David S. Millerf47c11e2005-06-24 20:18:35 -070016006 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016007
Joe Perches63c3a662011-04-26 08:12:10 +000016008 tg3_flag_set(tp, INIT_COMPLETE);
Michael Chanb9ec6c12006-07-25 16:37:27 -070016009 err = tg3_restart_hw(tp, 1);
16010 if (err)
16011 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016012
Matt Carlson21f76382012-02-22 12:35:21 +000016013 tg3_timer_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016014
Linus Torvalds1da177e2005-04-16 15:20:36 -070016015 tg3_netif_start(tp);
16016
Michael Chanb9ec6c12006-07-25 16:37:27 -070016017out:
David S. Millerf47c11e2005-06-24 20:18:35 -070016018 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016019
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070016020 if (!err)
16021 tg3_phy_start(tp);
16022
Michael Chanb9ec6c12006-07-25 16:37:27 -070016023 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016024}
16025
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000016026static SIMPLE_DEV_PM_OPS(tg3_pm_ops, tg3_suspend, tg3_resume);
Eric Dumazetaa6027c2011-01-01 05:22:46 +000016027#define TG3_PM_OPS (&tg3_pm_ops)
16028
16029#else
16030
16031#define TG3_PM_OPS NULL
16032
16033#endif /* CONFIG_PM_SLEEP */
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000016034
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000016035/**
16036 * tg3_io_error_detected - called when PCI error is detected
16037 * @pdev: Pointer to PCI device
16038 * @state: The current pci connection state
16039 *
16040 * This function is called after a PCI bus error affecting
16041 * this device has been detected.
16042 */
16043static pci_ers_result_t tg3_io_error_detected(struct pci_dev *pdev,
16044 pci_channel_state_t state)
16045{
16046 struct net_device *netdev = pci_get_drvdata(pdev);
16047 struct tg3 *tp = netdev_priv(netdev);
16048 pci_ers_result_t err = PCI_ERS_RESULT_NEED_RESET;
16049
16050 netdev_info(netdev, "PCI I/O error detected\n");
16051
16052 rtnl_lock();
16053
16054 if (!netif_running(netdev))
16055 goto done;
16056
16057 tg3_phy_stop(tp);
16058
16059 tg3_netif_stop(tp);
16060
Matt Carlson21f76382012-02-22 12:35:21 +000016061 tg3_timer_stop(tp);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000016062
16063 /* Want to make sure that the reset task doesn't run */
Matt Carlsondb219972011-11-04 09:15:03 +000016064 tg3_reset_task_cancel(tp);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000016065
16066 netif_device_detach(netdev);
16067
16068 /* Clean up software state, even if MMIO is blocked */
16069 tg3_full_lock(tp, 0);
16070 tg3_halt(tp, RESET_KIND_SHUTDOWN, 0);
16071 tg3_full_unlock(tp);
16072
16073done:
16074 if (state == pci_channel_io_perm_failure)
16075 err = PCI_ERS_RESULT_DISCONNECT;
16076 else
16077 pci_disable_device(pdev);
16078
16079 rtnl_unlock();
16080
16081 return err;
16082}
16083
16084/**
16085 * tg3_io_slot_reset - called after the pci bus has been reset.
16086 * @pdev: Pointer to PCI device
16087 *
16088 * Restart the card from scratch, as if from a cold-boot.
16089 * At this point, the card has exprienced a hard reset,
16090 * followed by fixups by BIOS, and has its config space
16091 * set up identically to what it was at cold boot.
16092 */
16093static pci_ers_result_t tg3_io_slot_reset(struct pci_dev *pdev)
16094{
16095 struct net_device *netdev = pci_get_drvdata(pdev);
16096 struct tg3 *tp = netdev_priv(netdev);
16097 pci_ers_result_t rc = PCI_ERS_RESULT_DISCONNECT;
16098 int err;
16099
16100 rtnl_lock();
16101
16102 if (pci_enable_device(pdev)) {
16103 netdev_err(netdev, "Cannot re-enable PCI device after reset.\n");
16104 goto done;
16105 }
16106
16107 pci_set_master(pdev);
16108 pci_restore_state(pdev);
16109 pci_save_state(pdev);
16110
16111 if (!netif_running(netdev)) {
16112 rc = PCI_ERS_RESULT_RECOVERED;
16113 goto done;
16114 }
16115
16116 err = tg3_power_up(tp);
Matt Carlsonbed98292011-07-13 09:27:29 +000016117 if (err)
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000016118 goto done;
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000016119
16120 rc = PCI_ERS_RESULT_RECOVERED;
16121
16122done:
16123 rtnl_unlock();
16124
16125 return rc;
16126}
16127
16128/**
16129 * tg3_io_resume - called when traffic can start flowing again.
16130 * @pdev: Pointer to PCI device
16131 *
16132 * This callback is called when the error recovery driver tells
16133 * us that its OK to resume normal operation.
16134 */
16135static void tg3_io_resume(struct pci_dev *pdev)
16136{
16137 struct net_device *netdev = pci_get_drvdata(pdev);
16138 struct tg3 *tp = netdev_priv(netdev);
16139 int err;
16140
16141 rtnl_lock();
16142
16143 if (!netif_running(netdev))
16144 goto done;
16145
16146 tg3_full_lock(tp, 0);
Joe Perches63c3a662011-04-26 08:12:10 +000016147 tg3_flag_set(tp, INIT_COMPLETE);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000016148 err = tg3_restart_hw(tp, 1);
16149 tg3_full_unlock(tp);
16150 if (err) {
16151 netdev_err(netdev, "Cannot restart hardware after reset.\n");
16152 goto done;
16153 }
16154
16155 netif_device_attach(netdev);
16156
Matt Carlson21f76382012-02-22 12:35:21 +000016157 tg3_timer_start(tp);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000016158
16159 tg3_netif_start(tp);
16160
16161 tg3_phy_start(tp);
16162
16163done:
16164 rtnl_unlock();
16165}
16166
16167static struct pci_error_handlers tg3_err_handler = {
16168 .error_detected = tg3_io_error_detected,
16169 .slot_reset = tg3_io_slot_reset,
16170 .resume = tg3_io_resume
16171};
16172
Linus Torvalds1da177e2005-04-16 15:20:36 -070016173static struct pci_driver tg3_driver = {
16174 .name = DRV_MODULE_NAME,
16175 .id_table = tg3_pci_tbl,
16176 .probe = tg3_init_one,
16177 .remove = __devexit_p(tg3_remove_one),
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000016178 .err_handler = &tg3_err_handler,
Eric Dumazetaa6027c2011-01-01 05:22:46 +000016179 .driver.pm = TG3_PM_OPS,
Linus Torvalds1da177e2005-04-16 15:20:36 -070016180};
16181
16182static int __init tg3_init(void)
16183{
Jeff Garzik29917622006-08-19 17:48:59 -040016184 return pci_register_driver(&tg3_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016185}
16186
16187static void __exit tg3_cleanup(void)
16188{
16189 pci_unregister_driver(&tg3_driver);
16190}
16191
16192module_init(tg3_init);
16193module_exit(tg3_cleanup);