blob: d1681db7ea54b189f03547d0ad37e83e91308de1 [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 Carlson66711e62009-11-13 13:03:49 +0000473 if (off == TG3_RX_STD_PROD_IDX_REG) {
Michael Chan68929142005-08-09 20:17:14 -0700474 pci_write_config_dword(tp->pdev, TG3PCI_STD_RING_PROD_IDX +
475 TG3_64BIT_REG_LOW, val);
476 return;
477 }
478
479 spin_lock_irqsave(&tp->indirect_lock, flags);
480 pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off + 0x5600);
481 pci_write_config_dword(tp->pdev, TG3PCI_REG_DATA, val);
482 spin_unlock_irqrestore(&tp->indirect_lock, flags);
483
484 /* In indirect mode when disabling interrupts, we also need
485 * to clear the interrupt bit in the GRC local ctrl register.
486 */
487 if ((off == (MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW)) &&
488 (val == 0x1)) {
489 pci_write_config_dword(tp->pdev, TG3PCI_MISC_LOCAL_CTRL,
490 tp->grc_local_ctrl|GRC_LCLCTRL_CLEARINT);
491 }
492}
493
494static u32 tg3_read_indirect_mbox(struct tg3 *tp, u32 off)
495{
496 unsigned long flags;
497 u32 val;
498
499 spin_lock_irqsave(&tp->indirect_lock, flags);
500 pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off + 0x5600);
501 pci_read_config_dword(tp->pdev, TG3PCI_REG_DATA, &val);
502 spin_unlock_irqrestore(&tp->indirect_lock, flags);
503 return val;
504}
505
Michael Chanb401e9e2005-12-19 16:27:04 -0800506/* usec_wait specifies the wait time in usec when writing to certain registers
507 * where it is unsafe to read back the register without some delay.
508 * GRC_LOCAL_CTRL is one example if the GPIOs are toggled to switch power.
509 * TG3PCI_CLOCK_CTRL is another example if the clock frequencies are changed.
510 */
511static void _tw32_flush(struct tg3 *tp, u32 off, u32 val, u32 usec_wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512{
Joe Perches63c3a662011-04-26 08:12:10 +0000513 if (tg3_flag(tp, PCIX_TARGET_HWBUG) || tg3_flag(tp, ICH_WORKAROUND))
Michael Chanb401e9e2005-12-19 16:27:04 -0800514 /* Non-posted methods */
515 tp->write32(tp, off, val);
516 else {
517 /* Posted method */
518 tg3_write32(tp, off, val);
519 if (usec_wait)
520 udelay(usec_wait);
521 tp->read32(tp, off);
522 }
523 /* Wait again after the read for the posted method to guarantee that
524 * the wait time is met.
525 */
526 if (usec_wait)
527 udelay(usec_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528}
529
Michael Chan09ee9292005-08-09 20:17:00 -0700530static inline void tw32_mailbox_flush(struct tg3 *tp, u32 off, u32 val)
531{
532 tp->write32_mbox(tp, off, val);
Joe Perches63c3a662011-04-26 08:12:10 +0000533 if (!tg3_flag(tp, MBOX_WRITE_REORDER) && !tg3_flag(tp, ICH_WORKAROUND))
Michael Chan68929142005-08-09 20:17:14 -0700534 tp->read32_mbox(tp, off);
Michael Chan09ee9292005-08-09 20:17:00 -0700535}
536
Michael Chan20094932005-08-09 20:16:32 -0700537static void tg3_write32_tx_mbox(struct tg3 *tp, u32 off, u32 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538{
539 void __iomem *mbox = tp->regs + off;
540 writel(val, mbox);
Joe Perches63c3a662011-04-26 08:12:10 +0000541 if (tg3_flag(tp, TXD_MBOX_HWBUG))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 writel(val, mbox);
Joe Perches63c3a662011-04-26 08:12:10 +0000543 if (tg3_flag(tp, MBOX_WRITE_REORDER))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 readl(mbox);
545}
546
Michael Chanb5d37722006-09-27 16:06:21 -0700547static u32 tg3_read32_mbox_5906(struct tg3 *tp, u32 off)
548{
Matt Carlsonde6f31e2010-04-12 06:58:30 +0000549 return readl(tp->regs + off + GRCMBOX_BASE);
Michael Chanb5d37722006-09-27 16:06:21 -0700550}
551
552static void tg3_write32_mbox_5906(struct tg3 *tp, u32 off, u32 val)
553{
554 writel(val, tp->regs + off + GRCMBOX_BASE);
555}
556
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000557#define tw32_mailbox(reg, val) tp->write32_mbox(tp, reg, val)
Michael Chan09ee9292005-08-09 20:17:00 -0700558#define tw32_mailbox_f(reg, val) tw32_mailbox_flush(tp, (reg), (val))
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000559#define tw32_rx_mbox(reg, val) tp->write32_rx_mbox(tp, reg, val)
560#define tw32_tx_mbox(reg, val) tp->write32_tx_mbox(tp, reg, val)
561#define tr32_mailbox(reg) tp->read32_mbox(tp, reg)
Michael Chan20094932005-08-09 20:16:32 -0700562
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000563#define tw32(reg, val) tp->write32(tp, reg, val)
564#define tw32_f(reg, val) _tw32_flush(tp, (reg), (val), 0)
565#define tw32_wait_f(reg, val, us) _tw32_flush(tp, (reg), (val), (us))
566#define tr32(reg) tp->read32(tp, reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567
568static void tg3_write_mem(struct tg3 *tp, u32 off, u32 val)
569{
Michael Chan68929142005-08-09 20:17:14 -0700570 unsigned long flags;
571
Matt Carlson6ff6f812011-05-19 12:12:54 +0000572 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906 &&
Michael Chanb5d37722006-09-27 16:06:21 -0700573 (off >= NIC_SRAM_STATS_BLK) && (off < NIC_SRAM_TX_BUFFER_DESC))
574 return;
575
Michael Chan68929142005-08-09 20:17:14 -0700576 spin_lock_irqsave(&tp->indirect_lock, flags);
Joe Perches63c3a662011-04-26 08:12:10 +0000577 if (tg3_flag(tp, SRAM_USE_CONFIG)) {
Michael Chanbbadf502006-04-06 21:46:34 -0700578 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, off);
579 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_DATA, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580
Michael Chanbbadf502006-04-06 21:46:34 -0700581 /* Always leave this as zero. */
582 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, 0);
583 } else {
584 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, off);
585 tw32_f(TG3PCI_MEM_WIN_DATA, val);
586
587 /* Always leave this as zero. */
588 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, 0);
589 }
Michael Chan68929142005-08-09 20:17:14 -0700590 spin_unlock_irqrestore(&tp->indirect_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591}
592
593static void tg3_read_mem(struct tg3 *tp, u32 off, u32 *val)
594{
Michael Chan68929142005-08-09 20:17:14 -0700595 unsigned long flags;
596
Matt Carlson6ff6f812011-05-19 12:12:54 +0000597 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906 &&
Michael Chanb5d37722006-09-27 16:06:21 -0700598 (off >= NIC_SRAM_STATS_BLK) && (off < NIC_SRAM_TX_BUFFER_DESC)) {
599 *val = 0;
600 return;
601 }
602
Michael Chan68929142005-08-09 20:17:14 -0700603 spin_lock_irqsave(&tp->indirect_lock, flags);
Joe Perches63c3a662011-04-26 08:12:10 +0000604 if (tg3_flag(tp, SRAM_USE_CONFIG)) {
Michael Chanbbadf502006-04-06 21:46:34 -0700605 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, off);
606 pci_read_config_dword(tp->pdev, TG3PCI_MEM_WIN_DATA, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607
Michael Chanbbadf502006-04-06 21:46:34 -0700608 /* Always leave this as zero. */
609 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, 0);
610 } else {
611 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, off);
612 *val = tr32(TG3PCI_MEM_WIN_DATA);
613
614 /* Always leave this as zero. */
615 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, 0);
616 }
Michael Chan68929142005-08-09 20:17:14 -0700617 spin_unlock_irqrestore(&tp->indirect_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618}
619
Matt Carlson0d3031d2007-10-10 18:02:43 -0700620static void tg3_ape_lock_init(struct tg3 *tp)
621{
622 int i;
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000623 u32 regbase, bit;
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000624
625 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
626 regbase = TG3_APE_LOCK_GRANT;
627 else
628 regbase = TG3_APE_PER_LOCK_GRANT;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700629
630 /* Make sure the driver hasn't any stale locks. */
Matt Carlson78f94dc2011-11-04 09:14:58 +0000631 for (i = TG3_APE_LOCK_PHY0; i <= TG3_APE_LOCK_GPIO; i++) {
632 switch (i) {
633 case TG3_APE_LOCK_PHY0:
634 case TG3_APE_LOCK_PHY1:
635 case TG3_APE_LOCK_PHY2:
636 case TG3_APE_LOCK_PHY3:
637 bit = APE_LOCK_GRANT_DRIVER;
638 break;
639 default:
640 if (!tp->pci_fn)
641 bit = APE_LOCK_GRANT_DRIVER;
642 else
643 bit = 1 << tp->pci_fn;
644 }
645 tg3_ape_write32(tp, regbase + 4 * i, bit);
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000646 }
647
Matt Carlson0d3031d2007-10-10 18:02:43 -0700648}
649
650static int tg3_ape_lock(struct tg3 *tp, int locknum)
651{
652 int i, off;
653 int ret = 0;
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000654 u32 status, req, gnt, bit;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700655
Joe Perches63c3a662011-04-26 08:12:10 +0000656 if (!tg3_flag(tp, ENABLE_APE))
Matt Carlson0d3031d2007-10-10 18:02:43 -0700657 return 0;
658
659 switch (locknum) {
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000660 case TG3_APE_LOCK_GPIO:
661 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
662 return 0;
Matt Carlson33f401a2010-04-05 10:19:27 +0000663 case TG3_APE_LOCK_GRC:
664 case TG3_APE_LOCK_MEM:
Matt Carlson78f94dc2011-11-04 09:14:58 +0000665 if (!tp->pci_fn)
666 bit = APE_LOCK_REQ_DRIVER;
667 else
668 bit = 1 << tp->pci_fn;
Matt Carlson33f401a2010-04-05 10:19:27 +0000669 break;
670 default:
671 return -EINVAL;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700672 }
673
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000674 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761) {
675 req = TG3_APE_LOCK_REQ;
676 gnt = TG3_APE_LOCK_GRANT;
677 } else {
678 req = TG3_APE_PER_LOCK_REQ;
679 gnt = TG3_APE_PER_LOCK_GRANT;
680 }
681
Matt Carlson0d3031d2007-10-10 18:02:43 -0700682 off = 4 * locknum;
683
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000684 tg3_ape_write32(tp, req + off, bit);
Matt Carlson0d3031d2007-10-10 18:02:43 -0700685
686 /* Wait for up to 1 millisecond to acquire lock. */
687 for (i = 0; i < 100; i++) {
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000688 status = tg3_ape_read32(tp, gnt + off);
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000689 if (status == bit)
Matt Carlson0d3031d2007-10-10 18:02:43 -0700690 break;
691 udelay(10);
692 }
693
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000694 if (status != bit) {
Matt Carlson0d3031d2007-10-10 18:02:43 -0700695 /* Revoke the lock request. */
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000696 tg3_ape_write32(tp, gnt + off, bit);
Matt Carlson0d3031d2007-10-10 18:02:43 -0700697 ret = -EBUSY;
698 }
699
700 return ret;
701}
702
703static void tg3_ape_unlock(struct tg3 *tp, int locknum)
704{
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000705 u32 gnt, bit;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700706
Joe Perches63c3a662011-04-26 08:12:10 +0000707 if (!tg3_flag(tp, ENABLE_APE))
Matt Carlson0d3031d2007-10-10 18:02:43 -0700708 return;
709
710 switch (locknum) {
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000711 case TG3_APE_LOCK_GPIO:
712 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
713 return;
Matt Carlson33f401a2010-04-05 10:19:27 +0000714 case TG3_APE_LOCK_GRC:
715 case TG3_APE_LOCK_MEM:
Matt Carlson78f94dc2011-11-04 09:14:58 +0000716 if (!tp->pci_fn)
717 bit = APE_LOCK_GRANT_DRIVER;
718 else
719 bit = 1 << tp->pci_fn;
Matt Carlson33f401a2010-04-05 10:19:27 +0000720 break;
721 default:
722 return;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700723 }
724
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000725 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
726 gnt = TG3_APE_LOCK_GRANT;
727 else
728 gnt = TG3_APE_PER_LOCK_GRANT;
729
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000730 tg3_ape_write32(tp, gnt + 4 * locknum, bit);
Matt Carlson0d3031d2007-10-10 18:02:43 -0700731}
732
Matt Carlsonfd6d3f02011-08-31 11:44:52 +0000733static void tg3_ape_send_event(struct tg3 *tp, u32 event)
734{
735 int i;
736 u32 apedata;
737
738 /* NCSI does not support APE events */
739 if (tg3_flag(tp, APE_HAS_NCSI))
740 return;
741
742 apedata = tg3_ape_read32(tp, TG3_APE_SEG_SIG);
743 if (apedata != APE_SEG_SIG_MAGIC)
744 return;
745
746 apedata = tg3_ape_read32(tp, TG3_APE_FW_STATUS);
747 if (!(apedata & APE_FW_STATUS_READY))
748 return;
749
750 /* Wait for up to 1 millisecond for APE to service previous event. */
751 for (i = 0; i < 10; i++) {
752 if (tg3_ape_lock(tp, TG3_APE_LOCK_MEM))
753 return;
754
755 apedata = tg3_ape_read32(tp, TG3_APE_EVENT_STATUS);
756
757 if (!(apedata & APE_EVENT_STATUS_EVENT_PENDING))
758 tg3_ape_write32(tp, TG3_APE_EVENT_STATUS,
759 event | APE_EVENT_STATUS_EVENT_PENDING);
760
761 tg3_ape_unlock(tp, TG3_APE_LOCK_MEM);
762
763 if (!(apedata & APE_EVENT_STATUS_EVENT_PENDING))
764 break;
765
766 udelay(100);
767 }
768
769 if (!(apedata & APE_EVENT_STATUS_EVENT_PENDING))
770 tg3_ape_write32(tp, TG3_APE_EVENT, APE_EVENT_1);
771}
772
773static void tg3_ape_driver_state_change(struct tg3 *tp, int kind)
774{
775 u32 event;
776 u32 apedata;
777
778 if (!tg3_flag(tp, ENABLE_APE))
779 return;
780
781 switch (kind) {
782 case RESET_KIND_INIT:
783 tg3_ape_write32(tp, TG3_APE_HOST_SEG_SIG,
784 APE_HOST_SEG_SIG_MAGIC);
785 tg3_ape_write32(tp, TG3_APE_HOST_SEG_LEN,
786 APE_HOST_SEG_LEN_MAGIC);
787 apedata = tg3_ape_read32(tp, TG3_APE_HOST_INIT_COUNT);
788 tg3_ape_write32(tp, TG3_APE_HOST_INIT_COUNT, ++apedata);
789 tg3_ape_write32(tp, TG3_APE_HOST_DRIVER_ID,
790 APE_HOST_DRIVER_ID_MAGIC(TG3_MAJ_NUM, TG3_MIN_NUM));
791 tg3_ape_write32(tp, TG3_APE_HOST_BEHAVIOR,
792 APE_HOST_BEHAV_NO_PHYLOCK);
793 tg3_ape_write32(tp, TG3_APE_HOST_DRVR_STATE,
794 TG3_APE_HOST_DRVR_STATE_START);
795
796 event = APE_EVENT_STATUS_STATE_START;
797 break;
798 case RESET_KIND_SHUTDOWN:
799 /* With the interface we are currently using,
800 * APE does not track driver state. Wiping
801 * out the HOST SEGMENT SIGNATURE forces
802 * the APE to assume OS absent status.
803 */
804 tg3_ape_write32(tp, TG3_APE_HOST_SEG_SIG, 0x0);
805
806 if (device_may_wakeup(&tp->pdev->dev) &&
807 tg3_flag(tp, WOL_ENABLE)) {
808 tg3_ape_write32(tp, TG3_APE_HOST_WOL_SPEED,
809 TG3_APE_HOST_WOL_SPEED_AUTO);
810 apedata = TG3_APE_HOST_DRVR_STATE_WOL;
811 } else
812 apedata = TG3_APE_HOST_DRVR_STATE_UNLOAD;
813
814 tg3_ape_write32(tp, TG3_APE_HOST_DRVR_STATE, apedata);
815
816 event = APE_EVENT_STATUS_STATE_UNLOAD;
817 break;
818 case RESET_KIND_SUSPEND:
819 event = APE_EVENT_STATUS_STATE_SUSPEND;
820 break;
821 default:
822 return;
823 }
824
825 event |= APE_EVENT_STATUS_DRIVER_EVNT | APE_EVENT_STATUS_STATE_CHNGE;
826
827 tg3_ape_send_event(tp, event);
828}
829
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830static void tg3_disable_ints(struct tg3 *tp)
831{
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000832 int i;
833
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 tw32(TG3PCI_MISC_HOST_CTRL,
835 (tp->misc_host_ctrl | MISC_HOST_CTRL_MASK_PCI_INT));
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000836 for (i = 0; i < tp->irq_max; i++)
837 tw32_mailbox_f(tp->napi[i].int_mbox, 0x00000001);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838}
839
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840static void tg3_enable_ints(struct tg3 *tp)
841{
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000842 int i;
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000843
Michael Chanbbe832c2005-06-24 20:20:04 -0700844 tp->irq_sync = 0;
845 wmb();
846
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 tw32(TG3PCI_MISC_HOST_CTRL,
848 (tp->misc_host_ctrl & ~MISC_HOST_CTRL_MASK_PCI_INT));
Matt Carlsonf19af9c2009-09-01 12:47:49 +0000849
Matt Carlsonf89f38b2010-02-12 14:47:07 +0000850 tp->coal_now = tp->coalesce_mode | HOSTCC_MODE_ENABLE;
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000851 for (i = 0; i < tp->irq_cnt; i++) {
852 struct tg3_napi *tnapi = &tp->napi[i];
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000853
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000854 tw32_mailbox_f(tnapi->int_mbox, tnapi->last_tag << 24);
Joe Perches63c3a662011-04-26 08:12:10 +0000855 if (tg3_flag(tp, 1SHOT_MSI))
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000856 tw32_mailbox_f(tnapi->int_mbox, tnapi->last_tag << 24);
857
Matt Carlsonf89f38b2010-02-12 14:47:07 +0000858 tp->coal_now |= tnapi->coal_now;
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000859 }
Matt Carlsonf19af9c2009-09-01 12:47:49 +0000860
861 /* Force an initial interrupt */
Joe Perches63c3a662011-04-26 08:12:10 +0000862 if (!tg3_flag(tp, TAGGED_STATUS) &&
Matt Carlsonf19af9c2009-09-01 12:47:49 +0000863 (tp->napi[0].hw_status->status & SD_STATUS_UPDATED))
864 tw32(GRC_LOCAL_CTRL, tp->grc_local_ctrl | GRC_LCLCTRL_SETINT);
865 else
Matt Carlsonf89f38b2010-02-12 14:47:07 +0000866 tw32(HOSTCC_MODE, tp->coal_now);
867
868 tp->coal_now &= ~(tp->napi[0].coal_now | tp->napi[1].coal_now);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869}
870
Matt Carlson17375d22009-08-28 14:02:18 +0000871static inline unsigned int tg3_has_work(struct tg3_napi *tnapi)
Michael Chan04237dd2005-04-25 15:17:17 -0700872{
Matt Carlson17375d22009-08-28 14:02:18 +0000873 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +0000874 struct tg3_hw_status *sblk = tnapi->hw_status;
Michael Chan04237dd2005-04-25 15:17:17 -0700875 unsigned int work_exists = 0;
876
877 /* check for phy events */
Joe Perches63c3a662011-04-26 08:12:10 +0000878 if (!(tg3_flag(tp, USE_LINKCHG_REG) || tg3_flag(tp, POLL_SERDES))) {
Michael Chan04237dd2005-04-25 15:17:17 -0700879 if (sblk->status & SD_STATUS_LINK_CHG)
880 work_exists = 1;
881 }
882 /* check for RX/TX work to do */
Matt Carlsonf3f3f272009-08-28 14:03:21 +0000883 if (sblk->idx[0].tx_consumer != tnapi->tx_cons ||
Matt Carlson8d9d7cf2009-09-01 13:19:05 +0000884 *(tnapi->rx_rcb_prod_idx) != tnapi->rx_rcb_ptr)
Michael Chan04237dd2005-04-25 15:17:17 -0700885 work_exists = 1;
886
887 return work_exists;
888}
889
Matt Carlson17375d22009-08-28 14:02:18 +0000890/* tg3_int_reenable
Michael Chan04237dd2005-04-25 15:17:17 -0700891 * similar to tg3_enable_ints, but it accurately determines whether there
892 * is new work pending and can return without flushing the PIO write
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400893 * which reenables interrupts
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 */
Matt Carlson17375d22009-08-28 14:02:18 +0000895static void tg3_int_reenable(struct tg3_napi *tnapi)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896{
Matt Carlson17375d22009-08-28 14:02:18 +0000897 struct tg3 *tp = tnapi->tp;
898
Matt Carlson898a56f2009-08-28 14:02:40 +0000899 tw32_mailbox(tnapi->int_mbox, tnapi->last_tag << 24);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 mmiowb();
901
David S. Millerfac9b832005-05-18 22:46:34 -0700902 /* When doing tagged status, this work check is unnecessary.
903 * The last_tag we write above tells the chip which piece of
904 * work we've completed.
905 */
Joe Perches63c3a662011-04-26 08:12:10 +0000906 if (!tg3_flag(tp, TAGGED_STATUS) && tg3_has_work(tnapi))
Michael Chan04237dd2005-04-25 15:17:17 -0700907 tw32(HOSTCC_MODE, tp->coalesce_mode |
Matt Carlsonfd2ce372009-09-01 12:51:13 +0000908 HOSTCC_MODE_ENABLE | tnapi->coal_now);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909}
910
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911static void tg3_switch_clocks(struct tg3 *tp)
912{
Matt Carlsonf6eb9b12009-09-01 13:19:53 +0000913 u32 clock_ctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 u32 orig_clock_ctrl;
915
Joe Perches63c3a662011-04-26 08:12:10 +0000916 if (tg3_flag(tp, CPMU_PRESENT) || tg3_flag(tp, 5780_CLASS))
Michael Chan4cf78e42005-07-25 12:29:19 -0700917 return;
918
Matt Carlsonf6eb9b12009-09-01 13:19:53 +0000919 clock_ctrl = tr32(TG3PCI_CLOCK_CTRL);
920
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 orig_clock_ctrl = clock_ctrl;
922 clock_ctrl &= (CLOCK_CTRL_FORCE_CLKRUN |
923 CLOCK_CTRL_CLKRUN_OENABLE |
924 0x1f);
925 tp->pci_clock_ctrl = clock_ctrl;
926
Joe Perches63c3a662011-04-26 08:12:10 +0000927 if (tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 if (orig_clock_ctrl & CLOCK_CTRL_625_CORE) {
Michael Chanb401e9e2005-12-19 16:27:04 -0800929 tw32_wait_f(TG3PCI_CLOCK_CTRL,
930 clock_ctrl | CLOCK_CTRL_625_CORE, 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 }
932 } else if ((orig_clock_ctrl & CLOCK_CTRL_44MHZ_CORE) != 0) {
Michael Chanb401e9e2005-12-19 16:27:04 -0800933 tw32_wait_f(TG3PCI_CLOCK_CTRL,
934 clock_ctrl |
935 (CLOCK_CTRL_44MHZ_CORE | CLOCK_CTRL_ALTCLK),
936 40);
937 tw32_wait_f(TG3PCI_CLOCK_CTRL,
938 clock_ctrl | (CLOCK_CTRL_ALTCLK),
939 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 }
Michael Chanb401e9e2005-12-19 16:27:04 -0800941 tw32_wait_f(TG3PCI_CLOCK_CTRL, clock_ctrl, 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942}
943
944#define PHY_BUSY_LOOPS 5000
945
946static int tg3_readphy(struct tg3 *tp, int reg, u32 *val)
947{
948 u32 frame_val;
949 unsigned int loops;
950 int ret;
951
952 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
953 tw32_f(MAC_MI_MODE,
954 (tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL));
955 udelay(80);
956 }
957
958 *val = 0x0;
959
Matt Carlson882e9792009-09-01 13:21:36 +0000960 frame_val = ((tp->phy_addr << MI_COM_PHY_ADDR_SHIFT) &
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 MI_COM_PHY_ADDR_MASK);
962 frame_val |= ((reg << MI_COM_REG_ADDR_SHIFT) &
963 MI_COM_REG_ADDR_MASK);
964 frame_val |= (MI_COM_CMD_READ | MI_COM_START);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400965
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 tw32_f(MAC_MI_COM, frame_val);
967
968 loops = PHY_BUSY_LOOPS;
969 while (loops != 0) {
970 udelay(10);
971 frame_val = tr32(MAC_MI_COM);
972
973 if ((frame_val & MI_COM_BUSY) == 0) {
974 udelay(5);
975 frame_val = tr32(MAC_MI_COM);
976 break;
977 }
978 loops -= 1;
979 }
980
981 ret = -EBUSY;
982 if (loops != 0) {
983 *val = frame_val & MI_COM_DATA_MASK;
984 ret = 0;
985 }
986
987 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
988 tw32_f(MAC_MI_MODE, tp->mi_mode);
989 udelay(80);
990 }
991
992 return ret;
993}
994
995static int tg3_writephy(struct tg3 *tp, int reg, u32 val)
996{
997 u32 frame_val;
998 unsigned int loops;
999 int ret;
1000
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001001 if ((tp->phy_flags & TG3_PHYFLG_IS_FET) &&
Matt Carlson221c5632011-06-13 13:39:01 +00001002 (reg == MII_CTRL1000 || reg == MII_TG3_AUX_CTRL))
Michael Chanb5d37722006-09-27 16:06:21 -07001003 return 0;
1004
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
1006 tw32_f(MAC_MI_MODE,
1007 (tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL));
1008 udelay(80);
1009 }
1010
Matt Carlson882e9792009-09-01 13:21:36 +00001011 frame_val = ((tp->phy_addr << MI_COM_PHY_ADDR_SHIFT) &
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 MI_COM_PHY_ADDR_MASK);
1013 frame_val |= ((reg << MI_COM_REG_ADDR_SHIFT) &
1014 MI_COM_REG_ADDR_MASK);
1015 frame_val |= (val & MI_COM_DATA_MASK);
1016 frame_val |= (MI_COM_CMD_WRITE | MI_COM_START);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001017
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 tw32_f(MAC_MI_COM, frame_val);
1019
1020 loops = PHY_BUSY_LOOPS;
1021 while (loops != 0) {
1022 udelay(10);
1023 frame_val = tr32(MAC_MI_COM);
1024 if ((frame_val & MI_COM_BUSY) == 0) {
1025 udelay(5);
1026 frame_val = tr32(MAC_MI_COM);
1027 break;
1028 }
1029 loops -= 1;
1030 }
1031
1032 ret = -EBUSY;
1033 if (loops != 0)
1034 ret = 0;
1035
1036 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
1037 tw32_f(MAC_MI_MODE, tp->mi_mode);
1038 udelay(80);
1039 }
1040
1041 return ret;
1042}
1043
Matt Carlsonb0988c12011-04-20 07:57:39 +00001044static int tg3_phy_cl45_write(struct tg3 *tp, u32 devad, u32 addr, u32 val)
1045{
1046 int err;
1047
1048 err = tg3_writephy(tp, MII_TG3_MMD_CTRL, devad);
1049 if (err)
1050 goto done;
1051
1052 err = tg3_writephy(tp, MII_TG3_MMD_ADDRESS, addr);
1053 if (err)
1054 goto done;
1055
1056 err = tg3_writephy(tp, MII_TG3_MMD_CTRL,
1057 MII_TG3_MMD_CTRL_DATA_NOINC | devad);
1058 if (err)
1059 goto done;
1060
1061 err = tg3_writephy(tp, MII_TG3_MMD_ADDRESS, val);
1062
1063done:
1064 return err;
1065}
1066
1067static int tg3_phy_cl45_read(struct tg3 *tp, u32 devad, u32 addr, u32 *val)
1068{
1069 int err;
1070
1071 err = tg3_writephy(tp, MII_TG3_MMD_CTRL, devad);
1072 if (err)
1073 goto done;
1074
1075 err = tg3_writephy(tp, MII_TG3_MMD_ADDRESS, addr);
1076 if (err)
1077 goto done;
1078
1079 err = tg3_writephy(tp, MII_TG3_MMD_CTRL,
1080 MII_TG3_MMD_CTRL_DATA_NOINC | devad);
1081 if (err)
1082 goto done;
1083
1084 err = tg3_readphy(tp, MII_TG3_MMD_ADDRESS, val);
1085
1086done:
1087 return err;
1088}
1089
1090static int tg3_phydsp_read(struct tg3 *tp, u32 reg, u32 *val)
1091{
1092 int err;
1093
1094 err = tg3_writephy(tp, MII_TG3_DSP_ADDRESS, reg);
1095 if (!err)
1096 err = tg3_readphy(tp, MII_TG3_DSP_RW_PORT, val);
1097
1098 return err;
1099}
1100
1101static int tg3_phydsp_write(struct tg3 *tp, u32 reg, u32 val)
1102{
1103 int err;
1104
1105 err = tg3_writephy(tp, MII_TG3_DSP_ADDRESS, reg);
1106 if (!err)
1107 err = tg3_writephy(tp, MII_TG3_DSP_RW_PORT, val);
1108
1109 return err;
1110}
1111
Matt Carlson15ee95c2011-04-20 07:57:40 +00001112static int tg3_phy_auxctl_read(struct tg3 *tp, int reg, u32 *val)
1113{
1114 int err;
1115
1116 err = tg3_writephy(tp, MII_TG3_AUX_CTRL,
1117 (reg << MII_TG3_AUXCTL_MISC_RDSEL_SHIFT) |
1118 MII_TG3_AUXCTL_SHDWSEL_MISC);
1119 if (!err)
1120 err = tg3_readphy(tp, MII_TG3_AUX_CTRL, val);
1121
1122 return err;
1123}
1124
Matt Carlsonb4bd2922011-04-20 07:57:41 +00001125static int tg3_phy_auxctl_write(struct tg3 *tp, int reg, u32 set)
1126{
1127 if (reg == MII_TG3_AUXCTL_SHDWSEL_MISC)
1128 set |= MII_TG3_AUXCTL_MISC_WREN;
1129
1130 return tg3_writephy(tp, MII_TG3_AUX_CTRL, set | reg);
1131}
1132
Matt Carlson1d36ba42011-04-20 07:57:42 +00001133#define TG3_PHY_AUXCTL_SMDSP_ENABLE(tp) \
1134 tg3_phy_auxctl_write((tp), MII_TG3_AUXCTL_SHDWSEL_AUXCTL, \
1135 MII_TG3_AUXCTL_ACTL_SMDSP_ENA | \
1136 MII_TG3_AUXCTL_ACTL_TX_6DB)
1137
1138#define TG3_PHY_AUXCTL_SMDSP_DISABLE(tp) \
1139 tg3_phy_auxctl_write((tp), MII_TG3_AUXCTL_SHDWSEL_AUXCTL, \
1140 MII_TG3_AUXCTL_ACTL_TX_6DB);
1141
Matt Carlson95e28692008-05-25 23:44:14 -07001142static int tg3_bmcr_reset(struct tg3 *tp)
1143{
1144 u32 phy_control;
1145 int limit, err;
1146
1147 /* OK, reset it, and poll the BMCR_RESET bit until it
1148 * clears or we time out.
1149 */
1150 phy_control = BMCR_RESET;
1151 err = tg3_writephy(tp, MII_BMCR, phy_control);
1152 if (err != 0)
1153 return -EBUSY;
1154
1155 limit = 5000;
1156 while (limit--) {
1157 err = tg3_readphy(tp, MII_BMCR, &phy_control);
1158 if (err != 0)
1159 return -EBUSY;
1160
1161 if ((phy_control & BMCR_RESET) == 0) {
1162 udelay(40);
1163 break;
1164 }
1165 udelay(10);
1166 }
Roel Kluind4675b52009-02-12 16:33:27 -08001167 if (limit < 0)
Matt Carlson95e28692008-05-25 23:44:14 -07001168 return -EBUSY;
1169
1170 return 0;
1171}
1172
Matt Carlson158d7ab2008-05-29 01:37:54 -07001173static int tg3_mdio_read(struct mii_bus *bp, int mii_id, int reg)
1174{
Francois Romieu3d165432009-01-19 16:56:50 -08001175 struct tg3 *tp = bp->priv;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001176 u32 val;
1177
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001178 spin_lock_bh(&tp->lock);
Matt Carlson158d7ab2008-05-29 01:37:54 -07001179
1180 if (tg3_readphy(tp, reg, &val))
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001181 val = -EIO;
1182
1183 spin_unlock_bh(&tp->lock);
Matt Carlson158d7ab2008-05-29 01:37:54 -07001184
1185 return val;
1186}
1187
1188static int tg3_mdio_write(struct mii_bus *bp, int mii_id, int reg, u16 val)
1189{
Francois Romieu3d165432009-01-19 16:56:50 -08001190 struct tg3 *tp = bp->priv;
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001191 u32 ret = 0;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001192
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001193 spin_lock_bh(&tp->lock);
Matt Carlson158d7ab2008-05-29 01:37:54 -07001194
1195 if (tg3_writephy(tp, reg, val))
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001196 ret = -EIO;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001197
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001198 spin_unlock_bh(&tp->lock);
1199
1200 return ret;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001201}
1202
1203static int tg3_mdio_reset(struct mii_bus *bp)
1204{
1205 return 0;
1206}
1207
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001208static void tg3_mdio_config_5785(struct tg3 *tp)
Matt Carlsona9daf362008-05-25 23:49:44 -07001209{
1210 u32 val;
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001211 struct phy_device *phydev;
Matt Carlsona9daf362008-05-25 23:49:44 -07001212
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001213 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001214 switch (phydev->drv->phy_id & phydev->drv->phy_id_mask) {
Matt Carlson6a443a02010-02-17 15:17:04 +00001215 case PHY_ID_BCM50610:
1216 case PHY_ID_BCM50610M:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001217 val = MAC_PHYCFG2_50610_LED_MODES;
1218 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001219 case PHY_ID_BCMAC131:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001220 val = MAC_PHYCFG2_AC131_LED_MODES;
1221 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001222 case PHY_ID_RTL8211C:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001223 val = MAC_PHYCFG2_RTL8211C_LED_MODES;
1224 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001225 case PHY_ID_RTL8201E:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001226 val = MAC_PHYCFG2_RTL8201E_LED_MODES;
1227 break;
1228 default:
Matt Carlsona9daf362008-05-25 23:49:44 -07001229 return;
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001230 }
1231
1232 if (phydev->interface != PHY_INTERFACE_MODE_RGMII) {
1233 tw32(MAC_PHYCFG2, val);
1234
1235 val = tr32(MAC_PHYCFG1);
Matt Carlsonbb85fbb2009-08-25 10:09:07 +00001236 val &= ~(MAC_PHYCFG1_RGMII_INT |
1237 MAC_PHYCFG1_RXCLK_TO_MASK | MAC_PHYCFG1_TXCLK_TO_MASK);
1238 val |= MAC_PHYCFG1_RXCLK_TIMEOUT | MAC_PHYCFG1_TXCLK_TIMEOUT;
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001239 tw32(MAC_PHYCFG1, val);
1240
1241 return;
1242 }
1243
Joe Perches63c3a662011-04-26 08:12:10 +00001244 if (!tg3_flag(tp, RGMII_INBAND_DISABLE))
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001245 val |= MAC_PHYCFG2_EMODE_MASK_MASK |
1246 MAC_PHYCFG2_FMODE_MASK_MASK |
1247 MAC_PHYCFG2_GMODE_MASK_MASK |
1248 MAC_PHYCFG2_ACT_MASK_MASK |
1249 MAC_PHYCFG2_QUAL_MASK_MASK |
1250 MAC_PHYCFG2_INBAND_ENABLE;
1251
1252 tw32(MAC_PHYCFG2, val);
Matt Carlsona9daf362008-05-25 23:49:44 -07001253
Matt Carlsonbb85fbb2009-08-25 10:09:07 +00001254 val = tr32(MAC_PHYCFG1);
1255 val &= ~(MAC_PHYCFG1_RXCLK_TO_MASK | MAC_PHYCFG1_TXCLK_TO_MASK |
1256 MAC_PHYCFG1_RGMII_EXT_RX_DEC | MAC_PHYCFG1_RGMII_SND_STAT_EN);
Joe Perches63c3a662011-04-26 08:12:10 +00001257 if (!tg3_flag(tp, RGMII_INBAND_DISABLE)) {
1258 if (tg3_flag(tp, RGMII_EXT_IBND_RX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001259 val |= MAC_PHYCFG1_RGMII_EXT_RX_DEC;
Joe Perches63c3a662011-04-26 08:12:10 +00001260 if (tg3_flag(tp, RGMII_EXT_IBND_TX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001261 val |= MAC_PHYCFG1_RGMII_SND_STAT_EN;
1262 }
Matt Carlsonbb85fbb2009-08-25 10:09:07 +00001263 val |= MAC_PHYCFG1_RXCLK_TIMEOUT | MAC_PHYCFG1_TXCLK_TIMEOUT |
1264 MAC_PHYCFG1_RGMII_INT | MAC_PHYCFG1_TXC_DRV;
1265 tw32(MAC_PHYCFG1, val);
Matt Carlsona9daf362008-05-25 23:49:44 -07001266
Matt Carlsona9daf362008-05-25 23:49:44 -07001267 val = tr32(MAC_EXT_RGMII_MODE);
1268 val &= ~(MAC_RGMII_MODE_RX_INT_B |
1269 MAC_RGMII_MODE_RX_QUALITY |
1270 MAC_RGMII_MODE_RX_ACTIVITY |
1271 MAC_RGMII_MODE_RX_ENG_DET |
1272 MAC_RGMII_MODE_TX_ENABLE |
1273 MAC_RGMII_MODE_TX_LOWPWR |
1274 MAC_RGMII_MODE_TX_RESET);
Joe Perches63c3a662011-04-26 08:12:10 +00001275 if (!tg3_flag(tp, RGMII_INBAND_DISABLE)) {
1276 if (tg3_flag(tp, RGMII_EXT_IBND_RX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001277 val |= MAC_RGMII_MODE_RX_INT_B |
1278 MAC_RGMII_MODE_RX_QUALITY |
1279 MAC_RGMII_MODE_RX_ACTIVITY |
1280 MAC_RGMII_MODE_RX_ENG_DET;
Joe Perches63c3a662011-04-26 08:12:10 +00001281 if (tg3_flag(tp, RGMII_EXT_IBND_TX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001282 val |= MAC_RGMII_MODE_TX_ENABLE |
1283 MAC_RGMII_MODE_TX_LOWPWR |
1284 MAC_RGMII_MODE_TX_RESET;
1285 }
1286 tw32(MAC_EXT_RGMII_MODE, val);
1287}
1288
Matt Carlson158d7ab2008-05-29 01:37:54 -07001289static void tg3_mdio_start(struct tg3 *tp)
1290{
Matt Carlson158d7ab2008-05-29 01:37:54 -07001291 tp->mi_mode &= ~MAC_MI_MODE_AUTO_POLL;
1292 tw32_f(MAC_MI_MODE, tp->mi_mode);
1293 udelay(80);
Matt Carlsona9daf362008-05-25 23:49:44 -07001294
Joe Perches63c3a662011-04-26 08:12:10 +00001295 if (tg3_flag(tp, MDIOBUS_INITED) &&
Matt Carlson9ea48182010-02-17 15:17:01 +00001296 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
1297 tg3_mdio_config_5785(tp);
1298}
1299
1300static int tg3_mdio_init(struct tg3 *tp)
1301{
1302 int i;
1303 u32 reg;
1304 struct phy_device *phydev;
1305
Joe Perches63c3a662011-04-26 08:12:10 +00001306 if (tg3_flag(tp, 5717_PLUS)) {
Matt Carlson9c7df912010-06-05 17:24:36 +00001307 u32 is_serdes;
Matt Carlson882e9792009-09-01 13:21:36 +00001308
Matt Carlson69f11c92011-07-13 09:27:30 +00001309 tp->phy_addr = tp->pci_fn + 1;
Matt Carlson882e9792009-09-01 13:21:36 +00001310
Matt Carlsond1ec96a2010-01-12 10:11:38 +00001311 if (tp->pci_chip_rev_id != CHIPREV_ID_5717_A0)
1312 is_serdes = tr32(SG_DIG_STATUS) & SG_DIG_IS_SERDES;
1313 else
1314 is_serdes = tr32(TG3_CPMU_PHY_STRAP) &
1315 TG3_CPMU_PHY_STRAP_IS_SERDES;
Matt Carlson882e9792009-09-01 13:21:36 +00001316 if (is_serdes)
1317 tp->phy_addr += 7;
1318 } else
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001319 tp->phy_addr = TG3_PHY_MII_ADDR;
Matt Carlson882e9792009-09-01 13:21:36 +00001320
Matt Carlson158d7ab2008-05-29 01:37:54 -07001321 tg3_mdio_start(tp);
1322
Joe Perches63c3a662011-04-26 08:12:10 +00001323 if (!tg3_flag(tp, USE_PHYLIB) || tg3_flag(tp, MDIOBUS_INITED))
Matt Carlson158d7ab2008-05-29 01:37:54 -07001324 return 0;
1325
Lennert 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
Matt Carlsonf3791cd2011-11-21 15:01:17 +00001709 if (lcladv & rmtadv & ADVERTISE_1000XPAUSE) {
1710 cap = FLOW_CTRL_TX | FLOW_CTRL_RX;
1711 } else if (lcladv & rmtadv & ADVERTISE_1000XPSE_ASYM) {
1712 if (lcladv & ADVERTISE_1000XPAUSE)
1713 cap = FLOW_CTRL_RX;
1714 if (rmtadv & ADVERTISE_1000XPAUSE)
Steve Glendinninge18ce342008-12-16 02:00:00 -08001715 cap = FLOW_CTRL_TX;
Matt Carlson95e28692008-05-25 23:44:14 -07001716 }
1717
1718 return cap;
1719}
1720
Matt Carlsonf51f3562008-05-25 23:45:08 -07001721static void tg3_setup_flow_control(struct tg3 *tp, u32 lcladv, u32 rmtadv)
Matt Carlson95e28692008-05-25 23:44:14 -07001722{
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001723 u8 autoneg;
Matt Carlsonf51f3562008-05-25 23:45:08 -07001724 u8 flowctrl = 0;
Matt Carlson95e28692008-05-25 23:44:14 -07001725 u32 old_rx_mode = tp->rx_mode;
1726 u32 old_tx_mode = tp->tx_mode;
1727
Joe Perches63c3a662011-04-26 08:12:10 +00001728 if (tg3_flag(tp, USE_PHYLIB))
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001729 autoneg = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]->autoneg;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001730 else
1731 autoneg = tp->link_config.autoneg;
1732
Joe Perches63c3a662011-04-26 08:12:10 +00001733 if (autoneg == AUTONEG_ENABLE && tg3_flag(tp, PAUSE_AUTONEG)) {
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001734 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
Matt Carlsonf51f3562008-05-25 23:45:08 -07001735 flowctrl = tg3_resolve_flowctrl_1000X(lcladv, rmtadv);
Matt Carlson95e28692008-05-25 23:44:14 -07001736 else
Steve Glendinningbc02ff92008-12-16 02:00:48 -08001737 flowctrl = mii_resolve_flowctrl_fdx(lcladv, rmtadv);
Matt Carlsonf51f3562008-05-25 23:45:08 -07001738 } else
1739 flowctrl = tp->link_config.flowctrl;
Matt Carlson95e28692008-05-25 23:44:14 -07001740
Matt Carlsonf51f3562008-05-25 23:45:08 -07001741 tp->link_config.active_flowctrl = flowctrl;
Matt Carlson95e28692008-05-25 23:44:14 -07001742
Steve Glendinninge18ce342008-12-16 02:00:00 -08001743 if (flowctrl & FLOW_CTRL_RX)
Matt Carlson95e28692008-05-25 23:44:14 -07001744 tp->rx_mode |= RX_MODE_FLOW_CTRL_ENABLE;
1745 else
1746 tp->rx_mode &= ~RX_MODE_FLOW_CTRL_ENABLE;
1747
Matt Carlsonf51f3562008-05-25 23:45:08 -07001748 if (old_rx_mode != tp->rx_mode)
Matt Carlson95e28692008-05-25 23:44:14 -07001749 tw32_f(MAC_RX_MODE, tp->rx_mode);
Matt Carlson95e28692008-05-25 23:44:14 -07001750
Steve Glendinninge18ce342008-12-16 02:00:00 -08001751 if (flowctrl & FLOW_CTRL_TX)
Matt Carlson95e28692008-05-25 23:44:14 -07001752 tp->tx_mode |= TX_MODE_FLOW_CTRL_ENABLE;
1753 else
1754 tp->tx_mode &= ~TX_MODE_FLOW_CTRL_ENABLE;
1755
Matt Carlsonf51f3562008-05-25 23:45:08 -07001756 if (old_tx_mode != tp->tx_mode)
Matt Carlson95e28692008-05-25 23:44:14 -07001757 tw32_f(MAC_TX_MODE, tp->tx_mode);
Matt Carlson95e28692008-05-25 23:44:14 -07001758}
1759
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001760static void tg3_adjust_link(struct net_device *dev)
1761{
1762 u8 oldflowctrl, linkmesg = 0;
1763 u32 mac_mode, lcl_adv, rmt_adv;
1764 struct tg3 *tp = netdev_priv(dev);
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001765 struct phy_device *phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001766
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001767 spin_lock_bh(&tp->lock);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001768
1769 mac_mode = tp->mac_mode & ~(MAC_MODE_PORT_MODE_MASK |
1770 MAC_MODE_HALF_DUPLEX);
1771
1772 oldflowctrl = tp->link_config.active_flowctrl;
1773
1774 if (phydev->link) {
1775 lcl_adv = 0;
1776 rmt_adv = 0;
1777
1778 if (phydev->speed == SPEED_100 || phydev->speed == SPEED_10)
1779 mac_mode |= MAC_MODE_PORT_MODE_MII;
Matt Carlsonc3df0742009-11-02 14:27:02 +00001780 else if (phydev->speed == SPEED_1000 ||
1781 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785)
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001782 mac_mode |= MAC_MODE_PORT_MODE_GMII;
Matt Carlsonc3df0742009-11-02 14:27:02 +00001783 else
1784 mac_mode |= MAC_MODE_PORT_MODE_MII;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001785
1786 if (phydev->duplex == DUPLEX_HALF)
1787 mac_mode |= MAC_MODE_HALF_DUPLEX;
1788 else {
1789 lcl_adv = tg3_advert_flowctrl_1000T(
1790 tp->link_config.flowctrl);
1791
1792 if (phydev->pause)
1793 rmt_adv = LPA_PAUSE_CAP;
1794 if (phydev->asym_pause)
1795 rmt_adv |= LPA_PAUSE_ASYM;
1796 }
1797
1798 tg3_setup_flow_control(tp, lcl_adv, rmt_adv);
1799 } else
1800 mac_mode |= MAC_MODE_PORT_MODE_GMII;
1801
1802 if (mac_mode != tp->mac_mode) {
1803 tp->mac_mode = mac_mode;
1804 tw32_f(MAC_MODE, tp->mac_mode);
1805 udelay(40);
1806 }
1807
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001808 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785) {
1809 if (phydev->speed == SPEED_10)
1810 tw32(MAC_MI_STAT,
1811 MAC_MI_STAT_10MBPS_MODE |
1812 MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
1813 else
1814 tw32(MAC_MI_STAT, MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
1815 }
1816
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001817 if (phydev->speed == SPEED_1000 && phydev->duplex == DUPLEX_HALF)
1818 tw32(MAC_TX_LENGTHS,
1819 ((2 << TX_LENGTHS_IPG_CRS_SHIFT) |
1820 (6 << TX_LENGTHS_IPG_SHIFT) |
1821 (0xff << TX_LENGTHS_SLOT_TIME_SHIFT)));
1822 else
1823 tw32(MAC_TX_LENGTHS,
1824 ((2 << TX_LENGTHS_IPG_CRS_SHIFT) |
1825 (6 << TX_LENGTHS_IPG_SHIFT) |
1826 (32 << TX_LENGTHS_SLOT_TIME_SHIFT)));
1827
1828 if ((phydev->link && tp->link_config.active_speed == SPEED_INVALID) ||
1829 (!phydev->link && tp->link_config.active_speed != SPEED_INVALID) ||
1830 phydev->speed != tp->link_config.active_speed ||
1831 phydev->duplex != tp->link_config.active_duplex ||
1832 oldflowctrl != tp->link_config.active_flowctrl)
Matt Carlsonc6cdf432010-04-05 10:19:26 +00001833 linkmesg = 1;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001834
1835 tp->link_config.active_speed = phydev->speed;
1836 tp->link_config.active_duplex = phydev->duplex;
1837
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001838 spin_unlock_bh(&tp->lock);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001839
1840 if (linkmesg)
1841 tg3_link_report(tp);
1842}
1843
1844static int tg3_phy_init(struct tg3 *tp)
1845{
1846 struct phy_device *phydev;
1847
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001848 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED)
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001849 return 0;
1850
1851 /* Bring the PHY back to a known state. */
1852 tg3_bmcr_reset(tp);
1853
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001854 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001855
1856 /* Attach the MAC to the PHY. */
Kay Sieversfb28ad32008-11-10 13:55:14 -08001857 phydev = phy_connect(tp->dev, dev_name(&phydev->dev), tg3_adjust_link,
Matt Carlsona9daf362008-05-25 23:49:44 -07001858 phydev->dev_flags, phydev->interface);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001859 if (IS_ERR(phydev)) {
Matt Carlsonab96b242010-04-05 10:19:22 +00001860 dev_err(&tp->pdev->dev, "Could not attach to PHY\n");
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001861 return PTR_ERR(phydev);
1862 }
1863
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001864 /* Mask with MAC supported features. */
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001865 switch (phydev->interface) {
1866 case PHY_INTERFACE_MODE_GMII:
1867 case PHY_INTERFACE_MODE_RGMII:
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001868 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
Matt Carlson321d32a2008-11-21 17:22:19 -08001869 phydev->supported &= (PHY_GBIT_FEATURES |
1870 SUPPORTED_Pause |
1871 SUPPORTED_Asym_Pause);
1872 break;
1873 }
1874 /* fallthru */
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001875 case PHY_INTERFACE_MODE_MII:
1876 phydev->supported &= (PHY_BASIC_FEATURES |
1877 SUPPORTED_Pause |
1878 SUPPORTED_Asym_Pause);
1879 break;
1880 default:
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001881 phy_disconnect(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001882 return -EINVAL;
1883 }
1884
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001885 tp->phy_flags |= TG3_PHYFLG_IS_CONNECTED;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001886
1887 phydev->advertising = phydev->supported;
1888
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001889 return 0;
1890}
1891
1892static void tg3_phy_start(struct tg3 *tp)
1893{
1894 struct phy_device *phydev;
1895
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001896 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001897 return;
1898
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001899 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001900
Matt Carlson80096062010-08-02 11:26:06 +00001901 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) {
1902 tp->phy_flags &= ~TG3_PHYFLG_IS_LOW_POWER;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001903 phydev->speed = tp->link_config.orig_speed;
1904 phydev->duplex = tp->link_config.orig_duplex;
1905 phydev->autoneg = tp->link_config.orig_autoneg;
1906 phydev->advertising = tp->link_config.orig_advertising;
1907 }
1908
1909 phy_start(phydev);
1910
1911 phy_start_aneg(phydev);
1912}
1913
1914static void tg3_phy_stop(struct tg3 *tp)
1915{
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001916 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001917 return;
1918
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001919 phy_stop(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001920}
1921
1922static void tg3_phy_fini(struct tg3 *tp)
1923{
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001924 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001925 phy_disconnect(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001926 tp->phy_flags &= ~TG3_PHYFLG_IS_CONNECTED;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001927 }
1928}
1929
Matt Carlson941ec902011-08-19 13:58:23 +00001930static int tg3_phy_set_extloopbk(struct tg3 *tp)
1931{
1932 int err;
1933 u32 val;
1934
1935 if (tp->phy_flags & TG3_PHYFLG_IS_FET)
1936 return 0;
1937
1938 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
1939 /* Cannot do read-modify-write on 5401 */
1940 err = tg3_phy_auxctl_write(tp,
1941 MII_TG3_AUXCTL_SHDWSEL_AUXCTL,
1942 MII_TG3_AUXCTL_ACTL_EXTLOOPBK |
1943 0x4c20);
1944 goto done;
1945 }
1946
1947 err = tg3_phy_auxctl_read(tp,
1948 MII_TG3_AUXCTL_SHDWSEL_AUXCTL, &val);
1949 if (err)
1950 return err;
1951
1952 val |= MII_TG3_AUXCTL_ACTL_EXTLOOPBK;
1953 err = tg3_phy_auxctl_write(tp,
1954 MII_TG3_AUXCTL_SHDWSEL_AUXCTL, val);
1955
1956done:
1957 return err;
1958}
1959
Matt Carlson7f97a4b2009-08-25 10:10:03 +00001960static void tg3_phy_fet_toggle_apd(struct tg3 *tp, bool enable)
1961{
1962 u32 phytest;
1963
1964 if (!tg3_readphy(tp, MII_TG3_FET_TEST, &phytest)) {
1965 u32 phy;
1966
1967 tg3_writephy(tp, MII_TG3_FET_TEST,
1968 phytest | MII_TG3_FET_SHADOW_EN);
1969 if (!tg3_readphy(tp, MII_TG3_FET_SHDW_AUXSTAT2, &phy)) {
1970 if (enable)
1971 phy |= MII_TG3_FET_SHDW_AUXSTAT2_APD;
1972 else
1973 phy &= ~MII_TG3_FET_SHDW_AUXSTAT2_APD;
1974 tg3_writephy(tp, MII_TG3_FET_SHDW_AUXSTAT2, phy);
1975 }
1976 tg3_writephy(tp, MII_TG3_FET_TEST, phytest);
1977 }
1978}
1979
Matt Carlson6833c042008-11-21 17:18:59 -08001980static void tg3_phy_toggle_apd(struct tg3 *tp, bool enable)
1981{
1982 u32 reg;
1983
Joe Perches63c3a662011-04-26 08:12:10 +00001984 if (!tg3_flag(tp, 5705_PLUS) ||
1985 (tg3_flag(tp, 5717_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001986 (tp->phy_flags & TG3_PHYFLG_MII_SERDES)))
Matt Carlson6833c042008-11-21 17:18:59 -08001987 return;
1988
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001989 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Matt Carlson7f97a4b2009-08-25 10:10:03 +00001990 tg3_phy_fet_toggle_apd(tp, enable);
1991 return;
1992 }
1993
Matt Carlson6833c042008-11-21 17:18:59 -08001994 reg = MII_TG3_MISC_SHDW_WREN |
1995 MII_TG3_MISC_SHDW_SCR5_SEL |
1996 MII_TG3_MISC_SHDW_SCR5_LPED |
1997 MII_TG3_MISC_SHDW_SCR5_DLPTLM |
1998 MII_TG3_MISC_SHDW_SCR5_SDTL |
1999 MII_TG3_MISC_SHDW_SCR5_C125OE;
2000 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5784 || !enable)
2001 reg |= MII_TG3_MISC_SHDW_SCR5_DLLAPD;
2002
2003 tg3_writephy(tp, MII_TG3_MISC_SHDW, reg);
2004
2005
2006 reg = MII_TG3_MISC_SHDW_WREN |
2007 MII_TG3_MISC_SHDW_APD_SEL |
2008 MII_TG3_MISC_SHDW_APD_WKTM_84MS;
2009 if (enable)
2010 reg |= MII_TG3_MISC_SHDW_APD_ENABLE;
2011
2012 tg3_writephy(tp, MII_TG3_MISC_SHDW, reg);
2013}
2014
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002015static void tg3_phy_toggle_automdix(struct tg3 *tp, int enable)
2016{
2017 u32 phy;
2018
Joe Perches63c3a662011-04-26 08:12:10 +00002019 if (!tg3_flag(tp, 5705_PLUS) ||
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002020 (tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002021 return;
2022
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002023 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002024 u32 ephy;
2025
Matt Carlson535ef6e2009-08-25 10:09:36 +00002026 if (!tg3_readphy(tp, MII_TG3_FET_TEST, &ephy)) {
2027 u32 reg = MII_TG3_FET_SHDW_MISCCTRL;
2028
2029 tg3_writephy(tp, MII_TG3_FET_TEST,
2030 ephy | MII_TG3_FET_SHADOW_EN);
2031 if (!tg3_readphy(tp, reg, &phy)) {
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002032 if (enable)
Matt Carlson535ef6e2009-08-25 10:09:36 +00002033 phy |= MII_TG3_FET_SHDW_MISCCTRL_MDIX;
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002034 else
Matt Carlson535ef6e2009-08-25 10:09:36 +00002035 phy &= ~MII_TG3_FET_SHDW_MISCCTRL_MDIX;
2036 tg3_writephy(tp, reg, phy);
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002037 }
Matt Carlson535ef6e2009-08-25 10:09:36 +00002038 tg3_writephy(tp, MII_TG3_FET_TEST, ephy);
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002039 }
2040 } else {
Matt Carlson15ee95c2011-04-20 07:57:40 +00002041 int ret;
2042
2043 ret = tg3_phy_auxctl_read(tp,
2044 MII_TG3_AUXCTL_SHDWSEL_MISC, &phy);
2045 if (!ret) {
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002046 if (enable)
2047 phy |= MII_TG3_AUXCTL_MISC_FORCE_AMDIX;
2048 else
2049 phy &= ~MII_TG3_AUXCTL_MISC_FORCE_AMDIX;
Matt Carlsonb4bd2922011-04-20 07:57:41 +00002050 tg3_phy_auxctl_write(tp,
2051 MII_TG3_AUXCTL_SHDWSEL_MISC, phy);
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002052 }
2053 }
2054}
2055
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056static void tg3_phy_set_wirespeed(struct tg3 *tp)
2057{
Matt Carlson15ee95c2011-04-20 07:57:40 +00002058 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059 u32 val;
2060
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002061 if (tp->phy_flags & TG3_PHYFLG_NO_ETH_WIRE_SPEED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 return;
2063
Matt Carlson15ee95c2011-04-20 07:57:40 +00002064 ret = tg3_phy_auxctl_read(tp, MII_TG3_AUXCTL_SHDWSEL_MISC, &val);
2065 if (!ret)
Matt Carlsonb4bd2922011-04-20 07:57:41 +00002066 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_MISC,
2067 val | MII_TG3_AUXCTL_MISC_WIRESPD_EN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068}
2069
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002070static void tg3_phy_apply_otp(struct tg3 *tp)
2071{
2072 u32 otp, phy;
2073
2074 if (!tp->phy_otp)
2075 return;
2076
2077 otp = tp->phy_otp;
2078
Matt Carlson1d36ba42011-04-20 07:57:42 +00002079 if (TG3_PHY_AUXCTL_SMDSP_ENABLE(tp))
2080 return;
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002081
2082 phy = ((otp & TG3_OTP_AGCTGT_MASK) >> TG3_OTP_AGCTGT_SHIFT);
2083 phy |= MII_TG3_DSP_TAP1_AGCTGT_DFLT;
2084 tg3_phydsp_write(tp, MII_TG3_DSP_TAP1, phy);
2085
2086 phy = ((otp & TG3_OTP_HPFFLTR_MASK) >> TG3_OTP_HPFFLTR_SHIFT) |
2087 ((otp & TG3_OTP_HPFOVER_MASK) >> TG3_OTP_HPFOVER_SHIFT);
2088 tg3_phydsp_write(tp, MII_TG3_DSP_AADJ1CH0, phy);
2089
2090 phy = ((otp & TG3_OTP_LPFDIS_MASK) >> TG3_OTP_LPFDIS_SHIFT);
2091 phy |= MII_TG3_DSP_AADJ1CH3_ADCCKADJ;
2092 tg3_phydsp_write(tp, MII_TG3_DSP_AADJ1CH3, phy);
2093
2094 phy = ((otp & TG3_OTP_VDAC_MASK) >> TG3_OTP_VDAC_SHIFT);
2095 tg3_phydsp_write(tp, MII_TG3_DSP_EXP75, phy);
2096
2097 phy = ((otp & TG3_OTP_10BTAMP_MASK) >> TG3_OTP_10BTAMP_SHIFT);
2098 tg3_phydsp_write(tp, MII_TG3_DSP_EXP96, phy);
2099
2100 phy = ((otp & TG3_OTP_ROFF_MASK) >> TG3_OTP_ROFF_SHIFT) |
2101 ((otp & TG3_OTP_RCOFF_MASK) >> TG3_OTP_RCOFF_SHIFT);
2102 tg3_phydsp_write(tp, MII_TG3_DSP_EXP97, phy);
2103
Matt Carlson1d36ba42011-04-20 07:57:42 +00002104 TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002105}
2106
Matt Carlson52b02d02010-10-14 10:37:41 +00002107static void tg3_phy_eee_adjust(struct tg3 *tp, u32 current_link_up)
2108{
2109 u32 val;
2110
2111 if (!(tp->phy_flags & TG3_PHYFLG_EEE_CAP))
2112 return;
2113
2114 tp->setlpicnt = 0;
2115
2116 if (tp->link_config.autoneg == AUTONEG_ENABLE &&
2117 current_link_up == 1 &&
Matt Carlsona6b68da2010-12-06 08:28:52 +00002118 tp->link_config.active_duplex == DUPLEX_FULL &&
2119 (tp->link_config.active_speed == SPEED_100 ||
2120 tp->link_config.active_speed == SPEED_1000)) {
Matt Carlson52b02d02010-10-14 10:37:41 +00002121 u32 eeectl;
2122
2123 if (tp->link_config.active_speed == SPEED_1000)
2124 eeectl = TG3_CPMU_EEE_CTRL_EXIT_16_5_US;
2125 else
2126 eeectl = TG3_CPMU_EEE_CTRL_EXIT_36_US;
2127
2128 tw32(TG3_CPMU_EEE_CTRL, eeectl);
2129
Matt Carlson3110f5f52010-12-06 08:28:50 +00002130 tg3_phy_cl45_read(tp, MDIO_MMD_AN,
2131 TG3_CL45_D7_EEERES_STAT, &val);
Matt Carlson52b02d02010-10-14 10:37:41 +00002132
Matt Carlsonb0c59432011-05-19 12:12:48 +00002133 if (val == TG3_CL45_D7_EEERES_STAT_LP_1000T ||
2134 val == TG3_CL45_D7_EEERES_STAT_LP_100TX)
Matt Carlson52b02d02010-10-14 10:37:41 +00002135 tp->setlpicnt = 2;
2136 }
2137
2138 if (!tp->setlpicnt) {
Matt Carlsonb715ce92011-07-20 10:20:52 +00002139 if (current_link_up == 1 &&
2140 !TG3_PHY_AUXCTL_SMDSP_ENABLE(tp)) {
2141 tg3_phydsp_write(tp, MII_TG3_DSP_TAP26, 0x0000);
2142 TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
2143 }
2144
Matt Carlson52b02d02010-10-14 10:37:41 +00002145 val = tr32(TG3_CPMU_EEE_MODE);
2146 tw32(TG3_CPMU_EEE_MODE, val & ~TG3_CPMU_EEEMD_LPI_ENABLE);
2147 }
2148}
2149
Matt Carlsonb0c59432011-05-19 12:12:48 +00002150static void tg3_phy_eee_enable(struct tg3 *tp)
2151{
2152 u32 val;
2153
2154 if (tp->link_config.active_speed == SPEED_1000 &&
2155 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
2156 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
2157 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765) &&
2158 !TG3_PHY_AUXCTL_SMDSP_ENABLE(tp)) {
Matt Carlsonb715ce92011-07-20 10:20:52 +00002159 val = MII_TG3_DSP_TAP26_ALNOKO |
2160 MII_TG3_DSP_TAP26_RMRXSTO;
2161 tg3_phydsp_write(tp, MII_TG3_DSP_TAP26, val);
Matt Carlsonb0c59432011-05-19 12:12:48 +00002162 TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
2163 }
2164
2165 val = tr32(TG3_CPMU_EEE_MODE);
2166 tw32(TG3_CPMU_EEE_MODE, val | TG3_CPMU_EEEMD_LPI_ENABLE);
2167}
2168
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169static int tg3_wait_macro_done(struct tg3 *tp)
2170{
2171 int limit = 100;
2172
2173 while (limit--) {
2174 u32 tmp32;
2175
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002176 if (!tg3_readphy(tp, MII_TG3_DSP_CONTROL, &tmp32)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177 if ((tmp32 & 0x1000) == 0)
2178 break;
2179 }
2180 }
Roel Kluind4675b52009-02-12 16:33:27 -08002181 if (limit < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182 return -EBUSY;
2183
2184 return 0;
2185}
2186
2187static int tg3_phy_write_and_check_testpat(struct tg3 *tp, int *resetp)
2188{
2189 static const u32 test_pat[4][6] = {
2190 { 0x00005555, 0x00000005, 0x00002aaa, 0x0000000a, 0x00003456, 0x00000003 },
2191 { 0x00002aaa, 0x0000000a, 0x00003333, 0x00000003, 0x0000789a, 0x00000005 },
2192 { 0x00005a5a, 0x00000005, 0x00002a6a, 0x0000000a, 0x00001bcd, 0x00000003 },
2193 { 0x00002a5a, 0x0000000a, 0x000033c3, 0x00000003, 0x00002ef1, 0x00000005 }
2194 };
2195 int chan;
2196
2197 for (chan = 0; chan < 4; chan++) {
2198 int i;
2199
2200 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
2201 (chan * 0x2000) | 0x0200);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002202 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0002);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203
2204 for (i = 0; i < 6; i++)
2205 tg3_writephy(tp, MII_TG3_DSP_RW_PORT,
2206 test_pat[chan][i]);
2207
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002208 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0202);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209 if (tg3_wait_macro_done(tp)) {
2210 *resetp = 1;
2211 return -EBUSY;
2212 }
2213
2214 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
2215 (chan * 0x2000) | 0x0200);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002216 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0082);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217 if (tg3_wait_macro_done(tp)) {
2218 *resetp = 1;
2219 return -EBUSY;
2220 }
2221
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002222 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0802);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223 if (tg3_wait_macro_done(tp)) {
2224 *resetp = 1;
2225 return -EBUSY;
2226 }
2227
2228 for (i = 0; i < 6; i += 2) {
2229 u32 low, high;
2230
2231 if (tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &low) ||
2232 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &high) ||
2233 tg3_wait_macro_done(tp)) {
2234 *resetp = 1;
2235 return -EBUSY;
2236 }
2237 low &= 0x7fff;
2238 high &= 0x000f;
2239 if (low != test_pat[chan][i] ||
2240 high != test_pat[chan][i+1]) {
2241 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x000b);
2242 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x4001);
2243 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x4005);
2244
2245 return -EBUSY;
2246 }
2247 }
2248 }
2249
2250 return 0;
2251}
2252
2253static int tg3_phy_reset_chanpat(struct tg3 *tp)
2254{
2255 int chan;
2256
2257 for (chan = 0; chan < 4; chan++) {
2258 int i;
2259
2260 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
2261 (chan * 0x2000) | 0x0200);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002262 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0002);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263 for (i = 0; i < 6; i++)
2264 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x000);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002265 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0202);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266 if (tg3_wait_macro_done(tp))
2267 return -EBUSY;
2268 }
2269
2270 return 0;
2271}
2272
2273static int tg3_phy_reset_5703_4_5(struct tg3 *tp)
2274{
2275 u32 reg32, phy9_orig;
2276 int retries, do_phy_reset, err;
2277
2278 retries = 10;
2279 do_phy_reset = 1;
2280 do {
2281 if (do_phy_reset) {
2282 err = tg3_bmcr_reset(tp);
2283 if (err)
2284 return err;
2285 do_phy_reset = 0;
2286 }
2287
2288 /* Disable transmitter and interrupt. */
2289 if (tg3_readphy(tp, MII_TG3_EXT_CTRL, &reg32))
2290 continue;
2291
2292 reg32 |= 0x3000;
2293 tg3_writephy(tp, MII_TG3_EXT_CTRL, reg32);
2294
2295 /* Set full-duplex, 1000 mbps. */
2296 tg3_writephy(tp, MII_BMCR,
Matt Carlson221c5632011-06-13 13:39:01 +00002297 BMCR_FULLDPLX | BMCR_SPEED1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298
2299 /* Set to master mode. */
Matt Carlson221c5632011-06-13 13:39:01 +00002300 if (tg3_readphy(tp, MII_CTRL1000, &phy9_orig))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301 continue;
2302
Matt Carlson221c5632011-06-13 13:39:01 +00002303 tg3_writephy(tp, MII_CTRL1000,
2304 CTL1000_AS_MASTER | CTL1000_ENABLE_MASTER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305
Matt Carlson1d36ba42011-04-20 07:57:42 +00002306 err = TG3_PHY_AUXCTL_SMDSP_ENABLE(tp);
2307 if (err)
2308 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309
2310 /* Block the PHY control access. */
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00002311 tg3_phydsp_write(tp, 0x8005, 0x0800);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312
2313 err = tg3_phy_write_and_check_testpat(tp, &do_phy_reset);
2314 if (!err)
2315 break;
2316 } while (--retries);
2317
2318 err = tg3_phy_reset_chanpat(tp);
2319 if (err)
2320 return err;
2321
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00002322 tg3_phydsp_write(tp, 0x8005, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323
2324 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x8200);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002325 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326
Matt Carlson1d36ba42011-04-20 07:57:42 +00002327 TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328
Matt Carlson221c5632011-06-13 13:39:01 +00002329 tg3_writephy(tp, MII_CTRL1000, phy9_orig);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330
2331 if (!tg3_readphy(tp, MII_TG3_EXT_CTRL, &reg32)) {
2332 reg32 &= ~0x3000;
2333 tg3_writephy(tp, MII_TG3_EXT_CTRL, reg32);
2334 } else if (!err)
2335 err = -EBUSY;
2336
2337 return err;
2338}
2339
2340/* This will reset the tigon3 PHY if there is no valid
2341 * link unless the FORCE argument is non-zero.
2342 */
2343static int tg3_phy_reset(struct tg3 *tp)
2344{
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002345 u32 val, cpmuctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346 int err;
2347
Michael Chan60189dd2006-12-17 17:08:07 -08002348 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan60189dd2006-12-17 17:08:07 -08002349 val = tr32(GRC_MISC_CFG);
2350 tw32_f(GRC_MISC_CFG, val & ~GRC_MISC_CFG_EPHY_IDDQ);
2351 udelay(40);
2352 }
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002353 err = tg3_readphy(tp, MII_BMSR, &val);
2354 err |= tg3_readphy(tp, MII_BMSR, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355 if (err != 0)
2356 return -EBUSY;
2357
Michael Chanc8e1e822006-04-29 18:55:17 -07002358 if (netif_running(tp->dev) && netif_carrier_ok(tp->dev)) {
2359 netif_carrier_off(tp->dev);
2360 tg3_link_report(tp);
2361 }
2362
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
2364 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
2365 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
2366 err = tg3_phy_reset_5703_4_5(tp);
2367 if (err)
2368 return err;
2369 goto out;
2370 }
2371
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002372 cpmuctrl = 0;
2373 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
2374 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX) {
2375 cpmuctrl = tr32(TG3_CPMU_CTRL);
2376 if (cpmuctrl & CPMU_CTRL_GPHY_10MB_RXONLY)
2377 tw32(TG3_CPMU_CTRL,
2378 cpmuctrl & ~CPMU_CTRL_GPHY_10MB_RXONLY);
2379 }
2380
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381 err = tg3_bmcr_reset(tp);
2382 if (err)
2383 return err;
2384
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002385 if (cpmuctrl & CPMU_CTRL_GPHY_10MB_RXONLY) {
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002386 val = MII_TG3_DSP_EXP8_AEDW | MII_TG3_DSP_EXP8_REJ2MHz;
2387 tg3_phydsp_write(tp, MII_TG3_DSP_EXP8, val);
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002388
2389 tw32(TG3_CPMU_CTRL, cpmuctrl);
2390 }
2391
Matt Carlsonbcb37f62008-11-03 16:52:09 -08002392 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX ||
2393 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5761_AX) {
Matt Carlsonce057f02007-11-12 21:08:03 -08002394 val = tr32(TG3_CPMU_LSPD_1000MB_CLK);
2395 if ((val & CPMU_LSPD_1000MB_MACCLK_MASK) ==
2396 CPMU_LSPD_1000MB_MACCLK_12_5) {
2397 val &= ~CPMU_LSPD_1000MB_MACCLK_MASK;
2398 udelay(40);
2399 tw32_f(TG3_CPMU_LSPD_1000MB_CLK, val);
2400 }
2401 }
2402
Joe Perches63c3a662011-04-26 08:12:10 +00002403 if (tg3_flag(tp, 5717_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002404 (tp->phy_flags & TG3_PHYFLG_MII_SERDES))
Matt Carlsonecf14102010-01-20 16:58:05 +00002405 return 0;
2406
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002407 tg3_phy_apply_otp(tp);
2408
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002409 if (tp->phy_flags & TG3_PHYFLG_ENABLE_APD)
Matt Carlson6833c042008-11-21 17:18:59 -08002410 tg3_phy_toggle_apd(tp, true);
2411 else
2412 tg3_phy_toggle_apd(tp, false);
2413
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414out:
Matt Carlson1d36ba42011-04-20 07:57:42 +00002415 if ((tp->phy_flags & TG3_PHYFLG_ADC_BUG) &&
2416 !TG3_PHY_AUXCTL_SMDSP_ENABLE(tp)) {
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00002417 tg3_phydsp_write(tp, 0x201f, 0x2aaa);
2418 tg3_phydsp_write(tp, 0x000a, 0x0323);
Matt Carlson1d36ba42011-04-20 07:57:42 +00002419 TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420 }
Matt Carlson1d36ba42011-04-20 07:57:42 +00002421
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002422 if (tp->phy_flags & TG3_PHYFLG_5704_A0_BUG) {
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002423 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8d68);
2424 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8d68);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425 }
Matt Carlson1d36ba42011-04-20 07:57:42 +00002426
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002427 if (tp->phy_flags & TG3_PHYFLG_BER_BUG) {
Matt Carlson1d36ba42011-04-20 07:57:42 +00002428 if (!TG3_PHY_AUXCTL_SMDSP_ENABLE(tp)) {
2429 tg3_phydsp_write(tp, 0x000a, 0x310b);
2430 tg3_phydsp_write(tp, 0x201f, 0x9506);
2431 tg3_phydsp_write(tp, 0x401f, 0x14e2);
2432 TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
2433 }
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002434 } else if (tp->phy_flags & TG3_PHYFLG_JITTER_BUG) {
Matt Carlson1d36ba42011-04-20 07:57:42 +00002435 if (!TG3_PHY_AUXCTL_SMDSP_ENABLE(tp)) {
2436 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x000a);
2437 if (tp->phy_flags & TG3_PHYFLG_ADJUST_TRIM) {
2438 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x110b);
2439 tg3_writephy(tp, MII_TG3_TEST1,
2440 MII_TG3_TEST1_TRIM_EN | 0x4);
2441 } else
2442 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x010b);
2443
2444 TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
2445 }
Michael Chanc424cb22006-04-29 18:56:34 -07002446 }
Matt Carlson1d36ba42011-04-20 07:57:42 +00002447
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448 /* Set Extended packet length bit (bit 14) on all chips that */
2449 /* support jumbo frames */
Matt Carlson79eb6902010-02-17 15:17:03 +00002450 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451 /* Cannot do read-modify-write on 5401 */
Matt Carlsonb4bd2922011-04-20 07:57:41 +00002452 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_AUXCTL, 0x4c20);
Joe Perches63c3a662011-04-26 08:12:10 +00002453 } else if (tg3_flag(tp, JUMBO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454 /* Set bit 14 with read-modify-write to preserve other bits */
Matt Carlson15ee95c2011-04-20 07:57:40 +00002455 err = tg3_phy_auxctl_read(tp,
2456 MII_TG3_AUXCTL_SHDWSEL_AUXCTL, &val);
2457 if (!err)
Matt Carlsonb4bd2922011-04-20 07:57:41 +00002458 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_AUXCTL,
2459 val | MII_TG3_AUXCTL_ACTL_EXTPKTLEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460 }
2461
2462 /* Set phy register 0x10 bit 0 to high fifo elasticity to support
2463 * jumbo frames transmission.
2464 */
Joe Perches63c3a662011-04-26 08:12:10 +00002465 if (tg3_flag(tp, JUMBO_CAPABLE)) {
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002466 if (!tg3_readphy(tp, MII_TG3_EXT_CTRL, &val))
Matt Carlsonc6cdf432010-04-05 10:19:26 +00002467 tg3_writephy(tp, MII_TG3_EXT_CTRL,
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002468 val | MII_TG3_EXT_CTRL_FIFO_ELASTIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469 }
2470
Michael Chan715116a2006-09-27 16:09:25 -07002471 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan715116a2006-09-27 16:09:25 -07002472 /* adjust output voltage */
Matt Carlson535ef6e2009-08-25 10:09:36 +00002473 tg3_writephy(tp, MII_TG3_FET_PTEST, 0x12);
Michael Chan715116a2006-09-27 16:09:25 -07002474 }
2475
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002476 tg3_phy_toggle_automdix(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477 tg3_phy_set_wirespeed(tp);
2478 return 0;
2479}
2480
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002481#define TG3_GPIO_MSG_DRVR_PRES 0x00000001
2482#define TG3_GPIO_MSG_NEED_VAUX 0x00000002
2483#define TG3_GPIO_MSG_MASK (TG3_GPIO_MSG_DRVR_PRES | \
2484 TG3_GPIO_MSG_NEED_VAUX)
2485#define TG3_GPIO_MSG_ALL_DRVR_PRES_MASK \
2486 ((TG3_GPIO_MSG_DRVR_PRES << 0) | \
2487 (TG3_GPIO_MSG_DRVR_PRES << 4) | \
2488 (TG3_GPIO_MSG_DRVR_PRES << 8) | \
2489 (TG3_GPIO_MSG_DRVR_PRES << 12))
2490
2491#define TG3_GPIO_MSG_ALL_NEED_VAUX_MASK \
2492 ((TG3_GPIO_MSG_NEED_VAUX << 0) | \
2493 (TG3_GPIO_MSG_NEED_VAUX << 4) | \
2494 (TG3_GPIO_MSG_NEED_VAUX << 8) | \
2495 (TG3_GPIO_MSG_NEED_VAUX << 12))
2496
2497static inline u32 tg3_set_function_status(struct tg3 *tp, u32 newstat)
2498{
2499 u32 status, shift;
2500
2501 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
2502 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
2503 status = tg3_ape_read32(tp, TG3_APE_GPIO_MSG);
2504 else
2505 status = tr32(TG3_CPMU_DRV_STATUS);
2506
2507 shift = TG3_APE_GPIO_MSG_SHIFT + 4 * tp->pci_fn;
2508 status &= ~(TG3_GPIO_MSG_MASK << shift);
2509 status |= (newstat << shift);
2510
2511 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
2512 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
2513 tg3_ape_write32(tp, TG3_APE_GPIO_MSG, status);
2514 else
2515 tw32(TG3_CPMU_DRV_STATUS, status);
2516
2517 return status >> TG3_APE_GPIO_MSG_SHIFT;
2518}
2519
Matt Carlson520b2752011-06-13 13:39:02 +00002520static inline int tg3_pwrsrc_switch_to_vmain(struct tg3 *tp)
2521{
2522 if (!tg3_flag(tp, IS_NIC))
2523 return 0;
2524
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002525 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
2526 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
2527 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
2528 if (tg3_ape_lock(tp, TG3_APE_LOCK_GPIO))
2529 return -EIO;
Matt Carlson520b2752011-06-13 13:39:02 +00002530
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002531 tg3_set_function_status(tp, TG3_GPIO_MSG_DRVR_PRES);
2532
2533 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl,
2534 TG3_GRC_LCLCTL_PWRSW_DELAY);
2535
2536 tg3_ape_unlock(tp, TG3_APE_LOCK_GPIO);
2537 } else {
2538 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl,
2539 TG3_GRC_LCLCTL_PWRSW_DELAY);
2540 }
Matt Carlson6f5c8f832011-07-13 09:27:31 +00002541
Matt Carlson520b2752011-06-13 13:39:02 +00002542 return 0;
2543}
2544
2545static void tg3_pwrsrc_die_with_vmain(struct tg3 *tp)
2546{
2547 u32 grc_local_ctrl;
2548
2549 if (!tg3_flag(tp, IS_NIC) ||
2550 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2551 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)
2552 return;
2553
2554 grc_local_ctrl = tp->grc_local_ctrl | GRC_LCLCTRL_GPIO_OE1;
2555
2556 tw32_wait_f(GRC_LOCAL_CTRL,
2557 grc_local_ctrl | GRC_LCLCTRL_GPIO_OUTPUT1,
2558 TG3_GRC_LCLCTL_PWRSW_DELAY);
2559
2560 tw32_wait_f(GRC_LOCAL_CTRL,
2561 grc_local_ctrl,
2562 TG3_GRC_LCLCTL_PWRSW_DELAY);
2563
2564 tw32_wait_f(GRC_LOCAL_CTRL,
2565 grc_local_ctrl | GRC_LCLCTRL_GPIO_OUTPUT1,
2566 TG3_GRC_LCLCTL_PWRSW_DELAY);
2567}
2568
2569static void tg3_pwrsrc_switch_to_vaux(struct tg3 *tp)
2570{
2571 if (!tg3_flag(tp, IS_NIC))
2572 return;
2573
2574 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2575 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
2576 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2577 (GRC_LCLCTRL_GPIO_OE0 |
2578 GRC_LCLCTRL_GPIO_OE1 |
2579 GRC_LCLCTRL_GPIO_OE2 |
2580 GRC_LCLCTRL_GPIO_OUTPUT0 |
2581 GRC_LCLCTRL_GPIO_OUTPUT1),
2582 TG3_GRC_LCLCTL_PWRSW_DELAY);
2583 } else if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761 ||
2584 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761S) {
2585 /* The 5761 non-e device swaps GPIO 0 and GPIO 2. */
2586 u32 grc_local_ctrl = GRC_LCLCTRL_GPIO_OE0 |
2587 GRC_LCLCTRL_GPIO_OE1 |
2588 GRC_LCLCTRL_GPIO_OE2 |
2589 GRC_LCLCTRL_GPIO_OUTPUT0 |
2590 GRC_LCLCTRL_GPIO_OUTPUT1 |
2591 tp->grc_local_ctrl;
2592 tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl,
2593 TG3_GRC_LCLCTL_PWRSW_DELAY);
2594
2595 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OUTPUT2;
2596 tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl,
2597 TG3_GRC_LCLCTL_PWRSW_DELAY);
2598
2599 grc_local_ctrl &= ~GRC_LCLCTRL_GPIO_OUTPUT0;
2600 tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl,
2601 TG3_GRC_LCLCTL_PWRSW_DELAY);
2602 } else {
2603 u32 no_gpio2;
2604 u32 grc_local_ctrl = 0;
2605
2606 /* Workaround to prevent overdrawing Amps. */
2607 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
2608 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE3;
2609 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2610 grc_local_ctrl,
2611 TG3_GRC_LCLCTL_PWRSW_DELAY);
2612 }
2613
2614 /* On 5753 and variants, GPIO2 cannot be used. */
2615 no_gpio2 = tp->nic_sram_data_cfg &
2616 NIC_SRAM_DATA_CFG_NO_GPIO2;
2617
2618 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE0 |
2619 GRC_LCLCTRL_GPIO_OE1 |
2620 GRC_LCLCTRL_GPIO_OE2 |
2621 GRC_LCLCTRL_GPIO_OUTPUT1 |
2622 GRC_LCLCTRL_GPIO_OUTPUT2;
2623 if (no_gpio2) {
2624 grc_local_ctrl &= ~(GRC_LCLCTRL_GPIO_OE2 |
2625 GRC_LCLCTRL_GPIO_OUTPUT2);
2626 }
2627 tw32_wait_f(GRC_LOCAL_CTRL,
2628 tp->grc_local_ctrl | grc_local_ctrl,
2629 TG3_GRC_LCLCTL_PWRSW_DELAY);
2630
2631 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OUTPUT0;
2632
2633 tw32_wait_f(GRC_LOCAL_CTRL,
2634 tp->grc_local_ctrl | grc_local_ctrl,
2635 TG3_GRC_LCLCTL_PWRSW_DELAY);
2636
2637 if (!no_gpio2) {
2638 grc_local_ctrl &= ~GRC_LCLCTRL_GPIO_OUTPUT2;
2639 tw32_wait_f(GRC_LOCAL_CTRL,
2640 tp->grc_local_ctrl | grc_local_ctrl,
2641 TG3_GRC_LCLCTL_PWRSW_DELAY);
2642 }
2643 }
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002644}
Matt Carlson6f5c8f832011-07-13 09:27:31 +00002645
Matt Carlsoncd0d7222011-07-13 09:27:33 +00002646static void tg3_frob_aux_power_5717(struct tg3 *tp, bool wol_enable)
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002647{
2648 u32 msg = 0;
2649
2650 /* Serialize power state transitions */
2651 if (tg3_ape_lock(tp, TG3_APE_LOCK_GPIO))
2652 return;
2653
Matt Carlsoncd0d7222011-07-13 09:27:33 +00002654 if (tg3_flag(tp, ENABLE_ASF) || tg3_flag(tp, ENABLE_APE) || wol_enable)
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002655 msg = TG3_GPIO_MSG_NEED_VAUX;
2656
2657 msg = tg3_set_function_status(tp, msg);
2658
2659 if (msg & TG3_GPIO_MSG_ALL_DRVR_PRES_MASK)
2660 goto done;
2661
2662 if (msg & TG3_GPIO_MSG_ALL_NEED_VAUX_MASK)
2663 tg3_pwrsrc_switch_to_vaux(tp);
2664 else
2665 tg3_pwrsrc_die_with_vmain(tp);
2666
2667done:
Matt Carlson6f5c8f832011-07-13 09:27:31 +00002668 tg3_ape_unlock(tp, TG3_APE_LOCK_GPIO);
Matt Carlson520b2752011-06-13 13:39:02 +00002669}
2670
Matt Carlsoncd0d7222011-07-13 09:27:33 +00002671static void tg3_frob_aux_power(struct tg3 *tp, bool include_wol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672{
Matt Carlson683644b2011-03-09 16:58:23 +00002673 bool need_vaux = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002674
Matt Carlson334355a2010-01-20 16:58:10 +00002675 /* The GPIOs do something completely different on 57765. */
Joe Perches63c3a662011-04-26 08:12:10 +00002676 if (!tg3_flag(tp, IS_NIC) ||
Matt Carlson334355a2010-01-20 16:58:10 +00002677 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002678 return;
2679
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002680 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
2681 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
2682 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
Matt Carlsoncd0d7222011-07-13 09:27:33 +00002683 tg3_frob_aux_power_5717(tp, include_wol ?
2684 tg3_flag(tp, WOL_ENABLE) != 0 : 0);
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002685 return;
2686 }
2687
2688 if (tp->pdev_peer && tp->pdev_peer != tp->pdev) {
Michael Chan8c2dc7e2005-12-19 16:26:02 -08002689 struct net_device *dev_peer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002690
Michael Chan8c2dc7e2005-12-19 16:26:02 -08002691 dev_peer = pci_get_drvdata(tp->pdev_peer);
Matt Carlson683644b2011-03-09 16:58:23 +00002692
Michael Chanbc1c7562006-03-20 17:48:03 -08002693 /* remove_one() may have been run on the peer. */
Matt Carlson683644b2011-03-09 16:58:23 +00002694 if (dev_peer) {
2695 struct tg3 *tp_peer = netdev_priv(dev_peer);
2696
Joe Perches63c3a662011-04-26 08:12:10 +00002697 if (tg3_flag(tp_peer, INIT_COMPLETE))
Matt Carlson683644b2011-03-09 16:58:23 +00002698 return;
2699
Matt Carlsoncd0d7222011-07-13 09:27:33 +00002700 if ((include_wol && tg3_flag(tp_peer, WOL_ENABLE)) ||
Joe Perches63c3a662011-04-26 08:12:10 +00002701 tg3_flag(tp_peer, ENABLE_ASF))
Matt Carlson683644b2011-03-09 16:58:23 +00002702 need_vaux = true;
2703 }
Michael Chan8c2dc7e2005-12-19 16:26:02 -08002704 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002705
Matt Carlsoncd0d7222011-07-13 09:27:33 +00002706 if ((include_wol && tg3_flag(tp, WOL_ENABLE)) ||
2707 tg3_flag(tp, ENABLE_ASF))
Matt Carlson683644b2011-03-09 16:58:23 +00002708 need_vaux = true;
2709
Matt Carlson520b2752011-06-13 13:39:02 +00002710 if (need_vaux)
2711 tg3_pwrsrc_switch_to_vaux(tp);
2712 else
2713 tg3_pwrsrc_die_with_vmain(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714}
2715
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07002716static int tg3_5700_link_polarity(struct tg3 *tp, u32 speed)
2717{
2718 if (tp->led_ctrl == LED_CTRL_MODE_PHY_2)
2719 return 1;
Matt Carlson79eb6902010-02-17 15:17:03 +00002720 else if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5411) {
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07002721 if (speed != SPEED_10)
2722 return 1;
2723 } else if (speed == SPEED_10)
2724 return 1;
2725
2726 return 0;
2727}
2728
Linus Torvalds1da177e2005-04-16 15:20:36 -07002729static int tg3_setup_phy(struct tg3 *, int);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730static int tg3_halt_cpu(struct tg3 *, u32);
2731
Matt Carlson0a459aa2008-11-03 16:54:15 -08002732static void tg3_power_down_phy(struct tg3 *tp, bool do_low_power)
Michael Chan15c3b692006-03-22 01:06:52 -08002733{
Matt Carlsonce057f02007-11-12 21:08:03 -08002734 u32 val;
2735
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002736 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Michael Chan51297242007-02-13 12:17:57 -08002737 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
2738 u32 sg_dig_ctrl = tr32(SG_DIG_CTRL);
2739 u32 serdes_cfg = tr32(MAC_SERDES_CFG);
2740
2741 sg_dig_ctrl |=
2742 SG_DIG_USING_HW_AUTONEG | SG_DIG_SOFT_RESET;
2743 tw32(SG_DIG_CTRL, sg_dig_ctrl);
2744 tw32(MAC_SERDES_CFG, serdes_cfg | (1 << 15));
2745 }
Michael Chan3f7045c2006-09-27 16:02:29 -07002746 return;
Michael Chan51297242007-02-13 12:17:57 -08002747 }
Michael Chan3f7045c2006-09-27 16:02:29 -07002748
Michael Chan60189dd2006-12-17 17:08:07 -08002749 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan60189dd2006-12-17 17:08:07 -08002750 tg3_bmcr_reset(tp);
2751 val = tr32(GRC_MISC_CFG);
2752 tw32_f(GRC_MISC_CFG, val | GRC_MISC_CFG_EPHY_IDDQ);
2753 udelay(40);
2754 return;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002755 } else if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Matt Carlson0e5f7842009-11-02 14:26:38 +00002756 u32 phytest;
2757 if (!tg3_readphy(tp, MII_TG3_FET_TEST, &phytest)) {
2758 u32 phy;
2759
2760 tg3_writephy(tp, MII_ADVERTISE, 0);
2761 tg3_writephy(tp, MII_BMCR,
2762 BMCR_ANENABLE | BMCR_ANRESTART);
2763
2764 tg3_writephy(tp, MII_TG3_FET_TEST,
2765 phytest | MII_TG3_FET_SHADOW_EN);
2766 if (!tg3_readphy(tp, MII_TG3_FET_SHDW_AUXMODE4, &phy)) {
2767 phy |= MII_TG3_FET_SHDW_AUXMODE4_SBPD;
2768 tg3_writephy(tp,
2769 MII_TG3_FET_SHDW_AUXMODE4,
2770 phy);
2771 }
2772 tg3_writephy(tp, MII_TG3_FET_TEST, phytest);
2773 }
2774 return;
Matt Carlson0a459aa2008-11-03 16:54:15 -08002775 } else if (do_low_power) {
Michael Chan715116a2006-09-27 16:09:25 -07002776 tg3_writephy(tp, MII_TG3_EXT_CTRL,
2777 MII_TG3_EXT_CTRL_FORCE_LED_OFF);
Matt Carlson0a459aa2008-11-03 16:54:15 -08002778
Matt Carlsonb4bd2922011-04-20 07:57:41 +00002779 val = MII_TG3_AUXCTL_PCTL_100TX_LPWR |
2780 MII_TG3_AUXCTL_PCTL_SPR_ISOLATE |
2781 MII_TG3_AUXCTL_PCTL_VREG_11V;
2782 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_PWRCTL, val);
Michael Chan715116a2006-09-27 16:09:25 -07002783 }
Michael Chan3f7045c2006-09-27 16:02:29 -07002784
Michael Chan15c3b692006-03-22 01:06:52 -08002785 /* The PHY should not be powered down on some chips because
2786 * of bugs.
2787 */
2788 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2789 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
2790 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780 &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002791 (tp->phy_flags & TG3_PHYFLG_MII_SERDES)))
Michael Chan15c3b692006-03-22 01:06:52 -08002792 return;
Matt Carlsonce057f02007-11-12 21:08:03 -08002793
Matt Carlsonbcb37f62008-11-03 16:52:09 -08002794 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX ||
2795 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5761_AX) {
Matt Carlsonce057f02007-11-12 21:08:03 -08002796 val = tr32(TG3_CPMU_LSPD_1000MB_CLK);
2797 val &= ~CPMU_LSPD_1000MB_MACCLK_MASK;
2798 val |= CPMU_LSPD_1000MB_MACCLK_12_5;
2799 tw32_f(TG3_CPMU_LSPD_1000MB_CLK, val);
2800 }
2801
Michael Chan15c3b692006-03-22 01:06:52 -08002802 tg3_writephy(tp, MII_BMCR, BMCR_PDOWN);
2803}
2804
Matt Carlson3f007892008-11-03 16:51:36 -08002805/* tp->lock is held. */
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002806static int tg3_nvram_lock(struct tg3 *tp)
2807{
Joe Perches63c3a662011-04-26 08:12:10 +00002808 if (tg3_flag(tp, NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002809 int i;
2810
2811 if (tp->nvram_lock_cnt == 0) {
2812 tw32(NVRAM_SWARB, SWARB_REQ_SET1);
2813 for (i = 0; i < 8000; i++) {
2814 if (tr32(NVRAM_SWARB) & SWARB_GNT1)
2815 break;
2816 udelay(20);
2817 }
2818 if (i == 8000) {
2819 tw32(NVRAM_SWARB, SWARB_REQ_CLR1);
2820 return -ENODEV;
2821 }
2822 }
2823 tp->nvram_lock_cnt++;
2824 }
2825 return 0;
2826}
2827
2828/* tp->lock is held. */
2829static void tg3_nvram_unlock(struct tg3 *tp)
2830{
Joe Perches63c3a662011-04-26 08:12:10 +00002831 if (tg3_flag(tp, NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002832 if (tp->nvram_lock_cnt > 0)
2833 tp->nvram_lock_cnt--;
2834 if (tp->nvram_lock_cnt == 0)
2835 tw32_f(NVRAM_SWARB, SWARB_REQ_CLR1);
2836 }
2837}
2838
2839/* tp->lock is held. */
2840static void tg3_enable_nvram_access(struct tg3 *tp)
2841{
Joe Perches63c3a662011-04-26 08:12:10 +00002842 if (tg3_flag(tp, 5750_PLUS) && !tg3_flag(tp, PROTECTED_NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002843 u32 nvaccess = tr32(NVRAM_ACCESS);
2844
2845 tw32(NVRAM_ACCESS, nvaccess | ACCESS_ENABLE);
2846 }
2847}
2848
2849/* tp->lock is held. */
2850static void tg3_disable_nvram_access(struct tg3 *tp)
2851{
Joe Perches63c3a662011-04-26 08:12:10 +00002852 if (tg3_flag(tp, 5750_PLUS) && !tg3_flag(tp, PROTECTED_NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002853 u32 nvaccess = tr32(NVRAM_ACCESS);
2854
2855 tw32(NVRAM_ACCESS, nvaccess & ~ACCESS_ENABLE);
2856 }
2857}
2858
2859static int tg3_nvram_read_using_eeprom(struct tg3 *tp,
2860 u32 offset, u32 *val)
2861{
2862 u32 tmp;
2863 int i;
2864
2865 if (offset > EEPROM_ADDR_ADDR_MASK || (offset % 4) != 0)
2866 return -EINVAL;
2867
2868 tmp = tr32(GRC_EEPROM_ADDR) & ~(EEPROM_ADDR_ADDR_MASK |
2869 EEPROM_ADDR_DEVID_MASK |
2870 EEPROM_ADDR_READ);
2871 tw32(GRC_EEPROM_ADDR,
2872 tmp |
2873 (0 << EEPROM_ADDR_DEVID_SHIFT) |
2874 ((offset << EEPROM_ADDR_ADDR_SHIFT) &
2875 EEPROM_ADDR_ADDR_MASK) |
2876 EEPROM_ADDR_READ | EEPROM_ADDR_START);
2877
2878 for (i = 0; i < 1000; i++) {
2879 tmp = tr32(GRC_EEPROM_ADDR);
2880
2881 if (tmp & EEPROM_ADDR_COMPLETE)
2882 break;
2883 msleep(1);
2884 }
2885 if (!(tmp & EEPROM_ADDR_COMPLETE))
2886 return -EBUSY;
2887
Matt Carlson62cedd12009-04-20 14:52:29 -07002888 tmp = tr32(GRC_EEPROM_DATA);
2889
2890 /*
2891 * The data will always be opposite the native endian
2892 * format. Perform a blind byteswap to compensate.
2893 */
2894 *val = swab32(tmp);
2895
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002896 return 0;
2897}
2898
2899#define NVRAM_CMD_TIMEOUT 10000
2900
2901static int tg3_nvram_exec_cmd(struct tg3 *tp, u32 nvram_cmd)
2902{
2903 int i;
2904
2905 tw32(NVRAM_CMD, nvram_cmd);
2906 for (i = 0; i < NVRAM_CMD_TIMEOUT; i++) {
2907 udelay(10);
2908 if (tr32(NVRAM_CMD) & NVRAM_CMD_DONE) {
2909 udelay(10);
2910 break;
2911 }
2912 }
2913
2914 if (i == NVRAM_CMD_TIMEOUT)
2915 return -EBUSY;
2916
2917 return 0;
2918}
2919
2920static u32 tg3_nvram_phys_addr(struct tg3 *tp, u32 addr)
2921{
Joe Perches63c3a662011-04-26 08:12:10 +00002922 if (tg3_flag(tp, NVRAM) &&
2923 tg3_flag(tp, NVRAM_BUFFERED) &&
2924 tg3_flag(tp, FLASH) &&
2925 !tg3_flag(tp, NO_NVRAM_ADDR_TRANS) &&
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002926 (tp->nvram_jedecnum == JEDEC_ATMEL))
2927
2928 addr = ((addr / tp->nvram_pagesize) <<
2929 ATMEL_AT45DB0X1B_PAGE_POS) +
2930 (addr % tp->nvram_pagesize);
2931
2932 return addr;
2933}
2934
2935static u32 tg3_nvram_logical_addr(struct tg3 *tp, u32 addr)
2936{
Joe Perches63c3a662011-04-26 08:12:10 +00002937 if (tg3_flag(tp, NVRAM) &&
2938 tg3_flag(tp, NVRAM_BUFFERED) &&
2939 tg3_flag(tp, FLASH) &&
2940 !tg3_flag(tp, NO_NVRAM_ADDR_TRANS) &&
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002941 (tp->nvram_jedecnum == JEDEC_ATMEL))
2942
2943 addr = ((addr >> ATMEL_AT45DB0X1B_PAGE_POS) *
2944 tp->nvram_pagesize) +
2945 (addr & ((1 << ATMEL_AT45DB0X1B_PAGE_POS) - 1));
2946
2947 return addr;
2948}
2949
Matt Carlsone4f34112009-02-25 14:25:00 +00002950/* NOTE: Data read in from NVRAM is byteswapped according to
2951 * the byteswapping settings for all other register accesses.
2952 * tg3 devices are BE devices, so on a BE machine, the data
2953 * returned will be exactly as it is seen in NVRAM. On a LE
2954 * machine, the 32-bit value will be byteswapped.
2955 */
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002956static int tg3_nvram_read(struct tg3 *tp, u32 offset, u32 *val)
2957{
2958 int ret;
2959
Joe Perches63c3a662011-04-26 08:12:10 +00002960 if (!tg3_flag(tp, NVRAM))
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002961 return tg3_nvram_read_using_eeprom(tp, offset, val);
2962
2963 offset = tg3_nvram_phys_addr(tp, offset);
2964
2965 if (offset > NVRAM_ADDR_MSK)
2966 return -EINVAL;
2967
2968 ret = tg3_nvram_lock(tp);
2969 if (ret)
2970 return ret;
2971
2972 tg3_enable_nvram_access(tp);
2973
2974 tw32(NVRAM_ADDR, offset);
2975 ret = tg3_nvram_exec_cmd(tp, NVRAM_CMD_RD | NVRAM_CMD_GO |
2976 NVRAM_CMD_FIRST | NVRAM_CMD_LAST | NVRAM_CMD_DONE);
2977
2978 if (ret == 0)
Matt Carlsone4f34112009-02-25 14:25:00 +00002979 *val = tr32(NVRAM_RDDATA);
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002980
2981 tg3_disable_nvram_access(tp);
2982
2983 tg3_nvram_unlock(tp);
2984
2985 return ret;
2986}
2987
Matt Carlsona9dc5292009-02-25 14:25:30 +00002988/* Ensures NVRAM data is in bytestream format. */
2989static int tg3_nvram_read_be32(struct tg3 *tp, u32 offset, __be32 *val)
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002990{
2991 u32 v;
Matt Carlsona9dc5292009-02-25 14:25:30 +00002992 int res = tg3_nvram_read(tp, offset, &v);
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002993 if (!res)
Matt Carlsona9dc5292009-02-25 14:25:30 +00002994 *val = cpu_to_be32(v);
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002995 return res;
2996}
2997
Matt Carlson997b4f12011-08-31 11:44:53 +00002998#define RX_CPU_SCRATCH_BASE 0x30000
2999#define RX_CPU_SCRATCH_SIZE 0x04000
3000#define TX_CPU_SCRATCH_BASE 0x34000
3001#define TX_CPU_SCRATCH_SIZE 0x04000
3002
3003/* tp->lock is held. */
3004static int tg3_halt_cpu(struct tg3 *tp, u32 offset)
3005{
3006 int i;
3007
3008 BUG_ON(offset == TX_CPU_BASE && tg3_flag(tp, 5705_PLUS));
3009
3010 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
3011 u32 val = tr32(GRC_VCPU_EXT_CTRL);
3012
3013 tw32(GRC_VCPU_EXT_CTRL, val | GRC_VCPU_EXT_CTRL_HALT_CPU);
3014 return 0;
3015 }
3016 if (offset == RX_CPU_BASE) {
3017 for (i = 0; i < 10000; i++) {
3018 tw32(offset + CPU_STATE, 0xffffffff);
3019 tw32(offset + CPU_MODE, CPU_MODE_HALT);
3020 if (tr32(offset + CPU_MODE) & CPU_MODE_HALT)
3021 break;
3022 }
3023
3024 tw32(offset + CPU_STATE, 0xffffffff);
3025 tw32_f(offset + CPU_MODE, CPU_MODE_HALT);
3026 udelay(10);
3027 } else {
3028 for (i = 0; i < 10000; i++) {
3029 tw32(offset + CPU_STATE, 0xffffffff);
3030 tw32(offset + CPU_MODE, CPU_MODE_HALT);
3031 if (tr32(offset + CPU_MODE) & CPU_MODE_HALT)
3032 break;
3033 }
3034 }
3035
3036 if (i >= 10000) {
3037 netdev_err(tp->dev, "%s timed out, %s CPU\n",
3038 __func__, offset == RX_CPU_BASE ? "RX" : "TX");
3039 return -ENODEV;
3040 }
3041
3042 /* Clear firmware's nvram arbitration. */
3043 if (tg3_flag(tp, NVRAM))
3044 tw32(NVRAM_SWARB, SWARB_REQ_CLR0);
3045 return 0;
3046}
3047
3048struct fw_info {
3049 unsigned int fw_base;
3050 unsigned int fw_len;
3051 const __be32 *fw_data;
3052};
3053
3054/* tp->lock is held. */
3055static int tg3_load_firmware_cpu(struct tg3 *tp, u32 cpu_base,
3056 u32 cpu_scratch_base, int cpu_scratch_size,
3057 struct fw_info *info)
3058{
3059 int err, lock_err, i;
3060 void (*write_op)(struct tg3 *, u32, u32);
3061
3062 if (cpu_base == TX_CPU_BASE && tg3_flag(tp, 5705_PLUS)) {
3063 netdev_err(tp->dev,
3064 "%s: Trying to load TX cpu firmware which is 5705\n",
3065 __func__);
3066 return -EINVAL;
3067 }
3068
3069 if (tg3_flag(tp, 5705_PLUS))
3070 write_op = tg3_write_mem;
3071 else
3072 write_op = tg3_write_indirect_reg32;
3073
3074 /* It is possible that bootcode is still loading at this point.
3075 * Get the nvram lock first before halting the cpu.
3076 */
3077 lock_err = tg3_nvram_lock(tp);
3078 err = tg3_halt_cpu(tp, cpu_base);
3079 if (!lock_err)
3080 tg3_nvram_unlock(tp);
3081 if (err)
3082 goto out;
3083
3084 for (i = 0; i < cpu_scratch_size; i += sizeof(u32))
3085 write_op(tp, cpu_scratch_base + i, 0);
3086 tw32(cpu_base + CPU_STATE, 0xffffffff);
3087 tw32(cpu_base + CPU_MODE, tr32(cpu_base+CPU_MODE)|CPU_MODE_HALT);
3088 for (i = 0; i < (info->fw_len / sizeof(u32)); i++)
3089 write_op(tp, (cpu_scratch_base +
3090 (info->fw_base & 0xffff) +
3091 (i * sizeof(u32))),
3092 be32_to_cpu(info->fw_data[i]));
3093
3094 err = 0;
3095
3096out:
3097 return err;
3098}
3099
3100/* tp->lock is held. */
3101static int tg3_load_5701_a0_firmware_fix(struct tg3 *tp)
3102{
3103 struct fw_info info;
3104 const __be32 *fw_data;
3105 int err, i;
3106
3107 fw_data = (void *)tp->fw->data;
3108
3109 /* Firmware blob starts with version numbers, followed by
3110 start address and length. We are setting complete length.
3111 length = end_address_of_bss - start_address_of_text.
3112 Remainder is the blob to be loaded contiguously
3113 from start address. */
3114
3115 info.fw_base = be32_to_cpu(fw_data[1]);
3116 info.fw_len = tp->fw->size - 12;
3117 info.fw_data = &fw_data[3];
3118
3119 err = tg3_load_firmware_cpu(tp, RX_CPU_BASE,
3120 RX_CPU_SCRATCH_BASE, RX_CPU_SCRATCH_SIZE,
3121 &info);
3122 if (err)
3123 return err;
3124
3125 err = tg3_load_firmware_cpu(tp, TX_CPU_BASE,
3126 TX_CPU_SCRATCH_BASE, TX_CPU_SCRATCH_SIZE,
3127 &info);
3128 if (err)
3129 return err;
3130
3131 /* Now startup only the RX cpu. */
3132 tw32(RX_CPU_BASE + CPU_STATE, 0xffffffff);
3133 tw32_f(RX_CPU_BASE + CPU_PC, info.fw_base);
3134
3135 for (i = 0; i < 5; i++) {
3136 if (tr32(RX_CPU_BASE + CPU_PC) == info.fw_base)
3137 break;
3138 tw32(RX_CPU_BASE + CPU_STATE, 0xffffffff);
3139 tw32(RX_CPU_BASE + CPU_MODE, CPU_MODE_HALT);
3140 tw32_f(RX_CPU_BASE + CPU_PC, info.fw_base);
3141 udelay(1000);
3142 }
3143 if (i >= 5) {
3144 netdev_err(tp->dev, "%s fails to set RX CPU PC, is %08x "
3145 "should be %08x\n", __func__,
3146 tr32(RX_CPU_BASE + CPU_PC), info.fw_base);
3147 return -ENODEV;
3148 }
3149 tw32(RX_CPU_BASE + CPU_STATE, 0xffffffff);
3150 tw32_f(RX_CPU_BASE + CPU_MODE, 0x00000000);
3151
3152 return 0;
3153}
3154
3155/* tp->lock is held. */
3156static int tg3_load_tso_firmware(struct tg3 *tp)
3157{
3158 struct fw_info info;
3159 const __be32 *fw_data;
3160 unsigned long cpu_base, cpu_scratch_base, cpu_scratch_size;
3161 int err, i;
3162
3163 if (tg3_flag(tp, HW_TSO_1) ||
3164 tg3_flag(tp, HW_TSO_2) ||
3165 tg3_flag(tp, HW_TSO_3))
3166 return 0;
3167
3168 fw_data = (void *)tp->fw->data;
3169
3170 /* Firmware blob starts with version numbers, followed by
3171 start address and length. We are setting complete length.
3172 length = end_address_of_bss - start_address_of_text.
3173 Remainder is the blob to be loaded contiguously
3174 from start address. */
3175
3176 info.fw_base = be32_to_cpu(fw_data[1]);
3177 cpu_scratch_size = tp->fw_len;
3178 info.fw_len = tp->fw->size - 12;
3179 info.fw_data = &fw_data[3];
3180
3181 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
3182 cpu_base = RX_CPU_BASE;
3183 cpu_scratch_base = NIC_SRAM_MBUF_POOL_BASE5705;
3184 } else {
3185 cpu_base = TX_CPU_BASE;
3186 cpu_scratch_base = TX_CPU_SCRATCH_BASE;
3187 cpu_scratch_size = TX_CPU_SCRATCH_SIZE;
3188 }
3189
3190 err = tg3_load_firmware_cpu(tp, cpu_base,
3191 cpu_scratch_base, cpu_scratch_size,
3192 &info);
3193 if (err)
3194 return err;
3195
3196 /* Now startup the cpu. */
3197 tw32(cpu_base + CPU_STATE, 0xffffffff);
3198 tw32_f(cpu_base + CPU_PC, info.fw_base);
3199
3200 for (i = 0; i < 5; i++) {
3201 if (tr32(cpu_base + CPU_PC) == info.fw_base)
3202 break;
3203 tw32(cpu_base + CPU_STATE, 0xffffffff);
3204 tw32(cpu_base + CPU_MODE, CPU_MODE_HALT);
3205 tw32_f(cpu_base + CPU_PC, info.fw_base);
3206 udelay(1000);
3207 }
3208 if (i >= 5) {
3209 netdev_err(tp->dev,
3210 "%s fails to set CPU PC, is %08x should be %08x\n",
3211 __func__, tr32(cpu_base + CPU_PC), info.fw_base);
3212 return -ENODEV;
3213 }
3214 tw32(cpu_base + CPU_STATE, 0xffffffff);
3215 tw32_f(cpu_base + CPU_MODE, 0x00000000);
3216 return 0;
3217}
3218
3219
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003220/* tp->lock is held. */
Matt Carlson3f007892008-11-03 16:51:36 -08003221static void __tg3_set_mac_addr(struct tg3 *tp, int skip_mac_1)
3222{
3223 u32 addr_high, addr_low;
3224 int i;
3225
3226 addr_high = ((tp->dev->dev_addr[0] << 8) |
3227 tp->dev->dev_addr[1]);
3228 addr_low = ((tp->dev->dev_addr[2] << 24) |
3229 (tp->dev->dev_addr[3] << 16) |
3230 (tp->dev->dev_addr[4] << 8) |
3231 (tp->dev->dev_addr[5] << 0));
3232 for (i = 0; i < 4; i++) {
3233 if (i == 1 && skip_mac_1)
3234 continue;
3235 tw32(MAC_ADDR_0_HIGH + (i * 8), addr_high);
3236 tw32(MAC_ADDR_0_LOW + (i * 8), addr_low);
3237 }
3238
3239 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
3240 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
3241 for (i = 0; i < 12; i++) {
3242 tw32(MAC_EXTADDR_0_HIGH + (i * 8), addr_high);
3243 tw32(MAC_EXTADDR_0_LOW + (i * 8), addr_low);
3244 }
3245 }
3246
3247 addr_high = (tp->dev->dev_addr[0] +
3248 tp->dev->dev_addr[1] +
3249 tp->dev->dev_addr[2] +
3250 tp->dev->dev_addr[3] +
3251 tp->dev->dev_addr[4] +
3252 tp->dev->dev_addr[5]) &
3253 TX_BACKOFF_SEED_MASK;
3254 tw32(MAC_TX_BACKOFF_SEED, addr_high);
3255}
3256
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003257static void tg3_enable_register_access(struct tg3 *tp)
3258{
3259 /*
3260 * Make sure register accesses (indirect or otherwise) will function
3261 * correctly.
3262 */
3263 pci_write_config_dword(tp->pdev,
3264 TG3PCI_MISC_HOST_CTRL, tp->misc_host_ctrl);
3265}
3266
3267static int tg3_power_up(struct tg3 *tp)
3268{
Matt Carlsonbed98292011-07-13 09:27:29 +00003269 int err;
3270
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003271 tg3_enable_register_access(tp);
3272
Matt Carlsonbed98292011-07-13 09:27:29 +00003273 err = pci_set_power_state(tp->pdev, PCI_D0);
3274 if (!err) {
3275 /* Switch out of Vaux if it is a NIC */
3276 tg3_pwrsrc_switch_to_vmain(tp);
3277 } else {
3278 netdev_err(tp->dev, "Transition to D0 failed\n");
3279 }
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003280
Matt Carlsonbed98292011-07-13 09:27:29 +00003281 return err;
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003282}
3283
3284static int tg3_power_down_prepare(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003285{
3286 u32 misc_host_ctrl;
Matt Carlson0a459aa2008-11-03 16:54:15 -08003287 bool device_should_wake, do_low_power;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003288
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003289 tg3_enable_register_access(tp);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08003290
3291 /* Restore the CLKREQ setting. */
Joe Perches63c3a662011-04-26 08:12:10 +00003292 if (tg3_flag(tp, CLKREQ_BUG)) {
Matt Carlson5e7dfd02008-11-21 17:18:16 -08003293 u16 lnkctl;
3294
3295 pci_read_config_word(tp->pdev,
Jon Mason708ebb32011-06-27 12:56:50 +00003296 pci_pcie_cap(tp->pdev) + PCI_EXP_LNKCTL,
Matt Carlson5e7dfd02008-11-21 17:18:16 -08003297 &lnkctl);
3298 lnkctl |= PCI_EXP_LNKCTL_CLKREQ_EN;
3299 pci_write_config_word(tp->pdev,
Jon Mason708ebb32011-06-27 12:56:50 +00003300 pci_pcie_cap(tp->pdev) + PCI_EXP_LNKCTL,
Matt Carlson5e7dfd02008-11-21 17:18:16 -08003301 lnkctl);
3302 }
3303
Linus Torvalds1da177e2005-04-16 15:20:36 -07003304 misc_host_ctrl = tr32(TG3PCI_MISC_HOST_CTRL);
3305 tw32(TG3PCI_MISC_HOST_CTRL,
3306 misc_host_ctrl | MISC_HOST_CTRL_MASK_PCI_INT);
3307
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003308 device_should_wake = device_may_wakeup(&tp->pdev->dev) &&
Joe Perches63c3a662011-04-26 08:12:10 +00003309 tg3_flag(tp, WOL_ENABLE);
Matt Carlson05ac4cb2008-11-03 16:53:46 -08003310
Joe Perches63c3a662011-04-26 08:12:10 +00003311 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson0a459aa2008-11-03 16:54:15 -08003312 do_low_power = false;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003313 if ((tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) &&
Matt Carlson80096062010-08-02 11:26:06 +00003314 !(tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07003315 struct phy_device *phydev;
Matt Carlson0a459aa2008-11-03 16:54:15 -08003316 u32 phyid, advertising;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07003317
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00003318 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07003319
Matt Carlson80096062010-08-02 11:26:06 +00003320 tp->phy_flags |= TG3_PHYFLG_IS_LOW_POWER;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07003321
3322 tp->link_config.orig_speed = phydev->speed;
3323 tp->link_config.orig_duplex = phydev->duplex;
3324 tp->link_config.orig_autoneg = phydev->autoneg;
3325 tp->link_config.orig_advertising = phydev->advertising;
3326
3327 advertising = ADVERTISED_TP |
3328 ADVERTISED_Pause |
3329 ADVERTISED_Autoneg |
3330 ADVERTISED_10baseT_Half;
3331
Joe Perches63c3a662011-04-26 08:12:10 +00003332 if (tg3_flag(tp, ENABLE_ASF) || device_should_wake) {
3333 if (tg3_flag(tp, WOL_SPEED_100MB))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07003334 advertising |=
3335 ADVERTISED_100baseT_Half |
3336 ADVERTISED_100baseT_Full |
3337 ADVERTISED_10baseT_Full;
3338 else
3339 advertising |= ADVERTISED_10baseT_Full;
3340 }
3341
3342 phydev->advertising = advertising;
3343
3344 phy_start_aneg(phydev);
Matt Carlson0a459aa2008-11-03 16:54:15 -08003345
3346 phyid = phydev->drv->phy_id & phydev->drv->phy_id_mask;
Matt Carlson6a443a02010-02-17 15:17:04 +00003347 if (phyid != PHY_ID_BCMAC131) {
3348 phyid &= PHY_BCM_OUI_MASK;
3349 if (phyid == PHY_BCM_OUI_1 ||
3350 phyid == PHY_BCM_OUI_2 ||
3351 phyid == PHY_BCM_OUI_3)
Matt Carlson0a459aa2008-11-03 16:54:15 -08003352 do_low_power = true;
3353 }
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07003354 }
Matt Carlsondd477002008-05-25 23:45:58 -07003355 } else {
Matt Carlson20232762008-12-21 20:18:56 -08003356 do_low_power = true;
Matt Carlson0a459aa2008-11-03 16:54:15 -08003357
Matt Carlson80096062010-08-02 11:26:06 +00003358 if (!(tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)) {
3359 tp->phy_flags |= TG3_PHYFLG_IS_LOW_POWER;
Matt Carlsondd477002008-05-25 23:45:58 -07003360 tp->link_config.orig_speed = tp->link_config.speed;
3361 tp->link_config.orig_duplex = tp->link_config.duplex;
3362 tp->link_config.orig_autoneg = tp->link_config.autoneg;
3363 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003364
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003365 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES)) {
Matt Carlsondd477002008-05-25 23:45:58 -07003366 tp->link_config.speed = SPEED_10;
3367 tp->link_config.duplex = DUPLEX_HALF;
3368 tp->link_config.autoneg = AUTONEG_ENABLE;
3369 tg3_setup_phy(tp, 0);
3370 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003371 }
3372
Michael Chanb5d37722006-09-27 16:06:21 -07003373 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
3374 u32 val;
3375
3376 val = tr32(GRC_VCPU_EXT_CTRL);
3377 tw32(GRC_VCPU_EXT_CTRL, val | GRC_VCPU_EXT_CTRL_DISABLE_WOL);
Joe Perches63c3a662011-04-26 08:12:10 +00003378 } else if (!tg3_flag(tp, ENABLE_ASF)) {
Michael Chan6921d202005-12-13 21:15:53 -08003379 int i;
3380 u32 val;
3381
3382 for (i = 0; i < 200; i++) {
3383 tg3_read_mem(tp, NIC_SRAM_FW_ASF_STATUS_MBOX, &val);
3384 if (val == ~NIC_SRAM_FIRMWARE_MBOX_MAGIC1)
3385 break;
3386 msleep(1);
3387 }
3388 }
Joe Perches63c3a662011-04-26 08:12:10 +00003389 if (tg3_flag(tp, WOL_CAP))
Gary Zambranoa85feb82007-05-05 11:52:19 -07003390 tg3_write_mem(tp, NIC_SRAM_WOL_MBOX, WOL_SIGNATURE |
3391 WOL_DRV_STATE_SHUTDOWN |
3392 WOL_DRV_WOL |
3393 WOL_SET_MAGIC_PKT);
Michael Chan6921d202005-12-13 21:15:53 -08003394
Matt Carlson05ac4cb2008-11-03 16:53:46 -08003395 if (device_should_wake) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003396 u32 mac_mode;
3397
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003398 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES)) {
Matt Carlsonb4bd2922011-04-20 07:57:41 +00003399 if (do_low_power &&
3400 !(tp->phy_flags & TG3_PHYFLG_IS_FET)) {
3401 tg3_phy_auxctl_write(tp,
3402 MII_TG3_AUXCTL_SHDWSEL_PWRCTL,
3403 MII_TG3_AUXCTL_PCTL_WOL_EN |
3404 MII_TG3_AUXCTL_PCTL_100TX_LPWR |
3405 MII_TG3_AUXCTL_PCTL_CL_AB_TXDAC);
Matt Carlsondd477002008-05-25 23:45:58 -07003406 udelay(40);
3407 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003408
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003409 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
Michael Chan3f7045c2006-09-27 16:02:29 -07003410 mac_mode = MAC_MODE_PORT_MODE_GMII;
3411 else
3412 mac_mode = MAC_MODE_PORT_MODE_MII;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003413
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07003414 mac_mode |= tp->mac_mode & MAC_MODE_LINK_POLARITY;
3415 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
3416 ASIC_REV_5700) {
Joe Perches63c3a662011-04-26 08:12:10 +00003417 u32 speed = tg3_flag(tp, WOL_SPEED_100MB) ?
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07003418 SPEED_100 : SPEED_10;
3419 if (tg3_5700_link_polarity(tp, speed))
3420 mac_mode |= MAC_MODE_LINK_POLARITY;
3421 else
3422 mac_mode &= ~MAC_MODE_LINK_POLARITY;
3423 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003424 } else {
3425 mac_mode = MAC_MODE_PORT_MODE_TBI;
3426 }
3427
Joe Perches63c3a662011-04-26 08:12:10 +00003428 if (!tg3_flag(tp, 5750_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003429 tw32(MAC_LED_CTRL, tp->led_ctrl);
3430
Matt Carlson05ac4cb2008-11-03 16:53:46 -08003431 mac_mode |= MAC_MODE_MAGIC_PKT_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00003432 if ((tg3_flag(tp, 5705_PLUS) && !tg3_flag(tp, 5780_CLASS)) &&
3433 (tg3_flag(tp, ENABLE_ASF) || tg3_flag(tp, ENABLE_APE)))
Matt Carlson05ac4cb2008-11-03 16:53:46 -08003434 mac_mode |= MAC_MODE_KEEP_FRAME_IN_WOL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003435
Joe Perches63c3a662011-04-26 08:12:10 +00003436 if (tg3_flag(tp, ENABLE_APE))
Matt Carlsond2394e6b2010-11-24 08:31:47 +00003437 mac_mode |= MAC_MODE_APE_TX_EN |
3438 MAC_MODE_APE_RX_EN |
3439 MAC_MODE_TDE_ENABLE;
Matt Carlson3bda1252008-08-15 14:08:22 -07003440
Linus Torvalds1da177e2005-04-16 15:20:36 -07003441 tw32_f(MAC_MODE, mac_mode);
3442 udelay(100);
3443
3444 tw32_f(MAC_RX_MODE, RX_MODE_ENABLE);
3445 udelay(10);
3446 }
3447
Joe Perches63c3a662011-04-26 08:12:10 +00003448 if (!tg3_flag(tp, WOL_SPEED_100MB) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003449 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
3450 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)) {
3451 u32 base_val;
3452
3453 base_val = tp->pci_clock_ctrl;
3454 base_val |= (CLOCK_CTRL_RXCLK_DISABLE |
3455 CLOCK_CTRL_TXCLK_DISABLE);
3456
Michael Chanb401e9e2005-12-19 16:27:04 -08003457 tw32_wait_f(TG3PCI_CLOCK_CTRL, base_val | CLOCK_CTRL_ALTCLK |
3458 CLOCK_CTRL_PWRDOWN_PLL133, 40);
Joe Perches63c3a662011-04-26 08:12:10 +00003459 } else if (tg3_flag(tp, 5780_CLASS) ||
3460 tg3_flag(tp, CPMU_PRESENT) ||
Matt Carlson6ff6f812011-05-19 12:12:54 +00003461 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan4cf78e42005-07-25 12:29:19 -07003462 /* do nothing */
Joe Perches63c3a662011-04-26 08:12:10 +00003463 } else if (!(tg3_flag(tp, 5750_PLUS) && tg3_flag(tp, ENABLE_ASF))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003464 u32 newbits1, newbits2;
3465
3466 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
3467 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
3468 newbits1 = (CLOCK_CTRL_RXCLK_DISABLE |
3469 CLOCK_CTRL_TXCLK_DISABLE |
3470 CLOCK_CTRL_ALTCLK);
3471 newbits2 = newbits1 | CLOCK_CTRL_44MHZ_CORE;
Joe Perches63c3a662011-04-26 08:12:10 +00003472 } else if (tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003473 newbits1 = CLOCK_CTRL_625_CORE;
3474 newbits2 = newbits1 | CLOCK_CTRL_ALTCLK;
3475 } else {
3476 newbits1 = CLOCK_CTRL_ALTCLK;
3477 newbits2 = newbits1 | CLOCK_CTRL_44MHZ_CORE;
3478 }
3479
Michael Chanb401e9e2005-12-19 16:27:04 -08003480 tw32_wait_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl | newbits1,
3481 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003482
Michael Chanb401e9e2005-12-19 16:27:04 -08003483 tw32_wait_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl | newbits2,
3484 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003485
Joe Perches63c3a662011-04-26 08:12:10 +00003486 if (!tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003487 u32 newbits3;
3488
3489 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
3490 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
3491 newbits3 = (CLOCK_CTRL_RXCLK_DISABLE |
3492 CLOCK_CTRL_TXCLK_DISABLE |
3493 CLOCK_CTRL_44MHZ_CORE);
3494 } else {
3495 newbits3 = CLOCK_CTRL_44MHZ_CORE;
3496 }
3497
Michael Chanb401e9e2005-12-19 16:27:04 -08003498 tw32_wait_f(TG3PCI_CLOCK_CTRL,
3499 tp->pci_clock_ctrl | newbits3, 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003500 }
3501 }
3502
Joe Perches63c3a662011-04-26 08:12:10 +00003503 if (!(device_should_wake) && !tg3_flag(tp, ENABLE_ASF))
Matt Carlson0a459aa2008-11-03 16:54:15 -08003504 tg3_power_down_phy(tp, do_low_power);
Michael Chan6921d202005-12-13 21:15:53 -08003505
Matt Carlsoncd0d7222011-07-13 09:27:33 +00003506 tg3_frob_aux_power(tp, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003507
3508 /* Workaround for unstable PLL clock */
3509 if ((GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_AX) ||
3510 (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_BX)) {
3511 u32 val = tr32(0x7d00);
3512
3513 val &= ~((1 << 16) | (1 << 4) | (1 << 2) | (1 << 1) | 1);
3514 tw32(0x7d00, val);
Joe Perches63c3a662011-04-26 08:12:10 +00003515 if (!tg3_flag(tp, ENABLE_ASF)) {
Michael Chanec41c7d2006-01-17 02:40:55 -08003516 int err;
3517
3518 err = tg3_nvram_lock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003519 tg3_halt_cpu(tp, RX_CPU_BASE);
Michael Chanec41c7d2006-01-17 02:40:55 -08003520 if (!err)
3521 tg3_nvram_unlock(tp);
Michael Chan6921d202005-12-13 21:15:53 -08003522 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003523 }
3524
Michael Chanbbadf502006-04-06 21:46:34 -07003525 tg3_write_sig_post_reset(tp, RESET_KIND_SHUTDOWN);
3526
Linus Torvalds1da177e2005-04-16 15:20:36 -07003527 return 0;
3528}
3529
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003530static void tg3_power_down(struct tg3 *tp)
3531{
3532 tg3_power_down_prepare(tp);
3533
Joe Perches63c3a662011-04-26 08:12:10 +00003534 pci_wake_from_d3(tp->pdev, tg3_flag(tp, WOL_ENABLE));
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003535 pci_set_power_state(tp->pdev, PCI_D3hot);
3536}
3537
Linus Torvalds1da177e2005-04-16 15:20:36 -07003538static void tg3_aux_stat_to_speed_duplex(struct tg3 *tp, u32 val, u16 *speed, u8 *duplex)
3539{
3540 switch (val & MII_TG3_AUX_STAT_SPDMASK) {
3541 case MII_TG3_AUX_STAT_10HALF:
3542 *speed = SPEED_10;
3543 *duplex = DUPLEX_HALF;
3544 break;
3545
3546 case MII_TG3_AUX_STAT_10FULL:
3547 *speed = SPEED_10;
3548 *duplex = DUPLEX_FULL;
3549 break;
3550
3551 case MII_TG3_AUX_STAT_100HALF:
3552 *speed = SPEED_100;
3553 *duplex = DUPLEX_HALF;
3554 break;
3555
3556 case MII_TG3_AUX_STAT_100FULL:
3557 *speed = SPEED_100;
3558 *duplex = DUPLEX_FULL;
3559 break;
3560
3561 case MII_TG3_AUX_STAT_1000HALF:
3562 *speed = SPEED_1000;
3563 *duplex = DUPLEX_HALF;
3564 break;
3565
3566 case MII_TG3_AUX_STAT_1000FULL:
3567 *speed = SPEED_1000;
3568 *duplex = DUPLEX_FULL;
3569 break;
3570
3571 default:
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003572 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Michael Chan715116a2006-09-27 16:09:25 -07003573 *speed = (val & MII_TG3_AUX_STAT_100) ? SPEED_100 :
3574 SPEED_10;
3575 *duplex = (val & MII_TG3_AUX_STAT_FULL) ? DUPLEX_FULL :
3576 DUPLEX_HALF;
3577 break;
3578 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003579 *speed = SPEED_INVALID;
3580 *duplex = DUPLEX_INVALID;
3581 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07003582 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003583}
3584
Matt Carlson42b64a42011-05-19 12:12:49 +00003585static int tg3_phy_autoneg_cfg(struct tg3 *tp, u32 advertise, u32 flowctrl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003586{
Matt Carlson42b64a42011-05-19 12:12:49 +00003587 int err = 0;
3588 u32 val, new_adv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003589
Matt Carlson42b64a42011-05-19 12:12:49 +00003590 new_adv = ADVERTISE_CSMA;
Hiroaki SHIMODA202ff1c2011-11-22 04:05:41 +00003591 new_adv |= ethtool_adv_to_mii_adv_t(advertise) & ADVERTISE_ALL;
Matt Carlson42b64a42011-05-19 12:12:49 +00003592 new_adv |= tg3_advert_flowctrl_1000T(flowctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003593
Matt Carlson42b64a42011-05-19 12:12:49 +00003594 err = tg3_writephy(tp, MII_ADVERTISE, new_adv);
3595 if (err)
3596 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003597
Matt Carlson42b64a42011-05-19 12:12:49 +00003598 if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
3599 goto done;
Matt Carlsonba4d07a2007-12-20 20:08:00 -08003600
Matt Carlson37f07022011-11-17 14:30:55 +00003601 new_adv = ethtool_adv_to_mii_ctrl1000_t(advertise);
Matt Carlsonba4d07a2007-12-20 20:08:00 -08003602
Matt Carlson42b64a42011-05-19 12:12:49 +00003603 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
3604 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0)
Matt Carlson221c5632011-06-13 13:39:01 +00003605 new_adv |= CTL1000_AS_MASTER | CTL1000_ENABLE_MASTER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003606
Matt Carlson221c5632011-06-13 13:39:01 +00003607 err = tg3_writephy(tp, MII_CTRL1000, new_adv);
Matt Carlson42b64a42011-05-19 12:12:49 +00003608 if (err)
3609 goto done;
Matt Carlsonba4d07a2007-12-20 20:08:00 -08003610
Matt Carlson42b64a42011-05-19 12:12:49 +00003611 if (!(tp->phy_flags & TG3_PHYFLG_EEE_CAP))
3612 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003613
Matt Carlson42b64a42011-05-19 12:12:49 +00003614 tw32(TG3_CPMU_EEE_MODE,
3615 tr32(TG3_CPMU_EEE_MODE) & ~TG3_CPMU_EEEMD_LPI_ENABLE);
Matt Carlsonba4d07a2007-12-20 20:08:00 -08003616
Matt Carlson42b64a42011-05-19 12:12:49 +00003617 err = TG3_PHY_AUXCTL_SMDSP_ENABLE(tp);
3618 if (!err) {
3619 u32 err2;
Matt Carlson52b02d02010-10-14 10:37:41 +00003620
Matt Carlsona6b68da2010-12-06 08:28:52 +00003621 val = 0;
Matt Carlson42b64a42011-05-19 12:12:49 +00003622 /* Advertise 100-BaseTX EEE ability */
3623 if (advertise & ADVERTISED_100baseT_Full)
3624 val |= MDIO_AN_EEE_ADV_100TX;
3625 /* Advertise 1000-BaseT EEE ability */
3626 if (advertise & ADVERTISED_1000baseT_Full)
3627 val |= MDIO_AN_EEE_ADV_1000T;
3628 err = tg3_phy_cl45_write(tp, MDIO_MMD_AN, MDIO_AN_EEE_ADV, val);
Matt Carlsonb715ce92011-07-20 10:20:52 +00003629 if (err)
3630 val = 0;
3631
3632 switch (GET_ASIC_REV(tp->pci_chip_rev_id)) {
3633 case ASIC_REV_5717:
3634 case ASIC_REV_57765:
3635 case ASIC_REV_5719:
3636 /* If we advertised any eee advertisements above... */
3637 if (val)
3638 val = MII_TG3_DSP_TAP26_ALNOKO |
3639 MII_TG3_DSP_TAP26_RMRXSTO |
3640 MII_TG3_DSP_TAP26_OPCSINPT;
3641 tg3_phydsp_write(tp, MII_TG3_DSP_TAP26, val);
3642 /* Fall through */
3643 case ASIC_REV_5720:
3644 if (!tg3_phydsp_read(tp, MII_TG3_DSP_CH34TP2, &val))
3645 tg3_phydsp_write(tp, MII_TG3_DSP_CH34TP2, val |
3646 MII_TG3_DSP_CH34TP2_HIBW01);
3647 }
Matt Carlson52b02d02010-10-14 10:37:41 +00003648
Matt Carlson42b64a42011-05-19 12:12:49 +00003649 err2 = TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
3650 if (!err)
3651 err = err2;
3652 }
3653
3654done:
3655 return err;
3656}
3657
3658static void tg3_phy_copper_begin(struct tg3 *tp)
3659{
3660 u32 new_adv;
3661 int i;
3662
3663 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) {
3664 new_adv = ADVERTISED_10baseT_Half |
3665 ADVERTISED_10baseT_Full;
3666 if (tg3_flag(tp, WOL_SPEED_100MB))
3667 new_adv |= ADVERTISED_100baseT_Half |
3668 ADVERTISED_100baseT_Full;
3669
3670 tg3_phy_autoneg_cfg(tp, new_adv,
3671 FLOW_CTRL_TX | FLOW_CTRL_RX);
3672 } else if (tp->link_config.speed == SPEED_INVALID) {
3673 if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
3674 tp->link_config.advertising &=
3675 ~(ADVERTISED_1000baseT_Half |
3676 ADVERTISED_1000baseT_Full);
3677
3678 tg3_phy_autoneg_cfg(tp, tp->link_config.advertising,
3679 tp->link_config.flowctrl);
3680 } else {
3681 /* Asking for a specific link mode. */
3682 if (tp->link_config.speed == SPEED_1000) {
3683 if (tp->link_config.duplex == DUPLEX_FULL)
3684 new_adv = ADVERTISED_1000baseT_Full;
3685 else
3686 new_adv = ADVERTISED_1000baseT_Half;
3687 } else if (tp->link_config.speed == SPEED_100) {
3688 if (tp->link_config.duplex == DUPLEX_FULL)
3689 new_adv = ADVERTISED_100baseT_Full;
3690 else
3691 new_adv = ADVERTISED_100baseT_Half;
3692 } else {
3693 if (tp->link_config.duplex == DUPLEX_FULL)
3694 new_adv = ADVERTISED_10baseT_Full;
3695 else
3696 new_adv = ADVERTISED_10baseT_Half;
3697 }
3698
3699 tg3_phy_autoneg_cfg(tp, new_adv,
3700 tp->link_config.flowctrl);
Matt Carlson52b02d02010-10-14 10:37:41 +00003701 }
3702
Linus Torvalds1da177e2005-04-16 15:20:36 -07003703 if (tp->link_config.autoneg == AUTONEG_DISABLE &&
3704 tp->link_config.speed != SPEED_INVALID) {
3705 u32 bmcr, orig_bmcr;
3706
3707 tp->link_config.active_speed = tp->link_config.speed;
3708 tp->link_config.active_duplex = tp->link_config.duplex;
3709
3710 bmcr = 0;
3711 switch (tp->link_config.speed) {
3712 default:
3713 case SPEED_10:
3714 break;
3715
3716 case SPEED_100:
3717 bmcr |= BMCR_SPEED100;
3718 break;
3719
3720 case SPEED_1000:
Matt Carlson221c5632011-06-13 13:39:01 +00003721 bmcr |= BMCR_SPEED1000;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003722 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07003723 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003724
3725 if (tp->link_config.duplex == DUPLEX_FULL)
3726 bmcr |= BMCR_FULLDPLX;
3727
3728 if (!tg3_readphy(tp, MII_BMCR, &orig_bmcr) &&
3729 (bmcr != orig_bmcr)) {
3730 tg3_writephy(tp, MII_BMCR, BMCR_LOOPBACK);
3731 for (i = 0; i < 1500; i++) {
3732 u32 tmp;
3733
3734 udelay(10);
3735 if (tg3_readphy(tp, MII_BMSR, &tmp) ||
3736 tg3_readphy(tp, MII_BMSR, &tmp))
3737 continue;
3738 if (!(tmp & BMSR_LSTATUS)) {
3739 udelay(40);
3740 break;
3741 }
3742 }
3743 tg3_writephy(tp, MII_BMCR, bmcr);
3744 udelay(40);
3745 }
3746 } else {
3747 tg3_writephy(tp, MII_BMCR,
3748 BMCR_ANENABLE | BMCR_ANRESTART);
3749 }
3750}
3751
3752static int tg3_init_5401phy_dsp(struct tg3 *tp)
3753{
3754 int err;
3755
3756 /* Turn off tap power management. */
3757 /* Set Extended packet length bit */
Matt Carlsonb4bd2922011-04-20 07:57:41 +00003758 err = tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_AUXCTL, 0x4c20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003759
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00003760 err |= tg3_phydsp_write(tp, 0x0012, 0x1804);
3761 err |= tg3_phydsp_write(tp, 0x0013, 0x1204);
3762 err |= tg3_phydsp_write(tp, 0x8006, 0x0132);
3763 err |= tg3_phydsp_write(tp, 0x8006, 0x0232);
3764 err |= tg3_phydsp_write(tp, 0x201f, 0x0a20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003765
3766 udelay(40);
3767
3768 return err;
3769}
3770
Matt Carlsone2bf73e2011-12-08 14:40:15 +00003771static bool tg3_phy_copper_an_config_ok(struct tg3 *tp, u32 *lcladv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003772{
Matt Carlsone2bf73e2011-12-08 14:40:15 +00003773 u32 advmsk, tgtadv, advertising;
Michael Chan3600d912006-12-07 00:21:48 -08003774
Matt Carlsone2bf73e2011-12-08 14:40:15 +00003775 advertising = tp->link_config.advertising;
3776 tgtadv = ethtool_adv_to_mii_adv_t(advertising) & ADVERTISE_ALL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003777
Matt Carlsone2bf73e2011-12-08 14:40:15 +00003778 advmsk = ADVERTISE_ALL;
3779 if (tp->link_config.active_duplex == DUPLEX_FULL) {
3780 tgtadv |= tg3_advert_flowctrl_1000T(tp->link_config.flowctrl);
3781 advmsk |= ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
3782 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003783
Matt Carlsone2bf73e2011-12-08 14:40:15 +00003784 if (tg3_readphy(tp, MII_ADVERTISE, lcladv))
3785 return false;
3786
3787 if ((*lcladv & advmsk) != tgtadv)
3788 return false;
Matt Carlsonb99d2a52011-08-31 11:44:47 +00003789
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003790 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003791 u32 tg3_ctrl;
3792
Matt Carlsone2bf73e2011-12-08 14:40:15 +00003793 tgtadv = ethtool_adv_to_mii_ctrl1000_t(advertising);
Michael Chan3600d912006-12-07 00:21:48 -08003794
Matt Carlson221c5632011-06-13 13:39:01 +00003795 if (tg3_readphy(tp, MII_CTRL1000, &tg3_ctrl))
Matt Carlsone2bf73e2011-12-08 14:40:15 +00003796 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003797
Matt Carlsonb99d2a52011-08-31 11:44:47 +00003798 tg3_ctrl &= (ADVERTISE_1000HALF | ADVERTISE_1000FULL);
Matt Carlsone2bf73e2011-12-08 14:40:15 +00003799 if (tg3_ctrl != tgtadv)
3800 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003801 }
Matt Carlson93a700a2011-08-31 11:44:54 +00003802
Matt Carlsone2bf73e2011-12-08 14:40:15 +00003803 return true;
Matt Carlsonef167e22007-12-20 20:10:01 -08003804}
3805
Linus Torvalds1da177e2005-04-16 15:20:36 -07003806static int tg3_setup_copper_phy(struct tg3 *tp, int force_reset)
3807{
3808 int current_link_up;
Matt Carlsonf833c4c2010-09-15 09:00:01 +00003809 u32 bmsr, val;
Matt Carlsonef167e22007-12-20 20:10:01 -08003810 u32 lcl_adv, rmt_adv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003811 u16 current_speed;
3812 u8 current_duplex;
3813 int i, err;
3814
3815 tw32(MAC_EVENT, 0);
3816
3817 tw32_f(MAC_STATUS,
3818 (MAC_STATUS_SYNC_CHANGED |
3819 MAC_STATUS_CFG_CHANGED |
3820 MAC_STATUS_MI_COMPLETION |
3821 MAC_STATUS_LNKSTATE_CHANGED));
3822 udelay(40);
3823
Matt Carlson8ef21422008-05-02 16:47:53 -07003824 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
3825 tw32_f(MAC_MI_MODE,
3826 (tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL));
3827 udelay(80);
3828 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003829
Matt Carlsonb4bd2922011-04-20 07:57:41 +00003830 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_PWRCTL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003831
3832 /* Some third-party PHYs need to be reset on link going
3833 * down.
3834 */
3835 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
3836 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
3837 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) &&
3838 netif_carrier_ok(tp->dev)) {
3839 tg3_readphy(tp, MII_BMSR, &bmsr);
3840 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
3841 !(bmsr & BMSR_LSTATUS))
3842 force_reset = 1;
3843 }
3844 if (force_reset)
3845 tg3_phy_reset(tp);
3846
Matt Carlson79eb6902010-02-17 15:17:03 +00003847 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003848 tg3_readphy(tp, MII_BMSR, &bmsr);
3849 if (tg3_readphy(tp, MII_BMSR, &bmsr) ||
Joe Perches63c3a662011-04-26 08:12:10 +00003850 !tg3_flag(tp, INIT_COMPLETE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003851 bmsr = 0;
3852
3853 if (!(bmsr & BMSR_LSTATUS)) {
3854 err = tg3_init_5401phy_dsp(tp);
3855 if (err)
3856 return err;
3857
3858 tg3_readphy(tp, MII_BMSR, &bmsr);
3859 for (i = 0; i < 1000; i++) {
3860 udelay(10);
3861 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
3862 (bmsr & BMSR_LSTATUS)) {
3863 udelay(40);
3864 break;
3865 }
3866 }
3867
Matt Carlson79eb6902010-02-17 15:17:03 +00003868 if ((tp->phy_id & TG3_PHY_ID_REV_MASK) ==
3869 TG3_PHY_REV_BCM5401_B0 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003870 !(bmsr & BMSR_LSTATUS) &&
3871 tp->link_config.active_speed == SPEED_1000) {
3872 err = tg3_phy_reset(tp);
3873 if (!err)
3874 err = tg3_init_5401phy_dsp(tp);
3875 if (err)
3876 return err;
3877 }
3878 }
3879 } else if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
3880 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0) {
3881 /* 5701 {A0,B0} CRC bug workaround */
3882 tg3_writephy(tp, 0x15, 0x0a75);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00003883 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8c68);
3884 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8d68);
3885 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8c68);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003886 }
3887
3888 /* Clear pending interrupts... */
Matt Carlsonf833c4c2010-09-15 09:00:01 +00003889 tg3_readphy(tp, MII_TG3_ISTAT, &val);
3890 tg3_readphy(tp, MII_TG3_ISTAT, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003891
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003892 if (tp->phy_flags & TG3_PHYFLG_USE_MI_INTERRUPT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003893 tg3_writephy(tp, MII_TG3_IMASK, ~MII_TG3_INT_LINKCHG);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003894 else if (!(tp->phy_flags & TG3_PHYFLG_IS_FET))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003895 tg3_writephy(tp, MII_TG3_IMASK, ~0);
3896
3897 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
3898 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
3899 if (tp->led_ctrl == LED_CTRL_MODE_PHY_1)
3900 tg3_writephy(tp, MII_TG3_EXT_CTRL,
3901 MII_TG3_EXT_CTRL_LNK3_LED_MODE);
3902 else
3903 tg3_writephy(tp, MII_TG3_EXT_CTRL, 0);
3904 }
3905
3906 current_link_up = 0;
3907 current_speed = SPEED_INVALID;
3908 current_duplex = DUPLEX_INVALID;
Matt Carlsone348c5e2011-11-21 15:01:20 +00003909 tp->phy_flags &= ~TG3_PHYFLG_MDIX_STATE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003910
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003911 if (tp->phy_flags & TG3_PHYFLG_CAPACITIVE_COUPLING) {
Matt Carlson15ee95c2011-04-20 07:57:40 +00003912 err = tg3_phy_auxctl_read(tp,
3913 MII_TG3_AUXCTL_SHDWSEL_MISCTEST,
3914 &val);
3915 if (!err && !(val & (1 << 10))) {
Matt Carlsonb4bd2922011-04-20 07:57:41 +00003916 tg3_phy_auxctl_write(tp,
3917 MII_TG3_AUXCTL_SHDWSEL_MISCTEST,
3918 val | (1 << 10));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003919 goto relink;
3920 }
3921 }
3922
3923 bmsr = 0;
3924 for (i = 0; i < 100; i++) {
3925 tg3_readphy(tp, MII_BMSR, &bmsr);
3926 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
3927 (bmsr & BMSR_LSTATUS))
3928 break;
3929 udelay(40);
3930 }
3931
3932 if (bmsr & BMSR_LSTATUS) {
3933 u32 aux_stat, bmcr;
3934
3935 tg3_readphy(tp, MII_TG3_AUX_STAT, &aux_stat);
3936 for (i = 0; i < 2000; i++) {
3937 udelay(10);
3938 if (!tg3_readphy(tp, MII_TG3_AUX_STAT, &aux_stat) &&
3939 aux_stat)
3940 break;
3941 }
3942
3943 tg3_aux_stat_to_speed_duplex(tp, aux_stat,
3944 &current_speed,
3945 &current_duplex);
3946
3947 bmcr = 0;
3948 for (i = 0; i < 200; i++) {
3949 tg3_readphy(tp, MII_BMCR, &bmcr);
3950 if (tg3_readphy(tp, MII_BMCR, &bmcr))
3951 continue;
3952 if (bmcr && bmcr != 0x7fff)
3953 break;
3954 udelay(10);
3955 }
3956
Matt Carlsonef167e22007-12-20 20:10:01 -08003957 lcl_adv = 0;
3958 rmt_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003959
Matt Carlsonef167e22007-12-20 20:10:01 -08003960 tp->link_config.active_speed = current_speed;
3961 tp->link_config.active_duplex = current_duplex;
3962
3963 if (tp->link_config.autoneg == AUTONEG_ENABLE) {
3964 if ((bmcr & BMCR_ANENABLE) &&
Matt Carlsone2bf73e2011-12-08 14:40:15 +00003965 tg3_phy_copper_an_config_ok(tp, &lcl_adv) &&
3966 (tg3_flag(tp, PAUSE_AUTONEG) &&
3967 !tg3_readphy(tp, MII_LPA, &rmt_adv)))
3968 current_link_up = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003969 } else {
3970 if (!(bmcr & BMCR_ANENABLE) &&
3971 tp->link_config.speed == current_speed &&
Matt Carlsonef167e22007-12-20 20:10:01 -08003972 tp->link_config.duplex == current_duplex &&
3973 tp->link_config.flowctrl ==
3974 tp->link_config.active_flowctrl) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003975 current_link_up = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003976 }
3977 }
3978
Matt Carlsonef167e22007-12-20 20:10:01 -08003979 if (current_link_up == 1 &&
Matt Carlsone348c5e2011-11-21 15:01:20 +00003980 tp->link_config.active_duplex == DUPLEX_FULL) {
3981 u32 reg, bit;
3982
3983 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
3984 reg = MII_TG3_FET_GEN_STAT;
3985 bit = MII_TG3_FET_GEN_STAT_MDIXSTAT;
3986 } else {
3987 reg = MII_TG3_EXT_STAT;
3988 bit = MII_TG3_EXT_STAT_MDIX;
3989 }
3990
3991 if (!tg3_readphy(tp, reg, &val) && (val & bit))
3992 tp->phy_flags |= TG3_PHYFLG_MDIX_STATE;
3993
Matt Carlsonef167e22007-12-20 20:10:01 -08003994 tg3_setup_flow_control(tp, lcl_adv, rmt_adv);
Matt Carlsone348c5e2011-11-21 15:01:20 +00003995 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003996 }
3997
Linus Torvalds1da177e2005-04-16 15:20:36 -07003998relink:
Matt Carlson80096062010-08-02 11:26:06 +00003999 if (current_link_up == 0 || (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004000 tg3_phy_copper_begin(tp);
4001
Matt Carlsonf833c4c2010-09-15 09:00:01 +00004002 tg3_readphy(tp, MII_BMSR, &bmsr);
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00004003 if ((!tg3_readphy(tp, MII_BMSR, &bmsr) && (bmsr & BMSR_LSTATUS)) ||
4004 (tp->mac_mode & MAC_MODE_PORT_INT_LPBACK))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004005 current_link_up = 1;
4006 }
4007
4008 tp->mac_mode &= ~MAC_MODE_PORT_MODE_MASK;
4009 if (current_link_up == 1) {
4010 if (tp->link_config.active_speed == SPEED_100 ||
4011 tp->link_config.active_speed == SPEED_10)
4012 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
4013 else
4014 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004015 } else if (tp->phy_flags & TG3_PHYFLG_IS_FET)
Matt Carlson7f97a4b2009-08-25 10:10:03 +00004016 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
4017 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004018 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
4019
4020 tp->mac_mode &= ~MAC_MODE_HALF_DUPLEX;
4021 if (tp->link_config.active_duplex == DUPLEX_HALF)
4022 tp->mac_mode |= MAC_MODE_HALF_DUPLEX;
4023
Linus Torvalds1da177e2005-04-16 15:20:36 -07004024 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700) {
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07004025 if (current_link_up == 1 &&
4026 tg3_5700_link_polarity(tp, tp->link_config.active_speed))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004027 tp->mac_mode |= MAC_MODE_LINK_POLARITY;
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07004028 else
4029 tp->mac_mode &= ~MAC_MODE_LINK_POLARITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004030 }
4031
4032 /* ??? Without this setting Netgear GA302T PHY does not
4033 * ??? send/receive packets...
4034 */
Matt Carlson79eb6902010-02-17 15:17:03 +00004035 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5411 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004036 tp->pci_chip_rev_id == CHIPREV_ID_5700_ALTIMA) {
4037 tp->mi_mode |= MAC_MI_MODE_AUTO_POLL;
4038 tw32_f(MAC_MI_MODE, tp->mi_mode);
4039 udelay(80);
4040 }
4041
4042 tw32_f(MAC_MODE, tp->mac_mode);
4043 udelay(40);
4044
Matt Carlson52b02d02010-10-14 10:37:41 +00004045 tg3_phy_eee_adjust(tp, current_link_up);
4046
Joe Perches63c3a662011-04-26 08:12:10 +00004047 if (tg3_flag(tp, USE_LINKCHG_REG)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004048 /* Polled via timer. */
4049 tw32_f(MAC_EVENT, 0);
4050 } else {
4051 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
4052 }
4053 udelay(40);
4054
4055 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 &&
4056 current_link_up == 1 &&
4057 tp->link_config.active_speed == SPEED_1000 &&
Joe Perches63c3a662011-04-26 08:12:10 +00004058 (tg3_flag(tp, PCIX_MODE) || tg3_flag(tp, PCI_HIGH_SPEED))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004059 udelay(120);
4060 tw32_f(MAC_STATUS,
4061 (MAC_STATUS_SYNC_CHANGED |
4062 MAC_STATUS_CFG_CHANGED));
4063 udelay(40);
4064 tg3_write_mem(tp,
4065 NIC_SRAM_FIRMWARE_MBOX,
4066 NIC_SRAM_FIRMWARE_MBOX_MAGIC2);
4067 }
4068
Matt Carlson5e7dfd02008-11-21 17:18:16 -08004069 /* Prevent send BD corruption. */
Joe Perches63c3a662011-04-26 08:12:10 +00004070 if (tg3_flag(tp, CLKREQ_BUG)) {
Matt Carlson5e7dfd02008-11-21 17:18:16 -08004071 u16 oldlnkctl, newlnkctl;
4072
4073 pci_read_config_word(tp->pdev,
Jon Mason708ebb32011-06-27 12:56:50 +00004074 pci_pcie_cap(tp->pdev) + PCI_EXP_LNKCTL,
Matt Carlson5e7dfd02008-11-21 17:18:16 -08004075 &oldlnkctl);
4076 if (tp->link_config.active_speed == SPEED_100 ||
4077 tp->link_config.active_speed == SPEED_10)
4078 newlnkctl = oldlnkctl & ~PCI_EXP_LNKCTL_CLKREQ_EN;
4079 else
4080 newlnkctl = oldlnkctl | PCI_EXP_LNKCTL_CLKREQ_EN;
4081 if (newlnkctl != oldlnkctl)
4082 pci_write_config_word(tp->pdev,
Matt Carlson93a700a2011-08-31 11:44:54 +00004083 pci_pcie_cap(tp->pdev) +
4084 PCI_EXP_LNKCTL, newlnkctl);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08004085 }
4086
Linus Torvalds1da177e2005-04-16 15:20:36 -07004087 if (current_link_up != netif_carrier_ok(tp->dev)) {
4088 if (current_link_up)
4089 netif_carrier_on(tp->dev);
4090 else
4091 netif_carrier_off(tp->dev);
4092 tg3_link_report(tp);
4093 }
4094
4095 return 0;
4096}
4097
4098struct tg3_fiber_aneginfo {
4099 int state;
4100#define ANEG_STATE_UNKNOWN 0
4101#define ANEG_STATE_AN_ENABLE 1
4102#define ANEG_STATE_RESTART_INIT 2
4103#define ANEG_STATE_RESTART 3
4104#define ANEG_STATE_DISABLE_LINK_OK 4
4105#define ANEG_STATE_ABILITY_DETECT_INIT 5
4106#define ANEG_STATE_ABILITY_DETECT 6
4107#define ANEG_STATE_ACK_DETECT_INIT 7
4108#define ANEG_STATE_ACK_DETECT 8
4109#define ANEG_STATE_COMPLETE_ACK_INIT 9
4110#define ANEG_STATE_COMPLETE_ACK 10
4111#define ANEG_STATE_IDLE_DETECT_INIT 11
4112#define ANEG_STATE_IDLE_DETECT 12
4113#define ANEG_STATE_LINK_OK 13
4114#define ANEG_STATE_NEXT_PAGE_WAIT_INIT 14
4115#define ANEG_STATE_NEXT_PAGE_WAIT 15
4116
4117 u32 flags;
4118#define MR_AN_ENABLE 0x00000001
4119#define MR_RESTART_AN 0x00000002
4120#define MR_AN_COMPLETE 0x00000004
4121#define MR_PAGE_RX 0x00000008
4122#define MR_NP_LOADED 0x00000010
4123#define MR_TOGGLE_TX 0x00000020
4124#define MR_LP_ADV_FULL_DUPLEX 0x00000040
4125#define MR_LP_ADV_HALF_DUPLEX 0x00000080
4126#define MR_LP_ADV_SYM_PAUSE 0x00000100
4127#define MR_LP_ADV_ASYM_PAUSE 0x00000200
4128#define MR_LP_ADV_REMOTE_FAULT1 0x00000400
4129#define MR_LP_ADV_REMOTE_FAULT2 0x00000800
4130#define MR_LP_ADV_NEXT_PAGE 0x00001000
4131#define MR_TOGGLE_RX 0x00002000
4132#define MR_NP_RX 0x00004000
4133
4134#define MR_LINK_OK 0x80000000
4135
4136 unsigned long link_time, cur_time;
4137
4138 u32 ability_match_cfg;
4139 int ability_match_count;
4140
4141 char ability_match, idle_match, ack_match;
4142
4143 u32 txconfig, rxconfig;
4144#define ANEG_CFG_NP 0x00000080
4145#define ANEG_CFG_ACK 0x00000040
4146#define ANEG_CFG_RF2 0x00000020
4147#define ANEG_CFG_RF1 0x00000010
4148#define ANEG_CFG_PS2 0x00000001
4149#define ANEG_CFG_PS1 0x00008000
4150#define ANEG_CFG_HD 0x00004000
4151#define ANEG_CFG_FD 0x00002000
4152#define ANEG_CFG_INVAL 0x00001f06
4153
4154};
4155#define ANEG_OK 0
4156#define ANEG_DONE 1
4157#define ANEG_TIMER_ENAB 2
4158#define ANEG_FAILED -1
4159
4160#define ANEG_STATE_SETTLE_TIME 10000
4161
4162static int tg3_fiber_aneg_smachine(struct tg3 *tp,
4163 struct tg3_fiber_aneginfo *ap)
4164{
Matt Carlson5be73b42007-12-20 20:09:29 -08004165 u16 flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004166 unsigned long delta;
4167 u32 rx_cfg_reg;
4168 int ret;
4169
4170 if (ap->state == ANEG_STATE_UNKNOWN) {
4171 ap->rxconfig = 0;
4172 ap->link_time = 0;
4173 ap->cur_time = 0;
4174 ap->ability_match_cfg = 0;
4175 ap->ability_match_count = 0;
4176 ap->ability_match = 0;
4177 ap->idle_match = 0;
4178 ap->ack_match = 0;
4179 }
4180 ap->cur_time++;
4181
4182 if (tr32(MAC_STATUS) & MAC_STATUS_RCVD_CFG) {
4183 rx_cfg_reg = tr32(MAC_RX_AUTO_NEG);
4184
4185 if (rx_cfg_reg != ap->ability_match_cfg) {
4186 ap->ability_match_cfg = rx_cfg_reg;
4187 ap->ability_match = 0;
4188 ap->ability_match_count = 0;
4189 } else {
4190 if (++ap->ability_match_count > 1) {
4191 ap->ability_match = 1;
4192 ap->ability_match_cfg = rx_cfg_reg;
4193 }
4194 }
4195 if (rx_cfg_reg & ANEG_CFG_ACK)
4196 ap->ack_match = 1;
4197 else
4198 ap->ack_match = 0;
4199
4200 ap->idle_match = 0;
4201 } else {
4202 ap->idle_match = 1;
4203 ap->ability_match_cfg = 0;
4204 ap->ability_match_count = 0;
4205 ap->ability_match = 0;
4206 ap->ack_match = 0;
4207
4208 rx_cfg_reg = 0;
4209 }
4210
4211 ap->rxconfig = rx_cfg_reg;
4212 ret = ANEG_OK;
4213
Matt Carlson33f401a2010-04-05 10:19:27 +00004214 switch (ap->state) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004215 case ANEG_STATE_UNKNOWN:
4216 if (ap->flags & (MR_AN_ENABLE | MR_RESTART_AN))
4217 ap->state = ANEG_STATE_AN_ENABLE;
4218
4219 /* fallthru */
4220 case ANEG_STATE_AN_ENABLE:
4221 ap->flags &= ~(MR_AN_COMPLETE | MR_PAGE_RX);
4222 if (ap->flags & MR_AN_ENABLE) {
4223 ap->link_time = 0;
4224 ap->cur_time = 0;
4225 ap->ability_match_cfg = 0;
4226 ap->ability_match_count = 0;
4227 ap->ability_match = 0;
4228 ap->idle_match = 0;
4229 ap->ack_match = 0;
4230
4231 ap->state = ANEG_STATE_RESTART_INIT;
4232 } else {
4233 ap->state = ANEG_STATE_DISABLE_LINK_OK;
4234 }
4235 break;
4236
4237 case ANEG_STATE_RESTART_INIT:
4238 ap->link_time = ap->cur_time;
4239 ap->flags &= ~(MR_NP_LOADED);
4240 ap->txconfig = 0;
4241 tw32(MAC_TX_AUTO_NEG, 0);
4242 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
4243 tw32_f(MAC_MODE, tp->mac_mode);
4244 udelay(40);
4245
4246 ret = ANEG_TIMER_ENAB;
4247 ap->state = ANEG_STATE_RESTART;
4248
4249 /* fallthru */
4250 case ANEG_STATE_RESTART:
4251 delta = ap->cur_time - ap->link_time;
Matt Carlson859a5882010-04-05 10:19:28 +00004252 if (delta > ANEG_STATE_SETTLE_TIME)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004253 ap->state = ANEG_STATE_ABILITY_DETECT_INIT;
Matt Carlson859a5882010-04-05 10:19:28 +00004254 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004255 ret = ANEG_TIMER_ENAB;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004256 break;
4257
4258 case ANEG_STATE_DISABLE_LINK_OK:
4259 ret = ANEG_DONE;
4260 break;
4261
4262 case ANEG_STATE_ABILITY_DETECT_INIT:
4263 ap->flags &= ~(MR_TOGGLE_TX);
Matt Carlson5be73b42007-12-20 20:09:29 -08004264 ap->txconfig = ANEG_CFG_FD;
4265 flowctrl = tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
4266 if (flowctrl & ADVERTISE_1000XPAUSE)
4267 ap->txconfig |= ANEG_CFG_PS1;
4268 if (flowctrl & ADVERTISE_1000XPSE_ASYM)
4269 ap->txconfig |= ANEG_CFG_PS2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004270 tw32(MAC_TX_AUTO_NEG, ap->txconfig);
4271 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
4272 tw32_f(MAC_MODE, tp->mac_mode);
4273 udelay(40);
4274
4275 ap->state = ANEG_STATE_ABILITY_DETECT;
4276 break;
4277
4278 case ANEG_STATE_ABILITY_DETECT:
Matt Carlson859a5882010-04-05 10:19:28 +00004279 if (ap->ability_match != 0 && ap->rxconfig != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004280 ap->state = ANEG_STATE_ACK_DETECT_INIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004281 break;
4282
4283 case ANEG_STATE_ACK_DETECT_INIT:
4284 ap->txconfig |= ANEG_CFG_ACK;
4285 tw32(MAC_TX_AUTO_NEG, ap->txconfig);
4286 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
4287 tw32_f(MAC_MODE, tp->mac_mode);
4288 udelay(40);
4289
4290 ap->state = ANEG_STATE_ACK_DETECT;
4291
4292 /* fallthru */
4293 case ANEG_STATE_ACK_DETECT:
4294 if (ap->ack_match != 0) {
4295 if ((ap->rxconfig & ~ANEG_CFG_ACK) ==
4296 (ap->ability_match_cfg & ~ANEG_CFG_ACK)) {
4297 ap->state = ANEG_STATE_COMPLETE_ACK_INIT;
4298 } else {
4299 ap->state = ANEG_STATE_AN_ENABLE;
4300 }
4301 } else if (ap->ability_match != 0 &&
4302 ap->rxconfig == 0) {
4303 ap->state = ANEG_STATE_AN_ENABLE;
4304 }
4305 break;
4306
4307 case ANEG_STATE_COMPLETE_ACK_INIT:
4308 if (ap->rxconfig & ANEG_CFG_INVAL) {
4309 ret = ANEG_FAILED;
4310 break;
4311 }
4312 ap->flags &= ~(MR_LP_ADV_FULL_DUPLEX |
4313 MR_LP_ADV_HALF_DUPLEX |
4314 MR_LP_ADV_SYM_PAUSE |
4315 MR_LP_ADV_ASYM_PAUSE |
4316 MR_LP_ADV_REMOTE_FAULT1 |
4317 MR_LP_ADV_REMOTE_FAULT2 |
4318 MR_LP_ADV_NEXT_PAGE |
4319 MR_TOGGLE_RX |
4320 MR_NP_RX);
4321 if (ap->rxconfig & ANEG_CFG_FD)
4322 ap->flags |= MR_LP_ADV_FULL_DUPLEX;
4323 if (ap->rxconfig & ANEG_CFG_HD)
4324 ap->flags |= MR_LP_ADV_HALF_DUPLEX;
4325 if (ap->rxconfig & ANEG_CFG_PS1)
4326 ap->flags |= MR_LP_ADV_SYM_PAUSE;
4327 if (ap->rxconfig & ANEG_CFG_PS2)
4328 ap->flags |= MR_LP_ADV_ASYM_PAUSE;
4329 if (ap->rxconfig & ANEG_CFG_RF1)
4330 ap->flags |= MR_LP_ADV_REMOTE_FAULT1;
4331 if (ap->rxconfig & ANEG_CFG_RF2)
4332 ap->flags |= MR_LP_ADV_REMOTE_FAULT2;
4333 if (ap->rxconfig & ANEG_CFG_NP)
4334 ap->flags |= MR_LP_ADV_NEXT_PAGE;
4335
4336 ap->link_time = ap->cur_time;
4337
4338 ap->flags ^= (MR_TOGGLE_TX);
4339 if (ap->rxconfig & 0x0008)
4340 ap->flags |= MR_TOGGLE_RX;
4341 if (ap->rxconfig & ANEG_CFG_NP)
4342 ap->flags |= MR_NP_RX;
4343 ap->flags |= MR_PAGE_RX;
4344
4345 ap->state = ANEG_STATE_COMPLETE_ACK;
4346 ret = ANEG_TIMER_ENAB;
4347 break;
4348
4349 case ANEG_STATE_COMPLETE_ACK:
4350 if (ap->ability_match != 0 &&
4351 ap->rxconfig == 0) {
4352 ap->state = ANEG_STATE_AN_ENABLE;
4353 break;
4354 }
4355 delta = ap->cur_time - ap->link_time;
4356 if (delta > ANEG_STATE_SETTLE_TIME) {
4357 if (!(ap->flags & (MR_LP_ADV_NEXT_PAGE))) {
4358 ap->state = ANEG_STATE_IDLE_DETECT_INIT;
4359 } else {
4360 if ((ap->txconfig & ANEG_CFG_NP) == 0 &&
4361 !(ap->flags & MR_NP_RX)) {
4362 ap->state = ANEG_STATE_IDLE_DETECT_INIT;
4363 } else {
4364 ret = ANEG_FAILED;
4365 }
4366 }
4367 }
4368 break;
4369
4370 case ANEG_STATE_IDLE_DETECT_INIT:
4371 ap->link_time = ap->cur_time;
4372 tp->mac_mode &= ~MAC_MODE_SEND_CONFIGS;
4373 tw32_f(MAC_MODE, tp->mac_mode);
4374 udelay(40);
4375
4376 ap->state = ANEG_STATE_IDLE_DETECT;
4377 ret = ANEG_TIMER_ENAB;
4378 break;
4379
4380 case ANEG_STATE_IDLE_DETECT:
4381 if (ap->ability_match != 0 &&
4382 ap->rxconfig == 0) {
4383 ap->state = ANEG_STATE_AN_ENABLE;
4384 break;
4385 }
4386 delta = ap->cur_time - ap->link_time;
4387 if (delta > ANEG_STATE_SETTLE_TIME) {
4388 /* XXX another gem from the Broadcom driver :( */
4389 ap->state = ANEG_STATE_LINK_OK;
4390 }
4391 break;
4392
4393 case ANEG_STATE_LINK_OK:
4394 ap->flags |= (MR_AN_COMPLETE | MR_LINK_OK);
4395 ret = ANEG_DONE;
4396 break;
4397
4398 case ANEG_STATE_NEXT_PAGE_WAIT_INIT:
4399 /* ??? unimplemented */
4400 break;
4401
4402 case ANEG_STATE_NEXT_PAGE_WAIT:
4403 /* ??? unimplemented */
4404 break;
4405
4406 default:
4407 ret = ANEG_FAILED;
4408 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07004409 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004410
4411 return ret;
4412}
4413
Matt Carlson5be73b42007-12-20 20:09:29 -08004414static int fiber_autoneg(struct tg3 *tp, u32 *txflags, u32 *rxflags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004415{
4416 int res = 0;
4417 struct tg3_fiber_aneginfo aninfo;
4418 int status = ANEG_FAILED;
4419 unsigned int tick;
4420 u32 tmp;
4421
4422 tw32_f(MAC_TX_AUTO_NEG, 0);
4423
4424 tmp = tp->mac_mode & ~MAC_MODE_PORT_MODE_MASK;
4425 tw32_f(MAC_MODE, tmp | MAC_MODE_PORT_MODE_GMII);
4426 udelay(40);
4427
4428 tw32_f(MAC_MODE, tp->mac_mode | MAC_MODE_SEND_CONFIGS);
4429 udelay(40);
4430
4431 memset(&aninfo, 0, sizeof(aninfo));
4432 aninfo.flags |= MR_AN_ENABLE;
4433 aninfo.state = ANEG_STATE_UNKNOWN;
4434 aninfo.cur_time = 0;
4435 tick = 0;
4436 while (++tick < 195000) {
4437 status = tg3_fiber_aneg_smachine(tp, &aninfo);
4438 if (status == ANEG_DONE || status == ANEG_FAILED)
4439 break;
4440
4441 udelay(1);
4442 }
4443
4444 tp->mac_mode &= ~MAC_MODE_SEND_CONFIGS;
4445 tw32_f(MAC_MODE, tp->mac_mode);
4446 udelay(40);
4447
Matt Carlson5be73b42007-12-20 20:09:29 -08004448 *txflags = aninfo.txconfig;
4449 *rxflags = aninfo.flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004450
4451 if (status == ANEG_DONE &&
4452 (aninfo.flags & (MR_AN_COMPLETE | MR_LINK_OK |
4453 MR_LP_ADV_FULL_DUPLEX)))
4454 res = 1;
4455
4456 return res;
4457}
4458
4459static void tg3_init_bcm8002(struct tg3 *tp)
4460{
4461 u32 mac_status = tr32(MAC_STATUS);
4462 int i;
4463
4464 /* Reset when initting first time or we have a link. */
Joe Perches63c3a662011-04-26 08:12:10 +00004465 if (tg3_flag(tp, INIT_COMPLETE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004466 !(mac_status & MAC_STATUS_PCS_SYNCED))
4467 return;
4468
4469 /* Set PLL lock range. */
4470 tg3_writephy(tp, 0x16, 0x8007);
4471
4472 /* SW reset */
4473 tg3_writephy(tp, MII_BMCR, BMCR_RESET);
4474
4475 /* Wait for reset to complete. */
4476 /* XXX schedule_timeout() ... */
4477 for (i = 0; i < 500; i++)
4478 udelay(10);
4479
4480 /* Config mode; select PMA/Ch 1 regs. */
4481 tg3_writephy(tp, 0x10, 0x8411);
4482
4483 /* Enable auto-lock and comdet, select txclk for tx. */
4484 tg3_writephy(tp, 0x11, 0x0a10);
4485
4486 tg3_writephy(tp, 0x18, 0x00a0);
4487 tg3_writephy(tp, 0x16, 0x41ff);
4488
4489 /* Assert and deassert POR. */
4490 tg3_writephy(tp, 0x13, 0x0400);
4491 udelay(40);
4492 tg3_writephy(tp, 0x13, 0x0000);
4493
4494 tg3_writephy(tp, 0x11, 0x0a50);
4495 udelay(40);
4496 tg3_writephy(tp, 0x11, 0x0a10);
4497
4498 /* Wait for signal to stabilize */
4499 /* XXX schedule_timeout() ... */
4500 for (i = 0; i < 15000; i++)
4501 udelay(10);
4502
4503 /* Deselect the channel register so we can read the PHYID
4504 * later.
4505 */
4506 tg3_writephy(tp, 0x10, 0x8011);
4507}
4508
4509static int tg3_setup_fiber_hw_autoneg(struct tg3 *tp, u32 mac_status)
4510{
Matt Carlson82cd3d12007-12-20 20:09:00 -08004511 u16 flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004512 u32 sg_dig_ctrl, sg_dig_status;
4513 u32 serdes_cfg, expected_sg_dig_ctrl;
4514 int workaround, port_a;
4515 int current_link_up;
4516
4517 serdes_cfg = 0;
4518 expected_sg_dig_ctrl = 0;
4519 workaround = 0;
4520 port_a = 1;
4521 current_link_up = 0;
4522
4523 if (tp->pci_chip_rev_id != CHIPREV_ID_5704_A0 &&
4524 tp->pci_chip_rev_id != CHIPREV_ID_5704_A1) {
4525 workaround = 1;
4526 if (tr32(TG3PCI_DUAL_MAC_CTRL) & DUAL_MAC_CTRL_ID)
4527 port_a = 0;
4528
4529 /* preserve bits 0-11,13,14 for signal pre-emphasis */
4530 /* preserve bits 20-23 for voltage regulator */
4531 serdes_cfg = tr32(MAC_SERDES_CFG) & 0x00f06fff;
4532 }
4533
4534 sg_dig_ctrl = tr32(SG_DIG_CTRL);
4535
4536 if (tp->link_config.autoneg != AUTONEG_ENABLE) {
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004537 if (sg_dig_ctrl & SG_DIG_USING_HW_AUTONEG) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004538 if (workaround) {
4539 u32 val = serdes_cfg;
4540
4541 if (port_a)
4542 val |= 0xc010000;
4543 else
4544 val |= 0x4010000;
4545 tw32_f(MAC_SERDES_CFG, val);
4546 }
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004547
4548 tw32_f(SG_DIG_CTRL, SG_DIG_COMMON_SETUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004549 }
4550 if (mac_status & MAC_STATUS_PCS_SYNCED) {
4551 tg3_setup_flow_control(tp, 0, 0);
4552 current_link_up = 1;
4553 }
4554 goto out;
4555 }
4556
4557 /* Want auto-negotiation. */
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004558 expected_sg_dig_ctrl = SG_DIG_USING_HW_AUTONEG | SG_DIG_COMMON_SETUP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004559
Matt Carlson82cd3d12007-12-20 20:09:00 -08004560 flowctrl = tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
4561 if (flowctrl & ADVERTISE_1000XPAUSE)
4562 expected_sg_dig_ctrl |= SG_DIG_PAUSE_CAP;
4563 if (flowctrl & ADVERTISE_1000XPSE_ASYM)
4564 expected_sg_dig_ctrl |= SG_DIG_ASYM_PAUSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004565
4566 if (sg_dig_ctrl != expected_sg_dig_ctrl) {
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004567 if ((tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT) &&
Michael Chan3d3ebe72006-09-27 15:59:15 -07004568 tp->serdes_counter &&
4569 ((mac_status & (MAC_STATUS_PCS_SYNCED |
4570 MAC_STATUS_RCVD_CFG)) ==
4571 MAC_STATUS_PCS_SYNCED)) {
4572 tp->serdes_counter--;
4573 current_link_up = 1;
4574 goto out;
4575 }
4576restart_autoneg:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004577 if (workaround)
4578 tw32_f(MAC_SERDES_CFG, serdes_cfg | 0xc011000);
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004579 tw32_f(SG_DIG_CTRL, expected_sg_dig_ctrl | SG_DIG_SOFT_RESET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004580 udelay(5);
4581 tw32_f(SG_DIG_CTRL, expected_sg_dig_ctrl);
4582
Michael Chan3d3ebe72006-09-27 15:59:15 -07004583 tp->serdes_counter = SERDES_AN_TIMEOUT_5704S;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004584 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004585 } else if (mac_status & (MAC_STATUS_PCS_SYNCED |
4586 MAC_STATUS_SIGNAL_DET)) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07004587 sg_dig_status = tr32(SG_DIG_STATUS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004588 mac_status = tr32(MAC_STATUS);
4589
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004590 if ((sg_dig_status & SG_DIG_AUTONEG_COMPLETE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004591 (mac_status & MAC_STATUS_PCS_SYNCED)) {
Matt Carlson82cd3d12007-12-20 20:09:00 -08004592 u32 local_adv = 0, remote_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004593
Matt Carlson82cd3d12007-12-20 20:09:00 -08004594 if (sg_dig_ctrl & SG_DIG_PAUSE_CAP)
4595 local_adv |= ADVERTISE_1000XPAUSE;
4596 if (sg_dig_ctrl & SG_DIG_ASYM_PAUSE)
4597 local_adv |= ADVERTISE_1000XPSE_ASYM;
4598
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004599 if (sg_dig_status & SG_DIG_PARTNER_PAUSE_CAPABLE)
Matt Carlson82cd3d12007-12-20 20:09:00 -08004600 remote_adv |= LPA_1000XPAUSE;
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004601 if (sg_dig_status & SG_DIG_PARTNER_ASYM_PAUSE)
Matt Carlson82cd3d12007-12-20 20:09:00 -08004602 remote_adv |= LPA_1000XPAUSE_ASYM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004603
4604 tg3_setup_flow_control(tp, local_adv, remote_adv);
4605 current_link_up = 1;
Michael Chan3d3ebe72006-09-27 15:59:15 -07004606 tp->serdes_counter = 0;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004607 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004608 } else if (!(sg_dig_status & SG_DIG_AUTONEG_COMPLETE)) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07004609 if (tp->serdes_counter)
4610 tp->serdes_counter--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004611 else {
4612 if (workaround) {
4613 u32 val = serdes_cfg;
4614
4615 if (port_a)
4616 val |= 0xc010000;
4617 else
4618 val |= 0x4010000;
4619
4620 tw32_f(MAC_SERDES_CFG, val);
4621 }
4622
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004623 tw32_f(SG_DIG_CTRL, SG_DIG_COMMON_SETUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004624 udelay(40);
4625
4626 /* Link parallel detection - link is up */
4627 /* only if we have PCS_SYNC and not */
4628 /* receiving config code words */
4629 mac_status = tr32(MAC_STATUS);
4630 if ((mac_status & MAC_STATUS_PCS_SYNCED) &&
4631 !(mac_status & MAC_STATUS_RCVD_CFG)) {
4632 tg3_setup_flow_control(tp, 0, 0);
4633 current_link_up = 1;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004634 tp->phy_flags |=
4635 TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan3d3ebe72006-09-27 15:59:15 -07004636 tp->serdes_counter =
4637 SERDES_PARALLEL_DET_TIMEOUT;
4638 } else
4639 goto restart_autoneg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004640 }
4641 }
Michael Chan3d3ebe72006-09-27 15:59:15 -07004642 } else {
4643 tp->serdes_counter = SERDES_AN_TIMEOUT_5704S;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004644 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004645 }
4646
4647out:
4648 return current_link_up;
4649}
4650
4651static int tg3_setup_fiber_by_hand(struct tg3 *tp, u32 mac_status)
4652{
4653 int current_link_up = 0;
4654
Michael Chan5cf64b82007-05-05 12:11:21 -07004655 if (!(mac_status & MAC_STATUS_PCS_SYNCED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004656 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004657
4658 if (tp->link_config.autoneg == AUTONEG_ENABLE) {
Matt Carlson5be73b42007-12-20 20:09:29 -08004659 u32 txflags, rxflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004660 int i;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04004661
Matt Carlson5be73b42007-12-20 20:09:29 -08004662 if (fiber_autoneg(tp, &txflags, &rxflags)) {
4663 u32 local_adv = 0, remote_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004664
Matt Carlson5be73b42007-12-20 20:09:29 -08004665 if (txflags & ANEG_CFG_PS1)
4666 local_adv |= ADVERTISE_1000XPAUSE;
4667 if (txflags & ANEG_CFG_PS2)
4668 local_adv |= ADVERTISE_1000XPSE_ASYM;
4669
4670 if (rxflags & MR_LP_ADV_SYM_PAUSE)
4671 remote_adv |= LPA_1000XPAUSE;
4672 if (rxflags & MR_LP_ADV_ASYM_PAUSE)
4673 remote_adv |= LPA_1000XPAUSE_ASYM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004674
4675 tg3_setup_flow_control(tp, local_adv, remote_adv);
4676
Linus Torvalds1da177e2005-04-16 15:20:36 -07004677 current_link_up = 1;
4678 }
4679 for (i = 0; i < 30; i++) {
4680 udelay(20);
4681 tw32_f(MAC_STATUS,
4682 (MAC_STATUS_SYNC_CHANGED |
4683 MAC_STATUS_CFG_CHANGED));
4684 udelay(40);
4685 if ((tr32(MAC_STATUS) &
4686 (MAC_STATUS_SYNC_CHANGED |
4687 MAC_STATUS_CFG_CHANGED)) == 0)
4688 break;
4689 }
4690
4691 mac_status = tr32(MAC_STATUS);
4692 if (current_link_up == 0 &&
4693 (mac_status & MAC_STATUS_PCS_SYNCED) &&
4694 !(mac_status & MAC_STATUS_RCVD_CFG))
4695 current_link_up = 1;
4696 } else {
Matt Carlson5be73b42007-12-20 20:09:29 -08004697 tg3_setup_flow_control(tp, 0, 0);
4698
Linus Torvalds1da177e2005-04-16 15:20:36 -07004699 /* Forcing 1000FD link up. */
4700 current_link_up = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004701
4702 tw32_f(MAC_MODE, (tp->mac_mode | MAC_MODE_SEND_CONFIGS));
4703 udelay(40);
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07004704
4705 tw32_f(MAC_MODE, tp->mac_mode);
4706 udelay(40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004707 }
4708
4709out:
4710 return current_link_up;
4711}
4712
4713static int tg3_setup_fiber_phy(struct tg3 *tp, int force_reset)
4714{
4715 u32 orig_pause_cfg;
4716 u16 orig_active_speed;
4717 u8 orig_active_duplex;
4718 u32 mac_status;
4719 int current_link_up;
4720 int i;
4721
Matt Carlson8d018622007-12-20 20:05:44 -08004722 orig_pause_cfg = tp->link_config.active_flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004723 orig_active_speed = tp->link_config.active_speed;
4724 orig_active_duplex = tp->link_config.active_duplex;
4725
Joe Perches63c3a662011-04-26 08:12:10 +00004726 if (!tg3_flag(tp, HW_AUTONEG) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004727 netif_carrier_ok(tp->dev) &&
Joe Perches63c3a662011-04-26 08:12:10 +00004728 tg3_flag(tp, INIT_COMPLETE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004729 mac_status = tr32(MAC_STATUS);
4730 mac_status &= (MAC_STATUS_PCS_SYNCED |
4731 MAC_STATUS_SIGNAL_DET |
4732 MAC_STATUS_CFG_CHANGED |
4733 MAC_STATUS_RCVD_CFG);
4734 if (mac_status == (MAC_STATUS_PCS_SYNCED |
4735 MAC_STATUS_SIGNAL_DET)) {
4736 tw32_f(MAC_STATUS, (MAC_STATUS_SYNC_CHANGED |
4737 MAC_STATUS_CFG_CHANGED));
4738 return 0;
4739 }
4740 }
4741
4742 tw32_f(MAC_TX_AUTO_NEG, 0);
4743
4744 tp->mac_mode &= ~(MAC_MODE_PORT_MODE_MASK | MAC_MODE_HALF_DUPLEX);
4745 tp->mac_mode |= MAC_MODE_PORT_MODE_TBI;
4746 tw32_f(MAC_MODE, tp->mac_mode);
4747 udelay(40);
4748
Matt Carlson79eb6902010-02-17 15:17:03 +00004749 if (tp->phy_id == TG3_PHY_ID_BCM8002)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004750 tg3_init_bcm8002(tp);
4751
4752 /* Enable link change event even when serdes polling. */
4753 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
4754 udelay(40);
4755
4756 current_link_up = 0;
4757 mac_status = tr32(MAC_STATUS);
4758
Joe Perches63c3a662011-04-26 08:12:10 +00004759 if (tg3_flag(tp, HW_AUTONEG))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004760 current_link_up = tg3_setup_fiber_hw_autoneg(tp, mac_status);
4761 else
4762 current_link_up = tg3_setup_fiber_by_hand(tp, mac_status);
4763
Matt Carlson898a56f2009-08-28 14:02:40 +00004764 tp->napi[0].hw_status->status =
Linus Torvalds1da177e2005-04-16 15:20:36 -07004765 (SD_STATUS_UPDATED |
Matt Carlson898a56f2009-08-28 14:02:40 +00004766 (tp->napi[0].hw_status->status & ~SD_STATUS_LINK_CHG));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004767
4768 for (i = 0; i < 100; i++) {
4769 tw32_f(MAC_STATUS, (MAC_STATUS_SYNC_CHANGED |
4770 MAC_STATUS_CFG_CHANGED));
4771 udelay(5);
4772 if ((tr32(MAC_STATUS) & (MAC_STATUS_SYNC_CHANGED |
Michael Chan3d3ebe72006-09-27 15:59:15 -07004773 MAC_STATUS_CFG_CHANGED |
4774 MAC_STATUS_LNKSTATE_CHANGED)) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004775 break;
4776 }
4777
4778 mac_status = tr32(MAC_STATUS);
4779 if ((mac_status & MAC_STATUS_PCS_SYNCED) == 0) {
4780 current_link_up = 0;
Michael Chan3d3ebe72006-09-27 15:59:15 -07004781 if (tp->link_config.autoneg == AUTONEG_ENABLE &&
4782 tp->serdes_counter == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004783 tw32_f(MAC_MODE, (tp->mac_mode |
4784 MAC_MODE_SEND_CONFIGS));
4785 udelay(1);
4786 tw32_f(MAC_MODE, tp->mac_mode);
4787 }
4788 }
4789
4790 if (current_link_up == 1) {
4791 tp->link_config.active_speed = SPEED_1000;
4792 tp->link_config.active_duplex = DUPLEX_FULL;
4793 tw32(MAC_LED_CTRL, (tp->led_ctrl |
4794 LED_CTRL_LNKLED_OVERRIDE |
4795 LED_CTRL_1000MBPS_ON));
4796 } else {
4797 tp->link_config.active_speed = SPEED_INVALID;
4798 tp->link_config.active_duplex = DUPLEX_INVALID;
4799 tw32(MAC_LED_CTRL, (tp->led_ctrl |
4800 LED_CTRL_LNKLED_OVERRIDE |
4801 LED_CTRL_TRAFFIC_OVERRIDE));
4802 }
4803
4804 if (current_link_up != netif_carrier_ok(tp->dev)) {
4805 if (current_link_up)
4806 netif_carrier_on(tp->dev);
4807 else
4808 netif_carrier_off(tp->dev);
4809 tg3_link_report(tp);
4810 } else {
Matt Carlson8d018622007-12-20 20:05:44 -08004811 u32 now_pause_cfg = tp->link_config.active_flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004812 if (orig_pause_cfg != now_pause_cfg ||
4813 orig_active_speed != tp->link_config.active_speed ||
4814 orig_active_duplex != tp->link_config.active_duplex)
4815 tg3_link_report(tp);
4816 }
4817
4818 return 0;
4819}
4820
Michael Chan747e8f82005-07-25 12:33:22 -07004821static int tg3_setup_fiber_mii_phy(struct tg3 *tp, int force_reset)
4822{
4823 int current_link_up, err = 0;
4824 u32 bmsr, bmcr;
4825 u16 current_speed;
4826 u8 current_duplex;
Matt Carlsonef167e22007-12-20 20:10:01 -08004827 u32 local_adv, remote_adv;
Michael Chan747e8f82005-07-25 12:33:22 -07004828
4829 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
4830 tw32_f(MAC_MODE, tp->mac_mode);
4831 udelay(40);
4832
4833 tw32(MAC_EVENT, 0);
4834
4835 tw32_f(MAC_STATUS,
4836 (MAC_STATUS_SYNC_CHANGED |
4837 MAC_STATUS_CFG_CHANGED |
4838 MAC_STATUS_MI_COMPLETION |
4839 MAC_STATUS_LNKSTATE_CHANGED));
4840 udelay(40);
4841
4842 if (force_reset)
4843 tg3_phy_reset(tp);
4844
4845 current_link_up = 0;
4846 current_speed = SPEED_INVALID;
4847 current_duplex = DUPLEX_INVALID;
4848
4849 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
4850 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
Michael Chand4d2c552006-03-20 17:47:20 -08004851 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
4852 if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
4853 bmsr |= BMSR_LSTATUS;
4854 else
4855 bmsr &= ~BMSR_LSTATUS;
4856 }
Michael Chan747e8f82005-07-25 12:33:22 -07004857
4858 err |= tg3_readphy(tp, MII_BMCR, &bmcr);
4859
4860 if ((tp->link_config.autoneg == AUTONEG_ENABLE) && !force_reset &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004861 (tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT)) {
Michael Chan747e8f82005-07-25 12:33:22 -07004862 /* do nothing, just check for link up at the end */
4863 } else if (tp->link_config.autoneg == AUTONEG_ENABLE) {
Matt Carlson28011cf2011-11-16 18:36:59 -05004864 u32 adv, newadv;
Michael Chan747e8f82005-07-25 12:33:22 -07004865
4866 err |= tg3_readphy(tp, MII_ADVERTISE, &adv);
Matt Carlson28011cf2011-11-16 18:36:59 -05004867 newadv = adv & ~(ADVERTISE_1000XFULL | ADVERTISE_1000XHALF |
4868 ADVERTISE_1000XPAUSE |
4869 ADVERTISE_1000XPSE_ASYM |
4870 ADVERTISE_SLCT);
Michael Chan747e8f82005-07-25 12:33:22 -07004871
Matt Carlson28011cf2011-11-16 18:36:59 -05004872 newadv |= tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
Matt Carlson37f07022011-11-17 14:30:55 +00004873 newadv |= ethtool_adv_to_mii_adv_x(tp->link_config.advertising);
Michael Chan747e8f82005-07-25 12:33:22 -07004874
Matt Carlson28011cf2011-11-16 18:36:59 -05004875 if ((newadv != adv) || !(bmcr & BMCR_ANENABLE)) {
4876 tg3_writephy(tp, MII_ADVERTISE, newadv);
Michael Chan747e8f82005-07-25 12:33:22 -07004877 bmcr |= BMCR_ANENABLE | BMCR_ANRESTART;
4878 tg3_writephy(tp, MII_BMCR, bmcr);
4879
4880 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
Michael Chan3d3ebe72006-09-27 15:59:15 -07004881 tp->serdes_counter = SERDES_AN_TIMEOUT_5714S;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004882 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07004883
4884 return err;
4885 }
4886 } else {
4887 u32 new_bmcr;
4888
4889 bmcr &= ~BMCR_SPEED1000;
4890 new_bmcr = bmcr & ~(BMCR_ANENABLE | BMCR_FULLDPLX);
4891
4892 if (tp->link_config.duplex == DUPLEX_FULL)
4893 new_bmcr |= BMCR_FULLDPLX;
4894
4895 if (new_bmcr != bmcr) {
4896 /* BMCR_SPEED1000 is a reserved bit that needs
4897 * to be set on write.
4898 */
4899 new_bmcr |= BMCR_SPEED1000;
4900
4901 /* Force a linkdown */
4902 if (netif_carrier_ok(tp->dev)) {
4903 u32 adv;
4904
4905 err |= tg3_readphy(tp, MII_ADVERTISE, &adv);
4906 adv &= ~(ADVERTISE_1000XFULL |
4907 ADVERTISE_1000XHALF |
4908 ADVERTISE_SLCT);
4909 tg3_writephy(tp, MII_ADVERTISE, adv);
4910 tg3_writephy(tp, MII_BMCR, bmcr |
4911 BMCR_ANRESTART |
4912 BMCR_ANENABLE);
4913 udelay(10);
4914 netif_carrier_off(tp->dev);
4915 }
4916 tg3_writephy(tp, MII_BMCR, new_bmcr);
4917 bmcr = new_bmcr;
4918 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
4919 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
Michael Chand4d2c552006-03-20 17:47:20 -08004920 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
4921 ASIC_REV_5714) {
4922 if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
4923 bmsr |= BMSR_LSTATUS;
4924 else
4925 bmsr &= ~BMSR_LSTATUS;
4926 }
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004927 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07004928 }
4929 }
4930
4931 if (bmsr & BMSR_LSTATUS) {
4932 current_speed = SPEED_1000;
4933 current_link_up = 1;
4934 if (bmcr & BMCR_FULLDPLX)
4935 current_duplex = DUPLEX_FULL;
4936 else
4937 current_duplex = DUPLEX_HALF;
4938
Matt Carlsonef167e22007-12-20 20:10:01 -08004939 local_adv = 0;
4940 remote_adv = 0;
4941
Michael Chan747e8f82005-07-25 12:33:22 -07004942 if (bmcr & BMCR_ANENABLE) {
Matt Carlsonef167e22007-12-20 20:10:01 -08004943 u32 common;
Michael Chan747e8f82005-07-25 12:33:22 -07004944
4945 err |= tg3_readphy(tp, MII_ADVERTISE, &local_adv);
4946 err |= tg3_readphy(tp, MII_LPA, &remote_adv);
4947 common = local_adv & remote_adv;
4948 if (common & (ADVERTISE_1000XHALF |
4949 ADVERTISE_1000XFULL)) {
4950 if (common & ADVERTISE_1000XFULL)
4951 current_duplex = DUPLEX_FULL;
4952 else
4953 current_duplex = DUPLEX_HALF;
Joe Perches63c3a662011-04-26 08:12:10 +00004954 } else if (!tg3_flag(tp, 5780_CLASS)) {
Matt Carlson57d8b882010-06-05 17:24:35 +00004955 /* Link is up via parallel detect */
Matt Carlson859a5882010-04-05 10:19:28 +00004956 } else {
Michael Chan747e8f82005-07-25 12:33:22 -07004957 current_link_up = 0;
Matt Carlson859a5882010-04-05 10:19:28 +00004958 }
Michael Chan747e8f82005-07-25 12:33:22 -07004959 }
4960 }
4961
Matt Carlsonef167e22007-12-20 20:10:01 -08004962 if (current_link_up == 1 && current_duplex == DUPLEX_FULL)
4963 tg3_setup_flow_control(tp, local_adv, remote_adv);
4964
Michael Chan747e8f82005-07-25 12:33:22 -07004965 tp->mac_mode &= ~MAC_MODE_HALF_DUPLEX;
4966 if (tp->link_config.active_duplex == DUPLEX_HALF)
4967 tp->mac_mode |= MAC_MODE_HALF_DUPLEX;
4968
4969 tw32_f(MAC_MODE, tp->mac_mode);
4970 udelay(40);
4971
4972 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
4973
4974 tp->link_config.active_speed = current_speed;
4975 tp->link_config.active_duplex = current_duplex;
4976
4977 if (current_link_up != netif_carrier_ok(tp->dev)) {
4978 if (current_link_up)
4979 netif_carrier_on(tp->dev);
4980 else {
4981 netif_carrier_off(tp->dev);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004982 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07004983 }
4984 tg3_link_report(tp);
4985 }
4986 return err;
4987}
4988
4989static void tg3_serdes_parallel_detect(struct tg3 *tp)
4990{
Michael Chan3d3ebe72006-09-27 15:59:15 -07004991 if (tp->serdes_counter) {
Michael Chan747e8f82005-07-25 12:33:22 -07004992 /* Give autoneg time to complete. */
Michael Chan3d3ebe72006-09-27 15:59:15 -07004993 tp->serdes_counter--;
Michael Chan747e8f82005-07-25 12:33:22 -07004994 return;
4995 }
Matt Carlsonc6cdf432010-04-05 10:19:26 +00004996
Michael Chan747e8f82005-07-25 12:33:22 -07004997 if (!netif_carrier_ok(tp->dev) &&
4998 (tp->link_config.autoneg == AUTONEG_ENABLE)) {
4999 u32 bmcr;
5000
5001 tg3_readphy(tp, MII_BMCR, &bmcr);
5002 if (bmcr & BMCR_ANENABLE) {
5003 u32 phy1, phy2;
5004
5005 /* Select shadow register 0x1f */
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00005006 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x7c00);
5007 tg3_readphy(tp, MII_TG3_MISC_SHDW, &phy1);
Michael Chan747e8f82005-07-25 12:33:22 -07005008
5009 /* Select expansion interrupt status register */
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00005010 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
5011 MII_TG3_DSP_EXP1_INT_STAT);
5012 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &phy2);
5013 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &phy2);
Michael Chan747e8f82005-07-25 12:33:22 -07005014
5015 if ((phy1 & 0x10) && !(phy2 & 0x20)) {
5016 /* We have signal detect and not receiving
5017 * config code words, link is up by parallel
5018 * detection.
5019 */
5020
5021 bmcr &= ~BMCR_ANENABLE;
5022 bmcr |= BMCR_SPEED1000 | BMCR_FULLDPLX;
5023 tg3_writephy(tp, MII_BMCR, bmcr);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005024 tp->phy_flags |= TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07005025 }
5026 }
Matt Carlson859a5882010-04-05 10:19:28 +00005027 } else if (netif_carrier_ok(tp->dev) &&
5028 (tp->link_config.autoneg == AUTONEG_ENABLE) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005029 (tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT)) {
Michael Chan747e8f82005-07-25 12:33:22 -07005030 u32 phy2;
5031
5032 /* Select expansion interrupt status register */
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00005033 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
5034 MII_TG3_DSP_EXP1_INT_STAT);
5035 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &phy2);
Michael Chan747e8f82005-07-25 12:33:22 -07005036 if (phy2 & 0x20) {
5037 u32 bmcr;
5038
5039 /* Config code words received, turn on autoneg. */
5040 tg3_readphy(tp, MII_BMCR, &bmcr);
5041 tg3_writephy(tp, MII_BMCR, bmcr | BMCR_ANENABLE);
5042
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005043 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07005044
5045 }
5046 }
5047}
5048
Linus Torvalds1da177e2005-04-16 15:20:36 -07005049static int tg3_setup_phy(struct tg3 *tp, int force_reset)
5050{
Matt Carlsonf2096f92011-04-05 14:22:48 +00005051 u32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005052 int err;
5053
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005054 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005055 err = tg3_setup_fiber_phy(tp, force_reset);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005056 else if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
Michael Chan747e8f82005-07-25 12:33:22 -07005057 err = tg3_setup_fiber_mii_phy(tp, force_reset);
Matt Carlson859a5882010-04-05 10:19:28 +00005058 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07005059 err = tg3_setup_copper_phy(tp, force_reset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005060
Matt Carlsonbcb37f62008-11-03 16:52:09 -08005061 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX) {
Matt Carlsonf2096f92011-04-05 14:22:48 +00005062 u32 scale;
Matt Carlsonaa6c91f2007-11-12 21:18:04 -08005063
5064 val = tr32(TG3_CPMU_CLCK_STAT) & CPMU_CLCK_STAT_MAC_CLCK_MASK;
5065 if (val == CPMU_CLCK_STAT_MAC_CLCK_62_5)
5066 scale = 65;
5067 else if (val == CPMU_CLCK_STAT_MAC_CLCK_6_25)
5068 scale = 6;
5069 else
5070 scale = 12;
5071
5072 val = tr32(GRC_MISC_CFG) & ~GRC_MISC_CFG_PRESCALAR_MASK;
5073 val |= (scale << GRC_MISC_CFG_PRESCALAR_SHIFT);
5074 tw32(GRC_MISC_CFG, val);
5075 }
5076
Matt Carlsonf2096f92011-04-05 14:22:48 +00005077 val = (2 << TX_LENGTHS_IPG_CRS_SHIFT) |
5078 (6 << TX_LENGTHS_IPG_SHIFT);
5079 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
5080 val |= tr32(MAC_TX_LENGTHS) &
5081 (TX_LENGTHS_JMB_FRM_LEN_MSK |
5082 TX_LENGTHS_CNT_DWN_VAL_MSK);
5083
Linus Torvalds1da177e2005-04-16 15:20:36 -07005084 if (tp->link_config.active_speed == SPEED_1000 &&
5085 tp->link_config.active_duplex == DUPLEX_HALF)
Matt Carlsonf2096f92011-04-05 14:22:48 +00005086 tw32(MAC_TX_LENGTHS, val |
5087 (0xff << TX_LENGTHS_SLOT_TIME_SHIFT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005088 else
Matt Carlsonf2096f92011-04-05 14:22:48 +00005089 tw32(MAC_TX_LENGTHS, val |
5090 (32 << TX_LENGTHS_SLOT_TIME_SHIFT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005091
Joe Perches63c3a662011-04-26 08:12:10 +00005092 if (!tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005093 if (netif_carrier_ok(tp->dev)) {
5094 tw32(HOSTCC_STAT_COAL_TICKS,
David S. Miller15f98502005-05-18 22:49:26 -07005095 tp->coal.stats_block_coalesce_usecs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005096 } else {
5097 tw32(HOSTCC_STAT_COAL_TICKS, 0);
5098 }
5099 }
5100
Joe Perches63c3a662011-04-26 08:12:10 +00005101 if (tg3_flag(tp, ASPM_WORKAROUND)) {
Matt Carlsonf2096f92011-04-05 14:22:48 +00005102 val = tr32(PCIE_PWR_MGMT_THRESH);
Matt Carlson8ed5d972007-05-07 00:25:49 -07005103 if (!netif_carrier_ok(tp->dev))
5104 val = (val & ~PCIE_PWR_MGMT_L1_THRESH_MSK) |
5105 tp->pwrmgmt_thresh;
5106 else
5107 val |= PCIE_PWR_MGMT_L1_THRESH_MSK;
5108 tw32(PCIE_PWR_MGMT_THRESH, val);
5109 }
5110
Linus Torvalds1da177e2005-04-16 15:20:36 -07005111 return err;
5112}
5113
Matt Carlson66cfd1b2010-09-30 10:34:30 +00005114static inline int tg3_irq_sync(struct tg3 *tp)
5115{
5116 return tp->irq_sync;
5117}
5118
Matt Carlson97bd8e42011-04-13 11:05:04 +00005119static inline void tg3_rd32_loop(struct tg3 *tp, u32 *dst, u32 off, u32 len)
5120{
5121 int i;
5122
5123 dst = (u32 *)((u8 *)dst + off);
5124 for (i = 0; i < len; i += sizeof(u32))
5125 *dst++ = tr32(off + i);
5126}
5127
5128static void tg3_dump_legacy_regs(struct tg3 *tp, u32 *regs)
5129{
5130 tg3_rd32_loop(tp, regs, TG3PCI_VENDOR, 0xb0);
5131 tg3_rd32_loop(tp, regs, MAILBOX_INTERRUPT_0, 0x200);
5132 tg3_rd32_loop(tp, regs, MAC_MODE, 0x4f0);
5133 tg3_rd32_loop(tp, regs, SNDDATAI_MODE, 0xe0);
5134 tg3_rd32_loop(tp, regs, SNDDATAC_MODE, 0x04);
5135 tg3_rd32_loop(tp, regs, SNDBDS_MODE, 0x80);
5136 tg3_rd32_loop(tp, regs, SNDBDI_MODE, 0x48);
5137 tg3_rd32_loop(tp, regs, SNDBDC_MODE, 0x04);
5138 tg3_rd32_loop(tp, regs, RCVLPC_MODE, 0x20);
5139 tg3_rd32_loop(tp, regs, RCVLPC_SELLST_BASE, 0x15c);
5140 tg3_rd32_loop(tp, regs, RCVDBDI_MODE, 0x0c);
5141 tg3_rd32_loop(tp, regs, RCVDBDI_JUMBO_BD, 0x3c);
5142 tg3_rd32_loop(tp, regs, RCVDBDI_BD_PROD_IDX_0, 0x44);
5143 tg3_rd32_loop(tp, regs, RCVDCC_MODE, 0x04);
5144 tg3_rd32_loop(tp, regs, RCVBDI_MODE, 0x20);
5145 tg3_rd32_loop(tp, regs, RCVCC_MODE, 0x14);
5146 tg3_rd32_loop(tp, regs, RCVLSC_MODE, 0x08);
5147 tg3_rd32_loop(tp, regs, MBFREE_MODE, 0x08);
5148 tg3_rd32_loop(tp, regs, HOSTCC_MODE, 0x100);
5149
Joe Perches63c3a662011-04-26 08:12:10 +00005150 if (tg3_flag(tp, SUPPORT_MSIX))
Matt Carlson97bd8e42011-04-13 11:05:04 +00005151 tg3_rd32_loop(tp, regs, HOSTCC_RXCOL_TICKS_VEC1, 0x180);
5152
5153 tg3_rd32_loop(tp, regs, MEMARB_MODE, 0x10);
5154 tg3_rd32_loop(tp, regs, BUFMGR_MODE, 0x58);
5155 tg3_rd32_loop(tp, regs, RDMAC_MODE, 0x08);
5156 tg3_rd32_loop(tp, regs, WDMAC_MODE, 0x08);
5157 tg3_rd32_loop(tp, regs, RX_CPU_MODE, 0x04);
5158 tg3_rd32_loop(tp, regs, RX_CPU_STATE, 0x04);
5159 tg3_rd32_loop(tp, regs, RX_CPU_PGMCTR, 0x04);
5160 tg3_rd32_loop(tp, regs, RX_CPU_HWBKPT, 0x04);
5161
Joe Perches63c3a662011-04-26 08:12:10 +00005162 if (!tg3_flag(tp, 5705_PLUS)) {
Matt Carlson97bd8e42011-04-13 11:05:04 +00005163 tg3_rd32_loop(tp, regs, TX_CPU_MODE, 0x04);
5164 tg3_rd32_loop(tp, regs, TX_CPU_STATE, 0x04);
5165 tg3_rd32_loop(tp, regs, TX_CPU_PGMCTR, 0x04);
5166 }
5167
5168 tg3_rd32_loop(tp, regs, GRCMBOX_INTERRUPT_0, 0x110);
5169 tg3_rd32_loop(tp, regs, FTQ_RESET, 0x120);
5170 tg3_rd32_loop(tp, regs, MSGINT_MODE, 0x0c);
5171 tg3_rd32_loop(tp, regs, DMAC_MODE, 0x04);
5172 tg3_rd32_loop(tp, regs, GRC_MODE, 0x4c);
5173
Joe Perches63c3a662011-04-26 08:12:10 +00005174 if (tg3_flag(tp, NVRAM))
Matt Carlson97bd8e42011-04-13 11:05:04 +00005175 tg3_rd32_loop(tp, regs, NVRAM_CMD, 0x24);
5176}
5177
5178static void tg3_dump_state(struct tg3 *tp)
5179{
5180 int i;
5181 u32 *regs;
5182
5183 regs = kzalloc(TG3_REG_BLK_SIZE, GFP_ATOMIC);
5184 if (!regs) {
5185 netdev_err(tp->dev, "Failed allocating register dump buffer\n");
5186 return;
5187 }
5188
Joe Perches63c3a662011-04-26 08:12:10 +00005189 if (tg3_flag(tp, PCI_EXPRESS)) {
Matt Carlson97bd8e42011-04-13 11:05:04 +00005190 /* Read up to but not including private PCI registers */
5191 for (i = 0; i < TG3_PCIE_TLDLPL_PORT; i += sizeof(u32))
5192 regs[i / sizeof(u32)] = tr32(i);
5193 } else
5194 tg3_dump_legacy_regs(tp, regs);
5195
5196 for (i = 0; i < TG3_REG_BLK_SIZE / sizeof(u32); i += 4) {
5197 if (!regs[i + 0] && !regs[i + 1] &&
5198 !regs[i + 2] && !regs[i + 3])
5199 continue;
5200
5201 netdev_err(tp->dev, "0x%08x: 0x%08x, 0x%08x, 0x%08x, 0x%08x\n",
5202 i * 4,
5203 regs[i + 0], regs[i + 1], regs[i + 2], regs[i + 3]);
5204 }
5205
5206 kfree(regs);
5207
5208 for (i = 0; i < tp->irq_cnt; i++) {
5209 struct tg3_napi *tnapi = &tp->napi[i];
5210
5211 /* SW status block */
5212 netdev_err(tp->dev,
5213 "%d: Host status block [%08x:%08x:(%04x:%04x:%04x):(%04x:%04x)]\n",
5214 i,
5215 tnapi->hw_status->status,
5216 tnapi->hw_status->status_tag,
5217 tnapi->hw_status->rx_jumbo_consumer,
5218 tnapi->hw_status->rx_consumer,
5219 tnapi->hw_status->rx_mini_consumer,
5220 tnapi->hw_status->idx[0].rx_producer,
5221 tnapi->hw_status->idx[0].tx_consumer);
5222
5223 netdev_err(tp->dev,
5224 "%d: NAPI info [%08x:%08x:(%04x:%04x:%04x):%04x:(%04x:%04x:%04x:%04x)]\n",
5225 i,
5226 tnapi->last_tag, tnapi->last_irq_tag,
5227 tnapi->tx_prod, tnapi->tx_cons, tnapi->tx_pending,
5228 tnapi->rx_rcb_ptr,
5229 tnapi->prodring.rx_std_prod_idx,
5230 tnapi->prodring.rx_std_cons_idx,
5231 tnapi->prodring.rx_jmb_prod_idx,
5232 tnapi->prodring.rx_jmb_cons_idx);
5233 }
5234}
5235
Michael Chandf3e6542006-05-26 17:48:07 -07005236/* This is called whenever we suspect that the system chipset is re-
5237 * ordering the sequence of MMIO to the tx send mailbox. The symptom
5238 * is bogus tx completions. We try to recover by setting the
5239 * TG3_FLAG_MBOX_WRITE_REORDER flag and resetting the chip later
5240 * in the workqueue.
5241 */
5242static void tg3_tx_recover(struct tg3 *tp)
5243{
Joe Perches63c3a662011-04-26 08:12:10 +00005244 BUG_ON(tg3_flag(tp, MBOX_WRITE_REORDER) ||
Michael Chandf3e6542006-05-26 17:48:07 -07005245 tp->write32_tx_mbox == tg3_write_indirect_mbox);
5246
Matt Carlson5129c3a2010-04-05 10:19:23 +00005247 netdev_warn(tp->dev,
5248 "The system may be re-ordering memory-mapped I/O "
5249 "cycles to the network device, attempting to recover. "
5250 "Please report the problem to the driver maintainer "
5251 "and include system chipset information.\n");
Michael Chandf3e6542006-05-26 17:48:07 -07005252
5253 spin_lock(&tp->lock);
Joe Perches63c3a662011-04-26 08:12:10 +00005254 tg3_flag_set(tp, TX_RECOVERY_PENDING);
Michael Chandf3e6542006-05-26 17:48:07 -07005255 spin_unlock(&tp->lock);
5256}
5257
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005258static inline u32 tg3_tx_avail(struct tg3_napi *tnapi)
Michael Chan1b2a7202006-08-07 21:46:02 -07005259{
Matt Carlsonf65aac12010-08-02 11:26:03 +00005260 /* Tell compiler to fetch tx indices from memory. */
5261 barrier();
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005262 return tnapi->tx_pending -
5263 ((tnapi->tx_prod - tnapi->tx_cons) & (TG3_TX_RING_SIZE - 1));
Michael Chan1b2a7202006-08-07 21:46:02 -07005264}
5265
Linus Torvalds1da177e2005-04-16 15:20:36 -07005266/* Tigon3 never reports partial packet sends. So we do not
5267 * need special logic to handle SKBs that have not had all
5268 * of their frags sent yet, like SunGEM does.
5269 */
Matt Carlson17375d22009-08-28 14:02:18 +00005270static void tg3_tx(struct tg3_napi *tnapi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005271{
Matt Carlson17375d22009-08-28 14:02:18 +00005272 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00005273 u32 hw_idx = tnapi->hw_status->idx[0].tx_consumer;
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005274 u32 sw_idx = tnapi->tx_cons;
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005275 struct netdev_queue *txq;
5276 int index = tnapi - tp->napi;
Tom Herbert298376d2011-11-28 16:33:30 +00005277 unsigned int pkts_compl = 0, bytes_compl = 0;
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005278
Joe Perches63c3a662011-04-26 08:12:10 +00005279 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005280 index--;
5281
5282 txq = netdev_get_tx_queue(tp->dev, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005283
5284 while (sw_idx != hw_idx) {
Matt Carlsondf8944c2011-07-27 14:20:46 +00005285 struct tg3_tx_ring_info *ri = &tnapi->tx_buffers[sw_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07005286 struct sk_buff *skb = ri->skb;
Michael Chandf3e6542006-05-26 17:48:07 -07005287 int i, tx_bug = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005288
Michael Chandf3e6542006-05-26 17:48:07 -07005289 if (unlikely(skb == NULL)) {
5290 tg3_tx_recover(tp);
5291 return;
5292 }
5293
Alexander Duyckf4188d82009-12-02 16:48:38 +00005294 pci_unmap_single(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005295 dma_unmap_addr(ri, mapping),
Alexander Duyckf4188d82009-12-02 16:48:38 +00005296 skb_headlen(skb),
5297 PCI_DMA_TODEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005298
5299 ri->skb = NULL;
5300
Matt Carlsone01ee142011-07-27 14:20:50 +00005301 while (ri->fragmented) {
5302 ri->fragmented = false;
5303 sw_idx = NEXT_TX(sw_idx);
5304 ri = &tnapi->tx_buffers[sw_idx];
5305 }
5306
Linus Torvalds1da177e2005-04-16 15:20:36 -07005307 sw_idx = NEXT_TX(sw_idx);
5308
5309 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005310 ri = &tnapi->tx_buffers[sw_idx];
Michael Chandf3e6542006-05-26 17:48:07 -07005311 if (unlikely(ri->skb != NULL || sw_idx == hw_idx))
5312 tx_bug = 1;
Alexander Duyckf4188d82009-12-02 16:48:38 +00005313
5314 pci_unmap_page(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005315 dma_unmap_addr(ri, mapping),
Eric Dumazet9e903e02011-10-18 21:00:24 +00005316 skb_frag_size(&skb_shinfo(skb)->frags[i]),
Alexander Duyckf4188d82009-12-02 16:48:38 +00005317 PCI_DMA_TODEVICE);
Matt Carlsone01ee142011-07-27 14:20:50 +00005318
5319 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
Tom Herbert298376d2011-11-28 16:33:30 +00005328 pkts_compl++;
5329 bytes_compl += skb->len;
5330
David S. Millerf47c11e2005-06-24 20:18:35 -07005331 dev_kfree_skb(skb);
Michael Chandf3e6542006-05-26 17:48:07 -07005332
5333 if (unlikely(tx_bug)) {
5334 tg3_tx_recover(tp);
5335 return;
5336 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005337 }
5338
Tom Herbert298376d2011-11-28 16:33:30 +00005339 netdev_completed_queue(tp->dev, pkts_compl, bytes_compl);
5340
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005341 tnapi->tx_cons = sw_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005342
Michael Chan1b2a7202006-08-07 21:46:02 -07005343 /* Need to make the tx_cons update visible to tg3_start_xmit()
5344 * before checking for netif_queue_stopped(). Without the
5345 * memory barrier, there is a small possibility that tg3_start_xmit()
5346 * will miss it and cause the queue to be stopped forever.
5347 */
5348 smp_mb();
5349
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005350 if (unlikely(netif_tx_queue_stopped(txq) &&
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005351 (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi)))) {
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005352 __netif_tx_lock(txq, smp_processor_id());
5353 if (netif_tx_queue_stopped(txq) &&
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005354 (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi)))
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005355 netif_tx_wake_queue(txq);
5356 __netif_tx_unlock(txq);
Michael Chan51b91462005-09-01 17:41:28 -07005357 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005358}
5359
Eric Dumazet9205fd92011-11-18 06:47:01 +00005360static void tg3_rx_data_free(struct tg3 *tp, struct ring_info *ri, u32 map_sz)
Matt Carlson2b2cdb62009-11-13 13:03:48 +00005361{
Eric Dumazet9205fd92011-11-18 06:47:01 +00005362 if (!ri->data)
Matt Carlson2b2cdb62009-11-13 13:03:48 +00005363 return;
5364
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005365 pci_unmap_single(tp->pdev, dma_unmap_addr(ri, mapping),
Matt Carlson2b2cdb62009-11-13 13:03:48 +00005366 map_sz, PCI_DMA_FROMDEVICE);
Eric Dumazet9205fd92011-11-18 06:47:01 +00005367 kfree(ri->data);
5368 ri->data = NULL;
Matt Carlson2b2cdb62009-11-13 13:03:48 +00005369}
5370
Linus Torvalds1da177e2005-04-16 15:20:36 -07005371/* Returns size of skb allocated or < 0 on error.
5372 *
5373 * We only need to fill in the address because the other members
5374 * of the RX descriptor are invariant, see tg3_init_rings.
5375 *
5376 * Note the purposeful assymetry of cpu vs. chip accesses. For
5377 * posting buffers we only dirty the first cache line of the RX
5378 * descriptor (containing the address). Whereas for the RX status
5379 * buffers the cpu only reads the last cacheline of the RX descriptor
5380 * (to fetch the error flags, vlan tag, checksum, and opaque cookie).
5381 */
Eric Dumazet9205fd92011-11-18 06:47:01 +00005382static int tg3_alloc_rx_data(struct tg3 *tp, struct tg3_rx_prodring_set *tpr,
Matt Carlsona3896162009-11-13 13:03:44 +00005383 u32 opaque_key, u32 dest_idx_unmasked)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005384{
5385 struct tg3_rx_buffer_desc *desc;
Matt Carlsonf94e2902010-10-14 10:37:42 +00005386 struct ring_info *map;
Eric Dumazet9205fd92011-11-18 06:47:01 +00005387 u8 *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005388 dma_addr_t mapping;
Eric Dumazet9205fd92011-11-18 06:47:01 +00005389 int skb_size, data_size, dest_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005390
Linus Torvalds1da177e2005-04-16 15:20:36 -07005391 switch (opaque_key) {
5392 case RXD_OPAQUE_RING_STD:
Matt Carlson2c49a442010-09-30 10:34:35 +00005393 dest_idx = dest_idx_unmasked & tp->rx_std_ring_mask;
Matt Carlson21f581a2009-08-28 14:00:25 +00005394 desc = &tpr->rx_std[dest_idx];
5395 map = &tpr->rx_std_buffers[dest_idx];
Eric Dumazet9205fd92011-11-18 06:47:01 +00005396 data_size = tp->rx_pkt_map_sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005397 break;
5398
5399 case RXD_OPAQUE_RING_JUMBO:
Matt Carlson2c49a442010-09-30 10:34:35 +00005400 dest_idx = dest_idx_unmasked & tp->rx_jmb_ring_mask;
Matt Carlson79ed5ac2009-08-28 14:00:55 +00005401 desc = &tpr->rx_jmb[dest_idx].std;
Matt Carlson21f581a2009-08-28 14:00:25 +00005402 map = &tpr->rx_jmb_buffers[dest_idx];
Eric Dumazet9205fd92011-11-18 06:47:01 +00005403 data_size = TG3_RX_JMB_MAP_SZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005404 break;
5405
5406 default:
5407 return -EINVAL;
Stephen Hemminger855e1112008-04-16 16:37:28 -07005408 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005409
5410 /* Do not overwrite any of the map or rp information
5411 * until we are sure we can commit to a new buffer.
5412 *
5413 * Callers depend upon this behavior and assume that
5414 * we leave everything unchanged if we fail.
5415 */
Eric Dumazet9205fd92011-11-18 06:47:01 +00005416 skb_size = SKB_DATA_ALIGN(data_size + TG3_RX_OFFSET(tp)) +
5417 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
5418 data = kmalloc(skb_size, GFP_ATOMIC);
5419 if (!data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005420 return -ENOMEM;
5421
Eric Dumazet9205fd92011-11-18 06:47:01 +00005422 mapping = pci_map_single(tp->pdev,
5423 data + TG3_RX_OFFSET(tp),
5424 data_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005425 PCI_DMA_FROMDEVICE);
Matt Carlsona21771d2009-11-02 14:25:31 +00005426 if (pci_dma_mapping_error(tp->pdev, mapping)) {
Eric Dumazet9205fd92011-11-18 06:47:01 +00005427 kfree(data);
Matt Carlsona21771d2009-11-02 14:25:31 +00005428 return -EIO;
5429 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005430
Eric Dumazet9205fd92011-11-18 06:47:01 +00005431 map->data = data;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005432 dma_unmap_addr_set(map, mapping, mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005433
Linus Torvalds1da177e2005-04-16 15:20:36 -07005434 desc->addr_hi = ((u64)mapping >> 32);
5435 desc->addr_lo = ((u64)mapping & 0xffffffff);
5436
Eric Dumazet9205fd92011-11-18 06:47:01 +00005437 return data_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005438}
5439
5440/* We only need to move over in the address because the other
5441 * members of the RX descriptor are invariant. See notes above
Eric Dumazet9205fd92011-11-18 06:47:01 +00005442 * tg3_alloc_rx_data for full details.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005443 */
Matt Carlsona3896162009-11-13 13:03:44 +00005444static void tg3_recycle_rx(struct tg3_napi *tnapi,
5445 struct tg3_rx_prodring_set *dpr,
5446 u32 opaque_key, int src_idx,
5447 u32 dest_idx_unmasked)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005448{
Matt Carlson17375d22009-08-28 14:02:18 +00005449 struct tg3 *tp = tnapi->tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005450 struct tg3_rx_buffer_desc *src_desc, *dest_desc;
5451 struct ring_info *src_map, *dest_map;
Matt Carlson8fea32b2010-09-15 08:59:58 +00005452 struct tg3_rx_prodring_set *spr = &tp->napi[0].prodring;
Matt Carlsonc6cdf432010-04-05 10:19:26 +00005453 int dest_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005454
5455 switch (opaque_key) {
5456 case RXD_OPAQUE_RING_STD:
Matt Carlson2c49a442010-09-30 10:34:35 +00005457 dest_idx = dest_idx_unmasked & tp->rx_std_ring_mask;
Matt Carlsona3896162009-11-13 13:03:44 +00005458 dest_desc = &dpr->rx_std[dest_idx];
5459 dest_map = &dpr->rx_std_buffers[dest_idx];
5460 src_desc = &spr->rx_std[src_idx];
5461 src_map = &spr->rx_std_buffers[src_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07005462 break;
5463
5464 case RXD_OPAQUE_RING_JUMBO:
Matt Carlson2c49a442010-09-30 10:34:35 +00005465 dest_idx = dest_idx_unmasked & tp->rx_jmb_ring_mask;
Matt Carlsona3896162009-11-13 13:03:44 +00005466 dest_desc = &dpr->rx_jmb[dest_idx].std;
5467 dest_map = &dpr->rx_jmb_buffers[dest_idx];
5468 src_desc = &spr->rx_jmb[src_idx].std;
5469 src_map = &spr->rx_jmb_buffers[src_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07005470 break;
5471
5472 default:
5473 return;
Stephen Hemminger855e1112008-04-16 16:37:28 -07005474 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005475
Eric Dumazet9205fd92011-11-18 06:47:01 +00005476 dest_map->data = src_map->data;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005477 dma_unmap_addr_set(dest_map, mapping,
5478 dma_unmap_addr(src_map, mapping));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005479 dest_desc->addr_hi = src_desc->addr_hi;
5480 dest_desc->addr_lo = src_desc->addr_lo;
Matt Carlsone92967b2010-02-12 14:47:06 +00005481
5482 /* Ensure that the update to the skb happens after the physical
5483 * addresses have been transferred to the new BD location.
5484 */
5485 smp_wmb();
5486
Eric Dumazet9205fd92011-11-18 06:47:01 +00005487 src_map->data = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005488}
5489
Linus Torvalds1da177e2005-04-16 15:20:36 -07005490/* The RX ring scheme is composed of multiple rings which post fresh
5491 * buffers to the chip, and one special ring the chip uses to report
5492 * status back to the host.
5493 *
5494 * The special ring reports the status of received packets to the
5495 * host. The chip does not write into the original descriptor the
5496 * RX buffer was obtained from. The chip simply takes the original
5497 * descriptor as provided by the host, updates the status and length
5498 * field, then writes this into the next status ring entry.
5499 *
5500 * Each ring the host uses to post buffers to the chip is described
5501 * by a TG3_BDINFO entry in the chips SRAM area. When a packet arrives,
5502 * it is first placed into the on-chip ram. When the packet's length
5503 * is known, it walks down the TG3_BDINFO entries to select the ring.
5504 * Each TG3_BDINFO specifies a MAXLEN field and the first TG3_BDINFO
5505 * which is within the range of the new packet's length is chosen.
5506 *
5507 * The "separate ring for rx status" scheme may sound queer, but it makes
5508 * sense from a cache coherency perspective. If only the host writes
5509 * to the buffer post rings, and only the chip writes to the rx status
5510 * rings, then cache lines never move beyond shared-modified state.
5511 * If both the host and chip were to write into the same ring, cache line
5512 * eviction could occur since both entities want it in an exclusive state.
5513 */
Matt Carlson17375d22009-08-28 14:02:18 +00005514static int tg3_rx(struct tg3_napi *tnapi, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005515{
Matt Carlson17375d22009-08-28 14:02:18 +00005516 struct tg3 *tp = tnapi->tp;
Michael Chanf92905d2006-06-29 20:14:29 -07005517 u32 work_mask, rx_std_posted = 0;
Matt Carlson43619352009-11-13 13:03:47 +00005518 u32 std_prod_idx, jmb_prod_idx;
Matt Carlson72334482009-08-28 14:03:01 +00005519 u32 sw_idx = tnapi->rx_rcb_ptr;
Michael Chan483ba502005-04-25 15:14:03 -07005520 u16 hw_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005521 int received;
Matt Carlson8fea32b2010-09-15 08:59:58 +00005522 struct tg3_rx_prodring_set *tpr = &tnapi->prodring;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005523
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00005524 hw_idx = *(tnapi->rx_rcb_prod_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005525 /*
5526 * We need to order the read of hw_idx and the read of
5527 * the opaque cookie.
5528 */
5529 rmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005530 work_mask = 0;
5531 received = 0;
Matt Carlson43619352009-11-13 13:03:47 +00005532 std_prod_idx = tpr->rx_std_prod_idx;
5533 jmb_prod_idx = tpr->rx_jmb_prod_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005534 while (sw_idx != hw_idx && budget > 0) {
Matt Carlsonafc081f2009-11-13 13:03:43 +00005535 struct ring_info *ri;
Matt Carlson72334482009-08-28 14:03:01 +00005536 struct tg3_rx_buffer_desc *desc = &tnapi->rx_rcb[sw_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07005537 unsigned int len;
5538 struct sk_buff *skb;
5539 dma_addr_t dma_addr;
5540 u32 opaque_key, desc_idx, *post_ptr;
Eric Dumazet9205fd92011-11-18 06:47:01 +00005541 u8 *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005542
5543 desc_idx = desc->opaque & RXD_OPAQUE_INDEX_MASK;
5544 opaque_key = desc->opaque & RXD_OPAQUE_RING_MASK;
5545 if (opaque_key == RXD_OPAQUE_RING_STD) {
Matt Carlson8fea32b2010-09-15 08:59:58 +00005546 ri = &tp->napi[0].prodring.rx_std_buffers[desc_idx];
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005547 dma_addr = dma_unmap_addr(ri, mapping);
Eric Dumazet9205fd92011-11-18 06:47:01 +00005548 data = ri->data;
Matt Carlson43619352009-11-13 13:03:47 +00005549 post_ptr = &std_prod_idx;
Michael Chanf92905d2006-06-29 20:14:29 -07005550 rx_std_posted++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005551 } else if (opaque_key == RXD_OPAQUE_RING_JUMBO) {
Matt Carlson8fea32b2010-09-15 08:59:58 +00005552 ri = &tp->napi[0].prodring.rx_jmb_buffers[desc_idx];
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005553 dma_addr = dma_unmap_addr(ri, mapping);
Eric Dumazet9205fd92011-11-18 06:47:01 +00005554 data = ri->data;
Matt Carlson43619352009-11-13 13:03:47 +00005555 post_ptr = &jmb_prod_idx;
Matt Carlson21f581a2009-08-28 14:00:25 +00005556 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07005557 goto next_pkt_nopost;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005558
5559 work_mask |= opaque_key;
5560
5561 if ((desc->err_vlan & RXD_ERR_MASK) != 0 &&
5562 (desc->err_vlan != RXD_ERR_ODD_NIBBLE_RCVD_MII)) {
5563 drop_it:
Matt Carlsona3896162009-11-13 13:03:44 +00005564 tg3_recycle_rx(tnapi, tpr, opaque_key,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005565 desc_idx, *post_ptr);
5566 drop_it_no_recycle:
5567 /* Other statistics kept track of by card. */
Eric Dumazetb0057c52010-10-10 19:55:52 +00005568 tp->rx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005569 goto next_pkt;
5570 }
5571
Eric Dumazet9205fd92011-11-18 06:47:01 +00005572 prefetch(data + TG3_RX_OFFSET(tp));
Matt Carlsonad829262008-11-21 17:16:16 -08005573 len = ((desc->idx_len & RXD_LEN_MASK) >> RXD_LEN_SHIFT) -
5574 ETH_FCS_LEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005575
Matt Carlsond2757fc2010-04-12 06:58:27 +00005576 if (len > TG3_RX_COPY_THRESH(tp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005577 int skb_size;
5578
Eric Dumazet9205fd92011-11-18 06:47:01 +00005579 skb_size = tg3_alloc_rx_data(tp, tpr, opaque_key,
Matt Carlsonafc081f2009-11-13 13:03:43 +00005580 *post_ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005581 if (skb_size < 0)
5582 goto drop_it;
5583
Matt Carlson287be122009-08-28 13:58:46 +00005584 pci_unmap_single(tp->pdev, dma_addr, skb_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005585 PCI_DMA_FROMDEVICE);
5586
Eric Dumazet9205fd92011-11-18 06:47:01 +00005587 skb = build_skb(data);
5588 if (!skb) {
5589 kfree(data);
5590 goto drop_it_no_recycle;
5591 }
5592 skb_reserve(skb, TG3_RX_OFFSET(tp));
5593 /* Ensure that the update to the data happens
Matt Carlson61e800c2010-02-17 15:16:54 +00005594 * after the usage of the old DMA mapping.
5595 */
5596 smp_wmb();
5597
Eric Dumazet9205fd92011-11-18 06:47:01 +00005598 ri->data = NULL;
Matt Carlson61e800c2010-02-17 15:16:54 +00005599
Linus Torvalds1da177e2005-04-16 15:20:36 -07005600 } else {
Matt Carlsona3896162009-11-13 13:03:44 +00005601 tg3_recycle_rx(tnapi, tpr, opaque_key,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005602 desc_idx, *post_ptr);
5603
Eric Dumazet9205fd92011-11-18 06:47:01 +00005604 skb = netdev_alloc_skb(tp->dev,
5605 len + TG3_RAW_IP_ALIGN);
5606 if (skb == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005607 goto drop_it_no_recycle;
5608
Eric Dumazet9205fd92011-11-18 06:47:01 +00005609 skb_reserve(skb, TG3_RAW_IP_ALIGN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005610 pci_dma_sync_single_for_cpu(tp->pdev, dma_addr, len, PCI_DMA_FROMDEVICE);
Eric Dumazet9205fd92011-11-18 06:47:01 +00005611 memcpy(skb->data,
5612 data + TG3_RX_OFFSET(tp),
5613 len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005614 pci_dma_sync_single_for_device(tp->pdev, dma_addr, len, PCI_DMA_FROMDEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005615 }
5616
Eric Dumazet9205fd92011-11-18 06:47:01 +00005617 skb_put(skb, len);
Michał Mirosławdc668912011-04-07 03:35:07 +00005618 if ((tp->dev->features & NETIF_F_RXCSUM) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005619 (desc->type_flags & RXD_FLAG_TCPUDP_CSUM) &&
5620 (((desc->ip_tcp_csum & RXD_TCPCSUM_MASK)
5621 >> RXD_TCPCSUM_SHIFT) == 0xffff))
5622 skb->ip_summed = CHECKSUM_UNNECESSARY;
5623 else
Eric Dumazetbc8acf22010-09-02 13:07:41 -07005624 skb_checksum_none_assert(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005625
5626 skb->protocol = eth_type_trans(skb, tp->dev);
Matt Carlsonf7b493e2009-02-25 14:21:52 +00005627
5628 if (len > (tp->dev->mtu + ETH_HLEN) &&
5629 skb->protocol != htons(ETH_P_8021Q)) {
5630 dev_kfree_skb(skb);
Eric Dumazetb0057c52010-10-10 19:55:52 +00005631 goto drop_it_no_recycle;
Matt Carlsonf7b493e2009-02-25 14:21:52 +00005632 }
5633
Matt Carlson9dc7a112010-04-12 06:58:28 +00005634 if (desc->type_flags & RXD_FLAG_VLAN &&
Matt Carlsonbf933c82011-01-25 15:58:49 +00005635 !(tp->rx_mode & RX_MODE_KEEP_VLAN_TAG))
5636 __vlan_hwaccel_put_tag(skb,
5637 desc->err_vlan & RXD_VLAN_MASK);
Matt Carlson9dc7a112010-04-12 06:58:28 +00005638
Matt Carlsonbf933c82011-01-25 15:58:49 +00005639 napi_gro_receive(&tnapi->napi, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005640
Linus Torvalds1da177e2005-04-16 15:20:36 -07005641 received++;
5642 budget--;
5643
5644next_pkt:
5645 (*post_ptr)++;
Michael Chanf92905d2006-06-29 20:14:29 -07005646
5647 if (unlikely(rx_std_posted >= tp->rx_std_max_post)) {
Matt Carlson2c49a442010-09-30 10:34:35 +00005648 tpr->rx_std_prod_idx = std_prod_idx &
5649 tp->rx_std_ring_mask;
Matt Carlson86cfe4f2010-01-12 10:11:37 +00005650 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
5651 tpr->rx_std_prod_idx);
Michael Chanf92905d2006-06-29 20:14:29 -07005652 work_mask &= ~RXD_OPAQUE_RING_STD;
5653 rx_std_posted = 0;
5654 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005655next_pkt_nopost:
Michael Chan483ba502005-04-25 15:14:03 -07005656 sw_idx++;
Matt Carlson7cb32cf2010-09-30 10:34:36 +00005657 sw_idx &= tp->rx_ret_ring_mask;
Michael Chan52f6d692005-04-25 15:14:32 -07005658
5659 /* Refresh hw_idx to see if there is new work */
5660 if (sw_idx == hw_idx) {
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00005661 hw_idx = *(tnapi->rx_rcb_prod_idx);
Michael Chan52f6d692005-04-25 15:14:32 -07005662 rmb();
5663 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005664 }
5665
5666 /* ACK the status ring. */
Matt Carlson72334482009-08-28 14:03:01 +00005667 tnapi->rx_rcb_ptr = sw_idx;
5668 tw32_rx_mbox(tnapi->consmbox, sw_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005669
5670 /* Refill RX ring(s). */
Joe Perches63c3a662011-04-26 08:12:10 +00005671 if (!tg3_flag(tp, ENABLE_RSS)) {
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005672 if (work_mask & RXD_OPAQUE_RING_STD) {
Matt Carlson2c49a442010-09-30 10:34:35 +00005673 tpr->rx_std_prod_idx = std_prod_idx &
5674 tp->rx_std_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005675 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
5676 tpr->rx_std_prod_idx);
5677 }
5678 if (work_mask & RXD_OPAQUE_RING_JUMBO) {
Matt Carlson2c49a442010-09-30 10:34:35 +00005679 tpr->rx_jmb_prod_idx = jmb_prod_idx &
5680 tp->rx_jmb_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005681 tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG,
5682 tpr->rx_jmb_prod_idx);
5683 }
5684 mmiowb();
5685 } else if (work_mask) {
5686 /* rx_std_buffers[] and rx_jmb_buffers[] entries must be
5687 * updated before the producer indices can be updated.
5688 */
5689 smp_wmb();
5690
Matt Carlson2c49a442010-09-30 10:34:35 +00005691 tpr->rx_std_prod_idx = std_prod_idx & tp->rx_std_ring_mask;
5692 tpr->rx_jmb_prod_idx = jmb_prod_idx & tp->rx_jmb_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005693
Matt Carlsone4af1af2010-02-12 14:47:05 +00005694 if (tnapi != &tp->napi[1])
5695 napi_schedule(&tp->napi[1].napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005696 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005697
5698 return received;
5699}
5700
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005701static void tg3_poll_link(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005702{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005703 /* handle link change and other phy events */
Joe Perches63c3a662011-04-26 08:12:10 +00005704 if (!(tg3_flag(tp, USE_LINKCHG_REG) || tg3_flag(tp, POLL_SERDES))) {
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005705 struct tg3_hw_status *sblk = tp->napi[0].hw_status;
5706
Linus Torvalds1da177e2005-04-16 15:20:36 -07005707 if (sblk->status & SD_STATUS_LINK_CHG) {
5708 sblk->status = SD_STATUS_UPDATED |
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005709 (sblk->status & ~SD_STATUS_LINK_CHG);
David S. Millerf47c11e2005-06-24 20:18:35 -07005710 spin_lock(&tp->lock);
Joe Perches63c3a662011-04-26 08:12:10 +00005711 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlsondd477002008-05-25 23:45:58 -07005712 tw32_f(MAC_STATUS,
5713 (MAC_STATUS_SYNC_CHANGED |
5714 MAC_STATUS_CFG_CHANGED |
5715 MAC_STATUS_MI_COMPLETION |
5716 MAC_STATUS_LNKSTATE_CHANGED));
5717 udelay(40);
5718 } else
5719 tg3_setup_phy(tp, 0);
David S. Millerf47c11e2005-06-24 20:18:35 -07005720 spin_unlock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005721 }
5722 }
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005723}
5724
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005725static int tg3_rx_prodring_xfer(struct tg3 *tp,
5726 struct tg3_rx_prodring_set *dpr,
5727 struct tg3_rx_prodring_set *spr)
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005728{
5729 u32 si, di, cpycnt, src_prod_idx;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005730 int i, err = 0;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005731
5732 while (1) {
5733 src_prod_idx = spr->rx_std_prod_idx;
5734
5735 /* Make sure updates to the rx_std_buffers[] entries and the
5736 * standard producer index are seen in the correct order.
5737 */
5738 smp_rmb();
5739
5740 if (spr->rx_std_cons_idx == src_prod_idx)
5741 break;
5742
5743 if (spr->rx_std_cons_idx < src_prod_idx)
5744 cpycnt = src_prod_idx - spr->rx_std_cons_idx;
5745 else
Matt Carlson2c49a442010-09-30 10:34:35 +00005746 cpycnt = tp->rx_std_ring_mask + 1 -
5747 spr->rx_std_cons_idx;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005748
Matt Carlson2c49a442010-09-30 10:34:35 +00005749 cpycnt = min(cpycnt,
5750 tp->rx_std_ring_mask + 1 - dpr->rx_std_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005751
5752 si = spr->rx_std_cons_idx;
5753 di = dpr->rx_std_prod_idx;
5754
Matt Carlsone92967b2010-02-12 14:47:06 +00005755 for (i = di; i < di + cpycnt; i++) {
Eric Dumazet9205fd92011-11-18 06:47:01 +00005756 if (dpr->rx_std_buffers[i].data) {
Matt Carlsone92967b2010-02-12 14:47:06 +00005757 cpycnt = i - di;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005758 err = -ENOSPC;
Matt Carlsone92967b2010-02-12 14:47:06 +00005759 break;
5760 }
5761 }
5762
5763 if (!cpycnt)
5764 break;
5765
5766 /* Ensure that updates to the rx_std_buffers ring and the
5767 * shadowed hardware producer ring from tg3_recycle_skb() are
5768 * ordered correctly WRT the skb check above.
5769 */
5770 smp_rmb();
5771
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005772 memcpy(&dpr->rx_std_buffers[di],
5773 &spr->rx_std_buffers[si],
5774 cpycnt * sizeof(struct ring_info));
5775
5776 for (i = 0; i < cpycnt; i++, di++, si++) {
5777 struct tg3_rx_buffer_desc *sbd, *dbd;
5778 sbd = &spr->rx_std[si];
5779 dbd = &dpr->rx_std[di];
5780 dbd->addr_hi = sbd->addr_hi;
5781 dbd->addr_lo = sbd->addr_lo;
5782 }
5783
Matt Carlson2c49a442010-09-30 10:34:35 +00005784 spr->rx_std_cons_idx = (spr->rx_std_cons_idx + cpycnt) &
5785 tp->rx_std_ring_mask;
5786 dpr->rx_std_prod_idx = (dpr->rx_std_prod_idx + cpycnt) &
5787 tp->rx_std_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005788 }
5789
5790 while (1) {
5791 src_prod_idx = spr->rx_jmb_prod_idx;
5792
5793 /* Make sure updates to the rx_jmb_buffers[] entries and
5794 * the jumbo producer index are seen in the correct order.
5795 */
5796 smp_rmb();
5797
5798 if (spr->rx_jmb_cons_idx == src_prod_idx)
5799 break;
5800
5801 if (spr->rx_jmb_cons_idx < src_prod_idx)
5802 cpycnt = src_prod_idx - spr->rx_jmb_cons_idx;
5803 else
Matt Carlson2c49a442010-09-30 10:34:35 +00005804 cpycnt = tp->rx_jmb_ring_mask + 1 -
5805 spr->rx_jmb_cons_idx;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005806
5807 cpycnt = min(cpycnt,
Matt Carlson2c49a442010-09-30 10:34:35 +00005808 tp->rx_jmb_ring_mask + 1 - dpr->rx_jmb_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005809
5810 si = spr->rx_jmb_cons_idx;
5811 di = dpr->rx_jmb_prod_idx;
5812
Matt Carlsone92967b2010-02-12 14:47:06 +00005813 for (i = di; i < di + cpycnt; i++) {
Eric Dumazet9205fd92011-11-18 06:47:01 +00005814 if (dpr->rx_jmb_buffers[i].data) {
Matt Carlsone92967b2010-02-12 14:47:06 +00005815 cpycnt = i - di;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005816 err = -ENOSPC;
Matt Carlsone92967b2010-02-12 14:47:06 +00005817 break;
5818 }
5819 }
5820
5821 if (!cpycnt)
5822 break;
5823
5824 /* Ensure that updates to the rx_jmb_buffers ring and the
5825 * shadowed hardware producer ring from tg3_recycle_skb() are
5826 * ordered correctly WRT the skb check above.
5827 */
5828 smp_rmb();
5829
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005830 memcpy(&dpr->rx_jmb_buffers[di],
5831 &spr->rx_jmb_buffers[si],
5832 cpycnt * sizeof(struct ring_info));
5833
5834 for (i = 0; i < cpycnt; i++, di++, si++) {
5835 struct tg3_rx_buffer_desc *sbd, *dbd;
5836 sbd = &spr->rx_jmb[si].std;
5837 dbd = &dpr->rx_jmb[di].std;
5838 dbd->addr_hi = sbd->addr_hi;
5839 dbd->addr_lo = sbd->addr_lo;
5840 }
5841
Matt Carlson2c49a442010-09-30 10:34:35 +00005842 spr->rx_jmb_cons_idx = (spr->rx_jmb_cons_idx + cpycnt) &
5843 tp->rx_jmb_ring_mask;
5844 dpr->rx_jmb_prod_idx = (dpr->rx_jmb_prod_idx + cpycnt) &
5845 tp->rx_jmb_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005846 }
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005847
5848 return err;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005849}
5850
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005851static int tg3_poll_work(struct tg3_napi *tnapi, int work_done, int budget)
5852{
5853 struct tg3 *tp = tnapi->tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005854
5855 /* run TX completion thread */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005856 if (tnapi->hw_status->idx[0].tx_consumer != tnapi->tx_cons) {
Matt Carlson17375d22009-08-28 14:02:18 +00005857 tg3_tx(tnapi);
Joe Perches63c3a662011-04-26 08:12:10 +00005858 if (unlikely(tg3_flag(tp, TX_RECOVERY_PENDING)))
Michael Chan4fd7ab52007-10-12 01:39:50 -07005859 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005860 }
5861
Linus Torvalds1da177e2005-04-16 15:20:36 -07005862 /* run RX thread, within the bounds set by NAPI.
5863 * All RX "locking" is done by ensuring outside
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005864 * code synchronizes with tg3->napi.poll()
Linus Torvalds1da177e2005-04-16 15:20:36 -07005865 */
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00005866 if (*(tnapi->rx_rcb_prod_idx) != tnapi->rx_rcb_ptr)
Matt Carlson17375d22009-08-28 14:02:18 +00005867 work_done += tg3_rx(tnapi, budget - work_done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005868
Joe Perches63c3a662011-04-26 08:12:10 +00005869 if (tg3_flag(tp, ENABLE_RSS) && tnapi == &tp->napi[1]) {
Matt Carlson8fea32b2010-09-15 08:59:58 +00005870 struct tg3_rx_prodring_set *dpr = &tp->napi[0].prodring;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005871 int i, err = 0;
Matt Carlsone4af1af2010-02-12 14:47:05 +00005872 u32 std_prod_idx = dpr->rx_std_prod_idx;
5873 u32 jmb_prod_idx = dpr->rx_jmb_prod_idx;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005874
Matt Carlsone4af1af2010-02-12 14:47:05 +00005875 for (i = 1; i < tp->irq_cnt; i++)
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005876 err |= tg3_rx_prodring_xfer(tp, dpr,
Matt Carlson8fea32b2010-09-15 08:59:58 +00005877 &tp->napi[i].prodring);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005878
5879 wmb();
5880
Matt Carlsone4af1af2010-02-12 14:47:05 +00005881 if (std_prod_idx != dpr->rx_std_prod_idx)
5882 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
5883 dpr->rx_std_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005884
Matt Carlsone4af1af2010-02-12 14:47:05 +00005885 if (jmb_prod_idx != dpr->rx_jmb_prod_idx)
5886 tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG,
5887 dpr->rx_jmb_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005888
5889 mmiowb();
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005890
5891 if (err)
5892 tw32_f(HOSTCC_MODE, tp->coal_now);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005893 }
5894
David S. Miller6f535762007-10-11 18:08:29 -07005895 return work_done;
5896}
David S. Millerf7383c22005-05-18 22:50:53 -07005897
Matt Carlsondb219972011-11-04 09:15:03 +00005898static inline void tg3_reset_task_schedule(struct tg3 *tp)
5899{
5900 if (!test_and_set_bit(TG3_FLAG_RESET_TASK_PENDING, tp->tg3_flags))
5901 schedule_work(&tp->reset_task);
5902}
5903
5904static inline void tg3_reset_task_cancel(struct tg3 *tp)
5905{
5906 cancel_work_sync(&tp->reset_task);
5907 tg3_flag_clear(tp, RESET_TASK_PENDING);
5908}
5909
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005910static int tg3_poll_msix(struct napi_struct *napi, int budget)
5911{
5912 struct tg3_napi *tnapi = container_of(napi, struct tg3_napi, napi);
5913 struct tg3 *tp = tnapi->tp;
5914 int work_done = 0;
5915 struct tg3_hw_status *sblk = tnapi->hw_status;
5916
5917 while (1) {
5918 work_done = tg3_poll_work(tnapi, work_done, budget);
5919
Joe Perches63c3a662011-04-26 08:12:10 +00005920 if (unlikely(tg3_flag(tp, TX_RECOVERY_PENDING)))
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005921 goto tx_recovery;
5922
5923 if (unlikely(work_done >= budget))
5924 break;
5925
Matt Carlsonc6cdf432010-04-05 10:19:26 +00005926 /* tp->last_tag is used in tg3_int_reenable() below
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005927 * to tell the hw how much work has been processed,
5928 * so we must read it before checking for more work.
5929 */
5930 tnapi->last_tag = sblk->status_tag;
5931 tnapi->last_irq_tag = tnapi->last_tag;
5932 rmb();
5933
5934 /* check for RX/TX work to do */
Matt Carlson6d40db72010-04-05 10:19:20 +00005935 if (likely(sblk->idx[0].tx_consumer == tnapi->tx_cons &&
5936 *(tnapi->rx_rcb_prod_idx) == tnapi->rx_rcb_ptr)) {
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005937 napi_complete(napi);
5938 /* Reenable interrupts. */
5939 tw32_mailbox(tnapi->int_mbox, tnapi->last_tag << 24);
5940 mmiowb();
5941 break;
5942 }
5943 }
5944
5945 return work_done;
5946
5947tx_recovery:
5948 /* work_done is guaranteed to be less than budget. */
5949 napi_complete(napi);
Matt Carlsondb219972011-11-04 09:15:03 +00005950 tg3_reset_task_schedule(tp);
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005951 return work_done;
5952}
5953
Matt Carlsone64de4e2011-04-13 11:05:05 +00005954static void tg3_process_error(struct tg3 *tp)
5955{
5956 u32 val;
5957 bool real_error = false;
5958
Joe Perches63c3a662011-04-26 08:12:10 +00005959 if (tg3_flag(tp, ERROR_PROCESSED))
Matt Carlsone64de4e2011-04-13 11:05:05 +00005960 return;
5961
5962 /* Check Flow Attention register */
5963 val = tr32(HOSTCC_FLOW_ATTN);
5964 if (val & ~HOSTCC_FLOW_ATTN_MBUF_LWM) {
5965 netdev_err(tp->dev, "FLOW Attention error. Resetting chip.\n");
5966 real_error = true;
5967 }
5968
5969 if (tr32(MSGINT_STATUS) & ~MSGINT_STATUS_MSI_REQ) {
5970 netdev_err(tp->dev, "MSI Status error. Resetting chip.\n");
5971 real_error = true;
5972 }
5973
5974 if (tr32(RDMAC_STATUS) || tr32(WDMAC_STATUS)) {
5975 netdev_err(tp->dev, "DMA Status error. Resetting chip.\n");
5976 real_error = true;
5977 }
5978
5979 if (!real_error)
5980 return;
5981
5982 tg3_dump_state(tp);
5983
Joe Perches63c3a662011-04-26 08:12:10 +00005984 tg3_flag_set(tp, ERROR_PROCESSED);
Matt Carlsondb219972011-11-04 09:15:03 +00005985 tg3_reset_task_schedule(tp);
Matt Carlsone64de4e2011-04-13 11:05:05 +00005986}
5987
David S. Miller6f535762007-10-11 18:08:29 -07005988static int tg3_poll(struct napi_struct *napi, int budget)
5989{
Matt Carlson8ef04422009-08-28 14:01:37 +00005990 struct tg3_napi *tnapi = container_of(napi, struct tg3_napi, napi);
5991 struct tg3 *tp = tnapi->tp;
David S. Miller6f535762007-10-11 18:08:29 -07005992 int work_done = 0;
Matt Carlson898a56f2009-08-28 14:02:40 +00005993 struct tg3_hw_status *sblk = tnapi->hw_status;
David S. Miller6f535762007-10-11 18:08:29 -07005994
5995 while (1) {
Matt Carlsone64de4e2011-04-13 11:05:05 +00005996 if (sblk->status & SD_STATUS_ERROR)
5997 tg3_process_error(tp);
5998
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005999 tg3_poll_link(tp);
6000
Matt Carlson17375d22009-08-28 14:02:18 +00006001 work_done = tg3_poll_work(tnapi, work_done, budget);
David S. Miller6f535762007-10-11 18:08:29 -07006002
Joe Perches63c3a662011-04-26 08:12:10 +00006003 if (unlikely(tg3_flag(tp, TX_RECOVERY_PENDING)))
David S. Miller6f535762007-10-11 18:08:29 -07006004 goto tx_recovery;
6005
6006 if (unlikely(work_done >= budget))
6007 break;
6008
Joe Perches63c3a662011-04-26 08:12:10 +00006009 if (tg3_flag(tp, TAGGED_STATUS)) {
Matt Carlson17375d22009-08-28 14:02:18 +00006010 /* tp->last_tag is used in tg3_int_reenable() below
Michael Chan4fd7ab52007-10-12 01:39:50 -07006011 * to tell the hw how much work has been processed,
6012 * so we must read it before checking for more work.
6013 */
Matt Carlson898a56f2009-08-28 14:02:40 +00006014 tnapi->last_tag = sblk->status_tag;
6015 tnapi->last_irq_tag = tnapi->last_tag;
Michael Chan4fd7ab52007-10-12 01:39:50 -07006016 rmb();
6017 } else
6018 sblk->status &= ~SD_STATUS_UPDATED;
6019
Matt Carlson17375d22009-08-28 14:02:18 +00006020 if (likely(!tg3_has_work(tnapi))) {
Ben Hutchings288379f2009-01-19 16:43:59 -08006021 napi_complete(napi);
Matt Carlson17375d22009-08-28 14:02:18 +00006022 tg3_int_reenable(tnapi);
David S. Miller6f535762007-10-11 18:08:29 -07006023 break;
6024 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006025 }
6026
Stephen Hemmingerbea33482007-10-03 16:41:36 -07006027 return work_done;
David S. Miller6f535762007-10-11 18:08:29 -07006028
6029tx_recovery:
Michael Chan4fd7ab52007-10-12 01:39:50 -07006030 /* work_done is guaranteed to be less than budget. */
Ben Hutchings288379f2009-01-19 16:43:59 -08006031 napi_complete(napi);
Matt Carlsondb219972011-11-04 09:15:03 +00006032 tg3_reset_task_schedule(tp);
Michael Chan4fd7ab52007-10-12 01:39:50 -07006033 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006034}
6035
Matt Carlson66cfd1b2010-09-30 10:34:30 +00006036static void tg3_napi_disable(struct tg3 *tp)
6037{
6038 int i;
6039
6040 for (i = tp->irq_cnt - 1; i >= 0; i--)
6041 napi_disable(&tp->napi[i].napi);
6042}
6043
6044static void tg3_napi_enable(struct tg3 *tp)
6045{
6046 int i;
6047
6048 for (i = 0; i < tp->irq_cnt; i++)
6049 napi_enable(&tp->napi[i].napi);
6050}
6051
6052static void tg3_napi_init(struct tg3 *tp)
6053{
6054 int i;
6055
6056 netif_napi_add(tp->dev, &tp->napi[0].napi, tg3_poll, 64);
6057 for (i = 1; i < tp->irq_cnt; i++)
6058 netif_napi_add(tp->dev, &tp->napi[i].napi, tg3_poll_msix, 64);
6059}
6060
6061static void tg3_napi_fini(struct tg3 *tp)
6062{
6063 int i;
6064
6065 for (i = 0; i < tp->irq_cnt; i++)
6066 netif_napi_del(&tp->napi[i].napi);
6067}
6068
6069static inline void tg3_netif_stop(struct tg3 *tp)
6070{
6071 tp->dev->trans_start = jiffies; /* prevent tx timeout */
6072 tg3_napi_disable(tp);
6073 netif_tx_disable(tp->dev);
6074}
6075
6076static inline void tg3_netif_start(struct tg3 *tp)
6077{
6078 /* NOTE: unconditional netif_tx_wake_all_queues is only
6079 * appropriate so long as all callers are assured to
6080 * have free tx slots (such as after tg3_init_hw)
6081 */
6082 netif_tx_wake_all_queues(tp->dev);
6083
6084 tg3_napi_enable(tp);
6085 tp->napi[0].hw_status->status |= SD_STATUS_UPDATED;
6086 tg3_enable_ints(tp);
6087}
6088
David S. Millerf47c11e2005-06-24 20:18:35 -07006089static void tg3_irq_quiesce(struct tg3 *tp)
6090{
Matt Carlson4f125f42009-09-01 12:55:02 +00006091 int i;
6092
David S. Millerf47c11e2005-06-24 20:18:35 -07006093 BUG_ON(tp->irq_sync);
6094
6095 tp->irq_sync = 1;
6096 smp_mb();
6097
Matt Carlson4f125f42009-09-01 12:55:02 +00006098 for (i = 0; i < tp->irq_cnt; i++)
6099 synchronize_irq(tp->napi[i].irq_vec);
David S. Millerf47c11e2005-06-24 20:18:35 -07006100}
6101
David S. Millerf47c11e2005-06-24 20:18:35 -07006102/* Fully shutdown all tg3 driver activity elsewhere in the system.
6103 * If irq_sync is non-zero, then the IRQ handler must be synchronized
6104 * with as well. Most of the time, this is not necessary except when
6105 * shutting down the device.
6106 */
6107static inline void tg3_full_lock(struct tg3 *tp, int irq_sync)
6108{
Michael Chan46966542007-07-11 19:47:19 -07006109 spin_lock_bh(&tp->lock);
David S. Millerf47c11e2005-06-24 20:18:35 -07006110 if (irq_sync)
6111 tg3_irq_quiesce(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -07006112}
6113
6114static inline void tg3_full_unlock(struct tg3 *tp)
6115{
David S. Millerf47c11e2005-06-24 20:18:35 -07006116 spin_unlock_bh(&tp->lock);
6117}
6118
Michael Chanfcfa0a32006-03-20 22:28:41 -08006119/* One-shot MSI handler - Chip automatically disables interrupt
6120 * after sending MSI so driver doesn't have to do it.
6121 */
David Howells7d12e782006-10-05 14:55:46 +01006122static irqreturn_t tg3_msi_1shot(int irq, void *dev_id)
Michael Chanfcfa0a32006-03-20 22:28:41 -08006123{
Matt Carlson09943a12009-08-28 14:01:57 +00006124 struct tg3_napi *tnapi = dev_id;
6125 struct tg3 *tp = tnapi->tp;
Michael Chanfcfa0a32006-03-20 22:28:41 -08006126
Matt Carlson898a56f2009-08-28 14:02:40 +00006127 prefetch(tnapi->hw_status);
Matt Carlson0c1d0e22009-09-01 13:16:33 +00006128 if (tnapi->rx_rcb)
6129 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Michael Chanfcfa0a32006-03-20 22:28:41 -08006130
6131 if (likely(!tg3_irq_sync(tp)))
Matt Carlson09943a12009-08-28 14:01:57 +00006132 napi_schedule(&tnapi->napi);
Michael Chanfcfa0a32006-03-20 22:28:41 -08006133
6134 return IRQ_HANDLED;
6135}
6136
Michael Chan88b06bc2005-04-21 17:13:25 -07006137/* MSI ISR - No need to check for interrupt sharing and no need to
6138 * flush status block and interrupt mailbox. PCI ordering rules
6139 * guarantee that MSI will arrive after the status block.
6140 */
David Howells7d12e782006-10-05 14:55:46 +01006141static irqreturn_t tg3_msi(int irq, void *dev_id)
Michael Chan88b06bc2005-04-21 17:13:25 -07006142{
Matt Carlson09943a12009-08-28 14:01:57 +00006143 struct tg3_napi *tnapi = dev_id;
6144 struct tg3 *tp = tnapi->tp;
Michael Chan88b06bc2005-04-21 17:13:25 -07006145
Matt Carlson898a56f2009-08-28 14:02:40 +00006146 prefetch(tnapi->hw_status);
Matt Carlson0c1d0e22009-09-01 13:16:33 +00006147 if (tnapi->rx_rcb)
6148 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Michael Chan88b06bc2005-04-21 17:13:25 -07006149 /*
David S. Millerfac9b832005-05-18 22:46:34 -07006150 * Writing any value to intr-mbox-0 clears PCI INTA# and
Michael Chan88b06bc2005-04-21 17:13:25 -07006151 * chip-internal interrupt pending events.
David S. Millerfac9b832005-05-18 22:46:34 -07006152 * Writing non-zero to intr-mbox-0 additional tells the
Michael Chan88b06bc2005-04-21 17:13:25 -07006153 * NIC to stop sending us irqs, engaging "in-intr-handler"
6154 * event coalescing.
6155 */
Matt Carlson5b39de92011-08-31 11:44:50 +00006156 tw32_mailbox(tnapi->int_mbox, 0x00000001);
Michael Chan61487482005-09-05 17:53:19 -07006157 if (likely(!tg3_irq_sync(tp)))
Matt Carlson09943a12009-08-28 14:01:57 +00006158 napi_schedule(&tnapi->napi);
Michael Chan61487482005-09-05 17:53:19 -07006159
Michael Chan88b06bc2005-04-21 17:13:25 -07006160 return IRQ_RETVAL(1);
6161}
6162
David Howells7d12e782006-10-05 14:55:46 +01006163static irqreturn_t tg3_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006164{
Matt Carlson09943a12009-08-28 14:01:57 +00006165 struct tg3_napi *tnapi = dev_id;
6166 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00006167 struct tg3_hw_status *sblk = tnapi->hw_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006168 unsigned int handled = 1;
6169
Linus Torvalds1da177e2005-04-16 15:20:36 -07006170 /* In INTx mode, it is possible for the interrupt to arrive at
6171 * the CPU before the status block posted prior to the interrupt.
6172 * Reading the PCI State register will confirm whether the
6173 * interrupt is ours and will flush the status block.
6174 */
Michael Chand18edcb2007-03-24 20:57:11 -07006175 if (unlikely(!(sblk->status & SD_STATUS_UPDATED))) {
Joe Perches63c3a662011-04-26 08:12:10 +00006176 if (tg3_flag(tp, CHIP_RESETTING) ||
Michael Chand18edcb2007-03-24 20:57:11 -07006177 (tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
6178 handled = 0;
David S. Millerf47c11e2005-06-24 20:18:35 -07006179 goto out;
David S. Millerfac9b832005-05-18 22:46:34 -07006180 }
Michael Chand18edcb2007-03-24 20:57:11 -07006181 }
6182
6183 /*
6184 * Writing any value to intr-mbox-0 clears PCI INTA# and
6185 * chip-internal interrupt pending events.
6186 * Writing non-zero to intr-mbox-0 additional tells the
6187 * NIC to stop sending us irqs, engaging "in-intr-handler"
6188 * event coalescing.
Michael Chanc04cb342007-05-07 00:26:15 -07006189 *
6190 * Flush the mailbox to de-assert the IRQ immediately to prevent
6191 * spurious interrupts. The flush impacts performance but
6192 * excessive spurious interrupts can be worse in some cases.
Michael Chand18edcb2007-03-24 20:57:11 -07006193 */
Michael Chanc04cb342007-05-07 00:26:15 -07006194 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 0x00000001);
Michael Chand18edcb2007-03-24 20:57:11 -07006195 if (tg3_irq_sync(tp))
6196 goto out;
6197 sblk->status &= ~SD_STATUS_UPDATED;
Matt Carlson17375d22009-08-28 14:02:18 +00006198 if (likely(tg3_has_work(tnapi))) {
Matt Carlson72334482009-08-28 14:03:01 +00006199 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Matt Carlson09943a12009-08-28 14:01:57 +00006200 napi_schedule(&tnapi->napi);
Michael Chand18edcb2007-03-24 20:57:11 -07006201 } else {
6202 /* No work, shared interrupt perhaps? re-enable
6203 * interrupts, and flush that PCI write
6204 */
6205 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW,
6206 0x00000000);
David S. Millerfac9b832005-05-18 22:46:34 -07006207 }
David S. Millerf47c11e2005-06-24 20:18:35 -07006208out:
David S. Millerfac9b832005-05-18 22:46:34 -07006209 return IRQ_RETVAL(handled);
6210}
6211
David Howells7d12e782006-10-05 14:55:46 +01006212static irqreturn_t tg3_interrupt_tagged(int irq, void *dev_id)
David S. Millerfac9b832005-05-18 22:46:34 -07006213{
Matt Carlson09943a12009-08-28 14:01:57 +00006214 struct tg3_napi *tnapi = dev_id;
6215 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00006216 struct tg3_hw_status *sblk = tnapi->hw_status;
David S. Millerfac9b832005-05-18 22:46:34 -07006217 unsigned int handled = 1;
6218
David S. Millerfac9b832005-05-18 22:46:34 -07006219 /* In INTx mode, it is possible for the interrupt to arrive at
6220 * the CPU before the status block posted prior to the interrupt.
6221 * Reading the PCI State register will confirm whether the
6222 * interrupt is ours and will flush the status block.
6223 */
Matt Carlson898a56f2009-08-28 14:02:40 +00006224 if (unlikely(sblk->status_tag == tnapi->last_irq_tag)) {
Joe Perches63c3a662011-04-26 08:12:10 +00006225 if (tg3_flag(tp, CHIP_RESETTING) ||
Michael Chand18edcb2007-03-24 20:57:11 -07006226 (tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
6227 handled = 0;
David S. Millerf47c11e2005-06-24 20:18:35 -07006228 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006229 }
Michael Chand18edcb2007-03-24 20:57:11 -07006230 }
6231
6232 /*
6233 * writing any value to intr-mbox-0 clears PCI INTA# and
6234 * chip-internal interrupt pending events.
6235 * writing non-zero to intr-mbox-0 additional tells the
6236 * NIC to stop sending us irqs, engaging "in-intr-handler"
6237 * event coalescing.
Michael Chanc04cb342007-05-07 00:26:15 -07006238 *
6239 * Flush the mailbox to de-assert the IRQ immediately to prevent
6240 * spurious interrupts. The flush impacts performance but
6241 * excessive spurious interrupts can be worse in some cases.
Michael Chand18edcb2007-03-24 20:57:11 -07006242 */
Michael Chanc04cb342007-05-07 00:26:15 -07006243 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 0x00000001);
Matt Carlson624f8e52009-04-20 06:55:01 +00006244
6245 /*
6246 * In a shared interrupt configuration, sometimes other devices'
6247 * interrupts will scream. We record the current status tag here
6248 * so that the above check can report that the screaming interrupts
6249 * are unhandled. Eventually they will be silenced.
6250 */
Matt Carlson898a56f2009-08-28 14:02:40 +00006251 tnapi->last_irq_tag = sblk->status_tag;
Matt Carlson624f8e52009-04-20 06:55:01 +00006252
Michael Chand18edcb2007-03-24 20:57:11 -07006253 if (tg3_irq_sync(tp))
6254 goto out;
Matt Carlson624f8e52009-04-20 06:55:01 +00006255
Matt Carlson72334482009-08-28 14:03:01 +00006256 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Matt Carlson624f8e52009-04-20 06:55:01 +00006257
Matt Carlson09943a12009-08-28 14:01:57 +00006258 napi_schedule(&tnapi->napi);
Matt Carlson624f8e52009-04-20 06:55:01 +00006259
David S. Millerf47c11e2005-06-24 20:18:35 -07006260out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006261 return IRQ_RETVAL(handled);
6262}
6263
Michael Chan79381092005-04-21 17:13:59 -07006264/* ISR for interrupt test */
David Howells7d12e782006-10-05 14:55:46 +01006265static irqreturn_t tg3_test_isr(int irq, void *dev_id)
Michael Chan79381092005-04-21 17:13:59 -07006266{
Matt Carlson09943a12009-08-28 14:01:57 +00006267 struct tg3_napi *tnapi = dev_id;
6268 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00006269 struct tg3_hw_status *sblk = tnapi->hw_status;
Michael Chan79381092005-04-21 17:13:59 -07006270
Michael Chanf9804dd2005-09-27 12:13:10 -07006271 if ((sblk->status & SD_STATUS_UPDATED) ||
6272 !(tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
Michael Chanb16250e2006-09-27 16:10:14 -07006273 tg3_disable_ints(tp);
Michael Chan79381092005-04-21 17:13:59 -07006274 return IRQ_RETVAL(1);
6275 }
6276 return IRQ_RETVAL(0);
6277}
6278
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07006279static int tg3_init_hw(struct tg3 *, int);
Michael Chan944d9802005-05-29 14:57:48 -07006280static int tg3_halt(struct tg3 *, int, int);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006281
Michael Chanb9ec6c12006-07-25 16:37:27 -07006282/* Restart hardware after configuration changes, self-test, etc.
6283 * Invoked with tp->lock held.
6284 */
6285static int tg3_restart_hw(struct tg3 *tp, int reset_phy)
Eric Dumazet78c61462008-04-24 23:33:06 -07006286 __releases(tp->lock)
6287 __acquires(tp->lock)
Michael Chanb9ec6c12006-07-25 16:37:27 -07006288{
6289 int err;
6290
6291 err = tg3_init_hw(tp, reset_phy);
6292 if (err) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00006293 netdev_err(tp->dev,
6294 "Failed to re-initialize device, aborting\n");
Michael Chanb9ec6c12006-07-25 16:37:27 -07006295 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
6296 tg3_full_unlock(tp);
6297 del_timer_sync(&tp->timer);
6298 tp->irq_sync = 0;
Matt Carlsonfed97812009-09-01 13:10:19 +00006299 tg3_napi_enable(tp);
Michael Chanb9ec6c12006-07-25 16:37:27 -07006300 dev_close(tp->dev);
6301 tg3_full_lock(tp, 0);
6302 }
6303 return err;
6304}
6305
Linus Torvalds1da177e2005-04-16 15:20:36 -07006306#ifdef CONFIG_NET_POLL_CONTROLLER
6307static void tg3_poll_controller(struct net_device *dev)
6308{
Matt Carlson4f125f42009-09-01 12:55:02 +00006309 int i;
Michael Chan88b06bc2005-04-21 17:13:25 -07006310 struct tg3 *tp = netdev_priv(dev);
6311
Matt Carlson4f125f42009-09-01 12:55:02 +00006312 for (i = 0; i < tp->irq_cnt; i++)
Louis Rillingfe234f02010-03-09 06:14:41 +00006313 tg3_interrupt(tp->napi[i].irq_vec, &tp->napi[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006314}
6315#endif
6316
David Howellsc4028952006-11-22 14:57:56 +00006317static void tg3_reset_task(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006318{
David Howellsc4028952006-11-22 14:57:56 +00006319 struct tg3 *tp = container_of(work, struct tg3, reset_task);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07006320 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006321
Michael Chan7faa0062006-02-02 17:29:28 -08006322 tg3_full_lock(tp, 0);
Michael Chan7faa0062006-02-02 17:29:28 -08006323
6324 if (!netif_running(tp->dev)) {
Matt Carlsondb219972011-11-04 09:15:03 +00006325 tg3_flag_clear(tp, RESET_TASK_PENDING);
Michael Chan7faa0062006-02-02 17:29:28 -08006326 tg3_full_unlock(tp);
6327 return;
6328 }
6329
6330 tg3_full_unlock(tp);
6331
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07006332 tg3_phy_stop(tp);
6333
Linus Torvalds1da177e2005-04-16 15:20:36 -07006334 tg3_netif_stop(tp);
6335
David S. Millerf47c11e2005-06-24 20:18:35 -07006336 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006337
Joe Perches63c3a662011-04-26 08:12:10 +00006338 if (tg3_flag(tp, TX_RECOVERY_PENDING)) {
Michael Chandf3e6542006-05-26 17:48:07 -07006339 tp->write32_tx_mbox = tg3_write32_tx_mbox;
6340 tp->write32_rx_mbox = tg3_write_flush_reg32;
Joe Perches63c3a662011-04-26 08:12:10 +00006341 tg3_flag_set(tp, MBOX_WRITE_REORDER);
6342 tg3_flag_clear(tp, TX_RECOVERY_PENDING);
Michael Chandf3e6542006-05-26 17:48:07 -07006343 }
6344
Michael Chan944d9802005-05-29 14:57:48 -07006345 tg3_halt(tp, RESET_KIND_SHUTDOWN, 0);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07006346 err = tg3_init_hw(tp, 1);
6347 if (err)
Michael Chanb9ec6c12006-07-25 16:37:27 -07006348 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006349
6350 tg3_netif_start(tp);
6351
Michael Chanb9ec6c12006-07-25 16:37:27 -07006352out:
Michael Chan7faa0062006-02-02 17:29:28 -08006353 tg3_full_unlock(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07006354
6355 if (!err)
6356 tg3_phy_start(tp);
Matt Carlsondb219972011-11-04 09:15:03 +00006357
6358 tg3_flag_clear(tp, RESET_TASK_PENDING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006359}
6360
6361static void tg3_tx_timeout(struct net_device *dev)
6362{
6363 struct tg3 *tp = netdev_priv(dev);
6364
Michael Chanb0408752007-02-13 12:18:30 -08006365 if (netif_msg_tx_err(tp)) {
Joe Perches05dbe002010-02-17 19:44:19 +00006366 netdev_err(dev, "transmit timed out, resetting\n");
Matt Carlson97bd8e42011-04-13 11:05:04 +00006367 tg3_dump_state(tp);
Michael Chanb0408752007-02-13 12:18:30 -08006368 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006369
Matt Carlsondb219972011-11-04 09:15:03 +00006370 tg3_reset_task_schedule(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006371}
6372
Michael Chanc58ec932005-09-17 00:46:27 -07006373/* Test for DMA buffers crossing any 4GB boundaries: 4G, 8G, etc */
6374static inline int tg3_4g_overflow_test(dma_addr_t mapping, int len)
6375{
6376 u32 base = (u32) mapping & 0xffffffff;
6377
Eric Dumazet807540b2010-09-23 05:40:09 +00006378 return (base > 0xffffdcc0) && (base + len + 8 < base);
Michael Chanc58ec932005-09-17 00:46:27 -07006379}
6380
Michael Chan72f2afb2006-03-06 19:28:35 -08006381/* Test for DMA addresses > 40-bit */
6382static inline int tg3_40bit_overflow_test(struct tg3 *tp, dma_addr_t mapping,
6383 int len)
6384{
6385#if defined(CONFIG_HIGHMEM) && (BITS_PER_LONG == 64)
Joe Perches63c3a662011-04-26 08:12:10 +00006386 if (tg3_flag(tp, 40BIT_DMA_BUG))
Eric Dumazet807540b2010-09-23 05:40:09 +00006387 return ((u64) mapping + len) > DMA_BIT_MASK(40);
Michael Chan72f2afb2006-03-06 19:28:35 -08006388 return 0;
6389#else
6390 return 0;
6391#endif
6392}
6393
Matt Carlsond1a3b732011-07-27 14:20:51 +00006394static inline void tg3_tx_set_bd(struct tg3_tx_buffer_desc *txbd,
Matt Carlson92cd3a12011-07-27 14:20:47 +00006395 dma_addr_t mapping, u32 len, u32 flags,
6396 u32 mss, u32 vlan)
Matt Carlson2ffcc982011-05-19 12:12:44 +00006397{
Matt Carlson92cd3a12011-07-27 14:20:47 +00006398 txbd->addr_hi = ((u64) mapping >> 32);
6399 txbd->addr_lo = ((u64) mapping & 0xffffffff);
6400 txbd->len_flags = (len << TXD_LEN_SHIFT) | (flags & 0x0000ffff);
6401 txbd->vlan_tag = (mss << TXD_MSS_SHIFT) | (vlan << TXD_VLAN_TAG_SHIFT);
Matt Carlson2ffcc982011-05-19 12:12:44 +00006402}
Linus Torvalds1da177e2005-04-16 15:20:36 -07006403
Matt Carlson84b67b22011-07-27 14:20:52 +00006404static bool tg3_tx_frag_set(struct tg3_napi *tnapi, u32 *entry, u32 *budget,
Matt Carlsond1a3b732011-07-27 14:20:51 +00006405 dma_addr_t map, u32 len, u32 flags,
6406 u32 mss, u32 vlan)
6407{
6408 struct tg3 *tp = tnapi->tp;
6409 bool hwbug = false;
6410
6411 if (tg3_flag(tp, SHORT_DMA_BUG) && len <= 8)
6412 hwbug = 1;
6413
6414 if (tg3_4g_overflow_test(map, len))
6415 hwbug = 1;
6416
6417 if (tg3_40bit_overflow_test(tp, map, len))
6418 hwbug = 1;
6419
Matt Carlsone31aa982011-07-27 14:20:53 +00006420 if (tg3_flag(tp, 4K_FIFO_LIMIT)) {
Matt Carlsonb9e45482011-11-04 09:14:59 +00006421 u32 prvidx = *entry;
Matt Carlsone31aa982011-07-27 14:20:53 +00006422 u32 tmp_flag = flags & ~TXD_FLAG_END;
Matt Carlsonb9e45482011-11-04 09:14:59 +00006423 while (len > TG3_TX_BD_DMA_MAX && *budget) {
Matt Carlsone31aa982011-07-27 14:20:53 +00006424 u32 frag_len = TG3_TX_BD_DMA_MAX;
6425 len -= TG3_TX_BD_DMA_MAX;
6426
Matt Carlsonb9e45482011-11-04 09:14:59 +00006427 /* Avoid the 8byte DMA problem */
6428 if (len <= 8) {
6429 len += TG3_TX_BD_DMA_MAX / 2;
6430 frag_len = TG3_TX_BD_DMA_MAX / 2;
Matt Carlsone31aa982011-07-27 14:20:53 +00006431 }
6432
Matt Carlsonb9e45482011-11-04 09:14:59 +00006433 tnapi->tx_buffers[*entry].fragmented = true;
6434
6435 tg3_tx_set_bd(&tnapi->tx_ring[*entry], map,
6436 frag_len, tmp_flag, mss, vlan);
6437 *budget -= 1;
6438 prvidx = *entry;
6439 *entry = NEXT_TX(*entry);
6440
Matt Carlsone31aa982011-07-27 14:20:53 +00006441 map += frag_len;
6442 }
6443
6444 if (len) {
6445 if (*budget) {
6446 tg3_tx_set_bd(&tnapi->tx_ring[*entry], map,
6447 len, flags, mss, vlan);
Matt Carlsonb9e45482011-11-04 09:14:59 +00006448 *budget -= 1;
Matt Carlsone31aa982011-07-27 14:20:53 +00006449 *entry = NEXT_TX(*entry);
6450 } else {
6451 hwbug = 1;
Matt Carlsonb9e45482011-11-04 09:14:59 +00006452 tnapi->tx_buffers[prvidx].fragmented = false;
Matt Carlsone31aa982011-07-27 14:20:53 +00006453 }
6454 }
6455 } else {
Matt Carlson84b67b22011-07-27 14:20:52 +00006456 tg3_tx_set_bd(&tnapi->tx_ring[*entry], map,
6457 len, flags, mss, vlan);
Matt Carlsone31aa982011-07-27 14:20:53 +00006458 *entry = NEXT_TX(*entry);
6459 }
Matt Carlsond1a3b732011-07-27 14:20:51 +00006460
6461 return hwbug;
6462}
6463
Matt Carlson0d681b22011-07-27 14:20:49 +00006464static void tg3_tx_skb_unmap(struct tg3_napi *tnapi, u32 entry, int last)
Matt Carlson432aa7e2011-05-19 12:12:45 +00006465{
6466 int i;
Matt Carlson0d681b22011-07-27 14:20:49 +00006467 struct sk_buff *skb;
Matt Carlsondf8944c2011-07-27 14:20:46 +00006468 struct tg3_tx_ring_info *txb = &tnapi->tx_buffers[entry];
Matt Carlson432aa7e2011-05-19 12:12:45 +00006469
Matt Carlson0d681b22011-07-27 14:20:49 +00006470 skb = txb->skb;
6471 txb->skb = NULL;
6472
Matt Carlson432aa7e2011-05-19 12:12:45 +00006473 pci_unmap_single(tnapi->tp->pdev,
6474 dma_unmap_addr(txb, mapping),
6475 skb_headlen(skb),
6476 PCI_DMA_TODEVICE);
Matt Carlsone01ee142011-07-27 14:20:50 +00006477
6478 while (txb->fragmented) {
6479 txb->fragmented = false;
6480 entry = NEXT_TX(entry);
6481 txb = &tnapi->tx_buffers[entry];
6482 }
6483
Matt Carlsonba1142e2011-11-04 09:15:00 +00006484 for (i = 0; i <= last; i++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00006485 const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
Matt Carlson432aa7e2011-05-19 12:12:45 +00006486
6487 entry = NEXT_TX(entry);
6488 txb = &tnapi->tx_buffers[entry];
6489
6490 pci_unmap_page(tnapi->tp->pdev,
6491 dma_unmap_addr(txb, mapping),
Eric Dumazet9e903e02011-10-18 21:00:24 +00006492 skb_frag_size(frag), PCI_DMA_TODEVICE);
Matt Carlsone01ee142011-07-27 14:20:50 +00006493
6494 while (txb->fragmented) {
6495 txb->fragmented = false;
6496 entry = NEXT_TX(entry);
6497 txb = &tnapi->tx_buffers[entry];
6498 }
Matt Carlson432aa7e2011-05-19 12:12:45 +00006499 }
6500}
6501
Michael Chan72f2afb2006-03-06 19:28:35 -08006502/* Workaround 4GB and 40-bit hardware DMA bugs. */
Matt Carlson24f4efd2009-11-13 13:03:35 +00006503static int tigon3_dma_hwbug_workaround(struct tg3_napi *tnapi,
David S. Miller1805b2f2011-10-24 18:18:09 -04006504 struct sk_buff **pskb,
Matt Carlson84b67b22011-07-27 14:20:52 +00006505 u32 *entry, u32 *budget,
Matt Carlson92cd3a12011-07-27 14:20:47 +00006506 u32 base_flags, u32 mss, u32 vlan)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006507{
Matt Carlson24f4efd2009-11-13 13:03:35 +00006508 struct tg3 *tp = tnapi->tp;
David S. Miller1805b2f2011-10-24 18:18:09 -04006509 struct sk_buff *new_skb, *skb = *pskb;
Michael Chanc58ec932005-09-17 00:46:27 -07006510 dma_addr_t new_addr = 0;
Matt Carlson432aa7e2011-05-19 12:12:45 +00006511 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006512
Matt Carlson41588ba2008-04-19 18:12:33 -07006513 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701)
6514 new_skb = skb_copy(skb, GFP_ATOMIC);
6515 else {
6516 int more_headroom = 4 - ((unsigned long)skb->data & 3);
6517
6518 new_skb = skb_copy_expand(skb,
6519 skb_headroom(skb) + more_headroom,
6520 skb_tailroom(skb), GFP_ATOMIC);
6521 }
6522
Linus Torvalds1da177e2005-04-16 15:20:36 -07006523 if (!new_skb) {
Michael Chanc58ec932005-09-17 00:46:27 -07006524 ret = -1;
6525 } else {
6526 /* New SKB is guaranteed to be linear. */
Alexander Duyckf4188d82009-12-02 16:48:38 +00006527 new_addr = pci_map_single(tp->pdev, new_skb->data, new_skb->len,
6528 PCI_DMA_TODEVICE);
6529 /* Make sure the mapping succeeded */
6530 if (pci_dma_mapping_error(tp->pdev, new_addr)) {
Alexander Duyckf4188d82009-12-02 16:48:38 +00006531 dev_kfree_skb(new_skb);
Michael Chanc58ec932005-09-17 00:46:27 -07006532 ret = -1;
Michael Chanc58ec932005-09-17 00:46:27 -07006533 } else {
Matt Carlsonb9e45482011-11-04 09:14:59 +00006534 u32 save_entry = *entry;
6535
Matt Carlson92cd3a12011-07-27 14:20:47 +00006536 base_flags |= TXD_FLAG_END;
6537
Matt Carlson84b67b22011-07-27 14:20:52 +00006538 tnapi->tx_buffers[*entry].skb = new_skb;
6539 dma_unmap_addr_set(&tnapi->tx_buffers[*entry],
Matt Carlson432aa7e2011-05-19 12:12:45 +00006540 mapping, new_addr);
6541
Matt Carlson84b67b22011-07-27 14:20:52 +00006542 if (tg3_tx_frag_set(tnapi, entry, budget, new_addr,
Matt Carlsond1a3b732011-07-27 14:20:51 +00006543 new_skb->len, base_flags,
6544 mss, vlan)) {
Matt Carlsonba1142e2011-11-04 09:15:00 +00006545 tg3_tx_skb_unmap(tnapi, save_entry, -1);
Matt Carlsond1a3b732011-07-27 14:20:51 +00006546 dev_kfree_skb(new_skb);
6547 ret = -1;
6548 }
Michael Chanc58ec932005-09-17 00:46:27 -07006549 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006550 }
6551
Linus Torvalds1da177e2005-04-16 15:20:36 -07006552 dev_kfree_skb(skb);
David S. Miller1805b2f2011-10-24 18:18:09 -04006553 *pskb = new_skb;
Michael Chanc58ec932005-09-17 00:46:27 -07006554 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006555}
6556
Matt Carlson2ffcc982011-05-19 12:12:44 +00006557static netdev_tx_t tg3_start_xmit(struct sk_buff *, struct net_device *);
Michael Chan52c0fd82006-06-29 20:15:54 -07006558
6559/* Use GSO to workaround a rare TSO bug that may be triggered when the
6560 * TSO header is greater than 80 bytes.
6561 */
6562static int tg3_tso_bug(struct tg3 *tp, struct sk_buff *skb)
6563{
6564 struct sk_buff *segs, *nskb;
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006565 u32 frag_cnt_est = skb_shinfo(skb)->gso_segs * 3;
Michael Chan52c0fd82006-06-29 20:15:54 -07006566
6567 /* Estimate the number of fragments in the worst case */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006568 if (unlikely(tg3_tx_avail(&tp->napi[0]) <= frag_cnt_est)) {
Michael Chan52c0fd82006-06-29 20:15:54 -07006569 netif_stop_queue(tp->dev);
Matt Carlsonf65aac12010-08-02 11:26:03 +00006570
6571 /* netif_tx_stop_queue() must be done before checking
6572 * checking tx index in tg3_tx_avail() below, because in
6573 * tg3_tx(), we update tx index before checking for
6574 * netif_tx_queue_stopped().
6575 */
6576 smp_mb();
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006577 if (tg3_tx_avail(&tp->napi[0]) <= frag_cnt_est)
Michael Chan7f62ad52007-02-20 23:25:40 -08006578 return NETDEV_TX_BUSY;
6579
6580 netif_wake_queue(tp->dev);
Michael Chan52c0fd82006-06-29 20:15:54 -07006581 }
6582
6583 segs = skb_gso_segment(skb, tp->dev->features & ~NETIF_F_TSO);
Hirofumi Nakagawa801678c2008-04-29 01:03:09 -07006584 if (IS_ERR(segs))
Michael Chan52c0fd82006-06-29 20:15:54 -07006585 goto tg3_tso_bug_end;
6586
6587 do {
6588 nskb = segs;
6589 segs = segs->next;
6590 nskb->next = NULL;
Matt Carlson2ffcc982011-05-19 12:12:44 +00006591 tg3_start_xmit(nskb, tp->dev);
Michael Chan52c0fd82006-06-29 20:15:54 -07006592 } while (segs);
6593
6594tg3_tso_bug_end:
6595 dev_kfree_skb(skb);
6596
6597 return NETDEV_TX_OK;
6598}
Michael Chan52c0fd82006-06-29 20:15:54 -07006599
Michael Chan5a6f3072006-03-20 22:28:05 -08006600/* hard_start_xmit for devices that have the 4G bug and/or 40-bit bug and
Joe Perches63c3a662011-04-26 08:12:10 +00006601 * support TG3_FLAG_HW_TSO_1 or firmware TSO only.
Michael Chan5a6f3072006-03-20 22:28:05 -08006602 */
Matt Carlson2ffcc982011-05-19 12:12:44 +00006603static netdev_tx_t tg3_start_xmit(struct sk_buff *skb, struct net_device *dev)
Michael Chan5a6f3072006-03-20 22:28:05 -08006604{
6605 struct tg3 *tp = netdev_priv(dev);
Matt Carlson92cd3a12011-07-27 14:20:47 +00006606 u32 len, entry, base_flags, mss, vlan = 0;
Matt Carlson84b67b22011-07-27 14:20:52 +00006607 u32 budget;
Matt Carlson432aa7e2011-05-19 12:12:45 +00006608 int i = -1, would_hit_hwbug;
David S. Miller90079ce2008-09-11 04:52:51 -07006609 dma_addr_t mapping;
Matt Carlson24f4efd2009-11-13 13:03:35 +00006610 struct tg3_napi *tnapi;
6611 struct netdev_queue *txq;
Matt Carlson432aa7e2011-05-19 12:12:45 +00006612 unsigned int last;
Alexander Duyckf4188d82009-12-02 16:48:38 +00006613
Matt Carlson24f4efd2009-11-13 13:03:35 +00006614 txq = netdev_get_tx_queue(dev, skb_get_queue_mapping(skb));
6615 tnapi = &tp->napi[skb_get_queue_mapping(skb)];
Joe Perches63c3a662011-04-26 08:12:10 +00006616 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlson24f4efd2009-11-13 13:03:35 +00006617 tnapi++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006618
Matt Carlson84b67b22011-07-27 14:20:52 +00006619 budget = tg3_tx_avail(tnapi);
6620
Michael Chan00b70502006-06-17 21:58:45 -07006621 /* We are running in BH disabled context with netif_tx_lock
Stephen Hemmingerbea33482007-10-03 16:41:36 -07006622 * and TX reclaim runs via tp->napi.poll inside of a software
David S. Millerf47c11e2005-06-24 20:18:35 -07006623 * interrupt. Furthermore, IRQ processing runs lockless so we have
6624 * no IRQ context deadlocks to worry about either. Rejoice!
Linus Torvalds1da177e2005-04-16 15:20:36 -07006625 */
Matt Carlson84b67b22011-07-27 14:20:52 +00006626 if (unlikely(budget <= (skb_shinfo(skb)->nr_frags + 1))) {
Matt Carlson24f4efd2009-11-13 13:03:35 +00006627 if (!netif_tx_queue_stopped(txq)) {
6628 netif_tx_stop_queue(txq);
Stephen Hemminger1f064a82005-12-06 17:36:44 -08006629
6630 /* This is a hard error, log it. */
Matt Carlson5129c3a2010-04-05 10:19:23 +00006631 netdev_err(dev,
6632 "BUG! Tx Ring full when queue awake!\n");
Stephen Hemminger1f064a82005-12-06 17:36:44 -08006633 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006634 return NETDEV_TX_BUSY;
6635 }
6636
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006637 entry = tnapi->tx_prod;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006638 base_flags = 0;
Patrick McHardy84fa7932006-08-29 16:44:56 -07006639 if (skb->ip_summed == CHECKSUM_PARTIAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006640 base_flags |= TXD_FLAG_TCPUDP_CSUM;
Matt Carlson24f4efd2009-11-13 13:03:35 +00006641
Matt Carlsonbe98da62010-07-11 09:31:46 +00006642 mss = skb_shinfo(skb)->gso_size;
6643 if (mss) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07006644 struct iphdr *iph;
Matt Carlson34195c32010-07-11 09:31:42 +00006645 u32 tcp_opt_len, hdr_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006646
6647 if (skb_header_cloned(skb) &&
Eric Dumazet48855432011-10-24 07:53:03 +00006648 pskb_expand_head(skb, 0, 0, GFP_ATOMIC))
6649 goto drop;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006650
Matt Carlson34195c32010-07-11 09:31:42 +00006651 iph = ip_hdr(skb);
Arnaldo Carvalho de Meloab6a5bb2007-03-18 17:43:48 -07006652 tcp_opt_len = tcp_optlen(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006653
Matt Carlson02e96082010-09-15 08:59:59 +00006654 if (skb_is_gso_v6(skb)) {
Matt Carlson34195c32010-07-11 09:31:42 +00006655 hdr_len = skb_headlen(skb) - ETH_HLEN;
6656 } else {
6657 u32 ip_tcp_len;
6658
6659 ip_tcp_len = ip_hdrlen(skb) + sizeof(struct tcphdr);
6660 hdr_len = ip_tcp_len + tcp_opt_len;
6661
6662 iph->check = 0;
6663 iph->tot_len = htons(mss + hdr_len);
6664 }
6665
Michael Chan52c0fd82006-06-29 20:15:54 -07006666 if (unlikely((ETH_HLEN + hdr_len) > 80) &&
Joe Perches63c3a662011-04-26 08:12:10 +00006667 tg3_flag(tp, TSO_BUG))
Matt Carlsonde6f31e2010-04-12 06:58:30 +00006668 return tg3_tso_bug(tp, skb);
Michael Chan52c0fd82006-06-29 20:15:54 -07006669
Linus Torvalds1da177e2005-04-16 15:20:36 -07006670 base_flags |= (TXD_FLAG_CPU_PRE_DMA |
6671 TXD_FLAG_CPU_POST_DMA);
6672
Joe Perches63c3a662011-04-26 08:12:10 +00006673 if (tg3_flag(tp, HW_TSO_1) ||
6674 tg3_flag(tp, HW_TSO_2) ||
6675 tg3_flag(tp, HW_TSO_3)) {
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07006676 tcp_hdr(skb)->check = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006677 base_flags &= ~TXD_FLAG_TCPUDP_CSUM;
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07006678 } else
6679 tcp_hdr(skb)->check = ~csum_tcpudp_magic(iph->saddr,
6680 iph->daddr, 0,
6681 IPPROTO_TCP,
6682 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006683
Joe Perches63c3a662011-04-26 08:12:10 +00006684 if (tg3_flag(tp, HW_TSO_3)) {
Matt Carlson615774f2009-11-13 13:03:39 +00006685 mss |= (hdr_len & 0xc) << 12;
6686 if (hdr_len & 0x10)
6687 base_flags |= 0x00000010;
6688 base_flags |= (hdr_len & 0x3e0) << 5;
Joe Perches63c3a662011-04-26 08:12:10 +00006689 } else if (tg3_flag(tp, HW_TSO_2))
Matt Carlson92c6b8d2009-11-02 14:23:27 +00006690 mss |= hdr_len << 9;
Joe Perches63c3a662011-04-26 08:12:10 +00006691 else if (tg3_flag(tp, HW_TSO_1) ||
Matt Carlson92c6b8d2009-11-02 14:23:27 +00006692 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07006693 if (tcp_opt_len || iph->ihl > 5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006694 int tsflags;
6695
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07006696 tsflags = (iph->ihl - 5) + (tcp_opt_len >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006697 mss |= (tsflags << 11);
6698 }
6699 } else {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07006700 if (tcp_opt_len || iph->ihl > 5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006701 int tsflags;
6702
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07006703 tsflags = (iph->ihl - 5) + (tcp_opt_len >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006704 base_flags |= tsflags << 12;
6705 }
6706 }
6707 }
Matt Carlsonbf933c82011-01-25 15:58:49 +00006708
Matt Carlson93a700a2011-08-31 11:44:54 +00006709 if (tg3_flag(tp, USE_JUMBO_BDFLAG) &&
6710 !mss && skb->len > VLAN_ETH_FRAME_LEN)
6711 base_flags |= TXD_FLAG_JMB_PKT;
6712
Matt Carlson92cd3a12011-07-27 14:20:47 +00006713 if (vlan_tx_tag_present(skb)) {
6714 base_flags |= TXD_FLAG_VLAN;
6715 vlan = vlan_tx_tag_get(skb);
6716 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006717
Alexander Duyckf4188d82009-12-02 16:48:38 +00006718 len = skb_headlen(skb);
6719
6720 mapping = pci_map_single(tp->pdev, skb->data, len, PCI_DMA_TODEVICE);
Eric Dumazet48855432011-10-24 07:53:03 +00006721 if (pci_dma_mapping_error(tp->pdev, mapping))
6722 goto drop;
6723
David S. Miller90079ce2008-09-11 04:52:51 -07006724
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006725 tnapi->tx_buffers[entry].skb = skb;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006726 dma_unmap_addr_set(&tnapi->tx_buffers[entry], mapping, mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006727
6728 would_hit_hwbug = 0;
6729
Joe Perches63c3a662011-04-26 08:12:10 +00006730 if (tg3_flag(tp, 5701_DMA_BUG))
Michael Chanc58ec932005-09-17 00:46:27 -07006731 would_hit_hwbug = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006732
Matt Carlson84b67b22011-07-27 14:20:52 +00006733 if (tg3_tx_frag_set(tnapi, &entry, &budget, mapping, len, base_flags |
Matt Carlsond1a3b732011-07-27 14:20:51 +00006734 ((skb_shinfo(skb)->nr_frags == 0) ? TXD_FLAG_END : 0),
Matt Carlsonba1142e2011-11-04 09:15:00 +00006735 mss, vlan)) {
Matt Carlsond1a3b732011-07-27 14:20:51 +00006736 would_hit_hwbug = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006737 /* Now loop through additional data fragments, and queue them. */
Matt Carlsonba1142e2011-11-04 09:15:00 +00006738 } else if (skb_shinfo(skb)->nr_frags > 0) {
Matt Carlson92cd3a12011-07-27 14:20:47 +00006739 u32 tmp_mss = mss;
6740
6741 if (!tg3_flag(tp, HW_TSO_1) &&
6742 !tg3_flag(tp, HW_TSO_2) &&
6743 !tg3_flag(tp, HW_TSO_3))
6744 tmp_mss = 0;
6745
Linus Torvalds1da177e2005-04-16 15:20:36 -07006746 last = skb_shinfo(skb)->nr_frags - 1;
6747 for (i = 0; i <= last; i++) {
6748 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
6749
Eric Dumazet9e903e02011-10-18 21:00:24 +00006750 len = skb_frag_size(frag);
Ian Campbelldc234d02011-08-24 22:28:11 +00006751 mapping = skb_frag_dma_map(&tp->pdev->dev, frag, 0,
Ian Campbell5d6bcdf2011-10-06 11:10:48 +01006752 len, DMA_TO_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006753
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006754 tnapi->tx_buffers[entry].skb = NULL;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006755 dma_unmap_addr_set(&tnapi->tx_buffers[entry], mapping,
Alexander Duyckf4188d82009-12-02 16:48:38 +00006756 mapping);
Ian Campbell5d6bcdf2011-10-06 11:10:48 +01006757 if (dma_mapping_error(&tp->pdev->dev, mapping))
Alexander Duyckf4188d82009-12-02 16:48:38 +00006758 goto dma_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006759
Matt Carlsonb9e45482011-11-04 09:14:59 +00006760 if (!budget ||
6761 tg3_tx_frag_set(tnapi, &entry, &budget, mapping,
Matt Carlson84b67b22011-07-27 14:20:52 +00006762 len, base_flags |
6763 ((i == last) ? TXD_FLAG_END : 0),
Matt Carlsonb9e45482011-11-04 09:14:59 +00006764 tmp_mss, vlan)) {
Matt Carlson92c6b8d2009-11-02 14:23:27 +00006765 would_hit_hwbug = 1;
Matt Carlsonb9e45482011-11-04 09:14:59 +00006766 break;
6767 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006768 }
6769 }
6770
6771 if (would_hit_hwbug) {
Matt Carlson0d681b22011-07-27 14:20:49 +00006772 tg3_tx_skb_unmap(tnapi, tnapi->tx_prod, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006773
6774 /* If the workaround fails due to memory/mapping
6775 * failure, silently drop this packet.
6776 */
Matt Carlson84b67b22011-07-27 14:20:52 +00006777 entry = tnapi->tx_prod;
6778 budget = tg3_tx_avail(tnapi);
David S. Miller1805b2f2011-10-24 18:18:09 -04006779 if (tigon3_dma_hwbug_workaround(tnapi, &skb, &entry, &budget,
Matt Carlson84b67b22011-07-27 14:20:52 +00006780 base_flags, mss, vlan))
Eric Dumazet48855432011-10-24 07:53:03 +00006781 goto drop_nofree;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006782 }
6783
Richard Cochrand515b452011-06-19 03:31:41 +00006784 skb_tx_timestamp(skb);
Tom Herbert298376d2011-11-28 16:33:30 +00006785 netdev_sent_queue(tp->dev, skb->len);
Richard Cochrand515b452011-06-19 03:31:41 +00006786
Linus Torvalds1da177e2005-04-16 15:20:36 -07006787 /* Packets are ready, update Tx producer idx local and on card. */
Matt Carlson24f4efd2009-11-13 13:03:35 +00006788 tw32_tx_mbox(tnapi->prodmbox, entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006789
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006790 tnapi->tx_prod = entry;
6791 if (unlikely(tg3_tx_avail(tnapi) <= (MAX_SKB_FRAGS + 1))) {
Matt Carlson24f4efd2009-11-13 13:03:35 +00006792 netif_tx_stop_queue(txq);
Matt Carlsonf65aac12010-08-02 11:26:03 +00006793
6794 /* netif_tx_stop_queue() must be done before checking
6795 * checking tx index in tg3_tx_avail() below, because in
6796 * tg3_tx(), we update tx index before checking for
6797 * netif_tx_queue_stopped().
6798 */
6799 smp_mb();
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006800 if (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi))
Matt Carlson24f4efd2009-11-13 13:03:35 +00006801 netif_tx_wake_queue(txq);
Michael Chan51b91462005-09-01 17:41:28 -07006802 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006803
Eric Dumazetcdd0db02009-05-28 00:00:41 +00006804 mmiowb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006805 return NETDEV_TX_OK;
Alexander Duyckf4188d82009-12-02 16:48:38 +00006806
6807dma_error:
Matt Carlsonba1142e2011-11-04 09:15:00 +00006808 tg3_tx_skb_unmap(tnapi, tnapi->tx_prod, --i);
Matt Carlson432aa7e2011-05-19 12:12:45 +00006809 tnapi->tx_buffers[tnapi->tx_prod].skb = NULL;
Eric Dumazet48855432011-10-24 07:53:03 +00006810drop:
6811 dev_kfree_skb(skb);
6812drop_nofree:
6813 tp->tx_dropped++;
Alexander Duyckf4188d82009-12-02 16:48:38 +00006814 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006815}
6816
Matt Carlson6e01b202011-08-19 13:58:20 +00006817static void tg3_mac_loopback(struct tg3 *tp, bool enable)
6818{
6819 if (enable) {
6820 tp->mac_mode &= ~(MAC_MODE_HALF_DUPLEX |
6821 MAC_MODE_PORT_MODE_MASK);
6822
6823 tp->mac_mode |= MAC_MODE_PORT_INT_LPBACK;
6824
6825 if (!tg3_flag(tp, 5705_PLUS))
6826 tp->mac_mode |= MAC_MODE_LINK_POLARITY;
6827
6828 if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
6829 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
6830 else
6831 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
6832 } else {
6833 tp->mac_mode &= ~MAC_MODE_PORT_INT_LPBACK;
6834
6835 if (tg3_flag(tp, 5705_PLUS) ||
6836 (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) ||
6837 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700)
6838 tp->mac_mode &= ~MAC_MODE_LINK_POLARITY;
6839 }
6840
6841 tw32(MAC_MODE, tp->mac_mode);
6842 udelay(40);
6843}
6844
Matt Carlson941ec902011-08-19 13:58:23 +00006845static int tg3_phy_lpbk_set(struct tg3 *tp, u32 speed, bool extlpbk)
Matt Carlson5e5a7f32011-08-19 13:58:21 +00006846{
Matt Carlson941ec902011-08-19 13:58:23 +00006847 u32 val, bmcr, mac_mode, ptest = 0;
Matt Carlson5e5a7f32011-08-19 13:58:21 +00006848
6849 tg3_phy_toggle_apd(tp, false);
6850 tg3_phy_toggle_automdix(tp, 0);
6851
Matt Carlson941ec902011-08-19 13:58:23 +00006852 if (extlpbk && tg3_phy_set_extloopbk(tp))
6853 return -EIO;
6854
6855 bmcr = BMCR_FULLDPLX;
Matt Carlson5e5a7f32011-08-19 13:58:21 +00006856 switch (speed) {
6857 case SPEED_10:
6858 break;
6859 case SPEED_100:
6860 bmcr |= BMCR_SPEED100;
6861 break;
6862 case SPEED_1000:
6863 default:
6864 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
6865 speed = SPEED_100;
6866 bmcr |= BMCR_SPEED100;
6867 } else {
6868 speed = SPEED_1000;
6869 bmcr |= BMCR_SPEED1000;
6870 }
6871 }
6872
Matt Carlson941ec902011-08-19 13:58:23 +00006873 if (extlpbk) {
6874 if (!(tp->phy_flags & TG3_PHYFLG_IS_FET)) {
6875 tg3_readphy(tp, MII_CTRL1000, &val);
6876 val |= CTL1000_AS_MASTER |
6877 CTL1000_ENABLE_MASTER;
6878 tg3_writephy(tp, MII_CTRL1000, val);
6879 } else {
6880 ptest = MII_TG3_FET_PTEST_TRIM_SEL |
6881 MII_TG3_FET_PTEST_TRIM_2;
6882 tg3_writephy(tp, MII_TG3_FET_PTEST, ptest);
6883 }
6884 } else
6885 bmcr |= BMCR_LOOPBACK;
6886
Matt Carlson5e5a7f32011-08-19 13:58:21 +00006887 tg3_writephy(tp, MII_BMCR, bmcr);
6888
6889 /* The write needs to be flushed for the FETs */
6890 if (tp->phy_flags & TG3_PHYFLG_IS_FET)
6891 tg3_readphy(tp, MII_BMCR, &bmcr);
6892
6893 udelay(40);
6894
6895 if ((tp->phy_flags & TG3_PHYFLG_IS_FET) &&
6896 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785) {
Matt Carlson941ec902011-08-19 13:58:23 +00006897 tg3_writephy(tp, MII_TG3_FET_PTEST, ptest |
Matt Carlson5e5a7f32011-08-19 13:58:21 +00006898 MII_TG3_FET_PTEST_FRC_TX_LINK |
6899 MII_TG3_FET_PTEST_FRC_TX_LOCK);
6900
6901 /* The write needs to be flushed for the AC131 */
6902 tg3_readphy(tp, MII_TG3_FET_PTEST, &val);
6903 }
6904
6905 /* Reset to prevent losing 1st rx packet intermittently */
6906 if ((tp->phy_flags & TG3_PHYFLG_MII_SERDES) &&
6907 tg3_flag(tp, 5780_CLASS)) {
6908 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
6909 udelay(10);
6910 tw32_f(MAC_RX_MODE, tp->rx_mode);
6911 }
6912
6913 mac_mode = tp->mac_mode &
6914 ~(MAC_MODE_PORT_MODE_MASK | MAC_MODE_HALF_DUPLEX);
6915 if (speed == SPEED_1000)
6916 mac_mode |= MAC_MODE_PORT_MODE_GMII;
6917 else
6918 mac_mode |= MAC_MODE_PORT_MODE_MII;
6919
6920 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700) {
6921 u32 masked_phy_id = tp->phy_id & TG3_PHY_ID_MASK;
6922
6923 if (masked_phy_id == TG3_PHY_ID_BCM5401)
6924 mac_mode &= ~MAC_MODE_LINK_POLARITY;
6925 else if (masked_phy_id == TG3_PHY_ID_BCM5411)
6926 mac_mode |= MAC_MODE_LINK_POLARITY;
6927
6928 tg3_writephy(tp, MII_TG3_EXT_CTRL,
6929 MII_TG3_EXT_CTRL_LNK3_LED_MODE);
6930 }
6931
6932 tw32(MAC_MODE, mac_mode);
6933 udelay(40);
Matt Carlson941ec902011-08-19 13:58:23 +00006934
6935 return 0;
Matt Carlson5e5a7f32011-08-19 13:58:21 +00006936}
6937
Michał Mirosławc8f44af2011-11-15 15:29:55 +00006938static void tg3_set_loopback(struct net_device *dev, netdev_features_t features)
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00006939{
6940 struct tg3 *tp = netdev_priv(dev);
6941
6942 if (features & NETIF_F_LOOPBACK) {
6943 if (tp->mac_mode & MAC_MODE_PORT_INT_LPBACK)
6944 return;
6945
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00006946 spin_lock_bh(&tp->lock);
Matt Carlson6e01b202011-08-19 13:58:20 +00006947 tg3_mac_loopback(tp, true);
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00006948 netif_carrier_on(tp->dev);
6949 spin_unlock_bh(&tp->lock);
6950 netdev_info(dev, "Internal MAC loopback mode enabled.\n");
6951 } else {
6952 if (!(tp->mac_mode & MAC_MODE_PORT_INT_LPBACK))
6953 return;
6954
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00006955 spin_lock_bh(&tp->lock);
Matt Carlson6e01b202011-08-19 13:58:20 +00006956 tg3_mac_loopback(tp, false);
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00006957 /* Force link status check */
6958 tg3_setup_phy(tp, 1);
6959 spin_unlock_bh(&tp->lock);
6960 netdev_info(dev, "Internal MAC loopback mode disabled.\n");
6961 }
6962}
6963
Michał Mirosławc8f44af2011-11-15 15:29:55 +00006964static netdev_features_t tg3_fix_features(struct net_device *dev,
6965 netdev_features_t features)
Michał Mirosławdc668912011-04-07 03:35:07 +00006966{
6967 struct tg3 *tp = netdev_priv(dev);
6968
Joe Perches63c3a662011-04-26 08:12:10 +00006969 if (dev->mtu > ETH_DATA_LEN && tg3_flag(tp, 5780_CLASS))
Michał Mirosławdc668912011-04-07 03:35:07 +00006970 features &= ~NETIF_F_ALL_TSO;
6971
6972 return features;
6973}
6974
Michał Mirosławc8f44af2011-11-15 15:29:55 +00006975static int tg3_set_features(struct net_device *dev, netdev_features_t features)
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00006976{
Michał Mirosławc8f44af2011-11-15 15:29:55 +00006977 netdev_features_t changed = dev->features ^ features;
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00006978
6979 if ((changed & NETIF_F_LOOPBACK) && netif_running(dev))
6980 tg3_set_loopback(dev, features);
6981
6982 return 0;
6983}
6984
Linus Torvalds1da177e2005-04-16 15:20:36 -07006985static inline void tg3_set_mtu(struct net_device *dev, struct tg3 *tp,
6986 int new_mtu)
6987{
6988 dev->mtu = new_mtu;
6989
Michael Chanef7f5ec2005-07-25 12:32:25 -07006990 if (new_mtu > ETH_DATA_LEN) {
Joe Perches63c3a662011-04-26 08:12:10 +00006991 if (tg3_flag(tp, 5780_CLASS)) {
Michał Mirosławdc668912011-04-07 03:35:07 +00006992 netdev_update_features(dev);
Joe Perches63c3a662011-04-26 08:12:10 +00006993 tg3_flag_clear(tp, TSO_CAPABLE);
Matt Carlson859a5882010-04-05 10:19:28 +00006994 } else {
Joe Perches63c3a662011-04-26 08:12:10 +00006995 tg3_flag_set(tp, JUMBO_RING_ENABLE);
Matt Carlson859a5882010-04-05 10:19:28 +00006996 }
Michael Chanef7f5ec2005-07-25 12:32:25 -07006997 } else {
Joe Perches63c3a662011-04-26 08:12:10 +00006998 if (tg3_flag(tp, 5780_CLASS)) {
6999 tg3_flag_set(tp, TSO_CAPABLE);
Michał Mirosławdc668912011-04-07 03:35:07 +00007000 netdev_update_features(dev);
7001 }
Joe Perches63c3a662011-04-26 08:12:10 +00007002 tg3_flag_clear(tp, JUMBO_RING_ENABLE);
Michael Chanef7f5ec2005-07-25 12:32:25 -07007003 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007004}
7005
7006static int tg3_change_mtu(struct net_device *dev, int new_mtu)
7007{
7008 struct tg3 *tp = netdev_priv(dev);
Michael Chanb9ec6c12006-07-25 16:37:27 -07007009 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007010
7011 if (new_mtu < TG3_MIN_MTU || new_mtu > TG3_MAX_MTU(tp))
7012 return -EINVAL;
7013
7014 if (!netif_running(dev)) {
7015 /* We'll just catch it later when the
7016 * device is up'd.
7017 */
7018 tg3_set_mtu(dev, tp, new_mtu);
7019 return 0;
7020 }
7021
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07007022 tg3_phy_stop(tp);
7023
Linus Torvalds1da177e2005-04-16 15:20:36 -07007024 tg3_netif_stop(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -07007025
7026 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007027
Michael Chan944d9802005-05-29 14:57:48 -07007028 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007029
7030 tg3_set_mtu(dev, tp, new_mtu);
7031
Michael Chanb9ec6c12006-07-25 16:37:27 -07007032 err = tg3_restart_hw(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007033
Michael Chanb9ec6c12006-07-25 16:37:27 -07007034 if (!err)
7035 tg3_netif_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007036
David S. Millerf47c11e2005-06-24 20:18:35 -07007037 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007038
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07007039 if (!err)
7040 tg3_phy_start(tp);
7041
Michael Chanb9ec6c12006-07-25 16:37:27 -07007042 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007043}
7044
Matt Carlson21f581a2009-08-28 14:00:25 +00007045static void tg3_rx_prodring_free(struct tg3 *tp,
7046 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007047{
Linus Torvalds1da177e2005-04-16 15:20:36 -07007048 int i;
7049
Matt Carlson8fea32b2010-09-15 08:59:58 +00007050 if (tpr != &tp->napi[0].prodring) {
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007051 for (i = tpr->rx_std_cons_idx; i != tpr->rx_std_prod_idx;
Matt Carlson2c49a442010-09-30 10:34:35 +00007052 i = (i + 1) & tp->rx_std_ring_mask)
Eric Dumazet9205fd92011-11-18 06:47:01 +00007053 tg3_rx_data_free(tp, &tpr->rx_std_buffers[i],
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007054 tp->rx_pkt_map_sz);
7055
Joe Perches63c3a662011-04-26 08:12:10 +00007056 if (tg3_flag(tp, JUMBO_CAPABLE)) {
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007057 for (i = tpr->rx_jmb_cons_idx;
7058 i != tpr->rx_jmb_prod_idx;
Matt Carlson2c49a442010-09-30 10:34:35 +00007059 i = (i + 1) & tp->rx_jmb_ring_mask) {
Eric Dumazet9205fd92011-11-18 06:47:01 +00007060 tg3_rx_data_free(tp, &tpr->rx_jmb_buffers[i],
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007061 TG3_RX_JMB_MAP_SZ);
7062 }
7063 }
7064
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007065 return;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007066 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007067
Matt Carlson2c49a442010-09-30 10:34:35 +00007068 for (i = 0; i <= tp->rx_std_ring_mask; i++)
Eric Dumazet9205fd92011-11-18 06:47:01 +00007069 tg3_rx_data_free(tp, &tpr->rx_std_buffers[i],
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007070 tp->rx_pkt_map_sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007071
Joe Perches63c3a662011-04-26 08:12:10 +00007072 if (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS)) {
Matt Carlson2c49a442010-09-30 10:34:35 +00007073 for (i = 0; i <= tp->rx_jmb_ring_mask; i++)
Eric Dumazet9205fd92011-11-18 06:47:01 +00007074 tg3_rx_data_free(tp, &tpr->rx_jmb_buffers[i],
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007075 TG3_RX_JMB_MAP_SZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007076 }
7077}
7078
Matt Carlsonc6cdf432010-04-05 10:19:26 +00007079/* Initialize rx rings for packet processing.
Linus Torvalds1da177e2005-04-16 15:20:36 -07007080 *
7081 * The chip has been shut down and the driver detached from
7082 * the networking, so no interrupts or new tx packets will
7083 * end up in the driver. tp->{tx,}lock are held and thus
7084 * we may not sleep.
7085 */
Matt Carlson21f581a2009-08-28 14:00:25 +00007086static int tg3_rx_prodring_alloc(struct tg3 *tp,
7087 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007088{
Matt Carlson287be122009-08-28 13:58:46 +00007089 u32 i, rx_pkt_dma_sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007090
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007091 tpr->rx_std_cons_idx = 0;
7092 tpr->rx_std_prod_idx = 0;
7093 tpr->rx_jmb_cons_idx = 0;
7094 tpr->rx_jmb_prod_idx = 0;
7095
Matt Carlson8fea32b2010-09-15 08:59:58 +00007096 if (tpr != &tp->napi[0].prodring) {
Matt Carlson2c49a442010-09-30 10:34:35 +00007097 memset(&tpr->rx_std_buffers[0], 0,
7098 TG3_RX_STD_BUFF_RING_SIZE(tp));
Matt Carlson48035722010-10-14 10:37:43 +00007099 if (tpr->rx_jmb_buffers)
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007100 memset(&tpr->rx_jmb_buffers[0], 0,
Matt Carlson2c49a442010-09-30 10:34:35 +00007101 TG3_RX_JMB_BUFF_RING_SIZE(tp));
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007102 goto done;
7103 }
7104
Linus Torvalds1da177e2005-04-16 15:20:36 -07007105 /* Zero out all descriptors. */
Matt Carlson2c49a442010-09-30 10:34:35 +00007106 memset(tpr->rx_std, 0, TG3_RX_STD_RING_BYTES(tp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007107
Matt Carlson287be122009-08-28 13:58:46 +00007108 rx_pkt_dma_sz = TG3_RX_STD_DMA_SZ;
Joe Perches63c3a662011-04-26 08:12:10 +00007109 if (tg3_flag(tp, 5780_CLASS) &&
Matt Carlson287be122009-08-28 13:58:46 +00007110 tp->dev->mtu > ETH_DATA_LEN)
7111 rx_pkt_dma_sz = TG3_RX_JMB_DMA_SZ;
7112 tp->rx_pkt_map_sz = TG3_RX_DMA_TO_MAP_SZ(rx_pkt_dma_sz);
Michael Chan7e72aad2005-07-25 12:31:17 -07007113
Linus Torvalds1da177e2005-04-16 15:20:36 -07007114 /* Initialize invariants of the rings, we only set this
7115 * stuff once. This works because the card does not
7116 * write into the rx buffer posting rings.
7117 */
Matt Carlson2c49a442010-09-30 10:34:35 +00007118 for (i = 0; i <= tp->rx_std_ring_mask; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007119 struct tg3_rx_buffer_desc *rxd;
7120
Matt Carlson21f581a2009-08-28 14:00:25 +00007121 rxd = &tpr->rx_std[i];
Matt Carlson287be122009-08-28 13:58:46 +00007122 rxd->idx_len = rx_pkt_dma_sz << RXD_LEN_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007123 rxd->type_flags = (RXD_FLAG_END << RXD_FLAGS_SHIFT);
7124 rxd->opaque = (RXD_OPAQUE_RING_STD |
7125 (i << RXD_OPAQUE_INDEX_SHIFT));
7126 }
7127
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007128 /* Now allocate fresh SKBs for each rx ring. */
7129 for (i = 0; i < tp->rx_pending; i++) {
Eric Dumazet9205fd92011-11-18 06:47:01 +00007130 if (tg3_alloc_rx_data(tp, tpr, RXD_OPAQUE_RING_STD, i) < 0) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00007131 netdev_warn(tp->dev,
7132 "Using a smaller RX standard ring. Only "
7133 "%d out of %d buffers were allocated "
7134 "successfully\n", i, tp->rx_pending);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007135 if (i == 0)
7136 goto initfail;
7137 tp->rx_pending = i;
7138 break;
7139 }
7140 }
7141
Joe Perches63c3a662011-04-26 08:12:10 +00007142 if (!tg3_flag(tp, JUMBO_CAPABLE) || tg3_flag(tp, 5780_CLASS))
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007143 goto done;
7144
Matt Carlson2c49a442010-09-30 10:34:35 +00007145 memset(tpr->rx_jmb, 0, TG3_RX_JMB_RING_BYTES(tp));
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007146
Joe Perches63c3a662011-04-26 08:12:10 +00007147 if (!tg3_flag(tp, JUMBO_RING_ENABLE))
Matt Carlson0d86df82010-02-17 15:17:00 +00007148 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007149
Matt Carlson2c49a442010-09-30 10:34:35 +00007150 for (i = 0; i <= tp->rx_jmb_ring_mask; i++) {
Matt Carlson0d86df82010-02-17 15:17:00 +00007151 struct tg3_rx_buffer_desc *rxd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007152
Matt Carlson0d86df82010-02-17 15:17:00 +00007153 rxd = &tpr->rx_jmb[i].std;
7154 rxd->idx_len = TG3_RX_JMB_DMA_SZ << RXD_LEN_SHIFT;
7155 rxd->type_flags = (RXD_FLAG_END << RXD_FLAGS_SHIFT) |
7156 RXD_FLAG_JUMBO;
7157 rxd->opaque = (RXD_OPAQUE_RING_JUMBO |
7158 (i << RXD_OPAQUE_INDEX_SHIFT));
7159 }
7160
7161 for (i = 0; i < tp->rx_jumbo_pending; i++) {
Eric Dumazet9205fd92011-11-18 06:47:01 +00007162 if (tg3_alloc_rx_data(tp, tpr, RXD_OPAQUE_RING_JUMBO, i) < 0) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00007163 netdev_warn(tp->dev,
7164 "Using a smaller RX jumbo ring. Only %d "
7165 "out of %d buffers were allocated "
7166 "successfully\n", i, tp->rx_jumbo_pending);
Matt Carlson0d86df82010-02-17 15:17:00 +00007167 if (i == 0)
7168 goto initfail;
7169 tp->rx_jumbo_pending = i;
7170 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007171 }
7172 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007173
7174done:
Michael Chan32d8c572006-07-25 16:38:29 -07007175 return 0;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007176
7177initfail:
Matt Carlson21f581a2009-08-28 14:00:25 +00007178 tg3_rx_prodring_free(tp, tpr);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007179 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007180}
7181
Matt Carlson21f581a2009-08-28 14:00:25 +00007182static void tg3_rx_prodring_fini(struct tg3 *tp,
7183 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007184{
Matt Carlson21f581a2009-08-28 14:00:25 +00007185 kfree(tpr->rx_std_buffers);
7186 tpr->rx_std_buffers = NULL;
7187 kfree(tpr->rx_jmb_buffers);
7188 tpr->rx_jmb_buffers = NULL;
7189 if (tpr->rx_std) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00007190 dma_free_coherent(&tp->pdev->dev, TG3_RX_STD_RING_BYTES(tp),
7191 tpr->rx_std, tpr->rx_std_mapping);
Matt Carlson21f581a2009-08-28 14:00:25 +00007192 tpr->rx_std = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007193 }
Matt Carlson21f581a2009-08-28 14:00:25 +00007194 if (tpr->rx_jmb) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00007195 dma_free_coherent(&tp->pdev->dev, TG3_RX_JMB_RING_BYTES(tp),
7196 tpr->rx_jmb, tpr->rx_jmb_mapping);
Matt Carlson21f581a2009-08-28 14:00:25 +00007197 tpr->rx_jmb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007198 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007199}
7200
Matt Carlson21f581a2009-08-28 14:00:25 +00007201static int tg3_rx_prodring_init(struct tg3 *tp,
7202 struct tg3_rx_prodring_set *tpr)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007203{
Matt Carlson2c49a442010-09-30 10:34:35 +00007204 tpr->rx_std_buffers = kzalloc(TG3_RX_STD_BUFF_RING_SIZE(tp),
7205 GFP_KERNEL);
Matt Carlson21f581a2009-08-28 14:00:25 +00007206 if (!tpr->rx_std_buffers)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007207 return -ENOMEM;
7208
Matt Carlson4bae65c2010-11-24 08:31:52 +00007209 tpr->rx_std = dma_alloc_coherent(&tp->pdev->dev,
7210 TG3_RX_STD_RING_BYTES(tp),
7211 &tpr->rx_std_mapping,
7212 GFP_KERNEL);
Matt Carlson21f581a2009-08-28 14:00:25 +00007213 if (!tpr->rx_std)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007214 goto err_out;
7215
Joe Perches63c3a662011-04-26 08:12:10 +00007216 if (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS)) {
Matt Carlson2c49a442010-09-30 10:34:35 +00007217 tpr->rx_jmb_buffers = kzalloc(TG3_RX_JMB_BUFF_RING_SIZE(tp),
Matt Carlson21f581a2009-08-28 14:00:25 +00007218 GFP_KERNEL);
7219 if (!tpr->rx_jmb_buffers)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007220 goto err_out;
7221
Matt Carlson4bae65c2010-11-24 08:31:52 +00007222 tpr->rx_jmb = dma_alloc_coherent(&tp->pdev->dev,
7223 TG3_RX_JMB_RING_BYTES(tp),
7224 &tpr->rx_jmb_mapping,
7225 GFP_KERNEL);
Matt Carlson21f581a2009-08-28 14:00:25 +00007226 if (!tpr->rx_jmb)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007227 goto err_out;
7228 }
7229
7230 return 0;
7231
7232err_out:
Matt Carlson21f581a2009-08-28 14:00:25 +00007233 tg3_rx_prodring_fini(tp, tpr);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007234 return -ENOMEM;
7235}
7236
7237/* Free up pending packets in all rx/tx rings.
7238 *
7239 * The chip has been shut down and the driver detached from
7240 * the networking, so no interrupts or new tx packets will
7241 * end up in the driver. tp->{tx,}lock is not held and we are not
7242 * in an interrupt context and thus may sleep.
7243 */
7244static void tg3_free_rings(struct tg3 *tp)
7245{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007246 int i, j;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007247
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007248 for (j = 0; j < tp->irq_cnt; j++) {
7249 struct tg3_napi *tnapi = &tp->napi[j];
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007250
Matt Carlson8fea32b2010-09-15 08:59:58 +00007251 tg3_rx_prodring_free(tp, &tnapi->prodring);
Matt Carlsonb28f6422010-06-05 17:24:32 +00007252
Matt Carlson0c1d0e22009-09-01 13:16:33 +00007253 if (!tnapi->tx_buffers)
7254 continue;
7255
Matt Carlson0d681b22011-07-27 14:20:49 +00007256 for (i = 0; i < TG3_TX_RING_SIZE; i++) {
7257 struct sk_buff *skb = tnapi->tx_buffers[i].skb;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007258
Matt Carlson0d681b22011-07-27 14:20:49 +00007259 if (!skb)
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007260 continue;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007261
Matt Carlsonba1142e2011-11-04 09:15:00 +00007262 tg3_tx_skb_unmap(tnapi, i,
7263 skb_shinfo(skb)->nr_frags - 1);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007264
7265 dev_kfree_skb_any(skb);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007266 }
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007267 }
Tom Herbert298376d2011-11-28 16:33:30 +00007268 netdev_reset_queue(tp->dev);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007269}
7270
7271/* Initialize tx/rx rings for packet processing.
7272 *
7273 * The chip has been shut down and the driver detached from
7274 * the networking, so no interrupts or new tx packets will
7275 * end up in the driver. tp->{tx,}lock are held and thus
7276 * we may not sleep.
7277 */
7278static int tg3_init_rings(struct tg3 *tp)
7279{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007280 int i;
Matt Carlson72334482009-08-28 14:03:01 +00007281
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007282 /* Free up all the SKBs. */
7283 tg3_free_rings(tp);
7284
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007285 for (i = 0; i < tp->irq_cnt; i++) {
7286 struct tg3_napi *tnapi = &tp->napi[i];
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007287
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007288 tnapi->last_tag = 0;
7289 tnapi->last_irq_tag = 0;
7290 tnapi->hw_status->status = 0;
7291 tnapi->hw_status->status_tag = 0;
7292 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
7293
7294 tnapi->tx_prod = 0;
7295 tnapi->tx_cons = 0;
Matt Carlson0c1d0e22009-09-01 13:16:33 +00007296 if (tnapi->tx_ring)
7297 memset(tnapi->tx_ring, 0, TG3_TX_RING_BYTES);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007298
7299 tnapi->rx_rcb_ptr = 0;
Matt Carlson0c1d0e22009-09-01 13:16:33 +00007300 if (tnapi->rx_rcb)
7301 memset(tnapi->rx_rcb, 0, TG3_RX_RCB_RING_BYTES(tp));
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007302
Matt Carlson8fea32b2010-09-15 08:59:58 +00007303 if (tg3_rx_prodring_alloc(tp, &tnapi->prodring)) {
Matt Carlsone4af1af2010-02-12 14:47:05 +00007304 tg3_free_rings(tp);
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007305 return -ENOMEM;
Matt Carlsone4af1af2010-02-12 14:47:05 +00007306 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007307 }
Matt Carlson72334482009-08-28 14:03:01 +00007308
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007309 return 0;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007310}
7311
7312/*
7313 * Must not be invoked with interrupt sources disabled and
7314 * the hardware shutdown down.
7315 */
7316static void tg3_free_consistent(struct tg3 *tp)
7317{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007318 int i;
Matt Carlson898a56f2009-08-28 14:02:40 +00007319
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007320 for (i = 0; i < tp->irq_cnt; i++) {
7321 struct tg3_napi *tnapi = &tp->napi[i];
7322
7323 if (tnapi->tx_ring) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00007324 dma_free_coherent(&tp->pdev->dev, TG3_TX_RING_BYTES,
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007325 tnapi->tx_ring, tnapi->tx_desc_mapping);
7326 tnapi->tx_ring = NULL;
7327 }
7328
7329 kfree(tnapi->tx_buffers);
7330 tnapi->tx_buffers = NULL;
7331
7332 if (tnapi->rx_rcb) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00007333 dma_free_coherent(&tp->pdev->dev,
7334 TG3_RX_RCB_RING_BYTES(tp),
7335 tnapi->rx_rcb,
7336 tnapi->rx_rcb_mapping);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007337 tnapi->rx_rcb = NULL;
7338 }
7339
Matt Carlson8fea32b2010-09-15 08:59:58 +00007340 tg3_rx_prodring_fini(tp, &tnapi->prodring);
7341
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007342 if (tnapi->hw_status) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00007343 dma_free_coherent(&tp->pdev->dev, TG3_HW_STATUS_SIZE,
7344 tnapi->hw_status,
7345 tnapi->status_mapping);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007346 tnapi->hw_status = NULL;
7347 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007348 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007349
Linus Torvalds1da177e2005-04-16 15:20:36 -07007350 if (tp->hw_stats) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00007351 dma_free_coherent(&tp->pdev->dev, sizeof(struct tg3_hw_stats),
7352 tp->hw_stats, tp->stats_mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007353 tp->hw_stats = NULL;
7354 }
7355}
7356
7357/*
7358 * Must not be invoked with interrupt sources disabled and
7359 * the hardware shutdown down. Can sleep.
7360 */
7361static int tg3_alloc_consistent(struct tg3 *tp)
7362{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007363 int i;
Matt Carlson898a56f2009-08-28 14:02:40 +00007364
Matt Carlson4bae65c2010-11-24 08:31:52 +00007365 tp->hw_stats = dma_alloc_coherent(&tp->pdev->dev,
7366 sizeof(struct tg3_hw_stats),
7367 &tp->stats_mapping,
7368 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007369 if (!tp->hw_stats)
7370 goto err_out;
7371
Linus Torvalds1da177e2005-04-16 15:20:36 -07007372 memset(tp->hw_stats, 0, sizeof(struct tg3_hw_stats));
7373
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007374 for (i = 0; i < tp->irq_cnt; i++) {
7375 struct tg3_napi *tnapi = &tp->napi[i];
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00007376 struct tg3_hw_status *sblk;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007377
Matt Carlson4bae65c2010-11-24 08:31:52 +00007378 tnapi->hw_status = dma_alloc_coherent(&tp->pdev->dev,
7379 TG3_HW_STATUS_SIZE,
7380 &tnapi->status_mapping,
7381 GFP_KERNEL);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007382 if (!tnapi->hw_status)
7383 goto err_out;
7384
7385 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00007386 sblk = tnapi->hw_status;
7387
Matt Carlson8fea32b2010-09-15 08:59:58 +00007388 if (tg3_rx_prodring_init(tp, &tnapi->prodring))
7389 goto err_out;
7390
Matt Carlson19cfaec2009-12-03 08:36:20 +00007391 /* If multivector TSS is enabled, vector 0 does not handle
7392 * tx interrupts. Don't allocate any resources for it.
7393 */
Joe Perches63c3a662011-04-26 08:12:10 +00007394 if ((!i && !tg3_flag(tp, ENABLE_TSS)) ||
7395 (i && tg3_flag(tp, ENABLE_TSS))) {
Matt Carlsondf8944c2011-07-27 14:20:46 +00007396 tnapi->tx_buffers = kzalloc(
7397 sizeof(struct tg3_tx_ring_info) *
7398 TG3_TX_RING_SIZE, GFP_KERNEL);
Matt Carlson19cfaec2009-12-03 08:36:20 +00007399 if (!tnapi->tx_buffers)
7400 goto err_out;
7401
Matt Carlson4bae65c2010-11-24 08:31:52 +00007402 tnapi->tx_ring = dma_alloc_coherent(&tp->pdev->dev,
7403 TG3_TX_RING_BYTES,
7404 &tnapi->tx_desc_mapping,
7405 GFP_KERNEL);
Matt Carlson19cfaec2009-12-03 08:36:20 +00007406 if (!tnapi->tx_ring)
7407 goto err_out;
7408 }
7409
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00007410 /*
7411 * When RSS is enabled, the status block format changes
7412 * slightly. The "rx_jumbo_consumer", "reserved",
7413 * and "rx_mini_consumer" members get mapped to the
7414 * other three rx return ring producer indexes.
7415 */
7416 switch (i) {
7417 default:
7418 tnapi->rx_rcb_prod_idx = &sblk->idx[0].rx_producer;
7419 break;
7420 case 2:
7421 tnapi->rx_rcb_prod_idx = &sblk->rx_jumbo_consumer;
7422 break;
7423 case 3:
7424 tnapi->rx_rcb_prod_idx = &sblk->reserved;
7425 break;
7426 case 4:
7427 tnapi->rx_rcb_prod_idx = &sblk->rx_mini_consumer;
7428 break;
7429 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007430
Matt Carlson0c1d0e22009-09-01 13:16:33 +00007431 /*
7432 * If multivector RSS is enabled, vector 0 does not handle
7433 * rx or tx interrupts. Don't allocate any resources for it.
7434 */
Joe Perches63c3a662011-04-26 08:12:10 +00007435 if (!i && tg3_flag(tp, ENABLE_RSS))
Matt Carlson0c1d0e22009-09-01 13:16:33 +00007436 continue;
7437
Matt Carlson4bae65c2010-11-24 08:31:52 +00007438 tnapi->rx_rcb = dma_alloc_coherent(&tp->pdev->dev,
7439 TG3_RX_RCB_RING_BYTES(tp),
7440 &tnapi->rx_rcb_mapping,
7441 GFP_KERNEL);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007442 if (!tnapi->rx_rcb)
7443 goto err_out;
7444
7445 memset(tnapi->rx_rcb, 0, TG3_RX_RCB_RING_BYTES(tp));
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007446 }
7447
Linus Torvalds1da177e2005-04-16 15:20:36 -07007448 return 0;
7449
7450err_out:
7451 tg3_free_consistent(tp);
7452 return -ENOMEM;
7453}
7454
7455#define MAX_WAIT_CNT 1000
7456
7457/* To stop a block, clear the enable bit and poll till it
7458 * clears. tp->lock is held.
7459 */
David S. Millerb3b7d6b2005-05-05 14:40:20 -07007460static int tg3_stop_block(struct tg3 *tp, unsigned long ofs, u32 enable_bit, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007461{
7462 unsigned int i;
7463 u32 val;
7464
Joe Perches63c3a662011-04-26 08:12:10 +00007465 if (tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007466 switch (ofs) {
7467 case RCVLSC_MODE:
7468 case DMAC_MODE:
7469 case MBFREE_MODE:
7470 case BUFMGR_MODE:
7471 case MEMARB_MODE:
7472 /* We can't enable/disable these bits of the
7473 * 5705/5750, just say success.
7474 */
7475 return 0;
7476
7477 default:
7478 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07007479 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007480 }
7481
7482 val = tr32(ofs);
7483 val &= ~enable_bit;
7484 tw32_f(ofs, val);
7485
7486 for (i = 0; i < MAX_WAIT_CNT; i++) {
7487 udelay(100);
7488 val = tr32(ofs);
7489 if ((val & enable_bit) == 0)
7490 break;
7491 }
7492
David S. Millerb3b7d6b2005-05-05 14:40:20 -07007493 if (i == MAX_WAIT_CNT && !silent) {
Matt Carlson2445e462010-04-05 10:19:21 +00007494 dev_err(&tp->pdev->dev,
7495 "tg3_stop_block timed out, ofs=%lx enable_bit=%x\n",
7496 ofs, enable_bit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007497 return -ENODEV;
7498 }
7499
7500 return 0;
7501}
7502
7503/* tp->lock is held. */
David S. Millerb3b7d6b2005-05-05 14:40:20 -07007504static int tg3_abort_hw(struct tg3 *tp, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007505{
7506 int i, err;
7507
7508 tg3_disable_ints(tp);
7509
7510 tp->rx_mode &= ~RX_MODE_ENABLE;
7511 tw32_f(MAC_RX_MODE, tp->rx_mode);
7512 udelay(10);
7513
David S. Millerb3b7d6b2005-05-05 14:40:20 -07007514 err = tg3_stop_block(tp, RCVBDI_MODE, RCVBDI_MODE_ENABLE, silent);
7515 err |= tg3_stop_block(tp, RCVLPC_MODE, RCVLPC_MODE_ENABLE, silent);
7516 err |= tg3_stop_block(tp, RCVLSC_MODE, RCVLSC_MODE_ENABLE, silent);
7517 err |= tg3_stop_block(tp, RCVDBDI_MODE, RCVDBDI_MODE_ENABLE, silent);
7518 err |= tg3_stop_block(tp, RCVDCC_MODE, RCVDCC_MODE_ENABLE, silent);
7519 err |= tg3_stop_block(tp, RCVCC_MODE, RCVCC_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007520
David S. Millerb3b7d6b2005-05-05 14:40:20 -07007521 err |= tg3_stop_block(tp, SNDBDS_MODE, SNDBDS_MODE_ENABLE, silent);
7522 err |= tg3_stop_block(tp, SNDBDI_MODE, SNDBDI_MODE_ENABLE, silent);
7523 err |= tg3_stop_block(tp, SNDDATAI_MODE, SNDDATAI_MODE_ENABLE, silent);
7524 err |= tg3_stop_block(tp, RDMAC_MODE, RDMAC_MODE_ENABLE, silent);
7525 err |= tg3_stop_block(tp, SNDDATAC_MODE, SNDDATAC_MODE_ENABLE, silent);
7526 err |= tg3_stop_block(tp, DMAC_MODE, DMAC_MODE_ENABLE, silent);
7527 err |= tg3_stop_block(tp, SNDBDC_MODE, SNDBDC_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007528
7529 tp->mac_mode &= ~MAC_MODE_TDE_ENABLE;
7530 tw32_f(MAC_MODE, tp->mac_mode);
7531 udelay(40);
7532
7533 tp->tx_mode &= ~TX_MODE_ENABLE;
7534 tw32_f(MAC_TX_MODE, tp->tx_mode);
7535
7536 for (i = 0; i < MAX_WAIT_CNT; i++) {
7537 udelay(100);
7538 if (!(tr32(MAC_TX_MODE) & TX_MODE_ENABLE))
7539 break;
7540 }
7541 if (i >= MAX_WAIT_CNT) {
Matt Carlsonab96b242010-04-05 10:19:22 +00007542 dev_err(&tp->pdev->dev,
7543 "%s timed out, TX_MODE_ENABLE will not clear "
7544 "MAC_TX_MODE=%08x\n", __func__, tr32(MAC_TX_MODE));
Michael Chane6de8ad2005-05-05 14:42:41 -07007545 err |= -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007546 }
7547
Michael Chane6de8ad2005-05-05 14:42:41 -07007548 err |= tg3_stop_block(tp, HOSTCC_MODE, HOSTCC_MODE_ENABLE, silent);
David S. Millerb3b7d6b2005-05-05 14:40:20 -07007549 err |= tg3_stop_block(tp, WDMAC_MODE, WDMAC_MODE_ENABLE, silent);
7550 err |= tg3_stop_block(tp, MBFREE_MODE, MBFREE_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007551
7552 tw32(FTQ_RESET, 0xffffffff);
7553 tw32(FTQ_RESET, 0x00000000);
7554
David S. Millerb3b7d6b2005-05-05 14:40:20 -07007555 err |= tg3_stop_block(tp, BUFMGR_MODE, BUFMGR_MODE_ENABLE, silent);
7556 err |= tg3_stop_block(tp, MEMARB_MODE, MEMARB_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007557
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007558 for (i = 0; i < tp->irq_cnt; i++) {
7559 struct tg3_napi *tnapi = &tp->napi[i];
7560 if (tnapi->hw_status)
7561 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
7562 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007563
Linus Torvalds1da177e2005-04-16 15:20:36 -07007564 return err;
7565}
7566
Michael Chanee6a99b2007-07-18 21:49:10 -07007567/* Save PCI command register before chip reset */
7568static void tg3_save_pci_state(struct tg3 *tp)
7569{
Matt Carlson8a6eac92007-10-21 16:17:55 -07007570 pci_read_config_word(tp->pdev, PCI_COMMAND, &tp->pci_cmd);
Michael Chanee6a99b2007-07-18 21:49:10 -07007571}
7572
7573/* Restore PCI state after chip reset */
7574static void tg3_restore_pci_state(struct tg3 *tp)
7575{
7576 u32 val;
7577
7578 /* Re-enable indirect register accesses. */
7579 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
7580 tp->misc_host_ctrl);
7581
7582 /* Set MAX PCI retry to zero. */
7583 val = (PCISTATE_ROM_ENABLE | PCISTATE_ROM_RETRY_ENABLE);
7584 if (tp->pci_chip_rev_id == CHIPREV_ID_5704_A0 &&
Joe Perches63c3a662011-04-26 08:12:10 +00007585 tg3_flag(tp, PCIX_MODE))
Michael Chanee6a99b2007-07-18 21:49:10 -07007586 val |= PCISTATE_RETRY_SAME_DMA;
Matt Carlson0d3031d2007-10-10 18:02:43 -07007587 /* Allow reads and writes to the APE register and memory space. */
Joe Perches63c3a662011-04-26 08:12:10 +00007588 if (tg3_flag(tp, ENABLE_APE))
Matt Carlson0d3031d2007-10-10 18:02:43 -07007589 val |= PCISTATE_ALLOW_APE_CTLSPC_WR |
Matt Carlsonf92d9dc12010-06-05 17:24:30 +00007590 PCISTATE_ALLOW_APE_SHMEM_WR |
7591 PCISTATE_ALLOW_APE_PSPACE_WR;
Michael Chanee6a99b2007-07-18 21:49:10 -07007592 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE, val);
7593
Matt Carlson8a6eac92007-10-21 16:17:55 -07007594 pci_write_config_word(tp->pdev, PCI_COMMAND, tp->pci_cmd);
Michael Chanee6a99b2007-07-18 21:49:10 -07007595
Matt Carlson2c55a3d2011-11-28 09:41:04 +00007596 if (!tg3_flag(tp, PCI_EXPRESS)) {
7597 pci_write_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE,
7598 tp->pci_cacheline_sz);
7599 pci_write_config_byte(tp->pdev, PCI_LATENCY_TIMER,
7600 tp->pci_lat_timer);
Michael Chan114342f2007-10-15 02:12:26 -07007601 }
Matt Carlson5f5c51e2007-11-12 21:19:37 -08007602
Michael Chanee6a99b2007-07-18 21:49:10 -07007603 /* Make sure PCI-X relaxed ordering bit is clear. */
Joe Perches63c3a662011-04-26 08:12:10 +00007604 if (tg3_flag(tp, PCIX_MODE)) {
Matt Carlson9974a352007-10-07 23:27:28 -07007605 u16 pcix_cmd;
7606
7607 pci_read_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
7608 &pcix_cmd);
7609 pcix_cmd &= ~PCI_X_CMD_ERO;
7610 pci_write_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
7611 pcix_cmd);
7612 }
Michael Chanee6a99b2007-07-18 21:49:10 -07007613
Joe Perches63c3a662011-04-26 08:12:10 +00007614 if (tg3_flag(tp, 5780_CLASS)) {
Michael Chanee6a99b2007-07-18 21:49:10 -07007615
7616 /* Chip reset on 5780 will reset MSI enable bit,
7617 * so need to restore it.
7618 */
Joe Perches63c3a662011-04-26 08:12:10 +00007619 if (tg3_flag(tp, USING_MSI)) {
Michael Chanee6a99b2007-07-18 21:49:10 -07007620 u16 ctrl;
7621
7622 pci_read_config_word(tp->pdev,
7623 tp->msi_cap + PCI_MSI_FLAGS,
7624 &ctrl);
7625 pci_write_config_word(tp->pdev,
7626 tp->msi_cap + PCI_MSI_FLAGS,
7627 ctrl | PCI_MSI_FLAGS_ENABLE);
7628 val = tr32(MSGINT_MODE);
7629 tw32(MSGINT_MODE, val | MSGINT_MODE_ENABLE);
7630 }
7631 }
7632}
7633
Linus Torvalds1da177e2005-04-16 15:20:36 -07007634/* tp->lock is held. */
7635static int tg3_chip_reset(struct tg3 *tp)
7636{
7637 u32 val;
Michael Chan1ee582d2005-08-09 20:16:46 -07007638 void (*write_op)(struct tg3 *, u32, u32);
Matt Carlson4f125f42009-09-01 12:55:02 +00007639 int i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007640
David S. Millerf49639e2006-06-09 11:58:36 -07007641 tg3_nvram_lock(tp);
7642
Matt Carlson77b483f2008-08-15 14:07:24 -07007643 tg3_ape_lock(tp, TG3_APE_LOCK_GRC);
7644
David S. Millerf49639e2006-06-09 11:58:36 -07007645 /* No matching tg3_nvram_unlock() after this because
7646 * chip reset below will undo the nvram lock.
7647 */
7648 tp->nvram_lock_cnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007649
Michael Chanee6a99b2007-07-18 21:49:10 -07007650 /* GRC_MISC_CFG core clock reset will clear the memory
7651 * enable bit in PCI register 4 and the MSI enable bit
7652 * on some chips, so we save relevant registers here.
7653 */
7654 tg3_save_pci_state(tp);
7655
Michael Chand9ab5ad2006-03-20 22:27:35 -08007656 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752 ||
Joe Perches63c3a662011-04-26 08:12:10 +00007657 tg3_flag(tp, 5755_PLUS))
Michael Chand9ab5ad2006-03-20 22:27:35 -08007658 tw32(GRC_FASTBOOT_PC, 0);
7659
Linus Torvalds1da177e2005-04-16 15:20:36 -07007660 /*
7661 * We must avoid the readl() that normally takes place.
7662 * It locks machines, causes machine checks, and other
7663 * fun things. So, temporarily disable the 5701
7664 * hardware workaround, while we do the reset.
7665 */
Michael Chan1ee582d2005-08-09 20:16:46 -07007666 write_op = tp->write32;
7667 if (write_op == tg3_write_flush_reg32)
7668 tp->write32 = tg3_write32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007669
Michael Chand18edcb2007-03-24 20:57:11 -07007670 /* Prevent the irq handler from reading or writing PCI registers
7671 * during chip reset when the memory enable bit in the PCI command
7672 * register may be cleared. The chip does not generate interrupt
7673 * at this time, but the irq handler may still be called due to irq
7674 * sharing or irqpoll.
7675 */
Joe Perches63c3a662011-04-26 08:12:10 +00007676 tg3_flag_set(tp, CHIP_RESETTING);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007677 for (i = 0; i < tp->irq_cnt; i++) {
7678 struct tg3_napi *tnapi = &tp->napi[i];
7679 if (tnapi->hw_status) {
7680 tnapi->hw_status->status = 0;
7681 tnapi->hw_status->status_tag = 0;
7682 }
7683 tnapi->last_tag = 0;
7684 tnapi->last_irq_tag = 0;
Michael Chanb8fa2f32007-04-06 17:35:37 -07007685 }
Michael Chand18edcb2007-03-24 20:57:11 -07007686 smp_mb();
Matt Carlson4f125f42009-09-01 12:55:02 +00007687
7688 for (i = 0; i < tp->irq_cnt; i++)
7689 synchronize_irq(tp->napi[i].irq_vec);
Michael Chand18edcb2007-03-24 20:57:11 -07007690
Matt Carlson255ca312009-08-25 10:07:27 +00007691 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780) {
7692 val = tr32(TG3_PCIE_LNKCTL) & ~TG3_PCIE_LNKCTL_L1_PLL_PD_EN;
7693 tw32(TG3_PCIE_LNKCTL, val | TG3_PCIE_LNKCTL_L1_PLL_PD_DIS);
7694 }
7695
Linus Torvalds1da177e2005-04-16 15:20:36 -07007696 /* do the reset */
7697 val = GRC_MISC_CFG_CORECLK_RESET;
7698
Joe Perches63c3a662011-04-26 08:12:10 +00007699 if (tg3_flag(tp, PCI_EXPRESS)) {
Matt Carlson88075d92010-08-02 11:25:58 +00007700 /* Force PCIe 1.0a mode */
7701 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
Joe Perches63c3a662011-04-26 08:12:10 +00007702 !tg3_flag(tp, 57765_PLUS) &&
Matt Carlson88075d92010-08-02 11:25:58 +00007703 tr32(TG3_PCIE_PHY_TSTCTL) ==
7704 (TG3_PCIE_PHY_TSTCTL_PCIE10 | TG3_PCIE_PHY_TSTCTL_PSCRAM))
7705 tw32(TG3_PCIE_PHY_TSTCTL, TG3_PCIE_PHY_TSTCTL_PSCRAM);
7706
Linus Torvalds1da177e2005-04-16 15:20:36 -07007707 if (tp->pci_chip_rev_id != CHIPREV_ID_5750_A0) {
7708 tw32(GRC_MISC_CFG, (1 << 29));
7709 val |= (1 << 29);
7710 }
7711 }
7712
Michael Chanb5d37722006-09-27 16:06:21 -07007713 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
7714 tw32(VCPU_STATUS, tr32(VCPU_STATUS) | VCPU_STATUS_DRV_RESET);
7715 tw32(GRC_VCPU_EXT_CTRL,
7716 tr32(GRC_VCPU_EXT_CTRL) & ~GRC_VCPU_EXT_CTRL_HALT_CPU);
7717 }
7718
Matt Carlsonf37500d2010-08-02 11:25:59 +00007719 /* Manage gphy power for all CPMU absent PCIe devices. */
Joe Perches63c3a662011-04-26 08:12:10 +00007720 if (tg3_flag(tp, 5705_PLUS) && !tg3_flag(tp, CPMU_PRESENT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007721 val |= GRC_MISC_CFG_KEEP_GPHY_POWER;
Matt Carlsonf37500d2010-08-02 11:25:59 +00007722
Linus Torvalds1da177e2005-04-16 15:20:36 -07007723 tw32(GRC_MISC_CFG, val);
7724
Michael Chan1ee582d2005-08-09 20:16:46 -07007725 /* restore 5701 hardware bug workaround write method */
7726 tp->write32 = write_op;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007727
7728 /* Unfortunately, we have to delay before the PCI read back.
7729 * Some 575X chips even will not respond to a PCI cfg access
7730 * when the reset command is given to the chip.
7731 *
7732 * How do these hardware designers expect things to work
7733 * properly if the PCI write is posted for a long period
7734 * of time? It is always necessary to have some method by
7735 * which a register read back can occur to push the write
7736 * out which does the reset.
7737 *
7738 * For most tg3 variants the trick below was working.
7739 * Ho hum...
7740 */
7741 udelay(120);
7742
7743 /* Flush PCI posted writes. The normal MMIO registers
7744 * are inaccessible at this time so this is the only
7745 * way to make this reliably (actually, this is no longer
7746 * the case, see above). I tried to use indirect
7747 * register read/write but this upset some 5701 variants.
7748 */
7749 pci_read_config_dword(tp->pdev, PCI_COMMAND, &val);
7750
7751 udelay(120);
7752
Jon Mason708ebb32011-06-27 12:56:50 +00007753 if (tg3_flag(tp, PCI_EXPRESS) && pci_pcie_cap(tp->pdev)) {
Matt Carlsone7126992009-08-25 10:08:16 +00007754 u16 val16;
7755
Linus Torvalds1da177e2005-04-16 15:20:36 -07007756 if (tp->pci_chip_rev_id == CHIPREV_ID_5750_A0) {
7757 int i;
7758 u32 cfg_val;
7759
7760 /* Wait for link training to complete. */
7761 for (i = 0; i < 5000; i++)
7762 udelay(100);
7763
7764 pci_read_config_dword(tp->pdev, 0xc4, &cfg_val);
7765 pci_write_config_dword(tp->pdev, 0xc4,
7766 cfg_val | (1 << 15));
7767 }
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007768
Matt Carlsone7126992009-08-25 10:08:16 +00007769 /* Clear the "no snoop" and "relaxed ordering" bits. */
7770 pci_read_config_word(tp->pdev,
Jon Mason708ebb32011-06-27 12:56:50 +00007771 pci_pcie_cap(tp->pdev) + PCI_EXP_DEVCTL,
Matt Carlsone7126992009-08-25 10:08:16 +00007772 &val16);
7773 val16 &= ~(PCI_EXP_DEVCTL_RELAX_EN |
7774 PCI_EXP_DEVCTL_NOSNOOP_EN);
7775 /*
7776 * Older PCIe devices only support the 128 byte
7777 * MPS setting. Enforce the restriction.
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007778 */
Joe Perches63c3a662011-04-26 08:12:10 +00007779 if (!tg3_flag(tp, CPMU_PRESENT))
Matt Carlsone7126992009-08-25 10:08:16 +00007780 val16 &= ~PCI_EXP_DEVCTL_PAYLOAD;
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007781 pci_write_config_word(tp->pdev,
Jon Mason708ebb32011-06-27 12:56:50 +00007782 pci_pcie_cap(tp->pdev) + PCI_EXP_DEVCTL,
Matt Carlsone7126992009-08-25 10:08:16 +00007783 val16);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007784
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007785 /* Clear error status */
7786 pci_write_config_word(tp->pdev,
Jon Mason708ebb32011-06-27 12:56:50 +00007787 pci_pcie_cap(tp->pdev) + PCI_EXP_DEVSTA,
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007788 PCI_EXP_DEVSTA_CED |
7789 PCI_EXP_DEVSTA_NFED |
7790 PCI_EXP_DEVSTA_FED |
7791 PCI_EXP_DEVSTA_URD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007792 }
7793
Michael Chanee6a99b2007-07-18 21:49:10 -07007794 tg3_restore_pci_state(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007795
Joe Perches63c3a662011-04-26 08:12:10 +00007796 tg3_flag_clear(tp, CHIP_RESETTING);
7797 tg3_flag_clear(tp, ERROR_PROCESSED);
Michael Chand18edcb2007-03-24 20:57:11 -07007798
Michael Chanee6a99b2007-07-18 21:49:10 -07007799 val = 0;
Joe Perches63c3a662011-04-26 08:12:10 +00007800 if (tg3_flag(tp, 5780_CLASS))
Michael Chan4cf78e42005-07-25 12:29:19 -07007801 val = tr32(MEMARB_MODE);
Michael Chanee6a99b2007-07-18 21:49:10 -07007802 tw32(MEMARB_MODE, val | MEMARB_MODE_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007803
7804 if (tp->pci_chip_rev_id == CHIPREV_ID_5750_A3) {
7805 tg3_stop_fw(tp);
7806 tw32(0x5000, 0x400);
7807 }
7808
7809 tw32(GRC_MODE, tp->grc_mode);
7810
7811 if (tp->pci_chip_rev_id == CHIPREV_ID_5705_A0) {
Andy Gospodarekab0049b2007-09-06 20:42:14 +01007812 val = tr32(0xc4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007813
7814 tw32(0xc4, val | (1 << 15));
7815 }
7816
7817 if ((tp->nic_sram_data_cfg & NIC_SRAM_DATA_CFG_MINI_PCI) != 0 &&
7818 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
7819 tp->pci_clock_ctrl |= CLOCK_CTRL_CLKRUN_OENABLE;
7820 if (tp->pci_chip_rev_id == CHIPREV_ID_5705_A0)
7821 tp->pci_clock_ctrl |= CLOCK_CTRL_FORCE_CLKRUN;
7822 tw32(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl);
7823 }
7824
Matt Carlsonf07e9af2010-08-02 11:26:07 +00007825 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Matt Carlson9e975cc2011-07-20 10:20:50 +00007826 tp->mac_mode = MAC_MODE_PORT_MODE_TBI;
Matt Carlsond2394e6b2010-11-24 08:31:47 +00007827 val = tp->mac_mode;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00007828 } else if (tp->phy_flags & TG3_PHYFLG_MII_SERDES) {
Matt Carlson9e975cc2011-07-20 10:20:50 +00007829 tp->mac_mode = MAC_MODE_PORT_MODE_GMII;
Matt Carlsond2394e6b2010-11-24 08:31:47 +00007830 val = tp->mac_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007831 } else
Matt Carlsond2394e6b2010-11-24 08:31:47 +00007832 val = 0;
7833
7834 tw32_f(MAC_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007835 udelay(40);
7836
Matt Carlson77b483f2008-08-15 14:07:24 -07007837 tg3_ape_unlock(tp, TG3_APE_LOCK_GRC);
7838
Michael Chan7a6f4362006-09-27 16:03:31 -07007839 err = tg3_poll_fw(tp);
7840 if (err)
7841 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007842
Matt Carlson0a9140c2009-08-28 12:27:50 +00007843 tg3_mdio_start(tp);
7844
Joe Perches63c3a662011-04-26 08:12:10 +00007845 if (tg3_flag(tp, PCI_EXPRESS) &&
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00007846 tp->pci_chip_rev_id != CHIPREV_ID_5750_A0 &&
7847 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
Joe Perches63c3a662011-04-26 08:12:10 +00007848 !tg3_flag(tp, 57765_PLUS)) {
Andy Gospodarekab0049b2007-09-06 20:42:14 +01007849 val = tr32(0x7c00);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007850
7851 tw32(0x7c00, val | (1 << 25));
7852 }
7853
Matt Carlsond78b59f2011-04-05 14:22:46 +00007854 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
7855 val = tr32(TG3_CPMU_CLCK_ORIDE);
7856 tw32(TG3_CPMU_CLCK_ORIDE, val & ~CPMU_CLCK_ORIDE_MAC_ORIDE_EN);
7857 }
7858
Linus Torvalds1da177e2005-04-16 15:20:36 -07007859 /* Reprobe ASF enable state. */
Joe Perches63c3a662011-04-26 08:12:10 +00007860 tg3_flag_clear(tp, ENABLE_ASF);
7861 tg3_flag_clear(tp, ASF_NEW_HANDSHAKE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007862 tg3_read_mem(tp, NIC_SRAM_DATA_SIG, &val);
7863 if (val == NIC_SRAM_DATA_SIG_MAGIC) {
7864 u32 nic_cfg;
7865
7866 tg3_read_mem(tp, NIC_SRAM_DATA_CFG, &nic_cfg);
7867 if (nic_cfg & NIC_SRAM_DATA_CFG_ASF_ENABLE) {
Joe Perches63c3a662011-04-26 08:12:10 +00007868 tg3_flag_set(tp, ENABLE_ASF);
Matt Carlson4ba526c2008-08-15 14:10:04 -07007869 tp->last_event_jiffies = jiffies;
Joe Perches63c3a662011-04-26 08:12:10 +00007870 if (tg3_flag(tp, 5750_PLUS))
7871 tg3_flag_set(tp, ASF_NEW_HANDSHAKE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007872 }
7873 }
7874
7875 return 0;
7876}
7877
Matt Carlson92feeab2011-12-08 14:40:14 +00007878static struct rtnl_link_stats64 *tg3_get_stats64(struct net_device *,
7879 struct rtnl_link_stats64 *);
7880static struct tg3_ethtool_stats *tg3_get_estats(struct tg3 *,
7881 struct tg3_ethtool_stats *);
7882
Linus Torvalds1da177e2005-04-16 15:20:36 -07007883/* tp->lock is held. */
Michael Chan944d9802005-05-29 14:57:48 -07007884static int tg3_halt(struct tg3 *tp, int kind, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007885{
7886 int err;
7887
7888 tg3_stop_fw(tp);
7889
Michael Chan944d9802005-05-29 14:57:48 -07007890 tg3_write_sig_pre_reset(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007891
David S. Millerb3b7d6b2005-05-05 14:40:20 -07007892 tg3_abort_hw(tp, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007893 err = tg3_chip_reset(tp);
7894
Matt Carlsondaba2a62009-04-20 06:58:52 +00007895 __tg3_set_mac_addr(tp, 0);
7896
Michael Chan944d9802005-05-29 14:57:48 -07007897 tg3_write_sig_legacy(tp, kind);
7898 tg3_write_sig_post_reset(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007899
Matt Carlson92feeab2011-12-08 14:40:14 +00007900 if (tp->hw_stats) {
7901 /* Save the stats across chip resets... */
7902 tg3_get_stats64(tp->dev, &tp->net_stats_prev),
7903 tg3_get_estats(tp, &tp->estats_prev);
7904
7905 /* And make sure the next sample is new data */
7906 memset(tp->hw_stats, 0, sizeof(struct tg3_hw_stats));
7907 }
7908
Linus Torvalds1da177e2005-04-16 15:20:36 -07007909 if (err)
7910 return err;
7911
7912 return 0;
7913}
7914
Linus Torvalds1da177e2005-04-16 15:20:36 -07007915static int tg3_set_mac_addr(struct net_device *dev, void *p)
7916{
7917 struct tg3 *tp = netdev_priv(dev);
7918 struct sockaddr *addr = p;
Michael Chan986e0ae2007-05-05 12:10:20 -07007919 int err = 0, skip_mac_1 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007920
Michael Chanf9804dd2005-09-27 12:13:10 -07007921 if (!is_valid_ether_addr(addr->sa_data))
7922 return -EINVAL;
7923
Linus Torvalds1da177e2005-04-16 15:20:36 -07007924 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
7925
Michael Chane75f7c92006-03-20 21:33:26 -08007926 if (!netif_running(dev))
7927 return 0;
7928
Joe Perches63c3a662011-04-26 08:12:10 +00007929 if (tg3_flag(tp, ENABLE_ASF)) {
Michael Chan986e0ae2007-05-05 12:10:20 -07007930 u32 addr0_high, addr0_low, addr1_high, addr1_low;
Michael Chan58712ef2006-04-29 18:58:01 -07007931
Michael Chan986e0ae2007-05-05 12:10:20 -07007932 addr0_high = tr32(MAC_ADDR_0_HIGH);
7933 addr0_low = tr32(MAC_ADDR_0_LOW);
7934 addr1_high = tr32(MAC_ADDR_1_HIGH);
7935 addr1_low = tr32(MAC_ADDR_1_LOW);
7936
7937 /* Skip MAC addr 1 if ASF is using it. */
7938 if ((addr0_high != addr1_high || addr0_low != addr1_low) &&
7939 !(addr1_high == 0 && addr1_low == 0))
7940 skip_mac_1 = 1;
Michael Chan58712ef2006-04-29 18:58:01 -07007941 }
Michael Chan986e0ae2007-05-05 12:10:20 -07007942 spin_lock_bh(&tp->lock);
7943 __tg3_set_mac_addr(tp, skip_mac_1);
7944 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007945
Michael Chanb9ec6c12006-07-25 16:37:27 -07007946 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007947}
7948
7949/* tp->lock is held. */
7950static void tg3_set_bdinfo(struct tg3 *tp, u32 bdinfo_addr,
7951 dma_addr_t mapping, u32 maxlen_flags,
7952 u32 nic_addr)
7953{
7954 tg3_write_mem(tp,
7955 (bdinfo_addr + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH),
7956 ((u64) mapping >> 32));
7957 tg3_write_mem(tp,
7958 (bdinfo_addr + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW),
7959 ((u64) mapping & 0xffffffff));
7960 tg3_write_mem(tp,
7961 (bdinfo_addr + TG3_BDINFO_MAXLEN_FLAGS),
7962 maxlen_flags);
7963
Joe Perches63c3a662011-04-26 08:12:10 +00007964 if (!tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007965 tg3_write_mem(tp,
7966 (bdinfo_addr + TG3_BDINFO_NIC_ADDR),
7967 nic_addr);
7968}
7969
7970static void __tg3_set_rx_mode(struct net_device *);
Michael Chand244c892005-07-05 14:42:33 -07007971static void __tg3_set_coalesce(struct tg3 *tp, struct ethtool_coalesce *ec)
David S. Miller15f98502005-05-18 22:49:26 -07007972{
Matt Carlsonb6080e12009-09-01 13:12:00 +00007973 int i;
7974
Joe Perches63c3a662011-04-26 08:12:10 +00007975 if (!tg3_flag(tp, ENABLE_TSS)) {
Matt Carlsonb6080e12009-09-01 13:12:00 +00007976 tw32(HOSTCC_TXCOL_TICKS, ec->tx_coalesce_usecs);
7977 tw32(HOSTCC_TXMAX_FRAMES, ec->tx_max_coalesced_frames);
7978 tw32(HOSTCC_TXCOAL_MAXF_INT, ec->tx_max_coalesced_frames_irq);
Matt Carlsonb6080e12009-09-01 13:12:00 +00007979 } else {
7980 tw32(HOSTCC_TXCOL_TICKS, 0);
7981 tw32(HOSTCC_TXMAX_FRAMES, 0);
7982 tw32(HOSTCC_TXCOAL_MAXF_INT, 0);
Matt Carlson19cfaec2009-12-03 08:36:20 +00007983 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00007984
Joe Perches63c3a662011-04-26 08:12:10 +00007985 if (!tg3_flag(tp, ENABLE_RSS)) {
Matt Carlson19cfaec2009-12-03 08:36:20 +00007986 tw32(HOSTCC_RXCOL_TICKS, ec->rx_coalesce_usecs);
7987 tw32(HOSTCC_RXMAX_FRAMES, ec->rx_max_coalesced_frames);
7988 tw32(HOSTCC_RXCOAL_MAXF_INT, ec->rx_max_coalesced_frames_irq);
7989 } else {
Matt Carlsonb6080e12009-09-01 13:12:00 +00007990 tw32(HOSTCC_RXCOL_TICKS, 0);
7991 tw32(HOSTCC_RXMAX_FRAMES, 0);
7992 tw32(HOSTCC_RXCOAL_MAXF_INT, 0);
David S. Miller15f98502005-05-18 22:49:26 -07007993 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00007994
Joe Perches63c3a662011-04-26 08:12:10 +00007995 if (!tg3_flag(tp, 5705_PLUS)) {
David S. Miller15f98502005-05-18 22:49:26 -07007996 u32 val = ec->stats_block_coalesce_usecs;
7997
Matt Carlsonb6080e12009-09-01 13:12:00 +00007998 tw32(HOSTCC_RXCOAL_TICK_INT, ec->rx_coalesce_usecs_irq);
7999 tw32(HOSTCC_TXCOAL_TICK_INT, ec->tx_coalesce_usecs_irq);
8000
David S. Miller15f98502005-05-18 22:49:26 -07008001 if (!netif_carrier_ok(tp->dev))
8002 val = 0;
8003
8004 tw32(HOSTCC_STAT_COAL_TICKS, val);
8005 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00008006
8007 for (i = 0; i < tp->irq_cnt - 1; i++) {
8008 u32 reg;
8009
8010 reg = HOSTCC_RXCOL_TICKS_VEC1 + i * 0x18;
8011 tw32(reg, ec->rx_coalesce_usecs);
Matt Carlsonb6080e12009-09-01 13:12:00 +00008012 reg = HOSTCC_RXMAX_FRAMES_VEC1 + i * 0x18;
8013 tw32(reg, ec->rx_max_coalesced_frames);
Matt Carlsonb6080e12009-09-01 13:12:00 +00008014 reg = HOSTCC_RXCOAL_MAXF_INT_VEC1 + i * 0x18;
8015 tw32(reg, ec->rx_max_coalesced_frames_irq);
Matt Carlson19cfaec2009-12-03 08:36:20 +00008016
Joe Perches63c3a662011-04-26 08:12:10 +00008017 if (tg3_flag(tp, ENABLE_TSS)) {
Matt Carlson19cfaec2009-12-03 08:36:20 +00008018 reg = HOSTCC_TXCOL_TICKS_VEC1 + i * 0x18;
8019 tw32(reg, ec->tx_coalesce_usecs);
8020 reg = HOSTCC_TXMAX_FRAMES_VEC1 + i * 0x18;
8021 tw32(reg, ec->tx_max_coalesced_frames);
8022 reg = HOSTCC_TXCOAL_MAXF_INT_VEC1 + i * 0x18;
8023 tw32(reg, ec->tx_max_coalesced_frames_irq);
8024 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00008025 }
8026
8027 for (; i < tp->irq_max - 1; i++) {
8028 tw32(HOSTCC_RXCOL_TICKS_VEC1 + i * 0x18, 0);
Matt Carlsonb6080e12009-09-01 13:12:00 +00008029 tw32(HOSTCC_RXMAX_FRAMES_VEC1 + i * 0x18, 0);
Matt Carlsonb6080e12009-09-01 13:12:00 +00008030 tw32(HOSTCC_RXCOAL_MAXF_INT_VEC1 + i * 0x18, 0);
Matt Carlson19cfaec2009-12-03 08:36:20 +00008031
Joe Perches63c3a662011-04-26 08:12:10 +00008032 if (tg3_flag(tp, ENABLE_TSS)) {
Matt Carlson19cfaec2009-12-03 08:36:20 +00008033 tw32(HOSTCC_TXCOL_TICKS_VEC1 + i * 0x18, 0);
8034 tw32(HOSTCC_TXMAX_FRAMES_VEC1 + i * 0x18, 0);
8035 tw32(HOSTCC_TXCOAL_MAXF_INT_VEC1 + i * 0x18, 0);
8036 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00008037 }
David S. Miller15f98502005-05-18 22:49:26 -07008038}
Linus Torvalds1da177e2005-04-16 15:20:36 -07008039
8040/* tp->lock is held. */
Matt Carlson2d31eca2009-09-01 12:53:31 +00008041static void tg3_rings_reset(struct tg3 *tp)
8042{
8043 int i;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008044 u32 stblk, txrcb, rxrcb, limit;
Matt Carlson2d31eca2009-09-01 12:53:31 +00008045 struct tg3_napi *tnapi = &tp->napi[0];
8046
8047 /* Disable all transmit rings but the first. */
Joe Perches63c3a662011-04-26 08:12:10 +00008048 if (!tg3_flag(tp, 5705_PLUS))
Matt Carlson2d31eca2009-09-01 12:53:31 +00008049 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 16;
Joe Perches63c3a662011-04-26 08:12:10 +00008050 else if (tg3_flag(tp, 5717_PLUS))
Matt Carlson3d377282010-10-14 10:37:39 +00008051 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 4;
Matt Carlsonb703df62009-12-03 08:36:21 +00008052 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
8053 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 2;
Matt Carlson2d31eca2009-09-01 12:53:31 +00008054 else
8055 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE;
8056
8057 for (txrcb = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE;
8058 txrcb < limit; txrcb += TG3_BDINFO_SIZE)
8059 tg3_write_mem(tp, txrcb + TG3_BDINFO_MAXLEN_FLAGS,
8060 BDINFO_FLAGS_DISABLED);
8061
8062
8063 /* Disable all receive return rings but the first. */
Joe Perches63c3a662011-04-26 08:12:10 +00008064 if (tg3_flag(tp, 5717_PLUS))
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00008065 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 17;
Joe Perches63c3a662011-04-26 08:12:10 +00008066 else if (!tg3_flag(tp, 5705_PLUS))
Matt Carlson2d31eca2009-09-01 12:53:31 +00008067 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 16;
Matt Carlsonb703df62009-12-03 08:36:21 +00008068 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
8069 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Matt Carlson2d31eca2009-09-01 12:53:31 +00008070 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 4;
8071 else
8072 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE;
8073
8074 for (rxrcb = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE;
8075 rxrcb < limit; rxrcb += TG3_BDINFO_SIZE)
8076 tg3_write_mem(tp, rxrcb + TG3_BDINFO_MAXLEN_FLAGS,
8077 BDINFO_FLAGS_DISABLED);
8078
8079 /* Disable interrupts */
8080 tw32_mailbox_f(tp->napi[0].int_mbox, 1);
Matt Carlson0e6cf6a2011-06-13 13:38:55 +00008081 tp->napi[0].chk_msi_cnt = 0;
8082 tp->napi[0].last_rx_cons = 0;
8083 tp->napi[0].last_tx_cons = 0;
Matt Carlson2d31eca2009-09-01 12:53:31 +00008084
8085 /* Zero mailbox registers. */
Joe Perches63c3a662011-04-26 08:12:10 +00008086 if (tg3_flag(tp, SUPPORT_MSIX)) {
Matt Carlson6fd45cb2010-09-15 08:59:57 +00008087 for (i = 1; i < tp->irq_max; i++) {
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008088 tp->napi[i].tx_prod = 0;
8089 tp->napi[i].tx_cons = 0;
Joe Perches63c3a662011-04-26 08:12:10 +00008090 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlsonc2353a32010-01-20 16:58:08 +00008091 tw32_mailbox(tp->napi[i].prodmbox, 0);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008092 tw32_rx_mbox(tp->napi[i].consmbox, 0);
8093 tw32_mailbox_f(tp->napi[i].int_mbox, 1);
Matt Carlson7f230732011-08-31 11:44:48 +00008094 tp->napi[i].chk_msi_cnt = 0;
Matt Carlson0e6cf6a2011-06-13 13:38:55 +00008095 tp->napi[i].last_rx_cons = 0;
8096 tp->napi[i].last_tx_cons = 0;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008097 }
Joe Perches63c3a662011-04-26 08:12:10 +00008098 if (!tg3_flag(tp, ENABLE_TSS))
Matt Carlsonc2353a32010-01-20 16:58:08 +00008099 tw32_mailbox(tp->napi[0].prodmbox, 0);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008100 } else {
8101 tp->napi[0].tx_prod = 0;
8102 tp->napi[0].tx_cons = 0;
8103 tw32_mailbox(tp->napi[0].prodmbox, 0);
8104 tw32_rx_mbox(tp->napi[0].consmbox, 0);
8105 }
Matt Carlson2d31eca2009-09-01 12:53:31 +00008106
8107 /* Make sure the NIC-based send BD rings are disabled. */
Joe Perches63c3a662011-04-26 08:12:10 +00008108 if (!tg3_flag(tp, 5705_PLUS)) {
Matt Carlson2d31eca2009-09-01 12:53:31 +00008109 u32 mbox = MAILBOX_SNDNIC_PROD_IDX_0 + TG3_64BIT_REG_LOW;
8110 for (i = 0; i < 16; i++)
8111 tw32_tx_mbox(mbox + i * 8, 0);
8112 }
8113
8114 txrcb = NIC_SRAM_SEND_RCB;
8115 rxrcb = NIC_SRAM_RCV_RET_RCB;
8116
8117 /* Clear status block in ram. */
8118 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
8119
8120 /* Set status block DMA address */
8121 tw32(HOSTCC_STATUS_BLK_HOST_ADDR + TG3_64BIT_REG_HIGH,
8122 ((u64) tnapi->status_mapping >> 32));
8123 tw32(HOSTCC_STATUS_BLK_HOST_ADDR + TG3_64BIT_REG_LOW,
8124 ((u64) tnapi->status_mapping & 0xffffffff));
8125
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008126 if (tnapi->tx_ring) {
8127 tg3_set_bdinfo(tp, txrcb, tnapi->tx_desc_mapping,
8128 (TG3_TX_RING_SIZE <<
8129 BDINFO_FLAGS_MAXLEN_SHIFT),
8130 NIC_SRAM_TX_BUFFER_DESC);
8131 txrcb += TG3_BDINFO_SIZE;
8132 }
Matt Carlson2d31eca2009-09-01 12:53:31 +00008133
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008134 if (tnapi->rx_rcb) {
8135 tg3_set_bdinfo(tp, rxrcb, tnapi->rx_rcb_mapping,
Matt Carlson7cb32cf2010-09-30 10:34:36 +00008136 (tp->rx_ret_ring_mask + 1) <<
8137 BDINFO_FLAGS_MAXLEN_SHIFT, 0);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008138 rxrcb += TG3_BDINFO_SIZE;
8139 }
8140
8141 stblk = HOSTCC_STATBLCK_RING1;
8142
8143 for (i = 1, tnapi++; i < tp->irq_cnt; i++, tnapi++) {
8144 u64 mapping = (u64)tnapi->status_mapping;
8145 tw32(stblk + TG3_64BIT_REG_HIGH, mapping >> 32);
8146 tw32(stblk + TG3_64BIT_REG_LOW, mapping & 0xffffffff);
8147
8148 /* Clear status block in ram. */
8149 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
8150
Matt Carlson19cfaec2009-12-03 08:36:20 +00008151 if (tnapi->tx_ring) {
8152 tg3_set_bdinfo(tp, txrcb, tnapi->tx_desc_mapping,
8153 (TG3_TX_RING_SIZE <<
8154 BDINFO_FLAGS_MAXLEN_SHIFT),
8155 NIC_SRAM_TX_BUFFER_DESC);
8156 txrcb += TG3_BDINFO_SIZE;
8157 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008158
8159 tg3_set_bdinfo(tp, rxrcb, tnapi->rx_rcb_mapping,
Matt Carlson7cb32cf2010-09-30 10:34:36 +00008160 ((tp->rx_ret_ring_mask + 1) <<
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008161 BDINFO_FLAGS_MAXLEN_SHIFT), 0);
8162
8163 stblk += 8;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008164 rxrcb += TG3_BDINFO_SIZE;
8165 }
Matt Carlson2d31eca2009-09-01 12:53:31 +00008166}
8167
Matt Carlsoneb07a942011-04-20 07:57:36 +00008168static void tg3_setup_rxbd_thresholds(struct tg3 *tp)
8169{
8170 u32 val, bdcache_maxcnt, host_rep_thresh, nic_rep_thresh;
8171
Joe Perches63c3a662011-04-26 08:12:10 +00008172 if (!tg3_flag(tp, 5750_PLUS) ||
8173 tg3_flag(tp, 5780_CLASS) ||
Matt Carlsoneb07a942011-04-20 07:57:36 +00008174 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 ||
Matt Carlson513aa6e2011-11-21 15:01:18 +00008175 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752 ||
8176 tg3_flag(tp, 57765_PLUS))
Matt Carlsoneb07a942011-04-20 07:57:36 +00008177 bdcache_maxcnt = TG3_SRAM_RX_STD_BDCACHE_SIZE_5700;
8178 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
8179 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787)
8180 bdcache_maxcnt = TG3_SRAM_RX_STD_BDCACHE_SIZE_5755;
8181 else
8182 bdcache_maxcnt = TG3_SRAM_RX_STD_BDCACHE_SIZE_5906;
8183
8184 nic_rep_thresh = min(bdcache_maxcnt / 2, tp->rx_std_max_post);
8185 host_rep_thresh = max_t(u32, tp->rx_pending / 8, 1);
8186
8187 val = min(nic_rep_thresh, host_rep_thresh);
8188 tw32(RCVBDI_STD_THRESH, val);
8189
Joe Perches63c3a662011-04-26 08:12:10 +00008190 if (tg3_flag(tp, 57765_PLUS))
Matt Carlsoneb07a942011-04-20 07:57:36 +00008191 tw32(STD_REPLENISH_LWM, bdcache_maxcnt);
8192
Joe Perches63c3a662011-04-26 08:12:10 +00008193 if (!tg3_flag(tp, JUMBO_CAPABLE) || tg3_flag(tp, 5780_CLASS))
Matt Carlsoneb07a942011-04-20 07:57:36 +00008194 return;
8195
Matt Carlson513aa6e2011-11-21 15:01:18 +00008196 bdcache_maxcnt = TG3_SRAM_RX_JMB_BDCACHE_SIZE_5700;
Matt Carlsoneb07a942011-04-20 07:57:36 +00008197
8198 host_rep_thresh = max_t(u32, tp->rx_jumbo_pending / 8, 1);
8199
8200 val = min(bdcache_maxcnt / 2, host_rep_thresh);
8201 tw32(RCVBDI_JUMBO_THRESH, val);
8202
Joe Perches63c3a662011-04-26 08:12:10 +00008203 if (tg3_flag(tp, 57765_PLUS))
Matt Carlsoneb07a942011-04-20 07:57:36 +00008204 tw32(JMB_REPLENISH_LWM, bdcache_maxcnt);
8205}
8206
Matt Carlson2d31eca2009-09-01 12:53:31 +00008207/* tp->lock is held. */
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07008208static int tg3_reset_hw(struct tg3 *tp, int reset_phy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008209{
8210 u32 val, rdmac_mode;
8211 int i, err, limit;
Matt Carlson8fea32b2010-09-15 08:59:58 +00008212 struct tg3_rx_prodring_set *tpr = &tp->napi[0].prodring;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008213
8214 tg3_disable_ints(tp);
8215
8216 tg3_stop_fw(tp);
8217
8218 tg3_write_sig_pre_reset(tp, RESET_KIND_INIT);
8219
Joe Perches63c3a662011-04-26 08:12:10 +00008220 if (tg3_flag(tp, INIT_COMPLETE))
Michael Chane6de8ad2005-05-05 14:42:41 -07008221 tg3_abort_hw(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008222
Matt Carlson699c0192010-12-06 08:28:51 +00008223 /* Enable MAC control of LPI */
8224 if (tp->phy_flags & TG3_PHYFLG_EEE_CAP) {
8225 tw32_f(TG3_CPMU_EEE_LNKIDL_CTRL,
8226 TG3_CPMU_EEE_LNKIDL_PCIE_NL0 |
8227 TG3_CPMU_EEE_LNKIDL_UART_IDL);
8228
8229 tw32_f(TG3_CPMU_EEE_CTRL,
8230 TG3_CPMU_EEE_CTRL_EXIT_20_1_US);
8231
Matt Carlsona386b902010-12-06 08:28:53 +00008232 val = TG3_CPMU_EEEMD_ERLY_L1_XIT_DET |
8233 TG3_CPMU_EEEMD_LPI_IN_TX |
8234 TG3_CPMU_EEEMD_LPI_IN_RX |
8235 TG3_CPMU_EEEMD_EEE_ENABLE;
8236
8237 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717)
8238 val |= TG3_CPMU_EEEMD_SND_IDX_DET_EN;
8239
Joe Perches63c3a662011-04-26 08:12:10 +00008240 if (tg3_flag(tp, ENABLE_APE))
Matt Carlsona386b902010-12-06 08:28:53 +00008241 val |= TG3_CPMU_EEEMD_APE_TX_DET_EN;
8242
8243 tw32_f(TG3_CPMU_EEE_MODE, val);
8244
8245 tw32_f(TG3_CPMU_EEE_DBTMR1,
8246 TG3_CPMU_DBTMR1_PCIEXIT_2047US |
8247 TG3_CPMU_DBTMR1_LNKIDLE_2047US);
8248
8249 tw32_f(TG3_CPMU_EEE_DBTMR2,
Matt Carlsond7f2ab22011-01-25 15:58:56 +00008250 TG3_CPMU_DBTMR2_APE_TX_2047US |
Matt Carlsona386b902010-12-06 08:28:53 +00008251 TG3_CPMU_DBTMR2_TXIDXEQ_2047US);
Matt Carlson699c0192010-12-06 08:28:51 +00008252 }
8253
Matt Carlson603f1172010-02-12 14:47:10 +00008254 if (reset_phy)
Michael Chand4d2c552006-03-20 17:47:20 -08008255 tg3_phy_reset(tp);
8256
Linus Torvalds1da177e2005-04-16 15:20:36 -07008257 err = tg3_chip_reset(tp);
8258 if (err)
8259 return err;
8260
8261 tg3_write_sig_legacy(tp, RESET_KIND_INIT);
8262
Matt Carlsonbcb37f62008-11-03 16:52:09 -08008263 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX) {
Matt Carlsond30cdd22007-10-07 23:28:35 -07008264 val = tr32(TG3_CPMU_CTRL);
8265 val &= ~(CPMU_CTRL_LINK_AWARE_MODE | CPMU_CTRL_LINK_IDLE_MODE);
8266 tw32(TG3_CPMU_CTRL, val);
Matt Carlson9acb9612007-11-12 21:10:06 -08008267
8268 val = tr32(TG3_CPMU_LSPD_10MB_CLK);
8269 val &= ~CPMU_LSPD_10MB_MACCLK_MASK;
8270 val |= CPMU_LSPD_10MB_MACCLK_6_25;
8271 tw32(TG3_CPMU_LSPD_10MB_CLK, val);
8272
8273 val = tr32(TG3_CPMU_LNK_AWARE_PWRMD);
8274 val &= ~CPMU_LNK_AWARE_MACCLK_MASK;
8275 val |= CPMU_LNK_AWARE_MACCLK_6_25;
8276 tw32(TG3_CPMU_LNK_AWARE_PWRMD, val);
8277
8278 val = tr32(TG3_CPMU_HST_ACC);
8279 val &= ~CPMU_HST_ACC_MACCLK_MASK;
8280 val |= CPMU_HST_ACC_MACCLK_6_25;
8281 tw32(TG3_CPMU_HST_ACC, val);
Matt Carlsond30cdd22007-10-07 23:28:35 -07008282 }
8283
Matt Carlson33466d92009-04-20 06:57:41 +00008284 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780) {
8285 val = tr32(PCIE_PWR_MGMT_THRESH) & ~PCIE_PWR_MGMT_L1_THRESH_MSK;
8286 val |= PCIE_PWR_MGMT_EXT_ASPM_TMR_EN |
8287 PCIE_PWR_MGMT_L1_THRESH_4MS;
8288 tw32(PCIE_PWR_MGMT_THRESH, val);
Matt Carlson521e6b92009-08-25 10:06:01 +00008289
8290 val = tr32(TG3_PCIE_EIDLE_DELAY) & ~TG3_PCIE_EIDLE_DELAY_MASK;
8291 tw32(TG3_PCIE_EIDLE_DELAY, val | TG3_PCIE_EIDLE_DELAY_13_CLKS);
8292
8293 tw32(TG3_CORR_ERR_STAT, TG3_CORR_ERR_STAT_CLEAR);
Matt Carlson33466d92009-04-20 06:57:41 +00008294
Matt Carlsonf40386c2009-11-02 14:24:02 +00008295 val = tr32(TG3_PCIE_LNKCTL) & ~TG3_PCIE_LNKCTL_L1_PLL_PD_EN;
8296 tw32(TG3_PCIE_LNKCTL, val | TG3_PCIE_LNKCTL_L1_PLL_PD_DIS);
Matt Carlson255ca312009-08-25 10:07:27 +00008297 }
8298
Joe Perches63c3a662011-04-26 08:12:10 +00008299 if (tg3_flag(tp, L1PLLPD_EN)) {
Matt Carlson614b0592010-01-20 16:58:02 +00008300 u32 grc_mode = tr32(GRC_MODE);
8301
8302 /* Access the lower 1K of PL PCIE block registers. */
8303 val = grc_mode & ~GRC_MODE_PCIE_PORT_MASK;
8304 tw32(GRC_MODE, val | GRC_MODE_PCIE_PL_SEL);
8305
8306 val = tr32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL1);
8307 tw32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL1,
8308 val | TG3_PCIE_PL_LO_PHYCTL1_L1PLLPD_EN);
8309
8310 tw32(GRC_MODE, grc_mode);
8311 }
8312
Matt Carlson5093eed2010-11-24 08:31:45 +00008313 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765) {
8314 if (tp->pci_chip_rev_id == CHIPREV_ID_57765_A0) {
8315 u32 grc_mode = tr32(GRC_MODE);
Matt Carlsoncea46462010-04-12 06:58:24 +00008316
Matt Carlson5093eed2010-11-24 08:31:45 +00008317 /* Access the lower 1K of PL PCIE block registers. */
8318 val = grc_mode & ~GRC_MODE_PCIE_PORT_MASK;
8319 tw32(GRC_MODE, val | GRC_MODE_PCIE_PL_SEL);
Matt Carlsoncea46462010-04-12 06:58:24 +00008320
Matt Carlson5093eed2010-11-24 08:31:45 +00008321 val = tr32(TG3_PCIE_TLDLPL_PORT +
8322 TG3_PCIE_PL_LO_PHYCTL5);
8323 tw32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL5,
8324 val | TG3_PCIE_PL_LO_PHYCTL5_DIS_L2CLKREQ);
Matt Carlsoncea46462010-04-12 06:58:24 +00008325
Matt Carlson5093eed2010-11-24 08:31:45 +00008326 tw32(GRC_MODE, grc_mode);
8327 }
Matt Carlsona977dbe2010-04-12 06:58:26 +00008328
Matt Carlson1ff30a52011-05-19 12:12:46 +00008329 if (GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_57765_AX) {
8330 u32 grc_mode = tr32(GRC_MODE);
8331
8332 /* Access the lower 1K of DL PCIE block registers. */
8333 val = grc_mode & ~GRC_MODE_PCIE_PORT_MASK;
8334 tw32(GRC_MODE, val | GRC_MODE_PCIE_DL_SEL);
8335
8336 val = tr32(TG3_PCIE_TLDLPL_PORT +
8337 TG3_PCIE_DL_LO_FTSMAX);
8338 val &= ~TG3_PCIE_DL_LO_FTSMAX_MSK;
8339 tw32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_DL_LO_FTSMAX,
8340 val | TG3_PCIE_DL_LO_FTSMAX_VAL);
8341
8342 tw32(GRC_MODE, grc_mode);
8343 }
8344
Matt Carlsona977dbe2010-04-12 06:58:26 +00008345 val = tr32(TG3_CPMU_LSPD_10MB_CLK);
8346 val &= ~CPMU_LSPD_10MB_MACCLK_MASK;
8347 val |= CPMU_LSPD_10MB_MACCLK_6_25;
8348 tw32(TG3_CPMU_LSPD_10MB_CLK, val);
Matt Carlsoncea46462010-04-12 06:58:24 +00008349 }
8350
Linus Torvalds1da177e2005-04-16 15:20:36 -07008351 /* This works around an issue with Athlon chipsets on
8352 * B3 tigon3 silicon. This bit has no effect on any
8353 * other revision. But do not set this on PCI Express
Matt Carlson795d01c2007-10-07 23:28:17 -07008354 * chips and don't even touch the clocks if the CPMU is present.
Linus Torvalds1da177e2005-04-16 15:20:36 -07008355 */
Joe Perches63c3a662011-04-26 08:12:10 +00008356 if (!tg3_flag(tp, CPMU_PRESENT)) {
8357 if (!tg3_flag(tp, PCI_EXPRESS))
Matt Carlson795d01c2007-10-07 23:28:17 -07008358 tp->pci_clock_ctrl |= CLOCK_CTRL_DELAY_PCI_GRANT;
8359 tw32_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl);
8360 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008361
8362 if (tp->pci_chip_rev_id == CHIPREV_ID_5704_A0 &&
Joe Perches63c3a662011-04-26 08:12:10 +00008363 tg3_flag(tp, PCIX_MODE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008364 val = tr32(TG3PCI_PCISTATE);
8365 val |= PCISTATE_RETRY_SAME_DMA;
8366 tw32(TG3PCI_PCISTATE, val);
8367 }
8368
Joe Perches63c3a662011-04-26 08:12:10 +00008369 if (tg3_flag(tp, ENABLE_APE)) {
Matt Carlson0d3031d2007-10-10 18:02:43 -07008370 /* Allow reads and writes to the
8371 * APE register and memory space.
8372 */
8373 val = tr32(TG3PCI_PCISTATE);
8374 val |= PCISTATE_ALLOW_APE_CTLSPC_WR |
Matt Carlsonf92d9dc12010-06-05 17:24:30 +00008375 PCISTATE_ALLOW_APE_SHMEM_WR |
8376 PCISTATE_ALLOW_APE_PSPACE_WR;
Matt Carlson0d3031d2007-10-10 18:02:43 -07008377 tw32(TG3PCI_PCISTATE, val);
8378 }
8379
Linus Torvalds1da177e2005-04-16 15:20:36 -07008380 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5704_BX) {
8381 /* Enable some hw fixes. */
8382 val = tr32(TG3PCI_MSI_DATA);
8383 val |= (1 << 26) | (1 << 28) | (1 << 29);
8384 tw32(TG3PCI_MSI_DATA, val);
8385 }
8386
8387 /* Descriptor ring init may make accesses to the
8388 * NIC SRAM area to setup the TX descriptors, so we
8389 * can only do this after the hardware has been
8390 * successfully reset.
8391 */
Michael Chan32d8c572006-07-25 16:38:29 -07008392 err = tg3_init_rings(tp);
8393 if (err)
8394 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008395
Joe Perches63c3a662011-04-26 08:12:10 +00008396 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlsoncbf9ca62009-11-13 13:03:40 +00008397 val = tr32(TG3PCI_DMA_RW_CTRL) &
8398 ~DMA_RWCTRL_DIS_CACHE_ALIGNMENT;
Matt Carlson1a319022010-04-12 06:58:25 +00008399 if (tp->pci_chip_rev_id == CHIPREV_ID_57765_A0)
8400 val &= ~DMA_RWCTRL_CRDRDR_RDMA_MRRS_MSK;
Matt Carlson0aebff42011-04-25 12:42:45 +00008401 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_57765 &&
8402 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717)
8403 val |= DMA_RWCTRL_TAGGED_STAT_WA;
Matt Carlsoncbf9ca62009-11-13 13:03:40 +00008404 tw32(TG3PCI_DMA_RW_CTRL, val | tp->dma_rwctrl);
8405 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5784 &&
8406 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5761) {
Matt Carlsond30cdd22007-10-07 23:28:35 -07008407 /* This value is determined during the probe time DMA
8408 * engine test, tg3_test_dma.
8409 */
8410 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
8411 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008412
8413 tp->grc_mode &= ~(GRC_MODE_HOST_SENDBDS |
8414 GRC_MODE_4X_NIC_SEND_RINGS |
8415 GRC_MODE_NO_TX_PHDR_CSUM |
8416 GRC_MODE_NO_RX_PHDR_CSUM);
8417 tp->grc_mode |= GRC_MODE_HOST_SENDBDS;
Michael Chand2d746f2006-04-06 21:45:39 -07008418
8419 /* Pseudo-header checksum is done by hardware logic and not
8420 * the offload processers, so make the chip do the pseudo-
8421 * header checksums on receive. For transmit it is more
8422 * convenient to do the pseudo-header checksum in software
8423 * as Linux does that on transmit for us in all cases.
8424 */
8425 tp->grc_mode |= GRC_MODE_NO_TX_PHDR_CSUM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008426
8427 tw32(GRC_MODE,
8428 tp->grc_mode |
8429 (GRC_MODE_IRQ_ON_MAC_ATTN | GRC_MODE_HOST_STACKUP));
8430
8431 /* Setup the timer prescalar register. Clock is always 66Mhz. */
8432 val = tr32(GRC_MISC_CFG);
8433 val &= ~0xff;
8434 val |= (65 << GRC_MISC_CFG_PRESCALAR_SHIFT);
8435 tw32(GRC_MISC_CFG, val);
8436
8437 /* Initialize MBUF/DESC pool. */
Joe Perches63c3a662011-04-26 08:12:10 +00008438 if (tg3_flag(tp, 5750_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008439 /* Do nothing. */
8440 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5705) {
8441 tw32(BUFMGR_MB_POOL_ADDR, NIC_SRAM_MBUF_POOL_BASE);
8442 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704)
8443 tw32(BUFMGR_MB_POOL_SIZE, NIC_SRAM_MBUF_POOL_SIZE64);
8444 else
8445 tw32(BUFMGR_MB_POOL_SIZE, NIC_SRAM_MBUF_POOL_SIZE96);
8446 tw32(BUFMGR_DMA_DESC_POOL_ADDR, NIC_SRAM_DMA_DESC_POOL_BASE);
8447 tw32(BUFMGR_DMA_DESC_POOL_SIZE, NIC_SRAM_DMA_DESC_POOL_SIZE);
Joe Perches63c3a662011-04-26 08:12:10 +00008448 } else if (tg3_flag(tp, TSO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008449 int fw_len;
8450
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08008451 fw_len = tp->fw_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008452 fw_len = (fw_len + (0x80 - 1)) & ~(0x80 - 1);
8453 tw32(BUFMGR_MB_POOL_ADDR,
8454 NIC_SRAM_MBUF_POOL_BASE5705 + fw_len);
8455 tw32(BUFMGR_MB_POOL_SIZE,
8456 NIC_SRAM_MBUF_POOL_SIZE5705 - fw_len - 0xa00);
8457 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008458
Michael Chan0f893dc2005-07-25 12:30:38 -07008459 if (tp->dev->mtu <= ETH_DATA_LEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008460 tw32(BUFMGR_MB_RDMA_LOW_WATER,
8461 tp->bufmgr_config.mbuf_read_dma_low_water);
8462 tw32(BUFMGR_MB_MACRX_LOW_WATER,
8463 tp->bufmgr_config.mbuf_mac_rx_low_water);
8464 tw32(BUFMGR_MB_HIGH_WATER,
8465 tp->bufmgr_config.mbuf_high_water);
8466 } else {
8467 tw32(BUFMGR_MB_RDMA_LOW_WATER,
8468 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo);
8469 tw32(BUFMGR_MB_MACRX_LOW_WATER,
8470 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo);
8471 tw32(BUFMGR_MB_HIGH_WATER,
8472 tp->bufmgr_config.mbuf_high_water_jumbo);
8473 }
8474 tw32(BUFMGR_DMA_LOW_WATER,
8475 tp->bufmgr_config.dma_low_water);
8476 tw32(BUFMGR_DMA_HIGH_WATER,
8477 tp->bufmgr_config.dma_high_water);
8478
Matt Carlsond309a462010-09-30 10:34:31 +00008479 val = BUFMGR_MODE_ENABLE | BUFMGR_MODE_ATTN_ENABLE;
8480 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
8481 val |= BUFMGR_MODE_NO_TX_UNDERRUN;
Matt Carlson4d958472011-04-20 07:57:35 +00008482 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
8483 tp->pci_chip_rev_id == CHIPREV_ID_5719_A0 ||
8484 tp->pci_chip_rev_id == CHIPREV_ID_5720_A0)
8485 val |= BUFMGR_MODE_MBLOW_ATTN_ENAB;
Matt Carlsond309a462010-09-30 10:34:31 +00008486 tw32(BUFMGR_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008487 for (i = 0; i < 2000; i++) {
8488 if (tr32(BUFMGR_MODE) & BUFMGR_MODE_ENABLE)
8489 break;
8490 udelay(10);
8491 }
8492 if (i >= 2000) {
Joe Perches05dbe002010-02-17 19:44:19 +00008493 netdev_err(tp->dev, "%s cannot enable BUFMGR\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008494 return -ENODEV;
8495 }
8496
Matt Carlsoneb07a942011-04-20 07:57:36 +00008497 if (tp->pci_chip_rev_id == CHIPREV_ID_5906_A1)
8498 tw32(ISO_PKT_TX, (tr32(ISO_PKT_TX) & ~0x3) | 0x2);
Michael Chanb5d37722006-09-27 16:06:21 -07008499
Matt Carlsoneb07a942011-04-20 07:57:36 +00008500 tg3_setup_rxbd_thresholds(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008501
8502 /* Initialize TG3_BDINFO's at:
8503 * RCVDBDI_STD_BD: standard eth size rx ring
8504 * RCVDBDI_JUMBO_BD: jumbo frame rx ring
8505 * RCVDBDI_MINI_BD: small frame rx ring (??? does not work)
8506 *
8507 * like so:
8508 * TG3_BDINFO_HOST_ADDR: high/low parts of DMA address of ring
8509 * TG3_BDINFO_MAXLEN_FLAGS: (rx max buffer size << 16) |
8510 * ring attribute flags
8511 * TG3_BDINFO_NIC_ADDR: location of descriptors in nic SRAM
8512 *
8513 * Standard receive ring @ NIC_SRAM_RX_BUFFER_DESC, 512 entries.
8514 * Jumbo receive ring @ NIC_SRAM_RX_JUMBO_BUFFER_DESC, 256 entries.
8515 *
8516 * The size of each ring is fixed in the firmware, but the location is
8517 * configurable.
8518 */
8519 tw32(RCVDBDI_STD_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH,
Matt Carlson21f581a2009-08-28 14:00:25 +00008520 ((u64) tpr->rx_std_mapping >> 32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07008521 tw32(RCVDBDI_STD_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW,
Matt Carlson21f581a2009-08-28 14:00:25 +00008522 ((u64) tpr->rx_std_mapping & 0xffffffff));
Joe Perches63c3a662011-04-26 08:12:10 +00008523 if (!tg3_flag(tp, 5717_PLUS))
Matt Carlson87668d32009-11-13 13:03:34 +00008524 tw32(RCVDBDI_STD_BD + TG3_BDINFO_NIC_ADDR,
8525 NIC_SRAM_RX_BUFFER_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008526
Matt Carlsonfdb72b32009-08-28 13:57:12 +00008527 /* Disable the mini ring */
Joe Perches63c3a662011-04-26 08:12:10 +00008528 if (!tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008529 tw32(RCVDBDI_MINI_BD + TG3_BDINFO_MAXLEN_FLAGS,
8530 BDINFO_FLAGS_DISABLED);
8531
Matt Carlsonfdb72b32009-08-28 13:57:12 +00008532 /* Program the jumbo buffer descriptor ring control
8533 * blocks on those devices that have them.
8534 */
Matt Carlsona0512942011-07-27 14:20:54 +00008535 if (tp->pci_chip_rev_id == CHIPREV_ID_5719_A0 ||
Joe Perches63c3a662011-04-26 08:12:10 +00008536 (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008537
Joe Perches63c3a662011-04-26 08:12:10 +00008538 if (tg3_flag(tp, JUMBO_RING_ENABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008539 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH,
Matt Carlson21f581a2009-08-28 14:00:25 +00008540 ((u64) tpr->rx_jmb_mapping >> 32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07008541 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW,
Matt Carlson21f581a2009-08-28 14:00:25 +00008542 ((u64) tpr->rx_jmb_mapping & 0xffffffff));
Matt Carlsonde9f5232011-04-05 14:22:43 +00008543 val = TG3_RX_JMB_RING_SIZE(tp) <<
8544 BDINFO_FLAGS_MAXLEN_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008545 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_MAXLEN_FLAGS,
Matt Carlsonde9f5232011-04-05 14:22:43 +00008546 val | BDINFO_FLAGS_USE_EXT_RECV);
Joe Perches63c3a662011-04-26 08:12:10 +00008547 if (!tg3_flag(tp, USE_JUMBO_BDFLAG) ||
Matt Carlsona50d0792010-06-05 17:24:37 +00008548 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Matt Carlson87668d32009-11-13 13:03:34 +00008549 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_NIC_ADDR,
8550 NIC_SRAM_RX_JUMBO_BUFFER_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008551 } else {
8552 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_MAXLEN_FLAGS,
8553 BDINFO_FLAGS_DISABLED);
8554 }
8555
Joe Perches63c3a662011-04-26 08:12:10 +00008556 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlsonfa6b2aa2011-11-21 15:01:19 +00008557 val = TG3_RX_STD_RING_SIZE(tp);
Matt Carlson7cb32cf2010-09-30 10:34:36 +00008558 val <<= BDINFO_FLAGS_MAXLEN_SHIFT;
8559 val |= (TG3_RX_STD_DMA_SZ << 2);
8560 } else
Matt Carlson04380d42010-04-12 06:58:29 +00008561 val = TG3_RX_STD_DMA_SZ << BDINFO_FLAGS_MAXLEN_SHIFT;
Matt Carlsonfdb72b32009-08-28 13:57:12 +00008562 } else
Matt Carlsonde9f5232011-04-05 14:22:43 +00008563 val = TG3_RX_STD_MAX_SIZE_5700 << BDINFO_FLAGS_MAXLEN_SHIFT;
Matt Carlsonfdb72b32009-08-28 13:57:12 +00008564
8565 tw32(RCVDBDI_STD_BD + TG3_BDINFO_MAXLEN_FLAGS, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008566
Matt Carlson411da642009-11-13 13:03:46 +00008567 tpr->rx_std_prod_idx = tp->rx_pending;
Matt Carlson66711e62009-11-13 13:03:49 +00008568 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG, tpr->rx_std_prod_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008569
Joe Perches63c3a662011-04-26 08:12:10 +00008570 tpr->rx_jmb_prod_idx =
8571 tg3_flag(tp, JUMBO_RING_ENABLE) ? tp->rx_jumbo_pending : 0;
Matt Carlson66711e62009-11-13 13:03:49 +00008572 tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG, tpr->rx_jmb_prod_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008573
Matt Carlson2d31eca2009-09-01 12:53:31 +00008574 tg3_rings_reset(tp);
8575
Linus Torvalds1da177e2005-04-16 15:20:36 -07008576 /* Initialize MAC address and backoff seed. */
Michael Chan986e0ae2007-05-05 12:10:20 -07008577 __tg3_set_mac_addr(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008578
8579 /* MTU + ethernet header + FCS + optional VLAN tag */
Matt Carlsonf7b493e2009-02-25 14:21:52 +00008580 tw32(MAC_RX_MTU_SIZE,
8581 tp->dev->mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008582
8583 /* The slot time is changed by tg3_setup_phy if we
8584 * run at gigabit with half duplex.
8585 */
Matt Carlsonf2096f92011-04-05 14:22:48 +00008586 val = (2 << TX_LENGTHS_IPG_CRS_SHIFT) |
8587 (6 << TX_LENGTHS_IPG_SHIFT) |
8588 (32 << TX_LENGTHS_SLOT_TIME_SHIFT);
8589
8590 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
8591 val |= tr32(MAC_TX_LENGTHS) &
8592 (TX_LENGTHS_JMB_FRM_LEN_MSK |
8593 TX_LENGTHS_CNT_DWN_VAL_MSK);
8594
8595 tw32(MAC_TX_LENGTHS, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008596
8597 /* Receive rules. */
8598 tw32(MAC_RCV_RULE_CFG, RCV_RULE_CFG_DEFAULT_CLASS);
8599 tw32(RCVLPC_CONFIG, 0x0181);
8600
8601 /* Calculate RDMAC_MODE setting early, we need it to determine
8602 * the RCVLPC_STATE_ENABLE mask.
8603 */
8604 rdmac_mode = (RDMAC_MODE_ENABLE | RDMAC_MODE_TGTABORT_ENAB |
8605 RDMAC_MODE_MSTABORT_ENAB | RDMAC_MODE_PARITYERR_ENAB |
8606 RDMAC_MODE_ADDROFLOW_ENAB | RDMAC_MODE_FIFOOFLOW_ENAB |
8607 RDMAC_MODE_FIFOURUN_ENAB | RDMAC_MODE_FIFOOREAD_ENAB |
8608 RDMAC_MODE_LNGREAD_ENAB);
Michael Chan85e94ce2005-04-21 17:05:28 -07008609
Matt Carlsondeabaac2010-11-24 08:31:50 +00008610 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717)
Matt Carlson0339e4e2010-02-12 14:47:09 +00008611 rdmac_mode |= RDMAC_MODE_MULT_DMA_RD_DIS;
8612
Matt Carlson57e69832008-05-25 23:48:31 -07008613 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson321d32a2008-11-21 17:22:19 -08008614 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
8615 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
Matt Carlsond30cdd22007-10-07 23:28:35 -07008616 rdmac_mode |= RDMAC_MODE_BD_SBD_CRPT_ENAB |
8617 RDMAC_MODE_MBUF_RBD_CRPT_ENAB |
8618 RDMAC_MODE_MBUF_SBD_CRPT_ENAB;
8619
Matt Carlsonc5908932011-03-09 16:58:25 +00008620 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
8621 tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) {
Joe Perches63c3a662011-04-26 08:12:10 +00008622 if (tg3_flag(tp, TSO_CAPABLE) &&
Matt Carlsonc13e3712007-05-05 11:50:04 -07008623 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008624 rdmac_mode |= RDMAC_MODE_FIFO_SIZE_128;
8625 } else if (!(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH) &&
Joe Perches63c3a662011-04-26 08:12:10 +00008626 !tg3_flag(tp, IS_5788)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008627 rdmac_mode |= RDMAC_MODE_FIFO_LONG_BURST;
8628 }
8629 }
8630
Joe Perches63c3a662011-04-26 08:12:10 +00008631 if (tg3_flag(tp, PCI_EXPRESS))
Michael Chan85e94ce2005-04-21 17:05:28 -07008632 rdmac_mode |= RDMAC_MODE_FIFO_LONG_BURST;
8633
Joe Perches63c3a662011-04-26 08:12:10 +00008634 if (tg3_flag(tp, HW_TSO_1) ||
8635 tg3_flag(tp, HW_TSO_2) ||
8636 tg3_flag(tp, HW_TSO_3))
Matt Carlson027455a2008-12-21 20:19:30 -08008637 rdmac_mode |= RDMAC_MODE_IPV4_LSO_EN;
8638
Matt Carlson108a6c12011-05-19 12:12:47 +00008639 if (tg3_flag(tp, 57765_PLUS) ||
Matt Carlsone849cdc2009-11-13 13:03:38 +00008640 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Matt Carlson027455a2008-12-21 20:19:30 -08008641 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
8642 rdmac_mode |= RDMAC_MODE_IPV6_LSO_EN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008643
Matt Carlsonf2096f92011-04-05 14:22:48 +00008644 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
8645 rdmac_mode |= tr32(RDMAC_MODE) & RDMAC_MODE_H2BNC_VLAN_DET;
8646
Matt Carlson41a8a7e2010-09-15 08:59:53 +00008647 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
8648 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
8649 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
8650 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
Joe Perches63c3a662011-04-26 08:12:10 +00008651 tg3_flag(tp, 57765_PLUS)) {
Matt Carlson41a8a7e2010-09-15 08:59:53 +00008652 val = tr32(TG3_RDMA_RSRVCTRL_REG);
Matt Carlsond78b59f2011-04-05 14:22:46 +00008653 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
8654 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
Matt Carlsonb4495ed2011-01-25 15:58:47 +00008655 val &= ~(TG3_RDMA_RSRVCTRL_TXMRGN_MASK |
8656 TG3_RDMA_RSRVCTRL_FIFO_LWM_MASK |
8657 TG3_RDMA_RSRVCTRL_FIFO_HWM_MASK);
8658 val |= TG3_RDMA_RSRVCTRL_TXMRGN_320B |
8659 TG3_RDMA_RSRVCTRL_FIFO_LWM_1_5K |
8660 TG3_RDMA_RSRVCTRL_FIFO_HWM_1_5K;
Matt Carlsonb75cc0e2010-11-24 08:31:46 +00008661 }
Matt Carlson41a8a7e2010-09-15 08:59:53 +00008662 tw32(TG3_RDMA_RSRVCTRL_REG,
8663 val | TG3_RDMA_RSRVCTRL_FIFO_OFLW_FIX);
8664 }
8665
Matt Carlsond78b59f2011-04-05 14:22:46 +00008666 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
8667 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
Matt Carlsond309a462010-09-30 10:34:31 +00008668 val = tr32(TG3_LSO_RD_DMA_CRPTEN_CTRL);
8669 tw32(TG3_LSO_RD_DMA_CRPTEN_CTRL, val |
8670 TG3_LSO_RD_DMA_CRPTEN_CTRL_BLEN_BD_4K |
8671 TG3_LSO_RD_DMA_CRPTEN_CTRL_BLEN_LSO_4K);
8672 }
8673
Linus Torvalds1da177e2005-04-16 15:20:36 -07008674 /* Receive/send statistics. */
Joe Perches63c3a662011-04-26 08:12:10 +00008675 if (tg3_flag(tp, 5750_PLUS)) {
Michael Chan16613942006-06-29 20:15:13 -07008676 val = tr32(RCVLPC_STATS_ENABLE);
8677 val &= ~RCVLPC_STATSENAB_DACK_FIX;
8678 tw32(RCVLPC_STATS_ENABLE, val);
8679 } else if ((rdmac_mode & RDMAC_MODE_FIFO_SIZE_128) &&
Joe Perches63c3a662011-04-26 08:12:10 +00008680 tg3_flag(tp, TSO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008681 val = tr32(RCVLPC_STATS_ENABLE);
8682 val &= ~RCVLPC_STATSENAB_LNGBRST_RFIX;
8683 tw32(RCVLPC_STATS_ENABLE, val);
8684 } else {
8685 tw32(RCVLPC_STATS_ENABLE, 0xffffff);
8686 }
8687 tw32(RCVLPC_STATSCTRL, RCVLPC_STATSCTRL_ENABLE);
8688 tw32(SNDDATAI_STATSENAB, 0xffffff);
8689 tw32(SNDDATAI_STATSCTRL,
8690 (SNDDATAI_SCTRL_ENABLE |
8691 SNDDATAI_SCTRL_FASTUPD));
8692
8693 /* Setup host coalescing engine. */
8694 tw32(HOSTCC_MODE, 0);
8695 for (i = 0; i < 2000; i++) {
8696 if (!(tr32(HOSTCC_MODE) & HOSTCC_MODE_ENABLE))
8697 break;
8698 udelay(10);
8699 }
8700
Michael Chand244c892005-07-05 14:42:33 -07008701 __tg3_set_coalesce(tp, &tp->coal);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008702
Joe Perches63c3a662011-04-26 08:12:10 +00008703 if (!tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008704 /* Status/statistics block address. See tg3_timer,
8705 * the tg3_periodic_fetch_stats call there, and
8706 * tg3_get_stats to see how this works for 5705/5750 chips.
8707 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008708 tw32(HOSTCC_STATS_BLK_HOST_ADDR + TG3_64BIT_REG_HIGH,
8709 ((u64) tp->stats_mapping >> 32));
8710 tw32(HOSTCC_STATS_BLK_HOST_ADDR + TG3_64BIT_REG_LOW,
8711 ((u64) tp->stats_mapping & 0xffffffff));
8712 tw32(HOSTCC_STATS_BLK_NIC_ADDR, NIC_SRAM_STATS_BLK);
Matt Carlson2d31eca2009-09-01 12:53:31 +00008713
Linus Torvalds1da177e2005-04-16 15:20:36 -07008714 tw32(HOSTCC_STATUS_BLK_NIC_ADDR, NIC_SRAM_STATUS_BLK);
Matt Carlson2d31eca2009-09-01 12:53:31 +00008715
8716 /* Clear statistics and status block memory areas */
8717 for (i = NIC_SRAM_STATS_BLK;
8718 i < NIC_SRAM_STATUS_BLK + TG3_HW_STATUS_SIZE;
8719 i += sizeof(u32)) {
8720 tg3_write_mem(tp, i, 0);
8721 udelay(40);
8722 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008723 }
8724
8725 tw32(HOSTCC_MODE, HOSTCC_MODE_ENABLE | tp->coalesce_mode);
8726
8727 tw32(RCVCC_MODE, RCVCC_MODE_ENABLE | RCVCC_MODE_ATTN_ENABLE);
8728 tw32(RCVLPC_MODE, RCVLPC_MODE_ENABLE);
Joe Perches63c3a662011-04-26 08:12:10 +00008729 if (!tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008730 tw32(RCVLSC_MODE, RCVLSC_MODE_ENABLE | RCVLSC_MODE_ATTN_ENABLE);
8731
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008732 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES) {
8733 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chanc94e3942005-09-27 12:12:42 -07008734 /* reset to prevent losing 1st rx packet intermittently */
8735 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
8736 udelay(10);
8737 }
8738
Matt Carlson3bda1252008-08-15 14:08:22 -07008739 tp->mac_mode |= MAC_MODE_TXSTAT_ENABLE | MAC_MODE_RXSTAT_ENABLE |
Matt Carlson9e975cc2011-07-20 10:20:50 +00008740 MAC_MODE_TDE_ENABLE | MAC_MODE_RDE_ENABLE |
8741 MAC_MODE_FHDE_ENABLE;
8742 if (tg3_flag(tp, ENABLE_APE))
8743 tp->mac_mode |= MAC_MODE_APE_TX_EN | MAC_MODE_APE_RX_EN;
Joe Perches63c3a662011-04-26 08:12:10 +00008744 if (!tg3_flag(tp, 5705_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008745 !(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07008746 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700)
8747 tp->mac_mode |= MAC_MODE_LINK_POLARITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008748 tw32_f(MAC_MODE, tp->mac_mode | MAC_MODE_RXSTAT_CLEAR | MAC_MODE_TXSTAT_CLEAR);
8749 udelay(40);
8750
Michael Chan314fba32005-04-21 17:07:04 -07008751 /* tp->grc_local_ctrl is partially set up during tg3_get_invariants().
Joe Perches63c3a662011-04-26 08:12:10 +00008752 * If TG3_FLAG_IS_NIC is zero, we should read the
Michael Chan314fba32005-04-21 17:07:04 -07008753 * register to preserve the GPIO settings for LOMs. The GPIOs,
8754 * whether used as inputs or outputs, are set by boot code after
8755 * reset.
8756 */
Joe Perches63c3a662011-04-26 08:12:10 +00008757 if (!tg3_flag(tp, IS_NIC)) {
Michael Chan314fba32005-04-21 17:07:04 -07008758 u32 gpio_mask;
8759
Michael Chan9d26e212006-12-07 00:21:14 -08008760 gpio_mask = GRC_LCLCTRL_GPIO_OE0 | GRC_LCLCTRL_GPIO_OE1 |
8761 GRC_LCLCTRL_GPIO_OE2 | GRC_LCLCTRL_GPIO_OUTPUT0 |
8762 GRC_LCLCTRL_GPIO_OUTPUT1 | GRC_LCLCTRL_GPIO_OUTPUT2;
Michael Chan3e7d83b2005-04-21 17:10:36 -07008763
8764 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752)
8765 gpio_mask |= GRC_LCLCTRL_GPIO_OE3 |
8766 GRC_LCLCTRL_GPIO_OUTPUT3;
8767
Michael Chanaf36e6b2006-03-23 01:28:06 -08008768 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755)
8769 gpio_mask |= GRC_LCLCTRL_GPIO_UART_SEL;
8770
Gary Zambranoaaf84462007-05-05 11:51:45 -07008771 tp->grc_local_ctrl &= ~gpio_mask;
Michael Chan314fba32005-04-21 17:07:04 -07008772 tp->grc_local_ctrl |= tr32(GRC_LOCAL_CTRL) & gpio_mask;
8773
8774 /* GPIO1 must be driven high for eeprom write protect */
Joe Perches63c3a662011-04-26 08:12:10 +00008775 if (tg3_flag(tp, EEPROM_WRITE_PROT))
Michael Chan9d26e212006-12-07 00:21:14 -08008776 tp->grc_local_ctrl |= (GRC_LCLCTRL_GPIO_OE1 |
8777 GRC_LCLCTRL_GPIO_OUTPUT1);
Michael Chan314fba32005-04-21 17:07:04 -07008778 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008779 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
8780 udelay(100);
8781
Joe Perches63c3a662011-04-26 08:12:10 +00008782 if (tg3_flag(tp, USING_MSIX) && tp->irq_cnt > 1) {
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008783 val = tr32(MSGINT_MODE);
8784 val |= MSGINT_MODE_MULTIVEC_EN | MSGINT_MODE_ENABLE;
Matt Carlson5b39de92011-08-31 11:44:50 +00008785 if (!tg3_flag(tp, 1SHOT_MSI))
8786 val |= MSGINT_MODE_ONE_SHOT_DISABLE;
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008787 tw32(MSGINT_MODE, val);
8788 }
8789
Joe Perches63c3a662011-04-26 08:12:10 +00008790 if (!tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008791 tw32_f(DMAC_MODE, DMAC_MODE_ENABLE);
8792 udelay(40);
8793 }
8794
8795 val = (WDMAC_MODE_ENABLE | WDMAC_MODE_TGTABORT_ENAB |
8796 WDMAC_MODE_MSTABORT_ENAB | WDMAC_MODE_PARITYERR_ENAB |
8797 WDMAC_MODE_ADDROFLOW_ENAB | WDMAC_MODE_FIFOOFLOW_ENAB |
8798 WDMAC_MODE_FIFOURUN_ENAB | WDMAC_MODE_FIFOOREAD_ENAB |
8799 WDMAC_MODE_LNGREAD_ENAB);
8800
Matt Carlsonc5908932011-03-09 16:58:25 +00008801 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
8802 tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) {
Joe Perches63c3a662011-04-26 08:12:10 +00008803 if (tg3_flag(tp, TSO_CAPABLE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07008804 (tp->pci_chip_rev_id == CHIPREV_ID_5705_A1 ||
8805 tp->pci_chip_rev_id == CHIPREV_ID_5705_A2)) {
8806 /* nothing */
8807 } else if (!(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH) &&
Joe Perches63c3a662011-04-26 08:12:10 +00008808 !tg3_flag(tp, IS_5788)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008809 val |= WDMAC_MODE_RX_ACCEL;
8810 }
8811 }
8812
Michael Chand9ab5ad2006-03-20 22:27:35 -08008813 /* Enable host coalescing bug fix */
Joe Perches63c3a662011-04-26 08:12:10 +00008814 if (tg3_flag(tp, 5755_PLUS))
Matt Carlsonf51f3562008-05-25 23:45:08 -07008815 val |= WDMAC_MODE_STATUS_TAG_FIX;
Michael Chand9ab5ad2006-03-20 22:27:35 -08008816
Matt Carlson788a0352009-11-02 14:26:03 +00008817 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
8818 val |= WDMAC_MODE_BURST_ALL_DATA;
8819
Linus Torvalds1da177e2005-04-16 15:20:36 -07008820 tw32_f(WDMAC_MODE, val);
8821 udelay(40);
8822
Joe Perches63c3a662011-04-26 08:12:10 +00008823 if (tg3_flag(tp, PCIX_MODE)) {
Matt Carlson9974a352007-10-07 23:27:28 -07008824 u16 pcix_cmd;
8825
8826 pci_read_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
8827 &pcix_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008828 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703) {
Matt Carlson9974a352007-10-07 23:27:28 -07008829 pcix_cmd &= ~PCI_X_CMD_MAX_READ;
8830 pcix_cmd |= PCI_X_CMD_READ_2K;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008831 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
Matt Carlson9974a352007-10-07 23:27:28 -07008832 pcix_cmd &= ~(PCI_X_CMD_MAX_SPLIT | PCI_X_CMD_MAX_READ);
8833 pcix_cmd |= PCI_X_CMD_READ_2K;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008834 }
Matt Carlson9974a352007-10-07 23:27:28 -07008835 pci_write_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
8836 pcix_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008837 }
8838
8839 tw32_f(RDMAC_MODE, rdmac_mode);
8840 udelay(40);
8841
8842 tw32(RCVDCC_MODE, RCVDCC_MODE_ENABLE | RCVDCC_MODE_ATTN_ENABLE);
Joe Perches63c3a662011-04-26 08:12:10 +00008843 if (!tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008844 tw32(MBFREE_MODE, MBFREE_MODE_ENABLE);
Matt Carlson9936bcf2007-10-10 18:03:07 -07008845
8846 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
8847 tw32(SNDDATAC_MODE,
8848 SNDDATAC_MODE_ENABLE | SNDDATAC_MODE_CDELAY);
8849 else
8850 tw32(SNDDATAC_MODE, SNDDATAC_MODE_ENABLE);
8851
Linus Torvalds1da177e2005-04-16 15:20:36 -07008852 tw32(SNDBDC_MODE, SNDBDC_MODE_ENABLE | SNDBDC_MODE_ATTN_ENABLE);
8853 tw32(RCVBDI_MODE, RCVBDI_MODE_ENABLE | RCVBDI_MODE_RCB_ATTN_ENAB);
Matt Carlson7cb32cf2010-09-30 10:34:36 +00008854 val = RCVDBDI_MODE_ENABLE | RCVDBDI_MODE_INV_RING_SZ;
Joe Perches63c3a662011-04-26 08:12:10 +00008855 if (tg3_flag(tp, LRG_PROD_RING_CAP))
Matt Carlson7cb32cf2010-09-30 10:34:36 +00008856 val |= RCVDBDI_MODE_LRG_RING_SZ;
8857 tw32(RCVDBDI_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008858 tw32(SNDDATAI_MODE, SNDDATAI_MODE_ENABLE);
Joe Perches63c3a662011-04-26 08:12:10 +00008859 if (tg3_flag(tp, HW_TSO_1) ||
8860 tg3_flag(tp, HW_TSO_2) ||
8861 tg3_flag(tp, HW_TSO_3))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008862 tw32(SNDDATAI_MODE, SNDDATAI_MODE_ENABLE | 0x8);
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008863 val = SNDBDI_MODE_ENABLE | SNDBDI_MODE_ATTN_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00008864 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008865 val |= SNDBDI_MODE_MULTI_TXQ_EN;
8866 tw32(SNDBDI_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008867 tw32(SNDBDS_MODE, SNDBDS_MODE_ENABLE | SNDBDS_MODE_ATTN_ENABLE);
8868
8869 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0) {
8870 err = tg3_load_5701_a0_firmware_fix(tp);
8871 if (err)
8872 return err;
8873 }
8874
Joe Perches63c3a662011-04-26 08:12:10 +00008875 if (tg3_flag(tp, TSO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008876 err = tg3_load_tso_firmware(tp);
8877 if (err)
8878 return err;
8879 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008880
8881 tp->tx_mode = TX_MODE_ENABLE;
Matt Carlsonf2096f92011-04-05 14:22:48 +00008882
Joe Perches63c3a662011-04-26 08:12:10 +00008883 if (tg3_flag(tp, 5755_PLUS) ||
Matt Carlsonb1d05212010-06-05 17:24:31 +00008884 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
8885 tp->tx_mode |= TX_MODE_MBUF_LOCKUP_FIX;
Matt Carlsonf2096f92011-04-05 14:22:48 +00008886
8887 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
8888 val = TX_MODE_JMB_FRM_LEN | TX_MODE_CNT_DN_MODE;
8889 tp->tx_mode &= ~val;
8890 tp->tx_mode |= tr32(MAC_TX_MODE) & val;
8891 }
8892
Linus Torvalds1da177e2005-04-16 15:20:36 -07008893 tw32_f(MAC_TX_MODE, tp->tx_mode);
8894 udelay(100);
8895
Joe Perches63c3a662011-04-26 08:12:10 +00008896 if (tg3_flag(tp, ENABLE_RSS)) {
Matt Carlson9d53fa12011-07-20 10:20:54 +00008897 int i = 0;
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008898 u32 reg = MAC_RSS_INDIR_TBL_0;
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008899
Matt Carlson9d53fa12011-07-20 10:20:54 +00008900 if (tp->irq_cnt == 2) {
8901 for (i = 0; i < TG3_RSS_INDIR_TBL_SIZE; i += 8) {
8902 tw32(reg, 0x0);
8903 reg += 4;
8904 }
8905 } else {
8906 u32 val;
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008907
Matt Carlson9d53fa12011-07-20 10:20:54 +00008908 while (i < TG3_RSS_INDIR_TBL_SIZE) {
8909 val = i % (tp->irq_cnt - 1);
8910 i++;
8911 for (; i % 8; i++) {
8912 val <<= 4;
8913 val |= (i % (tp->irq_cnt - 1));
8914 }
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008915 tw32(reg, val);
8916 reg += 4;
8917 }
8918 }
8919
8920 /* Setup the "secret" hash key. */
8921 tw32(MAC_RSS_HASH_KEY_0, 0x5f865437);
8922 tw32(MAC_RSS_HASH_KEY_1, 0xe4ac62cc);
8923 tw32(MAC_RSS_HASH_KEY_2, 0x50103a45);
8924 tw32(MAC_RSS_HASH_KEY_3, 0x36621985);
8925 tw32(MAC_RSS_HASH_KEY_4, 0xbf14c0e8);
8926 tw32(MAC_RSS_HASH_KEY_5, 0x1bc27a1e);
8927 tw32(MAC_RSS_HASH_KEY_6, 0x84f4b556);
8928 tw32(MAC_RSS_HASH_KEY_7, 0x094ea6fe);
8929 tw32(MAC_RSS_HASH_KEY_8, 0x7dda01e7);
8930 tw32(MAC_RSS_HASH_KEY_9, 0xc04d7481);
8931 }
8932
Linus Torvalds1da177e2005-04-16 15:20:36 -07008933 tp->rx_mode = RX_MODE_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00008934 if (tg3_flag(tp, 5755_PLUS))
Michael Chanaf36e6b2006-03-23 01:28:06 -08008935 tp->rx_mode |= RX_MODE_IPV6_CSUM_ENABLE;
8936
Joe Perches63c3a662011-04-26 08:12:10 +00008937 if (tg3_flag(tp, ENABLE_RSS))
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008938 tp->rx_mode |= RX_MODE_RSS_ENABLE |
8939 RX_MODE_RSS_ITBL_HASH_BITS_7 |
8940 RX_MODE_RSS_IPV6_HASH_EN |
8941 RX_MODE_RSS_TCP_IPV6_HASH_EN |
8942 RX_MODE_RSS_IPV4_HASH_EN |
8943 RX_MODE_RSS_TCP_IPV4_HASH_EN;
8944
Linus Torvalds1da177e2005-04-16 15:20:36 -07008945 tw32_f(MAC_RX_MODE, tp->rx_mode);
8946 udelay(10);
8947
Linus Torvalds1da177e2005-04-16 15:20:36 -07008948 tw32(MAC_LED_CTRL, tp->led_ctrl);
8949
8950 tw32(MAC_MI_STAT, MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008951 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008952 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
8953 udelay(10);
8954 }
8955 tw32_f(MAC_RX_MODE, tp->rx_mode);
8956 udelay(10);
8957
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008958 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008959 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008960 !(tp->phy_flags & TG3_PHYFLG_SERDES_PREEMPHASIS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008961 /* Set drive transmission level to 1.2V */
8962 /* only if the signal pre-emphasis bit is not set */
8963 val = tr32(MAC_SERDES_CFG);
8964 val &= 0xfffff000;
8965 val |= 0x880;
8966 tw32(MAC_SERDES_CFG, val);
8967 }
8968 if (tp->pci_chip_rev_id == CHIPREV_ID_5703_A1)
8969 tw32(MAC_SERDES_CFG, 0x616000);
8970 }
8971
8972 /* Prevent chip from dropping frames when flow control
8973 * is enabled.
8974 */
Matt Carlson666bc832010-01-20 16:58:03 +00008975 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
8976 val = 1;
8977 else
8978 val = 2;
8979 tw32_f(MAC_LOW_WMARK_MAX_RX_FRAME, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008980
8981 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008982 (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008983 /* Use hardware link auto-negotiation */
Joe Perches63c3a662011-04-26 08:12:10 +00008984 tg3_flag_set(tp, HW_AUTONEG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008985 }
8986
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008987 if ((tp->phy_flags & TG3_PHYFLG_MII_SERDES) &&
Matt Carlson6ff6f812011-05-19 12:12:54 +00008988 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
Michael Chand4d2c552006-03-20 17:47:20 -08008989 u32 tmp;
8990
8991 tmp = tr32(SERDES_RX_CTRL);
8992 tw32(SERDES_RX_CTRL, tmp | SERDES_RX_SIG_DETECT);
8993 tp->grc_local_ctrl &= ~GRC_LCLCTRL_USE_EXT_SIG_DETECT;
8994 tp->grc_local_ctrl |= GRC_LCLCTRL_USE_SIG_DETECT;
8995 tw32(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
8996 }
8997
Joe Perches63c3a662011-04-26 08:12:10 +00008998 if (!tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson80096062010-08-02 11:26:06 +00008999 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) {
9000 tp->phy_flags &= ~TG3_PHYFLG_IS_LOW_POWER;
Matt Carlsondd477002008-05-25 23:45:58 -07009001 tp->link_config.speed = tp->link_config.orig_speed;
9002 tp->link_config.duplex = tp->link_config.orig_duplex;
9003 tp->link_config.autoneg = tp->link_config.orig_autoneg;
9004 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009005
Matt Carlsondd477002008-05-25 23:45:58 -07009006 err = tg3_setup_phy(tp, 0);
9007 if (err)
9008 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009009
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009010 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
9011 !(tp->phy_flags & TG3_PHYFLG_IS_FET)) {
Matt Carlsondd477002008-05-25 23:45:58 -07009012 u32 tmp;
9013
9014 /* Clear CRC stats. */
9015 if (!tg3_readphy(tp, MII_TG3_TEST1, &tmp)) {
9016 tg3_writephy(tp, MII_TG3_TEST1,
9017 tmp | MII_TG3_TEST1_CRC_EN);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00009018 tg3_readphy(tp, MII_TG3_RXR_COUNTERS, &tmp);
Matt Carlsondd477002008-05-25 23:45:58 -07009019 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009020 }
9021 }
9022
9023 __tg3_set_rx_mode(tp->dev);
9024
9025 /* Initialize receive rules. */
9026 tw32(MAC_RCV_RULE_0, 0xc2000000 & RCV_RULE_DISABLE_MASK);
9027 tw32(MAC_RCV_VALUE_0, 0xffffffff & RCV_RULE_DISABLE_MASK);
9028 tw32(MAC_RCV_RULE_1, 0x86000004 & RCV_RULE_DISABLE_MASK);
9029 tw32(MAC_RCV_VALUE_1, 0xffffffff & RCV_RULE_DISABLE_MASK);
9030
Joe Perches63c3a662011-04-26 08:12:10 +00009031 if (tg3_flag(tp, 5705_PLUS) && !tg3_flag(tp, 5780_CLASS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009032 limit = 8;
9033 else
9034 limit = 16;
Joe Perches63c3a662011-04-26 08:12:10 +00009035 if (tg3_flag(tp, ENABLE_ASF))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009036 limit -= 4;
9037 switch (limit) {
9038 case 16:
9039 tw32(MAC_RCV_RULE_15, 0); tw32(MAC_RCV_VALUE_15, 0);
9040 case 15:
9041 tw32(MAC_RCV_RULE_14, 0); tw32(MAC_RCV_VALUE_14, 0);
9042 case 14:
9043 tw32(MAC_RCV_RULE_13, 0); tw32(MAC_RCV_VALUE_13, 0);
9044 case 13:
9045 tw32(MAC_RCV_RULE_12, 0); tw32(MAC_RCV_VALUE_12, 0);
9046 case 12:
9047 tw32(MAC_RCV_RULE_11, 0); tw32(MAC_RCV_VALUE_11, 0);
9048 case 11:
9049 tw32(MAC_RCV_RULE_10, 0); tw32(MAC_RCV_VALUE_10, 0);
9050 case 10:
9051 tw32(MAC_RCV_RULE_9, 0); tw32(MAC_RCV_VALUE_9, 0);
9052 case 9:
9053 tw32(MAC_RCV_RULE_8, 0); tw32(MAC_RCV_VALUE_8, 0);
9054 case 8:
9055 tw32(MAC_RCV_RULE_7, 0); tw32(MAC_RCV_VALUE_7, 0);
9056 case 7:
9057 tw32(MAC_RCV_RULE_6, 0); tw32(MAC_RCV_VALUE_6, 0);
9058 case 6:
9059 tw32(MAC_RCV_RULE_5, 0); tw32(MAC_RCV_VALUE_5, 0);
9060 case 5:
9061 tw32(MAC_RCV_RULE_4, 0); tw32(MAC_RCV_VALUE_4, 0);
9062 case 4:
9063 /* tw32(MAC_RCV_RULE_3, 0); tw32(MAC_RCV_VALUE_3, 0); */
9064 case 3:
9065 /* tw32(MAC_RCV_RULE_2, 0); tw32(MAC_RCV_VALUE_2, 0); */
9066 case 2:
9067 case 1:
9068
9069 default:
9070 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07009071 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009072
Joe Perches63c3a662011-04-26 08:12:10 +00009073 if (tg3_flag(tp, ENABLE_APE))
Matt Carlson9ce768e2007-10-11 19:49:11 -07009074 /* Write our heartbeat update interval to APE. */
9075 tg3_ape_write32(tp, TG3_APE_HOST_HEARTBEAT_INT_MS,
9076 APE_HOST_HEARTBEAT_INT_DISABLE);
Matt Carlson0d3031d2007-10-10 18:02:43 -07009077
Linus Torvalds1da177e2005-04-16 15:20:36 -07009078 tg3_write_sig_post_reset(tp, RESET_KIND_INIT);
9079
Linus Torvalds1da177e2005-04-16 15:20:36 -07009080 return 0;
9081}
9082
9083/* Called at device open time to get the chip ready for
9084 * packet processing. Invoked with tp->lock held.
9085 */
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07009086static int tg3_init_hw(struct tg3 *tp, int reset_phy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009087{
Linus Torvalds1da177e2005-04-16 15:20:36 -07009088 tg3_switch_clocks(tp);
9089
9090 tw32(TG3PCI_MEM_WIN_BASE_ADDR, 0);
9091
Matt Carlson2f751b62008-08-04 23:17:34 -07009092 return tg3_reset_hw(tp, reset_phy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009093}
9094
9095#define TG3_STAT_ADD32(PSTAT, REG) \
9096do { u32 __val = tr32(REG); \
9097 (PSTAT)->low += __val; \
9098 if ((PSTAT)->low < __val) \
9099 (PSTAT)->high += 1; \
9100} while (0)
9101
9102static void tg3_periodic_fetch_stats(struct tg3 *tp)
9103{
9104 struct tg3_hw_stats *sp = tp->hw_stats;
9105
9106 if (!netif_carrier_ok(tp->dev))
9107 return;
9108
9109 TG3_STAT_ADD32(&sp->tx_octets, MAC_TX_STATS_OCTETS);
9110 TG3_STAT_ADD32(&sp->tx_collisions, MAC_TX_STATS_COLLISIONS);
9111 TG3_STAT_ADD32(&sp->tx_xon_sent, MAC_TX_STATS_XON_SENT);
9112 TG3_STAT_ADD32(&sp->tx_xoff_sent, MAC_TX_STATS_XOFF_SENT);
9113 TG3_STAT_ADD32(&sp->tx_mac_errors, MAC_TX_STATS_MAC_ERRORS);
9114 TG3_STAT_ADD32(&sp->tx_single_collisions, MAC_TX_STATS_SINGLE_COLLISIONS);
9115 TG3_STAT_ADD32(&sp->tx_mult_collisions, MAC_TX_STATS_MULT_COLLISIONS);
9116 TG3_STAT_ADD32(&sp->tx_deferred, MAC_TX_STATS_DEFERRED);
9117 TG3_STAT_ADD32(&sp->tx_excessive_collisions, MAC_TX_STATS_EXCESSIVE_COL);
9118 TG3_STAT_ADD32(&sp->tx_late_collisions, MAC_TX_STATS_LATE_COL);
9119 TG3_STAT_ADD32(&sp->tx_ucast_packets, MAC_TX_STATS_UCAST);
9120 TG3_STAT_ADD32(&sp->tx_mcast_packets, MAC_TX_STATS_MCAST);
9121 TG3_STAT_ADD32(&sp->tx_bcast_packets, MAC_TX_STATS_BCAST);
9122
9123 TG3_STAT_ADD32(&sp->rx_octets, MAC_RX_STATS_OCTETS);
9124 TG3_STAT_ADD32(&sp->rx_fragments, MAC_RX_STATS_FRAGMENTS);
9125 TG3_STAT_ADD32(&sp->rx_ucast_packets, MAC_RX_STATS_UCAST);
9126 TG3_STAT_ADD32(&sp->rx_mcast_packets, MAC_RX_STATS_MCAST);
9127 TG3_STAT_ADD32(&sp->rx_bcast_packets, MAC_RX_STATS_BCAST);
9128 TG3_STAT_ADD32(&sp->rx_fcs_errors, MAC_RX_STATS_FCS_ERRORS);
9129 TG3_STAT_ADD32(&sp->rx_align_errors, MAC_RX_STATS_ALIGN_ERRORS);
9130 TG3_STAT_ADD32(&sp->rx_xon_pause_rcvd, MAC_RX_STATS_XON_PAUSE_RECVD);
9131 TG3_STAT_ADD32(&sp->rx_xoff_pause_rcvd, MAC_RX_STATS_XOFF_PAUSE_RECVD);
9132 TG3_STAT_ADD32(&sp->rx_mac_ctrl_rcvd, MAC_RX_STATS_MAC_CTRL_RECVD);
9133 TG3_STAT_ADD32(&sp->rx_xoff_entered, MAC_RX_STATS_XOFF_ENTERED);
9134 TG3_STAT_ADD32(&sp->rx_frame_too_long_errors, MAC_RX_STATS_FRAME_TOO_LONG);
9135 TG3_STAT_ADD32(&sp->rx_jabbers, MAC_RX_STATS_JABBERS);
9136 TG3_STAT_ADD32(&sp->rx_undersize_packets, MAC_RX_STATS_UNDERSIZE);
Michael Chan463d3052006-05-22 16:36:27 -07009137
9138 TG3_STAT_ADD32(&sp->rxbds_empty, RCVLPC_NO_RCV_BD_CNT);
Matt Carlson310050f2011-05-19 12:12:55 +00009139 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717 &&
9140 tp->pci_chip_rev_id != CHIPREV_ID_5719_A0 &&
9141 tp->pci_chip_rev_id != CHIPREV_ID_5720_A0) {
Matt Carlson4d958472011-04-20 07:57:35 +00009142 TG3_STAT_ADD32(&sp->rx_discards, RCVLPC_IN_DISCARDS_CNT);
9143 } else {
9144 u32 val = tr32(HOSTCC_FLOW_ATTN);
9145 val = (val & HOSTCC_FLOW_ATTN_MBUF_LWM) ? 1 : 0;
9146 if (val) {
9147 tw32(HOSTCC_FLOW_ATTN, HOSTCC_FLOW_ATTN_MBUF_LWM);
9148 sp->rx_discards.low += val;
9149 if (sp->rx_discards.low < val)
9150 sp->rx_discards.high += 1;
9151 }
9152 sp->mbuf_lwm_thresh_hit = sp->rx_discards;
9153 }
Michael Chan463d3052006-05-22 16:36:27 -07009154 TG3_STAT_ADD32(&sp->rx_errors, RCVLPC_IN_ERRORS_CNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009155}
9156
Matt Carlson0e6cf6a2011-06-13 13:38:55 +00009157static void tg3_chk_missed_msi(struct tg3 *tp)
9158{
9159 u32 i;
9160
9161 for (i = 0; i < tp->irq_cnt; i++) {
9162 struct tg3_napi *tnapi = &tp->napi[i];
9163
9164 if (tg3_has_work(tnapi)) {
9165 if (tnapi->last_rx_cons == tnapi->rx_rcb_ptr &&
9166 tnapi->last_tx_cons == tnapi->tx_cons) {
9167 if (tnapi->chk_msi_cnt < 1) {
9168 tnapi->chk_msi_cnt++;
9169 return;
9170 }
Matt Carlson7f230732011-08-31 11:44:48 +00009171 tg3_msi(0, tnapi);
Matt Carlson0e6cf6a2011-06-13 13:38:55 +00009172 }
9173 }
9174 tnapi->chk_msi_cnt = 0;
9175 tnapi->last_rx_cons = tnapi->rx_rcb_ptr;
9176 tnapi->last_tx_cons = tnapi->tx_cons;
9177 }
9178}
9179
Linus Torvalds1da177e2005-04-16 15:20:36 -07009180static void tg3_timer(unsigned long __opaque)
9181{
9182 struct tg3 *tp = (struct tg3 *) __opaque;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009183
Matt Carlson5b190622011-11-04 09:15:04 +00009184 if (tp->irq_sync || tg3_flag(tp, RESET_TASK_PENDING))
Michael Chanf475f162006-03-27 23:20:14 -08009185 goto restart_timer;
9186
David S. Millerf47c11e2005-06-24 20:18:35 -07009187 spin_lock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009188
Matt Carlson0e6cf6a2011-06-13 13:38:55 +00009189 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
9190 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
9191 tg3_chk_missed_msi(tp);
9192
Joe Perches63c3a662011-04-26 08:12:10 +00009193 if (!tg3_flag(tp, TAGGED_STATUS)) {
David S. Millerfac9b832005-05-18 22:46:34 -07009194 /* All of this garbage is because when using non-tagged
9195 * IRQ status the mailbox/status_block protocol the chip
9196 * uses with the cpu is race prone.
9197 */
Matt Carlson898a56f2009-08-28 14:02:40 +00009198 if (tp->napi[0].hw_status->status & SD_STATUS_UPDATED) {
David S. Millerfac9b832005-05-18 22:46:34 -07009199 tw32(GRC_LOCAL_CTRL,
9200 tp->grc_local_ctrl | GRC_LCLCTRL_SETINT);
9201 } else {
9202 tw32(HOSTCC_MODE, tp->coalesce_mode |
Matt Carlsonfd2ce372009-09-01 12:51:13 +00009203 HOSTCC_MODE_ENABLE | HOSTCC_MODE_NOW);
David S. Millerfac9b832005-05-18 22:46:34 -07009204 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009205
David S. Millerfac9b832005-05-18 22:46:34 -07009206 if (!(tr32(WDMAC_MODE) & WDMAC_MODE_ENABLE)) {
David S. Millerf47c11e2005-06-24 20:18:35 -07009207 spin_unlock(&tp->lock);
Matt Carlsondb219972011-11-04 09:15:03 +00009208 tg3_reset_task_schedule(tp);
Matt Carlson5b190622011-11-04 09:15:04 +00009209 goto restart_timer;
David S. Millerfac9b832005-05-18 22:46:34 -07009210 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009211 }
9212
Linus Torvalds1da177e2005-04-16 15:20:36 -07009213 /* This part only runs once per second. */
9214 if (!--tp->timer_counter) {
Joe Perches63c3a662011-04-26 08:12:10 +00009215 if (tg3_flag(tp, 5705_PLUS))
David S. Millerfac9b832005-05-18 22:46:34 -07009216 tg3_periodic_fetch_stats(tp);
9217
Matt Carlsonb0c59432011-05-19 12:12:48 +00009218 if (tp->setlpicnt && !--tp->setlpicnt)
9219 tg3_phy_eee_enable(tp);
Matt Carlson52b02d02010-10-14 10:37:41 +00009220
Joe Perches63c3a662011-04-26 08:12:10 +00009221 if (tg3_flag(tp, USE_LINKCHG_REG)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009222 u32 mac_stat;
9223 int phy_event;
9224
9225 mac_stat = tr32(MAC_STATUS);
9226
9227 phy_event = 0;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009228 if (tp->phy_flags & TG3_PHYFLG_USE_MI_INTERRUPT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009229 if (mac_stat & MAC_STATUS_MI_INTERRUPT)
9230 phy_event = 1;
9231 } else if (mac_stat & MAC_STATUS_LNKSTATE_CHANGED)
9232 phy_event = 1;
9233
9234 if (phy_event)
9235 tg3_setup_phy(tp, 0);
Joe Perches63c3a662011-04-26 08:12:10 +00009236 } else if (tg3_flag(tp, POLL_SERDES)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009237 u32 mac_stat = tr32(MAC_STATUS);
9238 int need_setup = 0;
9239
9240 if (netif_carrier_ok(tp->dev) &&
9241 (mac_stat & MAC_STATUS_LNKSTATE_CHANGED)) {
9242 need_setup = 1;
9243 }
Matt Carlsonbe98da62010-07-11 09:31:46 +00009244 if (!netif_carrier_ok(tp->dev) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07009245 (mac_stat & (MAC_STATUS_PCS_SYNCED |
9246 MAC_STATUS_SIGNAL_DET))) {
9247 need_setup = 1;
9248 }
9249 if (need_setup) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07009250 if (!tp->serdes_counter) {
9251 tw32_f(MAC_MODE,
9252 (tp->mac_mode &
9253 ~MAC_MODE_PORT_MODE_MASK));
9254 udelay(40);
9255 tw32_f(MAC_MODE, tp->mac_mode);
9256 udelay(40);
9257 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009258 tg3_setup_phy(tp, 0);
9259 }
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009260 } else if ((tp->phy_flags & TG3_PHYFLG_MII_SERDES) &&
Joe Perches63c3a662011-04-26 08:12:10 +00009261 tg3_flag(tp, 5780_CLASS)) {
Michael Chan747e8f82005-07-25 12:33:22 -07009262 tg3_serdes_parallel_detect(tp);
Matt Carlson57d8b882010-06-05 17:24:35 +00009263 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009264
9265 tp->timer_counter = tp->timer_multiplier;
9266 }
9267
Michael Chan130b8e42006-09-27 16:00:40 -07009268 /* Heartbeat is only sent once every 2 seconds.
9269 *
9270 * The heartbeat is to tell the ASF firmware that the host
9271 * driver is still alive. In the event that the OS crashes,
9272 * ASF needs to reset the hardware to free up the FIFO space
9273 * that may be filled with rx packets destined for the host.
9274 * If the FIFO is full, ASF will no longer function properly.
9275 *
9276 * Unintended resets have been reported on real time kernels
9277 * where the timer doesn't run on time. Netpoll will also have
9278 * same problem.
9279 *
9280 * The new FWCMD_NICDRV_ALIVE3 command tells the ASF firmware
9281 * to check the ring condition when the heartbeat is expiring
9282 * before doing the reset. This will prevent most unintended
9283 * resets.
9284 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07009285 if (!--tp->asf_counter) {
Joe Perches63c3a662011-04-26 08:12:10 +00009286 if (tg3_flag(tp, ENABLE_ASF) && !tg3_flag(tp, ENABLE_APE)) {
Matt Carlson7c5026a2008-05-02 16:49:29 -07009287 tg3_wait_for_event_ack(tp);
9288
Michael Chanbbadf502006-04-06 21:46:34 -07009289 tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX,
Michael Chan130b8e42006-09-27 16:00:40 -07009290 FWCMD_NICDRV_ALIVE3);
Michael Chanbbadf502006-04-06 21:46:34 -07009291 tg3_write_mem(tp, NIC_SRAM_FW_CMD_LEN_MBOX, 4);
Matt Carlsonc6cdf432010-04-05 10:19:26 +00009292 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX,
9293 TG3_FW_UPDATE_TIMEOUT_SEC);
Matt Carlson4ba526c2008-08-15 14:10:04 -07009294
9295 tg3_generate_fw_event(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009296 }
9297 tp->asf_counter = tp->asf_multiplier;
9298 }
9299
David S. Millerf47c11e2005-06-24 20:18:35 -07009300 spin_unlock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009301
Michael Chanf475f162006-03-27 23:20:14 -08009302restart_timer:
Linus Torvalds1da177e2005-04-16 15:20:36 -07009303 tp->timer.expires = jiffies + tp->timer_offset;
9304 add_timer(&tp->timer);
9305}
9306
Matt Carlson4f125f42009-09-01 12:55:02 +00009307static int tg3_request_irq(struct tg3 *tp, int irq_num)
Michael Chanfcfa0a32006-03-20 22:28:41 -08009308{
David Howells7d12e782006-10-05 14:55:46 +01009309 irq_handler_t fn;
Michael Chanfcfa0a32006-03-20 22:28:41 -08009310 unsigned long flags;
Matt Carlson4f125f42009-09-01 12:55:02 +00009311 char *name;
9312 struct tg3_napi *tnapi = &tp->napi[irq_num];
9313
9314 if (tp->irq_cnt == 1)
9315 name = tp->dev->name;
9316 else {
9317 name = &tnapi->irq_lbl[0];
9318 snprintf(name, IFNAMSIZ, "%s-%d", tp->dev->name, irq_num);
9319 name[IFNAMSIZ-1] = 0;
9320 }
Michael Chanfcfa0a32006-03-20 22:28:41 -08009321
Joe Perches63c3a662011-04-26 08:12:10 +00009322 if (tg3_flag(tp, USING_MSI) || tg3_flag(tp, USING_MSIX)) {
Michael Chanfcfa0a32006-03-20 22:28:41 -08009323 fn = tg3_msi;
Joe Perches63c3a662011-04-26 08:12:10 +00009324 if (tg3_flag(tp, 1SHOT_MSI))
Michael Chanfcfa0a32006-03-20 22:28:41 -08009325 fn = tg3_msi_1shot;
Javier Martinez Canillasab392d22011-03-28 16:27:31 +00009326 flags = 0;
Michael Chanfcfa0a32006-03-20 22:28:41 -08009327 } else {
9328 fn = tg3_interrupt;
Joe Perches63c3a662011-04-26 08:12:10 +00009329 if (tg3_flag(tp, TAGGED_STATUS))
Michael Chanfcfa0a32006-03-20 22:28:41 -08009330 fn = tg3_interrupt_tagged;
Javier Martinez Canillasab392d22011-03-28 16:27:31 +00009331 flags = IRQF_SHARED;
Michael Chanfcfa0a32006-03-20 22:28:41 -08009332 }
Matt Carlson4f125f42009-09-01 12:55:02 +00009333
9334 return request_irq(tnapi->irq_vec, fn, flags, name, tnapi);
Michael Chanfcfa0a32006-03-20 22:28:41 -08009335}
9336
Michael Chan79381092005-04-21 17:13:59 -07009337static int tg3_test_interrupt(struct tg3 *tp)
9338{
Matt Carlson09943a12009-08-28 14:01:57 +00009339 struct tg3_napi *tnapi = &tp->napi[0];
Michael Chan79381092005-04-21 17:13:59 -07009340 struct net_device *dev = tp->dev;
Michael Chanb16250e2006-09-27 16:10:14 -07009341 int err, i, intr_ok = 0;
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009342 u32 val;
Michael Chan79381092005-04-21 17:13:59 -07009343
Michael Chand4bc3922005-05-29 14:59:20 -07009344 if (!netif_running(dev))
9345 return -ENODEV;
9346
Michael Chan79381092005-04-21 17:13:59 -07009347 tg3_disable_ints(tp);
9348
Matt Carlson4f125f42009-09-01 12:55:02 +00009349 free_irq(tnapi->irq_vec, tnapi);
Michael Chan79381092005-04-21 17:13:59 -07009350
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009351 /*
9352 * Turn off MSI one shot mode. Otherwise this test has no
9353 * observable way to know whether the interrupt was delivered.
9354 */
Matt Carlson3aa1cdf2011-07-20 10:20:55 +00009355 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009356 val = tr32(MSGINT_MODE) | MSGINT_MODE_ONE_SHOT_DISABLE;
9357 tw32(MSGINT_MODE, val);
9358 }
9359
Matt Carlson4f125f42009-09-01 12:55:02 +00009360 err = request_irq(tnapi->irq_vec, tg3_test_isr,
Matt Carlson09943a12009-08-28 14:01:57 +00009361 IRQF_SHARED | IRQF_SAMPLE_RANDOM, dev->name, tnapi);
Michael Chan79381092005-04-21 17:13:59 -07009362 if (err)
9363 return err;
9364
Matt Carlson898a56f2009-08-28 14:02:40 +00009365 tnapi->hw_status->status &= ~SD_STATUS_UPDATED;
Michael Chan79381092005-04-21 17:13:59 -07009366 tg3_enable_ints(tp);
9367
9368 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +00009369 tnapi->coal_now);
Michael Chan79381092005-04-21 17:13:59 -07009370
9371 for (i = 0; i < 5; i++) {
Michael Chanb16250e2006-09-27 16:10:14 -07009372 u32 int_mbox, misc_host_ctrl;
9373
Matt Carlson898a56f2009-08-28 14:02:40 +00009374 int_mbox = tr32_mailbox(tnapi->int_mbox);
Michael Chanb16250e2006-09-27 16:10:14 -07009375 misc_host_ctrl = tr32(TG3PCI_MISC_HOST_CTRL);
9376
9377 if ((int_mbox != 0) ||
9378 (misc_host_ctrl & MISC_HOST_CTRL_MASK_PCI_INT)) {
9379 intr_ok = 1;
Michael Chan79381092005-04-21 17:13:59 -07009380 break;
Michael Chanb16250e2006-09-27 16:10:14 -07009381 }
9382
Matt Carlson3aa1cdf2011-07-20 10:20:55 +00009383 if (tg3_flag(tp, 57765_PLUS) &&
9384 tnapi->hw_status->status_tag != tnapi->last_tag)
9385 tw32_mailbox_f(tnapi->int_mbox, tnapi->last_tag << 24);
9386
Michael Chan79381092005-04-21 17:13:59 -07009387 msleep(10);
9388 }
9389
9390 tg3_disable_ints(tp);
9391
Matt Carlson4f125f42009-09-01 12:55:02 +00009392 free_irq(tnapi->irq_vec, tnapi);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009393
Matt Carlson4f125f42009-09-01 12:55:02 +00009394 err = tg3_request_irq(tp, 0);
Michael Chan79381092005-04-21 17:13:59 -07009395
9396 if (err)
9397 return err;
9398
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009399 if (intr_ok) {
9400 /* Reenable MSI one shot mode. */
Matt Carlson5b39de92011-08-31 11:44:50 +00009401 if (tg3_flag(tp, 57765_PLUS) && tg3_flag(tp, 1SHOT_MSI)) {
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009402 val = tr32(MSGINT_MODE) & ~MSGINT_MODE_ONE_SHOT_DISABLE;
9403 tw32(MSGINT_MODE, val);
9404 }
Michael Chan79381092005-04-21 17:13:59 -07009405 return 0;
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009406 }
Michael Chan79381092005-04-21 17:13:59 -07009407
9408 return -EIO;
9409}
9410
9411/* Returns 0 if MSI test succeeds or MSI test fails and INTx mode is
9412 * successfully restored
9413 */
9414static int tg3_test_msi(struct tg3 *tp)
9415{
Michael Chan79381092005-04-21 17:13:59 -07009416 int err;
9417 u16 pci_cmd;
9418
Joe Perches63c3a662011-04-26 08:12:10 +00009419 if (!tg3_flag(tp, USING_MSI))
Michael Chan79381092005-04-21 17:13:59 -07009420 return 0;
9421
9422 /* Turn off SERR reporting in case MSI terminates with Master
9423 * Abort.
9424 */
9425 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
9426 pci_write_config_word(tp->pdev, PCI_COMMAND,
9427 pci_cmd & ~PCI_COMMAND_SERR);
9428
9429 err = tg3_test_interrupt(tp);
9430
9431 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
9432
9433 if (!err)
9434 return 0;
9435
9436 /* other failures */
9437 if (err != -EIO)
9438 return err;
9439
9440 /* MSI test failed, go back to INTx mode */
Matt Carlson5129c3a2010-04-05 10:19:23 +00009441 netdev_warn(tp->dev, "No interrupt was generated using MSI. Switching "
9442 "to INTx mode. Please report this failure to the PCI "
9443 "maintainer and include system chipset information\n");
Michael Chan79381092005-04-21 17:13:59 -07009444
Matt Carlson4f125f42009-09-01 12:55:02 +00009445 free_irq(tp->napi[0].irq_vec, &tp->napi[0]);
Matt Carlson09943a12009-08-28 14:01:57 +00009446
Michael Chan79381092005-04-21 17:13:59 -07009447 pci_disable_msi(tp->pdev);
9448
Joe Perches63c3a662011-04-26 08:12:10 +00009449 tg3_flag_clear(tp, USING_MSI);
Andre Detschdc8bf1b2010-04-26 07:27:07 +00009450 tp->napi[0].irq_vec = tp->pdev->irq;
Michael Chan79381092005-04-21 17:13:59 -07009451
Matt Carlson4f125f42009-09-01 12:55:02 +00009452 err = tg3_request_irq(tp, 0);
Michael Chan79381092005-04-21 17:13:59 -07009453 if (err)
9454 return err;
9455
9456 /* Need to reset the chip because the MSI cycle may have terminated
9457 * with Master Abort.
9458 */
David S. Millerf47c11e2005-06-24 20:18:35 -07009459 tg3_full_lock(tp, 1);
Michael Chan79381092005-04-21 17:13:59 -07009460
Michael Chan944d9802005-05-29 14:57:48 -07009461 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07009462 err = tg3_init_hw(tp, 1);
Michael Chan79381092005-04-21 17:13:59 -07009463
David S. Millerf47c11e2005-06-24 20:18:35 -07009464 tg3_full_unlock(tp);
Michael Chan79381092005-04-21 17:13:59 -07009465
9466 if (err)
Matt Carlson4f125f42009-09-01 12:55:02 +00009467 free_irq(tp->napi[0].irq_vec, &tp->napi[0]);
Michael Chan79381092005-04-21 17:13:59 -07009468
9469 return err;
9470}
9471
Matt Carlson9e9fd122009-01-19 16:57:45 -08009472static int tg3_request_firmware(struct tg3 *tp)
9473{
9474 const __be32 *fw_data;
9475
9476 if (request_firmware(&tp->fw, tp->fw_needed, &tp->pdev->dev)) {
Joe Perches05dbe002010-02-17 19:44:19 +00009477 netdev_err(tp->dev, "Failed to load firmware \"%s\"\n",
9478 tp->fw_needed);
Matt Carlson9e9fd122009-01-19 16:57:45 -08009479 return -ENOENT;
9480 }
9481
9482 fw_data = (void *)tp->fw->data;
9483
9484 /* Firmware blob starts with version numbers, followed by
9485 * start address and _full_ length including BSS sections
9486 * (which must be longer than the actual data, of course
9487 */
9488
9489 tp->fw_len = be32_to_cpu(fw_data[2]); /* includes bss */
9490 if (tp->fw_len < (tp->fw->size - 12)) {
Joe Perches05dbe002010-02-17 19:44:19 +00009491 netdev_err(tp->dev, "bogus length %d in \"%s\"\n",
9492 tp->fw_len, tp->fw_needed);
Matt Carlson9e9fd122009-01-19 16:57:45 -08009493 release_firmware(tp->fw);
9494 tp->fw = NULL;
9495 return -EINVAL;
9496 }
9497
9498 /* We no longer need firmware; we have it. */
9499 tp->fw_needed = NULL;
9500 return 0;
9501}
9502
Matt Carlson679563f2009-09-01 12:55:46 +00009503static bool tg3_enable_msix(struct tg3 *tp)
9504{
9505 int i, rc, cpus = num_online_cpus();
9506 struct msix_entry msix_ent[tp->irq_max];
9507
9508 if (cpus == 1)
9509 /* Just fallback to the simpler MSI mode. */
9510 return false;
9511
9512 /*
9513 * We want as many rx rings enabled as there are cpus.
9514 * The first MSIX vector only deals with link interrupts, etc,
9515 * so we add one to the number of vectors we are requesting.
9516 */
9517 tp->irq_cnt = min_t(unsigned, cpus + 1, tp->irq_max);
9518
9519 for (i = 0; i < tp->irq_max; i++) {
9520 msix_ent[i].entry = i;
9521 msix_ent[i].vector = 0;
9522 }
9523
9524 rc = pci_enable_msix(tp->pdev, msix_ent, tp->irq_cnt);
Matt Carlson2430b032010-06-05 17:24:34 +00009525 if (rc < 0) {
9526 return false;
9527 } else if (rc != 0) {
Matt Carlson679563f2009-09-01 12:55:46 +00009528 if (pci_enable_msix(tp->pdev, msix_ent, rc))
9529 return false;
Joe Perches05dbe002010-02-17 19:44:19 +00009530 netdev_notice(tp->dev, "Requested %d MSI-X vectors, received %d\n",
9531 tp->irq_cnt, rc);
Matt Carlson679563f2009-09-01 12:55:46 +00009532 tp->irq_cnt = rc;
9533 }
9534
9535 for (i = 0; i < tp->irq_max; i++)
9536 tp->napi[i].irq_vec = msix_ent[i].vector;
9537
Ben Hutchings2ddaad32010-09-27 22:11:51 -07009538 netif_set_real_num_tx_queues(tp->dev, 1);
9539 rc = tp->irq_cnt > 1 ? tp->irq_cnt - 1 : 1;
9540 if (netif_set_real_num_rx_queues(tp->dev, rc)) {
9541 pci_disable_msix(tp->pdev);
9542 return false;
9543 }
Matt Carlsonb92b9042010-11-24 08:31:51 +00009544
9545 if (tp->irq_cnt > 1) {
Joe Perches63c3a662011-04-26 08:12:10 +00009546 tg3_flag_set(tp, ENABLE_RSS);
Matt Carlsond78b59f2011-04-05 14:22:46 +00009547
9548 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
9549 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
Joe Perches63c3a662011-04-26 08:12:10 +00009550 tg3_flag_set(tp, ENABLE_TSS);
Matt Carlsonb92b9042010-11-24 08:31:51 +00009551 netif_set_real_num_tx_queues(tp->dev, tp->irq_cnt - 1);
9552 }
9553 }
Matt Carlson2430b032010-06-05 17:24:34 +00009554
Matt Carlson679563f2009-09-01 12:55:46 +00009555 return true;
9556}
9557
Matt Carlson07b01732009-08-28 14:01:15 +00009558static void tg3_ints_init(struct tg3 *tp)
9559{
Joe Perches63c3a662011-04-26 08:12:10 +00009560 if ((tg3_flag(tp, SUPPORT_MSI) || tg3_flag(tp, SUPPORT_MSIX)) &&
9561 !tg3_flag(tp, TAGGED_STATUS)) {
Matt Carlson07b01732009-08-28 14:01:15 +00009562 /* All MSI supporting chips should support tagged
9563 * status. Assert that this is the case.
9564 */
Matt Carlson5129c3a2010-04-05 10:19:23 +00009565 netdev_warn(tp->dev,
9566 "MSI without TAGGED_STATUS? Not using MSI\n");
Matt Carlson679563f2009-09-01 12:55:46 +00009567 goto defcfg;
Matt Carlson07b01732009-08-28 14:01:15 +00009568 }
Matt Carlson4f125f42009-09-01 12:55:02 +00009569
Joe Perches63c3a662011-04-26 08:12:10 +00009570 if (tg3_flag(tp, SUPPORT_MSIX) && tg3_enable_msix(tp))
9571 tg3_flag_set(tp, USING_MSIX);
9572 else if (tg3_flag(tp, SUPPORT_MSI) && pci_enable_msi(tp->pdev) == 0)
9573 tg3_flag_set(tp, USING_MSI);
Matt Carlson679563f2009-09-01 12:55:46 +00009574
Joe Perches63c3a662011-04-26 08:12:10 +00009575 if (tg3_flag(tp, USING_MSI) || tg3_flag(tp, USING_MSIX)) {
Matt Carlson679563f2009-09-01 12:55:46 +00009576 u32 msi_mode = tr32(MSGINT_MODE);
Joe Perches63c3a662011-04-26 08:12:10 +00009577 if (tg3_flag(tp, USING_MSIX) && tp->irq_cnt > 1)
Matt Carlsonbaf8a942009-09-01 13:13:00 +00009578 msi_mode |= MSGINT_MODE_MULTIVEC_EN;
Matt Carlson5b39de92011-08-31 11:44:50 +00009579 if (!tg3_flag(tp, 1SHOT_MSI))
9580 msi_mode |= MSGINT_MODE_ONE_SHOT_DISABLE;
Matt Carlson679563f2009-09-01 12:55:46 +00009581 tw32(MSGINT_MODE, msi_mode | MSGINT_MODE_ENABLE);
9582 }
9583defcfg:
Joe Perches63c3a662011-04-26 08:12:10 +00009584 if (!tg3_flag(tp, USING_MSIX)) {
Matt Carlson679563f2009-09-01 12:55:46 +00009585 tp->irq_cnt = 1;
9586 tp->napi[0].irq_vec = tp->pdev->irq;
Ben Hutchings2ddaad32010-09-27 22:11:51 -07009587 netif_set_real_num_tx_queues(tp->dev, 1);
Matt Carlson85407882010-10-06 13:40:58 -07009588 netif_set_real_num_rx_queues(tp->dev, 1);
Matt Carlson679563f2009-09-01 12:55:46 +00009589 }
Matt Carlson07b01732009-08-28 14:01:15 +00009590}
9591
9592static void tg3_ints_fini(struct tg3 *tp)
9593{
Joe Perches63c3a662011-04-26 08:12:10 +00009594 if (tg3_flag(tp, USING_MSIX))
Matt Carlson679563f2009-09-01 12:55:46 +00009595 pci_disable_msix(tp->pdev);
Joe Perches63c3a662011-04-26 08:12:10 +00009596 else if (tg3_flag(tp, USING_MSI))
Matt Carlson679563f2009-09-01 12:55:46 +00009597 pci_disable_msi(tp->pdev);
Joe Perches63c3a662011-04-26 08:12:10 +00009598 tg3_flag_clear(tp, USING_MSI);
9599 tg3_flag_clear(tp, USING_MSIX);
9600 tg3_flag_clear(tp, ENABLE_RSS);
9601 tg3_flag_clear(tp, ENABLE_TSS);
Matt Carlson07b01732009-08-28 14:01:15 +00009602}
9603
Linus Torvalds1da177e2005-04-16 15:20:36 -07009604static int tg3_open(struct net_device *dev)
9605{
9606 struct tg3 *tp = netdev_priv(dev);
Matt Carlson4f125f42009-09-01 12:55:02 +00009607 int i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009608
Matt Carlson9e9fd122009-01-19 16:57:45 -08009609 if (tp->fw_needed) {
9610 err = tg3_request_firmware(tp);
9611 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0) {
9612 if (err)
9613 return err;
9614 } else if (err) {
Joe Perches05dbe002010-02-17 19:44:19 +00009615 netdev_warn(tp->dev, "TSO capability disabled\n");
Joe Perches63c3a662011-04-26 08:12:10 +00009616 tg3_flag_clear(tp, TSO_CAPABLE);
9617 } else if (!tg3_flag(tp, TSO_CAPABLE)) {
Joe Perches05dbe002010-02-17 19:44:19 +00009618 netdev_notice(tp->dev, "TSO capability restored\n");
Joe Perches63c3a662011-04-26 08:12:10 +00009619 tg3_flag_set(tp, TSO_CAPABLE);
Matt Carlson9e9fd122009-01-19 16:57:45 -08009620 }
9621 }
9622
Michael Chanc49a1562006-12-17 17:07:29 -08009623 netif_carrier_off(tp->dev);
9624
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00009625 err = tg3_power_up(tp);
Matt Carlson2f751b62008-08-04 23:17:34 -07009626 if (err)
Michael Chanbc1c7562006-03-20 17:48:03 -08009627 return err;
Matt Carlson2f751b62008-08-04 23:17:34 -07009628
9629 tg3_full_lock(tp, 0);
Michael Chanbc1c7562006-03-20 17:48:03 -08009630
Linus Torvalds1da177e2005-04-16 15:20:36 -07009631 tg3_disable_ints(tp);
Joe Perches63c3a662011-04-26 08:12:10 +00009632 tg3_flag_clear(tp, INIT_COMPLETE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009633
David S. Millerf47c11e2005-06-24 20:18:35 -07009634 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009635
Matt Carlson679563f2009-09-01 12:55:46 +00009636 /*
9637 * Setup interrupts first so we know how
9638 * many NAPI resources to allocate
9639 */
9640 tg3_ints_init(tp);
9641
Linus Torvalds1da177e2005-04-16 15:20:36 -07009642 /* The placement of this call is tied
9643 * to the setup and use of Host TX descriptors.
9644 */
9645 err = tg3_alloc_consistent(tp);
9646 if (err)
Matt Carlson679563f2009-09-01 12:55:46 +00009647 goto err_out1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009648
Matt Carlson66cfd1b2010-09-30 10:34:30 +00009649 tg3_napi_init(tp);
9650
Matt Carlsonfed97812009-09-01 13:10:19 +00009651 tg3_napi_enable(tp);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07009652
Matt Carlson4f125f42009-09-01 12:55:02 +00009653 for (i = 0; i < tp->irq_cnt; i++) {
9654 struct tg3_napi *tnapi = &tp->napi[i];
9655 err = tg3_request_irq(tp, i);
9656 if (err) {
Matt Carlson5bc09182011-11-04 09:15:01 +00009657 for (i--; i >= 0; i--) {
9658 tnapi = &tp->napi[i];
Matt Carlson4f125f42009-09-01 12:55:02 +00009659 free_irq(tnapi->irq_vec, tnapi);
Matt Carlson5bc09182011-11-04 09:15:01 +00009660 }
9661 goto err_out2;
Matt Carlson4f125f42009-09-01 12:55:02 +00009662 }
9663 }
Matt Carlson07b01732009-08-28 14:01:15 +00009664
David S. Millerf47c11e2005-06-24 20:18:35 -07009665 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009666
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07009667 err = tg3_init_hw(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009668 if (err) {
Michael Chan944d9802005-05-29 14:57:48 -07009669 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009670 tg3_free_rings(tp);
9671 } else {
Matt Carlson0e6cf6a2011-06-13 13:38:55 +00009672 if (tg3_flag(tp, TAGGED_STATUS) &&
9673 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717 &&
9674 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_57765)
David S. Millerfac9b832005-05-18 22:46:34 -07009675 tp->timer_offset = HZ;
9676 else
9677 tp->timer_offset = HZ / 10;
9678
9679 BUG_ON(tp->timer_offset > HZ);
9680 tp->timer_counter = tp->timer_multiplier =
9681 (HZ / tp->timer_offset);
9682 tp->asf_counter = tp->asf_multiplier =
Michael Chan28fbef72005-10-26 15:48:35 -07009683 ((HZ / tp->timer_offset) * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009684
9685 init_timer(&tp->timer);
9686 tp->timer.expires = jiffies + tp->timer_offset;
9687 tp->timer.data = (unsigned long) tp;
9688 tp->timer.function = tg3_timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009689 }
9690
David S. Millerf47c11e2005-06-24 20:18:35 -07009691 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009692
Matt Carlson07b01732009-08-28 14:01:15 +00009693 if (err)
Matt Carlson679563f2009-09-01 12:55:46 +00009694 goto err_out3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009695
Joe Perches63c3a662011-04-26 08:12:10 +00009696 if (tg3_flag(tp, USING_MSI)) {
Michael Chan79381092005-04-21 17:13:59 -07009697 err = tg3_test_msi(tp);
David S. Millerfac9b832005-05-18 22:46:34 -07009698
Michael Chan79381092005-04-21 17:13:59 -07009699 if (err) {
David S. Millerf47c11e2005-06-24 20:18:35 -07009700 tg3_full_lock(tp, 0);
Michael Chan944d9802005-05-29 14:57:48 -07009701 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Michael Chan79381092005-04-21 17:13:59 -07009702 tg3_free_rings(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -07009703 tg3_full_unlock(tp);
Michael Chan79381092005-04-21 17:13:59 -07009704
Matt Carlson679563f2009-09-01 12:55:46 +00009705 goto err_out2;
Michael Chan79381092005-04-21 17:13:59 -07009706 }
Michael Chanfcfa0a32006-03-20 22:28:41 -08009707
Joe Perches63c3a662011-04-26 08:12:10 +00009708 if (!tg3_flag(tp, 57765_PLUS) && tg3_flag(tp, USING_MSI)) {
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009709 u32 val = tr32(PCIE_TRANSACTION_CFG);
Michael Chanfcfa0a32006-03-20 22:28:41 -08009710
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009711 tw32(PCIE_TRANSACTION_CFG,
9712 val | PCIE_TRANS_CFG_1SHOT_MSI);
Michael Chanfcfa0a32006-03-20 22:28:41 -08009713 }
Michael Chan79381092005-04-21 17:13:59 -07009714 }
9715
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009716 tg3_phy_start(tp);
9717
David S. Millerf47c11e2005-06-24 20:18:35 -07009718 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009719
Michael Chan79381092005-04-21 17:13:59 -07009720 add_timer(&tp->timer);
Joe Perches63c3a662011-04-26 08:12:10 +00009721 tg3_flag_set(tp, INIT_COMPLETE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009722 tg3_enable_ints(tp);
9723
David S. Millerf47c11e2005-06-24 20:18:35 -07009724 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009725
Matt Carlsonfe5f5782009-09-01 13:09:39 +00009726 netif_tx_start_all_queues(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009727
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00009728 /*
9729 * Reset loopback feature if it was turned on while the device was down
9730 * make sure that it's installed properly now.
9731 */
9732 if (dev->features & NETIF_F_LOOPBACK)
9733 tg3_set_loopback(dev, dev->features);
9734
Linus Torvalds1da177e2005-04-16 15:20:36 -07009735 return 0;
Matt Carlson07b01732009-08-28 14:01:15 +00009736
Matt Carlson679563f2009-09-01 12:55:46 +00009737err_out3:
Matt Carlson4f125f42009-09-01 12:55:02 +00009738 for (i = tp->irq_cnt - 1; i >= 0; i--) {
9739 struct tg3_napi *tnapi = &tp->napi[i];
9740 free_irq(tnapi->irq_vec, tnapi);
9741 }
Matt Carlson07b01732009-08-28 14:01:15 +00009742
Matt Carlson679563f2009-09-01 12:55:46 +00009743err_out2:
Matt Carlsonfed97812009-09-01 13:10:19 +00009744 tg3_napi_disable(tp);
Matt Carlson66cfd1b2010-09-30 10:34:30 +00009745 tg3_napi_fini(tp);
Matt Carlson07b01732009-08-28 14:01:15 +00009746 tg3_free_consistent(tp);
Matt Carlson679563f2009-09-01 12:55:46 +00009747
9748err_out1:
9749 tg3_ints_fini(tp);
Matt Carlsoncd0d7222011-07-13 09:27:33 +00009750 tg3_frob_aux_power(tp, false);
9751 pci_set_power_state(tp->pdev, PCI_D3hot);
Matt Carlson07b01732009-08-28 14:01:15 +00009752 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009753}
9754
Linus Torvalds1da177e2005-04-16 15:20:36 -07009755static int tg3_close(struct net_device *dev)
9756{
Matt Carlson4f125f42009-09-01 12:55:02 +00009757 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009758 struct tg3 *tp = netdev_priv(dev);
9759
Matt Carlsonfed97812009-09-01 13:10:19 +00009760 tg3_napi_disable(tp);
Matt Carlsondb219972011-11-04 09:15:03 +00009761 tg3_reset_task_cancel(tp);
Michael Chan7faa0062006-02-02 17:29:28 -08009762
Matt Carlsonfe5f5782009-09-01 13:09:39 +00009763 netif_tx_stop_all_queues(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009764
9765 del_timer_sync(&tp->timer);
9766
Matt Carlson24bb4fb2009-10-05 17:55:29 +00009767 tg3_phy_stop(tp);
9768
David S. Millerf47c11e2005-06-24 20:18:35 -07009769 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009770
9771 tg3_disable_ints(tp);
9772
Michael Chan944d9802005-05-29 14:57:48 -07009773 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009774 tg3_free_rings(tp);
Joe Perches63c3a662011-04-26 08:12:10 +00009775 tg3_flag_clear(tp, INIT_COMPLETE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009776
David S. Millerf47c11e2005-06-24 20:18:35 -07009777 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009778
Matt Carlson4f125f42009-09-01 12:55:02 +00009779 for (i = tp->irq_cnt - 1; i >= 0; i--) {
9780 struct tg3_napi *tnapi = &tp->napi[i];
9781 free_irq(tnapi->irq_vec, tnapi);
9782 }
Matt Carlson07b01732009-08-28 14:01:15 +00009783
9784 tg3_ints_fini(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009785
Matt Carlson92feeab2011-12-08 14:40:14 +00009786 /* Clear stats across close / open calls */
9787 memset(&tp->net_stats_prev, 0, sizeof(tp->net_stats_prev));
9788 memset(&tp->estats_prev, 0, sizeof(tp->estats_prev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07009789
Matt Carlson66cfd1b2010-09-30 10:34:30 +00009790 tg3_napi_fini(tp);
9791
Linus Torvalds1da177e2005-04-16 15:20:36 -07009792 tg3_free_consistent(tp);
9793
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00009794 tg3_power_down(tp);
Michael Chanbc1c7562006-03-20 17:48:03 -08009795
9796 netif_carrier_off(tp->dev);
9797
Linus Torvalds1da177e2005-04-16 15:20:36 -07009798 return 0;
9799}
9800
Eric Dumazet511d2222010-07-07 20:44:24 +00009801static inline u64 get_stat64(tg3_stat64_t *val)
Stefan Buehler816f8b82008-08-15 14:10:54 -07009802{
9803 return ((u64)val->high << 32) | ((u64)val->low);
9804}
9805
Eric Dumazet511d2222010-07-07 20:44:24 +00009806static u64 calc_crc_errors(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009807{
9808 struct tg3_hw_stats *hw_stats = tp->hw_stats;
9809
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009810 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07009811 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
9812 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009813 u32 val;
9814
David S. Millerf47c11e2005-06-24 20:18:35 -07009815 spin_lock_bh(&tp->lock);
Michael Chan569a5df2007-02-13 12:18:15 -08009816 if (!tg3_readphy(tp, MII_TG3_TEST1, &val)) {
9817 tg3_writephy(tp, MII_TG3_TEST1,
9818 val | MII_TG3_TEST1_CRC_EN);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00009819 tg3_readphy(tp, MII_TG3_RXR_COUNTERS, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009820 } else
9821 val = 0;
David S. Millerf47c11e2005-06-24 20:18:35 -07009822 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009823
9824 tp->phy_crc_errors += val;
9825
9826 return tp->phy_crc_errors;
9827 }
9828
9829 return get_stat64(&hw_stats->rx_fcs_errors);
9830}
9831
9832#define ESTAT_ADD(member) \
9833 estats->member = old_estats->member + \
Eric Dumazet511d2222010-07-07 20:44:24 +00009834 get_stat64(&hw_stats->member)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009835
Matt Carlson0e6c9da2011-12-08 14:40:13 +00009836static struct tg3_ethtool_stats *tg3_get_estats(struct tg3 *tp,
9837 struct tg3_ethtool_stats *estats)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009838{
Linus Torvalds1da177e2005-04-16 15:20:36 -07009839 struct tg3_ethtool_stats *old_estats = &tp->estats_prev;
9840 struct tg3_hw_stats *hw_stats = tp->hw_stats;
9841
9842 if (!hw_stats)
9843 return old_estats;
9844
9845 ESTAT_ADD(rx_octets);
9846 ESTAT_ADD(rx_fragments);
9847 ESTAT_ADD(rx_ucast_packets);
9848 ESTAT_ADD(rx_mcast_packets);
9849 ESTAT_ADD(rx_bcast_packets);
9850 ESTAT_ADD(rx_fcs_errors);
9851 ESTAT_ADD(rx_align_errors);
9852 ESTAT_ADD(rx_xon_pause_rcvd);
9853 ESTAT_ADD(rx_xoff_pause_rcvd);
9854 ESTAT_ADD(rx_mac_ctrl_rcvd);
9855 ESTAT_ADD(rx_xoff_entered);
9856 ESTAT_ADD(rx_frame_too_long_errors);
9857 ESTAT_ADD(rx_jabbers);
9858 ESTAT_ADD(rx_undersize_packets);
9859 ESTAT_ADD(rx_in_length_errors);
9860 ESTAT_ADD(rx_out_length_errors);
9861 ESTAT_ADD(rx_64_or_less_octet_packets);
9862 ESTAT_ADD(rx_65_to_127_octet_packets);
9863 ESTAT_ADD(rx_128_to_255_octet_packets);
9864 ESTAT_ADD(rx_256_to_511_octet_packets);
9865 ESTAT_ADD(rx_512_to_1023_octet_packets);
9866 ESTAT_ADD(rx_1024_to_1522_octet_packets);
9867 ESTAT_ADD(rx_1523_to_2047_octet_packets);
9868 ESTAT_ADD(rx_2048_to_4095_octet_packets);
9869 ESTAT_ADD(rx_4096_to_8191_octet_packets);
9870 ESTAT_ADD(rx_8192_to_9022_octet_packets);
9871
9872 ESTAT_ADD(tx_octets);
9873 ESTAT_ADD(tx_collisions);
9874 ESTAT_ADD(tx_xon_sent);
9875 ESTAT_ADD(tx_xoff_sent);
9876 ESTAT_ADD(tx_flow_control);
9877 ESTAT_ADD(tx_mac_errors);
9878 ESTAT_ADD(tx_single_collisions);
9879 ESTAT_ADD(tx_mult_collisions);
9880 ESTAT_ADD(tx_deferred);
9881 ESTAT_ADD(tx_excessive_collisions);
9882 ESTAT_ADD(tx_late_collisions);
9883 ESTAT_ADD(tx_collide_2times);
9884 ESTAT_ADD(tx_collide_3times);
9885 ESTAT_ADD(tx_collide_4times);
9886 ESTAT_ADD(tx_collide_5times);
9887 ESTAT_ADD(tx_collide_6times);
9888 ESTAT_ADD(tx_collide_7times);
9889 ESTAT_ADD(tx_collide_8times);
9890 ESTAT_ADD(tx_collide_9times);
9891 ESTAT_ADD(tx_collide_10times);
9892 ESTAT_ADD(tx_collide_11times);
9893 ESTAT_ADD(tx_collide_12times);
9894 ESTAT_ADD(tx_collide_13times);
9895 ESTAT_ADD(tx_collide_14times);
9896 ESTAT_ADD(tx_collide_15times);
9897 ESTAT_ADD(tx_ucast_packets);
9898 ESTAT_ADD(tx_mcast_packets);
9899 ESTAT_ADD(tx_bcast_packets);
9900 ESTAT_ADD(tx_carrier_sense_errors);
9901 ESTAT_ADD(tx_discards);
9902 ESTAT_ADD(tx_errors);
9903
9904 ESTAT_ADD(dma_writeq_full);
9905 ESTAT_ADD(dma_write_prioq_full);
9906 ESTAT_ADD(rxbds_empty);
9907 ESTAT_ADD(rx_discards);
9908 ESTAT_ADD(rx_errors);
9909 ESTAT_ADD(rx_threshold_hit);
9910
9911 ESTAT_ADD(dma_readq_full);
9912 ESTAT_ADD(dma_read_prioq_full);
9913 ESTAT_ADD(tx_comp_queue_full);
9914
9915 ESTAT_ADD(ring_set_send_prod_index);
9916 ESTAT_ADD(ring_status_update);
9917 ESTAT_ADD(nic_irqs);
9918 ESTAT_ADD(nic_avoided_irqs);
9919 ESTAT_ADD(nic_tx_threshold_hit);
9920
Matt Carlson4452d092011-05-19 12:12:51 +00009921 ESTAT_ADD(mbuf_lwm_thresh_hit);
9922
Linus Torvalds1da177e2005-04-16 15:20:36 -07009923 return estats;
9924}
9925
Eric Dumazet511d2222010-07-07 20:44:24 +00009926static struct rtnl_link_stats64 *tg3_get_stats64(struct net_device *dev,
9927 struct rtnl_link_stats64 *stats)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009928{
9929 struct tg3 *tp = netdev_priv(dev);
Eric Dumazet511d2222010-07-07 20:44:24 +00009930 struct rtnl_link_stats64 *old_stats = &tp->net_stats_prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009931 struct tg3_hw_stats *hw_stats = tp->hw_stats;
9932
9933 if (!hw_stats)
9934 return old_stats;
9935
9936 stats->rx_packets = old_stats->rx_packets +
9937 get_stat64(&hw_stats->rx_ucast_packets) +
9938 get_stat64(&hw_stats->rx_mcast_packets) +
9939 get_stat64(&hw_stats->rx_bcast_packets);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009940
Linus Torvalds1da177e2005-04-16 15:20:36 -07009941 stats->tx_packets = old_stats->tx_packets +
9942 get_stat64(&hw_stats->tx_ucast_packets) +
9943 get_stat64(&hw_stats->tx_mcast_packets) +
9944 get_stat64(&hw_stats->tx_bcast_packets);
9945
9946 stats->rx_bytes = old_stats->rx_bytes +
9947 get_stat64(&hw_stats->rx_octets);
9948 stats->tx_bytes = old_stats->tx_bytes +
9949 get_stat64(&hw_stats->tx_octets);
9950
9951 stats->rx_errors = old_stats->rx_errors +
John W. Linville4f63b872005-09-12 14:43:18 -07009952 get_stat64(&hw_stats->rx_errors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009953 stats->tx_errors = old_stats->tx_errors +
9954 get_stat64(&hw_stats->tx_errors) +
9955 get_stat64(&hw_stats->tx_mac_errors) +
9956 get_stat64(&hw_stats->tx_carrier_sense_errors) +
9957 get_stat64(&hw_stats->tx_discards);
9958
9959 stats->multicast = old_stats->multicast +
9960 get_stat64(&hw_stats->rx_mcast_packets);
9961 stats->collisions = old_stats->collisions +
9962 get_stat64(&hw_stats->tx_collisions);
9963
9964 stats->rx_length_errors = old_stats->rx_length_errors +
9965 get_stat64(&hw_stats->rx_frame_too_long_errors) +
9966 get_stat64(&hw_stats->rx_undersize_packets);
9967
9968 stats->rx_over_errors = old_stats->rx_over_errors +
9969 get_stat64(&hw_stats->rxbds_empty);
9970 stats->rx_frame_errors = old_stats->rx_frame_errors +
9971 get_stat64(&hw_stats->rx_align_errors);
9972 stats->tx_aborted_errors = old_stats->tx_aborted_errors +
9973 get_stat64(&hw_stats->tx_discards);
9974 stats->tx_carrier_errors = old_stats->tx_carrier_errors +
9975 get_stat64(&hw_stats->tx_carrier_sense_errors);
9976
9977 stats->rx_crc_errors = old_stats->rx_crc_errors +
9978 calc_crc_errors(tp);
9979
John W. Linville4f63b872005-09-12 14:43:18 -07009980 stats->rx_missed_errors = old_stats->rx_missed_errors +
9981 get_stat64(&hw_stats->rx_discards);
9982
Eric Dumazetb0057c52010-10-10 19:55:52 +00009983 stats->rx_dropped = tp->rx_dropped;
Eric Dumazet48855432011-10-24 07:53:03 +00009984 stats->tx_dropped = tp->tx_dropped;
Eric Dumazetb0057c52010-10-10 19:55:52 +00009985
Linus Torvalds1da177e2005-04-16 15:20:36 -07009986 return stats;
9987}
9988
9989static inline u32 calc_crc(unsigned char *buf, int len)
9990{
9991 u32 reg;
9992 u32 tmp;
9993 int j, k;
9994
9995 reg = 0xffffffff;
9996
9997 for (j = 0; j < len; j++) {
9998 reg ^= buf[j];
9999
10000 for (k = 0; k < 8; k++) {
10001 tmp = reg & 0x01;
10002
10003 reg >>= 1;
10004
Matt Carlson859a5882010-04-05 10:19:28 +000010005 if (tmp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010006 reg ^= 0xedb88320;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010007 }
10008 }
10009
10010 return ~reg;
10011}
10012
10013static void tg3_set_multi(struct tg3 *tp, unsigned int accept_all)
10014{
10015 /* accept or reject all multicast frames */
10016 tw32(MAC_HASH_REG_0, accept_all ? 0xffffffff : 0);
10017 tw32(MAC_HASH_REG_1, accept_all ? 0xffffffff : 0);
10018 tw32(MAC_HASH_REG_2, accept_all ? 0xffffffff : 0);
10019 tw32(MAC_HASH_REG_3, accept_all ? 0xffffffff : 0);
10020}
10021
10022static void __tg3_set_rx_mode(struct net_device *dev)
10023{
10024 struct tg3 *tp = netdev_priv(dev);
10025 u32 rx_mode;
10026
10027 rx_mode = tp->rx_mode & ~(RX_MODE_PROMISC |
10028 RX_MODE_KEEP_VLAN_TAG);
10029
Matt Carlsonbf933c82011-01-25 15:58:49 +000010030#if !defined(CONFIG_VLAN_8021Q) && !defined(CONFIG_VLAN_8021Q_MODULE)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010031 /* When ASF is in use, we always keep the RX_MODE_KEEP_VLAN_TAG
10032 * flag clear.
10033 */
Joe Perches63c3a662011-04-26 08:12:10 +000010034 if (!tg3_flag(tp, ENABLE_ASF))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010035 rx_mode |= RX_MODE_KEEP_VLAN_TAG;
10036#endif
10037
10038 if (dev->flags & IFF_PROMISC) {
10039 /* Promiscuous mode. */
10040 rx_mode |= RX_MODE_PROMISC;
10041 } else if (dev->flags & IFF_ALLMULTI) {
10042 /* Accept all multicast. */
Matt Carlsonde6f31e2010-04-12 06:58:30 +000010043 tg3_set_multi(tp, 1);
Jiri Pirko4cd24ea2010-02-08 04:30:35 +000010044 } else if (netdev_mc_empty(dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010045 /* Reject all multicast. */
Matt Carlsonde6f31e2010-04-12 06:58:30 +000010046 tg3_set_multi(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010047 } else {
10048 /* Accept one or more multicast(s). */
Jiri Pirko22bedad2010-04-01 21:22:57 +000010049 struct netdev_hw_addr *ha;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010050 u32 mc_filter[4] = { 0, };
10051 u32 regidx;
10052 u32 bit;
10053 u32 crc;
10054
Jiri Pirko22bedad2010-04-01 21:22:57 +000010055 netdev_for_each_mc_addr(ha, dev) {
10056 crc = calc_crc(ha->addr, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010057 bit = ~crc & 0x7f;
10058 regidx = (bit & 0x60) >> 5;
10059 bit &= 0x1f;
10060 mc_filter[regidx] |= (1 << bit);
10061 }
10062
10063 tw32(MAC_HASH_REG_0, mc_filter[0]);
10064 tw32(MAC_HASH_REG_1, mc_filter[1]);
10065 tw32(MAC_HASH_REG_2, mc_filter[2]);
10066 tw32(MAC_HASH_REG_3, mc_filter[3]);
10067 }
10068
10069 if (rx_mode != tp->rx_mode) {
10070 tp->rx_mode = rx_mode;
10071 tw32_f(MAC_RX_MODE, rx_mode);
10072 udelay(10);
10073 }
10074}
10075
10076static void tg3_set_rx_mode(struct net_device *dev)
10077{
10078 struct tg3 *tp = netdev_priv(dev);
10079
Michael Chane75f7c92006-03-20 21:33:26 -080010080 if (!netif_running(dev))
10081 return;
10082
David S. Millerf47c11e2005-06-24 20:18:35 -070010083 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010084 __tg3_set_rx_mode(dev);
David S. Millerf47c11e2005-06-24 20:18:35 -070010085 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010086}
10087
Linus Torvalds1da177e2005-04-16 15:20:36 -070010088static int tg3_get_regs_len(struct net_device *dev)
10089{
Matt Carlson97bd8e42011-04-13 11:05:04 +000010090 return TG3_REG_BLK_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010091}
10092
10093static void tg3_get_regs(struct net_device *dev,
10094 struct ethtool_regs *regs, void *_p)
10095{
Linus Torvalds1da177e2005-04-16 15:20:36 -070010096 struct tg3 *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010097
10098 regs->version = 0;
10099
Matt Carlson97bd8e42011-04-13 11:05:04 +000010100 memset(_p, 0, TG3_REG_BLK_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010101
Matt Carlson80096062010-08-02 11:26:06 +000010102 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Michael Chanbc1c7562006-03-20 17:48:03 -080010103 return;
10104
David S. Millerf47c11e2005-06-24 20:18:35 -070010105 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010106
Matt Carlson97bd8e42011-04-13 11:05:04 +000010107 tg3_dump_legacy_regs(tp, (u32 *)_p);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010108
David S. Millerf47c11e2005-06-24 20:18:35 -070010109 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010110}
10111
10112static int tg3_get_eeprom_len(struct net_device *dev)
10113{
10114 struct tg3 *tp = netdev_priv(dev);
10115
10116 return tp->nvram_size;
10117}
10118
Linus Torvalds1da177e2005-04-16 15:20:36 -070010119static int tg3_get_eeprom(struct net_device *dev, struct ethtool_eeprom *eeprom, u8 *data)
10120{
10121 struct tg3 *tp = netdev_priv(dev);
10122 int ret;
10123 u8 *pd;
Al Virob9fc7dc2007-12-17 22:59:57 -080010124 u32 i, offset, len, b_offset, b_count;
Matt Carlsona9dc5292009-02-25 14:25:30 +000010125 __be32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010126
Joe Perches63c3a662011-04-26 08:12:10 +000010127 if (tg3_flag(tp, NO_NVRAM))
Matt Carlsondf259d82009-04-20 06:57:14 +000010128 return -EINVAL;
10129
Matt Carlson80096062010-08-02 11:26:06 +000010130 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Michael Chanbc1c7562006-03-20 17:48:03 -080010131 return -EAGAIN;
10132
Linus Torvalds1da177e2005-04-16 15:20:36 -070010133 offset = eeprom->offset;
10134 len = eeprom->len;
10135 eeprom->len = 0;
10136
10137 eeprom->magic = TG3_EEPROM_MAGIC;
10138
10139 if (offset & 3) {
10140 /* adjustments to start on required 4 byte boundary */
10141 b_offset = offset & 3;
10142 b_count = 4 - b_offset;
10143 if (b_count > len) {
10144 /* i.e. offset=1 len=2 */
10145 b_count = len;
10146 }
Matt Carlsona9dc5292009-02-25 14:25:30 +000010147 ret = tg3_nvram_read_be32(tp, offset-b_offset, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010148 if (ret)
10149 return ret;
Matt Carlsonbe98da62010-07-11 09:31:46 +000010150 memcpy(data, ((char *)&val) + b_offset, b_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010151 len -= b_count;
10152 offset += b_count;
Matt Carlsonc6cdf432010-04-05 10:19:26 +000010153 eeprom->len += b_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010154 }
10155
Lucas De Marchi25985ed2011-03-30 22:57:33 -030010156 /* read bytes up to the last 4 byte boundary */
Linus Torvalds1da177e2005-04-16 15:20:36 -070010157 pd = &data[eeprom->len];
10158 for (i = 0; i < (len - (len & 3)); i += 4) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000010159 ret = tg3_nvram_read_be32(tp, offset + i, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010160 if (ret) {
10161 eeprom->len += i;
10162 return ret;
10163 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010164 memcpy(pd + i, &val, 4);
10165 }
10166 eeprom->len += i;
10167
10168 if (len & 3) {
10169 /* read last bytes not ending on 4 byte boundary */
10170 pd = &data[eeprom->len];
10171 b_count = len & 3;
10172 b_offset = offset + len - b_count;
Matt Carlsona9dc5292009-02-25 14:25:30 +000010173 ret = tg3_nvram_read_be32(tp, b_offset, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010174 if (ret)
10175 return ret;
Al Virob9fc7dc2007-12-17 22:59:57 -080010176 memcpy(pd, &val, b_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010177 eeprom->len += b_count;
10178 }
10179 return 0;
10180}
10181
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010182static int tg3_nvram_write_block(struct tg3 *tp, u32 offset, u32 len, u8 *buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010183
10184static int tg3_set_eeprom(struct net_device *dev, struct ethtool_eeprom *eeprom, u8 *data)
10185{
10186 struct tg3 *tp = netdev_priv(dev);
10187 int ret;
Al Virob9fc7dc2007-12-17 22:59:57 -080010188 u32 offset, len, b_offset, odd_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010189 u8 *buf;
Matt Carlsona9dc5292009-02-25 14:25:30 +000010190 __be32 start, end;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010191
Matt Carlson80096062010-08-02 11:26:06 +000010192 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Michael Chanbc1c7562006-03-20 17:48:03 -080010193 return -EAGAIN;
10194
Joe Perches63c3a662011-04-26 08:12:10 +000010195 if (tg3_flag(tp, NO_NVRAM) ||
Matt Carlsondf259d82009-04-20 06:57:14 +000010196 eeprom->magic != TG3_EEPROM_MAGIC)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010197 return -EINVAL;
10198
10199 offset = eeprom->offset;
10200 len = eeprom->len;
10201
10202 if ((b_offset = (offset & 3))) {
10203 /* adjustments to start on required 4 byte boundary */
Matt Carlsona9dc5292009-02-25 14:25:30 +000010204 ret = tg3_nvram_read_be32(tp, offset-b_offset, &start);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010205 if (ret)
10206 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010207 len += b_offset;
10208 offset &= ~3;
Michael Chan1c8594b2005-04-21 17:12:46 -070010209 if (len < 4)
10210 len = 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010211 }
10212
10213 odd_len = 0;
Michael Chan1c8594b2005-04-21 17:12:46 -070010214 if (len & 3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010215 /* adjustments to end on required 4 byte boundary */
10216 odd_len = 1;
10217 len = (len + 3) & ~3;
Matt Carlsona9dc5292009-02-25 14:25:30 +000010218 ret = tg3_nvram_read_be32(tp, offset+len-4, &end);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010219 if (ret)
10220 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010221 }
10222
10223 buf = data;
10224 if (b_offset || odd_len) {
10225 buf = kmalloc(len, GFP_KERNEL);
Andy Gospodarekab0049b2007-09-06 20:42:14 +010010226 if (!buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010227 return -ENOMEM;
10228 if (b_offset)
10229 memcpy(buf, &start, 4);
10230 if (odd_len)
10231 memcpy(buf+len-4, &end, 4);
10232 memcpy(buf + b_offset, data, eeprom->len);
10233 }
10234
10235 ret = tg3_nvram_write_block(tp, offset, len, buf);
10236
10237 if (buf != data)
10238 kfree(buf);
10239
10240 return ret;
10241}
10242
10243static int tg3_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
10244{
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010245 struct tg3 *tp = netdev_priv(dev);
10246
Joe Perches63c3a662011-04-26 08:12:10 +000010247 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000010248 struct phy_device *phydev;
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010249 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010250 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000010251 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
10252 return phy_ethtool_gset(phydev, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010253 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010254
Linus Torvalds1da177e2005-04-16 15:20:36 -070010255 cmd->supported = (SUPPORTED_Autoneg);
10256
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010257 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010258 cmd->supported |= (SUPPORTED_1000baseT_Half |
10259 SUPPORTED_1000baseT_Full);
10260
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010261 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010262 cmd->supported |= (SUPPORTED_100baseT_Half |
10263 SUPPORTED_100baseT_Full |
10264 SUPPORTED_10baseT_Half |
10265 SUPPORTED_10baseT_Full |
Matt Carlson3bebab52007-11-12 21:22:40 -080010266 SUPPORTED_TP);
Karsten Keilef348142006-05-12 12:49:08 -070010267 cmd->port = PORT_TP;
10268 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010269 cmd->supported |= SUPPORTED_FIBRE;
Karsten Keilef348142006-05-12 12:49:08 -070010270 cmd->port = PORT_FIBRE;
10271 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010272
Linus Torvalds1da177e2005-04-16 15:20:36 -070010273 cmd->advertising = tp->link_config.advertising;
Matt Carlson5bb09772011-06-13 13:39:00 +000010274 if (tg3_flag(tp, PAUSE_AUTONEG)) {
10275 if (tp->link_config.flowctrl & FLOW_CTRL_RX) {
10276 if (tp->link_config.flowctrl & FLOW_CTRL_TX) {
10277 cmd->advertising |= ADVERTISED_Pause;
10278 } else {
10279 cmd->advertising |= ADVERTISED_Pause |
10280 ADVERTISED_Asym_Pause;
10281 }
10282 } else if (tp->link_config.flowctrl & FLOW_CTRL_TX) {
10283 cmd->advertising |= ADVERTISED_Asym_Pause;
10284 }
10285 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010286 if (netif_running(dev)) {
David Decotigny70739492011-04-27 18:32:40 +000010287 ethtool_cmd_speed_set(cmd, tp->link_config.active_speed);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010288 cmd->duplex = tp->link_config.active_duplex;
Matt Carlsone348c5e2011-11-21 15:01:20 +000010289 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES)) {
10290 if (tp->phy_flags & TG3_PHYFLG_MDIX_STATE)
10291 cmd->eth_tp_mdix = ETH_TP_MDI_X;
10292 else
10293 cmd->eth_tp_mdix = ETH_TP_MDI;
10294 }
Matt Carlson64c22182010-10-14 10:37:44 +000010295 } else {
David Decotigny70739492011-04-27 18:32:40 +000010296 ethtool_cmd_speed_set(cmd, SPEED_INVALID);
Matt Carlson64c22182010-10-14 10:37:44 +000010297 cmd->duplex = DUPLEX_INVALID;
Matt Carlsone348c5e2011-11-21 15:01:20 +000010298 cmd->eth_tp_mdix = ETH_TP_MDI_INVALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010299 }
Matt Carlson882e9792009-09-01 13:21:36 +000010300 cmd->phy_address = tp->phy_addr;
Matt Carlson7e5856b2009-02-25 14:23:01 +000010301 cmd->transceiver = XCVR_INTERNAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010302 cmd->autoneg = tp->link_config.autoneg;
10303 cmd->maxtxpkt = 0;
10304 cmd->maxrxpkt = 0;
10305 return 0;
10306}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010307
Linus Torvalds1da177e2005-04-16 15:20:36 -070010308static int tg3_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
10309{
10310 struct tg3 *tp = netdev_priv(dev);
David Decotigny25db0332011-04-27 18:32:39 +000010311 u32 speed = ethtool_cmd_speed(cmd);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010312
Joe Perches63c3a662011-04-26 08:12:10 +000010313 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000010314 struct phy_device *phydev;
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010315 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010316 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000010317 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
10318 return phy_ethtool_sset(phydev, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010319 }
10320
Matt Carlson7e5856b2009-02-25 14:23:01 +000010321 if (cmd->autoneg != AUTONEG_ENABLE &&
10322 cmd->autoneg != AUTONEG_DISABLE)
Michael Chan37ff2382005-10-26 15:49:51 -070010323 return -EINVAL;
Matt Carlson7e5856b2009-02-25 14:23:01 +000010324
10325 if (cmd->autoneg == AUTONEG_DISABLE &&
10326 cmd->duplex != DUPLEX_FULL &&
10327 cmd->duplex != DUPLEX_HALF)
Michael Chan37ff2382005-10-26 15:49:51 -070010328 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010329
Matt Carlson7e5856b2009-02-25 14:23:01 +000010330 if (cmd->autoneg == AUTONEG_ENABLE) {
10331 u32 mask = ADVERTISED_Autoneg |
10332 ADVERTISED_Pause |
10333 ADVERTISED_Asym_Pause;
10334
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010335 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
Matt Carlson7e5856b2009-02-25 14:23:01 +000010336 mask |= ADVERTISED_1000baseT_Half |
10337 ADVERTISED_1000baseT_Full;
10338
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010339 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
Matt Carlson7e5856b2009-02-25 14:23:01 +000010340 mask |= ADVERTISED_100baseT_Half |
10341 ADVERTISED_100baseT_Full |
10342 ADVERTISED_10baseT_Half |
10343 ADVERTISED_10baseT_Full |
10344 ADVERTISED_TP;
10345 else
10346 mask |= ADVERTISED_FIBRE;
10347
10348 if (cmd->advertising & ~mask)
10349 return -EINVAL;
10350
10351 mask &= (ADVERTISED_1000baseT_Half |
10352 ADVERTISED_1000baseT_Full |
10353 ADVERTISED_100baseT_Half |
10354 ADVERTISED_100baseT_Full |
10355 ADVERTISED_10baseT_Half |
10356 ADVERTISED_10baseT_Full);
10357
10358 cmd->advertising &= mask;
10359 } else {
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010360 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES) {
David Decotigny25db0332011-04-27 18:32:39 +000010361 if (speed != SPEED_1000)
Matt Carlson7e5856b2009-02-25 14:23:01 +000010362 return -EINVAL;
10363
10364 if (cmd->duplex != DUPLEX_FULL)
10365 return -EINVAL;
10366 } else {
David Decotigny25db0332011-04-27 18:32:39 +000010367 if (speed != SPEED_100 &&
10368 speed != SPEED_10)
Matt Carlson7e5856b2009-02-25 14:23:01 +000010369 return -EINVAL;
10370 }
10371 }
10372
David S. Millerf47c11e2005-06-24 20:18:35 -070010373 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010374
10375 tp->link_config.autoneg = cmd->autoneg;
10376 if (cmd->autoneg == AUTONEG_ENABLE) {
Andy Gospodarek405d8e52007-10-08 01:08:47 -070010377 tp->link_config.advertising = (cmd->advertising |
10378 ADVERTISED_Autoneg);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010379 tp->link_config.speed = SPEED_INVALID;
10380 tp->link_config.duplex = DUPLEX_INVALID;
10381 } else {
10382 tp->link_config.advertising = 0;
David Decotigny25db0332011-04-27 18:32:39 +000010383 tp->link_config.speed = speed;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010384 tp->link_config.duplex = cmd->duplex;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010385 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010386
Michael Chan24fcad62006-12-17 17:06:46 -080010387 tp->link_config.orig_speed = tp->link_config.speed;
10388 tp->link_config.orig_duplex = tp->link_config.duplex;
10389 tp->link_config.orig_autoneg = tp->link_config.autoneg;
10390
Linus Torvalds1da177e2005-04-16 15:20:36 -070010391 if (netif_running(dev))
10392 tg3_setup_phy(tp, 1);
10393
David S. Millerf47c11e2005-06-24 20:18:35 -070010394 tg3_full_unlock(tp);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010395
Linus Torvalds1da177e2005-04-16 15:20:36 -070010396 return 0;
10397}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010398
Linus Torvalds1da177e2005-04-16 15:20:36 -070010399static void tg3_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
10400{
10401 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010402
Rick Jones68aad782011-11-07 13:29:27 +000010403 strlcpy(info->driver, DRV_MODULE_NAME, sizeof(info->driver));
10404 strlcpy(info->version, DRV_MODULE_VERSION, sizeof(info->version));
10405 strlcpy(info->fw_version, tp->fw_ver, sizeof(info->fw_version));
10406 strlcpy(info->bus_info, pci_name(tp->pdev), sizeof(info->bus_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -070010407}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010408
Linus Torvalds1da177e2005-04-16 15:20:36 -070010409static void tg3_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
10410{
10411 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010412
Joe Perches63c3a662011-04-26 08:12:10 +000010413 if (tg3_flag(tp, WOL_CAP) && device_can_wakeup(&tp->pdev->dev))
Gary Zambranoa85feb82007-05-05 11:52:19 -070010414 wol->supported = WAKE_MAGIC;
10415 else
10416 wol->supported = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010417 wol->wolopts = 0;
Joe Perches63c3a662011-04-26 08:12:10 +000010418 if (tg3_flag(tp, WOL_ENABLE) && device_can_wakeup(&tp->pdev->dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010419 wol->wolopts = WAKE_MAGIC;
10420 memset(&wol->sopass, 0, sizeof(wol->sopass));
10421}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010422
Linus Torvalds1da177e2005-04-16 15:20:36 -070010423static int tg3_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
10424{
10425 struct tg3 *tp = netdev_priv(dev);
Rafael J. Wysocki12dac072008-07-30 16:37:33 -070010426 struct device *dp = &tp->pdev->dev;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010427
Linus Torvalds1da177e2005-04-16 15:20:36 -070010428 if (wol->wolopts & ~WAKE_MAGIC)
10429 return -EINVAL;
10430 if ((wol->wolopts & WAKE_MAGIC) &&
Joe Perches63c3a662011-04-26 08:12:10 +000010431 !(tg3_flag(tp, WOL_CAP) && device_can_wakeup(dp)))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010432 return -EINVAL;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010433
Rafael J. Wysockif2dc0d12010-10-25 13:01:55 +000010434 device_set_wakeup_enable(dp, wol->wolopts & WAKE_MAGIC);
10435
David S. Millerf47c11e2005-06-24 20:18:35 -070010436 spin_lock_bh(&tp->lock);
Rafael J. Wysockif2dc0d12010-10-25 13:01:55 +000010437 if (device_may_wakeup(dp))
Joe Perches63c3a662011-04-26 08:12:10 +000010438 tg3_flag_set(tp, WOL_ENABLE);
Rafael J. Wysockif2dc0d12010-10-25 13:01:55 +000010439 else
Joe Perches63c3a662011-04-26 08:12:10 +000010440 tg3_flag_clear(tp, WOL_ENABLE);
David S. Millerf47c11e2005-06-24 20:18:35 -070010441 spin_unlock_bh(&tp->lock);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010442
Linus Torvalds1da177e2005-04-16 15:20:36 -070010443 return 0;
10444}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010445
Linus Torvalds1da177e2005-04-16 15:20:36 -070010446static u32 tg3_get_msglevel(struct net_device *dev)
10447{
10448 struct tg3 *tp = netdev_priv(dev);
10449 return tp->msg_enable;
10450}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010451
Linus Torvalds1da177e2005-04-16 15:20:36 -070010452static void tg3_set_msglevel(struct net_device *dev, u32 value)
10453{
10454 struct tg3 *tp = netdev_priv(dev);
10455 tp->msg_enable = value;
10456}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010457
Linus Torvalds1da177e2005-04-16 15:20:36 -070010458static int tg3_nway_reset(struct net_device *dev)
10459{
10460 struct tg3 *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010461 int r;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010462
Linus Torvalds1da177e2005-04-16 15:20:36 -070010463 if (!netif_running(dev))
10464 return -EAGAIN;
10465
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010466 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Michael Chanc94e3942005-09-27 12:12:42 -070010467 return -EINVAL;
10468
Joe Perches63c3a662011-04-26 08:12:10 +000010469 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010470 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010471 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000010472 r = phy_start_aneg(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010473 } else {
10474 u32 bmcr;
10475
10476 spin_lock_bh(&tp->lock);
10477 r = -EINVAL;
10478 tg3_readphy(tp, MII_BMCR, &bmcr);
10479 if (!tg3_readphy(tp, MII_BMCR, &bmcr) &&
10480 ((bmcr & BMCR_ANENABLE) ||
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010481 (tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT))) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010482 tg3_writephy(tp, MII_BMCR, bmcr | BMCR_ANRESTART |
10483 BMCR_ANENABLE);
10484 r = 0;
10485 }
10486 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010487 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010488
Linus Torvalds1da177e2005-04-16 15:20:36 -070010489 return r;
10490}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010491
Linus Torvalds1da177e2005-04-16 15:20:36 -070010492static void tg3_get_ringparam(struct net_device *dev, struct ethtool_ringparam *ering)
10493{
10494 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010495
Matt Carlson2c49a442010-09-30 10:34:35 +000010496 ering->rx_max_pending = tp->rx_std_ring_mask;
Joe Perches63c3a662011-04-26 08:12:10 +000010497 if (tg3_flag(tp, JUMBO_RING_ENABLE))
Matt Carlson2c49a442010-09-30 10:34:35 +000010498 ering->rx_jumbo_max_pending = tp->rx_jmb_ring_mask;
Michael Chan4f81c322006-03-20 21:33:42 -080010499 else
10500 ering->rx_jumbo_max_pending = 0;
10501
10502 ering->tx_max_pending = TG3_TX_RING_SIZE - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010503
10504 ering->rx_pending = tp->rx_pending;
Joe Perches63c3a662011-04-26 08:12:10 +000010505 if (tg3_flag(tp, JUMBO_RING_ENABLE))
Michael Chan4f81c322006-03-20 21:33:42 -080010506 ering->rx_jumbo_pending = tp->rx_jumbo_pending;
10507 else
10508 ering->rx_jumbo_pending = 0;
10509
Matt Carlsonf3f3f272009-08-28 14:03:21 +000010510 ering->tx_pending = tp->napi[0].tx_pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010511}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010512
Linus Torvalds1da177e2005-04-16 15:20:36 -070010513static int tg3_set_ringparam(struct net_device *dev, struct ethtool_ringparam *ering)
10514{
10515 struct tg3 *tp = netdev_priv(dev);
Matt Carlson646c9ed2009-09-01 12:58:41 +000010516 int i, irq_sync = 0, err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010517
Matt Carlson2c49a442010-09-30 10:34:35 +000010518 if ((ering->rx_pending > tp->rx_std_ring_mask) ||
10519 (ering->rx_jumbo_pending > tp->rx_jmb_ring_mask) ||
Michael Chanbc3a9252006-10-18 20:55:18 -070010520 (ering->tx_pending > TG3_TX_RING_SIZE - 1) ||
10521 (ering->tx_pending <= MAX_SKB_FRAGS) ||
Joe Perches63c3a662011-04-26 08:12:10 +000010522 (tg3_flag(tp, TSO_BUG) &&
Michael Chanbc3a9252006-10-18 20:55:18 -070010523 (ering->tx_pending <= (MAX_SKB_FRAGS * 3))))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010524 return -EINVAL;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010525
Michael Chanbbe832c2005-06-24 20:20:04 -070010526 if (netif_running(dev)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010527 tg3_phy_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010528 tg3_netif_stop(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -070010529 irq_sync = 1;
10530 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010531
Michael Chanbbe832c2005-06-24 20:20:04 -070010532 tg3_full_lock(tp, irq_sync);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010533
Linus Torvalds1da177e2005-04-16 15:20:36 -070010534 tp->rx_pending = ering->rx_pending;
10535
Joe Perches63c3a662011-04-26 08:12:10 +000010536 if (tg3_flag(tp, MAX_RXPEND_64) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070010537 tp->rx_pending > 63)
10538 tp->rx_pending = 63;
10539 tp->rx_jumbo_pending = ering->rx_jumbo_pending;
Matt Carlson646c9ed2009-09-01 12:58:41 +000010540
Matt Carlson6fd45cb2010-09-15 08:59:57 +000010541 for (i = 0; i < tp->irq_max; i++)
Matt Carlson646c9ed2009-09-01 12:58:41 +000010542 tp->napi[i].tx_pending = ering->tx_pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010543
10544 if (netif_running(dev)) {
Michael Chan944d9802005-05-29 14:57:48 -070010545 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Michael Chanb9ec6c12006-07-25 16:37:27 -070010546 err = tg3_restart_hw(tp, 1);
10547 if (!err)
10548 tg3_netif_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010549 }
10550
David S. Millerf47c11e2005-06-24 20:18:35 -070010551 tg3_full_unlock(tp);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010552
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010553 if (irq_sync && !err)
10554 tg3_phy_start(tp);
10555
Michael Chanb9ec6c12006-07-25 16:37:27 -070010556 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010557}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010558
Linus Torvalds1da177e2005-04-16 15:20:36 -070010559static void tg3_get_pauseparam(struct net_device *dev, struct ethtool_pauseparam *epause)
10560{
10561 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010562
Joe Perches63c3a662011-04-26 08:12:10 +000010563 epause->autoneg = !!tg3_flag(tp, PAUSE_AUTONEG);
Matt Carlson8d018622007-12-20 20:05:44 -080010564
Steve Glendinninge18ce342008-12-16 02:00:00 -080010565 if (tp->link_config.active_flowctrl & FLOW_CTRL_RX)
Matt Carlson8d018622007-12-20 20:05:44 -080010566 epause->rx_pause = 1;
10567 else
10568 epause->rx_pause = 0;
10569
Steve Glendinninge18ce342008-12-16 02:00:00 -080010570 if (tp->link_config.active_flowctrl & FLOW_CTRL_TX)
Matt Carlson8d018622007-12-20 20:05:44 -080010571 epause->tx_pause = 1;
10572 else
10573 epause->tx_pause = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010574}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010575
Linus Torvalds1da177e2005-04-16 15:20:36 -070010576static int tg3_set_pauseparam(struct net_device *dev, struct ethtool_pauseparam *epause)
10577{
10578 struct tg3 *tp = netdev_priv(dev);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010579 int err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010580
Joe Perches63c3a662011-04-26 08:12:10 +000010581 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson27121682010-02-17 15:16:57 +000010582 u32 newadv;
10583 struct phy_device *phydev;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010584
Matt Carlson27121682010-02-17 15:16:57 +000010585 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010586
Matt Carlson27121682010-02-17 15:16:57 +000010587 if (!(phydev->supported & SUPPORTED_Pause) ||
10588 (!(phydev->supported & SUPPORTED_Asym_Pause) &&
Nicolas Kaiser2259dca2010-10-07 23:29:27 +000010589 (epause->rx_pause != epause->tx_pause)))
Matt Carlson27121682010-02-17 15:16:57 +000010590 return -EINVAL;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010591
Matt Carlson27121682010-02-17 15:16:57 +000010592 tp->link_config.flowctrl = 0;
10593 if (epause->rx_pause) {
10594 tp->link_config.flowctrl |= FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010595
Matt Carlson27121682010-02-17 15:16:57 +000010596 if (epause->tx_pause) {
Steve Glendinninge18ce342008-12-16 02:00:00 -080010597 tp->link_config.flowctrl |= FLOW_CTRL_TX;
Matt Carlson27121682010-02-17 15:16:57 +000010598 newadv = ADVERTISED_Pause;
10599 } else
10600 newadv = ADVERTISED_Pause |
10601 ADVERTISED_Asym_Pause;
10602 } else if (epause->tx_pause) {
10603 tp->link_config.flowctrl |= FLOW_CTRL_TX;
10604 newadv = ADVERTISED_Asym_Pause;
10605 } else
10606 newadv = 0;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010607
Matt Carlson27121682010-02-17 15:16:57 +000010608 if (epause->autoneg)
Joe Perches63c3a662011-04-26 08:12:10 +000010609 tg3_flag_set(tp, PAUSE_AUTONEG);
Matt Carlson27121682010-02-17 15:16:57 +000010610 else
Joe Perches63c3a662011-04-26 08:12:10 +000010611 tg3_flag_clear(tp, PAUSE_AUTONEG);
Matt Carlson27121682010-02-17 15:16:57 +000010612
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010613 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) {
Matt Carlson27121682010-02-17 15:16:57 +000010614 u32 oldadv = phydev->advertising &
10615 (ADVERTISED_Pause | ADVERTISED_Asym_Pause);
10616 if (oldadv != newadv) {
10617 phydev->advertising &=
10618 ~(ADVERTISED_Pause |
10619 ADVERTISED_Asym_Pause);
10620 phydev->advertising |= newadv;
10621 if (phydev->autoneg) {
10622 /*
10623 * Always renegotiate the link to
10624 * inform our link partner of our
10625 * flow control settings, even if the
10626 * flow control is forced. Let
10627 * tg3_adjust_link() do the final
10628 * flow control setup.
10629 */
10630 return phy_start_aneg(phydev);
10631 }
10632 }
10633
10634 if (!epause->autoneg)
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010635 tg3_setup_flow_control(tp, 0, 0);
Matt Carlson27121682010-02-17 15:16:57 +000010636 } else {
10637 tp->link_config.orig_advertising &=
10638 ~(ADVERTISED_Pause |
10639 ADVERTISED_Asym_Pause);
10640 tp->link_config.orig_advertising |= newadv;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010641 }
10642 } else {
10643 int irq_sync = 0;
10644
10645 if (netif_running(dev)) {
10646 tg3_netif_stop(tp);
10647 irq_sync = 1;
10648 }
10649
10650 tg3_full_lock(tp, irq_sync);
10651
10652 if (epause->autoneg)
Joe Perches63c3a662011-04-26 08:12:10 +000010653 tg3_flag_set(tp, PAUSE_AUTONEG);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010654 else
Joe Perches63c3a662011-04-26 08:12:10 +000010655 tg3_flag_clear(tp, PAUSE_AUTONEG);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010656 if (epause->rx_pause)
Steve Glendinninge18ce342008-12-16 02:00:00 -080010657 tp->link_config.flowctrl |= FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010658 else
Steve Glendinninge18ce342008-12-16 02:00:00 -080010659 tp->link_config.flowctrl &= ~FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010660 if (epause->tx_pause)
Steve Glendinninge18ce342008-12-16 02:00:00 -080010661 tp->link_config.flowctrl |= FLOW_CTRL_TX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010662 else
Steve Glendinninge18ce342008-12-16 02:00:00 -080010663 tp->link_config.flowctrl &= ~FLOW_CTRL_TX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010664
10665 if (netif_running(dev)) {
10666 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
10667 err = tg3_restart_hw(tp, 1);
10668 if (!err)
10669 tg3_netif_start(tp);
10670 }
10671
10672 tg3_full_unlock(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -070010673 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010674
Michael Chanb9ec6c12006-07-25 16:37:27 -070010675 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010676}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010677
Matt Carlsonde6f31e2010-04-12 06:58:30 +000010678static int tg3_get_sset_count(struct net_device *dev, int sset)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010679{
Jeff Garzikb9f2c042007-10-03 18:07:32 -070010680 switch (sset) {
10681 case ETH_SS_TEST:
10682 return TG3_NUM_TEST;
10683 case ETH_SS_STATS:
10684 return TG3_NUM_STATS;
10685 default:
10686 return -EOPNOTSUPP;
10687 }
Michael Chan4cafd3f2005-05-29 14:56:34 -070010688}
10689
Matt Carlsonde6f31e2010-04-12 06:58:30 +000010690static void tg3_get_strings(struct net_device *dev, u32 stringset, u8 *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010691{
10692 switch (stringset) {
10693 case ETH_SS_STATS:
10694 memcpy(buf, &ethtool_stats_keys, sizeof(ethtool_stats_keys));
10695 break;
Michael Chan4cafd3f2005-05-29 14:56:34 -070010696 case ETH_SS_TEST:
10697 memcpy(buf, &ethtool_test_keys, sizeof(ethtool_test_keys));
10698 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010699 default:
10700 WARN_ON(1); /* we need a WARN() */
10701 break;
10702 }
10703}
10704
stephen hemminger81b87092011-04-04 08:43:50 +000010705static int tg3_set_phys_id(struct net_device *dev,
10706 enum ethtool_phys_id_state state)
Michael Chan4009a932005-09-05 17:52:54 -070010707{
10708 struct tg3 *tp = netdev_priv(dev);
Michael Chan4009a932005-09-05 17:52:54 -070010709
10710 if (!netif_running(tp->dev))
10711 return -EAGAIN;
10712
stephen hemminger81b87092011-04-04 08:43:50 +000010713 switch (state) {
10714 case ETHTOOL_ID_ACTIVE:
Allan, Bruce Wfce55922011-04-13 13:09:10 +000010715 return 1; /* cycle on/off once per second */
Michael Chan4009a932005-09-05 17:52:54 -070010716
stephen hemminger81b87092011-04-04 08:43:50 +000010717 case ETHTOOL_ID_ON:
10718 tw32(MAC_LED_CTRL, LED_CTRL_LNKLED_OVERRIDE |
10719 LED_CTRL_1000MBPS_ON |
10720 LED_CTRL_100MBPS_ON |
10721 LED_CTRL_10MBPS_ON |
10722 LED_CTRL_TRAFFIC_OVERRIDE |
10723 LED_CTRL_TRAFFIC_BLINK |
10724 LED_CTRL_TRAFFIC_LED);
10725 break;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010726
stephen hemminger81b87092011-04-04 08:43:50 +000010727 case ETHTOOL_ID_OFF:
10728 tw32(MAC_LED_CTRL, LED_CTRL_LNKLED_OVERRIDE |
10729 LED_CTRL_TRAFFIC_OVERRIDE);
10730 break;
Michael Chan4009a932005-09-05 17:52:54 -070010731
stephen hemminger81b87092011-04-04 08:43:50 +000010732 case ETHTOOL_ID_INACTIVE:
10733 tw32(MAC_LED_CTRL, tp->led_ctrl);
10734 break;
Michael Chan4009a932005-09-05 17:52:54 -070010735 }
stephen hemminger81b87092011-04-04 08:43:50 +000010736
Michael Chan4009a932005-09-05 17:52:54 -070010737 return 0;
10738}
10739
Matt Carlsonde6f31e2010-04-12 06:58:30 +000010740static void tg3_get_ethtool_stats(struct net_device *dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010741 struct ethtool_stats *estats, u64 *tmp_stats)
10742{
10743 struct tg3 *tp = netdev_priv(dev);
Matt Carlson0e6c9da2011-12-08 14:40:13 +000010744
10745 tg3_get_estats(tp, (struct tg3_ethtool_stats *)tmp_stats);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010746}
10747
Matt Carlson535a4902011-07-20 10:20:56 +000010748static __be32 *tg3_vpd_readblock(struct tg3 *tp, u32 *vpdlen)
Matt Carlsonc3e94502011-04-13 11:05:08 +000010749{
10750 int i;
10751 __be32 *buf;
10752 u32 offset = 0, len = 0;
10753 u32 magic, val;
10754
Joe Perches63c3a662011-04-26 08:12:10 +000010755 if (tg3_flag(tp, NO_NVRAM) || tg3_nvram_read(tp, 0, &magic))
Matt Carlsonc3e94502011-04-13 11:05:08 +000010756 return NULL;
10757
10758 if (magic == TG3_EEPROM_MAGIC) {
10759 for (offset = TG3_NVM_DIR_START;
10760 offset < TG3_NVM_DIR_END;
10761 offset += TG3_NVM_DIRENT_SIZE) {
10762 if (tg3_nvram_read(tp, offset, &val))
10763 return NULL;
10764
10765 if ((val >> TG3_NVM_DIRTYPE_SHIFT) ==
10766 TG3_NVM_DIRTYPE_EXTVPD)
10767 break;
10768 }
10769
10770 if (offset != TG3_NVM_DIR_END) {
10771 len = (val & TG3_NVM_DIRTYPE_LENMSK) * 4;
10772 if (tg3_nvram_read(tp, offset + 4, &offset))
10773 return NULL;
10774
10775 offset = tg3_nvram_logical_addr(tp, offset);
10776 }
10777 }
10778
10779 if (!offset || !len) {
10780 offset = TG3_NVM_VPD_OFF;
10781 len = TG3_NVM_VPD_LEN;
10782 }
10783
10784 buf = kmalloc(len, GFP_KERNEL);
10785 if (buf == NULL)
10786 return NULL;
10787
10788 if (magic == TG3_EEPROM_MAGIC) {
10789 for (i = 0; i < len; i += 4) {
10790 /* The data is in little-endian format in NVRAM.
10791 * Use the big-endian read routines to preserve
10792 * the byte order as it exists in NVRAM.
10793 */
10794 if (tg3_nvram_read_be32(tp, offset + i, &buf[i/4]))
10795 goto error;
10796 }
10797 } else {
10798 u8 *ptr;
10799 ssize_t cnt;
10800 unsigned int pos = 0;
10801
10802 ptr = (u8 *)&buf[0];
10803 for (i = 0; pos < len && i < 3; i++, pos += cnt, ptr += cnt) {
10804 cnt = pci_read_vpd(tp->pdev, pos,
10805 len - pos, ptr);
10806 if (cnt == -ETIMEDOUT || cnt == -EINTR)
10807 cnt = 0;
10808 else if (cnt < 0)
10809 goto error;
10810 }
10811 if (pos != len)
10812 goto error;
10813 }
10814
Matt Carlson535a4902011-07-20 10:20:56 +000010815 *vpdlen = len;
10816
Matt Carlsonc3e94502011-04-13 11:05:08 +000010817 return buf;
10818
10819error:
10820 kfree(buf);
10821 return NULL;
10822}
10823
Michael Chan566f86a2005-05-29 14:56:58 -070010824#define NVRAM_TEST_SIZE 0x100
Matt Carlsona5767de2007-11-12 21:10:58 -080010825#define NVRAM_SELFBOOT_FORMAT1_0_SIZE 0x14
10826#define NVRAM_SELFBOOT_FORMAT1_2_SIZE 0x18
10827#define NVRAM_SELFBOOT_FORMAT1_3_SIZE 0x1c
Matt Carlson727a6d92011-06-13 13:38:58 +000010828#define NVRAM_SELFBOOT_FORMAT1_4_SIZE 0x20
10829#define NVRAM_SELFBOOT_FORMAT1_5_SIZE 0x24
Matt Carlsonbda18fa2011-07-20 10:20:57 +000010830#define NVRAM_SELFBOOT_FORMAT1_6_SIZE 0x50
Michael Chanb16250e2006-09-27 16:10:14 -070010831#define NVRAM_SELFBOOT_HW_SIZE 0x20
10832#define NVRAM_SELFBOOT_DATA_SIZE 0x1c
Michael Chan566f86a2005-05-29 14:56:58 -070010833
10834static int tg3_test_nvram(struct tg3 *tp)
10835{
Matt Carlson535a4902011-07-20 10:20:56 +000010836 u32 csum, magic, len;
Matt Carlsona9dc5292009-02-25 14:25:30 +000010837 __be32 *buf;
Andy Gospodarekab0049b2007-09-06 20:42:14 +010010838 int i, j, k, err = 0, size;
Michael Chan566f86a2005-05-29 14:56:58 -070010839
Joe Perches63c3a662011-04-26 08:12:10 +000010840 if (tg3_flag(tp, NO_NVRAM))
Matt Carlsondf259d82009-04-20 06:57:14 +000010841 return 0;
10842
Matt Carlsone4f34112009-02-25 14:25:00 +000010843 if (tg3_nvram_read(tp, 0, &magic) != 0)
Michael Chan1b277772006-03-20 22:27:48 -080010844 return -EIO;
10845
Michael Chan1b277772006-03-20 22:27:48 -080010846 if (magic == TG3_EEPROM_MAGIC)
10847 size = NVRAM_TEST_SIZE;
Michael Chanb16250e2006-09-27 16:10:14 -070010848 else if ((magic & TG3_EEPROM_MAGIC_FW_MSK) == TG3_EEPROM_MAGIC_FW) {
Matt Carlsona5767de2007-11-12 21:10:58 -080010849 if ((magic & TG3_EEPROM_SB_FORMAT_MASK) ==
10850 TG3_EEPROM_SB_FORMAT_1) {
10851 switch (magic & TG3_EEPROM_SB_REVISION_MASK) {
10852 case TG3_EEPROM_SB_REVISION_0:
10853 size = NVRAM_SELFBOOT_FORMAT1_0_SIZE;
10854 break;
10855 case TG3_EEPROM_SB_REVISION_2:
10856 size = NVRAM_SELFBOOT_FORMAT1_2_SIZE;
10857 break;
10858 case TG3_EEPROM_SB_REVISION_3:
10859 size = NVRAM_SELFBOOT_FORMAT1_3_SIZE;
10860 break;
Matt Carlson727a6d92011-06-13 13:38:58 +000010861 case TG3_EEPROM_SB_REVISION_4:
10862 size = NVRAM_SELFBOOT_FORMAT1_4_SIZE;
10863 break;
10864 case TG3_EEPROM_SB_REVISION_5:
10865 size = NVRAM_SELFBOOT_FORMAT1_5_SIZE;
10866 break;
10867 case TG3_EEPROM_SB_REVISION_6:
10868 size = NVRAM_SELFBOOT_FORMAT1_6_SIZE;
10869 break;
Matt Carlsona5767de2007-11-12 21:10:58 -080010870 default:
Matt Carlson727a6d92011-06-13 13:38:58 +000010871 return -EIO;
Matt Carlsona5767de2007-11-12 21:10:58 -080010872 }
10873 } else
Michael Chan1b277772006-03-20 22:27:48 -080010874 return 0;
Michael Chanb16250e2006-09-27 16:10:14 -070010875 } else if ((magic & TG3_EEPROM_MAGIC_HW_MSK) == TG3_EEPROM_MAGIC_HW)
10876 size = NVRAM_SELFBOOT_HW_SIZE;
10877 else
Michael Chan1b277772006-03-20 22:27:48 -080010878 return -EIO;
10879
10880 buf = kmalloc(size, GFP_KERNEL);
Michael Chan566f86a2005-05-29 14:56:58 -070010881 if (buf == NULL)
10882 return -ENOMEM;
10883
Michael Chan1b277772006-03-20 22:27:48 -080010884 err = -EIO;
10885 for (i = 0, j = 0; i < size; i += 4, j++) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000010886 err = tg3_nvram_read_be32(tp, i, &buf[j]);
10887 if (err)
Michael Chan566f86a2005-05-29 14:56:58 -070010888 break;
Michael Chan566f86a2005-05-29 14:56:58 -070010889 }
Michael Chan1b277772006-03-20 22:27:48 -080010890 if (i < size)
Michael Chan566f86a2005-05-29 14:56:58 -070010891 goto out;
10892
Michael Chan1b277772006-03-20 22:27:48 -080010893 /* Selfboot format */
Matt Carlsona9dc5292009-02-25 14:25:30 +000010894 magic = be32_to_cpu(buf[0]);
Al Virob9fc7dc2007-12-17 22:59:57 -080010895 if ((magic & TG3_EEPROM_MAGIC_FW_MSK) ==
Michael Chanb16250e2006-09-27 16:10:14 -070010896 TG3_EEPROM_MAGIC_FW) {
Michael Chan1b277772006-03-20 22:27:48 -080010897 u8 *buf8 = (u8 *) buf, csum8 = 0;
10898
Al Virob9fc7dc2007-12-17 22:59:57 -080010899 if ((magic & TG3_EEPROM_SB_REVISION_MASK) ==
Matt Carlsona5767de2007-11-12 21:10:58 -080010900 TG3_EEPROM_SB_REVISION_2) {
10901 /* For rev 2, the csum doesn't include the MBA. */
10902 for (i = 0; i < TG3_EEPROM_SB_F1R2_MBA_OFF; i++)
10903 csum8 += buf8[i];
10904 for (i = TG3_EEPROM_SB_F1R2_MBA_OFF + 4; i < size; i++)
10905 csum8 += buf8[i];
10906 } else {
10907 for (i = 0; i < size; i++)
10908 csum8 += buf8[i];
10909 }
Michael Chan1b277772006-03-20 22:27:48 -080010910
Adrian Bunkad96b482006-04-05 22:21:04 -070010911 if (csum8 == 0) {
10912 err = 0;
10913 goto out;
10914 }
10915
10916 err = -EIO;
10917 goto out;
Michael Chan1b277772006-03-20 22:27:48 -080010918 }
Michael Chan566f86a2005-05-29 14:56:58 -070010919
Al Virob9fc7dc2007-12-17 22:59:57 -080010920 if ((magic & TG3_EEPROM_MAGIC_HW_MSK) ==
Michael Chanb16250e2006-09-27 16:10:14 -070010921 TG3_EEPROM_MAGIC_HW) {
10922 u8 data[NVRAM_SELFBOOT_DATA_SIZE];
Matt Carlsona9dc5292009-02-25 14:25:30 +000010923 u8 parity[NVRAM_SELFBOOT_DATA_SIZE];
Michael Chanb16250e2006-09-27 16:10:14 -070010924 u8 *buf8 = (u8 *) buf;
Michael Chanb16250e2006-09-27 16:10:14 -070010925
10926 /* Separate the parity bits and the data bytes. */
10927 for (i = 0, j = 0, k = 0; i < NVRAM_SELFBOOT_HW_SIZE; i++) {
10928 if ((i == 0) || (i == 8)) {
10929 int l;
10930 u8 msk;
10931
10932 for (l = 0, msk = 0x80; l < 7; l++, msk >>= 1)
10933 parity[k++] = buf8[i] & msk;
10934 i++;
Matt Carlson859a5882010-04-05 10:19:28 +000010935 } else if (i == 16) {
Michael Chanb16250e2006-09-27 16:10:14 -070010936 int l;
10937 u8 msk;
10938
10939 for (l = 0, msk = 0x20; l < 6; l++, msk >>= 1)
10940 parity[k++] = buf8[i] & msk;
10941 i++;
10942
10943 for (l = 0, msk = 0x80; l < 8; l++, msk >>= 1)
10944 parity[k++] = buf8[i] & msk;
10945 i++;
10946 }
10947 data[j++] = buf8[i];
10948 }
10949
10950 err = -EIO;
10951 for (i = 0; i < NVRAM_SELFBOOT_DATA_SIZE; i++) {
10952 u8 hw8 = hweight8(data[i]);
10953
10954 if ((hw8 & 0x1) && parity[i])
10955 goto out;
10956 else if (!(hw8 & 0x1) && !parity[i])
10957 goto out;
10958 }
10959 err = 0;
10960 goto out;
10961 }
10962
Matt Carlson01c3a392011-03-09 16:58:20 +000010963 err = -EIO;
10964
Michael Chan566f86a2005-05-29 14:56:58 -070010965 /* Bootstrap checksum at offset 0x10 */
10966 csum = calc_crc((unsigned char *) buf, 0x10);
Matt Carlson01c3a392011-03-09 16:58:20 +000010967 if (csum != le32_to_cpu(buf[0x10/4]))
Michael Chan566f86a2005-05-29 14:56:58 -070010968 goto out;
10969
10970 /* Manufacturing block starts at offset 0x74, checksum at 0xfc */
10971 csum = calc_crc((unsigned char *) &buf[0x74/4], 0x88);
Matt Carlson01c3a392011-03-09 16:58:20 +000010972 if (csum != le32_to_cpu(buf[0xfc/4]))
Matt Carlsona9dc5292009-02-25 14:25:30 +000010973 goto out;
Michael Chan566f86a2005-05-29 14:56:58 -070010974
Matt Carlsonc3e94502011-04-13 11:05:08 +000010975 kfree(buf);
10976
Matt Carlson535a4902011-07-20 10:20:56 +000010977 buf = tg3_vpd_readblock(tp, &len);
Matt Carlsonc3e94502011-04-13 11:05:08 +000010978 if (!buf)
10979 return -ENOMEM;
Matt Carlsond4894f32011-03-09 16:58:21 +000010980
Matt Carlson535a4902011-07-20 10:20:56 +000010981 i = pci_vpd_find_tag((u8 *)buf, 0, len, PCI_VPD_LRDT_RO_DATA);
Matt Carlsond4894f32011-03-09 16:58:21 +000010982 if (i > 0) {
10983 j = pci_vpd_lrdt_size(&((u8 *)buf)[i]);
10984 if (j < 0)
10985 goto out;
10986
Matt Carlson535a4902011-07-20 10:20:56 +000010987 if (i + PCI_VPD_LRDT_TAG_SIZE + j > len)
Matt Carlsond4894f32011-03-09 16:58:21 +000010988 goto out;
10989
10990 i += PCI_VPD_LRDT_TAG_SIZE;
10991 j = pci_vpd_find_info_keyword((u8 *)buf, i, j,
10992 PCI_VPD_RO_KEYWORD_CHKSUM);
10993 if (j > 0) {
10994 u8 csum8 = 0;
10995
10996 j += PCI_VPD_INFO_FLD_HDR_SIZE;
10997
10998 for (i = 0; i <= j; i++)
10999 csum8 += ((u8 *)buf)[i];
11000
11001 if (csum8)
11002 goto out;
11003 }
11004 }
11005
Michael Chan566f86a2005-05-29 14:56:58 -070011006 err = 0;
11007
11008out:
11009 kfree(buf);
11010 return err;
11011}
11012
Michael Chanca430072005-05-29 14:57:23 -070011013#define TG3_SERDES_TIMEOUT_SEC 2
11014#define TG3_COPPER_TIMEOUT_SEC 6
11015
11016static int tg3_test_link(struct tg3 *tp)
11017{
11018 int i, max;
11019
11020 if (!netif_running(tp->dev))
11021 return -ENODEV;
11022
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011023 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
Michael Chanca430072005-05-29 14:57:23 -070011024 max = TG3_SERDES_TIMEOUT_SEC;
11025 else
11026 max = TG3_COPPER_TIMEOUT_SEC;
11027
11028 for (i = 0; i < max; i++) {
11029 if (netif_carrier_ok(tp->dev))
11030 return 0;
11031
11032 if (msleep_interruptible(1000))
11033 break;
11034 }
11035
11036 return -EIO;
11037}
11038
Michael Chana71116d2005-05-29 14:58:11 -070011039/* Only test the commonly used registers */
David S. Miller30ca3e32006-03-20 23:02:36 -080011040static int tg3_test_registers(struct tg3 *tp)
Michael Chana71116d2005-05-29 14:58:11 -070011041{
Michael Chanb16250e2006-09-27 16:10:14 -070011042 int i, is_5705, is_5750;
Michael Chana71116d2005-05-29 14:58:11 -070011043 u32 offset, read_mask, write_mask, val, save_val, read_val;
11044 static struct {
11045 u16 offset;
11046 u16 flags;
11047#define TG3_FL_5705 0x1
11048#define TG3_FL_NOT_5705 0x2
11049#define TG3_FL_NOT_5788 0x4
Michael Chanb16250e2006-09-27 16:10:14 -070011050#define TG3_FL_NOT_5750 0x8
Michael Chana71116d2005-05-29 14:58:11 -070011051 u32 read_mask;
11052 u32 write_mask;
11053 } reg_tbl[] = {
11054 /* MAC Control Registers */
11055 { MAC_MODE, TG3_FL_NOT_5705,
11056 0x00000000, 0x00ef6f8c },
11057 { MAC_MODE, TG3_FL_5705,
11058 0x00000000, 0x01ef6b8c },
11059 { MAC_STATUS, TG3_FL_NOT_5705,
11060 0x03800107, 0x00000000 },
11061 { MAC_STATUS, TG3_FL_5705,
11062 0x03800100, 0x00000000 },
11063 { MAC_ADDR_0_HIGH, 0x0000,
11064 0x00000000, 0x0000ffff },
11065 { MAC_ADDR_0_LOW, 0x0000,
Matt Carlsonc6cdf432010-04-05 10:19:26 +000011066 0x00000000, 0xffffffff },
Michael Chana71116d2005-05-29 14:58:11 -070011067 { MAC_RX_MTU_SIZE, 0x0000,
11068 0x00000000, 0x0000ffff },
11069 { MAC_TX_MODE, 0x0000,
11070 0x00000000, 0x00000070 },
11071 { MAC_TX_LENGTHS, 0x0000,
11072 0x00000000, 0x00003fff },
11073 { MAC_RX_MODE, TG3_FL_NOT_5705,
11074 0x00000000, 0x000007fc },
11075 { MAC_RX_MODE, TG3_FL_5705,
11076 0x00000000, 0x000007dc },
11077 { MAC_HASH_REG_0, 0x0000,
11078 0x00000000, 0xffffffff },
11079 { MAC_HASH_REG_1, 0x0000,
11080 0x00000000, 0xffffffff },
11081 { MAC_HASH_REG_2, 0x0000,
11082 0x00000000, 0xffffffff },
11083 { MAC_HASH_REG_3, 0x0000,
11084 0x00000000, 0xffffffff },
11085
11086 /* Receive Data and Receive BD Initiator Control Registers. */
11087 { RCVDBDI_JUMBO_BD+0, TG3_FL_NOT_5705,
11088 0x00000000, 0xffffffff },
11089 { RCVDBDI_JUMBO_BD+4, TG3_FL_NOT_5705,
11090 0x00000000, 0xffffffff },
11091 { RCVDBDI_JUMBO_BD+8, TG3_FL_NOT_5705,
11092 0x00000000, 0x00000003 },
11093 { RCVDBDI_JUMBO_BD+0xc, TG3_FL_NOT_5705,
11094 0x00000000, 0xffffffff },
11095 { RCVDBDI_STD_BD+0, 0x0000,
11096 0x00000000, 0xffffffff },
11097 { RCVDBDI_STD_BD+4, 0x0000,
11098 0x00000000, 0xffffffff },
11099 { RCVDBDI_STD_BD+8, 0x0000,
11100 0x00000000, 0xffff0002 },
11101 { RCVDBDI_STD_BD+0xc, 0x0000,
11102 0x00000000, 0xffffffff },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011103
Michael Chana71116d2005-05-29 14:58:11 -070011104 /* Receive BD Initiator Control Registers. */
11105 { RCVBDI_STD_THRESH, TG3_FL_NOT_5705,
11106 0x00000000, 0xffffffff },
11107 { RCVBDI_STD_THRESH, TG3_FL_5705,
11108 0x00000000, 0x000003ff },
11109 { RCVBDI_JUMBO_THRESH, TG3_FL_NOT_5705,
11110 0x00000000, 0xffffffff },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011111
Michael Chana71116d2005-05-29 14:58:11 -070011112 /* Host Coalescing Control Registers. */
11113 { HOSTCC_MODE, TG3_FL_NOT_5705,
11114 0x00000000, 0x00000004 },
11115 { HOSTCC_MODE, TG3_FL_5705,
11116 0x00000000, 0x000000f6 },
11117 { HOSTCC_RXCOL_TICKS, TG3_FL_NOT_5705,
11118 0x00000000, 0xffffffff },
11119 { HOSTCC_RXCOL_TICKS, TG3_FL_5705,
11120 0x00000000, 0x000003ff },
11121 { HOSTCC_TXCOL_TICKS, TG3_FL_NOT_5705,
11122 0x00000000, 0xffffffff },
11123 { HOSTCC_TXCOL_TICKS, TG3_FL_5705,
11124 0x00000000, 0x000003ff },
11125 { HOSTCC_RXMAX_FRAMES, TG3_FL_NOT_5705,
11126 0x00000000, 0xffffffff },
11127 { HOSTCC_RXMAX_FRAMES, TG3_FL_5705 | TG3_FL_NOT_5788,
11128 0x00000000, 0x000000ff },
11129 { HOSTCC_TXMAX_FRAMES, TG3_FL_NOT_5705,
11130 0x00000000, 0xffffffff },
11131 { HOSTCC_TXMAX_FRAMES, TG3_FL_5705 | TG3_FL_NOT_5788,
11132 0x00000000, 0x000000ff },
11133 { HOSTCC_RXCOAL_TICK_INT, TG3_FL_NOT_5705,
11134 0x00000000, 0xffffffff },
11135 { HOSTCC_TXCOAL_TICK_INT, TG3_FL_NOT_5705,
11136 0x00000000, 0xffffffff },
11137 { HOSTCC_RXCOAL_MAXF_INT, TG3_FL_NOT_5705,
11138 0x00000000, 0xffffffff },
11139 { HOSTCC_RXCOAL_MAXF_INT, TG3_FL_5705 | TG3_FL_NOT_5788,
11140 0x00000000, 0x000000ff },
11141 { HOSTCC_TXCOAL_MAXF_INT, TG3_FL_NOT_5705,
11142 0x00000000, 0xffffffff },
11143 { HOSTCC_TXCOAL_MAXF_INT, TG3_FL_5705 | TG3_FL_NOT_5788,
11144 0x00000000, 0x000000ff },
11145 { HOSTCC_STAT_COAL_TICKS, TG3_FL_NOT_5705,
11146 0x00000000, 0xffffffff },
11147 { HOSTCC_STATS_BLK_HOST_ADDR, TG3_FL_NOT_5705,
11148 0x00000000, 0xffffffff },
11149 { HOSTCC_STATS_BLK_HOST_ADDR+4, TG3_FL_NOT_5705,
11150 0x00000000, 0xffffffff },
11151 { HOSTCC_STATUS_BLK_HOST_ADDR, 0x0000,
11152 0x00000000, 0xffffffff },
11153 { HOSTCC_STATUS_BLK_HOST_ADDR+4, 0x0000,
11154 0x00000000, 0xffffffff },
11155 { HOSTCC_STATS_BLK_NIC_ADDR, 0x0000,
11156 0xffffffff, 0x00000000 },
11157 { HOSTCC_STATUS_BLK_NIC_ADDR, 0x0000,
11158 0xffffffff, 0x00000000 },
11159
11160 /* Buffer Manager Control Registers. */
Michael Chanb16250e2006-09-27 16:10:14 -070011161 { BUFMGR_MB_POOL_ADDR, TG3_FL_NOT_5750,
Michael Chana71116d2005-05-29 14:58:11 -070011162 0x00000000, 0x007fff80 },
Michael Chanb16250e2006-09-27 16:10:14 -070011163 { BUFMGR_MB_POOL_SIZE, TG3_FL_NOT_5750,
Michael Chana71116d2005-05-29 14:58:11 -070011164 0x00000000, 0x007fffff },
11165 { BUFMGR_MB_RDMA_LOW_WATER, 0x0000,
11166 0x00000000, 0x0000003f },
11167 { BUFMGR_MB_MACRX_LOW_WATER, 0x0000,
11168 0x00000000, 0x000001ff },
11169 { BUFMGR_MB_HIGH_WATER, 0x0000,
11170 0x00000000, 0x000001ff },
11171 { BUFMGR_DMA_DESC_POOL_ADDR, TG3_FL_NOT_5705,
11172 0xffffffff, 0x00000000 },
11173 { BUFMGR_DMA_DESC_POOL_SIZE, TG3_FL_NOT_5705,
11174 0xffffffff, 0x00000000 },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011175
Michael Chana71116d2005-05-29 14:58:11 -070011176 /* Mailbox Registers */
11177 { GRCMBOX_RCVSTD_PROD_IDX+4, 0x0000,
11178 0x00000000, 0x000001ff },
11179 { GRCMBOX_RCVJUMBO_PROD_IDX+4, TG3_FL_NOT_5705,
11180 0x00000000, 0x000001ff },
11181 { GRCMBOX_RCVRET_CON_IDX_0+4, 0x0000,
11182 0x00000000, 0x000007ff },
11183 { GRCMBOX_SNDHOST_PROD_IDX_0+4, 0x0000,
11184 0x00000000, 0x000001ff },
11185
11186 { 0xffff, 0x0000, 0x00000000, 0x00000000 },
11187 };
11188
Michael Chanb16250e2006-09-27 16:10:14 -070011189 is_5705 = is_5750 = 0;
Joe Perches63c3a662011-04-26 08:12:10 +000011190 if (tg3_flag(tp, 5705_PLUS)) {
Michael Chana71116d2005-05-29 14:58:11 -070011191 is_5705 = 1;
Joe Perches63c3a662011-04-26 08:12:10 +000011192 if (tg3_flag(tp, 5750_PLUS))
Michael Chanb16250e2006-09-27 16:10:14 -070011193 is_5750 = 1;
11194 }
Michael Chana71116d2005-05-29 14:58:11 -070011195
11196 for (i = 0; reg_tbl[i].offset != 0xffff; i++) {
11197 if (is_5705 && (reg_tbl[i].flags & TG3_FL_NOT_5705))
11198 continue;
11199
11200 if (!is_5705 && (reg_tbl[i].flags & TG3_FL_5705))
11201 continue;
11202
Joe Perches63c3a662011-04-26 08:12:10 +000011203 if (tg3_flag(tp, IS_5788) &&
Michael Chana71116d2005-05-29 14:58:11 -070011204 (reg_tbl[i].flags & TG3_FL_NOT_5788))
11205 continue;
11206
Michael Chanb16250e2006-09-27 16:10:14 -070011207 if (is_5750 && (reg_tbl[i].flags & TG3_FL_NOT_5750))
11208 continue;
11209
Michael Chana71116d2005-05-29 14:58:11 -070011210 offset = (u32) reg_tbl[i].offset;
11211 read_mask = reg_tbl[i].read_mask;
11212 write_mask = reg_tbl[i].write_mask;
11213
11214 /* Save the original register content */
11215 save_val = tr32(offset);
11216
11217 /* Determine the read-only value. */
11218 read_val = save_val & read_mask;
11219
11220 /* Write zero to the register, then make sure the read-only bits
11221 * are not changed and the read/write bits are all zeros.
11222 */
11223 tw32(offset, 0);
11224
11225 val = tr32(offset);
11226
11227 /* Test the read-only and read/write bits. */
11228 if (((val & read_mask) != read_val) || (val & write_mask))
11229 goto out;
11230
11231 /* Write ones to all the bits defined by RdMask and WrMask, then
11232 * make sure the read-only bits are not changed and the
11233 * read/write bits are all ones.
11234 */
11235 tw32(offset, read_mask | write_mask);
11236
11237 val = tr32(offset);
11238
11239 /* Test the read-only bits. */
11240 if ((val & read_mask) != read_val)
11241 goto out;
11242
11243 /* Test the read/write bits. */
11244 if ((val & write_mask) != write_mask)
11245 goto out;
11246
11247 tw32(offset, save_val);
11248 }
11249
11250 return 0;
11251
11252out:
Michael Chan9f88f292006-12-07 00:22:54 -080011253 if (netif_msg_hw(tp))
Matt Carlson2445e462010-04-05 10:19:21 +000011254 netdev_err(tp->dev,
11255 "Register test failed at offset %x\n", offset);
Michael Chana71116d2005-05-29 14:58:11 -070011256 tw32(offset, save_val);
11257 return -EIO;
11258}
11259
Michael Chan7942e1d2005-05-29 14:58:36 -070011260static int tg3_do_mem_test(struct tg3 *tp, u32 offset, u32 len)
11261{
Arjan van de Venf71e1302006-03-03 21:33:57 -050011262 static const u32 test_pattern[] = { 0x00000000, 0xffffffff, 0xaa55a55a };
Michael Chan7942e1d2005-05-29 14:58:36 -070011263 int i;
11264 u32 j;
11265
Alejandro Martinez Ruize9edda62007-10-15 03:37:43 +020011266 for (i = 0; i < ARRAY_SIZE(test_pattern); i++) {
Michael Chan7942e1d2005-05-29 14:58:36 -070011267 for (j = 0; j < len; j += 4) {
11268 u32 val;
11269
11270 tg3_write_mem(tp, offset + j, test_pattern[i]);
11271 tg3_read_mem(tp, offset + j, &val);
11272 if (val != test_pattern[i])
11273 return -EIO;
11274 }
11275 }
11276 return 0;
11277}
11278
11279static int tg3_test_memory(struct tg3 *tp)
11280{
11281 static struct mem_entry {
11282 u32 offset;
11283 u32 len;
11284 } mem_tbl_570x[] = {
Michael Chan38690192005-12-19 16:27:28 -080011285 { 0x00000000, 0x00b50},
Michael Chan7942e1d2005-05-29 14:58:36 -070011286 { 0x00002000, 0x1c000},
11287 { 0xffffffff, 0x00000}
11288 }, mem_tbl_5705[] = {
11289 { 0x00000100, 0x0000c},
11290 { 0x00000200, 0x00008},
Michael Chan7942e1d2005-05-29 14:58:36 -070011291 { 0x00004000, 0x00800},
11292 { 0x00006000, 0x01000},
11293 { 0x00008000, 0x02000},
11294 { 0x00010000, 0x0e000},
11295 { 0xffffffff, 0x00000}
Michael Chan79f4d132006-03-20 22:28:57 -080011296 }, mem_tbl_5755[] = {
11297 { 0x00000200, 0x00008},
11298 { 0x00004000, 0x00800},
11299 { 0x00006000, 0x00800},
11300 { 0x00008000, 0x02000},
11301 { 0x00010000, 0x0c000},
11302 { 0xffffffff, 0x00000}
Michael Chanb16250e2006-09-27 16:10:14 -070011303 }, mem_tbl_5906[] = {
11304 { 0x00000200, 0x00008},
11305 { 0x00004000, 0x00400},
11306 { 0x00006000, 0x00400},
11307 { 0x00008000, 0x01000},
11308 { 0x00010000, 0x01000},
11309 { 0xffffffff, 0x00000}
Matt Carlson8b5a6c42010-01-20 16:58:06 +000011310 }, mem_tbl_5717[] = {
11311 { 0x00000200, 0x00008},
11312 { 0x00010000, 0x0a000},
11313 { 0x00020000, 0x13c00},
11314 { 0xffffffff, 0x00000}
11315 }, mem_tbl_57765[] = {
11316 { 0x00000200, 0x00008},
11317 { 0x00004000, 0x00800},
11318 { 0x00006000, 0x09800},
11319 { 0x00010000, 0x0a000},
11320 { 0xffffffff, 0x00000}
Michael Chan7942e1d2005-05-29 14:58:36 -070011321 };
11322 struct mem_entry *mem_tbl;
11323 int err = 0;
11324 int i;
11325
Joe Perches63c3a662011-04-26 08:12:10 +000011326 if (tg3_flag(tp, 5717_PLUS))
Matt Carlson8b5a6c42010-01-20 16:58:06 +000011327 mem_tbl = mem_tbl_5717;
11328 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
11329 mem_tbl = mem_tbl_57765;
Joe Perches63c3a662011-04-26 08:12:10 +000011330 else if (tg3_flag(tp, 5755_PLUS))
Matt Carlson321d32a2008-11-21 17:22:19 -080011331 mem_tbl = mem_tbl_5755;
11332 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
11333 mem_tbl = mem_tbl_5906;
Joe Perches63c3a662011-04-26 08:12:10 +000011334 else if (tg3_flag(tp, 5705_PLUS))
Matt Carlson321d32a2008-11-21 17:22:19 -080011335 mem_tbl = mem_tbl_5705;
11336 else
Michael Chan7942e1d2005-05-29 14:58:36 -070011337 mem_tbl = mem_tbl_570x;
11338
11339 for (i = 0; mem_tbl[i].offset != 0xffffffff; i++) {
Matt Carlsonbe98da62010-07-11 09:31:46 +000011340 err = tg3_do_mem_test(tp, mem_tbl[i].offset, mem_tbl[i].len);
11341 if (err)
Michael Chan7942e1d2005-05-29 14:58:36 -070011342 break;
11343 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011344
Michael Chan7942e1d2005-05-29 14:58:36 -070011345 return err;
11346}
11347
Matt Carlsonbb158d62011-04-25 12:42:47 +000011348#define TG3_TSO_MSS 500
11349
11350#define TG3_TSO_IP_HDR_LEN 20
11351#define TG3_TSO_TCP_HDR_LEN 20
11352#define TG3_TSO_TCP_OPT_LEN 12
11353
11354static const u8 tg3_tso_header[] = {
113550x08, 0x00,
113560x45, 0x00, 0x00, 0x00,
113570x00, 0x00, 0x40, 0x00,
113580x40, 0x06, 0x00, 0x00,
113590x0a, 0x00, 0x00, 0x01,
113600x0a, 0x00, 0x00, 0x02,
113610x0d, 0x00, 0xe0, 0x00,
113620x00, 0x00, 0x01, 0x00,
113630x00, 0x00, 0x02, 0x00,
113640x80, 0x10, 0x10, 0x00,
113650x14, 0x09, 0x00, 0x00,
113660x01, 0x01, 0x08, 0x0a,
113670x11, 0x11, 0x11, 0x11,
113680x11, 0x11, 0x11, 0x11,
11369};
Michael Chan9f40dea2005-09-05 17:53:06 -070011370
Matt Carlson28a45952011-08-19 13:58:22 +000011371static int tg3_run_loopback(struct tg3 *tp, u32 pktsz, bool tso_loopback)
Michael Chanc76949a2005-05-29 14:58:59 -070011372{
Matt Carlson5e5a7f32011-08-19 13:58:21 +000011373 u32 rx_start_idx, rx_idx, tx_idx, opaque_key;
Matt Carlsonbb158d62011-04-25 12:42:47 +000011374 u32 base_flags = 0, mss = 0, desc_idx, coal_now, data_off, val;
Matt Carlson84b67b22011-07-27 14:20:52 +000011375 u32 budget;
Eric Dumazet9205fd92011-11-18 06:47:01 +000011376 struct sk_buff *skb;
11377 u8 *tx_data, *rx_data;
Michael Chanc76949a2005-05-29 14:58:59 -070011378 dma_addr_t map;
11379 int num_pkts, tx_len, rx_len, i, err;
11380 struct tg3_rx_buffer_desc *desc;
Matt Carlson898a56f2009-08-28 14:02:40 +000011381 struct tg3_napi *tnapi, *rnapi;
Matt Carlson8fea32b2010-09-15 08:59:58 +000011382 struct tg3_rx_prodring_set *tpr = &tp->napi[0].prodring;
Michael Chanc76949a2005-05-29 14:58:59 -070011383
Matt Carlsonc8873402010-02-12 14:47:11 +000011384 tnapi = &tp->napi[0];
11385 rnapi = &tp->napi[0];
Matt Carlson0c1d0e22009-09-01 13:16:33 +000011386 if (tp->irq_cnt > 1) {
Joe Perches63c3a662011-04-26 08:12:10 +000011387 if (tg3_flag(tp, ENABLE_RSS))
Matt Carlson1da85aa2010-09-30 10:34:34 +000011388 rnapi = &tp->napi[1];
Joe Perches63c3a662011-04-26 08:12:10 +000011389 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlsonc8873402010-02-12 14:47:11 +000011390 tnapi = &tp->napi[1];
Matt Carlson0c1d0e22009-09-01 13:16:33 +000011391 }
Matt Carlsonfd2ce372009-09-01 12:51:13 +000011392 coal_now = tnapi->coal_now | rnapi->coal_now;
Matt Carlson898a56f2009-08-28 14:02:40 +000011393
Michael Chanc76949a2005-05-29 14:58:59 -070011394 err = -EIO;
11395
Matt Carlson4852a862011-04-13 11:05:07 +000011396 tx_len = pktsz;
David S. Millera20e9c62006-07-31 22:38:16 -070011397 skb = netdev_alloc_skb(tp->dev, tx_len);
Jesper Juhla50bb7b2006-05-09 23:14:35 -070011398 if (!skb)
11399 return -ENOMEM;
11400
Michael Chanc76949a2005-05-29 14:58:59 -070011401 tx_data = skb_put(skb, tx_len);
11402 memcpy(tx_data, tp->dev->dev_addr, 6);
11403 memset(tx_data + 6, 0x0, 8);
11404
Matt Carlson4852a862011-04-13 11:05:07 +000011405 tw32(MAC_RX_MTU_SIZE, tx_len + ETH_FCS_LEN);
Michael Chanc76949a2005-05-29 14:58:59 -070011406
Matt Carlson28a45952011-08-19 13:58:22 +000011407 if (tso_loopback) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000011408 struct iphdr *iph = (struct iphdr *)&tx_data[ETH_HLEN];
11409
11410 u32 hdr_len = TG3_TSO_IP_HDR_LEN + TG3_TSO_TCP_HDR_LEN +
11411 TG3_TSO_TCP_OPT_LEN;
11412
11413 memcpy(tx_data + ETH_ALEN * 2, tg3_tso_header,
11414 sizeof(tg3_tso_header));
11415 mss = TG3_TSO_MSS;
11416
11417 val = tx_len - ETH_ALEN * 2 - sizeof(tg3_tso_header);
11418 num_pkts = DIV_ROUND_UP(val, TG3_TSO_MSS);
11419
11420 /* Set the total length field in the IP header */
11421 iph->tot_len = htons((u16)(mss + hdr_len));
11422
11423 base_flags = (TXD_FLAG_CPU_PRE_DMA |
11424 TXD_FLAG_CPU_POST_DMA);
11425
Joe Perches63c3a662011-04-26 08:12:10 +000011426 if (tg3_flag(tp, HW_TSO_1) ||
11427 tg3_flag(tp, HW_TSO_2) ||
11428 tg3_flag(tp, HW_TSO_3)) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000011429 struct tcphdr *th;
11430 val = ETH_HLEN + TG3_TSO_IP_HDR_LEN;
11431 th = (struct tcphdr *)&tx_data[val];
11432 th->check = 0;
11433 } else
11434 base_flags |= TXD_FLAG_TCPUDP_CSUM;
11435
Joe Perches63c3a662011-04-26 08:12:10 +000011436 if (tg3_flag(tp, HW_TSO_3)) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000011437 mss |= (hdr_len & 0xc) << 12;
11438 if (hdr_len & 0x10)
11439 base_flags |= 0x00000010;
11440 base_flags |= (hdr_len & 0x3e0) << 5;
Joe Perches63c3a662011-04-26 08:12:10 +000011441 } else if (tg3_flag(tp, HW_TSO_2))
Matt Carlsonbb158d62011-04-25 12:42:47 +000011442 mss |= hdr_len << 9;
Joe Perches63c3a662011-04-26 08:12:10 +000011443 else if (tg3_flag(tp, HW_TSO_1) ||
Matt Carlsonbb158d62011-04-25 12:42:47 +000011444 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
11445 mss |= (TG3_TSO_TCP_OPT_LEN << 9);
11446 } else {
11447 base_flags |= (TG3_TSO_TCP_OPT_LEN << 10);
11448 }
11449
11450 data_off = ETH_ALEN * 2 + sizeof(tg3_tso_header);
11451 } else {
11452 num_pkts = 1;
11453 data_off = ETH_HLEN;
11454 }
11455
11456 for (i = data_off; i < tx_len; i++)
Michael Chanc76949a2005-05-29 14:58:59 -070011457 tx_data[i] = (u8) (i & 0xff);
11458
Alexander Duyckf4188d82009-12-02 16:48:38 +000011459 map = pci_map_single(tp->pdev, skb->data, tx_len, PCI_DMA_TODEVICE);
11460 if (pci_dma_mapping_error(tp->pdev, map)) {
Matt Carlsona21771d2009-11-02 14:25:31 +000011461 dev_kfree_skb(skb);
11462 return -EIO;
11463 }
Michael Chanc76949a2005-05-29 14:58:59 -070011464
Matt Carlson0d681b22011-07-27 14:20:49 +000011465 val = tnapi->tx_prod;
11466 tnapi->tx_buffers[val].skb = skb;
11467 dma_unmap_addr_set(&tnapi->tx_buffers[val], mapping, map);
11468
Michael Chanc76949a2005-05-29 14:58:59 -070011469 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +000011470 rnapi->coal_now);
Michael Chanc76949a2005-05-29 14:58:59 -070011471
11472 udelay(10);
11473
Matt Carlson898a56f2009-08-28 14:02:40 +000011474 rx_start_idx = rnapi->hw_status->idx[0].rx_producer;
Michael Chanc76949a2005-05-29 14:58:59 -070011475
Matt Carlson84b67b22011-07-27 14:20:52 +000011476 budget = tg3_tx_avail(tnapi);
11477 if (tg3_tx_frag_set(tnapi, &val, &budget, map, tx_len,
Matt Carlsond1a3b732011-07-27 14:20:51 +000011478 base_flags | TXD_FLAG_END, mss, 0)) {
11479 tnapi->tx_buffers[val].skb = NULL;
11480 dev_kfree_skb(skb);
11481 return -EIO;
11482 }
Michael Chanc76949a2005-05-29 14:58:59 -070011483
Matt Carlsonf3f3f272009-08-28 14:03:21 +000011484 tnapi->tx_prod++;
Michael Chanc76949a2005-05-29 14:58:59 -070011485
Matt Carlsonf3f3f272009-08-28 14:03:21 +000011486 tw32_tx_mbox(tnapi->prodmbox, tnapi->tx_prod);
11487 tr32_mailbox(tnapi->prodmbox);
Michael Chanc76949a2005-05-29 14:58:59 -070011488
11489 udelay(10);
11490
Matt Carlson303fc922009-11-02 14:27:34 +000011491 /* 350 usec to allow enough time on some 10/100 Mbps devices. */
11492 for (i = 0; i < 35; i++) {
Michael Chanc76949a2005-05-29 14:58:59 -070011493 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +000011494 coal_now);
Michael Chanc76949a2005-05-29 14:58:59 -070011495
11496 udelay(10);
11497
Matt Carlson898a56f2009-08-28 14:02:40 +000011498 tx_idx = tnapi->hw_status->idx[0].tx_consumer;
11499 rx_idx = rnapi->hw_status->idx[0].rx_producer;
Matt Carlsonf3f3f272009-08-28 14:03:21 +000011500 if ((tx_idx == tnapi->tx_prod) &&
Michael Chanc76949a2005-05-29 14:58:59 -070011501 (rx_idx == (rx_start_idx + num_pkts)))
11502 break;
11503 }
11504
Matt Carlsonba1142e2011-11-04 09:15:00 +000011505 tg3_tx_skb_unmap(tnapi, tnapi->tx_prod - 1, -1);
Michael Chanc76949a2005-05-29 14:58:59 -070011506 dev_kfree_skb(skb);
11507
Matt Carlsonf3f3f272009-08-28 14:03:21 +000011508 if (tx_idx != tnapi->tx_prod)
Michael Chanc76949a2005-05-29 14:58:59 -070011509 goto out;
11510
11511 if (rx_idx != rx_start_idx + num_pkts)
11512 goto out;
11513
Matt Carlsonbb158d62011-04-25 12:42:47 +000011514 val = data_off;
11515 while (rx_idx != rx_start_idx) {
11516 desc = &rnapi->rx_rcb[rx_start_idx++];
11517 desc_idx = desc->opaque & RXD_OPAQUE_INDEX_MASK;
11518 opaque_key = desc->opaque & RXD_OPAQUE_RING_MASK;
Michael Chanc76949a2005-05-29 14:58:59 -070011519
Matt Carlsonbb158d62011-04-25 12:42:47 +000011520 if ((desc->err_vlan & RXD_ERR_MASK) != 0 &&
11521 (desc->err_vlan != RXD_ERR_ODD_NIBBLE_RCVD_MII))
Matt Carlson4852a862011-04-13 11:05:07 +000011522 goto out;
Michael Chanc76949a2005-05-29 14:58:59 -070011523
Matt Carlsonbb158d62011-04-25 12:42:47 +000011524 rx_len = ((desc->idx_len & RXD_LEN_MASK) >> RXD_LEN_SHIFT)
11525 - ETH_FCS_LEN;
11526
Matt Carlson28a45952011-08-19 13:58:22 +000011527 if (!tso_loopback) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000011528 if (rx_len != tx_len)
11529 goto out;
11530
11531 if (pktsz <= TG3_RX_STD_DMA_SZ - ETH_FCS_LEN) {
11532 if (opaque_key != RXD_OPAQUE_RING_STD)
11533 goto out;
11534 } else {
11535 if (opaque_key != RXD_OPAQUE_RING_JUMBO)
11536 goto out;
11537 }
11538 } else if ((desc->type_flags & RXD_FLAG_TCPUDP_CSUM) &&
11539 (desc->ip_tcp_csum & RXD_TCPCSUM_MASK)
Matt Carlson54e0a672011-05-19 12:12:50 +000011540 >> RXD_TCPCSUM_SHIFT != 0xffff) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000011541 goto out;
11542 }
11543
11544 if (opaque_key == RXD_OPAQUE_RING_STD) {
Eric Dumazet9205fd92011-11-18 06:47:01 +000011545 rx_data = tpr->rx_std_buffers[desc_idx].data;
Matt Carlsonbb158d62011-04-25 12:42:47 +000011546 map = dma_unmap_addr(&tpr->rx_std_buffers[desc_idx],
11547 mapping);
11548 } else if (opaque_key == RXD_OPAQUE_RING_JUMBO) {
Eric Dumazet9205fd92011-11-18 06:47:01 +000011549 rx_data = tpr->rx_jmb_buffers[desc_idx].data;
Matt Carlsonbb158d62011-04-25 12:42:47 +000011550 map = dma_unmap_addr(&tpr->rx_jmb_buffers[desc_idx],
11551 mapping);
11552 } else
Matt Carlson4852a862011-04-13 11:05:07 +000011553 goto out;
11554
Matt Carlsonbb158d62011-04-25 12:42:47 +000011555 pci_dma_sync_single_for_cpu(tp->pdev, map, rx_len,
11556 PCI_DMA_FROMDEVICE);
11557
Eric Dumazet9205fd92011-11-18 06:47:01 +000011558 rx_data += TG3_RX_OFFSET(tp);
Matt Carlsonbb158d62011-04-25 12:42:47 +000011559 for (i = data_off; i < rx_len; i++, val++) {
Eric Dumazet9205fd92011-11-18 06:47:01 +000011560 if (*(rx_data + i) != (u8) (val & 0xff))
Matt Carlsonbb158d62011-04-25 12:42:47 +000011561 goto out;
11562 }
Matt Carlson4852a862011-04-13 11:05:07 +000011563 }
11564
Michael Chanc76949a2005-05-29 14:58:59 -070011565 err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011566
Eric Dumazet9205fd92011-11-18 06:47:01 +000011567 /* tg3_free_rings will unmap and free the rx_data */
Michael Chanc76949a2005-05-29 14:58:59 -070011568out:
11569 return err;
11570}
11571
Matt Carlson00c266b2011-04-25 12:42:46 +000011572#define TG3_STD_LOOPBACK_FAILED 1
11573#define TG3_JMB_LOOPBACK_FAILED 2
Matt Carlsonbb158d62011-04-25 12:42:47 +000011574#define TG3_TSO_LOOPBACK_FAILED 4
Matt Carlson28a45952011-08-19 13:58:22 +000011575#define TG3_LOOPBACK_FAILED \
11576 (TG3_STD_LOOPBACK_FAILED | \
11577 TG3_JMB_LOOPBACK_FAILED | \
11578 TG3_TSO_LOOPBACK_FAILED)
Matt Carlson00c266b2011-04-25 12:42:46 +000011579
Matt Carlson941ec902011-08-19 13:58:23 +000011580static int tg3_test_loopback(struct tg3 *tp, u64 *data, bool do_extlpbk)
Michael Chan9f40dea2005-09-05 17:53:06 -070011581{
Matt Carlson28a45952011-08-19 13:58:22 +000011582 int err = -EIO;
Matt Carlson2215e242011-08-19 13:58:19 +000011583 u32 eee_cap;
Michael Chan9f40dea2005-09-05 17:53:06 -070011584
Matt Carlsonab789042011-01-25 15:58:54 +000011585 eee_cap = tp->phy_flags & TG3_PHYFLG_EEE_CAP;
11586 tp->phy_flags &= ~TG3_PHYFLG_EEE_CAP;
11587
Matt Carlson28a45952011-08-19 13:58:22 +000011588 if (!netif_running(tp->dev)) {
11589 data[0] = TG3_LOOPBACK_FAILED;
11590 data[1] = TG3_LOOPBACK_FAILED;
Matt Carlson941ec902011-08-19 13:58:23 +000011591 if (do_extlpbk)
11592 data[2] = TG3_LOOPBACK_FAILED;
Matt Carlson28a45952011-08-19 13:58:22 +000011593 goto done;
11594 }
11595
Michael Chanb9ec6c12006-07-25 16:37:27 -070011596 err = tg3_reset_hw(tp, 1);
Matt Carlsonab789042011-01-25 15:58:54 +000011597 if (err) {
Matt Carlson28a45952011-08-19 13:58:22 +000011598 data[0] = TG3_LOOPBACK_FAILED;
11599 data[1] = TG3_LOOPBACK_FAILED;
Matt Carlson941ec902011-08-19 13:58:23 +000011600 if (do_extlpbk)
11601 data[2] = TG3_LOOPBACK_FAILED;
Matt Carlsonab789042011-01-25 15:58:54 +000011602 goto done;
11603 }
Michael Chan9f40dea2005-09-05 17:53:06 -070011604
Joe Perches63c3a662011-04-26 08:12:10 +000011605 if (tg3_flag(tp, ENABLE_RSS)) {
Matt Carlson4a85f092011-04-20 07:57:37 +000011606 int i;
11607
11608 /* Reroute all rx packets to the 1st queue */
11609 for (i = MAC_RSS_INDIR_TBL_0;
11610 i < MAC_RSS_INDIR_TBL_0 + TG3_RSS_INDIR_TBL_SIZE; i += 4)
11611 tw32(i, 0x0);
11612 }
11613
Matt Carlson6e01b202011-08-19 13:58:20 +000011614 /* HW errata - mac loopback fails in some cases on 5780.
11615 * Normal traffic and PHY loopback are not affected by
11616 * errata. Also, the MAC loopback test is deprecated for
11617 * all newer ASIC revisions.
11618 */
11619 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5780 &&
11620 !tg3_flag(tp, CPMU_PRESENT)) {
11621 tg3_mac_loopback(tp, true);
Matt Carlson9936bcf2007-10-10 18:03:07 -070011622
Matt Carlson28a45952011-08-19 13:58:22 +000011623 if (tg3_run_loopback(tp, ETH_FRAME_LEN, false))
11624 data[0] |= TG3_STD_LOOPBACK_FAILED;
Matt Carlson6e01b202011-08-19 13:58:20 +000011625
11626 if (tg3_flag(tp, JUMBO_RING_ENABLE) &&
Matt Carlson28a45952011-08-19 13:58:22 +000011627 tg3_run_loopback(tp, 9000 + ETH_HLEN, false))
11628 data[0] |= TG3_JMB_LOOPBACK_FAILED;
Matt Carlson6e01b202011-08-19 13:58:20 +000011629
11630 tg3_mac_loopback(tp, false);
11631 }
Matt Carlson4852a862011-04-13 11:05:07 +000011632
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011633 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
Joe Perches63c3a662011-04-26 08:12:10 +000011634 !tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson5e5a7f32011-08-19 13:58:21 +000011635 int i;
11636
Matt Carlson941ec902011-08-19 13:58:23 +000011637 tg3_phy_lpbk_set(tp, 0, false);
Matt Carlson5e5a7f32011-08-19 13:58:21 +000011638
11639 /* Wait for link */
11640 for (i = 0; i < 100; i++) {
11641 if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
11642 break;
11643 mdelay(1);
11644 }
11645
Matt Carlson28a45952011-08-19 13:58:22 +000011646 if (tg3_run_loopback(tp, ETH_FRAME_LEN, false))
11647 data[1] |= TG3_STD_LOOPBACK_FAILED;
Joe Perches63c3a662011-04-26 08:12:10 +000011648 if (tg3_flag(tp, TSO_CAPABLE) &&
Matt Carlson28a45952011-08-19 13:58:22 +000011649 tg3_run_loopback(tp, ETH_FRAME_LEN, true))
11650 data[1] |= TG3_TSO_LOOPBACK_FAILED;
Joe Perches63c3a662011-04-26 08:12:10 +000011651 if (tg3_flag(tp, JUMBO_RING_ENABLE) &&
Matt Carlson28a45952011-08-19 13:58:22 +000011652 tg3_run_loopback(tp, 9000 + ETH_HLEN, false))
11653 data[1] |= TG3_JMB_LOOPBACK_FAILED;
Michael Chan9f40dea2005-09-05 17:53:06 -070011654
Matt Carlson941ec902011-08-19 13:58:23 +000011655 if (do_extlpbk) {
11656 tg3_phy_lpbk_set(tp, 0, true);
11657
11658 /* All link indications report up, but the hardware
11659 * isn't really ready for about 20 msec. Double it
11660 * to be sure.
11661 */
11662 mdelay(40);
11663
11664 if (tg3_run_loopback(tp, ETH_FRAME_LEN, false))
11665 data[2] |= TG3_STD_LOOPBACK_FAILED;
11666 if (tg3_flag(tp, TSO_CAPABLE) &&
11667 tg3_run_loopback(tp, ETH_FRAME_LEN, true))
11668 data[2] |= TG3_TSO_LOOPBACK_FAILED;
11669 if (tg3_flag(tp, JUMBO_RING_ENABLE) &&
11670 tg3_run_loopback(tp, 9000 + ETH_HLEN, false))
11671 data[2] |= TG3_JMB_LOOPBACK_FAILED;
11672 }
11673
Matt Carlson5e5a7f32011-08-19 13:58:21 +000011674 /* Re-enable gphy autopowerdown. */
11675 if (tp->phy_flags & TG3_PHYFLG_ENABLE_APD)
11676 tg3_phy_toggle_apd(tp, true);
11677 }
Matt Carlson6833c042008-11-21 17:18:59 -080011678
Matt Carlson941ec902011-08-19 13:58:23 +000011679 err = (data[0] | data[1] | data[2]) ? -EIO : 0;
Matt Carlson28a45952011-08-19 13:58:22 +000011680
Matt Carlsonab789042011-01-25 15:58:54 +000011681done:
11682 tp->phy_flags |= eee_cap;
11683
Michael Chan9f40dea2005-09-05 17:53:06 -070011684 return err;
11685}
11686
Michael Chan4cafd3f2005-05-29 14:56:34 -070011687static void tg3_self_test(struct net_device *dev, struct ethtool_test *etest,
11688 u64 *data)
11689{
Michael Chan566f86a2005-05-29 14:56:58 -070011690 struct tg3 *tp = netdev_priv(dev);
Matt Carlson941ec902011-08-19 13:58:23 +000011691 bool doextlpbk = etest->flags & ETH_TEST_FL_EXTERNAL_LB;
Michael Chan566f86a2005-05-29 14:56:58 -070011692
Matt Carlsonbed98292011-07-13 09:27:29 +000011693 if ((tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) &&
11694 tg3_power_up(tp)) {
11695 etest->flags |= ETH_TEST_FL_FAILED;
11696 memset(data, 1, sizeof(u64) * TG3_NUM_TEST);
11697 return;
11698 }
Michael Chanbc1c7562006-03-20 17:48:03 -080011699
Michael Chan566f86a2005-05-29 14:56:58 -070011700 memset(data, 0, sizeof(u64) * TG3_NUM_TEST);
11701
11702 if (tg3_test_nvram(tp) != 0) {
11703 etest->flags |= ETH_TEST_FL_FAILED;
11704 data[0] = 1;
11705 }
Matt Carlson941ec902011-08-19 13:58:23 +000011706 if (!doextlpbk && tg3_test_link(tp)) {
Michael Chanca430072005-05-29 14:57:23 -070011707 etest->flags |= ETH_TEST_FL_FAILED;
11708 data[1] = 1;
11709 }
Michael Chana71116d2005-05-29 14:58:11 -070011710 if (etest->flags & ETH_TEST_FL_OFFLINE) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011711 int err, err2 = 0, irq_sync = 0;
Michael Chana71116d2005-05-29 14:58:11 -070011712
Michael Chanbbe832c2005-06-24 20:20:04 -070011713 if (netif_running(dev)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011714 tg3_phy_stop(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -070011715 tg3_netif_stop(tp);
11716 irq_sync = 1;
11717 }
11718
11719 tg3_full_lock(tp, irq_sync);
Michael Chana71116d2005-05-29 14:58:11 -070011720
11721 tg3_halt(tp, RESET_KIND_SUSPEND, 1);
Michael Chanec41c7d2006-01-17 02:40:55 -080011722 err = tg3_nvram_lock(tp);
Michael Chana71116d2005-05-29 14:58:11 -070011723 tg3_halt_cpu(tp, RX_CPU_BASE);
Joe Perches63c3a662011-04-26 08:12:10 +000011724 if (!tg3_flag(tp, 5705_PLUS))
Michael Chana71116d2005-05-29 14:58:11 -070011725 tg3_halt_cpu(tp, TX_CPU_BASE);
Michael Chanec41c7d2006-01-17 02:40:55 -080011726 if (!err)
11727 tg3_nvram_unlock(tp);
Michael Chana71116d2005-05-29 14:58:11 -070011728
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011729 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
Michael Chand9ab5ad2006-03-20 22:27:35 -080011730 tg3_phy_reset(tp);
11731
Michael Chana71116d2005-05-29 14:58:11 -070011732 if (tg3_test_registers(tp) != 0) {
11733 etest->flags |= ETH_TEST_FL_FAILED;
11734 data[2] = 1;
11735 }
Matt Carlson28a45952011-08-19 13:58:22 +000011736
Michael Chan7942e1d2005-05-29 14:58:36 -070011737 if (tg3_test_memory(tp) != 0) {
11738 etest->flags |= ETH_TEST_FL_FAILED;
11739 data[3] = 1;
11740 }
Matt Carlson28a45952011-08-19 13:58:22 +000011741
Matt Carlson941ec902011-08-19 13:58:23 +000011742 if (doextlpbk)
11743 etest->flags |= ETH_TEST_FL_EXTERNAL_LB_DONE;
11744
11745 if (tg3_test_loopback(tp, &data[4], doextlpbk))
Michael Chanc76949a2005-05-29 14:58:59 -070011746 etest->flags |= ETH_TEST_FL_FAILED;
Michael Chana71116d2005-05-29 14:58:11 -070011747
David S. Millerf47c11e2005-06-24 20:18:35 -070011748 tg3_full_unlock(tp);
11749
Michael Chand4bc3922005-05-29 14:59:20 -070011750 if (tg3_test_interrupt(tp) != 0) {
11751 etest->flags |= ETH_TEST_FL_FAILED;
Matt Carlson941ec902011-08-19 13:58:23 +000011752 data[7] = 1;
Michael Chand4bc3922005-05-29 14:59:20 -070011753 }
David S. Millerf47c11e2005-06-24 20:18:35 -070011754
11755 tg3_full_lock(tp, 0);
Michael Chand4bc3922005-05-29 14:59:20 -070011756
Michael Chana71116d2005-05-29 14:58:11 -070011757 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
11758 if (netif_running(dev)) {
Joe Perches63c3a662011-04-26 08:12:10 +000011759 tg3_flag_set(tp, INIT_COMPLETE);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011760 err2 = tg3_restart_hw(tp, 1);
11761 if (!err2)
Michael Chanb9ec6c12006-07-25 16:37:27 -070011762 tg3_netif_start(tp);
Michael Chana71116d2005-05-29 14:58:11 -070011763 }
David S. Millerf47c11e2005-06-24 20:18:35 -070011764
11765 tg3_full_unlock(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011766
11767 if (irq_sync && !err2)
11768 tg3_phy_start(tp);
Michael Chana71116d2005-05-29 14:58:11 -070011769 }
Matt Carlson80096062010-08-02 11:26:06 +000011770 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000011771 tg3_power_down(tp);
Michael Chanbc1c7562006-03-20 17:48:03 -080011772
Michael Chan4cafd3f2005-05-29 14:56:34 -070011773}
11774
Linus Torvalds1da177e2005-04-16 15:20:36 -070011775static int tg3_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
11776{
11777 struct mii_ioctl_data *data = if_mii(ifr);
11778 struct tg3 *tp = netdev_priv(dev);
11779 int err;
11780
Joe Perches63c3a662011-04-26 08:12:10 +000011781 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000011782 struct phy_device *phydev;
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011783 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011784 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000011785 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Richard Cochran28b04112010-07-17 08:48:55 +000011786 return phy_mii_ioctl(phydev, ifr, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011787 }
11788
Matt Carlson33f401a2010-04-05 10:19:27 +000011789 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011790 case SIOCGMIIPHY:
Matt Carlson882e9792009-09-01 13:21:36 +000011791 data->phy_id = tp->phy_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011792
11793 /* fallthru */
11794 case SIOCGMIIREG: {
11795 u32 mii_regval;
11796
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011797 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011798 break; /* We have no PHY */
11799
Matt Carlson34eea5a2011-04-20 07:57:38 +000011800 if (!netif_running(dev))
Michael Chanbc1c7562006-03-20 17:48:03 -080011801 return -EAGAIN;
11802
David S. Millerf47c11e2005-06-24 20:18:35 -070011803 spin_lock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011804 err = tg3_readphy(tp, data->reg_num & 0x1f, &mii_regval);
David S. Millerf47c11e2005-06-24 20:18:35 -070011805 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011806
11807 data->val_out = mii_regval;
11808
11809 return err;
11810 }
11811
11812 case SIOCSMIIREG:
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011813 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011814 break; /* We have no PHY */
11815
Matt Carlson34eea5a2011-04-20 07:57:38 +000011816 if (!netif_running(dev))
Michael Chanbc1c7562006-03-20 17:48:03 -080011817 return -EAGAIN;
11818
David S. Millerf47c11e2005-06-24 20:18:35 -070011819 spin_lock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011820 err = tg3_writephy(tp, data->reg_num & 0x1f, data->val_in);
David S. Millerf47c11e2005-06-24 20:18:35 -070011821 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011822
11823 return err;
11824
11825 default:
11826 /* do nothing */
11827 break;
11828 }
11829 return -EOPNOTSUPP;
11830}
11831
David S. Miller15f98502005-05-18 22:49:26 -070011832static int tg3_get_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
11833{
11834 struct tg3 *tp = netdev_priv(dev);
11835
11836 memcpy(ec, &tp->coal, sizeof(*ec));
11837 return 0;
11838}
11839
Michael Chand244c892005-07-05 14:42:33 -070011840static int tg3_set_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
11841{
11842 struct tg3 *tp = netdev_priv(dev);
11843 u32 max_rxcoal_tick_int = 0, max_txcoal_tick_int = 0;
11844 u32 max_stat_coal_ticks = 0, min_stat_coal_ticks = 0;
11845
Joe Perches63c3a662011-04-26 08:12:10 +000011846 if (!tg3_flag(tp, 5705_PLUS)) {
Michael Chand244c892005-07-05 14:42:33 -070011847 max_rxcoal_tick_int = MAX_RXCOAL_TICK_INT;
11848 max_txcoal_tick_int = MAX_TXCOAL_TICK_INT;
11849 max_stat_coal_ticks = MAX_STAT_COAL_TICKS;
11850 min_stat_coal_ticks = MIN_STAT_COAL_TICKS;
11851 }
11852
11853 if ((ec->rx_coalesce_usecs > MAX_RXCOL_TICKS) ||
11854 (ec->tx_coalesce_usecs > MAX_TXCOL_TICKS) ||
11855 (ec->rx_max_coalesced_frames > MAX_RXMAX_FRAMES) ||
11856 (ec->tx_max_coalesced_frames > MAX_TXMAX_FRAMES) ||
11857 (ec->rx_coalesce_usecs_irq > max_rxcoal_tick_int) ||
11858 (ec->tx_coalesce_usecs_irq > max_txcoal_tick_int) ||
11859 (ec->rx_max_coalesced_frames_irq > MAX_RXCOAL_MAXF_INT) ||
11860 (ec->tx_max_coalesced_frames_irq > MAX_TXCOAL_MAXF_INT) ||
11861 (ec->stats_block_coalesce_usecs > max_stat_coal_ticks) ||
11862 (ec->stats_block_coalesce_usecs < min_stat_coal_ticks))
11863 return -EINVAL;
11864
11865 /* No rx interrupts will be generated if both are zero */
11866 if ((ec->rx_coalesce_usecs == 0) &&
11867 (ec->rx_max_coalesced_frames == 0))
11868 return -EINVAL;
11869
11870 /* No tx interrupts will be generated if both are zero */
11871 if ((ec->tx_coalesce_usecs == 0) &&
11872 (ec->tx_max_coalesced_frames == 0))
11873 return -EINVAL;
11874
11875 /* Only copy relevant parameters, ignore all others. */
11876 tp->coal.rx_coalesce_usecs = ec->rx_coalesce_usecs;
11877 tp->coal.tx_coalesce_usecs = ec->tx_coalesce_usecs;
11878 tp->coal.rx_max_coalesced_frames = ec->rx_max_coalesced_frames;
11879 tp->coal.tx_max_coalesced_frames = ec->tx_max_coalesced_frames;
11880 tp->coal.rx_coalesce_usecs_irq = ec->rx_coalesce_usecs_irq;
11881 tp->coal.tx_coalesce_usecs_irq = ec->tx_coalesce_usecs_irq;
11882 tp->coal.rx_max_coalesced_frames_irq = ec->rx_max_coalesced_frames_irq;
11883 tp->coal.tx_max_coalesced_frames_irq = ec->tx_max_coalesced_frames_irq;
11884 tp->coal.stats_block_coalesce_usecs = ec->stats_block_coalesce_usecs;
11885
11886 if (netif_running(dev)) {
11887 tg3_full_lock(tp, 0);
11888 __tg3_set_coalesce(tp, &tp->coal);
11889 tg3_full_unlock(tp);
11890 }
11891 return 0;
11892}
11893
Jeff Garzik7282d492006-09-13 14:30:00 -040011894static const struct ethtool_ops tg3_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011895 .get_settings = tg3_get_settings,
11896 .set_settings = tg3_set_settings,
11897 .get_drvinfo = tg3_get_drvinfo,
11898 .get_regs_len = tg3_get_regs_len,
11899 .get_regs = tg3_get_regs,
11900 .get_wol = tg3_get_wol,
11901 .set_wol = tg3_set_wol,
11902 .get_msglevel = tg3_get_msglevel,
11903 .set_msglevel = tg3_set_msglevel,
11904 .nway_reset = tg3_nway_reset,
11905 .get_link = ethtool_op_get_link,
11906 .get_eeprom_len = tg3_get_eeprom_len,
11907 .get_eeprom = tg3_get_eeprom,
11908 .set_eeprom = tg3_set_eeprom,
11909 .get_ringparam = tg3_get_ringparam,
11910 .set_ringparam = tg3_set_ringparam,
11911 .get_pauseparam = tg3_get_pauseparam,
11912 .set_pauseparam = tg3_set_pauseparam,
Michael Chan4cafd3f2005-05-29 14:56:34 -070011913 .self_test = tg3_self_test,
Linus Torvalds1da177e2005-04-16 15:20:36 -070011914 .get_strings = tg3_get_strings,
stephen hemminger81b87092011-04-04 08:43:50 +000011915 .set_phys_id = tg3_set_phys_id,
Linus Torvalds1da177e2005-04-16 15:20:36 -070011916 .get_ethtool_stats = tg3_get_ethtool_stats,
David S. Miller15f98502005-05-18 22:49:26 -070011917 .get_coalesce = tg3_get_coalesce,
Michael Chand244c892005-07-05 14:42:33 -070011918 .set_coalesce = tg3_set_coalesce,
Jeff Garzikb9f2c042007-10-03 18:07:32 -070011919 .get_sset_count = tg3_get_sset_count,
Linus Torvalds1da177e2005-04-16 15:20:36 -070011920};
11921
11922static void __devinit tg3_get_eeprom_size(struct tg3 *tp)
11923{
Michael Chan1b277772006-03-20 22:27:48 -080011924 u32 cursize, val, magic;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011925
11926 tp->nvram_size = EEPROM_CHIP_SIZE;
11927
Matt Carlsone4f34112009-02-25 14:25:00 +000011928 if (tg3_nvram_read(tp, 0, &magic) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011929 return;
11930
Michael Chanb16250e2006-09-27 16:10:14 -070011931 if ((magic != TG3_EEPROM_MAGIC) &&
11932 ((magic & TG3_EEPROM_MAGIC_FW_MSK) != TG3_EEPROM_MAGIC_FW) &&
11933 ((magic & TG3_EEPROM_MAGIC_HW_MSK) != TG3_EEPROM_MAGIC_HW))
Linus Torvalds1da177e2005-04-16 15:20:36 -070011934 return;
11935
11936 /*
11937 * Size the chip by reading offsets at increasing powers of two.
11938 * When we encounter our validation signature, we know the addressing
11939 * has wrapped around, and thus have our chip size.
11940 */
Michael Chan1b277772006-03-20 22:27:48 -080011941 cursize = 0x10;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011942
11943 while (cursize < tp->nvram_size) {
Matt Carlsone4f34112009-02-25 14:25:00 +000011944 if (tg3_nvram_read(tp, cursize, &val) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011945 return;
11946
Michael Chan18201802006-03-20 22:29:15 -080011947 if (val == magic)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011948 break;
11949
11950 cursize <<= 1;
11951 }
11952
11953 tp->nvram_size = cursize;
11954}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011955
Linus Torvalds1da177e2005-04-16 15:20:36 -070011956static void __devinit tg3_get_nvram_size(struct tg3 *tp)
11957{
11958 u32 val;
11959
Joe Perches63c3a662011-04-26 08:12:10 +000011960 if (tg3_flag(tp, NO_NVRAM) || tg3_nvram_read(tp, 0, &val) != 0)
Michael Chan1b277772006-03-20 22:27:48 -080011961 return;
11962
11963 /* Selfboot format */
Michael Chan18201802006-03-20 22:29:15 -080011964 if (val != TG3_EEPROM_MAGIC) {
Michael Chan1b277772006-03-20 22:27:48 -080011965 tg3_get_eeprom_size(tp);
11966 return;
11967 }
11968
Matt Carlson6d348f22009-02-25 14:25:52 +000011969 if (tg3_nvram_read(tp, 0xf0, &val) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011970 if (val != 0) {
Matt Carlson6d348f22009-02-25 14:25:52 +000011971 /* This is confusing. We want to operate on the
11972 * 16-bit value at offset 0xf2. The tg3_nvram_read()
11973 * call will read from NVRAM and byteswap the data
11974 * according to the byteswapping settings for all
11975 * other register accesses. This ensures the data we
11976 * want will always reside in the lower 16-bits.
11977 * However, the data in NVRAM is in LE format, which
11978 * means the data from the NVRAM read will always be
11979 * opposite the endianness of the CPU. The 16-bit
11980 * byteswap then brings the data to CPU endianness.
11981 */
11982 tp->nvram_size = swab16((u16)(val & 0x0000ffff)) * 1024;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011983 return;
11984 }
11985 }
Matt Carlsonfd1122a2008-05-02 16:48:36 -070011986 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011987}
11988
11989static void __devinit tg3_get_nvram_info(struct tg3 *tp)
11990{
11991 u32 nvcfg1;
11992
11993 nvcfg1 = tr32(NVRAM_CFG1);
11994 if (nvcfg1 & NVRAM_CFG1_FLASHIF_ENAB) {
Joe Perches63c3a662011-04-26 08:12:10 +000011995 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000011996 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011997 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
11998 tw32(NVRAM_CFG1, nvcfg1);
11999 }
12000
Matt Carlson6ff6f812011-05-19 12:12:54 +000012001 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 ||
Joe Perches63c3a662011-04-26 08:12:10 +000012002 tg3_flag(tp, 5780_CLASS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012003 switch (nvcfg1 & NVRAM_CFG1_VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000012004 case FLASH_VENDOR_ATMEL_FLASH_BUFFERED:
12005 tp->nvram_jedecnum = JEDEC_ATMEL;
12006 tp->nvram_pagesize = ATMEL_AT45DB0X1B_PAGE_SIZE;
Joe Perches63c3a662011-04-26 08:12:10 +000012007 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000012008 break;
12009 case FLASH_VENDOR_ATMEL_FLASH_UNBUFFERED:
12010 tp->nvram_jedecnum = JEDEC_ATMEL;
12011 tp->nvram_pagesize = ATMEL_AT25F512_PAGE_SIZE;
12012 break;
12013 case FLASH_VENDOR_ATMEL_EEPROM:
12014 tp->nvram_jedecnum = JEDEC_ATMEL;
12015 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
Joe Perches63c3a662011-04-26 08:12:10 +000012016 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000012017 break;
12018 case FLASH_VENDOR_ST:
12019 tp->nvram_jedecnum = JEDEC_ST;
12020 tp->nvram_pagesize = ST_M45PEX0_PAGE_SIZE;
Joe Perches63c3a662011-04-26 08:12:10 +000012021 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000012022 break;
12023 case FLASH_VENDOR_SAIFUN:
12024 tp->nvram_jedecnum = JEDEC_SAIFUN;
12025 tp->nvram_pagesize = SAIFUN_SA25F0XX_PAGE_SIZE;
12026 break;
12027 case FLASH_VENDOR_SST_SMALL:
12028 case FLASH_VENDOR_SST_LARGE:
12029 tp->nvram_jedecnum = JEDEC_SST;
12030 tp->nvram_pagesize = SST_25VF0X0_PAGE_SIZE;
12031 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012032 }
Matt Carlson8590a602009-08-28 12:29:16 +000012033 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012034 tp->nvram_jedecnum = JEDEC_ATMEL;
12035 tp->nvram_pagesize = ATMEL_AT45DB0X1B_PAGE_SIZE;
Joe Perches63c3a662011-04-26 08:12:10 +000012036 tg3_flag_set(tp, NVRAM_BUFFERED);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012037 }
12038}
12039
Matt Carlsona1b950d2009-09-01 13:20:17 +000012040static void __devinit tg3_nvram_get_pagesize(struct tg3 *tp, u32 nvmcfg1)
12041{
12042 switch (nvmcfg1 & NVRAM_CFG1_5752PAGE_SIZE_MASK) {
12043 case FLASH_5752PAGE_SIZE_256:
12044 tp->nvram_pagesize = 256;
12045 break;
12046 case FLASH_5752PAGE_SIZE_512:
12047 tp->nvram_pagesize = 512;
12048 break;
12049 case FLASH_5752PAGE_SIZE_1K:
12050 tp->nvram_pagesize = 1024;
12051 break;
12052 case FLASH_5752PAGE_SIZE_2K:
12053 tp->nvram_pagesize = 2048;
12054 break;
12055 case FLASH_5752PAGE_SIZE_4K:
12056 tp->nvram_pagesize = 4096;
12057 break;
12058 case FLASH_5752PAGE_SIZE_264:
12059 tp->nvram_pagesize = 264;
12060 break;
12061 case FLASH_5752PAGE_SIZE_528:
12062 tp->nvram_pagesize = 528;
12063 break;
12064 }
12065}
12066
Michael Chan361b4ac2005-04-21 17:11:21 -070012067static void __devinit tg3_get_5752_nvram_info(struct tg3 *tp)
12068{
12069 u32 nvcfg1;
12070
12071 nvcfg1 = tr32(NVRAM_CFG1);
12072
Michael Chane6af3012005-04-21 17:12:05 -070012073 /* NVRAM protection for TPM */
12074 if (nvcfg1 & (1 << 27))
Joe Perches63c3a662011-04-26 08:12:10 +000012075 tg3_flag_set(tp, PROTECTED_NVRAM);
Michael Chane6af3012005-04-21 17:12:05 -070012076
Michael Chan361b4ac2005-04-21 17:11:21 -070012077 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000012078 case FLASH_5752VENDOR_ATMEL_EEPROM_64KHZ:
12079 case FLASH_5752VENDOR_ATMEL_EEPROM_376KHZ:
12080 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012081 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000012082 break;
12083 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
12084 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012085 tg3_flag_set(tp, NVRAM_BUFFERED);
12086 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000012087 break;
12088 case FLASH_5752VENDOR_ST_M45PE10:
12089 case FLASH_5752VENDOR_ST_M45PE20:
12090 case FLASH_5752VENDOR_ST_M45PE40:
12091 tp->nvram_jedecnum = JEDEC_ST;
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;
Michael Chan361b4ac2005-04-21 17:11:21 -070012095 }
12096
Joe Perches63c3a662011-04-26 08:12:10 +000012097 if (tg3_flag(tp, FLASH)) {
Matt Carlsona1b950d2009-09-01 13:20:17 +000012098 tg3_nvram_get_pagesize(tp, nvcfg1);
Matt Carlson8590a602009-08-28 12:29:16 +000012099 } else {
Michael Chan361b4ac2005-04-21 17:11:21 -070012100 /* For eeprom, set pagesize to maximum eeprom size */
12101 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
12102
12103 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
12104 tw32(NVRAM_CFG1, nvcfg1);
12105 }
12106}
12107
Michael Chand3c7b882006-03-23 01:28:25 -080012108static void __devinit tg3_get_5755_nvram_info(struct tg3 *tp)
12109{
Matt Carlson989a9d22007-05-05 11:51:05 -070012110 u32 nvcfg1, protect = 0;
Michael Chand3c7b882006-03-23 01:28:25 -080012111
12112 nvcfg1 = tr32(NVRAM_CFG1);
12113
12114 /* NVRAM protection for TPM */
Matt Carlson989a9d22007-05-05 11:51:05 -070012115 if (nvcfg1 & (1 << 27)) {
Joe Perches63c3a662011-04-26 08:12:10 +000012116 tg3_flag_set(tp, PROTECTED_NVRAM);
Matt Carlson989a9d22007-05-05 11:51:05 -070012117 protect = 1;
12118 }
Michael Chand3c7b882006-03-23 01:28:25 -080012119
Matt Carlson989a9d22007-05-05 11:51:05 -070012120 nvcfg1 &= NVRAM_CFG1_5752VENDOR_MASK;
12121 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000012122 case FLASH_5755VENDOR_ATMEL_FLASH_1:
12123 case FLASH_5755VENDOR_ATMEL_FLASH_2:
12124 case FLASH_5755VENDOR_ATMEL_FLASH_3:
12125 case FLASH_5755VENDOR_ATMEL_FLASH_5:
12126 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012127 tg3_flag_set(tp, NVRAM_BUFFERED);
12128 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000012129 tp->nvram_pagesize = 264;
12130 if (nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_1 ||
12131 nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_5)
12132 tp->nvram_size = (protect ? 0x3e200 :
12133 TG3_NVRAM_SIZE_512KB);
12134 else if (nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_2)
12135 tp->nvram_size = (protect ? 0x1f200 :
12136 TG3_NVRAM_SIZE_256KB);
12137 else
12138 tp->nvram_size = (protect ? 0x1f200 :
12139 TG3_NVRAM_SIZE_128KB);
12140 break;
12141 case FLASH_5752VENDOR_ST_M45PE10:
12142 case FLASH_5752VENDOR_ST_M45PE20:
12143 case FLASH_5752VENDOR_ST_M45PE40:
12144 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000012145 tg3_flag_set(tp, NVRAM_BUFFERED);
12146 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000012147 tp->nvram_pagesize = 256;
12148 if (nvcfg1 == FLASH_5752VENDOR_ST_M45PE10)
12149 tp->nvram_size = (protect ?
12150 TG3_NVRAM_SIZE_64KB :
12151 TG3_NVRAM_SIZE_128KB);
12152 else if (nvcfg1 == FLASH_5752VENDOR_ST_M45PE20)
12153 tp->nvram_size = (protect ?
12154 TG3_NVRAM_SIZE_64KB :
12155 TG3_NVRAM_SIZE_256KB);
12156 else
12157 tp->nvram_size = (protect ?
12158 TG3_NVRAM_SIZE_128KB :
12159 TG3_NVRAM_SIZE_512KB);
12160 break;
Michael Chand3c7b882006-03-23 01:28:25 -080012161 }
12162}
12163
Michael Chan1b277772006-03-20 22:27:48 -080012164static void __devinit tg3_get_5787_nvram_info(struct tg3 *tp)
12165{
12166 u32 nvcfg1;
12167
12168 nvcfg1 = tr32(NVRAM_CFG1);
12169
12170 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000012171 case FLASH_5787VENDOR_ATMEL_EEPROM_64KHZ:
12172 case FLASH_5787VENDOR_ATMEL_EEPROM_376KHZ:
12173 case FLASH_5787VENDOR_MICRO_EEPROM_64KHZ:
12174 case FLASH_5787VENDOR_MICRO_EEPROM_376KHZ:
12175 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012176 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000012177 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
Michael Chan1b277772006-03-20 22:27:48 -080012178
Matt Carlson8590a602009-08-28 12:29:16 +000012179 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
12180 tw32(NVRAM_CFG1, nvcfg1);
12181 break;
12182 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
12183 case FLASH_5755VENDOR_ATMEL_FLASH_1:
12184 case FLASH_5755VENDOR_ATMEL_FLASH_2:
12185 case FLASH_5755VENDOR_ATMEL_FLASH_3:
12186 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012187 tg3_flag_set(tp, NVRAM_BUFFERED);
12188 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000012189 tp->nvram_pagesize = 264;
12190 break;
12191 case FLASH_5752VENDOR_ST_M45PE10:
12192 case FLASH_5752VENDOR_ST_M45PE20:
12193 case FLASH_5752VENDOR_ST_M45PE40:
12194 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000012195 tg3_flag_set(tp, NVRAM_BUFFERED);
12196 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000012197 tp->nvram_pagesize = 256;
12198 break;
Michael Chan1b277772006-03-20 22:27:48 -080012199 }
12200}
12201
Matt Carlson6b91fa02007-10-10 18:01:09 -070012202static void __devinit tg3_get_5761_nvram_info(struct tg3 *tp)
12203{
12204 u32 nvcfg1, protect = 0;
12205
12206 nvcfg1 = tr32(NVRAM_CFG1);
12207
12208 /* NVRAM protection for TPM */
12209 if (nvcfg1 & (1 << 27)) {
Joe Perches63c3a662011-04-26 08:12:10 +000012210 tg3_flag_set(tp, PROTECTED_NVRAM);
Matt Carlson6b91fa02007-10-10 18:01:09 -070012211 protect = 1;
12212 }
12213
12214 nvcfg1 &= NVRAM_CFG1_5752VENDOR_MASK;
12215 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000012216 case FLASH_5761VENDOR_ATMEL_ADB021D:
12217 case FLASH_5761VENDOR_ATMEL_ADB041D:
12218 case FLASH_5761VENDOR_ATMEL_ADB081D:
12219 case FLASH_5761VENDOR_ATMEL_ADB161D:
12220 case FLASH_5761VENDOR_ATMEL_MDB021D:
12221 case FLASH_5761VENDOR_ATMEL_MDB041D:
12222 case FLASH_5761VENDOR_ATMEL_MDB081D:
12223 case FLASH_5761VENDOR_ATMEL_MDB161D:
12224 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012225 tg3_flag_set(tp, NVRAM_BUFFERED);
12226 tg3_flag_set(tp, FLASH);
12227 tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
Matt Carlson8590a602009-08-28 12:29:16 +000012228 tp->nvram_pagesize = 256;
12229 break;
12230 case FLASH_5761VENDOR_ST_A_M45PE20:
12231 case FLASH_5761VENDOR_ST_A_M45PE40:
12232 case FLASH_5761VENDOR_ST_A_M45PE80:
12233 case FLASH_5761VENDOR_ST_A_M45PE16:
12234 case FLASH_5761VENDOR_ST_M_M45PE20:
12235 case FLASH_5761VENDOR_ST_M_M45PE40:
12236 case FLASH_5761VENDOR_ST_M_M45PE80:
12237 case FLASH_5761VENDOR_ST_M_M45PE16:
12238 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000012239 tg3_flag_set(tp, NVRAM_BUFFERED);
12240 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000012241 tp->nvram_pagesize = 256;
12242 break;
Matt Carlson6b91fa02007-10-10 18:01:09 -070012243 }
12244
12245 if (protect) {
12246 tp->nvram_size = tr32(NVRAM_ADDR_LOCKOUT);
12247 } else {
12248 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000012249 case FLASH_5761VENDOR_ATMEL_ADB161D:
12250 case FLASH_5761VENDOR_ATMEL_MDB161D:
12251 case FLASH_5761VENDOR_ST_A_M45PE16:
12252 case FLASH_5761VENDOR_ST_M_M45PE16:
12253 tp->nvram_size = TG3_NVRAM_SIZE_2MB;
12254 break;
12255 case FLASH_5761VENDOR_ATMEL_ADB081D:
12256 case FLASH_5761VENDOR_ATMEL_MDB081D:
12257 case FLASH_5761VENDOR_ST_A_M45PE80:
12258 case FLASH_5761VENDOR_ST_M_M45PE80:
12259 tp->nvram_size = TG3_NVRAM_SIZE_1MB;
12260 break;
12261 case FLASH_5761VENDOR_ATMEL_ADB041D:
12262 case FLASH_5761VENDOR_ATMEL_MDB041D:
12263 case FLASH_5761VENDOR_ST_A_M45PE40:
12264 case FLASH_5761VENDOR_ST_M_M45PE40:
12265 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
12266 break;
12267 case FLASH_5761VENDOR_ATMEL_ADB021D:
12268 case FLASH_5761VENDOR_ATMEL_MDB021D:
12269 case FLASH_5761VENDOR_ST_A_M45PE20:
12270 case FLASH_5761VENDOR_ST_M_M45PE20:
12271 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
12272 break;
Matt Carlson6b91fa02007-10-10 18:01:09 -070012273 }
12274 }
12275}
12276
Michael Chanb5d37722006-09-27 16:06:21 -070012277static void __devinit tg3_get_5906_nvram_info(struct tg3 *tp)
12278{
12279 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012280 tg3_flag_set(tp, NVRAM_BUFFERED);
Michael Chanb5d37722006-09-27 16:06:21 -070012281 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
12282}
12283
Matt Carlson321d32a2008-11-21 17:22:19 -080012284static void __devinit tg3_get_57780_nvram_info(struct tg3 *tp)
12285{
12286 u32 nvcfg1;
12287
12288 nvcfg1 = tr32(NVRAM_CFG1);
12289
12290 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
12291 case FLASH_5787VENDOR_ATMEL_EEPROM_376KHZ:
12292 case FLASH_5787VENDOR_MICRO_EEPROM_376KHZ:
12293 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012294 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson321d32a2008-11-21 17:22:19 -080012295 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
12296
12297 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
12298 tw32(NVRAM_CFG1, nvcfg1);
12299 return;
12300 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
12301 case FLASH_57780VENDOR_ATMEL_AT45DB011D:
12302 case FLASH_57780VENDOR_ATMEL_AT45DB011B:
12303 case FLASH_57780VENDOR_ATMEL_AT45DB021D:
12304 case FLASH_57780VENDOR_ATMEL_AT45DB021B:
12305 case FLASH_57780VENDOR_ATMEL_AT45DB041D:
12306 case FLASH_57780VENDOR_ATMEL_AT45DB041B:
12307 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012308 tg3_flag_set(tp, NVRAM_BUFFERED);
12309 tg3_flag_set(tp, FLASH);
Matt Carlson321d32a2008-11-21 17:22:19 -080012310
12311 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
12312 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
12313 case FLASH_57780VENDOR_ATMEL_AT45DB011D:
12314 case FLASH_57780VENDOR_ATMEL_AT45DB011B:
12315 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
12316 break;
12317 case FLASH_57780VENDOR_ATMEL_AT45DB021D:
12318 case FLASH_57780VENDOR_ATMEL_AT45DB021B:
12319 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
12320 break;
12321 case FLASH_57780VENDOR_ATMEL_AT45DB041D:
12322 case FLASH_57780VENDOR_ATMEL_AT45DB041B:
12323 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
12324 break;
12325 }
12326 break;
12327 case FLASH_5752VENDOR_ST_M45PE10:
12328 case FLASH_5752VENDOR_ST_M45PE20:
12329 case FLASH_5752VENDOR_ST_M45PE40:
12330 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000012331 tg3_flag_set(tp, NVRAM_BUFFERED);
12332 tg3_flag_set(tp, FLASH);
Matt Carlson321d32a2008-11-21 17:22:19 -080012333
12334 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
12335 case FLASH_5752VENDOR_ST_M45PE10:
12336 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
12337 break;
12338 case FLASH_5752VENDOR_ST_M45PE20:
12339 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
12340 break;
12341 case FLASH_5752VENDOR_ST_M45PE40:
12342 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
12343 break;
12344 }
12345 break;
12346 default:
Joe Perches63c3a662011-04-26 08:12:10 +000012347 tg3_flag_set(tp, NO_NVRAM);
Matt Carlson321d32a2008-11-21 17:22:19 -080012348 return;
12349 }
12350
Matt Carlsona1b950d2009-09-01 13:20:17 +000012351 tg3_nvram_get_pagesize(tp, nvcfg1);
12352 if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
Joe Perches63c3a662011-04-26 08:12:10 +000012353 tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
Matt Carlsona1b950d2009-09-01 13:20:17 +000012354}
12355
12356
12357static void __devinit tg3_get_5717_nvram_info(struct tg3 *tp)
12358{
12359 u32 nvcfg1;
12360
12361 nvcfg1 = tr32(NVRAM_CFG1);
12362
12363 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
12364 case FLASH_5717VENDOR_ATMEL_EEPROM:
12365 case FLASH_5717VENDOR_MICRO_EEPROM:
12366 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012367 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlsona1b950d2009-09-01 13:20:17 +000012368 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
12369
12370 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
12371 tw32(NVRAM_CFG1, nvcfg1);
12372 return;
12373 case FLASH_5717VENDOR_ATMEL_MDB011D:
12374 case FLASH_5717VENDOR_ATMEL_ADB011B:
12375 case FLASH_5717VENDOR_ATMEL_ADB011D:
12376 case FLASH_5717VENDOR_ATMEL_MDB021D:
12377 case FLASH_5717VENDOR_ATMEL_ADB021B:
12378 case FLASH_5717VENDOR_ATMEL_ADB021D:
12379 case FLASH_5717VENDOR_ATMEL_45USPT:
12380 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012381 tg3_flag_set(tp, NVRAM_BUFFERED);
12382 tg3_flag_set(tp, FLASH);
Matt Carlsona1b950d2009-09-01 13:20:17 +000012383
12384 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
12385 case FLASH_5717VENDOR_ATMEL_MDB021D:
Matt Carlson66ee33b2011-04-05 14:22:51 +000012386 /* Detect size with tg3_nvram_get_size() */
12387 break;
Matt Carlsona1b950d2009-09-01 13:20:17 +000012388 case FLASH_5717VENDOR_ATMEL_ADB021B:
12389 case FLASH_5717VENDOR_ATMEL_ADB021D:
12390 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
12391 break;
12392 default:
12393 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
12394 break;
12395 }
Matt Carlson321d32a2008-11-21 17:22:19 -080012396 break;
Matt Carlsona1b950d2009-09-01 13:20:17 +000012397 case FLASH_5717VENDOR_ST_M_M25PE10:
12398 case FLASH_5717VENDOR_ST_A_M25PE10:
12399 case FLASH_5717VENDOR_ST_M_M45PE10:
12400 case FLASH_5717VENDOR_ST_A_M45PE10:
12401 case FLASH_5717VENDOR_ST_M_M25PE20:
12402 case FLASH_5717VENDOR_ST_A_M25PE20:
12403 case FLASH_5717VENDOR_ST_M_M45PE20:
12404 case FLASH_5717VENDOR_ST_A_M45PE20:
12405 case FLASH_5717VENDOR_ST_25USPT:
12406 case FLASH_5717VENDOR_ST_45USPT:
12407 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000012408 tg3_flag_set(tp, NVRAM_BUFFERED);
12409 tg3_flag_set(tp, FLASH);
Matt Carlsona1b950d2009-09-01 13:20:17 +000012410
12411 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
12412 case FLASH_5717VENDOR_ST_M_M25PE20:
Matt Carlsona1b950d2009-09-01 13:20:17 +000012413 case FLASH_5717VENDOR_ST_M_M45PE20:
Matt Carlson66ee33b2011-04-05 14:22:51 +000012414 /* Detect size with tg3_nvram_get_size() */
12415 break;
12416 case FLASH_5717VENDOR_ST_A_M25PE20:
Matt Carlsona1b950d2009-09-01 13:20:17 +000012417 case FLASH_5717VENDOR_ST_A_M45PE20:
12418 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
12419 break;
12420 default:
12421 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
12422 break;
12423 }
Matt Carlson321d32a2008-11-21 17:22:19 -080012424 break;
Matt Carlsona1b950d2009-09-01 13:20:17 +000012425 default:
Joe Perches63c3a662011-04-26 08:12:10 +000012426 tg3_flag_set(tp, NO_NVRAM);
Matt Carlsona1b950d2009-09-01 13:20:17 +000012427 return;
Matt Carlson321d32a2008-11-21 17:22:19 -080012428 }
Matt Carlsona1b950d2009-09-01 13:20:17 +000012429
12430 tg3_nvram_get_pagesize(tp, nvcfg1);
12431 if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
Joe Perches63c3a662011-04-26 08:12:10 +000012432 tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
Matt Carlson321d32a2008-11-21 17:22:19 -080012433}
12434
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012435static void __devinit tg3_get_5720_nvram_info(struct tg3 *tp)
12436{
12437 u32 nvcfg1, nvmpinstrp;
12438
12439 nvcfg1 = tr32(NVRAM_CFG1);
12440 nvmpinstrp = nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK;
12441
12442 switch (nvmpinstrp) {
12443 case FLASH_5720_EEPROM_HD:
12444 case FLASH_5720_EEPROM_LD:
12445 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012446 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012447
12448 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
12449 tw32(NVRAM_CFG1, nvcfg1);
12450 if (nvmpinstrp == FLASH_5720_EEPROM_HD)
12451 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
12452 else
12453 tp->nvram_pagesize = ATMEL_AT24C02_CHIP_SIZE;
12454 return;
12455 case FLASH_5720VENDOR_M_ATMEL_DB011D:
12456 case FLASH_5720VENDOR_A_ATMEL_DB011B:
12457 case FLASH_5720VENDOR_A_ATMEL_DB011D:
12458 case FLASH_5720VENDOR_M_ATMEL_DB021D:
12459 case FLASH_5720VENDOR_A_ATMEL_DB021B:
12460 case FLASH_5720VENDOR_A_ATMEL_DB021D:
12461 case FLASH_5720VENDOR_M_ATMEL_DB041D:
12462 case FLASH_5720VENDOR_A_ATMEL_DB041B:
12463 case FLASH_5720VENDOR_A_ATMEL_DB041D:
12464 case FLASH_5720VENDOR_M_ATMEL_DB081D:
12465 case FLASH_5720VENDOR_A_ATMEL_DB081D:
12466 case FLASH_5720VENDOR_ATMEL_45USPT:
12467 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012468 tg3_flag_set(tp, NVRAM_BUFFERED);
12469 tg3_flag_set(tp, FLASH);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012470
12471 switch (nvmpinstrp) {
12472 case FLASH_5720VENDOR_M_ATMEL_DB021D:
12473 case FLASH_5720VENDOR_A_ATMEL_DB021B:
12474 case FLASH_5720VENDOR_A_ATMEL_DB021D:
12475 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
12476 break;
12477 case FLASH_5720VENDOR_M_ATMEL_DB041D:
12478 case FLASH_5720VENDOR_A_ATMEL_DB041B:
12479 case FLASH_5720VENDOR_A_ATMEL_DB041D:
12480 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
12481 break;
12482 case FLASH_5720VENDOR_M_ATMEL_DB081D:
12483 case FLASH_5720VENDOR_A_ATMEL_DB081D:
12484 tp->nvram_size = TG3_NVRAM_SIZE_1MB;
12485 break;
12486 default:
12487 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
12488 break;
12489 }
12490 break;
12491 case FLASH_5720VENDOR_M_ST_M25PE10:
12492 case FLASH_5720VENDOR_M_ST_M45PE10:
12493 case FLASH_5720VENDOR_A_ST_M25PE10:
12494 case FLASH_5720VENDOR_A_ST_M45PE10:
12495 case FLASH_5720VENDOR_M_ST_M25PE20:
12496 case FLASH_5720VENDOR_M_ST_M45PE20:
12497 case FLASH_5720VENDOR_A_ST_M25PE20:
12498 case FLASH_5720VENDOR_A_ST_M45PE20:
12499 case FLASH_5720VENDOR_M_ST_M25PE40:
12500 case FLASH_5720VENDOR_M_ST_M45PE40:
12501 case FLASH_5720VENDOR_A_ST_M25PE40:
12502 case FLASH_5720VENDOR_A_ST_M45PE40:
12503 case FLASH_5720VENDOR_M_ST_M25PE80:
12504 case FLASH_5720VENDOR_M_ST_M45PE80:
12505 case FLASH_5720VENDOR_A_ST_M25PE80:
12506 case FLASH_5720VENDOR_A_ST_M45PE80:
12507 case FLASH_5720VENDOR_ST_25USPT:
12508 case FLASH_5720VENDOR_ST_45USPT:
12509 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000012510 tg3_flag_set(tp, NVRAM_BUFFERED);
12511 tg3_flag_set(tp, FLASH);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012512
12513 switch (nvmpinstrp) {
12514 case FLASH_5720VENDOR_M_ST_M25PE20:
12515 case FLASH_5720VENDOR_M_ST_M45PE20:
12516 case FLASH_5720VENDOR_A_ST_M25PE20:
12517 case FLASH_5720VENDOR_A_ST_M45PE20:
12518 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
12519 break;
12520 case FLASH_5720VENDOR_M_ST_M25PE40:
12521 case FLASH_5720VENDOR_M_ST_M45PE40:
12522 case FLASH_5720VENDOR_A_ST_M25PE40:
12523 case FLASH_5720VENDOR_A_ST_M45PE40:
12524 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
12525 break;
12526 case FLASH_5720VENDOR_M_ST_M25PE80:
12527 case FLASH_5720VENDOR_M_ST_M45PE80:
12528 case FLASH_5720VENDOR_A_ST_M25PE80:
12529 case FLASH_5720VENDOR_A_ST_M45PE80:
12530 tp->nvram_size = TG3_NVRAM_SIZE_1MB;
12531 break;
12532 default:
12533 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
12534 break;
12535 }
12536 break;
12537 default:
Joe Perches63c3a662011-04-26 08:12:10 +000012538 tg3_flag_set(tp, NO_NVRAM);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012539 return;
12540 }
12541
12542 tg3_nvram_get_pagesize(tp, nvcfg1);
12543 if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
Joe Perches63c3a662011-04-26 08:12:10 +000012544 tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012545}
12546
Linus Torvalds1da177e2005-04-16 15:20:36 -070012547/* Chips other than 5700/5701 use the NVRAM for fetching info. */
12548static void __devinit tg3_nvram_init(struct tg3 *tp)
12549{
Linus Torvalds1da177e2005-04-16 15:20:36 -070012550 tw32_f(GRC_EEPROM_ADDR,
12551 (EEPROM_ADDR_FSM_RESET |
12552 (EEPROM_DEFAULT_CLOCK_PERIOD <<
12553 EEPROM_ADDR_CLKPERD_SHIFT)));
12554
Michael Chan9d57f012006-12-07 00:23:25 -080012555 msleep(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012556
12557 /* Enable seeprom accesses. */
12558 tw32_f(GRC_LOCAL_CTRL,
12559 tr32(GRC_LOCAL_CTRL) | GRC_LCLCTRL_AUTO_SEEPROM);
12560 udelay(100);
12561
12562 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
12563 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701) {
Joe Perches63c3a662011-04-26 08:12:10 +000012564 tg3_flag_set(tp, NVRAM);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012565
Michael Chanec41c7d2006-01-17 02:40:55 -080012566 if (tg3_nvram_lock(tp)) {
Matt Carlson5129c3a2010-04-05 10:19:23 +000012567 netdev_warn(tp->dev,
12568 "Cannot get nvram lock, %s failed\n",
Joe Perches05dbe002010-02-17 19:44:19 +000012569 __func__);
Michael Chanec41c7d2006-01-17 02:40:55 -080012570 return;
12571 }
Michael Chane6af3012005-04-21 17:12:05 -070012572 tg3_enable_nvram_access(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012573
Matt Carlson989a9d22007-05-05 11:51:05 -070012574 tp->nvram_size = 0;
12575
Michael Chan361b4ac2005-04-21 17:11:21 -070012576 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752)
12577 tg3_get_5752_nvram_info(tp);
Michael Chand3c7b882006-03-23 01:28:25 -080012578 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755)
12579 tg3_get_5755_nvram_info(tp);
Matt Carlsond30cdd22007-10-07 23:28:35 -070012580 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787 ||
Matt Carlson57e69832008-05-25 23:48:31 -070012581 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
12582 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
Michael Chan1b277772006-03-20 22:27:48 -080012583 tg3_get_5787_nvram_info(tp);
Matt Carlson6b91fa02007-10-10 18:01:09 -070012584 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
12585 tg3_get_5761_nvram_info(tp);
Michael Chanb5d37722006-09-27 16:06:21 -070012586 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
12587 tg3_get_5906_nvram_info(tp);
Matt Carlsonb703df62009-12-03 08:36:21 +000012588 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
12589 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Matt Carlson321d32a2008-11-21 17:22:19 -080012590 tg3_get_57780_nvram_info(tp);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012591 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
12592 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
Matt Carlsona1b950d2009-09-01 13:20:17 +000012593 tg3_get_5717_nvram_info(tp);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012594 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
12595 tg3_get_5720_nvram_info(tp);
Michael Chan361b4ac2005-04-21 17:11:21 -070012596 else
12597 tg3_get_nvram_info(tp);
12598
Matt Carlson989a9d22007-05-05 11:51:05 -070012599 if (tp->nvram_size == 0)
12600 tg3_get_nvram_size(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012601
Michael Chane6af3012005-04-21 17:12:05 -070012602 tg3_disable_nvram_access(tp);
Michael Chan381291b2005-12-13 21:08:21 -080012603 tg3_nvram_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012604
12605 } else {
Joe Perches63c3a662011-04-26 08:12:10 +000012606 tg3_flag_clear(tp, NVRAM);
12607 tg3_flag_clear(tp, NVRAM_BUFFERED);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012608
12609 tg3_get_eeprom_size(tp);
12610 }
12611}
12612
Linus Torvalds1da177e2005-04-16 15:20:36 -070012613static int tg3_nvram_write_block_using_eeprom(struct tg3 *tp,
12614 u32 offset, u32 len, u8 *buf)
12615{
12616 int i, j, rc = 0;
12617 u32 val;
12618
12619 for (i = 0; i < len; i += 4) {
Al Virob9fc7dc2007-12-17 22:59:57 -080012620 u32 addr;
Matt Carlsona9dc5292009-02-25 14:25:30 +000012621 __be32 data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012622
12623 addr = offset + i;
12624
12625 memcpy(&data, buf + i, 4);
12626
Matt Carlson62cedd12009-04-20 14:52:29 -070012627 /*
12628 * The SEEPROM interface expects the data to always be opposite
12629 * the native endian format. We accomplish this by reversing
12630 * all the operations that would have been performed on the
12631 * data from a call to tg3_nvram_read_be32().
12632 */
12633 tw32(GRC_EEPROM_DATA, swab32(be32_to_cpu(data)));
Linus Torvalds1da177e2005-04-16 15:20:36 -070012634
12635 val = tr32(GRC_EEPROM_ADDR);
12636 tw32(GRC_EEPROM_ADDR, val | EEPROM_ADDR_COMPLETE);
12637
12638 val &= ~(EEPROM_ADDR_ADDR_MASK | EEPROM_ADDR_DEVID_MASK |
12639 EEPROM_ADDR_READ);
12640 tw32(GRC_EEPROM_ADDR, val |
12641 (0 << EEPROM_ADDR_DEVID_SHIFT) |
12642 (addr & EEPROM_ADDR_ADDR_MASK) |
12643 EEPROM_ADDR_START |
12644 EEPROM_ADDR_WRITE);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012645
Michael Chan9d57f012006-12-07 00:23:25 -080012646 for (j = 0; j < 1000; j++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012647 val = tr32(GRC_EEPROM_ADDR);
12648
12649 if (val & EEPROM_ADDR_COMPLETE)
12650 break;
Michael Chan9d57f012006-12-07 00:23:25 -080012651 msleep(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012652 }
12653 if (!(val & EEPROM_ADDR_COMPLETE)) {
12654 rc = -EBUSY;
12655 break;
12656 }
12657 }
12658
12659 return rc;
12660}
12661
12662/* offset and length are dword aligned */
12663static int tg3_nvram_write_block_unbuffered(struct tg3 *tp, u32 offset, u32 len,
12664 u8 *buf)
12665{
12666 int ret = 0;
12667 u32 pagesize = tp->nvram_pagesize;
12668 u32 pagemask = pagesize - 1;
12669 u32 nvram_cmd;
12670 u8 *tmp;
12671
12672 tmp = kmalloc(pagesize, GFP_KERNEL);
12673 if (tmp == NULL)
12674 return -ENOMEM;
12675
12676 while (len) {
12677 int j;
Michael Chane6af3012005-04-21 17:12:05 -070012678 u32 phy_addr, page_off, size;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012679
12680 phy_addr = offset & ~pagemask;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012681
Linus Torvalds1da177e2005-04-16 15:20:36 -070012682 for (j = 0; j < pagesize; j += 4) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000012683 ret = tg3_nvram_read_be32(tp, phy_addr + j,
12684 (__be32 *) (tmp + j));
12685 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012686 break;
12687 }
12688 if (ret)
12689 break;
12690
Matt Carlsonc6cdf432010-04-05 10:19:26 +000012691 page_off = offset & pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012692 size = pagesize;
12693 if (len < size)
12694 size = len;
12695
12696 len -= size;
12697
12698 memcpy(tmp + page_off, buf, size);
12699
12700 offset = offset + (pagesize - page_off);
12701
Michael Chane6af3012005-04-21 17:12:05 -070012702 tg3_enable_nvram_access(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012703
12704 /*
12705 * Before we can erase the flash page, we need
12706 * to issue a special "write enable" command.
12707 */
12708 nvram_cmd = NVRAM_CMD_WREN | NVRAM_CMD_GO | NVRAM_CMD_DONE;
12709
12710 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
12711 break;
12712
12713 /* Erase the target page */
12714 tw32(NVRAM_ADDR, phy_addr);
12715
12716 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE | NVRAM_CMD_WR |
12717 NVRAM_CMD_FIRST | NVRAM_CMD_LAST | NVRAM_CMD_ERASE;
12718
Matt Carlsonc6cdf432010-04-05 10:19:26 +000012719 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
Linus Torvalds1da177e2005-04-16 15:20:36 -070012720 break;
12721
12722 /* Issue another write enable to start the write. */
12723 nvram_cmd = NVRAM_CMD_WREN | NVRAM_CMD_GO | NVRAM_CMD_DONE;
12724
12725 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
12726 break;
12727
12728 for (j = 0; j < pagesize; j += 4) {
Al Virob9fc7dc2007-12-17 22:59:57 -080012729 __be32 data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012730
Al Virob9fc7dc2007-12-17 22:59:57 -080012731 data = *((__be32 *) (tmp + j));
Matt Carlsona9dc5292009-02-25 14:25:30 +000012732
Al Virob9fc7dc2007-12-17 22:59:57 -080012733 tw32(NVRAM_WRDATA, be32_to_cpu(data));
Linus Torvalds1da177e2005-04-16 15:20:36 -070012734
12735 tw32(NVRAM_ADDR, phy_addr + j);
12736
12737 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE |
12738 NVRAM_CMD_WR;
12739
12740 if (j == 0)
12741 nvram_cmd |= NVRAM_CMD_FIRST;
12742 else if (j == (pagesize - 4))
12743 nvram_cmd |= NVRAM_CMD_LAST;
12744
12745 if ((ret = tg3_nvram_exec_cmd(tp, nvram_cmd)))
12746 break;
12747 }
12748 if (ret)
12749 break;
12750 }
12751
12752 nvram_cmd = NVRAM_CMD_WRDI | NVRAM_CMD_GO | NVRAM_CMD_DONE;
12753 tg3_nvram_exec_cmd(tp, nvram_cmd);
12754
12755 kfree(tmp);
12756
12757 return ret;
12758}
12759
12760/* offset and length are dword aligned */
12761static int tg3_nvram_write_block_buffered(struct tg3 *tp, u32 offset, u32 len,
12762 u8 *buf)
12763{
12764 int i, ret = 0;
12765
12766 for (i = 0; i < len; i += 4, offset += 4) {
Al Virob9fc7dc2007-12-17 22:59:57 -080012767 u32 page_off, phy_addr, nvram_cmd;
12768 __be32 data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012769
12770 memcpy(&data, buf + i, 4);
Al Virob9fc7dc2007-12-17 22:59:57 -080012771 tw32(NVRAM_WRDATA, be32_to_cpu(data));
Linus Torvalds1da177e2005-04-16 15:20:36 -070012772
Matt Carlsonc6cdf432010-04-05 10:19:26 +000012773 page_off = offset % tp->nvram_pagesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012774
Michael Chan18201802006-03-20 22:29:15 -080012775 phy_addr = tg3_nvram_phys_addr(tp, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012776
12777 tw32(NVRAM_ADDR, phy_addr);
12778
12779 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE | NVRAM_CMD_WR;
12780
Matt Carlsonc6cdf432010-04-05 10:19:26 +000012781 if (page_off == 0 || i == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012782 nvram_cmd |= NVRAM_CMD_FIRST;
Michael Chanf6d9a252006-04-29 19:00:24 -070012783 if (page_off == (tp->nvram_pagesize - 4))
Linus Torvalds1da177e2005-04-16 15:20:36 -070012784 nvram_cmd |= NVRAM_CMD_LAST;
12785
12786 if (i == (len - 4))
12787 nvram_cmd |= NVRAM_CMD_LAST;
12788
Matt Carlson321d32a2008-11-21 17:22:19 -080012789 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5752 &&
Joe Perches63c3a662011-04-26 08:12:10 +000012790 !tg3_flag(tp, 5755_PLUS) &&
Michael Chan4c987482005-09-05 17:52:38 -070012791 (tp->nvram_jedecnum == JEDEC_ST) &&
12792 (nvram_cmd & NVRAM_CMD_FIRST)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012793
12794 if ((ret = tg3_nvram_exec_cmd(tp,
12795 NVRAM_CMD_WREN | NVRAM_CMD_GO |
12796 NVRAM_CMD_DONE)))
12797
12798 break;
12799 }
Joe Perches63c3a662011-04-26 08:12:10 +000012800 if (!tg3_flag(tp, FLASH)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012801 /* We always do complete word writes to eeprom. */
12802 nvram_cmd |= (NVRAM_CMD_FIRST | NVRAM_CMD_LAST);
12803 }
12804
12805 if ((ret = tg3_nvram_exec_cmd(tp, nvram_cmd)))
12806 break;
12807 }
12808 return ret;
12809}
12810
12811/* offset and length are dword aligned */
12812static int tg3_nvram_write_block(struct tg3 *tp, u32 offset, u32 len, u8 *buf)
12813{
12814 int ret;
12815
Joe Perches63c3a662011-04-26 08:12:10 +000012816 if (tg3_flag(tp, EEPROM_WRITE_PROT)) {
Michael Chan314fba32005-04-21 17:07:04 -070012817 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl &
12818 ~GRC_LCLCTRL_GPIO_OUTPUT1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012819 udelay(40);
12820 }
12821
Joe Perches63c3a662011-04-26 08:12:10 +000012822 if (!tg3_flag(tp, NVRAM)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012823 ret = tg3_nvram_write_block_using_eeprom(tp, offset, len, buf);
Matt Carlson859a5882010-04-05 10:19:28 +000012824 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012825 u32 grc_mode;
12826
Michael Chanec41c7d2006-01-17 02:40:55 -080012827 ret = tg3_nvram_lock(tp);
12828 if (ret)
12829 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012830
Michael Chane6af3012005-04-21 17:12:05 -070012831 tg3_enable_nvram_access(tp);
Joe Perches63c3a662011-04-26 08:12:10 +000012832 if (tg3_flag(tp, 5750_PLUS) && !tg3_flag(tp, PROTECTED_NVRAM))
Linus Torvalds1da177e2005-04-16 15:20:36 -070012833 tw32(NVRAM_WRITE1, 0x406);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012834
12835 grc_mode = tr32(GRC_MODE);
12836 tw32(GRC_MODE, grc_mode | GRC_MODE_NVRAM_WR_ENABLE);
12837
Joe Perches63c3a662011-04-26 08:12:10 +000012838 if (tg3_flag(tp, NVRAM_BUFFERED) || !tg3_flag(tp, FLASH)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012839 ret = tg3_nvram_write_block_buffered(tp, offset, len,
12840 buf);
Matt Carlson859a5882010-04-05 10:19:28 +000012841 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012842 ret = tg3_nvram_write_block_unbuffered(tp, offset, len,
12843 buf);
12844 }
12845
12846 grc_mode = tr32(GRC_MODE);
12847 tw32(GRC_MODE, grc_mode & ~GRC_MODE_NVRAM_WR_ENABLE);
12848
Michael Chane6af3012005-04-21 17:12:05 -070012849 tg3_disable_nvram_access(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012850 tg3_nvram_unlock(tp);
12851 }
12852
Joe Perches63c3a662011-04-26 08:12:10 +000012853 if (tg3_flag(tp, EEPROM_WRITE_PROT)) {
Michael Chan314fba32005-04-21 17:07:04 -070012854 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012855 udelay(40);
12856 }
12857
12858 return ret;
12859}
12860
12861struct subsys_tbl_ent {
12862 u16 subsys_vendor, subsys_devid;
12863 u32 phy_id;
12864};
12865
Matt Carlson24daf2b2010-02-17 15:17:02 +000012866static struct subsys_tbl_ent subsys_id_to_phy_id[] __devinitdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012867 /* Broadcom boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000012868 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012869 TG3PCI_SUBDEVICE_ID_BROADCOM_95700A6, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012870 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012871 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A5, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012872 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012873 TG3PCI_SUBDEVICE_ID_BROADCOM_95700T6, TG3_PHY_ID_BCM8002 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012874 { TG3PCI_SUBVENDOR_ID_BROADCOM,
12875 TG3PCI_SUBDEVICE_ID_BROADCOM_95700A9, 0 },
12876 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012877 TG3PCI_SUBDEVICE_ID_BROADCOM_95701T1, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012878 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012879 TG3PCI_SUBDEVICE_ID_BROADCOM_95701T8, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012880 { TG3PCI_SUBVENDOR_ID_BROADCOM,
12881 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A7, 0 },
12882 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012883 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A10, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012884 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012885 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A12, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012886 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012887 TG3PCI_SUBDEVICE_ID_BROADCOM_95703AX1, TG3_PHY_ID_BCM5703 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012888 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012889 TG3PCI_SUBDEVICE_ID_BROADCOM_95703AX2, TG3_PHY_ID_BCM5703 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070012890
12891 /* 3com boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000012892 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012893 TG3PCI_SUBDEVICE_ID_3COM_3C996T, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012894 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012895 TG3PCI_SUBDEVICE_ID_3COM_3C996BT, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012896 { TG3PCI_SUBVENDOR_ID_3COM,
12897 TG3PCI_SUBDEVICE_ID_3COM_3C996SX, 0 },
12898 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012899 TG3PCI_SUBDEVICE_ID_3COM_3C1000T, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012900 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012901 TG3PCI_SUBDEVICE_ID_3COM_3C940BR01, TG3_PHY_ID_BCM5701 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070012902
12903 /* DELL boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000012904 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000012905 TG3PCI_SUBDEVICE_ID_DELL_VIPER, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012906 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000012907 TG3PCI_SUBDEVICE_ID_DELL_JAGUAR, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012908 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000012909 TG3PCI_SUBDEVICE_ID_DELL_MERLOT, TG3_PHY_ID_BCM5411 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012910 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000012911 TG3PCI_SUBDEVICE_ID_DELL_SLIM_MERLOT, TG3_PHY_ID_BCM5411 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070012912
12913 /* Compaq boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000012914 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000012915 TG3PCI_SUBDEVICE_ID_COMPAQ_BANSHEE, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012916 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000012917 TG3PCI_SUBDEVICE_ID_COMPAQ_BANSHEE_2, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012918 { TG3PCI_SUBVENDOR_ID_COMPAQ,
12919 TG3PCI_SUBDEVICE_ID_COMPAQ_CHANGELING, 0 },
12920 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000012921 TG3PCI_SUBDEVICE_ID_COMPAQ_NC7780, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012922 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000012923 TG3PCI_SUBDEVICE_ID_COMPAQ_NC7780_2, TG3_PHY_ID_BCM5701 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070012924
12925 /* IBM boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000012926 { TG3PCI_SUBVENDOR_ID_IBM,
12927 TG3PCI_SUBDEVICE_ID_IBM_5703SAX2, 0 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070012928};
12929
Matt Carlson24daf2b2010-02-17 15:17:02 +000012930static struct subsys_tbl_ent * __devinit tg3_lookup_by_subsys(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012931{
12932 int i;
12933
12934 for (i = 0; i < ARRAY_SIZE(subsys_id_to_phy_id); i++) {
12935 if ((subsys_id_to_phy_id[i].subsys_vendor ==
12936 tp->pdev->subsystem_vendor) &&
12937 (subsys_id_to_phy_id[i].subsys_devid ==
12938 tp->pdev->subsystem_device))
12939 return &subsys_id_to_phy_id[i];
12940 }
12941 return NULL;
12942}
12943
Michael Chan7d0c41e2005-04-21 17:06:20 -070012944static void __devinit tg3_get_eeprom_hw_cfg(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012945{
Linus Torvalds1da177e2005-04-16 15:20:36 -070012946 u32 val;
David S. Millerf49639e2006-06-09 11:58:36 -070012947
Matt Carlson79eb6902010-02-17 15:17:03 +000012948 tp->phy_id = TG3_PHY_ID_INVALID;
Michael Chan7d0c41e2005-04-21 17:06:20 -070012949 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
12950
Gary Zambranoa85feb82007-05-05 11:52:19 -070012951 /* Assume an onboard device and WOL capable by default. */
Joe Perches63c3a662011-04-26 08:12:10 +000012952 tg3_flag_set(tp, EEPROM_WRITE_PROT);
12953 tg3_flag_set(tp, WOL_CAP);
David S. Miller72b845e2006-03-14 14:11:48 -080012954
Michael Chanb5d37722006-09-27 16:06:21 -070012955 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan9d26e212006-12-07 00:21:14 -080012956 if (!(tr32(PCIE_TRANSACTION_CFG) & PCIE_TRANS_CFG_LOM)) {
Joe Perches63c3a662011-04-26 08:12:10 +000012957 tg3_flag_clear(tp, EEPROM_WRITE_PROT);
12958 tg3_flag_set(tp, IS_NIC);
Michael Chan9d26e212006-12-07 00:21:14 -080012959 }
Matt Carlson0527ba32007-10-10 18:03:30 -070012960 val = tr32(VCPU_CFGSHDW);
12961 if (val & VCPU_CFGSHDW_ASPM_DBNC)
Joe Perches63c3a662011-04-26 08:12:10 +000012962 tg3_flag_set(tp, ASPM_WORKAROUND);
Matt Carlson0527ba32007-10-10 18:03:30 -070012963 if ((val & VCPU_CFGSHDW_WOL_ENABLE) &&
Rafael J. Wysocki6fdbab92011-04-28 11:02:15 +000012964 (val & VCPU_CFGSHDW_WOL_MAGPKT)) {
Joe Perches63c3a662011-04-26 08:12:10 +000012965 tg3_flag_set(tp, WOL_ENABLE);
Rafael J. Wysocki6fdbab92011-04-28 11:02:15 +000012966 device_set_wakeup_enable(&tp->pdev->dev, true);
12967 }
Matt Carlson05ac4cb2008-11-03 16:53:46 -080012968 goto done;
Michael Chanb5d37722006-09-27 16:06:21 -070012969 }
12970
Linus Torvalds1da177e2005-04-16 15:20:36 -070012971 tg3_read_mem(tp, NIC_SRAM_DATA_SIG, &val);
12972 if (val == NIC_SRAM_DATA_SIG_MAGIC) {
12973 u32 nic_cfg, led_cfg;
Matt Carlsona9daf362008-05-25 23:49:44 -070012974 u32 nic_phy_id, ver, cfg2 = 0, cfg4 = 0, eeprom_phy_id;
Michael Chan7d0c41e2005-04-21 17:06:20 -070012975 int eeprom_phy_serdes = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012976
12977 tg3_read_mem(tp, NIC_SRAM_DATA_CFG, &nic_cfg);
12978 tp->nic_sram_data_cfg = nic_cfg;
12979
12980 tg3_read_mem(tp, NIC_SRAM_DATA_VER, &ver);
12981 ver >>= NIC_SRAM_DATA_VER_SHIFT;
Matt Carlson6ff6f812011-05-19 12:12:54 +000012982 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
12983 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701 &&
12984 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5703 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070012985 (ver > 0) && (ver < 0x100))
12986 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_2, &cfg2);
12987
Matt Carlsona9daf362008-05-25 23:49:44 -070012988 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
12989 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_4, &cfg4);
12990
Linus Torvalds1da177e2005-04-16 15:20:36 -070012991 if ((nic_cfg & NIC_SRAM_DATA_CFG_PHY_TYPE_MASK) ==
12992 NIC_SRAM_DATA_CFG_PHY_TYPE_FIBER)
12993 eeprom_phy_serdes = 1;
12994
12995 tg3_read_mem(tp, NIC_SRAM_DATA_PHY_ID, &nic_phy_id);
12996 if (nic_phy_id != 0) {
12997 u32 id1 = nic_phy_id & NIC_SRAM_DATA_PHY_ID1_MASK;
12998 u32 id2 = nic_phy_id & NIC_SRAM_DATA_PHY_ID2_MASK;
12999
13000 eeprom_phy_id = (id1 >> 16) << 10;
13001 eeprom_phy_id |= (id2 & 0xfc00) << 16;
13002 eeprom_phy_id |= (id2 & 0x03ff) << 0;
13003 } else
13004 eeprom_phy_id = 0;
13005
Michael Chan7d0c41e2005-04-21 17:06:20 -070013006 tp->phy_id = eeprom_phy_id;
Michael Chan747e8f82005-07-25 12:33:22 -070013007 if (eeprom_phy_serdes) {
Joe Perches63c3a662011-04-26 08:12:10 +000013008 if (!tg3_flag(tp, 5705_PLUS))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013009 tp->phy_flags |= TG3_PHYFLG_PHY_SERDES;
Matt Carlsona50d0792010-06-05 17:24:37 +000013010 else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013011 tp->phy_flags |= TG3_PHYFLG_MII_SERDES;
Michael Chan747e8f82005-07-25 12:33:22 -070013012 }
Michael Chan7d0c41e2005-04-21 17:06:20 -070013013
Joe Perches63c3a662011-04-26 08:12:10 +000013014 if (tg3_flag(tp, 5750_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -070013015 led_cfg = cfg2 & (NIC_SRAM_DATA_CFG_LED_MODE_MASK |
13016 SHASTA_EXT_LED_MODE_MASK);
John W. Linvillecbf46852005-04-21 17:01:29 -070013017 else
Linus Torvalds1da177e2005-04-16 15:20:36 -070013018 led_cfg = nic_cfg & NIC_SRAM_DATA_CFG_LED_MODE_MASK;
13019
13020 switch (led_cfg) {
13021 default:
13022 case NIC_SRAM_DATA_CFG_LED_MODE_PHY_1:
13023 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
13024 break;
13025
13026 case NIC_SRAM_DATA_CFG_LED_MODE_PHY_2:
13027 tp->led_ctrl = LED_CTRL_MODE_PHY_2;
13028 break;
13029
13030 case NIC_SRAM_DATA_CFG_LED_MODE_MAC:
13031 tp->led_ctrl = LED_CTRL_MODE_MAC;
Michael Chan9ba27792005-06-06 15:16:20 -070013032
13033 /* Default to PHY_1_MODE if 0 (MAC_MODE) is
13034 * read on some older 5700/5701 bootcode.
13035 */
13036 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
13037 ASIC_REV_5700 ||
13038 GET_ASIC_REV(tp->pci_chip_rev_id) ==
13039 ASIC_REV_5701)
13040 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
13041
Linus Torvalds1da177e2005-04-16 15:20:36 -070013042 break;
13043
13044 case SHASTA_EXT_LED_SHARED:
13045 tp->led_ctrl = LED_CTRL_MODE_SHARED;
13046 if (tp->pci_chip_rev_id != CHIPREV_ID_5750_A0 &&
13047 tp->pci_chip_rev_id != CHIPREV_ID_5750_A1)
13048 tp->led_ctrl |= (LED_CTRL_MODE_PHY_1 |
13049 LED_CTRL_MODE_PHY_2);
13050 break;
13051
13052 case SHASTA_EXT_LED_MAC:
13053 tp->led_ctrl = LED_CTRL_MODE_SHASTA_MAC;
13054 break;
13055
13056 case SHASTA_EXT_LED_COMBO:
13057 tp->led_ctrl = LED_CTRL_MODE_COMBO;
13058 if (tp->pci_chip_rev_id != CHIPREV_ID_5750_A0)
13059 tp->led_ctrl |= (LED_CTRL_MODE_PHY_1 |
13060 LED_CTRL_MODE_PHY_2);
13061 break;
13062
Stephen Hemminger855e1112008-04-16 16:37:28 -070013063 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013064
13065 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
13066 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) &&
13067 tp->pdev->subsystem_vendor == PCI_VENDOR_ID_DELL)
13068 tp->led_ctrl = LED_CTRL_MODE_PHY_2;
13069
Matt Carlsonb2a5c192008-04-03 21:44:44 -070013070 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX)
13071 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
Matt Carlson5f608912007-11-12 21:17:07 -080013072
Michael Chan9d26e212006-12-07 00:21:14 -080013073 if (nic_cfg & NIC_SRAM_DATA_CFG_EEPROM_WP) {
Joe Perches63c3a662011-04-26 08:12:10 +000013074 tg3_flag_set(tp, EEPROM_WRITE_PROT);
Michael Chan9d26e212006-12-07 00:21:14 -080013075 if ((tp->pdev->subsystem_vendor ==
13076 PCI_VENDOR_ID_ARIMA) &&
13077 (tp->pdev->subsystem_device == 0x205a ||
13078 tp->pdev->subsystem_device == 0x2063))
Joe Perches63c3a662011-04-26 08:12:10 +000013079 tg3_flag_clear(tp, EEPROM_WRITE_PROT);
Michael Chan9d26e212006-12-07 00:21:14 -080013080 } else {
Joe Perches63c3a662011-04-26 08:12:10 +000013081 tg3_flag_clear(tp, EEPROM_WRITE_PROT);
13082 tg3_flag_set(tp, IS_NIC);
Michael Chan9d26e212006-12-07 00:21:14 -080013083 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013084
13085 if (nic_cfg & NIC_SRAM_DATA_CFG_ASF_ENABLE) {
Joe Perches63c3a662011-04-26 08:12:10 +000013086 tg3_flag_set(tp, ENABLE_ASF);
13087 if (tg3_flag(tp, 5750_PLUS))
13088 tg3_flag_set(tp, ASF_NEW_HANDSHAKE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013089 }
Matt Carlsonb2b98d42008-11-03 16:52:32 -080013090
13091 if ((nic_cfg & NIC_SRAM_DATA_CFG_APE_ENABLE) &&
Joe Perches63c3a662011-04-26 08:12:10 +000013092 tg3_flag(tp, 5750_PLUS))
13093 tg3_flag_set(tp, ENABLE_APE);
Matt Carlsonb2b98d42008-11-03 16:52:32 -080013094
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013095 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES &&
Gary Zambranoa85feb82007-05-05 11:52:19 -070013096 !(nic_cfg & NIC_SRAM_DATA_CFG_FIBER_WOL))
Joe Perches63c3a662011-04-26 08:12:10 +000013097 tg3_flag_clear(tp, WOL_CAP);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013098
Joe Perches63c3a662011-04-26 08:12:10 +000013099 if (tg3_flag(tp, WOL_CAP) &&
Rafael J. Wysocki6fdbab92011-04-28 11:02:15 +000013100 (nic_cfg & NIC_SRAM_DATA_CFG_WOL_ENABLE)) {
Joe Perches63c3a662011-04-26 08:12:10 +000013101 tg3_flag_set(tp, WOL_ENABLE);
Rafael J. Wysocki6fdbab92011-04-28 11:02:15 +000013102 device_set_wakeup_enable(&tp->pdev->dev, true);
13103 }
Matt Carlson0527ba32007-10-10 18:03:30 -070013104
Linus Torvalds1da177e2005-04-16 15:20:36 -070013105 if (cfg2 & (1 << 17))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013106 tp->phy_flags |= TG3_PHYFLG_CAPACITIVE_COUPLING;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013107
13108 /* serdes signal pre-emphasis in register 0x590 set by */
13109 /* bootcode if bit 18 is set */
13110 if (cfg2 & (1 << 18))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013111 tp->phy_flags |= TG3_PHYFLG_SERDES_PREEMPHASIS;
Matt Carlson8ed5d972007-05-07 00:25:49 -070013112
Joe Perches63c3a662011-04-26 08:12:10 +000013113 if ((tg3_flag(tp, 57765_PLUS) ||
13114 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
13115 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX)) &&
Matt Carlson6833c042008-11-21 17:18:59 -080013116 (cfg2 & NIC_SRAM_DATA_CFG_2_APD_EN))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013117 tp->phy_flags |= TG3_PHYFLG_ENABLE_APD;
Matt Carlson6833c042008-11-21 17:18:59 -080013118
Joe Perches63c3a662011-04-26 08:12:10 +000013119 if (tg3_flag(tp, PCI_EXPRESS) &&
Matt Carlson8c69b1e2010-08-02 11:26:00 +000013120 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
Joe Perches63c3a662011-04-26 08:12:10 +000013121 !tg3_flag(tp, 57765_PLUS)) {
Matt Carlson8ed5d972007-05-07 00:25:49 -070013122 u32 cfg3;
13123
13124 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_3, &cfg3);
13125 if (cfg3 & NIC_SRAM_ASPM_DEBOUNCE)
Joe Perches63c3a662011-04-26 08:12:10 +000013126 tg3_flag_set(tp, ASPM_WORKAROUND);
Matt Carlson8ed5d972007-05-07 00:25:49 -070013127 }
Matt Carlsona9daf362008-05-25 23:49:44 -070013128
Matt Carlson14417062010-02-17 15:16:59 +000013129 if (cfg4 & NIC_SRAM_RGMII_INBAND_DISABLE)
Joe Perches63c3a662011-04-26 08:12:10 +000013130 tg3_flag_set(tp, RGMII_INBAND_DISABLE);
Matt Carlsona9daf362008-05-25 23:49:44 -070013131 if (cfg4 & NIC_SRAM_RGMII_EXT_IBND_RX_EN)
Joe Perches63c3a662011-04-26 08:12:10 +000013132 tg3_flag_set(tp, RGMII_EXT_IBND_RX_EN);
Matt Carlsona9daf362008-05-25 23:49:44 -070013133 if (cfg4 & NIC_SRAM_RGMII_EXT_IBND_TX_EN)
Joe Perches63c3a662011-04-26 08:12:10 +000013134 tg3_flag_set(tp, RGMII_EXT_IBND_TX_EN);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013135 }
Matt Carlson05ac4cb2008-11-03 16:53:46 -080013136done:
Joe Perches63c3a662011-04-26 08:12:10 +000013137 if (tg3_flag(tp, WOL_CAP))
Rafael J. Wysocki43067ed2011-02-10 06:53:09 +000013138 device_set_wakeup_enable(&tp->pdev->dev,
Joe Perches63c3a662011-04-26 08:12:10 +000013139 tg3_flag(tp, WOL_ENABLE));
Rafael J. Wysocki43067ed2011-02-10 06:53:09 +000013140 else
13141 device_set_wakeup_capable(&tp->pdev->dev, false);
Michael Chan7d0c41e2005-04-21 17:06:20 -070013142}
13143
Matt Carlsonb2a5c192008-04-03 21:44:44 -070013144static int __devinit tg3_issue_otp_command(struct tg3 *tp, u32 cmd)
13145{
13146 int i;
13147 u32 val;
13148
13149 tw32(OTP_CTRL, cmd | OTP_CTRL_OTP_CMD_START);
13150 tw32(OTP_CTRL, cmd);
13151
13152 /* Wait for up to 1 ms for command to execute. */
13153 for (i = 0; i < 100; i++) {
13154 val = tr32(OTP_STATUS);
13155 if (val & OTP_STATUS_CMD_DONE)
13156 break;
13157 udelay(10);
13158 }
13159
13160 return (val & OTP_STATUS_CMD_DONE) ? 0 : -EBUSY;
13161}
13162
13163/* Read the gphy configuration from the OTP region of the chip. The gphy
13164 * configuration is a 32-bit value that straddles the alignment boundary.
13165 * We do two 32-bit reads and then shift and merge the results.
13166 */
13167static u32 __devinit tg3_read_otp_phycfg(struct tg3 *tp)
13168{
13169 u32 bhalf_otp, thalf_otp;
13170
13171 tw32(OTP_MODE, OTP_MODE_OTP_THRU_GRC);
13172
13173 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_INIT))
13174 return 0;
13175
13176 tw32(OTP_ADDRESS, OTP_ADDRESS_MAGIC1);
13177
13178 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_READ))
13179 return 0;
13180
13181 thalf_otp = tr32(OTP_READ_DATA);
13182
13183 tw32(OTP_ADDRESS, OTP_ADDRESS_MAGIC2);
13184
13185 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_READ))
13186 return 0;
13187
13188 bhalf_otp = tr32(OTP_READ_DATA);
13189
13190 return ((thalf_otp & 0x0000ffff) << 16) | (bhalf_otp >> 16);
13191}
13192
Matt Carlsone256f8a2011-03-09 16:58:24 +000013193static void __devinit tg3_phy_init_link_config(struct tg3 *tp)
13194{
Hiroaki SHIMODA202ff1c2011-11-22 04:05:41 +000013195 u32 adv = ADVERTISED_Autoneg;
Matt Carlsone256f8a2011-03-09 16:58:24 +000013196
13197 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
13198 adv |= ADVERTISED_1000baseT_Half |
13199 ADVERTISED_1000baseT_Full;
13200
13201 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
13202 adv |= ADVERTISED_100baseT_Half |
13203 ADVERTISED_100baseT_Full |
13204 ADVERTISED_10baseT_Half |
13205 ADVERTISED_10baseT_Full |
13206 ADVERTISED_TP;
13207 else
13208 adv |= ADVERTISED_FIBRE;
13209
13210 tp->link_config.advertising = adv;
13211 tp->link_config.speed = SPEED_INVALID;
13212 tp->link_config.duplex = DUPLEX_INVALID;
13213 tp->link_config.autoneg = AUTONEG_ENABLE;
13214 tp->link_config.active_speed = SPEED_INVALID;
13215 tp->link_config.active_duplex = DUPLEX_INVALID;
13216 tp->link_config.orig_speed = SPEED_INVALID;
13217 tp->link_config.orig_duplex = DUPLEX_INVALID;
13218 tp->link_config.orig_autoneg = AUTONEG_INVALID;
13219}
13220
Michael Chan7d0c41e2005-04-21 17:06:20 -070013221static int __devinit tg3_phy_probe(struct tg3 *tp)
13222{
13223 u32 hw_phy_id_1, hw_phy_id_2;
13224 u32 hw_phy_id, hw_phy_id_masked;
13225 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013226
Matt Carlsone256f8a2011-03-09 16:58:24 +000013227 /* flow control autonegotiation is default behavior */
Joe Perches63c3a662011-04-26 08:12:10 +000013228 tg3_flag_set(tp, PAUSE_AUTONEG);
Matt Carlsone256f8a2011-03-09 16:58:24 +000013229 tp->link_config.flowctrl = FLOW_CTRL_TX | FLOW_CTRL_RX;
13230
Joe Perches63c3a662011-04-26 08:12:10 +000013231 if (tg3_flag(tp, USE_PHYLIB))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070013232 return tg3_phy_init(tp);
13233
Linus Torvalds1da177e2005-04-16 15:20:36 -070013234 /* Reading the PHY ID register can conflict with ASF
Nick Andrew877d0312009-01-26 11:06:57 +010013235 * firmware access to the PHY hardware.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013236 */
13237 err = 0;
Joe Perches63c3a662011-04-26 08:12:10 +000013238 if (tg3_flag(tp, ENABLE_ASF) || tg3_flag(tp, ENABLE_APE)) {
Matt Carlson79eb6902010-02-17 15:17:03 +000013239 hw_phy_id = hw_phy_id_masked = TG3_PHY_ID_INVALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013240 } else {
13241 /* Now read the physical PHY_ID from the chip and verify
13242 * that it is sane. If it doesn't look good, we fall back
13243 * to either the hard-coded table based PHY_ID and failing
13244 * that the value found in the eeprom area.
13245 */
13246 err |= tg3_readphy(tp, MII_PHYSID1, &hw_phy_id_1);
13247 err |= tg3_readphy(tp, MII_PHYSID2, &hw_phy_id_2);
13248
13249 hw_phy_id = (hw_phy_id_1 & 0xffff) << 10;
13250 hw_phy_id |= (hw_phy_id_2 & 0xfc00) << 16;
13251 hw_phy_id |= (hw_phy_id_2 & 0x03ff) << 0;
13252
Matt Carlson79eb6902010-02-17 15:17:03 +000013253 hw_phy_id_masked = hw_phy_id & TG3_PHY_ID_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013254 }
13255
Matt Carlson79eb6902010-02-17 15:17:03 +000013256 if (!err && TG3_KNOWN_PHY_ID(hw_phy_id_masked)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070013257 tp->phy_id = hw_phy_id;
Matt Carlson79eb6902010-02-17 15:17:03 +000013258 if (hw_phy_id_masked == TG3_PHY_ID_BCM8002)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013259 tp->phy_flags |= TG3_PHYFLG_PHY_SERDES;
Michael Chanda6b2d02005-08-19 12:54:29 -070013260 else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013261 tp->phy_flags &= ~TG3_PHYFLG_PHY_SERDES;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013262 } else {
Matt Carlson79eb6902010-02-17 15:17:03 +000013263 if (tp->phy_id != TG3_PHY_ID_INVALID) {
Michael Chan7d0c41e2005-04-21 17:06:20 -070013264 /* Do nothing, phy ID already set up in
13265 * tg3_get_eeprom_hw_cfg().
13266 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070013267 } else {
13268 struct subsys_tbl_ent *p;
13269
13270 /* No eeprom signature? Try the hardcoded
13271 * subsys device table.
13272 */
Matt Carlson24daf2b2010-02-17 15:17:02 +000013273 p = tg3_lookup_by_subsys(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013274 if (!p)
13275 return -ENODEV;
13276
13277 tp->phy_id = p->phy_id;
13278 if (!tp->phy_id ||
Matt Carlson79eb6902010-02-17 15:17:03 +000013279 tp->phy_id == TG3_PHY_ID_BCM8002)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013280 tp->phy_flags |= TG3_PHYFLG_PHY_SERDES;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013281 }
13282 }
13283
Matt Carlsona6b68da2010-12-06 08:28:52 +000013284 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES) &&
Matt Carlson5baa5e92011-07-20 10:20:53 +000013285 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
13286 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720 ||
13287 (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718 &&
Matt Carlsona6b68da2010-12-06 08:28:52 +000013288 tp->pci_chip_rev_id != CHIPREV_ID_5717_A0) ||
13289 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765 &&
13290 tp->pci_chip_rev_id != CHIPREV_ID_57765_A0)))
Matt Carlson52b02d02010-10-14 10:37:41 +000013291 tp->phy_flags |= TG3_PHYFLG_EEE_CAP;
13292
Matt Carlsone256f8a2011-03-09 16:58:24 +000013293 tg3_phy_init_link_config(tp);
13294
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013295 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES) &&
Joe Perches63c3a662011-04-26 08:12:10 +000013296 !tg3_flag(tp, ENABLE_APE) &&
13297 !tg3_flag(tp, ENABLE_ASF)) {
Matt Carlsone2bf73e2011-12-08 14:40:15 +000013298 u32 bmsr, dummy;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013299
13300 tg3_readphy(tp, MII_BMSR, &bmsr);
13301 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
13302 (bmsr & BMSR_LSTATUS))
13303 goto skip_phy_reset;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040013304
Linus Torvalds1da177e2005-04-16 15:20:36 -070013305 err = tg3_phy_reset(tp);
13306 if (err)
13307 return err;
13308
Matt Carlson42b64a42011-05-19 12:12:49 +000013309 tg3_phy_set_wirespeed(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013310
Matt Carlsone2bf73e2011-12-08 14:40:15 +000013311 if (!tg3_phy_copper_an_config_ok(tp, &dummy)) {
Matt Carlson42b64a42011-05-19 12:12:49 +000013312 tg3_phy_autoneg_cfg(tp, tp->link_config.advertising,
13313 tp->link_config.flowctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013314
13315 tg3_writephy(tp, MII_BMCR,
13316 BMCR_ANENABLE | BMCR_ANRESTART);
13317 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013318 }
13319
13320skip_phy_reset:
Matt Carlson79eb6902010-02-17 15:17:03 +000013321 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070013322 err = tg3_init_5401phy_dsp(tp);
13323 if (err)
13324 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013325
Linus Torvalds1da177e2005-04-16 15:20:36 -070013326 err = tg3_init_5401phy_dsp(tp);
13327 }
13328
Linus Torvalds1da177e2005-04-16 15:20:36 -070013329 return err;
13330}
13331
Matt Carlson184b8902010-04-05 10:19:25 +000013332static void __devinit tg3_read_vpd(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013333{
Matt Carlsona4a8bb12010-09-15 09:00:00 +000013334 u8 *vpd_data;
Matt Carlson4181b2c2010-02-26 14:04:45 +000013335 unsigned int block_end, rosize, len;
Matt Carlson535a4902011-07-20 10:20:56 +000013336 u32 vpdlen;
Matt Carlson184b8902010-04-05 10:19:25 +000013337 int j, i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013338
Matt Carlson535a4902011-07-20 10:20:56 +000013339 vpd_data = (u8 *)tg3_vpd_readblock(tp, &vpdlen);
Matt Carlsona4a8bb12010-09-15 09:00:00 +000013340 if (!vpd_data)
13341 goto out_no_vpd;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013342
Matt Carlson535a4902011-07-20 10:20:56 +000013343 i = pci_vpd_find_tag(vpd_data, 0, vpdlen, PCI_VPD_LRDT_RO_DATA);
Matt Carlson4181b2c2010-02-26 14:04:45 +000013344 if (i < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013345 goto out_not_found;
Matt Carlson4181b2c2010-02-26 14:04:45 +000013346
13347 rosize = pci_vpd_lrdt_size(&vpd_data[i]);
13348 block_end = i + PCI_VPD_LRDT_TAG_SIZE + rosize;
13349 i += PCI_VPD_LRDT_TAG_SIZE;
13350
Matt Carlson535a4902011-07-20 10:20:56 +000013351 if (block_end > vpdlen)
Matt Carlson4181b2c2010-02-26 14:04:45 +000013352 goto out_not_found;
13353
Matt Carlson184b8902010-04-05 10:19:25 +000013354 j = pci_vpd_find_info_keyword(vpd_data, i, rosize,
13355 PCI_VPD_RO_KEYWORD_MFR_ID);
13356 if (j > 0) {
13357 len = pci_vpd_info_field_size(&vpd_data[j]);
13358
13359 j += PCI_VPD_INFO_FLD_HDR_SIZE;
13360 if (j + len > block_end || len != 4 ||
13361 memcmp(&vpd_data[j], "1028", 4))
13362 goto partno;
13363
13364 j = pci_vpd_find_info_keyword(vpd_data, i, rosize,
13365 PCI_VPD_RO_KEYWORD_VENDOR0);
13366 if (j < 0)
13367 goto partno;
13368
13369 len = pci_vpd_info_field_size(&vpd_data[j]);
13370
13371 j += PCI_VPD_INFO_FLD_HDR_SIZE;
13372 if (j + len > block_end)
13373 goto partno;
13374
13375 memcpy(tp->fw_ver, &vpd_data[j], len);
Matt Carlson535a4902011-07-20 10:20:56 +000013376 strncat(tp->fw_ver, " bc ", vpdlen - len - 1);
Matt Carlson184b8902010-04-05 10:19:25 +000013377 }
13378
13379partno:
Matt Carlson4181b2c2010-02-26 14:04:45 +000013380 i = pci_vpd_find_info_keyword(vpd_data, i, rosize,
13381 PCI_VPD_RO_KEYWORD_PARTNO);
13382 if (i < 0)
13383 goto out_not_found;
13384
13385 len = pci_vpd_info_field_size(&vpd_data[i]);
13386
13387 i += PCI_VPD_INFO_FLD_HDR_SIZE;
13388 if (len > TG3_BPN_SIZE ||
Matt Carlson535a4902011-07-20 10:20:56 +000013389 (len + i) > vpdlen)
Matt Carlson4181b2c2010-02-26 14:04:45 +000013390 goto out_not_found;
13391
13392 memcpy(tp->board_part_number, &vpd_data[i], len);
13393
Linus Torvalds1da177e2005-04-16 15:20:36 -070013394out_not_found:
Matt Carlsona4a8bb12010-09-15 09:00:00 +000013395 kfree(vpd_data);
Matt Carlson37a949c2010-09-30 10:34:33 +000013396 if (tp->board_part_number[0])
Matt Carlsona4a8bb12010-09-15 09:00:00 +000013397 return;
13398
13399out_no_vpd:
Matt Carlson37a949c2010-09-30 10:34:33 +000013400 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717) {
13401 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717)
13402 strcpy(tp->board_part_number, "BCM5717");
13403 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718)
13404 strcpy(tp->board_part_number, "BCM5718");
13405 else
13406 goto nomatch;
13407 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780) {
13408 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57780)
13409 strcpy(tp->board_part_number, "BCM57780");
13410 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57760)
13411 strcpy(tp->board_part_number, "BCM57760");
13412 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57790)
13413 strcpy(tp->board_part_number, "BCM57790");
13414 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57788)
13415 strcpy(tp->board_part_number, "BCM57788");
13416 else
13417 goto nomatch;
13418 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765) {
13419 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57761)
13420 strcpy(tp->board_part_number, "BCM57761");
13421 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57765)
13422 strcpy(tp->board_part_number, "BCM57765");
13423 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57781)
13424 strcpy(tp->board_part_number, "BCM57781");
13425 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57785)
13426 strcpy(tp->board_part_number, "BCM57785");
13427 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57791)
13428 strcpy(tp->board_part_number, "BCM57791");
13429 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57795)
13430 strcpy(tp->board_part_number, "BCM57795");
13431 else
13432 goto nomatch;
13433 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chanb5d37722006-09-27 16:06:21 -070013434 strcpy(tp->board_part_number, "BCM95906");
Matt Carlson37a949c2010-09-30 10:34:33 +000013435 } else {
13436nomatch:
Michael Chanb5d37722006-09-27 16:06:21 -070013437 strcpy(tp->board_part_number, "none");
Matt Carlson37a949c2010-09-30 10:34:33 +000013438 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013439}
13440
Matt Carlson9c8a6202007-10-21 16:16:08 -070013441static int __devinit tg3_fw_img_is_valid(struct tg3 *tp, u32 offset)
13442{
13443 u32 val;
13444
Matt Carlsone4f34112009-02-25 14:25:00 +000013445 if (tg3_nvram_read(tp, offset, &val) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070013446 (val & 0xfc000000) != 0x0c000000 ||
Matt Carlsone4f34112009-02-25 14:25:00 +000013447 tg3_nvram_read(tp, offset + 4, &val) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070013448 val != 0)
13449 return 0;
13450
13451 return 1;
13452}
13453
Matt Carlsonacd9c112009-02-25 14:26:33 +000013454static void __devinit tg3_read_bc_ver(struct tg3 *tp)
13455{
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013456 u32 val, offset, start, ver_offset;
Matt Carlson75f99362010-04-05 10:19:24 +000013457 int i, dst_off;
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013458 bool newver = false;
Matt Carlsonacd9c112009-02-25 14:26:33 +000013459
13460 if (tg3_nvram_read(tp, 0xc, &offset) ||
13461 tg3_nvram_read(tp, 0x4, &start))
13462 return;
13463
13464 offset = tg3_nvram_logical_addr(tp, offset);
13465
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013466 if (tg3_nvram_read(tp, offset, &val))
Matt Carlsonacd9c112009-02-25 14:26:33 +000013467 return;
13468
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013469 if ((val & 0xfc000000) == 0x0c000000) {
13470 if (tg3_nvram_read(tp, offset + 4, &val))
Matt Carlsonacd9c112009-02-25 14:26:33 +000013471 return;
13472
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013473 if (val == 0)
13474 newver = true;
13475 }
13476
Matt Carlson75f99362010-04-05 10:19:24 +000013477 dst_off = strlen(tp->fw_ver);
13478
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013479 if (newver) {
Matt Carlson75f99362010-04-05 10:19:24 +000013480 if (TG3_VER_SIZE - dst_off < 16 ||
13481 tg3_nvram_read(tp, offset + 8, &ver_offset))
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013482 return;
13483
13484 offset = offset + ver_offset - start;
13485 for (i = 0; i < 16; i += 4) {
13486 __be32 v;
13487 if (tg3_nvram_read_be32(tp, offset + i, &v))
13488 return;
13489
Matt Carlson75f99362010-04-05 10:19:24 +000013490 memcpy(tp->fw_ver + dst_off + i, &v, sizeof(v));
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013491 }
13492 } else {
13493 u32 major, minor;
13494
13495 if (tg3_nvram_read(tp, TG3_NVM_PTREV_BCVER, &ver_offset))
13496 return;
13497
13498 major = (ver_offset & TG3_NVM_BCVER_MAJMSK) >>
13499 TG3_NVM_BCVER_MAJSFT;
13500 minor = ver_offset & TG3_NVM_BCVER_MINMSK;
Matt Carlson75f99362010-04-05 10:19:24 +000013501 snprintf(&tp->fw_ver[dst_off], TG3_VER_SIZE - dst_off,
13502 "v%d.%02d", major, minor);
Matt Carlsonacd9c112009-02-25 14:26:33 +000013503 }
13504}
13505
Matt Carlsona6f6cb12009-02-25 14:27:43 +000013506static void __devinit tg3_read_hwsb_ver(struct tg3 *tp)
13507{
13508 u32 val, major, minor;
13509
13510 /* Use native endian representation */
13511 if (tg3_nvram_read(tp, TG3_NVM_HWSB_CFG1, &val))
13512 return;
13513
13514 major = (val & TG3_NVM_HWSB_CFG1_MAJMSK) >>
13515 TG3_NVM_HWSB_CFG1_MAJSFT;
13516 minor = (val & TG3_NVM_HWSB_CFG1_MINMSK) >>
13517 TG3_NVM_HWSB_CFG1_MINSFT;
13518
13519 snprintf(&tp->fw_ver[0], 32, "sb v%d.%02d", major, minor);
13520}
13521
Matt Carlsondfe00d72008-11-21 17:19:41 -080013522static void __devinit tg3_read_sb_ver(struct tg3 *tp, u32 val)
13523{
13524 u32 offset, major, minor, build;
13525
Matt Carlson75f99362010-04-05 10:19:24 +000013526 strncat(tp->fw_ver, "sb", TG3_VER_SIZE - strlen(tp->fw_ver) - 1);
Matt Carlsondfe00d72008-11-21 17:19:41 -080013527
13528 if ((val & TG3_EEPROM_SB_FORMAT_MASK) != TG3_EEPROM_SB_FORMAT_1)
13529 return;
13530
13531 switch (val & TG3_EEPROM_SB_REVISION_MASK) {
13532 case TG3_EEPROM_SB_REVISION_0:
13533 offset = TG3_EEPROM_SB_F1R0_EDH_OFF;
13534 break;
13535 case TG3_EEPROM_SB_REVISION_2:
13536 offset = TG3_EEPROM_SB_F1R2_EDH_OFF;
13537 break;
13538 case TG3_EEPROM_SB_REVISION_3:
13539 offset = TG3_EEPROM_SB_F1R3_EDH_OFF;
13540 break;
Matt Carlsona4153d42010-02-17 15:16:56 +000013541 case TG3_EEPROM_SB_REVISION_4:
13542 offset = TG3_EEPROM_SB_F1R4_EDH_OFF;
13543 break;
13544 case TG3_EEPROM_SB_REVISION_5:
13545 offset = TG3_EEPROM_SB_F1R5_EDH_OFF;
13546 break;
Matt Carlsonbba226a2010-10-14 10:37:38 +000013547 case TG3_EEPROM_SB_REVISION_6:
13548 offset = TG3_EEPROM_SB_F1R6_EDH_OFF;
13549 break;
Matt Carlsondfe00d72008-11-21 17:19:41 -080013550 default:
13551 return;
13552 }
13553
Matt Carlsone4f34112009-02-25 14:25:00 +000013554 if (tg3_nvram_read(tp, offset, &val))
Matt Carlsondfe00d72008-11-21 17:19:41 -080013555 return;
13556
13557 build = (val & TG3_EEPROM_SB_EDH_BLD_MASK) >>
13558 TG3_EEPROM_SB_EDH_BLD_SHFT;
13559 major = (val & TG3_EEPROM_SB_EDH_MAJ_MASK) >>
13560 TG3_EEPROM_SB_EDH_MAJ_SHFT;
13561 minor = val & TG3_EEPROM_SB_EDH_MIN_MASK;
13562
13563 if (minor > 99 || build > 26)
13564 return;
13565
Matt Carlson75f99362010-04-05 10:19:24 +000013566 offset = strlen(tp->fw_ver);
13567 snprintf(&tp->fw_ver[offset], TG3_VER_SIZE - offset,
13568 " v%d.%02d", major, minor);
Matt Carlsondfe00d72008-11-21 17:19:41 -080013569
13570 if (build > 0) {
Matt Carlson75f99362010-04-05 10:19:24 +000013571 offset = strlen(tp->fw_ver);
13572 if (offset < TG3_VER_SIZE - 1)
13573 tp->fw_ver[offset] = 'a' + build - 1;
Matt Carlsondfe00d72008-11-21 17:19:41 -080013574 }
13575}
13576
Matt Carlsonacd9c112009-02-25 14:26:33 +000013577static void __devinit tg3_read_mgmtfw_ver(struct tg3 *tp)
Michael Chanc4e65752006-03-20 22:29:32 -080013578{
13579 u32 val, offset, start;
Matt Carlsonacd9c112009-02-25 14:26:33 +000013580 int i, vlen;
Matt Carlson9c8a6202007-10-21 16:16:08 -070013581
13582 for (offset = TG3_NVM_DIR_START;
13583 offset < TG3_NVM_DIR_END;
13584 offset += TG3_NVM_DIRENT_SIZE) {
Matt Carlsone4f34112009-02-25 14:25:00 +000013585 if (tg3_nvram_read(tp, offset, &val))
Matt Carlson9c8a6202007-10-21 16:16:08 -070013586 return;
13587
13588 if ((val >> TG3_NVM_DIRTYPE_SHIFT) == TG3_NVM_DIRTYPE_ASFINI)
13589 break;
13590 }
13591
13592 if (offset == TG3_NVM_DIR_END)
13593 return;
13594
Joe Perches63c3a662011-04-26 08:12:10 +000013595 if (!tg3_flag(tp, 5705_PLUS))
Matt Carlson9c8a6202007-10-21 16:16:08 -070013596 start = 0x08000000;
Matt Carlsone4f34112009-02-25 14:25:00 +000013597 else if (tg3_nvram_read(tp, offset - 4, &start))
Matt Carlson9c8a6202007-10-21 16:16:08 -070013598 return;
13599
Matt Carlsone4f34112009-02-25 14:25:00 +000013600 if (tg3_nvram_read(tp, offset + 4, &offset) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070013601 !tg3_fw_img_is_valid(tp, offset) ||
Matt Carlsone4f34112009-02-25 14:25:00 +000013602 tg3_nvram_read(tp, offset + 8, &val))
Matt Carlson9c8a6202007-10-21 16:16:08 -070013603 return;
13604
13605 offset += val - start;
13606
Matt Carlsonacd9c112009-02-25 14:26:33 +000013607 vlen = strlen(tp->fw_ver);
Matt Carlson9c8a6202007-10-21 16:16:08 -070013608
Matt Carlsonacd9c112009-02-25 14:26:33 +000013609 tp->fw_ver[vlen++] = ',';
13610 tp->fw_ver[vlen++] = ' ';
Matt Carlson9c8a6202007-10-21 16:16:08 -070013611
13612 for (i = 0; i < 4; i++) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000013613 __be32 v;
13614 if (tg3_nvram_read_be32(tp, offset, &v))
Matt Carlson9c8a6202007-10-21 16:16:08 -070013615 return;
13616
Al Virob9fc7dc2007-12-17 22:59:57 -080013617 offset += sizeof(v);
Matt Carlson9c8a6202007-10-21 16:16:08 -070013618
Matt Carlsonacd9c112009-02-25 14:26:33 +000013619 if (vlen > TG3_VER_SIZE - sizeof(v)) {
13620 memcpy(&tp->fw_ver[vlen], &v, TG3_VER_SIZE - vlen);
Matt Carlson9c8a6202007-10-21 16:16:08 -070013621 break;
13622 }
13623
Matt Carlsonacd9c112009-02-25 14:26:33 +000013624 memcpy(&tp->fw_ver[vlen], &v, sizeof(v));
13625 vlen += sizeof(v);
Matt Carlson9c8a6202007-10-21 16:16:08 -070013626 }
Matt Carlsonacd9c112009-02-25 14:26:33 +000013627}
13628
Matt Carlson7fd76442009-02-25 14:27:20 +000013629static void __devinit tg3_read_dash_ver(struct tg3 *tp)
13630{
13631 int vlen;
13632 u32 apedata;
Matt Carlsonecc79642010-08-02 11:26:01 +000013633 char *fwtype;
Matt Carlson7fd76442009-02-25 14:27:20 +000013634
Joe Perches63c3a662011-04-26 08:12:10 +000013635 if (!tg3_flag(tp, ENABLE_APE) || !tg3_flag(tp, ENABLE_ASF))
Matt Carlson7fd76442009-02-25 14:27:20 +000013636 return;
13637
13638 apedata = tg3_ape_read32(tp, TG3_APE_SEG_SIG);
13639 if (apedata != APE_SEG_SIG_MAGIC)
13640 return;
13641
13642 apedata = tg3_ape_read32(tp, TG3_APE_FW_STATUS);
13643 if (!(apedata & APE_FW_STATUS_READY))
13644 return;
13645
13646 apedata = tg3_ape_read32(tp, TG3_APE_FW_VERSION);
13647
Matt Carlsondc6d0742010-09-15 08:59:55 +000013648 if (tg3_ape_read32(tp, TG3_APE_FW_FEATURES) & TG3_APE_FW_FEATURE_NCSI) {
Joe Perches63c3a662011-04-26 08:12:10 +000013649 tg3_flag_set(tp, APE_HAS_NCSI);
Matt Carlsonecc79642010-08-02 11:26:01 +000013650 fwtype = "NCSI";
Matt Carlsondc6d0742010-09-15 08:59:55 +000013651 } else {
Matt Carlsonecc79642010-08-02 11:26:01 +000013652 fwtype = "DASH";
Matt Carlsondc6d0742010-09-15 08:59:55 +000013653 }
Matt Carlsonecc79642010-08-02 11:26:01 +000013654
Matt Carlson7fd76442009-02-25 14:27:20 +000013655 vlen = strlen(tp->fw_ver);
13656
Matt Carlsonecc79642010-08-02 11:26:01 +000013657 snprintf(&tp->fw_ver[vlen], TG3_VER_SIZE - vlen, " %s v%d.%d.%d.%d",
13658 fwtype,
Matt Carlson7fd76442009-02-25 14:27:20 +000013659 (apedata & APE_FW_VERSION_MAJMSK) >> APE_FW_VERSION_MAJSFT,
13660 (apedata & APE_FW_VERSION_MINMSK) >> APE_FW_VERSION_MINSFT,
13661 (apedata & APE_FW_VERSION_REVMSK) >> APE_FW_VERSION_REVSFT,
13662 (apedata & APE_FW_VERSION_BLDMSK));
13663}
13664
Matt Carlsonacd9c112009-02-25 14:26:33 +000013665static void __devinit tg3_read_fw_ver(struct tg3 *tp)
13666{
13667 u32 val;
Matt Carlson75f99362010-04-05 10:19:24 +000013668 bool vpd_vers = false;
13669
13670 if (tp->fw_ver[0] != 0)
13671 vpd_vers = true;
Matt Carlsonacd9c112009-02-25 14:26:33 +000013672
Joe Perches63c3a662011-04-26 08:12:10 +000013673 if (tg3_flag(tp, NO_NVRAM)) {
Matt Carlson75f99362010-04-05 10:19:24 +000013674 strcat(tp->fw_ver, "sb");
Matt Carlsondf259d82009-04-20 06:57:14 +000013675 return;
13676 }
13677
Matt Carlsonacd9c112009-02-25 14:26:33 +000013678 if (tg3_nvram_read(tp, 0, &val))
13679 return;
13680
13681 if (val == TG3_EEPROM_MAGIC)
13682 tg3_read_bc_ver(tp);
13683 else if ((val & TG3_EEPROM_MAGIC_FW_MSK) == TG3_EEPROM_MAGIC_FW)
13684 tg3_read_sb_ver(tp, val);
Matt Carlsona6f6cb12009-02-25 14:27:43 +000013685 else if ((val & TG3_EEPROM_MAGIC_HW_MSK) == TG3_EEPROM_MAGIC_HW)
13686 tg3_read_hwsb_ver(tp);
Matt Carlsonacd9c112009-02-25 14:26:33 +000013687 else
13688 return;
13689
Matt Carlsonc9cab242011-07-13 09:27:27 +000013690 if (vpd_vers)
Matt Carlson75f99362010-04-05 10:19:24 +000013691 goto done;
Matt Carlsonacd9c112009-02-25 14:26:33 +000013692
Matt Carlsonc9cab242011-07-13 09:27:27 +000013693 if (tg3_flag(tp, ENABLE_APE)) {
13694 if (tg3_flag(tp, ENABLE_ASF))
13695 tg3_read_dash_ver(tp);
13696 } else if (tg3_flag(tp, ENABLE_ASF)) {
13697 tg3_read_mgmtfw_ver(tp);
13698 }
Matt Carlson9c8a6202007-10-21 16:16:08 -070013699
Matt Carlson75f99362010-04-05 10:19:24 +000013700done:
Matt Carlson9c8a6202007-10-21 16:16:08 -070013701 tp->fw_ver[TG3_VER_SIZE - 1] = 0;
Michael Chanc4e65752006-03-20 22:29:32 -080013702}
13703
Michael Chan7544b092007-05-05 13:08:32 -070013704static struct pci_dev * __devinit tg3_find_peer(struct tg3 *);
13705
Matt Carlson7cb32cf2010-09-30 10:34:36 +000013706static inline u32 tg3_rx_ret_ring_size(struct tg3 *tp)
13707{
Joe Perches63c3a662011-04-26 08:12:10 +000013708 if (tg3_flag(tp, LRG_PROD_RING_CAP))
Matt Carlsonde9f5232011-04-05 14:22:43 +000013709 return TG3_RX_RET_MAX_SIZE_5717;
Joe Perches63c3a662011-04-26 08:12:10 +000013710 else if (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS))
Matt Carlsonde9f5232011-04-05 14:22:43 +000013711 return TG3_RX_RET_MAX_SIZE_5700;
Matt Carlson7cb32cf2010-09-30 10:34:36 +000013712 else
Matt Carlsonde9f5232011-04-05 14:22:43 +000013713 return TG3_RX_RET_MAX_SIZE_5705;
Matt Carlson7cb32cf2010-09-30 10:34:36 +000013714}
13715
Matt Carlson41434702011-03-09 16:58:22 +000013716static DEFINE_PCI_DEVICE_TABLE(tg3_write_reorder_chipsets) = {
Joe Perches895950c2010-12-21 02:16:08 -080013717 { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_FE_GATE_700C) },
13718 { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_8131_BRIDGE) },
13719 { PCI_DEVICE(PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_8385_0) },
13720 { },
13721};
13722
Linus Torvalds1da177e2005-04-16 15:20:36 -070013723static int __devinit tg3_get_invariants(struct tg3 *tp)
13724{
Linus Torvalds1da177e2005-04-16 15:20:36 -070013725 u32 misc_ctrl_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013726 u32 pci_state_reg, grc_misc_cfg;
13727 u32 val;
13728 u16 pci_cmd;
Matt Carlson5e7dfd02008-11-21 17:18:16 -080013729 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013730
Linus Torvalds1da177e2005-04-16 15:20:36 -070013731 /* Force memory write invalidate off. If we leave it on,
13732 * then on 5700_BX chips we have to enable a workaround.
13733 * The workaround is to set the TG3PCI_DMA_RW_CTRL boundary
13734 * to match the cacheline size. The Broadcom driver have this
13735 * workaround but turns MWI off all the times so never uses
13736 * it. This seems to suggest that the workaround is insufficient.
13737 */
13738 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
13739 pci_cmd &= ~PCI_COMMAND_INVALIDATE;
13740 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
13741
Matt Carlson16821282011-07-13 09:27:28 +000013742 /* Important! -- Make sure register accesses are byteswapped
13743 * correctly. Also, for those chips that require it, make
13744 * sure that indirect register accesses are enabled before
13745 * the first operation.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013746 */
13747 pci_read_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
13748 &misc_ctrl_reg);
Matt Carlson16821282011-07-13 09:27:28 +000013749 tp->misc_host_ctrl |= (misc_ctrl_reg &
13750 MISC_HOST_CTRL_CHIPREV);
13751 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
13752 tp->misc_host_ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013753
13754 tp->pci_chip_rev_id = (misc_ctrl_reg >>
13755 MISC_HOST_CTRL_CHIPREV_SHIFT);
Matt Carlson795d01c2007-10-07 23:28:17 -070013756 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_USE_PROD_ID_REG) {
13757 u32 prod_id_asic_rev;
13758
Matt Carlson5001e2f2009-11-13 13:03:51 +000013759 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717 ||
13760 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718 ||
Matt Carlsond78b59f2011-04-05 14:22:46 +000013761 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5719 ||
13762 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5720)
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013763 pci_read_config_dword(tp->pdev,
13764 TG3PCI_GEN2_PRODID_ASICREV,
13765 &prod_id_asic_rev);
Matt Carlsonb703df62009-12-03 08:36:21 +000013766 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57781 ||
13767 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57785 ||
13768 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57761 ||
13769 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57765 ||
13770 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57791 ||
13771 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57795)
13772 pci_read_config_dword(tp->pdev,
13773 TG3PCI_GEN15_PRODID_ASICREV,
13774 &prod_id_asic_rev);
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013775 else
13776 pci_read_config_dword(tp->pdev, TG3PCI_PRODID_ASICREV,
13777 &prod_id_asic_rev);
13778
Matt Carlson321d32a2008-11-21 17:22:19 -080013779 tp->pci_chip_rev_id = prod_id_asic_rev;
Matt Carlson795d01c2007-10-07 23:28:17 -070013780 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013781
Michael Chanff645be2005-04-21 17:09:53 -070013782 /* Wrong chip ID in 5752 A0. This code can be removed later
13783 * as A0 is not in production.
13784 */
13785 if (tp->pci_chip_rev_id == CHIPREV_ID_5752_A0_HW)
13786 tp->pci_chip_rev_id = CHIPREV_ID_5752_A0;
13787
Michael Chan68929142005-08-09 20:17:14 -070013788 /* If we have 5702/03 A1 or A2 on certain ICH chipsets,
13789 * we need to disable memory and use config. cycles
13790 * only to access all registers. The 5702/03 chips
13791 * can mistakenly decode the special cycles from the
13792 * ICH chipsets as memory write cycles, causing corruption
13793 * of register and memory space. Only certain ICH bridges
13794 * will drive special cycles with non-zero data during the
13795 * address phase which can fall within the 5703's address
13796 * range. This is not an ICH bug as the PCI spec allows
13797 * non-zero address during special cycles. However, only
13798 * these ICH bridges are known to drive non-zero addresses
13799 * during special cycles.
13800 *
13801 * Since special cycles do not cross PCI bridges, we only
13802 * enable this workaround if the 5703 is on the secondary
13803 * bus of these ICH bridges.
13804 */
13805 if ((tp->pci_chip_rev_id == CHIPREV_ID_5703_A1) ||
13806 (tp->pci_chip_rev_id == CHIPREV_ID_5703_A2)) {
13807 static struct tg3_dev_id {
13808 u32 vendor;
13809 u32 device;
13810 u32 rev;
13811 } ich_chipsets[] = {
13812 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AA_8,
13813 PCI_ANY_ID },
13814 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AB_8,
13815 PCI_ANY_ID },
13816 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_11,
13817 0xa },
13818 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_6,
13819 PCI_ANY_ID },
13820 { },
13821 };
13822 struct tg3_dev_id *pci_id = &ich_chipsets[0];
13823 struct pci_dev *bridge = NULL;
13824
13825 while (pci_id->vendor != 0) {
13826 bridge = pci_get_device(pci_id->vendor, pci_id->device,
13827 bridge);
13828 if (!bridge) {
13829 pci_id++;
13830 continue;
13831 }
13832 if (pci_id->rev != PCI_ANY_ID) {
Auke Kok44c10132007-06-08 15:46:36 -070013833 if (bridge->revision > pci_id->rev)
Michael Chan68929142005-08-09 20:17:14 -070013834 continue;
13835 }
13836 if (bridge->subordinate &&
13837 (bridge->subordinate->number ==
13838 tp->pdev->bus->number)) {
Joe Perches63c3a662011-04-26 08:12:10 +000013839 tg3_flag_set(tp, ICH_WORKAROUND);
Michael Chan68929142005-08-09 20:17:14 -070013840 pci_dev_put(bridge);
13841 break;
13842 }
13843 }
13844 }
13845
Matt Carlson6ff6f812011-05-19 12:12:54 +000013846 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
Matt Carlson41588ba2008-04-19 18:12:33 -070013847 static struct tg3_dev_id {
13848 u32 vendor;
13849 u32 device;
13850 } bridge_chipsets[] = {
13851 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PXH_0 },
13852 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PXH_1 },
13853 { },
13854 };
13855 struct tg3_dev_id *pci_id = &bridge_chipsets[0];
13856 struct pci_dev *bridge = NULL;
13857
13858 while (pci_id->vendor != 0) {
13859 bridge = pci_get_device(pci_id->vendor,
13860 pci_id->device,
13861 bridge);
13862 if (!bridge) {
13863 pci_id++;
13864 continue;
13865 }
13866 if (bridge->subordinate &&
13867 (bridge->subordinate->number <=
13868 tp->pdev->bus->number) &&
13869 (bridge->subordinate->subordinate >=
13870 tp->pdev->bus->number)) {
Joe Perches63c3a662011-04-26 08:12:10 +000013871 tg3_flag_set(tp, 5701_DMA_BUG);
Matt Carlson41588ba2008-04-19 18:12:33 -070013872 pci_dev_put(bridge);
13873 break;
13874 }
13875 }
13876 }
13877
Michael Chan4a29cc22006-03-19 13:21:12 -080013878 /* The EPB bridge inside 5714, 5715, and 5780 cannot support
13879 * DMA addresses > 40-bit. This bridge may have other additional
13880 * 57xx devices behind it in some 4-port NIC designs for example.
13881 * Any tg3 device found behind the bridge will also need the 40-bit
13882 * DMA workaround.
13883 */
Michael Chana4e2b342005-10-26 15:46:52 -070013884 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780 ||
13885 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
Joe Perches63c3a662011-04-26 08:12:10 +000013886 tg3_flag_set(tp, 5780_CLASS);
13887 tg3_flag_set(tp, 40BIT_DMA_BUG);
Michael Chan4cf78e42005-07-25 12:29:19 -070013888 tp->msi_cap = pci_find_capability(tp->pdev, PCI_CAP_ID_MSI);
Matt Carlson859a5882010-04-05 10:19:28 +000013889 } else {
Michael Chan4a29cc22006-03-19 13:21:12 -080013890 struct pci_dev *bridge = NULL;
13891
13892 do {
13893 bridge = pci_get_device(PCI_VENDOR_ID_SERVERWORKS,
13894 PCI_DEVICE_ID_SERVERWORKS_EPB,
13895 bridge);
13896 if (bridge && bridge->subordinate &&
13897 (bridge->subordinate->number <=
13898 tp->pdev->bus->number) &&
13899 (bridge->subordinate->subordinate >=
13900 tp->pdev->bus->number)) {
Joe Perches63c3a662011-04-26 08:12:10 +000013901 tg3_flag_set(tp, 40BIT_DMA_BUG);
Michael Chan4a29cc22006-03-19 13:21:12 -080013902 pci_dev_put(bridge);
13903 break;
13904 }
13905 } while (bridge);
13906 }
Michael Chan4cf78e42005-07-25 12:29:19 -070013907
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013908 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
Matt Carlson3a1e19d2011-07-13 09:27:32 +000013909 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714)
Michael Chan7544b092007-05-05 13:08:32 -070013910 tp->pdev_peer = tg3_find_peer(tp);
13911
Matt Carlsonc885e822010-08-02 11:25:57 +000013912 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
Matt Carlsond78b59f2011-04-05 14:22:46 +000013913 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
13914 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
Joe Perches63c3a662011-04-26 08:12:10 +000013915 tg3_flag_set(tp, 5717_PLUS);
Matt Carlson0a58d662011-04-05 14:22:45 +000013916
13917 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765 ||
Joe Perches63c3a662011-04-26 08:12:10 +000013918 tg3_flag(tp, 5717_PLUS))
13919 tg3_flag_set(tp, 57765_PLUS);
Matt Carlsonc885e822010-08-02 11:25:57 +000013920
Matt Carlson321d32a2008-11-21 17:22:19 -080013921 /* Intentionally exclude ASIC_REV_5906 */
13922 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
Michael Chand9ab5ad2006-03-20 22:27:35 -080013923 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787 ||
Matt Carlsond30cdd22007-10-07 23:28:35 -070013924 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson9936bcf2007-10-10 18:03:07 -070013925 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson57e69832008-05-25 23:48:31 -070013926 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013927 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
Joe Perches63c3a662011-04-26 08:12:10 +000013928 tg3_flag(tp, 57765_PLUS))
13929 tg3_flag_set(tp, 5755_PLUS);
Matt Carlson321d32a2008-11-21 17:22:19 -080013930
13931 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 ||
13932 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752 ||
Michael Chanb5d37722006-09-27 16:06:21 -070013933 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906 ||
Joe Perches63c3a662011-04-26 08:12:10 +000013934 tg3_flag(tp, 5755_PLUS) ||
13935 tg3_flag(tp, 5780_CLASS))
13936 tg3_flag_set(tp, 5750_PLUS);
John W. Linville6708e5c2005-04-21 17:00:52 -070013937
Matt Carlson6ff6f812011-05-19 12:12:54 +000013938 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 ||
Joe Perches63c3a662011-04-26 08:12:10 +000013939 tg3_flag(tp, 5750_PLUS))
13940 tg3_flag_set(tp, 5705_PLUS);
John W. Linville1b440c562005-04-21 17:03:18 -070013941
Matt Carlson507399f2009-11-13 13:03:37 +000013942 /* Determine TSO capabilities */
Matt Carlsona0512942011-07-27 14:20:54 +000013943 if (tp->pci_chip_rev_id == CHIPREV_ID_5719_A0)
Matt Carlson4d163b72011-01-25 15:58:48 +000013944 ; /* Do nothing. HW bug. */
Joe Perches63c3a662011-04-26 08:12:10 +000013945 else if (tg3_flag(tp, 57765_PLUS))
13946 tg3_flag_set(tp, HW_TSO_3);
13947 else if (tg3_flag(tp, 5755_PLUS) ||
Matt Carlsone849cdc2009-11-13 13:03:38 +000013948 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
Joe Perches63c3a662011-04-26 08:12:10 +000013949 tg3_flag_set(tp, HW_TSO_2);
13950 else if (tg3_flag(tp, 5750_PLUS)) {
13951 tg3_flag_set(tp, HW_TSO_1);
13952 tg3_flag_set(tp, TSO_BUG);
Matt Carlson507399f2009-11-13 13:03:37 +000013953 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 &&
13954 tp->pci_chip_rev_id >= CHIPREV_ID_5750_C2)
Joe Perches63c3a662011-04-26 08:12:10 +000013955 tg3_flag_clear(tp, TSO_BUG);
Matt Carlson507399f2009-11-13 13:03:37 +000013956 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
13957 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701 &&
13958 tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) {
Joe Perches63c3a662011-04-26 08:12:10 +000013959 tg3_flag_set(tp, TSO_BUG);
Matt Carlson507399f2009-11-13 13:03:37 +000013960 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705)
13961 tp->fw_needed = FIRMWARE_TG3TSO5;
13962 else
13963 tp->fw_needed = FIRMWARE_TG3TSO;
13964 }
13965
Matt Carlsondabc5c62011-05-19 12:12:52 +000013966 /* Selectively allow TSO based on operating conditions */
Matt Carlson6ff6f812011-05-19 12:12:54 +000013967 if (tg3_flag(tp, HW_TSO_1) ||
13968 tg3_flag(tp, HW_TSO_2) ||
13969 tg3_flag(tp, HW_TSO_3) ||
Matt Carlsoncf9ecf42011-11-28 09:41:03 +000013970 tp->fw_needed) {
13971 /* For firmware TSO, assume ASF is disabled.
13972 * We'll disable TSO later if we discover ASF
13973 * is enabled in tg3_get_eeprom_hw_cfg().
13974 */
Matt Carlsondabc5c62011-05-19 12:12:52 +000013975 tg3_flag_set(tp, TSO_CAPABLE);
Matt Carlsoncf9ecf42011-11-28 09:41:03 +000013976 } else {
Matt Carlsondabc5c62011-05-19 12:12:52 +000013977 tg3_flag_clear(tp, TSO_CAPABLE);
13978 tg3_flag_clear(tp, TSO_BUG);
13979 tp->fw_needed = NULL;
13980 }
13981
13982 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0)
13983 tp->fw_needed = FIRMWARE_TG3;
13984
Matt Carlson507399f2009-11-13 13:03:37 +000013985 tp->irq_max = 1;
13986
Joe Perches63c3a662011-04-26 08:12:10 +000013987 if (tg3_flag(tp, 5750_PLUS)) {
13988 tg3_flag_set(tp, SUPPORT_MSI);
Michael Chan7544b092007-05-05 13:08:32 -070013989 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_AX ||
13990 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_BX ||
13991 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714 &&
13992 tp->pci_chip_rev_id <= CHIPREV_ID_5714_A2 &&
13993 tp->pdev_peer == tp->pdev))
Joe Perches63c3a662011-04-26 08:12:10 +000013994 tg3_flag_clear(tp, SUPPORT_MSI);
Michael Chan7544b092007-05-05 13:08:32 -070013995
Joe Perches63c3a662011-04-26 08:12:10 +000013996 if (tg3_flag(tp, 5755_PLUS) ||
Michael Chanb5d37722006-09-27 16:06:21 -070013997 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Joe Perches63c3a662011-04-26 08:12:10 +000013998 tg3_flag_set(tp, 1SHOT_MSI);
Michael Chan52c0fd82006-06-29 20:15:54 -070013999 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014000
Joe Perches63c3a662011-04-26 08:12:10 +000014001 if (tg3_flag(tp, 57765_PLUS)) {
14002 tg3_flag_set(tp, SUPPORT_MSIX);
Matt Carlson507399f2009-11-13 13:03:37 +000014003 tp->irq_max = TG3_IRQ_MAX_VECS;
14004 }
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000014005 }
Matt Carlson0e1406d2009-11-02 12:33:33 +000014006
Matt Carlson2ffcc982011-05-19 12:12:44 +000014007 if (tg3_flag(tp, 5755_PLUS))
Joe Perches63c3a662011-04-26 08:12:10 +000014008 tg3_flag_set(tp, SHORT_DMA_BUG);
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000014009
Matt Carlsone31aa982011-07-27 14:20:53 +000014010 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
14011 tg3_flag_set(tp, 4K_FIFO_LIMIT);
14012
Matt Carlsonfa6b2aa2011-11-21 15:01:19 +000014013 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
14014 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
14015 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
Joe Perches63c3a662011-04-26 08:12:10 +000014016 tg3_flag_set(tp, LRG_PROD_RING_CAP);
Matt Carlsonde9f5232011-04-05 14:22:43 +000014017
Joe Perches63c3a662011-04-26 08:12:10 +000014018 if (tg3_flag(tp, 57765_PLUS) &&
Matt Carlsona0512942011-07-27 14:20:54 +000014019 tp->pci_chip_rev_id != CHIPREV_ID_5719_A0)
Joe Perches63c3a662011-04-26 08:12:10 +000014020 tg3_flag_set(tp, USE_JUMBO_BDFLAG);
Matt Carlsonb703df62009-12-03 08:36:21 +000014021
Joe Perches63c3a662011-04-26 08:12:10 +000014022 if (!tg3_flag(tp, 5705_PLUS) ||
14023 tg3_flag(tp, 5780_CLASS) ||
14024 tg3_flag(tp, USE_JUMBO_BDFLAG))
14025 tg3_flag_set(tp, JUMBO_CAPABLE);
Michael Chan0f893dc2005-07-25 12:30:38 -070014026
Matt Carlson52f44902008-11-21 17:17:04 -080014027 pci_read_config_dword(tp->pdev, TG3PCI_PCISTATE,
14028 &pci_state_reg);
14029
Jon Mason708ebb32011-06-27 12:56:50 +000014030 if (pci_is_pcie(tp->pdev)) {
Matt Carlson5e7dfd02008-11-21 17:18:16 -080014031 u16 lnkctl;
14032
Joe Perches63c3a662011-04-26 08:12:10 +000014033 tg3_flag_set(tp, PCI_EXPRESS);
Matt Carlson5f5c51e2007-11-12 21:19:37 -080014034
Matt Carlson2c55a3d2011-11-28 09:41:04 +000014035 if (tp->pci_chip_rev_id == CHIPREV_ID_5719_A0) {
14036 int readrq = pcie_get_readrq(tp->pdev);
14037 if (readrq > 2048)
14038 pcie_set_readrq(tp->pdev, 2048);
14039 }
Matt Carlson5f5c51e2007-11-12 21:19:37 -080014040
Matt Carlson5e7dfd02008-11-21 17:18:16 -080014041 pci_read_config_word(tp->pdev,
Jon Mason708ebb32011-06-27 12:56:50 +000014042 pci_pcie_cap(tp->pdev) + PCI_EXP_LNKCTL,
Matt Carlson5e7dfd02008-11-21 17:18:16 -080014043 &lnkctl);
14044 if (lnkctl & PCI_EXP_LNKCTL_CLKREQ_EN) {
Matt Carlson7196cd62011-05-19 16:02:44 +000014045 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
14046 ASIC_REV_5906) {
Joe Perches63c3a662011-04-26 08:12:10 +000014047 tg3_flag_clear(tp, HW_TSO_2);
Matt Carlsondabc5c62011-05-19 12:12:52 +000014048 tg3_flag_clear(tp, TSO_CAPABLE);
Matt Carlson7196cd62011-05-19 16:02:44 +000014049 }
Matt Carlson5e7dfd02008-11-21 17:18:16 -080014050 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson321d32a2008-11-21 17:22:19 -080014051 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson9cf74eb2009-04-20 06:58:27 +000014052 tp->pci_chip_rev_id == CHIPREV_ID_57780_A0 ||
14053 tp->pci_chip_rev_id == CHIPREV_ID_57780_A1)
Joe Perches63c3a662011-04-26 08:12:10 +000014054 tg3_flag_set(tp, CLKREQ_BUG);
Matt Carlson614b0592010-01-20 16:58:02 +000014055 } else if (tp->pci_chip_rev_id == CHIPREV_ID_5717_A0) {
Joe Perches63c3a662011-04-26 08:12:10 +000014056 tg3_flag_set(tp, L1PLLPD_EN);
Michael Chanc7835a72006-11-15 21:14:42 -080014057 }
Matt Carlson52f44902008-11-21 17:17:04 -080014058 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785) {
Jon Mason708ebb32011-06-27 12:56:50 +000014059 /* BCM5785 devices are effectively PCIe devices, and should
14060 * follow PCIe codepaths, but do not have a PCIe capabilities
14061 * section.
Matt Carlson93a700a2011-08-31 11:44:54 +000014062 */
Joe Perches63c3a662011-04-26 08:12:10 +000014063 tg3_flag_set(tp, PCI_EXPRESS);
14064 } else if (!tg3_flag(tp, 5705_PLUS) ||
14065 tg3_flag(tp, 5780_CLASS)) {
Matt Carlson52f44902008-11-21 17:17:04 -080014066 tp->pcix_cap = pci_find_capability(tp->pdev, PCI_CAP_ID_PCIX);
14067 if (!tp->pcix_cap) {
Matt Carlson2445e462010-04-05 10:19:21 +000014068 dev_err(&tp->pdev->dev,
14069 "Cannot find PCI-X capability, aborting\n");
Matt Carlson52f44902008-11-21 17:17:04 -080014070 return -EIO;
14071 }
14072
14073 if (!(pci_state_reg & PCISTATE_CONV_PCI_MODE))
Joe Perches63c3a662011-04-26 08:12:10 +000014074 tg3_flag_set(tp, PCIX_MODE);
Matt Carlson52f44902008-11-21 17:17:04 -080014075 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014076
Michael Chan399de502005-10-03 14:02:39 -070014077 /* If we have an AMD 762 or VIA K8T800 chipset, write
14078 * reordering to the mailbox registers done by the host
14079 * controller can cause major troubles. We read back from
14080 * every mailbox register write to force the writes to be
14081 * posted to the chip in order.
14082 */
Matt Carlson41434702011-03-09 16:58:22 +000014083 if (pci_dev_present(tg3_write_reorder_chipsets) &&
Joe Perches63c3a662011-04-26 08:12:10 +000014084 !tg3_flag(tp, PCI_EXPRESS))
14085 tg3_flag_set(tp, MBOX_WRITE_REORDER);
Michael Chan399de502005-10-03 14:02:39 -070014086
Matt Carlson69fc4052008-12-21 20:19:57 -080014087 pci_read_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE,
14088 &tp->pci_cacheline_sz);
14089 pci_read_config_byte(tp->pdev, PCI_LATENCY_TIMER,
14090 &tp->pci_lat_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014091 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 &&
14092 tp->pci_lat_timer < 64) {
14093 tp->pci_lat_timer = 64;
Matt Carlson69fc4052008-12-21 20:19:57 -080014094 pci_write_config_byte(tp->pdev, PCI_LATENCY_TIMER,
14095 tp->pci_lat_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014096 }
14097
Matt Carlson16821282011-07-13 09:27:28 +000014098 /* Important! -- It is critical that the PCI-X hw workaround
14099 * situation is decided before the first MMIO register access.
14100 */
Matt Carlson52f44902008-11-21 17:17:04 -080014101 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5700_BX) {
14102 /* 5700 BX chips need to have their TX producer index
14103 * mailboxes written twice to workaround a bug.
14104 */
Joe Perches63c3a662011-04-26 08:12:10 +000014105 tg3_flag_set(tp, TXD_MBOX_HWBUG);
Matt Carlson9974a352007-10-07 23:27:28 -070014106
Matt Carlson52f44902008-11-21 17:17:04 -080014107 /* If we are in PCI-X mode, enable register write workaround.
Linus Torvalds1da177e2005-04-16 15:20:36 -070014108 *
14109 * The workaround is to use indirect register accesses
14110 * for all chip writes not to mailbox registers.
14111 */
Joe Perches63c3a662011-04-26 08:12:10 +000014112 if (tg3_flag(tp, PCIX_MODE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070014113 u32 pm_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014114
Joe Perches63c3a662011-04-26 08:12:10 +000014115 tg3_flag_set(tp, PCIX_TARGET_HWBUG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014116
14117 /* The chip can have it's power management PCI config
14118 * space registers clobbered due to this bug.
14119 * So explicitly force the chip into D0 here.
14120 */
Matt Carlson9974a352007-10-07 23:27:28 -070014121 pci_read_config_dword(tp->pdev,
14122 tp->pm_cap + PCI_PM_CTRL,
Linus Torvalds1da177e2005-04-16 15:20:36 -070014123 &pm_reg);
14124 pm_reg &= ~PCI_PM_CTRL_STATE_MASK;
14125 pm_reg |= PCI_PM_CTRL_PME_ENABLE | 0 /* D0 */;
Matt Carlson9974a352007-10-07 23:27:28 -070014126 pci_write_config_dword(tp->pdev,
14127 tp->pm_cap + PCI_PM_CTRL,
Linus Torvalds1da177e2005-04-16 15:20:36 -070014128 pm_reg);
14129
14130 /* Also, force SERR#/PERR# in PCI command. */
14131 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
14132 pci_cmd |= PCI_COMMAND_PARITY | PCI_COMMAND_SERR;
14133 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
14134 }
14135 }
14136
Linus Torvalds1da177e2005-04-16 15:20:36 -070014137 if ((pci_state_reg & PCISTATE_BUS_SPEED_HIGH) != 0)
Joe Perches63c3a662011-04-26 08:12:10 +000014138 tg3_flag_set(tp, PCI_HIGH_SPEED);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014139 if ((pci_state_reg & PCISTATE_BUS_32BIT) != 0)
Joe Perches63c3a662011-04-26 08:12:10 +000014140 tg3_flag_set(tp, PCI_32BIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014141
14142 /* Chip-specific fixup from Broadcom driver */
14143 if ((tp->pci_chip_rev_id == CHIPREV_ID_5704_A0) &&
14144 (!(pci_state_reg & PCISTATE_RETRY_SAME_DMA))) {
14145 pci_state_reg |= PCISTATE_RETRY_SAME_DMA;
14146 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE, pci_state_reg);
14147 }
14148
Michael Chan1ee582d2005-08-09 20:16:46 -070014149 /* Default fast path register access methods */
Michael Chan20094932005-08-09 20:16:32 -070014150 tp->read32 = tg3_read32;
Michael Chan1ee582d2005-08-09 20:16:46 -070014151 tp->write32 = tg3_write32;
Michael Chan09ee9292005-08-09 20:17:00 -070014152 tp->read32_mbox = tg3_read32;
Michael Chan20094932005-08-09 20:16:32 -070014153 tp->write32_mbox = tg3_write32;
Michael Chan1ee582d2005-08-09 20:16:46 -070014154 tp->write32_tx_mbox = tg3_write32;
14155 tp->write32_rx_mbox = tg3_write32;
14156
14157 /* Various workaround register access methods */
Joe Perches63c3a662011-04-26 08:12:10 +000014158 if (tg3_flag(tp, PCIX_TARGET_HWBUG))
Michael Chan1ee582d2005-08-09 20:16:46 -070014159 tp->write32 = tg3_write_indirect_reg32;
Matt Carlson98efd8a2007-05-05 12:47:25 -070014160 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701 ||
Joe Perches63c3a662011-04-26 08:12:10 +000014161 (tg3_flag(tp, PCI_EXPRESS) &&
Matt Carlson98efd8a2007-05-05 12:47:25 -070014162 tp->pci_chip_rev_id == CHIPREV_ID_5750_A0)) {
14163 /*
14164 * Back to back register writes can cause problems on these
14165 * chips, the workaround is to read back all reg writes
14166 * except those to mailbox regs.
14167 *
14168 * See tg3_write_indirect_reg32().
14169 */
Michael Chan1ee582d2005-08-09 20:16:46 -070014170 tp->write32 = tg3_write_flush_reg32;
Matt Carlson98efd8a2007-05-05 12:47:25 -070014171 }
14172
Joe Perches63c3a662011-04-26 08:12:10 +000014173 if (tg3_flag(tp, TXD_MBOX_HWBUG) || tg3_flag(tp, MBOX_WRITE_REORDER)) {
Michael Chan1ee582d2005-08-09 20:16:46 -070014174 tp->write32_tx_mbox = tg3_write32_tx_mbox;
Joe Perches63c3a662011-04-26 08:12:10 +000014175 if (tg3_flag(tp, MBOX_WRITE_REORDER))
Michael Chan1ee582d2005-08-09 20:16:46 -070014176 tp->write32_rx_mbox = tg3_write_flush_reg32;
14177 }
Michael Chan20094932005-08-09 20:16:32 -070014178
Joe Perches63c3a662011-04-26 08:12:10 +000014179 if (tg3_flag(tp, ICH_WORKAROUND)) {
Michael Chan68929142005-08-09 20:17:14 -070014180 tp->read32 = tg3_read_indirect_reg32;
14181 tp->write32 = tg3_write_indirect_reg32;
14182 tp->read32_mbox = tg3_read_indirect_mbox;
14183 tp->write32_mbox = tg3_write_indirect_mbox;
14184 tp->write32_tx_mbox = tg3_write_indirect_mbox;
14185 tp->write32_rx_mbox = tg3_write_indirect_mbox;
14186
14187 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070014188 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070014189
14190 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
14191 pci_cmd &= ~PCI_COMMAND_MEMORY;
14192 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
14193 }
Michael Chanb5d37722006-09-27 16:06:21 -070014194 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
14195 tp->read32_mbox = tg3_read32_mbox_5906;
14196 tp->write32_mbox = tg3_write32_mbox_5906;
14197 tp->write32_tx_mbox = tg3_write32_mbox_5906;
14198 tp->write32_rx_mbox = tg3_write32_mbox_5906;
14199 }
Michael Chan68929142005-08-09 20:17:14 -070014200
Michael Chanbbadf502006-04-06 21:46:34 -070014201 if (tp->write32 == tg3_write_indirect_reg32 ||
Joe Perches63c3a662011-04-26 08:12:10 +000014202 (tg3_flag(tp, PCIX_MODE) &&
Michael Chanbbadf502006-04-06 21:46:34 -070014203 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
David S. Millerf49639e2006-06-09 11:58:36 -070014204 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)))
Joe Perches63c3a662011-04-26 08:12:10 +000014205 tg3_flag_set(tp, SRAM_USE_CONFIG);
Michael Chanbbadf502006-04-06 21:46:34 -070014206
Matt Carlson16821282011-07-13 09:27:28 +000014207 /* The memory arbiter has to be enabled in order for SRAM accesses
14208 * to succeed. Normally on powerup the tg3 chip firmware will make
14209 * sure it is enabled, but other entities such as system netboot
14210 * code might disable it.
14211 */
14212 val = tr32(MEMARB_MODE);
14213 tw32(MEMARB_MODE, val | MEMARB_MODE_ENABLE);
14214
Matt Carlson9dc5e342011-11-04 09:15:02 +000014215 tp->pci_fn = PCI_FUNC(tp->pdev->devfn) & 3;
14216 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
14217 tg3_flag(tp, 5780_CLASS)) {
14218 if (tg3_flag(tp, PCIX_MODE)) {
14219 pci_read_config_dword(tp->pdev,
14220 tp->pcix_cap + PCI_X_STATUS,
14221 &val);
14222 tp->pci_fn = val & 0x7;
14223 }
14224 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717) {
14225 tg3_read_mem(tp, NIC_SRAM_CPMU_STATUS, &val);
14226 if ((val & NIC_SRAM_CPMUSTAT_SIG_MSK) ==
14227 NIC_SRAM_CPMUSTAT_SIG) {
14228 tp->pci_fn = val & TG3_CPMU_STATUS_FMSK_5717;
14229 tp->pci_fn = tp->pci_fn ? 1 : 0;
14230 }
14231 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
14232 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
14233 tg3_read_mem(tp, NIC_SRAM_CPMU_STATUS, &val);
14234 if ((val & NIC_SRAM_CPMUSTAT_SIG_MSK) ==
14235 NIC_SRAM_CPMUSTAT_SIG) {
14236 tp->pci_fn = (val & TG3_CPMU_STATUS_FMSK_5719) >>
14237 TG3_CPMU_STATUS_FSHFT_5719;
14238 }
Matt Carlson69f11c92011-07-13 09:27:30 +000014239 }
14240
Michael Chan7d0c41e2005-04-21 17:06:20 -070014241 /* Get eeprom hw config before calling tg3_set_power_state().
Joe Perches63c3a662011-04-26 08:12:10 +000014242 * In particular, the TG3_FLAG_IS_NIC flag must be
Michael Chan7d0c41e2005-04-21 17:06:20 -070014243 * determined before calling tg3_set_power_state() so that
14244 * we know whether or not to switch out of Vaux power.
14245 * When the flag is set, it means that GPIO1 is used for eeprom
14246 * write protect and also implies that it is a LOM where GPIOs
14247 * are not used to switch power.
Jeff Garzik6aa20a22006-09-13 13:24:59 -040014248 */
Michael Chan7d0c41e2005-04-21 17:06:20 -070014249 tg3_get_eeprom_hw_cfg(tp);
14250
Matt Carlsoncf9ecf42011-11-28 09:41:03 +000014251 if (tp->fw_needed && tg3_flag(tp, ENABLE_ASF)) {
14252 tg3_flag_clear(tp, TSO_CAPABLE);
14253 tg3_flag_clear(tp, TSO_BUG);
14254 tp->fw_needed = NULL;
14255 }
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 Carlson859a5882010-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 Carlson859a5882010-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 Carlson859a5882010-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 Carlson859a5882010-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 Chanfdfec172005-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 Chanfdfec172005-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 Chanfdfec172005-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);