blob: d9e9c8c652c849a6bfff346e66151353e36bc2c5 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * tg3.c: Broadcom Tigon3 ethernet driver.
3 *
4 * Copyright (C) 2001, 2002, 2003, 2004 David S. Miller (davem@redhat.com)
5 * Copyright (C) 2001, 2002, 2003 Jeff Garzik (jgarzik@pobox.com)
6 * Copyright (C) 2004 Sun Microsystems Inc.
Matt Carlsonb86fb2c2011-01-25 15:58:57 +00007 * Copyright (C) 2005-2011 Broadcom Corporation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 *
9 * Firmware is:
Michael Chan49cabf42005-06-06 15:15:17 -070010 * Derived from proprietary unpublished source code,
11 * Copyright (C) 2000-2003 Broadcom Corporation.
12 *
13 * Permission is hereby granted for the distribution of this firmware
14 * data in hexadecimal or equivalent format, provided this copyright
15 * notice is accompanying it.
Linus Torvalds1da177e2005-04-16 15:20:36 -070016 */
17
Linus Torvalds1da177e2005-04-16 15:20:36 -070018
19#include <linux/module.h>
20#include <linux/moduleparam.h>
Matt Carlson6867c842010-07-11 09:31:44 +000021#include <linux/stringify.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/kernel.h>
23#include <linux/types.h>
24#include <linux/compiler.h>
25#include <linux/slab.h>
26#include <linux/delay.h>
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020027#include <linux/in.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <linux/init.h>
Alexey Dobriyana6b7a402011-06-06 10:43:46 +000029#include <linux/interrupt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <linux/ioport.h>
31#include <linux/pci.h>
32#include <linux/netdevice.h>
33#include <linux/etherdevice.h>
34#include <linux/skbuff.h>
35#include <linux/ethtool.h>
Matt Carlson3110f5f52010-12-06 08:28:50 +000036#include <linux/mdio.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include <linux/mii.h>
Matt Carlson158d7ab2008-05-29 01:37:54 -070038#include <linux/phy.h>
Matt Carlsona9daf362008-05-25 23:49:44 -070039#include <linux/brcmphy.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include <linux/if_vlan.h>
41#include <linux/ip.h>
42#include <linux/tcp.h>
43#include <linux/workqueue.h>
Michael Chan61487482005-09-05 17:53:19 -070044#include <linux/prefetch.h>
Tobias Klauserf9a5f7d2005-10-29 15:09:26 +020045#include <linux/dma-mapping.h>
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -080046#include <linux/firmware.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
48#include <net/checksum.h>
Arnaldo Carvalho de Meloc9bdd4b2007-03-12 20:09:15 -030049#include <net/ip.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
51#include <asm/system.h>
Javier Martinez Canillas27fd9de2011-03-26 16:42:31 +000052#include <linux/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070053#include <asm/byteorder.h>
Javier Martinez Canillas27fd9de2011-03-26 16:42:31 +000054#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
David S. Miller49b6e95f2007-03-29 01:38:42 -070056#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070057#include <asm/idprom.h>
David S. Miller49b6e95f2007-03-29 01:38:42 -070058#include <asm/prom.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070059#endif
60
Matt Carlson63532392008-11-03 16:49:57 -080061#define BAR_0 0
62#define BAR_2 2
63
Linus Torvalds1da177e2005-04-16 15:20:36 -070064#include "tg3.h"
65
Joe Perches63c3a662011-04-26 08:12:10 +000066/* Functions & macros to verify TG3_FLAGS types */
67
68static inline int _tg3_flag(enum TG3_FLAGS flag, unsigned long *bits)
69{
70 return test_bit(flag, bits);
71}
72
73static inline void _tg3_flag_set(enum TG3_FLAGS flag, unsigned long *bits)
74{
75 set_bit(flag, bits);
76}
77
78static inline void _tg3_flag_clear(enum TG3_FLAGS flag, unsigned long *bits)
79{
80 clear_bit(flag, bits);
81}
82
83#define tg3_flag(tp, flag) \
84 _tg3_flag(TG3_FLAG_##flag, (tp)->tg3_flags)
85#define tg3_flag_set(tp, flag) \
86 _tg3_flag_set(TG3_FLAG_##flag, (tp)->tg3_flags)
87#define tg3_flag_clear(tp, flag) \
88 _tg3_flag_clear(TG3_FLAG_##flag, (tp)->tg3_flags)
89
Linus Torvalds1da177e2005-04-16 15:20:36 -070090#define DRV_MODULE_NAME "tg3"
Matt Carlson6867c842010-07-11 09:31:44 +000091#define TG3_MAJ_NUM 3
Matt Carlson5ae7fa02011-11-04 09:15:05 +000092#define TG3_MIN_NUM 121
Matt Carlson6867c842010-07-11 09:31:44 +000093#define DRV_MODULE_VERSION \
94 __stringify(TG3_MAJ_NUM) "." __stringify(TG3_MIN_NUM)
Matt Carlson5ae7fa02011-11-04 09:15:05 +000095#define DRV_MODULE_RELDATE "November 2, 2011"
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
Matt Carlsonfd6d3f02011-08-31 11:44:52 +000097#define RESET_KIND_SHUTDOWN 0
98#define RESET_KIND_INIT 1
99#define RESET_KIND_SUSPEND 2
100
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101#define TG3_DEF_RX_MODE 0
102#define TG3_DEF_TX_MODE 0
103#define TG3_DEF_MSG_ENABLE \
104 (NETIF_MSG_DRV | \
105 NETIF_MSG_PROBE | \
106 NETIF_MSG_LINK | \
107 NETIF_MSG_TIMER | \
108 NETIF_MSG_IFDOWN | \
109 NETIF_MSG_IFUP | \
110 NETIF_MSG_RX_ERR | \
111 NETIF_MSG_TX_ERR)
112
Matt Carlson520b2752011-06-13 13:39:02 +0000113#define TG3_GRC_LCLCTL_PWRSW_DELAY 100
114
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115/* length of time before we decide the hardware is borked,
116 * and dev->tx_timeout() should be called to fix the problem
117 */
Joe Perches63c3a662011-04-26 08:12:10 +0000118
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119#define TG3_TX_TIMEOUT (5 * HZ)
120
121/* hardware minimum and maximum for a single frame's data payload */
122#define TG3_MIN_MTU 60
123#define TG3_MAX_MTU(tp) \
Joe Perches63c3a662011-04-26 08:12:10 +0000124 (tg3_flag(tp, JUMBO_CAPABLE) ? 9000 : 1500)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125
126/* These numbers seem to be hard coded in the NIC firmware somehow.
127 * You can't change the ring sizes, but you can change where you place
128 * them in the NIC onboard memory.
129 */
Matt Carlson7cb32cf2010-09-30 10:34:36 +0000130#define TG3_RX_STD_RING_SIZE(tp) \
Joe Perches63c3a662011-04-26 08:12:10 +0000131 (tg3_flag(tp, LRG_PROD_RING_CAP) ? \
Matt Carlsonde9f5232011-04-05 14:22:43 +0000132 TG3_RX_STD_MAX_SIZE_5717 : TG3_RX_STD_MAX_SIZE_5700)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133#define TG3_DEF_RX_RING_PENDING 200
Matt Carlson7cb32cf2010-09-30 10:34:36 +0000134#define TG3_RX_JMB_RING_SIZE(tp) \
Joe Perches63c3a662011-04-26 08:12:10 +0000135 (tg3_flag(tp, LRG_PROD_RING_CAP) ? \
Matt Carlsonde9f5232011-04-05 14:22:43 +0000136 TG3_RX_JMB_MAX_SIZE_5717 : TG3_RX_JMB_MAX_SIZE_5700)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137#define TG3_DEF_RX_JUMBO_RING_PENDING 100
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000138#define TG3_RSS_INDIR_TBL_SIZE 128
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
140/* Do not place this n-ring entries value into the tp struct itself,
141 * we really want to expose these constants to GCC so that modulo et
142 * al. operations are done with shifts and masks instead of with
143 * hw multiply/modulo instructions. Another solution would be to
144 * replace things like '% foo' with '& (foo - 1)'.
145 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
147#define TG3_TX_RING_SIZE 512
148#define TG3_DEF_TX_RING_PENDING (TG3_TX_RING_SIZE - 1)
149
Matt Carlson2c49a442010-09-30 10:34:35 +0000150#define TG3_RX_STD_RING_BYTES(tp) \
151 (sizeof(struct tg3_rx_buffer_desc) * TG3_RX_STD_RING_SIZE(tp))
152#define TG3_RX_JMB_RING_BYTES(tp) \
153 (sizeof(struct tg3_ext_rx_buffer_desc) * TG3_RX_JMB_RING_SIZE(tp))
154#define TG3_RX_RCB_RING_BYTES(tp) \
Matt Carlson7cb32cf2010-09-30 10:34:36 +0000155 (sizeof(struct tg3_rx_buffer_desc) * (tp->rx_ret_ring_mask + 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156#define TG3_TX_RING_BYTES (sizeof(struct tg3_tx_buffer_desc) * \
157 TG3_TX_RING_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158#define NEXT_TX(N) (((N) + 1) & (TG3_TX_RING_SIZE - 1))
159
Matt Carlson287be122009-08-28 13:58:46 +0000160#define TG3_DMA_BYTE_ENAB 64
161
162#define TG3_RX_STD_DMA_SZ 1536
163#define TG3_RX_JMB_DMA_SZ 9046
164
165#define TG3_RX_DMA_TO_MAP_SZ(x) ((x) + TG3_DMA_BYTE_ENAB)
166
167#define TG3_RX_STD_MAP_SZ TG3_RX_DMA_TO_MAP_SZ(TG3_RX_STD_DMA_SZ)
168#define TG3_RX_JMB_MAP_SZ TG3_RX_DMA_TO_MAP_SZ(TG3_RX_JMB_DMA_SZ)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
Matt Carlson2c49a442010-09-30 10:34:35 +0000170#define TG3_RX_STD_BUFF_RING_SIZE(tp) \
171 (sizeof(struct ring_info) * TG3_RX_STD_RING_SIZE(tp))
Matt Carlson2b2cdb62009-11-13 13:03:48 +0000172
Matt Carlson2c49a442010-09-30 10:34:35 +0000173#define TG3_RX_JMB_BUFF_RING_SIZE(tp) \
174 (sizeof(struct ring_info) * TG3_RX_JMB_RING_SIZE(tp))
Matt Carlson2b2cdb62009-11-13 13:03:48 +0000175
Matt Carlsond2757fc2010-04-12 06:58:27 +0000176/* Due to a hardware bug, the 5701 can only DMA to memory addresses
177 * that are at least dword aligned when used in PCIX mode. The driver
178 * works around this bug by double copying the packet. This workaround
179 * is built into the normal double copy length check for efficiency.
180 *
181 * However, the double copy is only necessary on those architectures
182 * where unaligned memory accesses are inefficient. For those architectures
183 * where unaligned memory accesses incur little penalty, we can reintegrate
184 * the 5701 in the normal rx path. Doing so saves a device structure
185 * dereference by hardcoding the double copy threshold in place.
186 */
187#define TG3_RX_COPY_THRESHOLD 256
188#if NET_IP_ALIGN == 0 || defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)
189 #define TG3_RX_COPY_THRESH(tp) TG3_RX_COPY_THRESHOLD
190#else
191 #define TG3_RX_COPY_THRESH(tp) ((tp)->rx_copy_thresh)
192#endif
193
Matt Carlson81389f52011-08-31 11:44:49 +0000194#if (NET_IP_ALIGN != 0)
195#define TG3_RX_OFFSET(tp) ((tp)->rx_offset)
196#else
Eric Dumazet9205fd92011-11-18 06:47:01 +0000197#define TG3_RX_OFFSET(tp) (NET_SKB_PAD)
Matt Carlson81389f52011-08-31 11:44:49 +0000198#endif
199
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200/* minimum number of free TX descriptors required to wake up TX process */
Matt Carlsonf3f3f272009-08-28 14:03:21 +0000201#define TG3_TX_WAKEUP_THRESH(tnapi) ((tnapi)->tx_pending / 4)
Matt Carlsone31aa982011-07-27 14:20:53 +0000202#define TG3_TX_BD_DMA_MAX 4096
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203
Matt Carlsonad829262008-11-21 17:16:16 -0800204#define TG3_RAW_IP_ALIGN 2
205
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000206#define TG3_FW_UPDATE_TIMEOUT_SEC 5
207
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -0800208#define FIRMWARE_TG3 "tigon/tg3.bin"
209#define FIRMWARE_TG3TSO "tigon/tg3_tso.bin"
210#define FIRMWARE_TG3TSO5 "tigon/tg3_tso5.bin"
211
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212static char version[] __devinitdata =
Joe Perches05dbe002010-02-17 19:44:19 +0000213 DRV_MODULE_NAME ".c:v" DRV_MODULE_VERSION " (" DRV_MODULE_RELDATE ")";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214
215MODULE_AUTHOR("David S. Miller (davem@redhat.com) and Jeff Garzik (jgarzik@pobox.com)");
216MODULE_DESCRIPTION("Broadcom Tigon3 ethernet driver");
217MODULE_LICENSE("GPL");
218MODULE_VERSION(DRV_MODULE_VERSION);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -0800219MODULE_FIRMWARE(FIRMWARE_TG3);
220MODULE_FIRMWARE(FIRMWARE_TG3TSO);
221MODULE_FIRMWARE(FIRMWARE_TG3TSO5);
222
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223static int tg3_debug = -1; /* -1 == use TG3_DEF_MSG_ENABLE as value */
224module_param(tg3_debug, int, 0);
225MODULE_PARM_DESC(tg3_debug, "Tigon3 bitmapped debugging message enable value");
226
Alexey Dobriyana3aa1882010-01-07 11:58:11 +0000227static DEFINE_PCI_DEVICE_TABLE(tg3_pci_tbl) = {
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700228 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5700)},
229 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5701)},
230 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5702)},
231 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5703)},
232 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5704)},
233 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5702FE)},
234 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705)},
235 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705_2)},
236 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705M)},
237 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705M_2)},
238 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5702X)},
239 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5703X)},
240 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5704S)},
241 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5702A3)},
242 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5703A3)},
243 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5782)},
244 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5788)},
245 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5789)},
246 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5901)},
247 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5901_2)},
248 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5704S_2)},
249 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705F)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700250 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5721)},
Michael Chan126a3362006-09-27 16:03:07 -0700251 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5722)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700252 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5751)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700253 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5751M)},
254 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5751F)},
255 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5752)},
256 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5752M)},
257 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5753)},
258 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5753M)},
259 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5753F)},
260 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5754)},
261 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5754M)},
262 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5755)},
263 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5755M)},
Michael Chan126a3362006-09-27 16:03:07 -0700264 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5756)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700265 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5786)},
266 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5787)},
267 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5787M)},
Michael Chan676917d2006-12-07 00:20:22 -0800268 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5787F)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700269 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5714)},
270 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5714S)},
271 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5715)},
272 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5715S)},
273 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5780)},
274 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5780S)},
275 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5781)},
Michael Chanb5d37722006-09-27 16:06:21 -0700276 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5906)},
277 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5906M)},
Matt Carlsond30cdd22007-10-07 23:28:35 -0700278 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5784)},
279 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5764)},
Matt Carlson6c7af272007-10-21 16:12:02 -0700280 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5723)},
Matt Carlson9936bcf2007-10-10 18:03:07 -0700281 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5761)},
282 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5761E)},
Matt Carlsonc88e6682008-11-03 16:49:18 -0800283 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5761S)},
284 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5761SE)},
Matt Carlson2befdce2009-08-28 12:28:45 +0000285 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5785_G)},
286 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5785_F)},
Matt Carlson321d32a2008-11-21 17:22:19 -0800287 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57780)},
288 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57760)},
289 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57790)},
Matt Carlson5e7ccf22009-08-25 10:08:42 +0000290 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57788)},
Matt Carlson5001e2f2009-11-13 13:03:51 +0000291 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5717)},
292 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5718)},
Matt Carlsonb0f75222010-01-20 16:58:11 +0000293 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57781)},
294 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57785)},
295 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57761)},
296 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57765)},
297 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57791)},
298 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57795)},
Matt Carlson302b5002010-06-05 17:24:38 +0000299 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5719)},
Matt Carlsonba1f3c72011-04-05 14:22:50 +0000300 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5720)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700301 {PCI_DEVICE(PCI_VENDOR_ID_SYSKONNECT, PCI_DEVICE_ID_SYSKONNECT_9DXX)},
302 {PCI_DEVICE(PCI_VENDOR_ID_SYSKONNECT, PCI_DEVICE_ID_SYSKONNECT_9MXX)},
303 {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC1000)},
304 {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC1001)},
305 {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC1003)},
306 {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC9100)},
307 {PCI_DEVICE(PCI_VENDOR_ID_APPLE, PCI_DEVICE_ID_APPLE_TIGON3)},
Meelis Roos1dcb14d2011-05-25 05:43:47 +0000308 {PCI_DEVICE(0x10cf, 0x11a2)}, /* Fujitsu 1000base-SX with BCM5703SKHB */
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700309 {}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310};
311
312MODULE_DEVICE_TABLE(pci, tg3_pci_tbl);
313
Andreas Mohr50da8592006-08-14 23:54:30 -0700314static const struct {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 const char string[ETH_GSTRING_LEN];
Matt Carlson48fa55a2011-04-13 11:05:06 +0000316} ethtool_stats_keys[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 { "rx_octets" },
318 { "rx_fragments" },
319 { "rx_ucast_packets" },
320 { "rx_mcast_packets" },
321 { "rx_bcast_packets" },
322 { "rx_fcs_errors" },
323 { "rx_align_errors" },
324 { "rx_xon_pause_rcvd" },
325 { "rx_xoff_pause_rcvd" },
326 { "rx_mac_ctrl_rcvd" },
327 { "rx_xoff_entered" },
328 { "rx_frame_too_long_errors" },
329 { "rx_jabbers" },
330 { "rx_undersize_packets" },
331 { "rx_in_length_errors" },
332 { "rx_out_length_errors" },
333 { "rx_64_or_less_octet_packets" },
334 { "rx_65_to_127_octet_packets" },
335 { "rx_128_to_255_octet_packets" },
336 { "rx_256_to_511_octet_packets" },
337 { "rx_512_to_1023_octet_packets" },
338 { "rx_1024_to_1522_octet_packets" },
339 { "rx_1523_to_2047_octet_packets" },
340 { "rx_2048_to_4095_octet_packets" },
341 { "rx_4096_to_8191_octet_packets" },
342 { "rx_8192_to_9022_octet_packets" },
343
344 { "tx_octets" },
345 { "tx_collisions" },
346
347 { "tx_xon_sent" },
348 { "tx_xoff_sent" },
349 { "tx_flow_control" },
350 { "tx_mac_errors" },
351 { "tx_single_collisions" },
352 { "tx_mult_collisions" },
353 { "tx_deferred" },
354 { "tx_excessive_collisions" },
355 { "tx_late_collisions" },
356 { "tx_collide_2times" },
357 { "tx_collide_3times" },
358 { "tx_collide_4times" },
359 { "tx_collide_5times" },
360 { "tx_collide_6times" },
361 { "tx_collide_7times" },
362 { "tx_collide_8times" },
363 { "tx_collide_9times" },
364 { "tx_collide_10times" },
365 { "tx_collide_11times" },
366 { "tx_collide_12times" },
367 { "tx_collide_13times" },
368 { "tx_collide_14times" },
369 { "tx_collide_15times" },
370 { "tx_ucast_packets" },
371 { "tx_mcast_packets" },
372 { "tx_bcast_packets" },
373 { "tx_carrier_sense_errors" },
374 { "tx_discards" },
375 { "tx_errors" },
376
377 { "dma_writeq_full" },
378 { "dma_write_prioq_full" },
379 { "rxbds_empty" },
380 { "rx_discards" },
381 { "rx_errors" },
382 { "rx_threshold_hit" },
383
384 { "dma_readq_full" },
385 { "dma_read_prioq_full" },
386 { "tx_comp_queue_full" },
387
388 { "ring_set_send_prod_index" },
389 { "ring_status_update" },
390 { "nic_irqs" },
391 { "nic_avoided_irqs" },
Matt Carlson4452d092011-05-19 12:12:51 +0000392 { "nic_tx_threshold_hit" },
393
394 { "mbuf_lwm_thresh_hit" },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395};
396
Matt Carlson48fa55a2011-04-13 11:05:06 +0000397#define TG3_NUM_STATS ARRAY_SIZE(ethtool_stats_keys)
398
399
Andreas Mohr50da8592006-08-14 23:54:30 -0700400static const struct {
Michael Chan4cafd3f2005-05-29 14:56:34 -0700401 const char string[ETH_GSTRING_LEN];
Matt Carlson48fa55a2011-04-13 11:05:06 +0000402} ethtool_test_keys[] = {
Matt Carlson28a45952011-08-19 13:58:22 +0000403 { "nvram test (online) " },
404 { "link test (online) " },
405 { "register test (offline)" },
406 { "memory test (offline)" },
407 { "mac loopback test (offline)" },
408 { "phy loopback test (offline)" },
Matt Carlson941ec902011-08-19 13:58:23 +0000409 { "ext loopback test (offline)" },
Matt Carlson28a45952011-08-19 13:58:22 +0000410 { "interrupt test (offline)" },
Michael Chan4cafd3f2005-05-29 14:56:34 -0700411};
412
Matt Carlson48fa55a2011-04-13 11:05:06 +0000413#define TG3_NUM_TEST ARRAY_SIZE(ethtool_test_keys)
414
415
Michael Chanb401e9e2005-12-19 16:27:04 -0800416static void tg3_write32(struct tg3 *tp, u32 off, u32 val)
417{
418 writel(val, tp->regs + off);
419}
420
421static u32 tg3_read32(struct tg3 *tp, u32 off)
422{
Matt Carlsonde6f31e2010-04-12 06:58:30 +0000423 return readl(tp->regs + off);
Michael Chanb401e9e2005-12-19 16:27:04 -0800424}
425
Matt Carlson0d3031d2007-10-10 18:02:43 -0700426static void tg3_ape_write32(struct tg3 *tp, u32 off, u32 val)
427{
428 writel(val, tp->aperegs + off);
429}
430
431static u32 tg3_ape_read32(struct tg3 *tp, u32 off)
432{
Matt Carlsonde6f31e2010-04-12 06:58:30 +0000433 return readl(tp->aperegs + off);
Matt Carlson0d3031d2007-10-10 18:02:43 -0700434}
435
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436static void tg3_write_indirect_reg32(struct tg3 *tp, u32 off, u32 val)
437{
Michael Chan68929142005-08-09 20:17:14 -0700438 unsigned long flags;
439
440 spin_lock_irqsave(&tp->indirect_lock, flags);
Michael Chan1ee582d2005-08-09 20:16:46 -0700441 pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off);
442 pci_write_config_dword(tp->pdev, TG3PCI_REG_DATA, val);
Michael Chan68929142005-08-09 20:17:14 -0700443 spin_unlock_irqrestore(&tp->indirect_lock, flags);
Michael Chan1ee582d2005-08-09 20:16:46 -0700444}
445
446static void tg3_write_flush_reg32(struct tg3 *tp, u32 off, u32 val)
447{
448 writel(val, tp->regs + off);
449 readl(tp->regs + off);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450}
451
Michael Chan68929142005-08-09 20:17:14 -0700452static u32 tg3_read_indirect_reg32(struct tg3 *tp, u32 off)
453{
454 unsigned long flags;
455 u32 val;
456
457 spin_lock_irqsave(&tp->indirect_lock, flags);
458 pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off);
459 pci_read_config_dword(tp->pdev, TG3PCI_REG_DATA, &val);
460 spin_unlock_irqrestore(&tp->indirect_lock, flags);
461 return val;
462}
463
464static void tg3_write_indirect_mbox(struct tg3 *tp, u32 off, u32 val)
465{
466 unsigned long flags;
467
468 if (off == (MAILBOX_RCVRET_CON_IDX_0 + TG3_64BIT_REG_LOW)) {
469 pci_write_config_dword(tp->pdev, TG3PCI_RCV_RET_RING_CON_IDX +
470 TG3_64BIT_REG_LOW, val);
471 return;
472 }
Matt Carlson66711e662009-11-13 13:03:49 +0000473 if (off == TG3_RX_STD_PROD_IDX_REG) {
Michael Chan68929142005-08-09 20:17:14 -0700474 pci_write_config_dword(tp->pdev, TG3PCI_STD_RING_PROD_IDX +
475 TG3_64BIT_REG_LOW, val);
476 return;
477 }
478
479 spin_lock_irqsave(&tp->indirect_lock, flags);
480 pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off + 0x5600);
481 pci_write_config_dword(tp->pdev, TG3PCI_REG_DATA, val);
482 spin_unlock_irqrestore(&tp->indirect_lock, flags);
483
484 /* In indirect mode when disabling interrupts, we also need
485 * to clear the interrupt bit in the GRC local ctrl register.
486 */
487 if ((off == (MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW)) &&
488 (val == 0x1)) {
489 pci_write_config_dword(tp->pdev, TG3PCI_MISC_LOCAL_CTRL,
490 tp->grc_local_ctrl|GRC_LCLCTRL_CLEARINT);
491 }
492}
493
494static u32 tg3_read_indirect_mbox(struct tg3 *tp, u32 off)
495{
496 unsigned long flags;
497 u32 val;
498
499 spin_lock_irqsave(&tp->indirect_lock, flags);
500 pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off + 0x5600);
501 pci_read_config_dword(tp->pdev, TG3PCI_REG_DATA, &val);
502 spin_unlock_irqrestore(&tp->indirect_lock, flags);
503 return val;
504}
505
Michael Chanb401e9e2005-12-19 16:27:04 -0800506/* usec_wait specifies the wait time in usec when writing to certain registers
507 * where it is unsafe to read back the register without some delay.
508 * GRC_LOCAL_CTRL is one example if the GPIOs are toggled to switch power.
509 * TG3PCI_CLOCK_CTRL is another example if the clock frequencies are changed.
510 */
511static void _tw32_flush(struct tg3 *tp, u32 off, u32 val, u32 usec_wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512{
Joe Perches63c3a662011-04-26 08:12:10 +0000513 if (tg3_flag(tp, PCIX_TARGET_HWBUG) || tg3_flag(tp, ICH_WORKAROUND))
Michael Chanb401e9e2005-12-19 16:27:04 -0800514 /* Non-posted methods */
515 tp->write32(tp, off, val);
516 else {
517 /* Posted method */
518 tg3_write32(tp, off, val);
519 if (usec_wait)
520 udelay(usec_wait);
521 tp->read32(tp, off);
522 }
523 /* Wait again after the read for the posted method to guarantee that
524 * the wait time is met.
525 */
526 if (usec_wait)
527 udelay(usec_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528}
529
Michael Chan09ee9292005-08-09 20:17:00 -0700530static inline void tw32_mailbox_flush(struct tg3 *tp, u32 off, u32 val)
531{
532 tp->write32_mbox(tp, off, val);
Joe Perches63c3a662011-04-26 08:12:10 +0000533 if (!tg3_flag(tp, MBOX_WRITE_REORDER) && !tg3_flag(tp, ICH_WORKAROUND))
Michael Chan68929142005-08-09 20:17:14 -0700534 tp->read32_mbox(tp, off);
Michael Chan09ee9292005-08-09 20:17:00 -0700535}
536
Michael Chan20094932005-08-09 20:16:32 -0700537static void tg3_write32_tx_mbox(struct tg3 *tp, u32 off, u32 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538{
539 void __iomem *mbox = tp->regs + off;
540 writel(val, mbox);
Joe Perches63c3a662011-04-26 08:12:10 +0000541 if (tg3_flag(tp, TXD_MBOX_HWBUG))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 writel(val, mbox);
Joe Perches63c3a662011-04-26 08:12:10 +0000543 if (tg3_flag(tp, MBOX_WRITE_REORDER))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 readl(mbox);
545}
546
Michael Chanb5d37722006-09-27 16:06:21 -0700547static u32 tg3_read32_mbox_5906(struct tg3 *tp, u32 off)
548{
Matt Carlsonde6f31e2010-04-12 06:58:30 +0000549 return readl(tp->regs + off + GRCMBOX_BASE);
Michael Chanb5d37722006-09-27 16:06:21 -0700550}
551
552static void tg3_write32_mbox_5906(struct tg3 *tp, u32 off, u32 val)
553{
554 writel(val, tp->regs + off + GRCMBOX_BASE);
555}
556
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000557#define tw32_mailbox(reg, val) tp->write32_mbox(tp, reg, val)
Michael Chan09ee9292005-08-09 20:17:00 -0700558#define tw32_mailbox_f(reg, val) tw32_mailbox_flush(tp, (reg), (val))
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000559#define tw32_rx_mbox(reg, val) tp->write32_rx_mbox(tp, reg, val)
560#define tw32_tx_mbox(reg, val) tp->write32_tx_mbox(tp, reg, val)
561#define tr32_mailbox(reg) tp->read32_mbox(tp, reg)
Michael Chan20094932005-08-09 20:16:32 -0700562
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000563#define tw32(reg, val) tp->write32(tp, reg, val)
564#define tw32_f(reg, val) _tw32_flush(tp, (reg), (val), 0)
565#define tw32_wait_f(reg, val, us) _tw32_flush(tp, (reg), (val), (us))
566#define tr32(reg) tp->read32(tp, reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567
568static void tg3_write_mem(struct tg3 *tp, u32 off, u32 val)
569{
Michael Chan68929142005-08-09 20:17:14 -0700570 unsigned long flags;
571
Matt Carlson6ff6f812011-05-19 12:12:54 +0000572 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906 &&
Michael Chanb5d37722006-09-27 16:06:21 -0700573 (off >= NIC_SRAM_STATS_BLK) && (off < NIC_SRAM_TX_BUFFER_DESC))
574 return;
575
Michael Chan68929142005-08-09 20:17:14 -0700576 spin_lock_irqsave(&tp->indirect_lock, flags);
Joe Perches63c3a662011-04-26 08:12:10 +0000577 if (tg3_flag(tp, SRAM_USE_CONFIG)) {
Michael Chanbbadf502006-04-06 21:46:34 -0700578 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, off);
579 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_DATA, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580
Michael Chanbbadf502006-04-06 21:46:34 -0700581 /* Always leave this as zero. */
582 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, 0);
583 } else {
584 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, off);
585 tw32_f(TG3PCI_MEM_WIN_DATA, val);
586
587 /* Always leave this as zero. */
588 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, 0);
589 }
Michael Chan68929142005-08-09 20:17:14 -0700590 spin_unlock_irqrestore(&tp->indirect_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591}
592
593static void tg3_read_mem(struct tg3 *tp, u32 off, u32 *val)
594{
Michael Chan68929142005-08-09 20:17:14 -0700595 unsigned long flags;
596
Matt Carlson6ff6f812011-05-19 12:12:54 +0000597 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906 &&
Michael Chanb5d37722006-09-27 16:06:21 -0700598 (off >= NIC_SRAM_STATS_BLK) && (off < NIC_SRAM_TX_BUFFER_DESC)) {
599 *val = 0;
600 return;
601 }
602
Michael Chan68929142005-08-09 20:17:14 -0700603 spin_lock_irqsave(&tp->indirect_lock, flags);
Joe Perches63c3a662011-04-26 08:12:10 +0000604 if (tg3_flag(tp, SRAM_USE_CONFIG)) {
Michael Chanbbadf502006-04-06 21:46:34 -0700605 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, off);
606 pci_read_config_dword(tp->pdev, TG3PCI_MEM_WIN_DATA, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607
Michael Chanbbadf502006-04-06 21:46:34 -0700608 /* Always leave this as zero. */
609 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, 0);
610 } else {
611 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, off);
612 *val = tr32(TG3PCI_MEM_WIN_DATA);
613
614 /* Always leave this as zero. */
615 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, 0);
616 }
Michael Chan68929142005-08-09 20:17:14 -0700617 spin_unlock_irqrestore(&tp->indirect_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618}
619
Matt Carlson0d3031d2007-10-10 18:02:43 -0700620static void tg3_ape_lock_init(struct tg3 *tp)
621{
622 int i;
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000623 u32 regbase, bit;
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000624
625 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
626 regbase = TG3_APE_LOCK_GRANT;
627 else
628 regbase = TG3_APE_PER_LOCK_GRANT;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700629
630 /* Make sure the driver hasn't any stale locks. */
Matt Carlson78f94dc2011-11-04 09:14:58 +0000631 for (i = TG3_APE_LOCK_PHY0; i <= TG3_APE_LOCK_GPIO; i++) {
632 switch (i) {
633 case TG3_APE_LOCK_PHY0:
634 case TG3_APE_LOCK_PHY1:
635 case TG3_APE_LOCK_PHY2:
636 case TG3_APE_LOCK_PHY3:
637 bit = APE_LOCK_GRANT_DRIVER;
638 break;
639 default:
640 if (!tp->pci_fn)
641 bit = APE_LOCK_GRANT_DRIVER;
642 else
643 bit = 1 << tp->pci_fn;
644 }
645 tg3_ape_write32(tp, regbase + 4 * i, bit);
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000646 }
647
Matt Carlson0d3031d2007-10-10 18:02:43 -0700648}
649
650static int tg3_ape_lock(struct tg3 *tp, int locknum)
651{
652 int i, off;
653 int ret = 0;
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000654 u32 status, req, gnt, bit;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700655
Joe Perches63c3a662011-04-26 08:12:10 +0000656 if (!tg3_flag(tp, ENABLE_APE))
Matt Carlson0d3031d2007-10-10 18:02:43 -0700657 return 0;
658
659 switch (locknum) {
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000660 case TG3_APE_LOCK_GPIO:
661 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
662 return 0;
Matt Carlson33f401a2010-04-05 10:19:27 +0000663 case TG3_APE_LOCK_GRC:
664 case TG3_APE_LOCK_MEM:
Matt Carlson78f94dc2011-11-04 09:14:58 +0000665 if (!tp->pci_fn)
666 bit = APE_LOCK_REQ_DRIVER;
667 else
668 bit = 1 << tp->pci_fn;
Matt Carlson33f401a2010-04-05 10:19:27 +0000669 break;
670 default:
671 return -EINVAL;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700672 }
673
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000674 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761) {
675 req = TG3_APE_LOCK_REQ;
676 gnt = TG3_APE_LOCK_GRANT;
677 } else {
678 req = TG3_APE_PER_LOCK_REQ;
679 gnt = TG3_APE_PER_LOCK_GRANT;
680 }
681
Matt Carlson0d3031d2007-10-10 18:02:43 -0700682 off = 4 * locknum;
683
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000684 tg3_ape_write32(tp, req + off, bit);
Matt Carlson0d3031d2007-10-10 18:02:43 -0700685
686 /* Wait for up to 1 millisecond to acquire lock. */
687 for (i = 0; i < 100; i++) {
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000688 status = tg3_ape_read32(tp, gnt + off);
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000689 if (status == bit)
Matt Carlson0d3031d2007-10-10 18:02:43 -0700690 break;
691 udelay(10);
692 }
693
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000694 if (status != bit) {
Matt Carlson0d3031d2007-10-10 18:02:43 -0700695 /* Revoke the lock request. */
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000696 tg3_ape_write32(tp, gnt + off, bit);
Matt Carlson0d3031d2007-10-10 18:02:43 -0700697 ret = -EBUSY;
698 }
699
700 return ret;
701}
702
703static void tg3_ape_unlock(struct tg3 *tp, int locknum)
704{
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000705 u32 gnt, bit;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700706
Joe Perches63c3a662011-04-26 08:12:10 +0000707 if (!tg3_flag(tp, ENABLE_APE))
Matt Carlson0d3031d2007-10-10 18:02:43 -0700708 return;
709
710 switch (locknum) {
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000711 case TG3_APE_LOCK_GPIO:
712 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
713 return;
Matt Carlson33f401a2010-04-05 10:19:27 +0000714 case TG3_APE_LOCK_GRC:
715 case TG3_APE_LOCK_MEM:
Matt Carlson78f94dc2011-11-04 09:14:58 +0000716 if (!tp->pci_fn)
717 bit = APE_LOCK_GRANT_DRIVER;
718 else
719 bit = 1 << tp->pci_fn;
Matt Carlson33f401a2010-04-05 10:19:27 +0000720 break;
721 default:
722 return;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700723 }
724
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000725 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
726 gnt = TG3_APE_LOCK_GRANT;
727 else
728 gnt = TG3_APE_PER_LOCK_GRANT;
729
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000730 tg3_ape_write32(tp, gnt + 4 * locknum, bit);
Matt Carlson0d3031d2007-10-10 18:02:43 -0700731}
732
Matt Carlsonfd6d3f02011-08-31 11:44:52 +0000733static void tg3_ape_send_event(struct tg3 *tp, u32 event)
734{
735 int i;
736 u32 apedata;
737
738 /* NCSI does not support APE events */
739 if (tg3_flag(tp, APE_HAS_NCSI))
740 return;
741
742 apedata = tg3_ape_read32(tp, TG3_APE_SEG_SIG);
743 if (apedata != APE_SEG_SIG_MAGIC)
744 return;
745
746 apedata = tg3_ape_read32(tp, TG3_APE_FW_STATUS);
747 if (!(apedata & APE_FW_STATUS_READY))
748 return;
749
750 /* Wait for up to 1 millisecond for APE to service previous event. */
751 for (i = 0; i < 10; i++) {
752 if (tg3_ape_lock(tp, TG3_APE_LOCK_MEM))
753 return;
754
755 apedata = tg3_ape_read32(tp, TG3_APE_EVENT_STATUS);
756
757 if (!(apedata & APE_EVENT_STATUS_EVENT_PENDING))
758 tg3_ape_write32(tp, TG3_APE_EVENT_STATUS,
759 event | APE_EVENT_STATUS_EVENT_PENDING);
760
761 tg3_ape_unlock(tp, TG3_APE_LOCK_MEM);
762
763 if (!(apedata & APE_EVENT_STATUS_EVENT_PENDING))
764 break;
765
766 udelay(100);
767 }
768
769 if (!(apedata & APE_EVENT_STATUS_EVENT_PENDING))
770 tg3_ape_write32(tp, TG3_APE_EVENT, APE_EVENT_1);
771}
772
773static void tg3_ape_driver_state_change(struct tg3 *tp, int kind)
774{
775 u32 event;
776 u32 apedata;
777
778 if (!tg3_flag(tp, ENABLE_APE))
779 return;
780
781 switch (kind) {
782 case RESET_KIND_INIT:
783 tg3_ape_write32(tp, TG3_APE_HOST_SEG_SIG,
784 APE_HOST_SEG_SIG_MAGIC);
785 tg3_ape_write32(tp, TG3_APE_HOST_SEG_LEN,
786 APE_HOST_SEG_LEN_MAGIC);
787 apedata = tg3_ape_read32(tp, TG3_APE_HOST_INIT_COUNT);
788 tg3_ape_write32(tp, TG3_APE_HOST_INIT_COUNT, ++apedata);
789 tg3_ape_write32(tp, TG3_APE_HOST_DRIVER_ID,
790 APE_HOST_DRIVER_ID_MAGIC(TG3_MAJ_NUM, TG3_MIN_NUM));
791 tg3_ape_write32(tp, TG3_APE_HOST_BEHAVIOR,
792 APE_HOST_BEHAV_NO_PHYLOCK);
793 tg3_ape_write32(tp, TG3_APE_HOST_DRVR_STATE,
794 TG3_APE_HOST_DRVR_STATE_START);
795
796 event = APE_EVENT_STATUS_STATE_START;
797 break;
798 case RESET_KIND_SHUTDOWN:
799 /* With the interface we are currently using,
800 * APE does not track driver state. Wiping
801 * out the HOST SEGMENT SIGNATURE forces
802 * the APE to assume OS absent status.
803 */
804 tg3_ape_write32(tp, TG3_APE_HOST_SEG_SIG, 0x0);
805
806 if (device_may_wakeup(&tp->pdev->dev) &&
807 tg3_flag(tp, WOL_ENABLE)) {
808 tg3_ape_write32(tp, TG3_APE_HOST_WOL_SPEED,
809 TG3_APE_HOST_WOL_SPEED_AUTO);
810 apedata = TG3_APE_HOST_DRVR_STATE_WOL;
811 } else
812 apedata = TG3_APE_HOST_DRVR_STATE_UNLOAD;
813
814 tg3_ape_write32(tp, TG3_APE_HOST_DRVR_STATE, apedata);
815
816 event = APE_EVENT_STATUS_STATE_UNLOAD;
817 break;
818 case RESET_KIND_SUSPEND:
819 event = APE_EVENT_STATUS_STATE_SUSPEND;
820 break;
821 default:
822 return;
823 }
824
825 event |= APE_EVENT_STATUS_DRIVER_EVNT | APE_EVENT_STATUS_STATE_CHNGE;
826
827 tg3_ape_send_event(tp, event);
828}
829
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830static void tg3_disable_ints(struct tg3 *tp)
831{
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000832 int i;
833
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 tw32(TG3PCI_MISC_HOST_CTRL,
835 (tp->misc_host_ctrl | MISC_HOST_CTRL_MASK_PCI_INT));
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000836 for (i = 0; i < tp->irq_max; i++)
837 tw32_mailbox_f(tp->napi[i].int_mbox, 0x00000001);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838}
839
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840static void tg3_enable_ints(struct tg3 *tp)
841{
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000842 int i;
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000843
Michael Chanbbe832c2005-06-24 20:20:04 -0700844 tp->irq_sync = 0;
845 wmb();
846
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 tw32(TG3PCI_MISC_HOST_CTRL,
848 (tp->misc_host_ctrl & ~MISC_HOST_CTRL_MASK_PCI_INT));
Matt Carlsonf19af9c2009-09-01 12:47:49 +0000849
Matt Carlsonf89f38b2010-02-12 14:47:07 +0000850 tp->coal_now = tp->coalesce_mode | HOSTCC_MODE_ENABLE;
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000851 for (i = 0; i < tp->irq_cnt; i++) {
852 struct tg3_napi *tnapi = &tp->napi[i];
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000853
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000854 tw32_mailbox_f(tnapi->int_mbox, tnapi->last_tag << 24);
Joe Perches63c3a662011-04-26 08:12:10 +0000855 if (tg3_flag(tp, 1SHOT_MSI))
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000856 tw32_mailbox_f(tnapi->int_mbox, tnapi->last_tag << 24);
857
Matt Carlsonf89f38b2010-02-12 14:47:07 +0000858 tp->coal_now |= tnapi->coal_now;
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000859 }
Matt Carlsonf19af9c2009-09-01 12:47:49 +0000860
861 /* Force an initial interrupt */
Joe Perches63c3a662011-04-26 08:12:10 +0000862 if (!tg3_flag(tp, TAGGED_STATUS) &&
Matt Carlsonf19af9c2009-09-01 12:47:49 +0000863 (tp->napi[0].hw_status->status & SD_STATUS_UPDATED))
864 tw32(GRC_LOCAL_CTRL, tp->grc_local_ctrl | GRC_LCLCTRL_SETINT);
865 else
Matt Carlsonf89f38b2010-02-12 14:47:07 +0000866 tw32(HOSTCC_MODE, tp->coal_now);
867
868 tp->coal_now &= ~(tp->napi[0].coal_now | tp->napi[1].coal_now);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869}
870
Matt Carlson17375d22009-08-28 14:02:18 +0000871static inline unsigned int tg3_has_work(struct tg3_napi *tnapi)
Michael Chan04237dd2005-04-25 15:17:17 -0700872{
Matt Carlson17375d22009-08-28 14:02:18 +0000873 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +0000874 struct tg3_hw_status *sblk = tnapi->hw_status;
Michael Chan04237dd2005-04-25 15:17:17 -0700875 unsigned int work_exists = 0;
876
877 /* check for phy events */
Joe Perches63c3a662011-04-26 08:12:10 +0000878 if (!(tg3_flag(tp, USE_LINKCHG_REG) || tg3_flag(tp, POLL_SERDES))) {
Michael Chan04237dd2005-04-25 15:17:17 -0700879 if (sblk->status & SD_STATUS_LINK_CHG)
880 work_exists = 1;
881 }
882 /* check for RX/TX work to do */
Matt Carlsonf3f3f272009-08-28 14:03:21 +0000883 if (sblk->idx[0].tx_consumer != tnapi->tx_cons ||
Matt Carlson8d9d7cf2009-09-01 13:19:05 +0000884 *(tnapi->rx_rcb_prod_idx) != tnapi->rx_rcb_ptr)
Michael Chan04237dd2005-04-25 15:17:17 -0700885 work_exists = 1;
886
887 return work_exists;
888}
889
Matt Carlson17375d22009-08-28 14:02:18 +0000890/* tg3_int_reenable
Michael Chan04237dd2005-04-25 15:17:17 -0700891 * similar to tg3_enable_ints, but it accurately determines whether there
892 * is new work pending and can return without flushing the PIO write
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400893 * which reenables interrupts
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 */
Matt Carlson17375d22009-08-28 14:02:18 +0000895static void tg3_int_reenable(struct tg3_napi *tnapi)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896{
Matt Carlson17375d22009-08-28 14:02:18 +0000897 struct tg3 *tp = tnapi->tp;
898
Matt Carlson898a56f2009-08-28 14:02:40 +0000899 tw32_mailbox(tnapi->int_mbox, tnapi->last_tag << 24);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 mmiowb();
901
David S. Millerfac9b832005-05-18 22:46:34 -0700902 /* When doing tagged status, this work check is unnecessary.
903 * The last_tag we write above tells the chip which piece of
904 * work we've completed.
905 */
Joe Perches63c3a662011-04-26 08:12:10 +0000906 if (!tg3_flag(tp, TAGGED_STATUS) && tg3_has_work(tnapi))
Michael Chan04237dd2005-04-25 15:17:17 -0700907 tw32(HOSTCC_MODE, tp->coalesce_mode |
Matt Carlsonfd2ce372009-09-01 12:51:13 +0000908 HOSTCC_MODE_ENABLE | tnapi->coal_now);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909}
910
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911static void tg3_switch_clocks(struct tg3 *tp)
912{
Matt Carlsonf6eb9b12009-09-01 13:19:53 +0000913 u32 clock_ctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 u32 orig_clock_ctrl;
915
Joe Perches63c3a662011-04-26 08:12:10 +0000916 if (tg3_flag(tp, CPMU_PRESENT) || tg3_flag(tp, 5780_CLASS))
Michael Chan4cf78e42005-07-25 12:29:19 -0700917 return;
918
Matt Carlsonf6eb9b12009-09-01 13:19:53 +0000919 clock_ctrl = tr32(TG3PCI_CLOCK_CTRL);
920
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 orig_clock_ctrl = clock_ctrl;
922 clock_ctrl &= (CLOCK_CTRL_FORCE_CLKRUN |
923 CLOCK_CTRL_CLKRUN_OENABLE |
924 0x1f);
925 tp->pci_clock_ctrl = clock_ctrl;
926
Joe Perches63c3a662011-04-26 08:12:10 +0000927 if (tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 if (orig_clock_ctrl & CLOCK_CTRL_625_CORE) {
Michael Chanb401e9e2005-12-19 16:27:04 -0800929 tw32_wait_f(TG3PCI_CLOCK_CTRL,
930 clock_ctrl | CLOCK_CTRL_625_CORE, 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 }
932 } else if ((orig_clock_ctrl & CLOCK_CTRL_44MHZ_CORE) != 0) {
Michael Chanb401e9e2005-12-19 16:27:04 -0800933 tw32_wait_f(TG3PCI_CLOCK_CTRL,
934 clock_ctrl |
935 (CLOCK_CTRL_44MHZ_CORE | CLOCK_CTRL_ALTCLK),
936 40);
937 tw32_wait_f(TG3PCI_CLOCK_CTRL,
938 clock_ctrl | (CLOCK_CTRL_ALTCLK),
939 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 }
Michael Chanb401e9e2005-12-19 16:27:04 -0800941 tw32_wait_f(TG3PCI_CLOCK_CTRL, clock_ctrl, 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942}
943
944#define PHY_BUSY_LOOPS 5000
945
946static int tg3_readphy(struct tg3 *tp, int reg, u32 *val)
947{
948 u32 frame_val;
949 unsigned int loops;
950 int ret;
951
952 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
953 tw32_f(MAC_MI_MODE,
954 (tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL));
955 udelay(80);
956 }
957
958 *val = 0x0;
959
Matt Carlson882e9792009-09-01 13:21:36 +0000960 frame_val = ((tp->phy_addr << MI_COM_PHY_ADDR_SHIFT) &
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 MI_COM_PHY_ADDR_MASK);
962 frame_val |= ((reg << MI_COM_REG_ADDR_SHIFT) &
963 MI_COM_REG_ADDR_MASK);
964 frame_val |= (MI_COM_CMD_READ | MI_COM_START);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400965
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 tw32_f(MAC_MI_COM, frame_val);
967
968 loops = PHY_BUSY_LOOPS;
969 while (loops != 0) {
970 udelay(10);
971 frame_val = tr32(MAC_MI_COM);
972
973 if ((frame_val & MI_COM_BUSY) == 0) {
974 udelay(5);
975 frame_val = tr32(MAC_MI_COM);
976 break;
977 }
978 loops -= 1;
979 }
980
981 ret = -EBUSY;
982 if (loops != 0) {
983 *val = frame_val & MI_COM_DATA_MASK;
984 ret = 0;
985 }
986
987 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
988 tw32_f(MAC_MI_MODE, tp->mi_mode);
989 udelay(80);
990 }
991
992 return ret;
993}
994
995static int tg3_writephy(struct tg3 *tp, int reg, u32 val)
996{
997 u32 frame_val;
998 unsigned int loops;
999 int ret;
1000
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001001 if ((tp->phy_flags & TG3_PHYFLG_IS_FET) &&
Matt Carlson221c5632011-06-13 13:39:01 +00001002 (reg == MII_CTRL1000 || reg == MII_TG3_AUX_CTRL))
Michael Chanb5d37722006-09-27 16:06:21 -07001003 return 0;
1004
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
1006 tw32_f(MAC_MI_MODE,
1007 (tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL));
1008 udelay(80);
1009 }
1010
Matt Carlson882e9792009-09-01 13:21:36 +00001011 frame_val = ((tp->phy_addr << MI_COM_PHY_ADDR_SHIFT) &
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 MI_COM_PHY_ADDR_MASK);
1013 frame_val |= ((reg << MI_COM_REG_ADDR_SHIFT) &
1014 MI_COM_REG_ADDR_MASK);
1015 frame_val |= (val & MI_COM_DATA_MASK);
1016 frame_val |= (MI_COM_CMD_WRITE | MI_COM_START);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001017
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 tw32_f(MAC_MI_COM, frame_val);
1019
1020 loops = PHY_BUSY_LOOPS;
1021 while (loops != 0) {
1022 udelay(10);
1023 frame_val = tr32(MAC_MI_COM);
1024 if ((frame_val & MI_COM_BUSY) == 0) {
1025 udelay(5);
1026 frame_val = tr32(MAC_MI_COM);
1027 break;
1028 }
1029 loops -= 1;
1030 }
1031
1032 ret = -EBUSY;
1033 if (loops != 0)
1034 ret = 0;
1035
1036 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
1037 tw32_f(MAC_MI_MODE, tp->mi_mode);
1038 udelay(80);
1039 }
1040
1041 return ret;
1042}
1043
Matt Carlsonb0988c12011-04-20 07:57:39 +00001044static int tg3_phy_cl45_write(struct tg3 *tp, u32 devad, u32 addr, u32 val)
1045{
1046 int err;
1047
1048 err = tg3_writephy(tp, MII_TG3_MMD_CTRL, devad);
1049 if (err)
1050 goto done;
1051
1052 err = tg3_writephy(tp, MII_TG3_MMD_ADDRESS, addr);
1053 if (err)
1054 goto done;
1055
1056 err = tg3_writephy(tp, MII_TG3_MMD_CTRL,
1057 MII_TG3_MMD_CTRL_DATA_NOINC | devad);
1058 if (err)
1059 goto done;
1060
1061 err = tg3_writephy(tp, MII_TG3_MMD_ADDRESS, val);
1062
1063done:
1064 return err;
1065}
1066
1067static int tg3_phy_cl45_read(struct tg3 *tp, u32 devad, u32 addr, u32 *val)
1068{
1069 int err;
1070
1071 err = tg3_writephy(tp, MII_TG3_MMD_CTRL, devad);
1072 if (err)
1073 goto done;
1074
1075 err = tg3_writephy(tp, MII_TG3_MMD_ADDRESS, addr);
1076 if (err)
1077 goto done;
1078
1079 err = tg3_writephy(tp, MII_TG3_MMD_CTRL,
1080 MII_TG3_MMD_CTRL_DATA_NOINC | devad);
1081 if (err)
1082 goto done;
1083
1084 err = tg3_readphy(tp, MII_TG3_MMD_ADDRESS, val);
1085
1086done:
1087 return err;
1088}
1089
1090static int tg3_phydsp_read(struct tg3 *tp, u32 reg, u32 *val)
1091{
1092 int err;
1093
1094 err = tg3_writephy(tp, MII_TG3_DSP_ADDRESS, reg);
1095 if (!err)
1096 err = tg3_readphy(tp, MII_TG3_DSP_RW_PORT, val);
1097
1098 return err;
1099}
1100
1101static int tg3_phydsp_write(struct tg3 *tp, u32 reg, u32 val)
1102{
1103 int err;
1104
1105 err = tg3_writephy(tp, MII_TG3_DSP_ADDRESS, reg);
1106 if (!err)
1107 err = tg3_writephy(tp, MII_TG3_DSP_RW_PORT, val);
1108
1109 return err;
1110}
1111
Matt Carlson15ee95c2011-04-20 07:57:40 +00001112static int tg3_phy_auxctl_read(struct tg3 *tp, int reg, u32 *val)
1113{
1114 int err;
1115
1116 err = tg3_writephy(tp, MII_TG3_AUX_CTRL,
1117 (reg << MII_TG3_AUXCTL_MISC_RDSEL_SHIFT) |
1118 MII_TG3_AUXCTL_SHDWSEL_MISC);
1119 if (!err)
1120 err = tg3_readphy(tp, MII_TG3_AUX_CTRL, val);
1121
1122 return err;
1123}
1124
Matt Carlsonb4bd2922011-04-20 07:57:41 +00001125static int tg3_phy_auxctl_write(struct tg3 *tp, int reg, u32 set)
1126{
1127 if (reg == MII_TG3_AUXCTL_SHDWSEL_MISC)
1128 set |= MII_TG3_AUXCTL_MISC_WREN;
1129
1130 return tg3_writephy(tp, MII_TG3_AUX_CTRL, set | reg);
1131}
1132
Matt Carlson1d36ba42011-04-20 07:57:42 +00001133#define TG3_PHY_AUXCTL_SMDSP_ENABLE(tp) \
1134 tg3_phy_auxctl_write((tp), MII_TG3_AUXCTL_SHDWSEL_AUXCTL, \
1135 MII_TG3_AUXCTL_ACTL_SMDSP_ENA | \
1136 MII_TG3_AUXCTL_ACTL_TX_6DB)
1137
1138#define TG3_PHY_AUXCTL_SMDSP_DISABLE(tp) \
1139 tg3_phy_auxctl_write((tp), MII_TG3_AUXCTL_SHDWSEL_AUXCTL, \
1140 MII_TG3_AUXCTL_ACTL_TX_6DB);
1141
Matt Carlson95e28692008-05-25 23:44:14 -07001142static int tg3_bmcr_reset(struct tg3 *tp)
1143{
1144 u32 phy_control;
1145 int limit, err;
1146
1147 /* OK, reset it, and poll the BMCR_RESET bit until it
1148 * clears or we time out.
1149 */
1150 phy_control = BMCR_RESET;
1151 err = tg3_writephy(tp, MII_BMCR, phy_control);
1152 if (err != 0)
1153 return -EBUSY;
1154
1155 limit = 5000;
1156 while (limit--) {
1157 err = tg3_readphy(tp, MII_BMCR, &phy_control);
1158 if (err != 0)
1159 return -EBUSY;
1160
1161 if ((phy_control & BMCR_RESET) == 0) {
1162 udelay(40);
1163 break;
1164 }
1165 udelay(10);
1166 }
Roel Kluind4675b52009-02-12 16:33:27 -08001167 if (limit < 0)
Matt Carlson95e28692008-05-25 23:44:14 -07001168 return -EBUSY;
1169
1170 return 0;
1171}
1172
Matt Carlson158d7ab2008-05-29 01:37:54 -07001173static int tg3_mdio_read(struct mii_bus *bp, int mii_id, int reg)
1174{
Francois Romieu3d165432009-01-19 16:56:50 -08001175 struct tg3 *tp = bp->priv;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001176 u32 val;
1177
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001178 spin_lock_bh(&tp->lock);
Matt Carlson158d7ab2008-05-29 01:37:54 -07001179
1180 if (tg3_readphy(tp, reg, &val))
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001181 val = -EIO;
1182
1183 spin_unlock_bh(&tp->lock);
Matt Carlson158d7ab2008-05-29 01:37:54 -07001184
1185 return val;
1186}
1187
1188static int tg3_mdio_write(struct mii_bus *bp, int mii_id, int reg, u16 val)
1189{
Francois Romieu3d165432009-01-19 16:56:50 -08001190 struct tg3 *tp = bp->priv;
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001191 u32 ret = 0;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001192
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001193 spin_lock_bh(&tp->lock);
Matt Carlson158d7ab2008-05-29 01:37:54 -07001194
1195 if (tg3_writephy(tp, reg, val))
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001196 ret = -EIO;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001197
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001198 spin_unlock_bh(&tp->lock);
1199
1200 return ret;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001201}
1202
1203static int tg3_mdio_reset(struct mii_bus *bp)
1204{
1205 return 0;
1206}
1207
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001208static void tg3_mdio_config_5785(struct tg3 *tp)
Matt Carlsona9daf362008-05-25 23:49:44 -07001209{
1210 u32 val;
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001211 struct phy_device *phydev;
Matt Carlsona9daf362008-05-25 23:49:44 -07001212
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001213 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001214 switch (phydev->drv->phy_id & phydev->drv->phy_id_mask) {
Matt Carlson6a443a02010-02-17 15:17:04 +00001215 case PHY_ID_BCM50610:
1216 case PHY_ID_BCM50610M:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001217 val = MAC_PHYCFG2_50610_LED_MODES;
1218 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001219 case PHY_ID_BCMAC131:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001220 val = MAC_PHYCFG2_AC131_LED_MODES;
1221 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001222 case PHY_ID_RTL8211C:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001223 val = MAC_PHYCFG2_RTL8211C_LED_MODES;
1224 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001225 case PHY_ID_RTL8201E:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001226 val = MAC_PHYCFG2_RTL8201E_LED_MODES;
1227 break;
1228 default:
Matt Carlsona9daf362008-05-25 23:49:44 -07001229 return;
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001230 }
1231
1232 if (phydev->interface != PHY_INTERFACE_MODE_RGMII) {
1233 tw32(MAC_PHYCFG2, val);
1234
1235 val = tr32(MAC_PHYCFG1);
Matt Carlsonbb85fbb2009-08-25 10:09:07 +00001236 val &= ~(MAC_PHYCFG1_RGMII_INT |
1237 MAC_PHYCFG1_RXCLK_TO_MASK | MAC_PHYCFG1_TXCLK_TO_MASK);
1238 val |= MAC_PHYCFG1_RXCLK_TIMEOUT | MAC_PHYCFG1_TXCLK_TIMEOUT;
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001239 tw32(MAC_PHYCFG1, val);
1240
1241 return;
1242 }
1243
Joe Perches63c3a662011-04-26 08:12:10 +00001244 if (!tg3_flag(tp, RGMII_INBAND_DISABLE))
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001245 val |= MAC_PHYCFG2_EMODE_MASK_MASK |
1246 MAC_PHYCFG2_FMODE_MASK_MASK |
1247 MAC_PHYCFG2_GMODE_MASK_MASK |
1248 MAC_PHYCFG2_ACT_MASK_MASK |
1249 MAC_PHYCFG2_QUAL_MASK_MASK |
1250 MAC_PHYCFG2_INBAND_ENABLE;
1251
1252 tw32(MAC_PHYCFG2, val);
Matt Carlsona9daf362008-05-25 23:49:44 -07001253
Matt Carlsonbb85fbb2009-08-25 10:09:07 +00001254 val = tr32(MAC_PHYCFG1);
1255 val &= ~(MAC_PHYCFG1_RXCLK_TO_MASK | MAC_PHYCFG1_TXCLK_TO_MASK |
1256 MAC_PHYCFG1_RGMII_EXT_RX_DEC | MAC_PHYCFG1_RGMII_SND_STAT_EN);
Joe Perches63c3a662011-04-26 08:12:10 +00001257 if (!tg3_flag(tp, RGMII_INBAND_DISABLE)) {
1258 if (tg3_flag(tp, RGMII_EXT_IBND_RX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001259 val |= MAC_PHYCFG1_RGMII_EXT_RX_DEC;
Joe Perches63c3a662011-04-26 08:12:10 +00001260 if (tg3_flag(tp, RGMII_EXT_IBND_TX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001261 val |= MAC_PHYCFG1_RGMII_SND_STAT_EN;
1262 }
Matt Carlsonbb85fbb2009-08-25 10:09:07 +00001263 val |= MAC_PHYCFG1_RXCLK_TIMEOUT | MAC_PHYCFG1_TXCLK_TIMEOUT |
1264 MAC_PHYCFG1_RGMII_INT | MAC_PHYCFG1_TXC_DRV;
1265 tw32(MAC_PHYCFG1, val);
Matt Carlsona9daf362008-05-25 23:49:44 -07001266
Matt Carlsona9daf362008-05-25 23:49:44 -07001267 val = tr32(MAC_EXT_RGMII_MODE);
1268 val &= ~(MAC_RGMII_MODE_RX_INT_B |
1269 MAC_RGMII_MODE_RX_QUALITY |
1270 MAC_RGMII_MODE_RX_ACTIVITY |
1271 MAC_RGMII_MODE_RX_ENG_DET |
1272 MAC_RGMII_MODE_TX_ENABLE |
1273 MAC_RGMII_MODE_TX_LOWPWR |
1274 MAC_RGMII_MODE_TX_RESET);
Joe Perches63c3a662011-04-26 08:12:10 +00001275 if (!tg3_flag(tp, RGMII_INBAND_DISABLE)) {
1276 if (tg3_flag(tp, RGMII_EXT_IBND_RX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001277 val |= MAC_RGMII_MODE_RX_INT_B |
1278 MAC_RGMII_MODE_RX_QUALITY |
1279 MAC_RGMII_MODE_RX_ACTIVITY |
1280 MAC_RGMII_MODE_RX_ENG_DET;
Joe Perches63c3a662011-04-26 08:12:10 +00001281 if (tg3_flag(tp, RGMII_EXT_IBND_TX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001282 val |= MAC_RGMII_MODE_TX_ENABLE |
1283 MAC_RGMII_MODE_TX_LOWPWR |
1284 MAC_RGMII_MODE_TX_RESET;
1285 }
1286 tw32(MAC_EXT_RGMII_MODE, val);
1287}
1288
Matt Carlson158d7ab2008-05-29 01:37:54 -07001289static void tg3_mdio_start(struct tg3 *tp)
1290{
Matt Carlson158d7ab2008-05-29 01:37:54 -07001291 tp->mi_mode &= ~MAC_MI_MODE_AUTO_POLL;
1292 tw32_f(MAC_MI_MODE, tp->mi_mode);
1293 udelay(80);
Matt Carlsona9daf362008-05-25 23:49:44 -07001294
Joe Perches63c3a662011-04-26 08:12:10 +00001295 if (tg3_flag(tp, MDIOBUS_INITED) &&
Matt Carlson9ea48182010-02-17 15:17:01 +00001296 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
1297 tg3_mdio_config_5785(tp);
1298}
1299
1300static int tg3_mdio_init(struct tg3 *tp)
1301{
1302 int i;
1303 u32 reg;
1304 struct phy_device *phydev;
1305
Joe Perches63c3a662011-04-26 08:12:10 +00001306 if (tg3_flag(tp, 5717_PLUS)) {
Matt Carlson9c7df912010-06-05 17:24:36 +00001307 u32 is_serdes;
Matt Carlson882e9792009-09-01 13:21:36 +00001308
Matt Carlson69f11c92011-07-13 09:27:30 +00001309 tp->phy_addr = tp->pci_fn + 1;
Matt Carlson882e9792009-09-01 13:21:36 +00001310
Matt Carlsond1ec96a2010-01-12 10:11:38 +00001311 if (tp->pci_chip_rev_id != CHIPREV_ID_5717_A0)
1312 is_serdes = tr32(SG_DIG_STATUS) & SG_DIG_IS_SERDES;
1313 else
1314 is_serdes = tr32(TG3_CPMU_PHY_STRAP) &
1315 TG3_CPMU_PHY_STRAP_IS_SERDES;
Matt Carlson882e9792009-09-01 13:21:36 +00001316 if (is_serdes)
1317 tp->phy_addr += 7;
1318 } else
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001319 tp->phy_addr = TG3_PHY_MII_ADDR;
Matt Carlson882e9792009-09-01 13:21:36 +00001320
Matt Carlson158d7ab2008-05-29 01:37:54 -07001321 tg3_mdio_start(tp);
1322
Joe Perches63c3a662011-04-26 08:12:10 +00001323 if (!tg3_flag(tp, USE_PHYLIB) || tg3_flag(tp, MDIOBUS_INITED))
Matt Carlson158d7ab2008-05-29 01:37:54 -07001324 return 0;
1325
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001326 tp->mdio_bus = mdiobus_alloc();
1327 if (tp->mdio_bus == NULL)
1328 return -ENOMEM;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001329
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001330 tp->mdio_bus->name = "tg3 mdio bus";
1331 snprintf(tp->mdio_bus->id, MII_BUS_ID_SIZE, "%x",
Matt Carlson158d7ab2008-05-29 01:37:54 -07001332 (tp->pdev->bus->number << 8) | tp->pdev->devfn);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001333 tp->mdio_bus->priv = tp;
1334 tp->mdio_bus->parent = &tp->pdev->dev;
1335 tp->mdio_bus->read = &tg3_mdio_read;
1336 tp->mdio_bus->write = &tg3_mdio_write;
1337 tp->mdio_bus->reset = &tg3_mdio_reset;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001338 tp->mdio_bus->phy_mask = ~(1 << TG3_PHY_MII_ADDR);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001339 tp->mdio_bus->irq = &tp->mdio_irq[0];
Matt Carlson158d7ab2008-05-29 01:37:54 -07001340
1341 for (i = 0; i < PHY_MAX_ADDR; i++)
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001342 tp->mdio_bus->irq[i] = PHY_POLL;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001343
1344 /* The bus registration will look for all the PHYs on the mdio bus.
1345 * Unfortunately, it does not ensure the PHY is powered up before
1346 * accessing the PHY ID registers. A chip reset is the
1347 * quickest way to bring the device back to an operational state..
1348 */
1349 if (tg3_readphy(tp, MII_BMCR, &reg) || (reg & BMCR_PDOWN))
1350 tg3_bmcr_reset(tp);
1351
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001352 i = mdiobus_register(tp->mdio_bus);
Matt Carlsona9daf362008-05-25 23:49:44 -07001353 if (i) {
Matt Carlsonab96b242010-04-05 10:19:22 +00001354 dev_warn(&tp->pdev->dev, "mdiobus_reg failed (0x%x)\n", i);
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001355 mdiobus_free(tp->mdio_bus);
Matt Carlsona9daf362008-05-25 23:49:44 -07001356 return i;
1357 }
Matt Carlson158d7ab2008-05-29 01:37:54 -07001358
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001359 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsona9daf362008-05-25 23:49:44 -07001360
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001361 if (!phydev || !phydev->drv) {
Matt Carlsonab96b242010-04-05 10:19:22 +00001362 dev_warn(&tp->pdev->dev, "No PHY devices\n");
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001363 mdiobus_unregister(tp->mdio_bus);
1364 mdiobus_free(tp->mdio_bus);
1365 return -ENODEV;
1366 }
1367
1368 switch (phydev->drv->phy_id & phydev->drv->phy_id_mask) {
Matt Carlson6a443a02010-02-17 15:17:04 +00001369 case PHY_ID_BCM57780:
Matt Carlson321d32a2008-11-21 17:22:19 -08001370 phydev->interface = PHY_INTERFACE_MODE_GMII;
Matt Carlsonc704dc22009-11-02 14:32:12 +00001371 phydev->dev_flags |= PHY_BRCM_AUTO_PWRDWN_ENABLE;
Matt Carlson321d32a2008-11-21 17:22:19 -08001372 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001373 case PHY_ID_BCM50610:
1374 case PHY_ID_BCM50610M:
Matt Carlson32e5a8d2009-11-02 14:31:39 +00001375 phydev->dev_flags |= PHY_BRCM_CLEAR_RGMII_MODE |
Matt Carlsonc704dc22009-11-02 14:32:12 +00001376 PHY_BRCM_RX_REFCLK_UNUSED |
Matt Carlson52fae082009-11-02 14:32:38 +00001377 PHY_BRCM_DIS_TXCRXC_NOENRGY |
Matt Carlsonc704dc22009-11-02 14:32:12 +00001378 PHY_BRCM_AUTO_PWRDWN_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00001379 if (tg3_flag(tp, RGMII_INBAND_DISABLE))
Matt Carlsona9daf362008-05-25 23:49:44 -07001380 phydev->dev_flags |= PHY_BRCM_STD_IBND_DISABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00001381 if (tg3_flag(tp, RGMII_EXT_IBND_RX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001382 phydev->dev_flags |= PHY_BRCM_EXT_IBND_RX_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00001383 if (tg3_flag(tp, RGMII_EXT_IBND_TX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001384 phydev->dev_flags |= PHY_BRCM_EXT_IBND_TX_ENABLE;
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001385 /* fallthru */
Matt Carlson6a443a02010-02-17 15:17:04 +00001386 case PHY_ID_RTL8211C:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001387 phydev->interface = PHY_INTERFACE_MODE_RGMII;
Matt Carlsona9daf362008-05-25 23:49:44 -07001388 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001389 case PHY_ID_RTL8201E:
1390 case PHY_ID_BCMAC131:
Matt Carlsona9daf362008-05-25 23:49:44 -07001391 phydev->interface = PHY_INTERFACE_MODE_MII;
Matt Carlsoncdd4e09d2009-11-02 14:31:11 +00001392 phydev->dev_flags |= PHY_BRCM_AUTO_PWRDWN_ENABLE;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001393 tp->phy_flags |= TG3_PHYFLG_IS_FET;
Matt Carlsona9daf362008-05-25 23:49:44 -07001394 break;
1395 }
1396
Joe Perches63c3a662011-04-26 08:12:10 +00001397 tg3_flag_set(tp, MDIOBUS_INITED);
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001398
1399 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
1400 tg3_mdio_config_5785(tp);
Matt Carlsona9daf362008-05-25 23:49:44 -07001401
1402 return 0;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001403}
1404
1405static void tg3_mdio_fini(struct tg3 *tp)
1406{
Joe Perches63c3a662011-04-26 08:12:10 +00001407 if (tg3_flag(tp, MDIOBUS_INITED)) {
1408 tg3_flag_clear(tp, MDIOBUS_INITED);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001409 mdiobus_unregister(tp->mdio_bus);
1410 mdiobus_free(tp->mdio_bus);
Matt Carlson158d7ab2008-05-29 01:37:54 -07001411 }
1412}
1413
Matt Carlson95e28692008-05-25 23:44:14 -07001414/* tp->lock is held. */
Matt Carlson4ba526c2008-08-15 14:10:04 -07001415static inline void tg3_generate_fw_event(struct tg3 *tp)
1416{
1417 u32 val;
1418
1419 val = tr32(GRC_RX_CPU_EVENT);
1420 val |= GRC_RX_CPU_DRIVER_EVENT;
1421 tw32_f(GRC_RX_CPU_EVENT, val);
1422
1423 tp->last_event_jiffies = jiffies;
1424}
1425
1426#define TG3_FW_EVENT_TIMEOUT_USEC 2500
1427
1428/* tp->lock is held. */
Matt Carlson95e28692008-05-25 23:44:14 -07001429static void tg3_wait_for_event_ack(struct tg3 *tp)
1430{
1431 int i;
Matt Carlson4ba526c2008-08-15 14:10:04 -07001432 unsigned int delay_cnt;
1433 long time_remain;
Matt Carlson95e28692008-05-25 23:44:14 -07001434
Matt Carlson4ba526c2008-08-15 14:10:04 -07001435 /* If enough time has passed, no wait is necessary. */
1436 time_remain = (long)(tp->last_event_jiffies + 1 +
1437 usecs_to_jiffies(TG3_FW_EVENT_TIMEOUT_USEC)) -
1438 (long)jiffies;
1439 if (time_remain < 0)
1440 return;
1441
1442 /* Check if we can shorten the wait time. */
1443 delay_cnt = jiffies_to_usecs(time_remain);
1444 if (delay_cnt > TG3_FW_EVENT_TIMEOUT_USEC)
1445 delay_cnt = TG3_FW_EVENT_TIMEOUT_USEC;
1446 delay_cnt = (delay_cnt >> 3) + 1;
1447
1448 for (i = 0; i < delay_cnt; i++) {
Matt Carlson95e28692008-05-25 23:44:14 -07001449 if (!(tr32(GRC_RX_CPU_EVENT) & GRC_RX_CPU_DRIVER_EVENT))
1450 break;
Matt Carlson4ba526c2008-08-15 14:10:04 -07001451 udelay(8);
Matt Carlson95e28692008-05-25 23:44:14 -07001452 }
1453}
1454
1455/* tp->lock is held. */
1456static void tg3_ump_link_report(struct tg3 *tp)
1457{
1458 u32 reg;
1459 u32 val;
1460
Joe Perches63c3a662011-04-26 08:12:10 +00001461 if (!tg3_flag(tp, 5780_CLASS) || !tg3_flag(tp, ENABLE_ASF))
Matt Carlson95e28692008-05-25 23:44:14 -07001462 return;
1463
1464 tg3_wait_for_event_ack(tp);
1465
1466 tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX, FWCMD_NICDRV_LINK_UPDATE);
1467
1468 tg3_write_mem(tp, NIC_SRAM_FW_CMD_LEN_MBOX, 14);
1469
1470 val = 0;
1471 if (!tg3_readphy(tp, MII_BMCR, &reg))
1472 val = reg << 16;
1473 if (!tg3_readphy(tp, MII_BMSR, &reg))
1474 val |= (reg & 0xffff);
1475 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX, val);
1476
1477 val = 0;
1478 if (!tg3_readphy(tp, MII_ADVERTISE, &reg))
1479 val = reg << 16;
1480 if (!tg3_readphy(tp, MII_LPA, &reg))
1481 val |= (reg & 0xffff);
1482 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 4, val);
1483
1484 val = 0;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001485 if (!(tp->phy_flags & TG3_PHYFLG_MII_SERDES)) {
Matt Carlson95e28692008-05-25 23:44:14 -07001486 if (!tg3_readphy(tp, MII_CTRL1000, &reg))
1487 val = reg << 16;
1488 if (!tg3_readphy(tp, MII_STAT1000, &reg))
1489 val |= (reg & 0xffff);
1490 }
1491 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 8, val);
1492
1493 if (!tg3_readphy(tp, MII_PHYADDR, &reg))
1494 val = reg << 16;
1495 else
1496 val = 0;
1497 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 12, val);
1498
Matt Carlson4ba526c2008-08-15 14:10:04 -07001499 tg3_generate_fw_event(tp);
Matt Carlson95e28692008-05-25 23:44:14 -07001500}
1501
Matt Carlson8d5a89b2011-08-31 11:44:51 +00001502/* tp->lock is held. */
1503static void tg3_stop_fw(struct tg3 *tp)
1504{
1505 if (tg3_flag(tp, ENABLE_ASF) && !tg3_flag(tp, ENABLE_APE)) {
1506 /* Wait for RX cpu to ACK the previous event. */
1507 tg3_wait_for_event_ack(tp);
1508
1509 tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX, FWCMD_NICDRV_PAUSE_FW);
1510
1511 tg3_generate_fw_event(tp);
1512
1513 /* Wait for RX cpu to ACK this event. */
1514 tg3_wait_for_event_ack(tp);
1515 }
1516}
1517
Matt Carlsonfd6d3f02011-08-31 11:44:52 +00001518/* tp->lock is held. */
1519static void tg3_write_sig_pre_reset(struct tg3 *tp, int kind)
1520{
1521 tg3_write_mem(tp, NIC_SRAM_FIRMWARE_MBOX,
1522 NIC_SRAM_FIRMWARE_MBOX_MAGIC1);
1523
1524 if (tg3_flag(tp, ASF_NEW_HANDSHAKE)) {
1525 switch (kind) {
1526 case RESET_KIND_INIT:
1527 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1528 DRV_STATE_START);
1529 break;
1530
1531 case RESET_KIND_SHUTDOWN:
1532 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1533 DRV_STATE_UNLOAD);
1534 break;
1535
1536 case RESET_KIND_SUSPEND:
1537 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1538 DRV_STATE_SUSPEND);
1539 break;
1540
1541 default:
1542 break;
1543 }
1544 }
1545
1546 if (kind == RESET_KIND_INIT ||
1547 kind == RESET_KIND_SUSPEND)
1548 tg3_ape_driver_state_change(tp, kind);
1549}
1550
1551/* tp->lock is held. */
1552static void tg3_write_sig_post_reset(struct tg3 *tp, int kind)
1553{
1554 if (tg3_flag(tp, ASF_NEW_HANDSHAKE)) {
1555 switch (kind) {
1556 case RESET_KIND_INIT:
1557 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1558 DRV_STATE_START_DONE);
1559 break;
1560
1561 case RESET_KIND_SHUTDOWN:
1562 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1563 DRV_STATE_UNLOAD_DONE);
1564 break;
1565
1566 default:
1567 break;
1568 }
1569 }
1570
1571 if (kind == RESET_KIND_SHUTDOWN)
1572 tg3_ape_driver_state_change(tp, kind);
1573}
1574
1575/* tp->lock is held. */
1576static void tg3_write_sig_legacy(struct tg3 *tp, int kind)
1577{
1578 if (tg3_flag(tp, ENABLE_ASF)) {
1579 switch (kind) {
1580 case RESET_KIND_INIT:
1581 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1582 DRV_STATE_START);
1583 break;
1584
1585 case RESET_KIND_SHUTDOWN:
1586 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1587 DRV_STATE_UNLOAD);
1588 break;
1589
1590 case RESET_KIND_SUSPEND:
1591 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1592 DRV_STATE_SUSPEND);
1593 break;
1594
1595 default:
1596 break;
1597 }
1598 }
1599}
1600
1601static int tg3_poll_fw(struct tg3 *tp)
1602{
1603 int i;
1604 u32 val;
1605
1606 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
1607 /* Wait up to 20ms for init done. */
1608 for (i = 0; i < 200; i++) {
1609 if (tr32(VCPU_STATUS) & VCPU_STATUS_INIT_DONE)
1610 return 0;
1611 udelay(100);
1612 }
1613 return -ENODEV;
1614 }
1615
1616 /* Wait for firmware initialization to complete. */
1617 for (i = 0; i < 100000; i++) {
1618 tg3_read_mem(tp, NIC_SRAM_FIRMWARE_MBOX, &val);
1619 if (val == ~NIC_SRAM_FIRMWARE_MBOX_MAGIC1)
1620 break;
1621 udelay(10);
1622 }
1623
1624 /* Chip might not be fitted with firmware. Some Sun onboard
1625 * parts are configured like that. So don't signal the timeout
1626 * of the above loop as an error, but do report the lack of
1627 * running firmware once.
1628 */
1629 if (i >= 100000 && !tg3_flag(tp, NO_FWARE_REPORTED)) {
1630 tg3_flag_set(tp, NO_FWARE_REPORTED);
1631
1632 netdev_info(tp->dev, "No firmware running\n");
1633 }
1634
1635 if (tp->pci_chip_rev_id == CHIPREV_ID_57765_A0) {
1636 /* The 57765 A0 needs a little more
1637 * time to do some important work.
1638 */
1639 mdelay(10);
1640 }
1641
1642 return 0;
1643}
1644
Matt Carlson95e28692008-05-25 23:44:14 -07001645static void tg3_link_report(struct tg3 *tp)
1646{
1647 if (!netif_carrier_ok(tp->dev)) {
Joe Perches05dbe002010-02-17 19:44:19 +00001648 netif_info(tp, link, tp->dev, "Link is down\n");
Matt Carlson95e28692008-05-25 23:44:14 -07001649 tg3_ump_link_report(tp);
1650 } else if (netif_msg_link(tp)) {
Joe Perches05dbe002010-02-17 19:44:19 +00001651 netdev_info(tp->dev, "Link is up at %d Mbps, %s duplex\n",
1652 (tp->link_config.active_speed == SPEED_1000 ?
1653 1000 :
1654 (tp->link_config.active_speed == SPEED_100 ?
1655 100 : 10)),
1656 (tp->link_config.active_duplex == DUPLEX_FULL ?
1657 "full" : "half"));
Matt Carlson95e28692008-05-25 23:44:14 -07001658
Joe Perches05dbe002010-02-17 19:44:19 +00001659 netdev_info(tp->dev, "Flow control is %s for TX and %s for RX\n",
1660 (tp->link_config.active_flowctrl & FLOW_CTRL_TX) ?
1661 "on" : "off",
1662 (tp->link_config.active_flowctrl & FLOW_CTRL_RX) ?
1663 "on" : "off");
Matt Carlson47007832011-04-20 07:57:43 +00001664
1665 if (tp->phy_flags & TG3_PHYFLG_EEE_CAP)
1666 netdev_info(tp->dev, "EEE is %s\n",
1667 tp->setlpicnt ? "enabled" : "disabled");
1668
Matt Carlson95e28692008-05-25 23:44:14 -07001669 tg3_ump_link_report(tp);
1670 }
1671}
1672
1673static u16 tg3_advert_flowctrl_1000T(u8 flow_ctrl)
1674{
1675 u16 miireg;
1676
Steve Glendinninge18ce342008-12-16 02:00:00 -08001677 if ((flow_ctrl & FLOW_CTRL_TX) && (flow_ctrl & FLOW_CTRL_RX))
Matt Carlson95e28692008-05-25 23:44:14 -07001678 miireg = ADVERTISE_PAUSE_CAP;
Steve Glendinninge18ce342008-12-16 02:00:00 -08001679 else if (flow_ctrl & FLOW_CTRL_TX)
Matt Carlson95e28692008-05-25 23:44:14 -07001680 miireg = ADVERTISE_PAUSE_ASYM;
Steve Glendinninge18ce342008-12-16 02:00:00 -08001681 else if (flow_ctrl & FLOW_CTRL_RX)
Matt Carlson95e28692008-05-25 23:44:14 -07001682 miireg = ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
1683 else
1684 miireg = 0;
1685
1686 return miireg;
1687}
1688
1689static u16 tg3_advert_flowctrl_1000X(u8 flow_ctrl)
1690{
1691 u16 miireg;
1692
Steve Glendinninge18ce342008-12-16 02:00:00 -08001693 if ((flow_ctrl & FLOW_CTRL_TX) && (flow_ctrl & FLOW_CTRL_RX))
Matt Carlson95e28692008-05-25 23:44:14 -07001694 miireg = ADVERTISE_1000XPAUSE;
Steve Glendinninge18ce342008-12-16 02:00:00 -08001695 else if (flow_ctrl & FLOW_CTRL_TX)
Matt Carlson95e28692008-05-25 23:44:14 -07001696 miireg = ADVERTISE_1000XPSE_ASYM;
Steve Glendinninge18ce342008-12-16 02:00:00 -08001697 else if (flow_ctrl & FLOW_CTRL_RX)
Matt Carlson95e28692008-05-25 23:44:14 -07001698 miireg = ADVERTISE_1000XPAUSE | ADVERTISE_1000XPSE_ASYM;
1699 else
1700 miireg = 0;
1701
1702 return miireg;
1703}
1704
Matt Carlson95e28692008-05-25 23:44:14 -07001705static u8 tg3_resolve_flowctrl_1000X(u16 lcladv, u16 rmtadv)
1706{
1707 u8 cap = 0;
1708
1709 if (lcladv & ADVERTISE_1000XPAUSE) {
1710 if (lcladv & ADVERTISE_1000XPSE_ASYM) {
1711 if (rmtadv & LPA_1000XPAUSE)
Steve Glendinninge18ce342008-12-16 02:00:00 -08001712 cap = FLOW_CTRL_TX | FLOW_CTRL_RX;
Matt Carlson95e28692008-05-25 23:44:14 -07001713 else if (rmtadv & LPA_1000XPAUSE_ASYM)
Steve Glendinninge18ce342008-12-16 02:00:00 -08001714 cap = FLOW_CTRL_RX;
Matt Carlson95e28692008-05-25 23:44:14 -07001715 } else {
1716 if (rmtadv & LPA_1000XPAUSE)
Steve Glendinninge18ce342008-12-16 02:00:00 -08001717 cap = FLOW_CTRL_TX | FLOW_CTRL_RX;
Matt Carlson95e28692008-05-25 23:44:14 -07001718 }
1719 } else if (lcladv & ADVERTISE_1000XPSE_ASYM) {
1720 if ((rmtadv & LPA_1000XPAUSE) && (rmtadv & LPA_1000XPAUSE_ASYM))
Steve Glendinninge18ce342008-12-16 02:00:00 -08001721 cap = FLOW_CTRL_TX;
Matt Carlson95e28692008-05-25 23:44:14 -07001722 }
1723
1724 return cap;
1725}
1726
Matt Carlsonf51f3562008-05-25 23:45:08 -07001727static void tg3_setup_flow_control(struct tg3 *tp, u32 lcladv, u32 rmtadv)
Matt Carlson95e28692008-05-25 23:44:14 -07001728{
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001729 u8 autoneg;
Matt Carlsonf51f3562008-05-25 23:45:08 -07001730 u8 flowctrl = 0;
Matt Carlson95e28692008-05-25 23:44:14 -07001731 u32 old_rx_mode = tp->rx_mode;
1732 u32 old_tx_mode = tp->tx_mode;
1733
Joe Perches63c3a662011-04-26 08:12:10 +00001734 if (tg3_flag(tp, USE_PHYLIB))
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001735 autoneg = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]->autoneg;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001736 else
1737 autoneg = tp->link_config.autoneg;
1738
Joe Perches63c3a662011-04-26 08:12:10 +00001739 if (autoneg == AUTONEG_ENABLE && tg3_flag(tp, PAUSE_AUTONEG)) {
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001740 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
Matt Carlsonf51f3562008-05-25 23:45:08 -07001741 flowctrl = tg3_resolve_flowctrl_1000X(lcladv, rmtadv);
Matt Carlson95e28692008-05-25 23:44:14 -07001742 else
Steve Glendinningbc02ff92008-12-16 02:00:48 -08001743 flowctrl = mii_resolve_flowctrl_fdx(lcladv, rmtadv);
Matt Carlsonf51f3562008-05-25 23:45:08 -07001744 } else
1745 flowctrl = tp->link_config.flowctrl;
Matt Carlson95e28692008-05-25 23:44:14 -07001746
Matt Carlsonf51f3562008-05-25 23:45:08 -07001747 tp->link_config.active_flowctrl = flowctrl;
Matt Carlson95e28692008-05-25 23:44:14 -07001748
Steve Glendinninge18ce342008-12-16 02:00:00 -08001749 if (flowctrl & FLOW_CTRL_RX)
Matt Carlson95e28692008-05-25 23:44:14 -07001750 tp->rx_mode |= RX_MODE_FLOW_CTRL_ENABLE;
1751 else
1752 tp->rx_mode &= ~RX_MODE_FLOW_CTRL_ENABLE;
1753
Matt Carlsonf51f3562008-05-25 23:45:08 -07001754 if (old_rx_mode != tp->rx_mode)
Matt Carlson95e28692008-05-25 23:44:14 -07001755 tw32_f(MAC_RX_MODE, tp->rx_mode);
Matt Carlson95e28692008-05-25 23:44:14 -07001756
Steve Glendinninge18ce342008-12-16 02:00:00 -08001757 if (flowctrl & FLOW_CTRL_TX)
Matt Carlson95e28692008-05-25 23:44:14 -07001758 tp->tx_mode |= TX_MODE_FLOW_CTRL_ENABLE;
1759 else
1760 tp->tx_mode &= ~TX_MODE_FLOW_CTRL_ENABLE;
1761
Matt Carlsonf51f3562008-05-25 23:45:08 -07001762 if (old_tx_mode != tp->tx_mode)
Matt Carlson95e28692008-05-25 23:44:14 -07001763 tw32_f(MAC_TX_MODE, tp->tx_mode);
Matt Carlson95e28692008-05-25 23:44:14 -07001764}
1765
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001766static void tg3_adjust_link(struct net_device *dev)
1767{
1768 u8 oldflowctrl, linkmesg = 0;
1769 u32 mac_mode, lcl_adv, rmt_adv;
1770 struct tg3 *tp = netdev_priv(dev);
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001771 struct phy_device *phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001772
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001773 spin_lock_bh(&tp->lock);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001774
1775 mac_mode = tp->mac_mode & ~(MAC_MODE_PORT_MODE_MASK |
1776 MAC_MODE_HALF_DUPLEX);
1777
1778 oldflowctrl = tp->link_config.active_flowctrl;
1779
1780 if (phydev->link) {
1781 lcl_adv = 0;
1782 rmt_adv = 0;
1783
1784 if (phydev->speed == SPEED_100 || phydev->speed == SPEED_10)
1785 mac_mode |= MAC_MODE_PORT_MODE_MII;
Matt Carlsonc3df0742009-11-02 14:27:02 +00001786 else if (phydev->speed == SPEED_1000 ||
1787 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785)
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001788 mac_mode |= MAC_MODE_PORT_MODE_GMII;
Matt Carlsonc3df0742009-11-02 14:27:02 +00001789 else
1790 mac_mode |= MAC_MODE_PORT_MODE_MII;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001791
1792 if (phydev->duplex == DUPLEX_HALF)
1793 mac_mode |= MAC_MODE_HALF_DUPLEX;
1794 else {
1795 lcl_adv = tg3_advert_flowctrl_1000T(
1796 tp->link_config.flowctrl);
1797
1798 if (phydev->pause)
1799 rmt_adv = LPA_PAUSE_CAP;
1800 if (phydev->asym_pause)
1801 rmt_adv |= LPA_PAUSE_ASYM;
1802 }
1803
1804 tg3_setup_flow_control(tp, lcl_adv, rmt_adv);
1805 } else
1806 mac_mode |= MAC_MODE_PORT_MODE_GMII;
1807
1808 if (mac_mode != tp->mac_mode) {
1809 tp->mac_mode = mac_mode;
1810 tw32_f(MAC_MODE, tp->mac_mode);
1811 udelay(40);
1812 }
1813
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001814 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785) {
1815 if (phydev->speed == SPEED_10)
1816 tw32(MAC_MI_STAT,
1817 MAC_MI_STAT_10MBPS_MODE |
1818 MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
1819 else
1820 tw32(MAC_MI_STAT, MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
1821 }
1822
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001823 if (phydev->speed == SPEED_1000 && phydev->duplex == DUPLEX_HALF)
1824 tw32(MAC_TX_LENGTHS,
1825 ((2 << TX_LENGTHS_IPG_CRS_SHIFT) |
1826 (6 << TX_LENGTHS_IPG_SHIFT) |
1827 (0xff << TX_LENGTHS_SLOT_TIME_SHIFT)));
1828 else
1829 tw32(MAC_TX_LENGTHS,
1830 ((2 << TX_LENGTHS_IPG_CRS_SHIFT) |
1831 (6 << TX_LENGTHS_IPG_SHIFT) |
1832 (32 << TX_LENGTHS_SLOT_TIME_SHIFT)));
1833
1834 if ((phydev->link && tp->link_config.active_speed == SPEED_INVALID) ||
1835 (!phydev->link && tp->link_config.active_speed != SPEED_INVALID) ||
1836 phydev->speed != tp->link_config.active_speed ||
1837 phydev->duplex != tp->link_config.active_duplex ||
1838 oldflowctrl != tp->link_config.active_flowctrl)
Matt Carlsonc6cdf432010-04-05 10:19:26 +00001839 linkmesg = 1;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001840
1841 tp->link_config.active_speed = phydev->speed;
1842 tp->link_config.active_duplex = phydev->duplex;
1843
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001844 spin_unlock_bh(&tp->lock);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001845
1846 if (linkmesg)
1847 tg3_link_report(tp);
1848}
1849
1850static int tg3_phy_init(struct tg3 *tp)
1851{
1852 struct phy_device *phydev;
1853
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001854 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED)
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001855 return 0;
1856
1857 /* Bring the PHY back to a known state. */
1858 tg3_bmcr_reset(tp);
1859
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001860 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001861
1862 /* Attach the MAC to the PHY. */
Kay Sieversfb28ad32008-11-10 13:55:14 -08001863 phydev = phy_connect(tp->dev, dev_name(&phydev->dev), tg3_adjust_link,
Matt Carlsona9daf362008-05-25 23:49:44 -07001864 phydev->dev_flags, phydev->interface);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001865 if (IS_ERR(phydev)) {
Matt Carlsonab96b242010-04-05 10:19:22 +00001866 dev_err(&tp->pdev->dev, "Could not attach to PHY\n");
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001867 return PTR_ERR(phydev);
1868 }
1869
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001870 /* Mask with MAC supported features. */
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001871 switch (phydev->interface) {
1872 case PHY_INTERFACE_MODE_GMII:
1873 case PHY_INTERFACE_MODE_RGMII:
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001874 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
Matt Carlson321d32a2008-11-21 17:22:19 -08001875 phydev->supported &= (PHY_GBIT_FEATURES |
1876 SUPPORTED_Pause |
1877 SUPPORTED_Asym_Pause);
1878 break;
1879 }
1880 /* fallthru */
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001881 case PHY_INTERFACE_MODE_MII:
1882 phydev->supported &= (PHY_BASIC_FEATURES |
1883 SUPPORTED_Pause |
1884 SUPPORTED_Asym_Pause);
1885 break;
1886 default:
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001887 phy_disconnect(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001888 return -EINVAL;
1889 }
1890
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001891 tp->phy_flags |= TG3_PHYFLG_IS_CONNECTED;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001892
1893 phydev->advertising = phydev->supported;
1894
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001895 return 0;
1896}
1897
1898static void tg3_phy_start(struct tg3 *tp)
1899{
1900 struct phy_device *phydev;
1901
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001902 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001903 return;
1904
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001905 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001906
Matt Carlson80096062010-08-02 11:26:06 +00001907 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) {
1908 tp->phy_flags &= ~TG3_PHYFLG_IS_LOW_POWER;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001909 phydev->speed = tp->link_config.orig_speed;
1910 phydev->duplex = tp->link_config.orig_duplex;
1911 phydev->autoneg = tp->link_config.orig_autoneg;
1912 phydev->advertising = tp->link_config.orig_advertising;
1913 }
1914
1915 phy_start(phydev);
1916
1917 phy_start_aneg(phydev);
1918}
1919
1920static void tg3_phy_stop(struct tg3 *tp)
1921{
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001922 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001923 return;
1924
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001925 phy_stop(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001926}
1927
1928static void tg3_phy_fini(struct tg3 *tp)
1929{
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001930 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001931 phy_disconnect(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001932 tp->phy_flags &= ~TG3_PHYFLG_IS_CONNECTED;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001933 }
1934}
1935
Matt Carlson941ec902011-08-19 13:58:23 +00001936static int tg3_phy_set_extloopbk(struct tg3 *tp)
1937{
1938 int err;
1939 u32 val;
1940
1941 if (tp->phy_flags & TG3_PHYFLG_IS_FET)
1942 return 0;
1943
1944 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
1945 /* Cannot do read-modify-write on 5401 */
1946 err = tg3_phy_auxctl_write(tp,
1947 MII_TG3_AUXCTL_SHDWSEL_AUXCTL,
1948 MII_TG3_AUXCTL_ACTL_EXTLOOPBK |
1949 0x4c20);
1950 goto done;
1951 }
1952
1953 err = tg3_phy_auxctl_read(tp,
1954 MII_TG3_AUXCTL_SHDWSEL_AUXCTL, &val);
1955 if (err)
1956 return err;
1957
1958 val |= MII_TG3_AUXCTL_ACTL_EXTLOOPBK;
1959 err = tg3_phy_auxctl_write(tp,
1960 MII_TG3_AUXCTL_SHDWSEL_AUXCTL, val);
1961
1962done:
1963 return err;
1964}
1965
Matt Carlson7f97a4b2009-08-25 10:10:03 +00001966static void tg3_phy_fet_toggle_apd(struct tg3 *tp, bool enable)
1967{
1968 u32 phytest;
1969
1970 if (!tg3_readphy(tp, MII_TG3_FET_TEST, &phytest)) {
1971 u32 phy;
1972
1973 tg3_writephy(tp, MII_TG3_FET_TEST,
1974 phytest | MII_TG3_FET_SHADOW_EN);
1975 if (!tg3_readphy(tp, MII_TG3_FET_SHDW_AUXSTAT2, &phy)) {
1976 if (enable)
1977 phy |= MII_TG3_FET_SHDW_AUXSTAT2_APD;
1978 else
1979 phy &= ~MII_TG3_FET_SHDW_AUXSTAT2_APD;
1980 tg3_writephy(tp, MII_TG3_FET_SHDW_AUXSTAT2, phy);
1981 }
1982 tg3_writephy(tp, MII_TG3_FET_TEST, phytest);
1983 }
1984}
1985
Matt Carlson6833c042008-11-21 17:18:59 -08001986static void tg3_phy_toggle_apd(struct tg3 *tp, bool enable)
1987{
1988 u32 reg;
1989
Joe Perches63c3a662011-04-26 08:12:10 +00001990 if (!tg3_flag(tp, 5705_PLUS) ||
1991 (tg3_flag(tp, 5717_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001992 (tp->phy_flags & TG3_PHYFLG_MII_SERDES)))
Matt Carlson6833c042008-11-21 17:18:59 -08001993 return;
1994
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001995 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Matt Carlson7f97a4b2009-08-25 10:10:03 +00001996 tg3_phy_fet_toggle_apd(tp, enable);
1997 return;
1998 }
1999
Matt Carlson6833c042008-11-21 17:18:59 -08002000 reg = MII_TG3_MISC_SHDW_WREN |
2001 MII_TG3_MISC_SHDW_SCR5_SEL |
2002 MII_TG3_MISC_SHDW_SCR5_LPED |
2003 MII_TG3_MISC_SHDW_SCR5_DLPTLM |
2004 MII_TG3_MISC_SHDW_SCR5_SDTL |
2005 MII_TG3_MISC_SHDW_SCR5_C125OE;
2006 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5784 || !enable)
2007 reg |= MII_TG3_MISC_SHDW_SCR5_DLLAPD;
2008
2009 tg3_writephy(tp, MII_TG3_MISC_SHDW, reg);
2010
2011
2012 reg = MII_TG3_MISC_SHDW_WREN |
2013 MII_TG3_MISC_SHDW_APD_SEL |
2014 MII_TG3_MISC_SHDW_APD_WKTM_84MS;
2015 if (enable)
2016 reg |= MII_TG3_MISC_SHDW_APD_ENABLE;
2017
2018 tg3_writephy(tp, MII_TG3_MISC_SHDW, reg);
2019}
2020
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002021static void tg3_phy_toggle_automdix(struct tg3 *tp, int enable)
2022{
2023 u32 phy;
2024
Joe Perches63c3a662011-04-26 08:12:10 +00002025 if (!tg3_flag(tp, 5705_PLUS) ||
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002026 (tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002027 return;
2028
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002029 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002030 u32 ephy;
2031
Matt Carlson535ef6e2009-08-25 10:09:36 +00002032 if (!tg3_readphy(tp, MII_TG3_FET_TEST, &ephy)) {
2033 u32 reg = MII_TG3_FET_SHDW_MISCCTRL;
2034
2035 tg3_writephy(tp, MII_TG3_FET_TEST,
2036 ephy | MII_TG3_FET_SHADOW_EN);
2037 if (!tg3_readphy(tp, reg, &phy)) {
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002038 if (enable)
Matt Carlson535ef6e2009-08-25 10:09:36 +00002039 phy |= MII_TG3_FET_SHDW_MISCCTRL_MDIX;
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002040 else
Matt Carlson535ef6e2009-08-25 10:09:36 +00002041 phy &= ~MII_TG3_FET_SHDW_MISCCTRL_MDIX;
2042 tg3_writephy(tp, reg, phy);
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002043 }
Matt Carlson535ef6e2009-08-25 10:09:36 +00002044 tg3_writephy(tp, MII_TG3_FET_TEST, ephy);
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002045 }
2046 } else {
Matt Carlson15ee95c2011-04-20 07:57:40 +00002047 int ret;
2048
2049 ret = tg3_phy_auxctl_read(tp,
2050 MII_TG3_AUXCTL_SHDWSEL_MISC, &phy);
2051 if (!ret) {
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002052 if (enable)
2053 phy |= MII_TG3_AUXCTL_MISC_FORCE_AMDIX;
2054 else
2055 phy &= ~MII_TG3_AUXCTL_MISC_FORCE_AMDIX;
Matt Carlsonb4bd2922011-04-20 07:57:41 +00002056 tg3_phy_auxctl_write(tp,
2057 MII_TG3_AUXCTL_SHDWSEL_MISC, phy);
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002058 }
2059 }
2060}
2061
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062static void tg3_phy_set_wirespeed(struct tg3 *tp)
2063{
Matt Carlson15ee95c2011-04-20 07:57:40 +00002064 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065 u32 val;
2066
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002067 if (tp->phy_flags & TG3_PHYFLG_NO_ETH_WIRE_SPEED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068 return;
2069
Matt Carlson15ee95c2011-04-20 07:57:40 +00002070 ret = tg3_phy_auxctl_read(tp, MII_TG3_AUXCTL_SHDWSEL_MISC, &val);
2071 if (!ret)
Matt Carlsonb4bd2922011-04-20 07:57:41 +00002072 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_MISC,
2073 val | MII_TG3_AUXCTL_MISC_WIRESPD_EN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074}
2075
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002076static void tg3_phy_apply_otp(struct tg3 *tp)
2077{
2078 u32 otp, phy;
2079
2080 if (!tp->phy_otp)
2081 return;
2082
2083 otp = tp->phy_otp;
2084
Matt Carlson1d36ba42011-04-20 07:57:42 +00002085 if (TG3_PHY_AUXCTL_SMDSP_ENABLE(tp))
2086 return;
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002087
2088 phy = ((otp & TG3_OTP_AGCTGT_MASK) >> TG3_OTP_AGCTGT_SHIFT);
2089 phy |= MII_TG3_DSP_TAP1_AGCTGT_DFLT;
2090 tg3_phydsp_write(tp, MII_TG3_DSP_TAP1, phy);
2091
2092 phy = ((otp & TG3_OTP_HPFFLTR_MASK) >> TG3_OTP_HPFFLTR_SHIFT) |
2093 ((otp & TG3_OTP_HPFOVER_MASK) >> TG3_OTP_HPFOVER_SHIFT);
2094 tg3_phydsp_write(tp, MII_TG3_DSP_AADJ1CH0, phy);
2095
2096 phy = ((otp & TG3_OTP_LPFDIS_MASK) >> TG3_OTP_LPFDIS_SHIFT);
2097 phy |= MII_TG3_DSP_AADJ1CH3_ADCCKADJ;
2098 tg3_phydsp_write(tp, MII_TG3_DSP_AADJ1CH3, phy);
2099
2100 phy = ((otp & TG3_OTP_VDAC_MASK) >> TG3_OTP_VDAC_SHIFT);
2101 tg3_phydsp_write(tp, MII_TG3_DSP_EXP75, phy);
2102
2103 phy = ((otp & TG3_OTP_10BTAMP_MASK) >> TG3_OTP_10BTAMP_SHIFT);
2104 tg3_phydsp_write(tp, MII_TG3_DSP_EXP96, phy);
2105
2106 phy = ((otp & TG3_OTP_ROFF_MASK) >> TG3_OTP_ROFF_SHIFT) |
2107 ((otp & TG3_OTP_RCOFF_MASK) >> TG3_OTP_RCOFF_SHIFT);
2108 tg3_phydsp_write(tp, MII_TG3_DSP_EXP97, phy);
2109
Matt Carlson1d36ba42011-04-20 07:57:42 +00002110 TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002111}
2112
Matt Carlson52b02d02010-10-14 10:37:41 +00002113static void tg3_phy_eee_adjust(struct tg3 *tp, u32 current_link_up)
2114{
2115 u32 val;
2116
2117 if (!(tp->phy_flags & TG3_PHYFLG_EEE_CAP))
2118 return;
2119
2120 tp->setlpicnt = 0;
2121
2122 if (tp->link_config.autoneg == AUTONEG_ENABLE &&
2123 current_link_up == 1 &&
Matt Carlsona6b68da2010-12-06 08:28:52 +00002124 tp->link_config.active_duplex == DUPLEX_FULL &&
2125 (tp->link_config.active_speed == SPEED_100 ||
2126 tp->link_config.active_speed == SPEED_1000)) {
Matt Carlson52b02d02010-10-14 10:37:41 +00002127 u32 eeectl;
2128
2129 if (tp->link_config.active_speed == SPEED_1000)
2130 eeectl = TG3_CPMU_EEE_CTRL_EXIT_16_5_US;
2131 else
2132 eeectl = TG3_CPMU_EEE_CTRL_EXIT_36_US;
2133
2134 tw32(TG3_CPMU_EEE_CTRL, eeectl);
2135
Matt Carlson3110f5f52010-12-06 08:28:50 +00002136 tg3_phy_cl45_read(tp, MDIO_MMD_AN,
2137 TG3_CL45_D7_EEERES_STAT, &val);
Matt Carlson52b02d02010-10-14 10:37:41 +00002138
Matt Carlsonb0c59432011-05-19 12:12:48 +00002139 if (val == TG3_CL45_D7_EEERES_STAT_LP_1000T ||
2140 val == TG3_CL45_D7_EEERES_STAT_LP_100TX)
Matt Carlson52b02d02010-10-14 10:37:41 +00002141 tp->setlpicnt = 2;
2142 }
2143
2144 if (!tp->setlpicnt) {
Matt Carlsonb715ce92011-07-20 10:20:52 +00002145 if (current_link_up == 1 &&
2146 !TG3_PHY_AUXCTL_SMDSP_ENABLE(tp)) {
2147 tg3_phydsp_write(tp, MII_TG3_DSP_TAP26, 0x0000);
2148 TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
2149 }
2150
Matt Carlson52b02d02010-10-14 10:37:41 +00002151 val = tr32(TG3_CPMU_EEE_MODE);
2152 tw32(TG3_CPMU_EEE_MODE, val & ~TG3_CPMU_EEEMD_LPI_ENABLE);
2153 }
2154}
2155
Matt Carlsonb0c59432011-05-19 12:12:48 +00002156static void tg3_phy_eee_enable(struct tg3 *tp)
2157{
2158 u32 val;
2159
2160 if (tp->link_config.active_speed == SPEED_1000 &&
2161 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
2162 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
2163 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765) &&
2164 !TG3_PHY_AUXCTL_SMDSP_ENABLE(tp)) {
Matt Carlsonb715ce92011-07-20 10:20:52 +00002165 val = MII_TG3_DSP_TAP26_ALNOKO |
2166 MII_TG3_DSP_TAP26_RMRXSTO;
2167 tg3_phydsp_write(tp, MII_TG3_DSP_TAP26, val);
Matt Carlsonb0c59432011-05-19 12:12:48 +00002168 TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
2169 }
2170
2171 val = tr32(TG3_CPMU_EEE_MODE);
2172 tw32(TG3_CPMU_EEE_MODE, val | TG3_CPMU_EEEMD_LPI_ENABLE);
2173}
2174
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175static int tg3_wait_macro_done(struct tg3 *tp)
2176{
2177 int limit = 100;
2178
2179 while (limit--) {
2180 u32 tmp32;
2181
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002182 if (!tg3_readphy(tp, MII_TG3_DSP_CONTROL, &tmp32)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183 if ((tmp32 & 0x1000) == 0)
2184 break;
2185 }
2186 }
Roel Kluind4675b52009-02-12 16:33:27 -08002187 if (limit < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188 return -EBUSY;
2189
2190 return 0;
2191}
2192
2193static int tg3_phy_write_and_check_testpat(struct tg3 *tp, int *resetp)
2194{
2195 static const u32 test_pat[4][6] = {
2196 { 0x00005555, 0x00000005, 0x00002aaa, 0x0000000a, 0x00003456, 0x00000003 },
2197 { 0x00002aaa, 0x0000000a, 0x00003333, 0x00000003, 0x0000789a, 0x00000005 },
2198 { 0x00005a5a, 0x00000005, 0x00002a6a, 0x0000000a, 0x00001bcd, 0x00000003 },
2199 { 0x00002a5a, 0x0000000a, 0x000033c3, 0x00000003, 0x00002ef1, 0x00000005 }
2200 };
2201 int chan;
2202
2203 for (chan = 0; chan < 4; chan++) {
2204 int i;
2205
2206 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
2207 (chan * 0x2000) | 0x0200);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002208 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0002);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209
2210 for (i = 0; i < 6; i++)
2211 tg3_writephy(tp, MII_TG3_DSP_RW_PORT,
2212 test_pat[chan][i]);
2213
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002214 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0202);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215 if (tg3_wait_macro_done(tp)) {
2216 *resetp = 1;
2217 return -EBUSY;
2218 }
2219
2220 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
2221 (chan * 0x2000) | 0x0200);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002222 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0082);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223 if (tg3_wait_macro_done(tp)) {
2224 *resetp = 1;
2225 return -EBUSY;
2226 }
2227
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002228 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0802);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229 if (tg3_wait_macro_done(tp)) {
2230 *resetp = 1;
2231 return -EBUSY;
2232 }
2233
2234 for (i = 0; i < 6; i += 2) {
2235 u32 low, high;
2236
2237 if (tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &low) ||
2238 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &high) ||
2239 tg3_wait_macro_done(tp)) {
2240 *resetp = 1;
2241 return -EBUSY;
2242 }
2243 low &= 0x7fff;
2244 high &= 0x000f;
2245 if (low != test_pat[chan][i] ||
2246 high != test_pat[chan][i+1]) {
2247 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x000b);
2248 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x4001);
2249 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x4005);
2250
2251 return -EBUSY;
2252 }
2253 }
2254 }
2255
2256 return 0;
2257}
2258
2259static int tg3_phy_reset_chanpat(struct tg3 *tp)
2260{
2261 int chan;
2262
2263 for (chan = 0; chan < 4; chan++) {
2264 int i;
2265
2266 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
2267 (chan * 0x2000) | 0x0200);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002268 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0002);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269 for (i = 0; i < 6; i++)
2270 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x000);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002271 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0202);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272 if (tg3_wait_macro_done(tp))
2273 return -EBUSY;
2274 }
2275
2276 return 0;
2277}
2278
2279static int tg3_phy_reset_5703_4_5(struct tg3 *tp)
2280{
2281 u32 reg32, phy9_orig;
2282 int retries, do_phy_reset, err;
2283
2284 retries = 10;
2285 do_phy_reset = 1;
2286 do {
2287 if (do_phy_reset) {
2288 err = tg3_bmcr_reset(tp);
2289 if (err)
2290 return err;
2291 do_phy_reset = 0;
2292 }
2293
2294 /* Disable transmitter and interrupt. */
2295 if (tg3_readphy(tp, MII_TG3_EXT_CTRL, &reg32))
2296 continue;
2297
2298 reg32 |= 0x3000;
2299 tg3_writephy(tp, MII_TG3_EXT_CTRL, reg32);
2300
2301 /* Set full-duplex, 1000 mbps. */
2302 tg3_writephy(tp, MII_BMCR,
Matt Carlson221c5632011-06-13 13:39:01 +00002303 BMCR_FULLDPLX | BMCR_SPEED1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304
2305 /* Set to master mode. */
Matt Carlson221c5632011-06-13 13:39:01 +00002306 if (tg3_readphy(tp, MII_CTRL1000, &phy9_orig))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307 continue;
2308
Matt Carlson221c5632011-06-13 13:39:01 +00002309 tg3_writephy(tp, MII_CTRL1000,
2310 CTL1000_AS_MASTER | CTL1000_ENABLE_MASTER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311
Matt Carlson1d36ba42011-04-20 07:57:42 +00002312 err = TG3_PHY_AUXCTL_SMDSP_ENABLE(tp);
2313 if (err)
2314 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315
2316 /* Block the PHY control access. */
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00002317 tg3_phydsp_write(tp, 0x8005, 0x0800);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318
2319 err = tg3_phy_write_and_check_testpat(tp, &do_phy_reset);
2320 if (!err)
2321 break;
2322 } while (--retries);
2323
2324 err = tg3_phy_reset_chanpat(tp);
2325 if (err)
2326 return err;
2327
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00002328 tg3_phydsp_write(tp, 0x8005, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329
2330 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x8200);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002331 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332
Matt Carlson1d36ba42011-04-20 07:57:42 +00002333 TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334
Matt Carlson221c5632011-06-13 13:39:01 +00002335 tg3_writephy(tp, MII_CTRL1000, phy9_orig);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336
2337 if (!tg3_readphy(tp, MII_TG3_EXT_CTRL, &reg32)) {
2338 reg32 &= ~0x3000;
2339 tg3_writephy(tp, MII_TG3_EXT_CTRL, reg32);
2340 } else if (!err)
2341 err = -EBUSY;
2342
2343 return err;
2344}
2345
2346/* This will reset the tigon3 PHY if there is no valid
2347 * link unless the FORCE argument is non-zero.
2348 */
2349static int tg3_phy_reset(struct tg3 *tp)
2350{
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002351 u32 val, cpmuctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352 int err;
2353
Michael Chan60189dd2006-12-17 17:08:07 -08002354 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan60189dd2006-12-17 17:08:07 -08002355 val = tr32(GRC_MISC_CFG);
2356 tw32_f(GRC_MISC_CFG, val & ~GRC_MISC_CFG_EPHY_IDDQ);
2357 udelay(40);
2358 }
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002359 err = tg3_readphy(tp, MII_BMSR, &val);
2360 err |= tg3_readphy(tp, MII_BMSR, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361 if (err != 0)
2362 return -EBUSY;
2363
Michael Chanc8e1e822006-04-29 18:55:17 -07002364 if (netif_running(tp->dev) && netif_carrier_ok(tp->dev)) {
2365 netif_carrier_off(tp->dev);
2366 tg3_link_report(tp);
2367 }
2368
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
2370 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
2371 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
2372 err = tg3_phy_reset_5703_4_5(tp);
2373 if (err)
2374 return err;
2375 goto out;
2376 }
2377
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002378 cpmuctrl = 0;
2379 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
2380 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX) {
2381 cpmuctrl = tr32(TG3_CPMU_CTRL);
2382 if (cpmuctrl & CPMU_CTRL_GPHY_10MB_RXONLY)
2383 tw32(TG3_CPMU_CTRL,
2384 cpmuctrl & ~CPMU_CTRL_GPHY_10MB_RXONLY);
2385 }
2386
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387 err = tg3_bmcr_reset(tp);
2388 if (err)
2389 return err;
2390
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002391 if (cpmuctrl & CPMU_CTRL_GPHY_10MB_RXONLY) {
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002392 val = MII_TG3_DSP_EXP8_AEDW | MII_TG3_DSP_EXP8_REJ2MHz;
2393 tg3_phydsp_write(tp, MII_TG3_DSP_EXP8, val);
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002394
2395 tw32(TG3_CPMU_CTRL, cpmuctrl);
2396 }
2397
Matt Carlsonbcb37f62008-11-03 16:52:09 -08002398 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX ||
2399 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5761_AX) {
Matt Carlsonce057f02007-11-12 21:08:03 -08002400 val = tr32(TG3_CPMU_LSPD_1000MB_CLK);
2401 if ((val & CPMU_LSPD_1000MB_MACCLK_MASK) ==
2402 CPMU_LSPD_1000MB_MACCLK_12_5) {
2403 val &= ~CPMU_LSPD_1000MB_MACCLK_MASK;
2404 udelay(40);
2405 tw32_f(TG3_CPMU_LSPD_1000MB_CLK, val);
2406 }
2407 }
2408
Joe Perches63c3a662011-04-26 08:12:10 +00002409 if (tg3_flag(tp, 5717_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002410 (tp->phy_flags & TG3_PHYFLG_MII_SERDES))
Matt Carlsonecf14102010-01-20 16:58:05 +00002411 return 0;
2412
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002413 tg3_phy_apply_otp(tp);
2414
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002415 if (tp->phy_flags & TG3_PHYFLG_ENABLE_APD)
Matt Carlson6833c042008-11-21 17:18:59 -08002416 tg3_phy_toggle_apd(tp, true);
2417 else
2418 tg3_phy_toggle_apd(tp, false);
2419
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420out:
Matt Carlson1d36ba42011-04-20 07:57:42 +00002421 if ((tp->phy_flags & TG3_PHYFLG_ADC_BUG) &&
2422 !TG3_PHY_AUXCTL_SMDSP_ENABLE(tp)) {
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00002423 tg3_phydsp_write(tp, 0x201f, 0x2aaa);
2424 tg3_phydsp_write(tp, 0x000a, 0x0323);
Matt Carlson1d36ba42011-04-20 07:57:42 +00002425 TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426 }
Matt Carlson1d36ba42011-04-20 07:57:42 +00002427
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002428 if (tp->phy_flags & TG3_PHYFLG_5704_A0_BUG) {
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002429 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8d68);
2430 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8d68);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431 }
Matt Carlson1d36ba42011-04-20 07:57:42 +00002432
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002433 if (tp->phy_flags & TG3_PHYFLG_BER_BUG) {
Matt Carlson1d36ba42011-04-20 07:57:42 +00002434 if (!TG3_PHY_AUXCTL_SMDSP_ENABLE(tp)) {
2435 tg3_phydsp_write(tp, 0x000a, 0x310b);
2436 tg3_phydsp_write(tp, 0x201f, 0x9506);
2437 tg3_phydsp_write(tp, 0x401f, 0x14e2);
2438 TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
2439 }
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002440 } else if (tp->phy_flags & TG3_PHYFLG_JITTER_BUG) {
Matt Carlson1d36ba42011-04-20 07:57:42 +00002441 if (!TG3_PHY_AUXCTL_SMDSP_ENABLE(tp)) {
2442 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x000a);
2443 if (tp->phy_flags & TG3_PHYFLG_ADJUST_TRIM) {
2444 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x110b);
2445 tg3_writephy(tp, MII_TG3_TEST1,
2446 MII_TG3_TEST1_TRIM_EN | 0x4);
2447 } else
2448 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x010b);
2449
2450 TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
2451 }
Michael Chanc424cb22006-04-29 18:56:34 -07002452 }
Matt Carlson1d36ba42011-04-20 07:57:42 +00002453
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454 /* Set Extended packet length bit (bit 14) on all chips that */
2455 /* support jumbo frames */
Matt Carlson79eb6902010-02-17 15:17:03 +00002456 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457 /* Cannot do read-modify-write on 5401 */
Matt Carlsonb4bd2922011-04-20 07:57:41 +00002458 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_AUXCTL, 0x4c20);
Joe Perches63c3a662011-04-26 08:12:10 +00002459 } else if (tg3_flag(tp, JUMBO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460 /* Set bit 14 with read-modify-write to preserve other bits */
Matt Carlson15ee95c2011-04-20 07:57:40 +00002461 err = tg3_phy_auxctl_read(tp,
2462 MII_TG3_AUXCTL_SHDWSEL_AUXCTL, &val);
2463 if (!err)
Matt Carlsonb4bd2922011-04-20 07:57:41 +00002464 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_AUXCTL,
2465 val | MII_TG3_AUXCTL_ACTL_EXTPKTLEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466 }
2467
2468 /* Set phy register 0x10 bit 0 to high fifo elasticity to support
2469 * jumbo frames transmission.
2470 */
Joe Perches63c3a662011-04-26 08:12:10 +00002471 if (tg3_flag(tp, JUMBO_CAPABLE)) {
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002472 if (!tg3_readphy(tp, MII_TG3_EXT_CTRL, &val))
Matt Carlsonc6cdf432010-04-05 10:19:26 +00002473 tg3_writephy(tp, MII_TG3_EXT_CTRL,
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002474 val | MII_TG3_EXT_CTRL_FIFO_ELASTIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475 }
2476
Michael Chan715116a2006-09-27 16:09:25 -07002477 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan715116a2006-09-27 16:09:25 -07002478 /* adjust output voltage */
Matt Carlson535ef6e2009-08-25 10:09:36 +00002479 tg3_writephy(tp, MII_TG3_FET_PTEST, 0x12);
Michael Chan715116a2006-09-27 16:09:25 -07002480 }
2481
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002482 tg3_phy_toggle_automdix(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483 tg3_phy_set_wirespeed(tp);
2484 return 0;
2485}
2486
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002487#define TG3_GPIO_MSG_DRVR_PRES 0x00000001
2488#define TG3_GPIO_MSG_NEED_VAUX 0x00000002
2489#define TG3_GPIO_MSG_MASK (TG3_GPIO_MSG_DRVR_PRES | \
2490 TG3_GPIO_MSG_NEED_VAUX)
2491#define TG3_GPIO_MSG_ALL_DRVR_PRES_MASK \
2492 ((TG3_GPIO_MSG_DRVR_PRES << 0) | \
2493 (TG3_GPIO_MSG_DRVR_PRES << 4) | \
2494 (TG3_GPIO_MSG_DRVR_PRES << 8) | \
2495 (TG3_GPIO_MSG_DRVR_PRES << 12))
2496
2497#define TG3_GPIO_MSG_ALL_NEED_VAUX_MASK \
2498 ((TG3_GPIO_MSG_NEED_VAUX << 0) | \
2499 (TG3_GPIO_MSG_NEED_VAUX << 4) | \
2500 (TG3_GPIO_MSG_NEED_VAUX << 8) | \
2501 (TG3_GPIO_MSG_NEED_VAUX << 12))
2502
2503static inline u32 tg3_set_function_status(struct tg3 *tp, u32 newstat)
2504{
2505 u32 status, shift;
2506
2507 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
2508 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
2509 status = tg3_ape_read32(tp, TG3_APE_GPIO_MSG);
2510 else
2511 status = tr32(TG3_CPMU_DRV_STATUS);
2512
2513 shift = TG3_APE_GPIO_MSG_SHIFT + 4 * tp->pci_fn;
2514 status &= ~(TG3_GPIO_MSG_MASK << shift);
2515 status |= (newstat << shift);
2516
2517 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
2518 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
2519 tg3_ape_write32(tp, TG3_APE_GPIO_MSG, status);
2520 else
2521 tw32(TG3_CPMU_DRV_STATUS, status);
2522
2523 return status >> TG3_APE_GPIO_MSG_SHIFT;
2524}
2525
Matt Carlson520b2752011-06-13 13:39:02 +00002526static inline int tg3_pwrsrc_switch_to_vmain(struct tg3 *tp)
2527{
2528 if (!tg3_flag(tp, IS_NIC))
2529 return 0;
2530
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002531 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
2532 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
2533 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
2534 if (tg3_ape_lock(tp, TG3_APE_LOCK_GPIO))
2535 return -EIO;
Matt Carlson520b2752011-06-13 13:39:02 +00002536
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002537 tg3_set_function_status(tp, TG3_GPIO_MSG_DRVR_PRES);
2538
2539 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl,
2540 TG3_GRC_LCLCTL_PWRSW_DELAY);
2541
2542 tg3_ape_unlock(tp, TG3_APE_LOCK_GPIO);
2543 } else {
2544 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl,
2545 TG3_GRC_LCLCTL_PWRSW_DELAY);
2546 }
Matt Carlson6f5c8f832011-07-13 09:27:31 +00002547
Matt Carlson520b2752011-06-13 13:39:02 +00002548 return 0;
2549}
2550
2551static void tg3_pwrsrc_die_with_vmain(struct tg3 *tp)
2552{
2553 u32 grc_local_ctrl;
2554
2555 if (!tg3_flag(tp, IS_NIC) ||
2556 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2557 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)
2558 return;
2559
2560 grc_local_ctrl = tp->grc_local_ctrl | GRC_LCLCTRL_GPIO_OE1;
2561
2562 tw32_wait_f(GRC_LOCAL_CTRL,
2563 grc_local_ctrl | GRC_LCLCTRL_GPIO_OUTPUT1,
2564 TG3_GRC_LCLCTL_PWRSW_DELAY);
2565
2566 tw32_wait_f(GRC_LOCAL_CTRL,
2567 grc_local_ctrl,
2568 TG3_GRC_LCLCTL_PWRSW_DELAY);
2569
2570 tw32_wait_f(GRC_LOCAL_CTRL,
2571 grc_local_ctrl | GRC_LCLCTRL_GPIO_OUTPUT1,
2572 TG3_GRC_LCLCTL_PWRSW_DELAY);
2573}
2574
2575static void tg3_pwrsrc_switch_to_vaux(struct tg3 *tp)
2576{
2577 if (!tg3_flag(tp, IS_NIC))
2578 return;
2579
2580 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2581 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
2582 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2583 (GRC_LCLCTRL_GPIO_OE0 |
2584 GRC_LCLCTRL_GPIO_OE1 |
2585 GRC_LCLCTRL_GPIO_OE2 |
2586 GRC_LCLCTRL_GPIO_OUTPUT0 |
2587 GRC_LCLCTRL_GPIO_OUTPUT1),
2588 TG3_GRC_LCLCTL_PWRSW_DELAY);
2589 } else if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761 ||
2590 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761S) {
2591 /* The 5761 non-e device swaps GPIO 0 and GPIO 2. */
2592 u32 grc_local_ctrl = GRC_LCLCTRL_GPIO_OE0 |
2593 GRC_LCLCTRL_GPIO_OE1 |
2594 GRC_LCLCTRL_GPIO_OE2 |
2595 GRC_LCLCTRL_GPIO_OUTPUT0 |
2596 GRC_LCLCTRL_GPIO_OUTPUT1 |
2597 tp->grc_local_ctrl;
2598 tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl,
2599 TG3_GRC_LCLCTL_PWRSW_DELAY);
2600
2601 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OUTPUT2;
2602 tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl,
2603 TG3_GRC_LCLCTL_PWRSW_DELAY);
2604
2605 grc_local_ctrl &= ~GRC_LCLCTRL_GPIO_OUTPUT0;
2606 tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl,
2607 TG3_GRC_LCLCTL_PWRSW_DELAY);
2608 } else {
2609 u32 no_gpio2;
2610 u32 grc_local_ctrl = 0;
2611
2612 /* Workaround to prevent overdrawing Amps. */
2613 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
2614 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE3;
2615 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2616 grc_local_ctrl,
2617 TG3_GRC_LCLCTL_PWRSW_DELAY);
2618 }
2619
2620 /* On 5753 and variants, GPIO2 cannot be used. */
2621 no_gpio2 = tp->nic_sram_data_cfg &
2622 NIC_SRAM_DATA_CFG_NO_GPIO2;
2623
2624 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE0 |
2625 GRC_LCLCTRL_GPIO_OE1 |
2626 GRC_LCLCTRL_GPIO_OE2 |
2627 GRC_LCLCTRL_GPIO_OUTPUT1 |
2628 GRC_LCLCTRL_GPIO_OUTPUT2;
2629 if (no_gpio2) {
2630 grc_local_ctrl &= ~(GRC_LCLCTRL_GPIO_OE2 |
2631 GRC_LCLCTRL_GPIO_OUTPUT2);
2632 }
2633 tw32_wait_f(GRC_LOCAL_CTRL,
2634 tp->grc_local_ctrl | grc_local_ctrl,
2635 TG3_GRC_LCLCTL_PWRSW_DELAY);
2636
2637 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OUTPUT0;
2638
2639 tw32_wait_f(GRC_LOCAL_CTRL,
2640 tp->grc_local_ctrl | grc_local_ctrl,
2641 TG3_GRC_LCLCTL_PWRSW_DELAY);
2642
2643 if (!no_gpio2) {
2644 grc_local_ctrl &= ~GRC_LCLCTRL_GPIO_OUTPUT2;
2645 tw32_wait_f(GRC_LOCAL_CTRL,
2646 tp->grc_local_ctrl | grc_local_ctrl,
2647 TG3_GRC_LCLCTL_PWRSW_DELAY);
2648 }
2649 }
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002650}
Matt Carlson6f5c8f832011-07-13 09:27:31 +00002651
Matt Carlsoncd0d7222011-07-13 09:27:33 +00002652static void tg3_frob_aux_power_5717(struct tg3 *tp, bool wol_enable)
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002653{
2654 u32 msg = 0;
2655
2656 /* Serialize power state transitions */
2657 if (tg3_ape_lock(tp, TG3_APE_LOCK_GPIO))
2658 return;
2659
Matt Carlsoncd0d7222011-07-13 09:27:33 +00002660 if (tg3_flag(tp, ENABLE_ASF) || tg3_flag(tp, ENABLE_APE) || wol_enable)
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002661 msg = TG3_GPIO_MSG_NEED_VAUX;
2662
2663 msg = tg3_set_function_status(tp, msg);
2664
2665 if (msg & TG3_GPIO_MSG_ALL_DRVR_PRES_MASK)
2666 goto done;
2667
2668 if (msg & TG3_GPIO_MSG_ALL_NEED_VAUX_MASK)
2669 tg3_pwrsrc_switch_to_vaux(tp);
2670 else
2671 tg3_pwrsrc_die_with_vmain(tp);
2672
2673done:
Matt Carlson6f5c8f832011-07-13 09:27:31 +00002674 tg3_ape_unlock(tp, TG3_APE_LOCK_GPIO);
Matt Carlson520b2752011-06-13 13:39:02 +00002675}
2676
Matt Carlsoncd0d7222011-07-13 09:27:33 +00002677static void tg3_frob_aux_power(struct tg3 *tp, bool include_wol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002678{
Matt Carlson683644b2011-03-09 16:58:23 +00002679 bool need_vaux = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680
Matt Carlson334355a2010-01-20 16:58:10 +00002681 /* The GPIOs do something completely different on 57765. */
Joe Perches63c3a662011-04-26 08:12:10 +00002682 if (!tg3_flag(tp, IS_NIC) ||
Matt Carlson334355a2010-01-20 16:58:10 +00002683 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684 return;
2685
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002686 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
2687 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
2688 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
Matt Carlsoncd0d7222011-07-13 09:27:33 +00002689 tg3_frob_aux_power_5717(tp, include_wol ?
2690 tg3_flag(tp, WOL_ENABLE) != 0 : 0);
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002691 return;
2692 }
2693
2694 if (tp->pdev_peer && tp->pdev_peer != tp->pdev) {
Michael Chan8c2dc7e2005-12-19 16:26:02 -08002695 struct net_device *dev_peer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696
Michael Chan8c2dc7e2005-12-19 16:26:02 -08002697 dev_peer = pci_get_drvdata(tp->pdev_peer);
Matt Carlson683644b2011-03-09 16:58:23 +00002698
Michael Chanbc1c7562006-03-20 17:48:03 -08002699 /* remove_one() may have been run on the peer. */
Matt Carlson683644b2011-03-09 16:58:23 +00002700 if (dev_peer) {
2701 struct tg3 *tp_peer = netdev_priv(dev_peer);
2702
Joe Perches63c3a662011-04-26 08:12:10 +00002703 if (tg3_flag(tp_peer, INIT_COMPLETE))
Matt Carlson683644b2011-03-09 16:58:23 +00002704 return;
2705
Matt Carlsoncd0d7222011-07-13 09:27:33 +00002706 if ((include_wol && tg3_flag(tp_peer, WOL_ENABLE)) ||
Joe Perches63c3a662011-04-26 08:12:10 +00002707 tg3_flag(tp_peer, ENABLE_ASF))
Matt Carlson683644b2011-03-09 16:58:23 +00002708 need_vaux = true;
2709 }
Michael Chan8c2dc7e2005-12-19 16:26:02 -08002710 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002711
Matt Carlsoncd0d7222011-07-13 09:27:33 +00002712 if ((include_wol && tg3_flag(tp, WOL_ENABLE)) ||
2713 tg3_flag(tp, ENABLE_ASF))
Matt Carlson683644b2011-03-09 16:58:23 +00002714 need_vaux = true;
2715
Matt Carlson520b2752011-06-13 13:39:02 +00002716 if (need_vaux)
2717 tg3_pwrsrc_switch_to_vaux(tp);
2718 else
2719 tg3_pwrsrc_die_with_vmain(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002720}
2721
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07002722static int tg3_5700_link_polarity(struct tg3 *tp, u32 speed)
2723{
2724 if (tp->led_ctrl == LED_CTRL_MODE_PHY_2)
2725 return 1;
Matt Carlson79eb6902010-02-17 15:17:03 +00002726 else if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5411) {
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07002727 if (speed != SPEED_10)
2728 return 1;
2729 } else if (speed == SPEED_10)
2730 return 1;
2731
2732 return 0;
2733}
2734
Linus Torvalds1da177e2005-04-16 15:20:36 -07002735static int tg3_setup_phy(struct tg3 *, int);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002736static int tg3_halt_cpu(struct tg3 *, u32);
2737
Matt Carlson0a459aa2008-11-03 16:54:15 -08002738static void tg3_power_down_phy(struct tg3 *tp, bool do_low_power)
Michael Chan15c3b692006-03-22 01:06:52 -08002739{
Matt Carlsonce057f02007-11-12 21:08:03 -08002740 u32 val;
2741
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002742 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Michael Chan51297242007-02-13 12:17:57 -08002743 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
2744 u32 sg_dig_ctrl = tr32(SG_DIG_CTRL);
2745 u32 serdes_cfg = tr32(MAC_SERDES_CFG);
2746
2747 sg_dig_ctrl |=
2748 SG_DIG_USING_HW_AUTONEG | SG_DIG_SOFT_RESET;
2749 tw32(SG_DIG_CTRL, sg_dig_ctrl);
2750 tw32(MAC_SERDES_CFG, serdes_cfg | (1 << 15));
2751 }
Michael Chan3f7045c2006-09-27 16:02:29 -07002752 return;
Michael Chan51297242007-02-13 12:17:57 -08002753 }
Michael Chan3f7045c2006-09-27 16:02:29 -07002754
Michael Chan60189dd2006-12-17 17:08:07 -08002755 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan60189dd2006-12-17 17:08:07 -08002756 tg3_bmcr_reset(tp);
2757 val = tr32(GRC_MISC_CFG);
2758 tw32_f(GRC_MISC_CFG, val | GRC_MISC_CFG_EPHY_IDDQ);
2759 udelay(40);
2760 return;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002761 } else if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Matt Carlson0e5f7842009-11-02 14:26:38 +00002762 u32 phytest;
2763 if (!tg3_readphy(tp, MII_TG3_FET_TEST, &phytest)) {
2764 u32 phy;
2765
2766 tg3_writephy(tp, MII_ADVERTISE, 0);
2767 tg3_writephy(tp, MII_BMCR,
2768 BMCR_ANENABLE | BMCR_ANRESTART);
2769
2770 tg3_writephy(tp, MII_TG3_FET_TEST,
2771 phytest | MII_TG3_FET_SHADOW_EN);
2772 if (!tg3_readphy(tp, MII_TG3_FET_SHDW_AUXMODE4, &phy)) {
2773 phy |= MII_TG3_FET_SHDW_AUXMODE4_SBPD;
2774 tg3_writephy(tp,
2775 MII_TG3_FET_SHDW_AUXMODE4,
2776 phy);
2777 }
2778 tg3_writephy(tp, MII_TG3_FET_TEST, phytest);
2779 }
2780 return;
Matt Carlson0a459aa2008-11-03 16:54:15 -08002781 } else if (do_low_power) {
Michael Chan715116a2006-09-27 16:09:25 -07002782 tg3_writephy(tp, MII_TG3_EXT_CTRL,
2783 MII_TG3_EXT_CTRL_FORCE_LED_OFF);
Matt Carlson0a459aa2008-11-03 16:54:15 -08002784
Matt Carlsonb4bd2922011-04-20 07:57:41 +00002785 val = MII_TG3_AUXCTL_PCTL_100TX_LPWR |
2786 MII_TG3_AUXCTL_PCTL_SPR_ISOLATE |
2787 MII_TG3_AUXCTL_PCTL_VREG_11V;
2788 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_PWRCTL, val);
Michael Chan715116a2006-09-27 16:09:25 -07002789 }
Michael Chan3f7045c2006-09-27 16:02:29 -07002790
Michael Chan15c3b692006-03-22 01:06:52 -08002791 /* The PHY should not be powered down on some chips because
2792 * of bugs.
2793 */
2794 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2795 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
2796 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780 &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002797 (tp->phy_flags & TG3_PHYFLG_MII_SERDES)))
Michael Chan15c3b692006-03-22 01:06:52 -08002798 return;
Matt Carlsonce057f02007-11-12 21:08:03 -08002799
Matt Carlsonbcb37f62008-11-03 16:52:09 -08002800 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX ||
2801 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5761_AX) {
Matt Carlsonce057f02007-11-12 21:08:03 -08002802 val = tr32(TG3_CPMU_LSPD_1000MB_CLK);
2803 val &= ~CPMU_LSPD_1000MB_MACCLK_MASK;
2804 val |= CPMU_LSPD_1000MB_MACCLK_12_5;
2805 tw32_f(TG3_CPMU_LSPD_1000MB_CLK, val);
2806 }
2807
Michael Chan15c3b692006-03-22 01:06:52 -08002808 tg3_writephy(tp, MII_BMCR, BMCR_PDOWN);
2809}
2810
Matt Carlson3f007892008-11-03 16:51:36 -08002811/* tp->lock is held. */
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002812static int tg3_nvram_lock(struct tg3 *tp)
2813{
Joe Perches63c3a662011-04-26 08:12:10 +00002814 if (tg3_flag(tp, NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002815 int i;
2816
2817 if (tp->nvram_lock_cnt == 0) {
2818 tw32(NVRAM_SWARB, SWARB_REQ_SET1);
2819 for (i = 0; i < 8000; i++) {
2820 if (tr32(NVRAM_SWARB) & SWARB_GNT1)
2821 break;
2822 udelay(20);
2823 }
2824 if (i == 8000) {
2825 tw32(NVRAM_SWARB, SWARB_REQ_CLR1);
2826 return -ENODEV;
2827 }
2828 }
2829 tp->nvram_lock_cnt++;
2830 }
2831 return 0;
2832}
2833
2834/* tp->lock is held. */
2835static void tg3_nvram_unlock(struct tg3 *tp)
2836{
Joe Perches63c3a662011-04-26 08:12:10 +00002837 if (tg3_flag(tp, NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002838 if (tp->nvram_lock_cnt > 0)
2839 tp->nvram_lock_cnt--;
2840 if (tp->nvram_lock_cnt == 0)
2841 tw32_f(NVRAM_SWARB, SWARB_REQ_CLR1);
2842 }
2843}
2844
2845/* tp->lock is held. */
2846static void tg3_enable_nvram_access(struct tg3 *tp)
2847{
Joe Perches63c3a662011-04-26 08:12:10 +00002848 if (tg3_flag(tp, 5750_PLUS) && !tg3_flag(tp, PROTECTED_NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002849 u32 nvaccess = tr32(NVRAM_ACCESS);
2850
2851 tw32(NVRAM_ACCESS, nvaccess | ACCESS_ENABLE);
2852 }
2853}
2854
2855/* tp->lock is held. */
2856static void tg3_disable_nvram_access(struct tg3 *tp)
2857{
Joe Perches63c3a662011-04-26 08:12:10 +00002858 if (tg3_flag(tp, 5750_PLUS) && !tg3_flag(tp, PROTECTED_NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002859 u32 nvaccess = tr32(NVRAM_ACCESS);
2860
2861 tw32(NVRAM_ACCESS, nvaccess & ~ACCESS_ENABLE);
2862 }
2863}
2864
2865static int tg3_nvram_read_using_eeprom(struct tg3 *tp,
2866 u32 offset, u32 *val)
2867{
2868 u32 tmp;
2869 int i;
2870
2871 if (offset > EEPROM_ADDR_ADDR_MASK || (offset % 4) != 0)
2872 return -EINVAL;
2873
2874 tmp = tr32(GRC_EEPROM_ADDR) & ~(EEPROM_ADDR_ADDR_MASK |
2875 EEPROM_ADDR_DEVID_MASK |
2876 EEPROM_ADDR_READ);
2877 tw32(GRC_EEPROM_ADDR,
2878 tmp |
2879 (0 << EEPROM_ADDR_DEVID_SHIFT) |
2880 ((offset << EEPROM_ADDR_ADDR_SHIFT) &
2881 EEPROM_ADDR_ADDR_MASK) |
2882 EEPROM_ADDR_READ | EEPROM_ADDR_START);
2883
2884 for (i = 0; i < 1000; i++) {
2885 tmp = tr32(GRC_EEPROM_ADDR);
2886
2887 if (tmp & EEPROM_ADDR_COMPLETE)
2888 break;
2889 msleep(1);
2890 }
2891 if (!(tmp & EEPROM_ADDR_COMPLETE))
2892 return -EBUSY;
2893
Matt Carlson62cedd12009-04-20 14:52:29 -07002894 tmp = tr32(GRC_EEPROM_DATA);
2895
2896 /*
2897 * The data will always be opposite the native endian
2898 * format. Perform a blind byteswap to compensate.
2899 */
2900 *val = swab32(tmp);
2901
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002902 return 0;
2903}
2904
2905#define NVRAM_CMD_TIMEOUT 10000
2906
2907static int tg3_nvram_exec_cmd(struct tg3 *tp, u32 nvram_cmd)
2908{
2909 int i;
2910
2911 tw32(NVRAM_CMD, nvram_cmd);
2912 for (i = 0; i < NVRAM_CMD_TIMEOUT; i++) {
2913 udelay(10);
2914 if (tr32(NVRAM_CMD) & NVRAM_CMD_DONE) {
2915 udelay(10);
2916 break;
2917 }
2918 }
2919
2920 if (i == NVRAM_CMD_TIMEOUT)
2921 return -EBUSY;
2922
2923 return 0;
2924}
2925
2926static u32 tg3_nvram_phys_addr(struct tg3 *tp, u32 addr)
2927{
Joe Perches63c3a662011-04-26 08:12:10 +00002928 if (tg3_flag(tp, NVRAM) &&
2929 tg3_flag(tp, NVRAM_BUFFERED) &&
2930 tg3_flag(tp, FLASH) &&
2931 !tg3_flag(tp, NO_NVRAM_ADDR_TRANS) &&
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002932 (tp->nvram_jedecnum == JEDEC_ATMEL))
2933
2934 addr = ((addr / tp->nvram_pagesize) <<
2935 ATMEL_AT45DB0X1B_PAGE_POS) +
2936 (addr % tp->nvram_pagesize);
2937
2938 return addr;
2939}
2940
2941static u32 tg3_nvram_logical_addr(struct tg3 *tp, u32 addr)
2942{
Joe Perches63c3a662011-04-26 08:12:10 +00002943 if (tg3_flag(tp, NVRAM) &&
2944 tg3_flag(tp, NVRAM_BUFFERED) &&
2945 tg3_flag(tp, FLASH) &&
2946 !tg3_flag(tp, NO_NVRAM_ADDR_TRANS) &&
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002947 (tp->nvram_jedecnum == JEDEC_ATMEL))
2948
2949 addr = ((addr >> ATMEL_AT45DB0X1B_PAGE_POS) *
2950 tp->nvram_pagesize) +
2951 (addr & ((1 << ATMEL_AT45DB0X1B_PAGE_POS) - 1));
2952
2953 return addr;
2954}
2955
Matt Carlsone4f34112009-02-25 14:25:00 +00002956/* NOTE: Data read in from NVRAM is byteswapped according to
2957 * the byteswapping settings for all other register accesses.
2958 * tg3 devices are BE devices, so on a BE machine, the data
2959 * returned will be exactly as it is seen in NVRAM. On a LE
2960 * machine, the 32-bit value will be byteswapped.
2961 */
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002962static int tg3_nvram_read(struct tg3 *tp, u32 offset, u32 *val)
2963{
2964 int ret;
2965
Joe Perches63c3a662011-04-26 08:12:10 +00002966 if (!tg3_flag(tp, NVRAM))
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002967 return tg3_nvram_read_using_eeprom(tp, offset, val);
2968
2969 offset = tg3_nvram_phys_addr(tp, offset);
2970
2971 if (offset > NVRAM_ADDR_MSK)
2972 return -EINVAL;
2973
2974 ret = tg3_nvram_lock(tp);
2975 if (ret)
2976 return ret;
2977
2978 tg3_enable_nvram_access(tp);
2979
2980 tw32(NVRAM_ADDR, offset);
2981 ret = tg3_nvram_exec_cmd(tp, NVRAM_CMD_RD | NVRAM_CMD_GO |
2982 NVRAM_CMD_FIRST | NVRAM_CMD_LAST | NVRAM_CMD_DONE);
2983
2984 if (ret == 0)
Matt Carlsone4f34112009-02-25 14:25:00 +00002985 *val = tr32(NVRAM_RDDATA);
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002986
2987 tg3_disable_nvram_access(tp);
2988
2989 tg3_nvram_unlock(tp);
2990
2991 return ret;
2992}
2993
Matt Carlsona9dc5292009-02-25 14:25:30 +00002994/* Ensures NVRAM data is in bytestream format. */
2995static int tg3_nvram_read_be32(struct tg3 *tp, u32 offset, __be32 *val)
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002996{
2997 u32 v;
Matt Carlsona9dc5292009-02-25 14:25:30 +00002998 int res = tg3_nvram_read(tp, offset, &v);
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002999 if (!res)
Matt Carlsona9dc5292009-02-25 14:25:30 +00003000 *val = cpu_to_be32(v);
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003001 return res;
3002}
3003
Matt Carlson997b4f12011-08-31 11:44:53 +00003004#define RX_CPU_SCRATCH_BASE 0x30000
3005#define RX_CPU_SCRATCH_SIZE 0x04000
3006#define TX_CPU_SCRATCH_BASE 0x34000
3007#define TX_CPU_SCRATCH_SIZE 0x04000
3008
3009/* tp->lock is held. */
3010static int tg3_halt_cpu(struct tg3 *tp, u32 offset)
3011{
3012 int i;
3013
3014 BUG_ON(offset == TX_CPU_BASE && tg3_flag(tp, 5705_PLUS));
3015
3016 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
3017 u32 val = tr32(GRC_VCPU_EXT_CTRL);
3018
3019 tw32(GRC_VCPU_EXT_CTRL, val | GRC_VCPU_EXT_CTRL_HALT_CPU);
3020 return 0;
3021 }
3022 if (offset == RX_CPU_BASE) {
3023 for (i = 0; i < 10000; i++) {
3024 tw32(offset + CPU_STATE, 0xffffffff);
3025 tw32(offset + CPU_MODE, CPU_MODE_HALT);
3026 if (tr32(offset + CPU_MODE) & CPU_MODE_HALT)
3027 break;
3028 }
3029
3030 tw32(offset + CPU_STATE, 0xffffffff);
3031 tw32_f(offset + CPU_MODE, CPU_MODE_HALT);
3032 udelay(10);
3033 } else {
3034 for (i = 0; i < 10000; i++) {
3035 tw32(offset + CPU_STATE, 0xffffffff);
3036 tw32(offset + CPU_MODE, CPU_MODE_HALT);
3037 if (tr32(offset + CPU_MODE) & CPU_MODE_HALT)
3038 break;
3039 }
3040 }
3041
3042 if (i >= 10000) {
3043 netdev_err(tp->dev, "%s timed out, %s CPU\n",
3044 __func__, offset == RX_CPU_BASE ? "RX" : "TX");
3045 return -ENODEV;
3046 }
3047
3048 /* Clear firmware's nvram arbitration. */
3049 if (tg3_flag(tp, NVRAM))
3050 tw32(NVRAM_SWARB, SWARB_REQ_CLR0);
3051 return 0;
3052}
3053
3054struct fw_info {
3055 unsigned int fw_base;
3056 unsigned int fw_len;
3057 const __be32 *fw_data;
3058};
3059
3060/* tp->lock is held. */
3061static int tg3_load_firmware_cpu(struct tg3 *tp, u32 cpu_base,
3062 u32 cpu_scratch_base, int cpu_scratch_size,
3063 struct fw_info *info)
3064{
3065 int err, lock_err, i;
3066 void (*write_op)(struct tg3 *, u32, u32);
3067
3068 if (cpu_base == TX_CPU_BASE && tg3_flag(tp, 5705_PLUS)) {
3069 netdev_err(tp->dev,
3070 "%s: Trying to load TX cpu firmware which is 5705\n",
3071 __func__);
3072 return -EINVAL;
3073 }
3074
3075 if (tg3_flag(tp, 5705_PLUS))
3076 write_op = tg3_write_mem;
3077 else
3078 write_op = tg3_write_indirect_reg32;
3079
3080 /* It is possible that bootcode is still loading at this point.
3081 * Get the nvram lock first before halting the cpu.
3082 */
3083 lock_err = tg3_nvram_lock(tp);
3084 err = tg3_halt_cpu(tp, cpu_base);
3085 if (!lock_err)
3086 tg3_nvram_unlock(tp);
3087 if (err)
3088 goto out;
3089
3090 for (i = 0; i < cpu_scratch_size; i += sizeof(u32))
3091 write_op(tp, cpu_scratch_base + i, 0);
3092 tw32(cpu_base + CPU_STATE, 0xffffffff);
3093 tw32(cpu_base + CPU_MODE, tr32(cpu_base+CPU_MODE)|CPU_MODE_HALT);
3094 for (i = 0; i < (info->fw_len / sizeof(u32)); i++)
3095 write_op(tp, (cpu_scratch_base +
3096 (info->fw_base & 0xffff) +
3097 (i * sizeof(u32))),
3098 be32_to_cpu(info->fw_data[i]));
3099
3100 err = 0;
3101
3102out:
3103 return err;
3104}
3105
3106/* tp->lock is held. */
3107static int tg3_load_5701_a0_firmware_fix(struct tg3 *tp)
3108{
3109 struct fw_info info;
3110 const __be32 *fw_data;
3111 int err, i;
3112
3113 fw_data = (void *)tp->fw->data;
3114
3115 /* Firmware blob starts with version numbers, followed by
3116 start address and length. We are setting complete length.
3117 length = end_address_of_bss - start_address_of_text.
3118 Remainder is the blob to be loaded contiguously
3119 from start address. */
3120
3121 info.fw_base = be32_to_cpu(fw_data[1]);
3122 info.fw_len = tp->fw->size - 12;
3123 info.fw_data = &fw_data[3];
3124
3125 err = tg3_load_firmware_cpu(tp, RX_CPU_BASE,
3126 RX_CPU_SCRATCH_BASE, RX_CPU_SCRATCH_SIZE,
3127 &info);
3128 if (err)
3129 return err;
3130
3131 err = tg3_load_firmware_cpu(tp, TX_CPU_BASE,
3132 TX_CPU_SCRATCH_BASE, TX_CPU_SCRATCH_SIZE,
3133 &info);
3134 if (err)
3135 return err;
3136
3137 /* Now startup only the RX cpu. */
3138 tw32(RX_CPU_BASE + CPU_STATE, 0xffffffff);
3139 tw32_f(RX_CPU_BASE + CPU_PC, info.fw_base);
3140
3141 for (i = 0; i < 5; i++) {
3142 if (tr32(RX_CPU_BASE + CPU_PC) == info.fw_base)
3143 break;
3144 tw32(RX_CPU_BASE + CPU_STATE, 0xffffffff);
3145 tw32(RX_CPU_BASE + CPU_MODE, CPU_MODE_HALT);
3146 tw32_f(RX_CPU_BASE + CPU_PC, info.fw_base);
3147 udelay(1000);
3148 }
3149 if (i >= 5) {
3150 netdev_err(tp->dev, "%s fails to set RX CPU PC, is %08x "
3151 "should be %08x\n", __func__,
3152 tr32(RX_CPU_BASE + CPU_PC), info.fw_base);
3153 return -ENODEV;
3154 }
3155 tw32(RX_CPU_BASE + CPU_STATE, 0xffffffff);
3156 tw32_f(RX_CPU_BASE + CPU_MODE, 0x00000000);
3157
3158 return 0;
3159}
3160
3161/* tp->lock is held. */
3162static int tg3_load_tso_firmware(struct tg3 *tp)
3163{
3164 struct fw_info info;
3165 const __be32 *fw_data;
3166 unsigned long cpu_base, cpu_scratch_base, cpu_scratch_size;
3167 int err, i;
3168
3169 if (tg3_flag(tp, HW_TSO_1) ||
3170 tg3_flag(tp, HW_TSO_2) ||
3171 tg3_flag(tp, HW_TSO_3))
3172 return 0;
3173
3174 fw_data = (void *)tp->fw->data;
3175
3176 /* Firmware blob starts with version numbers, followed by
3177 start address and length. We are setting complete length.
3178 length = end_address_of_bss - start_address_of_text.
3179 Remainder is the blob to be loaded contiguously
3180 from start address. */
3181
3182 info.fw_base = be32_to_cpu(fw_data[1]);
3183 cpu_scratch_size = tp->fw_len;
3184 info.fw_len = tp->fw->size - 12;
3185 info.fw_data = &fw_data[3];
3186
3187 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
3188 cpu_base = RX_CPU_BASE;
3189 cpu_scratch_base = NIC_SRAM_MBUF_POOL_BASE5705;
3190 } else {
3191 cpu_base = TX_CPU_BASE;
3192 cpu_scratch_base = TX_CPU_SCRATCH_BASE;
3193 cpu_scratch_size = TX_CPU_SCRATCH_SIZE;
3194 }
3195
3196 err = tg3_load_firmware_cpu(tp, cpu_base,
3197 cpu_scratch_base, cpu_scratch_size,
3198 &info);
3199 if (err)
3200 return err;
3201
3202 /* Now startup the cpu. */
3203 tw32(cpu_base + CPU_STATE, 0xffffffff);
3204 tw32_f(cpu_base + CPU_PC, info.fw_base);
3205
3206 for (i = 0; i < 5; i++) {
3207 if (tr32(cpu_base + CPU_PC) == info.fw_base)
3208 break;
3209 tw32(cpu_base + CPU_STATE, 0xffffffff);
3210 tw32(cpu_base + CPU_MODE, CPU_MODE_HALT);
3211 tw32_f(cpu_base + CPU_PC, info.fw_base);
3212 udelay(1000);
3213 }
3214 if (i >= 5) {
3215 netdev_err(tp->dev,
3216 "%s fails to set CPU PC, is %08x should be %08x\n",
3217 __func__, tr32(cpu_base + CPU_PC), info.fw_base);
3218 return -ENODEV;
3219 }
3220 tw32(cpu_base + CPU_STATE, 0xffffffff);
3221 tw32_f(cpu_base + CPU_MODE, 0x00000000);
3222 return 0;
3223}
3224
3225
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003226/* tp->lock is held. */
Matt Carlson3f007892008-11-03 16:51:36 -08003227static void __tg3_set_mac_addr(struct tg3 *tp, int skip_mac_1)
3228{
3229 u32 addr_high, addr_low;
3230 int i;
3231
3232 addr_high = ((tp->dev->dev_addr[0] << 8) |
3233 tp->dev->dev_addr[1]);
3234 addr_low = ((tp->dev->dev_addr[2] << 24) |
3235 (tp->dev->dev_addr[3] << 16) |
3236 (tp->dev->dev_addr[4] << 8) |
3237 (tp->dev->dev_addr[5] << 0));
3238 for (i = 0; i < 4; i++) {
3239 if (i == 1 && skip_mac_1)
3240 continue;
3241 tw32(MAC_ADDR_0_HIGH + (i * 8), addr_high);
3242 tw32(MAC_ADDR_0_LOW + (i * 8), addr_low);
3243 }
3244
3245 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
3246 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
3247 for (i = 0; i < 12; i++) {
3248 tw32(MAC_EXTADDR_0_HIGH + (i * 8), addr_high);
3249 tw32(MAC_EXTADDR_0_LOW + (i * 8), addr_low);
3250 }
3251 }
3252
3253 addr_high = (tp->dev->dev_addr[0] +
3254 tp->dev->dev_addr[1] +
3255 tp->dev->dev_addr[2] +
3256 tp->dev->dev_addr[3] +
3257 tp->dev->dev_addr[4] +
3258 tp->dev->dev_addr[5]) &
3259 TX_BACKOFF_SEED_MASK;
3260 tw32(MAC_TX_BACKOFF_SEED, addr_high);
3261}
3262
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003263static void tg3_enable_register_access(struct tg3 *tp)
3264{
3265 /*
3266 * Make sure register accesses (indirect or otherwise) will function
3267 * correctly.
3268 */
3269 pci_write_config_dword(tp->pdev,
3270 TG3PCI_MISC_HOST_CTRL, tp->misc_host_ctrl);
3271}
3272
3273static int tg3_power_up(struct tg3 *tp)
3274{
Matt Carlsonbed98292011-07-13 09:27:29 +00003275 int err;
3276
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003277 tg3_enable_register_access(tp);
3278
Matt Carlsonbed98292011-07-13 09:27:29 +00003279 err = pci_set_power_state(tp->pdev, PCI_D0);
3280 if (!err) {
3281 /* Switch out of Vaux if it is a NIC */
3282 tg3_pwrsrc_switch_to_vmain(tp);
3283 } else {
3284 netdev_err(tp->dev, "Transition to D0 failed\n");
3285 }
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003286
Matt Carlsonbed98292011-07-13 09:27:29 +00003287 return err;
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003288}
3289
3290static int tg3_power_down_prepare(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003291{
3292 u32 misc_host_ctrl;
Matt Carlson0a459aa2008-11-03 16:54:15 -08003293 bool device_should_wake, do_low_power;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003294
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003295 tg3_enable_register_access(tp);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08003296
3297 /* Restore the CLKREQ setting. */
Joe Perches63c3a662011-04-26 08:12:10 +00003298 if (tg3_flag(tp, CLKREQ_BUG)) {
Matt Carlson5e7dfd02008-11-21 17:18:16 -08003299 u16 lnkctl;
3300
3301 pci_read_config_word(tp->pdev,
Jon Mason708ebb3a2011-06-27 12:56:50 +00003302 pci_pcie_cap(tp->pdev) + PCI_EXP_LNKCTL,
Matt Carlson5e7dfd02008-11-21 17:18:16 -08003303 &lnkctl);
3304 lnkctl |= PCI_EXP_LNKCTL_CLKREQ_EN;
3305 pci_write_config_word(tp->pdev,
Jon Mason708ebb3a2011-06-27 12:56:50 +00003306 pci_pcie_cap(tp->pdev) + PCI_EXP_LNKCTL,
Matt Carlson5e7dfd02008-11-21 17:18:16 -08003307 lnkctl);
3308 }
3309
Linus Torvalds1da177e2005-04-16 15:20:36 -07003310 misc_host_ctrl = tr32(TG3PCI_MISC_HOST_CTRL);
3311 tw32(TG3PCI_MISC_HOST_CTRL,
3312 misc_host_ctrl | MISC_HOST_CTRL_MASK_PCI_INT);
3313
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003314 device_should_wake = device_may_wakeup(&tp->pdev->dev) &&
Joe Perches63c3a662011-04-26 08:12:10 +00003315 tg3_flag(tp, WOL_ENABLE);
Matt Carlson05ac4cb2008-11-03 16:53:46 -08003316
Joe Perches63c3a662011-04-26 08:12:10 +00003317 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson0a459aa2008-11-03 16:54:15 -08003318 do_low_power = false;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003319 if ((tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) &&
Matt Carlson80096062010-08-02 11:26:06 +00003320 !(tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07003321 struct phy_device *phydev;
Matt Carlson0a459aa2008-11-03 16:54:15 -08003322 u32 phyid, advertising;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07003323
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00003324 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07003325
Matt Carlson80096062010-08-02 11:26:06 +00003326 tp->phy_flags |= TG3_PHYFLG_IS_LOW_POWER;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07003327
3328 tp->link_config.orig_speed = phydev->speed;
3329 tp->link_config.orig_duplex = phydev->duplex;
3330 tp->link_config.orig_autoneg = phydev->autoneg;
3331 tp->link_config.orig_advertising = phydev->advertising;
3332
3333 advertising = ADVERTISED_TP |
3334 ADVERTISED_Pause |
3335 ADVERTISED_Autoneg |
3336 ADVERTISED_10baseT_Half;
3337
Joe Perches63c3a662011-04-26 08:12:10 +00003338 if (tg3_flag(tp, ENABLE_ASF) || device_should_wake) {
3339 if (tg3_flag(tp, WOL_SPEED_100MB))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07003340 advertising |=
3341 ADVERTISED_100baseT_Half |
3342 ADVERTISED_100baseT_Full |
3343 ADVERTISED_10baseT_Full;
3344 else
3345 advertising |= ADVERTISED_10baseT_Full;
3346 }
3347
3348 phydev->advertising = advertising;
3349
3350 phy_start_aneg(phydev);
Matt Carlson0a459aa2008-11-03 16:54:15 -08003351
3352 phyid = phydev->drv->phy_id & phydev->drv->phy_id_mask;
Matt Carlson6a443a02010-02-17 15:17:04 +00003353 if (phyid != PHY_ID_BCMAC131) {
3354 phyid &= PHY_BCM_OUI_MASK;
3355 if (phyid == PHY_BCM_OUI_1 ||
3356 phyid == PHY_BCM_OUI_2 ||
3357 phyid == PHY_BCM_OUI_3)
Matt Carlson0a459aa2008-11-03 16:54:15 -08003358 do_low_power = true;
3359 }
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07003360 }
Matt Carlsondd477002008-05-25 23:45:58 -07003361 } else {
Matt Carlson20232762008-12-21 20:18:56 -08003362 do_low_power = true;
Matt Carlson0a459aa2008-11-03 16:54:15 -08003363
Matt Carlson80096062010-08-02 11:26:06 +00003364 if (!(tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)) {
3365 tp->phy_flags |= TG3_PHYFLG_IS_LOW_POWER;
Matt Carlsondd477002008-05-25 23:45:58 -07003366 tp->link_config.orig_speed = tp->link_config.speed;
3367 tp->link_config.orig_duplex = tp->link_config.duplex;
3368 tp->link_config.orig_autoneg = tp->link_config.autoneg;
3369 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003370
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003371 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES)) {
Matt Carlsondd477002008-05-25 23:45:58 -07003372 tp->link_config.speed = SPEED_10;
3373 tp->link_config.duplex = DUPLEX_HALF;
3374 tp->link_config.autoneg = AUTONEG_ENABLE;
3375 tg3_setup_phy(tp, 0);
3376 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003377 }
3378
Michael Chanb5d37722006-09-27 16:06:21 -07003379 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
3380 u32 val;
3381
3382 val = tr32(GRC_VCPU_EXT_CTRL);
3383 tw32(GRC_VCPU_EXT_CTRL, val | GRC_VCPU_EXT_CTRL_DISABLE_WOL);
Joe Perches63c3a662011-04-26 08:12:10 +00003384 } else if (!tg3_flag(tp, ENABLE_ASF)) {
Michael Chan6921d202005-12-13 21:15:53 -08003385 int i;
3386 u32 val;
3387
3388 for (i = 0; i < 200; i++) {
3389 tg3_read_mem(tp, NIC_SRAM_FW_ASF_STATUS_MBOX, &val);
3390 if (val == ~NIC_SRAM_FIRMWARE_MBOX_MAGIC1)
3391 break;
3392 msleep(1);
3393 }
3394 }
Joe Perches63c3a662011-04-26 08:12:10 +00003395 if (tg3_flag(tp, WOL_CAP))
Gary Zambranoa85feb82007-05-05 11:52:19 -07003396 tg3_write_mem(tp, NIC_SRAM_WOL_MBOX, WOL_SIGNATURE |
3397 WOL_DRV_STATE_SHUTDOWN |
3398 WOL_DRV_WOL |
3399 WOL_SET_MAGIC_PKT);
Michael Chan6921d202005-12-13 21:15:53 -08003400
Matt Carlson05ac4cb2008-11-03 16:53:46 -08003401 if (device_should_wake) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003402 u32 mac_mode;
3403
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003404 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES)) {
Matt Carlsonb4bd2922011-04-20 07:57:41 +00003405 if (do_low_power &&
3406 !(tp->phy_flags & TG3_PHYFLG_IS_FET)) {
3407 tg3_phy_auxctl_write(tp,
3408 MII_TG3_AUXCTL_SHDWSEL_PWRCTL,
3409 MII_TG3_AUXCTL_PCTL_WOL_EN |
3410 MII_TG3_AUXCTL_PCTL_100TX_LPWR |
3411 MII_TG3_AUXCTL_PCTL_CL_AB_TXDAC);
Matt Carlsondd477002008-05-25 23:45:58 -07003412 udelay(40);
3413 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003414
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003415 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
Michael Chan3f7045c2006-09-27 16:02:29 -07003416 mac_mode = MAC_MODE_PORT_MODE_GMII;
3417 else
3418 mac_mode = MAC_MODE_PORT_MODE_MII;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003419
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07003420 mac_mode |= tp->mac_mode & MAC_MODE_LINK_POLARITY;
3421 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
3422 ASIC_REV_5700) {
Joe Perches63c3a662011-04-26 08:12:10 +00003423 u32 speed = tg3_flag(tp, WOL_SPEED_100MB) ?
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07003424 SPEED_100 : SPEED_10;
3425 if (tg3_5700_link_polarity(tp, speed))
3426 mac_mode |= MAC_MODE_LINK_POLARITY;
3427 else
3428 mac_mode &= ~MAC_MODE_LINK_POLARITY;
3429 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003430 } else {
3431 mac_mode = MAC_MODE_PORT_MODE_TBI;
3432 }
3433
Joe Perches63c3a662011-04-26 08:12:10 +00003434 if (!tg3_flag(tp, 5750_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003435 tw32(MAC_LED_CTRL, tp->led_ctrl);
3436
Matt Carlson05ac4cb2008-11-03 16:53:46 -08003437 mac_mode |= MAC_MODE_MAGIC_PKT_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00003438 if ((tg3_flag(tp, 5705_PLUS) && !tg3_flag(tp, 5780_CLASS)) &&
3439 (tg3_flag(tp, ENABLE_ASF) || tg3_flag(tp, ENABLE_APE)))
Matt Carlson05ac4cb2008-11-03 16:53:46 -08003440 mac_mode |= MAC_MODE_KEEP_FRAME_IN_WOL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003441
Joe Perches63c3a662011-04-26 08:12:10 +00003442 if (tg3_flag(tp, ENABLE_APE))
Matt Carlsond2394e6b2010-11-24 08:31:47 +00003443 mac_mode |= MAC_MODE_APE_TX_EN |
3444 MAC_MODE_APE_RX_EN |
3445 MAC_MODE_TDE_ENABLE;
Matt Carlson3bda1252008-08-15 14:08:22 -07003446
Linus Torvalds1da177e2005-04-16 15:20:36 -07003447 tw32_f(MAC_MODE, mac_mode);
3448 udelay(100);
3449
3450 tw32_f(MAC_RX_MODE, RX_MODE_ENABLE);
3451 udelay(10);
3452 }
3453
Joe Perches63c3a662011-04-26 08:12:10 +00003454 if (!tg3_flag(tp, WOL_SPEED_100MB) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003455 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
3456 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)) {
3457 u32 base_val;
3458
3459 base_val = tp->pci_clock_ctrl;
3460 base_val |= (CLOCK_CTRL_RXCLK_DISABLE |
3461 CLOCK_CTRL_TXCLK_DISABLE);
3462
Michael Chanb401e9e2005-12-19 16:27:04 -08003463 tw32_wait_f(TG3PCI_CLOCK_CTRL, base_val | CLOCK_CTRL_ALTCLK |
3464 CLOCK_CTRL_PWRDOWN_PLL133, 40);
Joe Perches63c3a662011-04-26 08:12:10 +00003465 } else if (tg3_flag(tp, 5780_CLASS) ||
3466 tg3_flag(tp, CPMU_PRESENT) ||
Matt Carlson6ff6f812011-05-19 12:12:54 +00003467 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan4cf78e42005-07-25 12:29:19 -07003468 /* do nothing */
Joe Perches63c3a662011-04-26 08:12:10 +00003469 } else if (!(tg3_flag(tp, 5750_PLUS) && tg3_flag(tp, ENABLE_ASF))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003470 u32 newbits1, newbits2;
3471
3472 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
3473 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
3474 newbits1 = (CLOCK_CTRL_RXCLK_DISABLE |
3475 CLOCK_CTRL_TXCLK_DISABLE |
3476 CLOCK_CTRL_ALTCLK);
3477 newbits2 = newbits1 | CLOCK_CTRL_44MHZ_CORE;
Joe Perches63c3a662011-04-26 08:12:10 +00003478 } else if (tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003479 newbits1 = CLOCK_CTRL_625_CORE;
3480 newbits2 = newbits1 | CLOCK_CTRL_ALTCLK;
3481 } else {
3482 newbits1 = CLOCK_CTRL_ALTCLK;
3483 newbits2 = newbits1 | CLOCK_CTRL_44MHZ_CORE;
3484 }
3485
Michael Chanb401e9e2005-12-19 16:27:04 -08003486 tw32_wait_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl | newbits1,
3487 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003488
Michael Chanb401e9e2005-12-19 16:27:04 -08003489 tw32_wait_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl | newbits2,
3490 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003491
Joe Perches63c3a662011-04-26 08:12:10 +00003492 if (!tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003493 u32 newbits3;
3494
3495 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
3496 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
3497 newbits3 = (CLOCK_CTRL_RXCLK_DISABLE |
3498 CLOCK_CTRL_TXCLK_DISABLE |
3499 CLOCK_CTRL_44MHZ_CORE);
3500 } else {
3501 newbits3 = CLOCK_CTRL_44MHZ_CORE;
3502 }
3503
Michael Chanb401e9e2005-12-19 16:27:04 -08003504 tw32_wait_f(TG3PCI_CLOCK_CTRL,
3505 tp->pci_clock_ctrl | newbits3, 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003506 }
3507 }
3508
Joe Perches63c3a662011-04-26 08:12:10 +00003509 if (!(device_should_wake) && !tg3_flag(tp, ENABLE_ASF))
Matt Carlson0a459aa2008-11-03 16:54:15 -08003510 tg3_power_down_phy(tp, do_low_power);
Michael Chan6921d202005-12-13 21:15:53 -08003511
Matt Carlsoncd0d7222011-07-13 09:27:33 +00003512 tg3_frob_aux_power(tp, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003513
3514 /* Workaround for unstable PLL clock */
3515 if ((GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_AX) ||
3516 (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_BX)) {
3517 u32 val = tr32(0x7d00);
3518
3519 val &= ~((1 << 16) | (1 << 4) | (1 << 2) | (1 << 1) | 1);
3520 tw32(0x7d00, val);
Joe Perches63c3a662011-04-26 08:12:10 +00003521 if (!tg3_flag(tp, ENABLE_ASF)) {
Michael Chanec41c7d2006-01-17 02:40:55 -08003522 int err;
3523
3524 err = tg3_nvram_lock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003525 tg3_halt_cpu(tp, RX_CPU_BASE);
Michael Chanec41c7d2006-01-17 02:40:55 -08003526 if (!err)
3527 tg3_nvram_unlock(tp);
Michael Chan6921d202005-12-13 21:15:53 -08003528 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003529 }
3530
Michael Chanbbadf502006-04-06 21:46:34 -07003531 tg3_write_sig_post_reset(tp, RESET_KIND_SHUTDOWN);
3532
Linus Torvalds1da177e2005-04-16 15:20:36 -07003533 return 0;
3534}
3535
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003536static void tg3_power_down(struct tg3 *tp)
3537{
3538 tg3_power_down_prepare(tp);
3539
Joe Perches63c3a662011-04-26 08:12:10 +00003540 pci_wake_from_d3(tp->pdev, tg3_flag(tp, WOL_ENABLE));
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003541 pci_set_power_state(tp->pdev, PCI_D3hot);
3542}
3543
Linus Torvalds1da177e2005-04-16 15:20:36 -07003544static void tg3_aux_stat_to_speed_duplex(struct tg3 *tp, u32 val, u16 *speed, u8 *duplex)
3545{
3546 switch (val & MII_TG3_AUX_STAT_SPDMASK) {
3547 case MII_TG3_AUX_STAT_10HALF:
3548 *speed = SPEED_10;
3549 *duplex = DUPLEX_HALF;
3550 break;
3551
3552 case MII_TG3_AUX_STAT_10FULL:
3553 *speed = SPEED_10;
3554 *duplex = DUPLEX_FULL;
3555 break;
3556
3557 case MII_TG3_AUX_STAT_100HALF:
3558 *speed = SPEED_100;
3559 *duplex = DUPLEX_HALF;
3560 break;
3561
3562 case MII_TG3_AUX_STAT_100FULL:
3563 *speed = SPEED_100;
3564 *duplex = DUPLEX_FULL;
3565 break;
3566
3567 case MII_TG3_AUX_STAT_1000HALF:
3568 *speed = SPEED_1000;
3569 *duplex = DUPLEX_HALF;
3570 break;
3571
3572 case MII_TG3_AUX_STAT_1000FULL:
3573 *speed = SPEED_1000;
3574 *duplex = DUPLEX_FULL;
3575 break;
3576
3577 default:
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003578 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Michael Chan715116a2006-09-27 16:09:25 -07003579 *speed = (val & MII_TG3_AUX_STAT_100) ? SPEED_100 :
3580 SPEED_10;
3581 *duplex = (val & MII_TG3_AUX_STAT_FULL) ? DUPLEX_FULL :
3582 DUPLEX_HALF;
3583 break;
3584 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003585 *speed = SPEED_INVALID;
3586 *duplex = DUPLEX_INVALID;
3587 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07003588 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003589}
3590
Matt Carlson42b64a42011-05-19 12:12:49 +00003591static int tg3_phy_autoneg_cfg(struct tg3 *tp, u32 advertise, u32 flowctrl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003592{
Matt Carlson42b64a42011-05-19 12:12:49 +00003593 int err = 0;
3594 u32 val, new_adv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003595
Matt Carlson42b64a42011-05-19 12:12:49 +00003596 new_adv = ADVERTISE_CSMA;
Matt Carlson37f07022011-11-17 14:30:55 +00003597 new_adv |= ethtool_adv_to_mii_adv_t(advertise);
Matt Carlson42b64a42011-05-19 12:12:49 +00003598 new_adv |= tg3_advert_flowctrl_1000T(flowctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003599
Matt Carlson42b64a42011-05-19 12:12:49 +00003600 err = tg3_writephy(tp, MII_ADVERTISE, new_adv);
3601 if (err)
3602 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003603
Matt Carlson42b64a42011-05-19 12:12:49 +00003604 if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
3605 goto done;
Matt Carlsonba4d07a2007-12-20 20:08:00 -08003606
Matt Carlson37f07022011-11-17 14:30:55 +00003607 new_adv = ethtool_adv_to_mii_ctrl1000_t(advertise);
Matt Carlsonba4d07a2007-12-20 20:08:00 -08003608
Matt Carlson42b64a42011-05-19 12:12:49 +00003609 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
3610 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0)
Matt Carlson221c5632011-06-13 13:39:01 +00003611 new_adv |= CTL1000_AS_MASTER | CTL1000_ENABLE_MASTER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003612
Matt Carlson221c5632011-06-13 13:39:01 +00003613 err = tg3_writephy(tp, MII_CTRL1000, new_adv);
Matt Carlson42b64a42011-05-19 12:12:49 +00003614 if (err)
3615 goto done;
Matt Carlsonba4d07a2007-12-20 20:08:00 -08003616
Matt Carlson42b64a42011-05-19 12:12:49 +00003617 if (!(tp->phy_flags & TG3_PHYFLG_EEE_CAP))
3618 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003619
Matt Carlson42b64a42011-05-19 12:12:49 +00003620 tw32(TG3_CPMU_EEE_MODE,
3621 tr32(TG3_CPMU_EEE_MODE) & ~TG3_CPMU_EEEMD_LPI_ENABLE);
Matt Carlsonba4d07a2007-12-20 20:08:00 -08003622
Matt Carlson42b64a42011-05-19 12:12:49 +00003623 err = TG3_PHY_AUXCTL_SMDSP_ENABLE(tp);
3624 if (!err) {
3625 u32 err2;
Matt Carlson52b02d02010-10-14 10:37:41 +00003626
Matt Carlsona6b68da2010-12-06 08:28:52 +00003627 val = 0;
Matt Carlson42b64a42011-05-19 12:12:49 +00003628 /* Advertise 100-BaseTX EEE ability */
3629 if (advertise & ADVERTISED_100baseT_Full)
3630 val |= MDIO_AN_EEE_ADV_100TX;
3631 /* Advertise 1000-BaseT EEE ability */
3632 if (advertise & ADVERTISED_1000baseT_Full)
3633 val |= MDIO_AN_EEE_ADV_1000T;
3634 err = tg3_phy_cl45_write(tp, MDIO_MMD_AN, MDIO_AN_EEE_ADV, val);
Matt Carlsonb715ce92011-07-20 10:20:52 +00003635 if (err)
3636 val = 0;
3637
3638 switch (GET_ASIC_REV(tp->pci_chip_rev_id)) {
3639 case ASIC_REV_5717:
3640 case ASIC_REV_57765:
3641 case ASIC_REV_5719:
3642 /* If we advertised any eee advertisements above... */
3643 if (val)
3644 val = MII_TG3_DSP_TAP26_ALNOKO |
3645 MII_TG3_DSP_TAP26_RMRXSTO |
3646 MII_TG3_DSP_TAP26_OPCSINPT;
3647 tg3_phydsp_write(tp, MII_TG3_DSP_TAP26, val);
3648 /* Fall through */
3649 case ASIC_REV_5720:
3650 if (!tg3_phydsp_read(tp, MII_TG3_DSP_CH34TP2, &val))
3651 tg3_phydsp_write(tp, MII_TG3_DSP_CH34TP2, val |
3652 MII_TG3_DSP_CH34TP2_HIBW01);
3653 }
Matt Carlson52b02d02010-10-14 10:37:41 +00003654
Matt Carlson42b64a42011-05-19 12:12:49 +00003655 err2 = TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
3656 if (!err)
3657 err = err2;
3658 }
3659
3660done:
3661 return err;
3662}
3663
3664static void tg3_phy_copper_begin(struct tg3 *tp)
3665{
3666 u32 new_adv;
3667 int i;
3668
3669 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) {
3670 new_adv = ADVERTISED_10baseT_Half |
3671 ADVERTISED_10baseT_Full;
3672 if (tg3_flag(tp, WOL_SPEED_100MB))
3673 new_adv |= ADVERTISED_100baseT_Half |
3674 ADVERTISED_100baseT_Full;
3675
3676 tg3_phy_autoneg_cfg(tp, new_adv,
3677 FLOW_CTRL_TX | FLOW_CTRL_RX);
3678 } else if (tp->link_config.speed == SPEED_INVALID) {
3679 if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
3680 tp->link_config.advertising &=
3681 ~(ADVERTISED_1000baseT_Half |
3682 ADVERTISED_1000baseT_Full);
3683
3684 tg3_phy_autoneg_cfg(tp, tp->link_config.advertising,
3685 tp->link_config.flowctrl);
3686 } else {
3687 /* Asking for a specific link mode. */
3688 if (tp->link_config.speed == SPEED_1000) {
3689 if (tp->link_config.duplex == DUPLEX_FULL)
3690 new_adv = ADVERTISED_1000baseT_Full;
3691 else
3692 new_adv = ADVERTISED_1000baseT_Half;
3693 } else if (tp->link_config.speed == SPEED_100) {
3694 if (tp->link_config.duplex == DUPLEX_FULL)
3695 new_adv = ADVERTISED_100baseT_Full;
3696 else
3697 new_adv = ADVERTISED_100baseT_Half;
3698 } else {
3699 if (tp->link_config.duplex == DUPLEX_FULL)
3700 new_adv = ADVERTISED_10baseT_Full;
3701 else
3702 new_adv = ADVERTISED_10baseT_Half;
3703 }
3704
3705 tg3_phy_autoneg_cfg(tp, new_adv,
3706 tp->link_config.flowctrl);
Matt Carlson52b02d02010-10-14 10:37:41 +00003707 }
3708
Linus Torvalds1da177e2005-04-16 15:20:36 -07003709 if (tp->link_config.autoneg == AUTONEG_DISABLE &&
3710 tp->link_config.speed != SPEED_INVALID) {
3711 u32 bmcr, orig_bmcr;
3712
3713 tp->link_config.active_speed = tp->link_config.speed;
3714 tp->link_config.active_duplex = tp->link_config.duplex;
3715
3716 bmcr = 0;
3717 switch (tp->link_config.speed) {
3718 default:
3719 case SPEED_10:
3720 break;
3721
3722 case SPEED_100:
3723 bmcr |= BMCR_SPEED100;
3724 break;
3725
3726 case SPEED_1000:
Matt Carlson221c5632011-06-13 13:39:01 +00003727 bmcr |= BMCR_SPEED1000;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003728 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07003729 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003730
3731 if (tp->link_config.duplex == DUPLEX_FULL)
3732 bmcr |= BMCR_FULLDPLX;
3733
3734 if (!tg3_readphy(tp, MII_BMCR, &orig_bmcr) &&
3735 (bmcr != orig_bmcr)) {
3736 tg3_writephy(tp, MII_BMCR, BMCR_LOOPBACK);
3737 for (i = 0; i < 1500; i++) {
3738 u32 tmp;
3739
3740 udelay(10);
3741 if (tg3_readphy(tp, MII_BMSR, &tmp) ||
3742 tg3_readphy(tp, MII_BMSR, &tmp))
3743 continue;
3744 if (!(tmp & BMSR_LSTATUS)) {
3745 udelay(40);
3746 break;
3747 }
3748 }
3749 tg3_writephy(tp, MII_BMCR, bmcr);
3750 udelay(40);
3751 }
3752 } else {
3753 tg3_writephy(tp, MII_BMCR,
3754 BMCR_ANENABLE | BMCR_ANRESTART);
3755 }
3756}
3757
3758static int tg3_init_5401phy_dsp(struct tg3 *tp)
3759{
3760 int err;
3761
3762 /* Turn off tap power management. */
3763 /* Set Extended packet length bit */
Matt Carlsonb4bd2922011-04-20 07:57:41 +00003764 err = tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_AUXCTL, 0x4c20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003765
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00003766 err |= tg3_phydsp_write(tp, 0x0012, 0x1804);
3767 err |= tg3_phydsp_write(tp, 0x0013, 0x1204);
3768 err |= tg3_phydsp_write(tp, 0x8006, 0x0132);
3769 err |= tg3_phydsp_write(tp, 0x8006, 0x0232);
3770 err |= tg3_phydsp_write(tp, 0x201f, 0x0a20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003771
3772 udelay(40);
3773
3774 return err;
3775}
3776
Michael Chan3600d912006-12-07 00:21:48 -08003777static int tg3_copper_is_advertising_all(struct tg3 *tp, u32 mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003778{
Michael Chan3600d912006-12-07 00:21:48 -08003779 u32 adv_reg, all_mask = 0;
3780
Matt Carlson37f07022011-11-17 14:30:55 +00003781 all_mask = ethtool_adv_to_mii_adv_t(mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003782
3783 if (tg3_readphy(tp, MII_ADVERTISE, &adv_reg))
3784 return 0;
3785
Matt Carlsonb99d2a52011-08-31 11:44:47 +00003786 if ((adv_reg & ADVERTISE_ALL) != all_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003787 return 0;
Matt Carlsonb99d2a52011-08-31 11:44:47 +00003788
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003789 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003790 u32 tg3_ctrl;
3791
Matt Carlson37f07022011-11-17 14:30:55 +00003792 all_mask = ethtool_adv_to_mii_ctrl1000_t(mask);
Michael Chan3600d912006-12-07 00:21:48 -08003793
Matt Carlson221c5632011-06-13 13:39:01 +00003794 if (tg3_readphy(tp, MII_CTRL1000, &tg3_ctrl))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003795 return 0;
3796
Matt Carlsonb99d2a52011-08-31 11:44:47 +00003797 tg3_ctrl &= (ADVERTISE_1000HALF | ADVERTISE_1000FULL);
3798 if (tg3_ctrl != all_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003799 return 0;
3800 }
Matt Carlson93a700a2011-08-31 11:44:54 +00003801
Linus Torvalds1da177e2005-04-16 15:20:36 -07003802 return 1;
3803}
3804
Matt Carlsonef167e22007-12-20 20:10:01 -08003805static int tg3_adv_1000T_flowctrl_ok(struct tg3 *tp, u32 *lcladv, u32 *rmtadv)
3806{
3807 u32 curadv, reqadv;
3808
3809 if (tg3_readphy(tp, MII_ADVERTISE, lcladv))
3810 return 1;
3811
3812 curadv = *lcladv & (ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM);
3813 reqadv = tg3_advert_flowctrl_1000T(tp->link_config.flowctrl);
3814
3815 if (tp->link_config.active_duplex == DUPLEX_FULL) {
3816 if (curadv != reqadv)
3817 return 0;
3818
Joe Perches63c3a662011-04-26 08:12:10 +00003819 if (tg3_flag(tp, PAUSE_AUTONEG))
Matt Carlsonef167e22007-12-20 20:10:01 -08003820 tg3_readphy(tp, MII_LPA, rmtadv);
3821 } else {
3822 /* Reprogram the advertisement register, even if it
3823 * does not affect the current link. If the link
3824 * gets renegotiated in the future, we can save an
3825 * additional renegotiation cycle by advertising
3826 * it correctly in the first place.
3827 */
3828 if (curadv != reqadv) {
3829 *lcladv &= ~(ADVERTISE_PAUSE_CAP |
3830 ADVERTISE_PAUSE_ASYM);
3831 tg3_writephy(tp, MII_ADVERTISE, *lcladv | reqadv);
3832 }
3833 }
3834
3835 return 1;
3836}
3837
Linus Torvalds1da177e2005-04-16 15:20:36 -07003838static int tg3_setup_copper_phy(struct tg3 *tp, int force_reset)
3839{
3840 int current_link_up;
Matt Carlsonf833c4c2010-09-15 09:00:01 +00003841 u32 bmsr, val;
Matt Carlsonef167e22007-12-20 20:10:01 -08003842 u32 lcl_adv, rmt_adv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003843 u16 current_speed;
3844 u8 current_duplex;
3845 int i, err;
3846
3847 tw32(MAC_EVENT, 0);
3848
3849 tw32_f(MAC_STATUS,
3850 (MAC_STATUS_SYNC_CHANGED |
3851 MAC_STATUS_CFG_CHANGED |
3852 MAC_STATUS_MI_COMPLETION |
3853 MAC_STATUS_LNKSTATE_CHANGED));
3854 udelay(40);
3855
Matt Carlson8ef21422008-05-02 16:47:53 -07003856 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
3857 tw32_f(MAC_MI_MODE,
3858 (tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL));
3859 udelay(80);
3860 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003861
Matt Carlsonb4bd2922011-04-20 07:57:41 +00003862 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_PWRCTL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003863
3864 /* Some third-party PHYs need to be reset on link going
3865 * down.
3866 */
3867 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
3868 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
3869 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) &&
3870 netif_carrier_ok(tp->dev)) {
3871 tg3_readphy(tp, MII_BMSR, &bmsr);
3872 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
3873 !(bmsr & BMSR_LSTATUS))
3874 force_reset = 1;
3875 }
3876 if (force_reset)
3877 tg3_phy_reset(tp);
3878
Matt Carlson79eb6902010-02-17 15:17:03 +00003879 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003880 tg3_readphy(tp, MII_BMSR, &bmsr);
3881 if (tg3_readphy(tp, MII_BMSR, &bmsr) ||
Joe Perches63c3a662011-04-26 08:12:10 +00003882 !tg3_flag(tp, INIT_COMPLETE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003883 bmsr = 0;
3884
3885 if (!(bmsr & BMSR_LSTATUS)) {
3886 err = tg3_init_5401phy_dsp(tp);
3887 if (err)
3888 return err;
3889
3890 tg3_readphy(tp, MII_BMSR, &bmsr);
3891 for (i = 0; i < 1000; i++) {
3892 udelay(10);
3893 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
3894 (bmsr & BMSR_LSTATUS)) {
3895 udelay(40);
3896 break;
3897 }
3898 }
3899
Matt Carlson79eb6902010-02-17 15:17:03 +00003900 if ((tp->phy_id & TG3_PHY_ID_REV_MASK) ==
3901 TG3_PHY_REV_BCM5401_B0 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003902 !(bmsr & BMSR_LSTATUS) &&
3903 tp->link_config.active_speed == SPEED_1000) {
3904 err = tg3_phy_reset(tp);
3905 if (!err)
3906 err = tg3_init_5401phy_dsp(tp);
3907 if (err)
3908 return err;
3909 }
3910 }
3911 } else if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
3912 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0) {
3913 /* 5701 {A0,B0} CRC bug workaround */
3914 tg3_writephy(tp, 0x15, 0x0a75);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00003915 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8c68);
3916 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8d68);
3917 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8c68);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003918 }
3919
3920 /* Clear pending interrupts... */
Matt Carlsonf833c4c2010-09-15 09:00:01 +00003921 tg3_readphy(tp, MII_TG3_ISTAT, &val);
3922 tg3_readphy(tp, MII_TG3_ISTAT, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003923
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003924 if (tp->phy_flags & TG3_PHYFLG_USE_MI_INTERRUPT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003925 tg3_writephy(tp, MII_TG3_IMASK, ~MII_TG3_INT_LINKCHG);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003926 else if (!(tp->phy_flags & TG3_PHYFLG_IS_FET))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003927 tg3_writephy(tp, MII_TG3_IMASK, ~0);
3928
3929 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
3930 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
3931 if (tp->led_ctrl == LED_CTRL_MODE_PHY_1)
3932 tg3_writephy(tp, MII_TG3_EXT_CTRL,
3933 MII_TG3_EXT_CTRL_LNK3_LED_MODE);
3934 else
3935 tg3_writephy(tp, MII_TG3_EXT_CTRL, 0);
3936 }
3937
3938 current_link_up = 0;
3939 current_speed = SPEED_INVALID;
3940 current_duplex = DUPLEX_INVALID;
3941
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003942 if (tp->phy_flags & TG3_PHYFLG_CAPACITIVE_COUPLING) {
Matt Carlson15ee95c2011-04-20 07:57:40 +00003943 err = tg3_phy_auxctl_read(tp,
3944 MII_TG3_AUXCTL_SHDWSEL_MISCTEST,
3945 &val);
3946 if (!err && !(val & (1 << 10))) {
Matt Carlsonb4bd2922011-04-20 07:57:41 +00003947 tg3_phy_auxctl_write(tp,
3948 MII_TG3_AUXCTL_SHDWSEL_MISCTEST,
3949 val | (1 << 10));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003950 goto relink;
3951 }
3952 }
3953
3954 bmsr = 0;
3955 for (i = 0; i < 100; i++) {
3956 tg3_readphy(tp, MII_BMSR, &bmsr);
3957 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
3958 (bmsr & BMSR_LSTATUS))
3959 break;
3960 udelay(40);
3961 }
3962
3963 if (bmsr & BMSR_LSTATUS) {
3964 u32 aux_stat, bmcr;
3965
3966 tg3_readphy(tp, MII_TG3_AUX_STAT, &aux_stat);
3967 for (i = 0; i < 2000; i++) {
3968 udelay(10);
3969 if (!tg3_readphy(tp, MII_TG3_AUX_STAT, &aux_stat) &&
3970 aux_stat)
3971 break;
3972 }
3973
3974 tg3_aux_stat_to_speed_duplex(tp, aux_stat,
3975 &current_speed,
3976 &current_duplex);
3977
3978 bmcr = 0;
3979 for (i = 0; i < 200; i++) {
3980 tg3_readphy(tp, MII_BMCR, &bmcr);
3981 if (tg3_readphy(tp, MII_BMCR, &bmcr))
3982 continue;
3983 if (bmcr && bmcr != 0x7fff)
3984 break;
3985 udelay(10);
3986 }
3987
Matt Carlsonef167e22007-12-20 20:10:01 -08003988 lcl_adv = 0;
3989 rmt_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003990
Matt Carlsonef167e22007-12-20 20:10:01 -08003991 tp->link_config.active_speed = current_speed;
3992 tp->link_config.active_duplex = current_duplex;
3993
3994 if (tp->link_config.autoneg == AUTONEG_ENABLE) {
3995 if ((bmcr & BMCR_ANENABLE) &&
3996 tg3_copper_is_advertising_all(tp,
3997 tp->link_config.advertising)) {
3998 if (tg3_adv_1000T_flowctrl_ok(tp, &lcl_adv,
3999 &rmt_adv))
4000 current_link_up = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004001 }
4002 } else {
4003 if (!(bmcr & BMCR_ANENABLE) &&
4004 tp->link_config.speed == current_speed &&
Matt Carlsonef167e22007-12-20 20:10:01 -08004005 tp->link_config.duplex == current_duplex &&
4006 tp->link_config.flowctrl ==
4007 tp->link_config.active_flowctrl) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004008 current_link_up = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004009 }
4010 }
4011
Matt Carlsonef167e22007-12-20 20:10:01 -08004012 if (current_link_up == 1 &&
4013 tp->link_config.active_duplex == DUPLEX_FULL)
4014 tg3_setup_flow_control(tp, lcl_adv, rmt_adv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004015 }
4016
Linus Torvalds1da177e2005-04-16 15:20:36 -07004017relink:
Matt Carlson80096062010-08-02 11:26:06 +00004018 if (current_link_up == 0 || (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004019 tg3_phy_copper_begin(tp);
4020
Matt Carlsonf833c4c2010-09-15 09:00:01 +00004021 tg3_readphy(tp, MII_BMSR, &bmsr);
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00004022 if ((!tg3_readphy(tp, MII_BMSR, &bmsr) && (bmsr & BMSR_LSTATUS)) ||
4023 (tp->mac_mode & MAC_MODE_PORT_INT_LPBACK))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004024 current_link_up = 1;
4025 }
4026
4027 tp->mac_mode &= ~MAC_MODE_PORT_MODE_MASK;
4028 if (current_link_up == 1) {
4029 if (tp->link_config.active_speed == SPEED_100 ||
4030 tp->link_config.active_speed == SPEED_10)
4031 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
4032 else
4033 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004034 } else if (tp->phy_flags & TG3_PHYFLG_IS_FET)
Matt Carlson7f97a4b2009-08-25 10:10:03 +00004035 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
4036 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004037 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
4038
4039 tp->mac_mode &= ~MAC_MODE_HALF_DUPLEX;
4040 if (tp->link_config.active_duplex == DUPLEX_HALF)
4041 tp->mac_mode |= MAC_MODE_HALF_DUPLEX;
4042
Linus Torvalds1da177e2005-04-16 15:20:36 -07004043 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700) {
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07004044 if (current_link_up == 1 &&
4045 tg3_5700_link_polarity(tp, tp->link_config.active_speed))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004046 tp->mac_mode |= MAC_MODE_LINK_POLARITY;
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07004047 else
4048 tp->mac_mode &= ~MAC_MODE_LINK_POLARITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004049 }
4050
4051 /* ??? Without this setting Netgear GA302T PHY does not
4052 * ??? send/receive packets...
4053 */
Matt Carlson79eb6902010-02-17 15:17:03 +00004054 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5411 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004055 tp->pci_chip_rev_id == CHIPREV_ID_5700_ALTIMA) {
4056 tp->mi_mode |= MAC_MI_MODE_AUTO_POLL;
4057 tw32_f(MAC_MI_MODE, tp->mi_mode);
4058 udelay(80);
4059 }
4060
4061 tw32_f(MAC_MODE, tp->mac_mode);
4062 udelay(40);
4063
Matt Carlson52b02d02010-10-14 10:37:41 +00004064 tg3_phy_eee_adjust(tp, current_link_up);
4065
Joe Perches63c3a662011-04-26 08:12:10 +00004066 if (tg3_flag(tp, USE_LINKCHG_REG)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004067 /* Polled via timer. */
4068 tw32_f(MAC_EVENT, 0);
4069 } else {
4070 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
4071 }
4072 udelay(40);
4073
4074 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 &&
4075 current_link_up == 1 &&
4076 tp->link_config.active_speed == SPEED_1000 &&
Joe Perches63c3a662011-04-26 08:12:10 +00004077 (tg3_flag(tp, PCIX_MODE) || tg3_flag(tp, PCI_HIGH_SPEED))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004078 udelay(120);
4079 tw32_f(MAC_STATUS,
4080 (MAC_STATUS_SYNC_CHANGED |
4081 MAC_STATUS_CFG_CHANGED));
4082 udelay(40);
4083 tg3_write_mem(tp,
4084 NIC_SRAM_FIRMWARE_MBOX,
4085 NIC_SRAM_FIRMWARE_MBOX_MAGIC2);
4086 }
4087
Matt Carlson5e7dfd02008-11-21 17:18:16 -08004088 /* Prevent send BD corruption. */
Joe Perches63c3a662011-04-26 08:12:10 +00004089 if (tg3_flag(tp, CLKREQ_BUG)) {
Matt Carlson5e7dfd02008-11-21 17:18:16 -08004090 u16 oldlnkctl, newlnkctl;
4091
4092 pci_read_config_word(tp->pdev,
Jon Mason708ebb3a2011-06-27 12:56:50 +00004093 pci_pcie_cap(tp->pdev) + PCI_EXP_LNKCTL,
Matt Carlson5e7dfd02008-11-21 17:18:16 -08004094 &oldlnkctl);
4095 if (tp->link_config.active_speed == SPEED_100 ||
4096 tp->link_config.active_speed == SPEED_10)
4097 newlnkctl = oldlnkctl & ~PCI_EXP_LNKCTL_CLKREQ_EN;
4098 else
4099 newlnkctl = oldlnkctl | PCI_EXP_LNKCTL_CLKREQ_EN;
4100 if (newlnkctl != oldlnkctl)
4101 pci_write_config_word(tp->pdev,
Matt Carlson93a700a2011-08-31 11:44:54 +00004102 pci_pcie_cap(tp->pdev) +
4103 PCI_EXP_LNKCTL, newlnkctl);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08004104 }
4105
Linus Torvalds1da177e2005-04-16 15:20:36 -07004106 if (current_link_up != netif_carrier_ok(tp->dev)) {
4107 if (current_link_up)
4108 netif_carrier_on(tp->dev);
4109 else
4110 netif_carrier_off(tp->dev);
4111 tg3_link_report(tp);
4112 }
4113
4114 return 0;
4115}
4116
4117struct tg3_fiber_aneginfo {
4118 int state;
4119#define ANEG_STATE_UNKNOWN 0
4120#define ANEG_STATE_AN_ENABLE 1
4121#define ANEG_STATE_RESTART_INIT 2
4122#define ANEG_STATE_RESTART 3
4123#define ANEG_STATE_DISABLE_LINK_OK 4
4124#define ANEG_STATE_ABILITY_DETECT_INIT 5
4125#define ANEG_STATE_ABILITY_DETECT 6
4126#define ANEG_STATE_ACK_DETECT_INIT 7
4127#define ANEG_STATE_ACK_DETECT 8
4128#define ANEG_STATE_COMPLETE_ACK_INIT 9
4129#define ANEG_STATE_COMPLETE_ACK 10
4130#define ANEG_STATE_IDLE_DETECT_INIT 11
4131#define ANEG_STATE_IDLE_DETECT 12
4132#define ANEG_STATE_LINK_OK 13
4133#define ANEG_STATE_NEXT_PAGE_WAIT_INIT 14
4134#define ANEG_STATE_NEXT_PAGE_WAIT 15
4135
4136 u32 flags;
4137#define MR_AN_ENABLE 0x00000001
4138#define MR_RESTART_AN 0x00000002
4139#define MR_AN_COMPLETE 0x00000004
4140#define MR_PAGE_RX 0x00000008
4141#define MR_NP_LOADED 0x00000010
4142#define MR_TOGGLE_TX 0x00000020
4143#define MR_LP_ADV_FULL_DUPLEX 0x00000040
4144#define MR_LP_ADV_HALF_DUPLEX 0x00000080
4145#define MR_LP_ADV_SYM_PAUSE 0x00000100
4146#define MR_LP_ADV_ASYM_PAUSE 0x00000200
4147#define MR_LP_ADV_REMOTE_FAULT1 0x00000400
4148#define MR_LP_ADV_REMOTE_FAULT2 0x00000800
4149#define MR_LP_ADV_NEXT_PAGE 0x00001000
4150#define MR_TOGGLE_RX 0x00002000
4151#define MR_NP_RX 0x00004000
4152
4153#define MR_LINK_OK 0x80000000
4154
4155 unsigned long link_time, cur_time;
4156
4157 u32 ability_match_cfg;
4158 int ability_match_count;
4159
4160 char ability_match, idle_match, ack_match;
4161
4162 u32 txconfig, rxconfig;
4163#define ANEG_CFG_NP 0x00000080
4164#define ANEG_CFG_ACK 0x00000040
4165#define ANEG_CFG_RF2 0x00000020
4166#define ANEG_CFG_RF1 0x00000010
4167#define ANEG_CFG_PS2 0x00000001
4168#define ANEG_CFG_PS1 0x00008000
4169#define ANEG_CFG_HD 0x00004000
4170#define ANEG_CFG_FD 0x00002000
4171#define ANEG_CFG_INVAL 0x00001f06
4172
4173};
4174#define ANEG_OK 0
4175#define ANEG_DONE 1
4176#define ANEG_TIMER_ENAB 2
4177#define ANEG_FAILED -1
4178
4179#define ANEG_STATE_SETTLE_TIME 10000
4180
4181static int tg3_fiber_aneg_smachine(struct tg3 *tp,
4182 struct tg3_fiber_aneginfo *ap)
4183{
Matt Carlson5be73b42007-12-20 20:09:29 -08004184 u16 flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004185 unsigned long delta;
4186 u32 rx_cfg_reg;
4187 int ret;
4188
4189 if (ap->state == ANEG_STATE_UNKNOWN) {
4190 ap->rxconfig = 0;
4191 ap->link_time = 0;
4192 ap->cur_time = 0;
4193 ap->ability_match_cfg = 0;
4194 ap->ability_match_count = 0;
4195 ap->ability_match = 0;
4196 ap->idle_match = 0;
4197 ap->ack_match = 0;
4198 }
4199 ap->cur_time++;
4200
4201 if (tr32(MAC_STATUS) & MAC_STATUS_RCVD_CFG) {
4202 rx_cfg_reg = tr32(MAC_RX_AUTO_NEG);
4203
4204 if (rx_cfg_reg != ap->ability_match_cfg) {
4205 ap->ability_match_cfg = rx_cfg_reg;
4206 ap->ability_match = 0;
4207 ap->ability_match_count = 0;
4208 } else {
4209 if (++ap->ability_match_count > 1) {
4210 ap->ability_match = 1;
4211 ap->ability_match_cfg = rx_cfg_reg;
4212 }
4213 }
4214 if (rx_cfg_reg & ANEG_CFG_ACK)
4215 ap->ack_match = 1;
4216 else
4217 ap->ack_match = 0;
4218
4219 ap->idle_match = 0;
4220 } else {
4221 ap->idle_match = 1;
4222 ap->ability_match_cfg = 0;
4223 ap->ability_match_count = 0;
4224 ap->ability_match = 0;
4225 ap->ack_match = 0;
4226
4227 rx_cfg_reg = 0;
4228 }
4229
4230 ap->rxconfig = rx_cfg_reg;
4231 ret = ANEG_OK;
4232
Matt Carlson33f401a2010-04-05 10:19:27 +00004233 switch (ap->state) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004234 case ANEG_STATE_UNKNOWN:
4235 if (ap->flags & (MR_AN_ENABLE | MR_RESTART_AN))
4236 ap->state = ANEG_STATE_AN_ENABLE;
4237
4238 /* fallthru */
4239 case ANEG_STATE_AN_ENABLE:
4240 ap->flags &= ~(MR_AN_COMPLETE | MR_PAGE_RX);
4241 if (ap->flags & MR_AN_ENABLE) {
4242 ap->link_time = 0;
4243 ap->cur_time = 0;
4244 ap->ability_match_cfg = 0;
4245 ap->ability_match_count = 0;
4246 ap->ability_match = 0;
4247 ap->idle_match = 0;
4248 ap->ack_match = 0;
4249
4250 ap->state = ANEG_STATE_RESTART_INIT;
4251 } else {
4252 ap->state = ANEG_STATE_DISABLE_LINK_OK;
4253 }
4254 break;
4255
4256 case ANEG_STATE_RESTART_INIT:
4257 ap->link_time = ap->cur_time;
4258 ap->flags &= ~(MR_NP_LOADED);
4259 ap->txconfig = 0;
4260 tw32(MAC_TX_AUTO_NEG, 0);
4261 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
4262 tw32_f(MAC_MODE, tp->mac_mode);
4263 udelay(40);
4264
4265 ret = ANEG_TIMER_ENAB;
4266 ap->state = ANEG_STATE_RESTART;
4267
4268 /* fallthru */
4269 case ANEG_STATE_RESTART:
4270 delta = ap->cur_time - ap->link_time;
Matt Carlson859a588792010-04-05 10:19:28 +00004271 if (delta > ANEG_STATE_SETTLE_TIME)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004272 ap->state = ANEG_STATE_ABILITY_DETECT_INIT;
Matt Carlson859a588792010-04-05 10:19:28 +00004273 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004274 ret = ANEG_TIMER_ENAB;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004275 break;
4276
4277 case ANEG_STATE_DISABLE_LINK_OK:
4278 ret = ANEG_DONE;
4279 break;
4280
4281 case ANEG_STATE_ABILITY_DETECT_INIT:
4282 ap->flags &= ~(MR_TOGGLE_TX);
Matt Carlson5be73b42007-12-20 20:09:29 -08004283 ap->txconfig = ANEG_CFG_FD;
4284 flowctrl = tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
4285 if (flowctrl & ADVERTISE_1000XPAUSE)
4286 ap->txconfig |= ANEG_CFG_PS1;
4287 if (flowctrl & ADVERTISE_1000XPSE_ASYM)
4288 ap->txconfig |= ANEG_CFG_PS2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004289 tw32(MAC_TX_AUTO_NEG, ap->txconfig);
4290 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
4291 tw32_f(MAC_MODE, tp->mac_mode);
4292 udelay(40);
4293
4294 ap->state = ANEG_STATE_ABILITY_DETECT;
4295 break;
4296
4297 case ANEG_STATE_ABILITY_DETECT:
Matt Carlson859a588792010-04-05 10:19:28 +00004298 if (ap->ability_match != 0 && ap->rxconfig != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004299 ap->state = ANEG_STATE_ACK_DETECT_INIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004300 break;
4301
4302 case ANEG_STATE_ACK_DETECT_INIT:
4303 ap->txconfig |= ANEG_CFG_ACK;
4304 tw32(MAC_TX_AUTO_NEG, ap->txconfig);
4305 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
4306 tw32_f(MAC_MODE, tp->mac_mode);
4307 udelay(40);
4308
4309 ap->state = ANEG_STATE_ACK_DETECT;
4310
4311 /* fallthru */
4312 case ANEG_STATE_ACK_DETECT:
4313 if (ap->ack_match != 0) {
4314 if ((ap->rxconfig & ~ANEG_CFG_ACK) ==
4315 (ap->ability_match_cfg & ~ANEG_CFG_ACK)) {
4316 ap->state = ANEG_STATE_COMPLETE_ACK_INIT;
4317 } else {
4318 ap->state = ANEG_STATE_AN_ENABLE;
4319 }
4320 } else if (ap->ability_match != 0 &&
4321 ap->rxconfig == 0) {
4322 ap->state = ANEG_STATE_AN_ENABLE;
4323 }
4324 break;
4325
4326 case ANEG_STATE_COMPLETE_ACK_INIT:
4327 if (ap->rxconfig & ANEG_CFG_INVAL) {
4328 ret = ANEG_FAILED;
4329 break;
4330 }
4331 ap->flags &= ~(MR_LP_ADV_FULL_DUPLEX |
4332 MR_LP_ADV_HALF_DUPLEX |
4333 MR_LP_ADV_SYM_PAUSE |
4334 MR_LP_ADV_ASYM_PAUSE |
4335 MR_LP_ADV_REMOTE_FAULT1 |
4336 MR_LP_ADV_REMOTE_FAULT2 |
4337 MR_LP_ADV_NEXT_PAGE |
4338 MR_TOGGLE_RX |
4339 MR_NP_RX);
4340 if (ap->rxconfig & ANEG_CFG_FD)
4341 ap->flags |= MR_LP_ADV_FULL_DUPLEX;
4342 if (ap->rxconfig & ANEG_CFG_HD)
4343 ap->flags |= MR_LP_ADV_HALF_DUPLEX;
4344 if (ap->rxconfig & ANEG_CFG_PS1)
4345 ap->flags |= MR_LP_ADV_SYM_PAUSE;
4346 if (ap->rxconfig & ANEG_CFG_PS2)
4347 ap->flags |= MR_LP_ADV_ASYM_PAUSE;
4348 if (ap->rxconfig & ANEG_CFG_RF1)
4349 ap->flags |= MR_LP_ADV_REMOTE_FAULT1;
4350 if (ap->rxconfig & ANEG_CFG_RF2)
4351 ap->flags |= MR_LP_ADV_REMOTE_FAULT2;
4352 if (ap->rxconfig & ANEG_CFG_NP)
4353 ap->flags |= MR_LP_ADV_NEXT_PAGE;
4354
4355 ap->link_time = ap->cur_time;
4356
4357 ap->flags ^= (MR_TOGGLE_TX);
4358 if (ap->rxconfig & 0x0008)
4359 ap->flags |= MR_TOGGLE_RX;
4360 if (ap->rxconfig & ANEG_CFG_NP)
4361 ap->flags |= MR_NP_RX;
4362 ap->flags |= MR_PAGE_RX;
4363
4364 ap->state = ANEG_STATE_COMPLETE_ACK;
4365 ret = ANEG_TIMER_ENAB;
4366 break;
4367
4368 case ANEG_STATE_COMPLETE_ACK:
4369 if (ap->ability_match != 0 &&
4370 ap->rxconfig == 0) {
4371 ap->state = ANEG_STATE_AN_ENABLE;
4372 break;
4373 }
4374 delta = ap->cur_time - ap->link_time;
4375 if (delta > ANEG_STATE_SETTLE_TIME) {
4376 if (!(ap->flags & (MR_LP_ADV_NEXT_PAGE))) {
4377 ap->state = ANEG_STATE_IDLE_DETECT_INIT;
4378 } else {
4379 if ((ap->txconfig & ANEG_CFG_NP) == 0 &&
4380 !(ap->flags & MR_NP_RX)) {
4381 ap->state = ANEG_STATE_IDLE_DETECT_INIT;
4382 } else {
4383 ret = ANEG_FAILED;
4384 }
4385 }
4386 }
4387 break;
4388
4389 case ANEG_STATE_IDLE_DETECT_INIT:
4390 ap->link_time = ap->cur_time;
4391 tp->mac_mode &= ~MAC_MODE_SEND_CONFIGS;
4392 tw32_f(MAC_MODE, tp->mac_mode);
4393 udelay(40);
4394
4395 ap->state = ANEG_STATE_IDLE_DETECT;
4396 ret = ANEG_TIMER_ENAB;
4397 break;
4398
4399 case ANEG_STATE_IDLE_DETECT:
4400 if (ap->ability_match != 0 &&
4401 ap->rxconfig == 0) {
4402 ap->state = ANEG_STATE_AN_ENABLE;
4403 break;
4404 }
4405 delta = ap->cur_time - ap->link_time;
4406 if (delta > ANEG_STATE_SETTLE_TIME) {
4407 /* XXX another gem from the Broadcom driver :( */
4408 ap->state = ANEG_STATE_LINK_OK;
4409 }
4410 break;
4411
4412 case ANEG_STATE_LINK_OK:
4413 ap->flags |= (MR_AN_COMPLETE | MR_LINK_OK);
4414 ret = ANEG_DONE;
4415 break;
4416
4417 case ANEG_STATE_NEXT_PAGE_WAIT_INIT:
4418 /* ??? unimplemented */
4419 break;
4420
4421 case ANEG_STATE_NEXT_PAGE_WAIT:
4422 /* ??? unimplemented */
4423 break;
4424
4425 default:
4426 ret = ANEG_FAILED;
4427 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07004428 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004429
4430 return ret;
4431}
4432
Matt Carlson5be73b42007-12-20 20:09:29 -08004433static int fiber_autoneg(struct tg3 *tp, u32 *txflags, u32 *rxflags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004434{
4435 int res = 0;
4436 struct tg3_fiber_aneginfo aninfo;
4437 int status = ANEG_FAILED;
4438 unsigned int tick;
4439 u32 tmp;
4440
4441 tw32_f(MAC_TX_AUTO_NEG, 0);
4442
4443 tmp = tp->mac_mode & ~MAC_MODE_PORT_MODE_MASK;
4444 tw32_f(MAC_MODE, tmp | MAC_MODE_PORT_MODE_GMII);
4445 udelay(40);
4446
4447 tw32_f(MAC_MODE, tp->mac_mode | MAC_MODE_SEND_CONFIGS);
4448 udelay(40);
4449
4450 memset(&aninfo, 0, sizeof(aninfo));
4451 aninfo.flags |= MR_AN_ENABLE;
4452 aninfo.state = ANEG_STATE_UNKNOWN;
4453 aninfo.cur_time = 0;
4454 tick = 0;
4455 while (++tick < 195000) {
4456 status = tg3_fiber_aneg_smachine(tp, &aninfo);
4457 if (status == ANEG_DONE || status == ANEG_FAILED)
4458 break;
4459
4460 udelay(1);
4461 }
4462
4463 tp->mac_mode &= ~MAC_MODE_SEND_CONFIGS;
4464 tw32_f(MAC_MODE, tp->mac_mode);
4465 udelay(40);
4466
Matt Carlson5be73b42007-12-20 20:09:29 -08004467 *txflags = aninfo.txconfig;
4468 *rxflags = aninfo.flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004469
4470 if (status == ANEG_DONE &&
4471 (aninfo.flags & (MR_AN_COMPLETE | MR_LINK_OK |
4472 MR_LP_ADV_FULL_DUPLEX)))
4473 res = 1;
4474
4475 return res;
4476}
4477
4478static void tg3_init_bcm8002(struct tg3 *tp)
4479{
4480 u32 mac_status = tr32(MAC_STATUS);
4481 int i;
4482
4483 /* Reset when initting first time or we have a link. */
Joe Perches63c3a662011-04-26 08:12:10 +00004484 if (tg3_flag(tp, INIT_COMPLETE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004485 !(mac_status & MAC_STATUS_PCS_SYNCED))
4486 return;
4487
4488 /* Set PLL lock range. */
4489 tg3_writephy(tp, 0x16, 0x8007);
4490
4491 /* SW reset */
4492 tg3_writephy(tp, MII_BMCR, BMCR_RESET);
4493
4494 /* Wait for reset to complete. */
4495 /* XXX schedule_timeout() ... */
4496 for (i = 0; i < 500; i++)
4497 udelay(10);
4498
4499 /* Config mode; select PMA/Ch 1 regs. */
4500 tg3_writephy(tp, 0x10, 0x8411);
4501
4502 /* Enable auto-lock and comdet, select txclk for tx. */
4503 tg3_writephy(tp, 0x11, 0x0a10);
4504
4505 tg3_writephy(tp, 0x18, 0x00a0);
4506 tg3_writephy(tp, 0x16, 0x41ff);
4507
4508 /* Assert and deassert POR. */
4509 tg3_writephy(tp, 0x13, 0x0400);
4510 udelay(40);
4511 tg3_writephy(tp, 0x13, 0x0000);
4512
4513 tg3_writephy(tp, 0x11, 0x0a50);
4514 udelay(40);
4515 tg3_writephy(tp, 0x11, 0x0a10);
4516
4517 /* Wait for signal to stabilize */
4518 /* XXX schedule_timeout() ... */
4519 for (i = 0; i < 15000; i++)
4520 udelay(10);
4521
4522 /* Deselect the channel register so we can read the PHYID
4523 * later.
4524 */
4525 tg3_writephy(tp, 0x10, 0x8011);
4526}
4527
4528static int tg3_setup_fiber_hw_autoneg(struct tg3 *tp, u32 mac_status)
4529{
Matt Carlson82cd3d12007-12-20 20:09:00 -08004530 u16 flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004531 u32 sg_dig_ctrl, sg_dig_status;
4532 u32 serdes_cfg, expected_sg_dig_ctrl;
4533 int workaround, port_a;
4534 int current_link_up;
4535
4536 serdes_cfg = 0;
4537 expected_sg_dig_ctrl = 0;
4538 workaround = 0;
4539 port_a = 1;
4540 current_link_up = 0;
4541
4542 if (tp->pci_chip_rev_id != CHIPREV_ID_5704_A0 &&
4543 tp->pci_chip_rev_id != CHIPREV_ID_5704_A1) {
4544 workaround = 1;
4545 if (tr32(TG3PCI_DUAL_MAC_CTRL) & DUAL_MAC_CTRL_ID)
4546 port_a = 0;
4547
4548 /* preserve bits 0-11,13,14 for signal pre-emphasis */
4549 /* preserve bits 20-23 for voltage regulator */
4550 serdes_cfg = tr32(MAC_SERDES_CFG) & 0x00f06fff;
4551 }
4552
4553 sg_dig_ctrl = tr32(SG_DIG_CTRL);
4554
4555 if (tp->link_config.autoneg != AUTONEG_ENABLE) {
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004556 if (sg_dig_ctrl & SG_DIG_USING_HW_AUTONEG) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004557 if (workaround) {
4558 u32 val = serdes_cfg;
4559
4560 if (port_a)
4561 val |= 0xc010000;
4562 else
4563 val |= 0x4010000;
4564 tw32_f(MAC_SERDES_CFG, val);
4565 }
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004566
4567 tw32_f(SG_DIG_CTRL, SG_DIG_COMMON_SETUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004568 }
4569 if (mac_status & MAC_STATUS_PCS_SYNCED) {
4570 tg3_setup_flow_control(tp, 0, 0);
4571 current_link_up = 1;
4572 }
4573 goto out;
4574 }
4575
4576 /* Want auto-negotiation. */
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004577 expected_sg_dig_ctrl = SG_DIG_USING_HW_AUTONEG | SG_DIG_COMMON_SETUP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004578
Matt Carlson82cd3d12007-12-20 20:09:00 -08004579 flowctrl = tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
4580 if (flowctrl & ADVERTISE_1000XPAUSE)
4581 expected_sg_dig_ctrl |= SG_DIG_PAUSE_CAP;
4582 if (flowctrl & ADVERTISE_1000XPSE_ASYM)
4583 expected_sg_dig_ctrl |= SG_DIG_ASYM_PAUSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004584
4585 if (sg_dig_ctrl != expected_sg_dig_ctrl) {
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004586 if ((tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT) &&
Michael Chan3d3ebe72006-09-27 15:59:15 -07004587 tp->serdes_counter &&
4588 ((mac_status & (MAC_STATUS_PCS_SYNCED |
4589 MAC_STATUS_RCVD_CFG)) ==
4590 MAC_STATUS_PCS_SYNCED)) {
4591 tp->serdes_counter--;
4592 current_link_up = 1;
4593 goto out;
4594 }
4595restart_autoneg:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004596 if (workaround)
4597 tw32_f(MAC_SERDES_CFG, serdes_cfg | 0xc011000);
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004598 tw32_f(SG_DIG_CTRL, expected_sg_dig_ctrl | SG_DIG_SOFT_RESET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004599 udelay(5);
4600 tw32_f(SG_DIG_CTRL, expected_sg_dig_ctrl);
4601
Michael Chan3d3ebe72006-09-27 15:59:15 -07004602 tp->serdes_counter = SERDES_AN_TIMEOUT_5704S;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004603 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004604 } else if (mac_status & (MAC_STATUS_PCS_SYNCED |
4605 MAC_STATUS_SIGNAL_DET)) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07004606 sg_dig_status = tr32(SG_DIG_STATUS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004607 mac_status = tr32(MAC_STATUS);
4608
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004609 if ((sg_dig_status & SG_DIG_AUTONEG_COMPLETE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004610 (mac_status & MAC_STATUS_PCS_SYNCED)) {
Matt Carlson82cd3d12007-12-20 20:09:00 -08004611 u32 local_adv = 0, remote_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004612
Matt Carlson82cd3d12007-12-20 20:09:00 -08004613 if (sg_dig_ctrl & SG_DIG_PAUSE_CAP)
4614 local_adv |= ADVERTISE_1000XPAUSE;
4615 if (sg_dig_ctrl & SG_DIG_ASYM_PAUSE)
4616 local_adv |= ADVERTISE_1000XPSE_ASYM;
4617
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004618 if (sg_dig_status & SG_DIG_PARTNER_PAUSE_CAPABLE)
Matt Carlson82cd3d12007-12-20 20:09:00 -08004619 remote_adv |= LPA_1000XPAUSE;
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004620 if (sg_dig_status & SG_DIG_PARTNER_ASYM_PAUSE)
Matt Carlson82cd3d12007-12-20 20:09:00 -08004621 remote_adv |= LPA_1000XPAUSE_ASYM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004622
4623 tg3_setup_flow_control(tp, local_adv, remote_adv);
4624 current_link_up = 1;
Michael Chan3d3ebe72006-09-27 15:59:15 -07004625 tp->serdes_counter = 0;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004626 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004627 } else if (!(sg_dig_status & SG_DIG_AUTONEG_COMPLETE)) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07004628 if (tp->serdes_counter)
4629 tp->serdes_counter--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004630 else {
4631 if (workaround) {
4632 u32 val = serdes_cfg;
4633
4634 if (port_a)
4635 val |= 0xc010000;
4636 else
4637 val |= 0x4010000;
4638
4639 tw32_f(MAC_SERDES_CFG, val);
4640 }
4641
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004642 tw32_f(SG_DIG_CTRL, SG_DIG_COMMON_SETUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004643 udelay(40);
4644
4645 /* Link parallel detection - link is up */
4646 /* only if we have PCS_SYNC and not */
4647 /* receiving config code words */
4648 mac_status = tr32(MAC_STATUS);
4649 if ((mac_status & MAC_STATUS_PCS_SYNCED) &&
4650 !(mac_status & MAC_STATUS_RCVD_CFG)) {
4651 tg3_setup_flow_control(tp, 0, 0);
4652 current_link_up = 1;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004653 tp->phy_flags |=
4654 TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan3d3ebe72006-09-27 15:59:15 -07004655 tp->serdes_counter =
4656 SERDES_PARALLEL_DET_TIMEOUT;
4657 } else
4658 goto restart_autoneg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004659 }
4660 }
Michael Chan3d3ebe72006-09-27 15:59:15 -07004661 } else {
4662 tp->serdes_counter = SERDES_AN_TIMEOUT_5704S;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004663 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004664 }
4665
4666out:
4667 return current_link_up;
4668}
4669
4670static int tg3_setup_fiber_by_hand(struct tg3 *tp, u32 mac_status)
4671{
4672 int current_link_up = 0;
4673
Michael Chan5cf64b8a2007-05-05 12:11:21 -07004674 if (!(mac_status & MAC_STATUS_PCS_SYNCED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004675 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004676
4677 if (tp->link_config.autoneg == AUTONEG_ENABLE) {
Matt Carlson5be73b42007-12-20 20:09:29 -08004678 u32 txflags, rxflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004679 int i;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04004680
Matt Carlson5be73b42007-12-20 20:09:29 -08004681 if (fiber_autoneg(tp, &txflags, &rxflags)) {
4682 u32 local_adv = 0, remote_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004683
Matt Carlson5be73b42007-12-20 20:09:29 -08004684 if (txflags & ANEG_CFG_PS1)
4685 local_adv |= ADVERTISE_1000XPAUSE;
4686 if (txflags & ANEG_CFG_PS2)
4687 local_adv |= ADVERTISE_1000XPSE_ASYM;
4688
4689 if (rxflags & MR_LP_ADV_SYM_PAUSE)
4690 remote_adv |= LPA_1000XPAUSE;
4691 if (rxflags & MR_LP_ADV_ASYM_PAUSE)
4692 remote_adv |= LPA_1000XPAUSE_ASYM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004693
4694 tg3_setup_flow_control(tp, local_adv, remote_adv);
4695
Linus Torvalds1da177e2005-04-16 15:20:36 -07004696 current_link_up = 1;
4697 }
4698 for (i = 0; i < 30; i++) {
4699 udelay(20);
4700 tw32_f(MAC_STATUS,
4701 (MAC_STATUS_SYNC_CHANGED |
4702 MAC_STATUS_CFG_CHANGED));
4703 udelay(40);
4704 if ((tr32(MAC_STATUS) &
4705 (MAC_STATUS_SYNC_CHANGED |
4706 MAC_STATUS_CFG_CHANGED)) == 0)
4707 break;
4708 }
4709
4710 mac_status = tr32(MAC_STATUS);
4711 if (current_link_up == 0 &&
4712 (mac_status & MAC_STATUS_PCS_SYNCED) &&
4713 !(mac_status & MAC_STATUS_RCVD_CFG))
4714 current_link_up = 1;
4715 } else {
Matt Carlson5be73b42007-12-20 20:09:29 -08004716 tg3_setup_flow_control(tp, 0, 0);
4717
Linus Torvalds1da177e2005-04-16 15:20:36 -07004718 /* Forcing 1000FD link up. */
4719 current_link_up = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004720
4721 tw32_f(MAC_MODE, (tp->mac_mode | MAC_MODE_SEND_CONFIGS));
4722 udelay(40);
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07004723
4724 tw32_f(MAC_MODE, tp->mac_mode);
4725 udelay(40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004726 }
4727
4728out:
4729 return current_link_up;
4730}
4731
4732static int tg3_setup_fiber_phy(struct tg3 *tp, int force_reset)
4733{
4734 u32 orig_pause_cfg;
4735 u16 orig_active_speed;
4736 u8 orig_active_duplex;
4737 u32 mac_status;
4738 int current_link_up;
4739 int i;
4740
Matt Carlson8d018622007-12-20 20:05:44 -08004741 orig_pause_cfg = tp->link_config.active_flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004742 orig_active_speed = tp->link_config.active_speed;
4743 orig_active_duplex = tp->link_config.active_duplex;
4744
Joe Perches63c3a662011-04-26 08:12:10 +00004745 if (!tg3_flag(tp, HW_AUTONEG) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004746 netif_carrier_ok(tp->dev) &&
Joe Perches63c3a662011-04-26 08:12:10 +00004747 tg3_flag(tp, INIT_COMPLETE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004748 mac_status = tr32(MAC_STATUS);
4749 mac_status &= (MAC_STATUS_PCS_SYNCED |
4750 MAC_STATUS_SIGNAL_DET |
4751 MAC_STATUS_CFG_CHANGED |
4752 MAC_STATUS_RCVD_CFG);
4753 if (mac_status == (MAC_STATUS_PCS_SYNCED |
4754 MAC_STATUS_SIGNAL_DET)) {
4755 tw32_f(MAC_STATUS, (MAC_STATUS_SYNC_CHANGED |
4756 MAC_STATUS_CFG_CHANGED));
4757 return 0;
4758 }
4759 }
4760
4761 tw32_f(MAC_TX_AUTO_NEG, 0);
4762
4763 tp->mac_mode &= ~(MAC_MODE_PORT_MODE_MASK | MAC_MODE_HALF_DUPLEX);
4764 tp->mac_mode |= MAC_MODE_PORT_MODE_TBI;
4765 tw32_f(MAC_MODE, tp->mac_mode);
4766 udelay(40);
4767
Matt Carlson79eb6902010-02-17 15:17:03 +00004768 if (tp->phy_id == TG3_PHY_ID_BCM8002)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004769 tg3_init_bcm8002(tp);
4770
4771 /* Enable link change event even when serdes polling. */
4772 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
4773 udelay(40);
4774
4775 current_link_up = 0;
4776 mac_status = tr32(MAC_STATUS);
4777
Joe Perches63c3a662011-04-26 08:12:10 +00004778 if (tg3_flag(tp, HW_AUTONEG))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004779 current_link_up = tg3_setup_fiber_hw_autoneg(tp, mac_status);
4780 else
4781 current_link_up = tg3_setup_fiber_by_hand(tp, mac_status);
4782
Matt Carlson898a56f2009-08-28 14:02:40 +00004783 tp->napi[0].hw_status->status =
Linus Torvalds1da177e2005-04-16 15:20:36 -07004784 (SD_STATUS_UPDATED |
Matt Carlson898a56f2009-08-28 14:02:40 +00004785 (tp->napi[0].hw_status->status & ~SD_STATUS_LINK_CHG));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004786
4787 for (i = 0; i < 100; i++) {
4788 tw32_f(MAC_STATUS, (MAC_STATUS_SYNC_CHANGED |
4789 MAC_STATUS_CFG_CHANGED));
4790 udelay(5);
4791 if ((tr32(MAC_STATUS) & (MAC_STATUS_SYNC_CHANGED |
Michael Chan3d3ebe72006-09-27 15:59:15 -07004792 MAC_STATUS_CFG_CHANGED |
4793 MAC_STATUS_LNKSTATE_CHANGED)) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004794 break;
4795 }
4796
4797 mac_status = tr32(MAC_STATUS);
4798 if ((mac_status & MAC_STATUS_PCS_SYNCED) == 0) {
4799 current_link_up = 0;
Michael Chan3d3ebe72006-09-27 15:59:15 -07004800 if (tp->link_config.autoneg == AUTONEG_ENABLE &&
4801 tp->serdes_counter == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004802 tw32_f(MAC_MODE, (tp->mac_mode |
4803 MAC_MODE_SEND_CONFIGS));
4804 udelay(1);
4805 tw32_f(MAC_MODE, tp->mac_mode);
4806 }
4807 }
4808
4809 if (current_link_up == 1) {
4810 tp->link_config.active_speed = SPEED_1000;
4811 tp->link_config.active_duplex = DUPLEX_FULL;
4812 tw32(MAC_LED_CTRL, (tp->led_ctrl |
4813 LED_CTRL_LNKLED_OVERRIDE |
4814 LED_CTRL_1000MBPS_ON));
4815 } else {
4816 tp->link_config.active_speed = SPEED_INVALID;
4817 tp->link_config.active_duplex = DUPLEX_INVALID;
4818 tw32(MAC_LED_CTRL, (tp->led_ctrl |
4819 LED_CTRL_LNKLED_OVERRIDE |
4820 LED_CTRL_TRAFFIC_OVERRIDE));
4821 }
4822
4823 if (current_link_up != netif_carrier_ok(tp->dev)) {
4824 if (current_link_up)
4825 netif_carrier_on(tp->dev);
4826 else
4827 netif_carrier_off(tp->dev);
4828 tg3_link_report(tp);
4829 } else {
Matt Carlson8d018622007-12-20 20:05:44 -08004830 u32 now_pause_cfg = tp->link_config.active_flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004831 if (orig_pause_cfg != now_pause_cfg ||
4832 orig_active_speed != tp->link_config.active_speed ||
4833 orig_active_duplex != tp->link_config.active_duplex)
4834 tg3_link_report(tp);
4835 }
4836
4837 return 0;
4838}
4839
Michael Chan747e8f82005-07-25 12:33:22 -07004840static int tg3_setup_fiber_mii_phy(struct tg3 *tp, int force_reset)
4841{
4842 int current_link_up, err = 0;
4843 u32 bmsr, bmcr;
4844 u16 current_speed;
4845 u8 current_duplex;
Matt Carlsonef167e22007-12-20 20:10:01 -08004846 u32 local_adv, remote_adv;
Michael Chan747e8f82005-07-25 12:33:22 -07004847
4848 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
4849 tw32_f(MAC_MODE, tp->mac_mode);
4850 udelay(40);
4851
4852 tw32(MAC_EVENT, 0);
4853
4854 tw32_f(MAC_STATUS,
4855 (MAC_STATUS_SYNC_CHANGED |
4856 MAC_STATUS_CFG_CHANGED |
4857 MAC_STATUS_MI_COMPLETION |
4858 MAC_STATUS_LNKSTATE_CHANGED));
4859 udelay(40);
4860
4861 if (force_reset)
4862 tg3_phy_reset(tp);
4863
4864 current_link_up = 0;
4865 current_speed = SPEED_INVALID;
4866 current_duplex = DUPLEX_INVALID;
4867
4868 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
4869 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
Michael Chand4d2c552006-03-20 17:47:20 -08004870 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
4871 if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
4872 bmsr |= BMSR_LSTATUS;
4873 else
4874 bmsr &= ~BMSR_LSTATUS;
4875 }
Michael Chan747e8f82005-07-25 12:33:22 -07004876
4877 err |= tg3_readphy(tp, MII_BMCR, &bmcr);
4878
4879 if ((tp->link_config.autoneg == AUTONEG_ENABLE) && !force_reset &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004880 (tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT)) {
Michael Chan747e8f82005-07-25 12:33:22 -07004881 /* do nothing, just check for link up at the end */
4882 } else if (tp->link_config.autoneg == AUTONEG_ENABLE) {
Matt Carlson28011cf2011-11-16 18:36:59 -05004883 u32 adv, newadv;
Michael Chan747e8f82005-07-25 12:33:22 -07004884
4885 err |= tg3_readphy(tp, MII_ADVERTISE, &adv);
Matt Carlson28011cf2011-11-16 18:36:59 -05004886 newadv = adv & ~(ADVERTISE_1000XFULL | ADVERTISE_1000XHALF |
4887 ADVERTISE_1000XPAUSE |
4888 ADVERTISE_1000XPSE_ASYM |
4889 ADVERTISE_SLCT);
Michael Chan747e8f82005-07-25 12:33:22 -07004890
Matt Carlson28011cf2011-11-16 18:36:59 -05004891 newadv |= tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
Matt Carlson37f07022011-11-17 14:30:55 +00004892 newadv |= ethtool_adv_to_mii_adv_x(tp->link_config.advertising);
Michael Chan747e8f82005-07-25 12:33:22 -07004893
Matt Carlson28011cf2011-11-16 18:36:59 -05004894 if ((newadv != adv) || !(bmcr & BMCR_ANENABLE)) {
4895 tg3_writephy(tp, MII_ADVERTISE, newadv);
Michael Chan747e8f82005-07-25 12:33:22 -07004896 bmcr |= BMCR_ANENABLE | BMCR_ANRESTART;
4897 tg3_writephy(tp, MII_BMCR, bmcr);
4898
4899 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
Michael Chan3d3ebe72006-09-27 15:59:15 -07004900 tp->serdes_counter = SERDES_AN_TIMEOUT_5714S;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004901 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07004902
4903 return err;
4904 }
4905 } else {
4906 u32 new_bmcr;
4907
4908 bmcr &= ~BMCR_SPEED1000;
4909 new_bmcr = bmcr & ~(BMCR_ANENABLE | BMCR_FULLDPLX);
4910
4911 if (tp->link_config.duplex == DUPLEX_FULL)
4912 new_bmcr |= BMCR_FULLDPLX;
4913
4914 if (new_bmcr != bmcr) {
4915 /* BMCR_SPEED1000 is a reserved bit that needs
4916 * to be set on write.
4917 */
4918 new_bmcr |= BMCR_SPEED1000;
4919
4920 /* Force a linkdown */
4921 if (netif_carrier_ok(tp->dev)) {
4922 u32 adv;
4923
4924 err |= tg3_readphy(tp, MII_ADVERTISE, &adv);
4925 adv &= ~(ADVERTISE_1000XFULL |
4926 ADVERTISE_1000XHALF |
4927 ADVERTISE_SLCT);
4928 tg3_writephy(tp, MII_ADVERTISE, adv);
4929 tg3_writephy(tp, MII_BMCR, bmcr |
4930 BMCR_ANRESTART |
4931 BMCR_ANENABLE);
4932 udelay(10);
4933 netif_carrier_off(tp->dev);
4934 }
4935 tg3_writephy(tp, MII_BMCR, new_bmcr);
4936 bmcr = new_bmcr;
4937 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
4938 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
Michael Chand4d2c552006-03-20 17:47:20 -08004939 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
4940 ASIC_REV_5714) {
4941 if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
4942 bmsr |= BMSR_LSTATUS;
4943 else
4944 bmsr &= ~BMSR_LSTATUS;
4945 }
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004946 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07004947 }
4948 }
4949
4950 if (bmsr & BMSR_LSTATUS) {
4951 current_speed = SPEED_1000;
4952 current_link_up = 1;
4953 if (bmcr & BMCR_FULLDPLX)
4954 current_duplex = DUPLEX_FULL;
4955 else
4956 current_duplex = DUPLEX_HALF;
4957
Matt Carlsonef167e22007-12-20 20:10:01 -08004958 local_adv = 0;
4959 remote_adv = 0;
4960
Michael Chan747e8f82005-07-25 12:33:22 -07004961 if (bmcr & BMCR_ANENABLE) {
Matt Carlsonef167e22007-12-20 20:10:01 -08004962 u32 common;
Michael Chan747e8f82005-07-25 12:33:22 -07004963
4964 err |= tg3_readphy(tp, MII_ADVERTISE, &local_adv);
4965 err |= tg3_readphy(tp, MII_LPA, &remote_adv);
4966 common = local_adv & remote_adv;
4967 if (common & (ADVERTISE_1000XHALF |
4968 ADVERTISE_1000XFULL)) {
4969 if (common & ADVERTISE_1000XFULL)
4970 current_duplex = DUPLEX_FULL;
4971 else
4972 current_duplex = DUPLEX_HALF;
Joe Perches63c3a662011-04-26 08:12:10 +00004973 } else if (!tg3_flag(tp, 5780_CLASS)) {
Matt Carlson57d8b882010-06-05 17:24:35 +00004974 /* Link is up via parallel detect */
Matt Carlson859a588792010-04-05 10:19:28 +00004975 } else {
Michael Chan747e8f82005-07-25 12:33:22 -07004976 current_link_up = 0;
Matt Carlson859a588792010-04-05 10:19:28 +00004977 }
Michael Chan747e8f82005-07-25 12:33:22 -07004978 }
4979 }
4980
Matt Carlsonef167e22007-12-20 20:10:01 -08004981 if (current_link_up == 1 && current_duplex == DUPLEX_FULL)
4982 tg3_setup_flow_control(tp, local_adv, remote_adv);
4983
Michael Chan747e8f82005-07-25 12:33:22 -07004984 tp->mac_mode &= ~MAC_MODE_HALF_DUPLEX;
4985 if (tp->link_config.active_duplex == DUPLEX_HALF)
4986 tp->mac_mode |= MAC_MODE_HALF_DUPLEX;
4987
4988 tw32_f(MAC_MODE, tp->mac_mode);
4989 udelay(40);
4990
4991 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
4992
4993 tp->link_config.active_speed = current_speed;
4994 tp->link_config.active_duplex = current_duplex;
4995
4996 if (current_link_up != netif_carrier_ok(tp->dev)) {
4997 if (current_link_up)
4998 netif_carrier_on(tp->dev);
4999 else {
5000 netif_carrier_off(tp->dev);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005001 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07005002 }
5003 tg3_link_report(tp);
5004 }
5005 return err;
5006}
5007
5008static void tg3_serdes_parallel_detect(struct tg3 *tp)
5009{
Michael Chan3d3ebe72006-09-27 15:59:15 -07005010 if (tp->serdes_counter) {
Michael Chan747e8f82005-07-25 12:33:22 -07005011 /* Give autoneg time to complete. */
Michael Chan3d3ebe72006-09-27 15:59:15 -07005012 tp->serdes_counter--;
Michael Chan747e8f82005-07-25 12:33:22 -07005013 return;
5014 }
Matt Carlsonc6cdf432010-04-05 10:19:26 +00005015
Michael Chan747e8f82005-07-25 12:33:22 -07005016 if (!netif_carrier_ok(tp->dev) &&
5017 (tp->link_config.autoneg == AUTONEG_ENABLE)) {
5018 u32 bmcr;
5019
5020 tg3_readphy(tp, MII_BMCR, &bmcr);
5021 if (bmcr & BMCR_ANENABLE) {
5022 u32 phy1, phy2;
5023
5024 /* Select shadow register 0x1f */
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00005025 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x7c00);
5026 tg3_readphy(tp, MII_TG3_MISC_SHDW, &phy1);
Michael Chan747e8f82005-07-25 12:33:22 -07005027
5028 /* Select expansion interrupt status register */
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00005029 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
5030 MII_TG3_DSP_EXP1_INT_STAT);
5031 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &phy2);
5032 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &phy2);
Michael Chan747e8f82005-07-25 12:33:22 -07005033
5034 if ((phy1 & 0x10) && !(phy2 & 0x20)) {
5035 /* We have signal detect and not receiving
5036 * config code words, link is up by parallel
5037 * detection.
5038 */
5039
5040 bmcr &= ~BMCR_ANENABLE;
5041 bmcr |= BMCR_SPEED1000 | BMCR_FULLDPLX;
5042 tg3_writephy(tp, MII_BMCR, bmcr);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005043 tp->phy_flags |= TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07005044 }
5045 }
Matt Carlson859a588792010-04-05 10:19:28 +00005046 } else if (netif_carrier_ok(tp->dev) &&
5047 (tp->link_config.autoneg == AUTONEG_ENABLE) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005048 (tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT)) {
Michael Chan747e8f82005-07-25 12:33:22 -07005049 u32 phy2;
5050
5051 /* Select expansion interrupt status register */
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00005052 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
5053 MII_TG3_DSP_EXP1_INT_STAT);
5054 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &phy2);
Michael Chan747e8f82005-07-25 12:33:22 -07005055 if (phy2 & 0x20) {
5056 u32 bmcr;
5057
5058 /* Config code words received, turn on autoneg. */
5059 tg3_readphy(tp, MII_BMCR, &bmcr);
5060 tg3_writephy(tp, MII_BMCR, bmcr | BMCR_ANENABLE);
5061
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005062 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07005063
5064 }
5065 }
5066}
5067
Linus Torvalds1da177e2005-04-16 15:20:36 -07005068static int tg3_setup_phy(struct tg3 *tp, int force_reset)
5069{
Matt Carlsonf2096f92011-04-05 14:22:48 +00005070 u32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005071 int err;
5072
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005073 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005074 err = tg3_setup_fiber_phy(tp, force_reset);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005075 else if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
Michael Chan747e8f82005-07-25 12:33:22 -07005076 err = tg3_setup_fiber_mii_phy(tp, force_reset);
Matt Carlson859a588792010-04-05 10:19:28 +00005077 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07005078 err = tg3_setup_copper_phy(tp, force_reset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005079
Matt Carlsonbcb37f62008-11-03 16:52:09 -08005080 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX) {
Matt Carlsonf2096f92011-04-05 14:22:48 +00005081 u32 scale;
Matt Carlsonaa6c91f2007-11-12 21:18:04 -08005082
5083 val = tr32(TG3_CPMU_CLCK_STAT) & CPMU_CLCK_STAT_MAC_CLCK_MASK;
5084 if (val == CPMU_CLCK_STAT_MAC_CLCK_62_5)
5085 scale = 65;
5086 else if (val == CPMU_CLCK_STAT_MAC_CLCK_6_25)
5087 scale = 6;
5088 else
5089 scale = 12;
5090
5091 val = tr32(GRC_MISC_CFG) & ~GRC_MISC_CFG_PRESCALAR_MASK;
5092 val |= (scale << GRC_MISC_CFG_PRESCALAR_SHIFT);
5093 tw32(GRC_MISC_CFG, val);
5094 }
5095
Matt Carlsonf2096f92011-04-05 14:22:48 +00005096 val = (2 << TX_LENGTHS_IPG_CRS_SHIFT) |
5097 (6 << TX_LENGTHS_IPG_SHIFT);
5098 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
5099 val |= tr32(MAC_TX_LENGTHS) &
5100 (TX_LENGTHS_JMB_FRM_LEN_MSK |
5101 TX_LENGTHS_CNT_DWN_VAL_MSK);
5102
Linus Torvalds1da177e2005-04-16 15:20:36 -07005103 if (tp->link_config.active_speed == SPEED_1000 &&
5104 tp->link_config.active_duplex == DUPLEX_HALF)
Matt Carlsonf2096f92011-04-05 14:22:48 +00005105 tw32(MAC_TX_LENGTHS, val |
5106 (0xff << TX_LENGTHS_SLOT_TIME_SHIFT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005107 else
Matt Carlsonf2096f92011-04-05 14:22:48 +00005108 tw32(MAC_TX_LENGTHS, val |
5109 (32 << TX_LENGTHS_SLOT_TIME_SHIFT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005110
Joe Perches63c3a662011-04-26 08:12:10 +00005111 if (!tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005112 if (netif_carrier_ok(tp->dev)) {
5113 tw32(HOSTCC_STAT_COAL_TICKS,
David S. Miller15f98502005-05-18 22:49:26 -07005114 tp->coal.stats_block_coalesce_usecs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005115 } else {
5116 tw32(HOSTCC_STAT_COAL_TICKS, 0);
5117 }
5118 }
5119
Joe Perches63c3a662011-04-26 08:12:10 +00005120 if (tg3_flag(tp, ASPM_WORKAROUND)) {
Matt Carlsonf2096f92011-04-05 14:22:48 +00005121 val = tr32(PCIE_PWR_MGMT_THRESH);
Matt Carlson8ed5d972007-05-07 00:25:49 -07005122 if (!netif_carrier_ok(tp->dev))
5123 val = (val & ~PCIE_PWR_MGMT_L1_THRESH_MSK) |
5124 tp->pwrmgmt_thresh;
5125 else
5126 val |= PCIE_PWR_MGMT_L1_THRESH_MSK;
5127 tw32(PCIE_PWR_MGMT_THRESH, val);
5128 }
5129
Linus Torvalds1da177e2005-04-16 15:20:36 -07005130 return err;
5131}
5132
Matt Carlson66cfd1b2010-09-30 10:34:30 +00005133static inline int tg3_irq_sync(struct tg3 *tp)
5134{
5135 return tp->irq_sync;
5136}
5137
Matt Carlson97bd8e42011-04-13 11:05:04 +00005138static inline void tg3_rd32_loop(struct tg3 *tp, u32 *dst, u32 off, u32 len)
5139{
5140 int i;
5141
5142 dst = (u32 *)((u8 *)dst + off);
5143 for (i = 0; i < len; i += sizeof(u32))
5144 *dst++ = tr32(off + i);
5145}
5146
5147static void tg3_dump_legacy_regs(struct tg3 *tp, u32 *regs)
5148{
5149 tg3_rd32_loop(tp, regs, TG3PCI_VENDOR, 0xb0);
5150 tg3_rd32_loop(tp, regs, MAILBOX_INTERRUPT_0, 0x200);
5151 tg3_rd32_loop(tp, regs, MAC_MODE, 0x4f0);
5152 tg3_rd32_loop(tp, regs, SNDDATAI_MODE, 0xe0);
5153 tg3_rd32_loop(tp, regs, SNDDATAC_MODE, 0x04);
5154 tg3_rd32_loop(tp, regs, SNDBDS_MODE, 0x80);
5155 tg3_rd32_loop(tp, regs, SNDBDI_MODE, 0x48);
5156 tg3_rd32_loop(tp, regs, SNDBDC_MODE, 0x04);
5157 tg3_rd32_loop(tp, regs, RCVLPC_MODE, 0x20);
5158 tg3_rd32_loop(tp, regs, RCVLPC_SELLST_BASE, 0x15c);
5159 tg3_rd32_loop(tp, regs, RCVDBDI_MODE, 0x0c);
5160 tg3_rd32_loop(tp, regs, RCVDBDI_JUMBO_BD, 0x3c);
5161 tg3_rd32_loop(tp, regs, RCVDBDI_BD_PROD_IDX_0, 0x44);
5162 tg3_rd32_loop(tp, regs, RCVDCC_MODE, 0x04);
5163 tg3_rd32_loop(tp, regs, RCVBDI_MODE, 0x20);
5164 tg3_rd32_loop(tp, regs, RCVCC_MODE, 0x14);
5165 tg3_rd32_loop(tp, regs, RCVLSC_MODE, 0x08);
5166 tg3_rd32_loop(tp, regs, MBFREE_MODE, 0x08);
5167 tg3_rd32_loop(tp, regs, HOSTCC_MODE, 0x100);
5168
Joe Perches63c3a662011-04-26 08:12:10 +00005169 if (tg3_flag(tp, SUPPORT_MSIX))
Matt Carlson97bd8e42011-04-13 11:05:04 +00005170 tg3_rd32_loop(tp, regs, HOSTCC_RXCOL_TICKS_VEC1, 0x180);
5171
5172 tg3_rd32_loop(tp, regs, MEMARB_MODE, 0x10);
5173 tg3_rd32_loop(tp, regs, BUFMGR_MODE, 0x58);
5174 tg3_rd32_loop(tp, regs, RDMAC_MODE, 0x08);
5175 tg3_rd32_loop(tp, regs, WDMAC_MODE, 0x08);
5176 tg3_rd32_loop(tp, regs, RX_CPU_MODE, 0x04);
5177 tg3_rd32_loop(tp, regs, RX_CPU_STATE, 0x04);
5178 tg3_rd32_loop(tp, regs, RX_CPU_PGMCTR, 0x04);
5179 tg3_rd32_loop(tp, regs, RX_CPU_HWBKPT, 0x04);
5180
Joe Perches63c3a662011-04-26 08:12:10 +00005181 if (!tg3_flag(tp, 5705_PLUS)) {
Matt Carlson97bd8e42011-04-13 11:05:04 +00005182 tg3_rd32_loop(tp, regs, TX_CPU_MODE, 0x04);
5183 tg3_rd32_loop(tp, regs, TX_CPU_STATE, 0x04);
5184 tg3_rd32_loop(tp, regs, TX_CPU_PGMCTR, 0x04);
5185 }
5186
5187 tg3_rd32_loop(tp, regs, GRCMBOX_INTERRUPT_0, 0x110);
5188 tg3_rd32_loop(tp, regs, FTQ_RESET, 0x120);
5189 tg3_rd32_loop(tp, regs, MSGINT_MODE, 0x0c);
5190 tg3_rd32_loop(tp, regs, DMAC_MODE, 0x04);
5191 tg3_rd32_loop(tp, regs, GRC_MODE, 0x4c);
5192
Joe Perches63c3a662011-04-26 08:12:10 +00005193 if (tg3_flag(tp, NVRAM))
Matt Carlson97bd8e42011-04-13 11:05:04 +00005194 tg3_rd32_loop(tp, regs, NVRAM_CMD, 0x24);
5195}
5196
5197static void tg3_dump_state(struct tg3 *tp)
5198{
5199 int i;
5200 u32 *regs;
5201
5202 regs = kzalloc(TG3_REG_BLK_SIZE, GFP_ATOMIC);
5203 if (!regs) {
5204 netdev_err(tp->dev, "Failed allocating register dump buffer\n");
5205 return;
5206 }
5207
Joe Perches63c3a662011-04-26 08:12:10 +00005208 if (tg3_flag(tp, PCI_EXPRESS)) {
Matt Carlson97bd8e42011-04-13 11:05:04 +00005209 /* Read up to but not including private PCI registers */
5210 for (i = 0; i < TG3_PCIE_TLDLPL_PORT; i += sizeof(u32))
5211 regs[i / sizeof(u32)] = tr32(i);
5212 } else
5213 tg3_dump_legacy_regs(tp, regs);
5214
5215 for (i = 0; i < TG3_REG_BLK_SIZE / sizeof(u32); i += 4) {
5216 if (!regs[i + 0] && !regs[i + 1] &&
5217 !regs[i + 2] && !regs[i + 3])
5218 continue;
5219
5220 netdev_err(tp->dev, "0x%08x: 0x%08x, 0x%08x, 0x%08x, 0x%08x\n",
5221 i * 4,
5222 regs[i + 0], regs[i + 1], regs[i + 2], regs[i + 3]);
5223 }
5224
5225 kfree(regs);
5226
5227 for (i = 0; i < tp->irq_cnt; i++) {
5228 struct tg3_napi *tnapi = &tp->napi[i];
5229
5230 /* SW status block */
5231 netdev_err(tp->dev,
5232 "%d: Host status block [%08x:%08x:(%04x:%04x:%04x):(%04x:%04x)]\n",
5233 i,
5234 tnapi->hw_status->status,
5235 tnapi->hw_status->status_tag,
5236 tnapi->hw_status->rx_jumbo_consumer,
5237 tnapi->hw_status->rx_consumer,
5238 tnapi->hw_status->rx_mini_consumer,
5239 tnapi->hw_status->idx[0].rx_producer,
5240 tnapi->hw_status->idx[0].tx_consumer);
5241
5242 netdev_err(tp->dev,
5243 "%d: NAPI info [%08x:%08x:(%04x:%04x:%04x):%04x:(%04x:%04x:%04x:%04x)]\n",
5244 i,
5245 tnapi->last_tag, tnapi->last_irq_tag,
5246 tnapi->tx_prod, tnapi->tx_cons, tnapi->tx_pending,
5247 tnapi->rx_rcb_ptr,
5248 tnapi->prodring.rx_std_prod_idx,
5249 tnapi->prodring.rx_std_cons_idx,
5250 tnapi->prodring.rx_jmb_prod_idx,
5251 tnapi->prodring.rx_jmb_cons_idx);
5252 }
5253}
5254
Michael Chandf3e6542006-05-26 17:48:07 -07005255/* This is called whenever we suspect that the system chipset is re-
5256 * ordering the sequence of MMIO to the tx send mailbox. The symptom
5257 * is bogus tx completions. We try to recover by setting the
5258 * TG3_FLAG_MBOX_WRITE_REORDER flag and resetting the chip later
5259 * in the workqueue.
5260 */
5261static void tg3_tx_recover(struct tg3 *tp)
5262{
Joe Perches63c3a662011-04-26 08:12:10 +00005263 BUG_ON(tg3_flag(tp, MBOX_WRITE_REORDER) ||
Michael Chandf3e6542006-05-26 17:48:07 -07005264 tp->write32_tx_mbox == tg3_write_indirect_mbox);
5265
Matt Carlson5129c3a2010-04-05 10:19:23 +00005266 netdev_warn(tp->dev,
5267 "The system may be re-ordering memory-mapped I/O "
5268 "cycles to the network device, attempting to recover. "
5269 "Please report the problem to the driver maintainer "
5270 "and include system chipset information.\n");
Michael Chandf3e6542006-05-26 17:48:07 -07005271
5272 spin_lock(&tp->lock);
Joe Perches63c3a662011-04-26 08:12:10 +00005273 tg3_flag_set(tp, TX_RECOVERY_PENDING);
Michael Chandf3e6542006-05-26 17:48:07 -07005274 spin_unlock(&tp->lock);
5275}
5276
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005277static inline u32 tg3_tx_avail(struct tg3_napi *tnapi)
Michael Chan1b2a7202006-08-07 21:46:02 -07005278{
Matt Carlsonf65aac12010-08-02 11:26:03 +00005279 /* Tell compiler to fetch tx indices from memory. */
5280 barrier();
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005281 return tnapi->tx_pending -
5282 ((tnapi->tx_prod - tnapi->tx_cons) & (TG3_TX_RING_SIZE - 1));
Michael Chan1b2a7202006-08-07 21:46:02 -07005283}
5284
Linus Torvalds1da177e2005-04-16 15:20:36 -07005285/* Tigon3 never reports partial packet sends. So we do not
5286 * need special logic to handle SKBs that have not had all
5287 * of their frags sent yet, like SunGEM does.
5288 */
Matt Carlson17375d22009-08-28 14:02:18 +00005289static void tg3_tx(struct tg3_napi *tnapi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005290{
Matt Carlson17375d22009-08-28 14:02:18 +00005291 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00005292 u32 hw_idx = tnapi->hw_status->idx[0].tx_consumer;
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005293 u32 sw_idx = tnapi->tx_cons;
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005294 struct netdev_queue *txq;
5295 int index = tnapi - tp->napi;
5296
Joe Perches63c3a662011-04-26 08:12:10 +00005297 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005298 index--;
5299
5300 txq = netdev_get_tx_queue(tp->dev, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005301
5302 while (sw_idx != hw_idx) {
Matt Carlsondf8944c2011-07-27 14:20:46 +00005303 struct tg3_tx_ring_info *ri = &tnapi->tx_buffers[sw_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07005304 struct sk_buff *skb = ri->skb;
Michael Chandf3e6542006-05-26 17:48:07 -07005305 int i, tx_bug = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005306
Michael Chandf3e6542006-05-26 17:48:07 -07005307 if (unlikely(skb == NULL)) {
5308 tg3_tx_recover(tp);
5309 return;
5310 }
5311
Alexander Duyckf4188d82009-12-02 16:48:38 +00005312 pci_unmap_single(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005313 dma_unmap_addr(ri, mapping),
Alexander Duyckf4188d82009-12-02 16:48:38 +00005314 skb_headlen(skb),
5315 PCI_DMA_TODEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005316
5317 ri->skb = NULL;
5318
Matt Carlsone01ee142011-07-27 14:20:50 +00005319 while (ri->fragmented) {
5320 ri->fragmented = false;
5321 sw_idx = NEXT_TX(sw_idx);
5322 ri = &tnapi->tx_buffers[sw_idx];
5323 }
5324
Linus Torvalds1da177e2005-04-16 15:20:36 -07005325 sw_idx = NEXT_TX(sw_idx);
5326
5327 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005328 ri = &tnapi->tx_buffers[sw_idx];
Michael Chandf3e6542006-05-26 17:48:07 -07005329 if (unlikely(ri->skb != NULL || sw_idx == hw_idx))
5330 tx_bug = 1;
Alexander Duyckf4188d82009-12-02 16:48:38 +00005331
5332 pci_unmap_page(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005333 dma_unmap_addr(ri, mapping),
Eric Dumazet9e903e02011-10-18 21:00:24 +00005334 skb_frag_size(&skb_shinfo(skb)->frags[i]),
Alexander Duyckf4188d82009-12-02 16:48:38 +00005335 PCI_DMA_TODEVICE);
Matt Carlsone01ee142011-07-27 14:20:50 +00005336
5337 while (ri->fragmented) {
5338 ri->fragmented = false;
5339 sw_idx = NEXT_TX(sw_idx);
5340 ri = &tnapi->tx_buffers[sw_idx];
5341 }
5342
Linus Torvalds1da177e2005-04-16 15:20:36 -07005343 sw_idx = NEXT_TX(sw_idx);
5344 }
5345
David S. Millerf47c11e2005-06-24 20:18:35 -07005346 dev_kfree_skb(skb);
Michael Chandf3e6542006-05-26 17:48:07 -07005347
5348 if (unlikely(tx_bug)) {
5349 tg3_tx_recover(tp);
5350 return;
5351 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005352 }
5353
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005354 tnapi->tx_cons = sw_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005355
Michael Chan1b2a7202006-08-07 21:46:02 -07005356 /* Need to make the tx_cons update visible to tg3_start_xmit()
5357 * before checking for netif_queue_stopped(). Without the
5358 * memory barrier, there is a small possibility that tg3_start_xmit()
5359 * will miss it and cause the queue to be stopped forever.
5360 */
5361 smp_mb();
5362
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005363 if (unlikely(netif_tx_queue_stopped(txq) &&
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005364 (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi)))) {
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005365 __netif_tx_lock(txq, smp_processor_id());
5366 if (netif_tx_queue_stopped(txq) &&
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005367 (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi)))
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005368 netif_tx_wake_queue(txq);
5369 __netif_tx_unlock(txq);
Michael Chan51b91462005-09-01 17:41:28 -07005370 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005371}
5372
Eric Dumazet9205fd92011-11-18 06:47:01 +00005373static void tg3_rx_data_free(struct tg3 *tp, struct ring_info *ri, u32 map_sz)
Matt Carlson2b2cdb62009-11-13 13:03:48 +00005374{
Eric Dumazet9205fd92011-11-18 06:47:01 +00005375 if (!ri->data)
Matt Carlson2b2cdb62009-11-13 13:03:48 +00005376 return;
5377
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005378 pci_unmap_single(tp->pdev, dma_unmap_addr(ri, mapping),
Matt Carlson2b2cdb62009-11-13 13:03:48 +00005379 map_sz, PCI_DMA_FROMDEVICE);
Eric Dumazet9205fd92011-11-18 06:47:01 +00005380 kfree(ri->data);
5381 ri->data = NULL;
Matt Carlson2b2cdb62009-11-13 13:03:48 +00005382}
5383
Linus Torvalds1da177e2005-04-16 15:20:36 -07005384/* Returns size of skb allocated or < 0 on error.
5385 *
5386 * We only need to fill in the address because the other members
5387 * of the RX descriptor are invariant, see tg3_init_rings.
5388 *
5389 * Note the purposeful assymetry of cpu vs. chip accesses. For
5390 * posting buffers we only dirty the first cache line of the RX
5391 * descriptor (containing the address). Whereas for the RX status
5392 * buffers the cpu only reads the last cacheline of the RX descriptor
5393 * (to fetch the error flags, vlan tag, checksum, and opaque cookie).
5394 */
Eric Dumazet9205fd92011-11-18 06:47:01 +00005395static int tg3_alloc_rx_data(struct tg3 *tp, struct tg3_rx_prodring_set *tpr,
Matt Carlsona3896162009-11-13 13:03:44 +00005396 u32 opaque_key, u32 dest_idx_unmasked)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005397{
5398 struct tg3_rx_buffer_desc *desc;
Matt Carlsonf94e2902010-10-14 10:37:42 +00005399 struct ring_info *map;
Eric Dumazet9205fd92011-11-18 06:47:01 +00005400 u8 *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005401 dma_addr_t mapping;
Eric Dumazet9205fd92011-11-18 06:47:01 +00005402 int skb_size, data_size, dest_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005403
Linus Torvalds1da177e2005-04-16 15:20:36 -07005404 switch (opaque_key) {
5405 case RXD_OPAQUE_RING_STD:
Matt Carlson2c49a442010-09-30 10:34:35 +00005406 dest_idx = dest_idx_unmasked & tp->rx_std_ring_mask;
Matt Carlson21f581a2009-08-28 14:00:25 +00005407 desc = &tpr->rx_std[dest_idx];
5408 map = &tpr->rx_std_buffers[dest_idx];
Eric Dumazet9205fd92011-11-18 06:47:01 +00005409 data_size = tp->rx_pkt_map_sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005410 break;
5411
5412 case RXD_OPAQUE_RING_JUMBO:
Matt Carlson2c49a442010-09-30 10:34:35 +00005413 dest_idx = dest_idx_unmasked & tp->rx_jmb_ring_mask;
Matt Carlson79ed5ac2009-08-28 14:00:55 +00005414 desc = &tpr->rx_jmb[dest_idx].std;
Matt Carlson21f581a2009-08-28 14:00:25 +00005415 map = &tpr->rx_jmb_buffers[dest_idx];
Eric Dumazet9205fd92011-11-18 06:47:01 +00005416 data_size = TG3_RX_JMB_MAP_SZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005417 break;
5418
5419 default:
5420 return -EINVAL;
Stephen Hemminger855e1112008-04-16 16:37:28 -07005421 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005422
5423 /* Do not overwrite any of the map or rp information
5424 * until we are sure we can commit to a new buffer.
5425 *
5426 * Callers depend upon this behavior and assume that
5427 * we leave everything unchanged if we fail.
5428 */
Eric Dumazet9205fd92011-11-18 06:47:01 +00005429 skb_size = SKB_DATA_ALIGN(data_size + TG3_RX_OFFSET(tp)) +
5430 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
5431 data = kmalloc(skb_size, GFP_ATOMIC);
5432 if (!data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005433 return -ENOMEM;
5434
Eric Dumazet9205fd92011-11-18 06:47:01 +00005435 mapping = pci_map_single(tp->pdev,
5436 data + TG3_RX_OFFSET(tp),
5437 data_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005438 PCI_DMA_FROMDEVICE);
Matt Carlsona21771d2009-11-02 14:25:31 +00005439 if (pci_dma_mapping_error(tp->pdev, mapping)) {
Eric Dumazet9205fd92011-11-18 06:47:01 +00005440 kfree(data);
Matt Carlsona21771d2009-11-02 14:25:31 +00005441 return -EIO;
5442 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005443
Eric Dumazet9205fd92011-11-18 06:47:01 +00005444 map->data = data;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005445 dma_unmap_addr_set(map, mapping, mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005446
Linus Torvalds1da177e2005-04-16 15:20:36 -07005447 desc->addr_hi = ((u64)mapping >> 32);
5448 desc->addr_lo = ((u64)mapping & 0xffffffff);
5449
Eric Dumazet9205fd92011-11-18 06:47:01 +00005450 return data_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005451}
5452
5453/* We only need to move over in the address because the other
5454 * members of the RX descriptor are invariant. See notes above
Eric Dumazet9205fd92011-11-18 06:47:01 +00005455 * tg3_alloc_rx_data for full details.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005456 */
Matt Carlsona3896162009-11-13 13:03:44 +00005457static void tg3_recycle_rx(struct tg3_napi *tnapi,
5458 struct tg3_rx_prodring_set *dpr,
5459 u32 opaque_key, int src_idx,
5460 u32 dest_idx_unmasked)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005461{
Matt Carlson17375d22009-08-28 14:02:18 +00005462 struct tg3 *tp = tnapi->tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005463 struct tg3_rx_buffer_desc *src_desc, *dest_desc;
5464 struct ring_info *src_map, *dest_map;
Matt Carlson8fea32b2010-09-15 08:59:58 +00005465 struct tg3_rx_prodring_set *spr = &tp->napi[0].prodring;
Matt Carlsonc6cdf432010-04-05 10:19:26 +00005466 int dest_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005467
5468 switch (opaque_key) {
5469 case RXD_OPAQUE_RING_STD:
Matt Carlson2c49a442010-09-30 10:34:35 +00005470 dest_idx = dest_idx_unmasked & tp->rx_std_ring_mask;
Matt Carlsona3896162009-11-13 13:03:44 +00005471 dest_desc = &dpr->rx_std[dest_idx];
5472 dest_map = &dpr->rx_std_buffers[dest_idx];
5473 src_desc = &spr->rx_std[src_idx];
5474 src_map = &spr->rx_std_buffers[src_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07005475 break;
5476
5477 case RXD_OPAQUE_RING_JUMBO:
Matt Carlson2c49a442010-09-30 10:34:35 +00005478 dest_idx = dest_idx_unmasked & tp->rx_jmb_ring_mask;
Matt Carlsona3896162009-11-13 13:03:44 +00005479 dest_desc = &dpr->rx_jmb[dest_idx].std;
5480 dest_map = &dpr->rx_jmb_buffers[dest_idx];
5481 src_desc = &spr->rx_jmb[src_idx].std;
5482 src_map = &spr->rx_jmb_buffers[src_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07005483 break;
5484
5485 default:
5486 return;
Stephen Hemminger855e1112008-04-16 16:37:28 -07005487 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005488
Eric Dumazet9205fd92011-11-18 06:47:01 +00005489 dest_map->data = src_map->data;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005490 dma_unmap_addr_set(dest_map, mapping,
5491 dma_unmap_addr(src_map, mapping));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005492 dest_desc->addr_hi = src_desc->addr_hi;
5493 dest_desc->addr_lo = src_desc->addr_lo;
Matt Carlsone92967b2010-02-12 14:47:06 +00005494
5495 /* Ensure that the update to the skb happens after the physical
5496 * addresses have been transferred to the new BD location.
5497 */
5498 smp_wmb();
5499
Eric Dumazet9205fd92011-11-18 06:47:01 +00005500 src_map->data = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005501}
5502
Linus Torvalds1da177e2005-04-16 15:20:36 -07005503/* The RX ring scheme is composed of multiple rings which post fresh
5504 * buffers to the chip, and one special ring the chip uses to report
5505 * status back to the host.
5506 *
5507 * The special ring reports the status of received packets to the
5508 * host. The chip does not write into the original descriptor the
5509 * RX buffer was obtained from. The chip simply takes the original
5510 * descriptor as provided by the host, updates the status and length
5511 * field, then writes this into the next status ring entry.
5512 *
5513 * Each ring the host uses to post buffers to the chip is described
5514 * by a TG3_BDINFO entry in the chips SRAM area. When a packet arrives,
5515 * it is first placed into the on-chip ram. When the packet's length
5516 * is known, it walks down the TG3_BDINFO entries to select the ring.
5517 * Each TG3_BDINFO specifies a MAXLEN field and the first TG3_BDINFO
5518 * which is within the range of the new packet's length is chosen.
5519 *
5520 * The "separate ring for rx status" scheme may sound queer, but it makes
5521 * sense from a cache coherency perspective. If only the host writes
5522 * to the buffer post rings, and only the chip writes to the rx status
5523 * rings, then cache lines never move beyond shared-modified state.
5524 * If both the host and chip were to write into the same ring, cache line
5525 * eviction could occur since both entities want it in an exclusive state.
5526 */
Matt Carlson17375d22009-08-28 14:02:18 +00005527static int tg3_rx(struct tg3_napi *tnapi, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005528{
Matt Carlson17375d22009-08-28 14:02:18 +00005529 struct tg3 *tp = tnapi->tp;
Michael Chanf92905d2006-06-29 20:14:29 -07005530 u32 work_mask, rx_std_posted = 0;
Matt Carlson43619352009-11-13 13:03:47 +00005531 u32 std_prod_idx, jmb_prod_idx;
Matt Carlson72334482009-08-28 14:03:01 +00005532 u32 sw_idx = tnapi->rx_rcb_ptr;
Michael Chan483ba502005-04-25 15:14:03 -07005533 u16 hw_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005534 int received;
Matt Carlson8fea32b2010-09-15 08:59:58 +00005535 struct tg3_rx_prodring_set *tpr = &tnapi->prodring;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005536
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00005537 hw_idx = *(tnapi->rx_rcb_prod_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005538 /*
5539 * We need to order the read of hw_idx and the read of
5540 * the opaque cookie.
5541 */
5542 rmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005543 work_mask = 0;
5544 received = 0;
Matt Carlson43619352009-11-13 13:03:47 +00005545 std_prod_idx = tpr->rx_std_prod_idx;
5546 jmb_prod_idx = tpr->rx_jmb_prod_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005547 while (sw_idx != hw_idx && budget > 0) {
Matt Carlsonafc081f2009-11-13 13:03:43 +00005548 struct ring_info *ri;
Matt Carlson72334482009-08-28 14:03:01 +00005549 struct tg3_rx_buffer_desc *desc = &tnapi->rx_rcb[sw_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07005550 unsigned int len;
5551 struct sk_buff *skb;
5552 dma_addr_t dma_addr;
5553 u32 opaque_key, desc_idx, *post_ptr;
Eric Dumazet9205fd92011-11-18 06:47:01 +00005554 u8 *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005555
5556 desc_idx = desc->opaque & RXD_OPAQUE_INDEX_MASK;
5557 opaque_key = desc->opaque & RXD_OPAQUE_RING_MASK;
5558 if (opaque_key == RXD_OPAQUE_RING_STD) {
Matt Carlson8fea32b2010-09-15 08:59:58 +00005559 ri = &tp->napi[0].prodring.rx_std_buffers[desc_idx];
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005560 dma_addr = dma_unmap_addr(ri, mapping);
Eric Dumazet9205fd92011-11-18 06:47:01 +00005561 data = ri->data;
Matt Carlson43619352009-11-13 13:03:47 +00005562 post_ptr = &std_prod_idx;
Michael Chanf92905d2006-06-29 20:14:29 -07005563 rx_std_posted++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005564 } else if (opaque_key == RXD_OPAQUE_RING_JUMBO) {
Matt Carlson8fea32b2010-09-15 08:59:58 +00005565 ri = &tp->napi[0].prodring.rx_jmb_buffers[desc_idx];
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005566 dma_addr = dma_unmap_addr(ri, mapping);
Eric Dumazet9205fd92011-11-18 06:47:01 +00005567 data = ri->data;
Matt Carlson43619352009-11-13 13:03:47 +00005568 post_ptr = &jmb_prod_idx;
Matt Carlson21f581a2009-08-28 14:00:25 +00005569 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07005570 goto next_pkt_nopost;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005571
5572 work_mask |= opaque_key;
5573
5574 if ((desc->err_vlan & RXD_ERR_MASK) != 0 &&
5575 (desc->err_vlan != RXD_ERR_ODD_NIBBLE_RCVD_MII)) {
5576 drop_it:
Matt Carlsona3896162009-11-13 13:03:44 +00005577 tg3_recycle_rx(tnapi, tpr, opaque_key,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005578 desc_idx, *post_ptr);
5579 drop_it_no_recycle:
5580 /* Other statistics kept track of by card. */
Eric Dumazetb0057c52010-10-10 19:55:52 +00005581 tp->rx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005582 goto next_pkt;
5583 }
5584
Eric Dumazet9205fd92011-11-18 06:47:01 +00005585 prefetch(data + TG3_RX_OFFSET(tp));
Matt Carlsonad829262008-11-21 17:16:16 -08005586 len = ((desc->idx_len & RXD_LEN_MASK) >> RXD_LEN_SHIFT) -
5587 ETH_FCS_LEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005588
Matt Carlsond2757fc2010-04-12 06:58:27 +00005589 if (len > TG3_RX_COPY_THRESH(tp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005590 int skb_size;
5591
Eric Dumazet9205fd92011-11-18 06:47:01 +00005592 skb_size = tg3_alloc_rx_data(tp, tpr, opaque_key,
Matt Carlsonafc081f2009-11-13 13:03:43 +00005593 *post_ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005594 if (skb_size < 0)
5595 goto drop_it;
5596
Matt Carlson287be122009-08-28 13:58:46 +00005597 pci_unmap_single(tp->pdev, dma_addr, skb_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005598 PCI_DMA_FROMDEVICE);
5599
Eric Dumazet9205fd92011-11-18 06:47:01 +00005600 skb = build_skb(data);
5601 if (!skb) {
5602 kfree(data);
5603 goto drop_it_no_recycle;
5604 }
5605 skb_reserve(skb, TG3_RX_OFFSET(tp));
5606 /* Ensure that the update to the data happens
Matt Carlson61e800c2010-02-17 15:16:54 +00005607 * after the usage of the old DMA mapping.
5608 */
5609 smp_wmb();
5610
Eric Dumazet9205fd92011-11-18 06:47:01 +00005611 ri->data = NULL;
Matt Carlson61e800c2010-02-17 15:16:54 +00005612
Linus Torvalds1da177e2005-04-16 15:20:36 -07005613 } else {
Matt Carlsona3896162009-11-13 13:03:44 +00005614 tg3_recycle_rx(tnapi, tpr, opaque_key,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005615 desc_idx, *post_ptr);
5616
Eric Dumazet9205fd92011-11-18 06:47:01 +00005617 skb = netdev_alloc_skb(tp->dev,
5618 len + TG3_RAW_IP_ALIGN);
5619 if (skb == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005620 goto drop_it_no_recycle;
5621
Eric Dumazet9205fd92011-11-18 06:47:01 +00005622 skb_reserve(skb, TG3_RAW_IP_ALIGN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005623 pci_dma_sync_single_for_cpu(tp->pdev, dma_addr, len, PCI_DMA_FROMDEVICE);
Eric Dumazet9205fd92011-11-18 06:47:01 +00005624 memcpy(skb->data,
5625 data + TG3_RX_OFFSET(tp),
5626 len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005627 pci_dma_sync_single_for_device(tp->pdev, dma_addr, len, PCI_DMA_FROMDEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005628 }
5629
Eric Dumazet9205fd92011-11-18 06:47:01 +00005630 skb_put(skb, len);
Michał Mirosławdc668912011-04-07 03:35:07 +00005631 if ((tp->dev->features & NETIF_F_RXCSUM) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005632 (desc->type_flags & RXD_FLAG_TCPUDP_CSUM) &&
5633 (((desc->ip_tcp_csum & RXD_TCPCSUM_MASK)
5634 >> RXD_TCPCSUM_SHIFT) == 0xffff))
5635 skb->ip_summed = CHECKSUM_UNNECESSARY;
5636 else
Eric Dumazetbc8acf22010-09-02 13:07:41 -07005637 skb_checksum_none_assert(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005638
5639 skb->protocol = eth_type_trans(skb, tp->dev);
Matt Carlsonf7b493e2009-02-25 14:21:52 +00005640
5641 if (len > (tp->dev->mtu + ETH_HLEN) &&
5642 skb->protocol != htons(ETH_P_8021Q)) {
5643 dev_kfree_skb(skb);
Eric Dumazetb0057c52010-10-10 19:55:52 +00005644 goto drop_it_no_recycle;
Matt Carlsonf7b493e2009-02-25 14:21:52 +00005645 }
5646
Matt Carlson9dc7a112010-04-12 06:58:28 +00005647 if (desc->type_flags & RXD_FLAG_VLAN &&
Matt Carlsonbf933c82011-01-25 15:58:49 +00005648 !(tp->rx_mode & RX_MODE_KEEP_VLAN_TAG))
5649 __vlan_hwaccel_put_tag(skb,
5650 desc->err_vlan & RXD_VLAN_MASK);
Matt Carlson9dc7a112010-04-12 06:58:28 +00005651
Matt Carlsonbf933c82011-01-25 15:58:49 +00005652 napi_gro_receive(&tnapi->napi, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005653
Linus Torvalds1da177e2005-04-16 15:20:36 -07005654 received++;
5655 budget--;
5656
5657next_pkt:
5658 (*post_ptr)++;
Michael Chanf92905d2006-06-29 20:14:29 -07005659
5660 if (unlikely(rx_std_posted >= tp->rx_std_max_post)) {
Matt Carlson2c49a442010-09-30 10:34:35 +00005661 tpr->rx_std_prod_idx = std_prod_idx &
5662 tp->rx_std_ring_mask;
Matt Carlson86cfe4f2010-01-12 10:11:37 +00005663 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
5664 tpr->rx_std_prod_idx);
Michael Chanf92905d2006-06-29 20:14:29 -07005665 work_mask &= ~RXD_OPAQUE_RING_STD;
5666 rx_std_posted = 0;
5667 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005668next_pkt_nopost:
Michael Chan483ba502005-04-25 15:14:03 -07005669 sw_idx++;
Matt Carlson7cb32cf2010-09-30 10:34:36 +00005670 sw_idx &= tp->rx_ret_ring_mask;
Michael Chan52f6d692005-04-25 15:14:32 -07005671
5672 /* Refresh hw_idx to see if there is new work */
5673 if (sw_idx == hw_idx) {
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00005674 hw_idx = *(tnapi->rx_rcb_prod_idx);
Michael Chan52f6d692005-04-25 15:14:32 -07005675 rmb();
5676 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005677 }
5678
5679 /* ACK the status ring. */
Matt Carlson72334482009-08-28 14:03:01 +00005680 tnapi->rx_rcb_ptr = sw_idx;
5681 tw32_rx_mbox(tnapi->consmbox, sw_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005682
5683 /* Refill RX ring(s). */
Joe Perches63c3a662011-04-26 08:12:10 +00005684 if (!tg3_flag(tp, ENABLE_RSS)) {
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005685 if (work_mask & RXD_OPAQUE_RING_STD) {
Matt Carlson2c49a442010-09-30 10:34:35 +00005686 tpr->rx_std_prod_idx = std_prod_idx &
5687 tp->rx_std_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005688 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
5689 tpr->rx_std_prod_idx);
5690 }
5691 if (work_mask & RXD_OPAQUE_RING_JUMBO) {
Matt Carlson2c49a442010-09-30 10:34:35 +00005692 tpr->rx_jmb_prod_idx = jmb_prod_idx &
5693 tp->rx_jmb_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005694 tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG,
5695 tpr->rx_jmb_prod_idx);
5696 }
5697 mmiowb();
5698 } else if (work_mask) {
5699 /* rx_std_buffers[] and rx_jmb_buffers[] entries must be
5700 * updated before the producer indices can be updated.
5701 */
5702 smp_wmb();
5703
Matt Carlson2c49a442010-09-30 10:34:35 +00005704 tpr->rx_std_prod_idx = std_prod_idx & tp->rx_std_ring_mask;
5705 tpr->rx_jmb_prod_idx = jmb_prod_idx & tp->rx_jmb_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005706
Matt Carlsone4af1af2010-02-12 14:47:05 +00005707 if (tnapi != &tp->napi[1])
5708 napi_schedule(&tp->napi[1].napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005709 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005710
5711 return received;
5712}
5713
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005714static void tg3_poll_link(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005715{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005716 /* handle link change and other phy events */
Joe Perches63c3a662011-04-26 08:12:10 +00005717 if (!(tg3_flag(tp, USE_LINKCHG_REG) || tg3_flag(tp, POLL_SERDES))) {
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005718 struct tg3_hw_status *sblk = tp->napi[0].hw_status;
5719
Linus Torvalds1da177e2005-04-16 15:20:36 -07005720 if (sblk->status & SD_STATUS_LINK_CHG) {
5721 sblk->status = SD_STATUS_UPDATED |
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005722 (sblk->status & ~SD_STATUS_LINK_CHG);
David S. Millerf47c11e2005-06-24 20:18:35 -07005723 spin_lock(&tp->lock);
Joe Perches63c3a662011-04-26 08:12:10 +00005724 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlsondd477002008-05-25 23:45:58 -07005725 tw32_f(MAC_STATUS,
5726 (MAC_STATUS_SYNC_CHANGED |
5727 MAC_STATUS_CFG_CHANGED |
5728 MAC_STATUS_MI_COMPLETION |
5729 MAC_STATUS_LNKSTATE_CHANGED));
5730 udelay(40);
5731 } else
5732 tg3_setup_phy(tp, 0);
David S. Millerf47c11e2005-06-24 20:18:35 -07005733 spin_unlock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005734 }
5735 }
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005736}
5737
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005738static int tg3_rx_prodring_xfer(struct tg3 *tp,
5739 struct tg3_rx_prodring_set *dpr,
5740 struct tg3_rx_prodring_set *spr)
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005741{
5742 u32 si, di, cpycnt, src_prod_idx;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005743 int i, err = 0;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005744
5745 while (1) {
5746 src_prod_idx = spr->rx_std_prod_idx;
5747
5748 /* Make sure updates to the rx_std_buffers[] entries and the
5749 * standard producer index are seen in the correct order.
5750 */
5751 smp_rmb();
5752
5753 if (spr->rx_std_cons_idx == src_prod_idx)
5754 break;
5755
5756 if (spr->rx_std_cons_idx < src_prod_idx)
5757 cpycnt = src_prod_idx - spr->rx_std_cons_idx;
5758 else
Matt Carlson2c49a442010-09-30 10:34:35 +00005759 cpycnt = tp->rx_std_ring_mask + 1 -
5760 spr->rx_std_cons_idx;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005761
Matt Carlson2c49a442010-09-30 10:34:35 +00005762 cpycnt = min(cpycnt,
5763 tp->rx_std_ring_mask + 1 - dpr->rx_std_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005764
5765 si = spr->rx_std_cons_idx;
5766 di = dpr->rx_std_prod_idx;
5767
Matt Carlsone92967b2010-02-12 14:47:06 +00005768 for (i = di; i < di + cpycnt; i++) {
Eric Dumazet9205fd92011-11-18 06:47:01 +00005769 if (dpr->rx_std_buffers[i].data) {
Matt Carlsone92967b2010-02-12 14:47:06 +00005770 cpycnt = i - di;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005771 err = -ENOSPC;
Matt Carlsone92967b2010-02-12 14:47:06 +00005772 break;
5773 }
5774 }
5775
5776 if (!cpycnt)
5777 break;
5778
5779 /* Ensure that updates to the rx_std_buffers ring and the
5780 * shadowed hardware producer ring from tg3_recycle_skb() are
5781 * ordered correctly WRT the skb check above.
5782 */
5783 smp_rmb();
5784
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005785 memcpy(&dpr->rx_std_buffers[di],
5786 &spr->rx_std_buffers[si],
5787 cpycnt * sizeof(struct ring_info));
5788
5789 for (i = 0; i < cpycnt; i++, di++, si++) {
5790 struct tg3_rx_buffer_desc *sbd, *dbd;
5791 sbd = &spr->rx_std[si];
5792 dbd = &dpr->rx_std[di];
5793 dbd->addr_hi = sbd->addr_hi;
5794 dbd->addr_lo = sbd->addr_lo;
5795 }
5796
Matt Carlson2c49a442010-09-30 10:34:35 +00005797 spr->rx_std_cons_idx = (spr->rx_std_cons_idx + cpycnt) &
5798 tp->rx_std_ring_mask;
5799 dpr->rx_std_prod_idx = (dpr->rx_std_prod_idx + cpycnt) &
5800 tp->rx_std_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005801 }
5802
5803 while (1) {
5804 src_prod_idx = spr->rx_jmb_prod_idx;
5805
5806 /* Make sure updates to the rx_jmb_buffers[] entries and
5807 * the jumbo producer index are seen in the correct order.
5808 */
5809 smp_rmb();
5810
5811 if (spr->rx_jmb_cons_idx == src_prod_idx)
5812 break;
5813
5814 if (spr->rx_jmb_cons_idx < src_prod_idx)
5815 cpycnt = src_prod_idx - spr->rx_jmb_cons_idx;
5816 else
Matt Carlson2c49a442010-09-30 10:34:35 +00005817 cpycnt = tp->rx_jmb_ring_mask + 1 -
5818 spr->rx_jmb_cons_idx;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005819
5820 cpycnt = min(cpycnt,
Matt Carlson2c49a442010-09-30 10:34:35 +00005821 tp->rx_jmb_ring_mask + 1 - dpr->rx_jmb_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005822
5823 si = spr->rx_jmb_cons_idx;
5824 di = dpr->rx_jmb_prod_idx;
5825
Matt Carlsone92967b2010-02-12 14:47:06 +00005826 for (i = di; i < di + cpycnt; i++) {
Eric Dumazet9205fd92011-11-18 06:47:01 +00005827 if (dpr->rx_jmb_buffers[i].data) {
Matt Carlsone92967b2010-02-12 14:47:06 +00005828 cpycnt = i - di;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005829 err = -ENOSPC;
Matt Carlsone92967b2010-02-12 14:47:06 +00005830 break;
5831 }
5832 }
5833
5834 if (!cpycnt)
5835 break;
5836
5837 /* Ensure that updates to the rx_jmb_buffers ring and the
5838 * shadowed hardware producer ring from tg3_recycle_skb() are
5839 * ordered correctly WRT the skb check above.
5840 */
5841 smp_rmb();
5842
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005843 memcpy(&dpr->rx_jmb_buffers[di],
5844 &spr->rx_jmb_buffers[si],
5845 cpycnt * sizeof(struct ring_info));
5846
5847 for (i = 0; i < cpycnt; i++, di++, si++) {
5848 struct tg3_rx_buffer_desc *sbd, *dbd;
5849 sbd = &spr->rx_jmb[si].std;
5850 dbd = &dpr->rx_jmb[di].std;
5851 dbd->addr_hi = sbd->addr_hi;
5852 dbd->addr_lo = sbd->addr_lo;
5853 }
5854
Matt Carlson2c49a442010-09-30 10:34:35 +00005855 spr->rx_jmb_cons_idx = (spr->rx_jmb_cons_idx + cpycnt) &
5856 tp->rx_jmb_ring_mask;
5857 dpr->rx_jmb_prod_idx = (dpr->rx_jmb_prod_idx + cpycnt) &
5858 tp->rx_jmb_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005859 }
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005860
5861 return err;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005862}
5863
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005864static int tg3_poll_work(struct tg3_napi *tnapi, int work_done, int budget)
5865{
5866 struct tg3 *tp = tnapi->tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005867
5868 /* run TX completion thread */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005869 if (tnapi->hw_status->idx[0].tx_consumer != tnapi->tx_cons) {
Matt Carlson17375d22009-08-28 14:02:18 +00005870 tg3_tx(tnapi);
Joe Perches63c3a662011-04-26 08:12:10 +00005871 if (unlikely(tg3_flag(tp, TX_RECOVERY_PENDING)))
Michael Chan4fd7ab52007-10-12 01:39:50 -07005872 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005873 }
5874
Linus Torvalds1da177e2005-04-16 15:20:36 -07005875 /* run RX thread, within the bounds set by NAPI.
5876 * All RX "locking" is done by ensuring outside
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005877 * code synchronizes with tg3->napi.poll()
Linus Torvalds1da177e2005-04-16 15:20:36 -07005878 */
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00005879 if (*(tnapi->rx_rcb_prod_idx) != tnapi->rx_rcb_ptr)
Matt Carlson17375d22009-08-28 14:02:18 +00005880 work_done += tg3_rx(tnapi, budget - work_done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005881
Joe Perches63c3a662011-04-26 08:12:10 +00005882 if (tg3_flag(tp, ENABLE_RSS) && tnapi == &tp->napi[1]) {
Matt Carlson8fea32b2010-09-15 08:59:58 +00005883 struct tg3_rx_prodring_set *dpr = &tp->napi[0].prodring;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005884 int i, err = 0;
Matt Carlsone4af1af2010-02-12 14:47:05 +00005885 u32 std_prod_idx = dpr->rx_std_prod_idx;
5886 u32 jmb_prod_idx = dpr->rx_jmb_prod_idx;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005887
Matt Carlsone4af1af2010-02-12 14:47:05 +00005888 for (i = 1; i < tp->irq_cnt; i++)
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005889 err |= tg3_rx_prodring_xfer(tp, dpr,
Matt Carlson8fea32b2010-09-15 08:59:58 +00005890 &tp->napi[i].prodring);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005891
5892 wmb();
5893
Matt Carlsone4af1af2010-02-12 14:47:05 +00005894 if (std_prod_idx != dpr->rx_std_prod_idx)
5895 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
5896 dpr->rx_std_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005897
Matt Carlsone4af1af2010-02-12 14:47:05 +00005898 if (jmb_prod_idx != dpr->rx_jmb_prod_idx)
5899 tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG,
5900 dpr->rx_jmb_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005901
5902 mmiowb();
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005903
5904 if (err)
5905 tw32_f(HOSTCC_MODE, tp->coal_now);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005906 }
5907
David S. Miller6f535762007-10-11 18:08:29 -07005908 return work_done;
5909}
David S. Millerf7383c22005-05-18 22:50:53 -07005910
Matt Carlsondb219972011-11-04 09:15:03 +00005911static inline void tg3_reset_task_schedule(struct tg3 *tp)
5912{
5913 if (!test_and_set_bit(TG3_FLAG_RESET_TASK_PENDING, tp->tg3_flags))
5914 schedule_work(&tp->reset_task);
5915}
5916
5917static inline void tg3_reset_task_cancel(struct tg3 *tp)
5918{
5919 cancel_work_sync(&tp->reset_task);
5920 tg3_flag_clear(tp, RESET_TASK_PENDING);
5921}
5922
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005923static int tg3_poll_msix(struct napi_struct *napi, int budget)
5924{
5925 struct tg3_napi *tnapi = container_of(napi, struct tg3_napi, napi);
5926 struct tg3 *tp = tnapi->tp;
5927 int work_done = 0;
5928 struct tg3_hw_status *sblk = tnapi->hw_status;
5929
5930 while (1) {
5931 work_done = tg3_poll_work(tnapi, work_done, budget);
5932
Joe Perches63c3a662011-04-26 08:12:10 +00005933 if (unlikely(tg3_flag(tp, TX_RECOVERY_PENDING)))
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005934 goto tx_recovery;
5935
5936 if (unlikely(work_done >= budget))
5937 break;
5938
Matt Carlsonc6cdf432010-04-05 10:19:26 +00005939 /* tp->last_tag is used in tg3_int_reenable() below
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005940 * to tell the hw how much work has been processed,
5941 * so we must read it before checking for more work.
5942 */
5943 tnapi->last_tag = sblk->status_tag;
5944 tnapi->last_irq_tag = tnapi->last_tag;
5945 rmb();
5946
5947 /* check for RX/TX work to do */
Matt Carlson6d40db72010-04-05 10:19:20 +00005948 if (likely(sblk->idx[0].tx_consumer == tnapi->tx_cons &&
5949 *(tnapi->rx_rcb_prod_idx) == tnapi->rx_rcb_ptr)) {
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005950 napi_complete(napi);
5951 /* Reenable interrupts. */
5952 tw32_mailbox(tnapi->int_mbox, tnapi->last_tag << 24);
5953 mmiowb();
5954 break;
5955 }
5956 }
5957
5958 return work_done;
5959
5960tx_recovery:
5961 /* work_done is guaranteed to be less than budget. */
5962 napi_complete(napi);
Matt Carlsondb219972011-11-04 09:15:03 +00005963 tg3_reset_task_schedule(tp);
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005964 return work_done;
5965}
5966
Matt Carlsone64de4e2011-04-13 11:05:05 +00005967static void tg3_process_error(struct tg3 *tp)
5968{
5969 u32 val;
5970 bool real_error = false;
5971
Joe Perches63c3a662011-04-26 08:12:10 +00005972 if (tg3_flag(tp, ERROR_PROCESSED))
Matt Carlsone64de4e2011-04-13 11:05:05 +00005973 return;
5974
5975 /* Check Flow Attention register */
5976 val = tr32(HOSTCC_FLOW_ATTN);
5977 if (val & ~HOSTCC_FLOW_ATTN_MBUF_LWM) {
5978 netdev_err(tp->dev, "FLOW Attention error. Resetting chip.\n");
5979 real_error = true;
5980 }
5981
5982 if (tr32(MSGINT_STATUS) & ~MSGINT_STATUS_MSI_REQ) {
5983 netdev_err(tp->dev, "MSI Status error. Resetting chip.\n");
5984 real_error = true;
5985 }
5986
5987 if (tr32(RDMAC_STATUS) || tr32(WDMAC_STATUS)) {
5988 netdev_err(tp->dev, "DMA Status error. Resetting chip.\n");
5989 real_error = true;
5990 }
5991
5992 if (!real_error)
5993 return;
5994
5995 tg3_dump_state(tp);
5996
Joe Perches63c3a662011-04-26 08:12:10 +00005997 tg3_flag_set(tp, ERROR_PROCESSED);
Matt Carlsondb219972011-11-04 09:15:03 +00005998 tg3_reset_task_schedule(tp);
Matt Carlsone64de4e2011-04-13 11:05:05 +00005999}
6000
David S. Miller6f535762007-10-11 18:08:29 -07006001static int tg3_poll(struct napi_struct *napi, int budget)
6002{
Matt Carlson8ef04422009-08-28 14:01:37 +00006003 struct tg3_napi *tnapi = container_of(napi, struct tg3_napi, napi);
6004 struct tg3 *tp = tnapi->tp;
David S. Miller6f535762007-10-11 18:08:29 -07006005 int work_done = 0;
Matt Carlson898a56f2009-08-28 14:02:40 +00006006 struct tg3_hw_status *sblk = tnapi->hw_status;
David S. Miller6f535762007-10-11 18:08:29 -07006007
6008 while (1) {
Matt Carlsone64de4e2011-04-13 11:05:05 +00006009 if (sblk->status & SD_STATUS_ERROR)
6010 tg3_process_error(tp);
6011
Matt Carlson35f2d7d2009-11-13 13:03:41 +00006012 tg3_poll_link(tp);
6013
Matt Carlson17375d22009-08-28 14:02:18 +00006014 work_done = tg3_poll_work(tnapi, work_done, budget);
David S. Miller6f535762007-10-11 18:08:29 -07006015
Joe Perches63c3a662011-04-26 08:12:10 +00006016 if (unlikely(tg3_flag(tp, TX_RECOVERY_PENDING)))
David S. Miller6f535762007-10-11 18:08:29 -07006017 goto tx_recovery;
6018
6019 if (unlikely(work_done >= budget))
6020 break;
6021
Joe Perches63c3a662011-04-26 08:12:10 +00006022 if (tg3_flag(tp, TAGGED_STATUS)) {
Matt Carlson17375d22009-08-28 14:02:18 +00006023 /* tp->last_tag is used in tg3_int_reenable() below
Michael Chan4fd7ab52007-10-12 01:39:50 -07006024 * to tell the hw how much work has been processed,
6025 * so we must read it before checking for more work.
6026 */
Matt Carlson898a56f2009-08-28 14:02:40 +00006027 tnapi->last_tag = sblk->status_tag;
6028 tnapi->last_irq_tag = tnapi->last_tag;
Michael Chan4fd7ab52007-10-12 01:39:50 -07006029 rmb();
6030 } else
6031 sblk->status &= ~SD_STATUS_UPDATED;
6032
Matt Carlson17375d22009-08-28 14:02:18 +00006033 if (likely(!tg3_has_work(tnapi))) {
Ben Hutchings288379f2009-01-19 16:43:59 -08006034 napi_complete(napi);
Matt Carlson17375d22009-08-28 14:02:18 +00006035 tg3_int_reenable(tnapi);
David S. Miller6f535762007-10-11 18:08:29 -07006036 break;
6037 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006038 }
6039
Stephen Hemmingerbea33482007-10-03 16:41:36 -07006040 return work_done;
David S. Miller6f535762007-10-11 18:08:29 -07006041
6042tx_recovery:
Michael Chan4fd7ab52007-10-12 01:39:50 -07006043 /* work_done is guaranteed to be less than budget. */
Ben Hutchings288379f2009-01-19 16:43:59 -08006044 napi_complete(napi);
Matt Carlsondb219972011-11-04 09:15:03 +00006045 tg3_reset_task_schedule(tp);
Michael Chan4fd7ab52007-10-12 01:39:50 -07006046 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006047}
6048
Matt Carlson66cfd1b2010-09-30 10:34:30 +00006049static void tg3_napi_disable(struct tg3 *tp)
6050{
6051 int i;
6052
6053 for (i = tp->irq_cnt - 1; i >= 0; i--)
6054 napi_disable(&tp->napi[i].napi);
6055}
6056
6057static void tg3_napi_enable(struct tg3 *tp)
6058{
6059 int i;
6060
6061 for (i = 0; i < tp->irq_cnt; i++)
6062 napi_enable(&tp->napi[i].napi);
6063}
6064
6065static void tg3_napi_init(struct tg3 *tp)
6066{
6067 int i;
6068
6069 netif_napi_add(tp->dev, &tp->napi[0].napi, tg3_poll, 64);
6070 for (i = 1; i < tp->irq_cnt; i++)
6071 netif_napi_add(tp->dev, &tp->napi[i].napi, tg3_poll_msix, 64);
6072}
6073
6074static void tg3_napi_fini(struct tg3 *tp)
6075{
6076 int i;
6077
6078 for (i = 0; i < tp->irq_cnt; i++)
6079 netif_napi_del(&tp->napi[i].napi);
6080}
6081
6082static inline void tg3_netif_stop(struct tg3 *tp)
6083{
6084 tp->dev->trans_start = jiffies; /* prevent tx timeout */
6085 tg3_napi_disable(tp);
6086 netif_tx_disable(tp->dev);
6087}
6088
6089static inline void tg3_netif_start(struct tg3 *tp)
6090{
6091 /* NOTE: unconditional netif_tx_wake_all_queues is only
6092 * appropriate so long as all callers are assured to
6093 * have free tx slots (such as after tg3_init_hw)
6094 */
6095 netif_tx_wake_all_queues(tp->dev);
6096
6097 tg3_napi_enable(tp);
6098 tp->napi[0].hw_status->status |= SD_STATUS_UPDATED;
6099 tg3_enable_ints(tp);
6100}
6101
David S. Millerf47c11e2005-06-24 20:18:35 -07006102static void tg3_irq_quiesce(struct tg3 *tp)
6103{
Matt Carlson4f125f42009-09-01 12:55:02 +00006104 int i;
6105
David S. Millerf47c11e2005-06-24 20:18:35 -07006106 BUG_ON(tp->irq_sync);
6107
6108 tp->irq_sync = 1;
6109 smp_mb();
6110
Matt Carlson4f125f42009-09-01 12:55:02 +00006111 for (i = 0; i < tp->irq_cnt; i++)
6112 synchronize_irq(tp->napi[i].irq_vec);
David S. Millerf47c11e2005-06-24 20:18:35 -07006113}
6114
David S. Millerf47c11e2005-06-24 20:18:35 -07006115/* Fully shutdown all tg3 driver activity elsewhere in the system.
6116 * If irq_sync is non-zero, then the IRQ handler must be synchronized
6117 * with as well. Most of the time, this is not necessary except when
6118 * shutting down the device.
6119 */
6120static inline void tg3_full_lock(struct tg3 *tp, int irq_sync)
6121{
Michael Chan46966542007-07-11 19:47:19 -07006122 spin_lock_bh(&tp->lock);
David S. Millerf47c11e2005-06-24 20:18:35 -07006123 if (irq_sync)
6124 tg3_irq_quiesce(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -07006125}
6126
6127static inline void tg3_full_unlock(struct tg3 *tp)
6128{
David S. Millerf47c11e2005-06-24 20:18:35 -07006129 spin_unlock_bh(&tp->lock);
6130}
6131
Michael Chanfcfa0a32006-03-20 22:28:41 -08006132/* One-shot MSI handler - Chip automatically disables interrupt
6133 * after sending MSI so driver doesn't have to do it.
6134 */
David Howells7d12e782006-10-05 14:55:46 +01006135static irqreturn_t tg3_msi_1shot(int irq, void *dev_id)
Michael Chanfcfa0a32006-03-20 22:28:41 -08006136{
Matt Carlson09943a12009-08-28 14:01:57 +00006137 struct tg3_napi *tnapi = dev_id;
6138 struct tg3 *tp = tnapi->tp;
Michael Chanfcfa0a32006-03-20 22:28:41 -08006139
Matt Carlson898a56f2009-08-28 14:02:40 +00006140 prefetch(tnapi->hw_status);
Matt Carlson0c1d0e22009-09-01 13:16:33 +00006141 if (tnapi->rx_rcb)
6142 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Michael Chanfcfa0a32006-03-20 22:28:41 -08006143
6144 if (likely(!tg3_irq_sync(tp)))
Matt Carlson09943a12009-08-28 14:01:57 +00006145 napi_schedule(&tnapi->napi);
Michael Chanfcfa0a32006-03-20 22:28:41 -08006146
6147 return IRQ_HANDLED;
6148}
6149
Michael Chan88b06bc22005-04-21 17:13:25 -07006150/* MSI ISR - No need to check for interrupt sharing and no need to
6151 * flush status block and interrupt mailbox. PCI ordering rules
6152 * guarantee that MSI will arrive after the status block.
6153 */
David Howells7d12e782006-10-05 14:55:46 +01006154static irqreturn_t tg3_msi(int irq, void *dev_id)
Michael Chan88b06bc22005-04-21 17:13:25 -07006155{
Matt Carlson09943a12009-08-28 14:01:57 +00006156 struct tg3_napi *tnapi = dev_id;
6157 struct tg3 *tp = tnapi->tp;
Michael Chan88b06bc22005-04-21 17:13:25 -07006158
Matt Carlson898a56f2009-08-28 14:02:40 +00006159 prefetch(tnapi->hw_status);
Matt Carlson0c1d0e22009-09-01 13:16:33 +00006160 if (tnapi->rx_rcb)
6161 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Michael Chan88b06bc22005-04-21 17:13:25 -07006162 /*
David S. Millerfac9b832005-05-18 22:46:34 -07006163 * Writing any value to intr-mbox-0 clears PCI INTA# and
Michael Chan88b06bc22005-04-21 17:13:25 -07006164 * chip-internal interrupt pending events.
David S. Millerfac9b832005-05-18 22:46:34 -07006165 * Writing non-zero to intr-mbox-0 additional tells the
Michael Chan88b06bc22005-04-21 17:13:25 -07006166 * NIC to stop sending us irqs, engaging "in-intr-handler"
6167 * event coalescing.
6168 */
Matt Carlson5b39de92011-08-31 11:44:50 +00006169 tw32_mailbox(tnapi->int_mbox, 0x00000001);
Michael Chan61487482005-09-05 17:53:19 -07006170 if (likely(!tg3_irq_sync(tp)))
Matt Carlson09943a12009-08-28 14:01:57 +00006171 napi_schedule(&tnapi->napi);
Michael Chan61487482005-09-05 17:53:19 -07006172
Michael Chan88b06bc22005-04-21 17:13:25 -07006173 return IRQ_RETVAL(1);
6174}
6175
David Howells7d12e782006-10-05 14:55:46 +01006176static irqreturn_t tg3_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006177{
Matt Carlson09943a12009-08-28 14:01:57 +00006178 struct tg3_napi *tnapi = dev_id;
6179 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00006180 struct tg3_hw_status *sblk = tnapi->hw_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006181 unsigned int handled = 1;
6182
Linus Torvalds1da177e2005-04-16 15:20:36 -07006183 /* In INTx mode, it is possible for the interrupt to arrive at
6184 * the CPU before the status block posted prior to the interrupt.
6185 * Reading the PCI State register will confirm whether the
6186 * interrupt is ours and will flush the status block.
6187 */
Michael Chand18edcb2007-03-24 20:57:11 -07006188 if (unlikely(!(sblk->status & SD_STATUS_UPDATED))) {
Joe Perches63c3a662011-04-26 08:12:10 +00006189 if (tg3_flag(tp, CHIP_RESETTING) ||
Michael Chand18edcb2007-03-24 20:57:11 -07006190 (tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
6191 handled = 0;
David S. Millerf47c11e2005-06-24 20:18:35 -07006192 goto out;
David S. Millerfac9b832005-05-18 22:46:34 -07006193 }
Michael Chand18edcb2007-03-24 20:57:11 -07006194 }
6195
6196 /*
6197 * Writing any value to intr-mbox-0 clears PCI INTA# and
6198 * chip-internal interrupt pending events.
6199 * Writing non-zero to intr-mbox-0 additional tells the
6200 * NIC to stop sending us irqs, engaging "in-intr-handler"
6201 * event coalescing.
Michael Chanc04cb342007-05-07 00:26:15 -07006202 *
6203 * Flush the mailbox to de-assert the IRQ immediately to prevent
6204 * spurious interrupts. The flush impacts performance but
6205 * excessive spurious interrupts can be worse in some cases.
Michael Chand18edcb2007-03-24 20:57:11 -07006206 */
Michael Chanc04cb342007-05-07 00:26:15 -07006207 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 0x00000001);
Michael Chand18edcb2007-03-24 20:57:11 -07006208 if (tg3_irq_sync(tp))
6209 goto out;
6210 sblk->status &= ~SD_STATUS_UPDATED;
Matt Carlson17375d22009-08-28 14:02:18 +00006211 if (likely(tg3_has_work(tnapi))) {
Matt Carlson72334482009-08-28 14:03:01 +00006212 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Matt Carlson09943a12009-08-28 14:01:57 +00006213 napi_schedule(&tnapi->napi);
Michael Chand18edcb2007-03-24 20:57:11 -07006214 } else {
6215 /* No work, shared interrupt perhaps? re-enable
6216 * interrupts, and flush that PCI write
6217 */
6218 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW,
6219 0x00000000);
David S. Millerfac9b832005-05-18 22:46:34 -07006220 }
David S. Millerf47c11e2005-06-24 20:18:35 -07006221out:
David S. Millerfac9b832005-05-18 22:46:34 -07006222 return IRQ_RETVAL(handled);
6223}
6224
David Howells7d12e782006-10-05 14:55:46 +01006225static irqreturn_t tg3_interrupt_tagged(int irq, void *dev_id)
David S. Millerfac9b832005-05-18 22:46:34 -07006226{
Matt Carlson09943a12009-08-28 14:01:57 +00006227 struct tg3_napi *tnapi = dev_id;
6228 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00006229 struct tg3_hw_status *sblk = tnapi->hw_status;
David S. Millerfac9b832005-05-18 22:46:34 -07006230 unsigned int handled = 1;
6231
David S. Millerfac9b832005-05-18 22:46:34 -07006232 /* In INTx mode, it is possible for the interrupt to arrive at
6233 * the CPU before the status block posted prior to the interrupt.
6234 * Reading the PCI State register will confirm whether the
6235 * interrupt is ours and will flush the status block.
6236 */
Matt Carlson898a56f2009-08-28 14:02:40 +00006237 if (unlikely(sblk->status_tag == tnapi->last_irq_tag)) {
Joe Perches63c3a662011-04-26 08:12:10 +00006238 if (tg3_flag(tp, CHIP_RESETTING) ||
Michael Chand18edcb2007-03-24 20:57:11 -07006239 (tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
6240 handled = 0;
David S. Millerf47c11e2005-06-24 20:18:35 -07006241 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006242 }
Michael Chand18edcb2007-03-24 20:57:11 -07006243 }
6244
6245 /*
6246 * writing any value to intr-mbox-0 clears PCI INTA# and
6247 * chip-internal interrupt pending events.
6248 * writing non-zero to intr-mbox-0 additional tells the
6249 * NIC to stop sending us irqs, engaging "in-intr-handler"
6250 * event coalescing.
Michael Chanc04cb342007-05-07 00:26:15 -07006251 *
6252 * Flush the mailbox to de-assert the IRQ immediately to prevent
6253 * spurious interrupts. The flush impacts performance but
6254 * excessive spurious interrupts can be worse in some cases.
Michael Chand18edcb2007-03-24 20:57:11 -07006255 */
Michael Chanc04cb342007-05-07 00:26:15 -07006256 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 0x00000001);
Matt Carlson624f8e52009-04-20 06:55:01 +00006257
6258 /*
6259 * In a shared interrupt configuration, sometimes other devices'
6260 * interrupts will scream. We record the current status tag here
6261 * so that the above check can report that the screaming interrupts
6262 * are unhandled. Eventually they will be silenced.
6263 */
Matt Carlson898a56f2009-08-28 14:02:40 +00006264 tnapi->last_irq_tag = sblk->status_tag;
Matt Carlson624f8e52009-04-20 06:55:01 +00006265
Michael Chand18edcb2007-03-24 20:57:11 -07006266 if (tg3_irq_sync(tp))
6267 goto out;
Matt Carlson624f8e52009-04-20 06:55:01 +00006268
Matt Carlson72334482009-08-28 14:03:01 +00006269 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Matt Carlson624f8e52009-04-20 06:55:01 +00006270
Matt Carlson09943a12009-08-28 14:01:57 +00006271 napi_schedule(&tnapi->napi);
Matt Carlson624f8e52009-04-20 06:55:01 +00006272
David S. Millerf47c11e2005-06-24 20:18:35 -07006273out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006274 return IRQ_RETVAL(handled);
6275}
6276
Michael Chan79381092005-04-21 17:13:59 -07006277/* ISR for interrupt test */
David Howells7d12e782006-10-05 14:55:46 +01006278static irqreturn_t tg3_test_isr(int irq, void *dev_id)
Michael Chan79381092005-04-21 17:13:59 -07006279{
Matt Carlson09943a12009-08-28 14:01:57 +00006280 struct tg3_napi *tnapi = dev_id;
6281 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00006282 struct tg3_hw_status *sblk = tnapi->hw_status;
Michael Chan79381092005-04-21 17:13:59 -07006283
Michael Chanf9804dd2005-09-27 12:13:10 -07006284 if ((sblk->status & SD_STATUS_UPDATED) ||
6285 !(tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
Michael Chanb16250e2006-09-27 16:10:14 -07006286 tg3_disable_ints(tp);
Michael Chan79381092005-04-21 17:13:59 -07006287 return IRQ_RETVAL(1);
6288 }
6289 return IRQ_RETVAL(0);
6290}
6291
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07006292static int tg3_init_hw(struct tg3 *, int);
Michael Chan944d9802005-05-29 14:57:48 -07006293static int tg3_halt(struct tg3 *, int, int);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006294
Michael Chanb9ec6c12006-07-25 16:37:27 -07006295/* Restart hardware after configuration changes, self-test, etc.
6296 * Invoked with tp->lock held.
6297 */
6298static int tg3_restart_hw(struct tg3 *tp, int reset_phy)
Eric Dumazet78c61462008-04-24 23:33:06 -07006299 __releases(tp->lock)
6300 __acquires(tp->lock)
Michael Chanb9ec6c12006-07-25 16:37:27 -07006301{
6302 int err;
6303
6304 err = tg3_init_hw(tp, reset_phy);
6305 if (err) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00006306 netdev_err(tp->dev,
6307 "Failed to re-initialize device, aborting\n");
Michael Chanb9ec6c12006-07-25 16:37:27 -07006308 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
6309 tg3_full_unlock(tp);
6310 del_timer_sync(&tp->timer);
6311 tp->irq_sync = 0;
Matt Carlsonfed97812009-09-01 13:10:19 +00006312 tg3_napi_enable(tp);
Michael Chanb9ec6c12006-07-25 16:37:27 -07006313 dev_close(tp->dev);
6314 tg3_full_lock(tp, 0);
6315 }
6316 return err;
6317}
6318
Linus Torvalds1da177e2005-04-16 15:20:36 -07006319#ifdef CONFIG_NET_POLL_CONTROLLER
6320static void tg3_poll_controller(struct net_device *dev)
6321{
Matt Carlson4f125f42009-09-01 12:55:02 +00006322 int i;
Michael Chan88b06bc22005-04-21 17:13:25 -07006323 struct tg3 *tp = netdev_priv(dev);
6324
Matt Carlson4f125f42009-09-01 12:55:02 +00006325 for (i = 0; i < tp->irq_cnt; i++)
Louis Rillingfe234f02010-03-09 06:14:41 +00006326 tg3_interrupt(tp->napi[i].irq_vec, &tp->napi[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006327}
6328#endif
6329
David Howellsc4028952006-11-22 14:57:56 +00006330static void tg3_reset_task(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006331{
David Howellsc4028952006-11-22 14:57:56 +00006332 struct tg3 *tp = container_of(work, struct tg3, reset_task);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07006333 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006334
Michael Chan7faa0062006-02-02 17:29:28 -08006335 tg3_full_lock(tp, 0);
Michael Chan7faa0062006-02-02 17:29:28 -08006336
6337 if (!netif_running(tp->dev)) {
Matt Carlsondb219972011-11-04 09:15:03 +00006338 tg3_flag_clear(tp, RESET_TASK_PENDING);
Michael Chan7faa0062006-02-02 17:29:28 -08006339 tg3_full_unlock(tp);
6340 return;
6341 }
6342
6343 tg3_full_unlock(tp);
6344
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07006345 tg3_phy_stop(tp);
6346
Linus Torvalds1da177e2005-04-16 15:20:36 -07006347 tg3_netif_stop(tp);
6348
David S. Millerf47c11e2005-06-24 20:18:35 -07006349 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006350
Joe Perches63c3a662011-04-26 08:12:10 +00006351 if (tg3_flag(tp, TX_RECOVERY_PENDING)) {
Michael Chandf3e6542006-05-26 17:48:07 -07006352 tp->write32_tx_mbox = tg3_write32_tx_mbox;
6353 tp->write32_rx_mbox = tg3_write_flush_reg32;
Joe Perches63c3a662011-04-26 08:12:10 +00006354 tg3_flag_set(tp, MBOX_WRITE_REORDER);
6355 tg3_flag_clear(tp, TX_RECOVERY_PENDING);
Michael Chandf3e6542006-05-26 17:48:07 -07006356 }
6357
Michael Chan944d9802005-05-29 14:57:48 -07006358 tg3_halt(tp, RESET_KIND_SHUTDOWN, 0);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07006359 err = tg3_init_hw(tp, 1);
6360 if (err)
Michael Chanb9ec6c12006-07-25 16:37:27 -07006361 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006362
6363 tg3_netif_start(tp);
6364
Michael Chanb9ec6c12006-07-25 16:37:27 -07006365out:
Michael Chan7faa0062006-02-02 17:29:28 -08006366 tg3_full_unlock(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07006367
6368 if (!err)
6369 tg3_phy_start(tp);
Matt Carlsondb219972011-11-04 09:15:03 +00006370
6371 tg3_flag_clear(tp, RESET_TASK_PENDING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006372}
6373
6374static void tg3_tx_timeout(struct net_device *dev)
6375{
6376 struct tg3 *tp = netdev_priv(dev);
6377
Michael Chanb0408752007-02-13 12:18:30 -08006378 if (netif_msg_tx_err(tp)) {
Joe Perches05dbe002010-02-17 19:44:19 +00006379 netdev_err(dev, "transmit timed out, resetting\n");
Matt Carlson97bd8e42011-04-13 11:05:04 +00006380 tg3_dump_state(tp);
Michael Chanb0408752007-02-13 12:18:30 -08006381 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006382
Matt Carlsondb219972011-11-04 09:15:03 +00006383 tg3_reset_task_schedule(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006384}
6385
Michael Chanc58ec932005-09-17 00:46:27 -07006386/* Test for DMA buffers crossing any 4GB boundaries: 4G, 8G, etc */
6387static inline int tg3_4g_overflow_test(dma_addr_t mapping, int len)
6388{
6389 u32 base = (u32) mapping & 0xffffffff;
6390
Eric Dumazet807540b2010-09-23 05:40:09 +00006391 return (base > 0xffffdcc0) && (base + len + 8 < base);
Michael Chanc58ec932005-09-17 00:46:27 -07006392}
6393
Michael Chan72f2afb2006-03-06 19:28:35 -08006394/* Test for DMA addresses > 40-bit */
6395static inline int tg3_40bit_overflow_test(struct tg3 *tp, dma_addr_t mapping,
6396 int len)
6397{
6398#if defined(CONFIG_HIGHMEM) && (BITS_PER_LONG == 64)
Joe Perches63c3a662011-04-26 08:12:10 +00006399 if (tg3_flag(tp, 40BIT_DMA_BUG))
Eric Dumazet807540b2010-09-23 05:40:09 +00006400 return ((u64) mapping + len) > DMA_BIT_MASK(40);
Michael Chan72f2afb2006-03-06 19:28:35 -08006401 return 0;
6402#else
6403 return 0;
6404#endif
6405}
6406
Matt Carlsond1a3b732011-07-27 14:20:51 +00006407static inline void tg3_tx_set_bd(struct tg3_tx_buffer_desc *txbd,
Matt Carlson92cd3a12011-07-27 14:20:47 +00006408 dma_addr_t mapping, u32 len, u32 flags,
6409 u32 mss, u32 vlan)
Matt Carlson2ffcc982011-05-19 12:12:44 +00006410{
Matt Carlson92cd3a12011-07-27 14:20:47 +00006411 txbd->addr_hi = ((u64) mapping >> 32);
6412 txbd->addr_lo = ((u64) mapping & 0xffffffff);
6413 txbd->len_flags = (len << TXD_LEN_SHIFT) | (flags & 0x0000ffff);
6414 txbd->vlan_tag = (mss << TXD_MSS_SHIFT) | (vlan << TXD_VLAN_TAG_SHIFT);
Matt Carlson2ffcc982011-05-19 12:12:44 +00006415}
Linus Torvalds1da177e2005-04-16 15:20:36 -07006416
Matt Carlson84b67b22011-07-27 14:20:52 +00006417static bool tg3_tx_frag_set(struct tg3_napi *tnapi, u32 *entry, u32 *budget,
Matt Carlsond1a3b732011-07-27 14:20:51 +00006418 dma_addr_t map, u32 len, u32 flags,
6419 u32 mss, u32 vlan)
6420{
6421 struct tg3 *tp = tnapi->tp;
6422 bool hwbug = false;
6423
6424 if (tg3_flag(tp, SHORT_DMA_BUG) && len <= 8)
6425 hwbug = 1;
6426
6427 if (tg3_4g_overflow_test(map, len))
6428 hwbug = 1;
6429
6430 if (tg3_40bit_overflow_test(tp, map, len))
6431 hwbug = 1;
6432
Matt Carlsone31aa982011-07-27 14:20:53 +00006433 if (tg3_flag(tp, 4K_FIFO_LIMIT)) {
Matt Carlsonb9e45482011-11-04 09:14:59 +00006434 u32 prvidx = *entry;
Matt Carlsone31aa982011-07-27 14:20:53 +00006435 u32 tmp_flag = flags & ~TXD_FLAG_END;
Matt Carlsonb9e45482011-11-04 09:14:59 +00006436 while (len > TG3_TX_BD_DMA_MAX && *budget) {
Matt Carlsone31aa982011-07-27 14:20:53 +00006437 u32 frag_len = TG3_TX_BD_DMA_MAX;
6438 len -= TG3_TX_BD_DMA_MAX;
6439
Matt Carlsonb9e45482011-11-04 09:14:59 +00006440 /* Avoid the 8byte DMA problem */
6441 if (len <= 8) {
6442 len += TG3_TX_BD_DMA_MAX / 2;
6443 frag_len = TG3_TX_BD_DMA_MAX / 2;
Matt Carlsone31aa982011-07-27 14:20:53 +00006444 }
6445
Matt Carlsonb9e45482011-11-04 09:14:59 +00006446 tnapi->tx_buffers[*entry].fragmented = true;
6447
6448 tg3_tx_set_bd(&tnapi->tx_ring[*entry], map,
6449 frag_len, tmp_flag, mss, vlan);
6450 *budget -= 1;
6451 prvidx = *entry;
6452 *entry = NEXT_TX(*entry);
6453
Matt Carlsone31aa982011-07-27 14:20:53 +00006454 map += frag_len;
6455 }
6456
6457 if (len) {
6458 if (*budget) {
6459 tg3_tx_set_bd(&tnapi->tx_ring[*entry], map,
6460 len, flags, mss, vlan);
Matt Carlsonb9e45482011-11-04 09:14:59 +00006461 *budget -= 1;
Matt Carlsone31aa982011-07-27 14:20:53 +00006462 *entry = NEXT_TX(*entry);
6463 } else {
6464 hwbug = 1;
Matt Carlsonb9e45482011-11-04 09:14:59 +00006465 tnapi->tx_buffers[prvidx].fragmented = false;
Matt Carlsone31aa982011-07-27 14:20:53 +00006466 }
6467 }
6468 } else {
Matt Carlson84b67b22011-07-27 14:20:52 +00006469 tg3_tx_set_bd(&tnapi->tx_ring[*entry], map,
6470 len, flags, mss, vlan);
Matt Carlsone31aa982011-07-27 14:20:53 +00006471 *entry = NEXT_TX(*entry);
6472 }
Matt Carlsond1a3b732011-07-27 14:20:51 +00006473
6474 return hwbug;
6475}
6476
Matt Carlson0d681b22011-07-27 14:20:49 +00006477static void tg3_tx_skb_unmap(struct tg3_napi *tnapi, u32 entry, int last)
Matt Carlson432aa7e2011-05-19 12:12:45 +00006478{
6479 int i;
Matt Carlson0d681b22011-07-27 14:20:49 +00006480 struct sk_buff *skb;
Matt Carlsondf8944c2011-07-27 14:20:46 +00006481 struct tg3_tx_ring_info *txb = &tnapi->tx_buffers[entry];
Matt Carlson432aa7e2011-05-19 12:12:45 +00006482
Matt Carlson0d681b22011-07-27 14:20:49 +00006483 skb = txb->skb;
6484 txb->skb = NULL;
6485
Matt Carlson432aa7e2011-05-19 12:12:45 +00006486 pci_unmap_single(tnapi->tp->pdev,
6487 dma_unmap_addr(txb, mapping),
6488 skb_headlen(skb),
6489 PCI_DMA_TODEVICE);
Matt Carlsone01ee142011-07-27 14:20:50 +00006490
6491 while (txb->fragmented) {
6492 txb->fragmented = false;
6493 entry = NEXT_TX(entry);
6494 txb = &tnapi->tx_buffers[entry];
6495 }
6496
Matt Carlsonba1142e2011-11-04 09:15:00 +00006497 for (i = 0; i <= last; i++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00006498 const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
Matt Carlson432aa7e2011-05-19 12:12:45 +00006499
6500 entry = NEXT_TX(entry);
6501 txb = &tnapi->tx_buffers[entry];
6502
6503 pci_unmap_page(tnapi->tp->pdev,
6504 dma_unmap_addr(txb, mapping),
Eric Dumazet9e903e02011-10-18 21:00:24 +00006505 skb_frag_size(frag), PCI_DMA_TODEVICE);
Matt Carlsone01ee142011-07-27 14:20:50 +00006506
6507 while (txb->fragmented) {
6508 txb->fragmented = false;
6509 entry = NEXT_TX(entry);
6510 txb = &tnapi->tx_buffers[entry];
6511 }
Matt Carlson432aa7e2011-05-19 12:12:45 +00006512 }
6513}
6514
Michael Chan72f2afb2006-03-06 19:28:35 -08006515/* Workaround 4GB and 40-bit hardware DMA bugs. */
Matt Carlson24f4efd2009-11-13 13:03:35 +00006516static int tigon3_dma_hwbug_workaround(struct tg3_napi *tnapi,
David S. Miller1805b2f2011-10-24 18:18:09 -04006517 struct sk_buff **pskb,
Matt Carlson84b67b22011-07-27 14:20:52 +00006518 u32 *entry, u32 *budget,
Matt Carlson92cd3a12011-07-27 14:20:47 +00006519 u32 base_flags, u32 mss, u32 vlan)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006520{
Matt Carlson24f4efd2009-11-13 13:03:35 +00006521 struct tg3 *tp = tnapi->tp;
David S. Miller1805b2f2011-10-24 18:18:09 -04006522 struct sk_buff *new_skb, *skb = *pskb;
Michael Chanc58ec932005-09-17 00:46:27 -07006523 dma_addr_t new_addr = 0;
Matt Carlson432aa7e2011-05-19 12:12:45 +00006524 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006525
Matt Carlson41588ba2008-04-19 18:12:33 -07006526 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701)
6527 new_skb = skb_copy(skb, GFP_ATOMIC);
6528 else {
6529 int more_headroom = 4 - ((unsigned long)skb->data & 3);
6530
6531 new_skb = skb_copy_expand(skb,
6532 skb_headroom(skb) + more_headroom,
6533 skb_tailroom(skb), GFP_ATOMIC);
6534 }
6535
Linus Torvalds1da177e2005-04-16 15:20:36 -07006536 if (!new_skb) {
Michael Chanc58ec932005-09-17 00:46:27 -07006537 ret = -1;
6538 } else {
6539 /* New SKB is guaranteed to be linear. */
Alexander Duyckf4188d82009-12-02 16:48:38 +00006540 new_addr = pci_map_single(tp->pdev, new_skb->data, new_skb->len,
6541 PCI_DMA_TODEVICE);
6542 /* Make sure the mapping succeeded */
6543 if (pci_dma_mapping_error(tp->pdev, new_addr)) {
Alexander Duyckf4188d82009-12-02 16:48:38 +00006544 dev_kfree_skb(new_skb);
Michael Chanc58ec932005-09-17 00:46:27 -07006545 ret = -1;
Michael Chanc58ec932005-09-17 00:46:27 -07006546 } else {
Matt Carlsonb9e45482011-11-04 09:14:59 +00006547 u32 save_entry = *entry;
6548
Matt Carlson92cd3a12011-07-27 14:20:47 +00006549 base_flags |= TXD_FLAG_END;
6550
Matt Carlson84b67b22011-07-27 14:20:52 +00006551 tnapi->tx_buffers[*entry].skb = new_skb;
6552 dma_unmap_addr_set(&tnapi->tx_buffers[*entry],
Matt Carlson432aa7e2011-05-19 12:12:45 +00006553 mapping, new_addr);
6554
Matt Carlson84b67b22011-07-27 14:20:52 +00006555 if (tg3_tx_frag_set(tnapi, entry, budget, new_addr,
Matt Carlsond1a3b732011-07-27 14:20:51 +00006556 new_skb->len, base_flags,
6557 mss, vlan)) {
Matt Carlsonba1142e2011-11-04 09:15:00 +00006558 tg3_tx_skb_unmap(tnapi, save_entry, -1);
Matt Carlsond1a3b732011-07-27 14:20:51 +00006559 dev_kfree_skb(new_skb);
6560 ret = -1;
6561 }
Michael Chanc58ec932005-09-17 00:46:27 -07006562 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006563 }
6564
Linus Torvalds1da177e2005-04-16 15:20:36 -07006565 dev_kfree_skb(skb);
David S. Miller1805b2f2011-10-24 18:18:09 -04006566 *pskb = new_skb;
Michael Chanc58ec932005-09-17 00:46:27 -07006567 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006568}
6569
Matt Carlson2ffcc982011-05-19 12:12:44 +00006570static netdev_tx_t tg3_start_xmit(struct sk_buff *, struct net_device *);
Michael Chan52c0fd82006-06-29 20:15:54 -07006571
6572/* Use GSO to workaround a rare TSO bug that may be triggered when the
6573 * TSO header is greater than 80 bytes.
6574 */
6575static int tg3_tso_bug(struct tg3 *tp, struct sk_buff *skb)
6576{
6577 struct sk_buff *segs, *nskb;
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006578 u32 frag_cnt_est = skb_shinfo(skb)->gso_segs * 3;
Michael Chan52c0fd82006-06-29 20:15:54 -07006579
6580 /* Estimate the number of fragments in the worst case */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006581 if (unlikely(tg3_tx_avail(&tp->napi[0]) <= frag_cnt_est)) {
Michael Chan52c0fd82006-06-29 20:15:54 -07006582 netif_stop_queue(tp->dev);
Matt Carlsonf65aac12010-08-02 11:26:03 +00006583
6584 /* netif_tx_stop_queue() must be done before checking
6585 * checking tx index in tg3_tx_avail() below, because in
6586 * tg3_tx(), we update tx index before checking for
6587 * netif_tx_queue_stopped().
6588 */
6589 smp_mb();
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006590 if (tg3_tx_avail(&tp->napi[0]) <= frag_cnt_est)
Michael Chan7f62ad52007-02-20 23:25:40 -08006591 return NETDEV_TX_BUSY;
6592
6593 netif_wake_queue(tp->dev);
Michael Chan52c0fd82006-06-29 20:15:54 -07006594 }
6595
6596 segs = skb_gso_segment(skb, tp->dev->features & ~NETIF_F_TSO);
Hirofumi Nakagawa801678c2008-04-29 01:03:09 -07006597 if (IS_ERR(segs))
Michael Chan52c0fd82006-06-29 20:15:54 -07006598 goto tg3_tso_bug_end;
6599
6600 do {
6601 nskb = segs;
6602 segs = segs->next;
6603 nskb->next = NULL;
Matt Carlson2ffcc982011-05-19 12:12:44 +00006604 tg3_start_xmit(nskb, tp->dev);
Michael Chan52c0fd82006-06-29 20:15:54 -07006605 } while (segs);
6606
6607tg3_tso_bug_end:
6608 dev_kfree_skb(skb);
6609
6610 return NETDEV_TX_OK;
6611}
Michael Chan52c0fd82006-06-29 20:15:54 -07006612
Michael Chan5a6f3072006-03-20 22:28:05 -08006613/* hard_start_xmit for devices that have the 4G bug and/or 40-bit bug and
Joe Perches63c3a662011-04-26 08:12:10 +00006614 * support TG3_FLAG_HW_TSO_1 or firmware TSO only.
Michael Chan5a6f3072006-03-20 22:28:05 -08006615 */
Matt Carlson2ffcc982011-05-19 12:12:44 +00006616static netdev_tx_t tg3_start_xmit(struct sk_buff *skb, struct net_device *dev)
Michael Chan5a6f3072006-03-20 22:28:05 -08006617{
6618 struct tg3 *tp = netdev_priv(dev);
Matt Carlson92cd3a12011-07-27 14:20:47 +00006619 u32 len, entry, base_flags, mss, vlan = 0;
Matt Carlson84b67b22011-07-27 14:20:52 +00006620 u32 budget;
Matt Carlson432aa7e2011-05-19 12:12:45 +00006621 int i = -1, would_hit_hwbug;
David S. Miller90079ce2008-09-11 04:52:51 -07006622 dma_addr_t mapping;
Matt Carlson24f4efd2009-11-13 13:03:35 +00006623 struct tg3_napi *tnapi;
6624 struct netdev_queue *txq;
Matt Carlson432aa7e2011-05-19 12:12:45 +00006625 unsigned int last;
Alexander Duyckf4188d82009-12-02 16:48:38 +00006626
Matt Carlson24f4efd2009-11-13 13:03:35 +00006627 txq = netdev_get_tx_queue(dev, skb_get_queue_mapping(skb));
6628 tnapi = &tp->napi[skb_get_queue_mapping(skb)];
Joe Perches63c3a662011-04-26 08:12:10 +00006629 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlson24f4efd2009-11-13 13:03:35 +00006630 tnapi++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006631
Matt Carlson84b67b22011-07-27 14:20:52 +00006632 budget = tg3_tx_avail(tnapi);
6633
Michael Chan00b70502006-06-17 21:58:45 -07006634 /* We are running in BH disabled context with netif_tx_lock
Stephen Hemmingerbea33482007-10-03 16:41:36 -07006635 * and TX reclaim runs via tp->napi.poll inside of a software
David S. Millerf47c11e2005-06-24 20:18:35 -07006636 * interrupt. Furthermore, IRQ processing runs lockless so we have
6637 * no IRQ context deadlocks to worry about either. Rejoice!
Linus Torvalds1da177e2005-04-16 15:20:36 -07006638 */
Matt Carlson84b67b22011-07-27 14:20:52 +00006639 if (unlikely(budget <= (skb_shinfo(skb)->nr_frags + 1))) {
Matt Carlson24f4efd2009-11-13 13:03:35 +00006640 if (!netif_tx_queue_stopped(txq)) {
6641 netif_tx_stop_queue(txq);
Stephen Hemminger1f064a82005-12-06 17:36:44 -08006642
6643 /* This is a hard error, log it. */
Matt Carlson5129c3a2010-04-05 10:19:23 +00006644 netdev_err(dev,
6645 "BUG! Tx Ring full when queue awake!\n");
Stephen Hemminger1f064a82005-12-06 17:36:44 -08006646 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006647 return NETDEV_TX_BUSY;
6648 }
6649
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006650 entry = tnapi->tx_prod;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006651 base_flags = 0;
Patrick McHardy84fa7932006-08-29 16:44:56 -07006652 if (skb->ip_summed == CHECKSUM_PARTIAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006653 base_flags |= TXD_FLAG_TCPUDP_CSUM;
Matt Carlson24f4efd2009-11-13 13:03:35 +00006654
Matt Carlsonbe98da62010-07-11 09:31:46 +00006655 mss = skb_shinfo(skb)->gso_size;
6656 if (mss) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07006657 struct iphdr *iph;
Matt Carlson34195c32010-07-11 09:31:42 +00006658 u32 tcp_opt_len, hdr_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006659
6660 if (skb_header_cloned(skb) &&
Eric Dumazet48855432011-10-24 07:53:03 +00006661 pskb_expand_head(skb, 0, 0, GFP_ATOMIC))
6662 goto drop;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006663
Matt Carlson34195c32010-07-11 09:31:42 +00006664 iph = ip_hdr(skb);
Arnaldo Carvalho de Meloab6a5bb2007-03-18 17:43:48 -07006665 tcp_opt_len = tcp_optlen(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006666
Matt Carlson02e96082010-09-15 08:59:59 +00006667 if (skb_is_gso_v6(skb)) {
Matt Carlson34195c32010-07-11 09:31:42 +00006668 hdr_len = skb_headlen(skb) - ETH_HLEN;
6669 } else {
6670 u32 ip_tcp_len;
6671
6672 ip_tcp_len = ip_hdrlen(skb) + sizeof(struct tcphdr);
6673 hdr_len = ip_tcp_len + tcp_opt_len;
6674
6675 iph->check = 0;
6676 iph->tot_len = htons(mss + hdr_len);
6677 }
6678
Michael Chan52c0fd82006-06-29 20:15:54 -07006679 if (unlikely((ETH_HLEN + hdr_len) > 80) &&
Joe Perches63c3a662011-04-26 08:12:10 +00006680 tg3_flag(tp, TSO_BUG))
Matt Carlsonde6f31e2010-04-12 06:58:30 +00006681 return tg3_tso_bug(tp, skb);
Michael Chan52c0fd82006-06-29 20:15:54 -07006682
Linus Torvalds1da177e2005-04-16 15:20:36 -07006683 base_flags |= (TXD_FLAG_CPU_PRE_DMA |
6684 TXD_FLAG_CPU_POST_DMA);
6685
Joe Perches63c3a662011-04-26 08:12:10 +00006686 if (tg3_flag(tp, HW_TSO_1) ||
6687 tg3_flag(tp, HW_TSO_2) ||
6688 tg3_flag(tp, HW_TSO_3)) {
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07006689 tcp_hdr(skb)->check = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006690 base_flags &= ~TXD_FLAG_TCPUDP_CSUM;
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07006691 } else
6692 tcp_hdr(skb)->check = ~csum_tcpudp_magic(iph->saddr,
6693 iph->daddr, 0,
6694 IPPROTO_TCP,
6695 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006696
Joe Perches63c3a662011-04-26 08:12:10 +00006697 if (tg3_flag(tp, HW_TSO_3)) {
Matt Carlson615774f2009-11-13 13:03:39 +00006698 mss |= (hdr_len & 0xc) << 12;
6699 if (hdr_len & 0x10)
6700 base_flags |= 0x00000010;
6701 base_flags |= (hdr_len & 0x3e0) << 5;
Joe Perches63c3a662011-04-26 08:12:10 +00006702 } else if (tg3_flag(tp, HW_TSO_2))
Matt Carlson92c6b8d2009-11-02 14:23:27 +00006703 mss |= hdr_len << 9;
Joe Perches63c3a662011-04-26 08:12:10 +00006704 else if (tg3_flag(tp, HW_TSO_1) ||
Matt Carlson92c6b8d2009-11-02 14:23:27 +00006705 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07006706 if (tcp_opt_len || iph->ihl > 5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006707 int tsflags;
6708
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07006709 tsflags = (iph->ihl - 5) + (tcp_opt_len >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006710 mss |= (tsflags << 11);
6711 }
6712 } else {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07006713 if (tcp_opt_len || iph->ihl > 5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006714 int tsflags;
6715
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07006716 tsflags = (iph->ihl - 5) + (tcp_opt_len >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006717 base_flags |= tsflags << 12;
6718 }
6719 }
6720 }
Matt Carlsonbf933c82011-01-25 15:58:49 +00006721
Matt Carlson93a700a2011-08-31 11:44:54 +00006722 if (tg3_flag(tp, USE_JUMBO_BDFLAG) &&
6723 !mss && skb->len > VLAN_ETH_FRAME_LEN)
6724 base_flags |= TXD_FLAG_JMB_PKT;
6725
Matt Carlson92cd3a12011-07-27 14:20:47 +00006726 if (vlan_tx_tag_present(skb)) {
6727 base_flags |= TXD_FLAG_VLAN;
6728 vlan = vlan_tx_tag_get(skb);
6729 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006730
Alexander Duyckf4188d82009-12-02 16:48:38 +00006731 len = skb_headlen(skb);
6732
6733 mapping = pci_map_single(tp->pdev, skb->data, len, PCI_DMA_TODEVICE);
Eric Dumazet48855432011-10-24 07:53:03 +00006734 if (pci_dma_mapping_error(tp->pdev, mapping))
6735 goto drop;
6736
David S. Miller90079ce2008-09-11 04:52:51 -07006737
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006738 tnapi->tx_buffers[entry].skb = skb;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006739 dma_unmap_addr_set(&tnapi->tx_buffers[entry], mapping, mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006740
6741 would_hit_hwbug = 0;
6742
Joe Perches63c3a662011-04-26 08:12:10 +00006743 if (tg3_flag(tp, 5701_DMA_BUG))
Michael Chanc58ec932005-09-17 00:46:27 -07006744 would_hit_hwbug = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006745
Matt Carlson84b67b22011-07-27 14:20:52 +00006746 if (tg3_tx_frag_set(tnapi, &entry, &budget, mapping, len, base_flags |
Matt Carlsond1a3b732011-07-27 14:20:51 +00006747 ((skb_shinfo(skb)->nr_frags == 0) ? TXD_FLAG_END : 0),
Matt Carlsonba1142e2011-11-04 09:15:00 +00006748 mss, vlan)) {
Matt Carlsond1a3b732011-07-27 14:20:51 +00006749 would_hit_hwbug = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006750 /* Now loop through additional data fragments, and queue them. */
Matt Carlsonba1142e2011-11-04 09:15:00 +00006751 } else if (skb_shinfo(skb)->nr_frags > 0) {
Matt Carlson92cd3a12011-07-27 14:20:47 +00006752 u32 tmp_mss = mss;
6753
6754 if (!tg3_flag(tp, HW_TSO_1) &&
6755 !tg3_flag(tp, HW_TSO_2) &&
6756 !tg3_flag(tp, HW_TSO_3))
6757 tmp_mss = 0;
6758
Linus Torvalds1da177e2005-04-16 15:20:36 -07006759 last = skb_shinfo(skb)->nr_frags - 1;
6760 for (i = 0; i <= last; i++) {
6761 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
6762
Eric Dumazet9e903e02011-10-18 21:00:24 +00006763 len = skb_frag_size(frag);
Ian Campbelldc234d02011-08-24 22:28:11 +00006764 mapping = skb_frag_dma_map(&tp->pdev->dev, frag, 0,
Ian Campbell5d6bcdf2011-10-06 11:10:48 +01006765 len, DMA_TO_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006766
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006767 tnapi->tx_buffers[entry].skb = NULL;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006768 dma_unmap_addr_set(&tnapi->tx_buffers[entry], mapping,
Alexander Duyckf4188d82009-12-02 16:48:38 +00006769 mapping);
Ian Campbell5d6bcdf2011-10-06 11:10:48 +01006770 if (dma_mapping_error(&tp->pdev->dev, mapping))
Alexander Duyckf4188d82009-12-02 16:48:38 +00006771 goto dma_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006772
Matt Carlsonb9e45482011-11-04 09:14:59 +00006773 if (!budget ||
6774 tg3_tx_frag_set(tnapi, &entry, &budget, mapping,
Matt Carlson84b67b22011-07-27 14:20:52 +00006775 len, base_flags |
6776 ((i == last) ? TXD_FLAG_END : 0),
Matt Carlsonb9e45482011-11-04 09:14:59 +00006777 tmp_mss, vlan)) {
Matt Carlson92c6b8d2009-11-02 14:23:27 +00006778 would_hit_hwbug = 1;
Matt Carlsonb9e45482011-11-04 09:14:59 +00006779 break;
6780 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006781 }
6782 }
6783
6784 if (would_hit_hwbug) {
Matt Carlson0d681b22011-07-27 14:20:49 +00006785 tg3_tx_skb_unmap(tnapi, tnapi->tx_prod, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006786
6787 /* If the workaround fails due to memory/mapping
6788 * failure, silently drop this packet.
6789 */
Matt Carlson84b67b22011-07-27 14:20:52 +00006790 entry = tnapi->tx_prod;
6791 budget = tg3_tx_avail(tnapi);
David S. Miller1805b2f2011-10-24 18:18:09 -04006792 if (tigon3_dma_hwbug_workaround(tnapi, &skb, &entry, &budget,
Matt Carlson84b67b22011-07-27 14:20:52 +00006793 base_flags, mss, vlan))
Eric Dumazet48855432011-10-24 07:53:03 +00006794 goto drop_nofree;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006795 }
6796
Richard Cochrand515b452011-06-19 03:31:41 +00006797 skb_tx_timestamp(skb);
6798
Linus Torvalds1da177e2005-04-16 15:20:36 -07006799 /* Packets are ready, update Tx producer idx local and on card. */
Matt Carlson24f4efd2009-11-13 13:03:35 +00006800 tw32_tx_mbox(tnapi->prodmbox, entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006801
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006802 tnapi->tx_prod = entry;
6803 if (unlikely(tg3_tx_avail(tnapi) <= (MAX_SKB_FRAGS + 1))) {
Matt Carlson24f4efd2009-11-13 13:03:35 +00006804 netif_tx_stop_queue(txq);
Matt Carlsonf65aac12010-08-02 11:26:03 +00006805
6806 /* netif_tx_stop_queue() must be done before checking
6807 * checking tx index in tg3_tx_avail() below, because in
6808 * tg3_tx(), we update tx index before checking for
6809 * netif_tx_queue_stopped().
6810 */
6811 smp_mb();
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006812 if (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi))
Matt Carlson24f4efd2009-11-13 13:03:35 +00006813 netif_tx_wake_queue(txq);
Michael Chan51b91462005-09-01 17:41:28 -07006814 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006815
Eric Dumazetcdd0db02009-05-28 00:00:41 +00006816 mmiowb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006817 return NETDEV_TX_OK;
Alexander Duyckf4188d82009-12-02 16:48:38 +00006818
6819dma_error:
Matt Carlsonba1142e2011-11-04 09:15:00 +00006820 tg3_tx_skb_unmap(tnapi, tnapi->tx_prod, --i);
Matt Carlson432aa7e2011-05-19 12:12:45 +00006821 tnapi->tx_buffers[tnapi->tx_prod].skb = NULL;
Eric Dumazet48855432011-10-24 07:53:03 +00006822drop:
6823 dev_kfree_skb(skb);
6824drop_nofree:
6825 tp->tx_dropped++;
Alexander Duyckf4188d82009-12-02 16:48:38 +00006826 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006827}
6828
Matt Carlson6e01b202011-08-19 13:58:20 +00006829static void tg3_mac_loopback(struct tg3 *tp, bool enable)
6830{
6831 if (enable) {
6832 tp->mac_mode &= ~(MAC_MODE_HALF_DUPLEX |
6833 MAC_MODE_PORT_MODE_MASK);
6834
6835 tp->mac_mode |= MAC_MODE_PORT_INT_LPBACK;
6836
6837 if (!tg3_flag(tp, 5705_PLUS))
6838 tp->mac_mode |= MAC_MODE_LINK_POLARITY;
6839
6840 if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
6841 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
6842 else
6843 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
6844 } else {
6845 tp->mac_mode &= ~MAC_MODE_PORT_INT_LPBACK;
6846
6847 if (tg3_flag(tp, 5705_PLUS) ||
6848 (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) ||
6849 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700)
6850 tp->mac_mode &= ~MAC_MODE_LINK_POLARITY;
6851 }
6852
6853 tw32(MAC_MODE, tp->mac_mode);
6854 udelay(40);
6855}
6856
Matt Carlson941ec902011-08-19 13:58:23 +00006857static int tg3_phy_lpbk_set(struct tg3 *tp, u32 speed, bool extlpbk)
Matt Carlson5e5a7f32011-08-19 13:58:21 +00006858{
Matt Carlson941ec902011-08-19 13:58:23 +00006859 u32 val, bmcr, mac_mode, ptest = 0;
Matt Carlson5e5a7f32011-08-19 13:58:21 +00006860
6861 tg3_phy_toggle_apd(tp, false);
6862 tg3_phy_toggle_automdix(tp, 0);
6863
Matt Carlson941ec902011-08-19 13:58:23 +00006864 if (extlpbk && tg3_phy_set_extloopbk(tp))
6865 return -EIO;
6866
6867 bmcr = BMCR_FULLDPLX;
Matt Carlson5e5a7f32011-08-19 13:58:21 +00006868 switch (speed) {
6869 case SPEED_10:
6870 break;
6871 case SPEED_100:
6872 bmcr |= BMCR_SPEED100;
6873 break;
6874 case SPEED_1000:
6875 default:
6876 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
6877 speed = SPEED_100;
6878 bmcr |= BMCR_SPEED100;
6879 } else {
6880 speed = SPEED_1000;
6881 bmcr |= BMCR_SPEED1000;
6882 }
6883 }
6884
Matt Carlson941ec902011-08-19 13:58:23 +00006885 if (extlpbk) {
6886 if (!(tp->phy_flags & TG3_PHYFLG_IS_FET)) {
6887 tg3_readphy(tp, MII_CTRL1000, &val);
6888 val |= CTL1000_AS_MASTER |
6889 CTL1000_ENABLE_MASTER;
6890 tg3_writephy(tp, MII_CTRL1000, val);
6891 } else {
6892 ptest = MII_TG3_FET_PTEST_TRIM_SEL |
6893 MII_TG3_FET_PTEST_TRIM_2;
6894 tg3_writephy(tp, MII_TG3_FET_PTEST, ptest);
6895 }
6896 } else
6897 bmcr |= BMCR_LOOPBACK;
6898
Matt Carlson5e5a7f32011-08-19 13:58:21 +00006899 tg3_writephy(tp, MII_BMCR, bmcr);
6900
6901 /* The write needs to be flushed for the FETs */
6902 if (tp->phy_flags & TG3_PHYFLG_IS_FET)
6903 tg3_readphy(tp, MII_BMCR, &bmcr);
6904
6905 udelay(40);
6906
6907 if ((tp->phy_flags & TG3_PHYFLG_IS_FET) &&
6908 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785) {
Matt Carlson941ec902011-08-19 13:58:23 +00006909 tg3_writephy(tp, MII_TG3_FET_PTEST, ptest |
Matt Carlson5e5a7f32011-08-19 13:58:21 +00006910 MII_TG3_FET_PTEST_FRC_TX_LINK |
6911 MII_TG3_FET_PTEST_FRC_TX_LOCK);
6912
6913 /* The write needs to be flushed for the AC131 */
6914 tg3_readphy(tp, MII_TG3_FET_PTEST, &val);
6915 }
6916
6917 /* Reset to prevent losing 1st rx packet intermittently */
6918 if ((tp->phy_flags & TG3_PHYFLG_MII_SERDES) &&
6919 tg3_flag(tp, 5780_CLASS)) {
6920 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
6921 udelay(10);
6922 tw32_f(MAC_RX_MODE, tp->rx_mode);
6923 }
6924
6925 mac_mode = tp->mac_mode &
6926 ~(MAC_MODE_PORT_MODE_MASK | MAC_MODE_HALF_DUPLEX);
6927 if (speed == SPEED_1000)
6928 mac_mode |= MAC_MODE_PORT_MODE_GMII;
6929 else
6930 mac_mode |= MAC_MODE_PORT_MODE_MII;
6931
6932 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700) {
6933 u32 masked_phy_id = tp->phy_id & TG3_PHY_ID_MASK;
6934
6935 if (masked_phy_id == TG3_PHY_ID_BCM5401)
6936 mac_mode &= ~MAC_MODE_LINK_POLARITY;
6937 else if (masked_phy_id == TG3_PHY_ID_BCM5411)
6938 mac_mode |= MAC_MODE_LINK_POLARITY;
6939
6940 tg3_writephy(tp, MII_TG3_EXT_CTRL,
6941 MII_TG3_EXT_CTRL_LNK3_LED_MODE);
6942 }
6943
6944 tw32(MAC_MODE, mac_mode);
6945 udelay(40);
Matt Carlson941ec902011-08-19 13:58:23 +00006946
6947 return 0;
Matt Carlson5e5a7f32011-08-19 13:58:21 +00006948}
6949
Michał Mirosławc8f44af2011-11-15 15:29:55 +00006950static void tg3_set_loopback(struct net_device *dev, netdev_features_t features)
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00006951{
6952 struct tg3 *tp = netdev_priv(dev);
6953
6954 if (features & NETIF_F_LOOPBACK) {
6955 if (tp->mac_mode & MAC_MODE_PORT_INT_LPBACK)
6956 return;
6957
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00006958 spin_lock_bh(&tp->lock);
Matt Carlson6e01b202011-08-19 13:58:20 +00006959 tg3_mac_loopback(tp, true);
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00006960 netif_carrier_on(tp->dev);
6961 spin_unlock_bh(&tp->lock);
6962 netdev_info(dev, "Internal MAC loopback mode enabled.\n");
6963 } else {
6964 if (!(tp->mac_mode & MAC_MODE_PORT_INT_LPBACK))
6965 return;
6966
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00006967 spin_lock_bh(&tp->lock);
Matt Carlson6e01b202011-08-19 13:58:20 +00006968 tg3_mac_loopback(tp, false);
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00006969 /* Force link status check */
6970 tg3_setup_phy(tp, 1);
6971 spin_unlock_bh(&tp->lock);
6972 netdev_info(dev, "Internal MAC loopback mode disabled.\n");
6973 }
6974}
6975
Michał Mirosławc8f44af2011-11-15 15:29:55 +00006976static netdev_features_t tg3_fix_features(struct net_device *dev,
6977 netdev_features_t features)
Michał Mirosławdc668912011-04-07 03:35:07 +00006978{
6979 struct tg3 *tp = netdev_priv(dev);
6980
Joe Perches63c3a662011-04-26 08:12:10 +00006981 if (dev->mtu > ETH_DATA_LEN && tg3_flag(tp, 5780_CLASS))
Michał Mirosławdc668912011-04-07 03:35:07 +00006982 features &= ~NETIF_F_ALL_TSO;
6983
6984 return features;
6985}
6986
Michał Mirosławc8f44af2011-11-15 15:29:55 +00006987static int tg3_set_features(struct net_device *dev, netdev_features_t features)
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00006988{
Michał Mirosławc8f44af2011-11-15 15:29:55 +00006989 netdev_features_t changed = dev->features ^ features;
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00006990
6991 if ((changed & NETIF_F_LOOPBACK) && netif_running(dev))
6992 tg3_set_loopback(dev, features);
6993
6994 return 0;
6995}
6996
Linus Torvalds1da177e2005-04-16 15:20:36 -07006997static inline void tg3_set_mtu(struct net_device *dev, struct tg3 *tp,
6998 int new_mtu)
6999{
7000 dev->mtu = new_mtu;
7001
Michael Chanef7f5ec2005-07-25 12:32:25 -07007002 if (new_mtu > ETH_DATA_LEN) {
Joe Perches63c3a662011-04-26 08:12:10 +00007003 if (tg3_flag(tp, 5780_CLASS)) {
Michał Mirosławdc668912011-04-07 03:35:07 +00007004 netdev_update_features(dev);
Joe Perches63c3a662011-04-26 08:12:10 +00007005 tg3_flag_clear(tp, TSO_CAPABLE);
Matt Carlson859a588792010-04-05 10:19:28 +00007006 } else {
Joe Perches63c3a662011-04-26 08:12:10 +00007007 tg3_flag_set(tp, JUMBO_RING_ENABLE);
Matt Carlson859a588792010-04-05 10:19:28 +00007008 }
Michael Chanef7f5ec2005-07-25 12:32:25 -07007009 } else {
Joe Perches63c3a662011-04-26 08:12:10 +00007010 if (tg3_flag(tp, 5780_CLASS)) {
7011 tg3_flag_set(tp, TSO_CAPABLE);
Michał Mirosławdc668912011-04-07 03:35:07 +00007012 netdev_update_features(dev);
7013 }
Joe Perches63c3a662011-04-26 08:12:10 +00007014 tg3_flag_clear(tp, JUMBO_RING_ENABLE);
Michael Chanef7f5ec2005-07-25 12:32:25 -07007015 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007016}
7017
7018static int tg3_change_mtu(struct net_device *dev, int new_mtu)
7019{
7020 struct tg3 *tp = netdev_priv(dev);
Michael Chanb9ec6c12006-07-25 16:37:27 -07007021 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007022
7023 if (new_mtu < TG3_MIN_MTU || new_mtu > TG3_MAX_MTU(tp))
7024 return -EINVAL;
7025
7026 if (!netif_running(dev)) {
7027 /* We'll just catch it later when the
7028 * device is up'd.
7029 */
7030 tg3_set_mtu(dev, tp, new_mtu);
7031 return 0;
7032 }
7033
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07007034 tg3_phy_stop(tp);
7035
Linus Torvalds1da177e2005-04-16 15:20:36 -07007036 tg3_netif_stop(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -07007037
7038 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007039
Michael Chan944d9802005-05-29 14:57:48 -07007040 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007041
7042 tg3_set_mtu(dev, tp, new_mtu);
7043
Michael Chanb9ec6c12006-07-25 16:37:27 -07007044 err = tg3_restart_hw(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007045
Michael Chanb9ec6c12006-07-25 16:37:27 -07007046 if (!err)
7047 tg3_netif_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007048
David S. Millerf47c11e2005-06-24 20:18:35 -07007049 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007050
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07007051 if (!err)
7052 tg3_phy_start(tp);
7053
Michael Chanb9ec6c12006-07-25 16:37:27 -07007054 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007055}
7056
Matt Carlson21f581a2009-08-28 14:00:25 +00007057static void tg3_rx_prodring_free(struct tg3 *tp,
7058 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007059{
Linus Torvalds1da177e2005-04-16 15:20:36 -07007060 int i;
7061
Matt Carlson8fea32b2010-09-15 08:59:58 +00007062 if (tpr != &tp->napi[0].prodring) {
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007063 for (i = tpr->rx_std_cons_idx; i != tpr->rx_std_prod_idx;
Matt Carlson2c49a442010-09-30 10:34:35 +00007064 i = (i + 1) & tp->rx_std_ring_mask)
Eric Dumazet9205fd92011-11-18 06:47:01 +00007065 tg3_rx_data_free(tp, &tpr->rx_std_buffers[i],
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007066 tp->rx_pkt_map_sz);
7067
Joe Perches63c3a662011-04-26 08:12:10 +00007068 if (tg3_flag(tp, JUMBO_CAPABLE)) {
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007069 for (i = tpr->rx_jmb_cons_idx;
7070 i != tpr->rx_jmb_prod_idx;
Matt Carlson2c49a442010-09-30 10:34:35 +00007071 i = (i + 1) & tp->rx_jmb_ring_mask) {
Eric Dumazet9205fd92011-11-18 06:47:01 +00007072 tg3_rx_data_free(tp, &tpr->rx_jmb_buffers[i],
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007073 TG3_RX_JMB_MAP_SZ);
7074 }
7075 }
7076
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007077 return;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007078 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007079
Matt Carlson2c49a442010-09-30 10:34:35 +00007080 for (i = 0; i <= tp->rx_std_ring_mask; i++)
Eric Dumazet9205fd92011-11-18 06:47:01 +00007081 tg3_rx_data_free(tp, &tpr->rx_std_buffers[i],
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007082 tp->rx_pkt_map_sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007083
Joe Perches63c3a662011-04-26 08:12:10 +00007084 if (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS)) {
Matt Carlson2c49a442010-09-30 10:34:35 +00007085 for (i = 0; i <= tp->rx_jmb_ring_mask; i++)
Eric Dumazet9205fd92011-11-18 06:47:01 +00007086 tg3_rx_data_free(tp, &tpr->rx_jmb_buffers[i],
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007087 TG3_RX_JMB_MAP_SZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007088 }
7089}
7090
Matt Carlsonc6cdf432010-04-05 10:19:26 +00007091/* Initialize rx rings for packet processing.
Linus Torvalds1da177e2005-04-16 15:20:36 -07007092 *
7093 * The chip has been shut down and the driver detached from
7094 * the networking, so no interrupts or new tx packets will
7095 * end up in the driver. tp->{tx,}lock are held and thus
7096 * we may not sleep.
7097 */
Matt Carlson21f581a2009-08-28 14:00:25 +00007098static int tg3_rx_prodring_alloc(struct tg3 *tp,
7099 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007100{
Matt Carlson287be122009-08-28 13:58:46 +00007101 u32 i, rx_pkt_dma_sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007102
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007103 tpr->rx_std_cons_idx = 0;
7104 tpr->rx_std_prod_idx = 0;
7105 tpr->rx_jmb_cons_idx = 0;
7106 tpr->rx_jmb_prod_idx = 0;
7107
Matt Carlson8fea32b2010-09-15 08:59:58 +00007108 if (tpr != &tp->napi[0].prodring) {
Matt Carlson2c49a442010-09-30 10:34:35 +00007109 memset(&tpr->rx_std_buffers[0], 0,
7110 TG3_RX_STD_BUFF_RING_SIZE(tp));
Matt Carlson48035722010-10-14 10:37:43 +00007111 if (tpr->rx_jmb_buffers)
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007112 memset(&tpr->rx_jmb_buffers[0], 0,
Matt Carlson2c49a442010-09-30 10:34:35 +00007113 TG3_RX_JMB_BUFF_RING_SIZE(tp));
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007114 goto done;
7115 }
7116
Linus Torvalds1da177e2005-04-16 15:20:36 -07007117 /* Zero out all descriptors. */
Matt Carlson2c49a442010-09-30 10:34:35 +00007118 memset(tpr->rx_std, 0, TG3_RX_STD_RING_BYTES(tp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007119
Matt Carlson287be122009-08-28 13:58:46 +00007120 rx_pkt_dma_sz = TG3_RX_STD_DMA_SZ;
Joe Perches63c3a662011-04-26 08:12:10 +00007121 if (tg3_flag(tp, 5780_CLASS) &&
Matt Carlson287be122009-08-28 13:58:46 +00007122 tp->dev->mtu > ETH_DATA_LEN)
7123 rx_pkt_dma_sz = TG3_RX_JMB_DMA_SZ;
7124 tp->rx_pkt_map_sz = TG3_RX_DMA_TO_MAP_SZ(rx_pkt_dma_sz);
Michael Chan7e72aad2005-07-25 12:31:17 -07007125
Linus Torvalds1da177e2005-04-16 15:20:36 -07007126 /* Initialize invariants of the rings, we only set this
7127 * stuff once. This works because the card does not
7128 * write into the rx buffer posting rings.
7129 */
Matt Carlson2c49a442010-09-30 10:34:35 +00007130 for (i = 0; i <= tp->rx_std_ring_mask; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007131 struct tg3_rx_buffer_desc *rxd;
7132
Matt Carlson21f581a2009-08-28 14:00:25 +00007133 rxd = &tpr->rx_std[i];
Matt Carlson287be122009-08-28 13:58:46 +00007134 rxd->idx_len = rx_pkt_dma_sz << RXD_LEN_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007135 rxd->type_flags = (RXD_FLAG_END << RXD_FLAGS_SHIFT);
7136 rxd->opaque = (RXD_OPAQUE_RING_STD |
7137 (i << RXD_OPAQUE_INDEX_SHIFT));
7138 }
7139
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007140 /* Now allocate fresh SKBs for each rx ring. */
7141 for (i = 0; i < tp->rx_pending; i++) {
Eric Dumazet9205fd92011-11-18 06:47:01 +00007142 if (tg3_alloc_rx_data(tp, tpr, RXD_OPAQUE_RING_STD, i) < 0) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00007143 netdev_warn(tp->dev,
7144 "Using a smaller RX standard ring. Only "
7145 "%d out of %d buffers were allocated "
7146 "successfully\n", i, tp->rx_pending);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007147 if (i == 0)
7148 goto initfail;
7149 tp->rx_pending = i;
7150 break;
7151 }
7152 }
7153
Joe Perches63c3a662011-04-26 08:12:10 +00007154 if (!tg3_flag(tp, JUMBO_CAPABLE) || tg3_flag(tp, 5780_CLASS))
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007155 goto done;
7156
Matt Carlson2c49a442010-09-30 10:34:35 +00007157 memset(tpr->rx_jmb, 0, TG3_RX_JMB_RING_BYTES(tp));
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007158
Joe Perches63c3a662011-04-26 08:12:10 +00007159 if (!tg3_flag(tp, JUMBO_RING_ENABLE))
Matt Carlson0d86df82010-02-17 15:17:00 +00007160 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007161
Matt Carlson2c49a442010-09-30 10:34:35 +00007162 for (i = 0; i <= tp->rx_jmb_ring_mask; i++) {
Matt Carlson0d86df82010-02-17 15:17:00 +00007163 struct tg3_rx_buffer_desc *rxd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007164
Matt Carlson0d86df82010-02-17 15:17:00 +00007165 rxd = &tpr->rx_jmb[i].std;
7166 rxd->idx_len = TG3_RX_JMB_DMA_SZ << RXD_LEN_SHIFT;
7167 rxd->type_flags = (RXD_FLAG_END << RXD_FLAGS_SHIFT) |
7168 RXD_FLAG_JUMBO;
7169 rxd->opaque = (RXD_OPAQUE_RING_JUMBO |
7170 (i << RXD_OPAQUE_INDEX_SHIFT));
7171 }
7172
7173 for (i = 0; i < tp->rx_jumbo_pending; i++) {
Eric Dumazet9205fd92011-11-18 06:47:01 +00007174 if (tg3_alloc_rx_data(tp, tpr, RXD_OPAQUE_RING_JUMBO, i) < 0) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00007175 netdev_warn(tp->dev,
7176 "Using a smaller RX jumbo ring. Only %d "
7177 "out of %d buffers were allocated "
7178 "successfully\n", i, tp->rx_jumbo_pending);
Matt Carlson0d86df82010-02-17 15:17:00 +00007179 if (i == 0)
7180 goto initfail;
7181 tp->rx_jumbo_pending = i;
7182 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007183 }
7184 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007185
7186done:
Michael Chan32d8c572006-07-25 16:38:29 -07007187 return 0;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007188
7189initfail:
Matt Carlson21f581a2009-08-28 14:00:25 +00007190 tg3_rx_prodring_free(tp, tpr);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007191 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007192}
7193
Matt Carlson21f581a2009-08-28 14:00:25 +00007194static void tg3_rx_prodring_fini(struct tg3 *tp,
7195 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007196{
Matt Carlson21f581a2009-08-28 14:00:25 +00007197 kfree(tpr->rx_std_buffers);
7198 tpr->rx_std_buffers = NULL;
7199 kfree(tpr->rx_jmb_buffers);
7200 tpr->rx_jmb_buffers = NULL;
7201 if (tpr->rx_std) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00007202 dma_free_coherent(&tp->pdev->dev, TG3_RX_STD_RING_BYTES(tp),
7203 tpr->rx_std, tpr->rx_std_mapping);
Matt Carlson21f581a2009-08-28 14:00:25 +00007204 tpr->rx_std = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007205 }
Matt Carlson21f581a2009-08-28 14:00:25 +00007206 if (tpr->rx_jmb) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00007207 dma_free_coherent(&tp->pdev->dev, TG3_RX_JMB_RING_BYTES(tp),
7208 tpr->rx_jmb, tpr->rx_jmb_mapping);
Matt Carlson21f581a2009-08-28 14:00:25 +00007209 tpr->rx_jmb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007210 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007211}
7212
Matt Carlson21f581a2009-08-28 14:00:25 +00007213static int tg3_rx_prodring_init(struct tg3 *tp,
7214 struct tg3_rx_prodring_set *tpr)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007215{
Matt Carlson2c49a442010-09-30 10:34:35 +00007216 tpr->rx_std_buffers = kzalloc(TG3_RX_STD_BUFF_RING_SIZE(tp),
7217 GFP_KERNEL);
Matt Carlson21f581a2009-08-28 14:00:25 +00007218 if (!tpr->rx_std_buffers)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007219 return -ENOMEM;
7220
Matt Carlson4bae65c2010-11-24 08:31:52 +00007221 tpr->rx_std = dma_alloc_coherent(&tp->pdev->dev,
7222 TG3_RX_STD_RING_BYTES(tp),
7223 &tpr->rx_std_mapping,
7224 GFP_KERNEL);
Matt Carlson21f581a2009-08-28 14:00:25 +00007225 if (!tpr->rx_std)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007226 goto err_out;
7227
Joe Perches63c3a662011-04-26 08:12:10 +00007228 if (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS)) {
Matt Carlson2c49a442010-09-30 10:34:35 +00007229 tpr->rx_jmb_buffers = kzalloc(TG3_RX_JMB_BUFF_RING_SIZE(tp),
Matt Carlson21f581a2009-08-28 14:00:25 +00007230 GFP_KERNEL);
7231 if (!tpr->rx_jmb_buffers)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007232 goto err_out;
7233
Matt Carlson4bae65c2010-11-24 08:31:52 +00007234 tpr->rx_jmb = dma_alloc_coherent(&tp->pdev->dev,
7235 TG3_RX_JMB_RING_BYTES(tp),
7236 &tpr->rx_jmb_mapping,
7237 GFP_KERNEL);
Matt Carlson21f581a2009-08-28 14:00:25 +00007238 if (!tpr->rx_jmb)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007239 goto err_out;
7240 }
7241
7242 return 0;
7243
7244err_out:
Matt Carlson21f581a2009-08-28 14:00:25 +00007245 tg3_rx_prodring_fini(tp, tpr);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007246 return -ENOMEM;
7247}
7248
7249/* Free up pending packets in all rx/tx rings.
7250 *
7251 * The chip has been shut down and the driver detached from
7252 * the networking, so no interrupts or new tx packets will
7253 * end up in the driver. tp->{tx,}lock is not held and we are not
7254 * in an interrupt context and thus may sleep.
7255 */
7256static void tg3_free_rings(struct tg3 *tp)
7257{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007258 int i, j;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007259
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007260 for (j = 0; j < tp->irq_cnt; j++) {
7261 struct tg3_napi *tnapi = &tp->napi[j];
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007262
Matt Carlson8fea32b2010-09-15 08:59:58 +00007263 tg3_rx_prodring_free(tp, &tnapi->prodring);
Matt Carlsonb28f6422010-06-05 17:24:32 +00007264
Matt Carlson0c1d0e22009-09-01 13:16:33 +00007265 if (!tnapi->tx_buffers)
7266 continue;
7267
Matt Carlson0d681b22011-07-27 14:20:49 +00007268 for (i = 0; i < TG3_TX_RING_SIZE; i++) {
7269 struct sk_buff *skb = tnapi->tx_buffers[i].skb;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007270
Matt Carlson0d681b22011-07-27 14:20:49 +00007271 if (!skb)
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007272 continue;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007273
Matt Carlsonba1142e2011-11-04 09:15:00 +00007274 tg3_tx_skb_unmap(tnapi, i,
7275 skb_shinfo(skb)->nr_frags - 1);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007276
7277 dev_kfree_skb_any(skb);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007278 }
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007279 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007280}
7281
7282/* Initialize tx/rx rings for packet processing.
7283 *
7284 * The chip has been shut down and the driver detached from
7285 * the networking, so no interrupts or new tx packets will
7286 * end up in the driver. tp->{tx,}lock are held and thus
7287 * we may not sleep.
7288 */
7289static int tg3_init_rings(struct tg3 *tp)
7290{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007291 int i;
Matt Carlson72334482009-08-28 14:03:01 +00007292
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007293 /* Free up all the SKBs. */
7294 tg3_free_rings(tp);
7295
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007296 for (i = 0; i < tp->irq_cnt; i++) {
7297 struct tg3_napi *tnapi = &tp->napi[i];
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007298
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007299 tnapi->last_tag = 0;
7300 tnapi->last_irq_tag = 0;
7301 tnapi->hw_status->status = 0;
7302 tnapi->hw_status->status_tag = 0;
7303 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
7304
7305 tnapi->tx_prod = 0;
7306 tnapi->tx_cons = 0;
Matt Carlson0c1d0e22009-09-01 13:16:33 +00007307 if (tnapi->tx_ring)
7308 memset(tnapi->tx_ring, 0, TG3_TX_RING_BYTES);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007309
7310 tnapi->rx_rcb_ptr = 0;
Matt Carlson0c1d0e22009-09-01 13:16:33 +00007311 if (tnapi->rx_rcb)
7312 memset(tnapi->rx_rcb, 0, TG3_RX_RCB_RING_BYTES(tp));
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007313
Matt Carlson8fea32b2010-09-15 08:59:58 +00007314 if (tg3_rx_prodring_alloc(tp, &tnapi->prodring)) {
Matt Carlsone4af1af2010-02-12 14:47:05 +00007315 tg3_free_rings(tp);
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007316 return -ENOMEM;
Matt Carlsone4af1af2010-02-12 14:47:05 +00007317 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007318 }
Matt Carlson72334482009-08-28 14:03:01 +00007319
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007320 return 0;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007321}
7322
7323/*
7324 * Must not be invoked with interrupt sources disabled and
7325 * the hardware shutdown down.
7326 */
7327static void tg3_free_consistent(struct tg3 *tp)
7328{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007329 int i;
Matt Carlson898a56f2009-08-28 14:02:40 +00007330
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007331 for (i = 0; i < tp->irq_cnt; i++) {
7332 struct tg3_napi *tnapi = &tp->napi[i];
7333
7334 if (tnapi->tx_ring) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00007335 dma_free_coherent(&tp->pdev->dev, TG3_TX_RING_BYTES,
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007336 tnapi->tx_ring, tnapi->tx_desc_mapping);
7337 tnapi->tx_ring = NULL;
7338 }
7339
7340 kfree(tnapi->tx_buffers);
7341 tnapi->tx_buffers = NULL;
7342
7343 if (tnapi->rx_rcb) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00007344 dma_free_coherent(&tp->pdev->dev,
7345 TG3_RX_RCB_RING_BYTES(tp),
7346 tnapi->rx_rcb,
7347 tnapi->rx_rcb_mapping);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007348 tnapi->rx_rcb = NULL;
7349 }
7350
Matt Carlson8fea32b2010-09-15 08:59:58 +00007351 tg3_rx_prodring_fini(tp, &tnapi->prodring);
7352
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007353 if (tnapi->hw_status) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00007354 dma_free_coherent(&tp->pdev->dev, TG3_HW_STATUS_SIZE,
7355 tnapi->hw_status,
7356 tnapi->status_mapping);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007357 tnapi->hw_status = NULL;
7358 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007359 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007360
Linus Torvalds1da177e2005-04-16 15:20:36 -07007361 if (tp->hw_stats) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00007362 dma_free_coherent(&tp->pdev->dev, sizeof(struct tg3_hw_stats),
7363 tp->hw_stats, tp->stats_mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007364 tp->hw_stats = NULL;
7365 }
7366}
7367
7368/*
7369 * Must not be invoked with interrupt sources disabled and
7370 * the hardware shutdown down. Can sleep.
7371 */
7372static int tg3_alloc_consistent(struct tg3 *tp)
7373{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007374 int i;
Matt Carlson898a56f2009-08-28 14:02:40 +00007375
Matt Carlson4bae65c2010-11-24 08:31:52 +00007376 tp->hw_stats = dma_alloc_coherent(&tp->pdev->dev,
7377 sizeof(struct tg3_hw_stats),
7378 &tp->stats_mapping,
7379 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007380 if (!tp->hw_stats)
7381 goto err_out;
7382
Linus Torvalds1da177e2005-04-16 15:20:36 -07007383 memset(tp->hw_stats, 0, sizeof(struct tg3_hw_stats));
7384
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007385 for (i = 0; i < tp->irq_cnt; i++) {
7386 struct tg3_napi *tnapi = &tp->napi[i];
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00007387 struct tg3_hw_status *sblk;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007388
Matt Carlson4bae65c2010-11-24 08:31:52 +00007389 tnapi->hw_status = dma_alloc_coherent(&tp->pdev->dev,
7390 TG3_HW_STATUS_SIZE,
7391 &tnapi->status_mapping,
7392 GFP_KERNEL);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007393 if (!tnapi->hw_status)
7394 goto err_out;
7395
7396 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00007397 sblk = tnapi->hw_status;
7398
Matt Carlson8fea32b2010-09-15 08:59:58 +00007399 if (tg3_rx_prodring_init(tp, &tnapi->prodring))
7400 goto err_out;
7401
Matt Carlson19cfaec2009-12-03 08:36:20 +00007402 /* If multivector TSS is enabled, vector 0 does not handle
7403 * tx interrupts. Don't allocate any resources for it.
7404 */
Joe Perches63c3a662011-04-26 08:12:10 +00007405 if ((!i && !tg3_flag(tp, ENABLE_TSS)) ||
7406 (i && tg3_flag(tp, ENABLE_TSS))) {
Matt Carlsondf8944c2011-07-27 14:20:46 +00007407 tnapi->tx_buffers = kzalloc(
7408 sizeof(struct tg3_tx_ring_info) *
7409 TG3_TX_RING_SIZE, GFP_KERNEL);
Matt Carlson19cfaec2009-12-03 08:36:20 +00007410 if (!tnapi->tx_buffers)
7411 goto err_out;
7412
Matt Carlson4bae65c2010-11-24 08:31:52 +00007413 tnapi->tx_ring = dma_alloc_coherent(&tp->pdev->dev,
7414 TG3_TX_RING_BYTES,
7415 &tnapi->tx_desc_mapping,
7416 GFP_KERNEL);
Matt Carlson19cfaec2009-12-03 08:36:20 +00007417 if (!tnapi->tx_ring)
7418 goto err_out;
7419 }
7420
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00007421 /*
7422 * When RSS is enabled, the status block format changes
7423 * slightly. The "rx_jumbo_consumer", "reserved",
7424 * and "rx_mini_consumer" members get mapped to the
7425 * other three rx return ring producer indexes.
7426 */
7427 switch (i) {
7428 default:
7429 tnapi->rx_rcb_prod_idx = &sblk->idx[0].rx_producer;
7430 break;
7431 case 2:
7432 tnapi->rx_rcb_prod_idx = &sblk->rx_jumbo_consumer;
7433 break;
7434 case 3:
7435 tnapi->rx_rcb_prod_idx = &sblk->reserved;
7436 break;
7437 case 4:
7438 tnapi->rx_rcb_prod_idx = &sblk->rx_mini_consumer;
7439 break;
7440 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007441
Matt Carlson0c1d0e22009-09-01 13:16:33 +00007442 /*
7443 * If multivector RSS is enabled, vector 0 does not handle
7444 * rx or tx interrupts. Don't allocate any resources for it.
7445 */
Joe Perches63c3a662011-04-26 08:12:10 +00007446 if (!i && tg3_flag(tp, ENABLE_RSS))
Matt Carlson0c1d0e22009-09-01 13:16:33 +00007447 continue;
7448
Matt Carlson4bae65c2010-11-24 08:31:52 +00007449 tnapi->rx_rcb = dma_alloc_coherent(&tp->pdev->dev,
7450 TG3_RX_RCB_RING_BYTES(tp),
7451 &tnapi->rx_rcb_mapping,
7452 GFP_KERNEL);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007453 if (!tnapi->rx_rcb)
7454 goto err_out;
7455
7456 memset(tnapi->rx_rcb, 0, TG3_RX_RCB_RING_BYTES(tp));
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007457 }
7458
Linus Torvalds1da177e2005-04-16 15:20:36 -07007459 return 0;
7460
7461err_out:
7462 tg3_free_consistent(tp);
7463 return -ENOMEM;
7464}
7465
7466#define MAX_WAIT_CNT 1000
7467
7468/* To stop a block, clear the enable bit and poll till it
7469 * clears. tp->lock is held.
7470 */
David S. Millerb3b7d6b2005-05-05 14:40:20 -07007471static int tg3_stop_block(struct tg3 *tp, unsigned long ofs, u32 enable_bit, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007472{
7473 unsigned int i;
7474 u32 val;
7475
Joe Perches63c3a662011-04-26 08:12:10 +00007476 if (tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007477 switch (ofs) {
7478 case RCVLSC_MODE:
7479 case DMAC_MODE:
7480 case MBFREE_MODE:
7481 case BUFMGR_MODE:
7482 case MEMARB_MODE:
7483 /* We can't enable/disable these bits of the
7484 * 5705/5750, just say success.
7485 */
7486 return 0;
7487
7488 default:
7489 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07007490 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007491 }
7492
7493 val = tr32(ofs);
7494 val &= ~enable_bit;
7495 tw32_f(ofs, val);
7496
7497 for (i = 0; i < MAX_WAIT_CNT; i++) {
7498 udelay(100);
7499 val = tr32(ofs);
7500 if ((val & enable_bit) == 0)
7501 break;
7502 }
7503
David S. Millerb3b7d6b2005-05-05 14:40:20 -07007504 if (i == MAX_WAIT_CNT && !silent) {
Matt Carlson2445e462010-04-05 10:19:21 +00007505 dev_err(&tp->pdev->dev,
7506 "tg3_stop_block timed out, ofs=%lx enable_bit=%x\n",
7507 ofs, enable_bit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007508 return -ENODEV;
7509 }
7510
7511 return 0;
7512}
7513
7514/* tp->lock is held. */
David S. Millerb3b7d6b2005-05-05 14:40:20 -07007515static int tg3_abort_hw(struct tg3 *tp, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007516{
7517 int i, err;
7518
7519 tg3_disable_ints(tp);
7520
7521 tp->rx_mode &= ~RX_MODE_ENABLE;
7522 tw32_f(MAC_RX_MODE, tp->rx_mode);
7523 udelay(10);
7524
David S. Millerb3b7d6b2005-05-05 14:40:20 -07007525 err = tg3_stop_block(tp, RCVBDI_MODE, RCVBDI_MODE_ENABLE, silent);
7526 err |= tg3_stop_block(tp, RCVLPC_MODE, RCVLPC_MODE_ENABLE, silent);
7527 err |= tg3_stop_block(tp, RCVLSC_MODE, RCVLSC_MODE_ENABLE, silent);
7528 err |= tg3_stop_block(tp, RCVDBDI_MODE, RCVDBDI_MODE_ENABLE, silent);
7529 err |= tg3_stop_block(tp, RCVDCC_MODE, RCVDCC_MODE_ENABLE, silent);
7530 err |= tg3_stop_block(tp, RCVCC_MODE, RCVCC_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007531
David S. Millerb3b7d6b2005-05-05 14:40:20 -07007532 err |= tg3_stop_block(tp, SNDBDS_MODE, SNDBDS_MODE_ENABLE, silent);
7533 err |= tg3_stop_block(tp, SNDBDI_MODE, SNDBDI_MODE_ENABLE, silent);
7534 err |= tg3_stop_block(tp, SNDDATAI_MODE, SNDDATAI_MODE_ENABLE, silent);
7535 err |= tg3_stop_block(tp, RDMAC_MODE, RDMAC_MODE_ENABLE, silent);
7536 err |= tg3_stop_block(tp, SNDDATAC_MODE, SNDDATAC_MODE_ENABLE, silent);
7537 err |= tg3_stop_block(tp, DMAC_MODE, DMAC_MODE_ENABLE, silent);
7538 err |= tg3_stop_block(tp, SNDBDC_MODE, SNDBDC_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007539
7540 tp->mac_mode &= ~MAC_MODE_TDE_ENABLE;
7541 tw32_f(MAC_MODE, tp->mac_mode);
7542 udelay(40);
7543
7544 tp->tx_mode &= ~TX_MODE_ENABLE;
7545 tw32_f(MAC_TX_MODE, tp->tx_mode);
7546
7547 for (i = 0; i < MAX_WAIT_CNT; i++) {
7548 udelay(100);
7549 if (!(tr32(MAC_TX_MODE) & TX_MODE_ENABLE))
7550 break;
7551 }
7552 if (i >= MAX_WAIT_CNT) {
Matt Carlsonab96b242010-04-05 10:19:22 +00007553 dev_err(&tp->pdev->dev,
7554 "%s timed out, TX_MODE_ENABLE will not clear "
7555 "MAC_TX_MODE=%08x\n", __func__, tr32(MAC_TX_MODE));
Michael Chane6de8ad2005-05-05 14:42:41 -07007556 err |= -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007557 }
7558
Michael Chane6de8ad2005-05-05 14:42:41 -07007559 err |= tg3_stop_block(tp, HOSTCC_MODE, HOSTCC_MODE_ENABLE, silent);
David S. Millerb3b7d6b2005-05-05 14:40:20 -07007560 err |= tg3_stop_block(tp, WDMAC_MODE, WDMAC_MODE_ENABLE, silent);
7561 err |= tg3_stop_block(tp, MBFREE_MODE, MBFREE_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007562
7563 tw32(FTQ_RESET, 0xffffffff);
7564 tw32(FTQ_RESET, 0x00000000);
7565
David S. Millerb3b7d6b2005-05-05 14:40:20 -07007566 err |= tg3_stop_block(tp, BUFMGR_MODE, BUFMGR_MODE_ENABLE, silent);
7567 err |= tg3_stop_block(tp, MEMARB_MODE, MEMARB_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007568
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007569 for (i = 0; i < tp->irq_cnt; i++) {
7570 struct tg3_napi *tnapi = &tp->napi[i];
7571 if (tnapi->hw_status)
7572 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
7573 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007574 if (tp->hw_stats)
7575 memset(tp->hw_stats, 0, sizeof(struct tg3_hw_stats));
7576
Linus Torvalds1da177e2005-04-16 15:20:36 -07007577 return err;
7578}
7579
Michael Chanee6a99b2007-07-18 21:49:10 -07007580/* Save PCI command register before chip reset */
7581static void tg3_save_pci_state(struct tg3 *tp)
7582{
Matt Carlson8a6eac92007-10-21 16:17:55 -07007583 pci_read_config_word(tp->pdev, PCI_COMMAND, &tp->pci_cmd);
Michael Chanee6a99b2007-07-18 21:49:10 -07007584}
7585
7586/* Restore PCI state after chip reset */
7587static void tg3_restore_pci_state(struct tg3 *tp)
7588{
7589 u32 val;
7590
7591 /* Re-enable indirect register accesses. */
7592 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
7593 tp->misc_host_ctrl);
7594
7595 /* Set MAX PCI retry to zero. */
7596 val = (PCISTATE_ROM_ENABLE | PCISTATE_ROM_RETRY_ENABLE);
7597 if (tp->pci_chip_rev_id == CHIPREV_ID_5704_A0 &&
Joe Perches63c3a662011-04-26 08:12:10 +00007598 tg3_flag(tp, PCIX_MODE))
Michael Chanee6a99b2007-07-18 21:49:10 -07007599 val |= PCISTATE_RETRY_SAME_DMA;
Matt Carlson0d3031d2007-10-10 18:02:43 -07007600 /* Allow reads and writes to the APE register and memory space. */
Joe Perches63c3a662011-04-26 08:12:10 +00007601 if (tg3_flag(tp, ENABLE_APE))
Matt Carlson0d3031d2007-10-10 18:02:43 -07007602 val |= PCISTATE_ALLOW_APE_CTLSPC_WR |
Matt Carlsonf92d9dc12010-06-05 17:24:30 +00007603 PCISTATE_ALLOW_APE_SHMEM_WR |
7604 PCISTATE_ALLOW_APE_PSPACE_WR;
Michael Chanee6a99b2007-07-18 21:49:10 -07007605 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE, val);
7606
Matt Carlson8a6eac92007-10-21 16:17:55 -07007607 pci_write_config_word(tp->pdev, PCI_COMMAND, tp->pci_cmd);
Michael Chanee6a99b2007-07-18 21:49:10 -07007608
Matt Carlsonfcb389d2008-11-03 16:55:44 -08007609 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785) {
Joe Perches63c3a662011-04-26 08:12:10 +00007610 if (tg3_flag(tp, PCI_EXPRESS))
Matt Carlsoncf790032010-11-24 08:31:48 +00007611 pcie_set_readrq(tp->pdev, tp->pcie_readrq);
Matt Carlsonfcb389d2008-11-03 16:55:44 -08007612 else {
7613 pci_write_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE,
7614 tp->pci_cacheline_sz);
7615 pci_write_config_byte(tp->pdev, PCI_LATENCY_TIMER,
7616 tp->pci_lat_timer);
7617 }
Michael Chan114342f2007-10-15 02:12:26 -07007618 }
Matt Carlson5f5c51e2007-11-12 21:19:37 -08007619
Michael Chanee6a99b2007-07-18 21:49:10 -07007620 /* Make sure PCI-X relaxed ordering bit is clear. */
Joe Perches63c3a662011-04-26 08:12:10 +00007621 if (tg3_flag(tp, PCIX_MODE)) {
Matt Carlson9974a352007-10-07 23:27:28 -07007622 u16 pcix_cmd;
7623
7624 pci_read_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
7625 &pcix_cmd);
7626 pcix_cmd &= ~PCI_X_CMD_ERO;
7627 pci_write_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
7628 pcix_cmd);
7629 }
Michael Chanee6a99b2007-07-18 21:49:10 -07007630
Joe Perches63c3a662011-04-26 08:12:10 +00007631 if (tg3_flag(tp, 5780_CLASS)) {
Michael Chanee6a99b2007-07-18 21:49:10 -07007632
7633 /* Chip reset on 5780 will reset MSI enable bit,
7634 * so need to restore it.
7635 */
Joe Perches63c3a662011-04-26 08:12:10 +00007636 if (tg3_flag(tp, USING_MSI)) {
Michael Chanee6a99b2007-07-18 21:49:10 -07007637 u16 ctrl;
7638
7639 pci_read_config_word(tp->pdev,
7640 tp->msi_cap + PCI_MSI_FLAGS,
7641 &ctrl);
7642 pci_write_config_word(tp->pdev,
7643 tp->msi_cap + PCI_MSI_FLAGS,
7644 ctrl | PCI_MSI_FLAGS_ENABLE);
7645 val = tr32(MSGINT_MODE);
7646 tw32(MSGINT_MODE, val | MSGINT_MODE_ENABLE);
7647 }
7648 }
7649}
7650
Linus Torvalds1da177e2005-04-16 15:20:36 -07007651/* tp->lock is held. */
7652static int tg3_chip_reset(struct tg3 *tp)
7653{
7654 u32 val;
Michael Chan1ee582d2005-08-09 20:16:46 -07007655 void (*write_op)(struct tg3 *, u32, u32);
Matt Carlson4f125f42009-09-01 12:55:02 +00007656 int i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007657
David S. Millerf49639e2006-06-09 11:58:36 -07007658 tg3_nvram_lock(tp);
7659
Matt Carlson77b483f2008-08-15 14:07:24 -07007660 tg3_ape_lock(tp, TG3_APE_LOCK_GRC);
7661
David S. Millerf49639e2006-06-09 11:58:36 -07007662 /* No matching tg3_nvram_unlock() after this because
7663 * chip reset below will undo the nvram lock.
7664 */
7665 tp->nvram_lock_cnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007666
Michael Chanee6a99b2007-07-18 21:49:10 -07007667 /* GRC_MISC_CFG core clock reset will clear the memory
7668 * enable bit in PCI register 4 and the MSI enable bit
7669 * on some chips, so we save relevant registers here.
7670 */
7671 tg3_save_pci_state(tp);
7672
Michael Chand9ab5ad12006-03-20 22:27:35 -08007673 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752 ||
Joe Perches63c3a662011-04-26 08:12:10 +00007674 tg3_flag(tp, 5755_PLUS))
Michael Chand9ab5ad12006-03-20 22:27:35 -08007675 tw32(GRC_FASTBOOT_PC, 0);
7676
Linus Torvalds1da177e2005-04-16 15:20:36 -07007677 /*
7678 * We must avoid the readl() that normally takes place.
7679 * It locks machines, causes machine checks, and other
7680 * fun things. So, temporarily disable the 5701
7681 * hardware workaround, while we do the reset.
7682 */
Michael Chan1ee582d2005-08-09 20:16:46 -07007683 write_op = tp->write32;
7684 if (write_op == tg3_write_flush_reg32)
7685 tp->write32 = tg3_write32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007686
Michael Chand18edcb2007-03-24 20:57:11 -07007687 /* Prevent the irq handler from reading or writing PCI registers
7688 * during chip reset when the memory enable bit in the PCI command
7689 * register may be cleared. The chip does not generate interrupt
7690 * at this time, but the irq handler may still be called due to irq
7691 * sharing or irqpoll.
7692 */
Joe Perches63c3a662011-04-26 08:12:10 +00007693 tg3_flag_set(tp, CHIP_RESETTING);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007694 for (i = 0; i < tp->irq_cnt; i++) {
7695 struct tg3_napi *tnapi = &tp->napi[i];
7696 if (tnapi->hw_status) {
7697 tnapi->hw_status->status = 0;
7698 tnapi->hw_status->status_tag = 0;
7699 }
7700 tnapi->last_tag = 0;
7701 tnapi->last_irq_tag = 0;
Michael Chanb8fa2f32007-04-06 17:35:37 -07007702 }
Michael Chand18edcb2007-03-24 20:57:11 -07007703 smp_mb();
Matt Carlson4f125f42009-09-01 12:55:02 +00007704
7705 for (i = 0; i < tp->irq_cnt; i++)
7706 synchronize_irq(tp->napi[i].irq_vec);
Michael Chand18edcb2007-03-24 20:57:11 -07007707
Matt Carlson255ca312009-08-25 10:07:27 +00007708 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780) {
7709 val = tr32(TG3_PCIE_LNKCTL) & ~TG3_PCIE_LNKCTL_L1_PLL_PD_EN;
7710 tw32(TG3_PCIE_LNKCTL, val | TG3_PCIE_LNKCTL_L1_PLL_PD_DIS);
7711 }
7712
Linus Torvalds1da177e2005-04-16 15:20:36 -07007713 /* do the reset */
7714 val = GRC_MISC_CFG_CORECLK_RESET;
7715
Joe Perches63c3a662011-04-26 08:12:10 +00007716 if (tg3_flag(tp, PCI_EXPRESS)) {
Matt Carlson88075d92010-08-02 11:25:58 +00007717 /* Force PCIe 1.0a mode */
7718 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
Joe Perches63c3a662011-04-26 08:12:10 +00007719 !tg3_flag(tp, 57765_PLUS) &&
Matt Carlson88075d92010-08-02 11:25:58 +00007720 tr32(TG3_PCIE_PHY_TSTCTL) ==
7721 (TG3_PCIE_PHY_TSTCTL_PCIE10 | TG3_PCIE_PHY_TSTCTL_PSCRAM))
7722 tw32(TG3_PCIE_PHY_TSTCTL, TG3_PCIE_PHY_TSTCTL_PSCRAM);
7723
Linus Torvalds1da177e2005-04-16 15:20:36 -07007724 if (tp->pci_chip_rev_id != CHIPREV_ID_5750_A0) {
7725 tw32(GRC_MISC_CFG, (1 << 29));
7726 val |= (1 << 29);
7727 }
7728 }
7729
Michael Chanb5d37722006-09-27 16:06:21 -07007730 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
7731 tw32(VCPU_STATUS, tr32(VCPU_STATUS) | VCPU_STATUS_DRV_RESET);
7732 tw32(GRC_VCPU_EXT_CTRL,
7733 tr32(GRC_VCPU_EXT_CTRL) & ~GRC_VCPU_EXT_CTRL_HALT_CPU);
7734 }
7735
Matt Carlsonf37500d2010-08-02 11:25:59 +00007736 /* Manage gphy power for all CPMU absent PCIe devices. */
Joe Perches63c3a662011-04-26 08:12:10 +00007737 if (tg3_flag(tp, 5705_PLUS) && !tg3_flag(tp, CPMU_PRESENT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007738 val |= GRC_MISC_CFG_KEEP_GPHY_POWER;
Matt Carlsonf37500d2010-08-02 11:25:59 +00007739
Linus Torvalds1da177e2005-04-16 15:20:36 -07007740 tw32(GRC_MISC_CFG, val);
7741
Michael Chan1ee582d2005-08-09 20:16:46 -07007742 /* restore 5701 hardware bug workaround write method */
7743 tp->write32 = write_op;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007744
7745 /* Unfortunately, we have to delay before the PCI read back.
7746 * Some 575X chips even will not respond to a PCI cfg access
7747 * when the reset command is given to the chip.
7748 *
7749 * How do these hardware designers expect things to work
7750 * properly if the PCI write is posted for a long period
7751 * of time? It is always necessary to have some method by
7752 * which a register read back can occur to push the write
7753 * out which does the reset.
7754 *
7755 * For most tg3 variants the trick below was working.
7756 * Ho hum...
7757 */
7758 udelay(120);
7759
7760 /* Flush PCI posted writes. The normal MMIO registers
7761 * are inaccessible at this time so this is the only
7762 * way to make this reliably (actually, this is no longer
7763 * the case, see above). I tried to use indirect
7764 * register read/write but this upset some 5701 variants.
7765 */
7766 pci_read_config_dword(tp->pdev, PCI_COMMAND, &val);
7767
7768 udelay(120);
7769
Jon Mason708ebb3a2011-06-27 12:56:50 +00007770 if (tg3_flag(tp, PCI_EXPRESS) && pci_pcie_cap(tp->pdev)) {
Matt Carlsone7126992009-08-25 10:08:16 +00007771 u16 val16;
7772
Linus Torvalds1da177e2005-04-16 15:20:36 -07007773 if (tp->pci_chip_rev_id == CHIPREV_ID_5750_A0) {
7774 int i;
7775 u32 cfg_val;
7776
7777 /* Wait for link training to complete. */
7778 for (i = 0; i < 5000; i++)
7779 udelay(100);
7780
7781 pci_read_config_dword(tp->pdev, 0xc4, &cfg_val);
7782 pci_write_config_dword(tp->pdev, 0xc4,
7783 cfg_val | (1 << 15));
7784 }
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007785
Matt Carlsone7126992009-08-25 10:08:16 +00007786 /* Clear the "no snoop" and "relaxed ordering" bits. */
7787 pci_read_config_word(tp->pdev,
Jon Mason708ebb3a2011-06-27 12:56:50 +00007788 pci_pcie_cap(tp->pdev) + PCI_EXP_DEVCTL,
Matt Carlsone7126992009-08-25 10:08:16 +00007789 &val16);
7790 val16 &= ~(PCI_EXP_DEVCTL_RELAX_EN |
7791 PCI_EXP_DEVCTL_NOSNOOP_EN);
7792 /*
7793 * Older PCIe devices only support the 128 byte
7794 * MPS setting. Enforce the restriction.
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007795 */
Joe Perches63c3a662011-04-26 08:12:10 +00007796 if (!tg3_flag(tp, CPMU_PRESENT))
Matt Carlsone7126992009-08-25 10:08:16 +00007797 val16 &= ~PCI_EXP_DEVCTL_PAYLOAD;
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007798 pci_write_config_word(tp->pdev,
Jon Mason708ebb3a2011-06-27 12:56:50 +00007799 pci_pcie_cap(tp->pdev) + PCI_EXP_DEVCTL,
Matt Carlsone7126992009-08-25 10:08:16 +00007800 val16);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007801
Matt Carlsoncf790032010-11-24 08:31:48 +00007802 pcie_set_readrq(tp->pdev, tp->pcie_readrq);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007803
7804 /* Clear error status */
7805 pci_write_config_word(tp->pdev,
Jon Mason708ebb3a2011-06-27 12:56:50 +00007806 pci_pcie_cap(tp->pdev) + PCI_EXP_DEVSTA,
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007807 PCI_EXP_DEVSTA_CED |
7808 PCI_EXP_DEVSTA_NFED |
7809 PCI_EXP_DEVSTA_FED |
7810 PCI_EXP_DEVSTA_URD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007811 }
7812
Michael Chanee6a99b2007-07-18 21:49:10 -07007813 tg3_restore_pci_state(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007814
Joe Perches63c3a662011-04-26 08:12:10 +00007815 tg3_flag_clear(tp, CHIP_RESETTING);
7816 tg3_flag_clear(tp, ERROR_PROCESSED);
Michael Chand18edcb2007-03-24 20:57:11 -07007817
Michael Chanee6a99b2007-07-18 21:49:10 -07007818 val = 0;
Joe Perches63c3a662011-04-26 08:12:10 +00007819 if (tg3_flag(tp, 5780_CLASS))
Michael Chan4cf78e42005-07-25 12:29:19 -07007820 val = tr32(MEMARB_MODE);
Michael Chanee6a99b2007-07-18 21:49:10 -07007821 tw32(MEMARB_MODE, val | MEMARB_MODE_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007822
7823 if (tp->pci_chip_rev_id == CHIPREV_ID_5750_A3) {
7824 tg3_stop_fw(tp);
7825 tw32(0x5000, 0x400);
7826 }
7827
7828 tw32(GRC_MODE, tp->grc_mode);
7829
7830 if (tp->pci_chip_rev_id == CHIPREV_ID_5705_A0) {
Andy Gospodarekab0049b2007-09-06 20:42:14 +01007831 val = tr32(0xc4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007832
7833 tw32(0xc4, val | (1 << 15));
7834 }
7835
7836 if ((tp->nic_sram_data_cfg & NIC_SRAM_DATA_CFG_MINI_PCI) != 0 &&
7837 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
7838 tp->pci_clock_ctrl |= CLOCK_CTRL_CLKRUN_OENABLE;
7839 if (tp->pci_chip_rev_id == CHIPREV_ID_5705_A0)
7840 tp->pci_clock_ctrl |= CLOCK_CTRL_FORCE_CLKRUN;
7841 tw32(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl);
7842 }
7843
Matt Carlsonf07e9af2010-08-02 11:26:07 +00007844 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Matt Carlson9e975cc2011-07-20 10:20:50 +00007845 tp->mac_mode = MAC_MODE_PORT_MODE_TBI;
Matt Carlsond2394e6b2010-11-24 08:31:47 +00007846 val = tp->mac_mode;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00007847 } else if (tp->phy_flags & TG3_PHYFLG_MII_SERDES) {
Matt Carlson9e975cc2011-07-20 10:20:50 +00007848 tp->mac_mode = MAC_MODE_PORT_MODE_GMII;
Matt Carlsond2394e6b2010-11-24 08:31:47 +00007849 val = tp->mac_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007850 } else
Matt Carlsond2394e6b2010-11-24 08:31:47 +00007851 val = 0;
7852
7853 tw32_f(MAC_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007854 udelay(40);
7855
Matt Carlson77b483f2008-08-15 14:07:24 -07007856 tg3_ape_unlock(tp, TG3_APE_LOCK_GRC);
7857
Michael Chan7a6f4362006-09-27 16:03:31 -07007858 err = tg3_poll_fw(tp);
7859 if (err)
7860 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007861
Matt Carlson0a9140c2009-08-28 12:27:50 +00007862 tg3_mdio_start(tp);
7863
Joe Perches63c3a662011-04-26 08:12:10 +00007864 if (tg3_flag(tp, PCI_EXPRESS) &&
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00007865 tp->pci_chip_rev_id != CHIPREV_ID_5750_A0 &&
7866 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
Joe Perches63c3a662011-04-26 08:12:10 +00007867 !tg3_flag(tp, 57765_PLUS)) {
Andy Gospodarekab0049b2007-09-06 20:42:14 +01007868 val = tr32(0x7c00);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007869
7870 tw32(0x7c00, val | (1 << 25));
7871 }
7872
Matt Carlsond78b59f2011-04-05 14:22:46 +00007873 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
7874 val = tr32(TG3_CPMU_CLCK_ORIDE);
7875 tw32(TG3_CPMU_CLCK_ORIDE, val & ~CPMU_CLCK_ORIDE_MAC_ORIDE_EN);
7876 }
7877
Linus Torvalds1da177e2005-04-16 15:20:36 -07007878 /* Reprobe ASF enable state. */
Joe Perches63c3a662011-04-26 08:12:10 +00007879 tg3_flag_clear(tp, ENABLE_ASF);
7880 tg3_flag_clear(tp, ASF_NEW_HANDSHAKE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007881 tg3_read_mem(tp, NIC_SRAM_DATA_SIG, &val);
7882 if (val == NIC_SRAM_DATA_SIG_MAGIC) {
7883 u32 nic_cfg;
7884
7885 tg3_read_mem(tp, NIC_SRAM_DATA_CFG, &nic_cfg);
7886 if (nic_cfg & NIC_SRAM_DATA_CFG_ASF_ENABLE) {
Joe Perches63c3a662011-04-26 08:12:10 +00007887 tg3_flag_set(tp, ENABLE_ASF);
Matt Carlson4ba526c2008-08-15 14:10:04 -07007888 tp->last_event_jiffies = jiffies;
Joe Perches63c3a662011-04-26 08:12:10 +00007889 if (tg3_flag(tp, 5750_PLUS))
7890 tg3_flag_set(tp, ASF_NEW_HANDSHAKE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007891 }
7892 }
7893
7894 return 0;
7895}
7896
7897/* tp->lock is held. */
Michael Chan944d9802005-05-29 14:57:48 -07007898static int tg3_halt(struct tg3 *tp, int kind, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007899{
7900 int err;
7901
7902 tg3_stop_fw(tp);
7903
Michael Chan944d9802005-05-29 14:57:48 -07007904 tg3_write_sig_pre_reset(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007905
David S. Millerb3b7d6b2005-05-05 14:40:20 -07007906 tg3_abort_hw(tp, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007907 err = tg3_chip_reset(tp);
7908
Matt Carlsondaba2a62009-04-20 06:58:52 +00007909 __tg3_set_mac_addr(tp, 0);
7910
Michael Chan944d9802005-05-29 14:57:48 -07007911 tg3_write_sig_legacy(tp, kind);
7912 tg3_write_sig_post_reset(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007913
7914 if (err)
7915 return err;
7916
7917 return 0;
7918}
7919
Linus Torvalds1da177e2005-04-16 15:20:36 -07007920static int tg3_set_mac_addr(struct net_device *dev, void *p)
7921{
7922 struct tg3 *tp = netdev_priv(dev);
7923 struct sockaddr *addr = p;
Michael Chan986e0ae2007-05-05 12:10:20 -07007924 int err = 0, skip_mac_1 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007925
Michael Chanf9804dd2005-09-27 12:13:10 -07007926 if (!is_valid_ether_addr(addr->sa_data))
7927 return -EINVAL;
7928
Linus Torvalds1da177e2005-04-16 15:20:36 -07007929 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
7930
Michael Chane75f7c92006-03-20 21:33:26 -08007931 if (!netif_running(dev))
7932 return 0;
7933
Joe Perches63c3a662011-04-26 08:12:10 +00007934 if (tg3_flag(tp, ENABLE_ASF)) {
Michael Chan986e0ae2007-05-05 12:10:20 -07007935 u32 addr0_high, addr0_low, addr1_high, addr1_low;
Michael Chan58712ef2006-04-29 18:58:01 -07007936
Michael Chan986e0ae2007-05-05 12:10:20 -07007937 addr0_high = tr32(MAC_ADDR_0_HIGH);
7938 addr0_low = tr32(MAC_ADDR_0_LOW);
7939 addr1_high = tr32(MAC_ADDR_1_HIGH);
7940 addr1_low = tr32(MAC_ADDR_1_LOW);
7941
7942 /* Skip MAC addr 1 if ASF is using it. */
7943 if ((addr0_high != addr1_high || addr0_low != addr1_low) &&
7944 !(addr1_high == 0 && addr1_low == 0))
7945 skip_mac_1 = 1;
Michael Chan58712ef2006-04-29 18:58:01 -07007946 }
Michael Chan986e0ae2007-05-05 12:10:20 -07007947 spin_lock_bh(&tp->lock);
7948 __tg3_set_mac_addr(tp, skip_mac_1);
7949 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007950
Michael Chanb9ec6c12006-07-25 16:37:27 -07007951 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007952}
7953
7954/* tp->lock is held. */
7955static void tg3_set_bdinfo(struct tg3 *tp, u32 bdinfo_addr,
7956 dma_addr_t mapping, u32 maxlen_flags,
7957 u32 nic_addr)
7958{
7959 tg3_write_mem(tp,
7960 (bdinfo_addr + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH),
7961 ((u64) mapping >> 32));
7962 tg3_write_mem(tp,
7963 (bdinfo_addr + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW),
7964 ((u64) mapping & 0xffffffff));
7965 tg3_write_mem(tp,
7966 (bdinfo_addr + TG3_BDINFO_MAXLEN_FLAGS),
7967 maxlen_flags);
7968
Joe Perches63c3a662011-04-26 08:12:10 +00007969 if (!tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007970 tg3_write_mem(tp,
7971 (bdinfo_addr + TG3_BDINFO_NIC_ADDR),
7972 nic_addr);
7973}
7974
7975static void __tg3_set_rx_mode(struct net_device *);
Michael Chand244c892005-07-05 14:42:33 -07007976static void __tg3_set_coalesce(struct tg3 *tp, struct ethtool_coalesce *ec)
David S. Miller15f98502005-05-18 22:49:26 -07007977{
Matt Carlsonb6080e12009-09-01 13:12:00 +00007978 int i;
7979
Joe Perches63c3a662011-04-26 08:12:10 +00007980 if (!tg3_flag(tp, ENABLE_TSS)) {
Matt Carlsonb6080e12009-09-01 13:12:00 +00007981 tw32(HOSTCC_TXCOL_TICKS, ec->tx_coalesce_usecs);
7982 tw32(HOSTCC_TXMAX_FRAMES, ec->tx_max_coalesced_frames);
7983 tw32(HOSTCC_TXCOAL_MAXF_INT, ec->tx_max_coalesced_frames_irq);
Matt Carlsonb6080e12009-09-01 13:12:00 +00007984 } else {
7985 tw32(HOSTCC_TXCOL_TICKS, 0);
7986 tw32(HOSTCC_TXMAX_FRAMES, 0);
7987 tw32(HOSTCC_TXCOAL_MAXF_INT, 0);
Matt Carlson19cfaec2009-12-03 08:36:20 +00007988 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00007989
Joe Perches63c3a662011-04-26 08:12:10 +00007990 if (!tg3_flag(tp, ENABLE_RSS)) {
Matt Carlson19cfaec2009-12-03 08:36:20 +00007991 tw32(HOSTCC_RXCOL_TICKS, ec->rx_coalesce_usecs);
7992 tw32(HOSTCC_RXMAX_FRAMES, ec->rx_max_coalesced_frames);
7993 tw32(HOSTCC_RXCOAL_MAXF_INT, ec->rx_max_coalesced_frames_irq);
7994 } else {
Matt Carlsonb6080e12009-09-01 13:12:00 +00007995 tw32(HOSTCC_RXCOL_TICKS, 0);
7996 tw32(HOSTCC_RXMAX_FRAMES, 0);
7997 tw32(HOSTCC_RXCOAL_MAXF_INT, 0);
David S. Miller15f98502005-05-18 22:49:26 -07007998 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00007999
Joe Perches63c3a662011-04-26 08:12:10 +00008000 if (!tg3_flag(tp, 5705_PLUS)) {
David S. Miller15f98502005-05-18 22:49:26 -07008001 u32 val = ec->stats_block_coalesce_usecs;
8002
Matt Carlsonb6080e12009-09-01 13:12:00 +00008003 tw32(HOSTCC_RXCOAL_TICK_INT, ec->rx_coalesce_usecs_irq);
8004 tw32(HOSTCC_TXCOAL_TICK_INT, ec->tx_coalesce_usecs_irq);
8005
David S. Miller15f98502005-05-18 22:49:26 -07008006 if (!netif_carrier_ok(tp->dev))
8007 val = 0;
8008
8009 tw32(HOSTCC_STAT_COAL_TICKS, val);
8010 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00008011
8012 for (i = 0; i < tp->irq_cnt - 1; i++) {
8013 u32 reg;
8014
8015 reg = HOSTCC_RXCOL_TICKS_VEC1 + i * 0x18;
8016 tw32(reg, ec->rx_coalesce_usecs);
Matt Carlsonb6080e12009-09-01 13:12:00 +00008017 reg = HOSTCC_RXMAX_FRAMES_VEC1 + i * 0x18;
8018 tw32(reg, ec->rx_max_coalesced_frames);
Matt Carlsonb6080e12009-09-01 13:12:00 +00008019 reg = HOSTCC_RXCOAL_MAXF_INT_VEC1 + i * 0x18;
8020 tw32(reg, ec->rx_max_coalesced_frames_irq);
Matt Carlson19cfaec2009-12-03 08:36:20 +00008021
Joe Perches63c3a662011-04-26 08:12:10 +00008022 if (tg3_flag(tp, ENABLE_TSS)) {
Matt Carlson19cfaec2009-12-03 08:36:20 +00008023 reg = HOSTCC_TXCOL_TICKS_VEC1 + i * 0x18;
8024 tw32(reg, ec->tx_coalesce_usecs);
8025 reg = HOSTCC_TXMAX_FRAMES_VEC1 + i * 0x18;
8026 tw32(reg, ec->tx_max_coalesced_frames);
8027 reg = HOSTCC_TXCOAL_MAXF_INT_VEC1 + i * 0x18;
8028 tw32(reg, ec->tx_max_coalesced_frames_irq);
8029 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00008030 }
8031
8032 for (; i < tp->irq_max - 1; i++) {
8033 tw32(HOSTCC_RXCOL_TICKS_VEC1 + i * 0x18, 0);
Matt Carlsonb6080e12009-09-01 13:12:00 +00008034 tw32(HOSTCC_RXMAX_FRAMES_VEC1 + i * 0x18, 0);
Matt Carlsonb6080e12009-09-01 13:12:00 +00008035 tw32(HOSTCC_RXCOAL_MAXF_INT_VEC1 + i * 0x18, 0);
Matt Carlson19cfaec2009-12-03 08:36:20 +00008036
Joe Perches63c3a662011-04-26 08:12:10 +00008037 if (tg3_flag(tp, ENABLE_TSS)) {
Matt Carlson19cfaec2009-12-03 08:36:20 +00008038 tw32(HOSTCC_TXCOL_TICKS_VEC1 + i * 0x18, 0);
8039 tw32(HOSTCC_TXMAX_FRAMES_VEC1 + i * 0x18, 0);
8040 tw32(HOSTCC_TXCOAL_MAXF_INT_VEC1 + i * 0x18, 0);
8041 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00008042 }
David S. Miller15f98502005-05-18 22:49:26 -07008043}
Linus Torvalds1da177e2005-04-16 15:20:36 -07008044
8045/* tp->lock is held. */
Matt Carlson2d31eca2009-09-01 12:53:31 +00008046static void tg3_rings_reset(struct tg3 *tp)
8047{
8048 int i;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008049 u32 stblk, txrcb, rxrcb, limit;
Matt Carlson2d31eca2009-09-01 12:53:31 +00008050 struct tg3_napi *tnapi = &tp->napi[0];
8051
8052 /* Disable all transmit rings but the first. */
Joe Perches63c3a662011-04-26 08:12:10 +00008053 if (!tg3_flag(tp, 5705_PLUS))
Matt Carlson2d31eca2009-09-01 12:53:31 +00008054 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 16;
Joe Perches63c3a662011-04-26 08:12:10 +00008055 else if (tg3_flag(tp, 5717_PLUS))
Matt Carlson3d377282010-10-14 10:37:39 +00008056 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 4;
Matt Carlsonb703df62009-12-03 08:36:21 +00008057 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
8058 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 2;
Matt Carlson2d31eca2009-09-01 12:53:31 +00008059 else
8060 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE;
8061
8062 for (txrcb = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE;
8063 txrcb < limit; txrcb += TG3_BDINFO_SIZE)
8064 tg3_write_mem(tp, txrcb + TG3_BDINFO_MAXLEN_FLAGS,
8065 BDINFO_FLAGS_DISABLED);
8066
8067
8068 /* Disable all receive return rings but the first. */
Joe Perches63c3a662011-04-26 08:12:10 +00008069 if (tg3_flag(tp, 5717_PLUS))
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00008070 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 17;
Joe Perches63c3a662011-04-26 08:12:10 +00008071 else if (!tg3_flag(tp, 5705_PLUS))
Matt Carlson2d31eca2009-09-01 12:53:31 +00008072 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 16;
Matt Carlsonb703df62009-12-03 08:36:21 +00008073 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
8074 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Matt Carlson2d31eca2009-09-01 12:53:31 +00008075 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 4;
8076 else
8077 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE;
8078
8079 for (rxrcb = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE;
8080 rxrcb < limit; rxrcb += TG3_BDINFO_SIZE)
8081 tg3_write_mem(tp, rxrcb + TG3_BDINFO_MAXLEN_FLAGS,
8082 BDINFO_FLAGS_DISABLED);
8083
8084 /* Disable interrupts */
8085 tw32_mailbox_f(tp->napi[0].int_mbox, 1);
Matt Carlson0e6cf6a2011-06-13 13:38:55 +00008086 tp->napi[0].chk_msi_cnt = 0;
8087 tp->napi[0].last_rx_cons = 0;
8088 tp->napi[0].last_tx_cons = 0;
Matt Carlson2d31eca2009-09-01 12:53:31 +00008089
8090 /* Zero mailbox registers. */
Joe Perches63c3a662011-04-26 08:12:10 +00008091 if (tg3_flag(tp, SUPPORT_MSIX)) {
Matt Carlson6fd45cb2010-09-15 08:59:57 +00008092 for (i = 1; i < tp->irq_max; i++) {
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008093 tp->napi[i].tx_prod = 0;
8094 tp->napi[i].tx_cons = 0;
Joe Perches63c3a662011-04-26 08:12:10 +00008095 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlsonc2353a32010-01-20 16:58:08 +00008096 tw32_mailbox(tp->napi[i].prodmbox, 0);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008097 tw32_rx_mbox(tp->napi[i].consmbox, 0);
8098 tw32_mailbox_f(tp->napi[i].int_mbox, 1);
Matt Carlson7f230732011-08-31 11:44:48 +00008099 tp->napi[i].chk_msi_cnt = 0;
Matt Carlson0e6cf6a2011-06-13 13:38:55 +00008100 tp->napi[i].last_rx_cons = 0;
8101 tp->napi[i].last_tx_cons = 0;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008102 }
Joe Perches63c3a662011-04-26 08:12:10 +00008103 if (!tg3_flag(tp, ENABLE_TSS))
Matt Carlsonc2353a32010-01-20 16:58:08 +00008104 tw32_mailbox(tp->napi[0].prodmbox, 0);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008105 } else {
8106 tp->napi[0].tx_prod = 0;
8107 tp->napi[0].tx_cons = 0;
8108 tw32_mailbox(tp->napi[0].prodmbox, 0);
8109 tw32_rx_mbox(tp->napi[0].consmbox, 0);
8110 }
Matt Carlson2d31eca2009-09-01 12:53:31 +00008111
8112 /* Make sure the NIC-based send BD rings are disabled. */
Joe Perches63c3a662011-04-26 08:12:10 +00008113 if (!tg3_flag(tp, 5705_PLUS)) {
Matt Carlson2d31eca2009-09-01 12:53:31 +00008114 u32 mbox = MAILBOX_SNDNIC_PROD_IDX_0 + TG3_64BIT_REG_LOW;
8115 for (i = 0; i < 16; i++)
8116 tw32_tx_mbox(mbox + i * 8, 0);
8117 }
8118
8119 txrcb = NIC_SRAM_SEND_RCB;
8120 rxrcb = NIC_SRAM_RCV_RET_RCB;
8121
8122 /* Clear status block in ram. */
8123 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
8124
8125 /* Set status block DMA address */
8126 tw32(HOSTCC_STATUS_BLK_HOST_ADDR + TG3_64BIT_REG_HIGH,
8127 ((u64) tnapi->status_mapping >> 32));
8128 tw32(HOSTCC_STATUS_BLK_HOST_ADDR + TG3_64BIT_REG_LOW,
8129 ((u64) tnapi->status_mapping & 0xffffffff));
8130
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008131 if (tnapi->tx_ring) {
8132 tg3_set_bdinfo(tp, txrcb, tnapi->tx_desc_mapping,
8133 (TG3_TX_RING_SIZE <<
8134 BDINFO_FLAGS_MAXLEN_SHIFT),
8135 NIC_SRAM_TX_BUFFER_DESC);
8136 txrcb += TG3_BDINFO_SIZE;
8137 }
Matt Carlson2d31eca2009-09-01 12:53:31 +00008138
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008139 if (tnapi->rx_rcb) {
8140 tg3_set_bdinfo(tp, rxrcb, tnapi->rx_rcb_mapping,
Matt Carlson7cb32cf2010-09-30 10:34:36 +00008141 (tp->rx_ret_ring_mask + 1) <<
8142 BDINFO_FLAGS_MAXLEN_SHIFT, 0);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008143 rxrcb += TG3_BDINFO_SIZE;
8144 }
8145
8146 stblk = HOSTCC_STATBLCK_RING1;
8147
8148 for (i = 1, tnapi++; i < tp->irq_cnt; i++, tnapi++) {
8149 u64 mapping = (u64)tnapi->status_mapping;
8150 tw32(stblk + TG3_64BIT_REG_HIGH, mapping >> 32);
8151 tw32(stblk + TG3_64BIT_REG_LOW, mapping & 0xffffffff);
8152
8153 /* Clear status block in ram. */
8154 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
8155
Matt Carlson19cfaec2009-12-03 08:36:20 +00008156 if (tnapi->tx_ring) {
8157 tg3_set_bdinfo(tp, txrcb, tnapi->tx_desc_mapping,
8158 (TG3_TX_RING_SIZE <<
8159 BDINFO_FLAGS_MAXLEN_SHIFT),
8160 NIC_SRAM_TX_BUFFER_DESC);
8161 txrcb += TG3_BDINFO_SIZE;
8162 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008163
8164 tg3_set_bdinfo(tp, rxrcb, tnapi->rx_rcb_mapping,
Matt Carlson7cb32cf2010-09-30 10:34:36 +00008165 ((tp->rx_ret_ring_mask + 1) <<
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008166 BDINFO_FLAGS_MAXLEN_SHIFT), 0);
8167
8168 stblk += 8;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008169 rxrcb += TG3_BDINFO_SIZE;
8170 }
Matt Carlson2d31eca2009-09-01 12:53:31 +00008171}
8172
Matt Carlsoneb07a942011-04-20 07:57:36 +00008173static void tg3_setup_rxbd_thresholds(struct tg3 *tp)
8174{
8175 u32 val, bdcache_maxcnt, host_rep_thresh, nic_rep_thresh;
8176
Joe Perches63c3a662011-04-26 08:12:10 +00008177 if (!tg3_flag(tp, 5750_PLUS) ||
8178 tg3_flag(tp, 5780_CLASS) ||
Matt Carlsoneb07a942011-04-20 07:57:36 +00008179 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 ||
8180 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752)
8181 bdcache_maxcnt = TG3_SRAM_RX_STD_BDCACHE_SIZE_5700;
8182 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
8183 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787)
8184 bdcache_maxcnt = TG3_SRAM_RX_STD_BDCACHE_SIZE_5755;
8185 else
8186 bdcache_maxcnt = TG3_SRAM_RX_STD_BDCACHE_SIZE_5906;
8187
8188 nic_rep_thresh = min(bdcache_maxcnt / 2, tp->rx_std_max_post);
8189 host_rep_thresh = max_t(u32, tp->rx_pending / 8, 1);
8190
8191 val = min(nic_rep_thresh, host_rep_thresh);
8192 tw32(RCVBDI_STD_THRESH, val);
8193
Joe Perches63c3a662011-04-26 08:12:10 +00008194 if (tg3_flag(tp, 57765_PLUS))
Matt Carlsoneb07a942011-04-20 07:57:36 +00008195 tw32(STD_REPLENISH_LWM, bdcache_maxcnt);
8196
Joe Perches63c3a662011-04-26 08:12:10 +00008197 if (!tg3_flag(tp, JUMBO_CAPABLE) || tg3_flag(tp, 5780_CLASS))
Matt Carlsoneb07a942011-04-20 07:57:36 +00008198 return;
8199
Joe Perches63c3a662011-04-26 08:12:10 +00008200 if (!tg3_flag(tp, 5705_PLUS))
Matt Carlsoneb07a942011-04-20 07:57:36 +00008201 bdcache_maxcnt = TG3_SRAM_RX_JMB_BDCACHE_SIZE_5700;
8202 else
8203 bdcache_maxcnt = TG3_SRAM_RX_JMB_BDCACHE_SIZE_5717;
8204
8205 host_rep_thresh = max_t(u32, tp->rx_jumbo_pending / 8, 1);
8206
8207 val = min(bdcache_maxcnt / 2, host_rep_thresh);
8208 tw32(RCVBDI_JUMBO_THRESH, val);
8209
Joe Perches63c3a662011-04-26 08:12:10 +00008210 if (tg3_flag(tp, 57765_PLUS))
Matt Carlsoneb07a942011-04-20 07:57:36 +00008211 tw32(JMB_REPLENISH_LWM, bdcache_maxcnt);
8212}
8213
Matt Carlson2d31eca2009-09-01 12:53:31 +00008214/* tp->lock is held. */
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07008215static int tg3_reset_hw(struct tg3 *tp, int reset_phy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008216{
8217 u32 val, rdmac_mode;
8218 int i, err, limit;
Matt Carlson8fea32b2010-09-15 08:59:58 +00008219 struct tg3_rx_prodring_set *tpr = &tp->napi[0].prodring;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008220
8221 tg3_disable_ints(tp);
8222
8223 tg3_stop_fw(tp);
8224
8225 tg3_write_sig_pre_reset(tp, RESET_KIND_INIT);
8226
Joe Perches63c3a662011-04-26 08:12:10 +00008227 if (tg3_flag(tp, INIT_COMPLETE))
Michael Chane6de8ad2005-05-05 14:42:41 -07008228 tg3_abort_hw(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008229
Matt Carlson699c0192010-12-06 08:28:51 +00008230 /* Enable MAC control of LPI */
8231 if (tp->phy_flags & TG3_PHYFLG_EEE_CAP) {
8232 tw32_f(TG3_CPMU_EEE_LNKIDL_CTRL,
8233 TG3_CPMU_EEE_LNKIDL_PCIE_NL0 |
8234 TG3_CPMU_EEE_LNKIDL_UART_IDL);
8235
8236 tw32_f(TG3_CPMU_EEE_CTRL,
8237 TG3_CPMU_EEE_CTRL_EXIT_20_1_US);
8238
Matt Carlsona386b902010-12-06 08:28:53 +00008239 val = TG3_CPMU_EEEMD_ERLY_L1_XIT_DET |
8240 TG3_CPMU_EEEMD_LPI_IN_TX |
8241 TG3_CPMU_EEEMD_LPI_IN_RX |
8242 TG3_CPMU_EEEMD_EEE_ENABLE;
8243
8244 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717)
8245 val |= TG3_CPMU_EEEMD_SND_IDX_DET_EN;
8246
Joe Perches63c3a662011-04-26 08:12:10 +00008247 if (tg3_flag(tp, ENABLE_APE))
Matt Carlsona386b902010-12-06 08:28:53 +00008248 val |= TG3_CPMU_EEEMD_APE_TX_DET_EN;
8249
8250 tw32_f(TG3_CPMU_EEE_MODE, val);
8251
8252 tw32_f(TG3_CPMU_EEE_DBTMR1,
8253 TG3_CPMU_DBTMR1_PCIEXIT_2047US |
8254 TG3_CPMU_DBTMR1_LNKIDLE_2047US);
8255
8256 tw32_f(TG3_CPMU_EEE_DBTMR2,
Matt Carlsond7f2ab22011-01-25 15:58:56 +00008257 TG3_CPMU_DBTMR2_APE_TX_2047US |
Matt Carlsona386b902010-12-06 08:28:53 +00008258 TG3_CPMU_DBTMR2_TXIDXEQ_2047US);
Matt Carlson699c0192010-12-06 08:28:51 +00008259 }
8260
Matt Carlson603f1172010-02-12 14:47:10 +00008261 if (reset_phy)
Michael Chand4d2c552006-03-20 17:47:20 -08008262 tg3_phy_reset(tp);
8263
Linus Torvalds1da177e2005-04-16 15:20:36 -07008264 err = tg3_chip_reset(tp);
8265 if (err)
8266 return err;
8267
8268 tg3_write_sig_legacy(tp, RESET_KIND_INIT);
8269
Matt Carlsonbcb37f62008-11-03 16:52:09 -08008270 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX) {
Matt Carlsond30cdd22007-10-07 23:28:35 -07008271 val = tr32(TG3_CPMU_CTRL);
8272 val &= ~(CPMU_CTRL_LINK_AWARE_MODE | CPMU_CTRL_LINK_IDLE_MODE);
8273 tw32(TG3_CPMU_CTRL, val);
Matt Carlson9acb9612007-11-12 21:10:06 -08008274
8275 val = tr32(TG3_CPMU_LSPD_10MB_CLK);
8276 val &= ~CPMU_LSPD_10MB_MACCLK_MASK;
8277 val |= CPMU_LSPD_10MB_MACCLK_6_25;
8278 tw32(TG3_CPMU_LSPD_10MB_CLK, val);
8279
8280 val = tr32(TG3_CPMU_LNK_AWARE_PWRMD);
8281 val &= ~CPMU_LNK_AWARE_MACCLK_MASK;
8282 val |= CPMU_LNK_AWARE_MACCLK_6_25;
8283 tw32(TG3_CPMU_LNK_AWARE_PWRMD, val);
8284
8285 val = tr32(TG3_CPMU_HST_ACC);
8286 val &= ~CPMU_HST_ACC_MACCLK_MASK;
8287 val |= CPMU_HST_ACC_MACCLK_6_25;
8288 tw32(TG3_CPMU_HST_ACC, val);
Matt Carlsond30cdd22007-10-07 23:28:35 -07008289 }
8290
Matt Carlson33466d932009-04-20 06:57:41 +00008291 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780) {
8292 val = tr32(PCIE_PWR_MGMT_THRESH) & ~PCIE_PWR_MGMT_L1_THRESH_MSK;
8293 val |= PCIE_PWR_MGMT_EXT_ASPM_TMR_EN |
8294 PCIE_PWR_MGMT_L1_THRESH_4MS;
8295 tw32(PCIE_PWR_MGMT_THRESH, val);
Matt Carlson521e6b92009-08-25 10:06:01 +00008296
8297 val = tr32(TG3_PCIE_EIDLE_DELAY) & ~TG3_PCIE_EIDLE_DELAY_MASK;
8298 tw32(TG3_PCIE_EIDLE_DELAY, val | TG3_PCIE_EIDLE_DELAY_13_CLKS);
8299
8300 tw32(TG3_CORR_ERR_STAT, TG3_CORR_ERR_STAT_CLEAR);
Matt Carlson33466d932009-04-20 06:57:41 +00008301
Matt Carlsonf40386c2009-11-02 14:24:02 +00008302 val = tr32(TG3_PCIE_LNKCTL) & ~TG3_PCIE_LNKCTL_L1_PLL_PD_EN;
8303 tw32(TG3_PCIE_LNKCTL, val | TG3_PCIE_LNKCTL_L1_PLL_PD_DIS);
Matt Carlson255ca312009-08-25 10:07:27 +00008304 }
8305
Joe Perches63c3a662011-04-26 08:12:10 +00008306 if (tg3_flag(tp, L1PLLPD_EN)) {
Matt Carlson614b0592010-01-20 16:58:02 +00008307 u32 grc_mode = tr32(GRC_MODE);
8308
8309 /* Access the lower 1K of PL PCIE block registers. */
8310 val = grc_mode & ~GRC_MODE_PCIE_PORT_MASK;
8311 tw32(GRC_MODE, val | GRC_MODE_PCIE_PL_SEL);
8312
8313 val = tr32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL1);
8314 tw32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL1,
8315 val | TG3_PCIE_PL_LO_PHYCTL1_L1PLLPD_EN);
8316
8317 tw32(GRC_MODE, grc_mode);
8318 }
8319
Matt Carlson5093eed2010-11-24 08:31:45 +00008320 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765) {
8321 if (tp->pci_chip_rev_id == CHIPREV_ID_57765_A0) {
8322 u32 grc_mode = tr32(GRC_MODE);
Matt Carlsoncea46462010-04-12 06:58:24 +00008323
Matt Carlson5093eed2010-11-24 08:31:45 +00008324 /* Access the lower 1K of PL PCIE block registers. */
8325 val = grc_mode & ~GRC_MODE_PCIE_PORT_MASK;
8326 tw32(GRC_MODE, val | GRC_MODE_PCIE_PL_SEL);
Matt Carlsoncea46462010-04-12 06:58:24 +00008327
Matt Carlson5093eed2010-11-24 08:31:45 +00008328 val = tr32(TG3_PCIE_TLDLPL_PORT +
8329 TG3_PCIE_PL_LO_PHYCTL5);
8330 tw32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL5,
8331 val | TG3_PCIE_PL_LO_PHYCTL5_DIS_L2CLKREQ);
Matt Carlsoncea46462010-04-12 06:58:24 +00008332
Matt Carlson5093eed2010-11-24 08:31:45 +00008333 tw32(GRC_MODE, grc_mode);
8334 }
Matt Carlsona977dbe2010-04-12 06:58:26 +00008335
Matt Carlson1ff30a52011-05-19 12:12:46 +00008336 if (GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_57765_AX) {
8337 u32 grc_mode = tr32(GRC_MODE);
8338
8339 /* Access the lower 1K of DL PCIE block registers. */
8340 val = grc_mode & ~GRC_MODE_PCIE_PORT_MASK;
8341 tw32(GRC_MODE, val | GRC_MODE_PCIE_DL_SEL);
8342
8343 val = tr32(TG3_PCIE_TLDLPL_PORT +
8344 TG3_PCIE_DL_LO_FTSMAX);
8345 val &= ~TG3_PCIE_DL_LO_FTSMAX_MSK;
8346 tw32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_DL_LO_FTSMAX,
8347 val | TG3_PCIE_DL_LO_FTSMAX_VAL);
8348
8349 tw32(GRC_MODE, grc_mode);
8350 }
8351
Matt Carlsona977dbe2010-04-12 06:58:26 +00008352 val = tr32(TG3_CPMU_LSPD_10MB_CLK);
8353 val &= ~CPMU_LSPD_10MB_MACCLK_MASK;
8354 val |= CPMU_LSPD_10MB_MACCLK_6_25;
8355 tw32(TG3_CPMU_LSPD_10MB_CLK, val);
Matt Carlsoncea46462010-04-12 06:58:24 +00008356 }
8357
Linus Torvalds1da177e2005-04-16 15:20:36 -07008358 /* This works around an issue with Athlon chipsets on
8359 * B3 tigon3 silicon. This bit has no effect on any
8360 * other revision. But do not set this on PCI Express
Matt Carlson795d01c2007-10-07 23:28:17 -07008361 * chips and don't even touch the clocks if the CPMU is present.
Linus Torvalds1da177e2005-04-16 15:20:36 -07008362 */
Joe Perches63c3a662011-04-26 08:12:10 +00008363 if (!tg3_flag(tp, CPMU_PRESENT)) {
8364 if (!tg3_flag(tp, PCI_EXPRESS))
Matt Carlson795d01c2007-10-07 23:28:17 -07008365 tp->pci_clock_ctrl |= CLOCK_CTRL_DELAY_PCI_GRANT;
8366 tw32_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl);
8367 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008368
8369 if (tp->pci_chip_rev_id == CHIPREV_ID_5704_A0 &&
Joe Perches63c3a662011-04-26 08:12:10 +00008370 tg3_flag(tp, PCIX_MODE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008371 val = tr32(TG3PCI_PCISTATE);
8372 val |= PCISTATE_RETRY_SAME_DMA;
8373 tw32(TG3PCI_PCISTATE, val);
8374 }
8375
Joe Perches63c3a662011-04-26 08:12:10 +00008376 if (tg3_flag(tp, ENABLE_APE)) {
Matt Carlson0d3031d2007-10-10 18:02:43 -07008377 /* Allow reads and writes to the
8378 * APE register and memory space.
8379 */
8380 val = tr32(TG3PCI_PCISTATE);
8381 val |= PCISTATE_ALLOW_APE_CTLSPC_WR |
Matt Carlsonf92d9dc12010-06-05 17:24:30 +00008382 PCISTATE_ALLOW_APE_SHMEM_WR |
8383 PCISTATE_ALLOW_APE_PSPACE_WR;
Matt Carlson0d3031d2007-10-10 18:02:43 -07008384 tw32(TG3PCI_PCISTATE, val);
8385 }
8386
Linus Torvalds1da177e2005-04-16 15:20:36 -07008387 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5704_BX) {
8388 /* Enable some hw fixes. */
8389 val = tr32(TG3PCI_MSI_DATA);
8390 val |= (1 << 26) | (1 << 28) | (1 << 29);
8391 tw32(TG3PCI_MSI_DATA, val);
8392 }
8393
8394 /* Descriptor ring init may make accesses to the
8395 * NIC SRAM area to setup the TX descriptors, so we
8396 * can only do this after the hardware has been
8397 * successfully reset.
8398 */
Michael Chan32d8c572006-07-25 16:38:29 -07008399 err = tg3_init_rings(tp);
8400 if (err)
8401 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008402
Joe Perches63c3a662011-04-26 08:12:10 +00008403 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlsoncbf9ca62009-11-13 13:03:40 +00008404 val = tr32(TG3PCI_DMA_RW_CTRL) &
8405 ~DMA_RWCTRL_DIS_CACHE_ALIGNMENT;
Matt Carlson1a319022010-04-12 06:58:25 +00008406 if (tp->pci_chip_rev_id == CHIPREV_ID_57765_A0)
8407 val &= ~DMA_RWCTRL_CRDRDR_RDMA_MRRS_MSK;
Matt Carlson0aebff42011-04-25 12:42:45 +00008408 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_57765 &&
8409 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717)
8410 val |= DMA_RWCTRL_TAGGED_STAT_WA;
Matt Carlsoncbf9ca62009-11-13 13:03:40 +00008411 tw32(TG3PCI_DMA_RW_CTRL, val | tp->dma_rwctrl);
8412 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5784 &&
8413 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5761) {
Matt Carlsond30cdd22007-10-07 23:28:35 -07008414 /* This value is determined during the probe time DMA
8415 * engine test, tg3_test_dma.
8416 */
8417 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
8418 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008419
8420 tp->grc_mode &= ~(GRC_MODE_HOST_SENDBDS |
8421 GRC_MODE_4X_NIC_SEND_RINGS |
8422 GRC_MODE_NO_TX_PHDR_CSUM |
8423 GRC_MODE_NO_RX_PHDR_CSUM);
8424 tp->grc_mode |= GRC_MODE_HOST_SENDBDS;
Michael Chand2d746f2006-04-06 21:45:39 -07008425
8426 /* Pseudo-header checksum is done by hardware logic and not
8427 * the offload processers, so make the chip do the pseudo-
8428 * header checksums on receive. For transmit it is more
8429 * convenient to do the pseudo-header checksum in software
8430 * as Linux does that on transmit for us in all cases.
8431 */
8432 tp->grc_mode |= GRC_MODE_NO_TX_PHDR_CSUM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008433
8434 tw32(GRC_MODE,
8435 tp->grc_mode |
8436 (GRC_MODE_IRQ_ON_MAC_ATTN | GRC_MODE_HOST_STACKUP));
8437
8438 /* Setup the timer prescalar register. Clock is always 66Mhz. */
8439 val = tr32(GRC_MISC_CFG);
8440 val &= ~0xff;
8441 val |= (65 << GRC_MISC_CFG_PRESCALAR_SHIFT);
8442 tw32(GRC_MISC_CFG, val);
8443
8444 /* Initialize MBUF/DESC pool. */
Joe Perches63c3a662011-04-26 08:12:10 +00008445 if (tg3_flag(tp, 5750_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008446 /* Do nothing. */
8447 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5705) {
8448 tw32(BUFMGR_MB_POOL_ADDR, NIC_SRAM_MBUF_POOL_BASE);
8449 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704)
8450 tw32(BUFMGR_MB_POOL_SIZE, NIC_SRAM_MBUF_POOL_SIZE64);
8451 else
8452 tw32(BUFMGR_MB_POOL_SIZE, NIC_SRAM_MBUF_POOL_SIZE96);
8453 tw32(BUFMGR_DMA_DESC_POOL_ADDR, NIC_SRAM_DMA_DESC_POOL_BASE);
8454 tw32(BUFMGR_DMA_DESC_POOL_SIZE, NIC_SRAM_DMA_DESC_POOL_SIZE);
Joe Perches63c3a662011-04-26 08:12:10 +00008455 } else if (tg3_flag(tp, TSO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008456 int fw_len;
8457
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08008458 fw_len = tp->fw_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008459 fw_len = (fw_len + (0x80 - 1)) & ~(0x80 - 1);
8460 tw32(BUFMGR_MB_POOL_ADDR,
8461 NIC_SRAM_MBUF_POOL_BASE5705 + fw_len);
8462 tw32(BUFMGR_MB_POOL_SIZE,
8463 NIC_SRAM_MBUF_POOL_SIZE5705 - fw_len - 0xa00);
8464 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008465
Michael Chan0f893dc2005-07-25 12:30:38 -07008466 if (tp->dev->mtu <= ETH_DATA_LEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008467 tw32(BUFMGR_MB_RDMA_LOW_WATER,
8468 tp->bufmgr_config.mbuf_read_dma_low_water);
8469 tw32(BUFMGR_MB_MACRX_LOW_WATER,
8470 tp->bufmgr_config.mbuf_mac_rx_low_water);
8471 tw32(BUFMGR_MB_HIGH_WATER,
8472 tp->bufmgr_config.mbuf_high_water);
8473 } else {
8474 tw32(BUFMGR_MB_RDMA_LOW_WATER,
8475 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo);
8476 tw32(BUFMGR_MB_MACRX_LOW_WATER,
8477 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo);
8478 tw32(BUFMGR_MB_HIGH_WATER,
8479 tp->bufmgr_config.mbuf_high_water_jumbo);
8480 }
8481 tw32(BUFMGR_DMA_LOW_WATER,
8482 tp->bufmgr_config.dma_low_water);
8483 tw32(BUFMGR_DMA_HIGH_WATER,
8484 tp->bufmgr_config.dma_high_water);
8485
Matt Carlsond309a462010-09-30 10:34:31 +00008486 val = BUFMGR_MODE_ENABLE | BUFMGR_MODE_ATTN_ENABLE;
8487 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
8488 val |= BUFMGR_MODE_NO_TX_UNDERRUN;
Matt Carlson4d958472011-04-20 07:57:35 +00008489 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
8490 tp->pci_chip_rev_id == CHIPREV_ID_5719_A0 ||
8491 tp->pci_chip_rev_id == CHIPREV_ID_5720_A0)
8492 val |= BUFMGR_MODE_MBLOW_ATTN_ENAB;
Matt Carlsond309a462010-09-30 10:34:31 +00008493 tw32(BUFMGR_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008494 for (i = 0; i < 2000; i++) {
8495 if (tr32(BUFMGR_MODE) & BUFMGR_MODE_ENABLE)
8496 break;
8497 udelay(10);
8498 }
8499 if (i >= 2000) {
Joe Perches05dbe002010-02-17 19:44:19 +00008500 netdev_err(tp->dev, "%s cannot enable BUFMGR\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008501 return -ENODEV;
8502 }
8503
Matt Carlsoneb07a942011-04-20 07:57:36 +00008504 if (tp->pci_chip_rev_id == CHIPREV_ID_5906_A1)
8505 tw32(ISO_PKT_TX, (tr32(ISO_PKT_TX) & ~0x3) | 0x2);
Michael Chanb5d37722006-09-27 16:06:21 -07008506
Matt Carlsoneb07a942011-04-20 07:57:36 +00008507 tg3_setup_rxbd_thresholds(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008508
8509 /* Initialize TG3_BDINFO's at:
8510 * RCVDBDI_STD_BD: standard eth size rx ring
8511 * RCVDBDI_JUMBO_BD: jumbo frame rx ring
8512 * RCVDBDI_MINI_BD: small frame rx ring (??? does not work)
8513 *
8514 * like so:
8515 * TG3_BDINFO_HOST_ADDR: high/low parts of DMA address of ring
8516 * TG3_BDINFO_MAXLEN_FLAGS: (rx max buffer size << 16) |
8517 * ring attribute flags
8518 * TG3_BDINFO_NIC_ADDR: location of descriptors in nic SRAM
8519 *
8520 * Standard receive ring @ NIC_SRAM_RX_BUFFER_DESC, 512 entries.
8521 * Jumbo receive ring @ NIC_SRAM_RX_JUMBO_BUFFER_DESC, 256 entries.
8522 *
8523 * The size of each ring is fixed in the firmware, but the location is
8524 * configurable.
8525 */
8526 tw32(RCVDBDI_STD_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH,
Matt Carlson21f581a2009-08-28 14:00:25 +00008527 ((u64) tpr->rx_std_mapping >> 32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07008528 tw32(RCVDBDI_STD_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW,
Matt Carlson21f581a2009-08-28 14:00:25 +00008529 ((u64) tpr->rx_std_mapping & 0xffffffff));
Joe Perches63c3a662011-04-26 08:12:10 +00008530 if (!tg3_flag(tp, 5717_PLUS))
Matt Carlson87668d32009-11-13 13:03:34 +00008531 tw32(RCVDBDI_STD_BD + TG3_BDINFO_NIC_ADDR,
8532 NIC_SRAM_RX_BUFFER_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008533
Matt Carlsonfdb72b32009-08-28 13:57:12 +00008534 /* Disable the mini ring */
Joe Perches63c3a662011-04-26 08:12:10 +00008535 if (!tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008536 tw32(RCVDBDI_MINI_BD + TG3_BDINFO_MAXLEN_FLAGS,
8537 BDINFO_FLAGS_DISABLED);
8538
Matt Carlsonfdb72b32009-08-28 13:57:12 +00008539 /* Program the jumbo buffer descriptor ring control
8540 * blocks on those devices that have them.
8541 */
Matt Carlsona0512942011-07-27 14:20:54 +00008542 if (tp->pci_chip_rev_id == CHIPREV_ID_5719_A0 ||
Joe Perches63c3a662011-04-26 08:12:10 +00008543 (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008544
Joe Perches63c3a662011-04-26 08:12:10 +00008545 if (tg3_flag(tp, JUMBO_RING_ENABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008546 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH,
Matt Carlson21f581a2009-08-28 14:00:25 +00008547 ((u64) tpr->rx_jmb_mapping >> 32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07008548 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW,
Matt Carlson21f581a2009-08-28 14:00:25 +00008549 ((u64) tpr->rx_jmb_mapping & 0xffffffff));
Matt Carlsonde9f5232011-04-05 14:22:43 +00008550 val = TG3_RX_JMB_RING_SIZE(tp) <<
8551 BDINFO_FLAGS_MAXLEN_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008552 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_MAXLEN_FLAGS,
Matt Carlsonde9f5232011-04-05 14:22:43 +00008553 val | BDINFO_FLAGS_USE_EXT_RECV);
Joe Perches63c3a662011-04-26 08:12:10 +00008554 if (!tg3_flag(tp, USE_JUMBO_BDFLAG) ||
Matt Carlsona50d0792010-06-05 17:24:37 +00008555 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Matt Carlson87668d32009-11-13 13:03:34 +00008556 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_NIC_ADDR,
8557 NIC_SRAM_RX_JUMBO_BUFFER_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008558 } else {
8559 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_MAXLEN_FLAGS,
8560 BDINFO_FLAGS_DISABLED);
8561 }
8562
Joe Perches63c3a662011-04-26 08:12:10 +00008563 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlson7cb32cf2010-09-30 10:34:36 +00008564 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Matt Carlsonde9f5232011-04-05 14:22:43 +00008565 val = TG3_RX_STD_MAX_SIZE_5700;
Matt Carlson7cb32cf2010-09-30 10:34:36 +00008566 else
Matt Carlsonde9f5232011-04-05 14:22:43 +00008567 val = TG3_RX_STD_MAX_SIZE_5717;
Matt Carlson7cb32cf2010-09-30 10:34:36 +00008568 val <<= BDINFO_FLAGS_MAXLEN_SHIFT;
8569 val |= (TG3_RX_STD_DMA_SZ << 2);
8570 } else
Matt Carlson04380d42010-04-12 06:58:29 +00008571 val = TG3_RX_STD_DMA_SZ << BDINFO_FLAGS_MAXLEN_SHIFT;
Matt Carlsonfdb72b32009-08-28 13:57:12 +00008572 } else
Matt Carlsonde9f5232011-04-05 14:22:43 +00008573 val = TG3_RX_STD_MAX_SIZE_5700 << BDINFO_FLAGS_MAXLEN_SHIFT;
Matt Carlsonfdb72b32009-08-28 13:57:12 +00008574
8575 tw32(RCVDBDI_STD_BD + TG3_BDINFO_MAXLEN_FLAGS, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008576
Matt Carlson411da642009-11-13 13:03:46 +00008577 tpr->rx_std_prod_idx = tp->rx_pending;
Matt Carlson66711e662009-11-13 13:03:49 +00008578 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG, tpr->rx_std_prod_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008579
Joe Perches63c3a662011-04-26 08:12:10 +00008580 tpr->rx_jmb_prod_idx =
8581 tg3_flag(tp, JUMBO_RING_ENABLE) ? tp->rx_jumbo_pending : 0;
Matt Carlson66711e662009-11-13 13:03:49 +00008582 tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG, tpr->rx_jmb_prod_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008583
Matt Carlson2d31eca2009-09-01 12:53:31 +00008584 tg3_rings_reset(tp);
8585
Linus Torvalds1da177e2005-04-16 15:20:36 -07008586 /* Initialize MAC address and backoff seed. */
Michael Chan986e0ae2007-05-05 12:10:20 -07008587 __tg3_set_mac_addr(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008588
8589 /* MTU + ethernet header + FCS + optional VLAN tag */
Matt Carlsonf7b493e2009-02-25 14:21:52 +00008590 tw32(MAC_RX_MTU_SIZE,
8591 tp->dev->mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008592
8593 /* The slot time is changed by tg3_setup_phy if we
8594 * run at gigabit with half duplex.
8595 */
Matt Carlsonf2096f92011-04-05 14:22:48 +00008596 val = (2 << TX_LENGTHS_IPG_CRS_SHIFT) |
8597 (6 << TX_LENGTHS_IPG_SHIFT) |
8598 (32 << TX_LENGTHS_SLOT_TIME_SHIFT);
8599
8600 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
8601 val |= tr32(MAC_TX_LENGTHS) &
8602 (TX_LENGTHS_JMB_FRM_LEN_MSK |
8603 TX_LENGTHS_CNT_DWN_VAL_MSK);
8604
8605 tw32(MAC_TX_LENGTHS, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008606
8607 /* Receive rules. */
8608 tw32(MAC_RCV_RULE_CFG, RCV_RULE_CFG_DEFAULT_CLASS);
8609 tw32(RCVLPC_CONFIG, 0x0181);
8610
8611 /* Calculate RDMAC_MODE setting early, we need it to determine
8612 * the RCVLPC_STATE_ENABLE mask.
8613 */
8614 rdmac_mode = (RDMAC_MODE_ENABLE | RDMAC_MODE_TGTABORT_ENAB |
8615 RDMAC_MODE_MSTABORT_ENAB | RDMAC_MODE_PARITYERR_ENAB |
8616 RDMAC_MODE_ADDROFLOW_ENAB | RDMAC_MODE_FIFOOFLOW_ENAB |
8617 RDMAC_MODE_FIFOURUN_ENAB | RDMAC_MODE_FIFOOREAD_ENAB |
8618 RDMAC_MODE_LNGREAD_ENAB);
Michael Chan85e94ce2005-04-21 17:05:28 -07008619
Matt Carlsondeabaac2010-11-24 08:31:50 +00008620 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717)
Matt Carlson0339e4e2010-02-12 14:47:09 +00008621 rdmac_mode |= RDMAC_MODE_MULT_DMA_RD_DIS;
8622
Matt Carlson57e69832008-05-25 23:48:31 -07008623 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson321d32a2008-11-21 17:22:19 -08008624 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
8625 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
Matt Carlsond30cdd22007-10-07 23:28:35 -07008626 rdmac_mode |= RDMAC_MODE_BD_SBD_CRPT_ENAB |
8627 RDMAC_MODE_MBUF_RBD_CRPT_ENAB |
8628 RDMAC_MODE_MBUF_SBD_CRPT_ENAB;
8629
Matt Carlsonc5908932011-03-09 16:58:25 +00008630 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
8631 tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) {
Joe Perches63c3a662011-04-26 08:12:10 +00008632 if (tg3_flag(tp, TSO_CAPABLE) &&
Matt Carlsonc13e3712007-05-05 11:50:04 -07008633 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008634 rdmac_mode |= RDMAC_MODE_FIFO_SIZE_128;
8635 } else if (!(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH) &&
Joe Perches63c3a662011-04-26 08:12:10 +00008636 !tg3_flag(tp, IS_5788)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008637 rdmac_mode |= RDMAC_MODE_FIFO_LONG_BURST;
8638 }
8639 }
8640
Joe Perches63c3a662011-04-26 08:12:10 +00008641 if (tg3_flag(tp, PCI_EXPRESS))
Michael Chan85e94ce2005-04-21 17:05:28 -07008642 rdmac_mode |= RDMAC_MODE_FIFO_LONG_BURST;
8643
Joe Perches63c3a662011-04-26 08:12:10 +00008644 if (tg3_flag(tp, HW_TSO_1) ||
8645 tg3_flag(tp, HW_TSO_2) ||
8646 tg3_flag(tp, HW_TSO_3))
Matt Carlson027455a2008-12-21 20:19:30 -08008647 rdmac_mode |= RDMAC_MODE_IPV4_LSO_EN;
8648
Matt Carlson108a6c12011-05-19 12:12:47 +00008649 if (tg3_flag(tp, 57765_PLUS) ||
Matt Carlsone849cdc2009-11-13 13:03:38 +00008650 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Matt Carlson027455a2008-12-21 20:19:30 -08008651 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
8652 rdmac_mode |= RDMAC_MODE_IPV6_LSO_EN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008653
Matt Carlsonf2096f92011-04-05 14:22:48 +00008654 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
8655 rdmac_mode |= tr32(RDMAC_MODE) & RDMAC_MODE_H2BNC_VLAN_DET;
8656
Matt Carlson41a8a7e2010-09-15 08:59:53 +00008657 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
8658 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
8659 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
8660 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
Joe Perches63c3a662011-04-26 08:12:10 +00008661 tg3_flag(tp, 57765_PLUS)) {
Matt Carlson41a8a7e2010-09-15 08:59:53 +00008662 val = tr32(TG3_RDMA_RSRVCTRL_REG);
Matt Carlsond78b59f2011-04-05 14:22:46 +00008663 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
8664 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
Matt Carlsonb4495ed2011-01-25 15:58:47 +00008665 val &= ~(TG3_RDMA_RSRVCTRL_TXMRGN_MASK |
8666 TG3_RDMA_RSRVCTRL_FIFO_LWM_MASK |
8667 TG3_RDMA_RSRVCTRL_FIFO_HWM_MASK);
8668 val |= TG3_RDMA_RSRVCTRL_TXMRGN_320B |
8669 TG3_RDMA_RSRVCTRL_FIFO_LWM_1_5K |
8670 TG3_RDMA_RSRVCTRL_FIFO_HWM_1_5K;
Matt Carlsonb75cc0e2010-11-24 08:31:46 +00008671 }
Matt Carlson41a8a7e2010-09-15 08:59:53 +00008672 tw32(TG3_RDMA_RSRVCTRL_REG,
8673 val | TG3_RDMA_RSRVCTRL_FIFO_OFLW_FIX);
8674 }
8675
Matt Carlsond78b59f2011-04-05 14:22:46 +00008676 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
8677 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
Matt Carlsond309a462010-09-30 10:34:31 +00008678 val = tr32(TG3_LSO_RD_DMA_CRPTEN_CTRL);
8679 tw32(TG3_LSO_RD_DMA_CRPTEN_CTRL, val |
8680 TG3_LSO_RD_DMA_CRPTEN_CTRL_BLEN_BD_4K |
8681 TG3_LSO_RD_DMA_CRPTEN_CTRL_BLEN_LSO_4K);
8682 }
8683
Linus Torvalds1da177e2005-04-16 15:20:36 -07008684 /* Receive/send statistics. */
Joe Perches63c3a662011-04-26 08:12:10 +00008685 if (tg3_flag(tp, 5750_PLUS)) {
Michael Chan16613942006-06-29 20:15:13 -07008686 val = tr32(RCVLPC_STATS_ENABLE);
8687 val &= ~RCVLPC_STATSENAB_DACK_FIX;
8688 tw32(RCVLPC_STATS_ENABLE, val);
8689 } else if ((rdmac_mode & RDMAC_MODE_FIFO_SIZE_128) &&
Joe Perches63c3a662011-04-26 08:12:10 +00008690 tg3_flag(tp, TSO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008691 val = tr32(RCVLPC_STATS_ENABLE);
8692 val &= ~RCVLPC_STATSENAB_LNGBRST_RFIX;
8693 tw32(RCVLPC_STATS_ENABLE, val);
8694 } else {
8695 tw32(RCVLPC_STATS_ENABLE, 0xffffff);
8696 }
8697 tw32(RCVLPC_STATSCTRL, RCVLPC_STATSCTRL_ENABLE);
8698 tw32(SNDDATAI_STATSENAB, 0xffffff);
8699 tw32(SNDDATAI_STATSCTRL,
8700 (SNDDATAI_SCTRL_ENABLE |
8701 SNDDATAI_SCTRL_FASTUPD));
8702
8703 /* Setup host coalescing engine. */
8704 tw32(HOSTCC_MODE, 0);
8705 for (i = 0; i < 2000; i++) {
8706 if (!(tr32(HOSTCC_MODE) & HOSTCC_MODE_ENABLE))
8707 break;
8708 udelay(10);
8709 }
8710
Michael Chand244c892005-07-05 14:42:33 -07008711 __tg3_set_coalesce(tp, &tp->coal);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008712
Joe Perches63c3a662011-04-26 08:12:10 +00008713 if (!tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008714 /* Status/statistics block address. See tg3_timer,
8715 * the tg3_periodic_fetch_stats call there, and
8716 * tg3_get_stats to see how this works for 5705/5750 chips.
8717 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008718 tw32(HOSTCC_STATS_BLK_HOST_ADDR + TG3_64BIT_REG_HIGH,
8719 ((u64) tp->stats_mapping >> 32));
8720 tw32(HOSTCC_STATS_BLK_HOST_ADDR + TG3_64BIT_REG_LOW,
8721 ((u64) tp->stats_mapping & 0xffffffff));
8722 tw32(HOSTCC_STATS_BLK_NIC_ADDR, NIC_SRAM_STATS_BLK);
Matt Carlson2d31eca2009-09-01 12:53:31 +00008723
Linus Torvalds1da177e2005-04-16 15:20:36 -07008724 tw32(HOSTCC_STATUS_BLK_NIC_ADDR, NIC_SRAM_STATUS_BLK);
Matt Carlson2d31eca2009-09-01 12:53:31 +00008725
8726 /* Clear statistics and status block memory areas */
8727 for (i = NIC_SRAM_STATS_BLK;
8728 i < NIC_SRAM_STATUS_BLK + TG3_HW_STATUS_SIZE;
8729 i += sizeof(u32)) {
8730 tg3_write_mem(tp, i, 0);
8731 udelay(40);
8732 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008733 }
8734
8735 tw32(HOSTCC_MODE, HOSTCC_MODE_ENABLE | tp->coalesce_mode);
8736
8737 tw32(RCVCC_MODE, RCVCC_MODE_ENABLE | RCVCC_MODE_ATTN_ENABLE);
8738 tw32(RCVLPC_MODE, RCVLPC_MODE_ENABLE);
Joe Perches63c3a662011-04-26 08:12:10 +00008739 if (!tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008740 tw32(RCVLSC_MODE, RCVLSC_MODE_ENABLE | RCVLSC_MODE_ATTN_ENABLE);
8741
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008742 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES) {
8743 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chanc94e3942005-09-27 12:12:42 -07008744 /* reset to prevent losing 1st rx packet intermittently */
8745 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
8746 udelay(10);
8747 }
8748
Matt Carlson3bda1252008-08-15 14:08:22 -07008749 tp->mac_mode |= MAC_MODE_TXSTAT_ENABLE | MAC_MODE_RXSTAT_ENABLE |
Matt Carlson9e975cc2011-07-20 10:20:50 +00008750 MAC_MODE_TDE_ENABLE | MAC_MODE_RDE_ENABLE |
8751 MAC_MODE_FHDE_ENABLE;
8752 if (tg3_flag(tp, ENABLE_APE))
8753 tp->mac_mode |= MAC_MODE_APE_TX_EN | MAC_MODE_APE_RX_EN;
Joe Perches63c3a662011-04-26 08:12:10 +00008754 if (!tg3_flag(tp, 5705_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008755 !(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07008756 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700)
8757 tp->mac_mode |= MAC_MODE_LINK_POLARITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008758 tw32_f(MAC_MODE, tp->mac_mode | MAC_MODE_RXSTAT_CLEAR | MAC_MODE_TXSTAT_CLEAR);
8759 udelay(40);
8760
Michael Chan314fba32005-04-21 17:07:04 -07008761 /* tp->grc_local_ctrl is partially set up during tg3_get_invariants().
Joe Perches63c3a662011-04-26 08:12:10 +00008762 * If TG3_FLAG_IS_NIC is zero, we should read the
Michael Chan314fba32005-04-21 17:07:04 -07008763 * register to preserve the GPIO settings for LOMs. The GPIOs,
8764 * whether used as inputs or outputs, are set by boot code after
8765 * reset.
8766 */
Joe Perches63c3a662011-04-26 08:12:10 +00008767 if (!tg3_flag(tp, IS_NIC)) {
Michael Chan314fba32005-04-21 17:07:04 -07008768 u32 gpio_mask;
8769
Michael Chan9d26e212006-12-07 00:21:14 -08008770 gpio_mask = GRC_LCLCTRL_GPIO_OE0 | GRC_LCLCTRL_GPIO_OE1 |
8771 GRC_LCLCTRL_GPIO_OE2 | GRC_LCLCTRL_GPIO_OUTPUT0 |
8772 GRC_LCLCTRL_GPIO_OUTPUT1 | GRC_LCLCTRL_GPIO_OUTPUT2;
Michael Chan3e7d83b2005-04-21 17:10:36 -07008773
8774 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752)
8775 gpio_mask |= GRC_LCLCTRL_GPIO_OE3 |
8776 GRC_LCLCTRL_GPIO_OUTPUT3;
8777
Michael Chanaf36e6b2006-03-23 01:28:06 -08008778 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755)
8779 gpio_mask |= GRC_LCLCTRL_GPIO_UART_SEL;
8780
Gary Zambranoaaf84462007-05-05 11:51:45 -07008781 tp->grc_local_ctrl &= ~gpio_mask;
Michael Chan314fba32005-04-21 17:07:04 -07008782 tp->grc_local_ctrl |= tr32(GRC_LOCAL_CTRL) & gpio_mask;
8783
8784 /* GPIO1 must be driven high for eeprom write protect */
Joe Perches63c3a662011-04-26 08:12:10 +00008785 if (tg3_flag(tp, EEPROM_WRITE_PROT))
Michael Chan9d26e212006-12-07 00:21:14 -08008786 tp->grc_local_ctrl |= (GRC_LCLCTRL_GPIO_OE1 |
8787 GRC_LCLCTRL_GPIO_OUTPUT1);
Michael Chan314fba32005-04-21 17:07:04 -07008788 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008789 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
8790 udelay(100);
8791
Joe Perches63c3a662011-04-26 08:12:10 +00008792 if (tg3_flag(tp, USING_MSIX) && tp->irq_cnt > 1) {
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008793 val = tr32(MSGINT_MODE);
8794 val |= MSGINT_MODE_MULTIVEC_EN | MSGINT_MODE_ENABLE;
Matt Carlson5b39de92011-08-31 11:44:50 +00008795 if (!tg3_flag(tp, 1SHOT_MSI))
8796 val |= MSGINT_MODE_ONE_SHOT_DISABLE;
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008797 tw32(MSGINT_MODE, val);
8798 }
8799
Joe Perches63c3a662011-04-26 08:12:10 +00008800 if (!tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008801 tw32_f(DMAC_MODE, DMAC_MODE_ENABLE);
8802 udelay(40);
8803 }
8804
8805 val = (WDMAC_MODE_ENABLE | WDMAC_MODE_TGTABORT_ENAB |
8806 WDMAC_MODE_MSTABORT_ENAB | WDMAC_MODE_PARITYERR_ENAB |
8807 WDMAC_MODE_ADDROFLOW_ENAB | WDMAC_MODE_FIFOOFLOW_ENAB |
8808 WDMAC_MODE_FIFOURUN_ENAB | WDMAC_MODE_FIFOOREAD_ENAB |
8809 WDMAC_MODE_LNGREAD_ENAB);
8810
Matt Carlsonc5908932011-03-09 16:58:25 +00008811 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
8812 tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) {
Joe Perches63c3a662011-04-26 08:12:10 +00008813 if (tg3_flag(tp, TSO_CAPABLE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07008814 (tp->pci_chip_rev_id == CHIPREV_ID_5705_A1 ||
8815 tp->pci_chip_rev_id == CHIPREV_ID_5705_A2)) {
8816 /* nothing */
8817 } else if (!(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH) &&
Joe Perches63c3a662011-04-26 08:12:10 +00008818 !tg3_flag(tp, IS_5788)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008819 val |= WDMAC_MODE_RX_ACCEL;
8820 }
8821 }
8822
Michael Chand9ab5ad12006-03-20 22:27:35 -08008823 /* Enable host coalescing bug fix */
Joe Perches63c3a662011-04-26 08:12:10 +00008824 if (tg3_flag(tp, 5755_PLUS))
Matt Carlsonf51f3562008-05-25 23:45:08 -07008825 val |= WDMAC_MODE_STATUS_TAG_FIX;
Michael Chand9ab5ad12006-03-20 22:27:35 -08008826
Matt Carlson788a0352009-11-02 14:26:03 +00008827 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
8828 val |= WDMAC_MODE_BURST_ALL_DATA;
8829
Linus Torvalds1da177e2005-04-16 15:20:36 -07008830 tw32_f(WDMAC_MODE, val);
8831 udelay(40);
8832
Joe Perches63c3a662011-04-26 08:12:10 +00008833 if (tg3_flag(tp, PCIX_MODE)) {
Matt Carlson9974a352007-10-07 23:27:28 -07008834 u16 pcix_cmd;
8835
8836 pci_read_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
8837 &pcix_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008838 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703) {
Matt Carlson9974a352007-10-07 23:27:28 -07008839 pcix_cmd &= ~PCI_X_CMD_MAX_READ;
8840 pcix_cmd |= PCI_X_CMD_READ_2K;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008841 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
Matt Carlson9974a352007-10-07 23:27:28 -07008842 pcix_cmd &= ~(PCI_X_CMD_MAX_SPLIT | PCI_X_CMD_MAX_READ);
8843 pcix_cmd |= PCI_X_CMD_READ_2K;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008844 }
Matt Carlson9974a352007-10-07 23:27:28 -07008845 pci_write_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
8846 pcix_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008847 }
8848
8849 tw32_f(RDMAC_MODE, rdmac_mode);
8850 udelay(40);
8851
8852 tw32(RCVDCC_MODE, RCVDCC_MODE_ENABLE | RCVDCC_MODE_ATTN_ENABLE);
Joe Perches63c3a662011-04-26 08:12:10 +00008853 if (!tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008854 tw32(MBFREE_MODE, MBFREE_MODE_ENABLE);
Matt Carlson9936bcf2007-10-10 18:03:07 -07008855
8856 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
8857 tw32(SNDDATAC_MODE,
8858 SNDDATAC_MODE_ENABLE | SNDDATAC_MODE_CDELAY);
8859 else
8860 tw32(SNDDATAC_MODE, SNDDATAC_MODE_ENABLE);
8861
Linus Torvalds1da177e2005-04-16 15:20:36 -07008862 tw32(SNDBDC_MODE, SNDBDC_MODE_ENABLE | SNDBDC_MODE_ATTN_ENABLE);
8863 tw32(RCVBDI_MODE, RCVBDI_MODE_ENABLE | RCVBDI_MODE_RCB_ATTN_ENAB);
Matt Carlson7cb32cf2010-09-30 10:34:36 +00008864 val = RCVDBDI_MODE_ENABLE | RCVDBDI_MODE_INV_RING_SZ;
Joe Perches63c3a662011-04-26 08:12:10 +00008865 if (tg3_flag(tp, LRG_PROD_RING_CAP))
Matt Carlson7cb32cf2010-09-30 10:34:36 +00008866 val |= RCVDBDI_MODE_LRG_RING_SZ;
8867 tw32(RCVDBDI_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008868 tw32(SNDDATAI_MODE, SNDDATAI_MODE_ENABLE);
Joe Perches63c3a662011-04-26 08:12:10 +00008869 if (tg3_flag(tp, HW_TSO_1) ||
8870 tg3_flag(tp, HW_TSO_2) ||
8871 tg3_flag(tp, HW_TSO_3))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008872 tw32(SNDDATAI_MODE, SNDDATAI_MODE_ENABLE | 0x8);
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008873 val = SNDBDI_MODE_ENABLE | SNDBDI_MODE_ATTN_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00008874 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008875 val |= SNDBDI_MODE_MULTI_TXQ_EN;
8876 tw32(SNDBDI_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008877 tw32(SNDBDS_MODE, SNDBDS_MODE_ENABLE | SNDBDS_MODE_ATTN_ENABLE);
8878
8879 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0) {
8880 err = tg3_load_5701_a0_firmware_fix(tp);
8881 if (err)
8882 return err;
8883 }
8884
Joe Perches63c3a662011-04-26 08:12:10 +00008885 if (tg3_flag(tp, TSO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008886 err = tg3_load_tso_firmware(tp);
8887 if (err)
8888 return err;
8889 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008890
8891 tp->tx_mode = TX_MODE_ENABLE;
Matt Carlsonf2096f92011-04-05 14:22:48 +00008892
Joe Perches63c3a662011-04-26 08:12:10 +00008893 if (tg3_flag(tp, 5755_PLUS) ||
Matt Carlsonb1d05212010-06-05 17:24:31 +00008894 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
8895 tp->tx_mode |= TX_MODE_MBUF_LOCKUP_FIX;
Matt Carlsonf2096f92011-04-05 14:22:48 +00008896
8897 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
8898 val = TX_MODE_JMB_FRM_LEN | TX_MODE_CNT_DN_MODE;
8899 tp->tx_mode &= ~val;
8900 tp->tx_mode |= tr32(MAC_TX_MODE) & val;
8901 }
8902
Linus Torvalds1da177e2005-04-16 15:20:36 -07008903 tw32_f(MAC_TX_MODE, tp->tx_mode);
8904 udelay(100);
8905
Joe Perches63c3a662011-04-26 08:12:10 +00008906 if (tg3_flag(tp, ENABLE_RSS)) {
Matt Carlson9d53fa12011-07-20 10:20:54 +00008907 int i = 0;
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008908 u32 reg = MAC_RSS_INDIR_TBL_0;
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008909
Matt Carlson9d53fa12011-07-20 10:20:54 +00008910 if (tp->irq_cnt == 2) {
8911 for (i = 0; i < TG3_RSS_INDIR_TBL_SIZE; i += 8) {
8912 tw32(reg, 0x0);
8913 reg += 4;
8914 }
8915 } else {
8916 u32 val;
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008917
Matt Carlson9d53fa12011-07-20 10:20:54 +00008918 while (i < TG3_RSS_INDIR_TBL_SIZE) {
8919 val = i % (tp->irq_cnt - 1);
8920 i++;
8921 for (; i % 8; i++) {
8922 val <<= 4;
8923 val |= (i % (tp->irq_cnt - 1));
8924 }
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008925 tw32(reg, val);
8926 reg += 4;
8927 }
8928 }
8929
8930 /* Setup the "secret" hash key. */
8931 tw32(MAC_RSS_HASH_KEY_0, 0x5f865437);
8932 tw32(MAC_RSS_HASH_KEY_1, 0xe4ac62cc);
8933 tw32(MAC_RSS_HASH_KEY_2, 0x50103a45);
8934 tw32(MAC_RSS_HASH_KEY_3, 0x36621985);
8935 tw32(MAC_RSS_HASH_KEY_4, 0xbf14c0e8);
8936 tw32(MAC_RSS_HASH_KEY_5, 0x1bc27a1e);
8937 tw32(MAC_RSS_HASH_KEY_6, 0x84f4b556);
8938 tw32(MAC_RSS_HASH_KEY_7, 0x094ea6fe);
8939 tw32(MAC_RSS_HASH_KEY_8, 0x7dda01e7);
8940 tw32(MAC_RSS_HASH_KEY_9, 0xc04d7481);
8941 }
8942
Linus Torvalds1da177e2005-04-16 15:20:36 -07008943 tp->rx_mode = RX_MODE_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00008944 if (tg3_flag(tp, 5755_PLUS))
Michael Chanaf36e6b2006-03-23 01:28:06 -08008945 tp->rx_mode |= RX_MODE_IPV6_CSUM_ENABLE;
8946
Joe Perches63c3a662011-04-26 08:12:10 +00008947 if (tg3_flag(tp, ENABLE_RSS))
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008948 tp->rx_mode |= RX_MODE_RSS_ENABLE |
8949 RX_MODE_RSS_ITBL_HASH_BITS_7 |
8950 RX_MODE_RSS_IPV6_HASH_EN |
8951 RX_MODE_RSS_TCP_IPV6_HASH_EN |
8952 RX_MODE_RSS_IPV4_HASH_EN |
8953 RX_MODE_RSS_TCP_IPV4_HASH_EN;
8954
Linus Torvalds1da177e2005-04-16 15:20:36 -07008955 tw32_f(MAC_RX_MODE, tp->rx_mode);
8956 udelay(10);
8957
Linus Torvalds1da177e2005-04-16 15:20:36 -07008958 tw32(MAC_LED_CTRL, tp->led_ctrl);
8959
8960 tw32(MAC_MI_STAT, MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008961 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008962 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
8963 udelay(10);
8964 }
8965 tw32_f(MAC_RX_MODE, tp->rx_mode);
8966 udelay(10);
8967
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008968 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008969 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008970 !(tp->phy_flags & TG3_PHYFLG_SERDES_PREEMPHASIS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008971 /* Set drive transmission level to 1.2V */
8972 /* only if the signal pre-emphasis bit is not set */
8973 val = tr32(MAC_SERDES_CFG);
8974 val &= 0xfffff000;
8975 val |= 0x880;
8976 tw32(MAC_SERDES_CFG, val);
8977 }
8978 if (tp->pci_chip_rev_id == CHIPREV_ID_5703_A1)
8979 tw32(MAC_SERDES_CFG, 0x616000);
8980 }
8981
8982 /* Prevent chip from dropping frames when flow control
8983 * is enabled.
8984 */
Matt Carlson666bc832010-01-20 16:58:03 +00008985 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
8986 val = 1;
8987 else
8988 val = 2;
8989 tw32_f(MAC_LOW_WMARK_MAX_RX_FRAME, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008990
8991 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008992 (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008993 /* Use hardware link auto-negotiation */
Joe Perches63c3a662011-04-26 08:12:10 +00008994 tg3_flag_set(tp, HW_AUTONEG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008995 }
8996
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008997 if ((tp->phy_flags & TG3_PHYFLG_MII_SERDES) &&
Matt Carlson6ff6f812011-05-19 12:12:54 +00008998 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
Michael Chand4d2c552006-03-20 17:47:20 -08008999 u32 tmp;
9000
9001 tmp = tr32(SERDES_RX_CTRL);
9002 tw32(SERDES_RX_CTRL, tmp | SERDES_RX_SIG_DETECT);
9003 tp->grc_local_ctrl &= ~GRC_LCLCTRL_USE_EXT_SIG_DETECT;
9004 tp->grc_local_ctrl |= GRC_LCLCTRL_USE_SIG_DETECT;
9005 tw32(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
9006 }
9007
Joe Perches63c3a662011-04-26 08:12:10 +00009008 if (!tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson80096062010-08-02 11:26:06 +00009009 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) {
9010 tp->phy_flags &= ~TG3_PHYFLG_IS_LOW_POWER;
Matt Carlsondd477002008-05-25 23:45:58 -07009011 tp->link_config.speed = tp->link_config.orig_speed;
9012 tp->link_config.duplex = tp->link_config.orig_duplex;
9013 tp->link_config.autoneg = tp->link_config.orig_autoneg;
9014 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009015
Matt Carlsondd477002008-05-25 23:45:58 -07009016 err = tg3_setup_phy(tp, 0);
9017 if (err)
9018 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009019
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009020 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
9021 !(tp->phy_flags & TG3_PHYFLG_IS_FET)) {
Matt Carlsondd477002008-05-25 23:45:58 -07009022 u32 tmp;
9023
9024 /* Clear CRC stats. */
9025 if (!tg3_readphy(tp, MII_TG3_TEST1, &tmp)) {
9026 tg3_writephy(tp, MII_TG3_TEST1,
9027 tmp | MII_TG3_TEST1_CRC_EN);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00009028 tg3_readphy(tp, MII_TG3_RXR_COUNTERS, &tmp);
Matt Carlsondd477002008-05-25 23:45:58 -07009029 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009030 }
9031 }
9032
9033 __tg3_set_rx_mode(tp->dev);
9034
9035 /* Initialize receive rules. */
9036 tw32(MAC_RCV_RULE_0, 0xc2000000 & RCV_RULE_DISABLE_MASK);
9037 tw32(MAC_RCV_VALUE_0, 0xffffffff & RCV_RULE_DISABLE_MASK);
9038 tw32(MAC_RCV_RULE_1, 0x86000004 & RCV_RULE_DISABLE_MASK);
9039 tw32(MAC_RCV_VALUE_1, 0xffffffff & RCV_RULE_DISABLE_MASK);
9040
Joe Perches63c3a662011-04-26 08:12:10 +00009041 if (tg3_flag(tp, 5705_PLUS) && !tg3_flag(tp, 5780_CLASS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009042 limit = 8;
9043 else
9044 limit = 16;
Joe Perches63c3a662011-04-26 08:12:10 +00009045 if (tg3_flag(tp, ENABLE_ASF))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009046 limit -= 4;
9047 switch (limit) {
9048 case 16:
9049 tw32(MAC_RCV_RULE_15, 0); tw32(MAC_RCV_VALUE_15, 0);
9050 case 15:
9051 tw32(MAC_RCV_RULE_14, 0); tw32(MAC_RCV_VALUE_14, 0);
9052 case 14:
9053 tw32(MAC_RCV_RULE_13, 0); tw32(MAC_RCV_VALUE_13, 0);
9054 case 13:
9055 tw32(MAC_RCV_RULE_12, 0); tw32(MAC_RCV_VALUE_12, 0);
9056 case 12:
9057 tw32(MAC_RCV_RULE_11, 0); tw32(MAC_RCV_VALUE_11, 0);
9058 case 11:
9059 tw32(MAC_RCV_RULE_10, 0); tw32(MAC_RCV_VALUE_10, 0);
9060 case 10:
9061 tw32(MAC_RCV_RULE_9, 0); tw32(MAC_RCV_VALUE_9, 0);
9062 case 9:
9063 tw32(MAC_RCV_RULE_8, 0); tw32(MAC_RCV_VALUE_8, 0);
9064 case 8:
9065 tw32(MAC_RCV_RULE_7, 0); tw32(MAC_RCV_VALUE_7, 0);
9066 case 7:
9067 tw32(MAC_RCV_RULE_6, 0); tw32(MAC_RCV_VALUE_6, 0);
9068 case 6:
9069 tw32(MAC_RCV_RULE_5, 0); tw32(MAC_RCV_VALUE_5, 0);
9070 case 5:
9071 tw32(MAC_RCV_RULE_4, 0); tw32(MAC_RCV_VALUE_4, 0);
9072 case 4:
9073 /* tw32(MAC_RCV_RULE_3, 0); tw32(MAC_RCV_VALUE_3, 0); */
9074 case 3:
9075 /* tw32(MAC_RCV_RULE_2, 0); tw32(MAC_RCV_VALUE_2, 0); */
9076 case 2:
9077 case 1:
9078
9079 default:
9080 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07009081 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009082
Joe Perches63c3a662011-04-26 08:12:10 +00009083 if (tg3_flag(tp, ENABLE_APE))
Matt Carlson9ce768e2007-10-11 19:49:11 -07009084 /* Write our heartbeat update interval to APE. */
9085 tg3_ape_write32(tp, TG3_APE_HOST_HEARTBEAT_INT_MS,
9086 APE_HOST_HEARTBEAT_INT_DISABLE);
Matt Carlson0d3031d2007-10-10 18:02:43 -07009087
Linus Torvalds1da177e2005-04-16 15:20:36 -07009088 tg3_write_sig_post_reset(tp, RESET_KIND_INIT);
9089
Linus Torvalds1da177e2005-04-16 15:20:36 -07009090 return 0;
9091}
9092
9093/* Called at device open time to get the chip ready for
9094 * packet processing. Invoked with tp->lock held.
9095 */
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07009096static int tg3_init_hw(struct tg3 *tp, int reset_phy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009097{
Linus Torvalds1da177e2005-04-16 15:20:36 -07009098 tg3_switch_clocks(tp);
9099
9100 tw32(TG3PCI_MEM_WIN_BASE_ADDR, 0);
9101
Matt Carlson2f751b62008-08-04 23:17:34 -07009102 return tg3_reset_hw(tp, reset_phy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009103}
9104
9105#define TG3_STAT_ADD32(PSTAT, REG) \
9106do { u32 __val = tr32(REG); \
9107 (PSTAT)->low += __val; \
9108 if ((PSTAT)->low < __val) \
9109 (PSTAT)->high += 1; \
9110} while (0)
9111
9112static void tg3_periodic_fetch_stats(struct tg3 *tp)
9113{
9114 struct tg3_hw_stats *sp = tp->hw_stats;
9115
9116 if (!netif_carrier_ok(tp->dev))
9117 return;
9118
9119 TG3_STAT_ADD32(&sp->tx_octets, MAC_TX_STATS_OCTETS);
9120 TG3_STAT_ADD32(&sp->tx_collisions, MAC_TX_STATS_COLLISIONS);
9121 TG3_STAT_ADD32(&sp->tx_xon_sent, MAC_TX_STATS_XON_SENT);
9122 TG3_STAT_ADD32(&sp->tx_xoff_sent, MAC_TX_STATS_XOFF_SENT);
9123 TG3_STAT_ADD32(&sp->tx_mac_errors, MAC_TX_STATS_MAC_ERRORS);
9124 TG3_STAT_ADD32(&sp->tx_single_collisions, MAC_TX_STATS_SINGLE_COLLISIONS);
9125 TG3_STAT_ADD32(&sp->tx_mult_collisions, MAC_TX_STATS_MULT_COLLISIONS);
9126 TG3_STAT_ADD32(&sp->tx_deferred, MAC_TX_STATS_DEFERRED);
9127 TG3_STAT_ADD32(&sp->tx_excessive_collisions, MAC_TX_STATS_EXCESSIVE_COL);
9128 TG3_STAT_ADD32(&sp->tx_late_collisions, MAC_TX_STATS_LATE_COL);
9129 TG3_STAT_ADD32(&sp->tx_ucast_packets, MAC_TX_STATS_UCAST);
9130 TG3_STAT_ADD32(&sp->tx_mcast_packets, MAC_TX_STATS_MCAST);
9131 TG3_STAT_ADD32(&sp->tx_bcast_packets, MAC_TX_STATS_BCAST);
9132
9133 TG3_STAT_ADD32(&sp->rx_octets, MAC_RX_STATS_OCTETS);
9134 TG3_STAT_ADD32(&sp->rx_fragments, MAC_RX_STATS_FRAGMENTS);
9135 TG3_STAT_ADD32(&sp->rx_ucast_packets, MAC_RX_STATS_UCAST);
9136 TG3_STAT_ADD32(&sp->rx_mcast_packets, MAC_RX_STATS_MCAST);
9137 TG3_STAT_ADD32(&sp->rx_bcast_packets, MAC_RX_STATS_BCAST);
9138 TG3_STAT_ADD32(&sp->rx_fcs_errors, MAC_RX_STATS_FCS_ERRORS);
9139 TG3_STAT_ADD32(&sp->rx_align_errors, MAC_RX_STATS_ALIGN_ERRORS);
9140 TG3_STAT_ADD32(&sp->rx_xon_pause_rcvd, MAC_RX_STATS_XON_PAUSE_RECVD);
9141 TG3_STAT_ADD32(&sp->rx_xoff_pause_rcvd, MAC_RX_STATS_XOFF_PAUSE_RECVD);
9142 TG3_STAT_ADD32(&sp->rx_mac_ctrl_rcvd, MAC_RX_STATS_MAC_CTRL_RECVD);
9143 TG3_STAT_ADD32(&sp->rx_xoff_entered, MAC_RX_STATS_XOFF_ENTERED);
9144 TG3_STAT_ADD32(&sp->rx_frame_too_long_errors, MAC_RX_STATS_FRAME_TOO_LONG);
9145 TG3_STAT_ADD32(&sp->rx_jabbers, MAC_RX_STATS_JABBERS);
9146 TG3_STAT_ADD32(&sp->rx_undersize_packets, MAC_RX_STATS_UNDERSIZE);
Michael Chan463d3052006-05-22 16:36:27 -07009147
9148 TG3_STAT_ADD32(&sp->rxbds_empty, RCVLPC_NO_RCV_BD_CNT);
Matt Carlson310050f2011-05-19 12:12:55 +00009149 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717 &&
9150 tp->pci_chip_rev_id != CHIPREV_ID_5719_A0 &&
9151 tp->pci_chip_rev_id != CHIPREV_ID_5720_A0) {
Matt Carlson4d958472011-04-20 07:57:35 +00009152 TG3_STAT_ADD32(&sp->rx_discards, RCVLPC_IN_DISCARDS_CNT);
9153 } else {
9154 u32 val = tr32(HOSTCC_FLOW_ATTN);
9155 val = (val & HOSTCC_FLOW_ATTN_MBUF_LWM) ? 1 : 0;
9156 if (val) {
9157 tw32(HOSTCC_FLOW_ATTN, HOSTCC_FLOW_ATTN_MBUF_LWM);
9158 sp->rx_discards.low += val;
9159 if (sp->rx_discards.low < val)
9160 sp->rx_discards.high += 1;
9161 }
9162 sp->mbuf_lwm_thresh_hit = sp->rx_discards;
9163 }
Michael Chan463d3052006-05-22 16:36:27 -07009164 TG3_STAT_ADD32(&sp->rx_errors, RCVLPC_IN_ERRORS_CNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009165}
9166
Matt Carlson0e6cf6a2011-06-13 13:38:55 +00009167static void tg3_chk_missed_msi(struct tg3 *tp)
9168{
9169 u32 i;
9170
9171 for (i = 0; i < tp->irq_cnt; i++) {
9172 struct tg3_napi *tnapi = &tp->napi[i];
9173
9174 if (tg3_has_work(tnapi)) {
9175 if (tnapi->last_rx_cons == tnapi->rx_rcb_ptr &&
9176 tnapi->last_tx_cons == tnapi->tx_cons) {
9177 if (tnapi->chk_msi_cnt < 1) {
9178 tnapi->chk_msi_cnt++;
9179 return;
9180 }
Matt Carlson7f230732011-08-31 11:44:48 +00009181 tg3_msi(0, tnapi);
Matt Carlson0e6cf6a2011-06-13 13:38:55 +00009182 }
9183 }
9184 tnapi->chk_msi_cnt = 0;
9185 tnapi->last_rx_cons = tnapi->rx_rcb_ptr;
9186 tnapi->last_tx_cons = tnapi->tx_cons;
9187 }
9188}
9189
Linus Torvalds1da177e2005-04-16 15:20:36 -07009190static void tg3_timer(unsigned long __opaque)
9191{
9192 struct tg3 *tp = (struct tg3 *) __opaque;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009193
Matt Carlson5b190622011-11-04 09:15:04 +00009194 if (tp->irq_sync || tg3_flag(tp, RESET_TASK_PENDING))
Michael Chanf475f162006-03-27 23:20:14 -08009195 goto restart_timer;
9196
David S. Millerf47c11e2005-06-24 20:18:35 -07009197 spin_lock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009198
Matt Carlson0e6cf6a2011-06-13 13:38:55 +00009199 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
9200 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
9201 tg3_chk_missed_msi(tp);
9202
Joe Perches63c3a662011-04-26 08:12:10 +00009203 if (!tg3_flag(tp, TAGGED_STATUS)) {
David S. Millerfac9b832005-05-18 22:46:34 -07009204 /* All of this garbage is because when using non-tagged
9205 * IRQ status the mailbox/status_block protocol the chip
9206 * uses with the cpu is race prone.
9207 */
Matt Carlson898a56f2009-08-28 14:02:40 +00009208 if (tp->napi[0].hw_status->status & SD_STATUS_UPDATED) {
David S. Millerfac9b832005-05-18 22:46:34 -07009209 tw32(GRC_LOCAL_CTRL,
9210 tp->grc_local_ctrl | GRC_LCLCTRL_SETINT);
9211 } else {
9212 tw32(HOSTCC_MODE, tp->coalesce_mode |
Matt Carlsonfd2ce372009-09-01 12:51:13 +00009213 HOSTCC_MODE_ENABLE | HOSTCC_MODE_NOW);
David S. Millerfac9b832005-05-18 22:46:34 -07009214 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009215
David S. Millerfac9b832005-05-18 22:46:34 -07009216 if (!(tr32(WDMAC_MODE) & WDMAC_MODE_ENABLE)) {
David S. Millerf47c11e2005-06-24 20:18:35 -07009217 spin_unlock(&tp->lock);
Matt Carlsondb219972011-11-04 09:15:03 +00009218 tg3_reset_task_schedule(tp);
Matt Carlson5b190622011-11-04 09:15:04 +00009219 goto restart_timer;
David S. Millerfac9b832005-05-18 22:46:34 -07009220 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009221 }
9222
Linus Torvalds1da177e2005-04-16 15:20:36 -07009223 /* This part only runs once per second. */
9224 if (!--tp->timer_counter) {
Joe Perches63c3a662011-04-26 08:12:10 +00009225 if (tg3_flag(tp, 5705_PLUS))
David S. Millerfac9b832005-05-18 22:46:34 -07009226 tg3_periodic_fetch_stats(tp);
9227
Matt Carlsonb0c59432011-05-19 12:12:48 +00009228 if (tp->setlpicnt && !--tp->setlpicnt)
9229 tg3_phy_eee_enable(tp);
Matt Carlson52b02d02010-10-14 10:37:41 +00009230
Joe Perches63c3a662011-04-26 08:12:10 +00009231 if (tg3_flag(tp, USE_LINKCHG_REG)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009232 u32 mac_stat;
9233 int phy_event;
9234
9235 mac_stat = tr32(MAC_STATUS);
9236
9237 phy_event = 0;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009238 if (tp->phy_flags & TG3_PHYFLG_USE_MI_INTERRUPT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009239 if (mac_stat & MAC_STATUS_MI_INTERRUPT)
9240 phy_event = 1;
9241 } else if (mac_stat & MAC_STATUS_LNKSTATE_CHANGED)
9242 phy_event = 1;
9243
9244 if (phy_event)
9245 tg3_setup_phy(tp, 0);
Joe Perches63c3a662011-04-26 08:12:10 +00009246 } else if (tg3_flag(tp, POLL_SERDES)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009247 u32 mac_stat = tr32(MAC_STATUS);
9248 int need_setup = 0;
9249
9250 if (netif_carrier_ok(tp->dev) &&
9251 (mac_stat & MAC_STATUS_LNKSTATE_CHANGED)) {
9252 need_setup = 1;
9253 }
Matt Carlsonbe98da62010-07-11 09:31:46 +00009254 if (!netif_carrier_ok(tp->dev) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07009255 (mac_stat & (MAC_STATUS_PCS_SYNCED |
9256 MAC_STATUS_SIGNAL_DET))) {
9257 need_setup = 1;
9258 }
9259 if (need_setup) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07009260 if (!tp->serdes_counter) {
9261 tw32_f(MAC_MODE,
9262 (tp->mac_mode &
9263 ~MAC_MODE_PORT_MODE_MASK));
9264 udelay(40);
9265 tw32_f(MAC_MODE, tp->mac_mode);
9266 udelay(40);
9267 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009268 tg3_setup_phy(tp, 0);
9269 }
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009270 } else if ((tp->phy_flags & TG3_PHYFLG_MII_SERDES) &&
Joe Perches63c3a662011-04-26 08:12:10 +00009271 tg3_flag(tp, 5780_CLASS)) {
Michael Chan747e8f82005-07-25 12:33:22 -07009272 tg3_serdes_parallel_detect(tp);
Matt Carlson57d8b882010-06-05 17:24:35 +00009273 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009274
9275 tp->timer_counter = tp->timer_multiplier;
9276 }
9277
Michael Chan130b8e42006-09-27 16:00:40 -07009278 /* Heartbeat is only sent once every 2 seconds.
9279 *
9280 * The heartbeat is to tell the ASF firmware that the host
9281 * driver is still alive. In the event that the OS crashes,
9282 * ASF needs to reset the hardware to free up the FIFO space
9283 * that may be filled with rx packets destined for the host.
9284 * If the FIFO is full, ASF will no longer function properly.
9285 *
9286 * Unintended resets have been reported on real time kernels
9287 * where the timer doesn't run on time. Netpoll will also have
9288 * same problem.
9289 *
9290 * The new FWCMD_NICDRV_ALIVE3 command tells the ASF firmware
9291 * to check the ring condition when the heartbeat is expiring
9292 * before doing the reset. This will prevent most unintended
9293 * resets.
9294 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07009295 if (!--tp->asf_counter) {
Joe Perches63c3a662011-04-26 08:12:10 +00009296 if (tg3_flag(tp, ENABLE_ASF) && !tg3_flag(tp, ENABLE_APE)) {
Matt Carlson7c5026a2008-05-02 16:49:29 -07009297 tg3_wait_for_event_ack(tp);
9298
Michael Chanbbadf502006-04-06 21:46:34 -07009299 tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX,
Michael Chan130b8e42006-09-27 16:00:40 -07009300 FWCMD_NICDRV_ALIVE3);
Michael Chanbbadf502006-04-06 21:46:34 -07009301 tg3_write_mem(tp, NIC_SRAM_FW_CMD_LEN_MBOX, 4);
Matt Carlsonc6cdf432010-04-05 10:19:26 +00009302 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX,
9303 TG3_FW_UPDATE_TIMEOUT_SEC);
Matt Carlson4ba526c2008-08-15 14:10:04 -07009304
9305 tg3_generate_fw_event(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009306 }
9307 tp->asf_counter = tp->asf_multiplier;
9308 }
9309
David S. Millerf47c11e2005-06-24 20:18:35 -07009310 spin_unlock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009311
Michael Chanf475f162006-03-27 23:20:14 -08009312restart_timer:
Linus Torvalds1da177e2005-04-16 15:20:36 -07009313 tp->timer.expires = jiffies + tp->timer_offset;
9314 add_timer(&tp->timer);
9315}
9316
Matt Carlson4f125f42009-09-01 12:55:02 +00009317static int tg3_request_irq(struct tg3 *tp, int irq_num)
Michael Chanfcfa0a32006-03-20 22:28:41 -08009318{
David Howells7d12e782006-10-05 14:55:46 +01009319 irq_handler_t fn;
Michael Chanfcfa0a32006-03-20 22:28:41 -08009320 unsigned long flags;
Matt Carlson4f125f42009-09-01 12:55:02 +00009321 char *name;
9322 struct tg3_napi *tnapi = &tp->napi[irq_num];
9323
9324 if (tp->irq_cnt == 1)
9325 name = tp->dev->name;
9326 else {
9327 name = &tnapi->irq_lbl[0];
9328 snprintf(name, IFNAMSIZ, "%s-%d", tp->dev->name, irq_num);
9329 name[IFNAMSIZ-1] = 0;
9330 }
Michael Chanfcfa0a32006-03-20 22:28:41 -08009331
Joe Perches63c3a662011-04-26 08:12:10 +00009332 if (tg3_flag(tp, USING_MSI) || tg3_flag(tp, USING_MSIX)) {
Michael Chanfcfa0a32006-03-20 22:28:41 -08009333 fn = tg3_msi;
Joe Perches63c3a662011-04-26 08:12:10 +00009334 if (tg3_flag(tp, 1SHOT_MSI))
Michael Chanfcfa0a32006-03-20 22:28:41 -08009335 fn = tg3_msi_1shot;
Javier Martinez Canillasab392d22011-03-28 16:27:31 +00009336 flags = 0;
Michael Chanfcfa0a32006-03-20 22:28:41 -08009337 } else {
9338 fn = tg3_interrupt;
Joe Perches63c3a662011-04-26 08:12:10 +00009339 if (tg3_flag(tp, TAGGED_STATUS))
Michael Chanfcfa0a32006-03-20 22:28:41 -08009340 fn = tg3_interrupt_tagged;
Javier Martinez Canillasab392d22011-03-28 16:27:31 +00009341 flags = IRQF_SHARED;
Michael Chanfcfa0a32006-03-20 22:28:41 -08009342 }
Matt Carlson4f125f42009-09-01 12:55:02 +00009343
9344 return request_irq(tnapi->irq_vec, fn, flags, name, tnapi);
Michael Chanfcfa0a32006-03-20 22:28:41 -08009345}
9346
Michael Chan79381092005-04-21 17:13:59 -07009347static int tg3_test_interrupt(struct tg3 *tp)
9348{
Matt Carlson09943a12009-08-28 14:01:57 +00009349 struct tg3_napi *tnapi = &tp->napi[0];
Michael Chan79381092005-04-21 17:13:59 -07009350 struct net_device *dev = tp->dev;
Michael Chanb16250e2006-09-27 16:10:14 -07009351 int err, i, intr_ok = 0;
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009352 u32 val;
Michael Chan79381092005-04-21 17:13:59 -07009353
Michael Chand4bc3922005-05-29 14:59:20 -07009354 if (!netif_running(dev))
9355 return -ENODEV;
9356
Michael Chan79381092005-04-21 17:13:59 -07009357 tg3_disable_ints(tp);
9358
Matt Carlson4f125f42009-09-01 12:55:02 +00009359 free_irq(tnapi->irq_vec, tnapi);
Michael Chan79381092005-04-21 17:13:59 -07009360
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009361 /*
9362 * Turn off MSI one shot mode. Otherwise this test has no
9363 * observable way to know whether the interrupt was delivered.
9364 */
Matt Carlson3aa1cdf2011-07-20 10:20:55 +00009365 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009366 val = tr32(MSGINT_MODE) | MSGINT_MODE_ONE_SHOT_DISABLE;
9367 tw32(MSGINT_MODE, val);
9368 }
9369
Matt Carlson4f125f42009-09-01 12:55:02 +00009370 err = request_irq(tnapi->irq_vec, tg3_test_isr,
Matt Carlson09943a12009-08-28 14:01:57 +00009371 IRQF_SHARED | IRQF_SAMPLE_RANDOM, dev->name, tnapi);
Michael Chan79381092005-04-21 17:13:59 -07009372 if (err)
9373 return err;
9374
Matt Carlson898a56f2009-08-28 14:02:40 +00009375 tnapi->hw_status->status &= ~SD_STATUS_UPDATED;
Michael Chan79381092005-04-21 17:13:59 -07009376 tg3_enable_ints(tp);
9377
9378 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +00009379 tnapi->coal_now);
Michael Chan79381092005-04-21 17:13:59 -07009380
9381 for (i = 0; i < 5; i++) {
Michael Chanb16250e2006-09-27 16:10:14 -07009382 u32 int_mbox, misc_host_ctrl;
9383
Matt Carlson898a56f2009-08-28 14:02:40 +00009384 int_mbox = tr32_mailbox(tnapi->int_mbox);
Michael Chanb16250e2006-09-27 16:10:14 -07009385 misc_host_ctrl = tr32(TG3PCI_MISC_HOST_CTRL);
9386
9387 if ((int_mbox != 0) ||
9388 (misc_host_ctrl & MISC_HOST_CTRL_MASK_PCI_INT)) {
9389 intr_ok = 1;
Michael Chan79381092005-04-21 17:13:59 -07009390 break;
Michael Chanb16250e2006-09-27 16:10:14 -07009391 }
9392
Matt Carlson3aa1cdf2011-07-20 10:20:55 +00009393 if (tg3_flag(tp, 57765_PLUS) &&
9394 tnapi->hw_status->status_tag != tnapi->last_tag)
9395 tw32_mailbox_f(tnapi->int_mbox, tnapi->last_tag << 24);
9396
Michael Chan79381092005-04-21 17:13:59 -07009397 msleep(10);
9398 }
9399
9400 tg3_disable_ints(tp);
9401
Matt Carlson4f125f42009-09-01 12:55:02 +00009402 free_irq(tnapi->irq_vec, tnapi);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009403
Matt Carlson4f125f42009-09-01 12:55:02 +00009404 err = tg3_request_irq(tp, 0);
Michael Chan79381092005-04-21 17:13:59 -07009405
9406 if (err)
9407 return err;
9408
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009409 if (intr_ok) {
9410 /* Reenable MSI one shot mode. */
Matt Carlson5b39de92011-08-31 11:44:50 +00009411 if (tg3_flag(tp, 57765_PLUS) && tg3_flag(tp, 1SHOT_MSI)) {
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009412 val = tr32(MSGINT_MODE) & ~MSGINT_MODE_ONE_SHOT_DISABLE;
9413 tw32(MSGINT_MODE, val);
9414 }
Michael Chan79381092005-04-21 17:13:59 -07009415 return 0;
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009416 }
Michael Chan79381092005-04-21 17:13:59 -07009417
9418 return -EIO;
9419}
9420
9421/* Returns 0 if MSI test succeeds or MSI test fails and INTx mode is
9422 * successfully restored
9423 */
9424static int tg3_test_msi(struct tg3 *tp)
9425{
Michael Chan79381092005-04-21 17:13:59 -07009426 int err;
9427 u16 pci_cmd;
9428
Joe Perches63c3a662011-04-26 08:12:10 +00009429 if (!tg3_flag(tp, USING_MSI))
Michael Chan79381092005-04-21 17:13:59 -07009430 return 0;
9431
9432 /* Turn off SERR reporting in case MSI terminates with Master
9433 * Abort.
9434 */
9435 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
9436 pci_write_config_word(tp->pdev, PCI_COMMAND,
9437 pci_cmd & ~PCI_COMMAND_SERR);
9438
9439 err = tg3_test_interrupt(tp);
9440
9441 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
9442
9443 if (!err)
9444 return 0;
9445
9446 /* other failures */
9447 if (err != -EIO)
9448 return err;
9449
9450 /* MSI test failed, go back to INTx mode */
Matt Carlson5129c3a2010-04-05 10:19:23 +00009451 netdev_warn(tp->dev, "No interrupt was generated using MSI. Switching "
9452 "to INTx mode. Please report this failure to the PCI "
9453 "maintainer and include system chipset information\n");
Michael Chan79381092005-04-21 17:13:59 -07009454
Matt Carlson4f125f42009-09-01 12:55:02 +00009455 free_irq(tp->napi[0].irq_vec, &tp->napi[0]);
Matt Carlson09943a12009-08-28 14:01:57 +00009456
Michael Chan79381092005-04-21 17:13:59 -07009457 pci_disable_msi(tp->pdev);
9458
Joe Perches63c3a662011-04-26 08:12:10 +00009459 tg3_flag_clear(tp, USING_MSI);
Andre Detschdc8bf1b2010-04-26 07:27:07 +00009460 tp->napi[0].irq_vec = tp->pdev->irq;
Michael Chan79381092005-04-21 17:13:59 -07009461
Matt Carlson4f125f42009-09-01 12:55:02 +00009462 err = tg3_request_irq(tp, 0);
Michael Chan79381092005-04-21 17:13:59 -07009463 if (err)
9464 return err;
9465
9466 /* Need to reset the chip because the MSI cycle may have terminated
9467 * with Master Abort.
9468 */
David S. Millerf47c11e2005-06-24 20:18:35 -07009469 tg3_full_lock(tp, 1);
Michael Chan79381092005-04-21 17:13:59 -07009470
Michael Chan944d9802005-05-29 14:57:48 -07009471 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07009472 err = tg3_init_hw(tp, 1);
Michael Chan79381092005-04-21 17:13:59 -07009473
David S. Millerf47c11e2005-06-24 20:18:35 -07009474 tg3_full_unlock(tp);
Michael Chan79381092005-04-21 17:13:59 -07009475
9476 if (err)
Matt Carlson4f125f42009-09-01 12:55:02 +00009477 free_irq(tp->napi[0].irq_vec, &tp->napi[0]);
Michael Chan79381092005-04-21 17:13:59 -07009478
9479 return err;
9480}
9481
Matt Carlson9e9fd122009-01-19 16:57:45 -08009482static int tg3_request_firmware(struct tg3 *tp)
9483{
9484 const __be32 *fw_data;
9485
9486 if (request_firmware(&tp->fw, tp->fw_needed, &tp->pdev->dev)) {
Joe Perches05dbe002010-02-17 19:44:19 +00009487 netdev_err(tp->dev, "Failed to load firmware \"%s\"\n",
9488 tp->fw_needed);
Matt Carlson9e9fd122009-01-19 16:57:45 -08009489 return -ENOENT;
9490 }
9491
9492 fw_data = (void *)tp->fw->data;
9493
9494 /* Firmware blob starts with version numbers, followed by
9495 * start address and _full_ length including BSS sections
9496 * (which must be longer than the actual data, of course
9497 */
9498
9499 tp->fw_len = be32_to_cpu(fw_data[2]); /* includes bss */
9500 if (tp->fw_len < (tp->fw->size - 12)) {
Joe Perches05dbe002010-02-17 19:44:19 +00009501 netdev_err(tp->dev, "bogus length %d in \"%s\"\n",
9502 tp->fw_len, tp->fw_needed);
Matt Carlson9e9fd122009-01-19 16:57:45 -08009503 release_firmware(tp->fw);
9504 tp->fw = NULL;
9505 return -EINVAL;
9506 }
9507
9508 /* We no longer need firmware; we have it. */
9509 tp->fw_needed = NULL;
9510 return 0;
9511}
9512
Matt Carlson679563f2009-09-01 12:55:46 +00009513static bool tg3_enable_msix(struct tg3 *tp)
9514{
9515 int i, rc, cpus = num_online_cpus();
9516 struct msix_entry msix_ent[tp->irq_max];
9517
9518 if (cpus == 1)
9519 /* Just fallback to the simpler MSI mode. */
9520 return false;
9521
9522 /*
9523 * We want as many rx rings enabled as there are cpus.
9524 * The first MSIX vector only deals with link interrupts, etc,
9525 * so we add one to the number of vectors we are requesting.
9526 */
9527 tp->irq_cnt = min_t(unsigned, cpus + 1, tp->irq_max);
9528
9529 for (i = 0; i < tp->irq_max; i++) {
9530 msix_ent[i].entry = i;
9531 msix_ent[i].vector = 0;
9532 }
9533
9534 rc = pci_enable_msix(tp->pdev, msix_ent, tp->irq_cnt);
Matt Carlson2430b032010-06-05 17:24:34 +00009535 if (rc < 0) {
9536 return false;
9537 } else if (rc != 0) {
Matt Carlson679563f2009-09-01 12:55:46 +00009538 if (pci_enable_msix(tp->pdev, msix_ent, rc))
9539 return false;
Joe Perches05dbe002010-02-17 19:44:19 +00009540 netdev_notice(tp->dev, "Requested %d MSI-X vectors, received %d\n",
9541 tp->irq_cnt, rc);
Matt Carlson679563f2009-09-01 12:55:46 +00009542 tp->irq_cnt = rc;
9543 }
9544
9545 for (i = 0; i < tp->irq_max; i++)
9546 tp->napi[i].irq_vec = msix_ent[i].vector;
9547
Ben Hutchings2ddaad32010-09-27 22:11:51 -07009548 netif_set_real_num_tx_queues(tp->dev, 1);
9549 rc = tp->irq_cnt > 1 ? tp->irq_cnt - 1 : 1;
9550 if (netif_set_real_num_rx_queues(tp->dev, rc)) {
9551 pci_disable_msix(tp->pdev);
9552 return false;
9553 }
Matt Carlsonb92b9042010-11-24 08:31:51 +00009554
9555 if (tp->irq_cnt > 1) {
Joe Perches63c3a662011-04-26 08:12:10 +00009556 tg3_flag_set(tp, ENABLE_RSS);
Matt Carlsond78b59f2011-04-05 14:22:46 +00009557
9558 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
9559 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
Joe Perches63c3a662011-04-26 08:12:10 +00009560 tg3_flag_set(tp, ENABLE_TSS);
Matt Carlsonb92b9042010-11-24 08:31:51 +00009561 netif_set_real_num_tx_queues(tp->dev, tp->irq_cnt - 1);
9562 }
9563 }
Matt Carlson2430b032010-06-05 17:24:34 +00009564
Matt Carlson679563f2009-09-01 12:55:46 +00009565 return true;
9566}
9567
Matt Carlson07b01732009-08-28 14:01:15 +00009568static void tg3_ints_init(struct tg3 *tp)
9569{
Joe Perches63c3a662011-04-26 08:12:10 +00009570 if ((tg3_flag(tp, SUPPORT_MSI) || tg3_flag(tp, SUPPORT_MSIX)) &&
9571 !tg3_flag(tp, TAGGED_STATUS)) {
Matt Carlson07b01732009-08-28 14:01:15 +00009572 /* All MSI supporting chips should support tagged
9573 * status. Assert that this is the case.
9574 */
Matt Carlson5129c3a2010-04-05 10:19:23 +00009575 netdev_warn(tp->dev,
9576 "MSI without TAGGED_STATUS? Not using MSI\n");
Matt Carlson679563f2009-09-01 12:55:46 +00009577 goto defcfg;
Matt Carlson07b01732009-08-28 14:01:15 +00009578 }
Matt Carlson4f125f42009-09-01 12:55:02 +00009579
Joe Perches63c3a662011-04-26 08:12:10 +00009580 if (tg3_flag(tp, SUPPORT_MSIX) && tg3_enable_msix(tp))
9581 tg3_flag_set(tp, USING_MSIX);
9582 else if (tg3_flag(tp, SUPPORT_MSI) && pci_enable_msi(tp->pdev) == 0)
9583 tg3_flag_set(tp, USING_MSI);
Matt Carlson679563f2009-09-01 12:55:46 +00009584
Joe Perches63c3a662011-04-26 08:12:10 +00009585 if (tg3_flag(tp, USING_MSI) || tg3_flag(tp, USING_MSIX)) {
Matt Carlson679563f2009-09-01 12:55:46 +00009586 u32 msi_mode = tr32(MSGINT_MODE);
Joe Perches63c3a662011-04-26 08:12:10 +00009587 if (tg3_flag(tp, USING_MSIX) && tp->irq_cnt > 1)
Matt Carlsonbaf8a942009-09-01 13:13:00 +00009588 msi_mode |= MSGINT_MODE_MULTIVEC_EN;
Matt Carlson5b39de92011-08-31 11:44:50 +00009589 if (!tg3_flag(tp, 1SHOT_MSI))
9590 msi_mode |= MSGINT_MODE_ONE_SHOT_DISABLE;
Matt Carlson679563f2009-09-01 12:55:46 +00009591 tw32(MSGINT_MODE, msi_mode | MSGINT_MODE_ENABLE);
9592 }
9593defcfg:
Joe Perches63c3a662011-04-26 08:12:10 +00009594 if (!tg3_flag(tp, USING_MSIX)) {
Matt Carlson679563f2009-09-01 12:55:46 +00009595 tp->irq_cnt = 1;
9596 tp->napi[0].irq_vec = tp->pdev->irq;
Ben Hutchings2ddaad32010-09-27 22:11:51 -07009597 netif_set_real_num_tx_queues(tp->dev, 1);
Matt Carlson85407882010-10-06 13:40:58 -07009598 netif_set_real_num_rx_queues(tp->dev, 1);
Matt Carlson679563f2009-09-01 12:55:46 +00009599 }
Matt Carlson07b01732009-08-28 14:01:15 +00009600}
9601
9602static void tg3_ints_fini(struct tg3 *tp)
9603{
Joe Perches63c3a662011-04-26 08:12:10 +00009604 if (tg3_flag(tp, USING_MSIX))
Matt Carlson679563f2009-09-01 12:55:46 +00009605 pci_disable_msix(tp->pdev);
Joe Perches63c3a662011-04-26 08:12:10 +00009606 else if (tg3_flag(tp, USING_MSI))
Matt Carlson679563f2009-09-01 12:55:46 +00009607 pci_disable_msi(tp->pdev);
Joe Perches63c3a662011-04-26 08:12:10 +00009608 tg3_flag_clear(tp, USING_MSI);
9609 tg3_flag_clear(tp, USING_MSIX);
9610 tg3_flag_clear(tp, ENABLE_RSS);
9611 tg3_flag_clear(tp, ENABLE_TSS);
Matt Carlson07b01732009-08-28 14:01:15 +00009612}
9613
Linus Torvalds1da177e2005-04-16 15:20:36 -07009614static int tg3_open(struct net_device *dev)
9615{
9616 struct tg3 *tp = netdev_priv(dev);
Matt Carlson4f125f42009-09-01 12:55:02 +00009617 int i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009618
Matt Carlson9e9fd122009-01-19 16:57:45 -08009619 if (tp->fw_needed) {
9620 err = tg3_request_firmware(tp);
9621 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0) {
9622 if (err)
9623 return err;
9624 } else if (err) {
Joe Perches05dbe002010-02-17 19:44:19 +00009625 netdev_warn(tp->dev, "TSO capability disabled\n");
Joe Perches63c3a662011-04-26 08:12:10 +00009626 tg3_flag_clear(tp, TSO_CAPABLE);
9627 } else if (!tg3_flag(tp, TSO_CAPABLE)) {
Joe Perches05dbe002010-02-17 19:44:19 +00009628 netdev_notice(tp->dev, "TSO capability restored\n");
Joe Perches63c3a662011-04-26 08:12:10 +00009629 tg3_flag_set(tp, TSO_CAPABLE);
Matt Carlson9e9fd122009-01-19 16:57:45 -08009630 }
9631 }
9632
Michael Chanc49a1562006-12-17 17:07:29 -08009633 netif_carrier_off(tp->dev);
9634
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00009635 err = tg3_power_up(tp);
Matt Carlson2f751b62008-08-04 23:17:34 -07009636 if (err)
Michael Chanbc1c7562006-03-20 17:48:03 -08009637 return err;
Matt Carlson2f751b62008-08-04 23:17:34 -07009638
9639 tg3_full_lock(tp, 0);
Michael Chanbc1c7562006-03-20 17:48:03 -08009640
Linus Torvalds1da177e2005-04-16 15:20:36 -07009641 tg3_disable_ints(tp);
Joe Perches63c3a662011-04-26 08:12:10 +00009642 tg3_flag_clear(tp, INIT_COMPLETE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009643
David S. Millerf47c11e2005-06-24 20:18:35 -07009644 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009645
Matt Carlson679563f2009-09-01 12:55:46 +00009646 /*
9647 * Setup interrupts first so we know how
9648 * many NAPI resources to allocate
9649 */
9650 tg3_ints_init(tp);
9651
Linus Torvalds1da177e2005-04-16 15:20:36 -07009652 /* The placement of this call is tied
9653 * to the setup and use of Host TX descriptors.
9654 */
9655 err = tg3_alloc_consistent(tp);
9656 if (err)
Matt Carlson679563f2009-09-01 12:55:46 +00009657 goto err_out1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009658
Matt Carlson66cfd1b2010-09-30 10:34:30 +00009659 tg3_napi_init(tp);
9660
Matt Carlsonfed97812009-09-01 13:10:19 +00009661 tg3_napi_enable(tp);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07009662
Matt Carlson4f125f42009-09-01 12:55:02 +00009663 for (i = 0; i < tp->irq_cnt; i++) {
9664 struct tg3_napi *tnapi = &tp->napi[i];
9665 err = tg3_request_irq(tp, i);
9666 if (err) {
Matt Carlson5bc09182011-11-04 09:15:01 +00009667 for (i--; i >= 0; i--) {
9668 tnapi = &tp->napi[i];
Matt Carlson4f125f42009-09-01 12:55:02 +00009669 free_irq(tnapi->irq_vec, tnapi);
Matt Carlson5bc09182011-11-04 09:15:01 +00009670 }
9671 goto err_out2;
Matt Carlson4f125f42009-09-01 12:55:02 +00009672 }
9673 }
Matt Carlson07b01732009-08-28 14:01:15 +00009674
David S. Millerf47c11e2005-06-24 20:18:35 -07009675 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009676
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07009677 err = tg3_init_hw(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009678 if (err) {
Michael Chan944d9802005-05-29 14:57:48 -07009679 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009680 tg3_free_rings(tp);
9681 } else {
Matt Carlson0e6cf6a2011-06-13 13:38:55 +00009682 if (tg3_flag(tp, TAGGED_STATUS) &&
9683 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717 &&
9684 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_57765)
David S. Millerfac9b832005-05-18 22:46:34 -07009685 tp->timer_offset = HZ;
9686 else
9687 tp->timer_offset = HZ / 10;
9688
9689 BUG_ON(tp->timer_offset > HZ);
9690 tp->timer_counter = tp->timer_multiplier =
9691 (HZ / tp->timer_offset);
9692 tp->asf_counter = tp->asf_multiplier =
Michael Chan28fbef72005-10-26 15:48:35 -07009693 ((HZ / tp->timer_offset) * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009694
9695 init_timer(&tp->timer);
9696 tp->timer.expires = jiffies + tp->timer_offset;
9697 tp->timer.data = (unsigned long) tp;
9698 tp->timer.function = tg3_timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009699 }
9700
David S. Millerf47c11e2005-06-24 20:18:35 -07009701 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009702
Matt Carlson07b01732009-08-28 14:01:15 +00009703 if (err)
Matt Carlson679563f2009-09-01 12:55:46 +00009704 goto err_out3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009705
Joe Perches63c3a662011-04-26 08:12:10 +00009706 if (tg3_flag(tp, USING_MSI)) {
Michael Chan79381092005-04-21 17:13:59 -07009707 err = tg3_test_msi(tp);
David S. Millerfac9b832005-05-18 22:46:34 -07009708
Michael Chan79381092005-04-21 17:13:59 -07009709 if (err) {
David S. Millerf47c11e2005-06-24 20:18:35 -07009710 tg3_full_lock(tp, 0);
Michael Chan944d9802005-05-29 14:57:48 -07009711 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Michael Chan79381092005-04-21 17:13:59 -07009712 tg3_free_rings(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -07009713 tg3_full_unlock(tp);
Michael Chan79381092005-04-21 17:13:59 -07009714
Matt Carlson679563f2009-09-01 12:55:46 +00009715 goto err_out2;
Michael Chan79381092005-04-21 17:13:59 -07009716 }
Michael Chanfcfa0a32006-03-20 22:28:41 -08009717
Joe Perches63c3a662011-04-26 08:12:10 +00009718 if (!tg3_flag(tp, 57765_PLUS) && tg3_flag(tp, USING_MSI)) {
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009719 u32 val = tr32(PCIE_TRANSACTION_CFG);
Michael Chanfcfa0a32006-03-20 22:28:41 -08009720
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009721 tw32(PCIE_TRANSACTION_CFG,
9722 val | PCIE_TRANS_CFG_1SHOT_MSI);
Michael Chanfcfa0a32006-03-20 22:28:41 -08009723 }
Michael Chan79381092005-04-21 17:13:59 -07009724 }
9725
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009726 tg3_phy_start(tp);
9727
David S. Millerf47c11e2005-06-24 20:18:35 -07009728 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009729
Michael Chan79381092005-04-21 17:13:59 -07009730 add_timer(&tp->timer);
Joe Perches63c3a662011-04-26 08:12:10 +00009731 tg3_flag_set(tp, INIT_COMPLETE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009732 tg3_enable_ints(tp);
9733
David S. Millerf47c11e2005-06-24 20:18:35 -07009734 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009735
Matt Carlsonfe5f5782009-09-01 13:09:39 +00009736 netif_tx_start_all_queues(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009737
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00009738 /*
9739 * Reset loopback feature if it was turned on while the device was down
9740 * make sure that it's installed properly now.
9741 */
9742 if (dev->features & NETIF_F_LOOPBACK)
9743 tg3_set_loopback(dev, dev->features);
9744
Linus Torvalds1da177e2005-04-16 15:20:36 -07009745 return 0;
Matt Carlson07b01732009-08-28 14:01:15 +00009746
Matt Carlson679563f2009-09-01 12:55:46 +00009747err_out3:
Matt Carlson4f125f42009-09-01 12:55:02 +00009748 for (i = tp->irq_cnt - 1; i >= 0; i--) {
9749 struct tg3_napi *tnapi = &tp->napi[i];
9750 free_irq(tnapi->irq_vec, tnapi);
9751 }
Matt Carlson07b01732009-08-28 14:01:15 +00009752
Matt Carlson679563f2009-09-01 12:55:46 +00009753err_out2:
Matt Carlsonfed97812009-09-01 13:10:19 +00009754 tg3_napi_disable(tp);
Matt Carlson66cfd1b2010-09-30 10:34:30 +00009755 tg3_napi_fini(tp);
Matt Carlson07b01732009-08-28 14:01:15 +00009756 tg3_free_consistent(tp);
Matt Carlson679563f2009-09-01 12:55:46 +00009757
9758err_out1:
9759 tg3_ints_fini(tp);
Matt Carlsoncd0d7222011-07-13 09:27:33 +00009760 tg3_frob_aux_power(tp, false);
9761 pci_set_power_state(tp->pdev, PCI_D3hot);
Matt Carlson07b01732009-08-28 14:01:15 +00009762 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009763}
9764
Eric Dumazet511d2222010-07-07 20:44:24 +00009765static struct rtnl_link_stats64 *tg3_get_stats64(struct net_device *,
9766 struct rtnl_link_stats64 *);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009767static struct tg3_ethtool_stats *tg3_get_estats(struct tg3 *);
9768
9769static int tg3_close(struct net_device *dev)
9770{
Matt Carlson4f125f42009-09-01 12:55:02 +00009771 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009772 struct tg3 *tp = netdev_priv(dev);
9773
Matt Carlsonfed97812009-09-01 13:10:19 +00009774 tg3_napi_disable(tp);
Matt Carlsondb219972011-11-04 09:15:03 +00009775 tg3_reset_task_cancel(tp);
Michael Chan7faa0062006-02-02 17:29:28 -08009776
Matt Carlsonfe5f5782009-09-01 13:09:39 +00009777 netif_tx_stop_all_queues(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009778
9779 del_timer_sync(&tp->timer);
9780
Matt Carlson24bb4fb2009-10-05 17:55:29 +00009781 tg3_phy_stop(tp);
9782
David S. Millerf47c11e2005-06-24 20:18:35 -07009783 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009784
9785 tg3_disable_ints(tp);
9786
Michael Chan944d9802005-05-29 14:57:48 -07009787 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009788 tg3_free_rings(tp);
Joe Perches63c3a662011-04-26 08:12:10 +00009789 tg3_flag_clear(tp, INIT_COMPLETE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009790
David S. Millerf47c11e2005-06-24 20:18:35 -07009791 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009792
Matt Carlson4f125f42009-09-01 12:55:02 +00009793 for (i = tp->irq_cnt - 1; i >= 0; i--) {
9794 struct tg3_napi *tnapi = &tp->napi[i];
9795 free_irq(tnapi->irq_vec, tnapi);
9796 }
Matt Carlson07b01732009-08-28 14:01:15 +00009797
9798 tg3_ints_fini(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009799
Eric Dumazet511d2222010-07-07 20:44:24 +00009800 tg3_get_stats64(tp->dev, &tp->net_stats_prev);
9801
Linus Torvalds1da177e2005-04-16 15:20:36 -07009802 memcpy(&tp->estats_prev, tg3_get_estats(tp),
9803 sizeof(tp->estats_prev));
9804
Matt Carlson66cfd1b2010-09-30 10:34:30 +00009805 tg3_napi_fini(tp);
9806
Linus Torvalds1da177e2005-04-16 15:20:36 -07009807 tg3_free_consistent(tp);
9808
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00009809 tg3_power_down(tp);
Michael Chanbc1c7562006-03-20 17:48:03 -08009810
9811 netif_carrier_off(tp->dev);
9812
Linus Torvalds1da177e2005-04-16 15:20:36 -07009813 return 0;
9814}
9815
Eric Dumazet511d2222010-07-07 20:44:24 +00009816static inline u64 get_stat64(tg3_stat64_t *val)
Stefan Buehler816f8b82008-08-15 14:10:54 -07009817{
9818 return ((u64)val->high << 32) | ((u64)val->low);
9819}
9820
Eric Dumazet511d2222010-07-07 20:44:24 +00009821static u64 calc_crc_errors(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009822{
9823 struct tg3_hw_stats *hw_stats = tp->hw_stats;
9824
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009825 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07009826 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
9827 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009828 u32 val;
9829
David S. Millerf47c11e2005-06-24 20:18:35 -07009830 spin_lock_bh(&tp->lock);
Michael Chan569a5df2007-02-13 12:18:15 -08009831 if (!tg3_readphy(tp, MII_TG3_TEST1, &val)) {
9832 tg3_writephy(tp, MII_TG3_TEST1,
9833 val | MII_TG3_TEST1_CRC_EN);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00009834 tg3_readphy(tp, MII_TG3_RXR_COUNTERS, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009835 } else
9836 val = 0;
David S. Millerf47c11e2005-06-24 20:18:35 -07009837 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009838
9839 tp->phy_crc_errors += val;
9840
9841 return tp->phy_crc_errors;
9842 }
9843
9844 return get_stat64(&hw_stats->rx_fcs_errors);
9845}
9846
9847#define ESTAT_ADD(member) \
9848 estats->member = old_estats->member + \
Eric Dumazet511d2222010-07-07 20:44:24 +00009849 get_stat64(&hw_stats->member)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009850
9851static struct tg3_ethtool_stats *tg3_get_estats(struct tg3 *tp)
9852{
9853 struct tg3_ethtool_stats *estats = &tp->estats;
9854 struct tg3_ethtool_stats *old_estats = &tp->estats_prev;
9855 struct tg3_hw_stats *hw_stats = tp->hw_stats;
9856
9857 if (!hw_stats)
9858 return old_estats;
9859
9860 ESTAT_ADD(rx_octets);
9861 ESTAT_ADD(rx_fragments);
9862 ESTAT_ADD(rx_ucast_packets);
9863 ESTAT_ADD(rx_mcast_packets);
9864 ESTAT_ADD(rx_bcast_packets);
9865 ESTAT_ADD(rx_fcs_errors);
9866 ESTAT_ADD(rx_align_errors);
9867 ESTAT_ADD(rx_xon_pause_rcvd);
9868 ESTAT_ADD(rx_xoff_pause_rcvd);
9869 ESTAT_ADD(rx_mac_ctrl_rcvd);
9870 ESTAT_ADD(rx_xoff_entered);
9871 ESTAT_ADD(rx_frame_too_long_errors);
9872 ESTAT_ADD(rx_jabbers);
9873 ESTAT_ADD(rx_undersize_packets);
9874 ESTAT_ADD(rx_in_length_errors);
9875 ESTAT_ADD(rx_out_length_errors);
9876 ESTAT_ADD(rx_64_or_less_octet_packets);
9877 ESTAT_ADD(rx_65_to_127_octet_packets);
9878 ESTAT_ADD(rx_128_to_255_octet_packets);
9879 ESTAT_ADD(rx_256_to_511_octet_packets);
9880 ESTAT_ADD(rx_512_to_1023_octet_packets);
9881 ESTAT_ADD(rx_1024_to_1522_octet_packets);
9882 ESTAT_ADD(rx_1523_to_2047_octet_packets);
9883 ESTAT_ADD(rx_2048_to_4095_octet_packets);
9884 ESTAT_ADD(rx_4096_to_8191_octet_packets);
9885 ESTAT_ADD(rx_8192_to_9022_octet_packets);
9886
9887 ESTAT_ADD(tx_octets);
9888 ESTAT_ADD(tx_collisions);
9889 ESTAT_ADD(tx_xon_sent);
9890 ESTAT_ADD(tx_xoff_sent);
9891 ESTAT_ADD(tx_flow_control);
9892 ESTAT_ADD(tx_mac_errors);
9893 ESTAT_ADD(tx_single_collisions);
9894 ESTAT_ADD(tx_mult_collisions);
9895 ESTAT_ADD(tx_deferred);
9896 ESTAT_ADD(tx_excessive_collisions);
9897 ESTAT_ADD(tx_late_collisions);
9898 ESTAT_ADD(tx_collide_2times);
9899 ESTAT_ADD(tx_collide_3times);
9900 ESTAT_ADD(tx_collide_4times);
9901 ESTAT_ADD(tx_collide_5times);
9902 ESTAT_ADD(tx_collide_6times);
9903 ESTAT_ADD(tx_collide_7times);
9904 ESTAT_ADD(tx_collide_8times);
9905 ESTAT_ADD(tx_collide_9times);
9906 ESTAT_ADD(tx_collide_10times);
9907 ESTAT_ADD(tx_collide_11times);
9908 ESTAT_ADD(tx_collide_12times);
9909 ESTAT_ADD(tx_collide_13times);
9910 ESTAT_ADD(tx_collide_14times);
9911 ESTAT_ADD(tx_collide_15times);
9912 ESTAT_ADD(tx_ucast_packets);
9913 ESTAT_ADD(tx_mcast_packets);
9914 ESTAT_ADD(tx_bcast_packets);
9915 ESTAT_ADD(tx_carrier_sense_errors);
9916 ESTAT_ADD(tx_discards);
9917 ESTAT_ADD(tx_errors);
9918
9919 ESTAT_ADD(dma_writeq_full);
9920 ESTAT_ADD(dma_write_prioq_full);
9921 ESTAT_ADD(rxbds_empty);
9922 ESTAT_ADD(rx_discards);
9923 ESTAT_ADD(rx_errors);
9924 ESTAT_ADD(rx_threshold_hit);
9925
9926 ESTAT_ADD(dma_readq_full);
9927 ESTAT_ADD(dma_read_prioq_full);
9928 ESTAT_ADD(tx_comp_queue_full);
9929
9930 ESTAT_ADD(ring_set_send_prod_index);
9931 ESTAT_ADD(ring_status_update);
9932 ESTAT_ADD(nic_irqs);
9933 ESTAT_ADD(nic_avoided_irqs);
9934 ESTAT_ADD(nic_tx_threshold_hit);
9935
Matt Carlson4452d092011-05-19 12:12:51 +00009936 ESTAT_ADD(mbuf_lwm_thresh_hit);
9937
Linus Torvalds1da177e2005-04-16 15:20:36 -07009938 return estats;
9939}
9940
Eric Dumazet511d2222010-07-07 20:44:24 +00009941static struct rtnl_link_stats64 *tg3_get_stats64(struct net_device *dev,
9942 struct rtnl_link_stats64 *stats)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009943{
9944 struct tg3 *tp = netdev_priv(dev);
Eric Dumazet511d2222010-07-07 20:44:24 +00009945 struct rtnl_link_stats64 *old_stats = &tp->net_stats_prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009946 struct tg3_hw_stats *hw_stats = tp->hw_stats;
9947
9948 if (!hw_stats)
9949 return old_stats;
9950
9951 stats->rx_packets = old_stats->rx_packets +
9952 get_stat64(&hw_stats->rx_ucast_packets) +
9953 get_stat64(&hw_stats->rx_mcast_packets) +
9954 get_stat64(&hw_stats->rx_bcast_packets);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009955
Linus Torvalds1da177e2005-04-16 15:20:36 -07009956 stats->tx_packets = old_stats->tx_packets +
9957 get_stat64(&hw_stats->tx_ucast_packets) +
9958 get_stat64(&hw_stats->tx_mcast_packets) +
9959 get_stat64(&hw_stats->tx_bcast_packets);
9960
9961 stats->rx_bytes = old_stats->rx_bytes +
9962 get_stat64(&hw_stats->rx_octets);
9963 stats->tx_bytes = old_stats->tx_bytes +
9964 get_stat64(&hw_stats->tx_octets);
9965
9966 stats->rx_errors = old_stats->rx_errors +
John W. Linville4f63b872005-09-12 14:43:18 -07009967 get_stat64(&hw_stats->rx_errors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009968 stats->tx_errors = old_stats->tx_errors +
9969 get_stat64(&hw_stats->tx_errors) +
9970 get_stat64(&hw_stats->tx_mac_errors) +
9971 get_stat64(&hw_stats->tx_carrier_sense_errors) +
9972 get_stat64(&hw_stats->tx_discards);
9973
9974 stats->multicast = old_stats->multicast +
9975 get_stat64(&hw_stats->rx_mcast_packets);
9976 stats->collisions = old_stats->collisions +
9977 get_stat64(&hw_stats->tx_collisions);
9978
9979 stats->rx_length_errors = old_stats->rx_length_errors +
9980 get_stat64(&hw_stats->rx_frame_too_long_errors) +
9981 get_stat64(&hw_stats->rx_undersize_packets);
9982
9983 stats->rx_over_errors = old_stats->rx_over_errors +
9984 get_stat64(&hw_stats->rxbds_empty);
9985 stats->rx_frame_errors = old_stats->rx_frame_errors +
9986 get_stat64(&hw_stats->rx_align_errors);
9987 stats->tx_aborted_errors = old_stats->tx_aborted_errors +
9988 get_stat64(&hw_stats->tx_discards);
9989 stats->tx_carrier_errors = old_stats->tx_carrier_errors +
9990 get_stat64(&hw_stats->tx_carrier_sense_errors);
9991
9992 stats->rx_crc_errors = old_stats->rx_crc_errors +
9993 calc_crc_errors(tp);
9994
John W. Linville4f63b872005-09-12 14:43:18 -07009995 stats->rx_missed_errors = old_stats->rx_missed_errors +
9996 get_stat64(&hw_stats->rx_discards);
9997
Eric Dumazetb0057c52010-10-10 19:55:52 +00009998 stats->rx_dropped = tp->rx_dropped;
Eric Dumazet48855432011-10-24 07:53:03 +00009999 stats->tx_dropped = tp->tx_dropped;
Eric Dumazetb0057c52010-10-10 19:55:52 +000010000
Linus Torvalds1da177e2005-04-16 15:20:36 -070010001 return stats;
10002}
10003
10004static inline u32 calc_crc(unsigned char *buf, int len)
10005{
10006 u32 reg;
10007 u32 tmp;
10008 int j, k;
10009
10010 reg = 0xffffffff;
10011
10012 for (j = 0; j < len; j++) {
10013 reg ^= buf[j];
10014
10015 for (k = 0; k < 8; k++) {
10016 tmp = reg & 0x01;
10017
10018 reg >>= 1;
10019
Matt Carlson859a588792010-04-05 10:19:28 +000010020 if (tmp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010021 reg ^= 0xedb88320;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010022 }
10023 }
10024
10025 return ~reg;
10026}
10027
10028static void tg3_set_multi(struct tg3 *tp, unsigned int accept_all)
10029{
10030 /* accept or reject all multicast frames */
10031 tw32(MAC_HASH_REG_0, accept_all ? 0xffffffff : 0);
10032 tw32(MAC_HASH_REG_1, accept_all ? 0xffffffff : 0);
10033 tw32(MAC_HASH_REG_2, accept_all ? 0xffffffff : 0);
10034 tw32(MAC_HASH_REG_3, accept_all ? 0xffffffff : 0);
10035}
10036
10037static void __tg3_set_rx_mode(struct net_device *dev)
10038{
10039 struct tg3 *tp = netdev_priv(dev);
10040 u32 rx_mode;
10041
10042 rx_mode = tp->rx_mode & ~(RX_MODE_PROMISC |
10043 RX_MODE_KEEP_VLAN_TAG);
10044
Matt Carlsonbf933c82011-01-25 15:58:49 +000010045#if !defined(CONFIG_VLAN_8021Q) && !defined(CONFIG_VLAN_8021Q_MODULE)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010046 /* When ASF is in use, we always keep the RX_MODE_KEEP_VLAN_TAG
10047 * flag clear.
10048 */
Joe Perches63c3a662011-04-26 08:12:10 +000010049 if (!tg3_flag(tp, ENABLE_ASF))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010050 rx_mode |= RX_MODE_KEEP_VLAN_TAG;
10051#endif
10052
10053 if (dev->flags & IFF_PROMISC) {
10054 /* Promiscuous mode. */
10055 rx_mode |= RX_MODE_PROMISC;
10056 } else if (dev->flags & IFF_ALLMULTI) {
10057 /* Accept all multicast. */
Matt Carlsonde6f31e2010-04-12 06:58:30 +000010058 tg3_set_multi(tp, 1);
Jiri Pirko4cd24ea2010-02-08 04:30:35 +000010059 } else if (netdev_mc_empty(dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010060 /* Reject all multicast. */
Matt Carlsonde6f31e2010-04-12 06:58:30 +000010061 tg3_set_multi(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010062 } else {
10063 /* Accept one or more multicast(s). */
Jiri Pirko22bedad32010-04-01 21:22:57 +000010064 struct netdev_hw_addr *ha;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010065 u32 mc_filter[4] = { 0, };
10066 u32 regidx;
10067 u32 bit;
10068 u32 crc;
10069
Jiri Pirko22bedad32010-04-01 21:22:57 +000010070 netdev_for_each_mc_addr(ha, dev) {
10071 crc = calc_crc(ha->addr, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010072 bit = ~crc & 0x7f;
10073 regidx = (bit & 0x60) >> 5;
10074 bit &= 0x1f;
10075 mc_filter[regidx] |= (1 << bit);
10076 }
10077
10078 tw32(MAC_HASH_REG_0, mc_filter[0]);
10079 tw32(MAC_HASH_REG_1, mc_filter[1]);
10080 tw32(MAC_HASH_REG_2, mc_filter[2]);
10081 tw32(MAC_HASH_REG_3, mc_filter[3]);
10082 }
10083
10084 if (rx_mode != tp->rx_mode) {
10085 tp->rx_mode = rx_mode;
10086 tw32_f(MAC_RX_MODE, rx_mode);
10087 udelay(10);
10088 }
10089}
10090
10091static void tg3_set_rx_mode(struct net_device *dev)
10092{
10093 struct tg3 *tp = netdev_priv(dev);
10094
Michael Chane75f7c92006-03-20 21:33:26 -080010095 if (!netif_running(dev))
10096 return;
10097
David S. Millerf47c11e2005-06-24 20:18:35 -070010098 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010099 __tg3_set_rx_mode(dev);
David S. Millerf47c11e2005-06-24 20:18:35 -070010100 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010101}
10102
Linus Torvalds1da177e2005-04-16 15:20:36 -070010103static int tg3_get_regs_len(struct net_device *dev)
10104{
Matt Carlson97bd8e42011-04-13 11:05:04 +000010105 return TG3_REG_BLK_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010106}
10107
10108static void tg3_get_regs(struct net_device *dev,
10109 struct ethtool_regs *regs, void *_p)
10110{
Linus Torvalds1da177e2005-04-16 15:20:36 -070010111 struct tg3 *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010112
10113 regs->version = 0;
10114
Matt Carlson97bd8e42011-04-13 11:05:04 +000010115 memset(_p, 0, TG3_REG_BLK_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010116
Matt Carlson80096062010-08-02 11:26:06 +000010117 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Michael Chanbc1c7562006-03-20 17:48:03 -080010118 return;
10119
David S. Millerf47c11e2005-06-24 20:18:35 -070010120 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010121
Matt Carlson97bd8e42011-04-13 11:05:04 +000010122 tg3_dump_legacy_regs(tp, (u32 *)_p);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010123
David S. Millerf47c11e2005-06-24 20:18:35 -070010124 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010125}
10126
10127static int tg3_get_eeprom_len(struct net_device *dev)
10128{
10129 struct tg3 *tp = netdev_priv(dev);
10130
10131 return tp->nvram_size;
10132}
10133
Linus Torvalds1da177e2005-04-16 15:20:36 -070010134static int tg3_get_eeprom(struct net_device *dev, struct ethtool_eeprom *eeprom, u8 *data)
10135{
10136 struct tg3 *tp = netdev_priv(dev);
10137 int ret;
10138 u8 *pd;
Al Virob9fc7dc2007-12-17 22:59:57 -080010139 u32 i, offset, len, b_offset, b_count;
Matt Carlsona9dc5292009-02-25 14:25:30 +000010140 __be32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010141
Joe Perches63c3a662011-04-26 08:12:10 +000010142 if (tg3_flag(tp, NO_NVRAM))
Matt Carlsondf259d82009-04-20 06:57:14 +000010143 return -EINVAL;
10144
Matt Carlson80096062010-08-02 11:26:06 +000010145 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Michael Chanbc1c7562006-03-20 17:48:03 -080010146 return -EAGAIN;
10147
Linus Torvalds1da177e2005-04-16 15:20:36 -070010148 offset = eeprom->offset;
10149 len = eeprom->len;
10150 eeprom->len = 0;
10151
10152 eeprom->magic = TG3_EEPROM_MAGIC;
10153
10154 if (offset & 3) {
10155 /* adjustments to start on required 4 byte boundary */
10156 b_offset = offset & 3;
10157 b_count = 4 - b_offset;
10158 if (b_count > len) {
10159 /* i.e. offset=1 len=2 */
10160 b_count = len;
10161 }
Matt Carlsona9dc5292009-02-25 14:25:30 +000010162 ret = tg3_nvram_read_be32(tp, offset-b_offset, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010163 if (ret)
10164 return ret;
Matt Carlsonbe98da62010-07-11 09:31:46 +000010165 memcpy(data, ((char *)&val) + b_offset, b_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010166 len -= b_count;
10167 offset += b_count;
Matt Carlsonc6cdf432010-04-05 10:19:26 +000010168 eeprom->len += b_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010169 }
10170
Lucas De Marchi25985ed2011-03-30 22:57:33 -030010171 /* read bytes up to the last 4 byte boundary */
Linus Torvalds1da177e2005-04-16 15:20:36 -070010172 pd = &data[eeprom->len];
10173 for (i = 0; i < (len - (len & 3)); i += 4) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000010174 ret = tg3_nvram_read_be32(tp, offset + i, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010175 if (ret) {
10176 eeprom->len += i;
10177 return ret;
10178 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010179 memcpy(pd + i, &val, 4);
10180 }
10181 eeprom->len += i;
10182
10183 if (len & 3) {
10184 /* read last bytes not ending on 4 byte boundary */
10185 pd = &data[eeprom->len];
10186 b_count = len & 3;
10187 b_offset = offset + len - b_count;
Matt Carlsona9dc5292009-02-25 14:25:30 +000010188 ret = tg3_nvram_read_be32(tp, b_offset, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010189 if (ret)
10190 return ret;
Al Virob9fc7dc2007-12-17 22:59:57 -080010191 memcpy(pd, &val, b_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010192 eeprom->len += b_count;
10193 }
10194 return 0;
10195}
10196
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010197static int tg3_nvram_write_block(struct tg3 *tp, u32 offset, u32 len, u8 *buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010198
10199static int tg3_set_eeprom(struct net_device *dev, struct ethtool_eeprom *eeprom, u8 *data)
10200{
10201 struct tg3 *tp = netdev_priv(dev);
10202 int ret;
Al Virob9fc7dc2007-12-17 22:59:57 -080010203 u32 offset, len, b_offset, odd_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010204 u8 *buf;
Matt Carlsona9dc5292009-02-25 14:25:30 +000010205 __be32 start, end;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010206
Matt Carlson80096062010-08-02 11:26:06 +000010207 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Michael Chanbc1c7562006-03-20 17:48:03 -080010208 return -EAGAIN;
10209
Joe Perches63c3a662011-04-26 08:12:10 +000010210 if (tg3_flag(tp, NO_NVRAM) ||
Matt Carlsondf259d82009-04-20 06:57:14 +000010211 eeprom->magic != TG3_EEPROM_MAGIC)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010212 return -EINVAL;
10213
10214 offset = eeprom->offset;
10215 len = eeprom->len;
10216
10217 if ((b_offset = (offset & 3))) {
10218 /* adjustments to start on required 4 byte boundary */
Matt Carlsona9dc5292009-02-25 14:25:30 +000010219 ret = tg3_nvram_read_be32(tp, offset-b_offset, &start);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010220 if (ret)
10221 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010222 len += b_offset;
10223 offset &= ~3;
Michael Chan1c8594b42005-04-21 17:12:46 -070010224 if (len < 4)
10225 len = 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010226 }
10227
10228 odd_len = 0;
Michael Chan1c8594b42005-04-21 17:12:46 -070010229 if (len & 3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010230 /* adjustments to end on required 4 byte boundary */
10231 odd_len = 1;
10232 len = (len + 3) & ~3;
Matt Carlsona9dc5292009-02-25 14:25:30 +000010233 ret = tg3_nvram_read_be32(tp, offset+len-4, &end);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010234 if (ret)
10235 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010236 }
10237
10238 buf = data;
10239 if (b_offset || odd_len) {
10240 buf = kmalloc(len, GFP_KERNEL);
Andy Gospodarekab0049b2007-09-06 20:42:14 +010010241 if (!buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010242 return -ENOMEM;
10243 if (b_offset)
10244 memcpy(buf, &start, 4);
10245 if (odd_len)
10246 memcpy(buf+len-4, &end, 4);
10247 memcpy(buf + b_offset, data, eeprom->len);
10248 }
10249
10250 ret = tg3_nvram_write_block(tp, offset, len, buf);
10251
10252 if (buf != data)
10253 kfree(buf);
10254
10255 return ret;
10256}
10257
10258static int tg3_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
10259{
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010260 struct tg3 *tp = netdev_priv(dev);
10261
Joe Perches63c3a662011-04-26 08:12:10 +000010262 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000010263 struct phy_device *phydev;
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010264 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010265 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000010266 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
10267 return phy_ethtool_gset(phydev, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010268 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010269
Linus Torvalds1da177e2005-04-16 15:20:36 -070010270 cmd->supported = (SUPPORTED_Autoneg);
10271
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010272 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010273 cmd->supported |= (SUPPORTED_1000baseT_Half |
10274 SUPPORTED_1000baseT_Full);
10275
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010276 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010277 cmd->supported |= (SUPPORTED_100baseT_Half |
10278 SUPPORTED_100baseT_Full |
10279 SUPPORTED_10baseT_Half |
10280 SUPPORTED_10baseT_Full |
Matt Carlson3bebab52007-11-12 21:22:40 -080010281 SUPPORTED_TP);
Karsten Keilef348142006-05-12 12:49:08 -070010282 cmd->port = PORT_TP;
10283 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010284 cmd->supported |= SUPPORTED_FIBRE;
Karsten Keilef348142006-05-12 12:49:08 -070010285 cmd->port = PORT_FIBRE;
10286 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010287
Linus Torvalds1da177e2005-04-16 15:20:36 -070010288 cmd->advertising = tp->link_config.advertising;
Matt Carlson5bb09772011-06-13 13:39:00 +000010289 if (tg3_flag(tp, PAUSE_AUTONEG)) {
10290 if (tp->link_config.flowctrl & FLOW_CTRL_RX) {
10291 if (tp->link_config.flowctrl & FLOW_CTRL_TX) {
10292 cmd->advertising |= ADVERTISED_Pause;
10293 } else {
10294 cmd->advertising |= ADVERTISED_Pause |
10295 ADVERTISED_Asym_Pause;
10296 }
10297 } else if (tp->link_config.flowctrl & FLOW_CTRL_TX) {
10298 cmd->advertising |= ADVERTISED_Asym_Pause;
10299 }
10300 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010301 if (netif_running(dev)) {
David Decotigny70739492011-04-27 18:32:40 +000010302 ethtool_cmd_speed_set(cmd, tp->link_config.active_speed);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010303 cmd->duplex = tp->link_config.active_duplex;
Matt Carlson64c22182010-10-14 10:37:44 +000010304 } else {
David Decotigny70739492011-04-27 18:32:40 +000010305 ethtool_cmd_speed_set(cmd, SPEED_INVALID);
Matt Carlson64c22182010-10-14 10:37:44 +000010306 cmd->duplex = DUPLEX_INVALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010307 }
Matt Carlson882e9792009-09-01 13:21:36 +000010308 cmd->phy_address = tp->phy_addr;
Matt Carlson7e5856b2009-02-25 14:23:01 +000010309 cmd->transceiver = XCVR_INTERNAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010310 cmd->autoneg = tp->link_config.autoneg;
10311 cmd->maxtxpkt = 0;
10312 cmd->maxrxpkt = 0;
10313 return 0;
10314}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010315
Linus Torvalds1da177e2005-04-16 15:20:36 -070010316static int tg3_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
10317{
10318 struct tg3 *tp = netdev_priv(dev);
David Decotigny25db0332011-04-27 18:32:39 +000010319 u32 speed = ethtool_cmd_speed(cmd);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010320
Joe Perches63c3a662011-04-26 08:12:10 +000010321 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000010322 struct phy_device *phydev;
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010323 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010324 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000010325 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
10326 return phy_ethtool_sset(phydev, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010327 }
10328
Matt Carlson7e5856b2009-02-25 14:23:01 +000010329 if (cmd->autoneg != AUTONEG_ENABLE &&
10330 cmd->autoneg != AUTONEG_DISABLE)
Michael Chan37ff2382005-10-26 15:49:51 -070010331 return -EINVAL;
Matt Carlson7e5856b2009-02-25 14:23:01 +000010332
10333 if (cmd->autoneg == AUTONEG_DISABLE &&
10334 cmd->duplex != DUPLEX_FULL &&
10335 cmd->duplex != DUPLEX_HALF)
Michael Chan37ff2382005-10-26 15:49:51 -070010336 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010337
Matt Carlson7e5856b2009-02-25 14:23:01 +000010338 if (cmd->autoneg == AUTONEG_ENABLE) {
10339 u32 mask = ADVERTISED_Autoneg |
10340 ADVERTISED_Pause |
10341 ADVERTISED_Asym_Pause;
10342
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010343 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
Matt Carlson7e5856b2009-02-25 14:23:01 +000010344 mask |= ADVERTISED_1000baseT_Half |
10345 ADVERTISED_1000baseT_Full;
10346
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010347 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
Matt Carlson7e5856b2009-02-25 14:23:01 +000010348 mask |= ADVERTISED_100baseT_Half |
10349 ADVERTISED_100baseT_Full |
10350 ADVERTISED_10baseT_Half |
10351 ADVERTISED_10baseT_Full |
10352 ADVERTISED_TP;
10353 else
10354 mask |= ADVERTISED_FIBRE;
10355
10356 if (cmd->advertising & ~mask)
10357 return -EINVAL;
10358
10359 mask &= (ADVERTISED_1000baseT_Half |
10360 ADVERTISED_1000baseT_Full |
10361 ADVERTISED_100baseT_Half |
10362 ADVERTISED_100baseT_Full |
10363 ADVERTISED_10baseT_Half |
10364 ADVERTISED_10baseT_Full);
10365
10366 cmd->advertising &= mask;
10367 } else {
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010368 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES) {
David Decotigny25db0332011-04-27 18:32:39 +000010369 if (speed != SPEED_1000)
Matt Carlson7e5856b2009-02-25 14:23:01 +000010370 return -EINVAL;
10371
10372 if (cmd->duplex != DUPLEX_FULL)
10373 return -EINVAL;
10374 } else {
David Decotigny25db0332011-04-27 18:32:39 +000010375 if (speed != SPEED_100 &&
10376 speed != SPEED_10)
Matt Carlson7e5856b2009-02-25 14:23:01 +000010377 return -EINVAL;
10378 }
10379 }
10380
David S. Millerf47c11e2005-06-24 20:18:35 -070010381 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010382
10383 tp->link_config.autoneg = cmd->autoneg;
10384 if (cmd->autoneg == AUTONEG_ENABLE) {
Andy Gospodarek405d8e52007-10-08 01:08:47 -070010385 tp->link_config.advertising = (cmd->advertising |
10386 ADVERTISED_Autoneg);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010387 tp->link_config.speed = SPEED_INVALID;
10388 tp->link_config.duplex = DUPLEX_INVALID;
10389 } else {
10390 tp->link_config.advertising = 0;
David Decotigny25db0332011-04-27 18:32:39 +000010391 tp->link_config.speed = speed;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010392 tp->link_config.duplex = cmd->duplex;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010393 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010394
Michael Chan24fcad62006-12-17 17:06:46 -080010395 tp->link_config.orig_speed = tp->link_config.speed;
10396 tp->link_config.orig_duplex = tp->link_config.duplex;
10397 tp->link_config.orig_autoneg = tp->link_config.autoneg;
10398
Linus Torvalds1da177e2005-04-16 15:20:36 -070010399 if (netif_running(dev))
10400 tg3_setup_phy(tp, 1);
10401
David S. Millerf47c11e2005-06-24 20:18:35 -070010402 tg3_full_unlock(tp);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010403
Linus Torvalds1da177e2005-04-16 15:20:36 -070010404 return 0;
10405}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010406
Linus Torvalds1da177e2005-04-16 15:20:36 -070010407static void tg3_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
10408{
10409 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010410
Rick Jones68aad782011-11-07 13:29:27 +000010411 strlcpy(info->driver, DRV_MODULE_NAME, sizeof(info->driver));
10412 strlcpy(info->version, DRV_MODULE_VERSION, sizeof(info->version));
10413 strlcpy(info->fw_version, tp->fw_ver, sizeof(info->fw_version));
10414 strlcpy(info->bus_info, pci_name(tp->pdev), sizeof(info->bus_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -070010415}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010416
Linus Torvalds1da177e2005-04-16 15:20:36 -070010417static void tg3_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
10418{
10419 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010420
Joe Perches63c3a662011-04-26 08:12:10 +000010421 if (tg3_flag(tp, WOL_CAP) && device_can_wakeup(&tp->pdev->dev))
Gary Zambranoa85feb82007-05-05 11:52:19 -070010422 wol->supported = WAKE_MAGIC;
10423 else
10424 wol->supported = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010425 wol->wolopts = 0;
Joe Perches63c3a662011-04-26 08:12:10 +000010426 if (tg3_flag(tp, WOL_ENABLE) && device_can_wakeup(&tp->pdev->dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010427 wol->wolopts = WAKE_MAGIC;
10428 memset(&wol->sopass, 0, sizeof(wol->sopass));
10429}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010430
Linus Torvalds1da177e2005-04-16 15:20:36 -070010431static int tg3_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
10432{
10433 struct tg3 *tp = netdev_priv(dev);
Rafael J. Wysocki12dac072008-07-30 16:37:33 -070010434 struct device *dp = &tp->pdev->dev;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010435
Linus Torvalds1da177e2005-04-16 15:20:36 -070010436 if (wol->wolopts & ~WAKE_MAGIC)
10437 return -EINVAL;
10438 if ((wol->wolopts & WAKE_MAGIC) &&
Joe Perches63c3a662011-04-26 08:12:10 +000010439 !(tg3_flag(tp, WOL_CAP) && device_can_wakeup(dp)))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010440 return -EINVAL;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010441
Rafael J. Wysockif2dc0d12010-10-25 13:01:55 +000010442 device_set_wakeup_enable(dp, wol->wolopts & WAKE_MAGIC);
10443
David S. Millerf47c11e2005-06-24 20:18:35 -070010444 spin_lock_bh(&tp->lock);
Rafael J. Wysockif2dc0d12010-10-25 13:01:55 +000010445 if (device_may_wakeup(dp))
Joe Perches63c3a662011-04-26 08:12:10 +000010446 tg3_flag_set(tp, WOL_ENABLE);
Rafael J. Wysockif2dc0d12010-10-25 13:01:55 +000010447 else
Joe Perches63c3a662011-04-26 08:12:10 +000010448 tg3_flag_clear(tp, WOL_ENABLE);
David S. Millerf47c11e2005-06-24 20:18:35 -070010449 spin_unlock_bh(&tp->lock);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010450
Linus Torvalds1da177e2005-04-16 15:20:36 -070010451 return 0;
10452}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010453
Linus Torvalds1da177e2005-04-16 15:20:36 -070010454static u32 tg3_get_msglevel(struct net_device *dev)
10455{
10456 struct tg3 *tp = netdev_priv(dev);
10457 return tp->msg_enable;
10458}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010459
Linus Torvalds1da177e2005-04-16 15:20:36 -070010460static void tg3_set_msglevel(struct net_device *dev, u32 value)
10461{
10462 struct tg3 *tp = netdev_priv(dev);
10463 tp->msg_enable = value;
10464}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010465
Linus Torvalds1da177e2005-04-16 15:20:36 -070010466static int tg3_nway_reset(struct net_device *dev)
10467{
10468 struct tg3 *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010469 int r;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010470
Linus Torvalds1da177e2005-04-16 15:20:36 -070010471 if (!netif_running(dev))
10472 return -EAGAIN;
10473
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010474 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Michael Chanc94e3942005-09-27 12:12:42 -070010475 return -EINVAL;
10476
Joe Perches63c3a662011-04-26 08:12:10 +000010477 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010478 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010479 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000010480 r = phy_start_aneg(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010481 } else {
10482 u32 bmcr;
10483
10484 spin_lock_bh(&tp->lock);
10485 r = -EINVAL;
10486 tg3_readphy(tp, MII_BMCR, &bmcr);
10487 if (!tg3_readphy(tp, MII_BMCR, &bmcr) &&
10488 ((bmcr & BMCR_ANENABLE) ||
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010489 (tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT))) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010490 tg3_writephy(tp, MII_BMCR, bmcr | BMCR_ANRESTART |
10491 BMCR_ANENABLE);
10492 r = 0;
10493 }
10494 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010495 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010496
Linus Torvalds1da177e2005-04-16 15:20:36 -070010497 return r;
10498}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010499
Linus Torvalds1da177e2005-04-16 15:20:36 -070010500static void tg3_get_ringparam(struct net_device *dev, struct ethtool_ringparam *ering)
10501{
10502 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010503
Matt Carlson2c49a442010-09-30 10:34:35 +000010504 ering->rx_max_pending = tp->rx_std_ring_mask;
Joe Perches63c3a662011-04-26 08:12:10 +000010505 if (tg3_flag(tp, JUMBO_RING_ENABLE))
Matt Carlson2c49a442010-09-30 10:34:35 +000010506 ering->rx_jumbo_max_pending = tp->rx_jmb_ring_mask;
Michael Chan4f81c322006-03-20 21:33:42 -080010507 else
10508 ering->rx_jumbo_max_pending = 0;
10509
10510 ering->tx_max_pending = TG3_TX_RING_SIZE - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010511
10512 ering->rx_pending = tp->rx_pending;
Joe Perches63c3a662011-04-26 08:12:10 +000010513 if (tg3_flag(tp, JUMBO_RING_ENABLE))
Michael Chan4f81c322006-03-20 21:33:42 -080010514 ering->rx_jumbo_pending = tp->rx_jumbo_pending;
10515 else
10516 ering->rx_jumbo_pending = 0;
10517
Matt Carlsonf3f3f272009-08-28 14:03:21 +000010518 ering->tx_pending = tp->napi[0].tx_pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010519}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010520
Linus Torvalds1da177e2005-04-16 15:20:36 -070010521static int tg3_set_ringparam(struct net_device *dev, struct ethtool_ringparam *ering)
10522{
10523 struct tg3 *tp = netdev_priv(dev);
Matt Carlson646c9ed2009-09-01 12:58:41 +000010524 int i, irq_sync = 0, err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010525
Matt Carlson2c49a442010-09-30 10:34:35 +000010526 if ((ering->rx_pending > tp->rx_std_ring_mask) ||
10527 (ering->rx_jumbo_pending > tp->rx_jmb_ring_mask) ||
Michael Chanbc3a9252006-10-18 20:55:18 -070010528 (ering->tx_pending > TG3_TX_RING_SIZE - 1) ||
10529 (ering->tx_pending <= MAX_SKB_FRAGS) ||
Joe Perches63c3a662011-04-26 08:12:10 +000010530 (tg3_flag(tp, TSO_BUG) &&
Michael Chanbc3a9252006-10-18 20:55:18 -070010531 (ering->tx_pending <= (MAX_SKB_FRAGS * 3))))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010532 return -EINVAL;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010533
Michael Chanbbe832c2005-06-24 20:20:04 -070010534 if (netif_running(dev)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010535 tg3_phy_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010536 tg3_netif_stop(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -070010537 irq_sync = 1;
10538 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010539
Michael Chanbbe832c2005-06-24 20:20:04 -070010540 tg3_full_lock(tp, irq_sync);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010541
Linus Torvalds1da177e2005-04-16 15:20:36 -070010542 tp->rx_pending = ering->rx_pending;
10543
Joe Perches63c3a662011-04-26 08:12:10 +000010544 if (tg3_flag(tp, MAX_RXPEND_64) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070010545 tp->rx_pending > 63)
10546 tp->rx_pending = 63;
10547 tp->rx_jumbo_pending = ering->rx_jumbo_pending;
Matt Carlson646c9ed2009-09-01 12:58:41 +000010548
Matt Carlson6fd45cb2010-09-15 08:59:57 +000010549 for (i = 0; i < tp->irq_max; i++)
Matt Carlson646c9ed2009-09-01 12:58:41 +000010550 tp->napi[i].tx_pending = ering->tx_pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010551
10552 if (netif_running(dev)) {
Michael Chan944d9802005-05-29 14:57:48 -070010553 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Michael Chanb9ec6c12006-07-25 16:37:27 -070010554 err = tg3_restart_hw(tp, 1);
10555 if (!err)
10556 tg3_netif_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010557 }
10558
David S. Millerf47c11e2005-06-24 20:18:35 -070010559 tg3_full_unlock(tp);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010560
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010561 if (irq_sync && !err)
10562 tg3_phy_start(tp);
10563
Michael Chanb9ec6c12006-07-25 16:37:27 -070010564 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010565}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010566
Linus Torvalds1da177e2005-04-16 15:20:36 -070010567static void tg3_get_pauseparam(struct net_device *dev, struct ethtool_pauseparam *epause)
10568{
10569 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010570
Joe Perches63c3a662011-04-26 08:12:10 +000010571 epause->autoneg = !!tg3_flag(tp, PAUSE_AUTONEG);
Matt Carlson8d018622007-12-20 20:05:44 -080010572
Steve Glendinninge18ce342008-12-16 02:00:00 -080010573 if (tp->link_config.active_flowctrl & FLOW_CTRL_RX)
Matt Carlson8d018622007-12-20 20:05:44 -080010574 epause->rx_pause = 1;
10575 else
10576 epause->rx_pause = 0;
10577
Steve Glendinninge18ce342008-12-16 02:00:00 -080010578 if (tp->link_config.active_flowctrl & FLOW_CTRL_TX)
Matt Carlson8d018622007-12-20 20:05:44 -080010579 epause->tx_pause = 1;
10580 else
10581 epause->tx_pause = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010582}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010583
Linus Torvalds1da177e2005-04-16 15:20:36 -070010584static int tg3_set_pauseparam(struct net_device *dev, struct ethtool_pauseparam *epause)
10585{
10586 struct tg3 *tp = netdev_priv(dev);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010587 int err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010588
Joe Perches63c3a662011-04-26 08:12:10 +000010589 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson27121682010-02-17 15:16:57 +000010590 u32 newadv;
10591 struct phy_device *phydev;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010592
Matt Carlson27121682010-02-17 15:16:57 +000010593 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010594
Matt Carlson27121682010-02-17 15:16:57 +000010595 if (!(phydev->supported & SUPPORTED_Pause) ||
10596 (!(phydev->supported & SUPPORTED_Asym_Pause) &&
Nicolas Kaiser2259dca2010-10-07 23:29:27 +000010597 (epause->rx_pause != epause->tx_pause)))
Matt Carlson27121682010-02-17 15:16:57 +000010598 return -EINVAL;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010599
Matt Carlson27121682010-02-17 15:16:57 +000010600 tp->link_config.flowctrl = 0;
10601 if (epause->rx_pause) {
10602 tp->link_config.flowctrl |= FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010603
Matt Carlson27121682010-02-17 15:16:57 +000010604 if (epause->tx_pause) {
Steve Glendinninge18ce342008-12-16 02:00:00 -080010605 tp->link_config.flowctrl |= FLOW_CTRL_TX;
Matt Carlson27121682010-02-17 15:16:57 +000010606 newadv = ADVERTISED_Pause;
10607 } else
10608 newadv = ADVERTISED_Pause |
10609 ADVERTISED_Asym_Pause;
10610 } else if (epause->tx_pause) {
10611 tp->link_config.flowctrl |= FLOW_CTRL_TX;
10612 newadv = ADVERTISED_Asym_Pause;
10613 } else
10614 newadv = 0;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010615
Matt Carlson27121682010-02-17 15:16:57 +000010616 if (epause->autoneg)
Joe Perches63c3a662011-04-26 08:12:10 +000010617 tg3_flag_set(tp, PAUSE_AUTONEG);
Matt Carlson27121682010-02-17 15:16:57 +000010618 else
Joe Perches63c3a662011-04-26 08:12:10 +000010619 tg3_flag_clear(tp, PAUSE_AUTONEG);
Matt Carlson27121682010-02-17 15:16:57 +000010620
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010621 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) {
Matt Carlson27121682010-02-17 15:16:57 +000010622 u32 oldadv = phydev->advertising &
10623 (ADVERTISED_Pause | ADVERTISED_Asym_Pause);
10624 if (oldadv != newadv) {
10625 phydev->advertising &=
10626 ~(ADVERTISED_Pause |
10627 ADVERTISED_Asym_Pause);
10628 phydev->advertising |= newadv;
10629 if (phydev->autoneg) {
10630 /*
10631 * Always renegotiate the link to
10632 * inform our link partner of our
10633 * flow control settings, even if the
10634 * flow control is forced. Let
10635 * tg3_adjust_link() do the final
10636 * flow control setup.
10637 */
10638 return phy_start_aneg(phydev);
10639 }
10640 }
10641
10642 if (!epause->autoneg)
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010643 tg3_setup_flow_control(tp, 0, 0);
Matt Carlson27121682010-02-17 15:16:57 +000010644 } else {
10645 tp->link_config.orig_advertising &=
10646 ~(ADVERTISED_Pause |
10647 ADVERTISED_Asym_Pause);
10648 tp->link_config.orig_advertising |= newadv;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010649 }
10650 } else {
10651 int irq_sync = 0;
10652
10653 if (netif_running(dev)) {
10654 tg3_netif_stop(tp);
10655 irq_sync = 1;
10656 }
10657
10658 tg3_full_lock(tp, irq_sync);
10659
10660 if (epause->autoneg)
Joe Perches63c3a662011-04-26 08:12:10 +000010661 tg3_flag_set(tp, PAUSE_AUTONEG);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010662 else
Joe Perches63c3a662011-04-26 08:12:10 +000010663 tg3_flag_clear(tp, PAUSE_AUTONEG);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010664 if (epause->rx_pause)
Steve Glendinninge18ce342008-12-16 02:00:00 -080010665 tp->link_config.flowctrl |= FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010666 else
Steve Glendinninge18ce342008-12-16 02:00:00 -080010667 tp->link_config.flowctrl &= ~FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010668 if (epause->tx_pause)
Steve Glendinninge18ce342008-12-16 02:00:00 -080010669 tp->link_config.flowctrl |= FLOW_CTRL_TX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010670 else
Steve Glendinninge18ce342008-12-16 02:00:00 -080010671 tp->link_config.flowctrl &= ~FLOW_CTRL_TX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010672
10673 if (netif_running(dev)) {
10674 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
10675 err = tg3_restart_hw(tp, 1);
10676 if (!err)
10677 tg3_netif_start(tp);
10678 }
10679
10680 tg3_full_unlock(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -070010681 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010682
Michael Chanb9ec6c12006-07-25 16:37:27 -070010683 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010684}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010685
Matt Carlsonde6f31e2010-04-12 06:58:30 +000010686static int tg3_get_sset_count(struct net_device *dev, int sset)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010687{
Jeff Garzikb9f2c042007-10-03 18:07:32 -070010688 switch (sset) {
10689 case ETH_SS_TEST:
10690 return TG3_NUM_TEST;
10691 case ETH_SS_STATS:
10692 return TG3_NUM_STATS;
10693 default:
10694 return -EOPNOTSUPP;
10695 }
Michael Chan4cafd3f2005-05-29 14:56:34 -070010696}
10697
Matt Carlsonde6f31e2010-04-12 06:58:30 +000010698static void tg3_get_strings(struct net_device *dev, u32 stringset, u8 *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010699{
10700 switch (stringset) {
10701 case ETH_SS_STATS:
10702 memcpy(buf, &ethtool_stats_keys, sizeof(ethtool_stats_keys));
10703 break;
Michael Chan4cafd3f2005-05-29 14:56:34 -070010704 case ETH_SS_TEST:
10705 memcpy(buf, &ethtool_test_keys, sizeof(ethtool_test_keys));
10706 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010707 default:
10708 WARN_ON(1); /* we need a WARN() */
10709 break;
10710 }
10711}
10712
stephen hemminger81b87092011-04-04 08:43:50 +000010713static int tg3_set_phys_id(struct net_device *dev,
10714 enum ethtool_phys_id_state state)
Michael Chan4009a932005-09-05 17:52:54 -070010715{
10716 struct tg3 *tp = netdev_priv(dev);
Michael Chan4009a932005-09-05 17:52:54 -070010717
10718 if (!netif_running(tp->dev))
10719 return -EAGAIN;
10720
stephen hemminger81b87092011-04-04 08:43:50 +000010721 switch (state) {
10722 case ETHTOOL_ID_ACTIVE:
Allan, Bruce Wfce55922011-04-13 13:09:10 +000010723 return 1; /* cycle on/off once per second */
Michael Chan4009a932005-09-05 17:52:54 -070010724
stephen hemminger81b87092011-04-04 08:43:50 +000010725 case ETHTOOL_ID_ON:
10726 tw32(MAC_LED_CTRL, LED_CTRL_LNKLED_OVERRIDE |
10727 LED_CTRL_1000MBPS_ON |
10728 LED_CTRL_100MBPS_ON |
10729 LED_CTRL_10MBPS_ON |
10730 LED_CTRL_TRAFFIC_OVERRIDE |
10731 LED_CTRL_TRAFFIC_BLINK |
10732 LED_CTRL_TRAFFIC_LED);
10733 break;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010734
stephen hemminger81b87092011-04-04 08:43:50 +000010735 case ETHTOOL_ID_OFF:
10736 tw32(MAC_LED_CTRL, LED_CTRL_LNKLED_OVERRIDE |
10737 LED_CTRL_TRAFFIC_OVERRIDE);
10738 break;
Michael Chan4009a932005-09-05 17:52:54 -070010739
stephen hemminger81b87092011-04-04 08:43:50 +000010740 case ETHTOOL_ID_INACTIVE:
10741 tw32(MAC_LED_CTRL, tp->led_ctrl);
10742 break;
Michael Chan4009a932005-09-05 17:52:54 -070010743 }
stephen hemminger81b87092011-04-04 08:43:50 +000010744
Michael Chan4009a932005-09-05 17:52:54 -070010745 return 0;
10746}
10747
Matt Carlsonde6f31e2010-04-12 06:58:30 +000010748static void tg3_get_ethtool_stats(struct net_device *dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010749 struct ethtool_stats *estats, u64 *tmp_stats)
10750{
10751 struct tg3 *tp = netdev_priv(dev);
10752 memcpy(tmp_stats, tg3_get_estats(tp), sizeof(tp->estats));
10753}
10754
Matt Carlson535a4902011-07-20 10:20:56 +000010755static __be32 *tg3_vpd_readblock(struct tg3 *tp, u32 *vpdlen)
Matt Carlsonc3e94502011-04-13 11:05:08 +000010756{
10757 int i;
10758 __be32 *buf;
10759 u32 offset = 0, len = 0;
10760 u32 magic, val;
10761
Joe Perches63c3a662011-04-26 08:12:10 +000010762 if (tg3_flag(tp, NO_NVRAM) || tg3_nvram_read(tp, 0, &magic))
Matt Carlsonc3e94502011-04-13 11:05:08 +000010763 return NULL;
10764
10765 if (magic == TG3_EEPROM_MAGIC) {
10766 for (offset = TG3_NVM_DIR_START;
10767 offset < TG3_NVM_DIR_END;
10768 offset += TG3_NVM_DIRENT_SIZE) {
10769 if (tg3_nvram_read(tp, offset, &val))
10770 return NULL;
10771
10772 if ((val >> TG3_NVM_DIRTYPE_SHIFT) ==
10773 TG3_NVM_DIRTYPE_EXTVPD)
10774 break;
10775 }
10776
10777 if (offset != TG3_NVM_DIR_END) {
10778 len = (val & TG3_NVM_DIRTYPE_LENMSK) * 4;
10779 if (tg3_nvram_read(tp, offset + 4, &offset))
10780 return NULL;
10781
10782 offset = tg3_nvram_logical_addr(tp, offset);
10783 }
10784 }
10785
10786 if (!offset || !len) {
10787 offset = TG3_NVM_VPD_OFF;
10788 len = TG3_NVM_VPD_LEN;
10789 }
10790
10791 buf = kmalloc(len, GFP_KERNEL);
10792 if (buf == NULL)
10793 return NULL;
10794
10795 if (magic == TG3_EEPROM_MAGIC) {
10796 for (i = 0; i < len; i += 4) {
10797 /* The data is in little-endian format in NVRAM.
10798 * Use the big-endian read routines to preserve
10799 * the byte order as it exists in NVRAM.
10800 */
10801 if (tg3_nvram_read_be32(tp, offset + i, &buf[i/4]))
10802 goto error;
10803 }
10804 } else {
10805 u8 *ptr;
10806 ssize_t cnt;
10807 unsigned int pos = 0;
10808
10809 ptr = (u8 *)&buf[0];
10810 for (i = 0; pos < len && i < 3; i++, pos += cnt, ptr += cnt) {
10811 cnt = pci_read_vpd(tp->pdev, pos,
10812 len - pos, ptr);
10813 if (cnt == -ETIMEDOUT || cnt == -EINTR)
10814 cnt = 0;
10815 else if (cnt < 0)
10816 goto error;
10817 }
10818 if (pos != len)
10819 goto error;
10820 }
10821
Matt Carlson535a4902011-07-20 10:20:56 +000010822 *vpdlen = len;
10823
Matt Carlsonc3e94502011-04-13 11:05:08 +000010824 return buf;
10825
10826error:
10827 kfree(buf);
10828 return NULL;
10829}
10830
Michael Chan566f86a2005-05-29 14:56:58 -070010831#define NVRAM_TEST_SIZE 0x100
Matt Carlsona5767de2007-11-12 21:10:58 -080010832#define NVRAM_SELFBOOT_FORMAT1_0_SIZE 0x14
10833#define NVRAM_SELFBOOT_FORMAT1_2_SIZE 0x18
10834#define NVRAM_SELFBOOT_FORMAT1_3_SIZE 0x1c
Matt Carlson727a6d92011-06-13 13:38:58 +000010835#define NVRAM_SELFBOOT_FORMAT1_4_SIZE 0x20
10836#define NVRAM_SELFBOOT_FORMAT1_5_SIZE 0x24
Matt Carlsonbda18fa2011-07-20 10:20:57 +000010837#define NVRAM_SELFBOOT_FORMAT1_6_SIZE 0x50
Michael Chanb16250e2006-09-27 16:10:14 -070010838#define NVRAM_SELFBOOT_HW_SIZE 0x20
10839#define NVRAM_SELFBOOT_DATA_SIZE 0x1c
Michael Chan566f86a2005-05-29 14:56:58 -070010840
10841static int tg3_test_nvram(struct tg3 *tp)
10842{
Matt Carlson535a4902011-07-20 10:20:56 +000010843 u32 csum, magic, len;
Matt Carlsona9dc5292009-02-25 14:25:30 +000010844 __be32 *buf;
Andy Gospodarekab0049b2007-09-06 20:42:14 +010010845 int i, j, k, err = 0, size;
Michael Chan566f86a2005-05-29 14:56:58 -070010846
Joe Perches63c3a662011-04-26 08:12:10 +000010847 if (tg3_flag(tp, NO_NVRAM))
Matt Carlsondf259d82009-04-20 06:57:14 +000010848 return 0;
10849
Matt Carlsone4f34112009-02-25 14:25:00 +000010850 if (tg3_nvram_read(tp, 0, &magic) != 0)
Michael Chan1b277772006-03-20 22:27:48 -080010851 return -EIO;
10852
Michael Chan1b277772006-03-20 22:27:48 -080010853 if (magic == TG3_EEPROM_MAGIC)
10854 size = NVRAM_TEST_SIZE;
Michael Chanb16250e2006-09-27 16:10:14 -070010855 else if ((magic & TG3_EEPROM_MAGIC_FW_MSK) == TG3_EEPROM_MAGIC_FW) {
Matt Carlsona5767de2007-11-12 21:10:58 -080010856 if ((magic & TG3_EEPROM_SB_FORMAT_MASK) ==
10857 TG3_EEPROM_SB_FORMAT_1) {
10858 switch (magic & TG3_EEPROM_SB_REVISION_MASK) {
10859 case TG3_EEPROM_SB_REVISION_0:
10860 size = NVRAM_SELFBOOT_FORMAT1_0_SIZE;
10861 break;
10862 case TG3_EEPROM_SB_REVISION_2:
10863 size = NVRAM_SELFBOOT_FORMAT1_2_SIZE;
10864 break;
10865 case TG3_EEPROM_SB_REVISION_3:
10866 size = NVRAM_SELFBOOT_FORMAT1_3_SIZE;
10867 break;
Matt Carlson727a6d92011-06-13 13:38:58 +000010868 case TG3_EEPROM_SB_REVISION_4:
10869 size = NVRAM_SELFBOOT_FORMAT1_4_SIZE;
10870 break;
10871 case TG3_EEPROM_SB_REVISION_5:
10872 size = NVRAM_SELFBOOT_FORMAT1_5_SIZE;
10873 break;
10874 case TG3_EEPROM_SB_REVISION_6:
10875 size = NVRAM_SELFBOOT_FORMAT1_6_SIZE;
10876 break;
Matt Carlsona5767de2007-11-12 21:10:58 -080010877 default:
Matt Carlson727a6d92011-06-13 13:38:58 +000010878 return -EIO;
Matt Carlsona5767de2007-11-12 21:10:58 -080010879 }
10880 } else
Michael Chan1b277772006-03-20 22:27:48 -080010881 return 0;
Michael Chanb16250e2006-09-27 16:10:14 -070010882 } else if ((magic & TG3_EEPROM_MAGIC_HW_MSK) == TG3_EEPROM_MAGIC_HW)
10883 size = NVRAM_SELFBOOT_HW_SIZE;
10884 else
Michael Chan1b277772006-03-20 22:27:48 -080010885 return -EIO;
10886
10887 buf = kmalloc(size, GFP_KERNEL);
Michael Chan566f86a2005-05-29 14:56:58 -070010888 if (buf == NULL)
10889 return -ENOMEM;
10890
Michael Chan1b277772006-03-20 22:27:48 -080010891 err = -EIO;
10892 for (i = 0, j = 0; i < size; i += 4, j++) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000010893 err = tg3_nvram_read_be32(tp, i, &buf[j]);
10894 if (err)
Michael Chan566f86a2005-05-29 14:56:58 -070010895 break;
Michael Chan566f86a2005-05-29 14:56:58 -070010896 }
Michael Chan1b277772006-03-20 22:27:48 -080010897 if (i < size)
Michael Chan566f86a2005-05-29 14:56:58 -070010898 goto out;
10899
Michael Chan1b277772006-03-20 22:27:48 -080010900 /* Selfboot format */
Matt Carlsona9dc5292009-02-25 14:25:30 +000010901 magic = be32_to_cpu(buf[0]);
Al Virob9fc7dc2007-12-17 22:59:57 -080010902 if ((magic & TG3_EEPROM_MAGIC_FW_MSK) ==
Michael Chanb16250e2006-09-27 16:10:14 -070010903 TG3_EEPROM_MAGIC_FW) {
Michael Chan1b277772006-03-20 22:27:48 -080010904 u8 *buf8 = (u8 *) buf, csum8 = 0;
10905
Al Virob9fc7dc2007-12-17 22:59:57 -080010906 if ((magic & TG3_EEPROM_SB_REVISION_MASK) ==
Matt Carlsona5767de2007-11-12 21:10:58 -080010907 TG3_EEPROM_SB_REVISION_2) {
10908 /* For rev 2, the csum doesn't include the MBA. */
10909 for (i = 0; i < TG3_EEPROM_SB_F1R2_MBA_OFF; i++)
10910 csum8 += buf8[i];
10911 for (i = TG3_EEPROM_SB_F1R2_MBA_OFF + 4; i < size; i++)
10912 csum8 += buf8[i];
10913 } else {
10914 for (i = 0; i < size; i++)
10915 csum8 += buf8[i];
10916 }
Michael Chan1b277772006-03-20 22:27:48 -080010917
Adrian Bunkad96b482006-04-05 22:21:04 -070010918 if (csum8 == 0) {
10919 err = 0;
10920 goto out;
10921 }
10922
10923 err = -EIO;
10924 goto out;
Michael Chan1b277772006-03-20 22:27:48 -080010925 }
Michael Chan566f86a2005-05-29 14:56:58 -070010926
Al Virob9fc7dc2007-12-17 22:59:57 -080010927 if ((magic & TG3_EEPROM_MAGIC_HW_MSK) ==
Michael Chanb16250e2006-09-27 16:10:14 -070010928 TG3_EEPROM_MAGIC_HW) {
10929 u8 data[NVRAM_SELFBOOT_DATA_SIZE];
Matt Carlsona9dc5292009-02-25 14:25:30 +000010930 u8 parity[NVRAM_SELFBOOT_DATA_SIZE];
Michael Chanb16250e2006-09-27 16:10:14 -070010931 u8 *buf8 = (u8 *) buf;
Michael Chanb16250e2006-09-27 16:10:14 -070010932
10933 /* Separate the parity bits and the data bytes. */
10934 for (i = 0, j = 0, k = 0; i < NVRAM_SELFBOOT_HW_SIZE; i++) {
10935 if ((i == 0) || (i == 8)) {
10936 int l;
10937 u8 msk;
10938
10939 for (l = 0, msk = 0x80; l < 7; l++, msk >>= 1)
10940 parity[k++] = buf8[i] & msk;
10941 i++;
Matt Carlson859a588792010-04-05 10:19:28 +000010942 } else if (i == 16) {
Michael Chanb16250e2006-09-27 16:10:14 -070010943 int l;
10944 u8 msk;
10945
10946 for (l = 0, msk = 0x20; l < 6; l++, msk >>= 1)
10947 parity[k++] = buf8[i] & msk;
10948 i++;
10949
10950 for (l = 0, msk = 0x80; l < 8; l++, msk >>= 1)
10951 parity[k++] = buf8[i] & msk;
10952 i++;
10953 }
10954 data[j++] = buf8[i];
10955 }
10956
10957 err = -EIO;
10958 for (i = 0; i < NVRAM_SELFBOOT_DATA_SIZE; i++) {
10959 u8 hw8 = hweight8(data[i]);
10960
10961 if ((hw8 & 0x1) && parity[i])
10962 goto out;
10963 else if (!(hw8 & 0x1) && !parity[i])
10964 goto out;
10965 }
10966 err = 0;
10967 goto out;
10968 }
10969
Matt Carlson01c3a392011-03-09 16:58:20 +000010970 err = -EIO;
10971
Michael Chan566f86a2005-05-29 14:56:58 -070010972 /* Bootstrap checksum at offset 0x10 */
10973 csum = calc_crc((unsigned char *) buf, 0x10);
Matt Carlson01c3a392011-03-09 16:58:20 +000010974 if (csum != le32_to_cpu(buf[0x10/4]))
Michael Chan566f86a2005-05-29 14:56:58 -070010975 goto out;
10976
10977 /* Manufacturing block starts at offset 0x74, checksum at 0xfc */
10978 csum = calc_crc((unsigned char *) &buf[0x74/4], 0x88);
Matt Carlson01c3a392011-03-09 16:58:20 +000010979 if (csum != le32_to_cpu(buf[0xfc/4]))
Matt Carlsona9dc5292009-02-25 14:25:30 +000010980 goto out;
Michael Chan566f86a2005-05-29 14:56:58 -070010981
Matt Carlsonc3e94502011-04-13 11:05:08 +000010982 kfree(buf);
10983
Matt Carlson535a4902011-07-20 10:20:56 +000010984 buf = tg3_vpd_readblock(tp, &len);
Matt Carlsonc3e94502011-04-13 11:05:08 +000010985 if (!buf)
10986 return -ENOMEM;
Matt Carlsond4894f32011-03-09 16:58:21 +000010987
Matt Carlson535a4902011-07-20 10:20:56 +000010988 i = pci_vpd_find_tag((u8 *)buf, 0, len, PCI_VPD_LRDT_RO_DATA);
Matt Carlsond4894f32011-03-09 16:58:21 +000010989 if (i > 0) {
10990 j = pci_vpd_lrdt_size(&((u8 *)buf)[i]);
10991 if (j < 0)
10992 goto out;
10993
Matt Carlson535a4902011-07-20 10:20:56 +000010994 if (i + PCI_VPD_LRDT_TAG_SIZE + j > len)
Matt Carlsond4894f32011-03-09 16:58:21 +000010995 goto out;
10996
10997 i += PCI_VPD_LRDT_TAG_SIZE;
10998 j = pci_vpd_find_info_keyword((u8 *)buf, i, j,
10999 PCI_VPD_RO_KEYWORD_CHKSUM);
11000 if (j > 0) {
11001 u8 csum8 = 0;
11002
11003 j += PCI_VPD_INFO_FLD_HDR_SIZE;
11004
11005 for (i = 0; i <= j; i++)
11006 csum8 += ((u8 *)buf)[i];
11007
11008 if (csum8)
11009 goto out;
11010 }
11011 }
11012
Michael Chan566f86a2005-05-29 14:56:58 -070011013 err = 0;
11014
11015out:
11016 kfree(buf);
11017 return err;
11018}
11019
Michael Chanca430072005-05-29 14:57:23 -070011020#define TG3_SERDES_TIMEOUT_SEC 2
11021#define TG3_COPPER_TIMEOUT_SEC 6
11022
11023static int tg3_test_link(struct tg3 *tp)
11024{
11025 int i, max;
11026
11027 if (!netif_running(tp->dev))
11028 return -ENODEV;
11029
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011030 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
Michael Chanca430072005-05-29 14:57:23 -070011031 max = TG3_SERDES_TIMEOUT_SEC;
11032 else
11033 max = TG3_COPPER_TIMEOUT_SEC;
11034
11035 for (i = 0; i < max; i++) {
11036 if (netif_carrier_ok(tp->dev))
11037 return 0;
11038
11039 if (msleep_interruptible(1000))
11040 break;
11041 }
11042
11043 return -EIO;
11044}
11045
Michael Chana71116d2005-05-29 14:58:11 -070011046/* Only test the commonly used registers */
David S. Miller30ca3e32006-03-20 23:02:36 -080011047static int tg3_test_registers(struct tg3 *tp)
Michael Chana71116d2005-05-29 14:58:11 -070011048{
Michael Chanb16250e2006-09-27 16:10:14 -070011049 int i, is_5705, is_5750;
Michael Chana71116d2005-05-29 14:58:11 -070011050 u32 offset, read_mask, write_mask, val, save_val, read_val;
11051 static struct {
11052 u16 offset;
11053 u16 flags;
11054#define TG3_FL_5705 0x1
11055#define TG3_FL_NOT_5705 0x2
11056#define TG3_FL_NOT_5788 0x4
Michael Chanb16250e2006-09-27 16:10:14 -070011057#define TG3_FL_NOT_5750 0x8
Michael Chana71116d2005-05-29 14:58:11 -070011058 u32 read_mask;
11059 u32 write_mask;
11060 } reg_tbl[] = {
11061 /* MAC Control Registers */
11062 { MAC_MODE, TG3_FL_NOT_5705,
11063 0x00000000, 0x00ef6f8c },
11064 { MAC_MODE, TG3_FL_5705,
11065 0x00000000, 0x01ef6b8c },
11066 { MAC_STATUS, TG3_FL_NOT_5705,
11067 0x03800107, 0x00000000 },
11068 { MAC_STATUS, TG3_FL_5705,
11069 0x03800100, 0x00000000 },
11070 { MAC_ADDR_0_HIGH, 0x0000,
11071 0x00000000, 0x0000ffff },
11072 { MAC_ADDR_0_LOW, 0x0000,
Matt Carlsonc6cdf432010-04-05 10:19:26 +000011073 0x00000000, 0xffffffff },
Michael Chana71116d2005-05-29 14:58:11 -070011074 { MAC_RX_MTU_SIZE, 0x0000,
11075 0x00000000, 0x0000ffff },
11076 { MAC_TX_MODE, 0x0000,
11077 0x00000000, 0x00000070 },
11078 { MAC_TX_LENGTHS, 0x0000,
11079 0x00000000, 0x00003fff },
11080 { MAC_RX_MODE, TG3_FL_NOT_5705,
11081 0x00000000, 0x000007fc },
11082 { MAC_RX_MODE, TG3_FL_5705,
11083 0x00000000, 0x000007dc },
11084 { MAC_HASH_REG_0, 0x0000,
11085 0x00000000, 0xffffffff },
11086 { MAC_HASH_REG_1, 0x0000,
11087 0x00000000, 0xffffffff },
11088 { MAC_HASH_REG_2, 0x0000,
11089 0x00000000, 0xffffffff },
11090 { MAC_HASH_REG_3, 0x0000,
11091 0x00000000, 0xffffffff },
11092
11093 /* Receive Data and Receive BD Initiator Control Registers. */
11094 { RCVDBDI_JUMBO_BD+0, TG3_FL_NOT_5705,
11095 0x00000000, 0xffffffff },
11096 { RCVDBDI_JUMBO_BD+4, TG3_FL_NOT_5705,
11097 0x00000000, 0xffffffff },
11098 { RCVDBDI_JUMBO_BD+8, TG3_FL_NOT_5705,
11099 0x00000000, 0x00000003 },
11100 { RCVDBDI_JUMBO_BD+0xc, TG3_FL_NOT_5705,
11101 0x00000000, 0xffffffff },
11102 { RCVDBDI_STD_BD+0, 0x0000,
11103 0x00000000, 0xffffffff },
11104 { RCVDBDI_STD_BD+4, 0x0000,
11105 0x00000000, 0xffffffff },
11106 { RCVDBDI_STD_BD+8, 0x0000,
11107 0x00000000, 0xffff0002 },
11108 { RCVDBDI_STD_BD+0xc, 0x0000,
11109 0x00000000, 0xffffffff },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011110
Michael Chana71116d2005-05-29 14:58:11 -070011111 /* Receive BD Initiator Control Registers. */
11112 { RCVBDI_STD_THRESH, TG3_FL_NOT_5705,
11113 0x00000000, 0xffffffff },
11114 { RCVBDI_STD_THRESH, TG3_FL_5705,
11115 0x00000000, 0x000003ff },
11116 { RCVBDI_JUMBO_THRESH, TG3_FL_NOT_5705,
11117 0x00000000, 0xffffffff },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011118
Michael Chana71116d2005-05-29 14:58:11 -070011119 /* Host Coalescing Control Registers. */
11120 { HOSTCC_MODE, TG3_FL_NOT_5705,
11121 0x00000000, 0x00000004 },
11122 { HOSTCC_MODE, TG3_FL_5705,
11123 0x00000000, 0x000000f6 },
11124 { HOSTCC_RXCOL_TICKS, TG3_FL_NOT_5705,
11125 0x00000000, 0xffffffff },
11126 { HOSTCC_RXCOL_TICKS, TG3_FL_5705,
11127 0x00000000, 0x000003ff },
11128 { HOSTCC_TXCOL_TICKS, TG3_FL_NOT_5705,
11129 0x00000000, 0xffffffff },
11130 { HOSTCC_TXCOL_TICKS, TG3_FL_5705,
11131 0x00000000, 0x000003ff },
11132 { HOSTCC_RXMAX_FRAMES, TG3_FL_NOT_5705,
11133 0x00000000, 0xffffffff },
11134 { HOSTCC_RXMAX_FRAMES, TG3_FL_5705 | TG3_FL_NOT_5788,
11135 0x00000000, 0x000000ff },
11136 { HOSTCC_TXMAX_FRAMES, TG3_FL_NOT_5705,
11137 0x00000000, 0xffffffff },
11138 { HOSTCC_TXMAX_FRAMES, TG3_FL_5705 | TG3_FL_NOT_5788,
11139 0x00000000, 0x000000ff },
11140 { HOSTCC_RXCOAL_TICK_INT, TG3_FL_NOT_5705,
11141 0x00000000, 0xffffffff },
11142 { HOSTCC_TXCOAL_TICK_INT, TG3_FL_NOT_5705,
11143 0x00000000, 0xffffffff },
11144 { HOSTCC_RXCOAL_MAXF_INT, TG3_FL_NOT_5705,
11145 0x00000000, 0xffffffff },
11146 { HOSTCC_RXCOAL_MAXF_INT, TG3_FL_5705 | TG3_FL_NOT_5788,
11147 0x00000000, 0x000000ff },
11148 { HOSTCC_TXCOAL_MAXF_INT, TG3_FL_NOT_5705,
11149 0x00000000, 0xffffffff },
11150 { HOSTCC_TXCOAL_MAXF_INT, TG3_FL_5705 | TG3_FL_NOT_5788,
11151 0x00000000, 0x000000ff },
11152 { HOSTCC_STAT_COAL_TICKS, TG3_FL_NOT_5705,
11153 0x00000000, 0xffffffff },
11154 { HOSTCC_STATS_BLK_HOST_ADDR, TG3_FL_NOT_5705,
11155 0x00000000, 0xffffffff },
11156 { HOSTCC_STATS_BLK_HOST_ADDR+4, TG3_FL_NOT_5705,
11157 0x00000000, 0xffffffff },
11158 { HOSTCC_STATUS_BLK_HOST_ADDR, 0x0000,
11159 0x00000000, 0xffffffff },
11160 { HOSTCC_STATUS_BLK_HOST_ADDR+4, 0x0000,
11161 0x00000000, 0xffffffff },
11162 { HOSTCC_STATS_BLK_NIC_ADDR, 0x0000,
11163 0xffffffff, 0x00000000 },
11164 { HOSTCC_STATUS_BLK_NIC_ADDR, 0x0000,
11165 0xffffffff, 0x00000000 },
11166
11167 /* Buffer Manager Control Registers. */
Michael Chanb16250e2006-09-27 16:10:14 -070011168 { BUFMGR_MB_POOL_ADDR, TG3_FL_NOT_5750,
Michael Chana71116d2005-05-29 14:58:11 -070011169 0x00000000, 0x007fff80 },
Michael Chanb16250e2006-09-27 16:10:14 -070011170 { BUFMGR_MB_POOL_SIZE, TG3_FL_NOT_5750,
Michael Chana71116d2005-05-29 14:58:11 -070011171 0x00000000, 0x007fffff },
11172 { BUFMGR_MB_RDMA_LOW_WATER, 0x0000,
11173 0x00000000, 0x0000003f },
11174 { BUFMGR_MB_MACRX_LOW_WATER, 0x0000,
11175 0x00000000, 0x000001ff },
11176 { BUFMGR_MB_HIGH_WATER, 0x0000,
11177 0x00000000, 0x000001ff },
11178 { BUFMGR_DMA_DESC_POOL_ADDR, TG3_FL_NOT_5705,
11179 0xffffffff, 0x00000000 },
11180 { BUFMGR_DMA_DESC_POOL_SIZE, TG3_FL_NOT_5705,
11181 0xffffffff, 0x00000000 },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011182
Michael Chana71116d2005-05-29 14:58:11 -070011183 /* Mailbox Registers */
11184 { GRCMBOX_RCVSTD_PROD_IDX+4, 0x0000,
11185 0x00000000, 0x000001ff },
11186 { GRCMBOX_RCVJUMBO_PROD_IDX+4, TG3_FL_NOT_5705,
11187 0x00000000, 0x000001ff },
11188 { GRCMBOX_RCVRET_CON_IDX_0+4, 0x0000,
11189 0x00000000, 0x000007ff },
11190 { GRCMBOX_SNDHOST_PROD_IDX_0+4, 0x0000,
11191 0x00000000, 0x000001ff },
11192
11193 { 0xffff, 0x0000, 0x00000000, 0x00000000 },
11194 };
11195
Michael Chanb16250e2006-09-27 16:10:14 -070011196 is_5705 = is_5750 = 0;
Joe Perches63c3a662011-04-26 08:12:10 +000011197 if (tg3_flag(tp, 5705_PLUS)) {
Michael Chana71116d2005-05-29 14:58:11 -070011198 is_5705 = 1;
Joe Perches63c3a662011-04-26 08:12:10 +000011199 if (tg3_flag(tp, 5750_PLUS))
Michael Chanb16250e2006-09-27 16:10:14 -070011200 is_5750 = 1;
11201 }
Michael Chana71116d2005-05-29 14:58:11 -070011202
11203 for (i = 0; reg_tbl[i].offset != 0xffff; i++) {
11204 if (is_5705 && (reg_tbl[i].flags & TG3_FL_NOT_5705))
11205 continue;
11206
11207 if (!is_5705 && (reg_tbl[i].flags & TG3_FL_5705))
11208 continue;
11209
Joe Perches63c3a662011-04-26 08:12:10 +000011210 if (tg3_flag(tp, IS_5788) &&
Michael Chana71116d2005-05-29 14:58:11 -070011211 (reg_tbl[i].flags & TG3_FL_NOT_5788))
11212 continue;
11213
Michael Chanb16250e2006-09-27 16:10:14 -070011214 if (is_5750 && (reg_tbl[i].flags & TG3_FL_NOT_5750))
11215 continue;
11216
Michael Chana71116d2005-05-29 14:58:11 -070011217 offset = (u32) reg_tbl[i].offset;
11218 read_mask = reg_tbl[i].read_mask;
11219 write_mask = reg_tbl[i].write_mask;
11220
11221 /* Save the original register content */
11222 save_val = tr32(offset);
11223
11224 /* Determine the read-only value. */
11225 read_val = save_val & read_mask;
11226
11227 /* Write zero to the register, then make sure the read-only bits
11228 * are not changed and the read/write bits are all zeros.
11229 */
11230 tw32(offset, 0);
11231
11232 val = tr32(offset);
11233
11234 /* Test the read-only and read/write bits. */
11235 if (((val & read_mask) != read_val) || (val & write_mask))
11236 goto out;
11237
11238 /* Write ones to all the bits defined by RdMask and WrMask, then
11239 * make sure the read-only bits are not changed and the
11240 * read/write bits are all ones.
11241 */
11242 tw32(offset, read_mask | write_mask);
11243
11244 val = tr32(offset);
11245
11246 /* Test the read-only bits. */
11247 if ((val & read_mask) != read_val)
11248 goto out;
11249
11250 /* Test the read/write bits. */
11251 if ((val & write_mask) != write_mask)
11252 goto out;
11253
11254 tw32(offset, save_val);
11255 }
11256
11257 return 0;
11258
11259out:
Michael Chan9f88f292006-12-07 00:22:54 -080011260 if (netif_msg_hw(tp))
Matt Carlson2445e462010-04-05 10:19:21 +000011261 netdev_err(tp->dev,
11262 "Register test failed at offset %x\n", offset);
Michael Chana71116d2005-05-29 14:58:11 -070011263 tw32(offset, save_val);
11264 return -EIO;
11265}
11266
Michael Chan7942e1d2005-05-29 14:58:36 -070011267static int tg3_do_mem_test(struct tg3 *tp, u32 offset, u32 len)
11268{
Arjan van de Venf71e1302006-03-03 21:33:57 -050011269 static const u32 test_pattern[] = { 0x00000000, 0xffffffff, 0xaa55a55a };
Michael Chan7942e1d2005-05-29 14:58:36 -070011270 int i;
11271 u32 j;
11272
Alejandro Martinez Ruize9edda62007-10-15 03:37:43 +020011273 for (i = 0; i < ARRAY_SIZE(test_pattern); i++) {
Michael Chan7942e1d2005-05-29 14:58:36 -070011274 for (j = 0; j < len; j += 4) {
11275 u32 val;
11276
11277 tg3_write_mem(tp, offset + j, test_pattern[i]);
11278 tg3_read_mem(tp, offset + j, &val);
11279 if (val != test_pattern[i])
11280 return -EIO;
11281 }
11282 }
11283 return 0;
11284}
11285
11286static int tg3_test_memory(struct tg3 *tp)
11287{
11288 static struct mem_entry {
11289 u32 offset;
11290 u32 len;
11291 } mem_tbl_570x[] = {
Michael Chan38690192005-12-19 16:27:28 -080011292 { 0x00000000, 0x00b50},
Michael Chan7942e1d2005-05-29 14:58:36 -070011293 { 0x00002000, 0x1c000},
11294 { 0xffffffff, 0x00000}
11295 }, mem_tbl_5705[] = {
11296 { 0x00000100, 0x0000c},
11297 { 0x00000200, 0x00008},
Michael Chan7942e1d2005-05-29 14:58:36 -070011298 { 0x00004000, 0x00800},
11299 { 0x00006000, 0x01000},
11300 { 0x00008000, 0x02000},
11301 { 0x00010000, 0x0e000},
11302 { 0xffffffff, 0x00000}
Michael Chan79f4d132006-03-20 22:28:57 -080011303 }, mem_tbl_5755[] = {
11304 { 0x00000200, 0x00008},
11305 { 0x00004000, 0x00800},
11306 { 0x00006000, 0x00800},
11307 { 0x00008000, 0x02000},
11308 { 0x00010000, 0x0c000},
11309 { 0xffffffff, 0x00000}
Michael Chanb16250e2006-09-27 16:10:14 -070011310 }, mem_tbl_5906[] = {
11311 { 0x00000200, 0x00008},
11312 { 0x00004000, 0x00400},
11313 { 0x00006000, 0x00400},
11314 { 0x00008000, 0x01000},
11315 { 0x00010000, 0x01000},
11316 { 0xffffffff, 0x00000}
Matt Carlson8b5a6c42010-01-20 16:58:06 +000011317 }, mem_tbl_5717[] = {
11318 { 0x00000200, 0x00008},
11319 { 0x00010000, 0x0a000},
11320 { 0x00020000, 0x13c00},
11321 { 0xffffffff, 0x00000}
11322 }, mem_tbl_57765[] = {
11323 { 0x00000200, 0x00008},
11324 { 0x00004000, 0x00800},
11325 { 0x00006000, 0x09800},
11326 { 0x00010000, 0x0a000},
11327 { 0xffffffff, 0x00000}
Michael Chan7942e1d2005-05-29 14:58:36 -070011328 };
11329 struct mem_entry *mem_tbl;
11330 int err = 0;
11331 int i;
11332
Joe Perches63c3a662011-04-26 08:12:10 +000011333 if (tg3_flag(tp, 5717_PLUS))
Matt Carlson8b5a6c42010-01-20 16:58:06 +000011334 mem_tbl = mem_tbl_5717;
11335 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
11336 mem_tbl = mem_tbl_57765;
Joe Perches63c3a662011-04-26 08:12:10 +000011337 else if (tg3_flag(tp, 5755_PLUS))
Matt Carlson321d32a2008-11-21 17:22:19 -080011338 mem_tbl = mem_tbl_5755;
11339 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
11340 mem_tbl = mem_tbl_5906;
Joe Perches63c3a662011-04-26 08:12:10 +000011341 else if (tg3_flag(tp, 5705_PLUS))
Matt Carlson321d32a2008-11-21 17:22:19 -080011342 mem_tbl = mem_tbl_5705;
11343 else
Michael Chan7942e1d2005-05-29 14:58:36 -070011344 mem_tbl = mem_tbl_570x;
11345
11346 for (i = 0; mem_tbl[i].offset != 0xffffffff; i++) {
Matt Carlsonbe98da62010-07-11 09:31:46 +000011347 err = tg3_do_mem_test(tp, mem_tbl[i].offset, mem_tbl[i].len);
11348 if (err)
Michael Chan7942e1d2005-05-29 14:58:36 -070011349 break;
11350 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011351
Michael Chan7942e1d2005-05-29 14:58:36 -070011352 return err;
11353}
11354
Matt Carlsonbb158d62011-04-25 12:42:47 +000011355#define TG3_TSO_MSS 500
11356
11357#define TG3_TSO_IP_HDR_LEN 20
11358#define TG3_TSO_TCP_HDR_LEN 20
11359#define TG3_TSO_TCP_OPT_LEN 12
11360
11361static const u8 tg3_tso_header[] = {
113620x08, 0x00,
113630x45, 0x00, 0x00, 0x00,
113640x00, 0x00, 0x40, 0x00,
113650x40, 0x06, 0x00, 0x00,
113660x0a, 0x00, 0x00, 0x01,
113670x0a, 0x00, 0x00, 0x02,
113680x0d, 0x00, 0xe0, 0x00,
113690x00, 0x00, 0x01, 0x00,
113700x00, 0x00, 0x02, 0x00,
113710x80, 0x10, 0x10, 0x00,
113720x14, 0x09, 0x00, 0x00,
113730x01, 0x01, 0x08, 0x0a,
113740x11, 0x11, 0x11, 0x11,
113750x11, 0x11, 0x11, 0x11,
11376};
Michael Chan9f40dea2005-09-05 17:53:06 -070011377
Matt Carlson28a45952011-08-19 13:58:22 +000011378static int tg3_run_loopback(struct tg3 *tp, u32 pktsz, bool tso_loopback)
Michael Chanc76949a2005-05-29 14:58:59 -070011379{
Matt Carlson5e5a7f32011-08-19 13:58:21 +000011380 u32 rx_start_idx, rx_idx, tx_idx, opaque_key;
Matt Carlsonbb158d62011-04-25 12:42:47 +000011381 u32 base_flags = 0, mss = 0, desc_idx, coal_now, data_off, val;
Matt Carlson84b67b22011-07-27 14:20:52 +000011382 u32 budget;
Eric Dumazet9205fd92011-11-18 06:47:01 +000011383 struct sk_buff *skb;
11384 u8 *tx_data, *rx_data;
Michael Chanc76949a2005-05-29 14:58:59 -070011385 dma_addr_t map;
11386 int num_pkts, tx_len, rx_len, i, err;
11387 struct tg3_rx_buffer_desc *desc;
Matt Carlson898a56f2009-08-28 14:02:40 +000011388 struct tg3_napi *tnapi, *rnapi;
Matt Carlson8fea32b2010-09-15 08:59:58 +000011389 struct tg3_rx_prodring_set *tpr = &tp->napi[0].prodring;
Michael Chanc76949a2005-05-29 14:58:59 -070011390
Matt Carlsonc8873402010-02-12 14:47:11 +000011391 tnapi = &tp->napi[0];
11392 rnapi = &tp->napi[0];
Matt Carlson0c1d0e22009-09-01 13:16:33 +000011393 if (tp->irq_cnt > 1) {
Joe Perches63c3a662011-04-26 08:12:10 +000011394 if (tg3_flag(tp, ENABLE_RSS))
Matt Carlson1da85aa2010-09-30 10:34:34 +000011395 rnapi = &tp->napi[1];
Joe Perches63c3a662011-04-26 08:12:10 +000011396 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlsonc8873402010-02-12 14:47:11 +000011397 tnapi = &tp->napi[1];
Matt Carlson0c1d0e22009-09-01 13:16:33 +000011398 }
Matt Carlsonfd2ce372009-09-01 12:51:13 +000011399 coal_now = tnapi->coal_now | rnapi->coal_now;
Matt Carlson898a56f2009-08-28 14:02:40 +000011400
Michael Chanc76949a2005-05-29 14:58:59 -070011401 err = -EIO;
11402
Matt Carlson4852a862011-04-13 11:05:07 +000011403 tx_len = pktsz;
David S. Millera20e9c62006-07-31 22:38:16 -070011404 skb = netdev_alloc_skb(tp->dev, tx_len);
Jesper Juhla50bb7b2006-05-09 23:14:35 -070011405 if (!skb)
11406 return -ENOMEM;
11407
Michael Chanc76949a2005-05-29 14:58:59 -070011408 tx_data = skb_put(skb, tx_len);
11409 memcpy(tx_data, tp->dev->dev_addr, 6);
11410 memset(tx_data + 6, 0x0, 8);
11411
Matt Carlson4852a862011-04-13 11:05:07 +000011412 tw32(MAC_RX_MTU_SIZE, tx_len + ETH_FCS_LEN);
Michael Chanc76949a2005-05-29 14:58:59 -070011413
Matt Carlson28a45952011-08-19 13:58:22 +000011414 if (tso_loopback) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000011415 struct iphdr *iph = (struct iphdr *)&tx_data[ETH_HLEN];
11416
11417 u32 hdr_len = TG3_TSO_IP_HDR_LEN + TG3_TSO_TCP_HDR_LEN +
11418 TG3_TSO_TCP_OPT_LEN;
11419
11420 memcpy(tx_data + ETH_ALEN * 2, tg3_tso_header,
11421 sizeof(tg3_tso_header));
11422 mss = TG3_TSO_MSS;
11423
11424 val = tx_len - ETH_ALEN * 2 - sizeof(tg3_tso_header);
11425 num_pkts = DIV_ROUND_UP(val, TG3_TSO_MSS);
11426
11427 /* Set the total length field in the IP header */
11428 iph->tot_len = htons((u16)(mss + hdr_len));
11429
11430 base_flags = (TXD_FLAG_CPU_PRE_DMA |
11431 TXD_FLAG_CPU_POST_DMA);
11432
Joe Perches63c3a662011-04-26 08:12:10 +000011433 if (tg3_flag(tp, HW_TSO_1) ||
11434 tg3_flag(tp, HW_TSO_2) ||
11435 tg3_flag(tp, HW_TSO_3)) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000011436 struct tcphdr *th;
11437 val = ETH_HLEN + TG3_TSO_IP_HDR_LEN;
11438 th = (struct tcphdr *)&tx_data[val];
11439 th->check = 0;
11440 } else
11441 base_flags |= TXD_FLAG_TCPUDP_CSUM;
11442
Joe Perches63c3a662011-04-26 08:12:10 +000011443 if (tg3_flag(tp, HW_TSO_3)) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000011444 mss |= (hdr_len & 0xc) << 12;
11445 if (hdr_len & 0x10)
11446 base_flags |= 0x00000010;
11447 base_flags |= (hdr_len & 0x3e0) << 5;
Joe Perches63c3a662011-04-26 08:12:10 +000011448 } else if (tg3_flag(tp, HW_TSO_2))
Matt Carlsonbb158d62011-04-25 12:42:47 +000011449 mss |= hdr_len << 9;
Joe Perches63c3a662011-04-26 08:12:10 +000011450 else if (tg3_flag(tp, HW_TSO_1) ||
Matt Carlsonbb158d62011-04-25 12:42:47 +000011451 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
11452 mss |= (TG3_TSO_TCP_OPT_LEN << 9);
11453 } else {
11454 base_flags |= (TG3_TSO_TCP_OPT_LEN << 10);
11455 }
11456
11457 data_off = ETH_ALEN * 2 + sizeof(tg3_tso_header);
11458 } else {
11459 num_pkts = 1;
11460 data_off = ETH_HLEN;
11461 }
11462
11463 for (i = data_off; i < tx_len; i++)
Michael Chanc76949a2005-05-29 14:58:59 -070011464 tx_data[i] = (u8) (i & 0xff);
11465
Alexander Duyckf4188d82009-12-02 16:48:38 +000011466 map = pci_map_single(tp->pdev, skb->data, tx_len, PCI_DMA_TODEVICE);
11467 if (pci_dma_mapping_error(tp->pdev, map)) {
Matt Carlsona21771d2009-11-02 14:25:31 +000011468 dev_kfree_skb(skb);
11469 return -EIO;
11470 }
Michael Chanc76949a2005-05-29 14:58:59 -070011471
Matt Carlson0d681b22011-07-27 14:20:49 +000011472 val = tnapi->tx_prod;
11473 tnapi->tx_buffers[val].skb = skb;
11474 dma_unmap_addr_set(&tnapi->tx_buffers[val], mapping, map);
11475
Michael Chanc76949a2005-05-29 14:58:59 -070011476 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +000011477 rnapi->coal_now);
Michael Chanc76949a2005-05-29 14:58:59 -070011478
11479 udelay(10);
11480
Matt Carlson898a56f2009-08-28 14:02:40 +000011481 rx_start_idx = rnapi->hw_status->idx[0].rx_producer;
Michael Chanc76949a2005-05-29 14:58:59 -070011482
Matt Carlson84b67b22011-07-27 14:20:52 +000011483 budget = tg3_tx_avail(tnapi);
11484 if (tg3_tx_frag_set(tnapi, &val, &budget, map, tx_len,
Matt Carlsond1a3b732011-07-27 14:20:51 +000011485 base_flags | TXD_FLAG_END, mss, 0)) {
11486 tnapi->tx_buffers[val].skb = NULL;
11487 dev_kfree_skb(skb);
11488 return -EIO;
11489 }
Michael Chanc76949a2005-05-29 14:58:59 -070011490
Matt Carlsonf3f3f272009-08-28 14:03:21 +000011491 tnapi->tx_prod++;
Michael Chanc76949a2005-05-29 14:58:59 -070011492
Matt Carlsonf3f3f272009-08-28 14:03:21 +000011493 tw32_tx_mbox(tnapi->prodmbox, tnapi->tx_prod);
11494 tr32_mailbox(tnapi->prodmbox);
Michael Chanc76949a2005-05-29 14:58:59 -070011495
11496 udelay(10);
11497
Matt Carlson303fc922009-11-02 14:27:34 +000011498 /* 350 usec to allow enough time on some 10/100 Mbps devices. */
11499 for (i = 0; i < 35; i++) {
Michael Chanc76949a2005-05-29 14:58:59 -070011500 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +000011501 coal_now);
Michael Chanc76949a2005-05-29 14:58:59 -070011502
11503 udelay(10);
11504
Matt Carlson898a56f2009-08-28 14:02:40 +000011505 tx_idx = tnapi->hw_status->idx[0].tx_consumer;
11506 rx_idx = rnapi->hw_status->idx[0].rx_producer;
Matt Carlsonf3f3f272009-08-28 14:03:21 +000011507 if ((tx_idx == tnapi->tx_prod) &&
Michael Chanc76949a2005-05-29 14:58:59 -070011508 (rx_idx == (rx_start_idx + num_pkts)))
11509 break;
11510 }
11511
Matt Carlsonba1142e2011-11-04 09:15:00 +000011512 tg3_tx_skb_unmap(tnapi, tnapi->tx_prod - 1, -1);
Michael Chanc76949a2005-05-29 14:58:59 -070011513 dev_kfree_skb(skb);
11514
Matt Carlsonf3f3f272009-08-28 14:03:21 +000011515 if (tx_idx != tnapi->tx_prod)
Michael Chanc76949a2005-05-29 14:58:59 -070011516 goto out;
11517
11518 if (rx_idx != rx_start_idx + num_pkts)
11519 goto out;
11520
Matt Carlsonbb158d62011-04-25 12:42:47 +000011521 val = data_off;
11522 while (rx_idx != rx_start_idx) {
11523 desc = &rnapi->rx_rcb[rx_start_idx++];
11524 desc_idx = desc->opaque & RXD_OPAQUE_INDEX_MASK;
11525 opaque_key = desc->opaque & RXD_OPAQUE_RING_MASK;
Michael Chanc76949a2005-05-29 14:58:59 -070011526
Matt Carlsonbb158d62011-04-25 12:42:47 +000011527 if ((desc->err_vlan & RXD_ERR_MASK) != 0 &&
11528 (desc->err_vlan != RXD_ERR_ODD_NIBBLE_RCVD_MII))
Matt Carlson4852a862011-04-13 11:05:07 +000011529 goto out;
Michael Chanc76949a2005-05-29 14:58:59 -070011530
Matt Carlsonbb158d62011-04-25 12:42:47 +000011531 rx_len = ((desc->idx_len & RXD_LEN_MASK) >> RXD_LEN_SHIFT)
11532 - ETH_FCS_LEN;
11533
Matt Carlson28a45952011-08-19 13:58:22 +000011534 if (!tso_loopback) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000011535 if (rx_len != tx_len)
11536 goto out;
11537
11538 if (pktsz <= TG3_RX_STD_DMA_SZ - ETH_FCS_LEN) {
11539 if (opaque_key != RXD_OPAQUE_RING_STD)
11540 goto out;
11541 } else {
11542 if (opaque_key != RXD_OPAQUE_RING_JUMBO)
11543 goto out;
11544 }
11545 } else if ((desc->type_flags & RXD_FLAG_TCPUDP_CSUM) &&
11546 (desc->ip_tcp_csum & RXD_TCPCSUM_MASK)
Matt Carlson54e0a672011-05-19 12:12:50 +000011547 >> RXD_TCPCSUM_SHIFT != 0xffff) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000011548 goto out;
11549 }
11550
11551 if (opaque_key == RXD_OPAQUE_RING_STD) {
Eric Dumazet9205fd92011-11-18 06:47:01 +000011552 rx_data = tpr->rx_std_buffers[desc_idx].data;
Matt Carlsonbb158d62011-04-25 12:42:47 +000011553 map = dma_unmap_addr(&tpr->rx_std_buffers[desc_idx],
11554 mapping);
11555 } else if (opaque_key == RXD_OPAQUE_RING_JUMBO) {
Eric Dumazet9205fd92011-11-18 06:47:01 +000011556 rx_data = tpr->rx_jmb_buffers[desc_idx].data;
Matt Carlsonbb158d62011-04-25 12:42:47 +000011557 map = dma_unmap_addr(&tpr->rx_jmb_buffers[desc_idx],
11558 mapping);
11559 } else
Matt Carlson4852a862011-04-13 11:05:07 +000011560 goto out;
11561
Matt Carlsonbb158d62011-04-25 12:42:47 +000011562 pci_dma_sync_single_for_cpu(tp->pdev, map, rx_len,
11563 PCI_DMA_FROMDEVICE);
11564
Eric Dumazet9205fd92011-11-18 06:47:01 +000011565 rx_data += TG3_RX_OFFSET(tp);
Matt Carlsonbb158d62011-04-25 12:42:47 +000011566 for (i = data_off; i < rx_len; i++, val++) {
Eric Dumazet9205fd92011-11-18 06:47:01 +000011567 if (*(rx_data + i) != (u8) (val & 0xff))
Matt Carlsonbb158d62011-04-25 12:42:47 +000011568 goto out;
11569 }
Matt Carlson4852a862011-04-13 11:05:07 +000011570 }
11571
Michael Chanc76949a2005-05-29 14:58:59 -070011572 err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011573
Eric Dumazet9205fd92011-11-18 06:47:01 +000011574 /* tg3_free_rings will unmap and free the rx_data */
Michael Chanc76949a2005-05-29 14:58:59 -070011575out:
11576 return err;
11577}
11578
Matt Carlson00c266b2011-04-25 12:42:46 +000011579#define TG3_STD_LOOPBACK_FAILED 1
11580#define TG3_JMB_LOOPBACK_FAILED 2
Matt Carlsonbb158d62011-04-25 12:42:47 +000011581#define TG3_TSO_LOOPBACK_FAILED 4
Matt Carlson28a45952011-08-19 13:58:22 +000011582#define TG3_LOOPBACK_FAILED \
11583 (TG3_STD_LOOPBACK_FAILED | \
11584 TG3_JMB_LOOPBACK_FAILED | \
11585 TG3_TSO_LOOPBACK_FAILED)
Matt Carlson00c266b2011-04-25 12:42:46 +000011586
Matt Carlson941ec902011-08-19 13:58:23 +000011587static int tg3_test_loopback(struct tg3 *tp, u64 *data, bool do_extlpbk)
Michael Chan9f40dea2005-09-05 17:53:06 -070011588{
Matt Carlson28a45952011-08-19 13:58:22 +000011589 int err = -EIO;
Matt Carlson2215e242011-08-19 13:58:19 +000011590 u32 eee_cap;
Michael Chan9f40dea2005-09-05 17:53:06 -070011591
Matt Carlsonab789042011-01-25 15:58:54 +000011592 eee_cap = tp->phy_flags & TG3_PHYFLG_EEE_CAP;
11593 tp->phy_flags &= ~TG3_PHYFLG_EEE_CAP;
11594
Matt Carlson28a45952011-08-19 13:58:22 +000011595 if (!netif_running(tp->dev)) {
11596 data[0] = TG3_LOOPBACK_FAILED;
11597 data[1] = TG3_LOOPBACK_FAILED;
Matt Carlson941ec902011-08-19 13:58:23 +000011598 if (do_extlpbk)
11599 data[2] = TG3_LOOPBACK_FAILED;
Matt Carlson28a45952011-08-19 13:58:22 +000011600 goto done;
11601 }
11602
Michael Chanb9ec6c12006-07-25 16:37:27 -070011603 err = tg3_reset_hw(tp, 1);
Matt Carlsonab789042011-01-25 15:58:54 +000011604 if (err) {
Matt Carlson28a45952011-08-19 13:58:22 +000011605 data[0] = TG3_LOOPBACK_FAILED;
11606 data[1] = TG3_LOOPBACK_FAILED;
Matt Carlson941ec902011-08-19 13:58:23 +000011607 if (do_extlpbk)
11608 data[2] = TG3_LOOPBACK_FAILED;
Matt Carlsonab789042011-01-25 15:58:54 +000011609 goto done;
11610 }
Michael Chan9f40dea2005-09-05 17:53:06 -070011611
Joe Perches63c3a662011-04-26 08:12:10 +000011612 if (tg3_flag(tp, ENABLE_RSS)) {
Matt Carlson4a85f092011-04-20 07:57:37 +000011613 int i;
11614
11615 /* Reroute all rx packets to the 1st queue */
11616 for (i = MAC_RSS_INDIR_TBL_0;
11617 i < MAC_RSS_INDIR_TBL_0 + TG3_RSS_INDIR_TBL_SIZE; i += 4)
11618 tw32(i, 0x0);
11619 }
11620
Matt Carlson6e01b202011-08-19 13:58:20 +000011621 /* HW errata - mac loopback fails in some cases on 5780.
11622 * Normal traffic and PHY loopback are not affected by
11623 * errata. Also, the MAC loopback test is deprecated for
11624 * all newer ASIC revisions.
11625 */
11626 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5780 &&
11627 !tg3_flag(tp, CPMU_PRESENT)) {
11628 tg3_mac_loopback(tp, true);
Matt Carlson9936bcf2007-10-10 18:03:07 -070011629
Matt Carlson28a45952011-08-19 13:58:22 +000011630 if (tg3_run_loopback(tp, ETH_FRAME_LEN, false))
11631 data[0] |= TG3_STD_LOOPBACK_FAILED;
Matt Carlson6e01b202011-08-19 13:58:20 +000011632
11633 if (tg3_flag(tp, JUMBO_RING_ENABLE) &&
Matt Carlson28a45952011-08-19 13:58:22 +000011634 tg3_run_loopback(tp, 9000 + ETH_HLEN, false))
11635 data[0] |= TG3_JMB_LOOPBACK_FAILED;
Matt Carlson6e01b202011-08-19 13:58:20 +000011636
11637 tg3_mac_loopback(tp, false);
11638 }
Matt Carlson4852a862011-04-13 11:05:07 +000011639
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011640 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
Joe Perches63c3a662011-04-26 08:12:10 +000011641 !tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson5e5a7f32011-08-19 13:58:21 +000011642 int i;
11643
Matt Carlson941ec902011-08-19 13:58:23 +000011644 tg3_phy_lpbk_set(tp, 0, false);
Matt Carlson5e5a7f32011-08-19 13:58:21 +000011645
11646 /* Wait for link */
11647 for (i = 0; i < 100; i++) {
11648 if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
11649 break;
11650 mdelay(1);
11651 }
11652
Matt Carlson28a45952011-08-19 13:58:22 +000011653 if (tg3_run_loopback(tp, ETH_FRAME_LEN, false))
11654 data[1] |= TG3_STD_LOOPBACK_FAILED;
Joe Perches63c3a662011-04-26 08:12:10 +000011655 if (tg3_flag(tp, TSO_CAPABLE) &&
Matt Carlson28a45952011-08-19 13:58:22 +000011656 tg3_run_loopback(tp, ETH_FRAME_LEN, true))
11657 data[1] |= TG3_TSO_LOOPBACK_FAILED;
Joe Perches63c3a662011-04-26 08:12:10 +000011658 if (tg3_flag(tp, JUMBO_RING_ENABLE) &&
Matt Carlson28a45952011-08-19 13:58:22 +000011659 tg3_run_loopback(tp, 9000 + ETH_HLEN, false))
11660 data[1] |= TG3_JMB_LOOPBACK_FAILED;
Michael Chan9f40dea2005-09-05 17:53:06 -070011661
Matt Carlson941ec902011-08-19 13:58:23 +000011662 if (do_extlpbk) {
11663 tg3_phy_lpbk_set(tp, 0, true);
11664
11665 /* All link indications report up, but the hardware
11666 * isn't really ready for about 20 msec. Double it
11667 * to be sure.
11668 */
11669 mdelay(40);
11670
11671 if (tg3_run_loopback(tp, ETH_FRAME_LEN, false))
11672 data[2] |= TG3_STD_LOOPBACK_FAILED;
11673 if (tg3_flag(tp, TSO_CAPABLE) &&
11674 tg3_run_loopback(tp, ETH_FRAME_LEN, true))
11675 data[2] |= TG3_TSO_LOOPBACK_FAILED;
11676 if (tg3_flag(tp, JUMBO_RING_ENABLE) &&
11677 tg3_run_loopback(tp, 9000 + ETH_HLEN, false))
11678 data[2] |= TG3_JMB_LOOPBACK_FAILED;
11679 }
11680
Matt Carlson5e5a7f32011-08-19 13:58:21 +000011681 /* Re-enable gphy autopowerdown. */
11682 if (tp->phy_flags & TG3_PHYFLG_ENABLE_APD)
11683 tg3_phy_toggle_apd(tp, true);
11684 }
Matt Carlson6833c042008-11-21 17:18:59 -080011685
Matt Carlson941ec902011-08-19 13:58:23 +000011686 err = (data[0] | data[1] | data[2]) ? -EIO : 0;
Matt Carlson28a45952011-08-19 13:58:22 +000011687
Matt Carlsonab789042011-01-25 15:58:54 +000011688done:
11689 tp->phy_flags |= eee_cap;
11690
Michael Chan9f40dea2005-09-05 17:53:06 -070011691 return err;
11692}
11693
Michael Chan4cafd3f2005-05-29 14:56:34 -070011694static void tg3_self_test(struct net_device *dev, struct ethtool_test *etest,
11695 u64 *data)
11696{
Michael Chan566f86a2005-05-29 14:56:58 -070011697 struct tg3 *tp = netdev_priv(dev);
Matt Carlson941ec902011-08-19 13:58:23 +000011698 bool doextlpbk = etest->flags & ETH_TEST_FL_EXTERNAL_LB;
Michael Chan566f86a2005-05-29 14:56:58 -070011699
Matt Carlsonbed98292011-07-13 09:27:29 +000011700 if ((tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) &&
11701 tg3_power_up(tp)) {
11702 etest->flags |= ETH_TEST_FL_FAILED;
11703 memset(data, 1, sizeof(u64) * TG3_NUM_TEST);
11704 return;
11705 }
Michael Chanbc1c7562006-03-20 17:48:03 -080011706
Michael Chan566f86a2005-05-29 14:56:58 -070011707 memset(data, 0, sizeof(u64) * TG3_NUM_TEST);
11708
11709 if (tg3_test_nvram(tp) != 0) {
11710 etest->flags |= ETH_TEST_FL_FAILED;
11711 data[0] = 1;
11712 }
Matt Carlson941ec902011-08-19 13:58:23 +000011713 if (!doextlpbk && tg3_test_link(tp)) {
Michael Chanca430072005-05-29 14:57:23 -070011714 etest->flags |= ETH_TEST_FL_FAILED;
11715 data[1] = 1;
11716 }
Michael Chana71116d2005-05-29 14:58:11 -070011717 if (etest->flags & ETH_TEST_FL_OFFLINE) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011718 int err, err2 = 0, irq_sync = 0;
Michael Chana71116d2005-05-29 14:58:11 -070011719
Michael Chanbbe832c2005-06-24 20:20:04 -070011720 if (netif_running(dev)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011721 tg3_phy_stop(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -070011722 tg3_netif_stop(tp);
11723 irq_sync = 1;
11724 }
11725
11726 tg3_full_lock(tp, irq_sync);
Michael Chana71116d2005-05-29 14:58:11 -070011727
11728 tg3_halt(tp, RESET_KIND_SUSPEND, 1);
Michael Chanec41c7d2006-01-17 02:40:55 -080011729 err = tg3_nvram_lock(tp);
Michael Chana71116d2005-05-29 14:58:11 -070011730 tg3_halt_cpu(tp, RX_CPU_BASE);
Joe Perches63c3a662011-04-26 08:12:10 +000011731 if (!tg3_flag(tp, 5705_PLUS))
Michael Chana71116d2005-05-29 14:58:11 -070011732 tg3_halt_cpu(tp, TX_CPU_BASE);
Michael Chanec41c7d2006-01-17 02:40:55 -080011733 if (!err)
11734 tg3_nvram_unlock(tp);
Michael Chana71116d2005-05-29 14:58:11 -070011735
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011736 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
Michael Chand9ab5ad12006-03-20 22:27:35 -080011737 tg3_phy_reset(tp);
11738
Michael Chana71116d2005-05-29 14:58:11 -070011739 if (tg3_test_registers(tp) != 0) {
11740 etest->flags |= ETH_TEST_FL_FAILED;
11741 data[2] = 1;
11742 }
Matt Carlson28a45952011-08-19 13:58:22 +000011743
Michael Chan7942e1d2005-05-29 14:58:36 -070011744 if (tg3_test_memory(tp) != 0) {
11745 etest->flags |= ETH_TEST_FL_FAILED;
11746 data[3] = 1;
11747 }
Matt Carlson28a45952011-08-19 13:58:22 +000011748
Matt Carlson941ec902011-08-19 13:58:23 +000011749 if (doextlpbk)
11750 etest->flags |= ETH_TEST_FL_EXTERNAL_LB_DONE;
11751
11752 if (tg3_test_loopback(tp, &data[4], doextlpbk))
Michael Chanc76949a2005-05-29 14:58:59 -070011753 etest->flags |= ETH_TEST_FL_FAILED;
Michael Chana71116d2005-05-29 14:58:11 -070011754
David S. Millerf47c11e2005-06-24 20:18:35 -070011755 tg3_full_unlock(tp);
11756
Michael Chand4bc3922005-05-29 14:59:20 -070011757 if (tg3_test_interrupt(tp) != 0) {
11758 etest->flags |= ETH_TEST_FL_FAILED;
Matt Carlson941ec902011-08-19 13:58:23 +000011759 data[7] = 1;
Michael Chand4bc3922005-05-29 14:59:20 -070011760 }
David S. Millerf47c11e2005-06-24 20:18:35 -070011761
11762 tg3_full_lock(tp, 0);
Michael Chand4bc3922005-05-29 14:59:20 -070011763
Michael Chana71116d2005-05-29 14:58:11 -070011764 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
11765 if (netif_running(dev)) {
Joe Perches63c3a662011-04-26 08:12:10 +000011766 tg3_flag_set(tp, INIT_COMPLETE);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011767 err2 = tg3_restart_hw(tp, 1);
11768 if (!err2)
Michael Chanb9ec6c12006-07-25 16:37:27 -070011769 tg3_netif_start(tp);
Michael Chana71116d2005-05-29 14:58:11 -070011770 }
David S. Millerf47c11e2005-06-24 20:18:35 -070011771
11772 tg3_full_unlock(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011773
11774 if (irq_sync && !err2)
11775 tg3_phy_start(tp);
Michael Chana71116d2005-05-29 14:58:11 -070011776 }
Matt Carlson80096062010-08-02 11:26:06 +000011777 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000011778 tg3_power_down(tp);
Michael Chanbc1c7562006-03-20 17:48:03 -080011779
Michael Chan4cafd3f2005-05-29 14:56:34 -070011780}
11781
Linus Torvalds1da177e2005-04-16 15:20:36 -070011782static int tg3_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
11783{
11784 struct mii_ioctl_data *data = if_mii(ifr);
11785 struct tg3 *tp = netdev_priv(dev);
11786 int err;
11787
Joe Perches63c3a662011-04-26 08:12:10 +000011788 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000011789 struct phy_device *phydev;
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011790 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011791 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000011792 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Richard Cochran28b04112010-07-17 08:48:55 +000011793 return phy_mii_ioctl(phydev, ifr, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011794 }
11795
Matt Carlson33f401a2010-04-05 10:19:27 +000011796 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011797 case SIOCGMIIPHY:
Matt Carlson882e9792009-09-01 13:21:36 +000011798 data->phy_id = tp->phy_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011799
11800 /* fallthru */
11801 case SIOCGMIIREG: {
11802 u32 mii_regval;
11803
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011804 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011805 break; /* We have no PHY */
11806
Matt Carlson34eea5a2011-04-20 07:57:38 +000011807 if (!netif_running(dev))
Michael Chanbc1c7562006-03-20 17:48:03 -080011808 return -EAGAIN;
11809
David S. Millerf47c11e2005-06-24 20:18:35 -070011810 spin_lock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011811 err = tg3_readphy(tp, data->reg_num & 0x1f, &mii_regval);
David S. Millerf47c11e2005-06-24 20:18:35 -070011812 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011813
11814 data->val_out = mii_regval;
11815
11816 return err;
11817 }
11818
11819 case SIOCSMIIREG:
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011820 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011821 break; /* We have no PHY */
11822
Matt Carlson34eea5a2011-04-20 07:57:38 +000011823 if (!netif_running(dev))
Michael Chanbc1c7562006-03-20 17:48:03 -080011824 return -EAGAIN;
11825
David S. Millerf47c11e2005-06-24 20:18:35 -070011826 spin_lock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011827 err = tg3_writephy(tp, data->reg_num & 0x1f, data->val_in);
David S. Millerf47c11e2005-06-24 20:18:35 -070011828 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011829
11830 return err;
11831
11832 default:
11833 /* do nothing */
11834 break;
11835 }
11836 return -EOPNOTSUPP;
11837}
11838
David S. Miller15f98502005-05-18 22:49:26 -070011839static int tg3_get_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
11840{
11841 struct tg3 *tp = netdev_priv(dev);
11842
11843 memcpy(ec, &tp->coal, sizeof(*ec));
11844 return 0;
11845}
11846
Michael Chand244c892005-07-05 14:42:33 -070011847static int tg3_set_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
11848{
11849 struct tg3 *tp = netdev_priv(dev);
11850 u32 max_rxcoal_tick_int = 0, max_txcoal_tick_int = 0;
11851 u32 max_stat_coal_ticks = 0, min_stat_coal_ticks = 0;
11852
Joe Perches63c3a662011-04-26 08:12:10 +000011853 if (!tg3_flag(tp, 5705_PLUS)) {
Michael Chand244c892005-07-05 14:42:33 -070011854 max_rxcoal_tick_int = MAX_RXCOAL_TICK_INT;
11855 max_txcoal_tick_int = MAX_TXCOAL_TICK_INT;
11856 max_stat_coal_ticks = MAX_STAT_COAL_TICKS;
11857 min_stat_coal_ticks = MIN_STAT_COAL_TICKS;
11858 }
11859
11860 if ((ec->rx_coalesce_usecs > MAX_RXCOL_TICKS) ||
11861 (ec->tx_coalesce_usecs > MAX_TXCOL_TICKS) ||
11862 (ec->rx_max_coalesced_frames > MAX_RXMAX_FRAMES) ||
11863 (ec->tx_max_coalesced_frames > MAX_TXMAX_FRAMES) ||
11864 (ec->rx_coalesce_usecs_irq > max_rxcoal_tick_int) ||
11865 (ec->tx_coalesce_usecs_irq > max_txcoal_tick_int) ||
11866 (ec->rx_max_coalesced_frames_irq > MAX_RXCOAL_MAXF_INT) ||
11867 (ec->tx_max_coalesced_frames_irq > MAX_TXCOAL_MAXF_INT) ||
11868 (ec->stats_block_coalesce_usecs > max_stat_coal_ticks) ||
11869 (ec->stats_block_coalesce_usecs < min_stat_coal_ticks))
11870 return -EINVAL;
11871
11872 /* No rx interrupts will be generated if both are zero */
11873 if ((ec->rx_coalesce_usecs == 0) &&
11874 (ec->rx_max_coalesced_frames == 0))
11875 return -EINVAL;
11876
11877 /* No tx interrupts will be generated if both are zero */
11878 if ((ec->tx_coalesce_usecs == 0) &&
11879 (ec->tx_max_coalesced_frames == 0))
11880 return -EINVAL;
11881
11882 /* Only copy relevant parameters, ignore all others. */
11883 tp->coal.rx_coalesce_usecs = ec->rx_coalesce_usecs;
11884 tp->coal.tx_coalesce_usecs = ec->tx_coalesce_usecs;
11885 tp->coal.rx_max_coalesced_frames = ec->rx_max_coalesced_frames;
11886 tp->coal.tx_max_coalesced_frames = ec->tx_max_coalesced_frames;
11887 tp->coal.rx_coalesce_usecs_irq = ec->rx_coalesce_usecs_irq;
11888 tp->coal.tx_coalesce_usecs_irq = ec->tx_coalesce_usecs_irq;
11889 tp->coal.rx_max_coalesced_frames_irq = ec->rx_max_coalesced_frames_irq;
11890 tp->coal.tx_max_coalesced_frames_irq = ec->tx_max_coalesced_frames_irq;
11891 tp->coal.stats_block_coalesce_usecs = ec->stats_block_coalesce_usecs;
11892
11893 if (netif_running(dev)) {
11894 tg3_full_lock(tp, 0);
11895 __tg3_set_coalesce(tp, &tp->coal);
11896 tg3_full_unlock(tp);
11897 }
11898 return 0;
11899}
11900
Jeff Garzik7282d492006-09-13 14:30:00 -040011901static const struct ethtool_ops tg3_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011902 .get_settings = tg3_get_settings,
11903 .set_settings = tg3_set_settings,
11904 .get_drvinfo = tg3_get_drvinfo,
11905 .get_regs_len = tg3_get_regs_len,
11906 .get_regs = tg3_get_regs,
11907 .get_wol = tg3_get_wol,
11908 .set_wol = tg3_set_wol,
11909 .get_msglevel = tg3_get_msglevel,
11910 .set_msglevel = tg3_set_msglevel,
11911 .nway_reset = tg3_nway_reset,
11912 .get_link = ethtool_op_get_link,
11913 .get_eeprom_len = tg3_get_eeprom_len,
11914 .get_eeprom = tg3_get_eeprom,
11915 .set_eeprom = tg3_set_eeprom,
11916 .get_ringparam = tg3_get_ringparam,
11917 .set_ringparam = tg3_set_ringparam,
11918 .get_pauseparam = tg3_get_pauseparam,
11919 .set_pauseparam = tg3_set_pauseparam,
Michael Chan4cafd3f2005-05-29 14:56:34 -070011920 .self_test = tg3_self_test,
Linus Torvalds1da177e2005-04-16 15:20:36 -070011921 .get_strings = tg3_get_strings,
stephen hemminger81b87092011-04-04 08:43:50 +000011922 .set_phys_id = tg3_set_phys_id,
Linus Torvalds1da177e2005-04-16 15:20:36 -070011923 .get_ethtool_stats = tg3_get_ethtool_stats,
David S. Miller15f98502005-05-18 22:49:26 -070011924 .get_coalesce = tg3_get_coalesce,
Michael Chand244c892005-07-05 14:42:33 -070011925 .set_coalesce = tg3_set_coalesce,
Jeff Garzikb9f2c042007-10-03 18:07:32 -070011926 .get_sset_count = tg3_get_sset_count,
Linus Torvalds1da177e2005-04-16 15:20:36 -070011927};
11928
11929static void __devinit tg3_get_eeprom_size(struct tg3 *tp)
11930{
Michael Chan1b277772006-03-20 22:27:48 -080011931 u32 cursize, val, magic;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011932
11933 tp->nvram_size = EEPROM_CHIP_SIZE;
11934
Matt Carlsone4f34112009-02-25 14:25:00 +000011935 if (tg3_nvram_read(tp, 0, &magic) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011936 return;
11937
Michael Chanb16250e2006-09-27 16:10:14 -070011938 if ((magic != TG3_EEPROM_MAGIC) &&
11939 ((magic & TG3_EEPROM_MAGIC_FW_MSK) != TG3_EEPROM_MAGIC_FW) &&
11940 ((magic & TG3_EEPROM_MAGIC_HW_MSK) != TG3_EEPROM_MAGIC_HW))
Linus Torvalds1da177e2005-04-16 15:20:36 -070011941 return;
11942
11943 /*
11944 * Size the chip by reading offsets at increasing powers of two.
11945 * When we encounter our validation signature, we know the addressing
11946 * has wrapped around, and thus have our chip size.
11947 */
Michael Chan1b277772006-03-20 22:27:48 -080011948 cursize = 0x10;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011949
11950 while (cursize < tp->nvram_size) {
Matt Carlsone4f34112009-02-25 14:25:00 +000011951 if (tg3_nvram_read(tp, cursize, &val) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011952 return;
11953
Michael Chan18201802006-03-20 22:29:15 -080011954 if (val == magic)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011955 break;
11956
11957 cursize <<= 1;
11958 }
11959
11960 tp->nvram_size = cursize;
11961}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011962
Linus Torvalds1da177e2005-04-16 15:20:36 -070011963static void __devinit tg3_get_nvram_size(struct tg3 *tp)
11964{
11965 u32 val;
11966
Joe Perches63c3a662011-04-26 08:12:10 +000011967 if (tg3_flag(tp, NO_NVRAM) || tg3_nvram_read(tp, 0, &val) != 0)
Michael Chan1b277772006-03-20 22:27:48 -080011968 return;
11969
11970 /* Selfboot format */
Michael Chan18201802006-03-20 22:29:15 -080011971 if (val != TG3_EEPROM_MAGIC) {
Michael Chan1b277772006-03-20 22:27:48 -080011972 tg3_get_eeprom_size(tp);
11973 return;
11974 }
11975
Matt Carlson6d348f22009-02-25 14:25:52 +000011976 if (tg3_nvram_read(tp, 0xf0, &val) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011977 if (val != 0) {
Matt Carlson6d348f22009-02-25 14:25:52 +000011978 /* This is confusing. We want to operate on the
11979 * 16-bit value at offset 0xf2. The tg3_nvram_read()
11980 * call will read from NVRAM and byteswap the data
11981 * according to the byteswapping settings for all
11982 * other register accesses. This ensures the data we
11983 * want will always reside in the lower 16-bits.
11984 * However, the data in NVRAM is in LE format, which
11985 * means the data from the NVRAM read will always be
11986 * opposite the endianness of the CPU. The 16-bit
11987 * byteswap then brings the data to CPU endianness.
11988 */
11989 tp->nvram_size = swab16((u16)(val & 0x0000ffff)) * 1024;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011990 return;
11991 }
11992 }
Matt Carlsonfd1122a2008-05-02 16:48:36 -070011993 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011994}
11995
11996static void __devinit tg3_get_nvram_info(struct tg3 *tp)
11997{
11998 u32 nvcfg1;
11999
12000 nvcfg1 = tr32(NVRAM_CFG1);
12001 if (nvcfg1 & NVRAM_CFG1_FLASHIF_ENAB) {
Joe Perches63c3a662011-04-26 08:12:10 +000012002 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000012003 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012004 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
12005 tw32(NVRAM_CFG1, nvcfg1);
12006 }
12007
Matt Carlson6ff6f812011-05-19 12:12:54 +000012008 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 ||
Joe Perches63c3a662011-04-26 08:12:10 +000012009 tg3_flag(tp, 5780_CLASS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012010 switch (nvcfg1 & NVRAM_CFG1_VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000012011 case FLASH_VENDOR_ATMEL_FLASH_BUFFERED:
12012 tp->nvram_jedecnum = JEDEC_ATMEL;
12013 tp->nvram_pagesize = ATMEL_AT45DB0X1B_PAGE_SIZE;
Joe Perches63c3a662011-04-26 08:12:10 +000012014 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000012015 break;
12016 case FLASH_VENDOR_ATMEL_FLASH_UNBUFFERED:
12017 tp->nvram_jedecnum = JEDEC_ATMEL;
12018 tp->nvram_pagesize = ATMEL_AT25F512_PAGE_SIZE;
12019 break;
12020 case FLASH_VENDOR_ATMEL_EEPROM:
12021 tp->nvram_jedecnum = JEDEC_ATMEL;
12022 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
Joe Perches63c3a662011-04-26 08:12:10 +000012023 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000012024 break;
12025 case FLASH_VENDOR_ST:
12026 tp->nvram_jedecnum = JEDEC_ST;
12027 tp->nvram_pagesize = ST_M45PEX0_PAGE_SIZE;
Joe Perches63c3a662011-04-26 08:12:10 +000012028 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000012029 break;
12030 case FLASH_VENDOR_SAIFUN:
12031 tp->nvram_jedecnum = JEDEC_SAIFUN;
12032 tp->nvram_pagesize = SAIFUN_SA25F0XX_PAGE_SIZE;
12033 break;
12034 case FLASH_VENDOR_SST_SMALL:
12035 case FLASH_VENDOR_SST_LARGE:
12036 tp->nvram_jedecnum = JEDEC_SST;
12037 tp->nvram_pagesize = SST_25VF0X0_PAGE_SIZE;
12038 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012039 }
Matt Carlson8590a602009-08-28 12:29:16 +000012040 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012041 tp->nvram_jedecnum = JEDEC_ATMEL;
12042 tp->nvram_pagesize = ATMEL_AT45DB0X1B_PAGE_SIZE;
Joe Perches63c3a662011-04-26 08:12:10 +000012043 tg3_flag_set(tp, NVRAM_BUFFERED);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012044 }
12045}
12046
Matt Carlsona1b950d2009-09-01 13:20:17 +000012047static void __devinit tg3_nvram_get_pagesize(struct tg3 *tp, u32 nvmcfg1)
12048{
12049 switch (nvmcfg1 & NVRAM_CFG1_5752PAGE_SIZE_MASK) {
12050 case FLASH_5752PAGE_SIZE_256:
12051 tp->nvram_pagesize = 256;
12052 break;
12053 case FLASH_5752PAGE_SIZE_512:
12054 tp->nvram_pagesize = 512;
12055 break;
12056 case FLASH_5752PAGE_SIZE_1K:
12057 tp->nvram_pagesize = 1024;
12058 break;
12059 case FLASH_5752PAGE_SIZE_2K:
12060 tp->nvram_pagesize = 2048;
12061 break;
12062 case FLASH_5752PAGE_SIZE_4K:
12063 tp->nvram_pagesize = 4096;
12064 break;
12065 case FLASH_5752PAGE_SIZE_264:
12066 tp->nvram_pagesize = 264;
12067 break;
12068 case FLASH_5752PAGE_SIZE_528:
12069 tp->nvram_pagesize = 528;
12070 break;
12071 }
12072}
12073
Michael Chan361b4ac2005-04-21 17:11:21 -070012074static void __devinit tg3_get_5752_nvram_info(struct tg3 *tp)
12075{
12076 u32 nvcfg1;
12077
12078 nvcfg1 = tr32(NVRAM_CFG1);
12079
Michael Chane6af3012005-04-21 17:12:05 -070012080 /* NVRAM protection for TPM */
12081 if (nvcfg1 & (1 << 27))
Joe Perches63c3a662011-04-26 08:12:10 +000012082 tg3_flag_set(tp, PROTECTED_NVRAM);
Michael Chane6af3012005-04-21 17:12:05 -070012083
Michael Chan361b4ac2005-04-21 17:11:21 -070012084 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000012085 case FLASH_5752VENDOR_ATMEL_EEPROM_64KHZ:
12086 case FLASH_5752VENDOR_ATMEL_EEPROM_376KHZ:
12087 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012088 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000012089 break;
12090 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
12091 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012092 tg3_flag_set(tp, NVRAM_BUFFERED);
12093 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000012094 break;
12095 case FLASH_5752VENDOR_ST_M45PE10:
12096 case FLASH_5752VENDOR_ST_M45PE20:
12097 case FLASH_5752VENDOR_ST_M45PE40:
12098 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000012099 tg3_flag_set(tp, NVRAM_BUFFERED);
12100 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000012101 break;
Michael Chan361b4ac2005-04-21 17:11:21 -070012102 }
12103
Joe Perches63c3a662011-04-26 08:12:10 +000012104 if (tg3_flag(tp, FLASH)) {
Matt Carlsona1b950d2009-09-01 13:20:17 +000012105 tg3_nvram_get_pagesize(tp, nvcfg1);
Matt Carlson8590a602009-08-28 12:29:16 +000012106 } else {
Michael Chan361b4ac2005-04-21 17:11:21 -070012107 /* For eeprom, set pagesize to maximum eeprom size */
12108 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
12109
12110 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
12111 tw32(NVRAM_CFG1, nvcfg1);
12112 }
12113}
12114
Michael Chand3c7b882006-03-23 01:28:25 -080012115static void __devinit tg3_get_5755_nvram_info(struct tg3 *tp)
12116{
Matt Carlson989a9d22007-05-05 11:51:05 -070012117 u32 nvcfg1, protect = 0;
Michael Chand3c7b882006-03-23 01:28:25 -080012118
12119 nvcfg1 = tr32(NVRAM_CFG1);
12120
12121 /* NVRAM protection for TPM */
Matt Carlson989a9d22007-05-05 11:51:05 -070012122 if (nvcfg1 & (1 << 27)) {
Joe Perches63c3a662011-04-26 08:12:10 +000012123 tg3_flag_set(tp, PROTECTED_NVRAM);
Matt Carlson989a9d22007-05-05 11:51:05 -070012124 protect = 1;
12125 }
Michael Chand3c7b882006-03-23 01:28:25 -080012126
Matt Carlson989a9d22007-05-05 11:51:05 -070012127 nvcfg1 &= NVRAM_CFG1_5752VENDOR_MASK;
12128 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000012129 case FLASH_5755VENDOR_ATMEL_FLASH_1:
12130 case FLASH_5755VENDOR_ATMEL_FLASH_2:
12131 case FLASH_5755VENDOR_ATMEL_FLASH_3:
12132 case FLASH_5755VENDOR_ATMEL_FLASH_5:
12133 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012134 tg3_flag_set(tp, NVRAM_BUFFERED);
12135 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000012136 tp->nvram_pagesize = 264;
12137 if (nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_1 ||
12138 nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_5)
12139 tp->nvram_size = (protect ? 0x3e200 :
12140 TG3_NVRAM_SIZE_512KB);
12141 else if (nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_2)
12142 tp->nvram_size = (protect ? 0x1f200 :
12143 TG3_NVRAM_SIZE_256KB);
12144 else
12145 tp->nvram_size = (protect ? 0x1f200 :
12146 TG3_NVRAM_SIZE_128KB);
12147 break;
12148 case FLASH_5752VENDOR_ST_M45PE10:
12149 case FLASH_5752VENDOR_ST_M45PE20:
12150 case FLASH_5752VENDOR_ST_M45PE40:
12151 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000012152 tg3_flag_set(tp, NVRAM_BUFFERED);
12153 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000012154 tp->nvram_pagesize = 256;
12155 if (nvcfg1 == FLASH_5752VENDOR_ST_M45PE10)
12156 tp->nvram_size = (protect ?
12157 TG3_NVRAM_SIZE_64KB :
12158 TG3_NVRAM_SIZE_128KB);
12159 else if (nvcfg1 == FLASH_5752VENDOR_ST_M45PE20)
12160 tp->nvram_size = (protect ?
12161 TG3_NVRAM_SIZE_64KB :
12162 TG3_NVRAM_SIZE_256KB);
12163 else
12164 tp->nvram_size = (protect ?
12165 TG3_NVRAM_SIZE_128KB :
12166 TG3_NVRAM_SIZE_512KB);
12167 break;
Michael Chand3c7b882006-03-23 01:28:25 -080012168 }
12169}
12170
Michael Chan1b277772006-03-20 22:27:48 -080012171static void __devinit tg3_get_5787_nvram_info(struct tg3 *tp)
12172{
12173 u32 nvcfg1;
12174
12175 nvcfg1 = tr32(NVRAM_CFG1);
12176
12177 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000012178 case FLASH_5787VENDOR_ATMEL_EEPROM_64KHZ:
12179 case FLASH_5787VENDOR_ATMEL_EEPROM_376KHZ:
12180 case FLASH_5787VENDOR_MICRO_EEPROM_64KHZ:
12181 case FLASH_5787VENDOR_MICRO_EEPROM_376KHZ:
12182 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012183 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000012184 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
Michael Chan1b277772006-03-20 22:27:48 -080012185
Matt Carlson8590a602009-08-28 12:29:16 +000012186 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
12187 tw32(NVRAM_CFG1, nvcfg1);
12188 break;
12189 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
12190 case FLASH_5755VENDOR_ATMEL_FLASH_1:
12191 case FLASH_5755VENDOR_ATMEL_FLASH_2:
12192 case FLASH_5755VENDOR_ATMEL_FLASH_3:
12193 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012194 tg3_flag_set(tp, NVRAM_BUFFERED);
12195 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000012196 tp->nvram_pagesize = 264;
12197 break;
12198 case FLASH_5752VENDOR_ST_M45PE10:
12199 case FLASH_5752VENDOR_ST_M45PE20:
12200 case FLASH_5752VENDOR_ST_M45PE40:
12201 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000012202 tg3_flag_set(tp, NVRAM_BUFFERED);
12203 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000012204 tp->nvram_pagesize = 256;
12205 break;
Michael Chan1b277772006-03-20 22:27:48 -080012206 }
12207}
12208
Matt Carlson6b91fa02007-10-10 18:01:09 -070012209static void __devinit tg3_get_5761_nvram_info(struct tg3 *tp)
12210{
12211 u32 nvcfg1, protect = 0;
12212
12213 nvcfg1 = tr32(NVRAM_CFG1);
12214
12215 /* NVRAM protection for TPM */
12216 if (nvcfg1 & (1 << 27)) {
Joe Perches63c3a662011-04-26 08:12:10 +000012217 tg3_flag_set(tp, PROTECTED_NVRAM);
Matt Carlson6b91fa02007-10-10 18:01:09 -070012218 protect = 1;
12219 }
12220
12221 nvcfg1 &= NVRAM_CFG1_5752VENDOR_MASK;
12222 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000012223 case FLASH_5761VENDOR_ATMEL_ADB021D:
12224 case FLASH_5761VENDOR_ATMEL_ADB041D:
12225 case FLASH_5761VENDOR_ATMEL_ADB081D:
12226 case FLASH_5761VENDOR_ATMEL_ADB161D:
12227 case FLASH_5761VENDOR_ATMEL_MDB021D:
12228 case FLASH_5761VENDOR_ATMEL_MDB041D:
12229 case FLASH_5761VENDOR_ATMEL_MDB081D:
12230 case FLASH_5761VENDOR_ATMEL_MDB161D:
12231 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012232 tg3_flag_set(tp, NVRAM_BUFFERED);
12233 tg3_flag_set(tp, FLASH);
12234 tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
Matt Carlson8590a602009-08-28 12:29:16 +000012235 tp->nvram_pagesize = 256;
12236 break;
12237 case FLASH_5761VENDOR_ST_A_M45PE20:
12238 case FLASH_5761VENDOR_ST_A_M45PE40:
12239 case FLASH_5761VENDOR_ST_A_M45PE80:
12240 case FLASH_5761VENDOR_ST_A_M45PE16:
12241 case FLASH_5761VENDOR_ST_M_M45PE20:
12242 case FLASH_5761VENDOR_ST_M_M45PE40:
12243 case FLASH_5761VENDOR_ST_M_M45PE80:
12244 case FLASH_5761VENDOR_ST_M_M45PE16:
12245 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000012246 tg3_flag_set(tp, NVRAM_BUFFERED);
12247 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000012248 tp->nvram_pagesize = 256;
12249 break;
Matt Carlson6b91fa02007-10-10 18:01:09 -070012250 }
12251
12252 if (protect) {
12253 tp->nvram_size = tr32(NVRAM_ADDR_LOCKOUT);
12254 } else {
12255 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000012256 case FLASH_5761VENDOR_ATMEL_ADB161D:
12257 case FLASH_5761VENDOR_ATMEL_MDB161D:
12258 case FLASH_5761VENDOR_ST_A_M45PE16:
12259 case FLASH_5761VENDOR_ST_M_M45PE16:
12260 tp->nvram_size = TG3_NVRAM_SIZE_2MB;
12261 break;
12262 case FLASH_5761VENDOR_ATMEL_ADB081D:
12263 case FLASH_5761VENDOR_ATMEL_MDB081D:
12264 case FLASH_5761VENDOR_ST_A_M45PE80:
12265 case FLASH_5761VENDOR_ST_M_M45PE80:
12266 tp->nvram_size = TG3_NVRAM_SIZE_1MB;
12267 break;
12268 case FLASH_5761VENDOR_ATMEL_ADB041D:
12269 case FLASH_5761VENDOR_ATMEL_MDB041D:
12270 case FLASH_5761VENDOR_ST_A_M45PE40:
12271 case FLASH_5761VENDOR_ST_M_M45PE40:
12272 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
12273 break;
12274 case FLASH_5761VENDOR_ATMEL_ADB021D:
12275 case FLASH_5761VENDOR_ATMEL_MDB021D:
12276 case FLASH_5761VENDOR_ST_A_M45PE20:
12277 case FLASH_5761VENDOR_ST_M_M45PE20:
12278 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
12279 break;
Matt Carlson6b91fa02007-10-10 18:01:09 -070012280 }
12281 }
12282}
12283
Michael Chanb5d37722006-09-27 16:06:21 -070012284static void __devinit tg3_get_5906_nvram_info(struct tg3 *tp)
12285{
12286 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012287 tg3_flag_set(tp, NVRAM_BUFFERED);
Michael Chanb5d37722006-09-27 16:06:21 -070012288 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
12289}
12290
Matt Carlson321d32a2008-11-21 17:22:19 -080012291static void __devinit tg3_get_57780_nvram_info(struct tg3 *tp)
12292{
12293 u32 nvcfg1;
12294
12295 nvcfg1 = tr32(NVRAM_CFG1);
12296
12297 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
12298 case FLASH_5787VENDOR_ATMEL_EEPROM_376KHZ:
12299 case FLASH_5787VENDOR_MICRO_EEPROM_376KHZ:
12300 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012301 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson321d32a2008-11-21 17:22:19 -080012302 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
12303
12304 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
12305 tw32(NVRAM_CFG1, nvcfg1);
12306 return;
12307 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
12308 case FLASH_57780VENDOR_ATMEL_AT45DB011D:
12309 case FLASH_57780VENDOR_ATMEL_AT45DB011B:
12310 case FLASH_57780VENDOR_ATMEL_AT45DB021D:
12311 case FLASH_57780VENDOR_ATMEL_AT45DB021B:
12312 case FLASH_57780VENDOR_ATMEL_AT45DB041D:
12313 case FLASH_57780VENDOR_ATMEL_AT45DB041B:
12314 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012315 tg3_flag_set(tp, NVRAM_BUFFERED);
12316 tg3_flag_set(tp, FLASH);
Matt Carlson321d32a2008-11-21 17:22:19 -080012317
12318 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
12319 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
12320 case FLASH_57780VENDOR_ATMEL_AT45DB011D:
12321 case FLASH_57780VENDOR_ATMEL_AT45DB011B:
12322 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
12323 break;
12324 case FLASH_57780VENDOR_ATMEL_AT45DB021D:
12325 case FLASH_57780VENDOR_ATMEL_AT45DB021B:
12326 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
12327 break;
12328 case FLASH_57780VENDOR_ATMEL_AT45DB041D:
12329 case FLASH_57780VENDOR_ATMEL_AT45DB041B:
12330 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
12331 break;
12332 }
12333 break;
12334 case FLASH_5752VENDOR_ST_M45PE10:
12335 case FLASH_5752VENDOR_ST_M45PE20:
12336 case FLASH_5752VENDOR_ST_M45PE40:
12337 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000012338 tg3_flag_set(tp, NVRAM_BUFFERED);
12339 tg3_flag_set(tp, FLASH);
Matt Carlson321d32a2008-11-21 17:22:19 -080012340
12341 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
12342 case FLASH_5752VENDOR_ST_M45PE10:
12343 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
12344 break;
12345 case FLASH_5752VENDOR_ST_M45PE20:
12346 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
12347 break;
12348 case FLASH_5752VENDOR_ST_M45PE40:
12349 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
12350 break;
12351 }
12352 break;
12353 default:
Joe Perches63c3a662011-04-26 08:12:10 +000012354 tg3_flag_set(tp, NO_NVRAM);
Matt Carlson321d32a2008-11-21 17:22:19 -080012355 return;
12356 }
12357
Matt Carlsona1b950d2009-09-01 13:20:17 +000012358 tg3_nvram_get_pagesize(tp, nvcfg1);
12359 if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
Joe Perches63c3a662011-04-26 08:12:10 +000012360 tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
Matt Carlsona1b950d2009-09-01 13:20:17 +000012361}
12362
12363
12364static void __devinit tg3_get_5717_nvram_info(struct tg3 *tp)
12365{
12366 u32 nvcfg1;
12367
12368 nvcfg1 = tr32(NVRAM_CFG1);
12369
12370 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
12371 case FLASH_5717VENDOR_ATMEL_EEPROM:
12372 case FLASH_5717VENDOR_MICRO_EEPROM:
12373 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012374 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlsona1b950d2009-09-01 13:20:17 +000012375 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
12376
12377 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
12378 tw32(NVRAM_CFG1, nvcfg1);
12379 return;
12380 case FLASH_5717VENDOR_ATMEL_MDB011D:
12381 case FLASH_5717VENDOR_ATMEL_ADB011B:
12382 case FLASH_5717VENDOR_ATMEL_ADB011D:
12383 case FLASH_5717VENDOR_ATMEL_MDB021D:
12384 case FLASH_5717VENDOR_ATMEL_ADB021B:
12385 case FLASH_5717VENDOR_ATMEL_ADB021D:
12386 case FLASH_5717VENDOR_ATMEL_45USPT:
12387 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012388 tg3_flag_set(tp, NVRAM_BUFFERED);
12389 tg3_flag_set(tp, FLASH);
Matt Carlsona1b950d2009-09-01 13:20:17 +000012390
12391 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
12392 case FLASH_5717VENDOR_ATMEL_MDB021D:
Matt Carlson66ee33b2011-04-05 14:22:51 +000012393 /* Detect size with tg3_nvram_get_size() */
12394 break;
Matt Carlsona1b950d2009-09-01 13:20:17 +000012395 case FLASH_5717VENDOR_ATMEL_ADB021B:
12396 case FLASH_5717VENDOR_ATMEL_ADB021D:
12397 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
12398 break;
12399 default:
12400 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
12401 break;
12402 }
Matt Carlson321d32a2008-11-21 17:22:19 -080012403 break;
Matt Carlsona1b950d2009-09-01 13:20:17 +000012404 case FLASH_5717VENDOR_ST_M_M25PE10:
12405 case FLASH_5717VENDOR_ST_A_M25PE10:
12406 case FLASH_5717VENDOR_ST_M_M45PE10:
12407 case FLASH_5717VENDOR_ST_A_M45PE10:
12408 case FLASH_5717VENDOR_ST_M_M25PE20:
12409 case FLASH_5717VENDOR_ST_A_M25PE20:
12410 case FLASH_5717VENDOR_ST_M_M45PE20:
12411 case FLASH_5717VENDOR_ST_A_M45PE20:
12412 case FLASH_5717VENDOR_ST_25USPT:
12413 case FLASH_5717VENDOR_ST_45USPT:
12414 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000012415 tg3_flag_set(tp, NVRAM_BUFFERED);
12416 tg3_flag_set(tp, FLASH);
Matt Carlsona1b950d2009-09-01 13:20:17 +000012417
12418 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
12419 case FLASH_5717VENDOR_ST_M_M25PE20:
Matt Carlsona1b950d2009-09-01 13:20:17 +000012420 case FLASH_5717VENDOR_ST_M_M45PE20:
Matt Carlson66ee33b2011-04-05 14:22:51 +000012421 /* Detect size with tg3_nvram_get_size() */
12422 break;
12423 case FLASH_5717VENDOR_ST_A_M25PE20:
Matt Carlsona1b950d2009-09-01 13:20:17 +000012424 case FLASH_5717VENDOR_ST_A_M45PE20:
12425 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
12426 break;
12427 default:
12428 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
12429 break;
12430 }
Matt Carlson321d32a2008-11-21 17:22:19 -080012431 break;
Matt Carlsona1b950d2009-09-01 13:20:17 +000012432 default:
Joe Perches63c3a662011-04-26 08:12:10 +000012433 tg3_flag_set(tp, NO_NVRAM);
Matt Carlsona1b950d2009-09-01 13:20:17 +000012434 return;
Matt Carlson321d32a2008-11-21 17:22:19 -080012435 }
Matt Carlsona1b950d2009-09-01 13:20:17 +000012436
12437 tg3_nvram_get_pagesize(tp, nvcfg1);
12438 if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
Joe Perches63c3a662011-04-26 08:12:10 +000012439 tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
Matt Carlson321d32a2008-11-21 17:22:19 -080012440}
12441
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012442static void __devinit tg3_get_5720_nvram_info(struct tg3 *tp)
12443{
12444 u32 nvcfg1, nvmpinstrp;
12445
12446 nvcfg1 = tr32(NVRAM_CFG1);
12447 nvmpinstrp = nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK;
12448
12449 switch (nvmpinstrp) {
12450 case FLASH_5720_EEPROM_HD:
12451 case FLASH_5720_EEPROM_LD:
12452 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012453 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012454
12455 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
12456 tw32(NVRAM_CFG1, nvcfg1);
12457 if (nvmpinstrp == FLASH_5720_EEPROM_HD)
12458 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
12459 else
12460 tp->nvram_pagesize = ATMEL_AT24C02_CHIP_SIZE;
12461 return;
12462 case FLASH_5720VENDOR_M_ATMEL_DB011D:
12463 case FLASH_5720VENDOR_A_ATMEL_DB011B:
12464 case FLASH_5720VENDOR_A_ATMEL_DB011D:
12465 case FLASH_5720VENDOR_M_ATMEL_DB021D:
12466 case FLASH_5720VENDOR_A_ATMEL_DB021B:
12467 case FLASH_5720VENDOR_A_ATMEL_DB021D:
12468 case FLASH_5720VENDOR_M_ATMEL_DB041D:
12469 case FLASH_5720VENDOR_A_ATMEL_DB041B:
12470 case FLASH_5720VENDOR_A_ATMEL_DB041D:
12471 case FLASH_5720VENDOR_M_ATMEL_DB081D:
12472 case FLASH_5720VENDOR_A_ATMEL_DB081D:
12473 case FLASH_5720VENDOR_ATMEL_45USPT:
12474 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012475 tg3_flag_set(tp, NVRAM_BUFFERED);
12476 tg3_flag_set(tp, FLASH);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012477
12478 switch (nvmpinstrp) {
12479 case FLASH_5720VENDOR_M_ATMEL_DB021D:
12480 case FLASH_5720VENDOR_A_ATMEL_DB021B:
12481 case FLASH_5720VENDOR_A_ATMEL_DB021D:
12482 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
12483 break;
12484 case FLASH_5720VENDOR_M_ATMEL_DB041D:
12485 case FLASH_5720VENDOR_A_ATMEL_DB041B:
12486 case FLASH_5720VENDOR_A_ATMEL_DB041D:
12487 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
12488 break;
12489 case FLASH_5720VENDOR_M_ATMEL_DB081D:
12490 case FLASH_5720VENDOR_A_ATMEL_DB081D:
12491 tp->nvram_size = TG3_NVRAM_SIZE_1MB;
12492 break;
12493 default:
12494 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
12495 break;
12496 }
12497 break;
12498 case FLASH_5720VENDOR_M_ST_M25PE10:
12499 case FLASH_5720VENDOR_M_ST_M45PE10:
12500 case FLASH_5720VENDOR_A_ST_M25PE10:
12501 case FLASH_5720VENDOR_A_ST_M45PE10:
12502 case FLASH_5720VENDOR_M_ST_M25PE20:
12503 case FLASH_5720VENDOR_M_ST_M45PE20:
12504 case FLASH_5720VENDOR_A_ST_M25PE20:
12505 case FLASH_5720VENDOR_A_ST_M45PE20:
12506 case FLASH_5720VENDOR_M_ST_M25PE40:
12507 case FLASH_5720VENDOR_M_ST_M45PE40:
12508 case FLASH_5720VENDOR_A_ST_M25PE40:
12509 case FLASH_5720VENDOR_A_ST_M45PE40:
12510 case FLASH_5720VENDOR_M_ST_M25PE80:
12511 case FLASH_5720VENDOR_M_ST_M45PE80:
12512 case FLASH_5720VENDOR_A_ST_M25PE80:
12513 case FLASH_5720VENDOR_A_ST_M45PE80:
12514 case FLASH_5720VENDOR_ST_25USPT:
12515 case FLASH_5720VENDOR_ST_45USPT:
12516 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000012517 tg3_flag_set(tp, NVRAM_BUFFERED);
12518 tg3_flag_set(tp, FLASH);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012519
12520 switch (nvmpinstrp) {
12521 case FLASH_5720VENDOR_M_ST_M25PE20:
12522 case FLASH_5720VENDOR_M_ST_M45PE20:
12523 case FLASH_5720VENDOR_A_ST_M25PE20:
12524 case FLASH_5720VENDOR_A_ST_M45PE20:
12525 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
12526 break;
12527 case FLASH_5720VENDOR_M_ST_M25PE40:
12528 case FLASH_5720VENDOR_M_ST_M45PE40:
12529 case FLASH_5720VENDOR_A_ST_M25PE40:
12530 case FLASH_5720VENDOR_A_ST_M45PE40:
12531 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
12532 break;
12533 case FLASH_5720VENDOR_M_ST_M25PE80:
12534 case FLASH_5720VENDOR_M_ST_M45PE80:
12535 case FLASH_5720VENDOR_A_ST_M25PE80:
12536 case FLASH_5720VENDOR_A_ST_M45PE80:
12537 tp->nvram_size = TG3_NVRAM_SIZE_1MB;
12538 break;
12539 default:
12540 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
12541 break;
12542 }
12543 break;
12544 default:
Joe Perches63c3a662011-04-26 08:12:10 +000012545 tg3_flag_set(tp, NO_NVRAM);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012546 return;
12547 }
12548
12549 tg3_nvram_get_pagesize(tp, nvcfg1);
12550 if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
Joe Perches63c3a662011-04-26 08:12:10 +000012551 tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012552}
12553
Linus Torvalds1da177e2005-04-16 15:20:36 -070012554/* Chips other than 5700/5701 use the NVRAM for fetching info. */
12555static void __devinit tg3_nvram_init(struct tg3 *tp)
12556{
Linus Torvalds1da177e2005-04-16 15:20:36 -070012557 tw32_f(GRC_EEPROM_ADDR,
12558 (EEPROM_ADDR_FSM_RESET |
12559 (EEPROM_DEFAULT_CLOCK_PERIOD <<
12560 EEPROM_ADDR_CLKPERD_SHIFT)));
12561
Michael Chan9d57f012006-12-07 00:23:25 -080012562 msleep(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012563
12564 /* Enable seeprom accesses. */
12565 tw32_f(GRC_LOCAL_CTRL,
12566 tr32(GRC_LOCAL_CTRL) | GRC_LCLCTRL_AUTO_SEEPROM);
12567 udelay(100);
12568
12569 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
12570 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701) {
Joe Perches63c3a662011-04-26 08:12:10 +000012571 tg3_flag_set(tp, NVRAM);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012572
Michael Chanec41c7d2006-01-17 02:40:55 -080012573 if (tg3_nvram_lock(tp)) {
Matt Carlson5129c3a2010-04-05 10:19:23 +000012574 netdev_warn(tp->dev,
12575 "Cannot get nvram lock, %s failed\n",
Joe Perches05dbe002010-02-17 19:44:19 +000012576 __func__);
Michael Chanec41c7d2006-01-17 02:40:55 -080012577 return;
12578 }
Michael Chane6af3012005-04-21 17:12:05 -070012579 tg3_enable_nvram_access(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012580
Matt Carlson989a9d22007-05-05 11:51:05 -070012581 tp->nvram_size = 0;
12582
Michael Chan361b4ac2005-04-21 17:11:21 -070012583 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752)
12584 tg3_get_5752_nvram_info(tp);
Michael Chand3c7b882006-03-23 01:28:25 -080012585 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755)
12586 tg3_get_5755_nvram_info(tp);
Matt Carlsond30cdd22007-10-07 23:28:35 -070012587 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787 ||
Matt Carlson57e69832008-05-25 23:48:31 -070012588 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
12589 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
Michael Chan1b277772006-03-20 22:27:48 -080012590 tg3_get_5787_nvram_info(tp);
Matt Carlson6b91fa02007-10-10 18:01:09 -070012591 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
12592 tg3_get_5761_nvram_info(tp);
Michael Chanb5d37722006-09-27 16:06:21 -070012593 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
12594 tg3_get_5906_nvram_info(tp);
Matt Carlsonb703df62009-12-03 08:36:21 +000012595 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
12596 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Matt Carlson321d32a2008-11-21 17:22:19 -080012597 tg3_get_57780_nvram_info(tp);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012598 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
12599 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
Matt Carlsona1b950d2009-09-01 13:20:17 +000012600 tg3_get_5717_nvram_info(tp);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012601 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
12602 tg3_get_5720_nvram_info(tp);
Michael Chan361b4ac2005-04-21 17:11:21 -070012603 else
12604 tg3_get_nvram_info(tp);
12605
Matt Carlson989a9d22007-05-05 11:51:05 -070012606 if (tp->nvram_size == 0)
12607 tg3_get_nvram_size(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012608
Michael Chane6af3012005-04-21 17:12:05 -070012609 tg3_disable_nvram_access(tp);
Michael Chan381291b2005-12-13 21:08:21 -080012610 tg3_nvram_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012611
12612 } else {
Joe Perches63c3a662011-04-26 08:12:10 +000012613 tg3_flag_clear(tp, NVRAM);
12614 tg3_flag_clear(tp, NVRAM_BUFFERED);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012615
12616 tg3_get_eeprom_size(tp);
12617 }
12618}
12619
Linus Torvalds1da177e2005-04-16 15:20:36 -070012620static int tg3_nvram_write_block_using_eeprom(struct tg3 *tp,
12621 u32 offset, u32 len, u8 *buf)
12622{
12623 int i, j, rc = 0;
12624 u32 val;
12625
12626 for (i = 0; i < len; i += 4) {
Al Virob9fc7dc2007-12-17 22:59:57 -080012627 u32 addr;
Matt Carlsona9dc5292009-02-25 14:25:30 +000012628 __be32 data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012629
12630 addr = offset + i;
12631
12632 memcpy(&data, buf + i, 4);
12633
Matt Carlson62cedd12009-04-20 14:52:29 -070012634 /*
12635 * The SEEPROM interface expects the data to always be opposite
12636 * the native endian format. We accomplish this by reversing
12637 * all the operations that would have been performed on the
12638 * data from a call to tg3_nvram_read_be32().
12639 */
12640 tw32(GRC_EEPROM_DATA, swab32(be32_to_cpu(data)));
Linus Torvalds1da177e2005-04-16 15:20:36 -070012641
12642 val = tr32(GRC_EEPROM_ADDR);
12643 tw32(GRC_EEPROM_ADDR, val | EEPROM_ADDR_COMPLETE);
12644
12645 val &= ~(EEPROM_ADDR_ADDR_MASK | EEPROM_ADDR_DEVID_MASK |
12646 EEPROM_ADDR_READ);
12647 tw32(GRC_EEPROM_ADDR, val |
12648 (0 << EEPROM_ADDR_DEVID_SHIFT) |
12649 (addr & EEPROM_ADDR_ADDR_MASK) |
12650 EEPROM_ADDR_START |
12651 EEPROM_ADDR_WRITE);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012652
Michael Chan9d57f012006-12-07 00:23:25 -080012653 for (j = 0; j < 1000; j++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012654 val = tr32(GRC_EEPROM_ADDR);
12655
12656 if (val & EEPROM_ADDR_COMPLETE)
12657 break;
Michael Chan9d57f012006-12-07 00:23:25 -080012658 msleep(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012659 }
12660 if (!(val & EEPROM_ADDR_COMPLETE)) {
12661 rc = -EBUSY;
12662 break;
12663 }
12664 }
12665
12666 return rc;
12667}
12668
12669/* offset and length are dword aligned */
12670static int tg3_nvram_write_block_unbuffered(struct tg3 *tp, u32 offset, u32 len,
12671 u8 *buf)
12672{
12673 int ret = 0;
12674 u32 pagesize = tp->nvram_pagesize;
12675 u32 pagemask = pagesize - 1;
12676 u32 nvram_cmd;
12677 u8 *tmp;
12678
12679 tmp = kmalloc(pagesize, GFP_KERNEL);
12680 if (tmp == NULL)
12681 return -ENOMEM;
12682
12683 while (len) {
12684 int j;
Michael Chane6af3012005-04-21 17:12:05 -070012685 u32 phy_addr, page_off, size;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012686
12687 phy_addr = offset & ~pagemask;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012688
Linus Torvalds1da177e2005-04-16 15:20:36 -070012689 for (j = 0; j < pagesize; j += 4) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000012690 ret = tg3_nvram_read_be32(tp, phy_addr + j,
12691 (__be32 *) (tmp + j));
12692 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012693 break;
12694 }
12695 if (ret)
12696 break;
12697
Matt Carlsonc6cdf432010-04-05 10:19:26 +000012698 page_off = offset & pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012699 size = pagesize;
12700 if (len < size)
12701 size = len;
12702
12703 len -= size;
12704
12705 memcpy(tmp + page_off, buf, size);
12706
12707 offset = offset + (pagesize - page_off);
12708
Michael Chane6af3012005-04-21 17:12:05 -070012709 tg3_enable_nvram_access(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012710
12711 /*
12712 * Before we can erase the flash page, we need
12713 * to issue a special "write enable" command.
12714 */
12715 nvram_cmd = NVRAM_CMD_WREN | NVRAM_CMD_GO | NVRAM_CMD_DONE;
12716
12717 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
12718 break;
12719
12720 /* Erase the target page */
12721 tw32(NVRAM_ADDR, phy_addr);
12722
12723 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE | NVRAM_CMD_WR |
12724 NVRAM_CMD_FIRST | NVRAM_CMD_LAST | NVRAM_CMD_ERASE;
12725
Matt Carlsonc6cdf432010-04-05 10:19:26 +000012726 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
Linus Torvalds1da177e2005-04-16 15:20:36 -070012727 break;
12728
12729 /* Issue another write enable to start the write. */
12730 nvram_cmd = NVRAM_CMD_WREN | NVRAM_CMD_GO | NVRAM_CMD_DONE;
12731
12732 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
12733 break;
12734
12735 for (j = 0; j < pagesize; j += 4) {
Al Virob9fc7dc2007-12-17 22:59:57 -080012736 __be32 data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012737
Al Virob9fc7dc2007-12-17 22:59:57 -080012738 data = *((__be32 *) (tmp + j));
Matt Carlsona9dc5292009-02-25 14:25:30 +000012739
Al Virob9fc7dc2007-12-17 22:59:57 -080012740 tw32(NVRAM_WRDATA, be32_to_cpu(data));
Linus Torvalds1da177e2005-04-16 15:20:36 -070012741
12742 tw32(NVRAM_ADDR, phy_addr + j);
12743
12744 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE |
12745 NVRAM_CMD_WR;
12746
12747 if (j == 0)
12748 nvram_cmd |= NVRAM_CMD_FIRST;
12749 else if (j == (pagesize - 4))
12750 nvram_cmd |= NVRAM_CMD_LAST;
12751
12752 if ((ret = tg3_nvram_exec_cmd(tp, nvram_cmd)))
12753 break;
12754 }
12755 if (ret)
12756 break;
12757 }
12758
12759 nvram_cmd = NVRAM_CMD_WRDI | NVRAM_CMD_GO | NVRAM_CMD_DONE;
12760 tg3_nvram_exec_cmd(tp, nvram_cmd);
12761
12762 kfree(tmp);
12763
12764 return ret;
12765}
12766
12767/* offset and length are dword aligned */
12768static int tg3_nvram_write_block_buffered(struct tg3 *tp, u32 offset, u32 len,
12769 u8 *buf)
12770{
12771 int i, ret = 0;
12772
12773 for (i = 0; i < len; i += 4, offset += 4) {
Al Virob9fc7dc2007-12-17 22:59:57 -080012774 u32 page_off, phy_addr, nvram_cmd;
12775 __be32 data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012776
12777 memcpy(&data, buf + i, 4);
Al Virob9fc7dc2007-12-17 22:59:57 -080012778 tw32(NVRAM_WRDATA, be32_to_cpu(data));
Linus Torvalds1da177e2005-04-16 15:20:36 -070012779
Matt Carlsonc6cdf432010-04-05 10:19:26 +000012780 page_off = offset % tp->nvram_pagesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012781
Michael Chan18201802006-03-20 22:29:15 -080012782 phy_addr = tg3_nvram_phys_addr(tp, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012783
12784 tw32(NVRAM_ADDR, phy_addr);
12785
12786 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE | NVRAM_CMD_WR;
12787
Matt Carlsonc6cdf432010-04-05 10:19:26 +000012788 if (page_off == 0 || i == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012789 nvram_cmd |= NVRAM_CMD_FIRST;
Michael Chanf6d9a252006-04-29 19:00:24 -070012790 if (page_off == (tp->nvram_pagesize - 4))
Linus Torvalds1da177e2005-04-16 15:20:36 -070012791 nvram_cmd |= NVRAM_CMD_LAST;
12792
12793 if (i == (len - 4))
12794 nvram_cmd |= NVRAM_CMD_LAST;
12795
Matt Carlson321d32a2008-11-21 17:22:19 -080012796 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5752 &&
Joe Perches63c3a662011-04-26 08:12:10 +000012797 !tg3_flag(tp, 5755_PLUS) &&
Michael Chan4c987482005-09-05 17:52:38 -070012798 (tp->nvram_jedecnum == JEDEC_ST) &&
12799 (nvram_cmd & NVRAM_CMD_FIRST)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012800
12801 if ((ret = tg3_nvram_exec_cmd(tp,
12802 NVRAM_CMD_WREN | NVRAM_CMD_GO |
12803 NVRAM_CMD_DONE)))
12804
12805 break;
12806 }
Joe Perches63c3a662011-04-26 08:12:10 +000012807 if (!tg3_flag(tp, FLASH)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012808 /* We always do complete word writes to eeprom. */
12809 nvram_cmd |= (NVRAM_CMD_FIRST | NVRAM_CMD_LAST);
12810 }
12811
12812 if ((ret = tg3_nvram_exec_cmd(tp, nvram_cmd)))
12813 break;
12814 }
12815 return ret;
12816}
12817
12818/* offset and length are dword aligned */
12819static int tg3_nvram_write_block(struct tg3 *tp, u32 offset, u32 len, u8 *buf)
12820{
12821 int ret;
12822
Joe Perches63c3a662011-04-26 08:12:10 +000012823 if (tg3_flag(tp, EEPROM_WRITE_PROT)) {
Michael Chan314fba32005-04-21 17:07:04 -070012824 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl &
12825 ~GRC_LCLCTRL_GPIO_OUTPUT1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012826 udelay(40);
12827 }
12828
Joe Perches63c3a662011-04-26 08:12:10 +000012829 if (!tg3_flag(tp, NVRAM)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012830 ret = tg3_nvram_write_block_using_eeprom(tp, offset, len, buf);
Matt Carlson859a588792010-04-05 10:19:28 +000012831 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012832 u32 grc_mode;
12833
Michael Chanec41c7d2006-01-17 02:40:55 -080012834 ret = tg3_nvram_lock(tp);
12835 if (ret)
12836 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012837
Michael Chane6af3012005-04-21 17:12:05 -070012838 tg3_enable_nvram_access(tp);
Joe Perches63c3a662011-04-26 08:12:10 +000012839 if (tg3_flag(tp, 5750_PLUS) && !tg3_flag(tp, PROTECTED_NVRAM))
Linus Torvalds1da177e2005-04-16 15:20:36 -070012840 tw32(NVRAM_WRITE1, 0x406);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012841
12842 grc_mode = tr32(GRC_MODE);
12843 tw32(GRC_MODE, grc_mode | GRC_MODE_NVRAM_WR_ENABLE);
12844
Joe Perches63c3a662011-04-26 08:12:10 +000012845 if (tg3_flag(tp, NVRAM_BUFFERED) || !tg3_flag(tp, FLASH)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012846 ret = tg3_nvram_write_block_buffered(tp, offset, len,
12847 buf);
Matt Carlson859a588792010-04-05 10:19:28 +000012848 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012849 ret = tg3_nvram_write_block_unbuffered(tp, offset, len,
12850 buf);
12851 }
12852
12853 grc_mode = tr32(GRC_MODE);
12854 tw32(GRC_MODE, grc_mode & ~GRC_MODE_NVRAM_WR_ENABLE);
12855
Michael Chane6af3012005-04-21 17:12:05 -070012856 tg3_disable_nvram_access(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012857 tg3_nvram_unlock(tp);
12858 }
12859
Joe Perches63c3a662011-04-26 08:12:10 +000012860 if (tg3_flag(tp, EEPROM_WRITE_PROT)) {
Michael Chan314fba32005-04-21 17:07:04 -070012861 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012862 udelay(40);
12863 }
12864
12865 return ret;
12866}
12867
12868struct subsys_tbl_ent {
12869 u16 subsys_vendor, subsys_devid;
12870 u32 phy_id;
12871};
12872
Matt Carlson24daf2b2010-02-17 15:17:02 +000012873static struct subsys_tbl_ent subsys_id_to_phy_id[] __devinitdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012874 /* Broadcom boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000012875 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012876 TG3PCI_SUBDEVICE_ID_BROADCOM_95700A6, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012877 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012878 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A5, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012879 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012880 TG3PCI_SUBDEVICE_ID_BROADCOM_95700T6, TG3_PHY_ID_BCM8002 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012881 { TG3PCI_SUBVENDOR_ID_BROADCOM,
12882 TG3PCI_SUBDEVICE_ID_BROADCOM_95700A9, 0 },
12883 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012884 TG3PCI_SUBDEVICE_ID_BROADCOM_95701T1, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012885 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012886 TG3PCI_SUBDEVICE_ID_BROADCOM_95701T8, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012887 { TG3PCI_SUBVENDOR_ID_BROADCOM,
12888 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A7, 0 },
12889 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012890 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A10, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012891 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012892 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A12, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012893 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012894 TG3PCI_SUBDEVICE_ID_BROADCOM_95703AX1, TG3_PHY_ID_BCM5703 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012895 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012896 TG3PCI_SUBDEVICE_ID_BROADCOM_95703AX2, TG3_PHY_ID_BCM5703 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070012897
12898 /* 3com boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000012899 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012900 TG3PCI_SUBDEVICE_ID_3COM_3C996T, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012901 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012902 TG3PCI_SUBDEVICE_ID_3COM_3C996BT, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012903 { TG3PCI_SUBVENDOR_ID_3COM,
12904 TG3PCI_SUBDEVICE_ID_3COM_3C996SX, 0 },
12905 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012906 TG3PCI_SUBDEVICE_ID_3COM_3C1000T, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012907 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012908 TG3PCI_SUBDEVICE_ID_3COM_3C940BR01, TG3_PHY_ID_BCM5701 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070012909
12910 /* DELL boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000012911 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000012912 TG3PCI_SUBDEVICE_ID_DELL_VIPER, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012913 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000012914 TG3PCI_SUBDEVICE_ID_DELL_JAGUAR, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012915 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000012916 TG3PCI_SUBDEVICE_ID_DELL_MERLOT, TG3_PHY_ID_BCM5411 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012917 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000012918 TG3PCI_SUBDEVICE_ID_DELL_SLIM_MERLOT, TG3_PHY_ID_BCM5411 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070012919
12920 /* Compaq boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000012921 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000012922 TG3PCI_SUBDEVICE_ID_COMPAQ_BANSHEE, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012923 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000012924 TG3PCI_SUBDEVICE_ID_COMPAQ_BANSHEE_2, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012925 { TG3PCI_SUBVENDOR_ID_COMPAQ,
12926 TG3PCI_SUBDEVICE_ID_COMPAQ_CHANGELING, 0 },
12927 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000012928 TG3PCI_SUBDEVICE_ID_COMPAQ_NC7780, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012929 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000012930 TG3PCI_SUBDEVICE_ID_COMPAQ_NC7780_2, TG3_PHY_ID_BCM5701 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070012931
12932 /* IBM boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000012933 { TG3PCI_SUBVENDOR_ID_IBM,
12934 TG3PCI_SUBDEVICE_ID_IBM_5703SAX2, 0 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070012935};
12936
Matt Carlson24daf2b2010-02-17 15:17:02 +000012937static struct subsys_tbl_ent * __devinit tg3_lookup_by_subsys(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012938{
12939 int i;
12940
12941 for (i = 0; i < ARRAY_SIZE(subsys_id_to_phy_id); i++) {
12942 if ((subsys_id_to_phy_id[i].subsys_vendor ==
12943 tp->pdev->subsystem_vendor) &&
12944 (subsys_id_to_phy_id[i].subsys_devid ==
12945 tp->pdev->subsystem_device))
12946 return &subsys_id_to_phy_id[i];
12947 }
12948 return NULL;
12949}
12950
Michael Chan7d0c41e2005-04-21 17:06:20 -070012951static void __devinit tg3_get_eeprom_hw_cfg(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012952{
Linus Torvalds1da177e2005-04-16 15:20:36 -070012953 u32 val;
David S. Millerf49639e2006-06-09 11:58:36 -070012954
Matt Carlson79eb6902010-02-17 15:17:03 +000012955 tp->phy_id = TG3_PHY_ID_INVALID;
Michael Chan7d0c41e2005-04-21 17:06:20 -070012956 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
12957
Gary Zambranoa85feb82007-05-05 11:52:19 -070012958 /* Assume an onboard device and WOL capable by default. */
Joe Perches63c3a662011-04-26 08:12:10 +000012959 tg3_flag_set(tp, EEPROM_WRITE_PROT);
12960 tg3_flag_set(tp, WOL_CAP);
David S. Miller72b845e2006-03-14 14:11:48 -080012961
Michael Chanb5d37722006-09-27 16:06:21 -070012962 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan9d26e212006-12-07 00:21:14 -080012963 if (!(tr32(PCIE_TRANSACTION_CFG) & PCIE_TRANS_CFG_LOM)) {
Joe Perches63c3a662011-04-26 08:12:10 +000012964 tg3_flag_clear(tp, EEPROM_WRITE_PROT);
12965 tg3_flag_set(tp, IS_NIC);
Michael Chan9d26e212006-12-07 00:21:14 -080012966 }
Matt Carlson0527ba32007-10-10 18:03:30 -070012967 val = tr32(VCPU_CFGSHDW);
12968 if (val & VCPU_CFGSHDW_ASPM_DBNC)
Joe Perches63c3a662011-04-26 08:12:10 +000012969 tg3_flag_set(tp, ASPM_WORKAROUND);
Matt Carlson0527ba32007-10-10 18:03:30 -070012970 if ((val & VCPU_CFGSHDW_WOL_ENABLE) &&
Rafael J. Wysocki6fdbab92011-04-28 11:02:15 +000012971 (val & VCPU_CFGSHDW_WOL_MAGPKT)) {
Joe Perches63c3a662011-04-26 08:12:10 +000012972 tg3_flag_set(tp, WOL_ENABLE);
Rafael J. Wysocki6fdbab92011-04-28 11:02:15 +000012973 device_set_wakeup_enable(&tp->pdev->dev, true);
12974 }
Matt Carlson05ac4cb2008-11-03 16:53:46 -080012975 goto done;
Michael Chanb5d37722006-09-27 16:06:21 -070012976 }
12977
Linus Torvalds1da177e2005-04-16 15:20:36 -070012978 tg3_read_mem(tp, NIC_SRAM_DATA_SIG, &val);
12979 if (val == NIC_SRAM_DATA_SIG_MAGIC) {
12980 u32 nic_cfg, led_cfg;
Matt Carlsona9daf362008-05-25 23:49:44 -070012981 u32 nic_phy_id, ver, cfg2 = 0, cfg4 = 0, eeprom_phy_id;
Michael Chan7d0c41e2005-04-21 17:06:20 -070012982 int eeprom_phy_serdes = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012983
12984 tg3_read_mem(tp, NIC_SRAM_DATA_CFG, &nic_cfg);
12985 tp->nic_sram_data_cfg = nic_cfg;
12986
12987 tg3_read_mem(tp, NIC_SRAM_DATA_VER, &ver);
12988 ver >>= NIC_SRAM_DATA_VER_SHIFT;
Matt Carlson6ff6f812011-05-19 12:12:54 +000012989 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
12990 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701 &&
12991 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5703 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070012992 (ver > 0) && (ver < 0x100))
12993 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_2, &cfg2);
12994
Matt Carlsona9daf362008-05-25 23:49:44 -070012995 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
12996 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_4, &cfg4);
12997
Linus Torvalds1da177e2005-04-16 15:20:36 -070012998 if ((nic_cfg & NIC_SRAM_DATA_CFG_PHY_TYPE_MASK) ==
12999 NIC_SRAM_DATA_CFG_PHY_TYPE_FIBER)
13000 eeprom_phy_serdes = 1;
13001
13002 tg3_read_mem(tp, NIC_SRAM_DATA_PHY_ID, &nic_phy_id);
13003 if (nic_phy_id != 0) {
13004 u32 id1 = nic_phy_id & NIC_SRAM_DATA_PHY_ID1_MASK;
13005 u32 id2 = nic_phy_id & NIC_SRAM_DATA_PHY_ID2_MASK;
13006
13007 eeprom_phy_id = (id1 >> 16) << 10;
13008 eeprom_phy_id |= (id2 & 0xfc00) << 16;
13009 eeprom_phy_id |= (id2 & 0x03ff) << 0;
13010 } else
13011 eeprom_phy_id = 0;
13012
Michael Chan7d0c41e2005-04-21 17:06:20 -070013013 tp->phy_id = eeprom_phy_id;
Michael Chan747e8f82005-07-25 12:33:22 -070013014 if (eeprom_phy_serdes) {
Joe Perches63c3a662011-04-26 08:12:10 +000013015 if (!tg3_flag(tp, 5705_PLUS))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013016 tp->phy_flags |= TG3_PHYFLG_PHY_SERDES;
Matt Carlsona50d0792010-06-05 17:24:37 +000013017 else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013018 tp->phy_flags |= TG3_PHYFLG_MII_SERDES;
Michael Chan747e8f82005-07-25 12:33:22 -070013019 }
Michael Chan7d0c41e2005-04-21 17:06:20 -070013020
Joe Perches63c3a662011-04-26 08:12:10 +000013021 if (tg3_flag(tp, 5750_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -070013022 led_cfg = cfg2 & (NIC_SRAM_DATA_CFG_LED_MODE_MASK |
13023 SHASTA_EXT_LED_MODE_MASK);
John W. Linvillecbf46852005-04-21 17:01:29 -070013024 else
Linus Torvalds1da177e2005-04-16 15:20:36 -070013025 led_cfg = nic_cfg & NIC_SRAM_DATA_CFG_LED_MODE_MASK;
13026
13027 switch (led_cfg) {
13028 default:
13029 case NIC_SRAM_DATA_CFG_LED_MODE_PHY_1:
13030 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
13031 break;
13032
13033 case NIC_SRAM_DATA_CFG_LED_MODE_PHY_2:
13034 tp->led_ctrl = LED_CTRL_MODE_PHY_2;
13035 break;
13036
13037 case NIC_SRAM_DATA_CFG_LED_MODE_MAC:
13038 tp->led_ctrl = LED_CTRL_MODE_MAC;
Michael Chan9ba27792005-06-06 15:16:20 -070013039
13040 /* Default to PHY_1_MODE if 0 (MAC_MODE) is
13041 * read on some older 5700/5701 bootcode.
13042 */
13043 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
13044 ASIC_REV_5700 ||
13045 GET_ASIC_REV(tp->pci_chip_rev_id) ==
13046 ASIC_REV_5701)
13047 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
13048
Linus Torvalds1da177e2005-04-16 15:20:36 -070013049 break;
13050
13051 case SHASTA_EXT_LED_SHARED:
13052 tp->led_ctrl = LED_CTRL_MODE_SHARED;
13053 if (tp->pci_chip_rev_id != CHIPREV_ID_5750_A0 &&
13054 tp->pci_chip_rev_id != CHIPREV_ID_5750_A1)
13055 tp->led_ctrl |= (LED_CTRL_MODE_PHY_1 |
13056 LED_CTRL_MODE_PHY_2);
13057 break;
13058
13059 case SHASTA_EXT_LED_MAC:
13060 tp->led_ctrl = LED_CTRL_MODE_SHASTA_MAC;
13061 break;
13062
13063 case SHASTA_EXT_LED_COMBO:
13064 tp->led_ctrl = LED_CTRL_MODE_COMBO;
13065 if (tp->pci_chip_rev_id != CHIPREV_ID_5750_A0)
13066 tp->led_ctrl |= (LED_CTRL_MODE_PHY_1 |
13067 LED_CTRL_MODE_PHY_2);
13068 break;
13069
Stephen Hemminger855e1112008-04-16 16:37:28 -070013070 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013071
13072 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
13073 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) &&
13074 tp->pdev->subsystem_vendor == PCI_VENDOR_ID_DELL)
13075 tp->led_ctrl = LED_CTRL_MODE_PHY_2;
13076
Matt Carlsonb2a5c192008-04-03 21:44:44 -070013077 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX)
13078 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
Matt Carlson5f608912007-11-12 21:17:07 -080013079
Michael Chan9d26e212006-12-07 00:21:14 -080013080 if (nic_cfg & NIC_SRAM_DATA_CFG_EEPROM_WP) {
Joe Perches63c3a662011-04-26 08:12:10 +000013081 tg3_flag_set(tp, EEPROM_WRITE_PROT);
Michael Chan9d26e212006-12-07 00:21:14 -080013082 if ((tp->pdev->subsystem_vendor ==
13083 PCI_VENDOR_ID_ARIMA) &&
13084 (tp->pdev->subsystem_device == 0x205a ||
13085 tp->pdev->subsystem_device == 0x2063))
Joe Perches63c3a662011-04-26 08:12:10 +000013086 tg3_flag_clear(tp, EEPROM_WRITE_PROT);
Michael Chan9d26e212006-12-07 00:21:14 -080013087 } else {
Joe Perches63c3a662011-04-26 08:12:10 +000013088 tg3_flag_clear(tp, EEPROM_WRITE_PROT);
13089 tg3_flag_set(tp, IS_NIC);
Michael Chan9d26e212006-12-07 00:21:14 -080013090 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013091
13092 if (nic_cfg & NIC_SRAM_DATA_CFG_ASF_ENABLE) {
Joe Perches63c3a662011-04-26 08:12:10 +000013093 tg3_flag_set(tp, ENABLE_ASF);
13094 if (tg3_flag(tp, 5750_PLUS))
13095 tg3_flag_set(tp, ASF_NEW_HANDSHAKE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013096 }
Matt Carlsonb2b98d42008-11-03 16:52:32 -080013097
13098 if ((nic_cfg & NIC_SRAM_DATA_CFG_APE_ENABLE) &&
Joe Perches63c3a662011-04-26 08:12:10 +000013099 tg3_flag(tp, 5750_PLUS))
13100 tg3_flag_set(tp, ENABLE_APE);
Matt Carlsonb2b98d42008-11-03 16:52:32 -080013101
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013102 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES &&
Gary Zambranoa85feb82007-05-05 11:52:19 -070013103 !(nic_cfg & NIC_SRAM_DATA_CFG_FIBER_WOL))
Joe Perches63c3a662011-04-26 08:12:10 +000013104 tg3_flag_clear(tp, WOL_CAP);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013105
Joe Perches63c3a662011-04-26 08:12:10 +000013106 if (tg3_flag(tp, WOL_CAP) &&
Rafael J. Wysocki6fdbab92011-04-28 11:02:15 +000013107 (nic_cfg & NIC_SRAM_DATA_CFG_WOL_ENABLE)) {
Joe Perches63c3a662011-04-26 08:12:10 +000013108 tg3_flag_set(tp, WOL_ENABLE);
Rafael J. Wysocki6fdbab92011-04-28 11:02:15 +000013109 device_set_wakeup_enable(&tp->pdev->dev, true);
13110 }
Matt Carlson0527ba32007-10-10 18:03:30 -070013111
Linus Torvalds1da177e2005-04-16 15:20:36 -070013112 if (cfg2 & (1 << 17))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013113 tp->phy_flags |= TG3_PHYFLG_CAPACITIVE_COUPLING;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013114
13115 /* serdes signal pre-emphasis in register 0x590 set by */
13116 /* bootcode if bit 18 is set */
13117 if (cfg2 & (1 << 18))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013118 tp->phy_flags |= TG3_PHYFLG_SERDES_PREEMPHASIS;
Matt Carlson8ed5d972007-05-07 00:25:49 -070013119
Joe Perches63c3a662011-04-26 08:12:10 +000013120 if ((tg3_flag(tp, 57765_PLUS) ||
13121 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
13122 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX)) &&
Matt Carlson6833c042008-11-21 17:18:59 -080013123 (cfg2 & NIC_SRAM_DATA_CFG_2_APD_EN))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013124 tp->phy_flags |= TG3_PHYFLG_ENABLE_APD;
Matt Carlson6833c042008-11-21 17:18:59 -080013125
Joe Perches63c3a662011-04-26 08:12:10 +000013126 if (tg3_flag(tp, PCI_EXPRESS) &&
Matt Carlson8c69b1e2010-08-02 11:26:00 +000013127 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
Joe Perches63c3a662011-04-26 08:12:10 +000013128 !tg3_flag(tp, 57765_PLUS)) {
Matt Carlson8ed5d972007-05-07 00:25:49 -070013129 u32 cfg3;
13130
13131 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_3, &cfg3);
13132 if (cfg3 & NIC_SRAM_ASPM_DEBOUNCE)
Joe Perches63c3a662011-04-26 08:12:10 +000013133 tg3_flag_set(tp, ASPM_WORKAROUND);
Matt Carlson8ed5d972007-05-07 00:25:49 -070013134 }
Matt Carlsona9daf362008-05-25 23:49:44 -070013135
Matt Carlson14417062010-02-17 15:16:59 +000013136 if (cfg4 & NIC_SRAM_RGMII_INBAND_DISABLE)
Joe Perches63c3a662011-04-26 08:12:10 +000013137 tg3_flag_set(tp, RGMII_INBAND_DISABLE);
Matt Carlsona9daf362008-05-25 23:49:44 -070013138 if (cfg4 & NIC_SRAM_RGMII_EXT_IBND_RX_EN)
Joe Perches63c3a662011-04-26 08:12:10 +000013139 tg3_flag_set(tp, RGMII_EXT_IBND_RX_EN);
Matt Carlsona9daf362008-05-25 23:49:44 -070013140 if (cfg4 & NIC_SRAM_RGMII_EXT_IBND_TX_EN)
Joe Perches63c3a662011-04-26 08:12:10 +000013141 tg3_flag_set(tp, RGMII_EXT_IBND_TX_EN);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013142 }
Matt Carlson05ac4cb2008-11-03 16:53:46 -080013143done:
Joe Perches63c3a662011-04-26 08:12:10 +000013144 if (tg3_flag(tp, WOL_CAP))
Rafael J. Wysocki43067ed2011-02-10 06:53:09 +000013145 device_set_wakeup_enable(&tp->pdev->dev,
Joe Perches63c3a662011-04-26 08:12:10 +000013146 tg3_flag(tp, WOL_ENABLE));
Rafael J. Wysocki43067ed2011-02-10 06:53:09 +000013147 else
13148 device_set_wakeup_capable(&tp->pdev->dev, false);
Michael Chan7d0c41e2005-04-21 17:06:20 -070013149}
13150
Matt Carlsonb2a5c192008-04-03 21:44:44 -070013151static int __devinit tg3_issue_otp_command(struct tg3 *tp, u32 cmd)
13152{
13153 int i;
13154 u32 val;
13155
13156 tw32(OTP_CTRL, cmd | OTP_CTRL_OTP_CMD_START);
13157 tw32(OTP_CTRL, cmd);
13158
13159 /* Wait for up to 1 ms for command to execute. */
13160 for (i = 0; i < 100; i++) {
13161 val = tr32(OTP_STATUS);
13162 if (val & OTP_STATUS_CMD_DONE)
13163 break;
13164 udelay(10);
13165 }
13166
13167 return (val & OTP_STATUS_CMD_DONE) ? 0 : -EBUSY;
13168}
13169
13170/* Read the gphy configuration from the OTP region of the chip. The gphy
13171 * configuration is a 32-bit value that straddles the alignment boundary.
13172 * We do two 32-bit reads and then shift and merge the results.
13173 */
13174static u32 __devinit tg3_read_otp_phycfg(struct tg3 *tp)
13175{
13176 u32 bhalf_otp, thalf_otp;
13177
13178 tw32(OTP_MODE, OTP_MODE_OTP_THRU_GRC);
13179
13180 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_INIT))
13181 return 0;
13182
13183 tw32(OTP_ADDRESS, OTP_ADDRESS_MAGIC1);
13184
13185 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_READ))
13186 return 0;
13187
13188 thalf_otp = tr32(OTP_READ_DATA);
13189
13190 tw32(OTP_ADDRESS, OTP_ADDRESS_MAGIC2);
13191
13192 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_READ))
13193 return 0;
13194
13195 bhalf_otp = tr32(OTP_READ_DATA);
13196
13197 return ((thalf_otp & 0x0000ffff) << 16) | (bhalf_otp >> 16);
13198}
13199
Matt Carlsone256f8a2011-03-09 16:58:24 +000013200static void __devinit tg3_phy_init_link_config(struct tg3 *tp)
13201{
13202 u32 adv = ADVERTISED_Autoneg |
13203 ADVERTISED_Pause;
13204
13205 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
13206 adv |= ADVERTISED_1000baseT_Half |
13207 ADVERTISED_1000baseT_Full;
13208
13209 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
13210 adv |= ADVERTISED_100baseT_Half |
13211 ADVERTISED_100baseT_Full |
13212 ADVERTISED_10baseT_Half |
13213 ADVERTISED_10baseT_Full |
13214 ADVERTISED_TP;
13215 else
13216 adv |= ADVERTISED_FIBRE;
13217
13218 tp->link_config.advertising = adv;
13219 tp->link_config.speed = SPEED_INVALID;
13220 tp->link_config.duplex = DUPLEX_INVALID;
13221 tp->link_config.autoneg = AUTONEG_ENABLE;
13222 tp->link_config.active_speed = SPEED_INVALID;
13223 tp->link_config.active_duplex = DUPLEX_INVALID;
13224 tp->link_config.orig_speed = SPEED_INVALID;
13225 tp->link_config.orig_duplex = DUPLEX_INVALID;
13226 tp->link_config.orig_autoneg = AUTONEG_INVALID;
13227}
13228
Michael Chan7d0c41e2005-04-21 17:06:20 -070013229static int __devinit tg3_phy_probe(struct tg3 *tp)
13230{
13231 u32 hw_phy_id_1, hw_phy_id_2;
13232 u32 hw_phy_id, hw_phy_id_masked;
13233 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013234
Matt Carlsone256f8a2011-03-09 16:58:24 +000013235 /* flow control autonegotiation is default behavior */
Joe Perches63c3a662011-04-26 08:12:10 +000013236 tg3_flag_set(tp, PAUSE_AUTONEG);
Matt Carlsone256f8a2011-03-09 16:58:24 +000013237 tp->link_config.flowctrl = FLOW_CTRL_TX | FLOW_CTRL_RX;
13238
Joe Perches63c3a662011-04-26 08:12:10 +000013239 if (tg3_flag(tp, USE_PHYLIB))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070013240 return tg3_phy_init(tp);
13241
Linus Torvalds1da177e2005-04-16 15:20:36 -070013242 /* Reading the PHY ID register can conflict with ASF
Nick Andrew877d0312009-01-26 11:06:57 +010013243 * firmware access to the PHY hardware.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013244 */
13245 err = 0;
Joe Perches63c3a662011-04-26 08:12:10 +000013246 if (tg3_flag(tp, ENABLE_ASF) || tg3_flag(tp, ENABLE_APE)) {
Matt Carlson79eb6902010-02-17 15:17:03 +000013247 hw_phy_id = hw_phy_id_masked = TG3_PHY_ID_INVALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013248 } else {
13249 /* Now read the physical PHY_ID from the chip and verify
13250 * that it is sane. If it doesn't look good, we fall back
13251 * to either the hard-coded table based PHY_ID and failing
13252 * that the value found in the eeprom area.
13253 */
13254 err |= tg3_readphy(tp, MII_PHYSID1, &hw_phy_id_1);
13255 err |= tg3_readphy(tp, MII_PHYSID2, &hw_phy_id_2);
13256
13257 hw_phy_id = (hw_phy_id_1 & 0xffff) << 10;
13258 hw_phy_id |= (hw_phy_id_2 & 0xfc00) << 16;
13259 hw_phy_id |= (hw_phy_id_2 & 0x03ff) << 0;
13260
Matt Carlson79eb6902010-02-17 15:17:03 +000013261 hw_phy_id_masked = hw_phy_id & TG3_PHY_ID_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013262 }
13263
Matt Carlson79eb6902010-02-17 15:17:03 +000013264 if (!err && TG3_KNOWN_PHY_ID(hw_phy_id_masked)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070013265 tp->phy_id = hw_phy_id;
Matt Carlson79eb6902010-02-17 15:17:03 +000013266 if (hw_phy_id_masked == TG3_PHY_ID_BCM8002)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013267 tp->phy_flags |= TG3_PHYFLG_PHY_SERDES;
Michael Chanda6b2d02005-08-19 12:54:29 -070013268 else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013269 tp->phy_flags &= ~TG3_PHYFLG_PHY_SERDES;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013270 } else {
Matt Carlson79eb6902010-02-17 15:17:03 +000013271 if (tp->phy_id != TG3_PHY_ID_INVALID) {
Michael Chan7d0c41e2005-04-21 17:06:20 -070013272 /* Do nothing, phy ID already set up in
13273 * tg3_get_eeprom_hw_cfg().
13274 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070013275 } else {
13276 struct subsys_tbl_ent *p;
13277
13278 /* No eeprom signature? Try the hardcoded
13279 * subsys device table.
13280 */
Matt Carlson24daf2b2010-02-17 15:17:02 +000013281 p = tg3_lookup_by_subsys(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013282 if (!p)
13283 return -ENODEV;
13284
13285 tp->phy_id = p->phy_id;
13286 if (!tp->phy_id ||
Matt Carlson79eb6902010-02-17 15:17:03 +000013287 tp->phy_id == TG3_PHY_ID_BCM8002)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013288 tp->phy_flags |= TG3_PHYFLG_PHY_SERDES;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013289 }
13290 }
13291
Matt Carlsona6b68da2010-12-06 08:28:52 +000013292 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES) &&
Matt Carlson5baa5e92011-07-20 10:20:53 +000013293 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
13294 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720 ||
13295 (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718 &&
Matt Carlsona6b68da2010-12-06 08:28:52 +000013296 tp->pci_chip_rev_id != CHIPREV_ID_5717_A0) ||
13297 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765 &&
13298 tp->pci_chip_rev_id != CHIPREV_ID_57765_A0)))
Matt Carlson52b02d02010-10-14 10:37:41 +000013299 tp->phy_flags |= TG3_PHYFLG_EEE_CAP;
13300
Matt Carlsone256f8a2011-03-09 16:58:24 +000013301 tg3_phy_init_link_config(tp);
13302
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013303 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES) &&
Joe Perches63c3a662011-04-26 08:12:10 +000013304 !tg3_flag(tp, ENABLE_APE) &&
13305 !tg3_flag(tp, ENABLE_ASF)) {
Matt Carlson42b64a42011-05-19 12:12:49 +000013306 u32 bmsr, mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013307
13308 tg3_readphy(tp, MII_BMSR, &bmsr);
13309 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
13310 (bmsr & BMSR_LSTATUS))
13311 goto skip_phy_reset;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040013312
Linus Torvalds1da177e2005-04-16 15:20:36 -070013313 err = tg3_phy_reset(tp);
13314 if (err)
13315 return err;
13316
Matt Carlson42b64a42011-05-19 12:12:49 +000013317 tg3_phy_set_wirespeed(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013318
Michael Chan3600d912006-12-07 00:21:48 -080013319 mask = (ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
13320 ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full |
13321 ADVERTISED_1000baseT_Half | ADVERTISED_1000baseT_Full);
13322 if (!tg3_copper_is_advertising_all(tp, mask)) {
Matt Carlson42b64a42011-05-19 12:12:49 +000013323 tg3_phy_autoneg_cfg(tp, tp->link_config.advertising,
13324 tp->link_config.flowctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013325
13326 tg3_writephy(tp, MII_BMCR,
13327 BMCR_ANENABLE | BMCR_ANRESTART);
13328 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013329 }
13330
13331skip_phy_reset:
Matt Carlson79eb6902010-02-17 15:17:03 +000013332 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070013333 err = tg3_init_5401phy_dsp(tp);
13334 if (err)
13335 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013336
Linus Torvalds1da177e2005-04-16 15:20:36 -070013337 err = tg3_init_5401phy_dsp(tp);
13338 }
13339
Linus Torvalds1da177e2005-04-16 15:20:36 -070013340 return err;
13341}
13342
Matt Carlson184b8902010-04-05 10:19:25 +000013343static void __devinit tg3_read_vpd(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013344{
Matt Carlsona4a8bb12010-09-15 09:00:00 +000013345 u8 *vpd_data;
Matt Carlson4181b2c2010-02-26 14:04:45 +000013346 unsigned int block_end, rosize, len;
Matt Carlson535a4902011-07-20 10:20:56 +000013347 u32 vpdlen;
Matt Carlson184b8902010-04-05 10:19:25 +000013348 int j, i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013349
Matt Carlson535a4902011-07-20 10:20:56 +000013350 vpd_data = (u8 *)tg3_vpd_readblock(tp, &vpdlen);
Matt Carlsona4a8bb12010-09-15 09:00:00 +000013351 if (!vpd_data)
13352 goto out_no_vpd;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013353
Matt Carlson535a4902011-07-20 10:20:56 +000013354 i = pci_vpd_find_tag(vpd_data, 0, vpdlen, PCI_VPD_LRDT_RO_DATA);
Matt Carlson4181b2c2010-02-26 14:04:45 +000013355 if (i < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013356 goto out_not_found;
Matt Carlson4181b2c2010-02-26 14:04:45 +000013357
13358 rosize = pci_vpd_lrdt_size(&vpd_data[i]);
13359 block_end = i + PCI_VPD_LRDT_TAG_SIZE + rosize;
13360 i += PCI_VPD_LRDT_TAG_SIZE;
13361
Matt Carlson535a4902011-07-20 10:20:56 +000013362 if (block_end > vpdlen)
Matt Carlson4181b2c2010-02-26 14:04:45 +000013363 goto out_not_found;
13364
Matt Carlson184b8902010-04-05 10:19:25 +000013365 j = pci_vpd_find_info_keyword(vpd_data, i, rosize,
13366 PCI_VPD_RO_KEYWORD_MFR_ID);
13367 if (j > 0) {
13368 len = pci_vpd_info_field_size(&vpd_data[j]);
13369
13370 j += PCI_VPD_INFO_FLD_HDR_SIZE;
13371 if (j + len > block_end || len != 4 ||
13372 memcmp(&vpd_data[j], "1028", 4))
13373 goto partno;
13374
13375 j = pci_vpd_find_info_keyword(vpd_data, i, rosize,
13376 PCI_VPD_RO_KEYWORD_VENDOR0);
13377 if (j < 0)
13378 goto partno;
13379
13380 len = pci_vpd_info_field_size(&vpd_data[j]);
13381
13382 j += PCI_VPD_INFO_FLD_HDR_SIZE;
13383 if (j + len > block_end)
13384 goto partno;
13385
13386 memcpy(tp->fw_ver, &vpd_data[j], len);
Matt Carlson535a4902011-07-20 10:20:56 +000013387 strncat(tp->fw_ver, " bc ", vpdlen - len - 1);
Matt Carlson184b8902010-04-05 10:19:25 +000013388 }
13389
13390partno:
Matt Carlson4181b2c2010-02-26 14:04:45 +000013391 i = pci_vpd_find_info_keyword(vpd_data, i, rosize,
13392 PCI_VPD_RO_KEYWORD_PARTNO);
13393 if (i < 0)
13394 goto out_not_found;
13395
13396 len = pci_vpd_info_field_size(&vpd_data[i]);
13397
13398 i += PCI_VPD_INFO_FLD_HDR_SIZE;
13399 if (len > TG3_BPN_SIZE ||
Matt Carlson535a4902011-07-20 10:20:56 +000013400 (len + i) > vpdlen)
Matt Carlson4181b2c2010-02-26 14:04:45 +000013401 goto out_not_found;
13402
13403 memcpy(tp->board_part_number, &vpd_data[i], len);
13404
Linus Torvalds1da177e2005-04-16 15:20:36 -070013405out_not_found:
Matt Carlsona4a8bb12010-09-15 09:00:00 +000013406 kfree(vpd_data);
Matt Carlson37a949c2010-09-30 10:34:33 +000013407 if (tp->board_part_number[0])
Matt Carlsona4a8bb12010-09-15 09:00:00 +000013408 return;
13409
13410out_no_vpd:
Matt Carlson37a949c2010-09-30 10:34:33 +000013411 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717) {
13412 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717)
13413 strcpy(tp->board_part_number, "BCM5717");
13414 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718)
13415 strcpy(tp->board_part_number, "BCM5718");
13416 else
13417 goto nomatch;
13418 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780) {
13419 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57780)
13420 strcpy(tp->board_part_number, "BCM57780");
13421 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57760)
13422 strcpy(tp->board_part_number, "BCM57760");
13423 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57790)
13424 strcpy(tp->board_part_number, "BCM57790");
13425 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57788)
13426 strcpy(tp->board_part_number, "BCM57788");
13427 else
13428 goto nomatch;
13429 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765) {
13430 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57761)
13431 strcpy(tp->board_part_number, "BCM57761");
13432 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57765)
13433 strcpy(tp->board_part_number, "BCM57765");
13434 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57781)
13435 strcpy(tp->board_part_number, "BCM57781");
13436 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57785)
13437 strcpy(tp->board_part_number, "BCM57785");
13438 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57791)
13439 strcpy(tp->board_part_number, "BCM57791");
13440 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57795)
13441 strcpy(tp->board_part_number, "BCM57795");
13442 else
13443 goto nomatch;
13444 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chanb5d37722006-09-27 16:06:21 -070013445 strcpy(tp->board_part_number, "BCM95906");
Matt Carlson37a949c2010-09-30 10:34:33 +000013446 } else {
13447nomatch:
Michael Chanb5d37722006-09-27 16:06:21 -070013448 strcpy(tp->board_part_number, "none");
Matt Carlson37a949c2010-09-30 10:34:33 +000013449 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013450}
13451
Matt Carlson9c8a6202007-10-21 16:16:08 -070013452static int __devinit tg3_fw_img_is_valid(struct tg3 *tp, u32 offset)
13453{
13454 u32 val;
13455
Matt Carlsone4f34112009-02-25 14:25:00 +000013456 if (tg3_nvram_read(tp, offset, &val) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070013457 (val & 0xfc000000) != 0x0c000000 ||
Matt Carlsone4f34112009-02-25 14:25:00 +000013458 tg3_nvram_read(tp, offset + 4, &val) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070013459 val != 0)
13460 return 0;
13461
13462 return 1;
13463}
13464
Matt Carlsonacd9c112009-02-25 14:26:33 +000013465static void __devinit tg3_read_bc_ver(struct tg3 *tp)
13466{
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013467 u32 val, offset, start, ver_offset;
Matt Carlson75f99362010-04-05 10:19:24 +000013468 int i, dst_off;
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013469 bool newver = false;
Matt Carlsonacd9c112009-02-25 14:26:33 +000013470
13471 if (tg3_nvram_read(tp, 0xc, &offset) ||
13472 tg3_nvram_read(tp, 0x4, &start))
13473 return;
13474
13475 offset = tg3_nvram_logical_addr(tp, offset);
13476
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013477 if (tg3_nvram_read(tp, offset, &val))
Matt Carlsonacd9c112009-02-25 14:26:33 +000013478 return;
13479
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013480 if ((val & 0xfc000000) == 0x0c000000) {
13481 if (tg3_nvram_read(tp, offset + 4, &val))
Matt Carlsonacd9c112009-02-25 14:26:33 +000013482 return;
13483
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013484 if (val == 0)
13485 newver = true;
13486 }
13487
Matt Carlson75f99362010-04-05 10:19:24 +000013488 dst_off = strlen(tp->fw_ver);
13489
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013490 if (newver) {
Matt Carlson75f99362010-04-05 10:19:24 +000013491 if (TG3_VER_SIZE - dst_off < 16 ||
13492 tg3_nvram_read(tp, offset + 8, &ver_offset))
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013493 return;
13494
13495 offset = offset + ver_offset - start;
13496 for (i = 0; i < 16; i += 4) {
13497 __be32 v;
13498 if (tg3_nvram_read_be32(tp, offset + i, &v))
13499 return;
13500
Matt Carlson75f99362010-04-05 10:19:24 +000013501 memcpy(tp->fw_ver + dst_off + i, &v, sizeof(v));
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013502 }
13503 } else {
13504 u32 major, minor;
13505
13506 if (tg3_nvram_read(tp, TG3_NVM_PTREV_BCVER, &ver_offset))
13507 return;
13508
13509 major = (ver_offset & TG3_NVM_BCVER_MAJMSK) >>
13510 TG3_NVM_BCVER_MAJSFT;
13511 minor = ver_offset & TG3_NVM_BCVER_MINMSK;
Matt Carlson75f99362010-04-05 10:19:24 +000013512 snprintf(&tp->fw_ver[dst_off], TG3_VER_SIZE - dst_off,
13513 "v%d.%02d", major, minor);
Matt Carlsonacd9c112009-02-25 14:26:33 +000013514 }
13515}
13516
Matt Carlsona6f6cb12009-02-25 14:27:43 +000013517static void __devinit tg3_read_hwsb_ver(struct tg3 *tp)
13518{
13519 u32 val, major, minor;
13520
13521 /* Use native endian representation */
13522 if (tg3_nvram_read(tp, TG3_NVM_HWSB_CFG1, &val))
13523 return;
13524
13525 major = (val & TG3_NVM_HWSB_CFG1_MAJMSK) >>
13526 TG3_NVM_HWSB_CFG1_MAJSFT;
13527 minor = (val & TG3_NVM_HWSB_CFG1_MINMSK) >>
13528 TG3_NVM_HWSB_CFG1_MINSFT;
13529
13530 snprintf(&tp->fw_ver[0], 32, "sb v%d.%02d", major, minor);
13531}
13532
Matt Carlsondfe00d72008-11-21 17:19:41 -080013533static void __devinit tg3_read_sb_ver(struct tg3 *tp, u32 val)
13534{
13535 u32 offset, major, minor, build;
13536
Matt Carlson75f99362010-04-05 10:19:24 +000013537 strncat(tp->fw_ver, "sb", TG3_VER_SIZE - strlen(tp->fw_ver) - 1);
Matt Carlsondfe00d72008-11-21 17:19:41 -080013538
13539 if ((val & TG3_EEPROM_SB_FORMAT_MASK) != TG3_EEPROM_SB_FORMAT_1)
13540 return;
13541
13542 switch (val & TG3_EEPROM_SB_REVISION_MASK) {
13543 case TG3_EEPROM_SB_REVISION_0:
13544 offset = TG3_EEPROM_SB_F1R0_EDH_OFF;
13545 break;
13546 case TG3_EEPROM_SB_REVISION_2:
13547 offset = TG3_EEPROM_SB_F1R2_EDH_OFF;
13548 break;
13549 case TG3_EEPROM_SB_REVISION_3:
13550 offset = TG3_EEPROM_SB_F1R3_EDH_OFF;
13551 break;
Matt Carlsona4153d42010-02-17 15:16:56 +000013552 case TG3_EEPROM_SB_REVISION_4:
13553 offset = TG3_EEPROM_SB_F1R4_EDH_OFF;
13554 break;
13555 case TG3_EEPROM_SB_REVISION_5:
13556 offset = TG3_EEPROM_SB_F1R5_EDH_OFF;
13557 break;
Matt Carlsonbba226a2010-10-14 10:37:38 +000013558 case TG3_EEPROM_SB_REVISION_6:
13559 offset = TG3_EEPROM_SB_F1R6_EDH_OFF;
13560 break;
Matt Carlsondfe00d72008-11-21 17:19:41 -080013561 default:
13562 return;
13563 }
13564
Matt Carlsone4f34112009-02-25 14:25:00 +000013565 if (tg3_nvram_read(tp, offset, &val))
Matt Carlsondfe00d72008-11-21 17:19:41 -080013566 return;
13567
13568 build = (val & TG3_EEPROM_SB_EDH_BLD_MASK) >>
13569 TG3_EEPROM_SB_EDH_BLD_SHFT;
13570 major = (val & TG3_EEPROM_SB_EDH_MAJ_MASK) >>
13571 TG3_EEPROM_SB_EDH_MAJ_SHFT;
13572 minor = val & TG3_EEPROM_SB_EDH_MIN_MASK;
13573
13574 if (minor > 99 || build > 26)
13575 return;
13576
Matt Carlson75f99362010-04-05 10:19:24 +000013577 offset = strlen(tp->fw_ver);
13578 snprintf(&tp->fw_ver[offset], TG3_VER_SIZE - offset,
13579 " v%d.%02d", major, minor);
Matt Carlsondfe00d72008-11-21 17:19:41 -080013580
13581 if (build > 0) {
Matt Carlson75f99362010-04-05 10:19:24 +000013582 offset = strlen(tp->fw_ver);
13583 if (offset < TG3_VER_SIZE - 1)
13584 tp->fw_ver[offset] = 'a' + build - 1;
Matt Carlsondfe00d72008-11-21 17:19:41 -080013585 }
13586}
13587
Matt Carlsonacd9c112009-02-25 14:26:33 +000013588static void __devinit tg3_read_mgmtfw_ver(struct tg3 *tp)
Michael Chanc4e65752006-03-20 22:29:32 -080013589{
13590 u32 val, offset, start;
Matt Carlsonacd9c112009-02-25 14:26:33 +000013591 int i, vlen;
Matt Carlson9c8a6202007-10-21 16:16:08 -070013592
13593 for (offset = TG3_NVM_DIR_START;
13594 offset < TG3_NVM_DIR_END;
13595 offset += TG3_NVM_DIRENT_SIZE) {
Matt Carlsone4f34112009-02-25 14:25:00 +000013596 if (tg3_nvram_read(tp, offset, &val))
Matt Carlson9c8a6202007-10-21 16:16:08 -070013597 return;
13598
13599 if ((val >> TG3_NVM_DIRTYPE_SHIFT) == TG3_NVM_DIRTYPE_ASFINI)
13600 break;
13601 }
13602
13603 if (offset == TG3_NVM_DIR_END)
13604 return;
13605
Joe Perches63c3a662011-04-26 08:12:10 +000013606 if (!tg3_flag(tp, 5705_PLUS))
Matt Carlson9c8a6202007-10-21 16:16:08 -070013607 start = 0x08000000;
Matt Carlsone4f34112009-02-25 14:25:00 +000013608 else if (tg3_nvram_read(tp, offset - 4, &start))
Matt Carlson9c8a6202007-10-21 16:16:08 -070013609 return;
13610
Matt Carlsone4f34112009-02-25 14:25:00 +000013611 if (tg3_nvram_read(tp, offset + 4, &offset) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070013612 !tg3_fw_img_is_valid(tp, offset) ||
Matt Carlsone4f34112009-02-25 14:25:00 +000013613 tg3_nvram_read(tp, offset + 8, &val))
Matt Carlson9c8a6202007-10-21 16:16:08 -070013614 return;
13615
13616 offset += val - start;
13617
Matt Carlsonacd9c112009-02-25 14:26:33 +000013618 vlen = strlen(tp->fw_ver);
Matt Carlson9c8a6202007-10-21 16:16:08 -070013619
Matt Carlsonacd9c112009-02-25 14:26:33 +000013620 tp->fw_ver[vlen++] = ',';
13621 tp->fw_ver[vlen++] = ' ';
Matt Carlson9c8a6202007-10-21 16:16:08 -070013622
13623 for (i = 0; i < 4; i++) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000013624 __be32 v;
13625 if (tg3_nvram_read_be32(tp, offset, &v))
Matt Carlson9c8a6202007-10-21 16:16:08 -070013626 return;
13627
Al Virob9fc7dc2007-12-17 22:59:57 -080013628 offset += sizeof(v);
Matt Carlson9c8a6202007-10-21 16:16:08 -070013629
Matt Carlsonacd9c112009-02-25 14:26:33 +000013630 if (vlen > TG3_VER_SIZE - sizeof(v)) {
13631 memcpy(&tp->fw_ver[vlen], &v, TG3_VER_SIZE - vlen);
Matt Carlson9c8a6202007-10-21 16:16:08 -070013632 break;
13633 }
13634
Matt Carlsonacd9c112009-02-25 14:26:33 +000013635 memcpy(&tp->fw_ver[vlen], &v, sizeof(v));
13636 vlen += sizeof(v);
Matt Carlson9c8a6202007-10-21 16:16:08 -070013637 }
Matt Carlsonacd9c112009-02-25 14:26:33 +000013638}
13639
Matt Carlson7fd76442009-02-25 14:27:20 +000013640static void __devinit tg3_read_dash_ver(struct tg3 *tp)
13641{
13642 int vlen;
13643 u32 apedata;
Matt Carlsonecc79642010-08-02 11:26:01 +000013644 char *fwtype;
Matt Carlson7fd76442009-02-25 14:27:20 +000013645
Joe Perches63c3a662011-04-26 08:12:10 +000013646 if (!tg3_flag(tp, ENABLE_APE) || !tg3_flag(tp, ENABLE_ASF))
Matt Carlson7fd76442009-02-25 14:27:20 +000013647 return;
13648
13649 apedata = tg3_ape_read32(tp, TG3_APE_SEG_SIG);
13650 if (apedata != APE_SEG_SIG_MAGIC)
13651 return;
13652
13653 apedata = tg3_ape_read32(tp, TG3_APE_FW_STATUS);
13654 if (!(apedata & APE_FW_STATUS_READY))
13655 return;
13656
13657 apedata = tg3_ape_read32(tp, TG3_APE_FW_VERSION);
13658
Matt Carlsondc6d0742010-09-15 08:59:55 +000013659 if (tg3_ape_read32(tp, TG3_APE_FW_FEATURES) & TG3_APE_FW_FEATURE_NCSI) {
Joe Perches63c3a662011-04-26 08:12:10 +000013660 tg3_flag_set(tp, APE_HAS_NCSI);
Matt Carlsonecc79642010-08-02 11:26:01 +000013661 fwtype = "NCSI";
Matt Carlsondc6d0742010-09-15 08:59:55 +000013662 } else {
Matt Carlsonecc79642010-08-02 11:26:01 +000013663 fwtype = "DASH";
Matt Carlsondc6d0742010-09-15 08:59:55 +000013664 }
Matt Carlsonecc79642010-08-02 11:26:01 +000013665
Matt Carlson7fd76442009-02-25 14:27:20 +000013666 vlen = strlen(tp->fw_ver);
13667
Matt Carlsonecc79642010-08-02 11:26:01 +000013668 snprintf(&tp->fw_ver[vlen], TG3_VER_SIZE - vlen, " %s v%d.%d.%d.%d",
13669 fwtype,
Matt Carlson7fd76442009-02-25 14:27:20 +000013670 (apedata & APE_FW_VERSION_MAJMSK) >> APE_FW_VERSION_MAJSFT,
13671 (apedata & APE_FW_VERSION_MINMSK) >> APE_FW_VERSION_MINSFT,
13672 (apedata & APE_FW_VERSION_REVMSK) >> APE_FW_VERSION_REVSFT,
13673 (apedata & APE_FW_VERSION_BLDMSK));
13674}
13675
Matt Carlsonacd9c112009-02-25 14:26:33 +000013676static void __devinit tg3_read_fw_ver(struct tg3 *tp)
13677{
13678 u32 val;
Matt Carlson75f99362010-04-05 10:19:24 +000013679 bool vpd_vers = false;
13680
13681 if (tp->fw_ver[0] != 0)
13682 vpd_vers = true;
Matt Carlsonacd9c112009-02-25 14:26:33 +000013683
Joe Perches63c3a662011-04-26 08:12:10 +000013684 if (tg3_flag(tp, NO_NVRAM)) {
Matt Carlson75f99362010-04-05 10:19:24 +000013685 strcat(tp->fw_ver, "sb");
Matt Carlsondf259d82009-04-20 06:57:14 +000013686 return;
13687 }
13688
Matt Carlsonacd9c112009-02-25 14:26:33 +000013689 if (tg3_nvram_read(tp, 0, &val))
13690 return;
13691
13692 if (val == TG3_EEPROM_MAGIC)
13693 tg3_read_bc_ver(tp);
13694 else if ((val & TG3_EEPROM_MAGIC_FW_MSK) == TG3_EEPROM_MAGIC_FW)
13695 tg3_read_sb_ver(tp, val);
Matt Carlsona6f6cb12009-02-25 14:27:43 +000013696 else if ((val & TG3_EEPROM_MAGIC_HW_MSK) == TG3_EEPROM_MAGIC_HW)
13697 tg3_read_hwsb_ver(tp);
Matt Carlsonacd9c112009-02-25 14:26:33 +000013698 else
13699 return;
13700
Matt Carlsonc9cab242011-07-13 09:27:27 +000013701 if (vpd_vers)
Matt Carlson75f99362010-04-05 10:19:24 +000013702 goto done;
Matt Carlsonacd9c112009-02-25 14:26:33 +000013703
Matt Carlsonc9cab242011-07-13 09:27:27 +000013704 if (tg3_flag(tp, ENABLE_APE)) {
13705 if (tg3_flag(tp, ENABLE_ASF))
13706 tg3_read_dash_ver(tp);
13707 } else if (tg3_flag(tp, ENABLE_ASF)) {
13708 tg3_read_mgmtfw_ver(tp);
13709 }
Matt Carlson9c8a6202007-10-21 16:16:08 -070013710
Matt Carlson75f99362010-04-05 10:19:24 +000013711done:
Matt Carlson9c8a6202007-10-21 16:16:08 -070013712 tp->fw_ver[TG3_VER_SIZE - 1] = 0;
Michael Chanc4e65752006-03-20 22:29:32 -080013713}
13714
Michael Chan7544b092007-05-05 13:08:32 -070013715static struct pci_dev * __devinit tg3_find_peer(struct tg3 *);
13716
Matt Carlson7cb32cf2010-09-30 10:34:36 +000013717static inline u32 tg3_rx_ret_ring_size(struct tg3 *tp)
13718{
Joe Perches63c3a662011-04-26 08:12:10 +000013719 if (tg3_flag(tp, LRG_PROD_RING_CAP))
Matt Carlsonde9f5232011-04-05 14:22:43 +000013720 return TG3_RX_RET_MAX_SIZE_5717;
Joe Perches63c3a662011-04-26 08:12:10 +000013721 else if (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS))
Matt Carlsonde9f5232011-04-05 14:22:43 +000013722 return TG3_RX_RET_MAX_SIZE_5700;
Matt Carlson7cb32cf2010-09-30 10:34:36 +000013723 else
Matt Carlsonde9f5232011-04-05 14:22:43 +000013724 return TG3_RX_RET_MAX_SIZE_5705;
Matt Carlson7cb32cf2010-09-30 10:34:36 +000013725}
13726
Matt Carlson41434702011-03-09 16:58:22 +000013727static DEFINE_PCI_DEVICE_TABLE(tg3_write_reorder_chipsets) = {
Joe Perches895950c2010-12-21 02:16:08 -080013728 { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_FE_GATE_700C) },
13729 { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_8131_BRIDGE) },
13730 { PCI_DEVICE(PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_8385_0) },
13731 { },
13732};
13733
Linus Torvalds1da177e2005-04-16 15:20:36 -070013734static int __devinit tg3_get_invariants(struct tg3 *tp)
13735{
Linus Torvalds1da177e2005-04-16 15:20:36 -070013736 u32 misc_ctrl_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013737 u32 pci_state_reg, grc_misc_cfg;
13738 u32 val;
13739 u16 pci_cmd;
Matt Carlson5e7dfd02008-11-21 17:18:16 -080013740 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013741
Linus Torvalds1da177e2005-04-16 15:20:36 -070013742 /* Force memory write invalidate off. If we leave it on,
13743 * then on 5700_BX chips we have to enable a workaround.
13744 * The workaround is to set the TG3PCI_DMA_RW_CTRL boundary
13745 * to match the cacheline size. The Broadcom driver have this
13746 * workaround but turns MWI off all the times so never uses
13747 * it. This seems to suggest that the workaround is insufficient.
13748 */
13749 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
13750 pci_cmd &= ~PCI_COMMAND_INVALIDATE;
13751 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
13752
Matt Carlson16821282011-07-13 09:27:28 +000013753 /* Important! -- Make sure register accesses are byteswapped
13754 * correctly. Also, for those chips that require it, make
13755 * sure that indirect register accesses are enabled before
13756 * the first operation.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013757 */
13758 pci_read_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
13759 &misc_ctrl_reg);
Matt Carlson16821282011-07-13 09:27:28 +000013760 tp->misc_host_ctrl |= (misc_ctrl_reg &
13761 MISC_HOST_CTRL_CHIPREV);
13762 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
13763 tp->misc_host_ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013764
13765 tp->pci_chip_rev_id = (misc_ctrl_reg >>
13766 MISC_HOST_CTRL_CHIPREV_SHIFT);
Matt Carlson795d01c2007-10-07 23:28:17 -070013767 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_USE_PROD_ID_REG) {
13768 u32 prod_id_asic_rev;
13769
Matt Carlson5001e2f2009-11-13 13:03:51 +000013770 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717 ||
13771 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718 ||
Matt Carlsond78b59f2011-04-05 14:22:46 +000013772 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5719 ||
13773 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5720)
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013774 pci_read_config_dword(tp->pdev,
13775 TG3PCI_GEN2_PRODID_ASICREV,
13776 &prod_id_asic_rev);
Matt Carlsonb703df62009-12-03 08:36:21 +000013777 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57781 ||
13778 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57785 ||
13779 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57761 ||
13780 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57765 ||
13781 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57791 ||
13782 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57795)
13783 pci_read_config_dword(tp->pdev,
13784 TG3PCI_GEN15_PRODID_ASICREV,
13785 &prod_id_asic_rev);
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013786 else
13787 pci_read_config_dword(tp->pdev, TG3PCI_PRODID_ASICREV,
13788 &prod_id_asic_rev);
13789
Matt Carlson321d32a2008-11-21 17:22:19 -080013790 tp->pci_chip_rev_id = prod_id_asic_rev;
Matt Carlson795d01c2007-10-07 23:28:17 -070013791 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013792
Michael Chanff645be2005-04-21 17:09:53 -070013793 /* Wrong chip ID in 5752 A0. This code can be removed later
13794 * as A0 is not in production.
13795 */
13796 if (tp->pci_chip_rev_id == CHIPREV_ID_5752_A0_HW)
13797 tp->pci_chip_rev_id = CHIPREV_ID_5752_A0;
13798
Michael Chan68929142005-08-09 20:17:14 -070013799 /* If we have 5702/03 A1 or A2 on certain ICH chipsets,
13800 * we need to disable memory and use config. cycles
13801 * only to access all registers. The 5702/03 chips
13802 * can mistakenly decode the special cycles from the
13803 * ICH chipsets as memory write cycles, causing corruption
13804 * of register and memory space. Only certain ICH bridges
13805 * will drive special cycles with non-zero data during the
13806 * address phase which can fall within the 5703's address
13807 * range. This is not an ICH bug as the PCI spec allows
13808 * non-zero address during special cycles. However, only
13809 * these ICH bridges are known to drive non-zero addresses
13810 * during special cycles.
13811 *
13812 * Since special cycles do not cross PCI bridges, we only
13813 * enable this workaround if the 5703 is on the secondary
13814 * bus of these ICH bridges.
13815 */
13816 if ((tp->pci_chip_rev_id == CHIPREV_ID_5703_A1) ||
13817 (tp->pci_chip_rev_id == CHIPREV_ID_5703_A2)) {
13818 static struct tg3_dev_id {
13819 u32 vendor;
13820 u32 device;
13821 u32 rev;
13822 } ich_chipsets[] = {
13823 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AA_8,
13824 PCI_ANY_ID },
13825 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AB_8,
13826 PCI_ANY_ID },
13827 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_11,
13828 0xa },
13829 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_6,
13830 PCI_ANY_ID },
13831 { },
13832 };
13833 struct tg3_dev_id *pci_id = &ich_chipsets[0];
13834 struct pci_dev *bridge = NULL;
13835
13836 while (pci_id->vendor != 0) {
13837 bridge = pci_get_device(pci_id->vendor, pci_id->device,
13838 bridge);
13839 if (!bridge) {
13840 pci_id++;
13841 continue;
13842 }
13843 if (pci_id->rev != PCI_ANY_ID) {
Auke Kok44c10132007-06-08 15:46:36 -070013844 if (bridge->revision > pci_id->rev)
Michael Chan68929142005-08-09 20:17:14 -070013845 continue;
13846 }
13847 if (bridge->subordinate &&
13848 (bridge->subordinate->number ==
13849 tp->pdev->bus->number)) {
Joe Perches63c3a662011-04-26 08:12:10 +000013850 tg3_flag_set(tp, ICH_WORKAROUND);
Michael Chan68929142005-08-09 20:17:14 -070013851 pci_dev_put(bridge);
13852 break;
13853 }
13854 }
13855 }
13856
Matt Carlson6ff6f812011-05-19 12:12:54 +000013857 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
Matt Carlson41588ba2008-04-19 18:12:33 -070013858 static struct tg3_dev_id {
13859 u32 vendor;
13860 u32 device;
13861 } bridge_chipsets[] = {
13862 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PXH_0 },
13863 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PXH_1 },
13864 { },
13865 };
13866 struct tg3_dev_id *pci_id = &bridge_chipsets[0];
13867 struct pci_dev *bridge = NULL;
13868
13869 while (pci_id->vendor != 0) {
13870 bridge = pci_get_device(pci_id->vendor,
13871 pci_id->device,
13872 bridge);
13873 if (!bridge) {
13874 pci_id++;
13875 continue;
13876 }
13877 if (bridge->subordinate &&
13878 (bridge->subordinate->number <=
13879 tp->pdev->bus->number) &&
13880 (bridge->subordinate->subordinate >=
13881 tp->pdev->bus->number)) {
Joe Perches63c3a662011-04-26 08:12:10 +000013882 tg3_flag_set(tp, 5701_DMA_BUG);
Matt Carlson41588ba2008-04-19 18:12:33 -070013883 pci_dev_put(bridge);
13884 break;
13885 }
13886 }
13887 }
13888
Michael Chan4a29cc22006-03-19 13:21:12 -080013889 /* The EPB bridge inside 5714, 5715, and 5780 cannot support
13890 * DMA addresses > 40-bit. This bridge may have other additional
13891 * 57xx devices behind it in some 4-port NIC designs for example.
13892 * Any tg3 device found behind the bridge will also need the 40-bit
13893 * DMA workaround.
13894 */
Michael Chana4e2b342005-10-26 15:46:52 -070013895 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780 ||
13896 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
Joe Perches63c3a662011-04-26 08:12:10 +000013897 tg3_flag_set(tp, 5780_CLASS);
13898 tg3_flag_set(tp, 40BIT_DMA_BUG);
Michael Chan4cf78e42005-07-25 12:29:19 -070013899 tp->msi_cap = pci_find_capability(tp->pdev, PCI_CAP_ID_MSI);
Matt Carlson859a588792010-04-05 10:19:28 +000013900 } else {
Michael Chan4a29cc22006-03-19 13:21:12 -080013901 struct pci_dev *bridge = NULL;
13902
13903 do {
13904 bridge = pci_get_device(PCI_VENDOR_ID_SERVERWORKS,
13905 PCI_DEVICE_ID_SERVERWORKS_EPB,
13906 bridge);
13907 if (bridge && bridge->subordinate &&
13908 (bridge->subordinate->number <=
13909 tp->pdev->bus->number) &&
13910 (bridge->subordinate->subordinate >=
13911 tp->pdev->bus->number)) {
Joe Perches63c3a662011-04-26 08:12:10 +000013912 tg3_flag_set(tp, 40BIT_DMA_BUG);
Michael Chan4a29cc22006-03-19 13:21:12 -080013913 pci_dev_put(bridge);
13914 break;
13915 }
13916 } while (bridge);
13917 }
Michael Chan4cf78e42005-07-25 12:29:19 -070013918
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013919 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
Matt Carlson3a1e19d2011-07-13 09:27:32 +000013920 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714)
Michael Chan7544b092007-05-05 13:08:32 -070013921 tp->pdev_peer = tg3_find_peer(tp);
13922
Matt Carlsonc885e822010-08-02 11:25:57 +000013923 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
Matt Carlsond78b59f2011-04-05 14:22:46 +000013924 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
13925 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
Joe Perches63c3a662011-04-26 08:12:10 +000013926 tg3_flag_set(tp, 5717_PLUS);
Matt Carlson0a58d662011-04-05 14:22:45 +000013927
13928 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765 ||
Joe Perches63c3a662011-04-26 08:12:10 +000013929 tg3_flag(tp, 5717_PLUS))
13930 tg3_flag_set(tp, 57765_PLUS);
Matt Carlsonc885e822010-08-02 11:25:57 +000013931
Matt Carlson321d32a2008-11-21 17:22:19 -080013932 /* Intentionally exclude ASIC_REV_5906 */
13933 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
Michael Chand9ab5ad12006-03-20 22:27:35 -080013934 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787 ||
Matt Carlsond30cdd22007-10-07 23:28:35 -070013935 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson9936bcf2007-10-10 18:03:07 -070013936 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson57e69832008-05-25 23:48:31 -070013937 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013938 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
Joe Perches63c3a662011-04-26 08:12:10 +000013939 tg3_flag(tp, 57765_PLUS))
13940 tg3_flag_set(tp, 5755_PLUS);
Matt Carlson321d32a2008-11-21 17:22:19 -080013941
13942 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 ||
13943 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752 ||
Michael Chanb5d37722006-09-27 16:06:21 -070013944 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906 ||
Joe Perches63c3a662011-04-26 08:12:10 +000013945 tg3_flag(tp, 5755_PLUS) ||
13946 tg3_flag(tp, 5780_CLASS))
13947 tg3_flag_set(tp, 5750_PLUS);
John W. Linville6708e5c2005-04-21 17:00:52 -070013948
Matt Carlson6ff6f812011-05-19 12:12:54 +000013949 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 ||
Joe Perches63c3a662011-04-26 08:12:10 +000013950 tg3_flag(tp, 5750_PLUS))
13951 tg3_flag_set(tp, 5705_PLUS);
John W. Linville1b440c562005-04-21 17:03:18 -070013952
Matt Carlson507399f2009-11-13 13:03:37 +000013953 /* Determine TSO capabilities */
Matt Carlsona0512942011-07-27 14:20:54 +000013954 if (tp->pci_chip_rev_id == CHIPREV_ID_5719_A0)
Matt Carlson4d163b72011-01-25 15:58:48 +000013955 ; /* Do nothing. HW bug. */
Joe Perches63c3a662011-04-26 08:12:10 +000013956 else if (tg3_flag(tp, 57765_PLUS))
13957 tg3_flag_set(tp, HW_TSO_3);
13958 else if (tg3_flag(tp, 5755_PLUS) ||
Matt Carlsone849cdc2009-11-13 13:03:38 +000013959 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
Joe Perches63c3a662011-04-26 08:12:10 +000013960 tg3_flag_set(tp, HW_TSO_2);
13961 else if (tg3_flag(tp, 5750_PLUS)) {
13962 tg3_flag_set(tp, HW_TSO_1);
13963 tg3_flag_set(tp, TSO_BUG);
Matt Carlson507399f2009-11-13 13:03:37 +000013964 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 &&
13965 tp->pci_chip_rev_id >= CHIPREV_ID_5750_C2)
Joe Perches63c3a662011-04-26 08:12:10 +000013966 tg3_flag_clear(tp, TSO_BUG);
Matt Carlson507399f2009-11-13 13:03:37 +000013967 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
13968 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701 &&
13969 tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) {
Joe Perches63c3a662011-04-26 08:12:10 +000013970 tg3_flag_set(tp, TSO_BUG);
Matt Carlson507399f2009-11-13 13:03:37 +000013971 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705)
13972 tp->fw_needed = FIRMWARE_TG3TSO5;
13973 else
13974 tp->fw_needed = FIRMWARE_TG3TSO;
13975 }
13976
Matt Carlsondabc5c62011-05-19 12:12:52 +000013977 /* Selectively allow TSO based on operating conditions */
Matt Carlson6ff6f812011-05-19 12:12:54 +000013978 if (tg3_flag(tp, HW_TSO_1) ||
13979 tg3_flag(tp, HW_TSO_2) ||
13980 tg3_flag(tp, HW_TSO_3) ||
Matt Carlsondabc5c62011-05-19 12:12:52 +000013981 (tp->fw_needed && !tg3_flag(tp, ENABLE_ASF)))
13982 tg3_flag_set(tp, TSO_CAPABLE);
13983 else {
13984 tg3_flag_clear(tp, TSO_CAPABLE);
13985 tg3_flag_clear(tp, TSO_BUG);
13986 tp->fw_needed = NULL;
13987 }
13988
13989 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0)
13990 tp->fw_needed = FIRMWARE_TG3;
13991
Matt Carlson507399f2009-11-13 13:03:37 +000013992 tp->irq_max = 1;
13993
Joe Perches63c3a662011-04-26 08:12:10 +000013994 if (tg3_flag(tp, 5750_PLUS)) {
13995 tg3_flag_set(tp, SUPPORT_MSI);
Michael Chan7544b092007-05-05 13:08:32 -070013996 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_AX ||
13997 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_BX ||
13998 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714 &&
13999 tp->pci_chip_rev_id <= CHIPREV_ID_5714_A2 &&
14000 tp->pdev_peer == tp->pdev))
Joe Perches63c3a662011-04-26 08:12:10 +000014001 tg3_flag_clear(tp, SUPPORT_MSI);
Michael Chan7544b092007-05-05 13:08:32 -070014002
Joe Perches63c3a662011-04-26 08:12:10 +000014003 if (tg3_flag(tp, 5755_PLUS) ||
Michael Chanb5d37722006-09-27 16:06:21 -070014004 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Joe Perches63c3a662011-04-26 08:12:10 +000014005 tg3_flag_set(tp, 1SHOT_MSI);
Michael Chan52c0fd82006-06-29 20:15:54 -070014006 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014007
Joe Perches63c3a662011-04-26 08:12:10 +000014008 if (tg3_flag(tp, 57765_PLUS)) {
14009 tg3_flag_set(tp, SUPPORT_MSIX);
Matt Carlson507399f2009-11-13 13:03:37 +000014010 tp->irq_max = TG3_IRQ_MAX_VECS;
14011 }
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000014012 }
Matt Carlson0e1406d2009-11-02 12:33:33 +000014013
Matt Carlson2ffcc982011-05-19 12:12:44 +000014014 if (tg3_flag(tp, 5755_PLUS))
Joe Perches63c3a662011-04-26 08:12:10 +000014015 tg3_flag_set(tp, SHORT_DMA_BUG);
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000014016
Matt Carlsone31aa982011-07-27 14:20:53 +000014017 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
14018 tg3_flag_set(tp, 4K_FIFO_LIMIT);
14019
Joe Perches63c3a662011-04-26 08:12:10 +000014020 if (tg3_flag(tp, 5717_PLUS))
14021 tg3_flag_set(tp, LRG_PROD_RING_CAP);
Matt Carlsonde9f5232011-04-05 14:22:43 +000014022
Joe Perches63c3a662011-04-26 08:12:10 +000014023 if (tg3_flag(tp, 57765_PLUS) &&
Matt Carlsona0512942011-07-27 14:20:54 +000014024 tp->pci_chip_rev_id != CHIPREV_ID_5719_A0)
Joe Perches63c3a662011-04-26 08:12:10 +000014025 tg3_flag_set(tp, USE_JUMBO_BDFLAG);
Matt Carlsonb703df62009-12-03 08:36:21 +000014026
Joe Perches63c3a662011-04-26 08:12:10 +000014027 if (!tg3_flag(tp, 5705_PLUS) ||
14028 tg3_flag(tp, 5780_CLASS) ||
14029 tg3_flag(tp, USE_JUMBO_BDFLAG))
14030 tg3_flag_set(tp, JUMBO_CAPABLE);
Michael Chan0f893dc2005-07-25 12:30:38 -070014031
Matt Carlson52f44902008-11-21 17:17:04 -080014032 pci_read_config_dword(tp->pdev, TG3PCI_PCISTATE,
14033 &pci_state_reg);
14034
Jon Mason708ebb3a2011-06-27 12:56:50 +000014035 if (pci_is_pcie(tp->pdev)) {
Matt Carlson5e7dfd02008-11-21 17:18:16 -080014036 u16 lnkctl;
14037
Joe Perches63c3a662011-04-26 08:12:10 +000014038 tg3_flag_set(tp, PCI_EXPRESS);
Matt Carlson5f5c51e2007-11-12 21:19:37 -080014039
Matt Carlsoncf790032010-11-24 08:31:48 +000014040 tp->pcie_readrq = 4096;
Matt Carlsond78b59f2011-04-05 14:22:46 +000014041 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
14042 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
Matt Carlsonb4495ed2011-01-25 15:58:47 +000014043 tp->pcie_readrq = 2048;
Matt Carlsoncf790032010-11-24 08:31:48 +000014044
14045 pcie_set_readrq(tp->pdev, tp->pcie_readrq);
Matt Carlson5f5c51e2007-11-12 21:19:37 -080014046
Matt Carlson5e7dfd02008-11-21 17:18:16 -080014047 pci_read_config_word(tp->pdev,
Jon Mason708ebb3a2011-06-27 12:56:50 +000014048 pci_pcie_cap(tp->pdev) + PCI_EXP_LNKCTL,
Matt Carlson5e7dfd02008-11-21 17:18:16 -080014049 &lnkctl);
14050 if (lnkctl & PCI_EXP_LNKCTL_CLKREQ_EN) {
Matt Carlson7196cd62011-05-19 16:02:44 +000014051 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
14052 ASIC_REV_5906) {
Joe Perches63c3a662011-04-26 08:12:10 +000014053 tg3_flag_clear(tp, HW_TSO_2);
Matt Carlsondabc5c62011-05-19 12:12:52 +000014054 tg3_flag_clear(tp, TSO_CAPABLE);
Matt Carlson7196cd62011-05-19 16:02:44 +000014055 }
Matt Carlson5e7dfd02008-11-21 17:18:16 -080014056 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson321d32a2008-11-21 17:22:19 -080014057 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson9cf74eb2009-04-20 06:58:27 +000014058 tp->pci_chip_rev_id == CHIPREV_ID_57780_A0 ||
14059 tp->pci_chip_rev_id == CHIPREV_ID_57780_A1)
Joe Perches63c3a662011-04-26 08:12:10 +000014060 tg3_flag_set(tp, CLKREQ_BUG);
Matt Carlson614b0592010-01-20 16:58:02 +000014061 } else if (tp->pci_chip_rev_id == CHIPREV_ID_5717_A0) {
Joe Perches63c3a662011-04-26 08:12:10 +000014062 tg3_flag_set(tp, L1PLLPD_EN);
Michael Chanc7835a72006-11-15 21:14:42 -080014063 }
Matt Carlson52f44902008-11-21 17:17:04 -080014064 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785) {
Jon Mason708ebb3a2011-06-27 12:56:50 +000014065 /* BCM5785 devices are effectively PCIe devices, and should
14066 * follow PCIe codepaths, but do not have a PCIe capabilities
14067 * section.
Matt Carlson93a700a2011-08-31 11:44:54 +000014068 */
Joe Perches63c3a662011-04-26 08:12:10 +000014069 tg3_flag_set(tp, PCI_EXPRESS);
14070 } else if (!tg3_flag(tp, 5705_PLUS) ||
14071 tg3_flag(tp, 5780_CLASS)) {
Matt Carlson52f44902008-11-21 17:17:04 -080014072 tp->pcix_cap = pci_find_capability(tp->pdev, PCI_CAP_ID_PCIX);
14073 if (!tp->pcix_cap) {
Matt Carlson2445e462010-04-05 10:19:21 +000014074 dev_err(&tp->pdev->dev,
14075 "Cannot find PCI-X capability, aborting\n");
Matt Carlson52f44902008-11-21 17:17:04 -080014076 return -EIO;
14077 }
14078
14079 if (!(pci_state_reg & PCISTATE_CONV_PCI_MODE))
Joe Perches63c3a662011-04-26 08:12:10 +000014080 tg3_flag_set(tp, PCIX_MODE);
Matt Carlson52f44902008-11-21 17:17:04 -080014081 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014082
Michael Chan399de502005-10-03 14:02:39 -070014083 /* If we have an AMD 762 or VIA K8T800 chipset, write
14084 * reordering to the mailbox registers done by the host
14085 * controller can cause major troubles. We read back from
14086 * every mailbox register write to force the writes to be
14087 * posted to the chip in order.
14088 */
Matt Carlson41434702011-03-09 16:58:22 +000014089 if (pci_dev_present(tg3_write_reorder_chipsets) &&
Joe Perches63c3a662011-04-26 08:12:10 +000014090 !tg3_flag(tp, PCI_EXPRESS))
14091 tg3_flag_set(tp, MBOX_WRITE_REORDER);
Michael Chan399de502005-10-03 14:02:39 -070014092
Matt Carlson69fc4052008-12-21 20:19:57 -080014093 pci_read_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE,
14094 &tp->pci_cacheline_sz);
14095 pci_read_config_byte(tp->pdev, PCI_LATENCY_TIMER,
14096 &tp->pci_lat_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014097 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 &&
14098 tp->pci_lat_timer < 64) {
14099 tp->pci_lat_timer = 64;
Matt Carlson69fc4052008-12-21 20:19:57 -080014100 pci_write_config_byte(tp->pdev, PCI_LATENCY_TIMER,
14101 tp->pci_lat_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014102 }
14103
Matt Carlson16821282011-07-13 09:27:28 +000014104 /* Important! -- It is critical that the PCI-X hw workaround
14105 * situation is decided before the first MMIO register access.
14106 */
Matt Carlson52f44902008-11-21 17:17:04 -080014107 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5700_BX) {
14108 /* 5700 BX chips need to have their TX producer index
14109 * mailboxes written twice to workaround a bug.
14110 */
Joe Perches63c3a662011-04-26 08:12:10 +000014111 tg3_flag_set(tp, TXD_MBOX_HWBUG);
Matt Carlson9974a352007-10-07 23:27:28 -070014112
Matt Carlson52f44902008-11-21 17:17:04 -080014113 /* If we are in PCI-X mode, enable register write workaround.
Linus Torvalds1da177e2005-04-16 15:20:36 -070014114 *
14115 * The workaround is to use indirect register accesses
14116 * for all chip writes not to mailbox registers.
14117 */
Joe Perches63c3a662011-04-26 08:12:10 +000014118 if (tg3_flag(tp, PCIX_MODE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070014119 u32 pm_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014120
Joe Perches63c3a662011-04-26 08:12:10 +000014121 tg3_flag_set(tp, PCIX_TARGET_HWBUG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014122
14123 /* The chip can have it's power management PCI config
14124 * space registers clobbered due to this bug.
14125 * So explicitly force the chip into D0 here.
14126 */
Matt Carlson9974a352007-10-07 23:27:28 -070014127 pci_read_config_dword(tp->pdev,
14128 tp->pm_cap + PCI_PM_CTRL,
Linus Torvalds1da177e2005-04-16 15:20:36 -070014129 &pm_reg);
14130 pm_reg &= ~PCI_PM_CTRL_STATE_MASK;
14131 pm_reg |= PCI_PM_CTRL_PME_ENABLE | 0 /* D0 */;
Matt Carlson9974a352007-10-07 23:27:28 -070014132 pci_write_config_dword(tp->pdev,
14133 tp->pm_cap + PCI_PM_CTRL,
Linus Torvalds1da177e2005-04-16 15:20:36 -070014134 pm_reg);
14135
14136 /* Also, force SERR#/PERR# in PCI command. */
14137 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
14138 pci_cmd |= PCI_COMMAND_PARITY | PCI_COMMAND_SERR;
14139 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
14140 }
14141 }
14142
Linus Torvalds1da177e2005-04-16 15:20:36 -070014143 if ((pci_state_reg & PCISTATE_BUS_SPEED_HIGH) != 0)
Joe Perches63c3a662011-04-26 08:12:10 +000014144 tg3_flag_set(tp, PCI_HIGH_SPEED);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014145 if ((pci_state_reg & PCISTATE_BUS_32BIT) != 0)
Joe Perches63c3a662011-04-26 08:12:10 +000014146 tg3_flag_set(tp, PCI_32BIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014147
14148 /* Chip-specific fixup from Broadcom driver */
14149 if ((tp->pci_chip_rev_id == CHIPREV_ID_5704_A0) &&
14150 (!(pci_state_reg & PCISTATE_RETRY_SAME_DMA))) {
14151 pci_state_reg |= PCISTATE_RETRY_SAME_DMA;
14152 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE, pci_state_reg);
14153 }
14154
Michael Chan1ee582d2005-08-09 20:16:46 -070014155 /* Default fast path register access methods */
Michael Chan20094932005-08-09 20:16:32 -070014156 tp->read32 = tg3_read32;
Michael Chan1ee582d2005-08-09 20:16:46 -070014157 tp->write32 = tg3_write32;
Michael Chan09ee9292005-08-09 20:17:00 -070014158 tp->read32_mbox = tg3_read32;
Michael Chan20094932005-08-09 20:16:32 -070014159 tp->write32_mbox = tg3_write32;
Michael Chan1ee582d2005-08-09 20:16:46 -070014160 tp->write32_tx_mbox = tg3_write32;
14161 tp->write32_rx_mbox = tg3_write32;
14162
14163 /* Various workaround register access methods */
Joe Perches63c3a662011-04-26 08:12:10 +000014164 if (tg3_flag(tp, PCIX_TARGET_HWBUG))
Michael Chan1ee582d2005-08-09 20:16:46 -070014165 tp->write32 = tg3_write_indirect_reg32;
Matt Carlson98efd8a2007-05-05 12:47:25 -070014166 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701 ||
Joe Perches63c3a662011-04-26 08:12:10 +000014167 (tg3_flag(tp, PCI_EXPRESS) &&
Matt Carlson98efd8a2007-05-05 12:47:25 -070014168 tp->pci_chip_rev_id == CHIPREV_ID_5750_A0)) {
14169 /*
14170 * Back to back register writes can cause problems on these
14171 * chips, the workaround is to read back all reg writes
14172 * except those to mailbox regs.
14173 *
14174 * See tg3_write_indirect_reg32().
14175 */
Michael Chan1ee582d2005-08-09 20:16:46 -070014176 tp->write32 = tg3_write_flush_reg32;
Matt Carlson98efd8a2007-05-05 12:47:25 -070014177 }
14178
Joe Perches63c3a662011-04-26 08:12:10 +000014179 if (tg3_flag(tp, TXD_MBOX_HWBUG) || tg3_flag(tp, MBOX_WRITE_REORDER)) {
Michael Chan1ee582d2005-08-09 20:16:46 -070014180 tp->write32_tx_mbox = tg3_write32_tx_mbox;
Joe Perches63c3a662011-04-26 08:12:10 +000014181 if (tg3_flag(tp, MBOX_WRITE_REORDER))
Michael Chan1ee582d2005-08-09 20:16:46 -070014182 tp->write32_rx_mbox = tg3_write_flush_reg32;
14183 }
Michael Chan20094932005-08-09 20:16:32 -070014184
Joe Perches63c3a662011-04-26 08:12:10 +000014185 if (tg3_flag(tp, ICH_WORKAROUND)) {
Michael Chan68929142005-08-09 20:17:14 -070014186 tp->read32 = tg3_read_indirect_reg32;
14187 tp->write32 = tg3_write_indirect_reg32;
14188 tp->read32_mbox = tg3_read_indirect_mbox;
14189 tp->write32_mbox = tg3_write_indirect_mbox;
14190 tp->write32_tx_mbox = tg3_write_indirect_mbox;
14191 tp->write32_rx_mbox = tg3_write_indirect_mbox;
14192
14193 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070014194 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070014195
14196 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
14197 pci_cmd &= ~PCI_COMMAND_MEMORY;
14198 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
14199 }
Michael Chanb5d37722006-09-27 16:06:21 -070014200 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
14201 tp->read32_mbox = tg3_read32_mbox_5906;
14202 tp->write32_mbox = tg3_write32_mbox_5906;
14203 tp->write32_tx_mbox = tg3_write32_mbox_5906;
14204 tp->write32_rx_mbox = tg3_write32_mbox_5906;
14205 }
Michael Chan68929142005-08-09 20:17:14 -070014206
Michael Chanbbadf502006-04-06 21:46:34 -070014207 if (tp->write32 == tg3_write_indirect_reg32 ||
Joe Perches63c3a662011-04-26 08:12:10 +000014208 (tg3_flag(tp, PCIX_MODE) &&
Michael Chanbbadf502006-04-06 21:46:34 -070014209 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
David S. Millerf49639e2006-06-09 11:58:36 -070014210 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)))
Joe Perches63c3a662011-04-26 08:12:10 +000014211 tg3_flag_set(tp, SRAM_USE_CONFIG);
Michael Chanbbadf502006-04-06 21:46:34 -070014212
Matt Carlson16821282011-07-13 09:27:28 +000014213 /* The memory arbiter has to be enabled in order for SRAM accesses
14214 * to succeed. Normally on powerup the tg3 chip firmware will make
14215 * sure it is enabled, but other entities such as system netboot
14216 * code might disable it.
14217 */
14218 val = tr32(MEMARB_MODE);
14219 tw32(MEMARB_MODE, val | MEMARB_MODE_ENABLE);
14220
Matt Carlson9dc5e342011-11-04 09:15:02 +000014221 tp->pci_fn = PCI_FUNC(tp->pdev->devfn) & 3;
14222 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
14223 tg3_flag(tp, 5780_CLASS)) {
14224 if (tg3_flag(tp, PCIX_MODE)) {
14225 pci_read_config_dword(tp->pdev,
14226 tp->pcix_cap + PCI_X_STATUS,
14227 &val);
14228 tp->pci_fn = val & 0x7;
14229 }
14230 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717) {
14231 tg3_read_mem(tp, NIC_SRAM_CPMU_STATUS, &val);
14232 if ((val & NIC_SRAM_CPMUSTAT_SIG_MSK) ==
14233 NIC_SRAM_CPMUSTAT_SIG) {
14234 tp->pci_fn = val & TG3_CPMU_STATUS_FMSK_5717;
14235 tp->pci_fn = tp->pci_fn ? 1 : 0;
14236 }
14237 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
14238 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
14239 tg3_read_mem(tp, NIC_SRAM_CPMU_STATUS, &val);
14240 if ((val & NIC_SRAM_CPMUSTAT_SIG_MSK) ==
14241 NIC_SRAM_CPMUSTAT_SIG) {
14242 tp->pci_fn = (val & TG3_CPMU_STATUS_FMSK_5719) >>
14243 TG3_CPMU_STATUS_FSHFT_5719;
14244 }
Matt Carlson69f11c92011-07-13 09:27:30 +000014245 }
14246
Michael Chan7d0c41e2005-04-21 17:06:20 -070014247 /* Get eeprom hw config before calling tg3_set_power_state().
Joe Perches63c3a662011-04-26 08:12:10 +000014248 * In particular, the TG3_FLAG_IS_NIC flag must be
Michael Chan7d0c41e2005-04-21 17:06:20 -070014249 * determined before calling tg3_set_power_state() so that
14250 * we know whether or not to switch out of Vaux power.
14251 * When the flag is set, it means that GPIO1 is used for eeprom
14252 * write protect and also implies that it is a LOM where GPIOs
14253 * are not used to switch power.
Jeff Garzik6aa20a22006-09-13 13:24:59 -040014254 */
Michael Chan7d0c41e2005-04-21 17:06:20 -070014255 tg3_get_eeprom_hw_cfg(tp);
14256
Joe Perches63c3a662011-04-26 08:12:10 +000014257 if (tg3_flag(tp, ENABLE_APE)) {
Matt Carlson0d3031d2007-10-10 18:02:43 -070014258 /* Allow reads and writes to the
14259 * APE register and memory space.
14260 */
14261 pci_state_reg |= PCISTATE_ALLOW_APE_CTLSPC_WR |
Matt Carlsonf92d9dc12010-06-05 17:24:30 +000014262 PCISTATE_ALLOW_APE_SHMEM_WR |
14263 PCISTATE_ALLOW_APE_PSPACE_WR;
Matt Carlson0d3031d2007-10-10 18:02:43 -070014264 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE,
14265 pci_state_reg);
Matt Carlsonc9cab242011-07-13 09:27:27 +000014266
14267 tg3_ape_lock_init(tp);
Matt Carlson0d3031d2007-10-10 18:02:43 -070014268 }
14269
Matt Carlson9936bcf2007-10-10 18:03:07 -070014270 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson57e69832008-05-25 23:48:31 -070014271 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson321d32a2008-11-21 17:22:19 -080014272 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000014273 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
Joe Perches63c3a662011-04-26 08:12:10 +000014274 tg3_flag(tp, 57765_PLUS))
14275 tg3_flag_set(tp, CPMU_PRESENT);
Matt Carlsond30cdd22007-10-07 23:28:35 -070014276
Matt Carlson16821282011-07-13 09:27:28 +000014277 /* Set up tp->grc_local_ctrl before calling
14278 * tg3_pwrsrc_switch_to_vmain(). GPIO1 driven high
14279 * will bring 5700's external PHY out of reset.
Michael Chan314fba32005-04-21 17:07:04 -070014280 * It is also used as eeprom write protect on LOMs.
14281 */
14282 tp->grc_local_ctrl = GRC_LCLCTRL_INT_ON_ATTN | GRC_LCLCTRL_AUTO_SEEPROM;
Matt Carlson6ff6f812011-05-19 12:12:54 +000014283 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
Joe Perches63c3a662011-04-26 08:12:10 +000014284 tg3_flag(tp, EEPROM_WRITE_PROT))
Michael Chan314fba32005-04-21 17:07:04 -070014285 tp->grc_local_ctrl |= (GRC_LCLCTRL_GPIO_OE1 |
14286 GRC_LCLCTRL_GPIO_OUTPUT1);
Michael Chan3e7d83b2005-04-21 17:10:36 -070014287 /* Unused GPIO3 must be driven as output on 5752 because there
14288 * are no pull-up resistors on unused GPIO pins.
14289 */
14290 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752)
14291 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE3;
Michael Chan314fba32005-04-21 17:07:04 -070014292
Matt Carlson321d32a2008-11-21 17:22:19 -080014293 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
Matt Carlsoncb4ed1f2010-01-20 16:58:09 +000014294 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
14295 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Michael Chanaf36e6b2006-03-23 01:28:06 -080014296 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_UART_SEL;
14297
Matt Carlson8d519ab2009-04-20 06:58:01 +000014298 if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761 ||
14299 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761S) {
Matt Carlson5f0c4a32008-06-09 15:41:12 -070014300 /* Turn off the debug UART. */
14301 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_UART_SEL;
Joe Perches63c3a662011-04-26 08:12:10 +000014302 if (tg3_flag(tp, IS_NIC))
Matt Carlson5f0c4a32008-06-09 15:41:12 -070014303 /* Keep VMain power. */
14304 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE0 |
14305 GRC_LCLCTRL_GPIO_OUTPUT0;
14306 }
14307
Matt Carlson16821282011-07-13 09:27:28 +000014308 /* Switch out of Vaux if it is a NIC */
14309 tg3_pwrsrc_switch_to_vmain(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014310
Linus Torvalds1da177e2005-04-16 15:20:36 -070014311 /* Derive initial jumbo mode from MTU assigned in
14312 * ether_setup() via the alloc_etherdev() call
14313 */
Joe Perches63c3a662011-04-26 08:12:10 +000014314 if (tp->dev->mtu > ETH_DATA_LEN && !tg3_flag(tp, 5780_CLASS))
14315 tg3_flag_set(tp, JUMBO_RING_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014316
14317 /* Determine WakeOnLan speed to use. */
14318 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
14319 tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
14320 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0 ||
14321 tp->pci_chip_rev_id == CHIPREV_ID_5701_B2) {
Joe Perches63c3a662011-04-26 08:12:10 +000014322 tg3_flag_clear(tp, WOL_SPEED_100MB);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014323 } else {
Joe Perches63c3a662011-04-26 08:12:10 +000014324 tg3_flag_set(tp, WOL_SPEED_100MB);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014325 }
14326
Matt Carlson7f97a4b2009-08-25 10:10:03 +000014327 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014328 tp->phy_flags |= TG3_PHYFLG_IS_FET;
Matt Carlson7f97a4b2009-08-25 10:10:03 +000014329
Linus Torvalds1da177e2005-04-16 15:20:36 -070014330 /* A few boards don't want Ethernet@WireSpeed phy feature */
Matt Carlson6ff6f812011-05-19 12:12:54 +000014331 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
14332 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070014333 (tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) &&
Michael Chan747e8f82005-07-25 12:33:22 -070014334 (tp->pci_chip_rev_id != CHIPREV_ID_5705_A1)) ||
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014335 (tp->phy_flags & TG3_PHYFLG_IS_FET) ||
14336 (tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
14337 tp->phy_flags |= TG3_PHYFLG_NO_ETH_WIRE_SPEED;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014338
14339 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5703_AX ||
14340 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5704_AX)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014341 tp->phy_flags |= TG3_PHYFLG_ADC_BUG;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014342 if (tp->pci_chip_rev_id == CHIPREV_ID_5704_A0)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014343 tp->phy_flags |= TG3_PHYFLG_5704_A0_BUG;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014344
Joe Perches63c3a662011-04-26 08:12:10 +000014345 if (tg3_flag(tp, 5705_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014346 !(tp->phy_flags & TG3_PHYFLG_IS_FET) &&
Matt Carlson321d32a2008-11-21 17:22:19 -080014347 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000014348 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_57780 &&
Joe Perches63c3a662011-04-26 08:12:10 +000014349 !tg3_flag(tp, 57765_PLUS)) {
Michael Chanc424cb22006-04-29 18:56:34 -070014350 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
Matt Carlsond30cdd22007-10-07 23:28:35 -070014351 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787 ||
Matt Carlson9936bcf2007-10-10 18:03:07 -070014352 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
14353 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761) {
Michael Chand4011ad2007-02-13 12:17:25 -080014354 if (tp->pdev->device != PCI_DEVICE_ID_TIGON3_5756 &&
14355 tp->pdev->device != PCI_DEVICE_ID_TIGON3_5722)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014356 tp->phy_flags |= TG3_PHYFLG_JITTER_BUG;
Michael Chanc1d2a192007-01-08 19:57:20 -080014357 if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5755M)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014358 tp->phy_flags |= TG3_PHYFLG_ADJUST_TRIM;
Matt Carlson321d32a2008-11-21 17:22:19 -080014359 } else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014360 tp->phy_flags |= TG3_PHYFLG_BER_BUG;
Michael Chanc424cb22006-04-29 18:56:34 -070014361 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014362
Matt Carlsonb2a5c192008-04-03 21:44:44 -070014363 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
14364 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX) {
14365 tp->phy_otp = tg3_read_otp_phycfg(tp);
14366 if (tp->phy_otp == 0)
14367 tp->phy_otp = TG3_OTP_DEFAULT;
14368 }
14369
Joe Perches63c3a662011-04-26 08:12:10 +000014370 if (tg3_flag(tp, CPMU_PRESENT))
Matt Carlson8ef21422008-05-02 16:47:53 -070014371 tp->mi_mode = MAC_MI_MODE_500KHZ_CONST;
14372 else
14373 tp->mi_mode = MAC_MI_MODE_BASE;
14374
Linus Torvalds1da177e2005-04-16 15:20:36 -070014375 tp->coalesce_mode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014376 if (GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5700_AX &&
14377 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5700_BX)
14378 tp->coalesce_mode |= HOSTCC_MODE_32BYTE;
14379
Matt Carlson4d958472011-04-20 07:57:35 +000014380 /* Set these bits to enable statistics workaround. */
14381 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
14382 tp->pci_chip_rev_id == CHIPREV_ID_5719_A0 ||
14383 tp->pci_chip_rev_id == CHIPREV_ID_5720_A0) {
14384 tp->coalesce_mode |= HOSTCC_MODE_ATTN;
14385 tp->grc_mode |= GRC_MODE_IRQ_ON_FLOW_ATTN;
14386 }
14387
Matt Carlson321d32a2008-11-21 17:22:19 -080014388 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
14389 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
Joe Perches63c3a662011-04-26 08:12:10 +000014390 tg3_flag_set(tp, USE_PHYLIB);
Matt Carlson57e69832008-05-25 23:48:31 -070014391
Matt Carlson158d7ab2008-05-29 01:37:54 -070014392 err = tg3_mdio_init(tp);
14393 if (err)
14394 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014395
14396 /* Initialize data/descriptor byte/word swapping. */
14397 val = tr32(GRC_MODE);
Matt Carlsonf2096f92011-04-05 14:22:48 +000014398 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
14399 val &= (GRC_MODE_BYTE_SWAP_B2HRX_DATA |
14400 GRC_MODE_WORD_SWAP_B2HRX_DATA |
14401 GRC_MODE_B2HRX_ENABLE |
14402 GRC_MODE_HTX2B_ENABLE |
14403 GRC_MODE_HOST_STACKUP);
14404 else
14405 val &= GRC_MODE_HOST_STACKUP;
14406
Linus Torvalds1da177e2005-04-16 15:20:36 -070014407 tw32(GRC_MODE, val | tp->grc_mode);
14408
14409 tg3_switch_clocks(tp);
14410
14411 /* Clear this out for sanity. */
14412 tw32(TG3PCI_MEM_WIN_BASE_ADDR, 0);
14413
14414 pci_read_config_dword(tp->pdev, TG3PCI_PCISTATE,
14415 &pci_state_reg);
14416 if ((pci_state_reg & PCISTATE_CONV_PCI_MODE) == 0 &&
Joe Perches63c3a662011-04-26 08:12:10 +000014417 !tg3_flag(tp, PCIX_TARGET_HWBUG)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070014418 u32 chiprevid = GET_CHIP_REV_ID(tp->misc_host_ctrl);
14419
14420 if (chiprevid == CHIPREV_ID_5701_A0 ||
14421 chiprevid == CHIPREV_ID_5701_B0 ||
14422 chiprevid == CHIPREV_ID_5701_B2 ||
14423 chiprevid == CHIPREV_ID_5701_B5) {
14424 void __iomem *sram_base;
14425
14426 /* Write some dummy words into the SRAM status block
14427 * area, see if it reads back correctly. If the return
14428 * value is bad, force enable the PCIX workaround.
14429 */
14430 sram_base = tp->regs + NIC_SRAM_WIN_BASE + NIC_SRAM_STATS_BLK;
14431
14432 writel(0x00000000, sram_base);
14433 writel(0x00000000, sram_base + 4);
14434 writel(0xffffffff, sram_base + 4);
14435 if (readl(sram_base) != 0x00000000)
Joe Perches63c3a662011-04-26 08:12:10 +000014436 tg3_flag_set(tp, PCIX_TARGET_HWBUG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014437 }
14438 }
14439
14440 udelay(50);
14441 tg3_nvram_init(tp);
14442
14443 grc_misc_cfg = tr32(GRC_MISC_CFG);
14444 grc_misc_cfg &= GRC_MISC_CFG_BOARD_ID_MASK;
14445
Linus Torvalds1da177e2005-04-16 15:20:36 -070014446 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
14447 (grc_misc_cfg == GRC_MISC_CFG_BOARD_ID_5788 ||
14448 grc_misc_cfg == GRC_MISC_CFG_BOARD_ID_5788M))
Joe Perches63c3a662011-04-26 08:12:10 +000014449 tg3_flag_set(tp, IS_5788);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014450
Joe Perches63c3a662011-04-26 08:12:10 +000014451 if (!tg3_flag(tp, IS_5788) &&
Matt Carlson6ff6f812011-05-19 12:12:54 +000014452 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700)
Joe Perches63c3a662011-04-26 08:12:10 +000014453 tg3_flag_set(tp, TAGGED_STATUS);
14454 if (tg3_flag(tp, TAGGED_STATUS)) {
David S. Millerfac9b832005-05-18 22:46:34 -070014455 tp->coalesce_mode |= (HOSTCC_MODE_CLRTICK_RXBD |
14456 HOSTCC_MODE_CLRTICK_TXBD);
14457
14458 tp->misc_host_ctrl |= MISC_HOST_CTRL_TAGGED_STATUS;
14459 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
14460 tp->misc_host_ctrl);
14461 }
14462
Matt Carlson3bda1252008-08-15 14:08:22 -070014463 /* Preserve the APE MAC_MODE bits */
Joe Perches63c3a662011-04-26 08:12:10 +000014464 if (tg3_flag(tp, ENABLE_APE))
Matt Carlsond2394e6b2010-11-24 08:31:47 +000014465 tp->mac_mode = MAC_MODE_APE_TX_EN | MAC_MODE_APE_RX_EN;
Matt Carlson3bda1252008-08-15 14:08:22 -070014466 else
Matt Carlson6e01b202011-08-19 13:58:20 +000014467 tp->mac_mode = 0;
Matt Carlson3bda1252008-08-15 14:08:22 -070014468
Linus Torvalds1da177e2005-04-16 15:20:36 -070014469 /* these are limited to 10/100 only */
14470 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 &&
14471 (grc_misc_cfg == 0x8000 || grc_misc_cfg == 0x4000)) ||
14472 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
14473 tp->pdev->vendor == PCI_VENDOR_ID_BROADCOM &&
14474 (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5901 ||
14475 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5901_2 ||
14476 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5705F)) ||
14477 (tp->pdev->vendor == PCI_VENDOR_ID_BROADCOM &&
14478 (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5751F ||
Michael Chan676917d2006-12-07 00:20:22 -080014479 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5753F ||
14480 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5787F)) ||
Matt Carlson321d32a2008-11-21 17:22:19 -080014481 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57790 ||
Matt Carlsond1101142010-02-17 15:16:55 +000014482 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57791 ||
14483 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57795 ||
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014484 (tp->phy_flags & TG3_PHYFLG_IS_FET))
14485 tp->phy_flags |= TG3_PHYFLG_10_100_ONLY;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014486
14487 err = tg3_phy_probe(tp);
14488 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000014489 dev_err(&tp->pdev->dev, "phy probe failed, err %d\n", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014490 /* ... but do not return immediately ... */
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070014491 tg3_mdio_fini(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014492 }
14493
Matt Carlson184b8902010-04-05 10:19:25 +000014494 tg3_read_vpd(tp);
Michael Chanc4e65752006-03-20 22:29:32 -080014495 tg3_read_fw_ver(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014496
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014497 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
14498 tp->phy_flags &= ~TG3_PHYFLG_USE_MI_INTERRUPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014499 } else {
14500 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014501 tp->phy_flags |= TG3_PHYFLG_USE_MI_INTERRUPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014502 else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014503 tp->phy_flags &= ~TG3_PHYFLG_USE_MI_INTERRUPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014504 }
14505
14506 /* 5700 {AX,BX} chips have a broken status block link
14507 * change bit implementation, so we must use the
14508 * status register in those cases.
14509 */
14510 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700)
Joe Perches63c3a662011-04-26 08:12:10 +000014511 tg3_flag_set(tp, USE_LINKCHG_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014512 else
Joe Perches63c3a662011-04-26 08:12:10 +000014513 tg3_flag_clear(tp, USE_LINKCHG_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014514
14515 /* The led_ctrl is set during tg3_phy_probe, here we might
14516 * have to force the link status polling mechanism based
14517 * upon subsystem IDs.
14518 */
14519 if (tp->pdev->subsystem_vendor == PCI_VENDOR_ID_DELL &&
Michael Chan007a880d2007-05-31 14:49:51 -070014520 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701 &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014521 !(tp->phy_flags & TG3_PHYFLG_PHY_SERDES)) {
14522 tp->phy_flags |= TG3_PHYFLG_USE_MI_INTERRUPT;
Joe Perches63c3a662011-04-26 08:12:10 +000014523 tg3_flag_set(tp, USE_LINKCHG_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014524 }
14525
14526 /* For all SERDES we poll the MAC status register. */
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014527 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Joe Perches63c3a662011-04-26 08:12:10 +000014528 tg3_flag_set(tp, POLL_SERDES);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014529 else
Joe Perches63c3a662011-04-26 08:12:10 +000014530 tg3_flag_clear(tp, POLL_SERDES);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014531
Eric Dumazet9205fd92011-11-18 06:47:01 +000014532 tp->rx_offset = NET_SKB_PAD + NET_IP_ALIGN;
Matt Carlsond2757fc2010-04-12 06:58:27 +000014533 tp->rx_copy_thresh = TG3_RX_COPY_THRESHOLD;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014534 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701 &&
Joe Perches63c3a662011-04-26 08:12:10 +000014535 tg3_flag(tp, PCIX_MODE)) {
Eric Dumazet9205fd92011-11-18 06:47:01 +000014536 tp->rx_offset = NET_SKB_PAD;
Matt Carlsond2757fc2010-04-12 06:58:27 +000014537#ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
Matt Carlson9dc7a112010-04-12 06:58:28 +000014538 tp->rx_copy_thresh = ~(u16)0;
Matt Carlsond2757fc2010-04-12 06:58:27 +000014539#endif
14540 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014541
Matt Carlson2c49a442010-09-30 10:34:35 +000014542 tp->rx_std_ring_mask = TG3_RX_STD_RING_SIZE(tp) - 1;
14543 tp->rx_jmb_ring_mask = TG3_RX_JMB_RING_SIZE(tp) - 1;
Matt Carlson7cb32cf2010-09-30 10:34:36 +000014544 tp->rx_ret_ring_mask = tg3_rx_ret_ring_size(tp) - 1;
14545
Matt Carlson2c49a442010-09-30 10:34:35 +000014546 tp->rx_std_max_post = tp->rx_std_ring_mask + 1;
Michael Chanf92905d2006-06-29 20:14:29 -070014547
14548 /* Increment the rx prod index on the rx std ring by at most
14549 * 8 for these chips to workaround hw errata.
14550 */
14551 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 ||
14552 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752 ||
14553 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755)
14554 tp->rx_std_max_post = 8;
14555
Joe Perches63c3a662011-04-26 08:12:10 +000014556 if (tg3_flag(tp, ASPM_WORKAROUND))
Matt Carlson8ed5d972007-05-07 00:25:49 -070014557 tp->pwrmgmt_thresh = tr32(PCIE_PWR_MGMT_THRESH) &
14558 PCIE_PWR_MGMT_L1_THRESH_MSK;
14559
Linus Torvalds1da177e2005-04-16 15:20:36 -070014560 return err;
14561}
14562
David S. Miller49b6e95f2007-03-29 01:38:42 -070014563#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070014564static int __devinit tg3_get_macaddr_sparc(struct tg3 *tp)
14565{
14566 struct net_device *dev = tp->dev;
14567 struct pci_dev *pdev = tp->pdev;
David S. Miller49b6e95f2007-03-29 01:38:42 -070014568 struct device_node *dp = pci_device_to_OF_node(pdev);
David S. Miller374d4ca2007-03-29 01:57:57 -070014569 const unsigned char *addr;
David S. Miller49b6e95f2007-03-29 01:38:42 -070014570 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014571
David S. Miller49b6e95f2007-03-29 01:38:42 -070014572 addr = of_get_property(dp, "local-mac-address", &len);
14573 if (addr && len == 6) {
14574 memcpy(dev->dev_addr, addr, 6);
14575 memcpy(dev->perm_addr, dev->dev_addr, 6);
14576 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014577 }
14578 return -ENODEV;
14579}
14580
14581static int __devinit tg3_get_default_macaddr_sparc(struct tg3 *tp)
14582{
14583 struct net_device *dev = tp->dev;
14584
14585 memcpy(dev->dev_addr, idprom->id_ethaddr, 6);
John W. Linville2ff43692005-09-12 14:44:20 -070014586 memcpy(dev->perm_addr, idprom->id_ethaddr, 6);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014587 return 0;
14588}
14589#endif
14590
14591static int __devinit tg3_get_device_address(struct tg3 *tp)
14592{
14593 struct net_device *dev = tp->dev;
14594 u32 hi, lo, mac_offset;
Michael Chan008652b2006-03-27 23:14:53 -080014595 int addr_ok = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014596
David S. Miller49b6e95f2007-03-29 01:38:42 -070014597#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070014598 if (!tg3_get_macaddr_sparc(tp))
14599 return 0;
14600#endif
14601
14602 mac_offset = 0x7c;
Matt Carlson6ff6f812011-05-19 12:12:54 +000014603 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
Joe Perches63c3a662011-04-26 08:12:10 +000014604 tg3_flag(tp, 5780_CLASS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070014605 if (tr32(TG3PCI_DUAL_MAC_CTRL) & DUAL_MAC_CTRL_ID)
14606 mac_offset = 0xcc;
14607 if (tg3_nvram_lock(tp))
14608 tw32_f(NVRAM_CMD, NVRAM_CMD_RESET);
14609 else
14610 tg3_nvram_unlock(tp);
Joe Perches63c3a662011-04-26 08:12:10 +000014611 } else if (tg3_flag(tp, 5717_PLUS)) {
Matt Carlson69f11c92011-07-13 09:27:30 +000014612 if (tp->pci_fn & 1)
Matt Carlsona1b950d2009-09-01 13:20:17 +000014613 mac_offset = 0xcc;
Matt Carlson69f11c92011-07-13 09:27:30 +000014614 if (tp->pci_fn > 1)
Matt Carlsona50d0792010-06-05 17:24:37 +000014615 mac_offset += 0x18c;
Matt Carlsona1b950d2009-09-01 13:20:17 +000014616 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
Michael Chanb5d37722006-09-27 16:06:21 -070014617 mac_offset = 0x10;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014618
14619 /* First try to get it from MAC address mailbox. */
14620 tg3_read_mem(tp, NIC_SRAM_MAC_ADDR_HIGH_MBOX, &hi);
14621 if ((hi >> 16) == 0x484b) {
14622 dev->dev_addr[0] = (hi >> 8) & 0xff;
14623 dev->dev_addr[1] = (hi >> 0) & 0xff;
14624
14625 tg3_read_mem(tp, NIC_SRAM_MAC_ADDR_LOW_MBOX, &lo);
14626 dev->dev_addr[2] = (lo >> 24) & 0xff;
14627 dev->dev_addr[3] = (lo >> 16) & 0xff;
14628 dev->dev_addr[4] = (lo >> 8) & 0xff;
14629 dev->dev_addr[5] = (lo >> 0) & 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014630
Michael Chan008652b2006-03-27 23:14:53 -080014631 /* Some old bootcode may report a 0 MAC address in SRAM */
14632 addr_ok = is_valid_ether_addr(&dev->dev_addr[0]);
14633 }
14634 if (!addr_ok) {
14635 /* Next, try NVRAM. */
Joe Perches63c3a662011-04-26 08:12:10 +000014636 if (!tg3_flag(tp, NO_NVRAM) &&
Matt Carlsondf259d82009-04-20 06:57:14 +000014637 !tg3_nvram_read_be32(tp, mac_offset + 0, &hi) &&
Matt Carlson6d348f22009-02-25 14:25:52 +000014638 !tg3_nvram_read_be32(tp, mac_offset + 4, &lo)) {
Matt Carlson62cedd12009-04-20 14:52:29 -070014639 memcpy(&dev->dev_addr[0], ((char *)&hi) + 2, 2);
14640 memcpy(&dev->dev_addr[2], (char *)&lo, sizeof(lo));
Michael Chan008652b2006-03-27 23:14:53 -080014641 }
14642 /* Finally just fetch it out of the MAC control regs. */
14643 else {
14644 hi = tr32(MAC_ADDR_0_HIGH);
14645 lo = tr32(MAC_ADDR_0_LOW);
14646
14647 dev->dev_addr[5] = lo & 0xff;
14648 dev->dev_addr[4] = (lo >> 8) & 0xff;
14649 dev->dev_addr[3] = (lo >> 16) & 0xff;
14650 dev->dev_addr[2] = (lo >> 24) & 0xff;
14651 dev->dev_addr[1] = hi & 0xff;
14652 dev->dev_addr[0] = (hi >> 8) & 0xff;
14653 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014654 }
14655
14656 if (!is_valid_ether_addr(&dev->dev_addr[0])) {
David S. Miller7582a332008-03-20 15:53:15 -070014657#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070014658 if (!tg3_get_default_macaddr_sparc(tp))
14659 return 0;
14660#endif
14661 return -EINVAL;
14662 }
John W. Linville2ff43692005-09-12 14:44:20 -070014663 memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014664 return 0;
14665}
14666
David S. Miller59e6b432005-05-18 22:50:10 -070014667#define BOUNDARY_SINGLE_CACHELINE 1
14668#define BOUNDARY_MULTI_CACHELINE 2
14669
14670static u32 __devinit tg3_calc_dma_bndry(struct tg3 *tp, u32 val)
14671{
14672 int cacheline_size;
14673 u8 byte;
14674 int goal;
14675
14676 pci_read_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE, &byte);
14677 if (byte == 0)
14678 cacheline_size = 1024;
14679 else
14680 cacheline_size = (int) byte * 4;
14681
14682 /* On 5703 and later chips, the boundary bits have no
14683 * effect.
14684 */
14685 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
14686 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701 &&
Joe Perches63c3a662011-04-26 08:12:10 +000014687 !tg3_flag(tp, PCI_EXPRESS))
David S. Miller59e6b432005-05-18 22:50:10 -070014688 goto out;
14689
14690#if defined(CONFIG_PPC64) || defined(CONFIG_IA64) || defined(CONFIG_PARISC)
14691 goal = BOUNDARY_MULTI_CACHELINE;
14692#else
14693#if defined(CONFIG_SPARC64) || defined(CONFIG_ALPHA)
14694 goal = BOUNDARY_SINGLE_CACHELINE;
14695#else
14696 goal = 0;
14697#endif
14698#endif
14699
Joe Perches63c3a662011-04-26 08:12:10 +000014700 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000014701 val = goal ? 0 : DMA_RWCTRL_DIS_CACHE_ALIGNMENT;
14702 goto out;
14703 }
14704
David S. Miller59e6b432005-05-18 22:50:10 -070014705 if (!goal)
14706 goto out;
14707
14708 /* PCI controllers on most RISC systems tend to disconnect
14709 * when a device tries to burst across a cache-line boundary.
14710 * Therefore, letting tg3 do so just wastes PCI bandwidth.
14711 *
14712 * Unfortunately, for PCI-E there are only limited
14713 * write-side controls for this, and thus for reads
14714 * we will still get the disconnects. We'll also waste
14715 * these PCI cycles for both read and write for chips
14716 * other than 5700 and 5701 which do not implement the
14717 * boundary bits.
14718 */
Joe Perches63c3a662011-04-26 08:12:10 +000014719 if (tg3_flag(tp, PCIX_MODE) && !tg3_flag(tp, PCI_EXPRESS)) {
David S. Miller59e6b432005-05-18 22:50:10 -070014720 switch (cacheline_size) {
14721 case 16:
14722 case 32:
14723 case 64:
14724 case 128:
14725 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14726 val |= (DMA_RWCTRL_READ_BNDRY_128_PCIX |
14727 DMA_RWCTRL_WRITE_BNDRY_128_PCIX);
14728 } else {
14729 val |= (DMA_RWCTRL_READ_BNDRY_384_PCIX |
14730 DMA_RWCTRL_WRITE_BNDRY_384_PCIX);
14731 }
14732 break;
14733
14734 case 256:
14735 val |= (DMA_RWCTRL_READ_BNDRY_256_PCIX |
14736 DMA_RWCTRL_WRITE_BNDRY_256_PCIX);
14737 break;
14738
14739 default:
14740 val |= (DMA_RWCTRL_READ_BNDRY_384_PCIX |
14741 DMA_RWCTRL_WRITE_BNDRY_384_PCIX);
14742 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070014743 }
Joe Perches63c3a662011-04-26 08:12:10 +000014744 } else if (tg3_flag(tp, PCI_EXPRESS)) {
David S. Miller59e6b432005-05-18 22:50:10 -070014745 switch (cacheline_size) {
14746 case 16:
14747 case 32:
14748 case 64:
14749 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14750 val &= ~DMA_RWCTRL_WRITE_BNDRY_DISAB_PCIE;
14751 val |= DMA_RWCTRL_WRITE_BNDRY_64_PCIE;
14752 break;
14753 }
14754 /* fallthrough */
14755 case 128:
14756 default:
14757 val &= ~DMA_RWCTRL_WRITE_BNDRY_DISAB_PCIE;
14758 val |= DMA_RWCTRL_WRITE_BNDRY_128_PCIE;
14759 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070014760 }
David S. Miller59e6b432005-05-18 22:50:10 -070014761 } else {
14762 switch (cacheline_size) {
14763 case 16:
14764 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14765 val |= (DMA_RWCTRL_READ_BNDRY_16 |
14766 DMA_RWCTRL_WRITE_BNDRY_16);
14767 break;
14768 }
14769 /* fallthrough */
14770 case 32:
14771 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14772 val |= (DMA_RWCTRL_READ_BNDRY_32 |
14773 DMA_RWCTRL_WRITE_BNDRY_32);
14774 break;
14775 }
14776 /* fallthrough */
14777 case 64:
14778 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14779 val |= (DMA_RWCTRL_READ_BNDRY_64 |
14780 DMA_RWCTRL_WRITE_BNDRY_64);
14781 break;
14782 }
14783 /* fallthrough */
14784 case 128:
14785 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14786 val |= (DMA_RWCTRL_READ_BNDRY_128 |
14787 DMA_RWCTRL_WRITE_BNDRY_128);
14788 break;
14789 }
14790 /* fallthrough */
14791 case 256:
14792 val |= (DMA_RWCTRL_READ_BNDRY_256 |
14793 DMA_RWCTRL_WRITE_BNDRY_256);
14794 break;
14795 case 512:
14796 val |= (DMA_RWCTRL_READ_BNDRY_512 |
14797 DMA_RWCTRL_WRITE_BNDRY_512);
14798 break;
14799 case 1024:
14800 default:
14801 val |= (DMA_RWCTRL_READ_BNDRY_1024 |
14802 DMA_RWCTRL_WRITE_BNDRY_1024);
14803 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070014804 }
David S. Miller59e6b432005-05-18 22:50:10 -070014805 }
14806
14807out:
14808 return val;
14809}
14810
Linus Torvalds1da177e2005-04-16 15:20:36 -070014811static int __devinit tg3_do_test_dma(struct tg3 *tp, u32 *buf, dma_addr_t buf_dma, int size, int to_device)
14812{
14813 struct tg3_internal_buffer_desc test_desc;
14814 u32 sram_dma_descs;
14815 int i, ret;
14816
14817 sram_dma_descs = NIC_SRAM_DMA_DESC_POOL_BASE;
14818
14819 tw32(FTQ_RCVBD_COMP_FIFO_ENQDEQ, 0);
14820 tw32(FTQ_RCVDATA_COMP_FIFO_ENQDEQ, 0);
14821 tw32(RDMAC_STATUS, 0);
14822 tw32(WDMAC_STATUS, 0);
14823
14824 tw32(BUFMGR_MODE, 0);
14825 tw32(FTQ_RESET, 0);
14826
14827 test_desc.addr_hi = ((u64) buf_dma) >> 32;
14828 test_desc.addr_lo = buf_dma & 0xffffffff;
14829 test_desc.nic_mbuf = 0x00002100;
14830 test_desc.len = size;
14831
14832 /*
14833 * HP ZX1 was seeing test failures for 5701 cards running at 33Mhz
14834 * the *second* time the tg3 driver was getting loaded after an
14835 * initial scan.
14836 *
14837 * Broadcom tells me:
14838 * ...the DMA engine is connected to the GRC block and a DMA
14839 * reset may affect the GRC block in some unpredictable way...
14840 * The behavior of resets to individual blocks has not been tested.
14841 *
14842 * Broadcom noted the GRC reset will also reset all sub-components.
14843 */
14844 if (to_device) {
14845 test_desc.cqid_sqid = (13 << 8) | 2;
14846
14847 tw32_f(RDMAC_MODE, RDMAC_MODE_ENABLE);
14848 udelay(40);
14849 } else {
14850 test_desc.cqid_sqid = (16 << 8) | 7;
14851
14852 tw32_f(WDMAC_MODE, WDMAC_MODE_ENABLE);
14853 udelay(40);
14854 }
14855 test_desc.flags = 0x00000005;
14856
14857 for (i = 0; i < (sizeof(test_desc) / sizeof(u32)); i++) {
14858 u32 val;
14859
14860 val = *(((u32 *)&test_desc) + i);
14861 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR,
14862 sram_dma_descs + (i * sizeof(u32)));
14863 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_DATA, val);
14864 }
14865 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, 0);
14866
Matt Carlson859a588792010-04-05 10:19:28 +000014867 if (to_device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014868 tw32(FTQ_DMA_HIGH_READ_FIFO_ENQDEQ, sram_dma_descs);
Matt Carlson859a588792010-04-05 10:19:28 +000014869 else
Linus Torvalds1da177e2005-04-16 15:20:36 -070014870 tw32(FTQ_DMA_HIGH_WRITE_FIFO_ENQDEQ, sram_dma_descs);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014871
14872 ret = -ENODEV;
14873 for (i = 0; i < 40; i++) {
14874 u32 val;
14875
14876 if (to_device)
14877 val = tr32(FTQ_RCVBD_COMP_FIFO_ENQDEQ);
14878 else
14879 val = tr32(FTQ_RCVDATA_COMP_FIFO_ENQDEQ);
14880 if ((val & 0xffff) == sram_dma_descs) {
14881 ret = 0;
14882 break;
14883 }
14884
14885 udelay(100);
14886 }
14887
14888 return ret;
14889}
14890
David S. Millerded73402005-05-23 13:59:47 -070014891#define TEST_BUFFER_SIZE 0x2000
Linus Torvalds1da177e2005-04-16 15:20:36 -070014892
Matt Carlson41434702011-03-09 16:58:22 +000014893static DEFINE_PCI_DEVICE_TABLE(tg3_dma_wait_state_chipsets) = {
Joe Perches895950c2010-12-21 02:16:08 -080014894 { PCI_DEVICE(PCI_VENDOR_ID_APPLE, PCI_DEVICE_ID_APPLE_UNI_N_PCI15) },
14895 { },
14896};
14897
Linus Torvalds1da177e2005-04-16 15:20:36 -070014898static int __devinit tg3_test_dma(struct tg3 *tp)
14899{
14900 dma_addr_t buf_dma;
David S. Miller59e6b432005-05-18 22:50:10 -070014901 u32 *buf, saved_dma_rwctrl;
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000014902 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014903
Matt Carlson4bae65c2010-11-24 08:31:52 +000014904 buf = dma_alloc_coherent(&tp->pdev->dev, TEST_BUFFER_SIZE,
14905 &buf_dma, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014906 if (!buf) {
14907 ret = -ENOMEM;
14908 goto out_nofree;
14909 }
14910
14911 tp->dma_rwctrl = ((0x7 << DMA_RWCTRL_PCI_WRITE_CMD_SHIFT) |
14912 (0x6 << DMA_RWCTRL_PCI_READ_CMD_SHIFT));
14913
David S. Miller59e6b432005-05-18 22:50:10 -070014914 tp->dma_rwctrl = tg3_calc_dma_bndry(tp, tp->dma_rwctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014915
Joe Perches63c3a662011-04-26 08:12:10 +000014916 if (tg3_flag(tp, 57765_PLUS))
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000014917 goto out;
14918
Joe Perches63c3a662011-04-26 08:12:10 +000014919 if (tg3_flag(tp, PCI_EXPRESS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070014920 /* DMA read watermark not used on PCIE */
14921 tp->dma_rwctrl |= 0x00180000;
Joe Perches63c3a662011-04-26 08:12:10 +000014922 } else if (!tg3_flag(tp, PCIX_MODE)) {
Michael Chan85e94ce2005-04-21 17:05:28 -070014923 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 ||
14924 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014925 tp->dma_rwctrl |= 0x003f0000;
14926 else
14927 tp->dma_rwctrl |= 0x003f000f;
14928 } else {
14929 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
14930 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
14931 u32 ccval = (tr32(TG3PCI_CLOCK_CTRL) & 0x1f);
Michael Chan49afdeb2007-02-13 12:17:03 -080014932 u32 read_water = 0x7;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014933
Michael Chan4a29cc22006-03-19 13:21:12 -080014934 /* If the 5704 is behind the EPB bridge, we can
14935 * do the less restrictive ONE_DMA workaround for
14936 * better performance.
14937 */
Joe Perches63c3a662011-04-26 08:12:10 +000014938 if (tg3_flag(tp, 40BIT_DMA_BUG) &&
Michael Chan4a29cc22006-03-19 13:21:12 -080014939 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704)
14940 tp->dma_rwctrl |= 0x8000;
14941 else if (ccval == 0x6 || ccval == 0x7)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014942 tp->dma_rwctrl |= DMA_RWCTRL_ONE_DMA;
14943
Michael Chan49afdeb2007-02-13 12:17:03 -080014944 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703)
14945 read_water = 4;
David S. Miller59e6b432005-05-18 22:50:10 -070014946 /* Set bit 23 to enable PCIX hw bug fix */
Michael Chan49afdeb2007-02-13 12:17:03 -080014947 tp->dma_rwctrl |=
14948 (read_water << DMA_RWCTRL_READ_WATER_SHIFT) |
14949 (0x3 << DMA_RWCTRL_WRITE_WATER_SHIFT) |
14950 (1 << 23);
Michael Chan4cf78e42005-07-25 12:29:19 -070014951 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780) {
14952 /* 5780 always in PCIX mode */
14953 tp->dma_rwctrl |= 0x00144000;
Michael Chana4e2b342005-10-26 15:46:52 -070014954 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
14955 /* 5714 always in PCIX mode */
14956 tp->dma_rwctrl |= 0x00148000;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014957 } else {
14958 tp->dma_rwctrl |= 0x001b000f;
14959 }
14960 }
14961
14962 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
14963 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704)
14964 tp->dma_rwctrl &= 0xfffffff0;
14965
14966 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
14967 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
14968 /* Remove this if it causes problems for some boards. */
14969 tp->dma_rwctrl |= DMA_RWCTRL_USE_MEM_READ_MULT;
14970
14971 /* On 5700/5701 chips, we need to set this bit.
14972 * Otherwise the chip will issue cacheline transactions
14973 * to streamable DMA memory with not all the byte
14974 * enables turned on. This is an error on several
14975 * RISC PCI controllers, in particular sparc64.
14976 *
14977 * On 5703/5704 chips, this bit has been reassigned
14978 * a different meaning. In particular, it is used
14979 * on those chips to enable a PCI-X workaround.
14980 */
14981 tp->dma_rwctrl |= DMA_RWCTRL_ASSERT_ALL_BE;
14982 }
14983
14984 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
14985
14986#if 0
14987 /* Unneeded, already done by tg3_get_invariants. */
14988 tg3_switch_clocks(tp);
14989#endif
14990
Linus Torvalds1da177e2005-04-16 15:20:36 -070014991 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
14992 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701)
14993 goto out;
14994
David S. Miller59e6b432005-05-18 22:50:10 -070014995 /* It is best to perform DMA test with maximum write burst size
14996 * to expose the 5700/5701 write DMA bug.
14997 */
14998 saved_dma_rwctrl = tp->dma_rwctrl;
14999 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
15000 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
15001
Linus Torvalds1da177e2005-04-16 15:20:36 -070015002 while (1) {
15003 u32 *p = buf, i;
15004
15005 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++)
15006 p[i] = i;
15007
15008 /* Send the buffer to the chip. */
15009 ret = tg3_do_test_dma(tp, buf, buf_dma, TEST_BUFFER_SIZE, 1);
15010 if (ret) {
Matt Carlson2445e462010-04-05 10:19:21 +000015011 dev_err(&tp->pdev->dev,
15012 "%s: Buffer write failed. err = %d\n",
15013 __func__, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015014 break;
15015 }
15016
15017#if 0
15018 /* validate data reached card RAM correctly. */
15019 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++) {
15020 u32 val;
15021 tg3_read_mem(tp, 0x2100 + (i*4), &val);
15022 if (le32_to_cpu(val) != p[i]) {
Matt Carlson2445e462010-04-05 10:19:21 +000015023 dev_err(&tp->pdev->dev,
15024 "%s: Buffer corrupted on device! "
15025 "(%d != %d)\n", __func__, val, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015026 /* ret = -ENODEV here? */
15027 }
15028 p[i] = 0;
15029 }
15030#endif
15031 /* Now read it back. */
15032 ret = tg3_do_test_dma(tp, buf, buf_dma, TEST_BUFFER_SIZE, 0);
15033 if (ret) {
Matt Carlson5129c3a2010-04-05 10:19:23 +000015034 dev_err(&tp->pdev->dev, "%s: Buffer read failed. "
15035 "err = %d\n", __func__, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015036 break;
15037 }
15038
15039 /* Verify it. */
15040 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++) {
15041 if (p[i] == i)
15042 continue;
15043
David S. Miller59e6b432005-05-18 22:50:10 -070015044 if ((tp->dma_rwctrl & DMA_RWCTRL_WRITE_BNDRY_MASK) !=
15045 DMA_RWCTRL_WRITE_BNDRY_16) {
15046 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015047 tp->dma_rwctrl |= DMA_RWCTRL_WRITE_BNDRY_16;
15048 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
15049 break;
15050 } else {
Matt Carlson2445e462010-04-05 10:19:21 +000015051 dev_err(&tp->pdev->dev,
15052 "%s: Buffer corrupted on read back! "
15053 "(%d != %d)\n", __func__, p[i], i);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015054 ret = -ENODEV;
15055 goto out;
15056 }
15057 }
15058
15059 if (i == (TEST_BUFFER_SIZE / sizeof(u32))) {
15060 /* Success. */
15061 ret = 0;
15062 break;
15063 }
15064 }
David S. Miller59e6b432005-05-18 22:50:10 -070015065 if ((tp->dma_rwctrl & DMA_RWCTRL_WRITE_BNDRY_MASK) !=
15066 DMA_RWCTRL_WRITE_BNDRY_16) {
15067 /* DMA test passed without adjusting DMA boundary,
Michael Chan6d1cfba2005-06-08 14:13:14 -070015068 * now look for chipsets that are known to expose the
15069 * DMA bug without failing the test.
David S. Miller59e6b432005-05-18 22:50:10 -070015070 */
Matt Carlson41434702011-03-09 16:58:22 +000015071 if (pci_dev_present(tg3_dma_wait_state_chipsets)) {
Michael Chan6d1cfba2005-06-08 14:13:14 -070015072 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
15073 tp->dma_rwctrl |= DMA_RWCTRL_WRITE_BNDRY_16;
Matt Carlson859a588792010-04-05 10:19:28 +000015074 } else {
Michael Chan6d1cfba2005-06-08 14:13:14 -070015075 /* Safe to use the calculated DMA boundary. */
15076 tp->dma_rwctrl = saved_dma_rwctrl;
Matt Carlson859a588792010-04-05 10:19:28 +000015077 }
Michael Chan6d1cfba2005-06-08 14:13:14 -070015078
David S. Miller59e6b432005-05-18 22:50:10 -070015079 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
15080 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015081
15082out:
Matt Carlson4bae65c2010-11-24 08:31:52 +000015083 dma_free_coherent(&tp->pdev->dev, TEST_BUFFER_SIZE, buf, buf_dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015084out_nofree:
15085 return ret;
15086}
15087
Linus Torvalds1da177e2005-04-16 15:20:36 -070015088static void __devinit tg3_init_bufmgr_config(struct tg3 *tp)
15089{
Joe Perches63c3a662011-04-26 08:12:10 +000015090 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlson666bc832010-01-20 16:58:03 +000015091 tp->bufmgr_config.mbuf_read_dma_low_water =
15092 DEFAULT_MB_RDMA_LOW_WATER_5705;
15093 tp->bufmgr_config.mbuf_mac_rx_low_water =
15094 DEFAULT_MB_MACRX_LOW_WATER_57765;
15095 tp->bufmgr_config.mbuf_high_water =
15096 DEFAULT_MB_HIGH_WATER_57765;
15097
15098 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
15099 DEFAULT_MB_RDMA_LOW_WATER_5705;
15100 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
15101 DEFAULT_MB_MACRX_LOW_WATER_JUMBO_57765;
15102 tp->bufmgr_config.mbuf_high_water_jumbo =
15103 DEFAULT_MB_HIGH_WATER_JUMBO_57765;
Joe Perches63c3a662011-04-26 08:12:10 +000015104 } else if (tg3_flag(tp, 5705_PLUS)) {
Michael Chanfdfec1722005-07-25 12:31:48 -070015105 tp->bufmgr_config.mbuf_read_dma_low_water =
15106 DEFAULT_MB_RDMA_LOW_WATER_5705;
15107 tp->bufmgr_config.mbuf_mac_rx_low_water =
15108 DEFAULT_MB_MACRX_LOW_WATER_5705;
15109 tp->bufmgr_config.mbuf_high_water =
15110 DEFAULT_MB_HIGH_WATER_5705;
Michael Chanb5d37722006-09-27 16:06:21 -070015111 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
15112 tp->bufmgr_config.mbuf_mac_rx_low_water =
15113 DEFAULT_MB_MACRX_LOW_WATER_5906;
15114 tp->bufmgr_config.mbuf_high_water =
15115 DEFAULT_MB_HIGH_WATER_5906;
15116 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015117
Michael Chanfdfec1722005-07-25 12:31:48 -070015118 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
15119 DEFAULT_MB_RDMA_LOW_WATER_JUMBO_5780;
15120 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
15121 DEFAULT_MB_MACRX_LOW_WATER_JUMBO_5780;
15122 tp->bufmgr_config.mbuf_high_water_jumbo =
15123 DEFAULT_MB_HIGH_WATER_JUMBO_5780;
15124 } else {
15125 tp->bufmgr_config.mbuf_read_dma_low_water =
15126 DEFAULT_MB_RDMA_LOW_WATER;
15127 tp->bufmgr_config.mbuf_mac_rx_low_water =
15128 DEFAULT_MB_MACRX_LOW_WATER;
15129 tp->bufmgr_config.mbuf_high_water =
15130 DEFAULT_MB_HIGH_WATER;
15131
15132 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
15133 DEFAULT_MB_RDMA_LOW_WATER_JUMBO;
15134 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
15135 DEFAULT_MB_MACRX_LOW_WATER_JUMBO;
15136 tp->bufmgr_config.mbuf_high_water_jumbo =
15137 DEFAULT_MB_HIGH_WATER_JUMBO;
15138 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015139
15140 tp->bufmgr_config.dma_low_water = DEFAULT_DMA_LOW_WATER;
15141 tp->bufmgr_config.dma_high_water = DEFAULT_DMA_HIGH_WATER;
15142}
15143
15144static char * __devinit tg3_phy_string(struct tg3 *tp)
15145{
Matt Carlson79eb6902010-02-17 15:17:03 +000015146 switch (tp->phy_id & TG3_PHY_ID_MASK) {
15147 case TG3_PHY_ID_BCM5400: return "5400";
15148 case TG3_PHY_ID_BCM5401: return "5401";
15149 case TG3_PHY_ID_BCM5411: return "5411";
15150 case TG3_PHY_ID_BCM5701: return "5701";
15151 case TG3_PHY_ID_BCM5703: return "5703";
15152 case TG3_PHY_ID_BCM5704: return "5704";
15153 case TG3_PHY_ID_BCM5705: return "5705";
15154 case TG3_PHY_ID_BCM5750: return "5750";
15155 case TG3_PHY_ID_BCM5752: return "5752";
15156 case TG3_PHY_ID_BCM5714: return "5714";
15157 case TG3_PHY_ID_BCM5780: return "5780";
15158 case TG3_PHY_ID_BCM5755: return "5755";
15159 case TG3_PHY_ID_BCM5787: return "5787";
15160 case TG3_PHY_ID_BCM5784: return "5784";
15161 case TG3_PHY_ID_BCM5756: return "5722/5756";
15162 case TG3_PHY_ID_BCM5906: return "5906";
15163 case TG3_PHY_ID_BCM5761: return "5761";
15164 case TG3_PHY_ID_BCM5718C: return "5718C";
15165 case TG3_PHY_ID_BCM5718S: return "5718S";
15166 case TG3_PHY_ID_BCM57765: return "57765";
Matt Carlson302b5002010-06-05 17:24:38 +000015167 case TG3_PHY_ID_BCM5719C: return "5719C";
Matt Carlson6418f2c2011-04-05 14:22:49 +000015168 case TG3_PHY_ID_BCM5720C: return "5720C";
Matt Carlson79eb6902010-02-17 15:17:03 +000015169 case TG3_PHY_ID_BCM8002: return "8002/serdes";
Linus Torvalds1da177e2005-04-16 15:20:36 -070015170 case 0: return "serdes";
15171 default: return "unknown";
Stephen Hemminger855e1112008-04-16 16:37:28 -070015172 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015173}
15174
Michael Chanf9804dd2005-09-27 12:13:10 -070015175static char * __devinit tg3_bus_string(struct tg3 *tp, char *str)
15176{
Joe Perches63c3a662011-04-26 08:12:10 +000015177 if (tg3_flag(tp, PCI_EXPRESS)) {
Michael Chanf9804dd2005-09-27 12:13:10 -070015178 strcpy(str, "PCI Express");
15179 return str;
Joe Perches63c3a662011-04-26 08:12:10 +000015180 } else if (tg3_flag(tp, PCIX_MODE)) {
Michael Chanf9804dd2005-09-27 12:13:10 -070015181 u32 clock_ctrl = tr32(TG3PCI_CLOCK_CTRL) & 0x1f;
15182
15183 strcpy(str, "PCIX:");
15184
15185 if ((clock_ctrl == 7) ||
15186 ((tr32(GRC_MISC_CFG) & GRC_MISC_CFG_BOARD_ID_MASK) ==
15187 GRC_MISC_CFG_BOARD_ID_5704CIOBE))
15188 strcat(str, "133MHz");
15189 else if (clock_ctrl == 0)
15190 strcat(str, "33MHz");
15191 else if (clock_ctrl == 2)
15192 strcat(str, "50MHz");
15193 else if (clock_ctrl == 4)
15194 strcat(str, "66MHz");
15195 else if (clock_ctrl == 6)
15196 strcat(str, "100MHz");
Michael Chanf9804dd2005-09-27 12:13:10 -070015197 } else {
15198 strcpy(str, "PCI:");
Joe Perches63c3a662011-04-26 08:12:10 +000015199 if (tg3_flag(tp, PCI_HIGH_SPEED))
Michael Chanf9804dd2005-09-27 12:13:10 -070015200 strcat(str, "66MHz");
15201 else
15202 strcat(str, "33MHz");
15203 }
Joe Perches63c3a662011-04-26 08:12:10 +000015204 if (tg3_flag(tp, PCI_32BIT))
Michael Chanf9804dd2005-09-27 12:13:10 -070015205 strcat(str, ":32-bit");
15206 else
15207 strcat(str, ":64-bit");
15208 return str;
15209}
15210
Michael Chan8c2dc7e2005-12-19 16:26:02 -080015211static struct pci_dev * __devinit tg3_find_peer(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015212{
15213 struct pci_dev *peer;
15214 unsigned int func, devnr = tp->pdev->devfn & ~7;
15215
15216 for (func = 0; func < 8; func++) {
15217 peer = pci_get_slot(tp->pdev->bus, devnr | func);
15218 if (peer && peer != tp->pdev)
15219 break;
15220 pci_dev_put(peer);
15221 }
Michael Chan16fe9d72005-12-13 21:09:54 -080015222 /* 5704 can be configured in single-port mode, set peer to
15223 * tp->pdev in that case.
15224 */
15225 if (!peer) {
15226 peer = tp->pdev;
15227 return peer;
15228 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015229
15230 /*
15231 * We don't need to keep the refcount elevated; there's no way
15232 * to remove one half of this device without removing the other
15233 */
15234 pci_dev_put(peer);
15235
15236 return peer;
15237}
15238
David S. Miller15f98502005-05-18 22:49:26 -070015239static void __devinit tg3_init_coal(struct tg3 *tp)
15240{
15241 struct ethtool_coalesce *ec = &tp->coal;
15242
15243 memset(ec, 0, sizeof(*ec));
15244 ec->cmd = ETHTOOL_GCOALESCE;
15245 ec->rx_coalesce_usecs = LOW_RXCOL_TICKS;
15246 ec->tx_coalesce_usecs = LOW_TXCOL_TICKS;
15247 ec->rx_max_coalesced_frames = LOW_RXMAX_FRAMES;
15248 ec->tx_max_coalesced_frames = LOW_TXMAX_FRAMES;
15249 ec->rx_coalesce_usecs_irq = DEFAULT_RXCOAL_TICK_INT;
15250 ec->tx_coalesce_usecs_irq = DEFAULT_TXCOAL_TICK_INT;
15251 ec->rx_max_coalesced_frames_irq = DEFAULT_RXCOAL_MAXF_INT;
15252 ec->tx_max_coalesced_frames_irq = DEFAULT_TXCOAL_MAXF_INT;
15253 ec->stats_block_coalesce_usecs = DEFAULT_STAT_COAL_TICKS;
15254
15255 if (tp->coalesce_mode & (HOSTCC_MODE_CLRTICK_RXBD |
15256 HOSTCC_MODE_CLRTICK_TXBD)) {
15257 ec->rx_coalesce_usecs = LOW_RXCOL_TICKS_CLRTCKS;
15258 ec->rx_coalesce_usecs_irq = DEFAULT_RXCOAL_TICK_INT_CLRTCKS;
15259 ec->tx_coalesce_usecs = LOW_TXCOL_TICKS_CLRTCKS;
15260 ec->tx_coalesce_usecs_irq = DEFAULT_TXCOAL_TICK_INT_CLRTCKS;
15261 }
Michael Chand244c892005-07-05 14:42:33 -070015262
Joe Perches63c3a662011-04-26 08:12:10 +000015263 if (tg3_flag(tp, 5705_PLUS)) {
Michael Chand244c892005-07-05 14:42:33 -070015264 ec->rx_coalesce_usecs_irq = 0;
15265 ec->tx_coalesce_usecs_irq = 0;
15266 ec->stats_block_coalesce_usecs = 0;
15267 }
David S. Miller15f98502005-05-18 22:49:26 -070015268}
15269
Stephen Hemminger7c7d64b2008-11-19 22:25:36 -080015270static const struct net_device_ops tg3_netdev_ops = {
15271 .ndo_open = tg3_open,
15272 .ndo_stop = tg3_close,
Stephen Hemminger00829822008-11-20 20:14:53 -080015273 .ndo_start_xmit = tg3_start_xmit,
Eric Dumazet511d2222010-07-07 20:44:24 +000015274 .ndo_get_stats64 = tg3_get_stats64,
Stephen Hemminger00829822008-11-20 20:14:53 -080015275 .ndo_validate_addr = eth_validate_addr,
Jiri Pirkoafc4b132011-08-16 06:29:01 +000015276 .ndo_set_rx_mode = tg3_set_rx_mode,
Stephen Hemminger00829822008-11-20 20:14:53 -080015277 .ndo_set_mac_address = tg3_set_mac_addr,
15278 .ndo_do_ioctl = tg3_ioctl,
15279 .ndo_tx_timeout = tg3_tx_timeout,
15280 .ndo_change_mtu = tg3_change_mtu,
Michał Mirosławdc668912011-04-07 03:35:07 +000015281 .ndo_fix_features = tg3_fix_features,
Mahesh Bandewar06c03c02011-05-08 06:51:48 +000015282 .ndo_set_features = tg3_set_features,
Stephen Hemminger00829822008-11-20 20:14:53 -080015283#ifdef CONFIG_NET_POLL_CONTROLLER
15284 .ndo_poll_controller = tg3_poll_controller,
15285#endif
15286};
15287
Linus Torvalds1da177e2005-04-16 15:20:36 -070015288static int __devinit tg3_init_one(struct pci_dev *pdev,
15289 const struct pci_device_id *ent)
15290{
Linus Torvalds1da177e2005-04-16 15:20:36 -070015291 struct net_device *dev;
15292 struct tg3 *tp;
Matt Carlson646c9ed2009-09-01 12:58:41 +000015293 int i, err, pm_cap;
15294 u32 sndmbx, rcvmbx, intmbx;
Michael Chanf9804dd2005-09-27 12:13:10 -070015295 char str[40];
Michael Chan72f2afb2006-03-06 19:28:35 -080015296 u64 dma_mask, persist_dma_mask;
Michał Mirosławc8f44af2011-11-15 15:29:55 +000015297 netdev_features_t features = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015298
Joe Perches05dbe002010-02-17 19:44:19 +000015299 printk_once(KERN_INFO "%s\n", version);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015300
15301 err = pci_enable_device(pdev);
15302 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000015303 dev_err(&pdev->dev, "Cannot enable PCI device, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070015304 return err;
15305 }
15306
Linus Torvalds1da177e2005-04-16 15:20:36 -070015307 err = pci_request_regions(pdev, DRV_MODULE_NAME);
15308 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000015309 dev_err(&pdev->dev, "Cannot obtain PCI resources, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070015310 goto err_out_disable_pdev;
15311 }
15312
15313 pci_set_master(pdev);
15314
15315 /* Find power-management capability. */
15316 pm_cap = pci_find_capability(pdev, PCI_CAP_ID_PM);
15317 if (pm_cap == 0) {
Matt Carlson2445e462010-04-05 10:19:21 +000015318 dev_err(&pdev->dev,
15319 "Cannot find Power Management capability, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070015320 err = -EIO;
15321 goto err_out_free_res;
15322 }
15323
Matt Carlson16821282011-07-13 09:27:28 +000015324 err = pci_set_power_state(pdev, PCI_D0);
15325 if (err) {
15326 dev_err(&pdev->dev, "Transition to D0 failed, aborting\n");
15327 goto err_out_free_res;
15328 }
15329
Matt Carlsonfe5f5782009-09-01 13:09:39 +000015330 dev = alloc_etherdev_mq(sizeof(*tp), TG3_IRQ_MAX_VECS);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015331 if (!dev) {
Matt Carlson2445e462010-04-05 10:19:21 +000015332 dev_err(&pdev->dev, "Etherdev alloc failed, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070015333 err = -ENOMEM;
Matt Carlson16821282011-07-13 09:27:28 +000015334 goto err_out_power_down;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015335 }
15336
Linus Torvalds1da177e2005-04-16 15:20:36 -070015337 SET_NETDEV_DEV(dev, &pdev->dev);
15338
Linus Torvalds1da177e2005-04-16 15:20:36 -070015339 tp = netdev_priv(dev);
15340 tp->pdev = pdev;
15341 tp->dev = dev;
15342 tp->pm_cap = pm_cap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015343 tp->rx_mode = TG3_DEF_RX_MODE;
15344 tp->tx_mode = TG3_DEF_TX_MODE;
Matt Carlson8ef21422008-05-02 16:47:53 -070015345
Linus Torvalds1da177e2005-04-16 15:20:36 -070015346 if (tg3_debug > 0)
15347 tp->msg_enable = tg3_debug;
15348 else
15349 tp->msg_enable = TG3_DEF_MSG_ENABLE;
15350
15351 /* The word/byte swap controls here control register access byte
15352 * swapping. DMA data byte swapping is controlled in the GRC_MODE
15353 * setting below.
15354 */
15355 tp->misc_host_ctrl =
15356 MISC_HOST_CTRL_MASK_PCI_INT |
15357 MISC_HOST_CTRL_WORD_SWAP |
15358 MISC_HOST_CTRL_INDIR_ACCESS |
15359 MISC_HOST_CTRL_PCISTATE_RW;
15360
15361 /* The NONFRM (non-frame) byte/word swap controls take effect
15362 * on descriptor entries, anything which isn't packet data.
15363 *
15364 * The StrongARM chips on the board (one for tx, one for rx)
15365 * are running in big-endian mode.
15366 */
15367 tp->grc_mode = (GRC_MODE_WSWAP_DATA | GRC_MODE_BSWAP_DATA |
15368 GRC_MODE_WSWAP_NONFRM_DATA);
15369#ifdef __BIG_ENDIAN
15370 tp->grc_mode |= GRC_MODE_BSWAP_NONFRM_DATA;
15371#endif
15372 spin_lock_init(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015373 spin_lock_init(&tp->indirect_lock);
David Howellsc4028952006-11-22 14:57:56 +000015374 INIT_WORK(&tp->reset_task, tg3_reset_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015375
Matt Carlsond5fe4882008-11-21 17:20:32 -080015376 tp->regs = pci_ioremap_bar(pdev, BAR_0);
Andy Gospodarekab0049b2007-09-06 20:42:14 +010015377 if (!tp->regs) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015378 dev_err(&pdev->dev, "Cannot map device registers, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070015379 err = -ENOMEM;
15380 goto err_out_free_dev;
15381 }
15382
Matt Carlsonc9cab242011-07-13 09:27:27 +000015383 if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761 ||
15384 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761E ||
15385 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761S ||
15386 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761SE ||
15387 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717 ||
15388 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718 ||
15389 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5719 ||
15390 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5720) {
15391 tg3_flag_set(tp, ENABLE_APE);
15392 tp->aperegs = pci_ioremap_bar(pdev, BAR_2);
15393 if (!tp->aperegs) {
15394 dev_err(&pdev->dev,
15395 "Cannot map APE registers, aborting\n");
15396 err = -ENOMEM;
15397 goto err_out_iounmap;
15398 }
15399 }
15400
Linus Torvalds1da177e2005-04-16 15:20:36 -070015401 tp->rx_pending = TG3_DEF_RX_RING_PENDING;
15402 tp->rx_jumbo_pending = TG3_DEF_RX_JUMBO_RING_PENDING;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015403
Linus Torvalds1da177e2005-04-16 15:20:36 -070015404 dev->ethtool_ops = &tg3_ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015405 dev->watchdog_timeo = TG3_TX_TIMEOUT;
Matt Carlson2ffcc982011-05-19 12:12:44 +000015406 dev->netdev_ops = &tg3_netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015407 dev->irq = pdev->irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015408
15409 err = tg3_get_invariants(tp);
15410 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015411 dev_err(&pdev->dev,
15412 "Problem fetching invariants of chip, aborting\n");
Matt Carlsonc9cab242011-07-13 09:27:27 +000015413 goto err_out_apeunmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015414 }
15415
Michael Chan4a29cc22006-03-19 13:21:12 -080015416 /* The EPB bridge inside 5714, 5715, and 5780 and any
15417 * device behind the EPB cannot support DMA addresses > 40-bit.
Michael Chan72f2afb2006-03-06 19:28:35 -080015418 * On 64-bit systems with IOMMU, use 40-bit dma_mask.
15419 * On 64-bit systems without IOMMU, use 64-bit dma_mask and
15420 * do DMA address check in tg3_start_xmit().
15421 */
Joe Perches63c3a662011-04-26 08:12:10 +000015422 if (tg3_flag(tp, IS_5788))
Yang Hongyang284901a2009-04-06 19:01:15 -070015423 persist_dma_mask = dma_mask = DMA_BIT_MASK(32);
Joe Perches63c3a662011-04-26 08:12:10 +000015424 else if (tg3_flag(tp, 40BIT_DMA_BUG)) {
Yang Hongyang50cf1562009-04-06 19:01:14 -070015425 persist_dma_mask = dma_mask = DMA_BIT_MASK(40);
Michael Chan72f2afb2006-03-06 19:28:35 -080015426#ifdef CONFIG_HIGHMEM
Yang Hongyang6a355282009-04-06 19:01:13 -070015427 dma_mask = DMA_BIT_MASK(64);
Michael Chan72f2afb2006-03-06 19:28:35 -080015428#endif
Michael Chan4a29cc22006-03-19 13:21:12 -080015429 } else
Yang Hongyang6a355282009-04-06 19:01:13 -070015430 persist_dma_mask = dma_mask = DMA_BIT_MASK(64);
Michael Chan72f2afb2006-03-06 19:28:35 -080015431
15432 /* Configure DMA attributes. */
Yang Hongyang284901a2009-04-06 19:01:15 -070015433 if (dma_mask > DMA_BIT_MASK(32)) {
Michael Chan72f2afb2006-03-06 19:28:35 -080015434 err = pci_set_dma_mask(pdev, dma_mask);
15435 if (!err) {
Matt Carlson0da06062011-05-19 12:12:53 +000015436 features |= NETIF_F_HIGHDMA;
Michael Chan72f2afb2006-03-06 19:28:35 -080015437 err = pci_set_consistent_dma_mask(pdev,
15438 persist_dma_mask);
15439 if (err < 0) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015440 dev_err(&pdev->dev, "Unable to obtain 64 bit "
15441 "DMA for consistent allocations\n");
Matt Carlsonc9cab242011-07-13 09:27:27 +000015442 goto err_out_apeunmap;
Michael Chan72f2afb2006-03-06 19:28:35 -080015443 }
15444 }
15445 }
Yang Hongyang284901a2009-04-06 19:01:15 -070015446 if (err || dma_mask == DMA_BIT_MASK(32)) {
15447 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
Michael Chan72f2afb2006-03-06 19:28:35 -080015448 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015449 dev_err(&pdev->dev,
15450 "No usable DMA configuration, aborting\n");
Matt Carlsonc9cab242011-07-13 09:27:27 +000015451 goto err_out_apeunmap;
Michael Chan72f2afb2006-03-06 19:28:35 -080015452 }
15453 }
15454
Michael Chanfdfec1722005-07-25 12:31:48 -070015455 tg3_init_bufmgr_config(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015456
Matt Carlson0da06062011-05-19 12:12:53 +000015457 features |= NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
15458
15459 /* 5700 B0 chips do not support checksumming correctly due
15460 * to hardware bugs.
15461 */
15462 if (tp->pci_chip_rev_id != CHIPREV_ID_5700_B0) {
15463 features |= NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_RXCSUM;
15464
15465 if (tg3_flag(tp, 5755_PLUS))
15466 features |= NETIF_F_IPV6_CSUM;
15467 }
15468
Michael Chan4e3a7aa2006-03-20 17:47:44 -080015469 /* TSO is on by default on chips that support hardware TSO.
15470 * Firmware TSO on older chips gives lower performance, so it
15471 * is off by default, but can be enabled using ethtool.
15472 */
Joe Perches63c3a662011-04-26 08:12:10 +000015473 if ((tg3_flag(tp, HW_TSO_1) ||
15474 tg3_flag(tp, HW_TSO_2) ||
15475 tg3_flag(tp, HW_TSO_3)) &&
Matt Carlson0da06062011-05-19 12:12:53 +000015476 (features & NETIF_F_IP_CSUM))
15477 features |= NETIF_F_TSO;
Joe Perches63c3a662011-04-26 08:12:10 +000015478 if (tg3_flag(tp, HW_TSO_2) || tg3_flag(tp, HW_TSO_3)) {
Matt Carlson0da06062011-05-19 12:12:53 +000015479 if (features & NETIF_F_IPV6_CSUM)
15480 features |= NETIF_F_TSO6;
Joe Perches63c3a662011-04-26 08:12:10 +000015481 if (tg3_flag(tp, HW_TSO_3) ||
Matt Carlsone849cdc2009-11-13 13:03:38 +000015482 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson57e69832008-05-25 23:48:31 -070015483 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
15484 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX) ||
Joe Perches63c3a662011-04-26 08:12:10 +000015485 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Michał Mirosławdc668912011-04-07 03:35:07 +000015486 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
Matt Carlson0da06062011-05-19 12:12:53 +000015487 features |= NETIF_F_TSO_ECN;
Michael Chanb0026622006-07-03 19:42:14 -070015488 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015489
Matt Carlsond542fe22011-05-19 16:02:43 +000015490 dev->features |= features;
15491 dev->vlan_features |= features;
15492
Mahesh Bandewar06c03c02011-05-08 06:51:48 +000015493 /*
15494 * Add loopback capability only for a subset of devices that support
15495 * MAC-LOOPBACK. Eventually this need to be enhanced to allow INT-PHY
15496 * loopback for the remaining devices.
15497 */
15498 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5780 &&
15499 !tg3_flag(tp, CPMU_PRESENT))
15500 /* Add the loopback capability */
Matt Carlson0da06062011-05-19 12:12:53 +000015501 features |= NETIF_F_LOOPBACK;
15502
Matt Carlson0da06062011-05-19 12:12:53 +000015503 dev->hw_features |= features;
Mahesh Bandewar06c03c02011-05-08 06:51:48 +000015504
Linus Torvalds1da177e2005-04-16 15:20:36 -070015505 if (tp->pci_chip_rev_id == CHIPREV_ID_5705_A1 &&
Joe Perches63c3a662011-04-26 08:12:10 +000015506 !tg3_flag(tp, TSO_CAPABLE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070015507 !(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH)) {
Joe Perches63c3a662011-04-26 08:12:10 +000015508 tg3_flag_set(tp, MAX_RXPEND_64);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015509 tp->rx_pending = 63;
15510 }
15511
Linus Torvalds1da177e2005-04-16 15:20:36 -070015512 err = tg3_get_device_address(tp);
15513 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015514 dev_err(&pdev->dev,
15515 "Could not obtain valid ethernet address, aborting\n");
Matt Carlsonc9cab242011-07-13 09:27:27 +000015516 goto err_out_apeunmap;
Matt Carlson0d3031d2007-10-10 18:02:43 -070015517 }
15518
Matt Carlsonc88864d2007-11-12 21:07:01 -080015519 /*
15520 * Reset chip in case UNDI or EFI driver did not shutdown
15521 * DMA self test will enable WDMAC and we'll see (spurious)
15522 * pending DMA on the PCI bus at that point.
15523 */
15524 if ((tr32(HOSTCC_MODE) & HOSTCC_MODE_ENABLE) ||
15525 (tr32(WDMAC_MODE) & WDMAC_MODE_ENABLE)) {
15526 tw32(MEMARB_MODE, MEMARB_MODE_ENABLE);
15527 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
15528 }
15529
15530 err = tg3_test_dma(tp);
15531 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015532 dev_err(&pdev->dev, "DMA engine test failed, aborting\n");
Matt Carlsonc88864d2007-11-12 21:07:01 -080015533 goto err_out_apeunmap;
15534 }
15535
Matt Carlson78f90dc2009-11-13 13:03:42 +000015536 intmbx = MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW;
15537 rcvmbx = MAILBOX_RCVRET_CON_IDX_0 + TG3_64BIT_REG_LOW;
15538 sndmbx = MAILBOX_SNDHOST_PROD_IDX_0 + TG3_64BIT_REG_LOW;
Matt Carlson6fd45cb2010-09-15 08:59:57 +000015539 for (i = 0; i < tp->irq_max; i++) {
Matt Carlson78f90dc2009-11-13 13:03:42 +000015540 struct tg3_napi *tnapi = &tp->napi[i];
15541
15542 tnapi->tp = tp;
15543 tnapi->tx_pending = TG3_DEF_TX_RING_PENDING;
15544
15545 tnapi->int_mbox = intmbx;
Matt Carlson93a700a2011-08-31 11:44:54 +000015546 if (i <= 4)
Matt Carlson78f90dc2009-11-13 13:03:42 +000015547 intmbx += 0x8;
15548 else
15549 intmbx += 0x4;
15550
15551 tnapi->consmbox = rcvmbx;
15552 tnapi->prodmbox = sndmbx;
15553
Matt Carlson66cfd1b2010-09-30 10:34:30 +000015554 if (i)
Matt Carlson78f90dc2009-11-13 13:03:42 +000015555 tnapi->coal_now = HOSTCC_MODE_COAL_VEC1_NOW << (i - 1);
Matt Carlson66cfd1b2010-09-30 10:34:30 +000015556 else
Matt Carlson78f90dc2009-11-13 13:03:42 +000015557 tnapi->coal_now = HOSTCC_MODE_NOW;
Matt Carlson78f90dc2009-11-13 13:03:42 +000015558
Joe Perches63c3a662011-04-26 08:12:10 +000015559 if (!tg3_flag(tp, SUPPORT_MSIX))
Matt Carlson78f90dc2009-11-13 13:03:42 +000015560 break;
15561
15562 /*
15563 * If we support MSIX, we'll be using RSS. If we're using
15564 * RSS, the first vector only handles link interrupts and the
15565 * remaining vectors handle rx and tx interrupts. Reuse the
15566 * mailbox values for the next iteration. The values we setup
15567 * above are still useful for the single vectored mode.
15568 */
15569 if (!i)
15570 continue;
15571
15572 rcvmbx += 0x8;
15573
15574 if (sndmbx & 0x4)
15575 sndmbx -= 0x4;
15576 else
15577 sndmbx += 0xc;
15578 }
15579
Matt Carlsonc88864d2007-11-12 21:07:01 -080015580 tg3_init_coal(tp);
15581
Michael Chanc49a1562006-12-17 17:07:29 -080015582 pci_set_drvdata(pdev, dev);
15583
Matt Carlsoncd0d7222011-07-13 09:27:33 +000015584 if (tg3_flag(tp, 5717_PLUS)) {
15585 /* Resume a low-power mode */
15586 tg3_frob_aux_power(tp, false);
15587 }
15588
Linus Torvalds1da177e2005-04-16 15:20:36 -070015589 err = register_netdev(dev);
15590 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015591 dev_err(&pdev->dev, "Cannot register net device, aborting\n");
Matt Carlson0d3031d2007-10-10 18:02:43 -070015592 goto err_out_apeunmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015593 }
15594
Joe Perches05dbe002010-02-17 19:44:19 +000015595 netdev_info(dev, "Tigon3 [partno(%s) rev %04x] (%s) MAC address %pM\n",
15596 tp->board_part_number,
15597 tp->pci_chip_rev_id,
15598 tg3_bus_string(tp, str),
15599 dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015600
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015601 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000015602 struct phy_device *phydev;
15603 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlson5129c3a2010-04-05 10:19:23 +000015604 netdev_info(dev,
15605 "attached PHY driver [%s] (mii_bus:phy_addr=%s)\n",
Joe Perches05dbe002010-02-17 19:44:19 +000015606 phydev->drv->name, dev_name(&phydev->dev));
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015607 } else {
15608 char *ethtype;
15609
15610 if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
15611 ethtype = "10/100Base-TX";
15612 else if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
15613 ethtype = "1000Base-SX";
15614 else
15615 ethtype = "10/100/1000Base-T";
15616
Matt Carlson5129c3a2010-04-05 10:19:23 +000015617 netdev_info(dev, "attached PHY is %s (%s Ethernet) "
Matt Carlson47007832011-04-20 07:57:43 +000015618 "(WireSpeed[%d], EEE[%d])\n",
15619 tg3_phy_string(tp), ethtype,
15620 (tp->phy_flags & TG3_PHYFLG_NO_ETH_WIRE_SPEED) == 0,
15621 (tp->phy_flags & TG3_PHYFLG_EEE_CAP) != 0);
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015622 }
Matt Carlsondf59c942008-11-03 16:52:56 -080015623
Joe Perches05dbe002010-02-17 19:44:19 +000015624 netdev_info(dev, "RXcsums[%d] LinkChgREG[%d] MIirq[%d] ASF[%d] TSOcap[%d]\n",
Michał Mirosławdc668912011-04-07 03:35:07 +000015625 (dev->features & NETIF_F_RXCSUM) != 0,
Joe Perches63c3a662011-04-26 08:12:10 +000015626 tg3_flag(tp, USE_LINKCHG_REG) != 0,
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015627 (tp->phy_flags & TG3_PHYFLG_USE_MI_INTERRUPT) != 0,
Joe Perches63c3a662011-04-26 08:12:10 +000015628 tg3_flag(tp, ENABLE_ASF) != 0,
15629 tg3_flag(tp, TSO_CAPABLE) != 0);
Joe Perches05dbe002010-02-17 19:44:19 +000015630 netdev_info(dev, "dma_rwctrl[%08x] dma_mask[%d-bit]\n",
15631 tp->dma_rwctrl,
15632 pdev->dma_mask == DMA_BIT_MASK(32) ? 32 :
15633 ((u64)pdev->dma_mask) == DMA_BIT_MASK(40) ? 40 : 64);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015634
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000015635 pci_save_state(pdev);
15636
Linus Torvalds1da177e2005-04-16 15:20:36 -070015637 return 0;
15638
Matt Carlson0d3031d2007-10-10 18:02:43 -070015639err_out_apeunmap:
15640 if (tp->aperegs) {
15641 iounmap(tp->aperegs);
15642 tp->aperegs = NULL;
15643 }
15644
Linus Torvalds1da177e2005-04-16 15:20:36 -070015645err_out_iounmap:
Michael Chan68929142005-08-09 20:17:14 -070015646 if (tp->regs) {
15647 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070015648 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070015649 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015650
15651err_out_free_dev:
15652 free_netdev(dev);
15653
Matt Carlson16821282011-07-13 09:27:28 +000015654err_out_power_down:
15655 pci_set_power_state(pdev, PCI_D3hot);
15656
Linus Torvalds1da177e2005-04-16 15:20:36 -070015657err_out_free_res:
15658 pci_release_regions(pdev);
15659
15660err_out_disable_pdev:
15661 pci_disable_device(pdev);
15662 pci_set_drvdata(pdev, NULL);
15663 return err;
15664}
15665
15666static void __devexit tg3_remove_one(struct pci_dev *pdev)
15667{
15668 struct net_device *dev = pci_get_drvdata(pdev);
15669
15670 if (dev) {
15671 struct tg3 *tp = netdev_priv(dev);
15672
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -080015673 if (tp->fw)
15674 release_firmware(tp->fw);
15675
Matt Carlsondb219972011-11-04 09:15:03 +000015676 tg3_reset_task_cancel(tp);
Matt Carlson158d7ab2008-05-29 01:37:54 -070015677
David S. Miller1805b2f2011-10-24 18:18:09 -040015678 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015679 tg3_phy_fini(tp);
Matt Carlson158d7ab2008-05-29 01:37:54 -070015680 tg3_mdio_fini(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015681 }
Matt Carlson158d7ab2008-05-29 01:37:54 -070015682
Linus Torvalds1da177e2005-04-16 15:20:36 -070015683 unregister_netdev(dev);
Matt Carlson0d3031d2007-10-10 18:02:43 -070015684 if (tp->aperegs) {
15685 iounmap(tp->aperegs);
15686 tp->aperegs = NULL;
15687 }
Michael Chan68929142005-08-09 20:17:14 -070015688 if (tp->regs) {
15689 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070015690 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070015691 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015692 free_netdev(dev);
15693 pci_release_regions(pdev);
15694 pci_disable_device(pdev);
15695 pci_set_drvdata(pdev, NULL);
15696 }
15697}
15698
Eric Dumazetaa6027c2011-01-01 05:22:46 +000015699#ifdef CONFIG_PM_SLEEP
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015700static int tg3_suspend(struct device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015701{
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015702 struct pci_dev *pdev = to_pci_dev(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015703 struct net_device *dev = pci_get_drvdata(pdev);
15704 struct tg3 *tp = netdev_priv(dev);
15705 int err;
15706
15707 if (!netif_running(dev))
15708 return 0;
15709
Matt Carlsondb219972011-11-04 09:15:03 +000015710 tg3_reset_task_cancel(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015711 tg3_phy_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015712 tg3_netif_stop(tp);
15713
15714 del_timer_sync(&tp->timer);
15715
David S. Millerf47c11e2005-06-24 20:18:35 -070015716 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015717 tg3_disable_ints(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -070015718 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015719
15720 netif_device_detach(dev);
15721
David S. Millerf47c11e2005-06-24 20:18:35 -070015722 tg3_full_lock(tp, 0);
Michael Chan944d9802005-05-29 14:57:48 -070015723 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Joe Perches63c3a662011-04-26 08:12:10 +000015724 tg3_flag_clear(tp, INIT_COMPLETE);
David S. Millerf47c11e2005-06-24 20:18:35 -070015725 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015726
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015727 err = tg3_power_down_prepare(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015728 if (err) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015729 int err2;
15730
David S. Millerf47c11e2005-06-24 20:18:35 -070015731 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015732
Joe Perches63c3a662011-04-26 08:12:10 +000015733 tg3_flag_set(tp, INIT_COMPLETE);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015734 err2 = tg3_restart_hw(tp, 1);
15735 if (err2)
Michael Chanb9ec6c12006-07-25 16:37:27 -070015736 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015737
15738 tp->timer.expires = jiffies + tp->timer_offset;
15739 add_timer(&tp->timer);
15740
15741 netif_device_attach(dev);
15742 tg3_netif_start(tp);
15743
Michael Chanb9ec6c12006-07-25 16:37:27 -070015744out:
David S. Millerf47c11e2005-06-24 20:18:35 -070015745 tg3_full_unlock(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015746
15747 if (!err2)
15748 tg3_phy_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015749 }
15750
15751 return err;
15752}
15753
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015754static int tg3_resume(struct device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015755{
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015756 struct pci_dev *pdev = to_pci_dev(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015757 struct net_device *dev = pci_get_drvdata(pdev);
15758 struct tg3 *tp = netdev_priv(dev);
15759 int err;
15760
15761 if (!netif_running(dev))
15762 return 0;
15763
Linus Torvalds1da177e2005-04-16 15:20:36 -070015764 netif_device_attach(dev);
15765
David S. Millerf47c11e2005-06-24 20:18:35 -070015766 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015767
Joe Perches63c3a662011-04-26 08:12:10 +000015768 tg3_flag_set(tp, INIT_COMPLETE);
Michael Chanb9ec6c12006-07-25 16:37:27 -070015769 err = tg3_restart_hw(tp, 1);
15770 if (err)
15771 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015772
15773 tp->timer.expires = jiffies + tp->timer_offset;
15774 add_timer(&tp->timer);
15775
Linus Torvalds1da177e2005-04-16 15:20:36 -070015776 tg3_netif_start(tp);
15777
Michael Chanb9ec6c12006-07-25 16:37:27 -070015778out:
David S. Millerf47c11e2005-06-24 20:18:35 -070015779 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015780
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015781 if (!err)
15782 tg3_phy_start(tp);
15783
Michael Chanb9ec6c12006-07-25 16:37:27 -070015784 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015785}
15786
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015787static SIMPLE_DEV_PM_OPS(tg3_pm_ops, tg3_suspend, tg3_resume);
Eric Dumazetaa6027c2011-01-01 05:22:46 +000015788#define TG3_PM_OPS (&tg3_pm_ops)
15789
15790#else
15791
15792#define TG3_PM_OPS NULL
15793
15794#endif /* CONFIG_PM_SLEEP */
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015795
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000015796/**
15797 * tg3_io_error_detected - called when PCI error is detected
15798 * @pdev: Pointer to PCI device
15799 * @state: The current pci connection state
15800 *
15801 * This function is called after a PCI bus error affecting
15802 * this device has been detected.
15803 */
15804static pci_ers_result_t tg3_io_error_detected(struct pci_dev *pdev,
15805 pci_channel_state_t state)
15806{
15807 struct net_device *netdev = pci_get_drvdata(pdev);
15808 struct tg3 *tp = netdev_priv(netdev);
15809 pci_ers_result_t err = PCI_ERS_RESULT_NEED_RESET;
15810
15811 netdev_info(netdev, "PCI I/O error detected\n");
15812
15813 rtnl_lock();
15814
15815 if (!netif_running(netdev))
15816 goto done;
15817
15818 tg3_phy_stop(tp);
15819
15820 tg3_netif_stop(tp);
15821
15822 del_timer_sync(&tp->timer);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000015823
15824 /* Want to make sure that the reset task doesn't run */
Matt Carlsondb219972011-11-04 09:15:03 +000015825 tg3_reset_task_cancel(tp);
Joe Perches63c3a662011-04-26 08:12:10 +000015826 tg3_flag_clear(tp, TX_RECOVERY_PENDING);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000015827
15828 netif_device_detach(netdev);
15829
15830 /* Clean up software state, even if MMIO is blocked */
15831 tg3_full_lock(tp, 0);
15832 tg3_halt(tp, RESET_KIND_SHUTDOWN, 0);
15833 tg3_full_unlock(tp);
15834
15835done:
15836 if (state == pci_channel_io_perm_failure)
15837 err = PCI_ERS_RESULT_DISCONNECT;
15838 else
15839 pci_disable_device(pdev);
15840
15841 rtnl_unlock();
15842
15843 return err;
15844}
15845
15846/**
15847 * tg3_io_slot_reset - called after the pci bus has been reset.
15848 * @pdev: Pointer to PCI device
15849 *
15850 * Restart the card from scratch, as if from a cold-boot.
15851 * At this point, the card has exprienced a hard reset,
15852 * followed by fixups by BIOS, and has its config space
15853 * set up identically to what it was at cold boot.
15854 */
15855static pci_ers_result_t tg3_io_slot_reset(struct pci_dev *pdev)
15856{
15857 struct net_device *netdev = pci_get_drvdata(pdev);
15858 struct tg3 *tp = netdev_priv(netdev);
15859 pci_ers_result_t rc = PCI_ERS_RESULT_DISCONNECT;
15860 int err;
15861
15862 rtnl_lock();
15863
15864 if (pci_enable_device(pdev)) {
15865 netdev_err(netdev, "Cannot re-enable PCI device after reset.\n");
15866 goto done;
15867 }
15868
15869 pci_set_master(pdev);
15870 pci_restore_state(pdev);
15871 pci_save_state(pdev);
15872
15873 if (!netif_running(netdev)) {
15874 rc = PCI_ERS_RESULT_RECOVERED;
15875 goto done;
15876 }
15877
15878 err = tg3_power_up(tp);
Matt Carlsonbed98292011-07-13 09:27:29 +000015879 if (err)
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000015880 goto done;
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000015881
15882 rc = PCI_ERS_RESULT_RECOVERED;
15883
15884done:
15885 rtnl_unlock();
15886
15887 return rc;
15888}
15889
15890/**
15891 * tg3_io_resume - called when traffic can start flowing again.
15892 * @pdev: Pointer to PCI device
15893 *
15894 * This callback is called when the error recovery driver tells
15895 * us that its OK to resume normal operation.
15896 */
15897static void tg3_io_resume(struct pci_dev *pdev)
15898{
15899 struct net_device *netdev = pci_get_drvdata(pdev);
15900 struct tg3 *tp = netdev_priv(netdev);
15901 int err;
15902
15903 rtnl_lock();
15904
15905 if (!netif_running(netdev))
15906 goto done;
15907
15908 tg3_full_lock(tp, 0);
Joe Perches63c3a662011-04-26 08:12:10 +000015909 tg3_flag_set(tp, INIT_COMPLETE);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000015910 err = tg3_restart_hw(tp, 1);
15911 tg3_full_unlock(tp);
15912 if (err) {
15913 netdev_err(netdev, "Cannot restart hardware after reset.\n");
15914 goto done;
15915 }
15916
15917 netif_device_attach(netdev);
15918
15919 tp->timer.expires = jiffies + tp->timer_offset;
15920 add_timer(&tp->timer);
15921
15922 tg3_netif_start(tp);
15923
15924 tg3_phy_start(tp);
15925
15926done:
15927 rtnl_unlock();
15928}
15929
15930static struct pci_error_handlers tg3_err_handler = {
15931 .error_detected = tg3_io_error_detected,
15932 .slot_reset = tg3_io_slot_reset,
15933 .resume = tg3_io_resume
15934};
15935
Linus Torvalds1da177e2005-04-16 15:20:36 -070015936static struct pci_driver tg3_driver = {
15937 .name = DRV_MODULE_NAME,
15938 .id_table = tg3_pci_tbl,
15939 .probe = tg3_init_one,
15940 .remove = __devexit_p(tg3_remove_one),
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000015941 .err_handler = &tg3_err_handler,
Eric Dumazetaa6027c2011-01-01 05:22:46 +000015942 .driver.pm = TG3_PM_OPS,
Linus Torvalds1da177e2005-04-16 15:20:36 -070015943};
15944
15945static int __init tg3_init(void)
15946{
Jeff Garzik29917622006-08-19 17:48:59 -040015947 return pci_register_driver(&tg3_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015948}
15949
15950static void __exit tg3_cleanup(void)
15951{
15952 pci_unregister_driver(&tg3_driver);
15953}
15954
15955module_init(tg3_init);
15956module_exit(tg3_cleanup);