blob: e47ff8be1d7b5c27be543c7d41f4584d56054644 [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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198/* minimum number of free TX descriptors required to wake up TX process */
Matt Carlsonf3f3f272009-08-28 14:03:21 +0000199#define TG3_TX_WAKEUP_THRESH(tnapi) ((tnapi)->tx_pending / 4)
Matt Carlson55086ad2011-12-14 11:09:59 +0000200#define TG3_TX_BD_DMA_MAX_2K 2048
Matt Carlsona4cb4282011-12-14 11:09:58 +0000201#define TG3_TX_BD_DMA_MAX_4K 4096
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
Matt Carlsonad829262008-11-21 17:16:16 -0800203#define TG3_RAW_IP_ALIGN 2
204
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000205#define TG3_FW_UPDATE_TIMEOUT_SEC 5
Matt Carlson21f76382012-02-22 12:35:21 +0000206#define TG3_FW_UPDATE_FREQ_SEC (TG3_FW_UPDATE_TIMEOUT_SEC / 2)
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000207
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -0800208#define FIRMWARE_TG3 "tigon/tg3.bin"
209#define FIRMWARE_TG3TSO "tigon/tg3_tso.bin"
210#define FIRMWARE_TG3TSO5 "tigon/tg3_tso5.bin"
211
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212static char version[] __devinitdata =
Joe Perches05dbe002010-02-17 19:44:19 +0000213 DRV_MODULE_NAME ".c:v" DRV_MODULE_VERSION " (" DRV_MODULE_RELDATE ")";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214
215MODULE_AUTHOR("David S. Miller (davem@redhat.com) and Jeff Garzik (jgarzik@pobox.com)");
216MODULE_DESCRIPTION("Broadcom Tigon3 ethernet driver");
217MODULE_LICENSE("GPL");
218MODULE_VERSION(DRV_MODULE_VERSION);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -0800219MODULE_FIRMWARE(FIRMWARE_TG3);
220MODULE_FIRMWARE(FIRMWARE_TG3TSO);
221MODULE_FIRMWARE(FIRMWARE_TG3TSO5);
222
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223static int tg3_debug = -1; /* -1 == use TG3_DEF_MSG_ENABLE as value */
224module_param(tg3_debug, int, 0);
225MODULE_PARM_DESC(tg3_debug, "Tigon3 bitmapped debugging message enable value");
226
Alexey Dobriyana3aa1882010-01-07 11:58:11 +0000227static DEFINE_PCI_DEVICE_TABLE(tg3_pci_tbl) = {
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700228 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5700)},
229 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5701)},
230 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5702)},
231 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5703)},
232 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5704)},
233 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5702FE)},
234 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705)},
235 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705_2)},
236 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705M)},
237 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705M_2)},
238 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5702X)},
239 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5703X)},
240 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5704S)},
241 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5702A3)},
242 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5703A3)},
243 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5782)},
244 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5788)},
245 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5789)},
246 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5901)},
247 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5901_2)},
248 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5704S_2)},
249 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705F)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700250 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5721)},
Michael Chan126a3362006-09-27 16:03:07 -0700251 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5722)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700252 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5751)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700253 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5751M)},
254 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5751F)},
255 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5752)},
256 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5752M)},
257 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5753)},
258 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5753M)},
259 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5753F)},
260 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5754)},
261 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5754M)},
262 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5755)},
263 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5755M)},
Michael Chan126a3362006-09-27 16:03:07 -0700264 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5756)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700265 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5786)},
266 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5787)},
267 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5787M)},
Michael Chan676917d2006-12-07 00:20:22 -0800268 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5787F)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700269 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5714)},
270 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5714S)},
271 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5715)},
272 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5715S)},
273 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5780)},
274 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5780S)},
275 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5781)},
Michael Chanb5d37722006-09-27 16:06:21 -0700276 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5906)},
277 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5906M)},
Matt Carlsond30cdd22007-10-07 23:28:35 -0700278 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5784)},
279 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5764)},
Matt Carlson6c7af272007-10-21 16:12:02 -0700280 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5723)},
Matt Carlson9936bcf2007-10-10 18:03:07 -0700281 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5761)},
282 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5761E)},
Matt Carlsonc88e6682008-11-03 16:49:18 -0800283 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5761S)},
284 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5761SE)},
Matt Carlson2befdce2009-08-28 12:28:45 +0000285 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5785_G)},
286 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5785_F)},
Matt Carlson321d32a2008-11-21 17:22:19 -0800287 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57780)},
288 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57760)},
289 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57790)},
Matt Carlson5e7ccf22009-08-25 10:08:42 +0000290 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57788)},
Matt Carlson5001e2f2009-11-13 13:03:51 +0000291 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5717)},
292 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5718)},
Matt Carlsonb0f75222010-01-20 16:58:11 +0000293 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57781)},
294 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57785)},
295 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57761)},
296 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57765)},
297 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57791)},
298 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57795)},
Matt Carlson302b5002010-06-05 17:24:38 +0000299 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5719)},
Matt Carlsonba1f3c72011-04-05 14:22:50 +0000300 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5720)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700301 {PCI_DEVICE(PCI_VENDOR_ID_SYSKONNECT, PCI_DEVICE_ID_SYSKONNECT_9DXX)},
302 {PCI_DEVICE(PCI_VENDOR_ID_SYSKONNECT, PCI_DEVICE_ID_SYSKONNECT_9MXX)},
303 {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC1000)},
304 {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC1001)},
305 {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC1003)},
306 {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC9100)},
307 {PCI_DEVICE(PCI_VENDOR_ID_APPLE, PCI_DEVICE_ID_APPLE_TIGON3)},
Meelis Roos1dcb14d2011-05-25 05:43:47 +0000308 {PCI_DEVICE(0x10cf, 0x11a2)}, /* Fujitsu 1000base-SX with BCM5703SKHB */
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700309 {}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310};
311
312MODULE_DEVICE_TABLE(pci, tg3_pci_tbl);
313
Andreas Mohr50da8592006-08-14 23:54:30 -0700314static const struct {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 const char string[ETH_GSTRING_LEN];
Matt Carlson48fa55a2011-04-13 11:05:06 +0000316} ethtool_stats_keys[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 { "rx_octets" },
318 { "rx_fragments" },
319 { "rx_ucast_packets" },
320 { "rx_mcast_packets" },
321 { "rx_bcast_packets" },
322 { "rx_fcs_errors" },
323 { "rx_align_errors" },
324 { "rx_xon_pause_rcvd" },
325 { "rx_xoff_pause_rcvd" },
326 { "rx_mac_ctrl_rcvd" },
327 { "rx_xoff_entered" },
328 { "rx_frame_too_long_errors" },
329 { "rx_jabbers" },
330 { "rx_undersize_packets" },
331 { "rx_in_length_errors" },
332 { "rx_out_length_errors" },
333 { "rx_64_or_less_octet_packets" },
334 { "rx_65_to_127_octet_packets" },
335 { "rx_128_to_255_octet_packets" },
336 { "rx_256_to_511_octet_packets" },
337 { "rx_512_to_1023_octet_packets" },
338 { "rx_1024_to_1522_octet_packets" },
339 { "rx_1523_to_2047_octet_packets" },
340 { "rx_2048_to_4095_octet_packets" },
341 { "rx_4096_to_8191_octet_packets" },
342 { "rx_8192_to_9022_octet_packets" },
343
344 { "tx_octets" },
345 { "tx_collisions" },
346
347 { "tx_xon_sent" },
348 { "tx_xoff_sent" },
349 { "tx_flow_control" },
350 { "tx_mac_errors" },
351 { "tx_single_collisions" },
352 { "tx_mult_collisions" },
353 { "tx_deferred" },
354 { "tx_excessive_collisions" },
355 { "tx_late_collisions" },
356 { "tx_collide_2times" },
357 { "tx_collide_3times" },
358 { "tx_collide_4times" },
359 { "tx_collide_5times" },
360 { "tx_collide_6times" },
361 { "tx_collide_7times" },
362 { "tx_collide_8times" },
363 { "tx_collide_9times" },
364 { "tx_collide_10times" },
365 { "tx_collide_11times" },
366 { "tx_collide_12times" },
367 { "tx_collide_13times" },
368 { "tx_collide_14times" },
369 { "tx_collide_15times" },
370 { "tx_ucast_packets" },
371 { "tx_mcast_packets" },
372 { "tx_bcast_packets" },
373 { "tx_carrier_sense_errors" },
374 { "tx_discards" },
375 { "tx_errors" },
376
377 { "dma_writeq_full" },
378 { "dma_write_prioq_full" },
379 { "rxbds_empty" },
380 { "rx_discards" },
381 { "rx_errors" },
382 { "rx_threshold_hit" },
383
384 { "dma_readq_full" },
385 { "dma_read_prioq_full" },
386 { "tx_comp_queue_full" },
387
388 { "ring_set_send_prod_index" },
389 { "ring_status_update" },
390 { "nic_irqs" },
391 { "nic_avoided_irqs" },
Matt Carlson4452d092011-05-19 12:12:51 +0000392 { "nic_tx_threshold_hit" },
393
394 { "mbuf_lwm_thresh_hit" },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395};
396
Matt Carlson48fa55a2011-04-13 11:05:06 +0000397#define TG3_NUM_STATS ARRAY_SIZE(ethtool_stats_keys)
398
399
Andreas Mohr50da8592006-08-14 23:54:30 -0700400static const struct {
Michael Chan4cafd3f2005-05-29 14:56:34 -0700401 const char string[ETH_GSTRING_LEN];
Matt Carlson48fa55a2011-04-13 11:05:06 +0000402} ethtool_test_keys[] = {
Matt Carlson28a45952011-08-19 13:58:22 +0000403 { "nvram test (online) " },
404 { "link test (online) " },
405 { "register test (offline)" },
406 { "memory test (offline)" },
407 { "mac loopback test (offline)" },
408 { "phy loopback test (offline)" },
Matt Carlson941ec902011-08-19 13:58:23 +0000409 { "ext loopback test (offline)" },
Matt Carlson28a45952011-08-19 13:58:22 +0000410 { "interrupt test (offline)" },
Michael Chan4cafd3f2005-05-29 14:56:34 -0700411};
412
Matt Carlson48fa55a2011-04-13 11:05:06 +0000413#define TG3_NUM_TEST ARRAY_SIZE(ethtool_test_keys)
414
415
Michael Chanb401e9e2005-12-19 16:27:04 -0800416static void tg3_write32(struct tg3 *tp, u32 off, u32 val)
417{
418 writel(val, tp->regs + off);
419}
420
421static u32 tg3_read32(struct tg3 *tp, u32 off)
422{
Matt Carlsonde6f31e2010-04-12 06:58:30 +0000423 return readl(tp->regs + off);
Michael Chanb401e9e2005-12-19 16:27:04 -0800424}
425
Matt Carlson0d3031d2007-10-10 18:02:43 -0700426static void tg3_ape_write32(struct tg3 *tp, u32 off, u32 val)
427{
428 writel(val, tp->aperegs + off);
429}
430
431static u32 tg3_ape_read32(struct tg3 *tp, u32 off)
432{
Matt Carlsonde6f31e2010-04-12 06:58:30 +0000433 return readl(tp->aperegs + off);
Matt Carlson0d3031d2007-10-10 18:02:43 -0700434}
435
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436static void tg3_write_indirect_reg32(struct tg3 *tp, u32 off, u32 val)
437{
Michael Chan68929142005-08-09 20:17:14 -0700438 unsigned long flags;
439
440 spin_lock_irqsave(&tp->indirect_lock, flags);
Michael Chan1ee582d2005-08-09 20:16:46 -0700441 pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off);
442 pci_write_config_dword(tp->pdev, TG3PCI_REG_DATA, val);
Michael Chan68929142005-08-09 20:17:14 -0700443 spin_unlock_irqrestore(&tp->indirect_lock, flags);
Michael Chan1ee582d2005-08-09 20:16:46 -0700444}
445
446static void tg3_write_flush_reg32(struct tg3 *tp, u32 off, u32 val)
447{
448 writel(val, tp->regs + off);
449 readl(tp->regs + off);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450}
451
Michael Chan68929142005-08-09 20:17:14 -0700452static u32 tg3_read_indirect_reg32(struct tg3 *tp, u32 off)
453{
454 unsigned long flags;
455 u32 val;
456
457 spin_lock_irqsave(&tp->indirect_lock, flags);
458 pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off);
459 pci_read_config_dword(tp->pdev, TG3PCI_REG_DATA, &val);
460 spin_unlock_irqrestore(&tp->indirect_lock, flags);
461 return val;
462}
463
464static void tg3_write_indirect_mbox(struct tg3 *tp, u32 off, u32 val)
465{
466 unsigned long flags;
467
468 if (off == (MAILBOX_RCVRET_CON_IDX_0 + TG3_64BIT_REG_LOW)) {
469 pci_write_config_dword(tp->pdev, TG3PCI_RCV_RET_RING_CON_IDX +
470 TG3_64BIT_REG_LOW, val);
471 return;
472 }
Matt Carlson66711e62009-11-13 13:03:49 +0000473 if (off == TG3_RX_STD_PROD_IDX_REG) {
Michael Chan68929142005-08-09 20:17:14 -0700474 pci_write_config_dword(tp->pdev, TG3PCI_STD_RING_PROD_IDX +
475 TG3_64BIT_REG_LOW, val);
476 return;
477 }
478
479 spin_lock_irqsave(&tp->indirect_lock, flags);
480 pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off + 0x5600);
481 pci_write_config_dword(tp->pdev, TG3PCI_REG_DATA, val);
482 spin_unlock_irqrestore(&tp->indirect_lock, flags);
483
484 /* In indirect mode when disabling interrupts, we also need
485 * to clear the interrupt bit in the GRC local ctrl register.
486 */
487 if ((off == (MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW)) &&
488 (val == 0x1)) {
489 pci_write_config_dword(tp->pdev, TG3PCI_MISC_LOCAL_CTRL,
490 tp->grc_local_ctrl|GRC_LCLCTRL_CLEARINT);
491 }
492}
493
494static u32 tg3_read_indirect_mbox(struct tg3 *tp, u32 off)
495{
496 unsigned long flags;
497 u32 val;
498
499 spin_lock_irqsave(&tp->indirect_lock, flags);
500 pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off + 0x5600);
501 pci_read_config_dword(tp->pdev, TG3PCI_REG_DATA, &val);
502 spin_unlock_irqrestore(&tp->indirect_lock, flags);
503 return val;
504}
505
Michael Chanb401e9e2005-12-19 16:27:04 -0800506/* usec_wait specifies the wait time in usec when writing to certain registers
507 * where it is unsafe to read back the register without some delay.
508 * GRC_LOCAL_CTRL is one example if the GPIOs are toggled to switch power.
509 * TG3PCI_CLOCK_CTRL is another example if the clock frequencies are changed.
510 */
511static void _tw32_flush(struct tg3 *tp, u32 off, u32 val, u32 usec_wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512{
Joe Perches63c3a662011-04-26 08:12:10 +0000513 if (tg3_flag(tp, PCIX_TARGET_HWBUG) || tg3_flag(tp, ICH_WORKAROUND))
Michael Chanb401e9e2005-12-19 16:27:04 -0800514 /* Non-posted methods */
515 tp->write32(tp, off, val);
516 else {
517 /* Posted method */
518 tg3_write32(tp, off, val);
519 if (usec_wait)
520 udelay(usec_wait);
521 tp->read32(tp, off);
522 }
523 /* Wait again after the read for the posted method to guarantee that
524 * the wait time is met.
525 */
526 if (usec_wait)
527 udelay(usec_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528}
529
Michael Chan09ee9292005-08-09 20:17:00 -0700530static inline void tw32_mailbox_flush(struct tg3 *tp, u32 off, u32 val)
531{
532 tp->write32_mbox(tp, off, val);
Joe Perches63c3a662011-04-26 08:12:10 +0000533 if (!tg3_flag(tp, MBOX_WRITE_REORDER) && !tg3_flag(tp, ICH_WORKAROUND))
Michael Chan68929142005-08-09 20:17:14 -0700534 tp->read32_mbox(tp, off);
Michael Chan09ee9292005-08-09 20:17:00 -0700535}
536
Michael Chan20094932005-08-09 20:16:32 -0700537static void tg3_write32_tx_mbox(struct tg3 *tp, u32 off, u32 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538{
539 void __iomem *mbox = tp->regs + off;
540 writel(val, mbox);
Joe Perches63c3a662011-04-26 08:12:10 +0000541 if (tg3_flag(tp, TXD_MBOX_HWBUG))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 writel(val, mbox);
Joe Perches63c3a662011-04-26 08:12:10 +0000543 if (tg3_flag(tp, MBOX_WRITE_REORDER))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 readl(mbox);
545}
546
Michael Chanb5d37722006-09-27 16:06:21 -0700547static u32 tg3_read32_mbox_5906(struct tg3 *tp, u32 off)
548{
Matt Carlsonde6f31e2010-04-12 06:58:30 +0000549 return readl(tp->regs + off + GRCMBOX_BASE);
Michael Chanb5d37722006-09-27 16:06:21 -0700550}
551
552static void tg3_write32_mbox_5906(struct tg3 *tp, u32 off, u32 val)
553{
554 writel(val, tp->regs + off + GRCMBOX_BASE);
555}
556
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000557#define tw32_mailbox(reg, val) tp->write32_mbox(tp, reg, val)
Michael Chan09ee9292005-08-09 20:17:00 -0700558#define tw32_mailbox_f(reg, val) tw32_mailbox_flush(tp, (reg), (val))
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000559#define tw32_rx_mbox(reg, val) tp->write32_rx_mbox(tp, reg, val)
560#define tw32_tx_mbox(reg, val) tp->write32_tx_mbox(tp, reg, val)
561#define tr32_mailbox(reg) tp->read32_mbox(tp, reg)
Michael Chan20094932005-08-09 20:16:32 -0700562
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000563#define tw32(reg, val) tp->write32(tp, reg, val)
564#define tw32_f(reg, val) _tw32_flush(tp, (reg), (val), 0)
565#define tw32_wait_f(reg, val, us) _tw32_flush(tp, (reg), (val), (us))
566#define tr32(reg) tp->read32(tp, reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567
568static void tg3_write_mem(struct tg3 *tp, u32 off, u32 val)
569{
Michael Chan68929142005-08-09 20:17:14 -0700570 unsigned long flags;
571
Matt Carlson6ff6f812011-05-19 12:12:54 +0000572 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906 &&
Michael Chanb5d37722006-09-27 16:06:21 -0700573 (off >= NIC_SRAM_STATS_BLK) && (off < NIC_SRAM_TX_BUFFER_DESC))
574 return;
575
Michael Chan68929142005-08-09 20:17:14 -0700576 spin_lock_irqsave(&tp->indirect_lock, flags);
Joe Perches63c3a662011-04-26 08:12:10 +0000577 if (tg3_flag(tp, SRAM_USE_CONFIG)) {
Michael Chanbbadf502006-04-06 21:46:34 -0700578 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, off);
579 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_DATA, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580
Michael Chanbbadf502006-04-06 21:46:34 -0700581 /* Always leave this as zero. */
582 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, 0);
583 } else {
584 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, off);
585 tw32_f(TG3PCI_MEM_WIN_DATA, val);
586
587 /* Always leave this as zero. */
588 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, 0);
589 }
Michael Chan68929142005-08-09 20:17:14 -0700590 spin_unlock_irqrestore(&tp->indirect_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591}
592
593static void tg3_read_mem(struct tg3 *tp, u32 off, u32 *val)
594{
Michael Chan68929142005-08-09 20:17:14 -0700595 unsigned long flags;
596
Matt Carlson6ff6f812011-05-19 12:12:54 +0000597 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906 &&
Michael Chanb5d37722006-09-27 16:06:21 -0700598 (off >= NIC_SRAM_STATS_BLK) && (off < NIC_SRAM_TX_BUFFER_DESC)) {
599 *val = 0;
600 return;
601 }
602
Michael Chan68929142005-08-09 20:17:14 -0700603 spin_lock_irqsave(&tp->indirect_lock, flags);
Joe Perches63c3a662011-04-26 08:12:10 +0000604 if (tg3_flag(tp, SRAM_USE_CONFIG)) {
Michael Chanbbadf502006-04-06 21:46:34 -0700605 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, off);
606 pci_read_config_dword(tp->pdev, TG3PCI_MEM_WIN_DATA, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607
Michael Chanbbadf502006-04-06 21:46:34 -0700608 /* Always leave this as zero. */
609 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, 0);
610 } else {
611 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, off);
612 *val = tr32(TG3PCI_MEM_WIN_DATA);
613
614 /* Always leave this as zero. */
615 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, 0);
616 }
Michael Chan68929142005-08-09 20:17:14 -0700617 spin_unlock_irqrestore(&tp->indirect_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618}
619
Matt Carlson0d3031d2007-10-10 18:02:43 -0700620static void tg3_ape_lock_init(struct tg3 *tp)
621{
622 int i;
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000623 u32 regbase, bit;
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000624
625 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
626 regbase = TG3_APE_LOCK_GRANT;
627 else
628 regbase = TG3_APE_PER_LOCK_GRANT;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700629
630 /* Make sure the driver hasn't any stale locks. */
Matt Carlson78f94dc2011-11-04 09:14:58 +0000631 for (i = TG3_APE_LOCK_PHY0; i <= TG3_APE_LOCK_GPIO; i++) {
632 switch (i) {
633 case TG3_APE_LOCK_PHY0:
634 case TG3_APE_LOCK_PHY1:
635 case TG3_APE_LOCK_PHY2:
636 case TG3_APE_LOCK_PHY3:
637 bit = APE_LOCK_GRANT_DRIVER;
638 break;
639 default:
640 if (!tp->pci_fn)
641 bit = APE_LOCK_GRANT_DRIVER;
642 else
643 bit = 1 << tp->pci_fn;
644 }
645 tg3_ape_write32(tp, regbase + 4 * i, bit);
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000646 }
647
Matt Carlson0d3031d2007-10-10 18:02:43 -0700648}
649
650static int tg3_ape_lock(struct tg3 *tp, int locknum)
651{
652 int i, off;
653 int ret = 0;
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000654 u32 status, req, gnt, bit;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700655
Joe Perches63c3a662011-04-26 08:12:10 +0000656 if (!tg3_flag(tp, ENABLE_APE))
Matt Carlson0d3031d2007-10-10 18:02:43 -0700657 return 0;
658
659 switch (locknum) {
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000660 case TG3_APE_LOCK_GPIO:
661 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
662 return 0;
Matt Carlson33f401a2010-04-05 10:19:27 +0000663 case TG3_APE_LOCK_GRC:
664 case TG3_APE_LOCK_MEM:
Matt Carlson78f94dc2011-11-04 09:14:58 +0000665 if (!tp->pci_fn)
666 bit = APE_LOCK_REQ_DRIVER;
667 else
668 bit = 1 << tp->pci_fn;
Matt Carlson33f401a2010-04-05 10:19:27 +0000669 break;
670 default:
671 return -EINVAL;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700672 }
673
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000674 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761) {
675 req = TG3_APE_LOCK_REQ;
676 gnt = TG3_APE_LOCK_GRANT;
677 } else {
678 req = TG3_APE_PER_LOCK_REQ;
679 gnt = TG3_APE_PER_LOCK_GRANT;
680 }
681
Matt Carlson0d3031d2007-10-10 18:02:43 -0700682 off = 4 * locknum;
683
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000684 tg3_ape_write32(tp, req + off, bit);
Matt Carlson0d3031d2007-10-10 18:02:43 -0700685
686 /* Wait for up to 1 millisecond to acquire lock. */
687 for (i = 0; i < 100; i++) {
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000688 status = tg3_ape_read32(tp, gnt + off);
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000689 if (status == bit)
Matt Carlson0d3031d2007-10-10 18:02:43 -0700690 break;
691 udelay(10);
692 }
693
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000694 if (status != bit) {
Matt Carlson0d3031d2007-10-10 18:02:43 -0700695 /* Revoke the lock request. */
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000696 tg3_ape_write32(tp, gnt + off, bit);
Matt Carlson0d3031d2007-10-10 18:02:43 -0700697 ret = -EBUSY;
698 }
699
700 return ret;
701}
702
703static void tg3_ape_unlock(struct tg3 *tp, int locknum)
704{
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000705 u32 gnt, bit;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700706
Joe Perches63c3a662011-04-26 08:12:10 +0000707 if (!tg3_flag(tp, ENABLE_APE))
Matt Carlson0d3031d2007-10-10 18:02:43 -0700708 return;
709
710 switch (locknum) {
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000711 case TG3_APE_LOCK_GPIO:
712 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
713 return;
Matt Carlson33f401a2010-04-05 10:19:27 +0000714 case TG3_APE_LOCK_GRC:
715 case TG3_APE_LOCK_MEM:
Matt Carlson78f94dc2011-11-04 09:14:58 +0000716 if (!tp->pci_fn)
717 bit = APE_LOCK_GRANT_DRIVER;
718 else
719 bit = 1 << tp->pci_fn;
Matt Carlson33f401a2010-04-05 10:19:27 +0000720 break;
721 default:
722 return;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700723 }
724
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000725 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
726 gnt = TG3_APE_LOCK_GRANT;
727 else
728 gnt = TG3_APE_PER_LOCK_GRANT;
729
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000730 tg3_ape_write32(tp, gnt + 4 * locknum, bit);
Matt Carlson0d3031d2007-10-10 18:02:43 -0700731}
732
Matt Carlsonfd6d3f02011-08-31 11:44:52 +0000733static void tg3_ape_send_event(struct tg3 *tp, u32 event)
734{
735 int i;
736 u32 apedata;
737
738 /* NCSI does not support APE events */
739 if (tg3_flag(tp, APE_HAS_NCSI))
740 return;
741
742 apedata = tg3_ape_read32(tp, TG3_APE_SEG_SIG);
743 if (apedata != APE_SEG_SIG_MAGIC)
744 return;
745
746 apedata = tg3_ape_read32(tp, TG3_APE_FW_STATUS);
747 if (!(apedata & APE_FW_STATUS_READY))
748 return;
749
750 /* Wait for up to 1 millisecond for APE to service previous event. */
751 for (i = 0; i < 10; i++) {
752 if (tg3_ape_lock(tp, TG3_APE_LOCK_MEM))
753 return;
754
755 apedata = tg3_ape_read32(tp, TG3_APE_EVENT_STATUS);
756
757 if (!(apedata & APE_EVENT_STATUS_EVENT_PENDING))
758 tg3_ape_write32(tp, TG3_APE_EVENT_STATUS,
759 event | APE_EVENT_STATUS_EVENT_PENDING);
760
761 tg3_ape_unlock(tp, TG3_APE_LOCK_MEM);
762
763 if (!(apedata & APE_EVENT_STATUS_EVENT_PENDING))
764 break;
765
766 udelay(100);
767 }
768
769 if (!(apedata & APE_EVENT_STATUS_EVENT_PENDING))
770 tg3_ape_write32(tp, TG3_APE_EVENT, APE_EVENT_1);
771}
772
773static void tg3_ape_driver_state_change(struct tg3 *tp, int kind)
774{
775 u32 event;
776 u32 apedata;
777
778 if (!tg3_flag(tp, ENABLE_APE))
779 return;
780
781 switch (kind) {
782 case RESET_KIND_INIT:
783 tg3_ape_write32(tp, TG3_APE_HOST_SEG_SIG,
784 APE_HOST_SEG_SIG_MAGIC);
785 tg3_ape_write32(tp, TG3_APE_HOST_SEG_LEN,
786 APE_HOST_SEG_LEN_MAGIC);
787 apedata = tg3_ape_read32(tp, TG3_APE_HOST_INIT_COUNT);
788 tg3_ape_write32(tp, TG3_APE_HOST_INIT_COUNT, ++apedata);
789 tg3_ape_write32(tp, TG3_APE_HOST_DRIVER_ID,
790 APE_HOST_DRIVER_ID_MAGIC(TG3_MAJ_NUM, TG3_MIN_NUM));
791 tg3_ape_write32(tp, TG3_APE_HOST_BEHAVIOR,
792 APE_HOST_BEHAV_NO_PHYLOCK);
793 tg3_ape_write32(tp, TG3_APE_HOST_DRVR_STATE,
794 TG3_APE_HOST_DRVR_STATE_START);
795
796 event = APE_EVENT_STATUS_STATE_START;
797 break;
798 case RESET_KIND_SHUTDOWN:
799 /* With the interface we are currently using,
800 * APE does not track driver state. Wiping
801 * out the HOST SEGMENT SIGNATURE forces
802 * the APE to assume OS absent status.
803 */
804 tg3_ape_write32(tp, TG3_APE_HOST_SEG_SIG, 0x0);
805
806 if (device_may_wakeup(&tp->pdev->dev) &&
807 tg3_flag(tp, WOL_ENABLE)) {
808 tg3_ape_write32(tp, TG3_APE_HOST_WOL_SPEED,
809 TG3_APE_HOST_WOL_SPEED_AUTO);
810 apedata = TG3_APE_HOST_DRVR_STATE_WOL;
811 } else
812 apedata = TG3_APE_HOST_DRVR_STATE_UNLOAD;
813
814 tg3_ape_write32(tp, TG3_APE_HOST_DRVR_STATE, apedata);
815
816 event = APE_EVENT_STATUS_STATE_UNLOAD;
817 break;
818 case RESET_KIND_SUSPEND:
819 event = APE_EVENT_STATUS_STATE_SUSPEND;
820 break;
821 default:
822 return;
823 }
824
825 event |= APE_EVENT_STATUS_DRIVER_EVNT | APE_EVENT_STATUS_STATE_CHNGE;
826
827 tg3_ape_send_event(tp, event);
828}
829
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830static void tg3_disable_ints(struct tg3 *tp)
831{
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000832 int i;
833
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 tw32(TG3PCI_MISC_HOST_CTRL,
835 (tp->misc_host_ctrl | MISC_HOST_CTRL_MASK_PCI_INT));
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000836 for (i = 0; i < tp->irq_max; i++)
837 tw32_mailbox_f(tp->napi[i].int_mbox, 0x00000001);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838}
839
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840static void tg3_enable_ints(struct tg3 *tp)
841{
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000842 int i;
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000843
Michael Chanbbe832c2005-06-24 20:20:04 -0700844 tp->irq_sync = 0;
845 wmb();
846
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 tw32(TG3PCI_MISC_HOST_CTRL,
848 (tp->misc_host_ctrl & ~MISC_HOST_CTRL_MASK_PCI_INT));
Matt Carlsonf19af9c2009-09-01 12:47:49 +0000849
Matt Carlsonf89f38b2010-02-12 14:47:07 +0000850 tp->coal_now = tp->coalesce_mode | HOSTCC_MODE_ENABLE;
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000851 for (i = 0; i < tp->irq_cnt; i++) {
852 struct tg3_napi *tnapi = &tp->napi[i];
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000853
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000854 tw32_mailbox_f(tnapi->int_mbox, tnapi->last_tag << 24);
Joe Perches63c3a662011-04-26 08:12:10 +0000855 if (tg3_flag(tp, 1SHOT_MSI))
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000856 tw32_mailbox_f(tnapi->int_mbox, tnapi->last_tag << 24);
857
Matt Carlsonf89f38b2010-02-12 14:47:07 +0000858 tp->coal_now |= tnapi->coal_now;
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000859 }
Matt Carlsonf19af9c2009-09-01 12:47:49 +0000860
861 /* Force an initial interrupt */
Joe Perches63c3a662011-04-26 08:12:10 +0000862 if (!tg3_flag(tp, TAGGED_STATUS) &&
Matt Carlsonf19af9c2009-09-01 12:47:49 +0000863 (tp->napi[0].hw_status->status & SD_STATUS_UPDATED))
864 tw32(GRC_LOCAL_CTRL, tp->grc_local_ctrl | GRC_LCLCTRL_SETINT);
865 else
Matt Carlsonf89f38b2010-02-12 14:47:07 +0000866 tw32(HOSTCC_MODE, tp->coal_now);
867
868 tp->coal_now &= ~(tp->napi[0].coal_now | tp->napi[1].coal_now);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869}
870
Matt Carlson17375d22009-08-28 14:02:18 +0000871static inline unsigned int tg3_has_work(struct tg3_napi *tnapi)
Michael Chan04237dd2005-04-25 15:17:17 -0700872{
Matt Carlson17375d22009-08-28 14:02:18 +0000873 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +0000874 struct tg3_hw_status *sblk = tnapi->hw_status;
Michael Chan04237dd2005-04-25 15:17:17 -0700875 unsigned int work_exists = 0;
876
877 /* check for phy events */
Joe Perches63c3a662011-04-26 08:12:10 +0000878 if (!(tg3_flag(tp, USE_LINKCHG_REG) || tg3_flag(tp, POLL_SERDES))) {
Michael Chan04237dd2005-04-25 15:17:17 -0700879 if (sblk->status & SD_STATUS_LINK_CHG)
880 work_exists = 1;
881 }
Matt Carlsonf891ea12012-04-24 13:37:01 +0000882
883 /* check for TX work to do */
884 if (sblk->idx[0].tx_consumer != tnapi->tx_cons)
885 work_exists = 1;
886
887 /* check for RX work to do */
888 if (tnapi->rx_rcb_prod_idx &&
Matt Carlson8d9d7cf2009-09-01 13:19:05 +0000889 *(tnapi->rx_rcb_prod_idx) != tnapi->rx_rcb_ptr)
Michael Chan04237dd2005-04-25 15:17:17 -0700890 work_exists = 1;
891
892 return work_exists;
893}
894
Matt Carlson17375d22009-08-28 14:02:18 +0000895/* tg3_int_reenable
Michael Chan04237dd2005-04-25 15:17:17 -0700896 * similar to tg3_enable_ints, but it accurately determines whether there
897 * is new work pending and can return without flushing the PIO write
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400898 * which reenables interrupts
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 */
Matt Carlson17375d22009-08-28 14:02:18 +0000900static void tg3_int_reenable(struct tg3_napi *tnapi)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901{
Matt Carlson17375d22009-08-28 14:02:18 +0000902 struct tg3 *tp = tnapi->tp;
903
Matt Carlson898a56f2009-08-28 14:02:40 +0000904 tw32_mailbox(tnapi->int_mbox, tnapi->last_tag << 24);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 mmiowb();
906
David S. Millerfac9b832005-05-18 22:46:34 -0700907 /* When doing tagged status, this work check is unnecessary.
908 * The last_tag we write above tells the chip which piece of
909 * work we've completed.
910 */
Joe Perches63c3a662011-04-26 08:12:10 +0000911 if (!tg3_flag(tp, TAGGED_STATUS) && tg3_has_work(tnapi))
Michael Chan04237dd2005-04-25 15:17:17 -0700912 tw32(HOSTCC_MODE, tp->coalesce_mode |
Matt Carlsonfd2ce372009-09-01 12:51:13 +0000913 HOSTCC_MODE_ENABLE | tnapi->coal_now);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914}
915
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916static void tg3_switch_clocks(struct tg3 *tp)
917{
Matt Carlsonf6eb9b12009-09-01 13:19:53 +0000918 u32 clock_ctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 u32 orig_clock_ctrl;
920
Joe Perches63c3a662011-04-26 08:12:10 +0000921 if (tg3_flag(tp, CPMU_PRESENT) || tg3_flag(tp, 5780_CLASS))
Michael Chan4cf78e42005-07-25 12:29:19 -0700922 return;
923
Matt Carlsonf6eb9b12009-09-01 13:19:53 +0000924 clock_ctrl = tr32(TG3PCI_CLOCK_CTRL);
925
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 orig_clock_ctrl = clock_ctrl;
927 clock_ctrl &= (CLOCK_CTRL_FORCE_CLKRUN |
928 CLOCK_CTRL_CLKRUN_OENABLE |
929 0x1f);
930 tp->pci_clock_ctrl = clock_ctrl;
931
Joe Perches63c3a662011-04-26 08:12:10 +0000932 if (tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 if (orig_clock_ctrl & CLOCK_CTRL_625_CORE) {
Michael Chanb401e9e2005-12-19 16:27:04 -0800934 tw32_wait_f(TG3PCI_CLOCK_CTRL,
935 clock_ctrl | CLOCK_CTRL_625_CORE, 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 }
937 } else if ((orig_clock_ctrl & CLOCK_CTRL_44MHZ_CORE) != 0) {
Michael Chanb401e9e2005-12-19 16:27:04 -0800938 tw32_wait_f(TG3PCI_CLOCK_CTRL,
939 clock_ctrl |
940 (CLOCK_CTRL_44MHZ_CORE | CLOCK_CTRL_ALTCLK),
941 40);
942 tw32_wait_f(TG3PCI_CLOCK_CTRL,
943 clock_ctrl | (CLOCK_CTRL_ALTCLK),
944 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 }
Michael Chanb401e9e2005-12-19 16:27:04 -0800946 tw32_wait_f(TG3PCI_CLOCK_CTRL, clock_ctrl, 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947}
948
949#define PHY_BUSY_LOOPS 5000
950
951static int tg3_readphy(struct tg3 *tp, int reg, u32 *val)
952{
953 u32 frame_val;
954 unsigned int loops;
955 int ret;
956
957 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
958 tw32_f(MAC_MI_MODE,
959 (tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL));
960 udelay(80);
961 }
962
963 *val = 0x0;
964
Matt Carlson882e9792009-09-01 13:21:36 +0000965 frame_val = ((tp->phy_addr << MI_COM_PHY_ADDR_SHIFT) &
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 MI_COM_PHY_ADDR_MASK);
967 frame_val |= ((reg << MI_COM_REG_ADDR_SHIFT) &
968 MI_COM_REG_ADDR_MASK);
969 frame_val |= (MI_COM_CMD_READ | MI_COM_START);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400970
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 tw32_f(MAC_MI_COM, frame_val);
972
973 loops = PHY_BUSY_LOOPS;
974 while (loops != 0) {
975 udelay(10);
976 frame_val = tr32(MAC_MI_COM);
977
978 if ((frame_val & MI_COM_BUSY) == 0) {
979 udelay(5);
980 frame_val = tr32(MAC_MI_COM);
981 break;
982 }
983 loops -= 1;
984 }
985
986 ret = -EBUSY;
987 if (loops != 0) {
988 *val = frame_val & MI_COM_DATA_MASK;
989 ret = 0;
990 }
991
992 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
993 tw32_f(MAC_MI_MODE, tp->mi_mode);
994 udelay(80);
995 }
996
997 return ret;
998}
999
1000static int tg3_writephy(struct tg3 *tp, int reg, u32 val)
1001{
1002 u32 frame_val;
1003 unsigned int loops;
1004 int ret;
1005
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001006 if ((tp->phy_flags & TG3_PHYFLG_IS_FET) &&
Matt Carlson221c5632011-06-13 13:39:01 +00001007 (reg == MII_CTRL1000 || reg == MII_TG3_AUX_CTRL))
Michael Chanb5d37722006-09-27 16:06:21 -07001008 return 0;
1009
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
1011 tw32_f(MAC_MI_MODE,
1012 (tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL));
1013 udelay(80);
1014 }
1015
Matt Carlson882e9792009-09-01 13:21:36 +00001016 frame_val = ((tp->phy_addr << MI_COM_PHY_ADDR_SHIFT) &
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 MI_COM_PHY_ADDR_MASK);
1018 frame_val |= ((reg << MI_COM_REG_ADDR_SHIFT) &
1019 MI_COM_REG_ADDR_MASK);
1020 frame_val |= (val & MI_COM_DATA_MASK);
1021 frame_val |= (MI_COM_CMD_WRITE | MI_COM_START);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001022
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 tw32_f(MAC_MI_COM, frame_val);
1024
1025 loops = PHY_BUSY_LOOPS;
1026 while (loops != 0) {
1027 udelay(10);
1028 frame_val = tr32(MAC_MI_COM);
1029 if ((frame_val & MI_COM_BUSY) == 0) {
1030 udelay(5);
1031 frame_val = tr32(MAC_MI_COM);
1032 break;
1033 }
1034 loops -= 1;
1035 }
1036
1037 ret = -EBUSY;
1038 if (loops != 0)
1039 ret = 0;
1040
1041 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
1042 tw32_f(MAC_MI_MODE, tp->mi_mode);
1043 udelay(80);
1044 }
1045
1046 return ret;
1047}
1048
Matt Carlsonb0988c12011-04-20 07:57:39 +00001049static int tg3_phy_cl45_write(struct tg3 *tp, u32 devad, u32 addr, u32 val)
1050{
1051 int err;
1052
1053 err = tg3_writephy(tp, MII_TG3_MMD_CTRL, devad);
1054 if (err)
1055 goto done;
1056
1057 err = tg3_writephy(tp, MII_TG3_MMD_ADDRESS, addr);
1058 if (err)
1059 goto done;
1060
1061 err = tg3_writephy(tp, MII_TG3_MMD_CTRL,
1062 MII_TG3_MMD_CTRL_DATA_NOINC | devad);
1063 if (err)
1064 goto done;
1065
1066 err = tg3_writephy(tp, MII_TG3_MMD_ADDRESS, val);
1067
1068done:
1069 return err;
1070}
1071
1072static int tg3_phy_cl45_read(struct tg3 *tp, u32 devad, u32 addr, u32 *val)
1073{
1074 int err;
1075
1076 err = tg3_writephy(tp, MII_TG3_MMD_CTRL, devad);
1077 if (err)
1078 goto done;
1079
1080 err = tg3_writephy(tp, MII_TG3_MMD_ADDRESS, addr);
1081 if (err)
1082 goto done;
1083
1084 err = tg3_writephy(tp, MII_TG3_MMD_CTRL,
1085 MII_TG3_MMD_CTRL_DATA_NOINC | devad);
1086 if (err)
1087 goto done;
1088
1089 err = tg3_readphy(tp, MII_TG3_MMD_ADDRESS, val);
1090
1091done:
1092 return err;
1093}
1094
1095static int tg3_phydsp_read(struct tg3 *tp, u32 reg, u32 *val)
1096{
1097 int err;
1098
1099 err = tg3_writephy(tp, MII_TG3_DSP_ADDRESS, reg);
1100 if (!err)
1101 err = tg3_readphy(tp, MII_TG3_DSP_RW_PORT, val);
1102
1103 return err;
1104}
1105
1106static int tg3_phydsp_write(struct tg3 *tp, u32 reg, u32 val)
1107{
1108 int err;
1109
1110 err = tg3_writephy(tp, MII_TG3_DSP_ADDRESS, reg);
1111 if (!err)
1112 err = tg3_writephy(tp, MII_TG3_DSP_RW_PORT, val);
1113
1114 return err;
1115}
1116
Matt Carlson15ee95c2011-04-20 07:57:40 +00001117static int tg3_phy_auxctl_read(struct tg3 *tp, int reg, u32 *val)
1118{
1119 int err;
1120
1121 err = tg3_writephy(tp, MII_TG3_AUX_CTRL,
1122 (reg << MII_TG3_AUXCTL_MISC_RDSEL_SHIFT) |
1123 MII_TG3_AUXCTL_SHDWSEL_MISC);
1124 if (!err)
1125 err = tg3_readphy(tp, MII_TG3_AUX_CTRL, val);
1126
1127 return err;
1128}
1129
Matt Carlsonb4bd2922011-04-20 07:57:41 +00001130static int tg3_phy_auxctl_write(struct tg3 *tp, int reg, u32 set)
1131{
1132 if (reg == MII_TG3_AUXCTL_SHDWSEL_MISC)
1133 set |= MII_TG3_AUXCTL_MISC_WREN;
1134
1135 return tg3_writephy(tp, MII_TG3_AUX_CTRL, set | reg);
1136}
1137
Matt Carlson1d36ba42011-04-20 07:57:42 +00001138#define TG3_PHY_AUXCTL_SMDSP_ENABLE(tp) \
1139 tg3_phy_auxctl_write((tp), MII_TG3_AUXCTL_SHDWSEL_AUXCTL, \
1140 MII_TG3_AUXCTL_ACTL_SMDSP_ENA | \
1141 MII_TG3_AUXCTL_ACTL_TX_6DB)
1142
1143#define TG3_PHY_AUXCTL_SMDSP_DISABLE(tp) \
1144 tg3_phy_auxctl_write((tp), MII_TG3_AUXCTL_SHDWSEL_AUXCTL, \
1145 MII_TG3_AUXCTL_ACTL_TX_6DB);
1146
Matt Carlson95e28692008-05-25 23:44:14 -07001147static int tg3_bmcr_reset(struct tg3 *tp)
1148{
1149 u32 phy_control;
1150 int limit, err;
1151
1152 /* OK, reset it, and poll the BMCR_RESET bit until it
1153 * clears or we time out.
1154 */
1155 phy_control = BMCR_RESET;
1156 err = tg3_writephy(tp, MII_BMCR, phy_control);
1157 if (err != 0)
1158 return -EBUSY;
1159
1160 limit = 5000;
1161 while (limit--) {
1162 err = tg3_readphy(tp, MII_BMCR, &phy_control);
1163 if (err != 0)
1164 return -EBUSY;
1165
1166 if ((phy_control & BMCR_RESET) == 0) {
1167 udelay(40);
1168 break;
1169 }
1170 udelay(10);
1171 }
Roel Kluind4675b52009-02-12 16:33:27 -08001172 if (limit < 0)
Matt Carlson95e28692008-05-25 23:44:14 -07001173 return -EBUSY;
1174
1175 return 0;
1176}
1177
Matt Carlson158d7ab2008-05-29 01:37:54 -07001178static int tg3_mdio_read(struct mii_bus *bp, int mii_id, int reg)
1179{
Francois Romieu3d165432009-01-19 16:56:50 -08001180 struct tg3 *tp = bp->priv;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001181 u32 val;
1182
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001183 spin_lock_bh(&tp->lock);
Matt Carlson158d7ab2008-05-29 01:37:54 -07001184
1185 if (tg3_readphy(tp, reg, &val))
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001186 val = -EIO;
1187
1188 spin_unlock_bh(&tp->lock);
Matt Carlson158d7ab2008-05-29 01:37:54 -07001189
1190 return val;
1191}
1192
1193static int tg3_mdio_write(struct mii_bus *bp, int mii_id, int reg, u16 val)
1194{
Francois Romieu3d165432009-01-19 16:56:50 -08001195 struct tg3 *tp = bp->priv;
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001196 u32 ret = 0;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001197
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001198 spin_lock_bh(&tp->lock);
Matt Carlson158d7ab2008-05-29 01:37:54 -07001199
1200 if (tg3_writephy(tp, reg, val))
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001201 ret = -EIO;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001202
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001203 spin_unlock_bh(&tp->lock);
1204
1205 return ret;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001206}
1207
1208static int tg3_mdio_reset(struct mii_bus *bp)
1209{
1210 return 0;
1211}
1212
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001213static void tg3_mdio_config_5785(struct tg3 *tp)
Matt Carlsona9daf362008-05-25 23:49:44 -07001214{
1215 u32 val;
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001216 struct phy_device *phydev;
Matt Carlsona9daf362008-05-25 23:49:44 -07001217
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001218 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001219 switch (phydev->drv->phy_id & phydev->drv->phy_id_mask) {
Matt Carlson6a443a02010-02-17 15:17:04 +00001220 case PHY_ID_BCM50610:
1221 case PHY_ID_BCM50610M:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001222 val = MAC_PHYCFG2_50610_LED_MODES;
1223 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001224 case PHY_ID_BCMAC131:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001225 val = MAC_PHYCFG2_AC131_LED_MODES;
1226 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001227 case PHY_ID_RTL8211C:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001228 val = MAC_PHYCFG2_RTL8211C_LED_MODES;
1229 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001230 case PHY_ID_RTL8201E:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001231 val = MAC_PHYCFG2_RTL8201E_LED_MODES;
1232 break;
1233 default:
Matt Carlsona9daf362008-05-25 23:49:44 -07001234 return;
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001235 }
1236
1237 if (phydev->interface != PHY_INTERFACE_MODE_RGMII) {
1238 tw32(MAC_PHYCFG2, val);
1239
1240 val = tr32(MAC_PHYCFG1);
Matt Carlsonbb85fbb2009-08-25 10:09:07 +00001241 val &= ~(MAC_PHYCFG1_RGMII_INT |
1242 MAC_PHYCFG1_RXCLK_TO_MASK | MAC_PHYCFG1_TXCLK_TO_MASK);
1243 val |= MAC_PHYCFG1_RXCLK_TIMEOUT | MAC_PHYCFG1_TXCLK_TIMEOUT;
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001244 tw32(MAC_PHYCFG1, val);
1245
1246 return;
1247 }
1248
Joe Perches63c3a662011-04-26 08:12:10 +00001249 if (!tg3_flag(tp, RGMII_INBAND_DISABLE))
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001250 val |= MAC_PHYCFG2_EMODE_MASK_MASK |
1251 MAC_PHYCFG2_FMODE_MASK_MASK |
1252 MAC_PHYCFG2_GMODE_MASK_MASK |
1253 MAC_PHYCFG2_ACT_MASK_MASK |
1254 MAC_PHYCFG2_QUAL_MASK_MASK |
1255 MAC_PHYCFG2_INBAND_ENABLE;
1256
1257 tw32(MAC_PHYCFG2, val);
Matt Carlsona9daf362008-05-25 23:49:44 -07001258
Matt Carlsonbb85fbb2009-08-25 10:09:07 +00001259 val = tr32(MAC_PHYCFG1);
1260 val &= ~(MAC_PHYCFG1_RXCLK_TO_MASK | MAC_PHYCFG1_TXCLK_TO_MASK |
1261 MAC_PHYCFG1_RGMII_EXT_RX_DEC | MAC_PHYCFG1_RGMII_SND_STAT_EN);
Joe Perches63c3a662011-04-26 08:12:10 +00001262 if (!tg3_flag(tp, RGMII_INBAND_DISABLE)) {
1263 if (tg3_flag(tp, RGMII_EXT_IBND_RX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001264 val |= MAC_PHYCFG1_RGMII_EXT_RX_DEC;
Joe Perches63c3a662011-04-26 08:12:10 +00001265 if (tg3_flag(tp, RGMII_EXT_IBND_TX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001266 val |= MAC_PHYCFG1_RGMII_SND_STAT_EN;
1267 }
Matt Carlsonbb85fbb2009-08-25 10:09:07 +00001268 val |= MAC_PHYCFG1_RXCLK_TIMEOUT | MAC_PHYCFG1_TXCLK_TIMEOUT |
1269 MAC_PHYCFG1_RGMII_INT | MAC_PHYCFG1_TXC_DRV;
1270 tw32(MAC_PHYCFG1, val);
Matt Carlsona9daf362008-05-25 23:49:44 -07001271
Matt Carlsona9daf362008-05-25 23:49:44 -07001272 val = tr32(MAC_EXT_RGMII_MODE);
1273 val &= ~(MAC_RGMII_MODE_RX_INT_B |
1274 MAC_RGMII_MODE_RX_QUALITY |
1275 MAC_RGMII_MODE_RX_ACTIVITY |
1276 MAC_RGMII_MODE_RX_ENG_DET |
1277 MAC_RGMII_MODE_TX_ENABLE |
1278 MAC_RGMII_MODE_TX_LOWPWR |
1279 MAC_RGMII_MODE_TX_RESET);
Joe Perches63c3a662011-04-26 08:12:10 +00001280 if (!tg3_flag(tp, RGMII_INBAND_DISABLE)) {
1281 if (tg3_flag(tp, RGMII_EXT_IBND_RX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001282 val |= MAC_RGMII_MODE_RX_INT_B |
1283 MAC_RGMII_MODE_RX_QUALITY |
1284 MAC_RGMII_MODE_RX_ACTIVITY |
1285 MAC_RGMII_MODE_RX_ENG_DET;
Joe Perches63c3a662011-04-26 08:12:10 +00001286 if (tg3_flag(tp, RGMII_EXT_IBND_TX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001287 val |= MAC_RGMII_MODE_TX_ENABLE |
1288 MAC_RGMII_MODE_TX_LOWPWR |
1289 MAC_RGMII_MODE_TX_RESET;
1290 }
1291 tw32(MAC_EXT_RGMII_MODE, val);
1292}
1293
Matt Carlson158d7ab2008-05-29 01:37:54 -07001294static void tg3_mdio_start(struct tg3 *tp)
1295{
Matt Carlson158d7ab2008-05-29 01:37:54 -07001296 tp->mi_mode &= ~MAC_MI_MODE_AUTO_POLL;
1297 tw32_f(MAC_MI_MODE, tp->mi_mode);
1298 udelay(80);
Matt Carlsona9daf362008-05-25 23:49:44 -07001299
Joe Perches63c3a662011-04-26 08:12:10 +00001300 if (tg3_flag(tp, MDIOBUS_INITED) &&
Matt Carlson9ea48182010-02-17 15:17:01 +00001301 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
1302 tg3_mdio_config_5785(tp);
1303}
1304
1305static int tg3_mdio_init(struct tg3 *tp)
1306{
1307 int i;
1308 u32 reg;
1309 struct phy_device *phydev;
1310
Joe Perches63c3a662011-04-26 08:12:10 +00001311 if (tg3_flag(tp, 5717_PLUS)) {
Matt Carlson9c7df912010-06-05 17:24:36 +00001312 u32 is_serdes;
Matt Carlson882e9792009-09-01 13:21:36 +00001313
Matt Carlson69f11c92011-07-13 09:27:30 +00001314 tp->phy_addr = tp->pci_fn + 1;
Matt Carlson882e9792009-09-01 13:21:36 +00001315
Matt Carlsond1ec96a2010-01-12 10:11:38 +00001316 if (tp->pci_chip_rev_id != CHIPREV_ID_5717_A0)
1317 is_serdes = tr32(SG_DIG_STATUS) & SG_DIG_IS_SERDES;
1318 else
1319 is_serdes = tr32(TG3_CPMU_PHY_STRAP) &
1320 TG3_CPMU_PHY_STRAP_IS_SERDES;
Matt Carlson882e9792009-09-01 13:21:36 +00001321 if (is_serdes)
1322 tp->phy_addr += 7;
1323 } else
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001324 tp->phy_addr = TG3_PHY_MII_ADDR;
Matt Carlson882e9792009-09-01 13:21:36 +00001325
Matt Carlson158d7ab2008-05-29 01:37:54 -07001326 tg3_mdio_start(tp);
1327
Joe Perches63c3a662011-04-26 08:12:10 +00001328 if (!tg3_flag(tp, USE_PHYLIB) || tg3_flag(tp, MDIOBUS_INITED))
Matt Carlson158d7ab2008-05-29 01:37:54 -07001329 return 0;
1330
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001331 tp->mdio_bus = mdiobus_alloc();
1332 if (tp->mdio_bus == NULL)
1333 return -ENOMEM;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001334
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001335 tp->mdio_bus->name = "tg3 mdio bus";
1336 snprintf(tp->mdio_bus->id, MII_BUS_ID_SIZE, "%x",
Matt Carlson158d7ab2008-05-29 01:37:54 -07001337 (tp->pdev->bus->number << 8) | tp->pdev->devfn);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001338 tp->mdio_bus->priv = tp;
1339 tp->mdio_bus->parent = &tp->pdev->dev;
1340 tp->mdio_bus->read = &tg3_mdio_read;
1341 tp->mdio_bus->write = &tg3_mdio_write;
1342 tp->mdio_bus->reset = &tg3_mdio_reset;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001343 tp->mdio_bus->phy_mask = ~(1 << TG3_PHY_MII_ADDR);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001344 tp->mdio_bus->irq = &tp->mdio_irq[0];
Matt Carlson158d7ab2008-05-29 01:37:54 -07001345
1346 for (i = 0; i < PHY_MAX_ADDR; i++)
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001347 tp->mdio_bus->irq[i] = PHY_POLL;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001348
1349 /* The bus registration will look for all the PHYs on the mdio bus.
1350 * Unfortunately, it does not ensure the PHY is powered up before
1351 * accessing the PHY ID registers. A chip reset is the
1352 * quickest way to bring the device back to an operational state..
1353 */
1354 if (tg3_readphy(tp, MII_BMCR, &reg) || (reg & BMCR_PDOWN))
1355 tg3_bmcr_reset(tp);
1356
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001357 i = mdiobus_register(tp->mdio_bus);
Matt Carlsona9daf362008-05-25 23:49:44 -07001358 if (i) {
Matt Carlsonab96b242010-04-05 10:19:22 +00001359 dev_warn(&tp->pdev->dev, "mdiobus_reg failed (0x%x)\n", i);
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001360 mdiobus_free(tp->mdio_bus);
Matt Carlsona9daf362008-05-25 23:49:44 -07001361 return i;
1362 }
Matt Carlson158d7ab2008-05-29 01:37:54 -07001363
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001364 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsona9daf362008-05-25 23:49:44 -07001365
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001366 if (!phydev || !phydev->drv) {
Matt Carlsonab96b242010-04-05 10:19:22 +00001367 dev_warn(&tp->pdev->dev, "No PHY devices\n");
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001368 mdiobus_unregister(tp->mdio_bus);
1369 mdiobus_free(tp->mdio_bus);
1370 return -ENODEV;
1371 }
1372
1373 switch (phydev->drv->phy_id & phydev->drv->phy_id_mask) {
Matt Carlson6a443a02010-02-17 15:17:04 +00001374 case PHY_ID_BCM57780:
Matt Carlson321d32a2008-11-21 17:22:19 -08001375 phydev->interface = PHY_INTERFACE_MODE_GMII;
Matt Carlsonc704dc22009-11-02 14:32:12 +00001376 phydev->dev_flags |= PHY_BRCM_AUTO_PWRDWN_ENABLE;
Matt Carlson321d32a2008-11-21 17:22:19 -08001377 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001378 case PHY_ID_BCM50610:
1379 case PHY_ID_BCM50610M:
Matt Carlson32e5a8d2009-11-02 14:31:39 +00001380 phydev->dev_flags |= PHY_BRCM_CLEAR_RGMII_MODE |
Matt Carlsonc704dc22009-11-02 14:32:12 +00001381 PHY_BRCM_RX_REFCLK_UNUSED |
Matt Carlson52fae082009-11-02 14:32:38 +00001382 PHY_BRCM_DIS_TXCRXC_NOENRGY |
Matt Carlsonc704dc22009-11-02 14:32:12 +00001383 PHY_BRCM_AUTO_PWRDWN_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00001384 if (tg3_flag(tp, RGMII_INBAND_DISABLE))
Matt Carlsona9daf362008-05-25 23:49:44 -07001385 phydev->dev_flags |= PHY_BRCM_STD_IBND_DISABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00001386 if (tg3_flag(tp, RGMII_EXT_IBND_RX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001387 phydev->dev_flags |= PHY_BRCM_EXT_IBND_RX_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00001388 if (tg3_flag(tp, RGMII_EXT_IBND_TX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001389 phydev->dev_flags |= PHY_BRCM_EXT_IBND_TX_ENABLE;
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001390 /* fallthru */
Matt Carlson6a443a02010-02-17 15:17:04 +00001391 case PHY_ID_RTL8211C:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001392 phydev->interface = PHY_INTERFACE_MODE_RGMII;
Matt Carlsona9daf362008-05-25 23:49:44 -07001393 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001394 case PHY_ID_RTL8201E:
1395 case PHY_ID_BCMAC131:
Matt Carlsona9daf362008-05-25 23:49:44 -07001396 phydev->interface = PHY_INTERFACE_MODE_MII;
Matt Carlsoncdd4e09d2009-11-02 14:31:11 +00001397 phydev->dev_flags |= PHY_BRCM_AUTO_PWRDWN_ENABLE;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001398 tp->phy_flags |= TG3_PHYFLG_IS_FET;
Matt Carlsona9daf362008-05-25 23:49:44 -07001399 break;
1400 }
1401
Joe Perches63c3a662011-04-26 08:12:10 +00001402 tg3_flag_set(tp, MDIOBUS_INITED);
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001403
1404 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
1405 tg3_mdio_config_5785(tp);
Matt Carlsona9daf362008-05-25 23:49:44 -07001406
1407 return 0;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001408}
1409
1410static void tg3_mdio_fini(struct tg3 *tp)
1411{
Joe Perches63c3a662011-04-26 08:12:10 +00001412 if (tg3_flag(tp, MDIOBUS_INITED)) {
1413 tg3_flag_clear(tp, MDIOBUS_INITED);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001414 mdiobus_unregister(tp->mdio_bus);
1415 mdiobus_free(tp->mdio_bus);
Matt Carlson158d7ab2008-05-29 01:37:54 -07001416 }
1417}
1418
Matt Carlson95e28692008-05-25 23:44:14 -07001419/* tp->lock is held. */
Matt Carlson4ba526c2008-08-15 14:10:04 -07001420static inline void tg3_generate_fw_event(struct tg3 *tp)
1421{
1422 u32 val;
1423
1424 val = tr32(GRC_RX_CPU_EVENT);
1425 val |= GRC_RX_CPU_DRIVER_EVENT;
1426 tw32_f(GRC_RX_CPU_EVENT, val);
1427
1428 tp->last_event_jiffies = jiffies;
1429}
1430
1431#define TG3_FW_EVENT_TIMEOUT_USEC 2500
1432
1433/* tp->lock is held. */
Matt Carlson95e28692008-05-25 23:44:14 -07001434static void tg3_wait_for_event_ack(struct tg3 *tp)
1435{
1436 int i;
Matt Carlson4ba526c2008-08-15 14:10:04 -07001437 unsigned int delay_cnt;
1438 long time_remain;
Matt Carlson95e28692008-05-25 23:44:14 -07001439
Matt Carlson4ba526c2008-08-15 14:10:04 -07001440 /* If enough time has passed, no wait is necessary. */
1441 time_remain = (long)(tp->last_event_jiffies + 1 +
1442 usecs_to_jiffies(TG3_FW_EVENT_TIMEOUT_USEC)) -
1443 (long)jiffies;
1444 if (time_remain < 0)
1445 return;
1446
1447 /* Check if we can shorten the wait time. */
1448 delay_cnt = jiffies_to_usecs(time_remain);
1449 if (delay_cnt > TG3_FW_EVENT_TIMEOUT_USEC)
1450 delay_cnt = TG3_FW_EVENT_TIMEOUT_USEC;
1451 delay_cnt = (delay_cnt >> 3) + 1;
1452
1453 for (i = 0; i < delay_cnt; i++) {
Matt Carlson95e28692008-05-25 23:44:14 -07001454 if (!(tr32(GRC_RX_CPU_EVENT) & GRC_RX_CPU_DRIVER_EVENT))
1455 break;
Matt Carlson4ba526c2008-08-15 14:10:04 -07001456 udelay(8);
Matt Carlson95e28692008-05-25 23:44:14 -07001457 }
1458}
1459
1460/* tp->lock is held. */
Matt Carlsonb28f3892012-02-13 15:20:12 +00001461static void tg3_phy_gather_ump_data(struct tg3 *tp, u32 *data)
Matt Carlson95e28692008-05-25 23:44:14 -07001462{
Matt Carlsonb28f3892012-02-13 15:20:12 +00001463 u32 reg, val;
Matt Carlson95e28692008-05-25 23:44:14 -07001464
1465 val = 0;
1466 if (!tg3_readphy(tp, MII_BMCR, &reg))
1467 val = reg << 16;
1468 if (!tg3_readphy(tp, MII_BMSR, &reg))
1469 val |= (reg & 0xffff);
Matt Carlsonb28f3892012-02-13 15:20:12 +00001470 *data++ = val;
Matt Carlson95e28692008-05-25 23:44:14 -07001471
1472 val = 0;
1473 if (!tg3_readphy(tp, MII_ADVERTISE, &reg))
1474 val = reg << 16;
1475 if (!tg3_readphy(tp, MII_LPA, &reg))
1476 val |= (reg & 0xffff);
Matt Carlsonb28f3892012-02-13 15:20:12 +00001477 *data++ = val;
Matt Carlson95e28692008-05-25 23:44:14 -07001478
1479 val = 0;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001480 if (!(tp->phy_flags & TG3_PHYFLG_MII_SERDES)) {
Matt Carlson95e28692008-05-25 23:44:14 -07001481 if (!tg3_readphy(tp, MII_CTRL1000, &reg))
1482 val = reg << 16;
1483 if (!tg3_readphy(tp, MII_STAT1000, &reg))
1484 val |= (reg & 0xffff);
1485 }
Matt Carlsonb28f3892012-02-13 15:20:12 +00001486 *data++ = val;
Matt Carlson95e28692008-05-25 23:44:14 -07001487
1488 if (!tg3_readphy(tp, MII_PHYADDR, &reg))
1489 val = reg << 16;
1490 else
1491 val = 0;
Matt Carlsonb28f3892012-02-13 15:20:12 +00001492 *data++ = val;
1493}
1494
1495/* tp->lock is held. */
1496static void tg3_ump_link_report(struct tg3 *tp)
1497{
1498 u32 data[4];
1499
1500 if (!tg3_flag(tp, 5780_CLASS) || !tg3_flag(tp, ENABLE_ASF))
1501 return;
1502
1503 tg3_phy_gather_ump_data(tp, data);
1504
1505 tg3_wait_for_event_ack(tp);
1506
1507 tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX, FWCMD_NICDRV_LINK_UPDATE);
1508 tg3_write_mem(tp, NIC_SRAM_FW_CMD_LEN_MBOX, 14);
1509 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 0x0, data[0]);
1510 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 0x4, data[1]);
1511 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 0x8, data[2]);
1512 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 0xc, data[3]);
Matt Carlson95e28692008-05-25 23:44:14 -07001513
Matt Carlson4ba526c2008-08-15 14:10:04 -07001514 tg3_generate_fw_event(tp);
Matt Carlson95e28692008-05-25 23:44:14 -07001515}
1516
Matt Carlson8d5a89b2011-08-31 11:44:51 +00001517/* tp->lock is held. */
1518static void tg3_stop_fw(struct tg3 *tp)
1519{
1520 if (tg3_flag(tp, ENABLE_ASF) && !tg3_flag(tp, ENABLE_APE)) {
1521 /* Wait for RX cpu to ACK the previous event. */
1522 tg3_wait_for_event_ack(tp);
1523
1524 tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX, FWCMD_NICDRV_PAUSE_FW);
1525
1526 tg3_generate_fw_event(tp);
1527
1528 /* Wait for RX cpu to ACK this event. */
1529 tg3_wait_for_event_ack(tp);
1530 }
1531}
1532
Matt Carlsonfd6d3f02011-08-31 11:44:52 +00001533/* tp->lock is held. */
1534static void tg3_write_sig_pre_reset(struct tg3 *tp, int kind)
1535{
1536 tg3_write_mem(tp, NIC_SRAM_FIRMWARE_MBOX,
1537 NIC_SRAM_FIRMWARE_MBOX_MAGIC1);
1538
1539 if (tg3_flag(tp, ASF_NEW_HANDSHAKE)) {
1540 switch (kind) {
1541 case RESET_KIND_INIT:
1542 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1543 DRV_STATE_START);
1544 break;
1545
1546 case RESET_KIND_SHUTDOWN:
1547 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1548 DRV_STATE_UNLOAD);
1549 break;
1550
1551 case RESET_KIND_SUSPEND:
1552 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1553 DRV_STATE_SUSPEND);
1554 break;
1555
1556 default:
1557 break;
1558 }
1559 }
1560
1561 if (kind == RESET_KIND_INIT ||
1562 kind == RESET_KIND_SUSPEND)
1563 tg3_ape_driver_state_change(tp, kind);
1564}
1565
1566/* tp->lock is held. */
1567static void tg3_write_sig_post_reset(struct tg3 *tp, int kind)
1568{
1569 if (tg3_flag(tp, ASF_NEW_HANDSHAKE)) {
1570 switch (kind) {
1571 case RESET_KIND_INIT:
1572 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1573 DRV_STATE_START_DONE);
1574 break;
1575
1576 case RESET_KIND_SHUTDOWN:
1577 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1578 DRV_STATE_UNLOAD_DONE);
1579 break;
1580
1581 default:
1582 break;
1583 }
1584 }
1585
1586 if (kind == RESET_KIND_SHUTDOWN)
1587 tg3_ape_driver_state_change(tp, kind);
1588}
1589
1590/* tp->lock is held. */
1591static void tg3_write_sig_legacy(struct tg3 *tp, int kind)
1592{
1593 if (tg3_flag(tp, ENABLE_ASF)) {
1594 switch (kind) {
1595 case RESET_KIND_INIT:
1596 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1597 DRV_STATE_START);
1598 break;
1599
1600 case RESET_KIND_SHUTDOWN:
1601 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1602 DRV_STATE_UNLOAD);
1603 break;
1604
1605 case RESET_KIND_SUSPEND:
1606 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1607 DRV_STATE_SUSPEND);
1608 break;
1609
1610 default:
1611 break;
1612 }
1613 }
1614}
1615
1616static int tg3_poll_fw(struct tg3 *tp)
1617{
1618 int i;
1619 u32 val;
1620
1621 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
1622 /* Wait up to 20ms for init done. */
1623 for (i = 0; i < 200; i++) {
1624 if (tr32(VCPU_STATUS) & VCPU_STATUS_INIT_DONE)
1625 return 0;
1626 udelay(100);
1627 }
1628 return -ENODEV;
1629 }
1630
1631 /* Wait for firmware initialization to complete. */
1632 for (i = 0; i < 100000; i++) {
1633 tg3_read_mem(tp, NIC_SRAM_FIRMWARE_MBOX, &val);
1634 if (val == ~NIC_SRAM_FIRMWARE_MBOX_MAGIC1)
1635 break;
1636 udelay(10);
1637 }
1638
1639 /* Chip might not be fitted with firmware. Some Sun onboard
1640 * parts are configured like that. So don't signal the timeout
1641 * of the above loop as an error, but do report the lack of
1642 * running firmware once.
1643 */
1644 if (i >= 100000 && !tg3_flag(tp, NO_FWARE_REPORTED)) {
1645 tg3_flag_set(tp, NO_FWARE_REPORTED);
1646
1647 netdev_info(tp->dev, "No firmware running\n");
1648 }
1649
1650 if (tp->pci_chip_rev_id == CHIPREV_ID_57765_A0) {
1651 /* The 57765 A0 needs a little more
1652 * time to do some important work.
1653 */
1654 mdelay(10);
1655 }
1656
1657 return 0;
1658}
1659
Matt Carlson95e28692008-05-25 23:44:14 -07001660static void tg3_link_report(struct tg3 *tp)
1661{
1662 if (!netif_carrier_ok(tp->dev)) {
Joe Perches05dbe002010-02-17 19:44:19 +00001663 netif_info(tp, link, tp->dev, "Link is down\n");
Matt Carlson95e28692008-05-25 23:44:14 -07001664 tg3_ump_link_report(tp);
1665 } else if (netif_msg_link(tp)) {
Joe Perches05dbe002010-02-17 19:44:19 +00001666 netdev_info(tp->dev, "Link is up at %d Mbps, %s duplex\n",
1667 (tp->link_config.active_speed == SPEED_1000 ?
1668 1000 :
1669 (tp->link_config.active_speed == SPEED_100 ?
1670 100 : 10)),
1671 (tp->link_config.active_duplex == DUPLEX_FULL ?
1672 "full" : "half"));
Matt Carlson95e28692008-05-25 23:44:14 -07001673
Joe Perches05dbe002010-02-17 19:44:19 +00001674 netdev_info(tp->dev, "Flow control is %s for TX and %s for RX\n",
1675 (tp->link_config.active_flowctrl & FLOW_CTRL_TX) ?
1676 "on" : "off",
1677 (tp->link_config.active_flowctrl & FLOW_CTRL_RX) ?
1678 "on" : "off");
Matt Carlson47007832011-04-20 07:57:43 +00001679
1680 if (tp->phy_flags & TG3_PHYFLG_EEE_CAP)
1681 netdev_info(tp->dev, "EEE is %s\n",
1682 tp->setlpicnt ? "enabled" : "disabled");
1683
Matt Carlson95e28692008-05-25 23:44:14 -07001684 tg3_ump_link_report(tp);
1685 }
1686}
1687
Matt Carlson95e28692008-05-25 23:44:14 -07001688static u16 tg3_advert_flowctrl_1000X(u8 flow_ctrl)
1689{
1690 u16 miireg;
1691
Steve Glendinninge18ce342008-12-16 02:00:00 -08001692 if ((flow_ctrl & FLOW_CTRL_TX) && (flow_ctrl & FLOW_CTRL_RX))
Matt Carlson95e28692008-05-25 23:44:14 -07001693 miireg = ADVERTISE_1000XPAUSE;
Steve Glendinninge18ce342008-12-16 02:00:00 -08001694 else if (flow_ctrl & FLOW_CTRL_TX)
Matt Carlson95e28692008-05-25 23:44:14 -07001695 miireg = ADVERTISE_1000XPSE_ASYM;
Steve Glendinninge18ce342008-12-16 02:00:00 -08001696 else if (flow_ctrl & FLOW_CTRL_RX)
Matt Carlson95e28692008-05-25 23:44:14 -07001697 miireg = ADVERTISE_1000XPAUSE | ADVERTISE_1000XPSE_ASYM;
1698 else
1699 miireg = 0;
1700
1701 return miireg;
1702}
1703
Matt Carlson95e28692008-05-25 23:44:14 -07001704static u8 tg3_resolve_flowctrl_1000X(u16 lcladv, u16 rmtadv)
1705{
1706 u8 cap = 0;
1707
Matt Carlsonf3791cd2011-11-21 15:01:17 +00001708 if (lcladv & rmtadv & ADVERTISE_1000XPAUSE) {
1709 cap = FLOW_CTRL_TX | FLOW_CTRL_RX;
1710 } else if (lcladv & rmtadv & ADVERTISE_1000XPSE_ASYM) {
1711 if (lcladv & ADVERTISE_1000XPAUSE)
1712 cap = FLOW_CTRL_RX;
1713 if (rmtadv & ADVERTISE_1000XPAUSE)
Steve Glendinninge18ce342008-12-16 02:00:00 -08001714 cap = FLOW_CTRL_TX;
Matt Carlson95e28692008-05-25 23:44:14 -07001715 }
1716
1717 return cap;
1718}
1719
Matt Carlsonf51f3562008-05-25 23:45:08 -07001720static void tg3_setup_flow_control(struct tg3 *tp, u32 lcladv, u32 rmtadv)
Matt Carlson95e28692008-05-25 23:44:14 -07001721{
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001722 u8 autoneg;
Matt Carlsonf51f3562008-05-25 23:45:08 -07001723 u8 flowctrl = 0;
Matt Carlson95e28692008-05-25 23:44:14 -07001724 u32 old_rx_mode = tp->rx_mode;
1725 u32 old_tx_mode = tp->tx_mode;
1726
Joe Perches63c3a662011-04-26 08:12:10 +00001727 if (tg3_flag(tp, USE_PHYLIB))
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001728 autoneg = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]->autoneg;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001729 else
1730 autoneg = tp->link_config.autoneg;
1731
Joe Perches63c3a662011-04-26 08:12:10 +00001732 if (autoneg == AUTONEG_ENABLE && tg3_flag(tp, PAUSE_AUTONEG)) {
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001733 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
Matt Carlsonf51f3562008-05-25 23:45:08 -07001734 flowctrl = tg3_resolve_flowctrl_1000X(lcladv, rmtadv);
Matt Carlson95e28692008-05-25 23:44:14 -07001735 else
Steve Glendinningbc02ff92008-12-16 02:00:48 -08001736 flowctrl = mii_resolve_flowctrl_fdx(lcladv, rmtadv);
Matt Carlsonf51f3562008-05-25 23:45:08 -07001737 } else
1738 flowctrl = tp->link_config.flowctrl;
Matt Carlson95e28692008-05-25 23:44:14 -07001739
Matt Carlsonf51f3562008-05-25 23:45:08 -07001740 tp->link_config.active_flowctrl = flowctrl;
Matt Carlson95e28692008-05-25 23:44:14 -07001741
Steve Glendinninge18ce342008-12-16 02:00:00 -08001742 if (flowctrl & FLOW_CTRL_RX)
Matt Carlson95e28692008-05-25 23:44:14 -07001743 tp->rx_mode |= RX_MODE_FLOW_CTRL_ENABLE;
1744 else
1745 tp->rx_mode &= ~RX_MODE_FLOW_CTRL_ENABLE;
1746
Matt Carlsonf51f3562008-05-25 23:45:08 -07001747 if (old_rx_mode != tp->rx_mode)
Matt Carlson95e28692008-05-25 23:44:14 -07001748 tw32_f(MAC_RX_MODE, tp->rx_mode);
Matt Carlson95e28692008-05-25 23:44:14 -07001749
Steve Glendinninge18ce342008-12-16 02:00:00 -08001750 if (flowctrl & FLOW_CTRL_TX)
Matt Carlson95e28692008-05-25 23:44:14 -07001751 tp->tx_mode |= TX_MODE_FLOW_CTRL_ENABLE;
1752 else
1753 tp->tx_mode &= ~TX_MODE_FLOW_CTRL_ENABLE;
1754
Matt Carlsonf51f3562008-05-25 23:45:08 -07001755 if (old_tx_mode != tp->tx_mode)
Matt Carlson95e28692008-05-25 23:44:14 -07001756 tw32_f(MAC_TX_MODE, tp->tx_mode);
Matt Carlson95e28692008-05-25 23:44:14 -07001757}
1758
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001759static void tg3_adjust_link(struct net_device *dev)
1760{
1761 u8 oldflowctrl, linkmesg = 0;
1762 u32 mac_mode, lcl_adv, rmt_adv;
1763 struct tg3 *tp = netdev_priv(dev);
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001764 struct phy_device *phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001765
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001766 spin_lock_bh(&tp->lock);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001767
1768 mac_mode = tp->mac_mode & ~(MAC_MODE_PORT_MODE_MASK |
1769 MAC_MODE_HALF_DUPLEX);
1770
1771 oldflowctrl = tp->link_config.active_flowctrl;
1772
1773 if (phydev->link) {
1774 lcl_adv = 0;
1775 rmt_adv = 0;
1776
1777 if (phydev->speed == SPEED_100 || phydev->speed == SPEED_10)
1778 mac_mode |= MAC_MODE_PORT_MODE_MII;
Matt Carlsonc3df0742009-11-02 14:27:02 +00001779 else if (phydev->speed == SPEED_1000 ||
1780 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785)
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001781 mac_mode |= MAC_MODE_PORT_MODE_GMII;
Matt Carlsonc3df0742009-11-02 14:27:02 +00001782 else
1783 mac_mode |= MAC_MODE_PORT_MODE_MII;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001784
1785 if (phydev->duplex == DUPLEX_HALF)
1786 mac_mode |= MAC_MODE_HALF_DUPLEX;
1787 else {
Matt Carlsonf88788f2011-12-14 11:10:00 +00001788 lcl_adv = mii_advertise_flowctrl(
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001789 tp->link_config.flowctrl);
1790
1791 if (phydev->pause)
1792 rmt_adv = LPA_PAUSE_CAP;
1793 if (phydev->asym_pause)
1794 rmt_adv |= LPA_PAUSE_ASYM;
1795 }
1796
1797 tg3_setup_flow_control(tp, lcl_adv, rmt_adv);
1798 } else
1799 mac_mode |= MAC_MODE_PORT_MODE_GMII;
1800
1801 if (mac_mode != tp->mac_mode) {
1802 tp->mac_mode = mac_mode;
1803 tw32_f(MAC_MODE, tp->mac_mode);
1804 udelay(40);
1805 }
1806
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001807 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785) {
1808 if (phydev->speed == SPEED_10)
1809 tw32(MAC_MI_STAT,
1810 MAC_MI_STAT_10MBPS_MODE |
1811 MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
1812 else
1813 tw32(MAC_MI_STAT, MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
1814 }
1815
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001816 if (phydev->speed == SPEED_1000 && phydev->duplex == DUPLEX_HALF)
1817 tw32(MAC_TX_LENGTHS,
1818 ((2 << TX_LENGTHS_IPG_CRS_SHIFT) |
1819 (6 << TX_LENGTHS_IPG_SHIFT) |
1820 (0xff << TX_LENGTHS_SLOT_TIME_SHIFT)));
1821 else
1822 tw32(MAC_TX_LENGTHS,
1823 ((2 << TX_LENGTHS_IPG_CRS_SHIFT) |
1824 (6 << TX_LENGTHS_IPG_SHIFT) |
1825 (32 << TX_LENGTHS_SLOT_TIME_SHIFT)));
1826
Matt Carlson34655ad2012-02-22 12:35:18 +00001827 if (phydev->link != tp->old_link ||
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001828 phydev->speed != tp->link_config.active_speed ||
1829 phydev->duplex != tp->link_config.active_duplex ||
1830 oldflowctrl != tp->link_config.active_flowctrl)
Matt Carlsonc6cdf432010-04-05 10:19:26 +00001831 linkmesg = 1;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001832
Matt Carlson34655ad2012-02-22 12:35:18 +00001833 tp->old_link = phydev->link;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001834 tp->link_config.active_speed = phydev->speed;
1835 tp->link_config.active_duplex = phydev->duplex;
1836
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001837 spin_unlock_bh(&tp->lock);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001838
1839 if (linkmesg)
1840 tg3_link_report(tp);
1841}
1842
1843static int tg3_phy_init(struct tg3 *tp)
1844{
1845 struct phy_device *phydev;
1846
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001847 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED)
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001848 return 0;
1849
1850 /* Bring the PHY back to a known state. */
1851 tg3_bmcr_reset(tp);
1852
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001853 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001854
1855 /* Attach the MAC to the PHY. */
Kay Sieversfb28ad352008-11-10 13:55:14 -08001856 phydev = phy_connect(tp->dev, dev_name(&phydev->dev), tg3_adjust_link,
Matt Carlsona9daf362008-05-25 23:49:44 -07001857 phydev->dev_flags, phydev->interface);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001858 if (IS_ERR(phydev)) {
Matt Carlsonab96b242010-04-05 10:19:22 +00001859 dev_err(&tp->pdev->dev, "Could not attach to PHY\n");
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001860 return PTR_ERR(phydev);
1861 }
1862
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001863 /* Mask with MAC supported features. */
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001864 switch (phydev->interface) {
1865 case PHY_INTERFACE_MODE_GMII:
1866 case PHY_INTERFACE_MODE_RGMII:
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001867 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
Matt Carlson321d32a2008-11-21 17:22:19 -08001868 phydev->supported &= (PHY_GBIT_FEATURES |
1869 SUPPORTED_Pause |
1870 SUPPORTED_Asym_Pause);
1871 break;
1872 }
1873 /* fallthru */
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001874 case PHY_INTERFACE_MODE_MII:
1875 phydev->supported &= (PHY_BASIC_FEATURES |
1876 SUPPORTED_Pause |
1877 SUPPORTED_Asym_Pause);
1878 break;
1879 default:
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001880 phy_disconnect(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001881 return -EINVAL;
1882 }
1883
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001884 tp->phy_flags |= TG3_PHYFLG_IS_CONNECTED;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001885
1886 phydev->advertising = phydev->supported;
1887
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001888 return 0;
1889}
1890
1891static void tg3_phy_start(struct tg3 *tp)
1892{
1893 struct phy_device *phydev;
1894
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001895 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001896 return;
1897
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001898 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001899
Matt Carlson80096062010-08-02 11:26:06 +00001900 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) {
1901 tp->phy_flags &= ~TG3_PHYFLG_IS_LOW_POWER;
Matt Carlsonc6700ce2012-02-13 15:20:15 +00001902 phydev->speed = tp->link_config.speed;
1903 phydev->duplex = tp->link_config.duplex;
1904 phydev->autoneg = tp->link_config.autoneg;
1905 phydev->advertising = tp->link_config.advertising;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001906 }
1907
1908 phy_start(phydev);
1909
1910 phy_start_aneg(phydev);
1911}
1912
1913static void tg3_phy_stop(struct tg3 *tp)
1914{
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001915 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001916 return;
1917
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001918 phy_stop(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001919}
1920
1921static void tg3_phy_fini(struct tg3 *tp)
1922{
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001923 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001924 phy_disconnect(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001925 tp->phy_flags &= ~TG3_PHYFLG_IS_CONNECTED;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001926 }
1927}
1928
Matt Carlson941ec902011-08-19 13:58:23 +00001929static int tg3_phy_set_extloopbk(struct tg3 *tp)
1930{
1931 int err;
1932 u32 val;
1933
1934 if (tp->phy_flags & TG3_PHYFLG_IS_FET)
1935 return 0;
1936
1937 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
1938 /* Cannot do read-modify-write on 5401 */
1939 err = tg3_phy_auxctl_write(tp,
1940 MII_TG3_AUXCTL_SHDWSEL_AUXCTL,
1941 MII_TG3_AUXCTL_ACTL_EXTLOOPBK |
1942 0x4c20);
1943 goto done;
1944 }
1945
1946 err = tg3_phy_auxctl_read(tp,
1947 MII_TG3_AUXCTL_SHDWSEL_AUXCTL, &val);
1948 if (err)
1949 return err;
1950
1951 val |= MII_TG3_AUXCTL_ACTL_EXTLOOPBK;
1952 err = tg3_phy_auxctl_write(tp,
1953 MII_TG3_AUXCTL_SHDWSEL_AUXCTL, val);
1954
1955done:
1956 return err;
1957}
1958
Matt Carlson7f97a4b2009-08-25 10:10:03 +00001959static void tg3_phy_fet_toggle_apd(struct tg3 *tp, bool enable)
1960{
1961 u32 phytest;
1962
1963 if (!tg3_readphy(tp, MII_TG3_FET_TEST, &phytest)) {
1964 u32 phy;
1965
1966 tg3_writephy(tp, MII_TG3_FET_TEST,
1967 phytest | MII_TG3_FET_SHADOW_EN);
1968 if (!tg3_readphy(tp, MII_TG3_FET_SHDW_AUXSTAT2, &phy)) {
1969 if (enable)
1970 phy |= MII_TG3_FET_SHDW_AUXSTAT2_APD;
1971 else
1972 phy &= ~MII_TG3_FET_SHDW_AUXSTAT2_APD;
1973 tg3_writephy(tp, MII_TG3_FET_SHDW_AUXSTAT2, phy);
1974 }
1975 tg3_writephy(tp, MII_TG3_FET_TEST, phytest);
1976 }
1977}
1978
Matt Carlson6833c042008-11-21 17:18:59 -08001979static void tg3_phy_toggle_apd(struct tg3 *tp, bool enable)
1980{
1981 u32 reg;
1982
Joe Perches63c3a662011-04-26 08:12:10 +00001983 if (!tg3_flag(tp, 5705_PLUS) ||
1984 (tg3_flag(tp, 5717_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001985 (tp->phy_flags & TG3_PHYFLG_MII_SERDES)))
Matt Carlson6833c042008-11-21 17:18:59 -08001986 return;
1987
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001988 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Matt Carlson7f97a4b2009-08-25 10:10:03 +00001989 tg3_phy_fet_toggle_apd(tp, enable);
1990 return;
1991 }
1992
Matt Carlson6833c042008-11-21 17:18:59 -08001993 reg = MII_TG3_MISC_SHDW_WREN |
1994 MII_TG3_MISC_SHDW_SCR5_SEL |
1995 MII_TG3_MISC_SHDW_SCR5_LPED |
1996 MII_TG3_MISC_SHDW_SCR5_DLPTLM |
1997 MII_TG3_MISC_SHDW_SCR5_SDTL |
1998 MII_TG3_MISC_SHDW_SCR5_C125OE;
1999 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5784 || !enable)
2000 reg |= MII_TG3_MISC_SHDW_SCR5_DLLAPD;
2001
2002 tg3_writephy(tp, MII_TG3_MISC_SHDW, reg);
2003
2004
2005 reg = MII_TG3_MISC_SHDW_WREN |
2006 MII_TG3_MISC_SHDW_APD_SEL |
2007 MII_TG3_MISC_SHDW_APD_WKTM_84MS;
2008 if (enable)
2009 reg |= MII_TG3_MISC_SHDW_APD_ENABLE;
2010
2011 tg3_writephy(tp, MII_TG3_MISC_SHDW, reg);
2012}
2013
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002014static void tg3_phy_toggle_automdix(struct tg3 *tp, int enable)
2015{
2016 u32 phy;
2017
Joe Perches63c3a662011-04-26 08:12:10 +00002018 if (!tg3_flag(tp, 5705_PLUS) ||
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002019 (tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002020 return;
2021
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002022 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002023 u32 ephy;
2024
Matt Carlson535ef6e2009-08-25 10:09:36 +00002025 if (!tg3_readphy(tp, MII_TG3_FET_TEST, &ephy)) {
2026 u32 reg = MII_TG3_FET_SHDW_MISCCTRL;
2027
2028 tg3_writephy(tp, MII_TG3_FET_TEST,
2029 ephy | MII_TG3_FET_SHADOW_EN);
2030 if (!tg3_readphy(tp, reg, &phy)) {
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002031 if (enable)
Matt Carlson535ef6e2009-08-25 10:09:36 +00002032 phy |= MII_TG3_FET_SHDW_MISCCTRL_MDIX;
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002033 else
Matt Carlson535ef6e2009-08-25 10:09:36 +00002034 phy &= ~MII_TG3_FET_SHDW_MISCCTRL_MDIX;
2035 tg3_writephy(tp, reg, phy);
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002036 }
Matt Carlson535ef6e2009-08-25 10:09:36 +00002037 tg3_writephy(tp, MII_TG3_FET_TEST, ephy);
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002038 }
2039 } else {
Matt Carlson15ee95c2011-04-20 07:57:40 +00002040 int ret;
2041
2042 ret = tg3_phy_auxctl_read(tp,
2043 MII_TG3_AUXCTL_SHDWSEL_MISC, &phy);
2044 if (!ret) {
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002045 if (enable)
2046 phy |= MII_TG3_AUXCTL_MISC_FORCE_AMDIX;
2047 else
2048 phy &= ~MII_TG3_AUXCTL_MISC_FORCE_AMDIX;
Matt Carlsonb4bd2922011-04-20 07:57:41 +00002049 tg3_phy_auxctl_write(tp,
2050 MII_TG3_AUXCTL_SHDWSEL_MISC, phy);
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002051 }
2052 }
2053}
2054
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055static void tg3_phy_set_wirespeed(struct tg3 *tp)
2056{
Matt Carlson15ee95c2011-04-20 07:57:40 +00002057 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 u32 val;
2059
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002060 if (tp->phy_flags & TG3_PHYFLG_NO_ETH_WIRE_SPEED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061 return;
2062
Matt Carlson15ee95c2011-04-20 07:57:40 +00002063 ret = tg3_phy_auxctl_read(tp, MII_TG3_AUXCTL_SHDWSEL_MISC, &val);
2064 if (!ret)
Matt Carlsonb4bd2922011-04-20 07:57:41 +00002065 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_MISC,
2066 val | MII_TG3_AUXCTL_MISC_WIRESPD_EN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067}
2068
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002069static void tg3_phy_apply_otp(struct tg3 *tp)
2070{
2071 u32 otp, phy;
2072
2073 if (!tp->phy_otp)
2074 return;
2075
2076 otp = tp->phy_otp;
2077
Matt Carlson1d36ba42011-04-20 07:57:42 +00002078 if (TG3_PHY_AUXCTL_SMDSP_ENABLE(tp))
2079 return;
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002080
2081 phy = ((otp & TG3_OTP_AGCTGT_MASK) >> TG3_OTP_AGCTGT_SHIFT);
2082 phy |= MII_TG3_DSP_TAP1_AGCTGT_DFLT;
2083 tg3_phydsp_write(tp, MII_TG3_DSP_TAP1, phy);
2084
2085 phy = ((otp & TG3_OTP_HPFFLTR_MASK) >> TG3_OTP_HPFFLTR_SHIFT) |
2086 ((otp & TG3_OTP_HPFOVER_MASK) >> TG3_OTP_HPFOVER_SHIFT);
2087 tg3_phydsp_write(tp, MII_TG3_DSP_AADJ1CH0, phy);
2088
2089 phy = ((otp & TG3_OTP_LPFDIS_MASK) >> TG3_OTP_LPFDIS_SHIFT);
2090 phy |= MII_TG3_DSP_AADJ1CH3_ADCCKADJ;
2091 tg3_phydsp_write(tp, MII_TG3_DSP_AADJ1CH3, phy);
2092
2093 phy = ((otp & TG3_OTP_VDAC_MASK) >> TG3_OTP_VDAC_SHIFT);
2094 tg3_phydsp_write(tp, MII_TG3_DSP_EXP75, phy);
2095
2096 phy = ((otp & TG3_OTP_10BTAMP_MASK) >> TG3_OTP_10BTAMP_SHIFT);
2097 tg3_phydsp_write(tp, MII_TG3_DSP_EXP96, phy);
2098
2099 phy = ((otp & TG3_OTP_ROFF_MASK) >> TG3_OTP_ROFF_SHIFT) |
2100 ((otp & TG3_OTP_RCOFF_MASK) >> TG3_OTP_RCOFF_SHIFT);
2101 tg3_phydsp_write(tp, MII_TG3_DSP_EXP97, phy);
2102
Matt Carlson1d36ba42011-04-20 07:57:42 +00002103 TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002104}
2105
Matt Carlson52b02d02010-10-14 10:37:41 +00002106static void tg3_phy_eee_adjust(struct tg3 *tp, u32 current_link_up)
2107{
2108 u32 val;
2109
2110 if (!(tp->phy_flags & TG3_PHYFLG_EEE_CAP))
2111 return;
2112
2113 tp->setlpicnt = 0;
2114
2115 if (tp->link_config.autoneg == AUTONEG_ENABLE &&
2116 current_link_up == 1 &&
Matt Carlsona6b68da2010-12-06 08:28:52 +00002117 tp->link_config.active_duplex == DUPLEX_FULL &&
2118 (tp->link_config.active_speed == SPEED_100 ||
2119 tp->link_config.active_speed == SPEED_1000)) {
Matt Carlson52b02d02010-10-14 10:37:41 +00002120 u32 eeectl;
2121
2122 if (tp->link_config.active_speed == SPEED_1000)
2123 eeectl = TG3_CPMU_EEE_CTRL_EXIT_16_5_US;
2124 else
2125 eeectl = TG3_CPMU_EEE_CTRL_EXIT_36_US;
2126
2127 tw32(TG3_CPMU_EEE_CTRL, eeectl);
2128
Matt Carlson3110f5f52010-12-06 08:28:50 +00002129 tg3_phy_cl45_read(tp, MDIO_MMD_AN,
2130 TG3_CL45_D7_EEERES_STAT, &val);
Matt Carlson52b02d02010-10-14 10:37:41 +00002131
Matt Carlsonb0c59432011-05-19 12:12:48 +00002132 if (val == TG3_CL45_D7_EEERES_STAT_LP_1000T ||
2133 val == TG3_CL45_D7_EEERES_STAT_LP_100TX)
Matt Carlson52b02d02010-10-14 10:37:41 +00002134 tp->setlpicnt = 2;
2135 }
2136
2137 if (!tp->setlpicnt) {
Matt Carlsonb715ce92011-07-20 10:20:52 +00002138 if (current_link_up == 1 &&
2139 !TG3_PHY_AUXCTL_SMDSP_ENABLE(tp)) {
2140 tg3_phydsp_write(tp, MII_TG3_DSP_TAP26, 0x0000);
2141 TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
2142 }
2143
Matt Carlson52b02d02010-10-14 10:37:41 +00002144 val = tr32(TG3_CPMU_EEE_MODE);
2145 tw32(TG3_CPMU_EEE_MODE, val & ~TG3_CPMU_EEEMD_LPI_ENABLE);
2146 }
2147}
2148
Matt Carlsonb0c59432011-05-19 12:12:48 +00002149static void tg3_phy_eee_enable(struct tg3 *tp)
2150{
2151 u32 val;
2152
2153 if (tp->link_config.active_speed == SPEED_1000 &&
2154 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
2155 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
Matt Carlson55086ad2011-12-14 11:09:59 +00002156 tg3_flag(tp, 57765_CLASS)) &&
Matt Carlsonb0c59432011-05-19 12:12:48 +00002157 !TG3_PHY_AUXCTL_SMDSP_ENABLE(tp)) {
Matt Carlsonb715ce92011-07-20 10:20:52 +00002158 val = MII_TG3_DSP_TAP26_ALNOKO |
2159 MII_TG3_DSP_TAP26_RMRXSTO;
2160 tg3_phydsp_write(tp, MII_TG3_DSP_TAP26, val);
Matt Carlsonb0c59432011-05-19 12:12:48 +00002161 TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
2162 }
2163
2164 val = tr32(TG3_CPMU_EEE_MODE);
2165 tw32(TG3_CPMU_EEE_MODE, val | TG3_CPMU_EEEMD_LPI_ENABLE);
2166}
2167
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168static int tg3_wait_macro_done(struct tg3 *tp)
2169{
2170 int limit = 100;
2171
2172 while (limit--) {
2173 u32 tmp32;
2174
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002175 if (!tg3_readphy(tp, MII_TG3_DSP_CONTROL, &tmp32)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176 if ((tmp32 & 0x1000) == 0)
2177 break;
2178 }
2179 }
Roel Kluind4675b52009-02-12 16:33:27 -08002180 if (limit < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181 return -EBUSY;
2182
2183 return 0;
2184}
2185
2186static int tg3_phy_write_and_check_testpat(struct tg3 *tp, int *resetp)
2187{
2188 static const u32 test_pat[4][6] = {
2189 { 0x00005555, 0x00000005, 0x00002aaa, 0x0000000a, 0x00003456, 0x00000003 },
2190 { 0x00002aaa, 0x0000000a, 0x00003333, 0x00000003, 0x0000789a, 0x00000005 },
2191 { 0x00005a5a, 0x00000005, 0x00002a6a, 0x0000000a, 0x00001bcd, 0x00000003 },
2192 { 0x00002a5a, 0x0000000a, 0x000033c3, 0x00000003, 0x00002ef1, 0x00000005 }
2193 };
2194 int chan;
2195
2196 for (chan = 0; chan < 4; chan++) {
2197 int i;
2198
2199 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
2200 (chan * 0x2000) | 0x0200);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002201 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0002);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202
2203 for (i = 0; i < 6; i++)
2204 tg3_writephy(tp, MII_TG3_DSP_RW_PORT,
2205 test_pat[chan][i]);
2206
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002207 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0202);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208 if (tg3_wait_macro_done(tp)) {
2209 *resetp = 1;
2210 return -EBUSY;
2211 }
2212
2213 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
2214 (chan * 0x2000) | 0x0200);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002215 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0082);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216 if (tg3_wait_macro_done(tp)) {
2217 *resetp = 1;
2218 return -EBUSY;
2219 }
2220
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002221 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0802);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222 if (tg3_wait_macro_done(tp)) {
2223 *resetp = 1;
2224 return -EBUSY;
2225 }
2226
2227 for (i = 0; i < 6; i += 2) {
2228 u32 low, high;
2229
2230 if (tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &low) ||
2231 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &high) ||
2232 tg3_wait_macro_done(tp)) {
2233 *resetp = 1;
2234 return -EBUSY;
2235 }
2236 low &= 0x7fff;
2237 high &= 0x000f;
2238 if (low != test_pat[chan][i] ||
2239 high != test_pat[chan][i+1]) {
2240 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x000b);
2241 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x4001);
2242 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x4005);
2243
2244 return -EBUSY;
2245 }
2246 }
2247 }
2248
2249 return 0;
2250}
2251
2252static int tg3_phy_reset_chanpat(struct tg3 *tp)
2253{
2254 int chan;
2255
2256 for (chan = 0; chan < 4; chan++) {
2257 int i;
2258
2259 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
2260 (chan * 0x2000) | 0x0200);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002261 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0002);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262 for (i = 0; i < 6; i++)
2263 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x000);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002264 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0202);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265 if (tg3_wait_macro_done(tp))
2266 return -EBUSY;
2267 }
2268
2269 return 0;
2270}
2271
2272static int tg3_phy_reset_5703_4_5(struct tg3 *tp)
2273{
2274 u32 reg32, phy9_orig;
2275 int retries, do_phy_reset, err;
2276
2277 retries = 10;
2278 do_phy_reset = 1;
2279 do {
2280 if (do_phy_reset) {
2281 err = tg3_bmcr_reset(tp);
2282 if (err)
2283 return err;
2284 do_phy_reset = 0;
2285 }
2286
2287 /* Disable transmitter and interrupt. */
2288 if (tg3_readphy(tp, MII_TG3_EXT_CTRL, &reg32))
2289 continue;
2290
2291 reg32 |= 0x3000;
2292 tg3_writephy(tp, MII_TG3_EXT_CTRL, reg32);
2293
2294 /* Set full-duplex, 1000 mbps. */
2295 tg3_writephy(tp, MII_BMCR,
Matt Carlson221c5632011-06-13 13:39:01 +00002296 BMCR_FULLDPLX | BMCR_SPEED1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297
2298 /* Set to master mode. */
Matt Carlson221c5632011-06-13 13:39:01 +00002299 if (tg3_readphy(tp, MII_CTRL1000, &phy9_orig))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300 continue;
2301
Matt Carlson221c5632011-06-13 13:39:01 +00002302 tg3_writephy(tp, MII_CTRL1000,
2303 CTL1000_AS_MASTER | CTL1000_ENABLE_MASTER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304
Matt Carlson1d36ba42011-04-20 07:57:42 +00002305 err = TG3_PHY_AUXCTL_SMDSP_ENABLE(tp);
2306 if (err)
2307 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308
2309 /* Block the PHY control access. */
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00002310 tg3_phydsp_write(tp, 0x8005, 0x0800);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311
2312 err = tg3_phy_write_and_check_testpat(tp, &do_phy_reset);
2313 if (!err)
2314 break;
2315 } while (--retries);
2316
2317 err = tg3_phy_reset_chanpat(tp);
2318 if (err)
2319 return err;
2320
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00002321 tg3_phydsp_write(tp, 0x8005, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322
2323 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x8200);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002324 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325
Matt Carlson1d36ba42011-04-20 07:57:42 +00002326 TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327
Matt Carlson221c5632011-06-13 13:39:01 +00002328 tg3_writephy(tp, MII_CTRL1000, phy9_orig);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329
2330 if (!tg3_readphy(tp, MII_TG3_EXT_CTRL, &reg32)) {
2331 reg32 &= ~0x3000;
2332 tg3_writephy(tp, MII_TG3_EXT_CTRL, reg32);
2333 } else if (!err)
2334 err = -EBUSY;
2335
2336 return err;
2337}
2338
2339/* This will reset the tigon3 PHY if there is no valid
2340 * link unless the FORCE argument is non-zero.
2341 */
2342static int tg3_phy_reset(struct tg3 *tp)
2343{
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002344 u32 val, cpmuctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345 int err;
2346
Michael Chan60189dd2006-12-17 17:08:07 -08002347 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan60189dd2006-12-17 17:08:07 -08002348 val = tr32(GRC_MISC_CFG);
2349 tw32_f(GRC_MISC_CFG, val & ~GRC_MISC_CFG_EPHY_IDDQ);
2350 udelay(40);
2351 }
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002352 err = tg3_readphy(tp, MII_BMSR, &val);
2353 err |= tg3_readphy(tp, MII_BMSR, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354 if (err != 0)
2355 return -EBUSY;
2356
Michael Chanc8e1e822006-04-29 18:55:17 -07002357 if (netif_running(tp->dev) && netif_carrier_ok(tp->dev)) {
2358 netif_carrier_off(tp->dev);
2359 tg3_link_report(tp);
2360 }
2361
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
2363 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
2364 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
2365 err = tg3_phy_reset_5703_4_5(tp);
2366 if (err)
2367 return err;
2368 goto out;
2369 }
2370
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002371 cpmuctrl = 0;
2372 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
2373 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX) {
2374 cpmuctrl = tr32(TG3_CPMU_CTRL);
2375 if (cpmuctrl & CPMU_CTRL_GPHY_10MB_RXONLY)
2376 tw32(TG3_CPMU_CTRL,
2377 cpmuctrl & ~CPMU_CTRL_GPHY_10MB_RXONLY);
2378 }
2379
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380 err = tg3_bmcr_reset(tp);
2381 if (err)
2382 return err;
2383
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002384 if (cpmuctrl & CPMU_CTRL_GPHY_10MB_RXONLY) {
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002385 val = MII_TG3_DSP_EXP8_AEDW | MII_TG3_DSP_EXP8_REJ2MHz;
2386 tg3_phydsp_write(tp, MII_TG3_DSP_EXP8, val);
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002387
2388 tw32(TG3_CPMU_CTRL, cpmuctrl);
2389 }
2390
Matt Carlsonbcb37f62008-11-03 16:52:09 -08002391 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX ||
2392 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5761_AX) {
Matt Carlsonce057f02007-11-12 21:08:03 -08002393 val = tr32(TG3_CPMU_LSPD_1000MB_CLK);
2394 if ((val & CPMU_LSPD_1000MB_MACCLK_MASK) ==
2395 CPMU_LSPD_1000MB_MACCLK_12_5) {
2396 val &= ~CPMU_LSPD_1000MB_MACCLK_MASK;
2397 udelay(40);
2398 tw32_f(TG3_CPMU_LSPD_1000MB_CLK, val);
2399 }
2400 }
2401
Joe Perches63c3a662011-04-26 08:12:10 +00002402 if (tg3_flag(tp, 5717_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002403 (tp->phy_flags & TG3_PHYFLG_MII_SERDES))
Matt Carlsonecf14102010-01-20 16:58:05 +00002404 return 0;
2405
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002406 tg3_phy_apply_otp(tp);
2407
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002408 if (tp->phy_flags & TG3_PHYFLG_ENABLE_APD)
Matt Carlson6833c042008-11-21 17:18:59 -08002409 tg3_phy_toggle_apd(tp, true);
2410 else
2411 tg3_phy_toggle_apd(tp, false);
2412
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413out:
Matt Carlson1d36ba42011-04-20 07:57:42 +00002414 if ((tp->phy_flags & TG3_PHYFLG_ADC_BUG) &&
2415 !TG3_PHY_AUXCTL_SMDSP_ENABLE(tp)) {
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00002416 tg3_phydsp_write(tp, 0x201f, 0x2aaa);
2417 tg3_phydsp_write(tp, 0x000a, 0x0323);
Matt Carlson1d36ba42011-04-20 07:57:42 +00002418 TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419 }
Matt Carlson1d36ba42011-04-20 07:57:42 +00002420
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002421 if (tp->phy_flags & TG3_PHYFLG_5704_A0_BUG) {
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002422 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8d68);
2423 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8d68);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424 }
Matt Carlson1d36ba42011-04-20 07:57:42 +00002425
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002426 if (tp->phy_flags & TG3_PHYFLG_BER_BUG) {
Matt Carlson1d36ba42011-04-20 07:57:42 +00002427 if (!TG3_PHY_AUXCTL_SMDSP_ENABLE(tp)) {
2428 tg3_phydsp_write(tp, 0x000a, 0x310b);
2429 tg3_phydsp_write(tp, 0x201f, 0x9506);
2430 tg3_phydsp_write(tp, 0x401f, 0x14e2);
2431 TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
2432 }
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002433 } else if (tp->phy_flags & TG3_PHYFLG_JITTER_BUG) {
Matt Carlson1d36ba42011-04-20 07:57:42 +00002434 if (!TG3_PHY_AUXCTL_SMDSP_ENABLE(tp)) {
2435 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x000a);
2436 if (tp->phy_flags & TG3_PHYFLG_ADJUST_TRIM) {
2437 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x110b);
2438 tg3_writephy(tp, MII_TG3_TEST1,
2439 MII_TG3_TEST1_TRIM_EN | 0x4);
2440 } else
2441 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x010b);
2442
2443 TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
2444 }
Michael Chanc424cb22006-04-29 18:56:34 -07002445 }
Matt Carlson1d36ba42011-04-20 07:57:42 +00002446
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447 /* Set Extended packet length bit (bit 14) on all chips that */
2448 /* support jumbo frames */
Matt Carlson79eb6902010-02-17 15:17:03 +00002449 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450 /* Cannot do read-modify-write on 5401 */
Matt Carlsonb4bd2922011-04-20 07:57:41 +00002451 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_AUXCTL, 0x4c20);
Joe Perches63c3a662011-04-26 08:12:10 +00002452 } else if (tg3_flag(tp, JUMBO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453 /* Set bit 14 with read-modify-write to preserve other bits */
Matt Carlson15ee95c2011-04-20 07:57:40 +00002454 err = tg3_phy_auxctl_read(tp,
2455 MII_TG3_AUXCTL_SHDWSEL_AUXCTL, &val);
2456 if (!err)
Matt Carlsonb4bd2922011-04-20 07:57:41 +00002457 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_AUXCTL,
2458 val | MII_TG3_AUXCTL_ACTL_EXTPKTLEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459 }
2460
2461 /* Set phy register 0x10 bit 0 to high fifo elasticity to support
2462 * jumbo frames transmission.
2463 */
Joe Perches63c3a662011-04-26 08:12:10 +00002464 if (tg3_flag(tp, JUMBO_CAPABLE)) {
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002465 if (!tg3_readphy(tp, MII_TG3_EXT_CTRL, &val))
Matt Carlsonc6cdf432010-04-05 10:19:26 +00002466 tg3_writephy(tp, MII_TG3_EXT_CTRL,
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002467 val | MII_TG3_EXT_CTRL_FIFO_ELASTIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468 }
2469
Michael Chan715116a2006-09-27 16:09:25 -07002470 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan715116a2006-09-27 16:09:25 -07002471 /* adjust output voltage */
Matt Carlson535ef6e2009-08-25 10:09:36 +00002472 tg3_writephy(tp, MII_TG3_FET_PTEST, 0x12);
Michael Chan715116a2006-09-27 16:09:25 -07002473 }
2474
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002475 tg3_phy_toggle_automdix(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476 tg3_phy_set_wirespeed(tp);
2477 return 0;
2478}
2479
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002480#define TG3_GPIO_MSG_DRVR_PRES 0x00000001
2481#define TG3_GPIO_MSG_NEED_VAUX 0x00000002
2482#define TG3_GPIO_MSG_MASK (TG3_GPIO_MSG_DRVR_PRES | \
2483 TG3_GPIO_MSG_NEED_VAUX)
2484#define TG3_GPIO_MSG_ALL_DRVR_PRES_MASK \
2485 ((TG3_GPIO_MSG_DRVR_PRES << 0) | \
2486 (TG3_GPIO_MSG_DRVR_PRES << 4) | \
2487 (TG3_GPIO_MSG_DRVR_PRES << 8) | \
2488 (TG3_GPIO_MSG_DRVR_PRES << 12))
2489
2490#define TG3_GPIO_MSG_ALL_NEED_VAUX_MASK \
2491 ((TG3_GPIO_MSG_NEED_VAUX << 0) | \
2492 (TG3_GPIO_MSG_NEED_VAUX << 4) | \
2493 (TG3_GPIO_MSG_NEED_VAUX << 8) | \
2494 (TG3_GPIO_MSG_NEED_VAUX << 12))
2495
2496static inline u32 tg3_set_function_status(struct tg3 *tp, u32 newstat)
2497{
2498 u32 status, shift;
2499
2500 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
2501 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
2502 status = tg3_ape_read32(tp, TG3_APE_GPIO_MSG);
2503 else
2504 status = tr32(TG3_CPMU_DRV_STATUS);
2505
2506 shift = TG3_APE_GPIO_MSG_SHIFT + 4 * tp->pci_fn;
2507 status &= ~(TG3_GPIO_MSG_MASK << shift);
2508 status |= (newstat << shift);
2509
2510 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
2511 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
2512 tg3_ape_write32(tp, TG3_APE_GPIO_MSG, status);
2513 else
2514 tw32(TG3_CPMU_DRV_STATUS, status);
2515
2516 return status >> TG3_APE_GPIO_MSG_SHIFT;
2517}
2518
Matt Carlson520b2752011-06-13 13:39:02 +00002519static inline int tg3_pwrsrc_switch_to_vmain(struct tg3 *tp)
2520{
2521 if (!tg3_flag(tp, IS_NIC))
2522 return 0;
2523
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002524 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
2525 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
2526 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
2527 if (tg3_ape_lock(tp, TG3_APE_LOCK_GPIO))
2528 return -EIO;
Matt Carlson520b2752011-06-13 13:39:02 +00002529
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002530 tg3_set_function_status(tp, TG3_GPIO_MSG_DRVR_PRES);
2531
2532 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl,
2533 TG3_GRC_LCLCTL_PWRSW_DELAY);
2534
2535 tg3_ape_unlock(tp, TG3_APE_LOCK_GPIO);
2536 } else {
2537 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl,
2538 TG3_GRC_LCLCTL_PWRSW_DELAY);
2539 }
Matt Carlson6f5c8f832011-07-13 09:27:31 +00002540
Matt Carlson520b2752011-06-13 13:39:02 +00002541 return 0;
2542}
2543
2544static void tg3_pwrsrc_die_with_vmain(struct tg3 *tp)
2545{
2546 u32 grc_local_ctrl;
2547
2548 if (!tg3_flag(tp, IS_NIC) ||
2549 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2550 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)
2551 return;
2552
2553 grc_local_ctrl = tp->grc_local_ctrl | GRC_LCLCTRL_GPIO_OE1;
2554
2555 tw32_wait_f(GRC_LOCAL_CTRL,
2556 grc_local_ctrl | GRC_LCLCTRL_GPIO_OUTPUT1,
2557 TG3_GRC_LCLCTL_PWRSW_DELAY);
2558
2559 tw32_wait_f(GRC_LOCAL_CTRL,
2560 grc_local_ctrl,
2561 TG3_GRC_LCLCTL_PWRSW_DELAY);
2562
2563 tw32_wait_f(GRC_LOCAL_CTRL,
2564 grc_local_ctrl | GRC_LCLCTRL_GPIO_OUTPUT1,
2565 TG3_GRC_LCLCTL_PWRSW_DELAY);
2566}
2567
2568static void tg3_pwrsrc_switch_to_vaux(struct tg3 *tp)
2569{
2570 if (!tg3_flag(tp, IS_NIC))
2571 return;
2572
2573 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2574 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
2575 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2576 (GRC_LCLCTRL_GPIO_OE0 |
2577 GRC_LCLCTRL_GPIO_OE1 |
2578 GRC_LCLCTRL_GPIO_OE2 |
2579 GRC_LCLCTRL_GPIO_OUTPUT0 |
2580 GRC_LCLCTRL_GPIO_OUTPUT1),
2581 TG3_GRC_LCLCTL_PWRSW_DELAY);
2582 } else if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761 ||
2583 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761S) {
2584 /* The 5761 non-e device swaps GPIO 0 and GPIO 2. */
2585 u32 grc_local_ctrl = GRC_LCLCTRL_GPIO_OE0 |
2586 GRC_LCLCTRL_GPIO_OE1 |
2587 GRC_LCLCTRL_GPIO_OE2 |
2588 GRC_LCLCTRL_GPIO_OUTPUT0 |
2589 GRC_LCLCTRL_GPIO_OUTPUT1 |
2590 tp->grc_local_ctrl;
2591 tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl,
2592 TG3_GRC_LCLCTL_PWRSW_DELAY);
2593
2594 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OUTPUT2;
2595 tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl,
2596 TG3_GRC_LCLCTL_PWRSW_DELAY);
2597
2598 grc_local_ctrl &= ~GRC_LCLCTRL_GPIO_OUTPUT0;
2599 tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl,
2600 TG3_GRC_LCLCTL_PWRSW_DELAY);
2601 } else {
2602 u32 no_gpio2;
2603 u32 grc_local_ctrl = 0;
2604
2605 /* Workaround to prevent overdrawing Amps. */
2606 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
2607 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE3;
2608 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2609 grc_local_ctrl,
2610 TG3_GRC_LCLCTL_PWRSW_DELAY);
2611 }
2612
2613 /* On 5753 and variants, GPIO2 cannot be used. */
2614 no_gpio2 = tp->nic_sram_data_cfg &
2615 NIC_SRAM_DATA_CFG_NO_GPIO2;
2616
2617 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE0 |
2618 GRC_LCLCTRL_GPIO_OE1 |
2619 GRC_LCLCTRL_GPIO_OE2 |
2620 GRC_LCLCTRL_GPIO_OUTPUT1 |
2621 GRC_LCLCTRL_GPIO_OUTPUT2;
2622 if (no_gpio2) {
2623 grc_local_ctrl &= ~(GRC_LCLCTRL_GPIO_OE2 |
2624 GRC_LCLCTRL_GPIO_OUTPUT2);
2625 }
2626 tw32_wait_f(GRC_LOCAL_CTRL,
2627 tp->grc_local_ctrl | grc_local_ctrl,
2628 TG3_GRC_LCLCTL_PWRSW_DELAY);
2629
2630 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OUTPUT0;
2631
2632 tw32_wait_f(GRC_LOCAL_CTRL,
2633 tp->grc_local_ctrl | grc_local_ctrl,
2634 TG3_GRC_LCLCTL_PWRSW_DELAY);
2635
2636 if (!no_gpio2) {
2637 grc_local_ctrl &= ~GRC_LCLCTRL_GPIO_OUTPUT2;
2638 tw32_wait_f(GRC_LOCAL_CTRL,
2639 tp->grc_local_ctrl | grc_local_ctrl,
2640 TG3_GRC_LCLCTL_PWRSW_DELAY);
2641 }
2642 }
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002643}
Matt Carlson6f5c8f832011-07-13 09:27:31 +00002644
Matt Carlsoncd0d7222011-07-13 09:27:33 +00002645static void tg3_frob_aux_power_5717(struct tg3 *tp, bool wol_enable)
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002646{
2647 u32 msg = 0;
2648
2649 /* Serialize power state transitions */
2650 if (tg3_ape_lock(tp, TG3_APE_LOCK_GPIO))
2651 return;
2652
Matt Carlsoncd0d7222011-07-13 09:27:33 +00002653 if (tg3_flag(tp, ENABLE_ASF) || tg3_flag(tp, ENABLE_APE) || wol_enable)
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002654 msg = TG3_GPIO_MSG_NEED_VAUX;
2655
2656 msg = tg3_set_function_status(tp, msg);
2657
2658 if (msg & TG3_GPIO_MSG_ALL_DRVR_PRES_MASK)
2659 goto done;
2660
2661 if (msg & TG3_GPIO_MSG_ALL_NEED_VAUX_MASK)
2662 tg3_pwrsrc_switch_to_vaux(tp);
2663 else
2664 tg3_pwrsrc_die_with_vmain(tp);
2665
2666done:
Matt Carlson6f5c8f832011-07-13 09:27:31 +00002667 tg3_ape_unlock(tp, TG3_APE_LOCK_GPIO);
Matt Carlson520b2752011-06-13 13:39:02 +00002668}
2669
Matt Carlsoncd0d7222011-07-13 09:27:33 +00002670static void tg3_frob_aux_power(struct tg3 *tp, bool include_wol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002671{
Matt Carlson683644b2011-03-09 16:58:23 +00002672 bool need_vaux = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002673
Matt Carlson334355a2010-01-20 16:58:10 +00002674 /* The GPIOs do something completely different on 57765. */
Matt Carlson55086ad2011-12-14 11:09:59 +00002675 if (!tg3_flag(tp, IS_NIC) || tg3_flag(tp, 57765_CLASS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002676 return;
2677
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002678 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
2679 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
2680 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
Matt Carlsoncd0d7222011-07-13 09:27:33 +00002681 tg3_frob_aux_power_5717(tp, include_wol ?
2682 tg3_flag(tp, WOL_ENABLE) != 0 : 0);
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002683 return;
2684 }
2685
2686 if (tp->pdev_peer && tp->pdev_peer != tp->pdev) {
Michael Chan8c2dc7e2005-12-19 16:26:02 -08002687 struct net_device *dev_peer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002688
Michael Chan8c2dc7e2005-12-19 16:26:02 -08002689 dev_peer = pci_get_drvdata(tp->pdev_peer);
Matt Carlson683644b2011-03-09 16:58:23 +00002690
Michael Chanbc1c7562006-03-20 17:48:03 -08002691 /* remove_one() may have been run on the peer. */
Matt Carlson683644b2011-03-09 16:58:23 +00002692 if (dev_peer) {
2693 struct tg3 *tp_peer = netdev_priv(dev_peer);
2694
Joe Perches63c3a662011-04-26 08:12:10 +00002695 if (tg3_flag(tp_peer, INIT_COMPLETE))
Matt Carlson683644b2011-03-09 16:58:23 +00002696 return;
2697
Matt Carlsoncd0d7222011-07-13 09:27:33 +00002698 if ((include_wol && tg3_flag(tp_peer, WOL_ENABLE)) ||
Joe Perches63c3a662011-04-26 08:12:10 +00002699 tg3_flag(tp_peer, ENABLE_ASF))
Matt Carlson683644b2011-03-09 16:58:23 +00002700 need_vaux = true;
2701 }
Michael Chan8c2dc7e2005-12-19 16:26:02 -08002702 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002703
Matt Carlsoncd0d7222011-07-13 09:27:33 +00002704 if ((include_wol && tg3_flag(tp, WOL_ENABLE)) ||
2705 tg3_flag(tp, ENABLE_ASF))
Matt Carlson683644b2011-03-09 16:58:23 +00002706 need_vaux = true;
2707
Matt Carlson520b2752011-06-13 13:39:02 +00002708 if (need_vaux)
2709 tg3_pwrsrc_switch_to_vaux(tp);
2710 else
2711 tg3_pwrsrc_die_with_vmain(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002712}
2713
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07002714static int tg3_5700_link_polarity(struct tg3 *tp, u32 speed)
2715{
2716 if (tp->led_ctrl == LED_CTRL_MODE_PHY_2)
2717 return 1;
Matt Carlson79eb6902010-02-17 15:17:03 +00002718 else if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5411) {
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07002719 if (speed != SPEED_10)
2720 return 1;
2721 } else if (speed == SPEED_10)
2722 return 1;
2723
2724 return 0;
2725}
2726
Matt Carlson0a459aa2008-11-03 16:54:15 -08002727static void tg3_power_down_phy(struct tg3 *tp, bool do_low_power)
Michael Chan15c3b692006-03-22 01:06:52 -08002728{
Matt Carlsonce057f02007-11-12 21:08:03 -08002729 u32 val;
2730
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002731 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Michael Chan51297242007-02-13 12:17:57 -08002732 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
2733 u32 sg_dig_ctrl = tr32(SG_DIG_CTRL);
2734 u32 serdes_cfg = tr32(MAC_SERDES_CFG);
2735
2736 sg_dig_ctrl |=
2737 SG_DIG_USING_HW_AUTONEG | SG_DIG_SOFT_RESET;
2738 tw32(SG_DIG_CTRL, sg_dig_ctrl);
2739 tw32(MAC_SERDES_CFG, serdes_cfg | (1 << 15));
2740 }
Michael Chan3f7045c2006-09-27 16:02:29 -07002741 return;
Michael Chan51297242007-02-13 12:17:57 -08002742 }
Michael Chan3f7045c2006-09-27 16:02:29 -07002743
Michael Chan60189dd2006-12-17 17:08:07 -08002744 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan60189dd2006-12-17 17:08:07 -08002745 tg3_bmcr_reset(tp);
2746 val = tr32(GRC_MISC_CFG);
2747 tw32_f(GRC_MISC_CFG, val | GRC_MISC_CFG_EPHY_IDDQ);
2748 udelay(40);
2749 return;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002750 } else if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Matt Carlson0e5f7842009-11-02 14:26:38 +00002751 u32 phytest;
2752 if (!tg3_readphy(tp, MII_TG3_FET_TEST, &phytest)) {
2753 u32 phy;
2754
2755 tg3_writephy(tp, MII_ADVERTISE, 0);
2756 tg3_writephy(tp, MII_BMCR,
2757 BMCR_ANENABLE | BMCR_ANRESTART);
2758
2759 tg3_writephy(tp, MII_TG3_FET_TEST,
2760 phytest | MII_TG3_FET_SHADOW_EN);
2761 if (!tg3_readphy(tp, MII_TG3_FET_SHDW_AUXMODE4, &phy)) {
2762 phy |= MII_TG3_FET_SHDW_AUXMODE4_SBPD;
2763 tg3_writephy(tp,
2764 MII_TG3_FET_SHDW_AUXMODE4,
2765 phy);
2766 }
2767 tg3_writephy(tp, MII_TG3_FET_TEST, phytest);
2768 }
2769 return;
Matt Carlson0a459aa2008-11-03 16:54:15 -08002770 } else if (do_low_power) {
Michael Chan715116a2006-09-27 16:09:25 -07002771 tg3_writephy(tp, MII_TG3_EXT_CTRL,
2772 MII_TG3_EXT_CTRL_FORCE_LED_OFF);
Matt Carlson0a459aa2008-11-03 16:54:15 -08002773
Matt Carlsonb4bd2922011-04-20 07:57:41 +00002774 val = MII_TG3_AUXCTL_PCTL_100TX_LPWR |
2775 MII_TG3_AUXCTL_PCTL_SPR_ISOLATE |
2776 MII_TG3_AUXCTL_PCTL_VREG_11V;
2777 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_PWRCTL, val);
Michael Chan715116a2006-09-27 16:09:25 -07002778 }
Michael Chan3f7045c2006-09-27 16:02:29 -07002779
Michael Chan15c3b692006-03-22 01:06:52 -08002780 /* The PHY should not be powered down on some chips because
2781 * of bugs.
2782 */
2783 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2784 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
2785 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780 &&
Matt Carlson085f1af2012-04-02 09:01:40 +00002786 (tp->phy_flags & TG3_PHYFLG_MII_SERDES)) ||
2787 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 &&
2788 !tp->pci_fn))
Michael Chan15c3b692006-03-22 01:06:52 -08002789 return;
Matt Carlsonce057f02007-11-12 21:08:03 -08002790
Matt Carlsonbcb37f62008-11-03 16:52:09 -08002791 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX ||
2792 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5761_AX) {
Matt Carlsonce057f02007-11-12 21:08:03 -08002793 val = tr32(TG3_CPMU_LSPD_1000MB_CLK);
2794 val &= ~CPMU_LSPD_1000MB_MACCLK_MASK;
2795 val |= CPMU_LSPD_1000MB_MACCLK_12_5;
2796 tw32_f(TG3_CPMU_LSPD_1000MB_CLK, val);
2797 }
2798
Michael Chan15c3b692006-03-22 01:06:52 -08002799 tg3_writephy(tp, MII_BMCR, BMCR_PDOWN);
2800}
2801
Matt Carlson3f007892008-11-03 16:51:36 -08002802/* tp->lock is held. */
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002803static int tg3_nvram_lock(struct tg3 *tp)
2804{
Joe Perches63c3a662011-04-26 08:12:10 +00002805 if (tg3_flag(tp, NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002806 int i;
2807
2808 if (tp->nvram_lock_cnt == 0) {
2809 tw32(NVRAM_SWARB, SWARB_REQ_SET1);
2810 for (i = 0; i < 8000; i++) {
2811 if (tr32(NVRAM_SWARB) & SWARB_GNT1)
2812 break;
2813 udelay(20);
2814 }
2815 if (i == 8000) {
2816 tw32(NVRAM_SWARB, SWARB_REQ_CLR1);
2817 return -ENODEV;
2818 }
2819 }
2820 tp->nvram_lock_cnt++;
2821 }
2822 return 0;
2823}
2824
2825/* tp->lock is held. */
2826static void tg3_nvram_unlock(struct tg3 *tp)
2827{
Joe Perches63c3a662011-04-26 08:12:10 +00002828 if (tg3_flag(tp, NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002829 if (tp->nvram_lock_cnt > 0)
2830 tp->nvram_lock_cnt--;
2831 if (tp->nvram_lock_cnt == 0)
2832 tw32_f(NVRAM_SWARB, SWARB_REQ_CLR1);
2833 }
2834}
2835
2836/* tp->lock is held. */
2837static void tg3_enable_nvram_access(struct tg3 *tp)
2838{
Joe Perches63c3a662011-04-26 08:12:10 +00002839 if (tg3_flag(tp, 5750_PLUS) && !tg3_flag(tp, PROTECTED_NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002840 u32 nvaccess = tr32(NVRAM_ACCESS);
2841
2842 tw32(NVRAM_ACCESS, nvaccess | ACCESS_ENABLE);
2843 }
2844}
2845
2846/* tp->lock is held. */
2847static void tg3_disable_nvram_access(struct tg3 *tp)
2848{
Joe Perches63c3a662011-04-26 08:12:10 +00002849 if (tg3_flag(tp, 5750_PLUS) && !tg3_flag(tp, PROTECTED_NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002850 u32 nvaccess = tr32(NVRAM_ACCESS);
2851
2852 tw32(NVRAM_ACCESS, nvaccess & ~ACCESS_ENABLE);
2853 }
2854}
2855
2856static int tg3_nvram_read_using_eeprom(struct tg3 *tp,
2857 u32 offset, u32 *val)
2858{
2859 u32 tmp;
2860 int i;
2861
2862 if (offset > EEPROM_ADDR_ADDR_MASK || (offset % 4) != 0)
2863 return -EINVAL;
2864
2865 tmp = tr32(GRC_EEPROM_ADDR) & ~(EEPROM_ADDR_ADDR_MASK |
2866 EEPROM_ADDR_DEVID_MASK |
2867 EEPROM_ADDR_READ);
2868 tw32(GRC_EEPROM_ADDR,
2869 tmp |
2870 (0 << EEPROM_ADDR_DEVID_SHIFT) |
2871 ((offset << EEPROM_ADDR_ADDR_SHIFT) &
2872 EEPROM_ADDR_ADDR_MASK) |
2873 EEPROM_ADDR_READ | EEPROM_ADDR_START);
2874
2875 for (i = 0; i < 1000; i++) {
2876 tmp = tr32(GRC_EEPROM_ADDR);
2877
2878 if (tmp & EEPROM_ADDR_COMPLETE)
2879 break;
2880 msleep(1);
2881 }
2882 if (!(tmp & EEPROM_ADDR_COMPLETE))
2883 return -EBUSY;
2884
Matt Carlson62cedd12009-04-20 14:52:29 -07002885 tmp = tr32(GRC_EEPROM_DATA);
2886
2887 /*
2888 * The data will always be opposite the native endian
2889 * format. Perform a blind byteswap to compensate.
2890 */
2891 *val = swab32(tmp);
2892
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002893 return 0;
2894}
2895
2896#define NVRAM_CMD_TIMEOUT 10000
2897
2898static int tg3_nvram_exec_cmd(struct tg3 *tp, u32 nvram_cmd)
2899{
2900 int i;
2901
2902 tw32(NVRAM_CMD, nvram_cmd);
2903 for (i = 0; i < NVRAM_CMD_TIMEOUT; i++) {
2904 udelay(10);
2905 if (tr32(NVRAM_CMD) & NVRAM_CMD_DONE) {
2906 udelay(10);
2907 break;
2908 }
2909 }
2910
2911 if (i == NVRAM_CMD_TIMEOUT)
2912 return -EBUSY;
2913
2914 return 0;
2915}
2916
2917static u32 tg3_nvram_phys_addr(struct tg3 *tp, u32 addr)
2918{
Joe Perches63c3a662011-04-26 08:12:10 +00002919 if (tg3_flag(tp, NVRAM) &&
2920 tg3_flag(tp, NVRAM_BUFFERED) &&
2921 tg3_flag(tp, FLASH) &&
2922 !tg3_flag(tp, NO_NVRAM_ADDR_TRANS) &&
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002923 (tp->nvram_jedecnum == JEDEC_ATMEL))
2924
2925 addr = ((addr / tp->nvram_pagesize) <<
2926 ATMEL_AT45DB0X1B_PAGE_POS) +
2927 (addr % tp->nvram_pagesize);
2928
2929 return addr;
2930}
2931
2932static u32 tg3_nvram_logical_addr(struct tg3 *tp, u32 addr)
2933{
Joe Perches63c3a662011-04-26 08:12:10 +00002934 if (tg3_flag(tp, NVRAM) &&
2935 tg3_flag(tp, NVRAM_BUFFERED) &&
2936 tg3_flag(tp, FLASH) &&
2937 !tg3_flag(tp, NO_NVRAM_ADDR_TRANS) &&
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002938 (tp->nvram_jedecnum == JEDEC_ATMEL))
2939
2940 addr = ((addr >> ATMEL_AT45DB0X1B_PAGE_POS) *
2941 tp->nvram_pagesize) +
2942 (addr & ((1 << ATMEL_AT45DB0X1B_PAGE_POS) - 1));
2943
2944 return addr;
2945}
2946
Matt Carlsone4f34112009-02-25 14:25:00 +00002947/* NOTE: Data read in from NVRAM is byteswapped according to
2948 * the byteswapping settings for all other register accesses.
2949 * tg3 devices are BE devices, so on a BE machine, the data
2950 * returned will be exactly as it is seen in NVRAM. On a LE
2951 * machine, the 32-bit value will be byteswapped.
2952 */
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002953static int tg3_nvram_read(struct tg3 *tp, u32 offset, u32 *val)
2954{
2955 int ret;
2956
Joe Perches63c3a662011-04-26 08:12:10 +00002957 if (!tg3_flag(tp, NVRAM))
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002958 return tg3_nvram_read_using_eeprom(tp, offset, val);
2959
2960 offset = tg3_nvram_phys_addr(tp, offset);
2961
2962 if (offset > NVRAM_ADDR_MSK)
2963 return -EINVAL;
2964
2965 ret = tg3_nvram_lock(tp);
2966 if (ret)
2967 return ret;
2968
2969 tg3_enable_nvram_access(tp);
2970
2971 tw32(NVRAM_ADDR, offset);
2972 ret = tg3_nvram_exec_cmd(tp, NVRAM_CMD_RD | NVRAM_CMD_GO |
2973 NVRAM_CMD_FIRST | NVRAM_CMD_LAST | NVRAM_CMD_DONE);
2974
2975 if (ret == 0)
Matt Carlsone4f34112009-02-25 14:25:00 +00002976 *val = tr32(NVRAM_RDDATA);
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002977
2978 tg3_disable_nvram_access(tp);
2979
2980 tg3_nvram_unlock(tp);
2981
2982 return ret;
2983}
2984
Matt Carlsona9dc5292009-02-25 14:25:30 +00002985/* Ensures NVRAM data is in bytestream format. */
2986static int tg3_nvram_read_be32(struct tg3 *tp, u32 offset, __be32 *val)
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002987{
2988 u32 v;
Matt Carlsona9dc5292009-02-25 14:25:30 +00002989 int res = tg3_nvram_read(tp, offset, &v);
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002990 if (!res)
Matt Carlsona9dc5292009-02-25 14:25:30 +00002991 *val = cpu_to_be32(v);
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002992 return res;
2993}
2994
Matt Carlsondbe9b922012-02-13 10:20:09 +00002995static int tg3_nvram_write_block_using_eeprom(struct tg3 *tp,
2996 u32 offset, u32 len, u8 *buf)
2997{
2998 int i, j, rc = 0;
2999 u32 val;
3000
3001 for (i = 0; i < len; i += 4) {
3002 u32 addr;
3003 __be32 data;
3004
3005 addr = offset + i;
3006
3007 memcpy(&data, buf + i, 4);
3008
3009 /*
3010 * The SEEPROM interface expects the data to always be opposite
3011 * the native endian format. We accomplish this by reversing
3012 * all the operations that would have been performed on the
3013 * data from a call to tg3_nvram_read_be32().
3014 */
3015 tw32(GRC_EEPROM_DATA, swab32(be32_to_cpu(data)));
3016
3017 val = tr32(GRC_EEPROM_ADDR);
3018 tw32(GRC_EEPROM_ADDR, val | EEPROM_ADDR_COMPLETE);
3019
3020 val &= ~(EEPROM_ADDR_ADDR_MASK | EEPROM_ADDR_DEVID_MASK |
3021 EEPROM_ADDR_READ);
3022 tw32(GRC_EEPROM_ADDR, val |
3023 (0 << EEPROM_ADDR_DEVID_SHIFT) |
3024 (addr & EEPROM_ADDR_ADDR_MASK) |
3025 EEPROM_ADDR_START |
3026 EEPROM_ADDR_WRITE);
3027
3028 for (j = 0; j < 1000; j++) {
3029 val = tr32(GRC_EEPROM_ADDR);
3030
3031 if (val & EEPROM_ADDR_COMPLETE)
3032 break;
3033 msleep(1);
3034 }
3035 if (!(val & EEPROM_ADDR_COMPLETE)) {
3036 rc = -EBUSY;
3037 break;
3038 }
3039 }
3040
3041 return rc;
3042}
3043
3044/* offset and length are dword aligned */
3045static int tg3_nvram_write_block_unbuffered(struct tg3 *tp, u32 offset, u32 len,
3046 u8 *buf)
3047{
3048 int ret = 0;
3049 u32 pagesize = tp->nvram_pagesize;
3050 u32 pagemask = pagesize - 1;
3051 u32 nvram_cmd;
3052 u8 *tmp;
3053
3054 tmp = kmalloc(pagesize, GFP_KERNEL);
3055 if (tmp == NULL)
3056 return -ENOMEM;
3057
3058 while (len) {
3059 int j;
3060 u32 phy_addr, page_off, size;
3061
3062 phy_addr = offset & ~pagemask;
3063
3064 for (j = 0; j < pagesize; j += 4) {
3065 ret = tg3_nvram_read_be32(tp, phy_addr + j,
3066 (__be32 *) (tmp + j));
3067 if (ret)
3068 break;
3069 }
3070 if (ret)
3071 break;
3072
3073 page_off = offset & pagemask;
3074 size = pagesize;
3075 if (len < size)
3076 size = len;
3077
3078 len -= size;
3079
3080 memcpy(tmp + page_off, buf, size);
3081
3082 offset = offset + (pagesize - page_off);
3083
3084 tg3_enable_nvram_access(tp);
3085
3086 /*
3087 * Before we can erase the flash page, we need
3088 * to issue a special "write enable" command.
3089 */
3090 nvram_cmd = NVRAM_CMD_WREN | NVRAM_CMD_GO | NVRAM_CMD_DONE;
3091
3092 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
3093 break;
3094
3095 /* Erase the target page */
3096 tw32(NVRAM_ADDR, phy_addr);
3097
3098 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE | NVRAM_CMD_WR |
3099 NVRAM_CMD_FIRST | NVRAM_CMD_LAST | NVRAM_CMD_ERASE;
3100
3101 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
3102 break;
3103
3104 /* Issue another write enable to start the write. */
3105 nvram_cmd = NVRAM_CMD_WREN | NVRAM_CMD_GO | NVRAM_CMD_DONE;
3106
3107 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
3108 break;
3109
3110 for (j = 0; j < pagesize; j += 4) {
3111 __be32 data;
3112
3113 data = *((__be32 *) (tmp + j));
3114
3115 tw32(NVRAM_WRDATA, be32_to_cpu(data));
3116
3117 tw32(NVRAM_ADDR, phy_addr + j);
3118
3119 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE |
3120 NVRAM_CMD_WR;
3121
3122 if (j == 0)
3123 nvram_cmd |= NVRAM_CMD_FIRST;
3124 else if (j == (pagesize - 4))
3125 nvram_cmd |= NVRAM_CMD_LAST;
3126
3127 ret = tg3_nvram_exec_cmd(tp, nvram_cmd);
3128 if (ret)
3129 break;
3130 }
3131 if (ret)
3132 break;
3133 }
3134
3135 nvram_cmd = NVRAM_CMD_WRDI | NVRAM_CMD_GO | NVRAM_CMD_DONE;
3136 tg3_nvram_exec_cmd(tp, nvram_cmd);
3137
3138 kfree(tmp);
3139
3140 return ret;
3141}
3142
3143/* offset and length are dword aligned */
3144static int tg3_nvram_write_block_buffered(struct tg3 *tp, u32 offset, u32 len,
3145 u8 *buf)
3146{
3147 int i, ret = 0;
3148
3149 for (i = 0; i < len; i += 4, offset += 4) {
3150 u32 page_off, phy_addr, nvram_cmd;
3151 __be32 data;
3152
3153 memcpy(&data, buf + i, 4);
3154 tw32(NVRAM_WRDATA, be32_to_cpu(data));
3155
3156 page_off = offset % tp->nvram_pagesize;
3157
3158 phy_addr = tg3_nvram_phys_addr(tp, offset);
3159
Matt Carlsondbe9b922012-02-13 10:20:09 +00003160 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE | NVRAM_CMD_WR;
3161
3162 if (page_off == 0 || i == 0)
3163 nvram_cmd |= NVRAM_CMD_FIRST;
3164 if (page_off == (tp->nvram_pagesize - 4))
3165 nvram_cmd |= NVRAM_CMD_LAST;
3166
3167 if (i == (len - 4))
3168 nvram_cmd |= NVRAM_CMD_LAST;
3169
Matt Carlson42278222012-02-13 15:20:11 +00003170 if ((nvram_cmd & NVRAM_CMD_FIRST) ||
3171 !tg3_flag(tp, FLASH) ||
3172 !tg3_flag(tp, 57765_PLUS))
3173 tw32(NVRAM_ADDR, phy_addr);
3174
Matt Carlsondbe9b922012-02-13 10:20:09 +00003175 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5752 &&
3176 !tg3_flag(tp, 5755_PLUS) &&
3177 (tp->nvram_jedecnum == JEDEC_ST) &&
3178 (nvram_cmd & NVRAM_CMD_FIRST)) {
3179 u32 cmd;
3180
3181 cmd = NVRAM_CMD_WREN | NVRAM_CMD_GO | NVRAM_CMD_DONE;
3182 ret = tg3_nvram_exec_cmd(tp, cmd);
3183 if (ret)
3184 break;
3185 }
3186 if (!tg3_flag(tp, FLASH)) {
3187 /* We always do complete word writes to eeprom. */
3188 nvram_cmd |= (NVRAM_CMD_FIRST | NVRAM_CMD_LAST);
3189 }
3190
3191 ret = tg3_nvram_exec_cmd(tp, nvram_cmd);
3192 if (ret)
3193 break;
3194 }
3195 return ret;
3196}
3197
3198/* offset and length are dword aligned */
3199static int tg3_nvram_write_block(struct tg3 *tp, u32 offset, u32 len, u8 *buf)
3200{
3201 int ret;
3202
3203 if (tg3_flag(tp, EEPROM_WRITE_PROT)) {
3204 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl &
3205 ~GRC_LCLCTRL_GPIO_OUTPUT1);
3206 udelay(40);
3207 }
3208
3209 if (!tg3_flag(tp, NVRAM)) {
3210 ret = tg3_nvram_write_block_using_eeprom(tp, offset, len, buf);
3211 } else {
3212 u32 grc_mode;
3213
3214 ret = tg3_nvram_lock(tp);
3215 if (ret)
3216 return ret;
3217
3218 tg3_enable_nvram_access(tp);
3219 if (tg3_flag(tp, 5750_PLUS) && !tg3_flag(tp, PROTECTED_NVRAM))
3220 tw32(NVRAM_WRITE1, 0x406);
3221
3222 grc_mode = tr32(GRC_MODE);
3223 tw32(GRC_MODE, grc_mode | GRC_MODE_NVRAM_WR_ENABLE);
3224
3225 if (tg3_flag(tp, NVRAM_BUFFERED) || !tg3_flag(tp, FLASH)) {
3226 ret = tg3_nvram_write_block_buffered(tp, offset, len,
3227 buf);
3228 } else {
3229 ret = tg3_nvram_write_block_unbuffered(tp, offset, len,
3230 buf);
3231 }
3232
3233 grc_mode = tr32(GRC_MODE);
3234 tw32(GRC_MODE, grc_mode & ~GRC_MODE_NVRAM_WR_ENABLE);
3235
3236 tg3_disable_nvram_access(tp);
3237 tg3_nvram_unlock(tp);
3238 }
3239
3240 if (tg3_flag(tp, EEPROM_WRITE_PROT)) {
3241 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
3242 udelay(40);
3243 }
3244
3245 return ret;
3246}
3247
Matt Carlson997b4f12011-08-31 11:44:53 +00003248#define RX_CPU_SCRATCH_BASE 0x30000
3249#define RX_CPU_SCRATCH_SIZE 0x04000
3250#define TX_CPU_SCRATCH_BASE 0x34000
3251#define TX_CPU_SCRATCH_SIZE 0x04000
3252
3253/* tp->lock is held. */
3254static int tg3_halt_cpu(struct tg3 *tp, u32 offset)
3255{
3256 int i;
3257
3258 BUG_ON(offset == TX_CPU_BASE && tg3_flag(tp, 5705_PLUS));
3259
3260 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
3261 u32 val = tr32(GRC_VCPU_EXT_CTRL);
3262
3263 tw32(GRC_VCPU_EXT_CTRL, val | GRC_VCPU_EXT_CTRL_HALT_CPU);
3264 return 0;
3265 }
3266 if (offset == RX_CPU_BASE) {
3267 for (i = 0; i < 10000; i++) {
3268 tw32(offset + CPU_STATE, 0xffffffff);
3269 tw32(offset + CPU_MODE, CPU_MODE_HALT);
3270 if (tr32(offset + CPU_MODE) & CPU_MODE_HALT)
3271 break;
3272 }
3273
3274 tw32(offset + CPU_STATE, 0xffffffff);
3275 tw32_f(offset + CPU_MODE, CPU_MODE_HALT);
3276 udelay(10);
3277 } else {
3278 for (i = 0; i < 10000; i++) {
3279 tw32(offset + CPU_STATE, 0xffffffff);
3280 tw32(offset + CPU_MODE, CPU_MODE_HALT);
3281 if (tr32(offset + CPU_MODE) & CPU_MODE_HALT)
3282 break;
3283 }
3284 }
3285
3286 if (i >= 10000) {
3287 netdev_err(tp->dev, "%s timed out, %s CPU\n",
3288 __func__, offset == RX_CPU_BASE ? "RX" : "TX");
3289 return -ENODEV;
3290 }
3291
3292 /* Clear firmware's nvram arbitration. */
3293 if (tg3_flag(tp, NVRAM))
3294 tw32(NVRAM_SWARB, SWARB_REQ_CLR0);
3295 return 0;
3296}
3297
3298struct fw_info {
3299 unsigned int fw_base;
3300 unsigned int fw_len;
3301 const __be32 *fw_data;
3302};
3303
3304/* tp->lock is held. */
3305static int tg3_load_firmware_cpu(struct tg3 *tp, u32 cpu_base,
3306 u32 cpu_scratch_base, int cpu_scratch_size,
3307 struct fw_info *info)
3308{
3309 int err, lock_err, i;
3310 void (*write_op)(struct tg3 *, u32, u32);
3311
3312 if (cpu_base == TX_CPU_BASE && tg3_flag(tp, 5705_PLUS)) {
3313 netdev_err(tp->dev,
3314 "%s: Trying to load TX cpu firmware which is 5705\n",
3315 __func__);
3316 return -EINVAL;
3317 }
3318
3319 if (tg3_flag(tp, 5705_PLUS))
3320 write_op = tg3_write_mem;
3321 else
3322 write_op = tg3_write_indirect_reg32;
3323
3324 /* It is possible that bootcode is still loading at this point.
3325 * Get the nvram lock first before halting the cpu.
3326 */
3327 lock_err = tg3_nvram_lock(tp);
3328 err = tg3_halt_cpu(tp, cpu_base);
3329 if (!lock_err)
3330 tg3_nvram_unlock(tp);
3331 if (err)
3332 goto out;
3333
3334 for (i = 0; i < cpu_scratch_size; i += sizeof(u32))
3335 write_op(tp, cpu_scratch_base + i, 0);
3336 tw32(cpu_base + CPU_STATE, 0xffffffff);
3337 tw32(cpu_base + CPU_MODE, tr32(cpu_base+CPU_MODE)|CPU_MODE_HALT);
3338 for (i = 0; i < (info->fw_len / sizeof(u32)); i++)
3339 write_op(tp, (cpu_scratch_base +
3340 (info->fw_base & 0xffff) +
3341 (i * sizeof(u32))),
3342 be32_to_cpu(info->fw_data[i]));
3343
3344 err = 0;
3345
3346out:
3347 return err;
3348}
3349
3350/* tp->lock is held. */
3351static int tg3_load_5701_a0_firmware_fix(struct tg3 *tp)
3352{
3353 struct fw_info info;
3354 const __be32 *fw_data;
3355 int err, i;
3356
3357 fw_data = (void *)tp->fw->data;
3358
3359 /* Firmware blob starts with version numbers, followed by
3360 start address and length. We are setting complete length.
3361 length = end_address_of_bss - start_address_of_text.
3362 Remainder is the blob to be loaded contiguously
3363 from start address. */
3364
3365 info.fw_base = be32_to_cpu(fw_data[1]);
3366 info.fw_len = tp->fw->size - 12;
3367 info.fw_data = &fw_data[3];
3368
3369 err = tg3_load_firmware_cpu(tp, RX_CPU_BASE,
3370 RX_CPU_SCRATCH_BASE, RX_CPU_SCRATCH_SIZE,
3371 &info);
3372 if (err)
3373 return err;
3374
3375 err = tg3_load_firmware_cpu(tp, TX_CPU_BASE,
3376 TX_CPU_SCRATCH_BASE, TX_CPU_SCRATCH_SIZE,
3377 &info);
3378 if (err)
3379 return err;
3380
3381 /* Now startup only the RX cpu. */
3382 tw32(RX_CPU_BASE + CPU_STATE, 0xffffffff);
3383 tw32_f(RX_CPU_BASE + CPU_PC, info.fw_base);
3384
3385 for (i = 0; i < 5; i++) {
3386 if (tr32(RX_CPU_BASE + CPU_PC) == info.fw_base)
3387 break;
3388 tw32(RX_CPU_BASE + CPU_STATE, 0xffffffff);
3389 tw32(RX_CPU_BASE + CPU_MODE, CPU_MODE_HALT);
3390 tw32_f(RX_CPU_BASE + CPU_PC, info.fw_base);
3391 udelay(1000);
3392 }
3393 if (i >= 5) {
3394 netdev_err(tp->dev, "%s fails to set RX CPU PC, is %08x "
3395 "should be %08x\n", __func__,
3396 tr32(RX_CPU_BASE + CPU_PC), info.fw_base);
3397 return -ENODEV;
3398 }
3399 tw32(RX_CPU_BASE + CPU_STATE, 0xffffffff);
3400 tw32_f(RX_CPU_BASE + CPU_MODE, 0x00000000);
3401
3402 return 0;
3403}
3404
3405/* tp->lock is held. */
3406static int tg3_load_tso_firmware(struct tg3 *tp)
3407{
3408 struct fw_info info;
3409 const __be32 *fw_data;
3410 unsigned long cpu_base, cpu_scratch_base, cpu_scratch_size;
3411 int err, i;
3412
3413 if (tg3_flag(tp, HW_TSO_1) ||
3414 tg3_flag(tp, HW_TSO_2) ||
3415 tg3_flag(tp, HW_TSO_3))
3416 return 0;
3417
3418 fw_data = (void *)tp->fw->data;
3419
3420 /* Firmware blob starts with version numbers, followed by
3421 start address and length. We are setting complete length.
3422 length = end_address_of_bss - start_address_of_text.
3423 Remainder is the blob to be loaded contiguously
3424 from start address. */
3425
3426 info.fw_base = be32_to_cpu(fw_data[1]);
3427 cpu_scratch_size = tp->fw_len;
3428 info.fw_len = tp->fw->size - 12;
3429 info.fw_data = &fw_data[3];
3430
3431 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
3432 cpu_base = RX_CPU_BASE;
3433 cpu_scratch_base = NIC_SRAM_MBUF_POOL_BASE5705;
3434 } else {
3435 cpu_base = TX_CPU_BASE;
3436 cpu_scratch_base = TX_CPU_SCRATCH_BASE;
3437 cpu_scratch_size = TX_CPU_SCRATCH_SIZE;
3438 }
3439
3440 err = tg3_load_firmware_cpu(tp, cpu_base,
3441 cpu_scratch_base, cpu_scratch_size,
3442 &info);
3443 if (err)
3444 return err;
3445
3446 /* Now startup the cpu. */
3447 tw32(cpu_base + CPU_STATE, 0xffffffff);
3448 tw32_f(cpu_base + CPU_PC, info.fw_base);
3449
3450 for (i = 0; i < 5; i++) {
3451 if (tr32(cpu_base + CPU_PC) == info.fw_base)
3452 break;
3453 tw32(cpu_base + CPU_STATE, 0xffffffff);
3454 tw32(cpu_base + CPU_MODE, CPU_MODE_HALT);
3455 tw32_f(cpu_base + CPU_PC, info.fw_base);
3456 udelay(1000);
3457 }
3458 if (i >= 5) {
3459 netdev_err(tp->dev,
3460 "%s fails to set CPU PC, is %08x should be %08x\n",
3461 __func__, tr32(cpu_base + CPU_PC), info.fw_base);
3462 return -ENODEV;
3463 }
3464 tw32(cpu_base + CPU_STATE, 0xffffffff);
3465 tw32_f(cpu_base + CPU_MODE, 0x00000000);
3466 return 0;
3467}
3468
3469
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003470/* tp->lock is held. */
Matt Carlson3f007892008-11-03 16:51:36 -08003471static void __tg3_set_mac_addr(struct tg3 *tp, int skip_mac_1)
3472{
3473 u32 addr_high, addr_low;
3474 int i;
3475
3476 addr_high = ((tp->dev->dev_addr[0] << 8) |
3477 tp->dev->dev_addr[1]);
3478 addr_low = ((tp->dev->dev_addr[2] << 24) |
3479 (tp->dev->dev_addr[3] << 16) |
3480 (tp->dev->dev_addr[4] << 8) |
3481 (tp->dev->dev_addr[5] << 0));
3482 for (i = 0; i < 4; i++) {
3483 if (i == 1 && skip_mac_1)
3484 continue;
3485 tw32(MAC_ADDR_0_HIGH + (i * 8), addr_high);
3486 tw32(MAC_ADDR_0_LOW + (i * 8), addr_low);
3487 }
3488
3489 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
3490 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
3491 for (i = 0; i < 12; i++) {
3492 tw32(MAC_EXTADDR_0_HIGH + (i * 8), addr_high);
3493 tw32(MAC_EXTADDR_0_LOW + (i * 8), addr_low);
3494 }
3495 }
3496
3497 addr_high = (tp->dev->dev_addr[0] +
3498 tp->dev->dev_addr[1] +
3499 tp->dev->dev_addr[2] +
3500 tp->dev->dev_addr[3] +
3501 tp->dev->dev_addr[4] +
3502 tp->dev->dev_addr[5]) &
3503 TX_BACKOFF_SEED_MASK;
3504 tw32(MAC_TX_BACKOFF_SEED, addr_high);
3505}
3506
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003507static void tg3_enable_register_access(struct tg3 *tp)
3508{
3509 /*
3510 * Make sure register accesses (indirect or otherwise) will function
3511 * correctly.
3512 */
3513 pci_write_config_dword(tp->pdev,
3514 TG3PCI_MISC_HOST_CTRL, tp->misc_host_ctrl);
3515}
3516
3517static int tg3_power_up(struct tg3 *tp)
3518{
Matt Carlsonbed98292011-07-13 09:27:29 +00003519 int err;
3520
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003521 tg3_enable_register_access(tp);
3522
Matt Carlsonbed98292011-07-13 09:27:29 +00003523 err = pci_set_power_state(tp->pdev, PCI_D0);
3524 if (!err) {
3525 /* Switch out of Vaux if it is a NIC */
3526 tg3_pwrsrc_switch_to_vmain(tp);
3527 } else {
3528 netdev_err(tp->dev, "Transition to D0 failed\n");
3529 }
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003530
Matt Carlsonbed98292011-07-13 09:27:29 +00003531 return err;
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003532}
3533
Matt Carlson4b409522012-02-13 10:20:11 +00003534static int tg3_setup_phy(struct tg3 *, int);
3535
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003536static int tg3_power_down_prepare(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003537{
3538 u32 misc_host_ctrl;
Matt Carlson0a459aa2008-11-03 16:54:15 -08003539 bool device_should_wake, do_low_power;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003540
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003541 tg3_enable_register_access(tp);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08003542
3543 /* Restore the CLKREQ setting. */
Joe Perches63c3a662011-04-26 08:12:10 +00003544 if (tg3_flag(tp, CLKREQ_BUG)) {
Matt Carlson5e7dfd02008-11-21 17:18:16 -08003545 u16 lnkctl;
3546
3547 pci_read_config_word(tp->pdev,
Jon Mason708ebb3a2011-06-27 12:56:50 +00003548 pci_pcie_cap(tp->pdev) + PCI_EXP_LNKCTL,
Matt Carlson5e7dfd02008-11-21 17:18:16 -08003549 &lnkctl);
3550 lnkctl |= PCI_EXP_LNKCTL_CLKREQ_EN;
3551 pci_write_config_word(tp->pdev,
Jon Mason708ebb3a2011-06-27 12:56:50 +00003552 pci_pcie_cap(tp->pdev) + PCI_EXP_LNKCTL,
Matt Carlson5e7dfd02008-11-21 17:18:16 -08003553 lnkctl);
3554 }
3555
Linus Torvalds1da177e2005-04-16 15:20:36 -07003556 misc_host_ctrl = tr32(TG3PCI_MISC_HOST_CTRL);
3557 tw32(TG3PCI_MISC_HOST_CTRL,
3558 misc_host_ctrl | MISC_HOST_CTRL_MASK_PCI_INT);
3559
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003560 device_should_wake = device_may_wakeup(&tp->pdev->dev) &&
Joe Perches63c3a662011-04-26 08:12:10 +00003561 tg3_flag(tp, WOL_ENABLE);
Matt Carlson05ac4cb2008-11-03 16:53:46 -08003562
Joe Perches63c3a662011-04-26 08:12:10 +00003563 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson0a459aa2008-11-03 16:54:15 -08003564 do_low_power = false;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003565 if ((tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) &&
Matt Carlson80096062010-08-02 11:26:06 +00003566 !(tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07003567 struct phy_device *phydev;
Matt Carlson0a459aa2008-11-03 16:54:15 -08003568 u32 phyid, advertising;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07003569
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00003570 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07003571
Matt Carlson80096062010-08-02 11:26:06 +00003572 tp->phy_flags |= TG3_PHYFLG_IS_LOW_POWER;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07003573
Matt Carlsonc6700ce2012-02-13 15:20:15 +00003574 tp->link_config.speed = phydev->speed;
3575 tp->link_config.duplex = phydev->duplex;
3576 tp->link_config.autoneg = phydev->autoneg;
3577 tp->link_config.advertising = phydev->advertising;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07003578
3579 advertising = ADVERTISED_TP |
3580 ADVERTISED_Pause |
3581 ADVERTISED_Autoneg |
3582 ADVERTISED_10baseT_Half;
3583
Joe Perches63c3a662011-04-26 08:12:10 +00003584 if (tg3_flag(tp, ENABLE_ASF) || device_should_wake) {
3585 if (tg3_flag(tp, WOL_SPEED_100MB))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07003586 advertising |=
3587 ADVERTISED_100baseT_Half |
3588 ADVERTISED_100baseT_Full |
3589 ADVERTISED_10baseT_Full;
3590 else
3591 advertising |= ADVERTISED_10baseT_Full;
3592 }
3593
3594 phydev->advertising = advertising;
3595
3596 phy_start_aneg(phydev);
Matt Carlson0a459aa2008-11-03 16:54:15 -08003597
3598 phyid = phydev->drv->phy_id & phydev->drv->phy_id_mask;
Matt Carlson6a443a02010-02-17 15:17:04 +00003599 if (phyid != PHY_ID_BCMAC131) {
3600 phyid &= PHY_BCM_OUI_MASK;
3601 if (phyid == PHY_BCM_OUI_1 ||
3602 phyid == PHY_BCM_OUI_2 ||
3603 phyid == PHY_BCM_OUI_3)
Matt Carlson0a459aa2008-11-03 16:54:15 -08003604 do_low_power = true;
3605 }
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07003606 }
Matt Carlsondd477002008-05-25 23:45:58 -07003607 } else {
Matt Carlson20232762008-12-21 20:18:56 -08003608 do_low_power = true;
Matt Carlson0a459aa2008-11-03 16:54:15 -08003609
Matt Carlsonc6700ce2012-02-13 15:20:15 +00003610 if (!(tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER))
Matt Carlson80096062010-08-02 11:26:06 +00003611 tp->phy_flags |= TG3_PHYFLG_IS_LOW_POWER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003612
Matt Carlson2855b9f2012-02-13 15:20:14 +00003613 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
Matt Carlsondd477002008-05-25 23:45:58 -07003614 tg3_setup_phy(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003615 }
3616
Michael Chanb5d37722006-09-27 16:06:21 -07003617 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
3618 u32 val;
3619
3620 val = tr32(GRC_VCPU_EXT_CTRL);
3621 tw32(GRC_VCPU_EXT_CTRL, val | GRC_VCPU_EXT_CTRL_DISABLE_WOL);
Joe Perches63c3a662011-04-26 08:12:10 +00003622 } else if (!tg3_flag(tp, ENABLE_ASF)) {
Michael Chan6921d202005-12-13 21:15:53 -08003623 int i;
3624 u32 val;
3625
3626 for (i = 0; i < 200; i++) {
3627 tg3_read_mem(tp, NIC_SRAM_FW_ASF_STATUS_MBOX, &val);
3628 if (val == ~NIC_SRAM_FIRMWARE_MBOX_MAGIC1)
3629 break;
3630 msleep(1);
3631 }
3632 }
Joe Perches63c3a662011-04-26 08:12:10 +00003633 if (tg3_flag(tp, WOL_CAP))
Gary Zambranoa85feb82007-05-05 11:52:19 -07003634 tg3_write_mem(tp, NIC_SRAM_WOL_MBOX, WOL_SIGNATURE |
3635 WOL_DRV_STATE_SHUTDOWN |
3636 WOL_DRV_WOL |
3637 WOL_SET_MAGIC_PKT);
Michael Chan6921d202005-12-13 21:15:53 -08003638
Matt Carlson05ac4cb2008-11-03 16:53:46 -08003639 if (device_should_wake) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003640 u32 mac_mode;
3641
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003642 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES)) {
Matt Carlsonb4bd2922011-04-20 07:57:41 +00003643 if (do_low_power &&
3644 !(tp->phy_flags & TG3_PHYFLG_IS_FET)) {
3645 tg3_phy_auxctl_write(tp,
3646 MII_TG3_AUXCTL_SHDWSEL_PWRCTL,
3647 MII_TG3_AUXCTL_PCTL_WOL_EN |
3648 MII_TG3_AUXCTL_PCTL_100TX_LPWR |
3649 MII_TG3_AUXCTL_PCTL_CL_AB_TXDAC);
Matt Carlsondd477002008-05-25 23:45:58 -07003650 udelay(40);
3651 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003652
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003653 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
Michael Chan3f7045c2006-09-27 16:02:29 -07003654 mac_mode = MAC_MODE_PORT_MODE_GMII;
3655 else
3656 mac_mode = MAC_MODE_PORT_MODE_MII;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003657
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07003658 mac_mode |= tp->mac_mode & MAC_MODE_LINK_POLARITY;
3659 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
3660 ASIC_REV_5700) {
Joe Perches63c3a662011-04-26 08:12:10 +00003661 u32 speed = tg3_flag(tp, WOL_SPEED_100MB) ?
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07003662 SPEED_100 : SPEED_10;
3663 if (tg3_5700_link_polarity(tp, speed))
3664 mac_mode |= MAC_MODE_LINK_POLARITY;
3665 else
3666 mac_mode &= ~MAC_MODE_LINK_POLARITY;
3667 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003668 } else {
3669 mac_mode = MAC_MODE_PORT_MODE_TBI;
3670 }
3671
Joe Perches63c3a662011-04-26 08:12:10 +00003672 if (!tg3_flag(tp, 5750_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003673 tw32(MAC_LED_CTRL, tp->led_ctrl);
3674
Matt Carlson05ac4cb2008-11-03 16:53:46 -08003675 mac_mode |= MAC_MODE_MAGIC_PKT_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00003676 if ((tg3_flag(tp, 5705_PLUS) && !tg3_flag(tp, 5780_CLASS)) &&
3677 (tg3_flag(tp, ENABLE_ASF) || tg3_flag(tp, ENABLE_APE)))
Matt Carlson05ac4cb2008-11-03 16:53:46 -08003678 mac_mode |= MAC_MODE_KEEP_FRAME_IN_WOL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003679
Joe Perches63c3a662011-04-26 08:12:10 +00003680 if (tg3_flag(tp, ENABLE_APE))
Matt Carlsond2394e6b2010-11-24 08:31:47 +00003681 mac_mode |= MAC_MODE_APE_TX_EN |
3682 MAC_MODE_APE_RX_EN |
3683 MAC_MODE_TDE_ENABLE;
Matt Carlson3bda1252008-08-15 14:08:22 -07003684
Linus Torvalds1da177e2005-04-16 15:20:36 -07003685 tw32_f(MAC_MODE, mac_mode);
3686 udelay(100);
3687
3688 tw32_f(MAC_RX_MODE, RX_MODE_ENABLE);
3689 udelay(10);
3690 }
3691
Joe Perches63c3a662011-04-26 08:12:10 +00003692 if (!tg3_flag(tp, WOL_SPEED_100MB) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003693 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
3694 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)) {
3695 u32 base_val;
3696
3697 base_val = tp->pci_clock_ctrl;
3698 base_val |= (CLOCK_CTRL_RXCLK_DISABLE |
3699 CLOCK_CTRL_TXCLK_DISABLE);
3700
Michael Chanb401e9e2005-12-19 16:27:04 -08003701 tw32_wait_f(TG3PCI_CLOCK_CTRL, base_val | CLOCK_CTRL_ALTCLK |
3702 CLOCK_CTRL_PWRDOWN_PLL133, 40);
Joe Perches63c3a662011-04-26 08:12:10 +00003703 } else if (tg3_flag(tp, 5780_CLASS) ||
3704 tg3_flag(tp, CPMU_PRESENT) ||
Matt Carlson6ff6f812011-05-19 12:12:54 +00003705 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan4cf78e42005-07-25 12:29:19 -07003706 /* do nothing */
Joe Perches63c3a662011-04-26 08:12:10 +00003707 } else if (!(tg3_flag(tp, 5750_PLUS) && tg3_flag(tp, ENABLE_ASF))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003708 u32 newbits1, newbits2;
3709
3710 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
3711 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
3712 newbits1 = (CLOCK_CTRL_RXCLK_DISABLE |
3713 CLOCK_CTRL_TXCLK_DISABLE |
3714 CLOCK_CTRL_ALTCLK);
3715 newbits2 = newbits1 | CLOCK_CTRL_44MHZ_CORE;
Joe Perches63c3a662011-04-26 08:12:10 +00003716 } else if (tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003717 newbits1 = CLOCK_CTRL_625_CORE;
3718 newbits2 = newbits1 | CLOCK_CTRL_ALTCLK;
3719 } else {
3720 newbits1 = CLOCK_CTRL_ALTCLK;
3721 newbits2 = newbits1 | CLOCK_CTRL_44MHZ_CORE;
3722 }
3723
Michael Chanb401e9e2005-12-19 16:27:04 -08003724 tw32_wait_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl | newbits1,
3725 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003726
Michael Chanb401e9e2005-12-19 16:27:04 -08003727 tw32_wait_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl | newbits2,
3728 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003729
Joe Perches63c3a662011-04-26 08:12:10 +00003730 if (!tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003731 u32 newbits3;
3732
3733 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
3734 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
3735 newbits3 = (CLOCK_CTRL_RXCLK_DISABLE |
3736 CLOCK_CTRL_TXCLK_DISABLE |
3737 CLOCK_CTRL_44MHZ_CORE);
3738 } else {
3739 newbits3 = CLOCK_CTRL_44MHZ_CORE;
3740 }
3741
Michael Chanb401e9e2005-12-19 16:27:04 -08003742 tw32_wait_f(TG3PCI_CLOCK_CTRL,
3743 tp->pci_clock_ctrl | newbits3, 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003744 }
3745 }
3746
Joe Perches63c3a662011-04-26 08:12:10 +00003747 if (!(device_should_wake) && !tg3_flag(tp, ENABLE_ASF))
Matt Carlson0a459aa2008-11-03 16:54:15 -08003748 tg3_power_down_phy(tp, do_low_power);
Michael Chan6921d202005-12-13 21:15:53 -08003749
Matt Carlsoncd0d7222011-07-13 09:27:33 +00003750 tg3_frob_aux_power(tp, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003751
3752 /* Workaround for unstable PLL clock */
3753 if ((GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_AX) ||
3754 (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_BX)) {
3755 u32 val = tr32(0x7d00);
3756
3757 val &= ~((1 << 16) | (1 << 4) | (1 << 2) | (1 << 1) | 1);
3758 tw32(0x7d00, val);
Joe Perches63c3a662011-04-26 08:12:10 +00003759 if (!tg3_flag(tp, ENABLE_ASF)) {
Michael Chanec41c7d2006-01-17 02:40:55 -08003760 int err;
3761
3762 err = tg3_nvram_lock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003763 tg3_halt_cpu(tp, RX_CPU_BASE);
Michael Chanec41c7d2006-01-17 02:40:55 -08003764 if (!err)
3765 tg3_nvram_unlock(tp);
Michael Chan6921d202005-12-13 21:15:53 -08003766 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003767 }
3768
Michael Chanbbadf502006-04-06 21:46:34 -07003769 tg3_write_sig_post_reset(tp, RESET_KIND_SHUTDOWN);
3770
Linus Torvalds1da177e2005-04-16 15:20:36 -07003771 return 0;
3772}
3773
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003774static void tg3_power_down(struct tg3 *tp)
3775{
3776 tg3_power_down_prepare(tp);
3777
Joe Perches63c3a662011-04-26 08:12:10 +00003778 pci_wake_from_d3(tp->pdev, tg3_flag(tp, WOL_ENABLE));
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003779 pci_set_power_state(tp->pdev, PCI_D3hot);
3780}
3781
Linus Torvalds1da177e2005-04-16 15:20:36 -07003782static void tg3_aux_stat_to_speed_duplex(struct tg3 *tp, u32 val, u16 *speed, u8 *duplex)
3783{
3784 switch (val & MII_TG3_AUX_STAT_SPDMASK) {
3785 case MII_TG3_AUX_STAT_10HALF:
3786 *speed = SPEED_10;
3787 *duplex = DUPLEX_HALF;
3788 break;
3789
3790 case MII_TG3_AUX_STAT_10FULL:
3791 *speed = SPEED_10;
3792 *duplex = DUPLEX_FULL;
3793 break;
3794
3795 case MII_TG3_AUX_STAT_100HALF:
3796 *speed = SPEED_100;
3797 *duplex = DUPLEX_HALF;
3798 break;
3799
3800 case MII_TG3_AUX_STAT_100FULL:
3801 *speed = SPEED_100;
3802 *duplex = DUPLEX_FULL;
3803 break;
3804
3805 case MII_TG3_AUX_STAT_1000HALF:
3806 *speed = SPEED_1000;
3807 *duplex = DUPLEX_HALF;
3808 break;
3809
3810 case MII_TG3_AUX_STAT_1000FULL:
3811 *speed = SPEED_1000;
3812 *duplex = DUPLEX_FULL;
3813 break;
3814
3815 default:
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003816 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Michael Chan715116a2006-09-27 16:09:25 -07003817 *speed = (val & MII_TG3_AUX_STAT_100) ? SPEED_100 :
3818 SPEED_10;
3819 *duplex = (val & MII_TG3_AUX_STAT_FULL) ? DUPLEX_FULL :
3820 DUPLEX_HALF;
3821 break;
3822 }
Matt Carlsone7405222012-02-13 15:20:16 +00003823 *speed = SPEED_UNKNOWN;
3824 *duplex = DUPLEX_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003825 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07003826 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003827}
3828
Matt Carlson42b64a42011-05-19 12:12:49 +00003829static int tg3_phy_autoneg_cfg(struct tg3 *tp, u32 advertise, u32 flowctrl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003830{
Matt Carlson42b64a42011-05-19 12:12:49 +00003831 int err = 0;
3832 u32 val, new_adv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003833
Matt Carlson42b64a42011-05-19 12:12:49 +00003834 new_adv = ADVERTISE_CSMA;
Hiroaki SHIMODA202ff1c2011-11-22 04:05:41 +00003835 new_adv |= ethtool_adv_to_mii_adv_t(advertise) & ADVERTISE_ALL;
Matt Carlsonf88788f2011-12-14 11:10:00 +00003836 new_adv |= mii_advertise_flowctrl(flowctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003837
Matt Carlson42b64a42011-05-19 12:12:49 +00003838 err = tg3_writephy(tp, MII_ADVERTISE, new_adv);
3839 if (err)
3840 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003841
Matt Carlson4f272092011-12-14 11:09:57 +00003842 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
3843 new_adv = ethtool_adv_to_mii_ctrl1000_t(advertise);
Matt Carlsonba4d07a2007-12-20 20:08:00 -08003844
Matt Carlson4f272092011-12-14 11:09:57 +00003845 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
3846 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0)
3847 new_adv |= CTL1000_AS_MASTER | CTL1000_ENABLE_MASTER;
Matt Carlsonba4d07a2007-12-20 20:08:00 -08003848
Matt Carlson4f272092011-12-14 11:09:57 +00003849 err = tg3_writephy(tp, MII_CTRL1000, new_adv);
3850 if (err)
3851 goto done;
3852 }
Matt Carlsonba4d07a2007-12-20 20:08:00 -08003853
Matt Carlson42b64a42011-05-19 12:12:49 +00003854 if (!(tp->phy_flags & TG3_PHYFLG_EEE_CAP))
3855 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003856
Matt Carlson42b64a42011-05-19 12:12:49 +00003857 tw32(TG3_CPMU_EEE_MODE,
3858 tr32(TG3_CPMU_EEE_MODE) & ~TG3_CPMU_EEEMD_LPI_ENABLE);
Matt Carlsonba4d07a2007-12-20 20:08:00 -08003859
Matt Carlson42b64a42011-05-19 12:12:49 +00003860 err = TG3_PHY_AUXCTL_SMDSP_ENABLE(tp);
3861 if (!err) {
3862 u32 err2;
Matt Carlson52b02d02010-10-14 10:37:41 +00003863
Matt Carlsona6b68da2010-12-06 08:28:52 +00003864 val = 0;
Matt Carlson42b64a42011-05-19 12:12:49 +00003865 /* Advertise 100-BaseTX EEE ability */
3866 if (advertise & ADVERTISED_100baseT_Full)
3867 val |= MDIO_AN_EEE_ADV_100TX;
3868 /* Advertise 1000-BaseT EEE ability */
3869 if (advertise & ADVERTISED_1000baseT_Full)
3870 val |= MDIO_AN_EEE_ADV_1000T;
3871 err = tg3_phy_cl45_write(tp, MDIO_MMD_AN, MDIO_AN_EEE_ADV, val);
Matt Carlsonb715ce92011-07-20 10:20:52 +00003872 if (err)
3873 val = 0;
3874
3875 switch (GET_ASIC_REV(tp->pci_chip_rev_id)) {
3876 case ASIC_REV_5717:
3877 case ASIC_REV_57765:
Matt Carlson55086ad2011-12-14 11:09:59 +00003878 case ASIC_REV_57766:
Matt Carlsonb715ce92011-07-20 10:20:52 +00003879 case ASIC_REV_5719:
3880 /* If we advertised any eee advertisements above... */
3881 if (val)
3882 val = MII_TG3_DSP_TAP26_ALNOKO |
3883 MII_TG3_DSP_TAP26_RMRXSTO |
3884 MII_TG3_DSP_TAP26_OPCSINPT;
3885 tg3_phydsp_write(tp, MII_TG3_DSP_TAP26, val);
3886 /* Fall through */
3887 case ASIC_REV_5720:
3888 if (!tg3_phydsp_read(tp, MII_TG3_DSP_CH34TP2, &val))
3889 tg3_phydsp_write(tp, MII_TG3_DSP_CH34TP2, val |
3890 MII_TG3_DSP_CH34TP2_HIBW01);
3891 }
Matt Carlson52b02d02010-10-14 10:37:41 +00003892
Matt Carlson42b64a42011-05-19 12:12:49 +00003893 err2 = TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
3894 if (!err)
3895 err = err2;
3896 }
3897
3898done:
3899 return err;
3900}
3901
3902static void tg3_phy_copper_begin(struct tg3 *tp)
3903{
Matt Carlsond13ba512012-02-22 12:35:19 +00003904 if (tp->link_config.autoneg == AUTONEG_ENABLE ||
3905 (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)) {
3906 u32 adv, fc;
Matt Carlson42b64a42011-05-19 12:12:49 +00003907
Matt Carlsond13ba512012-02-22 12:35:19 +00003908 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) {
3909 adv = ADVERTISED_10baseT_Half |
3910 ADVERTISED_10baseT_Full;
3911 if (tg3_flag(tp, WOL_SPEED_100MB))
3912 adv |= ADVERTISED_100baseT_Half |
3913 ADVERTISED_100baseT_Full;
Matt Carlson42b64a42011-05-19 12:12:49 +00003914
Matt Carlsond13ba512012-02-22 12:35:19 +00003915 fc = FLOW_CTRL_TX | FLOW_CTRL_RX;
Matt Carlson42b64a42011-05-19 12:12:49 +00003916 } else {
Matt Carlsond13ba512012-02-22 12:35:19 +00003917 adv = tp->link_config.advertising;
3918 if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
3919 adv &= ~(ADVERTISED_1000baseT_Half |
3920 ADVERTISED_1000baseT_Full);
3921
3922 fc = tp->link_config.flowctrl;
Matt Carlson42b64a42011-05-19 12:12:49 +00003923 }
3924
Matt Carlsond13ba512012-02-22 12:35:19 +00003925 tg3_phy_autoneg_cfg(tp, adv, fc);
Matt Carlson52b02d02010-10-14 10:37:41 +00003926
Matt Carlsond13ba512012-02-22 12:35:19 +00003927 tg3_writephy(tp, MII_BMCR,
3928 BMCR_ANENABLE | BMCR_ANRESTART);
3929 } else {
3930 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003931 u32 bmcr, orig_bmcr;
3932
3933 tp->link_config.active_speed = tp->link_config.speed;
3934 tp->link_config.active_duplex = tp->link_config.duplex;
3935
3936 bmcr = 0;
3937 switch (tp->link_config.speed) {
3938 default:
3939 case SPEED_10:
3940 break;
3941
3942 case SPEED_100:
3943 bmcr |= BMCR_SPEED100;
3944 break;
3945
3946 case SPEED_1000:
Matt Carlson221c5632011-06-13 13:39:01 +00003947 bmcr |= BMCR_SPEED1000;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003948 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07003949 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003950
3951 if (tp->link_config.duplex == DUPLEX_FULL)
3952 bmcr |= BMCR_FULLDPLX;
3953
3954 if (!tg3_readphy(tp, MII_BMCR, &orig_bmcr) &&
3955 (bmcr != orig_bmcr)) {
3956 tg3_writephy(tp, MII_BMCR, BMCR_LOOPBACK);
3957 for (i = 0; i < 1500; i++) {
3958 u32 tmp;
3959
3960 udelay(10);
3961 if (tg3_readphy(tp, MII_BMSR, &tmp) ||
3962 tg3_readphy(tp, MII_BMSR, &tmp))
3963 continue;
3964 if (!(tmp & BMSR_LSTATUS)) {
3965 udelay(40);
3966 break;
3967 }
3968 }
3969 tg3_writephy(tp, MII_BMCR, bmcr);
3970 udelay(40);
3971 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003972 }
3973}
3974
3975static int tg3_init_5401phy_dsp(struct tg3 *tp)
3976{
3977 int err;
3978
3979 /* Turn off tap power management. */
3980 /* Set Extended packet length bit */
Matt Carlsonb4bd2922011-04-20 07:57:41 +00003981 err = tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_AUXCTL, 0x4c20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003982
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00003983 err |= tg3_phydsp_write(tp, 0x0012, 0x1804);
3984 err |= tg3_phydsp_write(tp, 0x0013, 0x1204);
3985 err |= tg3_phydsp_write(tp, 0x8006, 0x0132);
3986 err |= tg3_phydsp_write(tp, 0x8006, 0x0232);
3987 err |= tg3_phydsp_write(tp, 0x201f, 0x0a20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003988
3989 udelay(40);
3990
3991 return err;
3992}
3993
Matt Carlsone2bf73e2011-12-08 14:40:15 +00003994static bool tg3_phy_copper_an_config_ok(struct tg3 *tp, u32 *lcladv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003995{
Matt Carlsone2bf73e2011-12-08 14:40:15 +00003996 u32 advmsk, tgtadv, advertising;
Michael Chan3600d912006-12-07 00:21:48 -08003997
Matt Carlsone2bf73e2011-12-08 14:40:15 +00003998 advertising = tp->link_config.advertising;
3999 tgtadv = ethtool_adv_to_mii_adv_t(advertising) & ADVERTISE_ALL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004000
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004001 advmsk = ADVERTISE_ALL;
4002 if (tp->link_config.active_duplex == DUPLEX_FULL) {
Matt Carlsonf88788f2011-12-14 11:10:00 +00004003 tgtadv |= mii_advertise_flowctrl(tp->link_config.flowctrl);
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004004 advmsk |= ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
4005 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004006
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004007 if (tg3_readphy(tp, MII_ADVERTISE, lcladv))
4008 return false;
4009
4010 if ((*lcladv & advmsk) != tgtadv)
4011 return false;
Matt Carlsonb99d2a52011-08-31 11:44:47 +00004012
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004013 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004014 u32 tg3_ctrl;
4015
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004016 tgtadv = ethtool_adv_to_mii_ctrl1000_t(advertising);
Michael Chan3600d912006-12-07 00:21:48 -08004017
Matt Carlson221c5632011-06-13 13:39:01 +00004018 if (tg3_readphy(tp, MII_CTRL1000, &tg3_ctrl))
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004019 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004020
Matt Carlson3198e072012-02-13 15:20:10 +00004021 if (tgtadv &&
4022 (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
4023 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0)) {
4024 tgtadv |= CTL1000_AS_MASTER | CTL1000_ENABLE_MASTER;
4025 tg3_ctrl &= (ADVERTISE_1000HALF | ADVERTISE_1000FULL |
4026 CTL1000_AS_MASTER | CTL1000_ENABLE_MASTER);
4027 } else {
4028 tg3_ctrl &= (ADVERTISE_1000HALF | ADVERTISE_1000FULL);
4029 }
4030
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004031 if (tg3_ctrl != tgtadv)
4032 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004033 }
Matt Carlson93a700a2011-08-31 11:44:54 +00004034
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004035 return true;
Matt Carlsonef167e22007-12-20 20:10:01 -08004036}
4037
Matt Carlson859edb22011-12-08 14:40:16 +00004038static bool tg3_phy_copper_fetch_rmtadv(struct tg3 *tp, u32 *rmtadv)
4039{
4040 u32 lpeth = 0;
4041
4042 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
4043 u32 val;
4044
4045 if (tg3_readphy(tp, MII_STAT1000, &val))
4046 return false;
4047
4048 lpeth = mii_stat1000_to_ethtool_lpa_t(val);
4049 }
4050
4051 if (tg3_readphy(tp, MII_LPA, rmtadv))
4052 return false;
4053
4054 lpeth |= mii_lpa_to_ethtool_lpa_t(*rmtadv);
4055 tp->link_config.rmt_adv = lpeth;
4056
4057 return true;
4058}
4059
Linus Torvalds1da177e2005-04-16 15:20:36 -07004060static int tg3_setup_copper_phy(struct tg3 *tp, int force_reset)
4061{
4062 int current_link_up;
Matt Carlsonf833c4c2010-09-15 09:00:01 +00004063 u32 bmsr, val;
Matt Carlsonef167e22007-12-20 20:10:01 -08004064 u32 lcl_adv, rmt_adv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004065 u16 current_speed;
4066 u8 current_duplex;
4067 int i, err;
4068
4069 tw32(MAC_EVENT, 0);
4070
4071 tw32_f(MAC_STATUS,
4072 (MAC_STATUS_SYNC_CHANGED |
4073 MAC_STATUS_CFG_CHANGED |
4074 MAC_STATUS_MI_COMPLETION |
4075 MAC_STATUS_LNKSTATE_CHANGED));
4076 udelay(40);
4077
Matt Carlson8ef21422008-05-02 16:47:53 -07004078 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
4079 tw32_f(MAC_MI_MODE,
4080 (tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL));
4081 udelay(80);
4082 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004083
Matt Carlsonb4bd2922011-04-20 07:57:41 +00004084 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_PWRCTL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004085
4086 /* Some third-party PHYs need to be reset on link going
4087 * down.
4088 */
4089 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
4090 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
4091 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) &&
4092 netif_carrier_ok(tp->dev)) {
4093 tg3_readphy(tp, MII_BMSR, &bmsr);
4094 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
4095 !(bmsr & BMSR_LSTATUS))
4096 force_reset = 1;
4097 }
4098 if (force_reset)
4099 tg3_phy_reset(tp);
4100
Matt Carlson79eb6902010-02-17 15:17:03 +00004101 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004102 tg3_readphy(tp, MII_BMSR, &bmsr);
4103 if (tg3_readphy(tp, MII_BMSR, &bmsr) ||
Joe Perches63c3a662011-04-26 08:12:10 +00004104 !tg3_flag(tp, INIT_COMPLETE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004105 bmsr = 0;
4106
4107 if (!(bmsr & BMSR_LSTATUS)) {
4108 err = tg3_init_5401phy_dsp(tp);
4109 if (err)
4110 return err;
4111
4112 tg3_readphy(tp, MII_BMSR, &bmsr);
4113 for (i = 0; i < 1000; i++) {
4114 udelay(10);
4115 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
4116 (bmsr & BMSR_LSTATUS)) {
4117 udelay(40);
4118 break;
4119 }
4120 }
4121
Matt Carlson79eb6902010-02-17 15:17:03 +00004122 if ((tp->phy_id & TG3_PHY_ID_REV_MASK) ==
4123 TG3_PHY_REV_BCM5401_B0 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004124 !(bmsr & BMSR_LSTATUS) &&
4125 tp->link_config.active_speed == SPEED_1000) {
4126 err = tg3_phy_reset(tp);
4127 if (!err)
4128 err = tg3_init_5401phy_dsp(tp);
4129 if (err)
4130 return err;
4131 }
4132 }
4133 } else if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
4134 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0) {
4135 /* 5701 {A0,B0} CRC bug workaround */
4136 tg3_writephy(tp, 0x15, 0x0a75);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00004137 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8c68);
4138 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8d68);
4139 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8c68);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004140 }
4141
4142 /* Clear pending interrupts... */
Matt Carlsonf833c4c2010-09-15 09:00:01 +00004143 tg3_readphy(tp, MII_TG3_ISTAT, &val);
4144 tg3_readphy(tp, MII_TG3_ISTAT, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004145
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004146 if (tp->phy_flags & TG3_PHYFLG_USE_MI_INTERRUPT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004147 tg3_writephy(tp, MII_TG3_IMASK, ~MII_TG3_INT_LINKCHG);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004148 else if (!(tp->phy_flags & TG3_PHYFLG_IS_FET))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004149 tg3_writephy(tp, MII_TG3_IMASK, ~0);
4150
4151 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
4152 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
4153 if (tp->led_ctrl == LED_CTRL_MODE_PHY_1)
4154 tg3_writephy(tp, MII_TG3_EXT_CTRL,
4155 MII_TG3_EXT_CTRL_LNK3_LED_MODE);
4156 else
4157 tg3_writephy(tp, MII_TG3_EXT_CTRL, 0);
4158 }
4159
4160 current_link_up = 0;
Matt Carlsone7405222012-02-13 15:20:16 +00004161 current_speed = SPEED_UNKNOWN;
4162 current_duplex = DUPLEX_UNKNOWN;
Matt Carlsone348c5e2011-11-21 15:01:20 +00004163 tp->phy_flags &= ~TG3_PHYFLG_MDIX_STATE;
Matt Carlson859edb22011-12-08 14:40:16 +00004164 tp->link_config.rmt_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004165
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004166 if (tp->phy_flags & TG3_PHYFLG_CAPACITIVE_COUPLING) {
Matt Carlson15ee95c2011-04-20 07:57:40 +00004167 err = tg3_phy_auxctl_read(tp,
4168 MII_TG3_AUXCTL_SHDWSEL_MISCTEST,
4169 &val);
4170 if (!err && !(val & (1 << 10))) {
Matt Carlsonb4bd2922011-04-20 07:57:41 +00004171 tg3_phy_auxctl_write(tp,
4172 MII_TG3_AUXCTL_SHDWSEL_MISCTEST,
4173 val | (1 << 10));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004174 goto relink;
4175 }
4176 }
4177
4178 bmsr = 0;
4179 for (i = 0; i < 100; i++) {
4180 tg3_readphy(tp, MII_BMSR, &bmsr);
4181 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
4182 (bmsr & BMSR_LSTATUS))
4183 break;
4184 udelay(40);
4185 }
4186
4187 if (bmsr & BMSR_LSTATUS) {
4188 u32 aux_stat, bmcr;
4189
4190 tg3_readphy(tp, MII_TG3_AUX_STAT, &aux_stat);
4191 for (i = 0; i < 2000; i++) {
4192 udelay(10);
4193 if (!tg3_readphy(tp, MII_TG3_AUX_STAT, &aux_stat) &&
4194 aux_stat)
4195 break;
4196 }
4197
4198 tg3_aux_stat_to_speed_duplex(tp, aux_stat,
4199 &current_speed,
4200 &current_duplex);
4201
4202 bmcr = 0;
4203 for (i = 0; i < 200; i++) {
4204 tg3_readphy(tp, MII_BMCR, &bmcr);
4205 if (tg3_readphy(tp, MII_BMCR, &bmcr))
4206 continue;
4207 if (bmcr && bmcr != 0x7fff)
4208 break;
4209 udelay(10);
4210 }
4211
Matt Carlsonef167e22007-12-20 20:10:01 -08004212 lcl_adv = 0;
4213 rmt_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004214
Matt Carlsonef167e22007-12-20 20:10:01 -08004215 tp->link_config.active_speed = current_speed;
4216 tp->link_config.active_duplex = current_duplex;
4217
4218 if (tp->link_config.autoneg == AUTONEG_ENABLE) {
4219 if ((bmcr & BMCR_ANENABLE) &&
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004220 tg3_phy_copper_an_config_ok(tp, &lcl_adv) &&
Matt Carlson859edb22011-12-08 14:40:16 +00004221 tg3_phy_copper_fetch_rmtadv(tp, &rmt_adv))
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004222 current_link_up = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004223 } else {
4224 if (!(bmcr & BMCR_ANENABLE) &&
4225 tp->link_config.speed == current_speed &&
Matt Carlsonef167e22007-12-20 20:10:01 -08004226 tp->link_config.duplex == current_duplex &&
4227 tp->link_config.flowctrl ==
4228 tp->link_config.active_flowctrl) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004229 current_link_up = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004230 }
4231 }
4232
Matt Carlsonef167e22007-12-20 20:10:01 -08004233 if (current_link_up == 1 &&
Matt Carlsone348c5e2011-11-21 15:01:20 +00004234 tp->link_config.active_duplex == DUPLEX_FULL) {
4235 u32 reg, bit;
4236
4237 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
4238 reg = MII_TG3_FET_GEN_STAT;
4239 bit = MII_TG3_FET_GEN_STAT_MDIXSTAT;
4240 } else {
4241 reg = MII_TG3_EXT_STAT;
4242 bit = MII_TG3_EXT_STAT_MDIX;
4243 }
4244
4245 if (!tg3_readphy(tp, reg, &val) && (val & bit))
4246 tp->phy_flags |= TG3_PHYFLG_MDIX_STATE;
4247
Matt Carlsonef167e22007-12-20 20:10:01 -08004248 tg3_setup_flow_control(tp, lcl_adv, rmt_adv);
Matt Carlsone348c5e2011-11-21 15:01:20 +00004249 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004250 }
4251
Linus Torvalds1da177e2005-04-16 15:20:36 -07004252relink:
Matt Carlson80096062010-08-02 11:26:06 +00004253 if (current_link_up == 0 || (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004254 tg3_phy_copper_begin(tp);
4255
Matt Carlsonf833c4c2010-09-15 09:00:01 +00004256 tg3_readphy(tp, MII_BMSR, &bmsr);
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00004257 if ((!tg3_readphy(tp, MII_BMSR, &bmsr) && (bmsr & BMSR_LSTATUS)) ||
4258 (tp->mac_mode & MAC_MODE_PORT_INT_LPBACK))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004259 current_link_up = 1;
4260 }
4261
4262 tp->mac_mode &= ~MAC_MODE_PORT_MODE_MASK;
4263 if (current_link_up == 1) {
4264 if (tp->link_config.active_speed == SPEED_100 ||
4265 tp->link_config.active_speed == SPEED_10)
4266 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
4267 else
4268 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004269 } else if (tp->phy_flags & TG3_PHYFLG_IS_FET)
Matt Carlson7f97a4b2009-08-25 10:10:03 +00004270 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
4271 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004272 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
4273
4274 tp->mac_mode &= ~MAC_MODE_HALF_DUPLEX;
4275 if (tp->link_config.active_duplex == DUPLEX_HALF)
4276 tp->mac_mode |= MAC_MODE_HALF_DUPLEX;
4277
Linus Torvalds1da177e2005-04-16 15:20:36 -07004278 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700) {
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07004279 if (current_link_up == 1 &&
4280 tg3_5700_link_polarity(tp, tp->link_config.active_speed))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004281 tp->mac_mode |= MAC_MODE_LINK_POLARITY;
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07004282 else
4283 tp->mac_mode &= ~MAC_MODE_LINK_POLARITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004284 }
4285
4286 /* ??? Without this setting Netgear GA302T PHY does not
4287 * ??? send/receive packets...
4288 */
Matt Carlson79eb6902010-02-17 15:17:03 +00004289 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5411 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004290 tp->pci_chip_rev_id == CHIPREV_ID_5700_ALTIMA) {
4291 tp->mi_mode |= MAC_MI_MODE_AUTO_POLL;
4292 tw32_f(MAC_MI_MODE, tp->mi_mode);
4293 udelay(80);
4294 }
4295
4296 tw32_f(MAC_MODE, tp->mac_mode);
4297 udelay(40);
4298
Matt Carlson52b02d02010-10-14 10:37:41 +00004299 tg3_phy_eee_adjust(tp, current_link_up);
4300
Joe Perches63c3a662011-04-26 08:12:10 +00004301 if (tg3_flag(tp, USE_LINKCHG_REG)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004302 /* Polled via timer. */
4303 tw32_f(MAC_EVENT, 0);
4304 } else {
4305 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
4306 }
4307 udelay(40);
4308
4309 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 &&
4310 current_link_up == 1 &&
4311 tp->link_config.active_speed == SPEED_1000 &&
Joe Perches63c3a662011-04-26 08:12:10 +00004312 (tg3_flag(tp, PCIX_MODE) || tg3_flag(tp, PCI_HIGH_SPEED))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004313 udelay(120);
4314 tw32_f(MAC_STATUS,
4315 (MAC_STATUS_SYNC_CHANGED |
4316 MAC_STATUS_CFG_CHANGED));
4317 udelay(40);
4318 tg3_write_mem(tp,
4319 NIC_SRAM_FIRMWARE_MBOX,
4320 NIC_SRAM_FIRMWARE_MBOX_MAGIC2);
4321 }
4322
Matt Carlson5e7dfd02008-11-21 17:18:16 -08004323 /* Prevent send BD corruption. */
Joe Perches63c3a662011-04-26 08:12:10 +00004324 if (tg3_flag(tp, CLKREQ_BUG)) {
Matt Carlson5e7dfd02008-11-21 17:18:16 -08004325 u16 oldlnkctl, newlnkctl;
4326
4327 pci_read_config_word(tp->pdev,
Jon Mason708ebb3a2011-06-27 12:56:50 +00004328 pci_pcie_cap(tp->pdev) + PCI_EXP_LNKCTL,
Matt Carlson5e7dfd02008-11-21 17:18:16 -08004329 &oldlnkctl);
4330 if (tp->link_config.active_speed == SPEED_100 ||
4331 tp->link_config.active_speed == SPEED_10)
4332 newlnkctl = oldlnkctl & ~PCI_EXP_LNKCTL_CLKREQ_EN;
4333 else
4334 newlnkctl = oldlnkctl | PCI_EXP_LNKCTL_CLKREQ_EN;
4335 if (newlnkctl != oldlnkctl)
4336 pci_write_config_word(tp->pdev,
Matt Carlson93a700a2011-08-31 11:44:54 +00004337 pci_pcie_cap(tp->pdev) +
4338 PCI_EXP_LNKCTL, newlnkctl);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08004339 }
4340
Linus Torvalds1da177e2005-04-16 15:20:36 -07004341 if (current_link_up != netif_carrier_ok(tp->dev)) {
4342 if (current_link_up)
4343 netif_carrier_on(tp->dev);
4344 else
4345 netif_carrier_off(tp->dev);
4346 tg3_link_report(tp);
4347 }
4348
4349 return 0;
4350}
4351
4352struct tg3_fiber_aneginfo {
4353 int state;
4354#define ANEG_STATE_UNKNOWN 0
4355#define ANEG_STATE_AN_ENABLE 1
4356#define ANEG_STATE_RESTART_INIT 2
4357#define ANEG_STATE_RESTART 3
4358#define ANEG_STATE_DISABLE_LINK_OK 4
4359#define ANEG_STATE_ABILITY_DETECT_INIT 5
4360#define ANEG_STATE_ABILITY_DETECT 6
4361#define ANEG_STATE_ACK_DETECT_INIT 7
4362#define ANEG_STATE_ACK_DETECT 8
4363#define ANEG_STATE_COMPLETE_ACK_INIT 9
4364#define ANEG_STATE_COMPLETE_ACK 10
4365#define ANEG_STATE_IDLE_DETECT_INIT 11
4366#define ANEG_STATE_IDLE_DETECT 12
4367#define ANEG_STATE_LINK_OK 13
4368#define ANEG_STATE_NEXT_PAGE_WAIT_INIT 14
4369#define ANEG_STATE_NEXT_PAGE_WAIT 15
4370
4371 u32 flags;
4372#define MR_AN_ENABLE 0x00000001
4373#define MR_RESTART_AN 0x00000002
4374#define MR_AN_COMPLETE 0x00000004
4375#define MR_PAGE_RX 0x00000008
4376#define MR_NP_LOADED 0x00000010
4377#define MR_TOGGLE_TX 0x00000020
4378#define MR_LP_ADV_FULL_DUPLEX 0x00000040
4379#define MR_LP_ADV_HALF_DUPLEX 0x00000080
4380#define MR_LP_ADV_SYM_PAUSE 0x00000100
4381#define MR_LP_ADV_ASYM_PAUSE 0x00000200
4382#define MR_LP_ADV_REMOTE_FAULT1 0x00000400
4383#define MR_LP_ADV_REMOTE_FAULT2 0x00000800
4384#define MR_LP_ADV_NEXT_PAGE 0x00001000
4385#define MR_TOGGLE_RX 0x00002000
4386#define MR_NP_RX 0x00004000
4387
4388#define MR_LINK_OK 0x80000000
4389
4390 unsigned long link_time, cur_time;
4391
4392 u32 ability_match_cfg;
4393 int ability_match_count;
4394
4395 char ability_match, idle_match, ack_match;
4396
4397 u32 txconfig, rxconfig;
4398#define ANEG_CFG_NP 0x00000080
4399#define ANEG_CFG_ACK 0x00000040
4400#define ANEG_CFG_RF2 0x00000020
4401#define ANEG_CFG_RF1 0x00000010
4402#define ANEG_CFG_PS2 0x00000001
4403#define ANEG_CFG_PS1 0x00008000
4404#define ANEG_CFG_HD 0x00004000
4405#define ANEG_CFG_FD 0x00002000
4406#define ANEG_CFG_INVAL 0x00001f06
4407
4408};
4409#define ANEG_OK 0
4410#define ANEG_DONE 1
4411#define ANEG_TIMER_ENAB 2
4412#define ANEG_FAILED -1
4413
4414#define ANEG_STATE_SETTLE_TIME 10000
4415
4416static int tg3_fiber_aneg_smachine(struct tg3 *tp,
4417 struct tg3_fiber_aneginfo *ap)
4418{
Matt Carlson5be73b42007-12-20 20:09:29 -08004419 u16 flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004420 unsigned long delta;
4421 u32 rx_cfg_reg;
4422 int ret;
4423
4424 if (ap->state == ANEG_STATE_UNKNOWN) {
4425 ap->rxconfig = 0;
4426 ap->link_time = 0;
4427 ap->cur_time = 0;
4428 ap->ability_match_cfg = 0;
4429 ap->ability_match_count = 0;
4430 ap->ability_match = 0;
4431 ap->idle_match = 0;
4432 ap->ack_match = 0;
4433 }
4434 ap->cur_time++;
4435
4436 if (tr32(MAC_STATUS) & MAC_STATUS_RCVD_CFG) {
4437 rx_cfg_reg = tr32(MAC_RX_AUTO_NEG);
4438
4439 if (rx_cfg_reg != ap->ability_match_cfg) {
4440 ap->ability_match_cfg = rx_cfg_reg;
4441 ap->ability_match = 0;
4442 ap->ability_match_count = 0;
4443 } else {
4444 if (++ap->ability_match_count > 1) {
4445 ap->ability_match = 1;
4446 ap->ability_match_cfg = rx_cfg_reg;
4447 }
4448 }
4449 if (rx_cfg_reg & ANEG_CFG_ACK)
4450 ap->ack_match = 1;
4451 else
4452 ap->ack_match = 0;
4453
4454 ap->idle_match = 0;
4455 } else {
4456 ap->idle_match = 1;
4457 ap->ability_match_cfg = 0;
4458 ap->ability_match_count = 0;
4459 ap->ability_match = 0;
4460 ap->ack_match = 0;
4461
4462 rx_cfg_reg = 0;
4463 }
4464
4465 ap->rxconfig = rx_cfg_reg;
4466 ret = ANEG_OK;
4467
Matt Carlson33f401a2010-04-05 10:19:27 +00004468 switch (ap->state) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004469 case ANEG_STATE_UNKNOWN:
4470 if (ap->flags & (MR_AN_ENABLE | MR_RESTART_AN))
4471 ap->state = ANEG_STATE_AN_ENABLE;
4472
4473 /* fallthru */
4474 case ANEG_STATE_AN_ENABLE:
4475 ap->flags &= ~(MR_AN_COMPLETE | MR_PAGE_RX);
4476 if (ap->flags & MR_AN_ENABLE) {
4477 ap->link_time = 0;
4478 ap->cur_time = 0;
4479 ap->ability_match_cfg = 0;
4480 ap->ability_match_count = 0;
4481 ap->ability_match = 0;
4482 ap->idle_match = 0;
4483 ap->ack_match = 0;
4484
4485 ap->state = ANEG_STATE_RESTART_INIT;
4486 } else {
4487 ap->state = ANEG_STATE_DISABLE_LINK_OK;
4488 }
4489 break;
4490
4491 case ANEG_STATE_RESTART_INIT:
4492 ap->link_time = ap->cur_time;
4493 ap->flags &= ~(MR_NP_LOADED);
4494 ap->txconfig = 0;
4495 tw32(MAC_TX_AUTO_NEG, 0);
4496 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
4497 tw32_f(MAC_MODE, tp->mac_mode);
4498 udelay(40);
4499
4500 ret = ANEG_TIMER_ENAB;
4501 ap->state = ANEG_STATE_RESTART;
4502
4503 /* fallthru */
4504 case ANEG_STATE_RESTART:
4505 delta = ap->cur_time - ap->link_time;
Matt Carlson859a588792010-04-05 10:19:28 +00004506 if (delta > ANEG_STATE_SETTLE_TIME)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004507 ap->state = ANEG_STATE_ABILITY_DETECT_INIT;
Matt Carlson859a588792010-04-05 10:19:28 +00004508 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004509 ret = ANEG_TIMER_ENAB;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004510 break;
4511
4512 case ANEG_STATE_DISABLE_LINK_OK:
4513 ret = ANEG_DONE;
4514 break;
4515
4516 case ANEG_STATE_ABILITY_DETECT_INIT:
4517 ap->flags &= ~(MR_TOGGLE_TX);
Matt Carlson5be73b42007-12-20 20:09:29 -08004518 ap->txconfig = ANEG_CFG_FD;
4519 flowctrl = tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
4520 if (flowctrl & ADVERTISE_1000XPAUSE)
4521 ap->txconfig |= ANEG_CFG_PS1;
4522 if (flowctrl & ADVERTISE_1000XPSE_ASYM)
4523 ap->txconfig |= ANEG_CFG_PS2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004524 tw32(MAC_TX_AUTO_NEG, ap->txconfig);
4525 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
4526 tw32_f(MAC_MODE, tp->mac_mode);
4527 udelay(40);
4528
4529 ap->state = ANEG_STATE_ABILITY_DETECT;
4530 break;
4531
4532 case ANEG_STATE_ABILITY_DETECT:
Matt Carlson859a588792010-04-05 10:19:28 +00004533 if (ap->ability_match != 0 && ap->rxconfig != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004534 ap->state = ANEG_STATE_ACK_DETECT_INIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004535 break;
4536
4537 case ANEG_STATE_ACK_DETECT_INIT:
4538 ap->txconfig |= ANEG_CFG_ACK;
4539 tw32(MAC_TX_AUTO_NEG, ap->txconfig);
4540 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
4541 tw32_f(MAC_MODE, tp->mac_mode);
4542 udelay(40);
4543
4544 ap->state = ANEG_STATE_ACK_DETECT;
4545
4546 /* fallthru */
4547 case ANEG_STATE_ACK_DETECT:
4548 if (ap->ack_match != 0) {
4549 if ((ap->rxconfig & ~ANEG_CFG_ACK) ==
4550 (ap->ability_match_cfg & ~ANEG_CFG_ACK)) {
4551 ap->state = ANEG_STATE_COMPLETE_ACK_INIT;
4552 } else {
4553 ap->state = ANEG_STATE_AN_ENABLE;
4554 }
4555 } else if (ap->ability_match != 0 &&
4556 ap->rxconfig == 0) {
4557 ap->state = ANEG_STATE_AN_ENABLE;
4558 }
4559 break;
4560
4561 case ANEG_STATE_COMPLETE_ACK_INIT:
4562 if (ap->rxconfig & ANEG_CFG_INVAL) {
4563 ret = ANEG_FAILED;
4564 break;
4565 }
4566 ap->flags &= ~(MR_LP_ADV_FULL_DUPLEX |
4567 MR_LP_ADV_HALF_DUPLEX |
4568 MR_LP_ADV_SYM_PAUSE |
4569 MR_LP_ADV_ASYM_PAUSE |
4570 MR_LP_ADV_REMOTE_FAULT1 |
4571 MR_LP_ADV_REMOTE_FAULT2 |
4572 MR_LP_ADV_NEXT_PAGE |
4573 MR_TOGGLE_RX |
4574 MR_NP_RX);
4575 if (ap->rxconfig & ANEG_CFG_FD)
4576 ap->flags |= MR_LP_ADV_FULL_DUPLEX;
4577 if (ap->rxconfig & ANEG_CFG_HD)
4578 ap->flags |= MR_LP_ADV_HALF_DUPLEX;
4579 if (ap->rxconfig & ANEG_CFG_PS1)
4580 ap->flags |= MR_LP_ADV_SYM_PAUSE;
4581 if (ap->rxconfig & ANEG_CFG_PS2)
4582 ap->flags |= MR_LP_ADV_ASYM_PAUSE;
4583 if (ap->rxconfig & ANEG_CFG_RF1)
4584 ap->flags |= MR_LP_ADV_REMOTE_FAULT1;
4585 if (ap->rxconfig & ANEG_CFG_RF2)
4586 ap->flags |= MR_LP_ADV_REMOTE_FAULT2;
4587 if (ap->rxconfig & ANEG_CFG_NP)
4588 ap->flags |= MR_LP_ADV_NEXT_PAGE;
4589
4590 ap->link_time = ap->cur_time;
4591
4592 ap->flags ^= (MR_TOGGLE_TX);
4593 if (ap->rxconfig & 0x0008)
4594 ap->flags |= MR_TOGGLE_RX;
4595 if (ap->rxconfig & ANEG_CFG_NP)
4596 ap->flags |= MR_NP_RX;
4597 ap->flags |= MR_PAGE_RX;
4598
4599 ap->state = ANEG_STATE_COMPLETE_ACK;
4600 ret = ANEG_TIMER_ENAB;
4601 break;
4602
4603 case ANEG_STATE_COMPLETE_ACK:
4604 if (ap->ability_match != 0 &&
4605 ap->rxconfig == 0) {
4606 ap->state = ANEG_STATE_AN_ENABLE;
4607 break;
4608 }
4609 delta = ap->cur_time - ap->link_time;
4610 if (delta > ANEG_STATE_SETTLE_TIME) {
4611 if (!(ap->flags & (MR_LP_ADV_NEXT_PAGE))) {
4612 ap->state = ANEG_STATE_IDLE_DETECT_INIT;
4613 } else {
4614 if ((ap->txconfig & ANEG_CFG_NP) == 0 &&
4615 !(ap->flags & MR_NP_RX)) {
4616 ap->state = ANEG_STATE_IDLE_DETECT_INIT;
4617 } else {
4618 ret = ANEG_FAILED;
4619 }
4620 }
4621 }
4622 break;
4623
4624 case ANEG_STATE_IDLE_DETECT_INIT:
4625 ap->link_time = ap->cur_time;
4626 tp->mac_mode &= ~MAC_MODE_SEND_CONFIGS;
4627 tw32_f(MAC_MODE, tp->mac_mode);
4628 udelay(40);
4629
4630 ap->state = ANEG_STATE_IDLE_DETECT;
4631 ret = ANEG_TIMER_ENAB;
4632 break;
4633
4634 case ANEG_STATE_IDLE_DETECT:
4635 if (ap->ability_match != 0 &&
4636 ap->rxconfig == 0) {
4637 ap->state = ANEG_STATE_AN_ENABLE;
4638 break;
4639 }
4640 delta = ap->cur_time - ap->link_time;
4641 if (delta > ANEG_STATE_SETTLE_TIME) {
4642 /* XXX another gem from the Broadcom driver :( */
4643 ap->state = ANEG_STATE_LINK_OK;
4644 }
4645 break;
4646
4647 case ANEG_STATE_LINK_OK:
4648 ap->flags |= (MR_AN_COMPLETE | MR_LINK_OK);
4649 ret = ANEG_DONE;
4650 break;
4651
4652 case ANEG_STATE_NEXT_PAGE_WAIT_INIT:
4653 /* ??? unimplemented */
4654 break;
4655
4656 case ANEG_STATE_NEXT_PAGE_WAIT:
4657 /* ??? unimplemented */
4658 break;
4659
4660 default:
4661 ret = ANEG_FAILED;
4662 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07004663 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004664
4665 return ret;
4666}
4667
Matt Carlson5be73b42007-12-20 20:09:29 -08004668static int fiber_autoneg(struct tg3 *tp, u32 *txflags, u32 *rxflags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004669{
4670 int res = 0;
4671 struct tg3_fiber_aneginfo aninfo;
4672 int status = ANEG_FAILED;
4673 unsigned int tick;
4674 u32 tmp;
4675
4676 tw32_f(MAC_TX_AUTO_NEG, 0);
4677
4678 tmp = tp->mac_mode & ~MAC_MODE_PORT_MODE_MASK;
4679 tw32_f(MAC_MODE, tmp | MAC_MODE_PORT_MODE_GMII);
4680 udelay(40);
4681
4682 tw32_f(MAC_MODE, tp->mac_mode | MAC_MODE_SEND_CONFIGS);
4683 udelay(40);
4684
4685 memset(&aninfo, 0, sizeof(aninfo));
4686 aninfo.flags |= MR_AN_ENABLE;
4687 aninfo.state = ANEG_STATE_UNKNOWN;
4688 aninfo.cur_time = 0;
4689 tick = 0;
4690 while (++tick < 195000) {
4691 status = tg3_fiber_aneg_smachine(tp, &aninfo);
4692 if (status == ANEG_DONE || status == ANEG_FAILED)
4693 break;
4694
4695 udelay(1);
4696 }
4697
4698 tp->mac_mode &= ~MAC_MODE_SEND_CONFIGS;
4699 tw32_f(MAC_MODE, tp->mac_mode);
4700 udelay(40);
4701
Matt Carlson5be73b42007-12-20 20:09:29 -08004702 *txflags = aninfo.txconfig;
4703 *rxflags = aninfo.flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004704
4705 if (status == ANEG_DONE &&
4706 (aninfo.flags & (MR_AN_COMPLETE | MR_LINK_OK |
4707 MR_LP_ADV_FULL_DUPLEX)))
4708 res = 1;
4709
4710 return res;
4711}
4712
4713static void tg3_init_bcm8002(struct tg3 *tp)
4714{
4715 u32 mac_status = tr32(MAC_STATUS);
4716 int i;
4717
4718 /* Reset when initting first time or we have a link. */
Joe Perches63c3a662011-04-26 08:12:10 +00004719 if (tg3_flag(tp, INIT_COMPLETE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004720 !(mac_status & MAC_STATUS_PCS_SYNCED))
4721 return;
4722
4723 /* Set PLL lock range. */
4724 tg3_writephy(tp, 0x16, 0x8007);
4725
4726 /* SW reset */
4727 tg3_writephy(tp, MII_BMCR, BMCR_RESET);
4728
4729 /* Wait for reset to complete. */
4730 /* XXX schedule_timeout() ... */
4731 for (i = 0; i < 500; i++)
4732 udelay(10);
4733
4734 /* Config mode; select PMA/Ch 1 regs. */
4735 tg3_writephy(tp, 0x10, 0x8411);
4736
4737 /* Enable auto-lock and comdet, select txclk for tx. */
4738 tg3_writephy(tp, 0x11, 0x0a10);
4739
4740 tg3_writephy(tp, 0x18, 0x00a0);
4741 tg3_writephy(tp, 0x16, 0x41ff);
4742
4743 /* Assert and deassert POR. */
4744 tg3_writephy(tp, 0x13, 0x0400);
4745 udelay(40);
4746 tg3_writephy(tp, 0x13, 0x0000);
4747
4748 tg3_writephy(tp, 0x11, 0x0a50);
4749 udelay(40);
4750 tg3_writephy(tp, 0x11, 0x0a10);
4751
4752 /* Wait for signal to stabilize */
4753 /* XXX schedule_timeout() ... */
4754 for (i = 0; i < 15000; i++)
4755 udelay(10);
4756
4757 /* Deselect the channel register so we can read the PHYID
4758 * later.
4759 */
4760 tg3_writephy(tp, 0x10, 0x8011);
4761}
4762
4763static int tg3_setup_fiber_hw_autoneg(struct tg3 *tp, u32 mac_status)
4764{
Matt Carlson82cd3d12007-12-20 20:09:00 -08004765 u16 flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004766 u32 sg_dig_ctrl, sg_dig_status;
4767 u32 serdes_cfg, expected_sg_dig_ctrl;
4768 int workaround, port_a;
4769 int current_link_up;
4770
4771 serdes_cfg = 0;
4772 expected_sg_dig_ctrl = 0;
4773 workaround = 0;
4774 port_a = 1;
4775 current_link_up = 0;
4776
4777 if (tp->pci_chip_rev_id != CHIPREV_ID_5704_A0 &&
4778 tp->pci_chip_rev_id != CHIPREV_ID_5704_A1) {
4779 workaround = 1;
4780 if (tr32(TG3PCI_DUAL_MAC_CTRL) & DUAL_MAC_CTRL_ID)
4781 port_a = 0;
4782
4783 /* preserve bits 0-11,13,14 for signal pre-emphasis */
4784 /* preserve bits 20-23 for voltage regulator */
4785 serdes_cfg = tr32(MAC_SERDES_CFG) & 0x00f06fff;
4786 }
4787
4788 sg_dig_ctrl = tr32(SG_DIG_CTRL);
4789
4790 if (tp->link_config.autoneg != AUTONEG_ENABLE) {
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004791 if (sg_dig_ctrl & SG_DIG_USING_HW_AUTONEG) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004792 if (workaround) {
4793 u32 val = serdes_cfg;
4794
4795 if (port_a)
4796 val |= 0xc010000;
4797 else
4798 val |= 0x4010000;
4799 tw32_f(MAC_SERDES_CFG, val);
4800 }
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004801
4802 tw32_f(SG_DIG_CTRL, SG_DIG_COMMON_SETUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004803 }
4804 if (mac_status & MAC_STATUS_PCS_SYNCED) {
4805 tg3_setup_flow_control(tp, 0, 0);
4806 current_link_up = 1;
4807 }
4808 goto out;
4809 }
4810
4811 /* Want auto-negotiation. */
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004812 expected_sg_dig_ctrl = SG_DIG_USING_HW_AUTONEG | SG_DIG_COMMON_SETUP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004813
Matt Carlson82cd3d12007-12-20 20:09:00 -08004814 flowctrl = tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
4815 if (flowctrl & ADVERTISE_1000XPAUSE)
4816 expected_sg_dig_ctrl |= SG_DIG_PAUSE_CAP;
4817 if (flowctrl & ADVERTISE_1000XPSE_ASYM)
4818 expected_sg_dig_ctrl |= SG_DIG_ASYM_PAUSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004819
4820 if (sg_dig_ctrl != expected_sg_dig_ctrl) {
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004821 if ((tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT) &&
Michael Chan3d3ebe72006-09-27 15:59:15 -07004822 tp->serdes_counter &&
4823 ((mac_status & (MAC_STATUS_PCS_SYNCED |
4824 MAC_STATUS_RCVD_CFG)) ==
4825 MAC_STATUS_PCS_SYNCED)) {
4826 tp->serdes_counter--;
4827 current_link_up = 1;
4828 goto out;
4829 }
4830restart_autoneg:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004831 if (workaround)
4832 tw32_f(MAC_SERDES_CFG, serdes_cfg | 0xc011000);
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004833 tw32_f(SG_DIG_CTRL, expected_sg_dig_ctrl | SG_DIG_SOFT_RESET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004834 udelay(5);
4835 tw32_f(SG_DIG_CTRL, expected_sg_dig_ctrl);
4836
Michael Chan3d3ebe72006-09-27 15:59:15 -07004837 tp->serdes_counter = SERDES_AN_TIMEOUT_5704S;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004838 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004839 } else if (mac_status & (MAC_STATUS_PCS_SYNCED |
4840 MAC_STATUS_SIGNAL_DET)) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07004841 sg_dig_status = tr32(SG_DIG_STATUS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004842 mac_status = tr32(MAC_STATUS);
4843
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004844 if ((sg_dig_status & SG_DIG_AUTONEG_COMPLETE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004845 (mac_status & MAC_STATUS_PCS_SYNCED)) {
Matt Carlson82cd3d12007-12-20 20:09:00 -08004846 u32 local_adv = 0, remote_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004847
Matt Carlson82cd3d12007-12-20 20:09:00 -08004848 if (sg_dig_ctrl & SG_DIG_PAUSE_CAP)
4849 local_adv |= ADVERTISE_1000XPAUSE;
4850 if (sg_dig_ctrl & SG_DIG_ASYM_PAUSE)
4851 local_adv |= ADVERTISE_1000XPSE_ASYM;
4852
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004853 if (sg_dig_status & SG_DIG_PARTNER_PAUSE_CAPABLE)
Matt Carlson82cd3d12007-12-20 20:09:00 -08004854 remote_adv |= LPA_1000XPAUSE;
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004855 if (sg_dig_status & SG_DIG_PARTNER_ASYM_PAUSE)
Matt Carlson82cd3d12007-12-20 20:09:00 -08004856 remote_adv |= LPA_1000XPAUSE_ASYM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004857
Matt Carlson859edb22011-12-08 14:40:16 +00004858 tp->link_config.rmt_adv =
4859 mii_adv_to_ethtool_adv_x(remote_adv);
4860
Linus Torvalds1da177e2005-04-16 15:20:36 -07004861 tg3_setup_flow_control(tp, local_adv, remote_adv);
4862 current_link_up = 1;
Michael Chan3d3ebe72006-09-27 15:59:15 -07004863 tp->serdes_counter = 0;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004864 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004865 } else if (!(sg_dig_status & SG_DIG_AUTONEG_COMPLETE)) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07004866 if (tp->serdes_counter)
4867 tp->serdes_counter--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004868 else {
4869 if (workaround) {
4870 u32 val = serdes_cfg;
4871
4872 if (port_a)
4873 val |= 0xc010000;
4874 else
4875 val |= 0x4010000;
4876
4877 tw32_f(MAC_SERDES_CFG, val);
4878 }
4879
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004880 tw32_f(SG_DIG_CTRL, SG_DIG_COMMON_SETUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004881 udelay(40);
4882
4883 /* Link parallel detection - link is up */
4884 /* only if we have PCS_SYNC and not */
4885 /* receiving config code words */
4886 mac_status = tr32(MAC_STATUS);
4887 if ((mac_status & MAC_STATUS_PCS_SYNCED) &&
4888 !(mac_status & MAC_STATUS_RCVD_CFG)) {
4889 tg3_setup_flow_control(tp, 0, 0);
4890 current_link_up = 1;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004891 tp->phy_flags |=
4892 TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan3d3ebe72006-09-27 15:59:15 -07004893 tp->serdes_counter =
4894 SERDES_PARALLEL_DET_TIMEOUT;
4895 } else
4896 goto restart_autoneg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004897 }
4898 }
Michael Chan3d3ebe72006-09-27 15:59:15 -07004899 } else {
4900 tp->serdes_counter = SERDES_AN_TIMEOUT_5704S;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004901 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004902 }
4903
4904out:
4905 return current_link_up;
4906}
4907
4908static int tg3_setup_fiber_by_hand(struct tg3 *tp, u32 mac_status)
4909{
4910 int current_link_up = 0;
4911
Michael Chan5cf64b8a2007-05-05 12:11:21 -07004912 if (!(mac_status & MAC_STATUS_PCS_SYNCED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004913 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004914
4915 if (tp->link_config.autoneg == AUTONEG_ENABLE) {
Matt Carlson5be73b42007-12-20 20:09:29 -08004916 u32 txflags, rxflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004917 int i;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04004918
Matt Carlson5be73b42007-12-20 20:09:29 -08004919 if (fiber_autoneg(tp, &txflags, &rxflags)) {
4920 u32 local_adv = 0, remote_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004921
Matt Carlson5be73b42007-12-20 20:09:29 -08004922 if (txflags & ANEG_CFG_PS1)
4923 local_adv |= ADVERTISE_1000XPAUSE;
4924 if (txflags & ANEG_CFG_PS2)
4925 local_adv |= ADVERTISE_1000XPSE_ASYM;
4926
4927 if (rxflags & MR_LP_ADV_SYM_PAUSE)
4928 remote_adv |= LPA_1000XPAUSE;
4929 if (rxflags & MR_LP_ADV_ASYM_PAUSE)
4930 remote_adv |= LPA_1000XPAUSE_ASYM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004931
Matt Carlson859edb22011-12-08 14:40:16 +00004932 tp->link_config.rmt_adv =
4933 mii_adv_to_ethtool_adv_x(remote_adv);
4934
Linus Torvalds1da177e2005-04-16 15:20:36 -07004935 tg3_setup_flow_control(tp, local_adv, remote_adv);
4936
Linus Torvalds1da177e2005-04-16 15:20:36 -07004937 current_link_up = 1;
4938 }
4939 for (i = 0; i < 30; i++) {
4940 udelay(20);
4941 tw32_f(MAC_STATUS,
4942 (MAC_STATUS_SYNC_CHANGED |
4943 MAC_STATUS_CFG_CHANGED));
4944 udelay(40);
4945 if ((tr32(MAC_STATUS) &
4946 (MAC_STATUS_SYNC_CHANGED |
4947 MAC_STATUS_CFG_CHANGED)) == 0)
4948 break;
4949 }
4950
4951 mac_status = tr32(MAC_STATUS);
4952 if (current_link_up == 0 &&
4953 (mac_status & MAC_STATUS_PCS_SYNCED) &&
4954 !(mac_status & MAC_STATUS_RCVD_CFG))
4955 current_link_up = 1;
4956 } else {
Matt Carlson5be73b42007-12-20 20:09:29 -08004957 tg3_setup_flow_control(tp, 0, 0);
4958
Linus Torvalds1da177e2005-04-16 15:20:36 -07004959 /* Forcing 1000FD link up. */
4960 current_link_up = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004961
4962 tw32_f(MAC_MODE, (tp->mac_mode | MAC_MODE_SEND_CONFIGS));
4963 udelay(40);
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07004964
4965 tw32_f(MAC_MODE, tp->mac_mode);
4966 udelay(40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004967 }
4968
4969out:
4970 return current_link_up;
4971}
4972
4973static int tg3_setup_fiber_phy(struct tg3 *tp, int force_reset)
4974{
4975 u32 orig_pause_cfg;
4976 u16 orig_active_speed;
4977 u8 orig_active_duplex;
4978 u32 mac_status;
4979 int current_link_up;
4980 int i;
4981
Matt Carlson8d018622007-12-20 20:05:44 -08004982 orig_pause_cfg = tp->link_config.active_flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004983 orig_active_speed = tp->link_config.active_speed;
4984 orig_active_duplex = tp->link_config.active_duplex;
4985
Joe Perches63c3a662011-04-26 08:12:10 +00004986 if (!tg3_flag(tp, HW_AUTONEG) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004987 netif_carrier_ok(tp->dev) &&
Joe Perches63c3a662011-04-26 08:12:10 +00004988 tg3_flag(tp, INIT_COMPLETE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004989 mac_status = tr32(MAC_STATUS);
4990 mac_status &= (MAC_STATUS_PCS_SYNCED |
4991 MAC_STATUS_SIGNAL_DET |
4992 MAC_STATUS_CFG_CHANGED |
4993 MAC_STATUS_RCVD_CFG);
4994 if (mac_status == (MAC_STATUS_PCS_SYNCED |
4995 MAC_STATUS_SIGNAL_DET)) {
4996 tw32_f(MAC_STATUS, (MAC_STATUS_SYNC_CHANGED |
4997 MAC_STATUS_CFG_CHANGED));
4998 return 0;
4999 }
5000 }
5001
5002 tw32_f(MAC_TX_AUTO_NEG, 0);
5003
5004 tp->mac_mode &= ~(MAC_MODE_PORT_MODE_MASK | MAC_MODE_HALF_DUPLEX);
5005 tp->mac_mode |= MAC_MODE_PORT_MODE_TBI;
5006 tw32_f(MAC_MODE, tp->mac_mode);
5007 udelay(40);
5008
Matt Carlson79eb6902010-02-17 15:17:03 +00005009 if (tp->phy_id == TG3_PHY_ID_BCM8002)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005010 tg3_init_bcm8002(tp);
5011
5012 /* Enable link change event even when serdes polling. */
5013 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
5014 udelay(40);
5015
5016 current_link_up = 0;
Matt Carlson859edb22011-12-08 14:40:16 +00005017 tp->link_config.rmt_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005018 mac_status = tr32(MAC_STATUS);
5019
Joe Perches63c3a662011-04-26 08:12:10 +00005020 if (tg3_flag(tp, HW_AUTONEG))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005021 current_link_up = tg3_setup_fiber_hw_autoneg(tp, mac_status);
5022 else
5023 current_link_up = tg3_setup_fiber_by_hand(tp, mac_status);
5024
Matt Carlson898a56f2009-08-28 14:02:40 +00005025 tp->napi[0].hw_status->status =
Linus Torvalds1da177e2005-04-16 15:20:36 -07005026 (SD_STATUS_UPDATED |
Matt Carlson898a56f2009-08-28 14:02:40 +00005027 (tp->napi[0].hw_status->status & ~SD_STATUS_LINK_CHG));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005028
5029 for (i = 0; i < 100; i++) {
5030 tw32_f(MAC_STATUS, (MAC_STATUS_SYNC_CHANGED |
5031 MAC_STATUS_CFG_CHANGED));
5032 udelay(5);
5033 if ((tr32(MAC_STATUS) & (MAC_STATUS_SYNC_CHANGED |
Michael Chan3d3ebe72006-09-27 15:59:15 -07005034 MAC_STATUS_CFG_CHANGED |
5035 MAC_STATUS_LNKSTATE_CHANGED)) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005036 break;
5037 }
5038
5039 mac_status = tr32(MAC_STATUS);
5040 if ((mac_status & MAC_STATUS_PCS_SYNCED) == 0) {
5041 current_link_up = 0;
Michael Chan3d3ebe72006-09-27 15:59:15 -07005042 if (tp->link_config.autoneg == AUTONEG_ENABLE &&
5043 tp->serdes_counter == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005044 tw32_f(MAC_MODE, (tp->mac_mode |
5045 MAC_MODE_SEND_CONFIGS));
5046 udelay(1);
5047 tw32_f(MAC_MODE, tp->mac_mode);
5048 }
5049 }
5050
5051 if (current_link_up == 1) {
5052 tp->link_config.active_speed = SPEED_1000;
5053 tp->link_config.active_duplex = DUPLEX_FULL;
5054 tw32(MAC_LED_CTRL, (tp->led_ctrl |
5055 LED_CTRL_LNKLED_OVERRIDE |
5056 LED_CTRL_1000MBPS_ON));
5057 } else {
Matt Carlsone7405222012-02-13 15:20:16 +00005058 tp->link_config.active_speed = SPEED_UNKNOWN;
5059 tp->link_config.active_duplex = DUPLEX_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005060 tw32(MAC_LED_CTRL, (tp->led_ctrl |
5061 LED_CTRL_LNKLED_OVERRIDE |
5062 LED_CTRL_TRAFFIC_OVERRIDE));
5063 }
5064
5065 if (current_link_up != netif_carrier_ok(tp->dev)) {
5066 if (current_link_up)
5067 netif_carrier_on(tp->dev);
5068 else
5069 netif_carrier_off(tp->dev);
5070 tg3_link_report(tp);
5071 } else {
Matt Carlson8d018622007-12-20 20:05:44 -08005072 u32 now_pause_cfg = tp->link_config.active_flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005073 if (orig_pause_cfg != now_pause_cfg ||
5074 orig_active_speed != tp->link_config.active_speed ||
5075 orig_active_duplex != tp->link_config.active_duplex)
5076 tg3_link_report(tp);
5077 }
5078
5079 return 0;
5080}
5081
Michael Chan747e8f82005-07-25 12:33:22 -07005082static int tg3_setup_fiber_mii_phy(struct tg3 *tp, int force_reset)
5083{
5084 int current_link_up, err = 0;
5085 u32 bmsr, bmcr;
5086 u16 current_speed;
5087 u8 current_duplex;
Matt Carlsonef167e22007-12-20 20:10:01 -08005088 u32 local_adv, remote_adv;
Michael Chan747e8f82005-07-25 12:33:22 -07005089
5090 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
5091 tw32_f(MAC_MODE, tp->mac_mode);
5092 udelay(40);
5093
5094 tw32(MAC_EVENT, 0);
5095
5096 tw32_f(MAC_STATUS,
5097 (MAC_STATUS_SYNC_CHANGED |
5098 MAC_STATUS_CFG_CHANGED |
5099 MAC_STATUS_MI_COMPLETION |
5100 MAC_STATUS_LNKSTATE_CHANGED));
5101 udelay(40);
5102
5103 if (force_reset)
5104 tg3_phy_reset(tp);
5105
5106 current_link_up = 0;
Matt Carlsone7405222012-02-13 15:20:16 +00005107 current_speed = SPEED_UNKNOWN;
5108 current_duplex = DUPLEX_UNKNOWN;
Matt Carlson859edb22011-12-08 14:40:16 +00005109 tp->link_config.rmt_adv = 0;
Michael Chan747e8f82005-07-25 12:33:22 -07005110
5111 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
5112 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
Michael Chand4d2c552006-03-20 17:47:20 -08005113 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
5114 if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
5115 bmsr |= BMSR_LSTATUS;
5116 else
5117 bmsr &= ~BMSR_LSTATUS;
5118 }
Michael Chan747e8f82005-07-25 12:33:22 -07005119
5120 err |= tg3_readphy(tp, MII_BMCR, &bmcr);
5121
5122 if ((tp->link_config.autoneg == AUTONEG_ENABLE) && !force_reset &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005123 (tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT)) {
Michael Chan747e8f82005-07-25 12:33:22 -07005124 /* do nothing, just check for link up at the end */
5125 } else if (tp->link_config.autoneg == AUTONEG_ENABLE) {
Matt Carlson28011cf2011-11-16 18:36:59 -05005126 u32 adv, newadv;
Michael Chan747e8f82005-07-25 12:33:22 -07005127
5128 err |= tg3_readphy(tp, MII_ADVERTISE, &adv);
Matt Carlson28011cf2011-11-16 18:36:59 -05005129 newadv = adv & ~(ADVERTISE_1000XFULL | ADVERTISE_1000XHALF |
5130 ADVERTISE_1000XPAUSE |
5131 ADVERTISE_1000XPSE_ASYM |
5132 ADVERTISE_SLCT);
Michael Chan747e8f82005-07-25 12:33:22 -07005133
Matt Carlson28011cf2011-11-16 18:36:59 -05005134 newadv |= tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
Matt Carlson37f07022011-11-17 14:30:55 +00005135 newadv |= ethtool_adv_to_mii_adv_x(tp->link_config.advertising);
Michael Chan747e8f82005-07-25 12:33:22 -07005136
Matt Carlson28011cf2011-11-16 18:36:59 -05005137 if ((newadv != adv) || !(bmcr & BMCR_ANENABLE)) {
5138 tg3_writephy(tp, MII_ADVERTISE, newadv);
Michael Chan747e8f82005-07-25 12:33:22 -07005139 bmcr |= BMCR_ANENABLE | BMCR_ANRESTART;
5140 tg3_writephy(tp, MII_BMCR, bmcr);
5141
5142 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
Michael Chan3d3ebe72006-09-27 15:59:15 -07005143 tp->serdes_counter = SERDES_AN_TIMEOUT_5714S;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005144 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07005145
5146 return err;
5147 }
5148 } else {
5149 u32 new_bmcr;
5150
5151 bmcr &= ~BMCR_SPEED1000;
5152 new_bmcr = bmcr & ~(BMCR_ANENABLE | BMCR_FULLDPLX);
5153
5154 if (tp->link_config.duplex == DUPLEX_FULL)
5155 new_bmcr |= BMCR_FULLDPLX;
5156
5157 if (new_bmcr != bmcr) {
5158 /* BMCR_SPEED1000 is a reserved bit that needs
5159 * to be set on write.
5160 */
5161 new_bmcr |= BMCR_SPEED1000;
5162
5163 /* Force a linkdown */
5164 if (netif_carrier_ok(tp->dev)) {
5165 u32 adv;
5166
5167 err |= tg3_readphy(tp, MII_ADVERTISE, &adv);
5168 adv &= ~(ADVERTISE_1000XFULL |
5169 ADVERTISE_1000XHALF |
5170 ADVERTISE_SLCT);
5171 tg3_writephy(tp, MII_ADVERTISE, adv);
5172 tg3_writephy(tp, MII_BMCR, bmcr |
5173 BMCR_ANRESTART |
5174 BMCR_ANENABLE);
5175 udelay(10);
5176 netif_carrier_off(tp->dev);
5177 }
5178 tg3_writephy(tp, MII_BMCR, new_bmcr);
5179 bmcr = new_bmcr;
5180 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
5181 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
Michael Chand4d2c552006-03-20 17:47:20 -08005182 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
5183 ASIC_REV_5714) {
5184 if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
5185 bmsr |= BMSR_LSTATUS;
5186 else
5187 bmsr &= ~BMSR_LSTATUS;
5188 }
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005189 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07005190 }
5191 }
5192
5193 if (bmsr & BMSR_LSTATUS) {
5194 current_speed = SPEED_1000;
5195 current_link_up = 1;
5196 if (bmcr & BMCR_FULLDPLX)
5197 current_duplex = DUPLEX_FULL;
5198 else
5199 current_duplex = DUPLEX_HALF;
5200
Matt Carlsonef167e22007-12-20 20:10:01 -08005201 local_adv = 0;
5202 remote_adv = 0;
5203
Michael Chan747e8f82005-07-25 12:33:22 -07005204 if (bmcr & BMCR_ANENABLE) {
Matt Carlsonef167e22007-12-20 20:10:01 -08005205 u32 common;
Michael Chan747e8f82005-07-25 12:33:22 -07005206
5207 err |= tg3_readphy(tp, MII_ADVERTISE, &local_adv);
5208 err |= tg3_readphy(tp, MII_LPA, &remote_adv);
5209 common = local_adv & remote_adv;
5210 if (common & (ADVERTISE_1000XHALF |
5211 ADVERTISE_1000XFULL)) {
5212 if (common & ADVERTISE_1000XFULL)
5213 current_duplex = DUPLEX_FULL;
5214 else
5215 current_duplex = DUPLEX_HALF;
Matt Carlson859edb22011-12-08 14:40:16 +00005216
5217 tp->link_config.rmt_adv =
5218 mii_adv_to_ethtool_adv_x(remote_adv);
Joe Perches63c3a662011-04-26 08:12:10 +00005219 } else if (!tg3_flag(tp, 5780_CLASS)) {
Matt Carlson57d8b882010-06-05 17:24:35 +00005220 /* Link is up via parallel detect */
Matt Carlson859a588792010-04-05 10:19:28 +00005221 } else {
Michael Chan747e8f82005-07-25 12:33:22 -07005222 current_link_up = 0;
Matt Carlson859a588792010-04-05 10:19:28 +00005223 }
Michael Chan747e8f82005-07-25 12:33:22 -07005224 }
5225 }
5226
Matt Carlsonef167e22007-12-20 20:10:01 -08005227 if (current_link_up == 1 && current_duplex == DUPLEX_FULL)
5228 tg3_setup_flow_control(tp, local_adv, remote_adv);
5229
Michael Chan747e8f82005-07-25 12:33:22 -07005230 tp->mac_mode &= ~MAC_MODE_HALF_DUPLEX;
5231 if (tp->link_config.active_duplex == DUPLEX_HALF)
5232 tp->mac_mode |= MAC_MODE_HALF_DUPLEX;
5233
5234 tw32_f(MAC_MODE, tp->mac_mode);
5235 udelay(40);
5236
5237 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
5238
5239 tp->link_config.active_speed = current_speed;
5240 tp->link_config.active_duplex = current_duplex;
5241
5242 if (current_link_up != netif_carrier_ok(tp->dev)) {
5243 if (current_link_up)
5244 netif_carrier_on(tp->dev);
5245 else {
5246 netif_carrier_off(tp->dev);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005247 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07005248 }
5249 tg3_link_report(tp);
5250 }
5251 return err;
5252}
5253
5254static void tg3_serdes_parallel_detect(struct tg3 *tp)
5255{
Michael Chan3d3ebe72006-09-27 15:59:15 -07005256 if (tp->serdes_counter) {
Michael Chan747e8f82005-07-25 12:33:22 -07005257 /* Give autoneg time to complete. */
Michael Chan3d3ebe72006-09-27 15:59:15 -07005258 tp->serdes_counter--;
Michael Chan747e8f82005-07-25 12:33:22 -07005259 return;
5260 }
Matt Carlsonc6cdf432010-04-05 10:19:26 +00005261
Michael Chan747e8f82005-07-25 12:33:22 -07005262 if (!netif_carrier_ok(tp->dev) &&
5263 (tp->link_config.autoneg == AUTONEG_ENABLE)) {
5264 u32 bmcr;
5265
5266 tg3_readphy(tp, MII_BMCR, &bmcr);
5267 if (bmcr & BMCR_ANENABLE) {
5268 u32 phy1, phy2;
5269
5270 /* Select shadow register 0x1f */
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00005271 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x7c00);
5272 tg3_readphy(tp, MII_TG3_MISC_SHDW, &phy1);
Michael Chan747e8f82005-07-25 12:33:22 -07005273
5274 /* Select expansion interrupt status register */
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00005275 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
5276 MII_TG3_DSP_EXP1_INT_STAT);
5277 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &phy2);
5278 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &phy2);
Michael Chan747e8f82005-07-25 12:33:22 -07005279
5280 if ((phy1 & 0x10) && !(phy2 & 0x20)) {
5281 /* We have signal detect and not receiving
5282 * config code words, link is up by parallel
5283 * detection.
5284 */
5285
5286 bmcr &= ~BMCR_ANENABLE;
5287 bmcr |= BMCR_SPEED1000 | BMCR_FULLDPLX;
5288 tg3_writephy(tp, MII_BMCR, bmcr);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005289 tp->phy_flags |= TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07005290 }
5291 }
Matt Carlson859a588792010-04-05 10:19:28 +00005292 } else if (netif_carrier_ok(tp->dev) &&
5293 (tp->link_config.autoneg == AUTONEG_ENABLE) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005294 (tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT)) {
Michael Chan747e8f82005-07-25 12:33:22 -07005295 u32 phy2;
5296
5297 /* Select expansion interrupt status register */
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00005298 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
5299 MII_TG3_DSP_EXP1_INT_STAT);
5300 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &phy2);
Michael Chan747e8f82005-07-25 12:33:22 -07005301 if (phy2 & 0x20) {
5302 u32 bmcr;
5303
5304 /* Config code words received, turn on autoneg. */
5305 tg3_readphy(tp, MII_BMCR, &bmcr);
5306 tg3_writephy(tp, MII_BMCR, bmcr | BMCR_ANENABLE);
5307
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005308 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07005309
5310 }
5311 }
5312}
5313
Linus Torvalds1da177e2005-04-16 15:20:36 -07005314static int tg3_setup_phy(struct tg3 *tp, int force_reset)
5315{
Matt Carlsonf2096f92011-04-05 14:22:48 +00005316 u32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005317 int err;
5318
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005319 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005320 err = tg3_setup_fiber_phy(tp, force_reset);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005321 else if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
Michael Chan747e8f82005-07-25 12:33:22 -07005322 err = tg3_setup_fiber_mii_phy(tp, force_reset);
Matt Carlson859a588792010-04-05 10:19:28 +00005323 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07005324 err = tg3_setup_copper_phy(tp, force_reset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005325
Matt Carlsonbcb37f62008-11-03 16:52:09 -08005326 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX) {
Matt Carlsonf2096f92011-04-05 14:22:48 +00005327 u32 scale;
Matt Carlsonaa6c91f2007-11-12 21:18:04 -08005328
5329 val = tr32(TG3_CPMU_CLCK_STAT) & CPMU_CLCK_STAT_MAC_CLCK_MASK;
5330 if (val == CPMU_CLCK_STAT_MAC_CLCK_62_5)
5331 scale = 65;
5332 else if (val == CPMU_CLCK_STAT_MAC_CLCK_6_25)
5333 scale = 6;
5334 else
5335 scale = 12;
5336
5337 val = tr32(GRC_MISC_CFG) & ~GRC_MISC_CFG_PRESCALAR_MASK;
5338 val |= (scale << GRC_MISC_CFG_PRESCALAR_SHIFT);
5339 tw32(GRC_MISC_CFG, val);
5340 }
5341
Matt Carlsonf2096f92011-04-05 14:22:48 +00005342 val = (2 << TX_LENGTHS_IPG_CRS_SHIFT) |
5343 (6 << TX_LENGTHS_IPG_SHIFT);
5344 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
5345 val |= tr32(MAC_TX_LENGTHS) &
5346 (TX_LENGTHS_JMB_FRM_LEN_MSK |
5347 TX_LENGTHS_CNT_DWN_VAL_MSK);
5348
Linus Torvalds1da177e2005-04-16 15:20:36 -07005349 if (tp->link_config.active_speed == SPEED_1000 &&
5350 tp->link_config.active_duplex == DUPLEX_HALF)
Matt Carlsonf2096f92011-04-05 14:22:48 +00005351 tw32(MAC_TX_LENGTHS, val |
5352 (0xff << TX_LENGTHS_SLOT_TIME_SHIFT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005353 else
Matt Carlsonf2096f92011-04-05 14:22:48 +00005354 tw32(MAC_TX_LENGTHS, val |
5355 (32 << TX_LENGTHS_SLOT_TIME_SHIFT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005356
Joe Perches63c3a662011-04-26 08:12:10 +00005357 if (!tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005358 if (netif_carrier_ok(tp->dev)) {
5359 tw32(HOSTCC_STAT_COAL_TICKS,
David S. Miller15f98502005-05-18 22:49:26 -07005360 tp->coal.stats_block_coalesce_usecs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005361 } else {
5362 tw32(HOSTCC_STAT_COAL_TICKS, 0);
5363 }
5364 }
5365
Joe Perches63c3a662011-04-26 08:12:10 +00005366 if (tg3_flag(tp, ASPM_WORKAROUND)) {
Matt Carlsonf2096f92011-04-05 14:22:48 +00005367 val = tr32(PCIE_PWR_MGMT_THRESH);
Matt Carlson8ed5d972007-05-07 00:25:49 -07005368 if (!netif_carrier_ok(tp->dev))
5369 val = (val & ~PCIE_PWR_MGMT_L1_THRESH_MSK) |
5370 tp->pwrmgmt_thresh;
5371 else
5372 val |= PCIE_PWR_MGMT_L1_THRESH_MSK;
5373 tw32(PCIE_PWR_MGMT_THRESH, val);
5374 }
5375
Linus Torvalds1da177e2005-04-16 15:20:36 -07005376 return err;
5377}
5378
Matt Carlson66cfd1b2010-09-30 10:34:30 +00005379static inline int tg3_irq_sync(struct tg3 *tp)
5380{
5381 return tp->irq_sync;
5382}
5383
Matt Carlson97bd8e42011-04-13 11:05:04 +00005384static inline void tg3_rd32_loop(struct tg3 *tp, u32 *dst, u32 off, u32 len)
5385{
5386 int i;
5387
5388 dst = (u32 *)((u8 *)dst + off);
5389 for (i = 0; i < len; i += sizeof(u32))
5390 *dst++ = tr32(off + i);
5391}
5392
5393static void tg3_dump_legacy_regs(struct tg3 *tp, u32 *regs)
5394{
5395 tg3_rd32_loop(tp, regs, TG3PCI_VENDOR, 0xb0);
5396 tg3_rd32_loop(tp, regs, MAILBOX_INTERRUPT_0, 0x200);
5397 tg3_rd32_loop(tp, regs, MAC_MODE, 0x4f0);
5398 tg3_rd32_loop(tp, regs, SNDDATAI_MODE, 0xe0);
5399 tg3_rd32_loop(tp, regs, SNDDATAC_MODE, 0x04);
5400 tg3_rd32_loop(tp, regs, SNDBDS_MODE, 0x80);
5401 tg3_rd32_loop(tp, regs, SNDBDI_MODE, 0x48);
5402 tg3_rd32_loop(tp, regs, SNDBDC_MODE, 0x04);
5403 tg3_rd32_loop(tp, regs, RCVLPC_MODE, 0x20);
5404 tg3_rd32_loop(tp, regs, RCVLPC_SELLST_BASE, 0x15c);
5405 tg3_rd32_loop(tp, regs, RCVDBDI_MODE, 0x0c);
5406 tg3_rd32_loop(tp, regs, RCVDBDI_JUMBO_BD, 0x3c);
5407 tg3_rd32_loop(tp, regs, RCVDBDI_BD_PROD_IDX_0, 0x44);
5408 tg3_rd32_loop(tp, regs, RCVDCC_MODE, 0x04);
5409 tg3_rd32_loop(tp, regs, RCVBDI_MODE, 0x20);
5410 tg3_rd32_loop(tp, regs, RCVCC_MODE, 0x14);
5411 tg3_rd32_loop(tp, regs, RCVLSC_MODE, 0x08);
5412 tg3_rd32_loop(tp, regs, MBFREE_MODE, 0x08);
5413 tg3_rd32_loop(tp, regs, HOSTCC_MODE, 0x100);
5414
Joe Perches63c3a662011-04-26 08:12:10 +00005415 if (tg3_flag(tp, SUPPORT_MSIX))
Matt Carlson97bd8e42011-04-13 11:05:04 +00005416 tg3_rd32_loop(tp, regs, HOSTCC_RXCOL_TICKS_VEC1, 0x180);
5417
5418 tg3_rd32_loop(tp, regs, MEMARB_MODE, 0x10);
5419 tg3_rd32_loop(tp, regs, BUFMGR_MODE, 0x58);
5420 tg3_rd32_loop(tp, regs, RDMAC_MODE, 0x08);
5421 tg3_rd32_loop(tp, regs, WDMAC_MODE, 0x08);
5422 tg3_rd32_loop(tp, regs, RX_CPU_MODE, 0x04);
5423 tg3_rd32_loop(tp, regs, RX_CPU_STATE, 0x04);
5424 tg3_rd32_loop(tp, regs, RX_CPU_PGMCTR, 0x04);
5425 tg3_rd32_loop(tp, regs, RX_CPU_HWBKPT, 0x04);
5426
Joe Perches63c3a662011-04-26 08:12:10 +00005427 if (!tg3_flag(tp, 5705_PLUS)) {
Matt Carlson97bd8e42011-04-13 11:05:04 +00005428 tg3_rd32_loop(tp, regs, TX_CPU_MODE, 0x04);
5429 tg3_rd32_loop(tp, regs, TX_CPU_STATE, 0x04);
5430 tg3_rd32_loop(tp, regs, TX_CPU_PGMCTR, 0x04);
5431 }
5432
5433 tg3_rd32_loop(tp, regs, GRCMBOX_INTERRUPT_0, 0x110);
5434 tg3_rd32_loop(tp, regs, FTQ_RESET, 0x120);
5435 tg3_rd32_loop(tp, regs, MSGINT_MODE, 0x0c);
5436 tg3_rd32_loop(tp, regs, DMAC_MODE, 0x04);
5437 tg3_rd32_loop(tp, regs, GRC_MODE, 0x4c);
5438
Joe Perches63c3a662011-04-26 08:12:10 +00005439 if (tg3_flag(tp, NVRAM))
Matt Carlson97bd8e42011-04-13 11:05:04 +00005440 tg3_rd32_loop(tp, regs, NVRAM_CMD, 0x24);
5441}
5442
5443static void tg3_dump_state(struct tg3 *tp)
5444{
5445 int i;
5446 u32 *regs;
5447
5448 regs = kzalloc(TG3_REG_BLK_SIZE, GFP_ATOMIC);
5449 if (!regs) {
5450 netdev_err(tp->dev, "Failed allocating register dump buffer\n");
5451 return;
5452 }
5453
Joe Perches63c3a662011-04-26 08:12:10 +00005454 if (tg3_flag(tp, PCI_EXPRESS)) {
Matt Carlson97bd8e42011-04-13 11:05:04 +00005455 /* Read up to but not including private PCI registers */
5456 for (i = 0; i < TG3_PCIE_TLDLPL_PORT; i += sizeof(u32))
5457 regs[i / sizeof(u32)] = tr32(i);
5458 } else
5459 tg3_dump_legacy_regs(tp, regs);
5460
5461 for (i = 0; i < TG3_REG_BLK_SIZE / sizeof(u32); i += 4) {
5462 if (!regs[i + 0] && !regs[i + 1] &&
5463 !regs[i + 2] && !regs[i + 3])
5464 continue;
5465
5466 netdev_err(tp->dev, "0x%08x: 0x%08x, 0x%08x, 0x%08x, 0x%08x\n",
5467 i * 4,
5468 regs[i + 0], regs[i + 1], regs[i + 2], regs[i + 3]);
5469 }
5470
5471 kfree(regs);
5472
5473 for (i = 0; i < tp->irq_cnt; i++) {
5474 struct tg3_napi *tnapi = &tp->napi[i];
5475
5476 /* SW status block */
5477 netdev_err(tp->dev,
5478 "%d: Host status block [%08x:%08x:(%04x:%04x:%04x):(%04x:%04x)]\n",
5479 i,
5480 tnapi->hw_status->status,
5481 tnapi->hw_status->status_tag,
5482 tnapi->hw_status->rx_jumbo_consumer,
5483 tnapi->hw_status->rx_consumer,
5484 tnapi->hw_status->rx_mini_consumer,
5485 tnapi->hw_status->idx[0].rx_producer,
5486 tnapi->hw_status->idx[0].tx_consumer);
5487
5488 netdev_err(tp->dev,
5489 "%d: NAPI info [%08x:%08x:(%04x:%04x:%04x):%04x:(%04x:%04x:%04x:%04x)]\n",
5490 i,
5491 tnapi->last_tag, tnapi->last_irq_tag,
5492 tnapi->tx_prod, tnapi->tx_cons, tnapi->tx_pending,
5493 tnapi->rx_rcb_ptr,
5494 tnapi->prodring.rx_std_prod_idx,
5495 tnapi->prodring.rx_std_cons_idx,
5496 tnapi->prodring.rx_jmb_prod_idx,
5497 tnapi->prodring.rx_jmb_cons_idx);
5498 }
5499}
5500
Michael Chandf3e6542006-05-26 17:48:07 -07005501/* This is called whenever we suspect that the system chipset is re-
5502 * ordering the sequence of MMIO to the tx send mailbox. The symptom
5503 * is bogus tx completions. We try to recover by setting the
5504 * TG3_FLAG_MBOX_WRITE_REORDER flag and resetting the chip later
5505 * in the workqueue.
5506 */
5507static void tg3_tx_recover(struct tg3 *tp)
5508{
Joe Perches63c3a662011-04-26 08:12:10 +00005509 BUG_ON(tg3_flag(tp, MBOX_WRITE_REORDER) ||
Michael Chandf3e6542006-05-26 17:48:07 -07005510 tp->write32_tx_mbox == tg3_write_indirect_mbox);
5511
Matt Carlson5129c3a2010-04-05 10:19:23 +00005512 netdev_warn(tp->dev,
5513 "The system may be re-ordering memory-mapped I/O "
5514 "cycles to the network device, attempting to recover. "
5515 "Please report the problem to the driver maintainer "
5516 "and include system chipset information.\n");
Michael Chandf3e6542006-05-26 17:48:07 -07005517
5518 spin_lock(&tp->lock);
Joe Perches63c3a662011-04-26 08:12:10 +00005519 tg3_flag_set(tp, TX_RECOVERY_PENDING);
Michael Chandf3e6542006-05-26 17:48:07 -07005520 spin_unlock(&tp->lock);
5521}
5522
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005523static inline u32 tg3_tx_avail(struct tg3_napi *tnapi)
Michael Chan1b2a7202006-08-07 21:46:02 -07005524{
Matt Carlsonf65aac12010-08-02 11:26:03 +00005525 /* Tell compiler to fetch tx indices from memory. */
5526 barrier();
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005527 return tnapi->tx_pending -
5528 ((tnapi->tx_prod - tnapi->tx_cons) & (TG3_TX_RING_SIZE - 1));
Michael Chan1b2a7202006-08-07 21:46:02 -07005529}
5530
Linus Torvalds1da177e2005-04-16 15:20:36 -07005531/* Tigon3 never reports partial packet sends. So we do not
5532 * need special logic to handle SKBs that have not had all
5533 * of their frags sent yet, like SunGEM does.
5534 */
Matt Carlson17375d22009-08-28 14:02:18 +00005535static void tg3_tx(struct tg3_napi *tnapi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005536{
Matt Carlson17375d22009-08-28 14:02:18 +00005537 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00005538 u32 hw_idx = tnapi->hw_status->idx[0].tx_consumer;
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005539 u32 sw_idx = tnapi->tx_cons;
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005540 struct netdev_queue *txq;
5541 int index = tnapi - tp->napi;
Tom Herbert298376d2011-11-28 16:33:30 +00005542 unsigned int pkts_compl = 0, bytes_compl = 0;
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005543
Joe Perches63c3a662011-04-26 08:12:10 +00005544 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005545 index--;
5546
5547 txq = netdev_get_tx_queue(tp->dev, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005548
5549 while (sw_idx != hw_idx) {
Matt Carlsondf8944c2011-07-27 14:20:46 +00005550 struct tg3_tx_ring_info *ri = &tnapi->tx_buffers[sw_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07005551 struct sk_buff *skb = ri->skb;
Michael Chandf3e6542006-05-26 17:48:07 -07005552 int i, tx_bug = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005553
Michael Chandf3e6542006-05-26 17:48:07 -07005554 if (unlikely(skb == NULL)) {
5555 tg3_tx_recover(tp);
5556 return;
5557 }
5558
Alexander Duyckf4188d82009-12-02 16:48:38 +00005559 pci_unmap_single(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005560 dma_unmap_addr(ri, mapping),
Alexander Duyckf4188d82009-12-02 16:48:38 +00005561 skb_headlen(skb),
5562 PCI_DMA_TODEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005563
5564 ri->skb = NULL;
5565
Matt Carlsone01ee142011-07-27 14:20:50 +00005566 while (ri->fragmented) {
5567 ri->fragmented = false;
5568 sw_idx = NEXT_TX(sw_idx);
5569 ri = &tnapi->tx_buffers[sw_idx];
5570 }
5571
Linus Torvalds1da177e2005-04-16 15:20:36 -07005572 sw_idx = NEXT_TX(sw_idx);
5573
5574 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005575 ri = &tnapi->tx_buffers[sw_idx];
Michael Chandf3e6542006-05-26 17:48:07 -07005576 if (unlikely(ri->skb != NULL || sw_idx == hw_idx))
5577 tx_bug = 1;
Alexander Duyckf4188d82009-12-02 16:48:38 +00005578
5579 pci_unmap_page(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005580 dma_unmap_addr(ri, mapping),
Eric Dumazet9e903e02011-10-18 21:00:24 +00005581 skb_frag_size(&skb_shinfo(skb)->frags[i]),
Alexander Duyckf4188d82009-12-02 16:48:38 +00005582 PCI_DMA_TODEVICE);
Matt Carlsone01ee142011-07-27 14:20:50 +00005583
5584 while (ri->fragmented) {
5585 ri->fragmented = false;
5586 sw_idx = NEXT_TX(sw_idx);
5587 ri = &tnapi->tx_buffers[sw_idx];
5588 }
5589
Linus Torvalds1da177e2005-04-16 15:20:36 -07005590 sw_idx = NEXT_TX(sw_idx);
5591 }
5592
Tom Herbert298376d2011-11-28 16:33:30 +00005593 pkts_compl++;
5594 bytes_compl += skb->len;
5595
David S. Millerf47c11e2005-06-24 20:18:35 -07005596 dev_kfree_skb(skb);
Michael Chandf3e6542006-05-26 17:48:07 -07005597
5598 if (unlikely(tx_bug)) {
5599 tg3_tx_recover(tp);
5600 return;
5601 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005602 }
5603
Tom Herbert5cb917b2012-03-05 19:53:50 +00005604 netdev_tx_completed_queue(txq, pkts_compl, bytes_compl);
Tom Herbert298376d2011-11-28 16:33:30 +00005605
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005606 tnapi->tx_cons = sw_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005607
Michael Chan1b2a7202006-08-07 21:46:02 -07005608 /* Need to make the tx_cons update visible to tg3_start_xmit()
5609 * before checking for netif_queue_stopped(). Without the
5610 * memory barrier, there is a small possibility that tg3_start_xmit()
5611 * will miss it and cause the queue to be stopped forever.
5612 */
5613 smp_mb();
5614
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005615 if (unlikely(netif_tx_queue_stopped(txq) &&
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005616 (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi)))) {
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005617 __netif_tx_lock(txq, smp_processor_id());
5618 if (netif_tx_queue_stopped(txq) &&
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005619 (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi)))
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005620 netif_tx_wake_queue(txq);
5621 __netif_tx_unlock(txq);
Michael Chan51b91462005-09-01 17:41:28 -07005622 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005623}
5624
Eric Dumazet8d4057a2012-04-27 00:34:49 +00005625static void tg3_frag_free(bool is_frag, void *data)
5626{
5627 if (is_frag)
5628 put_page(virt_to_head_page(data));
5629 else
5630 kfree(data);
5631}
5632
Eric Dumazet9205fd92011-11-18 06:47:01 +00005633static void tg3_rx_data_free(struct tg3 *tp, struct ring_info *ri, u32 map_sz)
Matt Carlson2b2cdb62009-11-13 13:03:48 +00005634{
Eric Dumazet8d4057a2012-04-27 00:34:49 +00005635 unsigned int skb_size = SKB_DATA_ALIGN(map_sz + TG3_RX_OFFSET(tp)) +
5636 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
5637
Eric Dumazet9205fd92011-11-18 06:47:01 +00005638 if (!ri->data)
Matt Carlson2b2cdb62009-11-13 13:03:48 +00005639 return;
5640
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005641 pci_unmap_single(tp->pdev, dma_unmap_addr(ri, mapping),
Matt Carlson2b2cdb62009-11-13 13:03:48 +00005642 map_sz, PCI_DMA_FROMDEVICE);
Eric Dumazeta1e8b3072012-05-18 21:33:39 +00005643 tg3_frag_free(skb_size <= PAGE_SIZE, ri->data);
Eric Dumazet9205fd92011-11-18 06:47:01 +00005644 ri->data = NULL;
Matt Carlson2b2cdb62009-11-13 13:03:48 +00005645}
5646
Eric Dumazet8d4057a2012-04-27 00:34:49 +00005647
Linus Torvalds1da177e2005-04-16 15:20:36 -07005648/* Returns size of skb allocated or < 0 on error.
5649 *
5650 * We only need to fill in the address because the other members
5651 * of the RX descriptor are invariant, see tg3_init_rings.
5652 *
5653 * Note the purposeful assymetry of cpu vs. chip accesses. For
5654 * posting buffers we only dirty the first cache line of the RX
5655 * descriptor (containing the address). Whereas for the RX status
5656 * buffers the cpu only reads the last cacheline of the RX descriptor
5657 * (to fetch the error flags, vlan tag, checksum, and opaque cookie).
5658 */
Eric Dumazet9205fd92011-11-18 06:47:01 +00005659static int tg3_alloc_rx_data(struct tg3 *tp, struct tg3_rx_prodring_set *tpr,
Eric Dumazet8d4057a2012-04-27 00:34:49 +00005660 u32 opaque_key, u32 dest_idx_unmasked,
5661 unsigned int *frag_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005662{
5663 struct tg3_rx_buffer_desc *desc;
Matt Carlsonf94e2902010-10-14 10:37:42 +00005664 struct ring_info *map;
Eric Dumazet9205fd92011-11-18 06:47:01 +00005665 u8 *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005666 dma_addr_t mapping;
Eric Dumazet9205fd92011-11-18 06:47:01 +00005667 int skb_size, data_size, dest_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005668
Linus Torvalds1da177e2005-04-16 15:20:36 -07005669 switch (opaque_key) {
5670 case RXD_OPAQUE_RING_STD:
Matt Carlson2c49a442010-09-30 10:34:35 +00005671 dest_idx = dest_idx_unmasked & tp->rx_std_ring_mask;
Matt Carlson21f581a2009-08-28 14:00:25 +00005672 desc = &tpr->rx_std[dest_idx];
5673 map = &tpr->rx_std_buffers[dest_idx];
Eric Dumazet9205fd92011-11-18 06:47:01 +00005674 data_size = tp->rx_pkt_map_sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005675 break;
5676
5677 case RXD_OPAQUE_RING_JUMBO:
Matt Carlson2c49a442010-09-30 10:34:35 +00005678 dest_idx = dest_idx_unmasked & tp->rx_jmb_ring_mask;
Matt Carlson79ed5ac2009-08-28 14:00:55 +00005679 desc = &tpr->rx_jmb[dest_idx].std;
Matt Carlson21f581a2009-08-28 14:00:25 +00005680 map = &tpr->rx_jmb_buffers[dest_idx];
Eric Dumazet9205fd92011-11-18 06:47:01 +00005681 data_size = TG3_RX_JMB_MAP_SZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005682 break;
5683
5684 default:
5685 return -EINVAL;
Stephen Hemminger855e1112008-04-16 16:37:28 -07005686 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005687
5688 /* Do not overwrite any of the map or rp information
5689 * until we are sure we can commit to a new buffer.
5690 *
5691 * Callers depend upon this behavior and assume that
5692 * we leave everything unchanged if we fail.
5693 */
Eric Dumazet9205fd92011-11-18 06:47:01 +00005694 skb_size = SKB_DATA_ALIGN(data_size + TG3_RX_OFFSET(tp)) +
5695 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
Eric Dumazeta1e8b3072012-05-18 21:33:39 +00005696 if (skb_size <= PAGE_SIZE) {
5697 data = netdev_alloc_frag(skb_size);
5698 *frag_size = skb_size;
Eric Dumazet8d4057a2012-04-27 00:34:49 +00005699 } else {
5700 data = kmalloc(skb_size, GFP_ATOMIC);
5701 *frag_size = 0;
5702 }
Eric Dumazet9205fd92011-11-18 06:47:01 +00005703 if (!data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005704 return -ENOMEM;
5705
Eric Dumazet9205fd92011-11-18 06:47:01 +00005706 mapping = pci_map_single(tp->pdev,
5707 data + TG3_RX_OFFSET(tp),
5708 data_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005709 PCI_DMA_FROMDEVICE);
Eric Dumazet8d4057a2012-04-27 00:34:49 +00005710 if (unlikely(pci_dma_mapping_error(tp->pdev, mapping))) {
Eric Dumazeta1e8b3072012-05-18 21:33:39 +00005711 tg3_frag_free(skb_size <= PAGE_SIZE, data);
Matt Carlsona21771d2009-11-02 14:25:31 +00005712 return -EIO;
5713 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005714
Eric Dumazet9205fd92011-11-18 06:47:01 +00005715 map->data = data;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005716 dma_unmap_addr_set(map, mapping, mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005717
Linus Torvalds1da177e2005-04-16 15:20:36 -07005718 desc->addr_hi = ((u64)mapping >> 32);
5719 desc->addr_lo = ((u64)mapping & 0xffffffff);
5720
Eric Dumazet9205fd92011-11-18 06:47:01 +00005721 return data_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005722}
5723
5724/* We only need to move over in the address because the other
5725 * members of the RX descriptor are invariant. See notes above
Eric Dumazet9205fd92011-11-18 06:47:01 +00005726 * tg3_alloc_rx_data for full details.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005727 */
Matt Carlsona3896162009-11-13 13:03:44 +00005728static void tg3_recycle_rx(struct tg3_napi *tnapi,
5729 struct tg3_rx_prodring_set *dpr,
5730 u32 opaque_key, int src_idx,
5731 u32 dest_idx_unmasked)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005732{
Matt Carlson17375d22009-08-28 14:02:18 +00005733 struct tg3 *tp = tnapi->tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005734 struct tg3_rx_buffer_desc *src_desc, *dest_desc;
5735 struct ring_info *src_map, *dest_map;
Matt Carlson8fea32b2010-09-15 08:59:58 +00005736 struct tg3_rx_prodring_set *spr = &tp->napi[0].prodring;
Matt Carlsonc6cdf432010-04-05 10:19:26 +00005737 int dest_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005738
5739 switch (opaque_key) {
5740 case RXD_OPAQUE_RING_STD:
Matt Carlson2c49a442010-09-30 10:34:35 +00005741 dest_idx = dest_idx_unmasked & tp->rx_std_ring_mask;
Matt Carlsona3896162009-11-13 13:03:44 +00005742 dest_desc = &dpr->rx_std[dest_idx];
5743 dest_map = &dpr->rx_std_buffers[dest_idx];
5744 src_desc = &spr->rx_std[src_idx];
5745 src_map = &spr->rx_std_buffers[src_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07005746 break;
5747
5748 case RXD_OPAQUE_RING_JUMBO:
Matt Carlson2c49a442010-09-30 10:34:35 +00005749 dest_idx = dest_idx_unmasked & tp->rx_jmb_ring_mask;
Matt Carlsona3896162009-11-13 13:03:44 +00005750 dest_desc = &dpr->rx_jmb[dest_idx].std;
5751 dest_map = &dpr->rx_jmb_buffers[dest_idx];
5752 src_desc = &spr->rx_jmb[src_idx].std;
5753 src_map = &spr->rx_jmb_buffers[src_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07005754 break;
5755
5756 default:
5757 return;
Stephen Hemminger855e1112008-04-16 16:37:28 -07005758 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005759
Eric Dumazet9205fd92011-11-18 06:47:01 +00005760 dest_map->data = src_map->data;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005761 dma_unmap_addr_set(dest_map, mapping,
5762 dma_unmap_addr(src_map, mapping));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005763 dest_desc->addr_hi = src_desc->addr_hi;
5764 dest_desc->addr_lo = src_desc->addr_lo;
Matt Carlsone92967b2010-02-12 14:47:06 +00005765
5766 /* Ensure that the update to the skb happens after the physical
5767 * addresses have been transferred to the new BD location.
5768 */
5769 smp_wmb();
5770
Eric Dumazet9205fd92011-11-18 06:47:01 +00005771 src_map->data = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005772}
5773
Linus Torvalds1da177e2005-04-16 15:20:36 -07005774/* The RX ring scheme is composed of multiple rings which post fresh
5775 * buffers to the chip, and one special ring the chip uses to report
5776 * status back to the host.
5777 *
5778 * The special ring reports the status of received packets to the
5779 * host. The chip does not write into the original descriptor the
5780 * RX buffer was obtained from. The chip simply takes the original
5781 * descriptor as provided by the host, updates the status and length
5782 * field, then writes this into the next status ring entry.
5783 *
5784 * Each ring the host uses to post buffers to the chip is described
5785 * by a TG3_BDINFO entry in the chips SRAM area. When a packet arrives,
5786 * it is first placed into the on-chip ram. When the packet's length
5787 * is known, it walks down the TG3_BDINFO entries to select the ring.
5788 * Each TG3_BDINFO specifies a MAXLEN field and the first TG3_BDINFO
5789 * which is within the range of the new packet's length is chosen.
5790 *
5791 * The "separate ring for rx status" scheme may sound queer, but it makes
5792 * sense from a cache coherency perspective. If only the host writes
5793 * to the buffer post rings, and only the chip writes to the rx status
5794 * rings, then cache lines never move beyond shared-modified state.
5795 * If both the host and chip were to write into the same ring, cache line
5796 * eviction could occur since both entities want it in an exclusive state.
5797 */
Matt Carlson17375d22009-08-28 14:02:18 +00005798static int tg3_rx(struct tg3_napi *tnapi, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005799{
Matt Carlson17375d22009-08-28 14:02:18 +00005800 struct tg3 *tp = tnapi->tp;
Michael Chanf92905d2006-06-29 20:14:29 -07005801 u32 work_mask, rx_std_posted = 0;
Matt Carlson43619352009-11-13 13:03:47 +00005802 u32 std_prod_idx, jmb_prod_idx;
Matt Carlson72334482009-08-28 14:03:01 +00005803 u32 sw_idx = tnapi->rx_rcb_ptr;
Michael Chan483ba502005-04-25 15:14:03 -07005804 u16 hw_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005805 int received;
Matt Carlson8fea32b2010-09-15 08:59:58 +00005806 struct tg3_rx_prodring_set *tpr = &tnapi->prodring;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005807
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00005808 hw_idx = *(tnapi->rx_rcb_prod_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005809 /*
5810 * We need to order the read of hw_idx and the read of
5811 * the opaque cookie.
5812 */
5813 rmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005814 work_mask = 0;
5815 received = 0;
Matt Carlson43619352009-11-13 13:03:47 +00005816 std_prod_idx = tpr->rx_std_prod_idx;
5817 jmb_prod_idx = tpr->rx_jmb_prod_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005818 while (sw_idx != hw_idx && budget > 0) {
Matt Carlsonafc081f2009-11-13 13:03:43 +00005819 struct ring_info *ri;
Matt Carlson72334482009-08-28 14:03:01 +00005820 struct tg3_rx_buffer_desc *desc = &tnapi->rx_rcb[sw_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07005821 unsigned int len;
5822 struct sk_buff *skb;
5823 dma_addr_t dma_addr;
5824 u32 opaque_key, desc_idx, *post_ptr;
Eric Dumazet9205fd92011-11-18 06:47:01 +00005825 u8 *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005826
5827 desc_idx = desc->opaque & RXD_OPAQUE_INDEX_MASK;
5828 opaque_key = desc->opaque & RXD_OPAQUE_RING_MASK;
5829 if (opaque_key == RXD_OPAQUE_RING_STD) {
Matt Carlson8fea32b2010-09-15 08:59:58 +00005830 ri = &tp->napi[0].prodring.rx_std_buffers[desc_idx];
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005831 dma_addr = dma_unmap_addr(ri, mapping);
Eric Dumazet9205fd92011-11-18 06:47:01 +00005832 data = ri->data;
Matt Carlson43619352009-11-13 13:03:47 +00005833 post_ptr = &std_prod_idx;
Michael Chanf92905d2006-06-29 20:14:29 -07005834 rx_std_posted++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005835 } else if (opaque_key == RXD_OPAQUE_RING_JUMBO) {
Matt Carlson8fea32b2010-09-15 08:59:58 +00005836 ri = &tp->napi[0].prodring.rx_jmb_buffers[desc_idx];
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005837 dma_addr = dma_unmap_addr(ri, mapping);
Eric Dumazet9205fd92011-11-18 06:47:01 +00005838 data = ri->data;
Matt Carlson43619352009-11-13 13:03:47 +00005839 post_ptr = &jmb_prod_idx;
Matt Carlson21f581a2009-08-28 14:00:25 +00005840 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07005841 goto next_pkt_nopost;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005842
5843 work_mask |= opaque_key;
5844
5845 if ((desc->err_vlan & RXD_ERR_MASK) != 0 &&
5846 (desc->err_vlan != RXD_ERR_ODD_NIBBLE_RCVD_MII)) {
5847 drop_it:
Matt Carlsona3896162009-11-13 13:03:44 +00005848 tg3_recycle_rx(tnapi, tpr, opaque_key,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005849 desc_idx, *post_ptr);
5850 drop_it_no_recycle:
5851 /* Other statistics kept track of by card. */
Eric Dumazetb0057c52010-10-10 19:55:52 +00005852 tp->rx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005853 goto next_pkt;
5854 }
5855
Eric Dumazet9205fd92011-11-18 06:47:01 +00005856 prefetch(data + TG3_RX_OFFSET(tp));
Matt Carlsonad829262008-11-21 17:16:16 -08005857 len = ((desc->idx_len & RXD_LEN_MASK) >> RXD_LEN_SHIFT) -
5858 ETH_FCS_LEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005859
Matt Carlsond2757fc2010-04-12 06:58:27 +00005860 if (len > TG3_RX_COPY_THRESH(tp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005861 int skb_size;
Eric Dumazet8d4057a2012-04-27 00:34:49 +00005862 unsigned int frag_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005863
Eric Dumazet9205fd92011-11-18 06:47:01 +00005864 skb_size = tg3_alloc_rx_data(tp, tpr, opaque_key,
Eric Dumazet8d4057a2012-04-27 00:34:49 +00005865 *post_ptr, &frag_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005866 if (skb_size < 0)
5867 goto drop_it;
5868
Matt Carlson287be122009-08-28 13:58:46 +00005869 pci_unmap_single(tp->pdev, dma_addr, skb_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005870 PCI_DMA_FROMDEVICE);
5871
Eric Dumazet8d4057a2012-04-27 00:34:49 +00005872 skb = build_skb(data, frag_size);
Eric Dumazet9205fd92011-11-18 06:47:01 +00005873 if (!skb) {
Eric Dumazet8d4057a2012-04-27 00:34:49 +00005874 tg3_frag_free(frag_size != 0, data);
Eric Dumazet9205fd92011-11-18 06:47:01 +00005875 goto drop_it_no_recycle;
5876 }
5877 skb_reserve(skb, TG3_RX_OFFSET(tp));
5878 /* Ensure that the update to the data happens
Matt Carlson61e800c2010-02-17 15:16:54 +00005879 * after the usage of the old DMA mapping.
5880 */
5881 smp_wmb();
5882
Eric Dumazet9205fd92011-11-18 06:47:01 +00005883 ri->data = NULL;
Matt Carlson61e800c2010-02-17 15:16:54 +00005884
Linus Torvalds1da177e2005-04-16 15:20:36 -07005885 } else {
Matt Carlsona3896162009-11-13 13:03:44 +00005886 tg3_recycle_rx(tnapi, tpr, opaque_key,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005887 desc_idx, *post_ptr);
5888
Eric Dumazet9205fd92011-11-18 06:47:01 +00005889 skb = netdev_alloc_skb(tp->dev,
5890 len + TG3_RAW_IP_ALIGN);
5891 if (skb == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005892 goto drop_it_no_recycle;
5893
Eric Dumazet9205fd92011-11-18 06:47:01 +00005894 skb_reserve(skb, TG3_RAW_IP_ALIGN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005895 pci_dma_sync_single_for_cpu(tp->pdev, dma_addr, len, PCI_DMA_FROMDEVICE);
Eric Dumazet9205fd92011-11-18 06:47:01 +00005896 memcpy(skb->data,
5897 data + TG3_RX_OFFSET(tp),
5898 len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005899 pci_dma_sync_single_for_device(tp->pdev, dma_addr, len, PCI_DMA_FROMDEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005900 }
5901
Eric Dumazet9205fd92011-11-18 06:47:01 +00005902 skb_put(skb, len);
Michał Mirosławdc668912011-04-07 03:35:07 +00005903 if ((tp->dev->features & NETIF_F_RXCSUM) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005904 (desc->type_flags & RXD_FLAG_TCPUDP_CSUM) &&
5905 (((desc->ip_tcp_csum & RXD_TCPCSUM_MASK)
5906 >> RXD_TCPCSUM_SHIFT) == 0xffff))
5907 skb->ip_summed = CHECKSUM_UNNECESSARY;
5908 else
Eric Dumazetbc8acf22010-09-02 13:07:41 -07005909 skb_checksum_none_assert(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005910
5911 skb->protocol = eth_type_trans(skb, tp->dev);
Matt Carlsonf7b493e2009-02-25 14:21:52 +00005912
5913 if (len > (tp->dev->mtu + ETH_HLEN) &&
5914 skb->protocol != htons(ETH_P_8021Q)) {
5915 dev_kfree_skb(skb);
Eric Dumazetb0057c52010-10-10 19:55:52 +00005916 goto drop_it_no_recycle;
Matt Carlsonf7b493e2009-02-25 14:21:52 +00005917 }
5918
Matt Carlson9dc7a112010-04-12 06:58:28 +00005919 if (desc->type_flags & RXD_FLAG_VLAN &&
Matt Carlsonbf933c82011-01-25 15:58:49 +00005920 !(tp->rx_mode & RX_MODE_KEEP_VLAN_TAG))
5921 __vlan_hwaccel_put_tag(skb,
5922 desc->err_vlan & RXD_VLAN_MASK);
Matt Carlson9dc7a112010-04-12 06:58:28 +00005923
Matt Carlsonbf933c82011-01-25 15:58:49 +00005924 napi_gro_receive(&tnapi->napi, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005925
Linus Torvalds1da177e2005-04-16 15:20:36 -07005926 received++;
5927 budget--;
5928
5929next_pkt:
5930 (*post_ptr)++;
Michael Chanf92905d2006-06-29 20:14:29 -07005931
5932 if (unlikely(rx_std_posted >= tp->rx_std_max_post)) {
Matt Carlson2c49a442010-09-30 10:34:35 +00005933 tpr->rx_std_prod_idx = std_prod_idx &
5934 tp->rx_std_ring_mask;
Matt Carlson86cfe4f2010-01-12 10:11:37 +00005935 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
5936 tpr->rx_std_prod_idx);
Michael Chanf92905d2006-06-29 20:14:29 -07005937 work_mask &= ~RXD_OPAQUE_RING_STD;
5938 rx_std_posted = 0;
5939 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005940next_pkt_nopost:
Michael Chan483ba502005-04-25 15:14:03 -07005941 sw_idx++;
Matt Carlson7cb32cf2010-09-30 10:34:36 +00005942 sw_idx &= tp->rx_ret_ring_mask;
Michael Chan52f6d692005-04-25 15:14:32 -07005943
5944 /* Refresh hw_idx to see if there is new work */
5945 if (sw_idx == hw_idx) {
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00005946 hw_idx = *(tnapi->rx_rcb_prod_idx);
Michael Chan52f6d692005-04-25 15:14:32 -07005947 rmb();
5948 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005949 }
5950
5951 /* ACK the status ring. */
Matt Carlson72334482009-08-28 14:03:01 +00005952 tnapi->rx_rcb_ptr = sw_idx;
5953 tw32_rx_mbox(tnapi->consmbox, sw_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005954
5955 /* Refill RX ring(s). */
Joe Perches63c3a662011-04-26 08:12:10 +00005956 if (!tg3_flag(tp, ENABLE_RSS)) {
Michael Chan6541b802012-03-04 14:48:14 +00005957 /* Sync BD data before updating mailbox */
5958 wmb();
5959
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005960 if (work_mask & RXD_OPAQUE_RING_STD) {
Matt Carlson2c49a442010-09-30 10:34:35 +00005961 tpr->rx_std_prod_idx = std_prod_idx &
5962 tp->rx_std_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005963 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
5964 tpr->rx_std_prod_idx);
5965 }
5966 if (work_mask & RXD_OPAQUE_RING_JUMBO) {
Matt Carlson2c49a442010-09-30 10:34:35 +00005967 tpr->rx_jmb_prod_idx = jmb_prod_idx &
5968 tp->rx_jmb_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005969 tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG,
5970 tpr->rx_jmb_prod_idx);
5971 }
5972 mmiowb();
5973 } else if (work_mask) {
5974 /* rx_std_buffers[] and rx_jmb_buffers[] entries must be
5975 * updated before the producer indices can be updated.
5976 */
5977 smp_wmb();
5978
Matt Carlson2c49a442010-09-30 10:34:35 +00005979 tpr->rx_std_prod_idx = std_prod_idx & tp->rx_std_ring_mask;
5980 tpr->rx_jmb_prod_idx = jmb_prod_idx & tp->rx_jmb_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005981
Michael Chan7ae52892012-03-21 15:38:33 +00005982 if (tnapi != &tp->napi[1]) {
5983 tp->rx_refill = true;
Matt Carlsone4af1af2010-02-12 14:47:05 +00005984 napi_schedule(&tp->napi[1].napi);
Michael Chan7ae52892012-03-21 15:38:33 +00005985 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005986 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005987
5988 return received;
5989}
5990
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005991static void tg3_poll_link(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005992{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005993 /* handle link change and other phy events */
Joe Perches63c3a662011-04-26 08:12:10 +00005994 if (!(tg3_flag(tp, USE_LINKCHG_REG) || tg3_flag(tp, POLL_SERDES))) {
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005995 struct tg3_hw_status *sblk = tp->napi[0].hw_status;
5996
Linus Torvalds1da177e2005-04-16 15:20:36 -07005997 if (sblk->status & SD_STATUS_LINK_CHG) {
5998 sblk->status = SD_STATUS_UPDATED |
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005999 (sblk->status & ~SD_STATUS_LINK_CHG);
David S. Millerf47c11e2005-06-24 20:18:35 -07006000 spin_lock(&tp->lock);
Joe Perches63c3a662011-04-26 08:12:10 +00006001 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlsondd477002008-05-25 23:45:58 -07006002 tw32_f(MAC_STATUS,
6003 (MAC_STATUS_SYNC_CHANGED |
6004 MAC_STATUS_CFG_CHANGED |
6005 MAC_STATUS_MI_COMPLETION |
6006 MAC_STATUS_LNKSTATE_CHANGED));
6007 udelay(40);
6008 } else
6009 tg3_setup_phy(tp, 0);
David S. Millerf47c11e2005-06-24 20:18:35 -07006010 spin_unlock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006011 }
6012 }
Matt Carlson35f2d7d2009-11-13 13:03:41 +00006013}
6014
Matt Carlsonf89f38b2010-02-12 14:47:07 +00006015static int tg3_rx_prodring_xfer(struct tg3 *tp,
6016 struct tg3_rx_prodring_set *dpr,
6017 struct tg3_rx_prodring_set *spr)
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006018{
6019 u32 si, di, cpycnt, src_prod_idx;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00006020 int i, err = 0;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006021
6022 while (1) {
6023 src_prod_idx = spr->rx_std_prod_idx;
6024
6025 /* Make sure updates to the rx_std_buffers[] entries and the
6026 * standard producer index are seen in the correct order.
6027 */
6028 smp_rmb();
6029
6030 if (spr->rx_std_cons_idx == src_prod_idx)
6031 break;
6032
6033 if (spr->rx_std_cons_idx < src_prod_idx)
6034 cpycnt = src_prod_idx - spr->rx_std_cons_idx;
6035 else
Matt Carlson2c49a442010-09-30 10:34:35 +00006036 cpycnt = tp->rx_std_ring_mask + 1 -
6037 spr->rx_std_cons_idx;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006038
Matt Carlson2c49a442010-09-30 10:34:35 +00006039 cpycnt = min(cpycnt,
6040 tp->rx_std_ring_mask + 1 - dpr->rx_std_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006041
6042 si = spr->rx_std_cons_idx;
6043 di = dpr->rx_std_prod_idx;
6044
Matt Carlsone92967b2010-02-12 14:47:06 +00006045 for (i = di; i < di + cpycnt; i++) {
Eric Dumazet9205fd92011-11-18 06:47:01 +00006046 if (dpr->rx_std_buffers[i].data) {
Matt Carlsone92967b2010-02-12 14:47:06 +00006047 cpycnt = i - di;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00006048 err = -ENOSPC;
Matt Carlsone92967b2010-02-12 14:47:06 +00006049 break;
6050 }
6051 }
6052
6053 if (!cpycnt)
6054 break;
6055
6056 /* Ensure that updates to the rx_std_buffers ring and the
6057 * shadowed hardware producer ring from tg3_recycle_skb() are
6058 * ordered correctly WRT the skb check above.
6059 */
6060 smp_rmb();
6061
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006062 memcpy(&dpr->rx_std_buffers[di],
6063 &spr->rx_std_buffers[si],
6064 cpycnt * sizeof(struct ring_info));
6065
6066 for (i = 0; i < cpycnt; i++, di++, si++) {
6067 struct tg3_rx_buffer_desc *sbd, *dbd;
6068 sbd = &spr->rx_std[si];
6069 dbd = &dpr->rx_std[di];
6070 dbd->addr_hi = sbd->addr_hi;
6071 dbd->addr_lo = sbd->addr_lo;
6072 }
6073
Matt Carlson2c49a442010-09-30 10:34:35 +00006074 spr->rx_std_cons_idx = (spr->rx_std_cons_idx + cpycnt) &
6075 tp->rx_std_ring_mask;
6076 dpr->rx_std_prod_idx = (dpr->rx_std_prod_idx + cpycnt) &
6077 tp->rx_std_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006078 }
6079
6080 while (1) {
6081 src_prod_idx = spr->rx_jmb_prod_idx;
6082
6083 /* Make sure updates to the rx_jmb_buffers[] entries and
6084 * the jumbo producer index are seen in the correct order.
6085 */
6086 smp_rmb();
6087
6088 if (spr->rx_jmb_cons_idx == src_prod_idx)
6089 break;
6090
6091 if (spr->rx_jmb_cons_idx < src_prod_idx)
6092 cpycnt = src_prod_idx - spr->rx_jmb_cons_idx;
6093 else
Matt Carlson2c49a442010-09-30 10:34:35 +00006094 cpycnt = tp->rx_jmb_ring_mask + 1 -
6095 spr->rx_jmb_cons_idx;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006096
6097 cpycnt = min(cpycnt,
Matt Carlson2c49a442010-09-30 10:34:35 +00006098 tp->rx_jmb_ring_mask + 1 - dpr->rx_jmb_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006099
6100 si = spr->rx_jmb_cons_idx;
6101 di = dpr->rx_jmb_prod_idx;
6102
Matt Carlsone92967b2010-02-12 14:47:06 +00006103 for (i = di; i < di + cpycnt; i++) {
Eric Dumazet9205fd92011-11-18 06:47:01 +00006104 if (dpr->rx_jmb_buffers[i].data) {
Matt Carlsone92967b2010-02-12 14:47:06 +00006105 cpycnt = i - di;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00006106 err = -ENOSPC;
Matt Carlsone92967b2010-02-12 14:47:06 +00006107 break;
6108 }
6109 }
6110
6111 if (!cpycnt)
6112 break;
6113
6114 /* Ensure that updates to the rx_jmb_buffers ring and the
6115 * shadowed hardware producer ring from tg3_recycle_skb() are
6116 * ordered correctly WRT the skb check above.
6117 */
6118 smp_rmb();
6119
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006120 memcpy(&dpr->rx_jmb_buffers[di],
6121 &spr->rx_jmb_buffers[si],
6122 cpycnt * sizeof(struct ring_info));
6123
6124 for (i = 0; i < cpycnt; i++, di++, si++) {
6125 struct tg3_rx_buffer_desc *sbd, *dbd;
6126 sbd = &spr->rx_jmb[si].std;
6127 dbd = &dpr->rx_jmb[di].std;
6128 dbd->addr_hi = sbd->addr_hi;
6129 dbd->addr_lo = sbd->addr_lo;
6130 }
6131
Matt Carlson2c49a442010-09-30 10:34:35 +00006132 spr->rx_jmb_cons_idx = (spr->rx_jmb_cons_idx + cpycnt) &
6133 tp->rx_jmb_ring_mask;
6134 dpr->rx_jmb_prod_idx = (dpr->rx_jmb_prod_idx + cpycnt) &
6135 tp->rx_jmb_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006136 }
Matt Carlsonf89f38b2010-02-12 14:47:07 +00006137
6138 return err;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006139}
6140
Matt Carlson35f2d7d2009-11-13 13:03:41 +00006141static int tg3_poll_work(struct tg3_napi *tnapi, int work_done, int budget)
6142{
6143 struct tg3 *tp = tnapi->tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006144
6145 /* run TX completion thread */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006146 if (tnapi->hw_status->idx[0].tx_consumer != tnapi->tx_cons) {
Matt Carlson17375d22009-08-28 14:02:18 +00006147 tg3_tx(tnapi);
Joe Perches63c3a662011-04-26 08:12:10 +00006148 if (unlikely(tg3_flag(tp, TX_RECOVERY_PENDING)))
Michael Chan4fd7ab52007-10-12 01:39:50 -07006149 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006150 }
6151
Matt Carlsonf891ea12012-04-24 13:37:01 +00006152 if (!tnapi->rx_rcb_prod_idx)
6153 return work_done;
6154
Linus Torvalds1da177e2005-04-16 15:20:36 -07006155 /* run RX thread, within the bounds set by NAPI.
6156 * All RX "locking" is done by ensuring outside
Stephen Hemmingerbea33482007-10-03 16:41:36 -07006157 * code synchronizes with tg3->napi.poll()
Linus Torvalds1da177e2005-04-16 15:20:36 -07006158 */
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00006159 if (*(tnapi->rx_rcb_prod_idx) != tnapi->rx_rcb_ptr)
Matt Carlson17375d22009-08-28 14:02:18 +00006160 work_done += tg3_rx(tnapi, budget - work_done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006161
Joe Perches63c3a662011-04-26 08:12:10 +00006162 if (tg3_flag(tp, ENABLE_RSS) && tnapi == &tp->napi[1]) {
Matt Carlson8fea32b2010-09-15 08:59:58 +00006163 struct tg3_rx_prodring_set *dpr = &tp->napi[0].prodring;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00006164 int i, err = 0;
Matt Carlsone4af1af2010-02-12 14:47:05 +00006165 u32 std_prod_idx = dpr->rx_std_prod_idx;
6166 u32 jmb_prod_idx = dpr->rx_jmb_prod_idx;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006167
Michael Chan7ae52892012-03-21 15:38:33 +00006168 tp->rx_refill = false;
Matt Carlsone4af1af2010-02-12 14:47:05 +00006169 for (i = 1; i < tp->irq_cnt; i++)
Matt Carlsonf89f38b2010-02-12 14:47:07 +00006170 err |= tg3_rx_prodring_xfer(tp, dpr,
Matt Carlson8fea32b2010-09-15 08:59:58 +00006171 &tp->napi[i].prodring);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006172
6173 wmb();
6174
Matt Carlsone4af1af2010-02-12 14:47:05 +00006175 if (std_prod_idx != dpr->rx_std_prod_idx)
6176 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
6177 dpr->rx_std_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006178
Matt Carlsone4af1af2010-02-12 14:47:05 +00006179 if (jmb_prod_idx != dpr->rx_jmb_prod_idx)
6180 tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG,
6181 dpr->rx_jmb_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006182
6183 mmiowb();
Matt Carlsonf89f38b2010-02-12 14:47:07 +00006184
6185 if (err)
6186 tw32_f(HOSTCC_MODE, tp->coal_now);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006187 }
6188
David S. Miller6f535762007-10-11 18:08:29 -07006189 return work_done;
6190}
David S. Millerf7383c22005-05-18 22:50:53 -07006191
Matt Carlsondb219972011-11-04 09:15:03 +00006192static inline void tg3_reset_task_schedule(struct tg3 *tp)
6193{
6194 if (!test_and_set_bit(TG3_FLAG_RESET_TASK_PENDING, tp->tg3_flags))
6195 schedule_work(&tp->reset_task);
6196}
6197
6198static inline void tg3_reset_task_cancel(struct tg3 *tp)
6199{
6200 cancel_work_sync(&tp->reset_task);
6201 tg3_flag_clear(tp, RESET_TASK_PENDING);
Matt Carlsonc7101352012-02-22 12:35:20 +00006202 tg3_flag_clear(tp, TX_RECOVERY_PENDING);
Matt Carlsondb219972011-11-04 09:15:03 +00006203}
6204
Matt Carlson35f2d7d2009-11-13 13:03:41 +00006205static int tg3_poll_msix(struct napi_struct *napi, int budget)
6206{
6207 struct tg3_napi *tnapi = container_of(napi, struct tg3_napi, napi);
6208 struct tg3 *tp = tnapi->tp;
6209 int work_done = 0;
6210 struct tg3_hw_status *sblk = tnapi->hw_status;
6211
6212 while (1) {
6213 work_done = tg3_poll_work(tnapi, work_done, budget);
6214
Joe Perches63c3a662011-04-26 08:12:10 +00006215 if (unlikely(tg3_flag(tp, TX_RECOVERY_PENDING)))
Matt Carlson35f2d7d2009-11-13 13:03:41 +00006216 goto tx_recovery;
6217
6218 if (unlikely(work_done >= budget))
6219 break;
6220
Matt Carlsonc6cdf432010-04-05 10:19:26 +00006221 /* tp->last_tag is used in tg3_int_reenable() below
Matt Carlson35f2d7d2009-11-13 13:03:41 +00006222 * to tell the hw how much work has been processed,
6223 * so we must read it before checking for more work.
6224 */
6225 tnapi->last_tag = sblk->status_tag;
6226 tnapi->last_irq_tag = tnapi->last_tag;
6227 rmb();
6228
6229 /* check for RX/TX work to do */
Matt Carlson6d40db72010-04-05 10:19:20 +00006230 if (likely(sblk->idx[0].tx_consumer == tnapi->tx_cons &&
6231 *(tnapi->rx_rcb_prod_idx) == tnapi->rx_rcb_ptr)) {
Michael Chan7ae52892012-03-21 15:38:33 +00006232
6233 /* This test here is not race free, but will reduce
6234 * the number of interrupts by looping again.
6235 */
6236 if (tnapi == &tp->napi[1] && tp->rx_refill)
6237 continue;
6238
Matt Carlson35f2d7d2009-11-13 13:03:41 +00006239 napi_complete(napi);
6240 /* Reenable interrupts. */
6241 tw32_mailbox(tnapi->int_mbox, tnapi->last_tag << 24);
Michael Chan7ae52892012-03-21 15:38:33 +00006242
6243 /* This test here is synchronized by napi_schedule()
6244 * and napi_complete() to close the race condition.
6245 */
6246 if (unlikely(tnapi == &tp->napi[1] && tp->rx_refill)) {
6247 tw32(HOSTCC_MODE, tp->coalesce_mode |
6248 HOSTCC_MODE_ENABLE |
6249 tnapi->coal_now);
6250 }
Matt Carlson35f2d7d2009-11-13 13:03:41 +00006251 mmiowb();
6252 break;
6253 }
6254 }
6255
6256 return work_done;
6257
6258tx_recovery:
6259 /* work_done is guaranteed to be less than budget. */
6260 napi_complete(napi);
Matt Carlsondb219972011-11-04 09:15:03 +00006261 tg3_reset_task_schedule(tp);
Matt Carlson35f2d7d2009-11-13 13:03:41 +00006262 return work_done;
6263}
6264
Matt Carlsone64de4e2011-04-13 11:05:05 +00006265static void tg3_process_error(struct tg3 *tp)
6266{
6267 u32 val;
6268 bool real_error = false;
6269
Joe Perches63c3a662011-04-26 08:12:10 +00006270 if (tg3_flag(tp, ERROR_PROCESSED))
Matt Carlsone64de4e2011-04-13 11:05:05 +00006271 return;
6272
6273 /* Check Flow Attention register */
6274 val = tr32(HOSTCC_FLOW_ATTN);
6275 if (val & ~HOSTCC_FLOW_ATTN_MBUF_LWM) {
6276 netdev_err(tp->dev, "FLOW Attention error. Resetting chip.\n");
6277 real_error = true;
6278 }
6279
6280 if (tr32(MSGINT_STATUS) & ~MSGINT_STATUS_MSI_REQ) {
6281 netdev_err(tp->dev, "MSI Status error. Resetting chip.\n");
6282 real_error = true;
6283 }
6284
6285 if (tr32(RDMAC_STATUS) || tr32(WDMAC_STATUS)) {
6286 netdev_err(tp->dev, "DMA Status error. Resetting chip.\n");
6287 real_error = true;
6288 }
6289
6290 if (!real_error)
6291 return;
6292
6293 tg3_dump_state(tp);
6294
Joe Perches63c3a662011-04-26 08:12:10 +00006295 tg3_flag_set(tp, ERROR_PROCESSED);
Matt Carlsondb219972011-11-04 09:15:03 +00006296 tg3_reset_task_schedule(tp);
Matt Carlsone64de4e2011-04-13 11:05:05 +00006297}
6298
David S. Miller6f535762007-10-11 18:08:29 -07006299static int tg3_poll(struct napi_struct *napi, int budget)
6300{
Matt Carlson8ef04422009-08-28 14:01:37 +00006301 struct tg3_napi *tnapi = container_of(napi, struct tg3_napi, napi);
6302 struct tg3 *tp = tnapi->tp;
David S. Miller6f535762007-10-11 18:08:29 -07006303 int work_done = 0;
Matt Carlson898a56f2009-08-28 14:02:40 +00006304 struct tg3_hw_status *sblk = tnapi->hw_status;
David S. Miller6f535762007-10-11 18:08:29 -07006305
6306 while (1) {
Matt Carlsone64de4e2011-04-13 11:05:05 +00006307 if (sblk->status & SD_STATUS_ERROR)
6308 tg3_process_error(tp);
6309
Matt Carlson35f2d7d2009-11-13 13:03:41 +00006310 tg3_poll_link(tp);
6311
Matt Carlson17375d22009-08-28 14:02:18 +00006312 work_done = tg3_poll_work(tnapi, work_done, budget);
David S. Miller6f535762007-10-11 18:08:29 -07006313
Joe Perches63c3a662011-04-26 08:12:10 +00006314 if (unlikely(tg3_flag(tp, TX_RECOVERY_PENDING)))
David S. Miller6f535762007-10-11 18:08:29 -07006315 goto tx_recovery;
6316
6317 if (unlikely(work_done >= budget))
6318 break;
6319
Joe Perches63c3a662011-04-26 08:12:10 +00006320 if (tg3_flag(tp, TAGGED_STATUS)) {
Matt Carlson17375d22009-08-28 14:02:18 +00006321 /* tp->last_tag is used in tg3_int_reenable() below
Michael Chan4fd7ab52007-10-12 01:39:50 -07006322 * to tell the hw how much work has been processed,
6323 * so we must read it before checking for more work.
6324 */
Matt Carlson898a56f2009-08-28 14:02:40 +00006325 tnapi->last_tag = sblk->status_tag;
6326 tnapi->last_irq_tag = tnapi->last_tag;
Michael Chan4fd7ab52007-10-12 01:39:50 -07006327 rmb();
6328 } else
6329 sblk->status &= ~SD_STATUS_UPDATED;
6330
Matt Carlson17375d22009-08-28 14:02:18 +00006331 if (likely(!tg3_has_work(tnapi))) {
Ben Hutchings288379f2009-01-19 16:43:59 -08006332 napi_complete(napi);
Matt Carlson17375d22009-08-28 14:02:18 +00006333 tg3_int_reenable(tnapi);
David S. Miller6f535762007-10-11 18:08:29 -07006334 break;
6335 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006336 }
6337
Stephen Hemmingerbea33482007-10-03 16:41:36 -07006338 return work_done;
David S. Miller6f535762007-10-11 18:08:29 -07006339
6340tx_recovery:
Michael Chan4fd7ab52007-10-12 01:39:50 -07006341 /* work_done is guaranteed to be less than budget. */
Ben Hutchings288379f2009-01-19 16:43:59 -08006342 napi_complete(napi);
Matt Carlsondb219972011-11-04 09:15:03 +00006343 tg3_reset_task_schedule(tp);
Michael Chan4fd7ab52007-10-12 01:39:50 -07006344 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006345}
6346
Matt Carlson66cfd1b2010-09-30 10:34:30 +00006347static void tg3_napi_disable(struct tg3 *tp)
6348{
6349 int i;
6350
6351 for (i = tp->irq_cnt - 1; i >= 0; i--)
6352 napi_disable(&tp->napi[i].napi);
6353}
6354
6355static void tg3_napi_enable(struct tg3 *tp)
6356{
6357 int i;
6358
6359 for (i = 0; i < tp->irq_cnt; i++)
6360 napi_enable(&tp->napi[i].napi);
6361}
6362
6363static void tg3_napi_init(struct tg3 *tp)
6364{
6365 int i;
6366
6367 netif_napi_add(tp->dev, &tp->napi[0].napi, tg3_poll, 64);
6368 for (i = 1; i < tp->irq_cnt; i++)
6369 netif_napi_add(tp->dev, &tp->napi[i].napi, tg3_poll_msix, 64);
6370}
6371
6372static void tg3_napi_fini(struct tg3 *tp)
6373{
6374 int i;
6375
6376 for (i = 0; i < tp->irq_cnt; i++)
6377 netif_napi_del(&tp->napi[i].napi);
6378}
6379
6380static inline void tg3_netif_stop(struct tg3 *tp)
6381{
6382 tp->dev->trans_start = jiffies; /* prevent tx timeout */
6383 tg3_napi_disable(tp);
6384 netif_tx_disable(tp->dev);
6385}
6386
6387static inline void tg3_netif_start(struct tg3 *tp)
6388{
6389 /* NOTE: unconditional netif_tx_wake_all_queues is only
6390 * appropriate so long as all callers are assured to
6391 * have free tx slots (such as after tg3_init_hw)
6392 */
6393 netif_tx_wake_all_queues(tp->dev);
6394
6395 tg3_napi_enable(tp);
6396 tp->napi[0].hw_status->status |= SD_STATUS_UPDATED;
6397 tg3_enable_ints(tp);
6398}
6399
David S. Millerf47c11e2005-06-24 20:18:35 -07006400static void tg3_irq_quiesce(struct tg3 *tp)
6401{
Matt Carlson4f125f42009-09-01 12:55:02 +00006402 int i;
6403
David S. Millerf47c11e2005-06-24 20:18:35 -07006404 BUG_ON(tp->irq_sync);
6405
6406 tp->irq_sync = 1;
6407 smp_mb();
6408
Matt Carlson4f125f42009-09-01 12:55:02 +00006409 for (i = 0; i < tp->irq_cnt; i++)
6410 synchronize_irq(tp->napi[i].irq_vec);
David S. Millerf47c11e2005-06-24 20:18:35 -07006411}
6412
David S. Millerf47c11e2005-06-24 20:18:35 -07006413/* Fully shutdown all tg3 driver activity elsewhere in the system.
6414 * If irq_sync is non-zero, then the IRQ handler must be synchronized
6415 * with as well. Most of the time, this is not necessary except when
6416 * shutting down the device.
6417 */
6418static inline void tg3_full_lock(struct tg3 *tp, int irq_sync)
6419{
Michael Chan46966542007-07-11 19:47:19 -07006420 spin_lock_bh(&tp->lock);
David S. Millerf47c11e2005-06-24 20:18:35 -07006421 if (irq_sync)
6422 tg3_irq_quiesce(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -07006423}
6424
6425static inline void tg3_full_unlock(struct tg3 *tp)
6426{
David S. Millerf47c11e2005-06-24 20:18:35 -07006427 spin_unlock_bh(&tp->lock);
6428}
6429
Michael Chanfcfa0a32006-03-20 22:28:41 -08006430/* One-shot MSI handler - Chip automatically disables interrupt
6431 * after sending MSI so driver doesn't have to do it.
6432 */
David Howells7d12e782006-10-05 14:55:46 +01006433static irqreturn_t tg3_msi_1shot(int irq, void *dev_id)
Michael Chanfcfa0a32006-03-20 22:28:41 -08006434{
Matt Carlson09943a12009-08-28 14:01:57 +00006435 struct tg3_napi *tnapi = dev_id;
6436 struct tg3 *tp = tnapi->tp;
Michael Chanfcfa0a32006-03-20 22:28:41 -08006437
Matt Carlson898a56f2009-08-28 14:02:40 +00006438 prefetch(tnapi->hw_status);
Matt Carlson0c1d0e22009-09-01 13:16:33 +00006439 if (tnapi->rx_rcb)
6440 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Michael Chanfcfa0a32006-03-20 22:28:41 -08006441
6442 if (likely(!tg3_irq_sync(tp)))
Matt Carlson09943a12009-08-28 14:01:57 +00006443 napi_schedule(&tnapi->napi);
Michael Chanfcfa0a32006-03-20 22:28:41 -08006444
6445 return IRQ_HANDLED;
6446}
6447
Michael Chan88b06bc22005-04-21 17:13:25 -07006448/* MSI ISR - No need to check for interrupt sharing and no need to
6449 * flush status block and interrupt mailbox. PCI ordering rules
6450 * guarantee that MSI will arrive after the status block.
6451 */
David Howells7d12e782006-10-05 14:55:46 +01006452static irqreturn_t tg3_msi(int irq, void *dev_id)
Michael Chan88b06bc22005-04-21 17:13:25 -07006453{
Matt Carlson09943a12009-08-28 14:01:57 +00006454 struct tg3_napi *tnapi = dev_id;
6455 struct tg3 *tp = tnapi->tp;
Michael Chan88b06bc22005-04-21 17:13:25 -07006456
Matt Carlson898a56f2009-08-28 14:02:40 +00006457 prefetch(tnapi->hw_status);
Matt Carlson0c1d0e22009-09-01 13:16:33 +00006458 if (tnapi->rx_rcb)
6459 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Michael Chan88b06bc22005-04-21 17:13:25 -07006460 /*
David S. Millerfac9b832005-05-18 22:46:34 -07006461 * Writing any value to intr-mbox-0 clears PCI INTA# and
Michael Chan88b06bc22005-04-21 17:13:25 -07006462 * chip-internal interrupt pending events.
David S. Millerfac9b832005-05-18 22:46:34 -07006463 * Writing non-zero to intr-mbox-0 additional tells the
Michael Chan88b06bc22005-04-21 17:13:25 -07006464 * NIC to stop sending us irqs, engaging "in-intr-handler"
6465 * event coalescing.
6466 */
Matt Carlson5b39de92011-08-31 11:44:50 +00006467 tw32_mailbox(tnapi->int_mbox, 0x00000001);
Michael Chan61487482005-09-05 17:53:19 -07006468 if (likely(!tg3_irq_sync(tp)))
Matt Carlson09943a12009-08-28 14:01:57 +00006469 napi_schedule(&tnapi->napi);
Michael Chan61487482005-09-05 17:53:19 -07006470
Michael Chan88b06bc22005-04-21 17:13:25 -07006471 return IRQ_RETVAL(1);
6472}
6473
David Howells7d12e782006-10-05 14:55:46 +01006474static irqreturn_t tg3_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006475{
Matt Carlson09943a12009-08-28 14:01:57 +00006476 struct tg3_napi *tnapi = dev_id;
6477 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00006478 struct tg3_hw_status *sblk = tnapi->hw_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006479 unsigned int handled = 1;
6480
Linus Torvalds1da177e2005-04-16 15:20:36 -07006481 /* In INTx mode, it is possible for the interrupt to arrive at
6482 * the CPU before the status block posted prior to the interrupt.
6483 * Reading the PCI State register will confirm whether the
6484 * interrupt is ours and will flush the status block.
6485 */
Michael Chand18edcb2007-03-24 20:57:11 -07006486 if (unlikely(!(sblk->status & SD_STATUS_UPDATED))) {
Joe Perches63c3a662011-04-26 08:12:10 +00006487 if (tg3_flag(tp, CHIP_RESETTING) ||
Michael Chand18edcb2007-03-24 20:57:11 -07006488 (tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
6489 handled = 0;
David S. Millerf47c11e2005-06-24 20:18:35 -07006490 goto out;
David S. Millerfac9b832005-05-18 22:46:34 -07006491 }
Michael Chand18edcb2007-03-24 20:57:11 -07006492 }
6493
6494 /*
6495 * Writing any value to intr-mbox-0 clears PCI INTA# and
6496 * chip-internal interrupt pending events.
6497 * Writing non-zero to intr-mbox-0 additional tells the
6498 * NIC to stop sending us irqs, engaging "in-intr-handler"
6499 * event coalescing.
Michael Chanc04cb342007-05-07 00:26:15 -07006500 *
6501 * Flush the mailbox to de-assert the IRQ immediately to prevent
6502 * spurious interrupts. The flush impacts performance but
6503 * excessive spurious interrupts can be worse in some cases.
Michael Chand18edcb2007-03-24 20:57:11 -07006504 */
Michael Chanc04cb342007-05-07 00:26:15 -07006505 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 0x00000001);
Michael Chand18edcb2007-03-24 20:57:11 -07006506 if (tg3_irq_sync(tp))
6507 goto out;
6508 sblk->status &= ~SD_STATUS_UPDATED;
Matt Carlson17375d22009-08-28 14:02:18 +00006509 if (likely(tg3_has_work(tnapi))) {
Matt Carlson72334482009-08-28 14:03:01 +00006510 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Matt Carlson09943a12009-08-28 14:01:57 +00006511 napi_schedule(&tnapi->napi);
Michael Chand18edcb2007-03-24 20:57:11 -07006512 } else {
6513 /* No work, shared interrupt perhaps? re-enable
6514 * interrupts, and flush that PCI write
6515 */
6516 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW,
6517 0x00000000);
David S. Millerfac9b832005-05-18 22:46:34 -07006518 }
David S. Millerf47c11e2005-06-24 20:18:35 -07006519out:
David S. Millerfac9b832005-05-18 22:46:34 -07006520 return IRQ_RETVAL(handled);
6521}
6522
David Howells7d12e782006-10-05 14:55:46 +01006523static irqreturn_t tg3_interrupt_tagged(int irq, void *dev_id)
David S. Millerfac9b832005-05-18 22:46:34 -07006524{
Matt Carlson09943a12009-08-28 14:01:57 +00006525 struct tg3_napi *tnapi = dev_id;
6526 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00006527 struct tg3_hw_status *sblk = tnapi->hw_status;
David S. Millerfac9b832005-05-18 22:46:34 -07006528 unsigned int handled = 1;
6529
David S. Millerfac9b832005-05-18 22:46:34 -07006530 /* In INTx mode, it is possible for the interrupt to arrive at
6531 * the CPU before the status block posted prior to the interrupt.
6532 * Reading the PCI State register will confirm whether the
6533 * interrupt is ours and will flush the status block.
6534 */
Matt Carlson898a56f2009-08-28 14:02:40 +00006535 if (unlikely(sblk->status_tag == tnapi->last_irq_tag)) {
Joe Perches63c3a662011-04-26 08:12:10 +00006536 if (tg3_flag(tp, CHIP_RESETTING) ||
Michael Chand18edcb2007-03-24 20:57:11 -07006537 (tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
6538 handled = 0;
David S. Millerf47c11e2005-06-24 20:18:35 -07006539 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006540 }
Michael Chand18edcb2007-03-24 20:57:11 -07006541 }
6542
6543 /*
6544 * writing any value to intr-mbox-0 clears PCI INTA# and
6545 * chip-internal interrupt pending events.
6546 * writing non-zero to intr-mbox-0 additional tells the
6547 * NIC to stop sending us irqs, engaging "in-intr-handler"
6548 * event coalescing.
Michael Chanc04cb342007-05-07 00:26:15 -07006549 *
6550 * Flush the mailbox to de-assert the IRQ immediately to prevent
6551 * spurious interrupts. The flush impacts performance but
6552 * excessive spurious interrupts can be worse in some cases.
Michael Chand18edcb2007-03-24 20:57:11 -07006553 */
Michael Chanc04cb342007-05-07 00:26:15 -07006554 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 0x00000001);
Matt Carlson624f8e52009-04-20 06:55:01 +00006555
6556 /*
6557 * In a shared interrupt configuration, sometimes other devices'
6558 * interrupts will scream. We record the current status tag here
6559 * so that the above check can report that the screaming interrupts
6560 * are unhandled. Eventually they will be silenced.
6561 */
Matt Carlson898a56f2009-08-28 14:02:40 +00006562 tnapi->last_irq_tag = sblk->status_tag;
Matt Carlson624f8e52009-04-20 06:55:01 +00006563
Michael Chand18edcb2007-03-24 20:57:11 -07006564 if (tg3_irq_sync(tp))
6565 goto out;
Matt Carlson624f8e52009-04-20 06:55:01 +00006566
Matt Carlson72334482009-08-28 14:03:01 +00006567 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Matt Carlson624f8e52009-04-20 06:55:01 +00006568
Matt Carlson09943a12009-08-28 14:01:57 +00006569 napi_schedule(&tnapi->napi);
Matt Carlson624f8e52009-04-20 06:55:01 +00006570
David S. Millerf47c11e2005-06-24 20:18:35 -07006571out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006572 return IRQ_RETVAL(handled);
6573}
6574
Michael Chan79381092005-04-21 17:13:59 -07006575/* ISR for interrupt test */
David Howells7d12e782006-10-05 14:55:46 +01006576static irqreturn_t tg3_test_isr(int irq, void *dev_id)
Michael Chan79381092005-04-21 17:13:59 -07006577{
Matt Carlson09943a12009-08-28 14:01:57 +00006578 struct tg3_napi *tnapi = dev_id;
6579 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00006580 struct tg3_hw_status *sblk = tnapi->hw_status;
Michael Chan79381092005-04-21 17:13:59 -07006581
Michael Chanf9804dd2005-09-27 12:13:10 -07006582 if ((sblk->status & SD_STATUS_UPDATED) ||
6583 !(tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
Michael Chanb16250e2006-09-27 16:10:14 -07006584 tg3_disable_ints(tp);
Michael Chan79381092005-04-21 17:13:59 -07006585 return IRQ_RETVAL(1);
6586 }
6587 return IRQ_RETVAL(0);
6588}
6589
Linus Torvalds1da177e2005-04-16 15:20:36 -07006590#ifdef CONFIG_NET_POLL_CONTROLLER
6591static void tg3_poll_controller(struct net_device *dev)
6592{
Matt Carlson4f125f42009-09-01 12:55:02 +00006593 int i;
Michael Chan88b06bc22005-04-21 17:13:25 -07006594 struct tg3 *tp = netdev_priv(dev);
6595
Matt Carlson4f125f42009-09-01 12:55:02 +00006596 for (i = 0; i < tp->irq_cnt; i++)
Louis Rillingfe234f02010-03-09 06:14:41 +00006597 tg3_interrupt(tp->napi[i].irq_vec, &tp->napi[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006598}
6599#endif
6600
Linus Torvalds1da177e2005-04-16 15:20:36 -07006601static void tg3_tx_timeout(struct net_device *dev)
6602{
6603 struct tg3 *tp = netdev_priv(dev);
6604
Michael Chanb0408752007-02-13 12:18:30 -08006605 if (netif_msg_tx_err(tp)) {
Joe Perches05dbe002010-02-17 19:44:19 +00006606 netdev_err(dev, "transmit timed out, resetting\n");
Matt Carlson97bd8e42011-04-13 11:05:04 +00006607 tg3_dump_state(tp);
Michael Chanb0408752007-02-13 12:18:30 -08006608 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006609
Matt Carlsondb219972011-11-04 09:15:03 +00006610 tg3_reset_task_schedule(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006611}
6612
Michael Chanc58ec932005-09-17 00:46:27 -07006613/* Test for DMA buffers crossing any 4GB boundaries: 4G, 8G, etc */
6614static inline int tg3_4g_overflow_test(dma_addr_t mapping, int len)
6615{
6616 u32 base = (u32) mapping & 0xffffffff;
6617
Eric Dumazet807540b2010-09-23 05:40:09 +00006618 return (base > 0xffffdcc0) && (base + len + 8 < base);
Michael Chanc58ec932005-09-17 00:46:27 -07006619}
6620
Michael Chan72f2afb2006-03-06 19:28:35 -08006621/* Test for DMA addresses > 40-bit */
6622static inline int tg3_40bit_overflow_test(struct tg3 *tp, dma_addr_t mapping,
6623 int len)
6624{
6625#if defined(CONFIG_HIGHMEM) && (BITS_PER_LONG == 64)
Joe Perches63c3a662011-04-26 08:12:10 +00006626 if (tg3_flag(tp, 40BIT_DMA_BUG))
Eric Dumazet807540b2010-09-23 05:40:09 +00006627 return ((u64) mapping + len) > DMA_BIT_MASK(40);
Michael Chan72f2afb2006-03-06 19:28:35 -08006628 return 0;
6629#else
6630 return 0;
6631#endif
6632}
6633
Matt Carlsond1a3b732011-07-27 14:20:51 +00006634static inline void tg3_tx_set_bd(struct tg3_tx_buffer_desc *txbd,
Matt Carlson92cd3a12011-07-27 14:20:47 +00006635 dma_addr_t mapping, u32 len, u32 flags,
6636 u32 mss, u32 vlan)
Matt Carlson2ffcc982011-05-19 12:12:44 +00006637{
Matt Carlson92cd3a12011-07-27 14:20:47 +00006638 txbd->addr_hi = ((u64) mapping >> 32);
6639 txbd->addr_lo = ((u64) mapping & 0xffffffff);
6640 txbd->len_flags = (len << TXD_LEN_SHIFT) | (flags & 0x0000ffff);
6641 txbd->vlan_tag = (mss << TXD_MSS_SHIFT) | (vlan << TXD_VLAN_TAG_SHIFT);
Matt Carlson2ffcc982011-05-19 12:12:44 +00006642}
Linus Torvalds1da177e2005-04-16 15:20:36 -07006643
Matt Carlson84b67b22011-07-27 14:20:52 +00006644static bool tg3_tx_frag_set(struct tg3_napi *tnapi, u32 *entry, u32 *budget,
Matt Carlsond1a3b732011-07-27 14:20:51 +00006645 dma_addr_t map, u32 len, u32 flags,
6646 u32 mss, u32 vlan)
6647{
6648 struct tg3 *tp = tnapi->tp;
6649 bool hwbug = false;
6650
6651 if (tg3_flag(tp, SHORT_DMA_BUG) && len <= 8)
Rusty Russell3db1cd52011-12-19 13:56:45 +00006652 hwbug = true;
Matt Carlsond1a3b732011-07-27 14:20:51 +00006653
6654 if (tg3_4g_overflow_test(map, len))
Rusty Russell3db1cd52011-12-19 13:56:45 +00006655 hwbug = true;
Matt Carlsond1a3b732011-07-27 14:20:51 +00006656
6657 if (tg3_40bit_overflow_test(tp, map, len))
Rusty Russell3db1cd52011-12-19 13:56:45 +00006658 hwbug = true;
Matt Carlsond1a3b732011-07-27 14:20:51 +00006659
Matt Carlsona4cb4282011-12-14 11:09:58 +00006660 if (tp->dma_limit) {
Matt Carlsonb9e45482011-11-04 09:14:59 +00006661 u32 prvidx = *entry;
Matt Carlsone31aa982011-07-27 14:20:53 +00006662 u32 tmp_flag = flags & ~TXD_FLAG_END;
Matt Carlsona4cb4282011-12-14 11:09:58 +00006663 while (len > tp->dma_limit && *budget) {
6664 u32 frag_len = tp->dma_limit;
6665 len -= tp->dma_limit;
Matt Carlsone31aa982011-07-27 14:20:53 +00006666
Matt Carlsonb9e45482011-11-04 09:14:59 +00006667 /* Avoid the 8byte DMA problem */
6668 if (len <= 8) {
Matt Carlsona4cb4282011-12-14 11:09:58 +00006669 len += tp->dma_limit / 2;
6670 frag_len = tp->dma_limit / 2;
Matt Carlsone31aa982011-07-27 14:20:53 +00006671 }
6672
Matt Carlsonb9e45482011-11-04 09:14:59 +00006673 tnapi->tx_buffers[*entry].fragmented = true;
6674
6675 tg3_tx_set_bd(&tnapi->tx_ring[*entry], map,
6676 frag_len, tmp_flag, mss, vlan);
6677 *budget -= 1;
6678 prvidx = *entry;
6679 *entry = NEXT_TX(*entry);
6680
Matt Carlsone31aa982011-07-27 14:20:53 +00006681 map += frag_len;
6682 }
6683
6684 if (len) {
6685 if (*budget) {
6686 tg3_tx_set_bd(&tnapi->tx_ring[*entry], map,
6687 len, flags, mss, vlan);
Matt Carlsonb9e45482011-11-04 09:14:59 +00006688 *budget -= 1;
Matt Carlsone31aa982011-07-27 14:20:53 +00006689 *entry = NEXT_TX(*entry);
6690 } else {
Rusty Russell3db1cd52011-12-19 13:56:45 +00006691 hwbug = true;
Matt Carlsonb9e45482011-11-04 09:14:59 +00006692 tnapi->tx_buffers[prvidx].fragmented = false;
Matt Carlsone31aa982011-07-27 14:20:53 +00006693 }
6694 }
6695 } else {
Matt Carlson84b67b22011-07-27 14:20:52 +00006696 tg3_tx_set_bd(&tnapi->tx_ring[*entry], map,
6697 len, flags, mss, vlan);
Matt Carlsone31aa982011-07-27 14:20:53 +00006698 *entry = NEXT_TX(*entry);
6699 }
Matt Carlsond1a3b732011-07-27 14:20:51 +00006700
6701 return hwbug;
6702}
6703
Matt Carlson0d681b22011-07-27 14:20:49 +00006704static void tg3_tx_skb_unmap(struct tg3_napi *tnapi, u32 entry, int last)
Matt Carlson432aa7e2011-05-19 12:12:45 +00006705{
6706 int i;
Matt Carlson0d681b22011-07-27 14:20:49 +00006707 struct sk_buff *skb;
Matt Carlsondf8944c2011-07-27 14:20:46 +00006708 struct tg3_tx_ring_info *txb = &tnapi->tx_buffers[entry];
Matt Carlson432aa7e2011-05-19 12:12:45 +00006709
Matt Carlson0d681b22011-07-27 14:20:49 +00006710 skb = txb->skb;
6711 txb->skb = NULL;
6712
Matt Carlson432aa7e2011-05-19 12:12:45 +00006713 pci_unmap_single(tnapi->tp->pdev,
6714 dma_unmap_addr(txb, mapping),
6715 skb_headlen(skb),
6716 PCI_DMA_TODEVICE);
Matt Carlsone01ee142011-07-27 14:20:50 +00006717
6718 while (txb->fragmented) {
6719 txb->fragmented = false;
6720 entry = NEXT_TX(entry);
6721 txb = &tnapi->tx_buffers[entry];
6722 }
6723
Matt Carlsonba1142e2011-11-04 09:15:00 +00006724 for (i = 0; i <= last; i++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00006725 const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
Matt Carlson432aa7e2011-05-19 12:12:45 +00006726
6727 entry = NEXT_TX(entry);
6728 txb = &tnapi->tx_buffers[entry];
6729
6730 pci_unmap_page(tnapi->tp->pdev,
6731 dma_unmap_addr(txb, mapping),
Eric Dumazet9e903e02011-10-18 21:00:24 +00006732 skb_frag_size(frag), PCI_DMA_TODEVICE);
Matt Carlsone01ee142011-07-27 14:20:50 +00006733
6734 while (txb->fragmented) {
6735 txb->fragmented = false;
6736 entry = NEXT_TX(entry);
6737 txb = &tnapi->tx_buffers[entry];
6738 }
Matt Carlson432aa7e2011-05-19 12:12:45 +00006739 }
6740}
6741
Michael Chan72f2afb2006-03-06 19:28:35 -08006742/* Workaround 4GB and 40-bit hardware DMA bugs. */
Matt Carlson24f4efd2009-11-13 13:03:35 +00006743static int tigon3_dma_hwbug_workaround(struct tg3_napi *tnapi,
David S. Miller1805b2f2011-10-24 18:18:09 -04006744 struct sk_buff **pskb,
Matt Carlson84b67b22011-07-27 14:20:52 +00006745 u32 *entry, u32 *budget,
Matt Carlson92cd3a12011-07-27 14:20:47 +00006746 u32 base_flags, u32 mss, u32 vlan)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006747{
Matt Carlson24f4efd2009-11-13 13:03:35 +00006748 struct tg3 *tp = tnapi->tp;
David S. Miller1805b2f2011-10-24 18:18:09 -04006749 struct sk_buff *new_skb, *skb = *pskb;
Michael Chanc58ec932005-09-17 00:46:27 -07006750 dma_addr_t new_addr = 0;
Matt Carlson432aa7e2011-05-19 12:12:45 +00006751 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006752
Matt Carlson41588ba2008-04-19 18:12:33 -07006753 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701)
6754 new_skb = skb_copy(skb, GFP_ATOMIC);
6755 else {
6756 int more_headroom = 4 - ((unsigned long)skb->data & 3);
6757
6758 new_skb = skb_copy_expand(skb,
6759 skb_headroom(skb) + more_headroom,
6760 skb_tailroom(skb), GFP_ATOMIC);
6761 }
6762
Linus Torvalds1da177e2005-04-16 15:20:36 -07006763 if (!new_skb) {
Michael Chanc58ec932005-09-17 00:46:27 -07006764 ret = -1;
6765 } else {
6766 /* New SKB is guaranteed to be linear. */
Alexander Duyckf4188d82009-12-02 16:48:38 +00006767 new_addr = pci_map_single(tp->pdev, new_skb->data, new_skb->len,
6768 PCI_DMA_TODEVICE);
6769 /* Make sure the mapping succeeded */
6770 if (pci_dma_mapping_error(tp->pdev, new_addr)) {
Alexander Duyckf4188d82009-12-02 16:48:38 +00006771 dev_kfree_skb(new_skb);
Michael Chanc58ec932005-09-17 00:46:27 -07006772 ret = -1;
Michael Chanc58ec932005-09-17 00:46:27 -07006773 } else {
Matt Carlsonb9e45482011-11-04 09:14:59 +00006774 u32 save_entry = *entry;
6775
Matt Carlson92cd3a12011-07-27 14:20:47 +00006776 base_flags |= TXD_FLAG_END;
6777
Matt Carlson84b67b22011-07-27 14:20:52 +00006778 tnapi->tx_buffers[*entry].skb = new_skb;
6779 dma_unmap_addr_set(&tnapi->tx_buffers[*entry],
Matt Carlson432aa7e2011-05-19 12:12:45 +00006780 mapping, new_addr);
6781
Matt Carlson84b67b22011-07-27 14:20:52 +00006782 if (tg3_tx_frag_set(tnapi, entry, budget, new_addr,
Matt Carlsond1a3b732011-07-27 14:20:51 +00006783 new_skb->len, base_flags,
6784 mss, vlan)) {
Matt Carlsonba1142e2011-11-04 09:15:00 +00006785 tg3_tx_skb_unmap(tnapi, save_entry, -1);
Matt Carlsond1a3b732011-07-27 14:20:51 +00006786 dev_kfree_skb(new_skb);
6787 ret = -1;
6788 }
Michael Chanc58ec932005-09-17 00:46:27 -07006789 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006790 }
6791
Linus Torvalds1da177e2005-04-16 15:20:36 -07006792 dev_kfree_skb(skb);
David S. Miller1805b2f2011-10-24 18:18:09 -04006793 *pskb = new_skb;
Michael Chanc58ec932005-09-17 00:46:27 -07006794 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006795}
6796
Matt Carlson2ffcc982011-05-19 12:12:44 +00006797static netdev_tx_t tg3_start_xmit(struct sk_buff *, struct net_device *);
Michael Chan52c0fd82006-06-29 20:15:54 -07006798
6799/* Use GSO to workaround a rare TSO bug that may be triggered when the
6800 * TSO header is greater than 80 bytes.
6801 */
6802static int tg3_tso_bug(struct tg3 *tp, struct sk_buff *skb)
6803{
6804 struct sk_buff *segs, *nskb;
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006805 u32 frag_cnt_est = skb_shinfo(skb)->gso_segs * 3;
Michael Chan52c0fd82006-06-29 20:15:54 -07006806
6807 /* Estimate the number of fragments in the worst case */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006808 if (unlikely(tg3_tx_avail(&tp->napi[0]) <= frag_cnt_est)) {
Michael Chan52c0fd82006-06-29 20:15:54 -07006809 netif_stop_queue(tp->dev);
Matt Carlsonf65aac12010-08-02 11:26:03 +00006810
6811 /* netif_tx_stop_queue() must be done before checking
6812 * checking tx index in tg3_tx_avail() below, because in
6813 * tg3_tx(), we update tx index before checking for
6814 * netif_tx_queue_stopped().
6815 */
6816 smp_mb();
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006817 if (tg3_tx_avail(&tp->napi[0]) <= frag_cnt_est)
Michael Chan7f62ad52007-02-20 23:25:40 -08006818 return NETDEV_TX_BUSY;
6819
6820 netif_wake_queue(tp->dev);
Michael Chan52c0fd82006-06-29 20:15:54 -07006821 }
6822
6823 segs = skb_gso_segment(skb, tp->dev->features & ~NETIF_F_TSO);
Hirofumi Nakagawa801678c2008-04-29 01:03:09 -07006824 if (IS_ERR(segs))
Michael Chan52c0fd82006-06-29 20:15:54 -07006825 goto tg3_tso_bug_end;
6826
6827 do {
6828 nskb = segs;
6829 segs = segs->next;
6830 nskb->next = NULL;
Matt Carlson2ffcc982011-05-19 12:12:44 +00006831 tg3_start_xmit(nskb, tp->dev);
Michael Chan52c0fd82006-06-29 20:15:54 -07006832 } while (segs);
6833
6834tg3_tso_bug_end:
6835 dev_kfree_skb(skb);
6836
6837 return NETDEV_TX_OK;
6838}
Michael Chan52c0fd82006-06-29 20:15:54 -07006839
Michael Chan5a6f3072006-03-20 22:28:05 -08006840/* hard_start_xmit for devices that have the 4G bug and/or 40-bit bug and
Joe Perches63c3a662011-04-26 08:12:10 +00006841 * support TG3_FLAG_HW_TSO_1 or firmware TSO only.
Michael Chan5a6f3072006-03-20 22:28:05 -08006842 */
Matt Carlson2ffcc982011-05-19 12:12:44 +00006843static netdev_tx_t tg3_start_xmit(struct sk_buff *skb, struct net_device *dev)
Michael Chan5a6f3072006-03-20 22:28:05 -08006844{
6845 struct tg3 *tp = netdev_priv(dev);
Matt Carlson92cd3a12011-07-27 14:20:47 +00006846 u32 len, entry, base_flags, mss, vlan = 0;
Matt Carlson84b67b22011-07-27 14:20:52 +00006847 u32 budget;
Matt Carlson432aa7e2011-05-19 12:12:45 +00006848 int i = -1, would_hit_hwbug;
David S. Miller90079ce2008-09-11 04:52:51 -07006849 dma_addr_t mapping;
Matt Carlson24f4efd2009-11-13 13:03:35 +00006850 struct tg3_napi *tnapi;
6851 struct netdev_queue *txq;
Matt Carlson432aa7e2011-05-19 12:12:45 +00006852 unsigned int last;
Alexander Duyckf4188d82009-12-02 16:48:38 +00006853
Matt Carlson24f4efd2009-11-13 13:03:35 +00006854 txq = netdev_get_tx_queue(dev, skb_get_queue_mapping(skb));
6855 tnapi = &tp->napi[skb_get_queue_mapping(skb)];
Joe Perches63c3a662011-04-26 08:12:10 +00006856 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlson24f4efd2009-11-13 13:03:35 +00006857 tnapi++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006858
Matt Carlson84b67b22011-07-27 14:20:52 +00006859 budget = tg3_tx_avail(tnapi);
6860
Michael Chan00b70502006-06-17 21:58:45 -07006861 /* We are running in BH disabled context with netif_tx_lock
Stephen Hemmingerbea33482007-10-03 16:41:36 -07006862 * and TX reclaim runs via tp->napi.poll inside of a software
David S. Millerf47c11e2005-06-24 20:18:35 -07006863 * interrupt. Furthermore, IRQ processing runs lockless so we have
6864 * no IRQ context deadlocks to worry about either. Rejoice!
Linus Torvalds1da177e2005-04-16 15:20:36 -07006865 */
Matt Carlson84b67b22011-07-27 14:20:52 +00006866 if (unlikely(budget <= (skb_shinfo(skb)->nr_frags + 1))) {
Matt Carlson24f4efd2009-11-13 13:03:35 +00006867 if (!netif_tx_queue_stopped(txq)) {
6868 netif_tx_stop_queue(txq);
Stephen Hemminger1f064a82005-12-06 17:36:44 -08006869
6870 /* This is a hard error, log it. */
Matt Carlson5129c3a2010-04-05 10:19:23 +00006871 netdev_err(dev,
6872 "BUG! Tx Ring full when queue awake!\n");
Stephen Hemminger1f064a82005-12-06 17:36:44 -08006873 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006874 return NETDEV_TX_BUSY;
6875 }
6876
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006877 entry = tnapi->tx_prod;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006878 base_flags = 0;
Patrick McHardy84fa7932006-08-29 16:44:56 -07006879 if (skb->ip_summed == CHECKSUM_PARTIAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006880 base_flags |= TXD_FLAG_TCPUDP_CSUM;
Matt Carlson24f4efd2009-11-13 13:03:35 +00006881
Matt Carlsonbe98da62010-07-11 09:31:46 +00006882 mss = skb_shinfo(skb)->gso_size;
6883 if (mss) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07006884 struct iphdr *iph;
Matt Carlson34195c32010-07-11 09:31:42 +00006885 u32 tcp_opt_len, hdr_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006886
6887 if (skb_header_cloned(skb) &&
Eric Dumazet48855432011-10-24 07:53:03 +00006888 pskb_expand_head(skb, 0, 0, GFP_ATOMIC))
6889 goto drop;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006890
Matt Carlson34195c32010-07-11 09:31:42 +00006891 iph = ip_hdr(skb);
Arnaldo Carvalho de Meloab6a5bb2007-03-18 17:43:48 -07006892 tcp_opt_len = tcp_optlen(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006893
Eric Dumazeta5a11952012-01-23 01:22:09 +00006894 hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb) - ETH_HLEN;
Matt Carlson34195c32010-07-11 09:31:42 +00006895
Eric Dumazeta5a11952012-01-23 01:22:09 +00006896 if (!skb_is_gso_v6(skb)) {
Matt Carlson34195c32010-07-11 09:31:42 +00006897 iph->check = 0;
6898 iph->tot_len = htons(mss + hdr_len);
6899 }
6900
Michael Chan52c0fd82006-06-29 20:15:54 -07006901 if (unlikely((ETH_HLEN + hdr_len) > 80) &&
Joe Perches63c3a662011-04-26 08:12:10 +00006902 tg3_flag(tp, TSO_BUG))
Matt Carlsonde6f31e2010-04-12 06:58:30 +00006903 return tg3_tso_bug(tp, skb);
Michael Chan52c0fd82006-06-29 20:15:54 -07006904
Linus Torvalds1da177e2005-04-16 15:20:36 -07006905 base_flags |= (TXD_FLAG_CPU_PRE_DMA |
6906 TXD_FLAG_CPU_POST_DMA);
6907
Joe Perches63c3a662011-04-26 08:12:10 +00006908 if (tg3_flag(tp, HW_TSO_1) ||
6909 tg3_flag(tp, HW_TSO_2) ||
6910 tg3_flag(tp, HW_TSO_3)) {
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07006911 tcp_hdr(skb)->check = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006912 base_flags &= ~TXD_FLAG_TCPUDP_CSUM;
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07006913 } else
6914 tcp_hdr(skb)->check = ~csum_tcpudp_magic(iph->saddr,
6915 iph->daddr, 0,
6916 IPPROTO_TCP,
6917 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006918
Joe Perches63c3a662011-04-26 08:12:10 +00006919 if (tg3_flag(tp, HW_TSO_3)) {
Matt Carlson615774f2009-11-13 13:03:39 +00006920 mss |= (hdr_len & 0xc) << 12;
6921 if (hdr_len & 0x10)
6922 base_flags |= 0x00000010;
6923 base_flags |= (hdr_len & 0x3e0) << 5;
Joe Perches63c3a662011-04-26 08:12:10 +00006924 } else if (tg3_flag(tp, HW_TSO_2))
Matt Carlson92c6b8d2009-11-02 14:23:27 +00006925 mss |= hdr_len << 9;
Joe Perches63c3a662011-04-26 08:12:10 +00006926 else if (tg3_flag(tp, HW_TSO_1) ||
Matt Carlson92c6b8d2009-11-02 14:23:27 +00006927 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07006928 if (tcp_opt_len || iph->ihl > 5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006929 int tsflags;
6930
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07006931 tsflags = (iph->ihl - 5) + (tcp_opt_len >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006932 mss |= (tsflags << 11);
6933 }
6934 } else {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07006935 if (tcp_opt_len || iph->ihl > 5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006936 int tsflags;
6937
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07006938 tsflags = (iph->ihl - 5) + (tcp_opt_len >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006939 base_flags |= tsflags << 12;
6940 }
6941 }
6942 }
Matt Carlsonbf933c82011-01-25 15:58:49 +00006943
Matt Carlson93a700a2011-08-31 11:44:54 +00006944 if (tg3_flag(tp, USE_JUMBO_BDFLAG) &&
6945 !mss && skb->len > VLAN_ETH_FRAME_LEN)
6946 base_flags |= TXD_FLAG_JMB_PKT;
6947
Matt Carlson92cd3a12011-07-27 14:20:47 +00006948 if (vlan_tx_tag_present(skb)) {
6949 base_flags |= TXD_FLAG_VLAN;
6950 vlan = vlan_tx_tag_get(skb);
6951 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006952
Alexander Duyckf4188d82009-12-02 16:48:38 +00006953 len = skb_headlen(skb);
6954
6955 mapping = pci_map_single(tp->pdev, skb->data, len, PCI_DMA_TODEVICE);
Eric Dumazet48855432011-10-24 07:53:03 +00006956 if (pci_dma_mapping_error(tp->pdev, mapping))
6957 goto drop;
6958
David S. Miller90079ce2008-09-11 04:52:51 -07006959
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006960 tnapi->tx_buffers[entry].skb = skb;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006961 dma_unmap_addr_set(&tnapi->tx_buffers[entry], mapping, mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006962
6963 would_hit_hwbug = 0;
6964
Joe Perches63c3a662011-04-26 08:12:10 +00006965 if (tg3_flag(tp, 5701_DMA_BUG))
Michael Chanc58ec932005-09-17 00:46:27 -07006966 would_hit_hwbug = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006967
Matt Carlson84b67b22011-07-27 14:20:52 +00006968 if (tg3_tx_frag_set(tnapi, &entry, &budget, mapping, len, base_flags |
Matt Carlsond1a3b732011-07-27 14:20:51 +00006969 ((skb_shinfo(skb)->nr_frags == 0) ? TXD_FLAG_END : 0),
Matt Carlsonba1142e2011-11-04 09:15:00 +00006970 mss, vlan)) {
Matt Carlsond1a3b732011-07-27 14:20:51 +00006971 would_hit_hwbug = 1;
Matt Carlsonba1142e2011-11-04 09:15:00 +00006972 } else if (skb_shinfo(skb)->nr_frags > 0) {
Matt Carlson92cd3a12011-07-27 14:20:47 +00006973 u32 tmp_mss = mss;
6974
6975 if (!tg3_flag(tp, HW_TSO_1) &&
6976 !tg3_flag(tp, HW_TSO_2) &&
6977 !tg3_flag(tp, HW_TSO_3))
6978 tmp_mss = 0;
6979
Matt Carlsonc5665a52012-02-13 10:20:12 +00006980 /* Now loop through additional data
6981 * fragments, and queue them.
6982 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006983 last = skb_shinfo(skb)->nr_frags - 1;
6984 for (i = 0; i <= last; i++) {
6985 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
6986
Eric Dumazet9e903e02011-10-18 21:00:24 +00006987 len = skb_frag_size(frag);
Ian Campbelldc234d02011-08-24 22:28:11 +00006988 mapping = skb_frag_dma_map(&tp->pdev->dev, frag, 0,
Ian Campbell5d6bcdf2011-10-06 11:10:48 +01006989 len, DMA_TO_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006990
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006991 tnapi->tx_buffers[entry].skb = NULL;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006992 dma_unmap_addr_set(&tnapi->tx_buffers[entry], mapping,
Alexander Duyckf4188d82009-12-02 16:48:38 +00006993 mapping);
Ian Campbell5d6bcdf2011-10-06 11:10:48 +01006994 if (dma_mapping_error(&tp->pdev->dev, mapping))
Alexander Duyckf4188d82009-12-02 16:48:38 +00006995 goto dma_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006996
Matt Carlsonb9e45482011-11-04 09:14:59 +00006997 if (!budget ||
6998 tg3_tx_frag_set(tnapi, &entry, &budget, mapping,
Matt Carlson84b67b22011-07-27 14:20:52 +00006999 len, base_flags |
7000 ((i == last) ? TXD_FLAG_END : 0),
Matt Carlsonb9e45482011-11-04 09:14:59 +00007001 tmp_mss, vlan)) {
Matt Carlson92c6b8d2009-11-02 14:23:27 +00007002 would_hit_hwbug = 1;
Matt Carlsonb9e45482011-11-04 09:14:59 +00007003 break;
7004 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007005 }
7006 }
7007
7008 if (would_hit_hwbug) {
Matt Carlson0d681b22011-07-27 14:20:49 +00007009 tg3_tx_skb_unmap(tnapi, tnapi->tx_prod, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007010
7011 /* If the workaround fails due to memory/mapping
7012 * failure, silently drop this packet.
7013 */
Matt Carlson84b67b22011-07-27 14:20:52 +00007014 entry = tnapi->tx_prod;
7015 budget = tg3_tx_avail(tnapi);
David S. Miller1805b2f2011-10-24 18:18:09 -04007016 if (tigon3_dma_hwbug_workaround(tnapi, &skb, &entry, &budget,
Matt Carlson84b67b22011-07-27 14:20:52 +00007017 base_flags, mss, vlan))
Eric Dumazet48855432011-10-24 07:53:03 +00007018 goto drop_nofree;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007019 }
7020
Richard Cochrand515b452011-06-19 03:31:41 +00007021 skb_tx_timestamp(skb);
Tom Herbert5cb917b2012-03-05 19:53:50 +00007022 netdev_tx_sent_queue(txq, skb->len);
Richard Cochrand515b452011-06-19 03:31:41 +00007023
Michael Chan6541b802012-03-04 14:48:14 +00007024 /* Sync BD data before updating mailbox */
7025 wmb();
7026
Linus Torvalds1da177e2005-04-16 15:20:36 -07007027 /* Packets are ready, update Tx producer idx local and on card. */
Matt Carlson24f4efd2009-11-13 13:03:35 +00007028 tw32_tx_mbox(tnapi->prodmbox, entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007029
Matt Carlsonf3f3f272009-08-28 14:03:21 +00007030 tnapi->tx_prod = entry;
7031 if (unlikely(tg3_tx_avail(tnapi) <= (MAX_SKB_FRAGS + 1))) {
Matt Carlson24f4efd2009-11-13 13:03:35 +00007032 netif_tx_stop_queue(txq);
Matt Carlsonf65aac12010-08-02 11:26:03 +00007033
7034 /* netif_tx_stop_queue() must be done before checking
7035 * checking tx index in tg3_tx_avail() below, because in
7036 * tg3_tx(), we update tx index before checking for
7037 * netif_tx_queue_stopped().
7038 */
7039 smp_mb();
Matt Carlsonf3f3f272009-08-28 14:03:21 +00007040 if (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi))
Matt Carlson24f4efd2009-11-13 13:03:35 +00007041 netif_tx_wake_queue(txq);
Michael Chan51b91462005-09-01 17:41:28 -07007042 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007043
Eric Dumazetcdd0db02009-05-28 00:00:41 +00007044 mmiowb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007045 return NETDEV_TX_OK;
Alexander Duyckf4188d82009-12-02 16:48:38 +00007046
7047dma_error:
Matt Carlsonba1142e2011-11-04 09:15:00 +00007048 tg3_tx_skb_unmap(tnapi, tnapi->tx_prod, --i);
Matt Carlson432aa7e2011-05-19 12:12:45 +00007049 tnapi->tx_buffers[tnapi->tx_prod].skb = NULL;
Eric Dumazet48855432011-10-24 07:53:03 +00007050drop:
7051 dev_kfree_skb(skb);
7052drop_nofree:
7053 tp->tx_dropped++;
Alexander Duyckf4188d82009-12-02 16:48:38 +00007054 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007055}
7056
Matt Carlson6e01b202011-08-19 13:58:20 +00007057static void tg3_mac_loopback(struct tg3 *tp, bool enable)
7058{
7059 if (enable) {
7060 tp->mac_mode &= ~(MAC_MODE_HALF_DUPLEX |
7061 MAC_MODE_PORT_MODE_MASK);
7062
7063 tp->mac_mode |= MAC_MODE_PORT_INT_LPBACK;
7064
7065 if (!tg3_flag(tp, 5705_PLUS))
7066 tp->mac_mode |= MAC_MODE_LINK_POLARITY;
7067
7068 if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
7069 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
7070 else
7071 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
7072 } else {
7073 tp->mac_mode &= ~MAC_MODE_PORT_INT_LPBACK;
7074
7075 if (tg3_flag(tp, 5705_PLUS) ||
7076 (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) ||
7077 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700)
7078 tp->mac_mode &= ~MAC_MODE_LINK_POLARITY;
7079 }
7080
7081 tw32(MAC_MODE, tp->mac_mode);
7082 udelay(40);
7083}
7084
Matt Carlson941ec902011-08-19 13:58:23 +00007085static int tg3_phy_lpbk_set(struct tg3 *tp, u32 speed, bool extlpbk)
Matt Carlson5e5a7f32011-08-19 13:58:21 +00007086{
Matt Carlson941ec902011-08-19 13:58:23 +00007087 u32 val, bmcr, mac_mode, ptest = 0;
Matt Carlson5e5a7f32011-08-19 13:58:21 +00007088
7089 tg3_phy_toggle_apd(tp, false);
7090 tg3_phy_toggle_automdix(tp, 0);
7091
Matt Carlson941ec902011-08-19 13:58:23 +00007092 if (extlpbk && tg3_phy_set_extloopbk(tp))
7093 return -EIO;
7094
7095 bmcr = BMCR_FULLDPLX;
Matt Carlson5e5a7f32011-08-19 13:58:21 +00007096 switch (speed) {
7097 case SPEED_10:
7098 break;
7099 case SPEED_100:
7100 bmcr |= BMCR_SPEED100;
7101 break;
7102 case SPEED_1000:
7103 default:
7104 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
7105 speed = SPEED_100;
7106 bmcr |= BMCR_SPEED100;
7107 } else {
7108 speed = SPEED_1000;
7109 bmcr |= BMCR_SPEED1000;
7110 }
7111 }
7112
Matt Carlson941ec902011-08-19 13:58:23 +00007113 if (extlpbk) {
7114 if (!(tp->phy_flags & TG3_PHYFLG_IS_FET)) {
7115 tg3_readphy(tp, MII_CTRL1000, &val);
7116 val |= CTL1000_AS_MASTER |
7117 CTL1000_ENABLE_MASTER;
7118 tg3_writephy(tp, MII_CTRL1000, val);
7119 } else {
7120 ptest = MII_TG3_FET_PTEST_TRIM_SEL |
7121 MII_TG3_FET_PTEST_TRIM_2;
7122 tg3_writephy(tp, MII_TG3_FET_PTEST, ptest);
7123 }
7124 } else
7125 bmcr |= BMCR_LOOPBACK;
7126
Matt Carlson5e5a7f32011-08-19 13:58:21 +00007127 tg3_writephy(tp, MII_BMCR, bmcr);
7128
7129 /* The write needs to be flushed for the FETs */
7130 if (tp->phy_flags & TG3_PHYFLG_IS_FET)
7131 tg3_readphy(tp, MII_BMCR, &bmcr);
7132
7133 udelay(40);
7134
7135 if ((tp->phy_flags & TG3_PHYFLG_IS_FET) &&
7136 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785) {
Matt Carlson941ec902011-08-19 13:58:23 +00007137 tg3_writephy(tp, MII_TG3_FET_PTEST, ptest |
Matt Carlson5e5a7f32011-08-19 13:58:21 +00007138 MII_TG3_FET_PTEST_FRC_TX_LINK |
7139 MII_TG3_FET_PTEST_FRC_TX_LOCK);
7140
7141 /* The write needs to be flushed for the AC131 */
7142 tg3_readphy(tp, MII_TG3_FET_PTEST, &val);
7143 }
7144
7145 /* Reset to prevent losing 1st rx packet intermittently */
7146 if ((tp->phy_flags & TG3_PHYFLG_MII_SERDES) &&
7147 tg3_flag(tp, 5780_CLASS)) {
7148 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
7149 udelay(10);
7150 tw32_f(MAC_RX_MODE, tp->rx_mode);
7151 }
7152
7153 mac_mode = tp->mac_mode &
7154 ~(MAC_MODE_PORT_MODE_MASK | MAC_MODE_HALF_DUPLEX);
7155 if (speed == SPEED_1000)
7156 mac_mode |= MAC_MODE_PORT_MODE_GMII;
7157 else
7158 mac_mode |= MAC_MODE_PORT_MODE_MII;
7159
7160 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700) {
7161 u32 masked_phy_id = tp->phy_id & TG3_PHY_ID_MASK;
7162
7163 if (masked_phy_id == TG3_PHY_ID_BCM5401)
7164 mac_mode &= ~MAC_MODE_LINK_POLARITY;
7165 else if (masked_phy_id == TG3_PHY_ID_BCM5411)
7166 mac_mode |= MAC_MODE_LINK_POLARITY;
7167
7168 tg3_writephy(tp, MII_TG3_EXT_CTRL,
7169 MII_TG3_EXT_CTRL_LNK3_LED_MODE);
7170 }
7171
7172 tw32(MAC_MODE, mac_mode);
7173 udelay(40);
Matt Carlson941ec902011-08-19 13:58:23 +00007174
7175 return 0;
Matt Carlson5e5a7f32011-08-19 13:58:21 +00007176}
7177
Michał Mirosławc8f44af2011-11-15 15:29:55 +00007178static void tg3_set_loopback(struct net_device *dev, netdev_features_t features)
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00007179{
7180 struct tg3 *tp = netdev_priv(dev);
7181
7182 if (features & NETIF_F_LOOPBACK) {
7183 if (tp->mac_mode & MAC_MODE_PORT_INT_LPBACK)
7184 return;
7185
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00007186 spin_lock_bh(&tp->lock);
Matt Carlson6e01b202011-08-19 13:58:20 +00007187 tg3_mac_loopback(tp, true);
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00007188 netif_carrier_on(tp->dev);
7189 spin_unlock_bh(&tp->lock);
7190 netdev_info(dev, "Internal MAC loopback mode enabled.\n");
7191 } else {
7192 if (!(tp->mac_mode & MAC_MODE_PORT_INT_LPBACK))
7193 return;
7194
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00007195 spin_lock_bh(&tp->lock);
Matt Carlson6e01b202011-08-19 13:58:20 +00007196 tg3_mac_loopback(tp, false);
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00007197 /* Force link status check */
7198 tg3_setup_phy(tp, 1);
7199 spin_unlock_bh(&tp->lock);
7200 netdev_info(dev, "Internal MAC loopback mode disabled.\n");
7201 }
7202}
7203
Michał Mirosławc8f44af2011-11-15 15:29:55 +00007204static netdev_features_t tg3_fix_features(struct net_device *dev,
7205 netdev_features_t features)
Michał Mirosławdc668912011-04-07 03:35:07 +00007206{
7207 struct tg3 *tp = netdev_priv(dev);
7208
Joe Perches63c3a662011-04-26 08:12:10 +00007209 if (dev->mtu > ETH_DATA_LEN && tg3_flag(tp, 5780_CLASS))
Michał Mirosławdc668912011-04-07 03:35:07 +00007210 features &= ~NETIF_F_ALL_TSO;
7211
7212 return features;
7213}
7214
Michał Mirosławc8f44af2011-11-15 15:29:55 +00007215static int tg3_set_features(struct net_device *dev, netdev_features_t features)
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00007216{
Michał Mirosławc8f44af2011-11-15 15:29:55 +00007217 netdev_features_t changed = dev->features ^ features;
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00007218
7219 if ((changed & NETIF_F_LOOPBACK) && netif_running(dev))
7220 tg3_set_loopback(dev, features);
7221
7222 return 0;
7223}
7224
Matt Carlson21f581a2009-08-28 14:00:25 +00007225static void tg3_rx_prodring_free(struct tg3 *tp,
7226 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007227{
Linus Torvalds1da177e2005-04-16 15:20:36 -07007228 int i;
7229
Matt Carlson8fea32b2010-09-15 08:59:58 +00007230 if (tpr != &tp->napi[0].prodring) {
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007231 for (i = tpr->rx_std_cons_idx; i != tpr->rx_std_prod_idx;
Matt Carlson2c49a442010-09-30 10:34:35 +00007232 i = (i + 1) & tp->rx_std_ring_mask)
Eric Dumazet9205fd92011-11-18 06:47:01 +00007233 tg3_rx_data_free(tp, &tpr->rx_std_buffers[i],
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007234 tp->rx_pkt_map_sz);
7235
Joe Perches63c3a662011-04-26 08:12:10 +00007236 if (tg3_flag(tp, JUMBO_CAPABLE)) {
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007237 for (i = tpr->rx_jmb_cons_idx;
7238 i != tpr->rx_jmb_prod_idx;
Matt Carlson2c49a442010-09-30 10:34:35 +00007239 i = (i + 1) & tp->rx_jmb_ring_mask) {
Eric Dumazet9205fd92011-11-18 06:47:01 +00007240 tg3_rx_data_free(tp, &tpr->rx_jmb_buffers[i],
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007241 TG3_RX_JMB_MAP_SZ);
7242 }
7243 }
7244
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007245 return;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007246 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007247
Matt Carlson2c49a442010-09-30 10:34:35 +00007248 for (i = 0; i <= tp->rx_std_ring_mask; i++)
Eric Dumazet9205fd92011-11-18 06:47:01 +00007249 tg3_rx_data_free(tp, &tpr->rx_std_buffers[i],
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007250 tp->rx_pkt_map_sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007251
Joe Perches63c3a662011-04-26 08:12:10 +00007252 if (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS)) {
Matt Carlson2c49a442010-09-30 10:34:35 +00007253 for (i = 0; i <= tp->rx_jmb_ring_mask; i++)
Eric Dumazet9205fd92011-11-18 06:47:01 +00007254 tg3_rx_data_free(tp, &tpr->rx_jmb_buffers[i],
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007255 TG3_RX_JMB_MAP_SZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007256 }
7257}
7258
Matt Carlsonc6cdf432010-04-05 10:19:26 +00007259/* Initialize rx rings for packet processing.
Linus Torvalds1da177e2005-04-16 15:20:36 -07007260 *
7261 * The chip has been shut down and the driver detached from
7262 * the networking, so no interrupts or new tx packets will
7263 * end up in the driver. tp->{tx,}lock are held and thus
7264 * we may not sleep.
7265 */
Matt Carlson21f581a2009-08-28 14:00:25 +00007266static int tg3_rx_prodring_alloc(struct tg3 *tp,
7267 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007268{
Matt Carlson287be122009-08-28 13:58:46 +00007269 u32 i, rx_pkt_dma_sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007270
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007271 tpr->rx_std_cons_idx = 0;
7272 tpr->rx_std_prod_idx = 0;
7273 tpr->rx_jmb_cons_idx = 0;
7274 tpr->rx_jmb_prod_idx = 0;
7275
Matt Carlson8fea32b2010-09-15 08:59:58 +00007276 if (tpr != &tp->napi[0].prodring) {
Matt Carlson2c49a442010-09-30 10:34:35 +00007277 memset(&tpr->rx_std_buffers[0], 0,
7278 TG3_RX_STD_BUFF_RING_SIZE(tp));
Matt Carlson48035722010-10-14 10:37:43 +00007279 if (tpr->rx_jmb_buffers)
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007280 memset(&tpr->rx_jmb_buffers[0], 0,
Matt Carlson2c49a442010-09-30 10:34:35 +00007281 TG3_RX_JMB_BUFF_RING_SIZE(tp));
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007282 goto done;
7283 }
7284
Linus Torvalds1da177e2005-04-16 15:20:36 -07007285 /* Zero out all descriptors. */
Matt Carlson2c49a442010-09-30 10:34:35 +00007286 memset(tpr->rx_std, 0, TG3_RX_STD_RING_BYTES(tp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007287
Matt Carlson287be122009-08-28 13:58:46 +00007288 rx_pkt_dma_sz = TG3_RX_STD_DMA_SZ;
Joe Perches63c3a662011-04-26 08:12:10 +00007289 if (tg3_flag(tp, 5780_CLASS) &&
Matt Carlson287be122009-08-28 13:58:46 +00007290 tp->dev->mtu > ETH_DATA_LEN)
7291 rx_pkt_dma_sz = TG3_RX_JMB_DMA_SZ;
7292 tp->rx_pkt_map_sz = TG3_RX_DMA_TO_MAP_SZ(rx_pkt_dma_sz);
Michael Chan7e72aad2005-07-25 12:31:17 -07007293
Linus Torvalds1da177e2005-04-16 15:20:36 -07007294 /* Initialize invariants of the rings, we only set this
7295 * stuff once. This works because the card does not
7296 * write into the rx buffer posting rings.
7297 */
Matt Carlson2c49a442010-09-30 10:34:35 +00007298 for (i = 0; i <= tp->rx_std_ring_mask; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007299 struct tg3_rx_buffer_desc *rxd;
7300
Matt Carlson21f581a2009-08-28 14:00:25 +00007301 rxd = &tpr->rx_std[i];
Matt Carlson287be122009-08-28 13:58:46 +00007302 rxd->idx_len = rx_pkt_dma_sz << RXD_LEN_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007303 rxd->type_flags = (RXD_FLAG_END << RXD_FLAGS_SHIFT);
7304 rxd->opaque = (RXD_OPAQUE_RING_STD |
7305 (i << RXD_OPAQUE_INDEX_SHIFT));
7306 }
7307
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007308 /* Now allocate fresh SKBs for each rx ring. */
7309 for (i = 0; i < tp->rx_pending; i++) {
Eric Dumazet8d4057a2012-04-27 00:34:49 +00007310 unsigned int frag_size;
7311
7312 if (tg3_alloc_rx_data(tp, tpr, RXD_OPAQUE_RING_STD, i,
7313 &frag_size) < 0) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00007314 netdev_warn(tp->dev,
7315 "Using a smaller RX standard ring. Only "
7316 "%d out of %d buffers were allocated "
7317 "successfully\n", i, tp->rx_pending);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007318 if (i == 0)
7319 goto initfail;
7320 tp->rx_pending = i;
7321 break;
7322 }
7323 }
7324
Joe Perches63c3a662011-04-26 08:12:10 +00007325 if (!tg3_flag(tp, JUMBO_CAPABLE) || tg3_flag(tp, 5780_CLASS))
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007326 goto done;
7327
Matt Carlson2c49a442010-09-30 10:34:35 +00007328 memset(tpr->rx_jmb, 0, TG3_RX_JMB_RING_BYTES(tp));
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007329
Joe Perches63c3a662011-04-26 08:12:10 +00007330 if (!tg3_flag(tp, JUMBO_RING_ENABLE))
Matt Carlson0d86df82010-02-17 15:17:00 +00007331 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007332
Matt Carlson2c49a442010-09-30 10:34:35 +00007333 for (i = 0; i <= tp->rx_jmb_ring_mask; i++) {
Matt Carlson0d86df82010-02-17 15:17:00 +00007334 struct tg3_rx_buffer_desc *rxd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007335
Matt Carlson0d86df82010-02-17 15:17:00 +00007336 rxd = &tpr->rx_jmb[i].std;
7337 rxd->idx_len = TG3_RX_JMB_DMA_SZ << RXD_LEN_SHIFT;
7338 rxd->type_flags = (RXD_FLAG_END << RXD_FLAGS_SHIFT) |
7339 RXD_FLAG_JUMBO;
7340 rxd->opaque = (RXD_OPAQUE_RING_JUMBO |
7341 (i << RXD_OPAQUE_INDEX_SHIFT));
7342 }
7343
7344 for (i = 0; i < tp->rx_jumbo_pending; i++) {
Eric Dumazet8d4057a2012-04-27 00:34:49 +00007345 unsigned int frag_size;
7346
7347 if (tg3_alloc_rx_data(tp, tpr, RXD_OPAQUE_RING_JUMBO, i,
7348 &frag_size) < 0) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00007349 netdev_warn(tp->dev,
7350 "Using a smaller RX jumbo ring. Only %d "
7351 "out of %d buffers were allocated "
7352 "successfully\n", i, tp->rx_jumbo_pending);
Matt Carlson0d86df82010-02-17 15:17:00 +00007353 if (i == 0)
7354 goto initfail;
7355 tp->rx_jumbo_pending = i;
7356 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007357 }
7358 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007359
7360done:
Michael Chan32d8c572006-07-25 16:38:29 -07007361 return 0;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007362
7363initfail:
Matt Carlson21f581a2009-08-28 14:00:25 +00007364 tg3_rx_prodring_free(tp, tpr);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007365 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007366}
7367
Matt Carlson21f581a2009-08-28 14:00:25 +00007368static void tg3_rx_prodring_fini(struct tg3 *tp,
7369 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007370{
Matt Carlson21f581a2009-08-28 14:00:25 +00007371 kfree(tpr->rx_std_buffers);
7372 tpr->rx_std_buffers = NULL;
7373 kfree(tpr->rx_jmb_buffers);
7374 tpr->rx_jmb_buffers = NULL;
7375 if (tpr->rx_std) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00007376 dma_free_coherent(&tp->pdev->dev, TG3_RX_STD_RING_BYTES(tp),
7377 tpr->rx_std, tpr->rx_std_mapping);
Matt Carlson21f581a2009-08-28 14:00:25 +00007378 tpr->rx_std = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007379 }
Matt Carlson21f581a2009-08-28 14:00:25 +00007380 if (tpr->rx_jmb) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00007381 dma_free_coherent(&tp->pdev->dev, TG3_RX_JMB_RING_BYTES(tp),
7382 tpr->rx_jmb, tpr->rx_jmb_mapping);
Matt Carlson21f581a2009-08-28 14:00:25 +00007383 tpr->rx_jmb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007384 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007385}
7386
Matt Carlson21f581a2009-08-28 14:00:25 +00007387static int tg3_rx_prodring_init(struct tg3 *tp,
7388 struct tg3_rx_prodring_set *tpr)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007389{
Matt Carlson2c49a442010-09-30 10:34:35 +00007390 tpr->rx_std_buffers = kzalloc(TG3_RX_STD_BUFF_RING_SIZE(tp),
7391 GFP_KERNEL);
Matt Carlson21f581a2009-08-28 14:00:25 +00007392 if (!tpr->rx_std_buffers)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007393 return -ENOMEM;
7394
Matt Carlson4bae65c2010-11-24 08:31:52 +00007395 tpr->rx_std = dma_alloc_coherent(&tp->pdev->dev,
7396 TG3_RX_STD_RING_BYTES(tp),
7397 &tpr->rx_std_mapping,
7398 GFP_KERNEL);
Matt Carlson21f581a2009-08-28 14:00:25 +00007399 if (!tpr->rx_std)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007400 goto err_out;
7401
Joe Perches63c3a662011-04-26 08:12:10 +00007402 if (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS)) {
Matt Carlson2c49a442010-09-30 10:34:35 +00007403 tpr->rx_jmb_buffers = kzalloc(TG3_RX_JMB_BUFF_RING_SIZE(tp),
Matt Carlson21f581a2009-08-28 14:00:25 +00007404 GFP_KERNEL);
7405 if (!tpr->rx_jmb_buffers)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007406 goto err_out;
7407
Matt Carlson4bae65c2010-11-24 08:31:52 +00007408 tpr->rx_jmb = dma_alloc_coherent(&tp->pdev->dev,
7409 TG3_RX_JMB_RING_BYTES(tp),
7410 &tpr->rx_jmb_mapping,
7411 GFP_KERNEL);
Matt Carlson21f581a2009-08-28 14:00:25 +00007412 if (!tpr->rx_jmb)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007413 goto err_out;
7414 }
7415
7416 return 0;
7417
7418err_out:
Matt Carlson21f581a2009-08-28 14:00:25 +00007419 tg3_rx_prodring_fini(tp, tpr);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007420 return -ENOMEM;
7421}
7422
7423/* Free up pending packets in all rx/tx rings.
7424 *
7425 * The chip has been shut down and the driver detached from
7426 * the networking, so no interrupts or new tx packets will
7427 * end up in the driver. tp->{tx,}lock is not held and we are not
7428 * in an interrupt context and thus may sleep.
7429 */
7430static void tg3_free_rings(struct tg3 *tp)
7431{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007432 int i, j;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007433
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007434 for (j = 0; j < tp->irq_cnt; j++) {
7435 struct tg3_napi *tnapi = &tp->napi[j];
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007436
Matt Carlson8fea32b2010-09-15 08:59:58 +00007437 tg3_rx_prodring_free(tp, &tnapi->prodring);
Matt Carlsonb28f6422010-06-05 17:24:32 +00007438
Matt Carlson0c1d0e22009-09-01 13:16:33 +00007439 if (!tnapi->tx_buffers)
7440 continue;
7441
Matt Carlson0d681b22011-07-27 14:20:49 +00007442 for (i = 0; i < TG3_TX_RING_SIZE; i++) {
7443 struct sk_buff *skb = tnapi->tx_buffers[i].skb;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007444
Matt Carlson0d681b22011-07-27 14:20:49 +00007445 if (!skb)
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007446 continue;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007447
Matt Carlsonba1142e2011-11-04 09:15:00 +00007448 tg3_tx_skb_unmap(tnapi, i,
7449 skb_shinfo(skb)->nr_frags - 1);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007450
7451 dev_kfree_skb_any(skb);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007452 }
Tom Herbert5cb917b2012-03-05 19:53:50 +00007453 netdev_tx_reset_queue(netdev_get_tx_queue(tp->dev, j));
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007454 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007455}
7456
7457/* Initialize tx/rx rings for packet processing.
7458 *
7459 * The chip has been shut down and the driver detached from
7460 * the networking, so no interrupts or new tx packets will
7461 * end up in the driver. tp->{tx,}lock are held and thus
7462 * we may not sleep.
7463 */
7464static int tg3_init_rings(struct tg3 *tp)
7465{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007466 int i;
Matt Carlson72334482009-08-28 14:03:01 +00007467
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007468 /* Free up all the SKBs. */
7469 tg3_free_rings(tp);
7470
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007471 for (i = 0; i < tp->irq_cnt; i++) {
7472 struct tg3_napi *tnapi = &tp->napi[i];
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007473
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007474 tnapi->last_tag = 0;
7475 tnapi->last_irq_tag = 0;
7476 tnapi->hw_status->status = 0;
7477 tnapi->hw_status->status_tag = 0;
7478 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
7479
7480 tnapi->tx_prod = 0;
7481 tnapi->tx_cons = 0;
Matt Carlson0c1d0e22009-09-01 13:16:33 +00007482 if (tnapi->tx_ring)
7483 memset(tnapi->tx_ring, 0, TG3_TX_RING_BYTES);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007484
7485 tnapi->rx_rcb_ptr = 0;
Matt Carlson0c1d0e22009-09-01 13:16:33 +00007486 if (tnapi->rx_rcb)
7487 memset(tnapi->rx_rcb, 0, TG3_RX_RCB_RING_BYTES(tp));
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007488
Matt Carlson8fea32b2010-09-15 08:59:58 +00007489 if (tg3_rx_prodring_alloc(tp, &tnapi->prodring)) {
Matt Carlsone4af1af2010-02-12 14:47:05 +00007490 tg3_free_rings(tp);
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007491 return -ENOMEM;
Matt Carlsone4af1af2010-02-12 14:47:05 +00007492 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007493 }
Matt Carlson72334482009-08-28 14:03:01 +00007494
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007495 return 0;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007496}
7497
7498/*
7499 * Must not be invoked with interrupt sources disabled and
7500 * the hardware shutdown down.
7501 */
7502static void tg3_free_consistent(struct tg3 *tp)
7503{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007504 int i;
Matt Carlson898a56f2009-08-28 14:02:40 +00007505
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007506 for (i = 0; i < tp->irq_cnt; i++) {
7507 struct tg3_napi *tnapi = &tp->napi[i];
7508
7509 if (tnapi->tx_ring) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00007510 dma_free_coherent(&tp->pdev->dev, TG3_TX_RING_BYTES,
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007511 tnapi->tx_ring, tnapi->tx_desc_mapping);
7512 tnapi->tx_ring = NULL;
7513 }
7514
7515 kfree(tnapi->tx_buffers);
7516 tnapi->tx_buffers = NULL;
7517
7518 if (tnapi->rx_rcb) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00007519 dma_free_coherent(&tp->pdev->dev,
7520 TG3_RX_RCB_RING_BYTES(tp),
7521 tnapi->rx_rcb,
7522 tnapi->rx_rcb_mapping);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007523 tnapi->rx_rcb = NULL;
7524 }
7525
Matt Carlson8fea32b2010-09-15 08:59:58 +00007526 tg3_rx_prodring_fini(tp, &tnapi->prodring);
7527
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007528 if (tnapi->hw_status) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00007529 dma_free_coherent(&tp->pdev->dev, TG3_HW_STATUS_SIZE,
7530 tnapi->hw_status,
7531 tnapi->status_mapping);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007532 tnapi->hw_status = NULL;
7533 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007534 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007535
Linus Torvalds1da177e2005-04-16 15:20:36 -07007536 if (tp->hw_stats) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00007537 dma_free_coherent(&tp->pdev->dev, sizeof(struct tg3_hw_stats),
7538 tp->hw_stats, tp->stats_mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007539 tp->hw_stats = NULL;
7540 }
7541}
7542
7543/*
7544 * Must not be invoked with interrupt sources disabled and
7545 * the hardware shutdown down. Can sleep.
7546 */
7547static int tg3_alloc_consistent(struct tg3 *tp)
7548{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007549 int i;
Matt Carlson898a56f2009-08-28 14:02:40 +00007550
Matt Carlson4bae65c2010-11-24 08:31:52 +00007551 tp->hw_stats = dma_alloc_coherent(&tp->pdev->dev,
7552 sizeof(struct tg3_hw_stats),
7553 &tp->stats_mapping,
7554 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007555 if (!tp->hw_stats)
7556 goto err_out;
7557
Linus Torvalds1da177e2005-04-16 15:20:36 -07007558 memset(tp->hw_stats, 0, sizeof(struct tg3_hw_stats));
7559
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007560 for (i = 0; i < tp->irq_cnt; i++) {
7561 struct tg3_napi *tnapi = &tp->napi[i];
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00007562 struct tg3_hw_status *sblk;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007563
Matt Carlson4bae65c2010-11-24 08:31:52 +00007564 tnapi->hw_status = dma_alloc_coherent(&tp->pdev->dev,
7565 TG3_HW_STATUS_SIZE,
7566 &tnapi->status_mapping,
7567 GFP_KERNEL);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007568 if (!tnapi->hw_status)
7569 goto err_out;
7570
7571 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00007572 sblk = tnapi->hw_status;
7573
Matt Carlson8fea32b2010-09-15 08:59:58 +00007574 if (tg3_rx_prodring_init(tp, &tnapi->prodring))
7575 goto err_out;
7576
Matt Carlson19cfaec2009-12-03 08:36:20 +00007577 /* If multivector TSS is enabled, vector 0 does not handle
7578 * tx interrupts. Don't allocate any resources for it.
7579 */
Joe Perches63c3a662011-04-26 08:12:10 +00007580 if ((!i && !tg3_flag(tp, ENABLE_TSS)) ||
7581 (i && tg3_flag(tp, ENABLE_TSS))) {
Matt Carlsondf8944c2011-07-27 14:20:46 +00007582 tnapi->tx_buffers = kzalloc(
7583 sizeof(struct tg3_tx_ring_info) *
7584 TG3_TX_RING_SIZE, GFP_KERNEL);
Matt Carlson19cfaec2009-12-03 08:36:20 +00007585 if (!tnapi->tx_buffers)
7586 goto err_out;
7587
Matt Carlson4bae65c2010-11-24 08:31:52 +00007588 tnapi->tx_ring = dma_alloc_coherent(&tp->pdev->dev,
7589 TG3_TX_RING_BYTES,
7590 &tnapi->tx_desc_mapping,
7591 GFP_KERNEL);
Matt Carlson19cfaec2009-12-03 08:36:20 +00007592 if (!tnapi->tx_ring)
7593 goto err_out;
7594 }
7595
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00007596 /*
7597 * When RSS is enabled, the status block format changes
7598 * slightly. The "rx_jumbo_consumer", "reserved",
7599 * and "rx_mini_consumer" members get mapped to the
7600 * other three rx return ring producer indexes.
7601 */
7602 switch (i) {
7603 default:
Matt Carlsonf891ea12012-04-24 13:37:01 +00007604 if (tg3_flag(tp, ENABLE_RSS)) {
7605 tnapi->rx_rcb_prod_idx = NULL;
7606 break;
7607 }
7608 /* Fall through */
7609 case 1:
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00007610 tnapi->rx_rcb_prod_idx = &sblk->idx[0].rx_producer;
7611 break;
7612 case 2:
7613 tnapi->rx_rcb_prod_idx = &sblk->rx_jumbo_consumer;
7614 break;
7615 case 3:
7616 tnapi->rx_rcb_prod_idx = &sblk->reserved;
7617 break;
7618 case 4:
7619 tnapi->rx_rcb_prod_idx = &sblk->rx_mini_consumer;
7620 break;
7621 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007622
Matt Carlson0c1d0e22009-09-01 13:16:33 +00007623 /*
7624 * If multivector RSS is enabled, vector 0 does not handle
7625 * rx or tx interrupts. Don't allocate any resources for it.
7626 */
Joe Perches63c3a662011-04-26 08:12:10 +00007627 if (!i && tg3_flag(tp, ENABLE_RSS))
Matt Carlson0c1d0e22009-09-01 13:16:33 +00007628 continue;
7629
Matt Carlson4bae65c2010-11-24 08:31:52 +00007630 tnapi->rx_rcb = dma_alloc_coherent(&tp->pdev->dev,
7631 TG3_RX_RCB_RING_BYTES(tp),
7632 &tnapi->rx_rcb_mapping,
7633 GFP_KERNEL);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007634 if (!tnapi->rx_rcb)
7635 goto err_out;
7636
7637 memset(tnapi->rx_rcb, 0, TG3_RX_RCB_RING_BYTES(tp));
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007638 }
7639
Linus Torvalds1da177e2005-04-16 15:20:36 -07007640 return 0;
7641
7642err_out:
7643 tg3_free_consistent(tp);
7644 return -ENOMEM;
7645}
7646
7647#define MAX_WAIT_CNT 1000
7648
7649/* To stop a block, clear the enable bit and poll till it
7650 * clears. tp->lock is held.
7651 */
David S. Millerb3b7d6b2005-05-05 14:40:20 -07007652static int tg3_stop_block(struct tg3 *tp, unsigned long ofs, u32 enable_bit, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007653{
7654 unsigned int i;
7655 u32 val;
7656
Joe Perches63c3a662011-04-26 08:12:10 +00007657 if (tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007658 switch (ofs) {
7659 case RCVLSC_MODE:
7660 case DMAC_MODE:
7661 case MBFREE_MODE:
7662 case BUFMGR_MODE:
7663 case MEMARB_MODE:
7664 /* We can't enable/disable these bits of the
7665 * 5705/5750, just say success.
7666 */
7667 return 0;
7668
7669 default:
7670 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07007671 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007672 }
7673
7674 val = tr32(ofs);
7675 val &= ~enable_bit;
7676 tw32_f(ofs, val);
7677
7678 for (i = 0; i < MAX_WAIT_CNT; i++) {
7679 udelay(100);
7680 val = tr32(ofs);
7681 if ((val & enable_bit) == 0)
7682 break;
7683 }
7684
David S. Millerb3b7d6b2005-05-05 14:40:20 -07007685 if (i == MAX_WAIT_CNT && !silent) {
Matt Carlson2445e462010-04-05 10:19:21 +00007686 dev_err(&tp->pdev->dev,
7687 "tg3_stop_block timed out, ofs=%lx enable_bit=%x\n",
7688 ofs, enable_bit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007689 return -ENODEV;
7690 }
7691
7692 return 0;
7693}
7694
7695/* tp->lock is held. */
David S. Millerb3b7d6b2005-05-05 14:40:20 -07007696static int tg3_abort_hw(struct tg3 *tp, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007697{
7698 int i, err;
7699
7700 tg3_disable_ints(tp);
7701
7702 tp->rx_mode &= ~RX_MODE_ENABLE;
7703 tw32_f(MAC_RX_MODE, tp->rx_mode);
7704 udelay(10);
7705
David S. Millerb3b7d6b2005-05-05 14:40:20 -07007706 err = tg3_stop_block(tp, RCVBDI_MODE, RCVBDI_MODE_ENABLE, silent);
7707 err |= tg3_stop_block(tp, RCVLPC_MODE, RCVLPC_MODE_ENABLE, silent);
7708 err |= tg3_stop_block(tp, RCVLSC_MODE, RCVLSC_MODE_ENABLE, silent);
7709 err |= tg3_stop_block(tp, RCVDBDI_MODE, RCVDBDI_MODE_ENABLE, silent);
7710 err |= tg3_stop_block(tp, RCVDCC_MODE, RCVDCC_MODE_ENABLE, silent);
7711 err |= tg3_stop_block(tp, RCVCC_MODE, RCVCC_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007712
David S. Millerb3b7d6b2005-05-05 14:40:20 -07007713 err |= tg3_stop_block(tp, SNDBDS_MODE, SNDBDS_MODE_ENABLE, silent);
7714 err |= tg3_stop_block(tp, SNDBDI_MODE, SNDBDI_MODE_ENABLE, silent);
7715 err |= tg3_stop_block(tp, SNDDATAI_MODE, SNDDATAI_MODE_ENABLE, silent);
7716 err |= tg3_stop_block(tp, RDMAC_MODE, RDMAC_MODE_ENABLE, silent);
7717 err |= tg3_stop_block(tp, SNDDATAC_MODE, SNDDATAC_MODE_ENABLE, silent);
7718 err |= tg3_stop_block(tp, DMAC_MODE, DMAC_MODE_ENABLE, silent);
7719 err |= tg3_stop_block(tp, SNDBDC_MODE, SNDBDC_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007720
7721 tp->mac_mode &= ~MAC_MODE_TDE_ENABLE;
7722 tw32_f(MAC_MODE, tp->mac_mode);
7723 udelay(40);
7724
7725 tp->tx_mode &= ~TX_MODE_ENABLE;
7726 tw32_f(MAC_TX_MODE, tp->tx_mode);
7727
7728 for (i = 0; i < MAX_WAIT_CNT; i++) {
7729 udelay(100);
7730 if (!(tr32(MAC_TX_MODE) & TX_MODE_ENABLE))
7731 break;
7732 }
7733 if (i >= MAX_WAIT_CNT) {
Matt Carlsonab96b242010-04-05 10:19:22 +00007734 dev_err(&tp->pdev->dev,
7735 "%s timed out, TX_MODE_ENABLE will not clear "
7736 "MAC_TX_MODE=%08x\n", __func__, tr32(MAC_TX_MODE));
Michael Chane6de8ad2005-05-05 14:42:41 -07007737 err |= -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007738 }
7739
Michael Chane6de8ad2005-05-05 14:42:41 -07007740 err |= tg3_stop_block(tp, HOSTCC_MODE, HOSTCC_MODE_ENABLE, silent);
David S. Millerb3b7d6b2005-05-05 14:40:20 -07007741 err |= tg3_stop_block(tp, WDMAC_MODE, WDMAC_MODE_ENABLE, silent);
7742 err |= tg3_stop_block(tp, MBFREE_MODE, MBFREE_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007743
7744 tw32(FTQ_RESET, 0xffffffff);
7745 tw32(FTQ_RESET, 0x00000000);
7746
David S. Millerb3b7d6b2005-05-05 14:40:20 -07007747 err |= tg3_stop_block(tp, BUFMGR_MODE, BUFMGR_MODE_ENABLE, silent);
7748 err |= tg3_stop_block(tp, MEMARB_MODE, MEMARB_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007749
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007750 for (i = 0; i < tp->irq_cnt; i++) {
7751 struct tg3_napi *tnapi = &tp->napi[i];
7752 if (tnapi->hw_status)
7753 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
7754 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007755
Linus Torvalds1da177e2005-04-16 15:20:36 -07007756 return err;
7757}
7758
Michael Chanee6a99b2007-07-18 21:49:10 -07007759/* Save PCI command register before chip reset */
7760static void tg3_save_pci_state(struct tg3 *tp)
7761{
Matt Carlson8a6eac92007-10-21 16:17:55 -07007762 pci_read_config_word(tp->pdev, PCI_COMMAND, &tp->pci_cmd);
Michael Chanee6a99b2007-07-18 21:49:10 -07007763}
7764
7765/* Restore PCI state after chip reset */
7766static void tg3_restore_pci_state(struct tg3 *tp)
7767{
7768 u32 val;
7769
7770 /* Re-enable indirect register accesses. */
7771 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
7772 tp->misc_host_ctrl);
7773
7774 /* Set MAX PCI retry to zero. */
7775 val = (PCISTATE_ROM_ENABLE | PCISTATE_ROM_RETRY_ENABLE);
7776 if (tp->pci_chip_rev_id == CHIPREV_ID_5704_A0 &&
Joe Perches63c3a662011-04-26 08:12:10 +00007777 tg3_flag(tp, PCIX_MODE))
Michael Chanee6a99b2007-07-18 21:49:10 -07007778 val |= PCISTATE_RETRY_SAME_DMA;
Matt Carlson0d3031d2007-10-10 18:02:43 -07007779 /* Allow reads and writes to the APE register and memory space. */
Joe Perches63c3a662011-04-26 08:12:10 +00007780 if (tg3_flag(tp, ENABLE_APE))
Matt Carlson0d3031d2007-10-10 18:02:43 -07007781 val |= PCISTATE_ALLOW_APE_CTLSPC_WR |
Matt Carlsonf92d9dc12010-06-05 17:24:30 +00007782 PCISTATE_ALLOW_APE_SHMEM_WR |
7783 PCISTATE_ALLOW_APE_PSPACE_WR;
Michael Chanee6a99b2007-07-18 21:49:10 -07007784 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE, val);
7785
Matt Carlson8a6eac92007-10-21 16:17:55 -07007786 pci_write_config_word(tp->pdev, PCI_COMMAND, tp->pci_cmd);
Michael Chanee6a99b2007-07-18 21:49:10 -07007787
Matt Carlson2c55a3d2011-11-28 09:41:04 +00007788 if (!tg3_flag(tp, PCI_EXPRESS)) {
7789 pci_write_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE,
7790 tp->pci_cacheline_sz);
7791 pci_write_config_byte(tp->pdev, PCI_LATENCY_TIMER,
7792 tp->pci_lat_timer);
Michael Chan114342f2007-10-15 02:12:26 -07007793 }
Matt Carlson5f5c51e2007-11-12 21:19:37 -08007794
Michael Chanee6a99b2007-07-18 21:49:10 -07007795 /* Make sure PCI-X relaxed ordering bit is clear. */
Joe Perches63c3a662011-04-26 08:12:10 +00007796 if (tg3_flag(tp, PCIX_MODE)) {
Matt Carlson9974a352007-10-07 23:27:28 -07007797 u16 pcix_cmd;
7798
7799 pci_read_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
7800 &pcix_cmd);
7801 pcix_cmd &= ~PCI_X_CMD_ERO;
7802 pci_write_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
7803 pcix_cmd);
7804 }
Michael Chanee6a99b2007-07-18 21:49:10 -07007805
Joe Perches63c3a662011-04-26 08:12:10 +00007806 if (tg3_flag(tp, 5780_CLASS)) {
Michael Chanee6a99b2007-07-18 21:49:10 -07007807
7808 /* Chip reset on 5780 will reset MSI enable bit,
7809 * so need to restore it.
7810 */
Joe Perches63c3a662011-04-26 08:12:10 +00007811 if (tg3_flag(tp, USING_MSI)) {
Michael Chanee6a99b2007-07-18 21:49:10 -07007812 u16 ctrl;
7813
7814 pci_read_config_word(tp->pdev,
7815 tp->msi_cap + PCI_MSI_FLAGS,
7816 &ctrl);
7817 pci_write_config_word(tp->pdev,
7818 tp->msi_cap + PCI_MSI_FLAGS,
7819 ctrl | PCI_MSI_FLAGS_ENABLE);
7820 val = tr32(MSGINT_MODE);
7821 tw32(MSGINT_MODE, val | MSGINT_MODE_ENABLE);
7822 }
7823 }
7824}
7825
Linus Torvalds1da177e2005-04-16 15:20:36 -07007826/* tp->lock is held. */
7827static int tg3_chip_reset(struct tg3 *tp)
7828{
7829 u32 val;
Michael Chan1ee582d2005-08-09 20:16:46 -07007830 void (*write_op)(struct tg3 *, u32, u32);
Matt Carlson4f125f42009-09-01 12:55:02 +00007831 int i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007832
David S. Millerf49639e2006-06-09 11:58:36 -07007833 tg3_nvram_lock(tp);
7834
Matt Carlson77b483f2008-08-15 14:07:24 -07007835 tg3_ape_lock(tp, TG3_APE_LOCK_GRC);
7836
David S. Millerf49639e2006-06-09 11:58:36 -07007837 /* No matching tg3_nvram_unlock() after this because
7838 * chip reset below will undo the nvram lock.
7839 */
7840 tp->nvram_lock_cnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007841
Michael Chanee6a99b2007-07-18 21:49:10 -07007842 /* GRC_MISC_CFG core clock reset will clear the memory
7843 * enable bit in PCI register 4 and the MSI enable bit
7844 * on some chips, so we save relevant registers here.
7845 */
7846 tg3_save_pci_state(tp);
7847
Michael Chand9ab5ad12006-03-20 22:27:35 -08007848 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752 ||
Joe Perches63c3a662011-04-26 08:12:10 +00007849 tg3_flag(tp, 5755_PLUS))
Michael Chand9ab5ad12006-03-20 22:27:35 -08007850 tw32(GRC_FASTBOOT_PC, 0);
7851
Linus Torvalds1da177e2005-04-16 15:20:36 -07007852 /*
7853 * We must avoid the readl() that normally takes place.
7854 * It locks machines, causes machine checks, and other
7855 * fun things. So, temporarily disable the 5701
7856 * hardware workaround, while we do the reset.
7857 */
Michael Chan1ee582d2005-08-09 20:16:46 -07007858 write_op = tp->write32;
7859 if (write_op == tg3_write_flush_reg32)
7860 tp->write32 = tg3_write32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007861
Michael Chand18edcb2007-03-24 20:57:11 -07007862 /* Prevent the irq handler from reading or writing PCI registers
7863 * during chip reset when the memory enable bit in the PCI command
7864 * register may be cleared. The chip does not generate interrupt
7865 * at this time, but the irq handler may still be called due to irq
7866 * sharing or irqpoll.
7867 */
Joe Perches63c3a662011-04-26 08:12:10 +00007868 tg3_flag_set(tp, CHIP_RESETTING);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007869 for (i = 0; i < tp->irq_cnt; i++) {
7870 struct tg3_napi *tnapi = &tp->napi[i];
7871 if (tnapi->hw_status) {
7872 tnapi->hw_status->status = 0;
7873 tnapi->hw_status->status_tag = 0;
7874 }
7875 tnapi->last_tag = 0;
7876 tnapi->last_irq_tag = 0;
Michael Chanb8fa2f32007-04-06 17:35:37 -07007877 }
Michael Chand18edcb2007-03-24 20:57:11 -07007878 smp_mb();
Matt Carlson4f125f42009-09-01 12:55:02 +00007879
7880 for (i = 0; i < tp->irq_cnt; i++)
7881 synchronize_irq(tp->napi[i].irq_vec);
Michael Chand18edcb2007-03-24 20:57:11 -07007882
Matt Carlson255ca312009-08-25 10:07:27 +00007883 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780) {
7884 val = tr32(TG3_PCIE_LNKCTL) & ~TG3_PCIE_LNKCTL_L1_PLL_PD_EN;
7885 tw32(TG3_PCIE_LNKCTL, val | TG3_PCIE_LNKCTL_L1_PLL_PD_DIS);
7886 }
7887
Linus Torvalds1da177e2005-04-16 15:20:36 -07007888 /* do the reset */
7889 val = GRC_MISC_CFG_CORECLK_RESET;
7890
Joe Perches63c3a662011-04-26 08:12:10 +00007891 if (tg3_flag(tp, PCI_EXPRESS)) {
Matt Carlson88075d92010-08-02 11:25:58 +00007892 /* Force PCIe 1.0a mode */
7893 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
Joe Perches63c3a662011-04-26 08:12:10 +00007894 !tg3_flag(tp, 57765_PLUS) &&
Matt Carlson88075d92010-08-02 11:25:58 +00007895 tr32(TG3_PCIE_PHY_TSTCTL) ==
7896 (TG3_PCIE_PHY_TSTCTL_PCIE10 | TG3_PCIE_PHY_TSTCTL_PSCRAM))
7897 tw32(TG3_PCIE_PHY_TSTCTL, TG3_PCIE_PHY_TSTCTL_PSCRAM);
7898
Linus Torvalds1da177e2005-04-16 15:20:36 -07007899 if (tp->pci_chip_rev_id != CHIPREV_ID_5750_A0) {
7900 tw32(GRC_MISC_CFG, (1 << 29));
7901 val |= (1 << 29);
7902 }
7903 }
7904
Michael Chanb5d37722006-09-27 16:06:21 -07007905 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
7906 tw32(VCPU_STATUS, tr32(VCPU_STATUS) | VCPU_STATUS_DRV_RESET);
7907 tw32(GRC_VCPU_EXT_CTRL,
7908 tr32(GRC_VCPU_EXT_CTRL) & ~GRC_VCPU_EXT_CTRL_HALT_CPU);
7909 }
7910
Matt Carlsonf37500d2010-08-02 11:25:59 +00007911 /* Manage gphy power for all CPMU absent PCIe devices. */
Joe Perches63c3a662011-04-26 08:12:10 +00007912 if (tg3_flag(tp, 5705_PLUS) && !tg3_flag(tp, CPMU_PRESENT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007913 val |= GRC_MISC_CFG_KEEP_GPHY_POWER;
Matt Carlsonf37500d2010-08-02 11:25:59 +00007914
Linus Torvalds1da177e2005-04-16 15:20:36 -07007915 tw32(GRC_MISC_CFG, val);
7916
Michael Chan1ee582d2005-08-09 20:16:46 -07007917 /* restore 5701 hardware bug workaround write method */
7918 tp->write32 = write_op;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007919
7920 /* Unfortunately, we have to delay before the PCI read back.
7921 * Some 575X chips even will not respond to a PCI cfg access
7922 * when the reset command is given to the chip.
7923 *
7924 * How do these hardware designers expect things to work
7925 * properly if the PCI write is posted for a long period
7926 * of time? It is always necessary to have some method by
7927 * which a register read back can occur to push the write
7928 * out which does the reset.
7929 *
7930 * For most tg3 variants the trick below was working.
7931 * Ho hum...
7932 */
7933 udelay(120);
7934
7935 /* Flush PCI posted writes. The normal MMIO registers
7936 * are inaccessible at this time so this is the only
7937 * way to make this reliably (actually, this is no longer
7938 * the case, see above). I tried to use indirect
7939 * register read/write but this upset some 5701 variants.
7940 */
7941 pci_read_config_dword(tp->pdev, PCI_COMMAND, &val);
7942
7943 udelay(120);
7944
Jon Mason708ebb3a2011-06-27 12:56:50 +00007945 if (tg3_flag(tp, PCI_EXPRESS) && pci_pcie_cap(tp->pdev)) {
Matt Carlsone7126992009-08-25 10:08:16 +00007946 u16 val16;
7947
Linus Torvalds1da177e2005-04-16 15:20:36 -07007948 if (tp->pci_chip_rev_id == CHIPREV_ID_5750_A0) {
7949 int i;
7950 u32 cfg_val;
7951
7952 /* Wait for link training to complete. */
7953 for (i = 0; i < 5000; i++)
7954 udelay(100);
7955
7956 pci_read_config_dword(tp->pdev, 0xc4, &cfg_val);
7957 pci_write_config_dword(tp->pdev, 0xc4,
7958 cfg_val | (1 << 15));
7959 }
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007960
Matt Carlsone7126992009-08-25 10:08:16 +00007961 /* Clear the "no snoop" and "relaxed ordering" bits. */
7962 pci_read_config_word(tp->pdev,
Jon Mason708ebb3a2011-06-27 12:56:50 +00007963 pci_pcie_cap(tp->pdev) + PCI_EXP_DEVCTL,
Matt Carlsone7126992009-08-25 10:08:16 +00007964 &val16);
7965 val16 &= ~(PCI_EXP_DEVCTL_RELAX_EN |
7966 PCI_EXP_DEVCTL_NOSNOOP_EN);
7967 /*
7968 * Older PCIe devices only support the 128 byte
7969 * MPS setting. Enforce the restriction.
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007970 */
Joe Perches63c3a662011-04-26 08:12:10 +00007971 if (!tg3_flag(tp, CPMU_PRESENT))
Matt Carlsone7126992009-08-25 10:08:16 +00007972 val16 &= ~PCI_EXP_DEVCTL_PAYLOAD;
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007973 pci_write_config_word(tp->pdev,
Jon Mason708ebb3a2011-06-27 12:56:50 +00007974 pci_pcie_cap(tp->pdev) + PCI_EXP_DEVCTL,
Matt Carlsone7126992009-08-25 10:08:16 +00007975 val16);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007976
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007977 /* Clear error status */
7978 pci_write_config_word(tp->pdev,
Jon Mason708ebb3a2011-06-27 12:56:50 +00007979 pci_pcie_cap(tp->pdev) + PCI_EXP_DEVSTA,
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007980 PCI_EXP_DEVSTA_CED |
7981 PCI_EXP_DEVSTA_NFED |
7982 PCI_EXP_DEVSTA_FED |
7983 PCI_EXP_DEVSTA_URD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007984 }
7985
Michael Chanee6a99b2007-07-18 21:49:10 -07007986 tg3_restore_pci_state(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007987
Joe Perches63c3a662011-04-26 08:12:10 +00007988 tg3_flag_clear(tp, CHIP_RESETTING);
7989 tg3_flag_clear(tp, ERROR_PROCESSED);
Michael Chand18edcb2007-03-24 20:57:11 -07007990
Michael Chanee6a99b2007-07-18 21:49:10 -07007991 val = 0;
Joe Perches63c3a662011-04-26 08:12:10 +00007992 if (tg3_flag(tp, 5780_CLASS))
Michael Chan4cf78e42005-07-25 12:29:19 -07007993 val = tr32(MEMARB_MODE);
Michael Chanee6a99b2007-07-18 21:49:10 -07007994 tw32(MEMARB_MODE, val | MEMARB_MODE_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007995
7996 if (tp->pci_chip_rev_id == CHIPREV_ID_5750_A3) {
7997 tg3_stop_fw(tp);
7998 tw32(0x5000, 0x400);
7999 }
8000
8001 tw32(GRC_MODE, tp->grc_mode);
8002
8003 if (tp->pci_chip_rev_id == CHIPREV_ID_5705_A0) {
Andy Gospodarekab0049b2007-09-06 20:42:14 +01008004 val = tr32(0xc4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008005
8006 tw32(0xc4, val | (1 << 15));
8007 }
8008
8009 if ((tp->nic_sram_data_cfg & NIC_SRAM_DATA_CFG_MINI_PCI) != 0 &&
8010 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
8011 tp->pci_clock_ctrl |= CLOCK_CTRL_CLKRUN_OENABLE;
8012 if (tp->pci_chip_rev_id == CHIPREV_ID_5705_A0)
8013 tp->pci_clock_ctrl |= CLOCK_CTRL_FORCE_CLKRUN;
8014 tw32(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl);
8015 }
8016
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008017 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Matt Carlson9e975cc2011-07-20 10:20:50 +00008018 tp->mac_mode = MAC_MODE_PORT_MODE_TBI;
Matt Carlsond2394e6b2010-11-24 08:31:47 +00008019 val = tp->mac_mode;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008020 } else if (tp->phy_flags & TG3_PHYFLG_MII_SERDES) {
Matt Carlson9e975cc2011-07-20 10:20:50 +00008021 tp->mac_mode = MAC_MODE_PORT_MODE_GMII;
Matt Carlsond2394e6b2010-11-24 08:31:47 +00008022 val = tp->mac_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008023 } else
Matt Carlsond2394e6b2010-11-24 08:31:47 +00008024 val = 0;
8025
8026 tw32_f(MAC_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008027 udelay(40);
8028
Matt Carlson77b483f2008-08-15 14:07:24 -07008029 tg3_ape_unlock(tp, TG3_APE_LOCK_GRC);
8030
Michael Chan7a6f4362006-09-27 16:03:31 -07008031 err = tg3_poll_fw(tp);
8032 if (err)
8033 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008034
Matt Carlson0a9140c2009-08-28 12:27:50 +00008035 tg3_mdio_start(tp);
8036
Joe Perches63c3a662011-04-26 08:12:10 +00008037 if (tg3_flag(tp, PCI_EXPRESS) &&
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00008038 tp->pci_chip_rev_id != CHIPREV_ID_5750_A0 &&
8039 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
Joe Perches63c3a662011-04-26 08:12:10 +00008040 !tg3_flag(tp, 57765_PLUS)) {
Andy Gospodarekab0049b2007-09-06 20:42:14 +01008041 val = tr32(0x7c00);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008042
8043 tw32(0x7c00, val | (1 << 25));
8044 }
8045
Matt Carlsond78b59f2011-04-05 14:22:46 +00008046 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
8047 val = tr32(TG3_CPMU_CLCK_ORIDE);
8048 tw32(TG3_CPMU_CLCK_ORIDE, val & ~CPMU_CLCK_ORIDE_MAC_ORIDE_EN);
8049 }
8050
Linus Torvalds1da177e2005-04-16 15:20:36 -07008051 /* Reprobe ASF enable state. */
Joe Perches63c3a662011-04-26 08:12:10 +00008052 tg3_flag_clear(tp, ENABLE_ASF);
8053 tg3_flag_clear(tp, ASF_NEW_HANDSHAKE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008054 tg3_read_mem(tp, NIC_SRAM_DATA_SIG, &val);
8055 if (val == NIC_SRAM_DATA_SIG_MAGIC) {
8056 u32 nic_cfg;
8057
8058 tg3_read_mem(tp, NIC_SRAM_DATA_CFG, &nic_cfg);
8059 if (nic_cfg & NIC_SRAM_DATA_CFG_ASF_ENABLE) {
Joe Perches63c3a662011-04-26 08:12:10 +00008060 tg3_flag_set(tp, ENABLE_ASF);
Matt Carlson4ba526c2008-08-15 14:10:04 -07008061 tp->last_event_jiffies = jiffies;
Joe Perches63c3a662011-04-26 08:12:10 +00008062 if (tg3_flag(tp, 5750_PLUS))
8063 tg3_flag_set(tp, ASF_NEW_HANDSHAKE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008064 }
8065 }
8066
8067 return 0;
8068}
8069
Matt Carlson65ec6982012-02-28 23:33:37 +00008070static void tg3_get_nstats(struct tg3 *, struct rtnl_link_stats64 *);
8071static void tg3_get_estats(struct tg3 *, struct tg3_ethtool_stats *);
Matt Carlson92feeab2011-12-08 14:40:14 +00008072
Linus Torvalds1da177e2005-04-16 15:20:36 -07008073/* tp->lock is held. */
Michael Chan944d9802005-05-29 14:57:48 -07008074static int tg3_halt(struct tg3 *tp, int kind, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008075{
8076 int err;
8077
8078 tg3_stop_fw(tp);
8079
Michael Chan944d9802005-05-29 14:57:48 -07008080 tg3_write_sig_pre_reset(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008081
David S. Millerb3b7d6b2005-05-05 14:40:20 -07008082 tg3_abort_hw(tp, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008083 err = tg3_chip_reset(tp);
8084
Matt Carlsondaba2a62009-04-20 06:58:52 +00008085 __tg3_set_mac_addr(tp, 0);
8086
Michael Chan944d9802005-05-29 14:57:48 -07008087 tg3_write_sig_legacy(tp, kind);
8088 tg3_write_sig_post_reset(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008089
Matt Carlson92feeab2011-12-08 14:40:14 +00008090 if (tp->hw_stats) {
8091 /* Save the stats across chip resets... */
David S. Millerb4017c52012-03-01 17:57:40 -05008092 tg3_get_nstats(tp, &tp->net_stats_prev);
Matt Carlson92feeab2011-12-08 14:40:14 +00008093 tg3_get_estats(tp, &tp->estats_prev);
8094
8095 /* And make sure the next sample is new data */
8096 memset(tp->hw_stats, 0, sizeof(struct tg3_hw_stats));
8097 }
8098
Linus Torvalds1da177e2005-04-16 15:20:36 -07008099 if (err)
8100 return err;
8101
8102 return 0;
8103}
8104
Linus Torvalds1da177e2005-04-16 15:20:36 -07008105static int tg3_set_mac_addr(struct net_device *dev, void *p)
8106{
8107 struct tg3 *tp = netdev_priv(dev);
8108 struct sockaddr *addr = p;
Michael Chan986e0ae2007-05-05 12:10:20 -07008109 int err = 0, skip_mac_1 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008110
Michael Chanf9804dd2005-09-27 12:13:10 -07008111 if (!is_valid_ether_addr(addr->sa_data))
Danny Kukawka504f9b52012-02-21 02:07:49 +00008112 return -EADDRNOTAVAIL;
Michael Chanf9804dd2005-09-27 12:13:10 -07008113
Linus Torvalds1da177e2005-04-16 15:20:36 -07008114 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
8115
Michael Chane75f7c92006-03-20 21:33:26 -08008116 if (!netif_running(dev))
8117 return 0;
8118
Joe Perches63c3a662011-04-26 08:12:10 +00008119 if (tg3_flag(tp, ENABLE_ASF)) {
Michael Chan986e0ae2007-05-05 12:10:20 -07008120 u32 addr0_high, addr0_low, addr1_high, addr1_low;
Michael Chan58712ef2006-04-29 18:58:01 -07008121
Michael Chan986e0ae2007-05-05 12:10:20 -07008122 addr0_high = tr32(MAC_ADDR_0_HIGH);
8123 addr0_low = tr32(MAC_ADDR_0_LOW);
8124 addr1_high = tr32(MAC_ADDR_1_HIGH);
8125 addr1_low = tr32(MAC_ADDR_1_LOW);
8126
8127 /* Skip MAC addr 1 if ASF is using it. */
8128 if ((addr0_high != addr1_high || addr0_low != addr1_low) &&
8129 !(addr1_high == 0 && addr1_low == 0))
8130 skip_mac_1 = 1;
Michael Chan58712ef2006-04-29 18:58:01 -07008131 }
Michael Chan986e0ae2007-05-05 12:10:20 -07008132 spin_lock_bh(&tp->lock);
8133 __tg3_set_mac_addr(tp, skip_mac_1);
8134 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008135
Michael Chanb9ec6c12006-07-25 16:37:27 -07008136 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008137}
8138
8139/* tp->lock is held. */
8140static void tg3_set_bdinfo(struct tg3 *tp, u32 bdinfo_addr,
8141 dma_addr_t mapping, u32 maxlen_flags,
8142 u32 nic_addr)
8143{
8144 tg3_write_mem(tp,
8145 (bdinfo_addr + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH),
8146 ((u64) mapping >> 32));
8147 tg3_write_mem(tp,
8148 (bdinfo_addr + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW),
8149 ((u64) mapping & 0xffffffff));
8150 tg3_write_mem(tp,
8151 (bdinfo_addr + TG3_BDINFO_MAXLEN_FLAGS),
8152 maxlen_flags);
8153
Joe Perches63c3a662011-04-26 08:12:10 +00008154 if (!tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008155 tg3_write_mem(tp,
8156 (bdinfo_addr + TG3_BDINFO_NIC_ADDR),
8157 nic_addr);
8158}
8159
Michael Chand244c892005-07-05 14:42:33 -07008160static void __tg3_set_coalesce(struct tg3 *tp, struct ethtool_coalesce *ec)
David S. Miller15f98502005-05-18 22:49:26 -07008161{
Matt Carlsonb6080e12009-09-01 13:12:00 +00008162 int i;
8163
Joe Perches63c3a662011-04-26 08:12:10 +00008164 if (!tg3_flag(tp, ENABLE_TSS)) {
Matt Carlsonb6080e12009-09-01 13:12:00 +00008165 tw32(HOSTCC_TXCOL_TICKS, ec->tx_coalesce_usecs);
8166 tw32(HOSTCC_TXMAX_FRAMES, ec->tx_max_coalesced_frames);
8167 tw32(HOSTCC_TXCOAL_MAXF_INT, ec->tx_max_coalesced_frames_irq);
Matt Carlsonb6080e12009-09-01 13:12:00 +00008168 } else {
8169 tw32(HOSTCC_TXCOL_TICKS, 0);
8170 tw32(HOSTCC_TXMAX_FRAMES, 0);
8171 tw32(HOSTCC_TXCOAL_MAXF_INT, 0);
Matt Carlson19cfaec2009-12-03 08:36:20 +00008172 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00008173
Joe Perches63c3a662011-04-26 08:12:10 +00008174 if (!tg3_flag(tp, ENABLE_RSS)) {
Matt Carlson19cfaec2009-12-03 08:36:20 +00008175 tw32(HOSTCC_RXCOL_TICKS, ec->rx_coalesce_usecs);
8176 tw32(HOSTCC_RXMAX_FRAMES, ec->rx_max_coalesced_frames);
8177 tw32(HOSTCC_RXCOAL_MAXF_INT, ec->rx_max_coalesced_frames_irq);
8178 } else {
Matt Carlsonb6080e12009-09-01 13:12:00 +00008179 tw32(HOSTCC_RXCOL_TICKS, 0);
8180 tw32(HOSTCC_RXMAX_FRAMES, 0);
8181 tw32(HOSTCC_RXCOAL_MAXF_INT, 0);
David S. Miller15f98502005-05-18 22:49:26 -07008182 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00008183
Joe Perches63c3a662011-04-26 08:12:10 +00008184 if (!tg3_flag(tp, 5705_PLUS)) {
David S. Miller15f98502005-05-18 22:49:26 -07008185 u32 val = ec->stats_block_coalesce_usecs;
8186
Matt Carlsonb6080e12009-09-01 13:12:00 +00008187 tw32(HOSTCC_RXCOAL_TICK_INT, ec->rx_coalesce_usecs_irq);
8188 tw32(HOSTCC_TXCOAL_TICK_INT, ec->tx_coalesce_usecs_irq);
8189
David S. Miller15f98502005-05-18 22:49:26 -07008190 if (!netif_carrier_ok(tp->dev))
8191 val = 0;
8192
8193 tw32(HOSTCC_STAT_COAL_TICKS, val);
8194 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00008195
8196 for (i = 0; i < tp->irq_cnt - 1; i++) {
8197 u32 reg;
8198
8199 reg = HOSTCC_RXCOL_TICKS_VEC1 + i * 0x18;
8200 tw32(reg, ec->rx_coalesce_usecs);
Matt Carlsonb6080e12009-09-01 13:12:00 +00008201 reg = HOSTCC_RXMAX_FRAMES_VEC1 + i * 0x18;
8202 tw32(reg, ec->rx_max_coalesced_frames);
Matt Carlsonb6080e12009-09-01 13:12:00 +00008203 reg = HOSTCC_RXCOAL_MAXF_INT_VEC1 + i * 0x18;
8204 tw32(reg, ec->rx_max_coalesced_frames_irq);
Matt Carlson19cfaec2009-12-03 08:36:20 +00008205
Joe Perches63c3a662011-04-26 08:12:10 +00008206 if (tg3_flag(tp, ENABLE_TSS)) {
Matt Carlson19cfaec2009-12-03 08:36:20 +00008207 reg = HOSTCC_TXCOL_TICKS_VEC1 + i * 0x18;
8208 tw32(reg, ec->tx_coalesce_usecs);
8209 reg = HOSTCC_TXMAX_FRAMES_VEC1 + i * 0x18;
8210 tw32(reg, ec->tx_max_coalesced_frames);
8211 reg = HOSTCC_TXCOAL_MAXF_INT_VEC1 + i * 0x18;
8212 tw32(reg, ec->tx_max_coalesced_frames_irq);
8213 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00008214 }
8215
8216 for (; i < tp->irq_max - 1; i++) {
8217 tw32(HOSTCC_RXCOL_TICKS_VEC1 + i * 0x18, 0);
Matt Carlsonb6080e12009-09-01 13:12:00 +00008218 tw32(HOSTCC_RXMAX_FRAMES_VEC1 + i * 0x18, 0);
Matt Carlsonb6080e12009-09-01 13:12:00 +00008219 tw32(HOSTCC_RXCOAL_MAXF_INT_VEC1 + i * 0x18, 0);
Matt Carlson19cfaec2009-12-03 08:36:20 +00008220
Joe Perches63c3a662011-04-26 08:12:10 +00008221 if (tg3_flag(tp, ENABLE_TSS)) {
Matt Carlson19cfaec2009-12-03 08:36:20 +00008222 tw32(HOSTCC_TXCOL_TICKS_VEC1 + i * 0x18, 0);
8223 tw32(HOSTCC_TXMAX_FRAMES_VEC1 + i * 0x18, 0);
8224 tw32(HOSTCC_TXCOAL_MAXF_INT_VEC1 + i * 0x18, 0);
8225 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00008226 }
David S. Miller15f98502005-05-18 22:49:26 -07008227}
Linus Torvalds1da177e2005-04-16 15:20:36 -07008228
8229/* tp->lock is held. */
Matt Carlson2d31eca2009-09-01 12:53:31 +00008230static void tg3_rings_reset(struct tg3 *tp)
8231{
8232 int i;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008233 u32 stblk, txrcb, rxrcb, limit;
Matt Carlson2d31eca2009-09-01 12:53:31 +00008234 struct tg3_napi *tnapi = &tp->napi[0];
8235
8236 /* Disable all transmit rings but the first. */
Joe Perches63c3a662011-04-26 08:12:10 +00008237 if (!tg3_flag(tp, 5705_PLUS))
Matt Carlson2d31eca2009-09-01 12:53:31 +00008238 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 16;
Joe Perches63c3a662011-04-26 08:12:10 +00008239 else if (tg3_flag(tp, 5717_PLUS))
Matt Carlson3d377282010-10-14 10:37:39 +00008240 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 4;
Matt Carlson55086ad2011-12-14 11:09:59 +00008241 else if (tg3_flag(tp, 57765_CLASS))
Matt Carlsonb703df62009-12-03 08:36:21 +00008242 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 2;
Matt Carlson2d31eca2009-09-01 12:53:31 +00008243 else
8244 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE;
8245
8246 for (txrcb = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE;
8247 txrcb < limit; txrcb += TG3_BDINFO_SIZE)
8248 tg3_write_mem(tp, txrcb + TG3_BDINFO_MAXLEN_FLAGS,
8249 BDINFO_FLAGS_DISABLED);
8250
8251
8252 /* Disable all receive return rings but the first. */
Joe Perches63c3a662011-04-26 08:12:10 +00008253 if (tg3_flag(tp, 5717_PLUS))
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00008254 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 17;
Joe Perches63c3a662011-04-26 08:12:10 +00008255 else if (!tg3_flag(tp, 5705_PLUS))
Matt Carlson2d31eca2009-09-01 12:53:31 +00008256 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 16;
Matt Carlsonb703df62009-12-03 08:36:21 +00008257 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
Matt Carlson55086ad2011-12-14 11:09:59 +00008258 tg3_flag(tp, 57765_CLASS))
Matt Carlson2d31eca2009-09-01 12:53:31 +00008259 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 4;
8260 else
8261 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE;
8262
8263 for (rxrcb = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE;
8264 rxrcb < limit; rxrcb += TG3_BDINFO_SIZE)
8265 tg3_write_mem(tp, rxrcb + TG3_BDINFO_MAXLEN_FLAGS,
8266 BDINFO_FLAGS_DISABLED);
8267
8268 /* Disable interrupts */
8269 tw32_mailbox_f(tp->napi[0].int_mbox, 1);
Matt Carlson0e6cf6a2011-06-13 13:38:55 +00008270 tp->napi[0].chk_msi_cnt = 0;
8271 tp->napi[0].last_rx_cons = 0;
8272 tp->napi[0].last_tx_cons = 0;
Matt Carlson2d31eca2009-09-01 12:53:31 +00008273
8274 /* Zero mailbox registers. */
Joe Perches63c3a662011-04-26 08:12:10 +00008275 if (tg3_flag(tp, SUPPORT_MSIX)) {
Matt Carlson6fd45cb2010-09-15 08:59:57 +00008276 for (i = 1; i < tp->irq_max; i++) {
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008277 tp->napi[i].tx_prod = 0;
8278 tp->napi[i].tx_cons = 0;
Joe Perches63c3a662011-04-26 08:12:10 +00008279 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlsonc2353a32010-01-20 16:58:08 +00008280 tw32_mailbox(tp->napi[i].prodmbox, 0);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008281 tw32_rx_mbox(tp->napi[i].consmbox, 0);
8282 tw32_mailbox_f(tp->napi[i].int_mbox, 1);
Matt Carlson7f230732011-08-31 11:44:48 +00008283 tp->napi[i].chk_msi_cnt = 0;
Matt Carlson0e6cf6a2011-06-13 13:38:55 +00008284 tp->napi[i].last_rx_cons = 0;
8285 tp->napi[i].last_tx_cons = 0;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008286 }
Joe Perches63c3a662011-04-26 08:12:10 +00008287 if (!tg3_flag(tp, ENABLE_TSS))
Matt Carlsonc2353a32010-01-20 16:58:08 +00008288 tw32_mailbox(tp->napi[0].prodmbox, 0);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008289 } else {
8290 tp->napi[0].tx_prod = 0;
8291 tp->napi[0].tx_cons = 0;
8292 tw32_mailbox(tp->napi[0].prodmbox, 0);
8293 tw32_rx_mbox(tp->napi[0].consmbox, 0);
8294 }
Matt Carlson2d31eca2009-09-01 12:53:31 +00008295
8296 /* Make sure the NIC-based send BD rings are disabled. */
Joe Perches63c3a662011-04-26 08:12:10 +00008297 if (!tg3_flag(tp, 5705_PLUS)) {
Matt Carlson2d31eca2009-09-01 12:53:31 +00008298 u32 mbox = MAILBOX_SNDNIC_PROD_IDX_0 + TG3_64BIT_REG_LOW;
8299 for (i = 0; i < 16; i++)
8300 tw32_tx_mbox(mbox + i * 8, 0);
8301 }
8302
8303 txrcb = NIC_SRAM_SEND_RCB;
8304 rxrcb = NIC_SRAM_RCV_RET_RCB;
8305
8306 /* Clear status block in ram. */
8307 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
8308
8309 /* Set status block DMA address */
8310 tw32(HOSTCC_STATUS_BLK_HOST_ADDR + TG3_64BIT_REG_HIGH,
8311 ((u64) tnapi->status_mapping >> 32));
8312 tw32(HOSTCC_STATUS_BLK_HOST_ADDR + TG3_64BIT_REG_LOW,
8313 ((u64) tnapi->status_mapping & 0xffffffff));
8314
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008315 if (tnapi->tx_ring) {
8316 tg3_set_bdinfo(tp, txrcb, tnapi->tx_desc_mapping,
8317 (TG3_TX_RING_SIZE <<
8318 BDINFO_FLAGS_MAXLEN_SHIFT),
8319 NIC_SRAM_TX_BUFFER_DESC);
8320 txrcb += TG3_BDINFO_SIZE;
8321 }
Matt Carlson2d31eca2009-09-01 12:53:31 +00008322
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008323 if (tnapi->rx_rcb) {
8324 tg3_set_bdinfo(tp, rxrcb, tnapi->rx_rcb_mapping,
Matt Carlson7cb32cf2010-09-30 10:34:36 +00008325 (tp->rx_ret_ring_mask + 1) <<
8326 BDINFO_FLAGS_MAXLEN_SHIFT, 0);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008327 rxrcb += TG3_BDINFO_SIZE;
8328 }
8329
8330 stblk = HOSTCC_STATBLCK_RING1;
8331
8332 for (i = 1, tnapi++; i < tp->irq_cnt; i++, tnapi++) {
8333 u64 mapping = (u64)tnapi->status_mapping;
8334 tw32(stblk + TG3_64BIT_REG_HIGH, mapping >> 32);
8335 tw32(stblk + TG3_64BIT_REG_LOW, mapping & 0xffffffff);
8336
8337 /* Clear status block in ram. */
8338 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
8339
Matt Carlson19cfaec2009-12-03 08:36:20 +00008340 if (tnapi->tx_ring) {
8341 tg3_set_bdinfo(tp, txrcb, tnapi->tx_desc_mapping,
8342 (TG3_TX_RING_SIZE <<
8343 BDINFO_FLAGS_MAXLEN_SHIFT),
8344 NIC_SRAM_TX_BUFFER_DESC);
8345 txrcb += TG3_BDINFO_SIZE;
8346 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008347
8348 tg3_set_bdinfo(tp, rxrcb, tnapi->rx_rcb_mapping,
Matt Carlson7cb32cf2010-09-30 10:34:36 +00008349 ((tp->rx_ret_ring_mask + 1) <<
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008350 BDINFO_FLAGS_MAXLEN_SHIFT), 0);
8351
8352 stblk += 8;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008353 rxrcb += TG3_BDINFO_SIZE;
8354 }
Matt Carlson2d31eca2009-09-01 12:53:31 +00008355}
8356
Matt Carlsoneb07a942011-04-20 07:57:36 +00008357static void tg3_setup_rxbd_thresholds(struct tg3 *tp)
8358{
8359 u32 val, bdcache_maxcnt, host_rep_thresh, nic_rep_thresh;
8360
Joe Perches63c3a662011-04-26 08:12:10 +00008361 if (!tg3_flag(tp, 5750_PLUS) ||
8362 tg3_flag(tp, 5780_CLASS) ||
Matt Carlsoneb07a942011-04-20 07:57:36 +00008363 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 ||
Matt Carlson513aa6e2011-11-21 15:01:18 +00008364 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752 ||
8365 tg3_flag(tp, 57765_PLUS))
Matt Carlsoneb07a942011-04-20 07:57:36 +00008366 bdcache_maxcnt = TG3_SRAM_RX_STD_BDCACHE_SIZE_5700;
8367 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
8368 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787)
8369 bdcache_maxcnt = TG3_SRAM_RX_STD_BDCACHE_SIZE_5755;
8370 else
8371 bdcache_maxcnt = TG3_SRAM_RX_STD_BDCACHE_SIZE_5906;
8372
8373 nic_rep_thresh = min(bdcache_maxcnt / 2, tp->rx_std_max_post);
8374 host_rep_thresh = max_t(u32, tp->rx_pending / 8, 1);
8375
8376 val = min(nic_rep_thresh, host_rep_thresh);
8377 tw32(RCVBDI_STD_THRESH, val);
8378
Joe Perches63c3a662011-04-26 08:12:10 +00008379 if (tg3_flag(tp, 57765_PLUS))
Matt Carlsoneb07a942011-04-20 07:57:36 +00008380 tw32(STD_REPLENISH_LWM, bdcache_maxcnt);
8381
Joe Perches63c3a662011-04-26 08:12:10 +00008382 if (!tg3_flag(tp, JUMBO_CAPABLE) || tg3_flag(tp, 5780_CLASS))
Matt Carlsoneb07a942011-04-20 07:57:36 +00008383 return;
8384
Matt Carlson513aa6e2011-11-21 15:01:18 +00008385 bdcache_maxcnt = TG3_SRAM_RX_JMB_BDCACHE_SIZE_5700;
Matt Carlsoneb07a942011-04-20 07:57:36 +00008386
8387 host_rep_thresh = max_t(u32, tp->rx_jumbo_pending / 8, 1);
8388
8389 val = min(bdcache_maxcnt / 2, host_rep_thresh);
8390 tw32(RCVBDI_JUMBO_THRESH, val);
8391
Joe Perches63c3a662011-04-26 08:12:10 +00008392 if (tg3_flag(tp, 57765_PLUS))
Matt Carlsoneb07a942011-04-20 07:57:36 +00008393 tw32(JMB_REPLENISH_LWM, bdcache_maxcnt);
8394}
8395
Matt Carlsonccd5ba92012-02-13 10:20:08 +00008396static inline u32 calc_crc(unsigned char *buf, int len)
8397{
8398 u32 reg;
8399 u32 tmp;
8400 int j, k;
8401
8402 reg = 0xffffffff;
8403
8404 for (j = 0; j < len; j++) {
8405 reg ^= buf[j];
8406
8407 for (k = 0; k < 8; k++) {
8408 tmp = reg & 0x01;
8409
8410 reg >>= 1;
8411
8412 if (tmp)
8413 reg ^= 0xedb88320;
8414 }
8415 }
8416
8417 return ~reg;
8418}
8419
8420static void tg3_set_multi(struct tg3 *tp, unsigned int accept_all)
8421{
8422 /* accept or reject all multicast frames */
8423 tw32(MAC_HASH_REG_0, accept_all ? 0xffffffff : 0);
8424 tw32(MAC_HASH_REG_1, accept_all ? 0xffffffff : 0);
8425 tw32(MAC_HASH_REG_2, accept_all ? 0xffffffff : 0);
8426 tw32(MAC_HASH_REG_3, accept_all ? 0xffffffff : 0);
8427}
8428
8429static void __tg3_set_rx_mode(struct net_device *dev)
8430{
8431 struct tg3 *tp = netdev_priv(dev);
8432 u32 rx_mode;
8433
8434 rx_mode = tp->rx_mode & ~(RX_MODE_PROMISC |
8435 RX_MODE_KEEP_VLAN_TAG);
8436
8437#if !defined(CONFIG_VLAN_8021Q) && !defined(CONFIG_VLAN_8021Q_MODULE)
8438 /* When ASF is in use, we always keep the RX_MODE_KEEP_VLAN_TAG
8439 * flag clear.
8440 */
8441 if (!tg3_flag(tp, ENABLE_ASF))
8442 rx_mode |= RX_MODE_KEEP_VLAN_TAG;
8443#endif
8444
8445 if (dev->flags & IFF_PROMISC) {
8446 /* Promiscuous mode. */
8447 rx_mode |= RX_MODE_PROMISC;
8448 } else if (dev->flags & IFF_ALLMULTI) {
8449 /* Accept all multicast. */
8450 tg3_set_multi(tp, 1);
8451 } else if (netdev_mc_empty(dev)) {
8452 /* Reject all multicast. */
8453 tg3_set_multi(tp, 0);
8454 } else {
8455 /* Accept one or more multicast(s). */
8456 struct netdev_hw_addr *ha;
8457 u32 mc_filter[4] = { 0, };
8458 u32 regidx;
8459 u32 bit;
8460 u32 crc;
8461
8462 netdev_for_each_mc_addr(ha, dev) {
8463 crc = calc_crc(ha->addr, ETH_ALEN);
8464 bit = ~crc & 0x7f;
8465 regidx = (bit & 0x60) >> 5;
8466 bit &= 0x1f;
8467 mc_filter[regidx] |= (1 << bit);
8468 }
8469
8470 tw32(MAC_HASH_REG_0, mc_filter[0]);
8471 tw32(MAC_HASH_REG_1, mc_filter[1]);
8472 tw32(MAC_HASH_REG_2, mc_filter[2]);
8473 tw32(MAC_HASH_REG_3, mc_filter[3]);
8474 }
8475
8476 if (rx_mode != tp->rx_mode) {
8477 tp->rx_mode = rx_mode;
8478 tw32_f(MAC_RX_MODE, rx_mode);
8479 udelay(10);
8480 }
8481}
8482
Matt Carlson90415472011-12-16 13:33:23 +00008483static void tg3_rss_init_dflt_indir_tbl(struct tg3 *tp)
8484{
8485 int i;
8486
8487 for (i = 0; i < TG3_RSS_INDIR_TBL_SIZE; i++)
8488 tp->rss_ind_tbl[i] =
8489 ethtool_rxfh_indir_default(i, tp->irq_cnt - 1);
8490}
8491
8492static void tg3_rss_check_indir_tbl(struct tg3 *tp)
Matt Carlsonbcebcc42011-12-14 11:10:01 +00008493{
8494 int i;
8495
8496 if (!tg3_flag(tp, SUPPORT_MSIX))
8497 return;
8498
Matt Carlson90415472011-12-16 13:33:23 +00008499 if (tp->irq_cnt <= 2) {
Matt Carlsonbcebcc42011-12-14 11:10:01 +00008500 memset(&tp->rss_ind_tbl[0], 0, sizeof(tp->rss_ind_tbl));
Matt Carlson90415472011-12-16 13:33:23 +00008501 return;
8502 }
8503
8504 /* Validate table against current IRQ count */
8505 for (i = 0; i < TG3_RSS_INDIR_TBL_SIZE; i++) {
8506 if (tp->rss_ind_tbl[i] >= tp->irq_cnt - 1)
8507 break;
8508 }
8509
8510 if (i != TG3_RSS_INDIR_TBL_SIZE)
8511 tg3_rss_init_dflt_indir_tbl(tp);
Matt Carlsonbcebcc42011-12-14 11:10:01 +00008512}
8513
Matt Carlson90415472011-12-16 13:33:23 +00008514static void tg3_rss_write_indir_tbl(struct tg3 *tp)
Matt Carlsonbcebcc42011-12-14 11:10:01 +00008515{
8516 int i = 0;
8517 u32 reg = MAC_RSS_INDIR_TBL_0;
8518
8519 while (i < TG3_RSS_INDIR_TBL_SIZE) {
8520 u32 val = tp->rss_ind_tbl[i];
8521 i++;
8522 for (; i % 8; i++) {
8523 val <<= 4;
8524 val |= tp->rss_ind_tbl[i];
8525 }
8526 tw32(reg, val);
8527 reg += 4;
8528 }
8529}
8530
Matt Carlson2d31eca2009-09-01 12:53:31 +00008531/* tp->lock is held. */
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07008532static int tg3_reset_hw(struct tg3 *tp, int reset_phy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008533{
8534 u32 val, rdmac_mode;
8535 int i, err, limit;
Matt Carlson8fea32b2010-09-15 08:59:58 +00008536 struct tg3_rx_prodring_set *tpr = &tp->napi[0].prodring;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008537
8538 tg3_disable_ints(tp);
8539
8540 tg3_stop_fw(tp);
8541
8542 tg3_write_sig_pre_reset(tp, RESET_KIND_INIT);
8543
Joe Perches63c3a662011-04-26 08:12:10 +00008544 if (tg3_flag(tp, INIT_COMPLETE))
Michael Chane6de8ad2005-05-05 14:42:41 -07008545 tg3_abort_hw(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008546
Matt Carlson699c0192010-12-06 08:28:51 +00008547 /* Enable MAC control of LPI */
8548 if (tp->phy_flags & TG3_PHYFLG_EEE_CAP) {
8549 tw32_f(TG3_CPMU_EEE_LNKIDL_CTRL,
8550 TG3_CPMU_EEE_LNKIDL_PCIE_NL0 |
8551 TG3_CPMU_EEE_LNKIDL_UART_IDL);
8552
8553 tw32_f(TG3_CPMU_EEE_CTRL,
8554 TG3_CPMU_EEE_CTRL_EXIT_20_1_US);
8555
Matt Carlsona386b902010-12-06 08:28:53 +00008556 val = TG3_CPMU_EEEMD_ERLY_L1_XIT_DET |
8557 TG3_CPMU_EEEMD_LPI_IN_TX |
8558 TG3_CPMU_EEEMD_LPI_IN_RX |
8559 TG3_CPMU_EEEMD_EEE_ENABLE;
8560
8561 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717)
8562 val |= TG3_CPMU_EEEMD_SND_IDX_DET_EN;
8563
Joe Perches63c3a662011-04-26 08:12:10 +00008564 if (tg3_flag(tp, ENABLE_APE))
Matt Carlsona386b902010-12-06 08:28:53 +00008565 val |= TG3_CPMU_EEEMD_APE_TX_DET_EN;
8566
8567 tw32_f(TG3_CPMU_EEE_MODE, val);
8568
8569 tw32_f(TG3_CPMU_EEE_DBTMR1,
8570 TG3_CPMU_DBTMR1_PCIEXIT_2047US |
8571 TG3_CPMU_DBTMR1_LNKIDLE_2047US);
8572
8573 tw32_f(TG3_CPMU_EEE_DBTMR2,
Matt Carlsond7f2ab22011-01-25 15:58:56 +00008574 TG3_CPMU_DBTMR2_APE_TX_2047US |
Matt Carlsona386b902010-12-06 08:28:53 +00008575 TG3_CPMU_DBTMR2_TXIDXEQ_2047US);
Matt Carlson699c0192010-12-06 08:28:51 +00008576 }
8577
Matt Carlson603f1172010-02-12 14:47:10 +00008578 if (reset_phy)
Michael Chand4d2c552006-03-20 17:47:20 -08008579 tg3_phy_reset(tp);
8580
Linus Torvalds1da177e2005-04-16 15:20:36 -07008581 err = tg3_chip_reset(tp);
8582 if (err)
8583 return err;
8584
8585 tg3_write_sig_legacy(tp, RESET_KIND_INIT);
8586
Matt Carlsonbcb37f62008-11-03 16:52:09 -08008587 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX) {
Matt Carlsond30cdd22007-10-07 23:28:35 -07008588 val = tr32(TG3_CPMU_CTRL);
8589 val &= ~(CPMU_CTRL_LINK_AWARE_MODE | CPMU_CTRL_LINK_IDLE_MODE);
8590 tw32(TG3_CPMU_CTRL, val);
Matt Carlson9acb9612007-11-12 21:10:06 -08008591
8592 val = tr32(TG3_CPMU_LSPD_10MB_CLK);
8593 val &= ~CPMU_LSPD_10MB_MACCLK_MASK;
8594 val |= CPMU_LSPD_10MB_MACCLK_6_25;
8595 tw32(TG3_CPMU_LSPD_10MB_CLK, val);
8596
8597 val = tr32(TG3_CPMU_LNK_AWARE_PWRMD);
8598 val &= ~CPMU_LNK_AWARE_MACCLK_MASK;
8599 val |= CPMU_LNK_AWARE_MACCLK_6_25;
8600 tw32(TG3_CPMU_LNK_AWARE_PWRMD, val);
8601
8602 val = tr32(TG3_CPMU_HST_ACC);
8603 val &= ~CPMU_HST_ACC_MACCLK_MASK;
8604 val |= CPMU_HST_ACC_MACCLK_6_25;
8605 tw32(TG3_CPMU_HST_ACC, val);
Matt Carlsond30cdd22007-10-07 23:28:35 -07008606 }
8607
Matt Carlson33466d92009-04-20 06:57:41 +00008608 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780) {
8609 val = tr32(PCIE_PWR_MGMT_THRESH) & ~PCIE_PWR_MGMT_L1_THRESH_MSK;
8610 val |= PCIE_PWR_MGMT_EXT_ASPM_TMR_EN |
8611 PCIE_PWR_MGMT_L1_THRESH_4MS;
8612 tw32(PCIE_PWR_MGMT_THRESH, val);
Matt Carlson521e6b92009-08-25 10:06:01 +00008613
8614 val = tr32(TG3_PCIE_EIDLE_DELAY) & ~TG3_PCIE_EIDLE_DELAY_MASK;
8615 tw32(TG3_PCIE_EIDLE_DELAY, val | TG3_PCIE_EIDLE_DELAY_13_CLKS);
8616
8617 tw32(TG3_CORR_ERR_STAT, TG3_CORR_ERR_STAT_CLEAR);
Matt Carlson33466d92009-04-20 06:57:41 +00008618
Matt Carlsonf40386c2009-11-02 14:24:02 +00008619 val = tr32(TG3_PCIE_LNKCTL) & ~TG3_PCIE_LNKCTL_L1_PLL_PD_EN;
8620 tw32(TG3_PCIE_LNKCTL, val | TG3_PCIE_LNKCTL_L1_PLL_PD_DIS);
Matt Carlson255ca312009-08-25 10:07:27 +00008621 }
8622
Joe Perches63c3a662011-04-26 08:12:10 +00008623 if (tg3_flag(tp, L1PLLPD_EN)) {
Matt Carlson614b0592010-01-20 16:58:02 +00008624 u32 grc_mode = tr32(GRC_MODE);
8625
8626 /* Access the lower 1K of PL PCIE block registers. */
8627 val = grc_mode & ~GRC_MODE_PCIE_PORT_MASK;
8628 tw32(GRC_MODE, val | GRC_MODE_PCIE_PL_SEL);
8629
8630 val = tr32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL1);
8631 tw32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL1,
8632 val | TG3_PCIE_PL_LO_PHYCTL1_L1PLLPD_EN);
8633
8634 tw32(GRC_MODE, grc_mode);
8635 }
8636
Matt Carlson55086ad2011-12-14 11:09:59 +00008637 if (tg3_flag(tp, 57765_CLASS)) {
Matt Carlson5093eed2010-11-24 08:31:45 +00008638 if (tp->pci_chip_rev_id == CHIPREV_ID_57765_A0) {
8639 u32 grc_mode = tr32(GRC_MODE);
Matt Carlsoncea46462010-04-12 06:58:24 +00008640
Matt Carlson5093eed2010-11-24 08:31:45 +00008641 /* Access the lower 1K of PL PCIE block registers. */
8642 val = grc_mode & ~GRC_MODE_PCIE_PORT_MASK;
8643 tw32(GRC_MODE, val | GRC_MODE_PCIE_PL_SEL);
Matt Carlsoncea46462010-04-12 06:58:24 +00008644
Matt Carlson5093eed2010-11-24 08:31:45 +00008645 val = tr32(TG3_PCIE_TLDLPL_PORT +
8646 TG3_PCIE_PL_LO_PHYCTL5);
8647 tw32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL5,
8648 val | TG3_PCIE_PL_LO_PHYCTL5_DIS_L2CLKREQ);
Matt Carlsoncea46462010-04-12 06:58:24 +00008649
Matt Carlson5093eed2010-11-24 08:31:45 +00008650 tw32(GRC_MODE, grc_mode);
8651 }
Matt Carlsona977dbe2010-04-12 06:58:26 +00008652
Matt Carlson1ff30a52011-05-19 12:12:46 +00008653 if (GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_57765_AX) {
8654 u32 grc_mode = tr32(GRC_MODE);
8655
8656 /* Access the lower 1K of DL PCIE block registers. */
8657 val = grc_mode & ~GRC_MODE_PCIE_PORT_MASK;
8658 tw32(GRC_MODE, val | GRC_MODE_PCIE_DL_SEL);
8659
8660 val = tr32(TG3_PCIE_TLDLPL_PORT +
8661 TG3_PCIE_DL_LO_FTSMAX);
8662 val &= ~TG3_PCIE_DL_LO_FTSMAX_MSK;
8663 tw32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_DL_LO_FTSMAX,
8664 val | TG3_PCIE_DL_LO_FTSMAX_VAL);
8665
8666 tw32(GRC_MODE, grc_mode);
8667 }
8668
Matt Carlsona977dbe2010-04-12 06:58:26 +00008669 val = tr32(TG3_CPMU_LSPD_10MB_CLK);
8670 val &= ~CPMU_LSPD_10MB_MACCLK_MASK;
8671 val |= CPMU_LSPD_10MB_MACCLK_6_25;
8672 tw32(TG3_CPMU_LSPD_10MB_CLK, val);
Matt Carlsoncea46462010-04-12 06:58:24 +00008673 }
8674
Linus Torvalds1da177e2005-04-16 15:20:36 -07008675 /* This works around an issue with Athlon chipsets on
8676 * B3 tigon3 silicon. This bit has no effect on any
8677 * other revision. But do not set this on PCI Express
Matt Carlson795d01c2007-10-07 23:28:17 -07008678 * chips and don't even touch the clocks if the CPMU is present.
Linus Torvalds1da177e2005-04-16 15:20:36 -07008679 */
Joe Perches63c3a662011-04-26 08:12:10 +00008680 if (!tg3_flag(tp, CPMU_PRESENT)) {
8681 if (!tg3_flag(tp, PCI_EXPRESS))
Matt Carlson795d01c2007-10-07 23:28:17 -07008682 tp->pci_clock_ctrl |= CLOCK_CTRL_DELAY_PCI_GRANT;
8683 tw32_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl);
8684 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008685
8686 if (tp->pci_chip_rev_id == CHIPREV_ID_5704_A0 &&
Joe Perches63c3a662011-04-26 08:12:10 +00008687 tg3_flag(tp, PCIX_MODE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008688 val = tr32(TG3PCI_PCISTATE);
8689 val |= PCISTATE_RETRY_SAME_DMA;
8690 tw32(TG3PCI_PCISTATE, val);
8691 }
8692
Joe Perches63c3a662011-04-26 08:12:10 +00008693 if (tg3_flag(tp, ENABLE_APE)) {
Matt Carlson0d3031d2007-10-10 18:02:43 -07008694 /* Allow reads and writes to the
8695 * APE register and memory space.
8696 */
8697 val = tr32(TG3PCI_PCISTATE);
8698 val |= PCISTATE_ALLOW_APE_CTLSPC_WR |
Matt Carlsonf92d9dc12010-06-05 17:24:30 +00008699 PCISTATE_ALLOW_APE_SHMEM_WR |
8700 PCISTATE_ALLOW_APE_PSPACE_WR;
Matt Carlson0d3031d2007-10-10 18:02:43 -07008701 tw32(TG3PCI_PCISTATE, val);
8702 }
8703
Linus Torvalds1da177e2005-04-16 15:20:36 -07008704 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5704_BX) {
8705 /* Enable some hw fixes. */
8706 val = tr32(TG3PCI_MSI_DATA);
8707 val |= (1 << 26) | (1 << 28) | (1 << 29);
8708 tw32(TG3PCI_MSI_DATA, val);
8709 }
8710
8711 /* Descriptor ring init may make accesses to the
8712 * NIC SRAM area to setup the TX descriptors, so we
8713 * can only do this after the hardware has been
8714 * successfully reset.
8715 */
Michael Chan32d8c572006-07-25 16:38:29 -07008716 err = tg3_init_rings(tp);
8717 if (err)
8718 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008719
Joe Perches63c3a662011-04-26 08:12:10 +00008720 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlsoncbf9ca62009-11-13 13:03:40 +00008721 val = tr32(TG3PCI_DMA_RW_CTRL) &
8722 ~DMA_RWCTRL_DIS_CACHE_ALIGNMENT;
Matt Carlson1a319022010-04-12 06:58:25 +00008723 if (tp->pci_chip_rev_id == CHIPREV_ID_57765_A0)
8724 val &= ~DMA_RWCTRL_CRDRDR_RDMA_MRRS_MSK;
Matt Carlson55086ad2011-12-14 11:09:59 +00008725 if (!tg3_flag(tp, 57765_CLASS) &&
Matt Carlson0aebff42011-04-25 12:42:45 +00008726 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717)
8727 val |= DMA_RWCTRL_TAGGED_STAT_WA;
Matt Carlsoncbf9ca62009-11-13 13:03:40 +00008728 tw32(TG3PCI_DMA_RW_CTRL, val | tp->dma_rwctrl);
8729 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5784 &&
8730 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5761) {
Matt Carlsond30cdd22007-10-07 23:28:35 -07008731 /* This value is determined during the probe time DMA
8732 * engine test, tg3_test_dma.
8733 */
8734 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
8735 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008736
8737 tp->grc_mode &= ~(GRC_MODE_HOST_SENDBDS |
8738 GRC_MODE_4X_NIC_SEND_RINGS |
8739 GRC_MODE_NO_TX_PHDR_CSUM |
8740 GRC_MODE_NO_RX_PHDR_CSUM);
8741 tp->grc_mode |= GRC_MODE_HOST_SENDBDS;
Michael Chand2d746f2006-04-06 21:45:39 -07008742
8743 /* Pseudo-header checksum is done by hardware logic and not
8744 * the offload processers, so make the chip do the pseudo-
8745 * header checksums on receive. For transmit it is more
8746 * convenient to do the pseudo-header checksum in software
8747 * as Linux does that on transmit for us in all cases.
8748 */
8749 tp->grc_mode |= GRC_MODE_NO_TX_PHDR_CSUM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008750
8751 tw32(GRC_MODE,
8752 tp->grc_mode |
8753 (GRC_MODE_IRQ_ON_MAC_ATTN | GRC_MODE_HOST_STACKUP));
8754
8755 /* Setup the timer prescalar register. Clock is always 66Mhz. */
8756 val = tr32(GRC_MISC_CFG);
8757 val &= ~0xff;
8758 val |= (65 << GRC_MISC_CFG_PRESCALAR_SHIFT);
8759 tw32(GRC_MISC_CFG, val);
8760
8761 /* Initialize MBUF/DESC pool. */
Joe Perches63c3a662011-04-26 08:12:10 +00008762 if (tg3_flag(tp, 5750_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008763 /* Do nothing. */
8764 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5705) {
8765 tw32(BUFMGR_MB_POOL_ADDR, NIC_SRAM_MBUF_POOL_BASE);
8766 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704)
8767 tw32(BUFMGR_MB_POOL_SIZE, NIC_SRAM_MBUF_POOL_SIZE64);
8768 else
8769 tw32(BUFMGR_MB_POOL_SIZE, NIC_SRAM_MBUF_POOL_SIZE96);
8770 tw32(BUFMGR_DMA_DESC_POOL_ADDR, NIC_SRAM_DMA_DESC_POOL_BASE);
8771 tw32(BUFMGR_DMA_DESC_POOL_SIZE, NIC_SRAM_DMA_DESC_POOL_SIZE);
Joe Perches63c3a662011-04-26 08:12:10 +00008772 } else if (tg3_flag(tp, TSO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008773 int fw_len;
8774
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08008775 fw_len = tp->fw_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008776 fw_len = (fw_len + (0x80 - 1)) & ~(0x80 - 1);
8777 tw32(BUFMGR_MB_POOL_ADDR,
8778 NIC_SRAM_MBUF_POOL_BASE5705 + fw_len);
8779 tw32(BUFMGR_MB_POOL_SIZE,
8780 NIC_SRAM_MBUF_POOL_SIZE5705 - fw_len - 0xa00);
8781 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008782
Michael Chan0f893dc2005-07-25 12:30:38 -07008783 if (tp->dev->mtu <= ETH_DATA_LEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008784 tw32(BUFMGR_MB_RDMA_LOW_WATER,
8785 tp->bufmgr_config.mbuf_read_dma_low_water);
8786 tw32(BUFMGR_MB_MACRX_LOW_WATER,
8787 tp->bufmgr_config.mbuf_mac_rx_low_water);
8788 tw32(BUFMGR_MB_HIGH_WATER,
8789 tp->bufmgr_config.mbuf_high_water);
8790 } else {
8791 tw32(BUFMGR_MB_RDMA_LOW_WATER,
8792 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo);
8793 tw32(BUFMGR_MB_MACRX_LOW_WATER,
8794 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo);
8795 tw32(BUFMGR_MB_HIGH_WATER,
8796 tp->bufmgr_config.mbuf_high_water_jumbo);
8797 }
8798 tw32(BUFMGR_DMA_LOW_WATER,
8799 tp->bufmgr_config.dma_low_water);
8800 tw32(BUFMGR_DMA_HIGH_WATER,
8801 tp->bufmgr_config.dma_high_water);
8802
Matt Carlsond309a462010-09-30 10:34:31 +00008803 val = BUFMGR_MODE_ENABLE | BUFMGR_MODE_ATTN_ENABLE;
8804 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
8805 val |= BUFMGR_MODE_NO_TX_UNDERRUN;
Matt Carlson4d958472011-04-20 07:57:35 +00008806 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
8807 tp->pci_chip_rev_id == CHIPREV_ID_5719_A0 ||
8808 tp->pci_chip_rev_id == CHIPREV_ID_5720_A0)
8809 val |= BUFMGR_MODE_MBLOW_ATTN_ENAB;
Matt Carlsond309a462010-09-30 10:34:31 +00008810 tw32(BUFMGR_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008811 for (i = 0; i < 2000; i++) {
8812 if (tr32(BUFMGR_MODE) & BUFMGR_MODE_ENABLE)
8813 break;
8814 udelay(10);
8815 }
8816 if (i >= 2000) {
Joe Perches05dbe002010-02-17 19:44:19 +00008817 netdev_err(tp->dev, "%s cannot enable BUFMGR\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008818 return -ENODEV;
8819 }
8820
Matt Carlsoneb07a942011-04-20 07:57:36 +00008821 if (tp->pci_chip_rev_id == CHIPREV_ID_5906_A1)
8822 tw32(ISO_PKT_TX, (tr32(ISO_PKT_TX) & ~0x3) | 0x2);
Michael Chanb5d37722006-09-27 16:06:21 -07008823
Matt Carlsoneb07a942011-04-20 07:57:36 +00008824 tg3_setup_rxbd_thresholds(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008825
8826 /* Initialize TG3_BDINFO's at:
8827 * RCVDBDI_STD_BD: standard eth size rx ring
8828 * RCVDBDI_JUMBO_BD: jumbo frame rx ring
8829 * RCVDBDI_MINI_BD: small frame rx ring (??? does not work)
8830 *
8831 * like so:
8832 * TG3_BDINFO_HOST_ADDR: high/low parts of DMA address of ring
8833 * TG3_BDINFO_MAXLEN_FLAGS: (rx max buffer size << 16) |
8834 * ring attribute flags
8835 * TG3_BDINFO_NIC_ADDR: location of descriptors in nic SRAM
8836 *
8837 * Standard receive ring @ NIC_SRAM_RX_BUFFER_DESC, 512 entries.
8838 * Jumbo receive ring @ NIC_SRAM_RX_JUMBO_BUFFER_DESC, 256 entries.
8839 *
8840 * The size of each ring is fixed in the firmware, but the location is
8841 * configurable.
8842 */
8843 tw32(RCVDBDI_STD_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH,
Matt Carlson21f581a2009-08-28 14:00:25 +00008844 ((u64) tpr->rx_std_mapping >> 32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07008845 tw32(RCVDBDI_STD_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW,
Matt Carlson21f581a2009-08-28 14:00:25 +00008846 ((u64) tpr->rx_std_mapping & 0xffffffff));
Joe Perches63c3a662011-04-26 08:12:10 +00008847 if (!tg3_flag(tp, 5717_PLUS))
Matt Carlson87668d32009-11-13 13:03:34 +00008848 tw32(RCVDBDI_STD_BD + TG3_BDINFO_NIC_ADDR,
8849 NIC_SRAM_RX_BUFFER_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008850
Matt Carlsonfdb72b32009-08-28 13:57:12 +00008851 /* Disable the mini ring */
Joe Perches63c3a662011-04-26 08:12:10 +00008852 if (!tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008853 tw32(RCVDBDI_MINI_BD + TG3_BDINFO_MAXLEN_FLAGS,
8854 BDINFO_FLAGS_DISABLED);
8855
Matt Carlsonfdb72b32009-08-28 13:57:12 +00008856 /* Program the jumbo buffer descriptor ring control
8857 * blocks on those devices that have them.
8858 */
Matt Carlsona0512942011-07-27 14:20:54 +00008859 if (tp->pci_chip_rev_id == CHIPREV_ID_5719_A0 ||
Joe Perches63c3a662011-04-26 08:12:10 +00008860 (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008861
Joe Perches63c3a662011-04-26 08:12:10 +00008862 if (tg3_flag(tp, JUMBO_RING_ENABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008863 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH,
Matt Carlson21f581a2009-08-28 14:00:25 +00008864 ((u64) tpr->rx_jmb_mapping >> 32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07008865 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW,
Matt Carlson21f581a2009-08-28 14:00:25 +00008866 ((u64) tpr->rx_jmb_mapping & 0xffffffff));
Matt Carlsonde9f5232011-04-05 14:22:43 +00008867 val = TG3_RX_JMB_RING_SIZE(tp) <<
8868 BDINFO_FLAGS_MAXLEN_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008869 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_MAXLEN_FLAGS,
Matt Carlsonde9f5232011-04-05 14:22:43 +00008870 val | BDINFO_FLAGS_USE_EXT_RECV);
Joe Perches63c3a662011-04-26 08:12:10 +00008871 if (!tg3_flag(tp, USE_JUMBO_BDFLAG) ||
Matt Carlson55086ad2011-12-14 11:09:59 +00008872 tg3_flag(tp, 57765_CLASS))
Matt Carlson87668d32009-11-13 13:03:34 +00008873 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_NIC_ADDR,
8874 NIC_SRAM_RX_JUMBO_BUFFER_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008875 } else {
8876 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_MAXLEN_FLAGS,
8877 BDINFO_FLAGS_DISABLED);
8878 }
8879
Joe Perches63c3a662011-04-26 08:12:10 +00008880 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlsonfa6b2aa2011-11-21 15:01:19 +00008881 val = TG3_RX_STD_RING_SIZE(tp);
Matt Carlson7cb32cf2010-09-30 10:34:36 +00008882 val <<= BDINFO_FLAGS_MAXLEN_SHIFT;
8883 val |= (TG3_RX_STD_DMA_SZ << 2);
8884 } else
Matt Carlson04380d42010-04-12 06:58:29 +00008885 val = TG3_RX_STD_DMA_SZ << BDINFO_FLAGS_MAXLEN_SHIFT;
Matt Carlsonfdb72b32009-08-28 13:57:12 +00008886 } else
Matt Carlsonde9f5232011-04-05 14:22:43 +00008887 val = TG3_RX_STD_MAX_SIZE_5700 << BDINFO_FLAGS_MAXLEN_SHIFT;
Matt Carlsonfdb72b32009-08-28 13:57:12 +00008888
8889 tw32(RCVDBDI_STD_BD + TG3_BDINFO_MAXLEN_FLAGS, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008890
Matt Carlson411da642009-11-13 13:03:46 +00008891 tpr->rx_std_prod_idx = tp->rx_pending;
Matt Carlson66711e62009-11-13 13:03:49 +00008892 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG, tpr->rx_std_prod_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008893
Joe Perches63c3a662011-04-26 08:12:10 +00008894 tpr->rx_jmb_prod_idx =
8895 tg3_flag(tp, JUMBO_RING_ENABLE) ? tp->rx_jumbo_pending : 0;
Matt Carlson66711e62009-11-13 13:03:49 +00008896 tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG, tpr->rx_jmb_prod_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008897
Matt Carlson2d31eca2009-09-01 12:53:31 +00008898 tg3_rings_reset(tp);
8899
Linus Torvalds1da177e2005-04-16 15:20:36 -07008900 /* Initialize MAC address and backoff seed. */
Michael Chan986e0ae2007-05-05 12:10:20 -07008901 __tg3_set_mac_addr(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008902
8903 /* MTU + ethernet header + FCS + optional VLAN tag */
Matt Carlsonf7b493e2009-02-25 14:21:52 +00008904 tw32(MAC_RX_MTU_SIZE,
8905 tp->dev->mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008906
8907 /* The slot time is changed by tg3_setup_phy if we
8908 * run at gigabit with half duplex.
8909 */
Matt Carlsonf2096f92011-04-05 14:22:48 +00008910 val = (2 << TX_LENGTHS_IPG_CRS_SHIFT) |
8911 (6 << TX_LENGTHS_IPG_SHIFT) |
8912 (32 << TX_LENGTHS_SLOT_TIME_SHIFT);
8913
8914 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
8915 val |= tr32(MAC_TX_LENGTHS) &
8916 (TX_LENGTHS_JMB_FRM_LEN_MSK |
8917 TX_LENGTHS_CNT_DWN_VAL_MSK);
8918
8919 tw32(MAC_TX_LENGTHS, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008920
8921 /* Receive rules. */
8922 tw32(MAC_RCV_RULE_CFG, RCV_RULE_CFG_DEFAULT_CLASS);
8923 tw32(RCVLPC_CONFIG, 0x0181);
8924
8925 /* Calculate RDMAC_MODE setting early, we need it to determine
8926 * the RCVLPC_STATE_ENABLE mask.
8927 */
8928 rdmac_mode = (RDMAC_MODE_ENABLE | RDMAC_MODE_TGTABORT_ENAB |
8929 RDMAC_MODE_MSTABORT_ENAB | RDMAC_MODE_PARITYERR_ENAB |
8930 RDMAC_MODE_ADDROFLOW_ENAB | RDMAC_MODE_FIFOOFLOW_ENAB |
8931 RDMAC_MODE_FIFOURUN_ENAB | RDMAC_MODE_FIFOOREAD_ENAB |
8932 RDMAC_MODE_LNGREAD_ENAB);
Michael Chan85e94ce2005-04-21 17:05:28 -07008933
Matt Carlsondeabaac2010-11-24 08:31:50 +00008934 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717)
Matt Carlson0339e4e2010-02-12 14:47:09 +00008935 rdmac_mode |= RDMAC_MODE_MULT_DMA_RD_DIS;
8936
Matt Carlson57e69832008-05-25 23:48:31 -07008937 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson321d32a2008-11-21 17:22:19 -08008938 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
8939 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
Matt Carlsond30cdd22007-10-07 23:28:35 -07008940 rdmac_mode |= RDMAC_MODE_BD_SBD_CRPT_ENAB |
8941 RDMAC_MODE_MBUF_RBD_CRPT_ENAB |
8942 RDMAC_MODE_MBUF_SBD_CRPT_ENAB;
8943
Matt Carlsonc5908932011-03-09 16:58:25 +00008944 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
8945 tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) {
Joe Perches63c3a662011-04-26 08:12:10 +00008946 if (tg3_flag(tp, TSO_CAPABLE) &&
Matt Carlsonc13e3712007-05-05 11:50:04 -07008947 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008948 rdmac_mode |= RDMAC_MODE_FIFO_SIZE_128;
8949 } else if (!(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH) &&
Joe Perches63c3a662011-04-26 08:12:10 +00008950 !tg3_flag(tp, IS_5788)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008951 rdmac_mode |= RDMAC_MODE_FIFO_LONG_BURST;
8952 }
8953 }
8954
Joe Perches63c3a662011-04-26 08:12:10 +00008955 if (tg3_flag(tp, PCI_EXPRESS))
Michael Chan85e94ce2005-04-21 17:05:28 -07008956 rdmac_mode |= RDMAC_MODE_FIFO_LONG_BURST;
8957
Joe Perches63c3a662011-04-26 08:12:10 +00008958 if (tg3_flag(tp, HW_TSO_1) ||
8959 tg3_flag(tp, HW_TSO_2) ||
8960 tg3_flag(tp, HW_TSO_3))
Matt Carlson027455a2008-12-21 20:19:30 -08008961 rdmac_mode |= RDMAC_MODE_IPV4_LSO_EN;
8962
Matt Carlson108a6c12011-05-19 12:12:47 +00008963 if (tg3_flag(tp, 57765_PLUS) ||
Matt Carlsone849cdc2009-11-13 13:03:38 +00008964 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Matt Carlson027455a2008-12-21 20:19:30 -08008965 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
8966 rdmac_mode |= RDMAC_MODE_IPV6_LSO_EN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008967
Matt Carlsonf2096f92011-04-05 14:22:48 +00008968 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
8969 rdmac_mode |= tr32(RDMAC_MODE) & RDMAC_MODE_H2BNC_VLAN_DET;
8970
Matt Carlson41a8a7e2010-09-15 08:59:53 +00008971 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
8972 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
8973 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
8974 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
Joe Perches63c3a662011-04-26 08:12:10 +00008975 tg3_flag(tp, 57765_PLUS)) {
Matt Carlson41a8a7e2010-09-15 08:59:53 +00008976 val = tr32(TG3_RDMA_RSRVCTRL_REG);
Matt Carlsond78b59f2011-04-05 14:22:46 +00008977 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
8978 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
Matt Carlsonb4495ed2011-01-25 15:58:47 +00008979 val &= ~(TG3_RDMA_RSRVCTRL_TXMRGN_MASK |
8980 TG3_RDMA_RSRVCTRL_FIFO_LWM_MASK |
8981 TG3_RDMA_RSRVCTRL_FIFO_HWM_MASK);
8982 val |= TG3_RDMA_RSRVCTRL_TXMRGN_320B |
8983 TG3_RDMA_RSRVCTRL_FIFO_LWM_1_5K |
8984 TG3_RDMA_RSRVCTRL_FIFO_HWM_1_5K;
Matt Carlsonb75cc0e2010-11-24 08:31:46 +00008985 }
Matt Carlson41a8a7e2010-09-15 08:59:53 +00008986 tw32(TG3_RDMA_RSRVCTRL_REG,
8987 val | TG3_RDMA_RSRVCTRL_FIFO_OFLW_FIX);
8988 }
8989
Matt Carlsond78b59f2011-04-05 14:22:46 +00008990 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
8991 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
Matt Carlsond309a462010-09-30 10:34:31 +00008992 val = tr32(TG3_LSO_RD_DMA_CRPTEN_CTRL);
8993 tw32(TG3_LSO_RD_DMA_CRPTEN_CTRL, val |
8994 TG3_LSO_RD_DMA_CRPTEN_CTRL_BLEN_BD_4K |
8995 TG3_LSO_RD_DMA_CRPTEN_CTRL_BLEN_LSO_4K);
8996 }
8997
Linus Torvalds1da177e2005-04-16 15:20:36 -07008998 /* Receive/send statistics. */
Joe Perches63c3a662011-04-26 08:12:10 +00008999 if (tg3_flag(tp, 5750_PLUS)) {
Michael Chan16613942006-06-29 20:15:13 -07009000 val = tr32(RCVLPC_STATS_ENABLE);
9001 val &= ~RCVLPC_STATSENAB_DACK_FIX;
9002 tw32(RCVLPC_STATS_ENABLE, val);
9003 } else if ((rdmac_mode & RDMAC_MODE_FIFO_SIZE_128) &&
Joe Perches63c3a662011-04-26 08:12:10 +00009004 tg3_flag(tp, TSO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009005 val = tr32(RCVLPC_STATS_ENABLE);
9006 val &= ~RCVLPC_STATSENAB_LNGBRST_RFIX;
9007 tw32(RCVLPC_STATS_ENABLE, val);
9008 } else {
9009 tw32(RCVLPC_STATS_ENABLE, 0xffffff);
9010 }
9011 tw32(RCVLPC_STATSCTRL, RCVLPC_STATSCTRL_ENABLE);
9012 tw32(SNDDATAI_STATSENAB, 0xffffff);
9013 tw32(SNDDATAI_STATSCTRL,
9014 (SNDDATAI_SCTRL_ENABLE |
9015 SNDDATAI_SCTRL_FASTUPD));
9016
9017 /* Setup host coalescing engine. */
9018 tw32(HOSTCC_MODE, 0);
9019 for (i = 0; i < 2000; i++) {
9020 if (!(tr32(HOSTCC_MODE) & HOSTCC_MODE_ENABLE))
9021 break;
9022 udelay(10);
9023 }
9024
Michael Chand244c892005-07-05 14:42:33 -07009025 __tg3_set_coalesce(tp, &tp->coal);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009026
Joe Perches63c3a662011-04-26 08:12:10 +00009027 if (!tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009028 /* Status/statistics block address. See tg3_timer,
9029 * the tg3_periodic_fetch_stats call there, and
9030 * tg3_get_stats to see how this works for 5705/5750 chips.
9031 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07009032 tw32(HOSTCC_STATS_BLK_HOST_ADDR + TG3_64BIT_REG_HIGH,
9033 ((u64) tp->stats_mapping >> 32));
9034 tw32(HOSTCC_STATS_BLK_HOST_ADDR + TG3_64BIT_REG_LOW,
9035 ((u64) tp->stats_mapping & 0xffffffff));
9036 tw32(HOSTCC_STATS_BLK_NIC_ADDR, NIC_SRAM_STATS_BLK);
Matt Carlson2d31eca2009-09-01 12:53:31 +00009037
Linus Torvalds1da177e2005-04-16 15:20:36 -07009038 tw32(HOSTCC_STATUS_BLK_NIC_ADDR, NIC_SRAM_STATUS_BLK);
Matt Carlson2d31eca2009-09-01 12:53:31 +00009039
9040 /* Clear statistics and status block memory areas */
9041 for (i = NIC_SRAM_STATS_BLK;
9042 i < NIC_SRAM_STATUS_BLK + TG3_HW_STATUS_SIZE;
9043 i += sizeof(u32)) {
9044 tg3_write_mem(tp, i, 0);
9045 udelay(40);
9046 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009047 }
9048
9049 tw32(HOSTCC_MODE, HOSTCC_MODE_ENABLE | tp->coalesce_mode);
9050
9051 tw32(RCVCC_MODE, RCVCC_MODE_ENABLE | RCVCC_MODE_ATTN_ENABLE);
9052 tw32(RCVLPC_MODE, RCVLPC_MODE_ENABLE);
Joe Perches63c3a662011-04-26 08:12:10 +00009053 if (!tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009054 tw32(RCVLSC_MODE, RCVLSC_MODE_ENABLE | RCVLSC_MODE_ATTN_ENABLE);
9055
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009056 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES) {
9057 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chanc94e3942005-09-27 12:12:42 -07009058 /* reset to prevent losing 1st rx packet intermittently */
9059 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
9060 udelay(10);
9061 }
9062
Matt Carlson3bda1252008-08-15 14:08:22 -07009063 tp->mac_mode |= MAC_MODE_TXSTAT_ENABLE | MAC_MODE_RXSTAT_ENABLE |
Matt Carlson9e975cc2011-07-20 10:20:50 +00009064 MAC_MODE_TDE_ENABLE | MAC_MODE_RDE_ENABLE |
9065 MAC_MODE_FHDE_ENABLE;
9066 if (tg3_flag(tp, ENABLE_APE))
9067 tp->mac_mode |= MAC_MODE_APE_TX_EN | MAC_MODE_APE_RX_EN;
Joe Perches63c3a662011-04-26 08:12:10 +00009068 if (!tg3_flag(tp, 5705_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009069 !(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07009070 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700)
9071 tp->mac_mode |= MAC_MODE_LINK_POLARITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009072 tw32_f(MAC_MODE, tp->mac_mode | MAC_MODE_RXSTAT_CLEAR | MAC_MODE_TXSTAT_CLEAR);
9073 udelay(40);
9074
Michael Chan314fba32005-04-21 17:07:04 -07009075 /* tp->grc_local_ctrl is partially set up during tg3_get_invariants().
Joe Perches63c3a662011-04-26 08:12:10 +00009076 * If TG3_FLAG_IS_NIC is zero, we should read the
Michael Chan314fba32005-04-21 17:07:04 -07009077 * register to preserve the GPIO settings for LOMs. The GPIOs,
9078 * whether used as inputs or outputs, are set by boot code after
9079 * reset.
9080 */
Joe Perches63c3a662011-04-26 08:12:10 +00009081 if (!tg3_flag(tp, IS_NIC)) {
Michael Chan314fba32005-04-21 17:07:04 -07009082 u32 gpio_mask;
9083
Michael Chan9d26e212006-12-07 00:21:14 -08009084 gpio_mask = GRC_LCLCTRL_GPIO_OE0 | GRC_LCLCTRL_GPIO_OE1 |
9085 GRC_LCLCTRL_GPIO_OE2 | GRC_LCLCTRL_GPIO_OUTPUT0 |
9086 GRC_LCLCTRL_GPIO_OUTPUT1 | GRC_LCLCTRL_GPIO_OUTPUT2;
Michael Chan3e7d83b2005-04-21 17:10:36 -07009087
9088 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752)
9089 gpio_mask |= GRC_LCLCTRL_GPIO_OE3 |
9090 GRC_LCLCTRL_GPIO_OUTPUT3;
9091
Michael Chanaf36e6b2006-03-23 01:28:06 -08009092 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755)
9093 gpio_mask |= GRC_LCLCTRL_GPIO_UART_SEL;
9094
Gary Zambranoaaf84462007-05-05 11:51:45 -07009095 tp->grc_local_ctrl &= ~gpio_mask;
Michael Chan314fba32005-04-21 17:07:04 -07009096 tp->grc_local_ctrl |= tr32(GRC_LOCAL_CTRL) & gpio_mask;
9097
9098 /* GPIO1 must be driven high for eeprom write protect */
Joe Perches63c3a662011-04-26 08:12:10 +00009099 if (tg3_flag(tp, EEPROM_WRITE_PROT))
Michael Chan9d26e212006-12-07 00:21:14 -08009100 tp->grc_local_ctrl |= (GRC_LCLCTRL_GPIO_OE1 |
9101 GRC_LCLCTRL_GPIO_OUTPUT1);
Michael Chan314fba32005-04-21 17:07:04 -07009102 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009103 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
9104 udelay(100);
9105
Matt Carlsonc3b50032012-01-17 15:27:23 +00009106 if (tg3_flag(tp, USING_MSIX)) {
Matt Carlsonbaf8a942009-09-01 13:13:00 +00009107 val = tr32(MSGINT_MODE);
Matt Carlsonc3b50032012-01-17 15:27:23 +00009108 val |= MSGINT_MODE_ENABLE;
9109 if (tp->irq_cnt > 1)
9110 val |= MSGINT_MODE_MULTIVEC_EN;
Matt Carlson5b39de92011-08-31 11:44:50 +00009111 if (!tg3_flag(tp, 1SHOT_MSI))
9112 val |= MSGINT_MODE_ONE_SHOT_DISABLE;
Matt Carlsonbaf8a942009-09-01 13:13:00 +00009113 tw32(MSGINT_MODE, val);
9114 }
9115
Joe Perches63c3a662011-04-26 08:12:10 +00009116 if (!tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009117 tw32_f(DMAC_MODE, DMAC_MODE_ENABLE);
9118 udelay(40);
9119 }
9120
9121 val = (WDMAC_MODE_ENABLE | WDMAC_MODE_TGTABORT_ENAB |
9122 WDMAC_MODE_MSTABORT_ENAB | WDMAC_MODE_PARITYERR_ENAB |
9123 WDMAC_MODE_ADDROFLOW_ENAB | WDMAC_MODE_FIFOOFLOW_ENAB |
9124 WDMAC_MODE_FIFOURUN_ENAB | WDMAC_MODE_FIFOOREAD_ENAB |
9125 WDMAC_MODE_LNGREAD_ENAB);
9126
Matt Carlsonc5908932011-03-09 16:58:25 +00009127 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
9128 tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) {
Joe Perches63c3a662011-04-26 08:12:10 +00009129 if (tg3_flag(tp, TSO_CAPABLE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07009130 (tp->pci_chip_rev_id == CHIPREV_ID_5705_A1 ||
9131 tp->pci_chip_rev_id == CHIPREV_ID_5705_A2)) {
9132 /* nothing */
9133 } else if (!(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH) &&
Joe Perches63c3a662011-04-26 08:12:10 +00009134 !tg3_flag(tp, IS_5788)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009135 val |= WDMAC_MODE_RX_ACCEL;
9136 }
9137 }
9138
Michael Chand9ab5ad12006-03-20 22:27:35 -08009139 /* Enable host coalescing bug fix */
Joe Perches63c3a662011-04-26 08:12:10 +00009140 if (tg3_flag(tp, 5755_PLUS))
Matt Carlsonf51f3562008-05-25 23:45:08 -07009141 val |= WDMAC_MODE_STATUS_TAG_FIX;
Michael Chand9ab5ad12006-03-20 22:27:35 -08009142
Matt Carlson788a0352009-11-02 14:26:03 +00009143 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
9144 val |= WDMAC_MODE_BURST_ALL_DATA;
9145
Linus Torvalds1da177e2005-04-16 15:20:36 -07009146 tw32_f(WDMAC_MODE, val);
9147 udelay(40);
9148
Joe Perches63c3a662011-04-26 08:12:10 +00009149 if (tg3_flag(tp, PCIX_MODE)) {
Matt Carlson9974a352007-10-07 23:27:28 -07009150 u16 pcix_cmd;
9151
9152 pci_read_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
9153 &pcix_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009154 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703) {
Matt Carlson9974a352007-10-07 23:27:28 -07009155 pcix_cmd &= ~PCI_X_CMD_MAX_READ;
9156 pcix_cmd |= PCI_X_CMD_READ_2K;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009157 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
Matt Carlson9974a352007-10-07 23:27:28 -07009158 pcix_cmd &= ~(PCI_X_CMD_MAX_SPLIT | PCI_X_CMD_MAX_READ);
9159 pcix_cmd |= PCI_X_CMD_READ_2K;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009160 }
Matt Carlson9974a352007-10-07 23:27:28 -07009161 pci_write_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
9162 pcix_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009163 }
9164
9165 tw32_f(RDMAC_MODE, rdmac_mode);
9166 udelay(40);
9167
9168 tw32(RCVDCC_MODE, RCVDCC_MODE_ENABLE | RCVDCC_MODE_ATTN_ENABLE);
Joe Perches63c3a662011-04-26 08:12:10 +00009169 if (!tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009170 tw32(MBFREE_MODE, MBFREE_MODE_ENABLE);
Matt Carlson9936bcf2007-10-10 18:03:07 -07009171
9172 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
9173 tw32(SNDDATAC_MODE,
9174 SNDDATAC_MODE_ENABLE | SNDDATAC_MODE_CDELAY);
9175 else
9176 tw32(SNDDATAC_MODE, SNDDATAC_MODE_ENABLE);
9177
Linus Torvalds1da177e2005-04-16 15:20:36 -07009178 tw32(SNDBDC_MODE, SNDBDC_MODE_ENABLE | SNDBDC_MODE_ATTN_ENABLE);
9179 tw32(RCVBDI_MODE, RCVBDI_MODE_ENABLE | RCVBDI_MODE_RCB_ATTN_ENAB);
Matt Carlson7cb32cf2010-09-30 10:34:36 +00009180 val = RCVDBDI_MODE_ENABLE | RCVDBDI_MODE_INV_RING_SZ;
Joe Perches63c3a662011-04-26 08:12:10 +00009181 if (tg3_flag(tp, LRG_PROD_RING_CAP))
Matt Carlson7cb32cf2010-09-30 10:34:36 +00009182 val |= RCVDBDI_MODE_LRG_RING_SZ;
9183 tw32(RCVDBDI_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009184 tw32(SNDDATAI_MODE, SNDDATAI_MODE_ENABLE);
Joe Perches63c3a662011-04-26 08:12:10 +00009185 if (tg3_flag(tp, HW_TSO_1) ||
9186 tg3_flag(tp, HW_TSO_2) ||
9187 tg3_flag(tp, HW_TSO_3))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009188 tw32(SNDDATAI_MODE, SNDDATAI_MODE_ENABLE | 0x8);
Matt Carlsonbaf8a942009-09-01 13:13:00 +00009189 val = SNDBDI_MODE_ENABLE | SNDBDI_MODE_ATTN_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00009190 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlsonbaf8a942009-09-01 13:13:00 +00009191 val |= SNDBDI_MODE_MULTI_TXQ_EN;
9192 tw32(SNDBDI_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009193 tw32(SNDBDS_MODE, SNDBDS_MODE_ENABLE | SNDBDS_MODE_ATTN_ENABLE);
9194
9195 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0) {
9196 err = tg3_load_5701_a0_firmware_fix(tp);
9197 if (err)
9198 return err;
9199 }
9200
Joe Perches63c3a662011-04-26 08:12:10 +00009201 if (tg3_flag(tp, TSO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009202 err = tg3_load_tso_firmware(tp);
9203 if (err)
9204 return err;
9205 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009206
9207 tp->tx_mode = TX_MODE_ENABLE;
Matt Carlsonf2096f92011-04-05 14:22:48 +00009208
Joe Perches63c3a662011-04-26 08:12:10 +00009209 if (tg3_flag(tp, 5755_PLUS) ||
Matt Carlsonb1d05212010-06-05 17:24:31 +00009210 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
9211 tp->tx_mode |= TX_MODE_MBUF_LOCKUP_FIX;
Matt Carlsonf2096f92011-04-05 14:22:48 +00009212
9213 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
9214 val = TX_MODE_JMB_FRM_LEN | TX_MODE_CNT_DN_MODE;
9215 tp->tx_mode &= ~val;
9216 tp->tx_mode |= tr32(MAC_TX_MODE) & val;
9217 }
9218
Linus Torvalds1da177e2005-04-16 15:20:36 -07009219 tw32_f(MAC_TX_MODE, tp->tx_mode);
9220 udelay(100);
9221
Joe Perches63c3a662011-04-26 08:12:10 +00009222 if (tg3_flag(tp, ENABLE_RSS)) {
Matt Carlsonbcebcc42011-12-14 11:10:01 +00009223 tg3_rss_write_indir_tbl(tp);
Matt Carlsonbaf8a942009-09-01 13:13:00 +00009224
9225 /* Setup the "secret" hash key. */
9226 tw32(MAC_RSS_HASH_KEY_0, 0x5f865437);
9227 tw32(MAC_RSS_HASH_KEY_1, 0xe4ac62cc);
9228 tw32(MAC_RSS_HASH_KEY_2, 0x50103a45);
9229 tw32(MAC_RSS_HASH_KEY_3, 0x36621985);
9230 tw32(MAC_RSS_HASH_KEY_4, 0xbf14c0e8);
9231 tw32(MAC_RSS_HASH_KEY_5, 0x1bc27a1e);
9232 tw32(MAC_RSS_HASH_KEY_6, 0x84f4b556);
9233 tw32(MAC_RSS_HASH_KEY_7, 0x094ea6fe);
9234 tw32(MAC_RSS_HASH_KEY_8, 0x7dda01e7);
9235 tw32(MAC_RSS_HASH_KEY_9, 0xc04d7481);
9236 }
9237
Linus Torvalds1da177e2005-04-16 15:20:36 -07009238 tp->rx_mode = RX_MODE_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00009239 if (tg3_flag(tp, 5755_PLUS))
Michael Chanaf36e6b2006-03-23 01:28:06 -08009240 tp->rx_mode |= RX_MODE_IPV6_CSUM_ENABLE;
9241
Joe Perches63c3a662011-04-26 08:12:10 +00009242 if (tg3_flag(tp, ENABLE_RSS))
Matt Carlsonbaf8a942009-09-01 13:13:00 +00009243 tp->rx_mode |= RX_MODE_RSS_ENABLE |
9244 RX_MODE_RSS_ITBL_HASH_BITS_7 |
9245 RX_MODE_RSS_IPV6_HASH_EN |
9246 RX_MODE_RSS_TCP_IPV6_HASH_EN |
9247 RX_MODE_RSS_IPV4_HASH_EN |
9248 RX_MODE_RSS_TCP_IPV4_HASH_EN;
9249
Linus Torvalds1da177e2005-04-16 15:20:36 -07009250 tw32_f(MAC_RX_MODE, tp->rx_mode);
9251 udelay(10);
9252
Linus Torvalds1da177e2005-04-16 15:20:36 -07009253 tw32(MAC_LED_CTRL, tp->led_ctrl);
9254
9255 tw32(MAC_MI_STAT, MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009256 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009257 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
9258 udelay(10);
9259 }
9260 tw32_f(MAC_RX_MODE, tp->rx_mode);
9261 udelay(10);
9262
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009263 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009264 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009265 !(tp->phy_flags & TG3_PHYFLG_SERDES_PREEMPHASIS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009266 /* Set drive transmission level to 1.2V */
9267 /* only if the signal pre-emphasis bit is not set */
9268 val = tr32(MAC_SERDES_CFG);
9269 val &= 0xfffff000;
9270 val |= 0x880;
9271 tw32(MAC_SERDES_CFG, val);
9272 }
9273 if (tp->pci_chip_rev_id == CHIPREV_ID_5703_A1)
9274 tw32(MAC_SERDES_CFG, 0x616000);
9275 }
9276
9277 /* Prevent chip from dropping frames when flow control
9278 * is enabled.
9279 */
Matt Carlson55086ad2011-12-14 11:09:59 +00009280 if (tg3_flag(tp, 57765_CLASS))
Matt Carlson666bc832010-01-20 16:58:03 +00009281 val = 1;
9282 else
9283 val = 2;
9284 tw32_f(MAC_LOW_WMARK_MAX_RX_FRAME, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009285
9286 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009287 (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009288 /* Use hardware link auto-negotiation */
Joe Perches63c3a662011-04-26 08:12:10 +00009289 tg3_flag_set(tp, HW_AUTONEG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009290 }
9291
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009292 if ((tp->phy_flags & TG3_PHYFLG_MII_SERDES) &&
Matt Carlson6ff6f812011-05-19 12:12:54 +00009293 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
Michael Chand4d2c552006-03-20 17:47:20 -08009294 u32 tmp;
9295
9296 tmp = tr32(SERDES_RX_CTRL);
9297 tw32(SERDES_RX_CTRL, tmp | SERDES_RX_SIG_DETECT);
9298 tp->grc_local_ctrl &= ~GRC_LCLCTRL_USE_EXT_SIG_DETECT;
9299 tp->grc_local_ctrl |= GRC_LCLCTRL_USE_SIG_DETECT;
9300 tw32(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
9301 }
9302
Joe Perches63c3a662011-04-26 08:12:10 +00009303 if (!tg3_flag(tp, USE_PHYLIB)) {
Matt Carlsonc6700ce2012-02-13 15:20:15 +00009304 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Matt Carlson80096062010-08-02 11:26:06 +00009305 tp->phy_flags &= ~TG3_PHYFLG_IS_LOW_POWER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009306
Matt Carlsondd477002008-05-25 23:45:58 -07009307 err = tg3_setup_phy(tp, 0);
9308 if (err)
9309 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009310
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009311 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
9312 !(tp->phy_flags & TG3_PHYFLG_IS_FET)) {
Matt Carlsondd477002008-05-25 23:45:58 -07009313 u32 tmp;
9314
9315 /* Clear CRC stats. */
9316 if (!tg3_readphy(tp, MII_TG3_TEST1, &tmp)) {
9317 tg3_writephy(tp, MII_TG3_TEST1,
9318 tmp | MII_TG3_TEST1_CRC_EN);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00009319 tg3_readphy(tp, MII_TG3_RXR_COUNTERS, &tmp);
Matt Carlsondd477002008-05-25 23:45:58 -07009320 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009321 }
9322 }
9323
9324 __tg3_set_rx_mode(tp->dev);
9325
9326 /* Initialize receive rules. */
9327 tw32(MAC_RCV_RULE_0, 0xc2000000 & RCV_RULE_DISABLE_MASK);
9328 tw32(MAC_RCV_VALUE_0, 0xffffffff & RCV_RULE_DISABLE_MASK);
9329 tw32(MAC_RCV_RULE_1, 0x86000004 & RCV_RULE_DISABLE_MASK);
9330 tw32(MAC_RCV_VALUE_1, 0xffffffff & RCV_RULE_DISABLE_MASK);
9331
Joe Perches63c3a662011-04-26 08:12:10 +00009332 if (tg3_flag(tp, 5705_PLUS) && !tg3_flag(tp, 5780_CLASS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009333 limit = 8;
9334 else
9335 limit = 16;
Joe Perches63c3a662011-04-26 08:12:10 +00009336 if (tg3_flag(tp, ENABLE_ASF))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009337 limit -= 4;
9338 switch (limit) {
9339 case 16:
9340 tw32(MAC_RCV_RULE_15, 0); tw32(MAC_RCV_VALUE_15, 0);
9341 case 15:
9342 tw32(MAC_RCV_RULE_14, 0); tw32(MAC_RCV_VALUE_14, 0);
9343 case 14:
9344 tw32(MAC_RCV_RULE_13, 0); tw32(MAC_RCV_VALUE_13, 0);
9345 case 13:
9346 tw32(MAC_RCV_RULE_12, 0); tw32(MAC_RCV_VALUE_12, 0);
9347 case 12:
9348 tw32(MAC_RCV_RULE_11, 0); tw32(MAC_RCV_VALUE_11, 0);
9349 case 11:
9350 tw32(MAC_RCV_RULE_10, 0); tw32(MAC_RCV_VALUE_10, 0);
9351 case 10:
9352 tw32(MAC_RCV_RULE_9, 0); tw32(MAC_RCV_VALUE_9, 0);
9353 case 9:
9354 tw32(MAC_RCV_RULE_8, 0); tw32(MAC_RCV_VALUE_8, 0);
9355 case 8:
9356 tw32(MAC_RCV_RULE_7, 0); tw32(MAC_RCV_VALUE_7, 0);
9357 case 7:
9358 tw32(MAC_RCV_RULE_6, 0); tw32(MAC_RCV_VALUE_6, 0);
9359 case 6:
9360 tw32(MAC_RCV_RULE_5, 0); tw32(MAC_RCV_VALUE_5, 0);
9361 case 5:
9362 tw32(MAC_RCV_RULE_4, 0); tw32(MAC_RCV_VALUE_4, 0);
9363 case 4:
9364 /* tw32(MAC_RCV_RULE_3, 0); tw32(MAC_RCV_VALUE_3, 0); */
9365 case 3:
9366 /* tw32(MAC_RCV_RULE_2, 0); tw32(MAC_RCV_VALUE_2, 0); */
9367 case 2:
9368 case 1:
9369
9370 default:
9371 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07009372 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009373
Joe Perches63c3a662011-04-26 08:12:10 +00009374 if (tg3_flag(tp, ENABLE_APE))
Matt Carlson9ce768e2007-10-11 19:49:11 -07009375 /* Write our heartbeat update interval to APE. */
9376 tg3_ape_write32(tp, TG3_APE_HOST_HEARTBEAT_INT_MS,
9377 APE_HOST_HEARTBEAT_INT_DISABLE);
Matt Carlson0d3031d2007-10-10 18:02:43 -07009378
Linus Torvalds1da177e2005-04-16 15:20:36 -07009379 tg3_write_sig_post_reset(tp, RESET_KIND_INIT);
9380
Linus Torvalds1da177e2005-04-16 15:20:36 -07009381 return 0;
9382}
9383
9384/* Called at device open time to get the chip ready for
9385 * packet processing. Invoked with tp->lock held.
9386 */
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07009387static int tg3_init_hw(struct tg3 *tp, int reset_phy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009388{
Linus Torvalds1da177e2005-04-16 15:20:36 -07009389 tg3_switch_clocks(tp);
9390
9391 tw32(TG3PCI_MEM_WIN_BASE_ADDR, 0);
9392
Matt Carlson2f751b62008-08-04 23:17:34 -07009393 return tg3_reset_hw(tp, reset_phy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009394}
9395
9396#define TG3_STAT_ADD32(PSTAT, REG) \
9397do { u32 __val = tr32(REG); \
9398 (PSTAT)->low += __val; \
9399 if ((PSTAT)->low < __val) \
9400 (PSTAT)->high += 1; \
9401} while (0)
9402
9403static void tg3_periodic_fetch_stats(struct tg3 *tp)
9404{
9405 struct tg3_hw_stats *sp = tp->hw_stats;
9406
9407 if (!netif_carrier_ok(tp->dev))
9408 return;
9409
9410 TG3_STAT_ADD32(&sp->tx_octets, MAC_TX_STATS_OCTETS);
9411 TG3_STAT_ADD32(&sp->tx_collisions, MAC_TX_STATS_COLLISIONS);
9412 TG3_STAT_ADD32(&sp->tx_xon_sent, MAC_TX_STATS_XON_SENT);
9413 TG3_STAT_ADD32(&sp->tx_xoff_sent, MAC_TX_STATS_XOFF_SENT);
9414 TG3_STAT_ADD32(&sp->tx_mac_errors, MAC_TX_STATS_MAC_ERRORS);
9415 TG3_STAT_ADD32(&sp->tx_single_collisions, MAC_TX_STATS_SINGLE_COLLISIONS);
9416 TG3_STAT_ADD32(&sp->tx_mult_collisions, MAC_TX_STATS_MULT_COLLISIONS);
9417 TG3_STAT_ADD32(&sp->tx_deferred, MAC_TX_STATS_DEFERRED);
9418 TG3_STAT_ADD32(&sp->tx_excessive_collisions, MAC_TX_STATS_EXCESSIVE_COL);
9419 TG3_STAT_ADD32(&sp->tx_late_collisions, MAC_TX_STATS_LATE_COL);
9420 TG3_STAT_ADD32(&sp->tx_ucast_packets, MAC_TX_STATS_UCAST);
9421 TG3_STAT_ADD32(&sp->tx_mcast_packets, MAC_TX_STATS_MCAST);
9422 TG3_STAT_ADD32(&sp->tx_bcast_packets, MAC_TX_STATS_BCAST);
9423
9424 TG3_STAT_ADD32(&sp->rx_octets, MAC_RX_STATS_OCTETS);
9425 TG3_STAT_ADD32(&sp->rx_fragments, MAC_RX_STATS_FRAGMENTS);
9426 TG3_STAT_ADD32(&sp->rx_ucast_packets, MAC_RX_STATS_UCAST);
9427 TG3_STAT_ADD32(&sp->rx_mcast_packets, MAC_RX_STATS_MCAST);
9428 TG3_STAT_ADD32(&sp->rx_bcast_packets, MAC_RX_STATS_BCAST);
9429 TG3_STAT_ADD32(&sp->rx_fcs_errors, MAC_RX_STATS_FCS_ERRORS);
9430 TG3_STAT_ADD32(&sp->rx_align_errors, MAC_RX_STATS_ALIGN_ERRORS);
9431 TG3_STAT_ADD32(&sp->rx_xon_pause_rcvd, MAC_RX_STATS_XON_PAUSE_RECVD);
9432 TG3_STAT_ADD32(&sp->rx_xoff_pause_rcvd, MAC_RX_STATS_XOFF_PAUSE_RECVD);
9433 TG3_STAT_ADD32(&sp->rx_mac_ctrl_rcvd, MAC_RX_STATS_MAC_CTRL_RECVD);
9434 TG3_STAT_ADD32(&sp->rx_xoff_entered, MAC_RX_STATS_XOFF_ENTERED);
9435 TG3_STAT_ADD32(&sp->rx_frame_too_long_errors, MAC_RX_STATS_FRAME_TOO_LONG);
9436 TG3_STAT_ADD32(&sp->rx_jabbers, MAC_RX_STATS_JABBERS);
9437 TG3_STAT_ADD32(&sp->rx_undersize_packets, MAC_RX_STATS_UNDERSIZE);
Michael Chan463d3052006-05-22 16:36:27 -07009438
9439 TG3_STAT_ADD32(&sp->rxbds_empty, RCVLPC_NO_RCV_BD_CNT);
Matt Carlson310050f2011-05-19 12:12:55 +00009440 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717 &&
9441 tp->pci_chip_rev_id != CHIPREV_ID_5719_A0 &&
9442 tp->pci_chip_rev_id != CHIPREV_ID_5720_A0) {
Matt Carlson4d958472011-04-20 07:57:35 +00009443 TG3_STAT_ADD32(&sp->rx_discards, RCVLPC_IN_DISCARDS_CNT);
9444 } else {
9445 u32 val = tr32(HOSTCC_FLOW_ATTN);
9446 val = (val & HOSTCC_FLOW_ATTN_MBUF_LWM) ? 1 : 0;
9447 if (val) {
9448 tw32(HOSTCC_FLOW_ATTN, HOSTCC_FLOW_ATTN_MBUF_LWM);
9449 sp->rx_discards.low += val;
9450 if (sp->rx_discards.low < val)
9451 sp->rx_discards.high += 1;
9452 }
9453 sp->mbuf_lwm_thresh_hit = sp->rx_discards;
9454 }
Michael Chan463d3052006-05-22 16:36:27 -07009455 TG3_STAT_ADD32(&sp->rx_errors, RCVLPC_IN_ERRORS_CNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009456}
9457
Matt Carlson0e6cf6a2011-06-13 13:38:55 +00009458static void tg3_chk_missed_msi(struct tg3 *tp)
9459{
9460 u32 i;
9461
9462 for (i = 0; i < tp->irq_cnt; i++) {
9463 struct tg3_napi *tnapi = &tp->napi[i];
9464
9465 if (tg3_has_work(tnapi)) {
9466 if (tnapi->last_rx_cons == tnapi->rx_rcb_ptr &&
9467 tnapi->last_tx_cons == tnapi->tx_cons) {
9468 if (tnapi->chk_msi_cnt < 1) {
9469 tnapi->chk_msi_cnt++;
9470 return;
9471 }
Matt Carlson7f230732011-08-31 11:44:48 +00009472 tg3_msi(0, tnapi);
Matt Carlson0e6cf6a2011-06-13 13:38:55 +00009473 }
9474 }
9475 tnapi->chk_msi_cnt = 0;
9476 tnapi->last_rx_cons = tnapi->rx_rcb_ptr;
9477 tnapi->last_tx_cons = tnapi->tx_cons;
9478 }
9479}
9480
Linus Torvalds1da177e2005-04-16 15:20:36 -07009481static void tg3_timer(unsigned long __opaque)
9482{
9483 struct tg3 *tp = (struct tg3 *) __opaque;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009484
Matt Carlson5b190622011-11-04 09:15:04 +00009485 if (tp->irq_sync || tg3_flag(tp, RESET_TASK_PENDING))
Michael Chanf475f162006-03-27 23:20:14 -08009486 goto restart_timer;
9487
David S. Millerf47c11e2005-06-24 20:18:35 -07009488 spin_lock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009489
Matt Carlson0e6cf6a2011-06-13 13:38:55 +00009490 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
Matt Carlson55086ad2011-12-14 11:09:59 +00009491 tg3_flag(tp, 57765_CLASS))
Matt Carlson0e6cf6a2011-06-13 13:38:55 +00009492 tg3_chk_missed_msi(tp);
9493
Joe Perches63c3a662011-04-26 08:12:10 +00009494 if (!tg3_flag(tp, TAGGED_STATUS)) {
David S. Millerfac9b832005-05-18 22:46:34 -07009495 /* All of this garbage is because when using non-tagged
9496 * IRQ status the mailbox/status_block protocol the chip
9497 * uses with the cpu is race prone.
9498 */
Matt Carlson898a56f2009-08-28 14:02:40 +00009499 if (tp->napi[0].hw_status->status & SD_STATUS_UPDATED) {
David S. Millerfac9b832005-05-18 22:46:34 -07009500 tw32(GRC_LOCAL_CTRL,
9501 tp->grc_local_ctrl | GRC_LCLCTRL_SETINT);
9502 } else {
9503 tw32(HOSTCC_MODE, tp->coalesce_mode |
Matt Carlsonfd2ce372009-09-01 12:51:13 +00009504 HOSTCC_MODE_ENABLE | HOSTCC_MODE_NOW);
David S. Millerfac9b832005-05-18 22:46:34 -07009505 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009506
David S. Millerfac9b832005-05-18 22:46:34 -07009507 if (!(tr32(WDMAC_MODE) & WDMAC_MODE_ENABLE)) {
David S. Millerf47c11e2005-06-24 20:18:35 -07009508 spin_unlock(&tp->lock);
Matt Carlsondb219972011-11-04 09:15:03 +00009509 tg3_reset_task_schedule(tp);
Matt Carlson5b190622011-11-04 09:15:04 +00009510 goto restart_timer;
David S. Millerfac9b832005-05-18 22:46:34 -07009511 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009512 }
9513
Linus Torvalds1da177e2005-04-16 15:20:36 -07009514 /* This part only runs once per second. */
9515 if (!--tp->timer_counter) {
Joe Perches63c3a662011-04-26 08:12:10 +00009516 if (tg3_flag(tp, 5705_PLUS))
David S. Millerfac9b832005-05-18 22:46:34 -07009517 tg3_periodic_fetch_stats(tp);
9518
Matt Carlsonb0c59432011-05-19 12:12:48 +00009519 if (tp->setlpicnt && !--tp->setlpicnt)
9520 tg3_phy_eee_enable(tp);
Matt Carlson52b02d02010-10-14 10:37:41 +00009521
Joe Perches63c3a662011-04-26 08:12:10 +00009522 if (tg3_flag(tp, USE_LINKCHG_REG)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009523 u32 mac_stat;
9524 int phy_event;
9525
9526 mac_stat = tr32(MAC_STATUS);
9527
9528 phy_event = 0;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009529 if (tp->phy_flags & TG3_PHYFLG_USE_MI_INTERRUPT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009530 if (mac_stat & MAC_STATUS_MI_INTERRUPT)
9531 phy_event = 1;
9532 } else if (mac_stat & MAC_STATUS_LNKSTATE_CHANGED)
9533 phy_event = 1;
9534
9535 if (phy_event)
9536 tg3_setup_phy(tp, 0);
Joe Perches63c3a662011-04-26 08:12:10 +00009537 } else if (tg3_flag(tp, POLL_SERDES)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009538 u32 mac_stat = tr32(MAC_STATUS);
9539 int need_setup = 0;
9540
9541 if (netif_carrier_ok(tp->dev) &&
9542 (mac_stat & MAC_STATUS_LNKSTATE_CHANGED)) {
9543 need_setup = 1;
9544 }
Matt Carlsonbe98da62010-07-11 09:31:46 +00009545 if (!netif_carrier_ok(tp->dev) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07009546 (mac_stat & (MAC_STATUS_PCS_SYNCED |
9547 MAC_STATUS_SIGNAL_DET))) {
9548 need_setup = 1;
9549 }
9550 if (need_setup) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07009551 if (!tp->serdes_counter) {
9552 tw32_f(MAC_MODE,
9553 (tp->mac_mode &
9554 ~MAC_MODE_PORT_MODE_MASK));
9555 udelay(40);
9556 tw32_f(MAC_MODE, tp->mac_mode);
9557 udelay(40);
9558 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009559 tg3_setup_phy(tp, 0);
9560 }
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009561 } else if ((tp->phy_flags & TG3_PHYFLG_MII_SERDES) &&
Joe Perches63c3a662011-04-26 08:12:10 +00009562 tg3_flag(tp, 5780_CLASS)) {
Michael Chan747e8f82005-07-25 12:33:22 -07009563 tg3_serdes_parallel_detect(tp);
Matt Carlson57d8b882010-06-05 17:24:35 +00009564 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009565
9566 tp->timer_counter = tp->timer_multiplier;
9567 }
9568
Michael Chan130b8e42006-09-27 16:00:40 -07009569 /* Heartbeat is only sent once every 2 seconds.
9570 *
9571 * The heartbeat is to tell the ASF firmware that the host
9572 * driver is still alive. In the event that the OS crashes,
9573 * ASF needs to reset the hardware to free up the FIFO space
9574 * that may be filled with rx packets destined for the host.
9575 * If the FIFO is full, ASF will no longer function properly.
9576 *
9577 * Unintended resets have been reported on real time kernels
9578 * where the timer doesn't run on time. Netpoll will also have
9579 * same problem.
9580 *
9581 * The new FWCMD_NICDRV_ALIVE3 command tells the ASF firmware
9582 * to check the ring condition when the heartbeat is expiring
9583 * before doing the reset. This will prevent most unintended
9584 * resets.
9585 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07009586 if (!--tp->asf_counter) {
Joe Perches63c3a662011-04-26 08:12:10 +00009587 if (tg3_flag(tp, ENABLE_ASF) && !tg3_flag(tp, ENABLE_APE)) {
Matt Carlson7c5026a2008-05-02 16:49:29 -07009588 tg3_wait_for_event_ack(tp);
9589
Michael Chanbbadf502006-04-06 21:46:34 -07009590 tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX,
Michael Chan130b8e42006-09-27 16:00:40 -07009591 FWCMD_NICDRV_ALIVE3);
Michael Chanbbadf502006-04-06 21:46:34 -07009592 tg3_write_mem(tp, NIC_SRAM_FW_CMD_LEN_MBOX, 4);
Matt Carlsonc6cdf432010-04-05 10:19:26 +00009593 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX,
9594 TG3_FW_UPDATE_TIMEOUT_SEC);
Matt Carlson4ba526c2008-08-15 14:10:04 -07009595
9596 tg3_generate_fw_event(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009597 }
9598 tp->asf_counter = tp->asf_multiplier;
9599 }
9600
David S. Millerf47c11e2005-06-24 20:18:35 -07009601 spin_unlock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009602
Michael Chanf475f162006-03-27 23:20:14 -08009603restart_timer:
Linus Torvalds1da177e2005-04-16 15:20:36 -07009604 tp->timer.expires = jiffies + tp->timer_offset;
9605 add_timer(&tp->timer);
9606}
9607
Matt Carlson21f76382012-02-22 12:35:21 +00009608static void __devinit tg3_timer_init(struct tg3 *tp)
9609{
9610 if (tg3_flag(tp, TAGGED_STATUS) &&
9611 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717 &&
9612 !tg3_flag(tp, 57765_CLASS))
9613 tp->timer_offset = HZ;
9614 else
9615 tp->timer_offset = HZ / 10;
9616
9617 BUG_ON(tp->timer_offset > HZ);
9618
9619 tp->timer_multiplier = (HZ / tp->timer_offset);
9620 tp->asf_multiplier = (HZ / tp->timer_offset) *
9621 TG3_FW_UPDATE_FREQ_SEC;
9622
9623 init_timer(&tp->timer);
9624 tp->timer.data = (unsigned long) tp;
9625 tp->timer.function = tg3_timer;
9626}
9627
9628static void tg3_timer_start(struct tg3 *tp)
9629{
9630 tp->asf_counter = tp->asf_multiplier;
9631 tp->timer_counter = tp->timer_multiplier;
9632
9633 tp->timer.expires = jiffies + tp->timer_offset;
9634 add_timer(&tp->timer);
9635}
9636
9637static void tg3_timer_stop(struct tg3 *tp)
9638{
9639 del_timer_sync(&tp->timer);
9640}
9641
9642/* Restart hardware after configuration changes, self-test, etc.
9643 * Invoked with tp->lock held.
9644 */
9645static int tg3_restart_hw(struct tg3 *tp, int reset_phy)
9646 __releases(tp->lock)
9647 __acquires(tp->lock)
9648{
9649 int err;
9650
9651 err = tg3_init_hw(tp, reset_phy);
9652 if (err) {
9653 netdev_err(tp->dev,
9654 "Failed to re-initialize device, aborting\n");
9655 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
9656 tg3_full_unlock(tp);
9657 tg3_timer_stop(tp);
9658 tp->irq_sync = 0;
9659 tg3_napi_enable(tp);
9660 dev_close(tp->dev);
9661 tg3_full_lock(tp, 0);
9662 }
9663 return err;
9664}
9665
9666static void tg3_reset_task(struct work_struct *work)
9667{
9668 struct tg3 *tp = container_of(work, struct tg3, reset_task);
9669 int err;
9670
9671 tg3_full_lock(tp, 0);
9672
9673 if (!netif_running(tp->dev)) {
9674 tg3_flag_clear(tp, RESET_TASK_PENDING);
9675 tg3_full_unlock(tp);
9676 return;
9677 }
9678
9679 tg3_full_unlock(tp);
9680
9681 tg3_phy_stop(tp);
9682
9683 tg3_netif_stop(tp);
9684
9685 tg3_full_lock(tp, 1);
9686
9687 if (tg3_flag(tp, TX_RECOVERY_PENDING)) {
9688 tp->write32_tx_mbox = tg3_write32_tx_mbox;
9689 tp->write32_rx_mbox = tg3_write_flush_reg32;
9690 tg3_flag_set(tp, MBOX_WRITE_REORDER);
9691 tg3_flag_clear(tp, TX_RECOVERY_PENDING);
9692 }
9693
9694 tg3_halt(tp, RESET_KIND_SHUTDOWN, 0);
9695 err = tg3_init_hw(tp, 1);
9696 if (err)
9697 goto out;
9698
9699 tg3_netif_start(tp);
9700
9701out:
9702 tg3_full_unlock(tp);
9703
9704 if (!err)
9705 tg3_phy_start(tp);
9706
9707 tg3_flag_clear(tp, RESET_TASK_PENDING);
9708}
9709
Matt Carlson4f125f42009-09-01 12:55:02 +00009710static int tg3_request_irq(struct tg3 *tp, int irq_num)
Michael Chanfcfa0a32006-03-20 22:28:41 -08009711{
David Howells7d12e782006-10-05 14:55:46 +01009712 irq_handler_t fn;
Michael Chanfcfa0a32006-03-20 22:28:41 -08009713 unsigned long flags;
Matt Carlson4f125f42009-09-01 12:55:02 +00009714 char *name;
9715 struct tg3_napi *tnapi = &tp->napi[irq_num];
9716
9717 if (tp->irq_cnt == 1)
9718 name = tp->dev->name;
9719 else {
9720 name = &tnapi->irq_lbl[0];
9721 snprintf(name, IFNAMSIZ, "%s-%d", tp->dev->name, irq_num);
9722 name[IFNAMSIZ-1] = 0;
9723 }
Michael Chanfcfa0a32006-03-20 22:28:41 -08009724
Joe Perches63c3a662011-04-26 08:12:10 +00009725 if (tg3_flag(tp, USING_MSI) || tg3_flag(tp, USING_MSIX)) {
Michael Chanfcfa0a32006-03-20 22:28:41 -08009726 fn = tg3_msi;
Joe Perches63c3a662011-04-26 08:12:10 +00009727 if (tg3_flag(tp, 1SHOT_MSI))
Michael Chanfcfa0a32006-03-20 22:28:41 -08009728 fn = tg3_msi_1shot;
Javier Martinez Canillasab392d22011-03-28 16:27:31 +00009729 flags = 0;
Michael Chanfcfa0a32006-03-20 22:28:41 -08009730 } else {
9731 fn = tg3_interrupt;
Joe Perches63c3a662011-04-26 08:12:10 +00009732 if (tg3_flag(tp, TAGGED_STATUS))
Michael Chanfcfa0a32006-03-20 22:28:41 -08009733 fn = tg3_interrupt_tagged;
Javier Martinez Canillasab392d22011-03-28 16:27:31 +00009734 flags = IRQF_SHARED;
Michael Chanfcfa0a32006-03-20 22:28:41 -08009735 }
Matt Carlson4f125f42009-09-01 12:55:02 +00009736
9737 return request_irq(tnapi->irq_vec, fn, flags, name, tnapi);
Michael Chanfcfa0a32006-03-20 22:28:41 -08009738}
9739
Michael Chan79381092005-04-21 17:13:59 -07009740static int tg3_test_interrupt(struct tg3 *tp)
9741{
Matt Carlson09943a12009-08-28 14:01:57 +00009742 struct tg3_napi *tnapi = &tp->napi[0];
Michael Chan79381092005-04-21 17:13:59 -07009743 struct net_device *dev = tp->dev;
Michael Chanb16250e2006-09-27 16:10:14 -07009744 int err, i, intr_ok = 0;
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009745 u32 val;
Michael Chan79381092005-04-21 17:13:59 -07009746
Michael Chand4bc3922005-05-29 14:59:20 -07009747 if (!netif_running(dev))
9748 return -ENODEV;
9749
Michael Chan79381092005-04-21 17:13:59 -07009750 tg3_disable_ints(tp);
9751
Matt Carlson4f125f42009-09-01 12:55:02 +00009752 free_irq(tnapi->irq_vec, tnapi);
Michael Chan79381092005-04-21 17:13:59 -07009753
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009754 /*
9755 * Turn off MSI one shot mode. Otherwise this test has no
9756 * observable way to know whether the interrupt was delivered.
9757 */
Matt Carlson3aa1cdf2011-07-20 10:20:55 +00009758 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009759 val = tr32(MSGINT_MODE) | MSGINT_MODE_ONE_SHOT_DISABLE;
9760 tw32(MSGINT_MODE, val);
9761 }
9762
Matt Carlson4f125f42009-09-01 12:55:02 +00009763 err = request_irq(tnapi->irq_vec, tg3_test_isr,
Davidlohr Buesof274fd92012-02-22 03:06:54 +00009764 IRQF_SHARED, dev->name, tnapi);
Michael Chan79381092005-04-21 17:13:59 -07009765 if (err)
9766 return err;
9767
Matt Carlson898a56f2009-08-28 14:02:40 +00009768 tnapi->hw_status->status &= ~SD_STATUS_UPDATED;
Michael Chan79381092005-04-21 17:13:59 -07009769 tg3_enable_ints(tp);
9770
9771 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +00009772 tnapi->coal_now);
Michael Chan79381092005-04-21 17:13:59 -07009773
9774 for (i = 0; i < 5; i++) {
Michael Chanb16250e2006-09-27 16:10:14 -07009775 u32 int_mbox, misc_host_ctrl;
9776
Matt Carlson898a56f2009-08-28 14:02:40 +00009777 int_mbox = tr32_mailbox(tnapi->int_mbox);
Michael Chanb16250e2006-09-27 16:10:14 -07009778 misc_host_ctrl = tr32(TG3PCI_MISC_HOST_CTRL);
9779
9780 if ((int_mbox != 0) ||
9781 (misc_host_ctrl & MISC_HOST_CTRL_MASK_PCI_INT)) {
9782 intr_ok = 1;
Michael Chan79381092005-04-21 17:13:59 -07009783 break;
Michael Chanb16250e2006-09-27 16:10:14 -07009784 }
9785
Matt Carlson3aa1cdf2011-07-20 10:20:55 +00009786 if (tg3_flag(tp, 57765_PLUS) &&
9787 tnapi->hw_status->status_tag != tnapi->last_tag)
9788 tw32_mailbox_f(tnapi->int_mbox, tnapi->last_tag << 24);
9789
Michael Chan79381092005-04-21 17:13:59 -07009790 msleep(10);
9791 }
9792
9793 tg3_disable_ints(tp);
9794
Matt Carlson4f125f42009-09-01 12:55:02 +00009795 free_irq(tnapi->irq_vec, tnapi);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009796
Matt Carlson4f125f42009-09-01 12:55:02 +00009797 err = tg3_request_irq(tp, 0);
Michael Chan79381092005-04-21 17:13:59 -07009798
9799 if (err)
9800 return err;
9801
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009802 if (intr_ok) {
9803 /* Reenable MSI one shot mode. */
Matt Carlson5b39de92011-08-31 11:44:50 +00009804 if (tg3_flag(tp, 57765_PLUS) && tg3_flag(tp, 1SHOT_MSI)) {
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009805 val = tr32(MSGINT_MODE) & ~MSGINT_MODE_ONE_SHOT_DISABLE;
9806 tw32(MSGINT_MODE, val);
9807 }
Michael Chan79381092005-04-21 17:13:59 -07009808 return 0;
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009809 }
Michael Chan79381092005-04-21 17:13:59 -07009810
9811 return -EIO;
9812}
9813
9814/* Returns 0 if MSI test succeeds or MSI test fails and INTx mode is
9815 * successfully restored
9816 */
9817static int tg3_test_msi(struct tg3 *tp)
9818{
Michael Chan79381092005-04-21 17:13:59 -07009819 int err;
9820 u16 pci_cmd;
9821
Joe Perches63c3a662011-04-26 08:12:10 +00009822 if (!tg3_flag(tp, USING_MSI))
Michael Chan79381092005-04-21 17:13:59 -07009823 return 0;
9824
9825 /* Turn off SERR reporting in case MSI terminates with Master
9826 * Abort.
9827 */
9828 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
9829 pci_write_config_word(tp->pdev, PCI_COMMAND,
9830 pci_cmd & ~PCI_COMMAND_SERR);
9831
9832 err = tg3_test_interrupt(tp);
9833
9834 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
9835
9836 if (!err)
9837 return 0;
9838
9839 /* other failures */
9840 if (err != -EIO)
9841 return err;
9842
9843 /* MSI test failed, go back to INTx mode */
Matt Carlson5129c3a2010-04-05 10:19:23 +00009844 netdev_warn(tp->dev, "No interrupt was generated using MSI. Switching "
9845 "to INTx mode. Please report this failure to the PCI "
9846 "maintainer and include system chipset information\n");
Michael Chan79381092005-04-21 17:13:59 -07009847
Matt Carlson4f125f42009-09-01 12:55:02 +00009848 free_irq(tp->napi[0].irq_vec, &tp->napi[0]);
Matt Carlson09943a12009-08-28 14:01:57 +00009849
Michael Chan79381092005-04-21 17:13:59 -07009850 pci_disable_msi(tp->pdev);
9851
Joe Perches63c3a662011-04-26 08:12:10 +00009852 tg3_flag_clear(tp, USING_MSI);
Andre Detschdc8bf1b2010-04-26 07:27:07 +00009853 tp->napi[0].irq_vec = tp->pdev->irq;
Michael Chan79381092005-04-21 17:13:59 -07009854
Matt Carlson4f125f42009-09-01 12:55:02 +00009855 err = tg3_request_irq(tp, 0);
Michael Chan79381092005-04-21 17:13:59 -07009856 if (err)
9857 return err;
9858
9859 /* Need to reset the chip because the MSI cycle may have terminated
9860 * with Master Abort.
9861 */
David S. Millerf47c11e2005-06-24 20:18:35 -07009862 tg3_full_lock(tp, 1);
Michael Chan79381092005-04-21 17:13:59 -07009863
Michael Chan944d9802005-05-29 14:57:48 -07009864 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07009865 err = tg3_init_hw(tp, 1);
Michael Chan79381092005-04-21 17:13:59 -07009866
David S. Millerf47c11e2005-06-24 20:18:35 -07009867 tg3_full_unlock(tp);
Michael Chan79381092005-04-21 17:13:59 -07009868
9869 if (err)
Matt Carlson4f125f42009-09-01 12:55:02 +00009870 free_irq(tp->napi[0].irq_vec, &tp->napi[0]);
Michael Chan79381092005-04-21 17:13:59 -07009871
9872 return err;
9873}
9874
Matt Carlson9e9fd122009-01-19 16:57:45 -08009875static int tg3_request_firmware(struct tg3 *tp)
9876{
9877 const __be32 *fw_data;
9878
9879 if (request_firmware(&tp->fw, tp->fw_needed, &tp->pdev->dev)) {
Joe Perches05dbe002010-02-17 19:44:19 +00009880 netdev_err(tp->dev, "Failed to load firmware \"%s\"\n",
9881 tp->fw_needed);
Matt Carlson9e9fd122009-01-19 16:57:45 -08009882 return -ENOENT;
9883 }
9884
9885 fw_data = (void *)tp->fw->data;
9886
9887 /* Firmware blob starts with version numbers, followed by
9888 * start address and _full_ length including BSS sections
9889 * (which must be longer than the actual data, of course
9890 */
9891
9892 tp->fw_len = be32_to_cpu(fw_data[2]); /* includes bss */
9893 if (tp->fw_len < (tp->fw->size - 12)) {
Joe Perches05dbe002010-02-17 19:44:19 +00009894 netdev_err(tp->dev, "bogus length %d in \"%s\"\n",
9895 tp->fw_len, tp->fw_needed);
Matt Carlson9e9fd122009-01-19 16:57:45 -08009896 release_firmware(tp->fw);
9897 tp->fw = NULL;
9898 return -EINVAL;
9899 }
9900
9901 /* We no longer need firmware; we have it. */
9902 tp->fw_needed = NULL;
9903 return 0;
9904}
9905
Matt Carlson679563f2009-09-01 12:55:46 +00009906static bool tg3_enable_msix(struct tg3 *tp)
9907{
Matt Carlsonc3b50032012-01-17 15:27:23 +00009908 int i, rc;
Matt Carlson679563f2009-09-01 12:55:46 +00009909 struct msix_entry msix_ent[tp->irq_max];
9910
Matt Carlsonc3b50032012-01-17 15:27:23 +00009911 tp->irq_cnt = num_online_cpus();
9912 if (tp->irq_cnt > 1) {
9913 /* We want as many rx rings enabled as there are cpus.
9914 * In multiqueue MSI-X mode, the first MSI-X vector
9915 * only deals with link interrupts, etc, so we add
9916 * one to the number of vectors we are requesting.
9917 */
9918 tp->irq_cnt = min_t(unsigned, tp->irq_cnt + 1, tp->irq_max);
9919 }
Matt Carlson679563f2009-09-01 12:55:46 +00009920
9921 for (i = 0; i < tp->irq_max; i++) {
9922 msix_ent[i].entry = i;
9923 msix_ent[i].vector = 0;
9924 }
9925
9926 rc = pci_enable_msix(tp->pdev, msix_ent, tp->irq_cnt);
Matt Carlson2430b032010-06-05 17:24:34 +00009927 if (rc < 0) {
9928 return false;
9929 } else if (rc != 0) {
Matt Carlson679563f2009-09-01 12:55:46 +00009930 if (pci_enable_msix(tp->pdev, msix_ent, rc))
9931 return false;
Joe Perches05dbe002010-02-17 19:44:19 +00009932 netdev_notice(tp->dev, "Requested %d MSI-X vectors, received %d\n",
9933 tp->irq_cnt, rc);
Matt Carlson679563f2009-09-01 12:55:46 +00009934 tp->irq_cnt = rc;
9935 }
9936
9937 for (i = 0; i < tp->irq_max; i++)
9938 tp->napi[i].irq_vec = msix_ent[i].vector;
9939
Ben Hutchings2ddaad32010-09-27 22:11:51 -07009940 netif_set_real_num_tx_queues(tp->dev, 1);
9941 rc = tp->irq_cnt > 1 ? tp->irq_cnt - 1 : 1;
9942 if (netif_set_real_num_rx_queues(tp->dev, rc)) {
9943 pci_disable_msix(tp->pdev);
9944 return false;
9945 }
Matt Carlsonb92b9042010-11-24 08:31:51 +00009946
9947 if (tp->irq_cnt > 1) {
Joe Perches63c3a662011-04-26 08:12:10 +00009948 tg3_flag_set(tp, ENABLE_RSS);
Matt Carlsond78b59f2011-04-05 14:22:46 +00009949
9950 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
9951 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
Joe Perches63c3a662011-04-26 08:12:10 +00009952 tg3_flag_set(tp, ENABLE_TSS);
Matt Carlsonb92b9042010-11-24 08:31:51 +00009953 netif_set_real_num_tx_queues(tp->dev, tp->irq_cnt - 1);
9954 }
9955 }
Matt Carlson2430b032010-06-05 17:24:34 +00009956
Matt Carlson679563f2009-09-01 12:55:46 +00009957 return true;
9958}
9959
Matt Carlson07b01732009-08-28 14:01:15 +00009960static void tg3_ints_init(struct tg3 *tp)
9961{
Joe Perches63c3a662011-04-26 08:12:10 +00009962 if ((tg3_flag(tp, SUPPORT_MSI) || tg3_flag(tp, SUPPORT_MSIX)) &&
9963 !tg3_flag(tp, TAGGED_STATUS)) {
Matt Carlson07b01732009-08-28 14:01:15 +00009964 /* All MSI supporting chips should support tagged
9965 * status. Assert that this is the case.
9966 */
Matt Carlson5129c3a2010-04-05 10:19:23 +00009967 netdev_warn(tp->dev,
9968 "MSI without TAGGED_STATUS? Not using MSI\n");
Matt Carlson679563f2009-09-01 12:55:46 +00009969 goto defcfg;
Matt Carlson07b01732009-08-28 14:01:15 +00009970 }
Matt Carlson4f125f42009-09-01 12:55:02 +00009971
Joe Perches63c3a662011-04-26 08:12:10 +00009972 if (tg3_flag(tp, SUPPORT_MSIX) && tg3_enable_msix(tp))
9973 tg3_flag_set(tp, USING_MSIX);
9974 else if (tg3_flag(tp, SUPPORT_MSI) && pci_enable_msi(tp->pdev) == 0)
9975 tg3_flag_set(tp, USING_MSI);
Matt Carlson679563f2009-09-01 12:55:46 +00009976
Joe Perches63c3a662011-04-26 08:12:10 +00009977 if (tg3_flag(tp, USING_MSI) || tg3_flag(tp, USING_MSIX)) {
Matt Carlson679563f2009-09-01 12:55:46 +00009978 u32 msi_mode = tr32(MSGINT_MODE);
Joe Perches63c3a662011-04-26 08:12:10 +00009979 if (tg3_flag(tp, USING_MSIX) && tp->irq_cnt > 1)
Matt Carlsonbaf8a942009-09-01 13:13:00 +00009980 msi_mode |= MSGINT_MODE_MULTIVEC_EN;
Matt Carlson5b39de92011-08-31 11:44:50 +00009981 if (!tg3_flag(tp, 1SHOT_MSI))
9982 msi_mode |= MSGINT_MODE_ONE_SHOT_DISABLE;
Matt Carlson679563f2009-09-01 12:55:46 +00009983 tw32(MSGINT_MODE, msi_mode | MSGINT_MODE_ENABLE);
9984 }
9985defcfg:
Joe Perches63c3a662011-04-26 08:12:10 +00009986 if (!tg3_flag(tp, USING_MSIX)) {
Matt Carlson679563f2009-09-01 12:55:46 +00009987 tp->irq_cnt = 1;
9988 tp->napi[0].irq_vec = tp->pdev->irq;
Ben Hutchings2ddaad32010-09-27 22:11:51 -07009989 netif_set_real_num_tx_queues(tp->dev, 1);
Matt Carlson85407882010-10-06 13:40:58 -07009990 netif_set_real_num_rx_queues(tp->dev, 1);
Matt Carlson679563f2009-09-01 12:55:46 +00009991 }
Matt Carlson07b01732009-08-28 14:01:15 +00009992}
9993
9994static void tg3_ints_fini(struct tg3 *tp)
9995{
Joe Perches63c3a662011-04-26 08:12:10 +00009996 if (tg3_flag(tp, USING_MSIX))
Matt Carlson679563f2009-09-01 12:55:46 +00009997 pci_disable_msix(tp->pdev);
Joe Perches63c3a662011-04-26 08:12:10 +00009998 else if (tg3_flag(tp, USING_MSI))
Matt Carlson679563f2009-09-01 12:55:46 +00009999 pci_disable_msi(tp->pdev);
Joe Perches63c3a662011-04-26 08:12:10 +000010000 tg3_flag_clear(tp, USING_MSI);
10001 tg3_flag_clear(tp, USING_MSIX);
10002 tg3_flag_clear(tp, ENABLE_RSS);
10003 tg3_flag_clear(tp, ENABLE_TSS);
Matt Carlson07b01732009-08-28 14:01:15 +000010004}
10005
Linus Torvalds1da177e2005-04-16 15:20:36 -070010006static int tg3_open(struct net_device *dev)
10007{
10008 struct tg3 *tp = netdev_priv(dev);
Matt Carlson4f125f42009-09-01 12:55:02 +000010009 int i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010010
Matt Carlson9e9fd122009-01-19 16:57:45 -080010011 if (tp->fw_needed) {
10012 err = tg3_request_firmware(tp);
10013 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0) {
10014 if (err)
10015 return err;
10016 } else if (err) {
Joe Perches05dbe002010-02-17 19:44:19 +000010017 netdev_warn(tp->dev, "TSO capability disabled\n");
Joe Perches63c3a662011-04-26 08:12:10 +000010018 tg3_flag_clear(tp, TSO_CAPABLE);
10019 } else if (!tg3_flag(tp, TSO_CAPABLE)) {
Joe Perches05dbe002010-02-17 19:44:19 +000010020 netdev_notice(tp->dev, "TSO capability restored\n");
Joe Perches63c3a662011-04-26 08:12:10 +000010021 tg3_flag_set(tp, TSO_CAPABLE);
Matt Carlson9e9fd122009-01-19 16:57:45 -080010022 }
10023 }
10024
Michael Chanc49a1562006-12-17 17:07:29 -080010025 netif_carrier_off(tp->dev);
10026
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000010027 err = tg3_power_up(tp);
Matt Carlson2f751b62008-08-04 23:17:34 -070010028 if (err)
Michael Chanbc1c7562006-03-20 17:48:03 -080010029 return err;
Matt Carlson2f751b62008-08-04 23:17:34 -070010030
10031 tg3_full_lock(tp, 0);
Michael Chanbc1c7562006-03-20 17:48:03 -080010032
Linus Torvalds1da177e2005-04-16 15:20:36 -070010033 tg3_disable_ints(tp);
Joe Perches63c3a662011-04-26 08:12:10 +000010034 tg3_flag_clear(tp, INIT_COMPLETE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010035
David S. Millerf47c11e2005-06-24 20:18:35 -070010036 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010037
Matt Carlson679563f2009-09-01 12:55:46 +000010038 /*
10039 * Setup interrupts first so we know how
10040 * many NAPI resources to allocate
10041 */
10042 tg3_ints_init(tp);
10043
Matt Carlson90415472011-12-16 13:33:23 +000010044 tg3_rss_check_indir_tbl(tp);
Matt Carlsonbcebcc42011-12-14 11:10:01 +000010045
Linus Torvalds1da177e2005-04-16 15:20:36 -070010046 /* The placement of this call is tied
10047 * to the setup and use of Host TX descriptors.
10048 */
10049 err = tg3_alloc_consistent(tp);
10050 if (err)
Matt Carlson679563f2009-09-01 12:55:46 +000010051 goto err_out1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010052
Matt Carlson66cfd1b2010-09-30 10:34:30 +000010053 tg3_napi_init(tp);
10054
Matt Carlsonfed97812009-09-01 13:10:19 +000010055 tg3_napi_enable(tp);
Stephen Hemmingerbea33482007-10-03 16:41:36 -070010056
Matt Carlson4f125f42009-09-01 12:55:02 +000010057 for (i = 0; i < tp->irq_cnt; i++) {
10058 struct tg3_napi *tnapi = &tp->napi[i];
10059 err = tg3_request_irq(tp, i);
10060 if (err) {
Matt Carlson5bc09182011-11-04 09:15:01 +000010061 for (i--; i >= 0; i--) {
10062 tnapi = &tp->napi[i];
Matt Carlson4f125f42009-09-01 12:55:02 +000010063 free_irq(tnapi->irq_vec, tnapi);
Matt Carlson5bc09182011-11-04 09:15:01 +000010064 }
10065 goto err_out2;
Matt Carlson4f125f42009-09-01 12:55:02 +000010066 }
10067 }
Matt Carlson07b01732009-08-28 14:01:15 +000010068
David S. Millerf47c11e2005-06-24 20:18:35 -070010069 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010070
Gary Zambrano8e7a22e2006-04-29 18:59:13 -070010071 err = tg3_init_hw(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010072 if (err) {
Michael Chan944d9802005-05-29 14:57:48 -070010073 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010074 tg3_free_rings(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010075 }
10076
David S. Millerf47c11e2005-06-24 20:18:35 -070010077 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010078
Matt Carlson07b01732009-08-28 14:01:15 +000010079 if (err)
Matt Carlson679563f2009-09-01 12:55:46 +000010080 goto err_out3;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010081
Joe Perches63c3a662011-04-26 08:12:10 +000010082 if (tg3_flag(tp, USING_MSI)) {
Michael Chan79381092005-04-21 17:13:59 -070010083 err = tg3_test_msi(tp);
David S. Millerfac9b832005-05-18 22:46:34 -070010084
Michael Chan79381092005-04-21 17:13:59 -070010085 if (err) {
David S. Millerf47c11e2005-06-24 20:18:35 -070010086 tg3_full_lock(tp, 0);
Michael Chan944d9802005-05-29 14:57:48 -070010087 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Michael Chan79381092005-04-21 17:13:59 -070010088 tg3_free_rings(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -070010089 tg3_full_unlock(tp);
Michael Chan79381092005-04-21 17:13:59 -070010090
Matt Carlson679563f2009-09-01 12:55:46 +000010091 goto err_out2;
Michael Chan79381092005-04-21 17:13:59 -070010092 }
Michael Chanfcfa0a32006-03-20 22:28:41 -080010093
Joe Perches63c3a662011-04-26 08:12:10 +000010094 if (!tg3_flag(tp, 57765_PLUS) && tg3_flag(tp, USING_MSI)) {
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000010095 u32 val = tr32(PCIE_TRANSACTION_CFG);
Michael Chanfcfa0a32006-03-20 22:28:41 -080010096
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000010097 tw32(PCIE_TRANSACTION_CFG,
10098 val | PCIE_TRANS_CFG_1SHOT_MSI);
Michael Chanfcfa0a32006-03-20 22:28:41 -080010099 }
Michael Chan79381092005-04-21 17:13:59 -070010100 }
10101
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010102 tg3_phy_start(tp);
10103
David S. Millerf47c11e2005-06-24 20:18:35 -070010104 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010105
Matt Carlson21f76382012-02-22 12:35:21 +000010106 tg3_timer_start(tp);
Joe Perches63c3a662011-04-26 08:12:10 +000010107 tg3_flag_set(tp, INIT_COMPLETE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010108 tg3_enable_ints(tp);
10109
David S. Millerf47c11e2005-06-24 20:18:35 -070010110 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010111
Matt Carlsonfe5f5782009-09-01 13:09:39 +000010112 netif_tx_start_all_queues(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010113
Mahesh Bandewar06c03c02011-05-08 06:51:48 +000010114 /*
10115 * Reset loopback feature if it was turned on while the device was down
10116 * make sure that it's installed properly now.
10117 */
10118 if (dev->features & NETIF_F_LOOPBACK)
10119 tg3_set_loopback(dev, dev->features);
10120
Linus Torvalds1da177e2005-04-16 15:20:36 -070010121 return 0;
Matt Carlson07b01732009-08-28 14:01:15 +000010122
Matt Carlson679563f2009-09-01 12:55:46 +000010123err_out3:
Matt Carlson4f125f42009-09-01 12:55:02 +000010124 for (i = tp->irq_cnt - 1; i >= 0; i--) {
10125 struct tg3_napi *tnapi = &tp->napi[i];
10126 free_irq(tnapi->irq_vec, tnapi);
10127 }
Matt Carlson07b01732009-08-28 14:01:15 +000010128
Matt Carlson679563f2009-09-01 12:55:46 +000010129err_out2:
Matt Carlsonfed97812009-09-01 13:10:19 +000010130 tg3_napi_disable(tp);
Matt Carlson66cfd1b2010-09-30 10:34:30 +000010131 tg3_napi_fini(tp);
Matt Carlson07b01732009-08-28 14:01:15 +000010132 tg3_free_consistent(tp);
Matt Carlson679563f2009-09-01 12:55:46 +000010133
10134err_out1:
10135 tg3_ints_fini(tp);
Matt Carlsoncd0d7222011-07-13 09:27:33 +000010136 tg3_frob_aux_power(tp, false);
10137 pci_set_power_state(tp->pdev, PCI_D3hot);
Matt Carlson07b01732009-08-28 14:01:15 +000010138 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010139}
10140
Linus Torvalds1da177e2005-04-16 15:20:36 -070010141static int tg3_close(struct net_device *dev)
10142{
Matt Carlson4f125f42009-09-01 12:55:02 +000010143 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010144 struct tg3 *tp = netdev_priv(dev);
10145
Matt Carlsonfed97812009-09-01 13:10:19 +000010146 tg3_napi_disable(tp);
Matt Carlsondb219972011-11-04 09:15:03 +000010147 tg3_reset_task_cancel(tp);
Michael Chan7faa0062006-02-02 17:29:28 -080010148
Matt Carlsonfe5f5782009-09-01 13:09:39 +000010149 netif_tx_stop_all_queues(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010150
Matt Carlson21f76382012-02-22 12:35:21 +000010151 tg3_timer_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010152
Matt Carlson24bb4fb2009-10-05 17:55:29 +000010153 tg3_phy_stop(tp);
10154
David S. Millerf47c11e2005-06-24 20:18:35 -070010155 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010156
10157 tg3_disable_ints(tp);
10158
Michael Chan944d9802005-05-29 14:57:48 -070010159 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010160 tg3_free_rings(tp);
Joe Perches63c3a662011-04-26 08:12:10 +000010161 tg3_flag_clear(tp, INIT_COMPLETE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010162
David S. Millerf47c11e2005-06-24 20:18:35 -070010163 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010164
Matt Carlson4f125f42009-09-01 12:55:02 +000010165 for (i = tp->irq_cnt - 1; i >= 0; i--) {
10166 struct tg3_napi *tnapi = &tp->napi[i];
10167 free_irq(tnapi->irq_vec, tnapi);
10168 }
Matt Carlson07b01732009-08-28 14:01:15 +000010169
10170 tg3_ints_fini(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010171
Matt Carlson92feeab2011-12-08 14:40:14 +000010172 /* Clear stats across close / open calls */
10173 memset(&tp->net_stats_prev, 0, sizeof(tp->net_stats_prev));
10174 memset(&tp->estats_prev, 0, sizeof(tp->estats_prev));
Linus Torvalds1da177e2005-04-16 15:20:36 -070010175
Matt Carlson66cfd1b2010-09-30 10:34:30 +000010176 tg3_napi_fini(tp);
10177
Linus Torvalds1da177e2005-04-16 15:20:36 -070010178 tg3_free_consistent(tp);
10179
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000010180 tg3_power_down(tp);
Michael Chanbc1c7562006-03-20 17:48:03 -080010181
10182 netif_carrier_off(tp->dev);
10183
Linus Torvalds1da177e2005-04-16 15:20:36 -070010184 return 0;
10185}
10186
Eric Dumazet511d2222010-07-07 20:44:24 +000010187static inline u64 get_stat64(tg3_stat64_t *val)
Stefan Buehler816f8b82008-08-15 14:10:54 -070010188{
10189 return ((u64)val->high << 32) | ((u64)val->low);
10190}
10191
Matt Carlson65ec6982012-02-28 23:33:37 +000010192static u64 tg3_calc_crc_errors(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010193{
10194 struct tg3_hw_stats *hw_stats = tp->hw_stats;
10195
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010196 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070010197 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
10198 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010199 u32 val;
10200
Michael Chan569a5df2007-02-13 12:18:15 -080010201 if (!tg3_readphy(tp, MII_TG3_TEST1, &val)) {
10202 tg3_writephy(tp, MII_TG3_TEST1,
10203 val | MII_TG3_TEST1_CRC_EN);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +000010204 tg3_readphy(tp, MII_TG3_RXR_COUNTERS, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010205 } else
10206 val = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010207
10208 tp->phy_crc_errors += val;
10209
10210 return tp->phy_crc_errors;
10211 }
10212
10213 return get_stat64(&hw_stats->rx_fcs_errors);
10214}
10215
10216#define ESTAT_ADD(member) \
10217 estats->member = old_estats->member + \
Eric Dumazet511d2222010-07-07 20:44:24 +000010218 get_stat64(&hw_stats->member)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010219
Matt Carlson65ec6982012-02-28 23:33:37 +000010220static void tg3_get_estats(struct tg3 *tp, struct tg3_ethtool_stats *estats)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010221{
Linus Torvalds1da177e2005-04-16 15:20:36 -070010222 struct tg3_ethtool_stats *old_estats = &tp->estats_prev;
10223 struct tg3_hw_stats *hw_stats = tp->hw_stats;
10224
Linus Torvalds1da177e2005-04-16 15:20:36 -070010225 ESTAT_ADD(rx_octets);
10226 ESTAT_ADD(rx_fragments);
10227 ESTAT_ADD(rx_ucast_packets);
10228 ESTAT_ADD(rx_mcast_packets);
10229 ESTAT_ADD(rx_bcast_packets);
10230 ESTAT_ADD(rx_fcs_errors);
10231 ESTAT_ADD(rx_align_errors);
10232 ESTAT_ADD(rx_xon_pause_rcvd);
10233 ESTAT_ADD(rx_xoff_pause_rcvd);
10234 ESTAT_ADD(rx_mac_ctrl_rcvd);
10235 ESTAT_ADD(rx_xoff_entered);
10236 ESTAT_ADD(rx_frame_too_long_errors);
10237 ESTAT_ADD(rx_jabbers);
10238 ESTAT_ADD(rx_undersize_packets);
10239 ESTAT_ADD(rx_in_length_errors);
10240 ESTAT_ADD(rx_out_length_errors);
10241 ESTAT_ADD(rx_64_or_less_octet_packets);
10242 ESTAT_ADD(rx_65_to_127_octet_packets);
10243 ESTAT_ADD(rx_128_to_255_octet_packets);
10244 ESTAT_ADD(rx_256_to_511_octet_packets);
10245 ESTAT_ADD(rx_512_to_1023_octet_packets);
10246 ESTAT_ADD(rx_1024_to_1522_octet_packets);
10247 ESTAT_ADD(rx_1523_to_2047_octet_packets);
10248 ESTAT_ADD(rx_2048_to_4095_octet_packets);
10249 ESTAT_ADD(rx_4096_to_8191_octet_packets);
10250 ESTAT_ADD(rx_8192_to_9022_octet_packets);
10251
10252 ESTAT_ADD(tx_octets);
10253 ESTAT_ADD(tx_collisions);
10254 ESTAT_ADD(tx_xon_sent);
10255 ESTAT_ADD(tx_xoff_sent);
10256 ESTAT_ADD(tx_flow_control);
10257 ESTAT_ADD(tx_mac_errors);
10258 ESTAT_ADD(tx_single_collisions);
10259 ESTAT_ADD(tx_mult_collisions);
10260 ESTAT_ADD(tx_deferred);
10261 ESTAT_ADD(tx_excessive_collisions);
10262 ESTAT_ADD(tx_late_collisions);
10263 ESTAT_ADD(tx_collide_2times);
10264 ESTAT_ADD(tx_collide_3times);
10265 ESTAT_ADD(tx_collide_4times);
10266 ESTAT_ADD(tx_collide_5times);
10267 ESTAT_ADD(tx_collide_6times);
10268 ESTAT_ADD(tx_collide_7times);
10269 ESTAT_ADD(tx_collide_8times);
10270 ESTAT_ADD(tx_collide_9times);
10271 ESTAT_ADD(tx_collide_10times);
10272 ESTAT_ADD(tx_collide_11times);
10273 ESTAT_ADD(tx_collide_12times);
10274 ESTAT_ADD(tx_collide_13times);
10275 ESTAT_ADD(tx_collide_14times);
10276 ESTAT_ADD(tx_collide_15times);
10277 ESTAT_ADD(tx_ucast_packets);
10278 ESTAT_ADD(tx_mcast_packets);
10279 ESTAT_ADD(tx_bcast_packets);
10280 ESTAT_ADD(tx_carrier_sense_errors);
10281 ESTAT_ADD(tx_discards);
10282 ESTAT_ADD(tx_errors);
10283
10284 ESTAT_ADD(dma_writeq_full);
10285 ESTAT_ADD(dma_write_prioq_full);
10286 ESTAT_ADD(rxbds_empty);
10287 ESTAT_ADD(rx_discards);
10288 ESTAT_ADD(rx_errors);
10289 ESTAT_ADD(rx_threshold_hit);
10290
10291 ESTAT_ADD(dma_readq_full);
10292 ESTAT_ADD(dma_read_prioq_full);
10293 ESTAT_ADD(tx_comp_queue_full);
10294
10295 ESTAT_ADD(ring_set_send_prod_index);
10296 ESTAT_ADD(ring_status_update);
10297 ESTAT_ADD(nic_irqs);
10298 ESTAT_ADD(nic_avoided_irqs);
10299 ESTAT_ADD(nic_tx_threshold_hit);
10300
Matt Carlson4452d092011-05-19 12:12:51 +000010301 ESTAT_ADD(mbuf_lwm_thresh_hit);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010302}
10303
Matt Carlson65ec6982012-02-28 23:33:37 +000010304static void tg3_get_nstats(struct tg3 *tp, struct rtnl_link_stats64 *stats)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010305{
Eric Dumazet511d2222010-07-07 20:44:24 +000010306 struct rtnl_link_stats64 *old_stats = &tp->net_stats_prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010307 struct tg3_hw_stats *hw_stats = tp->hw_stats;
10308
Linus Torvalds1da177e2005-04-16 15:20:36 -070010309 stats->rx_packets = old_stats->rx_packets +
10310 get_stat64(&hw_stats->rx_ucast_packets) +
10311 get_stat64(&hw_stats->rx_mcast_packets) +
10312 get_stat64(&hw_stats->rx_bcast_packets);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010313
Linus Torvalds1da177e2005-04-16 15:20:36 -070010314 stats->tx_packets = old_stats->tx_packets +
10315 get_stat64(&hw_stats->tx_ucast_packets) +
10316 get_stat64(&hw_stats->tx_mcast_packets) +
10317 get_stat64(&hw_stats->tx_bcast_packets);
10318
10319 stats->rx_bytes = old_stats->rx_bytes +
10320 get_stat64(&hw_stats->rx_octets);
10321 stats->tx_bytes = old_stats->tx_bytes +
10322 get_stat64(&hw_stats->tx_octets);
10323
10324 stats->rx_errors = old_stats->rx_errors +
John W. Linville4f63b872005-09-12 14:43:18 -070010325 get_stat64(&hw_stats->rx_errors);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010326 stats->tx_errors = old_stats->tx_errors +
10327 get_stat64(&hw_stats->tx_errors) +
10328 get_stat64(&hw_stats->tx_mac_errors) +
10329 get_stat64(&hw_stats->tx_carrier_sense_errors) +
10330 get_stat64(&hw_stats->tx_discards);
10331
10332 stats->multicast = old_stats->multicast +
10333 get_stat64(&hw_stats->rx_mcast_packets);
10334 stats->collisions = old_stats->collisions +
10335 get_stat64(&hw_stats->tx_collisions);
10336
10337 stats->rx_length_errors = old_stats->rx_length_errors +
10338 get_stat64(&hw_stats->rx_frame_too_long_errors) +
10339 get_stat64(&hw_stats->rx_undersize_packets);
10340
10341 stats->rx_over_errors = old_stats->rx_over_errors +
10342 get_stat64(&hw_stats->rxbds_empty);
10343 stats->rx_frame_errors = old_stats->rx_frame_errors +
10344 get_stat64(&hw_stats->rx_align_errors);
10345 stats->tx_aborted_errors = old_stats->tx_aborted_errors +
10346 get_stat64(&hw_stats->tx_discards);
10347 stats->tx_carrier_errors = old_stats->tx_carrier_errors +
10348 get_stat64(&hw_stats->tx_carrier_sense_errors);
10349
10350 stats->rx_crc_errors = old_stats->rx_crc_errors +
Matt Carlson65ec6982012-02-28 23:33:37 +000010351 tg3_calc_crc_errors(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010352
John W. Linville4f63b872005-09-12 14:43:18 -070010353 stats->rx_missed_errors = old_stats->rx_missed_errors +
10354 get_stat64(&hw_stats->rx_discards);
10355
Eric Dumazetb0057c52010-10-10 19:55:52 +000010356 stats->rx_dropped = tp->rx_dropped;
Eric Dumazet48855432011-10-24 07:53:03 +000010357 stats->tx_dropped = tp->tx_dropped;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010358}
10359
Linus Torvalds1da177e2005-04-16 15:20:36 -070010360static int tg3_get_regs_len(struct net_device *dev)
10361{
Matt Carlson97bd8e42011-04-13 11:05:04 +000010362 return TG3_REG_BLK_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010363}
10364
10365static void tg3_get_regs(struct net_device *dev,
10366 struct ethtool_regs *regs, void *_p)
10367{
Linus Torvalds1da177e2005-04-16 15:20:36 -070010368 struct tg3 *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010369
10370 regs->version = 0;
10371
Matt Carlson97bd8e42011-04-13 11:05:04 +000010372 memset(_p, 0, TG3_REG_BLK_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010373
Matt Carlson80096062010-08-02 11:26:06 +000010374 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Michael Chanbc1c7562006-03-20 17:48:03 -080010375 return;
10376
David S. Millerf47c11e2005-06-24 20:18:35 -070010377 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010378
Matt Carlson97bd8e42011-04-13 11:05:04 +000010379 tg3_dump_legacy_regs(tp, (u32 *)_p);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010380
David S. Millerf47c11e2005-06-24 20:18:35 -070010381 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010382}
10383
10384static int tg3_get_eeprom_len(struct net_device *dev)
10385{
10386 struct tg3 *tp = netdev_priv(dev);
10387
10388 return tp->nvram_size;
10389}
10390
Linus Torvalds1da177e2005-04-16 15:20:36 -070010391static int tg3_get_eeprom(struct net_device *dev, struct ethtool_eeprom *eeprom, u8 *data)
10392{
10393 struct tg3 *tp = netdev_priv(dev);
10394 int ret;
10395 u8 *pd;
Al Virob9fc7dc2007-12-17 22:59:57 -080010396 u32 i, offset, len, b_offset, b_count;
Matt Carlsona9dc5292009-02-25 14:25:30 +000010397 __be32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010398
Joe Perches63c3a662011-04-26 08:12:10 +000010399 if (tg3_flag(tp, NO_NVRAM))
Matt Carlsondf259d82009-04-20 06:57:14 +000010400 return -EINVAL;
10401
Matt Carlson80096062010-08-02 11:26:06 +000010402 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Michael Chanbc1c7562006-03-20 17:48:03 -080010403 return -EAGAIN;
10404
Linus Torvalds1da177e2005-04-16 15:20:36 -070010405 offset = eeprom->offset;
10406 len = eeprom->len;
10407 eeprom->len = 0;
10408
10409 eeprom->magic = TG3_EEPROM_MAGIC;
10410
10411 if (offset & 3) {
10412 /* adjustments to start on required 4 byte boundary */
10413 b_offset = offset & 3;
10414 b_count = 4 - b_offset;
10415 if (b_count > len) {
10416 /* i.e. offset=1 len=2 */
10417 b_count = len;
10418 }
Matt Carlsona9dc5292009-02-25 14:25:30 +000010419 ret = tg3_nvram_read_be32(tp, offset-b_offset, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010420 if (ret)
10421 return ret;
Matt Carlsonbe98da62010-07-11 09:31:46 +000010422 memcpy(data, ((char *)&val) + b_offset, b_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010423 len -= b_count;
10424 offset += b_count;
Matt Carlsonc6cdf432010-04-05 10:19:26 +000010425 eeprom->len += b_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010426 }
10427
Lucas De Marchi25985ed2011-03-30 22:57:33 -030010428 /* read bytes up to the last 4 byte boundary */
Linus Torvalds1da177e2005-04-16 15:20:36 -070010429 pd = &data[eeprom->len];
10430 for (i = 0; i < (len - (len & 3)); i += 4) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000010431 ret = tg3_nvram_read_be32(tp, offset + i, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010432 if (ret) {
10433 eeprom->len += i;
10434 return ret;
10435 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010436 memcpy(pd + i, &val, 4);
10437 }
10438 eeprom->len += i;
10439
10440 if (len & 3) {
10441 /* read last bytes not ending on 4 byte boundary */
10442 pd = &data[eeprom->len];
10443 b_count = len & 3;
10444 b_offset = offset + len - b_count;
Matt Carlsona9dc5292009-02-25 14:25:30 +000010445 ret = tg3_nvram_read_be32(tp, b_offset, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010446 if (ret)
10447 return ret;
Al Virob9fc7dc2007-12-17 22:59:57 -080010448 memcpy(pd, &val, b_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010449 eeprom->len += b_count;
10450 }
10451 return 0;
10452}
10453
Linus Torvalds1da177e2005-04-16 15:20:36 -070010454static int tg3_set_eeprom(struct net_device *dev, struct ethtool_eeprom *eeprom, u8 *data)
10455{
10456 struct tg3 *tp = netdev_priv(dev);
10457 int ret;
Al Virob9fc7dc2007-12-17 22:59:57 -080010458 u32 offset, len, b_offset, odd_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010459 u8 *buf;
Matt Carlsona9dc5292009-02-25 14:25:30 +000010460 __be32 start, end;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010461
Matt Carlson80096062010-08-02 11:26:06 +000010462 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Michael Chanbc1c7562006-03-20 17:48:03 -080010463 return -EAGAIN;
10464
Joe Perches63c3a662011-04-26 08:12:10 +000010465 if (tg3_flag(tp, NO_NVRAM) ||
Matt Carlsondf259d82009-04-20 06:57:14 +000010466 eeprom->magic != TG3_EEPROM_MAGIC)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010467 return -EINVAL;
10468
10469 offset = eeprom->offset;
10470 len = eeprom->len;
10471
10472 if ((b_offset = (offset & 3))) {
10473 /* adjustments to start on required 4 byte boundary */
Matt Carlsona9dc5292009-02-25 14:25:30 +000010474 ret = tg3_nvram_read_be32(tp, offset-b_offset, &start);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010475 if (ret)
10476 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010477 len += b_offset;
10478 offset &= ~3;
Michael Chan1c8594b2005-04-21 17:12:46 -070010479 if (len < 4)
10480 len = 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010481 }
10482
10483 odd_len = 0;
Michael Chan1c8594b2005-04-21 17:12:46 -070010484 if (len & 3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010485 /* adjustments to end on required 4 byte boundary */
10486 odd_len = 1;
10487 len = (len + 3) & ~3;
Matt Carlsona9dc5292009-02-25 14:25:30 +000010488 ret = tg3_nvram_read_be32(tp, offset+len-4, &end);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010489 if (ret)
10490 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010491 }
10492
10493 buf = data;
10494 if (b_offset || odd_len) {
10495 buf = kmalloc(len, GFP_KERNEL);
Andy Gospodarekab0049b2007-09-06 20:42:14 +010010496 if (!buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010497 return -ENOMEM;
10498 if (b_offset)
10499 memcpy(buf, &start, 4);
10500 if (odd_len)
10501 memcpy(buf+len-4, &end, 4);
10502 memcpy(buf + b_offset, data, eeprom->len);
10503 }
10504
10505 ret = tg3_nvram_write_block(tp, offset, len, buf);
10506
10507 if (buf != data)
10508 kfree(buf);
10509
10510 return ret;
10511}
10512
10513static int tg3_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
10514{
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010515 struct tg3 *tp = netdev_priv(dev);
10516
Joe Perches63c3a662011-04-26 08:12:10 +000010517 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000010518 struct phy_device *phydev;
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010519 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010520 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000010521 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
10522 return phy_ethtool_gset(phydev, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010523 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010524
Linus Torvalds1da177e2005-04-16 15:20:36 -070010525 cmd->supported = (SUPPORTED_Autoneg);
10526
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010527 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010528 cmd->supported |= (SUPPORTED_1000baseT_Half |
10529 SUPPORTED_1000baseT_Full);
10530
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010531 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010532 cmd->supported |= (SUPPORTED_100baseT_Half |
10533 SUPPORTED_100baseT_Full |
10534 SUPPORTED_10baseT_Half |
10535 SUPPORTED_10baseT_Full |
Matt Carlson3bebab52007-11-12 21:22:40 -080010536 SUPPORTED_TP);
Karsten Keilef348142006-05-12 12:49:08 -070010537 cmd->port = PORT_TP;
10538 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010539 cmd->supported |= SUPPORTED_FIBRE;
Karsten Keilef348142006-05-12 12:49:08 -070010540 cmd->port = PORT_FIBRE;
10541 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010542
Linus Torvalds1da177e2005-04-16 15:20:36 -070010543 cmd->advertising = tp->link_config.advertising;
Matt Carlson5bb09772011-06-13 13:39:00 +000010544 if (tg3_flag(tp, PAUSE_AUTONEG)) {
10545 if (tp->link_config.flowctrl & FLOW_CTRL_RX) {
10546 if (tp->link_config.flowctrl & FLOW_CTRL_TX) {
10547 cmd->advertising |= ADVERTISED_Pause;
10548 } else {
10549 cmd->advertising |= ADVERTISED_Pause |
10550 ADVERTISED_Asym_Pause;
10551 }
10552 } else if (tp->link_config.flowctrl & FLOW_CTRL_TX) {
10553 cmd->advertising |= ADVERTISED_Asym_Pause;
10554 }
10555 }
Matt Carlson859edb22011-12-08 14:40:16 +000010556 if (netif_running(dev) && netif_carrier_ok(dev)) {
David Decotigny70739492011-04-27 18:32:40 +000010557 ethtool_cmd_speed_set(cmd, tp->link_config.active_speed);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010558 cmd->duplex = tp->link_config.active_duplex;
Matt Carlson859edb22011-12-08 14:40:16 +000010559 cmd->lp_advertising = tp->link_config.rmt_adv;
Matt Carlsone348c5e2011-11-21 15:01:20 +000010560 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES)) {
10561 if (tp->phy_flags & TG3_PHYFLG_MDIX_STATE)
10562 cmd->eth_tp_mdix = ETH_TP_MDI_X;
10563 else
10564 cmd->eth_tp_mdix = ETH_TP_MDI;
10565 }
Matt Carlson64c22182010-10-14 10:37:44 +000010566 } else {
Matt Carlsone7405222012-02-13 15:20:16 +000010567 ethtool_cmd_speed_set(cmd, SPEED_UNKNOWN);
10568 cmd->duplex = DUPLEX_UNKNOWN;
Matt Carlsone348c5e2011-11-21 15:01:20 +000010569 cmd->eth_tp_mdix = ETH_TP_MDI_INVALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010570 }
Matt Carlson882e9792009-09-01 13:21:36 +000010571 cmd->phy_address = tp->phy_addr;
Matt Carlson7e5856b2009-02-25 14:23:01 +000010572 cmd->transceiver = XCVR_INTERNAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010573 cmd->autoneg = tp->link_config.autoneg;
10574 cmd->maxtxpkt = 0;
10575 cmd->maxrxpkt = 0;
10576 return 0;
10577}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010578
Linus Torvalds1da177e2005-04-16 15:20:36 -070010579static int tg3_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
10580{
10581 struct tg3 *tp = netdev_priv(dev);
David Decotigny25db0332011-04-27 18:32:39 +000010582 u32 speed = ethtool_cmd_speed(cmd);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010583
Joe Perches63c3a662011-04-26 08:12:10 +000010584 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000010585 struct phy_device *phydev;
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010586 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010587 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000010588 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
10589 return phy_ethtool_sset(phydev, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010590 }
10591
Matt Carlson7e5856b2009-02-25 14:23:01 +000010592 if (cmd->autoneg != AUTONEG_ENABLE &&
10593 cmd->autoneg != AUTONEG_DISABLE)
Michael Chan37ff2382005-10-26 15:49:51 -070010594 return -EINVAL;
Matt Carlson7e5856b2009-02-25 14:23:01 +000010595
10596 if (cmd->autoneg == AUTONEG_DISABLE &&
10597 cmd->duplex != DUPLEX_FULL &&
10598 cmd->duplex != DUPLEX_HALF)
Michael Chan37ff2382005-10-26 15:49:51 -070010599 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010600
Matt Carlson7e5856b2009-02-25 14:23:01 +000010601 if (cmd->autoneg == AUTONEG_ENABLE) {
10602 u32 mask = ADVERTISED_Autoneg |
10603 ADVERTISED_Pause |
10604 ADVERTISED_Asym_Pause;
10605
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010606 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
Matt Carlson7e5856b2009-02-25 14:23:01 +000010607 mask |= ADVERTISED_1000baseT_Half |
10608 ADVERTISED_1000baseT_Full;
10609
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010610 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
Matt Carlson7e5856b2009-02-25 14:23:01 +000010611 mask |= ADVERTISED_100baseT_Half |
10612 ADVERTISED_100baseT_Full |
10613 ADVERTISED_10baseT_Half |
10614 ADVERTISED_10baseT_Full |
10615 ADVERTISED_TP;
10616 else
10617 mask |= ADVERTISED_FIBRE;
10618
10619 if (cmd->advertising & ~mask)
10620 return -EINVAL;
10621
10622 mask &= (ADVERTISED_1000baseT_Half |
10623 ADVERTISED_1000baseT_Full |
10624 ADVERTISED_100baseT_Half |
10625 ADVERTISED_100baseT_Full |
10626 ADVERTISED_10baseT_Half |
10627 ADVERTISED_10baseT_Full);
10628
10629 cmd->advertising &= mask;
10630 } else {
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010631 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES) {
David Decotigny25db0332011-04-27 18:32:39 +000010632 if (speed != SPEED_1000)
Matt Carlson7e5856b2009-02-25 14:23:01 +000010633 return -EINVAL;
10634
10635 if (cmd->duplex != DUPLEX_FULL)
10636 return -EINVAL;
10637 } else {
David Decotigny25db0332011-04-27 18:32:39 +000010638 if (speed != SPEED_100 &&
10639 speed != SPEED_10)
Matt Carlson7e5856b2009-02-25 14:23:01 +000010640 return -EINVAL;
10641 }
10642 }
10643
David S. Millerf47c11e2005-06-24 20:18:35 -070010644 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010645
10646 tp->link_config.autoneg = cmd->autoneg;
10647 if (cmd->autoneg == AUTONEG_ENABLE) {
Andy Gospodarek405d8e52007-10-08 01:08:47 -070010648 tp->link_config.advertising = (cmd->advertising |
10649 ADVERTISED_Autoneg);
Matt Carlsone7405222012-02-13 15:20:16 +000010650 tp->link_config.speed = SPEED_UNKNOWN;
10651 tp->link_config.duplex = DUPLEX_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010652 } else {
10653 tp->link_config.advertising = 0;
David Decotigny25db0332011-04-27 18:32:39 +000010654 tp->link_config.speed = speed;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010655 tp->link_config.duplex = cmd->duplex;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010656 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010657
Linus Torvalds1da177e2005-04-16 15:20:36 -070010658 if (netif_running(dev))
10659 tg3_setup_phy(tp, 1);
10660
David S. Millerf47c11e2005-06-24 20:18:35 -070010661 tg3_full_unlock(tp);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010662
Linus Torvalds1da177e2005-04-16 15:20:36 -070010663 return 0;
10664}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010665
Linus Torvalds1da177e2005-04-16 15:20:36 -070010666static void tg3_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
10667{
10668 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010669
Rick Jones68aad782011-11-07 13:29:27 +000010670 strlcpy(info->driver, DRV_MODULE_NAME, sizeof(info->driver));
10671 strlcpy(info->version, DRV_MODULE_VERSION, sizeof(info->version));
10672 strlcpy(info->fw_version, tp->fw_ver, sizeof(info->fw_version));
10673 strlcpy(info->bus_info, pci_name(tp->pdev), sizeof(info->bus_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -070010674}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010675
Linus Torvalds1da177e2005-04-16 15:20:36 -070010676static void tg3_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
10677{
10678 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010679
Joe Perches63c3a662011-04-26 08:12:10 +000010680 if (tg3_flag(tp, WOL_CAP) && device_can_wakeup(&tp->pdev->dev))
Gary Zambranoa85feb82007-05-05 11:52:19 -070010681 wol->supported = WAKE_MAGIC;
10682 else
10683 wol->supported = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010684 wol->wolopts = 0;
Joe Perches63c3a662011-04-26 08:12:10 +000010685 if (tg3_flag(tp, WOL_ENABLE) && device_can_wakeup(&tp->pdev->dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010686 wol->wolopts = WAKE_MAGIC;
10687 memset(&wol->sopass, 0, sizeof(wol->sopass));
10688}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010689
Linus Torvalds1da177e2005-04-16 15:20:36 -070010690static int tg3_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
10691{
10692 struct tg3 *tp = netdev_priv(dev);
Rafael J. Wysocki12dac072008-07-30 16:37:33 -070010693 struct device *dp = &tp->pdev->dev;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010694
Linus Torvalds1da177e2005-04-16 15:20:36 -070010695 if (wol->wolopts & ~WAKE_MAGIC)
10696 return -EINVAL;
10697 if ((wol->wolopts & WAKE_MAGIC) &&
Joe Perches63c3a662011-04-26 08:12:10 +000010698 !(tg3_flag(tp, WOL_CAP) && device_can_wakeup(dp)))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010699 return -EINVAL;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010700
Rafael J. Wysockif2dc0d12010-10-25 13:01:55 +000010701 device_set_wakeup_enable(dp, wol->wolopts & WAKE_MAGIC);
10702
David S. Millerf47c11e2005-06-24 20:18:35 -070010703 spin_lock_bh(&tp->lock);
Rafael J. Wysockif2dc0d12010-10-25 13:01:55 +000010704 if (device_may_wakeup(dp))
Joe Perches63c3a662011-04-26 08:12:10 +000010705 tg3_flag_set(tp, WOL_ENABLE);
Rafael J. Wysockif2dc0d12010-10-25 13:01:55 +000010706 else
Joe Perches63c3a662011-04-26 08:12:10 +000010707 tg3_flag_clear(tp, WOL_ENABLE);
David S. Millerf47c11e2005-06-24 20:18:35 -070010708 spin_unlock_bh(&tp->lock);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010709
Linus Torvalds1da177e2005-04-16 15:20:36 -070010710 return 0;
10711}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010712
Linus Torvalds1da177e2005-04-16 15:20:36 -070010713static u32 tg3_get_msglevel(struct net_device *dev)
10714{
10715 struct tg3 *tp = netdev_priv(dev);
10716 return tp->msg_enable;
10717}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010718
Linus Torvalds1da177e2005-04-16 15:20:36 -070010719static void tg3_set_msglevel(struct net_device *dev, u32 value)
10720{
10721 struct tg3 *tp = netdev_priv(dev);
10722 tp->msg_enable = value;
10723}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010724
Linus Torvalds1da177e2005-04-16 15:20:36 -070010725static int tg3_nway_reset(struct net_device *dev)
10726{
10727 struct tg3 *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010728 int r;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010729
Linus Torvalds1da177e2005-04-16 15:20:36 -070010730 if (!netif_running(dev))
10731 return -EAGAIN;
10732
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010733 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Michael Chanc94e3942005-09-27 12:12:42 -070010734 return -EINVAL;
10735
Joe Perches63c3a662011-04-26 08:12:10 +000010736 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010737 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010738 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000010739 r = phy_start_aneg(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010740 } else {
10741 u32 bmcr;
10742
10743 spin_lock_bh(&tp->lock);
10744 r = -EINVAL;
10745 tg3_readphy(tp, MII_BMCR, &bmcr);
10746 if (!tg3_readphy(tp, MII_BMCR, &bmcr) &&
10747 ((bmcr & BMCR_ANENABLE) ||
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010748 (tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT))) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010749 tg3_writephy(tp, MII_BMCR, bmcr | BMCR_ANRESTART |
10750 BMCR_ANENABLE);
10751 r = 0;
10752 }
10753 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010754 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010755
Linus Torvalds1da177e2005-04-16 15:20:36 -070010756 return r;
10757}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010758
Linus Torvalds1da177e2005-04-16 15:20:36 -070010759static void tg3_get_ringparam(struct net_device *dev, struct ethtool_ringparam *ering)
10760{
10761 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010762
Matt Carlson2c49a442010-09-30 10:34:35 +000010763 ering->rx_max_pending = tp->rx_std_ring_mask;
Joe Perches63c3a662011-04-26 08:12:10 +000010764 if (tg3_flag(tp, JUMBO_RING_ENABLE))
Matt Carlson2c49a442010-09-30 10:34:35 +000010765 ering->rx_jumbo_max_pending = tp->rx_jmb_ring_mask;
Michael Chan4f81c322006-03-20 21:33:42 -080010766 else
10767 ering->rx_jumbo_max_pending = 0;
10768
10769 ering->tx_max_pending = TG3_TX_RING_SIZE - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010770
10771 ering->rx_pending = tp->rx_pending;
Joe Perches63c3a662011-04-26 08:12:10 +000010772 if (tg3_flag(tp, JUMBO_RING_ENABLE))
Michael Chan4f81c322006-03-20 21:33:42 -080010773 ering->rx_jumbo_pending = tp->rx_jumbo_pending;
10774 else
10775 ering->rx_jumbo_pending = 0;
10776
Matt Carlsonf3f3f272009-08-28 14:03:21 +000010777 ering->tx_pending = tp->napi[0].tx_pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010778}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010779
Linus Torvalds1da177e2005-04-16 15:20:36 -070010780static int tg3_set_ringparam(struct net_device *dev, struct ethtool_ringparam *ering)
10781{
10782 struct tg3 *tp = netdev_priv(dev);
Matt Carlson646c9ed2009-09-01 12:58:41 +000010783 int i, irq_sync = 0, err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010784
Matt Carlson2c49a442010-09-30 10:34:35 +000010785 if ((ering->rx_pending > tp->rx_std_ring_mask) ||
10786 (ering->rx_jumbo_pending > tp->rx_jmb_ring_mask) ||
Michael Chanbc3a9252006-10-18 20:55:18 -070010787 (ering->tx_pending > TG3_TX_RING_SIZE - 1) ||
10788 (ering->tx_pending <= MAX_SKB_FRAGS) ||
Joe Perches63c3a662011-04-26 08:12:10 +000010789 (tg3_flag(tp, TSO_BUG) &&
Michael Chanbc3a9252006-10-18 20:55:18 -070010790 (ering->tx_pending <= (MAX_SKB_FRAGS * 3))))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010791 return -EINVAL;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010792
Michael Chanbbe832c2005-06-24 20:20:04 -070010793 if (netif_running(dev)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010794 tg3_phy_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010795 tg3_netif_stop(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -070010796 irq_sync = 1;
10797 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010798
Michael Chanbbe832c2005-06-24 20:20:04 -070010799 tg3_full_lock(tp, irq_sync);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010800
Linus Torvalds1da177e2005-04-16 15:20:36 -070010801 tp->rx_pending = ering->rx_pending;
10802
Joe Perches63c3a662011-04-26 08:12:10 +000010803 if (tg3_flag(tp, MAX_RXPEND_64) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070010804 tp->rx_pending > 63)
10805 tp->rx_pending = 63;
10806 tp->rx_jumbo_pending = ering->rx_jumbo_pending;
Matt Carlson646c9ed2009-09-01 12:58:41 +000010807
Matt Carlson6fd45cb2010-09-15 08:59:57 +000010808 for (i = 0; i < tp->irq_max; i++)
Matt Carlson646c9ed2009-09-01 12:58:41 +000010809 tp->napi[i].tx_pending = ering->tx_pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010810
10811 if (netif_running(dev)) {
Michael Chan944d9802005-05-29 14:57:48 -070010812 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Michael Chanb9ec6c12006-07-25 16:37:27 -070010813 err = tg3_restart_hw(tp, 1);
10814 if (!err)
10815 tg3_netif_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010816 }
10817
David S. Millerf47c11e2005-06-24 20:18:35 -070010818 tg3_full_unlock(tp);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010819
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010820 if (irq_sync && !err)
10821 tg3_phy_start(tp);
10822
Michael Chanb9ec6c12006-07-25 16:37:27 -070010823 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010824}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010825
Linus Torvalds1da177e2005-04-16 15:20:36 -070010826static void tg3_get_pauseparam(struct net_device *dev, struct ethtool_pauseparam *epause)
10827{
10828 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010829
Joe Perches63c3a662011-04-26 08:12:10 +000010830 epause->autoneg = !!tg3_flag(tp, PAUSE_AUTONEG);
Matt Carlson8d018622007-12-20 20:05:44 -080010831
Matt Carlson4a2db502011-12-08 14:40:17 +000010832 if (tp->link_config.flowctrl & FLOW_CTRL_RX)
Matt Carlson8d018622007-12-20 20:05:44 -080010833 epause->rx_pause = 1;
10834 else
10835 epause->rx_pause = 0;
10836
Matt Carlson4a2db502011-12-08 14:40:17 +000010837 if (tp->link_config.flowctrl & FLOW_CTRL_TX)
Matt Carlson8d018622007-12-20 20:05:44 -080010838 epause->tx_pause = 1;
10839 else
10840 epause->tx_pause = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010841}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010842
Linus Torvalds1da177e2005-04-16 15:20:36 -070010843static int tg3_set_pauseparam(struct net_device *dev, struct ethtool_pauseparam *epause)
10844{
10845 struct tg3 *tp = netdev_priv(dev);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010846 int err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010847
Joe Perches63c3a662011-04-26 08:12:10 +000010848 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson27121682010-02-17 15:16:57 +000010849 u32 newadv;
10850 struct phy_device *phydev;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010851
Matt Carlson27121682010-02-17 15:16:57 +000010852 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010853
Matt Carlson27121682010-02-17 15:16:57 +000010854 if (!(phydev->supported & SUPPORTED_Pause) ||
10855 (!(phydev->supported & SUPPORTED_Asym_Pause) &&
Nicolas Kaiser2259dca2010-10-07 23:29:27 +000010856 (epause->rx_pause != epause->tx_pause)))
Matt Carlson27121682010-02-17 15:16:57 +000010857 return -EINVAL;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010858
Matt Carlson27121682010-02-17 15:16:57 +000010859 tp->link_config.flowctrl = 0;
10860 if (epause->rx_pause) {
10861 tp->link_config.flowctrl |= FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010862
Matt Carlson27121682010-02-17 15:16:57 +000010863 if (epause->tx_pause) {
Steve Glendinninge18ce342008-12-16 02:00:00 -080010864 tp->link_config.flowctrl |= FLOW_CTRL_TX;
Matt Carlson27121682010-02-17 15:16:57 +000010865 newadv = ADVERTISED_Pause;
10866 } else
10867 newadv = ADVERTISED_Pause |
10868 ADVERTISED_Asym_Pause;
10869 } else if (epause->tx_pause) {
10870 tp->link_config.flowctrl |= FLOW_CTRL_TX;
10871 newadv = ADVERTISED_Asym_Pause;
10872 } else
10873 newadv = 0;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010874
Matt Carlson27121682010-02-17 15:16:57 +000010875 if (epause->autoneg)
Joe Perches63c3a662011-04-26 08:12:10 +000010876 tg3_flag_set(tp, PAUSE_AUTONEG);
Matt Carlson27121682010-02-17 15:16:57 +000010877 else
Joe Perches63c3a662011-04-26 08:12:10 +000010878 tg3_flag_clear(tp, PAUSE_AUTONEG);
Matt Carlson27121682010-02-17 15:16:57 +000010879
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010880 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) {
Matt Carlson27121682010-02-17 15:16:57 +000010881 u32 oldadv = phydev->advertising &
10882 (ADVERTISED_Pause | ADVERTISED_Asym_Pause);
10883 if (oldadv != newadv) {
10884 phydev->advertising &=
10885 ~(ADVERTISED_Pause |
10886 ADVERTISED_Asym_Pause);
10887 phydev->advertising |= newadv;
10888 if (phydev->autoneg) {
10889 /*
10890 * Always renegotiate the link to
10891 * inform our link partner of our
10892 * flow control settings, even if the
10893 * flow control is forced. Let
10894 * tg3_adjust_link() do the final
10895 * flow control setup.
10896 */
10897 return phy_start_aneg(phydev);
10898 }
10899 }
10900
10901 if (!epause->autoneg)
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010902 tg3_setup_flow_control(tp, 0, 0);
Matt Carlson27121682010-02-17 15:16:57 +000010903 } else {
Matt Carlsonc6700ce2012-02-13 15:20:15 +000010904 tp->link_config.advertising &=
Matt Carlson27121682010-02-17 15:16:57 +000010905 ~(ADVERTISED_Pause |
10906 ADVERTISED_Asym_Pause);
Matt Carlsonc6700ce2012-02-13 15:20:15 +000010907 tp->link_config.advertising |= newadv;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010908 }
10909 } else {
10910 int irq_sync = 0;
10911
10912 if (netif_running(dev)) {
10913 tg3_netif_stop(tp);
10914 irq_sync = 1;
10915 }
10916
10917 tg3_full_lock(tp, irq_sync);
10918
10919 if (epause->autoneg)
Joe Perches63c3a662011-04-26 08:12:10 +000010920 tg3_flag_set(tp, PAUSE_AUTONEG);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010921 else
Joe Perches63c3a662011-04-26 08:12:10 +000010922 tg3_flag_clear(tp, PAUSE_AUTONEG);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010923 if (epause->rx_pause)
Steve Glendinninge18ce342008-12-16 02:00:00 -080010924 tp->link_config.flowctrl |= FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010925 else
Steve Glendinninge18ce342008-12-16 02:00:00 -080010926 tp->link_config.flowctrl &= ~FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010927 if (epause->tx_pause)
Steve Glendinninge18ce342008-12-16 02:00:00 -080010928 tp->link_config.flowctrl |= FLOW_CTRL_TX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010929 else
Steve Glendinninge18ce342008-12-16 02:00:00 -080010930 tp->link_config.flowctrl &= ~FLOW_CTRL_TX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010931
10932 if (netif_running(dev)) {
10933 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
10934 err = tg3_restart_hw(tp, 1);
10935 if (!err)
10936 tg3_netif_start(tp);
10937 }
10938
10939 tg3_full_unlock(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -070010940 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010941
Michael Chanb9ec6c12006-07-25 16:37:27 -070010942 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010943}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010944
Matt Carlsonde6f31e2010-04-12 06:58:30 +000010945static int tg3_get_sset_count(struct net_device *dev, int sset)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010946{
Jeff Garzikb9f2c042007-10-03 18:07:32 -070010947 switch (sset) {
10948 case ETH_SS_TEST:
10949 return TG3_NUM_TEST;
10950 case ETH_SS_STATS:
10951 return TG3_NUM_STATS;
10952 default:
10953 return -EOPNOTSUPP;
10954 }
Michael Chan4cafd3f2005-05-29 14:56:34 -070010955}
10956
Matt Carlson90415472011-12-16 13:33:23 +000010957static int tg3_get_rxnfc(struct net_device *dev, struct ethtool_rxnfc *info,
10958 u32 *rules __always_unused)
10959{
10960 struct tg3 *tp = netdev_priv(dev);
10961
10962 if (!tg3_flag(tp, SUPPORT_MSIX))
10963 return -EOPNOTSUPP;
10964
10965 switch (info->cmd) {
10966 case ETHTOOL_GRXRINGS:
10967 if (netif_running(tp->dev))
10968 info->data = tp->irq_cnt;
10969 else {
10970 info->data = num_online_cpus();
10971 if (info->data > TG3_IRQ_MAX_VECS_RSS)
10972 info->data = TG3_IRQ_MAX_VECS_RSS;
10973 }
10974
10975 /* The first interrupt vector only
10976 * handles link interrupts.
10977 */
10978 info->data -= 1;
10979 return 0;
10980
10981 default:
10982 return -EOPNOTSUPP;
10983 }
10984}
10985
10986static u32 tg3_get_rxfh_indir_size(struct net_device *dev)
10987{
10988 u32 size = 0;
10989 struct tg3 *tp = netdev_priv(dev);
10990
10991 if (tg3_flag(tp, SUPPORT_MSIX))
10992 size = TG3_RSS_INDIR_TBL_SIZE;
10993
10994 return size;
10995}
10996
10997static int tg3_get_rxfh_indir(struct net_device *dev, u32 *indir)
10998{
10999 struct tg3 *tp = netdev_priv(dev);
11000 int i;
11001
11002 for (i = 0; i < TG3_RSS_INDIR_TBL_SIZE; i++)
11003 indir[i] = tp->rss_ind_tbl[i];
11004
11005 return 0;
11006}
11007
11008static int tg3_set_rxfh_indir(struct net_device *dev, const u32 *indir)
11009{
11010 struct tg3 *tp = netdev_priv(dev);
11011 size_t i;
11012
11013 for (i = 0; i < TG3_RSS_INDIR_TBL_SIZE; i++)
11014 tp->rss_ind_tbl[i] = indir[i];
11015
11016 if (!netif_running(dev) || !tg3_flag(tp, ENABLE_RSS))
11017 return 0;
11018
11019 /* It is legal to write the indirection
11020 * table while the device is running.
11021 */
11022 tg3_full_lock(tp, 0);
11023 tg3_rss_write_indir_tbl(tp);
11024 tg3_full_unlock(tp);
11025
11026 return 0;
11027}
11028
Matt Carlsonde6f31e2010-04-12 06:58:30 +000011029static void tg3_get_strings(struct net_device *dev, u32 stringset, u8 *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011030{
11031 switch (stringset) {
11032 case ETH_SS_STATS:
11033 memcpy(buf, &ethtool_stats_keys, sizeof(ethtool_stats_keys));
11034 break;
Michael Chan4cafd3f2005-05-29 14:56:34 -070011035 case ETH_SS_TEST:
11036 memcpy(buf, &ethtool_test_keys, sizeof(ethtool_test_keys));
11037 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011038 default:
11039 WARN_ON(1); /* we need a WARN() */
11040 break;
11041 }
11042}
11043
stephen hemminger81b87092011-04-04 08:43:50 +000011044static int tg3_set_phys_id(struct net_device *dev,
11045 enum ethtool_phys_id_state state)
Michael Chan4009a932005-09-05 17:52:54 -070011046{
11047 struct tg3 *tp = netdev_priv(dev);
Michael Chan4009a932005-09-05 17:52:54 -070011048
11049 if (!netif_running(tp->dev))
11050 return -EAGAIN;
11051
stephen hemminger81b87092011-04-04 08:43:50 +000011052 switch (state) {
11053 case ETHTOOL_ID_ACTIVE:
Allan, Bruce Wfce55922011-04-13 13:09:10 +000011054 return 1; /* cycle on/off once per second */
Michael Chan4009a932005-09-05 17:52:54 -070011055
stephen hemminger81b87092011-04-04 08:43:50 +000011056 case ETHTOOL_ID_ON:
11057 tw32(MAC_LED_CTRL, LED_CTRL_LNKLED_OVERRIDE |
11058 LED_CTRL_1000MBPS_ON |
11059 LED_CTRL_100MBPS_ON |
11060 LED_CTRL_10MBPS_ON |
11061 LED_CTRL_TRAFFIC_OVERRIDE |
11062 LED_CTRL_TRAFFIC_BLINK |
11063 LED_CTRL_TRAFFIC_LED);
11064 break;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011065
stephen hemminger81b87092011-04-04 08:43:50 +000011066 case ETHTOOL_ID_OFF:
11067 tw32(MAC_LED_CTRL, LED_CTRL_LNKLED_OVERRIDE |
11068 LED_CTRL_TRAFFIC_OVERRIDE);
11069 break;
Michael Chan4009a932005-09-05 17:52:54 -070011070
stephen hemminger81b87092011-04-04 08:43:50 +000011071 case ETHTOOL_ID_INACTIVE:
11072 tw32(MAC_LED_CTRL, tp->led_ctrl);
11073 break;
Michael Chan4009a932005-09-05 17:52:54 -070011074 }
stephen hemminger81b87092011-04-04 08:43:50 +000011075
Michael Chan4009a932005-09-05 17:52:54 -070011076 return 0;
11077}
11078
Matt Carlsonde6f31e2010-04-12 06:58:30 +000011079static void tg3_get_ethtool_stats(struct net_device *dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -070011080 struct ethtool_stats *estats, u64 *tmp_stats)
11081{
11082 struct tg3 *tp = netdev_priv(dev);
Matt Carlson0e6c9da2011-12-08 14:40:13 +000011083
Matt Carlsonb546e462012-02-13 15:20:09 +000011084 if (tp->hw_stats)
11085 tg3_get_estats(tp, (struct tg3_ethtool_stats *)tmp_stats);
11086 else
11087 memset(tmp_stats, 0, sizeof(struct tg3_ethtool_stats));
Linus Torvalds1da177e2005-04-16 15:20:36 -070011088}
11089
Matt Carlson535a4902011-07-20 10:20:56 +000011090static __be32 *tg3_vpd_readblock(struct tg3 *tp, u32 *vpdlen)
Matt Carlsonc3e94502011-04-13 11:05:08 +000011091{
11092 int i;
11093 __be32 *buf;
11094 u32 offset = 0, len = 0;
11095 u32 magic, val;
11096
Joe Perches63c3a662011-04-26 08:12:10 +000011097 if (tg3_flag(tp, NO_NVRAM) || tg3_nvram_read(tp, 0, &magic))
Matt Carlsonc3e94502011-04-13 11:05:08 +000011098 return NULL;
11099
11100 if (magic == TG3_EEPROM_MAGIC) {
11101 for (offset = TG3_NVM_DIR_START;
11102 offset < TG3_NVM_DIR_END;
11103 offset += TG3_NVM_DIRENT_SIZE) {
11104 if (tg3_nvram_read(tp, offset, &val))
11105 return NULL;
11106
11107 if ((val >> TG3_NVM_DIRTYPE_SHIFT) ==
11108 TG3_NVM_DIRTYPE_EXTVPD)
11109 break;
11110 }
11111
11112 if (offset != TG3_NVM_DIR_END) {
11113 len = (val & TG3_NVM_DIRTYPE_LENMSK) * 4;
11114 if (tg3_nvram_read(tp, offset + 4, &offset))
11115 return NULL;
11116
11117 offset = tg3_nvram_logical_addr(tp, offset);
11118 }
11119 }
11120
11121 if (!offset || !len) {
11122 offset = TG3_NVM_VPD_OFF;
11123 len = TG3_NVM_VPD_LEN;
11124 }
11125
11126 buf = kmalloc(len, GFP_KERNEL);
11127 if (buf == NULL)
11128 return NULL;
11129
11130 if (magic == TG3_EEPROM_MAGIC) {
11131 for (i = 0; i < len; i += 4) {
11132 /* The data is in little-endian format in NVRAM.
11133 * Use the big-endian read routines to preserve
11134 * the byte order as it exists in NVRAM.
11135 */
11136 if (tg3_nvram_read_be32(tp, offset + i, &buf[i/4]))
11137 goto error;
11138 }
11139 } else {
11140 u8 *ptr;
11141 ssize_t cnt;
11142 unsigned int pos = 0;
11143
11144 ptr = (u8 *)&buf[0];
11145 for (i = 0; pos < len && i < 3; i++, pos += cnt, ptr += cnt) {
11146 cnt = pci_read_vpd(tp->pdev, pos,
11147 len - pos, ptr);
11148 if (cnt == -ETIMEDOUT || cnt == -EINTR)
11149 cnt = 0;
11150 else if (cnt < 0)
11151 goto error;
11152 }
11153 if (pos != len)
11154 goto error;
11155 }
11156
Matt Carlson535a4902011-07-20 10:20:56 +000011157 *vpdlen = len;
11158
Matt Carlsonc3e94502011-04-13 11:05:08 +000011159 return buf;
11160
11161error:
11162 kfree(buf);
11163 return NULL;
11164}
11165
Michael Chan566f86a2005-05-29 14:56:58 -070011166#define NVRAM_TEST_SIZE 0x100
Matt Carlsona5767de2007-11-12 21:10:58 -080011167#define NVRAM_SELFBOOT_FORMAT1_0_SIZE 0x14
11168#define NVRAM_SELFBOOT_FORMAT1_2_SIZE 0x18
11169#define NVRAM_SELFBOOT_FORMAT1_3_SIZE 0x1c
Matt Carlson727a6d92011-06-13 13:38:58 +000011170#define NVRAM_SELFBOOT_FORMAT1_4_SIZE 0x20
11171#define NVRAM_SELFBOOT_FORMAT1_5_SIZE 0x24
Matt Carlsonbda18fa2011-07-20 10:20:57 +000011172#define NVRAM_SELFBOOT_FORMAT1_6_SIZE 0x50
Michael Chanb16250e2006-09-27 16:10:14 -070011173#define NVRAM_SELFBOOT_HW_SIZE 0x20
11174#define NVRAM_SELFBOOT_DATA_SIZE 0x1c
Michael Chan566f86a2005-05-29 14:56:58 -070011175
11176static int tg3_test_nvram(struct tg3 *tp)
11177{
Matt Carlson535a4902011-07-20 10:20:56 +000011178 u32 csum, magic, len;
Matt Carlsona9dc5292009-02-25 14:25:30 +000011179 __be32 *buf;
Andy Gospodarekab0049b2007-09-06 20:42:14 +010011180 int i, j, k, err = 0, size;
Michael Chan566f86a2005-05-29 14:56:58 -070011181
Joe Perches63c3a662011-04-26 08:12:10 +000011182 if (tg3_flag(tp, NO_NVRAM))
Matt Carlsondf259d82009-04-20 06:57:14 +000011183 return 0;
11184
Matt Carlsone4f34112009-02-25 14:25:00 +000011185 if (tg3_nvram_read(tp, 0, &magic) != 0)
Michael Chan1b277772006-03-20 22:27:48 -080011186 return -EIO;
11187
Michael Chan1b277772006-03-20 22:27:48 -080011188 if (magic == TG3_EEPROM_MAGIC)
11189 size = NVRAM_TEST_SIZE;
Michael Chanb16250e2006-09-27 16:10:14 -070011190 else if ((magic & TG3_EEPROM_MAGIC_FW_MSK) == TG3_EEPROM_MAGIC_FW) {
Matt Carlsona5767de2007-11-12 21:10:58 -080011191 if ((magic & TG3_EEPROM_SB_FORMAT_MASK) ==
11192 TG3_EEPROM_SB_FORMAT_1) {
11193 switch (magic & TG3_EEPROM_SB_REVISION_MASK) {
11194 case TG3_EEPROM_SB_REVISION_0:
11195 size = NVRAM_SELFBOOT_FORMAT1_0_SIZE;
11196 break;
11197 case TG3_EEPROM_SB_REVISION_2:
11198 size = NVRAM_SELFBOOT_FORMAT1_2_SIZE;
11199 break;
11200 case TG3_EEPROM_SB_REVISION_3:
11201 size = NVRAM_SELFBOOT_FORMAT1_3_SIZE;
11202 break;
Matt Carlson727a6d92011-06-13 13:38:58 +000011203 case TG3_EEPROM_SB_REVISION_4:
11204 size = NVRAM_SELFBOOT_FORMAT1_4_SIZE;
11205 break;
11206 case TG3_EEPROM_SB_REVISION_5:
11207 size = NVRAM_SELFBOOT_FORMAT1_5_SIZE;
11208 break;
11209 case TG3_EEPROM_SB_REVISION_6:
11210 size = NVRAM_SELFBOOT_FORMAT1_6_SIZE;
11211 break;
Matt Carlsona5767de2007-11-12 21:10:58 -080011212 default:
Matt Carlson727a6d92011-06-13 13:38:58 +000011213 return -EIO;
Matt Carlsona5767de2007-11-12 21:10:58 -080011214 }
11215 } else
Michael Chan1b277772006-03-20 22:27:48 -080011216 return 0;
Michael Chanb16250e2006-09-27 16:10:14 -070011217 } else if ((magic & TG3_EEPROM_MAGIC_HW_MSK) == TG3_EEPROM_MAGIC_HW)
11218 size = NVRAM_SELFBOOT_HW_SIZE;
11219 else
Michael Chan1b277772006-03-20 22:27:48 -080011220 return -EIO;
11221
11222 buf = kmalloc(size, GFP_KERNEL);
Michael Chan566f86a2005-05-29 14:56:58 -070011223 if (buf == NULL)
11224 return -ENOMEM;
11225
Michael Chan1b277772006-03-20 22:27:48 -080011226 err = -EIO;
11227 for (i = 0, j = 0; i < size; i += 4, j++) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000011228 err = tg3_nvram_read_be32(tp, i, &buf[j]);
11229 if (err)
Michael Chan566f86a2005-05-29 14:56:58 -070011230 break;
Michael Chan566f86a2005-05-29 14:56:58 -070011231 }
Michael Chan1b277772006-03-20 22:27:48 -080011232 if (i < size)
Michael Chan566f86a2005-05-29 14:56:58 -070011233 goto out;
11234
Michael Chan1b277772006-03-20 22:27:48 -080011235 /* Selfboot format */
Matt Carlsona9dc5292009-02-25 14:25:30 +000011236 magic = be32_to_cpu(buf[0]);
Al Virob9fc7dc2007-12-17 22:59:57 -080011237 if ((magic & TG3_EEPROM_MAGIC_FW_MSK) ==
Michael Chanb16250e2006-09-27 16:10:14 -070011238 TG3_EEPROM_MAGIC_FW) {
Michael Chan1b277772006-03-20 22:27:48 -080011239 u8 *buf8 = (u8 *) buf, csum8 = 0;
11240
Al Virob9fc7dc2007-12-17 22:59:57 -080011241 if ((magic & TG3_EEPROM_SB_REVISION_MASK) ==
Matt Carlsona5767de2007-11-12 21:10:58 -080011242 TG3_EEPROM_SB_REVISION_2) {
11243 /* For rev 2, the csum doesn't include the MBA. */
11244 for (i = 0; i < TG3_EEPROM_SB_F1R2_MBA_OFF; i++)
11245 csum8 += buf8[i];
11246 for (i = TG3_EEPROM_SB_F1R2_MBA_OFF + 4; i < size; i++)
11247 csum8 += buf8[i];
11248 } else {
11249 for (i = 0; i < size; i++)
11250 csum8 += buf8[i];
11251 }
Michael Chan1b277772006-03-20 22:27:48 -080011252
Adrian Bunkad96b482006-04-05 22:21:04 -070011253 if (csum8 == 0) {
11254 err = 0;
11255 goto out;
11256 }
11257
11258 err = -EIO;
11259 goto out;
Michael Chan1b277772006-03-20 22:27:48 -080011260 }
Michael Chan566f86a2005-05-29 14:56:58 -070011261
Al Virob9fc7dc2007-12-17 22:59:57 -080011262 if ((magic & TG3_EEPROM_MAGIC_HW_MSK) ==
Michael Chanb16250e2006-09-27 16:10:14 -070011263 TG3_EEPROM_MAGIC_HW) {
11264 u8 data[NVRAM_SELFBOOT_DATA_SIZE];
Matt Carlsona9dc5292009-02-25 14:25:30 +000011265 u8 parity[NVRAM_SELFBOOT_DATA_SIZE];
Michael Chanb16250e2006-09-27 16:10:14 -070011266 u8 *buf8 = (u8 *) buf;
Michael Chanb16250e2006-09-27 16:10:14 -070011267
11268 /* Separate the parity bits and the data bytes. */
11269 for (i = 0, j = 0, k = 0; i < NVRAM_SELFBOOT_HW_SIZE; i++) {
11270 if ((i == 0) || (i == 8)) {
11271 int l;
11272 u8 msk;
11273
11274 for (l = 0, msk = 0x80; l < 7; l++, msk >>= 1)
11275 parity[k++] = buf8[i] & msk;
11276 i++;
Matt Carlson859a588792010-04-05 10:19:28 +000011277 } else if (i == 16) {
Michael Chanb16250e2006-09-27 16:10:14 -070011278 int l;
11279 u8 msk;
11280
11281 for (l = 0, msk = 0x20; l < 6; l++, msk >>= 1)
11282 parity[k++] = buf8[i] & msk;
11283 i++;
11284
11285 for (l = 0, msk = 0x80; l < 8; l++, msk >>= 1)
11286 parity[k++] = buf8[i] & msk;
11287 i++;
11288 }
11289 data[j++] = buf8[i];
11290 }
11291
11292 err = -EIO;
11293 for (i = 0; i < NVRAM_SELFBOOT_DATA_SIZE; i++) {
11294 u8 hw8 = hweight8(data[i]);
11295
11296 if ((hw8 & 0x1) && parity[i])
11297 goto out;
11298 else if (!(hw8 & 0x1) && !parity[i])
11299 goto out;
11300 }
11301 err = 0;
11302 goto out;
11303 }
11304
Matt Carlson01c3a392011-03-09 16:58:20 +000011305 err = -EIO;
11306
Michael Chan566f86a2005-05-29 14:56:58 -070011307 /* Bootstrap checksum at offset 0x10 */
11308 csum = calc_crc((unsigned char *) buf, 0x10);
Matt Carlson01c3a392011-03-09 16:58:20 +000011309 if (csum != le32_to_cpu(buf[0x10/4]))
Michael Chan566f86a2005-05-29 14:56:58 -070011310 goto out;
11311
11312 /* Manufacturing block starts at offset 0x74, checksum at 0xfc */
11313 csum = calc_crc((unsigned char *) &buf[0x74/4], 0x88);
Matt Carlson01c3a392011-03-09 16:58:20 +000011314 if (csum != le32_to_cpu(buf[0xfc/4]))
Matt Carlsona9dc5292009-02-25 14:25:30 +000011315 goto out;
Michael Chan566f86a2005-05-29 14:56:58 -070011316
Matt Carlsonc3e94502011-04-13 11:05:08 +000011317 kfree(buf);
11318
Matt Carlson535a4902011-07-20 10:20:56 +000011319 buf = tg3_vpd_readblock(tp, &len);
Matt Carlsonc3e94502011-04-13 11:05:08 +000011320 if (!buf)
11321 return -ENOMEM;
Matt Carlsond4894f32011-03-09 16:58:21 +000011322
Matt Carlson535a4902011-07-20 10:20:56 +000011323 i = pci_vpd_find_tag((u8 *)buf, 0, len, PCI_VPD_LRDT_RO_DATA);
Matt Carlsond4894f32011-03-09 16:58:21 +000011324 if (i > 0) {
11325 j = pci_vpd_lrdt_size(&((u8 *)buf)[i]);
11326 if (j < 0)
11327 goto out;
11328
Matt Carlson535a4902011-07-20 10:20:56 +000011329 if (i + PCI_VPD_LRDT_TAG_SIZE + j > len)
Matt Carlsond4894f32011-03-09 16:58:21 +000011330 goto out;
11331
11332 i += PCI_VPD_LRDT_TAG_SIZE;
11333 j = pci_vpd_find_info_keyword((u8 *)buf, i, j,
11334 PCI_VPD_RO_KEYWORD_CHKSUM);
11335 if (j > 0) {
11336 u8 csum8 = 0;
11337
11338 j += PCI_VPD_INFO_FLD_HDR_SIZE;
11339
11340 for (i = 0; i <= j; i++)
11341 csum8 += ((u8 *)buf)[i];
11342
11343 if (csum8)
11344 goto out;
11345 }
11346 }
11347
Michael Chan566f86a2005-05-29 14:56:58 -070011348 err = 0;
11349
11350out:
11351 kfree(buf);
11352 return err;
11353}
11354
Michael Chanca430072005-05-29 14:57:23 -070011355#define TG3_SERDES_TIMEOUT_SEC 2
11356#define TG3_COPPER_TIMEOUT_SEC 6
11357
11358static int tg3_test_link(struct tg3 *tp)
11359{
11360 int i, max;
11361
11362 if (!netif_running(tp->dev))
11363 return -ENODEV;
11364
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011365 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
Michael Chanca430072005-05-29 14:57:23 -070011366 max = TG3_SERDES_TIMEOUT_SEC;
11367 else
11368 max = TG3_COPPER_TIMEOUT_SEC;
11369
11370 for (i = 0; i < max; i++) {
11371 if (netif_carrier_ok(tp->dev))
11372 return 0;
11373
11374 if (msleep_interruptible(1000))
11375 break;
11376 }
11377
11378 return -EIO;
11379}
11380
Michael Chana71116d2005-05-29 14:58:11 -070011381/* Only test the commonly used registers */
David S. Miller30ca3e32006-03-20 23:02:36 -080011382static int tg3_test_registers(struct tg3 *tp)
Michael Chana71116d2005-05-29 14:58:11 -070011383{
Michael Chanb16250e2006-09-27 16:10:14 -070011384 int i, is_5705, is_5750;
Michael Chana71116d2005-05-29 14:58:11 -070011385 u32 offset, read_mask, write_mask, val, save_val, read_val;
11386 static struct {
11387 u16 offset;
11388 u16 flags;
11389#define TG3_FL_5705 0x1
11390#define TG3_FL_NOT_5705 0x2
11391#define TG3_FL_NOT_5788 0x4
Michael Chanb16250e2006-09-27 16:10:14 -070011392#define TG3_FL_NOT_5750 0x8
Michael Chana71116d2005-05-29 14:58:11 -070011393 u32 read_mask;
11394 u32 write_mask;
11395 } reg_tbl[] = {
11396 /* MAC Control Registers */
11397 { MAC_MODE, TG3_FL_NOT_5705,
11398 0x00000000, 0x00ef6f8c },
11399 { MAC_MODE, TG3_FL_5705,
11400 0x00000000, 0x01ef6b8c },
11401 { MAC_STATUS, TG3_FL_NOT_5705,
11402 0x03800107, 0x00000000 },
11403 { MAC_STATUS, TG3_FL_5705,
11404 0x03800100, 0x00000000 },
11405 { MAC_ADDR_0_HIGH, 0x0000,
11406 0x00000000, 0x0000ffff },
11407 { MAC_ADDR_0_LOW, 0x0000,
Matt Carlsonc6cdf432010-04-05 10:19:26 +000011408 0x00000000, 0xffffffff },
Michael Chana71116d2005-05-29 14:58:11 -070011409 { MAC_RX_MTU_SIZE, 0x0000,
11410 0x00000000, 0x0000ffff },
11411 { MAC_TX_MODE, 0x0000,
11412 0x00000000, 0x00000070 },
11413 { MAC_TX_LENGTHS, 0x0000,
11414 0x00000000, 0x00003fff },
11415 { MAC_RX_MODE, TG3_FL_NOT_5705,
11416 0x00000000, 0x000007fc },
11417 { MAC_RX_MODE, TG3_FL_5705,
11418 0x00000000, 0x000007dc },
11419 { MAC_HASH_REG_0, 0x0000,
11420 0x00000000, 0xffffffff },
11421 { MAC_HASH_REG_1, 0x0000,
11422 0x00000000, 0xffffffff },
11423 { MAC_HASH_REG_2, 0x0000,
11424 0x00000000, 0xffffffff },
11425 { MAC_HASH_REG_3, 0x0000,
11426 0x00000000, 0xffffffff },
11427
11428 /* Receive Data and Receive BD Initiator Control Registers. */
11429 { RCVDBDI_JUMBO_BD+0, TG3_FL_NOT_5705,
11430 0x00000000, 0xffffffff },
11431 { RCVDBDI_JUMBO_BD+4, TG3_FL_NOT_5705,
11432 0x00000000, 0xffffffff },
11433 { RCVDBDI_JUMBO_BD+8, TG3_FL_NOT_5705,
11434 0x00000000, 0x00000003 },
11435 { RCVDBDI_JUMBO_BD+0xc, TG3_FL_NOT_5705,
11436 0x00000000, 0xffffffff },
11437 { RCVDBDI_STD_BD+0, 0x0000,
11438 0x00000000, 0xffffffff },
11439 { RCVDBDI_STD_BD+4, 0x0000,
11440 0x00000000, 0xffffffff },
11441 { RCVDBDI_STD_BD+8, 0x0000,
11442 0x00000000, 0xffff0002 },
11443 { RCVDBDI_STD_BD+0xc, 0x0000,
11444 0x00000000, 0xffffffff },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011445
Michael Chana71116d2005-05-29 14:58:11 -070011446 /* Receive BD Initiator Control Registers. */
11447 { RCVBDI_STD_THRESH, TG3_FL_NOT_5705,
11448 0x00000000, 0xffffffff },
11449 { RCVBDI_STD_THRESH, TG3_FL_5705,
11450 0x00000000, 0x000003ff },
11451 { RCVBDI_JUMBO_THRESH, TG3_FL_NOT_5705,
11452 0x00000000, 0xffffffff },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011453
Michael Chana71116d2005-05-29 14:58:11 -070011454 /* Host Coalescing Control Registers. */
11455 { HOSTCC_MODE, TG3_FL_NOT_5705,
11456 0x00000000, 0x00000004 },
11457 { HOSTCC_MODE, TG3_FL_5705,
11458 0x00000000, 0x000000f6 },
11459 { HOSTCC_RXCOL_TICKS, TG3_FL_NOT_5705,
11460 0x00000000, 0xffffffff },
11461 { HOSTCC_RXCOL_TICKS, TG3_FL_5705,
11462 0x00000000, 0x000003ff },
11463 { HOSTCC_TXCOL_TICKS, TG3_FL_NOT_5705,
11464 0x00000000, 0xffffffff },
11465 { HOSTCC_TXCOL_TICKS, TG3_FL_5705,
11466 0x00000000, 0x000003ff },
11467 { HOSTCC_RXMAX_FRAMES, TG3_FL_NOT_5705,
11468 0x00000000, 0xffffffff },
11469 { HOSTCC_RXMAX_FRAMES, TG3_FL_5705 | TG3_FL_NOT_5788,
11470 0x00000000, 0x000000ff },
11471 { HOSTCC_TXMAX_FRAMES, TG3_FL_NOT_5705,
11472 0x00000000, 0xffffffff },
11473 { HOSTCC_TXMAX_FRAMES, TG3_FL_5705 | TG3_FL_NOT_5788,
11474 0x00000000, 0x000000ff },
11475 { HOSTCC_RXCOAL_TICK_INT, TG3_FL_NOT_5705,
11476 0x00000000, 0xffffffff },
11477 { HOSTCC_TXCOAL_TICK_INT, TG3_FL_NOT_5705,
11478 0x00000000, 0xffffffff },
11479 { HOSTCC_RXCOAL_MAXF_INT, TG3_FL_NOT_5705,
11480 0x00000000, 0xffffffff },
11481 { HOSTCC_RXCOAL_MAXF_INT, TG3_FL_5705 | TG3_FL_NOT_5788,
11482 0x00000000, 0x000000ff },
11483 { HOSTCC_TXCOAL_MAXF_INT, TG3_FL_NOT_5705,
11484 0x00000000, 0xffffffff },
11485 { HOSTCC_TXCOAL_MAXF_INT, TG3_FL_5705 | TG3_FL_NOT_5788,
11486 0x00000000, 0x000000ff },
11487 { HOSTCC_STAT_COAL_TICKS, TG3_FL_NOT_5705,
11488 0x00000000, 0xffffffff },
11489 { HOSTCC_STATS_BLK_HOST_ADDR, TG3_FL_NOT_5705,
11490 0x00000000, 0xffffffff },
11491 { HOSTCC_STATS_BLK_HOST_ADDR+4, TG3_FL_NOT_5705,
11492 0x00000000, 0xffffffff },
11493 { HOSTCC_STATUS_BLK_HOST_ADDR, 0x0000,
11494 0x00000000, 0xffffffff },
11495 { HOSTCC_STATUS_BLK_HOST_ADDR+4, 0x0000,
11496 0x00000000, 0xffffffff },
11497 { HOSTCC_STATS_BLK_NIC_ADDR, 0x0000,
11498 0xffffffff, 0x00000000 },
11499 { HOSTCC_STATUS_BLK_NIC_ADDR, 0x0000,
11500 0xffffffff, 0x00000000 },
11501
11502 /* Buffer Manager Control Registers. */
Michael Chanb16250e2006-09-27 16:10:14 -070011503 { BUFMGR_MB_POOL_ADDR, TG3_FL_NOT_5750,
Michael Chana71116d2005-05-29 14:58:11 -070011504 0x00000000, 0x007fff80 },
Michael Chanb16250e2006-09-27 16:10:14 -070011505 { BUFMGR_MB_POOL_SIZE, TG3_FL_NOT_5750,
Michael Chana71116d2005-05-29 14:58:11 -070011506 0x00000000, 0x007fffff },
11507 { BUFMGR_MB_RDMA_LOW_WATER, 0x0000,
11508 0x00000000, 0x0000003f },
11509 { BUFMGR_MB_MACRX_LOW_WATER, 0x0000,
11510 0x00000000, 0x000001ff },
11511 { BUFMGR_MB_HIGH_WATER, 0x0000,
11512 0x00000000, 0x000001ff },
11513 { BUFMGR_DMA_DESC_POOL_ADDR, TG3_FL_NOT_5705,
11514 0xffffffff, 0x00000000 },
11515 { BUFMGR_DMA_DESC_POOL_SIZE, TG3_FL_NOT_5705,
11516 0xffffffff, 0x00000000 },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011517
Michael Chana71116d2005-05-29 14:58:11 -070011518 /* Mailbox Registers */
11519 { GRCMBOX_RCVSTD_PROD_IDX+4, 0x0000,
11520 0x00000000, 0x000001ff },
11521 { GRCMBOX_RCVJUMBO_PROD_IDX+4, TG3_FL_NOT_5705,
11522 0x00000000, 0x000001ff },
11523 { GRCMBOX_RCVRET_CON_IDX_0+4, 0x0000,
11524 0x00000000, 0x000007ff },
11525 { GRCMBOX_SNDHOST_PROD_IDX_0+4, 0x0000,
11526 0x00000000, 0x000001ff },
11527
11528 { 0xffff, 0x0000, 0x00000000, 0x00000000 },
11529 };
11530
Michael Chanb16250e2006-09-27 16:10:14 -070011531 is_5705 = is_5750 = 0;
Joe Perches63c3a662011-04-26 08:12:10 +000011532 if (tg3_flag(tp, 5705_PLUS)) {
Michael Chana71116d2005-05-29 14:58:11 -070011533 is_5705 = 1;
Joe Perches63c3a662011-04-26 08:12:10 +000011534 if (tg3_flag(tp, 5750_PLUS))
Michael Chanb16250e2006-09-27 16:10:14 -070011535 is_5750 = 1;
11536 }
Michael Chana71116d2005-05-29 14:58:11 -070011537
11538 for (i = 0; reg_tbl[i].offset != 0xffff; i++) {
11539 if (is_5705 && (reg_tbl[i].flags & TG3_FL_NOT_5705))
11540 continue;
11541
11542 if (!is_5705 && (reg_tbl[i].flags & TG3_FL_5705))
11543 continue;
11544
Joe Perches63c3a662011-04-26 08:12:10 +000011545 if (tg3_flag(tp, IS_5788) &&
Michael Chana71116d2005-05-29 14:58:11 -070011546 (reg_tbl[i].flags & TG3_FL_NOT_5788))
11547 continue;
11548
Michael Chanb16250e2006-09-27 16:10:14 -070011549 if (is_5750 && (reg_tbl[i].flags & TG3_FL_NOT_5750))
11550 continue;
11551
Michael Chana71116d2005-05-29 14:58:11 -070011552 offset = (u32) reg_tbl[i].offset;
11553 read_mask = reg_tbl[i].read_mask;
11554 write_mask = reg_tbl[i].write_mask;
11555
11556 /* Save the original register content */
11557 save_val = tr32(offset);
11558
11559 /* Determine the read-only value. */
11560 read_val = save_val & read_mask;
11561
11562 /* Write zero to the register, then make sure the read-only bits
11563 * are not changed and the read/write bits are all zeros.
11564 */
11565 tw32(offset, 0);
11566
11567 val = tr32(offset);
11568
11569 /* Test the read-only and read/write bits. */
11570 if (((val & read_mask) != read_val) || (val & write_mask))
11571 goto out;
11572
11573 /* Write ones to all the bits defined by RdMask and WrMask, then
11574 * make sure the read-only bits are not changed and the
11575 * read/write bits are all ones.
11576 */
11577 tw32(offset, read_mask | write_mask);
11578
11579 val = tr32(offset);
11580
11581 /* Test the read-only bits. */
11582 if ((val & read_mask) != read_val)
11583 goto out;
11584
11585 /* Test the read/write bits. */
11586 if ((val & write_mask) != write_mask)
11587 goto out;
11588
11589 tw32(offset, save_val);
11590 }
11591
11592 return 0;
11593
11594out:
Michael Chan9f88f292006-12-07 00:22:54 -080011595 if (netif_msg_hw(tp))
Matt Carlson2445e462010-04-05 10:19:21 +000011596 netdev_err(tp->dev,
11597 "Register test failed at offset %x\n", offset);
Michael Chana71116d2005-05-29 14:58:11 -070011598 tw32(offset, save_val);
11599 return -EIO;
11600}
11601
Michael Chan7942e1d2005-05-29 14:58:36 -070011602static int tg3_do_mem_test(struct tg3 *tp, u32 offset, u32 len)
11603{
Arjan van de Venf71e1302006-03-03 21:33:57 -050011604 static const u32 test_pattern[] = { 0x00000000, 0xffffffff, 0xaa55a55a };
Michael Chan7942e1d2005-05-29 14:58:36 -070011605 int i;
11606 u32 j;
11607
Alejandro Martinez Ruize9edda62007-10-15 03:37:43 +020011608 for (i = 0; i < ARRAY_SIZE(test_pattern); i++) {
Michael Chan7942e1d2005-05-29 14:58:36 -070011609 for (j = 0; j < len; j += 4) {
11610 u32 val;
11611
11612 tg3_write_mem(tp, offset + j, test_pattern[i]);
11613 tg3_read_mem(tp, offset + j, &val);
11614 if (val != test_pattern[i])
11615 return -EIO;
11616 }
11617 }
11618 return 0;
11619}
11620
11621static int tg3_test_memory(struct tg3 *tp)
11622{
11623 static struct mem_entry {
11624 u32 offset;
11625 u32 len;
11626 } mem_tbl_570x[] = {
Michael Chan38690192005-12-19 16:27:28 -080011627 { 0x00000000, 0x00b50},
Michael Chan7942e1d2005-05-29 14:58:36 -070011628 { 0x00002000, 0x1c000},
11629 { 0xffffffff, 0x00000}
11630 }, mem_tbl_5705[] = {
11631 { 0x00000100, 0x0000c},
11632 { 0x00000200, 0x00008},
Michael Chan7942e1d2005-05-29 14:58:36 -070011633 { 0x00004000, 0x00800},
11634 { 0x00006000, 0x01000},
11635 { 0x00008000, 0x02000},
11636 { 0x00010000, 0x0e000},
11637 { 0xffffffff, 0x00000}
Michael Chan79f4d132006-03-20 22:28:57 -080011638 }, mem_tbl_5755[] = {
11639 { 0x00000200, 0x00008},
11640 { 0x00004000, 0x00800},
11641 { 0x00006000, 0x00800},
11642 { 0x00008000, 0x02000},
11643 { 0x00010000, 0x0c000},
11644 { 0xffffffff, 0x00000}
Michael Chanb16250e2006-09-27 16:10:14 -070011645 }, mem_tbl_5906[] = {
11646 { 0x00000200, 0x00008},
11647 { 0x00004000, 0x00400},
11648 { 0x00006000, 0x00400},
11649 { 0x00008000, 0x01000},
11650 { 0x00010000, 0x01000},
11651 { 0xffffffff, 0x00000}
Matt Carlson8b5a6c42010-01-20 16:58:06 +000011652 }, mem_tbl_5717[] = {
11653 { 0x00000200, 0x00008},
11654 { 0x00010000, 0x0a000},
11655 { 0x00020000, 0x13c00},
11656 { 0xffffffff, 0x00000}
11657 }, mem_tbl_57765[] = {
11658 { 0x00000200, 0x00008},
11659 { 0x00004000, 0x00800},
11660 { 0x00006000, 0x09800},
11661 { 0x00010000, 0x0a000},
11662 { 0xffffffff, 0x00000}
Michael Chan7942e1d2005-05-29 14:58:36 -070011663 };
11664 struct mem_entry *mem_tbl;
11665 int err = 0;
11666 int i;
11667
Joe Perches63c3a662011-04-26 08:12:10 +000011668 if (tg3_flag(tp, 5717_PLUS))
Matt Carlson8b5a6c42010-01-20 16:58:06 +000011669 mem_tbl = mem_tbl_5717;
Matt Carlson55086ad2011-12-14 11:09:59 +000011670 else if (tg3_flag(tp, 57765_CLASS))
Matt Carlson8b5a6c42010-01-20 16:58:06 +000011671 mem_tbl = mem_tbl_57765;
Joe Perches63c3a662011-04-26 08:12:10 +000011672 else if (tg3_flag(tp, 5755_PLUS))
Matt Carlson321d32a2008-11-21 17:22:19 -080011673 mem_tbl = mem_tbl_5755;
11674 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
11675 mem_tbl = mem_tbl_5906;
Joe Perches63c3a662011-04-26 08:12:10 +000011676 else if (tg3_flag(tp, 5705_PLUS))
Matt Carlson321d32a2008-11-21 17:22:19 -080011677 mem_tbl = mem_tbl_5705;
11678 else
Michael Chan7942e1d2005-05-29 14:58:36 -070011679 mem_tbl = mem_tbl_570x;
11680
11681 for (i = 0; mem_tbl[i].offset != 0xffffffff; i++) {
Matt Carlsonbe98da62010-07-11 09:31:46 +000011682 err = tg3_do_mem_test(tp, mem_tbl[i].offset, mem_tbl[i].len);
11683 if (err)
Michael Chan7942e1d2005-05-29 14:58:36 -070011684 break;
11685 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011686
Michael Chan7942e1d2005-05-29 14:58:36 -070011687 return err;
11688}
11689
Matt Carlsonbb158d62011-04-25 12:42:47 +000011690#define TG3_TSO_MSS 500
11691
11692#define TG3_TSO_IP_HDR_LEN 20
11693#define TG3_TSO_TCP_HDR_LEN 20
11694#define TG3_TSO_TCP_OPT_LEN 12
11695
11696static const u8 tg3_tso_header[] = {
116970x08, 0x00,
116980x45, 0x00, 0x00, 0x00,
116990x00, 0x00, 0x40, 0x00,
117000x40, 0x06, 0x00, 0x00,
117010x0a, 0x00, 0x00, 0x01,
117020x0a, 0x00, 0x00, 0x02,
117030x0d, 0x00, 0xe0, 0x00,
117040x00, 0x00, 0x01, 0x00,
117050x00, 0x00, 0x02, 0x00,
117060x80, 0x10, 0x10, 0x00,
117070x14, 0x09, 0x00, 0x00,
117080x01, 0x01, 0x08, 0x0a,
117090x11, 0x11, 0x11, 0x11,
117100x11, 0x11, 0x11, 0x11,
11711};
Michael Chan9f40dea2005-09-05 17:53:06 -070011712
Matt Carlson28a45952011-08-19 13:58:22 +000011713static int tg3_run_loopback(struct tg3 *tp, u32 pktsz, bool tso_loopback)
Michael Chanc76949a2005-05-29 14:58:59 -070011714{
Matt Carlson5e5a7f32011-08-19 13:58:21 +000011715 u32 rx_start_idx, rx_idx, tx_idx, opaque_key;
Matt Carlsonbb158d62011-04-25 12:42:47 +000011716 u32 base_flags = 0, mss = 0, desc_idx, coal_now, data_off, val;
Matt Carlson84b67b22011-07-27 14:20:52 +000011717 u32 budget;
Eric Dumazet9205fd92011-11-18 06:47:01 +000011718 struct sk_buff *skb;
11719 u8 *tx_data, *rx_data;
Michael Chanc76949a2005-05-29 14:58:59 -070011720 dma_addr_t map;
11721 int num_pkts, tx_len, rx_len, i, err;
11722 struct tg3_rx_buffer_desc *desc;
Matt Carlson898a56f2009-08-28 14:02:40 +000011723 struct tg3_napi *tnapi, *rnapi;
Matt Carlson8fea32b2010-09-15 08:59:58 +000011724 struct tg3_rx_prodring_set *tpr = &tp->napi[0].prodring;
Michael Chanc76949a2005-05-29 14:58:59 -070011725
Matt Carlsonc8873402010-02-12 14:47:11 +000011726 tnapi = &tp->napi[0];
11727 rnapi = &tp->napi[0];
Matt Carlson0c1d0e22009-09-01 13:16:33 +000011728 if (tp->irq_cnt > 1) {
Joe Perches63c3a662011-04-26 08:12:10 +000011729 if (tg3_flag(tp, ENABLE_RSS))
Matt Carlson1da85aa2010-09-30 10:34:34 +000011730 rnapi = &tp->napi[1];
Joe Perches63c3a662011-04-26 08:12:10 +000011731 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlsonc8873402010-02-12 14:47:11 +000011732 tnapi = &tp->napi[1];
Matt Carlson0c1d0e22009-09-01 13:16:33 +000011733 }
Matt Carlsonfd2ce372009-09-01 12:51:13 +000011734 coal_now = tnapi->coal_now | rnapi->coal_now;
Matt Carlson898a56f2009-08-28 14:02:40 +000011735
Michael Chanc76949a2005-05-29 14:58:59 -070011736 err = -EIO;
11737
Matt Carlson4852a862011-04-13 11:05:07 +000011738 tx_len = pktsz;
David S. Millera20e9c62006-07-31 22:38:16 -070011739 skb = netdev_alloc_skb(tp->dev, tx_len);
Jesper Juhla50bb7b2006-05-09 23:14:35 -070011740 if (!skb)
11741 return -ENOMEM;
11742
Michael Chanc76949a2005-05-29 14:58:59 -070011743 tx_data = skb_put(skb, tx_len);
11744 memcpy(tx_data, tp->dev->dev_addr, 6);
11745 memset(tx_data + 6, 0x0, 8);
11746
Matt Carlson4852a862011-04-13 11:05:07 +000011747 tw32(MAC_RX_MTU_SIZE, tx_len + ETH_FCS_LEN);
Michael Chanc76949a2005-05-29 14:58:59 -070011748
Matt Carlson28a45952011-08-19 13:58:22 +000011749 if (tso_loopback) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000011750 struct iphdr *iph = (struct iphdr *)&tx_data[ETH_HLEN];
11751
11752 u32 hdr_len = TG3_TSO_IP_HDR_LEN + TG3_TSO_TCP_HDR_LEN +
11753 TG3_TSO_TCP_OPT_LEN;
11754
11755 memcpy(tx_data + ETH_ALEN * 2, tg3_tso_header,
11756 sizeof(tg3_tso_header));
11757 mss = TG3_TSO_MSS;
11758
11759 val = tx_len - ETH_ALEN * 2 - sizeof(tg3_tso_header);
11760 num_pkts = DIV_ROUND_UP(val, TG3_TSO_MSS);
11761
11762 /* Set the total length field in the IP header */
11763 iph->tot_len = htons((u16)(mss + hdr_len));
11764
11765 base_flags = (TXD_FLAG_CPU_PRE_DMA |
11766 TXD_FLAG_CPU_POST_DMA);
11767
Joe Perches63c3a662011-04-26 08:12:10 +000011768 if (tg3_flag(tp, HW_TSO_1) ||
11769 tg3_flag(tp, HW_TSO_2) ||
11770 tg3_flag(tp, HW_TSO_3)) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000011771 struct tcphdr *th;
11772 val = ETH_HLEN + TG3_TSO_IP_HDR_LEN;
11773 th = (struct tcphdr *)&tx_data[val];
11774 th->check = 0;
11775 } else
11776 base_flags |= TXD_FLAG_TCPUDP_CSUM;
11777
Joe Perches63c3a662011-04-26 08:12:10 +000011778 if (tg3_flag(tp, HW_TSO_3)) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000011779 mss |= (hdr_len & 0xc) << 12;
11780 if (hdr_len & 0x10)
11781 base_flags |= 0x00000010;
11782 base_flags |= (hdr_len & 0x3e0) << 5;
Joe Perches63c3a662011-04-26 08:12:10 +000011783 } else if (tg3_flag(tp, HW_TSO_2))
Matt Carlsonbb158d62011-04-25 12:42:47 +000011784 mss |= hdr_len << 9;
Joe Perches63c3a662011-04-26 08:12:10 +000011785 else if (tg3_flag(tp, HW_TSO_1) ||
Matt Carlsonbb158d62011-04-25 12:42:47 +000011786 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
11787 mss |= (TG3_TSO_TCP_OPT_LEN << 9);
11788 } else {
11789 base_flags |= (TG3_TSO_TCP_OPT_LEN << 10);
11790 }
11791
11792 data_off = ETH_ALEN * 2 + sizeof(tg3_tso_header);
11793 } else {
11794 num_pkts = 1;
11795 data_off = ETH_HLEN;
Michael Chanc441b452012-03-04 14:48:13 +000011796
11797 if (tg3_flag(tp, USE_JUMBO_BDFLAG) &&
11798 tx_len > VLAN_ETH_FRAME_LEN)
11799 base_flags |= TXD_FLAG_JMB_PKT;
Matt Carlsonbb158d62011-04-25 12:42:47 +000011800 }
11801
11802 for (i = data_off; i < tx_len; i++)
Michael Chanc76949a2005-05-29 14:58:59 -070011803 tx_data[i] = (u8) (i & 0xff);
11804
Alexander Duyckf4188d82009-12-02 16:48:38 +000011805 map = pci_map_single(tp->pdev, skb->data, tx_len, PCI_DMA_TODEVICE);
11806 if (pci_dma_mapping_error(tp->pdev, map)) {
Matt Carlsona21771d2009-11-02 14:25:31 +000011807 dev_kfree_skb(skb);
11808 return -EIO;
11809 }
Michael Chanc76949a2005-05-29 14:58:59 -070011810
Matt Carlson0d681b22011-07-27 14:20:49 +000011811 val = tnapi->tx_prod;
11812 tnapi->tx_buffers[val].skb = skb;
11813 dma_unmap_addr_set(&tnapi->tx_buffers[val], mapping, map);
11814
Michael Chanc76949a2005-05-29 14:58:59 -070011815 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +000011816 rnapi->coal_now);
Michael Chanc76949a2005-05-29 14:58:59 -070011817
11818 udelay(10);
11819
Matt Carlson898a56f2009-08-28 14:02:40 +000011820 rx_start_idx = rnapi->hw_status->idx[0].rx_producer;
Michael Chanc76949a2005-05-29 14:58:59 -070011821
Matt Carlson84b67b22011-07-27 14:20:52 +000011822 budget = tg3_tx_avail(tnapi);
11823 if (tg3_tx_frag_set(tnapi, &val, &budget, map, tx_len,
Matt Carlsond1a3b732011-07-27 14:20:51 +000011824 base_flags | TXD_FLAG_END, mss, 0)) {
11825 tnapi->tx_buffers[val].skb = NULL;
11826 dev_kfree_skb(skb);
11827 return -EIO;
11828 }
Michael Chanc76949a2005-05-29 14:58:59 -070011829
Matt Carlsonf3f3f272009-08-28 14:03:21 +000011830 tnapi->tx_prod++;
Michael Chanc76949a2005-05-29 14:58:59 -070011831
Michael Chan6541b802012-03-04 14:48:14 +000011832 /* Sync BD data before updating mailbox */
11833 wmb();
11834
Matt Carlsonf3f3f272009-08-28 14:03:21 +000011835 tw32_tx_mbox(tnapi->prodmbox, tnapi->tx_prod);
11836 tr32_mailbox(tnapi->prodmbox);
Michael Chanc76949a2005-05-29 14:58:59 -070011837
11838 udelay(10);
11839
Matt Carlson303fc922009-11-02 14:27:34 +000011840 /* 350 usec to allow enough time on some 10/100 Mbps devices. */
11841 for (i = 0; i < 35; i++) {
Michael Chanc76949a2005-05-29 14:58:59 -070011842 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +000011843 coal_now);
Michael Chanc76949a2005-05-29 14:58:59 -070011844
11845 udelay(10);
11846
Matt Carlson898a56f2009-08-28 14:02:40 +000011847 tx_idx = tnapi->hw_status->idx[0].tx_consumer;
11848 rx_idx = rnapi->hw_status->idx[0].rx_producer;
Matt Carlsonf3f3f272009-08-28 14:03:21 +000011849 if ((tx_idx == tnapi->tx_prod) &&
Michael Chanc76949a2005-05-29 14:58:59 -070011850 (rx_idx == (rx_start_idx + num_pkts)))
11851 break;
11852 }
11853
Matt Carlsonba1142e2011-11-04 09:15:00 +000011854 tg3_tx_skb_unmap(tnapi, tnapi->tx_prod - 1, -1);
Michael Chanc76949a2005-05-29 14:58:59 -070011855 dev_kfree_skb(skb);
11856
Matt Carlsonf3f3f272009-08-28 14:03:21 +000011857 if (tx_idx != tnapi->tx_prod)
Michael Chanc76949a2005-05-29 14:58:59 -070011858 goto out;
11859
11860 if (rx_idx != rx_start_idx + num_pkts)
11861 goto out;
11862
Matt Carlsonbb158d62011-04-25 12:42:47 +000011863 val = data_off;
11864 while (rx_idx != rx_start_idx) {
11865 desc = &rnapi->rx_rcb[rx_start_idx++];
11866 desc_idx = desc->opaque & RXD_OPAQUE_INDEX_MASK;
11867 opaque_key = desc->opaque & RXD_OPAQUE_RING_MASK;
Michael Chanc76949a2005-05-29 14:58:59 -070011868
Matt Carlsonbb158d62011-04-25 12:42:47 +000011869 if ((desc->err_vlan & RXD_ERR_MASK) != 0 &&
11870 (desc->err_vlan != RXD_ERR_ODD_NIBBLE_RCVD_MII))
Matt Carlson4852a862011-04-13 11:05:07 +000011871 goto out;
Michael Chanc76949a2005-05-29 14:58:59 -070011872
Matt Carlsonbb158d62011-04-25 12:42:47 +000011873 rx_len = ((desc->idx_len & RXD_LEN_MASK) >> RXD_LEN_SHIFT)
11874 - ETH_FCS_LEN;
11875
Matt Carlson28a45952011-08-19 13:58:22 +000011876 if (!tso_loopback) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000011877 if (rx_len != tx_len)
11878 goto out;
11879
11880 if (pktsz <= TG3_RX_STD_DMA_SZ - ETH_FCS_LEN) {
11881 if (opaque_key != RXD_OPAQUE_RING_STD)
11882 goto out;
11883 } else {
11884 if (opaque_key != RXD_OPAQUE_RING_JUMBO)
11885 goto out;
11886 }
11887 } else if ((desc->type_flags & RXD_FLAG_TCPUDP_CSUM) &&
11888 (desc->ip_tcp_csum & RXD_TCPCSUM_MASK)
Matt Carlson54e0a672011-05-19 12:12:50 +000011889 >> RXD_TCPCSUM_SHIFT != 0xffff) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000011890 goto out;
11891 }
11892
11893 if (opaque_key == RXD_OPAQUE_RING_STD) {
Eric Dumazet9205fd92011-11-18 06:47:01 +000011894 rx_data = tpr->rx_std_buffers[desc_idx].data;
Matt Carlsonbb158d62011-04-25 12:42:47 +000011895 map = dma_unmap_addr(&tpr->rx_std_buffers[desc_idx],
11896 mapping);
11897 } else if (opaque_key == RXD_OPAQUE_RING_JUMBO) {
Eric Dumazet9205fd92011-11-18 06:47:01 +000011898 rx_data = tpr->rx_jmb_buffers[desc_idx].data;
Matt Carlsonbb158d62011-04-25 12:42:47 +000011899 map = dma_unmap_addr(&tpr->rx_jmb_buffers[desc_idx],
11900 mapping);
11901 } else
Matt Carlson4852a862011-04-13 11:05:07 +000011902 goto out;
11903
Matt Carlsonbb158d62011-04-25 12:42:47 +000011904 pci_dma_sync_single_for_cpu(tp->pdev, map, rx_len,
11905 PCI_DMA_FROMDEVICE);
11906
Eric Dumazet9205fd92011-11-18 06:47:01 +000011907 rx_data += TG3_RX_OFFSET(tp);
Matt Carlsonbb158d62011-04-25 12:42:47 +000011908 for (i = data_off; i < rx_len; i++, val++) {
Eric Dumazet9205fd92011-11-18 06:47:01 +000011909 if (*(rx_data + i) != (u8) (val & 0xff))
Matt Carlsonbb158d62011-04-25 12:42:47 +000011910 goto out;
11911 }
Matt Carlson4852a862011-04-13 11:05:07 +000011912 }
11913
Michael Chanc76949a2005-05-29 14:58:59 -070011914 err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011915
Eric Dumazet9205fd92011-11-18 06:47:01 +000011916 /* tg3_free_rings will unmap and free the rx_data */
Michael Chanc76949a2005-05-29 14:58:59 -070011917out:
11918 return err;
11919}
11920
Matt Carlson00c266b2011-04-25 12:42:46 +000011921#define TG3_STD_LOOPBACK_FAILED 1
11922#define TG3_JMB_LOOPBACK_FAILED 2
Matt Carlsonbb158d62011-04-25 12:42:47 +000011923#define TG3_TSO_LOOPBACK_FAILED 4
Matt Carlson28a45952011-08-19 13:58:22 +000011924#define TG3_LOOPBACK_FAILED \
11925 (TG3_STD_LOOPBACK_FAILED | \
11926 TG3_JMB_LOOPBACK_FAILED | \
11927 TG3_TSO_LOOPBACK_FAILED)
Matt Carlson00c266b2011-04-25 12:42:46 +000011928
Matt Carlson941ec902011-08-19 13:58:23 +000011929static int tg3_test_loopback(struct tg3 *tp, u64 *data, bool do_extlpbk)
Michael Chan9f40dea2005-09-05 17:53:06 -070011930{
Matt Carlson28a45952011-08-19 13:58:22 +000011931 int err = -EIO;
Matt Carlson2215e242011-08-19 13:58:19 +000011932 u32 eee_cap;
Michael Chanc441b452012-03-04 14:48:13 +000011933 u32 jmb_pkt_sz = 9000;
11934
11935 if (tp->dma_limit)
11936 jmb_pkt_sz = tp->dma_limit - ETH_HLEN;
Michael Chan9f40dea2005-09-05 17:53:06 -070011937
Matt Carlsonab789042011-01-25 15:58:54 +000011938 eee_cap = tp->phy_flags & TG3_PHYFLG_EEE_CAP;
11939 tp->phy_flags &= ~TG3_PHYFLG_EEE_CAP;
11940
Matt Carlson28a45952011-08-19 13:58:22 +000011941 if (!netif_running(tp->dev)) {
11942 data[0] = TG3_LOOPBACK_FAILED;
11943 data[1] = TG3_LOOPBACK_FAILED;
Matt Carlson941ec902011-08-19 13:58:23 +000011944 if (do_extlpbk)
11945 data[2] = TG3_LOOPBACK_FAILED;
Matt Carlson28a45952011-08-19 13:58:22 +000011946 goto done;
11947 }
11948
Michael Chanb9ec6c12006-07-25 16:37:27 -070011949 err = tg3_reset_hw(tp, 1);
Matt Carlsonab789042011-01-25 15:58:54 +000011950 if (err) {
Matt Carlson28a45952011-08-19 13:58:22 +000011951 data[0] = TG3_LOOPBACK_FAILED;
11952 data[1] = TG3_LOOPBACK_FAILED;
Matt Carlson941ec902011-08-19 13:58:23 +000011953 if (do_extlpbk)
11954 data[2] = TG3_LOOPBACK_FAILED;
Matt Carlsonab789042011-01-25 15:58:54 +000011955 goto done;
11956 }
Michael Chan9f40dea2005-09-05 17:53:06 -070011957
Joe Perches63c3a662011-04-26 08:12:10 +000011958 if (tg3_flag(tp, ENABLE_RSS)) {
Matt Carlson4a85f092011-04-20 07:57:37 +000011959 int i;
11960
11961 /* Reroute all rx packets to the 1st queue */
11962 for (i = MAC_RSS_INDIR_TBL_0;
11963 i < MAC_RSS_INDIR_TBL_0 + TG3_RSS_INDIR_TBL_SIZE; i += 4)
11964 tw32(i, 0x0);
11965 }
11966
Matt Carlson6e01b202011-08-19 13:58:20 +000011967 /* HW errata - mac loopback fails in some cases on 5780.
11968 * Normal traffic and PHY loopback are not affected by
11969 * errata. Also, the MAC loopback test is deprecated for
11970 * all newer ASIC revisions.
11971 */
11972 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5780 &&
11973 !tg3_flag(tp, CPMU_PRESENT)) {
11974 tg3_mac_loopback(tp, true);
Matt Carlson9936bcf2007-10-10 18:03:07 -070011975
Matt Carlson28a45952011-08-19 13:58:22 +000011976 if (tg3_run_loopback(tp, ETH_FRAME_LEN, false))
11977 data[0] |= TG3_STD_LOOPBACK_FAILED;
Matt Carlson6e01b202011-08-19 13:58:20 +000011978
11979 if (tg3_flag(tp, JUMBO_RING_ENABLE) &&
Michael Chanc441b452012-03-04 14:48:13 +000011980 tg3_run_loopback(tp, jmb_pkt_sz + ETH_HLEN, false))
Matt Carlson28a45952011-08-19 13:58:22 +000011981 data[0] |= TG3_JMB_LOOPBACK_FAILED;
Matt Carlson6e01b202011-08-19 13:58:20 +000011982
11983 tg3_mac_loopback(tp, false);
11984 }
Matt Carlson4852a862011-04-13 11:05:07 +000011985
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011986 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
Joe Perches63c3a662011-04-26 08:12:10 +000011987 !tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson5e5a7f32011-08-19 13:58:21 +000011988 int i;
11989
Matt Carlson941ec902011-08-19 13:58:23 +000011990 tg3_phy_lpbk_set(tp, 0, false);
Matt Carlson5e5a7f32011-08-19 13:58:21 +000011991
11992 /* Wait for link */
11993 for (i = 0; i < 100; i++) {
11994 if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
11995 break;
11996 mdelay(1);
11997 }
11998
Matt Carlson28a45952011-08-19 13:58:22 +000011999 if (tg3_run_loopback(tp, ETH_FRAME_LEN, false))
12000 data[1] |= TG3_STD_LOOPBACK_FAILED;
Joe Perches63c3a662011-04-26 08:12:10 +000012001 if (tg3_flag(tp, TSO_CAPABLE) &&
Matt Carlson28a45952011-08-19 13:58:22 +000012002 tg3_run_loopback(tp, ETH_FRAME_LEN, true))
12003 data[1] |= TG3_TSO_LOOPBACK_FAILED;
Joe Perches63c3a662011-04-26 08:12:10 +000012004 if (tg3_flag(tp, JUMBO_RING_ENABLE) &&
Michael Chanc441b452012-03-04 14:48:13 +000012005 tg3_run_loopback(tp, jmb_pkt_sz + ETH_HLEN, false))
Matt Carlson28a45952011-08-19 13:58:22 +000012006 data[1] |= TG3_JMB_LOOPBACK_FAILED;
Michael Chan9f40dea2005-09-05 17:53:06 -070012007
Matt Carlson941ec902011-08-19 13:58:23 +000012008 if (do_extlpbk) {
12009 tg3_phy_lpbk_set(tp, 0, true);
12010
12011 /* All link indications report up, but the hardware
12012 * isn't really ready for about 20 msec. Double it
12013 * to be sure.
12014 */
12015 mdelay(40);
12016
12017 if (tg3_run_loopback(tp, ETH_FRAME_LEN, false))
12018 data[2] |= TG3_STD_LOOPBACK_FAILED;
12019 if (tg3_flag(tp, TSO_CAPABLE) &&
12020 tg3_run_loopback(tp, ETH_FRAME_LEN, true))
12021 data[2] |= TG3_TSO_LOOPBACK_FAILED;
12022 if (tg3_flag(tp, JUMBO_RING_ENABLE) &&
Michael Chanc441b452012-03-04 14:48:13 +000012023 tg3_run_loopback(tp, jmb_pkt_sz + ETH_HLEN, false))
Matt Carlson941ec902011-08-19 13:58:23 +000012024 data[2] |= TG3_JMB_LOOPBACK_FAILED;
12025 }
12026
Matt Carlson5e5a7f32011-08-19 13:58:21 +000012027 /* Re-enable gphy autopowerdown. */
12028 if (tp->phy_flags & TG3_PHYFLG_ENABLE_APD)
12029 tg3_phy_toggle_apd(tp, true);
12030 }
Matt Carlson6833c042008-11-21 17:18:59 -080012031
Matt Carlson941ec902011-08-19 13:58:23 +000012032 err = (data[0] | data[1] | data[2]) ? -EIO : 0;
Matt Carlson28a45952011-08-19 13:58:22 +000012033
Matt Carlsonab789042011-01-25 15:58:54 +000012034done:
12035 tp->phy_flags |= eee_cap;
12036
Michael Chan9f40dea2005-09-05 17:53:06 -070012037 return err;
12038}
12039
Michael Chan4cafd3f2005-05-29 14:56:34 -070012040static void tg3_self_test(struct net_device *dev, struct ethtool_test *etest,
12041 u64 *data)
12042{
Michael Chan566f86a2005-05-29 14:56:58 -070012043 struct tg3 *tp = netdev_priv(dev);
Matt Carlson941ec902011-08-19 13:58:23 +000012044 bool doextlpbk = etest->flags & ETH_TEST_FL_EXTERNAL_LB;
Michael Chan566f86a2005-05-29 14:56:58 -070012045
Matt Carlsonbed98292011-07-13 09:27:29 +000012046 if ((tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) &&
12047 tg3_power_up(tp)) {
12048 etest->flags |= ETH_TEST_FL_FAILED;
12049 memset(data, 1, sizeof(u64) * TG3_NUM_TEST);
12050 return;
12051 }
Michael Chanbc1c7562006-03-20 17:48:03 -080012052
Michael Chan566f86a2005-05-29 14:56:58 -070012053 memset(data, 0, sizeof(u64) * TG3_NUM_TEST);
12054
12055 if (tg3_test_nvram(tp) != 0) {
12056 etest->flags |= ETH_TEST_FL_FAILED;
12057 data[0] = 1;
12058 }
Matt Carlson941ec902011-08-19 13:58:23 +000012059 if (!doextlpbk && tg3_test_link(tp)) {
Michael Chanca430072005-05-29 14:57:23 -070012060 etest->flags |= ETH_TEST_FL_FAILED;
12061 data[1] = 1;
12062 }
Michael Chana71116d2005-05-29 14:58:11 -070012063 if (etest->flags & ETH_TEST_FL_OFFLINE) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012064 int err, err2 = 0, irq_sync = 0;
Michael Chana71116d2005-05-29 14:58:11 -070012065
Michael Chanbbe832c2005-06-24 20:20:04 -070012066 if (netif_running(dev)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012067 tg3_phy_stop(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -070012068 tg3_netif_stop(tp);
12069 irq_sync = 1;
12070 }
12071
12072 tg3_full_lock(tp, irq_sync);
Michael Chana71116d2005-05-29 14:58:11 -070012073
12074 tg3_halt(tp, RESET_KIND_SUSPEND, 1);
Michael Chanec41c7d2006-01-17 02:40:55 -080012075 err = tg3_nvram_lock(tp);
Michael Chana71116d2005-05-29 14:58:11 -070012076 tg3_halt_cpu(tp, RX_CPU_BASE);
Joe Perches63c3a662011-04-26 08:12:10 +000012077 if (!tg3_flag(tp, 5705_PLUS))
Michael Chana71116d2005-05-29 14:58:11 -070012078 tg3_halt_cpu(tp, TX_CPU_BASE);
Michael Chanec41c7d2006-01-17 02:40:55 -080012079 if (!err)
12080 tg3_nvram_unlock(tp);
Michael Chana71116d2005-05-29 14:58:11 -070012081
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012082 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
Michael Chand9ab5ad12006-03-20 22:27:35 -080012083 tg3_phy_reset(tp);
12084
Michael Chana71116d2005-05-29 14:58:11 -070012085 if (tg3_test_registers(tp) != 0) {
12086 etest->flags |= ETH_TEST_FL_FAILED;
12087 data[2] = 1;
12088 }
Matt Carlson28a45952011-08-19 13:58:22 +000012089
Michael Chan7942e1d2005-05-29 14:58:36 -070012090 if (tg3_test_memory(tp) != 0) {
12091 etest->flags |= ETH_TEST_FL_FAILED;
12092 data[3] = 1;
12093 }
Matt Carlson28a45952011-08-19 13:58:22 +000012094
Matt Carlson941ec902011-08-19 13:58:23 +000012095 if (doextlpbk)
12096 etest->flags |= ETH_TEST_FL_EXTERNAL_LB_DONE;
12097
12098 if (tg3_test_loopback(tp, &data[4], doextlpbk))
Michael Chanc76949a2005-05-29 14:58:59 -070012099 etest->flags |= ETH_TEST_FL_FAILED;
Michael Chana71116d2005-05-29 14:58:11 -070012100
David S. Millerf47c11e2005-06-24 20:18:35 -070012101 tg3_full_unlock(tp);
12102
Michael Chand4bc3922005-05-29 14:59:20 -070012103 if (tg3_test_interrupt(tp) != 0) {
12104 etest->flags |= ETH_TEST_FL_FAILED;
Matt Carlson941ec902011-08-19 13:58:23 +000012105 data[7] = 1;
Michael Chand4bc3922005-05-29 14:59:20 -070012106 }
David S. Millerf47c11e2005-06-24 20:18:35 -070012107
12108 tg3_full_lock(tp, 0);
Michael Chand4bc3922005-05-29 14:59:20 -070012109
Michael Chana71116d2005-05-29 14:58:11 -070012110 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
12111 if (netif_running(dev)) {
Joe Perches63c3a662011-04-26 08:12:10 +000012112 tg3_flag_set(tp, INIT_COMPLETE);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012113 err2 = tg3_restart_hw(tp, 1);
12114 if (!err2)
Michael Chanb9ec6c12006-07-25 16:37:27 -070012115 tg3_netif_start(tp);
Michael Chana71116d2005-05-29 14:58:11 -070012116 }
David S. Millerf47c11e2005-06-24 20:18:35 -070012117
12118 tg3_full_unlock(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012119
12120 if (irq_sync && !err2)
12121 tg3_phy_start(tp);
Michael Chana71116d2005-05-29 14:58:11 -070012122 }
Matt Carlson80096062010-08-02 11:26:06 +000012123 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000012124 tg3_power_down(tp);
Michael Chanbc1c7562006-03-20 17:48:03 -080012125
Michael Chan4cafd3f2005-05-29 14:56:34 -070012126}
12127
Linus Torvalds1da177e2005-04-16 15:20:36 -070012128static int tg3_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
12129{
12130 struct mii_ioctl_data *data = if_mii(ifr);
12131 struct tg3 *tp = netdev_priv(dev);
12132 int err;
12133
Joe Perches63c3a662011-04-26 08:12:10 +000012134 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000012135 struct phy_device *phydev;
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012136 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012137 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000012138 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Richard Cochran28b04112010-07-17 08:48:55 +000012139 return phy_mii_ioctl(phydev, ifr, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012140 }
12141
Matt Carlson33f401a2010-04-05 10:19:27 +000012142 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012143 case SIOCGMIIPHY:
Matt Carlson882e9792009-09-01 13:21:36 +000012144 data->phy_id = tp->phy_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012145
12146 /* fallthru */
12147 case SIOCGMIIREG: {
12148 u32 mii_regval;
12149
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012150 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012151 break; /* We have no PHY */
12152
Matt Carlson34eea5a2011-04-20 07:57:38 +000012153 if (!netif_running(dev))
Michael Chanbc1c7562006-03-20 17:48:03 -080012154 return -EAGAIN;
12155
David S. Millerf47c11e2005-06-24 20:18:35 -070012156 spin_lock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012157 err = tg3_readphy(tp, data->reg_num & 0x1f, &mii_regval);
David S. Millerf47c11e2005-06-24 20:18:35 -070012158 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012159
12160 data->val_out = mii_regval;
12161
12162 return err;
12163 }
12164
12165 case SIOCSMIIREG:
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012166 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012167 break; /* We have no PHY */
12168
Matt Carlson34eea5a2011-04-20 07:57:38 +000012169 if (!netif_running(dev))
Michael Chanbc1c7562006-03-20 17:48:03 -080012170 return -EAGAIN;
12171
David S. Millerf47c11e2005-06-24 20:18:35 -070012172 spin_lock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012173 err = tg3_writephy(tp, data->reg_num & 0x1f, data->val_in);
David S. Millerf47c11e2005-06-24 20:18:35 -070012174 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012175
12176 return err;
12177
12178 default:
12179 /* do nothing */
12180 break;
12181 }
12182 return -EOPNOTSUPP;
12183}
12184
David S. Miller15f98502005-05-18 22:49:26 -070012185static int tg3_get_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
12186{
12187 struct tg3 *tp = netdev_priv(dev);
12188
12189 memcpy(ec, &tp->coal, sizeof(*ec));
12190 return 0;
12191}
12192
Michael Chand244c892005-07-05 14:42:33 -070012193static int tg3_set_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
12194{
12195 struct tg3 *tp = netdev_priv(dev);
12196 u32 max_rxcoal_tick_int = 0, max_txcoal_tick_int = 0;
12197 u32 max_stat_coal_ticks = 0, min_stat_coal_ticks = 0;
12198
Joe Perches63c3a662011-04-26 08:12:10 +000012199 if (!tg3_flag(tp, 5705_PLUS)) {
Michael Chand244c892005-07-05 14:42:33 -070012200 max_rxcoal_tick_int = MAX_RXCOAL_TICK_INT;
12201 max_txcoal_tick_int = MAX_TXCOAL_TICK_INT;
12202 max_stat_coal_ticks = MAX_STAT_COAL_TICKS;
12203 min_stat_coal_ticks = MIN_STAT_COAL_TICKS;
12204 }
12205
12206 if ((ec->rx_coalesce_usecs > MAX_RXCOL_TICKS) ||
12207 (ec->tx_coalesce_usecs > MAX_TXCOL_TICKS) ||
12208 (ec->rx_max_coalesced_frames > MAX_RXMAX_FRAMES) ||
12209 (ec->tx_max_coalesced_frames > MAX_TXMAX_FRAMES) ||
12210 (ec->rx_coalesce_usecs_irq > max_rxcoal_tick_int) ||
12211 (ec->tx_coalesce_usecs_irq > max_txcoal_tick_int) ||
12212 (ec->rx_max_coalesced_frames_irq > MAX_RXCOAL_MAXF_INT) ||
12213 (ec->tx_max_coalesced_frames_irq > MAX_TXCOAL_MAXF_INT) ||
12214 (ec->stats_block_coalesce_usecs > max_stat_coal_ticks) ||
12215 (ec->stats_block_coalesce_usecs < min_stat_coal_ticks))
12216 return -EINVAL;
12217
12218 /* No rx interrupts will be generated if both are zero */
12219 if ((ec->rx_coalesce_usecs == 0) &&
12220 (ec->rx_max_coalesced_frames == 0))
12221 return -EINVAL;
12222
12223 /* No tx interrupts will be generated if both are zero */
12224 if ((ec->tx_coalesce_usecs == 0) &&
12225 (ec->tx_max_coalesced_frames == 0))
12226 return -EINVAL;
12227
12228 /* Only copy relevant parameters, ignore all others. */
12229 tp->coal.rx_coalesce_usecs = ec->rx_coalesce_usecs;
12230 tp->coal.tx_coalesce_usecs = ec->tx_coalesce_usecs;
12231 tp->coal.rx_max_coalesced_frames = ec->rx_max_coalesced_frames;
12232 tp->coal.tx_max_coalesced_frames = ec->tx_max_coalesced_frames;
12233 tp->coal.rx_coalesce_usecs_irq = ec->rx_coalesce_usecs_irq;
12234 tp->coal.tx_coalesce_usecs_irq = ec->tx_coalesce_usecs_irq;
12235 tp->coal.rx_max_coalesced_frames_irq = ec->rx_max_coalesced_frames_irq;
12236 tp->coal.tx_max_coalesced_frames_irq = ec->tx_max_coalesced_frames_irq;
12237 tp->coal.stats_block_coalesce_usecs = ec->stats_block_coalesce_usecs;
12238
12239 if (netif_running(dev)) {
12240 tg3_full_lock(tp, 0);
12241 __tg3_set_coalesce(tp, &tp->coal);
12242 tg3_full_unlock(tp);
12243 }
12244 return 0;
12245}
12246
Jeff Garzik7282d492006-09-13 14:30:00 -040012247static const struct ethtool_ops tg3_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012248 .get_settings = tg3_get_settings,
12249 .set_settings = tg3_set_settings,
12250 .get_drvinfo = tg3_get_drvinfo,
12251 .get_regs_len = tg3_get_regs_len,
12252 .get_regs = tg3_get_regs,
12253 .get_wol = tg3_get_wol,
12254 .set_wol = tg3_set_wol,
12255 .get_msglevel = tg3_get_msglevel,
12256 .set_msglevel = tg3_set_msglevel,
12257 .nway_reset = tg3_nway_reset,
12258 .get_link = ethtool_op_get_link,
12259 .get_eeprom_len = tg3_get_eeprom_len,
12260 .get_eeprom = tg3_get_eeprom,
12261 .set_eeprom = tg3_set_eeprom,
12262 .get_ringparam = tg3_get_ringparam,
12263 .set_ringparam = tg3_set_ringparam,
12264 .get_pauseparam = tg3_get_pauseparam,
12265 .set_pauseparam = tg3_set_pauseparam,
Michael Chan4cafd3f2005-05-29 14:56:34 -070012266 .self_test = tg3_self_test,
Linus Torvalds1da177e2005-04-16 15:20:36 -070012267 .get_strings = tg3_get_strings,
stephen hemminger81b87092011-04-04 08:43:50 +000012268 .set_phys_id = tg3_set_phys_id,
Linus Torvalds1da177e2005-04-16 15:20:36 -070012269 .get_ethtool_stats = tg3_get_ethtool_stats,
David S. Miller15f98502005-05-18 22:49:26 -070012270 .get_coalesce = tg3_get_coalesce,
Michael Chand244c892005-07-05 14:42:33 -070012271 .set_coalesce = tg3_set_coalesce,
Jeff Garzikb9f2c042007-10-03 18:07:32 -070012272 .get_sset_count = tg3_get_sset_count,
Matt Carlson90415472011-12-16 13:33:23 +000012273 .get_rxnfc = tg3_get_rxnfc,
12274 .get_rxfh_indir_size = tg3_get_rxfh_indir_size,
12275 .get_rxfh_indir = tg3_get_rxfh_indir,
12276 .set_rxfh_indir = tg3_set_rxfh_indir,
Richard Cochran3f847492012-04-03 22:59:39 +000012277 .get_ts_info = ethtool_op_get_ts_info,
Linus Torvalds1da177e2005-04-16 15:20:36 -070012278};
12279
David S. Millerb4017c52012-03-01 17:57:40 -050012280static struct rtnl_link_stats64 *tg3_get_stats64(struct net_device *dev,
12281 struct rtnl_link_stats64 *stats)
12282{
12283 struct tg3 *tp = netdev_priv(dev);
12284
12285 if (!tp->hw_stats)
12286 return &tp->net_stats_prev;
12287
12288 spin_lock_bh(&tp->lock);
12289 tg3_get_nstats(tp, stats);
12290 spin_unlock_bh(&tp->lock);
12291
12292 return stats;
12293}
12294
Matt Carlsonccd5ba92012-02-13 10:20:08 +000012295static void tg3_set_rx_mode(struct net_device *dev)
12296{
12297 struct tg3 *tp = netdev_priv(dev);
12298
12299 if (!netif_running(dev))
12300 return;
12301
12302 tg3_full_lock(tp, 0);
12303 __tg3_set_rx_mode(dev);
12304 tg3_full_unlock(tp);
12305}
12306
Matt Carlsonfaf16272012-02-13 10:20:07 +000012307static inline void tg3_set_mtu(struct net_device *dev, struct tg3 *tp,
12308 int new_mtu)
12309{
12310 dev->mtu = new_mtu;
12311
12312 if (new_mtu > ETH_DATA_LEN) {
12313 if (tg3_flag(tp, 5780_CLASS)) {
12314 netdev_update_features(dev);
12315 tg3_flag_clear(tp, TSO_CAPABLE);
12316 } else {
12317 tg3_flag_set(tp, JUMBO_RING_ENABLE);
12318 }
12319 } else {
12320 if (tg3_flag(tp, 5780_CLASS)) {
12321 tg3_flag_set(tp, TSO_CAPABLE);
12322 netdev_update_features(dev);
12323 }
12324 tg3_flag_clear(tp, JUMBO_RING_ENABLE);
12325 }
12326}
12327
12328static int tg3_change_mtu(struct net_device *dev, int new_mtu)
12329{
12330 struct tg3 *tp = netdev_priv(dev);
Michael Chan2fae5e32012-03-04 14:48:15 +000012331 int err, reset_phy = 0;
Matt Carlsonfaf16272012-02-13 10:20:07 +000012332
12333 if (new_mtu < TG3_MIN_MTU || new_mtu > TG3_MAX_MTU(tp))
12334 return -EINVAL;
12335
12336 if (!netif_running(dev)) {
12337 /* We'll just catch it later when the
12338 * device is up'd.
12339 */
12340 tg3_set_mtu(dev, tp, new_mtu);
12341 return 0;
12342 }
12343
12344 tg3_phy_stop(tp);
12345
12346 tg3_netif_stop(tp);
12347
12348 tg3_full_lock(tp, 1);
12349
12350 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
12351
12352 tg3_set_mtu(dev, tp, new_mtu);
12353
Michael Chan2fae5e32012-03-04 14:48:15 +000012354 /* Reset PHY, otherwise the read DMA engine will be in a mode that
12355 * breaks all requests to 256 bytes.
12356 */
12357 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57766)
12358 reset_phy = 1;
12359
12360 err = tg3_restart_hw(tp, reset_phy);
Matt Carlsonfaf16272012-02-13 10:20:07 +000012361
12362 if (!err)
12363 tg3_netif_start(tp);
12364
12365 tg3_full_unlock(tp);
12366
12367 if (!err)
12368 tg3_phy_start(tp);
12369
12370 return err;
12371}
12372
12373static const struct net_device_ops tg3_netdev_ops = {
12374 .ndo_open = tg3_open,
12375 .ndo_stop = tg3_close,
12376 .ndo_start_xmit = tg3_start_xmit,
12377 .ndo_get_stats64 = tg3_get_stats64,
12378 .ndo_validate_addr = eth_validate_addr,
12379 .ndo_set_rx_mode = tg3_set_rx_mode,
12380 .ndo_set_mac_address = tg3_set_mac_addr,
12381 .ndo_do_ioctl = tg3_ioctl,
12382 .ndo_tx_timeout = tg3_tx_timeout,
12383 .ndo_change_mtu = tg3_change_mtu,
12384 .ndo_fix_features = tg3_fix_features,
12385 .ndo_set_features = tg3_set_features,
12386#ifdef CONFIG_NET_POLL_CONTROLLER
12387 .ndo_poll_controller = tg3_poll_controller,
12388#endif
12389};
12390
Linus Torvalds1da177e2005-04-16 15:20:36 -070012391static void __devinit tg3_get_eeprom_size(struct tg3 *tp)
12392{
Michael Chan1b277772006-03-20 22:27:48 -080012393 u32 cursize, val, magic;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012394
12395 tp->nvram_size = EEPROM_CHIP_SIZE;
12396
Matt Carlsone4f34112009-02-25 14:25:00 +000012397 if (tg3_nvram_read(tp, 0, &magic) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012398 return;
12399
Michael Chanb16250e2006-09-27 16:10:14 -070012400 if ((magic != TG3_EEPROM_MAGIC) &&
12401 ((magic & TG3_EEPROM_MAGIC_FW_MSK) != TG3_EEPROM_MAGIC_FW) &&
12402 ((magic & TG3_EEPROM_MAGIC_HW_MSK) != TG3_EEPROM_MAGIC_HW))
Linus Torvalds1da177e2005-04-16 15:20:36 -070012403 return;
12404
12405 /*
12406 * Size the chip by reading offsets at increasing powers of two.
12407 * When we encounter our validation signature, we know the addressing
12408 * has wrapped around, and thus have our chip size.
12409 */
Michael Chan1b277772006-03-20 22:27:48 -080012410 cursize = 0x10;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012411
12412 while (cursize < tp->nvram_size) {
Matt Carlsone4f34112009-02-25 14:25:00 +000012413 if (tg3_nvram_read(tp, cursize, &val) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012414 return;
12415
Michael Chan18201802006-03-20 22:29:15 -080012416 if (val == magic)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012417 break;
12418
12419 cursize <<= 1;
12420 }
12421
12422 tp->nvram_size = cursize;
12423}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012424
Linus Torvalds1da177e2005-04-16 15:20:36 -070012425static void __devinit tg3_get_nvram_size(struct tg3 *tp)
12426{
12427 u32 val;
12428
Joe Perches63c3a662011-04-26 08:12:10 +000012429 if (tg3_flag(tp, NO_NVRAM) || tg3_nvram_read(tp, 0, &val) != 0)
Michael Chan1b277772006-03-20 22:27:48 -080012430 return;
12431
12432 /* Selfboot format */
Michael Chan18201802006-03-20 22:29:15 -080012433 if (val != TG3_EEPROM_MAGIC) {
Michael Chan1b277772006-03-20 22:27:48 -080012434 tg3_get_eeprom_size(tp);
12435 return;
12436 }
12437
Matt Carlson6d348f22009-02-25 14:25:52 +000012438 if (tg3_nvram_read(tp, 0xf0, &val) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012439 if (val != 0) {
Matt Carlson6d348f22009-02-25 14:25:52 +000012440 /* This is confusing. We want to operate on the
12441 * 16-bit value at offset 0xf2. The tg3_nvram_read()
12442 * call will read from NVRAM and byteswap the data
12443 * according to the byteswapping settings for all
12444 * other register accesses. This ensures the data we
12445 * want will always reside in the lower 16-bits.
12446 * However, the data in NVRAM is in LE format, which
12447 * means the data from the NVRAM read will always be
12448 * opposite the endianness of the CPU. The 16-bit
12449 * byteswap then brings the data to CPU endianness.
12450 */
12451 tp->nvram_size = swab16((u16)(val & 0x0000ffff)) * 1024;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012452 return;
12453 }
12454 }
Matt Carlsonfd1122a2008-05-02 16:48:36 -070012455 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012456}
12457
12458static void __devinit tg3_get_nvram_info(struct tg3 *tp)
12459{
12460 u32 nvcfg1;
12461
12462 nvcfg1 = tr32(NVRAM_CFG1);
12463 if (nvcfg1 & NVRAM_CFG1_FLASHIF_ENAB) {
Joe Perches63c3a662011-04-26 08:12:10 +000012464 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000012465 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012466 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
12467 tw32(NVRAM_CFG1, nvcfg1);
12468 }
12469
Matt Carlson6ff6f812011-05-19 12:12:54 +000012470 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 ||
Joe Perches63c3a662011-04-26 08:12:10 +000012471 tg3_flag(tp, 5780_CLASS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012472 switch (nvcfg1 & NVRAM_CFG1_VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000012473 case FLASH_VENDOR_ATMEL_FLASH_BUFFERED:
12474 tp->nvram_jedecnum = JEDEC_ATMEL;
12475 tp->nvram_pagesize = ATMEL_AT45DB0X1B_PAGE_SIZE;
Joe Perches63c3a662011-04-26 08:12:10 +000012476 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000012477 break;
12478 case FLASH_VENDOR_ATMEL_FLASH_UNBUFFERED:
12479 tp->nvram_jedecnum = JEDEC_ATMEL;
12480 tp->nvram_pagesize = ATMEL_AT25F512_PAGE_SIZE;
12481 break;
12482 case FLASH_VENDOR_ATMEL_EEPROM:
12483 tp->nvram_jedecnum = JEDEC_ATMEL;
12484 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
Joe Perches63c3a662011-04-26 08:12:10 +000012485 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000012486 break;
12487 case FLASH_VENDOR_ST:
12488 tp->nvram_jedecnum = JEDEC_ST;
12489 tp->nvram_pagesize = ST_M45PEX0_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_SAIFUN:
12493 tp->nvram_jedecnum = JEDEC_SAIFUN;
12494 tp->nvram_pagesize = SAIFUN_SA25F0XX_PAGE_SIZE;
12495 break;
12496 case FLASH_VENDOR_SST_SMALL:
12497 case FLASH_VENDOR_SST_LARGE:
12498 tp->nvram_jedecnum = JEDEC_SST;
12499 tp->nvram_pagesize = SST_25VF0X0_PAGE_SIZE;
12500 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012501 }
Matt Carlson8590a602009-08-28 12:29:16 +000012502 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012503 tp->nvram_jedecnum = JEDEC_ATMEL;
12504 tp->nvram_pagesize = ATMEL_AT45DB0X1B_PAGE_SIZE;
Joe Perches63c3a662011-04-26 08:12:10 +000012505 tg3_flag_set(tp, NVRAM_BUFFERED);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012506 }
12507}
12508
Matt Carlsona1b950d2009-09-01 13:20:17 +000012509static void __devinit tg3_nvram_get_pagesize(struct tg3 *tp, u32 nvmcfg1)
12510{
12511 switch (nvmcfg1 & NVRAM_CFG1_5752PAGE_SIZE_MASK) {
12512 case FLASH_5752PAGE_SIZE_256:
12513 tp->nvram_pagesize = 256;
12514 break;
12515 case FLASH_5752PAGE_SIZE_512:
12516 tp->nvram_pagesize = 512;
12517 break;
12518 case FLASH_5752PAGE_SIZE_1K:
12519 tp->nvram_pagesize = 1024;
12520 break;
12521 case FLASH_5752PAGE_SIZE_2K:
12522 tp->nvram_pagesize = 2048;
12523 break;
12524 case FLASH_5752PAGE_SIZE_4K:
12525 tp->nvram_pagesize = 4096;
12526 break;
12527 case FLASH_5752PAGE_SIZE_264:
12528 tp->nvram_pagesize = 264;
12529 break;
12530 case FLASH_5752PAGE_SIZE_528:
12531 tp->nvram_pagesize = 528;
12532 break;
12533 }
12534}
12535
Michael Chan361b4ac2005-04-21 17:11:21 -070012536static void __devinit tg3_get_5752_nvram_info(struct tg3 *tp)
12537{
12538 u32 nvcfg1;
12539
12540 nvcfg1 = tr32(NVRAM_CFG1);
12541
Michael Chane6af3012005-04-21 17:12:05 -070012542 /* NVRAM protection for TPM */
12543 if (nvcfg1 & (1 << 27))
Joe Perches63c3a662011-04-26 08:12:10 +000012544 tg3_flag_set(tp, PROTECTED_NVRAM);
Michael Chane6af3012005-04-21 17:12:05 -070012545
Michael Chan361b4ac2005-04-21 17:11:21 -070012546 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000012547 case FLASH_5752VENDOR_ATMEL_EEPROM_64KHZ:
12548 case FLASH_5752VENDOR_ATMEL_EEPROM_376KHZ:
12549 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012550 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000012551 break;
12552 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
12553 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012554 tg3_flag_set(tp, NVRAM_BUFFERED);
12555 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000012556 break;
12557 case FLASH_5752VENDOR_ST_M45PE10:
12558 case FLASH_5752VENDOR_ST_M45PE20:
12559 case FLASH_5752VENDOR_ST_M45PE40:
12560 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000012561 tg3_flag_set(tp, NVRAM_BUFFERED);
12562 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000012563 break;
Michael Chan361b4ac2005-04-21 17:11:21 -070012564 }
12565
Joe Perches63c3a662011-04-26 08:12:10 +000012566 if (tg3_flag(tp, FLASH)) {
Matt Carlsona1b950d2009-09-01 13:20:17 +000012567 tg3_nvram_get_pagesize(tp, nvcfg1);
Matt Carlson8590a602009-08-28 12:29:16 +000012568 } else {
Michael Chan361b4ac2005-04-21 17:11:21 -070012569 /* For eeprom, set pagesize to maximum eeprom size */
12570 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
12571
12572 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
12573 tw32(NVRAM_CFG1, nvcfg1);
12574 }
12575}
12576
Michael Chand3c7b882006-03-23 01:28:25 -080012577static void __devinit tg3_get_5755_nvram_info(struct tg3 *tp)
12578{
Matt Carlson989a9d22007-05-05 11:51:05 -070012579 u32 nvcfg1, protect = 0;
Michael Chand3c7b882006-03-23 01:28:25 -080012580
12581 nvcfg1 = tr32(NVRAM_CFG1);
12582
12583 /* NVRAM protection for TPM */
Matt Carlson989a9d22007-05-05 11:51:05 -070012584 if (nvcfg1 & (1 << 27)) {
Joe Perches63c3a662011-04-26 08:12:10 +000012585 tg3_flag_set(tp, PROTECTED_NVRAM);
Matt Carlson989a9d22007-05-05 11:51:05 -070012586 protect = 1;
12587 }
Michael Chand3c7b882006-03-23 01:28:25 -080012588
Matt Carlson989a9d22007-05-05 11:51:05 -070012589 nvcfg1 &= NVRAM_CFG1_5752VENDOR_MASK;
12590 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000012591 case FLASH_5755VENDOR_ATMEL_FLASH_1:
12592 case FLASH_5755VENDOR_ATMEL_FLASH_2:
12593 case FLASH_5755VENDOR_ATMEL_FLASH_3:
12594 case FLASH_5755VENDOR_ATMEL_FLASH_5:
12595 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012596 tg3_flag_set(tp, NVRAM_BUFFERED);
12597 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000012598 tp->nvram_pagesize = 264;
12599 if (nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_1 ||
12600 nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_5)
12601 tp->nvram_size = (protect ? 0x3e200 :
12602 TG3_NVRAM_SIZE_512KB);
12603 else if (nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_2)
12604 tp->nvram_size = (protect ? 0x1f200 :
12605 TG3_NVRAM_SIZE_256KB);
12606 else
12607 tp->nvram_size = (protect ? 0x1f200 :
12608 TG3_NVRAM_SIZE_128KB);
12609 break;
12610 case FLASH_5752VENDOR_ST_M45PE10:
12611 case FLASH_5752VENDOR_ST_M45PE20:
12612 case FLASH_5752VENDOR_ST_M45PE40:
12613 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000012614 tg3_flag_set(tp, NVRAM_BUFFERED);
12615 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000012616 tp->nvram_pagesize = 256;
12617 if (nvcfg1 == FLASH_5752VENDOR_ST_M45PE10)
12618 tp->nvram_size = (protect ?
12619 TG3_NVRAM_SIZE_64KB :
12620 TG3_NVRAM_SIZE_128KB);
12621 else if (nvcfg1 == FLASH_5752VENDOR_ST_M45PE20)
12622 tp->nvram_size = (protect ?
12623 TG3_NVRAM_SIZE_64KB :
12624 TG3_NVRAM_SIZE_256KB);
12625 else
12626 tp->nvram_size = (protect ?
12627 TG3_NVRAM_SIZE_128KB :
12628 TG3_NVRAM_SIZE_512KB);
12629 break;
Michael Chand3c7b882006-03-23 01:28:25 -080012630 }
12631}
12632
Michael Chan1b277772006-03-20 22:27:48 -080012633static void __devinit tg3_get_5787_nvram_info(struct tg3 *tp)
12634{
12635 u32 nvcfg1;
12636
12637 nvcfg1 = tr32(NVRAM_CFG1);
12638
12639 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000012640 case FLASH_5787VENDOR_ATMEL_EEPROM_64KHZ:
12641 case FLASH_5787VENDOR_ATMEL_EEPROM_376KHZ:
12642 case FLASH_5787VENDOR_MICRO_EEPROM_64KHZ:
12643 case FLASH_5787VENDOR_MICRO_EEPROM_376KHZ:
12644 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012645 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000012646 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
Michael Chan1b277772006-03-20 22:27:48 -080012647
Matt Carlson8590a602009-08-28 12:29:16 +000012648 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
12649 tw32(NVRAM_CFG1, nvcfg1);
12650 break;
12651 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
12652 case FLASH_5755VENDOR_ATMEL_FLASH_1:
12653 case FLASH_5755VENDOR_ATMEL_FLASH_2:
12654 case FLASH_5755VENDOR_ATMEL_FLASH_3:
12655 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012656 tg3_flag_set(tp, NVRAM_BUFFERED);
12657 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000012658 tp->nvram_pagesize = 264;
12659 break;
12660 case FLASH_5752VENDOR_ST_M45PE10:
12661 case FLASH_5752VENDOR_ST_M45PE20:
12662 case FLASH_5752VENDOR_ST_M45PE40:
12663 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000012664 tg3_flag_set(tp, NVRAM_BUFFERED);
12665 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000012666 tp->nvram_pagesize = 256;
12667 break;
Michael Chan1b277772006-03-20 22:27:48 -080012668 }
12669}
12670
Matt Carlson6b91fa02007-10-10 18:01:09 -070012671static void __devinit tg3_get_5761_nvram_info(struct tg3 *tp)
12672{
12673 u32 nvcfg1, protect = 0;
12674
12675 nvcfg1 = tr32(NVRAM_CFG1);
12676
12677 /* NVRAM protection for TPM */
12678 if (nvcfg1 & (1 << 27)) {
Joe Perches63c3a662011-04-26 08:12:10 +000012679 tg3_flag_set(tp, PROTECTED_NVRAM);
Matt Carlson6b91fa02007-10-10 18:01:09 -070012680 protect = 1;
12681 }
12682
12683 nvcfg1 &= NVRAM_CFG1_5752VENDOR_MASK;
12684 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000012685 case FLASH_5761VENDOR_ATMEL_ADB021D:
12686 case FLASH_5761VENDOR_ATMEL_ADB041D:
12687 case FLASH_5761VENDOR_ATMEL_ADB081D:
12688 case FLASH_5761VENDOR_ATMEL_ADB161D:
12689 case FLASH_5761VENDOR_ATMEL_MDB021D:
12690 case FLASH_5761VENDOR_ATMEL_MDB041D:
12691 case FLASH_5761VENDOR_ATMEL_MDB081D:
12692 case FLASH_5761VENDOR_ATMEL_MDB161D:
12693 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012694 tg3_flag_set(tp, NVRAM_BUFFERED);
12695 tg3_flag_set(tp, FLASH);
12696 tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
Matt Carlson8590a602009-08-28 12:29:16 +000012697 tp->nvram_pagesize = 256;
12698 break;
12699 case FLASH_5761VENDOR_ST_A_M45PE20:
12700 case FLASH_5761VENDOR_ST_A_M45PE40:
12701 case FLASH_5761VENDOR_ST_A_M45PE80:
12702 case FLASH_5761VENDOR_ST_A_M45PE16:
12703 case FLASH_5761VENDOR_ST_M_M45PE20:
12704 case FLASH_5761VENDOR_ST_M_M45PE40:
12705 case FLASH_5761VENDOR_ST_M_M45PE80:
12706 case FLASH_5761VENDOR_ST_M_M45PE16:
12707 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000012708 tg3_flag_set(tp, NVRAM_BUFFERED);
12709 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000012710 tp->nvram_pagesize = 256;
12711 break;
Matt Carlson6b91fa02007-10-10 18:01:09 -070012712 }
12713
12714 if (protect) {
12715 tp->nvram_size = tr32(NVRAM_ADDR_LOCKOUT);
12716 } else {
12717 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000012718 case FLASH_5761VENDOR_ATMEL_ADB161D:
12719 case FLASH_5761VENDOR_ATMEL_MDB161D:
12720 case FLASH_5761VENDOR_ST_A_M45PE16:
12721 case FLASH_5761VENDOR_ST_M_M45PE16:
12722 tp->nvram_size = TG3_NVRAM_SIZE_2MB;
12723 break;
12724 case FLASH_5761VENDOR_ATMEL_ADB081D:
12725 case FLASH_5761VENDOR_ATMEL_MDB081D:
12726 case FLASH_5761VENDOR_ST_A_M45PE80:
12727 case FLASH_5761VENDOR_ST_M_M45PE80:
12728 tp->nvram_size = TG3_NVRAM_SIZE_1MB;
12729 break;
12730 case FLASH_5761VENDOR_ATMEL_ADB041D:
12731 case FLASH_5761VENDOR_ATMEL_MDB041D:
12732 case FLASH_5761VENDOR_ST_A_M45PE40:
12733 case FLASH_5761VENDOR_ST_M_M45PE40:
12734 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
12735 break;
12736 case FLASH_5761VENDOR_ATMEL_ADB021D:
12737 case FLASH_5761VENDOR_ATMEL_MDB021D:
12738 case FLASH_5761VENDOR_ST_A_M45PE20:
12739 case FLASH_5761VENDOR_ST_M_M45PE20:
12740 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
12741 break;
Matt Carlson6b91fa02007-10-10 18:01:09 -070012742 }
12743 }
12744}
12745
Michael Chanb5d37722006-09-27 16:06:21 -070012746static void __devinit tg3_get_5906_nvram_info(struct tg3 *tp)
12747{
12748 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012749 tg3_flag_set(tp, NVRAM_BUFFERED);
Michael Chanb5d37722006-09-27 16:06:21 -070012750 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
12751}
12752
Matt Carlson321d32a2008-11-21 17:22:19 -080012753static void __devinit tg3_get_57780_nvram_info(struct tg3 *tp)
12754{
12755 u32 nvcfg1;
12756
12757 nvcfg1 = tr32(NVRAM_CFG1);
12758
12759 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
12760 case FLASH_5787VENDOR_ATMEL_EEPROM_376KHZ:
12761 case FLASH_5787VENDOR_MICRO_EEPROM_376KHZ:
12762 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012763 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson321d32a2008-11-21 17:22:19 -080012764 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
12765
12766 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
12767 tw32(NVRAM_CFG1, nvcfg1);
12768 return;
12769 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
12770 case FLASH_57780VENDOR_ATMEL_AT45DB011D:
12771 case FLASH_57780VENDOR_ATMEL_AT45DB011B:
12772 case FLASH_57780VENDOR_ATMEL_AT45DB021D:
12773 case FLASH_57780VENDOR_ATMEL_AT45DB021B:
12774 case FLASH_57780VENDOR_ATMEL_AT45DB041D:
12775 case FLASH_57780VENDOR_ATMEL_AT45DB041B:
12776 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012777 tg3_flag_set(tp, NVRAM_BUFFERED);
12778 tg3_flag_set(tp, FLASH);
Matt Carlson321d32a2008-11-21 17:22:19 -080012779
12780 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
12781 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
12782 case FLASH_57780VENDOR_ATMEL_AT45DB011D:
12783 case FLASH_57780VENDOR_ATMEL_AT45DB011B:
12784 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
12785 break;
12786 case FLASH_57780VENDOR_ATMEL_AT45DB021D:
12787 case FLASH_57780VENDOR_ATMEL_AT45DB021B:
12788 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
12789 break;
12790 case FLASH_57780VENDOR_ATMEL_AT45DB041D:
12791 case FLASH_57780VENDOR_ATMEL_AT45DB041B:
12792 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
12793 break;
12794 }
12795 break;
12796 case FLASH_5752VENDOR_ST_M45PE10:
12797 case FLASH_5752VENDOR_ST_M45PE20:
12798 case FLASH_5752VENDOR_ST_M45PE40:
12799 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000012800 tg3_flag_set(tp, NVRAM_BUFFERED);
12801 tg3_flag_set(tp, FLASH);
Matt Carlson321d32a2008-11-21 17:22:19 -080012802
12803 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
12804 case FLASH_5752VENDOR_ST_M45PE10:
12805 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
12806 break;
12807 case FLASH_5752VENDOR_ST_M45PE20:
12808 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
12809 break;
12810 case FLASH_5752VENDOR_ST_M45PE40:
12811 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
12812 break;
12813 }
12814 break;
12815 default:
Joe Perches63c3a662011-04-26 08:12:10 +000012816 tg3_flag_set(tp, NO_NVRAM);
Matt Carlson321d32a2008-11-21 17:22:19 -080012817 return;
12818 }
12819
Matt Carlsona1b950d2009-09-01 13:20:17 +000012820 tg3_nvram_get_pagesize(tp, nvcfg1);
12821 if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
Joe Perches63c3a662011-04-26 08:12:10 +000012822 tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
Matt Carlsona1b950d2009-09-01 13:20:17 +000012823}
12824
12825
12826static void __devinit tg3_get_5717_nvram_info(struct tg3 *tp)
12827{
12828 u32 nvcfg1;
12829
12830 nvcfg1 = tr32(NVRAM_CFG1);
12831
12832 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
12833 case FLASH_5717VENDOR_ATMEL_EEPROM:
12834 case FLASH_5717VENDOR_MICRO_EEPROM:
12835 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012836 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlsona1b950d2009-09-01 13:20:17 +000012837 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
12838
12839 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
12840 tw32(NVRAM_CFG1, nvcfg1);
12841 return;
12842 case FLASH_5717VENDOR_ATMEL_MDB011D:
12843 case FLASH_5717VENDOR_ATMEL_ADB011B:
12844 case FLASH_5717VENDOR_ATMEL_ADB011D:
12845 case FLASH_5717VENDOR_ATMEL_MDB021D:
12846 case FLASH_5717VENDOR_ATMEL_ADB021B:
12847 case FLASH_5717VENDOR_ATMEL_ADB021D:
12848 case FLASH_5717VENDOR_ATMEL_45USPT:
12849 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012850 tg3_flag_set(tp, NVRAM_BUFFERED);
12851 tg3_flag_set(tp, FLASH);
Matt Carlsona1b950d2009-09-01 13:20:17 +000012852
12853 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
12854 case FLASH_5717VENDOR_ATMEL_MDB021D:
Matt Carlson66ee33b2011-04-05 14:22:51 +000012855 /* Detect size with tg3_nvram_get_size() */
12856 break;
Matt Carlsona1b950d2009-09-01 13:20:17 +000012857 case FLASH_5717VENDOR_ATMEL_ADB021B:
12858 case FLASH_5717VENDOR_ATMEL_ADB021D:
12859 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
12860 break;
12861 default:
12862 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
12863 break;
12864 }
Matt Carlson321d32a2008-11-21 17:22:19 -080012865 break;
Matt Carlsona1b950d2009-09-01 13:20:17 +000012866 case FLASH_5717VENDOR_ST_M_M25PE10:
12867 case FLASH_5717VENDOR_ST_A_M25PE10:
12868 case FLASH_5717VENDOR_ST_M_M45PE10:
12869 case FLASH_5717VENDOR_ST_A_M45PE10:
12870 case FLASH_5717VENDOR_ST_M_M25PE20:
12871 case FLASH_5717VENDOR_ST_A_M25PE20:
12872 case FLASH_5717VENDOR_ST_M_M45PE20:
12873 case FLASH_5717VENDOR_ST_A_M45PE20:
12874 case FLASH_5717VENDOR_ST_25USPT:
12875 case FLASH_5717VENDOR_ST_45USPT:
12876 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000012877 tg3_flag_set(tp, NVRAM_BUFFERED);
12878 tg3_flag_set(tp, FLASH);
Matt Carlsona1b950d2009-09-01 13:20:17 +000012879
12880 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
12881 case FLASH_5717VENDOR_ST_M_M25PE20:
Matt Carlsona1b950d2009-09-01 13:20:17 +000012882 case FLASH_5717VENDOR_ST_M_M45PE20:
Matt Carlson66ee33b2011-04-05 14:22:51 +000012883 /* Detect size with tg3_nvram_get_size() */
12884 break;
12885 case FLASH_5717VENDOR_ST_A_M25PE20:
Matt Carlsona1b950d2009-09-01 13:20:17 +000012886 case FLASH_5717VENDOR_ST_A_M45PE20:
12887 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
12888 break;
12889 default:
12890 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
12891 break;
12892 }
Matt Carlson321d32a2008-11-21 17:22:19 -080012893 break;
Matt Carlsona1b950d2009-09-01 13:20:17 +000012894 default:
Joe Perches63c3a662011-04-26 08:12:10 +000012895 tg3_flag_set(tp, NO_NVRAM);
Matt Carlsona1b950d2009-09-01 13:20:17 +000012896 return;
Matt Carlson321d32a2008-11-21 17:22:19 -080012897 }
Matt Carlsona1b950d2009-09-01 13:20:17 +000012898
12899 tg3_nvram_get_pagesize(tp, nvcfg1);
12900 if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
Joe Perches63c3a662011-04-26 08:12:10 +000012901 tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
Matt Carlson321d32a2008-11-21 17:22:19 -080012902}
12903
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012904static void __devinit tg3_get_5720_nvram_info(struct tg3 *tp)
12905{
12906 u32 nvcfg1, nvmpinstrp;
12907
12908 nvcfg1 = tr32(NVRAM_CFG1);
12909 nvmpinstrp = nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK;
12910
12911 switch (nvmpinstrp) {
12912 case FLASH_5720_EEPROM_HD:
12913 case FLASH_5720_EEPROM_LD:
12914 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012915 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012916
12917 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
12918 tw32(NVRAM_CFG1, nvcfg1);
12919 if (nvmpinstrp == FLASH_5720_EEPROM_HD)
12920 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
12921 else
12922 tp->nvram_pagesize = ATMEL_AT24C02_CHIP_SIZE;
12923 return;
12924 case FLASH_5720VENDOR_M_ATMEL_DB011D:
12925 case FLASH_5720VENDOR_A_ATMEL_DB011B:
12926 case FLASH_5720VENDOR_A_ATMEL_DB011D:
12927 case FLASH_5720VENDOR_M_ATMEL_DB021D:
12928 case FLASH_5720VENDOR_A_ATMEL_DB021B:
12929 case FLASH_5720VENDOR_A_ATMEL_DB021D:
12930 case FLASH_5720VENDOR_M_ATMEL_DB041D:
12931 case FLASH_5720VENDOR_A_ATMEL_DB041B:
12932 case FLASH_5720VENDOR_A_ATMEL_DB041D:
12933 case FLASH_5720VENDOR_M_ATMEL_DB081D:
12934 case FLASH_5720VENDOR_A_ATMEL_DB081D:
12935 case FLASH_5720VENDOR_ATMEL_45USPT:
12936 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012937 tg3_flag_set(tp, NVRAM_BUFFERED);
12938 tg3_flag_set(tp, FLASH);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012939
12940 switch (nvmpinstrp) {
12941 case FLASH_5720VENDOR_M_ATMEL_DB021D:
12942 case FLASH_5720VENDOR_A_ATMEL_DB021B:
12943 case FLASH_5720VENDOR_A_ATMEL_DB021D:
12944 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
12945 break;
12946 case FLASH_5720VENDOR_M_ATMEL_DB041D:
12947 case FLASH_5720VENDOR_A_ATMEL_DB041B:
12948 case FLASH_5720VENDOR_A_ATMEL_DB041D:
12949 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
12950 break;
12951 case FLASH_5720VENDOR_M_ATMEL_DB081D:
12952 case FLASH_5720VENDOR_A_ATMEL_DB081D:
12953 tp->nvram_size = TG3_NVRAM_SIZE_1MB;
12954 break;
12955 default:
12956 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
12957 break;
12958 }
12959 break;
12960 case FLASH_5720VENDOR_M_ST_M25PE10:
12961 case FLASH_5720VENDOR_M_ST_M45PE10:
12962 case FLASH_5720VENDOR_A_ST_M25PE10:
12963 case FLASH_5720VENDOR_A_ST_M45PE10:
12964 case FLASH_5720VENDOR_M_ST_M25PE20:
12965 case FLASH_5720VENDOR_M_ST_M45PE20:
12966 case FLASH_5720VENDOR_A_ST_M25PE20:
12967 case FLASH_5720VENDOR_A_ST_M45PE20:
12968 case FLASH_5720VENDOR_M_ST_M25PE40:
12969 case FLASH_5720VENDOR_M_ST_M45PE40:
12970 case FLASH_5720VENDOR_A_ST_M25PE40:
12971 case FLASH_5720VENDOR_A_ST_M45PE40:
12972 case FLASH_5720VENDOR_M_ST_M25PE80:
12973 case FLASH_5720VENDOR_M_ST_M45PE80:
12974 case FLASH_5720VENDOR_A_ST_M25PE80:
12975 case FLASH_5720VENDOR_A_ST_M45PE80:
12976 case FLASH_5720VENDOR_ST_25USPT:
12977 case FLASH_5720VENDOR_ST_45USPT:
12978 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000012979 tg3_flag_set(tp, NVRAM_BUFFERED);
12980 tg3_flag_set(tp, FLASH);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012981
12982 switch (nvmpinstrp) {
12983 case FLASH_5720VENDOR_M_ST_M25PE20:
12984 case FLASH_5720VENDOR_M_ST_M45PE20:
12985 case FLASH_5720VENDOR_A_ST_M25PE20:
12986 case FLASH_5720VENDOR_A_ST_M45PE20:
12987 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
12988 break;
12989 case FLASH_5720VENDOR_M_ST_M25PE40:
12990 case FLASH_5720VENDOR_M_ST_M45PE40:
12991 case FLASH_5720VENDOR_A_ST_M25PE40:
12992 case FLASH_5720VENDOR_A_ST_M45PE40:
12993 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
12994 break;
12995 case FLASH_5720VENDOR_M_ST_M25PE80:
12996 case FLASH_5720VENDOR_M_ST_M45PE80:
12997 case FLASH_5720VENDOR_A_ST_M25PE80:
12998 case FLASH_5720VENDOR_A_ST_M45PE80:
12999 tp->nvram_size = TG3_NVRAM_SIZE_1MB;
13000 break;
13001 default:
13002 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
13003 break;
13004 }
13005 break;
13006 default:
Joe Perches63c3a662011-04-26 08:12:10 +000013007 tg3_flag_set(tp, NO_NVRAM);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000013008 return;
13009 }
13010
13011 tg3_nvram_get_pagesize(tp, nvcfg1);
13012 if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
Joe Perches63c3a662011-04-26 08:12:10 +000013013 tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000013014}
13015
Linus Torvalds1da177e2005-04-16 15:20:36 -070013016/* Chips other than 5700/5701 use the NVRAM for fetching info. */
13017static void __devinit tg3_nvram_init(struct tg3 *tp)
13018{
Linus Torvalds1da177e2005-04-16 15:20:36 -070013019 tw32_f(GRC_EEPROM_ADDR,
13020 (EEPROM_ADDR_FSM_RESET |
13021 (EEPROM_DEFAULT_CLOCK_PERIOD <<
13022 EEPROM_ADDR_CLKPERD_SHIFT)));
13023
Michael Chan9d57f012006-12-07 00:23:25 -080013024 msleep(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013025
13026 /* Enable seeprom accesses. */
13027 tw32_f(GRC_LOCAL_CTRL,
13028 tr32(GRC_LOCAL_CTRL) | GRC_LCLCTRL_AUTO_SEEPROM);
13029 udelay(100);
13030
13031 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
13032 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701) {
Joe Perches63c3a662011-04-26 08:12:10 +000013033 tg3_flag_set(tp, NVRAM);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013034
Michael Chanec41c7d2006-01-17 02:40:55 -080013035 if (tg3_nvram_lock(tp)) {
Matt Carlson5129c3a2010-04-05 10:19:23 +000013036 netdev_warn(tp->dev,
13037 "Cannot get nvram lock, %s failed\n",
Joe Perches05dbe002010-02-17 19:44:19 +000013038 __func__);
Michael Chanec41c7d2006-01-17 02:40:55 -080013039 return;
13040 }
Michael Chane6af3012005-04-21 17:12:05 -070013041 tg3_enable_nvram_access(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013042
Matt Carlson989a9d22007-05-05 11:51:05 -070013043 tp->nvram_size = 0;
13044
Michael Chan361b4ac2005-04-21 17:11:21 -070013045 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752)
13046 tg3_get_5752_nvram_info(tp);
Michael Chand3c7b882006-03-23 01:28:25 -080013047 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755)
13048 tg3_get_5755_nvram_info(tp);
Matt Carlsond30cdd22007-10-07 23:28:35 -070013049 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787 ||
Matt Carlson57e69832008-05-25 23:48:31 -070013050 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
13051 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
Michael Chan1b277772006-03-20 22:27:48 -080013052 tg3_get_5787_nvram_info(tp);
Matt Carlson6b91fa02007-10-10 18:01:09 -070013053 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
13054 tg3_get_5761_nvram_info(tp);
Michael Chanb5d37722006-09-27 16:06:21 -070013055 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
13056 tg3_get_5906_nvram_info(tp);
Matt Carlsonb703df62009-12-03 08:36:21 +000013057 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
Matt Carlson55086ad2011-12-14 11:09:59 +000013058 tg3_flag(tp, 57765_CLASS))
Matt Carlson321d32a2008-11-21 17:22:19 -080013059 tg3_get_57780_nvram_info(tp);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000013060 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
13061 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
Matt Carlsona1b950d2009-09-01 13:20:17 +000013062 tg3_get_5717_nvram_info(tp);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000013063 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
13064 tg3_get_5720_nvram_info(tp);
Michael Chan361b4ac2005-04-21 17:11:21 -070013065 else
13066 tg3_get_nvram_info(tp);
13067
Matt Carlson989a9d22007-05-05 11:51:05 -070013068 if (tp->nvram_size == 0)
13069 tg3_get_nvram_size(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013070
Michael Chane6af3012005-04-21 17:12:05 -070013071 tg3_disable_nvram_access(tp);
Michael Chan381291b2005-12-13 21:08:21 -080013072 tg3_nvram_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013073
13074 } else {
Joe Perches63c3a662011-04-26 08:12:10 +000013075 tg3_flag_clear(tp, NVRAM);
13076 tg3_flag_clear(tp, NVRAM_BUFFERED);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013077
13078 tg3_get_eeprom_size(tp);
13079 }
13080}
13081
Linus Torvalds1da177e2005-04-16 15:20:36 -070013082struct subsys_tbl_ent {
13083 u16 subsys_vendor, subsys_devid;
13084 u32 phy_id;
13085};
13086
Matt Carlson24daf2b2010-02-17 15:17:02 +000013087static struct subsys_tbl_ent subsys_id_to_phy_id[] __devinitdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070013088 /* Broadcom boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000013089 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000013090 TG3PCI_SUBDEVICE_ID_BROADCOM_95700A6, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013091 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000013092 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A5, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013093 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000013094 TG3PCI_SUBDEVICE_ID_BROADCOM_95700T6, TG3_PHY_ID_BCM8002 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013095 { TG3PCI_SUBVENDOR_ID_BROADCOM,
13096 TG3PCI_SUBDEVICE_ID_BROADCOM_95700A9, 0 },
13097 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000013098 TG3PCI_SUBDEVICE_ID_BROADCOM_95701T1, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013099 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000013100 TG3PCI_SUBDEVICE_ID_BROADCOM_95701T8, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013101 { TG3PCI_SUBVENDOR_ID_BROADCOM,
13102 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A7, 0 },
13103 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000013104 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A10, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013105 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000013106 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A12, 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_95703AX1, TG3_PHY_ID_BCM5703 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013109 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000013110 TG3PCI_SUBDEVICE_ID_BROADCOM_95703AX2, TG3_PHY_ID_BCM5703 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070013111
13112 /* 3com boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000013113 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000013114 TG3PCI_SUBDEVICE_ID_3COM_3C996T, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013115 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000013116 TG3PCI_SUBDEVICE_ID_3COM_3C996BT, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013117 { TG3PCI_SUBVENDOR_ID_3COM,
13118 TG3PCI_SUBDEVICE_ID_3COM_3C996SX, 0 },
13119 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000013120 TG3PCI_SUBDEVICE_ID_3COM_3C1000T, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013121 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000013122 TG3PCI_SUBDEVICE_ID_3COM_3C940BR01, TG3_PHY_ID_BCM5701 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070013123
13124 /* DELL boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000013125 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000013126 TG3PCI_SUBDEVICE_ID_DELL_VIPER, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013127 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000013128 TG3PCI_SUBDEVICE_ID_DELL_JAGUAR, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013129 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000013130 TG3PCI_SUBDEVICE_ID_DELL_MERLOT, TG3_PHY_ID_BCM5411 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013131 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000013132 TG3PCI_SUBDEVICE_ID_DELL_SLIM_MERLOT, TG3_PHY_ID_BCM5411 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070013133
13134 /* Compaq boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000013135 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000013136 TG3PCI_SUBDEVICE_ID_COMPAQ_BANSHEE, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013137 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000013138 TG3PCI_SUBDEVICE_ID_COMPAQ_BANSHEE_2, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013139 { TG3PCI_SUBVENDOR_ID_COMPAQ,
13140 TG3PCI_SUBDEVICE_ID_COMPAQ_CHANGELING, 0 },
13141 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000013142 TG3PCI_SUBDEVICE_ID_COMPAQ_NC7780, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013143 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000013144 TG3PCI_SUBDEVICE_ID_COMPAQ_NC7780_2, TG3_PHY_ID_BCM5701 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070013145
13146 /* IBM boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000013147 { TG3PCI_SUBVENDOR_ID_IBM,
13148 TG3PCI_SUBDEVICE_ID_IBM_5703SAX2, 0 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013149};
13150
Matt Carlson24daf2b2010-02-17 15:17:02 +000013151static struct subsys_tbl_ent * __devinit tg3_lookup_by_subsys(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013152{
13153 int i;
13154
13155 for (i = 0; i < ARRAY_SIZE(subsys_id_to_phy_id); i++) {
13156 if ((subsys_id_to_phy_id[i].subsys_vendor ==
13157 tp->pdev->subsystem_vendor) &&
13158 (subsys_id_to_phy_id[i].subsys_devid ==
13159 tp->pdev->subsystem_device))
13160 return &subsys_id_to_phy_id[i];
13161 }
13162 return NULL;
13163}
13164
Michael Chan7d0c41e2005-04-21 17:06:20 -070013165static void __devinit tg3_get_eeprom_hw_cfg(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013166{
Linus Torvalds1da177e2005-04-16 15:20:36 -070013167 u32 val;
David S. Millerf49639e2006-06-09 11:58:36 -070013168
Matt Carlson79eb6902010-02-17 15:17:03 +000013169 tp->phy_id = TG3_PHY_ID_INVALID;
Michael Chan7d0c41e2005-04-21 17:06:20 -070013170 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
13171
Gary Zambranoa85feb82007-05-05 11:52:19 -070013172 /* Assume an onboard device and WOL capable by default. */
Joe Perches63c3a662011-04-26 08:12:10 +000013173 tg3_flag_set(tp, EEPROM_WRITE_PROT);
13174 tg3_flag_set(tp, WOL_CAP);
David S. Miller72b845e2006-03-14 14:11:48 -080013175
Michael Chanb5d37722006-09-27 16:06:21 -070013176 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan9d26e212006-12-07 00:21:14 -080013177 if (!(tr32(PCIE_TRANSACTION_CFG) & PCIE_TRANS_CFG_LOM)) {
Joe Perches63c3a662011-04-26 08:12:10 +000013178 tg3_flag_clear(tp, EEPROM_WRITE_PROT);
13179 tg3_flag_set(tp, IS_NIC);
Michael Chan9d26e212006-12-07 00:21:14 -080013180 }
Matt Carlson0527ba32007-10-10 18:03:30 -070013181 val = tr32(VCPU_CFGSHDW);
13182 if (val & VCPU_CFGSHDW_ASPM_DBNC)
Joe Perches63c3a662011-04-26 08:12:10 +000013183 tg3_flag_set(tp, ASPM_WORKAROUND);
Matt Carlson0527ba32007-10-10 18:03:30 -070013184 if ((val & VCPU_CFGSHDW_WOL_ENABLE) &&
Rafael J. Wysocki6fdbab92011-04-28 11:02:15 +000013185 (val & VCPU_CFGSHDW_WOL_MAGPKT)) {
Joe Perches63c3a662011-04-26 08:12:10 +000013186 tg3_flag_set(tp, WOL_ENABLE);
Rafael J. Wysocki6fdbab92011-04-28 11:02:15 +000013187 device_set_wakeup_enable(&tp->pdev->dev, true);
13188 }
Matt Carlson05ac4cb2008-11-03 16:53:46 -080013189 goto done;
Michael Chanb5d37722006-09-27 16:06:21 -070013190 }
13191
Linus Torvalds1da177e2005-04-16 15:20:36 -070013192 tg3_read_mem(tp, NIC_SRAM_DATA_SIG, &val);
13193 if (val == NIC_SRAM_DATA_SIG_MAGIC) {
13194 u32 nic_cfg, led_cfg;
Matt Carlsona9daf362008-05-25 23:49:44 -070013195 u32 nic_phy_id, ver, cfg2 = 0, cfg4 = 0, eeprom_phy_id;
Michael Chan7d0c41e2005-04-21 17:06:20 -070013196 int eeprom_phy_serdes = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013197
13198 tg3_read_mem(tp, NIC_SRAM_DATA_CFG, &nic_cfg);
13199 tp->nic_sram_data_cfg = nic_cfg;
13200
13201 tg3_read_mem(tp, NIC_SRAM_DATA_VER, &ver);
13202 ver >>= NIC_SRAM_DATA_VER_SHIFT;
Matt Carlson6ff6f812011-05-19 12:12:54 +000013203 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
13204 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701 &&
13205 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5703 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070013206 (ver > 0) && (ver < 0x100))
13207 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_2, &cfg2);
13208
Matt Carlsona9daf362008-05-25 23:49:44 -070013209 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
13210 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_4, &cfg4);
13211
Linus Torvalds1da177e2005-04-16 15:20:36 -070013212 if ((nic_cfg & NIC_SRAM_DATA_CFG_PHY_TYPE_MASK) ==
13213 NIC_SRAM_DATA_CFG_PHY_TYPE_FIBER)
13214 eeprom_phy_serdes = 1;
13215
13216 tg3_read_mem(tp, NIC_SRAM_DATA_PHY_ID, &nic_phy_id);
13217 if (nic_phy_id != 0) {
13218 u32 id1 = nic_phy_id & NIC_SRAM_DATA_PHY_ID1_MASK;
13219 u32 id2 = nic_phy_id & NIC_SRAM_DATA_PHY_ID2_MASK;
13220
13221 eeprom_phy_id = (id1 >> 16) << 10;
13222 eeprom_phy_id |= (id2 & 0xfc00) << 16;
13223 eeprom_phy_id |= (id2 & 0x03ff) << 0;
13224 } else
13225 eeprom_phy_id = 0;
13226
Michael Chan7d0c41e2005-04-21 17:06:20 -070013227 tp->phy_id = eeprom_phy_id;
Michael Chan747e8f82005-07-25 12:33:22 -070013228 if (eeprom_phy_serdes) {
Joe Perches63c3a662011-04-26 08:12:10 +000013229 if (!tg3_flag(tp, 5705_PLUS))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013230 tp->phy_flags |= TG3_PHYFLG_PHY_SERDES;
Matt Carlsona50d0792010-06-05 17:24:37 +000013231 else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013232 tp->phy_flags |= TG3_PHYFLG_MII_SERDES;
Michael Chan747e8f82005-07-25 12:33:22 -070013233 }
Michael Chan7d0c41e2005-04-21 17:06:20 -070013234
Joe Perches63c3a662011-04-26 08:12:10 +000013235 if (tg3_flag(tp, 5750_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -070013236 led_cfg = cfg2 & (NIC_SRAM_DATA_CFG_LED_MODE_MASK |
13237 SHASTA_EXT_LED_MODE_MASK);
John W. Linvillecbf46852005-04-21 17:01:29 -070013238 else
Linus Torvalds1da177e2005-04-16 15:20:36 -070013239 led_cfg = nic_cfg & NIC_SRAM_DATA_CFG_LED_MODE_MASK;
13240
13241 switch (led_cfg) {
13242 default:
13243 case NIC_SRAM_DATA_CFG_LED_MODE_PHY_1:
13244 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
13245 break;
13246
13247 case NIC_SRAM_DATA_CFG_LED_MODE_PHY_2:
13248 tp->led_ctrl = LED_CTRL_MODE_PHY_2;
13249 break;
13250
13251 case NIC_SRAM_DATA_CFG_LED_MODE_MAC:
13252 tp->led_ctrl = LED_CTRL_MODE_MAC;
Michael Chan9ba27792005-06-06 15:16:20 -070013253
13254 /* Default to PHY_1_MODE if 0 (MAC_MODE) is
13255 * read on some older 5700/5701 bootcode.
13256 */
13257 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
13258 ASIC_REV_5700 ||
13259 GET_ASIC_REV(tp->pci_chip_rev_id) ==
13260 ASIC_REV_5701)
13261 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
13262
Linus Torvalds1da177e2005-04-16 15:20:36 -070013263 break;
13264
13265 case SHASTA_EXT_LED_SHARED:
13266 tp->led_ctrl = LED_CTRL_MODE_SHARED;
13267 if (tp->pci_chip_rev_id != CHIPREV_ID_5750_A0 &&
13268 tp->pci_chip_rev_id != CHIPREV_ID_5750_A1)
13269 tp->led_ctrl |= (LED_CTRL_MODE_PHY_1 |
13270 LED_CTRL_MODE_PHY_2);
13271 break;
13272
13273 case SHASTA_EXT_LED_MAC:
13274 tp->led_ctrl = LED_CTRL_MODE_SHASTA_MAC;
13275 break;
13276
13277 case SHASTA_EXT_LED_COMBO:
13278 tp->led_ctrl = LED_CTRL_MODE_COMBO;
13279 if (tp->pci_chip_rev_id != CHIPREV_ID_5750_A0)
13280 tp->led_ctrl |= (LED_CTRL_MODE_PHY_1 |
13281 LED_CTRL_MODE_PHY_2);
13282 break;
13283
Stephen Hemminger855e1112008-04-16 16:37:28 -070013284 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013285
13286 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
13287 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) &&
13288 tp->pdev->subsystem_vendor == PCI_VENDOR_ID_DELL)
13289 tp->led_ctrl = LED_CTRL_MODE_PHY_2;
13290
Matt Carlsonb2a5c192008-04-03 21:44:44 -070013291 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX)
13292 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
Matt Carlson5f608912007-11-12 21:17:07 -080013293
Michael Chan9d26e212006-12-07 00:21:14 -080013294 if (nic_cfg & NIC_SRAM_DATA_CFG_EEPROM_WP) {
Joe Perches63c3a662011-04-26 08:12:10 +000013295 tg3_flag_set(tp, EEPROM_WRITE_PROT);
Michael Chan9d26e212006-12-07 00:21:14 -080013296 if ((tp->pdev->subsystem_vendor ==
13297 PCI_VENDOR_ID_ARIMA) &&
13298 (tp->pdev->subsystem_device == 0x205a ||
13299 tp->pdev->subsystem_device == 0x2063))
Joe Perches63c3a662011-04-26 08:12:10 +000013300 tg3_flag_clear(tp, EEPROM_WRITE_PROT);
Michael Chan9d26e212006-12-07 00:21:14 -080013301 } else {
Joe Perches63c3a662011-04-26 08:12:10 +000013302 tg3_flag_clear(tp, EEPROM_WRITE_PROT);
13303 tg3_flag_set(tp, IS_NIC);
Michael Chan9d26e212006-12-07 00:21:14 -080013304 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013305
13306 if (nic_cfg & NIC_SRAM_DATA_CFG_ASF_ENABLE) {
Joe Perches63c3a662011-04-26 08:12:10 +000013307 tg3_flag_set(tp, ENABLE_ASF);
13308 if (tg3_flag(tp, 5750_PLUS))
13309 tg3_flag_set(tp, ASF_NEW_HANDSHAKE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013310 }
Matt Carlsonb2b98d42008-11-03 16:52:32 -080013311
13312 if ((nic_cfg & NIC_SRAM_DATA_CFG_APE_ENABLE) &&
Joe Perches63c3a662011-04-26 08:12:10 +000013313 tg3_flag(tp, 5750_PLUS))
13314 tg3_flag_set(tp, ENABLE_APE);
Matt Carlsonb2b98d42008-11-03 16:52:32 -080013315
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013316 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES &&
Gary Zambranoa85feb82007-05-05 11:52:19 -070013317 !(nic_cfg & NIC_SRAM_DATA_CFG_FIBER_WOL))
Joe Perches63c3a662011-04-26 08:12:10 +000013318 tg3_flag_clear(tp, WOL_CAP);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013319
Joe Perches63c3a662011-04-26 08:12:10 +000013320 if (tg3_flag(tp, WOL_CAP) &&
Rafael J. Wysocki6fdbab92011-04-28 11:02:15 +000013321 (nic_cfg & NIC_SRAM_DATA_CFG_WOL_ENABLE)) {
Joe Perches63c3a662011-04-26 08:12:10 +000013322 tg3_flag_set(tp, WOL_ENABLE);
Rafael J. Wysocki6fdbab92011-04-28 11:02:15 +000013323 device_set_wakeup_enable(&tp->pdev->dev, true);
13324 }
Matt Carlson0527ba32007-10-10 18:03:30 -070013325
Linus Torvalds1da177e2005-04-16 15:20:36 -070013326 if (cfg2 & (1 << 17))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013327 tp->phy_flags |= TG3_PHYFLG_CAPACITIVE_COUPLING;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013328
13329 /* serdes signal pre-emphasis in register 0x590 set by */
13330 /* bootcode if bit 18 is set */
13331 if (cfg2 & (1 << 18))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013332 tp->phy_flags |= TG3_PHYFLG_SERDES_PREEMPHASIS;
Matt Carlson8ed5d972007-05-07 00:25:49 -070013333
Joe Perches63c3a662011-04-26 08:12:10 +000013334 if ((tg3_flag(tp, 57765_PLUS) ||
13335 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
13336 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX)) &&
Matt Carlson6833c042008-11-21 17:18:59 -080013337 (cfg2 & NIC_SRAM_DATA_CFG_2_APD_EN))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013338 tp->phy_flags |= TG3_PHYFLG_ENABLE_APD;
Matt Carlson6833c042008-11-21 17:18:59 -080013339
Joe Perches63c3a662011-04-26 08:12:10 +000013340 if (tg3_flag(tp, PCI_EXPRESS) &&
Matt Carlson8c69b1e2010-08-02 11:26:00 +000013341 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
Joe Perches63c3a662011-04-26 08:12:10 +000013342 !tg3_flag(tp, 57765_PLUS)) {
Matt Carlson8ed5d972007-05-07 00:25:49 -070013343 u32 cfg3;
13344
13345 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_3, &cfg3);
13346 if (cfg3 & NIC_SRAM_ASPM_DEBOUNCE)
Joe Perches63c3a662011-04-26 08:12:10 +000013347 tg3_flag_set(tp, ASPM_WORKAROUND);
Matt Carlson8ed5d972007-05-07 00:25:49 -070013348 }
Matt Carlsona9daf362008-05-25 23:49:44 -070013349
Matt Carlson14417062010-02-17 15:16:59 +000013350 if (cfg4 & NIC_SRAM_RGMII_INBAND_DISABLE)
Joe Perches63c3a662011-04-26 08:12:10 +000013351 tg3_flag_set(tp, RGMII_INBAND_DISABLE);
Matt Carlsona9daf362008-05-25 23:49:44 -070013352 if (cfg4 & NIC_SRAM_RGMII_EXT_IBND_RX_EN)
Joe Perches63c3a662011-04-26 08:12:10 +000013353 tg3_flag_set(tp, RGMII_EXT_IBND_RX_EN);
Matt Carlsona9daf362008-05-25 23:49:44 -070013354 if (cfg4 & NIC_SRAM_RGMII_EXT_IBND_TX_EN)
Joe Perches63c3a662011-04-26 08:12:10 +000013355 tg3_flag_set(tp, RGMII_EXT_IBND_TX_EN);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013356 }
Matt Carlson05ac4cb2008-11-03 16:53:46 -080013357done:
Joe Perches63c3a662011-04-26 08:12:10 +000013358 if (tg3_flag(tp, WOL_CAP))
Rafael J. Wysocki43067ed2011-02-10 06:53:09 +000013359 device_set_wakeup_enable(&tp->pdev->dev,
Joe Perches63c3a662011-04-26 08:12:10 +000013360 tg3_flag(tp, WOL_ENABLE));
Rafael J. Wysocki43067ed2011-02-10 06:53:09 +000013361 else
13362 device_set_wakeup_capable(&tp->pdev->dev, false);
Michael Chan7d0c41e2005-04-21 17:06:20 -070013363}
13364
Matt Carlsonb2a5c192008-04-03 21:44:44 -070013365static int __devinit tg3_issue_otp_command(struct tg3 *tp, u32 cmd)
13366{
13367 int i;
13368 u32 val;
13369
13370 tw32(OTP_CTRL, cmd | OTP_CTRL_OTP_CMD_START);
13371 tw32(OTP_CTRL, cmd);
13372
13373 /* Wait for up to 1 ms for command to execute. */
13374 for (i = 0; i < 100; i++) {
13375 val = tr32(OTP_STATUS);
13376 if (val & OTP_STATUS_CMD_DONE)
13377 break;
13378 udelay(10);
13379 }
13380
13381 return (val & OTP_STATUS_CMD_DONE) ? 0 : -EBUSY;
13382}
13383
13384/* Read the gphy configuration from the OTP region of the chip. The gphy
13385 * configuration is a 32-bit value that straddles the alignment boundary.
13386 * We do two 32-bit reads and then shift and merge the results.
13387 */
13388static u32 __devinit tg3_read_otp_phycfg(struct tg3 *tp)
13389{
13390 u32 bhalf_otp, thalf_otp;
13391
13392 tw32(OTP_MODE, OTP_MODE_OTP_THRU_GRC);
13393
13394 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_INIT))
13395 return 0;
13396
13397 tw32(OTP_ADDRESS, OTP_ADDRESS_MAGIC1);
13398
13399 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_READ))
13400 return 0;
13401
13402 thalf_otp = tr32(OTP_READ_DATA);
13403
13404 tw32(OTP_ADDRESS, OTP_ADDRESS_MAGIC2);
13405
13406 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_READ))
13407 return 0;
13408
13409 bhalf_otp = tr32(OTP_READ_DATA);
13410
13411 return ((thalf_otp & 0x0000ffff) << 16) | (bhalf_otp >> 16);
13412}
13413
Matt Carlsone256f8a2011-03-09 16:58:24 +000013414static void __devinit tg3_phy_init_link_config(struct tg3 *tp)
13415{
Hiroaki SHIMODA202ff1c2011-11-22 04:05:41 +000013416 u32 adv = ADVERTISED_Autoneg;
Matt Carlsone256f8a2011-03-09 16:58:24 +000013417
13418 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
13419 adv |= ADVERTISED_1000baseT_Half |
13420 ADVERTISED_1000baseT_Full;
13421
13422 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
13423 adv |= ADVERTISED_100baseT_Half |
13424 ADVERTISED_100baseT_Full |
13425 ADVERTISED_10baseT_Half |
13426 ADVERTISED_10baseT_Full |
13427 ADVERTISED_TP;
13428 else
13429 adv |= ADVERTISED_FIBRE;
13430
13431 tp->link_config.advertising = adv;
Matt Carlsone7405222012-02-13 15:20:16 +000013432 tp->link_config.speed = SPEED_UNKNOWN;
13433 tp->link_config.duplex = DUPLEX_UNKNOWN;
Matt Carlsone256f8a2011-03-09 16:58:24 +000013434 tp->link_config.autoneg = AUTONEG_ENABLE;
Matt Carlsone7405222012-02-13 15:20:16 +000013435 tp->link_config.active_speed = SPEED_UNKNOWN;
13436 tp->link_config.active_duplex = DUPLEX_UNKNOWN;
Matt Carlson34655ad2012-02-22 12:35:18 +000013437
13438 tp->old_link = -1;
Matt Carlsone256f8a2011-03-09 16:58:24 +000013439}
13440
Michael Chan7d0c41e2005-04-21 17:06:20 -070013441static int __devinit tg3_phy_probe(struct tg3 *tp)
13442{
13443 u32 hw_phy_id_1, hw_phy_id_2;
13444 u32 hw_phy_id, hw_phy_id_masked;
13445 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013446
Matt Carlsone256f8a2011-03-09 16:58:24 +000013447 /* flow control autonegotiation is default behavior */
Joe Perches63c3a662011-04-26 08:12:10 +000013448 tg3_flag_set(tp, PAUSE_AUTONEG);
Matt Carlsone256f8a2011-03-09 16:58:24 +000013449 tp->link_config.flowctrl = FLOW_CTRL_TX | FLOW_CTRL_RX;
13450
Joe Perches63c3a662011-04-26 08:12:10 +000013451 if (tg3_flag(tp, USE_PHYLIB))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070013452 return tg3_phy_init(tp);
13453
Linus Torvalds1da177e2005-04-16 15:20:36 -070013454 /* Reading the PHY ID register can conflict with ASF
Nick Andrew877d0312009-01-26 11:06:57 +010013455 * firmware access to the PHY hardware.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013456 */
13457 err = 0;
Joe Perches63c3a662011-04-26 08:12:10 +000013458 if (tg3_flag(tp, ENABLE_ASF) || tg3_flag(tp, ENABLE_APE)) {
Matt Carlson79eb6902010-02-17 15:17:03 +000013459 hw_phy_id = hw_phy_id_masked = TG3_PHY_ID_INVALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013460 } else {
13461 /* Now read the physical PHY_ID from the chip and verify
13462 * that it is sane. If it doesn't look good, we fall back
13463 * to either the hard-coded table based PHY_ID and failing
13464 * that the value found in the eeprom area.
13465 */
13466 err |= tg3_readphy(tp, MII_PHYSID1, &hw_phy_id_1);
13467 err |= tg3_readphy(tp, MII_PHYSID2, &hw_phy_id_2);
13468
13469 hw_phy_id = (hw_phy_id_1 & 0xffff) << 10;
13470 hw_phy_id |= (hw_phy_id_2 & 0xfc00) << 16;
13471 hw_phy_id |= (hw_phy_id_2 & 0x03ff) << 0;
13472
Matt Carlson79eb6902010-02-17 15:17:03 +000013473 hw_phy_id_masked = hw_phy_id & TG3_PHY_ID_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013474 }
13475
Matt Carlson79eb6902010-02-17 15:17:03 +000013476 if (!err && TG3_KNOWN_PHY_ID(hw_phy_id_masked)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070013477 tp->phy_id = hw_phy_id;
Matt Carlson79eb6902010-02-17 15:17:03 +000013478 if (hw_phy_id_masked == TG3_PHY_ID_BCM8002)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013479 tp->phy_flags |= TG3_PHYFLG_PHY_SERDES;
Michael Chanda6b2d02005-08-19 12:54:29 -070013480 else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013481 tp->phy_flags &= ~TG3_PHYFLG_PHY_SERDES;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013482 } else {
Matt Carlson79eb6902010-02-17 15:17:03 +000013483 if (tp->phy_id != TG3_PHY_ID_INVALID) {
Michael Chan7d0c41e2005-04-21 17:06:20 -070013484 /* Do nothing, phy ID already set up in
13485 * tg3_get_eeprom_hw_cfg().
13486 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070013487 } else {
13488 struct subsys_tbl_ent *p;
13489
13490 /* No eeprom signature? Try the hardcoded
13491 * subsys device table.
13492 */
Matt Carlson24daf2b2010-02-17 15:17:02 +000013493 p = tg3_lookup_by_subsys(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013494 if (!p)
13495 return -ENODEV;
13496
13497 tp->phy_id = p->phy_id;
13498 if (!tp->phy_id ||
Matt Carlson79eb6902010-02-17 15:17:03 +000013499 tp->phy_id == TG3_PHY_ID_BCM8002)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013500 tp->phy_flags |= TG3_PHYFLG_PHY_SERDES;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013501 }
13502 }
13503
Matt Carlsona6b68da2010-12-06 08:28:52 +000013504 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES) &&
Matt Carlson5baa5e92011-07-20 10:20:53 +000013505 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
13506 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720 ||
13507 (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718 &&
Matt Carlsona6b68da2010-12-06 08:28:52 +000013508 tp->pci_chip_rev_id != CHIPREV_ID_5717_A0) ||
13509 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765 &&
13510 tp->pci_chip_rev_id != CHIPREV_ID_57765_A0)))
Matt Carlson52b02d02010-10-14 10:37:41 +000013511 tp->phy_flags |= TG3_PHYFLG_EEE_CAP;
13512
Matt Carlsone256f8a2011-03-09 16:58:24 +000013513 tg3_phy_init_link_config(tp);
13514
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013515 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES) &&
Joe Perches63c3a662011-04-26 08:12:10 +000013516 !tg3_flag(tp, ENABLE_APE) &&
13517 !tg3_flag(tp, ENABLE_ASF)) {
Matt Carlsone2bf73e2011-12-08 14:40:15 +000013518 u32 bmsr, dummy;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013519
13520 tg3_readphy(tp, MII_BMSR, &bmsr);
13521 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
13522 (bmsr & BMSR_LSTATUS))
13523 goto skip_phy_reset;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040013524
Linus Torvalds1da177e2005-04-16 15:20:36 -070013525 err = tg3_phy_reset(tp);
13526 if (err)
13527 return err;
13528
Matt Carlson42b64a42011-05-19 12:12:49 +000013529 tg3_phy_set_wirespeed(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013530
Matt Carlsone2bf73e2011-12-08 14:40:15 +000013531 if (!tg3_phy_copper_an_config_ok(tp, &dummy)) {
Matt Carlson42b64a42011-05-19 12:12:49 +000013532 tg3_phy_autoneg_cfg(tp, tp->link_config.advertising,
13533 tp->link_config.flowctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013534
13535 tg3_writephy(tp, MII_BMCR,
13536 BMCR_ANENABLE | BMCR_ANRESTART);
13537 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013538 }
13539
13540skip_phy_reset:
Matt Carlson79eb6902010-02-17 15:17:03 +000013541 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070013542 err = tg3_init_5401phy_dsp(tp);
13543 if (err)
13544 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013545
Linus Torvalds1da177e2005-04-16 15:20:36 -070013546 err = tg3_init_5401phy_dsp(tp);
13547 }
13548
Linus Torvalds1da177e2005-04-16 15:20:36 -070013549 return err;
13550}
13551
Matt Carlson184b8902010-04-05 10:19:25 +000013552static void __devinit tg3_read_vpd(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013553{
Matt Carlsona4a8bb12010-09-15 09:00:00 +000013554 u8 *vpd_data;
Matt Carlson4181b2c2010-02-26 14:04:45 +000013555 unsigned int block_end, rosize, len;
Matt Carlson535a4902011-07-20 10:20:56 +000013556 u32 vpdlen;
Matt Carlson184b8902010-04-05 10:19:25 +000013557 int j, i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013558
Matt Carlson535a4902011-07-20 10:20:56 +000013559 vpd_data = (u8 *)tg3_vpd_readblock(tp, &vpdlen);
Matt Carlsona4a8bb12010-09-15 09:00:00 +000013560 if (!vpd_data)
13561 goto out_no_vpd;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013562
Matt Carlson535a4902011-07-20 10:20:56 +000013563 i = pci_vpd_find_tag(vpd_data, 0, vpdlen, PCI_VPD_LRDT_RO_DATA);
Matt Carlson4181b2c2010-02-26 14:04:45 +000013564 if (i < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013565 goto out_not_found;
Matt Carlson4181b2c2010-02-26 14:04:45 +000013566
13567 rosize = pci_vpd_lrdt_size(&vpd_data[i]);
13568 block_end = i + PCI_VPD_LRDT_TAG_SIZE + rosize;
13569 i += PCI_VPD_LRDT_TAG_SIZE;
13570
Matt Carlson535a4902011-07-20 10:20:56 +000013571 if (block_end > vpdlen)
Matt Carlson4181b2c2010-02-26 14:04:45 +000013572 goto out_not_found;
13573
Matt Carlson184b8902010-04-05 10:19:25 +000013574 j = pci_vpd_find_info_keyword(vpd_data, i, rosize,
13575 PCI_VPD_RO_KEYWORD_MFR_ID);
13576 if (j > 0) {
13577 len = pci_vpd_info_field_size(&vpd_data[j]);
13578
13579 j += PCI_VPD_INFO_FLD_HDR_SIZE;
13580 if (j + len > block_end || len != 4 ||
13581 memcmp(&vpd_data[j], "1028", 4))
13582 goto partno;
13583
13584 j = pci_vpd_find_info_keyword(vpd_data, i, rosize,
13585 PCI_VPD_RO_KEYWORD_VENDOR0);
13586 if (j < 0)
13587 goto partno;
13588
13589 len = pci_vpd_info_field_size(&vpd_data[j]);
13590
13591 j += PCI_VPD_INFO_FLD_HDR_SIZE;
13592 if (j + len > block_end)
13593 goto partno;
13594
13595 memcpy(tp->fw_ver, &vpd_data[j], len);
Matt Carlson535a4902011-07-20 10:20:56 +000013596 strncat(tp->fw_ver, " bc ", vpdlen - len - 1);
Matt Carlson184b8902010-04-05 10:19:25 +000013597 }
13598
13599partno:
Matt Carlson4181b2c2010-02-26 14:04:45 +000013600 i = pci_vpd_find_info_keyword(vpd_data, i, rosize,
13601 PCI_VPD_RO_KEYWORD_PARTNO);
13602 if (i < 0)
13603 goto out_not_found;
13604
13605 len = pci_vpd_info_field_size(&vpd_data[i]);
13606
13607 i += PCI_VPD_INFO_FLD_HDR_SIZE;
13608 if (len > TG3_BPN_SIZE ||
Matt Carlson535a4902011-07-20 10:20:56 +000013609 (len + i) > vpdlen)
Matt Carlson4181b2c2010-02-26 14:04:45 +000013610 goto out_not_found;
13611
13612 memcpy(tp->board_part_number, &vpd_data[i], len);
13613
Linus Torvalds1da177e2005-04-16 15:20:36 -070013614out_not_found:
Matt Carlsona4a8bb12010-09-15 09:00:00 +000013615 kfree(vpd_data);
Matt Carlson37a949c2010-09-30 10:34:33 +000013616 if (tp->board_part_number[0])
Matt Carlsona4a8bb12010-09-15 09:00:00 +000013617 return;
13618
13619out_no_vpd:
Matt Carlson37a949c2010-09-30 10:34:33 +000013620 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717) {
13621 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717)
13622 strcpy(tp->board_part_number, "BCM5717");
13623 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718)
13624 strcpy(tp->board_part_number, "BCM5718");
13625 else
13626 goto nomatch;
13627 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780) {
13628 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57780)
13629 strcpy(tp->board_part_number, "BCM57780");
13630 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57760)
13631 strcpy(tp->board_part_number, "BCM57760");
13632 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57790)
13633 strcpy(tp->board_part_number, "BCM57790");
13634 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57788)
13635 strcpy(tp->board_part_number, "BCM57788");
13636 else
13637 goto nomatch;
13638 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765) {
13639 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57761)
13640 strcpy(tp->board_part_number, "BCM57761");
13641 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57765)
13642 strcpy(tp->board_part_number, "BCM57765");
13643 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57781)
13644 strcpy(tp->board_part_number, "BCM57781");
13645 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57785)
13646 strcpy(tp->board_part_number, "BCM57785");
13647 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57791)
13648 strcpy(tp->board_part_number, "BCM57791");
13649 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57795)
13650 strcpy(tp->board_part_number, "BCM57795");
13651 else
13652 goto nomatch;
Matt Carlson55086ad2011-12-14 11:09:59 +000013653 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57766) {
13654 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57762)
13655 strcpy(tp->board_part_number, "BCM57762");
13656 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57766)
13657 strcpy(tp->board_part_number, "BCM57766");
13658 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57782)
13659 strcpy(tp->board_part_number, "BCM57782");
13660 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57786)
13661 strcpy(tp->board_part_number, "BCM57786");
13662 else
13663 goto nomatch;
Matt Carlson37a949c2010-09-30 10:34:33 +000013664 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chanb5d37722006-09-27 16:06:21 -070013665 strcpy(tp->board_part_number, "BCM95906");
Matt Carlson37a949c2010-09-30 10:34:33 +000013666 } else {
13667nomatch:
Michael Chanb5d37722006-09-27 16:06:21 -070013668 strcpy(tp->board_part_number, "none");
Matt Carlson37a949c2010-09-30 10:34:33 +000013669 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013670}
13671
Matt Carlson9c8a6202007-10-21 16:16:08 -070013672static int __devinit tg3_fw_img_is_valid(struct tg3 *tp, u32 offset)
13673{
13674 u32 val;
13675
Matt Carlsone4f34112009-02-25 14:25:00 +000013676 if (tg3_nvram_read(tp, offset, &val) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070013677 (val & 0xfc000000) != 0x0c000000 ||
Matt Carlsone4f34112009-02-25 14:25:00 +000013678 tg3_nvram_read(tp, offset + 4, &val) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070013679 val != 0)
13680 return 0;
13681
13682 return 1;
13683}
13684
Matt Carlsonacd9c112009-02-25 14:26:33 +000013685static void __devinit tg3_read_bc_ver(struct tg3 *tp)
13686{
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013687 u32 val, offset, start, ver_offset;
Matt Carlson75f99362010-04-05 10:19:24 +000013688 int i, dst_off;
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013689 bool newver = false;
Matt Carlsonacd9c112009-02-25 14:26:33 +000013690
13691 if (tg3_nvram_read(tp, 0xc, &offset) ||
13692 tg3_nvram_read(tp, 0x4, &start))
13693 return;
13694
13695 offset = tg3_nvram_logical_addr(tp, offset);
13696
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013697 if (tg3_nvram_read(tp, offset, &val))
Matt Carlsonacd9c112009-02-25 14:26:33 +000013698 return;
13699
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013700 if ((val & 0xfc000000) == 0x0c000000) {
13701 if (tg3_nvram_read(tp, offset + 4, &val))
Matt Carlsonacd9c112009-02-25 14:26:33 +000013702 return;
13703
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013704 if (val == 0)
13705 newver = true;
13706 }
13707
Matt Carlson75f99362010-04-05 10:19:24 +000013708 dst_off = strlen(tp->fw_ver);
13709
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013710 if (newver) {
Matt Carlson75f99362010-04-05 10:19:24 +000013711 if (TG3_VER_SIZE - dst_off < 16 ||
13712 tg3_nvram_read(tp, offset + 8, &ver_offset))
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013713 return;
13714
13715 offset = offset + ver_offset - start;
13716 for (i = 0; i < 16; i += 4) {
13717 __be32 v;
13718 if (tg3_nvram_read_be32(tp, offset + i, &v))
13719 return;
13720
Matt Carlson75f99362010-04-05 10:19:24 +000013721 memcpy(tp->fw_ver + dst_off + i, &v, sizeof(v));
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013722 }
13723 } else {
13724 u32 major, minor;
13725
13726 if (tg3_nvram_read(tp, TG3_NVM_PTREV_BCVER, &ver_offset))
13727 return;
13728
13729 major = (ver_offset & TG3_NVM_BCVER_MAJMSK) >>
13730 TG3_NVM_BCVER_MAJSFT;
13731 minor = ver_offset & TG3_NVM_BCVER_MINMSK;
Matt Carlson75f99362010-04-05 10:19:24 +000013732 snprintf(&tp->fw_ver[dst_off], TG3_VER_SIZE - dst_off,
13733 "v%d.%02d", major, minor);
Matt Carlsonacd9c112009-02-25 14:26:33 +000013734 }
13735}
13736
Matt Carlsona6f6cb12009-02-25 14:27:43 +000013737static void __devinit tg3_read_hwsb_ver(struct tg3 *tp)
13738{
13739 u32 val, major, minor;
13740
13741 /* Use native endian representation */
13742 if (tg3_nvram_read(tp, TG3_NVM_HWSB_CFG1, &val))
13743 return;
13744
13745 major = (val & TG3_NVM_HWSB_CFG1_MAJMSK) >>
13746 TG3_NVM_HWSB_CFG1_MAJSFT;
13747 minor = (val & TG3_NVM_HWSB_CFG1_MINMSK) >>
13748 TG3_NVM_HWSB_CFG1_MINSFT;
13749
13750 snprintf(&tp->fw_ver[0], 32, "sb v%d.%02d", major, minor);
13751}
13752
Matt Carlsondfe00d72008-11-21 17:19:41 -080013753static void __devinit tg3_read_sb_ver(struct tg3 *tp, u32 val)
13754{
13755 u32 offset, major, minor, build;
13756
Matt Carlson75f99362010-04-05 10:19:24 +000013757 strncat(tp->fw_ver, "sb", TG3_VER_SIZE - strlen(tp->fw_ver) - 1);
Matt Carlsondfe00d72008-11-21 17:19:41 -080013758
13759 if ((val & TG3_EEPROM_SB_FORMAT_MASK) != TG3_EEPROM_SB_FORMAT_1)
13760 return;
13761
13762 switch (val & TG3_EEPROM_SB_REVISION_MASK) {
13763 case TG3_EEPROM_SB_REVISION_0:
13764 offset = TG3_EEPROM_SB_F1R0_EDH_OFF;
13765 break;
13766 case TG3_EEPROM_SB_REVISION_2:
13767 offset = TG3_EEPROM_SB_F1R2_EDH_OFF;
13768 break;
13769 case TG3_EEPROM_SB_REVISION_3:
13770 offset = TG3_EEPROM_SB_F1R3_EDH_OFF;
13771 break;
Matt Carlsona4153d42010-02-17 15:16:56 +000013772 case TG3_EEPROM_SB_REVISION_4:
13773 offset = TG3_EEPROM_SB_F1R4_EDH_OFF;
13774 break;
13775 case TG3_EEPROM_SB_REVISION_5:
13776 offset = TG3_EEPROM_SB_F1R5_EDH_OFF;
13777 break;
Matt Carlsonbba226a2010-10-14 10:37:38 +000013778 case TG3_EEPROM_SB_REVISION_6:
13779 offset = TG3_EEPROM_SB_F1R6_EDH_OFF;
13780 break;
Matt Carlsondfe00d72008-11-21 17:19:41 -080013781 default:
13782 return;
13783 }
13784
Matt Carlsone4f34112009-02-25 14:25:00 +000013785 if (tg3_nvram_read(tp, offset, &val))
Matt Carlsondfe00d72008-11-21 17:19:41 -080013786 return;
13787
13788 build = (val & TG3_EEPROM_SB_EDH_BLD_MASK) >>
13789 TG3_EEPROM_SB_EDH_BLD_SHFT;
13790 major = (val & TG3_EEPROM_SB_EDH_MAJ_MASK) >>
13791 TG3_EEPROM_SB_EDH_MAJ_SHFT;
13792 minor = val & TG3_EEPROM_SB_EDH_MIN_MASK;
13793
13794 if (minor > 99 || build > 26)
13795 return;
13796
Matt Carlson75f99362010-04-05 10:19:24 +000013797 offset = strlen(tp->fw_ver);
13798 snprintf(&tp->fw_ver[offset], TG3_VER_SIZE - offset,
13799 " v%d.%02d", major, minor);
Matt Carlsondfe00d72008-11-21 17:19:41 -080013800
13801 if (build > 0) {
Matt Carlson75f99362010-04-05 10:19:24 +000013802 offset = strlen(tp->fw_ver);
13803 if (offset < TG3_VER_SIZE - 1)
13804 tp->fw_ver[offset] = 'a' + build - 1;
Matt Carlsondfe00d72008-11-21 17:19:41 -080013805 }
13806}
13807
Matt Carlsonacd9c112009-02-25 14:26:33 +000013808static void __devinit tg3_read_mgmtfw_ver(struct tg3 *tp)
Michael Chanc4e65752006-03-20 22:29:32 -080013809{
13810 u32 val, offset, start;
Matt Carlsonacd9c112009-02-25 14:26:33 +000013811 int i, vlen;
Matt Carlson9c8a6202007-10-21 16:16:08 -070013812
13813 for (offset = TG3_NVM_DIR_START;
13814 offset < TG3_NVM_DIR_END;
13815 offset += TG3_NVM_DIRENT_SIZE) {
Matt Carlsone4f34112009-02-25 14:25:00 +000013816 if (tg3_nvram_read(tp, offset, &val))
Matt Carlson9c8a6202007-10-21 16:16:08 -070013817 return;
13818
13819 if ((val >> TG3_NVM_DIRTYPE_SHIFT) == TG3_NVM_DIRTYPE_ASFINI)
13820 break;
13821 }
13822
13823 if (offset == TG3_NVM_DIR_END)
13824 return;
13825
Joe Perches63c3a662011-04-26 08:12:10 +000013826 if (!tg3_flag(tp, 5705_PLUS))
Matt Carlson9c8a6202007-10-21 16:16:08 -070013827 start = 0x08000000;
Matt Carlsone4f34112009-02-25 14:25:00 +000013828 else if (tg3_nvram_read(tp, offset - 4, &start))
Matt Carlson9c8a6202007-10-21 16:16:08 -070013829 return;
13830
Matt Carlsone4f34112009-02-25 14:25:00 +000013831 if (tg3_nvram_read(tp, offset + 4, &offset) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070013832 !tg3_fw_img_is_valid(tp, offset) ||
Matt Carlsone4f34112009-02-25 14:25:00 +000013833 tg3_nvram_read(tp, offset + 8, &val))
Matt Carlson9c8a6202007-10-21 16:16:08 -070013834 return;
13835
13836 offset += val - start;
13837
Matt Carlsonacd9c112009-02-25 14:26:33 +000013838 vlen = strlen(tp->fw_ver);
Matt Carlson9c8a6202007-10-21 16:16:08 -070013839
Matt Carlsonacd9c112009-02-25 14:26:33 +000013840 tp->fw_ver[vlen++] = ',';
13841 tp->fw_ver[vlen++] = ' ';
Matt Carlson9c8a6202007-10-21 16:16:08 -070013842
13843 for (i = 0; i < 4; i++) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000013844 __be32 v;
13845 if (tg3_nvram_read_be32(tp, offset, &v))
Matt Carlson9c8a6202007-10-21 16:16:08 -070013846 return;
13847
Al Virob9fc7dc2007-12-17 22:59:57 -080013848 offset += sizeof(v);
Matt Carlson9c8a6202007-10-21 16:16:08 -070013849
Matt Carlsonacd9c112009-02-25 14:26:33 +000013850 if (vlen > TG3_VER_SIZE - sizeof(v)) {
13851 memcpy(&tp->fw_ver[vlen], &v, TG3_VER_SIZE - vlen);
Matt Carlson9c8a6202007-10-21 16:16:08 -070013852 break;
13853 }
13854
Matt Carlsonacd9c112009-02-25 14:26:33 +000013855 memcpy(&tp->fw_ver[vlen], &v, sizeof(v));
13856 vlen += sizeof(v);
Matt Carlson9c8a6202007-10-21 16:16:08 -070013857 }
Matt Carlsonacd9c112009-02-25 14:26:33 +000013858}
13859
Matt Carlson7fd76442009-02-25 14:27:20 +000013860static void __devinit tg3_read_dash_ver(struct tg3 *tp)
13861{
13862 int vlen;
13863 u32 apedata;
Matt Carlsonecc79642010-08-02 11:26:01 +000013864 char *fwtype;
Matt Carlson7fd76442009-02-25 14:27:20 +000013865
Joe Perches63c3a662011-04-26 08:12:10 +000013866 if (!tg3_flag(tp, ENABLE_APE) || !tg3_flag(tp, ENABLE_ASF))
Matt Carlson7fd76442009-02-25 14:27:20 +000013867 return;
13868
13869 apedata = tg3_ape_read32(tp, TG3_APE_SEG_SIG);
13870 if (apedata != APE_SEG_SIG_MAGIC)
13871 return;
13872
13873 apedata = tg3_ape_read32(tp, TG3_APE_FW_STATUS);
13874 if (!(apedata & APE_FW_STATUS_READY))
13875 return;
13876
13877 apedata = tg3_ape_read32(tp, TG3_APE_FW_VERSION);
13878
Matt Carlsondc6d0742010-09-15 08:59:55 +000013879 if (tg3_ape_read32(tp, TG3_APE_FW_FEATURES) & TG3_APE_FW_FEATURE_NCSI) {
Joe Perches63c3a662011-04-26 08:12:10 +000013880 tg3_flag_set(tp, APE_HAS_NCSI);
Matt Carlsonecc79642010-08-02 11:26:01 +000013881 fwtype = "NCSI";
Matt Carlsondc6d0742010-09-15 08:59:55 +000013882 } else {
Matt Carlsonecc79642010-08-02 11:26:01 +000013883 fwtype = "DASH";
Matt Carlsondc6d0742010-09-15 08:59:55 +000013884 }
Matt Carlsonecc79642010-08-02 11:26:01 +000013885
Matt Carlson7fd76442009-02-25 14:27:20 +000013886 vlen = strlen(tp->fw_ver);
13887
Matt Carlsonecc79642010-08-02 11:26:01 +000013888 snprintf(&tp->fw_ver[vlen], TG3_VER_SIZE - vlen, " %s v%d.%d.%d.%d",
13889 fwtype,
Matt Carlson7fd76442009-02-25 14:27:20 +000013890 (apedata & APE_FW_VERSION_MAJMSK) >> APE_FW_VERSION_MAJSFT,
13891 (apedata & APE_FW_VERSION_MINMSK) >> APE_FW_VERSION_MINSFT,
13892 (apedata & APE_FW_VERSION_REVMSK) >> APE_FW_VERSION_REVSFT,
13893 (apedata & APE_FW_VERSION_BLDMSK));
13894}
13895
Matt Carlsonacd9c112009-02-25 14:26:33 +000013896static void __devinit tg3_read_fw_ver(struct tg3 *tp)
13897{
13898 u32 val;
Matt Carlson75f99362010-04-05 10:19:24 +000013899 bool vpd_vers = false;
13900
13901 if (tp->fw_ver[0] != 0)
13902 vpd_vers = true;
Matt Carlsonacd9c112009-02-25 14:26:33 +000013903
Joe Perches63c3a662011-04-26 08:12:10 +000013904 if (tg3_flag(tp, NO_NVRAM)) {
Matt Carlson75f99362010-04-05 10:19:24 +000013905 strcat(tp->fw_ver, "sb");
Matt Carlsondf259d82009-04-20 06:57:14 +000013906 return;
13907 }
13908
Matt Carlsonacd9c112009-02-25 14:26:33 +000013909 if (tg3_nvram_read(tp, 0, &val))
13910 return;
13911
13912 if (val == TG3_EEPROM_MAGIC)
13913 tg3_read_bc_ver(tp);
13914 else if ((val & TG3_EEPROM_MAGIC_FW_MSK) == TG3_EEPROM_MAGIC_FW)
13915 tg3_read_sb_ver(tp, val);
Matt Carlsona6f6cb12009-02-25 14:27:43 +000013916 else if ((val & TG3_EEPROM_MAGIC_HW_MSK) == TG3_EEPROM_MAGIC_HW)
13917 tg3_read_hwsb_ver(tp);
Matt Carlsonacd9c112009-02-25 14:26:33 +000013918 else
13919 return;
13920
Matt Carlsonc9cab242011-07-13 09:27:27 +000013921 if (vpd_vers)
Matt Carlson75f99362010-04-05 10:19:24 +000013922 goto done;
Matt Carlsonacd9c112009-02-25 14:26:33 +000013923
Matt Carlsonc9cab242011-07-13 09:27:27 +000013924 if (tg3_flag(tp, ENABLE_APE)) {
13925 if (tg3_flag(tp, ENABLE_ASF))
13926 tg3_read_dash_ver(tp);
13927 } else if (tg3_flag(tp, ENABLE_ASF)) {
13928 tg3_read_mgmtfw_ver(tp);
13929 }
Matt Carlson9c8a6202007-10-21 16:16:08 -070013930
Matt Carlson75f99362010-04-05 10:19:24 +000013931done:
Matt Carlson9c8a6202007-10-21 16:16:08 -070013932 tp->fw_ver[TG3_VER_SIZE - 1] = 0;
Michael Chanc4e65752006-03-20 22:29:32 -080013933}
13934
Matt Carlson7cb32cf2010-09-30 10:34:36 +000013935static inline u32 tg3_rx_ret_ring_size(struct tg3 *tp)
13936{
Joe Perches63c3a662011-04-26 08:12:10 +000013937 if (tg3_flag(tp, LRG_PROD_RING_CAP))
Matt Carlsonde9f5232011-04-05 14:22:43 +000013938 return TG3_RX_RET_MAX_SIZE_5717;
Joe Perches63c3a662011-04-26 08:12:10 +000013939 else if (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS))
Matt Carlsonde9f5232011-04-05 14:22:43 +000013940 return TG3_RX_RET_MAX_SIZE_5700;
Matt Carlson7cb32cf2010-09-30 10:34:36 +000013941 else
Matt Carlsonde9f5232011-04-05 14:22:43 +000013942 return TG3_RX_RET_MAX_SIZE_5705;
Matt Carlson7cb32cf2010-09-30 10:34:36 +000013943}
13944
Matt Carlson41434702011-03-09 16:58:22 +000013945static DEFINE_PCI_DEVICE_TABLE(tg3_write_reorder_chipsets) = {
Joe Perches895950c2010-12-21 02:16:08 -080013946 { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_FE_GATE_700C) },
13947 { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_8131_BRIDGE) },
13948 { PCI_DEVICE(PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_8385_0) },
13949 { },
13950};
13951
Matt Carlson16c7fa72012-02-13 10:20:10 +000013952static struct pci_dev * __devinit tg3_find_peer(struct tg3 *tp)
13953{
13954 struct pci_dev *peer;
13955 unsigned int func, devnr = tp->pdev->devfn & ~7;
13956
13957 for (func = 0; func < 8; func++) {
13958 peer = pci_get_slot(tp->pdev->bus, devnr | func);
13959 if (peer && peer != tp->pdev)
13960 break;
13961 pci_dev_put(peer);
13962 }
13963 /* 5704 can be configured in single-port mode, set peer to
13964 * tp->pdev in that case.
13965 */
13966 if (!peer) {
13967 peer = tp->pdev;
13968 return peer;
13969 }
13970
13971 /*
13972 * We don't need to keep the refcount elevated; there's no way
13973 * to remove one half of this device without removing the other
13974 */
13975 pci_dev_put(peer);
13976
13977 return peer;
13978}
13979
Matt Carlson42b123b2012-02-13 15:20:13 +000013980static void __devinit tg3_detect_asic_rev(struct tg3 *tp, u32 misc_ctrl_reg)
13981{
13982 tp->pci_chip_rev_id = misc_ctrl_reg >> MISC_HOST_CTRL_CHIPREV_SHIFT;
13983 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_USE_PROD_ID_REG) {
13984 u32 reg;
13985
13986 /* All devices that use the alternate
13987 * ASIC REV location have a CPMU.
13988 */
13989 tg3_flag_set(tp, CPMU_PRESENT);
13990
13991 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717 ||
13992 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718 ||
13993 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5719 ||
13994 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5720)
13995 reg = TG3PCI_GEN2_PRODID_ASICREV;
13996 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57781 ||
13997 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57785 ||
13998 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57761 ||
13999 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57765 ||
14000 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57791 ||
14001 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57795 ||
14002 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57762 ||
14003 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57766 ||
14004 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57782 ||
14005 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57786)
14006 reg = TG3PCI_GEN15_PRODID_ASICREV;
14007 else
14008 reg = TG3PCI_PRODID_ASICREV;
14009
14010 pci_read_config_dword(tp->pdev, reg, &tp->pci_chip_rev_id);
14011 }
14012
14013 /* Wrong chip ID in 5752 A0. This code can be removed later
14014 * as A0 is not in production.
14015 */
14016 if (tp->pci_chip_rev_id == CHIPREV_ID_5752_A0_HW)
14017 tp->pci_chip_rev_id = CHIPREV_ID_5752_A0;
14018
14019 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
14020 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
14021 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
14022 tg3_flag_set(tp, 5717_PLUS);
14023
14024 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765 ||
14025 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57766)
14026 tg3_flag_set(tp, 57765_CLASS);
14027
14028 if (tg3_flag(tp, 57765_CLASS) || tg3_flag(tp, 5717_PLUS))
14029 tg3_flag_set(tp, 57765_PLUS);
14030
14031 /* Intentionally exclude ASIC_REV_5906 */
14032 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
14033 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787 ||
14034 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
14035 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
14036 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
14037 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
14038 tg3_flag(tp, 57765_PLUS))
14039 tg3_flag_set(tp, 5755_PLUS);
14040
14041 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780 ||
14042 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714)
14043 tg3_flag_set(tp, 5780_CLASS);
14044
14045 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 ||
14046 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752 ||
14047 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906 ||
14048 tg3_flag(tp, 5755_PLUS) ||
14049 tg3_flag(tp, 5780_CLASS))
14050 tg3_flag_set(tp, 5750_PLUS);
14051
14052 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 ||
14053 tg3_flag(tp, 5750_PLUS))
14054 tg3_flag_set(tp, 5705_PLUS);
14055}
14056
Linus Torvalds1da177e2005-04-16 15:20:36 -070014057static int __devinit tg3_get_invariants(struct tg3 *tp)
14058{
Linus Torvalds1da177e2005-04-16 15:20:36 -070014059 u32 misc_ctrl_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014060 u32 pci_state_reg, grc_misc_cfg;
14061 u32 val;
14062 u16 pci_cmd;
Matt Carlson5e7dfd02008-11-21 17:18:16 -080014063 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014064
Linus Torvalds1da177e2005-04-16 15:20:36 -070014065 /* Force memory write invalidate off. If we leave it on,
14066 * then on 5700_BX chips we have to enable a workaround.
14067 * The workaround is to set the TG3PCI_DMA_RW_CTRL boundary
14068 * to match the cacheline size. The Broadcom driver have this
14069 * workaround but turns MWI off all the times so never uses
14070 * it. This seems to suggest that the workaround is insufficient.
14071 */
14072 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
14073 pci_cmd &= ~PCI_COMMAND_INVALIDATE;
14074 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
14075
Matt Carlson16821282011-07-13 09:27:28 +000014076 /* Important! -- Make sure register accesses are byteswapped
14077 * correctly. Also, for those chips that require it, make
14078 * sure that indirect register accesses are enabled before
14079 * the first operation.
Linus Torvalds1da177e2005-04-16 15:20:36 -070014080 */
14081 pci_read_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
14082 &misc_ctrl_reg);
Matt Carlson16821282011-07-13 09:27:28 +000014083 tp->misc_host_ctrl |= (misc_ctrl_reg &
14084 MISC_HOST_CTRL_CHIPREV);
14085 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
14086 tp->misc_host_ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014087
Matt Carlson42b123b2012-02-13 15:20:13 +000014088 tg3_detect_asic_rev(tp, misc_ctrl_reg);
Michael Chanff645be2005-04-21 17:09:53 -070014089
Michael Chan68929142005-08-09 20:17:14 -070014090 /* If we have 5702/03 A1 or A2 on certain ICH chipsets,
14091 * we need to disable memory and use config. cycles
14092 * only to access all registers. The 5702/03 chips
14093 * can mistakenly decode the special cycles from the
14094 * ICH chipsets as memory write cycles, causing corruption
14095 * of register and memory space. Only certain ICH bridges
14096 * will drive special cycles with non-zero data during the
14097 * address phase which can fall within the 5703's address
14098 * range. This is not an ICH bug as the PCI spec allows
14099 * non-zero address during special cycles. However, only
14100 * these ICH bridges are known to drive non-zero addresses
14101 * during special cycles.
14102 *
14103 * Since special cycles do not cross PCI bridges, we only
14104 * enable this workaround if the 5703 is on the secondary
14105 * bus of these ICH bridges.
14106 */
14107 if ((tp->pci_chip_rev_id == CHIPREV_ID_5703_A1) ||
14108 (tp->pci_chip_rev_id == CHIPREV_ID_5703_A2)) {
14109 static struct tg3_dev_id {
14110 u32 vendor;
14111 u32 device;
14112 u32 rev;
14113 } ich_chipsets[] = {
14114 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AA_8,
14115 PCI_ANY_ID },
14116 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AB_8,
14117 PCI_ANY_ID },
14118 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_11,
14119 0xa },
14120 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_6,
14121 PCI_ANY_ID },
14122 { },
14123 };
14124 struct tg3_dev_id *pci_id = &ich_chipsets[0];
14125 struct pci_dev *bridge = NULL;
14126
14127 while (pci_id->vendor != 0) {
14128 bridge = pci_get_device(pci_id->vendor, pci_id->device,
14129 bridge);
14130 if (!bridge) {
14131 pci_id++;
14132 continue;
14133 }
14134 if (pci_id->rev != PCI_ANY_ID) {
Auke Kok44c10132007-06-08 15:46:36 -070014135 if (bridge->revision > pci_id->rev)
Michael Chan68929142005-08-09 20:17:14 -070014136 continue;
14137 }
14138 if (bridge->subordinate &&
14139 (bridge->subordinate->number ==
14140 tp->pdev->bus->number)) {
Joe Perches63c3a662011-04-26 08:12:10 +000014141 tg3_flag_set(tp, ICH_WORKAROUND);
Michael Chan68929142005-08-09 20:17:14 -070014142 pci_dev_put(bridge);
14143 break;
14144 }
14145 }
14146 }
14147
Matt Carlson6ff6f812011-05-19 12:12:54 +000014148 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
Matt Carlson41588ba2008-04-19 18:12:33 -070014149 static struct tg3_dev_id {
14150 u32 vendor;
14151 u32 device;
14152 } bridge_chipsets[] = {
14153 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PXH_0 },
14154 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PXH_1 },
14155 { },
14156 };
14157 struct tg3_dev_id *pci_id = &bridge_chipsets[0];
14158 struct pci_dev *bridge = NULL;
14159
14160 while (pci_id->vendor != 0) {
14161 bridge = pci_get_device(pci_id->vendor,
14162 pci_id->device,
14163 bridge);
14164 if (!bridge) {
14165 pci_id++;
14166 continue;
14167 }
14168 if (bridge->subordinate &&
14169 (bridge->subordinate->number <=
14170 tp->pdev->bus->number) &&
14171 (bridge->subordinate->subordinate >=
14172 tp->pdev->bus->number)) {
Joe Perches63c3a662011-04-26 08:12:10 +000014173 tg3_flag_set(tp, 5701_DMA_BUG);
Matt Carlson41588ba2008-04-19 18:12:33 -070014174 pci_dev_put(bridge);
14175 break;
14176 }
14177 }
14178 }
14179
Michael Chan4a29cc22006-03-19 13:21:12 -080014180 /* The EPB bridge inside 5714, 5715, and 5780 cannot support
14181 * DMA addresses > 40-bit. This bridge may have other additional
14182 * 57xx devices behind it in some 4-port NIC designs for example.
14183 * Any tg3 device found behind the bridge will also need the 40-bit
14184 * DMA workaround.
14185 */
Matt Carlson42b123b2012-02-13 15:20:13 +000014186 if (tg3_flag(tp, 5780_CLASS)) {
Joe Perches63c3a662011-04-26 08:12:10 +000014187 tg3_flag_set(tp, 40BIT_DMA_BUG);
Michael Chan4cf78e42005-07-25 12:29:19 -070014188 tp->msi_cap = pci_find_capability(tp->pdev, PCI_CAP_ID_MSI);
Matt Carlson859a588792010-04-05 10:19:28 +000014189 } else {
Michael Chan4a29cc22006-03-19 13:21:12 -080014190 struct pci_dev *bridge = NULL;
14191
14192 do {
14193 bridge = pci_get_device(PCI_VENDOR_ID_SERVERWORKS,
14194 PCI_DEVICE_ID_SERVERWORKS_EPB,
14195 bridge);
14196 if (bridge && bridge->subordinate &&
14197 (bridge->subordinate->number <=
14198 tp->pdev->bus->number) &&
14199 (bridge->subordinate->subordinate >=
14200 tp->pdev->bus->number)) {
Joe Perches63c3a662011-04-26 08:12:10 +000014201 tg3_flag_set(tp, 40BIT_DMA_BUG);
Michael Chan4a29cc22006-03-19 13:21:12 -080014202 pci_dev_put(bridge);
14203 break;
14204 }
14205 } while (bridge);
14206 }
Michael Chan4cf78e42005-07-25 12:29:19 -070014207
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000014208 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
Matt Carlson3a1e19d2011-07-13 09:27:32 +000014209 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714)
Michael Chan7544b092007-05-05 13:08:32 -070014210 tp->pdev_peer = tg3_find_peer(tp);
14211
Matt Carlson507399f2009-11-13 13:03:37 +000014212 /* Determine TSO capabilities */
Matt Carlsona0512942011-07-27 14:20:54 +000014213 if (tp->pci_chip_rev_id == CHIPREV_ID_5719_A0)
Matt Carlson4d163b72011-01-25 15:58:48 +000014214 ; /* Do nothing. HW bug. */
Joe Perches63c3a662011-04-26 08:12:10 +000014215 else if (tg3_flag(tp, 57765_PLUS))
14216 tg3_flag_set(tp, HW_TSO_3);
14217 else if (tg3_flag(tp, 5755_PLUS) ||
Matt Carlsone849cdc2009-11-13 13:03:38 +000014218 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
Joe Perches63c3a662011-04-26 08:12:10 +000014219 tg3_flag_set(tp, HW_TSO_2);
14220 else if (tg3_flag(tp, 5750_PLUS)) {
14221 tg3_flag_set(tp, HW_TSO_1);
14222 tg3_flag_set(tp, TSO_BUG);
Matt Carlson507399f2009-11-13 13:03:37 +000014223 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 &&
14224 tp->pci_chip_rev_id >= CHIPREV_ID_5750_C2)
Joe Perches63c3a662011-04-26 08:12:10 +000014225 tg3_flag_clear(tp, TSO_BUG);
Matt Carlson507399f2009-11-13 13:03:37 +000014226 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
14227 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701 &&
14228 tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) {
Joe Perches63c3a662011-04-26 08:12:10 +000014229 tg3_flag_set(tp, TSO_BUG);
Matt Carlson507399f2009-11-13 13:03:37 +000014230 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705)
14231 tp->fw_needed = FIRMWARE_TG3TSO5;
14232 else
14233 tp->fw_needed = FIRMWARE_TG3TSO;
14234 }
14235
Matt Carlsondabc5c62011-05-19 12:12:52 +000014236 /* Selectively allow TSO based on operating conditions */
Matt Carlson6ff6f812011-05-19 12:12:54 +000014237 if (tg3_flag(tp, HW_TSO_1) ||
14238 tg3_flag(tp, HW_TSO_2) ||
14239 tg3_flag(tp, HW_TSO_3) ||
Matt Carlsoncf9ecf42011-11-28 09:41:03 +000014240 tp->fw_needed) {
14241 /* For firmware TSO, assume ASF is disabled.
14242 * We'll disable TSO later if we discover ASF
14243 * is enabled in tg3_get_eeprom_hw_cfg().
14244 */
Matt Carlsondabc5c62011-05-19 12:12:52 +000014245 tg3_flag_set(tp, TSO_CAPABLE);
Matt Carlsoncf9ecf42011-11-28 09:41:03 +000014246 } else {
Matt Carlsondabc5c62011-05-19 12:12:52 +000014247 tg3_flag_clear(tp, TSO_CAPABLE);
14248 tg3_flag_clear(tp, TSO_BUG);
14249 tp->fw_needed = NULL;
14250 }
14251
14252 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0)
14253 tp->fw_needed = FIRMWARE_TG3;
14254
Matt Carlson507399f2009-11-13 13:03:37 +000014255 tp->irq_max = 1;
14256
Joe Perches63c3a662011-04-26 08:12:10 +000014257 if (tg3_flag(tp, 5750_PLUS)) {
14258 tg3_flag_set(tp, SUPPORT_MSI);
Michael Chan7544b092007-05-05 13:08:32 -070014259 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_AX ||
14260 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_BX ||
14261 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714 &&
14262 tp->pci_chip_rev_id <= CHIPREV_ID_5714_A2 &&
14263 tp->pdev_peer == tp->pdev))
Joe Perches63c3a662011-04-26 08:12:10 +000014264 tg3_flag_clear(tp, SUPPORT_MSI);
Michael Chan7544b092007-05-05 13:08:32 -070014265
Joe Perches63c3a662011-04-26 08:12:10 +000014266 if (tg3_flag(tp, 5755_PLUS) ||
Michael Chanb5d37722006-09-27 16:06:21 -070014267 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Joe Perches63c3a662011-04-26 08:12:10 +000014268 tg3_flag_set(tp, 1SHOT_MSI);
Michael Chan52c0fd82006-06-29 20:15:54 -070014269 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014270
Joe Perches63c3a662011-04-26 08:12:10 +000014271 if (tg3_flag(tp, 57765_PLUS)) {
14272 tg3_flag_set(tp, SUPPORT_MSIX);
Matt Carlson507399f2009-11-13 13:03:37 +000014273 tp->irq_max = TG3_IRQ_MAX_VECS;
Matt Carlson90415472011-12-16 13:33:23 +000014274 tg3_rss_init_dflt_indir_tbl(tp);
Matt Carlson507399f2009-11-13 13:03:37 +000014275 }
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000014276 }
Matt Carlson0e1406d2009-11-02 12:33:33 +000014277
Matt Carlsonb7abee62012-06-07 12:56:54 +000014278 if (tg3_flag(tp, 5755_PLUS) ||
14279 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
Joe Perches63c3a662011-04-26 08:12:10 +000014280 tg3_flag_set(tp, SHORT_DMA_BUG);
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000014281
Matt Carlsone31aa982011-07-27 14:20:53 +000014282 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
Matt Carlsona4cb4282011-12-14 11:09:58 +000014283 tp->dma_limit = TG3_TX_BD_DMA_MAX_4K;
Matt Carlsone31aa982011-07-27 14:20:53 +000014284
Matt Carlsonfa6b2aa2011-11-21 15:01:19 +000014285 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
14286 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
14287 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
Joe Perches63c3a662011-04-26 08:12:10 +000014288 tg3_flag_set(tp, LRG_PROD_RING_CAP);
Matt Carlsonde9f5232011-04-05 14:22:43 +000014289
Joe Perches63c3a662011-04-26 08:12:10 +000014290 if (tg3_flag(tp, 57765_PLUS) &&
Matt Carlsona0512942011-07-27 14:20:54 +000014291 tp->pci_chip_rev_id != CHIPREV_ID_5719_A0)
Joe Perches63c3a662011-04-26 08:12:10 +000014292 tg3_flag_set(tp, USE_JUMBO_BDFLAG);
Matt Carlsonb703df62009-12-03 08:36:21 +000014293
Joe Perches63c3a662011-04-26 08:12:10 +000014294 if (!tg3_flag(tp, 5705_PLUS) ||
14295 tg3_flag(tp, 5780_CLASS) ||
14296 tg3_flag(tp, USE_JUMBO_BDFLAG))
14297 tg3_flag_set(tp, JUMBO_CAPABLE);
Michael Chan0f893dc2005-07-25 12:30:38 -070014298
Matt Carlson52f44902008-11-21 17:17:04 -080014299 pci_read_config_dword(tp->pdev, TG3PCI_PCISTATE,
14300 &pci_state_reg);
14301
Jon Mason708ebb3a2011-06-27 12:56:50 +000014302 if (pci_is_pcie(tp->pdev)) {
Matt Carlson5e7dfd02008-11-21 17:18:16 -080014303 u16 lnkctl;
14304
Joe Perches63c3a662011-04-26 08:12:10 +000014305 tg3_flag_set(tp, PCI_EXPRESS);
Matt Carlson5f5c51e2007-11-12 21:19:37 -080014306
Matt Carlson5e7dfd02008-11-21 17:18:16 -080014307 pci_read_config_word(tp->pdev,
Jon Mason708ebb3a2011-06-27 12:56:50 +000014308 pci_pcie_cap(tp->pdev) + PCI_EXP_LNKCTL,
Matt Carlson5e7dfd02008-11-21 17:18:16 -080014309 &lnkctl);
14310 if (lnkctl & PCI_EXP_LNKCTL_CLKREQ_EN) {
Matt Carlson7196cd62011-05-19 16:02:44 +000014311 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
14312 ASIC_REV_5906) {
Joe Perches63c3a662011-04-26 08:12:10 +000014313 tg3_flag_clear(tp, HW_TSO_2);
Matt Carlsondabc5c62011-05-19 12:12:52 +000014314 tg3_flag_clear(tp, TSO_CAPABLE);
Matt Carlson7196cd62011-05-19 16:02:44 +000014315 }
Matt Carlson5e7dfd02008-11-21 17:18:16 -080014316 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson321d32a2008-11-21 17:22:19 -080014317 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson9cf74eb2009-04-20 06:58:27 +000014318 tp->pci_chip_rev_id == CHIPREV_ID_57780_A0 ||
14319 tp->pci_chip_rev_id == CHIPREV_ID_57780_A1)
Joe Perches63c3a662011-04-26 08:12:10 +000014320 tg3_flag_set(tp, CLKREQ_BUG);
Matt Carlson614b0592010-01-20 16:58:02 +000014321 } else if (tp->pci_chip_rev_id == CHIPREV_ID_5717_A0) {
Joe Perches63c3a662011-04-26 08:12:10 +000014322 tg3_flag_set(tp, L1PLLPD_EN);
Michael Chanc7835a72006-11-15 21:14:42 -080014323 }
Matt Carlson52f44902008-11-21 17:17:04 -080014324 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785) {
Jon Mason708ebb3a2011-06-27 12:56:50 +000014325 /* BCM5785 devices are effectively PCIe devices, and should
14326 * follow PCIe codepaths, but do not have a PCIe capabilities
14327 * section.
Matt Carlson93a700a2011-08-31 11:44:54 +000014328 */
Joe Perches63c3a662011-04-26 08:12:10 +000014329 tg3_flag_set(tp, PCI_EXPRESS);
14330 } else if (!tg3_flag(tp, 5705_PLUS) ||
14331 tg3_flag(tp, 5780_CLASS)) {
Matt Carlson52f44902008-11-21 17:17:04 -080014332 tp->pcix_cap = pci_find_capability(tp->pdev, PCI_CAP_ID_PCIX);
14333 if (!tp->pcix_cap) {
Matt Carlson2445e462010-04-05 10:19:21 +000014334 dev_err(&tp->pdev->dev,
14335 "Cannot find PCI-X capability, aborting\n");
Matt Carlson52f44902008-11-21 17:17:04 -080014336 return -EIO;
14337 }
14338
14339 if (!(pci_state_reg & PCISTATE_CONV_PCI_MODE))
Joe Perches63c3a662011-04-26 08:12:10 +000014340 tg3_flag_set(tp, PCIX_MODE);
Matt Carlson52f44902008-11-21 17:17:04 -080014341 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014342
Michael Chan399de502005-10-03 14:02:39 -070014343 /* If we have an AMD 762 or VIA K8T800 chipset, write
14344 * reordering to the mailbox registers done by the host
14345 * controller can cause major troubles. We read back from
14346 * every mailbox register write to force the writes to be
14347 * posted to the chip in order.
14348 */
Matt Carlson41434702011-03-09 16:58:22 +000014349 if (pci_dev_present(tg3_write_reorder_chipsets) &&
Joe Perches63c3a662011-04-26 08:12:10 +000014350 !tg3_flag(tp, PCI_EXPRESS))
14351 tg3_flag_set(tp, MBOX_WRITE_REORDER);
Michael Chan399de502005-10-03 14:02:39 -070014352
Matt Carlson69fc4052008-12-21 20:19:57 -080014353 pci_read_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE,
14354 &tp->pci_cacheline_sz);
14355 pci_read_config_byte(tp->pdev, PCI_LATENCY_TIMER,
14356 &tp->pci_lat_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014357 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 &&
14358 tp->pci_lat_timer < 64) {
14359 tp->pci_lat_timer = 64;
Matt Carlson69fc4052008-12-21 20:19:57 -080014360 pci_write_config_byte(tp->pdev, PCI_LATENCY_TIMER,
14361 tp->pci_lat_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014362 }
14363
Matt Carlson16821282011-07-13 09:27:28 +000014364 /* Important! -- It is critical that the PCI-X hw workaround
14365 * situation is decided before the first MMIO register access.
14366 */
Matt Carlson52f44902008-11-21 17:17:04 -080014367 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5700_BX) {
14368 /* 5700 BX chips need to have their TX producer index
14369 * mailboxes written twice to workaround a bug.
14370 */
Joe Perches63c3a662011-04-26 08:12:10 +000014371 tg3_flag_set(tp, TXD_MBOX_HWBUG);
Matt Carlson9974a352007-10-07 23:27:28 -070014372
Matt Carlson52f44902008-11-21 17:17:04 -080014373 /* If we are in PCI-X mode, enable register write workaround.
Linus Torvalds1da177e2005-04-16 15:20:36 -070014374 *
14375 * The workaround is to use indirect register accesses
14376 * for all chip writes not to mailbox registers.
14377 */
Joe Perches63c3a662011-04-26 08:12:10 +000014378 if (tg3_flag(tp, PCIX_MODE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070014379 u32 pm_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014380
Joe Perches63c3a662011-04-26 08:12:10 +000014381 tg3_flag_set(tp, PCIX_TARGET_HWBUG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014382
14383 /* The chip can have it's power management PCI config
14384 * space registers clobbered due to this bug.
14385 * So explicitly force the chip into D0 here.
14386 */
Matt Carlson9974a352007-10-07 23:27:28 -070014387 pci_read_config_dword(tp->pdev,
14388 tp->pm_cap + PCI_PM_CTRL,
Linus Torvalds1da177e2005-04-16 15:20:36 -070014389 &pm_reg);
14390 pm_reg &= ~PCI_PM_CTRL_STATE_MASK;
14391 pm_reg |= PCI_PM_CTRL_PME_ENABLE | 0 /* D0 */;
Matt Carlson9974a352007-10-07 23:27:28 -070014392 pci_write_config_dword(tp->pdev,
14393 tp->pm_cap + PCI_PM_CTRL,
Linus Torvalds1da177e2005-04-16 15:20:36 -070014394 pm_reg);
14395
14396 /* Also, force SERR#/PERR# in PCI command. */
14397 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
14398 pci_cmd |= PCI_COMMAND_PARITY | PCI_COMMAND_SERR;
14399 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
14400 }
14401 }
14402
Linus Torvalds1da177e2005-04-16 15:20:36 -070014403 if ((pci_state_reg & PCISTATE_BUS_SPEED_HIGH) != 0)
Joe Perches63c3a662011-04-26 08:12:10 +000014404 tg3_flag_set(tp, PCI_HIGH_SPEED);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014405 if ((pci_state_reg & PCISTATE_BUS_32BIT) != 0)
Joe Perches63c3a662011-04-26 08:12:10 +000014406 tg3_flag_set(tp, PCI_32BIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014407
14408 /* Chip-specific fixup from Broadcom driver */
14409 if ((tp->pci_chip_rev_id == CHIPREV_ID_5704_A0) &&
14410 (!(pci_state_reg & PCISTATE_RETRY_SAME_DMA))) {
14411 pci_state_reg |= PCISTATE_RETRY_SAME_DMA;
14412 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE, pci_state_reg);
14413 }
14414
Michael Chan1ee582d2005-08-09 20:16:46 -070014415 /* Default fast path register access methods */
Michael Chan20094932005-08-09 20:16:32 -070014416 tp->read32 = tg3_read32;
Michael Chan1ee582d2005-08-09 20:16:46 -070014417 tp->write32 = tg3_write32;
Michael Chan09ee9292005-08-09 20:17:00 -070014418 tp->read32_mbox = tg3_read32;
Michael Chan20094932005-08-09 20:16:32 -070014419 tp->write32_mbox = tg3_write32;
Michael Chan1ee582d2005-08-09 20:16:46 -070014420 tp->write32_tx_mbox = tg3_write32;
14421 tp->write32_rx_mbox = tg3_write32;
14422
14423 /* Various workaround register access methods */
Joe Perches63c3a662011-04-26 08:12:10 +000014424 if (tg3_flag(tp, PCIX_TARGET_HWBUG))
Michael Chan1ee582d2005-08-09 20:16:46 -070014425 tp->write32 = tg3_write_indirect_reg32;
Matt Carlson98efd8a2007-05-05 12:47:25 -070014426 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701 ||
Joe Perches63c3a662011-04-26 08:12:10 +000014427 (tg3_flag(tp, PCI_EXPRESS) &&
Matt Carlson98efd8a2007-05-05 12:47:25 -070014428 tp->pci_chip_rev_id == CHIPREV_ID_5750_A0)) {
14429 /*
14430 * Back to back register writes can cause problems on these
14431 * chips, the workaround is to read back all reg writes
14432 * except those to mailbox regs.
14433 *
14434 * See tg3_write_indirect_reg32().
14435 */
Michael Chan1ee582d2005-08-09 20:16:46 -070014436 tp->write32 = tg3_write_flush_reg32;
Matt Carlson98efd8a2007-05-05 12:47:25 -070014437 }
14438
Joe Perches63c3a662011-04-26 08:12:10 +000014439 if (tg3_flag(tp, TXD_MBOX_HWBUG) || tg3_flag(tp, MBOX_WRITE_REORDER)) {
Michael Chan1ee582d2005-08-09 20:16:46 -070014440 tp->write32_tx_mbox = tg3_write32_tx_mbox;
Joe Perches63c3a662011-04-26 08:12:10 +000014441 if (tg3_flag(tp, MBOX_WRITE_REORDER))
Michael Chan1ee582d2005-08-09 20:16:46 -070014442 tp->write32_rx_mbox = tg3_write_flush_reg32;
14443 }
Michael Chan20094932005-08-09 20:16:32 -070014444
Joe Perches63c3a662011-04-26 08:12:10 +000014445 if (tg3_flag(tp, ICH_WORKAROUND)) {
Michael Chan68929142005-08-09 20:17:14 -070014446 tp->read32 = tg3_read_indirect_reg32;
14447 tp->write32 = tg3_write_indirect_reg32;
14448 tp->read32_mbox = tg3_read_indirect_mbox;
14449 tp->write32_mbox = tg3_write_indirect_mbox;
14450 tp->write32_tx_mbox = tg3_write_indirect_mbox;
14451 tp->write32_rx_mbox = tg3_write_indirect_mbox;
14452
14453 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070014454 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070014455
14456 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
14457 pci_cmd &= ~PCI_COMMAND_MEMORY;
14458 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
14459 }
Michael Chanb5d37722006-09-27 16:06:21 -070014460 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
14461 tp->read32_mbox = tg3_read32_mbox_5906;
14462 tp->write32_mbox = tg3_write32_mbox_5906;
14463 tp->write32_tx_mbox = tg3_write32_mbox_5906;
14464 tp->write32_rx_mbox = tg3_write32_mbox_5906;
14465 }
Michael Chan68929142005-08-09 20:17:14 -070014466
Michael Chanbbadf502006-04-06 21:46:34 -070014467 if (tp->write32 == tg3_write_indirect_reg32 ||
Joe Perches63c3a662011-04-26 08:12:10 +000014468 (tg3_flag(tp, PCIX_MODE) &&
Michael Chanbbadf502006-04-06 21:46:34 -070014469 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
David S. Millerf49639e2006-06-09 11:58:36 -070014470 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)))
Joe Perches63c3a662011-04-26 08:12:10 +000014471 tg3_flag_set(tp, SRAM_USE_CONFIG);
Michael Chanbbadf502006-04-06 21:46:34 -070014472
Matt Carlson16821282011-07-13 09:27:28 +000014473 /* The memory arbiter has to be enabled in order for SRAM accesses
14474 * to succeed. Normally on powerup the tg3 chip firmware will make
14475 * sure it is enabled, but other entities such as system netboot
14476 * code might disable it.
14477 */
14478 val = tr32(MEMARB_MODE);
14479 tw32(MEMARB_MODE, val | MEMARB_MODE_ENABLE);
14480
Matt Carlson9dc5e342011-11-04 09:15:02 +000014481 tp->pci_fn = PCI_FUNC(tp->pdev->devfn) & 3;
14482 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
14483 tg3_flag(tp, 5780_CLASS)) {
14484 if (tg3_flag(tp, PCIX_MODE)) {
14485 pci_read_config_dword(tp->pdev,
14486 tp->pcix_cap + PCI_X_STATUS,
14487 &val);
14488 tp->pci_fn = val & 0x7;
14489 }
14490 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717) {
14491 tg3_read_mem(tp, NIC_SRAM_CPMU_STATUS, &val);
14492 if ((val & NIC_SRAM_CPMUSTAT_SIG_MSK) ==
14493 NIC_SRAM_CPMUSTAT_SIG) {
14494 tp->pci_fn = val & TG3_CPMU_STATUS_FMSK_5717;
14495 tp->pci_fn = tp->pci_fn ? 1 : 0;
14496 }
14497 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
14498 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
14499 tg3_read_mem(tp, NIC_SRAM_CPMU_STATUS, &val);
14500 if ((val & NIC_SRAM_CPMUSTAT_SIG_MSK) ==
14501 NIC_SRAM_CPMUSTAT_SIG) {
14502 tp->pci_fn = (val & TG3_CPMU_STATUS_FMSK_5719) >>
14503 TG3_CPMU_STATUS_FSHFT_5719;
14504 }
Matt Carlson69f11c92011-07-13 09:27:30 +000014505 }
14506
Michael Chan7d0c41e2005-04-21 17:06:20 -070014507 /* Get eeprom hw config before calling tg3_set_power_state().
Joe Perches63c3a662011-04-26 08:12:10 +000014508 * In particular, the TG3_FLAG_IS_NIC flag must be
Michael Chan7d0c41e2005-04-21 17:06:20 -070014509 * determined before calling tg3_set_power_state() so that
14510 * we know whether or not to switch out of Vaux power.
14511 * When the flag is set, it means that GPIO1 is used for eeprom
14512 * write protect and also implies that it is a LOM where GPIOs
14513 * are not used to switch power.
Jeff Garzik6aa20a22006-09-13 13:24:59 -040014514 */
Michael Chan7d0c41e2005-04-21 17:06:20 -070014515 tg3_get_eeprom_hw_cfg(tp);
14516
Matt Carlsoncf9ecf42011-11-28 09:41:03 +000014517 if (tp->fw_needed && tg3_flag(tp, ENABLE_ASF)) {
14518 tg3_flag_clear(tp, TSO_CAPABLE);
14519 tg3_flag_clear(tp, TSO_BUG);
14520 tp->fw_needed = NULL;
14521 }
14522
Joe Perches63c3a662011-04-26 08:12:10 +000014523 if (tg3_flag(tp, ENABLE_APE)) {
Matt Carlson0d3031d2007-10-10 18:02:43 -070014524 /* Allow reads and writes to the
14525 * APE register and memory space.
14526 */
14527 pci_state_reg |= PCISTATE_ALLOW_APE_CTLSPC_WR |
Matt Carlsonf92d9dc12010-06-05 17:24:30 +000014528 PCISTATE_ALLOW_APE_SHMEM_WR |
14529 PCISTATE_ALLOW_APE_PSPACE_WR;
Matt Carlson0d3031d2007-10-10 18:02:43 -070014530 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE,
14531 pci_state_reg);
Matt Carlsonc9cab242011-07-13 09:27:27 +000014532
14533 tg3_ape_lock_init(tp);
Matt Carlson0d3031d2007-10-10 18:02:43 -070014534 }
14535
Matt Carlson16821282011-07-13 09:27:28 +000014536 /* Set up tp->grc_local_ctrl before calling
14537 * tg3_pwrsrc_switch_to_vmain(). GPIO1 driven high
14538 * will bring 5700's external PHY out of reset.
Michael Chan314fba32005-04-21 17:07:04 -070014539 * It is also used as eeprom write protect on LOMs.
14540 */
14541 tp->grc_local_ctrl = GRC_LCLCTRL_INT_ON_ATTN | GRC_LCLCTRL_AUTO_SEEPROM;
Matt Carlson6ff6f812011-05-19 12:12:54 +000014542 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
Joe Perches63c3a662011-04-26 08:12:10 +000014543 tg3_flag(tp, EEPROM_WRITE_PROT))
Michael Chan314fba32005-04-21 17:07:04 -070014544 tp->grc_local_ctrl |= (GRC_LCLCTRL_GPIO_OE1 |
14545 GRC_LCLCTRL_GPIO_OUTPUT1);
Michael Chan3e7d83b2005-04-21 17:10:36 -070014546 /* Unused GPIO3 must be driven as output on 5752 because there
14547 * are no pull-up resistors on unused GPIO pins.
14548 */
14549 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752)
14550 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE3;
Michael Chan314fba32005-04-21 17:07:04 -070014551
Matt Carlson321d32a2008-11-21 17:22:19 -080014552 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
Matt Carlsoncb4ed1f2010-01-20 16:58:09 +000014553 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
Matt Carlson55086ad2011-12-14 11:09:59 +000014554 tg3_flag(tp, 57765_CLASS))
Michael Chanaf36e6b2006-03-23 01:28:06 -080014555 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_UART_SEL;
14556
Matt Carlson8d519ab2009-04-20 06:58:01 +000014557 if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761 ||
14558 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761S) {
Matt Carlson5f0c4a32008-06-09 15:41:12 -070014559 /* Turn off the debug UART. */
14560 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_UART_SEL;
Joe Perches63c3a662011-04-26 08:12:10 +000014561 if (tg3_flag(tp, IS_NIC))
Matt Carlson5f0c4a32008-06-09 15:41:12 -070014562 /* Keep VMain power. */
14563 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE0 |
14564 GRC_LCLCTRL_GPIO_OUTPUT0;
14565 }
14566
Matt Carlson16821282011-07-13 09:27:28 +000014567 /* Switch out of Vaux if it is a NIC */
14568 tg3_pwrsrc_switch_to_vmain(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014569
Linus Torvalds1da177e2005-04-16 15:20:36 -070014570 /* Derive initial jumbo mode from MTU assigned in
14571 * ether_setup() via the alloc_etherdev() call
14572 */
Joe Perches63c3a662011-04-26 08:12:10 +000014573 if (tp->dev->mtu > ETH_DATA_LEN && !tg3_flag(tp, 5780_CLASS))
14574 tg3_flag_set(tp, JUMBO_RING_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014575
14576 /* Determine WakeOnLan speed to use. */
14577 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
14578 tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
14579 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0 ||
14580 tp->pci_chip_rev_id == CHIPREV_ID_5701_B2) {
Joe Perches63c3a662011-04-26 08:12:10 +000014581 tg3_flag_clear(tp, WOL_SPEED_100MB);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014582 } else {
Joe Perches63c3a662011-04-26 08:12:10 +000014583 tg3_flag_set(tp, WOL_SPEED_100MB);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014584 }
14585
Matt Carlson7f97a4b2009-08-25 10:10:03 +000014586 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014587 tp->phy_flags |= TG3_PHYFLG_IS_FET;
Matt Carlson7f97a4b2009-08-25 10:10:03 +000014588
Linus Torvalds1da177e2005-04-16 15:20:36 -070014589 /* A few boards don't want Ethernet@WireSpeed phy feature */
Matt Carlson6ff6f812011-05-19 12:12:54 +000014590 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
14591 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070014592 (tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) &&
Michael Chan747e8f82005-07-25 12:33:22 -070014593 (tp->pci_chip_rev_id != CHIPREV_ID_5705_A1)) ||
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014594 (tp->phy_flags & TG3_PHYFLG_IS_FET) ||
14595 (tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
14596 tp->phy_flags |= TG3_PHYFLG_NO_ETH_WIRE_SPEED;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014597
14598 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5703_AX ||
14599 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5704_AX)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014600 tp->phy_flags |= TG3_PHYFLG_ADC_BUG;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014601 if (tp->pci_chip_rev_id == CHIPREV_ID_5704_A0)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014602 tp->phy_flags |= TG3_PHYFLG_5704_A0_BUG;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014603
Joe Perches63c3a662011-04-26 08:12:10 +000014604 if (tg3_flag(tp, 5705_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014605 !(tp->phy_flags & TG3_PHYFLG_IS_FET) &&
Matt Carlson321d32a2008-11-21 17:22:19 -080014606 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000014607 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_57780 &&
Joe Perches63c3a662011-04-26 08:12:10 +000014608 !tg3_flag(tp, 57765_PLUS)) {
Michael Chanc424cb22006-04-29 18:56:34 -070014609 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
Matt Carlsond30cdd22007-10-07 23:28:35 -070014610 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787 ||
Matt Carlson9936bcf2007-10-10 18:03:07 -070014611 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
14612 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761) {
Michael Chand4011ad2007-02-13 12:17:25 -080014613 if (tp->pdev->device != PCI_DEVICE_ID_TIGON3_5756 &&
14614 tp->pdev->device != PCI_DEVICE_ID_TIGON3_5722)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014615 tp->phy_flags |= TG3_PHYFLG_JITTER_BUG;
Michael Chanc1d2a192007-01-08 19:57:20 -080014616 if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5755M)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014617 tp->phy_flags |= TG3_PHYFLG_ADJUST_TRIM;
Matt Carlson321d32a2008-11-21 17:22:19 -080014618 } else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014619 tp->phy_flags |= TG3_PHYFLG_BER_BUG;
Michael Chanc424cb22006-04-29 18:56:34 -070014620 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014621
Matt Carlsonb2a5c192008-04-03 21:44:44 -070014622 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
14623 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX) {
14624 tp->phy_otp = tg3_read_otp_phycfg(tp);
14625 if (tp->phy_otp == 0)
14626 tp->phy_otp = TG3_OTP_DEFAULT;
14627 }
14628
Joe Perches63c3a662011-04-26 08:12:10 +000014629 if (tg3_flag(tp, CPMU_PRESENT))
Matt Carlson8ef21422008-05-02 16:47:53 -070014630 tp->mi_mode = MAC_MI_MODE_500KHZ_CONST;
14631 else
14632 tp->mi_mode = MAC_MI_MODE_BASE;
14633
Linus Torvalds1da177e2005-04-16 15:20:36 -070014634 tp->coalesce_mode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014635 if (GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5700_AX &&
14636 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5700_BX)
14637 tp->coalesce_mode |= HOSTCC_MODE_32BYTE;
14638
Matt Carlson4d958472011-04-20 07:57:35 +000014639 /* Set these bits to enable statistics workaround. */
14640 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
14641 tp->pci_chip_rev_id == CHIPREV_ID_5719_A0 ||
14642 tp->pci_chip_rev_id == CHIPREV_ID_5720_A0) {
14643 tp->coalesce_mode |= HOSTCC_MODE_ATTN;
14644 tp->grc_mode |= GRC_MODE_IRQ_ON_FLOW_ATTN;
14645 }
14646
Matt Carlson321d32a2008-11-21 17:22:19 -080014647 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
14648 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
Joe Perches63c3a662011-04-26 08:12:10 +000014649 tg3_flag_set(tp, USE_PHYLIB);
Matt Carlson57e69832008-05-25 23:48:31 -070014650
Matt Carlson158d7ab2008-05-29 01:37:54 -070014651 err = tg3_mdio_init(tp);
14652 if (err)
14653 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014654
14655 /* Initialize data/descriptor byte/word swapping. */
14656 val = tr32(GRC_MODE);
Matt Carlsonf2096f92011-04-05 14:22:48 +000014657 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
14658 val &= (GRC_MODE_BYTE_SWAP_B2HRX_DATA |
14659 GRC_MODE_WORD_SWAP_B2HRX_DATA |
14660 GRC_MODE_B2HRX_ENABLE |
14661 GRC_MODE_HTX2B_ENABLE |
14662 GRC_MODE_HOST_STACKUP);
14663 else
14664 val &= GRC_MODE_HOST_STACKUP;
14665
Linus Torvalds1da177e2005-04-16 15:20:36 -070014666 tw32(GRC_MODE, val | tp->grc_mode);
14667
14668 tg3_switch_clocks(tp);
14669
14670 /* Clear this out for sanity. */
14671 tw32(TG3PCI_MEM_WIN_BASE_ADDR, 0);
14672
14673 pci_read_config_dword(tp->pdev, TG3PCI_PCISTATE,
14674 &pci_state_reg);
14675 if ((pci_state_reg & PCISTATE_CONV_PCI_MODE) == 0 &&
Joe Perches63c3a662011-04-26 08:12:10 +000014676 !tg3_flag(tp, PCIX_TARGET_HWBUG)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070014677 u32 chiprevid = GET_CHIP_REV_ID(tp->misc_host_ctrl);
14678
14679 if (chiprevid == CHIPREV_ID_5701_A0 ||
14680 chiprevid == CHIPREV_ID_5701_B0 ||
14681 chiprevid == CHIPREV_ID_5701_B2 ||
14682 chiprevid == CHIPREV_ID_5701_B5) {
14683 void __iomem *sram_base;
14684
14685 /* Write some dummy words into the SRAM status block
14686 * area, see if it reads back correctly. If the return
14687 * value is bad, force enable the PCIX workaround.
14688 */
14689 sram_base = tp->regs + NIC_SRAM_WIN_BASE + NIC_SRAM_STATS_BLK;
14690
14691 writel(0x00000000, sram_base);
14692 writel(0x00000000, sram_base + 4);
14693 writel(0xffffffff, sram_base + 4);
14694 if (readl(sram_base) != 0x00000000)
Joe Perches63c3a662011-04-26 08:12:10 +000014695 tg3_flag_set(tp, PCIX_TARGET_HWBUG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014696 }
14697 }
14698
14699 udelay(50);
14700 tg3_nvram_init(tp);
14701
14702 grc_misc_cfg = tr32(GRC_MISC_CFG);
14703 grc_misc_cfg &= GRC_MISC_CFG_BOARD_ID_MASK;
14704
Linus Torvalds1da177e2005-04-16 15:20:36 -070014705 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
14706 (grc_misc_cfg == GRC_MISC_CFG_BOARD_ID_5788 ||
14707 grc_misc_cfg == GRC_MISC_CFG_BOARD_ID_5788M))
Joe Perches63c3a662011-04-26 08:12:10 +000014708 tg3_flag_set(tp, IS_5788);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014709
Joe Perches63c3a662011-04-26 08:12:10 +000014710 if (!tg3_flag(tp, IS_5788) &&
Matt Carlson6ff6f812011-05-19 12:12:54 +000014711 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700)
Joe Perches63c3a662011-04-26 08:12:10 +000014712 tg3_flag_set(tp, TAGGED_STATUS);
14713 if (tg3_flag(tp, TAGGED_STATUS)) {
David S. Millerfac9b832005-05-18 22:46:34 -070014714 tp->coalesce_mode |= (HOSTCC_MODE_CLRTICK_RXBD |
14715 HOSTCC_MODE_CLRTICK_TXBD);
14716
14717 tp->misc_host_ctrl |= MISC_HOST_CTRL_TAGGED_STATUS;
14718 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
14719 tp->misc_host_ctrl);
14720 }
14721
Matt Carlson3bda1252008-08-15 14:08:22 -070014722 /* Preserve the APE MAC_MODE bits */
Joe Perches63c3a662011-04-26 08:12:10 +000014723 if (tg3_flag(tp, ENABLE_APE))
Matt Carlsond2394e6b2010-11-24 08:31:47 +000014724 tp->mac_mode = MAC_MODE_APE_TX_EN | MAC_MODE_APE_RX_EN;
Matt Carlson3bda1252008-08-15 14:08:22 -070014725 else
Matt Carlson6e01b202011-08-19 13:58:20 +000014726 tp->mac_mode = 0;
Matt Carlson3bda1252008-08-15 14:08:22 -070014727
Linus Torvalds1da177e2005-04-16 15:20:36 -070014728 /* these are limited to 10/100 only */
14729 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 &&
14730 (grc_misc_cfg == 0x8000 || grc_misc_cfg == 0x4000)) ||
14731 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
14732 tp->pdev->vendor == PCI_VENDOR_ID_BROADCOM &&
14733 (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5901 ||
14734 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5901_2 ||
14735 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5705F)) ||
14736 (tp->pdev->vendor == PCI_VENDOR_ID_BROADCOM &&
14737 (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5751F ||
Michael Chan676917d2006-12-07 00:20:22 -080014738 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5753F ||
14739 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5787F)) ||
Matt Carlson321d32a2008-11-21 17:22:19 -080014740 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57790 ||
Matt Carlsond1101142010-02-17 15:16:55 +000014741 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57791 ||
14742 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57795 ||
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014743 (tp->phy_flags & TG3_PHYFLG_IS_FET))
14744 tp->phy_flags |= TG3_PHYFLG_10_100_ONLY;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014745
14746 err = tg3_phy_probe(tp);
14747 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000014748 dev_err(&tp->pdev->dev, "phy probe failed, err %d\n", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014749 /* ... but do not return immediately ... */
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070014750 tg3_mdio_fini(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014751 }
14752
Matt Carlson184b8902010-04-05 10:19:25 +000014753 tg3_read_vpd(tp);
Michael Chanc4e65752006-03-20 22:29:32 -080014754 tg3_read_fw_ver(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014755
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014756 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
14757 tp->phy_flags &= ~TG3_PHYFLG_USE_MI_INTERRUPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014758 } else {
14759 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014760 tp->phy_flags |= TG3_PHYFLG_USE_MI_INTERRUPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014761 else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014762 tp->phy_flags &= ~TG3_PHYFLG_USE_MI_INTERRUPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014763 }
14764
14765 /* 5700 {AX,BX} chips have a broken status block link
14766 * change bit implementation, so we must use the
14767 * status register in those cases.
14768 */
14769 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700)
Joe Perches63c3a662011-04-26 08:12:10 +000014770 tg3_flag_set(tp, USE_LINKCHG_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014771 else
Joe Perches63c3a662011-04-26 08:12:10 +000014772 tg3_flag_clear(tp, USE_LINKCHG_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014773
14774 /* The led_ctrl is set during tg3_phy_probe, here we might
14775 * have to force the link status polling mechanism based
14776 * upon subsystem IDs.
14777 */
14778 if (tp->pdev->subsystem_vendor == PCI_VENDOR_ID_DELL &&
Michael Chan007a880d2007-05-31 14:49:51 -070014779 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701 &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014780 !(tp->phy_flags & TG3_PHYFLG_PHY_SERDES)) {
14781 tp->phy_flags |= TG3_PHYFLG_USE_MI_INTERRUPT;
Joe Perches63c3a662011-04-26 08:12:10 +000014782 tg3_flag_set(tp, USE_LINKCHG_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014783 }
14784
14785 /* For all SERDES we poll the MAC status register. */
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014786 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Joe Perches63c3a662011-04-26 08:12:10 +000014787 tg3_flag_set(tp, POLL_SERDES);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014788 else
Joe Perches63c3a662011-04-26 08:12:10 +000014789 tg3_flag_clear(tp, POLL_SERDES);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014790
Eric Dumazet9205fd92011-11-18 06:47:01 +000014791 tp->rx_offset = NET_SKB_PAD + NET_IP_ALIGN;
Matt Carlsond2757fc2010-04-12 06:58:27 +000014792 tp->rx_copy_thresh = TG3_RX_COPY_THRESHOLD;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014793 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701 &&
Joe Perches63c3a662011-04-26 08:12:10 +000014794 tg3_flag(tp, PCIX_MODE)) {
Eric Dumazet9205fd92011-11-18 06:47:01 +000014795 tp->rx_offset = NET_SKB_PAD;
Matt Carlsond2757fc2010-04-12 06:58:27 +000014796#ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
Matt Carlson9dc7a112010-04-12 06:58:28 +000014797 tp->rx_copy_thresh = ~(u16)0;
Matt Carlsond2757fc2010-04-12 06:58:27 +000014798#endif
14799 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014800
Matt Carlson2c49a442010-09-30 10:34:35 +000014801 tp->rx_std_ring_mask = TG3_RX_STD_RING_SIZE(tp) - 1;
14802 tp->rx_jmb_ring_mask = TG3_RX_JMB_RING_SIZE(tp) - 1;
Matt Carlson7cb32cf2010-09-30 10:34:36 +000014803 tp->rx_ret_ring_mask = tg3_rx_ret_ring_size(tp) - 1;
14804
Matt Carlson2c49a442010-09-30 10:34:35 +000014805 tp->rx_std_max_post = tp->rx_std_ring_mask + 1;
Michael Chanf92905d2006-06-29 20:14:29 -070014806
14807 /* Increment the rx prod index on the rx std ring by at most
14808 * 8 for these chips to workaround hw errata.
14809 */
14810 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 ||
14811 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752 ||
14812 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755)
14813 tp->rx_std_max_post = 8;
14814
Joe Perches63c3a662011-04-26 08:12:10 +000014815 if (tg3_flag(tp, ASPM_WORKAROUND))
Matt Carlson8ed5d972007-05-07 00:25:49 -070014816 tp->pwrmgmt_thresh = tr32(PCIE_PWR_MGMT_THRESH) &
14817 PCIE_PWR_MGMT_L1_THRESH_MSK;
14818
Linus Torvalds1da177e2005-04-16 15:20:36 -070014819 return err;
14820}
14821
David S. Miller49b6e95f2007-03-29 01:38:42 -070014822#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070014823static int __devinit tg3_get_macaddr_sparc(struct tg3 *tp)
14824{
14825 struct net_device *dev = tp->dev;
14826 struct pci_dev *pdev = tp->pdev;
David S. Miller49b6e95f2007-03-29 01:38:42 -070014827 struct device_node *dp = pci_device_to_OF_node(pdev);
David S. Miller374d4ca2007-03-29 01:57:57 -070014828 const unsigned char *addr;
David S. Miller49b6e95f2007-03-29 01:38:42 -070014829 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014830
David S. Miller49b6e95f2007-03-29 01:38:42 -070014831 addr = of_get_property(dp, "local-mac-address", &len);
14832 if (addr && len == 6) {
14833 memcpy(dev->dev_addr, addr, 6);
14834 memcpy(dev->perm_addr, dev->dev_addr, 6);
14835 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014836 }
14837 return -ENODEV;
14838}
14839
14840static int __devinit tg3_get_default_macaddr_sparc(struct tg3 *tp)
14841{
14842 struct net_device *dev = tp->dev;
14843
14844 memcpy(dev->dev_addr, idprom->id_ethaddr, 6);
John W. Linville2ff43692005-09-12 14:44:20 -070014845 memcpy(dev->perm_addr, idprom->id_ethaddr, 6);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014846 return 0;
14847}
14848#endif
14849
14850static int __devinit tg3_get_device_address(struct tg3 *tp)
14851{
14852 struct net_device *dev = tp->dev;
14853 u32 hi, lo, mac_offset;
Michael Chan008652b2006-03-27 23:14:53 -080014854 int addr_ok = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014855
David S. Miller49b6e95f2007-03-29 01:38:42 -070014856#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070014857 if (!tg3_get_macaddr_sparc(tp))
14858 return 0;
14859#endif
14860
14861 mac_offset = 0x7c;
Matt Carlson6ff6f812011-05-19 12:12:54 +000014862 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
Joe Perches63c3a662011-04-26 08:12:10 +000014863 tg3_flag(tp, 5780_CLASS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070014864 if (tr32(TG3PCI_DUAL_MAC_CTRL) & DUAL_MAC_CTRL_ID)
14865 mac_offset = 0xcc;
14866 if (tg3_nvram_lock(tp))
14867 tw32_f(NVRAM_CMD, NVRAM_CMD_RESET);
14868 else
14869 tg3_nvram_unlock(tp);
Joe Perches63c3a662011-04-26 08:12:10 +000014870 } else if (tg3_flag(tp, 5717_PLUS)) {
Matt Carlson69f11c92011-07-13 09:27:30 +000014871 if (tp->pci_fn & 1)
Matt Carlsona1b950d2009-09-01 13:20:17 +000014872 mac_offset = 0xcc;
Matt Carlson69f11c92011-07-13 09:27:30 +000014873 if (tp->pci_fn > 1)
Matt Carlsona50d0792010-06-05 17:24:37 +000014874 mac_offset += 0x18c;
Matt Carlsona1b950d2009-09-01 13:20:17 +000014875 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
Michael Chanb5d37722006-09-27 16:06:21 -070014876 mac_offset = 0x10;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014877
14878 /* First try to get it from MAC address mailbox. */
14879 tg3_read_mem(tp, NIC_SRAM_MAC_ADDR_HIGH_MBOX, &hi);
14880 if ((hi >> 16) == 0x484b) {
14881 dev->dev_addr[0] = (hi >> 8) & 0xff;
14882 dev->dev_addr[1] = (hi >> 0) & 0xff;
14883
14884 tg3_read_mem(tp, NIC_SRAM_MAC_ADDR_LOW_MBOX, &lo);
14885 dev->dev_addr[2] = (lo >> 24) & 0xff;
14886 dev->dev_addr[3] = (lo >> 16) & 0xff;
14887 dev->dev_addr[4] = (lo >> 8) & 0xff;
14888 dev->dev_addr[5] = (lo >> 0) & 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014889
Michael Chan008652b2006-03-27 23:14:53 -080014890 /* Some old bootcode may report a 0 MAC address in SRAM */
14891 addr_ok = is_valid_ether_addr(&dev->dev_addr[0]);
14892 }
14893 if (!addr_ok) {
14894 /* Next, try NVRAM. */
Joe Perches63c3a662011-04-26 08:12:10 +000014895 if (!tg3_flag(tp, NO_NVRAM) &&
Matt Carlsondf259d82009-04-20 06:57:14 +000014896 !tg3_nvram_read_be32(tp, mac_offset + 0, &hi) &&
Matt Carlson6d348f22009-02-25 14:25:52 +000014897 !tg3_nvram_read_be32(tp, mac_offset + 4, &lo)) {
Matt Carlson62cedd12009-04-20 14:52:29 -070014898 memcpy(&dev->dev_addr[0], ((char *)&hi) + 2, 2);
14899 memcpy(&dev->dev_addr[2], (char *)&lo, sizeof(lo));
Michael Chan008652b2006-03-27 23:14:53 -080014900 }
14901 /* Finally just fetch it out of the MAC control regs. */
14902 else {
14903 hi = tr32(MAC_ADDR_0_HIGH);
14904 lo = tr32(MAC_ADDR_0_LOW);
14905
14906 dev->dev_addr[5] = lo & 0xff;
14907 dev->dev_addr[4] = (lo >> 8) & 0xff;
14908 dev->dev_addr[3] = (lo >> 16) & 0xff;
14909 dev->dev_addr[2] = (lo >> 24) & 0xff;
14910 dev->dev_addr[1] = hi & 0xff;
14911 dev->dev_addr[0] = (hi >> 8) & 0xff;
14912 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014913 }
14914
14915 if (!is_valid_ether_addr(&dev->dev_addr[0])) {
David S. Miller7582a332008-03-20 15:53:15 -070014916#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070014917 if (!tg3_get_default_macaddr_sparc(tp))
14918 return 0;
14919#endif
14920 return -EINVAL;
14921 }
John W. Linville2ff43692005-09-12 14:44:20 -070014922 memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014923 return 0;
14924}
14925
David S. Miller59e6b432005-05-18 22:50:10 -070014926#define BOUNDARY_SINGLE_CACHELINE 1
14927#define BOUNDARY_MULTI_CACHELINE 2
14928
14929static u32 __devinit tg3_calc_dma_bndry(struct tg3 *tp, u32 val)
14930{
14931 int cacheline_size;
14932 u8 byte;
14933 int goal;
14934
14935 pci_read_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE, &byte);
14936 if (byte == 0)
14937 cacheline_size = 1024;
14938 else
14939 cacheline_size = (int) byte * 4;
14940
14941 /* On 5703 and later chips, the boundary bits have no
14942 * effect.
14943 */
14944 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
14945 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701 &&
Joe Perches63c3a662011-04-26 08:12:10 +000014946 !tg3_flag(tp, PCI_EXPRESS))
David S. Miller59e6b432005-05-18 22:50:10 -070014947 goto out;
14948
14949#if defined(CONFIG_PPC64) || defined(CONFIG_IA64) || defined(CONFIG_PARISC)
14950 goal = BOUNDARY_MULTI_CACHELINE;
14951#else
14952#if defined(CONFIG_SPARC64) || defined(CONFIG_ALPHA)
14953 goal = BOUNDARY_SINGLE_CACHELINE;
14954#else
14955 goal = 0;
14956#endif
14957#endif
14958
Joe Perches63c3a662011-04-26 08:12:10 +000014959 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000014960 val = goal ? 0 : DMA_RWCTRL_DIS_CACHE_ALIGNMENT;
14961 goto out;
14962 }
14963
David S. Miller59e6b432005-05-18 22:50:10 -070014964 if (!goal)
14965 goto out;
14966
14967 /* PCI controllers on most RISC systems tend to disconnect
14968 * when a device tries to burst across a cache-line boundary.
14969 * Therefore, letting tg3 do so just wastes PCI bandwidth.
14970 *
14971 * Unfortunately, for PCI-E there are only limited
14972 * write-side controls for this, and thus for reads
14973 * we will still get the disconnects. We'll also waste
14974 * these PCI cycles for both read and write for chips
14975 * other than 5700 and 5701 which do not implement the
14976 * boundary bits.
14977 */
Joe Perches63c3a662011-04-26 08:12:10 +000014978 if (tg3_flag(tp, PCIX_MODE) && !tg3_flag(tp, PCI_EXPRESS)) {
David S. Miller59e6b432005-05-18 22:50:10 -070014979 switch (cacheline_size) {
14980 case 16:
14981 case 32:
14982 case 64:
14983 case 128:
14984 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14985 val |= (DMA_RWCTRL_READ_BNDRY_128_PCIX |
14986 DMA_RWCTRL_WRITE_BNDRY_128_PCIX);
14987 } else {
14988 val |= (DMA_RWCTRL_READ_BNDRY_384_PCIX |
14989 DMA_RWCTRL_WRITE_BNDRY_384_PCIX);
14990 }
14991 break;
14992
14993 case 256:
14994 val |= (DMA_RWCTRL_READ_BNDRY_256_PCIX |
14995 DMA_RWCTRL_WRITE_BNDRY_256_PCIX);
14996 break;
14997
14998 default:
14999 val |= (DMA_RWCTRL_READ_BNDRY_384_PCIX |
15000 DMA_RWCTRL_WRITE_BNDRY_384_PCIX);
15001 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070015002 }
Joe Perches63c3a662011-04-26 08:12:10 +000015003 } else if (tg3_flag(tp, PCI_EXPRESS)) {
David S. Miller59e6b432005-05-18 22:50:10 -070015004 switch (cacheline_size) {
15005 case 16:
15006 case 32:
15007 case 64:
15008 if (goal == BOUNDARY_SINGLE_CACHELINE) {
15009 val &= ~DMA_RWCTRL_WRITE_BNDRY_DISAB_PCIE;
15010 val |= DMA_RWCTRL_WRITE_BNDRY_64_PCIE;
15011 break;
15012 }
15013 /* fallthrough */
15014 case 128:
15015 default:
15016 val &= ~DMA_RWCTRL_WRITE_BNDRY_DISAB_PCIE;
15017 val |= DMA_RWCTRL_WRITE_BNDRY_128_PCIE;
15018 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070015019 }
David S. Miller59e6b432005-05-18 22:50:10 -070015020 } else {
15021 switch (cacheline_size) {
15022 case 16:
15023 if (goal == BOUNDARY_SINGLE_CACHELINE) {
15024 val |= (DMA_RWCTRL_READ_BNDRY_16 |
15025 DMA_RWCTRL_WRITE_BNDRY_16);
15026 break;
15027 }
15028 /* fallthrough */
15029 case 32:
15030 if (goal == BOUNDARY_SINGLE_CACHELINE) {
15031 val |= (DMA_RWCTRL_READ_BNDRY_32 |
15032 DMA_RWCTRL_WRITE_BNDRY_32);
15033 break;
15034 }
15035 /* fallthrough */
15036 case 64:
15037 if (goal == BOUNDARY_SINGLE_CACHELINE) {
15038 val |= (DMA_RWCTRL_READ_BNDRY_64 |
15039 DMA_RWCTRL_WRITE_BNDRY_64);
15040 break;
15041 }
15042 /* fallthrough */
15043 case 128:
15044 if (goal == BOUNDARY_SINGLE_CACHELINE) {
15045 val |= (DMA_RWCTRL_READ_BNDRY_128 |
15046 DMA_RWCTRL_WRITE_BNDRY_128);
15047 break;
15048 }
15049 /* fallthrough */
15050 case 256:
15051 val |= (DMA_RWCTRL_READ_BNDRY_256 |
15052 DMA_RWCTRL_WRITE_BNDRY_256);
15053 break;
15054 case 512:
15055 val |= (DMA_RWCTRL_READ_BNDRY_512 |
15056 DMA_RWCTRL_WRITE_BNDRY_512);
15057 break;
15058 case 1024:
15059 default:
15060 val |= (DMA_RWCTRL_READ_BNDRY_1024 |
15061 DMA_RWCTRL_WRITE_BNDRY_1024);
15062 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070015063 }
David S. Miller59e6b432005-05-18 22:50:10 -070015064 }
15065
15066out:
15067 return val;
15068}
15069
Linus Torvalds1da177e2005-04-16 15:20:36 -070015070static int __devinit tg3_do_test_dma(struct tg3 *tp, u32 *buf, dma_addr_t buf_dma, int size, int to_device)
15071{
15072 struct tg3_internal_buffer_desc test_desc;
15073 u32 sram_dma_descs;
15074 int i, ret;
15075
15076 sram_dma_descs = NIC_SRAM_DMA_DESC_POOL_BASE;
15077
15078 tw32(FTQ_RCVBD_COMP_FIFO_ENQDEQ, 0);
15079 tw32(FTQ_RCVDATA_COMP_FIFO_ENQDEQ, 0);
15080 tw32(RDMAC_STATUS, 0);
15081 tw32(WDMAC_STATUS, 0);
15082
15083 tw32(BUFMGR_MODE, 0);
15084 tw32(FTQ_RESET, 0);
15085
15086 test_desc.addr_hi = ((u64) buf_dma) >> 32;
15087 test_desc.addr_lo = buf_dma & 0xffffffff;
15088 test_desc.nic_mbuf = 0x00002100;
15089 test_desc.len = size;
15090
15091 /*
15092 * HP ZX1 was seeing test failures for 5701 cards running at 33Mhz
15093 * the *second* time the tg3 driver was getting loaded after an
15094 * initial scan.
15095 *
15096 * Broadcom tells me:
15097 * ...the DMA engine is connected to the GRC block and a DMA
15098 * reset may affect the GRC block in some unpredictable way...
15099 * The behavior of resets to individual blocks has not been tested.
15100 *
15101 * Broadcom noted the GRC reset will also reset all sub-components.
15102 */
15103 if (to_device) {
15104 test_desc.cqid_sqid = (13 << 8) | 2;
15105
15106 tw32_f(RDMAC_MODE, RDMAC_MODE_ENABLE);
15107 udelay(40);
15108 } else {
15109 test_desc.cqid_sqid = (16 << 8) | 7;
15110
15111 tw32_f(WDMAC_MODE, WDMAC_MODE_ENABLE);
15112 udelay(40);
15113 }
15114 test_desc.flags = 0x00000005;
15115
15116 for (i = 0; i < (sizeof(test_desc) / sizeof(u32)); i++) {
15117 u32 val;
15118
15119 val = *(((u32 *)&test_desc) + i);
15120 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR,
15121 sram_dma_descs + (i * sizeof(u32)));
15122 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_DATA, val);
15123 }
15124 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, 0);
15125
Matt Carlson859a588792010-04-05 10:19:28 +000015126 if (to_device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015127 tw32(FTQ_DMA_HIGH_READ_FIFO_ENQDEQ, sram_dma_descs);
Matt Carlson859a588792010-04-05 10:19:28 +000015128 else
Linus Torvalds1da177e2005-04-16 15:20:36 -070015129 tw32(FTQ_DMA_HIGH_WRITE_FIFO_ENQDEQ, sram_dma_descs);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015130
15131 ret = -ENODEV;
15132 for (i = 0; i < 40; i++) {
15133 u32 val;
15134
15135 if (to_device)
15136 val = tr32(FTQ_RCVBD_COMP_FIFO_ENQDEQ);
15137 else
15138 val = tr32(FTQ_RCVDATA_COMP_FIFO_ENQDEQ);
15139 if ((val & 0xffff) == sram_dma_descs) {
15140 ret = 0;
15141 break;
15142 }
15143
15144 udelay(100);
15145 }
15146
15147 return ret;
15148}
15149
David S. Millerded73402005-05-23 13:59:47 -070015150#define TEST_BUFFER_SIZE 0x2000
Linus Torvalds1da177e2005-04-16 15:20:36 -070015151
Matt Carlson41434702011-03-09 16:58:22 +000015152static DEFINE_PCI_DEVICE_TABLE(tg3_dma_wait_state_chipsets) = {
Joe Perches895950c2010-12-21 02:16:08 -080015153 { PCI_DEVICE(PCI_VENDOR_ID_APPLE, PCI_DEVICE_ID_APPLE_UNI_N_PCI15) },
15154 { },
15155};
15156
Linus Torvalds1da177e2005-04-16 15:20:36 -070015157static int __devinit tg3_test_dma(struct tg3 *tp)
15158{
15159 dma_addr_t buf_dma;
David S. Miller59e6b432005-05-18 22:50:10 -070015160 u32 *buf, saved_dma_rwctrl;
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000015161 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015162
Matt Carlson4bae65c2010-11-24 08:31:52 +000015163 buf = dma_alloc_coherent(&tp->pdev->dev, TEST_BUFFER_SIZE,
15164 &buf_dma, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015165 if (!buf) {
15166 ret = -ENOMEM;
15167 goto out_nofree;
15168 }
15169
15170 tp->dma_rwctrl = ((0x7 << DMA_RWCTRL_PCI_WRITE_CMD_SHIFT) |
15171 (0x6 << DMA_RWCTRL_PCI_READ_CMD_SHIFT));
15172
David S. Miller59e6b432005-05-18 22:50:10 -070015173 tp->dma_rwctrl = tg3_calc_dma_bndry(tp, tp->dma_rwctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015174
Joe Perches63c3a662011-04-26 08:12:10 +000015175 if (tg3_flag(tp, 57765_PLUS))
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000015176 goto out;
15177
Joe Perches63c3a662011-04-26 08:12:10 +000015178 if (tg3_flag(tp, PCI_EXPRESS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070015179 /* DMA read watermark not used on PCIE */
15180 tp->dma_rwctrl |= 0x00180000;
Joe Perches63c3a662011-04-26 08:12:10 +000015181 } else if (!tg3_flag(tp, PCIX_MODE)) {
Michael Chan85e94ce2005-04-21 17:05:28 -070015182 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 ||
15183 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015184 tp->dma_rwctrl |= 0x003f0000;
15185 else
15186 tp->dma_rwctrl |= 0x003f000f;
15187 } else {
15188 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
15189 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
15190 u32 ccval = (tr32(TG3PCI_CLOCK_CTRL) & 0x1f);
Michael Chan49afdeb2007-02-13 12:17:03 -080015191 u32 read_water = 0x7;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015192
Michael Chan4a29cc22006-03-19 13:21:12 -080015193 /* If the 5704 is behind the EPB bridge, we can
15194 * do the less restrictive ONE_DMA workaround for
15195 * better performance.
15196 */
Joe Perches63c3a662011-04-26 08:12:10 +000015197 if (tg3_flag(tp, 40BIT_DMA_BUG) &&
Michael Chan4a29cc22006-03-19 13:21:12 -080015198 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704)
15199 tp->dma_rwctrl |= 0x8000;
15200 else if (ccval == 0x6 || ccval == 0x7)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015201 tp->dma_rwctrl |= DMA_RWCTRL_ONE_DMA;
15202
Michael Chan49afdeb2007-02-13 12:17:03 -080015203 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703)
15204 read_water = 4;
David S. Miller59e6b432005-05-18 22:50:10 -070015205 /* Set bit 23 to enable PCIX hw bug fix */
Michael Chan49afdeb2007-02-13 12:17:03 -080015206 tp->dma_rwctrl |=
15207 (read_water << DMA_RWCTRL_READ_WATER_SHIFT) |
15208 (0x3 << DMA_RWCTRL_WRITE_WATER_SHIFT) |
15209 (1 << 23);
Michael Chan4cf78e42005-07-25 12:29:19 -070015210 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780) {
15211 /* 5780 always in PCIX mode */
15212 tp->dma_rwctrl |= 0x00144000;
Michael Chana4e2b342005-10-26 15:46:52 -070015213 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
15214 /* 5714 always in PCIX mode */
15215 tp->dma_rwctrl |= 0x00148000;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015216 } else {
15217 tp->dma_rwctrl |= 0x001b000f;
15218 }
15219 }
15220
15221 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
15222 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704)
15223 tp->dma_rwctrl &= 0xfffffff0;
15224
15225 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
15226 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
15227 /* Remove this if it causes problems for some boards. */
15228 tp->dma_rwctrl |= DMA_RWCTRL_USE_MEM_READ_MULT;
15229
15230 /* On 5700/5701 chips, we need to set this bit.
15231 * Otherwise the chip will issue cacheline transactions
15232 * to streamable DMA memory with not all the byte
15233 * enables turned on. This is an error on several
15234 * RISC PCI controllers, in particular sparc64.
15235 *
15236 * On 5703/5704 chips, this bit has been reassigned
15237 * a different meaning. In particular, it is used
15238 * on those chips to enable a PCI-X workaround.
15239 */
15240 tp->dma_rwctrl |= DMA_RWCTRL_ASSERT_ALL_BE;
15241 }
15242
15243 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
15244
15245#if 0
15246 /* Unneeded, already done by tg3_get_invariants. */
15247 tg3_switch_clocks(tp);
15248#endif
15249
Linus Torvalds1da177e2005-04-16 15:20:36 -070015250 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
15251 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701)
15252 goto out;
15253
David S. Miller59e6b432005-05-18 22:50:10 -070015254 /* It is best to perform DMA test with maximum write burst size
15255 * to expose the 5700/5701 write DMA bug.
15256 */
15257 saved_dma_rwctrl = tp->dma_rwctrl;
15258 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
15259 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
15260
Linus Torvalds1da177e2005-04-16 15:20:36 -070015261 while (1) {
15262 u32 *p = buf, i;
15263
15264 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++)
15265 p[i] = i;
15266
15267 /* Send the buffer to the chip. */
15268 ret = tg3_do_test_dma(tp, buf, buf_dma, TEST_BUFFER_SIZE, 1);
15269 if (ret) {
Matt Carlson2445e462010-04-05 10:19:21 +000015270 dev_err(&tp->pdev->dev,
15271 "%s: Buffer write failed. err = %d\n",
15272 __func__, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015273 break;
15274 }
15275
15276#if 0
15277 /* validate data reached card RAM correctly. */
15278 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++) {
15279 u32 val;
15280 tg3_read_mem(tp, 0x2100 + (i*4), &val);
15281 if (le32_to_cpu(val) != p[i]) {
Matt Carlson2445e462010-04-05 10:19:21 +000015282 dev_err(&tp->pdev->dev,
15283 "%s: Buffer corrupted on device! "
15284 "(%d != %d)\n", __func__, val, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015285 /* ret = -ENODEV here? */
15286 }
15287 p[i] = 0;
15288 }
15289#endif
15290 /* Now read it back. */
15291 ret = tg3_do_test_dma(tp, buf, buf_dma, TEST_BUFFER_SIZE, 0);
15292 if (ret) {
Matt Carlson5129c3a2010-04-05 10:19:23 +000015293 dev_err(&tp->pdev->dev, "%s: Buffer read failed. "
15294 "err = %d\n", __func__, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015295 break;
15296 }
15297
15298 /* Verify it. */
15299 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++) {
15300 if (p[i] == i)
15301 continue;
15302
David S. Miller59e6b432005-05-18 22:50:10 -070015303 if ((tp->dma_rwctrl & DMA_RWCTRL_WRITE_BNDRY_MASK) !=
15304 DMA_RWCTRL_WRITE_BNDRY_16) {
15305 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015306 tp->dma_rwctrl |= DMA_RWCTRL_WRITE_BNDRY_16;
15307 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
15308 break;
15309 } else {
Matt Carlson2445e462010-04-05 10:19:21 +000015310 dev_err(&tp->pdev->dev,
15311 "%s: Buffer corrupted on read back! "
15312 "(%d != %d)\n", __func__, p[i], i);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015313 ret = -ENODEV;
15314 goto out;
15315 }
15316 }
15317
15318 if (i == (TEST_BUFFER_SIZE / sizeof(u32))) {
15319 /* Success. */
15320 ret = 0;
15321 break;
15322 }
15323 }
David S. Miller59e6b432005-05-18 22:50:10 -070015324 if ((tp->dma_rwctrl & DMA_RWCTRL_WRITE_BNDRY_MASK) !=
15325 DMA_RWCTRL_WRITE_BNDRY_16) {
15326 /* DMA test passed without adjusting DMA boundary,
Michael Chan6d1cfba2005-06-08 14:13:14 -070015327 * now look for chipsets that are known to expose the
15328 * DMA bug without failing the test.
David S. Miller59e6b432005-05-18 22:50:10 -070015329 */
Matt Carlson41434702011-03-09 16:58:22 +000015330 if (pci_dev_present(tg3_dma_wait_state_chipsets)) {
Michael Chan6d1cfba2005-06-08 14:13:14 -070015331 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
15332 tp->dma_rwctrl |= DMA_RWCTRL_WRITE_BNDRY_16;
Matt Carlson859a588792010-04-05 10:19:28 +000015333 } else {
Michael Chan6d1cfba2005-06-08 14:13:14 -070015334 /* Safe to use the calculated DMA boundary. */
15335 tp->dma_rwctrl = saved_dma_rwctrl;
Matt Carlson859a588792010-04-05 10:19:28 +000015336 }
Michael Chan6d1cfba2005-06-08 14:13:14 -070015337
David S. Miller59e6b432005-05-18 22:50:10 -070015338 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
15339 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015340
15341out:
Matt Carlson4bae65c2010-11-24 08:31:52 +000015342 dma_free_coherent(&tp->pdev->dev, TEST_BUFFER_SIZE, buf, buf_dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015343out_nofree:
15344 return ret;
15345}
15346
Linus Torvalds1da177e2005-04-16 15:20:36 -070015347static void __devinit tg3_init_bufmgr_config(struct tg3 *tp)
15348{
Joe Perches63c3a662011-04-26 08:12:10 +000015349 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlson666bc832010-01-20 16:58:03 +000015350 tp->bufmgr_config.mbuf_read_dma_low_water =
15351 DEFAULT_MB_RDMA_LOW_WATER_5705;
15352 tp->bufmgr_config.mbuf_mac_rx_low_water =
15353 DEFAULT_MB_MACRX_LOW_WATER_57765;
15354 tp->bufmgr_config.mbuf_high_water =
15355 DEFAULT_MB_HIGH_WATER_57765;
15356
15357 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
15358 DEFAULT_MB_RDMA_LOW_WATER_5705;
15359 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
15360 DEFAULT_MB_MACRX_LOW_WATER_JUMBO_57765;
15361 tp->bufmgr_config.mbuf_high_water_jumbo =
15362 DEFAULT_MB_HIGH_WATER_JUMBO_57765;
Joe Perches63c3a662011-04-26 08:12:10 +000015363 } else if (tg3_flag(tp, 5705_PLUS)) {
Michael Chanfdfec1722005-07-25 12:31:48 -070015364 tp->bufmgr_config.mbuf_read_dma_low_water =
15365 DEFAULT_MB_RDMA_LOW_WATER_5705;
15366 tp->bufmgr_config.mbuf_mac_rx_low_water =
15367 DEFAULT_MB_MACRX_LOW_WATER_5705;
15368 tp->bufmgr_config.mbuf_high_water =
15369 DEFAULT_MB_HIGH_WATER_5705;
Michael Chanb5d37722006-09-27 16:06:21 -070015370 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
15371 tp->bufmgr_config.mbuf_mac_rx_low_water =
15372 DEFAULT_MB_MACRX_LOW_WATER_5906;
15373 tp->bufmgr_config.mbuf_high_water =
15374 DEFAULT_MB_HIGH_WATER_5906;
15375 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015376
Michael Chanfdfec1722005-07-25 12:31:48 -070015377 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
15378 DEFAULT_MB_RDMA_LOW_WATER_JUMBO_5780;
15379 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
15380 DEFAULT_MB_MACRX_LOW_WATER_JUMBO_5780;
15381 tp->bufmgr_config.mbuf_high_water_jumbo =
15382 DEFAULT_MB_HIGH_WATER_JUMBO_5780;
15383 } else {
15384 tp->bufmgr_config.mbuf_read_dma_low_water =
15385 DEFAULT_MB_RDMA_LOW_WATER;
15386 tp->bufmgr_config.mbuf_mac_rx_low_water =
15387 DEFAULT_MB_MACRX_LOW_WATER;
15388 tp->bufmgr_config.mbuf_high_water =
15389 DEFAULT_MB_HIGH_WATER;
15390
15391 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
15392 DEFAULT_MB_RDMA_LOW_WATER_JUMBO;
15393 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
15394 DEFAULT_MB_MACRX_LOW_WATER_JUMBO;
15395 tp->bufmgr_config.mbuf_high_water_jumbo =
15396 DEFAULT_MB_HIGH_WATER_JUMBO;
15397 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015398
15399 tp->bufmgr_config.dma_low_water = DEFAULT_DMA_LOW_WATER;
15400 tp->bufmgr_config.dma_high_water = DEFAULT_DMA_HIGH_WATER;
15401}
15402
15403static char * __devinit tg3_phy_string(struct tg3 *tp)
15404{
Matt Carlson79eb6902010-02-17 15:17:03 +000015405 switch (tp->phy_id & TG3_PHY_ID_MASK) {
15406 case TG3_PHY_ID_BCM5400: return "5400";
15407 case TG3_PHY_ID_BCM5401: return "5401";
15408 case TG3_PHY_ID_BCM5411: return "5411";
15409 case TG3_PHY_ID_BCM5701: return "5701";
15410 case TG3_PHY_ID_BCM5703: return "5703";
15411 case TG3_PHY_ID_BCM5704: return "5704";
15412 case TG3_PHY_ID_BCM5705: return "5705";
15413 case TG3_PHY_ID_BCM5750: return "5750";
15414 case TG3_PHY_ID_BCM5752: return "5752";
15415 case TG3_PHY_ID_BCM5714: return "5714";
15416 case TG3_PHY_ID_BCM5780: return "5780";
15417 case TG3_PHY_ID_BCM5755: return "5755";
15418 case TG3_PHY_ID_BCM5787: return "5787";
15419 case TG3_PHY_ID_BCM5784: return "5784";
15420 case TG3_PHY_ID_BCM5756: return "5722/5756";
15421 case TG3_PHY_ID_BCM5906: return "5906";
15422 case TG3_PHY_ID_BCM5761: return "5761";
15423 case TG3_PHY_ID_BCM5718C: return "5718C";
15424 case TG3_PHY_ID_BCM5718S: return "5718S";
15425 case TG3_PHY_ID_BCM57765: return "57765";
Matt Carlson302b5002010-06-05 17:24:38 +000015426 case TG3_PHY_ID_BCM5719C: return "5719C";
Matt Carlson6418f2c2011-04-05 14:22:49 +000015427 case TG3_PHY_ID_BCM5720C: return "5720C";
Matt Carlson79eb6902010-02-17 15:17:03 +000015428 case TG3_PHY_ID_BCM8002: return "8002/serdes";
Linus Torvalds1da177e2005-04-16 15:20:36 -070015429 case 0: return "serdes";
15430 default: return "unknown";
Stephen Hemminger855e1112008-04-16 16:37:28 -070015431 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015432}
15433
Michael Chanf9804dd2005-09-27 12:13:10 -070015434static char * __devinit tg3_bus_string(struct tg3 *tp, char *str)
15435{
Joe Perches63c3a662011-04-26 08:12:10 +000015436 if (tg3_flag(tp, PCI_EXPRESS)) {
Michael Chanf9804dd2005-09-27 12:13:10 -070015437 strcpy(str, "PCI Express");
15438 return str;
Joe Perches63c3a662011-04-26 08:12:10 +000015439 } else if (tg3_flag(tp, PCIX_MODE)) {
Michael Chanf9804dd2005-09-27 12:13:10 -070015440 u32 clock_ctrl = tr32(TG3PCI_CLOCK_CTRL) & 0x1f;
15441
15442 strcpy(str, "PCIX:");
15443
15444 if ((clock_ctrl == 7) ||
15445 ((tr32(GRC_MISC_CFG) & GRC_MISC_CFG_BOARD_ID_MASK) ==
15446 GRC_MISC_CFG_BOARD_ID_5704CIOBE))
15447 strcat(str, "133MHz");
15448 else if (clock_ctrl == 0)
15449 strcat(str, "33MHz");
15450 else if (clock_ctrl == 2)
15451 strcat(str, "50MHz");
15452 else if (clock_ctrl == 4)
15453 strcat(str, "66MHz");
15454 else if (clock_ctrl == 6)
15455 strcat(str, "100MHz");
Michael Chanf9804dd2005-09-27 12:13:10 -070015456 } else {
15457 strcpy(str, "PCI:");
Joe Perches63c3a662011-04-26 08:12:10 +000015458 if (tg3_flag(tp, PCI_HIGH_SPEED))
Michael Chanf9804dd2005-09-27 12:13:10 -070015459 strcat(str, "66MHz");
15460 else
15461 strcat(str, "33MHz");
15462 }
Joe Perches63c3a662011-04-26 08:12:10 +000015463 if (tg3_flag(tp, PCI_32BIT))
Michael Chanf9804dd2005-09-27 12:13:10 -070015464 strcat(str, ":32-bit");
15465 else
15466 strcat(str, ":64-bit");
15467 return str;
15468}
15469
David S. Miller15f98502005-05-18 22:49:26 -070015470static void __devinit tg3_init_coal(struct tg3 *tp)
15471{
15472 struct ethtool_coalesce *ec = &tp->coal;
15473
15474 memset(ec, 0, sizeof(*ec));
15475 ec->cmd = ETHTOOL_GCOALESCE;
15476 ec->rx_coalesce_usecs = LOW_RXCOL_TICKS;
15477 ec->tx_coalesce_usecs = LOW_TXCOL_TICKS;
15478 ec->rx_max_coalesced_frames = LOW_RXMAX_FRAMES;
15479 ec->tx_max_coalesced_frames = LOW_TXMAX_FRAMES;
15480 ec->rx_coalesce_usecs_irq = DEFAULT_RXCOAL_TICK_INT;
15481 ec->tx_coalesce_usecs_irq = DEFAULT_TXCOAL_TICK_INT;
15482 ec->rx_max_coalesced_frames_irq = DEFAULT_RXCOAL_MAXF_INT;
15483 ec->tx_max_coalesced_frames_irq = DEFAULT_TXCOAL_MAXF_INT;
15484 ec->stats_block_coalesce_usecs = DEFAULT_STAT_COAL_TICKS;
15485
15486 if (tp->coalesce_mode & (HOSTCC_MODE_CLRTICK_RXBD |
15487 HOSTCC_MODE_CLRTICK_TXBD)) {
15488 ec->rx_coalesce_usecs = LOW_RXCOL_TICKS_CLRTCKS;
15489 ec->rx_coalesce_usecs_irq = DEFAULT_RXCOAL_TICK_INT_CLRTCKS;
15490 ec->tx_coalesce_usecs = LOW_TXCOL_TICKS_CLRTCKS;
15491 ec->tx_coalesce_usecs_irq = DEFAULT_TXCOAL_TICK_INT_CLRTCKS;
15492 }
Michael Chand244c892005-07-05 14:42:33 -070015493
Joe Perches63c3a662011-04-26 08:12:10 +000015494 if (tg3_flag(tp, 5705_PLUS)) {
Michael Chand244c892005-07-05 14:42:33 -070015495 ec->rx_coalesce_usecs_irq = 0;
15496 ec->tx_coalesce_usecs_irq = 0;
15497 ec->stats_block_coalesce_usecs = 0;
15498 }
David S. Miller15f98502005-05-18 22:49:26 -070015499}
15500
Linus Torvalds1da177e2005-04-16 15:20:36 -070015501static int __devinit tg3_init_one(struct pci_dev *pdev,
15502 const struct pci_device_id *ent)
15503{
Linus Torvalds1da177e2005-04-16 15:20:36 -070015504 struct net_device *dev;
15505 struct tg3 *tp;
Matt Carlson646c9ed2009-09-01 12:58:41 +000015506 int i, err, pm_cap;
15507 u32 sndmbx, rcvmbx, intmbx;
Michael Chanf9804dd2005-09-27 12:13:10 -070015508 char str[40];
Michael Chan72f2afb2006-03-06 19:28:35 -080015509 u64 dma_mask, persist_dma_mask;
Michał Mirosławc8f44af2011-11-15 15:29:55 +000015510 netdev_features_t features = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015511
Joe Perches05dbe002010-02-17 19:44:19 +000015512 printk_once(KERN_INFO "%s\n", version);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015513
15514 err = pci_enable_device(pdev);
15515 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000015516 dev_err(&pdev->dev, "Cannot enable PCI device, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070015517 return err;
15518 }
15519
Linus Torvalds1da177e2005-04-16 15:20:36 -070015520 err = pci_request_regions(pdev, DRV_MODULE_NAME);
15521 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000015522 dev_err(&pdev->dev, "Cannot obtain PCI resources, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070015523 goto err_out_disable_pdev;
15524 }
15525
15526 pci_set_master(pdev);
15527
15528 /* Find power-management capability. */
15529 pm_cap = pci_find_capability(pdev, PCI_CAP_ID_PM);
15530 if (pm_cap == 0) {
Matt Carlson2445e462010-04-05 10:19:21 +000015531 dev_err(&pdev->dev,
15532 "Cannot find Power Management capability, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070015533 err = -EIO;
15534 goto err_out_free_res;
15535 }
15536
Matt Carlson16821282011-07-13 09:27:28 +000015537 err = pci_set_power_state(pdev, PCI_D0);
15538 if (err) {
15539 dev_err(&pdev->dev, "Transition to D0 failed, aborting\n");
15540 goto err_out_free_res;
15541 }
15542
Matt Carlsonfe5f5782009-09-01 13:09:39 +000015543 dev = alloc_etherdev_mq(sizeof(*tp), TG3_IRQ_MAX_VECS);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015544 if (!dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070015545 err = -ENOMEM;
Matt Carlson16821282011-07-13 09:27:28 +000015546 goto err_out_power_down;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015547 }
15548
Linus Torvalds1da177e2005-04-16 15:20:36 -070015549 SET_NETDEV_DEV(dev, &pdev->dev);
15550
Linus Torvalds1da177e2005-04-16 15:20:36 -070015551 tp = netdev_priv(dev);
15552 tp->pdev = pdev;
15553 tp->dev = dev;
15554 tp->pm_cap = pm_cap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015555 tp->rx_mode = TG3_DEF_RX_MODE;
15556 tp->tx_mode = TG3_DEF_TX_MODE;
Matt Carlson8ef21422008-05-02 16:47:53 -070015557
Linus Torvalds1da177e2005-04-16 15:20:36 -070015558 if (tg3_debug > 0)
15559 tp->msg_enable = tg3_debug;
15560 else
15561 tp->msg_enable = TG3_DEF_MSG_ENABLE;
15562
15563 /* The word/byte swap controls here control register access byte
15564 * swapping. DMA data byte swapping is controlled in the GRC_MODE
15565 * setting below.
15566 */
15567 tp->misc_host_ctrl =
15568 MISC_HOST_CTRL_MASK_PCI_INT |
15569 MISC_HOST_CTRL_WORD_SWAP |
15570 MISC_HOST_CTRL_INDIR_ACCESS |
15571 MISC_HOST_CTRL_PCISTATE_RW;
15572
15573 /* The NONFRM (non-frame) byte/word swap controls take effect
15574 * on descriptor entries, anything which isn't packet data.
15575 *
15576 * The StrongARM chips on the board (one for tx, one for rx)
15577 * are running in big-endian mode.
15578 */
15579 tp->grc_mode = (GRC_MODE_WSWAP_DATA | GRC_MODE_BSWAP_DATA |
15580 GRC_MODE_WSWAP_NONFRM_DATA);
15581#ifdef __BIG_ENDIAN
15582 tp->grc_mode |= GRC_MODE_BSWAP_NONFRM_DATA;
15583#endif
15584 spin_lock_init(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015585 spin_lock_init(&tp->indirect_lock);
David Howellsc4028952006-11-22 14:57:56 +000015586 INIT_WORK(&tp->reset_task, tg3_reset_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015587
Matt Carlsond5fe4882008-11-21 17:20:32 -080015588 tp->regs = pci_ioremap_bar(pdev, BAR_0);
Andy Gospodarekab0049b2007-09-06 20:42:14 +010015589 if (!tp->regs) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015590 dev_err(&pdev->dev, "Cannot map device registers, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070015591 err = -ENOMEM;
15592 goto err_out_free_dev;
15593 }
15594
Matt Carlsonc9cab242011-07-13 09:27:27 +000015595 if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761 ||
15596 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761E ||
15597 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761S ||
15598 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761SE ||
15599 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717 ||
15600 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718 ||
15601 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5719 ||
15602 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5720) {
15603 tg3_flag_set(tp, ENABLE_APE);
15604 tp->aperegs = pci_ioremap_bar(pdev, BAR_2);
15605 if (!tp->aperegs) {
15606 dev_err(&pdev->dev,
15607 "Cannot map APE registers, aborting\n");
15608 err = -ENOMEM;
15609 goto err_out_iounmap;
15610 }
15611 }
15612
Linus Torvalds1da177e2005-04-16 15:20:36 -070015613 tp->rx_pending = TG3_DEF_RX_RING_PENDING;
15614 tp->rx_jumbo_pending = TG3_DEF_RX_JUMBO_RING_PENDING;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015615
Linus Torvalds1da177e2005-04-16 15:20:36 -070015616 dev->ethtool_ops = &tg3_ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015617 dev->watchdog_timeo = TG3_TX_TIMEOUT;
Matt Carlson2ffcc982011-05-19 12:12:44 +000015618 dev->netdev_ops = &tg3_netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015619 dev->irq = pdev->irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015620
15621 err = tg3_get_invariants(tp);
15622 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015623 dev_err(&pdev->dev,
15624 "Problem fetching invariants of chip, aborting\n");
Matt Carlsonc9cab242011-07-13 09:27:27 +000015625 goto err_out_apeunmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015626 }
15627
Michael Chan4a29cc22006-03-19 13:21:12 -080015628 /* The EPB bridge inside 5714, 5715, and 5780 and any
15629 * device behind the EPB cannot support DMA addresses > 40-bit.
Michael Chan72f2afb2006-03-06 19:28:35 -080015630 * On 64-bit systems with IOMMU, use 40-bit dma_mask.
15631 * On 64-bit systems without IOMMU, use 64-bit dma_mask and
15632 * do DMA address check in tg3_start_xmit().
15633 */
Joe Perches63c3a662011-04-26 08:12:10 +000015634 if (tg3_flag(tp, IS_5788))
Yang Hongyang284901a2009-04-06 19:01:15 -070015635 persist_dma_mask = dma_mask = DMA_BIT_MASK(32);
Joe Perches63c3a662011-04-26 08:12:10 +000015636 else if (tg3_flag(tp, 40BIT_DMA_BUG)) {
Yang Hongyang50cf1562009-04-06 19:01:14 -070015637 persist_dma_mask = dma_mask = DMA_BIT_MASK(40);
Michael Chan72f2afb2006-03-06 19:28:35 -080015638#ifdef CONFIG_HIGHMEM
Yang Hongyang6a355282009-04-06 19:01:13 -070015639 dma_mask = DMA_BIT_MASK(64);
Michael Chan72f2afb2006-03-06 19:28:35 -080015640#endif
Michael Chan4a29cc22006-03-19 13:21:12 -080015641 } else
Yang Hongyang6a355282009-04-06 19:01:13 -070015642 persist_dma_mask = dma_mask = DMA_BIT_MASK(64);
Michael Chan72f2afb2006-03-06 19:28:35 -080015643
15644 /* Configure DMA attributes. */
Yang Hongyang284901a2009-04-06 19:01:15 -070015645 if (dma_mask > DMA_BIT_MASK(32)) {
Michael Chan72f2afb2006-03-06 19:28:35 -080015646 err = pci_set_dma_mask(pdev, dma_mask);
15647 if (!err) {
Matt Carlson0da06062011-05-19 12:12:53 +000015648 features |= NETIF_F_HIGHDMA;
Michael Chan72f2afb2006-03-06 19:28:35 -080015649 err = pci_set_consistent_dma_mask(pdev,
15650 persist_dma_mask);
15651 if (err < 0) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015652 dev_err(&pdev->dev, "Unable to obtain 64 bit "
15653 "DMA for consistent allocations\n");
Matt Carlsonc9cab242011-07-13 09:27:27 +000015654 goto err_out_apeunmap;
Michael Chan72f2afb2006-03-06 19:28:35 -080015655 }
15656 }
15657 }
Yang Hongyang284901a2009-04-06 19:01:15 -070015658 if (err || dma_mask == DMA_BIT_MASK(32)) {
15659 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
Michael Chan72f2afb2006-03-06 19:28:35 -080015660 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015661 dev_err(&pdev->dev,
15662 "No usable DMA configuration, aborting\n");
Matt Carlsonc9cab242011-07-13 09:27:27 +000015663 goto err_out_apeunmap;
Michael Chan72f2afb2006-03-06 19:28:35 -080015664 }
15665 }
15666
Michael Chanfdfec1722005-07-25 12:31:48 -070015667 tg3_init_bufmgr_config(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015668
Matt Carlson0da06062011-05-19 12:12:53 +000015669 features |= NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
15670
15671 /* 5700 B0 chips do not support checksumming correctly due
15672 * to hardware bugs.
15673 */
15674 if (tp->pci_chip_rev_id != CHIPREV_ID_5700_B0) {
15675 features |= NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_RXCSUM;
15676
15677 if (tg3_flag(tp, 5755_PLUS))
15678 features |= NETIF_F_IPV6_CSUM;
15679 }
15680
Michael Chan4e3a7aa2006-03-20 17:47:44 -080015681 /* TSO is on by default on chips that support hardware TSO.
15682 * Firmware TSO on older chips gives lower performance, so it
15683 * is off by default, but can be enabled using ethtool.
15684 */
Joe Perches63c3a662011-04-26 08:12:10 +000015685 if ((tg3_flag(tp, HW_TSO_1) ||
15686 tg3_flag(tp, HW_TSO_2) ||
15687 tg3_flag(tp, HW_TSO_3)) &&
Matt Carlson0da06062011-05-19 12:12:53 +000015688 (features & NETIF_F_IP_CSUM))
15689 features |= NETIF_F_TSO;
Joe Perches63c3a662011-04-26 08:12:10 +000015690 if (tg3_flag(tp, HW_TSO_2) || tg3_flag(tp, HW_TSO_3)) {
Matt Carlson0da06062011-05-19 12:12:53 +000015691 if (features & NETIF_F_IPV6_CSUM)
15692 features |= NETIF_F_TSO6;
Joe Perches63c3a662011-04-26 08:12:10 +000015693 if (tg3_flag(tp, HW_TSO_3) ||
Matt Carlsone849cdc2009-11-13 13:03:38 +000015694 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson57e69832008-05-25 23:48:31 -070015695 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
15696 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX) ||
Joe Perches63c3a662011-04-26 08:12:10 +000015697 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Michał Mirosławdc668912011-04-07 03:35:07 +000015698 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
Matt Carlson0da06062011-05-19 12:12:53 +000015699 features |= NETIF_F_TSO_ECN;
Michael Chanb0026622006-07-03 19:42:14 -070015700 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015701
Matt Carlsond542fe22011-05-19 16:02:43 +000015702 dev->features |= features;
15703 dev->vlan_features |= features;
15704
Mahesh Bandewar06c03c02011-05-08 06:51:48 +000015705 /*
15706 * Add loopback capability only for a subset of devices that support
15707 * MAC-LOOPBACK. Eventually this need to be enhanced to allow INT-PHY
15708 * loopback for the remaining devices.
15709 */
15710 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5780 &&
15711 !tg3_flag(tp, CPMU_PRESENT))
15712 /* Add the loopback capability */
Matt Carlson0da06062011-05-19 12:12:53 +000015713 features |= NETIF_F_LOOPBACK;
15714
Matt Carlson0da06062011-05-19 12:12:53 +000015715 dev->hw_features |= features;
Mahesh Bandewar06c03c02011-05-08 06:51:48 +000015716
Linus Torvalds1da177e2005-04-16 15:20:36 -070015717 if (tp->pci_chip_rev_id == CHIPREV_ID_5705_A1 &&
Joe Perches63c3a662011-04-26 08:12:10 +000015718 !tg3_flag(tp, TSO_CAPABLE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070015719 !(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH)) {
Joe Perches63c3a662011-04-26 08:12:10 +000015720 tg3_flag_set(tp, MAX_RXPEND_64);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015721 tp->rx_pending = 63;
15722 }
15723
Linus Torvalds1da177e2005-04-16 15:20:36 -070015724 err = tg3_get_device_address(tp);
15725 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015726 dev_err(&pdev->dev,
15727 "Could not obtain valid ethernet address, aborting\n");
Matt Carlsonc9cab242011-07-13 09:27:27 +000015728 goto err_out_apeunmap;
Matt Carlson0d3031d2007-10-10 18:02:43 -070015729 }
15730
Matt Carlsonc88864d2007-11-12 21:07:01 -080015731 /*
15732 * Reset chip in case UNDI or EFI driver did not shutdown
15733 * DMA self test will enable WDMAC and we'll see (spurious)
15734 * pending DMA on the PCI bus at that point.
15735 */
15736 if ((tr32(HOSTCC_MODE) & HOSTCC_MODE_ENABLE) ||
15737 (tr32(WDMAC_MODE) & WDMAC_MODE_ENABLE)) {
15738 tw32(MEMARB_MODE, MEMARB_MODE_ENABLE);
15739 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
15740 }
15741
15742 err = tg3_test_dma(tp);
15743 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015744 dev_err(&pdev->dev, "DMA engine test failed, aborting\n");
Matt Carlsonc88864d2007-11-12 21:07:01 -080015745 goto err_out_apeunmap;
15746 }
15747
Matt Carlson78f90dc2009-11-13 13:03:42 +000015748 intmbx = MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW;
15749 rcvmbx = MAILBOX_RCVRET_CON_IDX_0 + TG3_64BIT_REG_LOW;
15750 sndmbx = MAILBOX_SNDHOST_PROD_IDX_0 + TG3_64BIT_REG_LOW;
Matt Carlson6fd45cb2010-09-15 08:59:57 +000015751 for (i = 0; i < tp->irq_max; i++) {
Matt Carlson78f90dc2009-11-13 13:03:42 +000015752 struct tg3_napi *tnapi = &tp->napi[i];
15753
15754 tnapi->tp = tp;
15755 tnapi->tx_pending = TG3_DEF_TX_RING_PENDING;
15756
15757 tnapi->int_mbox = intmbx;
Matt Carlson93a700a2011-08-31 11:44:54 +000015758 if (i <= 4)
Matt Carlson78f90dc2009-11-13 13:03:42 +000015759 intmbx += 0x8;
15760 else
15761 intmbx += 0x4;
15762
15763 tnapi->consmbox = rcvmbx;
15764 tnapi->prodmbox = sndmbx;
15765
Matt Carlson66cfd1b2010-09-30 10:34:30 +000015766 if (i)
Matt Carlson78f90dc2009-11-13 13:03:42 +000015767 tnapi->coal_now = HOSTCC_MODE_COAL_VEC1_NOW << (i - 1);
Matt Carlson66cfd1b2010-09-30 10:34:30 +000015768 else
Matt Carlson78f90dc2009-11-13 13:03:42 +000015769 tnapi->coal_now = HOSTCC_MODE_NOW;
Matt Carlson78f90dc2009-11-13 13:03:42 +000015770
Joe Perches63c3a662011-04-26 08:12:10 +000015771 if (!tg3_flag(tp, SUPPORT_MSIX))
Matt Carlson78f90dc2009-11-13 13:03:42 +000015772 break;
15773
15774 /*
15775 * If we support MSIX, we'll be using RSS. If we're using
15776 * RSS, the first vector only handles link interrupts and the
15777 * remaining vectors handle rx and tx interrupts. Reuse the
15778 * mailbox values for the next iteration. The values we setup
15779 * above are still useful for the single vectored mode.
15780 */
15781 if (!i)
15782 continue;
15783
15784 rcvmbx += 0x8;
15785
15786 if (sndmbx & 0x4)
15787 sndmbx -= 0x4;
15788 else
15789 sndmbx += 0xc;
15790 }
15791
Matt Carlsonc88864d2007-11-12 21:07:01 -080015792 tg3_init_coal(tp);
15793
Michael Chanc49a1562006-12-17 17:07:29 -080015794 pci_set_drvdata(pdev, dev);
15795
Matt Carlsoncd0d7222011-07-13 09:27:33 +000015796 if (tg3_flag(tp, 5717_PLUS)) {
15797 /* Resume a low-power mode */
15798 tg3_frob_aux_power(tp, false);
15799 }
15800
Matt Carlson21f76382012-02-22 12:35:21 +000015801 tg3_timer_init(tp);
15802
Linus Torvalds1da177e2005-04-16 15:20:36 -070015803 err = register_netdev(dev);
15804 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015805 dev_err(&pdev->dev, "Cannot register net device, aborting\n");
Matt Carlson0d3031d2007-10-10 18:02:43 -070015806 goto err_out_apeunmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015807 }
15808
Joe Perches05dbe002010-02-17 19:44:19 +000015809 netdev_info(dev, "Tigon3 [partno(%s) rev %04x] (%s) MAC address %pM\n",
15810 tp->board_part_number,
15811 tp->pci_chip_rev_id,
15812 tg3_bus_string(tp, str),
15813 dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015814
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015815 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000015816 struct phy_device *phydev;
15817 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlson5129c3a2010-04-05 10:19:23 +000015818 netdev_info(dev,
15819 "attached PHY driver [%s] (mii_bus:phy_addr=%s)\n",
Joe Perches05dbe002010-02-17 19:44:19 +000015820 phydev->drv->name, dev_name(&phydev->dev));
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015821 } else {
15822 char *ethtype;
15823
15824 if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
15825 ethtype = "10/100Base-TX";
15826 else if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
15827 ethtype = "1000Base-SX";
15828 else
15829 ethtype = "10/100/1000Base-T";
15830
Matt Carlson5129c3a2010-04-05 10:19:23 +000015831 netdev_info(dev, "attached PHY is %s (%s Ethernet) "
Matt Carlson47007832011-04-20 07:57:43 +000015832 "(WireSpeed[%d], EEE[%d])\n",
15833 tg3_phy_string(tp), ethtype,
15834 (tp->phy_flags & TG3_PHYFLG_NO_ETH_WIRE_SPEED) == 0,
15835 (tp->phy_flags & TG3_PHYFLG_EEE_CAP) != 0);
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015836 }
Matt Carlsondf59c942008-11-03 16:52:56 -080015837
Joe Perches05dbe002010-02-17 19:44:19 +000015838 netdev_info(dev, "RXcsums[%d] LinkChgREG[%d] MIirq[%d] ASF[%d] TSOcap[%d]\n",
Michał Mirosławdc668912011-04-07 03:35:07 +000015839 (dev->features & NETIF_F_RXCSUM) != 0,
Joe Perches63c3a662011-04-26 08:12:10 +000015840 tg3_flag(tp, USE_LINKCHG_REG) != 0,
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015841 (tp->phy_flags & TG3_PHYFLG_USE_MI_INTERRUPT) != 0,
Joe Perches63c3a662011-04-26 08:12:10 +000015842 tg3_flag(tp, ENABLE_ASF) != 0,
15843 tg3_flag(tp, TSO_CAPABLE) != 0);
Joe Perches05dbe002010-02-17 19:44:19 +000015844 netdev_info(dev, "dma_rwctrl[%08x] dma_mask[%d-bit]\n",
15845 tp->dma_rwctrl,
15846 pdev->dma_mask == DMA_BIT_MASK(32) ? 32 :
15847 ((u64)pdev->dma_mask) == DMA_BIT_MASK(40) ? 40 : 64);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015848
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000015849 pci_save_state(pdev);
15850
Linus Torvalds1da177e2005-04-16 15:20:36 -070015851 return 0;
15852
Matt Carlson0d3031d2007-10-10 18:02:43 -070015853err_out_apeunmap:
15854 if (tp->aperegs) {
15855 iounmap(tp->aperegs);
15856 tp->aperegs = NULL;
15857 }
15858
Linus Torvalds1da177e2005-04-16 15:20:36 -070015859err_out_iounmap:
Michael Chan68929142005-08-09 20:17:14 -070015860 if (tp->regs) {
15861 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070015862 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070015863 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015864
15865err_out_free_dev:
15866 free_netdev(dev);
15867
Matt Carlson16821282011-07-13 09:27:28 +000015868err_out_power_down:
15869 pci_set_power_state(pdev, PCI_D3hot);
15870
Linus Torvalds1da177e2005-04-16 15:20:36 -070015871err_out_free_res:
15872 pci_release_regions(pdev);
15873
15874err_out_disable_pdev:
15875 pci_disable_device(pdev);
15876 pci_set_drvdata(pdev, NULL);
15877 return err;
15878}
15879
15880static void __devexit tg3_remove_one(struct pci_dev *pdev)
15881{
15882 struct net_device *dev = pci_get_drvdata(pdev);
15883
15884 if (dev) {
15885 struct tg3 *tp = netdev_priv(dev);
15886
Jesper Juhle3c55302012-04-09 22:50:15 +020015887 release_firmware(tp->fw);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -080015888
Matt Carlsondb219972011-11-04 09:15:03 +000015889 tg3_reset_task_cancel(tp);
Matt Carlson158d7ab2008-05-29 01:37:54 -070015890
David S. Miller1805b2f2011-10-24 18:18:09 -040015891 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015892 tg3_phy_fini(tp);
Matt Carlson158d7ab2008-05-29 01:37:54 -070015893 tg3_mdio_fini(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015894 }
Matt Carlson158d7ab2008-05-29 01:37:54 -070015895
Linus Torvalds1da177e2005-04-16 15:20:36 -070015896 unregister_netdev(dev);
Matt Carlson0d3031d2007-10-10 18:02:43 -070015897 if (tp->aperegs) {
15898 iounmap(tp->aperegs);
15899 tp->aperegs = NULL;
15900 }
Michael Chan68929142005-08-09 20:17:14 -070015901 if (tp->regs) {
15902 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070015903 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070015904 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015905 free_netdev(dev);
15906 pci_release_regions(pdev);
15907 pci_disable_device(pdev);
15908 pci_set_drvdata(pdev, NULL);
15909 }
15910}
15911
Eric Dumazetaa6027c2011-01-01 05:22:46 +000015912#ifdef CONFIG_PM_SLEEP
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015913static int tg3_suspend(struct device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015914{
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015915 struct pci_dev *pdev = to_pci_dev(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015916 struct net_device *dev = pci_get_drvdata(pdev);
15917 struct tg3 *tp = netdev_priv(dev);
15918 int err;
15919
15920 if (!netif_running(dev))
15921 return 0;
15922
Matt Carlsondb219972011-11-04 09:15:03 +000015923 tg3_reset_task_cancel(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015924 tg3_phy_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015925 tg3_netif_stop(tp);
15926
Matt Carlson21f76382012-02-22 12:35:21 +000015927 tg3_timer_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015928
David S. Millerf47c11e2005-06-24 20:18:35 -070015929 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015930 tg3_disable_ints(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -070015931 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015932
15933 netif_device_detach(dev);
15934
David S. Millerf47c11e2005-06-24 20:18:35 -070015935 tg3_full_lock(tp, 0);
Michael Chan944d9802005-05-29 14:57:48 -070015936 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Joe Perches63c3a662011-04-26 08:12:10 +000015937 tg3_flag_clear(tp, INIT_COMPLETE);
David S. Millerf47c11e2005-06-24 20:18:35 -070015938 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015939
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015940 err = tg3_power_down_prepare(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015941 if (err) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015942 int err2;
15943
David S. Millerf47c11e2005-06-24 20:18:35 -070015944 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015945
Joe Perches63c3a662011-04-26 08:12:10 +000015946 tg3_flag_set(tp, INIT_COMPLETE);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015947 err2 = tg3_restart_hw(tp, 1);
15948 if (err2)
Michael Chanb9ec6c12006-07-25 16:37:27 -070015949 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015950
Matt Carlson21f76382012-02-22 12:35:21 +000015951 tg3_timer_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015952
15953 netif_device_attach(dev);
15954 tg3_netif_start(tp);
15955
Michael Chanb9ec6c12006-07-25 16:37:27 -070015956out:
David S. Millerf47c11e2005-06-24 20:18:35 -070015957 tg3_full_unlock(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015958
15959 if (!err2)
15960 tg3_phy_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015961 }
15962
15963 return err;
15964}
15965
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015966static int tg3_resume(struct device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015967{
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015968 struct pci_dev *pdev = to_pci_dev(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015969 struct net_device *dev = pci_get_drvdata(pdev);
15970 struct tg3 *tp = netdev_priv(dev);
15971 int err;
15972
15973 if (!netif_running(dev))
15974 return 0;
15975
Linus Torvalds1da177e2005-04-16 15:20:36 -070015976 netif_device_attach(dev);
15977
David S. Millerf47c11e2005-06-24 20:18:35 -070015978 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015979
Joe Perches63c3a662011-04-26 08:12:10 +000015980 tg3_flag_set(tp, INIT_COMPLETE);
Michael Chanb9ec6c12006-07-25 16:37:27 -070015981 err = tg3_restart_hw(tp, 1);
15982 if (err)
15983 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015984
Matt Carlson21f76382012-02-22 12:35:21 +000015985 tg3_timer_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015986
Linus Torvalds1da177e2005-04-16 15:20:36 -070015987 tg3_netif_start(tp);
15988
Michael Chanb9ec6c12006-07-25 16:37:27 -070015989out:
David S. Millerf47c11e2005-06-24 20:18:35 -070015990 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015991
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015992 if (!err)
15993 tg3_phy_start(tp);
15994
Michael Chanb9ec6c12006-07-25 16:37:27 -070015995 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015996}
15997
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015998static SIMPLE_DEV_PM_OPS(tg3_pm_ops, tg3_suspend, tg3_resume);
Eric Dumazetaa6027c2011-01-01 05:22:46 +000015999#define TG3_PM_OPS (&tg3_pm_ops)
16000
16001#else
16002
16003#define TG3_PM_OPS NULL
16004
16005#endif /* CONFIG_PM_SLEEP */
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000016006
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000016007/**
16008 * tg3_io_error_detected - called when PCI error is detected
16009 * @pdev: Pointer to PCI device
16010 * @state: The current pci connection state
16011 *
16012 * This function is called after a PCI bus error affecting
16013 * this device has been detected.
16014 */
16015static pci_ers_result_t tg3_io_error_detected(struct pci_dev *pdev,
16016 pci_channel_state_t state)
16017{
16018 struct net_device *netdev = pci_get_drvdata(pdev);
16019 struct tg3 *tp = netdev_priv(netdev);
16020 pci_ers_result_t err = PCI_ERS_RESULT_NEED_RESET;
16021
16022 netdev_info(netdev, "PCI I/O error detected\n");
16023
16024 rtnl_lock();
16025
16026 if (!netif_running(netdev))
16027 goto done;
16028
16029 tg3_phy_stop(tp);
16030
16031 tg3_netif_stop(tp);
16032
Matt Carlson21f76382012-02-22 12:35:21 +000016033 tg3_timer_stop(tp);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000016034
16035 /* Want to make sure that the reset task doesn't run */
Matt Carlsondb219972011-11-04 09:15:03 +000016036 tg3_reset_task_cancel(tp);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000016037
16038 netif_device_detach(netdev);
16039
16040 /* Clean up software state, even if MMIO is blocked */
16041 tg3_full_lock(tp, 0);
16042 tg3_halt(tp, RESET_KIND_SHUTDOWN, 0);
16043 tg3_full_unlock(tp);
16044
16045done:
16046 if (state == pci_channel_io_perm_failure)
16047 err = PCI_ERS_RESULT_DISCONNECT;
16048 else
16049 pci_disable_device(pdev);
16050
16051 rtnl_unlock();
16052
16053 return err;
16054}
16055
16056/**
16057 * tg3_io_slot_reset - called after the pci bus has been reset.
16058 * @pdev: Pointer to PCI device
16059 *
16060 * Restart the card from scratch, as if from a cold-boot.
16061 * At this point, the card has exprienced a hard reset,
16062 * followed by fixups by BIOS, and has its config space
16063 * set up identically to what it was at cold boot.
16064 */
16065static pci_ers_result_t tg3_io_slot_reset(struct pci_dev *pdev)
16066{
16067 struct net_device *netdev = pci_get_drvdata(pdev);
16068 struct tg3 *tp = netdev_priv(netdev);
16069 pci_ers_result_t rc = PCI_ERS_RESULT_DISCONNECT;
16070 int err;
16071
16072 rtnl_lock();
16073
16074 if (pci_enable_device(pdev)) {
16075 netdev_err(netdev, "Cannot re-enable PCI device after reset.\n");
16076 goto done;
16077 }
16078
16079 pci_set_master(pdev);
16080 pci_restore_state(pdev);
16081 pci_save_state(pdev);
16082
16083 if (!netif_running(netdev)) {
16084 rc = PCI_ERS_RESULT_RECOVERED;
16085 goto done;
16086 }
16087
16088 err = tg3_power_up(tp);
Matt Carlsonbed98292011-07-13 09:27:29 +000016089 if (err)
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000016090 goto done;
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000016091
16092 rc = PCI_ERS_RESULT_RECOVERED;
16093
16094done:
16095 rtnl_unlock();
16096
16097 return rc;
16098}
16099
16100/**
16101 * tg3_io_resume - called when traffic can start flowing again.
16102 * @pdev: Pointer to PCI device
16103 *
16104 * This callback is called when the error recovery driver tells
16105 * us that its OK to resume normal operation.
16106 */
16107static void tg3_io_resume(struct pci_dev *pdev)
16108{
16109 struct net_device *netdev = pci_get_drvdata(pdev);
16110 struct tg3 *tp = netdev_priv(netdev);
16111 int err;
16112
16113 rtnl_lock();
16114
16115 if (!netif_running(netdev))
16116 goto done;
16117
16118 tg3_full_lock(tp, 0);
Joe Perches63c3a662011-04-26 08:12:10 +000016119 tg3_flag_set(tp, INIT_COMPLETE);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000016120 err = tg3_restart_hw(tp, 1);
16121 tg3_full_unlock(tp);
16122 if (err) {
16123 netdev_err(netdev, "Cannot restart hardware after reset.\n");
16124 goto done;
16125 }
16126
16127 netif_device_attach(netdev);
16128
Matt Carlson21f76382012-02-22 12:35:21 +000016129 tg3_timer_start(tp);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000016130
16131 tg3_netif_start(tp);
16132
16133 tg3_phy_start(tp);
16134
16135done:
16136 rtnl_unlock();
16137}
16138
16139static struct pci_error_handlers tg3_err_handler = {
16140 .error_detected = tg3_io_error_detected,
16141 .slot_reset = tg3_io_slot_reset,
16142 .resume = tg3_io_resume
16143};
16144
Linus Torvalds1da177e2005-04-16 15:20:36 -070016145static struct pci_driver tg3_driver = {
16146 .name = DRV_MODULE_NAME,
16147 .id_table = tg3_pci_tbl,
16148 .probe = tg3_init_one,
16149 .remove = __devexit_p(tg3_remove_one),
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000016150 .err_handler = &tg3_err_handler,
Eric Dumazetaa6027c2011-01-01 05:22:46 +000016151 .driver.pm = TG3_PM_OPS,
Linus Torvalds1da177e2005-04-16 15:20:36 -070016152};
16153
16154static int __init tg3_init(void)
16155{
Jeff Garzik29917622006-08-19 17:48:59 -040016156 return pci_register_driver(&tg3_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016157}
16158
16159static void __exit tg3_cleanup(void)
16160{
16161 pci_unregister_driver(&tg3_driver);
16162}
16163
16164module_init(tg3_init);
16165module_exit(tg3_cleanup);