blob: 2497cc4ca89dd50643ab7e8713163a6ec1ff81de [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 Carlsonf92d9dc2010-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 Carlsonf92d9dc2010-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 Carlsonf92d9dc2010-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 Carlsonf92d9dc2010-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 Carlsoncdd4e092009-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
Michael Chan3600d912006-12-07 00:21:48 -08003771static int tg3_copper_is_advertising_all(struct tg3 *tp, u32 mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003772{
Michael Chan3600d912006-12-07 00:21:48 -08003773 u32 adv_reg, all_mask = 0;
3774
Hiroaki SHIMODA202ff1c2011-11-22 04:05:41 +00003775 all_mask = ethtool_adv_to_mii_adv_t(mask) & ADVERTISE_ALL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003776
3777 if (tg3_readphy(tp, MII_ADVERTISE, &adv_reg))
3778 return 0;
3779
Matt Carlsonb99d2a52011-08-31 11:44:47 +00003780 if ((adv_reg & ADVERTISE_ALL) != all_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003781 return 0;
Matt Carlsonb99d2a52011-08-31 11:44:47 +00003782
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003783 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003784 u32 tg3_ctrl;
3785
Matt Carlson37f07022011-11-17 14:30:55 +00003786 all_mask = ethtool_adv_to_mii_ctrl1000_t(mask);
Michael Chan3600d912006-12-07 00:21:48 -08003787
Matt Carlson221c5632011-06-13 13:39:01 +00003788 if (tg3_readphy(tp, MII_CTRL1000, &tg3_ctrl))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003789 return 0;
3790
Matt Carlsonb99d2a52011-08-31 11:44:47 +00003791 tg3_ctrl &= (ADVERTISE_1000HALF | ADVERTISE_1000FULL);
3792 if (tg3_ctrl != all_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003793 return 0;
3794 }
Matt Carlson93a700a2011-08-31 11:44:54 +00003795
Linus Torvalds1da177e2005-04-16 15:20:36 -07003796 return 1;
3797}
3798
Matt Carlsonef167e22007-12-20 20:10:01 -08003799static int tg3_adv_1000T_flowctrl_ok(struct tg3 *tp, u32 *lcladv, u32 *rmtadv)
3800{
3801 u32 curadv, reqadv;
3802
3803 if (tg3_readphy(tp, MII_ADVERTISE, lcladv))
3804 return 1;
3805
3806 curadv = *lcladv & (ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM);
3807 reqadv = tg3_advert_flowctrl_1000T(tp->link_config.flowctrl);
3808
3809 if (tp->link_config.active_duplex == DUPLEX_FULL) {
3810 if (curadv != reqadv)
3811 return 0;
3812
Joe Perches63c3a662011-04-26 08:12:10 +00003813 if (tg3_flag(tp, PAUSE_AUTONEG))
Matt Carlsonef167e22007-12-20 20:10:01 -08003814 tg3_readphy(tp, MII_LPA, rmtadv);
3815 } else {
3816 /* Reprogram the advertisement register, even if it
3817 * does not affect the current link. If the link
3818 * gets renegotiated in the future, we can save an
3819 * additional renegotiation cycle by advertising
3820 * it correctly in the first place.
3821 */
3822 if (curadv != reqadv) {
3823 *lcladv &= ~(ADVERTISE_PAUSE_CAP |
3824 ADVERTISE_PAUSE_ASYM);
3825 tg3_writephy(tp, MII_ADVERTISE, *lcladv | reqadv);
3826 }
3827 }
3828
3829 return 1;
3830}
3831
Linus Torvalds1da177e2005-04-16 15:20:36 -07003832static int tg3_setup_copper_phy(struct tg3 *tp, int force_reset)
3833{
3834 int current_link_up;
Matt Carlsonf833c4c2010-09-15 09:00:01 +00003835 u32 bmsr, val;
Matt Carlsonef167e22007-12-20 20:10:01 -08003836 u32 lcl_adv, rmt_adv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003837 u16 current_speed;
3838 u8 current_duplex;
3839 int i, err;
3840
3841 tw32(MAC_EVENT, 0);
3842
3843 tw32_f(MAC_STATUS,
3844 (MAC_STATUS_SYNC_CHANGED |
3845 MAC_STATUS_CFG_CHANGED |
3846 MAC_STATUS_MI_COMPLETION |
3847 MAC_STATUS_LNKSTATE_CHANGED));
3848 udelay(40);
3849
Matt Carlson8ef21422008-05-02 16:47:53 -07003850 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
3851 tw32_f(MAC_MI_MODE,
3852 (tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL));
3853 udelay(80);
3854 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003855
Matt Carlsonb4bd2922011-04-20 07:57:41 +00003856 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_PWRCTL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003857
3858 /* Some third-party PHYs need to be reset on link going
3859 * down.
3860 */
3861 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
3862 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
3863 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) &&
3864 netif_carrier_ok(tp->dev)) {
3865 tg3_readphy(tp, MII_BMSR, &bmsr);
3866 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
3867 !(bmsr & BMSR_LSTATUS))
3868 force_reset = 1;
3869 }
3870 if (force_reset)
3871 tg3_phy_reset(tp);
3872
Matt Carlson79eb6902010-02-17 15:17:03 +00003873 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003874 tg3_readphy(tp, MII_BMSR, &bmsr);
3875 if (tg3_readphy(tp, MII_BMSR, &bmsr) ||
Joe Perches63c3a662011-04-26 08:12:10 +00003876 !tg3_flag(tp, INIT_COMPLETE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003877 bmsr = 0;
3878
3879 if (!(bmsr & BMSR_LSTATUS)) {
3880 err = tg3_init_5401phy_dsp(tp);
3881 if (err)
3882 return err;
3883
3884 tg3_readphy(tp, MII_BMSR, &bmsr);
3885 for (i = 0; i < 1000; i++) {
3886 udelay(10);
3887 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
3888 (bmsr & BMSR_LSTATUS)) {
3889 udelay(40);
3890 break;
3891 }
3892 }
3893
Matt Carlson79eb6902010-02-17 15:17:03 +00003894 if ((tp->phy_id & TG3_PHY_ID_REV_MASK) ==
3895 TG3_PHY_REV_BCM5401_B0 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003896 !(bmsr & BMSR_LSTATUS) &&
3897 tp->link_config.active_speed == SPEED_1000) {
3898 err = tg3_phy_reset(tp);
3899 if (!err)
3900 err = tg3_init_5401phy_dsp(tp);
3901 if (err)
3902 return err;
3903 }
3904 }
3905 } else if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
3906 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0) {
3907 /* 5701 {A0,B0} CRC bug workaround */
3908 tg3_writephy(tp, 0x15, 0x0a75);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00003909 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8c68);
3910 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8d68);
3911 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8c68);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003912 }
3913
3914 /* Clear pending interrupts... */
Matt Carlsonf833c4c2010-09-15 09:00:01 +00003915 tg3_readphy(tp, MII_TG3_ISTAT, &val);
3916 tg3_readphy(tp, MII_TG3_ISTAT, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003917
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003918 if (tp->phy_flags & TG3_PHYFLG_USE_MI_INTERRUPT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003919 tg3_writephy(tp, MII_TG3_IMASK, ~MII_TG3_INT_LINKCHG);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003920 else if (!(tp->phy_flags & TG3_PHYFLG_IS_FET))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003921 tg3_writephy(tp, MII_TG3_IMASK, ~0);
3922
3923 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
3924 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
3925 if (tp->led_ctrl == LED_CTRL_MODE_PHY_1)
3926 tg3_writephy(tp, MII_TG3_EXT_CTRL,
3927 MII_TG3_EXT_CTRL_LNK3_LED_MODE);
3928 else
3929 tg3_writephy(tp, MII_TG3_EXT_CTRL, 0);
3930 }
3931
3932 current_link_up = 0;
3933 current_speed = SPEED_INVALID;
3934 current_duplex = DUPLEX_INVALID;
3935
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003936 if (tp->phy_flags & TG3_PHYFLG_CAPACITIVE_COUPLING) {
Matt Carlson15ee95c2011-04-20 07:57:40 +00003937 err = tg3_phy_auxctl_read(tp,
3938 MII_TG3_AUXCTL_SHDWSEL_MISCTEST,
3939 &val);
3940 if (!err && !(val & (1 << 10))) {
Matt Carlsonb4bd2922011-04-20 07:57:41 +00003941 tg3_phy_auxctl_write(tp,
3942 MII_TG3_AUXCTL_SHDWSEL_MISCTEST,
3943 val | (1 << 10));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003944 goto relink;
3945 }
3946 }
3947
3948 bmsr = 0;
3949 for (i = 0; i < 100; i++) {
3950 tg3_readphy(tp, MII_BMSR, &bmsr);
3951 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
3952 (bmsr & BMSR_LSTATUS))
3953 break;
3954 udelay(40);
3955 }
3956
3957 if (bmsr & BMSR_LSTATUS) {
3958 u32 aux_stat, bmcr;
3959
3960 tg3_readphy(tp, MII_TG3_AUX_STAT, &aux_stat);
3961 for (i = 0; i < 2000; i++) {
3962 udelay(10);
3963 if (!tg3_readphy(tp, MII_TG3_AUX_STAT, &aux_stat) &&
3964 aux_stat)
3965 break;
3966 }
3967
3968 tg3_aux_stat_to_speed_duplex(tp, aux_stat,
3969 &current_speed,
3970 &current_duplex);
3971
3972 bmcr = 0;
3973 for (i = 0; i < 200; i++) {
3974 tg3_readphy(tp, MII_BMCR, &bmcr);
3975 if (tg3_readphy(tp, MII_BMCR, &bmcr))
3976 continue;
3977 if (bmcr && bmcr != 0x7fff)
3978 break;
3979 udelay(10);
3980 }
3981
Matt Carlsonef167e22007-12-20 20:10:01 -08003982 lcl_adv = 0;
3983 rmt_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003984
Matt Carlsonef167e22007-12-20 20:10:01 -08003985 tp->link_config.active_speed = current_speed;
3986 tp->link_config.active_duplex = current_duplex;
3987
3988 if (tp->link_config.autoneg == AUTONEG_ENABLE) {
3989 if ((bmcr & BMCR_ANENABLE) &&
3990 tg3_copper_is_advertising_all(tp,
3991 tp->link_config.advertising)) {
3992 if (tg3_adv_1000T_flowctrl_ok(tp, &lcl_adv,
3993 &rmt_adv))
3994 current_link_up = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003995 }
3996 } else {
3997 if (!(bmcr & BMCR_ANENABLE) &&
3998 tp->link_config.speed == current_speed &&
Matt Carlsonef167e22007-12-20 20:10:01 -08003999 tp->link_config.duplex == current_duplex &&
4000 tp->link_config.flowctrl ==
4001 tp->link_config.active_flowctrl) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004002 current_link_up = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004003 }
4004 }
4005
Matt Carlsonef167e22007-12-20 20:10:01 -08004006 if (current_link_up == 1 &&
4007 tp->link_config.active_duplex == DUPLEX_FULL)
4008 tg3_setup_flow_control(tp, lcl_adv, rmt_adv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004009 }
4010
Linus Torvalds1da177e2005-04-16 15:20:36 -07004011relink:
Matt Carlson80096062010-08-02 11:26:06 +00004012 if (current_link_up == 0 || (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004013 tg3_phy_copper_begin(tp);
4014
Matt Carlsonf833c4c2010-09-15 09:00:01 +00004015 tg3_readphy(tp, MII_BMSR, &bmsr);
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00004016 if ((!tg3_readphy(tp, MII_BMSR, &bmsr) && (bmsr & BMSR_LSTATUS)) ||
4017 (tp->mac_mode & MAC_MODE_PORT_INT_LPBACK))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004018 current_link_up = 1;
4019 }
4020
4021 tp->mac_mode &= ~MAC_MODE_PORT_MODE_MASK;
4022 if (current_link_up == 1) {
4023 if (tp->link_config.active_speed == SPEED_100 ||
4024 tp->link_config.active_speed == SPEED_10)
4025 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
4026 else
4027 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004028 } else if (tp->phy_flags & TG3_PHYFLG_IS_FET)
Matt Carlson7f97a4b2009-08-25 10:10:03 +00004029 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
4030 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004031 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
4032
4033 tp->mac_mode &= ~MAC_MODE_HALF_DUPLEX;
4034 if (tp->link_config.active_duplex == DUPLEX_HALF)
4035 tp->mac_mode |= MAC_MODE_HALF_DUPLEX;
4036
Linus Torvalds1da177e2005-04-16 15:20:36 -07004037 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700) {
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07004038 if (current_link_up == 1 &&
4039 tg3_5700_link_polarity(tp, tp->link_config.active_speed))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004040 tp->mac_mode |= MAC_MODE_LINK_POLARITY;
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07004041 else
4042 tp->mac_mode &= ~MAC_MODE_LINK_POLARITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004043 }
4044
4045 /* ??? Without this setting Netgear GA302T PHY does not
4046 * ??? send/receive packets...
4047 */
Matt Carlson79eb6902010-02-17 15:17:03 +00004048 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5411 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004049 tp->pci_chip_rev_id == CHIPREV_ID_5700_ALTIMA) {
4050 tp->mi_mode |= MAC_MI_MODE_AUTO_POLL;
4051 tw32_f(MAC_MI_MODE, tp->mi_mode);
4052 udelay(80);
4053 }
4054
4055 tw32_f(MAC_MODE, tp->mac_mode);
4056 udelay(40);
4057
Matt Carlson52b02d02010-10-14 10:37:41 +00004058 tg3_phy_eee_adjust(tp, current_link_up);
4059
Joe Perches63c3a662011-04-26 08:12:10 +00004060 if (tg3_flag(tp, USE_LINKCHG_REG)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004061 /* Polled via timer. */
4062 tw32_f(MAC_EVENT, 0);
4063 } else {
4064 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
4065 }
4066 udelay(40);
4067
4068 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 &&
4069 current_link_up == 1 &&
4070 tp->link_config.active_speed == SPEED_1000 &&
Joe Perches63c3a662011-04-26 08:12:10 +00004071 (tg3_flag(tp, PCIX_MODE) || tg3_flag(tp, PCI_HIGH_SPEED))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004072 udelay(120);
4073 tw32_f(MAC_STATUS,
4074 (MAC_STATUS_SYNC_CHANGED |
4075 MAC_STATUS_CFG_CHANGED));
4076 udelay(40);
4077 tg3_write_mem(tp,
4078 NIC_SRAM_FIRMWARE_MBOX,
4079 NIC_SRAM_FIRMWARE_MBOX_MAGIC2);
4080 }
4081
Matt Carlson5e7dfd02008-11-21 17:18:16 -08004082 /* Prevent send BD corruption. */
Joe Perches63c3a662011-04-26 08:12:10 +00004083 if (tg3_flag(tp, CLKREQ_BUG)) {
Matt Carlson5e7dfd02008-11-21 17:18:16 -08004084 u16 oldlnkctl, newlnkctl;
4085
4086 pci_read_config_word(tp->pdev,
Jon Mason708ebb32011-06-27 12:56:50 +00004087 pci_pcie_cap(tp->pdev) + PCI_EXP_LNKCTL,
Matt Carlson5e7dfd02008-11-21 17:18:16 -08004088 &oldlnkctl);
4089 if (tp->link_config.active_speed == SPEED_100 ||
4090 tp->link_config.active_speed == SPEED_10)
4091 newlnkctl = oldlnkctl & ~PCI_EXP_LNKCTL_CLKREQ_EN;
4092 else
4093 newlnkctl = oldlnkctl | PCI_EXP_LNKCTL_CLKREQ_EN;
4094 if (newlnkctl != oldlnkctl)
4095 pci_write_config_word(tp->pdev,
Matt Carlson93a700a2011-08-31 11:44:54 +00004096 pci_pcie_cap(tp->pdev) +
4097 PCI_EXP_LNKCTL, newlnkctl);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08004098 }
4099
Linus Torvalds1da177e2005-04-16 15:20:36 -07004100 if (current_link_up != netif_carrier_ok(tp->dev)) {
4101 if (current_link_up)
4102 netif_carrier_on(tp->dev);
4103 else
4104 netif_carrier_off(tp->dev);
4105 tg3_link_report(tp);
4106 }
4107
4108 return 0;
4109}
4110
4111struct tg3_fiber_aneginfo {
4112 int state;
4113#define ANEG_STATE_UNKNOWN 0
4114#define ANEG_STATE_AN_ENABLE 1
4115#define ANEG_STATE_RESTART_INIT 2
4116#define ANEG_STATE_RESTART 3
4117#define ANEG_STATE_DISABLE_LINK_OK 4
4118#define ANEG_STATE_ABILITY_DETECT_INIT 5
4119#define ANEG_STATE_ABILITY_DETECT 6
4120#define ANEG_STATE_ACK_DETECT_INIT 7
4121#define ANEG_STATE_ACK_DETECT 8
4122#define ANEG_STATE_COMPLETE_ACK_INIT 9
4123#define ANEG_STATE_COMPLETE_ACK 10
4124#define ANEG_STATE_IDLE_DETECT_INIT 11
4125#define ANEG_STATE_IDLE_DETECT 12
4126#define ANEG_STATE_LINK_OK 13
4127#define ANEG_STATE_NEXT_PAGE_WAIT_INIT 14
4128#define ANEG_STATE_NEXT_PAGE_WAIT 15
4129
4130 u32 flags;
4131#define MR_AN_ENABLE 0x00000001
4132#define MR_RESTART_AN 0x00000002
4133#define MR_AN_COMPLETE 0x00000004
4134#define MR_PAGE_RX 0x00000008
4135#define MR_NP_LOADED 0x00000010
4136#define MR_TOGGLE_TX 0x00000020
4137#define MR_LP_ADV_FULL_DUPLEX 0x00000040
4138#define MR_LP_ADV_HALF_DUPLEX 0x00000080
4139#define MR_LP_ADV_SYM_PAUSE 0x00000100
4140#define MR_LP_ADV_ASYM_PAUSE 0x00000200
4141#define MR_LP_ADV_REMOTE_FAULT1 0x00000400
4142#define MR_LP_ADV_REMOTE_FAULT2 0x00000800
4143#define MR_LP_ADV_NEXT_PAGE 0x00001000
4144#define MR_TOGGLE_RX 0x00002000
4145#define MR_NP_RX 0x00004000
4146
4147#define MR_LINK_OK 0x80000000
4148
4149 unsigned long link_time, cur_time;
4150
4151 u32 ability_match_cfg;
4152 int ability_match_count;
4153
4154 char ability_match, idle_match, ack_match;
4155
4156 u32 txconfig, rxconfig;
4157#define ANEG_CFG_NP 0x00000080
4158#define ANEG_CFG_ACK 0x00000040
4159#define ANEG_CFG_RF2 0x00000020
4160#define ANEG_CFG_RF1 0x00000010
4161#define ANEG_CFG_PS2 0x00000001
4162#define ANEG_CFG_PS1 0x00008000
4163#define ANEG_CFG_HD 0x00004000
4164#define ANEG_CFG_FD 0x00002000
4165#define ANEG_CFG_INVAL 0x00001f06
4166
4167};
4168#define ANEG_OK 0
4169#define ANEG_DONE 1
4170#define ANEG_TIMER_ENAB 2
4171#define ANEG_FAILED -1
4172
4173#define ANEG_STATE_SETTLE_TIME 10000
4174
4175static int tg3_fiber_aneg_smachine(struct tg3 *tp,
4176 struct tg3_fiber_aneginfo *ap)
4177{
Matt Carlson5be73b42007-12-20 20:09:29 -08004178 u16 flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004179 unsigned long delta;
4180 u32 rx_cfg_reg;
4181 int ret;
4182
4183 if (ap->state == ANEG_STATE_UNKNOWN) {
4184 ap->rxconfig = 0;
4185 ap->link_time = 0;
4186 ap->cur_time = 0;
4187 ap->ability_match_cfg = 0;
4188 ap->ability_match_count = 0;
4189 ap->ability_match = 0;
4190 ap->idle_match = 0;
4191 ap->ack_match = 0;
4192 }
4193 ap->cur_time++;
4194
4195 if (tr32(MAC_STATUS) & MAC_STATUS_RCVD_CFG) {
4196 rx_cfg_reg = tr32(MAC_RX_AUTO_NEG);
4197
4198 if (rx_cfg_reg != ap->ability_match_cfg) {
4199 ap->ability_match_cfg = rx_cfg_reg;
4200 ap->ability_match = 0;
4201 ap->ability_match_count = 0;
4202 } else {
4203 if (++ap->ability_match_count > 1) {
4204 ap->ability_match = 1;
4205 ap->ability_match_cfg = rx_cfg_reg;
4206 }
4207 }
4208 if (rx_cfg_reg & ANEG_CFG_ACK)
4209 ap->ack_match = 1;
4210 else
4211 ap->ack_match = 0;
4212
4213 ap->idle_match = 0;
4214 } else {
4215 ap->idle_match = 1;
4216 ap->ability_match_cfg = 0;
4217 ap->ability_match_count = 0;
4218 ap->ability_match = 0;
4219 ap->ack_match = 0;
4220
4221 rx_cfg_reg = 0;
4222 }
4223
4224 ap->rxconfig = rx_cfg_reg;
4225 ret = ANEG_OK;
4226
Matt Carlson33f401a2010-04-05 10:19:27 +00004227 switch (ap->state) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004228 case ANEG_STATE_UNKNOWN:
4229 if (ap->flags & (MR_AN_ENABLE | MR_RESTART_AN))
4230 ap->state = ANEG_STATE_AN_ENABLE;
4231
4232 /* fallthru */
4233 case ANEG_STATE_AN_ENABLE:
4234 ap->flags &= ~(MR_AN_COMPLETE | MR_PAGE_RX);
4235 if (ap->flags & MR_AN_ENABLE) {
4236 ap->link_time = 0;
4237 ap->cur_time = 0;
4238 ap->ability_match_cfg = 0;
4239 ap->ability_match_count = 0;
4240 ap->ability_match = 0;
4241 ap->idle_match = 0;
4242 ap->ack_match = 0;
4243
4244 ap->state = ANEG_STATE_RESTART_INIT;
4245 } else {
4246 ap->state = ANEG_STATE_DISABLE_LINK_OK;
4247 }
4248 break;
4249
4250 case ANEG_STATE_RESTART_INIT:
4251 ap->link_time = ap->cur_time;
4252 ap->flags &= ~(MR_NP_LOADED);
4253 ap->txconfig = 0;
4254 tw32(MAC_TX_AUTO_NEG, 0);
4255 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
4256 tw32_f(MAC_MODE, tp->mac_mode);
4257 udelay(40);
4258
4259 ret = ANEG_TIMER_ENAB;
4260 ap->state = ANEG_STATE_RESTART;
4261
4262 /* fallthru */
4263 case ANEG_STATE_RESTART:
4264 delta = ap->cur_time - ap->link_time;
Matt Carlson859a5882010-04-05 10:19:28 +00004265 if (delta > ANEG_STATE_SETTLE_TIME)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004266 ap->state = ANEG_STATE_ABILITY_DETECT_INIT;
Matt Carlson859a5882010-04-05 10:19:28 +00004267 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004268 ret = ANEG_TIMER_ENAB;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004269 break;
4270
4271 case ANEG_STATE_DISABLE_LINK_OK:
4272 ret = ANEG_DONE;
4273 break;
4274
4275 case ANEG_STATE_ABILITY_DETECT_INIT:
4276 ap->flags &= ~(MR_TOGGLE_TX);
Matt Carlson5be73b42007-12-20 20:09:29 -08004277 ap->txconfig = ANEG_CFG_FD;
4278 flowctrl = tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
4279 if (flowctrl & ADVERTISE_1000XPAUSE)
4280 ap->txconfig |= ANEG_CFG_PS1;
4281 if (flowctrl & ADVERTISE_1000XPSE_ASYM)
4282 ap->txconfig |= ANEG_CFG_PS2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004283 tw32(MAC_TX_AUTO_NEG, ap->txconfig);
4284 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
4285 tw32_f(MAC_MODE, tp->mac_mode);
4286 udelay(40);
4287
4288 ap->state = ANEG_STATE_ABILITY_DETECT;
4289 break;
4290
4291 case ANEG_STATE_ABILITY_DETECT:
Matt Carlson859a5882010-04-05 10:19:28 +00004292 if (ap->ability_match != 0 && ap->rxconfig != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004293 ap->state = ANEG_STATE_ACK_DETECT_INIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004294 break;
4295
4296 case ANEG_STATE_ACK_DETECT_INIT:
4297 ap->txconfig |= ANEG_CFG_ACK;
4298 tw32(MAC_TX_AUTO_NEG, ap->txconfig);
4299 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
4300 tw32_f(MAC_MODE, tp->mac_mode);
4301 udelay(40);
4302
4303 ap->state = ANEG_STATE_ACK_DETECT;
4304
4305 /* fallthru */
4306 case ANEG_STATE_ACK_DETECT:
4307 if (ap->ack_match != 0) {
4308 if ((ap->rxconfig & ~ANEG_CFG_ACK) ==
4309 (ap->ability_match_cfg & ~ANEG_CFG_ACK)) {
4310 ap->state = ANEG_STATE_COMPLETE_ACK_INIT;
4311 } else {
4312 ap->state = ANEG_STATE_AN_ENABLE;
4313 }
4314 } else if (ap->ability_match != 0 &&
4315 ap->rxconfig == 0) {
4316 ap->state = ANEG_STATE_AN_ENABLE;
4317 }
4318 break;
4319
4320 case ANEG_STATE_COMPLETE_ACK_INIT:
4321 if (ap->rxconfig & ANEG_CFG_INVAL) {
4322 ret = ANEG_FAILED;
4323 break;
4324 }
4325 ap->flags &= ~(MR_LP_ADV_FULL_DUPLEX |
4326 MR_LP_ADV_HALF_DUPLEX |
4327 MR_LP_ADV_SYM_PAUSE |
4328 MR_LP_ADV_ASYM_PAUSE |
4329 MR_LP_ADV_REMOTE_FAULT1 |
4330 MR_LP_ADV_REMOTE_FAULT2 |
4331 MR_LP_ADV_NEXT_PAGE |
4332 MR_TOGGLE_RX |
4333 MR_NP_RX);
4334 if (ap->rxconfig & ANEG_CFG_FD)
4335 ap->flags |= MR_LP_ADV_FULL_DUPLEX;
4336 if (ap->rxconfig & ANEG_CFG_HD)
4337 ap->flags |= MR_LP_ADV_HALF_DUPLEX;
4338 if (ap->rxconfig & ANEG_CFG_PS1)
4339 ap->flags |= MR_LP_ADV_SYM_PAUSE;
4340 if (ap->rxconfig & ANEG_CFG_PS2)
4341 ap->flags |= MR_LP_ADV_ASYM_PAUSE;
4342 if (ap->rxconfig & ANEG_CFG_RF1)
4343 ap->flags |= MR_LP_ADV_REMOTE_FAULT1;
4344 if (ap->rxconfig & ANEG_CFG_RF2)
4345 ap->flags |= MR_LP_ADV_REMOTE_FAULT2;
4346 if (ap->rxconfig & ANEG_CFG_NP)
4347 ap->flags |= MR_LP_ADV_NEXT_PAGE;
4348
4349 ap->link_time = ap->cur_time;
4350
4351 ap->flags ^= (MR_TOGGLE_TX);
4352 if (ap->rxconfig & 0x0008)
4353 ap->flags |= MR_TOGGLE_RX;
4354 if (ap->rxconfig & ANEG_CFG_NP)
4355 ap->flags |= MR_NP_RX;
4356 ap->flags |= MR_PAGE_RX;
4357
4358 ap->state = ANEG_STATE_COMPLETE_ACK;
4359 ret = ANEG_TIMER_ENAB;
4360 break;
4361
4362 case ANEG_STATE_COMPLETE_ACK:
4363 if (ap->ability_match != 0 &&
4364 ap->rxconfig == 0) {
4365 ap->state = ANEG_STATE_AN_ENABLE;
4366 break;
4367 }
4368 delta = ap->cur_time - ap->link_time;
4369 if (delta > ANEG_STATE_SETTLE_TIME) {
4370 if (!(ap->flags & (MR_LP_ADV_NEXT_PAGE))) {
4371 ap->state = ANEG_STATE_IDLE_DETECT_INIT;
4372 } else {
4373 if ((ap->txconfig & ANEG_CFG_NP) == 0 &&
4374 !(ap->flags & MR_NP_RX)) {
4375 ap->state = ANEG_STATE_IDLE_DETECT_INIT;
4376 } else {
4377 ret = ANEG_FAILED;
4378 }
4379 }
4380 }
4381 break;
4382
4383 case ANEG_STATE_IDLE_DETECT_INIT:
4384 ap->link_time = ap->cur_time;
4385 tp->mac_mode &= ~MAC_MODE_SEND_CONFIGS;
4386 tw32_f(MAC_MODE, tp->mac_mode);
4387 udelay(40);
4388
4389 ap->state = ANEG_STATE_IDLE_DETECT;
4390 ret = ANEG_TIMER_ENAB;
4391 break;
4392
4393 case ANEG_STATE_IDLE_DETECT:
4394 if (ap->ability_match != 0 &&
4395 ap->rxconfig == 0) {
4396 ap->state = ANEG_STATE_AN_ENABLE;
4397 break;
4398 }
4399 delta = ap->cur_time - ap->link_time;
4400 if (delta > ANEG_STATE_SETTLE_TIME) {
4401 /* XXX another gem from the Broadcom driver :( */
4402 ap->state = ANEG_STATE_LINK_OK;
4403 }
4404 break;
4405
4406 case ANEG_STATE_LINK_OK:
4407 ap->flags |= (MR_AN_COMPLETE | MR_LINK_OK);
4408 ret = ANEG_DONE;
4409 break;
4410
4411 case ANEG_STATE_NEXT_PAGE_WAIT_INIT:
4412 /* ??? unimplemented */
4413 break;
4414
4415 case ANEG_STATE_NEXT_PAGE_WAIT:
4416 /* ??? unimplemented */
4417 break;
4418
4419 default:
4420 ret = ANEG_FAILED;
4421 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07004422 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004423
4424 return ret;
4425}
4426
Matt Carlson5be73b42007-12-20 20:09:29 -08004427static int fiber_autoneg(struct tg3 *tp, u32 *txflags, u32 *rxflags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004428{
4429 int res = 0;
4430 struct tg3_fiber_aneginfo aninfo;
4431 int status = ANEG_FAILED;
4432 unsigned int tick;
4433 u32 tmp;
4434
4435 tw32_f(MAC_TX_AUTO_NEG, 0);
4436
4437 tmp = tp->mac_mode & ~MAC_MODE_PORT_MODE_MASK;
4438 tw32_f(MAC_MODE, tmp | MAC_MODE_PORT_MODE_GMII);
4439 udelay(40);
4440
4441 tw32_f(MAC_MODE, tp->mac_mode | MAC_MODE_SEND_CONFIGS);
4442 udelay(40);
4443
4444 memset(&aninfo, 0, sizeof(aninfo));
4445 aninfo.flags |= MR_AN_ENABLE;
4446 aninfo.state = ANEG_STATE_UNKNOWN;
4447 aninfo.cur_time = 0;
4448 tick = 0;
4449 while (++tick < 195000) {
4450 status = tg3_fiber_aneg_smachine(tp, &aninfo);
4451 if (status == ANEG_DONE || status == ANEG_FAILED)
4452 break;
4453
4454 udelay(1);
4455 }
4456
4457 tp->mac_mode &= ~MAC_MODE_SEND_CONFIGS;
4458 tw32_f(MAC_MODE, tp->mac_mode);
4459 udelay(40);
4460
Matt Carlson5be73b42007-12-20 20:09:29 -08004461 *txflags = aninfo.txconfig;
4462 *rxflags = aninfo.flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004463
4464 if (status == ANEG_DONE &&
4465 (aninfo.flags & (MR_AN_COMPLETE | MR_LINK_OK |
4466 MR_LP_ADV_FULL_DUPLEX)))
4467 res = 1;
4468
4469 return res;
4470}
4471
4472static void tg3_init_bcm8002(struct tg3 *tp)
4473{
4474 u32 mac_status = tr32(MAC_STATUS);
4475 int i;
4476
4477 /* Reset when initting first time or we have a link. */
Joe Perches63c3a662011-04-26 08:12:10 +00004478 if (tg3_flag(tp, INIT_COMPLETE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004479 !(mac_status & MAC_STATUS_PCS_SYNCED))
4480 return;
4481
4482 /* Set PLL lock range. */
4483 tg3_writephy(tp, 0x16, 0x8007);
4484
4485 /* SW reset */
4486 tg3_writephy(tp, MII_BMCR, BMCR_RESET);
4487
4488 /* Wait for reset to complete. */
4489 /* XXX schedule_timeout() ... */
4490 for (i = 0; i < 500; i++)
4491 udelay(10);
4492
4493 /* Config mode; select PMA/Ch 1 regs. */
4494 tg3_writephy(tp, 0x10, 0x8411);
4495
4496 /* Enable auto-lock and comdet, select txclk for tx. */
4497 tg3_writephy(tp, 0x11, 0x0a10);
4498
4499 tg3_writephy(tp, 0x18, 0x00a0);
4500 tg3_writephy(tp, 0x16, 0x41ff);
4501
4502 /* Assert and deassert POR. */
4503 tg3_writephy(tp, 0x13, 0x0400);
4504 udelay(40);
4505 tg3_writephy(tp, 0x13, 0x0000);
4506
4507 tg3_writephy(tp, 0x11, 0x0a50);
4508 udelay(40);
4509 tg3_writephy(tp, 0x11, 0x0a10);
4510
4511 /* Wait for signal to stabilize */
4512 /* XXX schedule_timeout() ... */
4513 for (i = 0; i < 15000; i++)
4514 udelay(10);
4515
4516 /* Deselect the channel register so we can read the PHYID
4517 * later.
4518 */
4519 tg3_writephy(tp, 0x10, 0x8011);
4520}
4521
4522static int tg3_setup_fiber_hw_autoneg(struct tg3 *tp, u32 mac_status)
4523{
Matt Carlson82cd3d12007-12-20 20:09:00 -08004524 u16 flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004525 u32 sg_dig_ctrl, sg_dig_status;
4526 u32 serdes_cfg, expected_sg_dig_ctrl;
4527 int workaround, port_a;
4528 int current_link_up;
4529
4530 serdes_cfg = 0;
4531 expected_sg_dig_ctrl = 0;
4532 workaround = 0;
4533 port_a = 1;
4534 current_link_up = 0;
4535
4536 if (tp->pci_chip_rev_id != CHIPREV_ID_5704_A0 &&
4537 tp->pci_chip_rev_id != CHIPREV_ID_5704_A1) {
4538 workaround = 1;
4539 if (tr32(TG3PCI_DUAL_MAC_CTRL) & DUAL_MAC_CTRL_ID)
4540 port_a = 0;
4541
4542 /* preserve bits 0-11,13,14 for signal pre-emphasis */
4543 /* preserve bits 20-23 for voltage regulator */
4544 serdes_cfg = tr32(MAC_SERDES_CFG) & 0x00f06fff;
4545 }
4546
4547 sg_dig_ctrl = tr32(SG_DIG_CTRL);
4548
4549 if (tp->link_config.autoneg != AUTONEG_ENABLE) {
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004550 if (sg_dig_ctrl & SG_DIG_USING_HW_AUTONEG) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004551 if (workaround) {
4552 u32 val = serdes_cfg;
4553
4554 if (port_a)
4555 val |= 0xc010000;
4556 else
4557 val |= 0x4010000;
4558 tw32_f(MAC_SERDES_CFG, val);
4559 }
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004560
4561 tw32_f(SG_DIG_CTRL, SG_DIG_COMMON_SETUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004562 }
4563 if (mac_status & MAC_STATUS_PCS_SYNCED) {
4564 tg3_setup_flow_control(tp, 0, 0);
4565 current_link_up = 1;
4566 }
4567 goto out;
4568 }
4569
4570 /* Want auto-negotiation. */
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004571 expected_sg_dig_ctrl = SG_DIG_USING_HW_AUTONEG | SG_DIG_COMMON_SETUP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004572
Matt Carlson82cd3d12007-12-20 20:09:00 -08004573 flowctrl = tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
4574 if (flowctrl & ADVERTISE_1000XPAUSE)
4575 expected_sg_dig_ctrl |= SG_DIG_PAUSE_CAP;
4576 if (flowctrl & ADVERTISE_1000XPSE_ASYM)
4577 expected_sg_dig_ctrl |= SG_DIG_ASYM_PAUSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004578
4579 if (sg_dig_ctrl != expected_sg_dig_ctrl) {
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004580 if ((tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT) &&
Michael Chan3d3ebe72006-09-27 15:59:15 -07004581 tp->serdes_counter &&
4582 ((mac_status & (MAC_STATUS_PCS_SYNCED |
4583 MAC_STATUS_RCVD_CFG)) ==
4584 MAC_STATUS_PCS_SYNCED)) {
4585 tp->serdes_counter--;
4586 current_link_up = 1;
4587 goto out;
4588 }
4589restart_autoneg:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004590 if (workaround)
4591 tw32_f(MAC_SERDES_CFG, serdes_cfg | 0xc011000);
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004592 tw32_f(SG_DIG_CTRL, expected_sg_dig_ctrl | SG_DIG_SOFT_RESET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004593 udelay(5);
4594 tw32_f(SG_DIG_CTRL, expected_sg_dig_ctrl);
4595
Michael Chan3d3ebe72006-09-27 15:59:15 -07004596 tp->serdes_counter = SERDES_AN_TIMEOUT_5704S;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004597 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004598 } else if (mac_status & (MAC_STATUS_PCS_SYNCED |
4599 MAC_STATUS_SIGNAL_DET)) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07004600 sg_dig_status = tr32(SG_DIG_STATUS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004601 mac_status = tr32(MAC_STATUS);
4602
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004603 if ((sg_dig_status & SG_DIG_AUTONEG_COMPLETE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004604 (mac_status & MAC_STATUS_PCS_SYNCED)) {
Matt Carlson82cd3d12007-12-20 20:09:00 -08004605 u32 local_adv = 0, remote_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004606
Matt Carlson82cd3d12007-12-20 20:09:00 -08004607 if (sg_dig_ctrl & SG_DIG_PAUSE_CAP)
4608 local_adv |= ADVERTISE_1000XPAUSE;
4609 if (sg_dig_ctrl & SG_DIG_ASYM_PAUSE)
4610 local_adv |= ADVERTISE_1000XPSE_ASYM;
4611
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004612 if (sg_dig_status & SG_DIG_PARTNER_PAUSE_CAPABLE)
Matt Carlson82cd3d12007-12-20 20:09:00 -08004613 remote_adv |= LPA_1000XPAUSE;
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004614 if (sg_dig_status & SG_DIG_PARTNER_ASYM_PAUSE)
Matt Carlson82cd3d12007-12-20 20:09:00 -08004615 remote_adv |= LPA_1000XPAUSE_ASYM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004616
4617 tg3_setup_flow_control(tp, local_adv, remote_adv);
4618 current_link_up = 1;
Michael Chan3d3ebe72006-09-27 15:59:15 -07004619 tp->serdes_counter = 0;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004620 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004621 } else if (!(sg_dig_status & SG_DIG_AUTONEG_COMPLETE)) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07004622 if (tp->serdes_counter)
4623 tp->serdes_counter--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004624 else {
4625 if (workaround) {
4626 u32 val = serdes_cfg;
4627
4628 if (port_a)
4629 val |= 0xc010000;
4630 else
4631 val |= 0x4010000;
4632
4633 tw32_f(MAC_SERDES_CFG, val);
4634 }
4635
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004636 tw32_f(SG_DIG_CTRL, SG_DIG_COMMON_SETUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004637 udelay(40);
4638
4639 /* Link parallel detection - link is up */
4640 /* only if we have PCS_SYNC and not */
4641 /* receiving config code words */
4642 mac_status = tr32(MAC_STATUS);
4643 if ((mac_status & MAC_STATUS_PCS_SYNCED) &&
4644 !(mac_status & MAC_STATUS_RCVD_CFG)) {
4645 tg3_setup_flow_control(tp, 0, 0);
4646 current_link_up = 1;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004647 tp->phy_flags |=
4648 TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan3d3ebe72006-09-27 15:59:15 -07004649 tp->serdes_counter =
4650 SERDES_PARALLEL_DET_TIMEOUT;
4651 } else
4652 goto restart_autoneg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004653 }
4654 }
Michael Chan3d3ebe72006-09-27 15:59:15 -07004655 } else {
4656 tp->serdes_counter = SERDES_AN_TIMEOUT_5704S;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004657 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004658 }
4659
4660out:
4661 return current_link_up;
4662}
4663
4664static int tg3_setup_fiber_by_hand(struct tg3 *tp, u32 mac_status)
4665{
4666 int current_link_up = 0;
4667
Michael Chan5cf64b82007-05-05 12:11:21 -07004668 if (!(mac_status & MAC_STATUS_PCS_SYNCED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004669 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004670
4671 if (tp->link_config.autoneg == AUTONEG_ENABLE) {
Matt Carlson5be73b42007-12-20 20:09:29 -08004672 u32 txflags, rxflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004673 int i;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04004674
Matt Carlson5be73b42007-12-20 20:09:29 -08004675 if (fiber_autoneg(tp, &txflags, &rxflags)) {
4676 u32 local_adv = 0, remote_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004677
Matt Carlson5be73b42007-12-20 20:09:29 -08004678 if (txflags & ANEG_CFG_PS1)
4679 local_adv |= ADVERTISE_1000XPAUSE;
4680 if (txflags & ANEG_CFG_PS2)
4681 local_adv |= ADVERTISE_1000XPSE_ASYM;
4682
4683 if (rxflags & MR_LP_ADV_SYM_PAUSE)
4684 remote_adv |= LPA_1000XPAUSE;
4685 if (rxflags & MR_LP_ADV_ASYM_PAUSE)
4686 remote_adv |= LPA_1000XPAUSE_ASYM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004687
4688 tg3_setup_flow_control(tp, local_adv, remote_adv);
4689
Linus Torvalds1da177e2005-04-16 15:20:36 -07004690 current_link_up = 1;
4691 }
4692 for (i = 0; i < 30; i++) {
4693 udelay(20);
4694 tw32_f(MAC_STATUS,
4695 (MAC_STATUS_SYNC_CHANGED |
4696 MAC_STATUS_CFG_CHANGED));
4697 udelay(40);
4698 if ((tr32(MAC_STATUS) &
4699 (MAC_STATUS_SYNC_CHANGED |
4700 MAC_STATUS_CFG_CHANGED)) == 0)
4701 break;
4702 }
4703
4704 mac_status = tr32(MAC_STATUS);
4705 if (current_link_up == 0 &&
4706 (mac_status & MAC_STATUS_PCS_SYNCED) &&
4707 !(mac_status & MAC_STATUS_RCVD_CFG))
4708 current_link_up = 1;
4709 } else {
Matt Carlson5be73b42007-12-20 20:09:29 -08004710 tg3_setup_flow_control(tp, 0, 0);
4711
Linus Torvalds1da177e2005-04-16 15:20:36 -07004712 /* Forcing 1000FD link up. */
4713 current_link_up = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004714
4715 tw32_f(MAC_MODE, (tp->mac_mode | MAC_MODE_SEND_CONFIGS));
4716 udelay(40);
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07004717
4718 tw32_f(MAC_MODE, tp->mac_mode);
4719 udelay(40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004720 }
4721
4722out:
4723 return current_link_up;
4724}
4725
4726static int tg3_setup_fiber_phy(struct tg3 *tp, int force_reset)
4727{
4728 u32 orig_pause_cfg;
4729 u16 orig_active_speed;
4730 u8 orig_active_duplex;
4731 u32 mac_status;
4732 int current_link_up;
4733 int i;
4734
Matt Carlson8d018622007-12-20 20:05:44 -08004735 orig_pause_cfg = tp->link_config.active_flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004736 orig_active_speed = tp->link_config.active_speed;
4737 orig_active_duplex = tp->link_config.active_duplex;
4738
Joe Perches63c3a662011-04-26 08:12:10 +00004739 if (!tg3_flag(tp, HW_AUTONEG) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004740 netif_carrier_ok(tp->dev) &&
Joe Perches63c3a662011-04-26 08:12:10 +00004741 tg3_flag(tp, INIT_COMPLETE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004742 mac_status = tr32(MAC_STATUS);
4743 mac_status &= (MAC_STATUS_PCS_SYNCED |
4744 MAC_STATUS_SIGNAL_DET |
4745 MAC_STATUS_CFG_CHANGED |
4746 MAC_STATUS_RCVD_CFG);
4747 if (mac_status == (MAC_STATUS_PCS_SYNCED |
4748 MAC_STATUS_SIGNAL_DET)) {
4749 tw32_f(MAC_STATUS, (MAC_STATUS_SYNC_CHANGED |
4750 MAC_STATUS_CFG_CHANGED));
4751 return 0;
4752 }
4753 }
4754
4755 tw32_f(MAC_TX_AUTO_NEG, 0);
4756
4757 tp->mac_mode &= ~(MAC_MODE_PORT_MODE_MASK | MAC_MODE_HALF_DUPLEX);
4758 tp->mac_mode |= MAC_MODE_PORT_MODE_TBI;
4759 tw32_f(MAC_MODE, tp->mac_mode);
4760 udelay(40);
4761
Matt Carlson79eb6902010-02-17 15:17:03 +00004762 if (tp->phy_id == TG3_PHY_ID_BCM8002)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004763 tg3_init_bcm8002(tp);
4764
4765 /* Enable link change event even when serdes polling. */
4766 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
4767 udelay(40);
4768
4769 current_link_up = 0;
4770 mac_status = tr32(MAC_STATUS);
4771
Joe Perches63c3a662011-04-26 08:12:10 +00004772 if (tg3_flag(tp, HW_AUTONEG))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004773 current_link_up = tg3_setup_fiber_hw_autoneg(tp, mac_status);
4774 else
4775 current_link_up = tg3_setup_fiber_by_hand(tp, mac_status);
4776
Matt Carlson898a56f2009-08-28 14:02:40 +00004777 tp->napi[0].hw_status->status =
Linus Torvalds1da177e2005-04-16 15:20:36 -07004778 (SD_STATUS_UPDATED |
Matt Carlson898a56f2009-08-28 14:02:40 +00004779 (tp->napi[0].hw_status->status & ~SD_STATUS_LINK_CHG));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004780
4781 for (i = 0; i < 100; i++) {
4782 tw32_f(MAC_STATUS, (MAC_STATUS_SYNC_CHANGED |
4783 MAC_STATUS_CFG_CHANGED));
4784 udelay(5);
4785 if ((tr32(MAC_STATUS) & (MAC_STATUS_SYNC_CHANGED |
Michael Chan3d3ebe72006-09-27 15:59:15 -07004786 MAC_STATUS_CFG_CHANGED |
4787 MAC_STATUS_LNKSTATE_CHANGED)) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004788 break;
4789 }
4790
4791 mac_status = tr32(MAC_STATUS);
4792 if ((mac_status & MAC_STATUS_PCS_SYNCED) == 0) {
4793 current_link_up = 0;
Michael Chan3d3ebe72006-09-27 15:59:15 -07004794 if (tp->link_config.autoneg == AUTONEG_ENABLE &&
4795 tp->serdes_counter == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004796 tw32_f(MAC_MODE, (tp->mac_mode |
4797 MAC_MODE_SEND_CONFIGS));
4798 udelay(1);
4799 tw32_f(MAC_MODE, tp->mac_mode);
4800 }
4801 }
4802
4803 if (current_link_up == 1) {
4804 tp->link_config.active_speed = SPEED_1000;
4805 tp->link_config.active_duplex = DUPLEX_FULL;
4806 tw32(MAC_LED_CTRL, (tp->led_ctrl |
4807 LED_CTRL_LNKLED_OVERRIDE |
4808 LED_CTRL_1000MBPS_ON));
4809 } else {
4810 tp->link_config.active_speed = SPEED_INVALID;
4811 tp->link_config.active_duplex = DUPLEX_INVALID;
4812 tw32(MAC_LED_CTRL, (tp->led_ctrl |
4813 LED_CTRL_LNKLED_OVERRIDE |
4814 LED_CTRL_TRAFFIC_OVERRIDE));
4815 }
4816
4817 if (current_link_up != netif_carrier_ok(tp->dev)) {
4818 if (current_link_up)
4819 netif_carrier_on(tp->dev);
4820 else
4821 netif_carrier_off(tp->dev);
4822 tg3_link_report(tp);
4823 } else {
Matt Carlson8d018622007-12-20 20:05:44 -08004824 u32 now_pause_cfg = tp->link_config.active_flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004825 if (orig_pause_cfg != now_pause_cfg ||
4826 orig_active_speed != tp->link_config.active_speed ||
4827 orig_active_duplex != tp->link_config.active_duplex)
4828 tg3_link_report(tp);
4829 }
4830
4831 return 0;
4832}
4833
Michael Chan747e8f82005-07-25 12:33:22 -07004834static int tg3_setup_fiber_mii_phy(struct tg3 *tp, int force_reset)
4835{
4836 int current_link_up, err = 0;
4837 u32 bmsr, bmcr;
4838 u16 current_speed;
4839 u8 current_duplex;
Matt Carlsonef167e22007-12-20 20:10:01 -08004840 u32 local_adv, remote_adv;
Michael Chan747e8f82005-07-25 12:33:22 -07004841
4842 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
4843 tw32_f(MAC_MODE, tp->mac_mode);
4844 udelay(40);
4845
4846 tw32(MAC_EVENT, 0);
4847
4848 tw32_f(MAC_STATUS,
4849 (MAC_STATUS_SYNC_CHANGED |
4850 MAC_STATUS_CFG_CHANGED |
4851 MAC_STATUS_MI_COMPLETION |
4852 MAC_STATUS_LNKSTATE_CHANGED));
4853 udelay(40);
4854
4855 if (force_reset)
4856 tg3_phy_reset(tp);
4857
4858 current_link_up = 0;
4859 current_speed = SPEED_INVALID;
4860 current_duplex = DUPLEX_INVALID;
4861
4862 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
4863 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
Michael Chand4d2c552006-03-20 17:47:20 -08004864 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
4865 if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
4866 bmsr |= BMSR_LSTATUS;
4867 else
4868 bmsr &= ~BMSR_LSTATUS;
4869 }
Michael Chan747e8f82005-07-25 12:33:22 -07004870
4871 err |= tg3_readphy(tp, MII_BMCR, &bmcr);
4872
4873 if ((tp->link_config.autoneg == AUTONEG_ENABLE) && !force_reset &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004874 (tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT)) {
Michael Chan747e8f82005-07-25 12:33:22 -07004875 /* do nothing, just check for link up at the end */
4876 } else if (tp->link_config.autoneg == AUTONEG_ENABLE) {
Matt Carlson28011cf2011-11-16 18:36:59 -05004877 u32 adv, newadv;
Michael Chan747e8f82005-07-25 12:33:22 -07004878
4879 err |= tg3_readphy(tp, MII_ADVERTISE, &adv);
Matt Carlson28011cf2011-11-16 18:36:59 -05004880 newadv = adv & ~(ADVERTISE_1000XFULL | ADVERTISE_1000XHALF |
4881 ADVERTISE_1000XPAUSE |
4882 ADVERTISE_1000XPSE_ASYM |
4883 ADVERTISE_SLCT);
Michael Chan747e8f82005-07-25 12:33:22 -07004884
Matt Carlson28011cf2011-11-16 18:36:59 -05004885 newadv |= tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
Matt Carlson37f07022011-11-17 14:30:55 +00004886 newadv |= ethtool_adv_to_mii_adv_x(tp->link_config.advertising);
Michael Chan747e8f82005-07-25 12:33:22 -07004887
Matt Carlson28011cf2011-11-16 18:36:59 -05004888 if ((newadv != adv) || !(bmcr & BMCR_ANENABLE)) {
4889 tg3_writephy(tp, MII_ADVERTISE, newadv);
Michael Chan747e8f82005-07-25 12:33:22 -07004890 bmcr |= BMCR_ANENABLE | BMCR_ANRESTART;
4891 tg3_writephy(tp, MII_BMCR, bmcr);
4892
4893 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
Michael Chan3d3ebe72006-09-27 15:59:15 -07004894 tp->serdes_counter = SERDES_AN_TIMEOUT_5714S;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004895 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07004896
4897 return err;
4898 }
4899 } else {
4900 u32 new_bmcr;
4901
4902 bmcr &= ~BMCR_SPEED1000;
4903 new_bmcr = bmcr & ~(BMCR_ANENABLE | BMCR_FULLDPLX);
4904
4905 if (tp->link_config.duplex == DUPLEX_FULL)
4906 new_bmcr |= BMCR_FULLDPLX;
4907
4908 if (new_bmcr != bmcr) {
4909 /* BMCR_SPEED1000 is a reserved bit that needs
4910 * to be set on write.
4911 */
4912 new_bmcr |= BMCR_SPEED1000;
4913
4914 /* Force a linkdown */
4915 if (netif_carrier_ok(tp->dev)) {
4916 u32 adv;
4917
4918 err |= tg3_readphy(tp, MII_ADVERTISE, &adv);
4919 adv &= ~(ADVERTISE_1000XFULL |
4920 ADVERTISE_1000XHALF |
4921 ADVERTISE_SLCT);
4922 tg3_writephy(tp, MII_ADVERTISE, adv);
4923 tg3_writephy(tp, MII_BMCR, bmcr |
4924 BMCR_ANRESTART |
4925 BMCR_ANENABLE);
4926 udelay(10);
4927 netif_carrier_off(tp->dev);
4928 }
4929 tg3_writephy(tp, MII_BMCR, new_bmcr);
4930 bmcr = new_bmcr;
4931 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
4932 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
Michael Chand4d2c552006-03-20 17:47:20 -08004933 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
4934 ASIC_REV_5714) {
4935 if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
4936 bmsr |= BMSR_LSTATUS;
4937 else
4938 bmsr &= ~BMSR_LSTATUS;
4939 }
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004940 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07004941 }
4942 }
4943
4944 if (bmsr & BMSR_LSTATUS) {
4945 current_speed = SPEED_1000;
4946 current_link_up = 1;
4947 if (bmcr & BMCR_FULLDPLX)
4948 current_duplex = DUPLEX_FULL;
4949 else
4950 current_duplex = DUPLEX_HALF;
4951
Matt Carlsonef167e22007-12-20 20:10:01 -08004952 local_adv = 0;
4953 remote_adv = 0;
4954
Michael Chan747e8f82005-07-25 12:33:22 -07004955 if (bmcr & BMCR_ANENABLE) {
Matt Carlsonef167e22007-12-20 20:10:01 -08004956 u32 common;
Michael Chan747e8f82005-07-25 12:33:22 -07004957
4958 err |= tg3_readphy(tp, MII_ADVERTISE, &local_adv);
4959 err |= tg3_readphy(tp, MII_LPA, &remote_adv);
4960 common = local_adv & remote_adv;
4961 if (common & (ADVERTISE_1000XHALF |
4962 ADVERTISE_1000XFULL)) {
4963 if (common & ADVERTISE_1000XFULL)
4964 current_duplex = DUPLEX_FULL;
4965 else
4966 current_duplex = DUPLEX_HALF;
Joe Perches63c3a662011-04-26 08:12:10 +00004967 } else if (!tg3_flag(tp, 5780_CLASS)) {
Matt Carlson57d8b882010-06-05 17:24:35 +00004968 /* Link is up via parallel detect */
Matt Carlson859a5882010-04-05 10:19:28 +00004969 } else {
Michael Chan747e8f82005-07-25 12:33:22 -07004970 current_link_up = 0;
Matt Carlson859a5882010-04-05 10:19:28 +00004971 }
Michael Chan747e8f82005-07-25 12:33:22 -07004972 }
4973 }
4974
Matt Carlsonef167e22007-12-20 20:10:01 -08004975 if (current_link_up == 1 && current_duplex == DUPLEX_FULL)
4976 tg3_setup_flow_control(tp, local_adv, remote_adv);
4977
Michael Chan747e8f82005-07-25 12:33:22 -07004978 tp->mac_mode &= ~MAC_MODE_HALF_DUPLEX;
4979 if (tp->link_config.active_duplex == DUPLEX_HALF)
4980 tp->mac_mode |= MAC_MODE_HALF_DUPLEX;
4981
4982 tw32_f(MAC_MODE, tp->mac_mode);
4983 udelay(40);
4984
4985 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
4986
4987 tp->link_config.active_speed = current_speed;
4988 tp->link_config.active_duplex = current_duplex;
4989
4990 if (current_link_up != netif_carrier_ok(tp->dev)) {
4991 if (current_link_up)
4992 netif_carrier_on(tp->dev);
4993 else {
4994 netif_carrier_off(tp->dev);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004995 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07004996 }
4997 tg3_link_report(tp);
4998 }
4999 return err;
5000}
5001
5002static void tg3_serdes_parallel_detect(struct tg3 *tp)
5003{
Michael Chan3d3ebe72006-09-27 15:59:15 -07005004 if (tp->serdes_counter) {
Michael Chan747e8f82005-07-25 12:33:22 -07005005 /* Give autoneg time to complete. */
Michael Chan3d3ebe72006-09-27 15:59:15 -07005006 tp->serdes_counter--;
Michael Chan747e8f82005-07-25 12:33:22 -07005007 return;
5008 }
Matt Carlsonc6cdf432010-04-05 10:19:26 +00005009
Michael Chan747e8f82005-07-25 12:33:22 -07005010 if (!netif_carrier_ok(tp->dev) &&
5011 (tp->link_config.autoneg == AUTONEG_ENABLE)) {
5012 u32 bmcr;
5013
5014 tg3_readphy(tp, MII_BMCR, &bmcr);
5015 if (bmcr & BMCR_ANENABLE) {
5016 u32 phy1, phy2;
5017
5018 /* Select shadow register 0x1f */
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00005019 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x7c00);
5020 tg3_readphy(tp, MII_TG3_MISC_SHDW, &phy1);
Michael Chan747e8f82005-07-25 12:33:22 -07005021
5022 /* Select expansion interrupt status register */
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00005023 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
5024 MII_TG3_DSP_EXP1_INT_STAT);
5025 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &phy2);
5026 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &phy2);
Michael Chan747e8f82005-07-25 12:33:22 -07005027
5028 if ((phy1 & 0x10) && !(phy2 & 0x20)) {
5029 /* We have signal detect and not receiving
5030 * config code words, link is up by parallel
5031 * detection.
5032 */
5033
5034 bmcr &= ~BMCR_ANENABLE;
5035 bmcr |= BMCR_SPEED1000 | BMCR_FULLDPLX;
5036 tg3_writephy(tp, MII_BMCR, bmcr);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005037 tp->phy_flags |= TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07005038 }
5039 }
Matt Carlson859a5882010-04-05 10:19:28 +00005040 } else if (netif_carrier_ok(tp->dev) &&
5041 (tp->link_config.autoneg == AUTONEG_ENABLE) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005042 (tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT)) {
Michael Chan747e8f82005-07-25 12:33:22 -07005043 u32 phy2;
5044
5045 /* Select expansion interrupt status register */
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00005046 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
5047 MII_TG3_DSP_EXP1_INT_STAT);
5048 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &phy2);
Michael Chan747e8f82005-07-25 12:33:22 -07005049 if (phy2 & 0x20) {
5050 u32 bmcr;
5051
5052 /* Config code words received, turn on autoneg. */
5053 tg3_readphy(tp, MII_BMCR, &bmcr);
5054 tg3_writephy(tp, MII_BMCR, bmcr | BMCR_ANENABLE);
5055
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005056 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07005057
5058 }
5059 }
5060}
5061
Linus Torvalds1da177e2005-04-16 15:20:36 -07005062static int tg3_setup_phy(struct tg3 *tp, int force_reset)
5063{
Matt Carlsonf2096f92011-04-05 14:22:48 +00005064 u32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005065 int err;
5066
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005067 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005068 err = tg3_setup_fiber_phy(tp, force_reset);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005069 else if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
Michael Chan747e8f82005-07-25 12:33:22 -07005070 err = tg3_setup_fiber_mii_phy(tp, force_reset);
Matt Carlson859a5882010-04-05 10:19:28 +00005071 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07005072 err = tg3_setup_copper_phy(tp, force_reset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005073
Matt Carlsonbcb37f62008-11-03 16:52:09 -08005074 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX) {
Matt Carlsonf2096f92011-04-05 14:22:48 +00005075 u32 scale;
Matt Carlsonaa6c91f2007-11-12 21:18:04 -08005076
5077 val = tr32(TG3_CPMU_CLCK_STAT) & CPMU_CLCK_STAT_MAC_CLCK_MASK;
5078 if (val == CPMU_CLCK_STAT_MAC_CLCK_62_5)
5079 scale = 65;
5080 else if (val == CPMU_CLCK_STAT_MAC_CLCK_6_25)
5081 scale = 6;
5082 else
5083 scale = 12;
5084
5085 val = tr32(GRC_MISC_CFG) & ~GRC_MISC_CFG_PRESCALAR_MASK;
5086 val |= (scale << GRC_MISC_CFG_PRESCALAR_SHIFT);
5087 tw32(GRC_MISC_CFG, val);
5088 }
5089
Matt Carlsonf2096f92011-04-05 14:22:48 +00005090 val = (2 << TX_LENGTHS_IPG_CRS_SHIFT) |
5091 (6 << TX_LENGTHS_IPG_SHIFT);
5092 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
5093 val |= tr32(MAC_TX_LENGTHS) &
5094 (TX_LENGTHS_JMB_FRM_LEN_MSK |
5095 TX_LENGTHS_CNT_DWN_VAL_MSK);
5096
Linus Torvalds1da177e2005-04-16 15:20:36 -07005097 if (tp->link_config.active_speed == SPEED_1000 &&
5098 tp->link_config.active_duplex == DUPLEX_HALF)
Matt Carlsonf2096f92011-04-05 14:22:48 +00005099 tw32(MAC_TX_LENGTHS, val |
5100 (0xff << TX_LENGTHS_SLOT_TIME_SHIFT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005101 else
Matt Carlsonf2096f92011-04-05 14:22:48 +00005102 tw32(MAC_TX_LENGTHS, val |
5103 (32 << TX_LENGTHS_SLOT_TIME_SHIFT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005104
Joe Perches63c3a662011-04-26 08:12:10 +00005105 if (!tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005106 if (netif_carrier_ok(tp->dev)) {
5107 tw32(HOSTCC_STAT_COAL_TICKS,
David S. Miller15f98502005-05-18 22:49:26 -07005108 tp->coal.stats_block_coalesce_usecs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005109 } else {
5110 tw32(HOSTCC_STAT_COAL_TICKS, 0);
5111 }
5112 }
5113
Joe Perches63c3a662011-04-26 08:12:10 +00005114 if (tg3_flag(tp, ASPM_WORKAROUND)) {
Matt Carlsonf2096f92011-04-05 14:22:48 +00005115 val = tr32(PCIE_PWR_MGMT_THRESH);
Matt Carlson8ed5d972007-05-07 00:25:49 -07005116 if (!netif_carrier_ok(tp->dev))
5117 val = (val & ~PCIE_PWR_MGMT_L1_THRESH_MSK) |
5118 tp->pwrmgmt_thresh;
5119 else
5120 val |= PCIE_PWR_MGMT_L1_THRESH_MSK;
5121 tw32(PCIE_PWR_MGMT_THRESH, val);
5122 }
5123
Linus Torvalds1da177e2005-04-16 15:20:36 -07005124 return err;
5125}
5126
Matt Carlson66cfd1b2010-09-30 10:34:30 +00005127static inline int tg3_irq_sync(struct tg3 *tp)
5128{
5129 return tp->irq_sync;
5130}
5131
Matt Carlson97bd8e42011-04-13 11:05:04 +00005132static inline void tg3_rd32_loop(struct tg3 *tp, u32 *dst, u32 off, u32 len)
5133{
5134 int i;
5135
5136 dst = (u32 *)((u8 *)dst + off);
5137 for (i = 0; i < len; i += sizeof(u32))
5138 *dst++ = tr32(off + i);
5139}
5140
5141static void tg3_dump_legacy_regs(struct tg3 *tp, u32 *regs)
5142{
5143 tg3_rd32_loop(tp, regs, TG3PCI_VENDOR, 0xb0);
5144 tg3_rd32_loop(tp, regs, MAILBOX_INTERRUPT_0, 0x200);
5145 tg3_rd32_loop(tp, regs, MAC_MODE, 0x4f0);
5146 tg3_rd32_loop(tp, regs, SNDDATAI_MODE, 0xe0);
5147 tg3_rd32_loop(tp, regs, SNDDATAC_MODE, 0x04);
5148 tg3_rd32_loop(tp, regs, SNDBDS_MODE, 0x80);
5149 tg3_rd32_loop(tp, regs, SNDBDI_MODE, 0x48);
5150 tg3_rd32_loop(tp, regs, SNDBDC_MODE, 0x04);
5151 tg3_rd32_loop(tp, regs, RCVLPC_MODE, 0x20);
5152 tg3_rd32_loop(tp, regs, RCVLPC_SELLST_BASE, 0x15c);
5153 tg3_rd32_loop(tp, regs, RCVDBDI_MODE, 0x0c);
5154 tg3_rd32_loop(tp, regs, RCVDBDI_JUMBO_BD, 0x3c);
5155 tg3_rd32_loop(tp, regs, RCVDBDI_BD_PROD_IDX_0, 0x44);
5156 tg3_rd32_loop(tp, regs, RCVDCC_MODE, 0x04);
5157 tg3_rd32_loop(tp, regs, RCVBDI_MODE, 0x20);
5158 tg3_rd32_loop(tp, regs, RCVCC_MODE, 0x14);
5159 tg3_rd32_loop(tp, regs, RCVLSC_MODE, 0x08);
5160 tg3_rd32_loop(tp, regs, MBFREE_MODE, 0x08);
5161 tg3_rd32_loop(tp, regs, HOSTCC_MODE, 0x100);
5162
Joe Perches63c3a662011-04-26 08:12:10 +00005163 if (tg3_flag(tp, SUPPORT_MSIX))
Matt Carlson97bd8e42011-04-13 11:05:04 +00005164 tg3_rd32_loop(tp, regs, HOSTCC_RXCOL_TICKS_VEC1, 0x180);
5165
5166 tg3_rd32_loop(tp, regs, MEMARB_MODE, 0x10);
5167 tg3_rd32_loop(tp, regs, BUFMGR_MODE, 0x58);
5168 tg3_rd32_loop(tp, regs, RDMAC_MODE, 0x08);
5169 tg3_rd32_loop(tp, regs, WDMAC_MODE, 0x08);
5170 tg3_rd32_loop(tp, regs, RX_CPU_MODE, 0x04);
5171 tg3_rd32_loop(tp, regs, RX_CPU_STATE, 0x04);
5172 tg3_rd32_loop(tp, regs, RX_CPU_PGMCTR, 0x04);
5173 tg3_rd32_loop(tp, regs, RX_CPU_HWBKPT, 0x04);
5174
Joe Perches63c3a662011-04-26 08:12:10 +00005175 if (!tg3_flag(tp, 5705_PLUS)) {
Matt Carlson97bd8e42011-04-13 11:05:04 +00005176 tg3_rd32_loop(tp, regs, TX_CPU_MODE, 0x04);
5177 tg3_rd32_loop(tp, regs, TX_CPU_STATE, 0x04);
5178 tg3_rd32_loop(tp, regs, TX_CPU_PGMCTR, 0x04);
5179 }
5180
5181 tg3_rd32_loop(tp, regs, GRCMBOX_INTERRUPT_0, 0x110);
5182 tg3_rd32_loop(tp, regs, FTQ_RESET, 0x120);
5183 tg3_rd32_loop(tp, regs, MSGINT_MODE, 0x0c);
5184 tg3_rd32_loop(tp, regs, DMAC_MODE, 0x04);
5185 tg3_rd32_loop(tp, regs, GRC_MODE, 0x4c);
5186
Joe Perches63c3a662011-04-26 08:12:10 +00005187 if (tg3_flag(tp, NVRAM))
Matt Carlson97bd8e42011-04-13 11:05:04 +00005188 tg3_rd32_loop(tp, regs, NVRAM_CMD, 0x24);
5189}
5190
5191static void tg3_dump_state(struct tg3 *tp)
5192{
5193 int i;
5194 u32 *regs;
5195
5196 regs = kzalloc(TG3_REG_BLK_SIZE, GFP_ATOMIC);
5197 if (!regs) {
5198 netdev_err(tp->dev, "Failed allocating register dump buffer\n");
5199 return;
5200 }
5201
Joe Perches63c3a662011-04-26 08:12:10 +00005202 if (tg3_flag(tp, PCI_EXPRESS)) {
Matt Carlson97bd8e42011-04-13 11:05:04 +00005203 /* Read up to but not including private PCI registers */
5204 for (i = 0; i < TG3_PCIE_TLDLPL_PORT; i += sizeof(u32))
5205 regs[i / sizeof(u32)] = tr32(i);
5206 } else
5207 tg3_dump_legacy_regs(tp, regs);
5208
5209 for (i = 0; i < TG3_REG_BLK_SIZE / sizeof(u32); i += 4) {
5210 if (!regs[i + 0] && !regs[i + 1] &&
5211 !regs[i + 2] && !regs[i + 3])
5212 continue;
5213
5214 netdev_err(tp->dev, "0x%08x: 0x%08x, 0x%08x, 0x%08x, 0x%08x\n",
5215 i * 4,
5216 regs[i + 0], regs[i + 1], regs[i + 2], regs[i + 3]);
5217 }
5218
5219 kfree(regs);
5220
5221 for (i = 0; i < tp->irq_cnt; i++) {
5222 struct tg3_napi *tnapi = &tp->napi[i];
5223
5224 /* SW status block */
5225 netdev_err(tp->dev,
5226 "%d: Host status block [%08x:%08x:(%04x:%04x:%04x):(%04x:%04x)]\n",
5227 i,
5228 tnapi->hw_status->status,
5229 tnapi->hw_status->status_tag,
5230 tnapi->hw_status->rx_jumbo_consumer,
5231 tnapi->hw_status->rx_consumer,
5232 tnapi->hw_status->rx_mini_consumer,
5233 tnapi->hw_status->idx[0].rx_producer,
5234 tnapi->hw_status->idx[0].tx_consumer);
5235
5236 netdev_err(tp->dev,
5237 "%d: NAPI info [%08x:%08x:(%04x:%04x:%04x):%04x:(%04x:%04x:%04x:%04x)]\n",
5238 i,
5239 tnapi->last_tag, tnapi->last_irq_tag,
5240 tnapi->tx_prod, tnapi->tx_cons, tnapi->tx_pending,
5241 tnapi->rx_rcb_ptr,
5242 tnapi->prodring.rx_std_prod_idx,
5243 tnapi->prodring.rx_std_cons_idx,
5244 tnapi->prodring.rx_jmb_prod_idx,
5245 tnapi->prodring.rx_jmb_cons_idx);
5246 }
5247}
5248
Michael Chandf3e6542006-05-26 17:48:07 -07005249/* This is called whenever we suspect that the system chipset is re-
5250 * ordering the sequence of MMIO to the tx send mailbox. The symptom
5251 * is bogus tx completions. We try to recover by setting the
5252 * TG3_FLAG_MBOX_WRITE_REORDER flag and resetting the chip later
5253 * in the workqueue.
5254 */
5255static void tg3_tx_recover(struct tg3 *tp)
5256{
Joe Perches63c3a662011-04-26 08:12:10 +00005257 BUG_ON(tg3_flag(tp, MBOX_WRITE_REORDER) ||
Michael Chandf3e6542006-05-26 17:48:07 -07005258 tp->write32_tx_mbox == tg3_write_indirect_mbox);
5259
Matt Carlson5129c3a2010-04-05 10:19:23 +00005260 netdev_warn(tp->dev,
5261 "The system may be re-ordering memory-mapped I/O "
5262 "cycles to the network device, attempting to recover. "
5263 "Please report the problem to the driver maintainer "
5264 "and include system chipset information.\n");
Michael Chandf3e6542006-05-26 17:48:07 -07005265
5266 spin_lock(&tp->lock);
Joe Perches63c3a662011-04-26 08:12:10 +00005267 tg3_flag_set(tp, TX_RECOVERY_PENDING);
Michael Chandf3e6542006-05-26 17:48:07 -07005268 spin_unlock(&tp->lock);
5269}
5270
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005271static inline u32 tg3_tx_avail(struct tg3_napi *tnapi)
Michael Chan1b2a7202006-08-07 21:46:02 -07005272{
Matt Carlsonf65aac12010-08-02 11:26:03 +00005273 /* Tell compiler to fetch tx indices from memory. */
5274 barrier();
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005275 return tnapi->tx_pending -
5276 ((tnapi->tx_prod - tnapi->tx_cons) & (TG3_TX_RING_SIZE - 1));
Michael Chan1b2a7202006-08-07 21:46:02 -07005277}
5278
Linus Torvalds1da177e2005-04-16 15:20:36 -07005279/* Tigon3 never reports partial packet sends. So we do not
5280 * need special logic to handle SKBs that have not had all
5281 * of their frags sent yet, like SunGEM does.
5282 */
Matt Carlson17375d22009-08-28 14:02:18 +00005283static void tg3_tx(struct tg3_napi *tnapi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005284{
Matt Carlson17375d22009-08-28 14:02:18 +00005285 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00005286 u32 hw_idx = tnapi->hw_status->idx[0].tx_consumer;
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005287 u32 sw_idx = tnapi->tx_cons;
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005288 struct netdev_queue *txq;
5289 int index = tnapi - tp->napi;
5290
Joe Perches63c3a662011-04-26 08:12:10 +00005291 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005292 index--;
5293
5294 txq = netdev_get_tx_queue(tp->dev, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005295
5296 while (sw_idx != hw_idx) {
Matt Carlsondf8944c2011-07-27 14:20:46 +00005297 struct tg3_tx_ring_info *ri = &tnapi->tx_buffers[sw_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07005298 struct sk_buff *skb = ri->skb;
Michael Chandf3e6542006-05-26 17:48:07 -07005299 int i, tx_bug = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005300
Michael Chandf3e6542006-05-26 17:48:07 -07005301 if (unlikely(skb == NULL)) {
5302 tg3_tx_recover(tp);
5303 return;
5304 }
5305
Alexander Duyckf4188d82009-12-02 16:48:38 +00005306 pci_unmap_single(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005307 dma_unmap_addr(ri, mapping),
Alexander Duyckf4188d82009-12-02 16:48:38 +00005308 skb_headlen(skb),
5309 PCI_DMA_TODEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005310
5311 ri->skb = NULL;
5312
Matt Carlsone01ee142011-07-27 14:20:50 +00005313 while (ri->fragmented) {
5314 ri->fragmented = false;
5315 sw_idx = NEXT_TX(sw_idx);
5316 ri = &tnapi->tx_buffers[sw_idx];
5317 }
5318
Linus Torvalds1da177e2005-04-16 15:20:36 -07005319 sw_idx = NEXT_TX(sw_idx);
5320
5321 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005322 ri = &tnapi->tx_buffers[sw_idx];
Michael Chandf3e6542006-05-26 17:48:07 -07005323 if (unlikely(ri->skb != NULL || sw_idx == hw_idx))
5324 tx_bug = 1;
Alexander Duyckf4188d82009-12-02 16:48:38 +00005325
5326 pci_unmap_page(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005327 dma_unmap_addr(ri, mapping),
Eric Dumazet9e903e02011-10-18 21:00:24 +00005328 skb_frag_size(&skb_shinfo(skb)->frags[i]),
Alexander Duyckf4188d82009-12-02 16:48:38 +00005329 PCI_DMA_TODEVICE);
Matt Carlsone01ee142011-07-27 14:20:50 +00005330
5331 while (ri->fragmented) {
5332 ri->fragmented = false;
5333 sw_idx = NEXT_TX(sw_idx);
5334 ri = &tnapi->tx_buffers[sw_idx];
5335 }
5336
Linus Torvalds1da177e2005-04-16 15:20:36 -07005337 sw_idx = NEXT_TX(sw_idx);
5338 }
5339
David S. Millerf47c11e2005-06-24 20:18:35 -07005340 dev_kfree_skb(skb);
Michael Chandf3e6542006-05-26 17:48:07 -07005341
5342 if (unlikely(tx_bug)) {
5343 tg3_tx_recover(tp);
5344 return;
5345 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005346 }
5347
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005348 tnapi->tx_cons = sw_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005349
Michael Chan1b2a7202006-08-07 21:46:02 -07005350 /* Need to make the tx_cons update visible to tg3_start_xmit()
5351 * before checking for netif_queue_stopped(). Without the
5352 * memory barrier, there is a small possibility that tg3_start_xmit()
5353 * will miss it and cause the queue to be stopped forever.
5354 */
5355 smp_mb();
5356
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005357 if (unlikely(netif_tx_queue_stopped(txq) &&
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005358 (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi)))) {
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005359 __netif_tx_lock(txq, smp_processor_id());
5360 if (netif_tx_queue_stopped(txq) &&
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005361 (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi)))
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005362 netif_tx_wake_queue(txq);
5363 __netif_tx_unlock(txq);
Michael Chan51b91462005-09-01 17:41:28 -07005364 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005365}
5366
Eric Dumazet9205fd92011-11-18 06:47:01 +00005367static void tg3_rx_data_free(struct tg3 *tp, struct ring_info *ri, u32 map_sz)
Matt Carlson2b2cdb62009-11-13 13:03:48 +00005368{
Eric Dumazet9205fd92011-11-18 06:47:01 +00005369 if (!ri->data)
Matt Carlson2b2cdb62009-11-13 13:03:48 +00005370 return;
5371
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005372 pci_unmap_single(tp->pdev, dma_unmap_addr(ri, mapping),
Matt Carlson2b2cdb62009-11-13 13:03:48 +00005373 map_sz, PCI_DMA_FROMDEVICE);
Eric Dumazet9205fd92011-11-18 06:47:01 +00005374 kfree(ri->data);
5375 ri->data = NULL;
Matt Carlson2b2cdb62009-11-13 13:03:48 +00005376}
5377
Linus Torvalds1da177e2005-04-16 15:20:36 -07005378/* Returns size of skb allocated or < 0 on error.
5379 *
5380 * We only need to fill in the address because the other members
5381 * of the RX descriptor are invariant, see tg3_init_rings.
5382 *
5383 * Note the purposeful assymetry of cpu vs. chip accesses. For
5384 * posting buffers we only dirty the first cache line of the RX
5385 * descriptor (containing the address). Whereas for the RX status
5386 * buffers the cpu only reads the last cacheline of the RX descriptor
5387 * (to fetch the error flags, vlan tag, checksum, and opaque cookie).
5388 */
Eric Dumazet9205fd92011-11-18 06:47:01 +00005389static int tg3_alloc_rx_data(struct tg3 *tp, struct tg3_rx_prodring_set *tpr,
Matt Carlsona3896162009-11-13 13:03:44 +00005390 u32 opaque_key, u32 dest_idx_unmasked)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005391{
5392 struct tg3_rx_buffer_desc *desc;
Matt Carlsonf94e2902010-10-14 10:37:42 +00005393 struct ring_info *map;
Eric Dumazet9205fd92011-11-18 06:47:01 +00005394 u8 *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005395 dma_addr_t mapping;
Eric Dumazet9205fd92011-11-18 06:47:01 +00005396 int skb_size, data_size, dest_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005397
Linus Torvalds1da177e2005-04-16 15:20:36 -07005398 switch (opaque_key) {
5399 case RXD_OPAQUE_RING_STD:
Matt Carlson2c49a442010-09-30 10:34:35 +00005400 dest_idx = dest_idx_unmasked & tp->rx_std_ring_mask;
Matt Carlson21f581a2009-08-28 14:00:25 +00005401 desc = &tpr->rx_std[dest_idx];
5402 map = &tpr->rx_std_buffers[dest_idx];
Eric Dumazet9205fd92011-11-18 06:47:01 +00005403 data_size = tp->rx_pkt_map_sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005404 break;
5405
5406 case RXD_OPAQUE_RING_JUMBO:
Matt Carlson2c49a442010-09-30 10:34:35 +00005407 dest_idx = dest_idx_unmasked & tp->rx_jmb_ring_mask;
Matt Carlson79ed5ac2009-08-28 14:00:55 +00005408 desc = &tpr->rx_jmb[dest_idx].std;
Matt Carlson21f581a2009-08-28 14:00:25 +00005409 map = &tpr->rx_jmb_buffers[dest_idx];
Eric Dumazet9205fd92011-11-18 06:47:01 +00005410 data_size = TG3_RX_JMB_MAP_SZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005411 break;
5412
5413 default:
5414 return -EINVAL;
Stephen Hemminger855e1112008-04-16 16:37:28 -07005415 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005416
5417 /* Do not overwrite any of the map or rp information
5418 * until we are sure we can commit to a new buffer.
5419 *
5420 * Callers depend upon this behavior and assume that
5421 * we leave everything unchanged if we fail.
5422 */
Eric Dumazet9205fd92011-11-18 06:47:01 +00005423 skb_size = SKB_DATA_ALIGN(data_size + TG3_RX_OFFSET(tp)) +
5424 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
5425 data = kmalloc(skb_size, GFP_ATOMIC);
5426 if (!data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005427 return -ENOMEM;
5428
Eric Dumazet9205fd92011-11-18 06:47:01 +00005429 mapping = pci_map_single(tp->pdev,
5430 data + TG3_RX_OFFSET(tp),
5431 data_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005432 PCI_DMA_FROMDEVICE);
Matt Carlsona21771d2009-11-02 14:25:31 +00005433 if (pci_dma_mapping_error(tp->pdev, mapping)) {
Eric Dumazet9205fd92011-11-18 06:47:01 +00005434 kfree(data);
Matt Carlsona21771d2009-11-02 14:25:31 +00005435 return -EIO;
5436 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005437
Eric Dumazet9205fd92011-11-18 06:47:01 +00005438 map->data = data;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005439 dma_unmap_addr_set(map, mapping, mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005440
Linus Torvalds1da177e2005-04-16 15:20:36 -07005441 desc->addr_hi = ((u64)mapping >> 32);
5442 desc->addr_lo = ((u64)mapping & 0xffffffff);
5443
Eric Dumazet9205fd92011-11-18 06:47:01 +00005444 return data_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005445}
5446
5447/* We only need to move over in the address because the other
5448 * members of the RX descriptor are invariant. See notes above
Eric Dumazet9205fd92011-11-18 06:47:01 +00005449 * tg3_alloc_rx_data for full details.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005450 */
Matt Carlsona3896162009-11-13 13:03:44 +00005451static void tg3_recycle_rx(struct tg3_napi *tnapi,
5452 struct tg3_rx_prodring_set *dpr,
5453 u32 opaque_key, int src_idx,
5454 u32 dest_idx_unmasked)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005455{
Matt Carlson17375d22009-08-28 14:02:18 +00005456 struct tg3 *tp = tnapi->tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005457 struct tg3_rx_buffer_desc *src_desc, *dest_desc;
5458 struct ring_info *src_map, *dest_map;
Matt Carlson8fea32b2010-09-15 08:59:58 +00005459 struct tg3_rx_prodring_set *spr = &tp->napi[0].prodring;
Matt Carlsonc6cdf432010-04-05 10:19:26 +00005460 int dest_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005461
5462 switch (opaque_key) {
5463 case RXD_OPAQUE_RING_STD:
Matt Carlson2c49a442010-09-30 10:34:35 +00005464 dest_idx = dest_idx_unmasked & tp->rx_std_ring_mask;
Matt Carlsona3896162009-11-13 13:03:44 +00005465 dest_desc = &dpr->rx_std[dest_idx];
5466 dest_map = &dpr->rx_std_buffers[dest_idx];
5467 src_desc = &spr->rx_std[src_idx];
5468 src_map = &spr->rx_std_buffers[src_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07005469 break;
5470
5471 case RXD_OPAQUE_RING_JUMBO:
Matt Carlson2c49a442010-09-30 10:34:35 +00005472 dest_idx = dest_idx_unmasked & tp->rx_jmb_ring_mask;
Matt Carlsona3896162009-11-13 13:03:44 +00005473 dest_desc = &dpr->rx_jmb[dest_idx].std;
5474 dest_map = &dpr->rx_jmb_buffers[dest_idx];
5475 src_desc = &spr->rx_jmb[src_idx].std;
5476 src_map = &spr->rx_jmb_buffers[src_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07005477 break;
5478
5479 default:
5480 return;
Stephen Hemminger855e1112008-04-16 16:37:28 -07005481 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005482
Eric Dumazet9205fd92011-11-18 06:47:01 +00005483 dest_map->data = src_map->data;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005484 dma_unmap_addr_set(dest_map, mapping,
5485 dma_unmap_addr(src_map, mapping));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005486 dest_desc->addr_hi = src_desc->addr_hi;
5487 dest_desc->addr_lo = src_desc->addr_lo;
Matt Carlsone92967b2010-02-12 14:47:06 +00005488
5489 /* Ensure that the update to the skb happens after the physical
5490 * addresses have been transferred to the new BD location.
5491 */
5492 smp_wmb();
5493
Eric Dumazet9205fd92011-11-18 06:47:01 +00005494 src_map->data = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005495}
5496
Linus Torvalds1da177e2005-04-16 15:20:36 -07005497/* The RX ring scheme is composed of multiple rings which post fresh
5498 * buffers to the chip, and one special ring the chip uses to report
5499 * status back to the host.
5500 *
5501 * The special ring reports the status of received packets to the
5502 * host. The chip does not write into the original descriptor the
5503 * RX buffer was obtained from. The chip simply takes the original
5504 * descriptor as provided by the host, updates the status and length
5505 * field, then writes this into the next status ring entry.
5506 *
5507 * Each ring the host uses to post buffers to the chip is described
5508 * by a TG3_BDINFO entry in the chips SRAM area. When a packet arrives,
5509 * it is first placed into the on-chip ram. When the packet's length
5510 * is known, it walks down the TG3_BDINFO entries to select the ring.
5511 * Each TG3_BDINFO specifies a MAXLEN field and the first TG3_BDINFO
5512 * which is within the range of the new packet's length is chosen.
5513 *
5514 * The "separate ring for rx status" scheme may sound queer, but it makes
5515 * sense from a cache coherency perspective. If only the host writes
5516 * to the buffer post rings, and only the chip writes to the rx status
5517 * rings, then cache lines never move beyond shared-modified state.
5518 * If both the host and chip were to write into the same ring, cache line
5519 * eviction could occur since both entities want it in an exclusive state.
5520 */
Matt Carlson17375d22009-08-28 14:02:18 +00005521static int tg3_rx(struct tg3_napi *tnapi, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005522{
Matt Carlson17375d22009-08-28 14:02:18 +00005523 struct tg3 *tp = tnapi->tp;
Michael Chanf92905d2006-06-29 20:14:29 -07005524 u32 work_mask, rx_std_posted = 0;
Matt Carlson43619352009-11-13 13:03:47 +00005525 u32 std_prod_idx, jmb_prod_idx;
Matt Carlson72334482009-08-28 14:03:01 +00005526 u32 sw_idx = tnapi->rx_rcb_ptr;
Michael Chan483ba502005-04-25 15:14:03 -07005527 u16 hw_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005528 int received;
Matt Carlson8fea32b2010-09-15 08:59:58 +00005529 struct tg3_rx_prodring_set *tpr = &tnapi->prodring;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005530
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00005531 hw_idx = *(tnapi->rx_rcb_prod_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005532 /*
5533 * We need to order the read of hw_idx and the read of
5534 * the opaque cookie.
5535 */
5536 rmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005537 work_mask = 0;
5538 received = 0;
Matt Carlson43619352009-11-13 13:03:47 +00005539 std_prod_idx = tpr->rx_std_prod_idx;
5540 jmb_prod_idx = tpr->rx_jmb_prod_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005541 while (sw_idx != hw_idx && budget > 0) {
Matt Carlsonafc081f2009-11-13 13:03:43 +00005542 struct ring_info *ri;
Matt Carlson72334482009-08-28 14:03:01 +00005543 struct tg3_rx_buffer_desc *desc = &tnapi->rx_rcb[sw_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07005544 unsigned int len;
5545 struct sk_buff *skb;
5546 dma_addr_t dma_addr;
5547 u32 opaque_key, desc_idx, *post_ptr;
Eric Dumazet9205fd92011-11-18 06:47:01 +00005548 u8 *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005549
5550 desc_idx = desc->opaque & RXD_OPAQUE_INDEX_MASK;
5551 opaque_key = desc->opaque & RXD_OPAQUE_RING_MASK;
5552 if (opaque_key == RXD_OPAQUE_RING_STD) {
Matt Carlson8fea32b2010-09-15 08:59:58 +00005553 ri = &tp->napi[0].prodring.rx_std_buffers[desc_idx];
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005554 dma_addr = dma_unmap_addr(ri, mapping);
Eric Dumazet9205fd92011-11-18 06:47:01 +00005555 data = ri->data;
Matt Carlson43619352009-11-13 13:03:47 +00005556 post_ptr = &std_prod_idx;
Michael Chanf92905d2006-06-29 20:14:29 -07005557 rx_std_posted++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005558 } else if (opaque_key == RXD_OPAQUE_RING_JUMBO) {
Matt Carlson8fea32b2010-09-15 08:59:58 +00005559 ri = &tp->napi[0].prodring.rx_jmb_buffers[desc_idx];
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005560 dma_addr = dma_unmap_addr(ri, mapping);
Eric Dumazet9205fd92011-11-18 06:47:01 +00005561 data = ri->data;
Matt Carlson43619352009-11-13 13:03:47 +00005562 post_ptr = &jmb_prod_idx;
Matt Carlson21f581a2009-08-28 14:00:25 +00005563 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07005564 goto next_pkt_nopost;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005565
5566 work_mask |= opaque_key;
5567
5568 if ((desc->err_vlan & RXD_ERR_MASK) != 0 &&
5569 (desc->err_vlan != RXD_ERR_ODD_NIBBLE_RCVD_MII)) {
5570 drop_it:
Matt Carlsona3896162009-11-13 13:03:44 +00005571 tg3_recycle_rx(tnapi, tpr, opaque_key,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005572 desc_idx, *post_ptr);
5573 drop_it_no_recycle:
5574 /* Other statistics kept track of by card. */
Eric Dumazetb0057c52010-10-10 19:55:52 +00005575 tp->rx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005576 goto next_pkt;
5577 }
5578
Eric Dumazet9205fd92011-11-18 06:47:01 +00005579 prefetch(data + TG3_RX_OFFSET(tp));
Matt Carlsonad829262008-11-21 17:16:16 -08005580 len = ((desc->idx_len & RXD_LEN_MASK) >> RXD_LEN_SHIFT) -
5581 ETH_FCS_LEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005582
Matt Carlsond2757fc2010-04-12 06:58:27 +00005583 if (len > TG3_RX_COPY_THRESH(tp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005584 int skb_size;
5585
Eric Dumazet9205fd92011-11-18 06:47:01 +00005586 skb_size = tg3_alloc_rx_data(tp, tpr, opaque_key,
Matt Carlsonafc081f2009-11-13 13:03:43 +00005587 *post_ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005588 if (skb_size < 0)
5589 goto drop_it;
5590
Matt Carlson287be122009-08-28 13:58:46 +00005591 pci_unmap_single(tp->pdev, dma_addr, skb_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005592 PCI_DMA_FROMDEVICE);
5593
Eric Dumazet9205fd92011-11-18 06:47:01 +00005594 skb = build_skb(data);
5595 if (!skb) {
5596 kfree(data);
5597 goto drop_it_no_recycle;
5598 }
5599 skb_reserve(skb, TG3_RX_OFFSET(tp));
5600 /* Ensure that the update to the data happens
Matt Carlson61e800c2010-02-17 15:16:54 +00005601 * after the usage of the old DMA mapping.
5602 */
5603 smp_wmb();
5604
Eric Dumazet9205fd92011-11-18 06:47:01 +00005605 ri->data = NULL;
Matt Carlson61e800c2010-02-17 15:16:54 +00005606
Linus Torvalds1da177e2005-04-16 15:20:36 -07005607 } else {
Matt Carlsona3896162009-11-13 13:03:44 +00005608 tg3_recycle_rx(tnapi, tpr, opaque_key,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005609 desc_idx, *post_ptr);
5610
Eric Dumazet9205fd92011-11-18 06:47:01 +00005611 skb = netdev_alloc_skb(tp->dev,
5612 len + TG3_RAW_IP_ALIGN);
5613 if (skb == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005614 goto drop_it_no_recycle;
5615
Eric Dumazet9205fd92011-11-18 06:47:01 +00005616 skb_reserve(skb, TG3_RAW_IP_ALIGN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005617 pci_dma_sync_single_for_cpu(tp->pdev, dma_addr, len, PCI_DMA_FROMDEVICE);
Eric Dumazet9205fd92011-11-18 06:47:01 +00005618 memcpy(skb->data,
5619 data + TG3_RX_OFFSET(tp),
5620 len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005621 pci_dma_sync_single_for_device(tp->pdev, dma_addr, len, PCI_DMA_FROMDEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005622 }
5623
Eric Dumazet9205fd92011-11-18 06:47:01 +00005624 skb_put(skb, len);
Michał Mirosławdc668912011-04-07 03:35:07 +00005625 if ((tp->dev->features & NETIF_F_RXCSUM) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005626 (desc->type_flags & RXD_FLAG_TCPUDP_CSUM) &&
5627 (((desc->ip_tcp_csum & RXD_TCPCSUM_MASK)
5628 >> RXD_TCPCSUM_SHIFT) == 0xffff))
5629 skb->ip_summed = CHECKSUM_UNNECESSARY;
5630 else
Eric Dumazetbc8acf22010-09-02 13:07:41 -07005631 skb_checksum_none_assert(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005632
5633 skb->protocol = eth_type_trans(skb, tp->dev);
Matt Carlsonf7b493e2009-02-25 14:21:52 +00005634
5635 if (len > (tp->dev->mtu + ETH_HLEN) &&
5636 skb->protocol != htons(ETH_P_8021Q)) {
5637 dev_kfree_skb(skb);
Eric Dumazetb0057c52010-10-10 19:55:52 +00005638 goto drop_it_no_recycle;
Matt Carlsonf7b493e2009-02-25 14:21:52 +00005639 }
5640
Matt Carlson9dc7a112010-04-12 06:58:28 +00005641 if (desc->type_flags & RXD_FLAG_VLAN &&
Matt Carlsonbf933c82011-01-25 15:58:49 +00005642 !(tp->rx_mode & RX_MODE_KEEP_VLAN_TAG))
5643 __vlan_hwaccel_put_tag(skb,
5644 desc->err_vlan & RXD_VLAN_MASK);
Matt Carlson9dc7a112010-04-12 06:58:28 +00005645
Matt Carlsonbf933c82011-01-25 15:58:49 +00005646 napi_gro_receive(&tnapi->napi, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005647
Linus Torvalds1da177e2005-04-16 15:20:36 -07005648 received++;
5649 budget--;
5650
5651next_pkt:
5652 (*post_ptr)++;
Michael Chanf92905d2006-06-29 20:14:29 -07005653
5654 if (unlikely(rx_std_posted >= tp->rx_std_max_post)) {
Matt Carlson2c49a442010-09-30 10:34:35 +00005655 tpr->rx_std_prod_idx = std_prod_idx &
5656 tp->rx_std_ring_mask;
Matt Carlson86cfe4f2010-01-12 10:11:37 +00005657 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
5658 tpr->rx_std_prod_idx);
Michael Chanf92905d2006-06-29 20:14:29 -07005659 work_mask &= ~RXD_OPAQUE_RING_STD;
5660 rx_std_posted = 0;
5661 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005662next_pkt_nopost:
Michael Chan483ba502005-04-25 15:14:03 -07005663 sw_idx++;
Matt Carlson7cb32cf2010-09-30 10:34:36 +00005664 sw_idx &= tp->rx_ret_ring_mask;
Michael Chan52f6d692005-04-25 15:14:32 -07005665
5666 /* Refresh hw_idx to see if there is new work */
5667 if (sw_idx == hw_idx) {
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00005668 hw_idx = *(tnapi->rx_rcb_prod_idx);
Michael Chan52f6d692005-04-25 15:14:32 -07005669 rmb();
5670 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005671 }
5672
5673 /* ACK the status ring. */
Matt Carlson72334482009-08-28 14:03:01 +00005674 tnapi->rx_rcb_ptr = sw_idx;
5675 tw32_rx_mbox(tnapi->consmbox, sw_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005676
5677 /* Refill RX ring(s). */
Joe Perches63c3a662011-04-26 08:12:10 +00005678 if (!tg3_flag(tp, ENABLE_RSS)) {
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005679 if (work_mask & RXD_OPAQUE_RING_STD) {
Matt Carlson2c49a442010-09-30 10:34:35 +00005680 tpr->rx_std_prod_idx = std_prod_idx &
5681 tp->rx_std_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005682 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
5683 tpr->rx_std_prod_idx);
5684 }
5685 if (work_mask & RXD_OPAQUE_RING_JUMBO) {
Matt Carlson2c49a442010-09-30 10:34:35 +00005686 tpr->rx_jmb_prod_idx = jmb_prod_idx &
5687 tp->rx_jmb_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005688 tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG,
5689 tpr->rx_jmb_prod_idx);
5690 }
5691 mmiowb();
5692 } else if (work_mask) {
5693 /* rx_std_buffers[] and rx_jmb_buffers[] entries must be
5694 * updated before the producer indices can be updated.
5695 */
5696 smp_wmb();
5697
Matt Carlson2c49a442010-09-30 10:34:35 +00005698 tpr->rx_std_prod_idx = std_prod_idx & tp->rx_std_ring_mask;
5699 tpr->rx_jmb_prod_idx = jmb_prod_idx & tp->rx_jmb_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005700
Matt Carlsone4af1af2010-02-12 14:47:05 +00005701 if (tnapi != &tp->napi[1])
5702 napi_schedule(&tp->napi[1].napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005703 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005704
5705 return received;
5706}
5707
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005708static void tg3_poll_link(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005709{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005710 /* handle link change and other phy events */
Joe Perches63c3a662011-04-26 08:12:10 +00005711 if (!(tg3_flag(tp, USE_LINKCHG_REG) || tg3_flag(tp, POLL_SERDES))) {
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005712 struct tg3_hw_status *sblk = tp->napi[0].hw_status;
5713
Linus Torvalds1da177e2005-04-16 15:20:36 -07005714 if (sblk->status & SD_STATUS_LINK_CHG) {
5715 sblk->status = SD_STATUS_UPDATED |
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005716 (sblk->status & ~SD_STATUS_LINK_CHG);
David S. Millerf47c11e2005-06-24 20:18:35 -07005717 spin_lock(&tp->lock);
Joe Perches63c3a662011-04-26 08:12:10 +00005718 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlsondd477002008-05-25 23:45:58 -07005719 tw32_f(MAC_STATUS,
5720 (MAC_STATUS_SYNC_CHANGED |
5721 MAC_STATUS_CFG_CHANGED |
5722 MAC_STATUS_MI_COMPLETION |
5723 MAC_STATUS_LNKSTATE_CHANGED));
5724 udelay(40);
5725 } else
5726 tg3_setup_phy(tp, 0);
David S. Millerf47c11e2005-06-24 20:18:35 -07005727 spin_unlock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005728 }
5729 }
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005730}
5731
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005732static int tg3_rx_prodring_xfer(struct tg3 *tp,
5733 struct tg3_rx_prodring_set *dpr,
5734 struct tg3_rx_prodring_set *spr)
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005735{
5736 u32 si, di, cpycnt, src_prod_idx;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005737 int i, err = 0;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005738
5739 while (1) {
5740 src_prod_idx = spr->rx_std_prod_idx;
5741
5742 /* Make sure updates to the rx_std_buffers[] entries and the
5743 * standard producer index are seen in the correct order.
5744 */
5745 smp_rmb();
5746
5747 if (spr->rx_std_cons_idx == src_prod_idx)
5748 break;
5749
5750 if (spr->rx_std_cons_idx < src_prod_idx)
5751 cpycnt = src_prod_idx - spr->rx_std_cons_idx;
5752 else
Matt Carlson2c49a442010-09-30 10:34:35 +00005753 cpycnt = tp->rx_std_ring_mask + 1 -
5754 spr->rx_std_cons_idx;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005755
Matt Carlson2c49a442010-09-30 10:34:35 +00005756 cpycnt = min(cpycnt,
5757 tp->rx_std_ring_mask + 1 - dpr->rx_std_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005758
5759 si = spr->rx_std_cons_idx;
5760 di = dpr->rx_std_prod_idx;
5761
Matt Carlsone92967b2010-02-12 14:47:06 +00005762 for (i = di; i < di + cpycnt; i++) {
Eric Dumazet9205fd92011-11-18 06:47:01 +00005763 if (dpr->rx_std_buffers[i].data) {
Matt Carlsone92967b2010-02-12 14:47:06 +00005764 cpycnt = i - di;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005765 err = -ENOSPC;
Matt Carlsone92967b2010-02-12 14:47:06 +00005766 break;
5767 }
5768 }
5769
5770 if (!cpycnt)
5771 break;
5772
5773 /* Ensure that updates to the rx_std_buffers ring and the
5774 * shadowed hardware producer ring from tg3_recycle_skb() are
5775 * ordered correctly WRT the skb check above.
5776 */
5777 smp_rmb();
5778
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005779 memcpy(&dpr->rx_std_buffers[di],
5780 &spr->rx_std_buffers[si],
5781 cpycnt * sizeof(struct ring_info));
5782
5783 for (i = 0; i < cpycnt; i++, di++, si++) {
5784 struct tg3_rx_buffer_desc *sbd, *dbd;
5785 sbd = &spr->rx_std[si];
5786 dbd = &dpr->rx_std[di];
5787 dbd->addr_hi = sbd->addr_hi;
5788 dbd->addr_lo = sbd->addr_lo;
5789 }
5790
Matt Carlson2c49a442010-09-30 10:34:35 +00005791 spr->rx_std_cons_idx = (spr->rx_std_cons_idx + cpycnt) &
5792 tp->rx_std_ring_mask;
5793 dpr->rx_std_prod_idx = (dpr->rx_std_prod_idx + cpycnt) &
5794 tp->rx_std_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005795 }
5796
5797 while (1) {
5798 src_prod_idx = spr->rx_jmb_prod_idx;
5799
5800 /* Make sure updates to the rx_jmb_buffers[] entries and
5801 * the jumbo producer index are seen in the correct order.
5802 */
5803 smp_rmb();
5804
5805 if (spr->rx_jmb_cons_idx == src_prod_idx)
5806 break;
5807
5808 if (spr->rx_jmb_cons_idx < src_prod_idx)
5809 cpycnt = src_prod_idx - spr->rx_jmb_cons_idx;
5810 else
Matt Carlson2c49a442010-09-30 10:34:35 +00005811 cpycnt = tp->rx_jmb_ring_mask + 1 -
5812 spr->rx_jmb_cons_idx;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005813
5814 cpycnt = min(cpycnt,
Matt Carlson2c49a442010-09-30 10:34:35 +00005815 tp->rx_jmb_ring_mask + 1 - dpr->rx_jmb_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005816
5817 si = spr->rx_jmb_cons_idx;
5818 di = dpr->rx_jmb_prod_idx;
5819
Matt Carlsone92967b2010-02-12 14:47:06 +00005820 for (i = di; i < di + cpycnt; i++) {
Eric Dumazet9205fd92011-11-18 06:47:01 +00005821 if (dpr->rx_jmb_buffers[i].data) {
Matt Carlsone92967b2010-02-12 14:47:06 +00005822 cpycnt = i - di;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005823 err = -ENOSPC;
Matt Carlsone92967b2010-02-12 14:47:06 +00005824 break;
5825 }
5826 }
5827
5828 if (!cpycnt)
5829 break;
5830
5831 /* Ensure that updates to the rx_jmb_buffers ring and the
5832 * shadowed hardware producer ring from tg3_recycle_skb() are
5833 * ordered correctly WRT the skb check above.
5834 */
5835 smp_rmb();
5836
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005837 memcpy(&dpr->rx_jmb_buffers[di],
5838 &spr->rx_jmb_buffers[si],
5839 cpycnt * sizeof(struct ring_info));
5840
5841 for (i = 0; i < cpycnt; i++, di++, si++) {
5842 struct tg3_rx_buffer_desc *sbd, *dbd;
5843 sbd = &spr->rx_jmb[si].std;
5844 dbd = &dpr->rx_jmb[di].std;
5845 dbd->addr_hi = sbd->addr_hi;
5846 dbd->addr_lo = sbd->addr_lo;
5847 }
5848
Matt Carlson2c49a442010-09-30 10:34:35 +00005849 spr->rx_jmb_cons_idx = (spr->rx_jmb_cons_idx + cpycnt) &
5850 tp->rx_jmb_ring_mask;
5851 dpr->rx_jmb_prod_idx = (dpr->rx_jmb_prod_idx + cpycnt) &
5852 tp->rx_jmb_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005853 }
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005854
5855 return err;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005856}
5857
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005858static int tg3_poll_work(struct tg3_napi *tnapi, int work_done, int budget)
5859{
5860 struct tg3 *tp = tnapi->tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005861
5862 /* run TX completion thread */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005863 if (tnapi->hw_status->idx[0].tx_consumer != tnapi->tx_cons) {
Matt Carlson17375d22009-08-28 14:02:18 +00005864 tg3_tx(tnapi);
Joe Perches63c3a662011-04-26 08:12:10 +00005865 if (unlikely(tg3_flag(tp, TX_RECOVERY_PENDING)))
Michael Chan4fd7ab52007-10-12 01:39:50 -07005866 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005867 }
5868
Linus Torvalds1da177e2005-04-16 15:20:36 -07005869 /* run RX thread, within the bounds set by NAPI.
5870 * All RX "locking" is done by ensuring outside
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005871 * code synchronizes with tg3->napi.poll()
Linus Torvalds1da177e2005-04-16 15:20:36 -07005872 */
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00005873 if (*(tnapi->rx_rcb_prod_idx) != tnapi->rx_rcb_ptr)
Matt Carlson17375d22009-08-28 14:02:18 +00005874 work_done += tg3_rx(tnapi, budget - work_done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005875
Joe Perches63c3a662011-04-26 08:12:10 +00005876 if (tg3_flag(tp, ENABLE_RSS) && tnapi == &tp->napi[1]) {
Matt Carlson8fea32b2010-09-15 08:59:58 +00005877 struct tg3_rx_prodring_set *dpr = &tp->napi[0].prodring;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005878 int i, err = 0;
Matt Carlsone4af1af2010-02-12 14:47:05 +00005879 u32 std_prod_idx = dpr->rx_std_prod_idx;
5880 u32 jmb_prod_idx = dpr->rx_jmb_prod_idx;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005881
Matt Carlsone4af1af2010-02-12 14:47:05 +00005882 for (i = 1; i < tp->irq_cnt; i++)
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005883 err |= tg3_rx_prodring_xfer(tp, dpr,
Matt Carlson8fea32b2010-09-15 08:59:58 +00005884 &tp->napi[i].prodring);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005885
5886 wmb();
5887
Matt Carlsone4af1af2010-02-12 14:47:05 +00005888 if (std_prod_idx != dpr->rx_std_prod_idx)
5889 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
5890 dpr->rx_std_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005891
Matt Carlsone4af1af2010-02-12 14:47:05 +00005892 if (jmb_prod_idx != dpr->rx_jmb_prod_idx)
5893 tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG,
5894 dpr->rx_jmb_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005895
5896 mmiowb();
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005897
5898 if (err)
5899 tw32_f(HOSTCC_MODE, tp->coal_now);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005900 }
5901
David S. Miller6f535762007-10-11 18:08:29 -07005902 return work_done;
5903}
David S. Millerf7383c22005-05-18 22:50:53 -07005904
Matt Carlsondb219972011-11-04 09:15:03 +00005905static inline void tg3_reset_task_schedule(struct tg3 *tp)
5906{
5907 if (!test_and_set_bit(TG3_FLAG_RESET_TASK_PENDING, tp->tg3_flags))
5908 schedule_work(&tp->reset_task);
5909}
5910
5911static inline void tg3_reset_task_cancel(struct tg3 *tp)
5912{
5913 cancel_work_sync(&tp->reset_task);
5914 tg3_flag_clear(tp, RESET_TASK_PENDING);
5915}
5916
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005917static int tg3_poll_msix(struct napi_struct *napi, int budget)
5918{
5919 struct tg3_napi *tnapi = container_of(napi, struct tg3_napi, napi);
5920 struct tg3 *tp = tnapi->tp;
5921 int work_done = 0;
5922 struct tg3_hw_status *sblk = tnapi->hw_status;
5923
5924 while (1) {
5925 work_done = tg3_poll_work(tnapi, work_done, budget);
5926
Joe Perches63c3a662011-04-26 08:12:10 +00005927 if (unlikely(tg3_flag(tp, TX_RECOVERY_PENDING)))
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005928 goto tx_recovery;
5929
5930 if (unlikely(work_done >= budget))
5931 break;
5932
Matt Carlsonc6cdf432010-04-05 10:19:26 +00005933 /* tp->last_tag is used in tg3_int_reenable() below
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005934 * to tell the hw how much work has been processed,
5935 * so we must read it before checking for more work.
5936 */
5937 tnapi->last_tag = sblk->status_tag;
5938 tnapi->last_irq_tag = tnapi->last_tag;
5939 rmb();
5940
5941 /* check for RX/TX work to do */
Matt Carlson6d40db72010-04-05 10:19:20 +00005942 if (likely(sblk->idx[0].tx_consumer == tnapi->tx_cons &&
5943 *(tnapi->rx_rcb_prod_idx) == tnapi->rx_rcb_ptr)) {
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005944 napi_complete(napi);
5945 /* Reenable interrupts. */
5946 tw32_mailbox(tnapi->int_mbox, tnapi->last_tag << 24);
5947 mmiowb();
5948 break;
5949 }
5950 }
5951
5952 return work_done;
5953
5954tx_recovery:
5955 /* work_done is guaranteed to be less than budget. */
5956 napi_complete(napi);
Matt Carlsondb219972011-11-04 09:15:03 +00005957 tg3_reset_task_schedule(tp);
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005958 return work_done;
5959}
5960
Matt Carlsone64de4e2011-04-13 11:05:05 +00005961static void tg3_process_error(struct tg3 *tp)
5962{
5963 u32 val;
5964 bool real_error = false;
5965
Joe Perches63c3a662011-04-26 08:12:10 +00005966 if (tg3_flag(tp, ERROR_PROCESSED))
Matt Carlsone64de4e2011-04-13 11:05:05 +00005967 return;
5968
5969 /* Check Flow Attention register */
5970 val = tr32(HOSTCC_FLOW_ATTN);
5971 if (val & ~HOSTCC_FLOW_ATTN_MBUF_LWM) {
5972 netdev_err(tp->dev, "FLOW Attention error. Resetting chip.\n");
5973 real_error = true;
5974 }
5975
5976 if (tr32(MSGINT_STATUS) & ~MSGINT_STATUS_MSI_REQ) {
5977 netdev_err(tp->dev, "MSI Status error. Resetting chip.\n");
5978 real_error = true;
5979 }
5980
5981 if (tr32(RDMAC_STATUS) || tr32(WDMAC_STATUS)) {
5982 netdev_err(tp->dev, "DMA Status error. Resetting chip.\n");
5983 real_error = true;
5984 }
5985
5986 if (!real_error)
5987 return;
5988
5989 tg3_dump_state(tp);
5990
Joe Perches63c3a662011-04-26 08:12:10 +00005991 tg3_flag_set(tp, ERROR_PROCESSED);
Matt Carlsondb219972011-11-04 09:15:03 +00005992 tg3_reset_task_schedule(tp);
Matt Carlsone64de4e2011-04-13 11:05:05 +00005993}
5994
David S. Miller6f535762007-10-11 18:08:29 -07005995static int tg3_poll(struct napi_struct *napi, int budget)
5996{
Matt Carlson8ef04422009-08-28 14:01:37 +00005997 struct tg3_napi *tnapi = container_of(napi, struct tg3_napi, napi);
5998 struct tg3 *tp = tnapi->tp;
David S. Miller6f535762007-10-11 18:08:29 -07005999 int work_done = 0;
Matt Carlson898a56f2009-08-28 14:02:40 +00006000 struct tg3_hw_status *sblk = tnapi->hw_status;
David S. Miller6f535762007-10-11 18:08:29 -07006001
6002 while (1) {
Matt Carlsone64de4e2011-04-13 11:05:05 +00006003 if (sblk->status & SD_STATUS_ERROR)
6004 tg3_process_error(tp);
6005
Matt Carlson35f2d7d2009-11-13 13:03:41 +00006006 tg3_poll_link(tp);
6007
Matt Carlson17375d22009-08-28 14:02:18 +00006008 work_done = tg3_poll_work(tnapi, work_done, budget);
David S. Miller6f535762007-10-11 18:08:29 -07006009
Joe Perches63c3a662011-04-26 08:12:10 +00006010 if (unlikely(tg3_flag(tp, TX_RECOVERY_PENDING)))
David S. Miller6f535762007-10-11 18:08:29 -07006011 goto tx_recovery;
6012
6013 if (unlikely(work_done >= budget))
6014 break;
6015
Joe Perches63c3a662011-04-26 08:12:10 +00006016 if (tg3_flag(tp, TAGGED_STATUS)) {
Matt Carlson17375d22009-08-28 14:02:18 +00006017 /* tp->last_tag is used in tg3_int_reenable() below
Michael Chan4fd7ab52007-10-12 01:39:50 -07006018 * to tell the hw how much work has been processed,
6019 * so we must read it before checking for more work.
6020 */
Matt Carlson898a56f2009-08-28 14:02:40 +00006021 tnapi->last_tag = sblk->status_tag;
6022 tnapi->last_irq_tag = tnapi->last_tag;
Michael Chan4fd7ab52007-10-12 01:39:50 -07006023 rmb();
6024 } else
6025 sblk->status &= ~SD_STATUS_UPDATED;
6026
Matt Carlson17375d22009-08-28 14:02:18 +00006027 if (likely(!tg3_has_work(tnapi))) {
Ben Hutchings288379f2009-01-19 16:43:59 -08006028 napi_complete(napi);
Matt Carlson17375d22009-08-28 14:02:18 +00006029 tg3_int_reenable(tnapi);
David S. Miller6f535762007-10-11 18:08:29 -07006030 break;
6031 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006032 }
6033
Stephen Hemmingerbea33482007-10-03 16:41:36 -07006034 return work_done;
David S. Miller6f535762007-10-11 18:08:29 -07006035
6036tx_recovery:
Michael Chan4fd7ab52007-10-12 01:39:50 -07006037 /* work_done is guaranteed to be less than budget. */
Ben Hutchings288379f2009-01-19 16:43:59 -08006038 napi_complete(napi);
Matt Carlsondb219972011-11-04 09:15:03 +00006039 tg3_reset_task_schedule(tp);
Michael Chan4fd7ab52007-10-12 01:39:50 -07006040 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006041}
6042
Matt Carlson66cfd1b2010-09-30 10:34:30 +00006043static void tg3_napi_disable(struct tg3 *tp)
6044{
6045 int i;
6046
6047 for (i = tp->irq_cnt - 1; i >= 0; i--)
6048 napi_disable(&tp->napi[i].napi);
6049}
6050
6051static void tg3_napi_enable(struct tg3 *tp)
6052{
6053 int i;
6054
6055 for (i = 0; i < tp->irq_cnt; i++)
6056 napi_enable(&tp->napi[i].napi);
6057}
6058
6059static void tg3_napi_init(struct tg3 *tp)
6060{
6061 int i;
6062
6063 netif_napi_add(tp->dev, &tp->napi[0].napi, tg3_poll, 64);
6064 for (i = 1; i < tp->irq_cnt; i++)
6065 netif_napi_add(tp->dev, &tp->napi[i].napi, tg3_poll_msix, 64);
6066}
6067
6068static void tg3_napi_fini(struct tg3 *tp)
6069{
6070 int i;
6071
6072 for (i = 0; i < tp->irq_cnt; i++)
6073 netif_napi_del(&tp->napi[i].napi);
6074}
6075
6076static inline void tg3_netif_stop(struct tg3 *tp)
6077{
6078 tp->dev->trans_start = jiffies; /* prevent tx timeout */
6079 tg3_napi_disable(tp);
6080 netif_tx_disable(tp->dev);
6081}
6082
6083static inline void tg3_netif_start(struct tg3 *tp)
6084{
6085 /* NOTE: unconditional netif_tx_wake_all_queues is only
6086 * appropriate so long as all callers are assured to
6087 * have free tx slots (such as after tg3_init_hw)
6088 */
6089 netif_tx_wake_all_queues(tp->dev);
6090
6091 tg3_napi_enable(tp);
6092 tp->napi[0].hw_status->status |= SD_STATUS_UPDATED;
6093 tg3_enable_ints(tp);
6094}
6095
David S. Millerf47c11e2005-06-24 20:18:35 -07006096static void tg3_irq_quiesce(struct tg3 *tp)
6097{
Matt Carlson4f125f42009-09-01 12:55:02 +00006098 int i;
6099
David S. Millerf47c11e2005-06-24 20:18:35 -07006100 BUG_ON(tp->irq_sync);
6101
6102 tp->irq_sync = 1;
6103 smp_mb();
6104
Matt Carlson4f125f42009-09-01 12:55:02 +00006105 for (i = 0; i < tp->irq_cnt; i++)
6106 synchronize_irq(tp->napi[i].irq_vec);
David S. Millerf47c11e2005-06-24 20:18:35 -07006107}
6108
David S. Millerf47c11e2005-06-24 20:18:35 -07006109/* Fully shutdown all tg3 driver activity elsewhere in the system.
6110 * If irq_sync is non-zero, then the IRQ handler must be synchronized
6111 * with as well. Most of the time, this is not necessary except when
6112 * shutting down the device.
6113 */
6114static inline void tg3_full_lock(struct tg3 *tp, int irq_sync)
6115{
Michael Chan46966542007-07-11 19:47:19 -07006116 spin_lock_bh(&tp->lock);
David S. Millerf47c11e2005-06-24 20:18:35 -07006117 if (irq_sync)
6118 tg3_irq_quiesce(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -07006119}
6120
6121static inline void tg3_full_unlock(struct tg3 *tp)
6122{
David S. Millerf47c11e2005-06-24 20:18:35 -07006123 spin_unlock_bh(&tp->lock);
6124}
6125
Michael Chanfcfa0a32006-03-20 22:28:41 -08006126/* One-shot MSI handler - Chip automatically disables interrupt
6127 * after sending MSI so driver doesn't have to do it.
6128 */
David Howells7d12e782006-10-05 14:55:46 +01006129static irqreturn_t tg3_msi_1shot(int irq, void *dev_id)
Michael Chanfcfa0a32006-03-20 22:28:41 -08006130{
Matt Carlson09943a12009-08-28 14:01:57 +00006131 struct tg3_napi *tnapi = dev_id;
6132 struct tg3 *tp = tnapi->tp;
Michael Chanfcfa0a32006-03-20 22:28:41 -08006133
Matt Carlson898a56f2009-08-28 14:02:40 +00006134 prefetch(tnapi->hw_status);
Matt Carlson0c1d0e22009-09-01 13:16:33 +00006135 if (tnapi->rx_rcb)
6136 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Michael Chanfcfa0a32006-03-20 22:28:41 -08006137
6138 if (likely(!tg3_irq_sync(tp)))
Matt Carlson09943a12009-08-28 14:01:57 +00006139 napi_schedule(&tnapi->napi);
Michael Chanfcfa0a32006-03-20 22:28:41 -08006140
6141 return IRQ_HANDLED;
6142}
6143
Michael Chan88b06bc2005-04-21 17:13:25 -07006144/* MSI ISR - No need to check for interrupt sharing and no need to
6145 * flush status block and interrupt mailbox. PCI ordering rules
6146 * guarantee that MSI will arrive after the status block.
6147 */
David Howells7d12e782006-10-05 14:55:46 +01006148static irqreturn_t tg3_msi(int irq, void *dev_id)
Michael Chan88b06bc2005-04-21 17:13:25 -07006149{
Matt Carlson09943a12009-08-28 14:01:57 +00006150 struct tg3_napi *tnapi = dev_id;
6151 struct tg3 *tp = tnapi->tp;
Michael Chan88b06bc2005-04-21 17:13:25 -07006152
Matt Carlson898a56f2009-08-28 14:02:40 +00006153 prefetch(tnapi->hw_status);
Matt Carlson0c1d0e22009-09-01 13:16:33 +00006154 if (tnapi->rx_rcb)
6155 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Michael Chan88b06bc2005-04-21 17:13:25 -07006156 /*
David S. Millerfac9b832005-05-18 22:46:34 -07006157 * Writing any value to intr-mbox-0 clears PCI INTA# and
Michael Chan88b06bc2005-04-21 17:13:25 -07006158 * chip-internal interrupt pending events.
David S. Millerfac9b832005-05-18 22:46:34 -07006159 * Writing non-zero to intr-mbox-0 additional tells the
Michael Chan88b06bc2005-04-21 17:13:25 -07006160 * NIC to stop sending us irqs, engaging "in-intr-handler"
6161 * event coalescing.
6162 */
Matt Carlson5b39de92011-08-31 11:44:50 +00006163 tw32_mailbox(tnapi->int_mbox, 0x00000001);
Michael Chan61487482005-09-05 17:53:19 -07006164 if (likely(!tg3_irq_sync(tp)))
Matt Carlson09943a12009-08-28 14:01:57 +00006165 napi_schedule(&tnapi->napi);
Michael Chan61487482005-09-05 17:53:19 -07006166
Michael Chan88b06bc2005-04-21 17:13:25 -07006167 return IRQ_RETVAL(1);
6168}
6169
David Howells7d12e782006-10-05 14:55:46 +01006170static irqreturn_t tg3_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006171{
Matt Carlson09943a12009-08-28 14:01:57 +00006172 struct tg3_napi *tnapi = dev_id;
6173 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00006174 struct tg3_hw_status *sblk = tnapi->hw_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006175 unsigned int handled = 1;
6176
Linus Torvalds1da177e2005-04-16 15:20:36 -07006177 /* In INTx mode, it is possible for the interrupt to arrive at
6178 * the CPU before the status block posted prior to the interrupt.
6179 * Reading the PCI State register will confirm whether the
6180 * interrupt is ours and will flush the status block.
6181 */
Michael Chand18edcb2007-03-24 20:57:11 -07006182 if (unlikely(!(sblk->status & SD_STATUS_UPDATED))) {
Joe Perches63c3a662011-04-26 08:12:10 +00006183 if (tg3_flag(tp, CHIP_RESETTING) ||
Michael Chand18edcb2007-03-24 20:57:11 -07006184 (tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
6185 handled = 0;
David S. Millerf47c11e2005-06-24 20:18:35 -07006186 goto out;
David S. Millerfac9b832005-05-18 22:46:34 -07006187 }
Michael Chand18edcb2007-03-24 20:57:11 -07006188 }
6189
6190 /*
6191 * Writing any value to intr-mbox-0 clears PCI INTA# and
6192 * chip-internal interrupt pending events.
6193 * Writing non-zero to intr-mbox-0 additional tells the
6194 * NIC to stop sending us irqs, engaging "in-intr-handler"
6195 * event coalescing.
Michael Chanc04cb342007-05-07 00:26:15 -07006196 *
6197 * Flush the mailbox to de-assert the IRQ immediately to prevent
6198 * spurious interrupts. The flush impacts performance but
6199 * excessive spurious interrupts can be worse in some cases.
Michael Chand18edcb2007-03-24 20:57:11 -07006200 */
Michael Chanc04cb342007-05-07 00:26:15 -07006201 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 0x00000001);
Michael Chand18edcb2007-03-24 20:57:11 -07006202 if (tg3_irq_sync(tp))
6203 goto out;
6204 sblk->status &= ~SD_STATUS_UPDATED;
Matt Carlson17375d22009-08-28 14:02:18 +00006205 if (likely(tg3_has_work(tnapi))) {
Matt Carlson72334482009-08-28 14:03:01 +00006206 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Matt Carlson09943a12009-08-28 14:01:57 +00006207 napi_schedule(&tnapi->napi);
Michael Chand18edcb2007-03-24 20:57:11 -07006208 } else {
6209 /* No work, shared interrupt perhaps? re-enable
6210 * interrupts, and flush that PCI write
6211 */
6212 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW,
6213 0x00000000);
David S. Millerfac9b832005-05-18 22:46:34 -07006214 }
David S. Millerf47c11e2005-06-24 20:18:35 -07006215out:
David S. Millerfac9b832005-05-18 22:46:34 -07006216 return IRQ_RETVAL(handled);
6217}
6218
David Howells7d12e782006-10-05 14:55:46 +01006219static irqreturn_t tg3_interrupt_tagged(int irq, void *dev_id)
David S. Millerfac9b832005-05-18 22:46:34 -07006220{
Matt Carlson09943a12009-08-28 14:01:57 +00006221 struct tg3_napi *tnapi = dev_id;
6222 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00006223 struct tg3_hw_status *sblk = tnapi->hw_status;
David S. Millerfac9b832005-05-18 22:46:34 -07006224 unsigned int handled = 1;
6225
David S. Millerfac9b832005-05-18 22:46:34 -07006226 /* In INTx mode, it is possible for the interrupt to arrive at
6227 * the CPU before the status block posted prior to the interrupt.
6228 * Reading the PCI State register will confirm whether the
6229 * interrupt is ours and will flush the status block.
6230 */
Matt Carlson898a56f2009-08-28 14:02:40 +00006231 if (unlikely(sblk->status_tag == tnapi->last_irq_tag)) {
Joe Perches63c3a662011-04-26 08:12:10 +00006232 if (tg3_flag(tp, CHIP_RESETTING) ||
Michael Chand18edcb2007-03-24 20:57:11 -07006233 (tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
6234 handled = 0;
David S. Millerf47c11e2005-06-24 20:18:35 -07006235 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006236 }
Michael Chand18edcb2007-03-24 20:57:11 -07006237 }
6238
6239 /*
6240 * writing any value to intr-mbox-0 clears PCI INTA# and
6241 * chip-internal interrupt pending events.
6242 * writing non-zero to intr-mbox-0 additional tells the
6243 * NIC to stop sending us irqs, engaging "in-intr-handler"
6244 * event coalescing.
Michael Chanc04cb342007-05-07 00:26:15 -07006245 *
6246 * Flush the mailbox to de-assert the IRQ immediately to prevent
6247 * spurious interrupts. The flush impacts performance but
6248 * excessive spurious interrupts can be worse in some cases.
Michael Chand18edcb2007-03-24 20:57:11 -07006249 */
Michael Chanc04cb342007-05-07 00:26:15 -07006250 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 0x00000001);
Matt Carlson624f8e52009-04-20 06:55:01 +00006251
6252 /*
6253 * In a shared interrupt configuration, sometimes other devices'
6254 * interrupts will scream. We record the current status tag here
6255 * so that the above check can report that the screaming interrupts
6256 * are unhandled. Eventually they will be silenced.
6257 */
Matt Carlson898a56f2009-08-28 14:02:40 +00006258 tnapi->last_irq_tag = sblk->status_tag;
Matt Carlson624f8e52009-04-20 06:55:01 +00006259
Michael Chand18edcb2007-03-24 20:57:11 -07006260 if (tg3_irq_sync(tp))
6261 goto out;
Matt Carlson624f8e52009-04-20 06:55:01 +00006262
Matt Carlson72334482009-08-28 14:03:01 +00006263 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Matt Carlson624f8e52009-04-20 06:55:01 +00006264
Matt Carlson09943a12009-08-28 14:01:57 +00006265 napi_schedule(&tnapi->napi);
Matt Carlson624f8e52009-04-20 06:55:01 +00006266
David S. Millerf47c11e2005-06-24 20:18:35 -07006267out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006268 return IRQ_RETVAL(handled);
6269}
6270
Michael Chan79381092005-04-21 17:13:59 -07006271/* ISR for interrupt test */
David Howells7d12e782006-10-05 14:55:46 +01006272static irqreturn_t tg3_test_isr(int irq, void *dev_id)
Michael Chan79381092005-04-21 17:13:59 -07006273{
Matt Carlson09943a12009-08-28 14:01:57 +00006274 struct tg3_napi *tnapi = dev_id;
6275 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00006276 struct tg3_hw_status *sblk = tnapi->hw_status;
Michael Chan79381092005-04-21 17:13:59 -07006277
Michael Chanf9804dd2005-09-27 12:13:10 -07006278 if ((sblk->status & SD_STATUS_UPDATED) ||
6279 !(tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
Michael Chanb16250e2006-09-27 16:10:14 -07006280 tg3_disable_ints(tp);
Michael Chan79381092005-04-21 17:13:59 -07006281 return IRQ_RETVAL(1);
6282 }
6283 return IRQ_RETVAL(0);
6284}
6285
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07006286static int tg3_init_hw(struct tg3 *, int);
Michael Chan944d9802005-05-29 14:57:48 -07006287static int tg3_halt(struct tg3 *, int, int);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006288
Michael Chanb9ec6c12006-07-25 16:37:27 -07006289/* Restart hardware after configuration changes, self-test, etc.
6290 * Invoked with tp->lock held.
6291 */
6292static int tg3_restart_hw(struct tg3 *tp, int reset_phy)
Eric Dumazet78c61462008-04-24 23:33:06 -07006293 __releases(tp->lock)
6294 __acquires(tp->lock)
Michael Chanb9ec6c12006-07-25 16:37:27 -07006295{
6296 int err;
6297
6298 err = tg3_init_hw(tp, reset_phy);
6299 if (err) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00006300 netdev_err(tp->dev,
6301 "Failed to re-initialize device, aborting\n");
Michael Chanb9ec6c12006-07-25 16:37:27 -07006302 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
6303 tg3_full_unlock(tp);
6304 del_timer_sync(&tp->timer);
6305 tp->irq_sync = 0;
Matt Carlsonfed97812009-09-01 13:10:19 +00006306 tg3_napi_enable(tp);
Michael Chanb9ec6c12006-07-25 16:37:27 -07006307 dev_close(tp->dev);
6308 tg3_full_lock(tp, 0);
6309 }
6310 return err;
6311}
6312
Linus Torvalds1da177e2005-04-16 15:20:36 -07006313#ifdef CONFIG_NET_POLL_CONTROLLER
6314static void tg3_poll_controller(struct net_device *dev)
6315{
Matt Carlson4f125f42009-09-01 12:55:02 +00006316 int i;
Michael Chan88b06bc2005-04-21 17:13:25 -07006317 struct tg3 *tp = netdev_priv(dev);
6318
Matt Carlson4f125f42009-09-01 12:55:02 +00006319 for (i = 0; i < tp->irq_cnt; i++)
Louis Rillingfe234f02010-03-09 06:14:41 +00006320 tg3_interrupt(tp->napi[i].irq_vec, &tp->napi[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006321}
6322#endif
6323
David Howellsc4028952006-11-22 14:57:56 +00006324static void tg3_reset_task(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006325{
David Howellsc4028952006-11-22 14:57:56 +00006326 struct tg3 *tp = container_of(work, struct tg3, reset_task);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07006327 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006328
Michael Chan7faa0062006-02-02 17:29:28 -08006329 tg3_full_lock(tp, 0);
Michael Chan7faa0062006-02-02 17:29:28 -08006330
6331 if (!netif_running(tp->dev)) {
Matt Carlsondb219972011-11-04 09:15:03 +00006332 tg3_flag_clear(tp, RESET_TASK_PENDING);
Michael Chan7faa0062006-02-02 17:29:28 -08006333 tg3_full_unlock(tp);
6334 return;
6335 }
6336
6337 tg3_full_unlock(tp);
6338
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07006339 tg3_phy_stop(tp);
6340
Linus Torvalds1da177e2005-04-16 15:20:36 -07006341 tg3_netif_stop(tp);
6342
David S. Millerf47c11e2005-06-24 20:18:35 -07006343 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006344
Joe Perches63c3a662011-04-26 08:12:10 +00006345 if (tg3_flag(tp, TX_RECOVERY_PENDING)) {
Michael Chandf3e6542006-05-26 17:48:07 -07006346 tp->write32_tx_mbox = tg3_write32_tx_mbox;
6347 tp->write32_rx_mbox = tg3_write_flush_reg32;
Joe Perches63c3a662011-04-26 08:12:10 +00006348 tg3_flag_set(tp, MBOX_WRITE_REORDER);
6349 tg3_flag_clear(tp, TX_RECOVERY_PENDING);
Michael Chandf3e6542006-05-26 17:48:07 -07006350 }
6351
Michael Chan944d9802005-05-29 14:57:48 -07006352 tg3_halt(tp, RESET_KIND_SHUTDOWN, 0);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07006353 err = tg3_init_hw(tp, 1);
6354 if (err)
Michael Chanb9ec6c12006-07-25 16:37:27 -07006355 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006356
6357 tg3_netif_start(tp);
6358
Michael Chanb9ec6c12006-07-25 16:37:27 -07006359out:
Michael Chan7faa0062006-02-02 17:29:28 -08006360 tg3_full_unlock(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07006361
6362 if (!err)
6363 tg3_phy_start(tp);
Matt Carlsondb219972011-11-04 09:15:03 +00006364
6365 tg3_flag_clear(tp, RESET_TASK_PENDING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006366}
6367
6368static void tg3_tx_timeout(struct net_device *dev)
6369{
6370 struct tg3 *tp = netdev_priv(dev);
6371
Michael Chanb0408752007-02-13 12:18:30 -08006372 if (netif_msg_tx_err(tp)) {
Joe Perches05dbe002010-02-17 19:44:19 +00006373 netdev_err(dev, "transmit timed out, resetting\n");
Matt Carlson97bd8e42011-04-13 11:05:04 +00006374 tg3_dump_state(tp);
Michael Chanb0408752007-02-13 12:18:30 -08006375 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006376
Matt Carlsondb219972011-11-04 09:15:03 +00006377 tg3_reset_task_schedule(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006378}
6379
Michael Chanc58ec932005-09-17 00:46:27 -07006380/* Test for DMA buffers crossing any 4GB boundaries: 4G, 8G, etc */
6381static inline int tg3_4g_overflow_test(dma_addr_t mapping, int len)
6382{
6383 u32 base = (u32) mapping & 0xffffffff;
6384
Eric Dumazet807540b2010-09-23 05:40:09 +00006385 return (base > 0xffffdcc0) && (base + len + 8 < base);
Michael Chanc58ec932005-09-17 00:46:27 -07006386}
6387
Michael Chan72f2afb2006-03-06 19:28:35 -08006388/* Test for DMA addresses > 40-bit */
6389static inline int tg3_40bit_overflow_test(struct tg3 *tp, dma_addr_t mapping,
6390 int len)
6391{
6392#if defined(CONFIG_HIGHMEM) && (BITS_PER_LONG == 64)
Joe Perches63c3a662011-04-26 08:12:10 +00006393 if (tg3_flag(tp, 40BIT_DMA_BUG))
Eric Dumazet807540b2010-09-23 05:40:09 +00006394 return ((u64) mapping + len) > DMA_BIT_MASK(40);
Michael Chan72f2afb2006-03-06 19:28:35 -08006395 return 0;
6396#else
6397 return 0;
6398#endif
6399}
6400
Matt Carlsond1a3b732011-07-27 14:20:51 +00006401static inline void tg3_tx_set_bd(struct tg3_tx_buffer_desc *txbd,
Matt Carlson92cd3a12011-07-27 14:20:47 +00006402 dma_addr_t mapping, u32 len, u32 flags,
6403 u32 mss, u32 vlan)
Matt Carlson2ffcc982011-05-19 12:12:44 +00006404{
Matt Carlson92cd3a12011-07-27 14:20:47 +00006405 txbd->addr_hi = ((u64) mapping >> 32);
6406 txbd->addr_lo = ((u64) mapping & 0xffffffff);
6407 txbd->len_flags = (len << TXD_LEN_SHIFT) | (flags & 0x0000ffff);
6408 txbd->vlan_tag = (mss << TXD_MSS_SHIFT) | (vlan << TXD_VLAN_TAG_SHIFT);
Matt Carlson2ffcc982011-05-19 12:12:44 +00006409}
Linus Torvalds1da177e2005-04-16 15:20:36 -07006410
Matt Carlson84b67b22011-07-27 14:20:52 +00006411static bool tg3_tx_frag_set(struct tg3_napi *tnapi, u32 *entry, u32 *budget,
Matt Carlsond1a3b732011-07-27 14:20:51 +00006412 dma_addr_t map, u32 len, u32 flags,
6413 u32 mss, u32 vlan)
6414{
6415 struct tg3 *tp = tnapi->tp;
6416 bool hwbug = false;
6417
6418 if (tg3_flag(tp, SHORT_DMA_BUG) && len <= 8)
6419 hwbug = 1;
6420
6421 if (tg3_4g_overflow_test(map, len))
6422 hwbug = 1;
6423
6424 if (tg3_40bit_overflow_test(tp, map, len))
6425 hwbug = 1;
6426
Matt Carlsone31aa982011-07-27 14:20:53 +00006427 if (tg3_flag(tp, 4K_FIFO_LIMIT)) {
Matt Carlsonb9e45482011-11-04 09:14:59 +00006428 u32 prvidx = *entry;
Matt Carlsone31aa982011-07-27 14:20:53 +00006429 u32 tmp_flag = flags & ~TXD_FLAG_END;
Matt Carlsonb9e45482011-11-04 09:14:59 +00006430 while (len > TG3_TX_BD_DMA_MAX && *budget) {
Matt Carlsone31aa982011-07-27 14:20:53 +00006431 u32 frag_len = TG3_TX_BD_DMA_MAX;
6432 len -= TG3_TX_BD_DMA_MAX;
6433
Matt Carlsonb9e45482011-11-04 09:14:59 +00006434 /* Avoid the 8byte DMA problem */
6435 if (len <= 8) {
6436 len += TG3_TX_BD_DMA_MAX / 2;
6437 frag_len = TG3_TX_BD_DMA_MAX / 2;
Matt Carlsone31aa982011-07-27 14:20:53 +00006438 }
6439
Matt Carlsonb9e45482011-11-04 09:14:59 +00006440 tnapi->tx_buffers[*entry].fragmented = true;
6441
6442 tg3_tx_set_bd(&tnapi->tx_ring[*entry], map,
6443 frag_len, tmp_flag, mss, vlan);
6444 *budget -= 1;
6445 prvidx = *entry;
6446 *entry = NEXT_TX(*entry);
6447
Matt Carlsone31aa982011-07-27 14:20:53 +00006448 map += frag_len;
6449 }
6450
6451 if (len) {
6452 if (*budget) {
6453 tg3_tx_set_bd(&tnapi->tx_ring[*entry], map,
6454 len, flags, mss, vlan);
Matt Carlsonb9e45482011-11-04 09:14:59 +00006455 *budget -= 1;
Matt Carlsone31aa982011-07-27 14:20:53 +00006456 *entry = NEXT_TX(*entry);
6457 } else {
6458 hwbug = 1;
Matt Carlsonb9e45482011-11-04 09:14:59 +00006459 tnapi->tx_buffers[prvidx].fragmented = false;
Matt Carlsone31aa982011-07-27 14:20:53 +00006460 }
6461 }
6462 } else {
Matt Carlson84b67b22011-07-27 14:20:52 +00006463 tg3_tx_set_bd(&tnapi->tx_ring[*entry], map,
6464 len, flags, mss, vlan);
Matt Carlsone31aa982011-07-27 14:20:53 +00006465 *entry = NEXT_TX(*entry);
6466 }
Matt Carlsond1a3b732011-07-27 14:20:51 +00006467
6468 return hwbug;
6469}
6470
Matt Carlson0d681b22011-07-27 14:20:49 +00006471static void tg3_tx_skb_unmap(struct tg3_napi *tnapi, u32 entry, int last)
Matt Carlson432aa7e2011-05-19 12:12:45 +00006472{
6473 int i;
Matt Carlson0d681b22011-07-27 14:20:49 +00006474 struct sk_buff *skb;
Matt Carlsondf8944c2011-07-27 14:20:46 +00006475 struct tg3_tx_ring_info *txb = &tnapi->tx_buffers[entry];
Matt Carlson432aa7e2011-05-19 12:12:45 +00006476
Matt Carlson0d681b22011-07-27 14:20:49 +00006477 skb = txb->skb;
6478 txb->skb = NULL;
6479
Matt Carlson432aa7e2011-05-19 12:12:45 +00006480 pci_unmap_single(tnapi->tp->pdev,
6481 dma_unmap_addr(txb, mapping),
6482 skb_headlen(skb),
6483 PCI_DMA_TODEVICE);
Matt Carlsone01ee142011-07-27 14:20:50 +00006484
6485 while (txb->fragmented) {
6486 txb->fragmented = false;
6487 entry = NEXT_TX(entry);
6488 txb = &tnapi->tx_buffers[entry];
6489 }
6490
Matt Carlsonba1142e2011-11-04 09:15:00 +00006491 for (i = 0; i <= last; i++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00006492 const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
Matt Carlson432aa7e2011-05-19 12:12:45 +00006493
6494 entry = NEXT_TX(entry);
6495 txb = &tnapi->tx_buffers[entry];
6496
6497 pci_unmap_page(tnapi->tp->pdev,
6498 dma_unmap_addr(txb, mapping),
Eric Dumazet9e903e02011-10-18 21:00:24 +00006499 skb_frag_size(frag), PCI_DMA_TODEVICE);
Matt Carlsone01ee142011-07-27 14:20:50 +00006500
6501 while (txb->fragmented) {
6502 txb->fragmented = false;
6503 entry = NEXT_TX(entry);
6504 txb = &tnapi->tx_buffers[entry];
6505 }
Matt Carlson432aa7e2011-05-19 12:12:45 +00006506 }
6507}
6508
Michael Chan72f2afb2006-03-06 19:28:35 -08006509/* Workaround 4GB and 40-bit hardware DMA bugs. */
Matt Carlson24f4efd2009-11-13 13:03:35 +00006510static int tigon3_dma_hwbug_workaround(struct tg3_napi *tnapi,
David S. Miller1805b2f2011-10-24 18:18:09 -04006511 struct sk_buff **pskb,
Matt Carlson84b67b22011-07-27 14:20:52 +00006512 u32 *entry, u32 *budget,
Matt Carlson92cd3a12011-07-27 14:20:47 +00006513 u32 base_flags, u32 mss, u32 vlan)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006514{
Matt Carlson24f4efd2009-11-13 13:03:35 +00006515 struct tg3 *tp = tnapi->tp;
David S. Miller1805b2f2011-10-24 18:18:09 -04006516 struct sk_buff *new_skb, *skb = *pskb;
Michael Chanc58ec932005-09-17 00:46:27 -07006517 dma_addr_t new_addr = 0;
Matt Carlson432aa7e2011-05-19 12:12:45 +00006518 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006519
Matt Carlson41588ba2008-04-19 18:12:33 -07006520 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701)
6521 new_skb = skb_copy(skb, GFP_ATOMIC);
6522 else {
6523 int more_headroom = 4 - ((unsigned long)skb->data & 3);
6524
6525 new_skb = skb_copy_expand(skb,
6526 skb_headroom(skb) + more_headroom,
6527 skb_tailroom(skb), GFP_ATOMIC);
6528 }
6529
Linus Torvalds1da177e2005-04-16 15:20:36 -07006530 if (!new_skb) {
Michael Chanc58ec932005-09-17 00:46:27 -07006531 ret = -1;
6532 } else {
6533 /* New SKB is guaranteed to be linear. */
Alexander Duyckf4188d82009-12-02 16:48:38 +00006534 new_addr = pci_map_single(tp->pdev, new_skb->data, new_skb->len,
6535 PCI_DMA_TODEVICE);
6536 /* Make sure the mapping succeeded */
6537 if (pci_dma_mapping_error(tp->pdev, new_addr)) {
Alexander Duyckf4188d82009-12-02 16:48:38 +00006538 dev_kfree_skb(new_skb);
Michael Chanc58ec932005-09-17 00:46:27 -07006539 ret = -1;
Michael Chanc58ec932005-09-17 00:46:27 -07006540 } else {
Matt Carlsonb9e45482011-11-04 09:14:59 +00006541 u32 save_entry = *entry;
6542
Matt Carlson92cd3a12011-07-27 14:20:47 +00006543 base_flags |= TXD_FLAG_END;
6544
Matt Carlson84b67b22011-07-27 14:20:52 +00006545 tnapi->tx_buffers[*entry].skb = new_skb;
6546 dma_unmap_addr_set(&tnapi->tx_buffers[*entry],
Matt Carlson432aa7e2011-05-19 12:12:45 +00006547 mapping, new_addr);
6548
Matt Carlson84b67b22011-07-27 14:20:52 +00006549 if (tg3_tx_frag_set(tnapi, entry, budget, new_addr,
Matt Carlsond1a3b732011-07-27 14:20:51 +00006550 new_skb->len, base_flags,
6551 mss, vlan)) {
Matt Carlsonba1142e2011-11-04 09:15:00 +00006552 tg3_tx_skb_unmap(tnapi, save_entry, -1);
Matt Carlsond1a3b732011-07-27 14:20:51 +00006553 dev_kfree_skb(new_skb);
6554 ret = -1;
6555 }
Michael Chanc58ec932005-09-17 00:46:27 -07006556 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006557 }
6558
Linus Torvalds1da177e2005-04-16 15:20:36 -07006559 dev_kfree_skb(skb);
David S. Miller1805b2f2011-10-24 18:18:09 -04006560 *pskb = new_skb;
Michael Chanc58ec932005-09-17 00:46:27 -07006561 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006562}
6563
Matt Carlson2ffcc982011-05-19 12:12:44 +00006564static netdev_tx_t tg3_start_xmit(struct sk_buff *, struct net_device *);
Michael Chan52c0fd82006-06-29 20:15:54 -07006565
6566/* Use GSO to workaround a rare TSO bug that may be triggered when the
6567 * TSO header is greater than 80 bytes.
6568 */
6569static int tg3_tso_bug(struct tg3 *tp, struct sk_buff *skb)
6570{
6571 struct sk_buff *segs, *nskb;
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006572 u32 frag_cnt_est = skb_shinfo(skb)->gso_segs * 3;
Michael Chan52c0fd82006-06-29 20:15:54 -07006573
6574 /* Estimate the number of fragments in the worst case */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006575 if (unlikely(tg3_tx_avail(&tp->napi[0]) <= frag_cnt_est)) {
Michael Chan52c0fd82006-06-29 20:15:54 -07006576 netif_stop_queue(tp->dev);
Matt Carlsonf65aac12010-08-02 11:26:03 +00006577
6578 /* netif_tx_stop_queue() must be done before checking
6579 * checking tx index in tg3_tx_avail() below, because in
6580 * tg3_tx(), we update tx index before checking for
6581 * netif_tx_queue_stopped().
6582 */
6583 smp_mb();
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006584 if (tg3_tx_avail(&tp->napi[0]) <= frag_cnt_est)
Michael Chan7f62ad52007-02-20 23:25:40 -08006585 return NETDEV_TX_BUSY;
6586
6587 netif_wake_queue(tp->dev);
Michael Chan52c0fd82006-06-29 20:15:54 -07006588 }
6589
6590 segs = skb_gso_segment(skb, tp->dev->features & ~NETIF_F_TSO);
Hirofumi Nakagawa801678c2008-04-29 01:03:09 -07006591 if (IS_ERR(segs))
Michael Chan52c0fd82006-06-29 20:15:54 -07006592 goto tg3_tso_bug_end;
6593
6594 do {
6595 nskb = segs;
6596 segs = segs->next;
6597 nskb->next = NULL;
Matt Carlson2ffcc982011-05-19 12:12:44 +00006598 tg3_start_xmit(nskb, tp->dev);
Michael Chan52c0fd82006-06-29 20:15:54 -07006599 } while (segs);
6600
6601tg3_tso_bug_end:
6602 dev_kfree_skb(skb);
6603
6604 return NETDEV_TX_OK;
6605}
Michael Chan52c0fd82006-06-29 20:15:54 -07006606
Michael Chan5a6f3072006-03-20 22:28:05 -08006607/* hard_start_xmit for devices that have the 4G bug and/or 40-bit bug and
Joe Perches63c3a662011-04-26 08:12:10 +00006608 * support TG3_FLAG_HW_TSO_1 or firmware TSO only.
Michael Chan5a6f3072006-03-20 22:28:05 -08006609 */
Matt Carlson2ffcc982011-05-19 12:12:44 +00006610static netdev_tx_t tg3_start_xmit(struct sk_buff *skb, struct net_device *dev)
Michael Chan5a6f3072006-03-20 22:28:05 -08006611{
6612 struct tg3 *tp = netdev_priv(dev);
Matt Carlson92cd3a12011-07-27 14:20:47 +00006613 u32 len, entry, base_flags, mss, vlan = 0;
Matt Carlson84b67b22011-07-27 14:20:52 +00006614 u32 budget;
Matt Carlson432aa7e2011-05-19 12:12:45 +00006615 int i = -1, would_hit_hwbug;
David S. Miller90079ce2008-09-11 04:52:51 -07006616 dma_addr_t mapping;
Matt Carlson24f4efd2009-11-13 13:03:35 +00006617 struct tg3_napi *tnapi;
6618 struct netdev_queue *txq;
Matt Carlson432aa7e2011-05-19 12:12:45 +00006619 unsigned int last;
Alexander Duyckf4188d82009-12-02 16:48:38 +00006620
Matt Carlson24f4efd2009-11-13 13:03:35 +00006621 txq = netdev_get_tx_queue(dev, skb_get_queue_mapping(skb));
6622 tnapi = &tp->napi[skb_get_queue_mapping(skb)];
Joe Perches63c3a662011-04-26 08:12:10 +00006623 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlson24f4efd2009-11-13 13:03:35 +00006624 tnapi++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006625
Matt Carlson84b67b22011-07-27 14:20:52 +00006626 budget = tg3_tx_avail(tnapi);
6627
Michael Chan00b70502006-06-17 21:58:45 -07006628 /* We are running in BH disabled context with netif_tx_lock
Stephen Hemmingerbea33482007-10-03 16:41:36 -07006629 * and TX reclaim runs via tp->napi.poll inside of a software
David S. Millerf47c11e2005-06-24 20:18:35 -07006630 * interrupt. Furthermore, IRQ processing runs lockless so we have
6631 * no IRQ context deadlocks to worry about either. Rejoice!
Linus Torvalds1da177e2005-04-16 15:20:36 -07006632 */
Matt Carlson84b67b22011-07-27 14:20:52 +00006633 if (unlikely(budget <= (skb_shinfo(skb)->nr_frags + 1))) {
Matt Carlson24f4efd2009-11-13 13:03:35 +00006634 if (!netif_tx_queue_stopped(txq)) {
6635 netif_tx_stop_queue(txq);
Stephen Hemminger1f064a82005-12-06 17:36:44 -08006636
6637 /* This is a hard error, log it. */
Matt Carlson5129c3a2010-04-05 10:19:23 +00006638 netdev_err(dev,
6639 "BUG! Tx Ring full when queue awake!\n");
Stephen Hemminger1f064a82005-12-06 17:36:44 -08006640 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006641 return NETDEV_TX_BUSY;
6642 }
6643
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006644 entry = tnapi->tx_prod;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006645 base_flags = 0;
Patrick McHardy84fa7932006-08-29 16:44:56 -07006646 if (skb->ip_summed == CHECKSUM_PARTIAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006647 base_flags |= TXD_FLAG_TCPUDP_CSUM;
Matt Carlson24f4efd2009-11-13 13:03:35 +00006648
Matt Carlsonbe98da62010-07-11 09:31:46 +00006649 mss = skb_shinfo(skb)->gso_size;
6650 if (mss) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07006651 struct iphdr *iph;
Matt Carlson34195c32010-07-11 09:31:42 +00006652 u32 tcp_opt_len, hdr_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006653
6654 if (skb_header_cloned(skb) &&
Eric Dumazet48855432011-10-24 07:53:03 +00006655 pskb_expand_head(skb, 0, 0, GFP_ATOMIC))
6656 goto drop;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006657
Matt Carlson34195c32010-07-11 09:31:42 +00006658 iph = ip_hdr(skb);
Arnaldo Carvalho de Meloab6a5bb2007-03-18 17:43:48 -07006659 tcp_opt_len = tcp_optlen(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006660
Matt Carlson02e96082010-09-15 08:59:59 +00006661 if (skb_is_gso_v6(skb)) {
Matt Carlson34195c32010-07-11 09:31:42 +00006662 hdr_len = skb_headlen(skb) - ETH_HLEN;
6663 } else {
6664 u32 ip_tcp_len;
6665
6666 ip_tcp_len = ip_hdrlen(skb) + sizeof(struct tcphdr);
6667 hdr_len = ip_tcp_len + tcp_opt_len;
6668
6669 iph->check = 0;
6670 iph->tot_len = htons(mss + hdr_len);
6671 }
6672
Michael Chan52c0fd82006-06-29 20:15:54 -07006673 if (unlikely((ETH_HLEN + hdr_len) > 80) &&
Joe Perches63c3a662011-04-26 08:12:10 +00006674 tg3_flag(tp, TSO_BUG))
Matt Carlsonde6f31e2010-04-12 06:58:30 +00006675 return tg3_tso_bug(tp, skb);
Michael Chan52c0fd82006-06-29 20:15:54 -07006676
Linus Torvalds1da177e2005-04-16 15:20:36 -07006677 base_flags |= (TXD_FLAG_CPU_PRE_DMA |
6678 TXD_FLAG_CPU_POST_DMA);
6679
Joe Perches63c3a662011-04-26 08:12:10 +00006680 if (tg3_flag(tp, HW_TSO_1) ||
6681 tg3_flag(tp, HW_TSO_2) ||
6682 tg3_flag(tp, HW_TSO_3)) {
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07006683 tcp_hdr(skb)->check = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006684 base_flags &= ~TXD_FLAG_TCPUDP_CSUM;
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07006685 } else
6686 tcp_hdr(skb)->check = ~csum_tcpudp_magic(iph->saddr,
6687 iph->daddr, 0,
6688 IPPROTO_TCP,
6689 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006690
Joe Perches63c3a662011-04-26 08:12:10 +00006691 if (tg3_flag(tp, HW_TSO_3)) {
Matt Carlson615774f2009-11-13 13:03:39 +00006692 mss |= (hdr_len & 0xc) << 12;
6693 if (hdr_len & 0x10)
6694 base_flags |= 0x00000010;
6695 base_flags |= (hdr_len & 0x3e0) << 5;
Joe Perches63c3a662011-04-26 08:12:10 +00006696 } else if (tg3_flag(tp, HW_TSO_2))
Matt Carlson92c6b8d2009-11-02 14:23:27 +00006697 mss |= hdr_len << 9;
Joe Perches63c3a662011-04-26 08:12:10 +00006698 else if (tg3_flag(tp, HW_TSO_1) ||
Matt Carlson92c6b8d2009-11-02 14:23:27 +00006699 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
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 mss |= (tsflags << 11);
6705 }
6706 } else {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07006707 if (tcp_opt_len || iph->ihl > 5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006708 int tsflags;
6709
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07006710 tsflags = (iph->ihl - 5) + (tcp_opt_len >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006711 base_flags |= tsflags << 12;
6712 }
6713 }
6714 }
Matt Carlsonbf933c82011-01-25 15:58:49 +00006715
Matt Carlson93a700a2011-08-31 11:44:54 +00006716 if (tg3_flag(tp, USE_JUMBO_BDFLAG) &&
6717 !mss && skb->len > VLAN_ETH_FRAME_LEN)
6718 base_flags |= TXD_FLAG_JMB_PKT;
6719
Matt Carlson92cd3a12011-07-27 14:20:47 +00006720 if (vlan_tx_tag_present(skb)) {
6721 base_flags |= TXD_FLAG_VLAN;
6722 vlan = vlan_tx_tag_get(skb);
6723 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006724
Alexander Duyckf4188d82009-12-02 16:48:38 +00006725 len = skb_headlen(skb);
6726
6727 mapping = pci_map_single(tp->pdev, skb->data, len, PCI_DMA_TODEVICE);
Eric Dumazet48855432011-10-24 07:53:03 +00006728 if (pci_dma_mapping_error(tp->pdev, mapping))
6729 goto drop;
6730
David S. Miller90079ce2008-09-11 04:52:51 -07006731
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006732 tnapi->tx_buffers[entry].skb = skb;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006733 dma_unmap_addr_set(&tnapi->tx_buffers[entry], mapping, mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006734
6735 would_hit_hwbug = 0;
6736
Joe Perches63c3a662011-04-26 08:12:10 +00006737 if (tg3_flag(tp, 5701_DMA_BUG))
Michael Chanc58ec932005-09-17 00:46:27 -07006738 would_hit_hwbug = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006739
Matt Carlson84b67b22011-07-27 14:20:52 +00006740 if (tg3_tx_frag_set(tnapi, &entry, &budget, mapping, len, base_flags |
Matt Carlsond1a3b732011-07-27 14:20:51 +00006741 ((skb_shinfo(skb)->nr_frags == 0) ? TXD_FLAG_END : 0),
Matt Carlsonba1142e2011-11-04 09:15:00 +00006742 mss, vlan)) {
Matt Carlsond1a3b732011-07-27 14:20:51 +00006743 would_hit_hwbug = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006744 /* Now loop through additional data fragments, and queue them. */
Matt Carlsonba1142e2011-11-04 09:15:00 +00006745 } else if (skb_shinfo(skb)->nr_frags > 0) {
Matt Carlson92cd3a12011-07-27 14:20:47 +00006746 u32 tmp_mss = mss;
6747
6748 if (!tg3_flag(tp, HW_TSO_1) &&
6749 !tg3_flag(tp, HW_TSO_2) &&
6750 !tg3_flag(tp, HW_TSO_3))
6751 tmp_mss = 0;
6752
Linus Torvalds1da177e2005-04-16 15:20:36 -07006753 last = skb_shinfo(skb)->nr_frags - 1;
6754 for (i = 0; i <= last; i++) {
6755 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
6756
Eric Dumazet9e903e02011-10-18 21:00:24 +00006757 len = skb_frag_size(frag);
Ian Campbelldc234d02011-08-24 22:28:11 +00006758 mapping = skb_frag_dma_map(&tp->pdev->dev, frag, 0,
Ian Campbell5d6bcdf2011-10-06 11:10:48 +01006759 len, DMA_TO_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006760
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006761 tnapi->tx_buffers[entry].skb = NULL;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006762 dma_unmap_addr_set(&tnapi->tx_buffers[entry], mapping,
Alexander Duyckf4188d82009-12-02 16:48:38 +00006763 mapping);
Ian Campbell5d6bcdf2011-10-06 11:10:48 +01006764 if (dma_mapping_error(&tp->pdev->dev, mapping))
Alexander Duyckf4188d82009-12-02 16:48:38 +00006765 goto dma_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006766
Matt Carlsonb9e45482011-11-04 09:14:59 +00006767 if (!budget ||
6768 tg3_tx_frag_set(tnapi, &entry, &budget, mapping,
Matt Carlson84b67b22011-07-27 14:20:52 +00006769 len, base_flags |
6770 ((i == last) ? TXD_FLAG_END : 0),
Matt Carlsonb9e45482011-11-04 09:14:59 +00006771 tmp_mss, vlan)) {
Matt Carlson92c6b8d2009-11-02 14:23:27 +00006772 would_hit_hwbug = 1;
Matt Carlsonb9e45482011-11-04 09:14:59 +00006773 break;
6774 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006775 }
6776 }
6777
6778 if (would_hit_hwbug) {
Matt Carlson0d681b22011-07-27 14:20:49 +00006779 tg3_tx_skb_unmap(tnapi, tnapi->tx_prod, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006780
6781 /* If the workaround fails due to memory/mapping
6782 * failure, silently drop this packet.
6783 */
Matt Carlson84b67b22011-07-27 14:20:52 +00006784 entry = tnapi->tx_prod;
6785 budget = tg3_tx_avail(tnapi);
David S. Miller1805b2f2011-10-24 18:18:09 -04006786 if (tigon3_dma_hwbug_workaround(tnapi, &skb, &entry, &budget,
Matt Carlson84b67b22011-07-27 14:20:52 +00006787 base_flags, mss, vlan))
Eric Dumazet48855432011-10-24 07:53:03 +00006788 goto drop_nofree;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006789 }
6790
Richard Cochrand515b452011-06-19 03:31:41 +00006791 skb_tx_timestamp(skb);
6792
Linus Torvalds1da177e2005-04-16 15:20:36 -07006793 /* Packets are ready, update Tx producer idx local and on card. */
Matt Carlson24f4efd2009-11-13 13:03:35 +00006794 tw32_tx_mbox(tnapi->prodmbox, entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006795
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006796 tnapi->tx_prod = entry;
6797 if (unlikely(tg3_tx_avail(tnapi) <= (MAX_SKB_FRAGS + 1))) {
Matt Carlson24f4efd2009-11-13 13:03:35 +00006798 netif_tx_stop_queue(txq);
Matt Carlsonf65aac12010-08-02 11:26:03 +00006799
6800 /* netif_tx_stop_queue() must be done before checking
6801 * checking tx index in tg3_tx_avail() below, because in
6802 * tg3_tx(), we update tx index before checking for
6803 * netif_tx_queue_stopped().
6804 */
6805 smp_mb();
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006806 if (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi))
Matt Carlson24f4efd2009-11-13 13:03:35 +00006807 netif_tx_wake_queue(txq);
Michael Chan51b91462005-09-01 17:41:28 -07006808 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006809
Eric Dumazetcdd0db02009-05-28 00:00:41 +00006810 mmiowb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006811 return NETDEV_TX_OK;
Alexander Duyckf4188d82009-12-02 16:48:38 +00006812
6813dma_error:
Matt Carlsonba1142e2011-11-04 09:15:00 +00006814 tg3_tx_skb_unmap(tnapi, tnapi->tx_prod, --i);
Matt Carlson432aa7e2011-05-19 12:12:45 +00006815 tnapi->tx_buffers[tnapi->tx_prod].skb = NULL;
Eric Dumazet48855432011-10-24 07:53:03 +00006816drop:
6817 dev_kfree_skb(skb);
6818drop_nofree:
6819 tp->tx_dropped++;
Alexander Duyckf4188d82009-12-02 16:48:38 +00006820 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006821}
6822
Matt Carlson6e01b202011-08-19 13:58:20 +00006823static void tg3_mac_loopback(struct tg3 *tp, bool enable)
6824{
6825 if (enable) {
6826 tp->mac_mode &= ~(MAC_MODE_HALF_DUPLEX |
6827 MAC_MODE_PORT_MODE_MASK);
6828
6829 tp->mac_mode |= MAC_MODE_PORT_INT_LPBACK;
6830
6831 if (!tg3_flag(tp, 5705_PLUS))
6832 tp->mac_mode |= MAC_MODE_LINK_POLARITY;
6833
6834 if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
6835 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
6836 else
6837 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
6838 } else {
6839 tp->mac_mode &= ~MAC_MODE_PORT_INT_LPBACK;
6840
6841 if (tg3_flag(tp, 5705_PLUS) ||
6842 (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) ||
6843 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700)
6844 tp->mac_mode &= ~MAC_MODE_LINK_POLARITY;
6845 }
6846
6847 tw32(MAC_MODE, tp->mac_mode);
6848 udelay(40);
6849}
6850
Matt Carlson941ec902011-08-19 13:58:23 +00006851static int tg3_phy_lpbk_set(struct tg3 *tp, u32 speed, bool extlpbk)
Matt Carlson5e5a7f32011-08-19 13:58:21 +00006852{
Matt Carlson941ec902011-08-19 13:58:23 +00006853 u32 val, bmcr, mac_mode, ptest = 0;
Matt Carlson5e5a7f32011-08-19 13:58:21 +00006854
6855 tg3_phy_toggle_apd(tp, false);
6856 tg3_phy_toggle_automdix(tp, 0);
6857
Matt Carlson941ec902011-08-19 13:58:23 +00006858 if (extlpbk && tg3_phy_set_extloopbk(tp))
6859 return -EIO;
6860
6861 bmcr = BMCR_FULLDPLX;
Matt Carlson5e5a7f32011-08-19 13:58:21 +00006862 switch (speed) {
6863 case SPEED_10:
6864 break;
6865 case SPEED_100:
6866 bmcr |= BMCR_SPEED100;
6867 break;
6868 case SPEED_1000:
6869 default:
6870 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
6871 speed = SPEED_100;
6872 bmcr |= BMCR_SPEED100;
6873 } else {
6874 speed = SPEED_1000;
6875 bmcr |= BMCR_SPEED1000;
6876 }
6877 }
6878
Matt Carlson941ec902011-08-19 13:58:23 +00006879 if (extlpbk) {
6880 if (!(tp->phy_flags & TG3_PHYFLG_IS_FET)) {
6881 tg3_readphy(tp, MII_CTRL1000, &val);
6882 val |= CTL1000_AS_MASTER |
6883 CTL1000_ENABLE_MASTER;
6884 tg3_writephy(tp, MII_CTRL1000, val);
6885 } else {
6886 ptest = MII_TG3_FET_PTEST_TRIM_SEL |
6887 MII_TG3_FET_PTEST_TRIM_2;
6888 tg3_writephy(tp, MII_TG3_FET_PTEST, ptest);
6889 }
6890 } else
6891 bmcr |= BMCR_LOOPBACK;
6892
Matt Carlson5e5a7f32011-08-19 13:58:21 +00006893 tg3_writephy(tp, MII_BMCR, bmcr);
6894
6895 /* The write needs to be flushed for the FETs */
6896 if (tp->phy_flags & TG3_PHYFLG_IS_FET)
6897 tg3_readphy(tp, MII_BMCR, &bmcr);
6898
6899 udelay(40);
6900
6901 if ((tp->phy_flags & TG3_PHYFLG_IS_FET) &&
6902 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785) {
Matt Carlson941ec902011-08-19 13:58:23 +00006903 tg3_writephy(tp, MII_TG3_FET_PTEST, ptest |
Matt Carlson5e5a7f32011-08-19 13:58:21 +00006904 MII_TG3_FET_PTEST_FRC_TX_LINK |
6905 MII_TG3_FET_PTEST_FRC_TX_LOCK);
6906
6907 /* The write needs to be flushed for the AC131 */
6908 tg3_readphy(tp, MII_TG3_FET_PTEST, &val);
6909 }
6910
6911 /* Reset to prevent losing 1st rx packet intermittently */
6912 if ((tp->phy_flags & TG3_PHYFLG_MII_SERDES) &&
6913 tg3_flag(tp, 5780_CLASS)) {
6914 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
6915 udelay(10);
6916 tw32_f(MAC_RX_MODE, tp->rx_mode);
6917 }
6918
6919 mac_mode = tp->mac_mode &
6920 ~(MAC_MODE_PORT_MODE_MASK | MAC_MODE_HALF_DUPLEX);
6921 if (speed == SPEED_1000)
6922 mac_mode |= MAC_MODE_PORT_MODE_GMII;
6923 else
6924 mac_mode |= MAC_MODE_PORT_MODE_MII;
6925
6926 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700) {
6927 u32 masked_phy_id = tp->phy_id & TG3_PHY_ID_MASK;
6928
6929 if (masked_phy_id == TG3_PHY_ID_BCM5401)
6930 mac_mode &= ~MAC_MODE_LINK_POLARITY;
6931 else if (masked_phy_id == TG3_PHY_ID_BCM5411)
6932 mac_mode |= MAC_MODE_LINK_POLARITY;
6933
6934 tg3_writephy(tp, MII_TG3_EXT_CTRL,
6935 MII_TG3_EXT_CTRL_LNK3_LED_MODE);
6936 }
6937
6938 tw32(MAC_MODE, mac_mode);
6939 udelay(40);
Matt Carlson941ec902011-08-19 13:58:23 +00006940
6941 return 0;
Matt Carlson5e5a7f32011-08-19 13:58:21 +00006942}
6943
Michał Mirosławc8f44af2011-11-15 15:29:55 +00006944static void tg3_set_loopback(struct net_device *dev, netdev_features_t features)
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00006945{
6946 struct tg3 *tp = netdev_priv(dev);
6947
6948 if (features & NETIF_F_LOOPBACK) {
6949 if (tp->mac_mode & MAC_MODE_PORT_INT_LPBACK)
6950 return;
6951
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00006952 spin_lock_bh(&tp->lock);
Matt Carlson6e01b202011-08-19 13:58:20 +00006953 tg3_mac_loopback(tp, true);
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00006954 netif_carrier_on(tp->dev);
6955 spin_unlock_bh(&tp->lock);
6956 netdev_info(dev, "Internal MAC loopback mode enabled.\n");
6957 } else {
6958 if (!(tp->mac_mode & MAC_MODE_PORT_INT_LPBACK))
6959 return;
6960
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00006961 spin_lock_bh(&tp->lock);
Matt Carlson6e01b202011-08-19 13:58:20 +00006962 tg3_mac_loopback(tp, false);
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00006963 /* Force link status check */
6964 tg3_setup_phy(tp, 1);
6965 spin_unlock_bh(&tp->lock);
6966 netdev_info(dev, "Internal MAC loopback mode disabled.\n");
6967 }
6968}
6969
Michał Mirosławc8f44af2011-11-15 15:29:55 +00006970static netdev_features_t tg3_fix_features(struct net_device *dev,
6971 netdev_features_t features)
Michał Mirosławdc668912011-04-07 03:35:07 +00006972{
6973 struct tg3 *tp = netdev_priv(dev);
6974
Joe Perches63c3a662011-04-26 08:12:10 +00006975 if (dev->mtu > ETH_DATA_LEN && tg3_flag(tp, 5780_CLASS))
Michał Mirosławdc668912011-04-07 03:35:07 +00006976 features &= ~NETIF_F_ALL_TSO;
6977
6978 return features;
6979}
6980
Michał Mirosławc8f44af2011-11-15 15:29:55 +00006981static int tg3_set_features(struct net_device *dev, netdev_features_t features)
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00006982{
Michał Mirosławc8f44af2011-11-15 15:29:55 +00006983 netdev_features_t changed = dev->features ^ features;
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00006984
6985 if ((changed & NETIF_F_LOOPBACK) && netif_running(dev))
6986 tg3_set_loopback(dev, features);
6987
6988 return 0;
6989}
6990
Linus Torvalds1da177e2005-04-16 15:20:36 -07006991static inline void tg3_set_mtu(struct net_device *dev, struct tg3 *tp,
6992 int new_mtu)
6993{
6994 dev->mtu = new_mtu;
6995
Michael Chanef7f5ec2005-07-25 12:32:25 -07006996 if (new_mtu > ETH_DATA_LEN) {
Joe Perches63c3a662011-04-26 08:12:10 +00006997 if (tg3_flag(tp, 5780_CLASS)) {
Michał Mirosławdc668912011-04-07 03:35:07 +00006998 netdev_update_features(dev);
Joe Perches63c3a662011-04-26 08:12:10 +00006999 tg3_flag_clear(tp, TSO_CAPABLE);
Matt Carlson859a5882010-04-05 10:19:28 +00007000 } else {
Joe Perches63c3a662011-04-26 08:12:10 +00007001 tg3_flag_set(tp, JUMBO_RING_ENABLE);
Matt Carlson859a5882010-04-05 10:19:28 +00007002 }
Michael Chanef7f5ec2005-07-25 12:32:25 -07007003 } else {
Joe Perches63c3a662011-04-26 08:12:10 +00007004 if (tg3_flag(tp, 5780_CLASS)) {
7005 tg3_flag_set(tp, TSO_CAPABLE);
Michał Mirosławdc668912011-04-07 03:35:07 +00007006 netdev_update_features(dev);
7007 }
Joe Perches63c3a662011-04-26 08:12:10 +00007008 tg3_flag_clear(tp, JUMBO_RING_ENABLE);
Michael Chanef7f5ec2005-07-25 12:32:25 -07007009 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007010}
7011
7012static int tg3_change_mtu(struct net_device *dev, int new_mtu)
7013{
7014 struct tg3 *tp = netdev_priv(dev);
Michael Chanb9ec6c12006-07-25 16:37:27 -07007015 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007016
7017 if (new_mtu < TG3_MIN_MTU || new_mtu > TG3_MAX_MTU(tp))
7018 return -EINVAL;
7019
7020 if (!netif_running(dev)) {
7021 /* We'll just catch it later when the
7022 * device is up'd.
7023 */
7024 tg3_set_mtu(dev, tp, new_mtu);
7025 return 0;
7026 }
7027
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07007028 tg3_phy_stop(tp);
7029
Linus Torvalds1da177e2005-04-16 15:20:36 -07007030 tg3_netif_stop(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -07007031
7032 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007033
Michael Chan944d9802005-05-29 14:57:48 -07007034 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007035
7036 tg3_set_mtu(dev, tp, new_mtu);
7037
Michael Chanb9ec6c12006-07-25 16:37:27 -07007038 err = tg3_restart_hw(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007039
Michael Chanb9ec6c12006-07-25 16:37:27 -07007040 if (!err)
7041 tg3_netif_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007042
David S. Millerf47c11e2005-06-24 20:18:35 -07007043 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007044
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07007045 if (!err)
7046 tg3_phy_start(tp);
7047
Michael Chanb9ec6c12006-07-25 16:37:27 -07007048 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007049}
7050
Matt Carlson21f581a2009-08-28 14:00:25 +00007051static void tg3_rx_prodring_free(struct tg3 *tp,
7052 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007053{
Linus Torvalds1da177e2005-04-16 15:20:36 -07007054 int i;
7055
Matt Carlson8fea32b2010-09-15 08:59:58 +00007056 if (tpr != &tp->napi[0].prodring) {
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007057 for (i = tpr->rx_std_cons_idx; i != tpr->rx_std_prod_idx;
Matt Carlson2c49a442010-09-30 10:34:35 +00007058 i = (i + 1) & tp->rx_std_ring_mask)
Eric Dumazet9205fd92011-11-18 06:47:01 +00007059 tg3_rx_data_free(tp, &tpr->rx_std_buffers[i],
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007060 tp->rx_pkt_map_sz);
7061
Joe Perches63c3a662011-04-26 08:12:10 +00007062 if (tg3_flag(tp, JUMBO_CAPABLE)) {
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007063 for (i = tpr->rx_jmb_cons_idx;
7064 i != tpr->rx_jmb_prod_idx;
Matt Carlson2c49a442010-09-30 10:34:35 +00007065 i = (i + 1) & tp->rx_jmb_ring_mask) {
Eric Dumazet9205fd92011-11-18 06:47:01 +00007066 tg3_rx_data_free(tp, &tpr->rx_jmb_buffers[i],
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007067 TG3_RX_JMB_MAP_SZ);
7068 }
7069 }
7070
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007071 return;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007072 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007073
Matt Carlson2c49a442010-09-30 10:34:35 +00007074 for (i = 0; i <= tp->rx_std_ring_mask; i++)
Eric Dumazet9205fd92011-11-18 06:47:01 +00007075 tg3_rx_data_free(tp, &tpr->rx_std_buffers[i],
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007076 tp->rx_pkt_map_sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007077
Joe Perches63c3a662011-04-26 08:12:10 +00007078 if (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS)) {
Matt Carlson2c49a442010-09-30 10:34:35 +00007079 for (i = 0; i <= tp->rx_jmb_ring_mask; i++)
Eric Dumazet9205fd92011-11-18 06:47:01 +00007080 tg3_rx_data_free(tp, &tpr->rx_jmb_buffers[i],
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007081 TG3_RX_JMB_MAP_SZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007082 }
7083}
7084
Matt Carlsonc6cdf432010-04-05 10:19:26 +00007085/* Initialize rx rings for packet processing.
Linus Torvalds1da177e2005-04-16 15:20:36 -07007086 *
7087 * The chip has been shut down and the driver detached from
7088 * the networking, so no interrupts or new tx packets will
7089 * end up in the driver. tp->{tx,}lock are held and thus
7090 * we may not sleep.
7091 */
Matt Carlson21f581a2009-08-28 14:00:25 +00007092static int tg3_rx_prodring_alloc(struct tg3 *tp,
7093 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007094{
Matt Carlson287be122009-08-28 13:58:46 +00007095 u32 i, rx_pkt_dma_sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007096
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007097 tpr->rx_std_cons_idx = 0;
7098 tpr->rx_std_prod_idx = 0;
7099 tpr->rx_jmb_cons_idx = 0;
7100 tpr->rx_jmb_prod_idx = 0;
7101
Matt Carlson8fea32b2010-09-15 08:59:58 +00007102 if (tpr != &tp->napi[0].prodring) {
Matt Carlson2c49a442010-09-30 10:34:35 +00007103 memset(&tpr->rx_std_buffers[0], 0,
7104 TG3_RX_STD_BUFF_RING_SIZE(tp));
Matt Carlson48035722010-10-14 10:37:43 +00007105 if (tpr->rx_jmb_buffers)
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007106 memset(&tpr->rx_jmb_buffers[0], 0,
Matt Carlson2c49a442010-09-30 10:34:35 +00007107 TG3_RX_JMB_BUFF_RING_SIZE(tp));
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007108 goto done;
7109 }
7110
Linus Torvalds1da177e2005-04-16 15:20:36 -07007111 /* Zero out all descriptors. */
Matt Carlson2c49a442010-09-30 10:34:35 +00007112 memset(tpr->rx_std, 0, TG3_RX_STD_RING_BYTES(tp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007113
Matt Carlson287be122009-08-28 13:58:46 +00007114 rx_pkt_dma_sz = TG3_RX_STD_DMA_SZ;
Joe Perches63c3a662011-04-26 08:12:10 +00007115 if (tg3_flag(tp, 5780_CLASS) &&
Matt Carlson287be122009-08-28 13:58:46 +00007116 tp->dev->mtu > ETH_DATA_LEN)
7117 rx_pkt_dma_sz = TG3_RX_JMB_DMA_SZ;
7118 tp->rx_pkt_map_sz = TG3_RX_DMA_TO_MAP_SZ(rx_pkt_dma_sz);
Michael Chan7e72aad2005-07-25 12:31:17 -07007119
Linus Torvalds1da177e2005-04-16 15:20:36 -07007120 /* Initialize invariants of the rings, we only set this
7121 * stuff once. This works because the card does not
7122 * write into the rx buffer posting rings.
7123 */
Matt Carlson2c49a442010-09-30 10:34:35 +00007124 for (i = 0; i <= tp->rx_std_ring_mask; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007125 struct tg3_rx_buffer_desc *rxd;
7126
Matt Carlson21f581a2009-08-28 14:00:25 +00007127 rxd = &tpr->rx_std[i];
Matt Carlson287be122009-08-28 13:58:46 +00007128 rxd->idx_len = rx_pkt_dma_sz << RXD_LEN_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007129 rxd->type_flags = (RXD_FLAG_END << RXD_FLAGS_SHIFT);
7130 rxd->opaque = (RXD_OPAQUE_RING_STD |
7131 (i << RXD_OPAQUE_INDEX_SHIFT));
7132 }
7133
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007134 /* Now allocate fresh SKBs for each rx ring. */
7135 for (i = 0; i < tp->rx_pending; i++) {
Eric Dumazet9205fd92011-11-18 06:47:01 +00007136 if (tg3_alloc_rx_data(tp, tpr, RXD_OPAQUE_RING_STD, i) < 0) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00007137 netdev_warn(tp->dev,
7138 "Using a smaller RX standard ring. Only "
7139 "%d out of %d buffers were allocated "
7140 "successfully\n", i, tp->rx_pending);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007141 if (i == 0)
7142 goto initfail;
7143 tp->rx_pending = i;
7144 break;
7145 }
7146 }
7147
Joe Perches63c3a662011-04-26 08:12:10 +00007148 if (!tg3_flag(tp, JUMBO_CAPABLE) || tg3_flag(tp, 5780_CLASS))
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007149 goto done;
7150
Matt Carlson2c49a442010-09-30 10:34:35 +00007151 memset(tpr->rx_jmb, 0, TG3_RX_JMB_RING_BYTES(tp));
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007152
Joe Perches63c3a662011-04-26 08:12:10 +00007153 if (!tg3_flag(tp, JUMBO_RING_ENABLE))
Matt Carlson0d86df82010-02-17 15:17:00 +00007154 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007155
Matt Carlson2c49a442010-09-30 10:34:35 +00007156 for (i = 0; i <= tp->rx_jmb_ring_mask; i++) {
Matt Carlson0d86df82010-02-17 15:17:00 +00007157 struct tg3_rx_buffer_desc *rxd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007158
Matt Carlson0d86df82010-02-17 15:17:00 +00007159 rxd = &tpr->rx_jmb[i].std;
7160 rxd->idx_len = TG3_RX_JMB_DMA_SZ << RXD_LEN_SHIFT;
7161 rxd->type_flags = (RXD_FLAG_END << RXD_FLAGS_SHIFT) |
7162 RXD_FLAG_JUMBO;
7163 rxd->opaque = (RXD_OPAQUE_RING_JUMBO |
7164 (i << RXD_OPAQUE_INDEX_SHIFT));
7165 }
7166
7167 for (i = 0; i < tp->rx_jumbo_pending; i++) {
Eric Dumazet9205fd92011-11-18 06:47:01 +00007168 if (tg3_alloc_rx_data(tp, tpr, RXD_OPAQUE_RING_JUMBO, i) < 0) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00007169 netdev_warn(tp->dev,
7170 "Using a smaller RX jumbo ring. Only %d "
7171 "out of %d buffers were allocated "
7172 "successfully\n", i, tp->rx_jumbo_pending);
Matt Carlson0d86df82010-02-17 15:17:00 +00007173 if (i == 0)
7174 goto initfail;
7175 tp->rx_jumbo_pending = i;
7176 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007177 }
7178 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007179
7180done:
Michael Chan32d8c572006-07-25 16:38:29 -07007181 return 0;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007182
7183initfail:
Matt Carlson21f581a2009-08-28 14:00:25 +00007184 tg3_rx_prodring_free(tp, tpr);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007185 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007186}
7187
Matt Carlson21f581a2009-08-28 14:00:25 +00007188static void tg3_rx_prodring_fini(struct tg3 *tp,
7189 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007190{
Matt Carlson21f581a2009-08-28 14:00:25 +00007191 kfree(tpr->rx_std_buffers);
7192 tpr->rx_std_buffers = NULL;
7193 kfree(tpr->rx_jmb_buffers);
7194 tpr->rx_jmb_buffers = NULL;
7195 if (tpr->rx_std) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00007196 dma_free_coherent(&tp->pdev->dev, TG3_RX_STD_RING_BYTES(tp),
7197 tpr->rx_std, tpr->rx_std_mapping);
Matt Carlson21f581a2009-08-28 14:00:25 +00007198 tpr->rx_std = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007199 }
Matt Carlson21f581a2009-08-28 14:00:25 +00007200 if (tpr->rx_jmb) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00007201 dma_free_coherent(&tp->pdev->dev, TG3_RX_JMB_RING_BYTES(tp),
7202 tpr->rx_jmb, tpr->rx_jmb_mapping);
Matt Carlson21f581a2009-08-28 14:00:25 +00007203 tpr->rx_jmb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007204 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007205}
7206
Matt Carlson21f581a2009-08-28 14:00:25 +00007207static int tg3_rx_prodring_init(struct tg3 *tp,
7208 struct tg3_rx_prodring_set *tpr)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007209{
Matt Carlson2c49a442010-09-30 10:34:35 +00007210 tpr->rx_std_buffers = kzalloc(TG3_RX_STD_BUFF_RING_SIZE(tp),
7211 GFP_KERNEL);
Matt Carlson21f581a2009-08-28 14:00:25 +00007212 if (!tpr->rx_std_buffers)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007213 return -ENOMEM;
7214
Matt Carlson4bae65c2010-11-24 08:31:52 +00007215 tpr->rx_std = dma_alloc_coherent(&tp->pdev->dev,
7216 TG3_RX_STD_RING_BYTES(tp),
7217 &tpr->rx_std_mapping,
7218 GFP_KERNEL);
Matt Carlson21f581a2009-08-28 14:00:25 +00007219 if (!tpr->rx_std)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007220 goto err_out;
7221
Joe Perches63c3a662011-04-26 08:12:10 +00007222 if (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS)) {
Matt Carlson2c49a442010-09-30 10:34:35 +00007223 tpr->rx_jmb_buffers = kzalloc(TG3_RX_JMB_BUFF_RING_SIZE(tp),
Matt Carlson21f581a2009-08-28 14:00:25 +00007224 GFP_KERNEL);
7225 if (!tpr->rx_jmb_buffers)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007226 goto err_out;
7227
Matt Carlson4bae65c2010-11-24 08:31:52 +00007228 tpr->rx_jmb = dma_alloc_coherent(&tp->pdev->dev,
7229 TG3_RX_JMB_RING_BYTES(tp),
7230 &tpr->rx_jmb_mapping,
7231 GFP_KERNEL);
Matt Carlson21f581a2009-08-28 14:00:25 +00007232 if (!tpr->rx_jmb)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007233 goto err_out;
7234 }
7235
7236 return 0;
7237
7238err_out:
Matt Carlson21f581a2009-08-28 14:00:25 +00007239 tg3_rx_prodring_fini(tp, tpr);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007240 return -ENOMEM;
7241}
7242
7243/* Free up pending packets in all rx/tx rings.
7244 *
7245 * The chip has been shut down and the driver detached from
7246 * the networking, so no interrupts or new tx packets will
7247 * end up in the driver. tp->{tx,}lock is not held and we are not
7248 * in an interrupt context and thus may sleep.
7249 */
7250static void tg3_free_rings(struct tg3 *tp)
7251{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007252 int i, j;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007253
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007254 for (j = 0; j < tp->irq_cnt; j++) {
7255 struct tg3_napi *tnapi = &tp->napi[j];
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007256
Matt Carlson8fea32b2010-09-15 08:59:58 +00007257 tg3_rx_prodring_free(tp, &tnapi->prodring);
Matt Carlsonb28f6422010-06-05 17:24:32 +00007258
Matt Carlson0c1d0e22009-09-01 13:16:33 +00007259 if (!tnapi->tx_buffers)
7260 continue;
7261
Matt Carlson0d681b22011-07-27 14:20:49 +00007262 for (i = 0; i < TG3_TX_RING_SIZE; i++) {
7263 struct sk_buff *skb = tnapi->tx_buffers[i].skb;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007264
Matt Carlson0d681b22011-07-27 14:20:49 +00007265 if (!skb)
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007266 continue;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007267
Matt Carlsonba1142e2011-11-04 09:15:00 +00007268 tg3_tx_skb_unmap(tnapi, i,
7269 skb_shinfo(skb)->nr_frags - 1);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007270
7271 dev_kfree_skb_any(skb);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007272 }
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007273 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007274}
7275
7276/* Initialize tx/rx rings for packet processing.
7277 *
7278 * The chip has been shut down and the driver detached from
7279 * the networking, so no interrupts or new tx packets will
7280 * end up in the driver. tp->{tx,}lock are held and thus
7281 * we may not sleep.
7282 */
7283static int tg3_init_rings(struct tg3 *tp)
7284{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007285 int i;
Matt Carlson72334482009-08-28 14:03:01 +00007286
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007287 /* Free up all the SKBs. */
7288 tg3_free_rings(tp);
7289
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007290 for (i = 0; i < tp->irq_cnt; i++) {
7291 struct tg3_napi *tnapi = &tp->napi[i];
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007292
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007293 tnapi->last_tag = 0;
7294 tnapi->last_irq_tag = 0;
7295 tnapi->hw_status->status = 0;
7296 tnapi->hw_status->status_tag = 0;
7297 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
7298
7299 tnapi->tx_prod = 0;
7300 tnapi->tx_cons = 0;
Matt Carlson0c1d0e22009-09-01 13:16:33 +00007301 if (tnapi->tx_ring)
7302 memset(tnapi->tx_ring, 0, TG3_TX_RING_BYTES);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007303
7304 tnapi->rx_rcb_ptr = 0;
Matt Carlson0c1d0e22009-09-01 13:16:33 +00007305 if (tnapi->rx_rcb)
7306 memset(tnapi->rx_rcb, 0, TG3_RX_RCB_RING_BYTES(tp));
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007307
Matt Carlson8fea32b2010-09-15 08:59:58 +00007308 if (tg3_rx_prodring_alloc(tp, &tnapi->prodring)) {
Matt Carlsone4af1af2010-02-12 14:47:05 +00007309 tg3_free_rings(tp);
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007310 return -ENOMEM;
Matt Carlsone4af1af2010-02-12 14:47:05 +00007311 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007312 }
Matt Carlson72334482009-08-28 14:03:01 +00007313
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007314 return 0;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007315}
7316
7317/*
7318 * Must not be invoked with interrupt sources disabled and
7319 * the hardware shutdown down.
7320 */
7321static void tg3_free_consistent(struct tg3 *tp)
7322{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007323 int i;
Matt Carlson898a56f2009-08-28 14:02:40 +00007324
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007325 for (i = 0; i < tp->irq_cnt; i++) {
7326 struct tg3_napi *tnapi = &tp->napi[i];
7327
7328 if (tnapi->tx_ring) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00007329 dma_free_coherent(&tp->pdev->dev, TG3_TX_RING_BYTES,
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007330 tnapi->tx_ring, tnapi->tx_desc_mapping);
7331 tnapi->tx_ring = NULL;
7332 }
7333
7334 kfree(tnapi->tx_buffers);
7335 tnapi->tx_buffers = NULL;
7336
7337 if (tnapi->rx_rcb) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00007338 dma_free_coherent(&tp->pdev->dev,
7339 TG3_RX_RCB_RING_BYTES(tp),
7340 tnapi->rx_rcb,
7341 tnapi->rx_rcb_mapping);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007342 tnapi->rx_rcb = NULL;
7343 }
7344
Matt Carlson8fea32b2010-09-15 08:59:58 +00007345 tg3_rx_prodring_fini(tp, &tnapi->prodring);
7346
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007347 if (tnapi->hw_status) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00007348 dma_free_coherent(&tp->pdev->dev, TG3_HW_STATUS_SIZE,
7349 tnapi->hw_status,
7350 tnapi->status_mapping);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007351 tnapi->hw_status = NULL;
7352 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007353 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007354
Linus Torvalds1da177e2005-04-16 15:20:36 -07007355 if (tp->hw_stats) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00007356 dma_free_coherent(&tp->pdev->dev, sizeof(struct tg3_hw_stats),
7357 tp->hw_stats, tp->stats_mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007358 tp->hw_stats = NULL;
7359 }
7360}
7361
7362/*
7363 * Must not be invoked with interrupt sources disabled and
7364 * the hardware shutdown down. Can sleep.
7365 */
7366static int tg3_alloc_consistent(struct tg3 *tp)
7367{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007368 int i;
Matt Carlson898a56f2009-08-28 14:02:40 +00007369
Matt Carlson4bae65c2010-11-24 08:31:52 +00007370 tp->hw_stats = dma_alloc_coherent(&tp->pdev->dev,
7371 sizeof(struct tg3_hw_stats),
7372 &tp->stats_mapping,
7373 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007374 if (!tp->hw_stats)
7375 goto err_out;
7376
Linus Torvalds1da177e2005-04-16 15:20:36 -07007377 memset(tp->hw_stats, 0, sizeof(struct tg3_hw_stats));
7378
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007379 for (i = 0; i < tp->irq_cnt; i++) {
7380 struct tg3_napi *tnapi = &tp->napi[i];
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00007381 struct tg3_hw_status *sblk;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007382
Matt Carlson4bae65c2010-11-24 08:31:52 +00007383 tnapi->hw_status = dma_alloc_coherent(&tp->pdev->dev,
7384 TG3_HW_STATUS_SIZE,
7385 &tnapi->status_mapping,
7386 GFP_KERNEL);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007387 if (!tnapi->hw_status)
7388 goto err_out;
7389
7390 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00007391 sblk = tnapi->hw_status;
7392
Matt Carlson8fea32b2010-09-15 08:59:58 +00007393 if (tg3_rx_prodring_init(tp, &tnapi->prodring))
7394 goto err_out;
7395
Matt Carlson19cfaec2009-12-03 08:36:20 +00007396 /* If multivector TSS is enabled, vector 0 does not handle
7397 * tx interrupts. Don't allocate any resources for it.
7398 */
Joe Perches63c3a662011-04-26 08:12:10 +00007399 if ((!i && !tg3_flag(tp, ENABLE_TSS)) ||
7400 (i && tg3_flag(tp, ENABLE_TSS))) {
Matt Carlsondf8944c2011-07-27 14:20:46 +00007401 tnapi->tx_buffers = kzalloc(
7402 sizeof(struct tg3_tx_ring_info) *
7403 TG3_TX_RING_SIZE, GFP_KERNEL);
Matt Carlson19cfaec2009-12-03 08:36:20 +00007404 if (!tnapi->tx_buffers)
7405 goto err_out;
7406
Matt Carlson4bae65c2010-11-24 08:31:52 +00007407 tnapi->tx_ring = dma_alloc_coherent(&tp->pdev->dev,
7408 TG3_TX_RING_BYTES,
7409 &tnapi->tx_desc_mapping,
7410 GFP_KERNEL);
Matt Carlson19cfaec2009-12-03 08:36:20 +00007411 if (!tnapi->tx_ring)
7412 goto err_out;
7413 }
7414
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00007415 /*
7416 * When RSS is enabled, the status block format changes
7417 * slightly. The "rx_jumbo_consumer", "reserved",
7418 * and "rx_mini_consumer" members get mapped to the
7419 * other three rx return ring producer indexes.
7420 */
7421 switch (i) {
7422 default:
7423 tnapi->rx_rcb_prod_idx = &sblk->idx[0].rx_producer;
7424 break;
7425 case 2:
7426 tnapi->rx_rcb_prod_idx = &sblk->rx_jumbo_consumer;
7427 break;
7428 case 3:
7429 tnapi->rx_rcb_prod_idx = &sblk->reserved;
7430 break;
7431 case 4:
7432 tnapi->rx_rcb_prod_idx = &sblk->rx_mini_consumer;
7433 break;
7434 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007435
Matt Carlson0c1d0e22009-09-01 13:16:33 +00007436 /*
7437 * If multivector RSS is enabled, vector 0 does not handle
7438 * rx or tx interrupts. Don't allocate any resources for it.
7439 */
Joe Perches63c3a662011-04-26 08:12:10 +00007440 if (!i && tg3_flag(tp, ENABLE_RSS))
Matt Carlson0c1d0e22009-09-01 13:16:33 +00007441 continue;
7442
Matt Carlson4bae65c2010-11-24 08:31:52 +00007443 tnapi->rx_rcb = dma_alloc_coherent(&tp->pdev->dev,
7444 TG3_RX_RCB_RING_BYTES(tp),
7445 &tnapi->rx_rcb_mapping,
7446 GFP_KERNEL);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007447 if (!tnapi->rx_rcb)
7448 goto err_out;
7449
7450 memset(tnapi->rx_rcb, 0, TG3_RX_RCB_RING_BYTES(tp));
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007451 }
7452
Linus Torvalds1da177e2005-04-16 15:20:36 -07007453 return 0;
7454
7455err_out:
7456 tg3_free_consistent(tp);
7457 return -ENOMEM;
7458}
7459
7460#define MAX_WAIT_CNT 1000
7461
7462/* To stop a block, clear the enable bit and poll till it
7463 * clears. tp->lock is held.
7464 */
David S. Millerb3b7d6b2005-05-05 14:40:20 -07007465static int tg3_stop_block(struct tg3 *tp, unsigned long ofs, u32 enable_bit, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007466{
7467 unsigned int i;
7468 u32 val;
7469
Joe Perches63c3a662011-04-26 08:12:10 +00007470 if (tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007471 switch (ofs) {
7472 case RCVLSC_MODE:
7473 case DMAC_MODE:
7474 case MBFREE_MODE:
7475 case BUFMGR_MODE:
7476 case MEMARB_MODE:
7477 /* We can't enable/disable these bits of the
7478 * 5705/5750, just say success.
7479 */
7480 return 0;
7481
7482 default:
7483 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07007484 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007485 }
7486
7487 val = tr32(ofs);
7488 val &= ~enable_bit;
7489 tw32_f(ofs, val);
7490
7491 for (i = 0; i < MAX_WAIT_CNT; i++) {
7492 udelay(100);
7493 val = tr32(ofs);
7494 if ((val & enable_bit) == 0)
7495 break;
7496 }
7497
David S. Millerb3b7d6b2005-05-05 14:40:20 -07007498 if (i == MAX_WAIT_CNT && !silent) {
Matt Carlson2445e462010-04-05 10:19:21 +00007499 dev_err(&tp->pdev->dev,
7500 "tg3_stop_block timed out, ofs=%lx enable_bit=%x\n",
7501 ofs, enable_bit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007502 return -ENODEV;
7503 }
7504
7505 return 0;
7506}
7507
7508/* tp->lock is held. */
David S. Millerb3b7d6b2005-05-05 14:40:20 -07007509static int tg3_abort_hw(struct tg3 *tp, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007510{
7511 int i, err;
7512
7513 tg3_disable_ints(tp);
7514
7515 tp->rx_mode &= ~RX_MODE_ENABLE;
7516 tw32_f(MAC_RX_MODE, tp->rx_mode);
7517 udelay(10);
7518
David S. Millerb3b7d6b2005-05-05 14:40:20 -07007519 err = tg3_stop_block(tp, RCVBDI_MODE, RCVBDI_MODE_ENABLE, silent);
7520 err |= tg3_stop_block(tp, RCVLPC_MODE, RCVLPC_MODE_ENABLE, silent);
7521 err |= tg3_stop_block(tp, RCVLSC_MODE, RCVLSC_MODE_ENABLE, silent);
7522 err |= tg3_stop_block(tp, RCVDBDI_MODE, RCVDBDI_MODE_ENABLE, silent);
7523 err |= tg3_stop_block(tp, RCVDCC_MODE, RCVDCC_MODE_ENABLE, silent);
7524 err |= tg3_stop_block(tp, RCVCC_MODE, RCVCC_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007525
David S. Millerb3b7d6b2005-05-05 14:40:20 -07007526 err |= tg3_stop_block(tp, SNDBDS_MODE, SNDBDS_MODE_ENABLE, silent);
7527 err |= tg3_stop_block(tp, SNDBDI_MODE, SNDBDI_MODE_ENABLE, silent);
7528 err |= tg3_stop_block(tp, SNDDATAI_MODE, SNDDATAI_MODE_ENABLE, silent);
7529 err |= tg3_stop_block(tp, RDMAC_MODE, RDMAC_MODE_ENABLE, silent);
7530 err |= tg3_stop_block(tp, SNDDATAC_MODE, SNDDATAC_MODE_ENABLE, silent);
7531 err |= tg3_stop_block(tp, DMAC_MODE, DMAC_MODE_ENABLE, silent);
7532 err |= tg3_stop_block(tp, SNDBDC_MODE, SNDBDC_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007533
7534 tp->mac_mode &= ~MAC_MODE_TDE_ENABLE;
7535 tw32_f(MAC_MODE, tp->mac_mode);
7536 udelay(40);
7537
7538 tp->tx_mode &= ~TX_MODE_ENABLE;
7539 tw32_f(MAC_TX_MODE, tp->tx_mode);
7540
7541 for (i = 0; i < MAX_WAIT_CNT; i++) {
7542 udelay(100);
7543 if (!(tr32(MAC_TX_MODE) & TX_MODE_ENABLE))
7544 break;
7545 }
7546 if (i >= MAX_WAIT_CNT) {
Matt Carlsonab96b242010-04-05 10:19:22 +00007547 dev_err(&tp->pdev->dev,
7548 "%s timed out, TX_MODE_ENABLE will not clear "
7549 "MAC_TX_MODE=%08x\n", __func__, tr32(MAC_TX_MODE));
Michael Chane6de8ad2005-05-05 14:42:41 -07007550 err |= -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007551 }
7552
Michael Chane6de8ad2005-05-05 14:42:41 -07007553 err |= tg3_stop_block(tp, HOSTCC_MODE, HOSTCC_MODE_ENABLE, silent);
David S. Millerb3b7d6b2005-05-05 14:40:20 -07007554 err |= tg3_stop_block(tp, WDMAC_MODE, WDMAC_MODE_ENABLE, silent);
7555 err |= tg3_stop_block(tp, MBFREE_MODE, MBFREE_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007556
7557 tw32(FTQ_RESET, 0xffffffff);
7558 tw32(FTQ_RESET, 0x00000000);
7559
David S. Millerb3b7d6b2005-05-05 14:40:20 -07007560 err |= tg3_stop_block(tp, BUFMGR_MODE, BUFMGR_MODE_ENABLE, silent);
7561 err |= tg3_stop_block(tp, MEMARB_MODE, MEMARB_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007562
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007563 for (i = 0; i < tp->irq_cnt; i++) {
7564 struct tg3_napi *tnapi = &tp->napi[i];
7565 if (tnapi->hw_status)
7566 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
7567 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007568 if (tp->hw_stats)
7569 memset(tp->hw_stats, 0, sizeof(struct tg3_hw_stats));
7570
Linus Torvalds1da177e2005-04-16 15:20:36 -07007571 return err;
7572}
7573
Michael Chanee6a99b2007-07-18 21:49:10 -07007574/* Save PCI command register before chip reset */
7575static void tg3_save_pci_state(struct tg3 *tp)
7576{
Matt Carlson8a6eac92007-10-21 16:17:55 -07007577 pci_read_config_word(tp->pdev, PCI_COMMAND, &tp->pci_cmd);
Michael Chanee6a99b2007-07-18 21:49:10 -07007578}
7579
7580/* Restore PCI state after chip reset */
7581static void tg3_restore_pci_state(struct tg3 *tp)
7582{
7583 u32 val;
7584
7585 /* Re-enable indirect register accesses. */
7586 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
7587 tp->misc_host_ctrl);
7588
7589 /* Set MAX PCI retry to zero. */
7590 val = (PCISTATE_ROM_ENABLE | PCISTATE_ROM_RETRY_ENABLE);
7591 if (tp->pci_chip_rev_id == CHIPREV_ID_5704_A0 &&
Joe Perches63c3a662011-04-26 08:12:10 +00007592 tg3_flag(tp, PCIX_MODE))
Michael Chanee6a99b2007-07-18 21:49:10 -07007593 val |= PCISTATE_RETRY_SAME_DMA;
Matt Carlson0d3031d2007-10-10 18:02:43 -07007594 /* Allow reads and writes to the APE register and memory space. */
Joe Perches63c3a662011-04-26 08:12:10 +00007595 if (tg3_flag(tp, ENABLE_APE))
Matt Carlson0d3031d2007-10-10 18:02:43 -07007596 val |= PCISTATE_ALLOW_APE_CTLSPC_WR |
Matt Carlsonf92d9dc2010-06-05 17:24:30 +00007597 PCISTATE_ALLOW_APE_SHMEM_WR |
7598 PCISTATE_ALLOW_APE_PSPACE_WR;
Michael Chanee6a99b2007-07-18 21:49:10 -07007599 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE, val);
7600
Matt Carlson8a6eac92007-10-21 16:17:55 -07007601 pci_write_config_word(tp->pdev, PCI_COMMAND, tp->pci_cmd);
Michael Chanee6a99b2007-07-18 21:49:10 -07007602
Matt Carlsonfcb389d2008-11-03 16:55:44 -08007603 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785) {
Joe Perches63c3a662011-04-26 08:12:10 +00007604 if (tg3_flag(tp, PCI_EXPRESS))
Matt Carlsoncf790032010-11-24 08:31:48 +00007605 pcie_set_readrq(tp->pdev, tp->pcie_readrq);
Matt Carlsonfcb389d2008-11-03 16:55:44 -08007606 else {
7607 pci_write_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE,
7608 tp->pci_cacheline_sz);
7609 pci_write_config_byte(tp->pdev, PCI_LATENCY_TIMER,
7610 tp->pci_lat_timer);
7611 }
Michael Chan114342f2007-10-15 02:12:26 -07007612 }
Matt Carlson5f5c51e2007-11-12 21:19:37 -08007613
Michael Chanee6a99b2007-07-18 21:49:10 -07007614 /* Make sure PCI-X relaxed ordering bit is clear. */
Joe Perches63c3a662011-04-26 08:12:10 +00007615 if (tg3_flag(tp, PCIX_MODE)) {
Matt Carlson9974a352007-10-07 23:27:28 -07007616 u16 pcix_cmd;
7617
7618 pci_read_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
7619 &pcix_cmd);
7620 pcix_cmd &= ~PCI_X_CMD_ERO;
7621 pci_write_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
7622 pcix_cmd);
7623 }
Michael Chanee6a99b2007-07-18 21:49:10 -07007624
Joe Perches63c3a662011-04-26 08:12:10 +00007625 if (tg3_flag(tp, 5780_CLASS)) {
Michael Chanee6a99b2007-07-18 21:49:10 -07007626
7627 /* Chip reset on 5780 will reset MSI enable bit,
7628 * so need to restore it.
7629 */
Joe Perches63c3a662011-04-26 08:12:10 +00007630 if (tg3_flag(tp, USING_MSI)) {
Michael Chanee6a99b2007-07-18 21:49:10 -07007631 u16 ctrl;
7632
7633 pci_read_config_word(tp->pdev,
7634 tp->msi_cap + PCI_MSI_FLAGS,
7635 &ctrl);
7636 pci_write_config_word(tp->pdev,
7637 tp->msi_cap + PCI_MSI_FLAGS,
7638 ctrl | PCI_MSI_FLAGS_ENABLE);
7639 val = tr32(MSGINT_MODE);
7640 tw32(MSGINT_MODE, val | MSGINT_MODE_ENABLE);
7641 }
7642 }
7643}
7644
Linus Torvalds1da177e2005-04-16 15:20:36 -07007645/* tp->lock is held. */
7646static int tg3_chip_reset(struct tg3 *tp)
7647{
7648 u32 val;
Michael Chan1ee582d2005-08-09 20:16:46 -07007649 void (*write_op)(struct tg3 *, u32, u32);
Matt Carlson4f125f42009-09-01 12:55:02 +00007650 int i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007651
David S. Millerf49639e2006-06-09 11:58:36 -07007652 tg3_nvram_lock(tp);
7653
Matt Carlson77b483f2008-08-15 14:07:24 -07007654 tg3_ape_lock(tp, TG3_APE_LOCK_GRC);
7655
David S. Millerf49639e2006-06-09 11:58:36 -07007656 /* No matching tg3_nvram_unlock() after this because
7657 * chip reset below will undo the nvram lock.
7658 */
7659 tp->nvram_lock_cnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007660
Michael Chanee6a99b2007-07-18 21:49:10 -07007661 /* GRC_MISC_CFG core clock reset will clear the memory
7662 * enable bit in PCI register 4 and the MSI enable bit
7663 * on some chips, so we save relevant registers here.
7664 */
7665 tg3_save_pci_state(tp);
7666
Michael Chand9ab5ad2006-03-20 22:27:35 -08007667 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752 ||
Joe Perches63c3a662011-04-26 08:12:10 +00007668 tg3_flag(tp, 5755_PLUS))
Michael Chand9ab5ad2006-03-20 22:27:35 -08007669 tw32(GRC_FASTBOOT_PC, 0);
7670
Linus Torvalds1da177e2005-04-16 15:20:36 -07007671 /*
7672 * We must avoid the readl() that normally takes place.
7673 * It locks machines, causes machine checks, and other
7674 * fun things. So, temporarily disable the 5701
7675 * hardware workaround, while we do the reset.
7676 */
Michael Chan1ee582d2005-08-09 20:16:46 -07007677 write_op = tp->write32;
7678 if (write_op == tg3_write_flush_reg32)
7679 tp->write32 = tg3_write32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007680
Michael Chand18edcb2007-03-24 20:57:11 -07007681 /* Prevent the irq handler from reading or writing PCI registers
7682 * during chip reset when the memory enable bit in the PCI command
7683 * register may be cleared. The chip does not generate interrupt
7684 * at this time, but the irq handler may still be called due to irq
7685 * sharing or irqpoll.
7686 */
Joe Perches63c3a662011-04-26 08:12:10 +00007687 tg3_flag_set(tp, CHIP_RESETTING);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007688 for (i = 0; i < tp->irq_cnt; i++) {
7689 struct tg3_napi *tnapi = &tp->napi[i];
7690 if (tnapi->hw_status) {
7691 tnapi->hw_status->status = 0;
7692 tnapi->hw_status->status_tag = 0;
7693 }
7694 tnapi->last_tag = 0;
7695 tnapi->last_irq_tag = 0;
Michael Chanb8fa2f32007-04-06 17:35:37 -07007696 }
Michael Chand18edcb2007-03-24 20:57:11 -07007697 smp_mb();
Matt Carlson4f125f42009-09-01 12:55:02 +00007698
7699 for (i = 0; i < tp->irq_cnt; i++)
7700 synchronize_irq(tp->napi[i].irq_vec);
Michael Chand18edcb2007-03-24 20:57:11 -07007701
Matt Carlson255ca312009-08-25 10:07:27 +00007702 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780) {
7703 val = tr32(TG3_PCIE_LNKCTL) & ~TG3_PCIE_LNKCTL_L1_PLL_PD_EN;
7704 tw32(TG3_PCIE_LNKCTL, val | TG3_PCIE_LNKCTL_L1_PLL_PD_DIS);
7705 }
7706
Linus Torvalds1da177e2005-04-16 15:20:36 -07007707 /* do the reset */
7708 val = GRC_MISC_CFG_CORECLK_RESET;
7709
Joe Perches63c3a662011-04-26 08:12:10 +00007710 if (tg3_flag(tp, PCI_EXPRESS)) {
Matt Carlson88075d92010-08-02 11:25:58 +00007711 /* Force PCIe 1.0a mode */
7712 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
Joe Perches63c3a662011-04-26 08:12:10 +00007713 !tg3_flag(tp, 57765_PLUS) &&
Matt Carlson88075d92010-08-02 11:25:58 +00007714 tr32(TG3_PCIE_PHY_TSTCTL) ==
7715 (TG3_PCIE_PHY_TSTCTL_PCIE10 | TG3_PCIE_PHY_TSTCTL_PSCRAM))
7716 tw32(TG3_PCIE_PHY_TSTCTL, TG3_PCIE_PHY_TSTCTL_PSCRAM);
7717
Linus Torvalds1da177e2005-04-16 15:20:36 -07007718 if (tp->pci_chip_rev_id != CHIPREV_ID_5750_A0) {
7719 tw32(GRC_MISC_CFG, (1 << 29));
7720 val |= (1 << 29);
7721 }
7722 }
7723
Michael Chanb5d37722006-09-27 16:06:21 -07007724 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
7725 tw32(VCPU_STATUS, tr32(VCPU_STATUS) | VCPU_STATUS_DRV_RESET);
7726 tw32(GRC_VCPU_EXT_CTRL,
7727 tr32(GRC_VCPU_EXT_CTRL) & ~GRC_VCPU_EXT_CTRL_HALT_CPU);
7728 }
7729
Matt Carlsonf37500d2010-08-02 11:25:59 +00007730 /* Manage gphy power for all CPMU absent PCIe devices. */
Joe Perches63c3a662011-04-26 08:12:10 +00007731 if (tg3_flag(tp, 5705_PLUS) && !tg3_flag(tp, CPMU_PRESENT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007732 val |= GRC_MISC_CFG_KEEP_GPHY_POWER;
Matt Carlsonf37500d2010-08-02 11:25:59 +00007733
Linus Torvalds1da177e2005-04-16 15:20:36 -07007734 tw32(GRC_MISC_CFG, val);
7735
Michael Chan1ee582d2005-08-09 20:16:46 -07007736 /* restore 5701 hardware bug workaround write method */
7737 tp->write32 = write_op;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007738
7739 /* Unfortunately, we have to delay before the PCI read back.
7740 * Some 575X chips even will not respond to a PCI cfg access
7741 * when the reset command is given to the chip.
7742 *
7743 * How do these hardware designers expect things to work
7744 * properly if the PCI write is posted for a long period
7745 * of time? It is always necessary to have some method by
7746 * which a register read back can occur to push the write
7747 * out which does the reset.
7748 *
7749 * For most tg3 variants the trick below was working.
7750 * Ho hum...
7751 */
7752 udelay(120);
7753
7754 /* Flush PCI posted writes. The normal MMIO registers
7755 * are inaccessible at this time so this is the only
7756 * way to make this reliably (actually, this is no longer
7757 * the case, see above). I tried to use indirect
7758 * register read/write but this upset some 5701 variants.
7759 */
7760 pci_read_config_dword(tp->pdev, PCI_COMMAND, &val);
7761
7762 udelay(120);
7763
Jon Mason708ebb32011-06-27 12:56:50 +00007764 if (tg3_flag(tp, PCI_EXPRESS) && pci_pcie_cap(tp->pdev)) {
Matt Carlsone7126992009-08-25 10:08:16 +00007765 u16 val16;
7766
Linus Torvalds1da177e2005-04-16 15:20:36 -07007767 if (tp->pci_chip_rev_id == CHIPREV_ID_5750_A0) {
7768 int i;
7769 u32 cfg_val;
7770
7771 /* Wait for link training to complete. */
7772 for (i = 0; i < 5000; i++)
7773 udelay(100);
7774
7775 pci_read_config_dword(tp->pdev, 0xc4, &cfg_val);
7776 pci_write_config_dword(tp->pdev, 0xc4,
7777 cfg_val | (1 << 15));
7778 }
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007779
Matt Carlsone7126992009-08-25 10:08:16 +00007780 /* Clear the "no snoop" and "relaxed ordering" bits. */
7781 pci_read_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);
7784 val16 &= ~(PCI_EXP_DEVCTL_RELAX_EN |
7785 PCI_EXP_DEVCTL_NOSNOOP_EN);
7786 /*
7787 * Older PCIe devices only support the 128 byte
7788 * MPS setting. Enforce the restriction.
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007789 */
Joe Perches63c3a662011-04-26 08:12:10 +00007790 if (!tg3_flag(tp, CPMU_PRESENT))
Matt Carlsone7126992009-08-25 10:08:16 +00007791 val16 &= ~PCI_EXP_DEVCTL_PAYLOAD;
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007792 pci_write_config_word(tp->pdev,
Jon Mason708ebb32011-06-27 12:56:50 +00007793 pci_pcie_cap(tp->pdev) + PCI_EXP_DEVCTL,
Matt Carlsone7126992009-08-25 10:08:16 +00007794 val16);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007795
Matt Carlsoncf790032010-11-24 08:31:48 +00007796 pcie_set_readrq(tp->pdev, tp->pcie_readrq);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007797
7798 /* Clear error status */
7799 pci_write_config_word(tp->pdev,
Jon Mason708ebb32011-06-27 12:56:50 +00007800 pci_pcie_cap(tp->pdev) + PCI_EXP_DEVSTA,
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007801 PCI_EXP_DEVSTA_CED |
7802 PCI_EXP_DEVSTA_NFED |
7803 PCI_EXP_DEVSTA_FED |
7804 PCI_EXP_DEVSTA_URD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007805 }
7806
Michael Chanee6a99b2007-07-18 21:49:10 -07007807 tg3_restore_pci_state(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007808
Joe Perches63c3a662011-04-26 08:12:10 +00007809 tg3_flag_clear(tp, CHIP_RESETTING);
7810 tg3_flag_clear(tp, ERROR_PROCESSED);
Michael Chand18edcb2007-03-24 20:57:11 -07007811
Michael Chanee6a99b2007-07-18 21:49:10 -07007812 val = 0;
Joe Perches63c3a662011-04-26 08:12:10 +00007813 if (tg3_flag(tp, 5780_CLASS))
Michael Chan4cf78e42005-07-25 12:29:19 -07007814 val = tr32(MEMARB_MODE);
Michael Chanee6a99b2007-07-18 21:49:10 -07007815 tw32(MEMARB_MODE, val | MEMARB_MODE_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007816
7817 if (tp->pci_chip_rev_id == CHIPREV_ID_5750_A3) {
7818 tg3_stop_fw(tp);
7819 tw32(0x5000, 0x400);
7820 }
7821
7822 tw32(GRC_MODE, tp->grc_mode);
7823
7824 if (tp->pci_chip_rev_id == CHIPREV_ID_5705_A0) {
Andy Gospodarekab0049b2007-09-06 20:42:14 +01007825 val = tr32(0xc4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007826
7827 tw32(0xc4, val | (1 << 15));
7828 }
7829
7830 if ((tp->nic_sram_data_cfg & NIC_SRAM_DATA_CFG_MINI_PCI) != 0 &&
7831 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
7832 tp->pci_clock_ctrl |= CLOCK_CTRL_CLKRUN_OENABLE;
7833 if (tp->pci_chip_rev_id == CHIPREV_ID_5705_A0)
7834 tp->pci_clock_ctrl |= CLOCK_CTRL_FORCE_CLKRUN;
7835 tw32(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl);
7836 }
7837
Matt Carlsonf07e9af2010-08-02 11:26:07 +00007838 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Matt Carlson9e975cc2011-07-20 10:20:50 +00007839 tp->mac_mode = MAC_MODE_PORT_MODE_TBI;
Matt Carlsond2394e6b2010-11-24 08:31:47 +00007840 val = tp->mac_mode;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00007841 } else if (tp->phy_flags & TG3_PHYFLG_MII_SERDES) {
Matt Carlson9e975cc2011-07-20 10:20:50 +00007842 tp->mac_mode = MAC_MODE_PORT_MODE_GMII;
Matt Carlsond2394e6b2010-11-24 08:31:47 +00007843 val = tp->mac_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007844 } else
Matt Carlsond2394e6b2010-11-24 08:31:47 +00007845 val = 0;
7846
7847 tw32_f(MAC_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007848 udelay(40);
7849
Matt Carlson77b483f2008-08-15 14:07:24 -07007850 tg3_ape_unlock(tp, TG3_APE_LOCK_GRC);
7851
Michael Chan7a6f4362006-09-27 16:03:31 -07007852 err = tg3_poll_fw(tp);
7853 if (err)
7854 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007855
Matt Carlson0a9140c2009-08-28 12:27:50 +00007856 tg3_mdio_start(tp);
7857
Joe Perches63c3a662011-04-26 08:12:10 +00007858 if (tg3_flag(tp, PCI_EXPRESS) &&
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00007859 tp->pci_chip_rev_id != CHIPREV_ID_5750_A0 &&
7860 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
Joe Perches63c3a662011-04-26 08:12:10 +00007861 !tg3_flag(tp, 57765_PLUS)) {
Andy Gospodarekab0049b2007-09-06 20:42:14 +01007862 val = tr32(0x7c00);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007863
7864 tw32(0x7c00, val | (1 << 25));
7865 }
7866
Matt Carlsond78b59f2011-04-05 14:22:46 +00007867 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
7868 val = tr32(TG3_CPMU_CLCK_ORIDE);
7869 tw32(TG3_CPMU_CLCK_ORIDE, val & ~CPMU_CLCK_ORIDE_MAC_ORIDE_EN);
7870 }
7871
Linus Torvalds1da177e2005-04-16 15:20:36 -07007872 /* Reprobe ASF enable state. */
Joe Perches63c3a662011-04-26 08:12:10 +00007873 tg3_flag_clear(tp, ENABLE_ASF);
7874 tg3_flag_clear(tp, ASF_NEW_HANDSHAKE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007875 tg3_read_mem(tp, NIC_SRAM_DATA_SIG, &val);
7876 if (val == NIC_SRAM_DATA_SIG_MAGIC) {
7877 u32 nic_cfg;
7878
7879 tg3_read_mem(tp, NIC_SRAM_DATA_CFG, &nic_cfg);
7880 if (nic_cfg & NIC_SRAM_DATA_CFG_ASF_ENABLE) {
Joe Perches63c3a662011-04-26 08:12:10 +00007881 tg3_flag_set(tp, ENABLE_ASF);
Matt Carlson4ba526c2008-08-15 14:10:04 -07007882 tp->last_event_jiffies = jiffies;
Joe Perches63c3a662011-04-26 08:12:10 +00007883 if (tg3_flag(tp, 5750_PLUS))
7884 tg3_flag_set(tp, ASF_NEW_HANDSHAKE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007885 }
7886 }
7887
7888 return 0;
7889}
7890
7891/* tp->lock is held. */
Michael Chan944d9802005-05-29 14:57:48 -07007892static int tg3_halt(struct tg3 *tp, int kind, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007893{
7894 int err;
7895
7896 tg3_stop_fw(tp);
7897
Michael Chan944d9802005-05-29 14:57:48 -07007898 tg3_write_sig_pre_reset(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007899
David S. Millerb3b7d6b2005-05-05 14:40:20 -07007900 tg3_abort_hw(tp, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007901 err = tg3_chip_reset(tp);
7902
Matt Carlsondaba2a62009-04-20 06:58:52 +00007903 __tg3_set_mac_addr(tp, 0);
7904
Michael Chan944d9802005-05-29 14:57:48 -07007905 tg3_write_sig_legacy(tp, kind);
7906 tg3_write_sig_post_reset(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007907
7908 if (err)
7909 return err;
7910
7911 return 0;
7912}
7913
Linus Torvalds1da177e2005-04-16 15:20:36 -07007914static int tg3_set_mac_addr(struct net_device *dev, void *p)
7915{
7916 struct tg3 *tp = netdev_priv(dev);
7917 struct sockaddr *addr = p;
Michael Chan986e0ae2007-05-05 12:10:20 -07007918 int err = 0, skip_mac_1 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007919
Michael Chanf9804dd2005-09-27 12:13:10 -07007920 if (!is_valid_ether_addr(addr->sa_data))
7921 return -EINVAL;
7922
Linus Torvalds1da177e2005-04-16 15:20:36 -07007923 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
7924
Michael Chane75f7c92006-03-20 21:33:26 -08007925 if (!netif_running(dev))
7926 return 0;
7927
Joe Perches63c3a662011-04-26 08:12:10 +00007928 if (tg3_flag(tp, ENABLE_ASF)) {
Michael Chan986e0ae2007-05-05 12:10:20 -07007929 u32 addr0_high, addr0_low, addr1_high, addr1_low;
Michael Chan58712ef2006-04-29 18:58:01 -07007930
Michael Chan986e0ae2007-05-05 12:10:20 -07007931 addr0_high = tr32(MAC_ADDR_0_HIGH);
7932 addr0_low = tr32(MAC_ADDR_0_LOW);
7933 addr1_high = tr32(MAC_ADDR_1_HIGH);
7934 addr1_low = tr32(MAC_ADDR_1_LOW);
7935
7936 /* Skip MAC addr 1 if ASF is using it. */
7937 if ((addr0_high != addr1_high || addr0_low != addr1_low) &&
7938 !(addr1_high == 0 && addr1_low == 0))
7939 skip_mac_1 = 1;
Michael Chan58712ef2006-04-29 18:58:01 -07007940 }
Michael Chan986e0ae2007-05-05 12:10:20 -07007941 spin_lock_bh(&tp->lock);
7942 __tg3_set_mac_addr(tp, skip_mac_1);
7943 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007944
Michael Chanb9ec6c12006-07-25 16:37:27 -07007945 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007946}
7947
7948/* tp->lock is held. */
7949static void tg3_set_bdinfo(struct tg3 *tp, u32 bdinfo_addr,
7950 dma_addr_t mapping, u32 maxlen_flags,
7951 u32 nic_addr)
7952{
7953 tg3_write_mem(tp,
7954 (bdinfo_addr + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH),
7955 ((u64) mapping >> 32));
7956 tg3_write_mem(tp,
7957 (bdinfo_addr + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW),
7958 ((u64) mapping & 0xffffffff));
7959 tg3_write_mem(tp,
7960 (bdinfo_addr + TG3_BDINFO_MAXLEN_FLAGS),
7961 maxlen_flags);
7962
Joe Perches63c3a662011-04-26 08:12:10 +00007963 if (!tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007964 tg3_write_mem(tp,
7965 (bdinfo_addr + TG3_BDINFO_NIC_ADDR),
7966 nic_addr);
7967}
7968
7969static void __tg3_set_rx_mode(struct net_device *);
Michael Chand244c892005-07-05 14:42:33 -07007970static void __tg3_set_coalesce(struct tg3 *tp, struct ethtool_coalesce *ec)
David S. Miller15f98502005-05-18 22:49:26 -07007971{
Matt Carlsonb6080e12009-09-01 13:12:00 +00007972 int i;
7973
Joe Perches63c3a662011-04-26 08:12:10 +00007974 if (!tg3_flag(tp, ENABLE_TSS)) {
Matt Carlsonb6080e12009-09-01 13:12:00 +00007975 tw32(HOSTCC_TXCOL_TICKS, ec->tx_coalesce_usecs);
7976 tw32(HOSTCC_TXMAX_FRAMES, ec->tx_max_coalesced_frames);
7977 tw32(HOSTCC_TXCOAL_MAXF_INT, ec->tx_max_coalesced_frames_irq);
Matt Carlsonb6080e12009-09-01 13:12:00 +00007978 } else {
7979 tw32(HOSTCC_TXCOL_TICKS, 0);
7980 tw32(HOSTCC_TXMAX_FRAMES, 0);
7981 tw32(HOSTCC_TXCOAL_MAXF_INT, 0);
Matt Carlson19cfaec2009-12-03 08:36:20 +00007982 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00007983
Joe Perches63c3a662011-04-26 08:12:10 +00007984 if (!tg3_flag(tp, ENABLE_RSS)) {
Matt Carlson19cfaec2009-12-03 08:36:20 +00007985 tw32(HOSTCC_RXCOL_TICKS, ec->rx_coalesce_usecs);
7986 tw32(HOSTCC_RXMAX_FRAMES, ec->rx_max_coalesced_frames);
7987 tw32(HOSTCC_RXCOAL_MAXF_INT, ec->rx_max_coalesced_frames_irq);
7988 } else {
Matt Carlsonb6080e12009-09-01 13:12:00 +00007989 tw32(HOSTCC_RXCOL_TICKS, 0);
7990 tw32(HOSTCC_RXMAX_FRAMES, 0);
7991 tw32(HOSTCC_RXCOAL_MAXF_INT, 0);
David S. Miller15f98502005-05-18 22:49:26 -07007992 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00007993
Joe Perches63c3a662011-04-26 08:12:10 +00007994 if (!tg3_flag(tp, 5705_PLUS)) {
David S. Miller15f98502005-05-18 22:49:26 -07007995 u32 val = ec->stats_block_coalesce_usecs;
7996
Matt Carlsonb6080e12009-09-01 13:12:00 +00007997 tw32(HOSTCC_RXCOAL_TICK_INT, ec->rx_coalesce_usecs_irq);
7998 tw32(HOSTCC_TXCOAL_TICK_INT, ec->tx_coalesce_usecs_irq);
7999
David S. Miller15f98502005-05-18 22:49:26 -07008000 if (!netif_carrier_ok(tp->dev))
8001 val = 0;
8002
8003 tw32(HOSTCC_STAT_COAL_TICKS, val);
8004 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00008005
8006 for (i = 0; i < tp->irq_cnt - 1; i++) {
8007 u32 reg;
8008
8009 reg = HOSTCC_RXCOL_TICKS_VEC1 + i * 0x18;
8010 tw32(reg, ec->rx_coalesce_usecs);
Matt Carlsonb6080e12009-09-01 13:12:00 +00008011 reg = HOSTCC_RXMAX_FRAMES_VEC1 + i * 0x18;
8012 tw32(reg, ec->rx_max_coalesced_frames);
Matt Carlsonb6080e12009-09-01 13:12:00 +00008013 reg = HOSTCC_RXCOAL_MAXF_INT_VEC1 + i * 0x18;
8014 tw32(reg, ec->rx_max_coalesced_frames_irq);
Matt Carlson19cfaec2009-12-03 08:36:20 +00008015
Joe Perches63c3a662011-04-26 08:12:10 +00008016 if (tg3_flag(tp, ENABLE_TSS)) {
Matt Carlson19cfaec2009-12-03 08:36:20 +00008017 reg = HOSTCC_TXCOL_TICKS_VEC1 + i * 0x18;
8018 tw32(reg, ec->tx_coalesce_usecs);
8019 reg = HOSTCC_TXMAX_FRAMES_VEC1 + i * 0x18;
8020 tw32(reg, ec->tx_max_coalesced_frames);
8021 reg = HOSTCC_TXCOAL_MAXF_INT_VEC1 + i * 0x18;
8022 tw32(reg, ec->tx_max_coalesced_frames_irq);
8023 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00008024 }
8025
8026 for (; i < tp->irq_max - 1; i++) {
8027 tw32(HOSTCC_RXCOL_TICKS_VEC1 + i * 0x18, 0);
Matt Carlsonb6080e12009-09-01 13:12:00 +00008028 tw32(HOSTCC_RXMAX_FRAMES_VEC1 + i * 0x18, 0);
Matt Carlsonb6080e12009-09-01 13:12:00 +00008029 tw32(HOSTCC_RXCOAL_MAXF_INT_VEC1 + i * 0x18, 0);
Matt Carlson19cfaec2009-12-03 08:36:20 +00008030
Joe Perches63c3a662011-04-26 08:12:10 +00008031 if (tg3_flag(tp, ENABLE_TSS)) {
Matt Carlson19cfaec2009-12-03 08:36:20 +00008032 tw32(HOSTCC_TXCOL_TICKS_VEC1 + i * 0x18, 0);
8033 tw32(HOSTCC_TXMAX_FRAMES_VEC1 + i * 0x18, 0);
8034 tw32(HOSTCC_TXCOAL_MAXF_INT_VEC1 + i * 0x18, 0);
8035 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00008036 }
David S. Miller15f98502005-05-18 22:49:26 -07008037}
Linus Torvalds1da177e2005-04-16 15:20:36 -07008038
8039/* tp->lock is held. */
Matt Carlson2d31eca2009-09-01 12:53:31 +00008040static void tg3_rings_reset(struct tg3 *tp)
8041{
8042 int i;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008043 u32 stblk, txrcb, rxrcb, limit;
Matt Carlson2d31eca2009-09-01 12:53:31 +00008044 struct tg3_napi *tnapi = &tp->napi[0];
8045
8046 /* Disable all transmit rings but the first. */
Joe Perches63c3a662011-04-26 08:12:10 +00008047 if (!tg3_flag(tp, 5705_PLUS))
Matt Carlson2d31eca2009-09-01 12:53:31 +00008048 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 16;
Joe Perches63c3a662011-04-26 08:12:10 +00008049 else if (tg3_flag(tp, 5717_PLUS))
Matt Carlson3d377282010-10-14 10:37:39 +00008050 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 4;
Matt Carlsonb703df62009-12-03 08:36:21 +00008051 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
8052 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 2;
Matt Carlson2d31eca2009-09-01 12:53:31 +00008053 else
8054 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE;
8055
8056 for (txrcb = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE;
8057 txrcb < limit; txrcb += TG3_BDINFO_SIZE)
8058 tg3_write_mem(tp, txrcb + TG3_BDINFO_MAXLEN_FLAGS,
8059 BDINFO_FLAGS_DISABLED);
8060
8061
8062 /* Disable all receive return rings but the first. */
Joe Perches63c3a662011-04-26 08:12:10 +00008063 if (tg3_flag(tp, 5717_PLUS))
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00008064 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 17;
Joe Perches63c3a662011-04-26 08:12:10 +00008065 else if (!tg3_flag(tp, 5705_PLUS))
Matt Carlson2d31eca2009-09-01 12:53:31 +00008066 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 16;
Matt Carlsonb703df62009-12-03 08:36:21 +00008067 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
8068 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Matt Carlson2d31eca2009-09-01 12:53:31 +00008069 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 4;
8070 else
8071 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE;
8072
8073 for (rxrcb = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE;
8074 rxrcb < limit; rxrcb += TG3_BDINFO_SIZE)
8075 tg3_write_mem(tp, rxrcb + TG3_BDINFO_MAXLEN_FLAGS,
8076 BDINFO_FLAGS_DISABLED);
8077
8078 /* Disable interrupts */
8079 tw32_mailbox_f(tp->napi[0].int_mbox, 1);
Matt Carlson0e6cf6a2011-06-13 13:38:55 +00008080 tp->napi[0].chk_msi_cnt = 0;
8081 tp->napi[0].last_rx_cons = 0;
8082 tp->napi[0].last_tx_cons = 0;
Matt Carlson2d31eca2009-09-01 12:53:31 +00008083
8084 /* Zero mailbox registers. */
Joe Perches63c3a662011-04-26 08:12:10 +00008085 if (tg3_flag(tp, SUPPORT_MSIX)) {
Matt Carlson6fd45cb2010-09-15 08:59:57 +00008086 for (i = 1; i < tp->irq_max; i++) {
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008087 tp->napi[i].tx_prod = 0;
8088 tp->napi[i].tx_cons = 0;
Joe Perches63c3a662011-04-26 08:12:10 +00008089 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlsonc2353a32010-01-20 16:58:08 +00008090 tw32_mailbox(tp->napi[i].prodmbox, 0);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008091 tw32_rx_mbox(tp->napi[i].consmbox, 0);
8092 tw32_mailbox_f(tp->napi[i].int_mbox, 1);
Matt Carlson7f230732011-08-31 11:44:48 +00008093 tp->napi[i].chk_msi_cnt = 0;
Matt Carlson0e6cf6a2011-06-13 13:38:55 +00008094 tp->napi[i].last_rx_cons = 0;
8095 tp->napi[i].last_tx_cons = 0;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008096 }
Joe Perches63c3a662011-04-26 08:12:10 +00008097 if (!tg3_flag(tp, ENABLE_TSS))
Matt Carlsonc2353a32010-01-20 16:58:08 +00008098 tw32_mailbox(tp->napi[0].prodmbox, 0);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008099 } else {
8100 tp->napi[0].tx_prod = 0;
8101 tp->napi[0].tx_cons = 0;
8102 tw32_mailbox(tp->napi[0].prodmbox, 0);
8103 tw32_rx_mbox(tp->napi[0].consmbox, 0);
8104 }
Matt Carlson2d31eca2009-09-01 12:53:31 +00008105
8106 /* Make sure the NIC-based send BD rings are disabled. */
Joe Perches63c3a662011-04-26 08:12:10 +00008107 if (!tg3_flag(tp, 5705_PLUS)) {
Matt Carlson2d31eca2009-09-01 12:53:31 +00008108 u32 mbox = MAILBOX_SNDNIC_PROD_IDX_0 + TG3_64BIT_REG_LOW;
8109 for (i = 0; i < 16; i++)
8110 tw32_tx_mbox(mbox + i * 8, 0);
8111 }
8112
8113 txrcb = NIC_SRAM_SEND_RCB;
8114 rxrcb = NIC_SRAM_RCV_RET_RCB;
8115
8116 /* Clear status block in ram. */
8117 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
8118
8119 /* Set status block DMA address */
8120 tw32(HOSTCC_STATUS_BLK_HOST_ADDR + TG3_64BIT_REG_HIGH,
8121 ((u64) tnapi->status_mapping >> 32));
8122 tw32(HOSTCC_STATUS_BLK_HOST_ADDR + TG3_64BIT_REG_LOW,
8123 ((u64) tnapi->status_mapping & 0xffffffff));
8124
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008125 if (tnapi->tx_ring) {
8126 tg3_set_bdinfo(tp, txrcb, tnapi->tx_desc_mapping,
8127 (TG3_TX_RING_SIZE <<
8128 BDINFO_FLAGS_MAXLEN_SHIFT),
8129 NIC_SRAM_TX_BUFFER_DESC);
8130 txrcb += TG3_BDINFO_SIZE;
8131 }
Matt Carlson2d31eca2009-09-01 12:53:31 +00008132
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008133 if (tnapi->rx_rcb) {
8134 tg3_set_bdinfo(tp, rxrcb, tnapi->rx_rcb_mapping,
Matt Carlson7cb32cf2010-09-30 10:34:36 +00008135 (tp->rx_ret_ring_mask + 1) <<
8136 BDINFO_FLAGS_MAXLEN_SHIFT, 0);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008137 rxrcb += TG3_BDINFO_SIZE;
8138 }
8139
8140 stblk = HOSTCC_STATBLCK_RING1;
8141
8142 for (i = 1, tnapi++; i < tp->irq_cnt; i++, tnapi++) {
8143 u64 mapping = (u64)tnapi->status_mapping;
8144 tw32(stblk + TG3_64BIT_REG_HIGH, mapping >> 32);
8145 tw32(stblk + TG3_64BIT_REG_LOW, mapping & 0xffffffff);
8146
8147 /* Clear status block in ram. */
8148 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
8149
Matt Carlson19cfaec2009-12-03 08:36:20 +00008150 if (tnapi->tx_ring) {
8151 tg3_set_bdinfo(tp, txrcb, tnapi->tx_desc_mapping,
8152 (TG3_TX_RING_SIZE <<
8153 BDINFO_FLAGS_MAXLEN_SHIFT),
8154 NIC_SRAM_TX_BUFFER_DESC);
8155 txrcb += TG3_BDINFO_SIZE;
8156 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008157
8158 tg3_set_bdinfo(tp, rxrcb, tnapi->rx_rcb_mapping,
Matt Carlson7cb32cf2010-09-30 10:34:36 +00008159 ((tp->rx_ret_ring_mask + 1) <<
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008160 BDINFO_FLAGS_MAXLEN_SHIFT), 0);
8161
8162 stblk += 8;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008163 rxrcb += TG3_BDINFO_SIZE;
8164 }
Matt Carlson2d31eca2009-09-01 12:53:31 +00008165}
8166
Matt Carlsoneb07a942011-04-20 07:57:36 +00008167static void tg3_setup_rxbd_thresholds(struct tg3 *tp)
8168{
8169 u32 val, bdcache_maxcnt, host_rep_thresh, nic_rep_thresh;
8170
Joe Perches63c3a662011-04-26 08:12:10 +00008171 if (!tg3_flag(tp, 5750_PLUS) ||
8172 tg3_flag(tp, 5780_CLASS) ||
Matt Carlsoneb07a942011-04-20 07:57:36 +00008173 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 ||
8174 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752)
8175 bdcache_maxcnt = TG3_SRAM_RX_STD_BDCACHE_SIZE_5700;
8176 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
8177 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787)
8178 bdcache_maxcnt = TG3_SRAM_RX_STD_BDCACHE_SIZE_5755;
8179 else
8180 bdcache_maxcnt = TG3_SRAM_RX_STD_BDCACHE_SIZE_5906;
8181
8182 nic_rep_thresh = min(bdcache_maxcnt / 2, tp->rx_std_max_post);
8183 host_rep_thresh = max_t(u32, tp->rx_pending / 8, 1);
8184
8185 val = min(nic_rep_thresh, host_rep_thresh);
8186 tw32(RCVBDI_STD_THRESH, val);
8187
Joe Perches63c3a662011-04-26 08:12:10 +00008188 if (tg3_flag(tp, 57765_PLUS))
Matt Carlsoneb07a942011-04-20 07:57:36 +00008189 tw32(STD_REPLENISH_LWM, bdcache_maxcnt);
8190
Joe Perches63c3a662011-04-26 08:12:10 +00008191 if (!tg3_flag(tp, JUMBO_CAPABLE) || tg3_flag(tp, 5780_CLASS))
Matt Carlsoneb07a942011-04-20 07:57:36 +00008192 return;
8193
Joe Perches63c3a662011-04-26 08:12:10 +00008194 if (!tg3_flag(tp, 5705_PLUS))
Matt Carlsoneb07a942011-04-20 07:57:36 +00008195 bdcache_maxcnt = TG3_SRAM_RX_JMB_BDCACHE_SIZE_5700;
8196 else
8197 bdcache_maxcnt = TG3_SRAM_RX_JMB_BDCACHE_SIZE_5717;
8198
8199 host_rep_thresh = max_t(u32, tp->rx_jumbo_pending / 8, 1);
8200
8201 val = min(bdcache_maxcnt / 2, host_rep_thresh);
8202 tw32(RCVBDI_JUMBO_THRESH, val);
8203
Joe Perches63c3a662011-04-26 08:12:10 +00008204 if (tg3_flag(tp, 57765_PLUS))
Matt Carlsoneb07a942011-04-20 07:57:36 +00008205 tw32(JMB_REPLENISH_LWM, bdcache_maxcnt);
8206}
8207
Matt Carlson2d31eca2009-09-01 12:53:31 +00008208/* tp->lock is held. */
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07008209static int tg3_reset_hw(struct tg3 *tp, int reset_phy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008210{
8211 u32 val, rdmac_mode;
8212 int i, err, limit;
Matt Carlson8fea32b2010-09-15 08:59:58 +00008213 struct tg3_rx_prodring_set *tpr = &tp->napi[0].prodring;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008214
8215 tg3_disable_ints(tp);
8216
8217 tg3_stop_fw(tp);
8218
8219 tg3_write_sig_pre_reset(tp, RESET_KIND_INIT);
8220
Joe Perches63c3a662011-04-26 08:12:10 +00008221 if (tg3_flag(tp, INIT_COMPLETE))
Michael Chane6de8ad2005-05-05 14:42:41 -07008222 tg3_abort_hw(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008223
Matt Carlson699c0192010-12-06 08:28:51 +00008224 /* Enable MAC control of LPI */
8225 if (tp->phy_flags & TG3_PHYFLG_EEE_CAP) {
8226 tw32_f(TG3_CPMU_EEE_LNKIDL_CTRL,
8227 TG3_CPMU_EEE_LNKIDL_PCIE_NL0 |
8228 TG3_CPMU_EEE_LNKIDL_UART_IDL);
8229
8230 tw32_f(TG3_CPMU_EEE_CTRL,
8231 TG3_CPMU_EEE_CTRL_EXIT_20_1_US);
8232
Matt Carlsona386b902010-12-06 08:28:53 +00008233 val = TG3_CPMU_EEEMD_ERLY_L1_XIT_DET |
8234 TG3_CPMU_EEEMD_LPI_IN_TX |
8235 TG3_CPMU_EEEMD_LPI_IN_RX |
8236 TG3_CPMU_EEEMD_EEE_ENABLE;
8237
8238 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717)
8239 val |= TG3_CPMU_EEEMD_SND_IDX_DET_EN;
8240
Joe Perches63c3a662011-04-26 08:12:10 +00008241 if (tg3_flag(tp, ENABLE_APE))
Matt Carlsona386b902010-12-06 08:28:53 +00008242 val |= TG3_CPMU_EEEMD_APE_TX_DET_EN;
8243
8244 tw32_f(TG3_CPMU_EEE_MODE, val);
8245
8246 tw32_f(TG3_CPMU_EEE_DBTMR1,
8247 TG3_CPMU_DBTMR1_PCIEXIT_2047US |
8248 TG3_CPMU_DBTMR1_LNKIDLE_2047US);
8249
8250 tw32_f(TG3_CPMU_EEE_DBTMR2,
Matt Carlsond7f2ab22011-01-25 15:58:56 +00008251 TG3_CPMU_DBTMR2_APE_TX_2047US |
Matt Carlsona386b902010-12-06 08:28:53 +00008252 TG3_CPMU_DBTMR2_TXIDXEQ_2047US);
Matt Carlson699c0192010-12-06 08:28:51 +00008253 }
8254
Matt Carlson603f1172010-02-12 14:47:10 +00008255 if (reset_phy)
Michael Chand4d2c552006-03-20 17:47:20 -08008256 tg3_phy_reset(tp);
8257
Linus Torvalds1da177e2005-04-16 15:20:36 -07008258 err = tg3_chip_reset(tp);
8259 if (err)
8260 return err;
8261
8262 tg3_write_sig_legacy(tp, RESET_KIND_INIT);
8263
Matt Carlsonbcb37f62008-11-03 16:52:09 -08008264 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX) {
Matt Carlsond30cdd22007-10-07 23:28:35 -07008265 val = tr32(TG3_CPMU_CTRL);
8266 val &= ~(CPMU_CTRL_LINK_AWARE_MODE | CPMU_CTRL_LINK_IDLE_MODE);
8267 tw32(TG3_CPMU_CTRL, val);
Matt Carlson9acb9612007-11-12 21:10:06 -08008268
8269 val = tr32(TG3_CPMU_LSPD_10MB_CLK);
8270 val &= ~CPMU_LSPD_10MB_MACCLK_MASK;
8271 val |= CPMU_LSPD_10MB_MACCLK_6_25;
8272 tw32(TG3_CPMU_LSPD_10MB_CLK, val);
8273
8274 val = tr32(TG3_CPMU_LNK_AWARE_PWRMD);
8275 val &= ~CPMU_LNK_AWARE_MACCLK_MASK;
8276 val |= CPMU_LNK_AWARE_MACCLK_6_25;
8277 tw32(TG3_CPMU_LNK_AWARE_PWRMD, val);
8278
8279 val = tr32(TG3_CPMU_HST_ACC);
8280 val &= ~CPMU_HST_ACC_MACCLK_MASK;
8281 val |= CPMU_HST_ACC_MACCLK_6_25;
8282 tw32(TG3_CPMU_HST_ACC, val);
Matt Carlsond30cdd22007-10-07 23:28:35 -07008283 }
8284
Matt Carlson33466d92009-04-20 06:57:41 +00008285 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780) {
8286 val = tr32(PCIE_PWR_MGMT_THRESH) & ~PCIE_PWR_MGMT_L1_THRESH_MSK;
8287 val |= PCIE_PWR_MGMT_EXT_ASPM_TMR_EN |
8288 PCIE_PWR_MGMT_L1_THRESH_4MS;
8289 tw32(PCIE_PWR_MGMT_THRESH, val);
Matt Carlson521e6b92009-08-25 10:06:01 +00008290
8291 val = tr32(TG3_PCIE_EIDLE_DELAY) & ~TG3_PCIE_EIDLE_DELAY_MASK;
8292 tw32(TG3_PCIE_EIDLE_DELAY, val | TG3_PCIE_EIDLE_DELAY_13_CLKS);
8293
8294 tw32(TG3_CORR_ERR_STAT, TG3_CORR_ERR_STAT_CLEAR);
Matt Carlson33466d92009-04-20 06:57:41 +00008295
Matt Carlsonf40386c2009-11-02 14:24:02 +00008296 val = tr32(TG3_PCIE_LNKCTL) & ~TG3_PCIE_LNKCTL_L1_PLL_PD_EN;
8297 tw32(TG3_PCIE_LNKCTL, val | TG3_PCIE_LNKCTL_L1_PLL_PD_DIS);
Matt Carlson255ca312009-08-25 10:07:27 +00008298 }
8299
Joe Perches63c3a662011-04-26 08:12:10 +00008300 if (tg3_flag(tp, L1PLLPD_EN)) {
Matt Carlson614b05902010-01-20 16:58:02 +00008301 u32 grc_mode = tr32(GRC_MODE);
8302
8303 /* Access the lower 1K of PL PCIE block registers. */
8304 val = grc_mode & ~GRC_MODE_PCIE_PORT_MASK;
8305 tw32(GRC_MODE, val | GRC_MODE_PCIE_PL_SEL);
8306
8307 val = tr32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL1);
8308 tw32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL1,
8309 val | TG3_PCIE_PL_LO_PHYCTL1_L1PLLPD_EN);
8310
8311 tw32(GRC_MODE, grc_mode);
8312 }
8313
Matt Carlson5093eed2010-11-24 08:31:45 +00008314 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765) {
8315 if (tp->pci_chip_rev_id == CHIPREV_ID_57765_A0) {
8316 u32 grc_mode = tr32(GRC_MODE);
Matt Carlsoncea46462010-04-12 06:58:24 +00008317
Matt Carlson5093eed2010-11-24 08:31:45 +00008318 /* Access the lower 1K of PL PCIE block registers. */
8319 val = grc_mode & ~GRC_MODE_PCIE_PORT_MASK;
8320 tw32(GRC_MODE, val | GRC_MODE_PCIE_PL_SEL);
Matt Carlsoncea46462010-04-12 06:58:24 +00008321
Matt Carlson5093eed2010-11-24 08:31:45 +00008322 val = tr32(TG3_PCIE_TLDLPL_PORT +
8323 TG3_PCIE_PL_LO_PHYCTL5);
8324 tw32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL5,
8325 val | TG3_PCIE_PL_LO_PHYCTL5_DIS_L2CLKREQ);
Matt Carlsoncea46462010-04-12 06:58:24 +00008326
Matt Carlson5093eed2010-11-24 08:31:45 +00008327 tw32(GRC_MODE, grc_mode);
8328 }
Matt Carlsona977dbe2010-04-12 06:58:26 +00008329
Matt Carlson1ff30a52011-05-19 12:12:46 +00008330 if (GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_57765_AX) {
8331 u32 grc_mode = tr32(GRC_MODE);
8332
8333 /* Access the lower 1K of DL PCIE block registers. */
8334 val = grc_mode & ~GRC_MODE_PCIE_PORT_MASK;
8335 tw32(GRC_MODE, val | GRC_MODE_PCIE_DL_SEL);
8336
8337 val = tr32(TG3_PCIE_TLDLPL_PORT +
8338 TG3_PCIE_DL_LO_FTSMAX);
8339 val &= ~TG3_PCIE_DL_LO_FTSMAX_MSK;
8340 tw32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_DL_LO_FTSMAX,
8341 val | TG3_PCIE_DL_LO_FTSMAX_VAL);
8342
8343 tw32(GRC_MODE, grc_mode);
8344 }
8345
Matt Carlsona977dbe2010-04-12 06:58:26 +00008346 val = tr32(TG3_CPMU_LSPD_10MB_CLK);
8347 val &= ~CPMU_LSPD_10MB_MACCLK_MASK;
8348 val |= CPMU_LSPD_10MB_MACCLK_6_25;
8349 tw32(TG3_CPMU_LSPD_10MB_CLK, val);
Matt Carlsoncea46462010-04-12 06:58:24 +00008350 }
8351
Linus Torvalds1da177e2005-04-16 15:20:36 -07008352 /* This works around an issue with Athlon chipsets on
8353 * B3 tigon3 silicon. This bit has no effect on any
8354 * other revision. But do not set this on PCI Express
Matt Carlson795d01c2007-10-07 23:28:17 -07008355 * chips and don't even touch the clocks if the CPMU is present.
Linus Torvalds1da177e2005-04-16 15:20:36 -07008356 */
Joe Perches63c3a662011-04-26 08:12:10 +00008357 if (!tg3_flag(tp, CPMU_PRESENT)) {
8358 if (!tg3_flag(tp, PCI_EXPRESS))
Matt Carlson795d01c2007-10-07 23:28:17 -07008359 tp->pci_clock_ctrl |= CLOCK_CTRL_DELAY_PCI_GRANT;
8360 tw32_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl);
8361 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008362
8363 if (tp->pci_chip_rev_id == CHIPREV_ID_5704_A0 &&
Joe Perches63c3a662011-04-26 08:12:10 +00008364 tg3_flag(tp, PCIX_MODE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008365 val = tr32(TG3PCI_PCISTATE);
8366 val |= PCISTATE_RETRY_SAME_DMA;
8367 tw32(TG3PCI_PCISTATE, val);
8368 }
8369
Joe Perches63c3a662011-04-26 08:12:10 +00008370 if (tg3_flag(tp, ENABLE_APE)) {
Matt Carlson0d3031d2007-10-10 18:02:43 -07008371 /* Allow reads and writes to the
8372 * APE register and memory space.
8373 */
8374 val = tr32(TG3PCI_PCISTATE);
8375 val |= PCISTATE_ALLOW_APE_CTLSPC_WR |
Matt Carlsonf92d9dc2010-06-05 17:24:30 +00008376 PCISTATE_ALLOW_APE_SHMEM_WR |
8377 PCISTATE_ALLOW_APE_PSPACE_WR;
Matt Carlson0d3031d2007-10-10 18:02:43 -07008378 tw32(TG3PCI_PCISTATE, val);
8379 }
8380
Linus Torvalds1da177e2005-04-16 15:20:36 -07008381 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5704_BX) {
8382 /* Enable some hw fixes. */
8383 val = tr32(TG3PCI_MSI_DATA);
8384 val |= (1 << 26) | (1 << 28) | (1 << 29);
8385 tw32(TG3PCI_MSI_DATA, val);
8386 }
8387
8388 /* Descriptor ring init may make accesses to the
8389 * NIC SRAM area to setup the TX descriptors, so we
8390 * can only do this after the hardware has been
8391 * successfully reset.
8392 */
Michael Chan32d8c572006-07-25 16:38:29 -07008393 err = tg3_init_rings(tp);
8394 if (err)
8395 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008396
Joe Perches63c3a662011-04-26 08:12:10 +00008397 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlsoncbf9ca62009-11-13 13:03:40 +00008398 val = tr32(TG3PCI_DMA_RW_CTRL) &
8399 ~DMA_RWCTRL_DIS_CACHE_ALIGNMENT;
Matt Carlson1a319022010-04-12 06:58:25 +00008400 if (tp->pci_chip_rev_id == CHIPREV_ID_57765_A0)
8401 val &= ~DMA_RWCTRL_CRDRDR_RDMA_MRRS_MSK;
Matt Carlson0aebff42011-04-25 12:42:45 +00008402 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_57765 &&
8403 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717)
8404 val |= DMA_RWCTRL_TAGGED_STAT_WA;
Matt Carlsoncbf9ca62009-11-13 13:03:40 +00008405 tw32(TG3PCI_DMA_RW_CTRL, val | tp->dma_rwctrl);
8406 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5784 &&
8407 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5761) {
Matt Carlsond30cdd22007-10-07 23:28:35 -07008408 /* This value is determined during the probe time DMA
8409 * engine test, tg3_test_dma.
8410 */
8411 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
8412 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008413
8414 tp->grc_mode &= ~(GRC_MODE_HOST_SENDBDS |
8415 GRC_MODE_4X_NIC_SEND_RINGS |
8416 GRC_MODE_NO_TX_PHDR_CSUM |
8417 GRC_MODE_NO_RX_PHDR_CSUM);
8418 tp->grc_mode |= GRC_MODE_HOST_SENDBDS;
Michael Chand2d746f2006-04-06 21:45:39 -07008419
8420 /* Pseudo-header checksum is done by hardware logic and not
8421 * the offload processers, so make the chip do the pseudo-
8422 * header checksums on receive. For transmit it is more
8423 * convenient to do the pseudo-header checksum in software
8424 * as Linux does that on transmit for us in all cases.
8425 */
8426 tp->grc_mode |= GRC_MODE_NO_TX_PHDR_CSUM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008427
8428 tw32(GRC_MODE,
8429 tp->grc_mode |
8430 (GRC_MODE_IRQ_ON_MAC_ATTN | GRC_MODE_HOST_STACKUP));
8431
8432 /* Setup the timer prescalar register. Clock is always 66Mhz. */
8433 val = tr32(GRC_MISC_CFG);
8434 val &= ~0xff;
8435 val |= (65 << GRC_MISC_CFG_PRESCALAR_SHIFT);
8436 tw32(GRC_MISC_CFG, val);
8437
8438 /* Initialize MBUF/DESC pool. */
Joe Perches63c3a662011-04-26 08:12:10 +00008439 if (tg3_flag(tp, 5750_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008440 /* Do nothing. */
8441 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5705) {
8442 tw32(BUFMGR_MB_POOL_ADDR, NIC_SRAM_MBUF_POOL_BASE);
8443 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704)
8444 tw32(BUFMGR_MB_POOL_SIZE, NIC_SRAM_MBUF_POOL_SIZE64);
8445 else
8446 tw32(BUFMGR_MB_POOL_SIZE, NIC_SRAM_MBUF_POOL_SIZE96);
8447 tw32(BUFMGR_DMA_DESC_POOL_ADDR, NIC_SRAM_DMA_DESC_POOL_BASE);
8448 tw32(BUFMGR_DMA_DESC_POOL_SIZE, NIC_SRAM_DMA_DESC_POOL_SIZE);
Joe Perches63c3a662011-04-26 08:12:10 +00008449 } else if (tg3_flag(tp, TSO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008450 int fw_len;
8451
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08008452 fw_len = tp->fw_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008453 fw_len = (fw_len + (0x80 - 1)) & ~(0x80 - 1);
8454 tw32(BUFMGR_MB_POOL_ADDR,
8455 NIC_SRAM_MBUF_POOL_BASE5705 + fw_len);
8456 tw32(BUFMGR_MB_POOL_SIZE,
8457 NIC_SRAM_MBUF_POOL_SIZE5705 - fw_len - 0xa00);
8458 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008459
Michael Chan0f893dc2005-07-25 12:30:38 -07008460 if (tp->dev->mtu <= ETH_DATA_LEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008461 tw32(BUFMGR_MB_RDMA_LOW_WATER,
8462 tp->bufmgr_config.mbuf_read_dma_low_water);
8463 tw32(BUFMGR_MB_MACRX_LOW_WATER,
8464 tp->bufmgr_config.mbuf_mac_rx_low_water);
8465 tw32(BUFMGR_MB_HIGH_WATER,
8466 tp->bufmgr_config.mbuf_high_water);
8467 } else {
8468 tw32(BUFMGR_MB_RDMA_LOW_WATER,
8469 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo);
8470 tw32(BUFMGR_MB_MACRX_LOW_WATER,
8471 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo);
8472 tw32(BUFMGR_MB_HIGH_WATER,
8473 tp->bufmgr_config.mbuf_high_water_jumbo);
8474 }
8475 tw32(BUFMGR_DMA_LOW_WATER,
8476 tp->bufmgr_config.dma_low_water);
8477 tw32(BUFMGR_DMA_HIGH_WATER,
8478 tp->bufmgr_config.dma_high_water);
8479
Matt Carlsond309a462010-09-30 10:34:31 +00008480 val = BUFMGR_MODE_ENABLE | BUFMGR_MODE_ATTN_ENABLE;
8481 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
8482 val |= BUFMGR_MODE_NO_TX_UNDERRUN;
Matt Carlson4d958472011-04-20 07:57:35 +00008483 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
8484 tp->pci_chip_rev_id == CHIPREV_ID_5719_A0 ||
8485 tp->pci_chip_rev_id == CHIPREV_ID_5720_A0)
8486 val |= BUFMGR_MODE_MBLOW_ATTN_ENAB;
Matt Carlsond309a462010-09-30 10:34:31 +00008487 tw32(BUFMGR_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008488 for (i = 0; i < 2000; i++) {
8489 if (tr32(BUFMGR_MODE) & BUFMGR_MODE_ENABLE)
8490 break;
8491 udelay(10);
8492 }
8493 if (i >= 2000) {
Joe Perches05dbe002010-02-17 19:44:19 +00008494 netdev_err(tp->dev, "%s cannot enable BUFMGR\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008495 return -ENODEV;
8496 }
8497
Matt Carlsoneb07a942011-04-20 07:57:36 +00008498 if (tp->pci_chip_rev_id == CHIPREV_ID_5906_A1)
8499 tw32(ISO_PKT_TX, (tr32(ISO_PKT_TX) & ~0x3) | 0x2);
Michael Chanb5d37722006-09-27 16:06:21 -07008500
Matt Carlsoneb07a942011-04-20 07:57:36 +00008501 tg3_setup_rxbd_thresholds(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008502
8503 /* Initialize TG3_BDINFO's at:
8504 * RCVDBDI_STD_BD: standard eth size rx ring
8505 * RCVDBDI_JUMBO_BD: jumbo frame rx ring
8506 * RCVDBDI_MINI_BD: small frame rx ring (??? does not work)
8507 *
8508 * like so:
8509 * TG3_BDINFO_HOST_ADDR: high/low parts of DMA address of ring
8510 * TG3_BDINFO_MAXLEN_FLAGS: (rx max buffer size << 16) |
8511 * ring attribute flags
8512 * TG3_BDINFO_NIC_ADDR: location of descriptors in nic SRAM
8513 *
8514 * Standard receive ring @ NIC_SRAM_RX_BUFFER_DESC, 512 entries.
8515 * Jumbo receive ring @ NIC_SRAM_RX_JUMBO_BUFFER_DESC, 256 entries.
8516 *
8517 * The size of each ring is fixed in the firmware, but the location is
8518 * configurable.
8519 */
8520 tw32(RCVDBDI_STD_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH,
Matt Carlson21f581a2009-08-28 14:00:25 +00008521 ((u64) tpr->rx_std_mapping >> 32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07008522 tw32(RCVDBDI_STD_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW,
Matt Carlson21f581a2009-08-28 14:00:25 +00008523 ((u64) tpr->rx_std_mapping & 0xffffffff));
Joe Perches63c3a662011-04-26 08:12:10 +00008524 if (!tg3_flag(tp, 5717_PLUS))
Matt Carlson87668d32009-11-13 13:03:34 +00008525 tw32(RCVDBDI_STD_BD + TG3_BDINFO_NIC_ADDR,
8526 NIC_SRAM_RX_BUFFER_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008527
Matt Carlsonfdb72b32009-08-28 13:57:12 +00008528 /* Disable the mini ring */
Joe Perches63c3a662011-04-26 08:12:10 +00008529 if (!tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008530 tw32(RCVDBDI_MINI_BD + TG3_BDINFO_MAXLEN_FLAGS,
8531 BDINFO_FLAGS_DISABLED);
8532
Matt Carlsonfdb72b32009-08-28 13:57:12 +00008533 /* Program the jumbo buffer descriptor ring control
8534 * blocks on those devices that have them.
8535 */
Matt Carlsona0512942011-07-27 14:20:54 +00008536 if (tp->pci_chip_rev_id == CHIPREV_ID_5719_A0 ||
Joe Perches63c3a662011-04-26 08:12:10 +00008537 (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008538
Joe Perches63c3a662011-04-26 08:12:10 +00008539 if (tg3_flag(tp, JUMBO_RING_ENABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008540 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH,
Matt Carlson21f581a2009-08-28 14:00:25 +00008541 ((u64) tpr->rx_jmb_mapping >> 32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07008542 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW,
Matt Carlson21f581a2009-08-28 14:00:25 +00008543 ((u64) tpr->rx_jmb_mapping & 0xffffffff));
Matt Carlsonde9f5232011-04-05 14:22:43 +00008544 val = TG3_RX_JMB_RING_SIZE(tp) <<
8545 BDINFO_FLAGS_MAXLEN_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008546 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_MAXLEN_FLAGS,
Matt Carlsonde9f5232011-04-05 14:22:43 +00008547 val | BDINFO_FLAGS_USE_EXT_RECV);
Joe Perches63c3a662011-04-26 08:12:10 +00008548 if (!tg3_flag(tp, USE_JUMBO_BDFLAG) ||
Matt Carlsona50d0792010-06-05 17:24:37 +00008549 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Matt Carlson87668d32009-11-13 13:03:34 +00008550 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_NIC_ADDR,
8551 NIC_SRAM_RX_JUMBO_BUFFER_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008552 } else {
8553 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_MAXLEN_FLAGS,
8554 BDINFO_FLAGS_DISABLED);
8555 }
8556
Joe Perches63c3a662011-04-26 08:12:10 +00008557 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlson7cb32cf2010-09-30 10:34:36 +00008558 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Matt Carlsonde9f5232011-04-05 14:22:43 +00008559 val = TG3_RX_STD_MAX_SIZE_5700;
Matt Carlson7cb32cf2010-09-30 10:34:36 +00008560 else
Matt Carlsonde9f5232011-04-05 14:22:43 +00008561 val = TG3_RX_STD_MAX_SIZE_5717;
Matt Carlson7cb32cf2010-09-30 10:34:36 +00008562 val <<= BDINFO_FLAGS_MAXLEN_SHIFT;
8563 val |= (TG3_RX_STD_DMA_SZ << 2);
8564 } else
Matt Carlson04380d42010-04-12 06:58:29 +00008565 val = TG3_RX_STD_DMA_SZ << BDINFO_FLAGS_MAXLEN_SHIFT;
Matt Carlsonfdb72b32009-08-28 13:57:12 +00008566 } else
Matt Carlsonde9f5232011-04-05 14:22:43 +00008567 val = TG3_RX_STD_MAX_SIZE_5700 << BDINFO_FLAGS_MAXLEN_SHIFT;
Matt Carlsonfdb72b32009-08-28 13:57:12 +00008568
8569 tw32(RCVDBDI_STD_BD + TG3_BDINFO_MAXLEN_FLAGS, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008570
Matt Carlson411da642009-11-13 13:03:46 +00008571 tpr->rx_std_prod_idx = tp->rx_pending;
Matt Carlson66711e62009-11-13 13:03:49 +00008572 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG, tpr->rx_std_prod_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008573
Joe Perches63c3a662011-04-26 08:12:10 +00008574 tpr->rx_jmb_prod_idx =
8575 tg3_flag(tp, JUMBO_RING_ENABLE) ? tp->rx_jumbo_pending : 0;
Matt Carlson66711e62009-11-13 13:03:49 +00008576 tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG, tpr->rx_jmb_prod_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008577
Matt Carlson2d31eca2009-09-01 12:53:31 +00008578 tg3_rings_reset(tp);
8579
Linus Torvalds1da177e2005-04-16 15:20:36 -07008580 /* Initialize MAC address and backoff seed. */
Michael Chan986e0ae2007-05-05 12:10:20 -07008581 __tg3_set_mac_addr(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008582
8583 /* MTU + ethernet header + FCS + optional VLAN tag */
Matt Carlsonf7b493e2009-02-25 14:21:52 +00008584 tw32(MAC_RX_MTU_SIZE,
8585 tp->dev->mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008586
8587 /* The slot time is changed by tg3_setup_phy if we
8588 * run at gigabit with half duplex.
8589 */
Matt Carlsonf2096f92011-04-05 14:22:48 +00008590 val = (2 << TX_LENGTHS_IPG_CRS_SHIFT) |
8591 (6 << TX_LENGTHS_IPG_SHIFT) |
8592 (32 << TX_LENGTHS_SLOT_TIME_SHIFT);
8593
8594 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
8595 val |= tr32(MAC_TX_LENGTHS) &
8596 (TX_LENGTHS_JMB_FRM_LEN_MSK |
8597 TX_LENGTHS_CNT_DWN_VAL_MSK);
8598
8599 tw32(MAC_TX_LENGTHS, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008600
8601 /* Receive rules. */
8602 tw32(MAC_RCV_RULE_CFG, RCV_RULE_CFG_DEFAULT_CLASS);
8603 tw32(RCVLPC_CONFIG, 0x0181);
8604
8605 /* Calculate RDMAC_MODE setting early, we need it to determine
8606 * the RCVLPC_STATE_ENABLE mask.
8607 */
8608 rdmac_mode = (RDMAC_MODE_ENABLE | RDMAC_MODE_TGTABORT_ENAB |
8609 RDMAC_MODE_MSTABORT_ENAB | RDMAC_MODE_PARITYERR_ENAB |
8610 RDMAC_MODE_ADDROFLOW_ENAB | RDMAC_MODE_FIFOOFLOW_ENAB |
8611 RDMAC_MODE_FIFOURUN_ENAB | RDMAC_MODE_FIFOOREAD_ENAB |
8612 RDMAC_MODE_LNGREAD_ENAB);
Michael Chan85e94ce2005-04-21 17:05:28 -07008613
Matt Carlsondeabaac2010-11-24 08:31:50 +00008614 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717)
Matt Carlson0339e4e2010-02-12 14:47:09 +00008615 rdmac_mode |= RDMAC_MODE_MULT_DMA_RD_DIS;
8616
Matt Carlson57e69832008-05-25 23:48:31 -07008617 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson321d32a2008-11-21 17:22:19 -08008618 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
8619 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
Matt Carlsond30cdd22007-10-07 23:28:35 -07008620 rdmac_mode |= RDMAC_MODE_BD_SBD_CRPT_ENAB |
8621 RDMAC_MODE_MBUF_RBD_CRPT_ENAB |
8622 RDMAC_MODE_MBUF_SBD_CRPT_ENAB;
8623
Matt Carlsonc5908932011-03-09 16:58:25 +00008624 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
8625 tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) {
Joe Perches63c3a662011-04-26 08:12:10 +00008626 if (tg3_flag(tp, TSO_CAPABLE) &&
Matt Carlsonc13e3712007-05-05 11:50:04 -07008627 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008628 rdmac_mode |= RDMAC_MODE_FIFO_SIZE_128;
8629 } else if (!(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH) &&
Joe Perches63c3a662011-04-26 08:12:10 +00008630 !tg3_flag(tp, IS_5788)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008631 rdmac_mode |= RDMAC_MODE_FIFO_LONG_BURST;
8632 }
8633 }
8634
Joe Perches63c3a662011-04-26 08:12:10 +00008635 if (tg3_flag(tp, PCI_EXPRESS))
Michael Chan85e94ce2005-04-21 17:05:28 -07008636 rdmac_mode |= RDMAC_MODE_FIFO_LONG_BURST;
8637
Joe Perches63c3a662011-04-26 08:12:10 +00008638 if (tg3_flag(tp, HW_TSO_1) ||
8639 tg3_flag(tp, HW_TSO_2) ||
8640 tg3_flag(tp, HW_TSO_3))
Matt Carlson027455a2008-12-21 20:19:30 -08008641 rdmac_mode |= RDMAC_MODE_IPV4_LSO_EN;
8642
Matt Carlson108a6c12011-05-19 12:12:47 +00008643 if (tg3_flag(tp, 57765_PLUS) ||
Matt Carlsone849cdc2009-11-13 13:03:38 +00008644 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Matt Carlson027455a2008-12-21 20:19:30 -08008645 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
8646 rdmac_mode |= RDMAC_MODE_IPV6_LSO_EN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008647
Matt Carlsonf2096f92011-04-05 14:22:48 +00008648 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
8649 rdmac_mode |= tr32(RDMAC_MODE) & RDMAC_MODE_H2BNC_VLAN_DET;
8650
Matt Carlson41a8a7e2010-09-15 08:59:53 +00008651 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
8652 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
8653 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
8654 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
Joe Perches63c3a662011-04-26 08:12:10 +00008655 tg3_flag(tp, 57765_PLUS)) {
Matt Carlson41a8a7e2010-09-15 08:59:53 +00008656 val = tr32(TG3_RDMA_RSRVCTRL_REG);
Matt Carlsond78b59f2011-04-05 14:22:46 +00008657 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
8658 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
Matt Carlsonb4495ed2011-01-25 15:58:47 +00008659 val &= ~(TG3_RDMA_RSRVCTRL_TXMRGN_MASK |
8660 TG3_RDMA_RSRVCTRL_FIFO_LWM_MASK |
8661 TG3_RDMA_RSRVCTRL_FIFO_HWM_MASK);
8662 val |= TG3_RDMA_RSRVCTRL_TXMRGN_320B |
8663 TG3_RDMA_RSRVCTRL_FIFO_LWM_1_5K |
8664 TG3_RDMA_RSRVCTRL_FIFO_HWM_1_5K;
Matt Carlsonb75cc0e2010-11-24 08:31:46 +00008665 }
Matt Carlson41a8a7e2010-09-15 08:59:53 +00008666 tw32(TG3_RDMA_RSRVCTRL_REG,
8667 val | TG3_RDMA_RSRVCTRL_FIFO_OFLW_FIX);
8668 }
8669
Matt Carlsond78b59f2011-04-05 14:22:46 +00008670 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
8671 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
Matt Carlsond309a462010-09-30 10:34:31 +00008672 val = tr32(TG3_LSO_RD_DMA_CRPTEN_CTRL);
8673 tw32(TG3_LSO_RD_DMA_CRPTEN_CTRL, val |
8674 TG3_LSO_RD_DMA_CRPTEN_CTRL_BLEN_BD_4K |
8675 TG3_LSO_RD_DMA_CRPTEN_CTRL_BLEN_LSO_4K);
8676 }
8677
Linus Torvalds1da177e2005-04-16 15:20:36 -07008678 /* Receive/send statistics. */
Joe Perches63c3a662011-04-26 08:12:10 +00008679 if (tg3_flag(tp, 5750_PLUS)) {
Michael Chan16613942006-06-29 20:15:13 -07008680 val = tr32(RCVLPC_STATS_ENABLE);
8681 val &= ~RCVLPC_STATSENAB_DACK_FIX;
8682 tw32(RCVLPC_STATS_ENABLE, val);
8683 } else if ((rdmac_mode & RDMAC_MODE_FIFO_SIZE_128) &&
Joe Perches63c3a662011-04-26 08:12:10 +00008684 tg3_flag(tp, TSO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008685 val = tr32(RCVLPC_STATS_ENABLE);
8686 val &= ~RCVLPC_STATSENAB_LNGBRST_RFIX;
8687 tw32(RCVLPC_STATS_ENABLE, val);
8688 } else {
8689 tw32(RCVLPC_STATS_ENABLE, 0xffffff);
8690 }
8691 tw32(RCVLPC_STATSCTRL, RCVLPC_STATSCTRL_ENABLE);
8692 tw32(SNDDATAI_STATSENAB, 0xffffff);
8693 tw32(SNDDATAI_STATSCTRL,
8694 (SNDDATAI_SCTRL_ENABLE |
8695 SNDDATAI_SCTRL_FASTUPD));
8696
8697 /* Setup host coalescing engine. */
8698 tw32(HOSTCC_MODE, 0);
8699 for (i = 0; i < 2000; i++) {
8700 if (!(tr32(HOSTCC_MODE) & HOSTCC_MODE_ENABLE))
8701 break;
8702 udelay(10);
8703 }
8704
Michael Chand244c892005-07-05 14:42:33 -07008705 __tg3_set_coalesce(tp, &tp->coal);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008706
Joe Perches63c3a662011-04-26 08:12:10 +00008707 if (!tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008708 /* Status/statistics block address. See tg3_timer,
8709 * the tg3_periodic_fetch_stats call there, and
8710 * tg3_get_stats to see how this works for 5705/5750 chips.
8711 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008712 tw32(HOSTCC_STATS_BLK_HOST_ADDR + TG3_64BIT_REG_HIGH,
8713 ((u64) tp->stats_mapping >> 32));
8714 tw32(HOSTCC_STATS_BLK_HOST_ADDR + TG3_64BIT_REG_LOW,
8715 ((u64) tp->stats_mapping & 0xffffffff));
8716 tw32(HOSTCC_STATS_BLK_NIC_ADDR, NIC_SRAM_STATS_BLK);
Matt Carlson2d31eca2009-09-01 12:53:31 +00008717
Linus Torvalds1da177e2005-04-16 15:20:36 -07008718 tw32(HOSTCC_STATUS_BLK_NIC_ADDR, NIC_SRAM_STATUS_BLK);
Matt Carlson2d31eca2009-09-01 12:53:31 +00008719
8720 /* Clear statistics and status block memory areas */
8721 for (i = NIC_SRAM_STATS_BLK;
8722 i < NIC_SRAM_STATUS_BLK + TG3_HW_STATUS_SIZE;
8723 i += sizeof(u32)) {
8724 tg3_write_mem(tp, i, 0);
8725 udelay(40);
8726 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008727 }
8728
8729 tw32(HOSTCC_MODE, HOSTCC_MODE_ENABLE | tp->coalesce_mode);
8730
8731 tw32(RCVCC_MODE, RCVCC_MODE_ENABLE | RCVCC_MODE_ATTN_ENABLE);
8732 tw32(RCVLPC_MODE, RCVLPC_MODE_ENABLE);
Joe Perches63c3a662011-04-26 08:12:10 +00008733 if (!tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008734 tw32(RCVLSC_MODE, RCVLSC_MODE_ENABLE | RCVLSC_MODE_ATTN_ENABLE);
8735
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008736 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES) {
8737 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chanc94e3942005-09-27 12:12:42 -07008738 /* reset to prevent losing 1st rx packet intermittently */
8739 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
8740 udelay(10);
8741 }
8742
Matt Carlson3bda1252008-08-15 14:08:22 -07008743 tp->mac_mode |= MAC_MODE_TXSTAT_ENABLE | MAC_MODE_RXSTAT_ENABLE |
Matt Carlson9e975cc2011-07-20 10:20:50 +00008744 MAC_MODE_TDE_ENABLE | MAC_MODE_RDE_ENABLE |
8745 MAC_MODE_FHDE_ENABLE;
8746 if (tg3_flag(tp, ENABLE_APE))
8747 tp->mac_mode |= MAC_MODE_APE_TX_EN | MAC_MODE_APE_RX_EN;
Joe Perches63c3a662011-04-26 08:12:10 +00008748 if (!tg3_flag(tp, 5705_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008749 !(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07008750 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700)
8751 tp->mac_mode |= MAC_MODE_LINK_POLARITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008752 tw32_f(MAC_MODE, tp->mac_mode | MAC_MODE_RXSTAT_CLEAR | MAC_MODE_TXSTAT_CLEAR);
8753 udelay(40);
8754
Michael Chan314fba32005-04-21 17:07:04 -07008755 /* tp->grc_local_ctrl is partially set up during tg3_get_invariants().
Joe Perches63c3a662011-04-26 08:12:10 +00008756 * If TG3_FLAG_IS_NIC is zero, we should read the
Michael Chan314fba32005-04-21 17:07:04 -07008757 * register to preserve the GPIO settings for LOMs. The GPIOs,
8758 * whether used as inputs or outputs, are set by boot code after
8759 * reset.
8760 */
Joe Perches63c3a662011-04-26 08:12:10 +00008761 if (!tg3_flag(tp, IS_NIC)) {
Michael Chan314fba32005-04-21 17:07:04 -07008762 u32 gpio_mask;
8763
Michael Chan9d26e212006-12-07 00:21:14 -08008764 gpio_mask = GRC_LCLCTRL_GPIO_OE0 | GRC_LCLCTRL_GPIO_OE1 |
8765 GRC_LCLCTRL_GPIO_OE2 | GRC_LCLCTRL_GPIO_OUTPUT0 |
8766 GRC_LCLCTRL_GPIO_OUTPUT1 | GRC_LCLCTRL_GPIO_OUTPUT2;
Michael Chan3e7d83b2005-04-21 17:10:36 -07008767
8768 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752)
8769 gpio_mask |= GRC_LCLCTRL_GPIO_OE3 |
8770 GRC_LCLCTRL_GPIO_OUTPUT3;
8771
Michael Chanaf36e6b2006-03-23 01:28:06 -08008772 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755)
8773 gpio_mask |= GRC_LCLCTRL_GPIO_UART_SEL;
8774
Gary Zambranoaaf84462007-05-05 11:51:45 -07008775 tp->grc_local_ctrl &= ~gpio_mask;
Michael Chan314fba32005-04-21 17:07:04 -07008776 tp->grc_local_ctrl |= tr32(GRC_LOCAL_CTRL) & gpio_mask;
8777
8778 /* GPIO1 must be driven high for eeprom write protect */
Joe Perches63c3a662011-04-26 08:12:10 +00008779 if (tg3_flag(tp, EEPROM_WRITE_PROT))
Michael Chan9d26e212006-12-07 00:21:14 -08008780 tp->grc_local_ctrl |= (GRC_LCLCTRL_GPIO_OE1 |
8781 GRC_LCLCTRL_GPIO_OUTPUT1);
Michael Chan314fba32005-04-21 17:07:04 -07008782 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008783 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
8784 udelay(100);
8785
Joe Perches63c3a662011-04-26 08:12:10 +00008786 if (tg3_flag(tp, USING_MSIX) && tp->irq_cnt > 1) {
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008787 val = tr32(MSGINT_MODE);
8788 val |= MSGINT_MODE_MULTIVEC_EN | MSGINT_MODE_ENABLE;
Matt Carlson5b39de92011-08-31 11:44:50 +00008789 if (!tg3_flag(tp, 1SHOT_MSI))
8790 val |= MSGINT_MODE_ONE_SHOT_DISABLE;
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008791 tw32(MSGINT_MODE, val);
8792 }
8793
Joe Perches63c3a662011-04-26 08:12:10 +00008794 if (!tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008795 tw32_f(DMAC_MODE, DMAC_MODE_ENABLE);
8796 udelay(40);
8797 }
8798
8799 val = (WDMAC_MODE_ENABLE | WDMAC_MODE_TGTABORT_ENAB |
8800 WDMAC_MODE_MSTABORT_ENAB | WDMAC_MODE_PARITYERR_ENAB |
8801 WDMAC_MODE_ADDROFLOW_ENAB | WDMAC_MODE_FIFOOFLOW_ENAB |
8802 WDMAC_MODE_FIFOURUN_ENAB | WDMAC_MODE_FIFOOREAD_ENAB |
8803 WDMAC_MODE_LNGREAD_ENAB);
8804
Matt Carlsonc5908932011-03-09 16:58:25 +00008805 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
8806 tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) {
Joe Perches63c3a662011-04-26 08:12:10 +00008807 if (tg3_flag(tp, TSO_CAPABLE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07008808 (tp->pci_chip_rev_id == CHIPREV_ID_5705_A1 ||
8809 tp->pci_chip_rev_id == CHIPREV_ID_5705_A2)) {
8810 /* nothing */
8811 } else if (!(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH) &&
Joe Perches63c3a662011-04-26 08:12:10 +00008812 !tg3_flag(tp, IS_5788)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008813 val |= WDMAC_MODE_RX_ACCEL;
8814 }
8815 }
8816
Michael Chand9ab5ad2006-03-20 22:27:35 -08008817 /* Enable host coalescing bug fix */
Joe Perches63c3a662011-04-26 08:12:10 +00008818 if (tg3_flag(tp, 5755_PLUS))
Matt Carlsonf51f3562008-05-25 23:45:08 -07008819 val |= WDMAC_MODE_STATUS_TAG_FIX;
Michael Chand9ab5ad2006-03-20 22:27:35 -08008820
Matt Carlson788a0352009-11-02 14:26:03 +00008821 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
8822 val |= WDMAC_MODE_BURST_ALL_DATA;
8823
Linus Torvalds1da177e2005-04-16 15:20:36 -07008824 tw32_f(WDMAC_MODE, val);
8825 udelay(40);
8826
Joe Perches63c3a662011-04-26 08:12:10 +00008827 if (tg3_flag(tp, PCIX_MODE)) {
Matt Carlson9974a352007-10-07 23:27:28 -07008828 u16 pcix_cmd;
8829
8830 pci_read_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
8831 &pcix_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008832 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703) {
Matt Carlson9974a352007-10-07 23:27:28 -07008833 pcix_cmd &= ~PCI_X_CMD_MAX_READ;
8834 pcix_cmd |= PCI_X_CMD_READ_2K;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008835 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
Matt Carlson9974a352007-10-07 23:27:28 -07008836 pcix_cmd &= ~(PCI_X_CMD_MAX_SPLIT | PCI_X_CMD_MAX_READ);
8837 pcix_cmd |= PCI_X_CMD_READ_2K;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008838 }
Matt Carlson9974a352007-10-07 23:27:28 -07008839 pci_write_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
8840 pcix_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008841 }
8842
8843 tw32_f(RDMAC_MODE, rdmac_mode);
8844 udelay(40);
8845
8846 tw32(RCVDCC_MODE, RCVDCC_MODE_ENABLE | RCVDCC_MODE_ATTN_ENABLE);
Joe Perches63c3a662011-04-26 08:12:10 +00008847 if (!tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008848 tw32(MBFREE_MODE, MBFREE_MODE_ENABLE);
Matt Carlson9936bcf2007-10-10 18:03:07 -07008849
8850 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
8851 tw32(SNDDATAC_MODE,
8852 SNDDATAC_MODE_ENABLE | SNDDATAC_MODE_CDELAY);
8853 else
8854 tw32(SNDDATAC_MODE, SNDDATAC_MODE_ENABLE);
8855
Linus Torvalds1da177e2005-04-16 15:20:36 -07008856 tw32(SNDBDC_MODE, SNDBDC_MODE_ENABLE | SNDBDC_MODE_ATTN_ENABLE);
8857 tw32(RCVBDI_MODE, RCVBDI_MODE_ENABLE | RCVBDI_MODE_RCB_ATTN_ENAB);
Matt Carlson7cb32cf2010-09-30 10:34:36 +00008858 val = RCVDBDI_MODE_ENABLE | RCVDBDI_MODE_INV_RING_SZ;
Joe Perches63c3a662011-04-26 08:12:10 +00008859 if (tg3_flag(tp, LRG_PROD_RING_CAP))
Matt Carlson7cb32cf2010-09-30 10:34:36 +00008860 val |= RCVDBDI_MODE_LRG_RING_SZ;
8861 tw32(RCVDBDI_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008862 tw32(SNDDATAI_MODE, SNDDATAI_MODE_ENABLE);
Joe Perches63c3a662011-04-26 08:12:10 +00008863 if (tg3_flag(tp, HW_TSO_1) ||
8864 tg3_flag(tp, HW_TSO_2) ||
8865 tg3_flag(tp, HW_TSO_3))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008866 tw32(SNDDATAI_MODE, SNDDATAI_MODE_ENABLE | 0x8);
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008867 val = SNDBDI_MODE_ENABLE | SNDBDI_MODE_ATTN_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00008868 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008869 val |= SNDBDI_MODE_MULTI_TXQ_EN;
8870 tw32(SNDBDI_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008871 tw32(SNDBDS_MODE, SNDBDS_MODE_ENABLE | SNDBDS_MODE_ATTN_ENABLE);
8872
8873 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0) {
8874 err = tg3_load_5701_a0_firmware_fix(tp);
8875 if (err)
8876 return err;
8877 }
8878
Joe Perches63c3a662011-04-26 08:12:10 +00008879 if (tg3_flag(tp, TSO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008880 err = tg3_load_tso_firmware(tp);
8881 if (err)
8882 return err;
8883 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008884
8885 tp->tx_mode = TX_MODE_ENABLE;
Matt Carlsonf2096f92011-04-05 14:22:48 +00008886
Joe Perches63c3a662011-04-26 08:12:10 +00008887 if (tg3_flag(tp, 5755_PLUS) ||
Matt Carlsonb1d05212010-06-05 17:24:31 +00008888 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
8889 tp->tx_mode |= TX_MODE_MBUF_LOCKUP_FIX;
Matt Carlsonf2096f92011-04-05 14:22:48 +00008890
8891 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
8892 val = TX_MODE_JMB_FRM_LEN | TX_MODE_CNT_DN_MODE;
8893 tp->tx_mode &= ~val;
8894 tp->tx_mode |= tr32(MAC_TX_MODE) & val;
8895 }
8896
Linus Torvalds1da177e2005-04-16 15:20:36 -07008897 tw32_f(MAC_TX_MODE, tp->tx_mode);
8898 udelay(100);
8899
Joe Perches63c3a662011-04-26 08:12:10 +00008900 if (tg3_flag(tp, ENABLE_RSS)) {
Matt Carlson9d53fa12011-07-20 10:20:54 +00008901 int i = 0;
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008902 u32 reg = MAC_RSS_INDIR_TBL_0;
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008903
Matt Carlson9d53fa12011-07-20 10:20:54 +00008904 if (tp->irq_cnt == 2) {
8905 for (i = 0; i < TG3_RSS_INDIR_TBL_SIZE; i += 8) {
8906 tw32(reg, 0x0);
8907 reg += 4;
8908 }
8909 } else {
8910 u32 val;
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008911
Matt Carlson9d53fa12011-07-20 10:20:54 +00008912 while (i < TG3_RSS_INDIR_TBL_SIZE) {
8913 val = i % (tp->irq_cnt - 1);
8914 i++;
8915 for (; i % 8; i++) {
8916 val <<= 4;
8917 val |= (i % (tp->irq_cnt - 1));
8918 }
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008919 tw32(reg, val);
8920 reg += 4;
8921 }
8922 }
8923
8924 /* Setup the "secret" hash key. */
8925 tw32(MAC_RSS_HASH_KEY_0, 0x5f865437);
8926 tw32(MAC_RSS_HASH_KEY_1, 0xe4ac62cc);
8927 tw32(MAC_RSS_HASH_KEY_2, 0x50103a45);
8928 tw32(MAC_RSS_HASH_KEY_3, 0x36621985);
8929 tw32(MAC_RSS_HASH_KEY_4, 0xbf14c0e8);
8930 tw32(MAC_RSS_HASH_KEY_5, 0x1bc27a1e);
8931 tw32(MAC_RSS_HASH_KEY_6, 0x84f4b556);
8932 tw32(MAC_RSS_HASH_KEY_7, 0x094ea6fe);
8933 tw32(MAC_RSS_HASH_KEY_8, 0x7dda01e7);
8934 tw32(MAC_RSS_HASH_KEY_9, 0xc04d7481);
8935 }
8936
Linus Torvalds1da177e2005-04-16 15:20:36 -07008937 tp->rx_mode = RX_MODE_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00008938 if (tg3_flag(tp, 5755_PLUS))
Michael Chanaf36e6b2006-03-23 01:28:06 -08008939 tp->rx_mode |= RX_MODE_IPV6_CSUM_ENABLE;
8940
Joe Perches63c3a662011-04-26 08:12:10 +00008941 if (tg3_flag(tp, ENABLE_RSS))
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008942 tp->rx_mode |= RX_MODE_RSS_ENABLE |
8943 RX_MODE_RSS_ITBL_HASH_BITS_7 |
8944 RX_MODE_RSS_IPV6_HASH_EN |
8945 RX_MODE_RSS_TCP_IPV6_HASH_EN |
8946 RX_MODE_RSS_IPV4_HASH_EN |
8947 RX_MODE_RSS_TCP_IPV4_HASH_EN;
8948
Linus Torvalds1da177e2005-04-16 15:20:36 -07008949 tw32_f(MAC_RX_MODE, tp->rx_mode);
8950 udelay(10);
8951
Linus Torvalds1da177e2005-04-16 15:20:36 -07008952 tw32(MAC_LED_CTRL, tp->led_ctrl);
8953
8954 tw32(MAC_MI_STAT, MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008955 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008956 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
8957 udelay(10);
8958 }
8959 tw32_f(MAC_RX_MODE, tp->rx_mode);
8960 udelay(10);
8961
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008962 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008963 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008964 !(tp->phy_flags & TG3_PHYFLG_SERDES_PREEMPHASIS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008965 /* Set drive transmission level to 1.2V */
8966 /* only if the signal pre-emphasis bit is not set */
8967 val = tr32(MAC_SERDES_CFG);
8968 val &= 0xfffff000;
8969 val |= 0x880;
8970 tw32(MAC_SERDES_CFG, val);
8971 }
8972 if (tp->pci_chip_rev_id == CHIPREV_ID_5703_A1)
8973 tw32(MAC_SERDES_CFG, 0x616000);
8974 }
8975
8976 /* Prevent chip from dropping frames when flow control
8977 * is enabled.
8978 */
Matt Carlson666bc832010-01-20 16:58:03 +00008979 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
8980 val = 1;
8981 else
8982 val = 2;
8983 tw32_f(MAC_LOW_WMARK_MAX_RX_FRAME, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008984
8985 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008986 (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008987 /* Use hardware link auto-negotiation */
Joe Perches63c3a662011-04-26 08:12:10 +00008988 tg3_flag_set(tp, HW_AUTONEG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008989 }
8990
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008991 if ((tp->phy_flags & TG3_PHYFLG_MII_SERDES) &&
Matt Carlson6ff6f812011-05-19 12:12:54 +00008992 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
Michael Chand4d2c552006-03-20 17:47:20 -08008993 u32 tmp;
8994
8995 tmp = tr32(SERDES_RX_CTRL);
8996 tw32(SERDES_RX_CTRL, tmp | SERDES_RX_SIG_DETECT);
8997 tp->grc_local_ctrl &= ~GRC_LCLCTRL_USE_EXT_SIG_DETECT;
8998 tp->grc_local_ctrl |= GRC_LCLCTRL_USE_SIG_DETECT;
8999 tw32(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
9000 }
9001
Joe Perches63c3a662011-04-26 08:12:10 +00009002 if (!tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson80096062010-08-02 11:26:06 +00009003 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) {
9004 tp->phy_flags &= ~TG3_PHYFLG_IS_LOW_POWER;
Matt Carlsondd477002008-05-25 23:45:58 -07009005 tp->link_config.speed = tp->link_config.orig_speed;
9006 tp->link_config.duplex = tp->link_config.orig_duplex;
9007 tp->link_config.autoneg = tp->link_config.orig_autoneg;
9008 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009009
Matt Carlsondd477002008-05-25 23:45:58 -07009010 err = tg3_setup_phy(tp, 0);
9011 if (err)
9012 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009013
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009014 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
9015 !(tp->phy_flags & TG3_PHYFLG_IS_FET)) {
Matt Carlsondd477002008-05-25 23:45:58 -07009016 u32 tmp;
9017
9018 /* Clear CRC stats. */
9019 if (!tg3_readphy(tp, MII_TG3_TEST1, &tmp)) {
9020 tg3_writephy(tp, MII_TG3_TEST1,
9021 tmp | MII_TG3_TEST1_CRC_EN);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00009022 tg3_readphy(tp, MII_TG3_RXR_COUNTERS, &tmp);
Matt Carlsondd477002008-05-25 23:45:58 -07009023 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009024 }
9025 }
9026
9027 __tg3_set_rx_mode(tp->dev);
9028
9029 /* Initialize receive rules. */
9030 tw32(MAC_RCV_RULE_0, 0xc2000000 & RCV_RULE_DISABLE_MASK);
9031 tw32(MAC_RCV_VALUE_0, 0xffffffff & RCV_RULE_DISABLE_MASK);
9032 tw32(MAC_RCV_RULE_1, 0x86000004 & RCV_RULE_DISABLE_MASK);
9033 tw32(MAC_RCV_VALUE_1, 0xffffffff & RCV_RULE_DISABLE_MASK);
9034
Joe Perches63c3a662011-04-26 08:12:10 +00009035 if (tg3_flag(tp, 5705_PLUS) && !tg3_flag(tp, 5780_CLASS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009036 limit = 8;
9037 else
9038 limit = 16;
Joe Perches63c3a662011-04-26 08:12:10 +00009039 if (tg3_flag(tp, ENABLE_ASF))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009040 limit -= 4;
9041 switch (limit) {
9042 case 16:
9043 tw32(MAC_RCV_RULE_15, 0); tw32(MAC_RCV_VALUE_15, 0);
9044 case 15:
9045 tw32(MAC_RCV_RULE_14, 0); tw32(MAC_RCV_VALUE_14, 0);
9046 case 14:
9047 tw32(MAC_RCV_RULE_13, 0); tw32(MAC_RCV_VALUE_13, 0);
9048 case 13:
9049 tw32(MAC_RCV_RULE_12, 0); tw32(MAC_RCV_VALUE_12, 0);
9050 case 12:
9051 tw32(MAC_RCV_RULE_11, 0); tw32(MAC_RCV_VALUE_11, 0);
9052 case 11:
9053 tw32(MAC_RCV_RULE_10, 0); tw32(MAC_RCV_VALUE_10, 0);
9054 case 10:
9055 tw32(MAC_RCV_RULE_9, 0); tw32(MAC_RCV_VALUE_9, 0);
9056 case 9:
9057 tw32(MAC_RCV_RULE_8, 0); tw32(MAC_RCV_VALUE_8, 0);
9058 case 8:
9059 tw32(MAC_RCV_RULE_7, 0); tw32(MAC_RCV_VALUE_7, 0);
9060 case 7:
9061 tw32(MAC_RCV_RULE_6, 0); tw32(MAC_RCV_VALUE_6, 0);
9062 case 6:
9063 tw32(MAC_RCV_RULE_5, 0); tw32(MAC_RCV_VALUE_5, 0);
9064 case 5:
9065 tw32(MAC_RCV_RULE_4, 0); tw32(MAC_RCV_VALUE_4, 0);
9066 case 4:
9067 /* tw32(MAC_RCV_RULE_3, 0); tw32(MAC_RCV_VALUE_3, 0); */
9068 case 3:
9069 /* tw32(MAC_RCV_RULE_2, 0); tw32(MAC_RCV_VALUE_2, 0); */
9070 case 2:
9071 case 1:
9072
9073 default:
9074 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07009075 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009076
Joe Perches63c3a662011-04-26 08:12:10 +00009077 if (tg3_flag(tp, ENABLE_APE))
Matt Carlson9ce768e2007-10-11 19:49:11 -07009078 /* Write our heartbeat update interval to APE. */
9079 tg3_ape_write32(tp, TG3_APE_HOST_HEARTBEAT_INT_MS,
9080 APE_HOST_HEARTBEAT_INT_DISABLE);
Matt Carlson0d3031d2007-10-10 18:02:43 -07009081
Linus Torvalds1da177e2005-04-16 15:20:36 -07009082 tg3_write_sig_post_reset(tp, RESET_KIND_INIT);
9083
Linus Torvalds1da177e2005-04-16 15:20:36 -07009084 return 0;
9085}
9086
9087/* Called at device open time to get the chip ready for
9088 * packet processing. Invoked with tp->lock held.
9089 */
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07009090static int tg3_init_hw(struct tg3 *tp, int reset_phy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009091{
Linus Torvalds1da177e2005-04-16 15:20:36 -07009092 tg3_switch_clocks(tp);
9093
9094 tw32(TG3PCI_MEM_WIN_BASE_ADDR, 0);
9095
Matt Carlson2f751b62008-08-04 23:17:34 -07009096 return tg3_reset_hw(tp, reset_phy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009097}
9098
9099#define TG3_STAT_ADD32(PSTAT, REG) \
9100do { u32 __val = tr32(REG); \
9101 (PSTAT)->low += __val; \
9102 if ((PSTAT)->low < __val) \
9103 (PSTAT)->high += 1; \
9104} while (0)
9105
9106static void tg3_periodic_fetch_stats(struct tg3 *tp)
9107{
9108 struct tg3_hw_stats *sp = tp->hw_stats;
9109
9110 if (!netif_carrier_ok(tp->dev))
9111 return;
9112
9113 TG3_STAT_ADD32(&sp->tx_octets, MAC_TX_STATS_OCTETS);
9114 TG3_STAT_ADD32(&sp->tx_collisions, MAC_TX_STATS_COLLISIONS);
9115 TG3_STAT_ADD32(&sp->tx_xon_sent, MAC_TX_STATS_XON_SENT);
9116 TG3_STAT_ADD32(&sp->tx_xoff_sent, MAC_TX_STATS_XOFF_SENT);
9117 TG3_STAT_ADD32(&sp->tx_mac_errors, MAC_TX_STATS_MAC_ERRORS);
9118 TG3_STAT_ADD32(&sp->tx_single_collisions, MAC_TX_STATS_SINGLE_COLLISIONS);
9119 TG3_STAT_ADD32(&sp->tx_mult_collisions, MAC_TX_STATS_MULT_COLLISIONS);
9120 TG3_STAT_ADD32(&sp->tx_deferred, MAC_TX_STATS_DEFERRED);
9121 TG3_STAT_ADD32(&sp->tx_excessive_collisions, MAC_TX_STATS_EXCESSIVE_COL);
9122 TG3_STAT_ADD32(&sp->tx_late_collisions, MAC_TX_STATS_LATE_COL);
9123 TG3_STAT_ADD32(&sp->tx_ucast_packets, MAC_TX_STATS_UCAST);
9124 TG3_STAT_ADD32(&sp->tx_mcast_packets, MAC_TX_STATS_MCAST);
9125 TG3_STAT_ADD32(&sp->tx_bcast_packets, MAC_TX_STATS_BCAST);
9126
9127 TG3_STAT_ADD32(&sp->rx_octets, MAC_RX_STATS_OCTETS);
9128 TG3_STAT_ADD32(&sp->rx_fragments, MAC_RX_STATS_FRAGMENTS);
9129 TG3_STAT_ADD32(&sp->rx_ucast_packets, MAC_RX_STATS_UCAST);
9130 TG3_STAT_ADD32(&sp->rx_mcast_packets, MAC_RX_STATS_MCAST);
9131 TG3_STAT_ADD32(&sp->rx_bcast_packets, MAC_RX_STATS_BCAST);
9132 TG3_STAT_ADD32(&sp->rx_fcs_errors, MAC_RX_STATS_FCS_ERRORS);
9133 TG3_STAT_ADD32(&sp->rx_align_errors, MAC_RX_STATS_ALIGN_ERRORS);
9134 TG3_STAT_ADD32(&sp->rx_xon_pause_rcvd, MAC_RX_STATS_XON_PAUSE_RECVD);
9135 TG3_STAT_ADD32(&sp->rx_xoff_pause_rcvd, MAC_RX_STATS_XOFF_PAUSE_RECVD);
9136 TG3_STAT_ADD32(&sp->rx_mac_ctrl_rcvd, MAC_RX_STATS_MAC_CTRL_RECVD);
9137 TG3_STAT_ADD32(&sp->rx_xoff_entered, MAC_RX_STATS_XOFF_ENTERED);
9138 TG3_STAT_ADD32(&sp->rx_frame_too_long_errors, MAC_RX_STATS_FRAME_TOO_LONG);
9139 TG3_STAT_ADD32(&sp->rx_jabbers, MAC_RX_STATS_JABBERS);
9140 TG3_STAT_ADD32(&sp->rx_undersize_packets, MAC_RX_STATS_UNDERSIZE);
Michael Chan463d3052006-05-22 16:36:27 -07009141
9142 TG3_STAT_ADD32(&sp->rxbds_empty, RCVLPC_NO_RCV_BD_CNT);
Matt Carlson310050f2011-05-19 12:12:55 +00009143 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717 &&
9144 tp->pci_chip_rev_id != CHIPREV_ID_5719_A0 &&
9145 tp->pci_chip_rev_id != CHIPREV_ID_5720_A0) {
Matt Carlson4d958472011-04-20 07:57:35 +00009146 TG3_STAT_ADD32(&sp->rx_discards, RCVLPC_IN_DISCARDS_CNT);
9147 } else {
9148 u32 val = tr32(HOSTCC_FLOW_ATTN);
9149 val = (val & HOSTCC_FLOW_ATTN_MBUF_LWM) ? 1 : 0;
9150 if (val) {
9151 tw32(HOSTCC_FLOW_ATTN, HOSTCC_FLOW_ATTN_MBUF_LWM);
9152 sp->rx_discards.low += val;
9153 if (sp->rx_discards.low < val)
9154 sp->rx_discards.high += 1;
9155 }
9156 sp->mbuf_lwm_thresh_hit = sp->rx_discards;
9157 }
Michael Chan463d3052006-05-22 16:36:27 -07009158 TG3_STAT_ADD32(&sp->rx_errors, RCVLPC_IN_ERRORS_CNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009159}
9160
Matt Carlson0e6cf6a2011-06-13 13:38:55 +00009161static void tg3_chk_missed_msi(struct tg3 *tp)
9162{
9163 u32 i;
9164
9165 for (i = 0; i < tp->irq_cnt; i++) {
9166 struct tg3_napi *tnapi = &tp->napi[i];
9167
9168 if (tg3_has_work(tnapi)) {
9169 if (tnapi->last_rx_cons == tnapi->rx_rcb_ptr &&
9170 tnapi->last_tx_cons == tnapi->tx_cons) {
9171 if (tnapi->chk_msi_cnt < 1) {
9172 tnapi->chk_msi_cnt++;
9173 return;
9174 }
Matt Carlson7f230732011-08-31 11:44:48 +00009175 tg3_msi(0, tnapi);
Matt Carlson0e6cf6a2011-06-13 13:38:55 +00009176 }
9177 }
9178 tnapi->chk_msi_cnt = 0;
9179 tnapi->last_rx_cons = tnapi->rx_rcb_ptr;
9180 tnapi->last_tx_cons = tnapi->tx_cons;
9181 }
9182}
9183
Linus Torvalds1da177e2005-04-16 15:20:36 -07009184static void tg3_timer(unsigned long __opaque)
9185{
9186 struct tg3 *tp = (struct tg3 *) __opaque;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009187
Matt Carlson5b190622011-11-04 09:15:04 +00009188 if (tp->irq_sync || tg3_flag(tp, RESET_TASK_PENDING))
Michael Chanf475f162006-03-27 23:20:14 -08009189 goto restart_timer;
9190
David S. Millerf47c11e2005-06-24 20:18:35 -07009191 spin_lock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009192
Matt Carlson0e6cf6a2011-06-13 13:38:55 +00009193 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
9194 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
9195 tg3_chk_missed_msi(tp);
9196
Joe Perches63c3a662011-04-26 08:12:10 +00009197 if (!tg3_flag(tp, TAGGED_STATUS)) {
David S. Millerfac9b832005-05-18 22:46:34 -07009198 /* All of this garbage is because when using non-tagged
9199 * IRQ status the mailbox/status_block protocol the chip
9200 * uses with the cpu is race prone.
9201 */
Matt Carlson898a56f2009-08-28 14:02:40 +00009202 if (tp->napi[0].hw_status->status & SD_STATUS_UPDATED) {
David S. Millerfac9b832005-05-18 22:46:34 -07009203 tw32(GRC_LOCAL_CTRL,
9204 tp->grc_local_ctrl | GRC_LCLCTRL_SETINT);
9205 } else {
9206 tw32(HOSTCC_MODE, tp->coalesce_mode |
Matt Carlsonfd2ce372009-09-01 12:51:13 +00009207 HOSTCC_MODE_ENABLE | HOSTCC_MODE_NOW);
David S. Millerfac9b832005-05-18 22:46:34 -07009208 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009209
David S. Millerfac9b832005-05-18 22:46:34 -07009210 if (!(tr32(WDMAC_MODE) & WDMAC_MODE_ENABLE)) {
David S. Millerf47c11e2005-06-24 20:18:35 -07009211 spin_unlock(&tp->lock);
Matt Carlsondb219972011-11-04 09:15:03 +00009212 tg3_reset_task_schedule(tp);
Matt Carlson5b190622011-11-04 09:15:04 +00009213 goto restart_timer;
David S. Millerfac9b832005-05-18 22:46:34 -07009214 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009215 }
9216
Linus Torvalds1da177e2005-04-16 15:20:36 -07009217 /* This part only runs once per second. */
9218 if (!--tp->timer_counter) {
Joe Perches63c3a662011-04-26 08:12:10 +00009219 if (tg3_flag(tp, 5705_PLUS))
David S. Millerfac9b832005-05-18 22:46:34 -07009220 tg3_periodic_fetch_stats(tp);
9221
Matt Carlsonb0c59432011-05-19 12:12:48 +00009222 if (tp->setlpicnt && !--tp->setlpicnt)
9223 tg3_phy_eee_enable(tp);
Matt Carlson52b02d02010-10-14 10:37:41 +00009224
Joe Perches63c3a662011-04-26 08:12:10 +00009225 if (tg3_flag(tp, USE_LINKCHG_REG)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009226 u32 mac_stat;
9227 int phy_event;
9228
9229 mac_stat = tr32(MAC_STATUS);
9230
9231 phy_event = 0;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009232 if (tp->phy_flags & TG3_PHYFLG_USE_MI_INTERRUPT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009233 if (mac_stat & MAC_STATUS_MI_INTERRUPT)
9234 phy_event = 1;
9235 } else if (mac_stat & MAC_STATUS_LNKSTATE_CHANGED)
9236 phy_event = 1;
9237
9238 if (phy_event)
9239 tg3_setup_phy(tp, 0);
Joe Perches63c3a662011-04-26 08:12:10 +00009240 } else if (tg3_flag(tp, POLL_SERDES)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009241 u32 mac_stat = tr32(MAC_STATUS);
9242 int need_setup = 0;
9243
9244 if (netif_carrier_ok(tp->dev) &&
9245 (mac_stat & MAC_STATUS_LNKSTATE_CHANGED)) {
9246 need_setup = 1;
9247 }
Matt Carlsonbe98da62010-07-11 09:31:46 +00009248 if (!netif_carrier_ok(tp->dev) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07009249 (mac_stat & (MAC_STATUS_PCS_SYNCED |
9250 MAC_STATUS_SIGNAL_DET))) {
9251 need_setup = 1;
9252 }
9253 if (need_setup) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07009254 if (!tp->serdes_counter) {
9255 tw32_f(MAC_MODE,
9256 (tp->mac_mode &
9257 ~MAC_MODE_PORT_MODE_MASK));
9258 udelay(40);
9259 tw32_f(MAC_MODE, tp->mac_mode);
9260 udelay(40);
9261 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009262 tg3_setup_phy(tp, 0);
9263 }
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009264 } else if ((tp->phy_flags & TG3_PHYFLG_MII_SERDES) &&
Joe Perches63c3a662011-04-26 08:12:10 +00009265 tg3_flag(tp, 5780_CLASS)) {
Michael Chan747e8f82005-07-25 12:33:22 -07009266 tg3_serdes_parallel_detect(tp);
Matt Carlson57d8b882010-06-05 17:24:35 +00009267 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009268
9269 tp->timer_counter = tp->timer_multiplier;
9270 }
9271
Michael Chan130b8e42006-09-27 16:00:40 -07009272 /* Heartbeat is only sent once every 2 seconds.
9273 *
9274 * The heartbeat is to tell the ASF firmware that the host
9275 * driver is still alive. In the event that the OS crashes,
9276 * ASF needs to reset the hardware to free up the FIFO space
9277 * that may be filled with rx packets destined for the host.
9278 * If the FIFO is full, ASF will no longer function properly.
9279 *
9280 * Unintended resets have been reported on real time kernels
9281 * where the timer doesn't run on time. Netpoll will also have
9282 * same problem.
9283 *
9284 * The new FWCMD_NICDRV_ALIVE3 command tells the ASF firmware
9285 * to check the ring condition when the heartbeat is expiring
9286 * before doing the reset. This will prevent most unintended
9287 * resets.
9288 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07009289 if (!--tp->asf_counter) {
Joe Perches63c3a662011-04-26 08:12:10 +00009290 if (tg3_flag(tp, ENABLE_ASF) && !tg3_flag(tp, ENABLE_APE)) {
Matt Carlson7c5026a2008-05-02 16:49:29 -07009291 tg3_wait_for_event_ack(tp);
9292
Michael Chanbbadf502006-04-06 21:46:34 -07009293 tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX,
Michael Chan130b8e42006-09-27 16:00:40 -07009294 FWCMD_NICDRV_ALIVE3);
Michael Chanbbadf502006-04-06 21:46:34 -07009295 tg3_write_mem(tp, NIC_SRAM_FW_CMD_LEN_MBOX, 4);
Matt Carlsonc6cdf432010-04-05 10:19:26 +00009296 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX,
9297 TG3_FW_UPDATE_TIMEOUT_SEC);
Matt Carlson4ba526c2008-08-15 14:10:04 -07009298
9299 tg3_generate_fw_event(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009300 }
9301 tp->asf_counter = tp->asf_multiplier;
9302 }
9303
David S. Millerf47c11e2005-06-24 20:18:35 -07009304 spin_unlock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009305
Michael Chanf475f162006-03-27 23:20:14 -08009306restart_timer:
Linus Torvalds1da177e2005-04-16 15:20:36 -07009307 tp->timer.expires = jiffies + tp->timer_offset;
9308 add_timer(&tp->timer);
9309}
9310
Matt Carlson4f125f42009-09-01 12:55:02 +00009311static int tg3_request_irq(struct tg3 *tp, int irq_num)
Michael Chanfcfa0a32006-03-20 22:28:41 -08009312{
David Howells7d12e782006-10-05 14:55:46 +01009313 irq_handler_t fn;
Michael Chanfcfa0a32006-03-20 22:28:41 -08009314 unsigned long flags;
Matt Carlson4f125f42009-09-01 12:55:02 +00009315 char *name;
9316 struct tg3_napi *tnapi = &tp->napi[irq_num];
9317
9318 if (tp->irq_cnt == 1)
9319 name = tp->dev->name;
9320 else {
9321 name = &tnapi->irq_lbl[0];
9322 snprintf(name, IFNAMSIZ, "%s-%d", tp->dev->name, irq_num);
9323 name[IFNAMSIZ-1] = 0;
9324 }
Michael Chanfcfa0a32006-03-20 22:28:41 -08009325
Joe Perches63c3a662011-04-26 08:12:10 +00009326 if (tg3_flag(tp, USING_MSI) || tg3_flag(tp, USING_MSIX)) {
Michael Chanfcfa0a32006-03-20 22:28:41 -08009327 fn = tg3_msi;
Joe Perches63c3a662011-04-26 08:12:10 +00009328 if (tg3_flag(tp, 1SHOT_MSI))
Michael Chanfcfa0a32006-03-20 22:28:41 -08009329 fn = tg3_msi_1shot;
Javier Martinez Canillasab392d22011-03-28 16:27:31 +00009330 flags = 0;
Michael Chanfcfa0a32006-03-20 22:28:41 -08009331 } else {
9332 fn = tg3_interrupt;
Joe Perches63c3a662011-04-26 08:12:10 +00009333 if (tg3_flag(tp, TAGGED_STATUS))
Michael Chanfcfa0a32006-03-20 22:28:41 -08009334 fn = tg3_interrupt_tagged;
Javier Martinez Canillasab392d22011-03-28 16:27:31 +00009335 flags = IRQF_SHARED;
Michael Chanfcfa0a32006-03-20 22:28:41 -08009336 }
Matt Carlson4f125f42009-09-01 12:55:02 +00009337
9338 return request_irq(tnapi->irq_vec, fn, flags, name, tnapi);
Michael Chanfcfa0a32006-03-20 22:28:41 -08009339}
9340
Michael Chan79381092005-04-21 17:13:59 -07009341static int tg3_test_interrupt(struct tg3 *tp)
9342{
Matt Carlson09943a12009-08-28 14:01:57 +00009343 struct tg3_napi *tnapi = &tp->napi[0];
Michael Chan79381092005-04-21 17:13:59 -07009344 struct net_device *dev = tp->dev;
Michael Chanb16250e2006-09-27 16:10:14 -07009345 int err, i, intr_ok = 0;
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009346 u32 val;
Michael Chan79381092005-04-21 17:13:59 -07009347
Michael Chand4bc3922005-05-29 14:59:20 -07009348 if (!netif_running(dev))
9349 return -ENODEV;
9350
Michael Chan79381092005-04-21 17:13:59 -07009351 tg3_disable_ints(tp);
9352
Matt Carlson4f125f42009-09-01 12:55:02 +00009353 free_irq(tnapi->irq_vec, tnapi);
Michael Chan79381092005-04-21 17:13:59 -07009354
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009355 /*
9356 * Turn off MSI one shot mode. Otherwise this test has no
9357 * observable way to know whether the interrupt was delivered.
9358 */
Matt Carlson3aa1cdf2011-07-20 10:20:55 +00009359 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009360 val = tr32(MSGINT_MODE) | MSGINT_MODE_ONE_SHOT_DISABLE;
9361 tw32(MSGINT_MODE, val);
9362 }
9363
Matt Carlson4f125f42009-09-01 12:55:02 +00009364 err = request_irq(tnapi->irq_vec, tg3_test_isr,
Matt Carlson09943a12009-08-28 14:01:57 +00009365 IRQF_SHARED | IRQF_SAMPLE_RANDOM, dev->name, tnapi);
Michael Chan79381092005-04-21 17:13:59 -07009366 if (err)
9367 return err;
9368
Matt Carlson898a56f2009-08-28 14:02:40 +00009369 tnapi->hw_status->status &= ~SD_STATUS_UPDATED;
Michael Chan79381092005-04-21 17:13:59 -07009370 tg3_enable_ints(tp);
9371
9372 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +00009373 tnapi->coal_now);
Michael Chan79381092005-04-21 17:13:59 -07009374
9375 for (i = 0; i < 5; i++) {
Michael Chanb16250e2006-09-27 16:10:14 -07009376 u32 int_mbox, misc_host_ctrl;
9377
Matt Carlson898a56f2009-08-28 14:02:40 +00009378 int_mbox = tr32_mailbox(tnapi->int_mbox);
Michael Chanb16250e2006-09-27 16:10:14 -07009379 misc_host_ctrl = tr32(TG3PCI_MISC_HOST_CTRL);
9380
9381 if ((int_mbox != 0) ||
9382 (misc_host_ctrl & MISC_HOST_CTRL_MASK_PCI_INT)) {
9383 intr_ok = 1;
Michael Chan79381092005-04-21 17:13:59 -07009384 break;
Michael Chanb16250e2006-09-27 16:10:14 -07009385 }
9386
Matt Carlson3aa1cdf2011-07-20 10:20:55 +00009387 if (tg3_flag(tp, 57765_PLUS) &&
9388 tnapi->hw_status->status_tag != tnapi->last_tag)
9389 tw32_mailbox_f(tnapi->int_mbox, tnapi->last_tag << 24);
9390
Michael Chan79381092005-04-21 17:13:59 -07009391 msleep(10);
9392 }
9393
9394 tg3_disable_ints(tp);
9395
Matt Carlson4f125f42009-09-01 12:55:02 +00009396 free_irq(tnapi->irq_vec, tnapi);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009397
Matt Carlson4f125f42009-09-01 12:55:02 +00009398 err = tg3_request_irq(tp, 0);
Michael Chan79381092005-04-21 17:13:59 -07009399
9400 if (err)
9401 return err;
9402
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009403 if (intr_ok) {
9404 /* Reenable MSI one shot mode. */
Matt Carlson5b39de92011-08-31 11:44:50 +00009405 if (tg3_flag(tp, 57765_PLUS) && tg3_flag(tp, 1SHOT_MSI)) {
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009406 val = tr32(MSGINT_MODE) & ~MSGINT_MODE_ONE_SHOT_DISABLE;
9407 tw32(MSGINT_MODE, val);
9408 }
Michael Chan79381092005-04-21 17:13:59 -07009409 return 0;
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009410 }
Michael Chan79381092005-04-21 17:13:59 -07009411
9412 return -EIO;
9413}
9414
9415/* Returns 0 if MSI test succeeds or MSI test fails and INTx mode is
9416 * successfully restored
9417 */
9418static int tg3_test_msi(struct tg3 *tp)
9419{
Michael Chan79381092005-04-21 17:13:59 -07009420 int err;
9421 u16 pci_cmd;
9422
Joe Perches63c3a662011-04-26 08:12:10 +00009423 if (!tg3_flag(tp, USING_MSI))
Michael Chan79381092005-04-21 17:13:59 -07009424 return 0;
9425
9426 /* Turn off SERR reporting in case MSI terminates with Master
9427 * Abort.
9428 */
9429 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
9430 pci_write_config_word(tp->pdev, PCI_COMMAND,
9431 pci_cmd & ~PCI_COMMAND_SERR);
9432
9433 err = tg3_test_interrupt(tp);
9434
9435 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
9436
9437 if (!err)
9438 return 0;
9439
9440 /* other failures */
9441 if (err != -EIO)
9442 return err;
9443
9444 /* MSI test failed, go back to INTx mode */
Matt Carlson5129c3a2010-04-05 10:19:23 +00009445 netdev_warn(tp->dev, "No interrupt was generated using MSI. Switching "
9446 "to INTx mode. Please report this failure to the PCI "
9447 "maintainer and include system chipset information\n");
Michael Chan79381092005-04-21 17:13:59 -07009448
Matt Carlson4f125f42009-09-01 12:55:02 +00009449 free_irq(tp->napi[0].irq_vec, &tp->napi[0]);
Matt Carlson09943a12009-08-28 14:01:57 +00009450
Michael Chan79381092005-04-21 17:13:59 -07009451 pci_disable_msi(tp->pdev);
9452
Joe Perches63c3a662011-04-26 08:12:10 +00009453 tg3_flag_clear(tp, USING_MSI);
Andre Detschdc8bf1b2010-04-26 07:27:07 +00009454 tp->napi[0].irq_vec = tp->pdev->irq;
Michael Chan79381092005-04-21 17:13:59 -07009455
Matt Carlson4f125f42009-09-01 12:55:02 +00009456 err = tg3_request_irq(tp, 0);
Michael Chan79381092005-04-21 17:13:59 -07009457 if (err)
9458 return err;
9459
9460 /* Need to reset the chip because the MSI cycle may have terminated
9461 * with Master Abort.
9462 */
David S. Millerf47c11e2005-06-24 20:18:35 -07009463 tg3_full_lock(tp, 1);
Michael Chan79381092005-04-21 17:13:59 -07009464
Michael Chan944d9802005-05-29 14:57:48 -07009465 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07009466 err = tg3_init_hw(tp, 1);
Michael Chan79381092005-04-21 17:13:59 -07009467
David S. Millerf47c11e2005-06-24 20:18:35 -07009468 tg3_full_unlock(tp);
Michael Chan79381092005-04-21 17:13:59 -07009469
9470 if (err)
Matt Carlson4f125f42009-09-01 12:55:02 +00009471 free_irq(tp->napi[0].irq_vec, &tp->napi[0]);
Michael Chan79381092005-04-21 17:13:59 -07009472
9473 return err;
9474}
9475
Matt Carlson9e9fd122009-01-19 16:57:45 -08009476static int tg3_request_firmware(struct tg3 *tp)
9477{
9478 const __be32 *fw_data;
9479
9480 if (request_firmware(&tp->fw, tp->fw_needed, &tp->pdev->dev)) {
Joe Perches05dbe002010-02-17 19:44:19 +00009481 netdev_err(tp->dev, "Failed to load firmware \"%s\"\n",
9482 tp->fw_needed);
Matt Carlson9e9fd122009-01-19 16:57:45 -08009483 return -ENOENT;
9484 }
9485
9486 fw_data = (void *)tp->fw->data;
9487
9488 /* Firmware blob starts with version numbers, followed by
9489 * start address and _full_ length including BSS sections
9490 * (which must be longer than the actual data, of course
9491 */
9492
9493 tp->fw_len = be32_to_cpu(fw_data[2]); /* includes bss */
9494 if (tp->fw_len < (tp->fw->size - 12)) {
Joe Perches05dbe002010-02-17 19:44:19 +00009495 netdev_err(tp->dev, "bogus length %d in \"%s\"\n",
9496 tp->fw_len, tp->fw_needed);
Matt Carlson9e9fd122009-01-19 16:57:45 -08009497 release_firmware(tp->fw);
9498 tp->fw = NULL;
9499 return -EINVAL;
9500 }
9501
9502 /* We no longer need firmware; we have it. */
9503 tp->fw_needed = NULL;
9504 return 0;
9505}
9506
Matt Carlson679563f2009-09-01 12:55:46 +00009507static bool tg3_enable_msix(struct tg3 *tp)
9508{
9509 int i, rc, cpus = num_online_cpus();
9510 struct msix_entry msix_ent[tp->irq_max];
9511
9512 if (cpus == 1)
9513 /* Just fallback to the simpler MSI mode. */
9514 return false;
9515
9516 /*
9517 * We want as many rx rings enabled as there are cpus.
9518 * The first MSIX vector only deals with link interrupts, etc,
9519 * so we add one to the number of vectors we are requesting.
9520 */
9521 tp->irq_cnt = min_t(unsigned, cpus + 1, tp->irq_max);
9522
9523 for (i = 0; i < tp->irq_max; i++) {
9524 msix_ent[i].entry = i;
9525 msix_ent[i].vector = 0;
9526 }
9527
9528 rc = pci_enable_msix(tp->pdev, msix_ent, tp->irq_cnt);
Matt Carlson2430b032010-06-05 17:24:34 +00009529 if (rc < 0) {
9530 return false;
9531 } else if (rc != 0) {
Matt Carlson679563f2009-09-01 12:55:46 +00009532 if (pci_enable_msix(tp->pdev, msix_ent, rc))
9533 return false;
Joe Perches05dbe002010-02-17 19:44:19 +00009534 netdev_notice(tp->dev, "Requested %d MSI-X vectors, received %d\n",
9535 tp->irq_cnt, rc);
Matt Carlson679563f2009-09-01 12:55:46 +00009536 tp->irq_cnt = rc;
9537 }
9538
9539 for (i = 0; i < tp->irq_max; i++)
9540 tp->napi[i].irq_vec = msix_ent[i].vector;
9541
Ben Hutchings2ddaad32010-09-27 22:11:51 -07009542 netif_set_real_num_tx_queues(tp->dev, 1);
9543 rc = tp->irq_cnt > 1 ? tp->irq_cnt - 1 : 1;
9544 if (netif_set_real_num_rx_queues(tp->dev, rc)) {
9545 pci_disable_msix(tp->pdev);
9546 return false;
9547 }
Matt Carlsonb92b9042010-11-24 08:31:51 +00009548
9549 if (tp->irq_cnt > 1) {
Joe Perches63c3a662011-04-26 08:12:10 +00009550 tg3_flag_set(tp, ENABLE_RSS);
Matt Carlsond78b59f2011-04-05 14:22:46 +00009551
9552 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
9553 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
Joe Perches63c3a662011-04-26 08:12:10 +00009554 tg3_flag_set(tp, ENABLE_TSS);
Matt Carlsonb92b9042010-11-24 08:31:51 +00009555 netif_set_real_num_tx_queues(tp->dev, tp->irq_cnt - 1);
9556 }
9557 }
Matt Carlson2430b032010-06-05 17:24:34 +00009558
Matt Carlson679563f2009-09-01 12:55:46 +00009559 return true;
9560}
9561
Matt Carlson07b01732009-08-28 14:01:15 +00009562static void tg3_ints_init(struct tg3 *tp)
9563{
Joe Perches63c3a662011-04-26 08:12:10 +00009564 if ((tg3_flag(tp, SUPPORT_MSI) || tg3_flag(tp, SUPPORT_MSIX)) &&
9565 !tg3_flag(tp, TAGGED_STATUS)) {
Matt Carlson07b01732009-08-28 14:01:15 +00009566 /* All MSI supporting chips should support tagged
9567 * status. Assert that this is the case.
9568 */
Matt Carlson5129c3a2010-04-05 10:19:23 +00009569 netdev_warn(tp->dev,
9570 "MSI without TAGGED_STATUS? Not using MSI\n");
Matt Carlson679563f2009-09-01 12:55:46 +00009571 goto defcfg;
Matt Carlson07b01732009-08-28 14:01:15 +00009572 }
Matt Carlson4f125f42009-09-01 12:55:02 +00009573
Joe Perches63c3a662011-04-26 08:12:10 +00009574 if (tg3_flag(tp, SUPPORT_MSIX) && tg3_enable_msix(tp))
9575 tg3_flag_set(tp, USING_MSIX);
9576 else if (tg3_flag(tp, SUPPORT_MSI) && pci_enable_msi(tp->pdev) == 0)
9577 tg3_flag_set(tp, USING_MSI);
Matt Carlson679563f2009-09-01 12:55:46 +00009578
Joe Perches63c3a662011-04-26 08:12:10 +00009579 if (tg3_flag(tp, USING_MSI) || tg3_flag(tp, USING_MSIX)) {
Matt Carlson679563f2009-09-01 12:55:46 +00009580 u32 msi_mode = tr32(MSGINT_MODE);
Joe Perches63c3a662011-04-26 08:12:10 +00009581 if (tg3_flag(tp, USING_MSIX) && tp->irq_cnt > 1)
Matt Carlsonbaf8a942009-09-01 13:13:00 +00009582 msi_mode |= MSGINT_MODE_MULTIVEC_EN;
Matt Carlson5b39de92011-08-31 11:44:50 +00009583 if (!tg3_flag(tp, 1SHOT_MSI))
9584 msi_mode |= MSGINT_MODE_ONE_SHOT_DISABLE;
Matt Carlson679563f2009-09-01 12:55:46 +00009585 tw32(MSGINT_MODE, msi_mode | MSGINT_MODE_ENABLE);
9586 }
9587defcfg:
Joe Perches63c3a662011-04-26 08:12:10 +00009588 if (!tg3_flag(tp, USING_MSIX)) {
Matt Carlson679563f2009-09-01 12:55:46 +00009589 tp->irq_cnt = 1;
9590 tp->napi[0].irq_vec = tp->pdev->irq;
Ben Hutchings2ddaad32010-09-27 22:11:51 -07009591 netif_set_real_num_tx_queues(tp->dev, 1);
Matt Carlson85407882010-10-06 13:40:58 -07009592 netif_set_real_num_rx_queues(tp->dev, 1);
Matt Carlson679563f2009-09-01 12:55:46 +00009593 }
Matt Carlson07b01732009-08-28 14:01:15 +00009594}
9595
9596static void tg3_ints_fini(struct tg3 *tp)
9597{
Joe Perches63c3a662011-04-26 08:12:10 +00009598 if (tg3_flag(tp, USING_MSIX))
Matt Carlson679563f2009-09-01 12:55:46 +00009599 pci_disable_msix(tp->pdev);
Joe Perches63c3a662011-04-26 08:12:10 +00009600 else if (tg3_flag(tp, USING_MSI))
Matt Carlson679563f2009-09-01 12:55:46 +00009601 pci_disable_msi(tp->pdev);
Joe Perches63c3a662011-04-26 08:12:10 +00009602 tg3_flag_clear(tp, USING_MSI);
9603 tg3_flag_clear(tp, USING_MSIX);
9604 tg3_flag_clear(tp, ENABLE_RSS);
9605 tg3_flag_clear(tp, ENABLE_TSS);
Matt Carlson07b01732009-08-28 14:01:15 +00009606}
9607
Linus Torvalds1da177e2005-04-16 15:20:36 -07009608static int tg3_open(struct net_device *dev)
9609{
9610 struct tg3 *tp = netdev_priv(dev);
Matt Carlson4f125f42009-09-01 12:55:02 +00009611 int i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009612
Matt Carlson9e9fd122009-01-19 16:57:45 -08009613 if (tp->fw_needed) {
9614 err = tg3_request_firmware(tp);
9615 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0) {
9616 if (err)
9617 return err;
9618 } else if (err) {
Joe Perches05dbe002010-02-17 19:44:19 +00009619 netdev_warn(tp->dev, "TSO capability disabled\n");
Joe Perches63c3a662011-04-26 08:12:10 +00009620 tg3_flag_clear(tp, TSO_CAPABLE);
9621 } else if (!tg3_flag(tp, TSO_CAPABLE)) {
Joe Perches05dbe002010-02-17 19:44:19 +00009622 netdev_notice(tp->dev, "TSO capability restored\n");
Joe Perches63c3a662011-04-26 08:12:10 +00009623 tg3_flag_set(tp, TSO_CAPABLE);
Matt Carlson9e9fd122009-01-19 16:57:45 -08009624 }
9625 }
9626
Michael Chanc49a1562006-12-17 17:07:29 -08009627 netif_carrier_off(tp->dev);
9628
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00009629 err = tg3_power_up(tp);
Matt Carlson2f751b62008-08-04 23:17:34 -07009630 if (err)
Michael Chanbc1c7562006-03-20 17:48:03 -08009631 return err;
Matt Carlson2f751b62008-08-04 23:17:34 -07009632
9633 tg3_full_lock(tp, 0);
Michael Chanbc1c7562006-03-20 17:48:03 -08009634
Linus Torvalds1da177e2005-04-16 15:20:36 -07009635 tg3_disable_ints(tp);
Joe Perches63c3a662011-04-26 08:12:10 +00009636 tg3_flag_clear(tp, INIT_COMPLETE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009637
David S. Millerf47c11e2005-06-24 20:18:35 -07009638 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009639
Matt Carlson679563f2009-09-01 12:55:46 +00009640 /*
9641 * Setup interrupts first so we know how
9642 * many NAPI resources to allocate
9643 */
9644 tg3_ints_init(tp);
9645
Linus Torvalds1da177e2005-04-16 15:20:36 -07009646 /* The placement of this call is tied
9647 * to the setup and use of Host TX descriptors.
9648 */
9649 err = tg3_alloc_consistent(tp);
9650 if (err)
Matt Carlson679563f2009-09-01 12:55:46 +00009651 goto err_out1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009652
Matt Carlson66cfd1b2010-09-30 10:34:30 +00009653 tg3_napi_init(tp);
9654
Matt Carlsonfed97812009-09-01 13:10:19 +00009655 tg3_napi_enable(tp);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07009656
Matt Carlson4f125f42009-09-01 12:55:02 +00009657 for (i = 0; i < tp->irq_cnt; i++) {
9658 struct tg3_napi *tnapi = &tp->napi[i];
9659 err = tg3_request_irq(tp, i);
9660 if (err) {
Matt Carlson5bc09182011-11-04 09:15:01 +00009661 for (i--; i >= 0; i--) {
9662 tnapi = &tp->napi[i];
Matt Carlson4f125f42009-09-01 12:55:02 +00009663 free_irq(tnapi->irq_vec, tnapi);
Matt Carlson5bc09182011-11-04 09:15:01 +00009664 }
9665 goto err_out2;
Matt Carlson4f125f42009-09-01 12:55:02 +00009666 }
9667 }
Matt Carlson07b01732009-08-28 14:01:15 +00009668
David S. Millerf47c11e2005-06-24 20:18:35 -07009669 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009670
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07009671 err = tg3_init_hw(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009672 if (err) {
Michael Chan944d9802005-05-29 14:57:48 -07009673 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009674 tg3_free_rings(tp);
9675 } else {
Matt Carlson0e6cf6a2011-06-13 13:38:55 +00009676 if (tg3_flag(tp, TAGGED_STATUS) &&
9677 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717 &&
9678 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_57765)
David S. Millerfac9b832005-05-18 22:46:34 -07009679 tp->timer_offset = HZ;
9680 else
9681 tp->timer_offset = HZ / 10;
9682
9683 BUG_ON(tp->timer_offset > HZ);
9684 tp->timer_counter = tp->timer_multiplier =
9685 (HZ / tp->timer_offset);
9686 tp->asf_counter = tp->asf_multiplier =
Michael Chan28fbef72005-10-26 15:48:35 -07009687 ((HZ / tp->timer_offset) * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009688
9689 init_timer(&tp->timer);
9690 tp->timer.expires = jiffies + tp->timer_offset;
9691 tp->timer.data = (unsigned long) tp;
9692 tp->timer.function = tg3_timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009693 }
9694
David S. Millerf47c11e2005-06-24 20:18:35 -07009695 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009696
Matt Carlson07b01732009-08-28 14:01:15 +00009697 if (err)
Matt Carlson679563f2009-09-01 12:55:46 +00009698 goto err_out3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009699
Joe Perches63c3a662011-04-26 08:12:10 +00009700 if (tg3_flag(tp, USING_MSI)) {
Michael Chan79381092005-04-21 17:13:59 -07009701 err = tg3_test_msi(tp);
David S. Millerfac9b832005-05-18 22:46:34 -07009702
Michael Chan79381092005-04-21 17:13:59 -07009703 if (err) {
David S. Millerf47c11e2005-06-24 20:18:35 -07009704 tg3_full_lock(tp, 0);
Michael Chan944d9802005-05-29 14:57:48 -07009705 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Michael Chan79381092005-04-21 17:13:59 -07009706 tg3_free_rings(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -07009707 tg3_full_unlock(tp);
Michael Chan79381092005-04-21 17:13:59 -07009708
Matt Carlson679563f2009-09-01 12:55:46 +00009709 goto err_out2;
Michael Chan79381092005-04-21 17:13:59 -07009710 }
Michael Chanfcfa0a32006-03-20 22:28:41 -08009711
Joe Perches63c3a662011-04-26 08:12:10 +00009712 if (!tg3_flag(tp, 57765_PLUS) && tg3_flag(tp, USING_MSI)) {
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009713 u32 val = tr32(PCIE_TRANSACTION_CFG);
Michael Chanfcfa0a32006-03-20 22:28:41 -08009714
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009715 tw32(PCIE_TRANSACTION_CFG,
9716 val | PCIE_TRANS_CFG_1SHOT_MSI);
Michael Chanfcfa0a32006-03-20 22:28:41 -08009717 }
Michael Chan79381092005-04-21 17:13:59 -07009718 }
9719
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009720 tg3_phy_start(tp);
9721
David S. Millerf47c11e2005-06-24 20:18:35 -07009722 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009723
Michael Chan79381092005-04-21 17:13:59 -07009724 add_timer(&tp->timer);
Joe Perches63c3a662011-04-26 08:12:10 +00009725 tg3_flag_set(tp, INIT_COMPLETE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009726 tg3_enable_ints(tp);
9727
David S. Millerf47c11e2005-06-24 20:18:35 -07009728 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009729
Matt Carlsonfe5f5782009-09-01 13:09:39 +00009730 netif_tx_start_all_queues(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009731
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00009732 /*
9733 * Reset loopback feature if it was turned on while the device was down
9734 * make sure that it's installed properly now.
9735 */
9736 if (dev->features & NETIF_F_LOOPBACK)
9737 tg3_set_loopback(dev, dev->features);
9738
Linus Torvalds1da177e2005-04-16 15:20:36 -07009739 return 0;
Matt Carlson07b01732009-08-28 14:01:15 +00009740
Matt Carlson679563f2009-09-01 12:55:46 +00009741err_out3:
Matt Carlson4f125f42009-09-01 12:55:02 +00009742 for (i = tp->irq_cnt - 1; i >= 0; i--) {
9743 struct tg3_napi *tnapi = &tp->napi[i];
9744 free_irq(tnapi->irq_vec, tnapi);
9745 }
Matt Carlson07b01732009-08-28 14:01:15 +00009746
Matt Carlson679563f2009-09-01 12:55:46 +00009747err_out2:
Matt Carlsonfed97812009-09-01 13:10:19 +00009748 tg3_napi_disable(tp);
Matt Carlson66cfd1b2010-09-30 10:34:30 +00009749 tg3_napi_fini(tp);
Matt Carlson07b01732009-08-28 14:01:15 +00009750 tg3_free_consistent(tp);
Matt Carlson679563f2009-09-01 12:55:46 +00009751
9752err_out1:
9753 tg3_ints_fini(tp);
Matt Carlsoncd0d7222011-07-13 09:27:33 +00009754 tg3_frob_aux_power(tp, false);
9755 pci_set_power_state(tp->pdev, PCI_D3hot);
Matt Carlson07b01732009-08-28 14:01:15 +00009756 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009757}
9758
Eric Dumazet511d2222010-07-07 20:44:24 +00009759static struct rtnl_link_stats64 *tg3_get_stats64(struct net_device *,
9760 struct rtnl_link_stats64 *);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009761static struct tg3_ethtool_stats *tg3_get_estats(struct tg3 *);
9762
9763static int tg3_close(struct net_device *dev)
9764{
Matt Carlson4f125f42009-09-01 12:55:02 +00009765 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009766 struct tg3 *tp = netdev_priv(dev);
9767
Matt Carlsonfed97812009-09-01 13:10:19 +00009768 tg3_napi_disable(tp);
Matt Carlsondb219972011-11-04 09:15:03 +00009769 tg3_reset_task_cancel(tp);
Michael Chan7faa0062006-02-02 17:29:28 -08009770
Matt Carlsonfe5f5782009-09-01 13:09:39 +00009771 netif_tx_stop_all_queues(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009772
9773 del_timer_sync(&tp->timer);
9774
Matt Carlson24bb4fb2009-10-05 17:55:29 +00009775 tg3_phy_stop(tp);
9776
David S. Millerf47c11e2005-06-24 20:18:35 -07009777 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009778
9779 tg3_disable_ints(tp);
9780
Michael Chan944d9802005-05-29 14:57:48 -07009781 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009782 tg3_free_rings(tp);
Joe Perches63c3a662011-04-26 08:12:10 +00009783 tg3_flag_clear(tp, INIT_COMPLETE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009784
David S. Millerf47c11e2005-06-24 20:18:35 -07009785 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009786
Matt Carlson4f125f42009-09-01 12:55:02 +00009787 for (i = tp->irq_cnt - 1; i >= 0; i--) {
9788 struct tg3_napi *tnapi = &tp->napi[i];
9789 free_irq(tnapi->irq_vec, tnapi);
9790 }
Matt Carlson07b01732009-08-28 14:01:15 +00009791
9792 tg3_ints_fini(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009793
Eric Dumazet511d2222010-07-07 20:44:24 +00009794 tg3_get_stats64(tp->dev, &tp->net_stats_prev);
9795
Linus Torvalds1da177e2005-04-16 15:20:36 -07009796 memcpy(&tp->estats_prev, tg3_get_estats(tp),
9797 sizeof(tp->estats_prev));
9798
Matt Carlson66cfd1b2010-09-30 10:34:30 +00009799 tg3_napi_fini(tp);
9800
Linus Torvalds1da177e2005-04-16 15:20:36 -07009801 tg3_free_consistent(tp);
9802
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00009803 tg3_power_down(tp);
Michael Chanbc1c7562006-03-20 17:48:03 -08009804
9805 netif_carrier_off(tp->dev);
9806
Linus Torvalds1da177e2005-04-16 15:20:36 -07009807 return 0;
9808}
9809
Eric Dumazet511d2222010-07-07 20:44:24 +00009810static inline u64 get_stat64(tg3_stat64_t *val)
Stefan Buehler816f8b82008-08-15 14:10:54 -07009811{
9812 return ((u64)val->high << 32) | ((u64)val->low);
9813}
9814
Eric Dumazet511d2222010-07-07 20:44:24 +00009815static u64 calc_crc_errors(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009816{
9817 struct tg3_hw_stats *hw_stats = tp->hw_stats;
9818
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009819 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07009820 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
9821 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009822 u32 val;
9823
David S. Millerf47c11e2005-06-24 20:18:35 -07009824 spin_lock_bh(&tp->lock);
Michael Chan569a5df2007-02-13 12:18:15 -08009825 if (!tg3_readphy(tp, MII_TG3_TEST1, &val)) {
9826 tg3_writephy(tp, MII_TG3_TEST1,
9827 val | MII_TG3_TEST1_CRC_EN);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00009828 tg3_readphy(tp, MII_TG3_RXR_COUNTERS, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009829 } else
9830 val = 0;
David S. Millerf47c11e2005-06-24 20:18:35 -07009831 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009832
9833 tp->phy_crc_errors += val;
9834
9835 return tp->phy_crc_errors;
9836 }
9837
9838 return get_stat64(&hw_stats->rx_fcs_errors);
9839}
9840
9841#define ESTAT_ADD(member) \
9842 estats->member = old_estats->member + \
Eric Dumazet511d2222010-07-07 20:44:24 +00009843 get_stat64(&hw_stats->member)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009844
9845static struct tg3_ethtool_stats *tg3_get_estats(struct tg3 *tp)
9846{
9847 struct tg3_ethtool_stats *estats = &tp->estats;
9848 struct tg3_ethtool_stats *old_estats = &tp->estats_prev;
9849 struct tg3_hw_stats *hw_stats = tp->hw_stats;
9850
9851 if (!hw_stats)
9852 return old_estats;
9853
9854 ESTAT_ADD(rx_octets);
9855 ESTAT_ADD(rx_fragments);
9856 ESTAT_ADD(rx_ucast_packets);
9857 ESTAT_ADD(rx_mcast_packets);
9858 ESTAT_ADD(rx_bcast_packets);
9859 ESTAT_ADD(rx_fcs_errors);
9860 ESTAT_ADD(rx_align_errors);
9861 ESTAT_ADD(rx_xon_pause_rcvd);
9862 ESTAT_ADD(rx_xoff_pause_rcvd);
9863 ESTAT_ADD(rx_mac_ctrl_rcvd);
9864 ESTAT_ADD(rx_xoff_entered);
9865 ESTAT_ADD(rx_frame_too_long_errors);
9866 ESTAT_ADD(rx_jabbers);
9867 ESTAT_ADD(rx_undersize_packets);
9868 ESTAT_ADD(rx_in_length_errors);
9869 ESTAT_ADD(rx_out_length_errors);
9870 ESTAT_ADD(rx_64_or_less_octet_packets);
9871 ESTAT_ADD(rx_65_to_127_octet_packets);
9872 ESTAT_ADD(rx_128_to_255_octet_packets);
9873 ESTAT_ADD(rx_256_to_511_octet_packets);
9874 ESTAT_ADD(rx_512_to_1023_octet_packets);
9875 ESTAT_ADD(rx_1024_to_1522_octet_packets);
9876 ESTAT_ADD(rx_1523_to_2047_octet_packets);
9877 ESTAT_ADD(rx_2048_to_4095_octet_packets);
9878 ESTAT_ADD(rx_4096_to_8191_octet_packets);
9879 ESTAT_ADD(rx_8192_to_9022_octet_packets);
9880
9881 ESTAT_ADD(tx_octets);
9882 ESTAT_ADD(tx_collisions);
9883 ESTAT_ADD(tx_xon_sent);
9884 ESTAT_ADD(tx_xoff_sent);
9885 ESTAT_ADD(tx_flow_control);
9886 ESTAT_ADD(tx_mac_errors);
9887 ESTAT_ADD(tx_single_collisions);
9888 ESTAT_ADD(tx_mult_collisions);
9889 ESTAT_ADD(tx_deferred);
9890 ESTAT_ADD(tx_excessive_collisions);
9891 ESTAT_ADD(tx_late_collisions);
9892 ESTAT_ADD(tx_collide_2times);
9893 ESTAT_ADD(tx_collide_3times);
9894 ESTAT_ADD(tx_collide_4times);
9895 ESTAT_ADD(tx_collide_5times);
9896 ESTAT_ADD(tx_collide_6times);
9897 ESTAT_ADD(tx_collide_7times);
9898 ESTAT_ADD(tx_collide_8times);
9899 ESTAT_ADD(tx_collide_9times);
9900 ESTAT_ADD(tx_collide_10times);
9901 ESTAT_ADD(tx_collide_11times);
9902 ESTAT_ADD(tx_collide_12times);
9903 ESTAT_ADD(tx_collide_13times);
9904 ESTAT_ADD(tx_collide_14times);
9905 ESTAT_ADD(tx_collide_15times);
9906 ESTAT_ADD(tx_ucast_packets);
9907 ESTAT_ADD(tx_mcast_packets);
9908 ESTAT_ADD(tx_bcast_packets);
9909 ESTAT_ADD(tx_carrier_sense_errors);
9910 ESTAT_ADD(tx_discards);
9911 ESTAT_ADD(tx_errors);
9912
9913 ESTAT_ADD(dma_writeq_full);
9914 ESTAT_ADD(dma_write_prioq_full);
9915 ESTAT_ADD(rxbds_empty);
9916 ESTAT_ADD(rx_discards);
9917 ESTAT_ADD(rx_errors);
9918 ESTAT_ADD(rx_threshold_hit);
9919
9920 ESTAT_ADD(dma_readq_full);
9921 ESTAT_ADD(dma_read_prioq_full);
9922 ESTAT_ADD(tx_comp_queue_full);
9923
9924 ESTAT_ADD(ring_set_send_prod_index);
9925 ESTAT_ADD(ring_status_update);
9926 ESTAT_ADD(nic_irqs);
9927 ESTAT_ADD(nic_avoided_irqs);
9928 ESTAT_ADD(nic_tx_threshold_hit);
9929
Matt Carlson4452d092011-05-19 12:12:51 +00009930 ESTAT_ADD(mbuf_lwm_thresh_hit);
9931
Linus Torvalds1da177e2005-04-16 15:20:36 -07009932 return estats;
9933}
9934
Eric Dumazet511d2222010-07-07 20:44:24 +00009935static struct rtnl_link_stats64 *tg3_get_stats64(struct net_device *dev,
9936 struct rtnl_link_stats64 *stats)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009937{
9938 struct tg3 *tp = netdev_priv(dev);
Eric Dumazet511d2222010-07-07 20:44:24 +00009939 struct rtnl_link_stats64 *old_stats = &tp->net_stats_prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009940 struct tg3_hw_stats *hw_stats = tp->hw_stats;
9941
9942 if (!hw_stats)
9943 return old_stats;
9944
9945 stats->rx_packets = old_stats->rx_packets +
9946 get_stat64(&hw_stats->rx_ucast_packets) +
9947 get_stat64(&hw_stats->rx_mcast_packets) +
9948 get_stat64(&hw_stats->rx_bcast_packets);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009949
Linus Torvalds1da177e2005-04-16 15:20:36 -07009950 stats->tx_packets = old_stats->tx_packets +
9951 get_stat64(&hw_stats->tx_ucast_packets) +
9952 get_stat64(&hw_stats->tx_mcast_packets) +
9953 get_stat64(&hw_stats->tx_bcast_packets);
9954
9955 stats->rx_bytes = old_stats->rx_bytes +
9956 get_stat64(&hw_stats->rx_octets);
9957 stats->tx_bytes = old_stats->tx_bytes +
9958 get_stat64(&hw_stats->tx_octets);
9959
9960 stats->rx_errors = old_stats->rx_errors +
John W. Linville4f63b872005-09-12 14:43:18 -07009961 get_stat64(&hw_stats->rx_errors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009962 stats->tx_errors = old_stats->tx_errors +
9963 get_stat64(&hw_stats->tx_errors) +
9964 get_stat64(&hw_stats->tx_mac_errors) +
9965 get_stat64(&hw_stats->tx_carrier_sense_errors) +
9966 get_stat64(&hw_stats->tx_discards);
9967
9968 stats->multicast = old_stats->multicast +
9969 get_stat64(&hw_stats->rx_mcast_packets);
9970 stats->collisions = old_stats->collisions +
9971 get_stat64(&hw_stats->tx_collisions);
9972
9973 stats->rx_length_errors = old_stats->rx_length_errors +
9974 get_stat64(&hw_stats->rx_frame_too_long_errors) +
9975 get_stat64(&hw_stats->rx_undersize_packets);
9976
9977 stats->rx_over_errors = old_stats->rx_over_errors +
9978 get_stat64(&hw_stats->rxbds_empty);
9979 stats->rx_frame_errors = old_stats->rx_frame_errors +
9980 get_stat64(&hw_stats->rx_align_errors);
9981 stats->tx_aborted_errors = old_stats->tx_aborted_errors +
9982 get_stat64(&hw_stats->tx_discards);
9983 stats->tx_carrier_errors = old_stats->tx_carrier_errors +
9984 get_stat64(&hw_stats->tx_carrier_sense_errors);
9985
9986 stats->rx_crc_errors = old_stats->rx_crc_errors +
9987 calc_crc_errors(tp);
9988
John W. Linville4f63b872005-09-12 14:43:18 -07009989 stats->rx_missed_errors = old_stats->rx_missed_errors +
9990 get_stat64(&hw_stats->rx_discards);
9991
Eric Dumazetb0057c52010-10-10 19:55:52 +00009992 stats->rx_dropped = tp->rx_dropped;
Eric Dumazet48855432011-10-24 07:53:03 +00009993 stats->tx_dropped = tp->tx_dropped;
Eric Dumazetb0057c52010-10-10 19:55:52 +00009994
Linus Torvalds1da177e2005-04-16 15:20:36 -07009995 return stats;
9996}
9997
9998static inline u32 calc_crc(unsigned char *buf, int len)
9999{
10000 u32 reg;
10001 u32 tmp;
10002 int j, k;
10003
10004 reg = 0xffffffff;
10005
10006 for (j = 0; j < len; j++) {
10007 reg ^= buf[j];
10008
10009 for (k = 0; k < 8; k++) {
10010 tmp = reg & 0x01;
10011
10012 reg >>= 1;
10013
Matt Carlson859a5882010-04-05 10:19:28 +000010014 if (tmp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010015 reg ^= 0xedb88320;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010016 }
10017 }
10018
10019 return ~reg;
10020}
10021
10022static void tg3_set_multi(struct tg3 *tp, unsigned int accept_all)
10023{
10024 /* accept or reject all multicast frames */
10025 tw32(MAC_HASH_REG_0, accept_all ? 0xffffffff : 0);
10026 tw32(MAC_HASH_REG_1, accept_all ? 0xffffffff : 0);
10027 tw32(MAC_HASH_REG_2, accept_all ? 0xffffffff : 0);
10028 tw32(MAC_HASH_REG_3, accept_all ? 0xffffffff : 0);
10029}
10030
10031static void __tg3_set_rx_mode(struct net_device *dev)
10032{
10033 struct tg3 *tp = netdev_priv(dev);
10034 u32 rx_mode;
10035
10036 rx_mode = tp->rx_mode & ~(RX_MODE_PROMISC |
10037 RX_MODE_KEEP_VLAN_TAG);
10038
Matt Carlsonbf933c82011-01-25 15:58:49 +000010039#if !defined(CONFIG_VLAN_8021Q) && !defined(CONFIG_VLAN_8021Q_MODULE)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010040 /* When ASF is in use, we always keep the RX_MODE_KEEP_VLAN_TAG
10041 * flag clear.
10042 */
Joe Perches63c3a662011-04-26 08:12:10 +000010043 if (!tg3_flag(tp, ENABLE_ASF))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010044 rx_mode |= RX_MODE_KEEP_VLAN_TAG;
10045#endif
10046
10047 if (dev->flags & IFF_PROMISC) {
10048 /* Promiscuous mode. */
10049 rx_mode |= RX_MODE_PROMISC;
10050 } else if (dev->flags & IFF_ALLMULTI) {
10051 /* Accept all multicast. */
Matt Carlsonde6f31e2010-04-12 06:58:30 +000010052 tg3_set_multi(tp, 1);
Jiri Pirko4cd24ea2010-02-08 04:30:35 +000010053 } else if (netdev_mc_empty(dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010054 /* Reject all multicast. */
Matt Carlsonde6f31e2010-04-12 06:58:30 +000010055 tg3_set_multi(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010056 } else {
10057 /* Accept one or more multicast(s). */
Jiri Pirko22bedad2010-04-01 21:22:57 +000010058 struct netdev_hw_addr *ha;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010059 u32 mc_filter[4] = { 0, };
10060 u32 regidx;
10061 u32 bit;
10062 u32 crc;
10063
Jiri Pirko22bedad2010-04-01 21:22:57 +000010064 netdev_for_each_mc_addr(ha, dev) {
10065 crc = calc_crc(ha->addr, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010066 bit = ~crc & 0x7f;
10067 regidx = (bit & 0x60) >> 5;
10068 bit &= 0x1f;
10069 mc_filter[regidx] |= (1 << bit);
10070 }
10071
10072 tw32(MAC_HASH_REG_0, mc_filter[0]);
10073 tw32(MAC_HASH_REG_1, mc_filter[1]);
10074 tw32(MAC_HASH_REG_2, mc_filter[2]);
10075 tw32(MAC_HASH_REG_3, mc_filter[3]);
10076 }
10077
10078 if (rx_mode != tp->rx_mode) {
10079 tp->rx_mode = rx_mode;
10080 tw32_f(MAC_RX_MODE, rx_mode);
10081 udelay(10);
10082 }
10083}
10084
10085static void tg3_set_rx_mode(struct net_device *dev)
10086{
10087 struct tg3 *tp = netdev_priv(dev);
10088
Michael Chane75f7c92006-03-20 21:33:26 -080010089 if (!netif_running(dev))
10090 return;
10091
David S. Millerf47c11e2005-06-24 20:18:35 -070010092 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010093 __tg3_set_rx_mode(dev);
David S. Millerf47c11e2005-06-24 20:18:35 -070010094 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010095}
10096
Linus Torvalds1da177e2005-04-16 15:20:36 -070010097static int tg3_get_regs_len(struct net_device *dev)
10098{
Matt Carlson97bd8e42011-04-13 11:05:04 +000010099 return TG3_REG_BLK_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010100}
10101
10102static void tg3_get_regs(struct net_device *dev,
10103 struct ethtool_regs *regs, void *_p)
10104{
Linus Torvalds1da177e2005-04-16 15:20:36 -070010105 struct tg3 *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010106
10107 regs->version = 0;
10108
Matt Carlson97bd8e42011-04-13 11:05:04 +000010109 memset(_p, 0, TG3_REG_BLK_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010110
Matt Carlson80096062010-08-02 11:26:06 +000010111 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Michael Chanbc1c7562006-03-20 17:48:03 -080010112 return;
10113
David S. Millerf47c11e2005-06-24 20:18:35 -070010114 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010115
Matt Carlson97bd8e42011-04-13 11:05:04 +000010116 tg3_dump_legacy_regs(tp, (u32 *)_p);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010117
David S. Millerf47c11e2005-06-24 20:18:35 -070010118 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010119}
10120
10121static int tg3_get_eeprom_len(struct net_device *dev)
10122{
10123 struct tg3 *tp = netdev_priv(dev);
10124
10125 return tp->nvram_size;
10126}
10127
Linus Torvalds1da177e2005-04-16 15:20:36 -070010128static int tg3_get_eeprom(struct net_device *dev, struct ethtool_eeprom *eeprom, u8 *data)
10129{
10130 struct tg3 *tp = netdev_priv(dev);
10131 int ret;
10132 u8 *pd;
Al Virob9fc7dc2007-12-17 22:59:57 -080010133 u32 i, offset, len, b_offset, b_count;
Matt Carlsona9dc5292009-02-25 14:25:30 +000010134 __be32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010135
Joe Perches63c3a662011-04-26 08:12:10 +000010136 if (tg3_flag(tp, NO_NVRAM))
Matt Carlsondf259d82009-04-20 06:57:14 +000010137 return -EINVAL;
10138
Matt Carlson80096062010-08-02 11:26:06 +000010139 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Michael Chanbc1c7562006-03-20 17:48:03 -080010140 return -EAGAIN;
10141
Linus Torvalds1da177e2005-04-16 15:20:36 -070010142 offset = eeprom->offset;
10143 len = eeprom->len;
10144 eeprom->len = 0;
10145
10146 eeprom->magic = TG3_EEPROM_MAGIC;
10147
10148 if (offset & 3) {
10149 /* adjustments to start on required 4 byte boundary */
10150 b_offset = offset & 3;
10151 b_count = 4 - b_offset;
10152 if (b_count > len) {
10153 /* i.e. offset=1 len=2 */
10154 b_count = len;
10155 }
Matt Carlsona9dc5292009-02-25 14:25:30 +000010156 ret = tg3_nvram_read_be32(tp, offset-b_offset, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010157 if (ret)
10158 return ret;
Matt Carlsonbe98da62010-07-11 09:31:46 +000010159 memcpy(data, ((char *)&val) + b_offset, b_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010160 len -= b_count;
10161 offset += b_count;
Matt Carlsonc6cdf432010-04-05 10:19:26 +000010162 eeprom->len += b_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010163 }
10164
Lucas De Marchi25985ed2011-03-30 22:57:33 -030010165 /* read bytes up to the last 4 byte boundary */
Linus Torvalds1da177e2005-04-16 15:20:36 -070010166 pd = &data[eeprom->len];
10167 for (i = 0; i < (len - (len & 3)); i += 4) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000010168 ret = tg3_nvram_read_be32(tp, offset + i, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010169 if (ret) {
10170 eeprom->len += i;
10171 return ret;
10172 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010173 memcpy(pd + i, &val, 4);
10174 }
10175 eeprom->len += i;
10176
10177 if (len & 3) {
10178 /* read last bytes not ending on 4 byte boundary */
10179 pd = &data[eeprom->len];
10180 b_count = len & 3;
10181 b_offset = offset + len - b_count;
Matt Carlsona9dc5292009-02-25 14:25:30 +000010182 ret = tg3_nvram_read_be32(tp, b_offset, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010183 if (ret)
10184 return ret;
Al Virob9fc7dc2007-12-17 22:59:57 -080010185 memcpy(pd, &val, b_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010186 eeprom->len += b_count;
10187 }
10188 return 0;
10189}
10190
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010191static int tg3_nvram_write_block(struct tg3 *tp, u32 offset, u32 len, u8 *buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010192
10193static int tg3_set_eeprom(struct net_device *dev, struct ethtool_eeprom *eeprom, u8 *data)
10194{
10195 struct tg3 *tp = netdev_priv(dev);
10196 int ret;
Al Virob9fc7dc2007-12-17 22:59:57 -080010197 u32 offset, len, b_offset, odd_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010198 u8 *buf;
Matt Carlsona9dc5292009-02-25 14:25:30 +000010199 __be32 start, end;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010200
Matt Carlson80096062010-08-02 11:26:06 +000010201 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Michael Chanbc1c7562006-03-20 17:48:03 -080010202 return -EAGAIN;
10203
Joe Perches63c3a662011-04-26 08:12:10 +000010204 if (tg3_flag(tp, NO_NVRAM) ||
Matt Carlsondf259d82009-04-20 06:57:14 +000010205 eeprom->magic != TG3_EEPROM_MAGIC)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010206 return -EINVAL;
10207
10208 offset = eeprom->offset;
10209 len = eeprom->len;
10210
10211 if ((b_offset = (offset & 3))) {
10212 /* adjustments to start on required 4 byte boundary */
Matt Carlsona9dc5292009-02-25 14:25:30 +000010213 ret = tg3_nvram_read_be32(tp, offset-b_offset, &start);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010214 if (ret)
10215 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010216 len += b_offset;
10217 offset &= ~3;
Michael Chan1c8594b2005-04-21 17:12:46 -070010218 if (len < 4)
10219 len = 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010220 }
10221
10222 odd_len = 0;
Michael Chan1c8594b2005-04-21 17:12:46 -070010223 if (len & 3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010224 /* adjustments to end on required 4 byte boundary */
10225 odd_len = 1;
10226 len = (len + 3) & ~3;
Matt Carlsona9dc5292009-02-25 14:25:30 +000010227 ret = tg3_nvram_read_be32(tp, offset+len-4, &end);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010228 if (ret)
10229 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010230 }
10231
10232 buf = data;
10233 if (b_offset || odd_len) {
10234 buf = kmalloc(len, GFP_KERNEL);
Andy Gospodarekab0049b2007-09-06 20:42:14 +010010235 if (!buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010236 return -ENOMEM;
10237 if (b_offset)
10238 memcpy(buf, &start, 4);
10239 if (odd_len)
10240 memcpy(buf+len-4, &end, 4);
10241 memcpy(buf + b_offset, data, eeprom->len);
10242 }
10243
10244 ret = tg3_nvram_write_block(tp, offset, len, buf);
10245
10246 if (buf != data)
10247 kfree(buf);
10248
10249 return ret;
10250}
10251
10252static int tg3_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
10253{
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010254 struct tg3 *tp = netdev_priv(dev);
10255
Joe Perches63c3a662011-04-26 08:12:10 +000010256 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000010257 struct phy_device *phydev;
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010258 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010259 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000010260 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
10261 return phy_ethtool_gset(phydev, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010262 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010263
Linus Torvalds1da177e2005-04-16 15:20:36 -070010264 cmd->supported = (SUPPORTED_Autoneg);
10265
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010266 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010267 cmd->supported |= (SUPPORTED_1000baseT_Half |
10268 SUPPORTED_1000baseT_Full);
10269
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010270 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010271 cmd->supported |= (SUPPORTED_100baseT_Half |
10272 SUPPORTED_100baseT_Full |
10273 SUPPORTED_10baseT_Half |
10274 SUPPORTED_10baseT_Full |
Matt Carlson3bebab52007-11-12 21:22:40 -080010275 SUPPORTED_TP);
Karsten Keilef348142006-05-12 12:49:08 -070010276 cmd->port = PORT_TP;
10277 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010278 cmd->supported |= SUPPORTED_FIBRE;
Karsten Keilef348142006-05-12 12:49:08 -070010279 cmd->port = PORT_FIBRE;
10280 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010281
Linus Torvalds1da177e2005-04-16 15:20:36 -070010282 cmd->advertising = tp->link_config.advertising;
Matt Carlson5bb09772011-06-13 13:39:00 +000010283 if (tg3_flag(tp, PAUSE_AUTONEG)) {
10284 if (tp->link_config.flowctrl & FLOW_CTRL_RX) {
10285 if (tp->link_config.flowctrl & FLOW_CTRL_TX) {
10286 cmd->advertising |= ADVERTISED_Pause;
10287 } else {
10288 cmd->advertising |= ADVERTISED_Pause |
10289 ADVERTISED_Asym_Pause;
10290 }
10291 } else if (tp->link_config.flowctrl & FLOW_CTRL_TX) {
10292 cmd->advertising |= ADVERTISED_Asym_Pause;
10293 }
10294 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010295 if (netif_running(dev)) {
David Decotigny70739492011-04-27 18:32:40 +000010296 ethtool_cmd_speed_set(cmd, tp->link_config.active_speed);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010297 cmd->duplex = tp->link_config.active_duplex;
Matt Carlson64c22182010-10-14 10:37:44 +000010298 } else {
David Decotigny70739492011-04-27 18:32:40 +000010299 ethtool_cmd_speed_set(cmd, SPEED_INVALID);
Matt Carlson64c22182010-10-14 10:37:44 +000010300 cmd->duplex = DUPLEX_INVALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010301 }
Matt Carlson882e9792009-09-01 13:21:36 +000010302 cmd->phy_address = tp->phy_addr;
Matt Carlson7e5856b2009-02-25 14:23:01 +000010303 cmd->transceiver = XCVR_INTERNAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010304 cmd->autoneg = tp->link_config.autoneg;
10305 cmd->maxtxpkt = 0;
10306 cmd->maxrxpkt = 0;
10307 return 0;
10308}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010309
Linus Torvalds1da177e2005-04-16 15:20:36 -070010310static int tg3_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
10311{
10312 struct tg3 *tp = netdev_priv(dev);
David Decotigny25db0332011-04-27 18:32:39 +000010313 u32 speed = ethtool_cmd_speed(cmd);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010314
Joe Perches63c3a662011-04-26 08:12:10 +000010315 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000010316 struct phy_device *phydev;
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010317 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010318 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000010319 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
10320 return phy_ethtool_sset(phydev, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010321 }
10322
Matt Carlson7e5856b2009-02-25 14:23:01 +000010323 if (cmd->autoneg != AUTONEG_ENABLE &&
10324 cmd->autoneg != AUTONEG_DISABLE)
Michael Chan37ff2382005-10-26 15:49:51 -070010325 return -EINVAL;
Matt Carlson7e5856b2009-02-25 14:23:01 +000010326
10327 if (cmd->autoneg == AUTONEG_DISABLE &&
10328 cmd->duplex != DUPLEX_FULL &&
10329 cmd->duplex != DUPLEX_HALF)
Michael Chan37ff2382005-10-26 15:49:51 -070010330 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010331
Matt Carlson7e5856b2009-02-25 14:23:01 +000010332 if (cmd->autoneg == AUTONEG_ENABLE) {
10333 u32 mask = ADVERTISED_Autoneg |
10334 ADVERTISED_Pause |
10335 ADVERTISED_Asym_Pause;
10336
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010337 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
Matt Carlson7e5856b2009-02-25 14:23:01 +000010338 mask |= ADVERTISED_1000baseT_Half |
10339 ADVERTISED_1000baseT_Full;
10340
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010341 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
Matt Carlson7e5856b2009-02-25 14:23:01 +000010342 mask |= ADVERTISED_100baseT_Half |
10343 ADVERTISED_100baseT_Full |
10344 ADVERTISED_10baseT_Half |
10345 ADVERTISED_10baseT_Full |
10346 ADVERTISED_TP;
10347 else
10348 mask |= ADVERTISED_FIBRE;
10349
10350 if (cmd->advertising & ~mask)
10351 return -EINVAL;
10352
10353 mask &= (ADVERTISED_1000baseT_Half |
10354 ADVERTISED_1000baseT_Full |
10355 ADVERTISED_100baseT_Half |
10356 ADVERTISED_100baseT_Full |
10357 ADVERTISED_10baseT_Half |
10358 ADVERTISED_10baseT_Full);
10359
10360 cmd->advertising &= mask;
10361 } else {
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010362 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES) {
David Decotigny25db0332011-04-27 18:32:39 +000010363 if (speed != SPEED_1000)
Matt Carlson7e5856b2009-02-25 14:23:01 +000010364 return -EINVAL;
10365
10366 if (cmd->duplex != DUPLEX_FULL)
10367 return -EINVAL;
10368 } else {
David Decotigny25db0332011-04-27 18:32:39 +000010369 if (speed != SPEED_100 &&
10370 speed != SPEED_10)
Matt Carlson7e5856b2009-02-25 14:23:01 +000010371 return -EINVAL;
10372 }
10373 }
10374
David S. Millerf47c11e2005-06-24 20:18:35 -070010375 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010376
10377 tp->link_config.autoneg = cmd->autoneg;
10378 if (cmd->autoneg == AUTONEG_ENABLE) {
Andy Gospodarek405d8e52007-10-08 01:08:47 -070010379 tp->link_config.advertising = (cmd->advertising |
10380 ADVERTISED_Autoneg);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010381 tp->link_config.speed = SPEED_INVALID;
10382 tp->link_config.duplex = DUPLEX_INVALID;
10383 } else {
10384 tp->link_config.advertising = 0;
David Decotigny25db0332011-04-27 18:32:39 +000010385 tp->link_config.speed = speed;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010386 tp->link_config.duplex = cmd->duplex;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010387 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010388
Michael Chan24fcad62006-12-17 17:06:46 -080010389 tp->link_config.orig_speed = tp->link_config.speed;
10390 tp->link_config.orig_duplex = tp->link_config.duplex;
10391 tp->link_config.orig_autoneg = tp->link_config.autoneg;
10392
Linus Torvalds1da177e2005-04-16 15:20:36 -070010393 if (netif_running(dev))
10394 tg3_setup_phy(tp, 1);
10395
David S. Millerf47c11e2005-06-24 20:18:35 -070010396 tg3_full_unlock(tp);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010397
Linus Torvalds1da177e2005-04-16 15:20:36 -070010398 return 0;
10399}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010400
Linus Torvalds1da177e2005-04-16 15:20:36 -070010401static void tg3_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
10402{
10403 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010404
Rick Jones68aad782011-11-07 13:29:27 +000010405 strlcpy(info->driver, DRV_MODULE_NAME, sizeof(info->driver));
10406 strlcpy(info->version, DRV_MODULE_VERSION, sizeof(info->version));
10407 strlcpy(info->fw_version, tp->fw_ver, sizeof(info->fw_version));
10408 strlcpy(info->bus_info, pci_name(tp->pdev), sizeof(info->bus_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -070010409}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010410
Linus Torvalds1da177e2005-04-16 15:20:36 -070010411static void tg3_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
10412{
10413 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010414
Joe Perches63c3a662011-04-26 08:12:10 +000010415 if (tg3_flag(tp, WOL_CAP) && device_can_wakeup(&tp->pdev->dev))
Gary Zambranoa85feb82007-05-05 11:52:19 -070010416 wol->supported = WAKE_MAGIC;
10417 else
10418 wol->supported = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010419 wol->wolopts = 0;
Joe Perches63c3a662011-04-26 08:12:10 +000010420 if (tg3_flag(tp, WOL_ENABLE) && device_can_wakeup(&tp->pdev->dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010421 wol->wolopts = WAKE_MAGIC;
10422 memset(&wol->sopass, 0, sizeof(wol->sopass));
10423}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010424
Linus Torvalds1da177e2005-04-16 15:20:36 -070010425static int tg3_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
10426{
10427 struct tg3 *tp = netdev_priv(dev);
Rafael J. Wysocki12dac072008-07-30 16:37:33 -070010428 struct device *dp = &tp->pdev->dev;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010429
Linus Torvalds1da177e2005-04-16 15:20:36 -070010430 if (wol->wolopts & ~WAKE_MAGIC)
10431 return -EINVAL;
10432 if ((wol->wolopts & WAKE_MAGIC) &&
Joe Perches63c3a662011-04-26 08:12:10 +000010433 !(tg3_flag(tp, WOL_CAP) && device_can_wakeup(dp)))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010434 return -EINVAL;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010435
Rafael J. Wysockif2dc0d12010-10-25 13:01:55 +000010436 device_set_wakeup_enable(dp, wol->wolopts & WAKE_MAGIC);
10437
David S. Millerf47c11e2005-06-24 20:18:35 -070010438 spin_lock_bh(&tp->lock);
Rafael J. Wysockif2dc0d12010-10-25 13:01:55 +000010439 if (device_may_wakeup(dp))
Joe Perches63c3a662011-04-26 08:12:10 +000010440 tg3_flag_set(tp, WOL_ENABLE);
Rafael J. Wysockif2dc0d12010-10-25 13:01:55 +000010441 else
Joe Perches63c3a662011-04-26 08:12:10 +000010442 tg3_flag_clear(tp, WOL_ENABLE);
David S. Millerf47c11e2005-06-24 20:18:35 -070010443 spin_unlock_bh(&tp->lock);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010444
Linus Torvalds1da177e2005-04-16 15:20:36 -070010445 return 0;
10446}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010447
Linus Torvalds1da177e2005-04-16 15:20:36 -070010448static u32 tg3_get_msglevel(struct net_device *dev)
10449{
10450 struct tg3 *tp = netdev_priv(dev);
10451 return tp->msg_enable;
10452}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010453
Linus Torvalds1da177e2005-04-16 15:20:36 -070010454static void tg3_set_msglevel(struct net_device *dev, u32 value)
10455{
10456 struct tg3 *tp = netdev_priv(dev);
10457 tp->msg_enable = value;
10458}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010459
Linus Torvalds1da177e2005-04-16 15:20:36 -070010460static int tg3_nway_reset(struct net_device *dev)
10461{
10462 struct tg3 *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010463 int r;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010464
Linus Torvalds1da177e2005-04-16 15:20:36 -070010465 if (!netif_running(dev))
10466 return -EAGAIN;
10467
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010468 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Michael Chanc94e3942005-09-27 12:12:42 -070010469 return -EINVAL;
10470
Joe Perches63c3a662011-04-26 08:12:10 +000010471 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010472 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010473 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000010474 r = phy_start_aneg(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010475 } else {
10476 u32 bmcr;
10477
10478 spin_lock_bh(&tp->lock);
10479 r = -EINVAL;
10480 tg3_readphy(tp, MII_BMCR, &bmcr);
10481 if (!tg3_readphy(tp, MII_BMCR, &bmcr) &&
10482 ((bmcr & BMCR_ANENABLE) ||
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010483 (tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT))) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010484 tg3_writephy(tp, MII_BMCR, bmcr | BMCR_ANRESTART |
10485 BMCR_ANENABLE);
10486 r = 0;
10487 }
10488 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010489 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010490
Linus Torvalds1da177e2005-04-16 15:20:36 -070010491 return r;
10492}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010493
Linus Torvalds1da177e2005-04-16 15:20:36 -070010494static void tg3_get_ringparam(struct net_device *dev, struct ethtool_ringparam *ering)
10495{
10496 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010497
Matt Carlson2c49a442010-09-30 10:34:35 +000010498 ering->rx_max_pending = tp->rx_std_ring_mask;
Joe Perches63c3a662011-04-26 08:12:10 +000010499 if (tg3_flag(tp, JUMBO_RING_ENABLE))
Matt Carlson2c49a442010-09-30 10:34:35 +000010500 ering->rx_jumbo_max_pending = tp->rx_jmb_ring_mask;
Michael Chan4f81c322006-03-20 21:33:42 -080010501 else
10502 ering->rx_jumbo_max_pending = 0;
10503
10504 ering->tx_max_pending = TG3_TX_RING_SIZE - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010505
10506 ering->rx_pending = tp->rx_pending;
Joe Perches63c3a662011-04-26 08:12:10 +000010507 if (tg3_flag(tp, JUMBO_RING_ENABLE))
Michael Chan4f81c322006-03-20 21:33:42 -080010508 ering->rx_jumbo_pending = tp->rx_jumbo_pending;
10509 else
10510 ering->rx_jumbo_pending = 0;
10511
Matt Carlsonf3f3f272009-08-28 14:03:21 +000010512 ering->tx_pending = tp->napi[0].tx_pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010513}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010514
Linus Torvalds1da177e2005-04-16 15:20:36 -070010515static int tg3_set_ringparam(struct net_device *dev, struct ethtool_ringparam *ering)
10516{
10517 struct tg3 *tp = netdev_priv(dev);
Matt Carlson646c9ed2009-09-01 12:58:41 +000010518 int i, irq_sync = 0, err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010519
Matt Carlson2c49a442010-09-30 10:34:35 +000010520 if ((ering->rx_pending > tp->rx_std_ring_mask) ||
10521 (ering->rx_jumbo_pending > tp->rx_jmb_ring_mask) ||
Michael Chanbc3a9252006-10-18 20:55:18 -070010522 (ering->tx_pending > TG3_TX_RING_SIZE - 1) ||
10523 (ering->tx_pending <= MAX_SKB_FRAGS) ||
Joe Perches63c3a662011-04-26 08:12:10 +000010524 (tg3_flag(tp, TSO_BUG) &&
Michael Chanbc3a9252006-10-18 20:55:18 -070010525 (ering->tx_pending <= (MAX_SKB_FRAGS * 3))))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010526 return -EINVAL;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010527
Michael Chanbbe832c2005-06-24 20:20:04 -070010528 if (netif_running(dev)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010529 tg3_phy_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010530 tg3_netif_stop(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -070010531 irq_sync = 1;
10532 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010533
Michael Chanbbe832c2005-06-24 20:20:04 -070010534 tg3_full_lock(tp, irq_sync);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010535
Linus Torvalds1da177e2005-04-16 15:20:36 -070010536 tp->rx_pending = ering->rx_pending;
10537
Joe Perches63c3a662011-04-26 08:12:10 +000010538 if (tg3_flag(tp, MAX_RXPEND_64) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070010539 tp->rx_pending > 63)
10540 tp->rx_pending = 63;
10541 tp->rx_jumbo_pending = ering->rx_jumbo_pending;
Matt Carlson646c9ed2009-09-01 12:58:41 +000010542
Matt Carlson6fd45cb2010-09-15 08:59:57 +000010543 for (i = 0; i < tp->irq_max; i++)
Matt Carlson646c9ed2009-09-01 12:58:41 +000010544 tp->napi[i].tx_pending = ering->tx_pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010545
10546 if (netif_running(dev)) {
Michael Chan944d9802005-05-29 14:57:48 -070010547 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Michael Chanb9ec6c12006-07-25 16:37:27 -070010548 err = tg3_restart_hw(tp, 1);
10549 if (!err)
10550 tg3_netif_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010551 }
10552
David S. Millerf47c11e2005-06-24 20:18:35 -070010553 tg3_full_unlock(tp);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010554
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010555 if (irq_sync && !err)
10556 tg3_phy_start(tp);
10557
Michael Chanb9ec6c12006-07-25 16:37:27 -070010558 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010559}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010560
Linus Torvalds1da177e2005-04-16 15:20:36 -070010561static void tg3_get_pauseparam(struct net_device *dev, struct ethtool_pauseparam *epause)
10562{
10563 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010564
Joe Perches63c3a662011-04-26 08:12:10 +000010565 epause->autoneg = !!tg3_flag(tp, PAUSE_AUTONEG);
Matt Carlson8d018622007-12-20 20:05:44 -080010566
Steve Glendinninge18ce342008-12-16 02:00:00 -080010567 if (tp->link_config.active_flowctrl & FLOW_CTRL_RX)
Matt Carlson8d018622007-12-20 20:05:44 -080010568 epause->rx_pause = 1;
10569 else
10570 epause->rx_pause = 0;
10571
Steve Glendinninge18ce342008-12-16 02:00:00 -080010572 if (tp->link_config.active_flowctrl & FLOW_CTRL_TX)
Matt Carlson8d018622007-12-20 20:05:44 -080010573 epause->tx_pause = 1;
10574 else
10575 epause->tx_pause = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010576}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010577
Linus Torvalds1da177e2005-04-16 15:20:36 -070010578static int tg3_set_pauseparam(struct net_device *dev, struct ethtool_pauseparam *epause)
10579{
10580 struct tg3 *tp = netdev_priv(dev);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010581 int err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010582
Joe Perches63c3a662011-04-26 08:12:10 +000010583 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson27121682010-02-17 15:16:57 +000010584 u32 newadv;
10585 struct phy_device *phydev;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010586
Matt Carlson27121682010-02-17 15:16:57 +000010587 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010588
Matt Carlson27121682010-02-17 15:16:57 +000010589 if (!(phydev->supported & SUPPORTED_Pause) ||
10590 (!(phydev->supported & SUPPORTED_Asym_Pause) &&
Nicolas Kaiser2259dca2010-10-07 23:29:27 +000010591 (epause->rx_pause != epause->tx_pause)))
Matt Carlson27121682010-02-17 15:16:57 +000010592 return -EINVAL;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010593
Matt Carlson27121682010-02-17 15:16:57 +000010594 tp->link_config.flowctrl = 0;
10595 if (epause->rx_pause) {
10596 tp->link_config.flowctrl |= FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010597
Matt Carlson27121682010-02-17 15:16:57 +000010598 if (epause->tx_pause) {
Steve Glendinninge18ce342008-12-16 02:00:00 -080010599 tp->link_config.flowctrl |= FLOW_CTRL_TX;
Matt Carlson27121682010-02-17 15:16:57 +000010600 newadv = ADVERTISED_Pause;
10601 } else
10602 newadv = ADVERTISED_Pause |
10603 ADVERTISED_Asym_Pause;
10604 } else if (epause->tx_pause) {
10605 tp->link_config.flowctrl |= FLOW_CTRL_TX;
10606 newadv = ADVERTISED_Asym_Pause;
10607 } else
10608 newadv = 0;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010609
Matt Carlson27121682010-02-17 15:16:57 +000010610 if (epause->autoneg)
Joe Perches63c3a662011-04-26 08:12:10 +000010611 tg3_flag_set(tp, PAUSE_AUTONEG);
Matt Carlson27121682010-02-17 15:16:57 +000010612 else
Joe Perches63c3a662011-04-26 08:12:10 +000010613 tg3_flag_clear(tp, PAUSE_AUTONEG);
Matt Carlson27121682010-02-17 15:16:57 +000010614
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010615 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) {
Matt Carlson27121682010-02-17 15:16:57 +000010616 u32 oldadv = phydev->advertising &
10617 (ADVERTISED_Pause | ADVERTISED_Asym_Pause);
10618 if (oldadv != newadv) {
10619 phydev->advertising &=
10620 ~(ADVERTISED_Pause |
10621 ADVERTISED_Asym_Pause);
10622 phydev->advertising |= newadv;
10623 if (phydev->autoneg) {
10624 /*
10625 * Always renegotiate the link to
10626 * inform our link partner of our
10627 * flow control settings, even if the
10628 * flow control is forced. Let
10629 * tg3_adjust_link() do the final
10630 * flow control setup.
10631 */
10632 return phy_start_aneg(phydev);
10633 }
10634 }
10635
10636 if (!epause->autoneg)
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010637 tg3_setup_flow_control(tp, 0, 0);
Matt Carlson27121682010-02-17 15:16:57 +000010638 } else {
10639 tp->link_config.orig_advertising &=
10640 ~(ADVERTISED_Pause |
10641 ADVERTISED_Asym_Pause);
10642 tp->link_config.orig_advertising |= newadv;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010643 }
10644 } else {
10645 int irq_sync = 0;
10646
10647 if (netif_running(dev)) {
10648 tg3_netif_stop(tp);
10649 irq_sync = 1;
10650 }
10651
10652 tg3_full_lock(tp, irq_sync);
10653
10654 if (epause->autoneg)
Joe Perches63c3a662011-04-26 08:12:10 +000010655 tg3_flag_set(tp, PAUSE_AUTONEG);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010656 else
Joe Perches63c3a662011-04-26 08:12:10 +000010657 tg3_flag_clear(tp, PAUSE_AUTONEG);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010658 if (epause->rx_pause)
Steve Glendinninge18ce342008-12-16 02:00:00 -080010659 tp->link_config.flowctrl |= FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010660 else
Steve Glendinninge18ce342008-12-16 02:00:00 -080010661 tp->link_config.flowctrl &= ~FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010662 if (epause->tx_pause)
Steve Glendinninge18ce342008-12-16 02:00:00 -080010663 tp->link_config.flowctrl |= FLOW_CTRL_TX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010664 else
Steve Glendinninge18ce342008-12-16 02:00:00 -080010665 tp->link_config.flowctrl &= ~FLOW_CTRL_TX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010666
10667 if (netif_running(dev)) {
10668 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
10669 err = tg3_restart_hw(tp, 1);
10670 if (!err)
10671 tg3_netif_start(tp);
10672 }
10673
10674 tg3_full_unlock(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -070010675 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010676
Michael Chanb9ec6c12006-07-25 16:37:27 -070010677 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010678}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010679
Matt Carlsonde6f31e2010-04-12 06:58:30 +000010680static int tg3_get_sset_count(struct net_device *dev, int sset)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010681{
Jeff Garzikb9f2c042007-10-03 18:07:32 -070010682 switch (sset) {
10683 case ETH_SS_TEST:
10684 return TG3_NUM_TEST;
10685 case ETH_SS_STATS:
10686 return TG3_NUM_STATS;
10687 default:
10688 return -EOPNOTSUPP;
10689 }
Michael Chan4cafd3f2005-05-29 14:56:34 -070010690}
10691
Matt Carlsonde6f31e2010-04-12 06:58:30 +000010692static void tg3_get_strings(struct net_device *dev, u32 stringset, u8 *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010693{
10694 switch (stringset) {
10695 case ETH_SS_STATS:
10696 memcpy(buf, &ethtool_stats_keys, sizeof(ethtool_stats_keys));
10697 break;
Michael Chan4cafd3f2005-05-29 14:56:34 -070010698 case ETH_SS_TEST:
10699 memcpy(buf, &ethtool_test_keys, sizeof(ethtool_test_keys));
10700 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010701 default:
10702 WARN_ON(1); /* we need a WARN() */
10703 break;
10704 }
10705}
10706
stephen hemminger81b87092011-04-04 08:43:50 +000010707static int tg3_set_phys_id(struct net_device *dev,
10708 enum ethtool_phys_id_state state)
Michael Chan4009a932005-09-05 17:52:54 -070010709{
10710 struct tg3 *tp = netdev_priv(dev);
Michael Chan4009a932005-09-05 17:52:54 -070010711
10712 if (!netif_running(tp->dev))
10713 return -EAGAIN;
10714
stephen hemminger81b87092011-04-04 08:43:50 +000010715 switch (state) {
10716 case ETHTOOL_ID_ACTIVE:
Allan, Bruce Wfce55922011-04-13 13:09:10 +000010717 return 1; /* cycle on/off once per second */
Michael Chan4009a932005-09-05 17:52:54 -070010718
stephen hemminger81b87092011-04-04 08:43:50 +000010719 case ETHTOOL_ID_ON:
10720 tw32(MAC_LED_CTRL, LED_CTRL_LNKLED_OVERRIDE |
10721 LED_CTRL_1000MBPS_ON |
10722 LED_CTRL_100MBPS_ON |
10723 LED_CTRL_10MBPS_ON |
10724 LED_CTRL_TRAFFIC_OVERRIDE |
10725 LED_CTRL_TRAFFIC_BLINK |
10726 LED_CTRL_TRAFFIC_LED);
10727 break;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010728
stephen hemminger81b87092011-04-04 08:43:50 +000010729 case ETHTOOL_ID_OFF:
10730 tw32(MAC_LED_CTRL, LED_CTRL_LNKLED_OVERRIDE |
10731 LED_CTRL_TRAFFIC_OVERRIDE);
10732 break;
Michael Chan4009a932005-09-05 17:52:54 -070010733
stephen hemminger81b87092011-04-04 08:43:50 +000010734 case ETHTOOL_ID_INACTIVE:
10735 tw32(MAC_LED_CTRL, tp->led_ctrl);
10736 break;
Michael Chan4009a932005-09-05 17:52:54 -070010737 }
stephen hemminger81b87092011-04-04 08:43:50 +000010738
Michael Chan4009a932005-09-05 17:52:54 -070010739 return 0;
10740}
10741
Matt Carlsonde6f31e2010-04-12 06:58:30 +000010742static void tg3_get_ethtool_stats(struct net_device *dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010743 struct ethtool_stats *estats, u64 *tmp_stats)
10744{
10745 struct tg3 *tp = netdev_priv(dev);
10746 memcpy(tmp_stats, tg3_get_estats(tp), sizeof(tp->estats));
10747}
10748
Matt Carlson535a4902011-07-20 10:20:56 +000010749static __be32 *tg3_vpd_readblock(struct tg3 *tp, u32 *vpdlen)
Matt Carlsonc3e94502011-04-13 11:05:08 +000010750{
10751 int i;
10752 __be32 *buf;
10753 u32 offset = 0, len = 0;
10754 u32 magic, val;
10755
Joe Perches63c3a662011-04-26 08:12:10 +000010756 if (tg3_flag(tp, NO_NVRAM) || tg3_nvram_read(tp, 0, &magic))
Matt Carlsonc3e94502011-04-13 11:05:08 +000010757 return NULL;
10758
10759 if (magic == TG3_EEPROM_MAGIC) {
10760 for (offset = TG3_NVM_DIR_START;
10761 offset < TG3_NVM_DIR_END;
10762 offset += TG3_NVM_DIRENT_SIZE) {
10763 if (tg3_nvram_read(tp, offset, &val))
10764 return NULL;
10765
10766 if ((val >> TG3_NVM_DIRTYPE_SHIFT) ==
10767 TG3_NVM_DIRTYPE_EXTVPD)
10768 break;
10769 }
10770
10771 if (offset != TG3_NVM_DIR_END) {
10772 len = (val & TG3_NVM_DIRTYPE_LENMSK) * 4;
10773 if (tg3_nvram_read(tp, offset + 4, &offset))
10774 return NULL;
10775
10776 offset = tg3_nvram_logical_addr(tp, offset);
10777 }
10778 }
10779
10780 if (!offset || !len) {
10781 offset = TG3_NVM_VPD_OFF;
10782 len = TG3_NVM_VPD_LEN;
10783 }
10784
10785 buf = kmalloc(len, GFP_KERNEL);
10786 if (buf == NULL)
10787 return NULL;
10788
10789 if (magic == TG3_EEPROM_MAGIC) {
10790 for (i = 0; i < len; i += 4) {
10791 /* The data is in little-endian format in NVRAM.
10792 * Use the big-endian read routines to preserve
10793 * the byte order as it exists in NVRAM.
10794 */
10795 if (tg3_nvram_read_be32(tp, offset + i, &buf[i/4]))
10796 goto error;
10797 }
10798 } else {
10799 u8 *ptr;
10800 ssize_t cnt;
10801 unsigned int pos = 0;
10802
10803 ptr = (u8 *)&buf[0];
10804 for (i = 0; pos < len && i < 3; i++, pos += cnt, ptr += cnt) {
10805 cnt = pci_read_vpd(tp->pdev, pos,
10806 len - pos, ptr);
10807 if (cnt == -ETIMEDOUT || cnt == -EINTR)
10808 cnt = 0;
10809 else if (cnt < 0)
10810 goto error;
10811 }
10812 if (pos != len)
10813 goto error;
10814 }
10815
Matt Carlson535a4902011-07-20 10:20:56 +000010816 *vpdlen = len;
10817
Matt Carlsonc3e94502011-04-13 11:05:08 +000010818 return buf;
10819
10820error:
10821 kfree(buf);
10822 return NULL;
10823}
10824
Michael Chan566f86a2005-05-29 14:56:58 -070010825#define NVRAM_TEST_SIZE 0x100
Matt Carlsona5767de2007-11-12 21:10:58 -080010826#define NVRAM_SELFBOOT_FORMAT1_0_SIZE 0x14
10827#define NVRAM_SELFBOOT_FORMAT1_2_SIZE 0x18
10828#define NVRAM_SELFBOOT_FORMAT1_3_SIZE 0x1c
Matt Carlson727a6d92011-06-13 13:38:58 +000010829#define NVRAM_SELFBOOT_FORMAT1_4_SIZE 0x20
10830#define NVRAM_SELFBOOT_FORMAT1_5_SIZE 0x24
Matt Carlsonbda18fa2011-07-20 10:20:57 +000010831#define NVRAM_SELFBOOT_FORMAT1_6_SIZE 0x50
Michael Chanb16250e2006-09-27 16:10:14 -070010832#define NVRAM_SELFBOOT_HW_SIZE 0x20
10833#define NVRAM_SELFBOOT_DATA_SIZE 0x1c
Michael Chan566f86a2005-05-29 14:56:58 -070010834
10835static int tg3_test_nvram(struct tg3 *tp)
10836{
Matt Carlson535a4902011-07-20 10:20:56 +000010837 u32 csum, magic, len;
Matt Carlsona9dc5292009-02-25 14:25:30 +000010838 __be32 *buf;
Andy Gospodarekab0049b2007-09-06 20:42:14 +010010839 int i, j, k, err = 0, size;
Michael Chan566f86a2005-05-29 14:56:58 -070010840
Joe Perches63c3a662011-04-26 08:12:10 +000010841 if (tg3_flag(tp, NO_NVRAM))
Matt Carlsondf259d82009-04-20 06:57:14 +000010842 return 0;
10843
Matt Carlsone4f34112009-02-25 14:25:00 +000010844 if (tg3_nvram_read(tp, 0, &magic) != 0)
Michael Chan1b277772006-03-20 22:27:48 -080010845 return -EIO;
10846
Michael Chan1b277772006-03-20 22:27:48 -080010847 if (magic == TG3_EEPROM_MAGIC)
10848 size = NVRAM_TEST_SIZE;
Michael Chanb16250e2006-09-27 16:10:14 -070010849 else if ((magic & TG3_EEPROM_MAGIC_FW_MSK) == TG3_EEPROM_MAGIC_FW) {
Matt Carlsona5767de2007-11-12 21:10:58 -080010850 if ((magic & TG3_EEPROM_SB_FORMAT_MASK) ==
10851 TG3_EEPROM_SB_FORMAT_1) {
10852 switch (magic & TG3_EEPROM_SB_REVISION_MASK) {
10853 case TG3_EEPROM_SB_REVISION_0:
10854 size = NVRAM_SELFBOOT_FORMAT1_0_SIZE;
10855 break;
10856 case TG3_EEPROM_SB_REVISION_2:
10857 size = NVRAM_SELFBOOT_FORMAT1_2_SIZE;
10858 break;
10859 case TG3_EEPROM_SB_REVISION_3:
10860 size = NVRAM_SELFBOOT_FORMAT1_3_SIZE;
10861 break;
Matt Carlson727a6d92011-06-13 13:38:58 +000010862 case TG3_EEPROM_SB_REVISION_4:
10863 size = NVRAM_SELFBOOT_FORMAT1_4_SIZE;
10864 break;
10865 case TG3_EEPROM_SB_REVISION_5:
10866 size = NVRAM_SELFBOOT_FORMAT1_5_SIZE;
10867 break;
10868 case TG3_EEPROM_SB_REVISION_6:
10869 size = NVRAM_SELFBOOT_FORMAT1_6_SIZE;
10870 break;
Matt Carlsona5767de2007-11-12 21:10:58 -080010871 default:
Matt Carlson727a6d92011-06-13 13:38:58 +000010872 return -EIO;
Matt Carlsona5767de2007-11-12 21:10:58 -080010873 }
10874 } else
Michael Chan1b277772006-03-20 22:27:48 -080010875 return 0;
Michael Chanb16250e2006-09-27 16:10:14 -070010876 } else if ((magic & TG3_EEPROM_MAGIC_HW_MSK) == TG3_EEPROM_MAGIC_HW)
10877 size = NVRAM_SELFBOOT_HW_SIZE;
10878 else
Michael Chan1b277772006-03-20 22:27:48 -080010879 return -EIO;
10880
10881 buf = kmalloc(size, GFP_KERNEL);
Michael Chan566f86a2005-05-29 14:56:58 -070010882 if (buf == NULL)
10883 return -ENOMEM;
10884
Michael Chan1b277772006-03-20 22:27:48 -080010885 err = -EIO;
10886 for (i = 0, j = 0; i < size; i += 4, j++) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000010887 err = tg3_nvram_read_be32(tp, i, &buf[j]);
10888 if (err)
Michael Chan566f86a2005-05-29 14:56:58 -070010889 break;
Michael Chan566f86a2005-05-29 14:56:58 -070010890 }
Michael Chan1b277772006-03-20 22:27:48 -080010891 if (i < size)
Michael Chan566f86a2005-05-29 14:56:58 -070010892 goto out;
10893
Michael Chan1b277772006-03-20 22:27:48 -080010894 /* Selfboot format */
Matt Carlsona9dc5292009-02-25 14:25:30 +000010895 magic = be32_to_cpu(buf[0]);
Al Virob9fc7dc2007-12-17 22:59:57 -080010896 if ((magic & TG3_EEPROM_MAGIC_FW_MSK) ==
Michael Chanb16250e2006-09-27 16:10:14 -070010897 TG3_EEPROM_MAGIC_FW) {
Michael Chan1b277772006-03-20 22:27:48 -080010898 u8 *buf8 = (u8 *) buf, csum8 = 0;
10899
Al Virob9fc7dc2007-12-17 22:59:57 -080010900 if ((magic & TG3_EEPROM_SB_REVISION_MASK) ==
Matt Carlsona5767de2007-11-12 21:10:58 -080010901 TG3_EEPROM_SB_REVISION_2) {
10902 /* For rev 2, the csum doesn't include the MBA. */
10903 for (i = 0; i < TG3_EEPROM_SB_F1R2_MBA_OFF; i++)
10904 csum8 += buf8[i];
10905 for (i = TG3_EEPROM_SB_F1R2_MBA_OFF + 4; i < size; i++)
10906 csum8 += buf8[i];
10907 } else {
10908 for (i = 0; i < size; i++)
10909 csum8 += buf8[i];
10910 }
Michael Chan1b277772006-03-20 22:27:48 -080010911
Adrian Bunkad96b482006-04-05 22:21:04 -070010912 if (csum8 == 0) {
10913 err = 0;
10914 goto out;
10915 }
10916
10917 err = -EIO;
10918 goto out;
Michael Chan1b277772006-03-20 22:27:48 -080010919 }
Michael Chan566f86a2005-05-29 14:56:58 -070010920
Al Virob9fc7dc2007-12-17 22:59:57 -080010921 if ((magic & TG3_EEPROM_MAGIC_HW_MSK) ==
Michael Chanb16250e2006-09-27 16:10:14 -070010922 TG3_EEPROM_MAGIC_HW) {
10923 u8 data[NVRAM_SELFBOOT_DATA_SIZE];
Matt Carlsona9dc5292009-02-25 14:25:30 +000010924 u8 parity[NVRAM_SELFBOOT_DATA_SIZE];
Michael Chanb16250e2006-09-27 16:10:14 -070010925 u8 *buf8 = (u8 *) buf;
Michael Chanb16250e2006-09-27 16:10:14 -070010926
10927 /* Separate the parity bits and the data bytes. */
10928 for (i = 0, j = 0, k = 0; i < NVRAM_SELFBOOT_HW_SIZE; i++) {
10929 if ((i == 0) || (i == 8)) {
10930 int l;
10931 u8 msk;
10932
10933 for (l = 0, msk = 0x80; l < 7; l++, msk >>= 1)
10934 parity[k++] = buf8[i] & msk;
10935 i++;
Matt Carlson859a5882010-04-05 10:19:28 +000010936 } else if (i == 16) {
Michael Chanb16250e2006-09-27 16:10:14 -070010937 int l;
10938 u8 msk;
10939
10940 for (l = 0, msk = 0x20; l < 6; l++, msk >>= 1)
10941 parity[k++] = buf8[i] & msk;
10942 i++;
10943
10944 for (l = 0, msk = 0x80; l < 8; l++, msk >>= 1)
10945 parity[k++] = buf8[i] & msk;
10946 i++;
10947 }
10948 data[j++] = buf8[i];
10949 }
10950
10951 err = -EIO;
10952 for (i = 0; i < NVRAM_SELFBOOT_DATA_SIZE; i++) {
10953 u8 hw8 = hweight8(data[i]);
10954
10955 if ((hw8 & 0x1) && parity[i])
10956 goto out;
10957 else if (!(hw8 & 0x1) && !parity[i])
10958 goto out;
10959 }
10960 err = 0;
10961 goto out;
10962 }
10963
Matt Carlson01c3a392011-03-09 16:58:20 +000010964 err = -EIO;
10965
Michael Chan566f86a2005-05-29 14:56:58 -070010966 /* Bootstrap checksum at offset 0x10 */
10967 csum = calc_crc((unsigned char *) buf, 0x10);
Matt Carlson01c3a392011-03-09 16:58:20 +000010968 if (csum != le32_to_cpu(buf[0x10/4]))
Michael Chan566f86a2005-05-29 14:56:58 -070010969 goto out;
10970
10971 /* Manufacturing block starts at offset 0x74, checksum at 0xfc */
10972 csum = calc_crc((unsigned char *) &buf[0x74/4], 0x88);
Matt Carlson01c3a392011-03-09 16:58:20 +000010973 if (csum != le32_to_cpu(buf[0xfc/4]))
Matt Carlsona9dc5292009-02-25 14:25:30 +000010974 goto out;
Michael Chan566f86a2005-05-29 14:56:58 -070010975
Matt Carlsonc3e94502011-04-13 11:05:08 +000010976 kfree(buf);
10977
Matt Carlson535a4902011-07-20 10:20:56 +000010978 buf = tg3_vpd_readblock(tp, &len);
Matt Carlsonc3e94502011-04-13 11:05:08 +000010979 if (!buf)
10980 return -ENOMEM;
Matt Carlsond4894f32011-03-09 16:58:21 +000010981
Matt Carlson535a4902011-07-20 10:20:56 +000010982 i = pci_vpd_find_tag((u8 *)buf, 0, len, PCI_VPD_LRDT_RO_DATA);
Matt Carlsond4894f32011-03-09 16:58:21 +000010983 if (i > 0) {
10984 j = pci_vpd_lrdt_size(&((u8 *)buf)[i]);
10985 if (j < 0)
10986 goto out;
10987
Matt Carlson535a4902011-07-20 10:20:56 +000010988 if (i + PCI_VPD_LRDT_TAG_SIZE + j > len)
Matt Carlsond4894f32011-03-09 16:58:21 +000010989 goto out;
10990
10991 i += PCI_VPD_LRDT_TAG_SIZE;
10992 j = pci_vpd_find_info_keyword((u8 *)buf, i, j,
10993 PCI_VPD_RO_KEYWORD_CHKSUM);
10994 if (j > 0) {
10995 u8 csum8 = 0;
10996
10997 j += PCI_VPD_INFO_FLD_HDR_SIZE;
10998
10999 for (i = 0; i <= j; i++)
11000 csum8 += ((u8 *)buf)[i];
11001
11002 if (csum8)
11003 goto out;
11004 }
11005 }
11006
Michael Chan566f86a2005-05-29 14:56:58 -070011007 err = 0;
11008
11009out:
11010 kfree(buf);
11011 return err;
11012}
11013
Michael Chanca430072005-05-29 14:57:23 -070011014#define TG3_SERDES_TIMEOUT_SEC 2
11015#define TG3_COPPER_TIMEOUT_SEC 6
11016
11017static int tg3_test_link(struct tg3 *tp)
11018{
11019 int i, max;
11020
11021 if (!netif_running(tp->dev))
11022 return -ENODEV;
11023
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011024 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
Michael Chanca430072005-05-29 14:57:23 -070011025 max = TG3_SERDES_TIMEOUT_SEC;
11026 else
11027 max = TG3_COPPER_TIMEOUT_SEC;
11028
11029 for (i = 0; i < max; i++) {
11030 if (netif_carrier_ok(tp->dev))
11031 return 0;
11032
11033 if (msleep_interruptible(1000))
11034 break;
11035 }
11036
11037 return -EIO;
11038}
11039
Michael Chana71116d2005-05-29 14:58:11 -070011040/* Only test the commonly used registers */
David S. Miller30ca3e32006-03-20 23:02:36 -080011041static int tg3_test_registers(struct tg3 *tp)
Michael Chana71116d2005-05-29 14:58:11 -070011042{
Michael Chanb16250e2006-09-27 16:10:14 -070011043 int i, is_5705, is_5750;
Michael Chana71116d2005-05-29 14:58:11 -070011044 u32 offset, read_mask, write_mask, val, save_val, read_val;
11045 static struct {
11046 u16 offset;
11047 u16 flags;
11048#define TG3_FL_5705 0x1
11049#define TG3_FL_NOT_5705 0x2
11050#define TG3_FL_NOT_5788 0x4
Michael Chanb16250e2006-09-27 16:10:14 -070011051#define TG3_FL_NOT_5750 0x8
Michael Chana71116d2005-05-29 14:58:11 -070011052 u32 read_mask;
11053 u32 write_mask;
11054 } reg_tbl[] = {
11055 /* MAC Control Registers */
11056 { MAC_MODE, TG3_FL_NOT_5705,
11057 0x00000000, 0x00ef6f8c },
11058 { MAC_MODE, TG3_FL_5705,
11059 0x00000000, 0x01ef6b8c },
11060 { MAC_STATUS, TG3_FL_NOT_5705,
11061 0x03800107, 0x00000000 },
11062 { MAC_STATUS, TG3_FL_5705,
11063 0x03800100, 0x00000000 },
11064 { MAC_ADDR_0_HIGH, 0x0000,
11065 0x00000000, 0x0000ffff },
11066 { MAC_ADDR_0_LOW, 0x0000,
Matt Carlsonc6cdf432010-04-05 10:19:26 +000011067 0x00000000, 0xffffffff },
Michael Chana71116d2005-05-29 14:58:11 -070011068 { MAC_RX_MTU_SIZE, 0x0000,
11069 0x00000000, 0x0000ffff },
11070 { MAC_TX_MODE, 0x0000,
11071 0x00000000, 0x00000070 },
11072 { MAC_TX_LENGTHS, 0x0000,
11073 0x00000000, 0x00003fff },
11074 { MAC_RX_MODE, TG3_FL_NOT_5705,
11075 0x00000000, 0x000007fc },
11076 { MAC_RX_MODE, TG3_FL_5705,
11077 0x00000000, 0x000007dc },
11078 { MAC_HASH_REG_0, 0x0000,
11079 0x00000000, 0xffffffff },
11080 { MAC_HASH_REG_1, 0x0000,
11081 0x00000000, 0xffffffff },
11082 { MAC_HASH_REG_2, 0x0000,
11083 0x00000000, 0xffffffff },
11084 { MAC_HASH_REG_3, 0x0000,
11085 0x00000000, 0xffffffff },
11086
11087 /* Receive Data and Receive BD Initiator Control Registers. */
11088 { RCVDBDI_JUMBO_BD+0, TG3_FL_NOT_5705,
11089 0x00000000, 0xffffffff },
11090 { RCVDBDI_JUMBO_BD+4, TG3_FL_NOT_5705,
11091 0x00000000, 0xffffffff },
11092 { RCVDBDI_JUMBO_BD+8, TG3_FL_NOT_5705,
11093 0x00000000, 0x00000003 },
11094 { RCVDBDI_JUMBO_BD+0xc, TG3_FL_NOT_5705,
11095 0x00000000, 0xffffffff },
11096 { RCVDBDI_STD_BD+0, 0x0000,
11097 0x00000000, 0xffffffff },
11098 { RCVDBDI_STD_BD+4, 0x0000,
11099 0x00000000, 0xffffffff },
11100 { RCVDBDI_STD_BD+8, 0x0000,
11101 0x00000000, 0xffff0002 },
11102 { RCVDBDI_STD_BD+0xc, 0x0000,
11103 0x00000000, 0xffffffff },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011104
Michael Chana71116d2005-05-29 14:58:11 -070011105 /* Receive BD Initiator Control Registers. */
11106 { RCVBDI_STD_THRESH, TG3_FL_NOT_5705,
11107 0x00000000, 0xffffffff },
11108 { RCVBDI_STD_THRESH, TG3_FL_5705,
11109 0x00000000, 0x000003ff },
11110 { RCVBDI_JUMBO_THRESH, TG3_FL_NOT_5705,
11111 0x00000000, 0xffffffff },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011112
Michael Chana71116d2005-05-29 14:58:11 -070011113 /* Host Coalescing Control Registers. */
11114 { HOSTCC_MODE, TG3_FL_NOT_5705,
11115 0x00000000, 0x00000004 },
11116 { HOSTCC_MODE, TG3_FL_5705,
11117 0x00000000, 0x000000f6 },
11118 { HOSTCC_RXCOL_TICKS, TG3_FL_NOT_5705,
11119 0x00000000, 0xffffffff },
11120 { HOSTCC_RXCOL_TICKS, TG3_FL_5705,
11121 0x00000000, 0x000003ff },
11122 { HOSTCC_TXCOL_TICKS, TG3_FL_NOT_5705,
11123 0x00000000, 0xffffffff },
11124 { HOSTCC_TXCOL_TICKS, TG3_FL_5705,
11125 0x00000000, 0x000003ff },
11126 { HOSTCC_RXMAX_FRAMES, TG3_FL_NOT_5705,
11127 0x00000000, 0xffffffff },
11128 { HOSTCC_RXMAX_FRAMES, TG3_FL_5705 | TG3_FL_NOT_5788,
11129 0x00000000, 0x000000ff },
11130 { HOSTCC_TXMAX_FRAMES, TG3_FL_NOT_5705,
11131 0x00000000, 0xffffffff },
11132 { HOSTCC_TXMAX_FRAMES, TG3_FL_5705 | TG3_FL_NOT_5788,
11133 0x00000000, 0x000000ff },
11134 { HOSTCC_RXCOAL_TICK_INT, TG3_FL_NOT_5705,
11135 0x00000000, 0xffffffff },
11136 { HOSTCC_TXCOAL_TICK_INT, TG3_FL_NOT_5705,
11137 0x00000000, 0xffffffff },
11138 { HOSTCC_RXCOAL_MAXF_INT, TG3_FL_NOT_5705,
11139 0x00000000, 0xffffffff },
11140 { HOSTCC_RXCOAL_MAXF_INT, TG3_FL_5705 | TG3_FL_NOT_5788,
11141 0x00000000, 0x000000ff },
11142 { HOSTCC_TXCOAL_MAXF_INT, TG3_FL_NOT_5705,
11143 0x00000000, 0xffffffff },
11144 { HOSTCC_TXCOAL_MAXF_INT, TG3_FL_5705 | TG3_FL_NOT_5788,
11145 0x00000000, 0x000000ff },
11146 { HOSTCC_STAT_COAL_TICKS, TG3_FL_NOT_5705,
11147 0x00000000, 0xffffffff },
11148 { HOSTCC_STATS_BLK_HOST_ADDR, TG3_FL_NOT_5705,
11149 0x00000000, 0xffffffff },
11150 { HOSTCC_STATS_BLK_HOST_ADDR+4, TG3_FL_NOT_5705,
11151 0x00000000, 0xffffffff },
11152 { HOSTCC_STATUS_BLK_HOST_ADDR, 0x0000,
11153 0x00000000, 0xffffffff },
11154 { HOSTCC_STATUS_BLK_HOST_ADDR+4, 0x0000,
11155 0x00000000, 0xffffffff },
11156 { HOSTCC_STATS_BLK_NIC_ADDR, 0x0000,
11157 0xffffffff, 0x00000000 },
11158 { HOSTCC_STATUS_BLK_NIC_ADDR, 0x0000,
11159 0xffffffff, 0x00000000 },
11160
11161 /* Buffer Manager Control Registers. */
Michael Chanb16250e2006-09-27 16:10:14 -070011162 { BUFMGR_MB_POOL_ADDR, TG3_FL_NOT_5750,
Michael Chana71116d2005-05-29 14:58:11 -070011163 0x00000000, 0x007fff80 },
Michael Chanb16250e2006-09-27 16:10:14 -070011164 { BUFMGR_MB_POOL_SIZE, TG3_FL_NOT_5750,
Michael Chana71116d2005-05-29 14:58:11 -070011165 0x00000000, 0x007fffff },
11166 { BUFMGR_MB_RDMA_LOW_WATER, 0x0000,
11167 0x00000000, 0x0000003f },
11168 { BUFMGR_MB_MACRX_LOW_WATER, 0x0000,
11169 0x00000000, 0x000001ff },
11170 { BUFMGR_MB_HIGH_WATER, 0x0000,
11171 0x00000000, 0x000001ff },
11172 { BUFMGR_DMA_DESC_POOL_ADDR, TG3_FL_NOT_5705,
11173 0xffffffff, 0x00000000 },
11174 { BUFMGR_DMA_DESC_POOL_SIZE, TG3_FL_NOT_5705,
11175 0xffffffff, 0x00000000 },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011176
Michael Chana71116d2005-05-29 14:58:11 -070011177 /* Mailbox Registers */
11178 { GRCMBOX_RCVSTD_PROD_IDX+4, 0x0000,
11179 0x00000000, 0x000001ff },
11180 { GRCMBOX_RCVJUMBO_PROD_IDX+4, TG3_FL_NOT_5705,
11181 0x00000000, 0x000001ff },
11182 { GRCMBOX_RCVRET_CON_IDX_0+4, 0x0000,
11183 0x00000000, 0x000007ff },
11184 { GRCMBOX_SNDHOST_PROD_IDX_0+4, 0x0000,
11185 0x00000000, 0x000001ff },
11186
11187 { 0xffff, 0x0000, 0x00000000, 0x00000000 },
11188 };
11189
Michael Chanb16250e2006-09-27 16:10:14 -070011190 is_5705 = is_5750 = 0;
Joe Perches63c3a662011-04-26 08:12:10 +000011191 if (tg3_flag(tp, 5705_PLUS)) {
Michael Chana71116d2005-05-29 14:58:11 -070011192 is_5705 = 1;
Joe Perches63c3a662011-04-26 08:12:10 +000011193 if (tg3_flag(tp, 5750_PLUS))
Michael Chanb16250e2006-09-27 16:10:14 -070011194 is_5750 = 1;
11195 }
Michael Chana71116d2005-05-29 14:58:11 -070011196
11197 for (i = 0; reg_tbl[i].offset != 0xffff; i++) {
11198 if (is_5705 && (reg_tbl[i].flags & TG3_FL_NOT_5705))
11199 continue;
11200
11201 if (!is_5705 && (reg_tbl[i].flags & TG3_FL_5705))
11202 continue;
11203
Joe Perches63c3a662011-04-26 08:12:10 +000011204 if (tg3_flag(tp, IS_5788) &&
Michael Chana71116d2005-05-29 14:58:11 -070011205 (reg_tbl[i].flags & TG3_FL_NOT_5788))
11206 continue;
11207
Michael Chanb16250e2006-09-27 16:10:14 -070011208 if (is_5750 && (reg_tbl[i].flags & TG3_FL_NOT_5750))
11209 continue;
11210
Michael Chana71116d2005-05-29 14:58:11 -070011211 offset = (u32) reg_tbl[i].offset;
11212 read_mask = reg_tbl[i].read_mask;
11213 write_mask = reg_tbl[i].write_mask;
11214
11215 /* Save the original register content */
11216 save_val = tr32(offset);
11217
11218 /* Determine the read-only value. */
11219 read_val = save_val & read_mask;
11220
11221 /* Write zero to the register, then make sure the read-only bits
11222 * are not changed and the read/write bits are all zeros.
11223 */
11224 tw32(offset, 0);
11225
11226 val = tr32(offset);
11227
11228 /* Test the read-only and read/write bits. */
11229 if (((val & read_mask) != read_val) || (val & write_mask))
11230 goto out;
11231
11232 /* Write ones to all the bits defined by RdMask and WrMask, then
11233 * make sure the read-only bits are not changed and the
11234 * read/write bits are all ones.
11235 */
11236 tw32(offset, read_mask | write_mask);
11237
11238 val = tr32(offset);
11239
11240 /* Test the read-only bits. */
11241 if ((val & read_mask) != read_val)
11242 goto out;
11243
11244 /* Test the read/write bits. */
11245 if ((val & write_mask) != write_mask)
11246 goto out;
11247
11248 tw32(offset, save_val);
11249 }
11250
11251 return 0;
11252
11253out:
Michael Chan9f88f292006-12-07 00:22:54 -080011254 if (netif_msg_hw(tp))
Matt Carlson2445e462010-04-05 10:19:21 +000011255 netdev_err(tp->dev,
11256 "Register test failed at offset %x\n", offset);
Michael Chana71116d2005-05-29 14:58:11 -070011257 tw32(offset, save_val);
11258 return -EIO;
11259}
11260
Michael Chan7942e1d2005-05-29 14:58:36 -070011261static int tg3_do_mem_test(struct tg3 *tp, u32 offset, u32 len)
11262{
Arjan van de Venf71e1302006-03-03 21:33:57 -050011263 static const u32 test_pattern[] = { 0x00000000, 0xffffffff, 0xaa55a55a };
Michael Chan7942e1d2005-05-29 14:58:36 -070011264 int i;
11265 u32 j;
11266
Alejandro Martinez Ruize9edda62007-10-15 03:37:43 +020011267 for (i = 0; i < ARRAY_SIZE(test_pattern); i++) {
Michael Chan7942e1d2005-05-29 14:58:36 -070011268 for (j = 0; j < len; j += 4) {
11269 u32 val;
11270
11271 tg3_write_mem(tp, offset + j, test_pattern[i]);
11272 tg3_read_mem(tp, offset + j, &val);
11273 if (val != test_pattern[i])
11274 return -EIO;
11275 }
11276 }
11277 return 0;
11278}
11279
11280static int tg3_test_memory(struct tg3 *tp)
11281{
11282 static struct mem_entry {
11283 u32 offset;
11284 u32 len;
11285 } mem_tbl_570x[] = {
Michael Chan38690192005-12-19 16:27:28 -080011286 { 0x00000000, 0x00b50},
Michael Chan7942e1d2005-05-29 14:58:36 -070011287 { 0x00002000, 0x1c000},
11288 { 0xffffffff, 0x00000}
11289 }, mem_tbl_5705[] = {
11290 { 0x00000100, 0x0000c},
11291 { 0x00000200, 0x00008},
Michael Chan7942e1d2005-05-29 14:58:36 -070011292 { 0x00004000, 0x00800},
11293 { 0x00006000, 0x01000},
11294 { 0x00008000, 0x02000},
11295 { 0x00010000, 0x0e000},
11296 { 0xffffffff, 0x00000}
Michael Chan79f4d132006-03-20 22:28:57 -080011297 }, mem_tbl_5755[] = {
11298 { 0x00000200, 0x00008},
11299 { 0x00004000, 0x00800},
11300 { 0x00006000, 0x00800},
11301 { 0x00008000, 0x02000},
11302 { 0x00010000, 0x0c000},
11303 { 0xffffffff, 0x00000}
Michael Chanb16250e2006-09-27 16:10:14 -070011304 }, mem_tbl_5906[] = {
11305 { 0x00000200, 0x00008},
11306 { 0x00004000, 0x00400},
11307 { 0x00006000, 0x00400},
11308 { 0x00008000, 0x01000},
11309 { 0x00010000, 0x01000},
11310 { 0xffffffff, 0x00000}
Matt Carlson8b5a6c42010-01-20 16:58:06 +000011311 }, mem_tbl_5717[] = {
11312 { 0x00000200, 0x00008},
11313 { 0x00010000, 0x0a000},
11314 { 0x00020000, 0x13c00},
11315 { 0xffffffff, 0x00000}
11316 }, mem_tbl_57765[] = {
11317 { 0x00000200, 0x00008},
11318 { 0x00004000, 0x00800},
11319 { 0x00006000, 0x09800},
11320 { 0x00010000, 0x0a000},
11321 { 0xffffffff, 0x00000}
Michael Chan7942e1d2005-05-29 14:58:36 -070011322 };
11323 struct mem_entry *mem_tbl;
11324 int err = 0;
11325 int i;
11326
Joe Perches63c3a662011-04-26 08:12:10 +000011327 if (tg3_flag(tp, 5717_PLUS))
Matt Carlson8b5a6c42010-01-20 16:58:06 +000011328 mem_tbl = mem_tbl_5717;
11329 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
11330 mem_tbl = mem_tbl_57765;
Joe Perches63c3a662011-04-26 08:12:10 +000011331 else if (tg3_flag(tp, 5755_PLUS))
Matt Carlson321d32a2008-11-21 17:22:19 -080011332 mem_tbl = mem_tbl_5755;
11333 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
11334 mem_tbl = mem_tbl_5906;
Joe Perches63c3a662011-04-26 08:12:10 +000011335 else if (tg3_flag(tp, 5705_PLUS))
Matt Carlson321d32a2008-11-21 17:22:19 -080011336 mem_tbl = mem_tbl_5705;
11337 else
Michael Chan7942e1d2005-05-29 14:58:36 -070011338 mem_tbl = mem_tbl_570x;
11339
11340 for (i = 0; mem_tbl[i].offset != 0xffffffff; i++) {
Matt Carlsonbe98da62010-07-11 09:31:46 +000011341 err = tg3_do_mem_test(tp, mem_tbl[i].offset, mem_tbl[i].len);
11342 if (err)
Michael Chan7942e1d2005-05-29 14:58:36 -070011343 break;
11344 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011345
Michael Chan7942e1d2005-05-29 14:58:36 -070011346 return err;
11347}
11348
Matt Carlsonbb158d62011-04-25 12:42:47 +000011349#define TG3_TSO_MSS 500
11350
11351#define TG3_TSO_IP_HDR_LEN 20
11352#define TG3_TSO_TCP_HDR_LEN 20
11353#define TG3_TSO_TCP_OPT_LEN 12
11354
11355static const u8 tg3_tso_header[] = {
113560x08, 0x00,
113570x45, 0x00, 0x00, 0x00,
113580x00, 0x00, 0x40, 0x00,
113590x40, 0x06, 0x00, 0x00,
113600x0a, 0x00, 0x00, 0x01,
113610x0a, 0x00, 0x00, 0x02,
113620x0d, 0x00, 0xe0, 0x00,
113630x00, 0x00, 0x01, 0x00,
113640x00, 0x00, 0x02, 0x00,
113650x80, 0x10, 0x10, 0x00,
113660x14, 0x09, 0x00, 0x00,
113670x01, 0x01, 0x08, 0x0a,
113680x11, 0x11, 0x11, 0x11,
113690x11, 0x11, 0x11, 0x11,
11370};
Michael Chan9f40dea2005-09-05 17:53:06 -070011371
Matt Carlson28a45952011-08-19 13:58:22 +000011372static int tg3_run_loopback(struct tg3 *tp, u32 pktsz, bool tso_loopback)
Michael Chanc76949a2005-05-29 14:58:59 -070011373{
Matt Carlson5e5a7f32011-08-19 13:58:21 +000011374 u32 rx_start_idx, rx_idx, tx_idx, opaque_key;
Matt Carlsonbb158d62011-04-25 12:42:47 +000011375 u32 base_flags = 0, mss = 0, desc_idx, coal_now, data_off, val;
Matt Carlson84b67b22011-07-27 14:20:52 +000011376 u32 budget;
Eric Dumazet9205fd92011-11-18 06:47:01 +000011377 struct sk_buff *skb;
11378 u8 *tx_data, *rx_data;
Michael Chanc76949a2005-05-29 14:58:59 -070011379 dma_addr_t map;
11380 int num_pkts, tx_len, rx_len, i, err;
11381 struct tg3_rx_buffer_desc *desc;
Matt Carlson898a56f2009-08-28 14:02:40 +000011382 struct tg3_napi *tnapi, *rnapi;
Matt Carlson8fea32b2010-09-15 08:59:58 +000011383 struct tg3_rx_prodring_set *tpr = &tp->napi[0].prodring;
Michael Chanc76949a2005-05-29 14:58:59 -070011384
Matt Carlsonc8873402010-02-12 14:47:11 +000011385 tnapi = &tp->napi[0];
11386 rnapi = &tp->napi[0];
Matt Carlson0c1d0e22009-09-01 13:16:33 +000011387 if (tp->irq_cnt > 1) {
Joe Perches63c3a662011-04-26 08:12:10 +000011388 if (tg3_flag(tp, ENABLE_RSS))
Matt Carlson1da85aa2010-09-30 10:34:34 +000011389 rnapi = &tp->napi[1];
Joe Perches63c3a662011-04-26 08:12:10 +000011390 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlsonc8873402010-02-12 14:47:11 +000011391 tnapi = &tp->napi[1];
Matt Carlson0c1d0e22009-09-01 13:16:33 +000011392 }
Matt Carlsonfd2ce372009-09-01 12:51:13 +000011393 coal_now = tnapi->coal_now | rnapi->coal_now;
Matt Carlson898a56f2009-08-28 14:02:40 +000011394
Michael Chanc76949a2005-05-29 14:58:59 -070011395 err = -EIO;
11396
Matt Carlson4852a862011-04-13 11:05:07 +000011397 tx_len = pktsz;
David S. Millera20e9c62006-07-31 22:38:16 -070011398 skb = netdev_alloc_skb(tp->dev, tx_len);
Jesper Juhla50bb7b2006-05-09 23:14:35 -070011399 if (!skb)
11400 return -ENOMEM;
11401
Michael Chanc76949a2005-05-29 14:58:59 -070011402 tx_data = skb_put(skb, tx_len);
11403 memcpy(tx_data, tp->dev->dev_addr, 6);
11404 memset(tx_data + 6, 0x0, 8);
11405
Matt Carlson4852a862011-04-13 11:05:07 +000011406 tw32(MAC_RX_MTU_SIZE, tx_len + ETH_FCS_LEN);
Michael Chanc76949a2005-05-29 14:58:59 -070011407
Matt Carlson28a45952011-08-19 13:58:22 +000011408 if (tso_loopback) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000011409 struct iphdr *iph = (struct iphdr *)&tx_data[ETH_HLEN];
11410
11411 u32 hdr_len = TG3_TSO_IP_HDR_LEN + TG3_TSO_TCP_HDR_LEN +
11412 TG3_TSO_TCP_OPT_LEN;
11413
11414 memcpy(tx_data + ETH_ALEN * 2, tg3_tso_header,
11415 sizeof(tg3_tso_header));
11416 mss = TG3_TSO_MSS;
11417
11418 val = tx_len - ETH_ALEN * 2 - sizeof(tg3_tso_header);
11419 num_pkts = DIV_ROUND_UP(val, TG3_TSO_MSS);
11420
11421 /* Set the total length field in the IP header */
11422 iph->tot_len = htons((u16)(mss + hdr_len));
11423
11424 base_flags = (TXD_FLAG_CPU_PRE_DMA |
11425 TXD_FLAG_CPU_POST_DMA);
11426
Joe Perches63c3a662011-04-26 08:12:10 +000011427 if (tg3_flag(tp, HW_TSO_1) ||
11428 tg3_flag(tp, HW_TSO_2) ||
11429 tg3_flag(tp, HW_TSO_3)) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000011430 struct tcphdr *th;
11431 val = ETH_HLEN + TG3_TSO_IP_HDR_LEN;
11432 th = (struct tcphdr *)&tx_data[val];
11433 th->check = 0;
11434 } else
11435 base_flags |= TXD_FLAG_TCPUDP_CSUM;
11436
Joe Perches63c3a662011-04-26 08:12:10 +000011437 if (tg3_flag(tp, HW_TSO_3)) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000011438 mss |= (hdr_len & 0xc) << 12;
11439 if (hdr_len & 0x10)
11440 base_flags |= 0x00000010;
11441 base_flags |= (hdr_len & 0x3e0) << 5;
Joe Perches63c3a662011-04-26 08:12:10 +000011442 } else if (tg3_flag(tp, HW_TSO_2))
Matt Carlsonbb158d62011-04-25 12:42:47 +000011443 mss |= hdr_len << 9;
Joe Perches63c3a662011-04-26 08:12:10 +000011444 else if (tg3_flag(tp, HW_TSO_1) ||
Matt Carlsonbb158d62011-04-25 12:42:47 +000011445 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
11446 mss |= (TG3_TSO_TCP_OPT_LEN << 9);
11447 } else {
11448 base_flags |= (TG3_TSO_TCP_OPT_LEN << 10);
11449 }
11450
11451 data_off = ETH_ALEN * 2 + sizeof(tg3_tso_header);
11452 } else {
11453 num_pkts = 1;
11454 data_off = ETH_HLEN;
11455 }
11456
11457 for (i = data_off; i < tx_len; i++)
Michael Chanc76949a2005-05-29 14:58:59 -070011458 tx_data[i] = (u8) (i & 0xff);
11459
Alexander Duyckf4188d82009-12-02 16:48:38 +000011460 map = pci_map_single(tp->pdev, skb->data, tx_len, PCI_DMA_TODEVICE);
11461 if (pci_dma_mapping_error(tp->pdev, map)) {
Matt Carlsona21771d2009-11-02 14:25:31 +000011462 dev_kfree_skb(skb);
11463 return -EIO;
11464 }
Michael Chanc76949a2005-05-29 14:58:59 -070011465
Matt Carlson0d681b22011-07-27 14:20:49 +000011466 val = tnapi->tx_prod;
11467 tnapi->tx_buffers[val].skb = skb;
11468 dma_unmap_addr_set(&tnapi->tx_buffers[val], mapping, map);
11469
Michael Chanc76949a2005-05-29 14:58:59 -070011470 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +000011471 rnapi->coal_now);
Michael Chanc76949a2005-05-29 14:58:59 -070011472
11473 udelay(10);
11474
Matt Carlson898a56f2009-08-28 14:02:40 +000011475 rx_start_idx = rnapi->hw_status->idx[0].rx_producer;
Michael Chanc76949a2005-05-29 14:58:59 -070011476
Matt Carlson84b67b22011-07-27 14:20:52 +000011477 budget = tg3_tx_avail(tnapi);
11478 if (tg3_tx_frag_set(tnapi, &val, &budget, map, tx_len,
Matt Carlsond1a3b732011-07-27 14:20:51 +000011479 base_flags | TXD_FLAG_END, mss, 0)) {
11480 tnapi->tx_buffers[val].skb = NULL;
11481 dev_kfree_skb(skb);
11482 return -EIO;
11483 }
Michael Chanc76949a2005-05-29 14:58:59 -070011484
Matt Carlsonf3f3f272009-08-28 14:03:21 +000011485 tnapi->tx_prod++;
Michael Chanc76949a2005-05-29 14:58:59 -070011486
Matt Carlsonf3f3f272009-08-28 14:03:21 +000011487 tw32_tx_mbox(tnapi->prodmbox, tnapi->tx_prod);
11488 tr32_mailbox(tnapi->prodmbox);
Michael Chanc76949a2005-05-29 14:58:59 -070011489
11490 udelay(10);
11491
Matt Carlson303fc922009-11-02 14:27:34 +000011492 /* 350 usec to allow enough time on some 10/100 Mbps devices. */
11493 for (i = 0; i < 35; i++) {
Michael Chanc76949a2005-05-29 14:58:59 -070011494 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +000011495 coal_now);
Michael Chanc76949a2005-05-29 14:58:59 -070011496
11497 udelay(10);
11498
Matt Carlson898a56f2009-08-28 14:02:40 +000011499 tx_idx = tnapi->hw_status->idx[0].tx_consumer;
11500 rx_idx = rnapi->hw_status->idx[0].rx_producer;
Matt Carlsonf3f3f272009-08-28 14:03:21 +000011501 if ((tx_idx == tnapi->tx_prod) &&
Michael Chanc76949a2005-05-29 14:58:59 -070011502 (rx_idx == (rx_start_idx + num_pkts)))
11503 break;
11504 }
11505
Matt Carlsonba1142e2011-11-04 09:15:00 +000011506 tg3_tx_skb_unmap(tnapi, tnapi->tx_prod - 1, -1);
Michael Chanc76949a2005-05-29 14:58:59 -070011507 dev_kfree_skb(skb);
11508
Matt Carlsonf3f3f272009-08-28 14:03:21 +000011509 if (tx_idx != tnapi->tx_prod)
Michael Chanc76949a2005-05-29 14:58:59 -070011510 goto out;
11511
11512 if (rx_idx != rx_start_idx + num_pkts)
11513 goto out;
11514
Matt Carlsonbb158d62011-04-25 12:42:47 +000011515 val = data_off;
11516 while (rx_idx != rx_start_idx) {
11517 desc = &rnapi->rx_rcb[rx_start_idx++];
11518 desc_idx = desc->opaque & RXD_OPAQUE_INDEX_MASK;
11519 opaque_key = desc->opaque & RXD_OPAQUE_RING_MASK;
Michael Chanc76949a2005-05-29 14:58:59 -070011520
Matt Carlsonbb158d62011-04-25 12:42:47 +000011521 if ((desc->err_vlan & RXD_ERR_MASK) != 0 &&
11522 (desc->err_vlan != RXD_ERR_ODD_NIBBLE_RCVD_MII))
Matt Carlson4852a862011-04-13 11:05:07 +000011523 goto out;
Michael Chanc76949a2005-05-29 14:58:59 -070011524
Matt Carlsonbb158d62011-04-25 12:42:47 +000011525 rx_len = ((desc->idx_len & RXD_LEN_MASK) >> RXD_LEN_SHIFT)
11526 - ETH_FCS_LEN;
11527
Matt Carlson28a45952011-08-19 13:58:22 +000011528 if (!tso_loopback) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000011529 if (rx_len != tx_len)
11530 goto out;
11531
11532 if (pktsz <= TG3_RX_STD_DMA_SZ - ETH_FCS_LEN) {
11533 if (opaque_key != RXD_OPAQUE_RING_STD)
11534 goto out;
11535 } else {
11536 if (opaque_key != RXD_OPAQUE_RING_JUMBO)
11537 goto out;
11538 }
11539 } else if ((desc->type_flags & RXD_FLAG_TCPUDP_CSUM) &&
11540 (desc->ip_tcp_csum & RXD_TCPCSUM_MASK)
Matt Carlson54e0a672011-05-19 12:12:50 +000011541 >> RXD_TCPCSUM_SHIFT != 0xffff) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000011542 goto out;
11543 }
11544
11545 if (opaque_key == RXD_OPAQUE_RING_STD) {
Eric Dumazet9205fd92011-11-18 06:47:01 +000011546 rx_data = tpr->rx_std_buffers[desc_idx].data;
Matt Carlsonbb158d62011-04-25 12:42:47 +000011547 map = dma_unmap_addr(&tpr->rx_std_buffers[desc_idx],
11548 mapping);
11549 } else if (opaque_key == RXD_OPAQUE_RING_JUMBO) {
Eric Dumazet9205fd92011-11-18 06:47:01 +000011550 rx_data = tpr->rx_jmb_buffers[desc_idx].data;
Matt Carlsonbb158d62011-04-25 12:42:47 +000011551 map = dma_unmap_addr(&tpr->rx_jmb_buffers[desc_idx],
11552 mapping);
11553 } else
Matt Carlson4852a862011-04-13 11:05:07 +000011554 goto out;
11555
Matt Carlsonbb158d62011-04-25 12:42:47 +000011556 pci_dma_sync_single_for_cpu(tp->pdev, map, rx_len,
11557 PCI_DMA_FROMDEVICE);
11558
Eric Dumazet9205fd92011-11-18 06:47:01 +000011559 rx_data += TG3_RX_OFFSET(tp);
Matt Carlsonbb158d62011-04-25 12:42:47 +000011560 for (i = data_off; i < rx_len; i++, val++) {
Eric Dumazet9205fd92011-11-18 06:47:01 +000011561 if (*(rx_data + i) != (u8) (val & 0xff))
Matt Carlsonbb158d62011-04-25 12:42:47 +000011562 goto out;
11563 }
Matt Carlson4852a862011-04-13 11:05:07 +000011564 }
11565
Michael Chanc76949a2005-05-29 14:58:59 -070011566 err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011567
Eric Dumazet9205fd92011-11-18 06:47:01 +000011568 /* tg3_free_rings will unmap and free the rx_data */
Michael Chanc76949a2005-05-29 14:58:59 -070011569out:
11570 return err;
11571}
11572
Matt Carlson00c266b2011-04-25 12:42:46 +000011573#define TG3_STD_LOOPBACK_FAILED 1
11574#define TG3_JMB_LOOPBACK_FAILED 2
Matt Carlsonbb158d62011-04-25 12:42:47 +000011575#define TG3_TSO_LOOPBACK_FAILED 4
Matt Carlson28a45952011-08-19 13:58:22 +000011576#define TG3_LOOPBACK_FAILED \
11577 (TG3_STD_LOOPBACK_FAILED | \
11578 TG3_JMB_LOOPBACK_FAILED | \
11579 TG3_TSO_LOOPBACK_FAILED)
Matt Carlson00c266b2011-04-25 12:42:46 +000011580
Matt Carlson941ec902011-08-19 13:58:23 +000011581static int tg3_test_loopback(struct tg3 *tp, u64 *data, bool do_extlpbk)
Michael Chan9f40dea2005-09-05 17:53:06 -070011582{
Matt Carlson28a45952011-08-19 13:58:22 +000011583 int err = -EIO;
Matt Carlson2215e242011-08-19 13:58:19 +000011584 u32 eee_cap;
Michael Chan9f40dea2005-09-05 17:53:06 -070011585
Matt Carlsonab789042011-01-25 15:58:54 +000011586 eee_cap = tp->phy_flags & TG3_PHYFLG_EEE_CAP;
11587 tp->phy_flags &= ~TG3_PHYFLG_EEE_CAP;
11588
Matt Carlson28a45952011-08-19 13:58:22 +000011589 if (!netif_running(tp->dev)) {
11590 data[0] = TG3_LOOPBACK_FAILED;
11591 data[1] = TG3_LOOPBACK_FAILED;
Matt Carlson941ec902011-08-19 13:58:23 +000011592 if (do_extlpbk)
11593 data[2] = TG3_LOOPBACK_FAILED;
Matt Carlson28a45952011-08-19 13:58:22 +000011594 goto done;
11595 }
11596
Michael Chanb9ec6c12006-07-25 16:37:27 -070011597 err = tg3_reset_hw(tp, 1);
Matt Carlsonab789042011-01-25 15:58:54 +000011598 if (err) {
Matt Carlson28a45952011-08-19 13:58:22 +000011599 data[0] = TG3_LOOPBACK_FAILED;
11600 data[1] = TG3_LOOPBACK_FAILED;
Matt Carlson941ec902011-08-19 13:58:23 +000011601 if (do_extlpbk)
11602 data[2] = TG3_LOOPBACK_FAILED;
Matt Carlsonab789042011-01-25 15:58:54 +000011603 goto done;
11604 }
Michael Chan9f40dea2005-09-05 17:53:06 -070011605
Joe Perches63c3a662011-04-26 08:12:10 +000011606 if (tg3_flag(tp, ENABLE_RSS)) {
Matt Carlson4a85f092011-04-20 07:57:37 +000011607 int i;
11608
11609 /* Reroute all rx packets to the 1st queue */
11610 for (i = MAC_RSS_INDIR_TBL_0;
11611 i < MAC_RSS_INDIR_TBL_0 + TG3_RSS_INDIR_TBL_SIZE; i += 4)
11612 tw32(i, 0x0);
11613 }
11614
Matt Carlson6e01b202011-08-19 13:58:20 +000011615 /* HW errata - mac loopback fails in some cases on 5780.
11616 * Normal traffic and PHY loopback are not affected by
11617 * errata. Also, the MAC loopback test is deprecated for
11618 * all newer ASIC revisions.
11619 */
11620 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5780 &&
11621 !tg3_flag(tp, CPMU_PRESENT)) {
11622 tg3_mac_loopback(tp, true);
Matt Carlson9936bcf2007-10-10 18:03:07 -070011623
Matt Carlson28a45952011-08-19 13:58:22 +000011624 if (tg3_run_loopback(tp, ETH_FRAME_LEN, false))
11625 data[0] |= TG3_STD_LOOPBACK_FAILED;
Matt Carlson6e01b202011-08-19 13:58:20 +000011626
11627 if (tg3_flag(tp, JUMBO_RING_ENABLE) &&
Matt Carlson28a45952011-08-19 13:58:22 +000011628 tg3_run_loopback(tp, 9000 + ETH_HLEN, false))
11629 data[0] |= TG3_JMB_LOOPBACK_FAILED;
Matt Carlson6e01b202011-08-19 13:58:20 +000011630
11631 tg3_mac_loopback(tp, false);
11632 }
Matt Carlson4852a862011-04-13 11:05:07 +000011633
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011634 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
Joe Perches63c3a662011-04-26 08:12:10 +000011635 !tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson5e5a7f32011-08-19 13:58:21 +000011636 int i;
11637
Matt Carlson941ec902011-08-19 13:58:23 +000011638 tg3_phy_lpbk_set(tp, 0, false);
Matt Carlson5e5a7f32011-08-19 13:58:21 +000011639
11640 /* Wait for link */
11641 for (i = 0; i < 100; i++) {
11642 if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
11643 break;
11644 mdelay(1);
11645 }
11646
Matt Carlson28a45952011-08-19 13:58:22 +000011647 if (tg3_run_loopback(tp, ETH_FRAME_LEN, false))
11648 data[1] |= TG3_STD_LOOPBACK_FAILED;
Joe Perches63c3a662011-04-26 08:12:10 +000011649 if (tg3_flag(tp, TSO_CAPABLE) &&
Matt Carlson28a45952011-08-19 13:58:22 +000011650 tg3_run_loopback(tp, ETH_FRAME_LEN, true))
11651 data[1] |= TG3_TSO_LOOPBACK_FAILED;
Joe Perches63c3a662011-04-26 08:12:10 +000011652 if (tg3_flag(tp, JUMBO_RING_ENABLE) &&
Matt Carlson28a45952011-08-19 13:58:22 +000011653 tg3_run_loopback(tp, 9000 + ETH_HLEN, false))
11654 data[1] |= TG3_JMB_LOOPBACK_FAILED;
Michael Chan9f40dea2005-09-05 17:53:06 -070011655
Matt Carlson941ec902011-08-19 13:58:23 +000011656 if (do_extlpbk) {
11657 tg3_phy_lpbk_set(tp, 0, true);
11658
11659 /* All link indications report up, but the hardware
11660 * isn't really ready for about 20 msec. Double it
11661 * to be sure.
11662 */
11663 mdelay(40);
11664
11665 if (tg3_run_loopback(tp, ETH_FRAME_LEN, false))
11666 data[2] |= TG3_STD_LOOPBACK_FAILED;
11667 if (tg3_flag(tp, TSO_CAPABLE) &&
11668 tg3_run_loopback(tp, ETH_FRAME_LEN, true))
11669 data[2] |= TG3_TSO_LOOPBACK_FAILED;
11670 if (tg3_flag(tp, JUMBO_RING_ENABLE) &&
11671 tg3_run_loopback(tp, 9000 + ETH_HLEN, false))
11672 data[2] |= TG3_JMB_LOOPBACK_FAILED;
11673 }
11674
Matt Carlson5e5a7f32011-08-19 13:58:21 +000011675 /* Re-enable gphy autopowerdown. */
11676 if (tp->phy_flags & TG3_PHYFLG_ENABLE_APD)
11677 tg3_phy_toggle_apd(tp, true);
11678 }
Matt Carlson6833c042008-11-21 17:18:59 -080011679
Matt Carlson941ec902011-08-19 13:58:23 +000011680 err = (data[0] | data[1] | data[2]) ? -EIO : 0;
Matt Carlson28a45952011-08-19 13:58:22 +000011681
Matt Carlsonab789042011-01-25 15:58:54 +000011682done:
11683 tp->phy_flags |= eee_cap;
11684
Michael Chan9f40dea2005-09-05 17:53:06 -070011685 return err;
11686}
11687
Michael Chan4cafd3f2005-05-29 14:56:34 -070011688static void tg3_self_test(struct net_device *dev, struct ethtool_test *etest,
11689 u64 *data)
11690{
Michael Chan566f86a2005-05-29 14:56:58 -070011691 struct tg3 *tp = netdev_priv(dev);
Matt Carlson941ec902011-08-19 13:58:23 +000011692 bool doextlpbk = etest->flags & ETH_TEST_FL_EXTERNAL_LB;
Michael Chan566f86a2005-05-29 14:56:58 -070011693
Matt Carlsonbed98292011-07-13 09:27:29 +000011694 if ((tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) &&
11695 tg3_power_up(tp)) {
11696 etest->flags |= ETH_TEST_FL_FAILED;
11697 memset(data, 1, sizeof(u64) * TG3_NUM_TEST);
11698 return;
11699 }
Michael Chanbc1c7562006-03-20 17:48:03 -080011700
Michael Chan566f86a2005-05-29 14:56:58 -070011701 memset(data, 0, sizeof(u64) * TG3_NUM_TEST);
11702
11703 if (tg3_test_nvram(tp) != 0) {
11704 etest->flags |= ETH_TEST_FL_FAILED;
11705 data[0] = 1;
11706 }
Matt Carlson941ec902011-08-19 13:58:23 +000011707 if (!doextlpbk && tg3_test_link(tp)) {
Michael Chanca430072005-05-29 14:57:23 -070011708 etest->flags |= ETH_TEST_FL_FAILED;
11709 data[1] = 1;
11710 }
Michael Chana71116d2005-05-29 14:58:11 -070011711 if (etest->flags & ETH_TEST_FL_OFFLINE) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011712 int err, err2 = 0, irq_sync = 0;
Michael Chana71116d2005-05-29 14:58:11 -070011713
Michael Chanbbe832c2005-06-24 20:20:04 -070011714 if (netif_running(dev)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011715 tg3_phy_stop(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -070011716 tg3_netif_stop(tp);
11717 irq_sync = 1;
11718 }
11719
11720 tg3_full_lock(tp, irq_sync);
Michael Chana71116d2005-05-29 14:58:11 -070011721
11722 tg3_halt(tp, RESET_KIND_SUSPEND, 1);
Michael Chanec41c7d2006-01-17 02:40:55 -080011723 err = tg3_nvram_lock(tp);
Michael Chana71116d2005-05-29 14:58:11 -070011724 tg3_halt_cpu(tp, RX_CPU_BASE);
Joe Perches63c3a662011-04-26 08:12:10 +000011725 if (!tg3_flag(tp, 5705_PLUS))
Michael Chana71116d2005-05-29 14:58:11 -070011726 tg3_halt_cpu(tp, TX_CPU_BASE);
Michael Chanec41c7d2006-01-17 02:40:55 -080011727 if (!err)
11728 tg3_nvram_unlock(tp);
Michael Chana71116d2005-05-29 14:58:11 -070011729
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011730 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
Michael Chand9ab5ad2006-03-20 22:27:35 -080011731 tg3_phy_reset(tp);
11732
Michael Chana71116d2005-05-29 14:58:11 -070011733 if (tg3_test_registers(tp) != 0) {
11734 etest->flags |= ETH_TEST_FL_FAILED;
11735 data[2] = 1;
11736 }
Matt Carlson28a45952011-08-19 13:58:22 +000011737
Michael Chan7942e1d2005-05-29 14:58:36 -070011738 if (tg3_test_memory(tp) != 0) {
11739 etest->flags |= ETH_TEST_FL_FAILED;
11740 data[3] = 1;
11741 }
Matt Carlson28a45952011-08-19 13:58:22 +000011742
Matt Carlson941ec902011-08-19 13:58:23 +000011743 if (doextlpbk)
11744 etest->flags |= ETH_TEST_FL_EXTERNAL_LB_DONE;
11745
11746 if (tg3_test_loopback(tp, &data[4], doextlpbk))
Michael Chanc76949a2005-05-29 14:58:59 -070011747 etest->flags |= ETH_TEST_FL_FAILED;
Michael Chana71116d2005-05-29 14:58:11 -070011748
David S. Millerf47c11e2005-06-24 20:18:35 -070011749 tg3_full_unlock(tp);
11750
Michael Chand4bc3922005-05-29 14:59:20 -070011751 if (tg3_test_interrupt(tp) != 0) {
11752 etest->flags |= ETH_TEST_FL_FAILED;
Matt Carlson941ec902011-08-19 13:58:23 +000011753 data[7] = 1;
Michael Chand4bc3922005-05-29 14:59:20 -070011754 }
David S. Millerf47c11e2005-06-24 20:18:35 -070011755
11756 tg3_full_lock(tp, 0);
Michael Chand4bc3922005-05-29 14:59:20 -070011757
Michael Chana71116d2005-05-29 14:58:11 -070011758 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
11759 if (netif_running(dev)) {
Joe Perches63c3a662011-04-26 08:12:10 +000011760 tg3_flag_set(tp, INIT_COMPLETE);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011761 err2 = tg3_restart_hw(tp, 1);
11762 if (!err2)
Michael Chanb9ec6c12006-07-25 16:37:27 -070011763 tg3_netif_start(tp);
Michael Chana71116d2005-05-29 14:58:11 -070011764 }
David S. Millerf47c11e2005-06-24 20:18:35 -070011765
11766 tg3_full_unlock(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011767
11768 if (irq_sync && !err2)
11769 tg3_phy_start(tp);
Michael Chana71116d2005-05-29 14:58:11 -070011770 }
Matt Carlson80096062010-08-02 11:26:06 +000011771 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000011772 tg3_power_down(tp);
Michael Chanbc1c7562006-03-20 17:48:03 -080011773
Michael Chan4cafd3f2005-05-29 14:56:34 -070011774}
11775
Linus Torvalds1da177e2005-04-16 15:20:36 -070011776static int tg3_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
11777{
11778 struct mii_ioctl_data *data = if_mii(ifr);
11779 struct tg3 *tp = netdev_priv(dev);
11780 int err;
11781
Joe Perches63c3a662011-04-26 08:12:10 +000011782 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000011783 struct phy_device *phydev;
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011784 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011785 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000011786 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Richard Cochran28b04112010-07-17 08:48:55 +000011787 return phy_mii_ioctl(phydev, ifr, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011788 }
11789
Matt Carlson33f401a2010-04-05 10:19:27 +000011790 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011791 case SIOCGMIIPHY:
Matt Carlson882e9792009-09-01 13:21:36 +000011792 data->phy_id = tp->phy_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011793
11794 /* fallthru */
11795 case SIOCGMIIREG: {
11796 u32 mii_regval;
11797
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011798 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011799 break; /* We have no PHY */
11800
Matt Carlson34eea5a2011-04-20 07:57:38 +000011801 if (!netif_running(dev))
Michael Chanbc1c7562006-03-20 17:48:03 -080011802 return -EAGAIN;
11803
David S. Millerf47c11e2005-06-24 20:18:35 -070011804 spin_lock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011805 err = tg3_readphy(tp, data->reg_num & 0x1f, &mii_regval);
David S. Millerf47c11e2005-06-24 20:18:35 -070011806 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011807
11808 data->val_out = mii_regval;
11809
11810 return err;
11811 }
11812
11813 case SIOCSMIIREG:
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011814 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011815 break; /* We have no PHY */
11816
Matt Carlson34eea5a2011-04-20 07:57:38 +000011817 if (!netif_running(dev))
Michael Chanbc1c7562006-03-20 17:48:03 -080011818 return -EAGAIN;
11819
David S. Millerf47c11e2005-06-24 20:18:35 -070011820 spin_lock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011821 err = tg3_writephy(tp, data->reg_num & 0x1f, data->val_in);
David S. Millerf47c11e2005-06-24 20:18:35 -070011822 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011823
11824 return err;
11825
11826 default:
11827 /* do nothing */
11828 break;
11829 }
11830 return -EOPNOTSUPP;
11831}
11832
David S. Miller15f98502005-05-18 22:49:26 -070011833static int tg3_get_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
11834{
11835 struct tg3 *tp = netdev_priv(dev);
11836
11837 memcpy(ec, &tp->coal, sizeof(*ec));
11838 return 0;
11839}
11840
Michael Chand244c892005-07-05 14:42:33 -070011841static int tg3_set_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
11842{
11843 struct tg3 *tp = netdev_priv(dev);
11844 u32 max_rxcoal_tick_int = 0, max_txcoal_tick_int = 0;
11845 u32 max_stat_coal_ticks = 0, min_stat_coal_ticks = 0;
11846
Joe Perches63c3a662011-04-26 08:12:10 +000011847 if (!tg3_flag(tp, 5705_PLUS)) {
Michael Chand244c892005-07-05 14:42:33 -070011848 max_rxcoal_tick_int = MAX_RXCOAL_TICK_INT;
11849 max_txcoal_tick_int = MAX_TXCOAL_TICK_INT;
11850 max_stat_coal_ticks = MAX_STAT_COAL_TICKS;
11851 min_stat_coal_ticks = MIN_STAT_COAL_TICKS;
11852 }
11853
11854 if ((ec->rx_coalesce_usecs > MAX_RXCOL_TICKS) ||
11855 (ec->tx_coalesce_usecs > MAX_TXCOL_TICKS) ||
11856 (ec->rx_max_coalesced_frames > MAX_RXMAX_FRAMES) ||
11857 (ec->tx_max_coalesced_frames > MAX_TXMAX_FRAMES) ||
11858 (ec->rx_coalesce_usecs_irq > max_rxcoal_tick_int) ||
11859 (ec->tx_coalesce_usecs_irq > max_txcoal_tick_int) ||
11860 (ec->rx_max_coalesced_frames_irq > MAX_RXCOAL_MAXF_INT) ||
11861 (ec->tx_max_coalesced_frames_irq > MAX_TXCOAL_MAXF_INT) ||
11862 (ec->stats_block_coalesce_usecs > max_stat_coal_ticks) ||
11863 (ec->stats_block_coalesce_usecs < min_stat_coal_ticks))
11864 return -EINVAL;
11865
11866 /* No rx interrupts will be generated if both are zero */
11867 if ((ec->rx_coalesce_usecs == 0) &&
11868 (ec->rx_max_coalesced_frames == 0))
11869 return -EINVAL;
11870
11871 /* No tx interrupts will be generated if both are zero */
11872 if ((ec->tx_coalesce_usecs == 0) &&
11873 (ec->tx_max_coalesced_frames == 0))
11874 return -EINVAL;
11875
11876 /* Only copy relevant parameters, ignore all others. */
11877 tp->coal.rx_coalesce_usecs = ec->rx_coalesce_usecs;
11878 tp->coal.tx_coalesce_usecs = ec->tx_coalesce_usecs;
11879 tp->coal.rx_max_coalesced_frames = ec->rx_max_coalesced_frames;
11880 tp->coal.tx_max_coalesced_frames = ec->tx_max_coalesced_frames;
11881 tp->coal.rx_coalesce_usecs_irq = ec->rx_coalesce_usecs_irq;
11882 tp->coal.tx_coalesce_usecs_irq = ec->tx_coalesce_usecs_irq;
11883 tp->coal.rx_max_coalesced_frames_irq = ec->rx_max_coalesced_frames_irq;
11884 tp->coal.tx_max_coalesced_frames_irq = ec->tx_max_coalesced_frames_irq;
11885 tp->coal.stats_block_coalesce_usecs = ec->stats_block_coalesce_usecs;
11886
11887 if (netif_running(dev)) {
11888 tg3_full_lock(tp, 0);
11889 __tg3_set_coalesce(tp, &tp->coal);
11890 tg3_full_unlock(tp);
11891 }
11892 return 0;
11893}
11894
Jeff Garzik7282d492006-09-13 14:30:00 -040011895static const struct ethtool_ops tg3_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011896 .get_settings = tg3_get_settings,
11897 .set_settings = tg3_set_settings,
11898 .get_drvinfo = tg3_get_drvinfo,
11899 .get_regs_len = tg3_get_regs_len,
11900 .get_regs = tg3_get_regs,
11901 .get_wol = tg3_get_wol,
11902 .set_wol = tg3_set_wol,
11903 .get_msglevel = tg3_get_msglevel,
11904 .set_msglevel = tg3_set_msglevel,
11905 .nway_reset = tg3_nway_reset,
11906 .get_link = ethtool_op_get_link,
11907 .get_eeprom_len = tg3_get_eeprom_len,
11908 .get_eeprom = tg3_get_eeprom,
11909 .set_eeprom = tg3_set_eeprom,
11910 .get_ringparam = tg3_get_ringparam,
11911 .set_ringparam = tg3_set_ringparam,
11912 .get_pauseparam = tg3_get_pauseparam,
11913 .set_pauseparam = tg3_set_pauseparam,
Michael Chan4cafd3f2005-05-29 14:56:34 -070011914 .self_test = tg3_self_test,
Linus Torvalds1da177e2005-04-16 15:20:36 -070011915 .get_strings = tg3_get_strings,
stephen hemminger81b87092011-04-04 08:43:50 +000011916 .set_phys_id = tg3_set_phys_id,
Linus Torvalds1da177e2005-04-16 15:20:36 -070011917 .get_ethtool_stats = tg3_get_ethtool_stats,
David S. Miller15f98502005-05-18 22:49:26 -070011918 .get_coalesce = tg3_get_coalesce,
Michael Chand244c892005-07-05 14:42:33 -070011919 .set_coalesce = tg3_set_coalesce,
Jeff Garzikb9f2c042007-10-03 18:07:32 -070011920 .get_sset_count = tg3_get_sset_count,
Linus Torvalds1da177e2005-04-16 15:20:36 -070011921};
11922
11923static void __devinit tg3_get_eeprom_size(struct tg3 *tp)
11924{
Michael Chan1b277772006-03-20 22:27:48 -080011925 u32 cursize, val, magic;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011926
11927 tp->nvram_size = EEPROM_CHIP_SIZE;
11928
Matt Carlsone4f34112009-02-25 14:25:00 +000011929 if (tg3_nvram_read(tp, 0, &magic) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011930 return;
11931
Michael Chanb16250e2006-09-27 16:10:14 -070011932 if ((magic != TG3_EEPROM_MAGIC) &&
11933 ((magic & TG3_EEPROM_MAGIC_FW_MSK) != TG3_EEPROM_MAGIC_FW) &&
11934 ((magic & TG3_EEPROM_MAGIC_HW_MSK) != TG3_EEPROM_MAGIC_HW))
Linus Torvalds1da177e2005-04-16 15:20:36 -070011935 return;
11936
11937 /*
11938 * Size the chip by reading offsets at increasing powers of two.
11939 * When we encounter our validation signature, we know the addressing
11940 * has wrapped around, and thus have our chip size.
11941 */
Michael Chan1b277772006-03-20 22:27:48 -080011942 cursize = 0x10;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011943
11944 while (cursize < tp->nvram_size) {
Matt Carlsone4f34112009-02-25 14:25:00 +000011945 if (tg3_nvram_read(tp, cursize, &val) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011946 return;
11947
Michael Chan18201802006-03-20 22:29:15 -080011948 if (val == magic)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011949 break;
11950
11951 cursize <<= 1;
11952 }
11953
11954 tp->nvram_size = cursize;
11955}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011956
Linus Torvalds1da177e2005-04-16 15:20:36 -070011957static void __devinit tg3_get_nvram_size(struct tg3 *tp)
11958{
11959 u32 val;
11960
Joe Perches63c3a662011-04-26 08:12:10 +000011961 if (tg3_flag(tp, NO_NVRAM) || tg3_nvram_read(tp, 0, &val) != 0)
Michael Chan1b277772006-03-20 22:27:48 -080011962 return;
11963
11964 /* Selfboot format */
Michael Chan18201802006-03-20 22:29:15 -080011965 if (val != TG3_EEPROM_MAGIC) {
Michael Chan1b277772006-03-20 22:27:48 -080011966 tg3_get_eeprom_size(tp);
11967 return;
11968 }
11969
Matt Carlson6d348f22009-02-25 14:25:52 +000011970 if (tg3_nvram_read(tp, 0xf0, &val) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011971 if (val != 0) {
Matt Carlson6d348f22009-02-25 14:25:52 +000011972 /* This is confusing. We want to operate on the
11973 * 16-bit value at offset 0xf2. The tg3_nvram_read()
11974 * call will read from NVRAM and byteswap the data
11975 * according to the byteswapping settings for all
11976 * other register accesses. This ensures the data we
11977 * want will always reside in the lower 16-bits.
11978 * However, the data in NVRAM is in LE format, which
11979 * means the data from the NVRAM read will always be
11980 * opposite the endianness of the CPU. The 16-bit
11981 * byteswap then brings the data to CPU endianness.
11982 */
11983 tp->nvram_size = swab16((u16)(val & 0x0000ffff)) * 1024;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011984 return;
11985 }
11986 }
Matt Carlsonfd1122a2008-05-02 16:48:36 -070011987 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011988}
11989
11990static void __devinit tg3_get_nvram_info(struct tg3 *tp)
11991{
11992 u32 nvcfg1;
11993
11994 nvcfg1 = tr32(NVRAM_CFG1);
11995 if (nvcfg1 & NVRAM_CFG1_FLASHIF_ENAB) {
Joe Perches63c3a662011-04-26 08:12:10 +000011996 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000011997 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011998 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
11999 tw32(NVRAM_CFG1, nvcfg1);
12000 }
12001
Matt Carlson6ff6f812011-05-19 12:12:54 +000012002 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 ||
Joe Perches63c3a662011-04-26 08:12:10 +000012003 tg3_flag(tp, 5780_CLASS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012004 switch (nvcfg1 & NVRAM_CFG1_VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000012005 case FLASH_VENDOR_ATMEL_FLASH_BUFFERED:
12006 tp->nvram_jedecnum = JEDEC_ATMEL;
12007 tp->nvram_pagesize = ATMEL_AT45DB0X1B_PAGE_SIZE;
Joe Perches63c3a662011-04-26 08:12:10 +000012008 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000012009 break;
12010 case FLASH_VENDOR_ATMEL_FLASH_UNBUFFERED:
12011 tp->nvram_jedecnum = JEDEC_ATMEL;
12012 tp->nvram_pagesize = ATMEL_AT25F512_PAGE_SIZE;
12013 break;
12014 case FLASH_VENDOR_ATMEL_EEPROM:
12015 tp->nvram_jedecnum = JEDEC_ATMEL;
12016 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
Joe Perches63c3a662011-04-26 08:12:10 +000012017 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000012018 break;
12019 case FLASH_VENDOR_ST:
12020 tp->nvram_jedecnum = JEDEC_ST;
12021 tp->nvram_pagesize = ST_M45PEX0_PAGE_SIZE;
Joe Perches63c3a662011-04-26 08:12:10 +000012022 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000012023 break;
12024 case FLASH_VENDOR_SAIFUN:
12025 tp->nvram_jedecnum = JEDEC_SAIFUN;
12026 tp->nvram_pagesize = SAIFUN_SA25F0XX_PAGE_SIZE;
12027 break;
12028 case FLASH_VENDOR_SST_SMALL:
12029 case FLASH_VENDOR_SST_LARGE:
12030 tp->nvram_jedecnum = JEDEC_SST;
12031 tp->nvram_pagesize = SST_25VF0X0_PAGE_SIZE;
12032 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012033 }
Matt Carlson8590a602009-08-28 12:29:16 +000012034 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012035 tp->nvram_jedecnum = JEDEC_ATMEL;
12036 tp->nvram_pagesize = ATMEL_AT45DB0X1B_PAGE_SIZE;
Joe Perches63c3a662011-04-26 08:12:10 +000012037 tg3_flag_set(tp, NVRAM_BUFFERED);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012038 }
12039}
12040
Matt Carlsona1b950d2009-09-01 13:20:17 +000012041static void __devinit tg3_nvram_get_pagesize(struct tg3 *tp, u32 nvmcfg1)
12042{
12043 switch (nvmcfg1 & NVRAM_CFG1_5752PAGE_SIZE_MASK) {
12044 case FLASH_5752PAGE_SIZE_256:
12045 tp->nvram_pagesize = 256;
12046 break;
12047 case FLASH_5752PAGE_SIZE_512:
12048 tp->nvram_pagesize = 512;
12049 break;
12050 case FLASH_5752PAGE_SIZE_1K:
12051 tp->nvram_pagesize = 1024;
12052 break;
12053 case FLASH_5752PAGE_SIZE_2K:
12054 tp->nvram_pagesize = 2048;
12055 break;
12056 case FLASH_5752PAGE_SIZE_4K:
12057 tp->nvram_pagesize = 4096;
12058 break;
12059 case FLASH_5752PAGE_SIZE_264:
12060 tp->nvram_pagesize = 264;
12061 break;
12062 case FLASH_5752PAGE_SIZE_528:
12063 tp->nvram_pagesize = 528;
12064 break;
12065 }
12066}
12067
Michael Chan361b4ac2005-04-21 17:11:21 -070012068static void __devinit tg3_get_5752_nvram_info(struct tg3 *tp)
12069{
12070 u32 nvcfg1;
12071
12072 nvcfg1 = tr32(NVRAM_CFG1);
12073
Michael Chane6af3012005-04-21 17:12:05 -070012074 /* NVRAM protection for TPM */
12075 if (nvcfg1 & (1 << 27))
Joe Perches63c3a662011-04-26 08:12:10 +000012076 tg3_flag_set(tp, PROTECTED_NVRAM);
Michael Chane6af3012005-04-21 17:12:05 -070012077
Michael Chan361b4ac2005-04-21 17:11:21 -070012078 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000012079 case FLASH_5752VENDOR_ATMEL_EEPROM_64KHZ:
12080 case FLASH_5752VENDOR_ATMEL_EEPROM_376KHZ:
12081 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012082 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000012083 break;
12084 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
12085 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012086 tg3_flag_set(tp, NVRAM_BUFFERED);
12087 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000012088 break;
12089 case FLASH_5752VENDOR_ST_M45PE10:
12090 case FLASH_5752VENDOR_ST_M45PE20:
12091 case FLASH_5752VENDOR_ST_M45PE40:
12092 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000012093 tg3_flag_set(tp, NVRAM_BUFFERED);
12094 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000012095 break;
Michael Chan361b4ac2005-04-21 17:11:21 -070012096 }
12097
Joe Perches63c3a662011-04-26 08:12:10 +000012098 if (tg3_flag(tp, FLASH)) {
Matt Carlsona1b950d2009-09-01 13:20:17 +000012099 tg3_nvram_get_pagesize(tp, nvcfg1);
Matt Carlson8590a602009-08-28 12:29:16 +000012100 } else {
Michael Chan361b4ac2005-04-21 17:11:21 -070012101 /* For eeprom, set pagesize to maximum eeprom size */
12102 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
12103
12104 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
12105 tw32(NVRAM_CFG1, nvcfg1);
12106 }
12107}
12108
Michael Chand3c7b882006-03-23 01:28:25 -080012109static void __devinit tg3_get_5755_nvram_info(struct tg3 *tp)
12110{
Matt Carlson989a9d22007-05-05 11:51:05 -070012111 u32 nvcfg1, protect = 0;
Michael Chand3c7b882006-03-23 01:28:25 -080012112
12113 nvcfg1 = tr32(NVRAM_CFG1);
12114
12115 /* NVRAM protection for TPM */
Matt Carlson989a9d22007-05-05 11:51:05 -070012116 if (nvcfg1 & (1 << 27)) {
Joe Perches63c3a662011-04-26 08:12:10 +000012117 tg3_flag_set(tp, PROTECTED_NVRAM);
Matt Carlson989a9d22007-05-05 11:51:05 -070012118 protect = 1;
12119 }
Michael Chand3c7b882006-03-23 01:28:25 -080012120
Matt Carlson989a9d22007-05-05 11:51:05 -070012121 nvcfg1 &= NVRAM_CFG1_5752VENDOR_MASK;
12122 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000012123 case FLASH_5755VENDOR_ATMEL_FLASH_1:
12124 case FLASH_5755VENDOR_ATMEL_FLASH_2:
12125 case FLASH_5755VENDOR_ATMEL_FLASH_3:
12126 case FLASH_5755VENDOR_ATMEL_FLASH_5:
12127 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012128 tg3_flag_set(tp, NVRAM_BUFFERED);
12129 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000012130 tp->nvram_pagesize = 264;
12131 if (nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_1 ||
12132 nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_5)
12133 tp->nvram_size = (protect ? 0x3e200 :
12134 TG3_NVRAM_SIZE_512KB);
12135 else if (nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_2)
12136 tp->nvram_size = (protect ? 0x1f200 :
12137 TG3_NVRAM_SIZE_256KB);
12138 else
12139 tp->nvram_size = (protect ? 0x1f200 :
12140 TG3_NVRAM_SIZE_128KB);
12141 break;
12142 case FLASH_5752VENDOR_ST_M45PE10:
12143 case FLASH_5752VENDOR_ST_M45PE20:
12144 case FLASH_5752VENDOR_ST_M45PE40:
12145 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000012146 tg3_flag_set(tp, NVRAM_BUFFERED);
12147 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000012148 tp->nvram_pagesize = 256;
12149 if (nvcfg1 == FLASH_5752VENDOR_ST_M45PE10)
12150 tp->nvram_size = (protect ?
12151 TG3_NVRAM_SIZE_64KB :
12152 TG3_NVRAM_SIZE_128KB);
12153 else if (nvcfg1 == FLASH_5752VENDOR_ST_M45PE20)
12154 tp->nvram_size = (protect ?
12155 TG3_NVRAM_SIZE_64KB :
12156 TG3_NVRAM_SIZE_256KB);
12157 else
12158 tp->nvram_size = (protect ?
12159 TG3_NVRAM_SIZE_128KB :
12160 TG3_NVRAM_SIZE_512KB);
12161 break;
Michael Chand3c7b882006-03-23 01:28:25 -080012162 }
12163}
12164
Michael Chan1b277772006-03-20 22:27:48 -080012165static void __devinit tg3_get_5787_nvram_info(struct tg3 *tp)
12166{
12167 u32 nvcfg1;
12168
12169 nvcfg1 = tr32(NVRAM_CFG1);
12170
12171 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000012172 case FLASH_5787VENDOR_ATMEL_EEPROM_64KHZ:
12173 case FLASH_5787VENDOR_ATMEL_EEPROM_376KHZ:
12174 case FLASH_5787VENDOR_MICRO_EEPROM_64KHZ:
12175 case FLASH_5787VENDOR_MICRO_EEPROM_376KHZ:
12176 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012177 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000012178 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
Michael Chan1b277772006-03-20 22:27:48 -080012179
Matt Carlson8590a602009-08-28 12:29:16 +000012180 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
12181 tw32(NVRAM_CFG1, nvcfg1);
12182 break;
12183 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
12184 case FLASH_5755VENDOR_ATMEL_FLASH_1:
12185 case FLASH_5755VENDOR_ATMEL_FLASH_2:
12186 case FLASH_5755VENDOR_ATMEL_FLASH_3:
12187 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012188 tg3_flag_set(tp, NVRAM_BUFFERED);
12189 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000012190 tp->nvram_pagesize = 264;
12191 break;
12192 case FLASH_5752VENDOR_ST_M45PE10:
12193 case FLASH_5752VENDOR_ST_M45PE20:
12194 case FLASH_5752VENDOR_ST_M45PE40:
12195 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000012196 tg3_flag_set(tp, NVRAM_BUFFERED);
12197 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000012198 tp->nvram_pagesize = 256;
12199 break;
Michael Chan1b277772006-03-20 22:27:48 -080012200 }
12201}
12202
Matt Carlson6b91fa02007-10-10 18:01:09 -070012203static void __devinit tg3_get_5761_nvram_info(struct tg3 *tp)
12204{
12205 u32 nvcfg1, protect = 0;
12206
12207 nvcfg1 = tr32(NVRAM_CFG1);
12208
12209 /* NVRAM protection for TPM */
12210 if (nvcfg1 & (1 << 27)) {
Joe Perches63c3a662011-04-26 08:12:10 +000012211 tg3_flag_set(tp, PROTECTED_NVRAM);
Matt Carlson6b91fa02007-10-10 18:01:09 -070012212 protect = 1;
12213 }
12214
12215 nvcfg1 &= NVRAM_CFG1_5752VENDOR_MASK;
12216 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000012217 case FLASH_5761VENDOR_ATMEL_ADB021D:
12218 case FLASH_5761VENDOR_ATMEL_ADB041D:
12219 case FLASH_5761VENDOR_ATMEL_ADB081D:
12220 case FLASH_5761VENDOR_ATMEL_ADB161D:
12221 case FLASH_5761VENDOR_ATMEL_MDB021D:
12222 case FLASH_5761VENDOR_ATMEL_MDB041D:
12223 case FLASH_5761VENDOR_ATMEL_MDB081D:
12224 case FLASH_5761VENDOR_ATMEL_MDB161D:
12225 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012226 tg3_flag_set(tp, NVRAM_BUFFERED);
12227 tg3_flag_set(tp, FLASH);
12228 tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
Matt Carlson8590a602009-08-28 12:29:16 +000012229 tp->nvram_pagesize = 256;
12230 break;
12231 case FLASH_5761VENDOR_ST_A_M45PE20:
12232 case FLASH_5761VENDOR_ST_A_M45PE40:
12233 case FLASH_5761VENDOR_ST_A_M45PE80:
12234 case FLASH_5761VENDOR_ST_A_M45PE16:
12235 case FLASH_5761VENDOR_ST_M_M45PE20:
12236 case FLASH_5761VENDOR_ST_M_M45PE40:
12237 case FLASH_5761VENDOR_ST_M_M45PE80:
12238 case FLASH_5761VENDOR_ST_M_M45PE16:
12239 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000012240 tg3_flag_set(tp, NVRAM_BUFFERED);
12241 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000012242 tp->nvram_pagesize = 256;
12243 break;
Matt Carlson6b91fa02007-10-10 18:01:09 -070012244 }
12245
12246 if (protect) {
12247 tp->nvram_size = tr32(NVRAM_ADDR_LOCKOUT);
12248 } else {
12249 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000012250 case FLASH_5761VENDOR_ATMEL_ADB161D:
12251 case FLASH_5761VENDOR_ATMEL_MDB161D:
12252 case FLASH_5761VENDOR_ST_A_M45PE16:
12253 case FLASH_5761VENDOR_ST_M_M45PE16:
12254 tp->nvram_size = TG3_NVRAM_SIZE_2MB;
12255 break;
12256 case FLASH_5761VENDOR_ATMEL_ADB081D:
12257 case FLASH_5761VENDOR_ATMEL_MDB081D:
12258 case FLASH_5761VENDOR_ST_A_M45PE80:
12259 case FLASH_5761VENDOR_ST_M_M45PE80:
12260 tp->nvram_size = TG3_NVRAM_SIZE_1MB;
12261 break;
12262 case FLASH_5761VENDOR_ATMEL_ADB041D:
12263 case FLASH_5761VENDOR_ATMEL_MDB041D:
12264 case FLASH_5761VENDOR_ST_A_M45PE40:
12265 case FLASH_5761VENDOR_ST_M_M45PE40:
12266 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
12267 break;
12268 case FLASH_5761VENDOR_ATMEL_ADB021D:
12269 case FLASH_5761VENDOR_ATMEL_MDB021D:
12270 case FLASH_5761VENDOR_ST_A_M45PE20:
12271 case FLASH_5761VENDOR_ST_M_M45PE20:
12272 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
12273 break;
Matt Carlson6b91fa02007-10-10 18:01:09 -070012274 }
12275 }
12276}
12277
Michael Chanb5d37722006-09-27 16:06:21 -070012278static void __devinit tg3_get_5906_nvram_info(struct tg3 *tp)
12279{
12280 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012281 tg3_flag_set(tp, NVRAM_BUFFERED);
Michael Chanb5d37722006-09-27 16:06:21 -070012282 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
12283}
12284
Matt Carlson321d32a2008-11-21 17:22:19 -080012285static void __devinit tg3_get_57780_nvram_info(struct tg3 *tp)
12286{
12287 u32 nvcfg1;
12288
12289 nvcfg1 = tr32(NVRAM_CFG1);
12290
12291 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
12292 case FLASH_5787VENDOR_ATMEL_EEPROM_376KHZ:
12293 case FLASH_5787VENDOR_MICRO_EEPROM_376KHZ:
12294 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012295 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson321d32a2008-11-21 17:22:19 -080012296 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
12297
12298 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
12299 tw32(NVRAM_CFG1, nvcfg1);
12300 return;
12301 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
12302 case FLASH_57780VENDOR_ATMEL_AT45DB011D:
12303 case FLASH_57780VENDOR_ATMEL_AT45DB011B:
12304 case FLASH_57780VENDOR_ATMEL_AT45DB021D:
12305 case FLASH_57780VENDOR_ATMEL_AT45DB021B:
12306 case FLASH_57780VENDOR_ATMEL_AT45DB041D:
12307 case FLASH_57780VENDOR_ATMEL_AT45DB041B:
12308 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012309 tg3_flag_set(tp, NVRAM_BUFFERED);
12310 tg3_flag_set(tp, FLASH);
Matt Carlson321d32a2008-11-21 17:22:19 -080012311
12312 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
12313 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
12314 case FLASH_57780VENDOR_ATMEL_AT45DB011D:
12315 case FLASH_57780VENDOR_ATMEL_AT45DB011B:
12316 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
12317 break;
12318 case FLASH_57780VENDOR_ATMEL_AT45DB021D:
12319 case FLASH_57780VENDOR_ATMEL_AT45DB021B:
12320 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
12321 break;
12322 case FLASH_57780VENDOR_ATMEL_AT45DB041D:
12323 case FLASH_57780VENDOR_ATMEL_AT45DB041B:
12324 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
12325 break;
12326 }
12327 break;
12328 case FLASH_5752VENDOR_ST_M45PE10:
12329 case FLASH_5752VENDOR_ST_M45PE20:
12330 case FLASH_5752VENDOR_ST_M45PE40:
12331 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000012332 tg3_flag_set(tp, NVRAM_BUFFERED);
12333 tg3_flag_set(tp, FLASH);
Matt Carlson321d32a2008-11-21 17:22:19 -080012334
12335 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
12336 case FLASH_5752VENDOR_ST_M45PE10:
12337 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
12338 break;
12339 case FLASH_5752VENDOR_ST_M45PE20:
12340 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
12341 break;
12342 case FLASH_5752VENDOR_ST_M45PE40:
12343 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
12344 break;
12345 }
12346 break;
12347 default:
Joe Perches63c3a662011-04-26 08:12:10 +000012348 tg3_flag_set(tp, NO_NVRAM);
Matt Carlson321d32a2008-11-21 17:22:19 -080012349 return;
12350 }
12351
Matt Carlsona1b950d2009-09-01 13:20:17 +000012352 tg3_nvram_get_pagesize(tp, nvcfg1);
12353 if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
Joe Perches63c3a662011-04-26 08:12:10 +000012354 tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
Matt Carlsona1b950d2009-09-01 13:20:17 +000012355}
12356
12357
12358static void __devinit tg3_get_5717_nvram_info(struct tg3 *tp)
12359{
12360 u32 nvcfg1;
12361
12362 nvcfg1 = tr32(NVRAM_CFG1);
12363
12364 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
12365 case FLASH_5717VENDOR_ATMEL_EEPROM:
12366 case FLASH_5717VENDOR_MICRO_EEPROM:
12367 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012368 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlsona1b950d2009-09-01 13:20:17 +000012369 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
12370
12371 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
12372 tw32(NVRAM_CFG1, nvcfg1);
12373 return;
12374 case FLASH_5717VENDOR_ATMEL_MDB011D:
12375 case FLASH_5717VENDOR_ATMEL_ADB011B:
12376 case FLASH_5717VENDOR_ATMEL_ADB011D:
12377 case FLASH_5717VENDOR_ATMEL_MDB021D:
12378 case FLASH_5717VENDOR_ATMEL_ADB021B:
12379 case FLASH_5717VENDOR_ATMEL_ADB021D:
12380 case FLASH_5717VENDOR_ATMEL_45USPT:
12381 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012382 tg3_flag_set(tp, NVRAM_BUFFERED);
12383 tg3_flag_set(tp, FLASH);
Matt Carlsona1b950d2009-09-01 13:20:17 +000012384
12385 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
12386 case FLASH_5717VENDOR_ATMEL_MDB021D:
Matt Carlson66ee33b2011-04-05 14:22:51 +000012387 /* Detect size with tg3_nvram_get_size() */
12388 break;
Matt Carlsona1b950d2009-09-01 13:20:17 +000012389 case FLASH_5717VENDOR_ATMEL_ADB021B:
12390 case FLASH_5717VENDOR_ATMEL_ADB021D:
12391 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
12392 break;
12393 default:
12394 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
12395 break;
12396 }
Matt Carlson321d32a2008-11-21 17:22:19 -080012397 break;
Matt Carlsona1b950d2009-09-01 13:20:17 +000012398 case FLASH_5717VENDOR_ST_M_M25PE10:
12399 case FLASH_5717VENDOR_ST_A_M25PE10:
12400 case FLASH_5717VENDOR_ST_M_M45PE10:
12401 case FLASH_5717VENDOR_ST_A_M45PE10:
12402 case FLASH_5717VENDOR_ST_M_M25PE20:
12403 case FLASH_5717VENDOR_ST_A_M25PE20:
12404 case FLASH_5717VENDOR_ST_M_M45PE20:
12405 case FLASH_5717VENDOR_ST_A_M45PE20:
12406 case FLASH_5717VENDOR_ST_25USPT:
12407 case FLASH_5717VENDOR_ST_45USPT:
12408 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000012409 tg3_flag_set(tp, NVRAM_BUFFERED);
12410 tg3_flag_set(tp, FLASH);
Matt Carlsona1b950d2009-09-01 13:20:17 +000012411
12412 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
12413 case FLASH_5717VENDOR_ST_M_M25PE20:
Matt Carlsona1b950d2009-09-01 13:20:17 +000012414 case FLASH_5717VENDOR_ST_M_M45PE20:
Matt Carlson66ee33b2011-04-05 14:22:51 +000012415 /* Detect size with tg3_nvram_get_size() */
12416 break;
12417 case FLASH_5717VENDOR_ST_A_M25PE20:
Matt Carlsona1b950d2009-09-01 13:20:17 +000012418 case FLASH_5717VENDOR_ST_A_M45PE20:
12419 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
12420 break;
12421 default:
12422 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
12423 break;
12424 }
Matt Carlson321d32a2008-11-21 17:22:19 -080012425 break;
Matt Carlsona1b950d2009-09-01 13:20:17 +000012426 default:
Joe Perches63c3a662011-04-26 08:12:10 +000012427 tg3_flag_set(tp, NO_NVRAM);
Matt Carlsona1b950d2009-09-01 13:20:17 +000012428 return;
Matt Carlson321d32a2008-11-21 17:22:19 -080012429 }
Matt Carlsona1b950d2009-09-01 13:20:17 +000012430
12431 tg3_nvram_get_pagesize(tp, nvcfg1);
12432 if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
Joe Perches63c3a662011-04-26 08:12:10 +000012433 tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
Matt Carlson321d32a2008-11-21 17:22:19 -080012434}
12435
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012436static void __devinit tg3_get_5720_nvram_info(struct tg3 *tp)
12437{
12438 u32 nvcfg1, nvmpinstrp;
12439
12440 nvcfg1 = tr32(NVRAM_CFG1);
12441 nvmpinstrp = nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK;
12442
12443 switch (nvmpinstrp) {
12444 case FLASH_5720_EEPROM_HD:
12445 case FLASH_5720_EEPROM_LD:
12446 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012447 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012448
12449 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
12450 tw32(NVRAM_CFG1, nvcfg1);
12451 if (nvmpinstrp == FLASH_5720_EEPROM_HD)
12452 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
12453 else
12454 tp->nvram_pagesize = ATMEL_AT24C02_CHIP_SIZE;
12455 return;
12456 case FLASH_5720VENDOR_M_ATMEL_DB011D:
12457 case FLASH_5720VENDOR_A_ATMEL_DB011B:
12458 case FLASH_5720VENDOR_A_ATMEL_DB011D:
12459 case FLASH_5720VENDOR_M_ATMEL_DB021D:
12460 case FLASH_5720VENDOR_A_ATMEL_DB021B:
12461 case FLASH_5720VENDOR_A_ATMEL_DB021D:
12462 case FLASH_5720VENDOR_M_ATMEL_DB041D:
12463 case FLASH_5720VENDOR_A_ATMEL_DB041B:
12464 case FLASH_5720VENDOR_A_ATMEL_DB041D:
12465 case FLASH_5720VENDOR_M_ATMEL_DB081D:
12466 case FLASH_5720VENDOR_A_ATMEL_DB081D:
12467 case FLASH_5720VENDOR_ATMEL_45USPT:
12468 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012469 tg3_flag_set(tp, NVRAM_BUFFERED);
12470 tg3_flag_set(tp, FLASH);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012471
12472 switch (nvmpinstrp) {
12473 case FLASH_5720VENDOR_M_ATMEL_DB021D:
12474 case FLASH_5720VENDOR_A_ATMEL_DB021B:
12475 case FLASH_5720VENDOR_A_ATMEL_DB021D:
12476 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
12477 break;
12478 case FLASH_5720VENDOR_M_ATMEL_DB041D:
12479 case FLASH_5720VENDOR_A_ATMEL_DB041B:
12480 case FLASH_5720VENDOR_A_ATMEL_DB041D:
12481 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
12482 break;
12483 case FLASH_5720VENDOR_M_ATMEL_DB081D:
12484 case FLASH_5720VENDOR_A_ATMEL_DB081D:
12485 tp->nvram_size = TG3_NVRAM_SIZE_1MB;
12486 break;
12487 default:
12488 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
12489 break;
12490 }
12491 break;
12492 case FLASH_5720VENDOR_M_ST_M25PE10:
12493 case FLASH_5720VENDOR_M_ST_M45PE10:
12494 case FLASH_5720VENDOR_A_ST_M25PE10:
12495 case FLASH_5720VENDOR_A_ST_M45PE10:
12496 case FLASH_5720VENDOR_M_ST_M25PE20:
12497 case FLASH_5720VENDOR_M_ST_M45PE20:
12498 case FLASH_5720VENDOR_A_ST_M25PE20:
12499 case FLASH_5720VENDOR_A_ST_M45PE20:
12500 case FLASH_5720VENDOR_M_ST_M25PE40:
12501 case FLASH_5720VENDOR_M_ST_M45PE40:
12502 case FLASH_5720VENDOR_A_ST_M25PE40:
12503 case FLASH_5720VENDOR_A_ST_M45PE40:
12504 case FLASH_5720VENDOR_M_ST_M25PE80:
12505 case FLASH_5720VENDOR_M_ST_M45PE80:
12506 case FLASH_5720VENDOR_A_ST_M25PE80:
12507 case FLASH_5720VENDOR_A_ST_M45PE80:
12508 case FLASH_5720VENDOR_ST_25USPT:
12509 case FLASH_5720VENDOR_ST_45USPT:
12510 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000012511 tg3_flag_set(tp, NVRAM_BUFFERED);
12512 tg3_flag_set(tp, FLASH);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012513
12514 switch (nvmpinstrp) {
12515 case FLASH_5720VENDOR_M_ST_M25PE20:
12516 case FLASH_5720VENDOR_M_ST_M45PE20:
12517 case FLASH_5720VENDOR_A_ST_M25PE20:
12518 case FLASH_5720VENDOR_A_ST_M45PE20:
12519 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
12520 break;
12521 case FLASH_5720VENDOR_M_ST_M25PE40:
12522 case FLASH_5720VENDOR_M_ST_M45PE40:
12523 case FLASH_5720VENDOR_A_ST_M25PE40:
12524 case FLASH_5720VENDOR_A_ST_M45PE40:
12525 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
12526 break;
12527 case FLASH_5720VENDOR_M_ST_M25PE80:
12528 case FLASH_5720VENDOR_M_ST_M45PE80:
12529 case FLASH_5720VENDOR_A_ST_M25PE80:
12530 case FLASH_5720VENDOR_A_ST_M45PE80:
12531 tp->nvram_size = TG3_NVRAM_SIZE_1MB;
12532 break;
12533 default:
12534 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
12535 break;
12536 }
12537 break;
12538 default:
Joe Perches63c3a662011-04-26 08:12:10 +000012539 tg3_flag_set(tp, NO_NVRAM);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012540 return;
12541 }
12542
12543 tg3_nvram_get_pagesize(tp, nvcfg1);
12544 if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
Joe Perches63c3a662011-04-26 08:12:10 +000012545 tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012546}
12547
Linus Torvalds1da177e2005-04-16 15:20:36 -070012548/* Chips other than 5700/5701 use the NVRAM for fetching info. */
12549static void __devinit tg3_nvram_init(struct tg3 *tp)
12550{
Linus Torvalds1da177e2005-04-16 15:20:36 -070012551 tw32_f(GRC_EEPROM_ADDR,
12552 (EEPROM_ADDR_FSM_RESET |
12553 (EEPROM_DEFAULT_CLOCK_PERIOD <<
12554 EEPROM_ADDR_CLKPERD_SHIFT)));
12555
Michael Chan9d57f012006-12-07 00:23:25 -080012556 msleep(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012557
12558 /* Enable seeprom accesses. */
12559 tw32_f(GRC_LOCAL_CTRL,
12560 tr32(GRC_LOCAL_CTRL) | GRC_LCLCTRL_AUTO_SEEPROM);
12561 udelay(100);
12562
12563 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
12564 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701) {
Joe Perches63c3a662011-04-26 08:12:10 +000012565 tg3_flag_set(tp, NVRAM);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012566
Michael Chanec41c7d2006-01-17 02:40:55 -080012567 if (tg3_nvram_lock(tp)) {
Matt Carlson5129c3a2010-04-05 10:19:23 +000012568 netdev_warn(tp->dev,
12569 "Cannot get nvram lock, %s failed\n",
Joe Perches05dbe002010-02-17 19:44:19 +000012570 __func__);
Michael Chanec41c7d2006-01-17 02:40:55 -080012571 return;
12572 }
Michael Chane6af3012005-04-21 17:12:05 -070012573 tg3_enable_nvram_access(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012574
Matt Carlson989a9d22007-05-05 11:51:05 -070012575 tp->nvram_size = 0;
12576
Michael Chan361b4ac2005-04-21 17:11:21 -070012577 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752)
12578 tg3_get_5752_nvram_info(tp);
Michael Chand3c7b882006-03-23 01:28:25 -080012579 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755)
12580 tg3_get_5755_nvram_info(tp);
Matt Carlsond30cdd22007-10-07 23:28:35 -070012581 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787 ||
Matt Carlson57e69832008-05-25 23:48:31 -070012582 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
12583 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
Michael Chan1b277772006-03-20 22:27:48 -080012584 tg3_get_5787_nvram_info(tp);
Matt Carlson6b91fa02007-10-10 18:01:09 -070012585 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
12586 tg3_get_5761_nvram_info(tp);
Michael Chanb5d37722006-09-27 16:06:21 -070012587 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
12588 tg3_get_5906_nvram_info(tp);
Matt Carlsonb703df62009-12-03 08:36:21 +000012589 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
12590 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Matt Carlson321d32a2008-11-21 17:22:19 -080012591 tg3_get_57780_nvram_info(tp);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012592 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
12593 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
Matt Carlsona1b950d2009-09-01 13:20:17 +000012594 tg3_get_5717_nvram_info(tp);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012595 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
12596 tg3_get_5720_nvram_info(tp);
Michael Chan361b4ac2005-04-21 17:11:21 -070012597 else
12598 tg3_get_nvram_info(tp);
12599
Matt Carlson989a9d22007-05-05 11:51:05 -070012600 if (tp->nvram_size == 0)
12601 tg3_get_nvram_size(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012602
Michael Chane6af3012005-04-21 17:12:05 -070012603 tg3_disable_nvram_access(tp);
Michael Chan381291b2005-12-13 21:08:21 -080012604 tg3_nvram_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012605
12606 } else {
Joe Perches63c3a662011-04-26 08:12:10 +000012607 tg3_flag_clear(tp, NVRAM);
12608 tg3_flag_clear(tp, NVRAM_BUFFERED);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012609
12610 tg3_get_eeprom_size(tp);
12611 }
12612}
12613
Linus Torvalds1da177e2005-04-16 15:20:36 -070012614static int tg3_nvram_write_block_using_eeprom(struct tg3 *tp,
12615 u32 offset, u32 len, u8 *buf)
12616{
12617 int i, j, rc = 0;
12618 u32 val;
12619
12620 for (i = 0; i < len; i += 4) {
Al Virob9fc7dc2007-12-17 22:59:57 -080012621 u32 addr;
Matt Carlsona9dc5292009-02-25 14:25:30 +000012622 __be32 data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012623
12624 addr = offset + i;
12625
12626 memcpy(&data, buf + i, 4);
12627
Matt Carlson62cedd12009-04-20 14:52:29 -070012628 /*
12629 * The SEEPROM interface expects the data to always be opposite
12630 * the native endian format. We accomplish this by reversing
12631 * all the operations that would have been performed on the
12632 * data from a call to tg3_nvram_read_be32().
12633 */
12634 tw32(GRC_EEPROM_DATA, swab32(be32_to_cpu(data)));
Linus Torvalds1da177e2005-04-16 15:20:36 -070012635
12636 val = tr32(GRC_EEPROM_ADDR);
12637 tw32(GRC_EEPROM_ADDR, val | EEPROM_ADDR_COMPLETE);
12638
12639 val &= ~(EEPROM_ADDR_ADDR_MASK | EEPROM_ADDR_DEVID_MASK |
12640 EEPROM_ADDR_READ);
12641 tw32(GRC_EEPROM_ADDR, val |
12642 (0 << EEPROM_ADDR_DEVID_SHIFT) |
12643 (addr & EEPROM_ADDR_ADDR_MASK) |
12644 EEPROM_ADDR_START |
12645 EEPROM_ADDR_WRITE);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012646
Michael Chan9d57f012006-12-07 00:23:25 -080012647 for (j = 0; j < 1000; j++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012648 val = tr32(GRC_EEPROM_ADDR);
12649
12650 if (val & EEPROM_ADDR_COMPLETE)
12651 break;
Michael Chan9d57f012006-12-07 00:23:25 -080012652 msleep(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012653 }
12654 if (!(val & EEPROM_ADDR_COMPLETE)) {
12655 rc = -EBUSY;
12656 break;
12657 }
12658 }
12659
12660 return rc;
12661}
12662
12663/* offset and length are dword aligned */
12664static int tg3_nvram_write_block_unbuffered(struct tg3 *tp, u32 offset, u32 len,
12665 u8 *buf)
12666{
12667 int ret = 0;
12668 u32 pagesize = tp->nvram_pagesize;
12669 u32 pagemask = pagesize - 1;
12670 u32 nvram_cmd;
12671 u8 *tmp;
12672
12673 tmp = kmalloc(pagesize, GFP_KERNEL);
12674 if (tmp == NULL)
12675 return -ENOMEM;
12676
12677 while (len) {
12678 int j;
Michael Chane6af3012005-04-21 17:12:05 -070012679 u32 phy_addr, page_off, size;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012680
12681 phy_addr = offset & ~pagemask;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012682
Linus Torvalds1da177e2005-04-16 15:20:36 -070012683 for (j = 0; j < pagesize; j += 4) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000012684 ret = tg3_nvram_read_be32(tp, phy_addr + j,
12685 (__be32 *) (tmp + j));
12686 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012687 break;
12688 }
12689 if (ret)
12690 break;
12691
Matt Carlsonc6cdf432010-04-05 10:19:26 +000012692 page_off = offset & pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012693 size = pagesize;
12694 if (len < size)
12695 size = len;
12696
12697 len -= size;
12698
12699 memcpy(tmp + page_off, buf, size);
12700
12701 offset = offset + (pagesize - page_off);
12702
Michael Chane6af3012005-04-21 17:12:05 -070012703 tg3_enable_nvram_access(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012704
12705 /*
12706 * Before we can erase the flash page, we need
12707 * to issue a special "write enable" command.
12708 */
12709 nvram_cmd = NVRAM_CMD_WREN | NVRAM_CMD_GO | NVRAM_CMD_DONE;
12710
12711 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
12712 break;
12713
12714 /* Erase the target page */
12715 tw32(NVRAM_ADDR, phy_addr);
12716
12717 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE | NVRAM_CMD_WR |
12718 NVRAM_CMD_FIRST | NVRAM_CMD_LAST | NVRAM_CMD_ERASE;
12719
Matt Carlsonc6cdf432010-04-05 10:19:26 +000012720 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
Linus Torvalds1da177e2005-04-16 15:20:36 -070012721 break;
12722
12723 /* Issue another write enable to start the write. */
12724 nvram_cmd = NVRAM_CMD_WREN | NVRAM_CMD_GO | NVRAM_CMD_DONE;
12725
12726 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
12727 break;
12728
12729 for (j = 0; j < pagesize; j += 4) {
Al Virob9fc7dc2007-12-17 22:59:57 -080012730 __be32 data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012731
Al Virob9fc7dc2007-12-17 22:59:57 -080012732 data = *((__be32 *) (tmp + j));
Matt Carlsona9dc5292009-02-25 14:25:30 +000012733
Al Virob9fc7dc2007-12-17 22:59:57 -080012734 tw32(NVRAM_WRDATA, be32_to_cpu(data));
Linus Torvalds1da177e2005-04-16 15:20:36 -070012735
12736 tw32(NVRAM_ADDR, phy_addr + j);
12737
12738 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE |
12739 NVRAM_CMD_WR;
12740
12741 if (j == 0)
12742 nvram_cmd |= NVRAM_CMD_FIRST;
12743 else if (j == (pagesize - 4))
12744 nvram_cmd |= NVRAM_CMD_LAST;
12745
12746 if ((ret = tg3_nvram_exec_cmd(tp, nvram_cmd)))
12747 break;
12748 }
12749 if (ret)
12750 break;
12751 }
12752
12753 nvram_cmd = NVRAM_CMD_WRDI | NVRAM_CMD_GO | NVRAM_CMD_DONE;
12754 tg3_nvram_exec_cmd(tp, nvram_cmd);
12755
12756 kfree(tmp);
12757
12758 return ret;
12759}
12760
12761/* offset and length are dword aligned */
12762static int tg3_nvram_write_block_buffered(struct tg3 *tp, u32 offset, u32 len,
12763 u8 *buf)
12764{
12765 int i, ret = 0;
12766
12767 for (i = 0; i < len; i += 4, offset += 4) {
Al Virob9fc7dc2007-12-17 22:59:57 -080012768 u32 page_off, phy_addr, nvram_cmd;
12769 __be32 data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012770
12771 memcpy(&data, buf + i, 4);
Al Virob9fc7dc2007-12-17 22:59:57 -080012772 tw32(NVRAM_WRDATA, be32_to_cpu(data));
Linus Torvalds1da177e2005-04-16 15:20:36 -070012773
Matt Carlsonc6cdf432010-04-05 10:19:26 +000012774 page_off = offset % tp->nvram_pagesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012775
Michael Chan18201802006-03-20 22:29:15 -080012776 phy_addr = tg3_nvram_phys_addr(tp, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012777
12778 tw32(NVRAM_ADDR, phy_addr);
12779
12780 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE | NVRAM_CMD_WR;
12781
Matt Carlsonc6cdf432010-04-05 10:19:26 +000012782 if (page_off == 0 || i == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012783 nvram_cmd |= NVRAM_CMD_FIRST;
Michael Chanf6d9a252006-04-29 19:00:24 -070012784 if (page_off == (tp->nvram_pagesize - 4))
Linus Torvalds1da177e2005-04-16 15:20:36 -070012785 nvram_cmd |= NVRAM_CMD_LAST;
12786
12787 if (i == (len - 4))
12788 nvram_cmd |= NVRAM_CMD_LAST;
12789
Matt Carlson321d32a2008-11-21 17:22:19 -080012790 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5752 &&
Joe Perches63c3a662011-04-26 08:12:10 +000012791 !tg3_flag(tp, 5755_PLUS) &&
Michael Chan4c987482005-09-05 17:52:38 -070012792 (tp->nvram_jedecnum == JEDEC_ST) &&
12793 (nvram_cmd & NVRAM_CMD_FIRST)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012794
12795 if ((ret = tg3_nvram_exec_cmd(tp,
12796 NVRAM_CMD_WREN | NVRAM_CMD_GO |
12797 NVRAM_CMD_DONE)))
12798
12799 break;
12800 }
Joe Perches63c3a662011-04-26 08:12:10 +000012801 if (!tg3_flag(tp, FLASH)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012802 /* We always do complete word writes to eeprom. */
12803 nvram_cmd |= (NVRAM_CMD_FIRST | NVRAM_CMD_LAST);
12804 }
12805
12806 if ((ret = tg3_nvram_exec_cmd(tp, nvram_cmd)))
12807 break;
12808 }
12809 return ret;
12810}
12811
12812/* offset and length are dword aligned */
12813static int tg3_nvram_write_block(struct tg3 *tp, u32 offset, u32 len, u8 *buf)
12814{
12815 int ret;
12816
Joe Perches63c3a662011-04-26 08:12:10 +000012817 if (tg3_flag(tp, EEPROM_WRITE_PROT)) {
Michael Chan314fba32005-04-21 17:07:04 -070012818 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl &
12819 ~GRC_LCLCTRL_GPIO_OUTPUT1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012820 udelay(40);
12821 }
12822
Joe Perches63c3a662011-04-26 08:12:10 +000012823 if (!tg3_flag(tp, NVRAM)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012824 ret = tg3_nvram_write_block_using_eeprom(tp, offset, len, buf);
Matt Carlson859a5882010-04-05 10:19:28 +000012825 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012826 u32 grc_mode;
12827
Michael Chanec41c7d2006-01-17 02:40:55 -080012828 ret = tg3_nvram_lock(tp);
12829 if (ret)
12830 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012831
Michael Chane6af3012005-04-21 17:12:05 -070012832 tg3_enable_nvram_access(tp);
Joe Perches63c3a662011-04-26 08:12:10 +000012833 if (tg3_flag(tp, 5750_PLUS) && !tg3_flag(tp, PROTECTED_NVRAM))
Linus Torvalds1da177e2005-04-16 15:20:36 -070012834 tw32(NVRAM_WRITE1, 0x406);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012835
12836 grc_mode = tr32(GRC_MODE);
12837 tw32(GRC_MODE, grc_mode | GRC_MODE_NVRAM_WR_ENABLE);
12838
Joe Perches63c3a662011-04-26 08:12:10 +000012839 if (tg3_flag(tp, NVRAM_BUFFERED) || !tg3_flag(tp, FLASH)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012840 ret = tg3_nvram_write_block_buffered(tp, offset, len,
12841 buf);
Matt Carlson859a5882010-04-05 10:19:28 +000012842 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012843 ret = tg3_nvram_write_block_unbuffered(tp, offset, len,
12844 buf);
12845 }
12846
12847 grc_mode = tr32(GRC_MODE);
12848 tw32(GRC_MODE, grc_mode & ~GRC_MODE_NVRAM_WR_ENABLE);
12849
Michael Chane6af3012005-04-21 17:12:05 -070012850 tg3_disable_nvram_access(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012851 tg3_nvram_unlock(tp);
12852 }
12853
Joe Perches63c3a662011-04-26 08:12:10 +000012854 if (tg3_flag(tp, EEPROM_WRITE_PROT)) {
Michael Chan314fba32005-04-21 17:07:04 -070012855 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012856 udelay(40);
12857 }
12858
12859 return ret;
12860}
12861
12862struct subsys_tbl_ent {
12863 u16 subsys_vendor, subsys_devid;
12864 u32 phy_id;
12865};
12866
Matt Carlson24daf2b2010-02-17 15:17:02 +000012867static struct subsys_tbl_ent subsys_id_to_phy_id[] __devinitdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012868 /* Broadcom boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000012869 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012870 TG3PCI_SUBDEVICE_ID_BROADCOM_95700A6, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012871 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012872 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A5, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012873 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012874 TG3PCI_SUBDEVICE_ID_BROADCOM_95700T6, TG3_PHY_ID_BCM8002 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012875 { TG3PCI_SUBVENDOR_ID_BROADCOM,
12876 TG3PCI_SUBDEVICE_ID_BROADCOM_95700A9, 0 },
12877 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012878 TG3PCI_SUBDEVICE_ID_BROADCOM_95701T1, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012879 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012880 TG3PCI_SUBDEVICE_ID_BROADCOM_95701T8, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012881 { TG3PCI_SUBVENDOR_ID_BROADCOM,
12882 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A7, 0 },
12883 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012884 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A10, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012885 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012886 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A12, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012887 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012888 TG3PCI_SUBDEVICE_ID_BROADCOM_95703AX1, TG3_PHY_ID_BCM5703 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012889 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012890 TG3PCI_SUBDEVICE_ID_BROADCOM_95703AX2, TG3_PHY_ID_BCM5703 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070012891
12892 /* 3com boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000012893 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012894 TG3PCI_SUBDEVICE_ID_3COM_3C996T, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012895 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012896 TG3PCI_SUBDEVICE_ID_3COM_3C996BT, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012897 { TG3PCI_SUBVENDOR_ID_3COM,
12898 TG3PCI_SUBDEVICE_ID_3COM_3C996SX, 0 },
12899 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012900 TG3PCI_SUBDEVICE_ID_3COM_3C1000T, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012901 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012902 TG3PCI_SUBDEVICE_ID_3COM_3C940BR01, TG3_PHY_ID_BCM5701 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070012903
12904 /* DELL boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000012905 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000012906 TG3PCI_SUBDEVICE_ID_DELL_VIPER, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012907 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000012908 TG3PCI_SUBDEVICE_ID_DELL_JAGUAR, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012909 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000012910 TG3PCI_SUBDEVICE_ID_DELL_MERLOT, TG3_PHY_ID_BCM5411 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012911 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000012912 TG3PCI_SUBDEVICE_ID_DELL_SLIM_MERLOT, TG3_PHY_ID_BCM5411 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070012913
12914 /* Compaq boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000012915 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000012916 TG3PCI_SUBDEVICE_ID_COMPAQ_BANSHEE, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012917 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000012918 TG3PCI_SUBDEVICE_ID_COMPAQ_BANSHEE_2, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012919 { TG3PCI_SUBVENDOR_ID_COMPAQ,
12920 TG3PCI_SUBDEVICE_ID_COMPAQ_CHANGELING, 0 },
12921 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000012922 TG3PCI_SUBDEVICE_ID_COMPAQ_NC7780, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012923 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000012924 TG3PCI_SUBDEVICE_ID_COMPAQ_NC7780_2, TG3_PHY_ID_BCM5701 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070012925
12926 /* IBM boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000012927 { TG3PCI_SUBVENDOR_ID_IBM,
12928 TG3PCI_SUBDEVICE_ID_IBM_5703SAX2, 0 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070012929};
12930
Matt Carlson24daf2b2010-02-17 15:17:02 +000012931static struct subsys_tbl_ent * __devinit tg3_lookup_by_subsys(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012932{
12933 int i;
12934
12935 for (i = 0; i < ARRAY_SIZE(subsys_id_to_phy_id); i++) {
12936 if ((subsys_id_to_phy_id[i].subsys_vendor ==
12937 tp->pdev->subsystem_vendor) &&
12938 (subsys_id_to_phy_id[i].subsys_devid ==
12939 tp->pdev->subsystem_device))
12940 return &subsys_id_to_phy_id[i];
12941 }
12942 return NULL;
12943}
12944
Michael Chan7d0c41e2005-04-21 17:06:20 -070012945static void __devinit tg3_get_eeprom_hw_cfg(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012946{
Linus Torvalds1da177e2005-04-16 15:20:36 -070012947 u32 val;
David S. Millerf49639e2006-06-09 11:58:36 -070012948
Matt Carlson79eb6902010-02-17 15:17:03 +000012949 tp->phy_id = TG3_PHY_ID_INVALID;
Michael Chan7d0c41e2005-04-21 17:06:20 -070012950 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
12951
Gary Zambranoa85feb82007-05-05 11:52:19 -070012952 /* Assume an onboard device and WOL capable by default. */
Joe Perches63c3a662011-04-26 08:12:10 +000012953 tg3_flag_set(tp, EEPROM_WRITE_PROT);
12954 tg3_flag_set(tp, WOL_CAP);
David S. Miller72b845e2006-03-14 14:11:48 -080012955
Michael Chanb5d37722006-09-27 16:06:21 -070012956 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan9d26e212006-12-07 00:21:14 -080012957 if (!(tr32(PCIE_TRANSACTION_CFG) & PCIE_TRANS_CFG_LOM)) {
Joe Perches63c3a662011-04-26 08:12:10 +000012958 tg3_flag_clear(tp, EEPROM_WRITE_PROT);
12959 tg3_flag_set(tp, IS_NIC);
Michael Chan9d26e212006-12-07 00:21:14 -080012960 }
Matt Carlson0527ba32007-10-10 18:03:30 -070012961 val = tr32(VCPU_CFGSHDW);
12962 if (val & VCPU_CFGSHDW_ASPM_DBNC)
Joe Perches63c3a662011-04-26 08:12:10 +000012963 tg3_flag_set(tp, ASPM_WORKAROUND);
Matt Carlson0527ba32007-10-10 18:03:30 -070012964 if ((val & VCPU_CFGSHDW_WOL_ENABLE) &&
Rafael J. Wysocki6fdbab92011-04-28 11:02:15 +000012965 (val & VCPU_CFGSHDW_WOL_MAGPKT)) {
Joe Perches63c3a662011-04-26 08:12:10 +000012966 tg3_flag_set(tp, WOL_ENABLE);
Rafael J. Wysocki6fdbab92011-04-28 11:02:15 +000012967 device_set_wakeup_enable(&tp->pdev->dev, true);
12968 }
Matt Carlson05ac4cb2008-11-03 16:53:46 -080012969 goto done;
Michael Chanb5d37722006-09-27 16:06:21 -070012970 }
12971
Linus Torvalds1da177e2005-04-16 15:20:36 -070012972 tg3_read_mem(tp, NIC_SRAM_DATA_SIG, &val);
12973 if (val == NIC_SRAM_DATA_SIG_MAGIC) {
12974 u32 nic_cfg, led_cfg;
Matt Carlsona9daf362008-05-25 23:49:44 -070012975 u32 nic_phy_id, ver, cfg2 = 0, cfg4 = 0, eeprom_phy_id;
Michael Chan7d0c41e2005-04-21 17:06:20 -070012976 int eeprom_phy_serdes = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012977
12978 tg3_read_mem(tp, NIC_SRAM_DATA_CFG, &nic_cfg);
12979 tp->nic_sram_data_cfg = nic_cfg;
12980
12981 tg3_read_mem(tp, NIC_SRAM_DATA_VER, &ver);
12982 ver >>= NIC_SRAM_DATA_VER_SHIFT;
Matt Carlson6ff6f812011-05-19 12:12:54 +000012983 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
12984 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701 &&
12985 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5703 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070012986 (ver > 0) && (ver < 0x100))
12987 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_2, &cfg2);
12988
Matt Carlsona9daf362008-05-25 23:49:44 -070012989 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
12990 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_4, &cfg4);
12991
Linus Torvalds1da177e2005-04-16 15:20:36 -070012992 if ((nic_cfg & NIC_SRAM_DATA_CFG_PHY_TYPE_MASK) ==
12993 NIC_SRAM_DATA_CFG_PHY_TYPE_FIBER)
12994 eeprom_phy_serdes = 1;
12995
12996 tg3_read_mem(tp, NIC_SRAM_DATA_PHY_ID, &nic_phy_id);
12997 if (nic_phy_id != 0) {
12998 u32 id1 = nic_phy_id & NIC_SRAM_DATA_PHY_ID1_MASK;
12999 u32 id2 = nic_phy_id & NIC_SRAM_DATA_PHY_ID2_MASK;
13000
13001 eeprom_phy_id = (id1 >> 16) << 10;
13002 eeprom_phy_id |= (id2 & 0xfc00) << 16;
13003 eeprom_phy_id |= (id2 & 0x03ff) << 0;
13004 } else
13005 eeprom_phy_id = 0;
13006
Michael Chan7d0c41e2005-04-21 17:06:20 -070013007 tp->phy_id = eeprom_phy_id;
Michael Chan747e8f82005-07-25 12:33:22 -070013008 if (eeprom_phy_serdes) {
Joe Perches63c3a662011-04-26 08:12:10 +000013009 if (!tg3_flag(tp, 5705_PLUS))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013010 tp->phy_flags |= TG3_PHYFLG_PHY_SERDES;
Matt Carlsona50d0792010-06-05 17:24:37 +000013011 else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013012 tp->phy_flags |= TG3_PHYFLG_MII_SERDES;
Michael Chan747e8f82005-07-25 12:33:22 -070013013 }
Michael Chan7d0c41e2005-04-21 17:06:20 -070013014
Joe Perches63c3a662011-04-26 08:12:10 +000013015 if (tg3_flag(tp, 5750_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -070013016 led_cfg = cfg2 & (NIC_SRAM_DATA_CFG_LED_MODE_MASK |
13017 SHASTA_EXT_LED_MODE_MASK);
John W. Linvillecbf46852005-04-21 17:01:29 -070013018 else
Linus Torvalds1da177e2005-04-16 15:20:36 -070013019 led_cfg = nic_cfg & NIC_SRAM_DATA_CFG_LED_MODE_MASK;
13020
13021 switch (led_cfg) {
13022 default:
13023 case NIC_SRAM_DATA_CFG_LED_MODE_PHY_1:
13024 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
13025 break;
13026
13027 case NIC_SRAM_DATA_CFG_LED_MODE_PHY_2:
13028 tp->led_ctrl = LED_CTRL_MODE_PHY_2;
13029 break;
13030
13031 case NIC_SRAM_DATA_CFG_LED_MODE_MAC:
13032 tp->led_ctrl = LED_CTRL_MODE_MAC;
Michael Chan9ba27792005-06-06 15:16:20 -070013033
13034 /* Default to PHY_1_MODE if 0 (MAC_MODE) is
13035 * read on some older 5700/5701 bootcode.
13036 */
13037 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
13038 ASIC_REV_5700 ||
13039 GET_ASIC_REV(tp->pci_chip_rev_id) ==
13040 ASIC_REV_5701)
13041 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
13042
Linus Torvalds1da177e2005-04-16 15:20:36 -070013043 break;
13044
13045 case SHASTA_EXT_LED_SHARED:
13046 tp->led_ctrl = LED_CTRL_MODE_SHARED;
13047 if (tp->pci_chip_rev_id != CHIPREV_ID_5750_A0 &&
13048 tp->pci_chip_rev_id != CHIPREV_ID_5750_A1)
13049 tp->led_ctrl |= (LED_CTRL_MODE_PHY_1 |
13050 LED_CTRL_MODE_PHY_2);
13051 break;
13052
13053 case SHASTA_EXT_LED_MAC:
13054 tp->led_ctrl = LED_CTRL_MODE_SHASTA_MAC;
13055 break;
13056
13057 case SHASTA_EXT_LED_COMBO:
13058 tp->led_ctrl = LED_CTRL_MODE_COMBO;
13059 if (tp->pci_chip_rev_id != CHIPREV_ID_5750_A0)
13060 tp->led_ctrl |= (LED_CTRL_MODE_PHY_1 |
13061 LED_CTRL_MODE_PHY_2);
13062 break;
13063
Stephen Hemminger855e1112008-04-16 16:37:28 -070013064 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013065
13066 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
13067 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) &&
13068 tp->pdev->subsystem_vendor == PCI_VENDOR_ID_DELL)
13069 tp->led_ctrl = LED_CTRL_MODE_PHY_2;
13070
Matt Carlsonb2a5c192008-04-03 21:44:44 -070013071 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX)
13072 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
Matt Carlson5f608912007-11-12 21:17:07 -080013073
Michael Chan9d26e212006-12-07 00:21:14 -080013074 if (nic_cfg & NIC_SRAM_DATA_CFG_EEPROM_WP) {
Joe Perches63c3a662011-04-26 08:12:10 +000013075 tg3_flag_set(tp, EEPROM_WRITE_PROT);
Michael Chan9d26e212006-12-07 00:21:14 -080013076 if ((tp->pdev->subsystem_vendor ==
13077 PCI_VENDOR_ID_ARIMA) &&
13078 (tp->pdev->subsystem_device == 0x205a ||
13079 tp->pdev->subsystem_device == 0x2063))
Joe Perches63c3a662011-04-26 08:12:10 +000013080 tg3_flag_clear(tp, EEPROM_WRITE_PROT);
Michael Chan9d26e212006-12-07 00:21:14 -080013081 } else {
Joe Perches63c3a662011-04-26 08:12:10 +000013082 tg3_flag_clear(tp, EEPROM_WRITE_PROT);
13083 tg3_flag_set(tp, IS_NIC);
Michael Chan9d26e212006-12-07 00:21:14 -080013084 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013085
13086 if (nic_cfg & NIC_SRAM_DATA_CFG_ASF_ENABLE) {
Joe Perches63c3a662011-04-26 08:12:10 +000013087 tg3_flag_set(tp, ENABLE_ASF);
13088 if (tg3_flag(tp, 5750_PLUS))
13089 tg3_flag_set(tp, ASF_NEW_HANDSHAKE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013090 }
Matt Carlsonb2b98d42008-11-03 16:52:32 -080013091
13092 if ((nic_cfg & NIC_SRAM_DATA_CFG_APE_ENABLE) &&
Joe Perches63c3a662011-04-26 08:12:10 +000013093 tg3_flag(tp, 5750_PLUS))
13094 tg3_flag_set(tp, ENABLE_APE);
Matt Carlsonb2b98d42008-11-03 16:52:32 -080013095
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013096 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES &&
Gary Zambranoa85feb82007-05-05 11:52:19 -070013097 !(nic_cfg & NIC_SRAM_DATA_CFG_FIBER_WOL))
Joe Perches63c3a662011-04-26 08:12:10 +000013098 tg3_flag_clear(tp, WOL_CAP);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013099
Joe Perches63c3a662011-04-26 08:12:10 +000013100 if (tg3_flag(tp, WOL_CAP) &&
Rafael J. Wysocki6fdbab92011-04-28 11:02:15 +000013101 (nic_cfg & NIC_SRAM_DATA_CFG_WOL_ENABLE)) {
Joe Perches63c3a662011-04-26 08:12:10 +000013102 tg3_flag_set(tp, WOL_ENABLE);
Rafael J. Wysocki6fdbab92011-04-28 11:02:15 +000013103 device_set_wakeup_enable(&tp->pdev->dev, true);
13104 }
Matt Carlson0527ba32007-10-10 18:03:30 -070013105
Linus Torvalds1da177e2005-04-16 15:20:36 -070013106 if (cfg2 & (1 << 17))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013107 tp->phy_flags |= TG3_PHYFLG_CAPACITIVE_COUPLING;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013108
13109 /* serdes signal pre-emphasis in register 0x590 set by */
13110 /* bootcode if bit 18 is set */
13111 if (cfg2 & (1 << 18))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013112 tp->phy_flags |= TG3_PHYFLG_SERDES_PREEMPHASIS;
Matt Carlson8ed5d972007-05-07 00:25:49 -070013113
Joe Perches63c3a662011-04-26 08:12:10 +000013114 if ((tg3_flag(tp, 57765_PLUS) ||
13115 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
13116 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX)) &&
Matt Carlson6833c042008-11-21 17:18:59 -080013117 (cfg2 & NIC_SRAM_DATA_CFG_2_APD_EN))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013118 tp->phy_flags |= TG3_PHYFLG_ENABLE_APD;
Matt Carlson6833c042008-11-21 17:18:59 -080013119
Joe Perches63c3a662011-04-26 08:12:10 +000013120 if (tg3_flag(tp, PCI_EXPRESS) &&
Matt Carlson8c69b1e2010-08-02 11:26:00 +000013121 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
Joe Perches63c3a662011-04-26 08:12:10 +000013122 !tg3_flag(tp, 57765_PLUS)) {
Matt Carlson8ed5d972007-05-07 00:25:49 -070013123 u32 cfg3;
13124
13125 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_3, &cfg3);
13126 if (cfg3 & NIC_SRAM_ASPM_DEBOUNCE)
Joe Perches63c3a662011-04-26 08:12:10 +000013127 tg3_flag_set(tp, ASPM_WORKAROUND);
Matt Carlson8ed5d972007-05-07 00:25:49 -070013128 }
Matt Carlsona9daf362008-05-25 23:49:44 -070013129
Matt Carlson14417062010-02-17 15:16:59 +000013130 if (cfg4 & NIC_SRAM_RGMII_INBAND_DISABLE)
Joe Perches63c3a662011-04-26 08:12:10 +000013131 tg3_flag_set(tp, RGMII_INBAND_DISABLE);
Matt Carlsona9daf362008-05-25 23:49:44 -070013132 if (cfg4 & NIC_SRAM_RGMII_EXT_IBND_RX_EN)
Joe Perches63c3a662011-04-26 08:12:10 +000013133 tg3_flag_set(tp, RGMII_EXT_IBND_RX_EN);
Matt Carlsona9daf362008-05-25 23:49:44 -070013134 if (cfg4 & NIC_SRAM_RGMII_EXT_IBND_TX_EN)
Joe Perches63c3a662011-04-26 08:12:10 +000013135 tg3_flag_set(tp, RGMII_EXT_IBND_TX_EN);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013136 }
Matt Carlson05ac4cb2008-11-03 16:53:46 -080013137done:
Joe Perches63c3a662011-04-26 08:12:10 +000013138 if (tg3_flag(tp, WOL_CAP))
Rafael J. Wysocki43067ed2011-02-10 06:53:09 +000013139 device_set_wakeup_enable(&tp->pdev->dev,
Joe Perches63c3a662011-04-26 08:12:10 +000013140 tg3_flag(tp, WOL_ENABLE));
Rafael J. Wysocki43067ed2011-02-10 06:53:09 +000013141 else
13142 device_set_wakeup_capable(&tp->pdev->dev, false);
Michael Chan7d0c41e2005-04-21 17:06:20 -070013143}
13144
Matt Carlsonb2a5c192008-04-03 21:44:44 -070013145static int __devinit tg3_issue_otp_command(struct tg3 *tp, u32 cmd)
13146{
13147 int i;
13148 u32 val;
13149
13150 tw32(OTP_CTRL, cmd | OTP_CTRL_OTP_CMD_START);
13151 tw32(OTP_CTRL, cmd);
13152
13153 /* Wait for up to 1 ms for command to execute. */
13154 for (i = 0; i < 100; i++) {
13155 val = tr32(OTP_STATUS);
13156 if (val & OTP_STATUS_CMD_DONE)
13157 break;
13158 udelay(10);
13159 }
13160
13161 return (val & OTP_STATUS_CMD_DONE) ? 0 : -EBUSY;
13162}
13163
13164/* Read the gphy configuration from the OTP region of the chip. The gphy
13165 * configuration is a 32-bit value that straddles the alignment boundary.
13166 * We do two 32-bit reads and then shift and merge the results.
13167 */
13168static u32 __devinit tg3_read_otp_phycfg(struct tg3 *tp)
13169{
13170 u32 bhalf_otp, thalf_otp;
13171
13172 tw32(OTP_MODE, OTP_MODE_OTP_THRU_GRC);
13173
13174 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_INIT))
13175 return 0;
13176
13177 tw32(OTP_ADDRESS, OTP_ADDRESS_MAGIC1);
13178
13179 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_READ))
13180 return 0;
13181
13182 thalf_otp = tr32(OTP_READ_DATA);
13183
13184 tw32(OTP_ADDRESS, OTP_ADDRESS_MAGIC2);
13185
13186 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_READ))
13187 return 0;
13188
13189 bhalf_otp = tr32(OTP_READ_DATA);
13190
13191 return ((thalf_otp & 0x0000ffff) << 16) | (bhalf_otp >> 16);
13192}
13193
Matt Carlsone256f8a2011-03-09 16:58:24 +000013194static void __devinit tg3_phy_init_link_config(struct tg3 *tp)
13195{
Hiroaki SHIMODA202ff1c2011-11-22 04:05:41 +000013196 u32 adv = ADVERTISED_Autoneg;
Matt Carlsone256f8a2011-03-09 16:58:24 +000013197
13198 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
13199 adv |= ADVERTISED_1000baseT_Half |
13200 ADVERTISED_1000baseT_Full;
13201
13202 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
13203 adv |= ADVERTISED_100baseT_Half |
13204 ADVERTISED_100baseT_Full |
13205 ADVERTISED_10baseT_Half |
13206 ADVERTISED_10baseT_Full |
13207 ADVERTISED_TP;
13208 else
13209 adv |= ADVERTISED_FIBRE;
13210
13211 tp->link_config.advertising = adv;
13212 tp->link_config.speed = SPEED_INVALID;
13213 tp->link_config.duplex = DUPLEX_INVALID;
13214 tp->link_config.autoneg = AUTONEG_ENABLE;
13215 tp->link_config.active_speed = SPEED_INVALID;
13216 tp->link_config.active_duplex = DUPLEX_INVALID;
13217 tp->link_config.orig_speed = SPEED_INVALID;
13218 tp->link_config.orig_duplex = DUPLEX_INVALID;
13219 tp->link_config.orig_autoneg = AUTONEG_INVALID;
13220}
13221
Michael Chan7d0c41e2005-04-21 17:06:20 -070013222static int __devinit tg3_phy_probe(struct tg3 *tp)
13223{
13224 u32 hw_phy_id_1, hw_phy_id_2;
13225 u32 hw_phy_id, hw_phy_id_masked;
13226 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013227
Matt Carlsone256f8a2011-03-09 16:58:24 +000013228 /* flow control autonegotiation is default behavior */
Joe Perches63c3a662011-04-26 08:12:10 +000013229 tg3_flag_set(tp, PAUSE_AUTONEG);
Matt Carlsone256f8a2011-03-09 16:58:24 +000013230 tp->link_config.flowctrl = FLOW_CTRL_TX | FLOW_CTRL_RX;
13231
Joe Perches63c3a662011-04-26 08:12:10 +000013232 if (tg3_flag(tp, USE_PHYLIB))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070013233 return tg3_phy_init(tp);
13234
Linus Torvalds1da177e2005-04-16 15:20:36 -070013235 /* Reading the PHY ID register can conflict with ASF
Nick Andrew877d0312009-01-26 11:06:57 +010013236 * firmware access to the PHY hardware.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013237 */
13238 err = 0;
Joe Perches63c3a662011-04-26 08:12:10 +000013239 if (tg3_flag(tp, ENABLE_ASF) || tg3_flag(tp, ENABLE_APE)) {
Matt Carlson79eb6902010-02-17 15:17:03 +000013240 hw_phy_id = hw_phy_id_masked = TG3_PHY_ID_INVALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013241 } else {
13242 /* Now read the physical PHY_ID from the chip and verify
13243 * that it is sane. If it doesn't look good, we fall back
13244 * to either the hard-coded table based PHY_ID and failing
13245 * that the value found in the eeprom area.
13246 */
13247 err |= tg3_readphy(tp, MII_PHYSID1, &hw_phy_id_1);
13248 err |= tg3_readphy(tp, MII_PHYSID2, &hw_phy_id_2);
13249
13250 hw_phy_id = (hw_phy_id_1 & 0xffff) << 10;
13251 hw_phy_id |= (hw_phy_id_2 & 0xfc00) << 16;
13252 hw_phy_id |= (hw_phy_id_2 & 0x03ff) << 0;
13253
Matt Carlson79eb6902010-02-17 15:17:03 +000013254 hw_phy_id_masked = hw_phy_id & TG3_PHY_ID_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013255 }
13256
Matt Carlson79eb6902010-02-17 15:17:03 +000013257 if (!err && TG3_KNOWN_PHY_ID(hw_phy_id_masked)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070013258 tp->phy_id = hw_phy_id;
Matt Carlson79eb6902010-02-17 15:17:03 +000013259 if (hw_phy_id_masked == TG3_PHY_ID_BCM8002)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013260 tp->phy_flags |= TG3_PHYFLG_PHY_SERDES;
Michael Chanda6b2d02005-08-19 12:54:29 -070013261 else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013262 tp->phy_flags &= ~TG3_PHYFLG_PHY_SERDES;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013263 } else {
Matt Carlson79eb6902010-02-17 15:17:03 +000013264 if (tp->phy_id != TG3_PHY_ID_INVALID) {
Michael Chan7d0c41e2005-04-21 17:06:20 -070013265 /* Do nothing, phy ID already set up in
13266 * tg3_get_eeprom_hw_cfg().
13267 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070013268 } else {
13269 struct subsys_tbl_ent *p;
13270
13271 /* No eeprom signature? Try the hardcoded
13272 * subsys device table.
13273 */
Matt Carlson24daf2b2010-02-17 15:17:02 +000013274 p = tg3_lookup_by_subsys(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013275 if (!p)
13276 return -ENODEV;
13277
13278 tp->phy_id = p->phy_id;
13279 if (!tp->phy_id ||
Matt Carlson79eb6902010-02-17 15:17:03 +000013280 tp->phy_id == TG3_PHY_ID_BCM8002)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013281 tp->phy_flags |= TG3_PHYFLG_PHY_SERDES;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013282 }
13283 }
13284
Matt Carlsona6b68da2010-12-06 08:28:52 +000013285 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES) &&
Matt Carlson5baa5e92011-07-20 10:20:53 +000013286 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
13287 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720 ||
13288 (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718 &&
Matt Carlsona6b68da2010-12-06 08:28:52 +000013289 tp->pci_chip_rev_id != CHIPREV_ID_5717_A0) ||
13290 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765 &&
13291 tp->pci_chip_rev_id != CHIPREV_ID_57765_A0)))
Matt Carlson52b02d02010-10-14 10:37:41 +000013292 tp->phy_flags |= TG3_PHYFLG_EEE_CAP;
13293
Matt Carlsone256f8a2011-03-09 16:58:24 +000013294 tg3_phy_init_link_config(tp);
13295
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013296 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES) &&
Joe Perches63c3a662011-04-26 08:12:10 +000013297 !tg3_flag(tp, ENABLE_APE) &&
13298 !tg3_flag(tp, ENABLE_ASF)) {
Matt Carlson42b64a42011-05-19 12:12:49 +000013299 u32 bmsr, mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013300
13301 tg3_readphy(tp, MII_BMSR, &bmsr);
13302 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
13303 (bmsr & BMSR_LSTATUS))
13304 goto skip_phy_reset;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040013305
Linus Torvalds1da177e2005-04-16 15:20:36 -070013306 err = tg3_phy_reset(tp);
13307 if (err)
13308 return err;
13309
Matt Carlson42b64a42011-05-19 12:12:49 +000013310 tg3_phy_set_wirespeed(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013311
Michael Chan3600d912006-12-07 00:21:48 -080013312 mask = (ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
13313 ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full |
13314 ADVERTISED_1000baseT_Half | ADVERTISED_1000baseT_Full);
13315 if (!tg3_copper_is_advertising_all(tp, mask)) {
Matt Carlson42b64a42011-05-19 12:12:49 +000013316 tg3_phy_autoneg_cfg(tp, tp->link_config.advertising,
13317 tp->link_config.flowctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013318
13319 tg3_writephy(tp, MII_BMCR,
13320 BMCR_ANENABLE | BMCR_ANRESTART);
13321 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013322 }
13323
13324skip_phy_reset:
Matt Carlson79eb6902010-02-17 15:17:03 +000013325 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070013326 err = tg3_init_5401phy_dsp(tp);
13327 if (err)
13328 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013329
Linus Torvalds1da177e2005-04-16 15:20:36 -070013330 err = tg3_init_5401phy_dsp(tp);
13331 }
13332
Linus Torvalds1da177e2005-04-16 15:20:36 -070013333 return err;
13334}
13335
Matt Carlson184b8902010-04-05 10:19:25 +000013336static void __devinit tg3_read_vpd(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013337{
Matt Carlsona4a8bb12010-09-15 09:00:00 +000013338 u8 *vpd_data;
Matt Carlson4181b2c2010-02-26 14:04:45 +000013339 unsigned int block_end, rosize, len;
Matt Carlson535a4902011-07-20 10:20:56 +000013340 u32 vpdlen;
Matt Carlson184b8902010-04-05 10:19:25 +000013341 int j, i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013342
Matt Carlson535a4902011-07-20 10:20:56 +000013343 vpd_data = (u8 *)tg3_vpd_readblock(tp, &vpdlen);
Matt Carlsona4a8bb12010-09-15 09:00:00 +000013344 if (!vpd_data)
13345 goto out_no_vpd;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013346
Matt Carlson535a4902011-07-20 10:20:56 +000013347 i = pci_vpd_find_tag(vpd_data, 0, vpdlen, PCI_VPD_LRDT_RO_DATA);
Matt Carlson4181b2c2010-02-26 14:04:45 +000013348 if (i < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013349 goto out_not_found;
Matt Carlson4181b2c2010-02-26 14:04:45 +000013350
13351 rosize = pci_vpd_lrdt_size(&vpd_data[i]);
13352 block_end = i + PCI_VPD_LRDT_TAG_SIZE + rosize;
13353 i += PCI_VPD_LRDT_TAG_SIZE;
13354
Matt Carlson535a4902011-07-20 10:20:56 +000013355 if (block_end > vpdlen)
Matt Carlson4181b2c2010-02-26 14:04:45 +000013356 goto out_not_found;
13357
Matt Carlson184b8902010-04-05 10:19:25 +000013358 j = pci_vpd_find_info_keyword(vpd_data, i, rosize,
13359 PCI_VPD_RO_KEYWORD_MFR_ID);
13360 if (j > 0) {
13361 len = pci_vpd_info_field_size(&vpd_data[j]);
13362
13363 j += PCI_VPD_INFO_FLD_HDR_SIZE;
13364 if (j + len > block_end || len != 4 ||
13365 memcmp(&vpd_data[j], "1028", 4))
13366 goto partno;
13367
13368 j = pci_vpd_find_info_keyword(vpd_data, i, rosize,
13369 PCI_VPD_RO_KEYWORD_VENDOR0);
13370 if (j < 0)
13371 goto partno;
13372
13373 len = pci_vpd_info_field_size(&vpd_data[j]);
13374
13375 j += PCI_VPD_INFO_FLD_HDR_SIZE;
13376 if (j + len > block_end)
13377 goto partno;
13378
13379 memcpy(tp->fw_ver, &vpd_data[j], len);
Matt Carlson535a4902011-07-20 10:20:56 +000013380 strncat(tp->fw_ver, " bc ", vpdlen - len - 1);
Matt Carlson184b8902010-04-05 10:19:25 +000013381 }
13382
13383partno:
Matt Carlson4181b2c2010-02-26 14:04:45 +000013384 i = pci_vpd_find_info_keyword(vpd_data, i, rosize,
13385 PCI_VPD_RO_KEYWORD_PARTNO);
13386 if (i < 0)
13387 goto out_not_found;
13388
13389 len = pci_vpd_info_field_size(&vpd_data[i]);
13390
13391 i += PCI_VPD_INFO_FLD_HDR_SIZE;
13392 if (len > TG3_BPN_SIZE ||
Matt Carlson535a4902011-07-20 10:20:56 +000013393 (len + i) > vpdlen)
Matt Carlson4181b2c2010-02-26 14:04:45 +000013394 goto out_not_found;
13395
13396 memcpy(tp->board_part_number, &vpd_data[i], len);
13397
Linus Torvalds1da177e2005-04-16 15:20:36 -070013398out_not_found:
Matt Carlsona4a8bb12010-09-15 09:00:00 +000013399 kfree(vpd_data);
Matt Carlson37a949c2010-09-30 10:34:33 +000013400 if (tp->board_part_number[0])
Matt Carlsona4a8bb12010-09-15 09:00:00 +000013401 return;
13402
13403out_no_vpd:
Matt Carlson37a949c2010-09-30 10:34:33 +000013404 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717) {
13405 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717)
13406 strcpy(tp->board_part_number, "BCM5717");
13407 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718)
13408 strcpy(tp->board_part_number, "BCM5718");
13409 else
13410 goto nomatch;
13411 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780) {
13412 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57780)
13413 strcpy(tp->board_part_number, "BCM57780");
13414 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57760)
13415 strcpy(tp->board_part_number, "BCM57760");
13416 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57790)
13417 strcpy(tp->board_part_number, "BCM57790");
13418 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57788)
13419 strcpy(tp->board_part_number, "BCM57788");
13420 else
13421 goto nomatch;
13422 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765) {
13423 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57761)
13424 strcpy(tp->board_part_number, "BCM57761");
13425 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57765)
13426 strcpy(tp->board_part_number, "BCM57765");
13427 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57781)
13428 strcpy(tp->board_part_number, "BCM57781");
13429 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57785)
13430 strcpy(tp->board_part_number, "BCM57785");
13431 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57791)
13432 strcpy(tp->board_part_number, "BCM57791");
13433 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57795)
13434 strcpy(tp->board_part_number, "BCM57795");
13435 else
13436 goto nomatch;
13437 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chanb5d37722006-09-27 16:06:21 -070013438 strcpy(tp->board_part_number, "BCM95906");
Matt Carlson37a949c2010-09-30 10:34:33 +000013439 } else {
13440nomatch:
Michael Chanb5d37722006-09-27 16:06:21 -070013441 strcpy(tp->board_part_number, "none");
Matt Carlson37a949c2010-09-30 10:34:33 +000013442 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013443}
13444
Matt Carlson9c8a6202007-10-21 16:16:08 -070013445static int __devinit tg3_fw_img_is_valid(struct tg3 *tp, u32 offset)
13446{
13447 u32 val;
13448
Matt Carlsone4f34112009-02-25 14:25:00 +000013449 if (tg3_nvram_read(tp, offset, &val) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070013450 (val & 0xfc000000) != 0x0c000000 ||
Matt Carlsone4f34112009-02-25 14:25:00 +000013451 tg3_nvram_read(tp, offset + 4, &val) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070013452 val != 0)
13453 return 0;
13454
13455 return 1;
13456}
13457
Matt Carlsonacd9c112009-02-25 14:26:33 +000013458static void __devinit tg3_read_bc_ver(struct tg3 *tp)
13459{
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013460 u32 val, offset, start, ver_offset;
Matt Carlson75f99362010-04-05 10:19:24 +000013461 int i, dst_off;
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013462 bool newver = false;
Matt Carlsonacd9c112009-02-25 14:26:33 +000013463
13464 if (tg3_nvram_read(tp, 0xc, &offset) ||
13465 tg3_nvram_read(tp, 0x4, &start))
13466 return;
13467
13468 offset = tg3_nvram_logical_addr(tp, offset);
13469
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013470 if (tg3_nvram_read(tp, offset, &val))
Matt Carlsonacd9c112009-02-25 14:26:33 +000013471 return;
13472
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013473 if ((val & 0xfc000000) == 0x0c000000) {
13474 if (tg3_nvram_read(tp, offset + 4, &val))
Matt Carlsonacd9c112009-02-25 14:26:33 +000013475 return;
13476
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013477 if (val == 0)
13478 newver = true;
13479 }
13480
Matt Carlson75f99362010-04-05 10:19:24 +000013481 dst_off = strlen(tp->fw_ver);
13482
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013483 if (newver) {
Matt Carlson75f99362010-04-05 10:19:24 +000013484 if (TG3_VER_SIZE - dst_off < 16 ||
13485 tg3_nvram_read(tp, offset + 8, &ver_offset))
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013486 return;
13487
13488 offset = offset + ver_offset - start;
13489 for (i = 0; i < 16; i += 4) {
13490 __be32 v;
13491 if (tg3_nvram_read_be32(tp, offset + i, &v))
13492 return;
13493
Matt Carlson75f99362010-04-05 10:19:24 +000013494 memcpy(tp->fw_ver + dst_off + i, &v, sizeof(v));
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013495 }
13496 } else {
13497 u32 major, minor;
13498
13499 if (tg3_nvram_read(tp, TG3_NVM_PTREV_BCVER, &ver_offset))
13500 return;
13501
13502 major = (ver_offset & TG3_NVM_BCVER_MAJMSK) >>
13503 TG3_NVM_BCVER_MAJSFT;
13504 minor = ver_offset & TG3_NVM_BCVER_MINMSK;
Matt Carlson75f99362010-04-05 10:19:24 +000013505 snprintf(&tp->fw_ver[dst_off], TG3_VER_SIZE - dst_off,
13506 "v%d.%02d", major, minor);
Matt Carlsonacd9c112009-02-25 14:26:33 +000013507 }
13508}
13509
Matt Carlsona6f6cb12009-02-25 14:27:43 +000013510static void __devinit tg3_read_hwsb_ver(struct tg3 *tp)
13511{
13512 u32 val, major, minor;
13513
13514 /* Use native endian representation */
13515 if (tg3_nvram_read(tp, TG3_NVM_HWSB_CFG1, &val))
13516 return;
13517
13518 major = (val & TG3_NVM_HWSB_CFG1_MAJMSK) >>
13519 TG3_NVM_HWSB_CFG1_MAJSFT;
13520 minor = (val & TG3_NVM_HWSB_CFG1_MINMSK) >>
13521 TG3_NVM_HWSB_CFG1_MINSFT;
13522
13523 snprintf(&tp->fw_ver[0], 32, "sb v%d.%02d", major, minor);
13524}
13525
Matt Carlsondfe00d72008-11-21 17:19:41 -080013526static void __devinit tg3_read_sb_ver(struct tg3 *tp, u32 val)
13527{
13528 u32 offset, major, minor, build;
13529
Matt Carlson75f99362010-04-05 10:19:24 +000013530 strncat(tp->fw_ver, "sb", TG3_VER_SIZE - strlen(tp->fw_ver) - 1);
Matt Carlsondfe00d72008-11-21 17:19:41 -080013531
13532 if ((val & TG3_EEPROM_SB_FORMAT_MASK) != TG3_EEPROM_SB_FORMAT_1)
13533 return;
13534
13535 switch (val & TG3_EEPROM_SB_REVISION_MASK) {
13536 case TG3_EEPROM_SB_REVISION_0:
13537 offset = TG3_EEPROM_SB_F1R0_EDH_OFF;
13538 break;
13539 case TG3_EEPROM_SB_REVISION_2:
13540 offset = TG3_EEPROM_SB_F1R2_EDH_OFF;
13541 break;
13542 case TG3_EEPROM_SB_REVISION_3:
13543 offset = TG3_EEPROM_SB_F1R3_EDH_OFF;
13544 break;
Matt Carlsona4153d42010-02-17 15:16:56 +000013545 case TG3_EEPROM_SB_REVISION_4:
13546 offset = TG3_EEPROM_SB_F1R4_EDH_OFF;
13547 break;
13548 case TG3_EEPROM_SB_REVISION_5:
13549 offset = TG3_EEPROM_SB_F1R5_EDH_OFF;
13550 break;
Matt Carlsonbba226a2010-10-14 10:37:38 +000013551 case TG3_EEPROM_SB_REVISION_6:
13552 offset = TG3_EEPROM_SB_F1R6_EDH_OFF;
13553 break;
Matt Carlsondfe00d72008-11-21 17:19:41 -080013554 default:
13555 return;
13556 }
13557
Matt Carlsone4f34112009-02-25 14:25:00 +000013558 if (tg3_nvram_read(tp, offset, &val))
Matt Carlsondfe00d72008-11-21 17:19:41 -080013559 return;
13560
13561 build = (val & TG3_EEPROM_SB_EDH_BLD_MASK) >>
13562 TG3_EEPROM_SB_EDH_BLD_SHFT;
13563 major = (val & TG3_EEPROM_SB_EDH_MAJ_MASK) >>
13564 TG3_EEPROM_SB_EDH_MAJ_SHFT;
13565 minor = val & TG3_EEPROM_SB_EDH_MIN_MASK;
13566
13567 if (minor > 99 || build > 26)
13568 return;
13569
Matt Carlson75f99362010-04-05 10:19:24 +000013570 offset = strlen(tp->fw_ver);
13571 snprintf(&tp->fw_ver[offset], TG3_VER_SIZE - offset,
13572 " v%d.%02d", major, minor);
Matt Carlsondfe00d72008-11-21 17:19:41 -080013573
13574 if (build > 0) {
Matt Carlson75f99362010-04-05 10:19:24 +000013575 offset = strlen(tp->fw_ver);
13576 if (offset < TG3_VER_SIZE - 1)
13577 tp->fw_ver[offset] = 'a' + build - 1;
Matt Carlsondfe00d72008-11-21 17:19:41 -080013578 }
13579}
13580
Matt Carlsonacd9c112009-02-25 14:26:33 +000013581static void __devinit tg3_read_mgmtfw_ver(struct tg3 *tp)
Michael Chanc4e65752006-03-20 22:29:32 -080013582{
13583 u32 val, offset, start;
Matt Carlsonacd9c112009-02-25 14:26:33 +000013584 int i, vlen;
Matt Carlson9c8a6202007-10-21 16:16:08 -070013585
13586 for (offset = TG3_NVM_DIR_START;
13587 offset < TG3_NVM_DIR_END;
13588 offset += TG3_NVM_DIRENT_SIZE) {
Matt Carlsone4f34112009-02-25 14:25:00 +000013589 if (tg3_nvram_read(tp, offset, &val))
Matt Carlson9c8a6202007-10-21 16:16:08 -070013590 return;
13591
13592 if ((val >> TG3_NVM_DIRTYPE_SHIFT) == TG3_NVM_DIRTYPE_ASFINI)
13593 break;
13594 }
13595
13596 if (offset == TG3_NVM_DIR_END)
13597 return;
13598
Joe Perches63c3a662011-04-26 08:12:10 +000013599 if (!tg3_flag(tp, 5705_PLUS))
Matt Carlson9c8a6202007-10-21 16:16:08 -070013600 start = 0x08000000;
Matt Carlsone4f34112009-02-25 14:25:00 +000013601 else if (tg3_nvram_read(tp, offset - 4, &start))
Matt Carlson9c8a6202007-10-21 16:16:08 -070013602 return;
13603
Matt Carlsone4f34112009-02-25 14:25:00 +000013604 if (tg3_nvram_read(tp, offset + 4, &offset) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070013605 !tg3_fw_img_is_valid(tp, offset) ||
Matt Carlsone4f34112009-02-25 14:25:00 +000013606 tg3_nvram_read(tp, offset + 8, &val))
Matt Carlson9c8a6202007-10-21 16:16:08 -070013607 return;
13608
13609 offset += val - start;
13610
Matt Carlsonacd9c112009-02-25 14:26:33 +000013611 vlen = strlen(tp->fw_ver);
Matt Carlson9c8a6202007-10-21 16:16:08 -070013612
Matt Carlsonacd9c112009-02-25 14:26:33 +000013613 tp->fw_ver[vlen++] = ',';
13614 tp->fw_ver[vlen++] = ' ';
Matt Carlson9c8a6202007-10-21 16:16:08 -070013615
13616 for (i = 0; i < 4; i++) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000013617 __be32 v;
13618 if (tg3_nvram_read_be32(tp, offset, &v))
Matt Carlson9c8a6202007-10-21 16:16:08 -070013619 return;
13620
Al Virob9fc7dc2007-12-17 22:59:57 -080013621 offset += sizeof(v);
Matt Carlson9c8a6202007-10-21 16:16:08 -070013622
Matt Carlsonacd9c112009-02-25 14:26:33 +000013623 if (vlen > TG3_VER_SIZE - sizeof(v)) {
13624 memcpy(&tp->fw_ver[vlen], &v, TG3_VER_SIZE - vlen);
Matt Carlson9c8a6202007-10-21 16:16:08 -070013625 break;
13626 }
13627
Matt Carlsonacd9c112009-02-25 14:26:33 +000013628 memcpy(&tp->fw_ver[vlen], &v, sizeof(v));
13629 vlen += sizeof(v);
Matt Carlson9c8a6202007-10-21 16:16:08 -070013630 }
Matt Carlsonacd9c112009-02-25 14:26:33 +000013631}
13632
Matt Carlson7fd76442009-02-25 14:27:20 +000013633static void __devinit tg3_read_dash_ver(struct tg3 *tp)
13634{
13635 int vlen;
13636 u32 apedata;
Matt Carlsonecc79642010-08-02 11:26:01 +000013637 char *fwtype;
Matt Carlson7fd76442009-02-25 14:27:20 +000013638
Joe Perches63c3a662011-04-26 08:12:10 +000013639 if (!tg3_flag(tp, ENABLE_APE) || !tg3_flag(tp, ENABLE_ASF))
Matt Carlson7fd76442009-02-25 14:27:20 +000013640 return;
13641
13642 apedata = tg3_ape_read32(tp, TG3_APE_SEG_SIG);
13643 if (apedata != APE_SEG_SIG_MAGIC)
13644 return;
13645
13646 apedata = tg3_ape_read32(tp, TG3_APE_FW_STATUS);
13647 if (!(apedata & APE_FW_STATUS_READY))
13648 return;
13649
13650 apedata = tg3_ape_read32(tp, TG3_APE_FW_VERSION);
13651
Matt Carlsondc6d0742010-09-15 08:59:55 +000013652 if (tg3_ape_read32(tp, TG3_APE_FW_FEATURES) & TG3_APE_FW_FEATURE_NCSI) {
Joe Perches63c3a662011-04-26 08:12:10 +000013653 tg3_flag_set(tp, APE_HAS_NCSI);
Matt Carlsonecc79642010-08-02 11:26:01 +000013654 fwtype = "NCSI";
Matt Carlsondc6d0742010-09-15 08:59:55 +000013655 } else {
Matt Carlsonecc79642010-08-02 11:26:01 +000013656 fwtype = "DASH";
Matt Carlsondc6d0742010-09-15 08:59:55 +000013657 }
Matt Carlsonecc79642010-08-02 11:26:01 +000013658
Matt Carlson7fd76442009-02-25 14:27:20 +000013659 vlen = strlen(tp->fw_ver);
13660
Matt Carlsonecc79642010-08-02 11:26:01 +000013661 snprintf(&tp->fw_ver[vlen], TG3_VER_SIZE - vlen, " %s v%d.%d.%d.%d",
13662 fwtype,
Matt Carlson7fd76442009-02-25 14:27:20 +000013663 (apedata & APE_FW_VERSION_MAJMSK) >> APE_FW_VERSION_MAJSFT,
13664 (apedata & APE_FW_VERSION_MINMSK) >> APE_FW_VERSION_MINSFT,
13665 (apedata & APE_FW_VERSION_REVMSK) >> APE_FW_VERSION_REVSFT,
13666 (apedata & APE_FW_VERSION_BLDMSK));
13667}
13668
Matt Carlsonacd9c112009-02-25 14:26:33 +000013669static void __devinit tg3_read_fw_ver(struct tg3 *tp)
13670{
13671 u32 val;
Matt Carlson75f99362010-04-05 10:19:24 +000013672 bool vpd_vers = false;
13673
13674 if (tp->fw_ver[0] != 0)
13675 vpd_vers = true;
Matt Carlsonacd9c112009-02-25 14:26:33 +000013676
Joe Perches63c3a662011-04-26 08:12:10 +000013677 if (tg3_flag(tp, NO_NVRAM)) {
Matt Carlson75f99362010-04-05 10:19:24 +000013678 strcat(tp->fw_ver, "sb");
Matt Carlsondf259d82009-04-20 06:57:14 +000013679 return;
13680 }
13681
Matt Carlsonacd9c112009-02-25 14:26:33 +000013682 if (tg3_nvram_read(tp, 0, &val))
13683 return;
13684
13685 if (val == TG3_EEPROM_MAGIC)
13686 tg3_read_bc_ver(tp);
13687 else if ((val & TG3_EEPROM_MAGIC_FW_MSK) == TG3_EEPROM_MAGIC_FW)
13688 tg3_read_sb_ver(tp, val);
Matt Carlsona6f6cb12009-02-25 14:27:43 +000013689 else if ((val & TG3_EEPROM_MAGIC_HW_MSK) == TG3_EEPROM_MAGIC_HW)
13690 tg3_read_hwsb_ver(tp);
Matt Carlsonacd9c112009-02-25 14:26:33 +000013691 else
13692 return;
13693
Matt Carlsonc9cab242011-07-13 09:27:27 +000013694 if (vpd_vers)
Matt Carlson75f99362010-04-05 10:19:24 +000013695 goto done;
Matt Carlsonacd9c112009-02-25 14:26:33 +000013696
Matt Carlsonc9cab242011-07-13 09:27:27 +000013697 if (tg3_flag(tp, ENABLE_APE)) {
13698 if (tg3_flag(tp, ENABLE_ASF))
13699 tg3_read_dash_ver(tp);
13700 } else if (tg3_flag(tp, ENABLE_ASF)) {
13701 tg3_read_mgmtfw_ver(tp);
13702 }
Matt Carlson9c8a6202007-10-21 16:16:08 -070013703
Matt Carlson75f99362010-04-05 10:19:24 +000013704done:
Matt Carlson9c8a6202007-10-21 16:16:08 -070013705 tp->fw_ver[TG3_VER_SIZE - 1] = 0;
Michael Chanc4e65752006-03-20 22:29:32 -080013706}
13707
Michael Chan7544b092007-05-05 13:08:32 -070013708static struct pci_dev * __devinit tg3_find_peer(struct tg3 *);
13709
Matt Carlson7cb32cf2010-09-30 10:34:36 +000013710static inline u32 tg3_rx_ret_ring_size(struct tg3 *tp)
13711{
Joe Perches63c3a662011-04-26 08:12:10 +000013712 if (tg3_flag(tp, LRG_PROD_RING_CAP))
Matt Carlsonde9f5232011-04-05 14:22:43 +000013713 return TG3_RX_RET_MAX_SIZE_5717;
Joe Perches63c3a662011-04-26 08:12:10 +000013714 else if (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS))
Matt Carlsonde9f5232011-04-05 14:22:43 +000013715 return TG3_RX_RET_MAX_SIZE_5700;
Matt Carlson7cb32cf2010-09-30 10:34:36 +000013716 else
Matt Carlsonde9f5232011-04-05 14:22:43 +000013717 return TG3_RX_RET_MAX_SIZE_5705;
Matt Carlson7cb32cf2010-09-30 10:34:36 +000013718}
13719
Matt Carlson41434702011-03-09 16:58:22 +000013720static DEFINE_PCI_DEVICE_TABLE(tg3_write_reorder_chipsets) = {
Joe Perches895950c2010-12-21 02:16:08 -080013721 { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_FE_GATE_700C) },
13722 { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_8131_BRIDGE) },
13723 { PCI_DEVICE(PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_8385_0) },
13724 { },
13725};
13726
Linus Torvalds1da177e2005-04-16 15:20:36 -070013727static int __devinit tg3_get_invariants(struct tg3 *tp)
13728{
Linus Torvalds1da177e2005-04-16 15:20:36 -070013729 u32 misc_ctrl_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013730 u32 pci_state_reg, grc_misc_cfg;
13731 u32 val;
13732 u16 pci_cmd;
Matt Carlson5e7dfd02008-11-21 17:18:16 -080013733 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013734
Linus Torvalds1da177e2005-04-16 15:20:36 -070013735 /* Force memory write invalidate off. If we leave it on,
13736 * then on 5700_BX chips we have to enable a workaround.
13737 * The workaround is to set the TG3PCI_DMA_RW_CTRL boundary
13738 * to match the cacheline size. The Broadcom driver have this
13739 * workaround but turns MWI off all the times so never uses
13740 * it. This seems to suggest that the workaround is insufficient.
13741 */
13742 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
13743 pci_cmd &= ~PCI_COMMAND_INVALIDATE;
13744 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
13745
Matt Carlson16821282011-07-13 09:27:28 +000013746 /* Important! -- Make sure register accesses are byteswapped
13747 * correctly. Also, for those chips that require it, make
13748 * sure that indirect register accesses are enabled before
13749 * the first operation.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013750 */
13751 pci_read_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
13752 &misc_ctrl_reg);
Matt Carlson16821282011-07-13 09:27:28 +000013753 tp->misc_host_ctrl |= (misc_ctrl_reg &
13754 MISC_HOST_CTRL_CHIPREV);
13755 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
13756 tp->misc_host_ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013757
13758 tp->pci_chip_rev_id = (misc_ctrl_reg >>
13759 MISC_HOST_CTRL_CHIPREV_SHIFT);
Matt Carlson795d01c2007-10-07 23:28:17 -070013760 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_USE_PROD_ID_REG) {
13761 u32 prod_id_asic_rev;
13762
Matt Carlson5001e2f2009-11-13 13:03:51 +000013763 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717 ||
13764 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718 ||
Matt Carlsond78b59f2011-04-05 14:22:46 +000013765 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5719 ||
13766 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5720)
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013767 pci_read_config_dword(tp->pdev,
13768 TG3PCI_GEN2_PRODID_ASICREV,
13769 &prod_id_asic_rev);
Matt Carlsonb703df62009-12-03 08:36:21 +000013770 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57781 ||
13771 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57785 ||
13772 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57761 ||
13773 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57765 ||
13774 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57791 ||
13775 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57795)
13776 pci_read_config_dword(tp->pdev,
13777 TG3PCI_GEN15_PRODID_ASICREV,
13778 &prod_id_asic_rev);
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013779 else
13780 pci_read_config_dword(tp->pdev, TG3PCI_PRODID_ASICREV,
13781 &prod_id_asic_rev);
13782
Matt Carlson321d32a2008-11-21 17:22:19 -080013783 tp->pci_chip_rev_id = prod_id_asic_rev;
Matt Carlson795d01c2007-10-07 23:28:17 -070013784 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013785
Michael Chanff645be2005-04-21 17:09:53 -070013786 /* Wrong chip ID in 5752 A0. This code can be removed later
13787 * as A0 is not in production.
13788 */
13789 if (tp->pci_chip_rev_id == CHIPREV_ID_5752_A0_HW)
13790 tp->pci_chip_rev_id = CHIPREV_ID_5752_A0;
13791
Michael Chan68929142005-08-09 20:17:14 -070013792 /* If we have 5702/03 A1 or A2 on certain ICH chipsets,
13793 * we need to disable memory and use config. cycles
13794 * only to access all registers. The 5702/03 chips
13795 * can mistakenly decode the special cycles from the
13796 * ICH chipsets as memory write cycles, causing corruption
13797 * of register and memory space. Only certain ICH bridges
13798 * will drive special cycles with non-zero data during the
13799 * address phase which can fall within the 5703's address
13800 * range. This is not an ICH bug as the PCI spec allows
13801 * non-zero address during special cycles. However, only
13802 * these ICH bridges are known to drive non-zero addresses
13803 * during special cycles.
13804 *
13805 * Since special cycles do not cross PCI bridges, we only
13806 * enable this workaround if the 5703 is on the secondary
13807 * bus of these ICH bridges.
13808 */
13809 if ((tp->pci_chip_rev_id == CHIPREV_ID_5703_A1) ||
13810 (tp->pci_chip_rev_id == CHIPREV_ID_5703_A2)) {
13811 static struct tg3_dev_id {
13812 u32 vendor;
13813 u32 device;
13814 u32 rev;
13815 } ich_chipsets[] = {
13816 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AA_8,
13817 PCI_ANY_ID },
13818 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AB_8,
13819 PCI_ANY_ID },
13820 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_11,
13821 0xa },
13822 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_6,
13823 PCI_ANY_ID },
13824 { },
13825 };
13826 struct tg3_dev_id *pci_id = &ich_chipsets[0];
13827 struct pci_dev *bridge = NULL;
13828
13829 while (pci_id->vendor != 0) {
13830 bridge = pci_get_device(pci_id->vendor, pci_id->device,
13831 bridge);
13832 if (!bridge) {
13833 pci_id++;
13834 continue;
13835 }
13836 if (pci_id->rev != PCI_ANY_ID) {
Auke Kok44c10132007-06-08 15:46:36 -070013837 if (bridge->revision > pci_id->rev)
Michael Chan68929142005-08-09 20:17:14 -070013838 continue;
13839 }
13840 if (bridge->subordinate &&
13841 (bridge->subordinate->number ==
13842 tp->pdev->bus->number)) {
Joe Perches63c3a662011-04-26 08:12:10 +000013843 tg3_flag_set(tp, ICH_WORKAROUND);
Michael Chan68929142005-08-09 20:17:14 -070013844 pci_dev_put(bridge);
13845 break;
13846 }
13847 }
13848 }
13849
Matt Carlson6ff6f812011-05-19 12:12:54 +000013850 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
Matt Carlson41588ba2008-04-19 18:12:33 -070013851 static struct tg3_dev_id {
13852 u32 vendor;
13853 u32 device;
13854 } bridge_chipsets[] = {
13855 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PXH_0 },
13856 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PXH_1 },
13857 { },
13858 };
13859 struct tg3_dev_id *pci_id = &bridge_chipsets[0];
13860 struct pci_dev *bridge = NULL;
13861
13862 while (pci_id->vendor != 0) {
13863 bridge = pci_get_device(pci_id->vendor,
13864 pci_id->device,
13865 bridge);
13866 if (!bridge) {
13867 pci_id++;
13868 continue;
13869 }
13870 if (bridge->subordinate &&
13871 (bridge->subordinate->number <=
13872 tp->pdev->bus->number) &&
13873 (bridge->subordinate->subordinate >=
13874 tp->pdev->bus->number)) {
Joe Perches63c3a662011-04-26 08:12:10 +000013875 tg3_flag_set(tp, 5701_DMA_BUG);
Matt Carlson41588ba2008-04-19 18:12:33 -070013876 pci_dev_put(bridge);
13877 break;
13878 }
13879 }
13880 }
13881
Michael Chan4a29cc22006-03-19 13:21:12 -080013882 /* The EPB bridge inside 5714, 5715, and 5780 cannot support
13883 * DMA addresses > 40-bit. This bridge may have other additional
13884 * 57xx devices behind it in some 4-port NIC designs for example.
13885 * Any tg3 device found behind the bridge will also need the 40-bit
13886 * DMA workaround.
13887 */
Michael Chana4e2b342005-10-26 15:46:52 -070013888 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780 ||
13889 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
Joe Perches63c3a662011-04-26 08:12:10 +000013890 tg3_flag_set(tp, 5780_CLASS);
13891 tg3_flag_set(tp, 40BIT_DMA_BUG);
Michael Chan4cf78e42005-07-25 12:29:19 -070013892 tp->msi_cap = pci_find_capability(tp->pdev, PCI_CAP_ID_MSI);
Matt Carlson859a5882010-04-05 10:19:28 +000013893 } else {
Michael Chan4a29cc22006-03-19 13:21:12 -080013894 struct pci_dev *bridge = NULL;
13895
13896 do {
13897 bridge = pci_get_device(PCI_VENDOR_ID_SERVERWORKS,
13898 PCI_DEVICE_ID_SERVERWORKS_EPB,
13899 bridge);
13900 if (bridge && bridge->subordinate &&
13901 (bridge->subordinate->number <=
13902 tp->pdev->bus->number) &&
13903 (bridge->subordinate->subordinate >=
13904 tp->pdev->bus->number)) {
Joe Perches63c3a662011-04-26 08:12:10 +000013905 tg3_flag_set(tp, 40BIT_DMA_BUG);
Michael Chan4a29cc22006-03-19 13:21:12 -080013906 pci_dev_put(bridge);
13907 break;
13908 }
13909 } while (bridge);
13910 }
Michael Chan4cf78e42005-07-25 12:29:19 -070013911
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013912 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
Matt Carlson3a1e19d2011-07-13 09:27:32 +000013913 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714)
Michael Chan7544b092007-05-05 13:08:32 -070013914 tp->pdev_peer = tg3_find_peer(tp);
13915
Matt Carlsonc885e822010-08-02 11:25:57 +000013916 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
Matt Carlsond78b59f2011-04-05 14:22:46 +000013917 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
13918 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
Joe Perches63c3a662011-04-26 08:12:10 +000013919 tg3_flag_set(tp, 5717_PLUS);
Matt Carlson0a58d662011-04-05 14:22:45 +000013920
13921 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765 ||
Joe Perches63c3a662011-04-26 08:12:10 +000013922 tg3_flag(tp, 5717_PLUS))
13923 tg3_flag_set(tp, 57765_PLUS);
Matt Carlsonc885e822010-08-02 11:25:57 +000013924
Matt Carlson321d32a2008-11-21 17:22:19 -080013925 /* Intentionally exclude ASIC_REV_5906 */
13926 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
Michael Chand9ab5ad2006-03-20 22:27:35 -080013927 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787 ||
Matt Carlsond30cdd22007-10-07 23:28:35 -070013928 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson9936bcf2007-10-10 18:03:07 -070013929 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson57e69832008-05-25 23:48:31 -070013930 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013931 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
Joe Perches63c3a662011-04-26 08:12:10 +000013932 tg3_flag(tp, 57765_PLUS))
13933 tg3_flag_set(tp, 5755_PLUS);
Matt Carlson321d32a2008-11-21 17:22:19 -080013934
13935 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 ||
13936 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752 ||
Michael Chanb5d37722006-09-27 16:06:21 -070013937 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906 ||
Joe Perches63c3a662011-04-26 08:12:10 +000013938 tg3_flag(tp, 5755_PLUS) ||
13939 tg3_flag(tp, 5780_CLASS))
13940 tg3_flag_set(tp, 5750_PLUS);
John W. Linville6708e5c2005-04-21 17:00:52 -070013941
Matt Carlson6ff6f812011-05-19 12:12:54 +000013942 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 ||
Joe Perches63c3a662011-04-26 08:12:10 +000013943 tg3_flag(tp, 5750_PLUS))
13944 tg3_flag_set(tp, 5705_PLUS);
John W. Linville1b440c562005-04-21 17:03:18 -070013945
Matt Carlson507399f2009-11-13 13:03:37 +000013946 /* Determine TSO capabilities */
Matt Carlsona0512942011-07-27 14:20:54 +000013947 if (tp->pci_chip_rev_id == CHIPREV_ID_5719_A0)
Matt Carlson4d163b72011-01-25 15:58:48 +000013948 ; /* Do nothing. HW bug. */
Joe Perches63c3a662011-04-26 08:12:10 +000013949 else if (tg3_flag(tp, 57765_PLUS))
13950 tg3_flag_set(tp, HW_TSO_3);
13951 else if (tg3_flag(tp, 5755_PLUS) ||
Matt Carlsone849cdc2009-11-13 13:03:38 +000013952 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
Joe Perches63c3a662011-04-26 08:12:10 +000013953 tg3_flag_set(tp, HW_TSO_2);
13954 else if (tg3_flag(tp, 5750_PLUS)) {
13955 tg3_flag_set(tp, HW_TSO_1);
13956 tg3_flag_set(tp, TSO_BUG);
Matt Carlson507399f2009-11-13 13:03:37 +000013957 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 &&
13958 tp->pci_chip_rev_id >= CHIPREV_ID_5750_C2)
Joe Perches63c3a662011-04-26 08:12:10 +000013959 tg3_flag_clear(tp, TSO_BUG);
Matt Carlson507399f2009-11-13 13:03:37 +000013960 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
13961 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701 &&
13962 tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) {
Joe Perches63c3a662011-04-26 08:12:10 +000013963 tg3_flag_set(tp, TSO_BUG);
Matt Carlson507399f2009-11-13 13:03:37 +000013964 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705)
13965 tp->fw_needed = FIRMWARE_TG3TSO5;
13966 else
13967 tp->fw_needed = FIRMWARE_TG3TSO;
13968 }
13969
Matt Carlsondabc5c62011-05-19 12:12:52 +000013970 /* Selectively allow TSO based on operating conditions */
Matt Carlson6ff6f812011-05-19 12:12:54 +000013971 if (tg3_flag(tp, HW_TSO_1) ||
13972 tg3_flag(tp, HW_TSO_2) ||
13973 tg3_flag(tp, HW_TSO_3) ||
Matt Carlsondabc5c62011-05-19 12:12:52 +000013974 (tp->fw_needed && !tg3_flag(tp, ENABLE_ASF)))
13975 tg3_flag_set(tp, TSO_CAPABLE);
13976 else {
13977 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
Joe Perches63c3a662011-04-26 08:12:10 +000014013 if (tg3_flag(tp, 5717_PLUS))
14014 tg3_flag_set(tp, LRG_PROD_RING_CAP);
Matt Carlsonde9f5232011-04-05 14:22:43 +000014015
Joe Perches63c3a662011-04-26 08:12:10 +000014016 if (tg3_flag(tp, 57765_PLUS) &&
Matt Carlsona0512942011-07-27 14:20:54 +000014017 tp->pci_chip_rev_id != CHIPREV_ID_5719_A0)
Joe Perches63c3a662011-04-26 08:12:10 +000014018 tg3_flag_set(tp, USE_JUMBO_BDFLAG);
Matt Carlsonb703df62009-12-03 08:36:21 +000014019
Joe Perches63c3a662011-04-26 08:12:10 +000014020 if (!tg3_flag(tp, 5705_PLUS) ||
14021 tg3_flag(tp, 5780_CLASS) ||
14022 tg3_flag(tp, USE_JUMBO_BDFLAG))
14023 tg3_flag_set(tp, JUMBO_CAPABLE);
Michael Chan0f893dc2005-07-25 12:30:38 -070014024
Matt Carlson52f44902008-11-21 17:17:04 -080014025 pci_read_config_dword(tp->pdev, TG3PCI_PCISTATE,
14026 &pci_state_reg);
14027
Jon Mason708ebb32011-06-27 12:56:50 +000014028 if (pci_is_pcie(tp->pdev)) {
Matt Carlson5e7dfd02008-11-21 17:18:16 -080014029 u16 lnkctl;
14030
Joe Perches63c3a662011-04-26 08:12:10 +000014031 tg3_flag_set(tp, PCI_EXPRESS);
Matt Carlson5f5c51e2007-11-12 21:19:37 -080014032
Matt Carlsoncf790032010-11-24 08:31:48 +000014033 tp->pcie_readrq = 4096;
Matt Carlsond78b59f2011-04-05 14:22:46 +000014034 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
14035 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
Matt Carlsonb4495ed2011-01-25 15:58:47 +000014036 tp->pcie_readrq = 2048;
Matt Carlsoncf790032010-11-24 08:31:48 +000014037
14038 pcie_set_readrq(tp->pdev, tp->pcie_readrq);
Matt Carlson5f5c51e2007-11-12 21:19:37 -080014039
Matt Carlson5e7dfd02008-11-21 17:18:16 -080014040 pci_read_config_word(tp->pdev,
Jon Mason708ebb32011-06-27 12:56:50 +000014041 pci_pcie_cap(tp->pdev) + PCI_EXP_LNKCTL,
Matt Carlson5e7dfd02008-11-21 17:18:16 -080014042 &lnkctl);
14043 if (lnkctl & PCI_EXP_LNKCTL_CLKREQ_EN) {
Matt Carlson7196cd62011-05-19 16:02:44 +000014044 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
14045 ASIC_REV_5906) {
Joe Perches63c3a662011-04-26 08:12:10 +000014046 tg3_flag_clear(tp, HW_TSO_2);
Matt Carlsondabc5c62011-05-19 12:12:52 +000014047 tg3_flag_clear(tp, TSO_CAPABLE);
Matt Carlson7196cd62011-05-19 16:02:44 +000014048 }
Matt Carlson5e7dfd02008-11-21 17:18:16 -080014049 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson321d32a2008-11-21 17:22:19 -080014050 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson9cf74eb2009-04-20 06:58:27 +000014051 tp->pci_chip_rev_id == CHIPREV_ID_57780_A0 ||
14052 tp->pci_chip_rev_id == CHIPREV_ID_57780_A1)
Joe Perches63c3a662011-04-26 08:12:10 +000014053 tg3_flag_set(tp, CLKREQ_BUG);
Matt Carlson614b05902010-01-20 16:58:02 +000014054 } else if (tp->pci_chip_rev_id == CHIPREV_ID_5717_A0) {
Joe Perches63c3a662011-04-26 08:12:10 +000014055 tg3_flag_set(tp, L1PLLPD_EN);
Michael Chanc7835a72006-11-15 21:14:42 -080014056 }
Matt Carlson52f44902008-11-21 17:17:04 -080014057 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785) {
Jon Mason708ebb32011-06-27 12:56:50 +000014058 /* BCM5785 devices are effectively PCIe devices, and should
14059 * follow PCIe codepaths, but do not have a PCIe capabilities
14060 * section.
Matt Carlson93a700a2011-08-31 11:44:54 +000014061 */
Joe Perches63c3a662011-04-26 08:12:10 +000014062 tg3_flag_set(tp, PCI_EXPRESS);
14063 } else if (!tg3_flag(tp, 5705_PLUS) ||
14064 tg3_flag(tp, 5780_CLASS)) {
Matt Carlson52f44902008-11-21 17:17:04 -080014065 tp->pcix_cap = pci_find_capability(tp->pdev, PCI_CAP_ID_PCIX);
14066 if (!tp->pcix_cap) {
Matt Carlson2445e462010-04-05 10:19:21 +000014067 dev_err(&tp->pdev->dev,
14068 "Cannot find PCI-X capability, aborting\n");
Matt Carlson52f44902008-11-21 17:17:04 -080014069 return -EIO;
14070 }
14071
14072 if (!(pci_state_reg & PCISTATE_CONV_PCI_MODE))
Joe Perches63c3a662011-04-26 08:12:10 +000014073 tg3_flag_set(tp, PCIX_MODE);
Matt Carlson52f44902008-11-21 17:17:04 -080014074 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014075
Michael Chan399de502005-10-03 14:02:39 -070014076 /* If we have an AMD 762 or VIA K8T800 chipset, write
14077 * reordering to the mailbox registers done by the host
14078 * controller can cause major troubles. We read back from
14079 * every mailbox register write to force the writes to be
14080 * posted to the chip in order.
14081 */
Matt Carlson41434702011-03-09 16:58:22 +000014082 if (pci_dev_present(tg3_write_reorder_chipsets) &&
Joe Perches63c3a662011-04-26 08:12:10 +000014083 !tg3_flag(tp, PCI_EXPRESS))
14084 tg3_flag_set(tp, MBOX_WRITE_REORDER);
Michael Chan399de502005-10-03 14:02:39 -070014085
Matt Carlson69fc4052008-12-21 20:19:57 -080014086 pci_read_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE,
14087 &tp->pci_cacheline_sz);
14088 pci_read_config_byte(tp->pdev, PCI_LATENCY_TIMER,
14089 &tp->pci_lat_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014090 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 &&
14091 tp->pci_lat_timer < 64) {
14092 tp->pci_lat_timer = 64;
Matt Carlson69fc4052008-12-21 20:19:57 -080014093 pci_write_config_byte(tp->pdev, PCI_LATENCY_TIMER,
14094 tp->pci_lat_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014095 }
14096
Matt Carlson16821282011-07-13 09:27:28 +000014097 /* Important! -- It is critical that the PCI-X hw workaround
14098 * situation is decided before the first MMIO register access.
14099 */
Matt Carlson52f44902008-11-21 17:17:04 -080014100 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5700_BX) {
14101 /* 5700 BX chips need to have their TX producer index
14102 * mailboxes written twice to workaround a bug.
14103 */
Joe Perches63c3a662011-04-26 08:12:10 +000014104 tg3_flag_set(tp, TXD_MBOX_HWBUG);
Matt Carlson9974a352007-10-07 23:27:28 -070014105
Matt Carlson52f44902008-11-21 17:17:04 -080014106 /* If we are in PCI-X mode, enable register write workaround.
Linus Torvalds1da177e2005-04-16 15:20:36 -070014107 *
14108 * The workaround is to use indirect register accesses
14109 * for all chip writes not to mailbox registers.
14110 */
Joe Perches63c3a662011-04-26 08:12:10 +000014111 if (tg3_flag(tp, PCIX_MODE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070014112 u32 pm_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014113
Joe Perches63c3a662011-04-26 08:12:10 +000014114 tg3_flag_set(tp, PCIX_TARGET_HWBUG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014115
14116 /* The chip can have it's power management PCI config
14117 * space registers clobbered due to this bug.
14118 * So explicitly force the chip into D0 here.
14119 */
Matt Carlson9974a352007-10-07 23:27:28 -070014120 pci_read_config_dword(tp->pdev,
14121 tp->pm_cap + PCI_PM_CTRL,
Linus Torvalds1da177e2005-04-16 15:20:36 -070014122 &pm_reg);
14123 pm_reg &= ~PCI_PM_CTRL_STATE_MASK;
14124 pm_reg |= PCI_PM_CTRL_PME_ENABLE | 0 /* D0 */;
Matt Carlson9974a352007-10-07 23:27:28 -070014125 pci_write_config_dword(tp->pdev,
14126 tp->pm_cap + PCI_PM_CTRL,
Linus Torvalds1da177e2005-04-16 15:20:36 -070014127 pm_reg);
14128
14129 /* Also, force SERR#/PERR# in PCI command. */
14130 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
14131 pci_cmd |= PCI_COMMAND_PARITY | PCI_COMMAND_SERR;
14132 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
14133 }
14134 }
14135
Linus Torvalds1da177e2005-04-16 15:20:36 -070014136 if ((pci_state_reg & PCISTATE_BUS_SPEED_HIGH) != 0)
Joe Perches63c3a662011-04-26 08:12:10 +000014137 tg3_flag_set(tp, PCI_HIGH_SPEED);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014138 if ((pci_state_reg & PCISTATE_BUS_32BIT) != 0)
Joe Perches63c3a662011-04-26 08:12:10 +000014139 tg3_flag_set(tp, PCI_32BIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014140
14141 /* Chip-specific fixup from Broadcom driver */
14142 if ((tp->pci_chip_rev_id == CHIPREV_ID_5704_A0) &&
14143 (!(pci_state_reg & PCISTATE_RETRY_SAME_DMA))) {
14144 pci_state_reg |= PCISTATE_RETRY_SAME_DMA;
14145 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE, pci_state_reg);
14146 }
14147
Michael Chan1ee582d2005-08-09 20:16:46 -070014148 /* Default fast path register access methods */
Michael Chan20094932005-08-09 20:16:32 -070014149 tp->read32 = tg3_read32;
Michael Chan1ee582d2005-08-09 20:16:46 -070014150 tp->write32 = tg3_write32;
Michael Chan09ee9292005-08-09 20:17:00 -070014151 tp->read32_mbox = tg3_read32;
Michael Chan20094932005-08-09 20:16:32 -070014152 tp->write32_mbox = tg3_write32;
Michael Chan1ee582d2005-08-09 20:16:46 -070014153 tp->write32_tx_mbox = tg3_write32;
14154 tp->write32_rx_mbox = tg3_write32;
14155
14156 /* Various workaround register access methods */
Joe Perches63c3a662011-04-26 08:12:10 +000014157 if (tg3_flag(tp, PCIX_TARGET_HWBUG))
Michael Chan1ee582d2005-08-09 20:16:46 -070014158 tp->write32 = tg3_write_indirect_reg32;
Matt Carlson98efd8a2007-05-05 12:47:25 -070014159 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701 ||
Joe Perches63c3a662011-04-26 08:12:10 +000014160 (tg3_flag(tp, PCI_EXPRESS) &&
Matt Carlson98efd8a2007-05-05 12:47:25 -070014161 tp->pci_chip_rev_id == CHIPREV_ID_5750_A0)) {
14162 /*
14163 * Back to back register writes can cause problems on these
14164 * chips, the workaround is to read back all reg writes
14165 * except those to mailbox regs.
14166 *
14167 * See tg3_write_indirect_reg32().
14168 */
Michael Chan1ee582d2005-08-09 20:16:46 -070014169 tp->write32 = tg3_write_flush_reg32;
Matt Carlson98efd8a2007-05-05 12:47:25 -070014170 }
14171
Joe Perches63c3a662011-04-26 08:12:10 +000014172 if (tg3_flag(tp, TXD_MBOX_HWBUG) || tg3_flag(tp, MBOX_WRITE_REORDER)) {
Michael Chan1ee582d2005-08-09 20:16:46 -070014173 tp->write32_tx_mbox = tg3_write32_tx_mbox;
Joe Perches63c3a662011-04-26 08:12:10 +000014174 if (tg3_flag(tp, MBOX_WRITE_REORDER))
Michael Chan1ee582d2005-08-09 20:16:46 -070014175 tp->write32_rx_mbox = tg3_write_flush_reg32;
14176 }
Michael Chan20094932005-08-09 20:16:32 -070014177
Joe Perches63c3a662011-04-26 08:12:10 +000014178 if (tg3_flag(tp, ICH_WORKAROUND)) {
Michael Chan68929142005-08-09 20:17:14 -070014179 tp->read32 = tg3_read_indirect_reg32;
14180 tp->write32 = tg3_write_indirect_reg32;
14181 tp->read32_mbox = tg3_read_indirect_mbox;
14182 tp->write32_mbox = tg3_write_indirect_mbox;
14183 tp->write32_tx_mbox = tg3_write_indirect_mbox;
14184 tp->write32_rx_mbox = tg3_write_indirect_mbox;
14185
14186 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070014187 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070014188
14189 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
14190 pci_cmd &= ~PCI_COMMAND_MEMORY;
14191 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
14192 }
Michael Chanb5d37722006-09-27 16:06:21 -070014193 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
14194 tp->read32_mbox = tg3_read32_mbox_5906;
14195 tp->write32_mbox = tg3_write32_mbox_5906;
14196 tp->write32_tx_mbox = tg3_write32_mbox_5906;
14197 tp->write32_rx_mbox = tg3_write32_mbox_5906;
14198 }
Michael Chan68929142005-08-09 20:17:14 -070014199
Michael Chanbbadf502006-04-06 21:46:34 -070014200 if (tp->write32 == tg3_write_indirect_reg32 ||
Joe Perches63c3a662011-04-26 08:12:10 +000014201 (tg3_flag(tp, PCIX_MODE) &&
Michael Chanbbadf502006-04-06 21:46:34 -070014202 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
David S. Millerf49639e2006-06-09 11:58:36 -070014203 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)))
Joe Perches63c3a662011-04-26 08:12:10 +000014204 tg3_flag_set(tp, SRAM_USE_CONFIG);
Michael Chanbbadf502006-04-06 21:46:34 -070014205
Matt Carlson16821282011-07-13 09:27:28 +000014206 /* The memory arbiter has to be enabled in order for SRAM accesses
14207 * to succeed. Normally on powerup the tg3 chip firmware will make
14208 * sure it is enabled, but other entities such as system netboot
14209 * code might disable it.
14210 */
14211 val = tr32(MEMARB_MODE);
14212 tw32(MEMARB_MODE, val | MEMARB_MODE_ENABLE);
14213
Matt Carlson9dc5e342011-11-04 09:15:02 +000014214 tp->pci_fn = PCI_FUNC(tp->pdev->devfn) & 3;
14215 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
14216 tg3_flag(tp, 5780_CLASS)) {
14217 if (tg3_flag(tp, PCIX_MODE)) {
14218 pci_read_config_dword(tp->pdev,
14219 tp->pcix_cap + PCI_X_STATUS,
14220 &val);
14221 tp->pci_fn = val & 0x7;
14222 }
14223 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717) {
14224 tg3_read_mem(tp, NIC_SRAM_CPMU_STATUS, &val);
14225 if ((val & NIC_SRAM_CPMUSTAT_SIG_MSK) ==
14226 NIC_SRAM_CPMUSTAT_SIG) {
14227 tp->pci_fn = val & TG3_CPMU_STATUS_FMSK_5717;
14228 tp->pci_fn = tp->pci_fn ? 1 : 0;
14229 }
14230 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
14231 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
14232 tg3_read_mem(tp, NIC_SRAM_CPMU_STATUS, &val);
14233 if ((val & NIC_SRAM_CPMUSTAT_SIG_MSK) ==
14234 NIC_SRAM_CPMUSTAT_SIG) {
14235 tp->pci_fn = (val & TG3_CPMU_STATUS_FMSK_5719) >>
14236 TG3_CPMU_STATUS_FSHFT_5719;
14237 }
Matt Carlson69f11c92011-07-13 09:27:30 +000014238 }
14239
Michael Chan7d0c41e2005-04-21 17:06:20 -070014240 /* Get eeprom hw config before calling tg3_set_power_state().
Joe Perches63c3a662011-04-26 08:12:10 +000014241 * In particular, the TG3_FLAG_IS_NIC flag must be
Michael Chan7d0c41e2005-04-21 17:06:20 -070014242 * determined before calling tg3_set_power_state() so that
14243 * we know whether or not to switch out of Vaux power.
14244 * When the flag is set, it means that GPIO1 is used for eeprom
14245 * write protect and also implies that it is a LOM where GPIOs
14246 * are not used to switch power.
Jeff Garzik6aa20a22006-09-13 13:24:59 -040014247 */
Michael Chan7d0c41e2005-04-21 17:06:20 -070014248 tg3_get_eeprom_hw_cfg(tp);
14249
Joe Perches63c3a662011-04-26 08:12:10 +000014250 if (tg3_flag(tp, ENABLE_APE)) {
Matt Carlson0d3031d2007-10-10 18:02:43 -070014251 /* Allow reads and writes to the
14252 * APE register and memory space.
14253 */
14254 pci_state_reg |= PCISTATE_ALLOW_APE_CTLSPC_WR |
Matt Carlsonf92d9dc2010-06-05 17:24:30 +000014255 PCISTATE_ALLOW_APE_SHMEM_WR |
14256 PCISTATE_ALLOW_APE_PSPACE_WR;
Matt Carlson0d3031d2007-10-10 18:02:43 -070014257 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE,
14258 pci_state_reg);
Matt Carlsonc9cab242011-07-13 09:27:27 +000014259
14260 tg3_ape_lock_init(tp);
Matt Carlson0d3031d2007-10-10 18:02:43 -070014261 }
14262
Matt Carlson9936bcf2007-10-10 18:03:07 -070014263 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson57e69832008-05-25 23:48:31 -070014264 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson321d32a2008-11-21 17:22:19 -080014265 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000014266 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
Joe Perches63c3a662011-04-26 08:12:10 +000014267 tg3_flag(tp, 57765_PLUS))
14268 tg3_flag_set(tp, CPMU_PRESENT);
Matt Carlsond30cdd22007-10-07 23:28:35 -070014269
Matt Carlson16821282011-07-13 09:27:28 +000014270 /* Set up tp->grc_local_ctrl before calling
14271 * tg3_pwrsrc_switch_to_vmain(). GPIO1 driven high
14272 * will bring 5700's external PHY out of reset.
Michael Chan314fba32005-04-21 17:07:04 -070014273 * It is also used as eeprom write protect on LOMs.
14274 */
14275 tp->grc_local_ctrl = GRC_LCLCTRL_INT_ON_ATTN | GRC_LCLCTRL_AUTO_SEEPROM;
Matt Carlson6ff6f812011-05-19 12:12:54 +000014276 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
Joe Perches63c3a662011-04-26 08:12:10 +000014277 tg3_flag(tp, EEPROM_WRITE_PROT))
Michael Chan314fba32005-04-21 17:07:04 -070014278 tp->grc_local_ctrl |= (GRC_LCLCTRL_GPIO_OE1 |
14279 GRC_LCLCTRL_GPIO_OUTPUT1);
Michael Chan3e7d83b2005-04-21 17:10:36 -070014280 /* Unused GPIO3 must be driven as output on 5752 because there
14281 * are no pull-up resistors on unused GPIO pins.
14282 */
14283 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752)
14284 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE3;
Michael Chan314fba32005-04-21 17:07:04 -070014285
Matt Carlson321d32a2008-11-21 17:22:19 -080014286 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
Matt Carlsoncb4ed1f2010-01-20 16:58:09 +000014287 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
14288 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Michael Chanaf36e6b2006-03-23 01:28:06 -080014289 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_UART_SEL;
14290
Matt Carlson8d519ab2009-04-20 06:58:01 +000014291 if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761 ||
14292 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761S) {
Matt Carlson5f0c4a32008-06-09 15:41:12 -070014293 /* Turn off the debug UART. */
14294 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_UART_SEL;
Joe Perches63c3a662011-04-26 08:12:10 +000014295 if (tg3_flag(tp, IS_NIC))
Matt Carlson5f0c4a32008-06-09 15:41:12 -070014296 /* Keep VMain power. */
14297 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE0 |
14298 GRC_LCLCTRL_GPIO_OUTPUT0;
14299 }
14300
Matt Carlson16821282011-07-13 09:27:28 +000014301 /* Switch out of Vaux if it is a NIC */
14302 tg3_pwrsrc_switch_to_vmain(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014303
Linus Torvalds1da177e2005-04-16 15:20:36 -070014304 /* Derive initial jumbo mode from MTU assigned in
14305 * ether_setup() via the alloc_etherdev() call
14306 */
Joe Perches63c3a662011-04-26 08:12:10 +000014307 if (tp->dev->mtu > ETH_DATA_LEN && !tg3_flag(tp, 5780_CLASS))
14308 tg3_flag_set(tp, JUMBO_RING_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014309
14310 /* Determine WakeOnLan speed to use. */
14311 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
14312 tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
14313 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0 ||
14314 tp->pci_chip_rev_id == CHIPREV_ID_5701_B2) {
Joe Perches63c3a662011-04-26 08:12:10 +000014315 tg3_flag_clear(tp, WOL_SPEED_100MB);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014316 } else {
Joe Perches63c3a662011-04-26 08:12:10 +000014317 tg3_flag_set(tp, WOL_SPEED_100MB);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014318 }
14319
Matt Carlson7f97a4b2009-08-25 10:10:03 +000014320 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014321 tp->phy_flags |= TG3_PHYFLG_IS_FET;
Matt Carlson7f97a4b2009-08-25 10:10:03 +000014322
Linus Torvalds1da177e2005-04-16 15:20:36 -070014323 /* A few boards don't want Ethernet@WireSpeed phy feature */
Matt Carlson6ff6f812011-05-19 12:12:54 +000014324 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
14325 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070014326 (tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) &&
Michael Chan747e8f82005-07-25 12:33:22 -070014327 (tp->pci_chip_rev_id != CHIPREV_ID_5705_A1)) ||
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014328 (tp->phy_flags & TG3_PHYFLG_IS_FET) ||
14329 (tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
14330 tp->phy_flags |= TG3_PHYFLG_NO_ETH_WIRE_SPEED;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014331
14332 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5703_AX ||
14333 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5704_AX)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014334 tp->phy_flags |= TG3_PHYFLG_ADC_BUG;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014335 if (tp->pci_chip_rev_id == CHIPREV_ID_5704_A0)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014336 tp->phy_flags |= TG3_PHYFLG_5704_A0_BUG;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014337
Joe Perches63c3a662011-04-26 08:12:10 +000014338 if (tg3_flag(tp, 5705_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014339 !(tp->phy_flags & TG3_PHYFLG_IS_FET) &&
Matt Carlson321d32a2008-11-21 17:22:19 -080014340 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000014341 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_57780 &&
Joe Perches63c3a662011-04-26 08:12:10 +000014342 !tg3_flag(tp, 57765_PLUS)) {
Michael Chanc424cb22006-04-29 18:56:34 -070014343 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
Matt Carlsond30cdd22007-10-07 23:28:35 -070014344 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787 ||
Matt Carlson9936bcf2007-10-10 18:03:07 -070014345 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
14346 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761) {
Michael Chand4011ad2007-02-13 12:17:25 -080014347 if (tp->pdev->device != PCI_DEVICE_ID_TIGON3_5756 &&
14348 tp->pdev->device != PCI_DEVICE_ID_TIGON3_5722)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014349 tp->phy_flags |= TG3_PHYFLG_JITTER_BUG;
Michael Chanc1d2a192007-01-08 19:57:20 -080014350 if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5755M)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014351 tp->phy_flags |= TG3_PHYFLG_ADJUST_TRIM;
Matt Carlson321d32a2008-11-21 17:22:19 -080014352 } else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014353 tp->phy_flags |= TG3_PHYFLG_BER_BUG;
Michael Chanc424cb22006-04-29 18:56:34 -070014354 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014355
Matt Carlsonb2a5c192008-04-03 21:44:44 -070014356 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
14357 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX) {
14358 tp->phy_otp = tg3_read_otp_phycfg(tp);
14359 if (tp->phy_otp == 0)
14360 tp->phy_otp = TG3_OTP_DEFAULT;
14361 }
14362
Joe Perches63c3a662011-04-26 08:12:10 +000014363 if (tg3_flag(tp, CPMU_PRESENT))
Matt Carlson8ef21422008-05-02 16:47:53 -070014364 tp->mi_mode = MAC_MI_MODE_500KHZ_CONST;
14365 else
14366 tp->mi_mode = MAC_MI_MODE_BASE;
14367
Linus Torvalds1da177e2005-04-16 15:20:36 -070014368 tp->coalesce_mode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014369 if (GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5700_AX &&
14370 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5700_BX)
14371 tp->coalesce_mode |= HOSTCC_MODE_32BYTE;
14372
Matt Carlson4d958472011-04-20 07:57:35 +000014373 /* Set these bits to enable statistics workaround. */
14374 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
14375 tp->pci_chip_rev_id == CHIPREV_ID_5719_A0 ||
14376 tp->pci_chip_rev_id == CHIPREV_ID_5720_A0) {
14377 tp->coalesce_mode |= HOSTCC_MODE_ATTN;
14378 tp->grc_mode |= GRC_MODE_IRQ_ON_FLOW_ATTN;
14379 }
14380
Matt Carlson321d32a2008-11-21 17:22:19 -080014381 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
14382 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
Joe Perches63c3a662011-04-26 08:12:10 +000014383 tg3_flag_set(tp, USE_PHYLIB);
Matt Carlson57e69832008-05-25 23:48:31 -070014384
Matt Carlson158d7ab2008-05-29 01:37:54 -070014385 err = tg3_mdio_init(tp);
14386 if (err)
14387 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014388
14389 /* Initialize data/descriptor byte/word swapping. */
14390 val = tr32(GRC_MODE);
Matt Carlsonf2096f92011-04-05 14:22:48 +000014391 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
14392 val &= (GRC_MODE_BYTE_SWAP_B2HRX_DATA |
14393 GRC_MODE_WORD_SWAP_B2HRX_DATA |
14394 GRC_MODE_B2HRX_ENABLE |
14395 GRC_MODE_HTX2B_ENABLE |
14396 GRC_MODE_HOST_STACKUP);
14397 else
14398 val &= GRC_MODE_HOST_STACKUP;
14399
Linus Torvalds1da177e2005-04-16 15:20:36 -070014400 tw32(GRC_MODE, val | tp->grc_mode);
14401
14402 tg3_switch_clocks(tp);
14403
14404 /* Clear this out for sanity. */
14405 tw32(TG3PCI_MEM_WIN_BASE_ADDR, 0);
14406
14407 pci_read_config_dword(tp->pdev, TG3PCI_PCISTATE,
14408 &pci_state_reg);
14409 if ((pci_state_reg & PCISTATE_CONV_PCI_MODE) == 0 &&
Joe Perches63c3a662011-04-26 08:12:10 +000014410 !tg3_flag(tp, PCIX_TARGET_HWBUG)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070014411 u32 chiprevid = GET_CHIP_REV_ID(tp->misc_host_ctrl);
14412
14413 if (chiprevid == CHIPREV_ID_5701_A0 ||
14414 chiprevid == CHIPREV_ID_5701_B0 ||
14415 chiprevid == CHIPREV_ID_5701_B2 ||
14416 chiprevid == CHIPREV_ID_5701_B5) {
14417 void __iomem *sram_base;
14418
14419 /* Write some dummy words into the SRAM status block
14420 * area, see if it reads back correctly. If the return
14421 * value is bad, force enable the PCIX workaround.
14422 */
14423 sram_base = tp->regs + NIC_SRAM_WIN_BASE + NIC_SRAM_STATS_BLK;
14424
14425 writel(0x00000000, sram_base);
14426 writel(0x00000000, sram_base + 4);
14427 writel(0xffffffff, sram_base + 4);
14428 if (readl(sram_base) != 0x00000000)
Joe Perches63c3a662011-04-26 08:12:10 +000014429 tg3_flag_set(tp, PCIX_TARGET_HWBUG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014430 }
14431 }
14432
14433 udelay(50);
14434 tg3_nvram_init(tp);
14435
14436 grc_misc_cfg = tr32(GRC_MISC_CFG);
14437 grc_misc_cfg &= GRC_MISC_CFG_BOARD_ID_MASK;
14438
Linus Torvalds1da177e2005-04-16 15:20:36 -070014439 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
14440 (grc_misc_cfg == GRC_MISC_CFG_BOARD_ID_5788 ||
14441 grc_misc_cfg == GRC_MISC_CFG_BOARD_ID_5788M))
Joe Perches63c3a662011-04-26 08:12:10 +000014442 tg3_flag_set(tp, IS_5788);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014443
Joe Perches63c3a662011-04-26 08:12:10 +000014444 if (!tg3_flag(tp, IS_5788) &&
Matt Carlson6ff6f812011-05-19 12:12:54 +000014445 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700)
Joe Perches63c3a662011-04-26 08:12:10 +000014446 tg3_flag_set(tp, TAGGED_STATUS);
14447 if (tg3_flag(tp, TAGGED_STATUS)) {
David S. Millerfac9b832005-05-18 22:46:34 -070014448 tp->coalesce_mode |= (HOSTCC_MODE_CLRTICK_RXBD |
14449 HOSTCC_MODE_CLRTICK_TXBD);
14450
14451 tp->misc_host_ctrl |= MISC_HOST_CTRL_TAGGED_STATUS;
14452 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
14453 tp->misc_host_ctrl);
14454 }
14455
Matt Carlson3bda1252008-08-15 14:08:22 -070014456 /* Preserve the APE MAC_MODE bits */
Joe Perches63c3a662011-04-26 08:12:10 +000014457 if (tg3_flag(tp, ENABLE_APE))
Matt Carlsond2394e6b2010-11-24 08:31:47 +000014458 tp->mac_mode = MAC_MODE_APE_TX_EN | MAC_MODE_APE_RX_EN;
Matt Carlson3bda1252008-08-15 14:08:22 -070014459 else
Matt Carlson6e01b202011-08-19 13:58:20 +000014460 tp->mac_mode = 0;
Matt Carlson3bda1252008-08-15 14:08:22 -070014461
Linus Torvalds1da177e2005-04-16 15:20:36 -070014462 /* these are limited to 10/100 only */
14463 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 &&
14464 (grc_misc_cfg == 0x8000 || grc_misc_cfg == 0x4000)) ||
14465 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
14466 tp->pdev->vendor == PCI_VENDOR_ID_BROADCOM &&
14467 (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5901 ||
14468 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5901_2 ||
14469 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5705F)) ||
14470 (tp->pdev->vendor == PCI_VENDOR_ID_BROADCOM &&
14471 (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5751F ||
Michael Chan676917d2006-12-07 00:20:22 -080014472 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5753F ||
14473 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5787F)) ||
Matt Carlson321d32a2008-11-21 17:22:19 -080014474 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57790 ||
Matt Carlsond1101142010-02-17 15:16:55 +000014475 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57791 ||
14476 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57795 ||
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014477 (tp->phy_flags & TG3_PHYFLG_IS_FET))
14478 tp->phy_flags |= TG3_PHYFLG_10_100_ONLY;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014479
14480 err = tg3_phy_probe(tp);
14481 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000014482 dev_err(&tp->pdev->dev, "phy probe failed, err %d\n", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014483 /* ... but do not return immediately ... */
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070014484 tg3_mdio_fini(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014485 }
14486
Matt Carlson184b8902010-04-05 10:19:25 +000014487 tg3_read_vpd(tp);
Michael Chanc4e65752006-03-20 22:29:32 -080014488 tg3_read_fw_ver(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014489
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014490 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
14491 tp->phy_flags &= ~TG3_PHYFLG_USE_MI_INTERRUPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014492 } else {
14493 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014494 tp->phy_flags |= TG3_PHYFLG_USE_MI_INTERRUPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014495 else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014496 tp->phy_flags &= ~TG3_PHYFLG_USE_MI_INTERRUPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014497 }
14498
14499 /* 5700 {AX,BX} chips have a broken status block link
14500 * change bit implementation, so we must use the
14501 * status register in those cases.
14502 */
14503 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700)
Joe Perches63c3a662011-04-26 08:12:10 +000014504 tg3_flag_set(tp, USE_LINKCHG_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014505 else
Joe Perches63c3a662011-04-26 08:12:10 +000014506 tg3_flag_clear(tp, USE_LINKCHG_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014507
14508 /* The led_ctrl is set during tg3_phy_probe, here we might
14509 * have to force the link status polling mechanism based
14510 * upon subsystem IDs.
14511 */
14512 if (tp->pdev->subsystem_vendor == PCI_VENDOR_ID_DELL &&
Michael Chan007a880d2007-05-31 14:49:51 -070014513 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701 &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014514 !(tp->phy_flags & TG3_PHYFLG_PHY_SERDES)) {
14515 tp->phy_flags |= TG3_PHYFLG_USE_MI_INTERRUPT;
Joe Perches63c3a662011-04-26 08:12:10 +000014516 tg3_flag_set(tp, USE_LINKCHG_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014517 }
14518
14519 /* For all SERDES we poll the MAC status register. */
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014520 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Joe Perches63c3a662011-04-26 08:12:10 +000014521 tg3_flag_set(tp, POLL_SERDES);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014522 else
Joe Perches63c3a662011-04-26 08:12:10 +000014523 tg3_flag_clear(tp, POLL_SERDES);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014524
Eric Dumazet9205fd92011-11-18 06:47:01 +000014525 tp->rx_offset = NET_SKB_PAD + NET_IP_ALIGN;
Matt Carlsond2757fc2010-04-12 06:58:27 +000014526 tp->rx_copy_thresh = TG3_RX_COPY_THRESHOLD;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014527 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701 &&
Joe Perches63c3a662011-04-26 08:12:10 +000014528 tg3_flag(tp, PCIX_MODE)) {
Eric Dumazet9205fd92011-11-18 06:47:01 +000014529 tp->rx_offset = NET_SKB_PAD;
Matt Carlsond2757fc2010-04-12 06:58:27 +000014530#ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
Matt Carlson9dc7a112010-04-12 06:58:28 +000014531 tp->rx_copy_thresh = ~(u16)0;
Matt Carlsond2757fc2010-04-12 06:58:27 +000014532#endif
14533 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014534
Matt Carlson2c49a442010-09-30 10:34:35 +000014535 tp->rx_std_ring_mask = TG3_RX_STD_RING_SIZE(tp) - 1;
14536 tp->rx_jmb_ring_mask = TG3_RX_JMB_RING_SIZE(tp) - 1;
Matt Carlson7cb32cf2010-09-30 10:34:36 +000014537 tp->rx_ret_ring_mask = tg3_rx_ret_ring_size(tp) - 1;
14538
Matt Carlson2c49a442010-09-30 10:34:35 +000014539 tp->rx_std_max_post = tp->rx_std_ring_mask + 1;
Michael Chanf92905d2006-06-29 20:14:29 -070014540
14541 /* Increment the rx prod index on the rx std ring by at most
14542 * 8 for these chips to workaround hw errata.
14543 */
14544 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 ||
14545 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752 ||
14546 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755)
14547 tp->rx_std_max_post = 8;
14548
Joe Perches63c3a662011-04-26 08:12:10 +000014549 if (tg3_flag(tp, ASPM_WORKAROUND))
Matt Carlson8ed5d972007-05-07 00:25:49 -070014550 tp->pwrmgmt_thresh = tr32(PCIE_PWR_MGMT_THRESH) &
14551 PCIE_PWR_MGMT_L1_THRESH_MSK;
14552
Linus Torvalds1da177e2005-04-16 15:20:36 -070014553 return err;
14554}
14555
David S. Miller49b6e95f2007-03-29 01:38:42 -070014556#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070014557static int __devinit tg3_get_macaddr_sparc(struct tg3 *tp)
14558{
14559 struct net_device *dev = tp->dev;
14560 struct pci_dev *pdev = tp->pdev;
David S. Miller49b6e95f2007-03-29 01:38:42 -070014561 struct device_node *dp = pci_device_to_OF_node(pdev);
David S. Miller374d4ca2007-03-29 01:57:57 -070014562 const unsigned char *addr;
David S. Miller49b6e95f2007-03-29 01:38:42 -070014563 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014564
David S. Miller49b6e95f2007-03-29 01:38:42 -070014565 addr = of_get_property(dp, "local-mac-address", &len);
14566 if (addr && len == 6) {
14567 memcpy(dev->dev_addr, addr, 6);
14568 memcpy(dev->perm_addr, dev->dev_addr, 6);
14569 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014570 }
14571 return -ENODEV;
14572}
14573
14574static int __devinit tg3_get_default_macaddr_sparc(struct tg3 *tp)
14575{
14576 struct net_device *dev = tp->dev;
14577
14578 memcpy(dev->dev_addr, idprom->id_ethaddr, 6);
John W. Linville2ff43692005-09-12 14:44:20 -070014579 memcpy(dev->perm_addr, idprom->id_ethaddr, 6);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014580 return 0;
14581}
14582#endif
14583
14584static int __devinit tg3_get_device_address(struct tg3 *tp)
14585{
14586 struct net_device *dev = tp->dev;
14587 u32 hi, lo, mac_offset;
Michael Chan008652b2006-03-27 23:14:53 -080014588 int addr_ok = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014589
David S. Miller49b6e95f2007-03-29 01:38:42 -070014590#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070014591 if (!tg3_get_macaddr_sparc(tp))
14592 return 0;
14593#endif
14594
14595 mac_offset = 0x7c;
Matt Carlson6ff6f812011-05-19 12:12:54 +000014596 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
Joe Perches63c3a662011-04-26 08:12:10 +000014597 tg3_flag(tp, 5780_CLASS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070014598 if (tr32(TG3PCI_DUAL_MAC_CTRL) & DUAL_MAC_CTRL_ID)
14599 mac_offset = 0xcc;
14600 if (tg3_nvram_lock(tp))
14601 tw32_f(NVRAM_CMD, NVRAM_CMD_RESET);
14602 else
14603 tg3_nvram_unlock(tp);
Joe Perches63c3a662011-04-26 08:12:10 +000014604 } else if (tg3_flag(tp, 5717_PLUS)) {
Matt Carlson69f11c92011-07-13 09:27:30 +000014605 if (tp->pci_fn & 1)
Matt Carlsona1b950d2009-09-01 13:20:17 +000014606 mac_offset = 0xcc;
Matt Carlson69f11c92011-07-13 09:27:30 +000014607 if (tp->pci_fn > 1)
Matt Carlsona50d0792010-06-05 17:24:37 +000014608 mac_offset += 0x18c;
Matt Carlsona1b950d2009-09-01 13:20:17 +000014609 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
Michael Chanb5d37722006-09-27 16:06:21 -070014610 mac_offset = 0x10;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014611
14612 /* First try to get it from MAC address mailbox. */
14613 tg3_read_mem(tp, NIC_SRAM_MAC_ADDR_HIGH_MBOX, &hi);
14614 if ((hi >> 16) == 0x484b) {
14615 dev->dev_addr[0] = (hi >> 8) & 0xff;
14616 dev->dev_addr[1] = (hi >> 0) & 0xff;
14617
14618 tg3_read_mem(tp, NIC_SRAM_MAC_ADDR_LOW_MBOX, &lo);
14619 dev->dev_addr[2] = (lo >> 24) & 0xff;
14620 dev->dev_addr[3] = (lo >> 16) & 0xff;
14621 dev->dev_addr[4] = (lo >> 8) & 0xff;
14622 dev->dev_addr[5] = (lo >> 0) & 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014623
Michael Chan008652b2006-03-27 23:14:53 -080014624 /* Some old bootcode may report a 0 MAC address in SRAM */
14625 addr_ok = is_valid_ether_addr(&dev->dev_addr[0]);
14626 }
14627 if (!addr_ok) {
14628 /* Next, try NVRAM. */
Joe Perches63c3a662011-04-26 08:12:10 +000014629 if (!tg3_flag(tp, NO_NVRAM) &&
Matt Carlsondf259d82009-04-20 06:57:14 +000014630 !tg3_nvram_read_be32(tp, mac_offset + 0, &hi) &&
Matt Carlson6d348f22009-02-25 14:25:52 +000014631 !tg3_nvram_read_be32(tp, mac_offset + 4, &lo)) {
Matt Carlson62cedd12009-04-20 14:52:29 -070014632 memcpy(&dev->dev_addr[0], ((char *)&hi) + 2, 2);
14633 memcpy(&dev->dev_addr[2], (char *)&lo, sizeof(lo));
Michael Chan008652b2006-03-27 23:14:53 -080014634 }
14635 /* Finally just fetch it out of the MAC control regs. */
14636 else {
14637 hi = tr32(MAC_ADDR_0_HIGH);
14638 lo = tr32(MAC_ADDR_0_LOW);
14639
14640 dev->dev_addr[5] = lo & 0xff;
14641 dev->dev_addr[4] = (lo >> 8) & 0xff;
14642 dev->dev_addr[3] = (lo >> 16) & 0xff;
14643 dev->dev_addr[2] = (lo >> 24) & 0xff;
14644 dev->dev_addr[1] = hi & 0xff;
14645 dev->dev_addr[0] = (hi >> 8) & 0xff;
14646 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014647 }
14648
14649 if (!is_valid_ether_addr(&dev->dev_addr[0])) {
David S. Miller7582a332008-03-20 15:53:15 -070014650#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070014651 if (!tg3_get_default_macaddr_sparc(tp))
14652 return 0;
14653#endif
14654 return -EINVAL;
14655 }
John W. Linville2ff43692005-09-12 14:44:20 -070014656 memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014657 return 0;
14658}
14659
David S. Miller59e6b432005-05-18 22:50:10 -070014660#define BOUNDARY_SINGLE_CACHELINE 1
14661#define BOUNDARY_MULTI_CACHELINE 2
14662
14663static u32 __devinit tg3_calc_dma_bndry(struct tg3 *tp, u32 val)
14664{
14665 int cacheline_size;
14666 u8 byte;
14667 int goal;
14668
14669 pci_read_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE, &byte);
14670 if (byte == 0)
14671 cacheline_size = 1024;
14672 else
14673 cacheline_size = (int) byte * 4;
14674
14675 /* On 5703 and later chips, the boundary bits have no
14676 * effect.
14677 */
14678 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
14679 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701 &&
Joe Perches63c3a662011-04-26 08:12:10 +000014680 !tg3_flag(tp, PCI_EXPRESS))
David S. Miller59e6b432005-05-18 22:50:10 -070014681 goto out;
14682
14683#if defined(CONFIG_PPC64) || defined(CONFIG_IA64) || defined(CONFIG_PARISC)
14684 goal = BOUNDARY_MULTI_CACHELINE;
14685#else
14686#if defined(CONFIG_SPARC64) || defined(CONFIG_ALPHA)
14687 goal = BOUNDARY_SINGLE_CACHELINE;
14688#else
14689 goal = 0;
14690#endif
14691#endif
14692
Joe Perches63c3a662011-04-26 08:12:10 +000014693 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000014694 val = goal ? 0 : DMA_RWCTRL_DIS_CACHE_ALIGNMENT;
14695 goto out;
14696 }
14697
David S. Miller59e6b432005-05-18 22:50:10 -070014698 if (!goal)
14699 goto out;
14700
14701 /* PCI controllers on most RISC systems tend to disconnect
14702 * when a device tries to burst across a cache-line boundary.
14703 * Therefore, letting tg3 do so just wastes PCI bandwidth.
14704 *
14705 * Unfortunately, for PCI-E there are only limited
14706 * write-side controls for this, and thus for reads
14707 * we will still get the disconnects. We'll also waste
14708 * these PCI cycles for both read and write for chips
14709 * other than 5700 and 5701 which do not implement the
14710 * boundary bits.
14711 */
Joe Perches63c3a662011-04-26 08:12:10 +000014712 if (tg3_flag(tp, PCIX_MODE) && !tg3_flag(tp, PCI_EXPRESS)) {
David S. Miller59e6b432005-05-18 22:50:10 -070014713 switch (cacheline_size) {
14714 case 16:
14715 case 32:
14716 case 64:
14717 case 128:
14718 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14719 val |= (DMA_RWCTRL_READ_BNDRY_128_PCIX |
14720 DMA_RWCTRL_WRITE_BNDRY_128_PCIX);
14721 } else {
14722 val |= (DMA_RWCTRL_READ_BNDRY_384_PCIX |
14723 DMA_RWCTRL_WRITE_BNDRY_384_PCIX);
14724 }
14725 break;
14726
14727 case 256:
14728 val |= (DMA_RWCTRL_READ_BNDRY_256_PCIX |
14729 DMA_RWCTRL_WRITE_BNDRY_256_PCIX);
14730 break;
14731
14732 default:
14733 val |= (DMA_RWCTRL_READ_BNDRY_384_PCIX |
14734 DMA_RWCTRL_WRITE_BNDRY_384_PCIX);
14735 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070014736 }
Joe Perches63c3a662011-04-26 08:12:10 +000014737 } else if (tg3_flag(tp, PCI_EXPRESS)) {
David S. Miller59e6b432005-05-18 22:50:10 -070014738 switch (cacheline_size) {
14739 case 16:
14740 case 32:
14741 case 64:
14742 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14743 val &= ~DMA_RWCTRL_WRITE_BNDRY_DISAB_PCIE;
14744 val |= DMA_RWCTRL_WRITE_BNDRY_64_PCIE;
14745 break;
14746 }
14747 /* fallthrough */
14748 case 128:
14749 default:
14750 val &= ~DMA_RWCTRL_WRITE_BNDRY_DISAB_PCIE;
14751 val |= DMA_RWCTRL_WRITE_BNDRY_128_PCIE;
14752 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070014753 }
David S. Miller59e6b432005-05-18 22:50:10 -070014754 } else {
14755 switch (cacheline_size) {
14756 case 16:
14757 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14758 val |= (DMA_RWCTRL_READ_BNDRY_16 |
14759 DMA_RWCTRL_WRITE_BNDRY_16);
14760 break;
14761 }
14762 /* fallthrough */
14763 case 32:
14764 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14765 val |= (DMA_RWCTRL_READ_BNDRY_32 |
14766 DMA_RWCTRL_WRITE_BNDRY_32);
14767 break;
14768 }
14769 /* fallthrough */
14770 case 64:
14771 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14772 val |= (DMA_RWCTRL_READ_BNDRY_64 |
14773 DMA_RWCTRL_WRITE_BNDRY_64);
14774 break;
14775 }
14776 /* fallthrough */
14777 case 128:
14778 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14779 val |= (DMA_RWCTRL_READ_BNDRY_128 |
14780 DMA_RWCTRL_WRITE_BNDRY_128);
14781 break;
14782 }
14783 /* fallthrough */
14784 case 256:
14785 val |= (DMA_RWCTRL_READ_BNDRY_256 |
14786 DMA_RWCTRL_WRITE_BNDRY_256);
14787 break;
14788 case 512:
14789 val |= (DMA_RWCTRL_READ_BNDRY_512 |
14790 DMA_RWCTRL_WRITE_BNDRY_512);
14791 break;
14792 case 1024:
14793 default:
14794 val |= (DMA_RWCTRL_READ_BNDRY_1024 |
14795 DMA_RWCTRL_WRITE_BNDRY_1024);
14796 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070014797 }
David S. Miller59e6b432005-05-18 22:50:10 -070014798 }
14799
14800out:
14801 return val;
14802}
14803
Linus Torvalds1da177e2005-04-16 15:20:36 -070014804static int __devinit tg3_do_test_dma(struct tg3 *tp, u32 *buf, dma_addr_t buf_dma, int size, int to_device)
14805{
14806 struct tg3_internal_buffer_desc test_desc;
14807 u32 sram_dma_descs;
14808 int i, ret;
14809
14810 sram_dma_descs = NIC_SRAM_DMA_DESC_POOL_BASE;
14811
14812 tw32(FTQ_RCVBD_COMP_FIFO_ENQDEQ, 0);
14813 tw32(FTQ_RCVDATA_COMP_FIFO_ENQDEQ, 0);
14814 tw32(RDMAC_STATUS, 0);
14815 tw32(WDMAC_STATUS, 0);
14816
14817 tw32(BUFMGR_MODE, 0);
14818 tw32(FTQ_RESET, 0);
14819
14820 test_desc.addr_hi = ((u64) buf_dma) >> 32;
14821 test_desc.addr_lo = buf_dma & 0xffffffff;
14822 test_desc.nic_mbuf = 0x00002100;
14823 test_desc.len = size;
14824
14825 /*
14826 * HP ZX1 was seeing test failures for 5701 cards running at 33Mhz
14827 * the *second* time the tg3 driver was getting loaded after an
14828 * initial scan.
14829 *
14830 * Broadcom tells me:
14831 * ...the DMA engine is connected to the GRC block and a DMA
14832 * reset may affect the GRC block in some unpredictable way...
14833 * The behavior of resets to individual blocks has not been tested.
14834 *
14835 * Broadcom noted the GRC reset will also reset all sub-components.
14836 */
14837 if (to_device) {
14838 test_desc.cqid_sqid = (13 << 8) | 2;
14839
14840 tw32_f(RDMAC_MODE, RDMAC_MODE_ENABLE);
14841 udelay(40);
14842 } else {
14843 test_desc.cqid_sqid = (16 << 8) | 7;
14844
14845 tw32_f(WDMAC_MODE, WDMAC_MODE_ENABLE);
14846 udelay(40);
14847 }
14848 test_desc.flags = 0x00000005;
14849
14850 for (i = 0; i < (sizeof(test_desc) / sizeof(u32)); i++) {
14851 u32 val;
14852
14853 val = *(((u32 *)&test_desc) + i);
14854 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR,
14855 sram_dma_descs + (i * sizeof(u32)));
14856 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_DATA, val);
14857 }
14858 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, 0);
14859
Matt Carlson859a5882010-04-05 10:19:28 +000014860 if (to_device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014861 tw32(FTQ_DMA_HIGH_READ_FIFO_ENQDEQ, sram_dma_descs);
Matt Carlson859a5882010-04-05 10:19:28 +000014862 else
Linus Torvalds1da177e2005-04-16 15:20:36 -070014863 tw32(FTQ_DMA_HIGH_WRITE_FIFO_ENQDEQ, sram_dma_descs);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014864
14865 ret = -ENODEV;
14866 for (i = 0; i < 40; i++) {
14867 u32 val;
14868
14869 if (to_device)
14870 val = tr32(FTQ_RCVBD_COMP_FIFO_ENQDEQ);
14871 else
14872 val = tr32(FTQ_RCVDATA_COMP_FIFO_ENQDEQ);
14873 if ((val & 0xffff) == sram_dma_descs) {
14874 ret = 0;
14875 break;
14876 }
14877
14878 udelay(100);
14879 }
14880
14881 return ret;
14882}
14883
David S. Millerded73402005-05-23 13:59:47 -070014884#define TEST_BUFFER_SIZE 0x2000
Linus Torvalds1da177e2005-04-16 15:20:36 -070014885
Matt Carlson41434702011-03-09 16:58:22 +000014886static DEFINE_PCI_DEVICE_TABLE(tg3_dma_wait_state_chipsets) = {
Joe Perches895950c2010-12-21 02:16:08 -080014887 { PCI_DEVICE(PCI_VENDOR_ID_APPLE, PCI_DEVICE_ID_APPLE_UNI_N_PCI15) },
14888 { },
14889};
14890
Linus Torvalds1da177e2005-04-16 15:20:36 -070014891static int __devinit tg3_test_dma(struct tg3 *tp)
14892{
14893 dma_addr_t buf_dma;
David S. Miller59e6b432005-05-18 22:50:10 -070014894 u32 *buf, saved_dma_rwctrl;
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000014895 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014896
Matt Carlson4bae65c2010-11-24 08:31:52 +000014897 buf = dma_alloc_coherent(&tp->pdev->dev, TEST_BUFFER_SIZE,
14898 &buf_dma, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014899 if (!buf) {
14900 ret = -ENOMEM;
14901 goto out_nofree;
14902 }
14903
14904 tp->dma_rwctrl = ((0x7 << DMA_RWCTRL_PCI_WRITE_CMD_SHIFT) |
14905 (0x6 << DMA_RWCTRL_PCI_READ_CMD_SHIFT));
14906
David S. Miller59e6b432005-05-18 22:50:10 -070014907 tp->dma_rwctrl = tg3_calc_dma_bndry(tp, tp->dma_rwctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014908
Joe Perches63c3a662011-04-26 08:12:10 +000014909 if (tg3_flag(tp, 57765_PLUS))
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000014910 goto out;
14911
Joe Perches63c3a662011-04-26 08:12:10 +000014912 if (tg3_flag(tp, PCI_EXPRESS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070014913 /* DMA read watermark not used on PCIE */
14914 tp->dma_rwctrl |= 0x00180000;
Joe Perches63c3a662011-04-26 08:12:10 +000014915 } else if (!tg3_flag(tp, PCIX_MODE)) {
Michael Chan85e94ce2005-04-21 17:05:28 -070014916 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 ||
14917 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014918 tp->dma_rwctrl |= 0x003f0000;
14919 else
14920 tp->dma_rwctrl |= 0x003f000f;
14921 } else {
14922 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
14923 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
14924 u32 ccval = (tr32(TG3PCI_CLOCK_CTRL) & 0x1f);
Michael Chan49afdeb2007-02-13 12:17:03 -080014925 u32 read_water = 0x7;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014926
Michael Chan4a29cc22006-03-19 13:21:12 -080014927 /* If the 5704 is behind the EPB bridge, we can
14928 * do the less restrictive ONE_DMA workaround for
14929 * better performance.
14930 */
Joe Perches63c3a662011-04-26 08:12:10 +000014931 if (tg3_flag(tp, 40BIT_DMA_BUG) &&
Michael Chan4a29cc22006-03-19 13:21:12 -080014932 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704)
14933 tp->dma_rwctrl |= 0x8000;
14934 else if (ccval == 0x6 || ccval == 0x7)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014935 tp->dma_rwctrl |= DMA_RWCTRL_ONE_DMA;
14936
Michael Chan49afdeb2007-02-13 12:17:03 -080014937 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703)
14938 read_water = 4;
David S. Miller59e6b432005-05-18 22:50:10 -070014939 /* Set bit 23 to enable PCIX hw bug fix */
Michael Chan49afdeb2007-02-13 12:17:03 -080014940 tp->dma_rwctrl |=
14941 (read_water << DMA_RWCTRL_READ_WATER_SHIFT) |
14942 (0x3 << DMA_RWCTRL_WRITE_WATER_SHIFT) |
14943 (1 << 23);
Michael Chan4cf78e42005-07-25 12:29:19 -070014944 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780) {
14945 /* 5780 always in PCIX mode */
14946 tp->dma_rwctrl |= 0x00144000;
Michael Chana4e2b342005-10-26 15:46:52 -070014947 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
14948 /* 5714 always in PCIX mode */
14949 tp->dma_rwctrl |= 0x00148000;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014950 } else {
14951 tp->dma_rwctrl |= 0x001b000f;
14952 }
14953 }
14954
14955 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
14956 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704)
14957 tp->dma_rwctrl &= 0xfffffff0;
14958
14959 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
14960 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
14961 /* Remove this if it causes problems for some boards. */
14962 tp->dma_rwctrl |= DMA_RWCTRL_USE_MEM_READ_MULT;
14963
14964 /* On 5700/5701 chips, we need to set this bit.
14965 * Otherwise the chip will issue cacheline transactions
14966 * to streamable DMA memory with not all the byte
14967 * enables turned on. This is an error on several
14968 * RISC PCI controllers, in particular sparc64.
14969 *
14970 * On 5703/5704 chips, this bit has been reassigned
14971 * a different meaning. In particular, it is used
14972 * on those chips to enable a PCI-X workaround.
14973 */
14974 tp->dma_rwctrl |= DMA_RWCTRL_ASSERT_ALL_BE;
14975 }
14976
14977 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
14978
14979#if 0
14980 /* Unneeded, already done by tg3_get_invariants. */
14981 tg3_switch_clocks(tp);
14982#endif
14983
Linus Torvalds1da177e2005-04-16 15:20:36 -070014984 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
14985 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701)
14986 goto out;
14987
David S. Miller59e6b432005-05-18 22:50:10 -070014988 /* It is best to perform DMA test with maximum write burst size
14989 * to expose the 5700/5701 write DMA bug.
14990 */
14991 saved_dma_rwctrl = tp->dma_rwctrl;
14992 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
14993 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
14994
Linus Torvalds1da177e2005-04-16 15:20:36 -070014995 while (1) {
14996 u32 *p = buf, i;
14997
14998 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++)
14999 p[i] = i;
15000
15001 /* Send the buffer to the chip. */
15002 ret = tg3_do_test_dma(tp, buf, buf_dma, TEST_BUFFER_SIZE, 1);
15003 if (ret) {
Matt Carlson2445e462010-04-05 10:19:21 +000015004 dev_err(&tp->pdev->dev,
15005 "%s: Buffer write failed. err = %d\n",
15006 __func__, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015007 break;
15008 }
15009
15010#if 0
15011 /* validate data reached card RAM correctly. */
15012 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++) {
15013 u32 val;
15014 tg3_read_mem(tp, 0x2100 + (i*4), &val);
15015 if (le32_to_cpu(val) != p[i]) {
Matt Carlson2445e462010-04-05 10:19:21 +000015016 dev_err(&tp->pdev->dev,
15017 "%s: Buffer corrupted on device! "
15018 "(%d != %d)\n", __func__, val, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015019 /* ret = -ENODEV here? */
15020 }
15021 p[i] = 0;
15022 }
15023#endif
15024 /* Now read it back. */
15025 ret = tg3_do_test_dma(tp, buf, buf_dma, TEST_BUFFER_SIZE, 0);
15026 if (ret) {
Matt Carlson5129c3a2010-04-05 10:19:23 +000015027 dev_err(&tp->pdev->dev, "%s: Buffer read failed. "
15028 "err = %d\n", __func__, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015029 break;
15030 }
15031
15032 /* Verify it. */
15033 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++) {
15034 if (p[i] == i)
15035 continue;
15036
David S. Miller59e6b432005-05-18 22:50:10 -070015037 if ((tp->dma_rwctrl & DMA_RWCTRL_WRITE_BNDRY_MASK) !=
15038 DMA_RWCTRL_WRITE_BNDRY_16) {
15039 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015040 tp->dma_rwctrl |= DMA_RWCTRL_WRITE_BNDRY_16;
15041 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
15042 break;
15043 } else {
Matt Carlson2445e462010-04-05 10:19:21 +000015044 dev_err(&tp->pdev->dev,
15045 "%s: Buffer corrupted on read back! "
15046 "(%d != %d)\n", __func__, p[i], i);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015047 ret = -ENODEV;
15048 goto out;
15049 }
15050 }
15051
15052 if (i == (TEST_BUFFER_SIZE / sizeof(u32))) {
15053 /* Success. */
15054 ret = 0;
15055 break;
15056 }
15057 }
David S. Miller59e6b432005-05-18 22:50:10 -070015058 if ((tp->dma_rwctrl & DMA_RWCTRL_WRITE_BNDRY_MASK) !=
15059 DMA_RWCTRL_WRITE_BNDRY_16) {
15060 /* DMA test passed without adjusting DMA boundary,
Michael Chan6d1cfba2005-06-08 14:13:14 -070015061 * now look for chipsets that are known to expose the
15062 * DMA bug without failing the test.
David S. Miller59e6b432005-05-18 22:50:10 -070015063 */
Matt Carlson41434702011-03-09 16:58:22 +000015064 if (pci_dev_present(tg3_dma_wait_state_chipsets)) {
Michael Chan6d1cfba2005-06-08 14:13:14 -070015065 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
15066 tp->dma_rwctrl |= DMA_RWCTRL_WRITE_BNDRY_16;
Matt Carlson859a5882010-04-05 10:19:28 +000015067 } else {
Michael Chan6d1cfba2005-06-08 14:13:14 -070015068 /* Safe to use the calculated DMA boundary. */
15069 tp->dma_rwctrl = saved_dma_rwctrl;
Matt Carlson859a5882010-04-05 10:19:28 +000015070 }
Michael Chan6d1cfba2005-06-08 14:13:14 -070015071
David S. Miller59e6b432005-05-18 22:50:10 -070015072 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
15073 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015074
15075out:
Matt Carlson4bae65c2010-11-24 08:31:52 +000015076 dma_free_coherent(&tp->pdev->dev, TEST_BUFFER_SIZE, buf, buf_dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015077out_nofree:
15078 return ret;
15079}
15080
Linus Torvalds1da177e2005-04-16 15:20:36 -070015081static void __devinit tg3_init_bufmgr_config(struct tg3 *tp)
15082{
Joe Perches63c3a662011-04-26 08:12:10 +000015083 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlson666bc832010-01-20 16:58:03 +000015084 tp->bufmgr_config.mbuf_read_dma_low_water =
15085 DEFAULT_MB_RDMA_LOW_WATER_5705;
15086 tp->bufmgr_config.mbuf_mac_rx_low_water =
15087 DEFAULT_MB_MACRX_LOW_WATER_57765;
15088 tp->bufmgr_config.mbuf_high_water =
15089 DEFAULT_MB_HIGH_WATER_57765;
15090
15091 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
15092 DEFAULT_MB_RDMA_LOW_WATER_5705;
15093 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
15094 DEFAULT_MB_MACRX_LOW_WATER_JUMBO_57765;
15095 tp->bufmgr_config.mbuf_high_water_jumbo =
15096 DEFAULT_MB_HIGH_WATER_JUMBO_57765;
Joe Perches63c3a662011-04-26 08:12:10 +000015097 } else if (tg3_flag(tp, 5705_PLUS)) {
Michael Chanfdfec172005-07-25 12:31:48 -070015098 tp->bufmgr_config.mbuf_read_dma_low_water =
15099 DEFAULT_MB_RDMA_LOW_WATER_5705;
15100 tp->bufmgr_config.mbuf_mac_rx_low_water =
15101 DEFAULT_MB_MACRX_LOW_WATER_5705;
15102 tp->bufmgr_config.mbuf_high_water =
15103 DEFAULT_MB_HIGH_WATER_5705;
Michael Chanb5d37722006-09-27 16:06:21 -070015104 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
15105 tp->bufmgr_config.mbuf_mac_rx_low_water =
15106 DEFAULT_MB_MACRX_LOW_WATER_5906;
15107 tp->bufmgr_config.mbuf_high_water =
15108 DEFAULT_MB_HIGH_WATER_5906;
15109 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015110
Michael Chanfdfec172005-07-25 12:31:48 -070015111 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
15112 DEFAULT_MB_RDMA_LOW_WATER_JUMBO_5780;
15113 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
15114 DEFAULT_MB_MACRX_LOW_WATER_JUMBO_5780;
15115 tp->bufmgr_config.mbuf_high_water_jumbo =
15116 DEFAULT_MB_HIGH_WATER_JUMBO_5780;
15117 } else {
15118 tp->bufmgr_config.mbuf_read_dma_low_water =
15119 DEFAULT_MB_RDMA_LOW_WATER;
15120 tp->bufmgr_config.mbuf_mac_rx_low_water =
15121 DEFAULT_MB_MACRX_LOW_WATER;
15122 tp->bufmgr_config.mbuf_high_water =
15123 DEFAULT_MB_HIGH_WATER;
15124
15125 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
15126 DEFAULT_MB_RDMA_LOW_WATER_JUMBO;
15127 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
15128 DEFAULT_MB_MACRX_LOW_WATER_JUMBO;
15129 tp->bufmgr_config.mbuf_high_water_jumbo =
15130 DEFAULT_MB_HIGH_WATER_JUMBO;
15131 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015132
15133 tp->bufmgr_config.dma_low_water = DEFAULT_DMA_LOW_WATER;
15134 tp->bufmgr_config.dma_high_water = DEFAULT_DMA_HIGH_WATER;
15135}
15136
15137static char * __devinit tg3_phy_string(struct tg3 *tp)
15138{
Matt Carlson79eb6902010-02-17 15:17:03 +000015139 switch (tp->phy_id & TG3_PHY_ID_MASK) {
15140 case TG3_PHY_ID_BCM5400: return "5400";
15141 case TG3_PHY_ID_BCM5401: return "5401";
15142 case TG3_PHY_ID_BCM5411: return "5411";
15143 case TG3_PHY_ID_BCM5701: return "5701";
15144 case TG3_PHY_ID_BCM5703: return "5703";
15145 case TG3_PHY_ID_BCM5704: return "5704";
15146 case TG3_PHY_ID_BCM5705: return "5705";
15147 case TG3_PHY_ID_BCM5750: return "5750";
15148 case TG3_PHY_ID_BCM5752: return "5752";
15149 case TG3_PHY_ID_BCM5714: return "5714";
15150 case TG3_PHY_ID_BCM5780: return "5780";
15151 case TG3_PHY_ID_BCM5755: return "5755";
15152 case TG3_PHY_ID_BCM5787: return "5787";
15153 case TG3_PHY_ID_BCM5784: return "5784";
15154 case TG3_PHY_ID_BCM5756: return "5722/5756";
15155 case TG3_PHY_ID_BCM5906: return "5906";
15156 case TG3_PHY_ID_BCM5761: return "5761";
15157 case TG3_PHY_ID_BCM5718C: return "5718C";
15158 case TG3_PHY_ID_BCM5718S: return "5718S";
15159 case TG3_PHY_ID_BCM57765: return "57765";
Matt Carlson302b5002010-06-05 17:24:38 +000015160 case TG3_PHY_ID_BCM5719C: return "5719C";
Matt Carlson6418f2c2011-04-05 14:22:49 +000015161 case TG3_PHY_ID_BCM5720C: return "5720C";
Matt Carlson79eb6902010-02-17 15:17:03 +000015162 case TG3_PHY_ID_BCM8002: return "8002/serdes";
Linus Torvalds1da177e2005-04-16 15:20:36 -070015163 case 0: return "serdes";
15164 default: return "unknown";
Stephen Hemminger855e1112008-04-16 16:37:28 -070015165 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015166}
15167
Michael Chanf9804dd2005-09-27 12:13:10 -070015168static char * __devinit tg3_bus_string(struct tg3 *tp, char *str)
15169{
Joe Perches63c3a662011-04-26 08:12:10 +000015170 if (tg3_flag(tp, PCI_EXPRESS)) {
Michael Chanf9804dd2005-09-27 12:13:10 -070015171 strcpy(str, "PCI Express");
15172 return str;
Joe Perches63c3a662011-04-26 08:12:10 +000015173 } else if (tg3_flag(tp, PCIX_MODE)) {
Michael Chanf9804dd2005-09-27 12:13:10 -070015174 u32 clock_ctrl = tr32(TG3PCI_CLOCK_CTRL) & 0x1f;
15175
15176 strcpy(str, "PCIX:");
15177
15178 if ((clock_ctrl == 7) ||
15179 ((tr32(GRC_MISC_CFG) & GRC_MISC_CFG_BOARD_ID_MASK) ==
15180 GRC_MISC_CFG_BOARD_ID_5704CIOBE))
15181 strcat(str, "133MHz");
15182 else if (clock_ctrl == 0)
15183 strcat(str, "33MHz");
15184 else if (clock_ctrl == 2)
15185 strcat(str, "50MHz");
15186 else if (clock_ctrl == 4)
15187 strcat(str, "66MHz");
15188 else if (clock_ctrl == 6)
15189 strcat(str, "100MHz");
Michael Chanf9804dd2005-09-27 12:13:10 -070015190 } else {
15191 strcpy(str, "PCI:");
Joe Perches63c3a662011-04-26 08:12:10 +000015192 if (tg3_flag(tp, PCI_HIGH_SPEED))
Michael Chanf9804dd2005-09-27 12:13:10 -070015193 strcat(str, "66MHz");
15194 else
15195 strcat(str, "33MHz");
15196 }
Joe Perches63c3a662011-04-26 08:12:10 +000015197 if (tg3_flag(tp, PCI_32BIT))
Michael Chanf9804dd2005-09-27 12:13:10 -070015198 strcat(str, ":32-bit");
15199 else
15200 strcat(str, ":64-bit");
15201 return str;
15202}
15203
Michael Chan8c2dc7e2005-12-19 16:26:02 -080015204static struct pci_dev * __devinit tg3_find_peer(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015205{
15206 struct pci_dev *peer;
15207 unsigned int func, devnr = tp->pdev->devfn & ~7;
15208
15209 for (func = 0; func < 8; func++) {
15210 peer = pci_get_slot(tp->pdev->bus, devnr | func);
15211 if (peer && peer != tp->pdev)
15212 break;
15213 pci_dev_put(peer);
15214 }
Michael Chan16fe9d72005-12-13 21:09:54 -080015215 /* 5704 can be configured in single-port mode, set peer to
15216 * tp->pdev in that case.
15217 */
15218 if (!peer) {
15219 peer = tp->pdev;
15220 return peer;
15221 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015222
15223 /*
15224 * We don't need to keep the refcount elevated; there's no way
15225 * to remove one half of this device without removing the other
15226 */
15227 pci_dev_put(peer);
15228
15229 return peer;
15230}
15231
David S. Miller15f98502005-05-18 22:49:26 -070015232static void __devinit tg3_init_coal(struct tg3 *tp)
15233{
15234 struct ethtool_coalesce *ec = &tp->coal;
15235
15236 memset(ec, 0, sizeof(*ec));
15237 ec->cmd = ETHTOOL_GCOALESCE;
15238 ec->rx_coalesce_usecs = LOW_RXCOL_TICKS;
15239 ec->tx_coalesce_usecs = LOW_TXCOL_TICKS;
15240 ec->rx_max_coalesced_frames = LOW_RXMAX_FRAMES;
15241 ec->tx_max_coalesced_frames = LOW_TXMAX_FRAMES;
15242 ec->rx_coalesce_usecs_irq = DEFAULT_RXCOAL_TICK_INT;
15243 ec->tx_coalesce_usecs_irq = DEFAULT_TXCOAL_TICK_INT;
15244 ec->rx_max_coalesced_frames_irq = DEFAULT_RXCOAL_MAXF_INT;
15245 ec->tx_max_coalesced_frames_irq = DEFAULT_TXCOAL_MAXF_INT;
15246 ec->stats_block_coalesce_usecs = DEFAULT_STAT_COAL_TICKS;
15247
15248 if (tp->coalesce_mode & (HOSTCC_MODE_CLRTICK_RXBD |
15249 HOSTCC_MODE_CLRTICK_TXBD)) {
15250 ec->rx_coalesce_usecs = LOW_RXCOL_TICKS_CLRTCKS;
15251 ec->rx_coalesce_usecs_irq = DEFAULT_RXCOAL_TICK_INT_CLRTCKS;
15252 ec->tx_coalesce_usecs = LOW_TXCOL_TICKS_CLRTCKS;
15253 ec->tx_coalesce_usecs_irq = DEFAULT_TXCOAL_TICK_INT_CLRTCKS;
15254 }
Michael Chand244c892005-07-05 14:42:33 -070015255
Joe Perches63c3a662011-04-26 08:12:10 +000015256 if (tg3_flag(tp, 5705_PLUS)) {
Michael Chand244c892005-07-05 14:42:33 -070015257 ec->rx_coalesce_usecs_irq = 0;
15258 ec->tx_coalesce_usecs_irq = 0;
15259 ec->stats_block_coalesce_usecs = 0;
15260 }
David S. Miller15f98502005-05-18 22:49:26 -070015261}
15262
Stephen Hemminger7c7d64b2008-11-19 22:25:36 -080015263static const struct net_device_ops tg3_netdev_ops = {
15264 .ndo_open = tg3_open,
15265 .ndo_stop = tg3_close,
Stephen Hemminger00829822008-11-20 20:14:53 -080015266 .ndo_start_xmit = tg3_start_xmit,
Eric Dumazet511d2222010-07-07 20:44:24 +000015267 .ndo_get_stats64 = tg3_get_stats64,
Stephen Hemminger00829822008-11-20 20:14:53 -080015268 .ndo_validate_addr = eth_validate_addr,
Jiri Pirkoafc4b132011-08-16 06:29:01 +000015269 .ndo_set_rx_mode = tg3_set_rx_mode,
Stephen Hemminger00829822008-11-20 20:14:53 -080015270 .ndo_set_mac_address = tg3_set_mac_addr,
15271 .ndo_do_ioctl = tg3_ioctl,
15272 .ndo_tx_timeout = tg3_tx_timeout,
15273 .ndo_change_mtu = tg3_change_mtu,
Michał Mirosławdc668912011-04-07 03:35:07 +000015274 .ndo_fix_features = tg3_fix_features,
Mahesh Bandewar06c03c02011-05-08 06:51:48 +000015275 .ndo_set_features = tg3_set_features,
Stephen Hemminger00829822008-11-20 20:14:53 -080015276#ifdef CONFIG_NET_POLL_CONTROLLER
15277 .ndo_poll_controller = tg3_poll_controller,
15278#endif
15279};
15280
Linus Torvalds1da177e2005-04-16 15:20:36 -070015281static int __devinit tg3_init_one(struct pci_dev *pdev,
15282 const struct pci_device_id *ent)
15283{
Linus Torvalds1da177e2005-04-16 15:20:36 -070015284 struct net_device *dev;
15285 struct tg3 *tp;
Matt Carlson646c9ed2009-09-01 12:58:41 +000015286 int i, err, pm_cap;
15287 u32 sndmbx, rcvmbx, intmbx;
Michael Chanf9804dd2005-09-27 12:13:10 -070015288 char str[40];
Michael Chan72f2afb2006-03-06 19:28:35 -080015289 u64 dma_mask, persist_dma_mask;
Michał Mirosławc8f44af2011-11-15 15:29:55 +000015290 netdev_features_t features = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015291
Joe Perches05dbe002010-02-17 19:44:19 +000015292 printk_once(KERN_INFO "%s\n", version);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015293
15294 err = pci_enable_device(pdev);
15295 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000015296 dev_err(&pdev->dev, "Cannot enable PCI device, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070015297 return err;
15298 }
15299
Linus Torvalds1da177e2005-04-16 15:20:36 -070015300 err = pci_request_regions(pdev, DRV_MODULE_NAME);
15301 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000015302 dev_err(&pdev->dev, "Cannot obtain PCI resources, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070015303 goto err_out_disable_pdev;
15304 }
15305
15306 pci_set_master(pdev);
15307
15308 /* Find power-management capability. */
15309 pm_cap = pci_find_capability(pdev, PCI_CAP_ID_PM);
15310 if (pm_cap == 0) {
Matt Carlson2445e462010-04-05 10:19:21 +000015311 dev_err(&pdev->dev,
15312 "Cannot find Power Management capability, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070015313 err = -EIO;
15314 goto err_out_free_res;
15315 }
15316
Matt Carlson16821282011-07-13 09:27:28 +000015317 err = pci_set_power_state(pdev, PCI_D0);
15318 if (err) {
15319 dev_err(&pdev->dev, "Transition to D0 failed, aborting\n");
15320 goto err_out_free_res;
15321 }
15322
Matt Carlsonfe5f5782009-09-01 13:09:39 +000015323 dev = alloc_etherdev_mq(sizeof(*tp), TG3_IRQ_MAX_VECS);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015324 if (!dev) {
Matt Carlson2445e462010-04-05 10:19:21 +000015325 dev_err(&pdev->dev, "Etherdev alloc failed, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070015326 err = -ENOMEM;
Matt Carlson16821282011-07-13 09:27:28 +000015327 goto err_out_power_down;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015328 }
15329
Linus Torvalds1da177e2005-04-16 15:20:36 -070015330 SET_NETDEV_DEV(dev, &pdev->dev);
15331
Linus Torvalds1da177e2005-04-16 15:20:36 -070015332 tp = netdev_priv(dev);
15333 tp->pdev = pdev;
15334 tp->dev = dev;
15335 tp->pm_cap = pm_cap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015336 tp->rx_mode = TG3_DEF_RX_MODE;
15337 tp->tx_mode = TG3_DEF_TX_MODE;
Matt Carlson8ef21422008-05-02 16:47:53 -070015338
Linus Torvalds1da177e2005-04-16 15:20:36 -070015339 if (tg3_debug > 0)
15340 tp->msg_enable = tg3_debug;
15341 else
15342 tp->msg_enable = TG3_DEF_MSG_ENABLE;
15343
15344 /* The word/byte swap controls here control register access byte
15345 * swapping. DMA data byte swapping is controlled in the GRC_MODE
15346 * setting below.
15347 */
15348 tp->misc_host_ctrl =
15349 MISC_HOST_CTRL_MASK_PCI_INT |
15350 MISC_HOST_CTRL_WORD_SWAP |
15351 MISC_HOST_CTRL_INDIR_ACCESS |
15352 MISC_HOST_CTRL_PCISTATE_RW;
15353
15354 /* The NONFRM (non-frame) byte/word swap controls take effect
15355 * on descriptor entries, anything which isn't packet data.
15356 *
15357 * The StrongARM chips on the board (one for tx, one for rx)
15358 * are running in big-endian mode.
15359 */
15360 tp->grc_mode = (GRC_MODE_WSWAP_DATA | GRC_MODE_BSWAP_DATA |
15361 GRC_MODE_WSWAP_NONFRM_DATA);
15362#ifdef __BIG_ENDIAN
15363 tp->grc_mode |= GRC_MODE_BSWAP_NONFRM_DATA;
15364#endif
15365 spin_lock_init(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015366 spin_lock_init(&tp->indirect_lock);
David Howellsc4028952006-11-22 14:57:56 +000015367 INIT_WORK(&tp->reset_task, tg3_reset_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015368
Matt Carlsond5fe4882008-11-21 17:20:32 -080015369 tp->regs = pci_ioremap_bar(pdev, BAR_0);
Andy Gospodarekab0049b2007-09-06 20:42:14 +010015370 if (!tp->regs) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015371 dev_err(&pdev->dev, "Cannot map device registers, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070015372 err = -ENOMEM;
15373 goto err_out_free_dev;
15374 }
15375
Matt Carlsonc9cab242011-07-13 09:27:27 +000015376 if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761 ||
15377 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761E ||
15378 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761S ||
15379 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761SE ||
15380 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717 ||
15381 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718 ||
15382 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5719 ||
15383 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5720) {
15384 tg3_flag_set(tp, ENABLE_APE);
15385 tp->aperegs = pci_ioremap_bar(pdev, BAR_2);
15386 if (!tp->aperegs) {
15387 dev_err(&pdev->dev,
15388 "Cannot map APE registers, aborting\n");
15389 err = -ENOMEM;
15390 goto err_out_iounmap;
15391 }
15392 }
15393
Linus Torvalds1da177e2005-04-16 15:20:36 -070015394 tp->rx_pending = TG3_DEF_RX_RING_PENDING;
15395 tp->rx_jumbo_pending = TG3_DEF_RX_JUMBO_RING_PENDING;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015396
Linus Torvalds1da177e2005-04-16 15:20:36 -070015397 dev->ethtool_ops = &tg3_ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015398 dev->watchdog_timeo = TG3_TX_TIMEOUT;
Matt Carlson2ffcc982011-05-19 12:12:44 +000015399 dev->netdev_ops = &tg3_netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015400 dev->irq = pdev->irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015401
15402 err = tg3_get_invariants(tp);
15403 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015404 dev_err(&pdev->dev,
15405 "Problem fetching invariants of chip, aborting\n");
Matt Carlsonc9cab242011-07-13 09:27:27 +000015406 goto err_out_apeunmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015407 }
15408
Michael Chan4a29cc22006-03-19 13:21:12 -080015409 /* The EPB bridge inside 5714, 5715, and 5780 and any
15410 * device behind the EPB cannot support DMA addresses > 40-bit.
Michael Chan72f2afb2006-03-06 19:28:35 -080015411 * On 64-bit systems with IOMMU, use 40-bit dma_mask.
15412 * On 64-bit systems without IOMMU, use 64-bit dma_mask and
15413 * do DMA address check in tg3_start_xmit().
15414 */
Joe Perches63c3a662011-04-26 08:12:10 +000015415 if (tg3_flag(tp, IS_5788))
Yang Hongyang284901a2009-04-06 19:01:15 -070015416 persist_dma_mask = dma_mask = DMA_BIT_MASK(32);
Joe Perches63c3a662011-04-26 08:12:10 +000015417 else if (tg3_flag(tp, 40BIT_DMA_BUG)) {
Yang Hongyang50cf1562009-04-06 19:01:14 -070015418 persist_dma_mask = dma_mask = DMA_BIT_MASK(40);
Michael Chan72f2afb2006-03-06 19:28:35 -080015419#ifdef CONFIG_HIGHMEM
Yang Hongyang6a355282009-04-06 19:01:13 -070015420 dma_mask = DMA_BIT_MASK(64);
Michael Chan72f2afb2006-03-06 19:28:35 -080015421#endif
Michael Chan4a29cc22006-03-19 13:21:12 -080015422 } else
Yang Hongyang6a355282009-04-06 19:01:13 -070015423 persist_dma_mask = dma_mask = DMA_BIT_MASK(64);
Michael Chan72f2afb2006-03-06 19:28:35 -080015424
15425 /* Configure DMA attributes. */
Yang Hongyang284901a2009-04-06 19:01:15 -070015426 if (dma_mask > DMA_BIT_MASK(32)) {
Michael Chan72f2afb2006-03-06 19:28:35 -080015427 err = pci_set_dma_mask(pdev, dma_mask);
15428 if (!err) {
Matt Carlson0da06062011-05-19 12:12:53 +000015429 features |= NETIF_F_HIGHDMA;
Michael Chan72f2afb2006-03-06 19:28:35 -080015430 err = pci_set_consistent_dma_mask(pdev,
15431 persist_dma_mask);
15432 if (err < 0) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015433 dev_err(&pdev->dev, "Unable to obtain 64 bit "
15434 "DMA for consistent allocations\n");
Matt Carlsonc9cab242011-07-13 09:27:27 +000015435 goto err_out_apeunmap;
Michael Chan72f2afb2006-03-06 19:28:35 -080015436 }
15437 }
15438 }
Yang Hongyang284901a2009-04-06 19:01:15 -070015439 if (err || dma_mask == DMA_BIT_MASK(32)) {
15440 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
Michael Chan72f2afb2006-03-06 19:28:35 -080015441 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015442 dev_err(&pdev->dev,
15443 "No usable DMA configuration, aborting\n");
Matt Carlsonc9cab242011-07-13 09:27:27 +000015444 goto err_out_apeunmap;
Michael Chan72f2afb2006-03-06 19:28:35 -080015445 }
15446 }
15447
Michael Chanfdfec172005-07-25 12:31:48 -070015448 tg3_init_bufmgr_config(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015449
Matt Carlson0da06062011-05-19 12:12:53 +000015450 features |= NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
15451
15452 /* 5700 B0 chips do not support checksumming correctly due
15453 * to hardware bugs.
15454 */
15455 if (tp->pci_chip_rev_id != CHIPREV_ID_5700_B0) {
15456 features |= NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_RXCSUM;
15457
15458 if (tg3_flag(tp, 5755_PLUS))
15459 features |= NETIF_F_IPV6_CSUM;
15460 }
15461
Michael Chan4e3a7aa2006-03-20 17:47:44 -080015462 /* TSO is on by default on chips that support hardware TSO.
15463 * Firmware TSO on older chips gives lower performance, so it
15464 * is off by default, but can be enabled using ethtool.
15465 */
Joe Perches63c3a662011-04-26 08:12:10 +000015466 if ((tg3_flag(tp, HW_TSO_1) ||
15467 tg3_flag(tp, HW_TSO_2) ||
15468 tg3_flag(tp, HW_TSO_3)) &&
Matt Carlson0da06062011-05-19 12:12:53 +000015469 (features & NETIF_F_IP_CSUM))
15470 features |= NETIF_F_TSO;
Joe Perches63c3a662011-04-26 08:12:10 +000015471 if (tg3_flag(tp, HW_TSO_2) || tg3_flag(tp, HW_TSO_3)) {
Matt Carlson0da06062011-05-19 12:12:53 +000015472 if (features & NETIF_F_IPV6_CSUM)
15473 features |= NETIF_F_TSO6;
Joe Perches63c3a662011-04-26 08:12:10 +000015474 if (tg3_flag(tp, HW_TSO_3) ||
Matt Carlsone849cdc2009-11-13 13:03:38 +000015475 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson57e69832008-05-25 23:48:31 -070015476 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
15477 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX) ||
Joe Perches63c3a662011-04-26 08:12:10 +000015478 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Michał Mirosławdc668912011-04-07 03:35:07 +000015479 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
Matt Carlson0da06062011-05-19 12:12:53 +000015480 features |= NETIF_F_TSO_ECN;
Michael Chanb0026622006-07-03 19:42:14 -070015481 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015482
Matt Carlsond542fe22011-05-19 16:02:43 +000015483 dev->features |= features;
15484 dev->vlan_features |= features;
15485
Mahesh Bandewar06c03c02011-05-08 06:51:48 +000015486 /*
15487 * Add loopback capability only for a subset of devices that support
15488 * MAC-LOOPBACK. Eventually this need to be enhanced to allow INT-PHY
15489 * loopback for the remaining devices.
15490 */
15491 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5780 &&
15492 !tg3_flag(tp, CPMU_PRESENT))
15493 /* Add the loopback capability */
Matt Carlson0da06062011-05-19 12:12:53 +000015494 features |= NETIF_F_LOOPBACK;
15495
Matt Carlson0da06062011-05-19 12:12:53 +000015496 dev->hw_features |= features;
Mahesh Bandewar06c03c02011-05-08 06:51:48 +000015497
Linus Torvalds1da177e2005-04-16 15:20:36 -070015498 if (tp->pci_chip_rev_id == CHIPREV_ID_5705_A1 &&
Joe Perches63c3a662011-04-26 08:12:10 +000015499 !tg3_flag(tp, TSO_CAPABLE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070015500 !(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH)) {
Joe Perches63c3a662011-04-26 08:12:10 +000015501 tg3_flag_set(tp, MAX_RXPEND_64);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015502 tp->rx_pending = 63;
15503 }
15504
Linus Torvalds1da177e2005-04-16 15:20:36 -070015505 err = tg3_get_device_address(tp);
15506 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015507 dev_err(&pdev->dev,
15508 "Could not obtain valid ethernet address, aborting\n");
Matt Carlsonc9cab242011-07-13 09:27:27 +000015509 goto err_out_apeunmap;
Matt Carlson0d3031d2007-10-10 18:02:43 -070015510 }
15511
Matt Carlsonc88864d2007-11-12 21:07:01 -080015512 /*
15513 * Reset chip in case UNDI or EFI driver did not shutdown
15514 * DMA self test will enable WDMAC and we'll see (spurious)
15515 * pending DMA on the PCI bus at that point.
15516 */
15517 if ((tr32(HOSTCC_MODE) & HOSTCC_MODE_ENABLE) ||
15518 (tr32(WDMAC_MODE) & WDMAC_MODE_ENABLE)) {
15519 tw32(MEMARB_MODE, MEMARB_MODE_ENABLE);
15520 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
15521 }
15522
15523 err = tg3_test_dma(tp);
15524 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015525 dev_err(&pdev->dev, "DMA engine test failed, aborting\n");
Matt Carlsonc88864d2007-11-12 21:07:01 -080015526 goto err_out_apeunmap;
15527 }
15528
Matt Carlson78f90dc2009-11-13 13:03:42 +000015529 intmbx = MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW;
15530 rcvmbx = MAILBOX_RCVRET_CON_IDX_0 + TG3_64BIT_REG_LOW;
15531 sndmbx = MAILBOX_SNDHOST_PROD_IDX_0 + TG3_64BIT_REG_LOW;
Matt Carlson6fd45cb2010-09-15 08:59:57 +000015532 for (i = 0; i < tp->irq_max; i++) {
Matt Carlson78f90dc2009-11-13 13:03:42 +000015533 struct tg3_napi *tnapi = &tp->napi[i];
15534
15535 tnapi->tp = tp;
15536 tnapi->tx_pending = TG3_DEF_TX_RING_PENDING;
15537
15538 tnapi->int_mbox = intmbx;
Matt Carlson93a700a2011-08-31 11:44:54 +000015539 if (i <= 4)
Matt Carlson78f90dc2009-11-13 13:03:42 +000015540 intmbx += 0x8;
15541 else
15542 intmbx += 0x4;
15543
15544 tnapi->consmbox = rcvmbx;
15545 tnapi->prodmbox = sndmbx;
15546
Matt Carlson66cfd1b2010-09-30 10:34:30 +000015547 if (i)
Matt Carlson78f90dc2009-11-13 13:03:42 +000015548 tnapi->coal_now = HOSTCC_MODE_COAL_VEC1_NOW << (i - 1);
Matt Carlson66cfd1b2010-09-30 10:34:30 +000015549 else
Matt Carlson78f90dc2009-11-13 13:03:42 +000015550 tnapi->coal_now = HOSTCC_MODE_NOW;
Matt Carlson78f90dc2009-11-13 13:03:42 +000015551
Joe Perches63c3a662011-04-26 08:12:10 +000015552 if (!tg3_flag(tp, SUPPORT_MSIX))
Matt Carlson78f90dc2009-11-13 13:03:42 +000015553 break;
15554
15555 /*
15556 * If we support MSIX, we'll be using RSS. If we're using
15557 * RSS, the first vector only handles link interrupts and the
15558 * remaining vectors handle rx and tx interrupts. Reuse the
15559 * mailbox values for the next iteration. The values we setup
15560 * above are still useful for the single vectored mode.
15561 */
15562 if (!i)
15563 continue;
15564
15565 rcvmbx += 0x8;
15566
15567 if (sndmbx & 0x4)
15568 sndmbx -= 0x4;
15569 else
15570 sndmbx += 0xc;
15571 }
15572
Matt Carlsonc88864d2007-11-12 21:07:01 -080015573 tg3_init_coal(tp);
15574
Michael Chanc49a1562006-12-17 17:07:29 -080015575 pci_set_drvdata(pdev, dev);
15576
Matt Carlsoncd0d7222011-07-13 09:27:33 +000015577 if (tg3_flag(tp, 5717_PLUS)) {
15578 /* Resume a low-power mode */
15579 tg3_frob_aux_power(tp, false);
15580 }
15581
Linus Torvalds1da177e2005-04-16 15:20:36 -070015582 err = register_netdev(dev);
15583 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015584 dev_err(&pdev->dev, "Cannot register net device, aborting\n");
Matt Carlson0d3031d2007-10-10 18:02:43 -070015585 goto err_out_apeunmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015586 }
15587
Joe Perches05dbe002010-02-17 19:44:19 +000015588 netdev_info(dev, "Tigon3 [partno(%s) rev %04x] (%s) MAC address %pM\n",
15589 tp->board_part_number,
15590 tp->pci_chip_rev_id,
15591 tg3_bus_string(tp, str),
15592 dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015593
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015594 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000015595 struct phy_device *phydev;
15596 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlson5129c3a2010-04-05 10:19:23 +000015597 netdev_info(dev,
15598 "attached PHY driver [%s] (mii_bus:phy_addr=%s)\n",
Joe Perches05dbe002010-02-17 19:44:19 +000015599 phydev->drv->name, dev_name(&phydev->dev));
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015600 } else {
15601 char *ethtype;
15602
15603 if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
15604 ethtype = "10/100Base-TX";
15605 else if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
15606 ethtype = "1000Base-SX";
15607 else
15608 ethtype = "10/100/1000Base-T";
15609
Matt Carlson5129c3a2010-04-05 10:19:23 +000015610 netdev_info(dev, "attached PHY is %s (%s Ethernet) "
Matt Carlson47007832011-04-20 07:57:43 +000015611 "(WireSpeed[%d], EEE[%d])\n",
15612 tg3_phy_string(tp), ethtype,
15613 (tp->phy_flags & TG3_PHYFLG_NO_ETH_WIRE_SPEED) == 0,
15614 (tp->phy_flags & TG3_PHYFLG_EEE_CAP) != 0);
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015615 }
Matt Carlsondf59c942008-11-03 16:52:56 -080015616
Joe Perches05dbe002010-02-17 19:44:19 +000015617 netdev_info(dev, "RXcsums[%d] LinkChgREG[%d] MIirq[%d] ASF[%d] TSOcap[%d]\n",
Michał Mirosławdc668912011-04-07 03:35:07 +000015618 (dev->features & NETIF_F_RXCSUM) != 0,
Joe Perches63c3a662011-04-26 08:12:10 +000015619 tg3_flag(tp, USE_LINKCHG_REG) != 0,
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015620 (tp->phy_flags & TG3_PHYFLG_USE_MI_INTERRUPT) != 0,
Joe Perches63c3a662011-04-26 08:12:10 +000015621 tg3_flag(tp, ENABLE_ASF) != 0,
15622 tg3_flag(tp, TSO_CAPABLE) != 0);
Joe Perches05dbe002010-02-17 19:44:19 +000015623 netdev_info(dev, "dma_rwctrl[%08x] dma_mask[%d-bit]\n",
15624 tp->dma_rwctrl,
15625 pdev->dma_mask == DMA_BIT_MASK(32) ? 32 :
15626 ((u64)pdev->dma_mask) == DMA_BIT_MASK(40) ? 40 : 64);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015627
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000015628 pci_save_state(pdev);
15629
Linus Torvalds1da177e2005-04-16 15:20:36 -070015630 return 0;
15631
Matt Carlson0d3031d2007-10-10 18:02:43 -070015632err_out_apeunmap:
15633 if (tp->aperegs) {
15634 iounmap(tp->aperegs);
15635 tp->aperegs = NULL;
15636 }
15637
Linus Torvalds1da177e2005-04-16 15:20:36 -070015638err_out_iounmap:
Michael Chan68929142005-08-09 20:17:14 -070015639 if (tp->regs) {
15640 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070015641 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070015642 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015643
15644err_out_free_dev:
15645 free_netdev(dev);
15646
Matt Carlson16821282011-07-13 09:27:28 +000015647err_out_power_down:
15648 pci_set_power_state(pdev, PCI_D3hot);
15649
Linus Torvalds1da177e2005-04-16 15:20:36 -070015650err_out_free_res:
15651 pci_release_regions(pdev);
15652
15653err_out_disable_pdev:
15654 pci_disable_device(pdev);
15655 pci_set_drvdata(pdev, NULL);
15656 return err;
15657}
15658
15659static void __devexit tg3_remove_one(struct pci_dev *pdev)
15660{
15661 struct net_device *dev = pci_get_drvdata(pdev);
15662
15663 if (dev) {
15664 struct tg3 *tp = netdev_priv(dev);
15665
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -080015666 if (tp->fw)
15667 release_firmware(tp->fw);
15668
Matt Carlsondb219972011-11-04 09:15:03 +000015669 tg3_reset_task_cancel(tp);
Matt Carlson158d7ab2008-05-29 01:37:54 -070015670
David S. Miller1805b2f2011-10-24 18:18:09 -040015671 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015672 tg3_phy_fini(tp);
Matt Carlson158d7ab2008-05-29 01:37:54 -070015673 tg3_mdio_fini(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015674 }
Matt Carlson158d7ab2008-05-29 01:37:54 -070015675
Linus Torvalds1da177e2005-04-16 15:20:36 -070015676 unregister_netdev(dev);
Matt Carlson0d3031d2007-10-10 18:02:43 -070015677 if (tp->aperegs) {
15678 iounmap(tp->aperegs);
15679 tp->aperegs = NULL;
15680 }
Michael Chan68929142005-08-09 20:17:14 -070015681 if (tp->regs) {
15682 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070015683 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070015684 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015685 free_netdev(dev);
15686 pci_release_regions(pdev);
15687 pci_disable_device(pdev);
15688 pci_set_drvdata(pdev, NULL);
15689 }
15690}
15691
Eric Dumazetaa6027c2011-01-01 05:22:46 +000015692#ifdef CONFIG_PM_SLEEP
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015693static int tg3_suspend(struct device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015694{
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015695 struct pci_dev *pdev = to_pci_dev(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015696 struct net_device *dev = pci_get_drvdata(pdev);
15697 struct tg3 *tp = netdev_priv(dev);
15698 int err;
15699
15700 if (!netif_running(dev))
15701 return 0;
15702
Matt Carlsondb219972011-11-04 09:15:03 +000015703 tg3_reset_task_cancel(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015704 tg3_phy_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015705 tg3_netif_stop(tp);
15706
15707 del_timer_sync(&tp->timer);
15708
David S. Millerf47c11e2005-06-24 20:18:35 -070015709 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015710 tg3_disable_ints(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -070015711 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015712
15713 netif_device_detach(dev);
15714
David S. Millerf47c11e2005-06-24 20:18:35 -070015715 tg3_full_lock(tp, 0);
Michael Chan944d9802005-05-29 14:57:48 -070015716 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Joe Perches63c3a662011-04-26 08:12:10 +000015717 tg3_flag_clear(tp, INIT_COMPLETE);
David S. Millerf47c11e2005-06-24 20:18:35 -070015718 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015719
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015720 err = tg3_power_down_prepare(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015721 if (err) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015722 int err2;
15723
David S. Millerf47c11e2005-06-24 20:18:35 -070015724 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015725
Joe Perches63c3a662011-04-26 08:12:10 +000015726 tg3_flag_set(tp, INIT_COMPLETE);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015727 err2 = tg3_restart_hw(tp, 1);
15728 if (err2)
Michael Chanb9ec6c12006-07-25 16:37:27 -070015729 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015730
15731 tp->timer.expires = jiffies + tp->timer_offset;
15732 add_timer(&tp->timer);
15733
15734 netif_device_attach(dev);
15735 tg3_netif_start(tp);
15736
Michael Chanb9ec6c12006-07-25 16:37:27 -070015737out:
David S. Millerf47c11e2005-06-24 20:18:35 -070015738 tg3_full_unlock(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015739
15740 if (!err2)
15741 tg3_phy_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015742 }
15743
15744 return err;
15745}
15746
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015747static int tg3_resume(struct device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015748{
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015749 struct pci_dev *pdev = to_pci_dev(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015750 struct net_device *dev = pci_get_drvdata(pdev);
15751 struct tg3 *tp = netdev_priv(dev);
15752 int err;
15753
15754 if (!netif_running(dev))
15755 return 0;
15756
Linus Torvalds1da177e2005-04-16 15:20:36 -070015757 netif_device_attach(dev);
15758
David S. Millerf47c11e2005-06-24 20:18:35 -070015759 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015760
Joe Perches63c3a662011-04-26 08:12:10 +000015761 tg3_flag_set(tp, INIT_COMPLETE);
Michael Chanb9ec6c12006-07-25 16:37:27 -070015762 err = tg3_restart_hw(tp, 1);
15763 if (err)
15764 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015765
15766 tp->timer.expires = jiffies + tp->timer_offset;
15767 add_timer(&tp->timer);
15768
Linus Torvalds1da177e2005-04-16 15:20:36 -070015769 tg3_netif_start(tp);
15770
Michael Chanb9ec6c12006-07-25 16:37:27 -070015771out:
David S. Millerf47c11e2005-06-24 20:18:35 -070015772 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015773
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015774 if (!err)
15775 tg3_phy_start(tp);
15776
Michael Chanb9ec6c12006-07-25 16:37:27 -070015777 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015778}
15779
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015780static SIMPLE_DEV_PM_OPS(tg3_pm_ops, tg3_suspend, tg3_resume);
Eric Dumazetaa6027c2011-01-01 05:22:46 +000015781#define TG3_PM_OPS (&tg3_pm_ops)
15782
15783#else
15784
15785#define TG3_PM_OPS NULL
15786
15787#endif /* CONFIG_PM_SLEEP */
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015788
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000015789/**
15790 * tg3_io_error_detected - called when PCI error is detected
15791 * @pdev: Pointer to PCI device
15792 * @state: The current pci connection state
15793 *
15794 * This function is called after a PCI bus error affecting
15795 * this device has been detected.
15796 */
15797static pci_ers_result_t tg3_io_error_detected(struct pci_dev *pdev,
15798 pci_channel_state_t state)
15799{
15800 struct net_device *netdev = pci_get_drvdata(pdev);
15801 struct tg3 *tp = netdev_priv(netdev);
15802 pci_ers_result_t err = PCI_ERS_RESULT_NEED_RESET;
15803
15804 netdev_info(netdev, "PCI I/O error detected\n");
15805
15806 rtnl_lock();
15807
15808 if (!netif_running(netdev))
15809 goto done;
15810
15811 tg3_phy_stop(tp);
15812
15813 tg3_netif_stop(tp);
15814
15815 del_timer_sync(&tp->timer);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000015816
15817 /* Want to make sure that the reset task doesn't run */
Matt Carlsondb219972011-11-04 09:15:03 +000015818 tg3_reset_task_cancel(tp);
Joe Perches63c3a662011-04-26 08:12:10 +000015819 tg3_flag_clear(tp, TX_RECOVERY_PENDING);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000015820
15821 netif_device_detach(netdev);
15822
15823 /* Clean up software state, even if MMIO is blocked */
15824 tg3_full_lock(tp, 0);
15825 tg3_halt(tp, RESET_KIND_SHUTDOWN, 0);
15826 tg3_full_unlock(tp);
15827
15828done:
15829 if (state == pci_channel_io_perm_failure)
15830 err = PCI_ERS_RESULT_DISCONNECT;
15831 else
15832 pci_disable_device(pdev);
15833
15834 rtnl_unlock();
15835
15836 return err;
15837}
15838
15839/**
15840 * tg3_io_slot_reset - called after the pci bus has been reset.
15841 * @pdev: Pointer to PCI device
15842 *
15843 * Restart the card from scratch, as if from a cold-boot.
15844 * At this point, the card has exprienced a hard reset,
15845 * followed by fixups by BIOS, and has its config space
15846 * set up identically to what it was at cold boot.
15847 */
15848static pci_ers_result_t tg3_io_slot_reset(struct pci_dev *pdev)
15849{
15850 struct net_device *netdev = pci_get_drvdata(pdev);
15851 struct tg3 *tp = netdev_priv(netdev);
15852 pci_ers_result_t rc = PCI_ERS_RESULT_DISCONNECT;
15853 int err;
15854
15855 rtnl_lock();
15856
15857 if (pci_enable_device(pdev)) {
15858 netdev_err(netdev, "Cannot re-enable PCI device after reset.\n");
15859 goto done;
15860 }
15861
15862 pci_set_master(pdev);
15863 pci_restore_state(pdev);
15864 pci_save_state(pdev);
15865
15866 if (!netif_running(netdev)) {
15867 rc = PCI_ERS_RESULT_RECOVERED;
15868 goto done;
15869 }
15870
15871 err = tg3_power_up(tp);
Matt Carlsonbed98292011-07-13 09:27:29 +000015872 if (err)
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000015873 goto done;
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000015874
15875 rc = PCI_ERS_RESULT_RECOVERED;
15876
15877done:
15878 rtnl_unlock();
15879
15880 return rc;
15881}
15882
15883/**
15884 * tg3_io_resume - called when traffic can start flowing again.
15885 * @pdev: Pointer to PCI device
15886 *
15887 * This callback is called when the error recovery driver tells
15888 * us that its OK to resume normal operation.
15889 */
15890static void tg3_io_resume(struct pci_dev *pdev)
15891{
15892 struct net_device *netdev = pci_get_drvdata(pdev);
15893 struct tg3 *tp = netdev_priv(netdev);
15894 int err;
15895
15896 rtnl_lock();
15897
15898 if (!netif_running(netdev))
15899 goto done;
15900
15901 tg3_full_lock(tp, 0);
Joe Perches63c3a662011-04-26 08:12:10 +000015902 tg3_flag_set(tp, INIT_COMPLETE);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000015903 err = tg3_restart_hw(tp, 1);
15904 tg3_full_unlock(tp);
15905 if (err) {
15906 netdev_err(netdev, "Cannot restart hardware after reset.\n");
15907 goto done;
15908 }
15909
15910 netif_device_attach(netdev);
15911
15912 tp->timer.expires = jiffies + tp->timer_offset;
15913 add_timer(&tp->timer);
15914
15915 tg3_netif_start(tp);
15916
15917 tg3_phy_start(tp);
15918
15919done:
15920 rtnl_unlock();
15921}
15922
15923static struct pci_error_handlers tg3_err_handler = {
15924 .error_detected = tg3_io_error_detected,
15925 .slot_reset = tg3_io_slot_reset,
15926 .resume = tg3_io_resume
15927};
15928
Linus Torvalds1da177e2005-04-16 15:20:36 -070015929static struct pci_driver tg3_driver = {
15930 .name = DRV_MODULE_NAME,
15931 .id_table = tg3_pci_tbl,
15932 .probe = tg3_init_one,
15933 .remove = __devexit_p(tg3_remove_one),
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000015934 .err_handler = &tg3_err_handler,
Eric Dumazetaa6027c2011-01-01 05:22:46 +000015935 .driver.pm = TG3_PM_OPS,
Linus Torvalds1da177e2005-04-16 15:20:36 -070015936};
15937
15938static int __init tg3_init(void)
15939{
Jeff Garzik29917622006-08-19 17:48:59 -040015940 return pci_register_driver(&tg3_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015941}
15942
15943static void __exit tg3_cleanup(void)
15944{
15945 pci_unregister_driver(&tg3_driver);
15946}
15947
15948module_init(tg3_init);
15949module_exit(tg3_cleanup);