blob: 482138ec64d21d63577e4e8390971689ff799874 [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 }
891 /* check for RX/TX work to do */
Matt Carlsonf3f3f272009-08-28 14:03:21 +0000892 if (sblk->idx[0].tx_consumer != tnapi->tx_cons ||
Matt Carlson8d9d7cf2009-09-01 13:19:05 +0000893 *(tnapi->rx_rcb_prod_idx) != tnapi->rx_rcb_ptr)
Michael Chan04237dd2005-04-25 15:17:17 -0700894 work_exists = 1;
895
896 return work_exists;
897}
898
Matt Carlson17375d22009-08-28 14:02:18 +0000899/* tg3_int_reenable
Michael Chan04237dd2005-04-25 15:17:17 -0700900 * similar to tg3_enable_ints, but it accurately determines whether there
901 * is new work pending and can return without flushing the PIO write
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400902 * which reenables interrupts
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 */
Matt Carlson17375d22009-08-28 14:02:18 +0000904static void tg3_int_reenable(struct tg3_napi *tnapi)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905{
Matt Carlson17375d22009-08-28 14:02:18 +0000906 struct tg3 *tp = tnapi->tp;
907
Matt Carlson898a56f2009-08-28 14:02:40 +0000908 tw32_mailbox(tnapi->int_mbox, tnapi->last_tag << 24);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 mmiowb();
910
David S. Millerfac9b832005-05-18 22:46:34 -0700911 /* When doing tagged status, this work check is unnecessary.
912 * The last_tag we write above tells the chip which piece of
913 * work we've completed.
914 */
Joe Perches63c3a662011-04-26 08:12:10 +0000915 if (!tg3_flag(tp, TAGGED_STATUS) && tg3_has_work(tnapi))
Michael Chan04237dd2005-04-25 15:17:17 -0700916 tw32(HOSTCC_MODE, tp->coalesce_mode |
Matt Carlsonfd2ce372009-09-01 12:51:13 +0000917 HOSTCC_MODE_ENABLE | tnapi->coal_now);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918}
919
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920static void tg3_switch_clocks(struct tg3 *tp)
921{
Matt Carlsonf6eb9b12009-09-01 13:19:53 +0000922 u32 clock_ctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 u32 orig_clock_ctrl;
924
Joe Perches63c3a662011-04-26 08:12:10 +0000925 if (tg3_flag(tp, CPMU_PRESENT) || tg3_flag(tp, 5780_CLASS))
Michael Chan4cf78e42005-07-25 12:29:19 -0700926 return;
927
Matt Carlsonf6eb9b12009-09-01 13:19:53 +0000928 clock_ctrl = tr32(TG3PCI_CLOCK_CTRL);
929
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 orig_clock_ctrl = clock_ctrl;
931 clock_ctrl &= (CLOCK_CTRL_FORCE_CLKRUN |
932 CLOCK_CTRL_CLKRUN_OENABLE |
933 0x1f);
934 tp->pci_clock_ctrl = clock_ctrl;
935
Joe Perches63c3a662011-04-26 08:12:10 +0000936 if (tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 if (orig_clock_ctrl & CLOCK_CTRL_625_CORE) {
Michael Chanb401e9e2005-12-19 16:27:04 -0800938 tw32_wait_f(TG3PCI_CLOCK_CTRL,
939 clock_ctrl | CLOCK_CTRL_625_CORE, 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 }
941 } else if ((orig_clock_ctrl & CLOCK_CTRL_44MHZ_CORE) != 0) {
Michael Chanb401e9e2005-12-19 16:27:04 -0800942 tw32_wait_f(TG3PCI_CLOCK_CTRL,
943 clock_ctrl |
944 (CLOCK_CTRL_44MHZ_CORE | CLOCK_CTRL_ALTCLK),
945 40);
946 tw32_wait_f(TG3PCI_CLOCK_CTRL,
947 clock_ctrl | (CLOCK_CTRL_ALTCLK),
948 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 }
Michael Chanb401e9e2005-12-19 16:27:04 -0800950 tw32_wait_f(TG3PCI_CLOCK_CTRL, clock_ctrl, 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951}
952
953#define PHY_BUSY_LOOPS 5000
954
955static int tg3_readphy(struct tg3 *tp, int reg, u32 *val)
956{
957 u32 frame_val;
958 unsigned int loops;
959 int ret;
960
961 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
962 tw32_f(MAC_MI_MODE,
963 (tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL));
964 udelay(80);
965 }
966
967 *val = 0x0;
968
Matt Carlson882e9792009-09-01 13:21:36 +0000969 frame_val = ((tp->phy_addr << MI_COM_PHY_ADDR_SHIFT) &
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 MI_COM_PHY_ADDR_MASK);
971 frame_val |= ((reg << MI_COM_REG_ADDR_SHIFT) &
972 MI_COM_REG_ADDR_MASK);
973 frame_val |= (MI_COM_CMD_READ | MI_COM_START);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400974
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 tw32_f(MAC_MI_COM, frame_val);
976
977 loops = PHY_BUSY_LOOPS;
978 while (loops != 0) {
979 udelay(10);
980 frame_val = tr32(MAC_MI_COM);
981
982 if ((frame_val & MI_COM_BUSY) == 0) {
983 udelay(5);
984 frame_val = tr32(MAC_MI_COM);
985 break;
986 }
987 loops -= 1;
988 }
989
990 ret = -EBUSY;
991 if (loops != 0) {
992 *val = frame_val & MI_COM_DATA_MASK;
993 ret = 0;
994 }
995
996 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
997 tw32_f(MAC_MI_MODE, tp->mi_mode);
998 udelay(80);
999 }
1000
1001 return ret;
1002}
1003
1004static int tg3_writephy(struct tg3 *tp, int reg, u32 val)
1005{
1006 u32 frame_val;
1007 unsigned int loops;
1008 int ret;
1009
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001010 if ((tp->phy_flags & TG3_PHYFLG_IS_FET) &&
Matt Carlson221c5632011-06-13 13:39:01 +00001011 (reg == MII_CTRL1000 || reg == MII_TG3_AUX_CTRL))
Michael Chanb5d37722006-09-27 16:06:21 -07001012 return 0;
1013
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
1015 tw32_f(MAC_MI_MODE,
1016 (tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL));
1017 udelay(80);
1018 }
1019
Matt Carlson882e9792009-09-01 13:21:36 +00001020 frame_val = ((tp->phy_addr << MI_COM_PHY_ADDR_SHIFT) &
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 MI_COM_PHY_ADDR_MASK);
1022 frame_val |= ((reg << MI_COM_REG_ADDR_SHIFT) &
1023 MI_COM_REG_ADDR_MASK);
1024 frame_val |= (val & MI_COM_DATA_MASK);
1025 frame_val |= (MI_COM_CMD_WRITE | MI_COM_START);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001026
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 tw32_f(MAC_MI_COM, frame_val);
1028
1029 loops = PHY_BUSY_LOOPS;
1030 while (loops != 0) {
1031 udelay(10);
1032 frame_val = tr32(MAC_MI_COM);
1033 if ((frame_val & MI_COM_BUSY) == 0) {
1034 udelay(5);
1035 frame_val = tr32(MAC_MI_COM);
1036 break;
1037 }
1038 loops -= 1;
1039 }
1040
1041 ret = -EBUSY;
1042 if (loops != 0)
1043 ret = 0;
1044
1045 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
1046 tw32_f(MAC_MI_MODE, tp->mi_mode);
1047 udelay(80);
1048 }
1049
1050 return ret;
1051}
1052
Matt Carlsonb0988c12011-04-20 07:57:39 +00001053static int tg3_phy_cl45_write(struct tg3 *tp, u32 devad, u32 addr, u32 val)
1054{
1055 int err;
1056
1057 err = tg3_writephy(tp, MII_TG3_MMD_CTRL, devad);
1058 if (err)
1059 goto done;
1060
1061 err = tg3_writephy(tp, MII_TG3_MMD_ADDRESS, addr);
1062 if (err)
1063 goto done;
1064
1065 err = tg3_writephy(tp, MII_TG3_MMD_CTRL,
1066 MII_TG3_MMD_CTRL_DATA_NOINC | devad);
1067 if (err)
1068 goto done;
1069
1070 err = tg3_writephy(tp, MII_TG3_MMD_ADDRESS, val);
1071
1072done:
1073 return err;
1074}
1075
1076static int tg3_phy_cl45_read(struct tg3 *tp, u32 devad, u32 addr, u32 *val)
1077{
1078 int err;
1079
1080 err = tg3_writephy(tp, MII_TG3_MMD_CTRL, devad);
1081 if (err)
1082 goto done;
1083
1084 err = tg3_writephy(tp, MII_TG3_MMD_ADDRESS, addr);
1085 if (err)
1086 goto done;
1087
1088 err = tg3_writephy(tp, MII_TG3_MMD_CTRL,
1089 MII_TG3_MMD_CTRL_DATA_NOINC | devad);
1090 if (err)
1091 goto done;
1092
1093 err = tg3_readphy(tp, MII_TG3_MMD_ADDRESS, val);
1094
1095done:
1096 return err;
1097}
1098
1099static int tg3_phydsp_read(struct tg3 *tp, u32 reg, u32 *val)
1100{
1101 int err;
1102
1103 err = tg3_writephy(tp, MII_TG3_DSP_ADDRESS, reg);
1104 if (!err)
1105 err = tg3_readphy(tp, MII_TG3_DSP_RW_PORT, val);
1106
1107 return err;
1108}
1109
1110static int tg3_phydsp_write(struct tg3 *tp, u32 reg, u32 val)
1111{
1112 int err;
1113
1114 err = tg3_writephy(tp, MII_TG3_DSP_ADDRESS, reg);
1115 if (!err)
1116 err = tg3_writephy(tp, MII_TG3_DSP_RW_PORT, val);
1117
1118 return err;
1119}
1120
Matt Carlson15ee95c2011-04-20 07:57:40 +00001121static int tg3_phy_auxctl_read(struct tg3 *tp, int reg, u32 *val)
1122{
1123 int err;
1124
1125 err = tg3_writephy(tp, MII_TG3_AUX_CTRL,
1126 (reg << MII_TG3_AUXCTL_MISC_RDSEL_SHIFT) |
1127 MII_TG3_AUXCTL_SHDWSEL_MISC);
1128 if (!err)
1129 err = tg3_readphy(tp, MII_TG3_AUX_CTRL, val);
1130
1131 return err;
1132}
1133
Matt Carlsonb4bd2922011-04-20 07:57:41 +00001134static int tg3_phy_auxctl_write(struct tg3 *tp, int reg, u32 set)
1135{
1136 if (reg == MII_TG3_AUXCTL_SHDWSEL_MISC)
1137 set |= MII_TG3_AUXCTL_MISC_WREN;
1138
1139 return tg3_writephy(tp, MII_TG3_AUX_CTRL, set | reg);
1140}
1141
Matt Carlson1d36ba42011-04-20 07:57:42 +00001142#define TG3_PHY_AUXCTL_SMDSP_ENABLE(tp) \
1143 tg3_phy_auxctl_write((tp), MII_TG3_AUXCTL_SHDWSEL_AUXCTL, \
1144 MII_TG3_AUXCTL_ACTL_SMDSP_ENA | \
1145 MII_TG3_AUXCTL_ACTL_TX_6DB)
1146
1147#define TG3_PHY_AUXCTL_SMDSP_DISABLE(tp) \
1148 tg3_phy_auxctl_write((tp), MII_TG3_AUXCTL_SHDWSEL_AUXCTL, \
1149 MII_TG3_AUXCTL_ACTL_TX_6DB);
1150
Matt Carlson95e28692008-05-25 23:44:14 -07001151static int tg3_bmcr_reset(struct tg3 *tp)
1152{
1153 u32 phy_control;
1154 int limit, err;
1155
1156 /* OK, reset it, and poll the BMCR_RESET bit until it
1157 * clears or we time out.
1158 */
1159 phy_control = BMCR_RESET;
1160 err = tg3_writephy(tp, MII_BMCR, phy_control);
1161 if (err != 0)
1162 return -EBUSY;
1163
1164 limit = 5000;
1165 while (limit--) {
1166 err = tg3_readphy(tp, MII_BMCR, &phy_control);
1167 if (err != 0)
1168 return -EBUSY;
1169
1170 if ((phy_control & BMCR_RESET) == 0) {
1171 udelay(40);
1172 break;
1173 }
1174 udelay(10);
1175 }
Roel Kluind4675b52009-02-12 16:33:27 -08001176 if (limit < 0)
Matt Carlson95e28692008-05-25 23:44:14 -07001177 return -EBUSY;
1178
1179 return 0;
1180}
1181
Matt Carlson158d7ab2008-05-29 01:37:54 -07001182static int tg3_mdio_read(struct mii_bus *bp, int mii_id, int reg)
1183{
Francois Romieu3d165432009-01-19 16:56:50 -08001184 struct tg3 *tp = bp->priv;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001185 u32 val;
1186
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001187 spin_lock_bh(&tp->lock);
Matt Carlson158d7ab2008-05-29 01:37:54 -07001188
1189 if (tg3_readphy(tp, reg, &val))
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001190 val = -EIO;
1191
1192 spin_unlock_bh(&tp->lock);
Matt Carlson158d7ab2008-05-29 01:37:54 -07001193
1194 return val;
1195}
1196
1197static int tg3_mdio_write(struct mii_bus *bp, int mii_id, int reg, u16 val)
1198{
Francois Romieu3d165432009-01-19 16:56:50 -08001199 struct tg3 *tp = bp->priv;
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001200 u32 ret = 0;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001201
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001202 spin_lock_bh(&tp->lock);
Matt Carlson158d7ab2008-05-29 01:37:54 -07001203
1204 if (tg3_writephy(tp, reg, val))
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001205 ret = -EIO;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001206
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001207 spin_unlock_bh(&tp->lock);
1208
1209 return ret;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001210}
1211
1212static int tg3_mdio_reset(struct mii_bus *bp)
1213{
1214 return 0;
1215}
1216
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001217static void tg3_mdio_config_5785(struct tg3 *tp)
Matt Carlsona9daf362008-05-25 23:49:44 -07001218{
1219 u32 val;
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001220 struct phy_device *phydev;
Matt Carlsona9daf362008-05-25 23:49:44 -07001221
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001222 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001223 switch (phydev->drv->phy_id & phydev->drv->phy_id_mask) {
Matt Carlson6a443a02010-02-17 15:17:04 +00001224 case PHY_ID_BCM50610:
1225 case PHY_ID_BCM50610M:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001226 val = MAC_PHYCFG2_50610_LED_MODES;
1227 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001228 case PHY_ID_BCMAC131:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001229 val = MAC_PHYCFG2_AC131_LED_MODES;
1230 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001231 case PHY_ID_RTL8211C:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001232 val = MAC_PHYCFG2_RTL8211C_LED_MODES;
1233 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001234 case PHY_ID_RTL8201E:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001235 val = MAC_PHYCFG2_RTL8201E_LED_MODES;
1236 break;
1237 default:
Matt Carlsona9daf362008-05-25 23:49:44 -07001238 return;
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001239 }
1240
1241 if (phydev->interface != PHY_INTERFACE_MODE_RGMII) {
1242 tw32(MAC_PHYCFG2, val);
1243
1244 val = tr32(MAC_PHYCFG1);
Matt Carlsonbb85fbb2009-08-25 10:09:07 +00001245 val &= ~(MAC_PHYCFG1_RGMII_INT |
1246 MAC_PHYCFG1_RXCLK_TO_MASK | MAC_PHYCFG1_TXCLK_TO_MASK);
1247 val |= MAC_PHYCFG1_RXCLK_TIMEOUT | MAC_PHYCFG1_TXCLK_TIMEOUT;
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001248 tw32(MAC_PHYCFG1, val);
1249
1250 return;
1251 }
1252
Joe Perches63c3a662011-04-26 08:12:10 +00001253 if (!tg3_flag(tp, RGMII_INBAND_DISABLE))
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001254 val |= MAC_PHYCFG2_EMODE_MASK_MASK |
1255 MAC_PHYCFG2_FMODE_MASK_MASK |
1256 MAC_PHYCFG2_GMODE_MASK_MASK |
1257 MAC_PHYCFG2_ACT_MASK_MASK |
1258 MAC_PHYCFG2_QUAL_MASK_MASK |
1259 MAC_PHYCFG2_INBAND_ENABLE;
1260
1261 tw32(MAC_PHYCFG2, val);
Matt Carlsona9daf362008-05-25 23:49:44 -07001262
Matt Carlsonbb85fbb2009-08-25 10:09:07 +00001263 val = tr32(MAC_PHYCFG1);
1264 val &= ~(MAC_PHYCFG1_RXCLK_TO_MASK | MAC_PHYCFG1_TXCLK_TO_MASK |
1265 MAC_PHYCFG1_RGMII_EXT_RX_DEC | MAC_PHYCFG1_RGMII_SND_STAT_EN);
Joe Perches63c3a662011-04-26 08:12:10 +00001266 if (!tg3_flag(tp, RGMII_INBAND_DISABLE)) {
1267 if (tg3_flag(tp, RGMII_EXT_IBND_RX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001268 val |= MAC_PHYCFG1_RGMII_EXT_RX_DEC;
Joe Perches63c3a662011-04-26 08:12:10 +00001269 if (tg3_flag(tp, RGMII_EXT_IBND_TX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001270 val |= MAC_PHYCFG1_RGMII_SND_STAT_EN;
1271 }
Matt Carlsonbb85fbb2009-08-25 10:09:07 +00001272 val |= MAC_PHYCFG1_RXCLK_TIMEOUT | MAC_PHYCFG1_TXCLK_TIMEOUT |
1273 MAC_PHYCFG1_RGMII_INT | MAC_PHYCFG1_TXC_DRV;
1274 tw32(MAC_PHYCFG1, val);
Matt Carlsona9daf362008-05-25 23:49:44 -07001275
Matt Carlsona9daf362008-05-25 23:49:44 -07001276 val = tr32(MAC_EXT_RGMII_MODE);
1277 val &= ~(MAC_RGMII_MODE_RX_INT_B |
1278 MAC_RGMII_MODE_RX_QUALITY |
1279 MAC_RGMII_MODE_RX_ACTIVITY |
1280 MAC_RGMII_MODE_RX_ENG_DET |
1281 MAC_RGMII_MODE_TX_ENABLE |
1282 MAC_RGMII_MODE_TX_LOWPWR |
1283 MAC_RGMII_MODE_TX_RESET);
Joe Perches63c3a662011-04-26 08:12:10 +00001284 if (!tg3_flag(tp, RGMII_INBAND_DISABLE)) {
1285 if (tg3_flag(tp, RGMII_EXT_IBND_RX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001286 val |= MAC_RGMII_MODE_RX_INT_B |
1287 MAC_RGMII_MODE_RX_QUALITY |
1288 MAC_RGMII_MODE_RX_ACTIVITY |
1289 MAC_RGMII_MODE_RX_ENG_DET;
Joe Perches63c3a662011-04-26 08:12:10 +00001290 if (tg3_flag(tp, RGMII_EXT_IBND_TX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001291 val |= MAC_RGMII_MODE_TX_ENABLE |
1292 MAC_RGMII_MODE_TX_LOWPWR |
1293 MAC_RGMII_MODE_TX_RESET;
1294 }
1295 tw32(MAC_EXT_RGMII_MODE, val);
1296}
1297
Matt Carlson158d7ab2008-05-29 01:37:54 -07001298static void tg3_mdio_start(struct tg3 *tp)
1299{
Matt Carlson158d7ab2008-05-29 01:37:54 -07001300 tp->mi_mode &= ~MAC_MI_MODE_AUTO_POLL;
1301 tw32_f(MAC_MI_MODE, tp->mi_mode);
1302 udelay(80);
Matt Carlsona9daf362008-05-25 23:49:44 -07001303
Joe Perches63c3a662011-04-26 08:12:10 +00001304 if (tg3_flag(tp, MDIOBUS_INITED) &&
Matt Carlson9ea48182010-02-17 15:17:01 +00001305 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
1306 tg3_mdio_config_5785(tp);
1307}
1308
1309static int tg3_mdio_init(struct tg3 *tp)
1310{
1311 int i;
1312 u32 reg;
1313 struct phy_device *phydev;
1314
Joe Perches63c3a662011-04-26 08:12:10 +00001315 if (tg3_flag(tp, 5717_PLUS)) {
Matt Carlson9c7df912010-06-05 17:24:36 +00001316 u32 is_serdes;
Matt Carlson882e9792009-09-01 13:21:36 +00001317
Matt Carlson69f11c92011-07-13 09:27:30 +00001318 tp->phy_addr = tp->pci_fn + 1;
Matt Carlson882e9792009-09-01 13:21:36 +00001319
Matt Carlsond1ec96a2010-01-12 10:11:38 +00001320 if (tp->pci_chip_rev_id != CHIPREV_ID_5717_A0)
1321 is_serdes = tr32(SG_DIG_STATUS) & SG_DIG_IS_SERDES;
1322 else
1323 is_serdes = tr32(TG3_CPMU_PHY_STRAP) &
1324 TG3_CPMU_PHY_STRAP_IS_SERDES;
Matt Carlson882e9792009-09-01 13:21:36 +00001325 if (is_serdes)
1326 tp->phy_addr += 7;
1327 } else
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001328 tp->phy_addr = TG3_PHY_MII_ADDR;
Matt Carlson882e9792009-09-01 13:21:36 +00001329
Matt Carlson158d7ab2008-05-29 01:37:54 -07001330 tg3_mdio_start(tp);
1331
Joe Perches63c3a662011-04-26 08:12:10 +00001332 if (!tg3_flag(tp, USE_PHYLIB) || tg3_flag(tp, MDIOBUS_INITED))
Matt Carlson158d7ab2008-05-29 01:37:54 -07001333 return 0;
1334
Lennert Buytenhek298cf9be2008-10-08 16:29:57 -07001335 tp->mdio_bus = mdiobus_alloc();
1336 if (tp->mdio_bus == NULL)
1337 return -ENOMEM;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001338
Lennert Buytenhek298cf9be2008-10-08 16:29:57 -07001339 tp->mdio_bus->name = "tg3 mdio bus";
1340 snprintf(tp->mdio_bus->id, MII_BUS_ID_SIZE, "%x",
Matt Carlson158d7ab2008-05-29 01:37:54 -07001341 (tp->pdev->bus->number << 8) | tp->pdev->devfn);
Lennert Buytenhek298cf9be2008-10-08 16:29:57 -07001342 tp->mdio_bus->priv = tp;
1343 tp->mdio_bus->parent = &tp->pdev->dev;
1344 tp->mdio_bus->read = &tg3_mdio_read;
1345 tp->mdio_bus->write = &tg3_mdio_write;
1346 tp->mdio_bus->reset = &tg3_mdio_reset;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001347 tp->mdio_bus->phy_mask = ~(1 << TG3_PHY_MII_ADDR);
Lennert Buytenhek298cf9be2008-10-08 16:29:57 -07001348 tp->mdio_bus->irq = &tp->mdio_irq[0];
Matt Carlson158d7ab2008-05-29 01:37:54 -07001349
1350 for (i = 0; i < PHY_MAX_ADDR; i++)
Lennert Buytenhek298cf9be2008-10-08 16:29:57 -07001351 tp->mdio_bus->irq[i] = PHY_POLL;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001352
1353 /* The bus registration will look for all the PHYs on the mdio bus.
1354 * Unfortunately, it does not ensure the PHY is powered up before
1355 * accessing the PHY ID registers. A chip reset is the
1356 * quickest way to bring the device back to an operational state..
1357 */
1358 if (tg3_readphy(tp, MII_BMCR, &reg) || (reg & BMCR_PDOWN))
1359 tg3_bmcr_reset(tp);
1360
Lennert Buytenhek298cf9be2008-10-08 16:29:57 -07001361 i = mdiobus_register(tp->mdio_bus);
Matt Carlsona9daf362008-05-25 23:49:44 -07001362 if (i) {
Matt Carlsonab96b242010-04-05 10:19:22 +00001363 dev_warn(&tp->pdev->dev, "mdiobus_reg failed (0x%x)\n", i);
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001364 mdiobus_free(tp->mdio_bus);
Matt Carlsona9daf362008-05-25 23:49:44 -07001365 return i;
1366 }
Matt Carlson158d7ab2008-05-29 01:37:54 -07001367
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001368 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsona9daf362008-05-25 23:49:44 -07001369
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001370 if (!phydev || !phydev->drv) {
Matt Carlsonab96b242010-04-05 10:19:22 +00001371 dev_warn(&tp->pdev->dev, "No PHY devices\n");
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001372 mdiobus_unregister(tp->mdio_bus);
1373 mdiobus_free(tp->mdio_bus);
1374 return -ENODEV;
1375 }
1376
1377 switch (phydev->drv->phy_id & phydev->drv->phy_id_mask) {
Matt Carlson6a443a02010-02-17 15:17:04 +00001378 case PHY_ID_BCM57780:
Matt Carlson321d32a2008-11-21 17:22:19 -08001379 phydev->interface = PHY_INTERFACE_MODE_GMII;
Matt Carlsonc704dc22009-11-02 14:32:12 +00001380 phydev->dev_flags |= PHY_BRCM_AUTO_PWRDWN_ENABLE;
Matt Carlson321d32a2008-11-21 17:22:19 -08001381 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001382 case PHY_ID_BCM50610:
1383 case PHY_ID_BCM50610M:
Matt Carlson32e5a8d2009-11-02 14:31:39 +00001384 phydev->dev_flags |= PHY_BRCM_CLEAR_RGMII_MODE |
Matt Carlsonc704dc22009-11-02 14:32:12 +00001385 PHY_BRCM_RX_REFCLK_UNUSED |
Matt Carlson52fae082009-11-02 14:32:38 +00001386 PHY_BRCM_DIS_TXCRXC_NOENRGY |
Matt Carlsonc704dc22009-11-02 14:32:12 +00001387 PHY_BRCM_AUTO_PWRDWN_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00001388 if (tg3_flag(tp, RGMII_INBAND_DISABLE))
Matt Carlsona9daf362008-05-25 23:49:44 -07001389 phydev->dev_flags |= PHY_BRCM_STD_IBND_DISABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00001390 if (tg3_flag(tp, RGMII_EXT_IBND_RX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001391 phydev->dev_flags |= PHY_BRCM_EXT_IBND_RX_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00001392 if (tg3_flag(tp, RGMII_EXT_IBND_TX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001393 phydev->dev_flags |= PHY_BRCM_EXT_IBND_TX_ENABLE;
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001394 /* fallthru */
Matt Carlson6a443a02010-02-17 15:17:04 +00001395 case PHY_ID_RTL8211C:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001396 phydev->interface = PHY_INTERFACE_MODE_RGMII;
Matt Carlsona9daf362008-05-25 23:49:44 -07001397 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001398 case PHY_ID_RTL8201E:
1399 case PHY_ID_BCMAC131:
Matt Carlsona9daf362008-05-25 23:49:44 -07001400 phydev->interface = PHY_INTERFACE_MODE_MII;
Matt Carlsoncdd4e09d2009-11-02 14:31:11 +00001401 phydev->dev_flags |= PHY_BRCM_AUTO_PWRDWN_ENABLE;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001402 tp->phy_flags |= TG3_PHYFLG_IS_FET;
Matt Carlsona9daf362008-05-25 23:49:44 -07001403 break;
1404 }
1405
Joe Perches63c3a662011-04-26 08:12:10 +00001406 tg3_flag_set(tp, MDIOBUS_INITED);
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001407
1408 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
1409 tg3_mdio_config_5785(tp);
Matt Carlsona9daf362008-05-25 23:49:44 -07001410
1411 return 0;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001412}
1413
1414static void tg3_mdio_fini(struct tg3 *tp)
1415{
Joe Perches63c3a662011-04-26 08:12:10 +00001416 if (tg3_flag(tp, MDIOBUS_INITED)) {
1417 tg3_flag_clear(tp, MDIOBUS_INITED);
Lennert Buytenhek298cf9be2008-10-08 16:29:57 -07001418 mdiobus_unregister(tp->mdio_bus);
1419 mdiobus_free(tp->mdio_bus);
Matt Carlson158d7ab2008-05-29 01:37:54 -07001420 }
1421}
1422
Matt Carlson95e28692008-05-25 23:44:14 -07001423/* tp->lock is held. */
Matt Carlson4ba526c2008-08-15 14:10:04 -07001424static inline void tg3_generate_fw_event(struct tg3 *tp)
1425{
1426 u32 val;
1427
1428 val = tr32(GRC_RX_CPU_EVENT);
1429 val |= GRC_RX_CPU_DRIVER_EVENT;
1430 tw32_f(GRC_RX_CPU_EVENT, val);
1431
1432 tp->last_event_jiffies = jiffies;
1433}
1434
1435#define TG3_FW_EVENT_TIMEOUT_USEC 2500
1436
1437/* tp->lock is held. */
Matt Carlson95e28692008-05-25 23:44:14 -07001438static void tg3_wait_for_event_ack(struct tg3 *tp)
1439{
1440 int i;
Matt Carlson4ba526c2008-08-15 14:10:04 -07001441 unsigned int delay_cnt;
1442 long time_remain;
Matt Carlson95e28692008-05-25 23:44:14 -07001443
Matt Carlson4ba526c2008-08-15 14:10:04 -07001444 /* If enough time has passed, no wait is necessary. */
1445 time_remain = (long)(tp->last_event_jiffies + 1 +
1446 usecs_to_jiffies(TG3_FW_EVENT_TIMEOUT_USEC)) -
1447 (long)jiffies;
1448 if (time_remain < 0)
1449 return;
1450
1451 /* Check if we can shorten the wait time. */
1452 delay_cnt = jiffies_to_usecs(time_remain);
1453 if (delay_cnt > TG3_FW_EVENT_TIMEOUT_USEC)
1454 delay_cnt = TG3_FW_EVENT_TIMEOUT_USEC;
1455 delay_cnt = (delay_cnt >> 3) + 1;
1456
1457 for (i = 0; i < delay_cnt; i++) {
Matt Carlson95e28692008-05-25 23:44:14 -07001458 if (!(tr32(GRC_RX_CPU_EVENT) & GRC_RX_CPU_DRIVER_EVENT))
1459 break;
Matt Carlson4ba526c2008-08-15 14:10:04 -07001460 udelay(8);
Matt Carlson95e28692008-05-25 23:44:14 -07001461 }
1462}
1463
1464/* tp->lock is held. */
Matt Carlsonb28f3892012-02-13 15:20:12 +00001465static void tg3_phy_gather_ump_data(struct tg3 *tp, u32 *data)
Matt Carlson95e28692008-05-25 23:44:14 -07001466{
Matt Carlsonb28f3892012-02-13 15:20:12 +00001467 u32 reg, val;
Matt Carlson95e28692008-05-25 23:44:14 -07001468
1469 val = 0;
1470 if (!tg3_readphy(tp, MII_BMCR, &reg))
1471 val = reg << 16;
1472 if (!tg3_readphy(tp, MII_BMSR, &reg))
1473 val |= (reg & 0xffff);
Matt Carlsonb28f3892012-02-13 15:20:12 +00001474 *data++ = val;
Matt Carlson95e28692008-05-25 23:44:14 -07001475
1476 val = 0;
1477 if (!tg3_readphy(tp, MII_ADVERTISE, &reg))
1478 val = reg << 16;
1479 if (!tg3_readphy(tp, MII_LPA, &reg))
1480 val |= (reg & 0xffff);
Matt Carlsonb28f3892012-02-13 15:20:12 +00001481 *data++ = val;
Matt Carlson95e28692008-05-25 23:44:14 -07001482
1483 val = 0;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001484 if (!(tp->phy_flags & TG3_PHYFLG_MII_SERDES)) {
Matt Carlson95e28692008-05-25 23:44:14 -07001485 if (!tg3_readphy(tp, MII_CTRL1000, &reg))
1486 val = reg << 16;
1487 if (!tg3_readphy(tp, MII_STAT1000, &reg))
1488 val |= (reg & 0xffff);
1489 }
Matt Carlsonb28f3892012-02-13 15:20:12 +00001490 *data++ = val;
Matt Carlson95e28692008-05-25 23:44:14 -07001491
1492 if (!tg3_readphy(tp, MII_PHYADDR, &reg))
1493 val = reg << 16;
1494 else
1495 val = 0;
Matt Carlsonb28f3892012-02-13 15:20:12 +00001496 *data++ = val;
1497}
1498
1499/* tp->lock is held. */
1500static void tg3_ump_link_report(struct tg3 *tp)
1501{
1502 u32 data[4];
1503
1504 if (!tg3_flag(tp, 5780_CLASS) || !tg3_flag(tp, ENABLE_ASF))
1505 return;
1506
1507 tg3_phy_gather_ump_data(tp, data);
1508
1509 tg3_wait_for_event_ack(tp);
1510
1511 tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX, FWCMD_NICDRV_LINK_UPDATE);
1512 tg3_write_mem(tp, NIC_SRAM_FW_CMD_LEN_MBOX, 14);
1513 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 0x0, data[0]);
1514 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 0x4, data[1]);
1515 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 0x8, data[2]);
1516 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 0xc, data[3]);
Matt Carlson95e28692008-05-25 23:44:14 -07001517
Matt Carlson4ba526c2008-08-15 14:10:04 -07001518 tg3_generate_fw_event(tp);
Matt Carlson95e28692008-05-25 23:44:14 -07001519}
1520
Matt Carlson8d5a89b2011-08-31 11:44:51 +00001521/* tp->lock is held. */
1522static void tg3_stop_fw(struct tg3 *tp)
1523{
1524 if (tg3_flag(tp, ENABLE_ASF) && !tg3_flag(tp, ENABLE_APE)) {
1525 /* Wait for RX cpu to ACK the previous event. */
1526 tg3_wait_for_event_ack(tp);
1527
1528 tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX, FWCMD_NICDRV_PAUSE_FW);
1529
1530 tg3_generate_fw_event(tp);
1531
1532 /* Wait for RX cpu to ACK this event. */
1533 tg3_wait_for_event_ack(tp);
1534 }
1535}
1536
Matt Carlsonfd6d3f02011-08-31 11:44:52 +00001537/* tp->lock is held. */
1538static void tg3_write_sig_pre_reset(struct tg3 *tp, int kind)
1539{
1540 tg3_write_mem(tp, NIC_SRAM_FIRMWARE_MBOX,
1541 NIC_SRAM_FIRMWARE_MBOX_MAGIC1);
1542
1543 if (tg3_flag(tp, ASF_NEW_HANDSHAKE)) {
1544 switch (kind) {
1545 case RESET_KIND_INIT:
1546 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1547 DRV_STATE_START);
1548 break;
1549
1550 case RESET_KIND_SHUTDOWN:
1551 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1552 DRV_STATE_UNLOAD);
1553 break;
1554
1555 case RESET_KIND_SUSPEND:
1556 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1557 DRV_STATE_SUSPEND);
1558 break;
1559
1560 default:
1561 break;
1562 }
1563 }
1564
1565 if (kind == RESET_KIND_INIT ||
1566 kind == RESET_KIND_SUSPEND)
1567 tg3_ape_driver_state_change(tp, kind);
1568}
1569
1570/* tp->lock is held. */
1571static void tg3_write_sig_post_reset(struct tg3 *tp, int kind)
1572{
1573 if (tg3_flag(tp, ASF_NEW_HANDSHAKE)) {
1574 switch (kind) {
1575 case RESET_KIND_INIT:
1576 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1577 DRV_STATE_START_DONE);
1578 break;
1579
1580 case RESET_KIND_SHUTDOWN:
1581 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1582 DRV_STATE_UNLOAD_DONE);
1583 break;
1584
1585 default:
1586 break;
1587 }
1588 }
1589
1590 if (kind == RESET_KIND_SHUTDOWN)
1591 tg3_ape_driver_state_change(tp, kind);
1592}
1593
1594/* tp->lock is held. */
1595static void tg3_write_sig_legacy(struct tg3 *tp, int kind)
1596{
1597 if (tg3_flag(tp, ENABLE_ASF)) {
1598 switch (kind) {
1599 case RESET_KIND_INIT:
1600 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1601 DRV_STATE_START);
1602 break;
1603
1604 case RESET_KIND_SHUTDOWN:
1605 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1606 DRV_STATE_UNLOAD);
1607 break;
1608
1609 case RESET_KIND_SUSPEND:
1610 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1611 DRV_STATE_SUSPEND);
1612 break;
1613
1614 default:
1615 break;
1616 }
1617 }
1618}
1619
1620static int tg3_poll_fw(struct tg3 *tp)
1621{
1622 int i;
1623 u32 val;
1624
1625 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
1626 /* Wait up to 20ms for init done. */
1627 for (i = 0; i < 200; i++) {
1628 if (tr32(VCPU_STATUS) & VCPU_STATUS_INIT_DONE)
1629 return 0;
1630 udelay(100);
1631 }
1632 return -ENODEV;
1633 }
1634
1635 /* Wait for firmware initialization to complete. */
1636 for (i = 0; i < 100000; i++) {
1637 tg3_read_mem(tp, NIC_SRAM_FIRMWARE_MBOX, &val);
1638 if (val == ~NIC_SRAM_FIRMWARE_MBOX_MAGIC1)
1639 break;
1640 udelay(10);
1641 }
1642
1643 /* Chip might not be fitted with firmware. Some Sun onboard
1644 * parts are configured like that. So don't signal the timeout
1645 * of the above loop as an error, but do report the lack of
1646 * running firmware once.
1647 */
1648 if (i >= 100000 && !tg3_flag(tp, NO_FWARE_REPORTED)) {
1649 tg3_flag_set(tp, NO_FWARE_REPORTED);
1650
1651 netdev_info(tp->dev, "No firmware running\n");
1652 }
1653
1654 if (tp->pci_chip_rev_id == CHIPREV_ID_57765_A0) {
1655 /* The 57765 A0 needs a little more
1656 * time to do some important work.
1657 */
1658 mdelay(10);
1659 }
1660
1661 return 0;
1662}
1663
Matt Carlson95e28692008-05-25 23:44:14 -07001664static void tg3_link_report(struct tg3 *tp)
1665{
1666 if (!netif_carrier_ok(tp->dev)) {
Joe Perches05dbe002010-02-17 19:44:19 +00001667 netif_info(tp, link, tp->dev, "Link is down\n");
Matt Carlson95e28692008-05-25 23:44:14 -07001668 tg3_ump_link_report(tp);
1669 } else if (netif_msg_link(tp)) {
Joe Perches05dbe002010-02-17 19:44:19 +00001670 netdev_info(tp->dev, "Link is up at %d Mbps, %s duplex\n",
1671 (tp->link_config.active_speed == SPEED_1000 ?
1672 1000 :
1673 (tp->link_config.active_speed == SPEED_100 ?
1674 100 : 10)),
1675 (tp->link_config.active_duplex == DUPLEX_FULL ?
1676 "full" : "half"));
Matt Carlson95e28692008-05-25 23:44:14 -07001677
Joe Perches05dbe002010-02-17 19:44:19 +00001678 netdev_info(tp->dev, "Flow control is %s for TX and %s for RX\n",
1679 (tp->link_config.active_flowctrl & FLOW_CTRL_TX) ?
1680 "on" : "off",
1681 (tp->link_config.active_flowctrl & FLOW_CTRL_RX) ?
1682 "on" : "off");
Matt Carlson47007832011-04-20 07:57:43 +00001683
1684 if (tp->phy_flags & TG3_PHYFLG_EEE_CAP)
1685 netdev_info(tp->dev, "EEE is %s\n",
1686 tp->setlpicnt ? "enabled" : "disabled");
1687
Matt Carlson95e28692008-05-25 23:44:14 -07001688 tg3_ump_link_report(tp);
1689 }
1690}
1691
Matt Carlson95e28692008-05-25 23:44:14 -07001692static u16 tg3_advert_flowctrl_1000X(u8 flow_ctrl)
1693{
1694 u16 miireg;
1695
Steve Glendinninge18ce342008-12-16 02:00:00 -08001696 if ((flow_ctrl & FLOW_CTRL_TX) && (flow_ctrl & FLOW_CTRL_RX))
Matt Carlson95e28692008-05-25 23:44:14 -07001697 miireg = ADVERTISE_1000XPAUSE;
Steve Glendinninge18ce342008-12-16 02:00:00 -08001698 else if (flow_ctrl & FLOW_CTRL_TX)
Matt Carlson95e28692008-05-25 23:44:14 -07001699 miireg = ADVERTISE_1000XPSE_ASYM;
Steve Glendinninge18ce342008-12-16 02:00:00 -08001700 else if (flow_ctrl & FLOW_CTRL_RX)
Matt Carlson95e28692008-05-25 23:44:14 -07001701 miireg = ADVERTISE_1000XPAUSE | ADVERTISE_1000XPSE_ASYM;
1702 else
1703 miireg = 0;
1704
1705 return miireg;
1706}
1707
Matt Carlson95e28692008-05-25 23:44:14 -07001708static u8 tg3_resolve_flowctrl_1000X(u16 lcladv, u16 rmtadv)
1709{
1710 u8 cap = 0;
1711
Matt Carlsonf3791cd2011-11-21 15:01:17 +00001712 if (lcladv & rmtadv & ADVERTISE_1000XPAUSE) {
1713 cap = FLOW_CTRL_TX | FLOW_CTRL_RX;
1714 } else if (lcladv & rmtadv & ADVERTISE_1000XPSE_ASYM) {
1715 if (lcladv & ADVERTISE_1000XPAUSE)
1716 cap = FLOW_CTRL_RX;
1717 if (rmtadv & ADVERTISE_1000XPAUSE)
Steve Glendinninge18ce342008-12-16 02:00:00 -08001718 cap = FLOW_CTRL_TX;
Matt Carlson95e28692008-05-25 23:44:14 -07001719 }
1720
1721 return cap;
1722}
1723
Matt Carlsonf51f3562008-05-25 23:45:08 -07001724static void tg3_setup_flow_control(struct tg3 *tp, u32 lcladv, u32 rmtadv)
Matt Carlson95e28692008-05-25 23:44:14 -07001725{
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001726 u8 autoneg;
Matt Carlsonf51f3562008-05-25 23:45:08 -07001727 u8 flowctrl = 0;
Matt Carlson95e28692008-05-25 23:44:14 -07001728 u32 old_rx_mode = tp->rx_mode;
1729 u32 old_tx_mode = tp->tx_mode;
1730
Joe Perches63c3a662011-04-26 08:12:10 +00001731 if (tg3_flag(tp, USE_PHYLIB))
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001732 autoneg = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]->autoneg;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001733 else
1734 autoneg = tp->link_config.autoneg;
1735
Joe Perches63c3a662011-04-26 08:12:10 +00001736 if (autoneg == AUTONEG_ENABLE && tg3_flag(tp, PAUSE_AUTONEG)) {
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001737 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
Matt Carlsonf51f3562008-05-25 23:45:08 -07001738 flowctrl = tg3_resolve_flowctrl_1000X(lcladv, rmtadv);
Matt Carlson95e28692008-05-25 23:44:14 -07001739 else
Steve Glendinningbc02ff92008-12-16 02:00:48 -08001740 flowctrl = mii_resolve_flowctrl_fdx(lcladv, rmtadv);
Matt Carlsonf51f3562008-05-25 23:45:08 -07001741 } else
1742 flowctrl = tp->link_config.flowctrl;
Matt Carlson95e28692008-05-25 23:44:14 -07001743
Matt Carlsonf51f3562008-05-25 23:45:08 -07001744 tp->link_config.active_flowctrl = flowctrl;
Matt Carlson95e28692008-05-25 23:44:14 -07001745
Steve Glendinninge18ce342008-12-16 02:00:00 -08001746 if (flowctrl & FLOW_CTRL_RX)
Matt Carlson95e28692008-05-25 23:44:14 -07001747 tp->rx_mode |= RX_MODE_FLOW_CTRL_ENABLE;
1748 else
1749 tp->rx_mode &= ~RX_MODE_FLOW_CTRL_ENABLE;
1750
Matt Carlsonf51f3562008-05-25 23:45:08 -07001751 if (old_rx_mode != tp->rx_mode)
Matt Carlson95e28692008-05-25 23:44:14 -07001752 tw32_f(MAC_RX_MODE, tp->rx_mode);
Matt Carlson95e28692008-05-25 23:44:14 -07001753
Steve Glendinninge18ce342008-12-16 02:00:00 -08001754 if (flowctrl & FLOW_CTRL_TX)
Matt Carlson95e28692008-05-25 23:44:14 -07001755 tp->tx_mode |= TX_MODE_FLOW_CTRL_ENABLE;
1756 else
1757 tp->tx_mode &= ~TX_MODE_FLOW_CTRL_ENABLE;
1758
Matt Carlsonf51f3562008-05-25 23:45:08 -07001759 if (old_tx_mode != tp->tx_mode)
Matt Carlson95e28692008-05-25 23:44:14 -07001760 tw32_f(MAC_TX_MODE, tp->tx_mode);
Matt Carlson95e28692008-05-25 23:44:14 -07001761}
1762
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001763static void tg3_adjust_link(struct net_device *dev)
1764{
1765 u8 oldflowctrl, linkmesg = 0;
1766 u32 mac_mode, lcl_adv, rmt_adv;
1767 struct tg3 *tp = netdev_priv(dev);
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001768 struct phy_device *phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001769
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001770 spin_lock_bh(&tp->lock);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001771
1772 mac_mode = tp->mac_mode & ~(MAC_MODE_PORT_MODE_MASK |
1773 MAC_MODE_HALF_DUPLEX);
1774
1775 oldflowctrl = tp->link_config.active_flowctrl;
1776
1777 if (phydev->link) {
1778 lcl_adv = 0;
1779 rmt_adv = 0;
1780
1781 if (phydev->speed == SPEED_100 || phydev->speed == SPEED_10)
1782 mac_mode |= MAC_MODE_PORT_MODE_MII;
Matt Carlsonc3df0742009-11-02 14:27:02 +00001783 else if (phydev->speed == SPEED_1000 ||
1784 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785)
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001785 mac_mode |= MAC_MODE_PORT_MODE_GMII;
Matt Carlsonc3df0742009-11-02 14:27:02 +00001786 else
1787 mac_mode |= MAC_MODE_PORT_MODE_MII;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001788
1789 if (phydev->duplex == DUPLEX_HALF)
1790 mac_mode |= MAC_MODE_HALF_DUPLEX;
1791 else {
Matt Carlsonf88788f2011-12-14 11:10:00 +00001792 lcl_adv = mii_advertise_flowctrl(
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001793 tp->link_config.flowctrl);
1794
1795 if (phydev->pause)
1796 rmt_adv = LPA_PAUSE_CAP;
1797 if (phydev->asym_pause)
1798 rmt_adv |= LPA_PAUSE_ASYM;
1799 }
1800
1801 tg3_setup_flow_control(tp, lcl_adv, rmt_adv);
1802 } else
1803 mac_mode |= MAC_MODE_PORT_MODE_GMII;
1804
1805 if (mac_mode != tp->mac_mode) {
1806 tp->mac_mode = mac_mode;
1807 tw32_f(MAC_MODE, tp->mac_mode);
1808 udelay(40);
1809 }
1810
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001811 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785) {
1812 if (phydev->speed == SPEED_10)
1813 tw32(MAC_MI_STAT,
1814 MAC_MI_STAT_10MBPS_MODE |
1815 MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
1816 else
1817 tw32(MAC_MI_STAT, MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
1818 }
1819
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001820 if (phydev->speed == SPEED_1000 && phydev->duplex == DUPLEX_HALF)
1821 tw32(MAC_TX_LENGTHS,
1822 ((2 << TX_LENGTHS_IPG_CRS_SHIFT) |
1823 (6 << TX_LENGTHS_IPG_SHIFT) |
1824 (0xff << TX_LENGTHS_SLOT_TIME_SHIFT)));
1825 else
1826 tw32(MAC_TX_LENGTHS,
1827 ((2 << TX_LENGTHS_IPG_CRS_SHIFT) |
1828 (6 << TX_LENGTHS_IPG_SHIFT) |
1829 (32 << TX_LENGTHS_SLOT_TIME_SHIFT)));
1830
Matt Carlson34655ad2012-02-22 12:35:18 +00001831 if (phydev->link != tp->old_link ||
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001832 phydev->speed != tp->link_config.active_speed ||
1833 phydev->duplex != tp->link_config.active_duplex ||
1834 oldflowctrl != tp->link_config.active_flowctrl)
Matt Carlsonc6cdf432010-04-05 10:19:26 +00001835 linkmesg = 1;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001836
Matt Carlson34655ad2012-02-22 12:35:18 +00001837 tp->old_link = phydev->link;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001838 tp->link_config.active_speed = phydev->speed;
1839 tp->link_config.active_duplex = phydev->duplex;
1840
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001841 spin_unlock_bh(&tp->lock);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001842
1843 if (linkmesg)
1844 tg3_link_report(tp);
1845}
1846
1847static int tg3_phy_init(struct tg3 *tp)
1848{
1849 struct phy_device *phydev;
1850
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001851 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED)
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001852 return 0;
1853
1854 /* Bring the PHY back to a known state. */
1855 tg3_bmcr_reset(tp);
1856
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001857 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001858
1859 /* Attach the MAC to the PHY. */
Kay Sieversfb28ad32008-11-10 13:55:14 -08001860 phydev = phy_connect(tp->dev, dev_name(&phydev->dev), tg3_adjust_link,
Matt Carlsona9daf362008-05-25 23:49:44 -07001861 phydev->dev_flags, phydev->interface);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001862 if (IS_ERR(phydev)) {
Matt Carlsonab96b242010-04-05 10:19:22 +00001863 dev_err(&tp->pdev->dev, "Could not attach to PHY\n");
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001864 return PTR_ERR(phydev);
1865 }
1866
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001867 /* Mask with MAC supported features. */
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001868 switch (phydev->interface) {
1869 case PHY_INTERFACE_MODE_GMII:
1870 case PHY_INTERFACE_MODE_RGMII:
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001871 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
Matt Carlson321d32a2008-11-21 17:22:19 -08001872 phydev->supported &= (PHY_GBIT_FEATURES |
1873 SUPPORTED_Pause |
1874 SUPPORTED_Asym_Pause);
1875 break;
1876 }
1877 /* fallthru */
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001878 case PHY_INTERFACE_MODE_MII:
1879 phydev->supported &= (PHY_BASIC_FEATURES |
1880 SUPPORTED_Pause |
1881 SUPPORTED_Asym_Pause);
1882 break;
1883 default:
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001884 phy_disconnect(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001885 return -EINVAL;
1886 }
1887
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001888 tp->phy_flags |= TG3_PHYFLG_IS_CONNECTED;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001889
1890 phydev->advertising = phydev->supported;
1891
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001892 return 0;
1893}
1894
1895static void tg3_phy_start(struct tg3 *tp)
1896{
1897 struct phy_device *phydev;
1898
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001899 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001900 return;
1901
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001902 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001903
Matt Carlson800960682010-08-02 11:26:06 +00001904 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) {
1905 tp->phy_flags &= ~TG3_PHYFLG_IS_LOW_POWER;
Matt Carlsonc6700ce2012-02-13 15:20:15 +00001906 phydev->speed = tp->link_config.speed;
1907 phydev->duplex = tp->link_config.duplex;
1908 phydev->autoneg = tp->link_config.autoneg;
1909 phydev->advertising = tp->link_config.advertising;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001910 }
1911
1912 phy_start(phydev);
1913
1914 phy_start_aneg(phydev);
1915}
1916
1917static void tg3_phy_stop(struct tg3 *tp)
1918{
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001919 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001920 return;
1921
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001922 phy_stop(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001923}
1924
1925static void tg3_phy_fini(struct tg3 *tp)
1926{
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001927 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001928 phy_disconnect(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001929 tp->phy_flags &= ~TG3_PHYFLG_IS_CONNECTED;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001930 }
1931}
1932
Matt Carlson941ec902011-08-19 13:58:23 +00001933static int tg3_phy_set_extloopbk(struct tg3 *tp)
1934{
1935 int err;
1936 u32 val;
1937
1938 if (tp->phy_flags & TG3_PHYFLG_IS_FET)
1939 return 0;
1940
1941 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
1942 /* Cannot do read-modify-write on 5401 */
1943 err = tg3_phy_auxctl_write(tp,
1944 MII_TG3_AUXCTL_SHDWSEL_AUXCTL,
1945 MII_TG3_AUXCTL_ACTL_EXTLOOPBK |
1946 0x4c20);
1947 goto done;
1948 }
1949
1950 err = tg3_phy_auxctl_read(tp,
1951 MII_TG3_AUXCTL_SHDWSEL_AUXCTL, &val);
1952 if (err)
1953 return err;
1954
1955 val |= MII_TG3_AUXCTL_ACTL_EXTLOOPBK;
1956 err = tg3_phy_auxctl_write(tp,
1957 MII_TG3_AUXCTL_SHDWSEL_AUXCTL, val);
1958
1959done:
1960 return err;
1961}
1962
Matt Carlson7f97a4b2009-08-25 10:10:03 +00001963static void tg3_phy_fet_toggle_apd(struct tg3 *tp, bool enable)
1964{
1965 u32 phytest;
1966
1967 if (!tg3_readphy(tp, MII_TG3_FET_TEST, &phytest)) {
1968 u32 phy;
1969
1970 tg3_writephy(tp, MII_TG3_FET_TEST,
1971 phytest | MII_TG3_FET_SHADOW_EN);
1972 if (!tg3_readphy(tp, MII_TG3_FET_SHDW_AUXSTAT2, &phy)) {
1973 if (enable)
1974 phy |= MII_TG3_FET_SHDW_AUXSTAT2_APD;
1975 else
1976 phy &= ~MII_TG3_FET_SHDW_AUXSTAT2_APD;
1977 tg3_writephy(tp, MII_TG3_FET_SHDW_AUXSTAT2, phy);
1978 }
1979 tg3_writephy(tp, MII_TG3_FET_TEST, phytest);
1980 }
1981}
1982
Matt Carlson6833c042008-11-21 17:18:59 -08001983static void tg3_phy_toggle_apd(struct tg3 *tp, bool enable)
1984{
1985 u32 reg;
1986
Joe Perches63c3a662011-04-26 08:12:10 +00001987 if (!tg3_flag(tp, 5705_PLUS) ||
1988 (tg3_flag(tp, 5717_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001989 (tp->phy_flags & TG3_PHYFLG_MII_SERDES)))
Matt Carlson6833c042008-11-21 17:18:59 -08001990 return;
1991
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001992 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Matt Carlson7f97a4b2009-08-25 10:10:03 +00001993 tg3_phy_fet_toggle_apd(tp, enable);
1994 return;
1995 }
1996
Matt Carlson6833c042008-11-21 17:18:59 -08001997 reg = MII_TG3_MISC_SHDW_WREN |
1998 MII_TG3_MISC_SHDW_SCR5_SEL |
1999 MII_TG3_MISC_SHDW_SCR5_LPED |
2000 MII_TG3_MISC_SHDW_SCR5_DLPTLM |
2001 MII_TG3_MISC_SHDW_SCR5_SDTL |
2002 MII_TG3_MISC_SHDW_SCR5_C125OE;
2003 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5784 || !enable)
2004 reg |= MII_TG3_MISC_SHDW_SCR5_DLLAPD;
2005
2006 tg3_writephy(tp, MII_TG3_MISC_SHDW, reg);
2007
2008
2009 reg = MII_TG3_MISC_SHDW_WREN |
2010 MII_TG3_MISC_SHDW_APD_SEL |
2011 MII_TG3_MISC_SHDW_APD_WKTM_84MS;
2012 if (enable)
2013 reg |= MII_TG3_MISC_SHDW_APD_ENABLE;
2014
2015 tg3_writephy(tp, MII_TG3_MISC_SHDW, reg);
2016}
2017
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002018static void tg3_phy_toggle_automdix(struct tg3 *tp, int enable)
2019{
2020 u32 phy;
2021
Joe Perches63c3a662011-04-26 08:12:10 +00002022 if (!tg3_flag(tp, 5705_PLUS) ||
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002023 (tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002024 return;
2025
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002026 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002027 u32 ephy;
2028
Matt Carlson535ef6e2009-08-25 10:09:36 +00002029 if (!tg3_readphy(tp, MII_TG3_FET_TEST, &ephy)) {
2030 u32 reg = MII_TG3_FET_SHDW_MISCCTRL;
2031
2032 tg3_writephy(tp, MII_TG3_FET_TEST,
2033 ephy | MII_TG3_FET_SHADOW_EN);
2034 if (!tg3_readphy(tp, reg, &phy)) {
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002035 if (enable)
Matt Carlson535ef6e2009-08-25 10:09:36 +00002036 phy |= MII_TG3_FET_SHDW_MISCCTRL_MDIX;
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002037 else
Matt Carlson535ef6e2009-08-25 10:09:36 +00002038 phy &= ~MII_TG3_FET_SHDW_MISCCTRL_MDIX;
2039 tg3_writephy(tp, reg, phy);
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002040 }
Matt Carlson535ef6e2009-08-25 10:09:36 +00002041 tg3_writephy(tp, MII_TG3_FET_TEST, ephy);
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002042 }
2043 } else {
Matt Carlson15ee95c2011-04-20 07:57:40 +00002044 int ret;
2045
2046 ret = tg3_phy_auxctl_read(tp,
2047 MII_TG3_AUXCTL_SHDWSEL_MISC, &phy);
2048 if (!ret) {
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002049 if (enable)
2050 phy |= MII_TG3_AUXCTL_MISC_FORCE_AMDIX;
2051 else
2052 phy &= ~MII_TG3_AUXCTL_MISC_FORCE_AMDIX;
Matt Carlsonb4bd2922011-04-20 07:57:41 +00002053 tg3_phy_auxctl_write(tp,
2054 MII_TG3_AUXCTL_SHDWSEL_MISC, phy);
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002055 }
2056 }
2057}
2058
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059static void tg3_phy_set_wirespeed(struct tg3 *tp)
2060{
Matt Carlson15ee95c2011-04-20 07:57:40 +00002061 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 u32 val;
2063
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002064 if (tp->phy_flags & TG3_PHYFLG_NO_ETH_WIRE_SPEED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065 return;
2066
Matt Carlson15ee95c2011-04-20 07:57:40 +00002067 ret = tg3_phy_auxctl_read(tp, MII_TG3_AUXCTL_SHDWSEL_MISC, &val);
2068 if (!ret)
Matt Carlsonb4bd2922011-04-20 07:57:41 +00002069 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_MISC,
2070 val | MII_TG3_AUXCTL_MISC_WIRESPD_EN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071}
2072
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002073static void tg3_phy_apply_otp(struct tg3 *tp)
2074{
2075 u32 otp, phy;
2076
2077 if (!tp->phy_otp)
2078 return;
2079
2080 otp = tp->phy_otp;
2081
Matt Carlson1d36ba42011-04-20 07:57:42 +00002082 if (TG3_PHY_AUXCTL_SMDSP_ENABLE(tp))
2083 return;
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002084
2085 phy = ((otp & TG3_OTP_AGCTGT_MASK) >> TG3_OTP_AGCTGT_SHIFT);
2086 phy |= MII_TG3_DSP_TAP1_AGCTGT_DFLT;
2087 tg3_phydsp_write(tp, MII_TG3_DSP_TAP1, phy);
2088
2089 phy = ((otp & TG3_OTP_HPFFLTR_MASK) >> TG3_OTP_HPFFLTR_SHIFT) |
2090 ((otp & TG3_OTP_HPFOVER_MASK) >> TG3_OTP_HPFOVER_SHIFT);
2091 tg3_phydsp_write(tp, MII_TG3_DSP_AADJ1CH0, phy);
2092
2093 phy = ((otp & TG3_OTP_LPFDIS_MASK) >> TG3_OTP_LPFDIS_SHIFT);
2094 phy |= MII_TG3_DSP_AADJ1CH3_ADCCKADJ;
2095 tg3_phydsp_write(tp, MII_TG3_DSP_AADJ1CH3, phy);
2096
2097 phy = ((otp & TG3_OTP_VDAC_MASK) >> TG3_OTP_VDAC_SHIFT);
2098 tg3_phydsp_write(tp, MII_TG3_DSP_EXP75, phy);
2099
2100 phy = ((otp & TG3_OTP_10BTAMP_MASK) >> TG3_OTP_10BTAMP_SHIFT);
2101 tg3_phydsp_write(tp, MII_TG3_DSP_EXP96, phy);
2102
2103 phy = ((otp & TG3_OTP_ROFF_MASK) >> TG3_OTP_ROFF_SHIFT) |
2104 ((otp & TG3_OTP_RCOFF_MASK) >> TG3_OTP_RCOFF_SHIFT);
2105 tg3_phydsp_write(tp, MII_TG3_DSP_EXP97, phy);
2106
Matt Carlson1d36ba42011-04-20 07:57:42 +00002107 TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002108}
2109
Matt Carlson52b02d02010-10-14 10:37:41 +00002110static void tg3_phy_eee_adjust(struct tg3 *tp, u32 current_link_up)
2111{
2112 u32 val;
2113
2114 if (!(tp->phy_flags & TG3_PHYFLG_EEE_CAP))
2115 return;
2116
2117 tp->setlpicnt = 0;
2118
2119 if (tp->link_config.autoneg == AUTONEG_ENABLE &&
2120 current_link_up == 1 &&
Matt Carlsona6b68da2010-12-06 08:28:52 +00002121 tp->link_config.active_duplex == DUPLEX_FULL &&
2122 (tp->link_config.active_speed == SPEED_100 ||
2123 tp->link_config.active_speed == SPEED_1000)) {
Matt Carlson52b02d02010-10-14 10:37:41 +00002124 u32 eeectl;
2125
2126 if (tp->link_config.active_speed == SPEED_1000)
2127 eeectl = TG3_CPMU_EEE_CTRL_EXIT_16_5_US;
2128 else
2129 eeectl = TG3_CPMU_EEE_CTRL_EXIT_36_US;
2130
2131 tw32(TG3_CPMU_EEE_CTRL, eeectl);
2132
Matt Carlson3110f5f52010-12-06 08:28:50 +00002133 tg3_phy_cl45_read(tp, MDIO_MMD_AN,
2134 TG3_CL45_D7_EEERES_STAT, &val);
Matt Carlson52b02d02010-10-14 10:37:41 +00002135
Matt Carlsonb0c59432011-05-19 12:12:48 +00002136 if (val == TG3_CL45_D7_EEERES_STAT_LP_1000T ||
2137 val == TG3_CL45_D7_EEERES_STAT_LP_100TX)
Matt Carlson52b02d02010-10-14 10:37:41 +00002138 tp->setlpicnt = 2;
2139 }
2140
2141 if (!tp->setlpicnt) {
Matt Carlsonb715ce92011-07-20 10:20:52 +00002142 if (current_link_up == 1 &&
2143 !TG3_PHY_AUXCTL_SMDSP_ENABLE(tp)) {
2144 tg3_phydsp_write(tp, MII_TG3_DSP_TAP26, 0x0000);
2145 TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
2146 }
2147
Matt Carlson52b02d02010-10-14 10:37:41 +00002148 val = tr32(TG3_CPMU_EEE_MODE);
2149 tw32(TG3_CPMU_EEE_MODE, val & ~TG3_CPMU_EEEMD_LPI_ENABLE);
2150 }
2151}
2152
Matt Carlsonb0c59432011-05-19 12:12:48 +00002153static void tg3_phy_eee_enable(struct tg3 *tp)
2154{
2155 u32 val;
2156
2157 if (tp->link_config.active_speed == SPEED_1000 &&
2158 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
2159 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
Matt Carlson55086ad2011-12-14 11:09:59 +00002160 tg3_flag(tp, 57765_CLASS)) &&
Matt Carlsonb0c59432011-05-19 12:12:48 +00002161 !TG3_PHY_AUXCTL_SMDSP_ENABLE(tp)) {
Matt Carlsonb715ce92011-07-20 10:20:52 +00002162 val = MII_TG3_DSP_TAP26_ALNOKO |
2163 MII_TG3_DSP_TAP26_RMRXSTO;
2164 tg3_phydsp_write(tp, MII_TG3_DSP_TAP26, val);
Matt Carlsonb0c59432011-05-19 12:12:48 +00002165 TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
2166 }
2167
2168 val = tr32(TG3_CPMU_EEE_MODE);
2169 tw32(TG3_CPMU_EEE_MODE, val | TG3_CPMU_EEEMD_LPI_ENABLE);
2170}
2171
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172static int tg3_wait_macro_done(struct tg3 *tp)
2173{
2174 int limit = 100;
2175
2176 while (limit--) {
2177 u32 tmp32;
2178
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002179 if (!tg3_readphy(tp, MII_TG3_DSP_CONTROL, &tmp32)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180 if ((tmp32 & 0x1000) == 0)
2181 break;
2182 }
2183 }
Roel Kluind4675b52009-02-12 16:33:27 -08002184 if (limit < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185 return -EBUSY;
2186
2187 return 0;
2188}
2189
2190static int tg3_phy_write_and_check_testpat(struct tg3 *tp, int *resetp)
2191{
2192 static const u32 test_pat[4][6] = {
2193 { 0x00005555, 0x00000005, 0x00002aaa, 0x0000000a, 0x00003456, 0x00000003 },
2194 { 0x00002aaa, 0x0000000a, 0x00003333, 0x00000003, 0x0000789a, 0x00000005 },
2195 { 0x00005a5a, 0x00000005, 0x00002a6a, 0x0000000a, 0x00001bcd, 0x00000003 },
2196 { 0x00002a5a, 0x0000000a, 0x000033c3, 0x00000003, 0x00002ef1, 0x00000005 }
2197 };
2198 int chan;
2199
2200 for (chan = 0; chan < 4; chan++) {
2201 int i;
2202
2203 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
2204 (chan * 0x2000) | 0x0200);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002205 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0002);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206
2207 for (i = 0; i < 6; i++)
2208 tg3_writephy(tp, MII_TG3_DSP_RW_PORT,
2209 test_pat[chan][i]);
2210
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002211 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0202);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212 if (tg3_wait_macro_done(tp)) {
2213 *resetp = 1;
2214 return -EBUSY;
2215 }
2216
2217 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
2218 (chan * 0x2000) | 0x0200);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002219 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0082);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220 if (tg3_wait_macro_done(tp)) {
2221 *resetp = 1;
2222 return -EBUSY;
2223 }
2224
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002225 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0802);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226 if (tg3_wait_macro_done(tp)) {
2227 *resetp = 1;
2228 return -EBUSY;
2229 }
2230
2231 for (i = 0; i < 6; i += 2) {
2232 u32 low, high;
2233
2234 if (tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &low) ||
2235 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &high) ||
2236 tg3_wait_macro_done(tp)) {
2237 *resetp = 1;
2238 return -EBUSY;
2239 }
2240 low &= 0x7fff;
2241 high &= 0x000f;
2242 if (low != test_pat[chan][i] ||
2243 high != test_pat[chan][i+1]) {
2244 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x000b);
2245 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x4001);
2246 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x4005);
2247
2248 return -EBUSY;
2249 }
2250 }
2251 }
2252
2253 return 0;
2254}
2255
2256static int tg3_phy_reset_chanpat(struct tg3 *tp)
2257{
2258 int chan;
2259
2260 for (chan = 0; chan < 4; chan++) {
2261 int i;
2262
2263 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
2264 (chan * 0x2000) | 0x0200);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002265 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0002);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266 for (i = 0; i < 6; i++)
2267 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x000);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002268 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0202);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269 if (tg3_wait_macro_done(tp))
2270 return -EBUSY;
2271 }
2272
2273 return 0;
2274}
2275
2276static int tg3_phy_reset_5703_4_5(struct tg3 *tp)
2277{
2278 u32 reg32, phy9_orig;
2279 int retries, do_phy_reset, err;
2280
2281 retries = 10;
2282 do_phy_reset = 1;
2283 do {
2284 if (do_phy_reset) {
2285 err = tg3_bmcr_reset(tp);
2286 if (err)
2287 return err;
2288 do_phy_reset = 0;
2289 }
2290
2291 /* Disable transmitter and interrupt. */
2292 if (tg3_readphy(tp, MII_TG3_EXT_CTRL, &reg32))
2293 continue;
2294
2295 reg32 |= 0x3000;
2296 tg3_writephy(tp, MII_TG3_EXT_CTRL, reg32);
2297
2298 /* Set full-duplex, 1000 mbps. */
2299 tg3_writephy(tp, MII_BMCR,
Matt Carlson221c5632011-06-13 13:39:01 +00002300 BMCR_FULLDPLX | BMCR_SPEED1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301
2302 /* Set to master mode. */
Matt Carlson221c5632011-06-13 13:39:01 +00002303 if (tg3_readphy(tp, MII_CTRL1000, &phy9_orig))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304 continue;
2305
Matt Carlson221c5632011-06-13 13:39:01 +00002306 tg3_writephy(tp, MII_CTRL1000,
2307 CTL1000_AS_MASTER | CTL1000_ENABLE_MASTER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308
Matt Carlson1d36ba42011-04-20 07:57:42 +00002309 err = TG3_PHY_AUXCTL_SMDSP_ENABLE(tp);
2310 if (err)
2311 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312
2313 /* Block the PHY control access. */
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00002314 tg3_phydsp_write(tp, 0x8005, 0x0800);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315
2316 err = tg3_phy_write_and_check_testpat(tp, &do_phy_reset);
2317 if (!err)
2318 break;
2319 } while (--retries);
2320
2321 err = tg3_phy_reset_chanpat(tp);
2322 if (err)
2323 return err;
2324
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00002325 tg3_phydsp_write(tp, 0x8005, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326
2327 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x8200);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002328 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329
Matt Carlson1d36ba42011-04-20 07:57:42 +00002330 TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331
Matt Carlson221c5632011-06-13 13:39:01 +00002332 tg3_writephy(tp, MII_CTRL1000, phy9_orig);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333
2334 if (!tg3_readphy(tp, MII_TG3_EXT_CTRL, &reg32)) {
2335 reg32 &= ~0x3000;
2336 tg3_writephy(tp, MII_TG3_EXT_CTRL, reg32);
2337 } else if (!err)
2338 err = -EBUSY;
2339
2340 return err;
2341}
2342
2343/* This will reset the tigon3 PHY if there is no valid
2344 * link unless the FORCE argument is non-zero.
2345 */
2346static int tg3_phy_reset(struct tg3 *tp)
2347{
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002348 u32 val, cpmuctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349 int err;
2350
Michael Chan60189dd2006-12-17 17:08:07 -08002351 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan60189dd2006-12-17 17:08:07 -08002352 val = tr32(GRC_MISC_CFG);
2353 tw32_f(GRC_MISC_CFG, val & ~GRC_MISC_CFG_EPHY_IDDQ);
2354 udelay(40);
2355 }
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002356 err = tg3_readphy(tp, MII_BMSR, &val);
2357 err |= tg3_readphy(tp, MII_BMSR, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358 if (err != 0)
2359 return -EBUSY;
2360
Michael Chanc8e1e822006-04-29 18:55:17 -07002361 if (netif_running(tp->dev) && netif_carrier_ok(tp->dev)) {
2362 netif_carrier_off(tp->dev);
2363 tg3_link_report(tp);
2364 }
2365
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
2367 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
2368 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
2369 err = tg3_phy_reset_5703_4_5(tp);
2370 if (err)
2371 return err;
2372 goto out;
2373 }
2374
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002375 cpmuctrl = 0;
2376 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
2377 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX) {
2378 cpmuctrl = tr32(TG3_CPMU_CTRL);
2379 if (cpmuctrl & CPMU_CTRL_GPHY_10MB_RXONLY)
2380 tw32(TG3_CPMU_CTRL,
2381 cpmuctrl & ~CPMU_CTRL_GPHY_10MB_RXONLY);
2382 }
2383
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384 err = tg3_bmcr_reset(tp);
2385 if (err)
2386 return err;
2387
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002388 if (cpmuctrl & CPMU_CTRL_GPHY_10MB_RXONLY) {
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002389 val = MII_TG3_DSP_EXP8_AEDW | MII_TG3_DSP_EXP8_REJ2MHz;
2390 tg3_phydsp_write(tp, MII_TG3_DSP_EXP8, val);
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002391
2392 tw32(TG3_CPMU_CTRL, cpmuctrl);
2393 }
2394
Matt Carlsonbcb37f62008-11-03 16:52:09 -08002395 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX ||
2396 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5761_AX) {
Matt Carlsonce057f02007-11-12 21:08:03 -08002397 val = tr32(TG3_CPMU_LSPD_1000MB_CLK);
2398 if ((val & CPMU_LSPD_1000MB_MACCLK_MASK) ==
2399 CPMU_LSPD_1000MB_MACCLK_12_5) {
2400 val &= ~CPMU_LSPD_1000MB_MACCLK_MASK;
2401 udelay(40);
2402 tw32_f(TG3_CPMU_LSPD_1000MB_CLK, val);
2403 }
2404 }
2405
Joe Perches63c3a662011-04-26 08:12:10 +00002406 if (tg3_flag(tp, 5717_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002407 (tp->phy_flags & TG3_PHYFLG_MII_SERDES))
Matt Carlsonecf14102010-01-20 16:58:05 +00002408 return 0;
2409
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002410 tg3_phy_apply_otp(tp);
2411
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002412 if (tp->phy_flags & TG3_PHYFLG_ENABLE_APD)
Matt Carlson6833c042008-11-21 17:18:59 -08002413 tg3_phy_toggle_apd(tp, true);
2414 else
2415 tg3_phy_toggle_apd(tp, false);
2416
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417out:
Matt Carlson1d36ba42011-04-20 07:57:42 +00002418 if ((tp->phy_flags & TG3_PHYFLG_ADC_BUG) &&
2419 !TG3_PHY_AUXCTL_SMDSP_ENABLE(tp)) {
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00002420 tg3_phydsp_write(tp, 0x201f, 0x2aaa);
2421 tg3_phydsp_write(tp, 0x000a, 0x0323);
Matt Carlson1d36ba42011-04-20 07:57:42 +00002422 TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423 }
Matt Carlson1d36ba42011-04-20 07:57:42 +00002424
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002425 if (tp->phy_flags & TG3_PHYFLG_5704_A0_BUG) {
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002426 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8d68);
2427 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8d68);
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_BER_BUG) {
Matt Carlson1d36ba42011-04-20 07:57:42 +00002431 if (!TG3_PHY_AUXCTL_SMDSP_ENABLE(tp)) {
2432 tg3_phydsp_write(tp, 0x000a, 0x310b);
2433 tg3_phydsp_write(tp, 0x201f, 0x9506);
2434 tg3_phydsp_write(tp, 0x401f, 0x14e2);
2435 TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
2436 }
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002437 } else if (tp->phy_flags & TG3_PHYFLG_JITTER_BUG) {
Matt Carlson1d36ba42011-04-20 07:57:42 +00002438 if (!TG3_PHY_AUXCTL_SMDSP_ENABLE(tp)) {
2439 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x000a);
2440 if (tp->phy_flags & TG3_PHYFLG_ADJUST_TRIM) {
2441 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x110b);
2442 tg3_writephy(tp, MII_TG3_TEST1,
2443 MII_TG3_TEST1_TRIM_EN | 0x4);
2444 } else
2445 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x010b);
2446
2447 TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
2448 }
Michael Chanc424cb22006-04-29 18:56:34 -07002449 }
Matt Carlson1d36ba42011-04-20 07:57:42 +00002450
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451 /* Set Extended packet length bit (bit 14) on all chips that */
2452 /* support jumbo frames */
Matt Carlson79eb6902010-02-17 15:17:03 +00002453 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454 /* Cannot do read-modify-write on 5401 */
Matt Carlsonb4bd2922011-04-20 07:57:41 +00002455 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_AUXCTL, 0x4c20);
Joe Perches63c3a662011-04-26 08:12:10 +00002456 } else if (tg3_flag(tp, JUMBO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457 /* Set bit 14 with read-modify-write to preserve other bits */
Matt Carlson15ee95c2011-04-20 07:57:40 +00002458 err = tg3_phy_auxctl_read(tp,
2459 MII_TG3_AUXCTL_SHDWSEL_AUXCTL, &val);
2460 if (!err)
Matt Carlsonb4bd2922011-04-20 07:57:41 +00002461 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_AUXCTL,
2462 val | MII_TG3_AUXCTL_ACTL_EXTPKTLEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463 }
2464
2465 /* Set phy register 0x10 bit 0 to high fifo elasticity to support
2466 * jumbo frames transmission.
2467 */
Joe Perches63c3a662011-04-26 08:12:10 +00002468 if (tg3_flag(tp, JUMBO_CAPABLE)) {
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002469 if (!tg3_readphy(tp, MII_TG3_EXT_CTRL, &val))
Matt Carlsonc6cdf432010-04-05 10:19:26 +00002470 tg3_writephy(tp, MII_TG3_EXT_CTRL,
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002471 val | MII_TG3_EXT_CTRL_FIFO_ELASTIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472 }
2473
Michael Chan715116a2006-09-27 16:09:25 -07002474 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan715116a2006-09-27 16:09:25 -07002475 /* adjust output voltage */
Matt Carlson535ef6e2009-08-25 10:09:36 +00002476 tg3_writephy(tp, MII_TG3_FET_PTEST, 0x12);
Michael Chan715116a2006-09-27 16:09:25 -07002477 }
2478
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002479 tg3_phy_toggle_automdix(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480 tg3_phy_set_wirespeed(tp);
2481 return 0;
2482}
2483
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002484#define TG3_GPIO_MSG_DRVR_PRES 0x00000001
2485#define TG3_GPIO_MSG_NEED_VAUX 0x00000002
2486#define TG3_GPIO_MSG_MASK (TG3_GPIO_MSG_DRVR_PRES | \
2487 TG3_GPIO_MSG_NEED_VAUX)
2488#define TG3_GPIO_MSG_ALL_DRVR_PRES_MASK \
2489 ((TG3_GPIO_MSG_DRVR_PRES << 0) | \
2490 (TG3_GPIO_MSG_DRVR_PRES << 4) | \
2491 (TG3_GPIO_MSG_DRVR_PRES << 8) | \
2492 (TG3_GPIO_MSG_DRVR_PRES << 12))
2493
2494#define TG3_GPIO_MSG_ALL_NEED_VAUX_MASK \
2495 ((TG3_GPIO_MSG_NEED_VAUX << 0) | \
2496 (TG3_GPIO_MSG_NEED_VAUX << 4) | \
2497 (TG3_GPIO_MSG_NEED_VAUX << 8) | \
2498 (TG3_GPIO_MSG_NEED_VAUX << 12))
2499
2500static inline u32 tg3_set_function_status(struct tg3 *tp, u32 newstat)
2501{
2502 u32 status, shift;
2503
2504 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
2505 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
2506 status = tg3_ape_read32(tp, TG3_APE_GPIO_MSG);
2507 else
2508 status = tr32(TG3_CPMU_DRV_STATUS);
2509
2510 shift = TG3_APE_GPIO_MSG_SHIFT + 4 * tp->pci_fn;
2511 status &= ~(TG3_GPIO_MSG_MASK << shift);
2512 status |= (newstat << shift);
2513
2514 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
2515 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
2516 tg3_ape_write32(tp, TG3_APE_GPIO_MSG, status);
2517 else
2518 tw32(TG3_CPMU_DRV_STATUS, status);
2519
2520 return status >> TG3_APE_GPIO_MSG_SHIFT;
2521}
2522
Matt Carlson520b2752011-06-13 13:39:02 +00002523static inline int tg3_pwrsrc_switch_to_vmain(struct tg3 *tp)
2524{
2525 if (!tg3_flag(tp, IS_NIC))
2526 return 0;
2527
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002528 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
2529 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
2530 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
2531 if (tg3_ape_lock(tp, TG3_APE_LOCK_GPIO))
2532 return -EIO;
Matt Carlson520b2752011-06-13 13:39:02 +00002533
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002534 tg3_set_function_status(tp, TG3_GPIO_MSG_DRVR_PRES);
2535
2536 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl,
2537 TG3_GRC_LCLCTL_PWRSW_DELAY);
2538
2539 tg3_ape_unlock(tp, TG3_APE_LOCK_GPIO);
2540 } else {
2541 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl,
2542 TG3_GRC_LCLCTL_PWRSW_DELAY);
2543 }
Matt Carlson6f5c8f832011-07-13 09:27:31 +00002544
Matt Carlson520b2752011-06-13 13:39:02 +00002545 return 0;
2546}
2547
2548static void tg3_pwrsrc_die_with_vmain(struct tg3 *tp)
2549{
2550 u32 grc_local_ctrl;
2551
2552 if (!tg3_flag(tp, IS_NIC) ||
2553 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2554 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)
2555 return;
2556
2557 grc_local_ctrl = tp->grc_local_ctrl | GRC_LCLCTRL_GPIO_OE1;
2558
2559 tw32_wait_f(GRC_LOCAL_CTRL,
2560 grc_local_ctrl | GRC_LCLCTRL_GPIO_OUTPUT1,
2561 TG3_GRC_LCLCTL_PWRSW_DELAY);
2562
2563 tw32_wait_f(GRC_LOCAL_CTRL,
2564 grc_local_ctrl,
2565 TG3_GRC_LCLCTL_PWRSW_DELAY);
2566
2567 tw32_wait_f(GRC_LOCAL_CTRL,
2568 grc_local_ctrl | GRC_LCLCTRL_GPIO_OUTPUT1,
2569 TG3_GRC_LCLCTL_PWRSW_DELAY);
2570}
2571
2572static void tg3_pwrsrc_switch_to_vaux(struct tg3 *tp)
2573{
2574 if (!tg3_flag(tp, IS_NIC))
2575 return;
2576
2577 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2578 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
2579 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2580 (GRC_LCLCTRL_GPIO_OE0 |
2581 GRC_LCLCTRL_GPIO_OE1 |
2582 GRC_LCLCTRL_GPIO_OE2 |
2583 GRC_LCLCTRL_GPIO_OUTPUT0 |
2584 GRC_LCLCTRL_GPIO_OUTPUT1),
2585 TG3_GRC_LCLCTL_PWRSW_DELAY);
2586 } else if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761 ||
2587 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761S) {
2588 /* The 5761 non-e device swaps GPIO 0 and GPIO 2. */
2589 u32 grc_local_ctrl = GRC_LCLCTRL_GPIO_OE0 |
2590 GRC_LCLCTRL_GPIO_OE1 |
2591 GRC_LCLCTRL_GPIO_OE2 |
2592 GRC_LCLCTRL_GPIO_OUTPUT0 |
2593 GRC_LCLCTRL_GPIO_OUTPUT1 |
2594 tp->grc_local_ctrl;
2595 tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl,
2596 TG3_GRC_LCLCTL_PWRSW_DELAY);
2597
2598 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OUTPUT2;
2599 tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl,
2600 TG3_GRC_LCLCTL_PWRSW_DELAY);
2601
2602 grc_local_ctrl &= ~GRC_LCLCTRL_GPIO_OUTPUT0;
2603 tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl,
2604 TG3_GRC_LCLCTL_PWRSW_DELAY);
2605 } else {
2606 u32 no_gpio2;
2607 u32 grc_local_ctrl = 0;
2608
2609 /* Workaround to prevent overdrawing Amps. */
2610 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
2611 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE3;
2612 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2613 grc_local_ctrl,
2614 TG3_GRC_LCLCTL_PWRSW_DELAY);
2615 }
2616
2617 /* On 5753 and variants, GPIO2 cannot be used. */
2618 no_gpio2 = tp->nic_sram_data_cfg &
2619 NIC_SRAM_DATA_CFG_NO_GPIO2;
2620
2621 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE0 |
2622 GRC_LCLCTRL_GPIO_OE1 |
2623 GRC_LCLCTRL_GPIO_OE2 |
2624 GRC_LCLCTRL_GPIO_OUTPUT1 |
2625 GRC_LCLCTRL_GPIO_OUTPUT2;
2626 if (no_gpio2) {
2627 grc_local_ctrl &= ~(GRC_LCLCTRL_GPIO_OE2 |
2628 GRC_LCLCTRL_GPIO_OUTPUT2);
2629 }
2630 tw32_wait_f(GRC_LOCAL_CTRL,
2631 tp->grc_local_ctrl | grc_local_ctrl,
2632 TG3_GRC_LCLCTL_PWRSW_DELAY);
2633
2634 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OUTPUT0;
2635
2636 tw32_wait_f(GRC_LOCAL_CTRL,
2637 tp->grc_local_ctrl | grc_local_ctrl,
2638 TG3_GRC_LCLCTL_PWRSW_DELAY);
2639
2640 if (!no_gpio2) {
2641 grc_local_ctrl &= ~GRC_LCLCTRL_GPIO_OUTPUT2;
2642 tw32_wait_f(GRC_LOCAL_CTRL,
2643 tp->grc_local_ctrl | grc_local_ctrl,
2644 TG3_GRC_LCLCTL_PWRSW_DELAY);
2645 }
2646 }
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002647}
Matt Carlson6f5c8f832011-07-13 09:27:31 +00002648
Matt Carlsoncd0d7222011-07-13 09:27:33 +00002649static void tg3_frob_aux_power_5717(struct tg3 *tp, bool wol_enable)
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002650{
2651 u32 msg = 0;
2652
2653 /* Serialize power state transitions */
2654 if (tg3_ape_lock(tp, TG3_APE_LOCK_GPIO))
2655 return;
2656
Matt Carlsoncd0d7222011-07-13 09:27:33 +00002657 if (tg3_flag(tp, ENABLE_ASF) || tg3_flag(tp, ENABLE_APE) || wol_enable)
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002658 msg = TG3_GPIO_MSG_NEED_VAUX;
2659
2660 msg = tg3_set_function_status(tp, msg);
2661
2662 if (msg & TG3_GPIO_MSG_ALL_DRVR_PRES_MASK)
2663 goto done;
2664
2665 if (msg & TG3_GPIO_MSG_ALL_NEED_VAUX_MASK)
2666 tg3_pwrsrc_switch_to_vaux(tp);
2667 else
2668 tg3_pwrsrc_die_with_vmain(tp);
2669
2670done:
Matt Carlson6f5c8f832011-07-13 09:27:31 +00002671 tg3_ape_unlock(tp, TG3_APE_LOCK_GPIO);
Matt Carlson520b2752011-06-13 13:39:02 +00002672}
2673
Matt Carlsoncd0d7222011-07-13 09:27:33 +00002674static void tg3_frob_aux_power(struct tg3 *tp, bool include_wol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675{
Matt Carlson683644b2011-03-09 16:58:23 +00002676 bool need_vaux = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002677
Matt Carlson334355a2010-01-20 16:58:10 +00002678 /* The GPIOs do something completely different on 57765. */
Matt Carlson55086ad2011-12-14 11:09:59 +00002679 if (!tg3_flag(tp, IS_NIC) || tg3_flag(tp, 57765_CLASS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680 return;
2681
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002682 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
2683 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
2684 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
Matt Carlsoncd0d7222011-07-13 09:27:33 +00002685 tg3_frob_aux_power_5717(tp, include_wol ?
2686 tg3_flag(tp, WOL_ENABLE) != 0 : 0);
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002687 return;
2688 }
2689
2690 if (tp->pdev_peer && tp->pdev_peer != tp->pdev) {
Michael Chan8c2dc7e2005-12-19 16:26:02 -08002691 struct net_device *dev_peer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692
Michael Chan8c2dc7e2005-12-19 16:26:02 -08002693 dev_peer = pci_get_drvdata(tp->pdev_peer);
Matt Carlson683644b2011-03-09 16:58:23 +00002694
Michael Chanbc1c7562006-03-20 17:48:03 -08002695 /* remove_one() may have been run on the peer. */
Matt Carlson683644b2011-03-09 16:58:23 +00002696 if (dev_peer) {
2697 struct tg3 *tp_peer = netdev_priv(dev_peer);
2698
Joe Perches63c3a662011-04-26 08:12:10 +00002699 if (tg3_flag(tp_peer, INIT_COMPLETE))
Matt Carlson683644b2011-03-09 16:58:23 +00002700 return;
2701
Matt Carlsoncd0d7222011-07-13 09:27:33 +00002702 if ((include_wol && tg3_flag(tp_peer, WOL_ENABLE)) ||
Joe Perches63c3a662011-04-26 08:12:10 +00002703 tg3_flag(tp_peer, ENABLE_ASF))
Matt Carlson683644b2011-03-09 16:58:23 +00002704 need_vaux = true;
2705 }
Michael Chan8c2dc7e2005-12-19 16:26:02 -08002706 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707
Matt Carlsoncd0d7222011-07-13 09:27:33 +00002708 if ((include_wol && tg3_flag(tp, WOL_ENABLE)) ||
2709 tg3_flag(tp, ENABLE_ASF))
Matt Carlson683644b2011-03-09 16:58:23 +00002710 need_vaux = true;
2711
Matt Carlson520b2752011-06-13 13:39:02 +00002712 if (need_vaux)
2713 tg3_pwrsrc_switch_to_vaux(tp);
2714 else
2715 tg3_pwrsrc_die_with_vmain(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002716}
2717
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07002718static int tg3_5700_link_polarity(struct tg3 *tp, u32 speed)
2719{
2720 if (tp->led_ctrl == LED_CTRL_MODE_PHY_2)
2721 return 1;
Matt Carlson79eb6902010-02-17 15:17:03 +00002722 else if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5411) {
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07002723 if (speed != SPEED_10)
2724 return 1;
2725 } else if (speed == SPEED_10)
2726 return 1;
2727
2728 return 0;
2729}
2730
Matt Carlson0a459aa2008-11-03 16:54:15 -08002731static void tg3_power_down_phy(struct tg3 *tp, bool do_low_power)
Michael Chan15c3b692006-03-22 01:06:52 -08002732{
Matt Carlsonce057f02007-11-12 21:08:03 -08002733 u32 val;
2734
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002735 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Michael Chan51297242007-02-13 12:17:57 -08002736 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
2737 u32 sg_dig_ctrl = tr32(SG_DIG_CTRL);
2738 u32 serdes_cfg = tr32(MAC_SERDES_CFG);
2739
2740 sg_dig_ctrl |=
2741 SG_DIG_USING_HW_AUTONEG | SG_DIG_SOFT_RESET;
2742 tw32(SG_DIG_CTRL, sg_dig_ctrl);
2743 tw32(MAC_SERDES_CFG, serdes_cfg | (1 << 15));
2744 }
Michael Chan3f7045c2006-09-27 16:02:29 -07002745 return;
Michael Chan51297242007-02-13 12:17:57 -08002746 }
Michael Chan3f7045c2006-09-27 16:02:29 -07002747
Michael Chan60189dd2006-12-17 17:08:07 -08002748 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan60189dd2006-12-17 17:08:07 -08002749 tg3_bmcr_reset(tp);
2750 val = tr32(GRC_MISC_CFG);
2751 tw32_f(GRC_MISC_CFG, val | GRC_MISC_CFG_EPHY_IDDQ);
2752 udelay(40);
2753 return;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002754 } else if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Matt Carlson0e5f7842009-11-02 14:26:38 +00002755 u32 phytest;
2756 if (!tg3_readphy(tp, MII_TG3_FET_TEST, &phytest)) {
2757 u32 phy;
2758
2759 tg3_writephy(tp, MII_ADVERTISE, 0);
2760 tg3_writephy(tp, MII_BMCR,
2761 BMCR_ANENABLE | BMCR_ANRESTART);
2762
2763 tg3_writephy(tp, MII_TG3_FET_TEST,
2764 phytest | MII_TG3_FET_SHADOW_EN);
2765 if (!tg3_readphy(tp, MII_TG3_FET_SHDW_AUXMODE4, &phy)) {
2766 phy |= MII_TG3_FET_SHDW_AUXMODE4_SBPD;
2767 tg3_writephy(tp,
2768 MII_TG3_FET_SHDW_AUXMODE4,
2769 phy);
2770 }
2771 tg3_writephy(tp, MII_TG3_FET_TEST, phytest);
2772 }
2773 return;
Matt Carlson0a459aa2008-11-03 16:54:15 -08002774 } else if (do_low_power) {
Michael Chan715116a2006-09-27 16:09:25 -07002775 tg3_writephy(tp, MII_TG3_EXT_CTRL,
2776 MII_TG3_EXT_CTRL_FORCE_LED_OFF);
Matt Carlson0a459aa2008-11-03 16:54:15 -08002777
Matt Carlsonb4bd2922011-04-20 07:57:41 +00002778 val = MII_TG3_AUXCTL_PCTL_100TX_LPWR |
2779 MII_TG3_AUXCTL_PCTL_SPR_ISOLATE |
2780 MII_TG3_AUXCTL_PCTL_VREG_11V;
2781 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_PWRCTL, val);
Michael Chan715116a2006-09-27 16:09:25 -07002782 }
Michael Chan3f7045c2006-09-27 16:02:29 -07002783
Michael Chan15c3b692006-03-22 01:06:52 -08002784 /* The PHY should not be powered down on some chips because
2785 * of bugs.
2786 */
2787 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2788 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
2789 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780 &&
Matt Carlson085f1af2012-04-02 09:01:40 +00002790 (tp->phy_flags & TG3_PHYFLG_MII_SERDES)) ||
2791 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 &&
2792 !tp->pci_fn))
Michael Chan15c3b692006-03-22 01:06:52 -08002793 return;
Matt Carlsonce057f02007-11-12 21:08:03 -08002794
Matt Carlsonbcb37f62008-11-03 16:52:09 -08002795 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX ||
2796 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5761_AX) {
Matt Carlsonce057f02007-11-12 21:08:03 -08002797 val = tr32(TG3_CPMU_LSPD_1000MB_CLK);
2798 val &= ~CPMU_LSPD_1000MB_MACCLK_MASK;
2799 val |= CPMU_LSPD_1000MB_MACCLK_12_5;
2800 tw32_f(TG3_CPMU_LSPD_1000MB_CLK, val);
2801 }
2802
Michael Chan15c3b692006-03-22 01:06:52 -08002803 tg3_writephy(tp, MII_BMCR, BMCR_PDOWN);
2804}
2805
Matt Carlson3f007892008-11-03 16:51:36 -08002806/* tp->lock is held. */
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002807static int tg3_nvram_lock(struct tg3 *tp)
2808{
Joe Perches63c3a662011-04-26 08:12:10 +00002809 if (tg3_flag(tp, NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002810 int i;
2811
2812 if (tp->nvram_lock_cnt == 0) {
2813 tw32(NVRAM_SWARB, SWARB_REQ_SET1);
2814 for (i = 0; i < 8000; i++) {
2815 if (tr32(NVRAM_SWARB) & SWARB_GNT1)
2816 break;
2817 udelay(20);
2818 }
2819 if (i == 8000) {
2820 tw32(NVRAM_SWARB, SWARB_REQ_CLR1);
2821 return -ENODEV;
2822 }
2823 }
2824 tp->nvram_lock_cnt++;
2825 }
2826 return 0;
2827}
2828
2829/* tp->lock is held. */
2830static void tg3_nvram_unlock(struct tg3 *tp)
2831{
Joe Perches63c3a662011-04-26 08:12:10 +00002832 if (tg3_flag(tp, NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002833 if (tp->nvram_lock_cnt > 0)
2834 tp->nvram_lock_cnt--;
2835 if (tp->nvram_lock_cnt == 0)
2836 tw32_f(NVRAM_SWARB, SWARB_REQ_CLR1);
2837 }
2838}
2839
2840/* tp->lock is held. */
2841static void tg3_enable_nvram_access(struct tg3 *tp)
2842{
Joe Perches63c3a662011-04-26 08:12:10 +00002843 if (tg3_flag(tp, 5750_PLUS) && !tg3_flag(tp, PROTECTED_NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002844 u32 nvaccess = tr32(NVRAM_ACCESS);
2845
2846 tw32(NVRAM_ACCESS, nvaccess | ACCESS_ENABLE);
2847 }
2848}
2849
2850/* tp->lock is held. */
2851static void tg3_disable_nvram_access(struct tg3 *tp)
2852{
Joe Perches63c3a662011-04-26 08:12:10 +00002853 if (tg3_flag(tp, 5750_PLUS) && !tg3_flag(tp, PROTECTED_NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002854 u32 nvaccess = tr32(NVRAM_ACCESS);
2855
2856 tw32(NVRAM_ACCESS, nvaccess & ~ACCESS_ENABLE);
2857 }
2858}
2859
2860static int tg3_nvram_read_using_eeprom(struct tg3 *tp,
2861 u32 offset, u32 *val)
2862{
2863 u32 tmp;
2864 int i;
2865
2866 if (offset > EEPROM_ADDR_ADDR_MASK || (offset % 4) != 0)
2867 return -EINVAL;
2868
2869 tmp = tr32(GRC_EEPROM_ADDR) & ~(EEPROM_ADDR_ADDR_MASK |
2870 EEPROM_ADDR_DEVID_MASK |
2871 EEPROM_ADDR_READ);
2872 tw32(GRC_EEPROM_ADDR,
2873 tmp |
2874 (0 << EEPROM_ADDR_DEVID_SHIFT) |
2875 ((offset << EEPROM_ADDR_ADDR_SHIFT) &
2876 EEPROM_ADDR_ADDR_MASK) |
2877 EEPROM_ADDR_READ | EEPROM_ADDR_START);
2878
2879 for (i = 0; i < 1000; i++) {
2880 tmp = tr32(GRC_EEPROM_ADDR);
2881
2882 if (tmp & EEPROM_ADDR_COMPLETE)
2883 break;
2884 msleep(1);
2885 }
2886 if (!(tmp & EEPROM_ADDR_COMPLETE))
2887 return -EBUSY;
2888
Matt Carlson62cedd12009-04-20 14:52:29 -07002889 tmp = tr32(GRC_EEPROM_DATA);
2890
2891 /*
2892 * The data will always be opposite the native endian
2893 * format. Perform a blind byteswap to compensate.
2894 */
2895 *val = swab32(tmp);
2896
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002897 return 0;
2898}
2899
2900#define NVRAM_CMD_TIMEOUT 10000
2901
2902static int tg3_nvram_exec_cmd(struct tg3 *tp, u32 nvram_cmd)
2903{
2904 int i;
2905
2906 tw32(NVRAM_CMD, nvram_cmd);
2907 for (i = 0; i < NVRAM_CMD_TIMEOUT; i++) {
2908 udelay(10);
2909 if (tr32(NVRAM_CMD) & NVRAM_CMD_DONE) {
2910 udelay(10);
2911 break;
2912 }
2913 }
2914
2915 if (i == NVRAM_CMD_TIMEOUT)
2916 return -EBUSY;
2917
2918 return 0;
2919}
2920
2921static u32 tg3_nvram_phys_addr(struct tg3 *tp, u32 addr)
2922{
Joe Perches63c3a662011-04-26 08:12:10 +00002923 if (tg3_flag(tp, NVRAM) &&
2924 tg3_flag(tp, NVRAM_BUFFERED) &&
2925 tg3_flag(tp, FLASH) &&
2926 !tg3_flag(tp, NO_NVRAM_ADDR_TRANS) &&
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002927 (tp->nvram_jedecnum == JEDEC_ATMEL))
2928
2929 addr = ((addr / tp->nvram_pagesize) <<
2930 ATMEL_AT45DB0X1B_PAGE_POS) +
2931 (addr % tp->nvram_pagesize);
2932
2933 return addr;
2934}
2935
2936static u32 tg3_nvram_logical_addr(struct tg3 *tp, u32 addr)
2937{
Joe Perches63c3a662011-04-26 08:12:10 +00002938 if (tg3_flag(tp, NVRAM) &&
2939 tg3_flag(tp, NVRAM_BUFFERED) &&
2940 tg3_flag(tp, FLASH) &&
2941 !tg3_flag(tp, NO_NVRAM_ADDR_TRANS) &&
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002942 (tp->nvram_jedecnum == JEDEC_ATMEL))
2943
2944 addr = ((addr >> ATMEL_AT45DB0X1B_PAGE_POS) *
2945 tp->nvram_pagesize) +
2946 (addr & ((1 << ATMEL_AT45DB0X1B_PAGE_POS) - 1));
2947
2948 return addr;
2949}
2950
Matt Carlsone4f34112009-02-25 14:25:00 +00002951/* NOTE: Data read in from NVRAM is byteswapped according to
2952 * the byteswapping settings for all other register accesses.
2953 * tg3 devices are BE devices, so on a BE machine, the data
2954 * returned will be exactly as it is seen in NVRAM. On a LE
2955 * machine, the 32-bit value will be byteswapped.
2956 */
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002957static int tg3_nvram_read(struct tg3 *tp, u32 offset, u32 *val)
2958{
2959 int ret;
2960
Joe Perches63c3a662011-04-26 08:12:10 +00002961 if (!tg3_flag(tp, NVRAM))
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002962 return tg3_nvram_read_using_eeprom(tp, offset, val);
2963
2964 offset = tg3_nvram_phys_addr(tp, offset);
2965
2966 if (offset > NVRAM_ADDR_MSK)
2967 return -EINVAL;
2968
2969 ret = tg3_nvram_lock(tp);
2970 if (ret)
2971 return ret;
2972
2973 tg3_enable_nvram_access(tp);
2974
2975 tw32(NVRAM_ADDR, offset);
2976 ret = tg3_nvram_exec_cmd(tp, NVRAM_CMD_RD | NVRAM_CMD_GO |
2977 NVRAM_CMD_FIRST | NVRAM_CMD_LAST | NVRAM_CMD_DONE);
2978
2979 if (ret == 0)
Matt Carlsone4f34112009-02-25 14:25:00 +00002980 *val = tr32(NVRAM_RDDATA);
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002981
2982 tg3_disable_nvram_access(tp);
2983
2984 tg3_nvram_unlock(tp);
2985
2986 return ret;
2987}
2988
Matt Carlsona9dc5292009-02-25 14:25:30 +00002989/* Ensures NVRAM data is in bytestream format. */
2990static int tg3_nvram_read_be32(struct tg3 *tp, u32 offset, __be32 *val)
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002991{
2992 u32 v;
Matt Carlsona9dc5292009-02-25 14:25:30 +00002993 int res = tg3_nvram_read(tp, offset, &v);
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002994 if (!res)
Matt Carlsona9dc5292009-02-25 14:25:30 +00002995 *val = cpu_to_be32(v);
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002996 return res;
2997}
2998
Matt Carlsondbe9b922012-02-13 10:20:09 +00002999static int tg3_nvram_write_block_using_eeprom(struct tg3 *tp,
3000 u32 offset, u32 len, u8 *buf)
3001{
3002 int i, j, rc = 0;
3003 u32 val;
3004
3005 for (i = 0; i < len; i += 4) {
3006 u32 addr;
3007 __be32 data;
3008
3009 addr = offset + i;
3010
3011 memcpy(&data, buf + i, 4);
3012
3013 /*
3014 * The SEEPROM interface expects the data to always be opposite
3015 * the native endian format. We accomplish this by reversing
3016 * all the operations that would have been performed on the
3017 * data from a call to tg3_nvram_read_be32().
3018 */
3019 tw32(GRC_EEPROM_DATA, swab32(be32_to_cpu(data)));
3020
3021 val = tr32(GRC_EEPROM_ADDR);
3022 tw32(GRC_EEPROM_ADDR, val | EEPROM_ADDR_COMPLETE);
3023
3024 val &= ~(EEPROM_ADDR_ADDR_MASK | EEPROM_ADDR_DEVID_MASK |
3025 EEPROM_ADDR_READ);
3026 tw32(GRC_EEPROM_ADDR, val |
3027 (0 << EEPROM_ADDR_DEVID_SHIFT) |
3028 (addr & EEPROM_ADDR_ADDR_MASK) |
3029 EEPROM_ADDR_START |
3030 EEPROM_ADDR_WRITE);
3031
3032 for (j = 0; j < 1000; j++) {
3033 val = tr32(GRC_EEPROM_ADDR);
3034
3035 if (val & EEPROM_ADDR_COMPLETE)
3036 break;
3037 msleep(1);
3038 }
3039 if (!(val & EEPROM_ADDR_COMPLETE)) {
3040 rc = -EBUSY;
3041 break;
3042 }
3043 }
3044
3045 return rc;
3046}
3047
3048/* offset and length are dword aligned */
3049static int tg3_nvram_write_block_unbuffered(struct tg3 *tp, u32 offset, u32 len,
3050 u8 *buf)
3051{
3052 int ret = 0;
3053 u32 pagesize = tp->nvram_pagesize;
3054 u32 pagemask = pagesize - 1;
3055 u32 nvram_cmd;
3056 u8 *tmp;
3057
3058 tmp = kmalloc(pagesize, GFP_KERNEL);
3059 if (tmp == NULL)
3060 return -ENOMEM;
3061
3062 while (len) {
3063 int j;
3064 u32 phy_addr, page_off, size;
3065
3066 phy_addr = offset & ~pagemask;
3067
3068 for (j = 0; j < pagesize; j += 4) {
3069 ret = tg3_nvram_read_be32(tp, phy_addr + j,
3070 (__be32 *) (tmp + j));
3071 if (ret)
3072 break;
3073 }
3074 if (ret)
3075 break;
3076
3077 page_off = offset & pagemask;
3078 size = pagesize;
3079 if (len < size)
3080 size = len;
3081
3082 len -= size;
3083
3084 memcpy(tmp + page_off, buf, size);
3085
3086 offset = offset + (pagesize - page_off);
3087
3088 tg3_enable_nvram_access(tp);
3089
3090 /*
3091 * Before we can erase the flash page, we need
3092 * to issue a special "write enable" command.
3093 */
3094 nvram_cmd = NVRAM_CMD_WREN | NVRAM_CMD_GO | NVRAM_CMD_DONE;
3095
3096 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
3097 break;
3098
3099 /* Erase the target page */
3100 tw32(NVRAM_ADDR, phy_addr);
3101
3102 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE | NVRAM_CMD_WR |
3103 NVRAM_CMD_FIRST | NVRAM_CMD_LAST | NVRAM_CMD_ERASE;
3104
3105 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
3106 break;
3107
3108 /* Issue another write enable to start the write. */
3109 nvram_cmd = NVRAM_CMD_WREN | NVRAM_CMD_GO | NVRAM_CMD_DONE;
3110
3111 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
3112 break;
3113
3114 for (j = 0; j < pagesize; j += 4) {
3115 __be32 data;
3116
3117 data = *((__be32 *) (tmp + j));
3118
3119 tw32(NVRAM_WRDATA, be32_to_cpu(data));
3120
3121 tw32(NVRAM_ADDR, phy_addr + j);
3122
3123 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE |
3124 NVRAM_CMD_WR;
3125
3126 if (j == 0)
3127 nvram_cmd |= NVRAM_CMD_FIRST;
3128 else if (j == (pagesize - 4))
3129 nvram_cmd |= NVRAM_CMD_LAST;
3130
3131 ret = tg3_nvram_exec_cmd(tp, nvram_cmd);
3132 if (ret)
3133 break;
3134 }
3135 if (ret)
3136 break;
3137 }
3138
3139 nvram_cmd = NVRAM_CMD_WRDI | NVRAM_CMD_GO | NVRAM_CMD_DONE;
3140 tg3_nvram_exec_cmd(tp, nvram_cmd);
3141
3142 kfree(tmp);
3143
3144 return ret;
3145}
3146
3147/* offset and length are dword aligned */
3148static int tg3_nvram_write_block_buffered(struct tg3 *tp, u32 offset, u32 len,
3149 u8 *buf)
3150{
3151 int i, ret = 0;
3152
3153 for (i = 0; i < len; i += 4, offset += 4) {
3154 u32 page_off, phy_addr, nvram_cmd;
3155 __be32 data;
3156
3157 memcpy(&data, buf + i, 4);
3158 tw32(NVRAM_WRDATA, be32_to_cpu(data));
3159
3160 page_off = offset % tp->nvram_pagesize;
3161
3162 phy_addr = tg3_nvram_phys_addr(tp, offset);
3163
Matt Carlsondbe9b922012-02-13 10:20:09 +00003164 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE | NVRAM_CMD_WR;
3165
3166 if (page_off == 0 || i == 0)
3167 nvram_cmd |= NVRAM_CMD_FIRST;
3168 if (page_off == (tp->nvram_pagesize - 4))
3169 nvram_cmd |= NVRAM_CMD_LAST;
3170
3171 if (i == (len - 4))
3172 nvram_cmd |= NVRAM_CMD_LAST;
3173
Matt Carlson42278222012-02-13 15:20:11 +00003174 if ((nvram_cmd & NVRAM_CMD_FIRST) ||
3175 !tg3_flag(tp, FLASH) ||
3176 !tg3_flag(tp, 57765_PLUS))
3177 tw32(NVRAM_ADDR, phy_addr);
3178
Matt Carlsondbe9b922012-02-13 10:20:09 +00003179 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5752 &&
3180 !tg3_flag(tp, 5755_PLUS) &&
3181 (tp->nvram_jedecnum == JEDEC_ST) &&
3182 (nvram_cmd & NVRAM_CMD_FIRST)) {
3183 u32 cmd;
3184
3185 cmd = NVRAM_CMD_WREN | NVRAM_CMD_GO | NVRAM_CMD_DONE;
3186 ret = tg3_nvram_exec_cmd(tp, cmd);
3187 if (ret)
3188 break;
3189 }
3190 if (!tg3_flag(tp, FLASH)) {
3191 /* We always do complete word writes to eeprom. */
3192 nvram_cmd |= (NVRAM_CMD_FIRST | NVRAM_CMD_LAST);
3193 }
3194
3195 ret = tg3_nvram_exec_cmd(tp, nvram_cmd);
3196 if (ret)
3197 break;
3198 }
3199 return ret;
3200}
3201
3202/* offset and length are dword aligned */
3203static int tg3_nvram_write_block(struct tg3 *tp, u32 offset, u32 len, u8 *buf)
3204{
3205 int ret;
3206
3207 if (tg3_flag(tp, EEPROM_WRITE_PROT)) {
3208 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl &
3209 ~GRC_LCLCTRL_GPIO_OUTPUT1);
3210 udelay(40);
3211 }
3212
3213 if (!tg3_flag(tp, NVRAM)) {
3214 ret = tg3_nvram_write_block_using_eeprom(tp, offset, len, buf);
3215 } else {
3216 u32 grc_mode;
3217
3218 ret = tg3_nvram_lock(tp);
3219 if (ret)
3220 return ret;
3221
3222 tg3_enable_nvram_access(tp);
3223 if (tg3_flag(tp, 5750_PLUS) && !tg3_flag(tp, PROTECTED_NVRAM))
3224 tw32(NVRAM_WRITE1, 0x406);
3225
3226 grc_mode = tr32(GRC_MODE);
3227 tw32(GRC_MODE, grc_mode | GRC_MODE_NVRAM_WR_ENABLE);
3228
3229 if (tg3_flag(tp, NVRAM_BUFFERED) || !tg3_flag(tp, FLASH)) {
3230 ret = tg3_nvram_write_block_buffered(tp, offset, len,
3231 buf);
3232 } else {
3233 ret = tg3_nvram_write_block_unbuffered(tp, offset, len,
3234 buf);
3235 }
3236
3237 grc_mode = tr32(GRC_MODE);
3238 tw32(GRC_MODE, grc_mode & ~GRC_MODE_NVRAM_WR_ENABLE);
3239
3240 tg3_disable_nvram_access(tp);
3241 tg3_nvram_unlock(tp);
3242 }
3243
3244 if (tg3_flag(tp, EEPROM_WRITE_PROT)) {
3245 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
3246 udelay(40);
3247 }
3248
3249 return ret;
3250}
3251
Matt Carlson997b4f12011-08-31 11:44:53 +00003252#define RX_CPU_SCRATCH_BASE 0x30000
3253#define RX_CPU_SCRATCH_SIZE 0x04000
3254#define TX_CPU_SCRATCH_BASE 0x34000
3255#define TX_CPU_SCRATCH_SIZE 0x04000
3256
3257/* tp->lock is held. */
3258static int tg3_halt_cpu(struct tg3 *tp, u32 offset)
3259{
3260 int i;
3261
3262 BUG_ON(offset == TX_CPU_BASE && tg3_flag(tp, 5705_PLUS));
3263
3264 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
3265 u32 val = tr32(GRC_VCPU_EXT_CTRL);
3266
3267 tw32(GRC_VCPU_EXT_CTRL, val | GRC_VCPU_EXT_CTRL_HALT_CPU);
3268 return 0;
3269 }
3270 if (offset == RX_CPU_BASE) {
3271 for (i = 0; i < 10000; i++) {
3272 tw32(offset + CPU_STATE, 0xffffffff);
3273 tw32(offset + CPU_MODE, CPU_MODE_HALT);
3274 if (tr32(offset + CPU_MODE) & CPU_MODE_HALT)
3275 break;
3276 }
3277
3278 tw32(offset + CPU_STATE, 0xffffffff);
3279 tw32_f(offset + CPU_MODE, CPU_MODE_HALT);
3280 udelay(10);
3281 } else {
3282 for (i = 0; i < 10000; i++) {
3283 tw32(offset + CPU_STATE, 0xffffffff);
3284 tw32(offset + CPU_MODE, CPU_MODE_HALT);
3285 if (tr32(offset + CPU_MODE) & CPU_MODE_HALT)
3286 break;
3287 }
3288 }
3289
3290 if (i >= 10000) {
3291 netdev_err(tp->dev, "%s timed out, %s CPU\n",
3292 __func__, offset == RX_CPU_BASE ? "RX" : "TX");
3293 return -ENODEV;
3294 }
3295
3296 /* Clear firmware's nvram arbitration. */
3297 if (tg3_flag(tp, NVRAM))
3298 tw32(NVRAM_SWARB, SWARB_REQ_CLR0);
3299 return 0;
3300}
3301
3302struct fw_info {
3303 unsigned int fw_base;
3304 unsigned int fw_len;
3305 const __be32 *fw_data;
3306};
3307
3308/* tp->lock is held. */
3309static int tg3_load_firmware_cpu(struct tg3 *tp, u32 cpu_base,
3310 u32 cpu_scratch_base, int cpu_scratch_size,
3311 struct fw_info *info)
3312{
3313 int err, lock_err, i;
3314 void (*write_op)(struct tg3 *, u32, u32);
3315
3316 if (cpu_base == TX_CPU_BASE && tg3_flag(tp, 5705_PLUS)) {
3317 netdev_err(tp->dev,
3318 "%s: Trying to load TX cpu firmware which is 5705\n",
3319 __func__);
3320 return -EINVAL;
3321 }
3322
3323 if (tg3_flag(tp, 5705_PLUS))
3324 write_op = tg3_write_mem;
3325 else
3326 write_op = tg3_write_indirect_reg32;
3327
3328 /* It is possible that bootcode is still loading at this point.
3329 * Get the nvram lock first before halting the cpu.
3330 */
3331 lock_err = tg3_nvram_lock(tp);
3332 err = tg3_halt_cpu(tp, cpu_base);
3333 if (!lock_err)
3334 tg3_nvram_unlock(tp);
3335 if (err)
3336 goto out;
3337
3338 for (i = 0; i < cpu_scratch_size; i += sizeof(u32))
3339 write_op(tp, cpu_scratch_base + i, 0);
3340 tw32(cpu_base + CPU_STATE, 0xffffffff);
3341 tw32(cpu_base + CPU_MODE, tr32(cpu_base+CPU_MODE)|CPU_MODE_HALT);
3342 for (i = 0; i < (info->fw_len / sizeof(u32)); i++)
3343 write_op(tp, (cpu_scratch_base +
3344 (info->fw_base & 0xffff) +
3345 (i * sizeof(u32))),
3346 be32_to_cpu(info->fw_data[i]));
3347
3348 err = 0;
3349
3350out:
3351 return err;
3352}
3353
3354/* tp->lock is held. */
3355static int tg3_load_5701_a0_firmware_fix(struct tg3 *tp)
3356{
3357 struct fw_info info;
3358 const __be32 *fw_data;
3359 int err, i;
3360
3361 fw_data = (void *)tp->fw->data;
3362
3363 /* Firmware blob starts with version numbers, followed by
3364 start address and length. We are setting complete length.
3365 length = end_address_of_bss - start_address_of_text.
3366 Remainder is the blob to be loaded contiguously
3367 from start address. */
3368
3369 info.fw_base = be32_to_cpu(fw_data[1]);
3370 info.fw_len = tp->fw->size - 12;
3371 info.fw_data = &fw_data[3];
3372
3373 err = tg3_load_firmware_cpu(tp, RX_CPU_BASE,
3374 RX_CPU_SCRATCH_BASE, RX_CPU_SCRATCH_SIZE,
3375 &info);
3376 if (err)
3377 return err;
3378
3379 err = tg3_load_firmware_cpu(tp, TX_CPU_BASE,
3380 TX_CPU_SCRATCH_BASE, TX_CPU_SCRATCH_SIZE,
3381 &info);
3382 if (err)
3383 return err;
3384
3385 /* Now startup only the RX cpu. */
3386 tw32(RX_CPU_BASE + CPU_STATE, 0xffffffff);
3387 tw32_f(RX_CPU_BASE + CPU_PC, info.fw_base);
3388
3389 for (i = 0; i < 5; i++) {
3390 if (tr32(RX_CPU_BASE + CPU_PC) == info.fw_base)
3391 break;
3392 tw32(RX_CPU_BASE + CPU_STATE, 0xffffffff);
3393 tw32(RX_CPU_BASE + CPU_MODE, CPU_MODE_HALT);
3394 tw32_f(RX_CPU_BASE + CPU_PC, info.fw_base);
3395 udelay(1000);
3396 }
3397 if (i >= 5) {
3398 netdev_err(tp->dev, "%s fails to set RX CPU PC, is %08x "
3399 "should be %08x\n", __func__,
3400 tr32(RX_CPU_BASE + CPU_PC), info.fw_base);
3401 return -ENODEV;
3402 }
3403 tw32(RX_CPU_BASE + CPU_STATE, 0xffffffff);
3404 tw32_f(RX_CPU_BASE + CPU_MODE, 0x00000000);
3405
3406 return 0;
3407}
3408
3409/* tp->lock is held. */
3410static int tg3_load_tso_firmware(struct tg3 *tp)
3411{
3412 struct fw_info info;
3413 const __be32 *fw_data;
3414 unsigned long cpu_base, cpu_scratch_base, cpu_scratch_size;
3415 int err, i;
3416
3417 if (tg3_flag(tp, HW_TSO_1) ||
3418 tg3_flag(tp, HW_TSO_2) ||
3419 tg3_flag(tp, HW_TSO_3))
3420 return 0;
3421
3422 fw_data = (void *)tp->fw->data;
3423
3424 /* Firmware blob starts with version numbers, followed by
3425 start address and length. We are setting complete length.
3426 length = end_address_of_bss - start_address_of_text.
3427 Remainder is the blob to be loaded contiguously
3428 from start address. */
3429
3430 info.fw_base = be32_to_cpu(fw_data[1]);
3431 cpu_scratch_size = tp->fw_len;
3432 info.fw_len = tp->fw->size - 12;
3433 info.fw_data = &fw_data[3];
3434
3435 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
3436 cpu_base = RX_CPU_BASE;
3437 cpu_scratch_base = NIC_SRAM_MBUF_POOL_BASE5705;
3438 } else {
3439 cpu_base = TX_CPU_BASE;
3440 cpu_scratch_base = TX_CPU_SCRATCH_BASE;
3441 cpu_scratch_size = TX_CPU_SCRATCH_SIZE;
3442 }
3443
3444 err = tg3_load_firmware_cpu(tp, cpu_base,
3445 cpu_scratch_base, cpu_scratch_size,
3446 &info);
3447 if (err)
3448 return err;
3449
3450 /* Now startup the cpu. */
3451 tw32(cpu_base + CPU_STATE, 0xffffffff);
3452 tw32_f(cpu_base + CPU_PC, info.fw_base);
3453
3454 for (i = 0; i < 5; i++) {
3455 if (tr32(cpu_base + CPU_PC) == info.fw_base)
3456 break;
3457 tw32(cpu_base + CPU_STATE, 0xffffffff);
3458 tw32(cpu_base + CPU_MODE, CPU_MODE_HALT);
3459 tw32_f(cpu_base + CPU_PC, info.fw_base);
3460 udelay(1000);
3461 }
3462 if (i >= 5) {
3463 netdev_err(tp->dev,
3464 "%s fails to set CPU PC, is %08x should be %08x\n",
3465 __func__, tr32(cpu_base + CPU_PC), info.fw_base);
3466 return -ENODEV;
3467 }
3468 tw32(cpu_base + CPU_STATE, 0xffffffff);
3469 tw32_f(cpu_base + CPU_MODE, 0x00000000);
3470 return 0;
3471}
3472
3473
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003474/* tp->lock is held. */
Matt Carlson3f007892008-11-03 16:51:36 -08003475static void __tg3_set_mac_addr(struct tg3 *tp, int skip_mac_1)
3476{
3477 u32 addr_high, addr_low;
3478 int i;
3479
3480 addr_high = ((tp->dev->dev_addr[0] << 8) |
3481 tp->dev->dev_addr[1]);
3482 addr_low = ((tp->dev->dev_addr[2] << 24) |
3483 (tp->dev->dev_addr[3] << 16) |
3484 (tp->dev->dev_addr[4] << 8) |
3485 (tp->dev->dev_addr[5] << 0));
3486 for (i = 0; i < 4; i++) {
3487 if (i == 1 && skip_mac_1)
3488 continue;
3489 tw32(MAC_ADDR_0_HIGH + (i * 8), addr_high);
3490 tw32(MAC_ADDR_0_LOW + (i * 8), addr_low);
3491 }
3492
3493 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
3494 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
3495 for (i = 0; i < 12; i++) {
3496 tw32(MAC_EXTADDR_0_HIGH + (i * 8), addr_high);
3497 tw32(MAC_EXTADDR_0_LOW + (i * 8), addr_low);
3498 }
3499 }
3500
3501 addr_high = (tp->dev->dev_addr[0] +
3502 tp->dev->dev_addr[1] +
3503 tp->dev->dev_addr[2] +
3504 tp->dev->dev_addr[3] +
3505 tp->dev->dev_addr[4] +
3506 tp->dev->dev_addr[5]) &
3507 TX_BACKOFF_SEED_MASK;
3508 tw32(MAC_TX_BACKOFF_SEED, addr_high);
3509}
3510
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003511static void tg3_enable_register_access(struct tg3 *tp)
3512{
3513 /*
3514 * Make sure register accesses (indirect or otherwise) will function
3515 * correctly.
3516 */
3517 pci_write_config_dword(tp->pdev,
3518 TG3PCI_MISC_HOST_CTRL, tp->misc_host_ctrl);
3519}
3520
3521static int tg3_power_up(struct tg3 *tp)
3522{
Matt Carlsonbed98292011-07-13 09:27:29 +00003523 int err;
3524
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003525 tg3_enable_register_access(tp);
3526
Matt Carlsonbed98292011-07-13 09:27:29 +00003527 err = pci_set_power_state(tp->pdev, PCI_D0);
3528 if (!err) {
3529 /* Switch out of Vaux if it is a NIC */
3530 tg3_pwrsrc_switch_to_vmain(tp);
3531 } else {
3532 netdev_err(tp->dev, "Transition to D0 failed\n");
3533 }
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003534
Matt Carlsonbed98292011-07-13 09:27:29 +00003535 return err;
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003536}
3537
Matt Carlson4b409522012-02-13 10:20:11 +00003538static int tg3_setup_phy(struct tg3 *, int);
3539
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003540static int tg3_power_down_prepare(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003541{
3542 u32 misc_host_ctrl;
Matt Carlson0a459aa2008-11-03 16:54:15 -08003543 bool device_should_wake, do_low_power;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003544
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003545 tg3_enable_register_access(tp);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08003546
3547 /* Restore the CLKREQ setting. */
Joe Perches63c3a662011-04-26 08:12:10 +00003548 if (tg3_flag(tp, CLKREQ_BUG)) {
Matt Carlson5e7dfd02008-11-21 17:18:16 -08003549 u16 lnkctl;
3550
3551 pci_read_config_word(tp->pdev,
Jon Mason708ebb32011-06-27 12:56:50 +00003552 pci_pcie_cap(tp->pdev) + PCI_EXP_LNKCTL,
Matt Carlson5e7dfd02008-11-21 17:18:16 -08003553 &lnkctl);
3554 lnkctl |= PCI_EXP_LNKCTL_CLKREQ_EN;
3555 pci_write_config_word(tp->pdev,
Jon Mason708ebb32011-06-27 12:56:50 +00003556 pci_pcie_cap(tp->pdev) + PCI_EXP_LNKCTL,
Matt Carlson5e7dfd02008-11-21 17:18:16 -08003557 lnkctl);
3558 }
3559
Linus Torvalds1da177e2005-04-16 15:20:36 -07003560 misc_host_ctrl = tr32(TG3PCI_MISC_HOST_CTRL);
3561 tw32(TG3PCI_MISC_HOST_CTRL,
3562 misc_host_ctrl | MISC_HOST_CTRL_MASK_PCI_INT);
3563
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003564 device_should_wake = device_may_wakeup(&tp->pdev->dev) &&
Joe Perches63c3a662011-04-26 08:12:10 +00003565 tg3_flag(tp, WOL_ENABLE);
Matt Carlson05ac4cb2008-11-03 16:53:46 -08003566
Joe Perches63c3a662011-04-26 08:12:10 +00003567 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson0a459aa2008-11-03 16:54:15 -08003568 do_low_power = false;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003569 if ((tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) &&
Matt Carlson800960682010-08-02 11:26:06 +00003570 !(tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07003571 struct phy_device *phydev;
Matt Carlson0a459aa2008-11-03 16:54:15 -08003572 u32 phyid, advertising;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07003573
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00003574 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07003575
Matt Carlson800960682010-08-02 11:26:06 +00003576 tp->phy_flags |= TG3_PHYFLG_IS_LOW_POWER;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07003577
Matt Carlsonc6700ce2012-02-13 15:20:15 +00003578 tp->link_config.speed = phydev->speed;
3579 tp->link_config.duplex = phydev->duplex;
3580 tp->link_config.autoneg = phydev->autoneg;
3581 tp->link_config.advertising = phydev->advertising;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07003582
3583 advertising = ADVERTISED_TP |
3584 ADVERTISED_Pause |
3585 ADVERTISED_Autoneg |
3586 ADVERTISED_10baseT_Half;
3587
Joe Perches63c3a662011-04-26 08:12:10 +00003588 if (tg3_flag(tp, ENABLE_ASF) || device_should_wake) {
3589 if (tg3_flag(tp, WOL_SPEED_100MB))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07003590 advertising |=
3591 ADVERTISED_100baseT_Half |
3592 ADVERTISED_100baseT_Full |
3593 ADVERTISED_10baseT_Full;
3594 else
3595 advertising |= ADVERTISED_10baseT_Full;
3596 }
3597
3598 phydev->advertising = advertising;
3599
3600 phy_start_aneg(phydev);
Matt Carlson0a459aa2008-11-03 16:54:15 -08003601
3602 phyid = phydev->drv->phy_id & phydev->drv->phy_id_mask;
Matt Carlson6a443a02010-02-17 15:17:04 +00003603 if (phyid != PHY_ID_BCMAC131) {
3604 phyid &= PHY_BCM_OUI_MASK;
3605 if (phyid == PHY_BCM_OUI_1 ||
3606 phyid == PHY_BCM_OUI_2 ||
3607 phyid == PHY_BCM_OUI_3)
Matt Carlson0a459aa2008-11-03 16:54:15 -08003608 do_low_power = true;
3609 }
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07003610 }
Matt Carlsondd477002008-05-25 23:45:58 -07003611 } else {
Matt Carlson20232762008-12-21 20:18:56 -08003612 do_low_power = true;
Matt Carlson0a459aa2008-11-03 16:54:15 -08003613
Matt Carlsonc6700ce2012-02-13 15:20:15 +00003614 if (!(tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER))
Matt Carlson800960682010-08-02 11:26:06 +00003615 tp->phy_flags |= TG3_PHYFLG_IS_LOW_POWER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003616
Matt Carlson2855b9f2012-02-13 15:20:14 +00003617 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
Matt Carlsondd477002008-05-25 23:45:58 -07003618 tg3_setup_phy(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003619 }
3620
Michael Chanb5d37722006-09-27 16:06:21 -07003621 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
3622 u32 val;
3623
3624 val = tr32(GRC_VCPU_EXT_CTRL);
3625 tw32(GRC_VCPU_EXT_CTRL, val | GRC_VCPU_EXT_CTRL_DISABLE_WOL);
Joe Perches63c3a662011-04-26 08:12:10 +00003626 } else if (!tg3_flag(tp, ENABLE_ASF)) {
Michael Chan6921d202005-12-13 21:15:53 -08003627 int i;
3628 u32 val;
3629
3630 for (i = 0; i < 200; i++) {
3631 tg3_read_mem(tp, NIC_SRAM_FW_ASF_STATUS_MBOX, &val);
3632 if (val == ~NIC_SRAM_FIRMWARE_MBOX_MAGIC1)
3633 break;
3634 msleep(1);
3635 }
3636 }
Joe Perches63c3a662011-04-26 08:12:10 +00003637 if (tg3_flag(tp, WOL_CAP))
Gary Zambranoa85feb82007-05-05 11:52:19 -07003638 tg3_write_mem(tp, NIC_SRAM_WOL_MBOX, WOL_SIGNATURE |
3639 WOL_DRV_STATE_SHUTDOWN |
3640 WOL_DRV_WOL |
3641 WOL_SET_MAGIC_PKT);
Michael Chan6921d202005-12-13 21:15:53 -08003642
Matt Carlson05ac4cb2008-11-03 16:53:46 -08003643 if (device_should_wake) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003644 u32 mac_mode;
3645
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003646 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES)) {
Matt Carlsonb4bd2922011-04-20 07:57:41 +00003647 if (do_low_power &&
3648 !(tp->phy_flags & TG3_PHYFLG_IS_FET)) {
3649 tg3_phy_auxctl_write(tp,
3650 MII_TG3_AUXCTL_SHDWSEL_PWRCTL,
3651 MII_TG3_AUXCTL_PCTL_WOL_EN |
3652 MII_TG3_AUXCTL_PCTL_100TX_LPWR |
3653 MII_TG3_AUXCTL_PCTL_CL_AB_TXDAC);
Matt Carlsondd477002008-05-25 23:45:58 -07003654 udelay(40);
3655 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003656
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003657 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
Michael Chan3f7045c2006-09-27 16:02:29 -07003658 mac_mode = MAC_MODE_PORT_MODE_GMII;
3659 else
3660 mac_mode = MAC_MODE_PORT_MODE_MII;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003661
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07003662 mac_mode |= tp->mac_mode & MAC_MODE_LINK_POLARITY;
3663 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
3664 ASIC_REV_5700) {
Joe Perches63c3a662011-04-26 08:12:10 +00003665 u32 speed = tg3_flag(tp, WOL_SPEED_100MB) ?
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07003666 SPEED_100 : SPEED_10;
3667 if (tg3_5700_link_polarity(tp, speed))
3668 mac_mode |= MAC_MODE_LINK_POLARITY;
3669 else
3670 mac_mode &= ~MAC_MODE_LINK_POLARITY;
3671 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003672 } else {
3673 mac_mode = MAC_MODE_PORT_MODE_TBI;
3674 }
3675
Joe Perches63c3a662011-04-26 08:12:10 +00003676 if (!tg3_flag(tp, 5750_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003677 tw32(MAC_LED_CTRL, tp->led_ctrl);
3678
Matt Carlson05ac4cb2008-11-03 16:53:46 -08003679 mac_mode |= MAC_MODE_MAGIC_PKT_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00003680 if ((tg3_flag(tp, 5705_PLUS) && !tg3_flag(tp, 5780_CLASS)) &&
3681 (tg3_flag(tp, ENABLE_ASF) || tg3_flag(tp, ENABLE_APE)))
Matt Carlson05ac4cb2008-11-03 16:53:46 -08003682 mac_mode |= MAC_MODE_KEEP_FRAME_IN_WOL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003683
Joe Perches63c3a662011-04-26 08:12:10 +00003684 if (tg3_flag(tp, ENABLE_APE))
Matt Carlsond2394e6b2010-11-24 08:31:47 +00003685 mac_mode |= MAC_MODE_APE_TX_EN |
3686 MAC_MODE_APE_RX_EN |
3687 MAC_MODE_TDE_ENABLE;
Matt Carlson3bda1252008-08-15 14:08:22 -07003688
Linus Torvalds1da177e2005-04-16 15:20:36 -07003689 tw32_f(MAC_MODE, mac_mode);
3690 udelay(100);
3691
3692 tw32_f(MAC_RX_MODE, RX_MODE_ENABLE);
3693 udelay(10);
3694 }
3695
Joe Perches63c3a662011-04-26 08:12:10 +00003696 if (!tg3_flag(tp, WOL_SPEED_100MB) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003697 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
3698 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)) {
3699 u32 base_val;
3700
3701 base_val = tp->pci_clock_ctrl;
3702 base_val |= (CLOCK_CTRL_RXCLK_DISABLE |
3703 CLOCK_CTRL_TXCLK_DISABLE);
3704
Michael Chanb401e9e2005-12-19 16:27:04 -08003705 tw32_wait_f(TG3PCI_CLOCK_CTRL, base_val | CLOCK_CTRL_ALTCLK |
3706 CLOCK_CTRL_PWRDOWN_PLL133, 40);
Joe Perches63c3a662011-04-26 08:12:10 +00003707 } else if (tg3_flag(tp, 5780_CLASS) ||
3708 tg3_flag(tp, CPMU_PRESENT) ||
Matt Carlson6ff6f812011-05-19 12:12:54 +00003709 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan4cf78e42005-07-25 12:29:19 -07003710 /* do nothing */
Joe Perches63c3a662011-04-26 08:12:10 +00003711 } else if (!(tg3_flag(tp, 5750_PLUS) && tg3_flag(tp, ENABLE_ASF))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003712 u32 newbits1, newbits2;
3713
3714 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
3715 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
3716 newbits1 = (CLOCK_CTRL_RXCLK_DISABLE |
3717 CLOCK_CTRL_TXCLK_DISABLE |
3718 CLOCK_CTRL_ALTCLK);
3719 newbits2 = newbits1 | CLOCK_CTRL_44MHZ_CORE;
Joe Perches63c3a662011-04-26 08:12:10 +00003720 } else if (tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003721 newbits1 = CLOCK_CTRL_625_CORE;
3722 newbits2 = newbits1 | CLOCK_CTRL_ALTCLK;
3723 } else {
3724 newbits1 = CLOCK_CTRL_ALTCLK;
3725 newbits2 = newbits1 | CLOCK_CTRL_44MHZ_CORE;
3726 }
3727
Michael Chanb401e9e2005-12-19 16:27:04 -08003728 tw32_wait_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl | newbits1,
3729 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003730
Michael Chanb401e9e2005-12-19 16:27:04 -08003731 tw32_wait_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl | newbits2,
3732 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003733
Joe Perches63c3a662011-04-26 08:12:10 +00003734 if (!tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003735 u32 newbits3;
3736
3737 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
3738 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
3739 newbits3 = (CLOCK_CTRL_RXCLK_DISABLE |
3740 CLOCK_CTRL_TXCLK_DISABLE |
3741 CLOCK_CTRL_44MHZ_CORE);
3742 } else {
3743 newbits3 = CLOCK_CTRL_44MHZ_CORE;
3744 }
3745
Michael Chanb401e9e2005-12-19 16:27:04 -08003746 tw32_wait_f(TG3PCI_CLOCK_CTRL,
3747 tp->pci_clock_ctrl | newbits3, 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003748 }
3749 }
3750
Joe Perches63c3a662011-04-26 08:12:10 +00003751 if (!(device_should_wake) && !tg3_flag(tp, ENABLE_ASF))
Matt Carlson0a459aa2008-11-03 16:54:15 -08003752 tg3_power_down_phy(tp, do_low_power);
Michael Chan6921d202005-12-13 21:15:53 -08003753
Matt Carlsoncd0d7222011-07-13 09:27:33 +00003754 tg3_frob_aux_power(tp, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003755
3756 /* Workaround for unstable PLL clock */
3757 if ((GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_AX) ||
3758 (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_BX)) {
3759 u32 val = tr32(0x7d00);
3760
3761 val &= ~((1 << 16) | (1 << 4) | (1 << 2) | (1 << 1) | 1);
3762 tw32(0x7d00, val);
Joe Perches63c3a662011-04-26 08:12:10 +00003763 if (!tg3_flag(tp, ENABLE_ASF)) {
Michael Chanec41c7d2006-01-17 02:40:55 -08003764 int err;
3765
3766 err = tg3_nvram_lock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003767 tg3_halt_cpu(tp, RX_CPU_BASE);
Michael Chanec41c7d2006-01-17 02:40:55 -08003768 if (!err)
3769 tg3_nvram_unlock(tp);
Michael Chan6921d202005-12-13 21:15:53 -08003770 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003771 }
3772
Michael Chanbbadf502006-04-06 21:46:34 -07003773 tg3_write_sig_post_reset(tp, RESET_KIND_SHUTDOWN);
3774
Linus Torvalds1da177e2005-04-16 15:20:36 -07003775 return 0;
3776}
3777
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003778static void tg3_power_down(struct tg3 *tp)
3779{
3780 tg3_power_down_prepare(tp);
3781
Joe Perches63c3a662011-04-26 08:12:10 +00003782 pci_wake_from_d3(tp->pdev, tg3_flag(tp, WOL_ENABLE));
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003783 pci_set_power_state(tp->pdev, PCI_D3hot);
3784}
3785
Linus Torvalds1da177e2005-04-16 15:20:36 -07003786static void tg3_aux_stat_to_speed_duplex(struct tg3 *tp, u32 val, u16 *speed, u8 *duplex)
3787{
3788 switch (val & MII_TG3_AUX_STAT_SPDMASK) {
3789 case MII_TG3_AUX_STAT_10HALF:
3790 *speed = SPEED_10;
3791 *duplex = DUPLEX_HALF;
3792 break;
3793
3794 case MII_TG3_AUX_STAT_10FULL:
3795 *speed = SPEED_10;
3796 *duplex = DUPLEX_FULL;
3797 break;
3798
3799 case MII_TG3_AUX_STAT_100HALF:
3800 *speed = SPEED_100;
3801 *duplex = DUPLEX_HALF;
3802 break;
3803
3804 case MII_TG3_AUX_STAT_100FULL:
3805 *speed = SPEED_100;
3806 *duplex = DUPLEX_FULL;
3807 break;
3808
3809 case MII_TG3_AUX_STAT_1000HALF:
3810 *speed = SPEED_1000;
3811 *duplex = DUPLEX_HALF;
3812 break;
3813
3814 case MII_TG3_AUX_STAT_1000FULL:
3815 *speed = SPEED_1000;
3816 *duplex = DUPLEX_FULL;
3817 break;
3818
3819 default:
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003820 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Michael Chan715116a2006-09-27 16:09:25 -07003821 *speed = (val & MII_TG3_AUX_STAT_100) ? SPEED_100 :
3822 SPEED_10;
3823 *duplex = (val & MII_TG3_AUX_STAT_FULL) ? DUPLEX_FULL :
3824 DUPLEX_HALF;
3825 break;
3826 }
Matt Carlsone7405222012-02-13 15:20:16 +00003827 *speed = SPEED_UNKNOWN;
3828 *duplex = DUPLEX_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003829 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07003830 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003831}
3832
Matt Carlson42b64a42011-05-19 12:12:49 +00003833static int tg3_phy_autoneg_cfg(struct tg3 *tp, u32 advertise, u32 flowctrl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003834{
Matt Carlson42b64a42011-05-19 12:12:49 +00003835 int err = 0;
3836 u32 val, new_adv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003837
Matt Carlson42b64a42011-05-19 12:12:49 +00003838 new_adv = ADVERTISE_CSMA;
Hiroaki SHIMODA202ff1c2011-11-22 04:05:41 +00003839 new_adv |= ethtool_adv_to_mii_adv_t(advertise) & ADVERTISE_ALL;
Matt Carlsonf88788f2011-12-14 11:10:00 +00003840 new_adv |= mii_advertise_flowctrl(flowctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003841
Matt Carlson42b64a42011-05-19 12:12:49 +00003842 err = tg3_writephy(tp, MII_ADVERTISE, new_adv);
3843 if (err)
3844 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003845
Matt Carlson4f272092011-12-14 11:09:57 +00003846 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
3847 new_adv = ethtool_adv_to_mii_ctrl1000_t(advertise);
Matt Carlsonba4d07a2007-12-20 20:08:00 -08003848
Matt Carlson4f272092011-12-14 11:09:57 +00003849 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
3850 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0)
3851 new_adv |= CTL1000_AS_MASTER | CTL1000_ENABLE_MASTER;
Matt Carlsonba4d07a2007-12-20 20:08:00 -08003852
Matt Carlson4f272092011-12-14 11:09:57 +00003853 err = tg3_writephy(tp, MII_CTRL1000, new_adv);
3854 if (err)
3855 goto done;
3856 }
Matt Carlsonba4d07a2007-12-20 20:08:00 -08003857
Matt Carlson42b64a42011-05-19 12:12:49 +00003858 if (!(tp->phy_flags & TG3_PHYFLG_EEE_CAP))
3859 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003860
Matt Carlson42b64a42011-05-19 12:12:49 +00003861 tw32(TG3_CPMU_EEE_MODE,
3862 tr32(TG3_CPMU_EEE_MODE) & ~TG3_CPMU_EEEMD_LPI_ENABLE);
Matt Carlsonba4d07a2007-12-20 20:08:00 -08003863
Matt Carlson42b64a42011-05-19 12:12:49 +00003864 err = TG3_PHY_AUXCTL_SMDSP_ENABLE(tp);
3865 if (!err) {
3866 u32 err2;
Matt Carlson52b02d02010-10-14 10:37:41 +00003867
Matt Carlsona6b68da2010-12-06 08:28:52 +00003868 val = 0;
Matt Carlson42b64a42011-05-19 12:12:49 +00003869 /* Advertise 100-BaseTX EEE ability */
3870 if (advertise & ADVERTISED_100baseT_Full)
3871 val |= MDIO_AN_EEE_ADV_100TX;
3872 /* Advertise 1000-BaseT EEE ability */
3873 if (advertise & ADVERTISED_1000baseT_Full)
3874 val |= MDIO_AN_EEE_ADV_1000T;
3875 err = tg3_phy_cl45_write(tp, MDIO_MMD_AN, MDIO_AN_EEE_ADV, val);
Matt Carlsonb715ce92011-07-20 10:20:52 +00003876 if (err)
3877 val = 0;
3878
3879 switch (GET_ASIC_REV(tp->pci_chip_rev_id)) {
3880 case ASIC_REV_5717:
3881 case ASIC_REV_57765:
Matt Carlson55086ad2011-12-14 11:09:59 +00003882 case ASIC_REV_57766:
Matt Carlsonb715ce92011-07-20 10:20:52 +00003883 case ASIC_REV_5719:
3884 /* If we advertised any eee advertisements above... */
3885 if (val)
3886 val = MII_TG3_DSP_TAP26_ALNOKO |
3887 MII_TG3_DSP_TAP26_RMRXSTO |
3888 MII_TG3_DSP_TAP26_OPCSINPT;
3889 tg3_phydsp_write(tp, MII_TG3_DSP_TAP26, val);
3890 /* Fall through */
3891 case ASIC_REV_5720:
3892 if (!tg3_phydsp_read(tp, MII_TG3_DSP_CH34TP2, &val))
3893 tg3_phydsp_write(tp, MII_TG3_DSP_CH34TP2, val |
3894 MII_TG3_DSP_CH34TP2_HIBW01);
3895 }
Matt Carlson52b02d02010-10-14 10:37:41 +00003896
Matt Carlson42b64a42011-05-19 12:12:49 +00003897 err2 = TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
3898 if (!err)
3899 err = err2;
3900 }
3901
3902done:
3903 return err;
3904}
3905
3906static void tg3_phy_copper_begin(struct tg3 *tp)
3907{
Matt Carlsond13ba512012-02-22 12:35:19 +00003908 if (tp->link_config.autoneg == AUTONEG_ENABLE ||
3909 (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)) {
3910 u32 adv, fc;
Matt Carlson42b64a42011-05-19 12:12:49 +00003911
Matt Carlsond13ba512012-02-22 12:35:19 +00003912 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) {
3913 adv = ADVERTISED_10baseT_Half |
3914 ADVERTISED_10baseT_Full;
3915 if (tg3_flag(tp, WOL_SPEED_100MB))
3916 adv |= ADVERTISED_100baseT_Half |
3917 ADVERTISED_100baseT_Full;
Matt Carlson42b64a42011-05-19 12:12:49 +00003918
Matt Carlsond13ba512012-02-22 12:35:19 +00003919 fc = FLOW_CTRL_TX | FLOW_CTRL_RX;
Matt Carlson42b64a42011-05-19 12:12:49 +00003920 } else {
Matt Carlsond13ba512012-02-22 12:35:19 +00003921 adv = tp->link_config.advertising;
3922 if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
3923 adv &= ~(ADVERTISED_1000baseT_Half |
3924 ADVERTISED_1000baseT_Full);
3925
3926 fc = tp->link_config.flowctrl;
Matt Carlson42b64a42011-05-19 12:12:49 +00003927 }
3928
Matt Carlsond13ba512012-02-22 12:35:19 +00003929 tg3_phy_autoneg_cfg(tp, adv, fc);
Matt Carlson52b02d02010-10-14 10:37:41 +00003930
Matt Carlsond13ba512012-02-22 12:35:19 +00003931 tg3_writephy(tp, MII_BMCR,
3932 BMCR_ANENABLE | BMCR_ANRESTART);
3933 } else {
3934 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003935 u32 bmcr, orig_bmcr;
3936
3937 tp->link_config.active_speed = tp->link_config.speed;
3938 tp->link_config.active_duplex = tp->link_config.duplex;
3939
3940 bmcr = 0;
3941 switch (tp->link_config.speed) {
3942 default:
3943 case SPEED_10:
3944 break;
3945
3946 case SPEED_100:
3947 bmcr |= BMCR_SPEED100;
3948 break;
3949
3950 case SPEED_1000:
Matt Carlson221c5632011-06-13 13:39:01 +00003951 bmcr |= BMCR_SPEED1000;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003952 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07003953 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003954
3955 if (tp->link_config.duplex == DUPLEX_FULL)
3956 bmcr |= BMCR_FULLDPLX;
3957
3958 if (!tg3_readphy(tp, MII_BMCR, &orig_bmcr) &&
3959 (bmcr != orig_bmcr)) {
3960 tg3_writephy(tp, MII_BMCR, BMCR_LOOPBACK);
3961 for (i = 0; i < 1500; i++) {
3962 u32 tmp;
3963
3964 udelay(10);
3965 if (tg3_readphy(tp, MII_BMSR, &tmp) ||
3966 tg3_readphy(tp, MII_BMSR, &tmp))
3967 continue;
3968 if (!(tmp & BMSR_LSTATUS)) {
3969 udelay(40);
3970 break;
3971 }
3972 }
3973 tg3_writephy(tp, MII_BMCR, bmcr);
3974 udelay(40);
3975 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003976 }
3977}
3978
3979static int tg3_init_5401phy_dsp(struct tg3 *tp)
3980{
3981 int err;
3982
3983 /* Turn off tap power management. */
3984 /* Set Extended packet length bit */
Matt Carlsonb4bd2922011-04-20 07:57:41 +00003985 err = tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_AUXCTL, 0x4c20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003986
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00003987 err |= tg3_phydsp_write(tp, 0x0012, 0x1804);
3988 err |= tg3_phydsp_write(tp, 0x0013, 0x1204);
3989 err |= tg3_phydsp_write(tp, 0x8006, 0x0132);
3990 err |= tg3_phydsp_write(tp, 0x8006, 0x0232);
3991 err |= tg3_phydsp_write(tp, 0x201f, 0x0a20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003992
3993 udelay(40);
3994
3995 return err;
3996}
3997
Matt Carlsone2bf73e2011-12-08 14:40:15 +00003998static bool tg3_phy_copper_an_config_ok(struct tg3 *tp, u32 *lcladv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003999{
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004000 u32 advmsk, tgtadv, advertising;
Michael Chan3600d912006-12-07 00:21:48 -08004001
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004002 advertising = tp->link_config.advertising;
4003 tgtadv = ethtool_adv_to_mii_adv_t(advertising) & ADVERTISE_ALL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004004
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004005 advmsk = ADVERTISE_ALL;
4006 if (tp->link_config.active_duplex == DUPLEX_FULL) {
Matt Carlsonf88788f2011-12-14 11:10:00 +00004007 tgtadv |= mii_advertise_flowctrl(tp->link_config.flowctrl);
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004008 advmsk |= ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
4009 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004010
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004011 if (tg3_readphy(tp, MII_ADVERTISE, lcladv))
4012 return false;
4013
4014 if ((*lcladv & advmsk) != tgtadv)
4015 return false;
Matt Carlsonb99d2a52011-08-31 11:44:47 +00004016
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004017 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004018 u32 tg3_ctrl;
4019
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004020 tgtadv = ethtool_adv_to_mii_ctrl1000_t(advertising);
Michael Chan3600d912006-12-07 00:21:48 -08004021
Matt Carlson221c5632011-06-13 13:39:01 +00004022 if (tg3_readphy(tp, MII_CTRL1000, &tg3_ctrl))
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004023 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004024
Matt Carlson3198e072012-02-13 15:20:10 +00004025 if (tgtadv &&
4026 (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
4027 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0)) {
4028 tgtadv |= CTL1000_AS_MASTER | CTL1000_ENABLE_MASTER;
4029 tg3_ctrl &= (ADVERTISE_1000HALF | ADVERTISE_1000FULL |
4030 CTL1000_AS_MASTER | CTL1000_ENABLE_MASTER);
4031 } else {
4032 tg3_ctrl &= (ADVERTISE_1000HALF | ADVERTISE_1000FULL);
4033 }
4034
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004035 if (tg3_ctrl != tgtadv)
4036 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004037 }
Matt Carlson93a700a2011-08-31 11:44:54 +00004038
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004039 return true;
Matt Carlsonef167e22007-12-20 20:10:01 -08004040}
4041
Matt Carlson859edb22011-12-08 14:40:16 +00004042static bool tg3_phy_copper_fetch_rmtadv(struct tg3 *tp, u32 *rmtadv)
4043{
4044 u32 lpeth = 0;
4045
4046 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
4047 u32 val;
4048
4049 if (tg3_readphy(tp, MII_STAT1000, &val))
4050 return false;
4051
4052 lpeth = mii_stat1000_to_ethtool_lpa_t(val);
4053 }
4054
4055 if (tg3_readphy(tp, MII_LPA, rmtadv))
4056 return false;
4057
4058 lpeth |= mii_lpa_to_ethtool_lpa_t(*rmtadv);
4059 tp->link_config.rmt_adv = lpeth;
4060
4061 return true;
4062}
4063
Linus Torvalds1da177e2005-04-16 15:20:36 -07004064static int tg3_setup_copper_phy(struct tg3 *tp, int force_reset)
4065{
4066 int current_link_up;
Matt Carlsonf833c4c2010-09-15 09:00:01 +00004067 u32 bmsr, val;
Matt Carlsonef167e22007-12-20 20:10:01 -08004068 u32 lcl_adv, rmt_adv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004069 u16 current_speed;
4070 u8 current_duplex;
4071 int i, err;
4072
4073 tw32(MAC_EVENT, 0);
4074
4075 tw32_f(MAC_STATUS,
4076 (MAC_STATUS_SYNC_CHANGED |
4077 MAC_STATUS_CFG_CHANGED |
4078 MAC_STATUS_MI_COMPLETION |
4079 MAC_STATUS_LNKSTATE_CHANGED));
4080 udelay(40);
4081
Matt Carlson8ef21422008-05-02 16:47:53 -07004082 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
4083 tw32_f(MAC_MI_MODE,
4084 (tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL));
4085 udelay(80);
4086 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004087
Matt Carlsonb4bd2922011-04-20 07:57:41 +00004088 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_PWRCTL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004089
4090 /* Some third-party PHYs need to be reset on link going
4091 * down.
4092 */
4093 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
4094 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
4095 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) &&
4096 netif_carrier_ok(tp->dev)) {
4097 tg3_readphy(tp, MII_BMSR, &bmsr);
4098 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
4099 !(bmsr & BMSR_LSTATUS))
4100 force_reset = 1;
4101 }
4102 if (force_reset)
4103 tg3_phy_reset(tp);
4104
Matt Carlson79eb6902010-02-17 15:17:03 +00004105 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004106 tg3_readphy(tp, MII_BMSR, &bmsr);
4107 if (tg3_readphy(tp, MII_BMSR, &bmsr) ||
Joe Perches63c3a662011-04-26 08:12:10 +00004108 !tg3_flag(tp, INIT_COMPLETE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004109 bmsr = 0;
4110
4111 if (!(bmsr & BMSR_LSTATUS)) {
4112 err = tg3_init_5401phy_dsp(tp);
4113 if (err)
4114 return err;
4115
4116 tg3_readphy(tp, MII_BMSR, &bmsr);
4117 for (i = 0; i < 1000; i++) {
4118 udelay(10);
4119 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
4120 (bmsr & BMSR_LSTATUS)) {
4121 udelay(40);
4122 break;
4123 }
4124 }
4125
Matt Carlson79eb6902010-02-17 15:17:03 +00004126 if ((tp->phy_id & TG3_PHY_ID_REV_MASK) ==
4127 TG3_PHY_REV_BCM5401_B0 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004128 !(bmsr & BMSR_LSTATUS) &&
4129 tp->link_config.active_speed == SPEED_1000) {
4130 err = tg3_phy_reset(tp);
4131 if (!err)
4132 err = tg3_init_5401phy_dsp(tp);
4133 if (err)
4134 return err;
4135 }
4136 }
4137 } else if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
4138 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0) {
4139 /* 5701 {A0,B0} CRC bug workaround */
4140 tg3_writephy(tp, 0x15, 0x0a75);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00004141 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8c68);
4142 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8d68);
4143 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8c68);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004144 }
4145
4146 /* Clear pending interrupts... */
Matt Carlsonf833c4c2010-09-15 09:00:01 +00004147 tg3_readphy(tp, MII_TG3_ISTAT, &val);
4148 tg3_readphy(tp, MII_TG3_ISTAT, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004149
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004150 if (tp->phy_flags & TG3_PHYFLG_USE_MI_INTERRUPT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004151 tg3_writephy(tp, MII_TG3_IMASK, ~MII_TG3_INT_LINKCHG);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004152 else if (!(tp->phy_flags & TG3_PHYFLG_IS_FET))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004153 tg3_writephy(tp, MII_TG3_IMASK, ~0);
4154
4155 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
4156 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
4157 if (tp->led_ctrl == LED_CTRL_MODE_PHY_1)
4158 tg3_writephy(tp, MII_TG3_EXT_CTRL,
4159 MII_TG3_EXT_CTRL_LNK3_LED_MODE);
4160 else
4161 tg3_writephy(tp, MII_TG3_EXT_CTRL, 0);
4162 }
4163
4164 current_link_up = 0;
Matt Carlsone7405222012-02-13 15:20:16 +00004165 current_speed = SPEED_UNKNOWN;
4166 current_duplex = DUPLEX_UNKNOWN;
Matt Carlsone348c5e2011-11-21 15:01:20 +00004167 tp->phy_flags &= ~TG3_PHYFLG_MDIX_STATE;
Matt Carlson859edb22011-12-08 14:40:16 +00004168 tp->link_config.rmt_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004169
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004170 if (tp->phy_flags & TG3_PHYFLG_CAPACITIVE_COUPLING) {
Matt Carlson15ee95c2011-04-20 07:57:40 +00004171 err = tg3_phy_auxctl_read(tp,
4172 MII_TG3_AUXCTL_SHDWSEL_MISCTEST,
4173 &val);
4174 if (!err && !(val & (1 << 10))) {
Matt Carlsonb4bd2922011-04-20 07:57:41 +00004175 tg3_phy_auxctl_write(tp,
4176 MII_TG3_AUXCTL_SHDWSEL_MISCTEST,
4177 val | (1 << 10));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004178 goto relink;
4179 }
4180 }
4181
4182 bmsr = 0;
4183 for (i = 0; i < 100; i++) {
4184 tg3_readphy(tp, MII_BMSR, &bmsr);
4185 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
4186 (bmsr & BMSR_LSTATUS))
4187 break;
4188 udelay(40);
4189 }
4190
4191 if (bmsr & BMSR_LSTATUS) {
4192 u32 aux_stat, bmcr;
4193
4194 tg3_readphy(tp, MII_TG3_AUX_STAT, &aux_stat);
4195 for (i = 0; i < 2000; i++) {
4196 udelay(10);
4197 if (!tg3_readphy(tp, MII_TG3_AUX_STAT, &aux_stat) &&
4198 aux_stat)
4199 break;
4200 }
4201
4202 tg3_aux_stat_to_speed_duplex(tp, aux_stat,
4203 &current_speed,
4204 &current_duplex);
4205
4206 bmcr = 0;
4207 for (i = 0; i < 200; i++) {
4208 tg3_readphy(tp, MII_BMCR, &bmcr);
4209 if (tg3_readphy(tp, MII_BMCR, &bmcr))
4210 continue;
4211 if (bmcr && bmcr != 0x7fff)
4212 break;
4213 udelay(10);
4214 }
4215
Matt Carlsonef167e22007-12-20 20:10:01 -08004216 lcl_adv = 0;
4217 rmt_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004218
Matt Carlsonef167e22007-12-20 20:10:01 -08004219 tp->link_config.active_speed = current_speed;
4220 tp->link_config.active_duplex = current_duplex;
4221
4222 if (tp->link_config.autoneg == AUTONEG_ENABLE) {
4223 if ((bmcr & BMCR_ANENABLE) &&
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004224 tg3_phy_copper_an_config_ok(tp, &lcl_adv) &&
Matt Carlson859edb22011-12-08 14:40:16 +00004225 tg3_phy_copper_fetch_rmtadv(tp, &rmt_adv))
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004226 current_link_up = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004227 } else {
4228 if (!(bmcr & BMCR_ANENABLE) &&
4229 tp->link_config.speed == current_speed &&
Matt Carlsonef167e22007-12-20 20:10:01 -08004230 tp->link_config.duplex == current_duplex &&
4231 tp->link_config.flowctrl ==
4232 tp->link_config.active_flowctrl) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004233 current_link_up = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004234 }
4235 }
4236
Matt Carlsonef167e22007-12-20 20:10:01 -08004237 if (current_link_up == 1 &&
Matt Carlsone348c5e2011-11-21 15:01:20 +00004238 tp->link_config.active_duplex == DUPLEX_FULL) {
4239 u32 reg, bit;
4240
4241 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
4242 reg = MII_TG3_FET_GEN_STAT;
4243 bit = MII_TG3_FET_GEN_STAT_MDIXSTAT;
4244 } else {
4245 reg = MII_TG3_EXT_STAT;
4246 bit = MII_TG3_EXT_STAT_MDIX;
4247 }
4248
4249 if (!tg3_readphy(tp, reg, &val) && (val & bit))
4250 tp->phy_flags |= TG3_PHYFLG_MDIX_STATE;
4251
Matt Carlsonef167e22007-12-20 20:10:01 -08004252 tg3_setup_flow_control(tp, lcl_adv, rmt_adv);
Matt Carlsone348c5e2011-11-21 15:01:20 +00004253 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004254 }
4255
Linus Torvalds1da177e2005-04-16 15:20:36 -07004256relink:
Matt Carlson800960682010-08-02 11:26:06 +00004257 if (current_link_up == 0 || (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004258 tg3_phy_copper_begin(tp);
4259
Matt Carlsonf833c4c2010-09-15 09:00:01 +00004260 tg3_readphy(tp, MII_BMSR, &bmsr);
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00004261 if ((!tg3_readphy(tp, MII_BMSR, &bmsr) && (bmsr & BMSR_LSTATUS)) ||
4262 (tp->mac_mode & MAC_MODE_PORT_INT_LPBACK))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004263 current_link_up = 1;
4264 }
4265
4266 tp->mac_mode &= ~MAC_MODE_PORT_MODE_MASK;
4267 if (current_link_up == 1) {
4268 if (tp->link_config.active_speed == SPEED_100 ||
4269 tp->link_config.active_speed == SPEED_10)
4270 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
4271 else
4272 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004273 } else if (tp->phy_flags & TG3_PHYFLG_IS_FET)
Matt Carlson7f97a4b2009-08-25 10:10:03 +00004274 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
4275 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004276 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
4277
4278 tp->mac_mode &= ~MAC_MODE_HALF_DUPLEX;
4279 if (tp->link_config.active_duplex == DUPLEX_HALF)
4280 tp->mac_mode |= MAC_MODE_HALF_DUPLEX;
4281
Linus Torvalds1da177e2005-04-16 15:20:36 -07004282 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700) {
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07004283 if (current_link_up == 1 &&
4284 tg3_5700_link_polarity(tp, tp->link_config.active_speed))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004285 tp->mac_mode |= MAC_MODE_LINK_POLARITY;
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07004286 else
4287 tp->mac_mode &= ~MAC_MODE_LINK_POLARITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004288 }
4289
4290 /* ??? Without this setting Netgear GA302T PHY does not
4291 * ??? send/receive packets...
4292 */
Matt Carlson79eb6902010-02-17 15:17:03 +00004293 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5411 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004294 tp->pci_chip_rev_id == CHIPREV_ID_5700_ALTIMA) {
4295 tp->mi_mode |= MAC_MI_MODE_AUTO_POLL;
4296 tw32_f(MAC_MI_MODE, tp->mi_mode);
4297 udelay(80);
4298 }
4299
4300 tw32_f(MAC_MODE, tp->mac_mode);
4301 udelay(40);
4302
Matt Carlson52b02d02010-10-14 10:37:41 +00004303 tg3_phy_eee_adjust(tp, current_link_up);
4304
Joe Perches63c3a662011-04-26 08:12:10 +00004305 if (tg3_flag(tp, USE_LINKCHG_REG)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004306 /* Polled via timer. */
4307 tw32_f(MAC_EVENT, 0);
4308 } else {
4309 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
4310 }
4311 udelay(40);
4312
4313 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 &&
4314 current_link_up == 1 &&
4315 tp->link_config.active_speed == SPEED_1000 &&
Joe Perches63c3a662011-04-26 08:12:10 +00004316 (tg3_flag(tp, PCIX_MODE) || tg3_flag(tp, PCI_HIGH_SPEED))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004317 udelay(120);
4318 tw32_f(MAC_STATUS,
4319 (MAC_STATUS_SYNC_CHANGED |
4320 MAC_STATUS_CFG_CHANGED));
4321 udelay(40);
4322 tg3_write_mem(tp,
4323 NIC_SRAM_FIRMWARE_MBOX,
4324 NIC_SRAM_FIRMWARE_MBOX_MAGIC2);
4325 }
4326
Matt Carlson5e7dfd02008-11-21 17:18:16 -08004327 /* Prevent send BD corruption. */
Joe Perches63c3a662011-04-26 08:12:10 +00004328 if (tg3_flag(tp, CLKREQ_BUG)) {
Matt Carlson5e7dfd02008-11-21 17:18:16 -08004329 u16 oldlnkctl, newlnkctl;
4330
4331 pci_read_config_word(tp->pdev,
Jon Mason708ebb32011-06-27 12:56:50 +00004332 pci_pcie_cap(tp->pdev) + PCI_EXP_LNKCTL,
Matt Carlson5e7dfd02008-11-21 17:18:16 -08004333 &oldlnkctl);
4334 if (tp->link_config.active_speed == SPEED_100 ||
4335 tp->link_config.active_speed == SPEED_10)
4336 newlnkctl = oldlnkctl & ~PCI_EXP_LNKCTL_CLKREQ_EN;
4337 else
4338 newlnkctl = oldlnkctl | PCI_EXP_LNKCTL_CLKREQ_EN;
4339 if (newlnkctl != oldlnkctl)
4340 pci_write_config_word(tp->pdev,
Matt Carlson93a700a2011-08-31 11:44:54 +00004341 pci_pcie_cap(tp->pdev) +
4342 PCI_EXP_LNKCTL, newlnkctl);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08004343 }
4344
Linus Torvalds1da177e2005-04-16 15:20:36 -07004345 if (current_link_up != netif_carrier_ok(tp->dev)) {
4346 if (current_link_up)
4347 netif_carrier_on(tp->dev);
4348 else
4349 netif_carrier_off(tp->dev);
4350 tg3_link_report(tp);
4351 }
4352
4353 return 0;
4354}
4355
4356struct tg3_fiber_aneginfo {
4357 int state;
4358#define ANEG_STATE_UNKNOWN 0
4359#define ANEG_STATE_AN_ENABLE 1
4360#define ANEG_STATE_RESTART_INIT 2
4361#define ANEG_STATE_RESTART 3
4362#define ANEG_STATE_DISABLE_LINK_OK 4
4363#define ANEG_STATE_ABILITY_DETECT_INIT 5
4364#define ANEG_STATE_ABILITY_DETECT 6
4365#define ANEG_STATE_ACK_DETECT_INIT 7
4366#define ANEG_STATE_ACK_DETECT 8
4367#define ANEG_STATE_COMPLETE_ACK_INIT 9
4368#define ANEG_STATE_COMPLETE_ACK 10
4369#define ANEG_STATE_IDLE_DETECT_INIT 11
4370#define ANEG_STATE_IDLE_DETECT 12
4371#define ANEG_STATE_LINK_OK 13
4372#define ANEG_STATE_NEXT_PAGE_WAIT_INIT 14
4373#define ANEG_STATE_NEXT_PAGE_WAIT 15
4374
4375 u32 flags;
4376#define MR_AN_ENABLE 0x00000001
4377#define MR_RESTART_AN 0x00000002
4378#define MR_AN_COMPLETE 0x00000004
4379#define MR_PAGE_RX 0x00000008
4380#define MR_NP_LOADED 0x00000010
4381#define MR_TOGGLE_TX 0x00000020
4382#define MR_LP_ADV_FULL_DUPLEX 0x00000040
4383#define MR_LP_ADV_HALF_DUPLEX 0x00000080
4384#define MR_LP_ADV_SYM_PAUSE 0x00000100
4385#define MR_LP_ADV_ASYM_PAUSE 0x00000200
4386#define MR_LP_ADV_REMOTE_FAULT1 0x00000400
4387#define MR_LP_ADV_REMOTE_FAULT2 0x00000800
4388#define MR_LP_ADV_NEXT_PAGE 0x00001000
4389#define MR_TOGGLE_RX 0x00002000
4390#define MR_NP_RX 0x00004000
4391
4392#define MR_LINK_OK 0x80000000
4393
4394 unsigned long link_time, cur_time;
4395
4396 u32 ability_match_cfg;
4397 int ability_match_count;
4398
4399 char ability_match, idle_match, ack_match;
4400
4401 u32 txconfig, rxconfig;
4402#define ANEG_CFG_NP 0x00000080
4403#define ANEG_CFG_ACK 0x00000040
4404#define ANEG_CFG_RF2 0x00000020
4405#define ANEG_CFG_RF1 0x00000010
4406#define ANEG_CFG_PS2 0x00000001
4407#define ANEG_CFG_PS1 0x00008000
4408#define ANEG_CFG_HD 0x00004000
4409#define ANEG_CFG_FD 0x00002000
4410#define ANEG_CFG_INVAL 0x00001f06
4411
4412};
4413#define ANEG_OK 0
4414#define ANEG_DONE 1
4415#define ANEG_TIMER_ENAB 2
4416#define ANEG_FAILED -1
4417
4418#define ANEG_STATE_SETTLE_TIME 10000
4419
4420static int tg3_fiber_aneg_smachine(struct tg3 *tp,
4421 struct tg3_fiber_aneginfo *ap)
4422{
Matt Carlson5be73b42007-12-20 20:09:29 -08004423 u16 flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004424 unsigned long delta;
4425 u32 rx_cfg_reg;
4426 int ret;
4427
4428 if (ap->state == ANEG_STATE_UNKNOWN) {
4429 ap->rxconfig = 0;
4430 ap->link_time = 0;
4431 ap->cur_time = 0;
4432 ap->ability_match_cfg = 0;
4433 ap->ability_match_count = 0;
4434 ap->ability_match = 0;
4435 ap->idle_match = 0;
4436 ap->ack_match = 0;
4437 }
4438 ap->cur_time++;
4439
4440 if (tr32(MAC_STATUS) & MAC_STATUS_RCVD_CFG) {
4441 rx_cfg_reg = tr32(MAC_RX_AUTO_NEG);
4442
4443 if (rx_cfg_reg != ap->ability_match_cfg) {
4444 ap->ability_match_cfg = rx_cfg_reg;
4445 ap->ability_match = 0;
4446 ap->ability_match_count = 0;
4447 } else {
4448 if (++ap->ability_match_count > 1) {
4449 ap->ability_match = 1;
4450 ap->ability_match_cfg = rx_cfg_reg;
4451 }
4452 }
4453 if (rx_cfg_reg & ANEG_CFG_ACK)
4454 ap->ack_match = 1;
4455 else
4456 ap->ack_match = 0;
4457
4458 ap->idle_match = 0;
4459 } else {
4460 ap->idle_match = 1;
4461 ap->ability_match_cfg = 0;
4462 ap->ability_match_count = 0;
4463 ap->ability_match = 0;
4464 ap->ack_match = 0;
4465
4466 rx_cfg_reg = 0;
4467 }
4468
4469 ap->rxconfig = rx_cfg_reg;
4470 ret = ANEG_OK;
4471
Matt Carlson33f401a2010-04-05 10:19:27 +00004472 switch (ap->state) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004473 case ANEG_STATE_UNKNOWN:
4474 if (ap->flags & (MR_AN_ENABLE | MR_RESTART_AN))
4475 ap->state = ANEG_STATE_AN_ENABLE;
4476
4477 /* fallthru */
4478 case ANEG_STATE_AN_ENABLE:
4479 ap->flags &= ~(MR_AN_COMPLETE | MR_PAGE_RX);
4480 if (ap->flags & MR_AN_ENABLE) {
4481 ap->link_time = 0;
4482 ap->cur_time = 0;
4483 ap->ability_match_cfg = 0;
4484 ap->ability_match_count = 0;
4485 ap->ability_match = 0;
4486 ap->idle_match = 0;
4487 ap->ack_match = 0;
4488
4489 ap->state = ANEG_STATE_RESTART_INIT;
4490 } else {
4491 ap->state = ANEG_STATE_DISABLE_LINK_OK;
4492 }
4493 break;
4494
4495 case ANEG_STATE_RESTART_INIT:
4496 ap->link_time = ap->cur_time;
4497 ap->flags &= ~(MR_NP_LOADED);
4498 ap->txconfig = 0;
4499 tw32(MAC_TX_AUTO_NEG, 0);
4500 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
4501 tw32_f(MAC_MODE, tp->mac_mode);
4502 udelay(40);
4503
4504 ret = ANEG_TIMER_ENAB;
4505 ap->state = ANEG_STATE_RESTART;
4506
4507 /* fallthru */
4508 case ANEG_STATE_RESTART:
4509 delta = ap->cur_time - ap->link_time;
Matt Carlson859a588792010-04-05 10:19:28 +00004510 if (delta > ANEG_STATE_SETTLE_TIME)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004511 ap->state = ANEG_STATE_ABILITY_DETECT_INIT;
Matt Carlson859a588792010-04-05 10:19:28 +00004512 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004513 ret = ANEG_TIMER_ENAB;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004514 break;
4515
4516 case ANEG_STATE_DISABLE_LINK_OK:
4517 ret = ANEG_DONE;
4518 break;
4519
4520 case ANEG_STATE_ABILITY_DETECT_INIT:
4521 ap->flags &= ~(MR_TOGGLE_TX);
Matt Carlson5be73b42007-12-20 20:09:29 -08004522 ap->txconfig = ANEG_CFG_FD;
4523 flowctrl = tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
4524 if (flowctrl & ADVERTISE_1000XPAUSE)
4525 ap->txconfig |= ANEG_CFG_PS1;
4526 if (flowctrl & ADVERTISE_1000XPSE_ASYM)
4527 ap->txconfig |= ANEG_CFG_PS2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004528 tw32(MAC_TX_AUTO_NEG, ap->txconfig);
4529 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
4530 tw32_f(MAC_MODE, tp->mac_mode);
4531 udelay(40);
4532
4533 ap->state = ANEG_STATE_ABILITY_DETECT;
4534 break;
4535
4536 case ANEG_STATE_ABILITY_DETECT:
Matt Carlson859a588792010-04-05 10:19:28 +00004537 if (ap->ability_match != 0 && ap->rxconfig != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004538 ap->state = ANEG_STATE_ACK_DETECT_INIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004539 break;
4540
4541 case ANEG_STATE_ACK_DETECT_INIT:
4542 ap->txconfig |= ANEG_CFG_ACK;
4543 tw32(MAC_TX_AUTO_NEG, ap->txconfig);
4544 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
4545 tw32_f(MAC_MODE, tp->mac_mode);
4546 udelay(40);
4547
4548 ap->state = ANEG_STATE_ACK_DETECT;
4549
4550 /* fallthru */
4551 case ANEG_STATE_ACK_DETECT:
4552 if (ap->ack_match != 0) {
4553 if ((ap->rxconfig & ~ANEG_CFG_ACK) ==
4554 (ap->ability_match_cfg & ~ANEG_CFG_ACK)) {
4555 ap->state = ANEG_STATE_COMPLETE_ACK_INIT;
4556 } else {
4557 ap->state = ANEG_STATE_AN_ENABLE;
4558 }
4559 } else if (ap->ability_match != 0 &&
4560 ap->rxconfig == 0) {
4561 ap->state = ANEG_STATE_AN_ENABLE;
4562 }
4563 break;
4564
4565 case ANEG_STATE_COMPLETE_ACK_INIT:
4566 if (ap->rxconfig & ANEG_CFG_INVAL) {
4567 ret = ANEG_FAILED;
4568 break;
4569 }
4570 ap->flags &= ~(MR_LP_ADV_FULL_DUPLEX |
4571 MR_LP_ADV_HALF_DUPLEX |
4572 MR_LP_ADV_SYM_PAUSE |
4573 MR_LP_ADV_ASYM_PAUSE |
4574 MR_LP_ADV_REMOTE_FAULT1 |
4575 MR_LP_ADV_REMOTE_FAULT2 |
4576 MR_LP_ADV_NEXT_PAGE |
4577 MR_TOGGLE_RX |
4578 MR_NP_RX);
4579 if (ap->rxconfig & ANEG_CFG_FD)
4580 ap->flags |= MR_LP_ADV_FULL_DUPLEX;
4581 if (ap->rxconfig & ANEG_CFG_HD)
4582 ap->flags |= MR_LP_ADV_HALF_DUPLEX;
4583 if (ap->rxconfig & ANEG_CFG_PS1)
4584 ap->flags |= MR_LP_ADV_SYM_PAUSE;
4585 if (ap->rxconfig & ANEG_CFG_PS2)
4586 ap->flags |= MR_LP_ADV_ASYM_PAUSE;
4587 if (ap->rxconfig & ANEG_CFG_RF1)
4588 ap->flags |= MR_LP_ADV_REMOTE_FAULT1;
4589 if (ap->rxconfig & ANEG_CFG_RF2)
4590 ap->flags |= MR_LP_ADV_REMOTE_FAULT2;
4591 if (ap->rxconfig & ANEG_CFG_NP)
4592 ap->flags |= MR_LP_ADV_NEXT_PAGE;
4593
4594 ap->link_time = ap->cur_time;
4595
4596 ap->flags ^= (MR_TOGGLE_TX);
4597 if (ap->rxconfig & 0x0008)
4598 ap->flags |= MR_TOGGLE_RX;
4599 if (ap->rxconfig & ANEG_CFG_NP)
4600 ap->flags |= MR_NP_RX;
4601 ap->flags |= MR_PAGE_RX;
4602
4603 ap->state = ANEG_STATE_COMPLETE_ACK;
4604 ret = ANEG_TIMER_ENAB;
4605 break;
4606
4607 case ANEG_STATE_COMPLETE_ACK:
4608 if (ap->ability_match != 0 &&
4609 ap->rxconfig == 0) {
4610 ap->state = ANEG_STATE_AN_ENABLE;
4611 break;
4612 }
4613 delta = ap->cur_time - ap->link_time;
4614 if (delta > ANEG_STATE_SETTLE_TIME) {
4615 if (!(ap->flags & (MR_LP_ADV_NEXT_PAGE))) {
4616 ap->state = ANEG_STATE_IDLE_DETECT_INIT;
4617 } else {
4618 if ((ap->txconfig & ANEG_CFG_NP) == 0 &&
4619 !(ap->flags & MR_NP_RX)) {
4620 ap->state = ANEG_STATE_IDLE_DETECT_INIT;
4621 } else {
4622 ret = ANEG_FAILED;
4623 }
4624 }
4625 }
4626 break;
4627
4628 case ANEG_STATE_IDLE_DETECT_INIT:
4629 ap->link_time = ap->cur_time;
4630 tp->mac_mode &= ~MAC_MODE_SEND_CONFIGS;
4631 tw32_f(MAC_MODE, tp->mac_mode);
4632 udelay(40);
4633
4634 ap->state = ANEG_STATE_IDLE_DETECT;
4635 ret = ANEG_TIMER_ENAB;
4636 break;
4637
4638 case ANEG_STATE_IDLE_DETECT:
4639 if (ap->ability_match != 0 &&
4640 ap->rxconfig == 0) {
4641 ap->state = ANEG_STATE_AN_ENABLE;
4642 break;
4643 }
4644 delta = ap->cur_time - ap->link_time;
4645 if (delta > ANEG_STATE_SETTLE_TIME) {
4646 /* XXX another gem from the Broadcom driver :( */
4647 ap->state = ANEG_STATE_LINK_OK;
4648 }
4649 break;
4650
4651 case ANEG_STATE_LINK_OK:
4652 ap->flags |= (MR_AN_COMPLETE | MR_LINK_OK);
4653 ret = ANEG_DONE;
4654 break;
4655
4656 case ANEG_STATE_NEXT_PAGE_WAIT_INIT:
4657 /* ??? unimplemented */
4658 break;
4659
4660 case ANEG_STATE_NEXT_PAGE_WAIT:
4661 /* ??? unimplemented */
4662 break;
4663
4664 default:
4665 ret = ANEG_FAILED;
4666 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07004667 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004668
4669 return ret;
4670}
4671
Matt Carlson5be73b42007-12-20 20:09:29 -08004672static int fiber_autoneg(struct tg3 *tp, u32 *txflags, u32 *rxflags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004673{
4674 int res = 0;
4675 struct tg3_fiber_aneginfo aninfo;
4676 int status = ANEG_FAILED;
4677 unsigned int tick;
4678 u32 tmp;
4679
4680 tw32_f(MAC_TX_AUTO_NEG, 0);
4681
4682 tmp = tp->mac_mode & ~MAC_MODE_PORT_MODE_MASK;
4683 tw32_f(MAC_MODE, tmp | MAC_MODE_PORT_MODE_GMII);
4684 udelay(40);
4685
4686 tw32_f(MAC_MODE, tp->mac_mode | MAC_MODE_SEND_CONFIGS);
4687 udelay(40);
4688
4689 memset(&aninfo, 0, sizeof(aninfo));
4690 aninfo.flags |= MR_AN_ENABLE;
4691 aninfo.state = ANEG_STATE_UNKNOWN;
4692 aninfo.cur_time = 0;
4693 tick = 0;
4694 while (++tick < 195000) {
4695 status = tg3_fiber_aneg_smachine(tp, &aninfo);
4696 if (status == ANEG_DONE || status == ANEG_FAILED)
4697 break;
4698
4699 udelay(1);
4700 }
4701
4702 tp->mac_mode &= ~MAC_MODE_SEND_CONFIGS;
4703 tw32_f(MAC_MODE, tp->mac_mode);
4704 udelay(40);
4705
Matt Carlson5be73b42007-12-20 20:09:29 -08004706 *txflags = aninfo.txconfig;
4707 *rxflags = aninfo.flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004708
4709 if (status == ANEG_DONE &&
4710 (aninfo.flags & (MR_AN_COMPLETE | MR_LINK_OK |
4711 MR_LP_ADV_FULL_DUPLEX)))
4712 res = 1;
4713
4714 return res;
4715}
4716
4717static void tg3_init_bcm8002(struct tg3 *tp)
4718{
4719 u32 mac_status = tr32(MAC_STATUS);
4720 int i;
4721
4722 /* Reset when initting first time or we have a link. */
Joe Perches63c3a662011-04-26 08:12:10 +00004723 if (tg3_flag(tp, INIT_COMPLETE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004724 !(mac_status & MAC_STATUS_PCS_SYNCED))
4725 return;
4726
4727 /* Set PLL lock range. */
4728 tg3_writephy(tp, 0x16, 0x8007);
4729
4730 /* SW reset */
4731 tg3_writephy(tp, MII_BMCR, BMCR_RESET);
4732
4733 /* Wait for reset to complete. */
4734 /* XXX schedule_timeout() ... */
4735 for (i = 0; i < 500; i++)
4736 udelay(10);
4737
4738 /* Config mode; select PMA/Ch 1 regs. */
4739 tg3_writephy(tp, 0x10, 0x8411);
4740
4741 /* Enable auto-lock and comdet, select txclk for tx. */
4742 tg3_writephy(tp, 0x11, 0x0a10);
4743
4744 tg3_writephy(tp, 0x18, 0x00a0);
4745 tg3_writephy(tp, 0x16, 0x41ff);
4746
4747 /* Assert and deassert POR. */
4748 tg3_writephy(tp, 0x13, 0x0400);
4749 udelay(40);
4750 tg3_writephy(tp, 0x13, 0x0000);
4751
4752 tg3_writephy(tp, 0x11, 0x0a50);
4753 udelay(40);
4754 tg3_writephy(tp, 0x11, 0x0a10);
4755
4756 /* Wait for signal to stabilize */
4757 /* XXX schedule_timeout() ... */
4758 for (i = 0; i < 15000; i++)
4759 udelay(10);
4760
4761 /* Deselect the channel register so we can read the PHYID
4762 * later.
4763 */
4764 tg3_writephy(tp, 0x10, 0x8011);
4765}
4766
4767static int tg3_setup_fiber_hw_autoneg(struct tg3 *tp, u32 mac_status)
4768{
Matt Carlson82cd3d12007-12-20 20:09:00 -08004769 u16 flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004770 u32 sg_dig_ctrl, sg_dig_status;
4771 u32 serdes_cfg, expected_sg_dig_ctrl;
4772 int workaround, port_a;
4773 int current_link_up;
4774
4775 serdes_cfg = 0;
4776 expected_sg_dig_ctrl = 0;
4777 workaround = 0;
4778 port_a = 1;
4779 current_link_up = 0;
4780
4781 if (tp->pci_chip_rev_id != CHIPREV_ID_5704_A0 &&
4782 tp->pci_chip_rev_id != CHIPREV_ID_5704_A1) {
4783 workaround = 1;
4784 if (tr32(TG3PCI_DUAL_MAC_CTRL) & DUAL_MAC_CTRL_ID)
4785 port_a = 0;
4786
4787 /* preserve bits 0-11,13,14 for signal pre-emphasis */
4788 /* preserve bits 20-23 for voltage regulator */
4789 serdes_cfg = tr32(MAC_SERDES_CFG) & 0x00f06fff;
4790 }
4791
4792 sg_dig_ctrl = tr32(SG_DIG_CTRL);
4793
4794 if (tp->link_config.autoneg != AUTONEG_ENABLE) {
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004795 if (sg_dig_ctrl & SG_DIG_USING_HW_AUTONEG) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004796 if (workaround) {
4797 u32 val = serdes_cfg;
4798
4799 if (port_a)
4800 val |= 0xc010000;
4801 else
4802 val |= 0x4010000;
4803 tw32_f(MAC_SERDES_CFG, val);
4804 }
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004805
4806 tw32_f(SG_DIG_CTRL, SG_DIG_COMMON_SETUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004807 }
4808 if (mac_status & MAC_STATUS_PCS_SYNCED) {
4809 tg3_setup_flow_control(tp, 0, 0);
4810 current_link_up = 1;
4811 }
4812 goto out;
4813 }
4814
4815 /* Want auto-negotiation. */
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004816 expected_sg_dig_ctrl = SG_DIG_USING_HW_AUTONEG | SG_DIG_COMMON_SETUP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004817
Matt Carlson82cd3d12007-12-20 20:09:00 -08004818 flowctrl = tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
4819 if (flowctrl & ADVERTISE_1000XPAUSE)
4820 expected_sg_dig_ctrl |= SG_DIG_PAUSE_CAP;
4821 if (flowctrl & ADVERTISE_1000XPSE_ASYM)
4822 expected_sg_dig_ctrl |= SG_DIG_ASYM_PAUSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004823
4824 if (sg_dig_ctrl != expected_sg_dig_ctrl) {
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004825 if ((tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT) &&
Michael Chan3d3ebe72006-09-27 15:59:15 -07004826 tp->serdes_counter &&
4827 ((mac_status & (MAC_STATUS_PCS_SYNCED |
4828 MAC_STATUS_RCVD_CFG)) ==
4829 MAC_STATUS_PCS_SYNCED)) {
4830 tp->serdes_counter--;
4831 current_link_up = 1;
4832 goto out;
4833 }
4834restart_autoneg:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004835 if (workaround)
4836 tw32_f(MAC_SERDES_CFG, serdes_cfg | 0xc011000);
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004837 tw32_f(SG_DIG_CTRL, expected_sg_dig_ctrl | SG_DIG_SOFT_RESET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004838 udelay(5);
4839 tw32_f(SG_DIG_CTRL, expected_sg_dig_ctrl);
4840
Michael Chan3d3ebe72006-09-27 15:59:15 -07004841 tp->serdes_counter = SERDES_AN_TIMEOUT_5704S;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004842 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004843 } else if (mac_status & (MAC_STATUS_PCS_SYNCED |
4844 MAC_STATUS_SIGNAL_DET)) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07004845 sg_dig_status = tr32(SG_DIG_STATUS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004846 mac_status = tr32(MAC_STATUS);
4847
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004848 if ((sg_dig_status & SG_DIG_AUTONEG_COMPLETE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004849 (mac_status & MAC_STATUS_PCS_SYNCED)) {
Matt Carlson82cd3d12007-12-20 20:09:00 -08004850 u32 local_adv = 0, remote_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004851
Matt Carlson82cd3d12007-12-20 20:09:00 -08004852 if (sg_dig_ctrl & SG_DIG_PAUSE_CAP)
4853 local_adv |= ADVERTISE_1000XPAUSE;
4854 if (sg_dig_ctrl & SG_DIG_ASYM_PAUSE)
4855 local_adv |= ADVERTISE_1000XPSE_ASYM;
4856
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004857 if (sg_dig_status & SG_DIG_PARTNER_PAUSE_CAPABLE)
Matt Carlson82cd3d12007-12-20 20:09:00 -08004858 remote_adv |= LPA_1000XPAUSE;
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004859 if (sg_dig_status & SG_DIG_PARTNER_ASYM_PAUSE)
Matt Carlson82cd3d12007-12-20 20:09:00 -08004860 remote_adv |= LPA_1000XPAUSE_ASYM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004861
Matt Carlson859edb22011-12-08 14:40:16 +00004862 tp->link_config.rmt_adv =
4863 mii_adv_to_ethtool_adv_x(remote_adv);
4864
Linus Torvalds1da177e2005-04-16 15:20:36 -07004865 tg3_setup_flow_control(tp, local_adv, remote_adv);
4866 current_link_up = 1;
Michael Chan3d3ebe72006-09-27 15:59:15 -07004867 tp->serdes_counter = 0;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004868 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004869 } else if (!(sg_dig_status & SG_DIG_AUTONEG_COMPLETE)) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07004870 if (tp->serdes_counter)
4871 tp->serdes_counter--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004872 else {
4873 if (workaround) {
4874 u32 val = serdes_cfg;
4875
4876 if (port_a)
4877 val |= 0xc010000;
4878 else
4879 val |= 0x4010000;
4880
4881 tw32_f(MAC_SERDES_CFG, val);
4882 }
4883
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004884 tw32_f(SG_DIG_CTRL, SG_DIG_COMMON_SETUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004885 udelay(40);
4886
4887 /* Link parallel detection - link is up */
4888 /* only if we have PCS_SYNC and not */
4889 /* receiving config code words */
4890 mac_status = tr32(MAC_STATUS);
4891 if ((mac_status & MAC_STATUS_PCS_SYNCED) &&
4892 !(mac_status & MAC_STATUS_RCVD_CFG)) {
4893 tg3_setup_flow_control(tp, 0, 0);
4894 current_link_up = 1;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004895 tp->phy_flags |=
4896 TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan3d3ebe72006-09-27 15:59:15 -07004897 tp->serdes_counter =
4898 SERDES_PARALLEL_DET_TIMEOUT;
4899 } else
4900 goto restart_autoneg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004901 }
4902 }
Michael Chan3d3ebe72006-09-27 15:59:15 -07004903 } else {
4904 tp->serdes_counter = SERDES_AN_TIMEOUT_5704S;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004905 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004906 }
4907
4908out:
4909 return current_link_up;
4910}
4911
4912static int tg3_setup_fiber_by_hand(struct tg3 *tp, u32 mac_status)
4913{
4914 int current_link_up = 0;
4915
Michael Chan5cf64b8a2007-05-05 12:11:21 -07004916 if (!(mac_status & MAC_STATUS_PCS_SYNCED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004917 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004918
4919 if (tp->link_config.autoneg == AUTONEG_ENABLE) {
Matt Carlson5be73b42007-12-20 20:09:29 -08004920 u32 txflags, rxflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004921 int i;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04004922
Matt Carlson5be73b42007-12-20 20:09:29 -08004923 if (fiber_autoneg(tp, &txflags, &rxflags)) {
4924 u32 local_adv = 0, remote_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004925
Matt Carlson5be73b42007-12-20 20:09:29 -08004926 if (txflags & ANEG_CFG_PS1)
4927 local_adv |= ADVERTISE_1000XPAUSE;
4928 if (txflags & ANEG_CFG_PS2)
4929 local_adv |= ADVERTISE_1000XPSE_ASYM;
4930
4931 if (rxflags & MR_LP_ADV_SYM_PAUSE)
4932 remote_adv |= LPA_1000XPAUSE;
4933 if (rxflags & MR_LP_ADV_ASYM_PAUSE)
4934 remote_adv |= LPA_1000XPAUSE_ASYM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004935
Matt Carlson859edb22011-12-08 14:40:16 +00004936 tp->link_config.rmt_adv =
4937 mii_adv_to_ethtool_adv_x(remote_adv);
4938
Linus Torvalds1da177e2005-04-16 15:20:36 -07004939 tg3_setup_flow_control(tp, local_adv, remote_adv);
4940
Linus Torvalds1da177e2005-04-16 15:20:36 -07004941 current_link_up = 1;
4942 }
4943 for (i = 0; i < 30; i++) {
4944 udelay(20);
4945 tw32_f(MAC_STATUS,
4946 (MAC_STATUS_SYNC_CHANGED |
4947 MAC_STATUS_CFG_CHANGED));
4948 udelay(40);
4949 if ((tr32(MAC_STATUS) &
4950 (MAC_STATUS_SYNC_CHANGED |
4951 MAC_STATUS_CFG_CHANGED)) == 0)
4952 break;
4953 }
4954
4955 mac_status = tr32(MAC_STATUS);
4956 if (current_link_up == 0 &&
4957 (mac_status & MAC_STATUS_PCS_SYNCED) &&
4958 !(mac_status & MAC_STATUS_RCVD_CFG))
4959 current_link_up = 1;
4960 } else {
Matt Carlson5be73b42007-12-20 20:09:29 -08004961 tg3_setup_flow_control(tp, 0, 0);
4962
Linus Torvalds1da177e2005-04-16 15:20:36 -07004963 /* Forcing 1000FD link up. */
4964 current_link_up = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004965
4966 tw32_f(MAC_MODE, (tp->mac_mode | MAC_MODE_SEND_CONFIGS));
4967 udelay(40);
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07004968
4969 tw32_f(MAC_MODE, tp->mac_mode);
4970 udelay(40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004971 }
4972
4973out:
4974 return current_link_up;
4975}
4976
4977static int tg3_setup_fiber_phy(struct tg3 *tp, int force_reset)
4978{
4979 u32 orig_pause_cfg;
4980 u16 orig_active_speed;
4981 u8 orig_active_duplex;
4982 u32 mac_status;
4983 int current_link_up;
4984 int i;
4985
Matt Carlson8d018622007-12-20 20:05:44 -08004986 orig_pause_cfg = tp->link_config.active_flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004987 orig_active_speed = tp->link_config.active_speed;
4988 orig_active_duplex = tp->link_config.active_duplex;
4989
Joe Perches63c3a662011-04-26 08:12:10 +00004990 if (!tg3_flag(tp, HW_AUTONEG) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004991 netif_carrier_ok(tp->dev) &&
Joe Perches63c3a662011-04-26 08:12:10 +00004992 tg3_flag(tp, INIT_COMPLETE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004993 mac_status = tr32(MAC_STATUS);
4994 mac_status &= (MAC_STATUS_PCS_SYNCED |
4995 MAC_STATUS_SIGNAL_DET |
4996 MAC_STATUS_CFG_CHANGED |
4997 MAC_STATUS_RCVD_CFG);
4998 if (mac_status == (MAC_STATUS_PCS_SYNCED |
4999 MAC_STATUS_SIGNAL_DET)) {
5000 tw32_f(MAC_STATUS, (MAC_STATUS_SYNC_CHANGED |
5001 MAC_STATUS_CFG_CHANGED));
5002 return 0;
5003 }
5004 }
5005
5006 tw32_f(MAC_TX_AUTO_NEG, 0);
5007
5008 tp->mac_mode &= ~(MAC_MODE_PORT_MODE_MASK | MAC_MODE_HALF_DUPLEX);
5009 tp->mac_mode |= MAC_MODE_PORT_MODE_TBI;
5010 tw32_f(MAC_MODE, tp->mac_mode);
5011 udelay(40);
5012
Matt Carlson79eb6902010-02-17 15:17:03 +00005013 if (tp->phy_id == TG3_PHY_ID_BCM8002)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005014 tg3_init_bcm8002(tp);
5015
5016 /* Enable link change event even when serdes polling. */
5017 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
5018 udelay(40);
5019
5020 current_link_up = 0;
Matt Carlson859edb22011-12-08 14:40:16 +00005021 tp->link_config.rmt_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005022 mac_status = tr32(MAC_STATUS);
5023
Joe Perches63c3a662011-04-26 08:12:10 +00005024 if (tg3_flag(tp, HW_AUTONEG))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005025 current_link_up = tg3_setup_fiber_hw_autoneg(tp, mac_status);
5026 else
5027 current_link_up = tg3_setup_fiber_by_hand(tp, mac_status);
5028
Matt Carlson898a56f2009-08-28 14:02:40 +00005029 tp->napi[0].hw_status->status =
Linus Torvalds1da177e2005-04-16 15:20:36 -07005030 (SD_STATUS_UPDATED |
Matt Carlson898a56f2009-08-28 14:02:40 +00005031 (tp->napi[0].hw_status->status & ~SD_STATUS_LINK_CHG));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005032
5033 for (i = 0; i < 100; i++) {
5034 tw32_f(MAC_STATUS, (MAC_STATUS_SYNC_CHANGED |
5035 MAC_STATUS_CFG_CHANGED));
5036 udelay(5);
5037 if ((tr32(MAC_STATUS) & (MAC_STATUS_SYNC_CHANGED |
Michael Chan3d3ebe72006-09-27 15:59:15 -07005038 MAC_STATUS_CFG_CHANGED |
5039 MAC_STATUS_LNKSTATE_CHANGED)) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005040 break;
5041 }
5042
5043 mac_status = tr32(MAC_STATUS);
5044 if ((mac_status & MAC_STATUS_PCS_SYNCED) == 0) {
5045 current_link_up = 0;
Michael Chan3d3ebe72006-09-27 15:59:15 -07005046 if (tp->link_config.autoneg == AUTONEG_ENABLE &&
5047 tp->serdes_counter == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005048 tw32_f(MAC_MODE, (tp->mac_mode |
5049 MAC_MODE_SEND_CONFIGS));
5050 udelay(1);
5051 tw32_f(MAC_MODE, tp->mac_mode);
5052 }
5053 }
5054
5055 if (current_link_up == 1) {
5056 tp->link_config.active_speed = SPEED_1000;
5057 tp->link_config.active_duplex = DUPLEX_FULL;
5058 tw32(MAC_LED_CTRL, (tp->led_ctrl |
5059 LED_CTRL_LNKLED_OVERRIDE |
5060 LED_CTRL_1000MBPS_ON));
5061 } else {
Matt Carlsone7405222012-02-13 15:20:16 +00005062 tp->link_config.active_speed = SPEED_UNKNOWN;
5063 tp->link_config.active_duplex = DUPLEX_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005064 tw32(MAC_LED_CTRL, (tp->led_ctrl |
5065 LED_CTRL_LNKLED_OVERRIDE |
5066 LED_CTRL_TRAFFIC_OVERRIDE));
5067 }
5068
5069 if (current_link_up != netif_carrier_ok(tp->dev)) {
5070 if (current_link_up)
5071 netif_carrier_on(tp->dev);
5072 else
5073 netif_carrier_off(tp->dev);
5074 tg3_link_report(tp);
5075 } else {
Matt Carlson8d018622007-12-20 20:05:44 -08005076 u32 now_pause_cfg = tp->link_config.active_flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005077 if (orig_pause_cfg != now_pause_cfg ||
5078 orig_active_speed != tp->link_config.active_speed ||
5079 orig_active_duplex != tp->link_config.active_duplex)
5080 tg3_link_report(tp);
5081 }
5082
5083 return 0;
5084}
5085
Michael Chan747e8f82005-07-25 12:33:22 -07005086static int tg3_setup_fiber_mii_phy(struct tg3 *tp, int force_reset)
5087{
5088 int current_link_up, err = 0;
5089 u32 bmsr, bmcr;
5090 u16 current_speed;
5091 u8 current_duplex;
Matt Carlsonef167e22007-12-20 20:10:01 -08005092 u32 local_adv, remote_adv;
Michael Chan747e8f82005-07-25 12:33:22 -07005093
5094 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
5095 tw32_f(MAC_MODE, tp->mac_mode);
5096 udelay(40);
5097
5098 tw32(MAC_EVENT, 0);
5099
5100 tw32_f(MAC_STATUS,
5101 (MAC_STATUS_SYNC_CHANGED |
5102 MAC_STATUS_CFG_CHANGED |
5103 MAC_STATUS_MI_COMPLETION |
5104 MAC_STATUS_LNKSTATE_CHANGED));
5105 udelay(40);
5106
5107 if (force_reset)
5108 tg3_phy_reset(tp);
5109
5110 current_link_up = 0;
Matt Carlsone7405222012-02-13 15:20:16 +00005111 current_speed = SPEED_UNKNOWN;
5112 current_duplex = DUPLEX_UNKNOWN;
Matt Carlson859edb22011-12-08 14:40:16 +00005113 tp->link_config.rmt_adv = 0;
Michael Chan747e8f82005-07-25 12:33:22 -07005114
5115 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
5116 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
Michael Chand4d2c552006-03-20 17:47:20 -08005117 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
5118 if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
5119 bmsr |= BMSR_LSTATUS;
5120 else
5121 bmsr &= ~BMSR_LSTATUS;
5122 }
Michael Chan747e8f82005-07-25 12:33:22 -07005123
5124 err |= tg3_readphy(tp, MII_BMCR, &bmcr);
5125
5126 if ((tp->link_config.autoneg == AUTONEG_ENABLE) && !force_reset &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005127 (tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT)) {
Michael Chan747e8f82005-07-25 12:33:22 -07005128 /* do nothing, just check for link up at the end */
5129 } else if (tp->link_config.autoneg == AUTONEG_ENABLE) {
Matt Carlson28011cf2011-11-16 18:36:59 -05005130 u32 adv, newadv;
Michael Chan747e8f82005-07-25 12:33:22 -07005131
5132 err |= tg3_readphy(tp, MII_ADVERTISE, &adv);
Matt Carlson28011cf2011-11-16 18:36:59 -05005133 newadv = adv & ~(ADVERTISE_1000XFULL | ADVERTISE_1000XHALF |
5134 ADVERTISE_1000XPAUSE |
5135 ADVERTISE_1000XPSE_ASYM |
5136 ADVERTISE_SLCT);
Michael Chan747e8f82005-07-25 12:33:22 -07005137
Matt Carlson28011cf2011-11-16 18:36:59 -05005138 newadv |= tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
Matt Carlson37f07022011-11-17 14:30:55 +00005139 newadv |= ethtool_adv_to_mii_adv_x(tp->link_config.advertising);
Michael Chan747e8f82005-07-25 12:33:22 -07005140
Matt Carlson28011cf2011-11-16 18:36:59 -05005141 if ((newadv != adv) || !(bmcr & BMCR_ANENABLE)) {
5142 tg3_writephy(tp, MII_ADVERTISE, newadv);
Michael Chan747e8f82005-07-25 12:33:22 -07005143 bmcr |= BMCR_ANENABLE | BMCR_ANRESTART;
5144 tg3_writephy(tp, MII_BMCR, bmcr);
5145
5146 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
Michael Chan3d3ebe72006-09-27 15:59:15 -07005147 tp->serdes_counter = SERDES_AN_TIMEOUT_5714S;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005148 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07005149
5150 return err;
5151 }
5152 } else {
5153 u32 new_bmcr;
5154
5155 bmcr &= ~BMCR_SPEED1000;
5156 new_bmcr = bmcr & ~(BMCR_ANENABLE | BMCR_FULLDPLX);
5157
5158 if (tp->link_config.duplex == DUPLEX_FULL)
5159 new_bmcr |= BMCR_FULLDPLX;
5160
5161 if (new_bmcr != bmcr) {
5162 /* BMCR_SPEED1000 is a reserved bit that needs
5163 * to be set on write.
5164 */
5165 new_bmcr |= BMCR_SPEED1000;
5166
5167 /* Force a linkdown */
5168 if (netif_carrier_ok(tp->dev)) {
5169 u32 adv;
5170
5171 err |= tg3_readphy(tp, MII_ADVERTISE, &adv);
5172 adv &= ~(ADVERTISE_1000XFULL |
5173 ADVERTISE_1000XHALF |
5174 ADVERTISE_SLCT);
5175 tg3_writephy(tp, MII_ADVERTISE, adv);
5176 tg3_writephy(tp, MII_BMCR, bmcr |
5177 BMCR_ANRESTART |
5178 BMCR_ANENABLE);
5179 udelay(10);
5180 netif_carrier_off(tp->dev);
5181 }
5182 tg3_writephy(tp, MII_BMCR, new_bmcr);
5183 bmcr = new_bmcr;
5184 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
5185 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
Michael Chand4d2c552006-03-20 17:47:20 -08005186 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
5187 ASIC_REV_5714) {
5188 if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
5189 bmsr |= BMSR_LSTATUS;
5190 else
5191 bmsr &= ~BMSR_LSTATUS;
5192 }
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005193 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07005194 }
5195 }
5196
5197 if (bmsr & BMSR_LSTATUS) {
5198 current_speed = SPEED_1000;
5199 current_link_up = 1;
5200 if (bmcr & BMCR_FULLDPLX)
5201 current_duplex = DUPLEX_FULL;
5202 else
5203 current_duplex = DUPLEX_HALF;
5204
Matt Carlsonef167e22007-12-20 20:10:01 -08005205 local_adv = 0;
5206 remote_adv = 0;
5207
Michael Chan747e8f82005-07-25 12:33:22 -07005208 if (bmcr & BMCR_ANENABLE) {
Matt Carlsonef167e22007-12-20 20:10:01 -08005209 u32 common;
Michael Chan747e8f82005-07-25 12:33:22 -07005210
5211 err |= tg3_readphy(tp, MII_ADVERTISE, &local_adv);
5212 err |= tg3_readphy(tp, MII_LPA, &remote_adv);
5213 common = local_adv & remote_adv;
5214 if (common & (ADVERTISE_1000XHALF |
5215 ADVERTISE_1000XFULL)) {
5216 if (common & ADVERTISE_1000XFULL)
5217 current_duplex = DUPLEX_FULL;
5218 else
5219 current_duplex = DUPLEX_HALF;
Matt Carlson859edb22011-12-08 14:40:16 +00005220
5221 tp->link_config.rmt_adv =
5222 mii_adv_to_ethtool_adv_x(remote_adv);
Joe Perches63c3a662011-04-26 08:12:10 +00005223 } else if (!tg3_flag(tp, 5780_CLASS)) {
Matt Carlson57d8b882010-06-05 17:24:35 +00005224 /* Link is up via parallel detect */
Matt Carlson859a588792010-04-05 10:19:28 +00005225 } else {
Michael Chan747e8f82005-07-25 12:33:22 -07005226 current_link_up = 0;
Matt Carlson859a588792010-04-05 10:19:28 +00005227 }
Michael Chan747e8f82005-07-25 12:33:22 -07005228 }
5229 }
5230
Matt Carlsonef167e22007-12-20 20:10:01 -08005231 if (current_link_up == 1 && current_duplex == DUPLEX_FULL)
5232 tg3_setup_flow_control(tp, local_adv, remote_adv);
5233
Michael Chan747e8f82005-07-25 12:33:22 -07005234 tp->mac_mode &= ~MAC_MODE_HALF_DUPLEX;
5235 if (tp->link_config.active_duplex == DUPLEX_HALF)
5236 tp->mac_mode |= MAC_MODE_HALF_DUPLEX;
5237
5238 tw32_f(MAC_MODE, tp->mac_mode);
5239 udelay(40);
5240
5241 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
5242
5243 tp->link_config.active_speed = current_speed;
5244 tp->link_config.active_duplex = current_duplex;
5245
5246 if (current_link_up != netif_carrier_ok(tp->dev)) {
5247 if (current_link_up)
5248 netif_carrier_on(tp->dev);
5249 else {
5250 netif_carrier_off(tp->dev);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005251 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07005252 }
5253 tg3_link_report(tp);
5254 }
5255 return err;
5256}
5257
5258static void tg3_serdes_parallel_detect(struct tg3 *tp)
5259{
Michael Chan3d3ebe72006-09-27 15:59:15 -07005260 if (tp->serdes_counter) {
Michael Chan747e8f82005-07-25 12:33:22 -07005261 /* Give autoneg time to complete. */
Michael Chan3d3ebe72006-09-27 15:59:15 -07005262 tp->serdes_counter--;
Michael Chan747e8f82005-07-25 12:33:22 -07005263 return;
5264 }
Matt Carlsonc6cdf432010-04-05 10:19:26 +00005265
Michael Chan747e8f82005-07-25 12:33:22 -07005266 if (!netif_carrier_ok(tp->dev) &&
5267 (tp->link_config.autoneg == AUTONEG_ENABLE)) {
5268 u32 bmcr;
5269
5270 tg3_readphy(tp, MII_BMCR, &bmcr);
5271 if (bmcr & BMCR_ANENABLE) {
5272 u32 phy1, phy2;
5273
5274 /* Select shadow register 0x1f */
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00005275 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x7c00);
5276 tg3_readphy(tp, MII_TG3_MISC_SHDW, &phy1);
Michael Chan747e8f82005-07-25 12:33:22 -07005277
5278 /* Select expansion interrupt status register */
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00005279 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
5280 MII_TG3_DSP_EXP1_INT_STAT);
5281 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &phy2);
5282 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &phy2);
Michael Chan747e8f82005-07-25 12:33:22 -07005283
5284 if ((phy1 & 0x10) && !(phy2 & 0x20)) {
5285 /* We have signal detect and not receiving
5286 * config code words, link is up by parallel
5287 * detection.
5288 */
5289
5290 bmcr &= ~BMCR_ANENABLE;
5291 bmcr |= BMCR_SPEED1000 | BMCR_FULLDPLX;
5292 tg3_writephy(tp, MII_BMCR, bmcr);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005293 tp->phy_flags |= TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07005294 }
5295 }
Matt Carlson859a588792010-04-05 10:19:28 +00005296 } else if (netif_carrier_ok(tp->dev) &&
5297 (tp->link_config.autoneg == AUTONEG_ENABLE) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005298 (tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT)) {
Michael Chan747e8f82005-07-25 12:33:22 -07005299 u32 phy2;
5300
5301 /* Select expansion interrupt status register */
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00005302 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
5303 MII_TG3_DSP_EXP1_INT_STAT);
5304 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &phy2);
Michael Chan747e8f82005-07-25 12:33:22 -07005305 if (phy2 & 0x20) {
5306 u32 bmcr;
5307
5308 /* Config code words received, turn on autoneg. */
5309 tg3_readphy(tp, MII_BMCR, &bmcr);
5310 tg3_writephy(tp, MII_BMCR, bmcr | BMCR_ANENABLE);
5311
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005312 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07005313
5314 }
5315 }
5316}
5317
Linus Torvalds1da177e2005-04-16 15:20:36 -07005318static int tg3_setup_phy(struct tg3 *tp, int force_reset)
5319{
Matt Carlsonf2096f92011-04-05 14:22:48 +00005320 u32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005321 int err;
5322
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005323 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005324 err = tg3_setup_fiber_phy(tp, force_reset);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005325 else if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
Michael Chan747e8f82005-07-25 12:33:22 -07005326 err = tg3_setup_fiber_mii_phy(tp, force_reset);
Matt Carlson859a588792010-04-05 10:19:28 +00005327 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07005328 err = tg3_setup_copper_phy(tp, force_reset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005329
Matt Carlsonbcb37f62008-11-03 16:52:09 -08005330 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX) {
Matt Carlsonf2096f92011-04-05 14:22:48 +00005331 u32 scale;
Matt Carlsonaa6c91f2007-11-12 21:18:04 -08005332
5333 val = tr32(TG3_CPMU_CLCK_STAT) & CPMU_CLCK_STAT_MAC_CLCK_MASK;
5334 if (val == CPMU_CLCK_STAT_MAC_CLCK_62_5)
5335 scale = 65;
5336 else if (val == CPMU_CLCK_STAT_MAC_CLCK_6_25)
5337 scale = 6;
5338 else
5339 scale = 12;
5340
5341 val = tr32(GRC_MISC_CFG) & ~GRC_MISC_CFG_PRESCALAR_MASK;
5342 val |= (scale << GRC_MISC_CFG_PRESCALAR_SHIFT);
5343 tw32(GRC_MISC_CFG, val);
5344 }
5345
Matt Carlsonf2096f92011-04-05 14:22:48 +00005346 val = (2 << TX_LENGTHS_IPG_CRS_SHIFT) |
5347 (6 << TX_LENGTHS_IPG_SHIFT);
5348 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
5349 val |= tr32(MAC_TX_LENGTHS) &
5350 (TX_LENGTHS_JMB_FRM_LEN_MSK |
5351 TX_LENGTHS_CNT_DWN_VAL_MSK);
5352
Linus Torvalds1da177e2005-04-16 15:20:36 -07005353 if (tp->link_config.active_speed == SPEED_1000 &&
5354 tp->link_config.active_duplex == DUPLEX_HALF)
Matt Carlsonf2096f92011-04-05 14:22:48 +00005355 tw32(MAC_TX_LENGTHS, val |
5356 (0xff << TX_LENGTHS_SLOT_TIME_SHIFT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005357 else
Matt Carlsonf2096f92011-04-05 14:22:48 +00005358 tw32(MAC_TX_LENGTHS, val |
5359 (32 << TX_LENGTHS_SLOT_TIME_SHIFT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005360
Joe Perches63c3a662011-04-26 08:12:10 +00005361 if (!tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005362 if (netif_carrier_ok(tp->dev)) {
5363 tw32(HOSTCC_STAT_COAL_TICKS,
David S. Miller15f98502005-05-18 22:49:26 -07005364 tp->coal.stats_block_coalesce_usecs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005365 } else {
5366 tw32(HOSTCC_STAT_COAL_TICKS, 0);
5367 }
5368 }
5369
Joe Perches63c3a662011-04-26 08:12:10 +00005370 if (tg3_flag(tp, ASPM_WORKAROUND)) {
Matt Carlsonf2096f92011-04-05 14:22:48 +00005371 val = tr32(PCIE_PWR_MGMT_THRESH);
Matt Carlson8ed5d972007-05-07 00:25:49 -07005372 if (!netif_carrier_ok(tp->dev))
5373 val = (val & ~PCIE_PWR_MGMT_L1_THRESH_MSK) |
5374 tp->pwrmgmt_thresh;
5375 else
5376 val |= PCIE_PWR_MGMT_L1_THRESH_MSK;
5377 tw32(PCIE_PWR_MGMT_THRESH, val);
5378 }
5379
Linus Torvalds1da177e2005-04-16 15:20:36 -07005380 return err;
5381}
5382
Matt Carlson66cfd1b2010-09-30 10:34:30 +00005383static inline int tg3_irq_sync(struct tg3 *tp)
5384{
5385 return tp->irq_sync;
5386}
5387
Matt Carlson97bd8e42011-04-13 11:05:04 +00005388static inline void tg3_rd32_loop(struct tg3 *tp, u32 *dst, u32 off, u32 len)
5389{
5390 int i;
5391
5392 dst = (u32 *)((u8 *)dst + off);
5393 for (i = 0; i < len; i += sizeof(u32))
5394 *dst++ = tr32(off + i);
5395}
5396
5397static void tg3_dump_legacy_regs(struct tg3 *tp, u32 *regs)
5398{
5399 tg3_rd32_loop(tp, regs, TG3PCI_VENDOR, 0xb0);
5400 tg3_rd32_loop(tp, regs, MAILBOX_INTERRUPT_0, 0x200);
5401 tg3_rd32_loop(tp, regs, MAC_MODE, 0x4f0);
5402 tg3_rd32_loop(tp, regs, SNDDATAI_MODE, 0xe0);
5403 tg3_rd32_loop(tp, regs, SNDDATAC_MODE, 0x04);
5404 tg3_rd32_loop(tp, regs, SNDBDS_MODE, 0x80);
5405 tg3_rd32_loop(tp, regs, SNDBDI_MODE, 0x48);
5406 tg3_rd32_loop(tp, regs, SNDBDC_MODE, 0x04);
5407 tg3_rd32_loop(tp, regs, RCVLPC_MODE, 0x20);
5408 tg3_rd32_loop(tp, regs, RCVLPC_SELLST_BASE, 0x15c);
5409 tg3_rd32_loop(tp, regs, RCVDBDI_MODE, 0x0c);
5410 tg3_rd32_loop(tp, regs, RCVDBDI_JUMBO_BD, 0x3c);
5411 tg3_rd32_loop(tp, regs, RCVDBDI_BD_PROD_IDX_0, 0x44);
5412 tg3_rd32_loop(tp, regs, RCVDCC_MODE, 0x04);
5413 tg3_rd32_loop(tp, regs, RCVBDI_MODE, 0x20);
5414 tg3_rd32_loop(tp, regs, RCVCC_MODE, 0x14);
5415 tg3_rd32_loop(tp, regs, RCVLSC_MODE, 0x08);
5416 tg3_rd32_loop(tp, regs, MBFREE_MODE, 0x08);
5417 tg3_rd32_loop(tp, regs, HOSTCC_MODE, 0x100);
5418
Joe Perches63c3a662011-04-26 08:12:10 +00005419 if (tg3_flag(tp, SUPPORT_MSIX))
Matt Carlson97bd8e42011-04-13 11:05:04 +00005420 tg3_rd32_loop(tp, regs, HOSTCC_RXCOL_TICKS_VEC1, 0x180);
5421
5422 tg3_rd32_loop(tp, regs, MEMARB_MODE, 0x10);
5423 tg3_rd32_loop(tp, regs, BUFMGR_MODE, 0x58);
5424 tg3_rd32_loop(tp, regs, RDMAC_MODE, 0x08);
5425 tg3_rd32_loop(tp, regs, WDMAC_MODE, 0x08);
5426 tg3_rd32_loop(tp, regs, RX_CPU_MODE, 0x04);
5427 tg3_rd32_loop(tp, regs, RX_CPU_STATE, 0x04);
5428 tg3_rd32_loop(tp, regs, RX_CPU_PGMCTR, 0x04);
5429 tg3_rd32_loop(tp, regs, RX_CPU_HWBKPT, 0x04);
5430
Joe Perches63c3a662011-04-26 08:12:10 +00005431 if (!tg3_flag(tp, 5705_PLUS)) {
Matt Carlson97bd8e42011-04-13 11:05:04 +00005432 tg3_rd32_loop(tp, regs, TX_CPU_MODE, 0x04);
5433 tg3_rd32_loop(tp, regs, TX_CPU_STATE, 0x04);
5434 tg3_rd32_loop(tp, regs, TX_CPU_PGMCTR, 0x04);
5435 }
5436
5437 tg3_rd32_loop(tp, regs, GRCMBOX_INTERRUPT_0, 0x110);
5438 tg3_rd32_loop(tp, regs, FTQ_RESET, 0x120);
5439 tg3_rd32_loop(tp, regs, MSGINT_MODE, 0x0c);
5440 tg3_rd32_loop(tp, regs, DMAC_MODE, 0x04);
5441 tg3_rd32_loop(tp, regs, GRC_MODE, 0x4c);
5442
Joe Perches63c3a662011-04-26 08:12:10 +00005443 if (tg3_flag(tp, NVRAM))
Matt Carlson97bd8e42011-04-13 11:05:04 +00005444 tg3_rd32_loop(tp, regs, NVRAM_CMD, 0x24);
5445}
5446
5447static void tg3_dump_state(struct tg3 *tp)
5448{
5449 int i;
5450 u32 *regs;
5451
5452 regs = kzalloc(TG3_REG_BLK_SIZE, GFP_ATOMIC);
5453 if (!regs) {
5454 netdev_err(tp->dev, "Failed allocating register dump buffer\n");
5455 return;
5456 }
5457
Joe Perches63c3a662011-04-26 08:12:10 +00005458 if (tg3_flag(tp, PCI_EXPRESS)) {
Matt Carlson97bd8e42011-04-13 11:05:04 +00005459 /* Read up to but not including private PCI registers */
5460 for (i = 0; i < TG3_PCIE_TLDLPL_PORT; i += sizeof(u32))
5461 regs[i / sizeof(u32)] = tr32(i);
5462 } else
5463 tg3_dump_legacy_regs(tp, regs);
5464
5465 for (i = 0; i < TG3_REG_BLK_SIZE / sizeof(u32); i += 4) {
5466 if (!regs[i + 0] && !regs[i + 1] &&
5467 !regs[i + 2] && !regs[i + 3])
5468 continue;
5469
5470 netdev_err(tp->dev, "0x%08x: 0x%08x, 0x%08x, 0x%08x, 0x%08x\n",
5471 i * 4,
5472 regs[i + 0], regs[i + 1], regs[i + 2], regs[i + 3]);
5473 }
5474
5475 kfree(regs);
5476
5477 for (i = 0; i < tp->irq_cnt; i++) {
5478 struct tg3_napi *tnapi = &tp->napi[i];
5479
5480 /* SW status block */
5481 netdev_err(tp->dev,
5482 "%d: Host status block [%08x:%08x:(%04x:%04x:%04x):(%04x:%04x)]\n",
5483 i,
5484 tnapi->hw_status->status,
5485 tnapi->hw_status->status_tag,
5486 tnapi->hw_status->rx_jumbo_consumer,
5487 tnapi->hw_status->rx_consumer,
5488 tnapi->hw_status->rx_mini_consumer,
5489 tnapi->hw_status->idx[0].rx_producer,
5490 tnapi->hw_status->idx[0].tx_consumer);
5491
5492 netdev_err(tp->dev,
5493 "%d: NAPI info [%08x:%08x:(%04x:%04x:%04x):%04x:(%04x:%04x:%04x:%04x)]\n",
5494 i,
5495 tnapi->last_tag, tnapi->last_irq_tag,
5496 tnapi->tx_prod, tnapi->tx_cons, tnapi->tx_pending,
5497 tnapi->rx_rcb_ptr,
5498 tnapi->prodring.rx_std_prod_idx,
5499 tnapi->prodring.rx_std_cons_idx,
5500 tnapi->prodring.rx_jmb_prod_idx,
5501 tnapi->prodring.rx_jmb_cons_idx);
5502 }
5503}
5504
Michael Chandf3e6542006-05-26 17:48:07 -07005505/* This is called whenever we suspect that the system chipset is re-
5506 * ordering the sequence of MMIO to the tx send mailbox. The symptom
5507 * is bogus tx completions. We try to recover by setting the
5508 * TG3_FLAG_MBOX_WRITE_REORDER flag and resetting the chip later
5509 * in the workqueue.
5510 */
5511static void tg3_tx_recover(struct tg3 *tp)
5512{
Joe Perches63c3a662011-04-26 08:12:10 +00005513 BUG_ON(tg3_flag(tp, MBOX_WRITE_REORDER) ||
Michael Chandf3e6542006-05-26 17:48:07 -07005514 tp->write32_tx_mbox == tg3_write_indirect_mbox);
5515
Matt Carlson5129c3a2010-04-05 10:19:23 +00005516 netdev_warn(tp->dev,
5517 "The system may be re-ordering memory-mapped I/O "
5518 "cycles to the network device, attempting to recover. "
5519 "Please report the problem to the driver maintainer "
5520 "and include system chipset information.\n");
Michael Chandf3e6542006-05-26 17:48:07 -07005521
5522 spin_lock(&tp->lock);
Joe Perches63c3a662011-04-26 08:12:10 +00005523 tg3_flag_set(tp, TX_RECOVERY_PENDING);
Michael Chandf3e6542006-05-26 17:48:07 -07005524 spin_unlock(&tp->lock);
5525}
5526
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005527static inline u32 tg3_tx_avail(struct tg3_napi *tnapi)
Michael Chan1b2a7202006-08-07 21:46:02 -07005528{
Matt Carlsonf65aac12010-08-02 11:26:03 +00005529 /* Tell compiler to fetch tx indices from memory. */
5530 barrier();
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005531 return tnapi->tx_pending -
5532 ((tnapi->tx_prod - tnapi->tx_cons) & (TG3_TX_RING_SIZE - 1));
Michael Chan1b2a7202006-08-07 21:46:02 -07005533}
5534
Linus Torvalds1da177e2005-04-16 15:20:36 -07005535/* Tigon3 never reports partial packet sends. So we do not
5536 * need special logic to handle SKBs that have not had all
5537 * of their frags sent yet, like SunGEM does.
5538 */
Matt Carlson17375d22009-08-28 14:02:18 +00005539static void tg3_tx(struct tg3_napi *tnapi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005540{
Matt Carlson17375d22009-08-28 14:02:18 +00005541 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00005542 u32 hw_idx = tnapi->hw_status->idx[0].tx_consumer;
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005543 u32 sw_idx = tnapi->tx_cons;
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005544 struct netdev_queue *txq;
5545 int index = tnapi - tp->napi;
Tom Herbert298376d2011-11-28 16:33:30 +00005546 unsigned int pkts_compl = 0, bytes_compl = 0;
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005547
Joe Perches63c3a662011-04-26 08:12:10 +00005548 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005549 index--;
5550
5551 txq = netdev_get_tx_queue(tp->dev, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005552
5553 while (sw_idx != hw_idx) {
Matt Carlsondf8944c2011-07-27 14:20:46 +00005554 struct tg3_tx_ring_info *ri = &tnapi->tx_buffers[sw_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07005555 struct sk_buff *skb = ri->skb;
Michael Chandf3e6542006-05-26 17:48:07 -07005556 int i, tx_bug = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005557
Michael Chandf3e6542006-05-26 17:48:07 -07005558 if (unlikely(skb == NULL)) {
5559 tg3_tx_recover(tp);
5560 return;
5561 }
5562
Alexander Duyckf4188d82009-12-02 16:48:38 +00005563 pci_unmap_single(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005564 dma_unmap_addr(ri, mapping),
Alexander Duyckf4188d82009-12-02 16:48:38 +00005565 skb_headlen(skb),
5566 PCI_DMA_TODEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005567
5568 ri->skb = NULL;
5569
Matt Carlsone01ee142011-07-27 14:20:50 +00005570 while (ri->fragmented) {
5571 ri->fragmented = false;
5572 sw_idx = NEXT_TX(sw_idx);
5573 ri = &tnapi->tx_buffers[sw_idx];
5574 }
5575
Linus Torvalds1da177e2005-04-16 15:20:36 -07005576 sw_idx = NEXT_TX(sw_idx);
5577
5578 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005579 ri = &tnapi->tx_buffers[sw_idx];
Michael Chandf3e6542006-05-26 17:48:07 -07005580 if (unlikely(ri->skb != NULL || sw_idx == hw_idx))
5581 tx_bug = 1;
Alexander Duyckf4188d82009-12-02 16:48:38 +00005582
5583 pci_unmap_page(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005584 dma_unmap_addr(ri, mapping),
Eric Dumazet9e903e02011-10-18 21:00:24 +00005585 skb_frag_size(&skb_shinfo(skb)->frags[i]),
Alexander Duyckf4188d82009-12-02 16:48:38 +00005586 PCI_DMA_TODEVICE);
Matt Carlsone01ee142011-07-27 14:20:50 +00005587
5588 while (ri->fragmented) {
5589 ri->fragmented = false;
5590 sw_idx = NEXT_TX(sw_idx);
5591 ri = &tnapi->tx_buffers[sw_idx];
5592 }
5593
Linus Torvalds1da177e2005-04-16 15:20:36 -07005594 sw_idx = NEXT_TX(sw_idx);
5595 }
5596
Tom Herbert298376d2011-11-28 16:33:30 +00005597 pkts_compl++;
5598 bytes_compl += skb->len;
5599
David S. Millerf47c11e2005-06-24 20:18:35 -07005600 dev_kfree_skb(skb);
Michael Chandf3e6542006-05-26 17:48:07 -07005601
5602 if (unlikely(tx_bug)) {
5603 tg3_tx_recover(tp);
5604 return;
5605 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005606 }
5607
Tom Herbert5cb917b2012-03-05 19:53:50 +00005608 netdev_tx_completed_queue(txq, pkts_compl, bytes_compl);
Tom Herbert298376d2011-11-28 16:33:30 +00005609
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005610 tnapi->tx_cons = sw_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005611
Michael Chan1b2a7202006-08-07 21:46:02 -07005612 /* Need to make the tx_cons update visible to tg3_start_xmit()
5613 * before checking for netif_queue_stopped(). Without the
5614 * memory barrier, there is a small possibility that tg3_start_xmit()
5615 * will miss it and cause the queue to be stopped forever.
5616 */
5617 smp_mb();
5618
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005619 if (unlikely(netif_tx_queue_stopped(txq) &&
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005620 (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi)))) {
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005621 __netif_tx_lock(txq, smp_processor_id());
5622 if (netif_tx_queue_stopped(txq) &&
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005623 (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi)))
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005624 netif_tx_wake_queue(txq);
5625 __netif_tx_unlock(txq);
Michael Chan51b91462005-09-01 17:41:28 -07005626 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005627}
5628
Eric Dumazet8d4057a2012-04-27 00:34:49 +00005629static void *tg3_frag_alloc(struct tg3_rx_prodring_set *tpr)
5630{
5631 void *data;
5632
5633 if (tpr->rx_page_size < TG3_FRAGSIZE) {
5634 struct page *page = alloc_page(GFP_ATOMIC);
5635
5636 if (!page)
5637 return NULL;
5638 atomic_add((PAGE_SIZE / TG3_FRAGSIZE) - 1, &page->_count);
5639 tpr->rx_page_addr = page_address(page);
5640 tpr->rx_page_size = PAGE_SIZE;
5641 }
5642 data = tpr->rx_page_addr;
5643 tpr->rx_page_addr += TG3_FRAGSIZE;
5644 tpr->rx_page_size -= TG3_FRAGSIZE;
5645 return data;
5646}
5647
5648static void tg3_frag_free(bool is_frag, void *data)
5649{
5650 if (is_frag)
5651 put_page(virt_to_head_page(data));
5652 else
5653 kfree(data);
5654}
5655
Eric Dumazet9205fd92011-11-18 06:47:01 +00005656static void tg3_rx_data_free(struct tg3 *tp, struct ring_info *ri, u32 map_sz)
Matt Carlson2b2cdb62009-11-13 13:03:48 +00005657{
Eric Dumazet8d4057a2012-04-27 00:34:49 +00005658 unsigned int skb_size = SKB_DATA_ALIGN(map_sz + TG3_RX_OFFSET(tp)) +
5659 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
5660
Eric Dumazet9205fd92011-11-18 06:47:01 +00005661 if (!ri->data)
Matt Carlson2b2cdb62009-11-13 13:03:48 +00005662 return;
5663
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005664 pci_unmap_single(tp->pdev, dma_unmap_addr(ri, mapping),
Matt Carlson2b2cdb62009-11-13 13:03:48 +00005665 map_sz, PCI_DMA_FROMDEVICE);
Eric Dumazet8d4057a2012-04-27 00:34:49 +00005666 tg3_frag_free(skb_size <= TG3_FRAGSIZE, ri->data);
Eric Dumazet9205fd92011-11-18 06:47:01 +00005667 ri->data = NULL;
Matt Carlson2b2cdb62009-11-13 13:03:48 +00005668}
5669
Eric Dumazet8d4057a2012-04-27 00:34:49 +00005670
Linus Torvalds1da177e2005-04-16 15:20:36 -07005671/* Returns size of skb allocated or < 0 on error.
5672 *
5673 * We only need to fill in the address because the other members
5674 * of the RX descriptor are invariant, see tg3_init_rings.
5675 *
5676 * Note the purposeful assymetry of cpu vs. chip accesses. For
5677 * posting buffers we only dirty the first cache line of the RX
5678 * descriptor (containing the address). Whereas for the RX status
5679 * buffers the cpu only reads the last cacheline of the RX descriptor
5680 * (to fetch the error flags, vlan tag, checksum, and opaque cookie).
5681 */
Eric Dumazet9205fd92011-11-18 06:47:01 +00005682static int tg3_alloc_rx_data(struct tg3 *tp, struct tg3_rx_prodring_set *tpr,
Eric Dumazet8d4057a2012-04-27 00:34:49 +00005683 u32 opaque_key, u32 dest_idx_unmasked,
5684 unsigned int *frag_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005685{
5686 struct tg3_rx_buffer_desc *desc;
Matt Carlsonf94e2902010-10-14 10:37:42 +00005687 struct ring_info *map;
Eric Dumazet9205fd92011-11-18 06:47:01 +00005688 u8 *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005689 dma_addr_t mapping;
Eric Dumazet9205fd92011-11-18 06:47:01 +00005690 int skb_size, data_size, dest_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005691
Linus Torvalds1da177e2005-04-16 15:20:36 -07005692 switch (opaque_key) {
5693 case RXD_OPAQUE_RING_STD:
Matt Carlson2c49a442010-09-30 10:34:35 +00005694 dest_idx = dest_idx_unmasked & tp->rx_std_ring_mask;
Matt Carlson21f581a2009-08-28 14:00:25 +00005695 desc = &tpr->rx_std[dest_idx];
5696 map = &tpr->rx_std_buffers[dest_idx];
Eric Dumazet9205fd92011-11-18 06:47:01 +00005697 data_size = tp->rx_pkt_map_sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005698 break;
5699
5700 case RXD_OPAQUE_RING_JUMBO:
Matt Carlson2c49a442010-09-30 10:34:35 +00005701 dest_idx = dest_idx_unmasked & tp->rx_jmb_ring_mask;
Matt Carlson79ed5ac2009-08-28 14:00:55 +00005702 desc = &tpr->rx_jmb[dest_idx].std;
Matt Carlson21f581a2009-08-28 14:00:25 +00005703 map = &tpr->rx_jmb_buffers[dest_idx];
Eric Dumazet9205fd92011-11-18 06:47:01 +00005704 data_size = TG3_RX_JMB_MAP_SZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005705 break;
5706
5707 default:
5708 return -EINVAL;
Stephen Hemminger855e1112008-04-16 16:37:28 -07005709 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005710
5711 /* Do not overwrite any of the map or rp information
5712 * until we are sure we can commit to a new buffer.
5713 *
5714 * Callers depend upon this behavior and assume that
5715 * we leave everything unchanged if we fail.
5716 */
Eric Dumazet9205fd92011-11-18 06:47:01 +00005717 skb_size = SKB_DATA_ALIGN(data_size + TG3_RX_OFFSET(tp)) +
5718 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
Eric Dumazet8d4057a2012-04-27 00:34:49 +00005719 if (skb_size <= TG3_FRAGSIZE) {
5720 data = tg3_frag_alloc(tpr);
5721 *frag_size = TG3_FRAGSIZE;
5722 } else {
5723 data = kmalloc(skb_size, GFP_ATOMIC);
5724 *frag_size = 0;
5725 }
Eric Dumazet9205fd92011-11-18 06:47:01 +00005726 if (!data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005727 return -ENOMEM;
5728
Eric Dumazet9205fd92011-11-18 06:47:01 +00005729 mapping = pci_map_single(tp->pdev,
5730 data + TG3_RX_OFFSET(tp),
5731 data_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005732 PCI_DMA_FROMDEVICE);
Eric Dumazet8d4057a2012-04-27 00:34:49 +00005733 if (unlikely(pci_dma_mapping_error(tp->pdev, mapping))) {
5734 tg3_frag_free(skb_size <= TG3_FRAGSIZE, data);
Matt Carlsona21771d2009-11-02 14:25:31 +00005735 return -EIO;
5736 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005737
Eric Dumazet9205fd92011-11-18 06:47:01 +00005738 map->data = data;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005739 dma_unmap_addr_set(map, mapping, mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005740
Linus Torvalds1da177e2005-04-16 15:20:36 -07005741 desc->addr_hi = ((u64)mapping >> 32);
5742 desc->addr_lo = ((u64)mapping & 0xffffffff);
5743
Eric Dumazet9205fd92011-11-18 06:47:01 +00005744 return data_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005745}
5746
5747/* We only need to move over in the address because the other
5748 * members of the RX descriptor are invariant. See notes above
Eric Dumazet9205fd92011-11-18 06:47:01 +00005749 * tg3_alloc_rx_data for full details.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005750 */
Matt Carlsona3896162009-11-13 13:03:44 +00005751static void tg3_recycle_rx(struct tg3_napi *tnapi,
5752 struct tg3_rx_prodring_set *dpr,
5753 u32 opaque_key, int src_idx,
5754 u32 dest_idx_unmasked)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005755{
Matt Carlson17375d22009-08-28 14:02:18 +00005756 struct tg3 *tp = tnapi->tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005757 struct tg3_rx_buffer_desc *src_desc, *dest_desc;
5758 struct ring_info *src_map, *dest_map;
Matt Carlson8fea32b2010-09-15 08:59:58 +00005759 struct tg3_rx_prodring_set *spr = &tp->napi[0].prodring;
Matt Carlsonc6cdf432010-04-05 10:19:26 +00005760 int dest_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005761
5762 switch (opaque_key) {
5763 case RXD_OPAQUE_RING_STD:
Matt Carlson2c49a442010-09-30 10:34:35 +00005764 dest_idx = dest_idx_unmasked & tp->rx_std_ring_mask;
Matt Carlsona3896162009-11-13 13:03:44 +00005765 dest_desc = &dpr->rx_std[dest_idx];
5766 dest_map = &dpr->rx_std_buffers[dest_idx];
5767 src_desc = &spr->rx_std[src_idx];
5768 src_map = &spr->rx_std_buffers[src_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07005769 break;
5770
5771 case RXD_OPAQUE_RING_JUMBO:
Matt Carlson2c49a442010-09-30 10:34:35 +00005772 dest_idx = dest_idx_unmasked & tp->rx_jmb_ring_mask;
Matt Carlsona3896162009-11-13 13:03:44 +00005773 dest_desc = &dpr->rx_jmb[dest_idx].std;
5774 dest_map = &dpr->rx_jmb_buffers[dest_idx];
5775 src_desc = &spr->rx_jmb[src_idx].std;
5776 src_map = &spr->rx_jmb_buffers[src_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07005777 break;
5778
5779 default:
5780 return;
Stephen Hemminger855e1112008-04-16 16:37:28 -07005781 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005782
Eric Dumazet9205fd92011-11-18 06:47:01 +00005783 dest_map->data = src_map->data;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005784 dma_unmap_addr_set(dest_map, mapping,
5785 dma_unmap_addr(src_map, mapping));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005786 dest_desc->addr_hi = src_desc->addr_hi;
5787 dest_desc->addr_lo = src_desc->addr_lo;
Matt Carlsone92967b2010-02-12 14:47:06 +00005788
5789 /* Ensure that the update to the skb happens after the physical
5790 * addresses have been transferred to the new BD location.
5791 */
5792 smp_wmb();
5793
Eric Dumazet9205fd92011-11-18 06:47:01 +00005794 src_map->data = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005795}
5796
Linus Torvalds1da177e2005-04-16 15:20:36 -07005797/* The RX ring scheme is composed of multiple rings which post fresh
5798 * buffers to the chip, and one special ring the chip uses to report
5799 * status back to the host.
5800 *
5801 * The special ring reports the status of received packets to the
5802 * host. The chip does not write into the original descriptor the
5803 * RX buffer was obtained from. The chip simply takes the original
5804 * descriptor as provided by the host, updates the status and length
5805 * field, then writes this into the next status ring entry.
5806 *
5807 * Each ring the host uses to post buffers to the chip is described
5808 * by a TG3_BDINFO entry in the chips SRAM area. When a packet arrives,
5809 * it is first placed into the on-chip ram. When the packet's length
5810 * is known, it walks down the TG3_BDINFO entries to select the ring.
5811 * Each TG3_BDINFO specifies a MAXLEN field and the first TG3_BDINFO
5812 * which is within the range of the new packet's length is chosen.
5813 *
5814 * The "separate ring for rx status" scheme may sound queer, but it makes
5815 * sense from a cache coherency perspective. If only the host writes
5816 * to the buffer post rings, and only the chip writes to the rx status
5817 * rings, then cache lines never move beyond shared-modified state.
5818 * If both the host and chip were to write into the same ring, cache line
5819 * eviction could occur since both entities want it in an exclusive state.
5820 */
Matt Carlson17375d22009-08-28 14:02:18 +00005821static int tg3_rx(struct tg3_napi *tnapi, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005822{
Matt Carlson17375d22009-08-28 14:02:18 +00005823 struct tg3 *tp = tnapi->tp;
Michael Chanf92905d2006-06-29 20:14:29 -07005824 u32 work_mask, rx_std_posted = 0;
Matt Carlson43619352009-11-13 13:03:47 +00005825 u32 std_prod_idx, jmb_prod_idx;
Matt Carlson72334482009-08-28 14:03:01 +00005826 u32 sw_idx = tnapi->rx_rcb_ptr;
Michael Chan483ba502005-04-25 15:14:03 -07005827 u16 hw_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005828 int received;
Matt Carlson8fea32b2010-09-15 08:59:58 +00005829 struct tg3_rx_prodring_set *tpr = &tnapi->prodring;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005830
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00005831 hw_idx = *(tnapi->rx_rcb_prod_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005832 /*
5833 * We need to order the read of hw_idx and the read of
5834 * the opaque cookie.
5835 */
5836 rmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005837 work_mask = 0;
5838 received = 0;
Matt Carlson43619352009-11-13 13:03:47 +00005839 std_prod_idx = tpr->rx_std_prod_idx;
5840 jmb_prod_idx = tpr->rx_jmb_prod_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005841 while (sw_idx != hw_idx && budget > 0) {
Matt Carlsonafc081f2009-11-13 13:03:43 +00005842 struct ring_info *ri;
Matt Carlson72334482009-08-28 14:03:01 +00005843 struct tg3_rx_buffer_desc *desc = &tnapi->rx_rcb[sw_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07005844 unsigned int len;
5845 struct sk_buff *skb;
5846 dma_addr_t dma_addr;
5847 u32 opaque_key, desc_idx, *post_ptr;
Eric Dumazet9205fd92011-11-18 06:47:01 +00005848 u8 *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005849
5850 desc_idx = desc->opaque & RXD_OPAQUE_INDEX_MASK;
5851 opaque_key = desc->opaque & RXD_OPAQUE_RING_MASK;
5852 if (opaque_key == RXD_OPAQUE_RING_STD) {
Matt Carlson8fea32b2010-09-15 08:59:58 +00005853 ri = &tp->napi[0].prodring.rx_std_buffers[desc_idx];
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005854 dma_addr = dma_unmap_addr(ri, mapping);
Eric Dumazet9205fd92011-11-18 06:47:01 +00005855 data = ri->data;
Matt Carlson43619352009-11-13 13:03:47 +00005856 post_ptr = &std_prod_idx;
Michael Chanf92905d2006-06-29 20:14:29 -07005857 rx_std_posted++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005858 } else if (opaque_key == RXD_OPAQUE_RING_JUMBO) {
Matt Carlson8fea32b2010-09-15 08:59:58 +00005859 ri = &tp->napi[0].prodring.rx_jmb_buffers[desc_idx];
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005860 dma_addr = dma_unmap_addr(ri, mapping);
Eric Dumazet9205fd92011-11-18 06:47:01 +00005861 data = ri->data;
Matt Carlson43619352009-11-13 13:03:47 +00005862 post_ptr = &jmb_prod_idx;
Matt Carlson21f581a2009-08-28 14:00:25 +00005863 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07005864 goto next_pkt_nopost;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005865
5866 work_mask |= opaque_key;
5867
5868 if ((desc->err_vlan & RXD_ERR_MASK) != 0 &&
5869 (desc->err_vlan != RXD_ERR_ODD_NIBBLE_RCVD_MII)) {
5870 drop_it:
Matt Carlsona3896162009-11-13 13:03:44 +00005871 tg3_recycle_rx(tnapi, tpr, opaque_key,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005872 desc_idx, *post_ptr);
5873 drop_it_no_recycle:
5874 /* Other statistics kept track of by card. */
Eric Dumazetb0057c52010-10-10 19:55:52 +00005875 tp->rx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005876 goto next_pkt;
5877 }
5878
Eric Dumazet9205fd92011-11-18 06:47:01 +00005879 prefetch(data + TG3_RX_OFFSET(tp));
Matt Carlsonad829262008-11-21 17:16:16 -08005880 len = ((desc->idx_len & RXD_LEN_MASK) >> RXD_LEN_SHIFT) -
5881 ETH_FCS_LEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005882
Matt Carlsond2757fc2010-04-12 06:58:27 +00005883 if (len > TG3_RX_COPY_THRESH(tp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005884 int skb_size;
Eric Dumazet8d4057a2012-04-27 00:34:49 +00005885 unsigned int frag_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005886
Eric Dumazet9205fd92011-11-18 06:47:01 +00005887 skb_size = tg3_alloc_rx_data(tp, tpr, opaque_key,
Eric Dumazet8d4057a2012-04-27 00:34:49 +00005888 *post_ptr, &frag_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005889 if (skb_size < 0)
5890 goto drop_it;
5891
Matt Carlson287be122009-08-28 13:58:46 +00005892 pci_unmap_single(tp->pdev, dma_addr, skb_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005893 PCI_DMA_FROMDEVICE);
5894
Eric Dumazet8d4057a2012-04-27 00:34:49 +00005895 skb = build_skb(data, frag_size);
Eric Dumazet9205fd92011-11-18 06:47:01 +00005896 if (!skb) {
Eric Dumazet8d4057a2012-04-27 00:34:49 +00005897 tg3_frag_free(frag_size != 0, data);
Eric Dumazet9205fd92011-11-18 06:47:01 +00005898 goto drop_it_no_recycle;
5899 }
5900 skb_reserve(skb, TG3_RX_OFFSET(tp));
5901 /* Ensure that the update to the data happens
Matt Carlson61e800c2010-02-17 15:16:54 +00005902 * after the usage of the old DMA mapping.
5903 */
5904 smp_wmb();
5905
Eric Dumazet9205fd92011-11-18 06:47:01 +00005906 ri->data = NULL;
Matt Carlson61e800c2010-02-17 15:16:54 +00005907
Linus Torvalds1da177e2005-04-16 15:20:36 -07005908 } else {
Matt Carlsona3896162009-11-13 13:03:44 +00005909 tg3_recycle_rx(tnapi, tpr, opaque_key,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005910 desc_idx, *post_ptr);
5911
Eric Dumazet9205fd92011-11-18 06:47:01 +00005912 skb = netdev_alloc_skb(tp->dev,
5913 len + TG3_RAW_IP_ALIGN);
5914 if (skb == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005915 goto drop_it_no_recycle;
5916
Eric Dumazet9205fd92011-11-18 06:47:01 +00005917 skb_reserve(skb, TG3_RAW_IP_ALIGN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005918 pci_dma_sync_single_for_cpu(tp->pdev, dma_addr, len, PCI_DMA_FROMDEVICE);
Eric Dumazet9205fd92011-11-18 06:47:01 +00005919 memcpy(skb->data,
5920 data + TG3_RX_OFFSET(tp),
5921 len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005922 pci_dma_sync_single_for_device(tp->pdev, dma_addr, len, PCI_DMA_FROMDEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005923 }
5924
Eric Dumazet9205fd92011-11-18 06:47:01 +00005925 skb_put(skb, len);
Michał Mirosławdc668912011-04-07 03:35:07 +00005926 if ((tp->dev->features & NETIF_F_RXCSUM) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005927 (desc->type_flags & RXD_FLAG_TCPUDP_CSUM) &&
5928 (((desc->ip_tcp_csum & RXD_TCPCSUM_MASK)
5929 >> RXD_TCPCSUM_SHIFT) == 0xffff))
5930 skb->ip_summed = CHECKSUM_UNNECESSARY;
5931 else
Eric Dumazetbc8acf22010-09-02 13:07:41 -07005932 skb_checksum_none_assert(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005933
5934 skb->protocol = eth_type_trans(skb, tp->dev);
Matt Carlsonf7b493e2009-02-25 14:21:52 +00005935
5936 if (len > (tp->dev->mtu + ETH_HLEN) &&
5937 skb->protocol != htons(ETH_P_8021Q)) {
5938 dev_kfree_skb(skb);
Eric Dumazetb0057c52010-10-10 19:55:52 +00005939 goto drop_it_no_recycle;
Matt Carlsonf7b493e2009-02-25 14:21:52 +00005940 }
5941
Matt Carlson9dc7a112010-04-12 06:58:28 +00005942 if (desc->type_flags & RXD_FLAG_VLAN &&
Matt Carlsonbf933c82011-01-25 15:58:49 +00005943 !(tp->rx_mode & RX_MODE_KEEP_VLAN_TAG))
5944 __vlan_hwaccel_put_tag(skb,
5945 desc->err_vlan & RXD_VLAN_MASK);
Matt Carlson9dc7a112010-04-12 06:58:28 +00005946
Matt Carlsonbf933c82011-01-25 15:58:49 +00005947 napi_gro_receive(&tnapi->napi, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005948
Linus Torvalds1da177e2005-04-16 15:20:36 -07005949 received++;
5950 budget--;
5951
5952next_pkt:
5953 (*post_ptr)++;
Michael Chanf92905d2006-06-29 20:14:29 -07005954
5955 if (unlikely(rx_std_posted >= tp->rx_std_max_post)) {
Matt Carlson2c49a442010-09-30 10:34:35 +00005956 tpr->rx_std_prod_idx = std_prod_idx &
5957 tp->rx_std_ring_mask;
Matt Carlson86cfe4f2010-01-12 10:11:37 +00005958 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
5959 tpr->rx_std_prod_idx);
Michael Chanf92905d2006-06-29 20:14:29 -07005960 work_mask &= ~RXD_OPAQUE_RING_STD;
5961 rx_std_posted = 0;
5962 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005963next_pkt_nopost:
Michael Chan483ba502005-04-25 15:14:03 -07005964 sw_idx++;
Matt Carlson7cb32cf2010-09-30 10:34:36 +00005965 sw_idx &= tp->rx_ret_ring_mask;
Michael Chan52f6d692005-04-25 15:14:32 -07005966
5967 /* Refresh hw_idx to see if there is new work */
5968 if (sw_idx == hw_idx) {
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00005969 hw_idx = *(tnapi->rx_rcb_prod_idx);
Michael Chan52f6d692005-04-25 15:14:32 -07005970 rmb();
5971 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005972 }
5973
5974 /* ACK the status ring. */
Matt Carlson72334482009-08-28 14:03:01 +00005975 tnapi->rx_rcb_ptr = sw_idx;
5976 tw32_rx_mbox(tnapi->consmbox, sw_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005977
5978 /* Refill RX ring(s). */
Joe Perches63c3a662011-04-26 08:12:10 +00005979 if (!tg3_flag(tp, ENABLE_RSS)) {
Michael Chan6541b802012-03-04 14:48:14 +00005980 /* Sync BD data before updating mailbox */
5981 wmb();
5982
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005983 if (work_mask & RXD_OPAQUE_RING_STD) {
Matt Carlson2c49a442010-09-30 10:34:35 +00005984 tpr->rx_std_prod_idx = std_prod_idx &
5985 tp->rx_std_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005986 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
5987 tpr->rx_std_prod_idx);
5988 }
5989 if (work_mask & RXD_OPAQUE_RING_JUMBO) {
Matt Carlson2c49a442010-09-30 10:34:35 +00005990 tpr->rx_jmb_prod_idx = jmb_prod_idx &
5991 tp->rx_jmb_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005992 tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG,
5993 tpr->rx_jmb_prod_idx);
5994 }
5995 mmiowb();
5996 } else if (work_mask) {
5997 /* rx_std_buffers[] and rx_jmb_buffers[] entries must be
5998 * updated before the producer indices can be updated.
5999 */
6000 smp_wmb();
6001
Matt Carlson2c49a442010-09-30 10:34:35 +00006002 tpr->rx_std_prod_idx = std_prod_idx & tp->rx_std_ring_mask;
6003 tpr->rx_jmb_prod_idx = jmb_prod_idx & tp->rx_jmb_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006004
Michael Chan7ae52892012-03-21 15:38:33 +00006005 if (tnapi != &tp->napi[1]) {
6006 tp->rx_refill = true;
Matt Carlsone4af1af2010-02-12 14:47:05 +00006007 napi_schedule(&tp->napi[1].napi);
Michael Chan7ae52892012-03-21 15:38:33 +00006008 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006009 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006010
6011 return received;
6012}
6013
Matt Carlson35f2d7d2009-11-13 13:03:41 +00006014static void tg3_poll_link(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006015{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006016 /* handle link change and other phy events */
Joe Perches63c3a662011-04-26 08:12:10 +00006017 if (!(tg3_flag(tp, USE_LINKCHG_REG) || tg3_flag(tp, POLL_SERDES))) {
Matt Carlson35f2d7d2009-11-13 13:03:41 +00006018 struct tg3_hw_status *sblk = tp->napi[0].hw_status;
6019
Linus Torvalds1da177e2005-04-16 15:20:36 -07006020 if (sblk->status & SD_STATUS_LINK_CHG) {
6021 sblk->status = SD_STATUS_UPDATED |
Matt Carlson35f2d7d2009-11-13 13:03:41 +00006022 (sblk->status & ~SD_STATUS_LINK_CHG);
David S. Millerf47c11e2005-06-24 20:18:35 -07006023 spin_lock(&tp->lock);
Joe Perches63c3a662011-04-26 08:12:10 +00006024 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlsondd477002008-05-25 23:45:58 -07006025 tw32_f(MAC_STATUS,
6026 (MAC_STATUS_SYNC_CHANGED |
6027 MAC_STATUS_CFG_CHANGED |
6028 MAC_STATUS_MI_COMPLETION |
6029 MAC_STATUS_LNKSTATE_CHANGED));
6030 udelay(40);
6031 } else
6032 tg3_setup_phy(tp, 0);
David S. Millerf47c11e2005-06-24 20:18:35 -07006033 spin_unlock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006034 }
6035 }
Matt Carlson35f2d7d2009-11-13 13:03:41 +00006036}
6037
Matt Carlsonf89f38b2010-02-12 14:47:07 +00006038static int tg3_rx_prodring_xfer(struct tg3 *tp,
6039 struct tg3_rx_prodring_set *dpr,
6040 struct tg3_rx_prodring_set *spr)
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006041{
6042 u32 si, di, cpycnt, src_prod_idx;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00006043 int i, err = 0;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006044
6045 while (1) {
6046 src_prod_idx = spr->rx_std_prod_idx;
6047
6048 /* Make sure updates to the rx_std_buffers[] entries and the
6049 * standard producer index are seen in the correct order.
6050 */
6051 smp_rmb();
6052
6053 if (spr->rx_std_cons_idx == src_prod_idx)
6054 break;
6055
6056 if (spr->rx_std_cons_idx < src_prod_idx)
6057 cpycnt = src_prod_idx - spr->rx_std_cons_idx;
6058 else
Matt Carlson2c49a442010-09-30 10:34:35 +00006059 cpycnt = tp->rx_std_ring_mask + 1 -
6060 spr->rx_std_cons_idx;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006061
Matt Carlson2c49a442010-09-30 10:34:35 +00006062 cpycnt = min(cpycnt,
6063 tp->rx_std_ring_mask + 1 - dpr->rx_std_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006064
6065 si = spr->rx_std_cons_idx;
6066 di = dpr->rx_std_prod_idx;
6067
Matt Carlsone92967b2010-02-12 14:47:06 +00006068 for (i = di; i < di + cpycnt; i++) {
Eric Dumazet9205fd92011-11-18 06:47:01 +00006069 if (dpr->rx_std_buffers[i].data) {
Matt Carlsone92967b2010-02-12 14:47:06 +00006070 cpycnt = i - di;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00006071 err = -ENOSPC;
Matt Carlsone92967b2010-02-12 14:47:06 +00006072 break;
6073 }
6074 }
6075
6076 if (!cpycnt)
6077 break;
6078
6079 /* Ensure that updates to the rx_std_buffers ring and the
6080 * shadowed hardware producer ring from tg3_recycle_skb() are
6081 * ordered correctly WRT the skb check above.
6082 */
6083 smp_rmb();
6084
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006085 memcpy(&dpr->rx_std_buffers[di],
6086 &spr->rx_std_buffers[si],
6087 cpycnt * sizeof(struct ring_info));
6088
6089 for (i = 0; i < cpycnt; i++, di++, si++) {
6090 struct tg3_rx_buffer_desc *sbd, *dbd;
6091 sbd = &spr->rx_std[si];
6092 dbd = &dpr->rx_std[di];
6093 dbd->addr_hi = sbd->addr_hi;
6094 dbd->addr_lo = sbd->addr_lo;
6095 }
6096
Matt Carlson2c49a442010-09-30 10:34:35 +00006097 spr->rx_std_cons_idx = (spr->rx_std_cons_idx + cpycnt) &
6098 tp->rx_std_ring_mask;
6099 dpr->rx_std_prod_idx = (dpr->rx_std_prod_idx + cpycnt) &
6100 tp->rx_std_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006101 }
6102
6103 while (1) {
6104 src_prod_idx = spr->rx_jmb_prod_idx;
6105
6106 /* Make sure updates to the rx_jmb_buffers[] entries and
6107 * the jumbo producer index are seen in the correct order.
6108 */
6109 smp_rmb();
6110
6111 if (spr->rx_jmb_cons_idx == src_prod_idx)
6112 break;
6113
6114 if (spr->rx_jmb_cons_idx < src_prod_idx)
6115 cpycnt = src_prod_idx - spr->rx_jmb_cons_idx;
6116 else
Matt Carlson2c49a442010-09-30 10:34:35 +00006117 cpycnt = tp->rx_jmb_ring_mask + 1 -
6118 spr->rx_jmb_cons_idx;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006119
6120 cpycnt = min(cpycnt,
Matt Carlson2c49a442010-09-30 10:34:35 +00006121 tp->rx_jmb_ring_mask + 1 - dpr->rx_jmb_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006122
6123 si = spr->rx_jmb_cons_idx;
6124 di = dpr->rx_jmb_prod_idx;
6125
Matt Carlsone92967b2010-02-12 14:47:06 +00006126 for (i = di; i < di + cpycnt; i++) {
Eric Dumazet9205fd92011-11-18 06:47:01 +00006127 if (dpr->rx_jmb_buffers[i].data) {
Matt Carlsone92967b2010-02-12 14:47:06 +00006128 cpycnt = i - di;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00006129 err = -ENOSPC;
Matt Carlsone92967b2010-02-12 14:47:06 +00006130 break;
6131 }
6132 }
6133
6134 if (!cpycnt)
6135 break;
6136
6137 /* Ensure that updates to the rx_jmb_buffers ring and the
6138 * shadowed hardware producer ring from tg3_recycle_skb() are
6139 * ordered correctly WRT the skb check above.
6140 */
6141 smp_rmb();
6142
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006143 memcpy(&dpr->rx_jmb_buffers[di],
6144 &spr->rx_jmb_buffers[si],
6145 cpycnt * sizeof(struct ring_info));
6146
6147 for (i = 0; i < cpycnt; i++, di++, si++) {
6148 struct tg3_rx_buffer_desc *sbd, *dbd;
6149 sbd = &spr->rx_jmb[si].std;
6150 dbd = &dpr->rx_jmb[di].std;
6151 dbd->addr_hi = sbd->addr_hi;
6152 dbd->addr_lo = sbd->addr_lo;
6153 }
6154
Matt Carlson2c49a442010-09-30 10:34:35 +00006155 spr->rx_jmb_cons_idx = (spr->rx_jmb_cons_idx + cpycnt) &
6156 tp->rx_jmb_ring_mask;
6157 dpr->rx_jmb_prod_idx = (dpr->rx_jmb_prod_idx + cpycnt) &
6158 tp->rx_jmb_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006159 }
Matt Carlsonf89f38b2010-02-12 14:47:07 +00006160
6161 return err;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006162}
6163
Matt Carlson35f2d7d2009-11-13 13:03:41 +00006164static int tg3_poll_work(struct tg3_napi *tnapi, int work_done, int budget)
6165{
6166 struct tg3 *tp = tnapi->tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006167
6168 /* run TX completion thread */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006169 if (tnapi->hw_status->idx[0].tx_consumer != tnapi->tx_cons) {
Matt Carlson17375d22009-08-28 14:02:18 +00006170 tg3_tx(tnapi);
Joe Perches63c3a662011-04-26 08:12:10 +00006171 if (unlikely(tg3_flag(tp, TX_RECOVERY_PENDING)))
Michael Chan4fd7ab52007-10-12 01:39:50 -07006172 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006173 }
6174
Linus Torvalds1da177e2005-04-16 15:20:36 -07006175 /* run RX thread, within the bounds set by NAPI.
6176 * All RX "locking" is done by ensuring outside
Stephen Hemmingerbea33482007-10-03 16:41:36 -07006177 * code synchronizes with tg3->napi.poll()
Linus Torvalds1da177e2005-04-16 15:20:36 -07006178 */
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00006179 if (*(tnapi->rx_rcb_prod_idx) != tnapi->rx_rcb_ptr)
Matt Carlson17375d22009-08-28 14:02:18 +00006180 work_done += tg3_rx(tnapi, budget - work_done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006181
Joe Perches63c3a662011-04-26 08:12:10 +00006182 if (tg3_flag(tp, ENABLE_RSS) && tnapi == &tp->napi[1]) {
Matt Carlson8fea32b2010-09-15 08:59:58 +00006183 struct tg3_rx_prodring_set *dpr = &tp->napi[0].prodring;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00006184 int i, err = 0;
Matt Carlsone4af1af2010-02-12 14:47:05 +00006185 u32 std_prod_idx = dpr->rx_std_prod_idx;
6186 u32 jmb_prod_idx = dpr->rx_jmb_prod_idx;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006187
Michael Chan7ae52892012-03-21 15:38:33 +00006188 tp->rx_refill = false;
Matt Carlsone4af1af2010-02-12 14:47:05 +00006189 for (i = 1; i < tp->irq_cnt; i++)
Matt Carlsonf89f38b2010-02-12 14:47:07 +00006190 err |= tg3_rx_prodring_xfer(tp, dpr,
Matt Carlson8fea32b2010-09-15 08:59:58 +00006191 &tp->napi[i].prodring);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006192
6193 wmb();
6194
Matt Carlsone4af1af2010-02-12 14:47:05 +00006195 if (std_prod_idx != dpr->rx_std_prod_idx)
6196 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
6197 dpr->rx_std_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006198
Matt Carlsone4af1af2010-02-12 14:47:05 +00006199 if (jmb_prod_idx != dpr->rx_jmb_prod_idx)
6200 tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG,
6201 dpr->rx_jmb_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006202
6203 mmiowb();
Matt Carlsonf89f38b2010-02-12 14:47:07 +00006204
6205 if (err)
6206 tw32_f(HOSTCC_MODE, tp->coal_now);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006207 }
6208
David S. Miller6f535762007-10-11 18:08:29 -07006209 return work_done;
6210}
David S. Millerf7383c22005-05-18 22:50:53 -07006211
Matt Carlsondb219972011-11-04 09:15:03 +00006212static inline void tg3_reset_task_schedule(struct tg3 *tp)
6213{
6214 if (!test_and_set_bit(TG3_FLAG_RESET_TASK_PENDING, tp->tg3_flags))
6215 schedule_work(&tp->reset_task);
6216}
6217
6218static inline void tg3_reset_task_cancel(struct tg3 *tp)
6219{
6220 cancel_work_sync(&tp->reset_task);
6221 tg3_flag_clear(tp, RESET_TASK_PENDING);
Matt Carlsonc7101352012-02-22 12:35:20 +00006222 tg3_flag_clear(tp, TX_RECOVERY_PENDING);
Matt Carlsondb219972011-11-04 09:15:03 +00006223}
6224
Matt Carlson35f2d7d2009-11-13 13:03:41 +00006225static int tg3_poll_msix(struct napi_struct *napi, int budget)
6226{
6227 struct tg3_napi *tnapi = container_of(napi, struct tg3_napi, napi);
6228 struct tg3 *tp = tnapi->tp;
6229 int work_done = 0;
6230 struct tg3_hw_status *sblk = tnapi->hw_status;
6231
6232 while (1) {
6233 work_done = tg3_poll_work(tnapi, work_done, budget);
6234
Joe Perches63c3a662011-04-26 08:12:10 +00006235 if (unlikely(tg3_flag(tp, TX_RECOVERY_PENDING)))
Matt Carlson35f2d7d2009-11-13 13:03:41 +00006236 goto tx_recovery;
6237
6238 if (unlikely(work_done >= budget))
6239 break;
6240
Matt Carlsonc6cdf432010-04-05 10:19:26 +00006241 /* tp->last_tag is used in tg3_int_reenable() below
Matt Carlson35f2d7d2009-11-13 13:03:41 +00006242 * to tell the hw how much work has been processed,
6243 * so we must read it before checking for more work.
6244 */
6245 tnapi->last_tag = sblk->status_tag;
6246 tnapi->last_irq_tag = tnapi->last_tag;
6247 rmb();
6248
6249 /* check for RX/TX work to do */
Matt Carlson6d40db72010-04-05 10:19:20 +00006250 if (likely(sblk->idx[0].tx_consumer == tnapi->tx_cons &&
6251 *(tnapi->rx_rcb_prod_idx) == tnapi->rx_rcb_ptr)) {
Michael Chan7ae52892012-03-21 15:38:33 +00006252
6253 /* This test here is not race free, but will reduce
6254 * the number of interrupts by looping again.
6255 */
6256 if (tnapi == &tp->napi[1] && tp->rx_refill)
6257 continue;
6258
Matt Carlson35f2d7d2009-11-13 13:03:41 +00006259 napi_complete(napi);
6260 /* Reenable interrupts. */
6261 tw32_mailbox(tnapi->int_mbox, tnapi->last_tag << 24);
Michael Chan7ae52892012-03-21 15:38:33 +00006262
6263 /* This test here is synchronized by napi_schedule()
6264 * and napi_complete() to close the race condition.
6265 */
6266 if (unlikely(tnapi == &tp->napi[1] && tp->rx_refill)) {
6267 tw32(HOSTCC_MODE, tp->coalesce_mode |
6268 HOSTCC_MODE_ENABLE |
6269 tnapi->coal_now);
6270 }
Matt Carlson35f2d7d2009-11-13 13:03:41 +00006271 mmiowb();
6272 break;
6273 }
6274 }
6275
6276 return work_done;
6277
6278tx_recovery:
6279 /* work_done is guaranteed to be less than budget. */
6280 napi_complete(napi);
Matt Carlsondb219972011-11-04 09:15:03 +00006281 tg3_reset_task_schedule(tp);
Matt Carlson35f2d7d2009-11-13 13:03:41 +00006282 return work_done;
6283}
6284
Matt Carlsone64de4e2011-04-13 11:05:05 +00006285static void tg3_process_error(struct tg3 *tp)
6286{
6287 u32 val;
6288 bool real_error = false;
6289
Joe Perches63c3a662011-04-26 08:12:10 +00006290 if (tg3_flag(tp, ERROR_PROCESSED))
Matt Carlsone64de4e2011-04-13 11:05:05 +00006291 return;
6292
6293 /* Check Flow Attention register */
6294 val = tr32(HOSTCC_FLOW_ATTN);
6295 if (val & ~HOSTCC_FLOW_ATTN_MBUF_LWM) {
6296 netdev_err(tp->dev, "FLOW Attention error. Resetting chip.\n");
6297 real_error = true;
6298 }
6299
6300 if (tr32(MSGINT_STATUS) & ~MSGINT_STATUS_MSI_REQ) {
6301 netdev_err(tp->dev, "MSI Status error. Resetting chip.\n");
6302 real_error = true;
6303 }
6304
6305 if (tr32(RDMAC_STATUS) || tr32(WDMAC_STATUS)) {
6306 netdev_err(tp->dev, "DMA Status error. Resetting chip.\n");
6307 real_error = true;
6308 }
6309
6310 if (!real_error)
6311 return;
6312
6313 tg3_dump_state(tp);
6314
Joe Perches63c3a662011-04-26 08:12:10 +00006315 tg3_flag_set(tp, ERROR_PROCESSED);
Matt Carlsondb219972011-11-04 09:15:03 +00006316 tg3_reset_task_schedule(tp);
Matt Carlsone64de4e2011-04-13 11:05:05 +00006317}
6318
David S. Miller6f535762007-10-11 18:08:29 -07006319static int tg3_poll(struct napi_struct *napi, int budget)
6320{
Matt Carlson8ef04422009-08-28 14:01:37 +00006321 struct tg3_napi *tnapi = container_of(napi, struct tg3_napi, napi);
6322 struct tg3 *tp = tnapi->tp;
David S. Miller6f535762007-10-11 18:08:29 -07006323 int work_done = 0;
Matt Carlson898a56f2009-08-28 14:02:40 +00006324 struct tg3_hw_status *sblk = tnapi->hw_status;
David S. Miller6f535762007-10-11 18:08:29 -07006325
6326 while (1) {
Matt Carlsone64de4e2011-04-13 11:05:05 +00006327 if (sblk->status & SD_STATUS_ERROR)
6328 tg3_process_error(tp);
6329
Matt Carlson35f2d7d2009-11-13 13:03:41 +00006330 tg3_poll_link(tp);
6331
Matt Carlson17375d22009-08-28 14:02:18 +00006332 work_done = tg3_poll_work(tnapi, work_done, budget);
David S. Miller6f535762007-10-11 18:08:29 -07006333
Joe Perches63c3a662011-04-26 08:12:10 +00006334 if (unlikely(tg3_flag(tp, TX_RECOVERY_PENDING)))
David S. Miller6f535762007-10-11 18:08:29 -07006335 goto tx_recovery;
6336
6337 if (unlikely(work_done >= budget))
6338 break;
6339
Joe Perches63c3a662011-04-26 08:12:10 +00006340 if (tg3_flag(tp, TAGGED_STATUS)) {
Matt Carlson17375d22009-08-28 14:02:18 +00006341 /* tp->last_tag is used in tg3_int_reenable() below
Michael Chan4fd7ab52007-10-12 01:39:50 -07006342 * to tell the hw how much work has been processed,
6343 * so we must read it before checking for more work.
6344 */
Matt Carlson898a56f2009-08-28 14:02:40 +00006345 tnapi->last_tag = sblk->status_tag;
6346 tnapi->last_irq_tag = tnapi->last_tag;
Michael Chan4fd7ab52007-10-12 01:39:50 -07006347 rmb();
6348 } else
6349 sblk->status &= ~SD_STATUS_UPDATED;
6350
Matt Carlson17375d22009-08-28 14:02:18 +00006351 if (likely(!tg3_has_work(tnapi))) {
Ben Hutchings288379f2009-01-19 16:43:59 -08006352 napi_complete(napi);
Matt Carlson17375d22009-08-28 14:02:18 +00006353 tg3_int_reenable(tnapi);
David S. Miller6f535762007-10-11 18:08:29 -07006354 break;
6355 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006356 }
6357
Stephen Hemmingerbea33482007-10-03 16:41:36 -07006358 return work_done;
David S. Miller6f535762007-10-11 18:08:29 -07006359
6360tx_recovery:
Michael Chan4fd7ab52007-10-12 01:39:50 -07006361 /* work_done is guaranteed to be less than budget. */
Ben Hutchings288379f2009-01-19 16:43:59 -08006362 napi_complete(napi);
Matt Carlsondb219972011-11-04 09:15:03 +00006363 tg3_reset_task_schedule(tp);
Michael Chan4fd7ab52007-10-12 01:39:50 -07006364 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006365}
6366
Matt Carlson66cfd1b2010-09-30 10:34:30 +00006367static void tg3_napi_disable(struct tg3 *tp)
6368{
6369 int i;
6370
6371 for (i = tp->irq_cnt - 1; i >= 0; i--)
6372 napi_disable(&tp->napi[i].napi);
6373}
6374
6375static void tg3_napi_enable(struct tg3 *tp)
6376{
6377 int i;
6378
6379 for (i = 0; i < tp->irq_cnt; i++)
6380 napi_enable(&tp->napi[i].napi);
6381}
6382
6383static void tg3_napi_init(struct tg3 *tp)
6384{
6385 int i;
6386
6387 netif_napi_add(tp->dev, &tp->napi[0].napi, tg3_poll, 64);
6388 for (i = 1; i < tp->irq_cnt; i++)
6389 netif_napi_add(tp->dev, &tp->napi[i].napi, tg3_poll_msix, 64);
6390}
6391
6392static void tg3_napi_fini(struct tg3 *tp)
6393{
6394 int i;
6395
6396 for (i = 0; i < tp->irq_cnt; i++)
6397 netif_napi_del(&tp->napi[i].napi);
6398}
6399
6400static inline void tg3_netif_stop(struct tg3 *tp)
6401{
6402 tp->dev->trans_start = jiffies; /* prevent tx timeout */
6403 tg3_napi_disable(tp);
6404 netif_tx_disable(tp->dev);
6405}
6406
6407static inline void tg3_netif_start(struct tg3 *tp)
6408{
6409 /* NOTE: unconditional netif_tx_wake_all_queues is only
6410 * appropriate so long as all callers are assured to
6411 * have free tx slots (such as after tg3_init_hw)
6412 */
6413 netif_tx_wake_all_queues(tp->dev);
6414
6415 tg3_napi_enable(tp);
6416 tp->napi[0].hw_status->status |= SD_STATUS_UPDATED;
6417 tg3_enable_ints(tp);
6418}
6419
David S. Millerf47c11e2005-06-24 20:18:35 -07006420static void tg3_irq_quiesce(struct tg3 *tp)
6421{
Matt Carlson4f125f42009-09-01 12:55:02 +00006422 int i;
6423
David S. Millerf47c11e2005-06-24 20:18:35 -07006424 BUG_ON(tp->irq_sync);
6425
6426 tp->irq_sync = 1;
6427 smp_mb();
6428
Matt Carlson4f125f42009-09-01 12:55:02 +00006429 for (i = 0; i < tp->irq_cnt; i++)
6430 synchronize_irq(tp->napi[i].irq_vec);
David S. Millerf47c11e2005-06-24 20:18:35 -07006431}
6432
David S. Millerf47c11e2005-06-24 20:18:35 -07006433/* Fully shutdown all tg3 driver activity elsewhere in the system.
6434 * If irq_sync is non-zero, then the IRQ handler must be synchronized
6435 * with as well. Most of the time, this is not necessary except when
6436 * shutting down the device.
6437 */
6438static inline void tg3_full_lock(struct tg3 *tp, int irq_sync)
6439{
Michael Chan46966542007-07-11 19:47:19 -07006440 spin_lock_bh(&tp->lock);
David S. Millerf47c11e2005-06-24 20:18:35 -07006441 if (irq_sync)
6442 tg3_irq_quiesce(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -07006443}
6444
6445static inline void tg3_full_unlock(struct tg3 *tp)
6446{
David S. Millerf47c11e2005-06-24 20:18:35 -07006447 spin_unlock_bh(&tp->lock);
6448}
6449
Michael Chanfcfa0a32006-03-20 22:28:41 -08006450/* One-shot MSI handler - Chip automatically disables interrupt
6451 * after sending MSI so driver doesn't have to do it.
6452 */
David Howells7d12e782006-10-05 14:55:46 +01006453static irqreturn_t tg3_msi_1shot(int irq, void *dev_id)
Michael Chanfcfa0a32006-03-20 22:28:41 -08006454{
Matt Carlson09943a12009-08-28 14:01:57 +00006455 struct tg3_napi *tnapi = dev_id;
6456 struct tg3 *tp = tnapi->tp;
Michael Chanfcfa0a32006-03-20 22:28:41 -08006457
Matt Carlson898a56f2009-08-28 14:02:40 +00006458 prefetch(tnapi->hw_status);
Matt Carlson0c1d0e22009-09-01 13:16:33 +00006459 if (tnapi->rx_rcb)
6460 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Michael Chanfcfa0a32006-03-20 22:28:41 -08006461
6462 if (likely(!tg3_irq_sync(tp)))
Matt Carlson09943a12009-08-28 14:01:57 +00006463 napi_schedule(&tnapi->napi);
Michael Chanfcfa0a32006-03-20 22:28:41 -08006464
6465 return IRQ_HANDLED;
6466}
6467
Michael Chan88b06bc22005-04-21 17:13:25 -07006468/* MSI ISR - No need to check for interrupt sharing and no need to
6469 * flush status block and interrupt mailbox. PCI ordering rules
6470 * guarantee that MSI will arrive after the status block.
6471 */
David Howells7d12e782006-10-05 14:55:46 +01006472static irqreturn_t tg3_msi(int irq, void *dev_id)
Michael Chan88b06bc22005-04-21 17:13:25 -07006473{
Matt Carlson09943a12009-08-28 14:01:57 +00006474 struct tg3_napi *tnapi = dev_id;
6475 struct tg3 *tp = tnapi->tp;
Michael Chan88b06bc22005-04-21 17:13:25 -07006476
Matt Carlson898a56f2009-08-28 14:02:40 +00006477 prefetch(tnapi->hw_status);
Matt Carlson0c1d0e22009-09-01 13:16:33 +00006478 if (tnapi->rx_rcb)
6479 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Michael Chan88b06bc22005-04-21 17:13:25 -07006480 /*
David S. Millerfac9b832005-05-18 22:46:34 -07006481 * Writing any value to intr-mbox-0 clears PCI INTA# and
Michael Chan88b06bc22005-04-21 17:13:25 -07006482 * chip-internal interrupt pending events.
David S. Millerfac9b832005-05-18 22:46:34 -07006483 * Writing non-zero to intr-mbox-0 additional tells the
Michael Chan88b06bc22005-04-21 17:13:25 -07006484 * NIC to stop sending us irqs, engaging "in-intr-handler"
6485 * event coalescing.
6486 */
Matt Carlson5b39de92011-08-31 11:44:50 +00006487 tw32_mailbox(tnapi->int_mbox, 0x00000001);
Michael Chan61487482005-09-05 17:53:19 -07006488 if (likely(!tg3_irq_sync(tp)))
Matt Carlson09943a12009-08-28 14:01:57 +00006489 napi_schedule(&tnapi->napi);
Michael Chan61487482005-09-05 17:53:19 -07006490
Michael Chan88b06bc22005-04-21 17:13:25 -07006491 return IRQ_RETVAL(1);
6492}
6493
David Howells7d12e782006-10-05 14:55:46 +01006494static irqreturn_t tg3_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006495{
Matt Carlson09943a12009-08-28 14:01:57 +00006496 struct tg3_napi *tnapi = dev_id;
6497 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00006498 struct tg3_hw_status *sblk = tnapi->hw_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006499 unsigned int handled = 1;
6500
Linus Torvalds1da177e2005-04-16 15:20:36 -07006501 /* In INTx mode, it is possible for the interrupt to arrive at
6502 * the CPU before the status block posted prior to the interrupt.
6503 * Reading the PCI State register will confirm whether the
6504 * interrupt is ours and will flush the status block.
6505 */
Michael Chand18edcb2007-03-24 20:57:11 -07006506 if (unlikely(!(sblk->status & SD_STATUS_UPDATED))) {
Joe Perches63c3a662011-04-26 08:12:10 +00006507 if (tg3_flag(tp, CHIP_RESETTING) ||
Michael Chand18edcb2007-03-24 20:57:11 -07006508 (tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
6509 handled = 0;
David S. Millerf47c11e2005-06-24 20:18:35 -07006510 goto out;
David S. Millerfac9b832005-05-18 22:46:34 -07006511 }
Michael Chand18edcb2007-03-24 20:57:11 -07006512 }
6513
6514 /*
6515 * Writing any value to intr-mbox-0 clears PCI INTA# and
6516 * chip-internal interrupt pending events.
6517 * Writing non-zero to intr-mbox-0 additional tells the
6518 * NIC to stop sending us irqs, engaging "in-intr-handler"
6519 * event coalescing.
Michael Chanc04cb342007-05-07 00:26:15 -07006520 *
6521 * Flush the mailbox to de-assert the IRQ immediately to prevent
6522 * spurious interrupts. The flush impacts performance but
6523 * excessive spurious interrupts can be worse in some cases.
Michael Chand18edcb2007-03-24 20:57:11 -07006524 */
Michael Chanc04cb342007-05-07 00:26:15 -07006525 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 0x00000001);
Michael Chand18edcb2007-03-24 20:57:11 -07006526 if (tg3_irq_sync(tp))
6527 goto out;
6528 sblk->status &= ~SD_STATUS_UPDATED;
Matt Carlson17375d22009-08-28 14:02:18 +00006529 if (likely(tg3_has_work(tnapi))) {
Matt Carlson72334482009-08-28 14:03:01 +00006530 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Matt Carlson09943a12009-08-28 14:01:57 +00006531 napi_schedule(&tnapi->napi);
Michael Chand18edcb2007-03-24 20:57:11 -07006532 } else {
6533 /* No work, shared interrupt perhaps? re-enable
6534 * interrupts, and flush that PCI write
6535 */
6536 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW,
6537 0x00000000);
David S. Millerfac9b832005-05-18 22:46:34 -07006538 }
David S. Millerf47c11e2005-06-24 20:18:35 -07006539out:
David S. Millerfac9b832005-05-18 22:46:34 -07006540 return IRQ_RETVAL(handled);
6541}
6542
David Howells7d12e782006-10-05 14:55:46 +01006543static irqreturn_t tg3_interrupt_tagged(int irq, void *dev_id)
David S. Millerfac9b832005-05-18 22:46:34 -07006544{
Matt Carlson09943a12009-08-28 14:01:57 +00006545 struct tg3_napi *tnapi = dev_id;
6546 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00006547 struct tg3_hw_status *sblk = tnapi->hw_status;
David S. Millerfac9b832005-05-18 22:46:34 -07006548 unsigned int handled = 1;
6549
David S. Millerfac9b832005-05-18 22:46:34 -07006550 /* In INTx mode, it is possible for the interrupt to arrive at
6551 * the CPU before the status block posted prior to the interrupt.
6552 * Reading the PCI State register will confirm whether the
6553 * interrupt is ours and will flush the status block.
6554 */
Matt Carlson898a56f2009-08-28 14:02:40 +00006555 if (unlikely(sblk->status_tag == tnapi->last_irq_tag)) {
Joe Perches63c3a662011-04-26 08:12:10 +00006556 if (tg3_flag(tp, CHIP_RESETTING) ||
Michael Chand18edcb2007-03-24 20:57:11 -07006557 (tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
6558 handled = 0;
David S. Millerf47c11e2005-06-24 20:18:35 -07006559 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006560 }
Michael Chand18edcb2007-03-24 20:57:11 -07006561 }
6562
6563 /*
6564 * writing any value to intr-mbox-0 clears PCI INTA# and
6565 * chip-internal interrupt pending events.
6566 * writing non-zero to intr-mbox-0 additional tells the
6567 * NIC to stop sending us irqs, engaging "in-intr-handler"
6568 * event coalescing.
Michael Chanc04cb342007-05-07 00:26:15 -07006569 *
6570 * Flush the mailbox to de-assert the IRQ immediately to prevent
6571 * spurious interrupts. The flush impacts performance but
6572 * excessive spurious interrupts can be worse in some cases.
Michael Chand18edcb2007-03-24 20:57:11 -07006573 */
Michael Chanc04cb342007-05-07 00:26:15 -07006574 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 0x00000001);
Matt Carlson624f8e52009-04-20 06:55:01 +00006575
6576 /*
6577 * In a shared interrupt configuration, sometimes other devices'
6578 * interrupts will scream. We record the current status tag here
6579 * so that the above check can report that the screaming interrupts
6580 * are unhandled. Eventually they will be silenced.
6581 */
Matt Carlson898a56f2009-08-28 14:02:40 +00006582 tnapi->last_irq_tag = sblk->status_tag;
Matt Carlson624f8e52009-04-20 06:55:01 +00006583
Michael Chand18edcb2007-03-24 20:57:11 -07006584 if (tg3_irq_sync(tp))
6585 goto out;
Matt Carlson624f8e52009-04-20 06:55:01 +00006586
Matt Carlson72334482009-08-28 14:03:01 +00006587 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Matt Carlson624f8e52009-04-20 06:55:01 +00006588
Matt Carlson09943a12009-08-28 14:01:57 +00006589 napi_schedule(&tnapi->napi);
Matt Carlson624f8e52009-04-20 06:55:01 +00006590
David S. Millerf47c11e2005-06-24 20:18:35 -07006591out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006592 return IRQ_RETVAL(handled);
6593}
6594
Michael Chan79381092005-04-21 17:13:59 -07006595/* ISR for interrupt test */
David Howells7d12e782006-10-05 14:55:46 +01006596static irqreturn_t tg3_test_isr(int irq, void *dev_id)
Michael Chan79381092005-04-21 17:13:59 -07006597{
Matt Carlson09943a12009-08-28 14:01:57 +00006598 struct tg3_napi *tnapi = dev_id;
6599 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00006600 struct tg3_hw_status *sblk = tnapi->hw_status;
Michael Chan79381092005-04-21 17:13:59 -07006601
Michael Chanf9804dd2005-09-27 12:13:10 -07006602 if ((sblk->status & SD_STATUS_UPDATED) ||
6603 !(tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
Michael Chanb16250e2006-09-27 16:10:14 -07006604 tg3_disable_ints(tp);
Michael Chan79381092005-04-21 17:13:59 -07006605 return IRQ_RETVAL(1);
6606 }
6607 return IRQ_RETVAL(0);
6608}
6609
Linus Torvalds1da177e2005-04-16 15:20:36 -07006610#ifdef CONFIG_NET_POLL_CONTROLLER
6611static void tg3_poll_controller(struct net_device *dev)
6612{
Matt Carlson4f125f42009-09-01 12:55:02 +00006613 int i;
Michael Chan88b06bc22005-04-21 17:13:25 -07006614 struct tg3 *tp = netdev_priv(dev);
6615
Matt Carlson4f125f42009-09-01 12:55:02 +00006616 for (i = 0; i < tp->irq_cnt; i++)
Louis Rillingfe234f02010-03-09 06:14:41 +00006617 tg3_interrupt(tp->napi[i].irq_vec, &tp->napi[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006618}
6619#endif
6620
Linus Torvalds1da177e2005-04-16 15:20:36 -07006621static void tg3_tx_timeout(struct net_device *dev)
6622{
6623 struct tg3 *tp = netdev_priv(dev);
6624
Michael Chanb0408752007-02-13 12:18:30 -08006625 if (netif_msg_tx_err(tp)) {
Joe Perches05dbe002010-02-17 19:44:19 +00006626 netdev_err(dev, "transmit timed out, resetting\n");
Matt Carlson97bd8e42011-04-13 11:05:04 +00006627 tg3_dump_state(tp);
Michael Chanb0408752007-02-13 12:18:30 -08006628 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006629
Matt Carlsondb219972011-11-04 09:15:03 +00006630 tg3_reset_task_schedule(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006631}
6632
Michael Chanc58ec932005-09-17 00:46:27 -07006633/* Test for DMA buffers crossing any 4GB boundaries: 4G, 8G, etc */
6634static inline int tg3_4g_overflow_test(dma_addr_t mapping, int len)
6635{
6636 u32 base = (u32) mapping & 0xffffffff;
6637
Eric Dumazet807540b2010-09-23 05:40:09 +00006638 return (base > 0xffffdcc0) && (base + len + 8 < base);
Michael Chanc58ec932005-09-17 00:46:27 -07006639}
6640
Michael Chan72f2afb2006-03-06 19:28:35 -08006641/* Test for DMA addresses > 40-bit */
6642static inline int tg3_40bit_overflow_test(struct tg3 *tp, dma_addr_t mapping,
6643 int len)
6644{
6645#if defined(CONFIG_HIGHMEM) && (BITS_PER_LONG == 64)
Joe Perches63c3a662011-04-26 08:12:10 +00006646 if (tg3_flag(tp, 40BIT_DMA_BUG))
Eric Dumazet807540b2010-09-23 05:40:09 +00006647 return ((u64) mapping + len) > DMA_BIT_MASK(40);
Michael Chan72f2afb2006-03-06 19:28:35 -08006648 return 0;
6649#else
6650 return 0;
6651#endif
6652}
6653
Matt Carlsond1a3b732011-07-27 14:20:51 +00006654static inline void tg3_tx_set_bd(struct tg3_tx_buffer_desc *txbd,
Matt Carlson92cd3a12011-07-27 14:20:47 +00006655 dma_addr_t mapping, u32 len, u32 flags,
6656 u32 mss, u32 vlan)
Matt Carlson2ffcc982011-05-19 12:12:44 +00006657{
Matt Carlson92cd3a12011-07-27 14:20:47 +00006658 txbd->addr_hi = ((u64) mapping >> 32);
6659 txbd->addr_lo = ((u64) mapping & 0xffffffff);
6660 txbd->len_flags = (len << TXD_LEN_SHIFT) | (flags & 0x0000ffff);
6661 txbd->vlan_tag = (mss << TXD_MSS_SHIFT) | (vlan << TXD_VLAN_TAG_SHIFT);
Matt Carlson2ffcc982011-05-19 12:12:44 +00006662}
Linus Torvalds1da177e2005-04-16 15:20:36 -07006663
Matt Carlson84b67b22011-07-27 14:20:52 +00006664static bool tg3_tx_frag_set(struct tg3_napi *tnapi, u32 *entry, u32 *budget,
Matt Carlsond1a3b732011-07-27 14:20:51 +00006665 dma_addr_t map, u32 len, u32 flags,
6666 u32 mss, u32 vlan)
6667{
6668 struct tg3 *tp = tnapi->tp;
6669 bool hwbug = false;
6670
6671 if (tg3_flag(tp, SHORT_DMA_BUG) && len <= 8)
Rusty Russell3db1cd52011-12-19 13:56:45 +00006672 hwbug = true;
Matt Carlsond1a3b732011-07-27 14:20:51 +00006673
6674 if (tg3_4g_overflow_test(map, len))
Rusty Russell3db1cd52011-12-19 13:56:45 +00006675 hwbug = true;
Matt Carlsond1a3b732011-07-27 14:20:51 +00006676
6677 if (tg3_40bit_overflow_test(tp, map, len))
Rusty Russell3db1cd52011-12-19 13:56:45 +00006678 hwbug = true;
Matt Carlsond1a3b732011-07-27 14:20:51 +00006679
Matt Carlsona4cb4282011-12-14 11:09:58 +00006680 if (tp->dma_limit) {
Matt Carlsonb9e45482011-11-04 09:14:59 +00006681 u32 prvidx = *entry;
Matt Carlsone31aa982011-07-27 14:20:53 +00006682 u32 tmp_flag = flags & ~TXD_FLAG_END;
Matt Carlsona4cb4282011-12-14 11:09:58 +00006683 while (len > tp->dma_limit && *budget) {
6684 u32 frag_len = tp->dma_limit;
6685 len -= tp->dma_limit;
Matt Carlsone31aa982011-07-27 14:20:53 +00006686
Matt Carlsonb9e45482011-11-04 09:14:59 +00006687 /* Avoid the 8byte DMA problem */
6688 if (len <= 8) {
Matt Carlsona4cb4282011-12-14 11:09:58 +00006689 len += tp->dma_limit / 2;
6690 frag_len = tp->dma_limit / 2;
Matt Carlsone31aa982011-07-27 14:20:53 +00006691 }
6692
Matt Carlsonb9e45482011-11-04 09:14:59 +00006693 tnapi->tx_buffers[*entry].fragmented = true;
6694
6695 tg3_tx_set_bd(&tnapi->tx_ring[*entry], map,
6696 frag_len, tmp_flag, mss, vlan);
6697 *budget -= 1;
6698 prvidx = *entry;
6699 *entry = NEXT_TX(*entry);
6700
Matt Carlsone31aa982011-07-27 14:20:53 +00006701 map += frag_len;
6702 }
6703
6704 if (len) {
6705 if (*budget) {
6706 tg3_tx_set_bd(&tnapi->tx_ring[*entry], map,
6707 len, flags, mss, vlan);
Matt Carlsonb9e45482011-11-04 09:14:59 +00006708 *budget -= 1;
Matt Carlsone31aa982011-07-27 14:20:53 +00006709 *entry = NEXT_TX(*entry);
6710 } else {
Rusty Russell3db1cd52011-12-19 13:56:45 +00006711 hwbug = true;
Matt Carlsonb9e45482011-11-04 09:14:59 +00006712 tnapi->tx_buffers[prvidx].fragmented = false;
Matt Carlsone31aa982011-07-27 14:20:53 +00006713 }
6714 }
6715 } else {
Matt Carlson84b67b22011-07-27 14:20:52 +00006716 tg3_tx_set_bd(&tnapi->tx_ring[*entry], map,
6717 len, flags, mss, vlan);
Matt Carlsone31aa982011-07-27 14:20:53 +00006718 *entry = NEXT_TX(*entry);
6719 }
Matt Carlsond1a3b732011-07-27 14:20:51 +00006720
6721 return hwbug;
6722}
6723
Matt Carlson0d681b22011-07-27 14:20:49 +00006724static void tg3_tx_skb_unmap(struct tg3_napi *tnapi, u32 entry, int last)
Matt Carlson432aa7e2011-05-19 12:12:45 +00006725{
6726 int i;
Matt Carlson0d681b22011-07-27 14:20:49 +00006727 struct sk_buff *skb;
Matt Carlsondf8944c2011-07-27 14:20:46 +00006728 struct tg3_tx_ring_info *txb = &tnapi->tx_buffers[entry];
Matt Carlson432aa7e2011-05-19 12:12:45 +00006729
Matt Carlson0d681b22011-07-27 14:20:49 +00006730 skb = txb->skb;
6731 txb->skb = NULL;
6732
Matt Carlson432aa7e2011-05-19 12:12:45 +00006733 pci_unmap_single(tnapi->tp->pdev,
6734 dma_unmap_addr(txb, mapping),
6735 skb_headlen(skb),
6736 PCI_DMA_TODEVICE);
Matt Carlsone01ee142011-07-27 14:20:50 +00006737
6738 while (txb->fragmented) {
6739 txb->fragmented = false;
6740 entry = NEXT_TX(entry);
6741 txb = &tnapi->tx_buffers[entry];
6742 }
6743
Matt Carlsonba1142e2011-11-04 09:15:00 +00006744 for (i = 0; i <= last; i++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00006745 const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
Matt Carlson432aa7e2011-05-19 12:12:45 +00006746
6747 entry = NEXT_TX(entry);
6748 txb = &tnapi->tx_buffers[entry];
6749
6750 pci_unmap_page(tnapi->tp->pdev,
6751 dma_unmap_addr(txb, mapping),
Eric Dumazet9e903e02011-10-18 21:00:24 +00006752 skb_frag_size(frag), PCI_DMA_TODEVICE);
Matt Carlsone01ee142011-07-27 14:20:50 +00006753
6754 while (txb->fragmented) {
6755 txb->fragmented = false;
6756 entry = NEXT_TX(entry);
6757 txb = &tnapi->tx_buffers[entry];
6758 }
Matt Carlson432aa7e2011-05-19 12:12:45 +00006759 }
6760}
6761
Michael Chan72f2afb2006-03-06 19:28:35 -08006762/* Workaround 4GB and 40-bit hardware DMA bugs. */
Matt Carlson24f4efd2009-11-13 13:03:35 +00006763static int tigon3_dma_hwbug_workaround(struct tg3_napi *tnapi,
David S. Miller1805b2f2011-10-24 18:18:09 -04006764 struct sk_buff **pskb,
Matt Carlson84b67b22011-07-27 14:20:52 +00006765 u32 *entry, u32 *budget,
Matt Carlson92cd3a12011-07-27 14:20:47 +00006766 u32 base_flags, u32 mss, u32 vlan)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006767{
Matt Carlson24f4efd2009-11-13 13:03:35 +00006768 struct tg3 *tp = tnapi->tp;
David S. Miller1805b2f2011-10-24 18:18:09 -04006769 struct sk_buff *new_skb, *skb = *pskb;
Michael Chanc58ec932005-09-17 00:46:27 -07006770 dma_addr_t new_addr = 0;
Matt Carlson432aa7e2011-05-19 12:12:45 +00006771 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006772
Matt Carlson41588ba2008-04-19 18:12:33 -07006773 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701)
6774 new_skb = skb_copy(skb, GFP_ATOMIC);
6775 else {
6776 int more_headroom = 4 - ((unsigned long)skb->data & 3);
6777
6778 new_skb = skb_copy_expand(skb,
6779 skb_headroom(skb) + more_headroom,
6780 skb_tailroom(skb), GFP_ATOMIC);
6781 }
6782
Linus Torvalds1da177e2005-04-16 15:20:36 -07006783 if (!new_skb) {
Michael Chanc58ec932005-09-17 00:46:27 -07006784 ret = -1;
6785 } else {
6786 /* New SKB is guaranteed to be linear. */
Alexander Duyckf4188d82009-12-02 16:48:38 +00006787 new_addr = pci_map_single(tp->pdev, new_skb->data, new_skb->len,
6788 PCI_DMA_TODEVICE);
6789 /* Make sure the mapping succeeded */
6790 if (pci_dma_mapping_error(tp->pdev, new_addr)) {
Alexander Duyckf4188d82009-12-02 16:48:38 +00006791 dev_kfree_skb(new_skb);
Michael Chanc58ec932005-09-17 00:46:27 -07006792 ret = -1;
Michael Chanc58ec932005-09-17 00:46:27 -07006793 } else {
Matt Carlsonb9e45482011-11-04 09:14:59 +00006794 u32 save_entry = *entry;
6795
Matt Carlson92cd3a12011-07-27 14:20:47 +00006796 base_flags |= TXD_FLAG_END;
6797
Matt Carlson84b67b22011-07-27 14:20:52 +00006798 tnapi->tx_buffers[*entry].skb = new_skb;
6799 dma_unmap_addr_set(&tnapi->tx_buffers[*entry],
Matt Carlson432aa7e2011-05-19 12:12:45 +00006800 mapping, new_addr);
6801
Matt Carlson84b67b22011-07-27 14:20:52 +00006802 if (tg3_tx_frag_set(tnapi, entry, budget, new_addr,
Matt Carlsond1a3b732011-07-27 14:20:51 +00006803 new_skb->len, base_flags,
6804 mss, vlan)) {
Matt Carlsonba1142e2011-11-04 09:15:00 +00006805 tg3_tx_skb_unmap(tnapi, save_entry, -1);
Matt Carlsond1a3b732011-07-27 14:20:51 +00006806 dev_kfree_skb(new_skb);
6807 ret = -1;
6808 }
Michael Chanc58ec932005-09-17 00:46:27 -07006809 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006810 }
6811
Linus Torvalds1da177e2005-04-16 15:20:36 -07006812 dev_kfree_skb(skb);
David S. Miller1805b2f2011-10-24 18:18:09 -04006813 *pskb = new_skb;
Michael Chanc58ec932005-09-17 00:46:27 -07006814 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006815}
6816
Matt Carlson2ffcc982011-05-19 12:12:44 +00006817static netdev_tx_t tg3_start_xmit(struct sk_buff *, struct net_device *);
Michael Chan52c0fd82006-06-29 20:15:54 -07006818
6819/* Use GSO to workaround a rare TSO bug that may be triggered when the
6820 * TSO header is greater than 80 bytes.
6821 */
6822static int tg3_tso_bug(struct tg3 *tp, struct sk_buff *skb)
6823{
6824 struct sk_buff *segs, *nskb;
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006825 u32 frag_cnt_est = skb_shinfo(skb)->gso_segs * 3;
Michael Chan52c0fd82006-06-29 20:15:54 -07006826
6827 /* Estimate the number of fragments in the worst case */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006828 if (unlikely(tg3_tx_avail(&tp->napi[0]) <= frag_cnt_est)) {
Michael Chan52c0fd82006-06-29 20:15:54 -07006829 netif_stop_queue(tp->dev);
Matt Carlsonf65aac12010-08-02 11:26:03 +00006830
6831 /* netif_tx_stop_queue() must be done before checking
6832 * checking tx index in tg3_tx_avail() below, because in
6833 * tg3_tx(), we update tx index before checking for
6834 * netif_tx_queue_stopped().
6835 */
6836 smp_mb();
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006837 if (tg3_tx_avail(&tp->napi[0]) <= frag_cnt_est)
Michael Chan7f62ad52007-02-20 23:25:40 -08006838 return NETDEV_TX_BUSY;
6839
6840 netif_wake_queue(tp->dev);
Michael Chan52c0fd82006-06-29 20:15:54 -07006841 }
6842
6843 segs = skb_gso_segment(skb, tp->dev->features & ~NETIF_F_TSO);
Hirofumi Nakagawa801678c2008-04-29 01:03:09 -07006844 if (IS_ERR(segs))
Michael Chan52c0fd82006-06-29 20:15:54 -07006845 goto tg3_tso_bug_end;
6846
6847 do {
6848 nskb = segs;
6849 segs = segs->next;
6850 nskb->next = NULL;
Matt Carlson2ffcc982011-05-19 12:12:44 +00006851 tg3_start_xmit(nskb, tp->dev);
Michael Chan52c0fd82006-06-29 20:15:54 -07006852 } while (segs);
6853
6854tg3_tso_bug_end:
6855 dev_kfree_skb(skb);
6856
6857 return NETDEV_TX_OK;
6858}
Michael Chan52c0fd82006-06-29 20:15:54 -07006859
Michael Chan5a6f3072006-03-20 22:28:05 -08006860/* hard_start_xmit for devices that have the 4G bug and/or 40-bit bug and
Joe Perches63c3a662011-04-26 08:12:10 +00006861 * support TG3_FLAG_HW_TSO_1 or firmware TSO only.
Michael Chan5a6f3072006-03-20 22:28:05 -08006862 */
Matt Carlson2ffcc982011-05-19 12:12:44 +00006863static netdev_tx_t tg3_start_xmit(struct sk_buff *skb, struct net_device *dev)
Michael Chan5a6f3072006-03-20 22:28:05 -08006864{
6865 struct tg3 *tp = netdev_priv(dev);
Matt Carlson92cd3a12011-07-27 14:20:47 +00006866 u32 len, entry, base_flags, mss, vlan = 0;
Matt Carlson84b67b22011-07-27 14:20:52 +00006867 u32 budget;
Matt Carlson432aa7e2011-05-19 12:12:45 +00006868 int i = -1, would_hit_hwbug;
David S. Miller90079ce2008-09-11 04:52:51 -07006869 dma_addr_t mapping;
Matt Carlson24f4efd2009-11-13 13:03:35 +00006870 struct tg3_napi *tnapi;
6871 struct netdev_queue *txq;
Matt Carlson432aa7e2011-05-19 12:12:45 +00006872 unsigned int last;
Alexander Duyckf4188d82009-12-02 16:48:38 +00006873
Matt Carlson24f4efd2009-11-13 13:03:35 +00006874 txq = netdev_get_tx_queue(dev, skb_get_queue_mapping(skb));
6875 tnapi = &tp->napi[skb_get_queue_mapping(skb)];
Joe Perches63c3a662011-04-26 08:12:10 +00006876 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlson24f4efd2009-11-13 13:03:35 +00006877 tnapi++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006878
Matt Carlson84b67b22011-07-27 14:20:52 +00006879 budget = tg3_tx_avail(tnapi);
6880
Michael Chan00b70502006-06-17 21:58:45 -07006881 /* We are running in BH disabled context with netif_tx_lock
Stephen Hemmingerbea33482007-10-03 16:41:36 -07006882 * and TX reclaim runs via tp->napi.poll inside of a software
David S. Millerf47c11e2005-06-24 20:18:35 -07006883 * interrupt. Furthermore, IRQ processing runs lockless so we have
6884 * no IRQ context deadlocks to worry about either. Rejoice!
Linus Torvalds1da177e2005-04-16 15:20:36 -07006885 */
Matt Carlson84b67b22011-07-27 14:20:52 +00006886 if (unlikely(budget <= (skb_shinfo(skb)->nr_frags + 1))) {
Matt Carlson24f4efd2009-11-13 13:03:35 +00006887 if (!netif_tx_queue_stopped(txq)) {
6888 netif_tx_stop_queue(txq);
Stephen Hemminger1f064a82005-12-06 17:36:44 -08006889
6890 /* This is a hard error, log it. */
Matt Carlson5129c3a2010-04-05 10:19:23 +00006891 netdev_err(dev,
6892 "BUG! Tx Ring full when queue awake!\n");
Stephen Hemminger1f064a82005-12-06 17:36:44 -08006893 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006894 return NETDEV_TX_BUSY;
6895 }
6896
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006897 entry = tnapi->tx_prod;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006898 base_flags = 0;
Patrick McHardy84fa7932006-08-29 16:44:56 -07006899 if (skb->ip_summed == CHECKSUM_PARTIAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006900 base_flags |= TXD_FLAG_TCPUDP_CSUM;
Matt Carlson24f4efd2009-11-13 13:03:35 +00006901
Matt Carlsonbe98da62010-07-11 09:31:46 +00006902 mss = skb_shinfo(skb)->gso_size;
6903 if (mss) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07006904 struct iphdr *iph;
Matt Carlson34195c32010-07-11 09:31:42 +00006905 u32 tcp_opt_len, hdr_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006906
6907 if (skb_header_cloned(skb) &&
Eric Dumazet48855432011-10-24 07:53:03 +00006908 pskb_expand_head(skb, 0, 0, GFP_ATOMIC))
6909 goto drop;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006910
Matt Carlson34195c32010-07-11 09:31:42 +00006911 iph = ip_hdr(skb);
Arnaldo Carvalho de Meloab6a5bb2007-03-18 17:43:48 -07006912 tcp_opt_len = tcp_optlen(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006913
Eric Dumazeta5a11952012-01-23 01:22:09 +00006914 hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb) - ETH_HLEN;
Matt Carlson34195c32010-07-11 09:31:42 +00006915
Eric Dumazeta5a11952012-01-23 01:22:09 +00006916 if (!skb_is_gso_v6(skb)) {
Matt Carlson34195c32010-07-11 09:31:42 +00006917 iph->check = 0;
6918 iph->tot_len = htons(mss + hdr_len);
6919 }
6920
Michael Chan52c0fd82006-06-29 20:15:54 -07006921 if (unlikely((ETH_HLEN + hdr_len) > 80) &&
Joe Perches63c3a662011-04-26 08:12:10 +00006922 tg3_flag(tp, TSO_BUG))
Matt Carlsonde6f31e2010-04-12 06:58:30 +00006923 return tg3_tso_bug(tp, skb);
Michael Chan52c0fd82006-06-29 20:15:54 -07006924
Linus Torvalds1da177e2005-04-16 15:20:36 -07006925 base_flags |= (TXD_FLAG_CPU_PRE_DMA |
6926 TXD_FLAG_CPU_POST_DMA);
6927
Joe Perches63c3a662011-04-26 08:12:10 +00006928 if (tg3_flag(tp, HW_TSO_1) ||
6929 tg3_flag(tp, HW_TSO_2) ||
6930 tg3_flag(tp, HW_TSO_3)) {
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07006931 tcp_hdr(skb)->check = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006932 base_flags &= ~TXD_FLAG_TCPUDP_CSUM;
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07006933 } else
6934 tcp_hdr(skb)->check = ~csum_tcpudp_magic(iph->saddr,
6935 iph->daddr, 0,
6936 IPPROTO_TCP,
6937 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006938
Joe Perches63c3a662011-04-26 08:12:10 +00006939 if (tg3_flag(tp, HW_TSO_3)) {
Matt Carlson615774f2009-11-13 13:03:39 +00006940 mss |= (hdr_len & 0xc) << 12;
6941 if (hdr_len & 0x10)
6942 base_flags |= 0x00000010;
6943 base_flags |= (hdr_len & 0x3e0) << 5;
Joe Perches63c3a662011-04-26 08:12:10 +00006944 } else if (tg3_flag(tp, HW_TSO_2))
Matt Carlson92c6b8d2009-11-02 14:23:27 +00006945 mss |= hdr_len << 9;
Joe Perches63c3a662011-04-26 08:12:10 +00006946 else if (tg3_flag(tp, HW_TSO_1) ||
Matt Carlson92c6b8d2009-11-02 14:23:27 +00006947 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07006948 if (tcp_opt_len || iph->ihl > 5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006949 int tsflags;
6950
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07006951 tsflags = (iph->ihl - 5) + (tcp_opt_len >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006952 mss |= (tsflags << 11);
6953 }
6954 } else {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07006955 if (tcp_opt_len || iph->ihl > 5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006956 int tsflags;
6957
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07006958 tsflags = (iph->ihl - 5) + (tcp_opt_len >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006959 base_flags |= tsflags << 12;
6960 }
6961 }
6962 }
Matt Carlsonbf933c82011-01-25 15:58:49 +00006963
Matt Carlson93a700a2011-08-31 11:44:54 +00006964 if (tg3_flag(tp, USE_JUMBO_BDFLAG) &&
6965 !mss && skb->len > VLAN_ETH_FRAME_LEN)
6966 base_flags |= TXD_FLAG_JMB_PKT;
6967
Matt Carlson92cd3a12011-07-27 14:20:47 +00006968 if (vlan_tx_tag_present(skb)) {
6969 base_flags |= TXD_FLAG_VLAN;
6970 vlan = vlan_tx_tag_get(skb);
6971 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006972
Alexander Duyckf4188d82009-12-02 16:48:38 +00006973 len = skb_headlen(skb);
6974
6975 mapping = pci_map_single(tp->pdev, skb->data, len, PCI_DMA_TODEVICE);
Eric Dumazet48855432011-10-24 07:53:03 +00006976 if (pci_dma_mapping_error(tp->pdev, mapping))
6977 goto drop;
6978
David S. Miller90079ce2008-09-11 04:52:51 -07006979
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006980 tnapi->tx_buffers[entry].skb = skb;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006981 dma_unmap_addr_set(&tnapi->tx_buffers[entry], mapping, mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006982
6983 would_hit_hwbug = 0;
6984
Joe Perches63c3a662011-04-26 08:12:10 +00006985 if (tg3_flag(tp, 5701_DMA_BUG))
Michael Chanc58ec932005-09-17 00:46:27 -07006986 would_hit_hwbug = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006987
Matt Carlson84b67b22011-07-27 14:20:52 +00006988 if (tg3_tx_frag_set(tnapi, &entry, &budget, mapping, len, base_flags |
Matt Carlsond1a3b732011-07-27 14:20:51 +00006989 ((skb_shinfo(skb)->nr_frags == 0) ? TXD_FLAG_END : 0),
Matt Carlsonba1142e2011-11-04 09:15:00 +00006990 mss, vlan)) {
Matt Carlsond1a3b732011-07-27 14:20:51 +00006991 would_hit_hwbug = 1;
Matt Carlsonba1142e2011-11-04 09:15:00 +00006992 } else if (skb_shinfo(skb)->nr_frags > 0) {
Matt Carlson92cd3a12011-07-27 14:20:47 +00006993 u32 tmp_mss = mss;
6994
6995 if (!tg3_flag(tp, HW_TSO_1) &&
6996 !tg3_flag(tp, HW_TSO_2) &&
6997 !tg3_flag(tp, HW_TSO_3))
6998 tmp_mss = 0;
6999
Matt Carlsonc5665a52012-02-13 10:20:12 +00007000 /* Now loop through additional data
7001 * fragments, and queue them.
7002 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007003 last = skb_shinfo(skb)->nr_frags - 1;
7004 for (i = 0; i <= last; i++) {
7005 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
7006
Eric Dumazet9e903e02011-10-18 21:00:24 +00007007 len = skb_frag_size(frag);
Ian Campbelldc234d02011-08-24 22:28:11 +00007008 mapping = skb_frag_dma_map(&tp->pdev->dev, frag, 0,
Ian Campbell5d6bcdf2011-10-06 11:10:48 +01007009 len, DMA_TO_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007010
Matt Carlsonf3f3f272009-08-28 14:03:21 +00007011 tnapi->tx_buffers[entry].skb = NULL;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00007012 dma_unmap_addr_set(&tnapi->tx_buffers[entry], mapping,
Alexander Duyckf4188d82009-12-02 16:48:38 +00007013 mapping);
Ian Campbell5d6bcdf2011-10-06 11:10:48 +01007014 if (dma_mapping_error(&tp->pdev->dev, mapping))
Alexander Duyckf4188d82009-12-02 16:48:38 +00007015 goto dma_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007016
Matt Carlsonb9e45482011-11-04 09:14:59 +00007017 if (!budget ||
7018 tg3_tx_frag_set(tnapi, &entry, &budget, mapping,
Matt Carlson84b67b22011-07-27 14:20:52 +00007019 len, base_flags |
7020 ((i == last) ? TXD_FLAG_END : 0),
Matt Carlsonb9e45482011-11-04 09:14:59 +00007021 tmp_mss, vlan)) {
Matt Carlson92c6b8d2009-11-02 14:23:27 +00007022 would_hit_hwbug = 1;
Matt Carlsonb9e45482011-11-04 09:14:59 +00007023 break;
7024 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007025 }
7026 }
7027
7028 if (would_hit_hwbug) {
Matt Carlson0d681b22011-07-27 14:20:49 +00007029 tg3_tx_skb_unmap(tnapi, tnapi->tx_prod, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007030
7031 /* If the workaround fails due to memory/mapping
7032 * failure, silently drop this packet.
7033 */
Matt Carlson84b67b22011-07-27 14:20:52 +00007034 entry = tnapi->tx_prod;
7035 budget = tg3_tx_avail(tnapi);
David S. Miller1805b2f2011-10-24 18:18:09 -04007036 if (tigon3_dma_hwbug_workaround(tnapi, &skb, &entry, &budget,
Matt Carlson84b67b22011-07-27 14:20:52 +00007037 base_flags, mss, vlan))
Eric Dumazet48855432011-10-24 07:53:03 +00007038 goto drop_nofree;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007039 }
7040
Richard Cochrand515b452011-06-19 03:31:41 +00007041 skb_tx_timestamp(skb);
Tom Herbert5cb917b2012-03-05 19:53:50 +00007042 netdev_tx_sent_queue(txq, skb->len);
Richard Cochrand515b452011-06-19 03:31:41 +00007043
Michael Chan6541b802012-03-04 14:48:14 +00007044 /* Sync BD data before updating mailbox */
7045 wmb();
7046
Linus Torvalds1da177e2005-04-16 15:20:36 -07007047 /* Packets are ready, update Tx producer idx local and on card. */
Matt Carlson24f4efd2009-11-13 13:03:35 +00007048 tw32_tx_mbox(tnapi->prodmbox, entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007049
Matt Carlsonf3f3f272009-08-28 14:03:21 +00007050 tnapi->tx_prod = entry;
7051 if (unlikely(tg3_tx_avail(tnapi) <= (MAX_SKB_FRAGS + 1))) {
Matt Carlson24f4efd2009-11-13 13:03:35 +00007052 netif_tx_stop_queue(txq);
Matt Carlsonf65aac12010-08-02 11:26:03 +00007053
7054 /* netif_tx_stop_queue() must be done before checking
7055 * checking tx index in tg3_tx_avail() below, because in
7056 * tg3_tx(), we update tx index before checking for
7057 * netif_tx_queue_stopped().
7058 */
7059 smp_mb();
Matt Carlsonf3f3f272009-08-28 14:03:21 +00007060 if (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi))
Matt Carlson24f4efd2009-11-13 13:03:35 +00007061 netif_tx_wake_queue(txq);
Michael Chan51b91462005-09-01 17:41:28 -07007062 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007063
Eric Dumazetcdd0db02009-05-28 00:00:41 +00007064 mmiowb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007065 return NETDEV_TX_OK;
Alexander Duyckf4188d82009-12-02 16:48:38 +00007066
7067dma_error:
Matt Carlsonba1142e2011-11-04 09:15:00 +00007068 tg3_tx_skb_unmap(tnapi, tnapi->tx_prod, --i);
Matt Carlson432aa7e2011-05-19 12:12:45 +00007069 tnapi->tx_buffers[tnapi->tx_prod].skb = NULL;
Eric Dumazet48855432011-10-24 07:53:03 +00007070drop:
7071 dev_kfree_skb(skb);
7072drop_nofree:
7073 tp->tx_dropped++;
Alexander Duyckf4188d82009-12-02 16:48:38 +00007074 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007075}
7076
Matt Carlson6e01b202011-08-19 13:58:20 +00007077static void tg3_mac_loopback(struct tg3 *tp, bool enable)
7078{
7079 if (enable) {
7080 tp->mac_mode &= ~(MAC_MODE_HALF_DUPLEX |
7081 MAC_MODE_PORT_MODE_MASK);
7082
7083 tp->mac_mode |= MAC_MODE_PORT_INT_LPBACK;
7084
7085 if (!tg3_flag(tp, 5705_PLUS))
7086 tp->mac_mode |= MAC_MODE_LINK_POLARITY;
7087
7088 if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
7089 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
7090 else
7091 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
7092 } else {
7093 tp->mac_mode &= ~MAC_MODE_PORT_INT_LPBACK;
7094
7095 if (tg3_flag(tp, 5705_PLUS) ||
7096 (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) ||
7097 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700)
7098 tp->mac_mode &= ~MAC_MODE_LINK_POLARITY;
7099 }
7100
7101 tw32(MAC_MODE, tp->mac_mode);
7102 udelay(40);
7103}
7104
Matt Carlson941ec902011-08-19 13:58:23 +00007105static int tg3_phy_lpbk_set(struct tg3 *tp, u32 speed, bool extlpbk)
Matt Carlson5e5a7f32011-08-19 13:58:21 +00007106{
Matt Carlson941ec902011-08-19 13:58:23 +00007107 u32 val, bmcr, mac_mode, ptest = 0;
Matt Carlson5e5a7f32011-08-19 13:58:21 +00007108
7109 tg3_phy_toggle_apd(tp, false);
7110 tg3_phy_toggle_automdix(tp, 0);
7111
Matt Carlson941ec902011-08-19 13:58:23 +00007112 if (extlpbk && tg3_phy_set_extloopbk(tp))
7113 return -EIO;
7114
7115 bmcr = BMCR_FULLDPLX;
Matt Carlson5e5a7f32011-08-19 13:58:21 +00007116 switch (speed) {
7117 case SPEED_10:
7118 break;
7119 case SPEED_100:
7120 bmcr |= BMCR_SPEED100;
7121 break;
7122 case SPEED_1000:
7123 default:
7124 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
7125 speed = SPEED_100;
7126 bmcr |= BMCR_SPEED100;
7127 } else {
7128 speed = SPEED_1000;
7129 bmcr |= BMCR_SPEED1000;
7130 }
7131 }
7132
Matt Carlson941ec902011-08-19 13:58:23 +00007133 if (extlpbk) {
7134 if (!(tp->phy_flags & TG3_PHYFLG_IS_FET)) {
7135 tg3_readphy(tp, MII_CTRL1000, &val);
7136 val |= CTL1000_AS_MASTER |
7137 CTL1000_ENABLE_MASTER;
7138 tg3_writephy(tp, MII_CTRL1000, val);
7139 } else {
7140 ptest = MII_TG3_FET_PTEST_TRIM_SEL |
7141 MII_TG3_FET_PTEST_TRIM_2;
7142 tg3_writephy(tp, MII_TG3_FET_PTEST, ptest);
7143 }
7144 } else
7145 bmcr |= BMCR_LOOPBACK;
7146
Matt Carlson5e5a7f32011-08-19 13:58:21 +00007147 tg3_writephy(tp, MII_BMCR, bmcr);
7148
7149 /* The write needs to be flushed for the FETs */
7150 if (tp->phy_flags & TG3_PHYFLG_IS_FET)
7151 tg3_readphy(tp, MII_BMCR, &bmcr);
7152
7153 udelay(40);
7154
7155 if ((tp->phy_flags & TG3_PHYFLG_IS_FET) &&
7156 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785) {
Matt Carlson941ec902011-08-19 13:58:23 +00007157 tg3_writephy(tp, MII_TG3_FET_PTEST, ptest |
Matt Carlson5e5a7f32011-08-19 13:58:21 +00007158 MII_TG3_FET_PTEST_FRC_TX_LINK |
7159 MII_TG3_FET_PTEST_FRC_TX_LOCK);
7160
7161 /* The write needs to be flushed for the AC131 */
7162 tg3_readphy(tp, MII_TG3_FET_PTEST, &val);
7163 }
7164
7165 /* Reset to prevent losing 1st rx packet intermittently */
7166 if ((tp->phy_flags & TG3_PHYFLG_MII_SERDES) &&
7167 tg3_flag(tp, 5780_CLASS)) {
7168 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
7169 udelay(10);
7170 tw32_f(MAC_RX_MODE, tp->rx_mode);
7171 }
7172
7173 mac_mode = tp->mac_mode &
7174 ~(MAC_MODE_PORT_MODE_MASK | MAC_MODE_HALF_DUPLEX);
7175 if (speed == SPEED_1000)
7176 mac_mode |= MAC_MODE_PORT_MODE_GMII;
7177 else
7178 mac_mode |= MAC_MODE_PORT_MODE_MII;
7179
7180 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700) {
7181 u32 masked_phy_id = tp->phy_id & TG3_PHY_ID_MASK;
7182
7183 if (masked_phy_id == TG3_PHY_ID_BCM5401)
7184 mac_mode &= ~MAC_MODE_LINK_POLARITY;
7185 else if (masked_phy_id == TG3_PHY_ID_BCM5411)
7186 mac_mode |= MAC_MODE_LINK_POLARITY;
7187
7188 tg3_writephy(tp, MII_TG3_EXT_CTRL,
7189 MII_TG3_EXT_CTRL_LNK3_LED_MODE);
7190 }
7191
7192 tw32(MAC_MODE, mac_mode);
7193 udelay(40);
Matt Carlson941ec902011-08-19 13:58:23 +00007194
7195 return 0;
Matt Carlson5e5a7f32011-08-19 13:58:21 +00007196}
7197
Michał Mirosławc8f44af2011-11-15 15:29:55 +00007198static void tg3_set_loopback(struct net_device *dev, netdev_features_t features)
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00007199{
7200 struct tg3 *tp = netdev_priv(dev);
7201
7202 if (features & NETIF_F_LOOPBACK) {
7203 if (tp->mac_mode & MAC_MODE_PORT_INT_LPBACK)
7204 return;
7205
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00007206 spin_lock_bh(&tp->lock);
Matt Carlson6e01b202011-08-19 13:58:20 +00007207 tg3_mac_loopback(tp, true);
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00007208 netif_carrier_on(tp->dev);
7209 spin_unlock_bh(&tp->lock);
7210 netdev_info(dev, "Internal MAC loopback mode enabled.\n");
7211 } else {
7212 if (!(tp->mac_mode & MAC_MODE_PORT_INT_LPBACK))
7213 return;
7214
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00007215 spin_lock_bh(&tp->lock);
Matt Carlson6e01b202011-08-19 13:58:20 +00007216 tg3_mac_loopback(tp, false);
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00007217 /* Force link status check */
7218 tg3_setup_phy(tp, 1);
7219 spin_unlock_bh(&tp->lock);
7220 netdev_info(dev, "Internal MAC loopback mode disabled.\n");
7221 }
7222}
7223
Michał Mirosławc8f44af2011-11-15 15:29:55 +00007224static netdev_features_t tg3_fix_features(struct net_device *dev,
7225 netdev_features_t features)
Michał Mirosławdc668912011-04-07 03:35:07 +00007226{
7227 struct tg3 *tp = netdev_priv(dev);
7228
Joe Perches63c3a662011-04-26 08:12:10 +00007229 if (dev->mtu > ETH_DATA_LEN && tg3_flag(tp, 5780_CLASS))
Michał Mirosławdc668912011-04-07 03:35:07 +00007230 features &= ~NETIF_F_ALL_TSO;
7231
7232 return features;
7233}
7234
Michał Mirosławc8f44af2011-11-15 15:29:55 +00007235static int tg3_set_features(struct net_device *dev, netdev_features_t features)
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00007236{
Michał Mirosławc8f44af2011-11-15 15:29:55 +00007237 netdev_features_t changed = dev->features ^ features;
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00007238
7239 if ((changed & NETIF_F_LOOPBACK) && netif_running(dev))
7240 tg3_set_loopback(dev, features);
7241
7242 return 0;
7243}
7244
Matt Carlson21f581a2009-08-28 14:00:25 +00007245static void tg3_rx_prodring_free(struct tg3 *tp,
7246 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007247{
Linus Torvalds1da177e2005-04-16 15:20:36 -07007248 int i;
7249
Matt Carlson8fea32b2010-09-15 08:59:58 +00007250 if (tpr != &tp->napi[0].prodring) {
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007251 for (i = tpr->rx_std_cons_idx; i != tpr->rx_std_prod_idx;
Matt Carlson2c49a442010-09-30 10:34:35 +00007252 i = (i + 1) & tp->rx_std_ring_mask)
Eric Dumazet9205fd92011-11-18 06:47:01 +00007253 tg3_rx_data_free(tp, &tpr->rx_std_buffers[i],
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007254 tp->rx_pkt_map_sz);
7255
Joe Perches63c3a662011-04-26 08:12:10 +00007256 if (tg3_flag(tp, JUMBO_CAPABLE)) {
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007257 for (i = tpr->rx_jmb_cons_idx;
7258 i != tpr->rx_jmb_prod_idx;
Matt Carlson2c49a442010-09-30 10:34:35 +00007259 i = (i + 1) & tp->rx_jmb_ring_mask) {
Eric Dumazet9205fd92011-11-18 06:47:01 +00007260 tg3_rx_data_free(tp, &tpr->rx_jmb_buffers[i],
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007261 TG3_RX_JMB_MAP_SZ);
7262 }
7263 }
7264
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007265 return;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007266 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007267
Matt Carlson2c49a442010-09-30 10:34:35 +00007268 for (i = 0; i <= tp->rx_std_ring_mask; i++)
Eric Dumazet9205fd92011-11-18 06:47:01 +00007269 tg3_rx_data_free(tp, &tpr->rx_std_buffers[i],
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007270 tp->rx_pkt_map_sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007271
Joe Perches63c3a662011-04-26 08:12:10 +00007272 if (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS)) {
Matt Carlson2c49a442010-09-30 10:34:35 +00007273 for (i = 0; i <= tp->rx_jmb_ring_mask; i++)
Eric Dumazet9205fd92011-11-18 06:47:01 +00007274 tg3_rx_data_free(tp, &tpr->rx_jmb_buffers[i],
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007275 TG3_RX_JMB_MAP_SZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007276 }
7277}
7278
Matt Carlsonc6cdf432010-04-05 10:19:26 +00007279/* Initialize rx rings for packet processing.
Linus Torvalds1da177e2005-04-16 15:20:36 -07007280 *
7281 * The chip has been shut down and the driver detached from
7282 * the networking, so no interrupts or new tx packets will
7283 * end up in the driver. tp->{tx,}lock are held and thus
7284 * we may not sleep.
7285 */
Matt Carlson21f581a2009-08-28 14:00:25 +00007286static int tg3_rx_prodring_alloc(struct tg3 *tp,
7287 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007288{
Matt Carlson287be122009-08-28 13:58:46 +00007289 u32 i, rx_pkt_dma_sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007290
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007291 tpr->rx_std_cons_idx = 0;
7292 tpr->rx_std_prod_idx = 0;
7293 tpr->rx_jmb_cons_idx = 0;
7294 tpr->rx_jmb_prod_idx = 0;
7295
Matt Carlson8fea32b2010-09-15 08:59:58 +00007296 if (tpr != &tp->napi[0].prodring) {
Matt Carlson2c49a442010-09-30 10:34:35 +00007297 memset(&tpr->rx_std_buffers[0], 0,
7298 TG3_RX_STD_BUFF_RING_SIZE(tp));
Matt Carlson48035722010-10-14 10:37:43 +00007299 if (tpr->rx_jmb_buffers)
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007300 memset(&tpr->rx_jmb_buffers[0], 0,
Matt Carlson2c49a442010-09-30 10:34:35 +00007301 TG3_RX_JMB_BUFF_RING_SIZE(tp));
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007302 goto done;
7303 }
7304
Linus Torvalds1da177e2005-04-16 15:20:36 -07007305 /* Zero out all descriptors. */
Matt Carlson2c49a442010-09-30 10:34:35 +00007306 memset(tpr->rx_std, 0, TG3_RX_STD_RING_BYTES(tp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007307
Matt Carlson287be122009-08-28 13:58:46 +00007308 rx_pkt_dma_sz = TG3_RX_STD_DMA_SZ;
Joe Perches63c3a662011-04-26 08:12:10 +00007309 if (tg3_flag(tp, 5780_CLASS) &&
Matt Carlson287be122009-08-28 13:58:46 +00007310 tp->dev->mtu > ETH_DATA_LEN)
7311 rx_pkt_dma_sz = TG3_RX_JMB_DMA_SZ;
7312 tp->rx_pkt_map_sz = TG3_RX_DMA_TO_MAP_SZ(rx_pkt_dma_sz);
Michael Chan7e72aad2005-07-25 12:31:17 -07007313
Linus Torvalds1da177e2005-04-16 15:20:36 -07007314 /* Initialize invariants of the rings, we only set this
7315 * stuff once. This works because the card does not
7316 * write into the rx buffer posting rings.
7317 */
Matt Carlson2c49a442010-09-30 10:34:35 +00007318 for (i = 0; i <= tp->rx_std_ring_mask; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007319 struct tg3_rx_buffer_desc *rxd;
7320
Matt Carlson21f581a2009-08-28 14:00:25 +00007321 rxd = &tpr->rx_std[i];
Matt Carlson287be122009-08-28 13:58:46 +00007322 rxd->idx_len = rx_pkt_dma_sz << RXD_LEN_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007323 rxd->type_flags = (RXD_FLAG_END << RXD_FLAGS_SHIFT);
7324 rxd->opaque = (RXD_OPAQUE_RING_STD |
7325 (i << RXD_OPAQUE_INDEX_SHIFT));
7326 }
7327
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007328 /* Now allocate fresh SKBs for each rx ring. */
7329 for (i = 0; i < tp->rx_pending; i++) {
Eric Dumazet8d4057a2012-04-27 00:34:49 +00007330 unsigned int frag_size;
7331
7332 if (tg3_alloc_rx_data(tp, tpr, RXD_OPAQUE_RING_STD, i,
7333 &frag_size) < 0) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00007334 netdev_warn(tp->dev,
7335 "Using a smaller RX standard ring. Only "
7336 "%d out of %d buffers were allocated "
7337 "successfully\n", i, tp->rx_pending);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007338 if (i == 0)
7339 goto initfail;
7340 tp->rx_pending = i;
7341 break;
7342 }
7343 }
7344
Joe Perches63c3a662011-04-26 08:12:10 +00007345 if (!tg3_flag(tp, JUMBO_CAPABLE) || tg3_flag(tp, 5780_CLASS))
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007346 goto done;
7347
Matt Carlson2c49a442010-09-30 10:34:35 +00007348 memset(tpr->rx_jmb, 0, TG3_RX_JMB_RING_BYTES(tp));
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007349
Joe Perches63c3a662011-04-26 08:12:10 +00007350 if (!tg3_flag(tp, JUMBO_RING_ENABLE))
Matt Carlson0d86df82010-02-17 15:17:00 +00007351 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007352
Matt Carlson2c49a442010-09-30 10:34:35 +00007353 for (i = 0; i <= tp->rx_jmb_ring_mask; i++) {
Matt Carlson0d86df82010-02-17 15:17:00 +00007354 struct tg3_rx_buffer_desc *rxd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007355
Matt Carlson0d86df82010-02-17 15:17:00 +00007356 rxd = &tpr->rx_jmb[i].std;
7357 rxd->idx_len = TG3_RX_JMB_DMA_SZ << RXD_LEN_SHIFT;
7358 rxd->type_flags = (RXD_FLAG_END << RXD_FLAGS_SHIFT) |
7359 RXD_FLAG_JUMBO;
7360 rxd->opaque = (RXD_OPAQUE_RING_JUMBO |
7361 (i << RXD_OPAQUE_INDEX_SHIFT));
7362 }
7363
7364 for (i = 0; i < tp->rx_jumbo_pending; i++) {
Eric Dumazet8d4057a2012-04-27 00:34:49 +00007365 unsigned int frag_size;
7366
7367 if (tg3_alloc_rx_data(tp, tpr, RXD_OPAQUE_RING_JUMBO, i,
7368 &frag_size) < 0) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00007369 netdev_warn(tp->dev,
7370 "Using a smaller RX jumbo ring. Only %d "
7371 "out of %d buffers were allocated "
7372 "successfully\n", i, tp->rx_jumbo_pending);
Matt Carlson0d86df82010-02-17 15:17:00 +00007373 if (i == 0)
7374 goto initfail;
7375 tp->rx_jumbo_pending = i;
7376 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007377 }
7378 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007379
7380done:
Michael Chan32d8c572006-07-25 16:38:29 -07007381 return 0;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007382
7383initfail:
Matt Carlson21f581a2009-08-28 14:00:25 +00007384 tg3_rx_prodring_free(tp, tpr);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007385 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007386}
7387
Matt Carlson21f581a2009-08-28 14:00:25 +00007388static void tg3_rx_prodring_fini(struct tg3 *tp,
7389 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007390{
Matt Carlson21f581a2009-08-28 14:00:25 +00007391 kfree(tpr->rx_std_buffers);
7392 tpr->rx_std_buffers = NULL;
7393 kfree(tpr->rx_jmb_buffers);
7394 tpr->rx_jmb_buffers = NULL;
7395 if (tpr->rx_std) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00007396 dma_free_coherent(&tp->pdev->dev, TG3_RX_STD_RING_BYTES(tp),
7397 tpr->rx_std, tpr->rx_std_mapping);
Matt Carlson21f581a2009-08-28 14:00:25 +00007398 tpr->rx_std = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007399 }
Matt Carlson21f581a2009-08-28 14:00:25 +00007400 if (tpr->rx_jmb) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00007401 dma_free_coherent(&tp->pdev->dev, TG3_RX_JMB_RING_BYTES(tp),
7402 tpr->rx_jmb, tpr->rx_jmb_mapping);
Matt Carlson21f581a2009-08-28 14:00:25 +00007403 tpr->rx_jmb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007404 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007405}
7406
Matt Carlson21f581a2009-08-28 14:00:25 +00007407static int tg3_rx_prodring_init(struct tg3 *tp,
7408 struct tg3_rx_prodring_set *tpr)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007409{
Matt Carlson2c49a442010-09-30 10:34:35 +00007410 tpr->rx_std_buffers = kzalloc(TG3_RX_STD_BUFF_RING_SIZE(tp),
7411 GFP_KERNEL);
Matt Carlson21f581a2009-08-28 14:00:25 +00007412 if (!tpr->rx_std_buffers)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007413 return -ENOMEM;
7414
Matt Carlson4bae65c2010-11-24 08:31:52 +00007415 tpr->rx_std = dma_alloc_coherent(&tp->pdev->dev,
7416 TG3_RX_STD_RING_BYTES(tp),
7417 &tpr->rx_std_mapping,
7418 GFP_KERNEL);
Matt Carlson21f581a2009-08-28 14:00:25 +00007419 if (!tpr->rx_std)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007420 goto err_out;
7421
Joe Perches63c3a662011-04-26 08:12:10 +00007422 if (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS)) {
Matt Carlson2c49a442010-09-30 10:34:35 +00007423 tpr->rx_jmb_buffers = kzalloc(TG3_RX_JMB_BUFF_RING_SIZE(tp),
Matt Carlson21f581a2009-08-28 14:00:25 +00007424 GFP_KERNEL);
7425 if (!tpr->rx_jmb_buffers)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007426 goto err_out;
7427
Matt Carlson4bae65c2010-11-24 08:31:52 +00007428 tpr->rx_jmb = dma_alloc_coherent(&tp->pdev->dev,
7429 TG3_RX_JMB_RING_BYTES(tp),
7430 &tpr->rx_jmb_mapping,
7431 GFP_KERNEL);
Matt Carlson21f581a2009-08-28 14:00:25 +00007432 if (!tpr->rx_jmb)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007433 goto err_out;
7434 }
7435
7436 return 0;
7437
7438err_out:
Matt Carlson21f581a2009-08-28 14:00:25 +00007439 tg3_rx_prodring_fini(tp, tpr);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007440 return -ENOMEM;
7441}
7442
7443/* Free up pending packets in all rx/tx rings.
7444 *
7445 * The chip has been shut down and the driver detached from
7446 * the networking, so no interrupts or new tx packets will
7447 * end up in the driver. tp->{tx,}lock is not held and we are not
7448 * in an interrupt context and thus may sleep.
7449 */
7450static void tg3_free_rings(struct tg3 *tp)
7451{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007452 int i, j;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007453
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007454 for (j = 0; j < tp->irq_cnt; j++) {
7455 struct tg3_napi *tnapi = &tp->napi[j];
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007456
Matt Carlson8fea32b2010-09-15 08:59:58 +00007457 tg3_rx_prodring_free(tp, &tnapi->prodring);
Matt Carlsonb28f6422010-06-05 17:24:32 +00007458
Matt Carlson0c1d0e22009-09-01 13:16:33 +00007459 if (!tnapi->tx_buffers)
7460 continue;
7461
Matt Carlson0d681b22011-07-27 14:20:49 +00007462 for (i = 0; i < TG3_TX_RING_SIZE; i++) {
7463 struct sk_buff *skb = tnapi->tx_buffers[i].skb;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007464
Matt Carlson0d681b22011-07-27 14:20:49 +00007465 if (!skb)
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007466 continue;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007467
Matt Carlsonba1142e2011-11-04 09:15:00 +00007468 tg3_tx_skb_unmap(tnapi, i,
7469 skb_shinfo(skb)->nr_frags - 1);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007470
7471 dev_kfree_skb_any(skb);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007472 }
Tom Herbert5cb917b2012-03-05 19:53:50 +00007473 netdev_tx_reset_queue(netdev_get_tx_queue(tp->dev, j));
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007474 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007475}
7476
7477/* Initialize tx/rx rings for packet processing.
7478 *
7479 * The chip has been shut down and the driver detached from
7480 * the networking, so no interrupts or new tx packets will
7481 * end up in the driver. tp->{tx,}lock are held and thus
7482 * we may not sleep.
7483 */
7484static int tg3_init_rings(struct tg3 *tp)
7485{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007486 int i;
Matt Carlson72334482009-08-28 14:03:01 +00007487
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007488 /* Free up all the SKBs. */
7489 tg3_free_rings(tp);
7490
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007491 for (i = 0; i < tp->irq_cnt; i++) {
7492 struct tg3_napi *tnapi = &tp->napi[i];
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007493
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007494 tnapi->last_tag = 0;
7495 tnapi->last_irq_tag = 0;
7496 tnapi->hw_status->status = 0;
7497 tnapi->hw_status->status_tag = 0;
7498 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
7499
7500 tnapi->tx_prod = 0;
7501 tnapi->tx_cons = 0;
Matt Carlson0c1d0e22009-09-01 13:16:33 +00007502 if (tnapi->tx_ring)
7503 memset(tnapi->tx_ring, 0, TG3_TX_RING_BYTES);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007504
7505 tnapi->rx_rcb_ptr = 0;
Matt Carlson0c1d0e22009-09-01 13:16:33 +00007506 if (tnapi->rx_rcb)
7507 memset(tnapi->rx_rcb, 0, TG3_RX_RCB_RING_BYTES(tp));
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007508
Matt Carlson8fea32b2010-09-15 08:59:58 +00007509 if (tg3_rx_prodring_alloc(tp, &tnapi->prodring)) {
Matt Carlsone4af1af2010-02-12 14:47:05 +00007510 tg3_free_rings(tp);
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007511 return -ENOMEM;
Matt Carlsone4af1af2010-02-12 14:47:05 +00007512 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007513 }
Matt Carlson72334482009-08-28 14:03:01 +00007514
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007515 return 0;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007516}
7517
7518/*
7519 * Must not be invoked with interrupt sources disabled and
7520 * the hardware shutdown down.
7521 */
7522static void tg3_free_consistent(struct tg3 *tp)
7523{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007524 int i;
Matt Carlson898a56f2009-08-28 14:02:40 +00007525
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007526 for (i = 0; i < tp->irq_cnt; i++) {
7527 struct tg3_napi *tnapi = &tp->napi[i];
7528
7529 if (tnapi->tx_ring) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00007530 dma_free_coherent(&tp->pdev->dev, TG3_TX_RING_BYTES,
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007531 tnapi->tx_ring, tnapi->tx_desc_mapping);
7532 tnapi->tx_ring = NULL;
7533 }
7534
7535 kfree(tnapi->tx_buffers);
7536 tnapi->tx_buffers = NULL;
7537
7538 if (tnapi->rx_rcb) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00007539 dma_free_coherent(&tp->pdev->dev,
7540 TG3_RX_RCB_RING_BYTES(tp),
7541 tnapi->rx_rcb,
7542 tnapi->rx_rcb_mapping);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007543 tnapi->rx_rcb = NULL;
7544 }
7545
Matt Carlson8fea32b2010-09-15 08:59:58 +00007546 tg3_rx_prodring_fini(tp, &tnapi->prodring);
7547
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007548 if (tnapi->hw_status) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00007549 dma_free_coherent(&tp->pdev->dev, TG3_HW_STATUS_SIZE,
7550 tnapi->hw_status,
7551 tnapi->status_mapping);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007552 tnapi->hw_status = NULL;
7553 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007554 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007555
Linus Torvalds1da177e2005-04-16 15:20:36 -07007556 if (tp->hw_stats) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00007557 dma_free_coherent(&tp->pdev->dev, sizeof(struct tg3_hw_stats),
7558 tp->hw_stats, tp->stats_mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007559 tp->hw_stats = NULL;
7560 }
7561}
7562
7563/*
7564 * Must not be invoked with interrupt sources disabled and
7565 * the hardware shutdown down. Can sleep.
7566 */
7567static int tg3_alloc_consistent(struct tg3 *tp)
7568{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007569 int i;
Matt Carlson898a56f2009-08-28 14:02:40 +00007570
Matt Carlson4bae65c2010-11-24 08:31:52 +00007571 tp->hw_stats = dma_alloc_coherent(&tp->pdev->dev,
7572 sizeof(struct tg3_hw_stats),
7573 &tp->stats_mapping,
7574 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007575 if (!tp->hw_stats)
7576 goto err_out;
7577
Linus Torvalds1da177e2005-04-16 15:20:36 -07007578 memset(tp->hw_stats, 0, sizeof(struct tg3_hw_stats));
7579
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007580 for (i = 0; i < tp->irq_cnt; i++) {
7581 struct tg3_napi *tnapi = &tp->napi[i];
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00007582 struct tg3_hw_status *sblk;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007583
Matt Carlson4bae65c2010-11-24 08:31:52 +00007584 tnapi->hw_status = dma_alloc_coherent(&tp->pdev->dev,
7585 TG3_HW_STATUS_SIZE,
7586 &tnapi->status_mapping,
7587 GFP_KERNEL);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007588 if (!tnapi->hw_status)
7589 goto err_out;
7590
7591 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00007592 sblk = tnapi->hw_status;
7593
Matt Carlson8fea32b2010-09-15 08:59:58 +00007594 if (tg3_rx_prodring_init(tp, &tnapi->prodring))
7595 goto err_out;
7596
Matt Carlson19cfaec2009-12-03 08:36:20 +00007597 /* If multivector TSS is enabled, vector 0 does not handle
7598 * tx interrupts. Don't allocate any resources for it.
7599 */
Joe Perches63c3a662011-04-26 08:12:10 +00007600 if ((!i && !tg3_flag(tp, ENABLE_TSS)) ||
7601 (i && tg3_flag(tp, ENABLE_TSS))) {
Matt Carlsondf8944c2011-07-27 14:20:46 +00007602 tnapi->tx_buffers = kzalloc(
7603 sizeof(struct tg3_tx_ring_info) *
7604 TG3_TX_RING_SIZE, GFP_KERNEL);
Matt Carlson19cfaec2009-12-03 08:36:20 +00007605 if (!tnapi->tx_buffers)
7606 goto err_out;
7607
Matt Carlson4bae65c2010-11-24 08:31:52 +00007608 tnapi->tx_ring = dma_alloc_coherent(&tp->pdev->dev,
7609 TG3_TX_RING_BYTES,
7610 &tnapi->tx_desc_mapping,
7611 GFP_KERNEL);
Matt Carlson19cfaec2009-12-03 08:36:20 +00007612 if (!tnapi->tx_ring)
7613 goto err_out;
7614 }
7615
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00007616 /*
7617 * When RSS is enabled, the status block format changes
7618 * slightly. The "rx_jumbo_consumer", "reserved",
7619 * and "rx_mini_consumer" members get mapped to the
7620 * other three rx return ring producer indexes.
7621 */
7622 switch (i) {
7623 default:
7624 tnapi->rx_rcb_prod_idx = &sblk->idx[0].rx_producer;
7625 break;
7626 case 2:
7627 tnapi->rx_rcb_prod_idx = &sblk->rx_jumbo_consumer;
7628 break;
7629 case 3:
7630 tnapi->rx_rcb_prod_idx = &sblk->reserved;
7631 break;
7632 case 4:
7633 tnapi->rx_rcb_prod_idx = &sblk->rx_mini_consumer;
7634 break;
7635 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007636
Matt Carlson0c1d0e22009-09-01 13:16:33 +00007637 /*
7638 * If multivector RSS is enabled, vector 0 does not handle
7639 * rx or tx interrupts. Don't allocate any resources for it.
7640 */
Joe Perches63c3a662011-04-26 08:12:10 +00007641 if (!i && tg3_flag(tp, ENABLE_RSS))
Matt Carlson0c1d0e22009-09-01 13:16:33 +00007642 continue;
7643
Matt Carlson4bae65c2010-11-24 08:31:52 +00007644 tnapi->rx_rcb = dma_alloc_coherent(&tp->pdev->dev,
7645 TG3_RX_RCB_RING_BYTES(tp),
7646 &tnapi->rx_rcb_mapping,
7647 GFP_KERNEL);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007648 if (!tnapi->rx_rcb)
7649 goto err_out;
7650
7651 memset(tnapi->rx_rcb, 0, TG3_RX_RCB_RING_BYTES(tp));
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007652 }
7653
Linus Torvalds1da177e2005-04-16 15:20:36 -07007654 return 0;
7655
7656err_out:
7657 tg3_free_consistent(tp);
7658 return -ENOMEM;
7659}
7660
7661#define MAX_WAIT_CNT 1000
7662
7663/* To stop a block, clear the enable bit and poll till it
7664 * clears. tp->lock is held.
7665 */
David S. Millerb3b7d6b2005-05-05 14:40:20 -07007666static int tg3_stop_block(struct tg3 *tp, unsigned long ofs, u32 enable_bit, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007667{
7668 unsigned int i;
7669 u32 val;
7670
Joe Perches63c3a662011-04-26 08:12:10 +00007671 if (tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007672 switch (ofs) {
7673 case RCVLSC_MODE:
7674 case DMAC_MODE:
7675 case MBFREE_MODE:
7676 case BUFMGR_MODE:
7677 case MEMARB_MODE:
7678 /* We can't enable/disable these bits of the
7679 * 5705/5750, just say success.
7680 */
7681 return 0;
7682
7683 default:
7684 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07007685 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007686 }
7687
7688 val = tr32(ofs);
7689 val &= ~enable_bit;
7690 tw32_f(ofs, val);
7691
7692 for (i = 0; i < MAX_WAIT_CNT; i++) {
7693 udelay(100);
7694 val = tr32(ofs);
7695 if ((val & enable_bit) == 0)
7696 break;
7697 }
7698
David S. Millerb3b7d6b2005-05-05 14:40:20 -07007699 if (i == MAX_WAIT_CNT && !silent) {
Matt Carlson2445e462010-04-05 10:19:21 +00007700 dev_err(&tp->pdev->dev,
7701 "tg3_stop_block timed out, ofs=%lx enable_bit=%x\n",
7702 ofs, enable_bit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007703 return -ENODEV;
7704 }
7705
7706 return 0;
7707}
7708
7709/* tp->lock is held. */
David S. Millerb3b7d6b2005-05-05 14:40:20 -07007710static int tg3_abort_hw(struct tg3 *tp, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007711{
7712 int i, err;
7713
7714 tg3_disable_ints(tp);
7715
7716 tp->rx_mode &= ~RX_MODE_ENABLE;
7717 tw32_f(MAC_RX_MODE, tp->rx_mode);
7718 udelay(10);
7719
David S. Millerb3b7d6b2005-05-05 14:40:20 -07007720 err = tg3_stop_block(tp, RCVBDI_MODE, RCVBDI_MODE_ENABLE, silent);
7721 err |= tg3_stop_block(tp, RCVLPC_MODE, RCVLPC_MODE_ENABLE, silent);
7722 err |= tg3_stop_block(tp, RCVLSC_MODE, RCVLSC_MODE_ENABLE, silent);
7723 err |= tg3_stop_block(tp, RCVDBDI_MODE, RCVDBDI_MODE_ENABLE, silent);
7724 err |= tg3_stop_block(tp, RCVDCC_MODE, RCVDCC_MODE_ENABLE, silent);
7725 err |= tg3_stop_block(tp, RCVCC_MODE, RCVCC_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007726
David S. Millerb3b7d6b2005-05-05 14:40:20 -07007727 err |= tg3_stop_block(tp, SNDBDS_MODE, SNDBDS_MODE_ENABLE, silent);
7728 err |= tg3_stop_block(tp, SNDBDI_MODE, SNDBDI_MODE_ENABLE, silent);
7729 err |= tg3_stop_block(tp, SNDDATAI_MODE, SNDDATAI_MODE_ENABLE, silent);
7730 err |= tg3_stop_block(tp, RDMAC_MODE, RDMAC_MODE_ENABLE, silent);
7731 err |= tg3_stop_block(tp, SNDDATAC_MODE, SNDDATAC_MODE_ENABLE, silent);
7732 err |= tg3_stop_block(tp, DMAC_MODE, DMAC_MODE_ENABLE, silent);
7733 err |= tg3_stop_block(tp, SNDBDC_MODE, SNDBDC_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007734
7735 tp->mac_mode &= ~MAC_MODE_TDE_ENABLE;
7736 tw32_f(MAC_MODE, tp->mac_mode);
7737 udelay(40);
7738
7739 tp->tx_mode &= ~TX_MODE_ENABLE;
7740 tw32_f(MAC_TX_MODE, tp->tx_mode);
7741
7742 for (i = 0; i < MAX_WAIT_CNT; i++) {
7743 udelay(100);
7744 if (!(tr32(MAC_TX_MODE) & TX_MODE_ENABLE))
7745 break;
7746 }
7747 if (i >= MAX_WAIT_CNT) {
Matt Carlsonab96b242010-04-05 10:19:22 +00007748 dev_err(&tp->pdev->dev,
7749 "%s timed out, TX_MODE_ENABLE will not clear "
7750 "MAC_TX_MODE=%08x\n", __func__, tr32(MAC_TX_MODE));
Michael Chane6de8ad2005-05-05 14:42:41 -07007751 err |= -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007752 }
7753
Michael Chane6de8ad2005-05-05 14:42:41 -07007754 err |= tg3_stop_block(tp, HOSTCC_MODE, HOSTCC_MODE_ENABLE, silent);
David S. Millerb3b7d6b2005-05-05 14:40:20 -07007755 err |= tg3_stop_block(tp, WDMAC_MODE, WDMAC_MODE_ENABLE, silent);
7756 err |= tg3_stop_block(tp, MBFREE_MODE, MBFREE_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007757
7758 tw32(FTQ_RESET, 0xffffffff);
7759 tw32(FTQ_RESET, 0x00000000);
7760
David S. Millerb3b7d6b2005-05-05 14:40:20 -07007761 err |= tg3_stop_block(tp, BUFMGR_MODE, BUFMGR_MODE_ENABLE, silent);
7762 err |= tg3_stop_block(tp, MEMARB_MODE, MEMARB_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007763
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007764 for (i = 0; i < tp->irq_cnt; i++) {
7765 struct tg3_napi *tnapi = &tp->napi[i];
7766 if (tnapi->hw_status)
7767 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
7768 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007769
Linus Torvalds1da177e2005-04-16 15:20:36 -07007770 return err;
7771}
7772
Michael Chanee6a99b2007-07-18 21:49:10 -07007773/* Save PCI command register before chip reset */
7774static void tg3_save_pci_state(struct tg3 *tp)
7775{
Matt Carlson8a6eac92007-10-21 16:17:55 -07007776 pci_read_config_word(tp->pdev, PCI_COMMAND, &tp->pci_cmd);
Michael Chanee6a99b2007-07-18 21:49:10 -07007777}
7778
7779/* Restore PCI state after chip reset */
7780static void tg3_restore_pci_state(struct tg3 *tp)
7781{
7782 u32 val;
7783
7784 /* Re-enable indirect register accesses. */
7785 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
7786 tp->misc_host_ctrl);
7787
7788 /* Set MAX PCI retry to zero. */
7789 val = (PCISTATE_ROM_ENABLE | PCISTATE_ROM_RETRY_ENABLE);
7790 if (tp->pci_chip_rev_id == CHIPREV_ID_5704_A0 &&
Joe Perches63c3a662011-04-26 08:12:10 +00007791 tg3_flag(tp, PCIX_MODE))
Michael Chanee6a99b2007-07-18 21:49:10 -07007792 val |= PCISTATE_RETRY_SAME_DMA;
Matt Carlson0d3031d2007-10-10 18:02:43 -07007793 /* Allow reads and writes to the APE register and memory space. */
Joe Perches63c3a662011-04-26 08:12:10 +00007794 if (tg3_flag(tp, ENABLE_APE))
Matt Carlson0d3031d2007-10-10 18:02:43 -07007795 val |= PCISTATE_ALLOW_APE_CTLSPC_WR |
Matt Carlsonf92d9dc12010-06-05 17:24:30 +00007796 PCISTATE_ALLOW_APE_SHMEM_WR |
7797 PCISTATE_ALLOW_APE_PSPACE_WR;
Michael Chanee6a99b2007-07-18 21:49:10 -07007798 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE, val);
7799
Matt Carlson8a6eac92007-10-21 16:17:55 -07007800 pci_write_config_word(tp->pdev, PCI_COMMAND, tp->pci_cmd);
Michael Chanee6a99b2007-07-18 21:49:10 -07007801
Matt Carlson2c55a3d2011-11-28 09:41:04 +00007802 if (!tg3_flag(tp, PCI_EXPRESS)) {
7803 pci_write_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE,
7804 tp->pci_cacheline_sz);
7805 pci_write_config_byte(tp->pdev, PCI_LATENCY_TIMER,
7806 tp->pci_lat_timer);
Michael Chan114342f2007-10-15 02:12:26 -07007807 }
Matt Carlson5f5c51e2007-11-12 21:19:37 -08007808
Michael Chanee6a99b2007-07-18 21:49:10 -07007809 /* Make sure PCI-X relaxed ordering bit is clear. */
Joe Perches63c3a662011-04-26 08:12:10 +00007810 if (tg3_flag(tp, PCIX_MODE)) {
Matt Carlson9974a352007-10-07 23:27:28 -07007811 u16 pcix_cmd;
7812
7813 pci_read_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
7814 &pcix_cmd);
7815 pcix_cmd &= ~PCI_X_CMD_ERO;
7816 pci_write_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
7817 pcix_cmd);
7818 }
Michael Chanee6a99b2007-07-18 21:49:10 -07007819
Joe Perches63c3a662011-04-26 08:12:10 +00007820 if (tg3_flag(tp, 5780_CLASS)) {
Michael Chanee6a99b2007-07-18 21:49:10 -07007821
7822 /* Chip reset on 5780 will reset MSI enable bit,
7823 * so need to restore it.
7824 */
Joe Perches63c3a662011-04-26 08:12:10 +00007825 if (tg3_flag(tp, USING_MSI)) {
Michael Chanee6a99b2007-07-18 21:49:10 -07007826 u16 ctrl;
7827
7828 pci_read_config_word(tp->pdev,
7829 tp->msi_cap + PCI_MSI_FLAGS,
7830 &ctrl);
7831 pci_write_config_word(tp->pdev,
7832 tp->msi_cap + PCI_MSI_FLAGS,
7833 ctrl | PCI_MSI_FLAGS_ENABLE);
7834 val = tr32(MSGINT_MODE);
7835 tw32(MSGINT_MODE, val | MSGINT_MODE_ENABLE);
7836 }
7837 }
7838}
7839
Linus Torvalds1da177e2005-04-16 15:20:36 -07007840/* tp->lock is held. */
7841static int tg3_chip_reset(struct tg3 *tp)
7842{
7843 u32 val;
Michael Chan1ee582d2005-08-09 20:16:46 -07007844 void (*write_op)(struct tg3 *, u32, u32);
Matt Carlson4f125f42009-09-01 12:55:02 +00007845 int i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007846
David S. Millerf49639e2006-06-09 11:58:36 -07007847 tg3_nvram_lock(tp);
7848
Matt Carlson77b483f2008-08-15 14:07:24 -07007849 tg3_ape_lock(tp, TG3_APE_LOCK_GRC);
7850
David S. Millerf49639e2006-06-09 11:58:36 -07007851 /* No matching tg3_nvram_unlock() after this because
7852 * chip reset below will undo the nvram lock.
7853 */
7854 tp->nvram_lock_cnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007855
Michael Chanee6a99b2007-07-18 21:49:10 -07007856 /* GRC_MISC_CFG core clock reset will clear the memory
7857 * enable bit in PCI register 4 and the MSI enable bit
7858 * on some chips, so we save relevant registers here.
7859 */
7860 tg3_save_pci_state(tp);
7861
Michael Chand9ab5ad2006-03-20 22:27:35 -08007862 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752 ||
Joe Perches63c3a662011-04-26 08:12:10 +00007863 tg3_flag(tp, 5755_PLUS))
Michael Chand9ab5ad2006-03-20 22:27:35 -08007864 tw32(GRC_FASTBOOT_PC, 0);
7865
Linus Torvalds1da177e2005-04-16 15:20:36 -07007866 /*
7867 * We must avoid the readl() that normally takes place.
7868 * It locks machines, causes machine checks, and other
7869 * fun things. So, temporarily disable the 5701
7870 * hardware workaround, while we do the reset.
7871 */
Michael Chan1ee582d2005-08-09 20:16:46 -07007872 write_op = tp->write32;
7873 if (write_op == tg3_write_flush_reg32)
7874 tp->write32 = tg3_write32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007875
Michael Chand18edcb2007-03-24 20:57:11 -07007876 /* Prevent the irq handler from reading or writing PCI registers
7877 * during chip reset when the memory enable bit in the PCI command
7878 * register may be cleared. The chip does not generate interrupt
7879 * at this time, but the irq handler may still be called due to irq
7880 * sharing or irqpoll.
7881 */
Joe Perches63c3a662011-04-26 08:12:10 +00007882 tg3_flag_set(tp, CHIP_RESETTING);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007883 for (i = 0; i < tp->irq_cnt; i++) {
7884 struct tg3_napi *tnapi = &tp->napi[i];
7885 if (tnapi->hw_status) {
7886 tnapi->hw_status->status = 0;
7887 tnapi->hw_status->status_tag = 0;
7888 }
7889 tnapi->last_tag = 0;
7890 tnapi->last_irq_tag = 0;
Michael Chanb8fa2f32007-04-06 17:35:37 -07007891 }
Michael Chand18edcb2007-03-24 20:57:11 -07007892 smp_mb();
Matt Carlson4f125f42009-09-01 12:55:02 +00007893
7894 for (i = 0; i < tp->irq_cnt; i++)
7895 synchronize_irq(tp->napi[i].irq_vec);
Michael Chand18edcb2007-03-24 20:57:11 -07007896
Matt Carlson255ca312009-08-25 10:07:27 +00007897 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780) {
7898 val = tr32(TG3_PCIE_LNKCTL) & ~TG3_PCIE_LNKCTL_L1_PLL_PD_EN;
7899 tw32(TG3_PCIE_LNKCTL, val | TG3_PCIE_LNKCTL_L1_PLL_PD_DIS);
7900 }
7901
Linus Torvalds1da177e2005-04-16 15:20:36 -07007902 /* do the reset */
7903 val = GRC_MISC_CFG_CORECLK_RESET;
7904
Joe Perches63c3a662011-04-26 08:12:10 +00007905 if (tg3_flag(tp, PCI_EXPRESS)) {
Matt Carlson88075d92010-08-02 11:25:58 +00007906 /* Force PCIe 1.0a mode */
7907 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
Joe Perches63c3a662011-04-26 08:12:10 +00007908 !tg3_flag(tp, 57765_PLUS) &&
Matt Carlson88075d92010-08-02 11:25:58 +00007909 tr32(TG3_PCIE_PHY_TSTCTL) ==
7910 (TG3_PCIE_PHY_TSTCTL_PCIE10 | TG3_PCIE_PHY_TSTCTL_PSCRAM))
7911 tw32(TG3_PCIE_PHY_TSTCTL, TG3_PCIE_PHY_TSTCTL_PSCRAM);
7912
Linus Torvalds1da177e2005-04-16 15:20:36 -07007913 if (tp->pci_chip_rev_id != CHIPREV_ID_5750_A0) {
7914 tw32(GRC_MISC_CFG, (1 << 29));
7915 val |= (1 << 29);
7916 }
7917 }
7918
Michael Chanb5d37722006-09-27 16:06:21 -07007919 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
7920 tw32(VCPU_STATUS, tr32(VCPU_STATUS) | VCPU_STATUS_DRV_RESET);
7921 tw32(GRC_VCPU_EXT_CTRL,
7922 tr32(GRC_VCPU_EXT_CTRL) & ~GRC_VCPU_EXT_CTRL_HALT_CPU);
7923 }
7924
Matt Carlsonf37500d2010-08-02 11:25:59 +00007925 /* Manage gphy power for all CPMU absent PCIe devices. */
Joe Perches63c3a662011-04-26 08:12:10 +00007926 if (tg3_flag(tp, 5705_PLUS) && !tg3_flag(tp, CPMU_PRESENT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007927 val |= GRC_MISC_CFG_KEEP_GPHY_POWER;
Matt Carlsonf37500d2010-08-02 11:25:59 +00007928
Linus Torvalds1da177e2005-04-16 15:20:36 -07007929 tw32(GRC_MISC_CFG, val);
7930
Michael Chan1ee582d2005-08-09 20:16:46 -07007931 /* restore 5701 hardware bug workaround write method */
7932 tp->write32 = write_op;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007933
7934 /* Unfortunately, we have to delay before the PCI read back.
7935 * Some 575X chips even will not respond to a PCI cfg access
7936 * when the reset command is given to the chip.
7937 *
7938 * How do these hardware designers expect things to work
7939 * properly if the PCI write is posted for a long period
7940 * of time? It is always necessary to have some method by
7941 * which a register read back can occur to push the write
7942 * out which does the reset.
7943 *
7944 * For most tg3 variants the trick below was working.
7945 * Ho hum...
7946 */
7947 udelay(120);
7948
7949 /* Flush PCI posted writes. The normal MMIO registers
7950 * are inaccessible at this time so this is the only
7951 * way to make this reliably (actually, this is no longer
7952 * the case, see above). I tried to use indirect
7953 * register read/write but this upset some 5701 variants.
7954 */
7955 pci_read_config_dword(tp->pdev, PCI_COMMAND, &val);
7956
7957 udelay(120);
7958
Jon Mason708ebb32011-06-27 12:56:50 +00007959 if (tg3_flag(tp, PCI_EXPRESS) && pci_pcie_cap(tp->pdev)) {
Matt Carlsone7126992009-08-25 10:08:16 +00007960 u16 val16;
7961
Linus Torvalds1da177e2005-04-16 15:20:36 -07007962 if (tp->pci_chip_rev_id == CHIPREV_ID_5750_A0) {
7963 int i;
7964 u32 cfg_val;
7965
7966 /* Wait for link training to complete. */
7967 for (i = 0; i < 5000; i++)
7968 udelay(100);
7969
7970 pci_read_config_dword(tp->pdev, 0xc4, &cfg_val);
7971 pci_write_config_dword(tp->pdev, 0xc4,
7972 cfg_val | (1 << 15));
7973 }
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007974
Matt Carlsone7126992009-08-25 10:08:16 +00007975 /* Clear the "no snoop" and "relaxed ordering" bits. */
7976 pci_read_config_word(tp->pdev,
Jon Mason708ebb32011-06-27 12:56:50 +00007977 pci_pcie_cap(tp->pdev) + PCI_EXP_DEVCTL,
Matt Carlsone7126992009-08-25 10:08:16 +00007978 &val16);
7979 val16 &= ~(PCI_EXP_DEVCTL_RELAX_EN |
7980 PCI_EXP_DEVCTL_NOSNOOP_EN);
7981 /*
7982 * Older PCIe devices only support the 128 byte
7983 * MPS setting. Enforce the restriction.
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007984 */
Joe Perches63c3a662011-04-26 08:12:10 +00007985 if (!tg3_flag(tp, CPMU_PRESENT))
Matt Carlsone7126992009-08-25 10:08:16 +00007986 val16 &= ~PCI_EXP_DEVCTL_PAYLOAD;
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007987 pci_write_config_word(tp->pdev,
Jon Mason708ebb32011-06-27 12:56:50 +00007988 pci_pcie_cap(tp->pdev) + PCI_EXP_DEVCTL,
Matt Carlsone7126992009-08-25 10:08:16 +00007989 val16);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007990
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007991 /* Clear error status */
7992 pci_write_config_word(tp->pdev,
Jon Mason708ebb32011-06-27 12:56:50 +00007993 pci_pcie_cap(tp->pdev) + PCI_EXP_DEVSTA,
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007994 PCI_EXP_DEVSTA_CED |
7995 PCI_EXP_DEVSTA_NFED |
7996 PCI_EXP_DEVSTA_FED |
7997 PCI_EXP_DEVSTA_URD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007998 }
7999
Michael Chanee6a99b2007-07-18 21:49:10 -07008000 tg3_restore_pci_state(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008001
Joe Perches63c3a662011-04-26 08:12:10 +00008002 tg3_flag_clear(tp, CHIP_RESETTING);
8003 tg3_flag_clear(tp, ERROR_PROCESSED);
Michael Chand18edcb2007-03-24 20:57:11 -07008004
Michael Chanee6a99b2007-07-18 21:49:10 -07008005 val = 0;
Joe Perches63c3a662011-04-26 08:12:10 +00008006 if (tg3_flag(tp, 5780_CLASS))
Michael Chan4cf78e42005-07-25 12:29:19 -07008007 val = tr32(MEMARB_MODE);
Michael Chanee6a99b2007-07-18 21:49:10 -07008008 tw32(MEMARB_MODE, val | MEMARB_MODE_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008009
8010 if (tp->pci_chip_rev_id == CHIPREV_ID_5750_A3) {
8011 tg3_stop_fw(tp);
8012 tw32(0x5000, 0x400);
8013 }
8014
8015 tw32(GRC_MODE, tp->grc_mode);
8016
8017 if (tp->pci_chip_rev_id == CHIPREV_ID_5705_A0) {
Andy Gospodarekab0049b2007-09-06 20:42:14 +01008018 val = tr32(0xc4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008019
8020 tw32(0xc4, val | (1 << 15));
8021 }
8022
8023 if ((tp->nic_sram_data_cfg & NIC_SRAM_DATA_CFG_MINI_PCI) != 0 &&
8024 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
8025 tp->pci_clock_ctrl |= CLOCK_CTRL_CLKRUN_OENABLE;
8026 if (tp->pci_chip_rev_id == CHIPREV_ID_5705_A0)
8027 tp->pci_clock_ctrl |= CLOCK_CTRL_FORCE_CLKRUN;
8028 tw32(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl);
8029 }
8030
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008031 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Matt Carlson9e975cc2011-07-20 10:20:50 +00008032 tp->mac_mode = MAC_MODE_PORT_MODE_TBI;
Matt Carlsond2394e6b2010-11-24 08:31:47 +00008033 val = tp->mac_mode;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008034 } else if (tp->phy_flags & TG3_PHYFLG_MII_SERDES) {
Matt Carlson9e975cc2011-07-20 10:20:50 +00008035 tp->mac_mode = MAC_MODE_PORT_MODE_GMII;
Matt Carlsond2394e6b2010-11-24 08:31:47 +00008036 val = tp->mac_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008037 } else
Matt Carlsond2394e6b2010-11-24 08:31:47 +00008038 val = 0;
8039
8040 tw32_f(MAC_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008041 udelay(40);
8042
Matt Carlson77b483f2008-08-15 14:07:24 -07008043 tg3_ape_unlock(tp, TG3_APE_LOCK_GRC);
8044
Michael Chan7a6f4362006-09-27 16:03:31 -07008045 err = tg3_poll_fw(tp);
8046 if (err)
8047 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008048
Matt Carlson0a9140c2009-08-28 12:27:50 +00008049 tg3_mdio_start(tp);
8050
Joe Perches63c3a662011-04-26 08:12:10 +00008051 if (tg3_flag(tp, PCI_EXPRESS) &&
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00008052 tp->pci_chip_rev_id != CHIPREV_ID_5750_A0 &&
8053 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
Joe Perches63c3a662011-04-26 08:12:10 +00008054 !tg3_flag(tp, 57765_PLUS)) {
Andy Gospodarekab0049b2007-09-06 20:42:14 +01008055 val = tr32(0x7c00);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008056
8057 tw32(0x7c00, val | (1 << 25));
8058 }
8059
Matt Carlsond78b59f2011-04-05 14:22:46 +00008060 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
8061 val = tr32(TG3_CPMU_CLCK_ORIDE);
8062 tw32(TG3_CPMU_CLCK_ORIDE, val & ~CPMU_CLCK_ORIDE_MAC_ORIDE_EN);
8063 }
8064
Linus Torvalds1da177e2005-04-16 15:20:36 -07008065 /* Reprobe ASF enable state. */
Joe Perches63c3a662011-04-26 08:12:10 +00008066 tg3_flag_clear(tp, ENABLE_ASF);
8067 tg3_flag_clear(tp, ASF_NEW_HANDSHAKE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008068 tg3_read_mem(tp, NIC_SRAM_DATA_SIG, &val);
8069 if (val == NIC_SRAM_DATA_SIG_MAGIC) {
8070 u32 nic_cfg;
8071
8072 tg3_read_mem(tp, NIC_SRAM_DATA_CFG, &nic_cfg);
8073 if (nic_cfg & NIC_SRAM_DATA_CFG_ASF_ENABLE) {
Joe Perches63c3a662011-04-26 08:12:10 +00008074 tg3_flag_set(tp, ENABLE_ASF);
Matt Carlson4ba526c2008-08-15 14:10:04 -07008075 tp->last_event_jiffies = jiffies;
Joe Perches63c3a662011-04-26 08:12:10 +00008076 if (tg3_flag(tp, 5750_PLUS))
8077 tg3_flag_set(tp, ASF_NEW_HANDSHAKE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008078 }
8079 }
8080
8081 return 0;
8082}
8083
Matt Carlson65ec6982012-02-28 23:33:37 +00008084static void tg3_get_nstats(struct tg3 *, struct rtnl_link_stats64 *);
8085static void tg3_get_estats(struct tg3 *, struct tg3_ethtool_stats *);
Matt Carlson92feeab2011-12-08 14:40:14 +00008086
Linus Torvalds1da177e2005-04-16 15:20:36 -07008087/* tp->lock is held. */
Michael Chan944d9802005-05-29 14:57:48 -07008088static int tg3_halt(struct tg3 *tp, int kind, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008089{
8090 int err;
8091
8092 tg3_stop_fw(tp);
8093
Michael Chan944d9802005-05-29 14:57:48 -07008094 tg3_write_sig_pre_reset(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008095
David S. Millerb3b7d6b2005-05-05 14:40:20 -07008096 tg3_abort_hw(tp, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008097 err = tg3_chip_reset(tp);
8098
Matt Carlsondaba2a62009-04-20 06:58:52 +00008099 __tg3_set_mac_addr(tp, 0);
8100
Michael Chan944d9802005-05-29 14:57:48 -07008101 tg3_write_sig_legacy(tp, kind);
8102 tg3_write_sig_post_reset(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008103
Matt Carlson92feeab2011-12-08 14:40:14 +00008104 if (tp->hw_stats) {
8105 /* Save the stats across chip resets... */
David S. Millerb4017c52012-03-01 17:57:40 -05008106 tg3_get_nstats(tp, &tp->net_stats_prev);
Matt Carlson92feeab2011-12-08 14:40:14 +00008107 tg3_get_estats(tp, &tp->estats_prev);
8108
8109 /* And make sure the next sample is new data */
8110 memset(tp->hw_stats, 0, sizeof(struct tg3_hw_stats));
8111 }
8112
Linus Torvalds1da177e2005-04-16 15:20:36 -07008113 if (err)
8114 return err;
8115
8116 return 0;
8117}
8118
Linus Torvalds1da177e2005-04-16 15:20:36 -07008119static int tg3_set_mac_addr(struct net_device *dev, void *p)
8120{
8121 struct tg3 *tp = netdev_priv(dev);
8122 struct sockaddr *addr = p;
Michael Chan986e0ae2007-05-05 12:10:20 -07008123 int err = 0, skip_mac_1 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008124
Michael Chanf9804dd2005-09-27 12:13:10 -07008125 if (!is_valid_ether_addr(addr->sa_data))
Danny Kukawka504f9b52012-02-21 02:07:49 +00008126 return -EADDRNOTAVAIL;
Michael Chanf9804dd2005-09-27 12:13:10 -07008127
Linus Torvalds1da177e2005-04-16 15:20:36 -07008128 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
8129
Michael Chane75f7c92006-03-20 21:33:26 -08008130 if (!netif_running(dev))
8131 return 0;
8132
Joe Perches63c3a662011-04-26 08:12:10 +00008133 if (tg3_flag(tp, ENABLE_ASF)) {
Michael Chan986e0ae2007-05-05 12:10:20 -07008134 u32 addr0_high, addr0_low, addr1_high, addr1_low;
Michael Chan58712ef2006-04-29 18:58:01 -07008135
Michael Chan986e0ae2007-05-05 12:10:20 -07008136 addr0_high = tr32(MAC_ADDR_0_HIGH);
8137 addr0_low = tr32(MAC_ADDR_0_LOW);
8138 addr1_high = tr32(MAC_ADDR_1_HIGH);
8139 addr1_low = tr32(MAC_ADDR_1_LOW);
8140
8141 /* Skip MAC addr 1 if ASF is using it. */
8142 if ((addr0_high != addr1_high || addr0_low != addr1_low) &&
8143 !(addr1_high == 0 && addr1_low == 0))
8144 skip_mac_1 = 1;
Michael Chan58712ef2006-04-29 18:58:01 -07008145 }
Michael Chan986e0ae2007-05-05 12:10:20 -07008146 spin_lock_bh(&tp->lock);
8147 __tg3_set_mac_addr(tp, skip_mac_1);
8148 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008149
Michael Chanb9ec6c12006-07-25 16:37:27 -07008150 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008151}
8152
8153/* tp->lock is held. */
8154static void tg3_set_bdinfo(struct tg3 *tp, u32 bdinfo_addr,
8155 dma_addr_t mapping, u32 maxlen_flags,
8156 u32 nic_addr)
8157{
8158 tg3_write_mem(tp,
8159 (bdinfo_addr + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH),
8160 ((u64) mapping >> 32));
8161 tg3_write_mem(tp,
8162 (bdinfo_addr + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW),
8163 ((u64) mapping & 0xffffffff));
8164 tg3_write_mem(tp,
8165 (bdinfo_addr + TG3_BDINFO_MAXLEN_FLAGS),
8166 maxlen_flags);
8167
Joe Perches63c3a662011-04-26 08:12:10 +00008168 if (!tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008169 tg3_write_mem(tp,
8170 (bdinfo_addr + TG3_BDINFO_NIC_ADDR),
8171 nic_addr);
8172}
8173
Michael Chand244c892005-07-05 14:42:33 -07008174static void __tg3_set_coalesce(struct tg3 *tp, struct ethtool_coalesce *ec)
David S. Miller15f98502005-05-18 22:49:26 -07008175{
Matt Carlsonb6080e12009-09-01 13:12:00 +00008176 int i;
8177
Joe Perches63c3a662011-04-26 08:12:10 +00008178 if (!tg3_flag(tp, ENABLE_TSS)) {
Matt Carlsonb6080e12009-09-01 13:12:00 +00008179 tw32(HOSTCC_TXCOL_TICKS, ec->tx_coalesce_usecs);
8180 tw32(HOSTCC_TXMAX_FRAMES, ec->tx_max_coalesced_frames);
8181 tw32(HOSTCC_TXCOAL_MAXF_INT, ec->tx_max_coalesced_frames_irq);
Matt Carlsonb6080e12009-09-01 13:12:00 +00008182 } else {
8183 tw32(HOSTCC_TXCOL_TICKS, 0);
8184 tw32(HOSTCC_TXMAX_FRAMES, 0);
8185 tw32(HOSTCC_TXCOAL_MAXF_INT, 0);
Matt Carlson19cfaec2009-12-03 08:36:20 +00008186 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00008187
Joe Perches63c3a662011-04-26 08:12:10 +00008188 if (!tg3_flag(tp, ENABLE_RSS)) {
Matt Carlson19cfaec2009-12-03 08:36:20 +00008189 tw32(HOSTCC_RXCOL_TICKS, ec->rx_coalesce_usecs);
8190 tw32(HOSTCC_RXMAX_FRAMES, ec->rx_max_coalesced_frames);
8191 tw32(HOSTCC_RXCOAL_MAXF_INT, ec->rx_max_coalesced_frames_irq);
8192 } else {
Matt Carlsonb6080e12009-09-01 13:12:00 +00008193 tw32(HOSTCC_RXCOL_TICKS, 0);
8194 tw32(HOSTCC_RXMAX_FRAMES, 0);
8195 tw32(HOSTCC_RXCOAL_MAXF_INT, 0);
David S. Miller15f98502005-05-18 22:49:26 -07008196 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00008197
Joe Perches63c3a662011-04-26 08:12:10 +00008198 if (!tg3_flag(tp, 5705_PLUS)) {
David S. Miller15f98502005-05-18 22:49:26 -07008199 u32 val = ec->stats_block_coalesce_usecs;
8200
Matt Carlsonb6080e12009-09-01 13:12:00 +00008201 tw32(HOSTCC_RXCOAL_TICK_INT, ec->rx_coalesce_usecs_irq);
8202 tw32(HOSTCC_TXCOAL_TICK_INT, ec->tx_coalesce_usecs_irq);
8203
David S. Miller15f98502005-05-18 22:49:26 -07008204 if (!netif_carrier_ok(tp->dev))
8205 val = 0;
8206
8207 tw32(HOSTCC_STAT_COAL_TICKS, val);
8208 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00008209
8210 for (i = 0; i < tp->irq_cnt - 1; i++) {
8211 u32 reg;
8212
8213 reg = HOSTCC_RXCOL_TICKS_VEC1 + i * 0x18;
8214 tw32(reg, ec->rx_coalesce_usecs);
Matt Carlsonb6080e12009-09-01 13:12:00 +00008215 reg = HOSTCC_RXMAX_FRAMES_VEC1 + i * 0x18;
8216 tw32(reg, ec->rx_max_coalesced_frames);
Matt Carlsonb6080e12009-09-01 13:12:00 +00008217 reg = HOSTCC_RXCOAL_MAXF_INT_VEC1 + i * 0x18;
8218 tw32(reg, ec->rx_max_coalesced_frames_irq);
Matt Carlson19cfaec2009-12-03 08:36:20 +00008219
Joe Perches63c3a662011-04-26 08:12:10 +00008220 if (tg3_flag(tp, ENABLE_TSS)) {
Matt Carlson19cfaec2009-12-03 08:36:20 +00008221 reg = HOSTCC_TXCOL_TICKS_VEC1 + i * 0x18;
8222 tw32(reg, ec->tx_coalesce_usecs);
8223 reg = HOSTCC_TXMAX_FRAMES_VEC1 + i * 0x18;
8224 tw32(reg, ec->tx_max_coalesced_frames);
8225 reg = HOSTCC_TXCOAL_MAXF_INT_VEC1 + i * 0x18;
8226 tw32(reg, ec->tx_max_coalesced_frames_irq);
8227 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00008228 }
8229
8230 for (; i < tp->irq_max - 1; i++) {
8231 tw32(HOSTCC_RXCOL_TICKS_VEC1 + i * 0x18, 0);
Matt Carlsonb6080e12009-09-01 13:12:00 +00008232 tw32(HOSTCC_RXMAX_FRAMES_VEC1 + i * 0x18, 0);
Matt Carlsonb6080e12009-09-01 13:12:00 +00008233 tw32(HOSTCC_RXCOAL_MAXF_INT_VEC1 + i * 0x18, 0);
Matt Carlson19cfaec2009-12-03 08:36:20 +00008234
Joe Perches63c3a662011-04-26 08:12:10 +00008235 if (tg3_flag(tp, ENABLE_TSS)) {
Matt Carlson19cfaec2009-12-03 08:36:20 +00008236 tw32(HOSTCC_TXCOL_TICKS_VEC1 + i * 0x18, 0);
8237 tw32(HOSTCC_TXMAX_FRAMES_VEC1 + i * 0x18, 0);
8238 tw32(HOSTCC_TXCOAL_MAXF_INT_VEC1 + i * 0x18, 0);
8239 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00008240 }
David S. Miller15f98502005-05-18 22:49:26 -07008241}
Linus Torvalds1da177e2005-04-16 15:20:36 -07008242
8243/* tp->lock is held. */
Matt Carlson2d31eca2009-09-01 12:53:31 +00008244static void tg3_rings_reset(struct tg3 *tp)
8245{
8246 int i;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008247 u32 stblk, txrcb, rxrcb, limit;
Matt Carlson2d31eca2009-09-01 12:53:31 +00008248 struct tg3_napi *tnapi = &tp->napi[0];
8249
8250 /* Disable all transmit rings but the first. */
Joe Perches63c3a662011-04-26 08:12:10 +00008251 if (!tg3_flag(tp, 5705_PLUS))
Matt Carlson2d31eca2009-09-01 12:53:31 +00008252 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 16;
Joe Perches63c3a662011-04-26 08:12:10 +00008253 else if (tg3_flag(tp, 5717_PLUS))
Matt Carlson3d377282010-10-14 10:37:39 +00008254 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 4;
Matt Carlson55086ad2011-12-14 11:09:59 +00008255 else if (tg3_flag(tp, 57765_CLASS))
Matt Carlsonb703df62009-12-03 08:36:21 +00008256 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 2;
Matt Carlson2d31eca2009-09-01 12:53:31 +00008257 else
8258 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE;
8259
8260 for (txrcb = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE;
8261 txrcb < limit; txrcb += TG3_BDINFO_SIZE)
8262 tg3_write_mem(tp, txrcb + TG3_BDINFO_MAXLEN_FLAGS,
8263 BDINFO_FLAGS_DISABLED);
8264
8265
8266 /* Disable all receive return rings but the first. */
Joe Perches63c3a662011-04-26 08:12:10 +00008267 if (tg3_flag(tp, 5717_PLUS))
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00008268 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 17;
Joe Perches63c3a662011-04-26 08:12:10 +00008269 else if (!tg3_flag(tp, 5705_PLUS))
Matt Carlson2d31eca2009-09-01 12:53:31 +00008270 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 16;
Matt Carlsonb703df62009-12-03 08:36:21 +00008271 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
Matt Carlson55086ad2011-12-14 11:09:59 +00008272 tg3_flag(tp, 57765_CLASS))
Matt Carlson2d31eca2009-09-01 12:53:31 +00008273 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 4;
8274 else
8275 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE;
8276
8277 for (rxrcb = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE;
8278 rxrcb < limit; rxrcb += TG3_BDINFO_SIZE)
8279 tg3_write_mem(tp, rxrcb + TG3_BDINFO_MAXLEN_FLAGS,
8280 BDINFO_FLAGS_DISABLED);
8281
8282 /* Disable interrupts */
8283 tw32_mailbox_f(tp->napi[0].int_mbox, 1);
Matt Carlson0e6cf6a2011-06-13 13:38:55 +00008284 tp->napi[0].chk_msi_cnt = 0;
8285 tp->napi[0].last_rx_cons = 0;
8286 tp->napi[0].last_tx_cons = 0;
Matt Carlson2d31eca2009-09-01 12:53:31 +00008287
8288 /* Zero mailbox registers. */
Joe Perches63c3a662011-04-26 08:12:10 +00008289 if (tg3_flag(tp, SUPPORT_MSIX)) {
Matt Carlson6fd45cb2010-09-15 08:59:57 +00008290 for (i = 1; i < tp->irq_max; i++) {
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008291 tp->napi[i].tx_prod = 0;
8292 tp->napi[i].tx_cons = 0;
Joe Perches63c3a662011-04-26 08:12:10 +00008293 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlsonc2353a32010-01-20 16:58:08 +00008294 tw32_mailbox(tp->napi[i].prodmbox, 0);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008295 tw32_rx_mbox(tp->napi[i].consmbox, 0);
8296 tw32_mailbox_f(tp->napi[i].int_mbox, 1);
Matt Carlson7f230732011-08-31 11:44:48 +00008297 tp->napi[i].chk_msi_cnt = 0;
Matt Carlson0e6cf6a2011-06-13 13:38:55 +00008298 tp->napi[i].last_rx_cons = 0;
8299 tp->napi[i].last_tx_cons = 0;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008300 }
Joe Perches63c3a662011-04-26 08:12:10 +00008301 if (!tg3_flag(tp, ENABLE_TSS))
Matt Carlsonc2353a32010-01-20 16:58:08 +00008302 tw32_mailbox(tp->napi[0].prodmbox, 0);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008303 } else {
8304 tp->napi[0].tx_prod = 0;
8305 tp->napi[0].tx_cons = 0;
8306 tw32_mailbox(tp->napi[0].prodmbox, 0);
8307 tw32_rx_mbox(tp->napi[0].consmbox, 0);
8308 }
Matt Carlson2d31eca2009-09-01 12:53:31 +00008309
8310 /* Make sure the NIC-based send BD rings are disabled. */
Joe Perches63c3a662011-04-26 08:12:10 +00008311 if (!tg3_flag(tp, 5705_PLUS)) {
Matt Carlson2d31eca2009-09-01 12:53:31 +00008312 u32 mbox = MAILBOX_SNDNIC_PROD_IDX_0 + TG3_64BIT_REG_LOW;
8313 for (i = 0; i < 16; i++)
8314 tw32_tx_mbox(mbox + i * 8, 0);
8315 }
8316
8317 txrcb = NIC_SRAM_SEND_RCB;
8318 rxrcb = NIC_SRAM_RCV_RET_RCB;
8319
8320 /* Clear status block in ram. */
8321 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
8322
8323 /* Set status block DMA address */
8324 tw32(HOSTCC_STATUS_BLK_HOST_ADDR + TG3_64BIT_REG_HIGH,
8325 ((u64) tnapi->status_mapping >> 32));
8326 tw32(HOSTCC_STATUS_BLK_HOST_ADDR + TG3_64BIT_REG_LOW,
8327 ((u64) tnapi->status_mapping & 0xffffffff));
8328
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008329 if (tnapi->tx_ring) {
8330 tg3_set_bdinfo(tp, txrcb, tnapi->tx_desc_mapping,
8331 (TG3_TX_RING_SIZE <<
8332 BDINFO_FLAGS_MAXLEN_SHIFT),
8333 NIC_SRAM_TX_BUFFER_DESC);
8334 txrcb += TG3_BDINFO_SIZE;
8335 }
Matt Carlson2d31eca2009-09-01 12:53:31 +00008336
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008337 if (tnapi->rx_rcb) {
8338 tg3_set_bdinfo(tp, rxrcb, tnapi->rx_rcb_mapping,
Matt Carlson7cb32cf2010-09-30 10:34:36 +00008339 (tp->rx_ret_ring_mask + 1) <<
8340 BDINFO_FLAGS_MAXLEN_SHIFT, 0);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008341 rxrcb += TG3_BDINFO_SIZE;
8342 }
8343
8344 stblk = HOSTCC_STATBLCK_RING1;
8345
8346 for (i = 1, tnapi++; i < tp->irq_cnt; i++, tnapi++) {
8347 u64 mapping = (u64)tnapi->status_mapping;
8348 tw32(stblk + TG3_64BIT_REG_HIGH, mapping >> 32);
8349 tw32(stblk + TG3_64BIT_REG_LOW, mapping & 0xffffffff);
8350
8351 /* Clear status block in ram. */
8352 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
8353
Matt Carlson19cfaec2009-12-03 08:36:20 +00008354 if (tnapi->tx_ring) {
8355 tg3_set_bdinfo(tp, txrcb, tnapi->tx_desc_mapping,
8356 (TG3_TX_RING_SIZE <<
8357 BDINFO_FLAGS_MAXLEN_SHIFT),
8358 NIC_SRAM_TX_BUFFER_DESC);
8359 txrcb += TG3_BDINFO_SIZE;
8360 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008361
8362 tg3_set_bdinfo(tp, rxrcb, tnapi->rx_rcb_mapping,
Matt Carlson7cb32cf2010-09-30 10:34:36 +00008363 ((tp->rx_ret_ring_mask + 1) <<
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008364 BDINFO_FLAGS_MAXLEN_SHIFT), 0);
8365
8366 stblk += 8;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008367 rxrcb += TG3_BDINFO_SIZE;
8368 }
Matt Carlson2d31eca2009-09-01 12:53:31 +00008369}
8370
Matt Carlsoneb07a942011-04-20 07:57:36 +00008371static void tg3_setup_rxbd_thresholds(struct tg3 *tp)
8372{
8373 u32 val, bdcache_maxcnt, host_rep_thresh, nic_rep_thresh;
8374
Joe Perches63c3a662011-04-26 08:12:10 +00008375 if (!tg3_flag(tp, 5750_PLUS) ||
8376 tg3_flag(tp, 5780_CLASS) ||
Matt Carlsoneb07a942011-04-20 07:57:36 +00008377 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 ||
Matt Carlson513aa6e2011-11-21 15:01:18 +00008378 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752 ||
8379 tg3_flag(tp, 57765_PLUS))
Matt Carlsoneb07a942011-04-20 07:57:36 +00008380 bdcache_maxcnt = TG3_SRAM_RX_STD_BDCACHE_SIZE_5700;
8381 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
8382 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787)
8383 bdcache_maxcnt = TG3_SRAM_RX_STD_BDCACHE_SIZE_5755;
8384 else
8385 bdcache_maxcnt = TG3_SRAM_RX_STD_BDCACHE_SIZE_5906;
8386
8387 nic_rep_thresh = min(bdcache_maxcnt / 2, tp->rx_std_max_post);
8388 host_rep_thresh = max_t(u32, tp->rx_pending / 8, 1);
8389
8390 val = min(nic_rep_thresh, host_rep_thresh);
8391 tw32(RCVBDI_STD_THRESH, val);
8392
Joe Perches63c3a662011-04-26 08:12:10 +00008393 if (tg3_flag(tp, 57765_PLUS))
Matt Carlsoneb07a942011-04-20 07:57:36 +00008394 tw32(STD_REPLENISH_LWM, bdcache_maxcnt);
8395
Joe Perches63c3a662011-04-26 08:12:10 +00008396 if (!tg3_flag(tp, JUMBO_CAPABLE) || tg3_flag(tp, 5780_CLASS))
Matt Carlsoneb07a942011-04-20 07:57:36 +00008397 return;
8398
Matt Carlson513aa6e2011-11-21 15:01:18 +00008399 bdcache_maxcnt = TG3_SRAM_RX_JMB_BDCACHE_SIZE_5700;
Matt Carlsoneb07a942011-04-20 07:57:36 +00008400
8401 host_rep_thresh = max_t(u32, tp->rx_jumbo_pending / 8, 1);
8402
8403 val = min(bdcache_maxcnt / 2, host_rep_thresh);
8404 tw32(RCVBDI_JUMBO_THRESH, val);
8405
Joe Perches63c3a662011-04-26 08:12:10 +00008406 if (tg3_flag(tp, 57765_PLUS))
Matt Carlsoneb07a942011-04-20 07:57:36 +00008407 tw32(JMB_REPLENISH_LWM, bdcache_maxcnt);
8408}
8409
Matt Carlsonccd5ba92012-02-13 10:20:08 +00008410static inline u32 calc_crc(unsigned char *buf, int len)
8411{
8412 u32 reg;
8413 u32 tmp;
8414 int j, k;
8415
8416 reg = 0xffffffff;
8417
8418 for (j = 0; j < len; j++) {
8419 reg ^= buf[j];
8420
8421 for (k = 0; k < 8; k++) {
8422 tmp = reg & 0x01;
8423
8424 reg >>= 1;
8425
8426 if (tmp)
8427 reg ^= 0xedb88320;
8428 }
8429 }
8430
8431 return ~reg;
8432}
8433
8434static void tg3_set_multi(struct tg3 *tp, unsigned int accept_all)
8435{
8436 /* accept or reject all multicast frames */
8437 tw32(MAC_HASH_REG_0, accept_all ? 0xffffffff : 0);
8438 tw32(MAC_HASH_REG_1, accept_all ? 0xffffffff : 0);
8439 tw32(MAC_HASH_REG_2, accept_all ? 0xffffffff : 0);
8440 tw32(MAC_HASH_REG_3, accept_all ? 0xffffffff : 0);
8441}
8442
8443static void __tg3_set_rx_mode(struct net_device *dev)
8444{
8445 struct tg3 *tp = netdev_priv(dev);
8446 u32 rx_mode;
8447
8448 rx_mode = tp->rx_mode & ~(RX_MODE_PROMISC |
8449 RX_MODE_KEEP_VLAN_TAG);
8450
8451#if !defined(CONFIG_VLAN_8021Q) && !defined(CONFIG_VLAN_8021Q_MODULE)
8452 /* When ASF is in use, we always keep the RX_MODE_KEEP_VLAN_TAG
8453 * flag clear.
8454 */
8455 if (!tg3_flag(tp, ENABLE_ASF))
8456 rx_mode |= RX_MODE_KEEP_VLAN_TAG;
8457#endif
8458
8459 if (dev->flags & IFF_PROMISC) {
8460 /* Promiscuous mode. */
8461 rx_mode |= RX_MODE_PROMISC;
8462 } else if (dev->flags & IFF_ALLMULTI) {
8463 /* Accept all multicast. */
8464 tg3_set_multi(tp, 1);
8465 } else if (netdev_mc_empty(dev)) {
8466 /* Reject all multicast. */
8467 tg3_set_multi(tp, 0);
8468 } else {
8469 /* Accept one or more multicast(s). */
8470 struct netdev_hw_addr *ha;
8471 u32 mc_filter[4] = { 0, };
8472 u32 regidx;
8473 u32 bit;
8474 u32 crc;
8475
8476 netdev_for_each_mc_addr(ha, dev) {
8477 crc = calc_crc(ha->addr, ETH_ALEN);
8478 bit = ~crc & 0x7f;
8479 regidx = (bit & 0x60) >> 5;
8480 bit &= 0x1f;
8481 mc_filter[regidx] |= (1 << bit);
8482 }
8483
8484 tw32(MAC_HASH_REG_0, mc_filter[0]);
8485 tw32(MAC_HASH_REG_1, mc_filter[1]);
8486 tw32(MAC_HASH_REG_2, mc_filter[2]);
8487 tw32(MAC_HASH_REG_3, mc_filter[3]);
8488 }
8489
8490 if (rx_mode != tp->rx_mode) {
8491 tp->rx_mode = rx_mode;
8492 tw32_f(MAC_RX_MODE, rx_mode);
8493 udelay(10);
8494 }
8495}
8496
Matt Carlson90415472011-12-16 13:33:23 +00008497static void tg3_rss_init_dflt_indir_tbl(struct tg3 *tp)
8498{
8499 int i;
8500
8501 for (i = 0; i < TG3_RSS_INDIR_TBL_SIZE; i++)
8502 tp->rss_ind_tbl[i] =
8503 ethtool_rxfh_indir_default(i, tp->irq_cnt - 1);
8504}
8505
8506static void tg3_rss_check_indir_tbl(struct tg3 *tp)
Matt Carlsonbcebcc42011-12-14 11:10:01 +00008507{
8508 int i;
8509
8510 if (!tg3_flag(tp, SUPPORT_MSIX))
8511 return;
8512
Matt Carlson90415472011-12-16 13:33:23 +00008513 if (tp->irq_cnt <= 2) {
Matt Carlsonbcebcc42011-12-14 11:10:01 +00008514 memset(&tp->rss_ind_tbl[0], 0, sizeof(tp->rss_ind_tbl));
Matt Carlson90415472011-12-16 13:33:23 +00008515 return;
8516 }
8517
8518 /* Validate table against current IRQ count */
8519 for (i = 0; i < TG3_RSS_INDIR_TBL_SIZE; i++) {
8520 if (tp->rss_ind_tbl[i] >= tp->irq_cnt - 1)
8521 break;
8522 }
8523
8524 if (i != TG3_RSS_INDIR_TBL_SIZE)
8525 tg3_rss_init_dflt_indir_tbl(tp);
Matt Carlsonbcebcc42011-12-14 11:10:01 +00008526}
8527
Matt Carlson90415472011-12-16 13:33:23 +00008528static void tg3_rss_write_indir_tbl(struct tg3 *tp)
Matt Carlsonbcebcc42011-12-14 11:10:01 +00008529{
8530 int i = 0;
8531 u32 reg = MAC_RSS_INDIR_TBL_0;
8532
8533 while (i < TG3_RSS_INDIR_TBL_SIZE) {
8534 u32 val = tp->rss_ind_tbl[i];
8535 i++;
8536 for (; i % 8; i++) {
8537 val <<= 4;
8538 val |= tp->rss_ind_tbl[i];
8539 }
8540 tw32(reg, val);
8541 reg += 4;
8542 }
8543}
8544
Matt Carlson2d31eca2009-09-01 12:53:31 +00008545/* tp->lock is held. */
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07008546static int tg3_reset_hw(struct tg3 *tp, int reset_phy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008547{
8548 u32 val, rdmac_mode;
8549 int i, err, limit;
Matt Carlson8fea32b2010-09-15 08:59:58 +00008550 struct tg3_rx_prodring_set *tpr = &tp->napi[0].prodring;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008551
8552 tg3_disable_ints(tp);
8553
8554 tg3_stop_fw(tp);
8555
8556 tg3_write_sig_pre_reset(tp, RESET_KIND_INIT);
8557
Joe Perches63c3a662011-04-26 08:12:10 +00008558 if (tg3_flag(tp, INIT_COMPLETE))
Michael Chane6de8ad2005-05-05 14:42:41 -07008559 tg3_abort_hw(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008560
Matt Carlson699c0192010-12-06 08:28:51 +00008561 /* Enable MAC control of LPI */
8562 if (tp->phy_flags & TG3_PHYFLG_EEE_CAP) {
8563 tw32_f(TG3_CPMU_EEE_LNKIDL_CTRL,
8564 TG3_CPMU_EEE_LNKIDL_PCIE_NL0 |
8565 TG3_CPMU_EEE_LNKIDL_UART_IDL);
8566
8567 tw32_f(TG3_CPMU_EEE_CTRL,
8568 TG3_CPMU_EEE_CTRL_EXIT_20_1_US);
8569
Matt Carlsona386b902010-12-06 08:28:53 +00008570 val = TG3_CPMU_EEEMD_ERLY_L1_XIT_DET |
8571 TG3_CPMU_EEEMD_LPI_IN_TX |
8572 TG3_CPMU_EEEMD_LPI_IN_RX |
8573 TG3_CPMU_EEEMD_EEE_ENABLE;
8574
8575 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717)
8576 val |= TG3_CPMU_EEEMD_SND_IDX_DET_EN;
8577
Joe Perches63c3a662011-04-26 08:12:10 +00008578 if (tg3_flag(tp, ENABLE_APE))
Matt Carlsona386b902010-12-06 08:28:53 +00008579 val |= TG3_CPMU_EEEMD_APE_TX_DET_EN;
8580
8581 tw32_f(TG3_CPMU_EEE_MODE, val);
8582
8583 tw32_f(TG3_CPMU_EEE_DBTMR1,
8584 TG3_CPMU_DBTMR1_PCIEXIT_2047US |
8585 TG3_CPMU_DBTMR1_LNKIDLE_2047US);
8586
8587 tw32_f(TG3_CPMU_EEE_DBTMR2,
Matt Carlsond7f2ab22011-01-25 15:58:56 +00008588 TG3_CPMU_DBTMR2_APE_TX_2047US |
Matt Carlsona386b902010-12-06 08:28:53 +00008589 TG3_CPMU_DBTMR2_TXIDXEQ_2047US);
Matt Carlson699c0192010-12-06 08:28:51 +00008590 }
8591
Matt Carlson603f1172010-02-12 14:47:10 +00008592 if (reset_phy)
Michael Chand4d2c552006-03-20 17:47:20 -08008593 tg3_phy_reset(tp);
8594
Linus Torvalds1da177e2005-04-16 15:20:36 -07008595 err = tg3_chip_reset(tp);
8596 if (err)
8597 return err;
8598
8599 tg3_write_sig_legacy(tp, RESET_KIND_INIT);
8600
Matt Carlsonbcb37f62008-11-03 16:52:09 -08008601 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX) {
Matt Carlsond30cdd22007-10-07 23:28:35 -07008602 val = tr32(TG3_CPMU_CTRL);
8603 val &= ~(CPMU_CTRL_LINK_AWARE_MODE | CPMU_CTRL_LINK_IDLE_MODE);
8604 tw32(TG3_CPMU_CTRL, val);
Matt Carlson9acb9612007-11-12 21:10:06 -08008605
8606 val = tr32(TG3_CPMU_LSPD_10MB_CLK);
8607 val &= ~CPMU_LSPD_10MB_MACCLK_MASK;
8608 val |= CPMU_LSPD_10MB_MACCLK_6_25;
8609 tw32(TG3_CPMU_LSPD_10MB_CLK, val);
8610
8611 val = tr32(TG3_CPMU_LNK_AWARE_PWRMD);
8612 val &= ~CPMU_LNK_AWARE_MACCLK_MASK;
8613 val |= CPMU_LNK_AWARE_MACCLK_6_25;
8614 tw32(TG3_CPMU_LNK_AWARE_PWRMD, val);
8615
8616 val = tr32(TG3_CPMU_HST_ACC);
8617 val &= ~CPMU_HST_ACC_MACCLK_MASK;
8618 val |= CPMU_HST_ACC_MACCLK_6_25;
8619 tw32(TG3_CPMU_HST_ACC, val);
Matt Carlsond30cdd22007-10-07 23:28:35 -07008620 }
8621
Matt Carlson33466d92009-04-20 06:57:41 +00008622 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780) {
8623 val = tr32(PCIE_PWR_MGMT_THRESH) & ~PCIE_PWR_MGMT_L1_THRESH_MSK;
8624 val |= PCIE_PWR_MGMT_EXT_ASPM_TMR_EN |
8625 PCIE_PWR_MGMT_L1_THRESH_4MS;
8626 tw32(PCIE_PWR_MGMT_THRESH, val);
Matt Carlson521e6b92009-08-25 10:06:01 +00008627
8628 val = tr32(TG3_PCIE_EIDLE_DELAY) & ~TG3_PCIE_EIDLE_DELAY_MASK;
8629 tw32(TG3_PCIE_EIDLE_DELAY, val | TG3_PCIE_EIDLE_DELAY_13_CLKS);
8630
8631 tw32(TG3_CORR_ERR_STAT, TG3_CORR_ERR_STAT_CLEAR);
Matt Carlson33466d92009-04-20 06:57:41 +00008632
Matt Carlsonf40386c2009-11-02 14:24:02 +00008633 val = tr32(TG3_PCIE_LNKCTL) & ~TG3_PCIE_LNKCTL_L1_PLL_PD_EN;
8634 tw32(TG3_PCIE_LNKCTL, val | TG3_PCIE_LNKCTL_L1_PLL_PD_DIS);
Matt Carlson255ca312009-08-25 10:07:27 +00008635 }
8636
Joe Perches63c3a662011-04-26 08:12:10 +00008637 if (tg3_flag(tp, L1PLLPD_EN)) {
Matt Carlson614b0592010-01-20 16:58:02 +00008638 u32 grc_mode = tr32(GRC_MODE);
8639
8640 /* Access the lower 1K of PL PCIE block registers. */
8641 val = grc_mode & ~GRC_MODE_PCIE_PORT_MASK;
8642 tw32(GRC_MODE, val | GRC_MODE_PCIE_PL_SEL);
8643
8644 val = tr32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL1);
8645 tw32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL1,
8646 val | TG3_PCIE_PL_LO_PHYCTL1_L1PLLPD_EN);
8647
8648 tw32(GRC_MODE, grc_mode);
8649 }
8650
Matt Carlson55086ad2011-12-14 11:09:59 +00008651 if (tg3_flag(tp, 57765_CLASS)) {
Matt Carlson5093eed2010-11-24 08:31:45 +00008652 if (tp->pci_chip_rev_id == CHIPREV_ID_57765_A0) {
8653 u32 grc_mode = tr32(GRC_MODE);
Matt Carlsoncea46462010-04-12 06:58:24 +00008654
Matt Carlson5093eed2010-11-24 08:31:45 +00008655 /* Access the lower 1K of PL PCIE block registers. */
8656 val = grc_mode & ~GRC_MODE_PCIE_PORT_MASK;
8657 tw32(GRC_MODE, val | GRC_MODE_PCIE_PL_SEL);
Matt Carlsoncea46462010-04-12 06:58:24 +00008658
Matt Carlson5093eed2010-11-24 08:31:45 +00008659 val = tr32(TG3_PCIE_TLDLPL_PORT +
8660 TG3_PCIE_PL_LO_PHYCTL5);
8661 tw32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL5,
8662 val | TG3_PCIE_PL_LO_PHYCTL5_DIS_L2CLKREQ);
Matt Carlsoncea46462010-04-12 06:58:24 +00008663
Matt Carlson5093eed2010-11-24 08:31:45 +00008664 tw32(GRC_MODE, grc_mode);
8665 }
Matt Carlsona977dbe2010-04-12 06:58:26 +00008666
Matt Carlson1ff30a52011-05-19 12:12:46 +00008667 if (GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_57765_AX) {
8668 u32 grc_mode = tr32(GRC_MODE);
8669
8670 /* Access the lower 1K of DL PCIE block registers. */
8671 val = grc_mode & ~GRC_MODE_PCIE_PORT_MASK;
8672 tw32(GRC_MODE, val | GRC_MODE_PCIE_DL_SEL);
8673
8674 val = tr32(TG3_PCIE_TLDLPL_PORT +
8675 TG3_PCIE_DL_LO_FTSMAX);
8676 val &= ~TG3_PCIE_DL_LO_FTSMAX_MSK;
8677 tw32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_DL_LO_FTSMAX,
8678 val | TG3_PCIE_DL_LO_FTSMAX_VAL);
8679
8680 tw32(GRC_MODE, grc_mode);
8681 }
8682
Matt Carlsona977dbe2010-04-12 06:58:26 +00008683 val = tr32(TG3_CPMU_LSPD_10MB_CLK);
8684 val &= ~CPMU_LSPD_10MB_MACCLK_MASK;
8685 val |= CPMU_LSPD_10MB_MACCLK_6_25;
8686 tw32(TG3_CPMU_LSPD_10MB_CLK, val);
Matt Carlsoncea46462010-04-12 06:58:24 +00008687 }
8688
Linus Torvalds1da177e2005-04-16 15:20:36 -07008689 /* This works around an issue with Athlon chipsets on
8690 * B3 tigon3 silicon. This bit has no effect on any
8691 * other revision. But do not set this on PCI Express
Matt Carlson795d01c2007-10-07 23:28:17 -07008692 * chips and don't even touch the clocks if the CPMU is present.
Linus Torvalds1da177e2005-04-16 15:20:36 -07008693 */
Joe Perches63c3a662011-04-26 08:12:10 +00008694 if (!tg3_flag(tp, CPMU_PRESENT)) {
8695 if (!tg3_flag(tp, PCI_EXPRESS))
Matt Carlson795d01c2007-10-07 23:28:17 -07008696 tp->pci_clock_ctrl |= CLOCK_CTRL_DELAY_PCI_GRANT;
8697 tw32_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl);
8698 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008699
8700 if (tp->pci_chip_rev_id == CHIPREV_ID_5704_A0 &&
Joe Perches63c3a662011-04-26 08:12:10 +00008701 tg3_flag(tp, PCIX_MODE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008702 val = tr32(TG3PCI_PCISTATE);
8703 val |= PCISTATE_RETRY_SAME_DMA;
8704 tw32(TG3PCI_PCISTATE, val);
8705 }
8706
Joe Perches63c3a662011-04-26 08:12:10 +00008707 if (tg3_flag(tp, ENABLE_APE)) {
Matt Carlson0d3031d2007-10-10 18:02:43 -07008708 /* Allow reads and writes to the
8709 * APE register and memory space.
8710 */
8711 val = tr32(TG3PCI_PCISTATE);
8712 val |= PCISTATE_ALLOW_APE_CTLSPC_WR |
Matt Carlsonf92d9dc12010-06-05 17:24:30 +00008713 PCISTATE_ALLOW_APE_SHMEM_WR |
8714 PCISTATE_ALLOW_APE_PSPACE_WR;
Matt Carlson0d3031d2007-10-10 18:02:43 -07008715 tw32(TG3PCI_PCISTATE, val);
8716 }
8717
Linus Torvalds1da177e2005-04-16 15:20:36 -07008718 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5704_BX) {
8719 /* Enable some hw fixes. */
8720 val = tr32(TG3PCI_MSI_DATA);
8721 val |= (1 << 26) | (1 << 28) | (1 << 29);
8722 tw32(TG3PCI_MSI_DATA, val);
8723 }
8724
8725 /* Descriptor ring init may make accesses to the
8726 * NIC SRAM area to setup the TX descriptors, so we
8727 * can only do this after the hardware has been
8728 * successfully reset.
8729 */
Michael Chan32d8c572006-07-25 16:38:29 -07008730 err = tg3_init_rings(tp);
8731 if (err)
8732 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008733
Joe Perches63c3a662011-04-26 08:12:10 +00008734 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlsoncbf9ca62009-11-13 13:03:40 +00008735 val = tr32(TG3PCI_DMA_RW_CTRL) &
8736 ~DMA_RWCTRL_DIS_CACHE_ALIGNMENT;
Matt Carlson1a319022010-04-12 06:58:25 +00008737 if (tp->pci_chip_rev_id == CHIPREV_ID_57765_A0)
8738 val &= ~DMA_RWCTRL_CRDRDR_RDMA_MRRS_MSK;
Matt Carlson55086ad2011-12-14 11:09:59 +00008739 if (!tg3_flag(tp, 57765_CLASS) &&
Matt Carlson0aebff42011-04-25 12:42:45 +00008740 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717)
8741 val |= DMA_RWCTRL_TAGGED_STAT_WA;
Matt Carlsoncbf9ca62009-11-13 13:03:40 +00008742 tw32(TG3PCI_DMA_RW_CTRL, val | tp->dma_rwctrl);
8743 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5784 &&
8744 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5761) {
Matt Carlsond30cdd22007-10-07 23:28:35 -07008745 /* This value is determined during the probe time DMA
8746 * engine test, tg3_test_dma.
8747 */
8748 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
8749 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008750
8751 tp->grc_mode &= ~(GRC_MODE_HOST_SENDBDS |
8752 GRC_MODE_4X_NIC_SEND_RINGS |
8753 GRC_MODE_NO_TX_PHDR_CSUM |
8754 GRC_MODE_NO_RX_PHDR_CSUM);
8755 tp->grc_mode |= GRC_MODE_HOST_SENDBDS;
Michael Chand2d746f2006-04-06 21:45:39 -07008756
8757 /* Pseudo-header checksum is done by hardware logic and not
8758 * the offload processers, so make the chip do the pseudo-
8759 * header checksums on receive. For transmit it is more
8760 * convenient to do the pseudo-header checksum in software
8761 * as Linux does that on transmit for us in all cases.
8762 */
8763 tp->grc_mode |= GRC_MODE_NO_TX_PHDR_CSUM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008764
8765 tw32(GRC_MODE,
8766 tp->grc_mode |
8767 (GRC_MODE_IRQ_ON_MAC_ATTN | GRC_MODE_HOST_STACKUP));
8768
8769 /* Setup the timer prescalar register. Clock is always 66Mhz. */
8770 val = tr32(GRC_MISC_CFG);
8771 val &= ~0xff;
8772 val |= (65 << GRC_MISC_CFG_PRESCALAR_SHIFT);
8773 tw32(GRC_MISC_CFG, val);
8774
8775 /* Initialize MBUF/DESC pool. */
Joe Perches63c3a662011-04-26 08:12:10 +00008776 if (tg3_flag(tp, 5750_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008777 /* Do nothing. */
8778 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5705) {
8779 tw32(BUFMGR_MB_POOL_ADDR, NIC_SRAM_MBUF_POOL_BASE);
8780 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704)
8781 tw32(BUFMGR_MB_POOL_SIZE, NIC_SRAM_MBUF_POOL_SIZE64);
8782 else
8783 tw32(BUFMGR_MB_POOL_SIZE, NIC_SRAM_MBUF_POOL_SIZE96);
8784 tw32(BUFMGR_DMA_DESC_POOL_ADDR, NIC_SRAM_DMA_DESC_POOL_BASE);
8785 tw32(BUFMGR_DMA_DESC_POOL_SIZE, NIC_SRAM_DMA_DESC_POOL_SIZE);
Joe Perches63c3a662011-04-26 08:12:10 +00008786 } else if (tg3_flag(tp, TSO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008787 int fw_len;
8788
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08008789 fw_len = tp->fw_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008790 fw_len = (fw_len + (0x80 - 1)) & ~(0x80 - 1);
8791 tw32(BUFMGR_MB_POOL_ADDR,
8792 NIC_SRAM_MBUF_POOL_BASE5705 + fw_len);
8793 tw32(BUFMGR_MB_POOL_SIZE,
8794 NIC_SRAM_MBUF_POOL_SIZE5705 - fw_len - 0xa00);
8795 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008796
Michael Chan0f893dc2005-07-25 12:30:38 -07008797 if (tp->dev->mtu <= ETH_DATA_LEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008798 tw32(BUFMGR_MB_RDMA_LOW_WATER,
8799 tp->bufmgr_config.mbuf_read_dma_low_water);
8800 tw32(BUFMGR_MB_MACRX_LOW_WATER,
8801 tp->bufmgr_config.mbuf_mac_rx_low_water);
8802 tw32(BUFMGR_MB_HIGH_WATER,
8803 tp->bufmgr_config.mbuf_high_water);
8804 } else {
8805 tw32(BUFMGR_MB_RDMA_LOW_WATER,
8806 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo);
8807 tw32(BUFMGR_MB_MACRX_LOW_WATER,
8808 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo);
8809 tw32(BUFMGR_MB_HIGH_WATER,
8810 tp->bufmgr_config.mbuf_high_water_jumbo);
8811 }
8812 tw32(BUFMGR_DMA_LOW_WATER,
8813 tp->bufmgr_config.dma_low_water);
8814 tw32(BUFMGR_DMA_HIGH_WATER,
8815 tp->bufmgr_config.dma_high_water);
8816
Matt Carlsond309a462010-09-30 10:34:31 +00008817 val = BUFMGR_MODE_ENABLE | BUFMGR_MODE_ATTN_ENABLE;
8818 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
8819 val |= BUFMGR_MODE_NO_TX_UNDERRUN;
Matt Carlson4d958472011-04-20 07:57:35 +00008820 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
8821 tp->pci_chip_rev_id == CHIPREV_ID_5719_A0 ||
8822 tp->pci_chip_rev_id == CHIPREV_ID_5720_A0)
8823 val |= BUFMGR_MODE_MBLOW_ATTN_ENAB;
Matt Carlsond309a462010-09-30 10:34:31 +00008824 tw32(BUFMGR_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008825 for (i = 0; i < 2000; i++) {
8826 if (tr32(BUFMGR_MODE) & BUFMGR_MODE_ENABLE)
8827 break;
8828 udelay(10);
8829 }
8830 if (i >= 2000) {
Joe Perches05dbe002010-02-17 19:44:19 +00008831 netdev_err(tp->dev, "%s cannot enable BUFMGR\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008832 return -ENODEV;
8833 }
8834
Matt Carlsoneb07a942011-04-20 07:57:36 +00008835 if (tp->pci_chip_rev_id == CHIPREV_ID_5906_A1)
8836 tw32(ISO_PKT_TX, (tr32(ISO_PKT_TX) & ~0x3) | 0x2);
Michael Chanb5d37722006-09-27 16:06:21 -07008837
Matt Carlsoneb07a942011-04-20 07:57:36 +00008838 tg3_setup_rxbd_thresholds(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008839
8840 /* Initialize TG3_BDINFO's at:
8841 * RCVDBDI_STD_BD: standard eth size rx ring
8842 * RCVDBDI_JUMBO_BD: jumbo frame rx ring
8843 * RCVDBDI_MINI_BD: small frame rx ring (??? does not work)
8844 *
8845 * like so:
8846 * TG3_BDINFO_HOST_ADDR: high/low parts of DMA address of ring
8847 * TG3_BDINFO_MAXLEN_FLAGS: (rx max buffer size << 16) |
8848 * ring attribute flags
8849 * TG3_BDINFO_NIC_ADDR: location of descriptors in nic SRAM
8850 *
8851 * Standard receive ring @ NIC_SRAM_RX_BUFFER_DESC, 512 entries.
8852 * Jumbo receive ring @ NIC_SRAM_RX_JUMBO_BUFFER_DESC, 256 entries.
8853 *
8854 * The size of each ring is fixed in the firmware, but the location is
8855 * configurable.
8856 */
8857 tw32(RCVDBDI_STD_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH,
Matt Carlson21f581a2009-08-28 14:00:25 +00008858 ((u64) tpr->rx_std_mapping >> 32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07008859 tw32(RCVDBDI_STD_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW,
Matt Carlson21f581a2009-08-28 14:00:25 +00008860 ((u64) tpr->rx_std_mapping & 0xffffffff));
Joe Perches63c3a662011-04-26 08:12:10 +00008861 if (!tg3_flag(tp, 5717_PLUS))
Matt Carlson87668d32009-11-13 13:03:34 +00008862 tw32(RCVDBDI_STD_BD + TG3_BDINFO_NIC_ADDR,
8863 NIC_SRAM_RX_BUFFER_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008864
Matt Carlsonfdb72b32009-08-28 13:57:12 +00008865 /* Disable the mini ring */
Joe Perches63c3a662011-04-26 08:12:10 +00008866 if (!tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008867 tw32(RCVDBDI_MINI_BD + TG3_BDINFO_MAXLEN_FLAGS,
8868 BDINFO_FLAGS_DISABLED);
8869
Matt Carlsonfdb72b32009-08-28 13:57:12 +00008870 /* Program the jumbo buffer descriptor ring control
8871 * blocks on those devices that have them.
8872 */
Matt Carlsona0512942011-07-27 14:20:54 +00008873 if (tp->pci_chip_rev_id == CHIPREV_ID_5719_A0 ||
Joe Perches63c3a662011-04-26 08:12:10 +00008874 (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008875
Joe Perches63c3a662011-04-26 08:12:10 +00008876 if (tg3_flag(tp, JUMBO_RING_ENABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008877 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH,
Matt Carlson21f581a2009-08-28 14:00:25 +00008878 ((u64) tpr->rx_jmb_mapping >> 32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07008879 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW,
Matt Carlson21f581a2009-08-28 14:00:25 +00008880 ((u64) tpr->rx_jmb_mapping & 0xffffffff));
Matt Carlsonde9f5232011-04-05 14:22:43 +00008881 val = TG3_RX_JMB_RING_SIZE(tp) <<
8882 BDINFO_FLAGS_MAXLEN_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008883 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_MAXLEN_FLAGS,
Matt Carlsonde9f5232011-04-05 14:22:43 +00008884 val | BDINFO_FLAGS_USE_EXT_RECV);
Joe Perches63c3a662011-04-26 08:12:10 +00008885 if (!tg3_flag(tp, USE_JUMBO_BDFLAG) ||
Matt Carlson55086ad2011-12-14 11:09:59 +00008886 tg3_flag(tp, 57765_CLASS))
Matt Carlson87668d32009-11-13 13:03:34 +00008887 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_NIC_ADDR,
8888 NIC_SRAM_RX_JUMBO_BUFFER_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008889 } else {
8890 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_MAXLEN_FLAGS,
8891 BDINFO_FLAGS_DISABLED);
8892 }
8893
Joe Perches63c3a662011-04-26 08:12:10 +00008894 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlsonfa6b2aa2011-11-21 15:01:19 +00008895 val = TG3_RX_STD_RING_SIZE(tp);
Matt Carlson7cb32cf2010-09-30 10:34:36 +00008896 val <<= BDINFO_FLAGS_MAXLEN_SHIFT;
8897 val |= (TG3_RX_STD_DMA_SZ << 2);
8898 } else
Matt Carlson04380d42010-04-12 06:58:29 +00008899 val = TG3_RX_STD_DMA_SZ << BDINFO_FLAGS_MAXLEN_SHIFT;
Matt Carlsonfdb72b32009-08-28 13:57:12 +00008900 } else
Matt Carlsonde9f5232011-04-05 14:22:43 +00008901 val = TG3_RX_STD_MAX_SIZE_5700 << BDINFO_FLAGS_MAXLEN_SHIFT;
Matt Carlsonfdb72b32009-08-28 13:57:12 +00008902
8903 tw32(RCVDBDI_STD_BD + TG3_BDINFO_MAXLEN_FLAGS, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008904
Matt Carlson411da642009-11-13 13:03:46 +00008905 tpr->rx_std_prod_idx = tp->rx_pending;
Matt Carlson66711e62009-11-13 13:03:49 +00008906 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG, tpr->rx_std_prod_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008907
Joe Perches63c3a662011-04-26 08:12:10 +00008908 tpr->rx_jmb_prod_idx =
8909 tg3_flag(tp, JUMBO_RING_ENABLE) ? tp->rx_jumbo_pending : 0;
Matt Carlson66711e62009-11-13 13:03:49 +00008910 tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG, tpr->rx_jmb_prod_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008911
Matt Carlson2d31eca2009-09-01 12:53:31 +00008912 tg3_rings_reset(tp);
8913
Linus Torvalds1da177e2005-04-16 15:20:36 -07008914 /* Initialize MAC address and backoff seed. */
Michael Chan986e0ae2007-05-05 12:10:20 -07008915 __tg3_set_mac_addr(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008916
8917 /* MTU + ethernet header + FCS + optional VLAN tag */
Matt Carlsonf7b493e2009-02-25 14:21:52 +00008918 tw32(MAC_RX_MTU_SIZE,
8919 tp->dev->mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008920
8921 /* The slot time is changed by tg3_setup_phy if we
8922 * run at gigabit with half duplex.
8923 */
Matt Carlsonf2096f92011-04-05 14:22:48 +00008924 val = (2 << TX_LENGTHS_IPG_CRS_SHIFT) |
8925 (6 << TX_LENGTHS_IPG_SHIFT) |
8926 (32 << TX_LENGTHS_SLOT_TIME_SHIFT);
8927
8928 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
8929 val |= tr32(MAC_TX_LENGTHS) &
8930 (TX_LENGTHS_JMB_FRM_LEN_MSK |
8931 TX_LENGTHS_CNT_DWN_VAL_MSK);
8932
8933 tw32(MAC_TX_LENGTHS, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008934
8935 /* Receive rules. */
8936 tw32(MAC_RCV_RULE_CFG, RCV_RULE_CFG_DEFAULT_CLASS);
8937 tw32(RCVLPC_CONFIG, 0x0181);
8938
8939 /* Calculate RDMAC_MODE setting early, we need it to determine
8940 * the RCVLPC_STATE_ENABLE mask.
8941 */
8942 rdmac_mode = (RDMAC_MODE_ENABLE | RDMAC_MODE_TGTABORT_ENAB |
8943 RDMAC_MODE_MSTABORT_ENAB | RDMAC_MODE_PARITYERR_ENAB |
8944 RDMAC_MODE_ADDROFLOW_ENAB | RDMAC_MODE_FIFOOFLOW_ENAB |
8945 RDMAC_MODE_FIFOURUN_ENAB | RDMAC_MODE_FIFOOREAD_ENAB |
8946 RDMAC_MODE_LNGREAD_ENAB);
Michael Chan85e94ce2005-04-21 17:05:28 -07008947
Matt Carlsondeabaac2010-11-24 08:31:50 +00008948 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717)
Matt Carlson0339e4e2010-02-12 14:47:09 +00008949 rdmac_mode |= RDMAC_MODE_MULT_DMA_RD_DIS;
8950
Matt Carlson57e69832008-05-25 23:48:31 -07008951 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson321d32a2008-11-21 17:22:19 -08008952 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
8953 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
Matt Carlsond30cdd22007-10-07 23:28:35 -07008954 rdmac_mode |= RDMAC_MODE_BD_SBD_CRPT_ENAB |
8955 RDMAC_MODE_MBUF_RBD_CRPT_ENAB |
8956 RDMAC_MODE_MBUF_SBD_CRPT_ENAB;
8957
Matt Carlsonc5908932011-03-09 16:58:25 +00008958 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
8959 tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) {
Joe Perches63c3a662011-04-26 08:12:10 +00008960 if (tg3_flag(tp, TSO_CAPABLE) &&
Matt Carlsonc13e3712007-05-05 11:50:04 -07008961 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008962 rdmac_mode |= RDMAC_MODE_FIFO_SIZE_128;
8963 } else if (!(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH) &&
Joe Perches63c3a662011-04-26 08:12:10 +00008964 !tg3_flag(tp, IS_5788)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008965 rdmac_mode |= RDMAC_MODE_FIFO_LONG_BURST;
8966 }
8967 }
8968
Joe Perches63c3a662011-04-26 08:12:10 +00008969 if (tg3_flag(tp, PCI_EXPRESS))
Michael Chan85e94ce2005-04-21 17:05:28 -07008970 rdmac_mode |= RDMAC_MODE_FIFO_LONG_BURST;
8971
Joe Perches63c3a662011-04-26 08:12:10 +00008972 if (tg3_flag(tp, HW_TSO_1) ||
8973 tg3_flag(tp, HW_TSO_2) ||
8974 tg3_flag(tp, HW_TSO_3))
Matt Carlson027455a2008-12-21 20:19:30 -08008975 rdmac_mode |= RDMAC_MODE_IPV4_LSO_EN;
8976
Matt Carlson108a6c12011-05-19 12:12:47 +00008977 if (tg3_flag(tp, 57765_PLUS) ||
Matt Carlsone849cdc2009-11-13 13:03:38 +00008978 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Matt Carlson027455a2008-12-21 20:19:30 -08008979 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
8980 rdmac_mode |= RDMAC_MODE_IPV6_LSO_EN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008981
Matt Carlsonf2096f92011-04-05 14:22:48 +00008982 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
8983 rdmac_mode |= tr32(RDMAC_MODE) & RDMAC_MODE_H2BNC_VLAN_DET;
8984
Matt Carlson41a8a7e2010-09-15 08:59:53 +00008985 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
8986 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
8987 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
8988 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
Joe Perches63c3a662011-04-26 08:12:10 +00008989 tg3_flag(tp, 57765_PLUS)) {
Matt Carlson41a8a7e2010-09-15 08:59:53 +00008990 val = tr32(TG3_RDMA_RSRVCTRL_REG);
Matt Carlsond78b59f2011-04-05 14:22:46 +00008991 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
8992 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
Matt Carlsonb4495ed2011-01-25 15:58:47 +00008993 val &= ~(TG3_RDMA_RSRVCTRL_TXMRGN_MASK |
8994 TG3_RDMA_RSRVCTRL_FIFO_LWM_MASK |
8995 TG3_RDMA_RSRVCTRL_FIFO_HWM_MASK);
8996 val |= TG3_RDMA_RSRVCTRL_TXMRGN_320B |
8997 TG3_RDMA_RSRVCTRL_FIFO_LWM_1_5K |
8998 TG3_RDMA_RSRVCTRL_FIFO_HWM_1_5K;
Matt Carlsonb75cc0e2010-11-24 08:31:46 +00008999 }
Matt Carlson41a8a7e2010-09-15 08:59:53 +00009000 tw32(TG3_RDMA_RSRVCTRL_REG,
9001 val | TG3_RDMA_RSRVCTRL_FIFO_OFLW_FIX);
9002 }
9003
Matt Carlsond78b59f2011-04-05 14:22:46 +00009004 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
9005 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
Matt Carlsond309a462010-09-30 10:34:31 +00009006 val = tr32(TG3_LSO_RD_DMA_CRPTEN_CTRL);
9007 tw32(TG3_LSO_RD_DMA_CRPTEN_CTRL, val |
9008 TG3_LSO_RD_DMA_CRPTEN_CTRL_BLEN_BD_4K |
9009 TG3_LSO_RD_DMA_CRPTEN_CTRL_BLEN_LSO_4K);
9010 }
9011
Linus Torvalds1da177e2005-04-16 15:20:36 -07009012 /* Receive/send statistics. */
Joe Perches63c3a662011-04-26 08:12:10 +00009013 if (tg3_flag(tp, 5750_PLUS)) {
Michael Chan16613942006-06-29 20:15:13 -07009014 val = tr32(RCVLPC_STATS_ENABLE);
9015 val &= ~RCVLPC_STATSENAB_DACK_FIX;
9016 tw32(RCVLPC_STATS_ENABLE, val);
9017 } else if ((rdmac_mode & RDMAC_MODE_FIFO_SIZE_128) &&
Joe Perches63c3a662011-04-26 08:12:10 +00009018 tg3_flag(tp, TSO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009019 val = tr32(RCVLPC_STATS_ENABLE);
9020 val &= ~RCVLPC_STATSENAB_LNGBRST_RFIX;
9021 tw32(RCVLPC_STATS_ENABLE, val);
9022 } else {
9023 tw32(RCVLPC_STATS_ENABLE, 0xffffff);
9024 }
9025 tw32(RCVLPC_STATSCTRL, RCVLPC_STATSCTRL_ENABLE);
9026 tw32(SNDDATAI_STATSENAB, 0xffffff);
9027 tw32(SNDDATAI_STATSCTRL,
9028 (SNDDATAI_SCTRL_ENABLE |
9029 SNDDATAI_SCTRL_FASTUPD));
9030
9031 /* Setup host coalescing engine. */
9032 tw32(HOSTCC_MODE, 0);
9033 for (i = 0; i < 2000; i++) {
9034 if (!(tr32(HOSTCC_MODE) & HOSTCC_MODE_ENABLE))
9035 break;
9036 udelay(10);
9037 }
9038
Michael Chand244c892005-07-05 14:42:33 -07009039 __tg3_set_coalesce(tp, &tp->coal);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009040
Joe Perches63c3a662011-04-26 08:12:10 +00009041 if (!tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009042 /* Status/statistics block address. See tg3_timer,
9043 * the tg3_periodic_fetch_stats call there, and
9044 * tg3_get_stats to see how this works for 5705/5750 chips.
9045 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07009046 tw32(HOSTCC_STATS_BLK_HOST_ADDR + TG3_64BIT_REG_HIGH,
9047 ((u64) tp->stats_mapping >> 32));
9048 tw32(HOSTCC_STATS_BLK_HOST_ADDR + TG3_64BIT_REG_LOW,
9049 ((u64) tp->stats_mapping & 0xffffffff));
9050 tw32(HOSTCC_STATS_BLK_NIC_ADDR, NIC_SRAM_STATS_BLK);
Matt Carlson2d31eca2009-09-01 12:53:31 +00009051
Linus Torvalds1da177e2005-04-16 15:20:36 -07009052 tw32(HOSTCC_STATUS_BLK_NIC_ADDR, NIC_SRAM_STATUS_BLK);
Matt Carlson2d31eca2009-09-01 12:53:31 +00009053
9054 /* Clear statistics and status block memory areas */
9055 for (i = NIC_SRAM_STATS_BLK;
9056 i < NIC_SRAM_STATUS_BLK + TG3_HW_STATUS_SIZE;
9057 i += sizeof(u32)) {
9058 tg3_write_mem(tp, i, 0);
9059 udelay(40);
9060 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009061 }
9062
9063 tw32(HOSTCC_MODE, HOSTCC_MODE_ENABLE | tp->coalesce_mode);
9064
9065 tw32(RCVCC_MODE, RCVCC_MODE_ENABLE | RCVCC_MODE_ATTN_ENABLE);
9066 tw32(RCVLPC_MODE, RCVLPC_MODE_ENABLE);
Joe Perches63c3a662011-04-26 08:12:10 +00009067 if (!tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009068 tw32(RCVLSC_MODE, RCVLSC_MODE_ENABLE | RCVLSC_MODE_ATTN_ENABLE);
9069
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009070 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES) {
9071 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chanc94e3942005-09-27 12:12:42 -07009072 /* reset to prevent losing 1st rx packet intermittently */
9073 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
9074 udelay(10);
9075 }
9076
Matt Carlson3bda1252008-08-15 14:08:22 -07009077 tp->mac_mode |= MAC_MODE_TXSTAT_ENABLE | MAC_MODE_RXSTAT_ENABLE |
Matt Carlson9e975cc2011-07-20 10:20:50 +00009078 MAC_MODE_TDE_ENABLE | MAC_MODE_RDE_ENABLE |
9079 MAC_MODE_FHDE_ENABLE;
9080 if (tg3_flag(tp, ENABLE_APE))
9081 tp->mac_mode |= MAC_MODE_APE_TX_EN | MAC_MODE_APE_RX_EN;
Joe Perches63c3a662011-04-26 08:12:10 +00009082 if (!tg3_flag(tp, 5705_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009083 !(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07009084 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700)
9085 tp->mac_mode |= MAC_MODE_LINK_POLARITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009086 tw32_f(MAC_MODE, tp->mac_mode | MAC_MODE_RXSTAT_CLEAR | MAC_MODE_TXSTAT_CLEAR);
9087 udelay(40);
9088
Michael Chan314fba32005-04-21 17:07:04 -07009089 /* tp->grc_local_ctrl is partially set up during tg3_get_invariants().
Joe Perches63c3a662011-04-26 08:12:10 +00009090 * If TG3_FLAG_IS_NIC is zero, we should read the
Michael Chan314fba32005-04-21 17:07:04 -07009091 * register to preserve the GPIO settings for LOMs. The GPIOs,
9092 * whether used as inputs or outputs, are set by boot code after
9093 * reset.
9094 */
Joe Perches63c3a662011-04-26 08:12:10 +00009095 if (!tg3_flag(tp, IS_NIC)) {
Michael Chan314fba32005-04-21 17:07:04 -07009096 u32 gpio_mask;
9097
Michael Chan9d26e212006-12-07 00:21:14 -08009098 gpio_mask = GRC_LCLCTRL_GPIO_OE0 | GRC_LCLCTRL_GPIO_OE1 |
9099 GRC_LCLCTRL_GPIO_OE2 | GRC_LCLCTRL_GPIO_OUTPUT0 |
9100 GRC_LCLCTRL_GPIO_OUTPUT1 | GRC_LCLCTRL_GPIO_OUTPUT2;
Michael Chan3e7d83b2005-04-21 17:10:36 -07009101
9102 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752)
9103 gpio_mask |= GRC_LCLCTRL_GPIO_OE3 |
9104 GRC_LCLCTRL_GPIO_OUTPUT3;
9105
Michael Chanaf36e6b2006-03-23 01:28:06 -08009106 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755)
9107 gpio_mask |= GRC_LCLCTRL_GPIO_UART_SEL;
9108
Gary Zambranoaaf84462007-05-05 11:51:45 -07009109 tp->grc_local_ctrl &= ~gpio_mask;
Michael Chan314fba32005-04-21 17:07:04 -07009110 tp->grc_local_ctrl |= tr32(GRC_LOCAL_CTRL) & gpio_mask;
9111
9112 /* GPIO1 must be driven high for eeprom write protect */
Joe Perches63c3a662011-04-26 08:12:10 +00009113 if (tg3_flag(tp, EEPROM_WRITE_PROT))
Michael Chan9d26e212006-12-07 00:21:14 -08009114 tp->grc_local_ctrl |= (GRC_LCLCTRL_GPIO_OE1 |
9115 GRC_LCLCTRL_GPIO_OUTPUT1);
Michael Chan314fba32005-04-21 17:07:04 -07009116 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009117 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
9118 udelay(100);
9119
Matt Carlsonc3b50032012-01-17 15:27:23 +00009120 if (tg3_flag(tp, USING_MSIX)) {
Matt Carlsonbaf8a942009-09-01 13:13:00 +00009121 val = tr32(MSGINT_MODE);
Matt Carlsonc3b50032012-01-17 15:27:23 +00009122 val |= MSGINT_MODE_ENABLE;
9123 if (tp->irq_cnt > 1)
9124 val |= MSGINT_MODE_MULTIVEC_EN;
Matt Carlson5b39de92011-08-31 11:44:50 +00009125 if (!tg3_flag(tp, 1SHOT_MSI))
9126 val |= MSGINT_MODE_ONE_SHOT_DISABLE;
Matt Carlsonbaf8a942009-09-01 13:13:00 +00009127 tw32(MSGINT_MODE, val);
9128 }
9129
Joe Perches63c3a662011-04-26 08:12:10 +00009130 if (!tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009131 tw32_f(DMAC_MODE, DMAC_MODE_ENABLE);
9132 udelay(40);
9133 }
9134
9135 val = (WDMAC_MODE_ENABLE | WDMAC_MODE_TGTABORT_ENAB |
9136 WDMAC_MODE_MSTABORT_ENAB | WDMAC_MODE_PARITYERR_ENAB |
9137 WDMAC_MODE_ADDROFLOW_ENAB | WDMAC_MODE_FIFOOFLOW_ENAB |
9138 WDMAC_MODE_FIFOURUN_ENAB | WDMAC_MODE_FIFOOREAD_ENAB |
9139 WDMAC_MODE_LNGREAD_ENAB);
9140
Matt Carlsonc5908932011-03-09 16:58:25 +00009141 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
9142 tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) {
Joe Perches63c3a662011-04-26 08:12:10 +00009143 if (tg3_flag(tp, TSO_CAPABLE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07009144 (tp->pci_chip_rev_id == CHIPREV_ID_5705_A1 ||
9145 tp->pci_chip_rev_id == CHIPREV_ID_5705_A2)) {
9146 /* nothing */
9147 } else if (!(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH) &&
Joe Perches63c3a662011-04-26 08:12:10 +00009148 !tg3_flag(tp, IS_5788)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009149 val |= WDMAC_MODE_RX_ACCEL;
9150 }
9151 }
9152
Michael Chand9ab5ad2006-03-20 22:27:35 -08009153 /* Enable host coalescing bug fix */
Joe Perches63c3a662011-04-26 08:12:10 +00009154 if (tg3_flag(tp, 5755_PLUS))
Matt Carlsonf51f3562008-05-25 23:45:08 -07009155 val |= WDMAC_MODE_STATUS_TAG_FIX;
Michael Chand9ab5ad2006-03-20 22:27:35 -08009156
Matt Carlson788a0352009-11-02 14:26:03 +00009157 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
9158 val |= WDMAC_MODE_BURST_ALL_DATA;
9159
Linus Torvalds1da177e2005-04-16 15:20:36 -07009160 tw32_f(WDMAC_MODE, val);
9161 udelay(40);
9162
Joe Perches63c3a662011-04-26 08:12:10 +00009163 if (tg3_flag(tp, PCIX_MODE)) {
Matt Carlson9974a352007-10-07 23:27:28 -07009164 u16 pcix_cmd;
9165
9166 pci_read_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
9167 &pcix_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009168 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703) {
Matt Carlson9974a352007-10-07 23:27:28 -07009169 pcix_cmd &= ~PCI_X_CMD_MAX_READ;
9170 pcix_cmd |= PCI_X_CMD_READ_2K;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009171 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
Matt Carlson9974a352007-10-07 23:27:28 -07009172 pcix_cmd &= ~(PCI_X_CMD_MAX_SPLIT | PCI_X_CMD_MAX_READ);
9173 pcix_cmd |= PCI_X_CMD_READ_2K;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009174 }
Matt Carlson9974a352007-10-07 23:27:28 -07009175 pci_write_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
9176 pcix_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009177 }
9178
9179 tw32_f(RDMAC_MODE, rdmac_mode);
9180 udelay(40);
9181
9182 tw32(RCVDCC_MODE, RCVDCC_MODE_ENABLE | RCVDCC_MODE_ATTN_ENABLE);
Joe Perches63c3a662011-04-26 08:12:10 +00009183 if (!tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009184 tw32(MBFREE_MODE, MBFREE_MODE_ENABLE);
Matt Carlson9936bcf2007-10-10 18:03:07 -07009185
9186 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
9187 tw32(SNDDATAC_MODE,
9188 SNDDATAC_MODE_ENABLE | SNDDATAC_MODE_CDELAY);
9189 else
9190 tw32(SNDDATAC_MODE, SNDDATAC_MODE_ENABLE);
9191
Linus Torvalds1da177e2005-04-16 15:20:36 -07009192 tw32(SNDBDC_MODE, SNDBDC_MODE_ENABLE | SNDBDC_MODE_ATTN_ENABLE);
9193 tw32(RCVBDI_MODE, RCVBDI_MODE_ENABLE | RCVBDI_MODE_RCB_ATTN_ENAB);
Matt Carlson7cb32cf2010-09-30 10:34:36 +00009194 val = RCVDBDI_MODE_ENABLE | RCVDBDI_MODE_INV_RING_SZ;
Joe Perches63c3a662011-04-26 08:12:10 +00009195 if (tg3_flag(tp, LRG_PROD_RING_CAP))
Matt Carlson7cb32cf2010-09-30 10:34:36 +00009196 val |= RCVDBDI_MODE_LRG_RING_SZ;
9197 tw32(RCVDBDI_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009198 tw32(SNDDATAI_MODE, SNDDATAI_MODE_ENABLE);
Joe Perches63c3a662011-04-26 08:12:10 +00009199 if (tg3_flag(tp, HW_TSO_1) ||
9200 tg3_flag(tp, HW_TSO_2) ||
9201 tg3_flag(tp, HW_TSO_3))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009202 tw32(SNDDATAI_MODE, SNDDATAI_MODE_ENABLE | 0x8);
Matt Carlsonbaf8a942009-09-01 13:13:00 +00009203 val = SNDBDI_MODE_ENABLE | SNDBDI_MODE_ATTN_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00009204 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlsonbaf8a942009-09-01 13:13:00 +00009205 val |= SNDBDI_MODE_MULTI_TXQ_EN;
9206 tw32(SNDBDI_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009207 tw32(SNDBDS_MODE, SNDBDS_MODE_ENABLE | SNDBDS_MODE_ATTN_ENABLE);
9208
9209 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0) {
9210 err = tg3_load_5701_a0_firmware_fix(tp);
9211 if (err)
9212 return err;
9213 }
9214
Joe Perches63c3a662011-04-26 08:12:10 +00009215 if (tg3_flag(tp, TSO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009216 err = tg3_load_tso_firmware(tp);
9217 if (err)
9218 return err;
9219 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009220
9221 tp->tx_mode = TX_MODE_ENABLE;
Matt Carlsonf2096f92011-04-05 14:22:48 +00009222
Joe Perches63c3a662011-04-26 08:12:10 +00009223 if (tg3_flag(tp, 5755_PLUS) ||
Matt Carlsonb1d05212010-06-05 17:24:31 +00009224 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
9225 tp->tx_mode |= TX_MODE_MBUF_LOCKUP_FIX;
Matt Carlsonf2096f92011-04-05 14:22:48 +00009226
9227 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
9228 val = TX_MODE_JMB_FRM_LEN | TX_MODE_CNT_DN_MODE;
9229 tp->tx_mode &= ~val;
9230 tp->tx_mode |= tr32(MAC_TX_MODE) & val;
9231 }
9232
Linus Torvalds1da177e2005-04-16 15:20:36 -07009233 tw32_f(MAC_TX_MODE, tp->tx_mode);
9234 udelay(100);
9235
Joe Perches63c3a662011-04-26 08:12:10 +00009236 if (tg3_flag(tp, ENABLE_RSS)) {
Matt Carlsonbcebcc42011-12-14 11:10:01 +00009237 tg3_rss_write_indir_tbl(tp);
Matt Carlsonbaf8a942009-09-01 13:13:00 +00009238
9239 /* Setup the "secret" hash key. */
9240 tw32(MAC_RSS_HASH_KEY_0, 0x5f865437);
9241 tw32(MAC_RSS_HASH_KEY_1, 0xe4ac62cc);
9242 tw32(MAC_RSS_HASH_KEY_2, 0x50103a45);
9243 tw32(MAC_RSS_HASH_KEY_3, 0x36621985);
9244 tw32(MAC_RSS_HASH_KEY_4, 0xbf14c0e8);
9245 tw32(MAC_RSS_HASH_KEY_5, 0x1bc27a1e);
9246 tw32(MAC_RSS_HASH_KEY_6, 0x84f4b556);
9247 tw32(MAC_RSS_HASH_KEY_7, 0x094ea6fe);
9248 tw32(MAC_RSS_HASH_KEY_8, 0x7dda01e7);
9249 tw32(MAC_RSS_HASH_KEY_9, 0xc04d7481);
9250 }
9251
Linus Torvalds1da177e2005-04-16 15:20:36 -07009252 tp->rx_mode = RX_MODE_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00009253 if (tg3_flag(tp, 5755_PLUS))
Michael Chanaf36e6b2006-03-23 01:28:06 -08009254 tp->rx_mode |= RX_MODE_IPV6_CSUM_ENABLE;
9255
Joe Perches63c3a662011-04-26 08:12:10 +00009256 if (tg3_flag(tp, ENABLE_RSS))
Matt Carlsonbaf8a942009-09-01 13:13:00 +00009257 tp->rx_mode |= RX_MODE_RSS_ENABLE |
9258 RX_MODE_RSS_ITBL_HASH_BITS_7 |
9259 RX_MODE_RSS_IPV6_HASH_EN |
9260 RX_MODE_RSS_TCP_IPV6_HASH_EN |
9261 RX_MODE_RSS_IPV4_HASH_EN |
9262 RX_MODE_RSS_TCP_IPV4_HASH_EN;
9263
Linus Torvalds1da177e2005-04-16 15:20:36 -07009264 tw32_f(MAC_RX_MODE, tp->rx_mode);
9265 udelay(10);
9266
Linus Torvalds1da177e2005-04-16 15:20:36 -07009267 tw32(MAC_LED_CTRL, tp->led_ctrl);
9268
9269 tw32(MAC_MI_STAT, MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009270 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009271 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
9272 udelay(10);
9273 }
9274 tw32_f(MAC_RX_MODE, tp->rx_mode);
9275 udelay(10);
9276
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009277 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009278 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009279 !(tp->phy_flags & TG3_PHYFLG_SERDES_PREEMPHASIS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009280 /* Set drive transmission level to 1.2V */
9281 /* only if the signal pre-emphasis bit is not set */
9282 val = tr32(MAC_SERDES_CFG);
9283 val &= 0xfffff000;
9284 val |= 0x880;
9285 tw32(MAC_SERDES_CFG, val);
9286 }
9287 if (tp->pci_chip_rev_id == CHIPREV_ID_5703_A1)
9288 tw32(MAC_SERDES_CFG, 0x616000);
9289 }
9290
9291 /* Prevent chip from dropping frames when flow control
9292 * is enabled.
9293 */
Matt Carlson55086ad2011-12-14 11:09:59 +00009294 if (tg3_flag(tp, 57765_CLASS))
Matt Carlson666bc832010-01-20 16:58:03 +00009295 val = 1;
9296 else
9297 val = 2;
9298 tw32_f(MAC_LOW_WMARK_MAX_RX_FRAME, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009299
9300 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009301 (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009302 /* Use hardware link auto-negotiation */
Joe Perches63c3a662011-04-26 08:12:10 +00009303 tg3_flag_set(tp, HW_AUTONEG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009304 }
9305
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009306 if ((tp->phy_flags & TG3_PHYFLG_MII_SERDES) &&
Matt Carlson6ff6f812011-05-19 12:12:54 +00009307 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
Michael Chand4d2c552006-03-20 17:47:20 -08009308 u32 tmp;
9309
9310 tmp = tr32(SERDES_RX_CTRL);
9311 tw32(SERDES_RX_CTRL, tmp | SERDES_RX_SIG_DETECT);
9312 tp->grc_local_ctrl &= ~GRC_LCLCTRL_USE_EXT_SIG_DETECT;
9313 tp->grc_local_ctrl |= GRC_LCLCTRL_USE_SIG_DETECT;
9314 tw32(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
9315 }
9316
Joe Perches63c3a662011-04-26 08:12:10 +00009317 if (!tg3_flag(tp, USE_PHYLIB)) {
Matt Carlsonc6700ce2012-02-13 15:20:15 +00009318 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Matt Carlson800960682010-08-02 11:26:06 +00009319 tp->phy_flags &= ~TG3_PHYFLG_IS_LOW_POWER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009320
Matt Carlsondd477002008-05-25 23:45:58 -07009321 err = tg3_setup_phy(tp, 0);
9322 if (err)
9323 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009324
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009325 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
9326 !(tp->phy_flags & TG3_PHYFLG_IS_FET)) {
Matt Carlsondd477002008-05-25 23:45:58 -07009327 u32 tmp;
9328
9329 /* Clear CRC stats. */
9330 if (!tg3_readphy(tp, MII_TG3_TEST1, &tmp)) {
9331 tg3_writephy(tp, MII_TG3_TEST1,
9332 tmp | MII_TG3_TEST1_CRC_EN);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00009333 tg3_readphy(tp, MII_TG3_RXR_COUNTERS, &tmp);
Matt Carlsondd477002008-05-25 23:45:58 -07009334 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009335 }
9336 }
9337
9338 __tg3_set_rx_mode(tp->dev);
9339
9340 /* Initialize receive rules. */
9341 tw32(MAC_RCV_RULE_0, 0xc2000000 & RCV_RULE_DISABLE_MASK);
9342 tw32(MAC_RCV_VALUE_0, 0xffffffff & RCV_RULE_DISABLE_MASK);
9343 tw32(MAC_RCV_RULE_1, 0x86000004 & RCV_RULE_DISABLE_MASK);
9344 tw32(MAC_RCV_VALUE_1, 0xffffffff & RCV_RULE_DISABLE_MASK);
9345
Joe Perches63c3a662011-04-26 08:12:10 +00009346 if (tg3_flag(tp, 5705_PLUS) && !tg3_flag(tp, 5780_CLASS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009347 limit = 8;
9348 else
9349 limit = 16;
Joe Perches63c3a662011-04-26 08:12:10 +00009350 if (tg3_flag(tp, ENABLE_ASF))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009351 limit -= 4;
9352 switch (limit) {
9353 case 16:
9354 tw32(MAC_RCV_RULE_15, 0); tw32(MAC_RCV_VALUE_15, 0);
9355 case 15:
9356 tw32(MAC_RCV_RULE_14, 0); tw32(MAC_RCV_VALUE_14, 0);
9357 case 14:
9358 tw32(MAC_RCV_RULE_13, 0); tw32(MAC_RCV_VALUE_13, 0);
9359 case 13:
9360 tw32(MAC_RCV_RULE_12, 0); tw32(MAC_RCV_VALUE_12, 0);
9361 case 12:
9362 tw32(MAC_RCV_RULE_11, 0); tw32(MAC_RCV_VALUE_11, 0);
9363 case 11:
9364 tw32(MAC_RCV_RULE_10, 0); tw32(MAC_RCV_VALUE_10, 0);
9365 case 10:
9366 tw32(MAC_RCV_RULE_9, 0); tw32(MAC_RCV_VALUE_9, 0);
9367 case 9:
9368 tw32(MAC_RCV_RULE_8, 0); tw32(MAC_RCV_VALUE_8, 0);
9369 case 8:
9370 tw32(MAC_RCV_RULE_7, 0); tw32(MAC_RCV_VALUE_7, 0);
9371 case 7:
9372 tw32(MAC_RCV_RULE_6, 0); tw32(MAC_RCV_VALUE_6, 0);
9373 case 6:
9374 tw32(MAC_RCV_RULE_5, 0); tw32(MAC_RCV_VALUE_5, 0);
9375 case 5:
9376 tw32(MAC_RCV_RULE_4, 0); tw32(MAC_RCV_VALUE_4, 0);
9377 case 4:
9378 /* tw32(MAC_RCV_RULE_3, 0); tw32(MAC_RCV_VALUE_3, 0); */
9379 case 3:
9380 /* tw32(MAC_RCV_RULE_2, 0); tw32(MAC_RCV_VALUE_2, 0); */
9381 case 2:
9382 case 1:
9383
9384 default:
9385 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07009386 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009387
Joe Perches63c3a662011-04-26 08:12:10 +00009388 if (tg3_flag(tp, ENABLE_APE))
Matt Carlson9ce768e2007-10-11 19:49:11 -07009389 /* Write our heartbeat update interval to APE. */
9390 tg3_ape_write32(tp, TG3_APE_HOST_HEARTBEAT_INT_MS,
9391 APE_HOST_HEARTBEAT_INT_DISABLE);
Matt Carlson0d3031d2007-10-10 18:02:43 -07009392
Linus Torvalds1da177e2005-04-16 15:20:36 -07009393 tg3_write_sig_post_reset(tp, RESET_KIND_INIT);
9394
Linus Torvalds1da177e2005-04-16 15:20:36 -07009395 return 0;
9396}
9397
9398/* Called at device open time to get the chip ready for
9399 * packet processing. Invoked with tp->lock held.
9400 */
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07009401static int tg3_init_hw(struct tg3 *tp, int reset_phy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009402{
Linus Torvalds1da177e2005-04-16 15:20:36 -07009403 tg3_switch_clocks(tp);
9404
9405 tw32(TG3PCI_MEM_WIN_BASE_ADDR, 0);
9406
Matt Carlson2f751b62008-08-04 23:17:34 -07009407 return tg3_reset_hw(tp, reset_phy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009408}
9409
9410#define TG3_STAT_ADD32(PSTAT, REG) \
9411do { u32 __val = tr32(REG); \
9412 (PSTAT)->low += __val; \
9413 if ((PSTAT)->low < __val) \
9414 (PSTAT)->high += 1; \
9415} while (0)
9416
9417static void tg3_periodic_fetch_stats(struct tg3 *tp)
9418{
9419 struct tg3_hw_stats *sp = tp->hw_stats;
9420
9421 if (!netif_carrier_ok(tp->dev))
9422 return;
9423
9424 TG3_STAT_ADD32(&sp->tx_octets, MAC_TX_STATS_OCTETS);
9425 TG3_STAT_ADD32(&sp->tx_collisions, MAC_TX_STATS_COLLISIONS);
9426 TG3_STAT_ADD32(&sp->tx_xon_sent, MAC_TX_STATS_XON_SENT);
9427 TG3_STAT_ADD32(&sp->tx_xoff_sent, MAC_TX_STATS_XOFF_SENT);
9428 TG3_STAT_ADD32(&sp->tx_mac_errors, MAC_TX_STATS_MAC_ERRORS);
9429 TG3_STAT_ADD32(&sp->tx_single_collisions, MAC_TX_STATS_SINGLE_COLLISIONS);
9430 TG3_STAT_ADD32(&sp->tx_mult_collisions, MAC_TX_STATS_MULT_COLLISIONS);
9431 TG3_STAT_ADD32(&sp->tx_deferred, MAC_TX_STATS_DEFERRED);
9432 TG3_STAT_ADD32(&sp->tx_excessive_collisions, MAC_TX_STATS_EXCESSIVE_COL);
9433 TG3_STAT_ADD32(&sp->tx_late_collisions, MAC_TX_STATS_LATE_COL);
9434 TG3_STAT_ADD32(&sp->tx_ucast_packets, MAC_TX_STATS_UCAST);
9435 TG3_STAT_ADD32(&sp->tx_mcast_packets, MAC_TX_STATS_MCAST);
9436 TG3_STAT_ADD32(&sp->tx_bcast_packets, MAC_TX_STATS_BCAST);
9437
9438 TG3_STAT_ADD32(&sp->rx_octets, MAC_RX_STATS_OCTETS);
9439 TG3_STAT_ADD32(&sp->rx_fragments, MAC_RX_STATS_FRAGMENTS);
9440 TG3_STAT_ADD32(&sp->rx_ucast_packets, MAC_RX_STATS_UCAST);
9441 TG3_STAT_ADD32(&sp->rx_mcast_packets, MAC_RX_STATS_MCAST);
9442 TG3_STAT_ADD32(&sp->rx_bcast_packets, MAC_RX_STATS_BCAST);
9443 TG3_STAT_ADD32(&sp->rx_fcs_errors, MAC_RX_STATS_FCS_ERRORS);
9444 TG3_STAT_ADD32(&sp->rx_align_errors, MAC_RX_STATS_ALIGN_ERRORS);
9445 TG3_STAT_ADD32(&sp->rx_xon_pause_rcvd, MAC_RX_STATS_XON_PAUSE_RECVD);
9446 TG3_STAT_ADD32(&sp->rx_xoff_pause_rcvd, MAC_RX_STATS_XOFF_PAUSE_RECVD);
9447 TG3_STAT_ADD32(&sp->rx_mac_ctrl_rcvd, MAC_RX_STATS_MAC_CTRL_RECVD);
9448 TG3_STAT_ADD32(&sp->rx_xoff_entered, MAC_RX_STATS_XOFF_ENTERED);
9449 TG3_STAT_ADD32(&sp->rx_frame_too_long_errors, MAC_RX_STATS_FRAME_TOO_LONG);
9450 TG3_STAT_ADD32(&sp->rx_jabbers, MAC_RX_STATS_JABBERS);
9451 TG3_STAT_ADD32(&sp->rx_undersize_packets, MAC_RX_STATS_UNDERSIZE);
Michael Chan463d3052006-05-22 16:36:27 -07009452
9453 TG3_STAT_ADD32(&sp->rxbds_empty, RCVLPC_NO_RCV_BD_CNT);
Matt Carlson310050f2011-05-19 12:12:55 +00009454 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717 &&
9455 tp->pci_chip_rev_id != CHIPREV_ID_5719_A0 &&
9456 tp->pci_chip_rev_id != CHIPREV_ID_5720_A0) {
Matt Carlson4d958472011-04-20 07:57:35 +00009457 TG3_STAT_ADD32(&sp->rx_discards, RCVLPC_IN_DISCARDS_CNT);
9458 } else {
9459 u32 val = tr32(HOSTCC_FLOW_ATTN);
9460 val = (val & HOSTCC_FLOW_ATTN_MBUF_LWM) ? 1 : 0;
9461 if (val) {
9462 tw32(HOSTCC_FLOW_ATTN, HOSTCC_FLOW_ATTN_MBUF_LWM);
9463 sp->rx_discards.low += val;
9464 if (sp->rx_discards.low < val)
9465 sp->rx_discards.high += 1;
9466 }
9467 sp->mbuf_lwm_thresh_hit = sp->rx_discards;
9468 }
Michael Chan463d3052006-05-22 16:36:27 -07009469 TG3_STAT_ADD32(&sp->rx_errors, RCVLPC_IN_ERRORS_CNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009470}
9471
Matt Carlson0e6cf6a2011-06-13 13:38:55 +00009472static void tg3_chk_missed_msi(struct tg3 *tp)
9473{
9474 u32 i;
9475
9476 for (i = 0; i < tp->irq_cnt; i++) {
9477 struct tg3_napi *tnapi = &tp->napi[i];
9478
9479 if (tg3_has_work(tnapi)) {
9480 if (tnapi->last_rx_cons == tnapi->rx_rcb_ptr &&
9481 tnapi->last_tx_cons == tnapi->tx_cons) {
9482 if (tnapi->chk_msi_cnt < 1) {
9483 tnapi->chk_msi_cnt++;
9484 return;
9485 }
Matt Carlson7f230732011-08-31 11:44:48 +00009486 tg3_msi(0, tnapi);
Matt Carlson0e6cf6a2011-06-13 13:38:55 +00009487 }
9488 }
9489 tnapi->chk_msi_cnt = 0;
9490 tnapi->last_rx_cons = tnapi->rx_rcb_ptr;
9491 tnapi->last_tx_cons = tnapi->tx_cons;
9492 }
9493}
9494
Linus Torvalds1da177e2005-04-16 15:20:36 -07009495static void tg3_timer(unsigned long __opaque)
9496{
9497 struct tg3 *tp = (struct tg3 *) __opaque;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009498
Matt Carlson5b190622011-11-04 09:15:04 +00009499 if (tp->irq_sync || tg3_flag(tp, RESET_TASK_PENDING))
Michael Chanf475f162006-03-27 23:20:14 -08009500 goto restart_timer;
9501
David S. Millerf47c11e2005-06-24 20:18:35 -07009502 spin_lock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009503
Matt Carlson0e6cf6a2011-06-13 13:38:55 +00009504 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
Matt Carlson55086ad2011-12-14 11:09:59 +00009505 tg3_flag(tp, 57765_CLASS))
Matt Carlson0e6cf6a2011-06-13 13:38:55 +00009506 tg3_chk_missed_msi(tp);
9507
Joe Perches63c3a662011-04-26 08:12:10 +00009508 if (!tg3_flag(tp, TAGGED_STATUS)) {
David S. Millerfac9b832005-05-18 22:46:34 -07009509 /* All of this garbage is because when using non-tagged
9510 * IRQ status the mailbox/status_block protocol the chip
9511 * uses with the cpu is race prone.
9512 */
Matt Carlson898a56f2009-08-28 14:02:40 +00009513 if (tp->napi[0].hw_status->status & SD_STATUS_UPDATED) {
David S. Millerfac9b832005-05-18 22:46:34 -07009514 tw32(GRC_LOCAL_CTRL,
9515 tp->grc_local_ctrl | GRC_LCLCTRL_SETINT);
9516 } else {
9517 tw32(HOSTCC_MODE, tp->coalesce_mode |
Matt Carlsonfd2ce372009-09-01 12:51:13 +00009518 HOSTCC_MODE_ENABLE | HOSTCC_MODE_NOW);
David S. Millerfac9b832005-05-18 22:46:34 -07009519 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009520
David S. Millerfac9b832005-05-18 22:46:34 -07009521 if (!(tr32(WDMAC_MODE) & WDMAC_MODE_ENABLE)) {
David S. Millerf47c11e2005-06-24 20:18:35 -07009522 spin_unlock(&tp->lock);
Matt Carlsondb219972011-11-04 09:15:03 +00009523 tg3_reset_task_schedule(tp);
Matt Carlson5b190622011-11-04 09:15:04 +00009524 goto restart_timer;
David S. Millerfac9b832005-05-18 22:46:34 -07009525 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009526 }
9527
Linus Torvalds1da177e2005-04-16 15:20:36 -07009528 /* This part only runs once per second. */
9529 if (!--tp->timer_counter) {
Joe Perches63c3a662011-04-26 08:12:10 +00009530 if (tg3_flag(tp, 5705_PLUS))
David S. Millerfac9b832005-05-18 22:46:34 -07009531 tg3_periodic_fetch_stats(tp);
9532
Matt Carlsonb0c59432011-05-19 12:12:48 +00009533 if (tp->setlpicnt && !--tp->setlpicnt)
9534 tg3_phy_eee_enable(tp);
Matt Carlson52b02d02010-10-14 10:37:41 +00009535
Joe Perches63c3a662011-04-26 08:12:10 +00009536 if (tg3_flag(tp, USE_LINKCHG_REG)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009537 u32 mac_stat;
9538 int phy_event;
9539
9540 mac_stat = tr32(MAC_STATUS);
9541
9542 phy_event = 0;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009543 if (tp->phy_flags & TG3_PHYFLG_USE_MI_INTERRUPT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009544 if (mac_stat & MAC_STATUS_MI_INTERRUPT)
9545 phy_event = 1;
9546 } else if (mac_stat & MAC_STATUS_LNKSTATE_CHANGED)
9547 phy_event = 1;
9548
9549 if (phy_event)
9550 tg3_setup_phy(tp, 0);
Joe Perches63c3a662011-04-26 08:12:10 +00009551 } else if (tg3_flag(tp, POLL_SERDES)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009552 u32 mac_stat = tr32(MAC_STATUS);
9553 int need_setup = 0;
9554
9555 if (netif_carrier_ok(tp->dev) &&
9556 (mac_stat & MAC_STATUS_LNKSTATE_CHANGED)) {
9557 need_setup = 1;
9558 }
Matt Carlsonbe98da62010-07-11 09:31:46 +00009559 if (!netif_carrier_ok(tp->dev) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07009560 (mac_stat & (MAC_STATUS_PCS_SYNCED |
9561 MAC_STATUS_SIGNAL_DET))) {
9562 need_setup = 1;
9563 }
9564 if (need_setup) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07009565 if (!tp->serdes_counter) {
9566 tw32_f(MAC_MODE,
9567 (tp->mac_mode &
9568 ~MAC_MODE_PORT_MODE_MASK));
9569 udelay(40);
9570 tw32_f(MAC_MODE, tp->mac_mode);
9571 udelay(40);
9572 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009573 tg3_setup_phy(tp, 0);
9574 }
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009575 } else if ((tp->phy_flags & TG3_PHYFLG_MII_SERDES) &&
Joe Perches63c3a662011-04-26 08:12:10 +00009576 tg3_flag(tp, 5780_CLASS)) {
Michael Chan747e8f82005-07-25 12:33:22 -07009577 tg3_serdes_parallel_detect(tp);
Matt Carlson57d8b882010-06-05 17:24:35 +00009578 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009579
9580 tp->timer_counter = tp->timer_multiplier;
9581 }
9582
Michael Chan130b8e42006-09-27 16:00:40 -07009583 /* Heartbeat is only sent once every 2 seconds.
9584 *
9585 * The heartbeat is to tell the ASF firmware that the host
9586 * driver is still alive. In the event that the OS crashes,
9587 * ASF needs to reset the hardware to free up the FIFO space
9588 * that may be filled with rx packets destined for the host.
9589 * If the FIFO is full, ASF will no longer function properly.
9590 *
9591 * Unintended resets have been reported on real time kernels
9592 * where the timer doesn't run on time. Netpoll will also have
9593 * same problem.
9594 *
9595 * The new FWCMD_NICDRV_ALIVE3 command tells the ASF firmware
9596 * to check the ring condition when the heartbeat is expiring
9597 * before doing the reset. This will prevent most unintended
9598 * resets.
9599 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07009600 if (!--tp->asf_counter) {
Joe Perches63c3a662011-04-26 08:12:10 +00009601 if (tg3_flag(tp, ENABLE_ASF) && !tg3_flag(tp, ENABLE_APE)) {
Matt Carlson7c5026a2008-05-02 16:49:29 -07009602 tg3_wait_for_event_ack(tp);
9603
Michael Chanbbadf502006-04-06 21:46:34 -07009604 tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX,
Michael Chan130b8e42006-09-27 16:00:40 -07009605 FWCMD_NICDRV_ALIVE3);
Michael Chanbbadf502006-04-06 21:46:34 -07009606 tg3_write_mem(tp, NIC_SRAM_FW_CMD_LEN_MBOX, 4);
Matt Carlsonc6cdf432010-04-05 10:19:26 +00009607 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX,
9608 TG3_FW_UPDATE_TIMEOUT_SEC);
Matt Carlson4ba526c2008-08-15 14:10:04 -07009609
9610 tg3_generate_fw_event(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009611 }
9612 tp->asf_counter = tp->asf_multiplier;
9613 }
9614
David S. Millerf47c11e2005-06-24 20:18:35 -07009615 spin_unlock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009616
Michael Chanf475f162006-03-27 23:20:14 -08009617restart_timer:
Linus Torvalds1da177e2005-04-16 15:20:36 -07009618 tp->timer.expires = jiffies + tp->timer_offset;
9619 add_timer(&tp->timer);
9620}
9621
Matt Carlson21f76382012-02-22 12:35:21 +00009622static void __devinit tg3_timer_init(struct tg3 *tp)
9623{
9624 if (tg3_flag(tp, TAGGED_STATUS) &&
9625 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717 &&
9626 !tg3_flag(tp, 57765_CLASS))
9627 tp->timer_offset = HZ;
9628 else
9629 tp->timer_offset = HZ / 10;
9630
9631 BUG_ON(tp->timer_offset > HZ);
9632
9633 tp->timer_multiplier = (HZ / tp->timer_offset);
9634 tp->asf_multiplier = (HZ / tp->timer_offset) *
9635 TG3_FW_UPDATE_FREQ_SEC;
9636
9637 init_timer(&tp->timer);
9638 tp->timer.data = (unsigned long) tp;
9639 tp->timer.function = tg3_timer;
9640}
9641
9642static void tg3_timer_start(struct tg3 *tp)
9643{
9644 tp->asf_counter = tp->asf_multiplier;
9645 tp->timer_counter = tp->timer_multiplier;
9646
9647 tp->timer.expires = jiffies + tp->timer_offset;
9648 add_timer(&tp->timer);
9649}
9650
9651static void tg3_timer_stop(struct tg3 *tp)
9652{
9653 del_timer_sync(&tp->timer);
9654}
9655
9656/* Restart hardware after configuration changes, self-test, etc.
9657 * Invoked with tp->lock held.
9658 */
9659static int tg3_restart_hw(struct tg3 *tp, int reset_phy)
9660 __releases(tp->lock)
9661 __acquires(tp->lock)
9662{
9663 int err;
9664
9665 err = tg3_init_hw(tp, reset_phy);
9666 if (err) {
9667 netdev_err(tp->dev,
9668 "Failed to re-initialize device, aborting\n");
9669 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
9670 tg3_full_unlock(tp);
9671 tg3_timer_stop(tp);
9672 tp->irq_sync = 0;
9673 tg3_napi_enable(tp);
9674 dev_close(tp->dev);
9675 tg3_full_lock(tp, 0);
9676 }
9677 return err;
9678}
9679
9680static void tg3_reset_task(struct work_struct *work)
9681{
9682 struct tg3 *tp = container_of(work, struct tg3, reset_task);
9683 int err;
9684
9685 tg3_full_lock(tp, 0);
9686
9687 if (!netif_running(tp->dev)) {
9688 tg3_flag_clear(tp, RESET_TASK_PENDING);
9689 tg3_full_unlock(tp);
9690 return;
9691 }
9692
9693 tg3_full_unlock(tp);
9694
9695 tg3_phy_stop(tp);
9696
9697 tg3_netif_stop(tp);
9698
9699 tg3_full_lock(tp, 1);
9700
9701 if (tg3_flag(tp, TX_RECOVERY_PENDING)) {
9702 tp->write32_tx_mbox = tg3_write32_tx_mbox;
9703 tp->write32_rx_mbox = tg3_write_flush_reg32;
9704 tg3_flag_set(tp, MBOX_WRITE_REORDER);
9705 tg3_flag_clear(tp, TX_RECOVERY_PENDING);
9706 }
9707
9708 tg3_halt(tp, RESET_KIND_SHUTDOWN, 0);
9709 err = tg3_init_hw(tp, 1);
9710 if (err)
9711 goto out;
9712
9713 tg3_netif_start(tp);
9714
9715out:
9716 tg3_full_unlock(tp);
9717
9718 if (!err)
9719 tg3_phy_start(tp);
9720
9721 tg3_flag_clear(tp, RESET_TASK_PENDING);
9722}
9723
Matt Carlson4f125f42009-09-01 12:55:02 +00009724static int tg3_request_irq(struct tg3 *tp, int irq_num)
Michael Chanfcfa0a32006-03-20 22:28:41 -08009725{
David Howells7d12e782006-10-05 14:55:46 +01009726 irq_handler_t fn;
Michael Chanfcfa0a32006-03-20 22:28:41 -08009727 unsigned long flags;
Matt Carlson4f125f42009-09-01 12:55:02 +00009728 char *name;
9729 struct tg3_napi *tnapi = &tp->napi[irq_num];
9730
9731 if (tp->irq_cnt == 1)
9732 name = tp->dev->name;
9733 else {
9734 name = &tnapi->irq_lbl[0];
9735 snprintf(name, IFNAMSIZ, "%s-%d", tp->dev->name, irq_num);
9736 name[IFNAMSIZ-1] = 0;
9737 }
Michael Chanfcfa0a32006-03-20 22:28:41 -08009738
Joe Perches63c3a662011-04-26 08:12:10 +00009739 if (tg3_flag(tp, USING_MSI) || tg3_flag(tp, USING_MSIX)) {
Michael Chanfcfa0a32006-03-20 22:28:41 -08009740 fn = tg3_msi;
Joe Perches63c3a662011-04-26 08:12:10 +00009741 if (tg3_flag(tp, 1SHOT_MSI))
Michael Chanfcfa0a32006-03-20 22:28:41 -08009742 fn = tg3_msi_1shot;
Javier Martinez Canillasab392d22011-03-28 16:27:31 +00009743 flags = 0;
Michael Chanfcfa0a32006-03-20 22:28:41 -08009744 } else {
9745 fn = tg3_interrupt;
Joe Perches63c3a662011-04-26 08:12:10 +00009746 if (tg3_flag(tp, TAGGED_STATUS))
Michael Chanfcfa0a32006-03-20 22:28:41 -08009747 fn = tg3_interrupt_tagged;
Javier Martinez Canillasab392d22011-03-28 16:27:31 +00009748 flags = IRQF_SHARED;
Michael Chanfcfa0a32006-03-20 22:28:41 -08009749 }
Matt Carlson4f125f42009-09-01 12:55:02 +00009750
9751 return request_irq(tnapi->irq_vec, fn, flags, name, tnapi);
Michael Chanfcfa0a32006-03-20 22:28:41 -08009752}
9753
Michael Chan79381092005-04-21 17:13:59 -07009754static int tg3_test_interrupt(struct tg3 *tp)
9755{
Matt Carlson09943a12009-08-28 14:01:57 +00009756 struct tg3_napi *tnapi = &tp->napi[0];
Michael Chan79381092005-04-21 17:13:59 -07009757 struct net_device *dev = tp->dev;
Michael Chanb16250e2006-09-27 16:10:14 -07009758 int err, i, intr_ok = 0;
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009759 u32 val;
Michael Chan79381092005-04-21 17:13:59 -07009760
Michael Chand4bc3922005-05-29 14:59:20 -07009761 if (!netif_running(dev))
9762 return -ENODEV;
9763
Michael Chan79381092005-04-21 17:13:59 -07009764 tg3_disable_ints(tp);
9765
Matt Carlson4f125f42009-09-01 12:55:02 +00009766 free_irq(tnapi->irq_vec, tnapi);
Michael Chan79381092005-04-21 17:13:59 -07009767
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009768 /*
9769 * Turn off MSI one shot mode. Otherwise this test has no
9770 * observable way to know whether the interrupt was delivered.
9771 */
Matt Carlson3aa1cdf2011-07-20 10:20:55 +00009772 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009773 val = tr32(MSGINT_MODE) | MSGINT_MODE_ONE_SHOT_DISABLE;
9774 tw32(MSGINT_MODE, val);
9775 }
9776
Matt Carlson4f125f42009-09-01 12:55:02 +00009777 err = request_irq(tnapi->irq_vec, tg3_test_isr,
Davidlohr Buesof274fd92012-02-22 03:06:54 +00009778 IRQF_SHARED, dev->name, tnapi);
Michael Chan79381092005-04-21 17:13:59 -07009779 if (err)
9780 return err;
9781
Matt Carlson898a56f2009-08-28 14:02:40 +00009782 tnapi->hw_status->status &= ~SD_STATUS_UPDATED;
Michael Chan79381092005-04-21 17:13:59 -07009783 tg3_enable_ints(tp);
9784
9785 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +00009786 tnapi->coal_now);
Michael Chan79381092005-04-21 17:13:59 -07009787
9788 for (i = 0; i < 5; i++) {
Michael Chanb16250e2006-09-27 16:10:14 -07009789 u32 int_mbox, misc_host_ctrl;
9790
Matt Carlson898a56f2009-08-28 14:02:40 +00009791 int_mbox = tr32_mailbox(tnapi->int_mbox);
Michael Chanb16250e2006-09-27 16:10:14 -07009792 misc_host_ctrl = tr32(TG3PCI_MISC_HOST_CTRL);
9793
9794 if ((int_mbox != 0) ||
9795 (misc_host_ctrl & MISC_HOST_CTRL_MASK_PCI_INT)) {
9796 intr_ok = 1;
Michael Chan79381092005-04-21 17:13:59 -07009797 break;
Michael Chanb16250e2006-09-27 16:10:14 -07009798 }
9799
Matt Carlson3aa1cdf2011-07-20 10:20:55 +00009800 if (tg3_flag(tp, 57765_PLUS) &&
9801 tnapi->hw_status->status_tag != tnapi->last_tag)
9802 tw32_mailbox_f(tnapi->int_mbox, tnapi->last_tag << 24);
9803
Michael Chan79381092005-04-21 17:13:59 -07009804 msleep(10);
9805 }
9806
9807 tg3_disable_ints(tp);
9808
Matt Carlson4f125f42009-09-01 12:55:02 +00009809 free_irq(tnapi->irq_vec, tnapi);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009810
Matt Carlson4f125f42009-09-01 12:55:02 +00009811 err = tg3_request_irq(tp, 0);
Michael Chan79381092005-04-21 17:13:59 -07009812
9813 if (err)
9814 return err;
9815
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009816 if (intr_ok) {
9817 /* Reenable MSI one shot mode. */
Matt Carlson5b39de92011-08-31 11:44:50 +00009818 if (tg3_flag(tp, 57765_PLUS) && tg3_flag(tp, 1SHOT_MSI)) {
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009819 val = tr32(MSGINT_MODE) & ~MSGINT_MODE_ONE_SHOT_DISABLE;
9820 tw32(MSGINT_MODE, val);
9821 }
Michael Chan79381092005-04-21 17:13:59 -07009822 return 0;
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009823 }
Michael Chan79381092005-04-21 17:13:59 -07009824
9825 return -EIO;
9826}
9827
9828/* Returns 0 if MSI test succeeds or MSI test fails and INTx mode is
9829 * successfully restored
9830 */
9831static int tg3_test_msi(struct tg3 *tp)
9832{
Michael Chan79381092005-04-21 17:13:59 -07009833 int err;
9834 u16 pci_cmd;
9835
Joe Perches63c3a662011-04-26 08:12:10 +00009836 if (!tg3_flag(tp, USING_MSI))
Michael Chan79381092005-04-21 17:13:59 -07009837 return 0;
9838
9839 /* Turn off SERR reporting in case MSI terminates with Master
9840 * Abort.
9841 */
9842 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
9843 pci_write_config_word(tp->pdev, PCI_COMMAND,
9844 pci_cmd & ~PCI_COMMAND_SERR);
9845
9846 err = tg3_test_interrupt(tp);
9847
9848 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
9849
9850 if (!err)
9851 return 0;
9852
9853 /* other failures */
9854 if (err != -EIO)
9855 return err;
9856
9857 /* MSI test failed, go back to INTx mode */
Matt Carlson5129c3a2010-04-05 10:19:23 +00009858 netdev_warn(tp->dev, "No interrupt was generated using MSI. Switching "
9859 "to INTx mode. Please report this failure to the PCI "
9860 "maintainer and include system chipset information\n");
Michael Chan79381092005-04-21 17:13:59 -07009861
Matt Carlson4f125f42009-09-01 12:55:02 +00009862 free_irq(tp->napi[0].irq_vec, &tp->napi[0]);
Matt Carlson09943a12009-08-28 14:01:57 +00009863
Michael Chan79381092005-04-21 17:13:59 -07009864 pci_disable_msi(tp->pdev);
9865
Joe Perches63c3a662011-04-26 08:12:10 +00009866 tg3_flag_clear(tp, USING_MSI);
Andre Detschdc8bf1b2010-04-26 07:27:07 +00009867 tp->napi[0].irq_vec = tp->pdev->irq;
Michael Chan79381092005-04-21 17:13:59 -07009868
Matt Carlson4f125f42009-09-01 12:55:02 +00009869 err = tg3_request_irq(tp, 0);
Michael Chan79381092005-04-21 17:13:59 -07009870 if (err)
9871 return err;
9872
9873 /* Need to reset the chip because the MSI cycle may have terminated
9874 * with Master Abort.
9875 */
David S. Millerf47c11e2005-06-24 20:18:35 -07009876 tg3_full_lock(tp, 1);
Michael Chan79381092005-04-21 17:13:59 -07009877
Michael Chan944d9802005-05-29 14:57:48 -07009878 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07009879 err = tg3_init_hw(tp, 1);
Michael Chan79381092005-04-21 17:13:59 -07009880
David S. Millerf47c11e2005-06-24 20:18:35 -07009881 tg3_full_unlock(tp);
Michael Chan79381092005-04-21 17:13:59 -07009882
9883 if (err)
Matt Carlson4f125f42009-09-01 12:55:02 +00009884 free_irq(tp->napi[0].irq_vec, &tp->napi[0]);
Michael Chan79381092005-04-21 17:13:59 -07009885
9886 return err;
9887}
9888
Matt Carlson9e9fd122009-01-19 16:57:45 -08009889static int tg3_request_firmware(struct tg3 *tp)
9890{
9891 const __be32 *fw_data;
9892
9893 if (request_firmware(&tp->fw, tp->fw_needed, &tp->pdev->dev)) {
Joe Perches05dbe002010-02-17 19:44:19 +00009894 netdev_err(tp->dev, "Failed to load firmware \"%s\"\n",
9895 tp->fw_needed);
Matt Carlson9e9fd122009-01-19 16:57:45 -08009896 return -ENOENT;
9897 }
9898
9899 fw_data = (void *)tp->fw->data;
9900
9901 /* Firmware blob starts with version numbers, followed by
9902 * start address and _full_ length including BSS sections
9903 * (which must be longer than the actual data, of course
9904 */
9905
9906 tp->fw_len = be32_to_cpu(fw_data[2]); /* includes bss */
9907 if (tp->fw_len < (tp->fw->size - 12)) {
Joe Perches05dbe002010-02-17 19:44:19 +00009908 netdev_err(tp->dev, "bogus length %d in \"%s\"\n",
9909 tp->fw_len, tp->fw_needed);
Matt Carlson9e9fd122009-01-19 16:57:45 -08009910 release_firmware(tp->fw);
9911 tp->fw = NULL;
9912 return -EINVAL;
9913 }
9914
9915 /* We no longer need firmware; we have it. */
9916 tp->fw_needed = NULL;
9917 return 0;
9918}
9919
Matt Carlson679563f2009-09-01 12:55:46 +00009920static bool tg3_enable_msix(struct tg3 *tp)
9921{
Matt Carlsonc3b50032012-01-17 15:27:23 +00009922 int i, rc;
Matt Carlson679563f2009-09-01 12:55:46 +00009923 struct msix_entry msix_ent[tp->irq_max];
9924
Matt Carlsonc3b50032012-01-17 15:27:23 +00009925 tp->irq_cnt = num_online_cpus();
9926 if (tp->irq_cnt > 1) {
9927 /* We want as many rx rings enabled as there are cpus.
9928 * In multiqueue MSI-X mode, the first MSI-X vector
9929 * only deals with link interrupts, etc, so we add
9930 * one to the number of vectors we are requesting.
9931 */
9932 tp->irq_cnt = min_t(unsigned, tp->irq_cnt + 1, tp->irq_max);
9933 }
Matt Carlson679563f2009-09-01 12:55:46 +00009934
9935 for (i = 0; i < tp->irq_max; i++) {
9936 msix_ent[i].entry = i;
9937 msix_ent[i].vector = 0;
9938 }
9939
9940 rc = pci_enable_msix(tp->pdev, msix_ent, tp->irq_cnt);
Matt Carlson2430b032010-06-05 17:24:34 +00009941 if (rc < 0) {
9942 return false;
9943 } else if (rc != 0) {
Matt Carlson679563f2009-09-01 12:55:46 +00009944 if (pci_enable_msix(tp->pdev, msix_ent, rc))
9945 return false;
Joe Perches05dbe002010-02-17 19:44:19 +00009946 netdev_notice(tp->dev, "Requested %d MSI-X vectors, received %d\n",
9947 tp->irq_cnt, rc);
Matt Carlson679563f2009-09-01 12:55:46 +00009948 tp->irq_cnt = rc;
9949 }
9950
9951 for (i = 0; i < tp->irq_max; i++)
9952 tp->napi[i].irq_vec = msix_ent[i].vector;
9953
Ben Hutchings2ddaad32010-09-27 22:11:51 -07009954 netif_set_real_num_tx_queues(tp->dev, 1);
9955 rc = tp->irq_cnt > 1 ? tp->irq_cnt - 1 : 1;
9956 if (netif_set_real_num_rx_queues(tp->dev, rc)) {
9957 pci_disable_msix(tp->pdev);
9958 return false;
9959 }
Matt Carlsonb92b9042010-11-24 08:31:51 +00009960
9961 if (tp->irq_cnt > 1) {
Joe Perches63c3a662011-04-26 08:12:10 +00009962 tg3_flag_set(tp, ENABLE_RSS);
Matt Carlsond78b59f2011-04-05 14:22:46 +00009963
9964 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
9965 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
Joe Perches63c3a662011-04-26 08:12:10 +00009966 tg3_flag_set(tp, ENABLE_TSS);
Matt Carlsonb92b9042010-11-24 08:31:51 +00009967 netif_set_real_num_tx_queues(tp->dev, tp->irq_cnt - 1);
9968 }
9969 }
Matt Carlson2430b032010-06-05 17:24:34 +00009970
Matt Carlson679563f2009-09-01 12:55:46 +00009971 return true;
9972}
9973
Matt Carlson07b01732009-08-28 14:01:15 +00009974static void tg3_ints_init(struct tg3 *tp)
9975{
Joe Perches63c3a662011-04-26 08:12:10 +00009976 if ((tg3_flag(tp, SUPPORT_MSI) || tg3_flag(tp, SUPPORT_MSIX)) &&
9977 !tg3_flag(tp, TAGGED_STATUS)) {
Matt Carlson07b01732009-08-28 14:01:15 +00009978 /* All MSI supporting chips should support tagged
9979 * status. Assert that this is the case.
9980 */
Matt Carlson5129c3a2010-04-05 10:19:23 +00009981 netdev_warn(tp->dev,
9982 "MSI without TAGGED_STATUS? Not using MSI\n");
Matt Carlson679563f2009-09-01 12:55:46 +00009983 goto defcfg;
Matt Carlson07b01732009-08-28 14:01:15 +00009984 }
Matt Carlson4f125f42009-09-01 12:55:02 +00009985
Joe Perches63c3a662011-04-26 08:12:10 +00009986 if (tg3_flag(tp, SUPPORT_MSIX) && tg3_enable_msix(tp))
9987 tg3_flag_set(tp, USING_MSIX);
9988 else if (tg3_flag(tp, SUPPORT_MSI) && pci_enable_msi(tp->pdev) == 0)
9989 tg3_flag_set(tp, USING_MSI);
Matt Carlson679563f2009-09-01 12:55:46 +00009990
Joe Perches63c3a662011-04-26 08:12:10 +00009991 if (tg3_flag(tp, USING_MSI) || tg3_flag(tp, USING_MSIX)) {
Matt Carlson679563f2009-09-01 12:55:46 +00009992 u32 msi_mode = tr32(MSGINT_MODE);
Joe Perches63c3a662011-04-26 08:12:10 +00009993 if (tg3_flag(tp, USING_MSIX) && tp->irq_cnt > 1)
Matt Carlsonbaf8a942009-09-01 13:13:00 +00009994 msi_mode |= MSGINT_MODE_MULTIVEC_EN;
Matt Carlson5b39de92011-08-31 11:44:50 +00009995 if (!tg3_flag(tp, 1SHOT_MSI))
9996 msi_mode |= MSGINT_MODE_ONE_SHOT_DISABLE;
Matt Carlson679563f2009-09-01 12:55:46 +00009997 tw32(MSGINT_MODE, msi_mode | MSGINT_MODE_ENABLE);
9998 }
9999defcfg:
Joe Perches63c3a662011-04-26 08:12:10 +000010000 if (!tg3_flag(tp, USING_MSIX)) {
Matt Carlson679563f2009-09-01 12:55:46 +000010001 tp->irq_cnt = 1;
10002 tp->napi[0].irq_vec = tp->pdev->irq;
Ben Hutchings2ddaad32010-09-27 22:11:51 -070010003 netif_set_real_num_tx_queues(tp->dev, 1);
Matt Carlson85407882010-10-06 13:40:58 -070010004 netif_set_real_num_rx_queues(tp->dev, 1);
Matt Carlson679563f2009-09-01 12:55:46 +000010005 }
Matt Carlson07b01732009-08-28 14:01:15 +000010006}
10007
10008static void tg3_ints_fini(struct tg3 *tp)
10009{
Joe Perches63c3a662011-04-26 08:12:10 +000010010 if (tg3_flag(tp, USING_MSIX))
Matt Carlson679563f2009-09-01 12:55:46 +000010011 pci_disable_msix(tp->pdev);
Joe Perches63c3a662011-04-26 08:12:10 +000010012 else if (tg3_flag(tp, USING_MSI))
Matt Carlson679563f2009-09-01 12:55:46 +000010013 pci_disable_msi(tp->pdev);
Joe Perches63c3a662011-04-26 08:12:10 +000010014 tg3_flag_clear(tp, USING_MSI);
10015 tg3_flag_clear(tp, USING_MSIX);
10016 tg3_flag_clear(tp, ENABLE_RSS);
10017 tg3_flag_clear(tp, ENABLE_TSS);
Matt Carlson07b01732009-08-28 14:01:15 +000010018}
10019
Linus Torvalds1da177e2005-04-16 15:20:36 -070010020static int tg3_open(struct net_device *dev)
10021{
10022 struct tg3 *tp = netdev_priv(dev);
Matt Carlson4f125f42009-09-01 12:55:02 +000010023 int i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010024
Matt Carlson9e9fd122009-01-19 16:57:45 -080010025 if (tp->fw_needed) {
10026 err = tg3_request_firmware(tp);
10027 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0) {
10028 if (err)
10029 return err;
10030 } else if (err) {
Joe Perches05dbe002010-02-17 19:44:19 +000010031 netdev_warn(tp->dev, "TSO capability disabled\n");
Joe Perches63c3a662011-04-26 08:12:10 +000010032 tg3_flag_clear(tp, TSO_CAPABLE);
10033 } else if (!tg3_flag(tp, TSO_CAPABLE)) {
Joe Perches05dbe002010-02-17 19:44:19 +000010034 netdev_notice(tp->dev, "TSO capability restored\n");
Joe Perches63c3a662011-04-26 08:12:10 +000010035 tg3_flag_set(tp, TSO_CAPABLE);
Matt Carlson9e9fd122009-01-19 16:57:45 -080010036 }
10037 }
10038
Michael Chanc49a1562006-12-17 17:07:29 -080010039 netif_carrier_off(tp->dev);
10040
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000010041 err = tg3_power_up(tp);
Matt Carlson2f751b62008-08-04 23:17:34 -070010042 if (err)
Michael Chanbc1c7562006-03-20 17:48:03 -080010043 return err;
Matt Carlson2f751b62008-08-04 23:17:34 -070010044
10045 tg3_full_lock(tp, 0);
Michael Chanbc1c7562006-03-20 17:48:03 -080010046
Linus Torvalds1da177e2005-04-16 15:20:36 -070010047 tg3_disable_ints(tp);
Joe Perches63c3a662011-04-26 08:12:10 +000010048 tg3_flag_clear(tp, INIT_COMPLETE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010049
David S. Millerf47c11e2005-06-24 20:18:35 -070010050 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010051
Matt Carlson679563f2009-09-01 12:55:46 +000010052 /*
10053 * Setup interrupts first so we know how
10054 * many NAPI resources to allocate
10055 */
10056 tg3_ints_init(tp);
10057
Matt Carlson90415472011-12-16 13:33:23 +000010058 tg3_rss_check_indir_tbl(tp);
Matt Carlsonbcebcc42011-12-14 11:10:01 +000010059
Linus Torvalds1da177e2005-04-16 15:20:36 -070010060 /* The placement of this call is tied
10061 * to the setup and use of Host TX descriptors.
10062 */
10063 err = tg3_alloc_consistent(tp);
10064 if (err)
Matt Carlson679563f2009-09-01 12:55:46 +000010065 goto err_out1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010066
Matt Carlson66cfd1b2010-09-30 10:34:30 +000010067 tg3_napi_init(tp);
10068
Matt Carlsonfed97812009-09-01 13:10:19 +000010069 tg3_napi_enable(tp);
Stephen Hemmingerbea33482007-10-03 16:41:36 -070010070
Matt Carlson4f125f42009-09-01 12:55:02 +000010071 for (i = 0; i < tp->irq_cnt; i++) {
10072 struct tg3_napi *tnapi = &tp->napi[i];
10073 err = tg3_request_irq(tp, i);
10074 if (err) {
Matt Carlson5bc09182011-11-04 09:15:01 +000010075 for (i--; i >= 0; i--) {
10076 tnapi = &tp->napi[i];
Matt Carlson4f125f42009-09-01 12:55:02 +000010077 free_irq(tnapi->irq_vec, tnapi);
Matt Carlson5bc09182011-11-04 09:15:01 +000010078 }
10079 goto err_out2;
Matt Carlson4f125f42009-09-01 12:55:02 +000010080 }
10081 }
Matt Carlson07b01732009-08-28 14:01:15 +000010082
David S. Millerf47c11e2005-06-24 20:18:35 -070010083 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010084
Gary Zambrano8e7a22e2006-04-29 18:59:13 -070010085 err = tg3_init_hw(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010086 if (err) {
Michael Chan944d9802005-05-29 14:57:48 -070010087 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010088 tg3_free_rings(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010089 }
10090
David S. Millerf47c11e2005-06-24 20:18:35 -070010091 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010092
Matt Carlson07b01732009-08-28 14:01:15 +000010093 if (err)
Matt Carlson679563f2009-09-01 12:55:46 +000010094 goto err_out3;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010095
Joe Perches63c3a662011-04-26 08:12:10 +000010096 if (tg3_flag(tp, USING_MSI)) {
Michael Chan79381092005-04-21 17:13:59 -070010097 err = tg3_test_msi(tp);
David S. Millerfac9b832005-05-18 22:46:34 -070010098
Michael Chan79381092005-04-21 17:13:59 -070010099 if (err) {
David S. Millerf47c11e2005-06-24 20:18:35 -070010100 tg3_full_lock(tp, 0);
Michael Chan944d9802005-05-29 14:57:48 -070010101 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Michael Chan79381092005-04-21 17:13:59 -070010102 tg3_free_rings(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -070010103 tg3_full_unlock(tp);
Michael Chan79381092005-04-21 17:13:59 -070010104
Matt Carlson679563f2009-09-01 12:55:46 +000010105 goto err_out2;
Michael Chan79381092005-04-21 17:13:59 -070010106 }
Michael Chanfcfa0a32006-03-20 22:28:41 -080010107
Joe Perches63c3a662011-04-26 08:12:10 +000010108 if (!tg3_flag(tp, 57765_PLUS) && tg3_flag(tp, USING_MSI)) {
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000010109 u32 val = tr32(PCIE_TRANSACTION_CFG);
Michael Chanfcfa0a32006-03-20 22:28:41 -080010110
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000010111 tw32(PCIE_TRANSACTION_CFG,
10112 val | PCIE_TRANS_CFG_1SHOT_MSI);
Michael Chanfcfa0a32006-03-20 22:28:41 -080010113 }
Michael Chan79381092005-04-21 17:13:59 -070010114 }
10115
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010116 tg3_phy_start(tp);
10117
David S. Millerf47c11e2005-06-24 20:18:35 -070010118 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010119
Matt Carlson21f76382012-02-22 12:35:21 +000010120 tg3_timer_start(tp);
Joe Perches63c3a662011-04-26 08:12:10 +000010121 tg3_flag_set(tp, INIT_COMPLETE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010122 tg3_enable_ints(tp);
10123
David S. Millerf47c11e2005-06-24 20:18:35 -070010124 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010125
Matt Carlsonfe5f5782009-09-01 13:09:39 +000010126 netif_tx_start_all_queues(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010127
Mahesh Bandewar06c03c02011-05-08 06:51:48 +000010128 /*
10129 * Reset loopback feature if it was turned on while the device was down
10130 * make sure that it's installed properly now.
10131 */
10132 if (dev->features & NETIF_F_LOOPBACK)
10133 tg3_set_loopback(dev, dev->features);
10134
Linus Torvalds1da177e2005-04-16 15:20:36 -070010135 return 0;
Matt Carlson07b01732009-08-28 14:01:15 +000010136
Matt Carlson679563f2009-09-01 12:55:46 +000010137err_out3:
Matt Carlson4f125f42009-09-01 12:55:02 +000010138 for (i = tp->irq_cnt - 1; i >= 0; i--) {
10139 struct tg3_napi *tnapi = &tp->napi[i];
10140 free_irq(tnapi->irq_vec, tnapi);
10141 }
Matt Carlson07b01732009-08-28 14:01:15 +000010142
Matt Carlson679563f2009-09-01 12:55:46 +000010143err_out2:
Matt Carlsonfed97812009-09-01 13:10:19 +000010144 tg3_napi_disable(tp);
Matt Carlson66cfd1b2010-09-30 10:34:30 +000010145 tg3_napi_fini(tp);
Matt Carlson07b01732009-08-28 14:01:15 +000010146 tg3_free_consistent(tp);
Matt Carlson679563f2009-09-01 12:55:46 +000010147
10148err_out1:
10149 tg3_ints_fini(tp);
Matt Carlsoncd0d7222011-07-13 09:27:33 +000010150 tg3_frob_aux_power(tp, false);
10151 pci_set_power_state(tp->pdev, PCI_D3hot);
Matt Carlson07b01732009-08-28 14:01:15 +000010152 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010153}
10154
Linus Torvalds1da177e2005-04-16 15:20:36 -070010155static int tg3_close(struct net_device *dev)
10156{
Matt Carlson4f125f42009-09-01 12:55:02 +000010157 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010158 struct tg3 *tp = netdev_priv(dev);
10159
Matt Carlsonfed97812009-09-01 13:10:19 +000010160 tg3_napi_disable(tp);
Matt Carlsondb219972011-11-04 09:15:03 +000010161 tg3_reset_task_cancel(tp);
Michael Chan7faa0062006-02-02 17:29:28 -080010162
Matt Carlsonfe5f5782009-09-01 13:09:39 +000010163 netif_tx_stop_all_queues(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010164
Matt Carlson21f76382012-02-22 12:35:21 +000010165 tg3_timer_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010166
Matt Carlson24bb4fb2009-10-05 17:55:29 +000010167 tg3_phy_stop(tp);
10168
David S. Millerf47c11e2005-06-24 20:18:35 -070010169 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010170
10171 tg3_disable_ints(tp);
10172
Michael Chan944d9802005-05-29 14:57:48 -070010173 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010174 tg3_free_rings(tp);
Joe Perches63c3a662011-04-26 08:12:10 +000010175 tg3_flag_clear(tp, INIT_COMPLETE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010176
David S. Millerf47c11e2005-06-24 20:18:35 -070010177 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010178
Matt Carlson4f125f42009-09-01 12:55:02 +000010179 for (i = tp->irq_cnt - 1; i >= 0; i--) {
10180 struct tg3_napi *tnapi = &tp->napi[i];
10181 free_irq(tnapi->irq_vec, tnapi);
10182 }
Matt Carlson07b01732009-08-28 14:01:15 +000010183
10184 tg3_ints_fini(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010185
Matt Carlson92feeab2011-12-08 14:40:14 +000010186 /* Clear stats across close / open calls */
10187 memset(&tp->net_stats_prev, 0, sizeof(tp->net_stats_prev));
10188 memset(&tp->estats_prev, 0, sizeof(tp->estats_prev));
Linus Torvalds1da177e2005-04-16 15:20:36 -070010189
Matt Carlson66cfd1b2010-09-30 10:34:30 +000010190 tg3_napi_fini(tp);
10191
Linus Torvalds1da177e2005-04-16 15:20:36 -070010192 tg3_free_consistent(tp);
10193
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000010194 tg3_power_down(tp);
Michael Chanbc1c7562006-03-20 17:48:03 -080010195
10196 netif_carrier_off(tp->dev);
10197
Linus Torvalds1da177e2005-04-16 15:20:36 -070010198 return 0;
10199}
10200
Eric Dumazet511d2222010-07-07 20:44:24 +000010201static inline u64 get_stat64(tg3_stat64_t *val)
Stefan Buehler816f8b82008-08-15 14:10:54 -070010202{
10203 return ((u64)val->high << 32) | ((u64)val->low);
10204}
10205
Matt Carlson65ec6982012-02-28 23:33:37 +000010206static u64 tg3_calc_crc_errors(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010207{
10208 struct tg3_hw_stats *hw_stats = tp->hw_stats;
10209
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010210 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070010211 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
10212 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010213 u32 val;
10214
Michael Chan569a5df2007-02-13 12:18:15 -080010215 if (!tg3_readphy(tp, MII_TG3_TEST1, &val)) {
10216 tg3_writephy(tp, MII_TG3_TEST1,
10217 val | MII_TG3_TEST1_CRC_EN);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +000010218 tg3_readphy(tp, MII_TG3_RXR_COUNTERS, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010219 } else
10220 val = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010221
10222 tp->phy_crc_errors += val;
10223
10224 return tp->phy_crc_errors;
10225 }
10226
10227 return get_stat64(&hw_stats->rx_fcs_errors);
10228}
10229
10230#define ESTAT_ADD(member) \
10231 estats->member = old_estats->member + \
Eric Dumazet511d2222010-07-07 20:44:24 +000010232 get_stat64(&hw_stats->member)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010233
Matt Carlson65ec6982012-02-28 23:33:37 +000010234static void tg3_get_estats(struct tg3 *tp, struct tg3_ethtool_stats *estats)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010235{
Linus Torvalds1da177e2005-04-16 15:20:36 -070010236 struct tg3_ethtool_stats *old_estats = &tp->estats_prev;
10237 struct tg3_hw_stats *hw_stats = tp->hw_stats;
10238
Linus Torvalds1da177e2005-04-16 15:20:36 -070010239 ESTAT_ADD(rx_octets);
10240 ESTAT_ADD(rx_fragments);
10241 ESTAT_ADD(rx_ucast_packets);
10242 ESTAT_ADD(rx_mcast_packets);
10243 ESTAT_ADD(rx_bcast_packets);
10244 ESTAT_ADD(rx_fcs_errors);
10245 ESTAT_ADD(rx_align_errors);
10246 ESTAT_ADD(rx_xon_pause_rcvd);
10247 ESTAT_ADD(rx_xoff_pause_rcvd);
10248 ESTAT_ADD(rx_mac_ctrl_rcvd);
10249 ESTAT_ADD(rx_xoff_entered);
10250 ESTAT_ADD(rx_frame_too_long_errors);
10251 ESTAT_ADD(rx_jabbers);
10252 ESTAT_ADD(rx_undersize_packets);
10253 ESTAT_ADD(rx_in_length_errors);
10254 ESTAT_ADD(rx_out_length_errors);
10255 ESTAT_ADD(rx_64_or_less_octet_packets);
10256 ESTAT_ADD(rx_65_to_127_octet_packets);
10257 ESTAT_ADD(rx_128_to_255_octet_packets);
10258 ESTAT_ADD(rx_256_to_511_octet_packets);
10259 ESTAT_ADD(rx_512_to_1023_octet_packets);
10260 ESTAT_ADD(rx_1024_to_1522_octet_packets);
10261 ESTAT_ADD(rx_1523_to_2047_octet_packets);
10262 ESTAT_ADD(rx_2048_to_4095_octet_packets);
10263 ESTAT_ADD(rx_4096_to_8191_octet_packets);
10264 ESTAT_ADD(rx_8192_to_9022_octet_packets);
10265
10266 ESTAT_ADD(tx_octets);
10267 ESTAT_ADD(tx_collisions);
10268 ESTAT_ADD(tx_xon_sent);
10269 ESTAT_ADD(tx_xoff_sent);
10270 ESTAT_ADD(tx_flow_control);
10271 ESTAT_ADD(tx_mac_errors);
10272 ESTAT_ADD(tx_single_collisions);
10273 ESTAT_ADD(tx_mult_collisions);
10274 ESTAT_ADD(tx_deferred);
10275 ESTAT_ADD(tx_excessive_collisions);
10276 ESTAT_ADD(tx_late_collisions);
10277 ESTAT_ADD(tx_collide_2times);
10278 ESTAT_ADD(tx_collide_3times);
10279 ESTAT_ADD(tx_collide_4times);
10280 ESTAT_ADD(tx_collide_5times);
10281 ESTAT_ADD(tx_collide_6times);
10282 ESTAT_ADD(tx_collide_7times);
10283 ESTAT_ADD(tx_collide_8times);
10284 ESTAT_ADD(tx_collide_9times);
10285 ESTAT_ADD(tx_collide_10times);
10286 ESTAT_ADD(tx_collide_11times);
10287 ESTAT_ADD(tx_collide_12times);
10288 ESTAT_ADD(tx_collide_13times);
10289 ESTAT_ADD(tx_collide_14times);
10290 ESTAT_ADD(tx_collide_15times);
10291 ESTAT_ADD(tx_ucast_packets);
10292 ESTAT_ADD(tx_mcast_packets);
10293 ESTAT_ADD(tx_bcast_packets);
10294 ESTAT_ADD(tx_carrier_sense_errors);
10295 ESTAT_ADD(tx_discards);
10296 ESTAT_ADD(tx_errors);
10297
10298 ESTAT_ADD(dma_writeq_full);
10299 ESTAT_ADD(dma_write_prioq_full);
10300 ESTAT_ADD(rxbds_empty);
10301 ESTAT_ADD(rx_discards);
10302 ESTAT_ADD(rx_errors);
10303 ESTAT_ADD(rx_threshold_hit);
10304
10305 ESTAT_ADD(dma_readq_full);
10306 ESTAT_ADD(dma_read_prioq_full);
10307 ESTAT_ADD(tx_comp_queue_full);
10308
10309 ESTAT_ADD(ring_set_send_prod_index);
10310 ESTAT_ADD(ring_status_update);
10311 ESTAT_ADD(nic_irqs);
10312 ESTAT_ADD(nic_avoided_irqs);
10313 ESTAT_ADD(nic_tx_threshold_hit);
10314
Matt Carlson4452d092011-05-19 12:12:51 +000010315 ESTAT_ADD(mbuf_lwm_thresh_hit);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010316}
10317
Matt Carlson65ec6982012-02-28 23:33:37 +000010318static void tg3_get_nstats(struct tg3 *tp, struct rtnl_link_stats64 *stats)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010319{
Eric Dumazet511d2222010-07-07 20:44:24 +000010320 struct rtnl_link_stats64 *old_stats = &tp->net_stats_prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010321 struct tg3_hw_stats *hw_stats = tp->hw_stats;
10322
Linus Torvalds1da177e2005-04-16 15:20:36 -070010323 stats->rx_packets = old_stats->rx_packets +
10324 get_stat64(&hw_stats->rx_ucast_packets) +
10325 get_stat64(&hw_stats->rx_mcast_packets) +
10326 get_stat64(&hw_stats->rx_bcast_packets);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010327
Linus Torvalds1da177e2005-04-16 15:20:36 -070010328 stats->tx_packets = old_stats->tx_packets +
10329 get_stat64(&hw_stats->tx_ucast_packets) +
10330 get_stat64(&hw_stats->tx_mcast_packets) +
10331 get_stat64(&hw_stats->tx_bcast_packets);
10332
10333 stats->rx_bytes = old_stats->rx_bytes +
10334 get_stat64(&hw_stats->rx_octets);
10335 stats->tx_bytes = old_stats->tx_bytes +
10336 get_stat64(&hw_stats->tx_octets);
10337
10338 stats->rx_errors = old_stats->rx_errors +
John W. Linville4f63b872005-09-12 14:43:18 -070010339 get_stat64(&hw_stats->rx_errors);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010340 stats->tx_errors = old_stats->tx_errors +
10341 get_stat64(&hw_stats->tx_errors) +
10342 get_stat64(&hw_stats->tx_mac_errors) +
10343 get_stat64(&hw_stats->tx_carrier_sense_errors) +
10344 get_stat64(&hw_stats->tx_discards);
10345
10346 stats->multicast = old_stats->multicast +
10347 get_stat64(&hw_stats->rx_mcast_packets);
10348 stats->collisions = old_stats->collisions +
10349 get_stat64(&hw_stats->tx_collisions);
10350
10351 stats->rx_length_errors = old_stats->rx_length_errors +
10352 get_stat64(&hw_stats->rx_frame_too_long_errors) +
10353 get_stat64(&hw_stats->rx_undersize_packets);
10354
10355 stats->rx_over_errors = old_stats->rx_over_errors +
10356 get_stat64(&hw_stats->rxbds_empty);
10357 stats->rx_frame_errors = old_stats->rx_frame_errors +
10358 get_stat64(&hw_stats->rx_align_errors);
10359 stats->tx_aborted_errors = old_stats->tx_aborted_errors +
10360 get_stat64(&hw_stats->tx_discards);
10361 stats->tx_carrier_errors = old_stats->tx_carrier_errors +
10362 get_stat64(&hw_stats->tx_carrier_sense_errors);
10363
10364 stats->rx_crc_errors = old_stats->rx_crc_errors +
Matt Carlson65ec6982012-02-28 23:33:37 +000010365 tg3_calc_crc_errors(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010366
John W. Linville4f63b872005-09-12 14:43:18 -070010367 stats->rx_missed_errors = old_stats->rx_missed_errors +
10368 get_stat64(&hw_stats->rx_discards);
10369
Eric Dumazetb0057c52010-10-10 19:55:52 +000010370 stats->rx_dropped = tp->rx_dropped;
Eric Dumazet48855432011-10-24 07:53:03 +000010371 stats->tx_dropped = tp->tx_dropped;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010372}
10373
Linus Torvalds1da177e2005-04-16 15:20:36 -070010374static int tg3_get_regs_len(struct net_device *dev)
10375{
Matt Carlson97bd8e42011-04-13 11:05:04 +000010376 return TG3_REG_BLK_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010377}
10378
10379static void tg3_get_regs(struct net_device *dev,
10380 struct ethtool_regs *regs, void *_p)
10381{
Linus Torvalds1da177e2005-04-16 15:20:36 -070010382 struct tg3 *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010383
10384 regs->version = 0;
10385
Matt Carlson97bd8e42011-04-13 11:05:04 +000010386 memset(_p, 0, TG3_REG_BLK_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010387
Matt Carlson800960682010-08-02 11:26:06 +000010388 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Michael Chanbc1c7562006-03-20 17:48:03 -080010389 return;
10390
David S. Millerf47c11e2005-06-24 20:18:35 -070010391 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010392
Matt Carlson97bd8e42011-04-13 11:05:04 +000010393 tg3_dump_legacy_regs(tp, (u32 *)_p);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010394
David S. Millerf47c11e2005-06-24 20:18:35 -070010395 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010396}
10397
10398static int tg3_get_eeprom_len(struct net_device *dev)
10399{
10400 struct tg3 *tp = netdev_priv(dev);
10401
10402 return tp->nvram_size;
10403}
10404
Linus Torvalds1da177e2005-04-16 15:20:36 -070010405static int tg3_get_eeprom(struct net_device *dev, struct ethtool_eeprom *eeprom, u8 *data)
10406{
10407 struct tg3 *tp = netdev_priv(dev);
10408 int ret;
10409 u8 *pd;
Al Virob9fc7dc2007-12-17 22:59:57 -080010410 u32 i, offset, len, b_offset, b_count;
Matt Carlsona9dc5292009-02-25 14:25:30 +000010411 __be32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010412
Joe Perches63c3a662011-04-26 08:12:10 +000010413 if (tg3_flag(tp, NO_NVRAM))
Matt Carlsondf259d82009-04-20 06:57:14 +000010414 return -EINVAL;
10415
Matt Carlson800960682010-08-02 11:26:06 +000010416 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Michael Chanbc1c7562006-03-20 17:48:03 -080010417 return -EAGAIN;
10418
Linus Torvalds1da177e2005-04-16 15:20:36 -070010419 offset = eeprom->offset;
10420 len = eeprom->len;
10421 eeprom->len = 0;
10422
10423 eeprom->magic = TG3_EEPROM_MAGIC;
10424
10425 if (offset & 3) {
10426 /* adjustments to start on required 4 byte boundary */
10427 b_offset = offset & 3;
10428 b_count = 4 - b_offset;
10429 if (b_count > len) {
10430 /* i.e. offset=1 len=2 */
10431 b_count = len;
10432 }
Matt Carlsona9dc5292009-02-25 14:25:30 +000010433 ret = tg3_nvram_read_be32(tp, offset-b_offset, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010434 if (ret)
10435 return ret;
Matt Carlsonbe98da62010-07-11 09:31:46 +000010436 memcpy(data, ((char *)&val) + b_offset, b_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010437 len -= b_count;
10438 offset += b_count;
Matt Carlsonc6cdf432010-04-05 10:19:26 +000010439 eeprom->len += b_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010440 }
10441
Lucas De Marchi25985ed2011-03-30 22:57:33 -030010442 /* read bytes up to the last 4 byte boundary */
Linus Torvalds1da177e2005-04-16 15:20:36 -070010443 pd = &data[eeprom->len];
10444 for (i = 0; i < (len - (len & 3)); i += 4) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000010445 ret = tg3_nvram_read_be32(tp, offset + i, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010446 if (ret) {
10447 eeprom->len += i;
10448 return ret;
10449 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010450 memcpy(pd + i, &val, 4);
10451 }
10452 eeprom->len += i;
10453
10454 if (len & 3) {
10455 /* read last bytes not ending on 4 byte boundary */
10456 pd = &data[eeprom->len];
10457 b_count = len & 3;
10458 b_offset = offset + len - b_count;
Matt Carlsona9dc5292009-02-25 14:25:30 +000010459 ret = tg3_nvram_read_be32(tp, b_offset, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010460 if (ret)
10461 return ret;
Al Virob9fc7dc2007-12-17 22:59:57 -080010462 memcpy(pd, &val, b_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010463 eeprom->len += b_count;
10464 }
10465 return 0;
10466}
10467
Linus Torvalds1da177e2005-04-16 15:20:36 -070010468static int tg3_set_eeprom(struct net_device *dev, struct ethtool_eeprom *eeprom, u8 *data)
10469{
10470 struct tg3 *tp = netdev_priv(dev);
10471 int ret;
Al Virob9fc7dc2007-12-17 22:59:57 -080010472 u32 offset, len, b_offset, odd_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010473 u8 *buf;
Matt Carlsona9dc5292009-02-25 14:25:30 +000010474 __be32 start, end;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010475
Matt Carlson800960682010-08-02 11:26:06 +000010476 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Michael Chanbc1c7562006-03-20 17:48:03 -080010477 return -EAGAIN;
10478
Joe Perches63c3a662011-04-26 08:12:10 +000010479 if (tg3_flag(tp, NO_NVRAM) ||
Matt Carlsondf259d82009-04-20 06:57:14 +000010480 eeprom->magic != TG3_EEPROM_MAGIC)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010481 return -EINVAL;
10482
10483 offset = eeprom->offset;
10484 len = eeprom->len;
10485
10486 if ((b_offset = (offset & 3))) {
10487 /* adjustments to start on required 4 byte boundary */
Matt Carlsona9dc5292009-02-25 14:25:30 +000010488 ret = tg3_nvram_read_be32(tp, offset-b_offset, &start);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010489 if (ret)
10490 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010491 len += b_offset;
10492 offset &= ~3;
Michael Chan1c8594b2005-04-21 17:12:46 -070010493 if (len < 4)
10494 len = 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010495 }
10496
10497 odd_len = 0;
Michael Chan1c8594b2005-04-21 17:12:46 -070010498 if (len & 3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010499 /* adjustments to end on required 4 byte boundary */
10500 odd_len = 1;
10501 len = (len + 3) & ~3;
Matt Carlsona9dc5292009-02-25 14:25:30 +000010502 ret = tg3_nvram_read_be32(tp, offset+len-4, &end);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010503 if (ret)
10504 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010505 }
10506
10507 buf = data;
10508 if (b_offset || odd_len) {
10509 buf = kmalloc(len, GFP_KERNEL);
Andy Gospodarekab0049b2007-09-06 20:42:14 +010010510 if (!buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010511 return -ENOMEM;
10512 if (b_offset)
10513 memcpy(buf, &start, 4);
10514 if (odd_len)
10515 memcpy(buf+len-4, &end, 4);
10516 memcpy(buf + b_offset, data, eeprom->len);
10517 }
10518
10519 ret = tg3_nvram_write_block(tp, offset, len, buf);
10520
10521 if (buf != data)
10522 kfree(buf);
10523
10524 return ret;
10525}
10526
10527static int tg3_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
10528{
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010529 struct tg3 *tp = netdev_priv(dev);
10530
Joe Perches63c3a662011-04-26 08:12:10 +000010531 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000010532 struct phy_device *phydev;
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010533 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010534 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000010535 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
10536 return phy_ethtool_gset(phydev, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010537 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010538
Linus Torvalds1da177e2005-04-16 15:20:36 -070010539 cmd->supported = (SUPPORTED_Autoneg);
10540
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010541 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010542 cmd->supported |= (SUPPORTED_1000baseT_Half |
10543 SUPPORTED_1000baseT_Full);
10544
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010545 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010546 cmd->supported |= (SUPPORTED_100baseT_Half |
10547 SUPPORTED_100baseT_Full |
10548 SUPPORTED_10baseT_Half |
10549 SUPPORTED_10baseT_Full |
Matt Carlson3bebab52007-11-12 21:22:40 -080010550 SUPPORTED_TP);
Karsten Keilef348142006-05-12 12:49:08 -070010551 cmd->port = PORT_TP;
10552 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010553 cmd->supported |= SUPPORTED_FIBRE;
Karsten Keilef348142006-05-12 12:49:08 -070010554 cmd->port = PORT_FIBRE;
10555 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010556
Linus Torvalds1da177e2005-04-16 15:20:36 -070010557 cmd->advertising = tp->link_config.advertising;
Matt Carlson5bb09772011-06-13 13:39:00 +000010558 if (tg3_flag(tp, PAUSE_AUTONEG)) {
10559 if (tp->link_config.flowctrl & FLOW_CTRL_RX) {
10560 if (tp->link_config.flowctrl & FLOW_CTRL_TX) {
10561 cmd->advertising |= ADVERTISED_Pause;
10562 } else {
10563 cmd->advertising |= ADVERTISED_Pause |
10564 ADVERTISED_Asym_Pause;
10565 }
10566 } else if (tp->link_config.flowctrl & FLOW_CTRL_TX) {
10567 cmd->advertising |= ADVERTISED_Asym_Pause;
10568 }
10569 }
Matt Carlson859edb22011-12-08 14:40:16 +000010570 if (netif_running(dev) && netif_carrier_ok(dev)) {
David Decotigny70739492011-04-27 18:32:40 +000010571 ethtool_cmd_speed_set(cmd, tp->link_config.active_speed);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010572 cmd->duplex = tp->link_config.active_duplex;
Matt Carlson859edb22011-12-08 14:40:16 +000010573 cmd->lp_advertising = tp->link_config.rmt_adv;
Matt Carlsone348c5e2011-11-21 15:01:20 +000010574 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES)) {
10575 if (tp->phy_flags & TG3_PHYFLG_MDIX_STATE)
10576 cmd->eth_tp_mdix = ETH_TP_MDI_X;
10577 else
10578 cmd->eth_tp_mdix = ETH_TP_MDI;
10579 }
Matt Carlson64c22182010-10-14 10:37:44 +000010580 } else {
Matt Carlsone7405222012-02-13 15:20:16 +000010581 ethtool_cmd_speed_set(cmd, SPEED_UNKNOWN);
10582 cmd->duplex = DUPLEX_UNKNOWN;
Matt Carlsone348c5e2011-11-21 15:01:20 +000010583 cmd->eth_tp_mdix = ETH_TP_MDI_INVALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010584 }
Matt Carlson882e9792009-09-01 13:21:36 +000010585 cmd->phy_address = tp->phy_addr;
Matt Carlson7e5856b2009-02-25 14:23:01 +000010586 cmd->transceiver = XCVR_INTERNAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010587 cmd->autoneg = tp->link_config.autoneg;
10588 cmd->maxtxpkt = 0;
10589 cmd->maxrxpkt = 0;
10590 return 0;
10591}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010592
Linus Torvalds1da177e2005-04-16 15:20:36 -070010593static int tg3_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
10594{
10595 struct tg3 *tp = netdev_priv(dev);
David Decotigny25db0332011-04-27 18:32:39 +000010596 u32 speed = ethtool_cmd_speed(cmd);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010597
Joe Perches63c3a662011-04-26 08:12:10 +000010598 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000010599 struct phy_device *phydev;
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010600 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010601 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000010602 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
10603 return phy_ethtool_sset(phydev, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010604 }
10605
Matt Carlson7e5856b2009-02-25 14:23:01 +000010606 if (cmd->autoneg != AUTONEG_ENABLE &&
10607 cmd->autoneg != AUTONEG_DISABLE)
Michael Chan37ff2382005-10-26 15:49:51 -070010608 return -EINVAL;
Matt Carlson7e5856b2009-02-25 14:23:01 +000010609
10610 if (cmd->autoneg == AUTONEG_DISABLE &&
10611 cmd->duplex != DUPLEX_FULL &&
10612 cmd->duplex != DUPLEX_HALF)
Michael Chan37ff2382005-10-26 15:49:51 -070010613 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010614
Matt Carlson7e5856b2009-02-25 14:23:01 +000010615 if (cmd->autoneg == AUTONEG_ENABLE) {
10616 u32 mask = ADVERTISED_Autoneg |
10617 ADVERTISED_Pause |
10618 ADVERTISED_Asym_Pause;
10619
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010620 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
Matt Carlson7e5856b2009-02-25 14:23:01 +000010621 mask |= ADVERTISED_1000baseT_Half |
10622 ADVERTISED_1000baseT_Full;
10623
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010624 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
Matt Carlson7e5856b2009-02-25 14:23:01 +000010625 mask |= ADVERTISED_100baseT_Half |
10626 ADVERTISED_100baseT_Full |
10627 ADVERTISED_10baseT_Half |
10628 ADVERTISED_10baseT_Full |
10629 ADVERTISED_TP;
10630 else
10631 mask |= ADVERTISED_FIBRE;
10632
10633 if (cmd->advertising & ~mask)
10634 return -EINVAL;
10635
10636 mask &= (ADVERTISED_1000baseT_Half |
10637 ADVERTISED_1000baseT_Full |
10638 ADVERTISED_100baseT_Half |
10639 ADVERTISED_100baseT_Full |
10640 ADVERTISED_10baseT_Half |
10641 ADVERTISED_10baseT_Full);
10642
10643 cmd->advertising &= mask;
10644 } else {
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010645 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES) {
David Decotigny25db0332011-04-27 18:32:39 +000010646 if (speed != SPEED_1000)
Matt Carlson7e5856b2009-02-25 14:23:01 +000010647 return -EINVAL;
10648
10649 if (cmd->duplex != DUPLEX_FULL)
10650 return -EINVAL;
10651 } else {
David Decotigny25db0332011-04-27 18:32:39 +000010652 if (speed != SPEED_100 &&
10653 speed != SPEED_10)
Matt Carlson7e5856b2009-02-25 14:23:01 +000010654 return -EINVAL;
10655 }
10656 }
10657
David S. Millerf47c11e2005-06-24 20:18:35 -070010658 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010659
10660 tp->link_config.autoneg = cmd->autoneg;
10661 if (cmd->autoneg == AUTONEG_ENABLE) {
Andy Gospodarek405d8e52007-10-08 01:08:47 -070010662 tp->link_config.advertising = (cmd->advertising |
10663 ADVERTISED_Autoneg);
Matt Carlsone7405222012-02-13 15:20:16 +000010664 tp->link_config.speed = SPEED_UNKNOWN;
10665 tp->link_config.duplex = DUPLEX_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010666 } else {
10667 tp->link_config.advertising = 0;
David Decotigny25db0332011-04-27 18:32:39 +000010668 tp->link_config.speed = speed;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010669 tp->link_config.duplex = cmd->duplex;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010670 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010671
Linus Torvalds1da177e2005-04-16 15:20:36 -070010672 if (netif_running(dev))
10673 tg3_setup_phy(tp, 1);
10674
David S. Millerf47c11e2005-06-24 20:18:35 -070010675 tg3_full_unlock(tp);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010676
Linus Torvalds1da177e2005-04-16 15:20:36 -070010677 return 0;
10678}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010679
Linus Torvalds1da177e2005-04-16 15:20:36 -070010680static void tg3_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
10681{
10682 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010683
Rick Jones68aad782011-11-07 13:29:27 +000010684 strlcpy(info->driver, DRV_MODULE_NAME, sizeof(info->driver));
10685 strlcpy(info->version, DRV_MODULE_VERSION, sizeof(info->version));
10686 strlcpy(info->fw_version, tp->fw_ver, sizeof(info->fw_version));
10687 strlcpy(info->bus_info, pci_name(tp->pdev), sizeof(info->bus_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -070010688}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010689
Linus Torvalds1da177e2005-04-16 15:20:36 -070010690static void tg3_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
10691{
10692 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010693
Joe Perches63c3a662011-04-26 08:12:10 +000010694 if (tg3_flag(tp, WOL_CAP) && device_can_wakeup(&tp->pdev->dev))
Gary Zambranoa85feb82007-05-05 11:52:19 -070010695 wol->supported = WAKE_MAGIC;
10696 else
10697 wol->supported = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010698 wol->wolopts = 0;
Joe Perches63c3a662011-04-26 08:12:10 +000010699 if (tg3_flag(tp, WOL_ENABLE) && device_can_wakeup(&tp->pdev->dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010700 wol->wolopts = WAKE_MAGIC;
10701 memset(&wol->sopass, 0, sizeof(wol->sopass));
10702}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010703
Linus Torvalds1da177e2005-04-16 15:20:36 -070010704static int tg3_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
10705{
10706 struct tg3 *tp = netdev_priv(dev);
Rafael J. Wysocki12dac072008-07-30 16:37:33 -070010707 struct device *dp = &tp->pdev->dev;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010708
Linus Torvalds1da177e2005-04-16 15:20:36 -070010709 if (wol->wolopts & ~WAKE_MAGIC)
10710 return -EINVAL;
10711 if ((wol->wolopts & WAKE_MAGIC) &&
Joe Perches63c3a662011-04-26 08:12:10 +000010712 !(tg3_flag(tp, WOL_CAP) && device_can_wakeup(dp)))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010713 return -EINVAL;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010714
Rafael J. Wysockif2dc0d12010-10-25 13:01:55 +000010715 device_set_wakeup_enable(dp, wol->wolopts & WAKE_MAGIC);
10716
David S. Millerf47c11e2005-06-24 20:18:35 -070010717 spin_lock_bh(&tp->lock);
Rafael J. Wysockif2dc0d12010-10-25 13:01:55 +000010718 if (device_may_wakeup(dp))
Joe Perches63c3a662011-04-26 08:12:10 +000010719 tg3_flag_set(tp, WOL_ENABLE);
Rafael J. Wysockif2dc0d12010-10-25 13:01:55 +000010720 else
Joe Perches63c3a662011-04-26 08:12:10 +000010721 tg3_flag_clear(tp, WOL_ENABLE);
David S. Millerf47c11e2005-06-24 20:18:35 -070010722 spin_unlock_bh(&tp->lock);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010723
Linus Torvalds1da177e2005-04-16 15:20:36 -070010724 return 0;
10725}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010726
Linus Torvalds1da177e2005-04-16 15:20:36 -070010727static u32 tg3_get_msglevel(struct net_device *dev)
10728{
10729 struct tg3 *tp = netdev_priv(dev);
10730 return tp->msg_enable;
10731}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010732
Linus Torvalds1da177e2005-04-16 15:20:36 -070010733static void tg3_set_msglevel(struct net_device *dev, u32 value)
10734{
10735 struct tg3 *tp = netdev_priv(dev);
10736 tp->msg_enable = value;
10737}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010738
Linus Torvalds1da177e2005-04-16 15:20:36 -070010739static int tg3_nway_reset(struct net_device *dev)
10740{
10741 struct tg3 *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010742 int r;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010743
Linus Torvalds1da177e2005-04-16 15:20:36 -070010744 if (!netif_running(dev))
10745 return -EAGAIN;
10746
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010747 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Michael Chanc94e3942005-09-27 12:12:42 -070010748 return -EINVAL;
10749
Joe Perches63c3a662011-04-26 08:12:10 +000010750 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010751 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010752 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000010753 r = phy_start_aneg(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010754 } else {
10755 u32 bmcr;
10756
10757 spin_lock_bh(&tp->lock);
10758 r = -EINVAL;
10759 tg3_readphy(tp, MII_BMCR, &bmcr);
10760 if (!tg3_readphy(tp, MII_BMCR, &bmcr) &&
10761 ((bmcr & BMCR_ANENABLE) ||
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010762 (tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT))) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010763 tg3_writephy(tp, MII_BMCR, bmcr | BMCR_ANRESTART |
10764 BMCR_ANENABLE);
10765 r = 0;
10766 }
10767 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010768 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010769
Linus Torvalds1da177e2005-04-16 15:20:36 -070010770 return r;
10771}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010772
Linus Torvalds1da177e2005-04-16 15:20:36 -070010773static void tg3_get_ringparam(struct net_device *dev, struct ethtool_ringparam *ering)
10774{
10775 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010776
Matt Carlson2c49a442010-09-30 10:34:35 +000010777 ering->rx_max_pending = tp->rx_std_ring_mask;
Joe Perches63c3a662011-04-26 08:12:10 +000010778 if (tg3_flag(tp, JUMBO_RING_ENABLE))
Matt Carlson2c49a442010-09-30 10:34:35 +000010779 ering->rx_jumbo_max_pending = tp->rx_jmb_ring_mask;
Michael Chan4f81c322006-03-20 21:33:42 -080010780 else
10781 ering->rx_jumbo_max_pending = 0;
10782
10783 ering->tx_max_pending = TG3_TX_RING_SIZE - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010784
10785 ering->rx_pending = tp->rx_pending;
Joe Perches63c3a662011-04-26 08:12:10 +000010786 if (tg3_flag(tp, JUMBO_RING_ENABLE))
Michael Chan4f81c322006-03-20 21:33:42 -080010787 ering->rx_jumbo_pending = tp->rx_jumbo_pending;
10788 else
10789 ering->rx_jumbo_pending = 0;
10790
Matt Carlsonf3f3f272009-08-28 14:03:21 +000010791 ering->tx_pending = tp->napi[0].tx_pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010792}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010793
Linus Torvalds1da177e2005-04-16 15:20:36 -070010794static int tg3_set_ringparam(struct net_device *dev, struct ethtool_ringparam *ering)
10795{
10796 struct tg3 *tp = netdev_priv(dev);
Matt Carlson646c9ed2009-09-01 12:58:41 +000010797 int i, irq_sync = 0, err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010798
Matt Carlson2c49a442010-09-30 10:34:35 +000010799 if ((ering->rx_pending > tp->rx_std_ring_mask) ||
10800 (ering->rx_jumbo_pending > tp->rx_jmb_ring_mask) ||
Michael Chanbc3a9252006-10-18 20:55:18 -070010801 (ering->tx_pending > TG3_TX_RING_SIZE - 1) ||
10802 (ering->tx_pending <= MAX_SKB_FRAGS) ||
Joe Perches63c3a662011-04-26 08:12:10 +000010803 (tg3_flag(tp, TSO_BUG) &&
Michael Chanbc3a9252006-10-18 20:55:18 -070010804 (ering->tx_pending <= (MAX_SKB_FRAGS * 3))))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010805 return -EINVAL;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010806
Michael Chanbbe832c2005-06-24 20:20:04 -070010807 if (netif_running(dev)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010808 tg3_phy_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010809 tg3_netif_stop(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -070010810 irq_sync = 1;
10811 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010812
Michael Chanbbe832c2005-06-24 20:20:04 -070010813 tg3_full_lock(tp, irq_sync);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010814
Linus Torvalds1da177e2005-04-16 15:20:36 -070010815 tp->rx_pending = ering->rx_pending;
10816
Joe Perches63c3a662011-04-26 08:12:10 +000010817 if (tg3_flag(tp, MAX_RXPEND_64) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070010818 tp->rx_pending > 63)
10819 tp->rx_pending = 63;
10820 tp->rx_jumbo_pending = ering->rx_jumbo_pending;
Matt Carlson646c9ed2009-09-01 12:58:41 +000010821
Matt Carlson6fd45cb2010-09-15 08:59:57 +000010822 for (i = 0; i < tp->irq_max; i++)
Matt Carlson646c9ed2009-09-01 12:58:41 +000010823 tp->napi[i].tx_pending = ering->tx_pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010824
10825 if (netif_running(dev)) {
Michael Chan944d9802005-05-29 14:57:48 -070010826 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Michael Chanb9ec6c12006-07-25 16:37:27 -070010827 err = tg3_restart_hw(tp, 1);
10828 if (!err)
10829 tg3_netif_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010830 }
10831
David S. Millerf47c11e2005-06-24 20:18:35 -070010832 tg3_full_unlock(tp);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010833
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010834 if (irq_sync && !err)
10835 tg3_phy_start(tp);
10836
Michael Chanb9ec6c12006-07-25 16:37:27 -070010837 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010838}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010839
Linus Torvalds1da177e2005-04-16 15:20:36 -070010840static void tg3_get_pauseparam(struct net_device *dev, struct ethtool_pauseparam *epause)
10841{
10842 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010843
Joe Perches63c3a662011-04-26 08:12:10 +000010844 epause->autoneg = !!tg3_flag(tp, PAUSE_AUTONEG);
Matt Carlson8d018622007-12-20 20:05:44 -080010845
Matt Carlson4a2db502011-12-08 14:40:17 +000010846 if (tp->link_config.flowctrl & FLOW_CTRL_RX)
Matt Carlson8d018622007-12-20 20:05:44 -080010847 epause->rx_pause = 1;
10848 else
10849 epause->rx_pause = 0;
10850
Matt Carlson4a2db502011-12-08 14:40:17 +000010851 if (tp->link_config.flowctrl & FLOW_CTRL_TX)
Matt Carlson8d018622007-12-20 20:05:44 -080010852 epause->tx_pause = 1;
10853 else
10854 epause->tx_pause = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010855}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010856
Linus Torvalds1da177e2005-04-16 15:20:36 -070010857static int tg3_set_pauseparam(struct net_device *dev, struct ethtool_pauseparam *epause)
10858{
10859 struct tg3 *tp = netdev_priv(dev);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010860 int err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010861
Joe Perches63c3a662011-04-26 08:12:10 +000010862 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson27121682010-02-17 15:16:57 +000010863 u32 newadv;
10864 struct phy_device *phydev;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010865
Matt Carlson27121682010-02-17 15:16:57 +000010866 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010867
Matt Carlson27121682010-02-17 15:16:57 +000010868 if (!(phydev->supported & SUPPORTED_Pause) ||
10869 (!(phydev->supported & SUPPORTED_Asym_Pause) &&
Nicolas Kaiser2259dca2010-10-07 23:29:27 +000010870 (epause->rx_pause != epause->tx_pause)))
Matt Carlson27121682010-02-17 15:16:57 +000010871 return -EINVAL;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010872
Matt Carlson27121682010-02-17 15:16:57 +000010873 tp->link_config.flowctrl = 0;
10874 if (epause->rx_pause) {
10875 tp->link_config.flowctrl |= FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010876
Matt Carlson27121682010-02-17 15:16:57 +000010877 if (epause->tx_pause) {
Steve Glendinninge18ce342008-12-16 02:00:00 -080010878 tp->link_config.flowctrl |= FLOW_CTRL_TX;
Matt Carlson27121682010-02-17 15:16:57 +000010879 newadv = ADVERTISED_Pause;
10880 } else
10881 newadv = ADVERTISED_Pause |
10882 ADVERTISED_Asym_Pause;
10883 } else if (epause->tx_pause) {
10884 tp->link_config.flowctrl |= FLOW_CTRL_TX;
10885 newadv = ADVERTISED_Asym_Pause;
10886 } else
10887 newadv = 0;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010888
Matt Carlson27121682010-02-17 15:16:57 +000010889 if (epause->autoneg)
Joe Perches63c3a662011-04-26 08:12:10 +000010890 tg3_flag_set(tp, PAUSE_AUTONEG);
Matt Carlson27121682010-02-17 15:16:57 +000010891 else
Joe Perches63c3a662011-04-26 08:12:10 +000010892 tg3_flag_clear(tp, PAUSE_AUTONEG);
Matt Carlson27121682010-02-17 15:16:57 +000010893
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010894 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) {
Matt Carlson27121682010-02-17 15:16:57 +000010895 u32 oldadv = phydev->advertising &
10896 (ADVERTISED_Pause | ADVERTISED_Asym_Pause);
10897 if (oldadv != newadv) {
10898 phydev->advertising &=
10899 ~(ADVERTISED_Pause |
10900 ADVERTISED_Asym_Pause);
10901 phydev->advertising |= newadv;
10902 if (phydev->autoneg) {
10903 /*
10904 * Always renegotiate the link to
10905 * inform our link partner of our
10906 * flow control settings, even if the
10907 * flow control is forced. Let
10908 * tg3_adjust_link() do the final
10909 * flow control setup.
10910 */
10911 return phy_start_aneg(phydev);
10912 }
10913 }
10914
10915 if (!epause->autoneg)
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010916 tg3_setup_flow_control(tp, 0, 0);
Matt Carlson27121682010-02-17 15:16:57 +000010917 } else {
Matt Carlsonc6700ce2012-02-13 15:20:15 +000010918 tp->link_config.advertising &=
Matt Carlson27121682010-02-17 15:16:57 +000010919 ~(ADVERTISED_Pause |
10920 ADVERTISED_Asym_Pause);
Matt Carlsonc6700ce2012-02-13 15:20:15 +000010921 tp->link_config.advertising |= newadv;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010922 }
10923 } else {
10924 int irq_sync = 0;
10925
10926 if (netif_running(dev)) {
10927 tg3_netif_stop(tp);
10928 irq_sync = 1;
10929 }
10930
10931 tg3_full_lock(tp, irq_sync);
10932
10933 if (epause->autoneg)
Joe Perches63c3a662011-04-26 08:12:10 +000010934 tg3_flag_set(tp, PAUSE_AUTONEG);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010935 else
Joe Perches63c3a662011-04-26 08:12:10 +000010936 tg3_flag_clear(tp, PAUSE_AUTONEG);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010937 if (epause->rx_pause)
Steve Glendinninge18ce342008-12-16 02:00:00 -080010938 tp->link_config.flowctrl |= FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010939 else
Steve Glendinninge18ce342008-12-16 02:00:00 -080010940 tp->link_config.flowctrl &= ~FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010941 if (epause->tx_pause)
Steve Glendinninge18ce342008-12-16 02:00:00 -080010942 tp->link_config.flowctrl |= FLOW_CTRL_TX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010943 else
Steve Glendinninge18ce342008-12-16 02:00:00 -080010944 tp->link_config.flowctrl &= ~FLOW_CTRL_TX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010945
10946 if (netif_running(dev)) {
10947 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
10948 err = tg3_restart_hw(tp, 1);
10949 if (!err)
10950 tg3_netif_start(tp);
10951 }
10952
10953 tg3_full_unlock(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -070010954 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010955
Michael Chanb9ec6c12006-07-25 16:37:27 -070010956 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010957}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010958
Matt Carlsonde6f31e2010-04-12 06:58:30 +000010959static int tg3_get_sset_count(struct net_device *dev, int sset)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010960{
Jeff Garzikb9f2c042007-10-03 18:07:32 -070010961 switch (sset) {
10962 case ETH_SS_TEST:
10963 return TG3_NUM_TEST;
10964 case ETH_SS_STATS:
10965 return TG3_NUM_STATS;
10966 default:
10967 return -EOPNOTSUPP;
10968 }
Michael Chan4cafd3f2005-05-29 14:56:34 -070010969}
10970
Matt Carlson90415472011-12-16 13:33:23 +000010971static int tg3_get_rxnfc(struct net_device *dev, struct ethtool_rxnfc *info,
10972 u32 *rules __always_unused)
10973{
10974 struct tg3 *tp = netdev_priv(dev);
10975
10976 if (!tg3_flag(tp, SUPPORT_MSIX))
10977 return -EOPNOTSUPP;
10978
10979 switch (info->cmd) {
10980 case ETHTOOL_GRXRINGS:
10981 if (netif_running(tp->dev))
10982 info->data = tp->irq_cnt;
10983 else {
10984 info->data = num_online_cpus();
10985 if (info->data > TG3_IRQ_MAX_VECS_RSS)
10986 info->data = TG3_IRQ_MAX_VECS_RSS;
10987 }
10988
10989 /* The first interrupt vector only
10990 * handles link interrupts.
10991 */
10992 info->data -= 1;
10993 return 0;
10994
10995 default:
10996 return -EOPNOTSUPP;
10997 }
10998}
10999
11000static u32 tg3_get_rxfh_indir_size(struct net_device *dev)
11001{
11002 u32 size = 0;
11003 struct tg3 *tp = netdev_priv(dev);
11004
11005 if (tg3_flag(tp, SUPPORT_MSIX))
11006 size = TG3_RSS_INDIR_TBL_SIZE;
11007
11008 return size;
11009}
11010
11011static int tg3_get_rxfh_indir(struct net_device *dev, u32 *indir)
11012{
11013 struct tg3 *tp = netdev_priv(dev);
11014 int i;
11015
11016 for (i = 0; i < TG3_RSS_INDIR_TBL_SIZE; i++)
11017 indir[i] = tp->rss_ind_tbl[i];
11018
11019 return 0;
11020}
11021
11022static int tg3_set_rxfh_indir(struct net_device *dev, const u32 *indir)
11023{
11024 struct tg3 *tp = netdev_priv(dev);
11025 size_t i;
11026
11027 for (i = 0; i < TG3_RSS_INDIR_TBL_SIZE; i++)
11028 tp->rss_ind_tbl[i] = indir[i];
11029
11030 if (!netif_running(dev) || !tg3_flag(tp, ENABLE_RSS))
11031 return 0;
11032
11033 /* It is legal to write the indirection
11034 * table while the device is running.
11035 */
11036 tg3_full_lock(tp, 0);
11037 tg3_rss_write_indir_tbl(tp);
11038 tg3_full_unlock(tp);
11039
11040 return 0;
11041}
11042
Matt Carlsonde6f31e2010-04-12 06:58:30 +000011043static void tg3_get_strings(struct net_device *dev, u32 stringset, u8 *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011044{
11045 switch (stringset) {
11046 case ETH_SS_STATS:
11047 memcpy(buf, &ethtool_stats_keys, sizeof(ethtool_stats_keys));
11048 break;
Michael Chan4cafd3f2005-05-29 14:56:34 -070011049 case ETH_SS_TEST:
11050 memcpy(buf, &ethtool_test_keys, sizeof(ethtool_test_keys));
11051 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011052 default:
11053 WARN_ON(1); /* we need a WARN() */
11054 break;
11055 }
11056}
11057
stephen hemminger81b87092011-04-04 08:43:50 +000011058static int tg3_set_phys_id(struct net_device *dev,
11059 enum ethtool_phys_id_state state)
Michael Chan4009a932005-09-05 17:52:54 -070011060{
11061 struct tg3 *tp = netdev_priv(dev);
Michael Chan4009a932005-09-05 17:52:54 -070011062
11063 if (!netif_running(tp->dev))
11064 return -EAGAIN;
11065
stephen hemminger81b87092011-04-04 08:43:50 +000011066 switch (state) {
11067 case ETHTOOL_ID_ACTIVE:
Allan, Bruce Wfce55922011-04-13 13:09:10 +000011068 return 1; /* cycle on/off once per second */
Michael Chan4009a932005-09-05 17:52:54 -070011069
stephen hemminger81b87092011-04-04 08:43:50 +000011070 case ETHTOOL_ID_ON:
11071 tw32(MAC_LED_CTRL, LED_CTRL_LNKLED_OVERRIDE |
11072 LED_CTRL_1000MBPS_ON |
11073 LED_CTRL_100MBPS_ON |
11074 LED_CTRL_10MBPS_ON |
11075 LED_CTRL_TRAFFIC_OVERRIDE |
11076 LED_CTRL_TRAFFIC_BLINK |
11077 LED_CTRL_TRAFFIC_LED);
11078 break;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011079
stephen hemminger81b87092011-04-04 08:43:50 +000011080 case ETHTOOL_ID_OFF:
11081 tw32(MAC_LED_CTRL, LED_CTRL_LNKLED_OVERRIDE |
11082 LED_CTRL_TRAFFIC_OVERRIDE);
11083 break;
Michael Chan4009a932005-09-05 17:52:54 -070011084
stephen hemminger81b87092011-04-04 08:43:50 +000011085 case ETHTOOL_ID_INACTIVE:
11086 tw32(MAC_LED_CTRL, tp->led_ctrl);
11087 break;
Michael Chan4009a932005-09-05 17:52:54 -070011088 }
stephen hemminger81b87092011-04-04 08:43:50 +000011089
Michael Chan4009a932005-09-05 17:52:54 -070011090 return 0;
11091}
11092
Matt Carlsonde6f31e2010-04-12 06:58:30 +000011093static void tg3_get_ethtool_stats(struct net_device *dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -070011094 struct ethtool_stats *estats, u64 *tmp_stats)
11095{
11096 struct tg3 *tp = netdev_priv(dev);
Matt Carlson0e6c9da2011-12-08 14:40:13 +000011097
Matt Carlsonb546e462012-02-13 15:20:09 +000011098 if (tp->hw_stats)
11099 tg3_get_estats(tp, (struct tg3_ethtool_stats *)tmp_stats);
11100 else
11101 memset(tmp_stats, 0, sizeof(struct tg3_ethtool_stats));
Linus Torvalds1da177e2005-04-16 15:20:36 -070011102}
11103
Matt Carlson535a4902011-07-20 10:20:56 +000011104static __be32 *tg3_vpd_readblock(struct tg3 *tp, u32 *vpdlen)
Matt Carlsonc3e94502011-04-13 11:05:08 +000011105{
11106 int i;
11107 __be32 *buf;
11108 u32 offset = 0, len = 0;
11109 u32 magic, val;
11110
Joe Perches63c3a662011-04-26 08:12:10 +000011111 if (tg3_flag(tp, NO_NVRAM) || tg3_nvram_read(tp, 0, &magic))
Matt Carlsonc3e94502011-04-13 11:05:08 +000011112 return NULL;
11113
11114 if (magic == TG3_EEPROM_MAGIC) {
11115 for (offset = TG3_NVM_DIR_START;
11116 offset < TG3_NVM_DIR_END;
11117 offset += TG3_NVM_DIRENT_SIZE) {
11118 if (tg3_nvram_read(tp, offset, &val))
11119 return NULL;
11120
11121 if ((val >> TG3_NVM_DIRTYPE_SHIFT) ==
11122 TG3_NVM_DIRTYPE_EXTVPD)
11123 break;
11124 }
11125
11126 if (offset != TG3_NVM_DIR_END) {
11127 len = (val & TG3_NVM_DIRTYPE_LENMSK) * 4;
11128 if (tg3_nvram_read(tp, offset + 4, &offset))
11129 return NULL;
11130
11131 offset = tg3_nvram_logical_addr(tp, offset);
11132 }
11133 }
11134
11135 if (!offset || !len) {
11136 offset = TG3_NVM_VPD_OFF;
11137 len = TG3_NVM_VPD_LEN;
11138 }
11139
11140 buf = kmalloc(len, GFP_KERNEL);
11141 if (buf == NULL)
11142 return NULL;
11143
11144 if (magic == TG3_EEPROM_MAGIC) {
11145 for (i = 0; i < len; i += 4) {
11146 /* The data is in little-endian format in NVRAM.
11147 * Use the big-endian read routines to preserve
11148 * the byte order as it exists in NVRAM.
11149 */
11150 if (tg3_nvram_read_be32(tp, offset + i, &buf[i/4]))
11151 goto error;
11152 }
11153 } else {
11154 u8 *ptr;
11155 ssize_t cnt;
11156 unsigned int pos = 0;
11157
11158 ptr = (u8 *)&buf[0];
11159 for (i = 0; pos < len && i < 3; i++, pos += cnt, ptr += cnt) {
11160 cnt = pci_read_vpd(tp->pdev, pos,
11161 len - pos, ptr);
11162 if (cnt == -ETIMEDOUT || cnt == -EINTR)
11163 cnt = 0;
11164 else if (cnt < 0)
11165 goto error;
11166 }
11167 if (pos != len)
11168 goto error;
11169 }
11170
Matt Carlson535a4902011-07-20 10:20:56 +000011171 *vpdlen = len;
11172
Matt Carlsonc3e94502011-04-13 11:05:08 +000011173 return buf;
11174
11175error:
11176 kfree(buf);
11177 return NULL;
11178}
11179
Michael Chan566f86a2005-05-29 14:56:58 -070011180#define NVRAM_TEST_SIZE 0x100
Matt Carlsona5767de2007-11-12 21:10:58 -080011181#define NVRAM_SELFBOOT_FORMAT1_0_SIZE 0x14
11182#define NVRAM_SELFBOOT_FORMAT1_2_SIZE 0x18
11183#define NVRAM_SELFBOOT_FORMAT1_3_SIZE 0x1c
Matt Carlson727a6d92011-06-13 13:38:58 +000011184#define NVRAM_SELFBOOT_FORMAT1_4_SIZE 0x20
11185#define NVRAM_SELFBOOT_FORMAT1_5_SIZE 0x24
Matt Carlsonbda18fa2011-07-20 10:20:57 +000011186#define NVRAM_SELFBOOT_FORMAT1_6_SIZE 0x50
Michael Chanb16250e2006-09-27 16:10:14 -070011187#define NVRAM_SELFBOOT_HW_SIZE 0x20
11188#define NVRAM_SELFBOOT_DATA_SIZE 0x1c
Michael Chan566f86a2005-05-29 14:56:58 -070011189
11190static int tg3_test_nvram(struct tg3 *tp)
11191{
Matt Carlson535a4902011-07-20 10:20:56 +000011192 u32 csum, magic, len;
Matt Carlsona9dc5292009-02-25 14:25:30 +000011193 __be32 *buf;
Andy Gospodarekab0049b2007-09-06 20:42:14 +010011194 int i, j, k, err = 0, size;
Michael Chan566f86a2005-05-29 14:56:58 -070011195
Joe Perches63c3a662011-04-26 08:12:10 +000011196 if (tg3_flag(tp, NO_NVRAM))
Matt Carlsondf259d82009-04-20 06:57:14 +000011197 return 0;
11198
Matt Carlsone4f34112009-02-25 14:25:00 +000011199 if (tg3_nvram_read(tp, 0, &magic) != 0)
Michael Chan1b277772006-03-20 22:27:48 -080011200 return -EIO;
11201
Michael Chan1b277772006-03-20 22:27:48 -080011202 if (magic == TG3_EEPROM_MAGIC)
11203 size = NVRAM_TEST_SIZE;
Michael Chanb16250e2006-09-27 16:10:14 -070011204 else if ((magic & TG3_EEPROM_MAGIC_FW_MSK) == TG3_EEPROM_MAGIC_FW) {
Matt Carlsona5767de2007-11-12 21:10:58 -080011205 if ((magic & TG3_EEPROM_SB_FORMAT_MASK) ==
11206 TG3_EEPROM_SB_FORMAT_1) {
11207 switch (magic & TG3_EEPROM_SB_REVISION_MASK) {
11208 case TG3_EEPROM_SB_REVISION_0:
11209 size = NVRAM_SELFBOOT_FORMAT1_0_SIZE;
11210 break;
11211 case TG3_EEPROM_SB_REVISION_2:
11212 size = NVRAM_SELFBOOT_FORMAT1_2_SIZE;
11213 break;
11214 case TG3_EEPROM_SB_REVISION_3:
11215 size = NVRAM_SELFBOOT_FORMAT1_3_SIZE;
11216 break;
Matt Carlson727a6d92011-06-13 13:38:58 +000011217 case TG3_EEPROM_SB_REVISION_4:
11218 size = NVRAM_SELFBOOT_FORMAT1_4_SIZE;
11219 break;
11220 case TG3_EEPROM_SB_REVISION_5:
11221 size = NVRAM_SELFBOOT_FORMAT1_5_SIZE;
11222 break;
11223 case TG3_EEPROM_SB_REVISION_6:
11224 size = NVRAM_SELFBOOT_FORMAT1_6_SIZE;
11225 break;
Matt Carlsona5767de2007-11-12 21:10:58 -080011226 default:
Matt Carlson727a6d92011-06-13 13:38:58 +000011227 return -EIO;
Matt Carlsona5767de2007-11-12 21:10:58 -080011228 }
11229 } else
Michael Chan1b277772006-03-20 22:27:48 -080011230 return 0;
Michael Chanb16250e2006-09-27 16:10:14 -070011231 } else if ((magic & TG3_EEPROM_MAGIC_HW_MSK) == TG3_EEPROM_MAGIC_HW)
11232 size = NVRAM_SELFBOOT_HW_SIZE;
11233 else
Michael Chan1b277772006-03-20 22:27:48 -080011234 return -EIO;
11235
11236 buf = kmalloc(size, GFP_KERNEL);
Michael Chan566f86a2005-05-29 14:56:58 -070011237 if (buf == NULL)
11238 return -ENOMEM;
11239
Michael Chan1b277772006-03-20 22:27:48 -080011240 err = -EIO;
11241 for (i = 0, j = 0; i < size; i += 4, j++) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000011242 err = tg3_nvram_read_be32(tp, i, &buf[j]);
11243 if (err)
Michael Chan566f86a2005-05-29 14:56:58 -070011244 break;
Michael Chan566f86a2005-05-29 14:56:58 -070011245 }
Michael Chan1b277772006-03-20 22:27:48 -080011246 if (i < size)
Michael Chan566f86a2005-05-29 14:56:58 -070011247 goto out;
11248
Michael Chan1b277772006-03-20 22:27:48 -080011249 /* Selfboot format */
Matt Carlsona9dc5292009-02-25 14:25:30 +000011250 magic = be32_to_cpu(buf[0]);
Al Virob9fc7dc2007-12-17 22:59:57 -080011251 if ((magic & TG3_EEPROM_MAGIC_FW_MSK) ==
Michael Chanb16250e2006-09-27 16:10:14 -070011252 TG3_EEPROM_MAGIC_FW) {
Michael Chan1b277772006-03-20 22:27:48 -080011253 u8 *buf8 = (u8 *) buf, csum8 = 0;
11254
Al Virob9fc7dc2007-12-17 22:59:57 -080011255 if ((magic & TG3_EEPROM_SB_REVISION_MASK) ==
Matt Carlsona5767de2007-11-12 21:10:58 -080011256 TG3_EEPROM_SB_REVISION_2) {
11257 /* For rev 2, the csum doesn't include the MBA. */
11258 for (i = 0; i < TG3_EEPROM_SB_F1R2_MBA_OFF; i++)
11259 csum8 += buf8[i];
11260 for (i = TG3_EEPROM_SB_F1R2_MBA_OFF + 4; i < size; i++)
11261 csum8 += buf8[i];
11262 } else {
11263 for (i = 0; i < size; i++)
11264 csum8 += buf8[i];
11265 }
Michael Chan1b277772006-03-20 22:27:48 -080011266
Adrian Bunkad96b482006-04-05 22:21:04 -070011267 if (csum8 == 0) {
11268 err = 0;
11269 goto out;
11270 }
11271
11272 err = -EIO;
11273 goto out;
Michael Chan1b277772006-03-20 22:27:48 -080011274 }
Michael Chan566f86a2005-05-29 14:56:58 -070011275
Al Virob9fc7dc2007-12-17 22:59:57 -080011276 if ((magic & TG3_EEPROM_MAGIC_HW_MSK) ==
Michael Chanb16250e2006-09-27 16:10:14 -070011277 TG3_EEPROM_MAGIC_HW) {
11278 u8 data[NVRAM_SELFBOOT_DATA_SIZE];
Matt Carlsona9dc5292009-02-25 14:25:30 +000011279 u8 parity[NVRAM_SELFBOOT_DATA_SIZE];
Michael Chanb16250e2006-09-27 16:10:14 -070011280 u8 *buf8 = (u8 *) buf;
Michael Chanb16250e2006-09-27 16:10:14 -070011281
11282 /* Separate the parity bits and the data bytes. */
11283 for (i = 0, j = 0, k = 0; i < NVRAM_SELFBOOT_HW_SIZE; i++) {
11284 if ((i == 0) || (i == 8)) {
11285 int l;
11286 u8 msk;
11287
11288 for (l = 0, msk = 0x80; l < 7; l++, msk >>= 1)
11289 parity[k++] = buf8[i] & msk;
11290 i++;
Matt Carlson859a588792010-04-05 10:19:28 +000011291 } else if (i == 16) {
Michael Chanb16250e2006-09-27 16:10:14 -070011292 int l;
11293 u8 msk;
11294
11295 for (l = 0, msk = 0x20; l < 6; l++, msk >>= 1)
11296 parity[k++] = buf8[i] & msk;
11297 i++;
11298
11299 for (l = 0, msk = 0x80; l < 8; l++, msk >>= 1)
11300 parity[k++] = buf8[i] & msk;
11301 i++;
11302 }
11303 data[j++] = buf8[i];
11304 }
11305
11306 err = -EIO;
11307 for (i = 0; i < NVRAM_SELFBOOT_DATA_SIZE; i++) {
11308 u8 hw8 = hweight8(data[i]);
11309
11310 if ((hw8 & 0x1) && parity[i])
11311 goto out;
11312 else if (!(hw8 & 0x1) && !parity[i])
11313 goto out;
11314 }
11315 err = 0;
11316 goto out;
11317 }
11318
Matt Carlson01c3a392011-03-09 16:58:20 +000011319 err = -EIO;
11320
Michael Chan566f86a2005-05-29 14:56:58 -070011321 /* Bootstrap checksum at offset 0x10 */
11322 csum = calc_crc((unsigned char *) buf, 0x10);
Matt Carlson01c3a392011-03-09 16:58:20 +000011323 if (csum != le32_to_cpu(buf[0x10/4]))
Michael Chan566f86a2005-05-29 14:56:58 -070011324 goto out;
11325
11326 /* Manufacturing block starts at offset 0x74, checksum at 0xfc */
11327 csum = calc_crc((unsigned char *) &buf[0x74/4], 0x88);
Matt Carlson01c3a392011-03-09 16:58:20 +000011328 if (csum != le32_to_cpu(buf[0xfc/4]))
Matt Carlsona9dc5292009-02-25 14:25:30 +000011329 goto out;
Michael Chan566f86a2005-05-29 14:56:58 -070011330
Matt Carlsonc3e94502011-04-13 11:05:08 +000011331 kfree(buf);
11332
Matt Carlson535a4902011-07-20 10:20:56 +000011333 buf = tg3_vpd_readblock(tp, &len);
Matt Carlsonc3e94502011-04-13 11:05:08 +000011334 if (!buf)
11335 return -ENOMEM;
Matt Carlsond4894f32011-03-09 16:58:21 +000011336
Matt Carlson535a4902011-07-20 10:20:56 +000011337 i = pci_vpd_find_tag((u8 *)buf, 0, len, PCI_VPD_LRDT_RO_DATA);
Matt Carlsond4894f32011-03-09 16:58:21 +000011338 if (i > 0) {
11339 j = pci_vpd_lrdt_size(&((u8 *)buf)[i]);
11340 if (j < 0)
11341 goto out;
11342
Matt Carlson535a4902011-07-20 10:20:56 +000011343 if (i + PCI_VPD_LRDT_TAG_SIZE + j > len)
Matt Carlsond4894f32011-03-09 16:58:21 +000011344 goto out;
11345
11346 i += PCI_VPD_LRDT_TAG_SIZE;
11347 j = pci_vpd_find_info_keyword((u8 *)buf, i, j,
11348 PCI_VPD_RO_KEYWORD_CHKSUM);
11349 if (j > 0) {
11350 u8 csum8 = 0;
11351
11352 j += PCI_VPD_INFO_FLD_HDR_SIZE;
11353
11354 for (i = 0; i <= j; i++)
11355 csum8 += ((u8 *)buf)[i];
11356
11357 if (csum8)
11358 goto out;
11359 }
11360 }
11361
Michael Chan566f86a2005-05-29 14:56:58 -070011362 err = 0;
11363
11364out:
11365 kfree(buf);
11366 return err;
11367}
11368
Michael Chanca430072005-05-29 14:57:23 -070011369#define TG3_SERDES_TIMEOUT_SEC 2
11370#define TG3_COPPER_TIMEOUT_SEC 6
11371
11372static int tg3_test_link(struct tg3 *tp)
11373{
11374 int i, max;
11375
11376 if (!netif_running(tp->dev))
11377 return -ENODEV;
11378
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011379 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
Michael Chanca430072005-05-29 14:57:23 -070011380 max = TG3_SERDES_TIMEOUT_SEC;
11381 else
11382 max = TG3_COPPER_TIMEOUT_SEC;
11383
11384 for (i = 0; i < max; i++) {
11385 if (netif_carrier_ok(tp->dev))
11386 return 0;
11387
11388 if (msleep_interruptible(1000))
11389 break;
11390 }
11391
11392 return -EIO;
11393}
11394
Michael Chana71116d2005-05-29 14:58:11 -070011395/* Only test the commonly used registers */
David S. Miller30ca3e32006-03-20 23:02:36 -080011396static int tg3_test_registers(struct tg3 *tp)
Michael Chana71116d2005-05-29 14:58:11 -070011397{
Michael Chanb16250e2006-09-27 16:10:14 -070011398 int i, is_5705, is_5750;
Michael Chana71116d2005-05-29 14:58:11 -070011399 u32 offset, read_mask, write_mask, val, save_val, read_val;
11400 static struct {
11401 u16 offset;
11402 u16 flags;
11403#define TG3_FL_5705 0x1
11404#define TG3_FL_NOT_5705 0x2
11405#define TG3_FL_NOT_5788 0x4
Michael Chanb16250e2006-09-27 16:10:14 -070011406#define TG3_FL_NOT_5750 0x8
Michael Chana71116d2005-05-29 14:58:11 -070011407 u32 read_mask;
11408 u32 write_mask;
11409 } reg_tbl[] = {
11410 /* MAC Control Registers */
11411 { MAC_MODE, TG3_FL_NOT_5705,
11412 0x00000000, 0x00ef6f8c },
11413 { MAC_MODE, TG3_FL_5705,
11414 0x00000000, 0x01ef6b8c },
11415 { MAC_STATUS, TG3_FL_NOT_5705,
11416 0x03800107, 0x00000000 },
11417 { MAC_STATUS, TG3_FL_5705,
11418 0x03800100, 0x00000000 },
11419 { MAC_ADDR_0_HIGH, 0x0000,
11420 0x00000000, 0x0000ffff },
11421 { MAC_ADDR_0_LOW, 0x0000,
Matt Carlsonc6cdf432010-04-05 10:19:26 +000011422 0x00000000, 0xffffffff },
Michael Chana71116d2005-05-29 14:58:11 -070011423 { MAC_RX_MTU_SIZE, 0x0000,
11424 0x00000000, 0x0000ffff },
11425 { MAC_TX_MODE, 0x0000,
11426 0x00000000, 0x00000070 },
11427 { MAC_TX_LENGTHS, 0x0000,
11428 0x00000000, 0x00003fff },
11429 { MAC_RX_MODE, TG3_FL_NOT_5705,
11430 0x00000000, 0x000007fc },
11431 { MAC_RX_MODE, TG3_FL_5705,
11432 0x00000000, 0x000007dc },
11433 { MAC_HASH_REG_0, 0x0000,
11434 0x00000000, 0xffffffff },
11435 { MAC_HASH_REG_1, 0x0000,
11436 0x00000000, 0xffffffff },
11437 { MAC_HASH_REG_2, 0x0000,
11438 0x00000000, 0xffffffff },
11439 { MAC_HASH_REG_3, 0x0000,
11440 0x00000000, 0xffffffff },
11441
11442 /* Receive Data and Receive BD Initiator Control Registers. */
11443 { RCVDBDI_JUMBO_BD+0, TG3_FL_NOT_5705,
11444 0x00000000, 0xffffffff },
11445 { RCVDBDI_JUMBO_BD+4, TG3_FL_NOT_5705,
11446 0x00000000, 0xffffffff },
11447 { RCVDBDI_JUMBO_BD+8, TG3_FL_NOT_5705,
11448 0x00000000, 0x00000003 },
11449 { RCVDBDI_JUMBO_BD+0xc, TG3_FL_NOT_5705,
11450 0x00000000, 0xffffffff },
11451 { RCVDBDI_STD_BD+0, 0x0000,
11452 0x00000000, 0xffffffff },
11453 { RCVDBDI_STD_BD+4, 0x0000,
11454 0x00000000, 0xffffffff },
11455 { RCVDBDI_STD_BD+8, 0x0000,
11456 0x00000000, 0xffff0002 },
11457 { RCVDBDI_STD_BD+0xc, 0x0000,
11458 0x00000000, 0xffffffff },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011459
Michael Chana71116d2005-05-29 14:58:11 -070011460 /* Receive BD Initiator Control Registers. */
11461 { RCVBDI_STD_THRESH, TG3_FL_NOT_5705,
11462 0x00000000, 0xffffffff },
11463 { RCVBDI_STD_THRESH, TG3_FL_5705,
11464 0x00000000, 0x000003ff },
11465 { RCVBDI_JUMBO_THRESH, TG3_FL_NOT_5705,
11466 0x00000000, 0xffffffff },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011467
Michael Chana71116d2005-05-29 14:58:11 -070011468 /* Host Coalescing Control Registers. */
11469 { HOSTCC_MODE, TG3_FL_NOT_5705,
11470 0x00000000, 0x00000004 },
11471 { HOSTCC_MODE, TG3_FL_5705,
11472 0x00000000, 0x000000f6 },
11473 { HOSTCC_RXCOL_TICKS, TG3_FL_NOT_5705,
11474 0x00000000, 0xffffffff },
11475 { HOSTCC_RXCOL_TICKS, TG3_FL_5705,
11476 0x00000000, 0x000003ff },
11477 { HOSTCC_TXCOL_TICKS, TG3_FL_NOT_5705,
11478 0x00000000, 0xffffffff },
11479 { HOSTCC_TXCOL_TICKS, TG3_FL_5705,
11480 0x00000000, 0x000003ff },
11481 { HOSTCC_RXMAX_FRAMES, TG3_FL_NOT_5705,
11482 0x00000000, 0xffffffff },
11483 { HOSTCC_RXMAX_FRAMES, TG3_FL_5705 | TG3_FL_NOT_5788,
11484 0x00000000, 0x000000ff },
11485 { HOSTCC_TXMAX_FRAMES, TG3_FL_NOT_5705,
11486 0x00000000, 0xffffffff },
11487 { HOSTCC_TXMAX_FRAMES, TG3_FL_5705 | TG3_FL_NOT_5788,
11488 0x00000000, 0x000000ff },
11489 { HOSTCC_RXCOAL_TICK_INT, TG3_FL_NOT_5705,
11490 0x00000000, 0xffffffff },
11491 { HOSTCC_TXCOAL_TICK_INT, TG3_FL_NOT_5705,
11492 0x00000000, 0xffffffff },
11493 { HOSTCC_RXCOAL_MAXF_INT, TG3_FL_NOT_5705,
11494 0x00000000, 0xffffffff },
11495 { HOSTCC_RXCOAL_MAXF_INT, TG3_FL_5705 | TG3_FL_NOT_5788,
11496 0x00000000, 0x000000ff },
11497 { HOSTCC_TXCOAL_MAXF_INT, TG3_FL_NOT_5705,
11498 0x00000000, 0xffffffff },
11499 { HOSTCC_TXCOAL_MAXF_INT, TG3_FL_5705 | TG3_FL_NOT_5788,
11500 0x00000000, 0x000000ff },
11501 { HOSTCC_STAT_COAL_TICKS, TG3_FL_NOT_5705,
11502 0x00000000, 0xffffffff },
11503 { HOSTCC_STATS_BLK_HOST_ADDR, TG3_FL_NOT_5705,
11504 0x00000000, 0xffffffff },
11505 { HOSTCC_STATS_BLK_HOST_ADDR+4, TG3_FL_NOT_5705,
11506 0x00000000, 0xffffffff },
11507 { HOSTCC_STATUS_BLK_HOST_ADDR, 0x0000,
11508 0x00000000, 0xffffffff },
11509 { HOSTCC_STATUS_BLK_HOST_ADDR+4, 0x0000,
11510 0x00000000, 0xffffffff },
11511 { HOSTCC_STATS_BLK_NIC_ADDR, 0x0000,
11512 0xffffffff, 0x00000000 },
11513 { HOSTCC_STATUS_BLK_NIC_ADDR, 0x0000,
11514 0xffffffff, 0x00000000 },
11515
11516 /* Buffer Manager Control Registers. */
Michael Chanb16250e2006-09-27 16:10:14 -070011517 { BUFMGR_MB_POOL_ADDR, TG3_FL_NOT_5750,
Michael Chana71116d2005-05-29 14:58:11 -070011518 0x00000000, 0x007fff80 },
Michael Chanb16250e2006-09-27 16:10:14 -070011519 { BUFMGR_MB_POOL_SIZE, TG3_FL_NOT_5750,
Michael Chana71116d2005-05-29 14:58:11 -070011520 0x00000000, 0x007fffff },
11521 { BUFMGR_MB_RDMA_LOW_WATER, 0x0000,
11522 0x00000000, 0x0000003f },
11523 { BUFMGR_MB_MACRX_LOW_WATER, 0x0000,
11524 0x00000000, 0x000001ff },
11525 { BUFMGR_MB_HIGH_WATER, 0x0000,
11526 0x00000000, 0x000001ff },
11527 { BUFMGR_DMA_DESC_POOL_ADDR, TG3_FL_NOT_5705,
11528 0xffffffff, 0x00000000 },
11529 { BUFMGR_DMA_DESC_POOL_SIZE, TG3_FL_NOT_5705,
11530 0xffffffff, 0x00000000 },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011531
Michael Chana71116d2005-05-29 14:58:11 -070011532 /* Mailbox Registers */
11533 { GRCMBOX_RCVSTD_PROD_IDX+4, 0x0000,
11534 0x00000000, 0x000001ff },
11535 { GRCMBOX_RCVJUMBO_PROD_IDX+4, TG3_FL_NOT_5705,
11536 0x00000000, 0x000001ff },
11537 { GRCMBOX_RCVRET_CON_IDX_0+4, 0x0000,
11538 0x00000000, 0x000007ff },
11539 { GRCMBOX_SNDHOST_PROD_IDX_0+4, 0x0000,
11540 0x00000000, 0x000001ff },
11541
11542 { 0xffff, 0x0000, 0x00000000, 0x00000000 },
11543 };
11544
Michael Chanb16250e2006-09-27 16:10:14 -070011545 is_5705 = is_5750 = 0;
Joe Perches63c3a662011-04-26 08:12:10 +000011546 if (tg3_flag(tp, 5705_PLUS)) {
Michael Chana71116d2005-05-29 14:58:11 -070011547 is_5705 = 1;
Joe Perches63c3a662011-04-26 08:12:10 +000011548 if (tg3_flag(tp, 5750_PLUS))
Michael Chanb16250e2006-09-27 16:10:14 -070011549 is_5750 = 1;
11550 }
Michael Chana71116d2005-05-29 14:58:11 -070011551
11552 for (i = 0; reg_tbl[i].offset != 0xffff; i++) {
11553 if (is_5705 && (reg_tbl[i].flags & TG3_FL_NOT_5705))
11554 continue;
11555
11556 if (!is_5705 && (reg_tbl[i].flags & TG3_FL_5705))
11557 continue;
11558
Joe Perches63c3a662011-04-26 08:12:10 +000011559 if (tg3_flag(tp, IS_5788) &&
Michael Chana71116d2005-05-29 14:58:11 -070011560 (reg_tbl[i].flags & TG3_FL_NOT_5788))
11561 continue;
11562
Michael Chanb16250e2006-09-27 16:10:14 -070011563 if (is_5750 && (reg_tbl[i].flags & TG3_FL_NOT_5750))
11564 continue;
11565
Michael Chana71116d2005-05-29 14:58:11 -070011566 offset = (u32) reg_tbl[i].offset;
11567 read_mask = reg_tbl[i].read_mask;
11568 write_mask = reg_tbl[i].write_mask;
11569
11570 /* Save the original register content */
11571 save_val = tr32(offset);
11572
11573 /* Determine the read-only value. */
11574 read_val = save_val & read_mask;
11575
11576 /* Write zero to the register, then make sure the read-only bits
11577 * are not changed and the read/write bits are all zeros.
11578 */
11579 tw32(offset, 0);
11580
11581 val = tr32(offset);
11582
11583 /* Test the read-only and read/write bits. */
11584 if (((val & read_mask) != read_val) || (val & write_mask))
11585 goto out;
11586
11587 /* Write ones to all the bits defined by RdMask and WrMask, then
11588 * make sure the read-only bits are not changed and the
11589 * read/write bits are all ones.
11590 */
11591 tw32(offset, read_mask | write_mask);
11592
11593 val = tr32(offset);
11594
11595 /* Test the read-only bits. */
11596 if ((val & read_mask) != read_val)
11597 goto out;
11598
11599 /* Test the read/write bits. */
11600 if ((val & write_mask) != write_mask)
11601 goto out;
11602
11603 tw32(offset, save_val);
11604 }
11605
11606 return 0;
11607
11608out:
Michael Chan9f88f292006-12-07 00:22:54 -080011609 if (netif_msg_hw(tp))
Matt Carlson2445e462010-04-05 10:19:21 +000011610 netdev_err(tp->dev,
11611 "Register test failed at offset %x\n", offset);
Michael Chana71116d2005-05-29 14:58:11 -070011612 tw32(offset, save_val);
11613 return -EIO;
11614}
11615
Michael Chan7942e1d2005-05-29 14:58:36 -070011616static int tg3_do_mem_test(struct tg3 *tp, u32 offset, u32 len)
11617{
Arjan van de Venf71e1302006-03-03 21:33:57 -050011618 static const u32 test_pattern[] = { 0x00000000, 0xffffffff, 0xaa55a55a };
Michael Chan7942e1d2005-05-29 14:58:36 -070011619 int i;
11620 u32 j;
11621
Alejandro Martinez Ruize9edda62007-10-15 03:37:43 +020011622 for (i = 0; i < ARRAY_SIZE(test_pattern); i++) {
Michael Chan7942e1d2005-05-29 14:58:36 -070011623 for (j = 0; j < len; j += 4) {
11624 u32 val;
11625
11626 tg3_write_mem(tp, offset + j, test_pattern[i]);
11627 tg3_read_mem(tp, offset + j, &val);
11628 if (val != test_pattern[i])
11629 return -EIO;
11630 }
11631 }
11632 return 0;
11633}
11634
11635static int tg3_test_memory(struct tg3 *tp)
11636{
11637 static struct mem_entry {
11638 u32 offset;
11639 u32 len;
11640 } mem_tbl_570x[] = {
Michael Chan38690192005-12-19 16:27:28 -080011641 { 0x00000000, 0x00b50},
Michael Chan7942e1d2005-05-29 14:58:36 -070011642 { 0x00002000, 0x1c000},
11643 { 0xffffffff, 0x00000}
11644 }, mem_tbl_5705[] = {
11645 { 0x00000100, 0x0000c},
11646 { 0x00000200, 0x00008},
Michael Chan7942e1d2005-05-29 14:58:36 -070011647 { 0x00004000, 0x00800},
11648 { 0x00006000, 0x01000},
11649 { 0x00008000, 0x02000},
11650 { 0x00010000, 0x0e000},
11651 { 0xffffffff, 0x00000}
Michael Chan79f4d132006-03-20 22:28:57 -080011652 }, mem_tbl_5755[] = {
11653 { 0x00000200, 0x00008},
11654 { 0x00004000, 0x00800},
11655 { 0x00006000, 0x00800},
11656 { 0x00008000, 0x02000},
11657 { 0x00010000, 0x0c000},
11658 { 0xffffffff, 0x00000}
Michael Chanb16250e2006-09-27 16:10:14 -070011659 }, mem_tbl_5906[] = {
11660 { 0x00000200, 0x00008},
11661 { 0x00004000, 0x00400},
11662 { 0x00006000, 0x00400},
11663 { 0x00008000, 0x01000},
11664 { 0x00010000, 0x01000},
11665 { 0xffffffff, 0x00000}
Matt Carlson8b5a6c42010-01-20 16:58:06 +000011666 }, mem_tbl_5717[] = {
11667 { 0x00000200, 0x00008},
11668 { 0x00010000, 0x0a000},
11669 { 0x00020000, 0x13c00},
11670 { 0xffffffff, 0x00000}
11671 }, mem_tbl_57765[] = {
11672 { 0x00000200, 0x00008},
11673 { 0x00004000, 0x00800},
11674 { 0x00006000, 0x09800},
11675 { 0x00010000, 0x0a000},
11676 { 0xffffffff, 0x00000}
Michael Chan7942e1d2005-05-29 14:58:36 -070011677 };
11678 struct mem_entry *mem_tbl;
11679 int err = 0;
11680 int i;
11681
Joe Perches63c3a662011-04-26 08:12:10 +000011682 if (tg3_flag(tp, 5717_PLUS))
Matt Carlson8b5a6c42010-01-20 16:58:06 +000011683 mem_tbl = mem_tbl_5717;
Matt Carlson55086ad2011-12-14 11:09:59 +000011684 else if (tg3_flag(tp, 57765_CLASS))
Matt Carlson8b5a6c42010-01-20 16:58:06 +000011685 mem_tbl = mem_tbl_57765;
Joe Perches63c3a662011-04-26 08:12:10 +000011686 else if (tg3_flag(tp, 5755_PLUS))
Matt Carlson321d32a2008-11-21 17:22:19 -080011687 mem_tbl = mem_tbl_5755;
11688 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
11689 mem_tbl = mem_tbl_5906;
Joe Perches63c3a662011-04-26 08:12:10 +000011690 else if (tg3_flag(tp, 5705_PLUS))
Matt Carlson321d32a2008-11-21 17:22:19 -080011691 mem_tbl = mem_tbl_5705;
11692 else
Michael Chan7942e1d2005-05-29 14:58:36 -070011693 mem_tbl = mem_tbl_570x;
11694
11695 for (i = 0; mem_tbl[i].offset != 0xffffffff; i++) {
Matt Carlsonbe98da62010-07-11 09:31:46 +000011696 err = tg3_do_mem_test(tp, mem_tbl[i].offset, mem_tbl[i].len);
11697 if (err)
Michael Chan7942e1d2005-05-29 14:58:36 -070011698 break;
11699 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011700
Michael Chan7942e1d2005-05-29 14:58:36 -070011701 return err;
11702}
11703
Matt Carlsonbb158d62011-04-25 12:42:47 +000011704#define TG3_TSO_MSS 500
11705
11706#define TG3_TSO_IP_HDR_LEN 20
11707#define TG3_TSO_TCP_HDR_LEN 20
11708#define TG3_TSO_TCP_OPT_LEN 12
11709
11710static const u8 tg3_tso_header[] = {
117110x08, 0x00,
117120x45, 0x00, 0x00, 0x00,
117130x00, 0x00, 0x40, 0x00,
117140x40, 0x06, 0x00, 0x00,
117150x0a, 0x00, 0x00, 0x01,
117160x0a, 0x00, 0x00, 0x02,
117170x0d, 0x00, 0xe0, 0x00,
117180x00, 0x00, 0x01, 0x00,
117190x00, 0x00, 0x02, 0x00,
117200x80, 0x10, 0x10, 0x00,
117210x14, 0x09, 0x00, 0x00,
117220x01, 0x01, 0x08, 0x0a,
117230x11, 0x11, 0x11, 0x11,
117240x11, 0x11, 0x11, 0x11,
11725};
Michael Chan9f40dea2005-09-05 17:53:06 -070011726
Matt Carlson28a45952011-08-19 13:58:22 +000011727static int tg3_run_loopback(struct tg3 *tp, u32 pktsz, bool tso_loopback)
Michael Chanc76949a2005-05-29 14:58:59 -070011728{
Matt Carlson5e5a7f32011-08-19 13:58:21 +000011729 u32 rx_start_idx, rx_idx, tx_idx, opaque_key;
Matt Carlsonbb158d62011-04-25 12:42:47 +000011730 u32 base_flags = 0, mss = 0, desc_idx, coal_now, data_off, val;
Matt Carlson84b67b22011-07-27 14:20:52 +000011731 u32 budget;
Eric Dumazet9205fd92011-11-18 06:47:01 +000011732 struct sk_buff *skb;
11733 u8 *tx_data, *rx_data;
Michael Chanc76949a2005-05-29 14:58:59 -070011734 dma_addr_t map;
11735 int num_pkts, tx_len, rx_len, i, err;
11736 struct tg3_rx_buffer_desc *desc;
Matt Carlson898a56f2009-08-28 14:02:40 +000011737 struct tg3_napi *tnapi, *rnapi;
Matt Carlson8fea32b2010-09-15 08:59:58 +000011738 struct tg3_rx_prodring_set *tpr = &tp->napi[0].prodring;
Michael Chanc76949a2005-05-29 14:58:59 -070011739
Matt Carlsonc8873402010-02-12 14:47:11 +000011740 tnapi = &tp->napi[0];
11741 rnapi = &tp->napi[0];
Matt Carlson0c1d0e22009-09-01 13:16:33 +000011742 if (tp->irq_cnt > 1) {
Joe Perches63c3a662011-04-26 08:12:10 +000011743 if (tg3_flag(tp, ENABLE_RSS))
Matt Carlson1da85aa2010-09-30 10:34:34 +000011744 rnapi = &tp->napi[1];
Joe Perches63c3a662011-04-26 08:12:10 +000011745 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlsonc8873402010-02-12 14:47:11 +000011746 tnapi = &tp->napi[1];
Matt Carlson0c1d0e22009-09-01 13:16:33 +000011747 }
Matt Carlsonfd2ce372009-09-01 12:51:13 +000011748 coal_now = tnapi->coal_now | rnapi->coal_now;
Matt Carlson898a56f2009-08-28 14:02:40 +000011749
Michael Chanc76949a2005-05-29 14:58:59 -070011750 err = -EIO;
11751
Matt Carlson4852a862011-04-13 11:05:07 +000011752 tx_len = pktsz;
David S. Millera20e9c62006-07-31 22:38:16 -070011753 skb = netdev_alloc_skb(tp->dev, tx_len);
Jesper Juhla50bb7b2006-05-09 23:14:35 -070011754 if (!skb)
11755 return -ENOMEM;
11756
Michael Chanc76949a2005-05-29 14:58:59 -070011757 tx_data = skb_put(skb, tx_len);
11758 memcpy(tx_data, tp->dev->dev_addr, 6);
11759 memset(tx_data + 6, 0x0, 8);
11760
Matt Carlson4852a862011-04-13 11:05:07 +000011761 tw32(MAC_RX_MTU_SIZE, tx_len + ETH_FCS_LEN);
Michael Chanc76949a2005-05-29 14:58:59 -070011762
Matt Carlson28a45952011-08-19 13:58:22 +000011763 if (tso_loopback) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000011764 struct iphdr *iph = (struct iphdr *)&tx_data[ETH_HLEN];
11765
11766 u32 hdr_len = TG3_TSO_IP_HDR_LEN + TG3_TSO_TCP_HDR_LEN +
11767 TG3_TSO_TCP_OPT_LEN;
11768
11769 memcpy(tx_data + ETH_ALEN * 2, tg3_tso_header,
11770 sizeof(tg3_tso_header));
11771 mss = TG3_TSO_MSS;
11772
11773 val = tx_len - ETH_ALEN * 2 - sizeof(tg3_tso_header);
11774 num_pkts = DIV_ROUND_UP(val, TG3_TSO_MSS);
11775
11776 /* Set the total length field in the IP header */
11777 iph->tot_len = htons((u16)(mss + hdr_len));
11778
11779 base_flags = (TXD_FLAG_CPU_PRE_DMA |
11780 TXD_FLAG_CPU_POST_DMA);
11781
Joe Perches63c3a662011-04-26 08:12:10 +000011782 if (tg3_flag(tp, HW_TSO_1) ||
11783 tg3_flag(tp, HW_TSO_2) ||
11784 tg3_flag(tp, HW_TSO_3)) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000011785 struct tcphdr *th;
11786 val = ETH_HLEN + TG3_TSO_IP_HDR_LEN;
11787 th = (struct tcphdr *)&tx_data[val];
11788 th->check = 0;
11789 } else
11790 base_flags |= TXD_FLAG_TCPUDP_CSUM;
11791
Joe Perches63c3a662011-04-26 08:12:10 +000011792 if (tg3_flag(tp, HW_TSO_3)) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000011793 mss |= (hdr_len & 0xc) << 12;
11794 if (hdr_len & 0x10)
11795 base_flags |= 0x00000010;
11796 base_flags |= (hdr_len & 0x3e0) << 5;
Joe Perches63c3a662011-04-26 08:12:10 +000011797 } else if (tg3_flag(tp, HW_TSO_2))
Matt Carlsonbb158d62011-04-25 12:42:47 +000011798 mss |= hdr_len << 9;
Joe Perches63c3a662011-04-26 08:12:10 +000011799 else if (tg3_flag(tp, HW_TSO_1) ||
Matt Carlsonbb158d62011-04-25 12:42:47 +000011800 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
11801 mss |= (TG3_TSO_TCP_OPT_LEN << 9);
11802 } else {
11803 base_flags |= (TG3_TSO_TCP_OPT_LEN << 10);
11804 }
11805
11806 data_off = ETH_ALEN * 2 + sizeof(tg3_tso_header);
11807 } else {
11808 num_pkts = 1;
11809 data_off = ETH_HLEN;
Michael Chanc441b452012-03-04 14:48:13 +000011810
11811 if (tg3_flag(tp, USE_JUMBO_BDFLAG) &&
11812 tx_len > VLAN_ETH_FRAME_LEN)
11813 base_flags |= TXD_FLAG_JMB_PKT;
Matt Carlsonbb158d62011-04-25 12:42:47 +000011814 }
11815
11816 for (i = data_off; i < tx_len; i++)
Michael Chanc76949a2005-05-29 14:58:59 -070011817 tx_data[i] = (u8) (i & 0xff);
11818
Alexander Duyckf4188d82009-12-02 16:48:38 +000011819 map = pci_map_single(tp->pdev, skb->data, tx_len, PCI_DMA_TODEVICE);
11820 if (pci_dma_mapping_error(tp->pdev, map)) {
Matt Carlsona21771d2009-11-02 14:25:31 +000011821 dev_kfree_skb(skb);
11822 return -EIO;
11823 }
Michael Chanc76949a2005-05-29 14:58:59 -070011824
Matt Carlson0d681b22011-07-27 14:20:49 +000011825 val = tnapi->tx_prod;
11826 tnapi->tx_buffers[val].skb = skb;
11827 dma_unmap_addr_set(&tnapi->tx_buffers[val], mapping, map);
11828
Michael Chanc76949a2005-05-29 14:58:59 -070011829 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +000011830 rnapi->coal_now);
Michael Chanc76949a2005-05-29 14:58:59 -070011831
11832 udelay(10);
11833
Matt Carlson898a56f2009-08-28 14:02:40 +000011834 rx_start_idx = rnapi->hw_status->idx[0].rx_producer;
Michael Chanc76949a2005-05-29 14:58:59 -070011835
Matt Carlson84b67b22011-07-27 14:20:52 +000011836 budget = tg3_tx_avail(tnapi);
11837 if (tg3_tx_frag_set(tnapi, &val, &budget, map, tx_len,
Matt Carlsond1a3b732011-07-27 14:20:51 +000011838 base_flags | TXD_FLAG_END, mss, 0)) {
11839 tnapi->tx_buffers[val].skb = NULL;
11840 dev_kfree_skb(skb);
11841 return -EIO;
11842 }
Michael Chanc76949a2005-05-29 14:58:59 -070011843
Matt Carlsonf3f3f272009-08-28 14:03:21 +000011844 tnapi->tx_prod++;
Michael Chanc76949a2005-05-29 14:58:59 -070011845
Michael Chan6541b802012-03-04 14:48:14 +000011846 /* Sync BD data before updating mailbox */
11847 wmb();
11848
Matt Carlsonf3f3f272009-08-28 14:03:21 +000011849 tw32_tx_mbox(tnapi->prodmbox, tnapi->tx_prod);
11850 tr32_mailbox(tnapi->prodmbox);
Michael Chanc76949a2005-05-29 14:58:59 -070011851
11852 udelay(10);
11853
Matt Carlson303fc922009-11-02 14:27:34 +000011854 /* 350 usec to allow enough time on some 10/100 Mbps devices. */
11855 for (i = 0; i < 35; i++) {
Michael Chanc76949a2005-05-29 14:58:59 -070011856 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +000011857 coal_now);
Michael Chanc76949a2005-05-29 14:58:59 -070011858
11859 udelay(10);
11860
Matt Carlson898a56f2009-08-28 14:02:40 +000011861 tx_idx = tnapi->hw_status->idx[0].tx_consumer;
11862 rx_idx = rnapi->hw_status->idx[0].rx_producer;
Matt Carlsonf3f3f272009-08-28 14:03:21 +000011863 if ((tx_idx == tnapi->tx_prod) &&
Michael Chanc76949a2005-05-29 14:58:59 -070011864 (rx_idx == (rx_start_idx + num_pkts)))
11865 break;
11866 }
11867
Matt Carlsonba1142e2011-11-04 09:15:00 +000011868 tg3_tx_skb_unmap(tnapi, tnapi->tx_prod - 1, -1);
Michael Chanc76949a2005-05-29 14:58:59 -070011869 dev_kfree_skb(skb);
11870
Matt Carlsonf3f3f272009-08-28 14:03:21 +000011871 if (tx_idx != tnapi->tx_prod)
Michael Chanc76949a2005-05-29 14:58:59 -070011872 goto out;
11873
11874 if (rx_idx != rx_start_idx + num_pkts)
11875 goto out;
11876
Matt Carlsonbb158d62011-04-25 12:42:47 +000011877 val = data_off;
11878 while (rx_idx != rx_start_idx) {
11879 desc = &rnapi->rx_rcb[rx_start_idx++];
11880 desc_idx = desc->opaque & RXD_OPAQUE_INDEX_MASK;
11881 opaque_key = desc->opaque & RXD_OPAQUE_RING_MASK;
Michael Chanc76949a2005-05-29 14:58:59 -070011882
Matt Carlsonbb158d62011-04-25 12:42:47 +000011883 if ((desc->err_vlan & RXD_ERR_MASK) != 0 &&
11884 (desc->err_vlan != RXD_ERR_ODD_NIBBLE_RCVD_MII))
Matt Carlson4852a862011-04-13 11:05:07 +000011885 goto out;
Michael Chanc76949a2005-05-29 14:58:59 -070011886
Matt Carlsonbb158d62011-04-25 12:42:47 +000011887 rx_len = ((desc->idx_len & RXD_LEN_MASK) >> RXD_LEN_SHIFT)
11888 - ETH_FCS_LEN;
11889
Matt Carlson28a45952011-08-19 13:58:22 +000011890 if (!tso_loopback) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000011891 if (rx_len != tx_len)
11892 goto out;
11893
11894 if (pktsz <= TG3_RX_STD_DMA_SZ - ETH_FCS_LEN) {
11895 if (opaque_key != RXD_OPAQUE_RING_STD)
11896 goto out;
11897 } else {
11898 if (opaque_key != RXD_OPAQUE_RING_JUMBO)
11899 goto out;
11900 }
11901 } else if ((desc->type_flags & RXD_FLAG_TCPUDP_CSUM) &&
11902 (desc->ip_tcp_csum & RXD_TCPCSUM_MASK)
Matt Carlson54e0a672011-05-19 12:12:50 +000011903 >> RXD_TCPCSUM_SHIFT != 0xffff) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000011904 goto out;
11905 }
11906
11907 if (opaque_key == RXD_OPAQUE_RING_STD) {
Eric Dumazet9205fd92011-11-18 06:47:01 +000011908 rx_data = tpr->rx_std_buffers[desc_idx].data;
Matt Carlsonbb158d62011-04-25 12:42:47 +000011909 map = dma_unmap_addr(&tpr->rx_std_buffers[desc_idx],
11910 mapping);
11911 } else if (opaque_key == RXD_OPAQUE_RING_JUMBO) {
Eric Dumazet9205fd92011-11-18 06:47:01 +000011912 rx_data = tpr->rx_jmb_buffers[desc_idx].data;
Matt Carlsonbb158d62011-04-25 12:42:47 +000011913 map = dma_unmap_addr(&tpr->rx_jmb_buffers[desc_idx],
11914 mapping);
11915 } else
Matt Carlson4852a862011-04-13 11:05:07 +000011916 goto out;
11917
Matt Carlsonbb158d62011-04-25 12:42:47 +000011918 pci_dma_sync_single_for_cpu(tp->pdev, map, rx_len,
11919 PCI_DMA_FROMDEVICE);
11920
Eric Dumazet9205fd92011-11-18 06:47:01 +000011921 rx_data += TG3_RX_OFFSET(tp);
Matt Carlsonbb158d62011-04-25 12:42:47 +000011922 for (i = data_off; i < rx_len; i++, val++) {
Eric Dumazet9205fd92011-11-18 06:47:01 +000011923 if (*(rx_data + i) != (u8) (val & 0xff))
Matt Carlsonbb158d62011-04-25 12:42:47 +000011924 goto out;
11925 }
Matt Carlson4852a862011-04-13 11:05:07 +000011926 }
11927
Michael Chanc76949a2005-05-29 14:58:59 -070011928 err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011929
Eric Dumazet9205fd92011-11-18 06:47:01 +000011930 /* tg3_free_rings will unmap and free the rx_data */
Michael Chanc76949a2005-05-29 14:58:59 -070011931out:
11932 return err;
11933}
11934
Matt Carlson00c266b2011-04-25 12:42:46 +000011935#define TG3_STD_LOOPBACK_FAILED 1
11936#define TG3_JMB_LOOPBACK_FAILED 2
Matt Carlsonbb158d62011-04-25 12:42:47 +000011937#define TG3_TSO_LOOPBACK_FAILED 4
Matt Carlson28a45952011-08-19 13:58:22 +000011938#define TG3_LOOPBACK_FAILED \
11939 (TG3_STD_LOOPBACK_FAILED | \
11940 TG3_JMB_LOOPBACK_FAILED | \
11941 TG3_TSO_LOOPBACK_FAILED)
Matt Carlson00c266b2011-04-25 12:42:46 +000011942
Matt Carlson941ec902011-08-19 13:58:23 +000011943static int tg3_test_loopback(struct tg3 *tp, u64 *data, bool do_extlpbk)
Michael Chan9f40dea2005-09-05 17:53:06 -070011944{
Matt Carlson28a45952011-08-19 13:58:22 +000011945 int err = -EIO;
Matt Carlson2215e242011-08-19 13:58:19 +000011946 u32 eee_cap;
Michael Chanc441b452012-03-04 14:48:13 +000011947 u32 jmb_pkt_sz = 9000;
11948
11949 if (tp->dma_limit)
11950 jmb_pkt_sz = tp->dma_limit - ETH_HLEN;
Michael Chan9f40dea2005-09-05 17:53:06 -070011951
Matt Carlsonab789042011-01-25 15:58:54 +000011952 eee_cap = tp->phy_flags & TG3_PHYFLG_EEE_CAP;
11953 tp->phy_flags &= ~TG3_PHYFLG_EEE_CAP;
11954
Matt Carlson28a45952011-08-19 13:58:22 +000011955 if (!netif_running(tp->dev)) {
11956 data[0] = TG3_LOOPBACK_FAILED;
11957 data[1] = TG3_LOOPBACK_FAILED;
Matt Carlson941ec902011-08-19 13:58:23 +000011958 if (do_extlpbk)
11959 data[2] = TG3_LOOPBACK_FAILED;
Matt Carlson28a45952011-08-19 13:58:22 +000011960 goto done;
11961 }
11962
Michael Chanb9ec6c12006-07-25 16:37:27 -070011963 err = tg3_reset_hw(tp, 1);
Matt Carlsonab789042011-01-25 15:58:54 +000011964 if (err) {
Matt Carlson28a45952011-08-19 13:58:22 +000011965 data[0] = TG3_LOOPBACK_FAILED;
11966 data[1] = TG3_LOOPBACK_FAILED;
Matt Carlson941ec902011-08-19 13:58:23 +000011967 if (do_extlpbk)
11968 data[2] = TG3_LOOPBACK_FAILED;
Matt Carlsonab789042011-01-25 15:58:54 +000011969 goto done;
11970 }
Michael Chan9f40dea2005-09-05 17:53:06 -070011971
Joe Perches63c3a662011-04-26 08:12:10 +000011972 if (tg3_flag(tp, ENABLE_RSS)) {
Matt Carlson4a85f092011-04-20 07:57:37 +000011973 int i;
11974
11975 /* Reroute all rx packets to the 1st queue */
11976 for (i = MAC_RSS_INDIR_TBL_0;
11977 i < MAC_RSS_INDIR_TBL_0 + TG3_RSS_INDIR_TBL_SIZE; i += 4)
11978 tw32(i, 0x0);
11979 }
11980
Matt Carlson6e01b202011-08-19 13:58:20 +000011981 /* HW errata - mac loopback fails in some cases on 5780.
11982 * Normal traffic and PHY loopback are not affected by
11983 * errata. Also, the MAC loopback test is deprecated for
11984 * all newer ASIC revisions.
11985 */
11986 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5780 &&
11987 !tg3_flag(tp, CPMU_PRESENT)) {
11988 tg3_mac_loopback(tp, true);
Matt Carlson9936bcf2007-10-10 18:03:07 -070011989
Matt Carlson28a45952011-08-19 13:58:22 +000011990 if (tg3_run_loopback(tp, ETH_FRAME_LEN, false))
11991 data[0] |= TG3_STD_LOOPBACK_FAILED;
Matt Carlson6e01b202011-08-19 13:58:20 +000011992
11993 if (tg3_flag(tp, JUMBO_RING_ENABLE) &&
Michael Chanc441b452012-03-04 14:48:13 +000011994 tg3_run_loopback(tp, jmb_pkt_sz + ETH_HLEN, false))
Matt Carlson28a45952011-08-19 13:58:22 +000011995 data[0] |= TG3_JMB_LOOPBACK_FAILED;
Matt Carlson6e01b202011-08-19 13:58:20 +000011996
11997 tg3_mac_loopback(tp, false);
11998 }
Matt Carlson4852a862011-04-13 11:05:07 +000011999
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012000 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
Joe Perches63c3a662011-04-26 08:12:10 +000012001 !tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson5e5a7f32011-08-19 13:58:21 +000012002 int i;
12003
Matt Carlson941ec902011-08-19 13:58:23 +000012004 tg3_phy_lpbk_set(tp, 0, false);
Matt Carlson5e5a7f32011-08-19 13:58:21 +000012005
12006 /* Wait for link */
12007 for (i = 0; i < 100; i++) {
12008 if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
12009 break;
12010 mdelay(1);
12011 }
12012
Matt Carlson28a45952011-08-19 13:58:22 +000012013 if (tg3_run_loopback(tp, ETH_FRAME_LEN, false))
12014 data[1] |= TG3_STD_LOOPBACK_FAILED;
Joe Perches63c3a662011-04-26 08:12:10 +000012015 if (tg3_flag(tp, TSO_CAPABLE) &&
Matt Carlson28a45952011-08-19 13:58:22 +000012016 tg3_run_loopback(tp, ETH_FRAME_LEN, true))
12017 data[1] |= TG3_TSO_LOOPBACK_FAILED;
Joe Perches63c3a662011-04-26 08:12:10 +000012018 if (tg3_flag(tp, JUMBO_RING_ENABLE) &&
Michael Chanc441b452012-03-04 14:48:13 +000012019 tg3_run_loopback(tp, jmb_pkt_sz + ETH_HLEN, false))
Matt Carlson28a45952011-08-19 13:58:22 +000012020 data[1] |= TG3_JMB_LOOPBACK_FAILED;
Michael Chan9f40dea2005-09-05 17:53:06 -070012021
Matt Carlson941ec902011-08-19 13:58:23 +000012022 if (do_extlpbk) {
12023 tg3_phy_lpbk_set(tp, 0, true);
12024
12025 /* All link indications report up, but the hardware
12026 * isn't really ready for about 20 msec. Double it
12027 * to be sure.
12028 */
12029 mdelay(40);
12030
12031 if (tg3_run_loopback(tp, ETH_FRAME_LEN, false))
12032 data[2] |= TG3_STD_LOOPBACK_FAILED;
12033 if (tg3_flag(tp, TSO_CAPABLE) &&
12034 tg3_run_loopback(tp, ETH_FRAME_LEN, true))
12035 data[2] |= TG3_TSO_LOOPBACK_FAILED;
12036 if (tg3_flag(tp, JUMBO_RING_ENABLE) &&
Michael Chanc441b452012-03-04 14:48:13 +000012037 tg3_run_loopback(tp, jmb_pkt_sz + ETH_HLEN, false))
Matt Carlson941ec902011-08-19 13:58:23 +000012038 data[2] |= TG3_JMB_LOOPBACK_FAILED;
12039 }
12040
Matt Carlson5e5a7f32011-08-19 13:58:21 +000012041 /* Re-enable gphy autopowerdown. */
12042 if (tp->phy_flags & TG3_PHYFLG_ENABLE_APD)
12043 tg3_phy_toggle_apd(tp, true);
12044 }
Matt Carlson6833c042008-11-21 17:18:59 -080012045
Matt Carlson941ec902011-08-19 13:58:23 +000012046 err = (data[0] | data[1] | data[2]) ? -EIO : 0;
Matt Carlson28a45952011-08-19 13:58:22 +000012047
Matt Carlsonab789042011-01-25 15:58:54 +000012048done:
12049 tp->phy_flags |= eee_cap;
12050
Michael Chan9f40dea2005-09-05 17:53:06 -070012051 return err;
12052}
12053
Michael Chan4cafd3f2005-05-29 14:56:34 -070012054static void tg3_self_test(struct net_device *dev, struct ethtool_test *etest,
12055 u64 *data)
12056{
Michael Chan566f86a2005-05-29 14:56:58 -070012057 struct tg3 *tp = netdev_priv(dev);
Matt Carlson941ec902011-08-19 13:58:23 +000012058 bool doextlpbk = etest->flags & ETH_TEST_FL_EXTERNAL_LB;
Michael Chan566f86a2005-05-29 14:56:58 -070012059
Matt Carlsonbed98292011-07-13 09:27:29 +000012060 if ((tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) &&
12061 tg3_power_up(tp)) {
12062 etest->flags |= ETH_TEST_FL_FAILED;
12063 memset(data, 1, sizeof(u64) * TG3_NUM_TEST);
12064 return;
12065 }
Michael Chanbc1c7562006-03-20 17:48:03 -080012066
Michael Chan566f86a2005-05-29 14:56:58 -070012067 memset(data, 0, sizeof(u64) * TG3_NUM_TEST);
12068
12069 if (tg3_test_nvram(tp) != 0) {
12070 etest->flags |= ETH_TEST_FL_FAILED;
12071 data[0] = 1;
12072 }
Matt Carlson941ec902011-08-19 13:58:23 +000012073 if (!doextlpbk && tg3_test_link(tp)) {
Michael Chanca430072005-05-29 14:57:23 -070012074 etest->flags |= ETH_TEST_FL_FAILED;
12075 data[1] = 1;
12076 }
Michael Chana71116d2005-05-29 14:58:11 -070012077 if (etest->flags & ETH_TEST_FL_OFFLINE) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012078 int err, err2 = 0, irq_sync = 0;
Michael Chana71116d2005-05-29 14:58:11 -070012079
Michael Chanbbe832c2005-06-24 20:20:04 -070012080 if (netif_running(dev)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012081 tg3_phy_stop(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -070012082 tg3_netif_stop(tp);
12083 irq_sync = 1;
12084 }
12085
12086 tg3_full_lock(tp, irq_sync);
Michael Chana71116d2005-05-29 14:58:11 -070012087
12088 tg3_halt(tp, RESET_KIND_SUSPEND, 1);
Michael Chanec41c7d2006-01-17 02:40:55 -080012089 err = tg3_nvram_lock(tp);
Michael Chana71116d2005-05-29 14:58:11 -070012090 tg3_halt_cpu(tp, RX_CPU_BASE);
Joe Perches63c3a662011-04-26 08:12:10 +000012091 if (!tg3_flag(tp, 5705_PLUS))
Michael Chana71116d2005-05-29 14:58:11 -070012092 tg3_halt_cpu(tp, TX_CPU_BASE);
Michael Chanec41c7d2006-01-17 02:40:55 -080012093 if (!err)
12094 tg3_nvram_unlock(tp);
Michael Chana71116d2005-05-29 14:58:11 -070012095
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012096 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
Michael Chand9ab5ad2006-03-20 22:27:35 -080012097 tg3_phy_reset(tp);
12098
Michael Chana71116d2005-05-29 14:58:11 -070012099 if (tg3_test_registers(tp) != 0) {
12100 etest->flags |= ETH_TEST_FL_FAILED;
12101 data[2] = 1;
12102 }
Matt Carlson28a45952011-08-19 13:58:22 +000012103
Michael Chan7942e1d2005-05-29 14:58:36 -070012104 if (tg3_test_memory(tp) != 0) {
12105 etest->flags |= ETH_TEST_FL_FAILED;
12106 data[3] = 1;
12107 }
Matt Carlson28a45952011-08-19 13:58:22 +000012108
Matt Carlson941ec902011-08-19 13:58:23 +000012109 if (doextlpbk)
12110 etest->flags |= ETH_TEST_FL_EXTERNAL_LB_DONE;
12111
12112 if (tg3_test_loopback(tp, &data[4], doextlpbk))
Michael Chanc76949a2005-05-29 14:58:59 -070012113 etest->flags |= ETH_TEST_FL_FAILED;
Michael Chana71116d2005-05-29 14:58:11 -070012114
David S. Millerf47c11e2005-06-24 20:18:35 -070012115 tg3_full_unlock(tp);
12116
Michael Chand4bc3922005-05-29 14:59:20 -070012117 if (tg3_test_interrupt(tp) != 0) {
12118 etest->flags |= ETH_TEST_FL_FAILED;
Matt Carlson941ec902011-08-19 13:58:23 +000012119 data[7] = 1;
Michael Chand4bc3922005-05-29 14:59:20 -070012120 }
David S. Millerf47c11e2005-06-24 20:18:35 -070012121
12122 tg3_full_lock(tp, 0);
Michael Chand4bc3922005-05-29 14:59:20 -070012123
Michael Chana71116d2005-05-29 14:58:11 -070012124 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
12125 if (netif_running(dev)) {
Joe Perches63c3a662011-04-26 08:12:10 +000012126 tg3_flag_set(tp, INIT_COMPLETE);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012127 err2 = tg3_restart_hw(tp, 1);
12128 if (!err2)
Michael Chanb9ec6c12006-07-25 16:37:27 -070012129 tg3_netif_start(tp);
Michael Chana71116d2005-05-29 14:58:11 -070012130 }
David S. Millerf47c11e2005-06-24 20:18:35 -070012131
12132 tg3_full_unlock(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012133
12134 if (irq_sync && !err2)
12135 tg3_phy_start(tp);
Michael Chana71116d2005-05-29 14:58:11 -070012136 }
Matt Carlson800960682010-08-02 11:26:06 +000012137 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000012138 tg3_power_down(tp);
Michael Chanbc1c7562006-03-20 17:48:03 -080012139
Michael Chan4cafd3f2005-05-29 14:56:34 -070012140}
12141
Linus Torvalds1da177e2005-04-16 15:20:36 -070012142static int tg3_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
12143{
12144 struct mii_ioctl_data *data = if_mii(ifr);
12145 struct tg3 *tp = netdev_priv(dev);
12146 int err;
12147
Joe Perches63c3a662011-04-26 08:12:10 +000012148 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000012149 struct phy_device *phydev;
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012150 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012151 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000012152 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Richard Cochran28b04112010-07-17 08:48:55 +000012153 return phy_mii_ioctl(phydev, ifr, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012154 }
12155
Matt Carlson33f401a2010-04-05 10:19:27 +000012156 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012157 case SIOCGMIIPHY:
Matt Carlson882e9792009-09-01 13:21:36 +000012158 data->phy_id = tp->phy_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012159
12160 /* fallthru */
12161 case SIOCGMIIREG: {
12162 u32 mii_regval;
12163
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012164 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012165 break; /* We have no PHY */
12166
Matt Carlson34eea5a2011-04-20 07:57:38 +000012167 if (!netif_running(dev))
Michael Chanbc1c7562006-03-20 17:48:03 -080012168 return -EAGAIN;
12169
David S. Millerf47c11e2005-06-24 20:18:35 -070012170 spin_lock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012171 err = tg3_readphy(tp, data->reg_num & 0x1f, &mii_regval);
David S. Millerf47c11e2005-06-24 20:18:35 -070012172 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012173
12174 data->val_out = mii_regval;
12175
12176 return err;
12177 }
12178
12179 case SIOCSMIIREG:
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012180 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012181 break; /* We have no PHY */
12182
Matt Carlson34eea5a2011-04-20 07:57:38 +000012183 if (!netif_running(dev))
Michael Chanbc1c7562006-03-20 17:48:03 -080012184 return -EAGAIN;
12185
David S. Millerf47c11e2005-06-24 20:18:35 -070012186 spin_lock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012187 err = tg3_writephy(tp, data->reg_num & 0x1f, data->val_in);
David S. Millerf47c11e2005-06-24 20:18:35 -070012188 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012189
12190 return err;
12191
12192 default:
12193 /* do nothing */
12194 break;
12195 }
12196 return -EOPNOTSUPP;
12197}
12198
David S. Miller15f98502005-05-18 22:49:26 -070012199static int tg3_get_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
12200{
12201 struct tg3 *tp = netdev_priv(dev);
12202
12203 memcpy(ec, &tp->coal, sizeof(*ec));
12204 return 0;
12205}
12206
Michael Chand244c892005-07-05 14:42:33 -070012207static int tg3_set_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
12208{
12209 struct tg3 *tp = netdev_priv(dev);
12210 u32 max_rxcoal_tick_int = 0, max_txcoal_tick_int = 0;
12211 u32 max_stat_coal_ticks = 0, min_stat_coal_ticks = 0;
12212
Joe Perches63c3a662011-04-26 08:12:10 +000012213 if (!tg3_flag(tp, 5705_PLUS)) {
Michael Chand244c892005-07-05 14:42:33 -070012214 max_rxcoal_tick_int = MAX_RXCOAL_TICK_INT;
12215 max_txcoal_tick_int = MAX_TXCOAL_TICK_INT;
12216 max_stat_coal_ticks = MAX_STAT_COAL_TICKS;
12217 min_stat_coal_ticks = MIN_STAT_COAL_TICKS;
12218 }
12219
12220 if ((ec->rx_coalesce_usecs > MAX_RXCOL_TICKS) ||
12221 (ec->tx_coalesce_usecs > MAX_TXCOL_TICKS) ||
12222 (ec->rx_max_coalesced_frames > MAX_RXMAX_FRAMES) ||
12223 (ec->tx_max_coalesced_frames > MAX_TXMAX_FRAMES) ||
12224 (ec->rx_coalesce_usecs_irq > max_rxcoal_tick_int) ||
12225 (ec->tx_coalesce_usecs_irq > max_txcoal_tick_int) ||
12226 (ec->rx_max_coalesced_frames_irq > MAX_RXCOAL_MAXF_INT) ||
12227 (ec->tx_max_coalesced_frames_irq > MAX_TXCOAL_MAXF_INT) ||
12228 (ec->stats_block_coalesce_usecs > max_stat_coal_ticks) ||
12229 (ec->stats_block_coalesce_usecs < min_stat_coal_ticks))
12230 return -EINVAL;
12231
12232 /* No rx interrupts will be generated if both are zero */
12233 if ((ec->rx_coalesce_usecs == 0) &&
12234 (ec->rx_max_coalesced_frames == 0))
12235 return -EINVAL;
12236
12237 /* No tx interrupts will be generated if both are zero */
12238 if ((ec->tx_coalesce_usecs == 0) &&
12239 (ec->tx_max_coalesced_frames == 0))
12240 return -EINVAL;
12241
12242 /* Only copy relevant parameters, ignore all others. */
12243 tp->coal.rx_coalesce_usecs = ec->rx_coalesce_usecs;
12244 tp->coal.tx_coalesce_usecs = ec->tx_coalesce_usecs;
12245 tp->coal.rx_max_coalesced_frames = ec->rx_max_coalesced_frames;
12246 tp->coal.tx_max_coalesced_frames = ec->tx_max_coalesced_frames;
12247 tp->coal.rx_coalesce_usecs_irq = ec->rx_coalesce_usecs_irq;
12248 tp->coal.tx_coalesce_usecs_irq = ec->tx_coalesce_usecs_irq;
12249 tp->coal.rx_max_coalesced_frames_irq = ec->rx_max_coalesced_frames_irq;
12250 tp->coal.tx_max_coalesced_frames_irq = ec->tx_max_coalesced_frames_irq;
12251 tp->coal.stats_block_coalesce_usecs = ec->stats_block_coalesce_usecs;
12252
12253 if (netif_running(dev)) {
12254 tg3_full_lock(tp, 0);
12255 __tg3_set_coalesce(tp, &tp->coal);
12256 tg3_full_unlock(tp);
12257 }
12258 return 0;
12259}
12260
Jeff Garzik7282d492006-09-13 14:30:00 -040012261static const struct ethtool_ops tg3_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012262 .get_settings = tg3_get_settings,
12263 .set_settings = tg3_set_settings,
12264 .get_drvinfo = tg3_get_drvinfo,
12265 .get_regs_len = tg3_get_regs_len,
12266 .get_regs = tg3_get_regs,
12267 .get_wol = tg3_get_wol,
12268 .set_wol = tg3_set_wol,
12269 .get_msglevel = tg3_get_msglevel,
12270 .set_msglevel = tg3_set_msglevel,
12271 .nway_reset = tg3_nway_reset,
12272 .get_link = ethtool_op_get_link,
12273 .get_eeprom_len = tg3_get_eeprom_len,
12274 .get_eeprom = tg3_get_eeprom,
12275 .set_eeprom = tg3_set_eeprom,
12276 .get_ringparam = tg3_get_ringparam,
12277 .set_ringparam = tg3_set_ringparam,
12278 .get_pauseparam = tg3_get_pauseparam,
12279 .set_pauseparam = tg3_set_pauseparam,
Michael Chan4cafd3f2005-05-29 14:56:34 -070012280 .self_test = tg3_self_test,
Linus Torvalds1da177e2005-04-16 15:20:36 -070012281 .get_strings = tg3_get_strings,
stephen hemminger81b87092011-04-04 08:43:50 +000012282 .set_phys_id = tg3_set_phys_id,
Linus Torvalds1da177e2005-04-16 15:20:36 -070012283 .get_ethtool_stats = tg3_get_ethtool_stats,
David S. Miller15f98502005-05-18 22:49:26 -070012284 .get_coalesce = tg3_get_coalesce,
Michael Chand244c892005-07-05 14:42:33 -070012285 .set_coalesce = tg3_set_coalesce,
Jeff Garzikb9f2c042007-10-03 18:07:32 -070012286 .get_sset_count = tg3_get_sset_count,
Matt Carlson90415472011-12-16 13:33:23 +000012287 .get_rxnfc = tg3_get_rxnfc,
12288 .get_rxfh_indir_size = tg3_get_rxfh_indir_size,
12289 .get_rxfh_indir = tg3_get_rxfh_indir,
12290 .set_rxfh_indir = tg3_set_rxfh_indir,
Richard Cochran3f847492012-04-03 22:59:39 +000012291 .get_ts_info = ethtool_op_get_ts_info,
Linus Torvalds1da177e2005-04-16 15:20:36 -070012292};
12293
David S. Millerb4017c52012-03-01 17:57:40 -050012294static struct rtnl_link_stats64 *tg3_get_stats64(struct net_device *dev,
12295 struct rtnl_link_stats64 *stats)
12296{
12297 struct tg3 *tp = netdev_priv(dev);
12298
12299 if (!tp->hw_stats)
12300 return &tp->net_stats_prev;
12301
12302 spin_lock_bh(&tp->lock);
12303 tg3_get_nstats(tp, stats);
12304 spin_unlock_bh(&tp->lock);
12305
12306 return stats;
12307}
12308
Matt Carlsonccd5ba92012-02-13 10:20:08 +000012309static void tg3_set_rx_mode(struct net_device *dev)
12310{
12311 struct tg3 *tp = netdev_priv(dev);
12312
12313 if (!netif_running(dev))
12314 return;
12315
12316 tg3_full_lock(tp, 0);
12317 __tg3_set_rx_mode(dev);
12318 tg3_full_unlock(tp);
12319}
12320
Matt Carlsonfaf16272012-02-13 10:20:07 +000012321static inline void tg3_set_mtu(struct net_device *dev, struct tg3 *tp,
12322 int new_mtu)
12323{
12324 dev->mtu = new_mtu;
12325
12326 if (new_mtu > ETH_DATA_LEN) {
12327 if (tg3_flag(tp, 5780_CLASS)) {
12328 netdev_update_features(dev);
12329 tg3_flag_clear(tp, TSO_CAPABLE);
12330 } else {
12331 tg3_flag_set(tp, JUMBO_RING_ENABLE);
12332 }
12333 } else {
12334 if (tg3_flag(tp, 5780_CLASS)) {
12335 tg3_flag_set(tp, TSO_CAPABLE);
12336 netdev_update_features(dev);
12337 }
12338 tg3_flag_clear(tp, JUMBO_RING_ENABLE);
12339 }
12340}
12341
12342static int tg3_change_mtu(struct net_device *dev, int new_mtu)
12343{
12344 struct tg3 *tp = netdev_priv(dev);
Michael Chan2fae5e32012-03-04 14:48:15 +000012345 int err, reset_phy = 0;
Matt Carlsonfaf16272012-02-13 10:20:07 +000012346
12347 if (new_mtu < TG3_MIN_MTU || new_mtu > TG3_MAX_MTU(tp))
12348 return -EINVAL;
12349
12350 if (!netif_running(dev)) {
12351 /* We'll just catch it later when the
12352 * device is up'd.
12353 */
12354 tg3_set_mtu(dev, tp, new_mtu);
12355 return 0;
12356 }
12357
12358 tg3_phy_stop(tp);
12359
12360 tg3_netif_stop(tp);
12361
12362 tg3_full_lock(tp, 1);
12363
12364 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
12365
12366 tg3_set_mtu(dev, tp, new_mtu);
12367
Michael Chan2fae5e32012-03-04 14:48:15 +000012368 /* Reset PHY, otherwise the read DMA engine will be in a mode that
12369 * breaks all requests to 256 bytes.
12370 */
12371 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57766)
12372 reset_phy = 1;
12373
12374 err = tg3_restart_hw(tp, reset_phy);
Matt Carlsonfaf16272012-02-13 10:20:07 +000012375
12376 if (!err)
12377 tg3_netif_start(tp);
12378
12379 tg3_full_unlock(tp);
12380
12381 if (!err)
12382 tg3_phy_start(tp);
12383
12384 return err;
12385}
12386
12387static const struct net_device_ops tg3_netdev_ops = {
12388 .ndo_open = tg3_open,
12389 .ndo_stop = tg3_close,
12390 .ndo_start_xmit = tg3_start_xmit,
12391 .ndo_get_stats64 = tg3_get_stats64,
12392 .ndo_validate_addr = eth_validate_addr,
12393 .ndo_set_rx_mode = tg3_set_rx_mode,
12394 .ndo_set_mac_address = tg3_set_mac_addr,
12395 .ndo_do_ioctl = tg3_ioctl,
12396 .ndo_tx_timeout = tg3_tx_timeout,
12397 .ndo_change_mtu = tg3_change_mtu,
12398 .ndo_fix_features = tg3_fix_features,
12399 .ndo_set_features = tg3_set_features,
12400#ifdef CONFIG_NET_POLL_CONTROLLER
12401 .ndo_poll_controller = tg3_poll_controller,
12402#endif
12403};
12404
Linus Torvalds1da177e2005-04-16 15:20:36 -070012405static void __devinit tg3_get_eeprom_size(struct tg3 *tp)
12406{
Michael Chan1b277772006-03-20 22:27:48 -080012407 u32 cursize, val, magic;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012408
12409 tp->nvram_size = EEPROM_CHIP_SIZE;
12410
Matt Carlsone4f34112009-02-25 14:25:00 +000012411 if (tg3_nvram_read(tp, 0, &magic) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012412 return;
12413
Michael Chanb16250e2006-09-27 16:10:14 -070012414 if ((magic != TG3_EEPROM_MAGIC) &&
12415 ((magic & TG3_EEPROM_MAGIC_FW_MSK) != TG3_EEPROM_MAGIC_FW) &&
12416 ((magic & TG3_EEPROM_MAGIC_HW_MSK) != TG3_EEPROM_MAGIC_HW))
Linus Torvalds1da177e2005-04-16 15:20:36 -070012417 return;
12418
12419 /*
12420 * Size the chip by reading offsets at increasing powers of two.
12421 * When we encounter our validation signature, we know the addressing
12422 * has wrapped around, and thus have our chip size.
12423 */
Michael Chan1b277772006-03-20 22:27:48 -080012424 cursize = 0x10;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012425
12426 while (cursize < tp->nvram_size) {
Matt Carlsone4f34112009-02-25 14:25:00 +000012427 if (tg3_nvram_read(tp, cursize, &val) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012428 return;
12429
Michael Chan18201802006-03-20 22:29:15 -080012430 if (val == magic)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012431 break;
12432
12433 cursize <<= 1;
12434 }
12435
12436 tp->nvram_size = cursize;
12437}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012438
Linus Torvalds1da177e2005-04-16 15:20:36 -070012439static void __devinit tg3_get_nvram_size(struct tg3 *tp)
12440{
12441 u32 val;
12442
Joe Perches63c3a662011-04-26 08:12:10 +000012443 if (tg3_flag(tp, NO_NVRAM) || tg3_nvram_read(tp, 0, &val) != 0)
Michael Chan1b277772006-03-20 22:27:48 -080012444 return;
12445
12446 /* Selfboot format */
Michael Chan18201802006-03-20 22:29:15 -080012447 if (val != TG3_EEPROM_MAGIC) {
Michael Chan1b277772006-03-20 22:27:48 -080012448 tg3_get_eeprom_size(tp);
12449 return;
12450 }
12451
Matt Carlson6d348f22009-02-25 14:25:52 +000012452 if (tg3_nvram_read(tp, 0xf0, &val) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012453 if (val != 0) {
Matt Carlson6d348f22009-02-25 14:25:52 +000012454 /* This is confusing. We want to operate on the
12455 * 16-bit value at offset 0xf2. The tg3_nvram_read()
12456 * call will read from NVRAM and byteswap the data
12457 * according to the byteswapping settings for all
12458 * other register accesses. This ensures the data we
12459 * want will always reside in the lower 16-bits.
12460 * However, the data in NVRAM is in LE format, which
12461 * means the data from the NVRAM read will always be
12462 * opposite the endianness of the CPU. The 16-bit
12463 * byteswap then brings the data to CPU endianness.
12464 */
12465 tp->nvram_size = swab16((u16)(val & 0x0000ffff)) * 1024;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012466 return;
12467 }
12468 }
Matt Carlsonfd1122a2008-05-02 16:48:36 -070012469 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012470}
12471
12472static void __devinit tg3_get_nvram_info(struct tg3 *tp)
12473{
12474 u32 nvcfg1;
12475
12476 nvcfg1 = tr32(NVRAM_CFG1);
12477 if (nvcfg1 & NVRAM_CFG1_FLASHIF_ENAB) {
Joe Perches63c3a662011-04-26 08:12:10 +000012478 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000012479 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012480 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
12481 tw32(NVRAM_CFG1, nvcfg1);
12482 }
12483
Matt Carlson6ff6f812011-05-19 12:12:54 +000012484 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 ||
Joe Perches63c3a662011-04-26 08:12:10 +000012485 tg3_flag(tp, 5780_CLASS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012486 switch (nvcfg1 & NVRAM_CFG1_VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000012487 case FLASH_VENDOR_ATMEL_FLASH_BUFFERED:
12488 tp->nvram_jedecnum = JEDEC_ATMEL;
12489 tp->nvram_pagesize = ATMEL_AT45DB0X1B_PAGE_SIZE;
Joe Perches63c3a662011-04-26 08:12:10 +000012490 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000012491 break;
12492 case FLASH_VENDOR_ATMEL_FLASH_UNBUFFERED:
12493 tp->nvram_jedecnum = JEDEC_ATMEL;
12494 tp->nvram_pagesize = ATMEL_AT25F512_PAGE_SIZE;
12495 break;
12496 case FLASH_VENDOR_ATMEL_EEPROM:
12497 tp->nvram_jedecnum = JEDEC_ATMEL;
12498 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
Joe Perches63c3a662011-04-26 08:12:10 +000012499 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000012500 break;
12501 case FLASH_VENDOR_ST:
12502 tp->nvram_jedecnum = JEDEC_ST;
12503 tp->nvram_pagesize = ST_M45PEX0_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_SAIFUN:
12507 tp->nvram_jedecnum = JEDEC_SAIFUN;
12508 tp->nvram_pagesize = SAIFUN_SA25F0XX_PAGE_SIZE;
12509 break;
12510 case FLASH_VENDOR_SST_SMALL:
12511 case FLASH_VENDOR_SST_LARGE:
12512 tp->nvram_jedecnum = JEDEC_SST;
12513 tp->nvram_pagesize = SST_25VF0X0_PAGE_SIZE;
12514 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012515 }
Matt Carlson8590a602009-08-28 12:29:16 +000012516 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012517 tp->nvram_jedecnum = JEDEC_ATMEL;
12518 tp->nvram_pagesize = ATMEL_AT45DB0X1B_PAGE_SIZE;
Joe Perches63c3a662011-04-26 08:12:10 +000012519 tg3_flag_set(tp, NVRAM_BUFFERED);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012520 }
12521}
12522
Matt Carlsona1b950d2009-09-01 13:20:17 +000012523static void __devinit tg3_nvram_get_pagesize(struct tg3 *tp, u32 nvmcfg1)
12524{
12525 switch (nvmcfg1 & NVRAM_CFG1_5752PAGE_SIZE_MASK) {
12526 case FLASH_5752PAGE_SIZE_256:
12527 tp->nvram_pagesize = 256;
12528 break;
12529 case FLASH_5752PAGE_SIZE_512:
12530 tp->nvram_pagesize = 512;
12531 break;
12532 case FLASH_5752PAGE_SIZE_1K:
12533 tp->nvram_pagesize = 1024;
12534 break;
12535 case FLASH_5752PAGE_SIZE_2K:
12536 tp->nvram_pagesize = 2048;
12537 break;
12538 case FLASH_5752PAGE_SIZE_4K:
12539 tp->nvram_pagesize = 4096;
12540 break;
12541 case FLASH_5752PAGE_SIZE_264:
12542 tp->nvram_pagesize = 264;
12543 break;
12544 case FLASH_5752PAGE_SIZE_528:
12545 tp->nvram_pagesize = 528;
12546 break;
12547 }
12548}
12549
Michael Chan361b4ac2005-04-21 17:11:21 -070012550static void __devinit tg3_get_5752_nvram_info(struct tg3 *tp)
12551{
12552 u32 nvcfg1;
12553
12554 nvcfg1 = tr32(NVRAM_CFG1);
12555
Michael Chane6af3012005-04-21 17:12:05 -070012556 /* NVRAM protection for TPM */
12557 if (nvcfg1 & (1 << 27))
Joe Perches63c3a662011-04-26 08:12:10 +000012558 tg3_flag_set(tp, PROTECTED_NVRAM);
Michael Chane6af3012005-04-21 17:12:05 -070012559
Michael Chan361b4ac2005-04-21 17:11:21 -070012560 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000012561 case FLASH_5752VENDOR_ATMEL_EEPROM_64KHZ:
12562 case FLASH_5752VENDOR_ATMEL_EEPROM_376KHZ:
12563 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012564 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000012565 break;
12566 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
12567 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012568 tg3_flag_set(tp, NVRAM_BUFFERED);
12569 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000012570 break;
12571 case FLASH_5752VENDOR_ST_M45PE10:
12572 case FLASH_5752VENDOR_ST_M45PE20:
12573 case FLASH_5752VENDOR_ST_M45PE40:
12574 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000012575 tg3_flag_set(tp, NVRAM_BUFFERED);
12576 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000012577 break;
Michael Chan361b4ac2005-04-21 17:11:21 -070012578 }
12579
Joe Perches63c3a662011-04-26 08:12:10 +000012580 if (tg3_flag(tp, FLASH)) {
Matt Carlsona1b950d2009-09-01 13:20:17 +000012581 tg3_nvram_get_pagesize(tp, nvcfg1);
Matt Carlson8590a602009-08-28 12:29:16 +000012582 } else {
Michael Chan361b4ac2005-04-21 17:11:21 -070012583 /* For eeprom, set pagesize to maximum eeprom size */
12584 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
12585
12586 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
12587 tw32(NVRAM_CFG1, nvcfg1);
12588 }
12589}
12590
Michael Chand3c7b882006-03-23 01:28:25 -080012591static void __devinit tg3_get_5755_nvram_info(struct tg3 *tp)
12592{
Matt Carlson989a9d22007-05-05 11:51:05 -070012593 u32 nvcfg1, protect = 0;
Michael Chand3c7b882006-03-23 01:28:25 -080012594
12595 nvcfg1 = tr32(NVRAM_CFG1);
12596
12597 /* NVRAM protection for TPM */
Matt Carlson989a9d22007-05-05 11:51:05 -070012598 if (nvcfg1 & (1 << 27)) {
Joe Perches63c3a662011-04-26 08:12:10 +000012599 tg3_flag_set(tp, PROTECTED_NVRAM);
Matt Carlson989a9d22007-05-05 11:51:05 -070012600 protect = 1;
12601 }
Michael Chand3c7b882006-03-23 01:28:25 -080012602
Matt Carlson989a9d22007-05-05 11:51:05 -070012603 nvcfg1 &= NVRAM_CFG1_5752VENDOR_MASK;
12604 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000012605 case FLASH_5755VENDOR_ATMEL_FLASH_1:
12606 case FLASH_5755VENDOR_ATMEL_FLASH_2:
12607 case FLASH_5755VENDOR_ATMEL_FLASH_3:
12608 case FLASH_5755VENDOR_ATMEL_FLASH_5:
12609 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012610 tg3_flag_set(tp, NVRAM_BUFFERED);
12611 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000012612 tp->nvram_pagesize = 264;
12613 if (nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_1 ||
12614 nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_5)
12615 tp->nvram_size = (protect ? 0x3e200 :
12616 TG3_NVRAM_SIZE_512KB);
12617 else if (nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_2)
12618 tp->nvram_size = (protect ? 0x1f200 :
12619 TG3_NVRAM_SIZE_256KB);
12620 else
12621 tp->nvram_size = (protect ? 0x1f200 :
12622 TG3_NVRAM_SIZE_128KB);
12623 break;
12624 case FLASH_5752VENDOR_ST_M45PE10:
12625 case FLASH_5752VENDOR_ST_M45PE20:
12626 case FLASH_5752VENDOR_ST_M45PE40:
12627 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000012628 tg3_flag_set(tp, NVRAM_BUFFERED);
12629 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000012630 tp->nvram_pagesize = 256;
12631 if (nvcfg1 == FLASH_5752VENDOR_ST_M45PE10)
12632 tp->nvram_size = (protect ?
12633 TG3_NVRAM_SIZE_64KB :
12634 TG3_NVRAM_SIZE_128KB);
12635 else if (nvcfg1 == FLASH_5752VENDOR_ST_M45PE20)
12636 tp->nvram_size = (protect ?
12637 TG3_NVRAM_SIZE_64KB :
12638 TG3_NVRAM_SIZE_256KB);
12639 else
12640 tp->nvram_size = (protect ?
12641 TG3_NVRAM_SIZE_128KB :
12642 TG3_NVRAM_SIZE_512KB);
12643 break;
Michael Chand3c7b882006-03-23 01:28:25 -080012644 }
12645}
12646
Michael Chan1b277772006-03-20 22:27:48 -080012647static void __devinit tg3_get_5787_nvram_info(struct tg3 *tp)
12648{
12649 u32 nvcfg1;
12650
12651 nvcfg1 = tr32(NVRAM_CFG1);
12652
12653 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000012654 case FLASH_5787VENDOR_ATMEL_EEPROM_64KHZ:
12655 case FLASH_5787VENDOR_ATMEL_EEPROM_376KHZ:
12656 case FLASH_5787VENDOR_MICRO_EEPROM_64KHZ:
12657 case FLASH_5787VENDOR_MICRO_EEPROM_376KHZ:
12658 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012659 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000012660 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
Michael Chan1b277772006-03-20 22:27:48 -080012661
Matt Carlson8590a602009-08-28 12:29:16 +000012662 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
12663 tw32(NVRAM_CFG1, nvcfg1);
12664 break;
12665 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
12666 case FLASH_5755VENDOR_ATMEL_FLASH_1:
12667 case FLASH_5755VENDOR_ATMEL_FLASH_2:
12668 case FLASH_5755VENDOR_ATMEL_FLASH_3:
12669 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012670 tg3_flag_set(tp, NVRAM_BUFFERED);
12671 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000012672 tp->nvram_pagesize = 264;
12673 break;
12674 case FLASH_5752VENDOR_ST_M45PE10:
12675 case FLASH_5752VENDOR_ST_M45PE20:
12676 case FLASH_5752VENDOR_ST_M45PE40:
12677 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000012678 tg3_flag_set(tp, NVRAM_BUFFERED);
12679 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000012680 tp->nvram_pagesize = 256;
12681 break;
Michael Chan1b277772006-03-20 22:27:48 -080012682 }
12683}
12684
Matt Carlson6b91fa02007-10-10 18:01:09 -070012685static void __devinit tg3_get_5761_nvram_info(struct tg3 *tp)
12686{
12687 u32 nvcfg1, protect = 0;
12688
12689 nvcfg1 = tr32(NVRAM_CFG1);
12690
12691 /* NVRAM protection for TPM */
12692 if (nvcfg1 & (1 << 27)) {
Joe Perches63c3a662011-04-26 08:12:10 +000012693 tg3_flag_set(tp, PROTECTED_NVRAM);
Matt Carlson6b91fa02007-10-10 18:01:09 -070012694 protect = 1;
12695 }
12696
12697 nvcfg1 &= NVRAM_CFG1_5752VENDOR_MASK;
12698 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000012699 case FLASH_5761VENDOR_ATMEL_ADB021D:
12700 case FLASH_5761VENDOR_ATMEL_ADB041D:
12701 case FLASH_5761VENDOR_ATMEL_ADB081D:
12702 case FLASH_5761VENDOR_ATMEL_ADB161D:
12703 case FLASH_5761VENDOR_ATMEL_MDB021D:
12704 case FLASH_5761VENDOR_ATMEL_MDB041D:
12705 case FLASH_5761VENDOR_ATMEL_MDB081D:
12706 case FLASH_5761VENDOR_ATMEL_MDB161D:
12707 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012708 tg3_flag_set(tp, NVRAM_BUFFERED);
12709 tg3_flag_set(tp, FLASH);
12710 tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
Matt Carlson8590a602009-08-28 12:29:16 +000012711 tp->nvram_pagesize = 256;
12712 break;
12713 case FLASH_5761VENDOR_ST_A_M45PE20:
12714 case FLASH_5761VENDOR_ST_A_M45PE40:
12715 case FLASH_5761VENDOR_ST_A_M45PE80:
12716 case FLASH_5761VENDOR_ST_A_M45PE16:
12717 case FLASH_5761VENDOR_ST_M_M45PE20:
12718 case FLASH_5761VENDOR_ST_M_M45PE40:
12719 case FLASH_5761VENDOR_ST_M_M45PE80:
12720 case FLASH_5761VENDOR_ST_M_M45PE16:
12721 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000012722 tg3_flag_set(tp, NVRAM_BUFFERED);
12723 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000012724 tp->nvram_pagesize = 256;
12725 break;
Matt Carlson6b91fa02007-10-10 18:01:09 -070012726 }
12727
12728 if (protect) {
12729 tp->nvram_size = tr32(NVRAM_ADDR_LOCKOUT);
12730 } else {
12731 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000012732 case FLASH_5761VENDOR_ATMEL_ADB161D:
12733 case FLASH_5761VENDOR_ATMEL_MDB161D:
12734 case FLASH_5761VENDOR_ST_A_M45PE16:
12735 case FLASH_5761VENDOR_ST_M_M45PE16:
12736 tp->nvram_size = TG3_NVRAM_SIZE_2MB;
12737 break;
12738 case FLASH_5761VENDOR_ATMEL_ADB081D:
12739 case FLASH_5761VENDOR_ATMEL_MDB081D:
12740 case FLASH_5761VENDOR_ST_A_M45PE80:
12741 case FLASH_5761VENDOR_ST_M_M45PE80:
12742 tp->nvram_size = TG3_NVRAM_SIZE_1MB;
12743 break;
12744 case FLASH_5761VENDOR_ATMEL_ADB041D:
12745 case FLASH_5761VENDOR_ATMEL_MDB041D:
12746 case FLASH_5761VENDOR_ST_A_M45PE40:
12747 case FLASH_5761VENDOR_ST_M_M45PE40:
12748 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
12749 break;
12750 case FLASH_5761VENDOR_ATMEL_ADB021D:
12751 case FLASH_5761VENDOR_ATMEL_MDB021D:
12752 case FLASH_5761VENDOR_ST_A_M45PE20:
12753 case FLASH_5761VENDOR_ST_M_M45PE20:
12754 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
12755 break;
Matt Carlson6b91fa02007-10-10 18:01:09 -070012756 }
12757 }
12758}
12759
Michael Chanb5d37722006-09-27 16:06:21 -070012760static void __devinit tg3_get_5906_nvram_info(struct tg3 *tp)
12761{
12762 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012763 tg3_flag_set(tp, NVRAM_BUFFERED);
Michael Chanb5d37722006-09-27 16:06:21 -070012764 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
12765}
12766
Matt Carlson321d32a2008-11-21 17:22:19 -080012767static void __devinit tg3_get_57780_nvram_info(struct tg3 *tp)
12768{
12769 u32 nvcfg1;
12770
12771 nvcfg1 = tr32(NVRAM_CFG1);
12772
12773 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
12774 case FLASH_5787VENDOR_ATMEL_EEPROM_376KHZ:
12775 case FLASH_5787VENDOR_MICRO_EEPROM_376KHZ:
12776 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012777 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson321d32a2008-11-21 17:22:19 -080012778 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
12779
12780 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
12781 tw32(NVRAM_CFG1, nvcfg1);
12782 return;
12783 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
12784 case FLASH_57780VENDOR_ATMEL_AT45DB011D:
12785 case FLASH_57780VENDOR_ATMEL_AT45DB011B:
12786 case FLASH_57780VENDOR_ATMEL_AT45DB021D:
12787 case FLASH_57780VENDOR_ATMEL_AT45DB021B:
12788 case FLASH_57780VENDOR_ATMEL_AT45DB041D:
12789 case FLASH_57780VENDOR_ATMEL_AT45DB041B:
12790 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012791 tg3_flag_set(tp, NVRAM_BUFFERED);
12792 tg3_flag_set(tp, FLASH);
Matt Carlson321d32a2008-11-21 17:22:19 -080012793
12794 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
12795 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
12796 case FLASH_57780VENDOR_ATMEL_AT45DB011D:
12797 case FLASH_57780VENDOR_ATMEL_AT45DB011B:
12798 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
12799 break;
12800 case FLASH_57780VENDOR_ATMEL_AT45DB021D:
12801 case FLASH_57780VENDOR_ATMEL_AT45DB021B:
12802 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
12803 break;
12804 case FLASH_57780VENDOR_ATMEL_AT45DB041D:
12805 case FLASH_57780VENDOR_ATMEL_AT45DB041B:
12806 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
12807 break;
12808 }
12809 break;
12810 case FLASH_5752VENDOR_ST_M45PE10:
12811 case FLASH_5752VENDOR_ST_M45PE20:
12812 case FLASH_5752VENDOR_ST_M45PE40:
12813 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000012814 tg3_flag_set(tp, NVRAM_BUFFERED);
12815 tg3_flag_set(tp, FLASH);
Matt Carlson321d32a2008-11-21 17:22:19 -080012816
12817 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
12818 case FLASH_5752VENDOR_ST_M45PE10:
12819 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
12820 break;
12821 case FLASH_5752VENDOR_ST_M45PE20:
12822 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
12823 break;
12824 case FLASH_5752VENDOR_ST_M45PE40:
12825 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
12826 break;
12827 }
12828 break;
12829 default:
Joe Perches63c3a662011-04-26 08:12:10 +000012830 tg3_flag_set(tp, NO_NVRAM);
Matt Carlson321d32a2008-11-21 17:22:19 -080012831 return;
12832 }
12833
Matt Carlsona1b950d2009-09-01 13:20:17 +000012834 tg3_nvram_get_pagesize(tp, nvcfg1);
12835 if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
Joe Perches63c3a662011-04-26 08:12:10 +000012836 tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
Matt Carlsona1b950d2009-09-01 13:20:17 +000012837}
12838
12839
12840static void __devinit tg3_get_5717_nvram_info(struct tg3 *tp)
12841{
12842 u32 nvcfg1;
12843
12844 nvcfg1 = tr32(NVRAM_CFG1);
12845
12846 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
12847 case FLASH_5717VENDOR_ATMEL_EEPROM:
12848 case FLASH_5717VENDOR_MICRO_EEPROM:
12849 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012850 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlsona1b950d2009-09-01 13:20:17 +000012851 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
12852
12853 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
12854 tw32(NVRAM_CFG1, nvcfg1);
12855 return;
12856 case FLASH_5717VENDOR_ATMEL_MDB011D:
12857 case FLASH_5717VENDOR_ATMEL_ADB011B:
12858 case FLASH_5717VENDOR_ATMEL_ADB011D:
12859 case FLASH_5717VENDOR_ATMEL_MDB021D:
12860 case FLASH_5717VENDOR_ATMEL_ADB021B:
12861 case FLASH_5717VENDOR_ATMEL_ADB021D:
12862 case FLASH_5717VENDOR_ATMEL_45USPT:
12863 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012864 tg3_flag_set(tp, NVRAM_BUFFERED);
12865 tg3_flag_set(tp, FLASH);
Matt Carlsona1b950d2009-09-01 13:20:17 +000012866
12867 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
12868 case FLASH_5717VENDOR_ATMEL_MDB021D:
Matt Carlson66ee33b2011-04-05 14:22:51 +000012869 /* Detect size with tg3_nvram_get_size() */
12870 break;
Matt Carlsona1b950d2009-09-01 13:20:17 +000012871 case FLASH_5717VENDOR_ATMEL_ADB021B:
12872 case FLASH_5717VENDOR_ATMEL_ADB021D:
12873 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
12874 break;
12875 default:
12876 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
12877 break;
12878 }
Matt Carlson321d32a2008-11-21 17:22:19 -080012879 break;
Matt Carlsona1b950d2009-09-01 13:20:17 +000012880 case FLASH_5717VENDOR_ST_M_M25PE10:
12881 case FLASH_5717VENDOR_ST_A_M25PE10:
12882 case FLASH_5717VENDOR_ST_M_M45PE10:
12883 case FLASH_5717VENDOR_ST_A_M45PE10:
12884 case FLASH_5717VENDOR_ST_M_M25PE20:
12885 case FLASH_5717VENDOR_ST_A_M25PE20:
12886 case FLASH_5717VENDOR_ST_M_M45PE20:
12887 case FLASH_5717VENDOR_ST_A_M45PE20:
12888 case FLASH_5717VENDOR_ST_25USPT:
12889 case FLASH_5717VENDOR_ST_45USPT:
12890 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000012891 tg3_flag_set(tp, NVRAM_BUFFERED);
12892 tg3_flag_set(tp, FLASH);
Matt Carlsona1b950d2009-09-01 13:20:17 +000012893
12894 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
12895 case FLASH_5717VENDOR_ST_M_M25PE20:
Matt Carlsona1b950d2009-09-01 13:20:17 +000012896 case FLASH_5717VENDOR_ST_M_M45PE20:
Matt Carlson66ee33b2011-04-05 14:22:51 +000012897 /* Detect size with tg3_nvram_get_size() */
12898 break;
12899 case FLASH_5717VENDOR_ST_A_M25PE20:
Matt Carlsona1b950d2009-09-01 13:20:17 +000012900 case FLASH_5717VENDOR_ST_A_M45PE20:
12901 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
12902 break;
12903 default:
12904 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
12905 break;
12906 }
Matt Carlson321d32a2008-11-21 17:22:19 -080012907 break;
Matt Carlsona1b950d2009-09-01 13:20:17 +000012908 default:
Joe Perches63c3a662011-04-26 08:12:10 +000012909 tg3_flag_set(tp, NO_NVRAM);
Matt Carlsona1b950d2009-09-01 13:20:17 +000012910 return;
Matt Carlson321d32a2008-11-21 17:22:19 -080012911 }
Matt Carlsona1b950d2009-09-01 13:20:17 +000012912
12913 tg3_nvram_get_pagesize(tp, nvcfg1);
12914 if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
Joe Perches63c3a662011-04-26 08:12:10 +000012915 tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
Matt Carlson321d32a2008-11-21 17:22:19 -080012916}
12917
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012918static void __devinit tg3_get_5720_nvram_info(struct tg3 *tp)
12919{
12920 u32 nvcfg1, nvmpinstrp;
12921
12922 nvcfg1 = tr32(NVRAM_CFG1);
12923 nvmpinstrp = nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK;
12924
12925 switch (nvmpinstrp) {
12926 case FLASH_5720_EEPROM_HD:
12927 case FLASH_5720_EEPROM_LD:
12928 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012929 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012930
12931 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
12932 tw32(NVRAM_CFG1, nvcfg1);
12933 if (nvmpinstrp == FLASH_5720_EEPROM_HD)
12934 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
12935 else
12936 tp->nvram_pagesize = ATMEL_AT24C02_CHIP_SIZE;
12937 return;
12938 case FLASH_5720VENDOR_M_ATMEL_DB011D:
12939 case FLASH_5720VENDOR_A_ATMEL_DB011B:
12940 case FLASH_5720VENDOR_A_ATMEL_DB011D:
12941 case FLASH_5720VENDOR_M_ATMEL_DB021D:
12942 case FLASH_5720VENDOR_A_ATMEL_DB021B:
12943 case FLASH_5720VENDOR_A_ATMEL_DB021D:
12944 case FLASH_5720VENDOR_M_ATMEL_DB041D:
12945 case FLASH_5720VENDOR_A_ATMEL_DB041B:
12946 case FLASH_5720VENDOR_A_ATMEL_DB041D:
12947 case FLASH_5720VENDOR_M_ATMEL_DB081D:
12948 case FLASH_5720VENDOR_A_ATMEL_DB081D:
12949 case FLASH_5720VENDOR_ATMEL_45USPT:
12950 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012951 tg3_flag_set(tp, NVRAM_BUFFERED);
12952 tg3_flag_set(tp, FLASH);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012953
12954 switch (nvmpinstrp) {
12955 case FLASH_5720VENDOR_M_ATMEL_DB021D:
12956 case FLASH_5720VENDOR_A_ATMEL_DB021B:
12957 case FLASH_5720VENDOR_A_ATMEL_DB021D:
12958 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
12959 break;
12960 case FLASH_5720VENDOR_M_ATMEL_DB041D:
12961 case FLASH_5720VENDOR_A_ATMEL_DB041B:
12962 case FLASH_5720VENDOR_A_ATMEL_DB041D:
12963 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
12964 break;
12965 case FLASH_5720VENDOR_M_ATMEL_DB081D:
12966 case FLASH_5720VENDOR_A_ATMEL_DB081D:
12967 tp->nvram_size = TG3_NVRAM_SIZE_1MB;
12968 break;
12969 default:
12970 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
12971 break;
12972 }
12973 break;
12974 case FLASH_5720VENDOR_M_ST_M25PE10:
12975 case FLASH_5720VENDOR_M_ST_M45PE10:
12976 case FLASH_5720VENDOR_A_ST_M25PE10:
12977 case FLASH_5720VENDOR_A_ST_M45PE10:
12978 case FLASH_5720VENDOR_M_ST_M25PE20:
12979 case FLASH_5720VENDOR_M_ST_M45PE20:
12980 case FLASH_5720VENDOR_A_ST_M25PE20:
12981 case FLASH_5720VENDOR_A_ST_M45PE20:
12982 case FLASH_5720VENDOR_M_ST_M25PE40:
12983 case FLASH_5720VENDOR_M_ST_M45PE40:
12984 case FLASH_5720VENDOR_A_ST_M25PE40:
12985 case FLASH_5720VENDOR_A_ST_M45PE40:
12986 case FLASH_5720VENDOR_M_ST_M25PE80:
12987 case FLASH_5720VENDOR_M_ST_M45PE80:
12988 case FLASH_5720VENDOR_A_ST_M25PE80:
12989 case FLASH_5720VENDOR_A_ST_M45PE80:
12990 case FLASH_5720VENDOR_ST_25USPT:
12991 case FLASH_5720VENDOR_ST_45USPT:
12992 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000012993 tg3_flag_set(tp, NVRAM_BUFFERED);
12994 tg3_flag_set(tp, FLASH);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012995
12996 switch (nvmpinstrp) {
12997 case FLASH_5720VENDOR_M_ST_M25PE20:
12998 case FLASH_5720VENDOR_M_ST_M45PE20:
12999 case FLASH_5720VENDOR_A_ST_M25PE20:
13000 case FLASH_5720VENDOR_A_ST_M45PE20:
13001 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
13002 break;
13003 case FLASH_5720VENDOR_M_ST_M25PE40:
13004 case FLASH_5720VENDOR_M_ST_M45PE40:
13005 case FLASH_5720VENDOR_A_ST_M25PE40:
13006 case FLASH_5720VENDOR_A_ST_M45PE40:
13007 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
13008 break;
13009 case FLASH_5720VENDOR_M_ST_M25PE80:
13010 case FLASH_5720VENDOR_M_ST_M45PE80:
13011 case FLASH_5720VENDOR_A_ST_M25PE80:
13012 case FLASH_5720VENDOR_A_ST_M45PE80:
13013 tp->nvram_size = TG3_NVRAM_SIZE_1MB;
13014 break;
13015 default:
13016 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
13017 break;
13018 }
13019 break;
13020 default:
Joe Perches63c3a662011-04-26 08:12:10 +000013021 tg3_flag_set(tp, NO_NVRAM);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000013022 return;
13023 }
13024
13025 tg3_nvram_get_pagesize(tp, nvcfg1);
13026 if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
Joe Perches63c3a662011-04-26 08:12:10 +000013027 tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000013028}
13029
Linus Torvalds1da177e2005-04-16 15:20:36 -070013030/* Chips other than 5700/5701 use the NVRAM for fetching info. */
13031static void __devinit tg3_nvram_init(struct tg3 *tp)
13032{
Linus Torvalds1da177e2005-04-16 15:20:36 -070013033 tw32_f(GRC_EEPROM_ADDR,
13034 (EEPROM_ADDR_FSM_RESET |
13035 (EEPROM_DEFAULT_CLOCK_PERIOD <<
13036 EEPROM_ADDR_CLKPERD_SHIFT)));
13037
Michael Chan9d57f012006-12-07 00:23:25 -080013038 msleep(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013039
13040 /* Enable seeprom accesses. */
13041 tw32_f(GRC_LOCAL_CTRL,
13042 tr32(GRC_LOCAL_CTRL) | GRC_LCLCTRL_AUTO_SEEPROM);
13043 udelay(100);
13044
13045 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
13046 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701) {
Joe Perches63c3a662011-04-26 08:12:10 +000013047 tg3_flag_set(tp, NVRAM);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013048
Michael Chanec41c7d2006-01-17 02:40:55 -080013049 if (tg3_nvram_lock(tp)) {
Matt Carlson5129c3a2010-04-05 10:19:23 +000013050 netdev_warn(tp->dev,
13051 "Cannot get nvram lock, %s failed\n",
Joe Perches05dbe002010-02-17 19:44:19 +000013052 __func__);
Michael Chanec41c7d2006-01-17 02:40:55 -080013053 return;
13054 }
Michael Chane6af3012005-04-21 17:12:05 -070013055 tg3_enable_nvram_access(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013056
Matt Carlson989a9d22007-05-05 11:51:05 -070013057 tp->nvram_size = 0;
13058
Michael Chan361b4ac2005-04-21 17:11:21 -070013059 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752)
13060 tg3_get_5752_nvram_info(tp);
Michael Chand3c7b882006-03-23 01:28:25 -080013061 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755)
13062 tg3_get_5755_nvram_info(tp);
Matt Carlsond30cdd22007-10-07 23:28:35 -070013063 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787 ||
Matt Carlson57e69832008-05-25 23:48:31 -070013064 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
13065 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
Michael Chan1b277772006-03-20 22:27:48 -080013066 tg3_get_5787_nvram_info(tp);
Matt Carlson6b91fa02007-10-10 18:01:09 -070013067 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
13068 tg3_get_5761_nvram_info(tp);
Michael Chanb5d37722006-09-27 16:06:21 -070013069 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
13070 tg3_get_5906_nvram_info(tp);
Matt Carlsonb703df62009-12-03 08:36:21 +000013071 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
Matt Carlson55086ad2011-12-14 11:09:59 +000013072 tg3_flag(tp, 57765_CLASS))
Matt Carlson321d32a2008-11-21 17:22:19 -080013073 tg3_get_57780_nvram_info(tp);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000013074 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
13075 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
Matt Carlsona1b950d2009-09-01 13:20:17 +000013076 tg3_get_5717_nvram_info(tp);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000013077 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
13078 tg3_get_5720_nvram_info(tp);
Michael Chan361b4ac2005-04-21 17:11:21 -070013079 else
13080 tg3_get_nvram_info(tp);
13081
Matt Carlson989a9d22007-05-05 11:51:05 -070013082 if (tp->nvram_size == 0)
13083 tg3_get_nvram_size(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013084
Michael Chane6af3012005-04-21 17:12:05 -070013085 tg3_disable_nvram_access(tp);
Michael Chan381291b2005-12-13 21:08:21 -080013086 tg3_nvram_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013087
13088 } else {
Joe Perches63c3a662011-04-26 08:12:10 +000013089 tg3_flag_clear(tp, NVRAM);
13090 tg3_flag_clear(tp, NVRAM_BUFFERED);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013091
13092 tg3_get_eeprom_size(tp);
13093 }
13094}
13095
Linus Torvalds1da177e2005-04-16 15:20:36 -070013096struct subsys_tbl_ent {
13097 u16 subsys_vendor, subsys_devid;
13098 u32 phy_id;
13099};
13100
Matt Carlson24daf2b2010-02-17 15:17:02 +000013101static struct subsys_tbl_ent subsys_id_to_phy_id[] __devinitdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070013102 /* Broadcom boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000013103 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000013104 TG3PCI_SUBDEVICE_ID_BROADCOM_95700A6, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013105 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000013106 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A5, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013107 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000013108 TG3PCI_SUBDEVICE_ID_BROADCOM_95700T6, TG3_PHY_ID_BCM8002 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013109 { TG3PCI_SUBVENDOR_ID_BROADCOM,
13110 TG3PCI_SUBDEVICE_ID_BROADCOM_95700A9, 0 },
13111 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000013112 TG3PCI_SUBDEVICE_ID_BROADCOM_95701T1, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013113 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000013114 TG3PCI_SUBDEVICE_ID_BROADCOM_95701T8, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013115 { TG3PCI_SUBVENDOR_ID_BROADCOM,
13116 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A7, 0 },
13117 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000013118 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A10, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013119 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000013120 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A12, 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_95703AX1, TG3_PHY_ID_BCM5703 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013123 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000013124 TG3PCI_SUBDEVICE_ID_BROADCOM_95703AX2, TG3_PHY_ID_BCM5703 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070013125
13126 /* 3com boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000013127 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000013128 TG3PCI_SUBDEVICE_ID_3COM_3C996T, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013129 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000013130 TG3PCI_SUBDEVICE_ID_3COM_3C996BT, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013131 { TG3PCI_SUBVENDOR_ID_3COM,
13132 TG3PCI_SUBDEVICE_ID_3COM_3C996SX, 0 },
13133 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000013134 TG3PCI_SUBDEVICE_ID_3COM_3C1000T, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013135 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000013136 TG3PCI_SUBDEVICE_ID_3COM_3C940BR01, TG3_PHY_ID_BCM5701 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070013137
13138 /* DELL boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000013139 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000013140 TG3PCI_SUBDEVICE_ID_DELL_VIPER, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013141 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000013142 TG3PCI_SUBDEVICE_ID_DELL_JAGUAR, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013143 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000013144 TG3PCI_SUBDEVICE_ID_DELL_MERLOT, TG3_PHY_ID_BCM5411 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013145 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000013146 TG3PCI_SUBDEVICE_ID_DELL_SLIM_MERLOT, TG3_PHY_ID_BCM5411 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070013147
13148 /* Compaq boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000013149 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000013150 TG3PCI_SUBDEVICE_ID_COMPAQ_BANSHEE, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013151 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000013152 TG3PCI_SUBDEVICE_ID_COMPAQ_BANSHEE_2, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013153 { TG3PCI_SUBVENDOR_ID_COMPAQ,
13154 TG3PCI_SUBDEVICE_ID_COMPAQ_CHANGELING, 0 },
13155 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000013156 TG3PCI_SUBDEVICE_ID_COMPAQ_NC7780, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013157 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000013158 TG3PCI_SUBDEVICE_ID_COMPAQ_NC7780_2, TG3_PHY_ID_BCM5701 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070013159
13160 /* IBM boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000013161 { TG3PCI_SUBVENDOR_ID_IBM,
13162 TG3PCI_SUBDEVICE_ID_IBM_5703SAX2, 0 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013163};
13164
Matt Carlson24daf2b2010-02-17 15:17:02 +000013165static struct subsys_tbl_ent * __devinit tg3_lookup_by_subsys(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013166{
13167 int i;
13168
13169 for (i = 0; i < ARRAY_SIZE(subsys_id_to_phy_id); i++) {
13170 if ((subsys_id_to_phy_id[i].subsys_vendor ==
13171 tp->pdev->subsystem_vendor) &&
13172 (subsys_id_to_phy_id[i].subsys_devid ==
13173 tp->pdev->subsystem_device))
13174 return &subsys_id_to_phy_id[i];
13175 }
13176 return NULL;
13177}
13178
Michael Chan7d0c41e2005-04-21 17:06:20 -070013179static void __devinit tg3_get_eeprom_hw_cfg(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013180{
Linus Torvalds1da177e2005-04-16 15:20:36 -070013181 u32 val;
David S. Millerf49639e2006-06-09 11:58:36 -070013182
Matt Carlson79eb6902010-02-17 15:17:03 +000013183 tp->phy_id = TG3_PHY_ID_INVALID;
Michael Chan7d0c41e2005-04-21 17:06:20 -070013184 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
13185
Gary Zambranoa85feb82007-05-05 11:52:19 -070013186 /* Assume an onboard device and WOL capable by default. */
Joe Perches63c3a662011-04-26 08:12:10 +000013187 tg3_flag_set(tp, EEPROM_WRITE_PROT);
13188 tg3_flag_set(tp, WOL_CAP);
David S. Miller72b845e2006-03-14 14:11:48 -080013189
Michael Chanb5d37722006-09-27 16:06:21 -070013190 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan9d26e212006-12-07 00:21:14 -080013191 if (!(tr32(PCIE_TRANSACTION_CFG) & PCIE_TRANS_CFG_LOM)) {
Joe Perches63c3a662011-04-26 08:12:10 +000013192 tg3_flag_clear(tp, EEPROM_WRITE_PROT);
13193 tg3_flag_set(tp, IS_NIC);
Michael Chan9d26e212006-12-07 00:21:14 -080013194 }
Matt Carlson0527ba32007-10-10 18:03:30 -070013195 val = tr32(VCPU_CFGSHDW);
13196 if (val & VCPU_CFGSHDW_ASPM_DBNC)
Joe Perches63c3a662011-04-26 08:12:10 +000013197 tg3_flag_set(tp, ASPM_WORKAROUND);
Matt Carlson0527ba32007-10-10 18:03:30 -070013198 if ((val & VCPU_CFGSHDW_WOL_ENABLE) &&
Rafael J. Wysocki6fdbab92011-04-28 11:02:15 +000013199 (val & VCPU_CFGSHDW_WOL_MAGPKT)) {
Joe Perches63c3a662011-04-26 08:12:10 +000013200 tg3_flag_set(tp, WOL_ENABLE);
Rafael J. Wysocki6fdbab92011-04-28 11:02:15 +000013201 device_set_wakeup_enable(&tp->pdev->dev, true);
13202 }
Matt Carlson05ac4cb2008-11-03 16:53:46 -080013203 goto done;
Michael Chanb5d37722006-09-27 16:06:21 -070013204 }
13205
Linus Torvalds1da177e2005-04-16 15:20:36 -070013206 tg3_read_mem(tp, NIC_SRAM_DATA_SIG, &val);
13207 if (val == NIC_SRAM_DATA_SIG_MAGIC) {
13208 u32 nic_cfg, led_cfg;
Matt Carlsona9daf362008-05-25 23:49:44 -070013209 u32 nic_phy_id, ver, cfg2 = 0, cfg4 = 0, eeprom_phy_id;
Michael Chan7d0c41e2005-04-21 17:06:20 -070013210 int eeprom_phy_serdes = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013211
13212 tg3_read_mem(tp, NIC_SRAM_DATA_CFG, &nic_cfg);
13213 tp->nic_sram_data_cfg = nic_cfg;
13214
13215 tg3_read_mem(tp, NIC_SRAM_DATA_VER, &ver);
13216 ver >>= NIC_SRAM_DATA_VER_SHIFT;
Matt Carlson6ff6f812011-05-19 12:12:54 +000013217 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
13218 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701 &&
13219 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5703 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070013220 (ver > 0) && (ver < 0x100))
13221 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_2, &cfg2);
13222
Matt Carlsona9daf362008-05-25 23:49:44 -070013223 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
13224 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_4, &cfg4);
13225
Linus Torvalds1da177e2005-04-16 15:20:36 -070013226 if ((nic_cfg & NIC_SRAM_DATA_CFG_PHY_TYPE_MASK) ==
13227 NIC_SRAM_DATA_CFG_PHY_TYPE_FIBER)
13228 eeprom_phy_serdes = 1;
13229
13230 tg3_read_mem(tp, NIC_SRAM_DATA_PHY_ID, &nic_phy_id);
13231 if (nic_phy_id != 0) {
13232 u32 id1 = nic_phy_id & NIC_SRAM_DATA_PHY_ID1_MASK;
13233 u32 id2 = nic_phy_id & NIC_SRAM_DATA_PHY_ID2_MASK;
13234
13235 eeprom_phy_id = (id1 >> 16) << 10;
13236 eeprom_phy_id |= (id2 & 0xfc00) << 16;
13237 eeprom_phy_id |= (id2 & 0x03ff) << 0;
13238 } else
13239 eeprom_phy_id = 0;
13240
Michael Chan7d0c41e2005-04-21 17:06:20 -070013241 tp->phy_id = eeprom_phy_id;
Michael Chan747e8f82005-07-25 12:33:22 -070013242 if (eeprom_phy_serdes) {
Joe Perches63c3a662011-04-26 08:12:10 +000013243 if (!tg3_flag(tp, 5705_PLUS))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013244 tp->phy_flags |= TG3_PHYFLG_PHY_SERDES;
Matt Carlsona50d0792010-06-05 17:24:37 +000013245 else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013246 tp->phy_flags |= TG3_PHYFLG_MII_SERDES;
Michael Chan747e8f82005-07-25 12:33:22 -070013247 }
Michael Chan7d0c41e2005-04-21 17:06:20 -070013248
Joe Perches63c3a662011-04-26 08:12:10 +000013249 if (tg3_flag(tp, 5750_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -070013250 led_cfg = cfg2 & (NIC_SRAM_DATA_CFG_LED_MODE_MASK |
13251 SHASTA_EXT_LED_MODE_MASK);
John W. Linvillecbf46852005-04-21 17:01:29 -070013252 else
Linus Torvalds1da177e2005-04-16 15:20:36 -070013253 led_cfg = nic_cfg & NIC_SRAM_DATA_CFG_LED_MODE_MASK;
13254
13255 switch (led_cfg) {
13256 default:
13257 case NIC_SRAM_DATA_CFG_LED_MODE_PHY_1:
13258 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
13259 break;
13260
13261 case NIC_SRAM_DATA_CFG_LED_MODE_PHY_2:
13262 tp->led_ctrl = LED_CTRL_MODE_PHY_2;
13263 break;
13264
13265 case NIC_SRAM_DATA_CFG_LED_MODE_MAC:
13266 tp->led_ctrl = LED_CTRL_MODE_MAC;
Michael Chan9ba27792005-06-06 15:16:20 -070013267
13268 /* Default to PHY_1_MODE if 0 (MAC_MODE) is
13269 * read on some older 5700/5701 bootcode.
13270 */
13271 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
13272 ASIC_REV_5700 ||
13273 GET_ASIC_REV(tp->pci_chip_rev_id) ==
13274 ASIC_REV_5701)
13275 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
13276
Linus Torvalds1da177e2005-04-16 15:20:36 -070013277 break;
13278
13279 case SHASTA_EXT_LED_SHARED:
13280 tp->led_ctrl = LED_CTRL_MODE_SHARED;
13281 if (tp->pci_chip_rev_id != CHIPREV_ID_5750_A0 &&
13282 tp->pci_chip_rev_id != CHIPREV_ID_5750_A1)
13283 tp->led_ctrl |= (LED_CTRL_MODE_PHY_1 |
13284 LED_CTRL_MODE_PHY_2);
13285 break;
13286
13287 case SHASTA_EXT_LED_MAC:
13288 tp->led_ctrl = LED_CTRL_MODE_SHASTA_MAC;
13289 break;
13290
13291 case SHASTA_EXT_LED_COMBO:
13292 tp->led_ctrl = LED_CTRL_MODE_COMBO;
13293 if (tp->pci_chip_rev_id != CHIPREV_ID_5750_A0)
13294 tp->led_ctrl |= (LED_CTRL_MODE_PHY_1 |
13295 LED_CTRL_MODE_PHY_2);
13296 break;
13297
Stephen Hemminger855e1112008-04-16 16:37:28 -070013298 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013299
13300 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
13301 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) &&
13302 tp->pdev->subsystem_vendor == PCI_VENDOR_ID_DELL)
13303 tp->led_ctrl = LED_CTRL_MODE_PHY_2;
13304
Matt Carlsonb2a5c192008-04-03 21:44:44 -070013305 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX)
13306 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
Matt Carlson5f608912007-11-12 21:17:07 -080013307
Michael Chan9d26e212006-12-07 00:21:14 -080013308 if (nic_cfg & NIC_SRAM_DATA_CFG_EEPROM_WP) {
Joe Perches63c3a662011-04-26 08:12:10 +000013309 tg3_flag_set(tp, EEPROM_WRITE_PROT);
Michael Chan9d26e212006-12-07 00:21:14 -080013310 if ((tp->pdev->subsystem_vendor ==
13311 PCI_VENDOR_ID_ARIMA) &&
13312 (tp->pdev->subsystem_device == 0x205a ||
13313 tp->pdev->subsystem_device == 0x2063))
Joe Perches63c3a662011-04-26 08:12:10 +000013314 tg3_flag_clear(tp, EEPROM_WRITE_PROT);
Michael Chan9d26e212006-12-07 00:21:14 -080013315 } else {
Joe Perches63c3a662011-04-26 08:12:10 +000013316 tg3_flag_clear(tp, EEPROM_WRITE_PROT);
13317 tg3_flag_set(tp, IS_NIC);
Michael Chan9d26e212006-12-07 00:21:14 -080013318 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013319
13320 if (nic_cfg & NIC_SRAM_DATA_CFG_ASF_ENABLE) {
Joe Perches63c3a662011-04-26 08:12:10 +000013321 tg3_flag_set(tp, ENABLE_ASF);
13322 if (tg3_flag(tp, 5750_PLUS))
13323 tg3_flag_set(tp, ASF_NEW_HANDSHAKE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013324 }
Matt Carlsonb2b98d42008-11-03 16:52:32 -080013325
13326 if ((nic_cfg & NIC_SRAM_DATA_CFG_APE_ENABLE) &&
Joe Perches63c3a662011-04-26 08:12:10 +000013327 tg3_flag(tp, 5750_PLUS))
13328 tg3_flag_set(tp, ENABLE_APE);
Matt Carlsonb2b98d42008-11-03 16:52:32 -080013329
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013330 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES &&
Gary Zambranoa85feb82007-05-05 11:52:19 -070013331 !(nic_cfg & NIC_SRAM_DATA_CFG_FIBER_WOL))
Joe Perches63c3a662011-04-26 08:12:10 +000013332 tg3_flag_clear(tp, WOL_CAP);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013333
Joe Perches63c3a662011-04-26 08:12:10 +000013334 if (tg3_flag(tp, WOL_CAP) &&
Rafael J. Wysocki6fdbab92011-04-28 11:02:15 +000013335 (nic_cfg & NIC_SRAM_DATA_CFG_WOL_ENABLE)) {
Joe Perches63c3a662011-04-26 08:12:10 +000013336 tg3_flag_set(tp, WOL_ENABLE);
Rafael J. Wysocki6fdbab92011-04-28 11:02:15 +000013337 device_set_wakeup_enable(&tp->pdev->dev, true);
13338 }
Matt Carlson0527ba32007-10-10 18:03:30 -070013339
Linus Torvalds1da177e2005-04-16 15:20:36 -070013340 if (cfg2 & (1 << 17))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013341 tp->phy_flags |= TG3_PHYFLG_CAPACITIVE_COUPLING;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013342
13343 /* serdes signal pre-emphasis in register 0x590 set by */
13344 /* bootcode if bit 18 is set */
13345 if (cfg2 & (1 << 18))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013346 tp->phy_flags |= TG3_PHYFLG_SERDES_PREEMPHASIS;
Matt Carlson8ed5d972007-05-07 00:25:49 -070013347
Joe Perches63c3a662011-04-26 08:12:10 +000013348 if ((tg3_flag(tp, 57765_PLUS) ||
13349 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
13350 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX)) &&
Matt Carlson6833c042008-11-21 17:18:59 -080013351 (cfg2 & NIC_SRAM_DATA_CFG_2_APD_EN))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013352 tp->phy_flags |= TG3_PHYFLG_ENABLE_APD;
Matt Carlson6833c042008-11-21 17:18:59 -080013353
Joe Perches63c3a662011-04-26 08:12:10 +000013354 if (tg3_flag(tp, PCI_EXPRESS) &&
Matt Carlson8c69b1e2010-08-02 11:26:00 +000013355 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
Joe Perches63c3a662011-04-26 08:12:10 +000013356 !tg3_flag(tp, 57765_PLUS)) {
Matt Carlson8ed5d972007-05-07 00:25:49 -070013357 u32 cfg3;
13358
13359 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_3, &cfg3);
13360 if (cfg3 & NIC_SRAM_ASPM_DEBOUNCE)
Joe Perches63c3a662011-04-26 08:12:10 +000013361 tg3_flag_set(tp, ASPM_WORKAROUND);
Matt Carlson8ed5d972007-05-07 00:25:49 -070013362 }
Matt Carlsona9daf362008-05-25 23:49:44 -070013363
Matt Carlson14417062010-02-17 15:16:59 +000013364 if (cfg4 & NIC_SRAM_RGMII_INBAND_DISABLE)
Joe Perches63c3a662011-04-26 08:12:10 +000013365 tg3_flag_set(tp, RGMII_INBAND_DISABLE);
Matt Carlsona9daf362008-05-25 23:49:44 -070013366 if (cfg4 & NIC_SRAM_RGMII_EXT_IBND_RX_EN)
Joe Perches63c3a662011-04-26 08:12:10 +000013367 tg3_flag_set(tp, RGMII_EXT_IBND_RX_EN);
Matt Carlsona9daf362008-05-25 23:49:44 -070013368 if (cfg4 & NIC_SRAM_RGMII_EXT_IBND_TX_EN)
Joe Perches63c3a662011-04-26 08:12:10 +000013369 tg3_flag_set(tp, RGMII_EXT_IBND_TX_EN);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013370 }
Matt Carlson05ac4cb2008-11-03 16:53:46 -080013371done:
Joe Perches63c3a662011-04-26 08:12:10 +000013372 if (tg3_flag(tp, WOL_CAP))
Rafael J. Wysocki43067ed2011-02-10 06:53:09 +000013373 device_set_wakeup_enable(&tp->pdev->dev,
Joe Perches63c3a662011-04-26 08:12:10 +000013374 tg3_flag(tp, WOL_ENABLE));
Rafael J. Wysocki43067ed2011-02-10 06:53:09 +000013375 else
13376 device_set_wakeup_capable(&tp->pdev->dev, false);
Michael Chan7d0c41e2005-04-21 17:06:20 -070013377}
13378
Matt Carlsonb2a5c192008-04-03 21:44:44 -070013379static int __devinit tg3_issue_otp_command(struct tg3 *tp, u32 cmd)
13380{
13381 int i;
13382 u32 val;
13383
13384 tw32(OTP_CTRL, cmd | OTP_CTRL_OTP_CMD_START);
13385 tw32(OTP_CTRL, cmd);
13386
13387 /* Wait for up to 1 ms for command to execute. */
13388 for (i = 0; i < 100; i++) {
13389 val = tr32(OTP_STATUS);
13390 if (val & OTP_STATUS_CMD_DONE)
13391 break;
13392 udelay(10);
13393 }
13394
13395 return (val & OTP_STATUS_CMD_DONE) ? 0 : -EBUSY;
13396}
13397
13398/* Read the gphy configuration from the OTP region of the chip. The gphy
13399 * configuration is a 32-bit value that straddles the alignment boundary.
13400 * We do two 32-bit reads and then shift and merge the results.
13401 */
13402static u32 __devinit tg3_read_otp_phycfg(struct tg3 *tp)
13403{
13404 u32 bhalf_otp, thalf_otp;
13405
13406 tw32(OTP_MODE, OTP_MODE_OTP_THRU_GRC);
13407
13408 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_INIT))
13409 return 0;
13410
13411 tw32(OTP_ADDRESS, OTP_ADDRESS_MAGIC1);
13412
13413 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_READ))
13414 return 0;
13415
13416 thalf_otp = tr32(OTP_READ_DATA);
13417
13418 tw32(OTP_ADDRESS, OTP_ADDRESS_MAGIC2);
13419
13420 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_READ))
13421 return 0;
13422
13423 bhalf_otp = tr32(OTP_READ_DATA);
13424
13425 return ((thalf_otp & 0x0000ffff) << 16) | (bhalf_otp >> 16);
13426}
13427
Matt Carlsone256f8a2011-03-09 16:58:24 +000013428static void __devinit tg3_phy_init_link_config(struct tg3 *tp)
13429{
Hiroaki SHIMODA202ff1c2011-11-22 04:05:41 +000013430 u32 adv = ADVERTISED_Autoneg;
Matt Carlsone256f8a2011-03-09 16:58:24 +000013431
13432 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
13433 adv |= ADVERTISED_1000baseT_Half |
13434 ADVERTISED_1000baseT_Full;
13435
13436 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
13437 adv |= ADVERTISED_100baseT_Half |
13438 ADVERTISED_100baseT_Full |
13439 ADVERTISED_10baseT_Half |
13440 ADVERTISED_10baseT_Full |
13441 ADVERTISED_TP;
13442 else
13443 adv |= ADVERTISED_FIBRE;
13444
13445 tp->link_config.advertising = adv;
Matt Carlsone7405222012-02-13 15:20:16 +000013446 tp->link_config.speed = SPEED_UNKNOWN;
13447 tp->link_config.duplex = DUPLEX_UNKNOWN;
Matt Carlsone256f8a2011-03-09 16:58:24 +000013448 tp->link_config.autoneg = AUTONEG_ENABLE;
Matt Carlsone7405222012-02-13 15:20:16 +000013449 tp->link_config.active_speed = SPEED_UNKNOWN;
13450 tp->link_config.active_duplex = DUPLEX_UNKNOWN;
Matt Carlson34655ad2012-02-22 12:35:18 +000013451
13452 tp->old_link = -1;
Matt Carlsone256f8a2011-03-09 16:58:24 +000013453}
13454
Michael Chan7d0c41e2005-04-21 17:06:20 -070013455static int __devinit tg3_phy_probe(struct tg3 *tp)
13456{
13457 u32 hw_phy_id_1, hw_phy_id_2;
13458 u32 hw_phy_id, hw_phy_id_masked;
13459 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013460
Matt Carlsone256f8a2011-03-09 16:58:24 +000013461 /* flow control autonegotiation is default behavior */
Joe Perches63c3a662011-04-26 08:12:10 +000013462 tg3_flag_set(tp, PAUSE_AUTONEG);
Matt Carlsone256f8a2011-03-09 16:58:24 +000013463 tp->link_config.flowctrl = FLOW_CTRL_TX | FLOW_CTRL_RX;
13464
Joe Perches63c3a662011-04-26 08:12:10 +000013465 if (tg3_flag(tp, USE_PHYLIB))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070013466 return tg3_phy_init(tp);
13467
Linus Torvalds1da177e2005-04-16 15:20:36 -070013468 /* Reading the PHY ID register can conflict with ASF
Nick Andrew877d0312009-01-26 11:06:57 +010013469 * firmware access to the PHY hardware.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013470 */
13471 err = 0;
Joe Perches63c3a662011-04-26 08:12:10 +000013472 if (tg3_flag(tp, ENABLE_ASF) || tg3_flag(tp, ENABLE_APE)) {
Matt Carlson79eb6902010-02-17 15:17:03 +000013473 hw_phy_id = hw_phy_id_masked = TG3_PHY_ID_INVALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013474 } else {
13475 /* Now read the physical PHY_ID from the chip and verify
13476 * that it is sane. If it doesn't look good, we fall back
13477 * to either the hard-coded table based PHY_ID and failing
13478 * that the value found in the eeprom area.
13479 */
13480 err |= tg3_readphy(tp, MII_PHYSID1, &hw_phy_id_1);
13481 err |= tg3_readphy(tp, MII_PHYSID2, &hw_phy_id_2);
13482
13483 hw_phy_id = (hw_phy_id_1 & 0xffff) << 10;
13484 hw_phy_id |= (hw_phy_id_2 & 0xfc00) << 16;
13485 hw_phy_id |= (hw_phy_id_2 & 0x03ff) << 0;
13486
Matt Carlson79eb6902010-02-17 15:17:03 +000013487 hw_phy_id_masked = hw_phy_id & TG3_PHY_ID_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013488 }
13489
Matt Carlson79eb6902010-02-17 15:17:03 +000013490 if (!err && TG3_KNOWN_PHY_ID(hw_phy_id_masked)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070013491 tp->phy_id = hw_phy_id;
Matt Carlson79eb6902010-02-17 15:17:03 +000013492 if (hw_phy_id_masked == TG3_PHY_ID_BCM8002)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013493 tp->phy_flags |= TG3_PHYFLG_PHY_SERDES;
Michael Chanda6b2d02005-08-19 12:54:29 -070013494 else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013495 tp->phy_flags &= ~TG3_PHYFLG_PHY_SERDES;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013496 } else {
Matt Carlson79eb6902010-02-17 15:17:03 +000013497 if (tp->phy_id != TG3_PHY_ID_INVALID) {
Michael Chan7d0c41e2005-04-21 17:06:20 -070013498 /* Do nothing, phy ID already set up in
13499 * tg3_get_eeprom_hw_cfg().
13500 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070013501 } else {
13502 struct subsys_tbl_ent *p;
13503
13504 /* No eeprom signature? Try the hardcoded
13505 * subsys device table.
13506 */
Matt Carlson24daf2b2010-02-17 15:17:02 +000013507 p = tg3_lookup_by_subsys(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013508 if (!p)
13509 return -ENODEV;
13510
13511 tp->phy_id = p->phy_id;
13512 if (!tp->phy_id ||
Matt Carlson79eb6902010-02-17 15:17:03 +000013513 tp->phy_id == TG3_PHY_ID_BCM8002)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013514 tp->phy_flags |= TG3_PHYFLG_PHY_SERDES;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013515 }
13516 }
13517
Matt Carlsona6b68da2010-12-06 08:28:52 +000013518 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES) &&
Matt Carlson5baa5e92011-07-20 10:20:53 +000013519 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
13520 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720 ||
13521 (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718 &&
Matt Carlsona6b68da2010-12-06 08:28:52 +000013522 tp->pci_chip_rev_id != CHIPREV_ID_5717_A0) ||
13523 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765 &&
13524 tp->pci_chip_rev_id != CHIPREV_ID_57765_A0)))
Matt Carlson52b02d02010-10-14 10:37:41 +000013525 tp->phy_flags |= TG3_PHYFLG_EEE_CAP;
13526
Matt Carlsone256f8a2011-03-09 16:58:24 +000013527 tg3_phy_init_link_config(tp);
13528
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013529 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES) &&
Joe Perches63c3a662011-04-26 08:12:10 +000013530 !tg3_flag(tp, ENABLE_APE) &&
13531 !tg3_flag(tp, ENABLE_ASF)) {
Matt Carlsone2bf73e2011-12-08 14:40:15 +000013532 u32 bmsr, dummy;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013533
13534 tg3_readphy(tp, MII_BMSR, &bmsr);
13535 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
13536 (bmsr & BMSR_LSTATUS))
13537 goto skip_phy_reset;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040013538
Linus Torvalds1da177e2005-04-16 15:20:36 -070013539 err = tg3_phy_reset(tp);
13540 if (err)
13541 return err;
13542
Matt Carlson42b64a42011-05-19 12:12:49 +000013543 tg3_phy_set_wirespeed(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013544
Matt Carlsone2bf73e2011-12-08 14:40:15 +000013545 if (!tg3_phy_copper_an_config_ok(tp, &dummy)) {
Matt Carlson42b64a42011-05-19 12:12:49 +000013546 tg3_phy_autoneg_cfg(tp, tp->link_config.advertising,
13547 tp->link_config.flowctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013548
13549 tg3_writephy(tp, MII_BMCR,
13550 BMCR_ANENABLE | BMCR_ANRESTART);
13551 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013552 }
13553
13554skip_phy_reset:
Matt Carlson79eb6902010-02-17 15:17:03 +000013555 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070013556 err = tg3_init_5401phy_dsp(tp);
13557 if (err)
13558 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013559
Linus Torvalds1da177e2005-04-16 15:20:36 -070013560 err = tg3_init_5401phy_dsp(tp);
13561 }
13562
Linus Torvalds1da177e2005-04-16 15:20:36 -070013563 return err;
13564}
13565
Matt Carlson184b8902010-04-05 10:19:25 +000013566static void __devinit tg3_read_vpd(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013567{
Matt Carlsona4a8bb12010-09-15 09:00:00 +000013568 u8 *vpd_data;
Matt Carlson4181b2c2010-02-26 14:04:45 +000013569 unsigned int block_end, rosize, len;
Matt Carlson535a4902011-07-20 10:20:56 +000013570 u32 vpdlen;
Matt Carlson184b8902010-04-05 10:19:25 +000013571 int j, i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013572
Matt Carlson535a4902011-07-20 10:20:56 +000013573 vpd_data = (u8 *)tg3_vpd_readblock(tp, &vpdlen);
Matt Carlsona4a8bb12010-09-15 09:00:00 +000013574 if (!vpd_data)
13575 goto out_no_vpd;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013576
Matt Carlson535a4902011-07-20 10:20:56 +000013577 i = pci_vpd_find_tag(vpd_data, 0, vpdlen, PCI_VPD_LRDT_RO_DATA);
Matt Carlson4181b2c2010-02-26 14:04:45 +000013578 if (i < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013579 goto out_not_found;
Matt Carlson4181b2c2010-02-26 14:04:45 +000013580
13581 rosize = pci_vpd_lrdt_size(&vpd_data[i]);
13582 block_end = i + PCI_VPD_LRDT_TAG_SIZE + rosize;
13583 i += PCI_VPD_LRDT_TAG_SIZE;
13584
Matt Carlson535a4902011-07-20 10:20:56 +000013585 if (block_end > vpdlen)
Matt Carlson4181b2c2010-02-26 14:04:45 +000013586 goto out_not_found;
13587
Matt Carlson184b8902010-04-05 10:19:25 +000013588 j = pci_vpd_find_info_keyword(vpd_data, i, rosize,
13589 PCI_VPD_RO_KEYWORD_MFR_ID);
13590 if (j > 0) {
13591 len = pci_vpd_info_field_size(&vpd_data[j]);
13592
13593 j += PCI_VPD_INFO_FLD_HDR_SIZE;
13594 if (j + len > block_end || len != 4 ||
13595 memcmp(&vpd_data[j], "1028", 4))
13596 goto partno;
13597
13598 j = pci_vpd_find_info_keyword(vpd_data, i, rosize,
13599 PCI_VPD_RO_KEYWORD_VENDOR0);
13600 if (j < 0)
13601 goto partno;
13602
13603 len = pci_vpd_info_field_size(&vpd_data[j]);
13604
13605 j += PCI_VPD_INFO_FLD_HDR_SIZE;
13606 if (j + len > block_end)
13607 goto partno;
13608
13609 memcpy(tp->fw_ver, &vpd_data[j], len);
Matt Carlson535a4902011-07-20 10:20:56 +000013610 strncat(tp->fw_ver, " bc ", vpdlen - len - 1);
Matt Carlson184b8902010-04-05 10:19:25 +000013611 }
13612
13613partno:
Matt Carlson4181b2c2010-02-26 14:04:45 +000013614 i = pci_vpd_find_info_keyword(vpd_data, i, rosize,
13615 PCI_VPD_RO_KEYWORD_PARTNO);
13616 if (i < 0)
13617 goto out_not_found;
13618
13619 len = pci_vpd_info_field_size(&vpd_data[i]);
13620
13621 i += PCI_VPD_INFO_FLD_HDR_SIZE;
13622 if (len > TG3_BPN_SIZE ||
Matt Carlson535a4902011-07-20 10:20:56 +000013623 (len + i) > vpdlen)
Matt Carlson4181b2c2010-02-26 14:04:45 +000013624 goto out_not_found;
13625
13626 memcpy(tp->board_part_number, &vpd_data[i], len);
13627
Linus Torvalds1da177e2005-04-16 15:20:36 -070013628out_not_found:
Matt Carlsona4a8bb12010-09-15 09:00:00 +000013629 kfree(vpd_data);
Matt Carlson37a949c2010-09-30 10:34:33 +000013630 if (tp->board_part_number[0])
Matt Carlsona4a8bb12010-09-15 09:00:00 +000013631 return;
13632
13633out_no_vpd:
Matt Carlson37a949c2010-09-30 10:34:33 +000013634 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717) {
13635 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717)
13636 strcpy(tp->board_part_number, "BCM5717");
13637 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718)
13638 strcpy(tp->board_part_number, "BCM5718");
13639 else
13640 goto nomatch;
13641 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780) {
13642 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57780)
13643 strcpy(tp->board_part_number, "BCM57780");
13644 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57760)
13645 strcpy(tp->board_part_number, "BCM57760");
13646 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57790)
13647 strcpy(tp->board_part_number, "BCM57790");
13648 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57788)
13649 strcpy(tp->board_part_number, "BCM57788");
13650 else
13651 goto nomatch;
13652 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765) {
13653 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57761)
13654 strcpy(tp->board_part_number, "BCM57761");
13655 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57765)
13656 strcpy(tp->board_part_number, "BCM57765");
13657 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57781)
13658 strcpy(tp->board_part_number, "BCM57781");
13659 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57785)
13660 strcpy(tp->board_part_number, "BCM57785");
13661 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57791)
13662 strcpy(tp->board_part_number, "BCM57791");
13663 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57795)
13664 strcpy(tp->board_part_number, "BCM57795");
13665 else
13666 goto nomatch;
Matt Carlson55086ad2011-12-14 11:09:59 +000013667 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57766) {
13668 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57762)
13669 strcpy(tp->board_part_number, "BCM57762");
13670 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57766)
13671 strcpy(tp->board_part_number, "BCM57766");
13672 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57782)
13673 strcpy(tp->board_part_number, "BCM57782");
13674 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57786)
13675 strcpy(tp->board_part_number, "BCM57786");
13676 else
13677 goto nomatch;
Matt Carlson37a949c2010-09-30 10:34:33 +000013678 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chanb5d37722006-09-27 16:06:21 -070013679 strcpy(tp->board_part_number, "BCM95906");
Matt Carlson37a949c2010-09-30 10:34:33 +000013680 } else {
13681nomatch:
Michael Chanb5d37722006-09-27 16:06:21 -070013682 strcpy(tp->board_part_number, "none");
Matt Carlson37a949c2010-09-30 10:34:33 +000013683 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013684}
13685
Matt Carlson9c8a6202007-10-21 16:16:08 -070013686static int __devinit tg3_fw_img_is_valid(struct tg3 *tp, u32 offset)
13687{
13688 u32 val;
13689
Matt Carlsone4f34112009-02-25 14:25:00 +000013690 if (tg3_nvram_read(tp, offset, &val) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070013691 (val & 0xfc000000) != 0x0c000000 ||
Matt Carlsone4f34112009-02-25 14:25:00 +000013692 tg3_nvram_read(tp, offset + 4, &val) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070013693 val != 0)
13694 return 0;
13695
13696 return 1;
13697}
13698
Matt Carlsonacd9c112009-02-25 14:26:33 +000013699static void __devinit tg3_read_bc_ver(struct tg3 *tp)
13700{
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013701 u32 val, offset, start, ver_offset;
Matt Carlson75f99362010-04-05 10:19:24 +000013702 int i, dst_off;
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013703 bool newver = false;
Matt Carlsonacd9c112009-02-25 14:26:33 +000013704
13705 if (tg3_nvram_read(tp, 0xc, &offset) ||
13706 tg3_nvram_read(tp, 0x4, &start))
13707 return;
13708
13709 offset = tg3_nvram_logical_addr(tp, offset);
13710
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013711 if (tg3_nvram_read(tp, offset, &val))
Matt Carlsonacd9c112009-02-25 14:26:33 +000013712 return;
13713
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013714 if ((val & 0xfc000000) == 0x0c000000) {
13715 if (tg3_nvram_read(tp, offset + 4, &val))
Matt Carlsonacd9c112009-02-25 14:26:33 +000013716 return;
13717
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013718 if (val == 0)
13719 newver = true;
13720 }
13721
Matt Carlson75f99362010-04-05 10:19:24 +000013722 dst_off = strlen(tp->fw_ver);
13723
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013724 if (newver) {
Matt Carlson75f99362010-04-05 10:19:24 +000013725 if (TG3_VER_SIZE - dst_off < 16 ||
13726 tg3_nvram_read(tp, offset + 8, &ver_offset))
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013727 return;
13728
13729 offset = offset + ver_offset - start;
13730 for (i = 0; i < 16; i += 4) {
13731 __be32 v;
13732 if (tg3_nvram_read_be32(tp, offset + i, &v))
13733 return;
13734
Matt Carlson75f99362010-04-05 10:19:24 +000013735 memcpy(tp->fw_ver + dst_off + i, &v, sizeof(v));
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013736 }
13737 } else {
13738 u32 major, minor;
13739
13740 if (tg3_nvram_read(tp, TG3_NVM_PTREV_BCVER, &ver_offset))
13741 return;
13742
13743 major = (ver_offset & TG3_NVM_BCVER_MAJMSK) >>
13744 TG3_NVM_BCVER_MAJSFT;
13745 minor = ver_offset & TG3_NVM_BCVER_MINMSK;
Matt Carlson75f99362010-04-05 10:19:24 +000013746 snprintf(&tp->fw_ver[dst_off], TG3_VER_SIZE - dst_off,
13747 "v%d.%02d", major, minor);
Matt Carlsonacd9c112009-02-25 14:26:33 +000013748 }
13749}
13750
Matt Carlsona6f6cb12009-02-25 14:27:43 +000013751static void __devinit tg3_read_hwsb_ver(struct tg3 *tp)
13752{
13753 u32 val, major, minor;
13754
13755 /* Use native endian representation */
13756 if (tg3_nvram_read(tp, TG3_NVM_HWSB_CFG1, &val))
13757 return;
13758
13759 major = (val & TG3_NVM_HWSB_CFG1_MAJMSK) >>
13760 TG3_NVM_HWSB_CFG1_MAJSFT;
13761 minor = (val & TG3_NVM_HWSB_CFG1_MINMSK) >>
13762 TG3_NVM_HWSB_CFG1_MINSFT;
13763
13764 snprintf(&tp->fw_ver[0], 32, "sb v%d.%02d", major, minor);
13765}
13766
Matt Carlsondfe00d72008-11-21 17:19:41 -080013767static void __devinit tg3_read_sb_ver(struct tg3 *tp, u32 val)
13768{
13769 u32 offset, major, minor, build;
13770
Matt Carlson75f99362010-04-05 10:19:24 +000013771 strncat(tp->fw_ver, "sb", TG3_VER_SIZE - strlen(tp->fw_ver) - 1);
Matt Carlsondfe00d72008-11-21 17:19:41 -080013772
13773 if ((val & TG3_EEPROM_SB_FORMAT_MASK) != TG3_EEPROM_SB_FORMAT_1)
13774 return;
13775
13776 switch (val & TG3_EEPROM_SB_REVISION_MASK) {
13777 case TG3_EEPROM_SB_REVISION_0:
13778 offset = TG3_EEPROM_SB_F1R0_EDH_OFF;
13779 break;
13780 case TG3_EEPROM_SB_REVISION_2:
13781 offset = TG3_EEPROM_SB_F1R2_EDH_OFF;
13782 break;
13783 case TG3_EEPROM_SB_REVISION_3:
13784 offset = TG3_EEPROM_SB_F1R3_EDH_OFF;
13785 break;
Matt Carlsona4153d42010-02-17 15:16:56 +000013786 case TG3_EEPROM_SB_REVISION_4:
13787 offset = TG3_EEPROM_SB_F1R4_EDH_OFF;
13788 break;
13789 case TG3_EEPROM_SB_REVISION_5:
13790 offset = TG3_EEPROM_SB_F1R5_EDH_OFF;
13791 break;
Matt Carlsonbba226a2010-10-14 10:37:38 +000013792 case TG3_EEPROM_SB_REVISION_6:
13793 offset = TG3_EEPROM_SB_F1R6_EDH_OFF;
13794 break;
Matt Carlsondfe00d72008-11-21 17:19:41 -080013795 default:
13796 return;
13797 }
13798
Matt Carlsone4f34112009-02-25 14:25:00 +000013799 if (tg3_nvram_read(tp, offset, &val))
Matt Carlsondfe00d72008-11-21 17:19:41 -080013800 return;
13801
13802 build = (val & TG3_EEPROM_SB_EDH_BLD_MASK) >>
13803 TG3_EEPROM_SB_EDH_BLD_SHFT;
13804 major = (val & TG3_EEPROM_SB_EDH_MAJ_MASK) >>
13805 TG3_EEPROM_SB_EDH_MAJ_SHFT;
13806 minor = val & TG3_EEPROM_SB_EDH_MIN_MASK;
13807
13808 if (minor > 99 || build > 26)
13809 return;
13810
Matt Carlson75f99362010-04-05 10:19:24 +000013811 offset = strlen(tp->fw_ver);
13812 snprintf(&tp->fw_ver[offset], TG3_VER_SIZE - offset,
13813 " v%d.%02d", major, minor);
Matt Carlsondfe00d72008-11-21 17:19:41 -080013814
13815 if (build > 0) {
Matt Carlson75f99362010-04-05 10:19:24 +000013816 offset = strlen(tp->fw_ver);
13817 if (offset < TG3_VER_SIZE - 1)
13818 tp->fw_ver[offset] = 'a' + build - 1;
Matt Carlsondfe00d72008-11-21 17:19:41 -080013819 }
13820}
13821
Matt Carlsonacd9c112009-02-25 14:26:33 +000013822static void __devinit tg3_read_mgmtfw_ver(struct tg3 *tp)
Michael Chanc4e65752006-03-20 22:29:32 -080013823{
13824 u32 val, offset, start;
Matt Carlsonacd9c112009-02-25 14:26:33 +000013825 int i, vlen;
Matt Carlson9c8a6202007-10-21 16:16:08 -070013826
13827 for (offset = TG3_NVM_DIR_START;
13828 offset < TG3_NVM_DIR_END;
13829 offset += TG3_NVM_DIRENT_SIZE) {
Matt Carlsone4f34112009-02-25 14:25:00 +000013830 if (tg3_nvram_read(tp, offset, &val))
Matt Carlson9c8a6202007-10-21 16:16:08 -070013831 return;
13832
13833 if ((val >> TG3_NVM_DIRTYPE_SHIFT) == TG3_NVM_DIRTYPE_ASFINI)
13834 break;
13835 }
13836
13837 if (offset == TG3_NVM_DIR_END)
13838 return;
13839
Joe Perches63c3a662011-04-26 08:12:10 +000013840 if (!tg3_flag(tp, 5705_PLUS))
Matt Carlson9c8a6202007-10-21 16:16:08 -070013841 start = 0x08000000;
Matt Carlsone4f34112009-02-25 14:25:00 +000013842 else if (tg3_nvram_read(tp, offset - 4, &start))
Matt Carlson9c8a6202007-10-21 16:16:08 -070013843 return;
13844
Matt Carlsone4f34112009-02-25 14:25:00 +000013845 if (tg3_nvram_read(tp, offset + 4, &offset) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070013846 !tg3_fw_img_is_valid(tp, offset) ||
Matt Carlsone4f34112009-02-25 14:25:00 +000013847 tg3_nvram_read(tp, offset + 8, &val))
Matt Carlson9c8a6202007-10-21 16:16:08 -070013848 return;
13849
13850 offset += val - start;
13851
Matt Carlsonacd9c112009-02-25 14:26:33 +000013852 vlen = strlen(tp->fw_ver);
Matt Carlson9c8a6202007-10-21 16:16:08 -070013853
Matt Carlsonacd9c112009-02-25 14:26:33 +000013854 tp->fw_ver[vlen++] = ',';
13855 tp->fw_ver[vlen++] = ' ';
Matt Carlson9c8a6202007-10-21 16:16:08 -070013856
13857 for (i = 0; i < 4; i++) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000013858 __be32 v;
13859 if (tg3_nvram_read_be32(tp, offset, &v))
Matt Carlson9c8a6202007-10-21 16:16:08 -070013860 return;
13861
Al Virob9fc7dc2007-12-17 22:59:57 -080013862 offset += sizeof(v);
Matt Carlson9c8a6202007-10-21 16:16:08 -070013863
Matt Carlsonacd9c112009-02-25 14:26:33 +000013864 if (vlen > TG3_VER_SIZE - sizeof(v)) {
13865 memcpy(&tp->fw_ver[vlen], &v, TG3_VER_SIZE - vlen);
Matt Carlson9c8a6202007-10-21 16:16:08 -070013866 break;
13867 }
13868
Matt Carlsonacd9c112009-02-25 14:26:33 +000013869 memcpy(&tp->fw_ver[vlen], &v, sizeof(v));
13870 vlen += sizeof(v);
Matt Carlson9c8a6202007-10-21 16:16:08 -070013871 }
Matt Carlsonacd9c112009-02-25 14:26:33 +000013872}
13873
Matt Carlson7fd76442009-02-25 14:27:20 +000013874static void __devinit tg3_read_dash_ver(struct tg3 *tp)
13875{
13876 int vlen;
13877 u32 apedata;
Matt Carlsonecc79642010-08-02 11:26:01 +000013878 char *fwtype;
Matt Carlson7fd76442009-02-25 14:27:20 +000013879
Joe Perches63c3a662011-04-26 08:12:10 +000013880 if (!tg3_flag(tp, ENABLE_APE) || !tg3_flag(tp, ENABLE_ASF))
Matt Carlson7fd76442009-02-25 14:27:20 +000013881 return;
13882
13883 apedata = tg3_ape_read32(tp, TG3_APE_SEG_SIG);
13884 if (apedata != APE_SEG_SIG_MAGIC)
13885 return;
13886
13887 apedata = tg3_ape_read32(tp, TG3_APE_FW_STATUS);
13888 if (!(apedata & APE_FW_STATUS_READY))
13889 return;
13890
13891 apedata = tg3_ape_read32(tp, TG3_APE_FW_VERSION);
13892
Matt Carlsondc6d0742010-09-15 08:59:55 +000013893 if (tg3_ape_read32(tp, TG3_APE_FW_FEATURES) & TG3_APE_FW_FEATURE_NCSI) {
Joe Perches63c3a662011-04-26 08:12:10 +000013894 tg3_flag_set(tp, APE_HAS_NCSI);
Matt Carlsonecc79642010-08-02 11:26:01 +000013895 fwtype = "NCSI";
Matt Carlsondc6d0742010-09-15 08:59:55 +000013896 } else {
Matt Carlsonecc79642010-08-02 11:26:01 +000013897 fwtype = "DASH";
Matt Carlsondc6d0742010-09-15 08:59:55 +000013898 }
Matt Carlsonecc79642010-08-02 11:26:01 +000013899
Matt Carlson7fd76442009-02-25 14:27:20 +000013900 vlen = strlen(tp->fw_ver);
13901
Matt Carlsonecc79642010-08-02 11:26:01 +000013902 snprintf(&tp->fw_ver[vlen], TG3_VER_SIZE - vlen, " %s v%d.%d.%d.%d",
13903 fwtype,
Matt Carlson7fd76442009-02-25 14:27:20 +000013904 (apedata & APE_FW_VERSION_MAJMSK) >> APE_FW_VERSION_MAJSFT,
13905 (apedata & APE_FW_VERSION_MINMSK) >> APE_FW_VERSION_MINSFT,
13906 (apedata & APE_FW_VERSION_REVMSK) >> APE_FW_VERSION_REVSFT,
13907 (apedata & APE_FW_VERSION_BLDMSK));
13908}
13909
Matt Carlsonacd9c112009-02-25 14:26:33 +000013910static void __devinit tg3_read_fw_ver(struct tg3 *tp)
13911{
13912 u32 val;
Matt Carlson75f99362010-04-05 10:19:24 +000013913 bool vpd_vers = false;
13914
13915 if (tp->fw_ver[0] != 0)
13916 vpd_vers = true;
Matt Carlsonacd9c112009-02-25 14:26:33 +000013917
Joe Perches63c3a662011-04-26 08:12:10 +000013918 if (tg3_flag(tp, NO_NVRAM)) {
Matt Carlson75f99362010-04-05 10:19:24 +000013919 strcat(tp->fw_ver, "sb");
Matt Carlsondf259d82009-04-20 06:57:14 +000013920 return;
13921 }
13922
Matt Carlsonacd9c112009-02-25 14:26:33 +000013923 if (tg3_nvram_read(tp, 0, &val))
13924 return;
13925
13926 if (val == TG3_EEPROM_MAGIC)
13927 tg3_read_bc_ver(tp);
13928 else if ((val & TG3_EEPROM_MAGIC_FW_MSK) == TG3_EEPROM_MAGIC_FW)
13929 tg3_read_sb_ver(tp, val);
Matt Carlsona6f6cb12009-02-25 14:27:43 +000013930 else if ((val & TG3_EEPROM_MAGIC_HW_MSK) == TG3_EEPROM_MAGIC_HW)
13931 tg3_read_hwsb_ver(tp);
Matt Carlsonacd9c112009-02-25 14:26:33 +000013932 else
13933 return;
13934
Matt Carlsonc9cab242011-07-13 09:27:27 +000013935 if (vpd_vers)
Matt Carlson75f99362010-04-05 10:19:24 +000013936 goto done;
Matt Carlsonacd9c112009-02-25 14:26:33 +000013937
Matt Carlsonc9cab242011-07-13 09:27:27 +000013938 if (tg3_flag(tp, ENABLE_APE)) {
13939 if (tg3_flag(tp, ENABLE_ASF))
13940 tg3_read_dash_ver(tp);
13941 } else if (tg3_flag(tp, ENABLE_ASF)) {
13942 tg3_read_mgmtfw_ver(tp);
13943 }
Matt Carlson9c8a6202007-10-21 16:16:08 -070013944
Matt Carlson75f99362010-04-05 10:19:24 +000013945done:
Matt Carlson9c8a6202007-10-21 16:16:08 -070013946 tp->fw_ver[TG3_VER_SIZE - 1] = 0;
Michael Chanc4e65752006-03-20 22:29:32 -080013947}
13948
Matt Carlson7cb32cf2010-09-30 10:34:36 +000013949static inline u32 tg3_rx_ret_ring_size(struct tg3 *tp)
13950{
Joe Perches63c3a662011-04-26 08:12:10 +000013951 if (tg3_flag(tp, LRG_PROD_RING_CAP))
Matt Carlsonde9f5232011-04-05 14:22:43 +000013952 return TG3_RX_RET_MAX_SIZE_5717;
Joe Perches63c3a662011-04-26 08:12:10 +000013953 else if (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS))
Matt Carlsonde9f5232011-04-05 14:22:43 +000013954 return TG3_RX_RET_MAX_SIZE_5700;
Matt Carlson7cb32cf2010-09-30 10:34:36 +000013955 else
Matt Carlsonde9f5232011-04-05 14:22:43 +000013956 return TG3_RX_RET_MAX_SIZE_5705;
Matt Carlson7cb32cf2010-09-30 10:34:36 +000013957}
13958
Matt Carlson41434702011-03-09 16:58:22 +000013959static DEFINE_PCI_DEVICE_TABLE(tg3_write_reorder_chipsets) = {
Joe Perches895950c2010-12-21 02:16:08 -080013960 { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_FE_GATE_700C) },
13961 { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_8131_BRIDGE) },
13962 { PCI_DEVICE(PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_8385_0) },
13963 { },
13964};
13965
Matt Carlson16c7fa72012-02-13 10:20:10 +000013966static struct pci_dev * __devinit tg3_find_peer(struct tg3 *tp)
13967{
13968 struct pci_dev *peer;
13969 unsigned int func, devnr = tp->pdev->devfn & ~7;
13970
13971 for (func = 0; func < 8; func++) {
13972 peer = pci_get_slot(tp->pdev->bus, devnr | func);
13973 if (peer && peer != tp->pdev)
13974 break;
13975 pci_dev_put(peer);
13976 }
13977 /* 5704 can be configured in single-port mode, set peer to
13978 * tp->pdev in that case.
13979 */
13980 if (!peer) {
13981 peer = tp->pdev;
13982 return peer;
13983 }
13984
13985 /*
13986 * We don't need to keep the refcount elevated; there's no way
13987 * to remove one half of this device without removing the other
13988 */
13989 pci_dev_put(peer);
13990
13991 return peer;
13992}
13993
Matt Carlson42b123b2012-02-13 15:20:13 +000013994static void __devinit tg3_detect_asic_rev(struct tg3 *tp, u32 misc_ctrl_reg)
13995{
13996 tp->pci_chip_rev_id = misc_ctrl_reg >> MISC_HOST_CTRL_CHIPREV_SHIFT;
13997 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_USE_PROD_ID_REG) {
13998 u32 reg;
13999
14000 /* All devices that use the alternate
14001 * ASIC REV location have a CPMU.
14002 */
14003 tg3_flag_set(tp, CPMU_PRESENT);
14004
14005 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717 ||
14006 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718 ||
14007 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5719 ||
14008 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5720)
14009 reg = TG3PCI_GEN2_PRODID_ASICREV;
14010 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57781 ||
14011 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57785 ||
14012 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57761 ||
14013 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57765 ||
14014 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57791 ||
14015 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57795 ||
14016 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57762 ||
14017 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57766 ||
14018 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57782 ||
14019 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57786)
14020 reg = TG3PCI_GEN15_PRODID_ASICREV;
14021 else
14022 reg = TG3PCI_PRODID_ASICREV;
14023
14024 pci_read_config_dword(tp->pdev, reg, &tp->pci_chip_rev_id);
14025 }
14026
14027 /* Wrong chip ID in 5752 A0. This code can be removed later
14028 * as A0 is not in production.
14029 */
14030 if (tp->pci_chip_rev_id == CHIPREV_ID_5752_A0_HW)
14031 tp->pci_chip_rev_id = CHIPREV_ID_5752_A0;
14032
14033 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
14034 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
14035 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
14036 tg3_flag_set(tp, 5717_PLUS);
14037
14038 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765 ||
14039 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57766)
14040 tg3_flag_set(tp, 57765_CLASS);
14041
14042 if (tg3_flag(tp, 57765_CLASS) || tg3_flag(tp, 5717_PLUS))
14043 tg3_flag_set(tp, 57765_PLUS);
14044
14045 /* Intentionally exclude ASIC_REV_5906 */
14046 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
14047 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787 ||
14048 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
14049 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
14050 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
14051 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
14052 tg3_flag(tp, 57765_PLUS))
14053 tg3_flag_set(tp, 5755_PLUS);
14054
14055 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780 ||
14056 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714)
14057 tg3_flag_set(tp, 5780_CLASS);
14058
14059 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 ||
14060 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752 ||
14061 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906 ||
14062 tg3_flag(tp, 5755_PLUS) ||
14063 tg3_flag(tp, 5780_CLASS))
14064 tg3_flag_set(tp, 5750_PLUS);
14065
14066 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 ||
14067 tg3_flag(tp, 5750_PLUS))
14068 tg3_flag_set(tp, 5705_PLUS);
14069}
14070
Linus Torvalds1da177e2005-04-16 15:20:36 -070014071static int __devinit tg3_get_invariants(struct tg3 *tp)
14072{
Linus Torvalds1da177e2005-04-16 15:20:36 -070014073 u32 misc_ctrl_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014074 u32 pci_state_reg, grc_misc_cfg;
14075 u32 val;
14076 u16 pci_cmd;
Matt Carlson5e7dfd02008-11-21 17:18:16 -080014077 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014078
Linus Torvalds1da177e2005-04-16 15:20:36 -070014079 /* Force memory write invalidate off. If we leave it on,
14080 * then on 5700_BX chips we have to enable a workaround.
14081 * The workaround is to set the TG3PCI_DMA_RW_CTRL boundary
14082 * to match the cacheline size. The Broadcom driver have this
14083 * workaround but turns MWI off all the times so never uses
14084 * it. This seems to suggest that the workaround is insufficient.
14085 */
14086 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
14087 pci_cmd &= ~PCI_COMMAND_INVALIDATE;
14088 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
14089
Matt Carlson16821282011-07-13 09:27:28 +000014090 /* Important! -- Make sure register accesses are byteswapped
14091 * correctly. Also, for those chips that require it, make
14092 * sure that indirect register accesses are enabled before
14093 * the first operation.
Linus Torvalds1da177e2005-04-16 15:20:36 -070014094 */
14095 pci_read_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
14096 &misc_ctrl_reg);
Matt Carlson16821282011-07-13 09:27:28 +000014097 tp->misc_host_ctrl |= (misc_ctrl_reg &
14098 MISC_HOST_CTRL_CHIPREV);
14099 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
14100 tp->misc_host_ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014101
Matt Carlson42b123b2012-02-13 15:20:13 +000014102 tg3_detect_asic_rev(tp, misc_ctrl_reg);
Michael Chanff645be2005-04-21 17:09:53 -070014103
Michael Chan68929142005-08-09 20:17:14 -070014104 /* If we have 5702/03 A1 or A2 on certain ICH chipsets,
14105 * we need to disable memory and use config. cycles
14106 * only to access all registers. The 5702/03 chips
14107 * can mistakenly decode the special cycles from the
14108 * ICH chipsets as memory write cycles, causing corruption
14109 * of register and memory space. Only certain ICH bridges
14110 * will drive special cycles with non-zero data during the
14111 * address phase which can fall within the 5703's address
14112 * range. This is not an ICH bug as the PCI spec allows
14113 * non-zero address during special cycles. However, only
14114 * these ICH bridges are known to drive non-zero addresses
14115 * during special cycles.
14116 *
14117 * Since special cycles do not cross PCI bridges, we only
14118 * enable this workaround if the 5703 is on the secondary
14119 * bus of these ICH bridges.
14120 */
14121 if ((tp->pci_chip_rev_id == CHIPREV_ID_5703_A1) ||
14122 (tp->pci_chip_rev_id == CHIPREV_ID_5703_A2)) {
14123 static struct tg3_dev_id {
14124 u32 vendor;
14125 u32 device;
14126 u32 rev;
14127 } ich_chipsets[] = {
14128 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AA_8,
14129 PCI_ANY_ID },
14130 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AB_8,
14131 PCI_ANY_ID },
14132 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_11,
14133 0xa },
14134 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_6,
14135 PCI_ANY_ID },
14136 { },
14137 };
14138 struct tg3_dev_id *pci_id = &ich_chipsets[0];
14139 struct pci_dev *bridge = NULL;
14140
14141 while (pci_id->vendor != 0) {
14142 bridge = pci_get_device(pci_id->vendor, pci_id->device,
14143 bridge);
14144 if (!bridge) {
14145 pci_id++;
14146 continue;
14147 }
14148 if (pci_id->rev != PCI_ANY_ID) {
Auke Kok44c10132007-06-08 15:46:36 -070014149 if (bridge->revision > pci_id->rev)
Michael Chan68929142005-08-09 20:17:14 -070014150 continue;
14151 }
14152 if (bridge->subordinate &&
14153 (bridge->subordinate->number ==
14154 tp->pdev->bus->number)) {
Joe Perches63c3a662011-04-26 08:12:10 +000014155 tg3_flag_set(tp, ICH_WORKAROUND);
Michael Chan68929142005-08-09 20:17:14 -070014156 pci_dev_put(bridge);
14157 break;
14158 }
14159 }
14160 }
14161
Matt Carlson6ff6f812011-05-19 12:12:54 +000014162 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
Matt Carlson41588ba2008-04-19 18:12:33 -070014163 static struct tg3_dev_id {
14164 u32 vendor;
14165 u32 device;
14166 } bridge_chipsets[] = {
14167 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PXH_0 },
14168 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PXH_1 },
14169 { },
14170 };
14171 struct tg3_dev_id *pci_id = &bridge_chipsets[0];
14172 struct pci_dev *bridge = NULL;
14173
14174 while (pci_id->vendor != 0) {
14175 bridge = pci_get_device(pci_id->vendor,
14176 pci_id->device,
14177 bridge);
14178 if (!bridge) {
14179 pci_id++;
14180 continue;
14181 }
14182 if (bridge->subordinate &&
14183 (bridge->subordinate->number <=
14184 tp->pdev->bus->number) &&
14185 (bridge->subordinate->subordinate >=
14186 tp->pdev->bus->number)) {
Joe Perches63c3a662011-04-26 08:12:10 +000014187 tg3_flag_set(tp, 5701_DMA_BUG);
Matt Carlson41588ba2008-04-19 18:12:33 -070014188 pci_dev_put(bridge);
14189 break;
14190 }
14191 }
14192 }
14193
Michael Chan4a29cc22006-03-19 13:21:12 -080014194 /* The EPB bridge inside 5714, 5715, and 5780 cannot support
14195 * DMA addresses > 40-bit. This bridge may have other additional
14196 * 57xx devices behind it in some 4-port NIC designs for example.
14197 * Any tg3 device found behind the bridge will also need the 40-bit
14198 * DMA workaround.
14199 */
Matt Carlson42b123b2012-02-13 15:20:13 +000014200 if (tg3_flag(tp, 5780_CLASS)) {
Joe Perches63c3a662011-04-26 08:12:10 +000014201 tg3_flag_set(tp, 40BIT_DMA_BUG);
Michael Chan4cf78e42005-07-25 12:29:19 -070014202 tp->msi_cap = pci_find_capability(tp->pdev, PCI_CAP_ID_MSI);
Matt Carlson859a588792010-04-05 10:19:28 +000014203 } else {
Michael Chan4a29cc22006-03-19 13:21:12 -080014204 struct pci_dev *bridge = NULL;
14205
14206 do {
14207 bridge = pci_get_device(PCI_VENDOR_ID_SERVERWORKS,
14208 PCI_DEVICE_ID_SERVERWORKS_EPB,
14209 bridge);
14210 if (bridge && bridge->subordinate &&
14211 (bridge->subordinate->number <=
14212 tp->pdev->bus->number) &&
14213 (bridge->subordinate->subordinate >=
14214 tp->pdev->bus->number)) {
Joe Perches63c3a662011-04-26 08:12:10 +000014215 tg3_flag_set(tp, 40BIT_DMA_BUG);
Michael Chan4a29cc22006-03-19 13:21:12 -080014216 pci_dev_put(bridge);
14217 break;
14218 }
14219 } while (bridge);
14220 }
Michael Chan4cf78e42005-07-25 12:29:19 -070014221
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000014222 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
Matt Carlson3a1e19d2011-07-13 09:27:32 +000014223 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714)
Michael Chan7544b092007-05-05 13:08:32 -070014224 tp->pdev_peer = tg3_find_peer(tp);
14225
Matt Carlson507399f2009-11-13 13:03:37 +000014226 /* Determine TSO capabilities */
Matt Carlsona0512942011-07-27 14:20:54 +000014227 if (tp->pci_chip_rev_id == CHIPREV_ID_5719_A0)
Matt Carlson4d163b72011-01-25 15:58:48 +000014228 ; /* Do nothing. HW bug. */
Joe Perches63c3a662011-04-26 08:12:10 +000014229 else if (tg3_flag(tp, 57765_PLUS))
14230 tg3_flag_set(tp, HW_TSO_3);
14231 else if (tg3_flag(tp, 5755_PLUS) ||
Matt Carlsone849cdc2009-11-13 13:03:38 +000014232 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
Joe Perches63c3a662011-04-26 08:12:10 +000014233 tg3_flag_set(tp, HW_TSO_2);
14234 else if (tg3_flag(tp, 5750_PLUS)) {
14235 tg3_flag_set(tp, HW_TSO_1);
14236 tg3_flag_set(tp, TSO_BUG);
Matt Carlson507399f2009-11-13 13:03:37 +000014237 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 &&
14238 tp->pci_chip_rev_id >= CHIPREV_ID_5750_C2)
Joe Perches63c3a662011-04-26 08:12:10 +000014239 tg3_flag_clear(tp, TSO_BUG);
Matt Carlson507399f2009-11-13 13:03:37 +000014240 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
14241 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701 &&
14242 tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) {
Joe Perches63c3a662011-04-26 08:12:10 +000014243 tg3_flag_set(tp, TSO_BUG);
Matt Carlson507399f2009-11-13 13:03:37 +000014244 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705)
14245 tp->fw_needed = FIRMWARE_TG3TSO5;
14246 else
14247 tp->fw_needed = FIRMWARE_TG3TSO;
14248 }
14249
Matt Carlsondabc5c62011-05-19 12:12:52 +000014250 /* Selectively allow TSO based on operating conditions */
Matt Carlson6ff6f812011-05-19 12:12:54 +000014251 if (tg3_flag(tp, HW_TSO_1) ||
14252 tg3_flag(tp, HW_TSO_2) ||
14253 tg3_flag(tp, HW_TSO_3) ||
Matt Carlsoncf9ecf42011-11-28 09:41:03 +000014254 tp->fw_needed) {
14255 /* For firmware TSO, assume ASF is disabled.
14256 * We'll disable TSO later if we discover ASF
14257 * is enabled in tg3_get_eeprom_hw_cfg().
14258 */
Matt Carlsondabc5c62011-05-19 12:12:52 +000014259 tg3_flag_set(tp, TSO_CAPABLE);
Matt Carlsoncf9ecf42011-11-28 09:41:03 +000014260 } else {
Matt Carlsondabc5c62011-05-19 12:12:52 +000014261 tg3_flag_clear(tp, TSO_CAPABLE);
14262 tg3_flag_clear(tp, TSO_BUG);
14263 tp->fw_needed = NULL;
14264 }
14265
14266 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0)
14267 tp->fw_needed = FIRMWARE_TG3;
14268
Matt Carlson507399f2009-11-13 13:03:37 +000014269 tp->irq_max = 1;
14270
Joe Perches63c3a662011-04-26 08:12:10 +000014271 if (tg3_flag(tp, 5750_PLUS)) {
14272 tg3_flag_set(tp, SUPPORT_MSI);
Michael Chan7544b092007-05-05 13:08:32 -070014273 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_AX ||
14274 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_BX ||
14275 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714 &&
14276 tp->pci_chip_rev_id <= CHIPREV_ID_5714_A2 &&
14277 tp->pdev_peer == tp->pdev))
Joe Perches63c3a662011-04-26 08:12:10 +000014278 tg3_flag_clear(tp, SUPPORT_MSI);
Michael Chan7544b092007-05-05 13:08:32 -070014279
Joe Perches63c3a662011-04-26 08:12:10 +000014280 if (tg3_flag(tp, 5755_PLUS) ||
Michael Chanb5d37722006-09-27 16:06:21 -070014281 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Joe Perches63c3a662011-04-26 08:12:10 +000014282 tg3_flag_set(tp, 1SHOT_MSI);
Michael Chan52c0fd82006-06-29 20:15:54 -070014283 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014284
Joe Perches63c3a662011-04-26 08:12:10 +000014285 if (tg3_flag(tp, 57765_PLUS)) {
14286 tg3_flag_set(tp, SUPPORT_MSIX);
Matt Carlson507399f2009-11-13 13:03:37 +000014287 tp->irq_max = TG3_IRQ_MAX_VECS;
Matt Carlson90415472011-12-16 13:33:23 +000014288 tg3_rss_init_dflt_indir_tbl(tp);
Matt Carlson507399f2009-11-13 13:03:37 +000014289 }
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000014290 }
Matt Carlson0e1406d2009-11-02 12:33:33 +000014291
Matt Carlson2ffcc982011-05-19 12:12:44 +000014292 if (tg3_flag(tp, 5755_PLUS))
Joe Perches63c3a662011-04-26 08:12:10 +000014293 tg3_flag_set(tp, SHORT_DMA_BUG);
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000014294
Matt Carlsone31aa982011-07-27 14:20:53 +000014295 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
Matt Carlsona4cb4282011-12-14 11:09:58 +000014296 tp->dma_limit = TG3_TX_BD_DMA_MAX_4K;
Matt Carlsone31aa982011-07-27 14:20:53 +000014297
Matt Carlsonfa6b2aa2011-11-21 15:01:19 +000014298 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
14299 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
14300 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
Joe Perches63c3a662011-04-26 08:12:10 +000014301 tg3_flag_set(tp, LRG_PROD_RING_CAP);
Matt Carlsonde9f5232011-04-05 14:22:43 +000014302
Joe Perches63c3a662011-04-26 08:12:10 +000014303 if (tg3_flag(tp, 57765_PLUS) &&
Matt Carlsona0512942011-07-27 14:20:54 +000014304 tp->pci_chip_rev_id != CHIPREV_ID_5719_A0)
Joe Perches63c3a662011-04-26 08:12:10 +000014305 tg3_flag_set(tp, USE_JUMBO_BDFLAG);
Matt Carlsonb703df62009-12-03 08:36:21 +000014306
Joe Perches63c3a662011-04-26 08:12:10 +000014307 if (!tg3_flag(tp, 5705_PLUS) ||
14308 tg3_flag(tp, 5780_CLASS) ||
14309 tg3_flag(tp, USE_JUMBO_BDFLAG))
14310 tg3_flag_set(tp, JUMBO_CAPABLE);
Michael Chan0f893dc2005-07-25 12:30:38 -070014311
Matt Carlson52f44902008-11-21 17:17:04 -080014312 pci_read_config_dword(tp->pdev, TG3PCI_PCISTATE,
14313 &pci_state_reg);
14314
Jon Mason708ebb32011-06-27 12:56:50 +000014315 if (pci_is_pcie(tp->pdev)) {
Matt Carlson5e7dfd02008-11-21 17:18:16 -080014316 u16 lnkctl;
14317
Joe Perches63c3a662011-04-26 08:12:10 +000014318 tg3_flag_set(tp, PCI_EXPRESS);
Matt Carlson5f5c51e2007-11-12 21:19:37 -080014319
Matt Carlson5e7dfd02008-11-21 17:18:16 -080014320 pci_read_config_word(tp->pdev,
Jon Mason708ebb32011-06-27 12:56:50 +000014321 pci_pcie_cap(tp->pdev) + PCI_EXP_LNKCTL,
Matt Carlson5e7dfd02008-11-21 17:18:16 -080014322 &lnkctl);
14323 if (lnkctl & PCI_EXP_LNKCTL_CLKREQ_EN) {
Matt Carlson7196cd62011-05-19 16:02:44 +000014324 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
14325 ASIC_REV_5906) {
Joe Perches63c3a662011-04-26 08:12:10 +000014326 tg3_flag_clear(tp, HW_TSO_2);
Matt Carlsondabc5c62011-05-19 12:12:52 +000014327 tg3_flag_clear(tp, TSO_CAPABLE);
Matt Carlson7196cd62011-05-19 16:02:44 +000014328 }
Matt Carlson5e7dfd02008-11-21 17:18:16 -080014329 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson321d32a2008-11-21 17:22:19 -080014330 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson9cf74eb2009-04-20 06:58:27 +000014331 tp->pci_chip_rev_id == CHIPREV_ID_57780_A0 ||
14332 tp->pci_chip_rev_id == CHIPREV_ID_57780_A1)
Joe Perches63c3a662011-04-26 08:12:10 +000014333 tg3_flag_set(tp, CLKREQ_BUG);
Matt Carlson614b0592010-01-20 16:58:02 +000014334 } else if (tp->pci_chip_rev_id == CHIPREV_ID_5717_A0) {
Joe Perches63c3a662011-04-26 08:12:10 +000014335 tg3_flag_set(tp, L1PLLPD_EN);
Michael Chanc7835a72006-11-15 21:14:42 -080014336 }
Matt Carlson52f44902008-11-21 17:17:04 -080014337 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785) {
Jon Mason708ebb32011-06-27 12:56:50 +000014338 /* BCM5785 devices are effectively PCIe devices, and should
14339 * follow PCIe codepaths, but do not have a PCIe capabilities
14340 * section.
Matt Carlson93a700a2011-08-31 11:44:54 +000014341 */
Joe Perches63c3a662011-04-26 08:12:10 +000014342 tg3_flag_set(tp, PCI_EXPRESS);
14343 } else if (!tg3_flag(tp, 5705_PLUS) ||
14344 tg3_flag(tp, 5780_CLASS)) {
Matt Carlson52f44902008-11-21 17:17:04 -080014345 tp->pcix_cap = pci_find_capability(tp->pdev, PCI_CAP_ID_PCIX);
14346 if (!tp->pcix_cap) {
Matt Carlson2445e462010-04-05 10:19:21 +000014347 dev_err(&tp->pdev->dev,
14348 "Cannot find PCI-X capability, aborting\n");
Matt Carlson52f44902008-11-21 17:17:04 -080014349 return -EIO;
14350 }
14351
14352 if (!(pci_state_reg & PCISTATE_CONV_PCI_MODE))
Joe Perches63c3a662011-04-26 08:12:10 +000014353 tg3_flag_set(tp, PCIX_MODE);
Matt Carlson52f44902008-11-21 17:17:04 -080014354 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014355
Michael Chan399de502005-10-03 14:02:39 -070014356 /* If we have an AMD 762 or VIA K8T800 chipset, write
14357 * reordering to the mailbox registers done by the host
14358 * controller can cause major troubles. We read back from
14359 * every mailbox register write to force the writes to be
14360 * posted to the chip in order.
14361 */
Matt Carlson41434702011-03-09 16:58:22 +000014362 if (pci_dev_present(tg3_write_reorder_chipsets) &&
Joe Perches63c3a662011-04-26 08:12:10 +000014363 !tg3_flag(tp, PCI_EXPRESS))
14364 tg3_flag_set(tp, MBOX_WRITE_REORDER);
Michael Chan399de502005-10-03 14:02:39 -070014365
Matt Carlson69fc4052008-12-21 20:19:57 -080014366 pci_read_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE,
14367 &tp->pci_cacheline_sz);
14368 pci_read_config_byte(tp->pdev, PCI_LATENCY_TIMER,
14369 &tp->pci_lat_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014370 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 &&
14371 tp->pci_lat_timer < 64) {
14372 tp->pci_lat_timer = 64;
Matt Carlson69fc4052008-12-21 20:19:57 -080014373 pci_write_config_byte(tp->pdev, PCI_LATENCY_TIMER,
14374 tp->pci_lat_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014375 }
14376
Matt Carlson16821282011-07-13 09:27:28 +000014377 /* Important! -- It is critical that the PCI-X hw workaround
14378 * situation is decided before the first MMIO register access.
14379 */
Matt Carlson52f44902008-11-21 17:17:04 -080014380 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5700_BX) {
14381 /* 5700 BX chips need to have their TX producer index
14382 * mailboxes written twice to workaround a bug.
14383 */
Joe Perches63c3a662011-04-26 08:12:10 +000014384 tg3_flag_set(tp, TXD_MBOX_HWBUG);
Matt Carlson9974a352007-10-07 23:27:28 -070014385
Matt Carlson52f44902008-11-21 17:17:04 -080014386 /* If we are in PCI-X mode, enable register write workaround.
Linus Torvalds1da177e2005-04-16 15:20:36 -070014387 *
14388 * The workaround is to use indirect register accesses
14389 * for all chip writes not to mailbox registers.
14390 */
Joe Perches63c3a662011-04-26 08:12:10 +000014391 if (tg3_flag(tp, PCIX_MODE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070014392 u32 pm_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014393
Joe Perches63c3a662011-04-26 08:12:10 +000014394 tg3_flag_set(tp, PCIX_TARGET_HWBUG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014395
14396 /* The chip can have it's power management PCI config
14397 * space registers clobbered due to this bug.
14398 * So explicitly force the chip into D0 here.
14399 */
Matt Carlson9974a352007-10-07 23:27:28 -070014400 pci_read_config_dword(tp->pdev,
14401 tp->pm_cap + PCI_PM_CTRL,
Linus Torvalds1da177e2005-04-16 15:20:36 -070014402 &pm_reg);
14403 pm_reg &= ~PCI_PM_CTRL_STATE_MASK;
14404 pm_reg |= PCI_PM_CTRL_PME_ENABLE | 0 /* D0 */;
Matt Carlson9974a352007-10-07 23:27:28 -070014405 pci_write_config_dword(tp->pdev,
14406 tp->pm_cap + PCI_PM_CTRL,
Linus Torvalds1da177e2005-04-16 15:20:36 -070014407 pm_reg);
14408
14409 /* Also, force SERR#/PERR# in PCI command. */
14410 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
14411 pci_cmd |= PCI_COMMAND_PARITY | PCI_COMMAND_SERR;
14412 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
14413 }
14414 }
14415
Linus Torvalds1da177e2005-04-16 15:20:36 -070014416 if ((pci_state_reg & PCISTATE_BUS_SPEED_HIGH) != 0)
Joe Perches63c3a662011-04-26 08:12:10 +000014417 tg3_flag_set(tp, PCI_HIGH_SPEED);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014418 if ((pci_state_reg & PCISTATE_BUS_32BIT) != 0)
Joe Perches63c3a662011-04-26 08:12:10 +000014419 tg3_flag_set(tp, PCI_32BIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014420
14421 /* Chip-specific fixup from Broadcom driver */
14422 if ((tp->pci_chip_rev_id == CHIPREV_ID_5704_A0) &&
14423 (!(pci_state_reg & PCISTATE_RETRY_SAME_DMA))) {
14424 pci_state_reg |= PCISTATE_RETRY_SAME_DMA;
14425 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE, pci_state_reg);
14426 }
14427
Michael Chan1ee582d2005-08-09 20:16:46 -070014428 /* Default fast path register access methods */
Michael Chan20094932005-08-09 20:16:32 -070014429 tp->read32 = tg3_read32;
Michael Chan1ee582d2005-08-09 20:16:46 -070014430 tp->write32 = tg3_write32;
Michael Chan09ee9292005-08-09 20:17:00 -070014431 tp->read32_mbox = tg3_read32;
Michael Chan20094932005-08-09 20:16:32 -070014432 tp->write32_mbox = tg3_write32;
Michael Chan1ee582d2005-08-09 20:16:46 -070014433 tp->write32_tx_mbox = tg3_write32;
14434 tp->write32_rx_mbox = tg3_write32;
14435
14436 /* Various workaround register access methods */
Joe Perches63c3a662011-04-26 08:12:10 +000014437 if (tg3_flag(tp, PCIX_TARGET_HWBUG))
Michael Chan1ee582d2005-08-09 20:16:46 -070014438 tp->write32 = tg3_write_indirect_reg32;
Matt Carlson98efd8a2007-05-05 12:47:25 -070014439 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701 ||
Joe Perches63c3a662011-04-26 08:12:10 +000014440 (tg3_flag(tp, PCI_EXPRESS) &&
Matt Carlson98efd8a2007-05-05 12:47:25 -070014441 tp->pci_chip_rev_id == CHIPREV_ID_5750_A0)) {
14442 /*
14443 * Back to back register writes can cause problems on these
14444 * chips, the workaround is to read back all reg writes
14445 * except those to mailbox regs.
14446 *
14447 * See tg3_write_indirect_reg32().
14448 */
Michael Chan1ee582d2005-08-09 20:16:46 -070014449 tp->write32 = tg3_write_flush_reg32;
Matt Carlson98efd8a2007-05-05 12:47:25 -070014450 }
14451
Joe Perches63c3a662011-04-26 08:12:10 +000014452 if (tg3_flag(tp, TXD_MBOX_HWBUG) || tg3_flag(tp, MBOX_WRITE_REORDER)) {
Michael Chan1ee582d2005-08-09 20:16:46 -070014453 tp->write32_tx_mbox = tg3_write32_tx_mbox;
Joe Perches63c3a662011-04-26 08:12:10 +000014454 if (tg3_flag(tp, MBOX_WRITE_REORDER))
Michael Chan1ee582d2005-08-09 20:16:46 -070014455 tp->write32_rx_mbox = tg3_write_flush_reg32;
14456 }
Michael Chan20094932005-08-09 20:16:32 -070014457
Joe Perches63c3a662011-04-26 08:12:10 +000014458 if (tg3_flag(tp, ICH_WORKAROUND)) {
Michael Chan68929142005-08-09 20:17:14 -070014459 tp->read32 = tg3_read_indirect_reg32;
14460 tp->write32 = tg3_write_indirect_reg32;
14461 tp->read32_mbox = tg3_read_indirect_mbox;
14462 tp->write32_mbox = tg3_write_indirect_mbox;
14463 tp->write32_tx_mbox = tg3_write_indirect_mbox;
14464 tp->write32_rx_mbox = tg3_write_indirect_mbox;
14465
14466 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070014467 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070014468
14469 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
14470 pci_cmd &= ~PCI_COMMAND_MEMORY;
14471 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
14472 }
Michael Chanb5d37722006-09-27 16:06:21 -070014473 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
14474 tp->read32_mbox = tg3_read32_mbox_5906;
14475 tp->write32_mbox = tg3_write32_mbox_5906;
14476 tp->write32_tx_mbox = tg3_write32_mbox_5906;
14477 tp->write32_rx_mbox = tg3_write32_mbox_5906;
14478 }
Michael Chan68929142005-08-09 20:17:14 -070014479
Michael Chanbbadf502006-04-06 21:46:34 -070014480 if (tp->write32 == tg3_write_indirect_reg32 ||
Joe Perches63c3a662011-04-26 08:12:10 +000014481 (tg3_flag(tp, PCIX_MODE) &&
Michael Chanbbadf502006-04-06 21:46:34 -070014482 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
David S. Millerf49639e2006-06-09 11:58:36 -070014483 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)))
Joe Perches63c3a662011-04-26 08:12:10 +000014484 tg3_flag_set(tp, SRAM_USE_CONFIG);
Michael Chanbbadf502006-04-06 21:46:34 -070014485
Matt Carlson16821282011-07-13 09:27:28 +000014486 /* The memory arbiter has to be enabled in order for SRAM accesses
14487 * to succeed. Normally on powerup the tg3 chip firmware will make
14488 * sure it is enabled, but other entities such as system netboot
14489 * code might disable it.
14490 */
14491 val = tr32(MEMARB_MODE);
14492 tw32(MEMARB_MODE, val | MEMARB_MODE_ENABLE);
14493
Matt Carlson9dc5e342011-11-04 09:15:02 +000014494 tp->pci_fn = PCI_FUNC(tp->pdev->devfn) & 3;
14495 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
14496 tg3_flag(tp, 5780_CLASS)) {
14497 if (tg3_flag(tp, PCIX_MODE)) {
14498 pci_read_config_dword(tp->pdev,
14499 tp->pcix_cap + PCI_X_STATUS,
14500 &val);
14501 tp->pci_fn = val & 0x7;
14502 }
14503 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717) {
14504 tg3_read_mem(tp, NIC_SRAM_CPMU_STATUS, &val);
14505 if ((val & NIC_SRAM_CPMUSTAT_SIG_MSK) ==
14506 NIC_SRAM_CPMUSTAT_SIG) {
14507 tp->pci_fn = val & TG3_CPMU_STATUS_FMSK_5717;
14508 tp->pci_fn = tp->pci_fn ? 1 : 0;
14509 }
14510 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
14511 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
14512 tg3_read_mem(tp, NIC_SRAM_CPMU_STATUS, &val);
14513 if ((val & NIC_SRAM_CPMUSTAT_SIG_MSK) ==
14514 NIC_SRAM_CPMUSTAT_SIG) {
14515 tp->pci_fn = (val & TG3_CPMU_STATUS_FMSK_5719) >>
14516 TG3_CPMU_STATUS_FSHFT_5719;
14517 }
Matt Carlson69f11c92011-07-13 09:27:30 +000014518 }
14519
Michael Chan7d0c41e2005-04-21 17:06:20 -070014520 /* Get eeprom hw config before calling tg3_set_power_state().
Joe Perches63c3a662011-04-26 08:12:10 +000014521 * In particular, the TG3_FLAG_IS_NIC flag must be
Michael Chan7d0c41e2005-04-21 17:06:20 -070014522 * determined before calling tg3_set_power_state() so that
14523 * we know whether or not to switch out of Vaux power.
14524 * When the flag is set, it means that GPIO1 is used for eeprom
14525 * write protect and also implies that it is a LOM where GPIOs
14526 * are not used to switch power.
Jeff Garzik6aa20a22006-09-13 13:24:59 -040014527 */
Michael Chan7d0c41e2005-04-21 17:06:20 -070014528 tg3_get_eeprom_hw_cfg(tp);
14529
Matt Carlsoncf9ecf42011-11-28 09:41:03 +000014530 if (tp->fw_needed && tg3_flag(tp, ENABLE_ASF)) {
14531 tg3_flag_clear(tp, TSO_CAPABLE);
14532 tg3_flag_clear(tp, TSO_BUG);
14533 tp->fw_needed = NULL;
14534 }
14535
Joe Perches63c3a662011-04-26 08:12:10 +000014536 if (tg3_flag(tp, ENABLE_APE)) {
Matt Carlson0d3031d2007-10-10 18:02:43 -070014537 /* Allow reads and writes to the
14538 * APE register and memory space.
14539 */
14540 pci_state_reg |= PCISTATE_ALLOW_APE_CTLSPC_WR |
Matt Carlsonf92d9dc12010-06-05 17:24:30 +000014541 PCISTATE_ALLOW_APE_SHMEM_WR |
14542 PCISTATE_ALLOW_APE_PSPACE_WR;
Matt Carlson0d3031d2007-10-10 18:02:43 -070014543 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE,
14544 pci_state_reg);
Matt Carlsonc9cab242011-07-13 09:27:27 +000014545
14546 tg3_ape_lock_init(tp);
Matt Carlson0d3031d2007-10-10 18:02:43 -070014547 }
14548
Matt Carlson16821282011-07-13 09:27:28 +000014549 /* Set up tp->grc_local_ctrl before calling
14550 * tg3_pwrsrc_switch_to_vmain(). GPIO1 driven high
14551 * will bring 5700's external PHY out of reset.
Michael Chan314fba32005-04-21 17:07:04 -070014552 * It is also used as eeprom write protect on LOMs.
14553 */
14554 tp->grc_local_ctrl = GRC_LCLCTRL_INT_ON_ATTN | GRC_LCLCTRL_AUTO_SEEPROM;
Matt Carlson6ff6f812011-05-19 12:12:54 +000014555 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
Joe Perches63c3a662011-04-26 08:12:10 +000014556 tg3_flag(tp, EEPROM_WRITE_PROT))
Michael Chan314fba32005-04-21 17:07:04 -070014557 tp->grc_local_ctrl |= (GRC_LCLCTRL_GPIO_OE1 |
14558 GRC_LCLCTRL_GPIO_OUTPUT1);
Michael Chan3e7d83b2005-04-21 17:10:36 -070014559 /* Unused GPIO3 must be driven as output on 5752 because there
14560 * are no pull-up resistors on unused GPIO pins.
14561 */
14562 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752)
14563 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE3;
Michael Chan314fba32005-04-21 17:07:04 -070014564
Matt Carlson321d32a2008-11-21 17:22:19 -080014565 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
Matt Carlsoncb4ed1f2010-01-20 16:58:09 +000014566 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
Matt Carlson55086ad2011-12-14 11:09:59 +000014567 tg3_flag(tp, 57765_CLASS))
Michael Chanaf36e6b2006-03-23 01:28:06 -080014568 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_UART_SEL;
14569
Matt Carlson8d519ab2009-04-20 06:58:01 +000014570 if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761 ||
14571 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761S) {
Matt Carlson5f0c4a32008-06-09 15:41:12 -070014572 /* Turn off the debug UART. */
14573 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_UART_SEL;
Joe Perches63c3a662011-04-26 08:12:10 +000014574 if (tg3_flag(tp, IS_NIC))
Matt Carlson5f0c4a32008-06-09 15:41:12 -070014575 /* Keep VMain power. */
14576 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE0 |
14577 GRC_LCLCTRL_GPIO_OUTPUT0;
14578 }
14579
Matt Carlson16821282011-07-13 09:27:28 +000014580 /* Switch out of Vaux if it is a NIC */
14581 tg3_pwrsrc_switch_to_vmain(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014582
Linus Torvalds1da177e2005-04-16 15:20:36 -070014583 /* Derive initial jumbo mode from MTU assigned in
14584 * ether_setup() via the alloc_etherdev() call
14585 */
Joe Perches63c3a662011-04-26 08:12:10 +000014586 if (tp->dev->mtu > ETH_DATA_LEN && !tg3_flag(tp, 5780_CLASS))
14587 tg3_flag_set(tp, JUMBO_RING_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014588
14589 /* Determine WakeOnLan speed to use. */
14590 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
14591 tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
14592 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0 ||
14593 tp->pci_chip_rev_id == CHIPREV_ID_5701_B2) {
Joe Perches63c3a662011-04-26 08:12:10 +000014594 tg3_flag_clear(tp, WOL_SPEED_100MB);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014595 } else {
Joe Perches63c3a662011-04-26 08:12:10 +000014596 tg3_flag_set(tp, WOL_SPEED_100MB);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014597 }
14598
Matt Carlson7f97a4b2009-08-25 10:10:03 +000014599 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014600 tp->phy_flags |= TG3_PHYFLG_IS_FET;
Matt Carlson7f97a4b2009-08-25 10:10:03 +000014601
Linus Torvalds1da177e2005-04-16 15:20:36 -070014602 /* A few boards don't want Ethernet@WireSpeed phy feature */
Matt Carlson6ff6f812011-05-19 12:12:54 +000014603 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
14604 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070014605 (tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) &&
Michael Chan747e8f82005-07-25 12:33:22 -070014606 (tp->pci_chip_rev_id != CHIPREV_ID_5705_A1)) ||
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014607 (tp->phy_flags & TG3_PHYFLG_IS_FET) ||
14608 (tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
14609 tp->phy_flags |= TG3_PHYFLG_NO_ETH_WIRE_SPEED;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014610
14611 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5703_AX ||
14612 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5704_AX)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014613 tp->phy_flags |= TG3_PHYFLG_ADC_BUG;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014614 if (tp->pci_chip_rev_id == CHIPREV_ID_5704_A0)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014615 tp->phy_flags |= TG3_PHYFLG_5704_A0_BUG;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014616
Joe Perches63c3a662011-04-26 08:12:10 +000014617 if (tg3_flag(tp, 5705_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014618 !(tp->phy_flags & TG3_PHYFLG_IS_FET) &&
Matt Carlson321d32a2008-11-21 17:22:19 -080014619 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000014620 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_57780 &&
Joe Perches63c3a662011-04-26 08:12:10 +000014621 !tg3_flag(tp, 57765_PLUS)) {
Michael Chanc424cb22006-04-29 18:56:34 -070014622 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
Matt Carlsond30cdd22007-10-07 23:28:35 -070014623 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787 ||
Matt Carlson9936bcf2007-10-10 18:03:07 -070014624 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
14625 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761) {
Michael Chand4011ad2007-02-13 12:17:25 -080014626 if (tp->pdev->device != PCI_DEVICE_ID_TIGON3_5756 &&
14627 tp->pdev->device != PCI_DEVICE_ID_TIGON3_5722)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014628 tp->phy_flags |= TG3_PHYFLG_JITTER_BUG;
Michael Chanc1d2a192007-01-08 19:57:20 -080014629 if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5755M)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014630 tp->phy_flags |= TG3_PHYFLG_ADJUST_TRIM;
Matt Carlson321d32a2008-11-21 17:22:19 -080014631 } else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014632 tp->phy_flags |= TG3_PHYFLG_BER_BUG;
Michael Chanc424cb22006-04-29 18:56:34 -070014633 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014634
Matt Carlsonb2a5c192008-04-03 21:44:44 -070014635 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
14636 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX) {
14637 tp->phy_otp = tg3_read_otp_phycfg(tp);
14638 if (tp->phy_otp == 0)
14639 tp->phy_otp = TG3_OTP_DEFAULT;
14640 }
14641
Joe Perches63c3a662011-04-26 08:12:10 +000014642 if (tg3_flag(tp, CPMU_PRESENT))
Matt Carlson8ef21422008-05-02 16:47:53 -070014643 tp->mi_mode = MAC_MI_MODE_500KHZ_CONST;
14644 else
14645 tp->mi_mode = MAC_MI_MODE_BASE;
14646
Linus Torvalds1da177e2005-04-16 15:20:36 -070014647 tp->coalesce_mode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014648 if (GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5700_AX &&
14649 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5700_BX)
14650 tp->coalesce_mode |= HOSTCC_MODE_32BYTE;
14651
Matt Carlson4d958472011-04-20 07:57:35 +000014652 /* Set these bits to enable statistics workaround. */
14653 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
14654 tp->pci_chip_rev_id == CHIPREV_ID_5719_A0 ||
14655 tp->pci_chip_rev_id == CHIPREV_ID_5720_A0) {
14656 tp->coalesce_mode |= HOSTCC_MODE_ATTN;
14657 tp->grc_mode |= GRC_MODE_IRQ_ON_FLOW_ATTN;
14658 }
14659
Matt Carlson321d32a2008-11-21 17:22:19 -080014660 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
14661 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
Joe Perches63c3a662011-04-26 08:12:10 +000014662 tg3_flag_set(tp, USE_PHYLIB);
Matt Carlson57e69832008-05-25 23:48:31 -070014663
Matt Carlson158d7ab2008-05-29 01:37:54 -070014664 err = tg3_mdio_init(tp);
14665 if (err)
14666 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014667
14668 /* Initialize data/descriptor byte/word swapping. */
14669 val = tr32(GRC_MODE);
Matt Carlsonf2096f92011-04-05 14:22:48 +000014670 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
14671 val &= (GRC_MODE_BYTE_SWAP_B2HRX_DATA |
14672 GRC_MODE_WORD_SWAP_B2HRX_DATA |
14673 GRC_MODE_B2HRX_ENABLE |
14674 GRC_MODE_HTX2B_ENABLE |
14675 GRC_MODE_HOST_STACKUP);
14676 else
14677 val &= GRC_MODE_HOST_STACKUP;
14678
Linus Torvalds1da177e2005-04-16 15:20:36 -070014679 tw32(GRC_MODE, val | tp->grc_mode);
14680
14681 tg3_switch_clocks(tp);
14682
14683 /* Clear this out for sanity. */
14684 tw32(TG3PCI_MEM_WIN_BASE_ADDR, 0);
14685
14686 pci_read_config_dword(tp->pdev, TG3PCI_PCISTATE,
14687 &pci_state_reg);
14688 if ((pci_state_reg & PCISTATE_CONV_PCI_MODE) == 0 &&
Joe Perches63c3a662011-04-26 08:12:10 +000014689 !tg3_flag(tp, PCIX_TARGET_HWBUG)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070014690 u32 chiprevid = GET_CHIP_REV_ID(tp->misc_host_ctrl);
14691
14692 if (chiprevid == CHIPREV_ID_5701_A0 ||
14693 chiprevid == CHIPREV_ID_5701_B0 ||
14694 chiprevid == CHIPREV_ID_5701_B2 ||
14695 chiprevid == CHIPREV_ID_5701_B5) {
14696 void __iomem *sram_base;
14697
14698 /* Write some dummy words into the SRAM status block
14699 * area, see if it reads back correctly. If the return
14700 * value is bad, force enable the PCIX workaround.
14701 */
14702 sram_base = tp->regs + NIC_SRAM_WIN_BASE + NIC_SRAM_STATS_BLK;
14703
14704 writel(0x00000000, sram_base);
14705 writel(0x00000000, sram_base + 4);
14706 writel(0xffffffff, sram_base + 4);
14707 if (readl(sram_base) != 0x00000000)
Joe Perches63c3a662011-04-26 08:12:10 +000014708 tg3_flag_set(tp, PCIX_TARGET_HWBUG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014709 }
14710 }
14711
14712 udelay(50);
14713 tg3_nvram_init(tp);
14714
14715 grc_misc_cfg = tr32(GRC_MISC_CFG);
14716 grc_misc_cfg &= GRC_MISC_CFG_BOARD_ID_MASK;
14717
Linus Torvalds1da177e2005-04-16 15:20:36 -070014718 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
14719 (grc_misc_cfg == GRC_MISC_CFG_BOARD_ID_5788 ||
14720 grc_misc_cfg == GRC_MISC_CFG_BOARD_ID_5788M))
Joe Perches63c3a662011-04-26 08:12:10 +000014721 tg3_flag_set(tp, IS_5788);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014722
Joe Perches63c3a662011-04-26 08:12:10 +000014723 if (!tg3_flag(tp, IS_5788) &&
Matt Carlson6ff6f812011-05-19 12:12:54 +000014724 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700)
Joe Perches63c3a662011-04-26 08:12:10 +000014725 tg3_flag_set(tp, TAGGED_STATUS);
14726 if (tg3_flag(tp, TAGGED_STATUS)) {
David S. Millerfac9b832005-05-18 22:46:34 -070014727 tp->coalesce_mode |= (HOSTCC_MODE_CLRTICK_RXBD |
14728 HOSTCC_MODE_CLRTICK_TXBD);
14729
14730 tp->misc_host_ctrl |= MISC_HOST_CTRL_TAGGED_STATUS;
14731 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
14732 tp->misc_host_ctrl);
14733 }
14734
Matt Carlson3bda1252008-08-15 14:08:22 -070014735 /* Preserve the APE MAC_MODE bits */
Joe Perches63c3a662011-04-26 08:12:10 +000014736 if (tg3_flag(tp, ENABLE_APE))
Matt Carlsond2394e6b2010-11-24 08:31:47 +000014737 tp->mac_mode = MAC_MODE_APE_TX_EN | MAC_MODE_APE_RX_EN;
Matt Carlson3bda1252008-08-15 14:08:22 -070014738 else
Matt Carlson6e01b202011-08-19 13:58:20 +000014739 tp->mac_mode = 0;
Matt Carlson3bda1252008-08-15 14:08:22 -070014740
Linus Torvalds1da177e2005-04-16 15:20:36 -070014741 /* these are limited to 10/100 only */
14742 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 &&
14743 (grc_misc_cfg == 0x8000 || grc_misc_cfg == 0x4000)) ||
14744 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
14745 tp->pdev->vendor == PCI_VENDOR_ID_BROADCOM &&
14746 (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5901 ||
14747 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5901_2 ||
14748 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5705F)) ||
14749 (tp->pdev->vendor == PCI_VENDOR_ID_BROADCOM &&
14750 (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5751F ||
Michael Chan676917d2006-12-07 00:20:22 -080014751 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5753F ||
14752 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5787F)) ||
Matt Carlson321d32a2008-11-21 17:22:19 -080014753 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57790 ||
Matt Carlsond1101142010-02-17 15:16:55 +000014754 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57791 ||
14755 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57795 ||
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014756 (tp->phy_flags & TG3_PHYFLG_IS_FET))
14757 tp->phy_flags |= TG3_PHYFLG_10_100_ONLY;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014758
14759 err = tg3_phy_probe(tp);
14760 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000014761 dev_err(&tp->pdev->dev, "phy probe failed, err %d\n", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014762 /* ... but do not return immediately ... */
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070014763 tg3_mdio_fini(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014764 }
14765
Matt Carlson184b8902010-04-05 10:19:25 +000014766 tg3_read_vpd(tp);
Michael Chanc4e65752006-03-20 22:29:32 -080014767 tg3_read_fw_ver(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014768
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014769 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
14770 tp->phy_flags &= ~TG3_PHYFLG_USE_MI_INTERRUPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014771 } else {
14772 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014773 tp->phy_flags |= TG3_PHYFLG_USE_MI_INTERRUPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014774 else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014775 tp->phy_flags &= ~TG3_PHYFLG_USE_MI_INTERRUPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014776 }
14777
14778 /* 5700 {AX,BX} chips have a broken status block link
14779 * change bit implementation, so we must use the
14780 * status register in those cases.
14781 */
14782 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700)
Joe Perches63c3a662011-04-26 08:12:10 +000014783 tg3_flag_set(tp, USE_LINKCHG_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014784 else
Joe Perches63c3a662011-04-26 08:12:10 +000014785 tg3_flag_clear(tp, USE_LINKCHG_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014786
14787 /* The led_ctrl is set during tg3_phy_probe, here we might
14788 * have to force the link status polling mechanism based
14789 * upon subsystem IDs.
14790 */
14791 if (tp->pdev->subsystem_vendor == PCI_VENDOR_ID_DELL &&
Michael Chan007a880d2007-05-31 14:49:51 -070014792 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701 &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014793 !(tp->phy_flags & TG3_PHYFLG_PHY_SERDES)) {
14794 tp->phy_flags |= TG3_PHYFLG_USE_MI_INTERRUPT;
Joe Perches63c3a662011-04-26 08:12:10 +000014795 tg3_flag_set(tp, USE_LINKCHG_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014796 }
14797
14798 /* For all SERDES we poll the MAC status register. */
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014799 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Joe Perches63c3a662011-04-26 08:12:10 +000014800 tg3_flag_set(tp, POLL_SERDES);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014801 else
Joe Perches63c3a662011-04-26 08:12:10 +000014802 tg3_flag_clear(tp, POLL_SERDES);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014803
Eric Dumazet9205fd92011-11-18 06:47:01 +000014804 tp->rx_offset = NET_SKB_PAD + NET_IP_ALIGN;
Matt Carlsond2757fc2010-04-12 06:58:27 +000014805 tp->rx_copy_thresh = TG3_RX_COPY_THRESHOLD;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014806 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701 &&
Joe Perches63c3a662011-04-26 08:12:10 +000014807 tg3_flag(tp, PCIX_MODE)) {
Eric Dumazet9205fd92011-11-18 06:47:01 +000014808 tp->rx_offset = NET_SKB_PAD;
Matt Carlsond2757fc2010-04-12 06:58:27 +000014809#ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
Matt Carlson9dc7a112010-04-12 06:58:28 +000014810 tp->rx_copy_thresh = ~(u16)0;
Matt Carlsond2757fc2010-04-12 06:58:27 +000014811#endif
14812 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014813
Matt Carlson2c49a442010-09-30 10:34:35 +000014814 tp->rx_std_ring_mask = TG3_RX_STD_RING_SIZE(tp) - 1;
14815 tp->rx_jmb_ring_mask = TG3_RX_JMB_RING_SIZE(tp) - 1;
Matt Carlson7cb32cf2010-09-30 10:34:36 +000014816 tp->rx_ret_ring_mask = tg3_rx_ret_ring_size(tp) - 1;
14817
Matt Carlson2c49a442010-09-30 10:34:35 +000014818 tp->rx_std_max_post = tp->rx_std_ring_mask + 1;
Michael Chanf92905d2006-06-29 20:14:29 -070014819
14820 /* Increment the rx prod index on the rx std ring by at most
14821 * 8 for these chips to workaround hw errata.
14822 */
14823 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 ||
14824 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752 ||
14825 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755)
14826 tp->rx_std_max_post = 8;
14827
Joe Perches63c3a662011-04-26 08:12:10 +000014828 if (tg3_flag(tp, ASPM_WORKAROUND))
Matt Carlson8ed5d972007-05-07 00:25:49 -070014829 tp->pwrmgmt_thresh = tr32(PCIE_PWR_MGMT_THRESH) &
14830 PCIE_PWR_MGMT_L1_THRESH_MSK;
14831
Linus Torvalds1da177e2005-04-16 15:20:36 -070014832 return err;
14833}
14834
David S. Miller49b6e95f2007-03-29 01:38:42 -070014835#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070014836static int __devinit tg3_get_macaddr_sparc(struct tg3 *tp)
14837{
14838 struct net_device *dev = tp->dev;
14839 struct pci_dev *pdev = tp->pdev;
David S. Miller49b6e95f2007-03-29 01:38:42 -070014840 struct device_node *dp = pci_device_to_OF_node(pdev);
David S. Miller374d4ca2007-03-29 01:57:57 -070014841 const unsigned char *addr;
David S. Miller49b6e95f2007-03-29 01:38:42 -070014842 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014843
David S. Miller49b6e95f2007-03-29 01:38:42 -070014844 addr = of_get_property(dp, "local-mac-address", &len);
14845 if (addr && len == 6) {
14846 memcpy(dev->dev_addr, addr, 6);
14847 memcpy(dev->perm_addr, dev->dev_addr, 6);
14848 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014849 }
14850 return -ENODEV;
14851}
14852
14853static int __devinit tg3_get_default_macaddr_sparc(struct tg3 *tp)
14854{
14855 struct net_device *dev = tp->dev;
14856
14857 memcpy(dev->dev_addr, idprom->id_ethaddr, 6);
John W. Linville2ff43692005-09-12 14:44:20 -070014858 memcpy(dev->perm_addr, idprom->id_ethaddr, 6);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014859 return 0;
14860}
14861#endif
14862
14863static int __devinit tg3_get_device_address(struct tg3 *tp)
14864{
14865 struct net_device *dev = tp->dev;
14866 u32 hi, lo, mac_offset;
Michael Chan008652b2006-03-27 23:14:53 -080014867 int addr_ok = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014868
David S. Miller49b6e95f2007-03-29 01:38:42 -070014869#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070014870 if (!tg3_get_macaddr_sparc(tp))
14871 return 0;
14872#endif
14873
14874 mac_offset = 0x7c;
Matt Carlson6ff6f812011-05-19 12:12:54 +000014875 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
Joe Perches63c3a662011-04-26 08:12:10 +000014876 tg3_flag(tp, 5780_CLASS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070014877 if (tr32(TG3PCI_DUAL_MAC_CTRL) & DUAL_MAC_CTRL_ID)
14878 mac_offset = 0xcc;
14879 if (tg3_nvram_lock(tp))
14880 tw32_f(NVRAM_CMD, NVRAM_CMD_RESET);
14881 else
14882 tg3_nvram_unlock(tp);
Joe Perches63c3a662011-04-26 08:12:10 +000014883 } else if (tg3_flag(tp, 5717_PLUS)) {
Matt Carlson69f11c92011-07-13 09:27:30 +000014884 if (tp->pci_fn & 1)
Matt Carlsona1b950d2009-09-01 13:20:17 +000014885 mac_offset = 0xcc;
Matt Carlson69f11c92011-07-13 09:27:30 +000014886 if (tp->pci_fn > 1)
Matt Carlsona50d0792010-06-05 17:24:37 +000014887 mac_offset += 0x18c;
Matt Carlsona1b950d2009-09-01 13:20:17 +000014888 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
Michael Chanb5d37722006-09-27 16:06:21 -070014889 mac_offset = 0x10;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014890
14891 /* First try to get it from MAC address mailbox. */
14892 tg3_read_mem(tp, NIC_SRAM_MAC_ADDR_HIGH_MBOX, &hi);
14893 if ((hi >> 16) == 0x484b) {
14894 dev->dev_addr[0] = (hi >> 8) & 0xff;
14895 dev->dev_addr[1] = (hi >> 0) & 0xff;
14896
14897 tg3_read_mem(tp, NIC_SRAM_MAC_ADDR_LOW_MBOX, &lo);
14898 dev->dev_addr[2] = (lo >> 24) & 0xff;
14899 dev->dev_addr[3] = (lo >> 16) & 0xff;
14900 dev->dev_addr[4] = (lo >> 8) & 0xff;
14901 dev->dev_addr[5] = (lo >> 0) & 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014902
Michael Chan008652b2006-03-27 23:14:53 -080014903 /* Some old bootcode may report a 0 MAC address in SRAM */
14904 addr_ok = is_valid_ether_addr(&dev->dev_addr[0]);
14905 }
14906 if (!addr_ok) {
14907 /* Next, try NVRAM. */
Joe Perches63c3a662011-04-26 08:12:10 +000014908 if (!tg3_flag(tp, NO_NVRAM) &&
Matt Carlsondf259d82009-04-20 06:57:14 +000014909 !tg3_nvram_read_be32(tp, mac_offset + 0, &hi) &&
Matt Carlson6d348f22009-02-25 14:25:52 +000014910 !tg3_nvram_read_be32(tp, mac_offset + 4, &lo)) {
Matt Carlson62cedd12009-04-20 14:52:29 -070014911 memcpy(&dev->dev_addr[0], ((char *)&hi) + 2, 2);
14912 memcpy(&dev->dev_addr[2], (char *)&lo, sizeof(lo));
Michael Chan008652b2006-03-27 23:14:53 -080014913 }
14914 /* Finally just fetch it out of the MAC control regs. */
14915 else {
14916 hi = tr32(MAC_ADDR_0_HIGH);
14917 lo = tr32(MAC_ADDR_0_LOW);
14918
14919 dev->dev_addr[5] = lo & 0xff;
14920 dev->dev_addr[4] = (lo >> 8) & 0xff;
14921 dev->dev_addr[3] = (lo >> 16) & 0xff;
14922 dev->dev_addr[2] = (lo >> 24) & 0xff;
14923 dev->dev_addr[1] = hi & 0xff;
14924 dev->dev_addr[0] = (hi >> 8) & 0xff;
14925 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014926 }
14927
14928 if (!is_valid_ether_addr(&dev->dev_addr[0])) {
David S. Miller7582a332008-03-20 15:53:15 -070014929#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070014930 if (!tg3_get_default_macaddr_sparc(tp))
14931 return 0;
14932#endif
14933 return -EINVAL;
14934 }
John W. Linville2ff43692005-09-12 14:44:20 -070014935 memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014936 return 0;
14937}
14938
David S. Miller59e6b432005-05-18 22:50:10 -070014939#define BOUNDARY_SINGLE_CACHELINE 1
14940#define BOUNDARY_MULTI_CACHELINE 2
14941
14942static u32 __devinit tg3_calc_dma_bndry(struct tg3 *tp, u32 val)
14943{
14944 int cacheline_size;
14945 u8 byte;
14946 int goal;
14947
14948 pci_read_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE, &byte);
14949 if (byte == 0)
14950 cacheline_size = 1024;
14951 else
14952 cacheline_size = (int) byte * 4;
14953
14954 /* On 5703 and later chips, the boundary bits have no
14955 * effect.
14956 */
14957 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
14958 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701 &&
Joe Perches63c3a662011-04-26 08:12:10 +000014959 !tg3_flag(tp, PCI_EXPRESS))
David S. Miller59e6b432005-05-18 22:50:10 -070014960 goto out;
14961
14962#if defined(CONFIG_PPC64) || defined(CONFIG_IA64) || defined(CONFIG_PARISC)
14963 goal = BOUNDARY_MULTI_CACHELINE;
14964#else
14965#if defined(CONFIG_SPARC64) || defined(CONFIG_ALPHA)
14966 goal = BOUNDARY_SINGLE_CACHELINE;
14967#else
14968 goal = 0;
14969#endif
14970#endif
14971
Joe Perches63c3a662011-04-26 08:12:10 +000014972 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000014973 val = goal ? 0 : DMA_RWCTRL_DIS_CACHE_ALIGNMENT;
14974 goto out;
14975 }
14976
David S. Miller59e6b432005-05-18 22:50:10 -070014977 if (!goal)
14978 goto out;
14979
14980 /* PCI controllers on most RISC systems tend to disconnect
14981 * when a device tries to burst across a cache-line boundary.
14982 * Therefore, letting tg3 do so just wastes PCI bandwidth.
14983 *
14984 * Unfortunately, for PCI-E there are only limited
14985 * write-side controls for this, and thus for reads
14986 * we will still get the disconnects. We'll also waste
14987 * these PCI cycles for both read and write for chips
14988 * other than 5700 and 5701 which do not implement the
14989 * boundary bits.
14990 */
Joe Perches63c3a662011-04-26 08:12:10 +000014991 if (tg3_flag(tp, PCIX_MODE) && !tg3_flag(tp, PCI_EXPRESS)) {
David S. Miller59e6b432005-05-18 22:50:10 -070014992 switch (cacheline_size) {
14993 case 16:
14994 case 32:
14995 case 64:
14996 case 128:
14997 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14998 val |= (DMA_RWCTRL_READ_BNDRY_128_PCIX |
14999 DMA_RWCTRL_WRITE_BNDRY_128_PCIX);
15000 } else {
15001 val |= (DMA_RWCTRL_READ_BNDRY_384_PCIX |
15002 DMA_RWCTRL_WRITE_BNDRY_384_PCIX);
15003 }
15004 break;
15005
15006 case 256:
15007 val |= (DMA_RWCTRL_READ_BNDRY_256_PCIX |
15008 DMA_RWCTRL_WRITE_BNDRY_256_PCIX);
15009 break;
15010
15011 default:
15012 val |= (DMA_RWCTRL_READ_BNDRY_384_PCIX |
15013 DMA_RWCTRL_WRITE_BNDRY_384_PCIX);
15014 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070015015 }
Joe Perches63c3a662011-04-26 08:12:10 +000015016 } else if (tg3_flag(tp, PCI_EXPRESS)) {
David S. Miller59e6b432005-05-18 22:50:10 -070015017 switch (cacheline_size) {
15018 case 16:
15019 case 32:
15020 case 64:
15021 if (goal == BOUNDARY_SINGLE_CACHELINE) {
15022 val &= ~DMA_RWCTRL_WRITE_BNDRY_DISAB_PCIE;
15023 val |= DMA_RWCTRL_WRITE_BNDRY_64_PCIE;
15024 break;
15025 }
15026 /* fallthrough */
15027 case 128:
15028 default:
15029 val &= ~DMA_RWCTRL_WRITE_BNDRY_DISAB_PCIE;
15030 val |= DMA_RWCTRL_WRITE_BNDRY_128_PCIE;
15031 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070015032 }
David S. Miller59e6b432005-05-18 22:50:10 -070015033 } else {
15034 switch (cacheline_size) {
15035 case 16:
15036 if (goal == BOUNDARY_SINGLE_CACHELINE) {
15037 val |= (DMA_RWCTRL_READ_BNDRY_16 |
15038 DMA_RWCTRL_WRITE_BNDRY_16);
15039 break;
15040 }
15041 /* fallthrough */
15042 case 32:
15043 if (goal == BOUNDARY_SINGLE_CACHELINE) {
15044 val |= (DMA_RWCTRL_READ_BNDRY_32 |
15045 DMA_RWCTRL_WRITE_BNDRY_32);
15046 break;
15047 }
15048 /* fallthrough */
15049 case 64:
15050 if (goal == BOUNDARY_SINGLE_CACHELINE) {
15051 val |= (DMA_RWCTRL_READ_BNDRY_64 |
15052 DMA_RWCTRL_WRITE_BNDRY_64);
15053 break;
15054 }
15055 /* fallthrough */
15056 case 128:
15057 if (goal == BOUNDARY_SINGLE_CACHELINE) {
15058 val |= (DMA_RWCTRL_READ_BNDRY_128 |
15059 DMA_RWCTRL_WRITE_BNDRY_128);
15060 break;
15061 }
15062 /* fallthrough */
15063 case 256:
15064 val |= (DMA_RWCTRL_READ_BNDRY_256 |
15065 DMA_RWCTRL_WRITE_BNDRY_256);
15066 break;
15067 case 512:
15068 val |= (DMA_RWCTRL_READ_BNDRY_512 |
15069 DMA_RWCTRL_WRITE_BNDRY_512);
15070 break;
15071 case 1024:
15072 default:
15073 val |= (DMA_RWCTRL_READ_BNDRY_1024 |
15074 DMA_RWCTRL_WRITE_BNDRY_1024);
15075 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070015076 }
David S. Miller59e6b432005-05-18 22:50:10 -070015077 }
15078
15079out:
15080 return val;
15081}
15082
Linus Torvalds1da177e2005-04-16 15:20:36 -070015083static int __devinit tg3_do_test_dma(struct tg3 *tp, u32 *buf, dma_addr_t buf_dma, int size, int to_device)
15084{
15085 struct tg3_internal_buffer_desc test_desc;
15086 u32 sram_dma_descs;
15087 int i, ret;
15088
15089 sram_dma_descs = NIC_SRAM_DMA_DESC_POOL_BASE;
15090
15091 tw32(FTQ_RCVBD_COMP_FIFO_ENQDEQ, 0);
15092 tw32(FTQ_RCVDATA_COMP_FIFO_ENQDEQ, 0);
15093 tw32(RDMAC_STATUS, 0);
15094 tw32(WDMAC_STATUS, 0);
15095
15096 tw32(BUFMGR_MODE, 0);
15097 tw32(FTQ_RESET, 0);
15098
15099 test_desc.addr_hi = ((u64) buf_dma) >> 32;
15100 test_desc.addr_lo = buf_dma & 0xffffffff;
15101 test_desc.nic_mbuf = 0x00002100;
15102 test_desc.len = size;
15103
15104 /*
15105 * HP ZX1 was seeing test failures for 5701 cards running at 33Mhz
15106 * the *second* time the tg3 driver was getting loaded after an
15107 * initial scan.
15108 *
15109 * Broadcom tells me:
15110 * ...the DMA engine is connected to the GRC block and a DMA
15111 * reset may affect the GRC block in some unpredictable way...
15112 * The behavior of resets to individual blocks has not been tested.
15113 *
15114 * Broadcom noted the GRC reset will also reset all sub-components.
15115 */
15116 if (to_device) {
15117 test_desc.cqid_sqid = (13 << 8) | 2;
15118
15119 tw32_f(RDMAC_MODE, RDMAC_MODE_ENABLE);
15120 udelay(40);
15121 } else {
15122 test_desc.cqid_sqid = (16 << 8) | 7;
15123
15124 tw32_f(WDMAC_MODE, WDMAC_MODE_ENABLE);
15125 udelay(40);
15126 }
15127 test_desc.flags = 0x00000005;
15128
15129 for (i = 0; i < (sizeof(test_desc) / sizeof(u32)); i++) {
15130 u32 val;
15131
15132 val = *(((u32 *)&test_desc) + i);
15133 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR,
15134 sram_dma_descs + (i * sizeof(u32)));
15135 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_DATA, val);
15136 }
15137 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, 0);
15138
Matt Carlson859a588792010-04-05 10:19:28 +000015139 if (to_device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015140 tw32(FTQ_DMA_HIGH_READ_FIFO_ENQDEQ, sram_dma_descs);
Matt Carlson859a588792010-04-05 10:19:28 +000015141 else
Linus Torvalds1da177e2005-04-16 15:20:36 -070015142 tw32(FTQ_DMA_HIGH_WRITE_FIFO_ENQDEQ, sram_dma_descs);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015143
15144 ret = -ENODEV;
15145 for (i = 0; i < 40; i++) {
15146 u32 val;
15147
15148 if (to_device)
15149 val = tr32(FTQ_RCVBD_COMP_FIFO_ENQDEQ);
15150 else
15151 val = tr32(FTQ_RCVDATA_COMP_FIFO_ENQDEQ);
15152 if ((val & 0xffff) == sram_dma_descs) {
15153 ret = 0;
15154 break;
15155 }
15156
15157 udelay(100);
15158 }
15159
15160 return ret;
15161}
15162
David S. Millerded73402005-05-23 13:59:47 -070015163#define TEST_BUFFER_SIZE 0x2000
Linus Torvalds1da177e2005-04-16 15:20:36 -070015164
Matt Carlson41434702011-03-09 16:58:22 +000015165static DEFINE_PCI_DEVICE_TABLE(tg3_dma_wait_state_chipsets) = {
Joe Perches895950c2010-12-21 02:16:08 -080015166 { PCI_DEVICE(PCI_VENDOR_ID_APPLE, PCI_DEVICE_ID_APPLE_UNI_N_PCI15) },
15167 { },
15168};
15169
Linus Torvalds1da177e2005-04-16 15:20:36 -070015170static int __devinit tg3_test_dma(struct tg3 *tp)
15171{
15172 dma_addr_t buf_dma;
David S. Miller59e6b432005-05-18 22:50:10 -070015173 u32 *buf, saved_dma_rwctrl;
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000015174 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015175
Matt Carlson4bae65c2010-11-24 08:31:52 +000015176 buf = dma_alloc_coherent(&tp->pdev->dev, TEST_BUFFER_SIZE,
15177 &buf_dma, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015178 if (!buf) {
15179 ret = -ENOMEM;
15180 goto out_nofree;
15181 }
15182
15183 tp->dma_rwctrl = ((0x7 << DMA_RWCTRL_PCI_WRITE_CMD_SHIFT) |
15184 (0x6 << DMA_RWCTRL_PCI_READ_CMD_SHIFT));
15185
David S. Miller59e6b432005-05-18 22:50:10 -070015186 tp->dma_rwctrl = tg3_calc_dma_bndry(tp, tp->dma_rwctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015187
Joe Perches63c3a662011-04-26 08:12:10 +000015188 if (tg3_flag(tp, 57765_PLUS))
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000015189 goto out;
15190
Joe Perches63c3a662011-04-26 08:12:10 +000015191 if (tg3_flag(tp, PCI_EXPRESS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070015192 /* DMA read watermark not used on PCIE */
15193 tp->dma_rwctrl |= 0x00180000;
Joe Perches63c3a662011-04-26 08:12:10 +000015194 } else if (!tg3_flag(tp, PCIX_MODE)) {
Michael Chan85e94ce2005-04-21 17:05:28 -070015195 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 ||
15196 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015197 tp->dma_rwctrl |= 0x003f0000;
15198 else
15199 tp->dma_rwctrl |= 0x003f000f;
15200 } else {
15201 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
15202 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
15203 u32 ccval = (tr32(TG3PCI_CLOCK_CTRL) & 0x1f);
Michael Chan49afdeb2007-02-13 12:17:03 -080015204 u32 read_water = 0x7;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015205
Michael Chan4a29cc22006-03-19 13:21:12 -080015206 /* If the 5704 is behind the EPB bridge, we can
15207 * do the less restrictive ONE_DMA workaround for
15208 * better performance.
15209 */
Joe Perches63c3a662011-04-26 08:12:10 +000015210 if (tg3_flag(tp, 40BIT_DMA_BUG) &&
Michael Chan4a29cc22006-03-19 13:21:12 -080015211 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704)
15212 tp->dma_rwctrl |= 0x8000;
15213 else if (ccval == 0x6 || ccval == 0x7)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015214 tp->dma_rwctrl |= DMA_RWCTRL_ONE_DMA;
15215
Michael Chan49afdeb2007-02-13 12:17:03 -080015216 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703)
15217 read_water = 4;
David S. Miller59e6b432005-05-18 22:50:10 -070015218 /* Set bit 23 to enable PCIX hw bug fix */
Michael Chan49afdeb2007-02-13 12:17:03 -080015219 tp->dma_rwctrl |=
15220 (read_water << DMA_RWCTRL_READ_WATER_SHIFT) |
15221 (0x3 << DMA_RWCTRL_WRITE_WATER_SHIFT) |
15222 (1 << 23);
Michael Chan4cf78e42005-07-25 12:29:19 -070015223 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780) {
15224 /* 5780 always in PCIX mode */
15225 tp->dma_rwctrl |= 0x00144000;
Michael Chana4e2b342005-10-26 15:46:52 -070015226 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
15227 /* 5714 always in PCIX mode */
15228 tp->dma_rwctrl |= 0x00148000;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015229 } else {
15230 tp->dma_rwctrl |= 0x001b000f;
15231 }
15232 }
15233
15234 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
15235 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704)
15236 tp->dma_rwctrl &= 0xfffffff0;
15237
15238 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
15239 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
15240 /* Remove this if it causes problems for some boards. */
15241 tp->dma_rwctrl |= DMA_RWCTRL_USE_MEM_READ_MULT;
15242
15243 /* On 5700/5701 chips, we need to set this bit.
15244 * Otherwise the chip will issue cacheline transactions
15245 * to streamable DMA memory with not all the byte
15246 * enables turned on. This is an error on several
15247 * RISC PCI controllers, in particular sparc64.
15248 *
15249 * On 5703/5704 chips, this bit has been reassigned
15250 * a different meaning. In particular, it is used
15251 * on those chips to enable a PCI-X workaround.
15252 */
15253 tp->dma_rwctrl |= DMA_RWCTRL_ASSERT_ALL_BE;
15254 }
15255
15256 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
15257
15258#if 0
15259 /* Unneeded, already done by tg3_get_invariants. */
15260 tg3_switch_clocks(tp);
15261#endif
15262
Linus Torvalds1da177e2005-04-16 15:20:36 -070015263 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
15264 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701)
15265 goto out;
15266
David S. Miller59e6b432005-05-18 22:50:10 -070015267 /* It is best to perform DMA test with maximum write burst size
15268 * to expose the 5700/5701 write DMA bug.
15269 */
15270 saved_dma_rwctrl = tp->dma_rwctrl;
15271 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
15272 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
15273
Linus Torvalds1da177e2005-04-16 15:20:36 -070015274 while (1) {
15275 u32 *p = buf, i;
15276
15277 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++)
15278 p[i] = i;
15279
15280 /* Send the buffer to the chip. */
15281 ret = tg3_do_test_dma(tp, buf, buf_dma, TEST_BUFFER_SIZE, 1);
15282 if (ret) {
Matt Carlson2445e462010-04-05 10:19:21 +000015283 dev_err(&tp->pdev->dev,
15284 "%s: Buffer write failed. err = %d\n",
15285 __func__, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015286 break;
15287 }
15288
15289#if 0
15290 /* validate data reached card RAM correctly. */
15291 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++) {
15292 u32 val;
15293 tg3_read_mem(tp, 0x2100 + (i*4), &val);
15294 if (le32_to_cpu(val) != p[i]) {
Matt Carlson2445e462010-04-05 10:19:21 +000015295 dev_err(&tp->pdev->dev,
15296 "%s: Buffer corrupted on device! "
15297 "(%d != %d)\n", __func__, val, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015298 /* ret = -ENODEV here? */
15299 }
15300 p[i] = 0;
15301 }
15302#endif
15303 /* Now read it back. */
15304 ret = tg3_do_test_dma(tp, buf, buf_dma, TEST_BUFFER_SIZE, 0);
15305 if (ret) {
Matt Carlson5129c3a2010-04-05 10:19:23 +000015306 dev_err(&tp->pdev->dev, "%s: Buffer read failed. "
15307 "err = %d\n", __func__, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015308 break;
15309 }
15310
15311 /* Verify it. */
15312 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++) {
15313 if (p[i] == i)
15314 continue;
15315
David S. Miller59e6b432005-05-18 22:50:10 -070015316 if ((tp->dma_rwctrl & DMA_RWCTRL_WRITE_BNDRY_MASK) !=
15317 DMA_RWCTRL_WRITE_BNDRY_16) {
15318 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015319 tp->dma_rwctrl |= DMA_RWCTRL_WRITE_BNDRY_16;
15320 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
15321 break;
15322 } else {
Matt Carlson2445e462010-04-05 10:19:21 +000015323 dev_err(&tp->pdev->dev,
15324 "%s: Buffer corrupted on read back! "
15325 "(%d != %d)\n", __func__, p[i], i);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015326 ret = -ENODEV;
15327 goto out;
15328 }
15329 }
15330
15331 if (i == (TEST_BUFFER_SIZE / sizeof(u32))) {
15332 /* Success. */
15333 ret = 0;
15334 break;
15335 }
15336 }
David S. Miller59e6b432005-05-18 22:50:10 -070015337 if ((tp->dma_rwctrl & DMA_RWCTRL_WRITE_BNDRY_MASK) !=
15338 DMA_RWCTRL_WRITE_BNDRY_16) {
15339 /* DMA test passed without adjusting DMA boundary,
Michael Chan6d1cfba2005-06-08 14:13:14 -070015340 * now look for chipsets that are known to expose the
15341 * DMA bug without failing the test.
David S. Miller59e6b432005-05-18 22:50:10 -070015342 */
Matt Carlson41434702011-03-09 16:58:22 +000015343 if (pci_dev_present(tg3_dma_wait_state_chipsets)) {
Michael Chan6d1cfba2005-06-08 14:13:14 -070015344 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
15345 tp->dma_rwctrl |= DMA_RWCTRL_WRITE_BNDRY_16;
Matt Carlson859a588792010-04-05 10:19:28 +000015346 } else {
Michael Chan6d1cfba2005-06-08 14:13:14 -070015347 /* Safe to use the calculated DMA boundary. */
15348 tp->dma_rwctrl = saved_dma_rwctrl;
Matt Carlson859a588792010-04-05 10:19:28 +000015349 }
Michael Chan6d1cfba2005-06-08 14:13:14 -070015350
David S. Miller59e6b432005-05-18 22:50:10 -070015351 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
15352 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015353
15354out:
Matt Carlson4bae65c2010-11-24 08:31:52 +000015355 dma_free_coherent(&tp->pdev->dev, TEST_BUFFER_SIZE, buf, buf_dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015356out_nofree:
15357 return ret;
15358}
15359
Linus Torvalds1da177e2005-04-16 15:20:36 -070015360static void __devinit tg3_init_bufmgr_config(struct tg3 *tp)
15361{
Joe Perches63c3a662011-04-26 08:12:10 +000015362 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlson666bc832010-01-20 16:58:03 +000015363 tp->bufmgr_config.mbuf_read_dma_low_water =
15364 DEFAULT_MB_RDMA_LOW_WATER_5705;
15365 tp->bufmgr_config.mbuf_mac_rx_low_water =
15366 DEFAULT_MB_MACRX_LOW_WATER_57765;
15367 tp->bufmgr_config.mbuf_high_water =
15368 DEFAULT_MB_HIGH_WATER_57765;
15369
15370 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
15371 DEFAULT_MB_RDMA_LOW_WATER_5705;
15372 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
15373 DEFAULT_MB_MACRX_LOW_WATER_JUMBO_57765;
15374 tp->bufmgr_config.mbuf_high_water_jumbo =
15375 DEFAULT_MB_HIGH_WATER_JUMBO_57765;
Joe Perches63c3a662011-04-26 08:12:10 +000015376 } else if (tg3_flag(tp, 5705_PLUS)) {
Michael Chanfdfec1722005-07-25 12:31:48 -070015377 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_5705;
15381 tp->bufmgr_config.mbuf_high_water =
15382 DEFAULT_MB_HIGH_WATER_5705;
Michael Chanb5d37722006-09-27 16:06:21 -070015383 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
15384 tp->bufmgr_config.mbuf_mac_rx_low_water =
15385 DEFAULT_MB_MACRX_LOW_WATER_5906;
15386 tp->bufmgr_config.mbuf_high_water =
15387 DEFAULT_MB_HIGH_WATER_5906;
15388 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015389
Michael Chanfdfec1722005-07-25 12:31:48 -070015390 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
15391 DEFAULT_MB_RDMA_LOW_WATER_JUMBO_5780;
15392 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
15393 DEFAULT_MB_MACRX_LOW_WATER_JUMBO_5780;
15394 tp->bufmgr_config.mbuf_high_water_jumbo =
15395 DEFAULT_MB_HIGH_WATER_JUMBO_5780;
15396 } else {
15397 tp->bufmgr_config.mbuf_read_dma_low_water =
15398 DEFAULT_MB_RDMA_LOW_WATER;
15399 tp->bufmgr_config.mbuf_mac_rx_low_water =
15400 DEFAULT_MB_MACRX_LOW_WATER;
15401 tp->bufmgr_config.mbuf_high_water =
15402 DEFAULT_MB_HIGH_WATER;
15403
15404 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
15405 DEFAULT_MB_RDMA_LOW_WATER_JUMBO;
15406 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
15407 DEFAULT_MB_MACRX_LOW_WATER_JUMBO;
15408 tp->bufmgr_config.mbuf_high_water_jumbo =
15409 DEFAULT_MB_HIGH_WATER_JUMBO;
15410 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015411
15412 tp->bufmgr_config.dma_low_water = DEFAULT_DMA_LOW_WATER;
15413 tp->bufmgr_config.dma_high_water = DEFAULT_DMA_HIGH_WATER;
15414}
15415
15416static char * __devinit tg3_phy_string(struct tg3 *tp)
15417{
Matt Carlson79eb6902010-02-17 15:17:03 +000015418 switch (tp->phy_id & TG3_PHY_ID_MASK) {
15419 case TG3_PHY_ID_BCM5400: return "5400";
15420 case TG3_PHY_ID_BCM5401: return "5401";
15421 case TG3_PHY_ID_BCM5411: return "5411";
15422 case TG3_PHY_ID_BCM5701: return "5701";
15423 case TG3_PHY_ID_BCM5703: return "5703";
15424 case TG3_PHY_ID_BCM5704: return "5704";
15425 case TG3_PHY_ID_BCM5705: return "5705";
15426 case TG3_PHY_ID_BCM5750: return "5750";
15427 case TG3_PHY_ID_BCM5752: return "5752";
15428 case TG3_PHY_ID_BCM5714: return "5714";
15429 case TG3_PHY_ID_BCM5780: return "5780";
15430 case TG3_PHY_ID_BCM5755: return "5755";
15431 case TG3_PHY_ID_BCM5787: return "5787";
15432 case TG3_PHY_ID_BCM5784: return "5784";
15433 case TG3_PHY_ID_BCM5756: return "5722/5756";
15434 case TG3_PHY_ID_BCM5906: return "5906";
15435 case TG3_PHY_ID_BCM5761: return "5761";
15436 case TG3_PHY_ID_BCM5718C: return "5718C";
15437 case TG3_PHY_ID_BCM5718S: return "5718S";
15438 case TG3_PHY_ID_BCM57765: return "57765";
Matt Carlson302b5002010-06-05 17:24:38 +000015439 case TG3_PHY_ID_BCM5719C: return "5719C";
Matt Carlson6418f2c2011-04-05 14:22:49 +000015440 case TG3_PHY_ID_BCM5720C: return "5720C";
Matt Carlson79eb6902010-02-17 15:17:03 +000015441 case TG3_PHY_ID_BCM8002: return "8002/serdes";
Linus Torvalds1da177e2005-04-16 15:20:36 -070015442 case 0: return "serdes";
15443 default: return "unknown";
Stephen Hemminger855e1112008-04-16 16:37:28 -070015444 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015445}
15446
Michael Chanf9804dd2005-09-27 12:13:10 -070015447static char * __devinit tg3_bus_string(struct tg3 *tp, char *str)
15448{
Joe Perches63c3a662011-04-26 08:12:10 +000015449 if (tg3_flag(tp, PCI_EXPRESS)) {
Michael Chanf9804dd2005-09-27 12:13:10 -070015450 strcpy(str, "PCI Express");
15451 return str;
Joe Perches63c3a662011-04-26 08:12:10 +000015452 } else if (tg3_flag(tp, PCIX_MODE)) {
Michael Chanf9804dd2005-09-27 12:13:10 -070015453 u32 clock_ctrl = tr32(TG3PCI_CLOCK_CTRL) & 0x1f;
15454
15455 strcpy(str, "PCIX:");
15456
15457 if ((clock_ctrl == 7) ||
15458 ((tr32(GRC_MISC_CFG) & GRC_MISC_CFG_BOARD_ID_MASK) ==
15459 GRC_MISC_CFG_BOARD_ID_5704CIOBE))
15460 strcat(str, "133MHz");
15461 else if (clock_ctrl == 0)
15462 strcat(str, "33MHz");
15463 else if (clock_ctrl == 2)
15464 strcat(str, "50MHz");
15465 else if (clock_ctrl == 4)
15466 strcat(str, "66MHz");
15467 else if (clock_ctrl == 6)
15468 strcat(str, "100MHz");
Michael Chanf9804dd2005-09-27 12:13:10 -070015469 } else {
15470 strcpy(str, "PCI:");
Joe Perches63c3a662011-04-26 08:12:10 +000015471 if (tg3_flag(tp, PCI_HIGH_SPEED))
Michael Chanf9804dd2005-09-27 12:13:10 -070015472 strcat(str, "66MHz");
15473 else
15474 strcat(str, "33MHz");
15475 }
Joe Perches63c3a662011-04-26 08:12:10 +000015476 if (tg3_flag(tp, PCI_32BIT))
Michael Chanf9804dd2005-09-27 12:13:10 -070015477 strcat(str, ":32-bit");
15478 else
15479 strcat(str, ":64-bit");
15480 return str;
15481}
15482
David S. Miller15f98502005-05-18 22:49:26 -070015483static void __devinit tg3_init_coal(struct tg3 *tp)
15484{
15485 struct ethtool_coalesce *ec = &tp->coal;
15486
15487 memset(ec, 0, sizeof(*ec));
15488 ec->cmd = ETHTOOL_GCOALESCE;
15489 ec->rx_coalesce_usecs = LOW_RXCOL_TICKS;
15490 ec->tx_coalesce_usecs = LOW_TXCOL_TICKS;
15491 ec->rx_max_coalesced_frames = LOW_RXMAX_FRAMES;
15492 ec->tx_max_coalesced_frames = LOW_TXMAX_FRAMES;
15493 ec->rx_coalesce_usecs_irq = DEFAULT_RXCOAL_TICK_INT;
15494 ec->tx_coalesce_usecs_irq = DEFAULT_TXCOAL_TICK_INT;
15495 ec->rx_max_coalesced_frames_irq = DEFAULT_RXCOAL_MAXF_INT;
15496 ec->tx_max_coalesced_frames_irq = DEFAULT_TXCOAL_MAXF_INT;
15497 ec->stats_block_coalesce_usecs = DEFAULT_STAT_COAL_TICKS;
15498
15499 if (tp->coalesce_mode & (HOSTCC_MODE_CLRTICK_RXBD |
15500 HOSTCC_MODE_CLRTICK_TXBD)) {
15501 ec->rx_coalesce_usecs = LOW_RXCOL_TICKS_CLRTCKS;
15502 ec->rx_coalesce_usecs_irq = DEFAULT_RXCOAL_TICK_INT_CLRTCKS;
15503 ec->tx_coalesce_usecs = LOW_TXCOL_TICKS_CLRTCKS;
15504 ec->tx_coalesce_usecs_irq = DEFAULT_TXCOAL_TICK_INT_CLRTCKS;
15505 }
Michael Chand244c892005-07-05 14:42:33 -070015506
Joe Perches63c3a662011-04-26 08:12:10 +000015507 if (tg3_flag(tp, 5705_PLUS)) {
Michael Chand244c892005-07-05 14:42:33 -070015508 ec->rx_coalesce_usecs_irq = 0;
15509 ec->tx_coalesce_usecs_irq = 0;
15510 ec->stats_block_coalesce_usecs = 0;
15511 }
David S. Miller15f98502005-05-18 22:49:26 -070015512}
15513
Linus Torvalds1da177e2005-04-16 15:20:36 -070015514static int __devinit tg3_init_one(struct pci_dev *pdev,
15515 const struct pci_device_id *ent)
15516{
Linus Torvalds1da177e2005-04-16 15:20:36 -070015517 struct net_device *dev;
15518 struct tg3 *tp;
Matt Carlson646c9ed2009-09-01 12:58:41 +000015519 int i, err, pm_cap;
15520 u32 sndmbx, rcvmbx, intmbx;
Michael Chanf9804dd2005-09-27 12:13:10 -070015521 char str[40];
Michael Chan72f2afb2006-03-06 19:28:35 -080015522 u64 dma_mask, persist_dma_mask;
Michał Mirosławc8f44af2011-11-15 15:29:55 +000015523 netdev_features_t features = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015524
Joe Perches05dbe002010-02-17 19:44:19 +000015525 printk_once(KERN_INFO "%s\n", version);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015526
15527 err = pci_enable_device(pdev);
15528 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000015529 dev_err(&pdev->dev, "Cannot enable PCI device, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070015530 return err;
15531 }
15532
Linus Torvalds1da177e2005-04-16 15:20:36 -070015533 err = pci_request_regions(pdev, DRV_MODULE_NAME);
15534 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000015535 dev_err(&pdev->dev, "Cannot obtain PCI resources, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070015536 goto err_out_disable_pdev;
15537 }
15538
15539 pci_set_master(pdev);
15540
15541 /* Find power-management capability. */
15542 pm_cap = pci_find_capability(pdev, PCI_CAP_ID_PM);
15543 if (pm_cap == 0) {
Matt Carlson2445e462010-04-05 10:19:21 +000015544 dev_err(&pdev->dev,
15545 "Cannot find Power Management capability, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070015546 err = -EIO;
15547 goto err_out_free_res;
15548 }
15549
Matt Carlson16821282011-07-13 09:27:28 +000015550 err = pci_set_power_state(pdev, PCI_D0);
15551 if (err) {
15552 dev_err(&pdev->dev, "Transition to D0 failed, aborting\n");
15553 goto err_out_free_res;
15554 }
15555
Matt Carlsonfe5f5782009-09-01 13:09:39 +000015556 dev = alloc_etherdev_mq(sizeof(*tp), TG3_IRQ_MAX_VECS);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015557 if (!dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070015558 err = -ENOMEM;
Matt Carlson16821282011-07-13 09:27:28 +000015559 goto err_out_power_down;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015560 }
15561
Linus Torvalds1da177e2005-04-16 15:20:36 -070015562 SET_NETDEV_DEV(dev, &pdev->dev);
15563
Linus Torvalds1da177e2005-04-16 15:20:36 -070015564 tp = netdev_priv(dev);
15565 tp->pdev = pdev;
15566 tp->dev = dev;
15567 tp->pm_cap = pm_cap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015568 tp->rx_mode = TG3_DEF_RX_MODE;
15569 tp->tx_mode = TG3_DEF_TX_MODE;
Matt Carlson8ef21422008-05-02 16:47:53 -070015570
Linus Torvalds1da177e2005-04-16 15:20:36 -070015571 if (tg3_debug > 0)
15572 tp->msg_enable = tg3_debug;
15573 else
15574 tp->msg_enable = TG3_DEF_MSG_ENABLE;
15575
15576 /* The word/byte swap controls here control register access byte
15577 * swapping. DMA data byte swapping is controlled in the GRC_MODE
15578 * setting below.
15579 */
15580 tp->misc_host_ctrl =
15581 MISC_HOST_CTRL_MASK_PCI_INT |
15582 MISC_HOST_CTRL_WORD_SWAP |
15583 MISC_HOST_CTRL_INDIR_ACCESS |
15584 MISC_HOST_CTRL_PCISTATE_RW;
15585
15586 /* The NONFRM (non-frame) byte/word swap controls take effect
15587 * on descriptor entries, anything which isn't packet data.
15588 *
15589 * The StrongARM chips on the board (one for tx, one for rx)
15590 * are running in big-endian mode.
15591 */
15592 tp->grc_mode = (GRC_MODE_WSWAP_DATA | GRC_MODE_BSWAP_DATA |
15593 GRC_MODE_WSWAP_NONFRM_DATA);
15594#ifdef __BIG_ENDIAN
15595 tp->grc_mode |= GRC_MODE_BSWAP_NONFRM_DATA;
15596#endif
15597 spin_lock_init(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015598 spin_lock_init(&tp->indirect_lock);
David Howellsc4028952006-11-22 14:57:56 +000015599 INIT_WORK(&tp->reset_task, tg3_reset_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015600
Matt Carlsond5fe4882008-11-21 17:20:32 -080015601 tp->regs = pci_ioremap_bar(pdev, BAR_0);
Andy Gospodarekab0049b2007-09-06 20:42:14 +010015602 if (!tp->regs) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015603 dev_err(&pdev->dev, "Cannot map device registers, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070015604 err = -ENOMEM;
15605 goto err_out_free_dev;
15606 }
15607
Matt Carlsonc9cab242011-07-13 09:27:27 +000015608 if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761 ||
15609 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761E ||
15610 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761S ||
15611 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761SE ||
15612 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717 ||
15613 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718 ||
15614 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5719 ||
15615 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5720) {
15616 tg3_flag_set(tp, ENABLE_APE);
15617 tp->aperegs = pci_ioremap_bar(pdev, BAR_2);
15618 if (!tp->aperegs) {
15619 dev_err(&pdev->dev,
15620 "Cannot map APE registers, aborting\n");
15621 err = -ENOMEM;
15622 goto err_out_iounmap;
15623 }
15624 }
15625
Linus Torvalds1da177e2005-04-16 15:20:36 -070015626 tp->rx_pending = TG3_DEF_RX_RING_PENDING;
15627 tp->rx_jumbo_pending = TG3_DEF_RX_JUMBO_RING_PENDING;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015628
Linus Torvalds1da177e2005-04-16 15:20:36 -070015629 dev->ethtool_ops = &tg3_ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015630 dev->watchdog_timeo = TG3_TX_TIMEOUT;
Matt Carlson2ffcc982011-05-19 12:12:44 +000015631 dev->netdev_ops = &tg3_netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015632 dev->irq = pdev->irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015633
15634 err = tg3_get_invariants(tp);
15635 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015636 dev_err(&pdev->dev,
15637 "Problem fetching invariants of chip, aborting\n");
Matt Carlsonc9cab242011-07-13 09:27:27 +000015638 goto err_out_apeunmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015639 }
15640
Michael Chan4a29cc22006-03-19 13:21:12 -080015641 /* The EPB bridge inside 5714, 5715, and 5780 and any
15642 * device behind the EPB cannot support DMA addresses > 40-bit.
Michael Chan72f2afb2006-03-06 19:28:35 -080015643 * On 64-bit systems with IOMMU, use 40-bit dma_mask.
15644 * On 64-bit systems without IOMMU, use 64-bit dma_mask and
15645 * do DMA address check in tg3_start_xmit().
15646 */
Joe Perches63c3a662011-04-26 08:12:10 +000015647 if (tg3_flag(tp, IS_5788))
Yang Hongyang284901a2009-04-06 19:01:15 -070015648 persist_dma_mask = dma_mask = DMA_BIT_MASK(32);
Joe Perches63c3a662011-04-26 08:12:10 +000015649 else if (tg3_flag(tp, 40BIT_DMA_BUG)) {
Yang Hongyang50cf1562009-04-06 19:01:14 -070015650 persist_dma_mask = dma_mask = DMA_BIT_MASK(40);
Michael Chan72f2afb2006-03-06 19:28:35 -080015651#ifdef CONFIG_HIGHMEM
Yang Hongyang6a355282009-04-06 19:01:13 -070015652 dma_mask = DMA_BIT_MASK(64);
Michael Chan72f2afb2006-03-06 19:28:35 -080015653#endif
Michael Chan4a29cc22006-03-19 13:21:12 -080015654 } else
Yang Hongyang6a355282009-04-06 19:01:13 -070015655 persist_dma_mask = dma_mask = DMA_BIT_MASK(64);
Michael Chan72f2afb2006-03-06 19:28:35 -080015656
15657 /* Configure DMA attributes. */
Yang Hongyang284901a2009-04-06 19:01:15 -070015658 if (dma_mask > DMA_BIT_MASK(32)) {
Michael Chan72f2afb2006-03-06 19:28:35 -080015659 err = pci_set_dma_mask(pdev, dma_mask);
15660 if (!err) {
Matt Carlson0da06062011-05-19 12:12:53 +000015661 features |= NETIF_F_HIGHDMA;
Michael Chan72f2afb2006-03-06 19:28:35 -080015662 err = pci_set_consistent_dma_mask(pdev,
15663 persist_dma_mask);
15664 if (err < 0) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015665 dev_err(&pdev->dev, "Unable to obtain 64 bit "
15666 "DMA for consistent allocations\n");
Matt Carlsonc9cab242011-07-13 09:27:27 +000015667 goto err_out_apeunmap;
Michael Chan72f2afb2006-03-06 19:28:35 -080015668 }
15669 }
15670 }
Yang Hongyang284901a2009-04-06 19:01:15 -070015671 if (err || dma_mask == DMA_BIT_MASK(32)) {
15672 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
Michael Chan72f2afb2006-03-06 19:28:35 -080015673 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015674 dev_err(&pdev->dev,
15675 "No usable DMA configuration, aborting\n");
Matt Carlsonc9cab242011-07-13 09:27:27 +000015676 goto err_out_apeunmap;
Michael Chan72f2afb2006-03-06 19:28:35 -080015677 }
15678 }
15679
Michael Chanfdfec1722005-07-25 12:31:48 -070015680 tg3_init_bufmgr_config(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015681
Matt Carlson0da06062011-05-19 12:12:53 +000015682 features |= NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
15683
15684 /* 5700 B0 chips do not support checksumming correctly due
15685 * to hardware bugs.
15686 */
15687 if (tp->pci_chip_rev_id != CHIPREV_ID_5700_B0) {
15688 features |= NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_RXCSUM;
15689
15690 if (tg3_flag(tp, 5755_PLUS))
15691 features |= NETIF_F_IPV6_CSUM;
15692 }
15693
Michael Chan4e3a7aa2006-03-20 17:47:44 -080015694 /* TSO is on by default on chips that support hardware TSO.
15695 * Firmware TSO on older chips gives lower performance, so it
15696 * is off by default, but can be enabled using ethtool.
15697 */
Joe Perches63c3a662011-04-26 08:12:10 +000015698 if ((tg3_flag(tp, HW_TSO_1) ||
15699 tg3_flag(tp, HW_TSO_2) ||
15700 tg3_flag(tp, HW_TSO_3)) &&
Matt Carlson0da06062011-05-19 12:12:53 +000015701 (features & NETIF_F_IP_CSUM))
15702 features |= NETIF_F_TSO;
Joe Perches63c3a662011-04-26 08:12:10 +000015703 if (tg3_flag(tp, HW_TSO_2) || tg3_flag(tp, HW_TSO_3)) {
Matt Carlson0da06062011-05-19 12:12:53 +000015704 if (features & NETIF_F_IPV6_CSUM)
15705 features |= NETIF_F_TSO6;
Joe Perches63c3a662011-04-26 08:12:10 +000015706 if (tg3_flag(tp, HW_TSO_3) ||
Matt Carlsone849cdc2009-11-13 13:03:38 +000015707 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson57e69832008-05-25 23:48:31 -070015708 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
15709 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX) ||
Joe Perches63c3a662011-04-26 08:12:10 +000015710 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Michał Mirosławdc668912011-04-07 03:35:07 +000015711 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
Matt Carlson0da06062011-05-19 12:12:53 +000015712 features |= NETIF_F_TSO_ECN;
Michael Chanb0026622006-07-03 19:42:14 -070015713 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015714
Matt Carlsond542fe22011-05-19 16:02:43 +000015715 dev->features |= features;
15716 dev->vlan_features |= features;
15717
Mahesh Bandewar06c03c02011-05-08 06:51:48 +000015718 /*
15719 * Add loopback capability only for a subset of devices that support
15720 * MAC-LOOPBACK. Eventually this need to be enhanced to allow INT-PHY
15721 * loopback for the remaining devices.
15722 */
15723 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5780 &&
15724 !tg3_flag(tp, CPMU_PRESENT))
15725 /* Add the loopback capability */
Matt Carlson0da06062011-05-19 12:12:53 +000015726 features |= NETIF_F_LOOPBACK;
15727
Matt Carlson0da06062011-05-19 12:12:53 +000015728 dev->hw_features |= features;
Mahesh Bandewar06c03c02011-05-08 06:51:48 +000015729
Linus Torvalds1da177e2005-04-16 15:20:36 -070015730 if (tp->pci_chip_rev_id == CHIPREV_ID_5705_A1 &&
Joe Perches63c3a662011-04-26 08:12:10 +000015731 !tg3_flag(tp, TSO_CAPABLE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070015732 !(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH)) {
Joe Perches63c3a662011-04-26 08:12:10 +000015733 tg3_flag_set(tp, MAX_RXPEND_64);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015734 tp->rx_pending = 63;
15735 }
15736
Linus Torvalds1da177e2005-04-16 15:20:36 -070015737 err = tg3_get_device_address(tp);
15738 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015739 dev_err(&pdev->dev,
15740 "Could not obtain valid ethernet address, aborting\n");
Matt Carlsonc9cab242011-07-13 09:27:27 +000015741 goto err_out_apeunmap;
Matt Carlson0d3031d2007-10-10 18:02:43 -070015742 }
15743
Matt Carlsonc88864d2007-11-12 21:07:01 -080015744 /*
15745 * Reset chip in case UNDI or EFI driver did not shutdown
15746 * DMA self test will enable WDMAC and we'll see (spurious)
15747 * pending DMA on the PCI bus at that point.
15748 */
15749 if ((tr32(HOSTCC_MODE) & HOSTCC_MODE_ENABLE) ||
15750 (tr32(WDMAC_MODE) & WDMAC_MODE_ENABLE)) {
15751 tw32(MEMARB_MODE, MEMARB_MODE_ENABLE);
15752 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
15753 }
15754
15755 err = tg3_test_dma(tp);
15756 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015757 dev_err(&pdev->dev, "DMA engine test failed, aborting\n");
Matt Carlsonc88864d2007-11-12 21:07:01 -080015758 goto err_out_apeunmap;
15759 }
15760
Matt Carlson78f90dc2009-11-13 13:03:42 +000015761 intmbx = MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW;
15762 rcvmbx = MAILBOX_RCVRET_CON_IDX_0 + TG3_64BIT_REG_LOW;
15763 sndmbx = MAILBOX_SNDHOST_PROD_IDX_0 + TG3_64BIT_REG_LOW;
Matt Carlson6fd45cb2010-09-15 08:59:57 +000015764 for (i = 0; i < tp->irq_max; i++) {
Matt Carlson78f90dc2009-11-13 13:03:42 +000015765 struct tg3_napi *tnapi = &tp->napi[i];
15766
15767 tnapi->tp = tp;
15768 tnapi->tx_pending = TG3_DEF_TX_RING_PENDING;
15769
15770 tnapi->int_mbox = intmbx;
Matt Carlson93a700a2011-08-31 11:44:54 +000015771 if (i <= 4)
Matt Carlson78f90dc2009-11-13 13:03:42 +000015772 intmbx += 0x8;
15773 else
15774 intmbx += 0x4;
15775
15776 tnapi->consmbox = rcvmbx;
15777 tnapi->prodmbox = sndmbx;
15778
Matt Carlson66cfd1b2010-09-30 10:34:30 +000015779 if (i)
Matt Carlson78f90dc2009-11-13 13:03:42 +000015780 tnapi->coal_now = HOSTCC_MODE_COAL_VEC1_NOW << (i - 1);
Matt Carlson66cfd1b2010-09-30 10:34:30 +000015781 else
Matt Carlson78f90dc2009-11-13 13:03:42 +000015782 tnapi->coal_now = HOSTCC_MODE_NOW;
Matt Carlson78f90dc2009-11-13 13:03:42 +000015783
Joe Perches63c3a662011-04-26 08:12:10 +000015784 if (!tg3_flag(tp, SUPPORT_MSIX))
Matt Carlson78f90dc2009-11-13 13:03:42 +000015785 break;
15786
15787 /*
15788 * If we support MSIX, we'll be using RSS. If we're using
15789 * RSS, the first vector only handles link interrupts and the
15790 * remaining vectors handle rx and tx interrupts. Reuse the
15791 * mailbox values for the next iteration. The values we setup
15792 * above are still useful for the single vectored mode.
15793 */
15794 if (!i)
15795 continue;
15796
15797 rcvmbx += 0x8;
15798
15799 if (sndmbx & 0x4)
15800 sndmbx -= 0x4;
15801 else
15802 sndmbx += 0xc;
15803 }
15804
Matt Carlsonc88864d2007-11-12 21:07:01 -080015805 tg3_init_coal(tp);
15806
Michael Chanc49a1562006-12-17 17:07:29 -080015807 pci_set_drvdata(pdev, dev);
15808
Matt Carlsoncd0d7222011-07-13 09:27:33 +000015809 if (tg3_flag(tp, 5717_PLUS)) {
15810 /* Resume a low-power mode */
15811 tg3_frob_aux_power(tp, false);
15812 }
15813
Matt Carlson21f76382012-02-22 12:35:21 +000015814 tg3_timer_init(tp);
15815
Linus Torvalds1da177e2005-04-16 15:20:36 -070015816 err = register_netdev(dev);
15817 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015818 dev_err(&pdev->dev, "Cannot register net device, aborting\n");
Matt Carlson0d3031d2007-10-10 18:02:43 -070015819 goto err_out_apeunmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015820 }
15821
Joe Perches05dbe002010-02-17 19:44:19 +000015822 netdev_info(dev, "Tigon3 [partno(%s) rev %04x] (%s) MAC address %pM\n",
15823 tp->board_part_number,
15824 tp->pci_chip_rev_id,
15825 tg3_bus_string(tp, str),
15826 dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015827
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015828 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000015829 struct phy_device *phydev;
15830 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlson5129c3a2010-04-05 10:19:23 +000015831 netdev_info(dev,
15832 "attached PHY driver [%s] (mii_bus:phy_addr=%s)\n",
Joe Perches05dbe002010-02-17 19:44:19 +000015833 phydev->drv->name, dev_name(&phydev->dev));
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015834 } else {
15835 char *ethtype;
15836
15837 if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
15838 ethtype = "10/100Base-TX";
15839 else if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
15840 ethtype = "1000Base-SX";
15841 else
15842 ethtype = "10/100/1000Base-T";
15843
Matt Carlson5129c3a2010-04-05 10:19:23 +000015844 netdev_info(dev, "attached PHY is %s (%s Ethernet) "
Matt Carlson47007832011-04-20 07:57:43 +000015845 "(WireSpeed[%d], EEE[%d])\n",
15846 tg3_phy_string(tp), ethtype,
15847 (tp->phy_flags & TG3_PHYFLG_NO_ETH_WIRE_SPEED) == 0,
15848 (tp->phy_flags & TG3_PHYFLG_EEE_CAP) != 0);
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015849 }
Matt Carlsondf59c942008-11-03 16:52:56 -080015850
Joe Perches05dbe002010-02-17 19:44:19 +000015851 netdev_info(dev, "RXcsums[%d] LinkChgREG[%d] MIirq[%d] ASF[%d] TSOcap[%d]\n",
Michał Mirosławdc668912011-04-07 03:35:07 +000015852 (dev->features & NETIF_F_RXCSUM) != 0,
Joe Perches63c3a662011-04-26 08:12:10 +000015853 tg3_flag(tp, USE_LINKCHG_REG) != 0,
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015854 (tp->phy_flags & TG3_PHYFLG_USE_MI_INTERRUPT) != 0,
Joe Perches63c3a662011-04-26 08:12:10 +000015855 tg3_flag(tp, ENABLE_ASF) != 0,
15856 tg3_flag(tp, TSO_CAPABLE) != 0);
Joe Perches05dbe002010-02-17 19:44:19 +000015857 netdev_info(dev, "dma_rwctrl[%08x] dma_mask[%d-bit]\n",
15858 tp->dma_rwctrl,
15859 pdev->dma_mask == DMA_BIT_MASK(32) ? 32 :
15860 ((u64)pdev->dma_mask) == DMA_BIT_MASK(40) ? 40 : 64);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015861
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000015862 pci_save_state(pdev);
15863
Linus Torvalds1da177e2005-04-16 15:20:36 -070015864 return 0;
15865
Matt Carlson0d3031d2007-10-10 18:02:43 -070015866err_out_apeunmap:
15867 if (tp->aperegs) {
15868 iounmap(tp->aperegs);
15869 tp->aperegs = NULL;
15870 }
15871
Linus Torvalds1da177e2005-04-16 15:20:36 -070015872err_out_iounmap:
Michael Chan68929142005-08-09 20:17:14 -070015873 if (tp->regs) {
15874 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070015875 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070015876 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015877
15878err_out_free_dev:
15879 free_netdev(dev);
15880
Matt Carlson16821282011-07-13 09:27:28 +000015881err_out_power_down:
15882 pci_set_power_state(pdev, PCI_D3hot);
15883
Linus Torvalds1da177e2005-04-16 15:20:36 -070015884err_out_free_res:
15885 pci_release_regions(pdev);
15886
15887err_out_disable_pdev:
15888 pci_disable_device(pdev);
15889 pci_set_drvdata(pdev, NULL);
15890 return err;
15891}
15892
15893static void __devexit tg3_remove_one(struct pci_dev *pdev)
15894{
15895 struct net_device *dev = pci_get_drvdata(pdev);
15896
15897 if (dev) {
15898 struct tg3 *tp = netdev_priv(dev);
15899
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -080015900 if (tp->fw)
15901 release_firmware(tp->fw);
15902
Matt Carlsondb219972011-11-04 09:15:03 +000015903 tg3_reset_task_cancel(tp);
Matt Carlson158d7ab2008-05-29 01:37:54 -070015904
David S. Miller1805b2f2011-10-24 18:18:09 -040015905 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015906 tg3_phy_fini(tp);
Matt Carlson158d7ab2008-05-29 01:37:54 -070015907 tg3_mdio_fini(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015908 }
Matt Carlson158d7ab2008-05-29 01:37:54 -070015909
Linus Torvalds1da177e2005-04-16 15:20:36 -070015910 unregister_netdev(dev);
Matt Carlson0d3031d2007-10-10 18:02:43 -070015911 if (tp->aperegs) {
15912 iounmap(tp->aperegs);
15913 tp->aperegs = NULL;
15914 }
Michael Chan68929142005-08-09 20:17:14 -070015915 if (tp->regs) {
15916 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070015917 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070015918 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015919 free_netdev(dev);
15920 pci_release_regions(pdev);
15921 pci_disable_device(pdev);
15922 pci_set_drvdata(pdev, NULL);
15923 }
15924}
15925
Eric Dumazetaa6027c2011-01-01 05:22:46 +000015926#ifdef CONFIG_PM_SLEEP
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015927static int tg3_suspend(struct device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015928{
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015929 struct pci_dev *pdev = to_pci_dev(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015930 struct net_device *dev = pci_get_drvdata(pdev);
15931 struct tg3 *tp = netdev_priv(dev);
15932 int err;
15933
15934 if (!netif_running(dev))
15935 return 0;
15936
Matt Carlsondb219972011-11-04 09:15:03 +000015937 tg3_reset_task_cancel(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015938 tg3_phy_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015939 tg3_netif_stop(tp);
15940
Matt Carlson21f76382012-02-22 12:35:21 +000015941 tg3_timer_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015942
David S. Millerf47c11e2005-06-24 20:18:35 -070015943 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015944 tg3_disable_ints(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -070015945 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015946
15947 netif_device_detach(dev);
15948
David S. Millerf47c11e2005-06-24 20:18:35 -070015949 tg3_full_lock(tp, 0);
Michael Chan944d9802005-05-29 14:57:48 -070015950 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Joe Perches63c3a662011-04-26 08:12:10 +000015951 tg3_flag_clear(tp, INIT_COMPLETE);
David S. Millerf47c11e2005-06-24 20:18:35 -070015952 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015953
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015954 err = tg3_power_down_prepare(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015955 if (err) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015956 int err2;
15957
David S. Millerf47c11e2005-06-24 20:18:35 -070015958 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015959
Joe Perches63c3a662011-04-26 08:12:10 +000015960 tg3_flag_set(tp, INIT_COMPLETE);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015961 err2 = tg3_restart_hw(tp, 1);
15962 if (err2)
Michael Chanb9ec6c12006-07-25 16:37:27 -070015963 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015964
Matt Carlson21f76382012-02-22 12:35:21 +000015965 tg3_timer_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015966
15967 netif_device_attach(dev);
15968 tg3_netif_start(tp);
15969
Michael Chanb9ec6c12006-07-25 16:37:27 -070015970out:
David S. Millerf47c11e2005-06-24 20:18:35 -070015971 tg3_full_unlock(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015972
15973 if (!err2)
15974 tg3_phy_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015975 }
15976
15977 return err;
15978}
15979
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015980static int tg3_resume(struct device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015981{
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015982 struct pci_dev *pdev = to_pci_dev(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015983 struct net_device *dev = pci_get_drvdata(pdev);
15984 struct tg3 *tp = netdev_priv(dev);
15985 int err;
15986
15987 if (!netif_running(dev))
15988 return 0;
15989
Linus Torvalds1da177e2005-04-16 15:20:36 -070015990 netif_device_attach(dev);
15991
David S. Millerf47c11e2005-06-24 20:18:35 -070015992 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015993
Joe Perches63c3a662011-04-26 08:12:10 +000015994 tg3_flag_set(tp, INIT_COMPLETE);
Michael Chanb9ec6c12006-07-25 16:37:27 -070015995 err = tg3_restart_hw(tp, 1);
15996 if (err)
15997 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015998
Matt Carlson21f76382012-02-22 12:35:21 +000015999 tg3_timer_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016000
Linus Torvalds1da177e2005-04-16 15:20:36 -070016001 tg3_netif_start(tp);
16002
Michael Chanb9ec6c12006-07-25 16:37:27 -070016003out:
David S. Millerf47c11e2005-06-24 20:18:35 -070016004 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016005
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070016006 if (!err)
16007 tg3_phy_start(tp);
16008
Michael Chanb9ec6c12006-07-25 16:37:27 -070016009 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016010}
16011
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000016012static SIMPLE_DEV_PM_OPS(tg3_pm_ops, tg3_suspend, tg3_resume);
Eric Dumazetaa6027c2011-01-01 05:22:46 +000016013#define TG3_PM_OPS (&tg3_pm_ops)
16014
16015#else
16016
16017#define TG3_PM_OPS NULL
16018
16019#endif /* CONFIG_PM_SLEEP */
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000016020
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000016021/**
16022 * tg3_io_error_detected - called when PCI error is detected
16023 * @pdev: Pointer to PCI device
16024 * @state: The current pci connection state
16025 *
16026 * This function is called after a PCI bus error affecting
16027 * this device has been detected.
16028 */
16029static pci_ers_result_t tg3_io_error_detected(struct pci_dev *pdev,
16030 pci_channel_state_t state)
16031{
16032 struct net_device *netdev = pci_get_drvdata(pdev);
16033 struct tg3 *tp = netdev_priv(netdev);
16034 pci_ers_result_t err = PCI_ERS_RESULT_NEED_RESET;
16035
16036 netdev_info(netdev, "PCI I/O error detected\n");
16037
16038 rtnl_lock();
16039
16040 if (!netif_running(netdev))
16041 goto done;
16042
16043 tg3_phy_stop(tp);
16044
16045 tg3_netif_stop(tp);
16046
Matt Carlson21f76382012-02-22 12:35:21 +000016047 tg3_timer_stop(tp);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000016048
16049 /* Want to make sure that the reset task doesn't run */
Matt Carlsondb219972011-11-04 09:15:03 +000016050 tg3_reset_task_cancel(tp);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000016051
16052 netif_device_detach(netdev);
16053
16054 /* Clean up software state, even if MMIO is blocked */
16055 tg3_full_lock(tp, 0);
16056 tg3_halt(tp, RESET_KIND_SHUTDOWN, 0);
16057 tg3_full_unlock(tp);
16058
16059done:
16060 if (state == pci_channel_io_perm_failure)
16061 err = PCI_ERS_RESULT_DISCONNECT;
16062 else
16063 pci_disable_device(pdev);
16064
16065 rtnl_unlock();
16066
16067 return err;
16068}
16069
16070/**
16071 * tg3_io_slot_reset - called after the pci bus has been reset.
16072 * @pdev: Pointer to PCI device
16073 *
16074 * Restart the card from scratch, as if from a cold-boot.
16075 * At this point, the card has exprienced a hard reset,
16076 * followed by fixups by BIOS, and has its config space
16077 * set up identically to what it was at cold boot.
16078 */
16079static pci_ers_result_t tg3_io_slot_reset(struct pci_dev *pdev)
16080{
16081 struct net_device *netdev = pci_get_drvdata(pdev);
16082 struct tg3 *tp = netdev_priv(netdev);
16083 pci_ers_result_t rc = PCI_ERS_RESULT_DISCONNECT;
16084 int err;
16085
16086 rtnl_lock();
16087
16088 if (pci_enable_device(pdev)) {
16089 netdev_err(netdev, "Cannot re-enable PCI device after reset.\n");
16090 goto done;
16091 }
16092
16093 pci_set_master(pdev);
16094 pci_restore_state(pdev);
16095 pci_save_state(pdev);
16096
16097 if (!netif_running(netdev)) {
16098 rc = PCI_ERS_RESULT_RECOVERED;
16099 goto done;
16100 }
16101
16102 err = tg3_power_up(tp);
Matt Carlsonbed98292011-07-13 09:27:29 +000016103 if (err)
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000016104 goto done;
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000016105
16106 rc = PCI_ERS_RESULT_RECOVERED;
16107
16108done:
16109 rtnl_unlock();
16110
16111 return rc;
16112}
16113
16114/**
16115 * tg3_io_resume - called when traffic can start flowing again.
16116 * @pdev: Pointer to PCI device
16117 *
16118 * This callback is called when the error recovery driver tells
16119 * us that its OK to resume normal operation.
16120 */
16121static void tg3_io_resume(struct pci_dev *pdev)
16122{
16123 struct net_device *netdev = pci_get_drvdata(pdev);
16124 struct tg3 *tp = netdev_priv(netdev);
16125 int err;
16126
16127 rtnl_lock();
16128
16129 if (!netif_running(netdev))
16130 goto done;
16131
16132 tg3_full_lock(tp, 0);
Joe Perches63c3a662011-04-26 08:12:10 +000016133 tg3_flag_set(tp, INIT_COMPLETE);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000016134 err = tg3_restart_hw(tp, 1);
16135 tg3_full_unlock(tp);
16136 if (err) {
16137 netdev_err(netdev, "Cannot restart hardware after reset.\n");
16138 goto done;
16139 }
16140
16141 netif_device_attach(netdev);
16142
Matt Carlson21f76382012-02-22 12:35:21 +000016143 tg3_timer_start(tp);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000016144
16145 tg3_netif_start(tp);
16146
16147 tg3_phy_start(tp);
16148
16149done:
16150 rtnl_unlock();
16151}
16152
16153static struct pci_error_handlers tg3_err_handler = {
16154 .error_detected = tg3_io_error_detected,
16155 .slot_reset = tg3_io_slot_reset,
16156 .resume = tg3_io_resume
16157};
16158
Linus Torvalds1da177e2005-04-16 15:20:36 -070016159static struct pci_driver tg3_driver = {
16160 .name = DRV_MODULE_NAME,
16161 .id_table = tg3_pci_tbl,
16162 .probe = tg3_init_one,
16163 .remove = __devexit_p(tg3_remove_one),
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000016164 .err_handler = &tg3_err_handler,
Eric Dumazetaa6027c2011-01-01 05:22:46 +000016165 .driver.pm = TG3_PM_OPS,
Linus Torvalds1da177e2005-04-16 15:20:36 -070016166};
16167
16168static int __init tg3_init(void)
16169{
Jeff Garzik29917622006-08-19 17:48:59 -040016170 return pci_register_driver(&tg3_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016171}
16172
16173static void __exit tg3_cleanup(void)
16174{
16175 pci_unregister_driver(&tg3_driver);
16176}
16177
16178module_init(tg3_init);
16179module_exit(tg3_cleanup);