blob: 32ffb541667455427f37cf3eff2ddd2b54f23cd8 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * tg3.c: Broadcom Tigon3 ethernet driver.
3 *
4 * Copyright (C) 2001, 2002, 2003, 2004 David S. Miller (davem@redhat.com)
5 * Copyright (C) 2001, 2002, 2003 Jeff Garzik (jgarzik@pobox.com)
6 * Copyright (C) 2004 Sun Microsystems Inc.
Matt Carlson9e056c02012-02-13 15:20:17 +00007 * Copyright (C) 2005-2012 Broadcom Corporation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 *
9 * Firmware is:
Michael Chan49cabf42005-06-06 15:15:17 -070010 * Derived from proprietary unpublished source code,
11 * Copyright (C) 2000-2003 Broadcom Corporation.
12 *
13 * Permission is hereby granted for the distribution of this firmware
14 * data in hexadecimal or equivalent format, provided this copyright
15 * notice is accompanying it.
Linus Torvalds1da177e2005-04-16 15:20:36 -070016 */
17
Linus Torvalds1da177e2005-04-16 15:20:36 -070018
19#include <linux/module.h>
20#include <linux/moduleparam.h>
Matt Carlson6867c842010-07-11 09:31:44 +000021#include <linux/stringify.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/kernel.h>
23#include <linux/types.h>
24#include <linux/compiler.h>
25#include <linux/slab.h>
26#include <linux/delay.h>
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020027#include <linux/in.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <linux/init.h>
Alexey Dobriyana6b7a402011-06-06 10:43:46 +000029#include <linux/interrupt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <linux/ioport.h>
31#include <linux/pci.h>
32#include <linux/netdevice.h>
33#include <linux/etherdevice.h>
34#include <linux/skbuff.h>
35#include <linux/ethtool.h>
Matt Carlson3110f5f52010-12-06 08:28:50 +000036#include <linux/mdio.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include <linux/mii.h>
Matt Carlson158d7ab2008-05-29 01:37:54 -070038#include <linux/phy.h>
Matt Carlsona9daf362008-05-25 23:49:44 -070039#include <linux/brcmphy.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include <linux/if_vlan.h>
41#include <linux/ip.h>
42#include <linux/tcp.h>
43#include <linux/workqueue.h>
Michael Chan61487482005-09-05 17:53:19 -070044#include <linux/prefetch.h>
Tobias Klauserf9a5f7d2005-10-29 15:09:26 +020045#include <linux/dma-mapping.h>
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -080046#include <linux/firmware.h>
Michael Chanaed93e02012-07-16 16:24:02 +000047#include <linux/hwmon.h>
48#include <linux/hwmon-sysfs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
50#include <net/checksum.h>
Arnaldo Carvalho de Meloc9bdd4b2007-03-12 20:09:15 -030051#include <net/ip.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
Javier Martinez Canillas27fd9de2011-03-26 16:42:31 +000053#include <linux/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070054#include <asm/byteorder.h>
Javier Martinez Canillas27fd9de2011-03-26 16:42:31 +000055#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
Matt Carlsonbe947302012-12-03 19:36:57 +000057#include <uapi/linux/net_tstamp.h>
58#include <linux/ptp_clock_kernel.h>
59
David S. Miller49b6e95f2007-03-29 01:38:42 -070060#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070061#include <asm/idprom.h>
David S. Miller49b6e95f2007-03-29 01:38:42 -070062#include <asm/prom.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070063#endif
64
Matt Carlson63532392008-11-03 16:49:57 -080065#define BAR_0 0
66#define BAR_2 2
67
Linus Torvalds1da177e2005-04-16 15:20:36 -070068#include "tg3.h"
69
Joe Perches63c3a662011-04-26 08:12:10 +000070/* Functions & macros to verify TG3_FLAGS types */
71
72static inline int _tg3_flag(enum TG3_FLAGS flag, unsigned long *bits)
73{
74 return test_bit(flag, bits);
75}
76
77static inline void _tg3_flag_set(enum TG3_FLAGS flag, unsigned long *bits)
78{
79 set_bit(flag, bits);
80}
81
82static inline void _tg3_flag_clear(enum TG3_FLAGS flag, unsigned long *bits)
83{
84 clear_bit(flag, bits);
85}
86
87#define tg3_flag(tp, flag) \
88 _tg3_flag(TG3_FLAG_##flag, (tp)->tg3_flags)
89#define tg3_flag_set(tp, flag) \
90 _tg3_flag_set(TG3_FLAG_##flag, (tp)->tg3_flags)
91#define tg3_flag_clear(tp, flag) \
92 _tg3_flag_clear(TG3_FLAG_##flag, (tp)->tg3_flags)
93
Linus Torvalds1da177e2005-04-16 15:20:36 -070094#define DRV_MODULE_NAME "tg3"
Matt Carlson6867c842010-07-11 09:31:44 +000095#define TG3_MAJ_NUM 3
Michael Chan0b3ba052012-11-14 14:44:29 +000096#define TG3_MIN_NUM 127
Matt Carlson6867c842010-07-11 09:31:44 +000097#define DRV_MODULE_VERSION \
98 __stringify(TG3_MAJ_NUM) "." __stringify(TG3_MIN_NUM)
Michael Chan0b3ba052012-11-14 14:44:29 +000099#define DRV_MODULE_RELDATE "November 14, 2012"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
Matt Carlsonfd6d3f02011-08-31 11:44:52 +0000101#define RESET_KIND_SHUTDOWN 0
102#define RESET_KIND_INIT 1
103#define RESET_KIND_SUSPEND 2
104
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105#define TG3_DEF_RX_MODE 0
106#define TG3_DEF_TX_MODE 0
107#define TG3_DEF_MSG_ENABLE \
108 (NETIF_MSG_DRV | \
109 NETIF_MSG_PROBE | \
110 NETIF_MSG_LINK | \
111 NETIF_MSG_TIMER | \
112 NETIF_MSG_IFDOWN | \
113 NETIF_MSG_IFUP | \
114 NETIF_MSG_RX_ERR | \
115 NETIF_MSG_TX_ERR)
116
Matt Carlson520b2752011-06-13 13:39:02 +0000117#define TG3_GRC_LCLCTL_PWRSW_DELAY 100
118
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119/* length of time before we decide the hardware is borked,
120 * and dev->tx_timeout() should be called to fix the problem
121 */
Joe Perches63c3a662011-04-26 08:12:10 +0000122
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123#define TG3_TX_TIMEOUT (5 * HZ)
124
125/* hardware minimum and maximum for a single frame's data payload */
126#define TG3_MIN_MTU 60
127#define TG3_MAX_MTU(tp) \
Joe Perches63c3a662011-04-26 08:12:10 +0000128 (tg3_flag(tp, JUMBO_CAPABLE) ? 9000 : 1500)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
130/* These numbers seem to be hard coded in the NIC firmware somehow.
131 * You can't change the ring sizes, but you can change where you place
132 * them in the NIC onboard memory.
133 */
Matt Carlson7cb32cf2010-09-30 10:34:36 +0000134#define TG3_RX_STD_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_STD_MAX_SIZE_5717 : TG3_RX_STD_MAX_SIZE_5700)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137#define TG3_DEF_RX_RING_PENDING 200
Matt Carlson7cb32cf2010-09-30 10:34:36 +0000138#define TG3_RX_JMB_RING_SIZE(tp) \
Joe Perches63c3a662011-04-26 08:12:10 +0000139 (tg3_flag(tp, LRG_PROD_RING_CAP) ? \
Matt Carlsonde9f5232011-04-05 14:22:43 +0000140 TG3_RX_JMB_MAX_SIZE_5717 : TG3_RX_JMB_MAX_SIZE_5700)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141#define TG3_DEF_RX_JUMBO_RING_PENDING 100
142
143/* Do not place this n-ring entries value into the tp struct itself,
144 * we really want to expose these constants to GCC so that modulo et
145 * al. operations are done with shifts and masks instead of with
146 * hw multiply/modulo instructions. Another solution would be to
147 * replace things like '% foo' with '& (foo - 1)'.
148 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149
150#define TG3_TX_RING_SIZE 512
151#define TG3_DEF_TX_RING_PENDING (TG3_TX_RING_SIZE - 1)
152
Matt Carlson2c49a442010-09-30 10:34:35 +0000153#define TG3_RX_STD_RING_BYTES(tp) \
154 (sizeof(struct tg3_rx_buffer_desc) * TG3_RX_STD_RING_SIZE(tp))
155#define TG3_RX_JMB_RING_BYTES(tp) \
156 (sizeof(struct tg3_ext_rx_buffer_desc) * TG3_RX_JMB_RING_SIZE(tp))
157#define TG3_RX_RCB_RING_BYTES(tp) \
Matt Carlson7cb32cf2010-09-30 10:34:36 +0000158 (sizeof(struct tg3_rx_buffer_desc) * (tp->rx_ret_ring_mask + 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159#define TG3_TX_RING_BYTES (sizeof(struct tg3_tx_buffer_desc) * \
160 TG3_TX_RING_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161#define NEXT_TX(N) (((N) + 1) & (TG3_TX_RING_SIZE - 1))
162
Matt Carlson287be122009-08-28 13:58:46 +0000163#define TG3_DMA_BYTE_ENAB 64
164
165#define TG3_RX_STD_DMA_SZ 1536
166#define TG3_RX_JMB_DMA_SZ 9046
167
168#define TG3_RX_DMA_TO_MAP_SZ(x) ((x) + TG3_DMA_BYTE_ENAB)
169
170#define TG3_RX_STD_MAP_SZ TG3_RX_DMA_TO_MAP_SZ(TG3_RX_STD_DMA_SZ)
171#define TG3_RX_JMB_MAP_SZ TG3_RX_DMA_TO_MAP_SZ(TG3_RX_JMB_DMA_SZ)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
Matt Carlson2c49a442010-09-30 10:34:35 +0000173#define TG3_RX_STD_BUFF_RING_SIZE(tp) \
174 (sizeof(struct ring_info) * TG3_RX_STD_RING_SIZE(tp))
Matt Carlson2b2cdb62009-11-13 13:03:48 +0000175
Matt Carlson2c49a442010-09-30 10:34:35 +0000176#define TG3_RX_JMB_BUFF_RING_SIZE(tp) \
177 (sizeof(struct ring_info) * TG3_RX_JMB_RING_SIZE(tp))
Matt Carlson2b2cdb62009-11-13 13:03:48 +0000178
Matt Carlsond2757fc2010-04-12 06:58:27 +0000179/* Due to a hardware bug, the 5701 can only DMA to memory addresses
180 * that are at least dword aligned when used in PCIX mode. The driver
181 * works around this bug by double copying the packet. This workaround
182 * is built into the normal double copy length check for efficiency.
183 *
184 * However, the double copy is only necessary on those architectures
185 * where unaligned memory accesses are inefficient. For those architectures
186 * where unaligned memory accesses incur little penalty, we can reintegrate
187 * the 5701 in the normal rx path. Doing so saves a device structure
188 * dereference by hardcoding the double copy threshold in place.
189 */
190#define TG3_RX_COPY_THRESHOLD 256
191#if NET_IP_ALIGN == 0 || defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)
192 #define TG3_RX_COPY_THRESH(tp) TG3_RX_COPY_THRESHOLD
193#else
194 #define TG3_RX_COPY_THRESH(tp) ((tp)->rx_copy_thresh)
195#endif
196
Matt Carlson81389f52011-08-31 11:44:49 +0000197#if (NET_IP_ALIGN != 0)
198#define TG3_RX_OFFSET(tp) ((tp)->rx_offset)
199#else
Eric Dumazet9205fd92011-11-18 06:47:01 +0000200#define TG3_RX_OFFSET(tp) (NET_SKB_PAD)
Matt Carlson81389f52011-08-31 11:44:49 +0000201#endif
202
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203/* minimum number of free TX descriptors required to wake up TX process */
Matt Carlsonf3f3f272009-08-28 14:03:21 +0000204#define TG3_TX_WAKEUP_THRESH(tnapi) ((tnapi)->tx_pending / 4)
Matt Carlson55086ad2011-12-14 11:09:59 +0000205#define TG3_TX_BD_DMA_MAX_2K 2048
Matt Carlsona4cb4282011-12-14 11:09:58 +0000206#define TG3_TX_BD_DMA_MAX_4K 4096
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207
Matt Carlsonad829262008-11-21 17:16:16 -0800208#define TG3_RAW_IP_ALIGN 2
209
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000210#define TG3_FW_UPDATE_TIMEOUT_SEC 5
Matt Carlson21f76382012-02-22 12:35:21 +0000211#define TG3_FW_UPDATE_FREQ_SEC (TG3_FW_UPDATE_TIMEOUT_SEC / 2)
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000212
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -0800213#define FIRMWARE_TG3 "tigon/tg3.bin"
214#define FIRMWARE_TG3TSO "tigon/tg3_tso.bin"
215#define FIRMWARE_TG3TSO5 "tigon/tg3_tso5.bin"
216
Bill Pemberton229b1ad2012-12-03 09:22:59 -0500217static char version[] =
Joe Perches05dbe002010-02-17 19:44:19 +0000218 DRV_MODULE_NAME ".c:v" DRV_MODULE_VERSION " (" DRV_MODULE_RELDATE ")";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219
220MODULE_AUTHOR("David S. Miller (davem@redhat.com) and Jeff Garzik (jgarzik@pobox.com)");
221MODULE_DESCRIPTION("Broadcom Tigon3 ethernet driver");
222MODULE_LICENSE("GPL");
223MODULE_VERSION(DRV_MODULE_VERSION);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -0800224MODULE_FIRMWARE(FIRMWARE_TG3);
225MODULE_FIRMWARE(FIRMWARE_TG3TSO);
226MODULE_FIRMWARE(FIRMWARE_TG3TSO5);
227
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228static int tg3_debug = -1; /* -1 == use TG3_DEF_MSG_ENABLE as value */
229module_param(tg3_debug, int, 0);
230MODULE_PARM_DESC(tg3_debug, "Tigon3 bitmapped debugging message enable value");
231
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +0000232#define TG3_DRV_DATA_FLAG_10_100_ONLY 0x0001
233#define TG3_DRV_DATA_FLAG_5705_10_100 0x0002
234
Alexey Dobriyana3aa1882010-01-07 11:58:11 +0000235static DEFINE_PCI_DEVICE_TABLE(tg3_pci_tbl) = {
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700236 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5700)},
237 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5701)},
238 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5702)},
239 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5703)},
240 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5704)},
241 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5702FE)},
242 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705)},
243 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705_2)},
244 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705M)},
245 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705M_2)},
246 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5702X)},
247 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5703X)},
248 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5704S)},
249 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5702A3)},
250 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5703A3)},
251 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5782)},
252 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5788)},
253 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5789)},
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +0000254 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5901),
255 .driver_data = TG3_DRV_DATA_FLAG_10_100_ONLY |
256 TG3_DRV_DATA_FLAG_5705_10_100},
257 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5901_2),
258 .driver_data = TG3_DRV_DATA_FLAG_10_100_ONLY |
259 TG3_DRV_DATA_FLAG_5705_10_100},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700260 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5704S_2)},
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +0000261 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705F),
262 .driver_data = TG3_DRV_DATA_FLAG_10_100_ONLY |
263 TG3_DRV_DATA_FLAG_5705_10_100},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700264 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5721)},
Michael Chan126a3362006-09-27 16:03:07 -0700265 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5722)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700266 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5751)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700267 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5751M)},
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +0000268 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5751F),
269 .driver_data = TG3_DRV_DATA_FLAG_10_100_ONLY},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700270 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5752)},
271 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5752M)},
272 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5753)},
273 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5753M)},
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +0000274 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5753F),
275 .driver_data = TG3_DRV_DATA_FLAG_10_100_ONLY},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700276 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5754)},
277 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5754M)},
278 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5755)},
279 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5755M)},
Michael Chan126a3362006-09-27 16:03:07 -0700280 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5756)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700281 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5786)},
282 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5787)},
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +0000283 {PCI_DEVICE_SUB(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5787M,
284 PCI_VENDOR_ID_LENOVO,
285 TG3PCI_SUBDEVICE_ID_LENOVO_5787M),
286 .driver_data = TG3_DRV_DATA_FLAG_10_100_ONLY},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700287 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5787M)},
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +0000288 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5787F),
289 .driver_data = TG3_DRV_DATA_FLAG_10_100_ONLY},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700290 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5714)},
291 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5714S)},
292 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5715)},
293 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5715S)},
294 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5780)},
295 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5780S)},
296 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5781)},
Michael Chanb5d37722006-09-27 16:06:21 -0700297 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5906)},
298 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5906M)},
Matt Carlsond30cdd22007-10-07 23:28:35 -0700299 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5784)},
300 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5764)},
Matt Carlson6c7af272007-10-21 16:12:02 -0700301 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5723)},
Matt Carlson9936bcf2007-10-10 18:03:07 -0700302 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5761)},
303 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5761E)},
Matt Carlsonc88e6682008-11-03 16:49:18 -0800304 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5761S)},
305 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5761SE)},
Matt Carlson2befdce2009-08-28 12:28:45 +0000306 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5785_G)},
307 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5785_F)},
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +0000308 {PCI_DEVICE_SUB(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57780,
309 PCI_VENDOR_ID_AI, TG3PCI_SUBDEVICE_ID_ACER_57780_A),
310 .driver_data = TG3_DRV_DATA_FLAG_10_100_ONLY},
311 {PCI_DEVICE_SUB(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57780,
312 PCI_VENDOR_ID_AI, TG3PCI_SUBDEVICE_ID_ACER_57780_B),
313 .driver_data = TG3_DRV_DATA_FLAG_10_100_ONLY},
Matt Carlson321d32a2008-11-21 17:22:19 -0800314 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57780)},
315 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57760)},
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +0000316 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57790),
317 .driver_data = TG3_DRV_DATA_FLAG_10_100_ONLY},
Matt Carlson5e7ccf22009-08-25 10:08:42 +0000318 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57788)},
Matt Carlson5001e2f2009-11-13 13:03:51 +0000319 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5717)},
Michael Chan79d49692012-11-05 14:26:29 +0000320 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5717_C)},
Matt Carlson5001e2f2009-11-13 13:03:51 +0000321 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5718)},
Matt Carlsonb0f75222010-01-20 16:58:11 +0000322 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57781)},
323 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57785)},
324 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57761)},
325 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57765)},
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +0000326 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57791),
327 .driver_data = TG3_DRV_DATA_FLAG_10_100_ONLY},
328 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57795),
329 .driver_data = TG3_DRV_DATA_FLAG_10_100_ONLY},
Matt Carlson302b5002010-06-05 17:24:38 +0000330 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5719)},
Matt Carlsonba1f3c72011-04-05 14:22:50 +0000331 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5720)},
Greg KH02eca3f2012-07-12 15:39:44 +0000332 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57762)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700333 {PCI_DEVICE(PCI_VENDOR_ID_SYSKONNECT, PCI_DEVICE_ID_SYSKONNECT_9DXX)},
334 {PCI_DEVICE(PCI_VENDOR_ID_SYSKONNECT, PCI_DEVICE_ID_SYSKONNECT_9MXX)},
335 {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC1000)},
336 {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC1001)},
337 {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC1003)},
338 {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC9100)},
339 {PCI_DEVICE(PCI_VENDOR_ID_APPLE, PCI_DEVICE_ID_APPLE_TIGON3)},
Meelis Roos1dcb14d2011-05-25 05:43:47 +0000340 {PCI_DEVICE(0x10cf, 0x11a2)}, /* Fujitsu 1000base-SX with BCM5703SKHB */
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700341 {}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342};
343
344MODULE_DEVICE_TABLE(pci, tg3_pci_tbl);
345
Andreas Mohr50da8592006-08-14 23:54:30 -0700346static const struct {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 const char string[ETH_GSTRING_LEN];
Matt Carlson48fa55a2011-04-13 11:05:06 +0000348} ethtool_stats_keys[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 { "rx_octets" },
350 { "rx_fragments" },
351 { "rx_ucast_packets" },
352 { "rx_mcast_packets" },
353 { "rx_bcast_packets" },
354 { "rx_fcs_errors" },
355 { "rx_align_errors" },
356 { "rx_xon_pause_rcvd" },
357 { "rx_xoff_pause_rcvd" },
358 { "rx_mac_ctrl_rcvd" },
359 { "rx_xoff_entered" },
360 { "rx_frame_too_long_errors" },
361 { "rx_jabbers" },
362 { "rx_undersize_packets" },
363 { "rx_in_length_errors" },
364 { "rx_out_length_errors" },
365 { "rx_64_or_less_octet_packets" },
366 { "rx_65_to_127_octet_packets" },
367 { "rx_128_to_255_octet_packets" },
368 { "rx_256_to_511_octet_packets" },
369 { "rx_512_to_1023_octet_packets" },
370 { "rx_1024_to_1522_octet_packets" },
371 { "rx_1523_to_2047_octet_packets" },
372 { "rx_2048_to_4095_octet_packets" },
373 { "rx_4096_to_8191_octet_packets" },
374 { "rx_8192_to_9022_octet_packets" },
375
376 { "tx_octets" },
377 { "tx_collisions" },
378
379 { "tx_xon_sent" },
380 { "tx_xoff_sent" },
381 { "tx_flow_control" },
382 { "tx_mac_errors" },
383 { "tx_single_collisions" },
384 { "tx_mult_collisions" },
385 { "tx_deferred" },
386 { "tx_excessive_collisions" },
387 { "tx_late_collisions" },
388 { "tx_collide_2times" },
389 { "tx_collide_3times" },
390 { "tx_collide_4times" },
391 { "tx_collide_5times" },
392 { "tx_collide_6times" },
393 { "tx_collide_7times" },
394 { "tx_collide_8times" },
395 { "tx_collide_9times" },
396 { "tx_collide_10times" },
397 { "tx_collide_11times" },
398 { "tx_collide_12times" },
399 { "tx_collide_13times" },
400 { "tx_collide_14times" },
401 { "tx_collide_15times" },
402 { "tx_ucast_packets" },
403 { "tx_mcast_packets" },
404 { "tx_bcast_packets" },
405 { "tx_carrier_sense_errors" },
406 { "tx_discards" },
407 { "tx_errors" },
408
409 { "dma_writeq_full" },
410 { "dma_write_prioq_full" },
411 { "rxbds_empty" },
412 { "rx_discards" },
413 { "rx_errors" },
414 { "rx_threshold_hit" },
415
416 { "dma_readq_full" },
417 { "dma_read_prioq_full" },
418 { "tx_comp_queue_full" },
419
420 { "ring_set_send_prod_index" },
421 { "ring_status_update" },
422 { "nic_irqs" },
423 { "nic_avoided_irqs" },
Matt Carlson4452d092011-05-19 12:12:51 +0000424 { "nic_tx_threshold_hit" },
425
426 { "mbuf_lwm_thresh_hit" },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427};
428
Matt Carlson48fa55a2011-04-13 11:05:06 +0000429#define TG3_NUM_STATS ARRAY_SIZE(ethtool_stats_keys)
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +0000430#define TG3_NVRAM_TEST 0
431#define TG3_LINK_TEST 1
432#define TG3_REGISTER_TEST 2
433#define TG3_MEMORY_TEST 3
434#define TG3_MAC_LOOPB_TEST 4
435#define TG3_PHY_LOOPB_TEST 5
436#define TG3_EXT_LOOPB_TEST 6
437#define TG3_INTERRUPT_TEST 7
Matt Carlson48fa55a2011-04-13 11:05:06 +0000438
439
Andreas Mohr50da8592006-08-14 23:54:30 -0700440static const struct {
Michael Chan4cafd3f2005-05-29 14:56:34 -0700441 const char string[ETH_GSTRING_LEN];
Matt Carlson48fa55a2011-04-13 11:05:06 +0000442} ethtool_test_keys[] = {
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +0000443 [TG3_NVRAM_TEST] = { "nvram test (online) " },
444 [TG3_LINK_TEST] = { "link test (online) " },
445 [TG3_REGISTER_TEST] = { "register test (offline)" },
446 [TG3_MEMORY_TEST] = { "memory test (offline)" },
447 [TG3_MAC_LOOPB_TEST] = { "mac loopback test (offline)" },
448 [TG3_PHY_LOOPB_TEST] = { "phy loopback test (offline)" },
449 [TG3_EXT_LOOPB_TEST] = { "ext loopback test (offline)" },
450 [TG3_INTERRUPT_TEST] = { "interrupt test (offline)" },
Michael Chan4cafd3f2005-05-29 14:56:34 -0700451};
452
Matt Carlson48fa55a2011-04-13 11:05:06 +0000453#define TG3_NUM_TEST ARRAY_SIZE(ethtool_test_keys)
454
455
Michael Chanb401e9e2005-12-19 16:27:04 -0800456static void tg3_write32(struct tg3 *tp, u32 off, u32 val)
457{
458 writel(val, tp->regs + off);
459}
460
461static u32 tg3_read32(struct tg3 *tp, u32 off)
462{
Matt Carlsonde6f31e2010-04-12 06:58:30 +0000463 return readl(tp->regs + off);
Michael Chanb401e9e2005-12-19 16:27:04 -0800464}
465
Matt Carlson0d3031d2007-10-10 18:02:43 -0700466static void tg3_ape_write32(struct tg3 *tp, u32 off, u32 val)
467{
468 writel(val, tp->aperegs + off);
469}
470
471static u32 tg3_ape_read32(struct tg3 *tp, u32 off)
472{
Matt Carlsonde6f31e2010-04-12 06:58:30 +0000473 return readl(tp->aperegs + off);
Matt Carlson0d3031d2007-10-10 18:02:43 -0700474}
475
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476static void tg3_write_indirect_reg32(struct tg3 *tp, u32 off, u32 val)
477{
Michael Chan68929142005-08-09 20:17:14 -0700478 unsigned long flags;
479
480 spin_lock_irqsave(&tp->indirect_lock, flags);
Michael Chan1ee582d2005-08-09 20:16:46 -0700481 pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off);
482 pci_write_config_dword(tp->pdev, TG3PCI_REG_DATA, val);
Michael Chan68929142005-08-09 20:17:14 -0700483 spin_unlock_irqrestore(&tp->indirect_lock, flags);
Michael Chan1ee582d2005-08-09 20:16:46 -0700484}
485
486static void tg3_write_flush_reg32(struct tg3 *tp, u32 off, u32 val)
487{
488 writel(val, tp->regs + off);
489 readl(tp->regs + off);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490}
491
Michael Chan68929142005-08-09 20:17:14 -0700492static u32 tg3_read_indirect_reg32(struct tg3 *tp, u32 off)
493{
494 unsigned long flags;
495 u32 val;
496
497 spin_lock_irqsave(&tp->indirect_lock, flags);
498 pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off);
499 pci_read_config_dword(tp->pdev, TG3PCI_REG_DATA, &val);
500 spin_unlock_irqrestore(&tp->indirect_lock, flags);
501 return val;
502}
503
504static void tg3_write_indirect_mbox(struct tg3 *tp, u32 off, u32 val)
505{
506 unsigned long flags;
507
508 if (off == (MAILBOX_RCVRET_CON_IDX_0 + TG3_64BIT_REG_LOW)) {
509 pci_write_config_dword(tp->pdev, TG3PCI_RCV_RET_RING_CON_IDX +
510 TG3_64BIT_REG_LOW, val);
511 return;
512 }
Matt Carlson66711e62009-11-13 13:03:49 +0000513 if (off == TG3_RX_STD_PROD_IDX_REG) {
Michael Chan68929142005-08-09 20:17:14 -0700514 pci_write_config_dword(tp->pdev, TG3PCI_STD_RING_PROD_IDX +
515 TG3_64BIT_REG_LOW, val);
516 return;
517 }
518
519 spin_lock_irqsave(&tp->indirect_lock, flags);
520 pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off + 0x5600);
521 pci_write_config_dword(tp->pdev, TG3PCI_REG_DATA, val);
522 spin_unlock_irqrestore(&tp->indirect_lock, flags);
523
524 /* In indirect mode when disabling interrupts, we also need
525 * to clear the interrupt bit in the GRC local ctrl register.
526 */
527 if ((off == (MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW)) &&
528 (val == 0x1)) {
529 pci_write_config_dword(tp->pdev, TG3PCI_MISC_LOCAL_CTRL,
530 tp->grc_local_ctrl|GRC_LCLCTRL_CLEARINT);
531 }
532}
533
534static u32 tg3_read_indirect_mbox(struct tg3 *tp, u32 off)
535{
536 unsigned long flags;
537 u32 val;
538
539 spin_lock_irqsave(&tp->indirect_lock, flags);
540 pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off + 0x5600);
541 pci_read_config_dword(tp->pdev, TG3PCI_REG_DATA, &val);
542 spin_unlock_irqrestore(&tp->indirect_lock, flags);
543 return val;
544}
545
Michael Chanb401e9e2005-12-19 16:27:04 -0800546/* usec_wait specifies the wait time in usec when writing to certain registers
547 * where it is unsafe to read back the register without some delay.
548 * GRC_LOCAL_CTRL is one example if the GPIOs are toggled to switch power.
549 * TG3PCI_CLOCK_CTRL is another example if the clock frequencies are changed.
550 */
551static void _tw32_flush(struct tg3 *tp, u32 off, u32 val, u32 usec_wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552{
Joe Perches63c3a662011-04-26 08:12:10 +0000553 if (tg3_flag(tp, PCIX_TARGET_HWBUG) || tg3_flag(tp, ICH_WORKAROUND))
Michael Chanb401e9e2005-12-19 16:27:04 -0800554 /* Non-posted methods */
555 tp->write32(tp, off, val);
556 else {
557 /* Posted method */
558 tg3_write32(tp, off, val);
559 if (usec_wait)
560 udelay(usec_wait);
561 tp->read32(tp, off);
562 }
563 /* Wait again after the read for the posted method to guarantee that
564 * the wait time is met.
565 */
566 if (usec_wait)
567 udelay(usec_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568}
569
Michael Chan09ee9292005-08-09 20:17:00 -0700570static inline void tw32_mailbox_flush(struct tg3 *tp, u32 off, u32 val)
571{
572 tp->write32_mbox(tp, off, val);
Joe Perches63c3a662011-04-26 08:12:10 +0000573 if (!tg3_flag(tp, MBOX_WRITE_REORDER) && !tg3_flag(tp, ICH_WORKAROUND))
Michael Chan68929142005-08-09 20:17:14 -0700574 tp->read32_mbox(tp, off);
Michael Chan09ee9292005-08-09 20:17:00 -0700575}
576
Michael Chan20094932005-08-09 20:16:32 -0700577static void tg3_write32_tx_mbox(struct tg3 *tp, u32 off, u32 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578{
579 void __iomem *mbox = tp->regs + off;
580 writel(val, mbox);
Joe Perches63c3a662011-04-26 08:12:10 +0000581 if (tg3_flag(tp, TXD_MBOX_HWBUG))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 writel(val, mbox);
Joe Perches63c3a662011-04-26 08:12:10 +0000583 if (tg3_flag(tp, MBOX_WRITE_REORDER))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 readl(mbox);
585}
586
Michael Chanb5d37722006-09-27 16:06:21 -0700587static u32 tg3_read32_mbox_5906(struct tg3 *tp, u32 off)
588{
Matt Carlsonde6f31e2010-04-12 06:58:30 +0000589 return readl(tp->regs + off + GRCMBOX_BASE);
Michael Chanb5d37722006-09-27 16:06:21 -0700590}
591
592static void tg3_write32_mbox_5906(struct tg3 *tp, u32 off, u32 val)
593{
594 writel(val, tp->regs + off + GRCMBOX_BASE);
595}
596
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000597#define tw32_mailbox(reg, val) tp->write32_mbox(tp, reg, val)
Michael Chan09ee9292005-08-09 20:17:00 -0700598#define tw32_mailbox_f(reg, val) tw32_mailbox_flush(tp, (reg), (val))
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000599#define tw32_rx_mbox(reg, val) tp->write32_rx_mbox(tp, reg, val)
600#define tw32_tx_mbox(reg, val) tp->write32_tx_mbox(tp, reg, val)
601#define tr32_mailbox(reg) tp->read32_mbox(tp, reg)
Michael Chan20094932005-08-09 20:16:32 -0700602
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000603#define tw32(reg, val) tp->write32(tp, reg, val)
604#define tw32_f(reg, val) _tw32_flush(tp, (reg), (val), 0)
605#define tw32_wait_f(reg, val, us) _tw32_flush(tp, (reg), (val), (us))
606#define tr32(reg) tp->read32(tp, reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607
608static void tg3_write_mem(struct tg3 *tp, u32 off, u32 val)
609{
Michael Chan68929142005-08-09 20:17:14 -0700610 unsigned long flags;
611
Matt Carlson6ff6f812011-05-19 12:12:54 +0000612 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906 &&
Michael Chanb5d37722006-09-27 16:06:21 -0700613 (off >= NIC_SRAM_STATS_BLK) && (off < NIC_SRAM_TX_BUFFER_DESC))
614 return;
615
Michael Chan68929142005-08-09 20:17:14 -0700616 spin_lock_irqsave(&tp->indirect_lock, flags);
Joe Perches63c3a662011-04-26 08:12:10 +0000617 if (tg3_flag(tp, SRAM_USE_CONFIG)) {
Michael Chanbbadf502006-04-06 21:46:34 -0700618 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, off);
619 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_DATA, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620
Michael Chanbbadf502006-04-06 21:46:34 -0700621 /* Always leave this as zero. */
622 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, 0);
623 } else {
624 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, off);
625 tw32_f(TG3PCI_MEM_WIN_DATA, val);
626
627 /* Always leave this as zero. */
628 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, 0);
629 }
Michael Chan68929142005-08-09 20:17:14 -0700630 spin_unlock_irqrestore(&tp->indirect_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631}
632
633static void tg3_read_mem(struct tg3 *tp, u32 off, u32 *val)
634{
Michael Chan68929142005-08-09 20:17:14 -0700635 unsigned long flags;
636
Matt Carlson6ff6f812011-05-19 12:12:54 +0000637 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906 &&
Michael Chanb5d37722006-09-27 16:06:21 -0700638 (off >= NIC_SRAM_STATS_BLK) && (off < NIC_SRAM_TX_BUFFER_DESC)) {
639 *val = 0;
640 return;
641 }
642
Michael Chan68929142005-08-09 20:17:14 -0700643 spin_lock_irqsave(&tp->indirect_lock, flags);
Joe Perches63c3a662011-04-26 08:12:10 +0000644 if (tg3_flag(tp, SRAM_USE_CONFIG)) {
Michael Chanbbadf502006-04-06 21:46:34 -0700645 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, off);
646 pci_read_config_dword(tp->pdev, TG3PCI_MEM_WIN_DATA, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647
Michael Chanbbadf502006-04-06 21:46:34 -0700648 /* Always leave this as zero. */
649 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, 0);
650 } else {
651 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, off);
652 *val = tr32(TG3PCI_MEM_WIN_DATA);
653
654 /* Always leave this as zero. */
655 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, 0);
656 }
Michael Chan68929142005-08-09 20:17:14 -0700657 spin_unlock_irqrestore(&tp->indirect_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658}
659
Matt Carlson0d3031d2007-10-10 18:02:43 -0700660static void tg3_ape_lock_init(struct tg3 *tp)
661{
662 int i;
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000663 u32 regbase, bit;
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000664
665 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
666 regbase = TG3_APE_LOCK_GRANT;
667 else
668 regbase = TG3_APE_PER_LOCK_GRANT;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700669
670 /* Make sure the driver hasn't any stale locks. */
Matt Carlson78f94dc2011-11-04 09:14:58 +0000671 for (i = TG3_APE_LOCK_PHY0; i <= TG3_APE_LOCK_GPIO; i++) {
672 switch (i) {
673 case TG3_APE_LOCK_PHY0:
674 case TG3_APE_LOCK_PHY1:
675 case TG3_APE_LOCK_PHY2:
676 case TG3_APE_LOCK_PHY3:
677 bit = APE_LOCK_GRANT_DRIVER;
678 break;
679 default:
680 if (!tp->pci_fn)
681 bit = APE_LOCK_GRANT_DRIVER;
682 else
683 bit = 1 << tp->pci_fn;
684 }
685 tg3_ape_write32(tp, regbase + 4 * i, bit);
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000686 }
687
Matt Carlson0d3031d2007-10-10 18:02:43 -0700688}
689
690static int tg3_ape_lock(struct tg3 *tp, int locknum)
691{
692 int i, off;
693 int ret = 0;
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000694 u32 status, req, gnt, bit;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700695
Joe Perches63c3a662011-04-26 08:12:10 +0000696 if (!tg3_flag(tp, ENABLE_APE))
Matt Carlson0d3031d2007-10-10 18:02:43 -0700697 return 0;
698
699 switch (locknum) {
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000700 case TG3_APE_LOCK_GPIO:
701 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
702 return 0;
Matt Carlson33f401a2010-04-05 10:19:27 +0000703 case TG3_APE_LOCK_GRC:
704 case TG3_APE_LOCK_MEM:
Matt Carlson78f94dc2011-11-04 09:14:58 +0000705 if (!tp->pci_fn)
706 bit = APE_LOCK_REQ_DRIVER;
707 else
708 bit = 1 << tp->pci_fn;
Matt Carlson33f401a2010-04-05 10:19:27 +0000709 break;
Michael Chan8151ad52012-07-29 19:15:41 +0000710 case TG3_APE_LOCK_PHY0:
711 case TG3_APE_LOCK_PHY1:
712 case TG3_APE_LOCK_PHY2:
713 case TG3_APE_LOCK_PHY3:
714 bit = APE_LOCK_REQ_DRIVER;
715 break;
Matt Carlson33f401a2010-04-05 10:19:27 +0000716 default:
717 return -EINVAL;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700718 }
719
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000720 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761) {
721 req = TG3_APE_LOCK_REQ;
722 gnt = TG3_APE_LOCK_GRANT;
723 } else {
724 req = TG3_APE_PER_LOCK_REQ;
725 gnt = TG3_APE_PER_LOCK_GRANT;
726 }
727
Matt Carlson0d3031d2007-10-10 18:02:43 -0700728 off = 4 * locknum;
729
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000730 tg3_ape_write32(tp, req + off, bit);
Matt Carlson0d3031d2007-10-10 18:02:43 -0700731
732 /* Wait for up to 1 millisecond to acquire lock. */
733 for (i = 0; i < 100; i++) {
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000734 status = tg3_ape_read32(tp, gnt + off);
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000735 if (status == bit)
Matt Carlson0d3031d2007-10-10 18:02:43 -0700736 break;
737 udelay(10);
738 }
739
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000740 if (status != bit) {
Matt Carlson0d3031d2007-10-10 18:02:43 -0700741 /* Revoke the lock request. */
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000742 tg3_ape_write32(tp, gnt + off, bit);
Matt Carlson0d3031d2007-10-10 18:02:43 -0700743 ret = -EBUSY;
744 }
745
746 return ret;
747}
748
749static void tg3_ape_unlock(struct tg3 *tp, int locknum)
750{
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000751 u32 gnt, bit;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700752
Joe Perches63c3a662011-04-26 08:12:10 +0000753 if (!tg3_flag(tp, ENABLE_APE))
Matt Carlson0d3031d2007-10-10 18:02:43 -0700754 return;
755
756 switch (locknum) {
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000757 case TG3_APE_LOCK_GPIO:
758 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
759 return;
Matt Carlson33f401a2010-04-05 10:19:27 +0000760 case TG3_APE_LOCK_GRC:
761 case TG3_APE_LOCK_MEM:
Matt Carlson78f94dc2011-11-04 09:14:58 +0000762 if (!tp->pci_fn)
763 bit = APE_LOCK_GRANT_DRIVER;
764 else
765 bit = 1 << tp->pci_fn;
Matt Carlson33f401a2010-04-05 10:19:27 +0000766 break;
Michael Chan8151ad52012-07-29 19:15:41 +0000767 case TG3_APE_LOCK_PHY0:
768 case TG3_APE_LOCK_PHY1:
769 case TG3_APE_LOCK_PHY2:
770 case TG3_APE_LOCK_PHY3:
771 bit = APE_LOCK_GRANT_DRIVER;
772 break;
Matt Carlson33f401a2010-04-05 10:19:27 +0000773 default:
774 return;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700775 }
776
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000777 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
778 gnt = TG3_APE_LOCK_GRANT;
779 else
780 gnt = TG3_APE_PER_LOCK_GRANT;
781
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000782 tg3_ape_write32(tp, gnt + 4 * locknum, bit);
Matt Carlson0d3031d2007-10-10 18:02:43 -0700783}
784
Matt Carlsonb65a3722012-07-16 16:24:00 +0000785static int tg3_ape_event_lock(struct tg3 *tp, u32 timeout_us)
Matt Carlsonfd6d3f02011-08-31 11:44:52 +0000786{
Matt Carlsonfd6d3f02011-08-31 11:44:52 +0000787 u32 apedata;
788
Matt Carlsonb65a3722012-07-16 16:24:00 +0000789 while (timeout_us) {
Matt Carlsonfd6d3f02011-08-31 11:44:52 +0000790 if (tg3_ape_lock(tp, TG3_APE_LOCK_MEM))
Matt Carlsonb65a3722012-07-16 16:24:00 +0000791 return -EBUSY;
Matt Carlsonfd6d3f02011-08-31 11:44:52 +0000792
793 apedata = tg3_ape_read32(tp, TG3_APE_EVENT_STATUS);
Matt Carlsonfd6d3f02011-08-31 11:44:52 +0000794 if (!(apedata & APE_EVENT_STATUS_EVENT_PENDING))
795 break;
796
Matt Carlsonb65a3722012-07-16 16:24:00 +0000797 tg3_ape_unlock(tp, TG3_APE_LOCK_MEM);
798
799 udelay(10);
800 timeout_us -= (timeout_us > 10) ? 10 : timeout_us;
Matt Carlsonfd6d3f02011-08-31 11:44:52 +0000801 }
802
Matt Carlsonb65a3722012-07-16 16:24:00 +0000803 return timeout_us ? 0 : -EBUSY;
804}
805
Matt Carlsoncf8d55a2012-07-16 16:24:01 +0000806static int tg3_ape_wait_for_event(struct tg3 *tp, u32 timeout_us)
807{
808 u32 i, apedata;
809
810 for (i = 0; i < timeout_us / 10; i++) {
811 apedata = tg3_ape_read32(tp, TG3_APE_EVENT_STATUS);
812
813 if (!(apedata & APE_EVENT_STATUS_EVENT_PENDING))
814 break;
815
816 udelay(10);
817 }
818
819 return i == timeout_us / 10;
820}
821
Michael Chan86449942012-10-02 20:31:14 -0700822static int tg3_ape_scratchpad_read(struct tg3 *tp, u32 *data, u32 base_off,
823 u32 len)
Matt Carlsoncf8d55a2012-07-16 16:24:01 +0000824{
825 int err;
826 u32 i, bufoff, msgoff, maxlen, apedata;
827
828 if (!tg3_flag(tp, APE_HAS_NCSI))
829 return 0;
830
831 apedata = tg3_ape_read32(tp, TG3_APE_SEG_SIG);
832 if (apedata != APE_SEG_SIG_MAGIC)
833 return -ENODEV;
834
835 apedata = tg3_ape_read32(tp, TG3_APE_FW_STATUS);
836 if (!(apedata & APE_FW_STATUS_READY))
837 return -EAGAIN;
838
839 bufoff = tg3_ape_read32(tp, TG3_APE_SEG_MSG_BUF_OFF) +
840 TG3_APE_SHMEM_BASE;
841 msgoff = bufoff + 2 * sizeof(u32);
842 maxlen = tg3_ape_read32(tp, TG3_APE_SEG_MSG_BUF_LEN);
843
844 while (len) {
845 u32 length;
846
847 /* Cap xfer sizes to scratchpad limits. */
848 length = (len > maxlen) ? maxlen : len;
849 len -= length;
850
851 apedata = tg3_ape_read32(tp, TG3_APE_FW_STATUS);
852 if (!(apedata & APE_FW_STATUS_READY))
853 return -EAGAIN;
854
855 /* Wait for up to 1 msec for APE to service previous event. */
856 err = tg3_ape_event_lock(tp, 1000);
857 if (err)
858 return err;
859
860 apedata = APE_EVENT_STATUS_DRIVER_EVNT |
861 APE_EVENT_STATUS_SCRTCHPD_READ |
862 APE_EVENT_STATUS_EVENT_PENDING;
863 tg3_ape_write32(tp, TG3_APE_EVENT_STATUS, apedata);
864
865 tg3_ape_write32(tp, bufoff, base_off);
866 tg3_ape_write32(tp, bufoff + sizeof(u32), length);
867
868 tg3_ape_unlock(tp, TG3_APE_LOCK_MEM);
869 tg3_ape_write32(tp, TG3_APE_EVENT, APE_EVENT_1);
870
871 base_off += length;
872
873 if (tg3_ape_wait_for_event(tp, 30000))
874 return -EAGAIN;
875
876 for (i = 0; length; i += 4, length -= 4) {
877 u32 val = tg3_ape_read32(tp, msgoff + i);
878 memcpy(data, &val, sizeof(u32));
879 data++;
880 }
881 }
882
883 return 0;
884}
885
Matt Carlsonb65a3722012-07-16 16:24:00 +0000886static int tg3_ape_send_event(struct tg3 *tp, u32 event)
887{
888 int err;
889 u32 apedata;
890
891 apedata = tg3_ape_read32(tp, TG3_APE_SEG_SIG);
892 if (apedata != APE_SEG_SIG_MAGIC)
893 return -EAGAIN;
894
895 apedata = tg3_ape_read32(tp, TG3_APE_FW_STATUS);
896 if (!(apedata & APE_FW_STATUS_READY))
897 return -EAGAIN;
898
899 /* Wait for up to 1 millisecond for APE to service previous event. */
900 err = tg3_ape_event_lock(tp, 1000);
901 if (err)
902 return err;
903
904 tg3_ape_write32(tp, TG3_APE_EVENT_STATUS,
905 event | APE_EVENT_STATUS_EVENT_PENDING);
906
907 tg3_ape_unlock(tp, TG3_APE_LOCK_MEM);
908 tg3_ape_write32(tp, TG3_APE_EVENT, APE_EVENT_1);
909
910 return 0;
Matt Carlsonfd6d3f02011-08-31 11:44:52 +0000911}
912
913static void tg3_ape_driver_state_change(struct tg3 *tp, int kind)
914{
915 u32 event;
916 u32 apedata;
917
918 if (!tg3_flag(tp, ENABLE_APE))
919 return;
920
921 switch (kind) {
922 case RESET_KIND_INIT:
923 tg3_ape_write32(tp, TG3_APE_HOST_SEG_SIG,
924 APE_HOST_SEG_SIG_MAGIC);
925 tg3_ape_write32(tp, TG3_APE_HOST_SEG_LEN,
926 APE_HOST_SEG_LEN_MAGIC);
927 apedata = tg3_ape_read32(tp, TG3_APE_HOST_INIT_COUNT);
928 tg3_ape_write32(tp, TG3_APE_HOST_INIT_COUNT, ++apedata);
929 tg3_ape_write32(tp, TG3_APE_HOST_DRIVER_ID,
930 APE_HOST_DRIVER_ID_MAGIC(TG3_MAJ_NUM, TG3_MIN_NUM));
931 tg3_ape_write32(tp, TG3_APE_HOST_BEHAVIOR,
932 APE_HOST_BEHAV_NO_PHYLOCK);
933 tg3_ape_write32(tp, TG3_APE_HOST_DRVR_STATE,
934 TG3_APE_HOST_DRVR_STATE_START);
935
936 event = APE_EVENT_STATUS_STATE_START;
937 break;
938 case RESET_KIND_SHUTDOWN:
939 /* With the interface we are currently using,
940 * APE does not track driver state. Wiping
941 * out the HOST SEGMENT SIGNATURE forces
942 * the APE to assume OS absent status.
943 */
944 tg3_ape_write32(tp, TG3_APE_HOST_SEG_SIG, 0x0);
945
946 if (device_may_wakeup(&tp->pdev->dev) &&
947 tg3_flag(tp, WOL_ENABLE)) {
948 tg3_ape_write32(tp, TG3_APE_HOST_WOL_SPEED,
949 TG3_APE_HOST_WOL_SPEED_AUTO);
950 apedata = TG3_APE_HOST_DRVR_STATE_WOL;
951 } else
952 apedata = TG3_APE_HOST_DRVR_STATE_UNLOAD;
953
954 tg3_ape_write32(tp, TG3_APE_HOST_DRVR_STATE, apedata);
955
956 event = APE_EVENT_STATUS_STATE_UNLOAD;
957 break;
958 case RESET_KIND_SUSPEND:
959 event = APE_EVENT_STATUS_STATE_SUSPEND;
960 break;
961 default:
962 return;
963 }
964
965 event |= APE_EVENT_STATUS_DRIVER_EVNT | APE_EVENT_STATUS_STATE_CHNGE;
966
967 tg3_ape_send_event(tp, event);
968}
969
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970static void tg3_disable_ints(struct tg3 *tp)
971{
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000972 int i;
973
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 tw32(TG3PCI_MISC_HOST_CTRL,
975 (tp->misc_host_ctrl | MISC_HOST_CTRL_MASK_PCI_INT));
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000976 for (i = 0; i < tp->irq_max; i++)
977 tw32_mailbox_f(tp->napi[i].int_mbox, 0x00000001);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978}
979
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980static void tg3_enable_ints(struct tg3 *tp)
981{
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000982 int i;
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000983
Michael Chanbbe832c2005-06-24 20:20:04 -0700984 tp->irq_sync = 0;
985 wmb();
986
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 tw32(TG3PCI_MISC_HOST_CTRL,
988 (tp->misc_host_ctrl & ~MISC_HOST_CTRL_MASK_PCI_INT));
Matt Carlsonf19af9c2009-09-01 12:47:49 +0000989
Matt Carlsonf89f38b2010-02-12 14:47:07 +0000990 tp->coal_now = tp->coalesce_mode | HOSTCC_MODE_ENABLE;
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000991 for (i = 0; i < tp->irq_cnt; i++) {
992 struct tg3_napi *tnapi = &tp->napi[i];
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000993
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000994 tw32_mailbox_f(tnapi->int_mbox, tnapi->last_tag << 24);
Joe Perches63c3a662011-04-26 08:12:10 +0000995 if (tg3_flag(tp, 1SHOT_MSI))
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000996 tw32_mailbox_f(tnapi->int_mbox, tnapi->last_tag << 24);
997
Matt Carlsonf89f38b2010-02-12 14:47:07 +0000998 tp->coal_now |= tnapi->coal_now;
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000999 }
Matt Carlsonf19af9c2009-09-01 12:47:49 +00001000
1001 /* Force an initial interrupt */
Joe Perches63c3a662011-04-26 08:12:10 +00001002 if (!tg3_flag(tp, TAGGED_STATUS) &&
Matt Carlsonf19af9c2009-09-01 12:47:49 +00001003 (tp->napi[0].hw_status->status & SD_STATUS_UPDATED))
1004 tw32(GRC_LOCAL_CTRL, tp->grc_local_ctrl | GRC_LCLCTRL_SETINT);
1005 else
Matt Carlsonf89f38b2010-02-12 14:47:07 +00001006 tw32(HOSTCC_MODE, tp->coal_now);
1007
1008 tp->coal_now &= ~(tp->napi[0].coal_now | tp->napi[1].coal_now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009}
1010
Matt Carlson17375d22009-08-28 14:02:18 +00001011static inline unsigned int tg3_has_work(struct tg3_napi *tnapi)
Michael Chan04237dd2005-04-25 15:17:17 -07001012{
Matt Carlson17375d22009-08-28 14:02:18 +00001013 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00001014 struct tg3_hw_status *sblk = tnapi->hw_status;
Michael Chan04237dd2005-04-25 15:17:17 -07001015 unsigned int work_exists = 0;
1016
1017 /* check for phy events */
Joe Perches63c3a662011-04-26 08:12:10 +00001018 if (!(tg3_flag(tp, USE_LINKCHG_REG) || tg3_flag(tp, POLL_SERDES))) {
Michael Chan04237dd2005-04-25 15:17:17 -07001019 if (sblk->status & SD_STATUS_LINK_CHG)
1020 work_exists = 1;
1021 }
Matt Carlsonf891ea12012-04-24 13:37:01 +00001022
1023 /* check for TX work to do */
1024 if (sblk->idx[0].tx_consumer != tnapi->tx_cons)
1025 work_exists = 1;
1026
1027 /* check for RX work to do */
1028 if (tnapi->rx_rcb_prod_idx &&
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00001029 *(tnapi->rx_rcb_prod_idx) != tnapi->rx_rcb_ptr)
Michael Chan04237dd2005-04-25 15:17:17 -07001030 work_exists = 1;
1031
1032 return work_exists;
1033}
1034
Matt Carlson17375d22009-08-28 14:02:18 +00001035/* tg3_int_reenable
Michael Chan04237dd2005-04-25 15:17:17 -07001036 * similar to tg3_enable_ints, but it accurately determines whether there
1037 * is new work pending and can return without flushing the PIO write
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001038 * which reenables interrupts
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 */
Matt Carlson17375d22009-08-28 14:02:18 +00001040static void tg3_int_reenable(struct tg3_napi *tnapi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041{
Matt Carlson17375d22009-08-28 14:02:18 +00001042 struct tg3 *tp = tnapi->tp;
1043
Matt Carlson898a56f2009-08-28 14:02:40 +00001044 tw32_mailbox(tnapi->int_mbox, tnapi->last_tag << 24);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 mmiowb();
1046
David S. Millerfac9b832005-05-18 22:46:34 -07001047 /* When doing tagged status, this work check is unnecessary.
1048 * The last_tag we write above tells the chip which piece of
1049 * work we've completed.
1050 */
Joe Perches63c3a662011-04-26 08:12:10 +00001051 if (!tg3_flag(tp, TAGGED_STATUS) && tg3_has_work(tnapi))
Michael Chan04237dd2005-04-25 15:17:17 -07001052 tw32(HOSTCC_MODE, tp->coalesce_mode |
Matt Carlsonfd2ce372009-09-01 12:51:13 +00001053 HOSTCC_MODE_ENABLE | tnapi->coal_now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054}
1055
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056static void tg3_switch_clocks(struct tg3 *tp)
1057{
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00001058 u32 clock_ctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 u32 orig_clock_ctrl;
1060
Joe Perches63c3a662011-04-26 08:12:10 +00001061 if (tg3_flag(tp, CPMU_PRESENT) || tg3_flag(tp, 5780_CLASS))
Michael Chan4cf78e42005-07-25 12:29:19 -07001062 return;
1063
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00001064 clock_ctrl = tr32(TG3PCI_CLOCK_CTRL);
1065
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 orig_clock_ctrl = clock_ctrl;
1067 clock_ctrl &= (CLOCK_CTRL_FORCE_CLKRUN |
1068 CLOCK_CTRL_CLKRUN_OENABLE |
1069 0x1f);
1070 tp->pci_clock_ctrl = clock_ctrl;
1071
Joe Perches63c3a662011-04-26 08:12:10 +00001072 if (tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 if (orig_clock_ctrl & CLOCK_CTRL_625_CORE) {
Michael Chanb401e9e2005-12-19 16:27:04 -08001074 tw32_wait_f(TG3PCI_CLOCK_CTRL,
1075 clock_ctrl | CLOCK_CTRL_625_CORE, 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 }
1077 } else if ((orig_clock_ctrl & CLOCK_CTRL_44MHZ_CORE) != 0) {
Michael Chanb401e9e2005-12-19 16:27:04 -08001078 tw32_wait_f(TG3PCI_CLOCK_CTRL,
1079 clock_ctrl |
1080 (CLOCK_CTRL_44MHZ_CORE | CLOCK_CTRL_ALTCLK),
1081 40);
1082 tw32_wait_f(TG3PCI_CLOCK_CTRL,
1083 clock_ctrl | (CLOCK_CTRL_ALTCLK),
1084 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 }
Michael Chanb401e9e2005-12-19 16:27:04 -08001086 tw32_wait_f(TG3PCI_CLOCK_CTRL, clock_ctrl, 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087}
1088
1089#define PHY_BUSY_LOOPS 5000
1090
1091static int tg3_readphy(struct tg3 *tp, int reg, u32 *val)
1092{
1093 u32 frame_val;
1094 unsigned int loops;
1095 int ret;
1096
1097 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
1098 tw32_f(MAC_MI_MODE,
1099 (tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL));
1100 udelay(80);
1101 }
1102
Michael Chan8151ad52012-07-29 19:15:41 +00001103 tg3_ape_lock(tp, tp->phy_ape_lock);
1104
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 *val = 0x0;
1106
Matt Carlson882e9792009-09-01 13:21:36 +00001107 frame_val = ((tp->phy_addr << MI_COM_PHY_ADDR_SHIFT) &
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 MI_COM_PHY_ADDR_MASK);
1109 frame_val |= ((reg << MI_COM_REG_ADDR_SHIFT) &
1110 MI_COM_REG_ADDR_MASK);
1111 frame_val |= (MI_COM_CMD_READ | MI_COM_START);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001112
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 tw32_f(MAC_MI_COM, frame_val);
1114
1115 loops = PHY_BUSY_LOOPS;
1116 while (loops != 0) {
1117 udelay(10);
1118 frame_val = tr32(MAC_MI_COM);
1119
1120 if ((frame_val & MI_COM_BUSY) == 0) {
1121 udelay(5);
1122 frame_val = tr32(MAC_MI_COM);
1123 break;
1124 }
1125 loops -= 1;
1126 }
1127
1128 ret = -EBUSY;
1129 if (loops != 0) {
1130 *val = frame_val & MI_COM_DATA_MASK;
1131 ret = 0;
1132 }
1133
1134 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
1135 tw32_f(MAC_MI_MODE, tp->mi_mode);
1136 udelay(80);
1137 }
1138
Michael Chan8151ad52012-07-29 19:15:41 +00001139 tg3_ape_unlock(tp, tp->phy_ape_lock);
1140
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 return ret;
1142}
1143
1144static int tg3_writephy(struct tg3 *tp, int reg, u32 val)
1145{
1146 u32 frame_val;
1147 unsigned int loops;
1148 int ret;
1149
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001150 if ((tp->phy_flags & TG3_PHYFLG_IS_FET) &&
Matt Carlson221c5632011-06-13 13:39:01 +00001151 (reg == MII_CTRL1000 || reg == MII_TG3_AUX_CTRL))
Michael Chanb5d37722006-09-27 16:06:21 -07001152 return 0;
1153
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
1155 tw32_f(MAC_MI_MODE,
1156 (tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL));
1157 udelay(80);
1158 }
1159
Michael Chan8151ad52012-07-29 19:15:41 +00001160 tg3_ape_lock(tp, tp->phy_ape_lock);
1161
Matt Carlson882e9792009-09-01 13:21:36 +00001162 frame_val = ((tp->phy_addr << MI_COM_PHY_ADDR_SHIFT) &
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 MI_COM_PHY_ADDR_MASK);
1164 frame_val |= ((reg << MI_COM_REG_ADDR_SHIFT) &
1165 MI_COM_REG_ADDR_MASK);
1166 frame_val |= (val & MI_COM_DATA_MASK);
1167 frame_val |= (MI_COM_CMD_WRITE | MI_COM_START);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001168
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 tw32_f(MAC_MI_COM, frame_val);
1170
1171 loops = PHY_BUSY_LOOPS;
1172 while (loops != 0) {
1173 udelay(10);
1174 frame_val = tr32(MAC_MI_COM);
1175 if ((frame_val & MI_COM_BUSY) == 0) {
1176 udelay(5);
1177 frame_val = tr32(MAC_MI_COM);
1178 break;
1179 }
1180 loops -= 1;
1181 }
1182
1183 ret = -EBUSY;
1184 if (loops != 0)
1185 ret = 0;
1186
1187 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
1188 tw32_f(MAC_MI_MODE, tp->mi_mode);
1189 udelay(80);
1190 }
1191
Michael Chan8151ad52012-07-29 19:15:41 +00001192 tg3_ape_unlock(tp, tp->phy_ape_lock);
1193
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 return ret;
1195}
1196
Matt Carlsonb0988c12011-04-20 07:57:39 +00001197static int tg3_phy_cl45_write(struct tg3 *tp, u32 devad, u32 addr, u32 val)
1198{
1199 int err;
1200
1201 err = tg3_writephy(tp, MII_TG3_MMD_CTRL, devad);
1202 if (err)
1203 goto done;
1204
1205 err = tg3_writephy(tp, MII_TG3_MMD_ADDRESS, addr);
1206 if (err)
1207 goto done;
1208
1209 err = tg3_writephy(tp, MII_TG3_MMD_CTRL,
1210 MII_TG3_MMD_CTRL_DATA_NOINC | devad);
1211 if (err)
1212 goto done;
1213
1214 err = tg3_writephy(tp, MII_TG3_MMD_ADDRESS, val);
1215
1216done:
1217 return err;
1218}
1219
1220static int tg3_phy_cl45_read(struct tg3 *tp, u32 devad, u32 addr, u32 *val)
1221{
1222 int err;
1223
1224 err = tg3_writephy(tp, MII_TG3_MMD_CTRL, devad);
1225 if (err)
1226 goto done;
1227
1228 err = tg3_writephy(tp, MII_TG3_MMD_ADDRESS, addr);
1229 if (err)
1230 goto done;
1231
1232 err = tg3_writephy(tp, MII_TG3_MMD_CTRL,
1233 MII_TG3_MMD_CTRL_DATA_NOINC | devad);
1234 if (err)
1235 goto done;
1236
1237 err = tg3_readphy(tp, MII_TG3_MMD_ADDRESS, val);
1238
1239done:
1240 return err;
1241}
1242
1243static int tg3_phydsp_read(struct tg3 *tp, u32 reg, u32 *val)
1244{
1245 int err;
1246
1247 err = tg3_writephy(tp, MII_TG3_DSP_ADDRESS, reg);
1248 if (!err)
1249 err = tg3_readphy(tp, MII_TG3_DSP_RW_PORT, val);
1250
1251 return err;
1252}
1253
1254static int tg3_phydsp_write(struct tg3 *tp, u32 reg, u32 val)
1255{
1256 int err;
1257
1258 err = tg3_writephy(tp, MII_TG3_DSP_ADDRESS, reg);
1259 if (!err)
1260 err = tg3_writephy(tp, MII_TG3_DSP_RW_PORT, val);
1261
1262 return err;
1263}
1264
Matt Carlson15ee95c2011-04-20 07:57:40 +00001265static int tg3_phy_auxctl_read(struct tg3 *tp, int reg, u32 *val)
1266{
1267 int err;
1268
1269 err = tg3_writephy(tp, MII_TG3_AUX_CTRL,
1270 (reg << MII_TG3_AUXCTL_MISC_RDSEL_SHIFT) |
1271 MII_TG3_AUXCTL_SHDWSEL_MISC);
1272 if (!err)
1273 err = tg3_readphy(tp, MII_TG3_AUX_CTRL, val);
1274
1275 return err;
1276}
1277
Matt Carlsonb4bd2922011-04-20 07:57:41 +00001278static int tg3_phy_auxctl_write(struct tg3 *tp, int reg, u32 set)
1279{
1280 if (reg == MII_TG3_AUXCTL_SHDWSEL_MISC)
1281 set |= MII_TG3_AUXCTL_MISC_WREN;
1282
1283 return tg3_writephy(tp, MII_TG3_AUX_CTRL, set | reg);
1284}
1285
Matt Carlson1d36ba42011-04-20 07:57:42 +00001286#define TG3_PHY_AUXCTL_SMDSP_ENABLE(tp) \
1287 tg3_phy_auxctl_write((tp), MII_TG3_AUXCTL_SHDWSEL_AUXCTL, \
1288 MII_TG3_AUXCTL_ACTL_SMDSP_ENA | \
1289 MII_TG3_AUXCTL_ACTL_TX_6DB)
1290
1291#define TG3_PHY_AUXCTL_SMDSP_DISABLE(tp) \
1292 tg3_phy_auxctl_write((tp), MII_TG3_AUXCTL_SHDWSEL_AUXCTL, \
1293 MII_TG3_AUXCTL_ACTL_TX_6DB);
1294
Matt Carlson95e28692008-05-25 23:44:14 -07001295static int tg3_bmcr_reset(struct tg3 *tp)
1296{
1297 u32 phy_control;
1298 int limit, err;
1299
1300 /* OK, reset it, and poll the BMCR_RESET bit until it
1301 * clears or we time out.
1302 */
1303 phy_control = BMCR_RESET;
1304 err = tg3_writephy(tp, MII_BMCR, phy_control);
1305 if (err != 0)
1306 return -EBUSY;
1307
1308 limit = 5000;
1309 while (limit--) {
1310 err = tg3_readphy(tp, MII_BMCR, &phy_control);
1311 if (err != 0)
1312 return -EBUSY;
1313
1314 if ((phy_control & BMCR_RESET) == 0) {
1315 udelay(40);
1316 break;
1317 }
1318 udelay(10);
1319 }
Roel Kluind4675b52009-02-12 16:33:27 -08001320 if (limit < 0)
Matt Carlson95e28692008-05-25 23:44:14 -07001321 return -EBUSY;
1322
1323 return 0;
1324}
1325
Matt Carlson158d7ab2008-05-29 01:37:54 -07001326static int tg3_mdio_read(struct mii_bus *bp, int mii_id, int reg)
1327{
Francois Romieu3d165432009-01-19 16:56:50 -08001328 struct tg3 *tp = bp->priv;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001329 u32 val;
1330
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001331 spin_lock_bh(&tp->lock);
Matt Carlson158d7ab2008-05-29 01:37:54 -07001332
1333 if (tg3_readphy(tp, reg, &val))
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001334 val = -EIO;
1335
1336 spin_unlock_bh(&tp->lock);
Matt Carlson158d7ab2008-05-29 01:37:54 -07001337
1338 return val;
1339}
1340
1341static int tg3_mdio_write(struct mii_bus *bp, int mii_id, int reg, u16 val)
1342{
Francois Romieu3d165432009-01-19 16:56:50 -08001343 struct tg3 *tp = bp->priv;
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001344 u32 ret = 0;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001345
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001346 spin_lock_bh(&tp->lock);
Matt Carlson158d7ab2008-05-29 01:37:54 -07001347
1348 if (tg3_writephy(tp, reg, val))
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001349 ret = -EIO;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001350
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001351 spin_unlock_bh(&tp->lock);
1352
1353 return ret;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001354}
1355
1356static int tg3_mdio_reset(struct mii_bus *bp)
1357{
1358 return 0;
1359}
1360
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001361static void tg3_mdio_config_5785(struct tg3 *tp)
Matt Carlsona9daf362008-05-25 23:49:44 -07001362{
1363 u32 val;
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001364 struct phy_device *phydev;
Matt Carlsona9daf362008-05-25 23:49:44 -07001365
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001366 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001367 switch (phydev->drv->phy_id & phydev->drv->phy_id_mask) {
Matt Carlson6a443a02010-02-17 15:17:04 +00001368 case PHY_ID_BCM50610:
1369 case PHY_ID_BCM50610M:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001370 val = MAC_PHYCFG2_50610_LED_MODES;
1371 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001372 case PHY_ID_BCMAC131:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001373 val = MAC_PHYCFG2_AC131_LED_MODES;
1374 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001375 case PHY_ID_RTL8211C:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001376 val = MAC_PHYCFG2_RTL8211C_LED_MODES;
1377 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001378 case PHY_ID_RTL8201E:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001379 val = MAC_PHYCFG2_RTL8201E_LED_MODES;
1380 break;
1381 default:
Matt Carlsona9daf362008-05-25 23:49:44 -07001382 return;
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001383 }
1384
1385 if (phydev->interface != PHY_INTERFACE_MODE_RGMII) {
1386 tw32(MAC_PHYCFG2, val);
1387
1388 val = tr32(MAC_PHYCFG1);
Matt Carlsonbb85fbb2009-08-25 10:09:07 +00001389 val &= ~(MAC_PHYCFG1_RGMII_INT |
1390 MAC_PHYCFG1_RXCLK_TO_MASK | MAC_PHYCFG1_TXCLK_TO_MASK);
1391 val |= MAC_PHYCFG1_RXCLK_TIMEOUT | MAC_PHYCFG1_TXCLK_TIMEOUT;
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001392 tw32(MAC_PHYCFG1, val);
1393
1394 return;
1395 }
1396
Joe Perches63c3a662011-04-26 08:12:10 +00001397 if (!tg3_flag(tp, RGMII_INBAND_DISABLE))
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001398 val |= MAC_PHYCFG2_EMODE_MASK_MASK |
1399 MAC_PHYCFG2_FMODE_MASK_MASK |
1400 MAC_PHYCFG2_GMODE_MASK_MASK |
1401 MAC_PHYCFG2_ACT_MASK_MASK |
1402 MAC_PHYCFG2_QUAL_MASK_MASK |
1403 MAC_PHYCFG2_INBAND_ENABLE;
1404
1405 tw32(MAC_PHYCFG2, val);
Matt Carlsona9daf362008-05-25 23:49:44 -07001406
Matt Carlsonbb85fbb2009-08-25 10:09:07 +00001407 val = tr32(MAC_PHYCFG1);
1408 val &= ~(MAC_PHYCFG1_RXCLK_TO_MASK | MAC_PHYCFG1_TXCLK_TO_MASK |
1409 MAC_PHYCFG1_RGMII_EXT_RX_DEC | MAC_PHYCFG1_RGMII_SND_STAT_EN);
Joe Perches63c3a662011-04-26 08:12:10 +00001410 if (!tg3_flag(tp, RGMII_INBAND_DISABLE)) {
1411 if (tg3_flag(tp, RGMII_EXT_IBND_RX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001412 val |= MAC_PHYCFG1_RGMII_EXT_RX_DEC;
Joe Perches63c3a662011-04-26 08:12:10 +00001413 if (tg3_flag(tp, RGMII_EXT_IBND_TX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001414 val |= MAC_PHYCFG1_RGMII_SND_STAT_EN;
1415 }
Matt Carlsonbb85fbb2009-08-25 10:09:07 +00001416 val |= MAC_PHYCFG1_RXCLK_TIMEOUT | MAC_PHYCFG1_TXCLK_TIMEOUT |
1417 MAC_PHYCFG1_RGMII_INT | MAC_PHYCFG1_TXC_DRV;
1418 tw32(MAC_PHYCFG1, val);
Matt Carlsona9daf362008-05-25 23:49:44 -07001419
Matt Carlsona9daf362008-05-25 23:49:44 -07001420 val = tr32(MAC_EXT_RGMII_MODE);
1421 val &= ~(MAC_RGMII_MODE_RX_INT_B |
1422 MAC_RGMII_MODE_RX_QUALITY |
1423 MAC_RGMII_MODE_RX_ACTIVITY |
1424 MAC_RGMII_MODE_RX_ENG_DET |
1425 MAC_RGMII_MODE_TX_ENABLE |
1426 MAC_RGMII_MODE_TX_LOWPWR |
1427 MAC_RGMII_MODE_TX_RESET);
Joe Perches63c3a662011-04-26 08:12:10 +00001428 if (!tg3_flag(tp, RGMII_INBAND_DISABLE)) {
1429 if (tg3_flag(tp, RGMII_EXT_IBND_RX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001430 val |= MAC_RGMII_MODE_RX_INT_B |
1431 MAC_RGMII_MODE_RX_QUALITY |
1432 MAC_RGMII_MODE_RX_ACTIVITY |
1433 MAC_RGMII_MODE_RX_ENG_DET;
Joe Perches63c3a662011-04-26 08:12:10 +00001434 if (tg3_flag(tp, RGMII_EXT_IBND_TX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001435 val |= MAC_RGMII_MODE_TX_ENABLE |
1436 MAC_RGMII_MODE_TX_LOWPWR |
1437 MAC_RGMII_MODE_TX_RESET;
1438 }
1439 tw32(MAC_EXT_RGMII_MODE, val);
1440}
1441
Matt Carlson158d7ab2008-05-29 01:37:54 -07001442static void tg3_mdio_start(struct tg3 *tp)
1443{
Matt Carlson158d7ab2008-05-29 01:37:54 -07001444 tp->mi_mode &= ~MAC_MI_MODE_AUTO_POLL;
1445 tw32_f(MAC_MI_MODE, tp->mi_mode);
1446 udelay(80);
Matt Carlsona9daf362008-05-25 23:49:44 -07001447
Joe Perches63c3a662011-04-26 08:12:10 +00001448 if (tg3_flag(tp, MDIOBUS_INITED) &&
Matt Carlson9ea48182010-02-17 15:17:01 +00001449 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
1450 tg3_mdio_config_5785(tp);
1451}
1452
1453static int tg3_mdio_init(struct tg3 *tp)
1454{
1455 int i;
1456 u32 reg;
1457 struct phy_device *phydev;
1458
Joe Perches63c3a662011-04-26 08:12:10 +00001459 if (tg3_flag(tp, 5717_PLUS)) {
Matt Carlson9c7df912010-06-05 17:24:36 +00001460 u32 is_serdes;
Matt Carlson882e9792009-09-01 13:21:36 +00001461
Matt Carlson69f11c92011-07-13 09:27:30 +00001462 tp->phy_addr = tp->pci_fn + 1;
Matt Carlson882e9792009-09-01 13:21:36 +00001463
Matt Carlsond1ec96a2010-01-12 10:11:38 +00001464 if (tp->pci_chip_rev_id != CHIPREV_ID_5717_A0)
1465 is_serdes = tr32(SG_DIG_STATUS) & SG_DIG_IS_SERDES;
1466 else
1467 is_serdes = tr32(TG3_CPMU_PHY_STRAP) &
1468 TG3_CPMU_PHY_STRAP_IS_SERDES;
Matt Carlson882e9792009-09-01 13:21:36 +00001469 if (is_serdes)
1470 tp->phy_addr += 7;
1471 } else
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001472 tp->phy_addr = TG3_PHY_MII_ADDR;
Matt Carlson882e9792009-09-01 13:21:36 +00001473
Matt Carlson158d7ab2008-05-29 01:37:54 -07001474 tg3_mdio_start(tp);
1475
Joe Perches63c3a662011-04-26 08:12:10 +00001476 if (!tg3_flag(tp, USE_PHYLIB) || tg3_flag(tp, MDIOBUS_INITED))
Matt Carlson158d7ab2008-05-29 01:37:54 -07001477 return 0;
1478
Lennert Buytenhek298cf9be2008-10-08 16:29:57 -07001479 tp->mdio_bus = mdiobus_alloc();
1480 if (tp->mdio_bus == NULL)
1481 return -ENOMEM;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001482
Lennert Buytenhek298cf9be2008-10-08 16:29:57 -07001483 tp->mdio_bus->name = "tg3 mdio bus";
1484 snprintf(tp->mdio_bus->id, MII_BUS_ID_SIZE, "%x",
Matt Carlson158d7ab2008-05-29 01:37:54 -07001485 (tp->pdev->bus->number << 8) | tp->pdev->devfn);
Lennert Buytenhek298cf9be2008-10-08 16:29:57 -07001486 tp->mdio_bus->priv = tp;
1487 tp->mdio_bus->parent = &tp->pdev->dev;
1488 tp->mdio_bus->read = &tg3_mdio_read;
1489 tp->mdio_bus->write = &tg3_mdio_write;
1490 tp->mdio_bus->reset = &tg3_mdio_reset;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001491 tp->mdio_bus->phy_mask = ~(1 << TG3_PHY_MII_ADDR);
Lennert Buytenhek298cf9be2008-10-08 16:29:57 -07001492 tp->mdio_bus->irq = &tp->mdio_irq[0];
Matt Carlson158d7ab2008-05-29 01:37:54 -07001493
1494 for (i = 0; i < PHY_MAX_ADDR; i++)
Lennert Buytenhek298cf9be2008-10-08 16:29:57 -07001495 tp->mdio_bus->irq[i] = PHY_POLL;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001496
1497 /* The bus registration will look for all the PHYs on the mdio bus.
1498 * Unfortunately, it does not ensure the PHY is powered up before
1499 * accessing the PHY ID registers. A chip reset is the
1500 * quickest way to bring the device back to an operational state..
1501 */
1502 if (tg3_readphy(tp, MII_BMCR, &reg) || (reg & BMCR_PDOWN))
1503 tg3_bmcr_reset(tp);
1504
Lennert Buytenhek298cf9be2008-10-08 16:29:57 -07001505 i = mdiobus_register(tp->mdio_bus);
Matt Carlsona9daf362008-05-25 23:49:44 -07001506 if (i) {
Matt Carlsonab96b242010-04-05 10:19:22 +00001507 dev_warn(&tp->pdev->dev, "mdiobus_reg failed (0x%x)\n", i);
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001508 mdiobus_free(tp->mdio_bus);
Matt Carlsona9daf362008-05-25 23:49:44 -07001509 return i;
1510 }
Matt Carlson158d7ab2008-05-29 01:37:54 -07001511
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001512 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsona9daf362008-05-25 23:49:44 -07001513
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001514 if (!phydev || !phydev->drv) {
Matt Carlsonab96b242010-04-05 10:19:22 +00001515 dev_warn(&tp->pdev->dev, "No PHY devices\n");
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001516 mdiobus_unregister(tp->mdio_bus);
1517 mdiobus_free(tp->mdio_bus);
1518 return -ENODEV;
1519 }
1520
1521 switch (phydev->drv->phy_id & phydev->drv->phy_id_mask) {
Matt Carlson6a443a02010-02-17 15:17:04 +00001522 case PHY_ID_BCM57780:
Matt Carlson321d32a2008-11-21 17:22:19 -08001523 phydev->interface = PHY_INTERFACE_MODE_GMII;
Matt Carlsonc704dc22009-11-02 14:32:12 +00001524 phydev->dev_flags |= PHY_BRCM_AUTO_PWRDWN_ENABLE;
Matt Carlson321d32a2008-11-21 17:22:19 -08001525 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001526 case PHY_ID_BCM50610:
1527 case PHY_ID_BCM50610M:
Matt Carlson32e5a8d2009-11-02 14:31:39 +00001528 phydev->dev_flags |= PHY_BRCM_CLEAR_RGMII_MODE |
Matt Carlsonc704dc22009-11-02 14:32:12 +00001529 PHY_BRCM_RX_REFCLK_UNUSED |
Matt Carlson52fae082009-11-02 14:32:38 +00001530 PHY_BRCM_DIS_TXCRXC_NOENRGY |
Matt Carlsonc704dc22009-11-02 14:32:12 +00001531 PHY_BRCM_AUTO_PWRDWN_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00001532 if (tg3_flag(tp, RGMII_INBAND_DISABLE))
Matt Carlsona9daf362008-05-25 23:49:44 -07001533 phydev->dev_flags |= PHY_BRCM_STD_IBND_DISABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00001534 if (tg3_flag(tp, RGMII_EXT_IBND_RX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001535 phydev->dev_flags |= PHY_BRCM_EXT_IBND_RX_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00001536 if (tg3_flag(tp, RGMII_EXT_IBND_TX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001537 phydev->dev_flags |= PHY_BRCM_EXT_IBND_TX_ENABLE;
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001538 /* fallthru */
Matt Carlson6a443a02010-02-17 15:17:04 +00001539 case PHY_ID_RTL8211C:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001540 phydev->interface = PHY_INTERFACE_MODE_RGMII;
Matt Carlsona9daf362008-05-25 23:49:44 -07001541 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001542 case PHY_ID_RTL8201E:
1543 case PHY_ID_BCMAC131:
Matt Carlsona9daf362008-05-25 23:49:44 -07001544 phydev->interface = PHY_INTERFACE_MODE_MII;
Matt Carlsoncdd4e09d2009-11-02 14:31:11 +00001545 phydev->dev_flags |= PHY_BRCM_AUTO_PWRDWN_ENABLE;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001546 tp->phy_flags |= TG3_PHYFLG_IS_FET;
Matt Carlsona9daf362008-05-25 23:49:44 -07001547 break;
1548 }
1549
Joe Perches63c3a662011-04-26 08:12:10 +00001550 tg3_flag_set(tp, MDIOBUS_INITED);
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001551
1552 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
1553 tg3_mdio_config_5785(tp);
Matt Carlsona9daf362008-05-25 23:49:44 -07001554
1555 return 0;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001556}
1557
1558static void tg3_mdio_fini(struct tg3 *tp)
1559{
Joe Perches63c3a662011-04-26 08:12:10 +00001560 if (tg3_flag(tp, MDIOBUS_INITED)) {
1561 tg3_flag_clear(tp, MDIOBUS_INITED);
Lennert Buytenhek298cf9be2008-10-08 16:29:57 -07001562 mdiobus_unregister(tp->mdio_bus);
1563 mdiobus_free(tp->mdio_bus);
Matt Carlson158d7ab2008-05-29 01:37:54 -07001564 }
1565}
1566
Matt Carlson95e28692008-05-25 23:44:14 -07001567/* tp->lock is held. */
Matt Carlson4ba526c2008-08-15 14:10:04 -07001568static inline void tg3_generate_fw_event(struct tg3 *tp)
1569{
1570 u32 val;
1571
1572 val = tr32(GRC_RX_CPU_EVENT);
1573 val |= GRC_RX_CPU_DRIVER_EVENT;
1574 tw32_f(GRC_RX_CPU_EVENT, val);
1575
1576 tp->last_event_jiffies = jiffies;
1577}
1578
1579#define TG3_FW_EVENT_TIMEOUT_USEC 2500
1580
1581/* tp->lock is held. */
Matt Carlson95e28692008-05-25 23:44:14 -07001582static void tg3_wait_for_event_ack(struct tg3 *tp)
1583{
1584 int i;
Matt Carlson4ba526c2008-08-15 14:10:04 -07001585 unsigned int delay_cnt;
1586 long time_remain;
Matt Carlson95e28692008-05-25 23:44:14 -07001587
Matt Carlson4ba526c2008-08-15 14:10:04 -07001588 /* If enough time has passed, no wait is necessary. */
1589 time_remain = (long)(tp->last_event_jiffies + 1 +
1590 usecs_to_jiffies(TG3_FW_EVENT_TIMEOUT_USEC)) -
1591 (long)jiffies;
1592 if (time_remain < 0)
1593 return;
1594
1595 /* Check if we can shorten the wait time. */
1596 delay_cnt = jiffies_to_usecs(time_remain);
1597 if (delay_cnt > TG3_FW_EVENT_TIMEOUT_USEC)
1598 delay_cnt = TG3_FW_EVENT_TIMEOUT_USEC;
1599 delay_cnt = (delay_cnt >> 3) + 1;
1600
1601 for (i = 0; i < delay_cnt; i++) {
Matt Carlson95e28692008-05-25 23:44:14 -07001602 if (!(tr32(GRC_RX_CPU_EVENT) & GRC_RX_CPU_DRIVER_EVENT))
1603 break;
Matt Carlson4ba526c2008-08-15 14:10:04 -07001604 udelay(8);
Matt Carlson95e28692008-05-25 23:44:14 -07001605 }
1606}
1607
1608/* tp->lock is held. */
Matt Carlsonb28f3892012-02-13 15:20:12 +00001609static void tg3_phy_gather_ump_data(struct tg3 *tp, u32 *data)
Matt Carlson95e28692008-05-25 23:44:14 -07001610{
Matt Carlsonb28f3892012-02-13 15:20:12 +00001611 u32 reg, val;
Matt Carlson95e28692008-05-25 23:44:14 -07001612
1613 val = 0;
1614 if (!tg3_readphy(tp, MII_BMCR, &reg))
1615 val = reg << 16;
1616 if (!tg3_readphy(tp, MII_BMSR, &reg))
1617 val |= (reg & 0xffff);
Matt Carlsonb28f3892012-02-13 15:20:12 +00001618 *data++ = val;
Matt Carlson95e28692008-05-25 23:44:14 -07001619
1620 val = 0;
1621 if (!tg3_readphy(tp, MII_ADVERTISE, &reg))
1622 val = reg << 16;
1623 if (!tg3_readphy(tp, MII_LPA, &reg))
1624 val |= (reg & 0xffff);
Matt Carlsonb28f3892012-02-13 15:20:12 +00001625 *data++ = val;
Matt Carlson95e28692008-05-25 23:44:14 -07001626
1627 val = 0;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001628 if (!(tp->phy_flags & TG3_PHYFLG_MII_SERDES)) {
Matt Carlson95e28692008-05-25 23:44:14 -07001629 if (!tg3_readphy(tp, MII_CTRL1000, &reg))
1630 val = reg << 16;
1631 if (!tg3_readphy(tp, MII_STAT1000, &reg))
1632 val |= (reg & 0xffff);
1633 }
Matt Carlsonb28f3892012-02-13 15:20:12 +00001634 *data++ = val;
Matt Carlson95e28692008-05-25 23:44:14 -07001635
1636 if (!tg3_readphy(tp, MII_PHYADDR, &reg))
1637 val = reg << 16;
1638 else
1639 val = 0;
Matt Carlsonb28f3892012-02-13 15:20:12 +00001640 *data++ = val;
1641}
1642
1643/* tp->lock is held. */
1644static void tg3_ump_link_report(struct tg3 *tp)
1645{
1646 u32 data[4];
1647
1648 if (!tg3_flag(tp, 5780_CLASS) || !tg3_flag(tp, ENABLE_ASF))
1649 return;
1650
1651 tg3_phy_gather_ump_data(tp, data);
1652
1653 tg3_wait_for_event_ack(tp);
1654
1655 tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX, FWCMD_NICDRV_LINK_UPDATE);
1656 tg3_write_mem(tp, NIC_SRAM_FW_CMD_LEN_MBOX, 14);
1657 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 0x0, data[0]);
1658 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 0x4, data[1]);
1659 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 0x8, data[2]);
1660 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 0xc, data[3]);
Matt Carlson95e28692008-05-25 23:44:14 -07001661
Matt Carlson4ba526c2008-08-15 14:10:04 -07001662 tg3_generate_fw_event(tp);
Matt Carlson95e28692008-05-25 23:44:14 -07001663}
1664
Matt Carlson8d5a89b2011-08-31 11:44:51 +00001665/* tp->lock is held. */
1666static void tg3_stop_fw(struct tg3 *tp)
1667{
1668 if (tg3_flag(tp, ENABLE_ASF) && !tg3_flag(tp, ENABLE_APE)) {
1669 /* Wait for RX cpu to ACK the previous event. */
1670 tg3_wait_for_event_ack(tp);
1671
1672 tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX, FWCMD_NICDRV_PAUSE_FW);
1673
1674 tg3_generate_fw_event(tp);
1675
1676 /* Wait for RX cpu to ACK this event. */
1677 tg3_wait_for_event_ack(tp);
1678 }
1679}
1680
Matt Carlsonfd6d3f02011-08-31 11:44:52 +00001681/* tp->lock is held. */
1682static void tg3_write_sig_pre_reset(struct tg3 *tp, int kind)
1683{
1684 tg3_write_mem(tp, NIC_SRAM_FIRMWARE_MBOX,
1685 NIC_SRAM_FIRMWARE_MBOX_MAGIC1);
1686
1687 if (tg3_flag(tp, ASF_NEW_HANDSHAKE)) {
1688 switch (kind) {
1689 case RESET_KIND_INIT:
1690 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1691 DRV_STATE_START);
1692 break;
1693
1694 case RESET_KIND_SHUTDOWN:
1695 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1696 DRV_STATE_UNLOAD);
1697 break;
1698
1699 case RESET_KIND_SUSPEND:
1700 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1701 DRV_STATE_SUSPEND);
1702 break;
1703
1704 default:
1705 break;
1706 }
1707 }
1708
1709 if (kind == RESET_KIND_INIT ||
1710 kind == RESET_KIND_SUSPEND)
1711 tg3_ape_driver_state_change(tp, kind);
1712}
1713
1714/* tp->lock is held. */
1715static void tg3_write_sig_post_reset(struct tg3 *tp, int kind)
1716{
1717 if (tg3_flag(tp, ASF_NEW_HANDSHAKE)) {
1718 switch (kind) {
1719 case RESET_KIND_INIT:
1720 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1721 DRV_STATE_START_DONE);
1722 break;
1723
1724 case RESET_KIND_SHUTDOWN:
1725 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1726 DRV_STATE_UNLOAD_DONE);
1727 break;
1728
1729 default:
1730 break;
1731 }
1732 }
1733
1734 if (kind == RESET_KIND_SHUTDOWN)
1735 tg3_ape_driver_state_change(tp, kind);
1736}
1737
1738/* tp->lock is held. */
1739static void tg3_write_sig_legacy(struct tg3 *tp, int kind)
1740{
1741 if (tg3_flag(tp, ENABLE_ASF)) {
1742 switch (kind) {
1743 case RESET_KIND_INIT:
1744 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1745 DRV_STATE_START);
1746 break;
1747
1748 case RESET_KIND_SHUTDOWN:
1749 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1750 DRV_STATE_UNLOAD);
1751 break;
1752
1753 case RESET_KIND_SUSPEND:
1754 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
1755 DRV_STATE_SUSPEND);
1756 break;
1757
1758 default:
1759 break;
1760 }
1761 }
1762}
1763
1764static int tg3_poll_fw(struct tg3 *tp)
1765{
1766 int i;
1767 u32 val;
1768
1769 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
1770 /* Wait up to 20ms for init done. */
1771 for (i = 0; i < 200; i++) {
1772 if (tr32(VCPU_STATUS) & VCPU_STATUS_INIT_DONE)
1773 return 0;
1774 udelay(100);
1775 }
1776 return -ENODEV;
1777 }
1778
1779 /* Wait for firmware initialization to complete. */
1780 for (i = 0; i < 100000; i++) {
1781 tg3_read_mem(tp, NIC_SRAM_FIRMWARE_MBOX, &val);
1782 if (val == ~NIC_SRAM_FIRMWARE_MBOX_MAGIC1)
1783 break;
1784 udelay(10);
1785 }
1786
1787 /* Chip might not be fitted with firmware. Some Sun onboard
1788 * parts are configured like that. So don't signal the timeout
1789 * of the above loop as an error, but do report the lack of
1790 * running firmware once.
1791 */
1792 if (i >= 100000 && !tg3_flag(tp, NO_FWARE_REPORTED)) {
1793 tg3_flag_set(tp, NO_FWARE_REPORTED);
1794
1795 netdev_info(tp->dev, "No firmware running\n");
1796 }
1797
1798 if (tp->pci_chip_rev_id == CHIPREV_ID_57765_A0) {
1799 /* The 57765 A0 needs a little more
1800 * time to do some important work.
1801 */
1802 mdelay(10);
1803 }
1804
1805 return 0;
1806}
1807
Matt Carlson95e28692008-05-25 23:44:14 -07001808static void tg3_link_report(struct tg3 *tp)
1809{
1810 if (!netif_carrier_ok(tp->dev)) {
Joe Perches05dbe002010-02-17 19:44:19 +00001811 netif_info(tp, link, tp->dev, "Link is down\n");
Matt Carlson95e28692008-05-25 23:44:14 -07001812 tg3_ump_link_report(tp);
1813 } else if (netif_msg_link(tp)) {
Joe Perches05dbe002010-02-17 19:44:19 +00001814 netdev_info(tp->dev, "Link is up at %d Mbps, %s duplex\n",
1815 (tp->link_config.active_speed == SPEED_1000 ?
1816 1000 :
1817 (tp->link_config.active_speed == SPEED_100 ?
1818 100 : 10)),
1819 (tp->link_config.active_duplex == DUPLEX_FULL ?
1820 "full" : "half"));
Matt Carlson95e28692008-05-25 23:44:14 -07001821
Joe Perches05dbe002010-02-17 19:44:19 +00001822 netdev_info(tp->dev, "Flow control is %s for TX and %s for RX\n",
1823 (tp->link_config.active_flowctrl & FLOW_CTRL_TX) ?
1824 "on" : "off",
1825 (tp->link_config.active_flowctrl & FLOW_CTRL_RX) ?
1826 "on" : "off");
Matt Carlson47007832011-04-20 07:57:43 +00001827
1828 if (tp->phy_flags & TG3_PHYFLG_EEE_CAP)
1829 netdev_info(tp->dev, "EEE is %s\n",
1830 tp->setlpicnt ? "enabled" : "disabled");
1831
Matt Carlson95e28692008-05-25 23:44:14 -07001832 tg3_ump_link_report(tp);
1833 }
1834}
1835
Matt Carlson95e28692008-05-25 23:44:14 -07001836static u16 tg3_advert_flowctrl_1000X(u8 flow_ctrl)
1837{
1838 u16 miireg;
1839
Steve Glendinninge18ce342008-12-16 02:00:00 -08001840 if ((flow_ctrl & FLOW_CTRL_TX) && (flow_ctrl & FLOW_CTRL_RX))
Matt Carlson95e28692008-05-25 23:44:14 -07001841 miireg = ADVERTISE_1000XPAUSE;
Steve Glendinninge18ce342008-12-16 02:00:00 -08001842 else if (flow_ctrl & FLOW_CTRL_TX)
Matt Carlson95e28692008-05-25 23:44:14 -07001843 miireg = ADVERTISE_1000XPSE_ASYM;
Steve Glendinninge18ce342008-12-16 02:00:00 -08001844 else if (flow_ctrl & FLOW_CTRL_RX)
Matt Carlson95e28692008-05-25 23:44:14 -07001845 miireg = ADVERTISE_1000XPAUSE | ADVERTISE_1000XPSE_ASYM;
1846 else
1847 miireg = 0;
1848
1849 return miireg;
1850}
1851
Matt Carlson95e28692008-05-25 23:44:14 -07001852static u8 tg3_resolve_flowctrl_1000X(u16 lcladv, u16 rmtadv)
1853{
1854 u8 cap = 0;
1855
Matt Carlsonf3791cd2011-11-21 15:01:17 +00001856 if (lcladv & rmtadv & ADVERTISE_1000XPAUSE) {
1857 cap = FLOW_CTRL_TX | FLOW_CTRL_RX;
1858 } else if (lcladv & rmtadv & ADVERTISE_1000XPSE_ASYM) {
1859 if (lcladv & ADVERTISE_1000XPAUSE)
1860 cap = FLOW_CTRL_RX;
1861 if (rmtadv & ADVERTISE_1000XPAUSE)
Steve Glendinninge18ce342008-12-16 02:00:00 -08001862 cap = FLOW_CTRL_TX;
Matt Carlson95e28692008-05-25 23:44:14 -07001863 }
1864
1865 return cap;
1866}
1867
Matt Carlsonf51f3562008-05-25 23:45:08 -07001868static void tg3_setup_flow_control(struct tg3 *tp, u32 lcladv, u32 rmtadv)
Matt Carlson95e28692008-05-25 23:44:14 -07001869{
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001870 u8 autoneg;
Matt Carlsonf51f3562008-05-25 23:45:08 -07001871 u8 flowctrl = 0;
Matt Carlson95e28692008-05-25 23:44:14 -07001872 u32 old_rx_mode = tp->rx_mode;
1873 u32 old_tx_mode = tp->tx_mode;
1874
Joe Perches63c3a662011-04-26 08:12:10 +00001875 if (tg3_flag(tp, USE_PHYLIB))
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001876 autoneg = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]->autoneg;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001877 else
1878 autoneg = tp->link_config.autoneg;
1879
Joe Perches63c3a662011-04-26 08:12:10 +00001880 if (autoneg == AUTONEG_ENABLE && tg3_flag(tp, PAUSE_AUTONEG)) {
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001881 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
Matt Carlsonf51f3562008-05-25 23:45:08 -07001882 flowctrl = tg3_resolve_flowctrl_1000X(lcladv, rmtadv);
Matt Carlson95e28692008-05-25 23:44:14 -07001883 else
Steve Glendinningbc02ff92008-12-16 02:00:48 -08001884 flowctrl = mii_resolve_flowctrl_fdx(lcladv, rmtadv);
Matt Carlsonf51f3562008-05-25 23:45:08 -07001885 } else
1886 flowctrl = tp->link_config.flowctrl;
Matt Carlson95e28692008-05-25 23:44:14 -07001887
Matt Carlsonf51f3562008-05-25 23:45:08 -07001888 tp->link_config.active_flowctrl = flowctrl;
Matt Carlson95e28692008-05-25 23:44:14 -07001889
Steve Glendinninge18ce342008-12-16 02:00:00 -08001890 if (flowctrl & FLOW_CTRL_RX)
Matt Carlson95e28692008-05-25 23:44:14 -07001891 tp->rx_mode |= RX_MODE_FLOW_CTRL_ENABLE;
1892 else
1893 tp->rx_mode &= ~RX_MODE_FLOW_CTRL_ENABLE;
1894
Matt Carlsonf51f3562008-05-25 23:45:08 -07001895 if (old_rx_mode != tp->rx_mode)
Matt Carlson95e28692008-05-25 23:44:14 -07001896 tw32_f(MAC_RX_MODE, tp->rx_mode);
Matt Carlson95e28692008-05-25 23:44:14 -07001897
Steve Glendinninge18ce342008-12-16 02:00:00 -08001898 if (flowctrl & FLOW_CTRL_TX)
Matt Carlson95e28692008-05-25 23:44:14 -07001899 tp->tx_mode |= TX_MODE_FLOW_CTRL_ENABLE;
1900 else
1901 tp->tx_mode &= ~TX_MODE_FLOW_CTRL_ENABLE;
1902
Matt Carlsonf51f3562008-05-25 23:45:08 -07001903 if (old_tx_mode != tp->tx_mode)
Matt Carlson95e28692008-05-25 23:44:14 -07001904 tw32_f(MAC_TX_MODE, tp->tx_mode);
Matt Carlson95e28692008-05-25 23:44:14 -07001905}
1906
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001907static void tg3_adjust_link(struct net_device *dev)
1908{
1909 u8 oldflowctrl, linkmesg = 0;
1910 u32 mac_mode, lcl_adv, rmt_adv;
1911 struct tg3 *tp = netdev_priv(dev);
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001912 struct phy_device *phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001913
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001914 spin_lock_bh(&tp->lock);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001915
1916 mac_mode = tp->mac_mode & ~(MAC_MODE_PORT_MODE_MASK |
1917 MAC_MODE_HALF_DUPLEX);
1918
1919 oldflowctrl = tp->link_config.active_flowctrl;
1920
1921 if (phydev->link) {
1922 lcl_adv = 0;
1923 rmt_adv = 0;
1924
1925 if (phydev->speed == SPEED_100 || phydev->speed == SPEED_10)
1926 mac_mode |= MAC_MODE_PORT_MODE_MII;
Matt Carlsonc3df0742009-11-02 14:27:02 +00001927 else if (phydev->speed == SPEED_1000 ||
1928 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785)
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001929 mac_mode |= MAC_MODE_PORT_MODE_GMII;
Matt Carlsonc3df0742009-11-02 14:27:02 +00001930 else
1931 mac_mode |= MAC_MODE_PORT_MODE_MII;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001932
1933 if (phydev->duplex == DUPLEX_HALF)
1934 mac_mode |= MAC_MODE_HALF_DUPLEX;
1935 else {
Matt Carlsonf88788f2011-12-14 11:10:00 +00001936 lcl_adv = mii_advertise_flowctrl(
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001937 tp->link_config.flowctrl);
1938
1939 if (phydev->pause)
1940 rmt_adv = LPA_PAUSE_CAP;
1941 if (phydev->asym_pause)
1942 rmt_adv |= LPA_PAUSE_ASYM;
1943 }
1944
1945 tg3_setup_flow_control(tp, lcl_adv, rmt_adv);
1946 } else
1947 mac_mode |= MAC_MODE_PORT_MODE_GMII;
1948
1949 if (mac_mode != tp->mac_mode) {
1950 tp->mac_mode = mac_mode;
1951 tw32_f(MAC_MODE, tp->mac_mode);
1952 udelay(40);
1953 }
1954
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001955 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785) {
1956 if (phydev->speed == SPEED_10)
1957 tw32(MAC_MI_STAT,
1958 MAC_MI_STAT_10MBPS_MODE |
1959 MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
1960 else
1961 tw32(MAC_MI_STAT, MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
1962 }
1963
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001964 if (phydev->speed == SPEED_1000 && phydev->duplex == DUPLEX_HALF)
1965 tw32(MAC_TX_LENGTHS,
1966 ((2 << TX_LENGTHS_IPG_CRS_SHIFT) |
1967 (6 << TX_LENGTHS_IPG_SHIFT) |
1968 (0xff << TX_LENGTHS_SLOT_TIME_SHIFT)));
1969 else
1970 tw32(MAC_TX_LENGTHS,
1971 ((2 << TX_LENGTHS_IPG_CRS_SHIFT) |
1972 (6 << TX_LENGTHS_IPG_SHIFT) |
1973 (32 << TX_LENGTHS_SLOT_TIME_SHIFT)));
1974
Matt Carlson34655ad2012-02-22 12:35:18 +00001975 if (phydev->link != tp->old_link ||
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001976 phydev->speed != tp->link_config.active_speed ||
1977 phydev->duplex != tp->link_config.active_duplex ||
1978 oldflowctrl != tp->link_config.active_flowctrl)
Matt Carlsonc6cdf432010-04-05 10:19:26 +00001979 linkmesg = 1;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001980
Matt Carlson34655ad2012-02-22 12:35:18 +00001981 tp->old_link = phydev->link;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001982 tp->link_config.active_speed = phydev->speed;
1983 tp->link_config.active_duplex = phydev->duplex;
1984
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001985 spin_unlock_bh(&tp->lock);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001986
1987 if (linkmesg)
1988 tg3_link_report(tp);
1989}
1990
1991static int tg3_phy_init(struct tg3 *tp)
1992{
1993 struct phy_device *phydev;
1994
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001995 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED)
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001996 return 0;
1997
1998 /* Bring the PHY back to a known state. */
1999 tg3_bmcr_reset(tp);
2000
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00002001 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002002
2003 /* Attach the MAC to the PHY. */
Kay Sieversfb28ad32008-11-10 13:55:14 -08002004 phydev = phy_connect(tp->dev, dev_name(&phydev->dev), tg3_adjust_link,
Matt Carlsona9daf362008-05-25 23:49:44 -07002005 phydev->dev_flags, phydev->interface);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002006 if (IS_ERR(phydev)) {
Matt Carlsonab96b242010-04-05 10:19:22 +00002007 dev_err(&tp->pdev->dev, "Could not attach to PHY\n");
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002008 return PTR_ERR(phydev);
2009 }
2010
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002011 /* Mask with MAC supported features. */
Matt Carlson9c61d6b2008-11-03 16:54:56 -08002012 switch (phydev->interface) {
2013 case PHY_INTERFACE_MODE_GMII:
2014 case PHY_INTERFACE_MODE_RGMII:
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002015 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
Matt Carlson321d32a2008-11-21 17:22:19 -08002016 phydev->supported &= (PHY_GBIT_FEATURES |
2017 SUPPORTED_Pause |
2018 SUPPORTED_Asym_Pause);
2019 break;
2020 }
2021 /* fallthru */
Matt Carlson9c61d6b2008-11-03 16:54:56 -08002022 case PHY_INTERFACE_MODE_MII:
2023 phydev->supported &= (PHY_BASIC_FEATURES |
2024 SUPPORTED_Pause |
2025 SUPPORTED_Asym_Pause);
2026 break;
2027 default:
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00002028 phy_disconnect(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
Matt Carlson9c61d6b2008-11-03 16:54:56 -08002029 return -EINVAL;
2030 }
2031
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002032 tp->phy_flags |= TG3_PHYFLG_IS_CONNECTED;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002033
2034 phydev->advertising = phydev->supported;
2035
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002036 return 0;
2037}
2038
2039static void tg3_phy_start(struct tg3 *tp)
2040{
2041 struct phy_device *phydev;
2042
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002043 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002044 return;
2045
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00002046 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002047
Matt Carlson800960682010-08-02 11:26:06 +00002048 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) {
2049 tp->phy_flags &= ~TG3_PHYFLG_IS_LOW_POWER;
Matt Carlsonc6700ce2012-02-13 15:20:15 +00002050 phydev->speed = tp->link_config.speed;
2051 phydev->duplex = tp->link_config.duplex;
2052 phydev->autoneg = tp->link_config.autoneg;
2053 phydev->advertising = tp->link_config.advertising;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002054 }
2055
2056 phy_start(phydev);
2057
2058 phy_start_aneg(phydev);
2059}
2060
2061static void tg3_phy_stop(struct tg3 *tp)
2062{
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002063 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002064 return;
2065
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00002066 phy_stop(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002067}
2068
2069static void tg3_phy_fini(struct tg3 *tp)
2070{
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002071 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00002072 phy_disconnect(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002073 tp->phy_flags &= ~TG3_PHYFLG_IS_CONNECTED;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002074 }
2075}
2076
Matt Carlson941ec902011-08-19 13:58:23 +00002077static int tg3_phy_set_extloopbk(struct tg3 *tp)
2078{
2079 int err;
2080 u32 val;
2081
2082 if (tp->phy_flags & TG3_PHYFLG_IS_FET)
2083 return 0;
2084
2085 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
2086 /* Cannot do read-modify-write on 5401 */
2087 err = tg3_phy_auxctl_write(tp,
2088 MII_TG3_AUXCTL_SHDWSEL_AUXCTL,
2089 MII_TG3_AUXCTL_ACTL_EXTLOOPBK |
2090 0x4c20);
2091 goto done;
2092 }
2093
2094 err = tg3_phy_auxctl_read(tp,
2095 MII_TG3_AUXCTL_SHDWSEL_AUXCTL, &val);
2096 if (err)
2097 return err;
2098
2099 val |= MII_TG3_AUXCTL_ACTL_EXTLOOPBK;
2100 err = tg3_phy_auxctl_write(tp,
2101 MII_TG3_AUXCTL_SHDWSEL_AUXCTL, val);
2102
2103done:
2104 return err;
2105}
2106
Matt Carlson7f97a4b2009-08-25 10:10:03 +00002107static void tg3_phy_fet_toggle_apd(struct tg3 *tp, bool enable)
2108{
2109 u32 phytest;
2110
2111 if (!tg3_readphy(tp, MII_TG3_FET_TEST, &phytest)) {
2112 u32 phy;
2113
2114 tg3_writephy(tp, MII_TG3_FET_TEST,
2115 phytest | MII_TG3_FET_SHADOW_EN);
2116 if (!tg3_readphy(tp, MII_TG3_FET_SHDW_AUXSTAT2, &phy)) {
2117 if (enable)
2118 phy |= MII_TG3_FET_SHDW_AUXSTAT2_APD;
2119 else
2120 phy &= ~MII_TG3_FET_SHDW_AUXSTAT2_APD;
2121 tg3_writephy(tp, MII_TG3_FET_SHDW_AUXSTAT2, phy);
2122 }
2123 tg3_writephy(tp, MII_TG3_FET_TEST, phytest);
2124 }
2125}
2126
Matt Carlson6833c042008-11-21 17:18:59 -08002127static void tg3_phy_toggle_apd(struct tg3 *tp, bool enable)
2128{
2129 u32 reg;
2130
Joe Perches63c3a662011-04-26 08:12:10 +00002131 if (!tg3_flag(tp, 5705_PLUS) ||
2132 (tg3_flag(tp, 5717_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002133 (tp->phy_flags & TG3_PHYFLG_MII_SERDES)))
Matt Carlson6833c042008-11-21 17:18:59 -08002134 return;
2135
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002136 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Matt Carlson7f97a4b2009-08-25 10:10:03 +00002137 tg3_phy_fet_toggle_apd(tp, enable);
2138 return;
2139 }
2140
Matt Carlson6833c042008-11-21 17:18:59 -08002141 reg = MII_TG3_MISC_SHDW_WREN |
2142 MII_TG3_MISC_SHDW_SCR5_SEL |
2143 MII_TG3_MISC_SHDW_SCR5_LPED |
2144 MII_TG3_MISC_SHDW_SCR5_DLPTLM |
2145 MII_TG3_MISC_SHDW_SCR5_SDTL |
2146 MII_TG3_MISC_SHDW_SCR5_C125OE;
2147 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5784 || !enable)
2148 reg |= MII_TG3_MISC_SHDW_SCR5_DLLAPD;
2149
2150 tg3_writephy(tp, MII_TG3_MISC_SHDW, reg);
2151
2152
2153 reg = MII_TG3_MISC_SHDW_WREN |
2154 MII_TG3_MISC_SHDW_APD_SEL |
2155 MII_TG3_MISC_SHDW_APD_WKTM_84MS;
2156 if (enable)
2157 reg |= MII_TG3_MISC_SHDW_APD_ENABLE;
2158
2159 tg3_writephy(tp, MII_TG3_MISC_SHDW, reg);
2160}
2161
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002162static void tg3_phy_toggle_automdix(struct tg3 *tp, int enable)
2163{
2164 u32 phy;
2165
Joe Perches63c3a662011-04-26 08:12:10 +00002166 if (!tg3_flag(tp, 5705_PLUS) ||
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002167 (tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002168 return;
2169
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002170 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002171 u32 ephy;
2172
Matt Carlson535ef6e2009-08-25 10:09:36 +00002173 if (!tg3_readphy(tp, MII_TG3_FET_TEST, &ephy)) {
2174 u32 reg = MII_TG3_FET_SHDW_MISCCTRL;
2175
2176 tg3_writephy(tp, MII_TG3_FET_TEST,
2177 ephy | MII_TG3_FET_SHADOW_EN);
2178 if (!tg3_readphy(tp, reg, &phy)) {
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002179 if (enable)
Matt Carlson535ef6e2009-08-25 10:09:36 +00002180 phy |= MII_TG3_FET_SHDW_MISCCTRL_MDIX;
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002181 else
Matt Carlson535ef6e2009-08-25 10:09:36 +00002182 phy &= ~MII_TG3_FET_SHDW_MISCCTRL_MDIX;
2183 tg3_writephy(tp, reg, phy);
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002184 }
Matt Carlson535ef6e2009-08-25 10:09:36 +00002185 tg3_writephy(tp, MII_TG3_FET_TEST, ephy);
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002186 }
2187 } else {
Matt Carlson15ee95c2011-04-20 07:57:40 +00002188 int ret;
2189
2190 ret = tg3_phy_auxctl_read(tp,
2191 MII_TG3_AUXCTL_SHDWSEL_MISC, &phy);
2192 if (!ret) {
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002193 if (enable)
2194 phy |= MII_TG3_AUXCTL_MISC_FORCE_AMDIX;
2195 else
2196 phy &= ~MII_TG3_AUXCTL_MISC_FORCE_AMDIX;
Matt Carlsonb4bd2922011-04-20 07:57:41 +00002197 tg3_phy_auxctl_write(tp,
2198 MII_TG3_AUXCTL_SHDWSEL_MISC, phy);
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002199 }
2200 }
2201}
2202
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203static void tg3_phy_set_wirespeed(struct tg3 *tp)
2204{
Matt Carlson15ee95c2011-04-20 07:57:40 +00002205 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206 u32 val;
2207
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002208 if (tp->phy_flags & TG3_PHYFLG_NO_ETH_WIRE_SPEED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209 return;
2210
Matt Carlson15ee95c2011-04-20 07:57:40 +00002211 ret = tg3_phy_auxctl_read(tp, MII_TG3_AUXCTL_SHDWSEL_MISC, &val);
2212 if (!ret)
Matt Carlsonb4bd2922011-04-20 07:57:41 +00002213 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_MISC,
2214 val | MII_TG3_AUXCTL_MISC_WIRESPD_EN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215}
2216
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002217static void tg3_phy_apply_otp(struct tg3 *tp)
2218{
2219 u32 otp, phy;
2220
2221 if (!tp->phy_otp)
2222 return;
2223
2224 otp = tp->phy_otp;
2225
Matt Carlson1d36ba42011-04-20 07:57:42 +00002226 if (TG3_PHY_AUXCTL_SMDSP_ENABLE(tp))
2227 return;
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002228
2229 phy = ((otp & TG3_OTP_AGCTGT_MASK) >> TG3_OTP_AGCTGT_SHIFT);
2230 phy |= MII_TG3_DSP_TAP1_AGCTGT_DFLT;
2231 tg3_phydsp_write(tp, MII_TG3_DSP_TAP1, phy);
2232
2233 phy = ((otp & TG3_OTP_HPFFLTR_MASK) >> TG3_OTP_HPFFLTR_SHIFT) |
2234 ((otp & TG3_OTP_HPFOVER_MASK) >> TG3_OTP_HPFOVER_SHIFT);
2235 tg3_phydsp_write(tp, MII_TG3_DSP_AADJ1CH0, phy);
2236
2237 phy = ((otp & TG3_OTP_LPFDIS_MASK) >> TG3_OTP_LPFDIS_SHIFT);
2238 phy |= MII_TG3_DSP_AADJ1CH3_ADCCKADJ;
2239 tg3_phydsp_write(tp, MII_TG3_DSP_AADJ1CH3, phy);
2240
2241 phy = ((otp & TG3_OTP_VDAC_MASK) >> TG3_OTP_VDAC_SHIFT);
2242 tg3_phydsp_write(tp, MII_TG3_DSP_EXP75, phy);
2243
2244 phy = ((otp & TG3_OTP_10BTAMP_MASK) >> TG3_OTP_10BTAMP_SHIFT);
2245 tg3_phydsp_write(tp, MII_TG3_DSP_EXP96, phy);
2246
2247 phy = ((otp & TG3_OTP_ROFF_MASK) >> TG3_OTP_ROFF_SHIFT) |
2248 ((otp & TG3_OTP_RCOFF_MASK) >> TG3_OTP_RCOFF_SHIFT);
2249 tg3_phydsp_write(tp, MII_TG3_DSP_EXP97, phy);
2250
Matt Carlson1d36ba42011-04-20 07:57:42 +00002251 TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002252}
2253
Matt Carlson52b02d02010-10-14 10:37:41 +00002254static void tg3_phy_eee_adjust(struct tg3 *tp, u32 current_link_up)
2255{
2256 u32 val;
2257
2258 if (!(tp->phy_flags & TG3_PHYFLG_EEE_CAP))
2259 return;
2260
2261 tp->setlpicnt = 0;
2262
2263 if (tp->link_config.autoneg == AUTONEG_ENABLE &&
2264 current_link_up == 1 &&
Matt Carlsona6b68da2010-12-06 08:28:52 +00002265 tp->link_config.active_duplex == DUPLEX_FULL &&
2266 (tp->link_config.active_speed == SPEED_100 ||
2267 tp->link_config.active_speed == SPEED_1000)) {
Matt Carlson52b02d02010-10-14 10:37:41 +00002268 u32 eeectl;
2269
2270 if (tp->link_config.active_speed == SPEED_1000)
2271 eeectl = TG3_CPMU_EEE_CTRL_EXIT_16_5_US;
2272 else
2273 eeectl = TG3_CPMU_EEE_CTRL_EXIT_36_US;
2274
2275 tw32(TG3_CPMU_EEE_CTRL, eeectl);
2276
Matt Carlson3110f5f52010-12-06 08:28:50 +00002277 tg3_phy_cl45_read(tp, MDIO_MMD_AN,
2278 TG3_CL45_D7_EEERES_STAT, &val);
Matt Carlson52b02d02010-10-14 10:37:41 +00002279
Matt Carlsonb0c59432011-05-19 12:12:48 +00002280 if (val == TG3_CL45_D7_EEERES_STAT_LP_1000T ||
2281 val == TG3_CL45_D7_EEERES_STAT_LP_100TX)
Matt Carlson52b02d02010-10-14 10:37:41 +00002282 tp->setlpicnt = 2;
2283 }
2284
2285 if (!tp->setlpicnt) {
Matt Carlsonb715ce92011-07-20 10:20:52 +00002286 if (current_link_up == 1 &&
2287 !TG3_PHY_AUXCTL_SMDSP_ENABLE(tp)) {
2288 tg3_phydsp_write(tp, MII_TG3_DSP_TAP26, 0x0000);
2289 TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
2290 }
2291
Matt Carlson52b02d02010-10-14 10:37:41 +00002292 val = tr32(TG3_CPMU_EEE_MODE);
2293 tw32(TG3_CPMU_EEE_MODE, val & ~TG3_CPMU_EEEMD_LPI_ENABLE);
2294 }
2295}
2296
Matt Carlsonb0c59432011-05-19 12:12:48 +00002297static void tg3_phy_eee_enable(struct tg3 *tp)
2298{
2299 u32 val;
2300
2301 if (tp->link_config.active_speed == SPEED_1000 &&
2302 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
2303 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
Matt Carlson55086ad2011-12-14 11:09:59 +00002304 tg3_flag(tp, 57765_CLASS)) &&
Matt Carlsonb0c59432011-05-19 12:12:48 +00002305 !TG3_PHY_AUXCTL_SMDSP_ENABLE(tp)) {
Matt Carlsonb715ce92011-07-20 10:20:52 +00002306 val = MII_TG3_DSP_TAP26_ALNOKO |
2307 MII_TG3_DSP_TAP26_RMRXSTO;
2308 tg3_phydsp_write(tp, MII_TG3_DSP_TAP26, val);
Matt Carlsonb0c59432011-05-19 12:12:48 +00002309 TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
2310 }
2311
2312 val = tr32(TG3_CPMU_EEE_MODE);
2313 tw32(TG3_CPMU_EEE_MODE, val | TG3_CPMU_EEEMD_LPI_ENABLE);
2314}
2315
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316static int tg3_wait_macro_done(struct tg3 *tp)
2317{
2318 int limit = 100;
2319
2320 while (limit--) {
2321 u32 tmp32;
2322
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002323 if (!tg3_readphy(tp, MII_TG3_DSP_CONTROL, &tmp32)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324 if ((tmp32 & 0x1000) == 0)
2325 break;
2326 }
2327 }
Roel Kluind4675b52009-02-12 16:33:27 -08002328 if (limit < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329 return -EBUSY;
2330
2331 return 0;
2332}
2333
2334static int tg3_phy_write_and_check_testpat(struct tg3 *tp, int *resetp)
2335{
2336 static const u32 test_pat[4][6] = {
2337 { 0x00005555, 0x00000005, 0x00002aaa, 0x0000000a, 0x00003456, 0x00000003 },
2338 { 0x00002aaa, 0x0000000a, 0x00003333, 0x00000003, 0x0000789a, 0x00000005 },
2339 { 0x00005a5a, 0x00000005, 0x00002a6a, 0x0000000a, 0x00001bcd, 0x00000003 },
2340 { 0x00002a5a, 0x0000000a, 0x000033c3, 0x00000003, 0x00002ef1, 0x00000005 }
2341 };
2342 int chan;
2343
2344 for (chan = 0; chan < 4; chan++) {
2345 int i;
2346
2347 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
2348 (chan * 0x2000) | 0x0200);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002349 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0002);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350
2351 for (i = 0; i < 6; i++)
2352 tg3_writephy(tp, MII_TG3_DSP_RW_PORT,
2353 test_pat[chan][i]);
2354
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002355 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0202);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356 if (tg3_wait_macro_done(tp)) {
2357 *resetp = 1;
2358 return -EBUSY;
2359 }
2360
2361 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
2362 (chan * 0x2000) | 0x0200);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002363 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0082);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364 if (tg3_wait_macro_done(tp)) {
2365 *resetp = 1;
2366 return -EBUSY;
2367 }
2368
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002369 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0802);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370 if (tg3_wait_macro_done(tp)) {
2371 *resetp = 1;
2372 return -EBUSY;
2373 }
2374
2375 for (i = 0; i < 6; i += 2) {
2376 u32 low, high;
2377
2378 if (tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &low) ||
2379 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &high) ||
2380 tg3_wait_macro_done(tp)) {
2381 *resetp = 1;
2382 return -EBUSY;
2383 }
2384 low &= 0x7fff;
2385 high &= 0x000f;
2386 if (low != test_pat[chan][i] ||
2387 high != test_pat[chan][i+1]) {
2388 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x000b);
2389 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x4001);
2390 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x4005);
2391
2392 return -EBUSY;
2393 }
2394 }
2395 }
2396
2397 return 0;
2398}
2399
2400static int tg3_phy_reset_chanpat(struct tg3 *tp)
2401{
2402 int chan;
2403
2404 for (chan = 0; chan < 4; chan++) {
2405 int i;
2406
2407 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
2408 (chan * 0x2000) | 0x0200);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002409 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0002);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410 for (i = 0; i < 6; i++)
2411 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x000);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002412 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0202);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413 if (tg3_wait_macro_done(tp))
2414 return -EBUSY;
2415 }
2416
2417 return 0;
2418}
2419
2420static int tg3_phy_reset_5703_4_5(struct tg3 *tp)
2421{
2422 u32 reg32, phy9_orig;
2423 int retries, do_phy_reset, err;
2424
2425 retries = 10;
2426 do_phy_reset = 1;
2427 do {
2428 if (do_phy_reset) {
2429 err = tg3_bmcr_reset(tp);
2430 if (err)
2431 return err;
2432 do_phy_reset = 0;
2433 }
2434
2435 /* Disable transmitter and interrupt. */
2436 if (tg3_readphy(tp, MII_TG3_EXT_CTRL, &reg32))
2437 continue;
2438
2439 reg32 |= 0x3000;
2440 tg3_writephy(tp, MII_TG3_EXT_CTRL, reg32);
2441
2442 /* Set full-duplex, 1000 mbps. */
2443 tg3_writephy(tp, MII_BMCR,
Matt Carlson221c5632011-06-13 13:39:01 +00002444 BMCR_FULLDPLX | BMCR_SPEED1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445
2446 /* Set to master mode. */
Matt Carlson221c5632011-06-13 13:39:01 +00002447 if (tg3_readphy(tp, MII_CTRL1000, &phy9_orig))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448 continue;
2449
Matt Carlson221c5632011-06-13 13:39:01 +00002450 tg3_writephy(tp, MII_CTRL1000,
2451 CTL1000_AS_MASTER | CTL1000_ENABLE_MASTER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002452
Matt Carlson1d36ba42011-04-20 07:57:42 +00002453 err = TG3_PHY_AUXCTL_SMDSP_ENABLE(tp);
2454 if (err)
2455 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456
2457 /* Block the PHY control access. */
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00002458 tg3_phydsp_write(tp, 0x8005, 0x0800);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459
2460 err = tg3_phy_write_and_check_testpat(tp, &do_phy_reset);
2461 if (!err)
2462 break;
2463 } while (--retries);
2464
2465 err = tg3_phy_reset_chanpat(tp);
2466 if (err)
2467 return err;
2468
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00002469 tg3_phydsp_write(tp, 0x8005, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470
2471 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x8200);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002472 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473
Matt Carlson1d36ba42011-04-20 07:57:42 +00002474 TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475
Matt Carlson221c5632011-06-13 13:39:01 +00002476 tg3_writephy(tp, MII_CTRL1000, phy9_orig);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477
2478 if (!tg3_readphy(tp, MII_TG3_EXT_CTRL, &reg32)) {
2479 reg32 &= ~0x3000;
2480 tg3_writephy(tp, MII_TG3_EXT_CTRL, reg32);
2481 } else if (!err)
2482 err = -EBUSY;
2483
2484 return err;
2485}
2486
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00002487static void tg3_carrier_on(struct tg3 *tp)
2488{
2489 netif_carrier_on(tp->dev);
2490 tp->link_up = true;
2491}
2492
2493static void tg3_carrier_off(struct tg3 *tp)
2494{
2495 netif_carrier_off(tp->dev);
2496 tp->link_up = false;
2497}
2498
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499/* This will reset the tigon3 PHY if there is no valid
2500 * link unless the FORCE argument is non-zero.
2501 */
2502static int tg3_phy_reset(struct tg3 *tp)
2503{
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002504 u32 val, cpmuctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505 int err;
2506
Michael Chan60189dd2006-12-17 17:08:07 -08002507 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan60189dd2006-12-17 17:08:07 -08002508 val = tr32(GRC_MISC_CFG);
2509 tw32_f(GRC_MISC_CFG, val & ~GRC_MISC_CFG_EPHY_IDDQ);
2510 udelay(40);
2511 }
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002512 err = tg3_readphy(tp, MII_BMSR, &val);
2513 err |= tg3_readphy(tp, MII_BMSR, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514 if (err != 0)
2515 return -EBUSY;
2516
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00002517 if (netif_running(tp->dev) && tp->link_up) {
2518 tg3_carrier_off(tp);
Michael Chanc8e1e822006-04-29 18:55:17 -07002519 tg3_link_report(tp);
2520 }
2521
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
2523 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
2524 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
2525 err = tg3_phy_reset_5703_4_5(tp);
2526 if (err)
2527 return err;
2528 goto out;
2529 }
2530
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002531 cpmuctrl = 0;
2532 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
2533 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX) {
2534 cpmuctrl = tr32(TG3_CPMU_CTRL);
2535 if (cpmuctrl & CPMU_CTRL_GPHY_10MB_RXONLY)
2536 tw32(TG3_CPMU_CTRL,
2537 cpmuctrl & ~CPMU_CTRL_GPHY_10MB_RXONLY);
2538 }
2539
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540 err = tg3_bmcr_reset(tp);
2541 if (err)
2542 return err;
2543
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002544 if (cpmuctrl & CPMU_CTRL_GPHY_10MB_RXONLY) {
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002545 val = MII_TG3_DSP_EXP8_AEDW | MII_TG3_DSP_EXP8_REJ2MHz;
2546 tg3_phydsp_write(tp, MII_TG3_DSP_EXP8, val);
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002547
2548 tw32(TG3_CPMU_CTRL, cpmuctrl);
2549 }
2550
Matt Carlsonbcb37f62008-11-03 16:52:09 -08002551 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX ||
2552 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5761_AX) {
Matt Carlsonce057f02007-11-12 21:08:03 -08002553 val = tr32(TG3_CPMU_LSPD_1000MB_CLK);
2554 if ((val & CPMU_LSPD_1000MB_MACCLK_MASK) ==
2555 CPMU_LSPD_1000MB_MACCLK_12_5) {
2556 val &= ~CPMU_LSPD_1000MB_MACCLK_MASK;
2557 udelay(40);
2558 tw32_f(TG3_CPMU_LSPD_1000MB_CLK, val);
2559 }
2560 }
2561
Joe Perches63c3a662011-04-26 08:12:10 +00002562 if (tg3_flag(tp, 5717_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002563 (tp->phy_flags & TG3_PHYFLG_MII_SERDES))
Matt Carlsonecf14102010-01-20 16:58:05 +00002564 return 0;
2565
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002566 tg3_phy_apply_otp(tp);
2567
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002568 if (tp->phy_flags & TG3_PHYFLG_ENABLE_APD)
Matt Carlson6833c042008-11-21 17:18:59 -08002569 tg3_phy_toggle_apd(tp, true);
2570 else
2571 tg3_phy_toggle_apd(tp, false);
2572
Linus Torvalds1da177e2005-04-16 15:20:36 -07002573out:
Matt Carlson1d36ba42011-04-20 07:57:42 +00002574 if ((tp->phy_flags & TG3_PHYFLG_ADC_BUG) &&
2575 !TG3_PHY_AUXCTL_SMDSP_ENABLE(tp)) {
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00002576 tg3_phydsp_write(tp, 0x201f, 0x2aaa);
2577 tg3_phydsp_write(tp, 0x000a, 0x0323);
Matt Carlson1d36ba42011-04-20 07:57:42 +00002578 TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579 }
Matt Carlson1d36ba42011-04-20 07:57:42 +00002580
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002581 if (tp->phy_flags & TG3_PHYFLG_5704_A0_BUG) {
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002582 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8d68);
2583 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8d68);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002584 }
Matt Carlson1d36ba42011-04-20 07:57:42 +00002585
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002586 if (tp->phy_flags & TG3_PHYFLG_BER_BUG) {
Matt Carlson1d36ba42011-04-20 07:57:42 +00002587 if (!TG3_PHY_AUXCTL_SMDSP_ENABLE(tp)) {
2588 tg3_phydsp_write(tp, 0x000a, 0x310b);
2589 tg3_phydsp_write(tp, 0x201f, 0x9506);
2590 tg3_phydsp_write(tp, 0x401f, 0x14e2);
2591 TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
2592 }
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002593 } else if (tp->phy_flags & TG3_PHYFLG_JITTER_BUG) {
Matt Carlson1d36ba42011-04-20 07:57:42 +00002594 if (!TG3_PHY_AUXCTL_SMDSP_ENABLE(tp)) {
2595 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x000a);
2596 if (tp->phy_flags & TG3_PHYFLG_ADJUST_TRIM) {
2597 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x110b);
2598 tg3_writephy(tp, MII_TG3_TEST1,
2599 MII_TG3_TEST1_TRIM_EN | 0x4);
2600 } else
2601 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x010b);
2602
2603 TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
2604 }
Michael Chanc424cb22006-04-29 18:56:34 -07002605 }
Matt Carlson1d36ba42011-04-20 07:57:42 +00002606
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607 /* Set Extended packet length bit (bit 14) on all chips that */
2608 /* support jumbo frames */
Matt Carlson79eb6902010-02-17 15:17:03 +00002609 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610 /* Cannot do read-modify-write on 5401 */
Matt Carlsonb4bd2922011-04-20 07:57:41 +00002611 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_AUXCTL, 0x4c20);
Joe Perches63c3a662011-04-26 08:12:10 +00002612 } else if (tg3_flag(tp, JUMBO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613 /* Set bit 14 with read-modify-write to preserve other bits */
Matt Carlson15ee95c2011-04-20 07:57:40 +00002614 err = tg3_phy_auxctl_read(tp,
2615 MII_TG3_AUXCTL_SHDWSEL_AUXCTL, &val);
2616 if (!err)
Matt Carlsonb4bd2922011-04-20 07:57:41 +00002617 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_AUXCTL,
2618 val | MII_TG3_AUXCTL_ACTL_EXTPKTLEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002619 }
2620
2621 /* Set phy register 0x10 bit 0 to high fifo elasticity to support
2622 * jumbo frames transmission.
2623 */
Joe Perches63c3a662011-04-26 08:12:10 +00002624 if (tg3_flag(tp, JUMBO_CAPABLE)) {
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002625 if (!tg3_readphy(tp, MII_TG3_EXT_CTRL, &val))
Matt Carlsonc6cdf432010-04-05 10:19:26 +00002626 tg3_writephy(tp, MII_TG3_EXT_CTRL,
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002627 val | MII_TG3_EXT_CTRL_FIFO_ELASTIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002628 }
2629
Michael Chan715116a2006-09-27 16:09:25 -07002630 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan715116a2006-09-27 16:09:25 -07002631 /* adjust output voltage */
Matt Carlson535ef6e2009-08-25 10:09:36 +00002632 tg3_writephy(tp, MII_TG3_FET_PTEST, 0x12);
Michael Chan715116a2006-09-27 16:09:25 -07002633 }
2634
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002635 tg3_phy_toggle_automdix(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002636 tg3_phy_set_wirespeed(tp);
2637 return 0;
2638}
2639
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002640#define TG3_GPIO_MSG_DRVR_PRES 0x00000001
2641#define TG3_GPIO_MSG_NEED_VAUX 0x00000002
2642#define TG3_GPIO_MSG_MASK (TG3_GPIO_MSG_DRVR_PRES | \
2643 TG3_GPIO_MSG_NEED_VAUX)
2644#define TG3_GPIO_MSG_ALL_DRVR_PRES_MASK \
2645 ((TG3_GPIO_MSG_DRVR_PRES << 0) | \
2646 (TG3_GPIO_MSG_DRVR_PRES << 4) | \
2647 (TG3_GPIO_MSG_DRVR_PRES << 8) | \
2648 (TG3_GPIO_MSG_DRVR_PRES << 12))
2649
2650#define TG3_GPIO_MSG_ALL_NEED_VAUX_MASK \
2651 ((TG3_GPIO_MSG_NEED_VAUX << 0) | \
2652 (TG3_GPIO_MSG_NEED_VAUX << 4) | \
2653 (TG3_GPIO_MSG_NEED_VAUX << 8) | \
2654 (TG3_GPIO_MSG_NEED_VAUX << 12))
2655
2656static inline u32 tg3_set_function_status(struct tg3 *tp, u32 newstat)
2657{
2658 u32 status, shift;
2659
2660 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
2661 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
2662 status = tg3_ape_read32(tp, TG3_APE_GPIO_MSG);
2663 else
2664 status = tr32(TG3_CPMU_DRV_STATUS);
2665
2666 shift = TG3_APE_GPIO_MSG_SHIFT + 4 * tp->pci_fn;
2667 status &= ~(TG3_GPIO_MSG_MASK << shift);
2668 status |= (newstat << shift);
2669
2670 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
2671 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
2672 tg3_ape_write32(tp, TG3_APE_GPIO_MSG, status);
2673 else
2674 tw32(TG3_CPMU_DRV_STATUS, status);
2675
2676 return status >> TG3_APE_GPIO_MSG_SHIFT;
2677}
2678
Matt Carlson520b2752011-06-13 13:39:02 +00002679static inline int tg3_pwrsrc_switch_to_vmain(struct tg3 *tp)
2680{
2681 if (!tg3_flag(tp, IS_NIC))
2682 return 0;
2683
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002684 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
2685 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
2686 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
2687 if (tg3_ape_lock(tp, TG3_APE_LOCK_GPIO))
2688 return -EIO;
Matt Carlson520b2752011-06-13 13:39:02 +00002689
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002690 tg3_set_function_status(tp, TG3_GPIO_MSG_DRVR_PRES);
2691
2692 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl,
2693 TG3_GRC_LCLCTL_PWRSW_DELAY);
2694
2695 tg3_ape_unlock(tp, TG3_APE_LOCK_GPIO);
2696 } else {
2697 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl,
2698 TG3_GRC_LCLCTL_PWRSW_DELAY);
2699 }
Matt Carlson6f5c8f832011-07-13 09:27:31 +00002700
Matt Carlson520b2752011-06-13 13:39:02 +00002701 return 0;
2702}
2703
2704static void tg3_pwrsrc_die_with_vmain(struct tg3 *tp)
2705{
2706 u32 grc_local_ctrl;
2707
2708 if (!tg3_flag(tp, IS_NIC) ||
2709 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2710 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)
2711 return;
2712
2713 grc_local_ctrl = tp->grc_local_ctrl | GRC_LCLCTRL_GPIO_OE1;
2714
2715 tw32_wait_f(GRC_LOCAL_CTRL,
2716 grc_local_ctrl | GRC_LCLCTRL_GPIO_OUTPUT1,
2717 TG3_GRC_LCLCTL_PWRSW_DELAY);
2718
2719 tw32_wait_f(GRC_LOCAL_CTRL,
2720 grc_local_ctrl,
2721 TG3_GRC_LCLCTL_PWRSW_DELAY);
2722
2723 tw32_wait_f(GRC_LOCAL_CTRL,
2724 grc_local_ctrl | GRC_LCLCTRL_GPIO_OUTPUT1,
2725 TG3_GRC_LCLCTL_PWRSW_DELAY);
2726}
2727
2728static void tg3_pwrsrc_switch_to_vaux(struct tg3 *tp)
2729{
2730 if (!tg3_flag(tp, IS_NIC))
2731 return;
2732
2733 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2734 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
2735 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2736 (GRC_LCLCTRL_GPIO_OE0 |
2737 GRC_LCLCTRL_GPIO_OE1 |
2738 GRC_LCLCTRL_GPIO_OE2 |
2739 GRC_LCLCTRL_GPIO_OUTPUT0 |
2740 GRC_LCLCTRL_GPIO_OUTPUT1),
2741 TG3_GRC_LCLCTL_PWRSW_DELAY);
2742 } else if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761 ||
2743 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761S) {
2744 /* The 5761 non-e device swaps GPIO 0 and GPIO 2. */
2745 u32 grc_local_ctrl = GRC_LCLCTRL_GPIO_OE0 |
2746 GRC_LCLCTRL_GPIO_OE1 |
2747 GRC_LCLCTRL_GPIO_OE2 |
2748 GRC_LCLCTRL_GPIO_OUTPUT0 |
2749 GRC_LCLCTRL_GPIO_OUTPUT1 |
2750 tp->grc_local_ctrl;
2751 tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl,
2752 TG3_GRC_LCLCTL_PWRSW_DELAY);
2753
2754 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OUTPUT2;
2755 tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl,
2756 TG3_GRC_LCLCTL_PWRSW_DELAY);
2757
2758 grc_local_ctrl &= ~GRC_LCLCTRL_GPIO_OUTPUT0;
2759 tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl,
2760 TG3_GRC_LCLCTL_PWRSW_DELAY);
2761 } else {
2762 u32 no_gpio2;
2763 u32 grc_local_ctrl = 0;
2764
2765 /* Workaround to prevent overdrawing Amps. */
2766 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
2767 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE3;
2768 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2769 grc_local_ctrl,
2770 TG3_GRC_LCLCTL_PWRSW_DELAY);
2771 }
2772
2773 /* On 5753 and variants, GPIO2 cannot be used. */
2774 no_gpio2 = tp->nic_sram_data_cfg &
2775 NIC_SRAM_DATA_CFG_NO_GPIO2;
2776
2777 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE0 |
2778 GRC_LCLCTRL_GPIO_OE1 |
2779 GRC_LCLCTRL_GPIO_OE2 |
2780 GRC_LCLCTRL_GPIO_OUTPUT1 |
2781 GRC_LCLCTRL_GPIO_OUTPUT2;
2782 if (no_gpio2) {
2783 grc_local_ctrl &= ~(GRC_LCLCTRL_GPIO_OE2 |
2784 GRC_LCLCTRL_GPIO_OUTPUT2);
2785 }
2786 tw32_wait_f(GRC_LOCAL_CTRL,
2787 tp->grc_local_ctrl | grc_local_ctrl,
2788 TG3_GRC_LCLCTL_PWRSW_DELAY);
2789
2790 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OUTPUT0;
2791
2792 tw32_wait_f(GRC_LOCAL_CTRL,
2793 tp->grc_local_ctrl | grc_local_ctrl,
2794 TG3_GRC_LCLCTL_PWRSW_DELAY);
2795
2796 if (!no_gpio2) {
2797 grc_local_ctrl &= ~GRC_LCLCTRL_GPIO_OUTPUT2;
2798 tw32_wait_f(GRC_LOCAL_CTRL,
2799 tp->grc_local_ctrl | grc_local_ctrl,
2800 TG3_GRC_LCLCTL_PWRSW_DELAY);
2801 }
2802 }
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002803}
Matt Carlson6f5c8f832011-07-13 09:27:31 +00002804
Matt Carlsoncd0d7222011-07-13 09:27:33 +00002805static void tg3_frob_aux_power_5717(struct tg3 *tp, bool wol_enable)
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002806{
2807 u32 msg = 0;
2808
2809 /* Serialize power state transitions */
2810 if (tg3_ape_lock(tp, TG3_APE_LOCK_GPIO))
2811 return;
2812
Matt Carlsoncd0d7222011-07-13 09:27:33 +00002813 if (tg3_flag(tp, ENABLE_ASF) || tg3_flag(tp, ENABLE_APE) || wol_enable)
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002814 msg = TG3_GPIO_MSG_NEED_VAUX;
2815
2816 msg = tg3_set_function_status(tp, msg);
2817
2818 if (msg & TG3_GPIO_MSG_ALL_DRVR_PRES_MASK)
2819 goto done;
2820
2821 if (msg & TG3_GPIO_MSG_ALL_NEED_VAUX_MASK)
2822 tg3_pwrsrc_switch_to_vaux(tp);
2823 else
2824 tg3_pwrsrc_die_with_vmain(tp);
2825
2826done:
Matt Carlson6f5c8f832011-07-13 09:27:31 +00002827 tg3_ape_unlock(tp, TG3_APE_LOCK_GPIO);
Matt Carlson520b2752011-06-13 13:39:02 +00002828}
2829
Matt Carlsoncd0d7222011-07-13 09:27:33 +00002830static void tg3_frob_aux_power(struct tg3 *tp, bool include_wol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002831{
Matt Carlson683644b2011-03-09 16:58:23 +00002832 bool need_vaux = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002833
Matt Carlson334355a2010-01-20 16:58:10 +00002834 /* The GPIOs do something completely different on 57765. */
Matt Carlson55086ad2011-12-14 11:09:59 +00002835 if (!tg3_flag(tp, IS_NIC) || tg3_flag(tp, 57765_CLASS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836 return;
2837
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002838 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
2839 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
2840 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
Matt Carlsoncd0d7222011-07-13 09:27:33 +00002841 tg3_frob_aux_power_5717(tp, include_wol ?
2842 tg3_flag(tp, WOL_ENABLE) != 0 : 0);
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002843 return;
2844 }
2845
2846 if (tp->pdev_peer && tp->pdev_peer != tp->pdev) {
Michael Chan8c2dc7e2005-12-19 16:26:02 -08002847 struct net_device *dev_peer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848
Michael Chan8c2dc7e2005-12-19 16:26:02 -08002849 dev_peer = pci_get_drvdata(tp->pdev_peer);
Matt Carlson683644b2011-03-09 16:58:23 +00002850
Michael Chanbc1c7562006-03-20 17:48:03 -08002851 /* remove_one() may have been run on the peer. */
Matt Carlson683644b2011-03-09 16:58:23 +00002852 if (dev_peer) {
2853 struct tg3 *tp_peer = netdev_priv(dev_peer);
2854
Joe Perches63c3a662011-04-26 08:12:10 +00002855 if (tg3_flag(tp_peer, INIT_COMPLETE))
Matt Carlson683644b2011-03-09 16:58:23 +00002856 return;
2857
Matt Carlsoncd0d7222011-07-13 09:27:33 +00002858 if ((include_wol && tg3_flag(tp_peer, WOL_ENABLE)) ||
Joe Perches63c3a662011-04-26 08:12:10 +00002859 tg3_flag(tp_peer, ENABLE_ASF))
Matt Carlson683644b2011-03-09 16:58:23 +00002860 need_vaux = true;
2861 }
Michael Chan8c2dc7e2005-12-19 16:26:02 -08002862 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863
Matt Carlsoncd0d7222011-07-13 09:27:33 +00002864 if ((include_wol && tg3_flag(tp, WOL_ENABLE)) ||
2865 tg3_flag(tp, ENABLE_ASF))
Matt Carlson683644b2011-03-09 16:58:23 +00002866 need_vaux = true;
2867
Matt Carlson520b2752011-06-13 13:39:02 +00002868 if (need_vaux)
2869 tg3_pwrsrc_switch_to_vaux(tp);
2870 else
2871 tg3_pwrsrc_die_with_vmain(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002872}
2873
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07002874static int tg3_5700_link_polarity(struct tg3 *tp, u32 speed)
2875{
2876 if (tp->led_ctrl == LED_CTRL_MODE_PHY_2)
2877 return 1;
Matt Carlson79eb6902010-02-17 15:17:03 +00002878 else if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5411) {
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07002879 if (speed != SPEED_10)
2880 return 1;
2881 } else if (speed == SPEED_10)
2882 return 1;
2883
2884 return 0;
2885}
2886
Matt Carlson0a459aa2008-11-03 16:54:15 -08002887static void tg3_power_down_phy(struct tg3 *tp, bool do_low_power)
Michael Chan15c3b692006-03-22 01:06:52 -08002888{
Matt Carlsonce057f02007-11-12 21:08:03 -08002889 u32 val;
2890
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002891 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Michael Chan51297242007-02-13 12:17:57 -08002892 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
2893 u32 sg_dig_ctrl = tr32(SG_DIG_CTRL);
2894 u32 serdes_cfg = tr32(MAC_SERDES_CFG);
2895
2896 sg_dig_ctrl |=
2897 SG_DIG_USING_HW_AUTONEG | SG_DIG_SOFT_RESET;
2898 tw32(SG_DIG_CTRL, sg_dig_ctrl);
2899 tw32(MAC_SERDES_CFG, serdes_cfg | (1 << 15));
2900 }
Michael Chan3f7045c2006-09-27 16:02:29 -07002901 return;
Michael Chan51297242007-02-13 12:17:57 -08002902 }
Michael Chan3f7045c2006-09-27 16:02:29 -07002903
Michael Chan60189dd2006-12-17 17:08:07 -08002904 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan60189dd2006-12-17 17:08:07 -08002905 tg3_bmcr_reset(tp);
2906 val = tr32(GRC_MISC_CFG);
2907 tw32_f(GRC_MISC_CFG, val | GRC_MISC_CFG_EPHY_IDDQ);
2908 udelay(40);
2909 return;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002910 } else if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Matt Carlson0e5f7842009-11-02 14:26:38 +00002911 u32 phytest;
2912 if (!tg3_readphy(tp, MII_TG3_FET_TEST, &phytest)) {
2913 u32 phy;
2914
2915 tg3_writephy(tp, MII_ADVERTISE, 0);
2916 tg3_writephy(tp, MII_BMCR,
2917 BMCR_ANENABLE | BMCR_ANRESTART);
2918
2919 tg3_writephy(tp, MII_TG3_FET_TEST,
2920 phytest | MII_TG3_FET_SHADOW_EN);
2921 if (!tg3_readphy(tp, MII_TG3_FET_SHDW_AUXMODE4, &phy)) {
2922 phy |= MII_TG3_FET_SHDW_AUXMODE4_SBPD;
2923 tg3_writephy(tp,
2924 MII_TG3_FET_SHDW_AUXMODE4,
2925 phy);
2926 }
2927 tg3_writephy(tp, MII_TG3_FET_TEST, phytest);
2928 }
2929 return;
Matt Carlson0a459aa2008-11-03 16:54:15 -08002930 } else if (do_low_power) {
Michael Chan715116a2006-09-27 16:09:25 -07002931 tg3_writephy(tp, MII_TG3_EXT_CTRL,
2932 MII_TG3_EXT_CTRL_FORCE_LED_OFF);
Matt Carlson0a459aa2008-11-03 16:54:15 -08002933
Matt Carlsonb4bd2922011-04-20 07:57:41 +00002934 val = MII_TG3_AUXCTL_PCTL_100TX_LPWR |
2935 MII_TG3_AUXCTL_PCTL_SPR_ISOLATE |
2936 MII_TG3_AUXCTL_PCTL_VREG_11V;
2937 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_PWRCTL, val);
Michael Chan715116a2006-09-27 16:09:25 -07002938 }
Michael Chan3f7045c2006-09-27 16:02:29 -07002939
Michael Chan15c3b692006-03-22 01:06:52 -08002940 /* The PHY should not be powered down on some chips because
2941 * of bugs.
2942 */
2943 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2944 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
2945 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780 &&
Matt Carlson085f1af2012-04-02 09:01:40 +00002946 (tp->phy_flags & TG3_PHYFLG_MII_SERDES)) ||
2947 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 &&
2948 !tp->pci_fn))
Michael Chan15c3b692006-03-22 01:06:52 -08002949 return;
Matt Carlsonce057f02007-11-12 21:08:03 -08002950
Matt Carlsonbcb37f62008-11-03 16:52:09 -08002951 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX ||
2952 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5761_AX) {
Matt Carlsonce057f02007-11-12 21:08:03 -08002953 val = tr32(TG3_CPMU_LSPD_1000MB_CLK);
2954 val &= ~CPMU_LSPD_1000MB_MACCLK_MASK;
2955 val |= CPMU_LSPD_1000MB_MACCLK_12_5;
2956 tw32_f(TG3_CPMU_LSPD_1000MB_CLK, val);
2957 }
2958
Michael Chan15c3b692006-03-22 01:06:52 -08002959 tg3_writephy(tp, MII_BMCR, BMCR_PDOWN);
2960}
2961
Matt Carlson3f007892008-11-03 16:51:36 -08002962/* tp->lock is held. */
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002963static int tg3_nvram_lock(struct tg3 *tp)
2964{
Joe Perches63c3a662011-04-26 08:12:10 +00002965 if (tg3_flag(tp, NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002966 int i;
2967
2968 if (tp->nvram_lock_cnt == 0) {
2969 tw32(NVRAM_SWARB, SWARB_REQ_SET1);
2970 for (i = 0; i < 8000; i++) {
2971 if (tr32(NVRAM_SWARB) & SWARB_GNT1)
2972 break;
2973 udelay(20);
2974 }
2975 if (i == 8000) {
2976 tw32(NVRAM_SWARB, SWARB_REQ_CLR1);
2977 return -ENODEV;
2978 }
2979 }
2980 tp->nvram_lock_cnt++;
2981 }
2982 return 0;
2983}
2984
2985/* tp->lock is held. */
2986static void tg3_nvram_unlock(struct tg3 *tp)
2987{
Joe Perches63c3a662011-04-26 08:12:10 +00002988 if (tg3_flag(tp, NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002989 if (tp->nvram_lock_cnt > 0)
2990 tp->nvram_lock_cnt--;
2991 if (tp->nvram_lock_cnt == 0)
2992 tw32_f(NVRAM_SWARB, SWARB_REQ_CLR1);
2993 }
2994}
2995
2996/* tp->lock is held. */
2997static void tg3_enable_nvram_access(struct tg3 *tp)
2998{
Joe Perches63c3a662011-04-26 08:12:10 +00002999 if (tg3_flag(tp, 5750_PLUS) && !tg3_flag(tp, PROTECTED_NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003000 u32 nvaccess = tr32(NVRAM_ACCESS);
3001
3002 tw32(NVRAM_ACCESS, nvaccess | ACCESS_ENABLE);
3003 }
3004}
3005
3006/* tp->lock is held. */
3007static void tg3_disable_nvram_access(struct tg3 *tp)
3008{
Joe Perches63c3a662011-04-26 08:12:10 +00003009 if (tg3_flag(tp, 5750_PLUS) && !tg3_flag(tp, PROTECTED_NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003010 u32 nvaccess = tr32(NVRAM_ACCESS);
3011
3012 tw32(NVRAM_ACCESS, nvaccess & ~ACCESS_ENABLE);
3013 }
3014}
3015
3016static int tg3_nvram_read_using_eeprom(struct tg3 *tp,
3017 u32 offset, u32 *val)
3018{
3019 u32 tmp;
3020 int i;
3021
3022 if (offset > EEPROM_ADDR_ADDR_MASK || (offset % 4) != 0)
3023 return -EINVAL;
3024
3025 tmp = tr32(GRC_EEPROM_ADDR) & ~(EEPROM_ADDR_ADDR_MASK |
3026 EEPROM_ADDR_DEVID_MASK |
3027 EEPROM_ADDR_READ);
3028 tw32(GRC_EEPROM_ADDR,
3029 tmp |
3030 (0 << EEPROM_ADDR_DEVID_SHIFT) |
3031 ((offset << EEPROM_ADDR_ADDR_SHIFT) &
3032 EEPROM_ADDR_ADDR_MASK) |
3033 EEPROM_ADDR_READ | EEPROM_ADDR_START);
3034
3035 for (i = 0; i < 1000; i++) {
3036 tmp = tr32(GRC_EEPROM_ADDR);
3037
3038 if (tmp & EEPROM_ADDR_COMPLETE)
3039 break;
3040 msleep(1);
3041 }
3042 if (!(tmp & EEPROM_ADDR_COMPLETE))
3043 return -EBUSY;
3044
Matt Carlson62cedd12009-04-20 14:52:29 -07003045 tmp = tr32(GRC_EEPROM_DATA);
3046
3047 /*
3048 * The data will always be opposite the native endian
3049 * format. Perform a blind byteswap to compensate.
3050 */
3051 *val = swab32(tmp);
3052
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003053 return 0;
3054}
3055
3056#define NVRAM_CMD_TIMEOUT 10000
3057
3058static int tg3_nvram_exec_cmd(struct tg3 *tp, u32 nvram_cmd)
3059{
3060 int i;
3061
3062 tw32(NVRAM_CMD, nvram_cmd);
3063 for (i = 0; i < NVRAM_CMD_TIMEOUT; i++) {
3064 udelay(10);
3065 if (tr32(NVRAM_CMD) & NVRAM_CMD_DONE) {
3066 udelay(10);
3067 break;
3068 }
3069 }
3070
3071 if (i == NVRAM_CMD_TIMEOUT)
3072 return -EBUSY;
3073
3074 return 0;
3075}
3076
3077static u32 tg3_nvram_phys_addr(struct tg3 *tp, u32 addr)
3078{
Joe Perches63c3a662011-04-26 08:12:10 +00003079 if (tg3_flag(tp, NVRAM) &&
3080 tg3_flag(tp, NVRAM_BUFFERED) &&
3081 tg3_flag(tp, FLASH) &&
3082 !tg3_flag(tp, NO_NVRAM_ADDR_TRANS) &&
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003083 (tp->nvram_jedecnum == JEDEC_ATMEL))
3084
3085 addr = ((addr / tp->nvram_pagesize) <<
3086 ATMEL_AT45DB0X1B_PAGE_POS) +
3087 (addr % tp->nvram_pagesize);
3088
3089 return addr;
3090}
3091
3092static u32 tg3_nvram_logical_addr(struct tg3 *tp, u32 addr)
3093{
Joe Perches63c3a662011-04-26 08:12:10 +00003094 if (tg3_flag(tp, NVRAM) &&
3095 tg3_flag(tp, NVRAM_BUFFERED) &&
3096 tg3_flag(tp, FLASH) &&
3097 !tg3_flag(tp, NO_NVRAM_ADDR_TRANS) &&
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003098 (tp->nvram_jedecnum == JEDEC_ATMEL))
3099
3100 addr = ((addr >> ATMEL_AT45DB0X1B_PAGE_POS) *
3101 tp->nvram_pagesize) +
3102 (addr & ((1 << ATMEL_AT45DB0X1B_PAGE_POS) - 1));
3103
3104 return addr;
3105}
3106
Matt Carlsone4f34112009-02-25 14:25:00 +00003107/* NOTE: Data read in from NVRAM is byteswapped according to
3108 * the byteswapping settings for all other register accesses.
3109 * tg3 devices are BE devices, so on a BE machine, the data
3110 * returned will be exactly as it is seen in NVRAM. On a LE
3111 * machine, the 32-bit value will be byteswapped.
3112 */
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003113static int tg3_nvram_read(struct tg3 *tp, u32 offset, u32 *val)
3114{
3115 int ret;
3116
Joe Perches63c3a662011-04-26 08:12:10 +00003117 if (!tg3_flag(tp, NVRAM))
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003118 return tg3_nvram_read_using_eeprom(tp, offset, val);
3119
3120 offset = tg3_nvram_phys_addr(tp, offset);
3121
3122 if (offset > NVRAM_ADDR_MSK)
3123 return -EINVAL;
3124
3125 ret = tg3_nvram_lock(tp);
3126 if (ret)
3127 return ret;
3128
3129 tg3_enable_nvram_access(tp);
3130
3131 tw32(NVRAM_ADDR, offset);
3132 ret = tg3_nvram_exec_cmd(tp, NVRAM_CMD_RD | NVRAM_CMD_GO |
3133 NVRAM_CMD_FIRST | NVRAM_CMD_LAST | NVRAM_CMD_DONE);
3134
3135 if (ret == 0)
Matt Carlsone4f34112009-02-25 14:25:00 +00003136 *val = tr32(NVRAM_RDDATA);
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003137
3138 tg3_disable_nvram_access(tp);
3139
3140 tg3_nvram_unlock(tp);
3141
3142 return ret;
3143}
3144
Matt Carlsona9dc5292009-02-25 14:25:30 +00003145/* Ensures NVRAM data is in bytestream format. */
3146static int tg3_nvram_read_be32(struct tg3 *tp, u32 offset, __be32 *val)
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003147{
3148 u32 v;
Matt Carlsona9dc5292009-02-25 14:25:30 +00003149 int res = tg3_nvram_read(tp, offset, &v);
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003150 if (!res)
Matt Carlsona9dc5292009-02-25 14:25:30 +00003151 *val = cpu_to_be32(v);
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003152 return res;
3153}
3154
Matt Carlsondbe9b922012-02-13 10:20:09 +00003155static int tg3_nvram_write_block_using_eeprom(struct tg3 *tp,
3156 u32 offset, u32 len, u8 *buf)
3157{
3158 int i, j, rc = 0;
3159 u32 val;
3160
3161 for (i = 0; i < len; i += 4) {
3162 u32 addr;
3163 __be32 data;
3164
3165 addr = offset + i;
3166
3167 memcpy(&data, buf + i, 4);
3168
3169 /*
3170 * The SEEPROM interface expects the data to always be opposite
3171 * the native endian format. We accomplish this by reversing
3172 * all the operations that would have been performed on the
3173 * data from a call to tg3_nvram_read_be32().
3174 */
3175 tw32(GRC_EEPROM_DATA, swab32(be32_to_cpu(data)));
3176
3177 val = tr32(GRC_EEPROM_ADDR);
3178 tw32(GRC_EEPROM_ADDR, val | EEPROM_ADDR_COMPLETE);
3179
3180 val &= ~(EEPROM_ADDR_ADDR_MASK | EEPROM_ADDR_DEVID_MASK |
3181 EEPROM_ADDR_READ);
3182 tw32(GRC_EEPROM_ADDR, val |
3183 (0 << EEPROM_ADDR_DEVID_SHIFT) |
3184 (addr & EEPROM_ADDR_ADDR_MASK) |
3185 EEPROM_ADDR_START |
3186 EEPROM_ADDR_WRITE);
3187
3188 for (j = 0; j < 1000; j++) {
3189 val = tr32(GRC_EEPROM_ADDR);
3190
3191 if (val & EEPROM_ADDR_COMPLETE)
3192 break;
3193 msleep(1);
3194 }
3195 if (!(val & EEPROM_ADDR_COMPLETE)) {
3196 rc = -EBUSY;
3197 break;
3198 }
3199 }
3200
3201 return rc;
3202}
3203
3204/* offset and length are dword aligned */
3205static int tg3_nvram_write_block_unbuffered(struct tg3 *tp, u32 offset, u32 len,
3206 u8 *buf)
3207{
3208 int ret = 0;
3209 u32 pagesize = tp->nvram_pagesize;
3210 u32 pagemask = pagesize - 1;
3211 u32 nvram_cmd;
3212 u8 *tmp;
3213
3214 tmp = kmalloc(pagesize, GFP_KERNEL);
3215 if (tmp == NULL)
3216 return -ENOMEM;
3217
3218 while (len) {
3219 int j;
3220 u32 phy_addr, page_off, size;
3221
3222 phy_addr = offset & ~pagemask;
3223
3224 for (j = 0; j < pagesize; j += 4) {
3225 ret = tg3_nvram_read_be32(tp, phy_addr + j,
3226 (__be32 *) (tmp + j));
3227 if (ret)
3228 break;
3229 }
3230 if (ret)
3231 break;
3232
3233 page_off = offset & pagemask;
3234 size = pagesize;
3235 if (len < size)
3236 size = len;
3237
3238 len -= size;
3239
3240 memcpy(tmp + page_off, buf, size);
3241
3242 offset = offset + (pagesize - page_off);
3243
3244 tg3_enable_nvram_access(tp);
3245
3246 /*
3247 * Before we can erase the flash page, we need
3248 * to issue a special "write enable" command.
3249 */
3250 nvram_cmd = NVRAM_CMD_WREN | NVRAM_CMD_GO | NVRAM_CMD_DONE;
3251
3252 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
3253 break;
3254
3255 /* Erase the target page */
3256 tw32(NVRAM_ADDR, phy_addr);
3257
3258 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE | NVRAM_CMD_WR |
3259 NVRAM_CMD_FIRST | NVRAM_CMD_LAST | NVRAM_CMD_ERASE;
3260
3261 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
3262 break;
3263
3264 /* Issue another write enable to start the write. */
3265 nvram_cmd = NVRAM_CMD_WREN | NVRAM_CMD_GO | NVRAM_CMD_DONE;
3266
3267 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
3268 break;
3269
3270 for (j = 0; j < pagesize; j += 4) {
3271 __be32 data;
3272
3273 data = *((__be32 *) (tmp + j));
3274
3275 tw32(NVRAM_WRDATA, be32_to_cpu(data));
3276
3277 tw32(NVRAM_ADDR, phy_addr + j);
3278
3279 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE |
3280 NVRAM_CMD_WR;
3281
3282 if (j == 0)
3283 nvram_cmd |= NVRAM_CMD_FIRST;
3284 else if (j == (pagesize - 4))
3285 nvram_cmd |= NVRAM_CMD_LAST;
3286
3287 ret = tg3_nvram_exec_cmd(tp, nvram_cmd);
3288 if (ret)
3289 break;
3290 }
3291 if (ret)
3292 break;
3293 }
3294
3295 nvram_cmd = NVRAM_CMD_WRDI | NVRAM_CMD_GO | NVRAM_CMD_DONE;
3296 tg3_nvram_exec_cmd(tp, nvram_cmd);
3297
3298 kfree(tmp);
3299
3300 return ret;
3301}
3302
3303/* offset and length are dword aligned */
3304static int tg3_nvram_write_block_buffered(struct tg3 *tp, u32 offset, u32 len,
3305 u8 *buf)
3306{
3307 int i, ret = 0;
3308
3309 for (i = 0; i < len; i += 4, offset += 4) {
3310 u32 page_off, phy_addr, nvram_cmd;
3311 __be32 data;
3312
3313 memcpy(&data, buf + i, 4);
3314 tw32(NVRAM_WRDATA, be32_to_cpu(data));
3315
3316 page_off = offset % tp->nvram_pagesize;
3317
3318 phy_addr = tg3_nvram_phys_addr(tp, offset);
3319
Matt Carlsondbe9b922012-02-13 10:20:09 +00003320 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE | NVRAM_CMD_WR;
3321
3322 if (page_off == 0 || i == 0)
3323 nvram_cmd |= NVRAM_CMD_FIRST;
3324 if (page_off == (tp->nvram_pagesize - 4))
3325 nvram_cmd |= NVRAM_CMD_LAST;
3326
3327 if (i == (len - 4))
3328 nvram_cmd |= NVRAM_CMD_LAST;
3329
Matt Carlson42278222012-02-13 15:20:11 +00003330 if ((nvram_cmd & NVRAM_CMD_FIRST) ||
3331 !tg3_flag(tp, FLASH) ||
3332 !tg3_flag(tp, 57765_PLUS))
3333 tw32(NVRAM_ADDR, phy_addr);
3334
Matt Carlsondbe9b922012-02-13 10:20:09 +00003335 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5752 &&
3336 !tg3_flag(tp, 5755_PLUS) &&
3337 (tp->nvram_jedecnum == JEDEC_ST) &&
3338 (nvram_cmd & NVRAM_CMD_FIRST)) {
3339 u32 cmd;
3340
3341 cmd = NVRAM_CMD_WREN | NVRAM_CMD_GO | NVRAM_CMD_DONE;
3342 ret = tg3_nvram_exec_cmd(tp, cmd);
3343 if (ret)
3344 break;
3345 }
3346 if (!tg3_flag(tp, FLASH)) {
3347 /* We always do complete word writes to eeprom. */
3348 nvram_cmd |= (NVRAM_CMD_FIRST | NVRAM_CMD_LAST);
3349 }
3350
3351 ret = tg3_nvram_exec_cmd(tp, nvram_cmd);
3352 if (ret)
3353 break;
3354 }
3355 return ret;
3356}
3357
3358/* offset and length are dword aligned */
3359static int tg3_nvram_write_block(struct tg3 *tp, u32 offset, u32 len, u8 *buf)
3360{
3361 int ret;
3362
3363 if (tg3_flag(tp, EEPROM_WRITE_PROT)) {
3364 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl &
3365 ~GRC_LCLCTRL_GPIO_OUTPUT1);
3366 udelay(40);
3367 }
3368
3369 if (!tg3_flag(tp, NVRAM)) {
3370 ret = tg3_nvram_write_block_using_eeprom(tp, offset, len, buf);
3371 } else {
3372 u32 grc_mode;
3373
3374 ret = tg3_nvram_lock(tp);
3375 if (ret)
3376 return ret;
3377
3378 tg3_enable_nvram_access(tp);
3379 if (tg3_flag(tp, 5750_PLUS) && !tg3_flag(tp, PROTECTED_NVRAM))
3380 tw32(NVRAM_WRITE1, 0x406);
3381
3382 grc_mode = tr32(GRC_MODE);
3383 tw32(GRC_MODE, grc_mode | GRC_MODE_NVRAM_WR_ENABLE);
3384
3385 if (tg3_flag(tp, NVRAM_BUFFERED) || !tg3_flag(tp, FLASH)) {
3386 ret = tg3_nvram_write_block_buffered(tp, offset, len,
3387 buf);
3388 } else {
3389 ret = tg3_nvram_write_block_unbuffered(tp, offset, len,
3390 buf);
3391 }
3392
3393 grc_mode = tr32(GRC_MODE);
3394 tw32(GRC_MODE, grc_mode & ~GRC_MODE_NVRAM_WR_ENABLE);
3395
3396 tg3_disable_nvram_access(tp);
3397 tg3_nvram_unlock(tp);
3398 }
3399
3400 if (tg3_flag(tp, EEPROM_WRITE_PROT)) {
3401 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
3402 udelay(40);
3403 }
3404
3405 return ret;
3406}
3407
Matt Carlson997b4f12011-08-31 11:44:53 +00003408#define RX_CPU_SCRATCH_BASE 0x30000
3409#define RX_CPU_SCRATCH_SIZE 0x04000
3410#define TX_CPU_SCRATCH_BASE 0x34000
3411#define TX_CPU_SCRATCH_SIZE 0x04000
3412
3413/* tp->lock is held. */
3414static int tg3_halt_cpu(struct tg3 *tp, u32 offset)
3415{
3416 int i;
3417
3418 BUG_ON(offset == TX_CPU_BASE && tg3_flag(tp, 5705_PLUS));
3419
3420 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
3421 u32 val = tr32(GRC_VCPU_EXT_CTRL);
3422
3423 tw32(GRC_VCPU_EXT_CTRL, val | GRC_VCPU_EXT_CTRL_HALT_CPU);
3424 return 0;
3425 }
3426 if (offset == RX_CPU_BASE) {
3427 for (i = 0; i < 10000; i++) {
3428 tw32(offset + CPU_STATE, 0xffffffff);
3429 tw32(offset + CPU_MODE, CPU_MODE_HALT);
3430 if (tr32(offset + CPU_MODE) & CPU_MODE_HALT)
3431 break;
3432 }
3433
3434 tw32(offset + CPU_STATE, 0xffffffff);
3435 tw32_f(offset + CPU_MODE, CPU_MODE_HALT);
3436 udelay(10);
3437 } else {
3438 for (i = 0; i < 10000; i++) {
3439 tw32(offset + CPU_STATE, 0xffffffff);
3440 tw32(offset + CPU_MODE, CPU_MODE_HALT);
3441 if (tr32(offset + CPU_MODE) & CPU_MODE_HALT)
3442 break;
3443 }
3444 }
3445
3446 if (i >= 10000) {
3447 netdev_err(tp->dev, "%s timed out, %s CPU\n",
3448 __func__, offset == RX_CPU_BASE ? "RX" : "TX");
3449 return -ENODEV;
3450 }
3451
3452 /* Clear firmware's nvram arbitration. */
3453 if (tg3_flag(tp, NVRAM))
3454 tw32(NVRAM_SWARB, SWARB_REQ_CLR0);
3455 return 0;
3456}
3457
3458struct fw_info {
3459 unsigned int fw_base;
3460 unsigned int fw_len;
3461 const __be32 *fw_data;
3462};
3463
3464/* tp->lock is held. */
3465static int tg3_load_firmware_cpu(struct tg3 *tp, u32 cpu_base,
3466 u32 cpu_scratch_base, int cpu_scratch_size,
3467 struct fw_info *info)
3468{
3469 int err, lock_err, i;
3470 void (*write_op)(struct tg3 *, u32, u32);
3471
3472 if (cpu_base == TX_CPU_BASE && tg3_flag(tp, 5705_PLUS)) {
3473 netdev_err(tp->dev,
3474 "%s: Trying to load TX cpu firmware which is 5705\n",
3475 __func__);
3476 return -EINVAL;
3477 }
3478
3479 if (tg3_flag(tp, 5705_PLUS))
3480 write_op = tg3_write_mem;
3481 else
3482 write_op = tg3_write_indirect_reg32;
3483
3484 /* It is possible that bootcode is still loading at this point.
3485 * Get the nvram lock first before halting the cpu.
3486 */
3487 lock_err = tg3_nvram_lock(tp);
3488 err = tg3_halt_cpu(tp, cpu_base);
3489 if (!lock_err)
3490 tg3_nvram_unlock(tp);
3491 if (err)
3492 goto out;
3493
3494 for (i = 0; i < cpu_scratch_size; i += sizeof(u32))
3495 write_op(tp, cpu_scratch_base + i, 0);
3496 tw32(cpu_base + CPU_STATE, 0xffffffff);
3497 tw32(cpu_base + CPU_MODE, tr32(cpu_base+CPU_MODE)|CPU_MODE_HALT);
3498 for (i = 0; i < (info->fw_len / sizeof(u32)); i++)
3499 write_op(tp, (cpu_scratch_base +
3500 (info->fw_base & 0xffff) +
3501 (i * sizeof(u32))),
3502 be32_to_cpu(info->fw_data[i]));
3503
3504 err = 0;
3505
3506out:
3507 return err;
3508}
3509
3510/* tp->lock is held. */
3511static int tg3_load_5701_a0_firmware_fix(struct tg3 *tp)
3512{
3513 struct fw_info info;
3514 const __be32 *fw_data;
3515 int err, i;
3516
3517 fw_data = (void *)tp->fw->data;
3518
3519 /* Firmware blob starts with version numbers, followed by
3520 start address and length. We are setting complete length.
3521 length = end_address_of_bss - start_address_of_text.
3522 Remainder is the blob to be loaded contiguously
3523 from start address. */
3524
3525 info.fw_base = be32_to_cpu(fw_data[1]);
3526 info.fw_len = tp->fw->size - 12;
3527 info.fw_data = &fw_data[3];
3528
3529 err = tg3_load_firmware_cpu(tp, RX_CPU_BASE,
3530 RX_CPU_SCRATCH_BASE, RX_CPU_SCRATCH_SIZE,
3531 &info);
3532 if (err)
3533 return err;
3534
3535 err = tg3_load_firmware_cpu(tp, TX_CPU_BASE,
3536 TX_CPU_SCRATCH_BASE, TX_CPU_SCRATCH_SIZE,
3537 &info);
3538 if (err)
3539 return err;
3540
3541 /* Now startup only the RX cpu. */
3542 tw32(RX_CPU_BASE + CPU_STATE, 0xffffffff);
3543 tw32_f(RX_CPU_BASE + CPU_PC, info.fw_base);
3544
3545 for (i = 0; i < 5; i++) {
3546 if (tr32(RX_CPU_BASE + CPU_PC) == info.fw_base)
3547 break;
3548 tw32(RX_CPU_BASE + CPU_STATE, 0xffffffff);
3549 tw32(RX_CPU_BASE + CPU_MODE, CPU_MODE_HALT);
3550 tw32_f(RX_CPU_BASE + CPU_PC, info.fw_base);
3551 udelay(1000);
3552 }
3553 if (i >= 5) {
3554 netdev_err(tp->dev, "%s fails to set RX CPU PC, is %08x "
3555 "should be %08x\n", __func__,
3556 tr32(RX_CPU_BASE + CPU_PC), info.fw_base);
3557 return -ENODEV;
3558 }
3559 tw32(RX_CPU_BASE + CPU_STATE, 0xffffffff);
3560 tw32_f(RX_CPU_BASE + CPU_MODE, 0x00000000);
3561
3562 return 0;
3563}
3564
3565/* tp->lock is held. */
3566static int tg3_load_tso_firmware(struct tg3 *tp)
3567{
3568 struct fw_info info;
3569 const __be32 *fw_data;
3570 unsigned long cpu_base, cpu_scratch_base, cpu_scratch_size;
3571 int err, i;
3572
3573 if (tg3_flag(tp, HW_TSO_1) ||
3574 tg3_flag(tp, HW_TSO_2) ||
3575 tg3_flag(tp, HW_TSO_3))
3576 return 0;
3577
3578 fw_data = (void *)tp->fw->data;
3579
3580 /* Firmware blob starts with version numbers, followed by
3581 start address and length. We are setting complete length.
3582 length = end_address_of_bss - start_address_of_text.
3583 Remainder is the blob to be loaded contiguously
3584 from start address. */
3585
3586 info.fw_base = be32_to_cpu(fw_data[1]);
3587 cpu_scratch_size = tp->fw_len;
3588 info.fw_len = tp->fw->size - 12;
3589 info.fw_data = &fw_data[3];
3590
3591 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
3592 cpu_base = RX_CPU_BASE;
3593 cpu_scratch_base = NIC_SRAM_MBUF_POOL_BASE5705;
3594 } else {
3595 cpu_base = TX_CPU_BASE;
3596 cpu_scratch_base = TX_CPU_SCRATCH_BASE;
3597 cpu_scratch_size = TX_CPU_SCRATCH_SIZE;
3598 }
3599
3600 err = tg3_load_firmware_cpu(tp, cpu_base,
3601 cpu_scratch_base, cpu_scratch_size,
3602 &info);
3603 if (err)
3604 return err;
3605
3606 /* Now startup the cpu. */
3607 tw32(cpu_base + CPU_STATE, 0xffffffff);
3608 tw32_f(cpu_base + CPU_PC, info.fw_base);
3609
3610 for (i = 0; i < 5; i++) {
3611 if (tr32(cpu_base + CPU_PC) == info.fw_base)
3612 break;
3613 tw32(cpu_base + CPU_STATE, 0xffffffff);
3614 tw32(cpu_base + CPU_MODE, CPU_MODE_HALT);
3615 tw32_f(cpu_base + CPU_PC, info.fw_base);
3616 udelay(1000);
3617 }
3618 if (i >= 5) {
3619 netdev_err(tp->dev,
3620 "%s fails to set CPU PC, is %08x should be %08x\n",
3621 __func__, tr32(cpu_base + CPU_PC), info.fw_base);
3622 return -ENODEV;
3623 }
3624 tw32(cpu_base + CPU_STATE, 0xffffffff);
3625 tw32_f(cpu_base + CPU_MODE, 0x00000000);
3626 return 0;
3627}
3628
3629
Matt Carlsonffbcfed2009-02-25 14:24:28 +00003630/* tp->lock is held. */
Matt Carlson3f007892008-11-03 16:51:36 -08003631static void __tg3_set_mac_addr(struct tg3 *tp, int skip_mac_1)
3632{
3633 u32 addr_high, addr_low;
3634 int i;
3635
3636 addr_high = ((tp->dev->dev_addr[0] << 8) |
3637 tp->dev->dev_addr[1]);
3638 addr_low = ((tp->dev->dev_addr[2] << 24) |
3639 (tp->dev->dev_addr[3] << 16) |
3640 (tp->dev->dev_addr[4] << 8) |
3641 (tp->dev->dev_addr[5] << 0));
3642 for (i = 0; i < 4; i++) {
3643 if (i == 1 && skip_mac_1)
3644 continue;
3645 tw32(MAC_ADDR_0_HIGH + (i * 8), addr_high);
3646 tw32(MAC_ADDR_0_LOW + (i * 8), addr_low);
3647 }
3648
3649 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
3650 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
3651 for (i = 0; i < 12; i++) {
3652 tw32(MAC_EXTADDR_0_HIGH + (i * 8), addr_high);
3653 tw32(MAC_EXTADDR_0_LOW + (i * 8), addr_low);
3654 }
3655 }
3656
3657 addr_high = (tp->dev->dev_addr[0] +
3658 tp->dev->dev_addr[1] +
3659 tp->dev->dev_addr[2] +
3660 tp->dev->dev_addr[3] +
3661 tp->dev->dev_addr[4] +
3662 tp->dev->dev_addr[5]) &
3663 TX_BACKOFF_SEED_MASK;
3664 tw32(MAC_TX_BACKOFF_SEED, addr_high);
3665}
3666
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003667static void tg3_enable_register_access(struct tg3 *tp)
3668{
3669 /*
3670 * Make sure register accesses (indirect or otherwise) will function
3671 * correctly.
3672 */
3673 pci_write_config_dword(tp->pdev,
3674 TG3PCI_MISC_HOST_CTRL, tp->misc_host_ctrl);
3675}
3676
3677static int tg3_power_up(struct tg3 *tp)
3678{
Matt Carlsonbed98292011-07-13 09:27:29 +00003679 int err;
3680
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003681 tg3_enable_register_access(tp);
3682
Matt Carlsonbed98292011-07-13 09:27:29 +00003683 err = pci_set_power_state(tp->pdev, PCI_D0);
3684 if (!err) {
3685 /* Switch out of Vaux if it is a NIC */
3686 tg3_pwrsrc_switch_to_vmain(tp);
3687 } else {
3688 netdev_err(tp->dev, "Transition to D0 failed\n");
3689 }
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003690
Matt Carlsonbed98292011-07-13 09:27:29 +00003691 return err;
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003692}
3693
Matt Carlson4b409522012-02-13 10:20:11 +00003694static int tg3_setup_phy(struct tg3 *, int);
3695
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003696static int tg3_power_down_prepare(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003697{
3698 u32 misc_host_ctrl;
Matt Carlson0a459aa2008-11-03 16:54:15 -08003699 bool device_should_wake, do_low_power;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003700
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003701 tg3_enable_register_access(tp);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08003702
3703 /* Restore the CLKREQ setting. */
Jiang Liu0f49bfb2012-08-20 13:28:20 -06003704 if (tg3_flag(tp, CLKREQ_BUG))
3705 pcie_capability_set_word(tp->pdev, PCI_EXP_LNKCTL,
3706 PCI_EXP_LNKCTL_CLKREQ_EN);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08003707
Linus Torvalds1da177e2005-04-16 15:20:36 -07003708 misc_host_ctrl = tr32(TG3PCI_MISC_HOST_CTRL);
3709 tw32(TG3PCI_MISC_HOST_CTRL,
3710 misc_host_ctrl | MISC_HOST_CTRL_MASK_PCI_INT);
3711
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003712 device_should_wake = device_may_wakeup(&tp->pdev->dev) &&
Joe Perches63c3a662011-04-26 08:12:10 +00003713 tg3_flag(tp, WOL_ENABLE);
Matt Carlson05ac4cb2008-11-03 16:53:46 -08003714
Joe Perches63c3a662011-04-26 08:12:10 +00003715 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson0a459aa2008-11-03 16:54:15 -08003716 do_low_power = false;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003717 if ((tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) &&
Matt Carlson800960682010-08-02 11:26:06 +00003718 !(tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07003719 struct phy_device *phydev;
Matt Carlson0a459aa2008-11-03 16:54:15 -08003720 u32 phyid, advertising;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07003721
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00003722 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07003723
Matt Carlson800960682010-08-02 11:26:06 +00003724 tp->phy_flags |= TG3_PHYFLG_IS_LOW_POWER;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07003725
Matt Carlsonc6700ce2012-02-13 15:20:15 +00003726 tp->link_config.speed = phydev->speed;
3727 tp->link_config.duplex = phydev->duplex;
3728 tp->link_config.autoneg = phydev->autoneg;
3729 tp->link_config.advertising = phydev->advertising;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07003730
3731 advertising = ADVERTISED_TP |
3732 ADVERTISED_Pause |
3733 ADVERTISED_Autoneg |
3734 ADVERTISED_10baseT_Half;
3735
Joe Perches63c3a662011-04-26 08:12:10 +00003736 if (tg3_flag(tp, ENABLE_ASF) || device_should_wake) {
3737 if (tg3_flag(tp, WOL_SPEED_100MB))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07003738 advertising |=
3739 ADVERTISED_100baseT_Half |
3740 ADVERTISED_100baseT_Full |
3741 ADVERTISED_10baseT_Full;
3742 else
3743 advertising |= ADVERTISED_10baseT_Full;
3744 }
3745
3746 phydev->advertising = advertising;
3747
3748 phy_start_aneg(phydev);
Matt Carlson0a459aa2008-11-03 16:54:15 -08003749
3750 phyid = phydev->drv->phy_id & phydev->drv->phy_id_mask;
Matt Carlson6a443a02010-02-17 15:17:04 +00003751 if (phyid != PHY_ID_BCMAC131) {
3752 phyid &= PHY_BCM_OUI_MASK;
3753 if (phyid == PHY_BCM_OUI_1 ||
3754 phyid == PHY_BCM_OUI_2 ||
3755 phyid == PHY_BCM_OUI_3)
Matt Carlson0a459aa2008-11-03 16:54:15 -08003756 do_low_power = true;
3757 }
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07003758 }
Matt Carlsondd477002008-05-25 23:45:58 -07003759 } else {
Matt Carlson20232762008-12-21 20:18:56 -08003760 do_low_power = true;
Matt Carlson0a459aa2008-11-03 16:54:15 -08003761
Matt Carlsonc6700ce2012-02-13 15:20:15 +00003762 if (!(tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER))
Matt Carlson800960682010-08-02 11:26:06 +00003763 tp->phy_flags |= TG3_PHYFLG_IS_LOW_POWER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003764
Matt Carlson2855b9f2012-02-13 15:20:14 +00003765 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
Matt Carlsondd477002008-05-25 23:45:58 -07003766 tg3_setup_phy(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003767 }
3768
Michael Chanb5d37722006-09-27 16:06:21 -07003769 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
3770 u32 val;
3771
3772 val = tr32(GRC_VCPU_EXT_CTRL);
3773 tw32(GRC_VCPU_EXT_CTRL, val | GRC_VCPU_EXT_CTRL_DISABLE_WOL);
Joe Perches63c3a662011-04-26 08:12:10 +00003774 } else if (!tg3_flag(tp, ENABLE_ASF)) {
Michael Chan6921d202005-12-13 21:15:53 -08003775 int i;
3776 u32 val;
3777
3778 for (i = 0; i < 200; i++) {
3779 tg3_read_mem(tp, NIC_SRAM_FW_ASF_STATUS_MBOX, &val);
3780 if (val == ~NIC_SRAM_FIRMWARE_MBOX_MAGIC1)
3781 break;
3782 msleep(1);
3783 }
3784 }
Joe Perches63c3a662011-04-26 08:12:10 +00003785 if (tg3_flag(tp, WOL_CAP))
Gary Zambranoa85feb82007-05-05 11:52:19 -07003786 tg3_write_mem(tp, NIC_SRAM_WOL_MBOX, WOL_SIGNATURE |
3787 WOL_DRV_STATE_SHUTDOWN |
3788 WOL_DRV_WOL |
3789 WOL_SET_MAGIC_PKT);
Michael Chan6921d202005-12-13 21:15:53 -08003790
Matt Carlson05ac4cb2008-11-03 16:53:46 -08003791 if (device_should_wake) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003792 u32 mac_mode;
3793
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003794 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES)) {
Matt Carlsonb4bd2922011-04-20 07:57:41 +00003795 if (do_low_power &&
3796 !(tp->phy_flags & TG3_PHYFLG_IS_FET)) {
3797 tg3_phy_auxctl_write(tp,
3798 MII_TG3_AUXCTL_SHDWSEL_PWRCTL,
3799 MII_TG3_AUXCTL_PCTL_WOL_EN |
3800 MII_TG3_AUXCTL_PCTL_100TX_LPWR |
3801 MII_TG3_AUXCTL_PCTL_CL_AB_TXDAC);
Matt Carlsondd477002008-05-25 23:45:58 -07003802 udelay(40);
3803 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003804
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003805 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
Michael Chan3f7045c2006-09-27 16:02:29 -07003806 mac_mode = MAC_MODE_PORT_MODE_GMII;
3807 else
3808 mac_mode = MAC_MODE_PORT_MODE_MII;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003809
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07003810 mac_mode |= tp->mac_mode & MAC_MODE_LINK_POLARITY;
3811 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
3812 ASIC_REV_5700) {
Joe Perches63c3a662011-04-26 08:12:10 +00003813 u32 speed = tg3_flag(tp, WOL_SPEED_100MB) ?
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07003814 SPEED_100 : SPEED_10;
3815 if (tg3_5700_link_polarity(tp, speed))
3816 mac_mode |= MAC_MODE_LINK_POLARITY;
3817 else
3818 mac_mode &= ~MAC_MODE_LINK_POLARITY;
3819 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003820 } else {
3821 mac_mode = MAC_MODE_PORT_MODE_TBI;
3822 }
3823
Joe Perches63c3a662011-04-26 08:12:10 +00003824 if (!tg3_flag(tp, 5750_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003825 tw32(MAC_LED_CTRL, tp->led_ctrl);
3826
Matt Carlson05ac4cb2008-11-03 16:53:46 -08003827 mac_mode |= MAC_MODE_MAGIC_PKT_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00003828 if ((tg3_flag(tp, 5705_PLUS) && !tg3_flag(tp, 5780_CLASS)) &&
3829 (tg3_flag(tp, ENABLE_ASF) || tg3_flag(tp, ENABLE_APE)))
Matt Carlson05ac4cb2008-11-03 16:53:46 -08003830 mac_mode |= MAC_MODE_KEEP_FRAME_IN_WOL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003831
Joe Perches63c3a662011-04-26 08:12:10 +00003832 if (tg3_flag(tp, ENABLE_APE))
Matt Carlsond2394e6b2010-11-24 08:31:47 +00003833 mac_mode |= MAC_MODE_APE_TX_EN |
3834 MAC_MODE_APE_RX_EN |
3835 MAC_MODE_TDE_ENABLE;
Matt Carlson3bda1252008-08-15 14:08:22 -07003836
Linus Torvalds1da177e2005-04-16 15:20:36 -07003837 tw32_f(MAC_MODE, mac_mode);
3838 udelay(100);
3839
3840 tw32_f(MAC_RX_MODE, RX_MODE_ENABLE);
3841 udelay(10);
3842 }
3843
Joe Perches63c3a662011-04-26 08:12:10 +00003844 if (!tg3_flag(tp, WOL_SPEED_100MB) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003845 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
3846 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)) {
3847 u32 base_val;
3848
3849 base_val = tp->pci_clock_ctrl;
3850 base_val |= (CLOCK_CTRL_RXCLK_DISABLE |
3851 CLOCK_CTRL_TXCLK_DISABLE);
3852
Michael Chanb401e9e2005-12-19 16:27:04 -08003853 tw32_wait_f(TG3PCI_CLOCK_CTRL, base_val | CLOCK_CTRL_ALTCLK |
3854 CLOCK_CTRL_PWRDOWN_PLL133, 40);
Joe Perches63c3a662011-04-26 08:12:10 +00003855 } else if (tg3_flag(tp, 5780_CLASS) ||
3856 tg3_flag(tp, CPMU_PRESENT) ||
Matt Carlson6ff6f812011-05-19 12:12:54 +00003857 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan4cf78e42005-07-25 12:29:19 -07003858 /* do nothing */
Joe Perches63c3a662011-04-26 08:12:10 +00003859 } else if (!(tg3_flag(tp, 5750_PLUS) && tg3_flag(tp, ENABLE_ASF))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003860 u32 newbits1, newbits2;
3861
3862 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
3863 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
3864 newbits1 = (CLOCK_CTRL_RXCLK_DISABLE |
3865 CLOCK_CTRL_TXCLK_DISABLE |
3866 CLOCK_CTRL_ALTCLK);
3867 newbits2 = newbits1 | CLOCK_CTRL_44MHZ_CORE;
Joe Perches63c3a662011-04-26 08:12:10 +00003868 } else if (tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003869 newbits1 = CLOCK_CTRL_625_CORE;
3870 newbits2 = newbits1 | CLOCK_CTRL_ALTCLK;
3871 } else {
3872 newbits1 = CLOCK_CTRL_ALTCLK;
3873 newbits2 = newbits1 | CLOCK_CTRL_44MHZ_CORE;
3874 }
3875
Michael Chanb401e9e2005-12-19 16:27:04 -08003876 tw32_wait_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl | newbits1,
3877 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003878
Michael Chanb401e9e2005-12-19 16:27:04 -08003879 tw32_wait_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl | newbits2,
3880 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003881
Joe Perches63c3a662011-04-26 08:12:10 +00003882 if (!tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003883 u32 newbits3;
3884
3885 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
3886 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
3887 newbits3 = (CLOCK_CTRL_RXCLK_DISABLE |
3888 CLOCK_CTRL_TXCLK_DISABLE |
3889 CLOCK_CTRL_44MHZ_CORE);
3890 } else {
3891 newbits3 = CLOCK_CTRL_44MHZ_CORE;
3892 }
3893
Michael Chanb401e9e2005-12-19 16:27:04 -08003894 tw32_wait_f(TG3PCI_CLOCK_CTRL,
3895 tp->pci_clock_ctrl | newbits3, 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003896 }
3897 }
3898
Joe Perches63c3a662011-04-26 08:12:10 +00003899 if (!(device_should_wake) && !tg3_flag(tp, ENABLE_ASF))
Matt Carlson0a459aa2008-11-03 16:54:15 -08003900 tg3_power_down_phy(tp, do_low_power);
Michael Chan6921d202005-12-13 21:15:53 -08003901
Matt Carlsoncd0d7222011-07-13 09:27:33 +00003902 tg3_frob_aux_power(tp, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003903
3904 /* Workaround for unstable PLL clock */
3905 if ((GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_AX) ||
3906 (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_BX)) {
3907 u32 val = tr32(0x7d00);
3908
3909 val &= ~((1 << 16) | (1 << 4) | (1 << 2) | (1 << 1) | 1);
3910 tw32(0x7d00, val);
Joe Perches63c3a662011-04-26 08:12:10 +00003911 if (!tg3_flag(tp, ENABLE_ASF)) {
Michael Chanec41c7d2006-01-17 02:40:55 -08003912 int err;
3913
3914 err = tg3_nvram_lock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003915 tg3_halt_cpu(tp, RX_CPU_BASE);
Michael Chanec41c7d2006-01-17 02:40:55 -08003916 if (!err)
3917 tg3_nvram_unlock(tp);
Michael Chan6921d202005-12-13 21:15:53 -08003918 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003919 }
3920
Michael Chanbbadf502006-04-06 21:46:34 -07003921 tg3_write_sig_post_reset(tp, RESET_KIND_SHUTDOWN);
3922
Linus Torvalds1da177e2005-04-16 15:20:36 -07003923 return 0;
3924}
3925
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003926static void tg3_power_down(struct tg3 *tp)
3927{
3928 tg3_power_down_prepare(tp);
3929
Joe Perches63c3a662011-04-26 08:12:10 +00003930 pci_wake_from_d3(tp->pdev, tg3_flag(tp, WOL_ENABLE));
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003931 pci_set_power_state(tp->pdev, PCI_D3hot);
3932}
3933
Linus Torvalds1da177e2005-04-16 15:20:36 -07003934static void tg3_aux_stat_to_speed_duplex(struct tg3 *tp, u32 val, u16 *speed, u8 *duplex)
3935{
3936 switch (val & MII_TG3_AUX_STAT_SPDMASK) {
3937 case MII_TG3_AUX_STAT_10HALF:
3938 *speed = SPEED_10;
3939 *duplex = DUPLEX_HALF;
3940 break;
3941
3942 case MII_TG3_AUX_STAT_10FULL:
3943 *speed = SPEED_10;
3944 *duplex = DUPLEX_FULL;
3945 break;
3946
3947 case MII_TG3_AUX_STAT_100HALF:
3948 *speed = SPEED_100;
3949 *duplex = DUPLEX_HALF;
3950 break;
3951
3952 case MII_TG3_AUX_STAT_100FULL:
3953 *speed = SPEED_100;
3954 *duplex = DUPLEX_FULL;
3955 break;
3956
3957 case MII_TG3_AUX_STAT_1000HALF:
3958 *speed = SPEED_1000;
3959 *duplex = DUPLEX_HALF;
3960 break;
3961
3962 case MII_TG3_AUX_STAT_1000FULL:
3963 *speed = SPEED_1000;
3964 *duplex = DUPLEX_FULL;
3965 break;
3966
3967 default:
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003968 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Michael Chan715116a2006-09-27 16:09:25 -07003969 *speed = (val & MII_TG3_AUX_STAT_100) ? SPEED_100 :
3970 SPEED_10;
3971 *duplex = (val & MII_TG3_AUX_STAT_FULL) ? DUPLEX_FULL :
3972 DUPLEX_HALF;
3973 break;
3974 }
Matt Carlsone7405222012-02-13 15:20:16 +00003975 *speed = SPEED_UNKNOWN;
3976 *duplex = DUPLEX_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003977 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07003978 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003979}
3980
Matt Carlson42b64a42011-05-19 12:12:49 +00003981static int tg3_phy_autoneg_cfg(struct tg3 *tp, u32 advertise, u32 flowctrl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003982{
Matt Carlson42b64a42011-05-19 12:12:49 +00003983 int err = 0;
3984 u32 val, new_adv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003985
Matt Carlson42b64a42011-05-19 12:12:49 +00003986 new_adv = ADVERTISE_CSMA;
Hiroaki SHIMODA202ff1c2011-11-22 04:05:41 +00003987 new_adv |= ethtool_adv_to_mii_adv_t(advertise) & ADVERTISE_ALL;
Matt Carlsonf88788f2011-12-14 11:10:00 +00003988 new_adv |= mii_advertise_flowctrl(flowctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003989
Matt Carlson42b64a42011-05-19 12:12:49 +00003990 err = tg3_writephy(tp, MII_ADVERTISE, new_adv);
3991 if (err)
3992 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003993
Matt Carlson4f272092011-12-14 11:09:57 +00003994 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
3995 new_adv = ethtool_adv_to_mii_ctrl1000_t(advertise);
Matt Carlsonba4d07a2007-12-20 20:08:00 -08003996
Matt Carlson4f272092011-12-14 11:09:57 +00003997 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
3998 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0)
3999 new_adv |= CTL1000_AS_MASTER | CTL1000_ENABLE_MASTER;
Matt Carlsonba4d07a2007-12-20 20:08:00 -08004000
Matt Carlson4f272092011-12-14 11:09:57 +00004001 err = tg3_writephy(tp, MII_CTRL1000, new_adv);
4002 if (err)
4003 goto done;
4004 }
Matt Carlsonba4d07a2007-12-20 20:08:00 -08004005
Matt Carlson42b64a42011-05-19 12:12:49 +00004006 if (!(tp->phy_flags & TG3_PHYFLG_EEE_CAP))
4007 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004008
Matt Carlson42b64a42011-05-19 12:12:49 +00004009 tw32(TG3_CPMU_EEE_MODE,
4010 tr32(TG3_CPMU_EEE_MODE) & ~TG3_CPMU_EEEMD_LPI_ENABLE);
Matt Carlsonba4d07a2007-12-20 20:08:00 -08004011
Matt Carlson42b64a42011-05-19 12:12:49 +00004012 err = TG3_PHY_AUXCTL_SMDSP_ENABLE(tp);
4013 if (!err) {
4014 u32 err2;
Matt Carlson52b02d02010-10-14 10:37:41 +00004015
Matt Carlsona6b68da2010-12-06 08:28:52 +00004016 val = 0;
Matt Carlson42b64a42011-05-19 12:12:49 +00004017 /* Advertise 100-BaseTX EEE ability */
4018 if (advertise & ADVERTISED_100baseT_Full)
4019 val |= MDIO_AN_EEE_ADV_100TX;
4020 /* Advertise 1000-BaseT EEE ability */
4021 if (advertise & ADVERTISED_1000baseT_Full)
4022 val |= MDIO_AN_EEE_ADV_1000T;
4023 err = tg3_phy_cl45_write(tp, MDIO_MMD_AN, MDIO_AN_EEE_ADV, val);
Matt Carlsonb715ce92011-07-20 10:20:52 +00004024 if (err)
4025 val = 0;
4026
4027 switch (GET_ASIC_REV(tp->pci_chip_rev_id)) {
4028 case ASIC_REV_5717:
4029 case ASIC_REV_57765:
Matt Carlson55086ad2011-12-14 11:09:59 +00004030 case ASIC_REV_57766:
Matt Carlsonb715ce92011-07-20 10:20:52 +00004031 case ASIC_REV_5719:
4032 /* If we advertised any eee advertisements above... */
4033 if (val)
4034 val = MII_TG3_DSP_TAP26_ALNOKO |
4035 MII_TG3_DSP_TAP26_RMRXSTO |
4036 MII_TG3_DSP_TAP26_OPCSINPT;
4037 tg3_phydsp_write(tp, MII_TG3_DSP_TAP26, val);
4038 /* Fall through */
4039 case ASIC_REV_5720:
4040 if (!tg3_phydsp_read(tp, MII_TG3_DSP_CH34TP2, &val))
4041 tg3_phydsp_write(tp, MII_TG3_DSP_CH34TP2, val |
4042 MII_TG3_DSP_CH34TP2_HIBW01);
4043 }
Matt Carlson52b02d02010-10-14 10:37:41 +00004044
Matt Carlson42b64a42011-05-19 12:12:49 +00004045 err2 = TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
4046 if (!err)
4047 err = err2;
4048 }
4049
4050done:
4051 return err;
4052}
4053
4054static void tg3_phy_copper_begin(struct tg3 *tp)
4055{
Matt Carlsond13ba512012-02-22 12:35:19 +00004056 if (tp->link_config.autoneg == AUTONEG_ENABLE ||
4057 (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)) {
4058 u32 adv, fc;
Matt Carlson42b64a42011-05-19 12:12:49 +00004059
Matt Carlsond13ba512012-02-22 12:35:19 +00004060 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) {
4061 adv = ADVERTISED_10baseT_Half |
4062 ADVERTISED_10baseT_Full;
4063 if (tg3_flag(tp, WOL_SPEED_100MB))
4064 adv |= ADVERTISED_100baseT_Half |
4065 ADVERTISED_100baseT_Full;
Matt Carlson42b64a42011-05-19 12:12:49 +00004066
Matt Carlsond13ba512012-02-22 12:35:19 +00004067 fc = FLOW_CTRL_TX | FLOW_CTRL_RX;
Matt Carlson42b64a42011-05-19 12:12:49 +00004068 } else {
Matt Carlsond13ba512012-02-22 12:35:19 +00004069 adv = tp->link_config.advertising;
4070 if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
4071 adv &= ~(ADVERTISED_1000baseT_Half |
4072 ADVERTISED_1000baseT_Full);
4073
4074 fc = tp->link_config.flowctrl;
Matt Carlson42b64a42011-05-19 12:12:49 +00004075 }
4076
Matt Carlsond13ba512012-02-22 12:35:19 +00004077 tg3_phy_autoneg_cfg(tp, adv, fc);
Matt Carlson52b02d02010-10-14 10:37:41 +00004078
Matt Carlsond13ba512012-02-22 12:35:19 +00004079 tg3_writephy(tp, MII_BMCR,
4080 BMCR_ANENABLE | BMCR_ANRESTART);
4081 } else {
4082 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004083 u32 bmcr, orig_bmcr;
4084
4085 tp->link_config.active_speed = tp->link_config.speed;
4086 tp->link_config.active_duplex = tp->link_config.duplex;
4087
4088 bmcr = 0;
4089 switch (tp->link_config.speed) {
4090 default:
4091 case SPEED_10:
4092 break;
4093
4094 case SPEED_100:
4095 bmcr |= BMCR_SPEED100;
4096 break;
4097
4098 case SPEED_1000:
Matt Carlson221c5632011-06-13 13:39:01 +00004099 bmcr |= BMCR_SPEED1000;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004100 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07004101 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004102
4103 if (tp->link_config.duplex == DUPLEX_FULL)
4104 bmcr |= BMCR_FULLDPLX;
4105
4106 if (!tg3_readphy(tp, MII_BMCR, &orig_bmcr) &&
4107 (bmcr != orig_bmcr)) {
4108 tg3_writephy(tp, MII_BMCR, BMCR_LOOPBACK);
4109 for (i = 0; i < 1500; i++) {
4110 u32 tmp;
4111
4112 udelay(10);
4113 if (tg3_readphy(tp, MII_BMSR, &tmp) ||
4114 tg3_readphy(tp, MII_BMSR, &tmp))
4115 continue;
4116 if (!(tmp & BMSR_LSTATUS)) {
4117 udelay(40);
4118 break;
4119 }
4120 }
4121 tg3_writephy(tp, MII_BMCR, bmcr);
4122 udelay(40);
4123 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004124 }
4125}
4126
4127static int tg3_init_5401phy_dsp(struct tg3 *tp)
4128{
4129 int err;
4130
4131 /* Turn off tap power management. */
4132 /* Set Extended packet length bit */
Matt Carlsonb4bd2922011-04-20 07:57:41 +00004133 err = tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_AUXCTL, 0x4c20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004134
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00004135 err |= tg3_phydsp_write(tp, 0x0012, 0x1804);
4136 err |= tg3_phydsp_write(tp, 0x0013, 0x1204);
4137 err |= tg3_phydsp_write(tp, 0x8006, 0x0132);
4138 err |= tg3_phydsp_write(tp, 0x8006, 0x0232);
4139 err |= tg3_phydsp_write(tp, 0x201f, 0x0a20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004140
4141 udelay(40);
4142
4143 return err;
4144}
4145
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004146static bool tg3_phy_copper_an_config_ok(struct tg3 *tp, u32 *lcladv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004147{
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004148 u32 advmsk, tgtadv, advertising;
Michael Chan3600d912006-12-07 00:21:48 -08004149
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004150 advertising = tp->link_config.advertising;
4151 tgtadv = ethtool_adv_to_mii_adv_t(advertising) & ADVERTISE_ALL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004152
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004153 advmsk = ADVERTISE_ALL;
4154 if (tp->link_config.active_duplex == DUPLEX_FULL) {
Matt Carlsonf88788f2011-12-14 11:10:00 +00004155 tgtadv |= mii_advertise_flowctrl(tp->link_config.flowctrl);
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004156 advmsk |= ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
4157 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004158
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004159 if (tg3_readphy(tp, MII_ADVERTISE, lcladv))
4160 return false;
4161
4162 if ((*lcladv & advmsk) != tgtadv)
4163 return false;
Matt Carlsonb99d2a52011-08-31 11:44:47 +00004164
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004165 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004166 u32 tg3_ctrl;
4167
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004168 tgtadv = ethtool_adv_to_mii_ctrl1000_t(advertising);
Michael Chan3600d912006-12-07 00:21:48 -08004169
Matt Carlson221c5632011-06-13 13:39:01 +00004170 if (tg3_readphy(tp, MII_CTRL1000, &tg3_ctrl))
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004171 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004172
Matt Carlson3198e072012-02-13 15:20:10 +00004173 if (tgtadv &&
4174 (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
4175 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0)) {
4176 tgtadv |= CTL1000_AS_MASTER | CTL1000_ENABLE_MASTER;
4177 tg3_ctrl &= (ADVERTISE_1000HALF | ADVERTISE_1000FULL |
4178 CTL1000_AS_MASTER | CTL1000_ENABLE_MASTER);
4179 } else {
4180 tg3_ctrl &= (ADVERTISE_1000HALF | ADVERTISE_1000FULL);
4181 }
4182
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004183 if (tg3_ctrl != tgtadv)
4184 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004185 }
Matt Carlson93a700a2011-08-31 11:44:54 +00004186
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004187 return true;
Matt Carlsonef167e22007-12-20 20:10:01 -08004188}
4189
Matt Carlson859edb22011-12-08 14:40:16 +00004190static bool tg3_phy_copper_fetch_rmtadv(struct tg3 *tp, u32 *rmtadv)
4191{
4192 u32 lpeth = 0;
4193
4194 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
4195 u32 val;
4196
4197 if (tg3_readphy(tp, MII_STAT1000, &val))
4198 return false;
4199
4200 lpeth = mii_stat1000_to_ethtool_lpa_t(val);
4201 }
4202
4203 if (tg3_readphy(tp, MII_LPA, rmtadv))
4204 return false;
4205
4206 lpeth |= mii_lpa_to_ethtool_lpa_t(*rmtadv);
4207 tp->link_config.rmt_adv = lpeth;
4208
4209 return true;
4210}
4211
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00004212static bool tg3_test_and_report_link_chg(struct tg3 *tp, int curr_link_up)
4213{
4214 if (curr_link_up != tp->link_up) {
4215 if (curr_link_up) {
4216 tg3_carrier_on(tp);
4217 } else {
4218 tg3_carrier_off(tp);
4219 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
4220 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
4221 }
4222
4223 tg3_link_report(tp);
4224 return true;
4225 }
4226
4227 return false;
4228}
4229
Linus Torvalds1da177e2005-04-16 15:20:36 -07004230static int tg3_setup_copper_phy(struct tg3 *tp, int force_reset)
4231{
4232 int current_link_up;
Matt Carlsonf833c4c2010-09-15 09:00:01 +00004233 u32 bmsr, val;
Matt Carlsonef167e22007-12-20 20:10:01 -08004234 u32 lcl_adv, rmt_adv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004235 u16 current_speed;
4236 u8 current_duplex;
4237 int i, err;
4238
4239 tw32(MAC_EVENT, 0);
4240
4241 tw32_f(MAC_STATUS,
4242 (MAC_STATUS_SYNC_CHANGED |
4243 MAC_STATUS_CFG_CHANGED |
4244 MAC_STATUS_MI_COMPLETION |
4245 MAC_STATUS_LNKSTATE_CHANGED));
4246 udelay(40);
4247
Matt Carlson8ef21422008-05-02 16:47:53 -07004248 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
4249 tw32_f(MAC_MI_MODE,
4250 (tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL));
4251 udelay(80);
4252 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004253
Matt Carlsonb4bd2922011-04-20 07:57:41 +00004254 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_PWRCTL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004255
4256 /* Some third-party PHYs need to be reset on link going
4257 * down.
4258 */
4259 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
4260 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
4261 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) &&
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00004262 tp->link_up) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004263 tg3_readphy(tp, MII_BMSR, &bmsr);
4264 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
4265 !(bmsr & BMSR_LSTATUS))
4266 force_reset = 1;
4267 }
4268 if (force_reset)
4269 tg3_phy_reset(tp);
4270
Matt Carlson79eb6902010-02-17 15:17:03 +00004271 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004272 tg3_readphy(tp, MII_BMSR, &bmsr);
4273 if (tg3_readphy(tp, MII_BMSR, &bmsr) ||
Joe Perches63c3a662011-04-26 08:12:10 +00004274 !tg3_flag(tp, INIT_COMPLETE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004275 bmsr = 0;
4276
4277 if (!(bmsr & BMSR_LSTATUS)) {
4278 err = tg3_init_5401phy_dsp(tp);
4279 if (err)
4280 return err;
4281
4282 tg3_readphy(tp, MII_BMSR, &bmsr);
4283 for (i = 0; i < 1000; i++) {
4284 udelay(10);
4285 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
4286 (bmsr & BMSR_LSTATUS)) {
4287 udelay(40);
4288 break;
4289 }
4290 }
4291
Matt Carlson79eb6902010-02-17 15:17:03 +00004292 if ((tp->phy_id & TG3_PHY_ID_REV_MASK) ==
4293 TG3_PHY_REV_BCM5401_B0 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004294 !(bmsr & BMSR_LSTATUS) &&
4295 tp->link_config.active_speed == SPEED_1000) {
4296 err = tg3_phy_reset(tp);
4297 if (!err)
4298 err = tg3_init_5401phy_dsp(tp);
4299 if (err)
4300 return err;
4301 }
4302 }
4303 } else if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
4304 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0) {
4305 /* 5701 {A0,B0} CRC bug workaround */
4306 tg3_writephy(tp, 0x15, 0x0a75);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00004307 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8c68);
4308 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8d68);
4309 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8c68);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004310 }
4311
4312 /* Clear pending interrupts... */
Matt Carlsonf833c4c2010-09-15 09:00:01 +00004313 tg3_readphy(tp, MII_TG3_ISTAT, &val);
4314 tg3_readphy(tp, MII_TG3_ISTAT, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004315
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004316 if (tp->phy_flags & TG3_PHYFLG_USE_MI_INTERRUPT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004317 tg3_writephy(tp, MII_TG3_IMASK, ~MII_TG3_INT_LINKCHG);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004318 else if (!(tp->phy_flags & TG3_PHYFLG_IS_FET))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004319 tg3_writephy(tp, MII_TG3_IMASK, ~0);
4320
4321 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
4322 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
4323 if (tp->led_ctrl == LED_CTRL_MODE_PHY_1)
4324 tg3_writephy(tp, MII_TG3_EXT_CTRL,
4325 MII_TG3_EXT_CTRL_LNK3_LED_MODE);
4326 else
4327 tg3_writephy(tp, MII_TG3_EXT_CTRL, 0);
4328 }
4329
4330 current_link_up = 0;
Matt Carlsone7405222012-02-13 15:20:16 +00004331 current_speed = SPEED_UNKNOWN;
4332 current_duplex = DUPLEX_UNKNOWN;
Matt Carlsone348c5e2011-11-21 15:01:20 +00004333 tp->phy_flags &= ~TG3_PHYFLG_MDIX_STATE;
Matt Carlson859edb22011-12-08 14:40:16 +00004334 tp->link_config.rmt_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004335
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004336 if (tp->phy_flags & TG3_PHYFLG_CAPACITIVE_COUPLING) {
Matt Carlson15ee95c2011-04-20 07:57:40 +00004337 err = tg3_phy_auxctl_read(tp,
4338 MII_TG3_AUXCTL_SHDWSEL_MISCTEST,
4339 &val);
4340 if (!err && !(val & (1 << 10))) {
Matt Carlsonb4bd2922011-04-20 07:57:41 +00004341 tg3_phy_auxctl_write(tp,
4342 MII_TG3_AUXCTL_SHDWSEL_MISCTEST,
4343 val | (1 << 10));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004344 goto relink;
4345 }
4346 }
4347
4348 bmsr = 0;
4349 for (i = 0; i < 100; i++) {
4350 tg3_readphy(tp, MII_BMSR, &bmsr);
4351 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
4352 (bmsr & BMSR_LSTATUS))
4353 break;
4354 udelay(40);
4355 }
4356
4357 if (bmsr & BMSR_LSTATUS) {
4358 u32 aux_stat, bmcr;
4359
4360 tg3_readphy(tp, MII_TG3_AUX_STAT, &aux_stat);
4361 for (i = 0; i < 2000; i++) {
4362 udelay(10);
4363 if (!tg3_readphy(tp, MII_TG3_AUX_STAT, &aux_stat) &&
4364 aux_stat)
4365 break;
4366 }
4367
4368 tg3_aux_stat_to_speed_duplex(tp, aux_stat,
4369 &current_speed,
4370 &current_duplex);
4371
4372 bmcr = 0;
4373 for (i = 0; i < 200; i++) {
4374 tg3_readphy(tp, MII_BMCR, &bmcr);
4375 if (tg3_readphy(tp, MII_BMCR, &bmcr))
4376 continue;
4377 if (bmcr && bmcr != 0x7fff)
4378 break;
4379 udelay(10);
4380 }
4381
Matt Carlsonef167e22007-12-20 20:10:01 -08004382 lcl_adv = 0;
4383 rmt_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004384
Matt Carlsonef167e22007-12-20 20:10:01 -08004385 tp->link_config.active_speed = current_speed;
4386 tp->link_config.active_duplex = current_duplex;
4387
4388 if (tp->link_config.autoneg == AUTONEG_ENABLE) {
4389 if ((bmcr & BMCR_ANENABLE) &&
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004390 tg3_phy_copper_an_config_ok(tp, &lcl_adv) &&
Matt Carlson859edb22011-12-08 14:40:16 +00004391 tg3_phy_copper_fetch_rmtadv(tp, &rmt_adv))
Matt Carlsone2bf73e2011-12-08 14:40:15 +00004392 current_link_up = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004393 } else {
4394 if (!(bmcr & BMCR_ANENABLE) &&
4395 tp->link_config.speed == current_speed &&
Matt Carlsonef167e22007-12-20 20:10:01 -08004396 tp->link_config.duplex == current_duplex &&
4397 tp->link_config.flowctrl ==
4398 tp->link_config.active_flowctrl) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004399 current_link_up = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004400 }
4401 }
4402
Matt Carlsonef167e22007-12-20 20:10:01 -08004403 if (current_link_up == 1 &&
Matt Carlsone348c5e2011-11-21 15:01:20 +00004404 tp->link_config.active_duplex == DUPLEX_FULL) {
4405 u32 reg, bit;
4406
4407 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
4408 reg = MII_TG3_FET_GEN_STAT;
4409 bit = MII_TG3_FET_GEN_STAT_MDIXSTAT;
4410 } else {
4411 reg = MII_TG3_EXT_STAT;
4412 bit = MII_TG3_EXT_STAT_MDIX;
4413 }
4414
4415 if (!tg3_readphy(tp, reg, &val) && (val & bit))
4416 tp->phy_flags |= TG3_PHYFLG_MDIX_STATE;
4417
Matt Carlsonef167e22007-12-20 20:10:01 -08004418 tg3_setup_flow_control(tp, lcl_adv, rmt_adv);
Matt Carlsone348c5e2011-11-21 15:01:20 +00004419 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004420 }
4421
Linus Torvalds1da177e2005-04-16 15:20:36 -07004422relink:
Matt Carlson800960682010-08-02 11:26:06 +00004423 if (current_link_up == 0 || (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004424 tg3_phy_copper_begin(tp);
4425
Matt Carlsonf833c4c2010-09-15 09:00:01 +00004426 tg3_readphy(tp, MII_BMSR, &bmsr);
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00004427 if ((!tg3_readphy(tp, MII_BMSR, &bmsr) && (bmsr & BMSR_LSTATUS)) ||
4428 (tp->mac_mode & MAC_MODE_PORT_INT_LPBACK))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004429 current_link_up = 1;
4430 }
4431
4432 tp->mac_mode &= ~MAC_MODE_PORT_MODE_MASK;
4433 if (current_link_up == 1) {
4434 if (tp->link_config.active_speed == SPEED_100 ||
4435 tp->link_config.active_speed == SPEED_10)
4436 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
4437 else
4438 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004439 } else if (tp->phy_flags & TG3_PHYFLG_IS_FET)
Matt Carlson7f97a4b2009-08-25 10:10:03 +00004440 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
4441 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004442 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
4443
4444 tp->mac_mode &= ~MAC_MODE_HALF_DUPLEX;
4445 if (tp->link_config.active_duplex == DUPLEX_HALF)
4446 tp->mac_mode |= MAC_MODE_HALF_DUPLEX;
4447
Linus Torvalds1da177e2005-04-16 15:20:36 -07004448 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700) {
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07004449 if (current_link_up == 1 &&
4450 tg3_5700_link_polarity(tp, tp->link_config.active_speed))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004451 tp->mac_mode |= MAC_MODE_LINK_POLARITY;
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07004452 else
4453 tp->mac_mode &= ~MAC_MODE_LINK_POLARITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004454 }
4455
4456 /* ??? Without this setting Netgear GA302T PHY does not
4457 * ??? send/receive packets...
4458 */
Matt Carlson79eb6902010-02-17 15:17:03 +00004459 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5411 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004460 tp->pci_chip_rev_id == CHIPREV_ID_5700_ALTIMA) {
4461 tp->mi_mode |= MAC_MI_MODE_AUTO_POLL;
4462 tw32_f(MAC_MI_MODE, tp->mi_mode);
4463 udelay(80);
4464 }
4465
4466 tw32_f(MAC_MODE, tp->mac_mode);
4467 udelay(40);
4468
Matt Carlson52b02d02010-10-14 10:37:41 +00004469 tg3_phy_eee_adjust(tp, current_link_up);
4470
Joe Perches63c3a662011-04-26 08:12:10 +00004471 if (tg3_flag(tp, USE_LINKCHG_REG)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004472 /* Polled via timer. */
4473 tw32_f(MAC_EVENT, 0);
4474 } else {
4475 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
4476 }
4477 udelay(40);
4478
4479 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 &&
4480 current_link_up == 1 &&
4481 tp->link_config.active_speed == SPEED_1000 &&
Joe Perches63c3a662011-04-26 08:12:10 +00004482 (tg3_flag(tp, PCIX_MODE) || tg3_flag(tp, PCI_HIGH_SPEED))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004483 udelay(120);
4484 tw32_f(MAC_STATUS,
4485 (MAC_STATUS_SYNC_CHANGED |
4486 MAC_STATUS_CFG_CHANGED));
4487 udelay(40);
4488 tg3_write_mem(tp,
4489 NIC_SRAM_FIRMWARE_MBOX,
4490 NIC_SRAM_FIRMWARE_MBOX_MAGIC2);
4491 }
4492
Matt Carlson5e7dfd02008-11-21 17:18:16 -08004493 /* Prevent send BD corruption. */
Joe Perches63c3a662011-04-26 08:12:10 +00004494 if (tg3_flag(tp, CLKREQ_BUG)) {
Matt Carlson5e7dfd02008-11-21 17:18:16 -08004495 if (tp->link_config.active_speed == SPEED_100 ||
4496 tp->link_config.active_speed == SPEED_10)
Jiang Liu0f49bfb2012-08-20 13:28:20 -06004497 pcie_capability_clear_word(tp->pdev, PCI_EXP_LNKCTL,
4498 PCI_EXP_LNKCTL_CLKREQ_EN);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08004499 else
Jiang Liu0f49bfb2012-08-20 13:28:20 -06004500 pcie_capability_set_word(tp->pdev, PCI_EXP_LNKCTL,
4501 PCI_EXP_LNKCTL_CLKREQ_EN);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08004502 }
4503
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00004504 tg3_test_and_report_link_chg(tp, current_link_up);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004505
4506 return 0;
4507}
4508
4509struct tg3_fiber_aneginfo {
4510 int state;
4511#define ANEG_STATE_UNKNOWN 0
4512#define ANEG_STATE_AN_ENABLE 1
4513#define ANEG_STATE_RESTART_INIT 2
4514#define ANEG_STATE_RESTART 3
4515#define ANEG_STATE_DISABLE_LINK_OK 4
4516#define ANEG_STATE_ABILITY_DETECT_INIT 5
4517#define ANEG_STATE_ABILITY_DETECT 6
4518#define ANEG_STATE_ACK_DETECT_INIT 7
4519#define ANEG_STATE_ACK_DETECT 8
4520#define ANEG_STATE_COMPLETE_ACK_INIT 9
4521#define ANEG_STATE_COMPLETE_ACK 10
4522#define ANEG_STATE_IDLE_DETECT_INIT 11
4523#define ANEG_STATE_IDLE_DETECT 12
4524#define ANEG_STATE_LINK_OK 13
4525#define ANEG_STATE_NEXT_PAGE_WAIT_INIT 14
4526#define ANEG_STATE_NEXT_PAGE_WAIT 15
4527
4528 u32 flags;
4529#define MR_AN_ENABLE 0x00000001
4530#define MR_RESTART_AN 0x00000002
4531#define MR_AN_COMPLETE 0x00000004
4532#define MR_PAGE_RX 0x00000008
4533#define MR_NP_LOADED 0x00000010
4534#define MR_TOGGLE_TX 0x00000020
4535#define MR_LP_ADV_FULL_DUPLEX 0x00000040
4536#define MR_LP_ADV_HALF_DUPLEX 0x00000080
4537#define MR_LP_ADV_SYM_PAUSE 0x00000100
4538#define MR_LP_ADV_ASYM_PAUSE 0x00000200
4539#define MR_LP_ADV_REMOTE_FAULT1 0x00000400
4540#define MR_LP_ADV_REMOTE_FAULT2 0x00000800
4541#define MR_LP_ADV_NEXT_PAGE 0x00001000
4542#define MR_TOGGLE_RX 0x00002000
4543#define MR_NP_RX 0x00004000
4544
4545#define MR_LINK_OK 0x80000000
4546
4547 unsigned long link_time, cur_time;
4548
4549 u32 ability_match_cfg;
4550 int ability_match_count;
4551
4552 char ability_match, idle_match, ack_match;
4553
4554 u32 txconfig, rxconfig;
4555#define ANEG_CFG_NP 0x00000080
4556#define ANEG_CFG_ACK 0x00000040
4557#define ANEG_CFG_RF2 0x00000020
4558#define ANEG_CFG_RF1 0x00000010
4559#define ANEG_CFG_PS2 0x00000001
4560#define ANEG_CFG_PS1 0x00008000
4561#define ANEG_CFG_HD 0x00004000
4562#define ANEG_CFG_FD 0x00002000
4563#define ANEG_CFG_INVAL 0x00001f06
4564
4565};
4566#define ANEG_OK 0
4567#define ANEG_DONE 1
4568#define ANEG_TIMER_ENAB 2
4569#define ANEG_FAILED -1
4570
4571#define ANEG_STATE_SETTLE_TIME 10000
4572
4573static int tg3_fiber_aneg_smachine(struct tg3 *tp,
4574 struct tg3_fiber_aneginfo *ap)
4575{
Matt Carlson5be73b42007-12-20 20:09:29 -08004576 u16 flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004577 unsigned long delta;
4578 u32 rx_cfg_reg;
4579 int ret;
4580
4581 if (ap->state == ANEG_STATE_UNKNOWN) {
4582 ap->rxconfig = 0;
4583 ap->link_time = 0;
4584 ap->cur_time = 0;
4585 ap->ability_match_cfg = 0;
4586 ap->ability_match_count = 0;
4587 ap->ability_match = 0;
4588 ap->idle_match = 0;
4589 ap->ack_match = 0;
4590 }
4591 ap->cur_time++;
4592
4593 if (tr32(MAC_STATUS) & MAC_STATUS_RCVD_CFG) {
4594 rx_cfg_reg = tr32(MAC_RX_AUTO_NEG);
4595
4596 if (rx_cfg_reg != ap->ability_match_cfg) {
4597 ap->ability_match_cfg = rx_cfg_reg;
4598 ap->ability_match = 0;
4599 ap->ability_match_count = 0;
4600 } else {
4601 if (++ap->ability_match_count > 1) {
4602 ap->ability_match = 1;
4603 ap->ability_match_cfg = rx_cfg_reg;
4604 }
4605 }
4606 if (rx_cfg_reg & ANEG_CFG_ACK)
4607 ap->ack_match = 1;
4608 else
4609 ap->ack_match = 0;
4610
4611 ap->idle_match = 0;
4612 } else {
4613 ap->idle_match = 1;
4614 ap->ability_match_cfg = 0;
4615 ap->ability_match_count = 0;
4616 ap->ability_match = 0;
4617 ap->ack_match = 0;
4618
4619 rx_cfg_reg = 0;
4620 }
4621
4622 ap->rxconfig = rx_cfg_reg;
4623 ret = ANEG_OK;
4624
Matt Carlson33f401a2010-04-05 10:19:27 +00004625 switch (ap->state) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004626 case ANEG_STATE_UNKNOWN:
4627 if (ap->flags & (MR_AN_ENABLE | MR_RESTART_AN))
4628 ap->state = ANEG_STATE_AN_ENABLE;
4629
4630 /* fallthru */
4631 case ANEG_STATE_AN_ENABLE:
4632 ap->flags &= ~(MR_AN_COMPLETE | MR_PAGE_RX);
4633 if (ap->flags & MR_AN_ENABLE) {
4634 ap->link_time = 0;
4635 ap->cur_time = 0;
4636 ap->ability_match_cfg = 0;
4637 ap->ability_match_count = 0;
4638 ap->ability_match = 0;
4639 ap->idle_match = 0;
4640 ap->ack_match = 0;
4641
4642 ap->state = ANEG_STATE_RESTART_INIT;
4643 } else {
4644 ap->state = ANEG_STATE_DISABLE_LINK_OK;
4645 }
4646 break;
4647
4648 case ANEG_STATE_RESTART_INIT:
4649 ap->link_time = ap->cur_time;
4650 ap->flags &= ~(MR_NP_LOADED);
4651 ap->txconfig = 0;
4652 tw32(MAC_TX_AUTO_NEG, 0);
4653 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
4654 tw32_f(MAC_MODE, tp->mac_mode);
4655 udelay(40);
4656
4657 ret = ANEG_TIMER_ENAB;
4658 ap->state = ANEG_STATE_RESTART;
4659
4660 /* fallthru */
4661 case ANEG_STATE_RESTART:
4662 delta = ap->cur_time - ap->link_time;
Matt Carlson859a588792010-04-05 10:19:28 +00004663 if (delta > ANEG_STATE_SETTLE_TIME)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004664 ap->state = ANEG_STATE_ABILITY_DETECT_INIT;
Matt Carlson859a588792010-04-05 10:19:28 +00004665 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004666 ret = ANEG_TIMER_ENAB;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004667 break;
4668
4669 case ANEG_STATE_DISABLE_LINK_OK:
4670 ret = ANEG_DONE;
4671 break;
4672
4673 case ANEG_STATE_ABILITY_DETECT_INIT:
4674 ap->flags &= ~(MR_TOGGLE_TX);
Matt Carlson5be73b42007-12-20 20:09:29 -08004675 ap->txconfig = ANEG_CFG_FD;
4676 flowctrl = tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
4677 if (flowctrl & ADVERTISE_1000XPAUSE)
4678 ap->txconfig |= ANEG_CFG_PS1;
4679 if (flowctrl & ADVERTISE_1000XPSE_ASYM)
4680 ap->txconfig |= ANEG_CFG_PS2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004681 tw32(MAC_TX_AUTO_NEG, ap->txconfig);
4682 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
4683 tw32_f(MAC_MODE, tp->mac_mode);
4684 udelay(40);
4685
4686 ap->state = ANEG_STATE_ABILITY_DETECT;
4687 break;
4688
4689 case ANEG_STATE_ABILITY_DETECT:
Matt Carlson859a588792010-04-05 10:19:28 +00004690 if (ap->ability_match != 0 && ap->rxconfig != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004691 ap->state = ANEG_STATE_ACK_DETECT_INIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004692 break;
4693
4694 case ANEG_STATE_ACK_DETECT_INIT:
4695 ap->txconfig |= ANEG_CFG_ACK;
4696 tw32(MAC_TX_AUTO_NEG, ap->txconfig);
4697 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
4698 tw32_f(MAC_MODE, tp->mac_mode);
4699 udelay(40);
4700
4701 ap->state = ANEG_STATE_ACK_DETECT;
4702
4703 /* fallthru */
4704 case ANEG_STATE_ACK_DETECT:
4705 if (ap->ack_match != 0) {
4706 if ((ap->rxconfig & ~ANEG_CFG_ACK) ==
4707 (ap->ability_match_cfg & ~ANEG_CFG_ACK)) {
4708 ap->state = ANEG_STATE_COMPLETE_ACK_INIT;
4709 } else {
4710 ap->state = ANEG_STATE_AN_ENABLE;
4711 }
4712 } else if (ap->ability_match != 0 &&
4713 ap->rxconfig == 0) {
4714 ap->state = ANEG_STATE_AN_ENABLE;
4715 }
4716 break;
4717
4718 case ANEG_STATE_COMPLETE_ACK_INIT:
4719 if (ap->rxconfig & ANEG_CFG_INVAL) {
4720 ret = ANEG_FAILED;
4721 break;
4722 }
4723 ap->flags &= ~(MR_LP_ADV_FULL_DUPLEX |
4724 MR_LP_ADV_HALF_DUPLEX |
4725 MR_LP_ADV_SYM_PAUSE |
4726 MR_LP_ADV_ASYM_PAUSE |
4727 MR_LP_ADV_REMOTE_FAULT1 |
4728 MR_LP_ADV_REMOTE_FAULT2 |
4729 MR_LP_ADV_NEXT_PAGE |
4730 MR_TOGGLE_RX |
4731 MR_NP_RX);
4732 if (ap->rxconfig & ANEG_CFG_FD)
4733 ap->flags |= MR_LP_ADV_FULL_DUPLEX;
4734 if (ap->rxconfig & ANEG_CFG_HD)
4735 ap->flags |= MR_LP_ADV_HALF_DUPLEX;
4736 if (ap->rxconfig & ANEG_CFG_PS1)
4737 ap->flags |= MR_LP_ADV_SYM_PAUSE;
4738 if (ap->rxconfig & ANEG_CFG_PS2)
4739 ap->flags |= MR_LP_ADV_ASYM_PAUSE;
4740 if (ap->rxconfig & ANEG_CFG_RF1)
4741 ap->flags |= MR_LP_ADV_REMOTE_FAULT1;
4742 if (ap->rxconfig & ANEG_CFG_RF2)
4743 ap->flags |= MR_LP_ADV_REMOTE_FAULT2;
4744 if (ap->rxconfig & ANEG_CFG_NP)
4745 ap->flags |= MR_LP_ADV_NEXT_PAGE;
4746
4747 ap->link_time = ap->cur_time;
4748
4749 ap->flags ^= (MR_TOGGLE_TX);
4750 if (ap->rxconfig & 0x0008)
4751 ap->flags |= MR_TOGGLE_RX;
4752 if (ap->rxconfig & ANEG_CFG_NP)
4753 ap->flags |= MR_NP_RX;
4754 ap->flags |= MR_PAGE_RX;
4755
4756 ap->state = ANEG_STATE_COMPLETE_ACK;
4757 ret = ANEG_TIMER_ENAB;
4758 break;
4759
4760 case ANEG_STATE_COMPLETE_ACK:
4761 if (ap->ability_match != 0 &&
4762 ap->rxconfig == 0) {
4763 ap->state = ANEG_STATE_AN_ENABLE;
4764 break;
4765 }
4766 delta = ap->cur_time - ap->link_time;
4767 if (delta > ANEG_STATE_SETTLE_TIME) {
4768 if (!(ap->flags & (MR_LP_ADV_NEXT_PAGE))) {
4769 ap->state = ANEG_STATE_IDLE_DETECT_INIT;
4770 } else {
4771 if ((ap->txconfig & ANEG_CFG_NP) == 0 &&
4772 !(ap->flags & MR_NP_RX)) {
4773 ap->state = ANEG_STATE_IDLE_DETECT_INIT;
4774 } else {
4775 ret = ANEG_FAILED;
4776 }
4777 }
4778 }
4779 break;
4780
4781 case ANEG_STATE_IDLE_DETECT_INIT:
4782 ap->link_time = ap->cur_time;
4783 tp->mac_mode &= ~MAC_MODE_SEND_CONFIGS;
4784 tw32_f(MAC_MODE, tp->mac_mode);
4785 udelay(40);
4786
4787 ap->state = ANEG_STATE_IDLE_DETECT;
4788 ret = ANEG_TIMER_ENAB;
4789 break;
4790
4791 case ANEG_STATE_IDLE_DETECT:
4792 if (ap->ability_match != 0 &&
4793 ap->rxconfig == 0) {
4794 ap->state = ANEG_STATE_AN_ENABLE;
4795 break;
4796 }
4797 delta = ap->cur_time - ap->link_time;
4798 if (delta > ANEG_STATE_SETTLE_TIME) {
4799 /* XXX another gem from the Broadcom driver :( */
4800 ap->state = ANEG_STATE_LINK_OK;
4801 }
4802 break;
4803
4804 case ANEG_STATE_LINK_OK:
4805 ap->flags |= (MR_AN_COMPLETE | MR_LINK_OK);
4806 ret = ANEG_DONE;
4807 break;
4808
4809 case ANEG_STATE_NEXT_PAGE_WAIT_INIT:
4810 /* ??? unimplemented */
4811 break;
4812
4813 case ANEG_STATE_NEXT_PAGE_WAIT:
4814 /* ??? unimplemented */
4815 break;
4816
4817 default:
4818 ret = ANEG_FAILED;
4819 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07004820 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004821
4822 return ret;
4823}
4824
Matt Carlson5be73b42007-12-20 20:09:29 -08004825static int fiber_autoneg(struct tg3 *tp, u32 *txflags, u32 *rxflags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004826{
4827 int res = 0;
4828 struct tg3_fiber_aneginfo aninfo;
4829 int status = ANEG_FAILED;
4830 unsigned int tick;
4831 u32 tmp;
4832
4833 tw32_f(MAC_TX_AUTO_NEG, 0);
4834
4835 tmp = tp->mac_mode & ~MAC_MODE_PORT_MODE_MASK;
4836 tw32_f(MAC_MODE, tmp | MAC_MODE_PORT_MODE_GMII);
4837 udelay(40);
4838
4839 tw32_f(MAC_MODE, tp->mac_mode | MAC_MODE_SEND_CONFIGS);
4840 udelay(40);
4841
4842 memset(&aninfo, 0, sizeof(aninfo));
4843 aninfo.flags |= MR_AN_ENABLE;
4844 aninfo.state = ANEG_STATE_UNKNOWN;
4845 aninfo.cur_time = 0;
4846 tick = 0;
4847 while (++tick < 195000) {
4848 status = tg3_fiber_aneg_smachine(tp, &aninfo);
4849 if (status == ANEG_DONE || status == ANEG_FAILED)
4850 break;
4851
4852 udelay(1);
4853 }
4854
4855 tp->mac_mode &= ~MAC_MODE_SEND_CONFIGS;
4856 tw32_f(MAC_MODE, tp->mac_mode);
4857 udelay(40);
4858
Matt Carlson5be73b42007-12-20 20:09:29 -08004859 *txflags = aninfo.txconfig;
4860 *rxflags = aninfo.flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004861
4862 if (status == ANEG_DONE &&
4863 (aninfo.flags & (MR_AN_COMPLETE | MR_LINK_OK |
4864 MR_LP_ADV_FULL_DUPLEX)))
4865 res = 1;
4866
4867 return res;
4868}
4869
4870static void tg3_init_bcm8002(struct tg3 *tp)
4871{
4872 u32 mac_status = tr32(MAC_STATUS);
4873 int i;
4874
4875 /* Reset when initting first time or we have a link. */
Joe Perches63c3a662011-04-26 08:12:10 +00004876 if (tg3_flag(tp, INIT_COMPLETE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004877 !(mac_status & MAC_STATUS_PCS_SYNCED))
4878 return;
4879
4880 /* Set PLL lock range. */
4881 tg3_writephy(tp, 0x16, 0x8007);
4882
4883 /* SW reset */
4884 tg3_writephy(tp, MII_BMCR, BMCR_RESET);
4885
4886 /* Wait for reset to complete. */
4887 /* XXX schedule_timeout() ... */
4888 for (i = 0; i < 500; i++)
4889 udelay(10);
4890
4891 /* Config mode; select PMA/Ch 1 regs. */
4892 tg3_writephy(tp, 0x10, 0x8411);
4893
4894 /* Enable auto-lock and comdet, select txclk for tx. */
4895 tg3_writephy(tp, 0x11, 0x0a10);
4896
4897 tg3_writephy(tp, 0x18, 0x00a0);
4898 tg3_writephy(tp, 0x16, 0x41ff);
4899
4900 /* Assert and deassert POR. */
4901 tg3_writephy(tp, 0x13, 0x0400);
4902 udelay(40);
4903 tg3_writephy(tp, 0x13, 0x0000);
4904
4905 tg3_writephy(tp, 0x11, 0x0a50);
4906 udelay(40);
4907 tg3_writephy(tp, 0x11, 0x0a10);
4908
4909 /* Wait for signal to stabilize */
4910 /* XXX schedule_timeout() ... */
4911 for (i = 0; i < 15000; i++)
4912 udelay(10);
4913
4914 /* Deselect the channel register so we can read the PHYID
4915 * later.
4916 */
4917 tg3_writephy(tp, 0x10, 0x8011);
4918}
4919
4920static int tg3_setup_fiber_hw_autoneg(struct tg3 *tp, u32 mac_status)
4921{
Matt Carlson82cd3d12007-12-20 20:09:00 -08004922 u16 flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004923 u32 sg_dig_ctrl, sg_dig_status;
4924 u32 serdes_cfg, expected_sg_dig_ctrl;
4925 int workaround, port_a;
4926 int current_link_up;
4927
4928 serdes_cfg = 0;
4929 expected_sg_dig_ctrl = 0;
4930 workaround = 0;
4931 port_a = 1;
4932 current_link_up = 0;
4933
4934 if (tp->pci_chip_rev_id != CHIPREV_ID_5704_A0 &&
4935 tp->pci_chip_rev_id != CHIPREV_ID_5704_A1) {
4936 workaround = 1;
4937 if (tr32(TG3PCI_DUAL_MAC_CTRL) & DUAL_MAC_CTRL_ID)
4938 port_a = 0;
4939
4940 /* preserve bits 0-11,13,14 for signal pre-emphasis */
4941 /* preserve bits 20-23 for voltage regulator */
4942 serdes_cfg = tr32(MAC_SERDES_CFG) & 0x00f06fff;
4943 }
4944
4945 sg_dig_ctrl = tr32(SG_DIG_CTRL);
4946
4947 if (tp->link_config.autoneg != AUTONEG_ENABLE) {
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004948 if (sg_dig_ctrl & SG_DIG_USING_HW_AUTONEG) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004949 if (workaround) {
4950 u32 val = serdes_cfg;
4951
4952 if (port_a)
4953 val |= 0xc010000;
4954 else
4955 val |= 0x4010000;
4956 tw32_f(MAC_SERDES_CFG, val);
4957 }
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004958
4959 tw32_f(SG_DIG_CTRL, SG_DIG_COMMON_SETUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004960 }
4961 if (mac_status & MAC_STATUS_PCS_SYNCED) {
4962 tg3_setup_flow_control(tp, 0, 0);
4963 current_link_up = 1;
4964 }
4965 goto out;
4966 }
4967
4968 /* Want auto-negotiation. */
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004969 expected_sg_dig_ctrl = SG_DIG_USING_HW_AUTONEG | SG_DIG_COMMON_SETUP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004970
Matt Carlson82cd3d12007-12-20 20:09:00 -08004971 flowctrl = tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
4972 if (flowctrl & ADVERTISE_1000XPAUSE)
4973 expected_sg_dig_ctrl |= SG_DIG_PAUSE_CAP;
4974 if (flowctrl & ADVERTISE_1000XPSE_ASYM)
4975 expected_sg_dig_ctrl |= SG_DIG_ASYM_PAUSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004976
4977 if (sg_dig_ctrl != expected_sg_dig_ctrl) {
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004978 if ((tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT) &&
Michael Chan3d3ebe72006-09-27 15:59:15 -07004979 tp->serdes_counter &&
4980 ((mac_status & (MAC_STATUS_PCS_SYNCED |
4981 MAC_STATUS_RCVD_CFG)) ==
4982 MAC_STATUS_PCS_SYNCED)) {
4983 tp->serdes_counter--;
4984 current_link_up = 1;
4985 goto out;
4986 }
4987restart_autoneg:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004988 if (workaround)
4989 tw32_f(MAC_SERDES_CFG, serdes_cfg | 0xc011000);
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004990 tw32_f(SG_DIG_CTRL, expected_sg_dig_ctrl | SG_DIG_SOFT_RESET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004991 udelay(5);
4992 tw32_f(SG_DIG_CTRL, expected_sg_dig_ctrl);
4993
Michael Chan3d3ebe72006-09-27 15:59:15 -07004994 tp->serdes_counter = SERDES_AN_TIMEOUT_5704S;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004995 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004996 } else if (mac_status & (MAC_STATUS_PCS_SYNCED |
4997 MAC_STATUS_SIGNAL_DET)) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07004998 sg_dig_status = tr32(SG_DIG_STATUS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004999 mac_status = tr32(MAC_STATUS);
5000
Matt Carlsonc98f6e32007-12-20 20:08:32 -08005001 if ((sg_dig_status & SG_DIG_AUTONEG_COMPLETE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005002 (mac_status & MAC_STATUS_PCS_SYNCED)) {
Matt Carlson82cd3d12007-12-20 20:09:00 -08005003 u32 local_adv = 0, remote_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005004
Matt Carlson82cd3d12007-12-20 20:09:00 -08005005 if (sg_dig_ctrl & SG_DIG_PAUSE_CAP)
5006 local_adv |= ADVERTISE_1000XPAUSE;
5007 if (sg_dig_ctrl & SG_DIG_ASYM_PAUSE)
5008 local_adv |= ADVERTISE_1000XPSE_ASYM;
5009
Matt Carlsonc98f6e32007-12-20 20:08:32 -08005010 if (sg_dig_status & SG_DIG_PARTNER_PAUSE_CAPABLE)
Matt Carlson82cd3d12007-12-20 20:09:00 -08005011 remote_adv |= LPA_1000XPAUSE;
Matt Carlsonc98f6e32007-12-20 20:08:32 -08005012 if (sg_dig_status & SG_DIG_PARTNER_ASYM_PAUSE)
Matt Carlson82cd3d12007-12-20 20:09:00 -08005013 remote_adv |= LPA_1000XPAUSE_ASYM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005014
Matt Carlson859edb22011-12-08 14:40:16 +00005015 tp->link_config.rmt_adv =
5016 mii_adv_to_ethtool_adv_x(remote_adv);
5017
Linus Torvalds1da177e2005-04-16 15:20:36 -07005018 tg3_setup_flow_control(tp, local_adv, remote_adv);
5019 current_link_up = 1;
Michael Chan3d3ebe72006-09-27 15:59:15 -07005020 tp->serdes_counter = 0;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005021 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Matt Carlsonc98f6e32007-12-20 20:08:32 -08005022 } else if (!(sg_dig_status & SG_DIG_AUTONEG_COMPLETE)) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07005023 if (tp->serdes_counter)
5024 tp->serdes_counter--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005025 else {
5026 if (workaround) {
5027 u32 val = serdes_cfg;
5028
5029 if (port_a)
5030 val |= 0xc010000;
5031 else
5032 val |= 0x4010000;
5033
5034 tw32_f(MAC_SERDES_CFG, val);
5035 }
5036
Matt Carlsonc98f6e32007-12-20 20:08:32 -08005037 tw32_f(SG_DIG_CTRL, SG_DIG_COMMON_SETUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005038 udelay(40);
5039
5040 /* Link parallel detection - link is up */
5041 /* only if we have PCS_SYNC and not */
5042 /* receiving config code words */
5043 mac_status = tr32(MAC_STATUS);
5044 if ((mac_status & MAC_STATUS_PCS_SYNCED) &&
5045 !(mac_status & MAC_STATUS_RCVD_CFG)) {
5046 tg3_setup_flow_control(tp, 0, 0);
5047 current_link_up = 1;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005048 tp->phy_flags |=
5049 TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan3d3ebe72006-09-27 15:59:15 -07005050 tp->serdes_counter =
5051 SERDES_PARALLEL_DET_TIMEOUT;
5052 } else
5053 goto restart_autoneg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005054 }
5055 }
Michael Chan3d3ebe72006-09-27 15:59:15 -07005056 } else {
5057 tp->serdes_counter = SERDES_AN_TIMEOUT_5704S;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005058 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005059 }
5060
5061out:
5062 return current_link_up;
5063}
5064
5065static int tg3_setup_fiber_by_hand(struct tg3 *tp, u32 mac_status)
5066{
5067 int current_link_up = 0;
5068
Michael Chan5cf64b8a2007-05-05 12:11:21 -07005069 if (!(mac_status & MAC_STATUS_PCS_SYNCED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005070 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005071
5072 if (tp->link_config.autoneg == AUTONEG_ENABLE) {
Matt Carlson5be73b42007-12-20 20:09:29 -08005073 u32 txflags, rxflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005074 int i;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04005075
Matt Carlson5be73b42007-12-20 20:09:29 -08005076 if (fiber_autoneg(tp, &txflags, &rxflags)) {
5077 u32 local_adv = 0, remote_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005078
Matt Carlson5be73b42007-12-20 20:09:29 -08005079 if (txflags & ANEG_CFG_PS1)
5080 local_adv |= ADVERTISE_1000XPAUSE;
5081 if (txflags & ANEG_CFG_PS2)
5082 local_adv |= ADVERTISE_1000XPSE_ASYM;
5083
5084 if (rxflags & MR_LP_ADV_SYM_PAUSE)
5085 remote_adv |= LPA_1000XPAUSE;
5086 if (rxflags & MR_LP_ADV_ASYM_PAUSE)
5087 remote_adv |= LPA_1000XPAUSE_ASYM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005088
Matt Carlson859edb22011-12-08 14:40:16 +00005089 tp->link_config.rmt_adv =
5090 mii_adv_to_ethtool_adv_x(remote_adv);
5091
Linus Torvalds1da177e2005-04-16 15:20:36 -07005092 tg3_setup_flow_control(tp, local_adv, remote_adv);
5093
Linus Torvalds1da177e2005-04-16 15:20:36 -07005094 current_link_up = 1;
5095 }
5096 for (i = 0; i < 30; i++) {
5097 udelay(20);
5098 tw32_f(MAC_STATUS,
5099 (MAC_STATUS_SYNC_CHANGED |
5100 MAC_STATUS_CFG_CHANGED));
5101 udelay(40);
5102 if ((tr32(MAC_STATUS) &
5103 (MAC_STATUS_SYNC_CHANGED |
5104 MAC_STATUS_CFG_CHANGED)) == 0)
5105 break;
5106 }
5107
5108 mac_status = tr32(MAC_STATUS);
5109 if (current_link_up == 0 &&
5110 (mac_status & MAC_STATUS_PCS_SYNCED) &&
5111 !(mac_status & MAC_STATUS_RCVD_CFG))
5112 current_link_up = 1;
5113 } else {
Matt Carlson5be73b42007-12-20 20:09:29 -08005114 tg3_setup_flow_control(tp, 0, 0);
5115
Linus Torvalds1da177e2005-04-16 15:20:36 -07005116 /* Forcing 1000FD link up. */
5117 current_link_up = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005118
5119 tw32_f(MAC_MODE, (tp->mac_mode | MAC_MODE_SEND_CONFIGS));
5120 udelay(40);
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07005121
5122 tw32_f(MAC_MODE, tp->mac_mode);
5123 udelay(40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005124 }
5125
5126out:
5127 return current_link_up;
5128}
5129
5130static int tg3_setup_fiber_phy(struct tg3 *tp, int force_reset)
5131{
5132 u32 orig_pause_cfg;
5133 u16 orig_active_speed;
5134 u8 orig_active_duplex;
5135 u32 mac_status;
5136 int current_link_up;
5137 int i;
5138
Matt Carlson8d018622007-12-20 20:05:44 -08005139 orig_pause_cfg = tp->link_config.active_flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005140 orig_active_speed = tp->link_config.active_speed;
5141 orig_active_duplex = tp->link_config.active_duplex;
5142
Joe Perches63c3a662011-04-26 08:12:10 +00005143 if (!tg3_flag(tp, HW_AUTONEG) &&
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00005144 tp->link_up &&
Joe Perches63c3a662011-04-26 08:12:10 +00005145 tg3_flag(tp, INIT_COMPLETE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005146 mac_status = tr32(MAC_STATUS);
5147 mac_status &= (MAC_STATUS_PCS_SYNCED |
5148 MAC_STATUS_SIGNAL_DET |
5149 MAC_STATUS_CFG_CHANGED |
5150 MAC_STATUS_RCVD_CFG);
5151 if (mac_status == (MAC_STATUS_PCS_SYNCED |
5152 MAC_STATUS_SIGNAL_DET)) {
5153 tw32_f(MAC_STATUS, (MAC_STATUS_SYNC_CHANGED |
5154 MAC_STATUS_CFG_CHANGED));
5155 return 0;
5156 }
5157 }
5158
5159 tw32_f(MAC_TX_AUTO_NEG, 0);
5160
5161 tp->mac_mode &= ~(MAC_MODE_PORT_MODE_MASK | MAC_MODE_HALF_DUPLEX);
5162 tp->mac_mode |= MAC_MODE_PORT_MODE_TBI;
5163 tw32_f(MAC_MODE, tp->mac_mode);
5164 udelay(40);
5165
Matt Carlson79eb6902010-02-17 15:17:03 +00005166 if (tp->phy_id == TG3_PHY_ID_BCM8002)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005167 tg3_init_bcm8002(tp);
5168
5169 /* Enable link change event even when serdes polling. */
5170 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
5171 udelay(40);
5172
5173 current_link_up = 0;
Matt Carlson859edb22011-12-08 14:40:16 +00005174 tp->link_config.rmt_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005175 mac_status = tr32(MAC_STATUS);
5176
Joe Perches63c3a662011-04-26 08:12:10 +00005177 if (tg3_flag(tp, HW_AUTONEG))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005178 current_link_up = tg3_setup_fiber_hw_autoneg(tp, mac_status);
5179 else
5180 current_link_up = tg3_setup_fiber_by_hand(tp, mac_status);
5181
Matt Carlson898a56f2009-08-28 14:02:40 +00005182 tp->napi[0].hw_status->status =
Linus Torvalds1da177e2005-04-16 15:20:36 -07005183 (SD_STATUS_UPDATED |
Matt Carlson898a56f2009-08-28 14:02:40 +00005184 (tp->napi[0].hw_status->status & ~SD_STATUS_LINK_CHG));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005185
5186 for (i = 0; i < 100; i++) {
5187 tw32_f(MAC_STATUS, (MAC_STATUS_SYNC_CHANGED |
5188 MAC_STATUS_CFG_CHANGED));
5189 udelay(5);
5190 if ((tr32(MAC_STATUS) & (MAC_STATUS_SYNC_CHANGED |
Michael Chan3d3ebe72006-09-27 15:59:15 -07005191 MAC_STATUS_CFG_CHANGED |
5192 MAC_STATUS_LNKSTATE_CHANGED)) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005193 break;
5194 }
5195
5196 mac_status = tr32(MAC_STATUS);
5197 if ((mac_status & MAC_STATUS_PCS_SYNCED) == 0) {
5198 current_link_up = 0;
Michael Chan3d3ebe72006-09-27 15:59:15 -07005199 if (tp->link_config.autoneg == AUTONEG_ENABLE &&
5200 tp->serdes_counter == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005201 tw32_f(MAC_MODE, (tp->mac_mode |
5202 MAC_MODE_SEND_CONFIGS));
5203 udelay(1);
5204 tw32_f(MAC_MODE, tp->mac_mode);
5205 }
5206 }
5207
5208 if (current_link_up == 1) {
5209 tp->link_config.active_speed = SPEED_1000;
5210 tp->link_config.active_duplex = DUPLEX_FULL;
5211 tw32(MAC_LED_CTRL, (tp->led_ctrl |
5212 LED_CTRL_LNKLED_OVERRIDE |
5213 LED_CTRL_1000MBPS_ON));
5214 } else {
Matt Carlsone7405222012-02-13 15:20:16 +00005215 tp->link_config.active_speed = SPEED_UNKNOWN;
5216 tp->link_config.active_duplex = DUPLEX_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005217 tw32(MAC_LED_CTRL, (tp->led_ctrl |
5218 LED_CTRL_LNKLED_OVERRIDE |
5219 LED_CTRL_TRAFFIC_OVERRIDE));
5220 }
5221
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00005222 if (!tg3_test_and_report_link_chg(tp, current_link_up)) {
Matt Carlson8d018622007-12-20 20:05:44 -08005223 u32 now_pause_cfg = tp->link_config.active_flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005224 if (orig_pause_cfg != now_pause_cfg ||
5225 orig_active_speed != tp->link_config.active_speed ||
5226 orig_active_duplex != tp->link_config.active_duplex)
5227 tg3_link_report(tp);
5228 }
5229
5230 return 0;
5231}
5232
Michael Chan747e8f82005-07-25 12:33:22 -07005233static int tg3_setup_fiber_mii_phy(struct tg3 *tp, int force_reset)
5234{
5235 int current_link_up, err = 0;
5236 u32 bmsr, bmcr;
5237 u16 current_speed;
5238 u8 current_duplex;
Matt Carlsonef167e22007-12-20 20:10:01 -08005239 u32 local_adv, remote_adv;
Michael Chan747e8f82005-07-25 12:33:22 -07005240
5241 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
5242 tw32_f(MAC_MODE, tp->mac_mode);
5243 udelay(40);
5244
5245 tw32(MAC_EVENT, 0);
5246
5247 tw32_f(MAC_STATUS,
5248 (MAC_STATUS_SYNC_CHANGED |
5249 MAC_STATUS_CFG_CHANGED |
5250 MAC_STATUS_MI_COMPLETION |
5251 MAC_STATUS_LNKSTATE_CHANGED));
5252 udelay(40);
5253
5254 if (force_reset)
5255 tg3_phy_reset(tp);
5256
5257 current_link_up = 0;
Matt Carlsone7405222012-02-13 15:20:16 +00005258 current_speed = SPEED_UNKNOWN;
5259 current_duplex = DUPLEX_UNKNOWN;
Matt Carlson859edb22011-12-08 14:40:16 +00005260 tp->link_config.rmt_adv = 0;
Michael Chan747e8f82005-07-25 12:33:22 -07005261
5262 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
5263 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
Michael Chand4d2c552006-03-20 17:47:20 -08005264 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
5265 if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
5266 bmsr |= BMSR_LSTATUS;
5267 else
5268 bmsr &= ~BMSR_LSTATUS;
5269 }
Michael Chan747e8f82005-07-25 12:33:22 -07005270
5271 err |= tg3_readphy(tp, MII_BMCR, &bmcr);
5272
5273 if ((tp->link_config.autoneg == AUTONEG_ENABLE) && !force_reset &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005274 (tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT)) {
Michael Chan747e8f82005-07-25 12:33:22 -07005275 /* do nothing, just check for link up at the end */
5276 } else if (tp->link_config.autoneg == AUTONEG_ENABLE) {
Matt Carlson28011cf2011-11-16 18:36:59 -05005277 u32 adv, newadv;
Michael Chan747e8f82005-07-25 12:33:22 -07005278
5279 err |= tg3_readphy(tp, MII_ADVERTISE, &adv);
Matt Carlson28011cf2011-11-16 18:36:59 -05005280 newadv = adv & ~(ADVERTISE_1000XFULL | ADVERTISE_1000XHALF |
5281 ADVERTISE_1000XPAUSE |
5282 ADVERTISE_1000XPSE_ASYM |
5283 ADVERTISE_SLCT);
Michael Chan747e8f82005-07-25 12:33:22 -07005284
Matt Carlson28011cf2011-11-16 18:36:59 -05005285 newadv |= tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
Matt Carlson37f07022011-11-17 14:30:55 +00005286 newadv |= ethtool_adv_to_mii_adv_x(tp->link_config.advertising);
Michael Chan747e8f82005-07-25 12:33:22 -07005287
Matt Carlson28011cf2011-11-16 18:36:59 -05005288 if ((newadv != adv) || !(bmcr & BMCR_ANENABLE)) {
5289 tg3_writephy(tp, MII_ADVERTISE, newadv);
Michael Chan747e8f82005-07-25 12:33:22 -07005290 bmcr |= BMCR_ANENABLE | BMCR_ANRESTART;
5291 tg3_writephy(tp, MII_BMCR, bmcr);
5292
5293 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
Michael Chan3d3ebe72006-09-27 15:59:15 -07005294 tp->serdes_counter = SERDES_AN_TIMEOUT_5714S;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005295 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07005296
5297 return err;
5298 }
5299 } else {
5300 u32 new_bmcr;
5301
5302 bmcr &= ~BMCR_SPEED1000;
5303 new_bmcr = bmcr & ~(BMCR_ANENABLE | BMCR_FULLDPLX);
5304
5305 if (tp->link_config.duplex == DUPLEX_FULL)
5306 new_bmcr |= BMCR_FULLDPLX;
5307
5308 if (new_bmcr != bmcr) {
5309 /* BMCR_SPEED1000 is a reserved bit that needs
5310 * to be set on write.
5311 */
5312 new_bmcr |= BMCR_SPEED1000;
5313
5314 /* Force a linkdown */
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00005315 if (tp->link_up) {
Michael Chan747e8f82005-07-25 12:33:22 -07005316 u32 adv;
5317
5318 err |= tg3_readphy(tp, MII_ADVERTISE, &adv);
5319 adv &= ~(ADVERTISE_1000XFULL |
5320 ADVERTISE_1000XHALF |
5321 ADVERTISE_SLCT);
5322 tg3_writephy(tp, MII_ADVERTISE, adv);
5323 tg3_writephy(tp, MII_BMCR, bmcr |
5324 BMCR_ANRESTART |
5325 BMCR_ANENABLE);
5326 udelay(10);
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00005327 tg3_carrier_off(tp);
Michael Chan747e8f82005-07-25 12:33:22 -07005328 }
5329 tg3_writephy(tp, MII_BMCR, new_bmcr);
5330 bmcr = new_bmcr;
5331 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
5332 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
Michael Chand4d2c552006-03-20 17:47:20 -08005333 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
5334 ASIC_REV_5714) {
5335 if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
5336 bmsr |= BMSR_LSTATUS;
5337 else
5338 bmsr &= ~BMSR_LSTATUS;
5339 }
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005340 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07005341 }
5342 }
5343
5344 if (bmsr & BMSR_LSTATUS) {
5345 current_speed = SPEED_1000;
5346 current_link_up = 1;
5347 if (bmcr & BMCR_FULLDPLX)
5348 current_duplex = DUPLEX_FULL;
5349 else
5350 current_duplex = DUPLEX_HALF;
5351
Matt Carlsonef167e22007-12-20 20:10:01 -08005352 local_adv = 0;
5353 remote_adv = 0;
5354
Michael Chan747e8f82005-07-25 12:33:22 -07005355 if (bmcr & BMCR_ANENABLE) {
Matt Carlsonef167e22007-12-20 20:10:01 -08005356 u32 common;
Michael Chan747e8f82005-07-25 12:33:22 -07005357
5358 err |= tg3_readphy(tp, MII_ADVERTISE, &local_adv);
5359 err |= tg3_readphy(tp, MII_LPA, &remote_adv);
5360 common = local_adv & remote_adv;
5361 if (common & (ADVERTISE_1000XHALF |
5362 ADVERTISE_1000XFULL)) {
5363 if (common & ADVERTISE_1000XFULL)
5364 current_duplex = DUPLEX_FULL;
5365 else
5366 current_duplex = DUPLEX_HALF;
Matt Carlson859edb22011-12-08 14:40:16 +00005367
5368 tp->link_config.rmt_adv =
5369 mii_adv_to_ethtool_adv_x(remote_adv);
Joe Perches63c3a662011-04-26 08:12:10 +00005370 } else if (!tg3_flag(tp, 5780_CLASS)) {
Matt Carlson57d8b882010-06-05 17:24:35 +00005371 /* Link is up via parallel detect */
Matt Carlson859a588792010-04-05 10:19:28 +00005372 } else {
Michael Chan747e8f82005-07-25 12:33:22 -07005373 current_link_up = 0;
Matt Carlson859a588792010-04-05 10:19:28 +00005374 }
Michael Chan747e8f82005-07-25 12:33:22 -07005375 }
5376 }
5377
Matt Carlsonef167e22007-12-20 20:10:01 -08005378 if (current_link_up == 1 && current_duplex == DUPLEX_FULL)
5379 tg3_setup_flow_control(tp, local_adv, remote_adv);
5380
Michael Chan747e8f82005-07-25 12:33:22 -07005381 tp->mac_mode &= ~MAC_MODE_HALF_DUPLEX;
5382 if (tp->link_config.active_duplex == DUPLEX_HALF)
5383 tp->mac_mode |= MAC_MODE_HALF_DUPLEX;
5384
5385 tw32_f(MAC_MODE, tp->mac_mode);
5386 udelay(40);
5387
5388 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
5389
5390 tp->link_config.active_speed = current_speed;
5391 tp->link_config.active_duplex = current_duplex;
5392
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00005393 tg3_test_and_report_link_chg(tp, current_link_up);
Michael Chan747e8f82005-07-25 12:33:22 -07005394 return err;
5395}
5396
5397static void tg3_serdes_parallel_detect(struct tg3 *tp)
5398{
Michael Chan3d3ebe72006-09-27 15:59:15 -07005399 if (tp->serdes_counter) {
Michael Chan747e8f82005-07-25 12:33:22 -07005400 /* Give autoneg time to complete. */
Michael Chan3d3ebe72006-09-27 15:59:15 -07005401 tp->serdes_counter--;
Michael Chan747e8f82005-07-25 12:33:22 -07005402 return;
5403 }
Matt Carlsonc6cdf432010-04-05 10:19:26 +00005404
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00005405 if (!tp->link_up &&
Michael Chan747e8f82005-07-25 12:33:22 -07005406 (tp->link_config.autoneg == AUTONEG_ENABLE)) {
5407 u32 bmcr;
5408
5409 tg3_readphy(tp, MII_BMCR, &bmcr);
5410 if (bmcr & BMCR_ANENABLE) {
5411 u32 phy1, phy2;
5412
5413 /* Select shadow register 0x1f */
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00005414 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x7c00);
5415 tg3_readphy(tp, MII_TG3_MISC_SHDW, &phy1);
Michael Chan747e8f82005-07-25 12:33:22 -07005416
5417 /* Select expansion interrupt status register */
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00005418 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
5419 MII_TG3_DSP_EXP1_INT_STAT);
5420 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &phy2);
5421 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &phy2);
Michael Chan747e8f82005-07-25 12:33:22 -07005422
5423 if ((phy1 & 0x10) && !(phy2 & 0x20)) {
5424 /* We have signal detect and not receiving
5425 * config code words, link is up by parallel
5426 * detection.
5427 */
5428
5429 bmcr &= ~BMCR_ANENABLE;
5430 bmcr |= BMCR_SPEED1000 | BMCR_FULLDPLX;
5431 tg3_writephy(tp, MII_BMCR, bmcr);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005432 tp->phy_flags |= TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07005433 }
5434 }
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00005435 } else if (tp->link_up &&
Matt Carlson859a588792010-04-05 10:19:28 +00005436 (tp->link_config.autoneg == AUTONEG_ENABLE) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005437 (tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT)) {
Michael Chan747e8f82005-07-25 12:33:22 -07005438 u32 phy2;
5439
5440 /* Select expansion interrupt status register */
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00005441 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
5442 MII_TG3_DSP_EXP1_INT_STAT);
5443 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &phy2);
Michael Chan747e8f82005-07-25 12:33:22 -07005444 if (phy2 & 0x20) {
5445 u32 bmcr;
5446
5447 /* Config code words received, turn on autoneg. */
5448 tg3_readphy(tp, MII_BMCR, &bmcr);
5449 tg3_writephy(tp, MII_BMCR, bmcr | BMCR_ANENABLE);
5450
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005451 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07005452
5453 }
5454 }
5455}
5456
Linus Torvalds1da177e2005-04-16 15:20:36 -07005457static int tg3_setup_phy(struct tg3 *tp, int force_reset)
5458{
Matt Carlsonf2096f92011-04-05 14:22:48 +00005459 u32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005460 int err;
5461
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005462 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005463 err = tg3_setup_fiber_phy(tp, force_reset);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00005464 else if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
Michael Chan747e8f82005-07-25 12:33:22 -07005465 err = tg3_setup_fiber_mii_phy(tp, force_reset);
Matt Carlson859a588792010-04-05 10:19:28 +00005466 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07005467 err = tg3_setup_copper_phy(tp, force_reset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005468
Matt Carlsonbcb37f62008-11-03 16:52:09 -08005469 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX) {
Matt Carlsonf2096f92011-04-05 14:22:48 +00005470 u32 scale;
Matt Carlsonaa6c91f2007-11-12 21:18:04 -08005471
5472 val = tr32(TG3_CPMU_CLCK_STAT) & CPMU_CLCK_STAT_MAC_CLCK_MASK;
5473 if (val == CPMU_CLCK_STAT_MAC_CLCK_62_5)
5474 scale = 65;
5475 else if (val == CPMU_CLCK_STAT_MAC_CLCK_6_25)
5476 scale = 6;
5477 else
5478 scale = 12;
5479
5480 val = tr32(GRC_MISC_CFG) & ~GRC_MISC_CFG_PRESCALAR_MASK;
5481 val |= (scale << GRC_MISC_CFG_PRESCALAR_SHIFT);
5482 tw32(GRC_MISC_CFG, val);
5483 }
5484
Matt Carlsonf2096f92011-04-05 14:22:48 +00005485 val = (2 << TX_LENGTHS_IPG_CRS_SHIFT) |
5486 (6 << TX_LENGTHS_IPG_SHIFT);
5487 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
5488 val |= tr32(MAC_TX_LENGTHS) &
5489 (TX_LENGTHS_JMB_FRM_LEN_MSK |
5490 TX_LENGTHS_CNT_DWN_VAL_MSK);
5491
Linus Torvalds1da177e2005-04-16 15:20:36 -07005492 if (tp->link_config.active_speed == SPEED_1000 &&
5493 tp->link_config.active_duplex == DUPLEX_HALF)
Matt Carlsonf2096f92011-04-05 14:22:48 +00005494 tw32(MAC_TX_LENGTHS, val |
5495 (0xff << TX_LENGTHS_SLOT_TIME_SHIFT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005496 else
Matt Carlsonf2096f92011-04-05 14:22:48 +00005497 tw32(MAC_TX_LENGTHS, val |
5498 (32 << TX_LENGTHS_SLOT_TIME_SHIFT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005499
Joe Perches63c3a662011-04-26 08:12:10 +00005500 if (!tg3_flag(tp, 5705_PLUS)) {
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00005501 if (tp->link_up) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005502 tw32(HOSTCC_STAT_COAL_TICKS,
David S. Miller15f98502005-05-18 22:49:26 -07005503 tp->coal.stats_block_coalesce_usecs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005504 } else {
5505 tw32(HOSTCC_STAT_COAL_TICKS, 0);
5506 }
5507 }
5508
Joe Perches63c3a662011-04-26 08:12:10 +00005509 if (tg3_flag(tp, ASPM_WORKAROUND)) {
Matt Carlsonf2096f92011-04-05 14:22:48 +00005510 val = tr32(PCIE_PWR_MGMT_THRESH);
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00005511 if (!tp->link_up)
Matt Carlson8ed5d972007-05-07 00:25:49 -07005512 val = (val & ~PCIE_PWR_MGMT_L1_THRESH_MSK) |
5513 tp->pwrmgmt_thresh;
5514 else
5515 val |= PCIE_PWR_MGMT_L1_THRESH_MSK;
5516 tw32(PCIE_PWR_MGMT_THRESH, val);
5517 }
5518
Linus Torvalds1da177e2005-04-16 15:20:36 -07005519 return err;
5520}
5521
Matt Carlsonbe947302012-12-03 19:36:57 +00005522/* tp->lock must be held */
5523static void tg3_refclk_write(struct tg3 *tp, u64 newval)
5524{
5525 tw32(TG3_EAV_REF_CLCK_CTL, TG3_EAV_REF_CLCK_CTL_STOP);
5526 tw32(TG3_EAV_REF_CLCK_LSB, newval & 0xffffffff);
5527 tw32(TG3_EAV_REF_CLCK_MSB, newval >> 32);
5528 tw32_f(TG3_EAV_REF_CLCK_CTL, TG3_EAV_REF_CLCK_CTL_RESUME);
5529}
5530
5531/* tp->lock must be held */
5532static void tg3_ptp_init(struct tg3 *tp)
5533{
5534 if (!tg3_flag(tp, PTP_CAPABLE))
5535 return;
5536
5537 /* Initialize the hardware clock to the system time. */
5538 tg3_refclk_write(tp, ktime_to_ns(ktime_get_real()));
5539 tp->ptp_adjust = 0;
5540}
5541
5542/* tp->lock must be held */
5543static void tg3_ptp_resume(struct tg3 *tp)
5544{
5545 if (!tg3_flag(tp, PTP_CAPABLE))
5546 return;
5547
5548 tg3_refclk_write(tp, ktime_to_ns(ktime_get_real()) + tp->ptp_adjust);
5549 tp->ptp_adjust = 0;
5550}
5551
5552static void tg3_ptp_fini(struct tg3 *tp)
5553{
5554 if (!tg3_flag(tp, PTP_CAPABLE) || !tp->ptp_clock)
5555 return;
5556
5557 tp->ptp_clock = NULL;
5558 tp->ptp_adjust = 0;
5559}
5560
Matt Carlson66cfd1b2010-09-30 10:34:30 +00005561static inline int tg3_irq_sync(struct tg3 *tp)
5562{
5563 return tp->irq_sync;
5564}
5565
Matt Carlson97bd8e42011-04-13 11:05:04 +00005566static inline void tg3_rd32_loop(struct tg3 *tp, u32 *dst, u32 off, u32 len)
5567{
5568 int i;
5569
5570 dst = (u32 *)((u8 *)dst + off);
5571 for (i = 0; i < len; i += sizeof(u32))
5572 *dst++ = tr32(off + i);
5573}
5574
5575static void tg3_dump_legacy_regs(struct tg3 *tp, u32 *regs)
5576{
5577 tg3_rd32_loop(tp, regs, TG3PCI_VENDOR, 0xb0);
5578 tg3_rd32_loop(tp, regs, MAILBOX_INTERRUPT_0, 0x200);
5579 tg3_rd32_loop(tp, regs, MAC_MODE, 0x4f0);
5580 tg3_rd32_loop(tp, regs, SNDDATAI_MODE, 0xe0);
5581 tg3_rd32_loop(tp, regs, SNDDATAC_MODE, 0x04);
5582 tg3_rd32_loop(tp, regs, SNDBDS_MODE, 0x80);
5583 tg3_rd32_loop(tp, regs, SNDBDI_MODE, 0x48);
5584 tg3_rd32_loop(tp, regs, SNDBDC_MODE, 0x04);
5585 tg3_rd32_loop(tp, regs, RCVLPC_MODE, 0x20);
5586 tg3_rd32_loop(tp, regs, RCVLPC_SELLST_BASE, 0x15c);
5587 tg3_rd32_loop(tp, regs, RCVDBDI_MODE, 0x0c);
5588 tg3_rd32_loop(tp, regs, RCVDBDI_JUMBO_BD, 0x3c);
5589 tg3_rd32_loop(tp, regs, RCVDBDI_BD_PROD_IDX_0, 0x44);
5590 tg3_rd32_loop(tp, regs, RCVDCC_MODE, 0x04);
5591 tg3_rd32_loop(tp, regs, RCVBDI_MODE, 0x20);
5592 tg3_rd32_loop(tp, regs, RCVCC_MODE, 0x14);
5593 tg3_rd32_loop(tp, regs, RCVLSC_MODE, 0x08);
5594 tg3_rd32_loop(tp, regs, MBFREE_MODE, 0x08);
5595 tg3_rd32_loop(tp, regs, HOSTCC_MODE, 0x100);
5596
Joe Perches63c3a662011-04-26 08:12:10 +00005597 if (tg3_flag(tp, SUPPORT_MSIX))
Matt Carlson97bd8e42011-04-13 11:05:04 +00005598 tg3_rd32_loop(tp, regs, HOSTCC_RXCOL_TICKS_VEC1, 0x180);
5599
5600 tg3_rd32_loop(tp, regs, MEMARB_MODE, 0x10);
5601 tg3_rd32_loop(tp, regs, BUFMGR_MODE, 0x58);
5602 tg3_rd32_loop(tp, regs, RDMAC_MODE, 0x08);
5603 tg3_rd32_loop(tp, regs, WDMAC_MODE, 0x08);
5604 tg3_rd32_loop(tp, regs, RX_CPU_MODE, 0x04);
5605 tg3_rd32_loop(tp, regs, RX_CPU_STATE, 0x04);
5606 tg3_rd32_loop(tp, regs, RX_CPU_PGMCTR, 0x04);
5607 tg3_rd32_loop(tp, regs, RX_CPU_HWBKPT, 0x04);
5608
Joe Perches63c3a662011-04-26 08:12:10 +00005609 if (!tg3_flag(tp, 5705_PLUS)) {
Matt Carlson97bd8e42011-04-13 11:05:04 +00005610 tg3_rd32_loop(tp, regs, TX_CPU_MODE, 0x04);
5611 tg3_rd32_loop(tp, regs, TX_CPU_STATE, 0x04);
5612 tg3_rd32_loop(tp, regs, TX_CPU_PGMCTR, 0x04);
5613 }
5614
5615 tg3_rd32_loop(tp, regs, GRCMBOX_INTERRUPT_0, 0x110);
5616 tg3_rd32_loop(tp, regs, FTQ_RESET, 0x120);
5617 tg3_rd32_loop(tp, regs, MSGINT_MODE, 0x0c);
5618 tg3_rd32_loop(tp, regs, DMAC_MODE, 0x04);
5619 tg3_rd32_loop(tp, regs, GRC_MODE, 0x4c);
5620
Joe Perches63c3a662011-04-26 08:12:10 +00005621 if (tg3_flag(tp, NVRAM))
Matt Carlson97bd8e42011-04-13 11:05:04 +00005622 tg3_rd32_loop(tp, regs, NVRAM_CMD, 0x24);
5623}
5624
5625static void tg3_dump_state(struct tg3 *tp)
5626{
5627 int i;
5628 u32 *regs;
5629
5630 regs = kzalloc(TG3_REG_BLK_SIZE, GFP_ATOMIC);
5631 if (!regs) {
5632 netdev_err(tp->dev, "Failed allocating register dump buffer\n");
5633 return;
5634 }
5635
Joe Perches63c3a662011-04-26 08:12:10 +00005636 if (tg3_flag(tp, PCI_EXPRESS)) {
Matt Carlson97bd8e42011-04-13 11:05:04 +00005637 /* Read up to but not including private PCI registers */
5638 for (i = 0; i < TG3_PCIE_TLDLPL_PORT; i += sizeof(u32))
5639 regs[i / sizeof(u32)] = tr32(i);
5640 } else
5641 tg3_dump_legacy_regs(tp, regs);
5642
5643 for (i = 0; i < TG3_REG_BLK_SIZE / sizeof(u32); i += 4) {
5644 if (!regs[i + 0] && !regs[i + 1] &&
5645 !regs[i + 2] && !regs[i + 3])
5646 continue;
5647
5648 netdev_err(tp->dev, "0x%08x: 0x%08x, 0x%08x, 0x%08x, 0x%08x\n",
5649 i * 4,
5650 regs[i + 0], regs[i + 1], regs[i + 2], regs[i + 3]);
5651 }
5652
5653 kfree(regs);
5654
5655 for (i = 0; i < tp->irq_cnt; i++) {
5656 struct tg3_napi *tnapi = &tp->napi[i];
5657
5658 /* SW status block */
5659 netdev_err(tp->dev,
5660 "%d: Host status block [%08x:%08x:(%04x:%04x:%04x):(%04x:%04x)]\n",
5661 i,
5662 tnapi->hw_status->status,
5663 tnapi->hw_status->status_tag,
5664 tnapi->hw_status->rx_jumbo_consumer,
5665 tnapi->hw_status->rx_consumer,
5666 tnapi->hw_status->rx_mini_consumer,
5667 tnapi->hw_status->idx[0].rx_producer,
5668 tnapi->hw_status->idx[0].tx_consumer);
5669
5670 netdev_err(tp->dev,
5671 "%d: NAPI info [%08x:%08x:(%04x:%04x:%04x):%04x:(%04x:%04x:%04x:%04x)]\n",
5672 i,
5673 tnapi->last_tag, tnapi->last_irq_tag,
5674 tnapi->tx_prod, tnapi->tx_cons, tnapi->tx_pending,
5675 tnapi->rx_rcb_ptr,
5676 tnapi->prodring.rx_std_prod_idx,
5677 tnapi->prodring.rx_std_cons_idx,
5678 tnapi->prodring.rx_jmb_prod_idx,
5679 tnapi->prodring.rx_jmb_cons_idx);
5680 }
5681}
5682
Michael Chandf3e6542006-05-26 17:48:07 -07005683/* This is called whenever we suspect that the system chipset is re-
5684 * ordering the sequence of MMIO to the tx send mailbox. The symptom
5685 * is bogus tx completions. We try to recover by setting the
5686 * TG3_FLAG_MBOX_WRITE_REORDER flag and resetting the chip later
5687 * in the workqueue.
5688 */
5689static void tg3_tx_recover(struct tg3 *tp)
5690{
Joe Perches63c3a662011-04-26 08:12:10 +00005691 BUG_ON(tg3_flag(tp, MBOX_WRITE_REORDER) ||
Michael Chandf3e6542006-05-26 17:48:07 -07005692 tp->write32_tx_mbox == tg3_write_indirect_mbox);
5693
Matt Carlson5129c3a2010-04-05 10:19:23 +00005694 netdev_warn(tp->dev,
5695 "The system may be re-ordering memory-mapped I/O "
5696 "cycles to the network device, attempting to recover. "
5697 "Please report the problem to the driver maintainer "
5698 "and include system chipset information.\n");
Michael Chandf3e6542006-05-26 17:48:07 -07005699
5700 spin_lock(&tp->lock);
Joe Perches63c3a662011-04-26 08:12:10 +00005701 tg3_flag_set(tp, TX_RECOVERY_PENDING);
Michael Chandf3e6542006-05-26 17:48:07 -07005702 spin_unlock(&tp->lock);
5703}
5704
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005705static inline u32 tg3_tx_avail(struct tg3_napi *tnapi)
Michael Chan1b2a7202006-08-07 21:46:02 -07005706{
Matt Carlsonf65aac12010-08-02 11:26:03 +00005707 /* Tell compiler to fetch tx indices from memory. */
5708 barrier();
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005709 return tnapi->tx_pending -
5710 ((tnapi->tx_prod - tnapi->tx_cons) & (TG3_TX_RING_SIZE - 1));
Michael Chan1b2a7202006-08-07 21:46:02 -07005711}
5712
Linus Torvalds1da177e2005-04-16 15:20:36 -07005713/* Tigon3 never reports partial packet sends. So we do not
5714 * need special logic to handle SKBs that have not had all
5715 * of their frags sent yet, like SunGEM does.
5716 */
Matt Carlson17375d22009-08-28 14:02:18 +00005717static void tg3_tx(struct tg3_napi *tnapi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005718{
Matt Carlson17375d22009-08-28 14:02:18 +00005719 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00005720 u32 hw_idx = tnapi->hw_status->idx[0].tx_consumer;
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005721 u32 sw_idx = tnapi->tx_cons;
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005722 struct netdev_queue *txq;
5723 int index = tnapi - tp->napi;
Tom Herbert298376d2011-11-28 16:33:30 +00005724 unsigned int pkts_compl = 0, bytes_compl = 0;
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005725
Joe Perches63c3a662011-04-26 08:12:10 +00005726 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005727 index--;
5728
5729 txq = netdev_get_tx_queue(tp->dev, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005730
5731 while (sw_idx != hw_idx) {
Matt Carlsondf8944c2011-07-27 14:20:46 +00005732 struct tg3_tx_ring_info *ri = &tnapi->tx_buffers[sw_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07005733 struct sk_buff *skb = ri->skb;
Michael Chandf3e6542006-05-26 17:48:07 -07005734 int i, tx_bug = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005735
Michael Chandf3e6542006-05-26 17:48:07 -07005736 if (unlikely(skb == NULL)) {
5737 tg3_tx_recover(tp);
5738 return;
5739 }
5740
Alexander Duyckf4188d82009-12-02 16:48:38 +00005741 pci_unmap_single(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005742 dma_unmap_addr(ri, mapping),
Alexander Duyckf4188d82009-12-02 16:48:38 +00005743 skb_headlen(skb),
5744 PCI_DMA_TODEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005745
5746 ri->skb = NULL;
5747
Matt Carlsone01ee142011-07-27 14:20:50 +00005748 while (ri->fragmented) {
5749 ri->fragmented = false;
5750 sw_idx = NEXT_TX(sw_idx);
5751 ri = &tnapi->tx_buffers[sw_idx];
5752 }
5753
Linus Torvalds1da177e2005-04-16 15:20:36 -07005754 sw_idx = NEXT_TX(sw_idx);
5755
5756 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005757 ri = &tnapi->tx_buffers[sw_idx];
Michael Chandf3e6542006-05-26 17:48:07 -07005758 if (unlikely(ri->skb != NULL || sw_idx == hw_idx))
5759 tx_bug = 1;
Alexander Duyckf4188d82009-12-02 16:48:38 +00005760
5761 pci_unmap_page(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005762 dma_unmap_addr(ri, mapping),
Eric Dumazet9e903e02011-10-18 21:00:24 +00005763 skb_frag_size(&skb_shinfo(skb)->frags[i]),
Alexander Duyckf4188d82009-12-02 16:48:38 +00005764 PCI_DMA_TODEVICE);
Matt Carlsone01ee142011-07-27 14:20:50 +00005765
5766 while (ri->fragmented) {
5767 ri->fragmented = false;
5768 sw_idx = NEXT_TX(sw_idx);
5769 ri = &tnapi->tx_buffers[sw_idx];
5770 }
5771
Linus Torvalds1da177e2005-04-16 15:20:36 -07005772 sw_idx = NEXT_TX(sw_idx);
5773 }
5774
Tom Herbert298376d2011-11-28 16:33:30 +00005775 pkts_compl++;
5776 bytes_compl += skb->len;
5777
David S. Millerf47c11e2005-06-24 20:18:35 -07005778 dev_kfree_skb(skb);
Michael Chandf3e6542006-05-26 17:48:07 -07005779
5780 if (unlikely(tx_bug)) {
5781 tg3_tx_recover(tp);
5782 return;
5783 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005784 }
5785
Tom Herbert5cb917b2012-03-05 19:53:50 +00005786 netdev_tx_completed_queue(txq, pkts_compl, bytes_compl);
Tom Herbert298376d2011-11-28 16:33:30 +00005787
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005788 tnapi->tx_cons = sw_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005789
Michael Chan1b2a7202006-08-07 21:46:02 -07005790 /* Need to make the tx_cons update visible to tg3_start_xmit()
5791 * before checking for netif_queue_stopped(). Without the
5792 * memory barrier, there is a small possibility that tg3_start_xmit()
5793 * will miss it and cause the queue to be stopped forever.
5794 */
5795 smp_mb();
5796
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005797 if (unlikely(netif_tx_queue_stopped(txq) &&
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005798 (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi)))) {
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005799 __netif_tx_lock(txq, smp_processor_id());
5800 if (netif_tx_queue_stopped(txq) &&
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005801 (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi)))
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005802 netif_tx_wake_queue(txq);
5803 __netif_tx_unlock(txq);
Michael Chan51b91462005-09-01 17:41:28 -07005804 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005805}
5806
Eric Dumazet8d4057a2012-04-27 00:34:49 +00005807static void tg3_frag_free(bool is_frag, void *data)
5808{
5809 if (is_frag)
5810 put_page(virt_to_head_page(data));
5811 else
5812 kfree(data);
5813}
5814
Eric Dumazet9205fd92011-11-18 06:47:01 +00005815static void tg3_rx_data_free(struct tg3 *tp, struct ring_info *ri, u32 map_sz)
Matt Carlson2b2cdb62009-11-13 13:03:48 +00005816{
Eric Dumazet8d4057a2012-04-27 00:34:49 +00005817 unsigned int skb_size = SKB_DATA_ALIGN(map_sz + TG3_RX_OFFSET(tp)) +
5818 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
5819
Eric Dumazet9205fd92011-11-18 06:47:01 +00005820 if (!ri->data)
Matt Carlson2b2cdb62009-11-13 13:03:48 +00005821 return;
5822
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005823 pci_unmap_single(tp->pdev, dma_unmap_addr(ri, mapping),
Matt Carlson2b2cdb62009-11-13 13:03:48 +00005824 map_sz, PCI_DMA_FROMDEVICE);
Eric Dumazeta1e8b3072012-05-18 21:33:39 +00005825 tg3_frag_free(skb_size <= PAGE_SIZE, ri->data);
Eric Dumazet9205fd92011-11-18 06:47:01 +00005826 ri->data = NULL;
Matt Carlson2b2cdb62009-11-13 13:03:48 +00005827}
5828
Eric Dumazet8d4057a2012-04-27 00:34:49 +00005829
Linus Torvalds1da177e2005-04-16 15:20:36 -07005830/* Returns size of skb allocated or < 0 on error.
5831 *
5832 * We only need to fill in the address because the other members
5833 * of the RX descriptor are invariant, see tg3_init_rings.
5834 *
5835 * Note the purposeful assymetry of cpu vs. chip accesses. For
5836 * posting buffers we only dirty the first cache line of the RX
5837 * descriptor (containing the address). Whereas for the RX status
5838 * buffers the cpu only reads the last cacheline of the RX descriptor
5839 * (to fetch the error flags, vlan tag, checksum, and opaque cookie).
5840 */
Eric Dumazet9205fd92011-11-18 06:47:01 +00005841static int tg3_alloc_rx_data(struct tg3 *tp, struct tg3_rx_prodring_set *tpr,
Eric Dumazet8d4057a2012-04-27 00:34:49 +00005842 u32 opaque_key, u32 dest_idx_unmasked,
5843 unsigned int *frag_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005844{
5845 struct tg3_rx_buffer_desc *desc;
Matt Carlsonf94e2902010-10-14 10:37:42 +00005846 struct ring_info *map;
Eric Dumazet9205fd92011-11-18 06:47:01 +00005847 u8 *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005848 dma_addr_t mapping;
Eric Dumazet9205fd92011-11-18 06:47:01 +00005849 int skb_size, data_size, dest_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005850
Linus Torvalds1da177e2005-04-16 15:20:36 -07005851 switch (opaque_key) {
5852 case RXD_OPAQUE_RING_STD:
Matt Carlson2c49a442010-09-30 10:34:35 +00005853 dest_idx = dest_idx_unmasked & tp->rx_std_ring_mask;
Matt Carlson21f581a2009-08-28 14:00:25 +00005854 desc = &tpr->rx_std[dest_idx];
5855 map = &tpr->rx_std_buffers[dest_idx];
Eric Dumazet9205fd92011-11-18 06:47:01 +00005856 data_size = tp->rx_pkt_map_sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005857 break;
5858
5859 case RXD_OPAQUE_RING_JUMBO:
Matt Carlson2c49a442010-09-30 10:34:35 +00005860 dest_idx = dest_idx_unmasked & tp->rx_jmb_ring_mask;
Matt Carlson79ed5ac2009-08-28 14:00:55 +00005861 desc = &tpr->rx_jmb[dest_idx].std;
Matt Carlson21f581a2009-08-28 14:00:25 +00005862 map = &tpr->rx_jmb_buffers[dest_idx];
Eric Dumazet9205fd92011-11-18 06:47:01 +00005863 data_size = TG3_RX_JMB_MAP_SZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005864 break;
5865
5866 default:
5867 return -EINVAL;
Stephen Hemminger855e1112008-04-16 16:37:28 -07005868 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005869
5870 /* Do not overwrite any of the map or rp information
5871 * until we are sure we can commit to a new buffer.
5872 *
5873 * Callers depend upon this behavior and assume that
5874 * we leave everything unchanged if we fail.
5875 */
Eric Dumazet9205fd92011-11-18 06:47:01 +00005876 skb_size = SKB_DATA_ALIGN(data_size + TG3_RX_OFFSET(tp)) +
5877 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
Eric Dumazeta1e8b3072012-05-18 21:33:39 +00005878 if (skb_size <= PAGE_SIZE) {
5879 data = netdev_alloc_frag(skb_size);
5880 *frag_size = skb_size;
Eric Dumazet8d4057a2012-04-27 00:34:49 +00005881 } else {
5882 data = kmalloc(skb_size, GFP_ATOMIC);
5883 *frag_size = 0;
5884 }
Eric Dumazet9205fd92011-11-18 06:47:01 +00005885 if (!data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005886 return -ENOMEM;
5887
Eric Dumazet9205fd92011-11-18 06:47:01 +00005888 mapping = pci_map_single(tp->pdev,
5889 data + TG3_RX_OFFSET(tp),
5890 data_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005891 PCI_DMA_FROMDEVICE);
Eric Dumazet8d4057a2012-04-27 00:34:49 +00005892 if (unlikely(pci_dma_mapping_error(tp->pdev, mapping))) {
Eric Dumazeta1e8b3072012-05-18 21:33:39 +00005893 tg3_frag_free(skb_size <= PAGE_SIZE, data);
Matt Carlsona21771d2009-11-02 14:25:31 +00005894 return -EIO;
5895 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005896
Eric Dumazet9205fd92011-11-18 06:47:01 +00005897 map->data = data;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005898 dma_unmap_addr_set(map, mapping, mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005899
Linus Torvalds1da177e2005-04-16 15:20:36 -07005900 desc->addr_hi = ((u64)mapping >> 32);
5901 desc->addr_lo = ((u64)mapping & 0xffffffff);
5902
Eric Dumazet9205fd92011-11-18 06:47:01 +00005903 return data_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005904}
5905
5906/* We only need to move over in the address because the other
5907 * members of the RX descriptor are invariant. See notes above
Eric Dumazet9205fd92011-11-18 06:47:01 +00005908 * tg3_alloc_rx_data for full details.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005909 */
Matt Carlsona3896162009-11-13 13:03:44 +00005910static void tg3_recycle_rx(struct tg3_napi *tnapi,
5911 struct tg3_rx_prodring_set *dpr,
5912 u32 opaque_key, int src_idx,
5913 u32 dest_idx_unmasked)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005914{
Matt Carlson17375d22009-08-28 14:02:18 +00005915 struct tg3 *tp = tnapi->tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005916 struct tg3_rx_buffer_desc *src_desc, *dest_desc;
5917 struct ring_info *src_map, *dest_map;
Matt Carlson8fea32b2010-09-15 08:59:58 +00005918 struct tg3_rx_prodring_set *spr = &tp->napi[0].prodring;
Matt Carlsonc6cdf432010-04-05 10:19:26 +00005919 int dest_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005920
5921 switch (opaque_key) {
5922 case RXD_OPAQUE_RING_STD:
Matt Carlson2c49a442010-09-30 10:34:35 +00005923 dest_idx = dest_idx_unmasked & tp->rx_std_ring_mask;
Matt Carlsona3896162009-11-13 13:03:44 +00005924 dest_desc = &dpr->rx_std[dest_idx];
5925 dest_map = &dpr->rx_std_buffers[dest_idx];
5926 src_desc = &spr->rx_std[src_idx];
5927 src_map = &spr->rx_std_buffers[src_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07005928 break;
5929
5930 case RXD_OPAQUE_RING_JUMBO:
Matt Carlson2c49a442010-09-30 10:34:35 +00005931 dest_idx = dest_idx_unmasked & tp->rx_jmb_ring_mask;
Matt Carlsona3896162009-11-13 13:03:44 +00005932 dest_desc = &dpr->rx_jmb[dest_idx].std;
5933 dest_map = &dpr->rx_jmb_buffers[dest_idx];
5934 src_desc = &spr->rx_jmb[src_idx].std;
5935 src_map = &spr->rx_jmb_buffers[src_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07005936 break;
5937
5938 default:
5939 return;
Stephen Hemminger855e1112008-04-16 16:37:28 -07005940 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005941
Eric Dumazet9205fd92011-11-18 06:47:01 +00005942 dest_map->data = src_map->data;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005943 dma_unmap_addr_set(dest_map, mapping,
5944 dma_unmap_addr(src_map, mapping));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005945 dest_desc->addr_hi = src_desc->addr_hi;
5946 dest_desc->addr_lo = src_desc->addr_lo;
Matt Carlsone92967b2010-02-12 14:47:06 +00005947
5948 /* Ensure that the update to the skb happens after the physical
5949 * addresses have been transferred to the new BD location.
5950 */
5951 smp_wmb();
5952
Eric Dumazet9205fd92011-11-18 06:47:01 +00005953 src_map->data = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005954}
5955
Linus Torvalds1da177e2005-04-16 15:20:36 -07005956/* The RX ring scheme is composed of multiple rings which post fresh
5957 * buffers to the chip, and one special ring the chip uses to report
5958 * status back to the host.
5959 *
5960 * The special ring reports the status of received packets to the
5961 * host. The chip does not write into the original descriptor the
5962 * RX buffer was obtained from. The chip simply takes the original
5963 * descriptor as provided by the host, updates the status and length
5964 * field, then writes this into the next status ring entry.
5965 *
5966 * Each ring the host uses to post buffers to the chip is described
5967 * by a TG3_BDINFO entry in the chips SRAM area. When a packet arrives,
5968 * it is first placed into the on-chip ram. When the packet's length
5969 * is known, it walks down the TG3_BDINFO entries to select the ring.
5970 * Each TG3_BDINFO specifies a MAXLEN field and the first TG3_BDINFO
5971 * which is within the range of the new packet's length is chosen.
5972 *
5973 * The "separate ring for rx status" scheme may sound queer, but it makes
5974 * sense from a cache coherency perspective. If only the host writes
5975 * to the buffer post rings, and only the chip writes to the rx status
5976 * rings, then cache lines never move beyond shared-modified state.
5977 * If both the host and chip were to write into the same ring, cache line
5978 * eviction could occur since both entities want it in an exclusive state.
5979 */
Matt Carlson17375d22009-08-28 14:02:18 +00005980static int tg3_rx(struct tg3_napi *tnapi, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005981{
Matt Carlson17375d22009-08-28 14:02:18 +00005982 struct tg3 *tp = tnapi->tp;
Michael Chanf92905d2006-06-29 20:14:29 -07005983 u32 work_mask, rx_std_posted = 0;
Matt Carlson43619352009-11-13 13:03:47 +00005984 u32 std_prod_idx, jmb_prod_idx;
Matt Carlson72334482009-08-28 14:03:01 +00005985 u32 sw_idx = tnapi->rx_rcb_ptr;
Michael Chan483ba502005-04-25 15:14:03 -07005986 u16 hw_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005987 int received;
Matt Carlson8fea32b2010-09-15 08:59:58 +00005988 struct tg3_rx_prodring_set *tpr = &tnapi->prodring;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005989
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00005990 hw_idx = *(tnapi->rx_rcb_prod_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005991 /*
5992 * We need to order the read of hw_idx and the read of
5993 * the opaque cookie.
5994 */
5995 rmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005996 work_mask = 0;
5997 received = 0;
Matt Carlson43619352009-11-13 13:03:47 +00005998 std_prod_idx = tpr->rx_std_prod_idx;
5999 jmb_prod_idx = tpr->rx_jmb_prod_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006000 while (sw_idx != hw_idx && budget > 0) {
Matt Carlsonafc081f2009-11-13 13:03:43 +00006001 struct ring_info *ri;
Matt Carlson72334482009-08-28 14:03:01 +00006002 struct tg3_rx_buffer_desc *desc = &tnapi->rx_rcb[sw_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07006003 unsigned int len;
6004 struct sk_buff *skb;
6005 dma_addr_t dma_addr;
6006 u32 opaque_key, desc_idx, *post_ptr;
Eric Dumazet9205fd92011-11-18 06:47:01 +00006007 u8 *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006008
6009 desc_idx = desc->opaque & RXD_OPAQUE_INDEX_MASK;
6010 opaque_key = desc->opaque & RXD_OPAQUE_RING_MASK;
6011 if (opaque_key == RXD_OPAQUE_RING_STD) {
Matt Carlson8fea32b2010-09-15 08:59:58 +00006012 ri = &tp->napi[0].prodring.rx_std_buffers[desc_idx];
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006013 dma_addr = dma_unmap_addr(ri, mapping);
Eric Dumazet9205fd92011-11-18 06:47:01 +00006014 data = ri->data;
Matt Carlson43619352009-11-13 13:03:47 +00006015 post_ptr = &std_prod_idx;
Michael Chanf92905d2006-06-29 20:14:29 -07006016 rx_std_posted++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006017 } else if (opaque_key == RXD_OPAQUE_RING_JUMBO) {
Matt Carlson8fea32b2010-09-15 08:59:58 +00006018 ri = &tp->napi[0].prodring.rx_jmb_buffers[desc_idx];
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006019 dma_addr = dma_unmap_addr(ri, mapping);
Eric Dumazet9205fd92011-11-18 06:47:01 +00006020 data = ri->data;
Matt Carlson43619352009-11-13 13:03:47 +00006021 post_ptr = &jmb_prod_idx;
Matt Carlson21f581a2009-08-28 14:00:25 +00006022 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07006023 goto next_pkt_nopost;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006024
6025 work_mask |= opaque_key;
6026
6027 if ((desc->err_vlan & RXD_ERR_MASK) != 0 &&
6028 (desc->err_vlan != RXD_ERR_ODD_NIBBLE_RCVD_MII)) {
6029 drop_it:
Matt Carlsona3896162009-11-13 13:03:44 +00006030 tg3_recycle_rx(tnapi, tpr, opaque_key,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006031 desc_idx, *post_ptr);
6032 drop_it_no_recycle:
6033 /* Other statistics kept track of by card. */
Eric Dumazetb0057c52010-10-10 19:55:52 +00006034 tp->rx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006035 goto next_pkt;
6036 }
6037
Eric Dumazet9205fd92011-11-18 06:47:01 +00006038 prefetch(data + TG3_RX_OFFSET(tp));
Matt Carlsonad829262008-11-21 17:16:16 -08006039 len = ((desc->idx_len & RXD_LEN_MASK) >> RXD_LEN_SHIFT) -
6040 ETH_FCS_LEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006041
Matt Carlsond2757fc2010-04-12 06:58:27 +00006042 if (len > TG3_RX_COPY_THRESH(tp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006043 int skb_size;
Eric Dumazet8d4057a2012-04-27 00:34:49 +00006044 unsigned int frag_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006045
Eric Dumazet9205fd92011-11-18 06:47:01 +00006046 skb_size = tg3_alloc_rx_data(tp, tpr, opaque_key,
Eric Dumazet8d4057a2012-04-27 00:34:49 +00006047 *post_ptr, &frag_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006048 if (skb_size < 0)
6049 goto drop_it;
6050
Matt Carlson287be122009-08-28 13:58:46 +00006051 pci_unmap_single(tp->pdev, dma_addr, skb_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006052 PCI_DMA_FROMDEVICE);
6053
Eric Dumazet8d4057a2012-04-27 00:34:49 +00006054 skb = build_skb(data, frag_size);
Eric Dumazet9205fd92011-11-18 06:47:01 +00006055 if (!skb) {
Eric Dumazet8d4057a2012-04-27 00:34:49 +00006056 tg3_frag_free(frag_size != 0, data);
Eric Dumazet9205fd92011-11-18 06:47:01 +00006057 goto drop_it_no_recycle;
6058 }
6059 skb_reserve(skb, TG3_RX_OFFSET(tp));
6060 /* Ensure that the update to the data happens
Matt Carlson61e800c2010-02-17 15:16:54 +00006061 * after the usage of the old DMA mapping.
6062 */
6063 smp_wmb();
6064
Eric Dumazet9205fd92011-11-18 06:47:01 +00006065 ri->data = NULL;
Matt Carlson61e800c2010-02-17 15:16:54 +00006066
Linus Torvalds1da177e2005-04-16 15:20:36 -07006067 } else {
Matt Carlsona3896162009-11-13 13:03:44 +00006068 tg3_recycle_rx(tnapi, tpr, opaque_key,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006069 desc_idx, *post_ptr);
6070
Eric Dumazet9205fd92011-11-18 06:47:01 +00006071 skb = netdev_alloc_skb(tp->dev,
6072 len + TG3_RAW_IP_ALIGN);
6073 if (skb == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006074 goto drop_it_no_recycle;
6075
Eric Dumazet9205fd92011-11-18 06:47:01 +00006076 skb_reserve(skb, TG3_RAW_IP_ALIGN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006077 pci_dma_sync_single_for_cpu(tp->pdev, dma_addr, len, PCI_DMA_FROMDEVICE);
Eric Dumazet9205fd92011-11-18 06:47:01 +00006078 memcpy(skb->data,
6079 data + TG3_RX_OFFSET(tp),
6080 len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006081 pci_dma_sync_single_for_device(tp->pdev, dma_addr, len, PCI_DMA_FROMDEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006082 }
6083
Eric Dumazet9205fd92011-11-18 06:47:01 +00006084 skb_put(skb, len);
Michał Mirosławdc668912011-04-07 03:35:07 +00006085 if ((tp->dev->features & NETIF_F_RXCSUM) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07006086 (desc->type_flags & RXD_FLAG_TCPUDP_CSUM) &&
6087 (((desc->ip_tcp_csum & RXD_TCPCSUM_MASK)
6088 >> RXD_TCPCSUM_SHIFT) == 0xffff))
6089 skb->ip_summed = CHECKSUM_UNNECESSARY;
6090 else
Eric Dumazetbc8acf22010-09-02 13:07:41 -07006091 skb_checksum_none_assert(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006092
6093 skb->protocol = eth_type_trans(skb, tp->dev);
Matt Carlsonf7b493e2009-02-25 14:21:52 +00006094
6095 if (len > (tp->dev->mtu + ETH_HLEN) &&
6096 skb->protocol != htons(ETH_P_8021Q)) {
6097 dev_kfree_skb(skb);
Eric Dumazetb0057c52010-10-10 19:55:52 +00006098 goto drop_it_no_recycle;
Matt Carlsonf7b493e2009-02-25 14:21:52 +00006099 }
6100
Matt Carlson9dc7a112010-04-12 06:58:28 +00006101 if (desc->type_flags & RXD_FLAG_VLAN &&
Matt Carlsonbf933c82011-01-25 15:58:49 +00006102 !(tp->rx_mode & RX_MODE_KEEP_VLAN_TAG))
6103 __vlan_hwaccel_put_tag(skb,
6104 desc->err_vlan & RXD_VLAN_MASK);
Matt Carlson9dc7a112010-04-12 06:58:28 +00006105
Matt Carlsonbf933c82011-01-25 15:58:49 +00006106 napi_gro_receive(&tnapi->napi, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006107
Linus Torvalds1da177e2005-04-16 15:20:36 -07006108 received++;
6109 budget--;
6110
6111next_pkt:
6112 (*post_ptr)++;
Michael Chanf92905d2006-06-29 20:14:29 -07006113
6114 if (unlikely(rx_std_posted >= tp->rx_std_max_post)) {
Matt Carlson2c49a442010-09-30 10:34:35 +00006115 tpr->rx_std_prod_idx = std_prod_idx &
6116 tp->rx_std_ring_mask;
Matt Carlson86cfe4f2010-01-12 10:11:37 +00006117 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
6118 tpr->rx_std_prod_idx);
Michael Chanf92905d2006-06-29 20:14:29 -07006119 work_mask &= ~RXD_OPAQUE_RING_STD;
6120 rx_std_posted = 0;
6121 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006122next_pkt_nopost:
Michael Chan483ba502005-04-25 15:14:03 -07006123 sw_idx++;
Matt Carlson7cb32cf2010-09-30 10:34:36 +00006124 sw_idx &= tp->rx_ret_ring_mask;
Michael Chan52f6d692005-04-25 15:14:32 -07006125
6126 /* Refresh hw_idx to see if there is new work */
6127 if (sw_idx == hw_idx) {
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00006128 hw_idx = *(tnapi->rx_rcb_prod_idx);
Michael Chan52f6d692005-04-25 15:14:32 -07006129 rmb();
6130 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006131 }
6132
6133 /* ACK the status ring. */
Matt Carlson72334482009-08-28 14:03:01 +00006134 tnapi->rx_rcb_ptr = sw_idx;
6135 tw32_rx_mbox(tnapi->consmbox, sw_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006136
6137 /* Refill RX ring(s). */
Joe Perches63c3a662011-04-26 08:12:10 +00006138 if (!tg3_flag(tp, ENABLE_RSS)) {
Michael Chan6541b802012-03-04 14:48:14 +00006139 /* Sync BD data before updating mailbox */
6140 wmb();
6141
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006142 if (work_mask & RXD_OPAQUE_RING_STD) {
Matt Carlson2c49a442010-09-30 10:34:35 +00006143 tpr->rx_std_prod_idx = std_prod_idx &
6144 tp->rx_std_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006145 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
6146 tpr->rx_std_prod_idx);
6147 }
6148 if (work_mask & RXD_OPAQUE_RING_JUMBO) {
Matt Carlson2c49a442010-09-30 10:34:35 +00006149 tpr->rx_jmb_prod_idx = jmb_prod_idx &
6150 tp->rx_jmb_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006151 tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG,
6152 tpr->rx_jmb_prod_idx);
6153 }
6154 mmiowb();
6155 } else if (work_mask) {
6156 /* rx_std_buffers[] and rx_jmb_buffers[] entries must be
6157 * updated before the producer indices can be updated.
6158 */
6159 smp_wmb();
6160
Matt Carlson2c49a442010-09-30 10:34:35 +00006161 tpr->rx_std_prod_idx = std_prod_idx & tp->rx_std_ring_mask;
6162 tpr->rx_jmb_prod_idx = jmb_prod_idx & tp->rx_jmb_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006163
Michael Chan7ae52892012-03-21 15:38:33 +00006164 if (tnapi != &tp->napi[1]) {
6165 tp->rx_refill = true;
Matt Carlsone4af1af2010-02-12 14:47:05 +00006166 napi_schedule(&tp->napi[1].napi);
Michael Chan7ae52892012-03-21 15:38:33 +00006167 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006168 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006169
6170 return received;
6171}
6172
Matt Carlson35f2d7d2009-11-13 13:03:41 +00006173static void tg3_poll_link(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006174{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006175 /* handle link change and other phy events */
Joe Perches63c3a662011-04-26 08:12:10 +00006176 if (!(tg3_flag(tp, USE_LINKCHG_REG) || tg3_flag(tp, POLL_SERDES))) {
Matt Carlson35f2d7d2009-11-13 13:03:41 +00006177 struct tg3_hw_status *sblk = tp->napi[0].hw_status;
6178
Linus Torvalds1da177e2005-04-16 15:20:36 -07006179 if (sblk->status & SD_STATUS_LINK_CHG) {
6180 sblk->status = SD_STATUS_UPDATED |
Matt Carlson35f2d7d2009-11-13 13:03:41 +00006181 (sblk->status & ~SD_STATUS_LINK_CHG);
David S. Millerf47c11e2005-06-24 20:18:35 -07006182 spin_lock(&tp->lock);
Joe Perches63c3a662011-04-26 08:12:10 +00006183 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlsondd477002008-05-25 23:45:58 -07006184 tw32_f(MAC_STATUS,
6185 (MAC_STATUS_SYNC_CHANGED |
6186 MAC_STATUS_CFG_CHANGED |
6187 MAC_STATUS_MI_COMPLETION |
6188 MAC_STATUS_LNKSTATE_CHANGED));
6189 udelay(40);
6190 } else
6191 tg3_setup_phy(tp, 0);
David S. Millerf47c11e2005-06-24 20:18:35 -07006192 spin_unlock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006193 }
6194 }
Matt Carlson35f2d7d2009-11-13 13:03:41 +00006195}
6196
Matt Carlsonf89f38b2010-02-12 14:47:07 +00006197static int tg3_rx_prodring_xfer(struct tg3 *tp,
6198 struct tg3_rx_prodring_set *dpr,
6199 struct tg3_rx_prodring_set *spr)
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006200{
6201 u32 si, di, cpycnt, src_prod_idx;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00006202 int i, err = 0;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006203
6204 while (1) {
6205 src_prod_idx = spr->rx_std_prod_idx;
6206
6207 /* Make sure updates to the rx_std_buffers[] entries and the
6208 * standard producer index are seen in the correct order.
6209 */
6210 smp_rmb();
6211
6212 if (spr->rx_std_cons_idx == src_prod_idx)
6213 break;
6214
6215 if (spr->rx_std_cons_idx < src_prod_idx)
6216 cpycnt = src_prod_idx - spr->rx_std_cons_idx;
6217 else
Matt Carlson2c49a442010-09-30 10:34:35 +00006218 cpycnt = tp->rx_std_ring_mask + 1 -
6219 spr->rx_std_cons_idx;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006220
Matt Carlson2c49a442010-09-30 10:34:35 +00006221 cpycnt = min(cpycnt,
6222 tp->rx_std_ring_mask + 1 - dpr->rx_std_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006223
6224 si = spr->rx_std_cons_idx;
6225 di = dpr->rx_std_prod_idx;
6226
Matt Carlsone92967b2010-02-12 14:47:06 +00006227 for (i = di; i < di + cpycnt; i++) {
Eric Dumazet9205fd92011-11-18 06:47:01 +00006228 if (dpr->rx_std_buffers[i].data) {
Matt Carlsone92967b2010-02-12 14:47:06 +00006229 cpycnt = i - di;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00006230 err = -ENOSPC;
Matt Carlsone92967b2010-02-12 14:47:06 +00006231 break;
6232 }
6233 }
6234
6235 if (!cpycnt)
6236 break;
6237
6238 /* Ensure that updates to the rx_std_buffers ring and the
6239 * shadowed hardware producer ring from tg3_recycle_skb() are
6240 * ordered correctly WRT the skb check above.
6241 */
6242 smp_rmb();
6243
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006244 memcpy(&dpr->rx_std_buffers[di],
6245 &spr->rx_std_buffers[si],
6246 cpycnt * sizeof(struct ring_info));
6247
6248 for (i = 0; i < cpycnt; i++, di++, si++) {
6249 struct tg3_rx_buffer_desc *sbd, *dbd;
6250 sbd = &spr->rx_std[si];
6251 dbd = &dpr->rx_std[di];
6252 dbd->addr_hi = sbd->addr_hi;
6253 dbd->addr_lo = sbd->addr_lo;
6254 }
6255
Matt Carlson2c49a442010-09-30 10:34:35 +00006256 spr->rx_std_cons_idx = (spr->rx_std_cons_idx + cpycnt) &
6257 tp->rx_std_ring_mask;
6258 dpr->rx_std_prod_idx = (dpr->rx_std_prod_idx + cpycnt) &
6259 tp->rx_std_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006260 }
6261
6262 while (1) {
6263 src_prod_idx = spr->rx_jmb_prod_idx;
6264
6265 /* Make sure updates to the rx_jmb_buffers[] entries and
6266 * the jumbo producer index are seen in the correct order.
6267 */
6268 smp_rmb();
6269
6270 if (spr->rx_jmb_cons_idx == src_prod_idx)
6271 break;
6272
6273 if (spr->rx_jmb_cons_idx < src_prod_idx)
6274 cpycnt = src_prod_idx - spr->rx_jmb_cons_idx;
6275 else
Matt Carlson2c49a442010-09-30 10:34:35 +00006276 cpycnt = tp->rx_jmb_ring_mask + 1 -
6277 spr->rx_jmb_cons_idx;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006278
6279 cpycnt = min(cpycnt,
Matt Carlson2c49a442010-09-30 10:34:35 +00006280 tp->rx_jmb_ring_mask + 1 - dpr->rx_jmb_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006281
6282 si = spr->rx_jmb_cons_idx;
6283 di = dpr->rx_jmb_prod_idx;
6284
Matt Carlsone92967b2010-02-12 14:47:06 +00006285 for (i = di; i < di + cpycnt; i++) {
Eric Dumazet9205fd92011-11-18 06:47:01 +00006286 if (dpr->rx_jmb_buffers[i].data) {
Matt Carlsone92967b2010-02-12 14:47:06 +00006287 cpycnt = i - di;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00006288 err = -ENOSPC;
Matt Carlsone92967b2010-02-12 14:47:06 +00006289 break;
6290 }
6291 }
6292
6293 if (!cpycnt)
6294 break;
6295
6296 /* Ensure that updates to the rx_jmb_buffers ring and the
6297 * shadowed hardware producer ring from tg3_recycle_skb() are
6298 * ordered correctly WRT the skb check above.
6299 */
6300 smp_rmb();
6301
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006302 memcpy(&dpr->rx_jmb_buffers[di],
6303 &spr->rx_jmb_buffers[si],
6304 cpycnt * sizeof(struct ring_info));
6305
6306 for (i = 0; i < cpycnt; i++, di++, si++) {
6307 struct tg3_rx_buffer_desc *sbd, *dbd;
6308 sbd = &spr->rx_jmb[si].std;
6309 dbd = &dpr->rx_jmb[di].std;
6310 dbd->addr_hi = sbd->addr_hi;
6311 dbd->addr_lo = sbd->addr_lo;
6312 }
6313
Matt Carlson2c49a442010-09-30 10:34:35 +00006314 spr->rx_jmb_cons_idx = (spr->rx_jmb_cons_idx + cpycnt) &
6315 tp->rx_jmb_ring_mask;
6316 dpr->rx_jmb_prod_idx = (dpr->rx_jmb_prod_idx + cpycnt) &
6317 tp->rx_jmb_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006318 }
Matt Carlsonf89f38b2010-02-12 14:47:07 +00006319
6320 return err;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006321}
6322
Matt Carlson35f2d7d2009-11-13 13:03:41 +00006323static int tg3_poll_work(struct tg3_napi *tnapi, int work_done, int budget)
6324{
6325 struct tg3 *tp = tnapi->tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006326
6327 /* run TX completion thread */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006328 if (tnapi->hw_status->idx[0].tx_consumer != tnapi->tx_cons) {
Matt Carlson17375d22009-08-28 14:02:18 +00006329 tg3_tx(tnapi);
Joe Perches63c3a662011-04-26 08:12:10 +00006330 if (unlikely(tg3_flag(tp, TX_RECOVERY_PENDING)))
Michael Chan4fd7ab52007-10-12 01:39:50 -07006331 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006332 }
6333
Matt Carlsonf891ea12012-04-24 13:37:01 +00006334 if (!tnapi->rx_rcb_prod_idx)
6335 return work_done;
6336
Linus Torvalds1da177e2005-04-16 15:20:36 -07006337 /* run RX thread, within the bounds set by NAPI.
6338 * All RX "locking" is done by ensuring outside
Stephen Hemmingerbea33482007-10-03 16:41:36 -07006339 * code synchronizes with tg3->napi.poll()
Linus Torvalds1da177e2005-04-16 15:20:36 -07006340 */
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00006341 if (*(tnapi->rx_rcb_prod_idx) != tnapi->rx_rcb_ptr)
Matt Carlson17375d22009-08-28 14:02:18 +00006342 work_done += tg3_rx(tnapi, budget - work_done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006343
Joe Perches63c3a662011-04-26 08:12:10 +00006344 if (tg3_flag(tp, ENABLE_RSS) && tnapi == &tp->napi[1]) {
Matt Carlson8fea32b2010-09-15 08:59:58 +00006345 struct tg3_rx_prodring_set *dpr = &tp->napi[0].prodring;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00006346 int i, err = 0;
Matt Carlsone4af1af2010-02-12 14:47:05 +00006347 u32 std_prod_idx = dpr->rx_std_prod_idx;
6348 u32 jmb_prod_idx = dpr->rx_jmb_prod_idx;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006349
Michael Chan7ae52892012-03-21 15:38:33 +00006350 tp->rx_refill = false;
Michael Chan91024262012-09-28 07:12:38 +00006351 for (i = 1; i <= tp->rxq_cnt; i++)
Matt Carlsonf89f38b2010-02-12 14:47:07 +00006352 err |= tg3_rx_prodring_xfer(tp, dpr,
Matt Carlson8fea32b2010-09-15 08:59:58 +00006353 &tp->napi[i].prodring);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006354
6355 wmb();
6356
Matt Carlsone4af1af2010-02-12 14:47:05 +00006357 if (std_prod_idx != dpr->rx_std_prod_idx)
6358 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
6359 dpr->rx_std_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006360
Matt Carlsone4af1af2010-02-12 14:47:05 +00006361 if (jmb_prod_idx != dpr->rx_jmb_prod_idx)
6362 tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG,
6363 dpr->rx_jmb_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006364
6365 mmiowb();
Matt Carlsonf89f38b2010-02-12 14:47:07 +00006366
6367 if (err)
6368 tw32_f(HOSTCC_MODE, tp->coal_now);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006369 }
6370
David S. Miller6f535762007-10-11 18:08:29 -07006371 return work_done;
6372}
David S. Millerf7383c22005-05-18 22:50:53 -07006373
Matt Carlsondb219972011-11-04 09:15:03 +00006374static inline void tg3_reset_task_schedule(struct tg3 *tp)
6375{
6376 if (!test_and_set_bit(TG3_FLAG_RESET_TASK_PENDING, tp->tg3_flags))
6377 schedule_work(&tp->reset_task);
6378}
6379
6380static inline void tg3_reset_task_cancel(struct tg3 *tp)
6381{
6382 cancel_work_sync(&tp->reset_task);
6383 tg3_flag_clear(tp, RESET_TASK_PENDING);
Matt Carlsonc7101352012-02-22 12:35:20 +00006384 tg3_flag_clear(tp, TX_RECOVERY_PENDING);
Matt Carlsondb219972011-11-04 09:15:03 +00006385}
6386
Matt Carlson35f2d7d2009-11-13 13:03:41 +00006387static int tg3_poll_msix(struct napi_struct *napi, int budget)
6388{
6389 struct tg3_napi *tnapi = container_of(napi, struct tg3_napi, napi);
6390 struct tg3 *tp = tnapi->tp;
6391 int work_done = 0;
6392 struct tg3_hw_status *sblk = tnapi->hw_status;
6393
6394 while (1) {
6395 work_done = tg3_poll_work(tnapi, work_done, budget);
6396
Joe Perches63c3a662011-04-26 08:12:10 +00006397 if (unlikely(tg3_flag(tp, TX_RECOVERY_PENDING)))
Matt Carlson35f2d7d2009-11-13 13:03:41 +00006398 goto tx_recovery;
6399
6400 if (unlikely(work_done >= budget))
6401 break;
6402
Matt Carlsonc6cdf432010-04-05 10:19:26 +00006403 /* tp->last_tag is used in tg3_int_reenable() below
Matt Carlson35f2d7d2009-11-13 13:03:41 +00006404 * to tell the hw how much work has been processed,
6405 * so we must read it before checking for more work.
6406 */
6407 tnapi->last_tag = sblk->status_tag;
6408 tnapi->last_irq_tag = tnapi->last_tag;
6409 rmb();
6410
6411 /* check for RX/TX work to do */
Matt Carlson6d40db72010-04-05 10:19:20 +00006412 if (likely(sblk->idx[0].tx_consumer == tnapi->tx_cons &&
6413 *(tnapi->rx_rcb_prod_idx) == tnapi->rx_rcb_ptr)) {
Michael Chan7ae52892012-03-21 15:38:33 +00006414
6415 /* This test here is not race free, but will reduce
6416 * the number of interrupts by looping again.
6417 */
6418 if (tnapi == &tp->napi[1] && tp->rx_refill)
6419 continue;
6420
Matt Carlson35f2d7d2009-11-13 13:03:41 +00006421 napi_complete(napi);
6422 /* Reenable interrupts. */
6423 tw32_mailbox(tnapi->int_mbox, tnapi->last_tag << 24);
Michael Chan7ae52892012-03-21 15:38:33 +00006424
6425 /* This test here is synchronized by napi_schedule()
6426 * and napi_complete() to close the race condition.
6427 */
6428 if (unlikely(tnapi == &tp->napi[1] && tp->rx_refill)) {
6429 tw32(HOSTCC_MODE, tp->coalesce_mode |
6430 HOSTCC_MODE_ENABLE |
6431 tnapi->coal_now);
6432 }
Matt Carlson35f2d7d2009-11-13 13:03:41 +00006433 mmiowb();
6434 break;
6435 }
6436 }
6437
6438 return work_done;
6439
6440tx_recovery:
6441 /* work_done is guaranteed to be less than budget. */
6442 napi_complete(napi);
Matt Carlsondb219972011-11-04 09:15:03 +00006443 tg3_reset_task_schedule(tp);
Matt Carlson35f2d7d2009-11-13 13:03:41 +00006444 return work_done;
6445}
6446
Matt Carlsone64de4e2011-04-13 11:05:05 +00006447static void tg3_process_error(struct tg3 *tp)
6448{
6449 u32 val;
6450 bool real_error = false;
6451
Joe Perches63c3a662011-04-26 08:12:10 +00006452 if (tg3_flag(tp, ERROR_PROCESSED))
Matt Carlsone64de4e2011-04-13 11:05:05 +00006453 return;
6454
6455 /* Check Flow Attention register */
6456 val = tr32(HOSTCC_FLOW_ATTN);
6457 if (val & ~HOSTCC_FLOW_ATTN_MBUF_LWM) {
6458 netdev_err(tp->dev, "FLOW Attention error. Resetting chip.\n");
6459 real_error = true;
6460 }
6461
6462 if (tr32(MSGINT_STATUS) & ~MSGINT_STATUS_MSI_REQ) {
6463 netdev_err(tp->dev, "MSI Status error. Resetting chip.\n");
6464 real_error = true;
6465 }
6466
6467 if (tr32(RDMAC_STATUS) || tr32(WDMAC_STATUS)) {
6468 netdev_err(tp->dev, "DMA Status error. Resetting chip.\n");
6469 real_error = true;
6470 }
6471
6472 if (!real_error)
6473 return;
6474
6475 tg3_dump_state(tp);
6476
Joe Perches63c3a662011-04-26 08:12:10 +00006477 tg3_flag_set(tp, ERROR_PROCESSED);
Matt Carlsondb219972011-11-04 09:15:03 +00006478 tg3_reset_task_schedule(tp);
Matt Carlsone64de4e2011-04-13 11:05:05 +00006479}
6480
David S. Miller6f535762007-10-11 18:08:29 -07006481static int tg3_poll(struct napi_struct *napi, int budget)
6482{
Matt Carlson8ef04422009-08-28 14:01:37 +00006483 struct tg3_napi *tnapi = container_of(napi, struct tg3_napi, napi);
6484 struct tg3 *tp = tnapi->tp;
David S. Miller6f535762007-10-11 18:08:29 -07006485 int work_done = 0;
Matt Carlson898a56f2009-08-28 14:02:40 +00006486 struct tg3_hw_status *sblk = tnapi->hw_status;
David S. Miller6f535762007-10-11 18:08:29 -07006487
6488 while (1) {
Matt Carlsone64de4e2011-04-13 11:05:05 +00006489 if (sblk->status & SD_STATUS_ERROR)
6490 tg3_process_error(tp);
6491
Matt Carlson35f2d7d2009-11-13 13:03:41 +00006492 tg3_poll_link(tp);
6493
Matt Carlson17375d22009-08-28 14:02:18 +00006494 work_done = tg3_poll_work(tnapi, work_done, budget);
David S. Miller6f535762007-10-11 18:08:29 -07006495
Joe Perches63c3a662011-04-26 08:12:10 +00006496 if (unlikely(tg3_flag(tp, TX_RECOVERY_PENDING)))
David S. Miller6f535762007-10-11 18:08:29 -07006497 goto tx_recovery;
6498
6499 if (unlikely(work_done >= budget))
6500 break;
6501
Joe Perches63c3a662011-04-26 08:12:10 +00006502 if (tg3_flag(tp, TAGGED_STATUS)) {
Matt Carlson17375d22009-08-28 14:02:18 +00006503 /* tp->last_tag is used in tg3_int_reenable() below
Michael Chan4fd7ab52007-10-12 01:39:50 -07006504 * to tell the hw how much work has been processed,
6505 * so we must read it before checking for more work.
6506 */
Matt Carlson898a56f2009-08-28 14:02:40 +00006507 tnapi->last_tag = sblk->status_tag;
6508 tnapi->last_irq_tag = tnapi->last_tag;
Michael Chan4fd7ab52007-10-12 01:39:50 -07006509 rmb();
6510 } else
6511 sblk->status &= ~SD_STATUS_UPDATED;
6512
Matt Carlson17375d22009-08-28 14:02:18 +00006513 if (likely(!tg3_has_work(tnapi))) {
Ben Hutchings288379f2009-01-19 16:43:59 -08006514 napi_complete(napi);
Matt Carlson17375d22009-08-28 14:02:18 +00006515 tg3_int_reenable(tnapi);
David S. Miller6f535762007-10-11 18:08:29 -07006516 break;
6517 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006518 }
6519
Stephen Hemmingerbea33482007-10-03 16:41:36 -07006520 return work_done;
David S. Miller6f535762007-10-11 18:08:29 -07006521
6522tx_recovery:
Michael Chan4fd7ab52007-10-12 01:39:50 -07006523 /* work_done is guaranteed to be less than budget. */
Ben Hutchings288379f2009-01-19 16:43:59 -08006524 napi_complete(napi);
Matt Carlsondb219972011-11-04 09:15:03 +00006525 tg3_reset_task_schedule(tp);
Michael Chan4fd7ab52007-10-12 01:39:50 -07006526 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006527}
6528
Matt Carlson66cfd1b2010-09-30 10:34:30 +00006529static void tg3_napi_disable(struct tg3 *tp)
6530{
6531 int i;
6532
6533 for (i = tp->irq_cnt - 1; i >= 0; i--)
6534 napi_disable(&tp->napi[i].napi);
6535}
6536
6537static void tg3_napi_enable(struct tg3 *tp)
6538{
6539 int i;
6540
6541 for (i = 0; i < tp->irq_cnt; i++)
6542 napi_enable(&tp->napi[i].napi);
6543}
6544
6545static void tg3_napi_init(struct tg3 *tp)
6546{
6547 int i;
6548
6549 netif_napi_add(tp->dev, &tp->napi[0].napi, tg3_poll, 64);
6550 for (i = 1; i < tp->irq_cnt; i++)
6551 netif_napi_add(tp->dev, &tp->napi[i].napi, tg3_poll_msix, 64);
6552}
6553
6554static void tg3_napi_fini(struct tg3 *tp)
6555{
6556 int i;
6557
6558 for (i = 0; i < tp->irq_cnt; i++)
6559 netif_napi_del(&tp->napi[i].napi);
6560}
6561
6562static inline void tg3_netif_stop(struct tg3 *tp)
6563{
6564 tp->dev->trans_start = jiffies; /* prevent tx timeout */
6565 tg3_napi_disable(tp);
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00006566 netif_carrier_off(tp->dev);
Matt Carlson66cfd1b2010-09-30 10:34:30 +00006567 netif_tx_disable(tp->dev);
6568}
6569
Nithin Nayak Sujir35763062012-12-03 19:36:56 +00006570/* tp->lock must be held */
Matt Carlson66cfd1b2010-09-30 10:34:30 +00006571static inline void tg3_netif_start(struct tg3 *tp)
6572{
Matt Carlsonbe947302012-12-03 19:36:57 +00006573 tg3_ptp_resume(tp);
6574
Matt Carlson66cfd1b2010-09-30 10:34:30 +00006575 /* NOTE: unconditional netif_tx_wake_all_queues is only
6576 * appropriate so long as all callers are assured to
6577 * have free tx slots (such as after tg3_init_hw)
6578 */
6579 netif_tx_wake_all_queues(tp->dev);
6580
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00006581 if (tp->link_up)
6582 netif_carrier_on(tp->dev);
6583
Matt Carlson66cfd1b2010-09-30 10:34:30 +00006584 tg3_napi_enable(tp);
6585 tp->napi[0].hw_status->status |= SD_STATUS_UPDATED;
6586 tg3_enable_ints(tp);
6587}
6588
David S. Millerf47c11e2005-06-24 20:18:35 -07006589static void tg3_irq_quiesce(struct tg3 *tp)
6590{
Matt Carlson4f125f42009-09-01 12:55:02 +00006591 int i;
6592
David S. Millerf47c11e2005-06-24 20:18:35 -07006593 BUG_ON(tp->irq_sync);
6594
6595 tp->irq_sync = 1;
6596 smp_mb();
6597
Matt Carlson4f125f42009-09-01 12:55:02 +00006598 for (i = 0; i < tp->irq_cnt; i++)
6599 synchronize_irq(tp->napi[i].irq_vec);
David S. Millerf47c11e2005-06-24 20:18:35 -07006600}
6601
David S. Millerf47c11e2005-06-24 20:18:35 -07006602/* Fully shutdown all tg3 driver activity elsewhere in the system.
6603 * If irq_sync is non-zero, then the IRQ handler must be synchronized
6604 * with as well. Most of the time, this is not necessary except when
6605 * shutting down the device.
6606 */
6607static inline void tg3_full_lock(struct tg3 *tp, int irq_sync)
6608{
Michael Chan46966542007-07-11 19:47:19 -07006609 spin_lock_bh(&tp->lock);
David S. Millerf47c11e2005-06-24 20:18:35 -07006610 if (irq_sync)
6611 tg3_irq_quiesce(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -07006612}
6613
6614static inline void tg3_full_unlock(struct tg3 *tp)
6615{
David S. Millerf47c11e2005-06-24 20:18:35 -07006616 spin_unlock_bh(&tp->lock);
6617}
6618
Michael Chanfcfa0a32006-03-20 22:28:41 -08006619/* One-shot MSI handler - Chip automatically disables interrupt
6620 * after sending MSI so driver doesn't have to do it.
6621 */
David Howells7d12e782006-10-05 14:55:46 +01006622static irqreturn_t tg3_msi_1shot(int irq, void *dev_id)
Michael Chanfcfa0a32006-03-20 22:28:41 -08006623{
Matt Carlson09943a12009-08-28 14:01:57 +00006624 struct tg3_napi *tnapi = dev_id;
6625 struct tg3 *tp = tnapi->tp;
Michael Chanfcfa0a32006-03-20 22:28:41 -08006626
Matt Carlson898a56f2009-08-28 14:02:40 +00006627 prefetch(tnapi->hw_status);
Matt Carlson0c1d0e22009-09-01 13:16:33 +00006628 if (tnapi->rx_rcb)
6629 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Michael Chanfcfa0a32006-03-20 22:28:41 -08006630
6631 if (likely(!tg3_irq_sync(tp)))
Matt Carlson09943a12009-08-28 14:01:57 +00006632 napi_schedule(&tnapi->napi);
Michael Chanfcfa0a32006-03-20 22:28:41 -08006633
6634 return IRQ_HANDLED;
6635}
6636
Michael Chan88b06bc22005-04-21 17:13:25 -07006637/* MSI ISR - No need to check for interrupt sharing and no need to
6638 * flush status block and interrupt mailbox. PCI ordering rules
6639 * guarantee that MSI will arrive after the status block.
6640 */
David Howells7d12e782006-10-05 14:55:46 +01006641static irqreturn_t tg3_msi(int irq, void *dev_id)
Michael Chan88b06bc22005-04-21 17:13:25 -07006642{
Matt Carlson09943a12009-08-28 14:01:57 +00006643 struct tg3_napi *tnapi = dev_id;
6644 struct tg3 *tp = tnapi->tp;
Michael Chan88b06bc22005-04-21 17:13:25 -07006645
Matt Carlson898a56f2009-08-28 14:02:40 +00006646 prefetch(tnapi->hw_status);
Matt Carlson0c1d0e22009-09-01 13:16:33 +00006647 if (tnapi->rx_rcb)
6648 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Michael Chan88b06bc22005-04-21 17:13:25 -07006649 /*
David S. Millerfac9b832005-05-18 22:46:34 -07006650 * Writing any value to intr-mbox-0 clears PCI INTA# and
Michael Chan88b06bc22005-04-21 17:13:25 -07006651 * chip-internal interrupt pending events.
David S. Millerfac9b832005-05-18 22:46:34 -07006652 * Writing non-zero to intr-mbox-0 additional tells the
Michael Chan88b06bc22005-04-21 17:13:25 -07006653 * NIC to stop sending us irqs, engaging "in-intr-handler"
6654 * event coalescing.
6655 */
Matt Carlson5b39de92011-08-31 11:44:50 +00006656 tw32_mailbox(tnapi->int_mbox, 0x00000001);
Michael Chan61487482005-09-05 17:53:19 -07006657 if (likely(!tg3_irq_sync(tp)))
Matt Carlson09943a12009-08-28 14:01:57 +00006658 napi_schedule(&tnapi->napi);
Michael Chan61487482005-09-05 17:53:19 -07006659
Michael Chan88b06bc22005-04-21 17:13:25 -07006660 return IRQ_RETVAL(1);
6661}
6662
David Howells7d12e782006-10-05 14:55:46 +01006663static irqreturn_t tg3_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006664{
Matt Carlson09943a12009-08-28 14:01:57 +00006665 struct tg3_napi *tnapi = dev_id;
6666 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00006667 struct tg3_hw_status *sblk = tnapi->hw_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006668 unsigned int handled = 1;
6669
Linus Torvalds1da177e2005-04-16 15:20:36 -07006670 /* In INTx mode, it is possible for the interrupt to arrive at
6671 * the CPU before the status block posted prior to the interrupt.
6672 * Reading the PCI State register will confirm whether the
6673 * interrupt is ours and will flush the status block.
6674 */
Michael Chand18edcb2007-03-24 20:57:11 -07006675 if (unlikely(!(sblk->status & SD_STATUS_UPDATED))) {
Joe Perches63c3a662011-04-26 08:12:10 +00006676 if (tg3_flag(tp, CHIP_RESETTING) ||
Michael Chand18edcb2007-03-24 20:57:11 -07006677 (tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
6678 handled = 0;
David S. Millerf47c11e2005-06-24 20:18:35 -07006679 goto out;
David S. Millerfac9b832005-05-18 22:46:34 -07006680 }
Michael Chand18edcb2007-03-24 20:57:11 -07006681 }
6682
6683 /*
6684 * Writing any value to intr-mbox-0 clears PCI INTA# and
6685 * chip-internal interrupt pending events.
6686 * Writing non-zero to intr-mbox-0 additional tells the
6687 * NIC to stop sending us irqs, engaging "in-intr-handler"
6688 * event coalescing.
Michael Chanc04cb342007-05-07 00:26:15 -07006689 *
6690 * Flush the mailbox to de-assert the IRQ immediately to prevent
6691 * spurious interrupts. The flush impacts performance but
6692 * excessive spurious interrupts can be worse in some cases.
Michael Chand18edcb2007-03-24 20:57:11 -07006693 */
Michael Chanc04cb342007-05-07 00:26:15 -07006694 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 0x00000001);
Michael Chand18edcb2007-03-24 20:57:11 -07006695 if (tg3_irq_sync(tp))
6696 goto out;
6697 sblk->status &= ~SD_STATUS_UPDATED;
Matt Carlson17375d22009-08-28 14:02:18 +00006698 if (likely(tg3_has_work(tnapi))) {
Matt Carlson72334482009-08-28 14:03:01 +00006699 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Matt Carlson09943a12009-08-28 14:01:57 +00006700 napi_schedule(&tnapi->napi);
Michael Chand18edcb2007-03-24 20:57:11 -07006701 } else {
6702 /* No work, shared interrupt perhaps? re-enable
6703 * interrupts, and flush that PCI write
6704 */
6705 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW,
6706 0x00000000);
David S. Millerfac9b832005-05-18 22:46:34 -07006707 }
David S. Millerf47c11e2005-06-24 20:18:35 -07006708out:
David S. Millerfac9b832005-05-18 22:46:34 -07006709 return IRQ_RETVAL(handled);
6710}
6711
David Howells7d12e782006-10-05 14:55:46 +01006712static irqreturn_t tg3_interrupt_tagged(int irq, void *dev_id)
David S. Millerfac9b832005-05-18 22:46:34 -07006713{
Matt Carlson09943a12009-08-28 14:01:57 +00006714 struct tg3_napi *tnapi = dev_id;
6715 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00006716 struct tg3_hw_status *sblk = tnapi->hw_status;
David S. Millerfac9b832005-05-18 22:46:34 -07006717 unsigned int handled = 1;
6718
David S. Millerfac9b832005-05-18 22:46:34 -07006719 /* In INTx mode, it is possible for the interrupt to arrive at
6720 * the CPU before the status block posted prior to the interrupt.
6721 * Reading the PCI State register will confirm whether the
6722 * interrupt is ours and will flush the status block.
6723 */
Matt Carlson898a56f2009-08-28 14:02:40 +00006724 if (unlikely(sblk->status_tag == tnapi->last_irq_tag)) {
Joe Perches63c3a662011-04-26 08:12:10 +00006725 if (tg3_flag(tp, CHIP_RESETTING) ||
Michael Chand18edcb2007-03-24 20:57:11 -07006726 (tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
6727 handled = 0;
David S. Millerf47c11e2005-06-24 20:18:35 -07006728 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006729 }
Michael Chand18edcb2007-03-24 20:57:11 -07006730 }
6731
6732 /*
6733 * writing any value to intr-mbox-0 clears PCI INTA# and
6734 * chip-internal interrupt pending events.
6735 * writing non-zero to intr-mbox-0 additional tells the
6736 * NIC to stop sending us irqs, engaging "in-intr-handler"
6737 * event coalescing.
Michael Chanc04cb342007-05-07 00:26:15 -07006738 *
6739 * Flush the mailbox to de-assert the IRQ immediately to prevent
6740 * spurious interrupts. The flush impacts performance but
6741 * excessive spurious interrupts can be worse in some cases.
Michael Chand18edcb2007-03-24 20:57:11 -07006742 */
Michael Chanc04cb342007-05-07 00:26:15 -07006743 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 0x00000001);
Matt Carlson624f8e52009-04-20 06:55:01 +00006744
6745 /*
6746 * In a shared interrupt configuration, sometimes other devices'
6747 * interrupts will scream. We record the current status tag here
6748 * so that the above check can report that the screaming interrupts
6749 * are unhandled. Eventually they will be silenced.
6750 */
Matt Carlson898a56f2009-08-28 14:02:40 +00006751 tnapi->last_irq_tag = sblk->status_tag;
Matt Carlson624f8e52009-04-20 06:55:01 +00006752
Michael Chand18edcb2007-03-24 20:57:11 -07006753 if (tg3_irq_sync(tp))
6754 goto out;
Matt Carlson624f8e52009-04-20 06:55:01 +00006755
Matt Carlson72334482009-08-28 14:03:01 +00006756 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Matt Carlson624f8e52009-04-20 06:55:01 +00006757
Matt Carlson09943a12009-08-28 14:01:57 +00006758 napi_schedule(&tnapi->napi);
Matt Carlson624f8e52009-04-20 06:55:01 +00006759
David S. Millerf47c11e2005-06-24 20:18:35 -07006760out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006761 return IRQ_RETVAL(handled);
6762}
6763
Michael Chan79381092005-04-21 17:13:59 -07006764/* ISR for interrupt test */
David Howells7d12e782006-10-05 14:55:46 +01006765static irqreturn_t tg3_test_isr(int irq, void *dev_id)
Michael Chan79381092005-04-21 17:13:59 -07006766{
Matt Carlson09943a12009-08-28 14:01:57 +00006767 struct tg3_napi *tnapi = dev_id;
6768 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00006769 struct tg3_hw_status *sblk = tnapi->hw_status;
Michael Chan79381092005-04-21 17:13:59 -07006770
Michael Chanf9804dd2005-09-27 12:13:10 -07006771 if ((sblk->status & SD_STATUS_UPDATED) ||
6772 !(tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
Michael Chanb16250e2006-09-27 16:10:14 -07006773 tg3_disable_ints(tp);
Michael Chan79381092005-04-21 17:13:59 -07006774 return IRQ_RETVAL(1);
6775 }
6776 return IRQ_RETVAL(0);
6777}
6778
Linus Torvalds1da177e2005-04-16 15:20:36 -07006779#ifdef CONFIG_NET_POLL_CONTROLLER
6780static void tg3_poll_controller(struct net_device *dev)
6781{
Matt Carlson4f125f42009-09-01 12:55:02 +00006782 int i;
Michael Chan88b06bc22005-04-21 17:13:25 -07006783 struct tg3 *tp = netdev_priv(dev);
6784
Matt Carlson4f125f42009-09-01 12:55:02 +00006785 for (i = 0; i < tp->irq_cnt; i++)
Louis Rillingfe234f02010-03-09 06:14:41 +00006786 tg3_interrupt(tp->napi[i].irq_vec, &tp->napi[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006787}
6788#endif
6789
Linus Torvalds1da177e2005-04-16 15:20:36 -07006790static void tg3_tx_timeout(struct net_device *dev)
6791{
6792 struct tg3 *tp = netdev_priv(dev);
6793
Michael Chanb0408752007-02-13 12:18:30 -08006794 if (netif_msg_tx_err(tp)) {
Joe Perches05dbe002010-02-17 19:44:19 +00006795 netdev_err(dev, "transmit timed out, resetting\n");
Matt Carlson97bd8e42011-04-13 11:05:04 +00006796 tg3_dump_state(tp);
Michael Chanb0408752007-02-13 12:18:30 -08006797 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006798
Matt Carlsondb219972011-11-04 09:15:03 +00006799 tg3_reset_task_schedule(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006800}
6801
Michael Chanc58ec932005-09-17 00:46:27 -07006802/* Test for DMA buffers crossing any 4GB boundaries: 4G, 8G, etc */
6803static inline int tg3_4g_overflow_test(dma_addr_t mapping, int len)
6804{
6805 u32 base = (u32) mapping & 0xffffffff;
6806
Eric Dumazet807540b2010-09-23 05:40:09 +00006807 return (base > 0xffffdcc0) && (base + len + 8 < base);
Michael Chanc58ec932005-09-17 00:46:27 -07006808}
6809
Michael Chan72f2afb2006-03-06 19:28:35 -08006810/* Test for DMA addresses > 40-bit */
6811static inline int tg3_40bit_overflow_test(struct tg3 *tp, dma_addr_t mapping,
6812 int len)
6813{
6814#if defined(CONFIG_HIGHMEM) && (BITS_PER_LONG == 64)
Joe Perches63c3a662011-04-26 08:12:10 +00006815 if (tg3_flag(tp, 40BIT_DMA_BUG))
Eric Dumazet807540b2010-09-23 05:40:09 +00006816 return ((u64) mapping + len) > DMA_BIT_MASK(40);
Michael Chan72f2afb2006-03-06 19:28:35 -08006817 return 0;
6818#else
6819 return 0;
6820#endif
6821}
6822
Matt Carlsond1a3b732011-07-27 14:20:51 +00006823static inline void tg3_tx_set_bd(struct tg3_tx_buffer_desc *txbd,
Matt Carlson92cd3a12011-07-27 14:20:47 +00006824 dma_addr_t mapping, u32 len, u32 flags,
6825 u32 mss, u32 vlan)
Matt Carlson2ffcc982011-05-19 12:12:44 +00006826{
Matt Carlson92cd3a12011-07-27 14:20:47 +00006827 txbd->addr_hi = ((u64) mapping >> 32);
6828 txbd->addr_lo = ((u64) mapping & 0xffffffff);
6829 txbd->len_flags = (len << TXD_LEN_SHIFT) | (flags & 0x0000ffff);
6830 txbd->vlan_tag = (mss << TXD_MSS_SHIFT) | (vlan << TXD_VLAN_TAG_SHIFT);
Matt Carlson2ffcc982011-05-19 12:12:44 +00006831}
Linus Torvalds1da177e2005-04-16 15:20:36 -07006832
Matt Carlson84b67b22011-07-27 14:20:52 +00006833static bool tg3_tx_frag_set(struct tg3_napi *tnapi, u32 *entry, u32 *budget,
Matt Carlsond1a3b732011-07-27 14:20:51 +00006834 dma_addr_t map, u32 len, u32 flags,
6835 u32 mss, u32 vlan)
6836{
6837 struct tg3 *tp = tnapi->tp;
6838 bool hwbug = false;
6839
6840 if (tg3_flag(tp, SHORT_DMA_BUG) && len <= 8)
Rusty Russell3db1cd52011-12-19 13:56:45 +00006841 hwbug = true;
Matt Carlsond1a3b732011-07-27 14:20:51 +00006842
6843 if (tg3_4g_overflow_test(map, len))
Rusty Russell3db1cd52011-12-19 13:56:45 +00006844 hwbug = true;
Matt Carlsond1a3b732011-07-27 14:20:51 +00006845
6846 if (tg3_40bit_overflow_test(tp, map, len))
Rusty Russell3db1cd52011-12-19 13:56:45 +00006847 hwbug = true;
Matt Carlsond1a3b732011-07-27 14:20:51 +00006848
Matt Carlsona4cb4282011-12-14 11:09:58 +00006849 if (tp->dma_limit) {
Matt Carlsonb9e45482011-11-04 09:14:59 +00006850 u32 prvidx = *entry;
Matt Carlsone31aa982011-07-27 14:20:53 +00006851 u32 tmp_flag = flags & ~TXD_FLAG_END;
Matt Carlsona4cb4282011-12-14 11:09:58 +00006852 while (len > tp->dma_limit && *budget) {
6853 u32 frag_len = tp->dma_limit;
6854 len -= tp->dma_limit;
Matt Carlsone31aa982011-07-27 14:20:53 +00006855
Matt Carlsonb9e45482011-11-04 09:14:59 +00006856 /* Avoid the 8byte DMA problem */
6857 if (len <= 8) {
Matt Carlsona4cb4282011-12-14 11:09:58 +00006858 len += tp->dma_limit / 2;
6859 frag_len = tp->dma_limit / 2;
Matt Carlsone31aa982011-07-27 14:20:53 +00006860 }
6861
Matt Carlsonb9e45482011-11-04 09:14:59 +00006862 tnapi->tx_buffers[*entry].fragmented = true;
6863
6864 tg3_tx_set_bd(&tnapi->tx_ring[*entry], map,
6865 frag_len, tmp_flag, mss, vlan);
6866 *budget -= 1;
6867 prvidx = *entry;
6868 *entry = NEXT_TX(*entry);
6869
Matt Carlsone31aa982011-07-27 14:20:53 +00006870 map += frag_len;
6871 }
6872
6873 if (len) {
6874 if (*budget) {
6875 tg3_tx_set_bd(&tnapi->tx_ring[*entry], map,
6876 len, flags, mss, vlan);
Matt Carlsonb9e45482011-11-04 09:14:59 +00006877 *budget -= 1;
Matt Carlsone31aa982011-07-27 14:20:53 +00006878 *entry = NEXT_TX(*entry);
6879 } else {
Rusty Russell3db1cd52011-12-19 13:56:45 +00006880 hwbug = true;
Matt Carlsonb9e45482011-11-04 09:14:59 +00006881 tnapi->tx_buffers[prvidx].fragmented = false;
Matt Carlsone31aa982011-07-27 14:20:53 +00006882 }
6883 }
6884 } else {
Matt Carlson84b67b22011-07-27 14:20:52 +00006885 tg3_tx_set_bd(&tnapi->tx_ring[*entry], map,
6886 len, flags, mss, vlan);
Matt Carlsone31aa982011-07-27 14:20:53 +00006887 *entry = NEXT_TX(*entry);
6888 }
Matt Carlsond1a3b732011-07-27 14:20:51 +00006889
6890 return hwbug;
6891}
6892
Matt Carlson0d681b22011-07-27 14:20:49 +00006893static void tg3_tx_skb_unmap(struct tg3_napi *tnapi, u32 entry, int last)
Matt Carlson432aa7e2011-05-19 12:12:45 +00006894{
6895 int i;
Matt Carlson0d681b22011-07-27 14:20:49 +00006896 struct sk_buff *skb;
Matt Carlsondf8944c2011-07-27 14:20:46 +00006897 struct tg3_tx_ring_info *txb = &tnapi->tx_buffers[entry];
Matt Carlson432aa7e2011-05-19 12:12:45 +00006898
Matt Carlson0d681b22011-07-27 14:20:49 +00006899 skb = txb->skb;
6900 txb->skb = NULL;
6901
Matt Carlson432aa7e2011-05-19 12:12:45 +00006902 pci_unmap_single(tnapi->tp->pdev,
6903 dma_unmap_addr(txb, mapping),
6904 skb_headlen(skb),
6905 PCI_DMA_TODEVICE);
Matt Carlsone01ee142011-07-27 14:20:50 +00006906
6907 while (txb->fragmented) {
6908 txb->fragmented = false;
6909 entry = NEXT_TX(entry);
6910 txb = &tnapi->tx_buffers[entry];
6911 }
6912
Matt Carlsonba1142e2011-11-04 09:15:00 +00006913 for (i = 0; i <= last; i++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00006914 const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
Matt Carlson432aa7e2011-05-19 12:12:45 +00006915
6916 entry = NEXT_TX(entry);
6917 txb = &tnapi->tx_buffers[entry];
6918
6919 pci_unmap_page(tnapi->tp->pdev,
6920 dma_unmap_addr(txb, mapping),
Eric Dumazet9e903e02011-10-18 21:00:24 +00006921 skb_frag_size(frag), PCI_DMA_TODEVICE);
Matt Carlsone01ee142011-07-27 14:20:50 +00006922
6923 while (txb->fragmented) {
6924 txb->fragmented = false;
6925 entry = NEXT_TX(entry);
6926 txb = &tnapi->tx_buffers[entry];
6927 }
Matt Carlson432aa7e2011-05-19 12:12:45 +00006928 }
6929}
6930
Michael Chan72f2afb2006-03-06 19:28:35 -08006931/* Workaround 4GB and 40-bit hardware DMA bugs. */
Matt Carlson24f4efd2009-11-13 13:03:35 +00006932static int tigon3_dma_hwbug_workaround(struct tg3_napi *tnapi,
David S. Miller1805b2f2011-10-24 18:18:09 -04006933 struct sk_buff **pskb,
Matt Carlson84b67b22011-07-27 14:20:52 +00006934 u32 *entry, u32 *budget,
Matt Carlson92cd3a12011-07-27 14:20:47 +00006935 u32 base_flags, u32 mss, u32 vlan)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006936{
Matt Carlson24f4efd2009-11-13 13:03:35 +00006937 struct tg3 *tp = tnapi->tp;
David S. Miller1805b2f2011-10-24 18:18:09 -04006938 struct sk_buff *new_skb, *skb = *pskb;
Michael Chanc58ec932005-09-17 00:46:27 -07006939 dma_addr_t new_addr = 0;
Matt Carlson432aa7e2011-05-19 12:12:45 +00006940 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006941
Matt Carlson41588ba2008-04-19 18:12:33 -07006942 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701)
6943 new_skb = skb_copy(skb, GFP_ATOMIC);
6944 else {
6945 int more_headroom = 4 - ((unsigned long)skb->data & 3);
6946
6947 new_skb = skb_copy_expand(skb,
6948 skb_headroom(skb) + more_headroom,
6949 skb_tailroom(skb), GFP_ATOMIC);
6950 }
6951
Linus Torvalds1da177e2005-04-16 15:20:36 -07006952 if (!new_skb) {
Michael Chanc58ec932005-09-17 00:46:27 -07006953 ret = -1;
6954 } else {
6955 /* New SKB is guaranteed to be linear. */
Alexander Duyckf4188d82009-12-02 16:48:38 +00006956 new_addr = pci_map_single(tp->pdev, new_skb->data, new_skb->len,
6957 PCI_DMA_TODEVICE);
6958 /* Make sure the mapping succeeded */
6959 if (pci_dma_mapping_error(tp->pdev, new_addr)) {
Alexander Duyckf4188d82009-12-02 16:48:38 +00006960 dev_kfree_skb(new_skb);
Michael Chanc58ec932005-09-17 00:46:27 -07006961 ret = -1;
Michael Chanc58ec932005-09-17 00:46:27 -07006962 } else {
Matt Carlsonb9e45482011-11-04 09:14:59 +00006963 u32 save_entry = *entry;
6964
Matt Carlson92cd3a12011-07-27 14:20:47 +00006965 base_flags |= TXD_FLAG_END;
6966
Matt Carlson84b67b22011-07-27 14:20:52 +00006967 tnapi->tx_buffers[*entry].skb = new_skb;
6968 dma_unmap_addr_set(&tnapi->tx_buffers[*entry],
Matt Carlson432aa7e2011-05-19 12:12:45 +00006969 mapping, new_addr);
6970
Matt Carlson84b67b22011-07-27 14:20:52 +00006971 if (tg3_tx_frag_set(tnapi, entry, budget, new_addr,
Matt Carlsond1a3b732011-07-27 14:20:51 +00006972 new_skb->len, base_flags,
6973 mss, vlan)) {
Matt Carlsonba1142e2011-11-04 09:15:00 +00006974 tg3_tx_skb_unmap(tnapi, save_entry, -1);
Matt Carlsond1a3b732011-07-27 14:20:51 +00006975 dev_kfree_skb(new_skb);
6976 ret = -1;
6977 }
Michael Chanc58ec932005-09-17 00:46:27 -07006978 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006979 }
6980
Linus Torvalds1da177e2005-04-16 15:20:36 -07006981 dev_kfree_skb(skb);
David S. Miller1805b2f2011-10-24 18:18:09 -04006982 *pskb = new_skb;
Michael Chanc58ec932005-09-17 00:46:27 -07006983 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006984}
6985
Matt Carlson2ffcc982011-05-19 12:12:44 +00006986static netdev_tx_t tg3_start_xmit(struct sk_buff *, struct net_device *);
Michael Chan52c0fd82006-06-29 20:15:54 -07006987
6988/* Use GSO to workaround a rare TSO bug that may be triggered when the
6989 * TSO header is greater than 80 bytes.
6990 */
6991static int tg3_tso_bug(struct tg3 *tp, struct sk_buff *skb)
6992{
6993 struct sk_buff *segs, *nskb;
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006994 u32 frag_cnt_est = skb_shinfo(skb)->gso_segs * 3;
Michael Chan52c0fd82006-06-29 20:15:54 -07006995
6996 /* Estimate the number of fragments in the worst case */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006997 if (unlikely(tg3_tx_avail(&tp->napi[0]) <= frag_cnt_est)) {
Michael Chan52c0fd82006-06-29 20:15:54 -07006998 netif_stop_queue(tp->dev);
Matt Carlsonf65aac12010-08-02 11:26:03 +00006999
7000 /* netif_tx_stop_queue() must be done before checking
7001 * checking tx index in tg3_tx_avail() below, because in
7002 * tg3_tx(), we update tx index before checking for
7003 * netif_tx_queue_stopped().
7004 */
7005 smp_mb();
Matt Carlsonf3f3f272009-08-28 14:03:21 +00007006 if (tg3_tx_avail(&tp->napi[0]) <= frag_cnt_est)
Michael Chan7f62ad52007-02-20 23:25:40 -08007007 return NETDEV_TX_BUSY;
7008
7009 netif_wake_queue(tp->dev);
Michael Chan52c0fd82006-06-29 20:15:54 -07007010 }
7011
7012 segs = skb_gso_segment(skb, tp->dev->features & ~NETIF_F_TSO);
Hirofumi Nakagawa801678c2008-04-29 01:03:09 -07007013 if (IS_ERR(segs))
Michael Chan52c0fd82006-06-29 20:15:54 -07007014 goto tg3_tso_bug_end;
7015
7016 do {
7017 nskb = segs;
7018 segs = segs->next;
7019 nskb->next = NULL;
Matt Carlson2ffcc982011-05-19 12:12:44 +00007020 tg3_start_xmit(nskb, tp->dev);
Michael Chan52c0fd82006-06-29 20:15:54 -07007021 } while (segs);
7022
7023tg3_tso_bug_end:
7024 dev_kfree_skb(skb);
7025
7026 return NETDEV_TX_OK;
7027}
Michael Chan52c0fd82006-06-29 20:15:54 -07007028
Michael Chan5a6f3072006-03-20 22:28:05 -08007029/* hard_start_xmit for devices that have the 4G bug and/or 40-bit bug and
Joe Perches63c3a662011-04-26 08:12:10 +00007030 * support TG3_FLAG_HW_TSO_1 or firmware TSO only.
Michael Chan5a6f3072006-03-20 22:28:05 -08007031 */
Matt Carlson2ffcc982011-05-19 12:12:44 +00007032static netdev_tx_t tg3_start_xmit(struct sk_buff *skb, struct net_device *dev)
Michael Chan5a6f3072006-03-20 22:28:05 -08007033{
7034 struct tg3 *tp = netdev_priv(dev);
Matt Carlson92cd3a12011-07-27 14:20:47 +00007035 u32 len, entry, base_flags, mss, vlan = 0;
Matt Carlson84b67b22011-07-27 14:20:52 +00007036 u32 budget;
Matt Carlson432aa7e2011-05-19 12:12:45 +00007037 int i = -1, would_hit_hwbug;
David S. Miller90079ce2008-09-11 04:52:51 -07007038 dma_addr_t mapping;
Matt Carlson24f4efd2009-11-13 13:03:35 +00007039 struct tg3_napi *tnapi;
7040 struct netdev_queue *txq;
Matt Carlson432aa7e2011-05-19 12:12:45 +00007041 unsigned int last;
Alexander Duyckf4188d82009-12-02 16:48:38 +00007042
Matt Carlson24f4efd2009-11-13 13:03:35 +00007043 txq = netdev_get_tx_queue(dev, skb_get_queue_mapping(skb));
7044 tnapi = &tp->napi[skb_get_queue_mapping(skb)];
Joe Perches63c3a662011-04-26 08:12:10 +00007045 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlson24f4efd2009-11-13 13:03:35 +00007046 tnapi++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007047
Matt Carlson84b67b22011-07-27 14:20:52 +00007048 budget = tg3_tx_avail(tnapi);
7049
Michael Chan00b70502006-06-17 21:58:45 -07007050 /* We are running in BH disabled context with netif_tx_lock
Stephen Hemmingerbea33482007-10-03 16:41:36 -07007051 * and TX reclaim runs via tp->napi.poll inside of a software
David S. Millerf47c11e2005-06-24 20:18:35 -07007052 * interrupt. Furthermore, IRQ processing runs lockless so we have
7053 * no IRQ context deadlocks to worry about either. Rejoice!
Linus Torvalds1da177e2005-04-16 15:20:36 -07007054 */
Matt Carlson84b67b22011-07-27 14:20:52 +00007055 if (unlikely(budget <= (skb_shinfo(skb)->nr_frags + 1))) {
Matt Carlson24f4efd2009-11-13 13:03:35 +00007056 if (!netif_tx_queue_stopped(txq)) {
7057 netif_tx_stop_queue(txq);
Stephen Hemminger1f064a82005-12-06 17:36:44 -08007058
7059 /* This is a hard error, log it. */
Matt Carlson5129c3a2010-04-05 10:19:23 +00007060 netdev_err(dev,
7061 "BUG! Tx Ring full when queue awake!\n");
Stephen Hemminger1f064a82005-12-06 17:36:44 -08007062 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007063 return NETDEV_TX_BUSY;
7064 }
7065
Matt Carlsonf3f3f272009-08-28 14:03:21 +00007066 entry = tnapi->tx_prod;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007067 base_flags = 0;
Patrick McHardy84fa7932006-08-29 16:44:56 -07007068 if (skb->ip_summed == CHECKSUM_PARTIAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007069 base_flags |= TXD_FLAG_TCPUDP_CSUM;
Matt Carlson24f4efd2009-11-13 13:03:35 +00007070
Matt Carlsonbe98da62010-07-11 09:31:46 +00007071 mss = skb_shinfo(skb)->gso_size;
7072 if (mss) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07007073 struct iphdr *iph;
Matt Carlson34195c32010-07-11 09:31:42 +00007074 u32 tcp_opt_len, hdr_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007075
7076 if (skb_header_cloned(skb) &&
Eric Dumazet48855432011-10-24 07:53:03 +00007077 pskb_expand_head(skb, 0, 0, GFP_ATOMIC))
7078 goto drop;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007079
Matt Carlson34195c32010-07-11 09:31:42 +00007080 iph = ip_hdr(skb);
Arnaldo Carvalho de Meloab6a5bb2007-03-18 17:43:48 -07007081 tcp_opt_len = tcp_optlen(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007082
Eric Dumazeta5a11952012-01-23 01:22:09 +00007083 hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb) - ETH_HLEN;
Matt Carlson34195c32010-07-11 09:31:42 +00007084
Eric Dumazeta5a11952012-01-23 01:22:09 +00007085 if (!skb_is_gso_v6(skb)) {
Matt Carlson34195c32010-07-11 09:31:42 +00007086 iph->check = 0;
7087 iph->tot_len = htons(mss + hdr_len);
7088 }
7089
Michael Chan52c0fd82006-06-29 20:15:54 -07007090 if (unlikely((ETH_HLEN + hdr_len) > 80) &&
Joe Perches63c3a662011-04-26 08:12:10 +00007091 tg3_flag(tp, TSO_BUG))
Matt Carlsonde6f31e2010-04-12 06:58:30 +00007092 return tg3_tso_bug(tp, skb);
Michael Chan52c0fd82006-06-29 20:15:54 -07007093
Linus Torvalds1da177e2005-04-16 15:20:36 -07007094 base_flags |= (TXD_FLAG_CPU_PRE_DMA |
7095 TXD_FLAG_CPU_POST_DMA);
7096
Joe Perches63c3a662011-04-26 08:12:10 +00007097 if (tg3_flag(tp, HW_TSO_1) ||
7098 tg3_flag(tp, HW_TSO_2) ||
7099 tg3_flag(tp, HW_TSO_3)) {
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07007100 tcp_hdr(skb)->check = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007101 base_flags &= ~TXD_FLAG_TCPUDP_CSUM;
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07007102 } else
7103 tcp_hdr(skb)->check = ~csum_tcpudp_magic(iph->saddr,
7104 iph->daddr, 0,
7105 IPPROTO_TCP,
7106 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007107
Joe Perches63c3a662011-04-26 08:12:10 +00007108 if (tg3_flag(tp, HW_TSO_3)) {
Matt Carlson615774f2009-11-13 13:03:39 +00007109 mss |= (hdr_len & 0xc) << 12;
7110 if (hdr_len & 0x10)
7111 base_flags |= 0x00000010;
7112 base_flags |= (hdr_len & 0x3e0) << 5;
Joe Perches63c3a662011-04-26 08:12:10 +00007113 } else if (tg3_flag(tp, HW_TSO_2))
Matt Carlson92c6b8d2009-11-02 14:23:27 +00007114 mss |= hdr_len << 9;
Joe Perches63c3a662011-04-26 08:12:10 +00007115 else if (tg3_flag(tp, HW_TSO_1) ||
Matt Carlson92c6b8d2009-11-02 14:23:27 +00007116 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07007117 if (tcp_opt_len || iph->ihl > 5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007118 int tsflags;
7119
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07007120 tsflags = (iph->ihl - 5) + (tcp_opt_len >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007121 mss |= (tsflags << 11);
7122 }
7123 } else {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07007124 if (tcp_opt_len || iph->ihl > 5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007125 int tsflags;
7126
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07007127 tsflags = (iph->ihl - 5) + (tcp_opt_len >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007128 base_flags |= tsflags << 12;
7129 }
7130 }
7131 }
Matt Carlsonbf933c82011-01-25 15:58:49 +00007132
Matt Carlson93a700a2011-08-31 11:44:54 +00007133 if (tg3_flag(tp, USE_JUMBO_BDFLAG) &&
7134 !mss && skb->len > VLAN_ETH_FRAME_LEN)
7135 base_flags |= TXD_FLAG_JMB_PKT;
7136
Matt Carlson92cd3a12011-07-27 14:20:47 +00007137 if (vlan_tx_tag_present(skb)) {
7138 base_flags |= TXD_FLAG_VLAN;
7139 vlan = vlan_tx_tag_get(skb);
7140 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007141
Alexander Duyckf4188d82009-12-02 16:48:38 +00007142 len = skb_headlen(skb);
7143
7144 mapping = pci_map_single(tp->pdev, skb->data, len, PCI_DMA_TODEVICE);
Eric Dumazet48855432011-10-24 07:53:03 +00007145 if (pci_dma_mapping_error(tp->pdev, mapping))
7146 goto drop;
7147
David S. Miller90079ce2008-09-11 04:52:51 -07007148
Matt Carlsonf3f3f272009-08-28 14:03:21 +00007149 tnapi->tx_buffers[entry].skb = skb;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00007150 dma_unmap_addr_set(&tnapi->tx_buffers[entry], mapping, mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007151
7152 would_hit_hwbug = 0;
7153
Joe Perches63c3a662011-04-26 08:12:10 +00007154 if (tg3_flag(tp, 5701_DMA_BUG))
Michael Chanc58ec932005-09-17 00:46:27 -07007155 would_hit_hwbug = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007156
Matt Carlson84b67b22011-07-27 14:20:52 +00007157 if (tg3_tx_frag_set(tnapi, &entry, &budget, mapping, len, base_flags |
Matt Carlsond1a3b732011-07-27 14:20:51 +00007158 ((skb_shinfo(skb)->nr_frags == 0) ? TXD_FLAG_END : 0),
Matt Carlsonba1142e2011-11-04 09:15:00 +00007159 mss, vlan)) {
Matt Carlsond1a3b732011-07-27 14:20:51 +00007160 would_hit_hwbug = 1;
Matt Carlsonba1142e2011-11-04 09:15:00 +00007161 } else if (skb_shinfo(skb)->nr_frags > 0) {
Matt Carlson92cd3a12011-07-27 14:20:47 +00007162 u32 tmp_mss = mss;
7163
7164 if (!tg3_flag(tp, HW_TSO_1) &&
7165 !tg3_flag(tp, HW_TSO_2) &&
7166 !tg3_flag(tp, HW_TSO_3))
7167 tmp_mss = 0;
7168
Matt Carlsonc5665a52012-02-13 10:20:12 +00007169 /* Now loop through additional data
7170 * fragments, and queue them.
7171 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007172 last = skb_shinfo(skb)->nr_frags - 1;
7173 for (i = 0; i <= last; i++) {
7174 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
7175
Eric Dumazet9e903e02011-10-18 21:00:24 +00007176 len = skb_frag_size(frag);
Ian Campbelldc234d02011-08-24 22:28:11 +00007177 mapping = skb_frag_dma_map(&tp->pdev->dev, frag, 0,
Ian Campbell5d6bcdf2011-10-06 11:10:48 +01007178 len, DMA_TO_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007179
Matt Carlsonf3f3f272009-08-28 14:03:21 +00007180 tnapi->tx_buffers[entry].skb = NULL;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00007181 dma_unmap_addr_set(&tnapi->tx_buffers[entry], mapping,
Alexander Duyckf4188d82009-12-02 16:48:38 +00007182 mapping);
Ian Campbell5d6bcdf2011-10-06 11:10:48 +01007183 if (dma_mapping_error(&tp->pdev->dev, mapping))
Alexander Duyckf4188d82009-12-02 16:48:38 +00007184 goto dma_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007185
Matt Carlsonb9e45482011-11-04 09:14:59 +00007186 if (!budget ||
7187 tg3_tx_frag_set(tnapi, &entry, &budget, mapping,
Matt Carlson84b67b22011-07-27 14:20:52 +00007188 len, base_flags |
7189 ((i == last) ? TXD_FLAG_END : 0),
Matt Carlsonb9e45482011-11-04 09:14:59 +00007190 tmp_mss, vlan)) {
Matt Carlson92c6b8d2009-11-02 14:23:27 +00007191 would_hit_hwbug = 1;
Matt Carlsonb9e45482011-11-04 09:14:59 +00007192 break;
7193 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007194 }
7195 }
7196
7197 if (would_hit_hwbug) {
Matt Carlson0d681b22011-07-27 14:20:49 +00007198 tg3_tx_skb_unmap(tnapi, tnapi->tx_prod, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007199
7200 /* If the workaround fails due to memory/mapping
7201 * failure, silently drop this packet.
7202 */
Matt Carlson84b67b22011-07-27 14:20:52 +00007203 entry = tnapi->tx_prod;
7204 budget = tg3_tx_avail(tnapi);
David S. Miller1805b2f2011-10-24 18:18:09 -04007205 if (tigon3_dma_hwbug_workaround(tnapi, &skb, &entry, &budget,
Matt Carlson84b67b22011-07-27 14:20:52 +00007206 base_flags, mss, vlan))
Eric Dumazet48855432011-10-24 07:53:03 +00007207 goto drop_nofree;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007208 }
7209
Richard Cochrand515b452011-06-19 03:31:41 +00007210 skb_tx_timestamp(skb);
Tom Herbert5cb917b2012-03-05 19:53:50 +00007211 netdev_tx_sent_queue(txq, skb->len);
Richard Cochrand515b452011-06-19 03:31:41 +00007212
Michael Chan6541b802012-03-04 14:48:14 +00007213 /* Sync BD data before updating mailbox */
7214 wmb();
7215
Linus Torvalds1da177e2005-04-16 15:20:36 -07007216 /* Packets are ready, update Tx producer idx local and on card. */
Matt Carlson24f4efd2009-11-13 13:03:35 +00007217 tw32_tx_mbox(tnapi->prodmbox, entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007218
Matt Carlsonf3f3f272009-08-28 14:03:21 +00007219 tnapi->tx_prod = entry;
7220 if (unlikely(tg3_tx_avail(tnapi) <= (MAX_SKB_FRAGS + 1))) {
Matt Carlson24f4efd2009-11-13 13:03:35 +00007221 netif_tx_stop_queue(txq);
Matt Carlsonf65aac12010-08-02 11:26:03 +00007222
7223 /* netif_tx_stop_queue() must be done before checking
7224 * checking tx index in tg3_tx_avail() below, because in
7225 * tg3_tx(), we update tx index before checking for
7226 * netif_tx_queue_stopped().
7227 */
7228 smp_mb();
Matt Carlsonf3f3f272009-08-28 14:03:21 +00007229 if (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi))
Matt Carlson24f4efd2009-11-13 13:03:35 +00007230 netif_tx_wake_queue(txq);
Michael Chan51b91462005-09-01 17:41:28 -07007231 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007232
Eric Dumazetcdd0db02009-05-28 00:00:41 +00007233 mmiowb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007234 return NETDEV_TX_OK;
Alexander Duyckf4188d82009-12-02 16:48:38 +00007235
7236dma_error:
Matt Carlsonba1142e2011-11-04 09:15:00 +00007237 tg3_tx_skb_unmap(tnapi, tnapi->tx_prod, --i);
Matt Carlson432aa7e2011-05-19 12:12:45 +00007238 tnapi->tx_buffers[tnapi->tx_prod].skb = NULL;
Eric Dumazet48855432011-10-24 07:53:03 +00007239drop:
7240 dev_kfree_skb(skb);
7241drop_nofree:
7242 tp->tx_dropped++;
Alexander Duyckf4188d82009-12-02 16:48:38 +00007243 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007244}
7245
Matt Carlson6e01b202011-08-19 13:58:20 +00007246static void tg3_mac_loopback(struct tg3 *tp, bool enable)
7247{
7248 if (enable) {
7249 tp->mac_mode &= ~(MAC_MODE_HALF_DUPLEX |
7250 MAC_MODE_PORT_MODE_MASK);
7251
7252 tp->mac_mode |= MAC_MODE_PORT_INT_LPBACK;
7253
7254 if (!tg3_flag(tp, 5705_PLUS))
7255 tp->mac_mode |= MAC_MODE_LINK_POLARITY;
7256
7257 if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
7258 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
7259 else
7260 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
7261 } else {
7262 tp->mac_mode &= ~MAC_MODE_PORT_INT_LPBACK;
7263
7264 if (tg3_flag(tp, 5705_PLUS) ||
7265 (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) ||
7266 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700)
7267 tp->mac_mode &= ~MAC_MODE_LINK_POLARITY;
7268 }
7269
7270 tw32(MAC_MODE, tp->mac_mode);
7271 udelay(40);
7272}
7273
Matt Carlson941ec902011-08-19 13:58:23 +00007274static int tg3_phy_lpbk_set(struct tg3 *tp, u32 speed, bool extlpbk)
Matt Carlson5e5a7f32011-08-19 13:58:21 +00007275{
Matt Carlson941ec902011-08-19 13:58:23 +00007276 u32 val, bmcr, mac_mode, ptest = 0;
Matt Carlson5e5a7f32011-08-19 13:58:21 +00007277
7278 tg3_phy_toggle_apd(tp, false);
7279 tg3_phy_toggle_automdix(tp, 0);
7280
Matt Carlson941ec902011-08-19 13:58:23 +00007281 if (extlpbk && tg3_phy_set_extloopbk(tp))
7282 return -EIO;
7283
7284 bmcr = BMCR_FULLDPLX;
Matt Carlson5e5a7f32011-08-19 13:58:21 +00007285 switch (speed) {
7286 case SPEED_10:
7287 break;
7288 case SPEED_100:
7289 bmcr |= BMCR_SPEED100;
7290 break;
7291 case SPEED_1000:
7292 default:
7293 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
7294 speed = SPEED_100;
7295 bmcr |= BMCR_SPEED100;
7296 } else {
7297 speed = SPEED_1000;
7298 bmcr |= BMCR_SPEED1000;
7299 }
7300 }
7301
Matt Carlson941ec902011-08-19 13:58:23 +00007302 if (extlpbk) {
7303 if (!(tp->phy_flags & TG3_PHYFLG_IS_FET)) {
7304 tg3_readphy(tp, MII_CTRL1000, &val);
7305 val |= CTL1000_AS_MASTER |
7306 CTL1000_ENABLE_MASTER;
7307 tg3_writephy(tp, MII_CTRL1000, val);
7308 } else {
7309 ptest = MII_TG3_FET_PTEST_TRIM_SEL |
7310 MII_TG3_FET_PTEST_TRIM_2;
7311 tg3_writephy(tp, MII_TG3_FET_PTEST, ptest);
7312 }
7313 } else
7314 bmcr |= BMCR_LOOPBACK;
7315
Matt Carlson5e5a7f32011-08-19 13:58:21 +00007316 tg3_writephy(tp, MII_BMCR, bmcr);
7317
7318 /* The write needs to be flushed for the FETs */
7319 if (tp->phy_flags & TG3_PHYFLG_IS_FET)
7320 tg3_readphy(tp, MII_BMCR, &bmcr);
7321
7322 udelay(40);
7323
7324 if ((tp->phy_flags & TG3_PHYFLG_IS_FET) &&
7325 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785) {
Matt Carlson941ec902011-08-19 13:58:23 +00007326 tg3_writephy(tp, MII_TG3_FET_PTEST, ptest |
Matt Carlson5e5a7f32011-08-19 13:58:21 +00007327 MII_TG3_FET_PTEST_FRC_TX_LINK |
7328 MII_TG3_FET_PTEST_FRC_TX_LOCK);
7329
7330 /* The write needs to be flushed for the AC131 */
7331 tg3_readphy(tp, MII_TG3_FET_PTEST, &val);
7332 }
7333
7334 /* Reset to prevent losing 1st rx packet intermittently */
7335 if ((tp->phy_flags & TG3_PHYFLG_MII_SERDES) &&
7336 tg3_flag(tp, 5780_CLASS)) {
7337 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
7338 udelay(10);
7339 tw32_f(MAC_RX_MODE, tp->rx_mode);
7340 }
7341
7342 mac_mode = tp->mac_mode &
7343 ~(MAC_MODE_PORT_MODE_MASK | MAC_MODE_HALF_DUPLEX);
7344 if (speed == SPEED_1000)
7345 mac_mode |= MAC_MODE_PORT_MODE_GMII;
7346 else
7347 mac_mode |= MAC_MODE_PORT_MODE_MII;
7348
7349 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700) {
7350 u32 masked_phy_id = tp->phy_id & TG3_PHY_ID_MASK;
7351
7352 if (masked_phy_id == TG3_PHY_ID_BCM5401)
7353 mac_mode &= ~MAC_MODE_LINK_POLARITY;
7354 else if (masked_phy_id == TG3_PHY_ID_BCM5411)
7355 mac_mode |= MAC_MODE_LINK_POLARITY;
7356
7357 tg3_writephy(tp, MII_TG3_EXT_CTRL,
7358 MII_TG3_EXT_CTRL_LNK3_LED_MODE);
7359 }
7360
7361 tw32(MAC_MODE, mac_mode);
7362 udelay(40);
Matt Carlson941ec902011-08-19 13:58:23 +00007363
7364 return 0;
Matt Carlson5e5a7f32011-08-19 13:58:21 +00007365}
7366
Michał Mirosławc8f44af2011-11-15 15:29:55 +00007367static void tg3_set_loopback(struct net_device *dev, netdev_features_t features)
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00007368{
7369 struct tg3 *tp = netdev_priv(dev);
7370
7371 if (features & NETIF_F_LOOPBACK) {
7372 if (tp->mac_mode & MAC_MODE_PORT_INT_LPBACK)
7373 return;
7374
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00007375 spin_lock_bh(&tp->lock);
Matt Carlson6e01b202011-08-19 13:58:20 +00007376 tg3_mac_loopback(tp, true);
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00007377 netif_carrier_on(tp->dev);
7378 spin_unlock_bh(&tp->lock);
7379 netdev_info(dev, "Internal MAC loopback mode enabled.\n");
7380 } else {
7381 if (!(tp->mac_mode & MAC_MODE_PORT_INT_LPBACK))
7382 return;
7383
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00007384 spin_lock_bh(&tp->lock);
Matt Carlson6e01b202011-08-19 13:58:20 +00007385 tg3_mac_loopback(tp, false);
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00007386 /* Force link status check */
7387 tg3_setup_phy(tp, 1);
7388 spin_unlock_bh(&tp->lock);
7389 netdev_info(dev, "Internal MAC loopback mode disabled.\n");
7390 }
7391}
7392
Michał Mirosławc8f44af2011-11-15 15:29:55 +00007393static netdev_features_t tg3_fix_features(struct net_device *dev,
7394 netdev_features_t features)
Michał Mirosławdc668912011-04-07 03:35:07 +00007395{
7396 struct tg3 *tp = netdev_priv(dev);
7397
Joe Perches63c3a662011-04-26 08:12:10 +00007398 if (dev->mtu > ETH_DATA_LEN && tg3_flag(tp, 5780_CLASS))
Michał Mirosławdc668912011-04-07 03:35:07 +00007399 features &= ~NETIF_F_ALL_TSO;
7400
7401 return features;
7402}
7403
Michał Mirosławc8f44af2011-11-15 15:29:55 +00007404static int tg3_set_features(struct net_device *dev, netdev_features_t features)
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00007405{
Michał Mirosławc8f44af2011-11-15 15:29:55 +00007406 netdev_features_t changed = dev->features ^ features;
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00007407
7408 if ((changed & NETIF_F_LOOPBACK) && netif_running(dev))
7409 tg3_set_loopback(dev, features);
7410
7411 return 0;
7412}
7413
Matt Carlson21f581a2009-08-28 14:00:25 +00007414static void tg3_rx_prodring_free(struct tg3 *tp,
7415 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007416{
Linus Torvalds1da177e2005-04-16 15:20:36 -07007417 int i;
7418
Matt Carlson8fea32b2010-09-15 08:59:58 +00007419 if (tpr != &tp->napi[0].prodring) {
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007420 for (i = tpr->rx_std_cons_idx; i != tpr->rx_std_prod_idx;
Matt Carlson2c49a442010-09-30 10:34:35 +00007421 i = (i + 1) & tp->rx_std_ring_mask)
Eric Dumazet9205fd92011-11-18 06:47:01 +00007422 tg3_rx_data_free(tp, &tpr->rx_std_buffers[i],
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007423 tp->rx_pkt_map_sz);
7424
Joe Perches63c3a662011-04-26 08:12:10 +00007425 if (tg3_flag(tp, JUMBO_CAPABLE)) {
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007426 for (i = tpr->rx_jmb_cons_idx;
7427 i != tpr->rx_jmb_prod_idx;
Matt Carlson2c49a442010-09-30 10:34:35 +00007428 i = (i + 1) & tp->rx_jmb_ring_mask) {
Eric Dumazet9205fd92011-11-18 06:47:01 +00007429 tg3_rx_data_free(tp, &tpr->rx_jmb_buffers[i],
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007430 TG3_RX_JMB_MAP_SZ);
7431 }
7432 }
7433
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007434 return;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007435 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007436
Matt Carlson2c49a442010-09-30 10:34:35 +00007437 for (i = 0; i <= tp->rx_std_ring_mask; i++)
Eric Dumazet9205fd92011-11-18 06:47:01 +00007438 tg3_rx_data_free(tp, &tpr->rx_std_buffers[i],
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007439 tp->rx_pkt_map_sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007440
Joe Perches63c3a662011-04-26 08:12:10 +00007441 if (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS)) {
Matt Carlson2c49a442010-09-30 10:34:35 +00007442 for (i = 0; i <= tp->rx_jmb_ring_mask; i++)
Eric Dumazet9205fd92011-11-18 06:47:01 +00007443 tg3_rx_data_free(tp, &tpr->rx_jmb_buffers[i],
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007444 TG3_RX_JMB_MAP_SZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007445 }
7446}
7447
Matt Carlsonc6cdf432010-04-05 10:19:26 +00007448/* Initialize rx rings for packet processing.
Linus Torvalds1da177e2005-04-16 15:20:36 -07007449 *
7450 * The chip has been shut down and the driver detached from
7451 * the networking, so no interrupts or new tx packets will
7452 * end up in the driver. tp->{tx,}lock are held and thus
7453 * we may not sleep.
7454 */
Matt Carlson21f581a2009-08-28 14:00:25 +00007455static int tg3_rx_prodring_alloc(struct tg3 *tp,
7456 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007457{
Matt Carlson287be122009-08-28 13:58:46 +00007458 u32 i, rx_pkt_dma_sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007459
Matt Carlsonb196c7e2009-11-13 13:03:50 +00007460 tpr->rx_std_cons_idx = 0;
7461 tpr->rx_std_prod_idx = 0;
7462 tpr->rx_jmb_cons_idx = 0;
7463 tpr->rx_jmb_prod_idx = 0;
7464
Matt Carlson8fea32b2010-09-15 08:59:58 +00007465 if (tpr != &tp->napi[0].prodring) {
Matt Carlson2c49a442010-09-30 10:34:35 +00007466 memset(&tpr->rx_std_buffers[0], 0,
7467 TG3_RX_STD_BUFF_RING_SIZE(tp));
Matt Carlson48035722010-10-14 10:37:43 +00007468 if (tpr->rx_jmb_buffers)
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007469 memset(&tpr->rx_jmb_buffers[0], 0,
Matt Carlson2c49a442010-09-30 10:34:35 +00007470 TG3_RX_JMB_BUFF_RING_SIZE(tp));
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007471 goto done;
7472 }
7473
Linus Torvalds1da177e2005-04-16 15:20:36 -07007474 /* Zero out all descriptors. */
Matt Carlson2c49a442010-09-30 10:34:35 +00007475 memset(tpr->rx_std, 0, TG3_RX_STD_RING_BYTES(tp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007476
Matt Carlson287be122009-08-28 13:58:46 +00007477 rx_pkt_dma_sz = TG3_RX_STD_DMA_SZ;
Joe Perches63c3a662011-04-26 08:12:10 +00007478 if (tg3_flag(tp, 5780_CLASS) &&
Matt Carlson287be122009-08-28 13:58:46 +00007479 tp->dev->mtu > ETH_DATA_LEN)
7480 rx_pkt_dma_sz = TG3_RX_JMB_DMA_SZ;
7481 tp->rx_pkt_map_sz = TG3_RX_DMA_TO_MAP_SZ(rx_pkt_dma_sz);
Michael Chan7e72aad2005-07-25 12:31:17 -07007482
Linus Torvalds1da177e2005-04-16 15:20:36 -07007483 /* Initialize invariants of the rings, we only set this
7484 * stuff once. This works because the card does not
7485 * write into the rx buffer posting rings.
7486 */
Matt Carlson2c49a442010-09-30 10:34:35 +00007487 for (i = 0; i <= tp->rx_std_ring_mask; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007488 struct tg3_rx_buffer_desc *rxd;
7489
Matt Carlson21f581a2009-08-28 14:00:25 +00007490 rxd = &tpr->rx_std[i];
Matt Carlson287be122009-08-28 13:58:46 +00007491 rxd->idx_len = rx_pkt_dma_sz << RXD_LEN_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007492 rxd->type_flags = (RXD_FLAG_END << RXD_FLAGS_SHIFT);
7493 rxd->opaque = (RXD_OPAQUE_RING_STD |
7494 (i << RXD_OPAQUE_INDEX_SHIFT));
7495 }
7496
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007497 /* Now allocate fresh SKBs for each rx ring. */
7498 for (i = 0; i < tp->rx_pending; i++) {
Eric Dumazet8d4057a2012-04-27 00:34:49 +00007499 unsigned int frag_size;
7500
7501 if (tg3_alloc_rx_data(tp, tpr, RXD_OPAQUE_RING_STD, i,
7502 &frag_size) < 0) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00007503 netdev_warn(tp->dev,
7504 "Using a smaller RX standard ring. Only "
7505 "%d out of %d buffers were allocated "
7506 "successfully\n", i, tp->rx_pending);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007507 if (i == 0)
7508 goto initfail;
7509 tp->rx_pending = i;
7510 break;
7511 }
7512 }
7513
Joe Perches63c3a662011-04-26 08:12:10 +00007514 if (!tg3_flag(tp, JUMBO_CAPABLE) || tg3_flag(tp, 5780_CLASS))
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007515 goto done;
7516
Matt Carlson2c49a442010-09-30 10:34:35 +00007517 memset(tpr->rx_jmb, 0, TG3_RX_JMB_RING_BYTES(tp));
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007518
Joe Perches63c3a662011-04-26 08:12:10 +00007519 if (!tg3_flag(tp, JUMBO_RING_ENABLE))
Matt Carlson0d86df82010-02-17 15:17:00 +00007520 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007521
Matt Carlson2c49a442010-09-30 10:34:35 +00007522 for (i = 0; i <= tp->rx_jmb_ring_mask; i++) {
Matt Carlson0d86df82010-02-17 15:17:00 +00007523 struct tg3_rx_buffer_desc *rxd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007524
Matt Carlson0d86df82010-02-17 15:17:00 +00007525 rxd = &tpr->rx_jmb[i].std;
7526 rxd->idx_len = TG3_RX_JMB_DMA_SZ << RXD_LEN_SHIFT;
7527 rxd->type_flags = (RXD_FLAG_END << RXD_FLAGS_SHIFT) |
7528 RXD_FLAG_JUMBO;
7529 rxd->opaque = (RXD_OPAQUE_RING_JUMBO |
7530 (i << RXD_OPAQUE_INDEX_SHIFT));
7531 }
7532
7533 for (i = 0; i < tp->rx_jumbo_pending; i++) {
Eric Dumazet8d4057a2012-04-27 00:34:49 +00007534 unsigned int frag_size;
7535
7536 if (tg3_alloc_rx_data(tp, tpr, RXD_OPAQUE_RING_JUMBO, i,
7537 &frag_size) < 0) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00007538 netdev_warn(tp->dev,
7539 "Using a smaller RX jumbo ring. Only %d "
7540 "out of %d buffers were allocated "
7541 "successfully\n", i, tp->rx_jumbo_pending);
Matt Carlson0d86df82010-02-17 15:17:00 +00007542 if (i == 0)
7543 goto initfail;
7544 tp->rx_jumbo_pending = i;
7545 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007546 }
7547 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007548
7549done:
Michael Chan32d8c572006-07-25 16:38:29 -07007550 return 0;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007551
7552initfail:
Matt Carlson21f581a2009-08-28 14:00:25 +00007553 tg3_rx_prodring_free(tp, tpr);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007554 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007555}
7556
Matt Carlson21f581a2009-08-28 14:00:25 +00007557static void tg3_rx_prodring_fini(struct tg3 *tp,
7558 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007559{
Matt Carlson21f581a2009-08-28 14:00:25 +00007560 kfree(tpr->rx_std_buffers);
7561 tpr->rx_std_buffers = NULL;
7562 kfree(tpr->rx_jmb_buffers);
7563 tpr->rx_jmb_buffers = NULL;
7564 if (tpr->rx_std) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00007565 dma_free_coherent(&tp->pdev->dev, TG3_RX_STD_RING_BYTES(tp),
7566 tpr->rx_std, tpr->rx_std_mapping);
Matt Carlson21f581a2009-08-28 14:00:25 +00007567 tpr->rx_std = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007568 }
Matt Carlson21f581a2009-08-28 14:00:25 +00007569 if (tpr->rx_jmb) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00007570 dma_free_coherent(&tp->pdev->dev, TG3_RX_JMB_RING_BYTES(tp),
7571 tpr->rx_jmb, tpr->rx_jmb_mapping);
Matt Carlson21f581a2009-08-28 14:00:25 +00007572 tpr->rx_jmb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007573 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007574}
7575
Matt Carlson21f581a2009-08-28 14:00:25 +00007576static int tg3_rx_prodring_init(struct tg3 *tp,
7577 struct tg3_rx_prodring_set *tpr)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007578{
Matt Carlson2c49a442010-09-30 10:34:35 +00007579 tpr->rx_std_buffers = kzalloc(TG3_RX_STD_BUFF_RING_SIZE(tp),
7580 GFP_KERNEL);
Matt Carlson21f581a2009-08-28 14:00:25 +00007581 if (!tpr->rx_std_buffers)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007582 return -ENOMEM;
7583
Matt Carlson4bae65c2010-11-24 08:31:52 +00007584 tpr->rx_std = dma_alloc_coherent(&tp->pdev->dev,
7585 TG3_RX_STD_RING_BYTES(tp),
7586 &tpr->rx_std_mapping,
7587 GFP_KERNEL);
Matt Carlson21f581a2009-08-28 14:00:25 +00007588 if (!tpr->rx_std)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007589 goto err_out;
7590
Joe Perches63c3a662011-04-26 08:12:10 +00007591 if (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS)) {
Matt Carlson2c49a442010-09-30 10:34:35 +00007592 tpr->rx_jmb_buffers = kzalloc(TG3_RX_JMB_BUFF_RING_SIZE(tp),
Matt Carlson21f581a2009-08-28 14:00:25 +00007593 GFP_KERNEL);
7594 if (!tpr->rx_jmb_buffers)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007595 goto err_out;
7596
Matt Carlson4bae65c2010-11-24 08:31:52 +00007597 tpr->rx_jmb = dma_alloc_coherent(&tp->pdev->dev,
7598 TG3_RX_JMB_RING_BYTES(tp),
7599 &tpr->rx_jmb_mapping,
7600 GFP_KERNEL);
Matt Carlson21f581a2009-08-28 14:00:25 +00007601 if (!tpr->rx_jmb)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007602 goto err_out;
7603 }
7604
7605 return 0;
7606
7607err_out:
Matt Carlson21f581a2009-08-28 14:00:25 +00007608 tg3_rx_prodring_fini(tp, tpr);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007609 return -ENOMEM;
7610}
7611
7612/* Free up pending packets in all rx/tx rings.
7613 *
7614 * The chip has been shut down and the driver detached from
7615 * the networking, so no interrupts or new tx packets will
7616 * end up in the driver. tp->{tx,}lock is not held and we are not
7617 * in an interrupt context and thus may sleep.
7618 */
7619static void tg3_free_rings(struct tg3 *tp)
7620{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007621 int i, j;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007622
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007623 for (j = 0; j < tp->irq_cnt; j++) {
7624 struct tg3_napi *tnapi = &tp->napi[j];
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007625
Matt Carlson8fea32b2010-09-15 08:59:58 +00007626 tg3_rx_prodring_free(tp, &tnapi->prodring);
Matt Carlsonb28f6422010-06-05 17:24:32 +00007627
Matt Carlson0c1d0e22009-09-01 13:16:33 +00007628 if (!tnapi->tx_buffers)
7629 continue;
7630
Matt Carlson0d681b22011-07-27 14:20:49 +00007631 for (i = 0; i < TG3_TX_RING_SIZE; i++) {
7632 struct sk_buff *skb = tnapi->tx_buffers[i].skb;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007633
Matt Carlson0d681b22011-07-27 14:20:49 +00007634 if (!skb)
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007635 continue;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007636
Matt Carlsonba1142e2011-11-04 09:15:00 +00007637 tg3_tx_skb_unmap(tnapi, i,
7638 skb_shinfo(skb)->nr_frags - 1);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007639
7640 dev_kfree_skb_any(skb);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007641 }
Tom Herbert5cb917b2012-03-05 19:53:50 +00007642 netdev_tx_reset_queue(netdev_get_tx_queue(tp->dev, j));
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007643 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007644}
7645
7646/* Initialize tx/rx rings for packet processing.
7647 *
7648 * The chip has been shut down and the driver detached from
7649 * the networking, so no interrupts or new tx packets will
7650 * end up in the driver. tp->{tx,}lock are held and thus
7651 * we may not sleep.
7652 */
7653static int tg3_init_rings(struct tg3 *tp)
7654{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007655 int i;
Matt Carlson72334482009-08-28 14:03:01 +00007656
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007657 /* Free up all the SKBs. */
7658 tg3_free_rings(tp);
7659
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007660 for (i = 0; i < tp->irq_cnt; i++) {
7661 struct tg3_napi *tnapi = &tp->napi[i];
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007662
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007663 tnapi->last_tag = 0;
7664 tnapi->last_irq_tag = 0;
7665 tnapi->hw_status->status = 0;
7666 tnapi->hw_status->status_tag = 0;
7667 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
7668
7669 tnapi->tx_prod = 0;
7670 tnapi->tx_cons = 0;
Matt Carlson0c1d0e22009-09-01 13:16:33 +00007671 if (tnapi->tx_ring)
7672 memset(tnapi->tx_ring, 0, TG3_TX_RING_BYTES);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007673
7674 tnapi->rx_rcb_ptr = 0;
Matt Carlson0c1d0e22009-09-01 13:16:33 +00007675 if (tnapi->rx_rcb)
7676 memset(tnapi->rx_rcb, 0, TG3_RX_RCB_RING_BYTES(tp));
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007677
Matt Carlson8fea32b2010-09-15 08:59:58 +00007678 if (tg3_rx_prodring_alloc(tp, &tnapi->prodring)) {
Matt Carlsone4af1af2010-02-12 14:47:05 +00007679 tg3_free_rings(tp);
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007680 return -ENOMEM;
Matt Carlsone4af1af2010-02-12 14:47:05 +00007681 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007682 }
Matt Carlson72334482009-08-28 14:03:01 +00007683
Matt Carlson2b2cdb62009-11-13 13:03:48 +00007684 return 0;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007685}
7686
Michael Chan49a359e2012-09-28 07:12:37 +00007687static void tg3_mem_tx_release(struct tg3 *tp)
7688{
7689 int i;
7690
7691 for (i = 0; i < tp->irq_max; i++) {
7692 struct tg3_napi *tnapi = &tp->napi[i];
7693
7694 if (tnapi->tx_ring) {
7695 dma_free_coherent(&tp->pdev->dev, TG3_TX_RING_BYTES,
7696 tnapi->tx_ring, tnapi->tx_desc_mapping);
7697 tnapi->tx_ring = NULL;
7698 }
7699
7700 kfree(tnapi->tx_buffers);
7701 tnapi->tx_buffers = NULL;
7702 }
7703}
7704
7705static int tg3_mem_tx_acquire(struct tg3 *tp)
7706{
7707 int i;
7708 struct tg3_napi *tnapi = &tp->napi[0];
7709
7710 /* If multivector TSS is enabled, vector 0 does not handle
7711 * tx interrupts. Don't allocate any resources for it.
7712 */
7713 if (tg3_flag(tp, ENABLE_TSS))
7714 tnapi++;
7715
7716 for (i = 0; i < tp->txq_cnt; i++, tnapi++) {
7717 tnapi->tx_buffers = kzalloc(sizeof(struct tg3_tx_ring_info) *
7718 TG3_TX_RING_SIZE, GFP_KERNEL);
7719 if (!tnapi->tx_buffers)
7720 goto err_out;
7721
7722 tnapi->tx_ring = dma_alloc_coherent(&tp->pdev->dev,
7723 TG3_TX_RING_BYTES,
7724 &tnapi->tx_desc_mapping,
7725 GFP_KERNEL);
7726 if (!tnapi->tx_ring)
7727 goto err_out;
7728 }
7729
7730 return 0;
7731
7732err_out:
7733 tg3_mem_tx_release(tp);
7734 return -ENOMEM;
7735}
7736
7737static void tg3_mem_rx_release(struct tg3 *tp)
7738{
7739 int i;
7740
7741 for (i = 0; i < tp->irq_max; i++) {
7742 struct tg3_napi *tnapi = &tp->napi[i];
7743
7744 tg3_rx_prodring_fini(tp, &tnapi->prodring);
7745
7746 if (!tnapi->rx_rcb)
7747 continue;
7748
7749 dma_free_coherent(&tp->pdev->dev,
7750 TG3_RX_RCB_RING_BYTES(tp),
7751 tnapi->rx_rcb,
7752 tnapi->rx_rcb_mapping);
7753 tnapi->rx_rcb = NULL;
7754 }
7755}
7756
7757static int tg3_mem_rx_acquire(struct tg3 *tp)
7758{
7759 unsigned int i, limit;
7760
7761 limit = tp->rxq_cnt;
7762
7763 /* If RSS is enabled, we need a (dummy) producer ring
7764 * set on vector zero. This is the true hw prodring.
7765 */
7766 if (tg3_flag(tp, ENABLE_RSS))
7767 limit++;
7768
7769 for (i = 0; i < limit; i++) {
7770 struct tg3_napi *tnapi = &tp->napi[i];
7771
7772 if (tg3_rx_prodring_init(tp, &tnapi->prodring))
7773 goto err_out;
7774
7775 /* If multivector RSS is enabled, vector 0
7776 * does not handle rx or tx interrupts.
7777 * Don't allocate any resources for it.
7778 */
7779 if (!i && tg3_flag(tp, ENABLE_RSS))
7780 continue;
7781
7782 tnapi->rx_rcb = dma_alloc_coherent(&tp->pdev->dev,
7783 TG3_RX_RCB_RING_BYTES(tp),
7784 &tnapi->rx_rcb_mapping,
7785 GFP_KERNEL);
7786 if (!tnapi->rx_rcb)
7787 goto err_out;
7788
7789 memset(tnapi->rx_rcb, 0, TG3_RX_RCB_RING_BYTES(tp));
7790 }
7791
7792 return 0;
7793
7794err_out:
7795 tg3_mem_rx_release(tp);
7796 return -ENOMEM;
7797}
7798
Matt Carlsoncf7a7292009-08-28 13:59:57 +00007799/*
7800 * Must not be invoked with interrupt sources disabled and
7801 * the hardware shutdown down.
7802 */
7803static void tg3_free_consistent(struct tg3 *tp)
7804{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007805 int i;
Matt Carlson898a56f2009-08-28 14:02:40 +00007806
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007807 for (i = 0; i < tp->irq_cnt; i++) {
7808 struct tg3_napi *tnapi = &tp->napi[i];
7809
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007810 if (tnapi->hw_status) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00007811 dma_free_coherent(&tp->pdev->dev, TG3_HW_STATUS_SIZE,
7812 tnapi->hw_status,
7813 tnapi->status_mapping);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007814 tnapi->hw_status = NULL;
7815 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007816 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007817
Michael Chan49a359e2012-09-28 07:12:37 +00007818 tg3_mem_rx_release(tp);
7819 tg3_mem_tx_release(tp);
7820
Linus Torvalds1da177e2005-04-16 15:20:36 -07007821 if (tp->hw_stats) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00007822 dma_free_coherent(&tp->pdev->dev, sizeof(struct tg3_hw_stats),
7823 tp->hw_stats, tp->stats_mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007824 tp->hw_stats = NULL;
7825 }
7826}
7827
7828/*
7829 * Must not be invoked with interrupt sources disabled and
7830 * the hardware shutdown down. Can sleep.
7831 */
7832static int tg3_alloc_consistent(struct tg3 *tp)
7833{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007834 int i;
Matt Carlson898a56f2009-08-28 14:02:40 +00007835
Matt Carlson4bae65c2010-11-24 08:31:52 +00007836 tp->hw_stats = dma_alloc_coherent(&tp->pdev->dev,
7837 sizeof(struct tg3_hw_stats),
7838 &tp->stats_mapping,
7839 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007840 if (!tp->hw_stats)
7841 goto err_out;
7842
Linus Torvalds1da177e2005-04-16 15:20:36 -07007843 memset(tp->hw_stats, 0, sizeof(struct tg3_hw_stats));
7844
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007845 for (i = 0; i < tp->irq_cnt; i++) {
7846 struct tg3_napi *tnapi = &tp->napi[i];
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00007847 struct tg3_hw_status *sblk;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007848
Matt Carlson4bae65c2010-11-24 08:31:52 +00007849 tnapi->hw_status = dma_alloc_coherent(&tp->pdev->dev,
7850 TG3_HW_STATUS_SIZE,
7851 &tnapi->status_mapping,
7852 GFP_KERNEL);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007853 if (!tnapi->hw_status)
7854 goto err_out;
7855
7856 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00007857 sblk = tnapi->hw_status;
7858
Michael Chan49a359e2012-09-28 07:12:37 +00007859 if (tg3_flag(tp, ENABLE_RSS)) {
Michael Chan86449942012-10-02 20:31:14 -07007860 u16 *prodptr = NULL;
Matt Carlson8fea32b2010-09-15 08:59:58 +00007861
Michael Chan49a359e2012-09-28 07:12:37 +00007862 /*
7863 * When RSS is enabled, the status block format changes
7864 * slightly. The "rx_jumbo_consumer", "reserved",
7865 * and "rx_mini_consumer" members get mapped to the
7866 * other three rx return ring producer indexes.
7867 */
7868 switch (i) {
7869 case 1:
7870 prodptr = &sblk->idx[0].rx_producer;
7871 break;
7872 case 2:
7873 prodptr = &sblk->rx_jumbo_consumer;
7874 break;
7875 case 3:
7876 prodptr = &sblk->reserved;
7877 break;
7878 case 4:
7879 prodptr = &sblk->rx_mini_consumer;
Matt Carlsonf891ea12012-04-24 13:37:01 +00007880 break;
7881 }
Michael Chan49a359e2012-09-28 07:12:37 +00007882 tnapi->rx_rcb_prod_idx = prodptr;
7883 } else {
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00007884 tnapi->rx_rcb_prod_idx = &sblk->idx[0].rx_producer;
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00007885 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007886 }
7887
Michael Chan49a359e2012-09-28 07:12:37 +00007888 if (tg3_mem_tx_acquire(tp) || tg3_mem_rx_acquire(tp))
7889 goto err_out;
7890
Linus Torvalds1da177e2005-04-16 15:20:36 -07007891 return 0;
7892
7893err_out:
7894 tg3_free_consistent(tp);
7895 return -ENOMEM;
7896}
7897
7898#define MAX_WAIT_CNT 1000
7899
7900/* To stop a block, clear the enable bit and poll till it
7901 * clears. tp->lock is held.
7902 */
David S. Millerb3b7d6b2005-05-05 14:40:20 -07007903static int tg3_stop_block(struct tg3 *tp, unsigned long ofs, u32 enable_bit, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007904{
7905 unsigned int i;
7906 u32 val;
7907
Joe Perches63c3a662011-04-26 08:12:10 +00007908 if (tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007909 switch (ofs) {
7910 case RCVLSC_MODE:
7911 case DMAC_MODE:
7912 case MBFREE_MODE:
7913 case BUFMGR_MODE:
7914 case MEMARB_MODE:
7915 /* We can't enable/disable these bits of the
7916 * 5705/5750, just say success.
7917 */
7918 return 0;
7919
7920 default:
7921 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07007922 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007923 }
7924
7925 val = tr32(ofs);
7926 val &= ~enable_bit;
7927 tw32_f(ofs, val);
7928
7929 for (i = 0; i < MAX_WAIT_CNT; i++) {
7930 udelay(100);
7931 val = tr32(ofs);
7932 if ((val & enable_bit) == 0)
7933 break;
7934 }
7935
David S. Millerb3b7d6b2005-05-05 14:40:20 -07007936 if (i == MAX_WAIT_CNT && !silent) {
Matt Carlson2445e462010-04-05 10:19:21 +00007937 dev_err(&tp->pdev->dev,
7938 "tg3_stop_block timed out, ofs=%lx enable_bit=%x\n",
7939 ofs, enable_bit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007940 return -ENODEV;
7941 }
7942
7943 return 0;
7944}
7945
7946/* tp->lock is held. */
David S. Millerb3b7d6b2005-05-05 14:40:20 -07007947static int tg3_abort_hw(struct tg3 *tp, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007948{
7949 int i, err;
7950
7951 tg3_disable_ints(tp);
7952
7953 tp->rx_mode &= ~RX_MODE_ENABLE;
7954 tw32_f(MAC_RX_MODE, tp->rx_mode);
7955 udelay(10);
7956
David S. Millerb3b7d6b2005-05-05 14:40:20 -07007957 err = tg3_stop_block(tp, RCVBDI_MODE, RCVBDI_MODE_ENABLE, silent);
7958 err |= tg3_stop_block(tp, RCVLPC_MODE, RCVLPC_MODE_ENABLE, silent);
7959 err |= tg3_stop_block(tp, RCVLSC_MODE, RCVLSC_MODE_ENABLE, silent);
7960 err |= tg3_stop_block(tp, RCVDBDI_MODE, RCVDBDI_MODE_ENABLE, silent);
7961 err |= tg3_stop_block(tp, RCVDCC_MODE, RCVDCC_MODE_ENABLE, silent);
7962 err |= tg3_stop_block(tp, RCVCC_MODE, RCVCC_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007963
David S. Millerb3b7d6b2005-05-05 14:40:20 -07007964 err |= tg3_stop_block(tp, SNDBDS_MODE, SNDBDS_MODE_ENABLE, silent);
7965 err |= tg3_stop_block(tp, SNDBDI_MODE, SNDBDI_MODE_ENABLE, silent);
7966 err |= tg3_stop_block(tp, SNDDATAI_MODE, SNDDATAI_MODE_ENABLE, silent);
7967 err |= tg3_stop_block(tp, RDMAC_MODE, RDMAC_MODE_ENABLE, silent);
7968 err |= tg3_stop_block(tp, SNDDATAC_MODE, SNDDATAC_MODE_ENABLE, silent);
7969 err |= tg3_stop_block(tp, DMAC_MODE, DMAC_MODE_ENABLE, silent);
7970 err |= tg3_stop_block(tp, SNDBDC_MODE, SNDBDC_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007971
7972 tp->mac_mode &= ~MAC_MODE_TDE_ENABLE;
7973 tw32_f(MAC_MODE, tp->mac_mode);
7974 udelay(40);
7975
7976 tp->tx_mode &= ~TX_MODE_ENABLE;
7977 tw32_f(MAC_TX_MODE, tp->tx_mode);
7978
7979 for (i = 0; i < MAX_WAIT_CNT; i++) {
7980 udelay(100);
7981 if (!(tr32(MAC_TX_MODE) & TX_MODE_ENABLE))
7982 break;
7983 }
7984 if (i >= MAX_WAIT_CNT) {
Matt Carlsonab96b242010-04-05 10:19:22 +00007985 dev_err(&tp->pdev->dev,
7986 "%s timed out, TX_MODE_ENABLE will not clear "
7987 "MAC_TX_MODE=%08x\n", __func__, tr32(MAC_TX_MODE));
Michael Chane6de8ad2005-05-05 14:42:41 -07007988 err |= -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007989 }
7990
Michael Chane6de8ad2005-05-05 14:42:41 -07007991 err |= tg3_stop_block(tp, HOSTCC_MODE, HOSTCC_MODE_ENABLE, silent);
David S. Millerb3b7d6b2005-05-05 14:40:20 -07007992 err |= tg3_stop_block(tp, WDMAC_MODE, WDMAC_MODE_ENABLE, silent);
7993 err |= tg3_stop_block(tp, MBFREE_MODE, MBFREE_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007994
7995 tw32(FTQ_RESET, 0xffffffff);
7996 tw32(FTQ_RESET, 0x00000000);
7997
David S. Millerb3b7d6b2005-05-05 14:40:20 -07007998 err |= tg3_stop_block(tp, BUFMGR_MODE, BUFMGR_MODE_ENABLE, silent);
7999 err |= tg3_stop_block(tp, MEMARB_MODE, MEMARB_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008000
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008001 for (i = 0; i < tp->irq_cnt; i++) {
8002 struct tg3_napi *tnapi = &tp->napi[i];
8003 if (tnapi->hw_status)
8004 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
8005 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008006
Linus Torvalds1da177e2005-04-16 15:20:36 -07008007 return err;
8008}
8009
Michael Chanee6a99b2007-07-18 21:49:10 -07008010/* Save PCI command register before chip reset */
8011static void tg3_save_pci_state(struct tg3 *tp)
8012{
Matt Carlson8a6eac92007-10-21 16:17:55 -07008013 pci_read_config_word(tp->pdev, PCI_COMMAND, &tp->pci_cmd);
Michael Chanee6a99b2007-07-18 21:49:10 -07008014}
8015
8016/* Restore PCI state after chip reset */
8017static void tg3_restore_pci_state(struct tg3 *tp)
8018{
8019 u32 val;
8020
8021 /* Re-enable indirect register accesses. */
8022 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
8023 tp->misc_host_ctrl);
8024
8025 /* Set MAX PCI retry to zero. */
8026 val = (PCISTATE_ROM_ENABLE | PCISTATE_ROM_RETRY_ENABLE);
8027 if (tp->pci_chip_rev_id == CHIPREV_ID_5704_A0 &&
Joe Perches63c3a662011-04-26 08:12:10 +00008028 tg3_flag(tp, PCIX_MODE))
Michael Chanee6a99b2007-07-18 21:49:10 -07008029 val |= PCISTATE_RETRY_SAME_DMA;
Matt Carlson0d3031d2007-10-10 18:02:43 -07008030 /* Allow reads and writes to the APE register and memory space. */
Joe Perches63c3a662011-04-26 08:12:10 +00008031 if (tg3_flag(tp, ENABLE_APE))
Matt Carlson0d3031d2007-10-10 18:02:43 -07008032 val |= PCISTATE_ALLOW_APE_CTLSPC_WR |
Matt Carlsonf92d9dc12010-06-05 17:24:30 +00008033 PCISTATE_ALLOW_APE_SHMEM_WR |
8034 PCISTATE_ALLOW_APE_PSPACE_WR;
Michael Chanee6a99b2007-07-18 21:49:10 -07008035 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE, val);
8036
Matt Carlson8a6eac92007-10-21 16:17:55 -07008037 pci_write_config_word(tp->pdev, PCI_COMMAND, tp->pci_cmd);
Michael Chanee6a99b2007-07-18 21:49:10 -07008038
Matt Carlson2c55a3d2011-11-28 09:41:04 +00008039 if (!tg3_flag(tp, PCI_EXPRESS)) {
8040 pci_write_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE,
8041 tp->pci_cacheline_sz);
8042 pci_write_config_byte(tp->pdev, PCI_LATENCY_TIMER,
8043 tp->pci_lat_timer);
Michael Chan114342f2007-10-15 02:12:26 -07008044 }
Matt Carlson5f5c51e2007-11-12 21:19:37 -08008045
Michael Chanee6a99b2007-07-18 21:49:10 -07008046 /* Make sure PCI-X relaxed ordering bit is clear. */
Joe Perches63c3a662011-04-26 08:12:10 +00008047 if (tg3_flag(tp, PCIX_MODE)) {
Matt Carlson9974a352007-10-07 23:27:28 -07008048 u16 pcix_cmd;
8049
8050 pci_read_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
8051 &pcix_cmd);
8052 pcix_cmd &= ~PCI_X_CMD_ERO;
8053 pci_write_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
8054 pcix_cmd);
8055 }
Michael Chanee6a99b2007-07-18 21:49:10 -07008056
Joe Perches63c3a662011-04-26 08:12:10 +00008057 if (tg3_flag(tp, 5780_CLASS)) {
Michael Chanee6a99b2007-07-18 21:49:10 -07008058
8059 /* Chip reset on 5780 will reset MSI enable bit,
8060 * so need to restore it.
8061 */
Joe Perches63c3a662011-04-26 08:12:10 +00008062 if (tg3_flag(tp, USING_MSI)) {
Michael Chanee6a99b2007-07-18 21:49:10 -07008063 u16 ctrl;
8064
8065 pci_read_config_word(tp->pdev,
8066 tp->msi_cap + PCI_MSI_FLAGS,
8067 &ctrl);
8068 pci_write_config_word(tp->pdev,
8069 tp->msi_cap + PCI_MSI_FLAGS,
8070 ctrl | PCI_MSI_FLAGS_ENABLE);
8071 val = tr32(MSGINT_MODE);
8072 tw32(MSGINT_MODE, val | MSGINT_MODE_ENABLE);
8073 }
8074 }
8075}
8076
Linus Torvalds1da177e2005-04-16 15:20:36 -07008077/* tp->lock is held. */
8078static int tg3_chip_reset(struct tg3 *tp)
8079{
8080 u32 val;
Michael Chan1ee582d2005-08-09 20:16:46 -07008081 void (*write_op)(struct tg3 *, u32, u32);
Matt Carlson4f125f42009-09-01 12:55:02 +00008082 int i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008083
David S. Millerf49639e2006-06-09 11:58:36 -07008084 tg3_nvram_lock(tp);
8085
Matt Carlson77b483f2008-08-15 14:07:24 -07008086 tg3_ape_lock(tp, TG3_APE_LOCK_GRC);
8087
David S. Millerf49639e2006-06-09 11:58:36 -07008088 /* No matching tg3_nvram_unlock() after this because
8089 * chip reset below will undo the nvram lock.
8090 */
8091 tp->nvram_lock_cnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008092
Michael Chanee6a99b2007-07-18 21:49:10 -07008093 /* GRC_MISC_CFG core clock reset will clear the memory
8094 * enable bit in PCI register 4 and the MSI enable bit
8095 * on some chips, so we save relevant registers here.
8096 */
8097 tg3_save_pci_state(tp);
8098
Michael Chand9ab5ad2006-03-20 22:27:35 -08008099 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752 ||
Joe Perches63c3a662011-04-26 08:12:10 +00008100 tg3_flag(tp, 5755_PLUS))
Michael Chand9ab5ad2006-03-20 22:27:35 -08008101 tw32(GRC_FASTBOOT_PC, 0);
8102
Linus Torvalds1da177e2005-04-16 15:20:36 -07008103 /*
8104 * We must avoid the readl() that normally takes place.
8105 * It locks machines, causes machine checks, and other
8106 * fun things. So, temporarily disable the 5701
8107 * hardware workaround, while we do the reset.
8108 */
Michael Chan1ee582d2005-08-09 20:16:46 -07008109 write_op = tp->write32;
8110 if (write_op == tg3_write_flush_reg32)
8111 tp->write32 = tg3_write32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008112
Michael Chand18edcb2007-03-24 20:57:11 -07008113 /* Prevent the irq handler from reading or writing PCI registers
8114 * during chip reset when the memory enable bit in the PCI command
8115 * register may be cleared. The chip does not generate interrupt
8116 * at this time, but the irq handler may still be called due to irq
8117 * sharing or irqpoll.
8118 */
Joe Perches63c3a662011-04-26 08:12:10 +00008119 tg3_flag_set(tp, CHIP_RESETTING);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008120 for (i = 0; i < tp->irq_cnt; i++) {
8121 struct tg3_napi *tnapi = &tp->napi[i];
8122 if (tnapi->hw_status) {
8123 tnapi->hw_status->status = 0;
8124 tnapi->hw_status->status_tag = 0;
8125 }
8126 tnapi->last_tag = 0;
8127 tnapi->last_irq_tag = 0;
Michael Chanb8fa2f32007-04-06 17:35:37 -07008128 }
Michael Chand18edcb2007-03-24 20:57:11 -07008129 smp_mb();
Matt Carlson4f125f42009-09-01 12:55:02 +00008130
8131 for (i = 0; i < tp->irq_cnt; i++)
8132 synchronize_irq(tp->napi[i].irq_vec);
Michael Chand18edcb2007-03-24 20:57:11 -07008133
Matt Carlson255ca312009-08-25 10:07:27 +00008134 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780) {
8135 val = tr32(TG3_PCIE_LNKCTL) & ~TG3_PCIE_LNKCTL_L1_PLL_PD_EN;
8136 tw32(TG3_PCIE_LNKCTL, val | TG3_PCIE_LNKCTL_L1_PLL_PD_DIS);
8137 }
8138
Linus Torvalds1da177e2005-04-16 15:20:36 -07008139 /* do the reset */
8140 val = GRC_MISC_CFG_CORECLK_RESET;
8141
Joe Perches63c3a662011-04-26 08:12:10 +00008142 if (tg3_flag(tp, PCI_EXPRESS)) {
Matt Carlson88075d92010-08-02 11:25:58 +00008143 /* Force PCIe 1.0a mode */
8144 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
Joe Perches63c3a662011-04-26 08:12:10 +00008145 !tg3_flag(tp, 57765_PLUS) &&
Matt Carlson88075d92010-08-02 11:25:58 +00008146 tr32(TG3_PCIE_PHY_TSTCTL) ==
8147 (TG3_PCIE_PHY_TSTCTL_PCIE10 | TG3_PCIE_PHY_TSTCTL_PSCRAM))
8148 tw32(TG3_PCIE_PHY_TSTCTL, TG3_PCIE_PHY_TSTCTL_PSCRAM);
8149
Linus Torvalds1da177e2005-04-16 15:20:36 -07008150 if (tp->pci_chip_rev_id != CHIPREV_ID_5750_A0) {
8151 tw32(GRC_MISC_CFG, (1 << 29));
8152 val |= (1 << 29);
8153 }
8154 }
8155
Michael Chanb5d37722006-09-27 16:06:21 -07008156 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
8157 tw32(VCPU_STATUS, tr32(VCPU_STATUS) | VCPU_STATUS_DRV_RESET);
8158 tw32(GRC_VCPU_EXT_CTRL,
8159 tr32(GRC_VCPU_EXT_CTRL) & ~GRC_VCPU_EXT_CTRL_HALT_CPU);
8160 }
8161
Matt Carlsonf37500d2010-08-02 11:25:59 +00008162 /* Manage gphy power for all CPMU absent PCIe devices. */
Joe Perches63c3a662011-04-26 08:12:10 +00008163 if (tg3_flag(tp, 5705_PLUS) && !tg3_flag(tp, CPMU_PRESENT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008164 val |= GRC_MISC_CFG_KEEP_GPHY_POWER;
Matt Carlsonf37500d2010-08-02 11:25:59 +00008165
Linus Torvalds1da177e2005-04-16 15:20:36 -07008166 tw32(GRC_MISC_CFG, val);
8167
Michael Chan1ee582d2005-08-09 20:16:46 -07008168 /* restore 5701 hardware bug workaround write method */
8169 tp->write32 = write_op;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008170
8171 /* Unfortunately, we have to delay before the PCI read back.
8172 * Some 575X chips even will not respond to a PCI cfg access
8173 * when the reset command is given to the chip.
8174 *
8175 * How do these hardware designers expect things to work
8176 * properly if the PCI write is posted for a long period
8177 * of time? It is always necessary to have some method by
8178 * which a register read back can occur to push the write
8179 * out which does the reset.
8180 *
8181 * For most tg3 variants the trick below was working.
8182 * Ho hum...
8183 */
8184 udelay(120);
8185
8186 /* Flush PCI posted writes. The normal MMIO registers
8187 * are inaccessible at this time so this is the only
8188 * way to make this reliably (actually, this is no longer
8189 * the case, see above). I tried to use indirect
8190 * register read/write but this upset some 5701 variants.
8191 */
8192 pci_read_config_dword(tp->pdev, PCI_COMMAND, &val);
8193
8194 udelay(120);
8195
Jiang Liu0f49bfb2012-08-20 13:28:20 -06008196 if (tg3_flag(tp, PCI_EXPRESS) && pci_is_pcie(tp->pdev)) {
Matt Carlsone7126992009-08-25 10:08:16 +00008197 u16 val16;
8198
Linus Torvalds1da177e2005-04-16 15:20:36 -07008199 if (tp->pci_chip_rev_id == CHIPREV_ID_5750_A0) {
Michael Chan86449942012-10-02 20:31:14 -07008200 int j;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008201 u32 cfg_val;
8202
8203 /* Wait for link training to complete. */
Michael Chan86449942012-10-02 20:31:14 -07008204 for (j = 0; j < 5000; j++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008205 udelay(100);
8206
8207 pci_read_config_dword(tp->pdev, 0xc4, &cfg_val);
8208 pci_write_config_dword(tp->pdev, 0xc4,
8209 cfg_val | (1 << 15));
8210 }
Matt Carlson5e7dfd02008-11-21 17:18:16 -08008211
Matt Carlsone7126992009-08-25 10:08:16 +00008212 /* Clear the "no snoop" and "relaxed ordering" bits. */
Jiang Liu0f49bfb2012-08-20 13:28:20 -06008213 val16 = PCI_EXP_DEVCTL_RELAX_EN | PCI_EXP_DEVCTL_NOSNOOP_EN;
Matt Carlsone7126992009-08-25 10:08:16 +00008214 /*
8215 * Older PCIe devices only support the 128 byte
8216 * MPS setting. Enforce the restriction.
Matt Carlson5e7dfd02008-11-21 17:18:16 -08008217 */
Joe Perches63c3a662011-04-26 08:12:10 +00008218 if (!tg3_flag(tp, CPMU_PRESENT))
Jiang Liu0f49bfb2012-08-20 13:28:20 -06008219 val16 |= PCI_EXP_DEVCTL_PAYLOAD;
8220 pcie_capability_clear_word(tp->pdev, PCI_EXP_DEVCTL, val16);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08008221
Matt Carlson5e7dfd02008-11-21 17:18:16 -08008222 /* Clear error status */
Jiang Liu0f49bfb2012-08-20 13:28:20 -06008223 pcie_capability_write_word(tp->pdev, PCI_EXP_DEVSTA,
Matt Carlson5e7dfd02008-11-21 17:18:16 -08008224 PCI_EXP_DEVSTA_CED |
8225 PCI_EXP_DEVSTA_NFED |
8226 PCI_EXP_DEVSTA_FED |
8227 PCI_EXP_DEVSTA_URD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008228 }
8229
Michael Chanee6a99b2007-07-18 21:49:10 -07008230 tg3_restore_pci_state(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008231
Joe Perches63c3a662011-04-26 08:12:10 +00008232 tg3_flag_clear(tp, CHIP_RESETTING);
8233 tg3_flag_clear(tp, ERROR_PROCESSED);
Michael Chand18edcb2007-03-24 20:57:11 -07008234
Michael Chanee6a99b2007-07-18 21:49:10 -07008235 val = 0;
Joe Perches63c3a662011-04-26 08:12:10 +00008236 if (tg3_flag(tp, 5780_CLASS))
Michael Chan4cf78e42005-07-25 12:29:19 -07008237 val = tr32(MEMARB_MODE);
Michael Chanee6a99b2007-07-18 21:49:10 -07008238 tw32(MEMARB_MODE, val | MEMARB_MODE_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008239
8240 if (tp->pci_chip_rev_id == CHIPREV_ID_5750_A3) {
8241 tg3_stop_fw(tp);
8242 tw32(0x5000, 0x400);
8243 }
8244
8245 tw32(GRC_MODE, tp->grc_mode);
8246
8247 if (tp->pci_chip_rev_id == CHIPREV_ID_5705_A0) {
Andy Gospodarekab0049b2007-09-06 20:42:14 +01008248 val = tr32(0xc4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008249
8250 tw32(0xc4, val | (1 << 15));
8251 }
8252
8253 if ((tp->nic_sram_data_cfg & NIC_SRAM_DATA_CFG_MINI_PCI) != 0 &&
8254 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
8255 tp->pci_clock_ctrl |= CLOCK_CTRL_CLKRUN_OENABLE;
8256 if (tp->pci_chip_rev_id == CHIPREV_ID_5705_A0)
8257 tp->pci_clock_ctrl |= CLOCK_CTRL_FORCE_CLKRUN;
8258 tw32(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl);
8259 }
8260
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008261 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Matt Carlson9e975cc2011-07-20 10:20:50 +00008262 tp->mac_mode = MAC_MODE_PORT_MODE_TBI;
Matt Carlsond2394e6b2010-11-24 08:31:47 +00008263 val = tp->mac_mode;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008264 } else if (tp->phy_flags & TG3_PHYFLG_MII_SERDES) {
Matt Carlson9e975cc2011-07-20 10:20:50 +00008265 tp->mac_mode = MAC_MODE_PORT_MODE_GMII;
Matt Carlsond2394e6b2010-11-24 08:31:47 +00008266 val = tp->mac_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008267 } else
Matt Carlsond2394e6b2010-11-24 08:31:47 +00008268 val = 0;
8269
8270 tw32_f(MAC_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008271 udelay(40);
8272
Matt Carlson77b483f2008-08-15 14:07:24 -07008273 tg3_ape_unlock(tp, TG3_APE_LOCK_GRC);
8274
Michael Chan7a6f4362006-09-27 16:03:31 -07008275 err = tg3_poll_fw(tp);
8276 if (err)
8277 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008278
Matt Carlson0a9140c2009-08-28 12:27:50 +00008279 tg3_mdio_start(tp);
8280
Joe Perches63c3a662011-04-26 08:12:10 +00008281 if (tg3_flag(tp, PCI_EXPRESS) &&
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00008282 tp->pci_chip_rev_id != CHIPREV_ID_5750_A0 &&
8283 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
Joe Perches63c3a662011-04-26 08:12:10 +00008284 !tg3_flag(tp, 57765_PLUS)) {
Andy Gospodarekab0049b2007-09-06 20:42:14 +01008285 val = tr32(0x7c00);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008286
8287 tw32(0x7c00, val | (1 << 25));
8288 }
8289
Matt Carlsond78b59f2011-04-05 14:22:46 +00008290 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
8291 val = tr32(TG3_CPMU_CLCK_ORIDE);
8292 tw32(TG3_CPMU_CLCK_ORIDE, val & ~CPMU_CLCK_ORIDE_MAC_ORIDE_EN);
8293 }
8294
Linus Torvalds1da177e2005-04-16 15:20:36 -07008295 /* Reprobe ASF enable state. */
Joe Perches63c3a662011-04-26 08:12:10 +00008296 tg3_flag_clear(tp, ENABLE_ASF);
8297 tg3_flag_clear(tp, ASF_NEW_HANDSHAKE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008298 tg3_read_mem(tp, NIC_SRAM_DATA_SIG, &val);
8299 if (val == NIC_SRAM_DATA_SIG_MAGIC) {
8300 u32 nic_cfg;
8301
8302 tg3_read_mem(tp, NIC_SRAM_DATA_CFG, &nic_cfg);
8303 if (nic_cfg & NIC_SRAM_DATA_CFG_ASF_ENABLE) {
Joe Perches63c3a662011-04-26 08:12:10 +00008304 tg3_flag_set(tp, ENABLE_ASF);
Matt Carlson4ba526c2008-08-15 14:10:04 -07008305 tp->last_event_jiffies = jiffies;
Joe Perches63c3a662011-04-26 08:12:10 +00008306 if (tg3_flag(tp, 5750_PLUS))
8307 tg3_flag_set(tp, ASF_NEW_HANDSHAKE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008308 }
8309 }
8310
8311 return 0;
8312}
8313
Matt Carlson65ec6982012-02-28 23:33:37 +00008314static void tg3_get_nstats(struct tg3 *, struct rtnl_link_stats64 *);
8315static void tg3_get_estats(struct tg3 *, struct tg3_ethtool_stats *);
Matt Carlson92feeab2011-12-08 14:40:14 +00008316
Linus Torvalds1da177e2005-04-16 15:20:36 -07008317/* tp->lock is held. */
Michael Chan944d9802005-05-29 14:57:48 -07008318static int tg3_halt(struct tg3 *tp, int kind, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008319{
8320 int err;
8321
8322 tg3_stop_fw(tp);
8323
Michael Chan944d9802005-05-29 14:57:48 -07008324 tg3_write_sig_pre_reset(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008325
David S. Millerb3b7d6b2005-05-05 14:40:20 -07008326 tg3_abort_hw(tp, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008327 err = tg3_chip_reset(tp);
8328
Matt Carlsondaba2a62009-04-20 06:58:52 +00008329 __tg3_set_mac_addr(tp, 0);
8330
Michael Chan944d9802005-05-29 14:57:48 -07008331 tg3_write_sig_legacy(tp, kind);
8332 tg3_write_sig_post_reset(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008333
Matt Carlson92feeab2011-12-08 14:40:14 +00008334 if (tp->hw_stats) {
8335 /* Save the stats across chip resets... */
David S. Millerb4017c52012-03-01 17:57:40 -05008336 tg3_get_nstats(tp, &tp->net_stats_prev);
Matt Carlson92feeab2011-12-08 14:40:14 +00008337 tg3_get_estats(tp, &tp->estats_prev);
8338
8339 /* And make sure the next sample is new data */
8340 memset(tp->hw_stats, 0, sizeof(struct tg3_hw_stats));
8341 }
8342
Linus Torvalds1da177e2005-04-16 15:20:36 -07008343 if (err)
8344 return err;
8345
8346 return 0;
8347}
8348
Linus Torvalds1da177e2005-04-16 15:20:36 -07008349static int tg3_set_mac_addr(struct net_device *dev, void *p)
8350{
8351 struct tg3 *tp = netdev_priv(dev);
8352 struct sockaddr *addr = p;
Michael Chan986e0ae2007-05-05 12:10:20 -07008353 int err = 0, skip_mac_1 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008354
Michael Chanf9804dd2005-09-27 12:13:10 -07008355 if (!is_valid_ether_addr(addr->sa_data))
Danny Kukawka504f9b52012-02-21 02:07:49 +00008356 return -EADDRNOTAVAIL;
Michael Chanf9804dd2005-09-27 12:13:10 -07008357
Linus Torvalds1da177e2005-04-16 15:20:36 -07008358 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
8359
Michael Chane75f7c92006-03-20 21:33:26 -08008360 if (!netif_running(dev))
8361 return 0;
8362
Joe Perches63c3a662011-04-26 08:12:10 +00008363 if (tg3_flag(tp, ENABLE_ASF)) {
Michael Chan986e0ae2007-05-05 12:10:20 -07008364 u32 addr0_high, addr0_low, addr1_high, addr1_low;
Michael Chan58712ef2006-04-29 18:58:01 -07008365
Michael Chan986e0ae2007-05-05 12:10:20 -07008366 addr0_high = tr32(MAC_ADDR_0_HIGH);
8367 addr0_low = tr32(MAC_ADDR_0_LOW);
8368 addr1_high = tr32(MAC_ADDR_1_HIGH);
8369 addr1_low = tr32(MAC_ADDR_1_LOW);
8370
8371 /* Skip MAC addr 1 if ASF is using it. */
8372 if ((addr0_high != addr1_high || addr0_low != addr1_low) &&
8373 !(addr1_high == 0 && addr1_low == 0))
8374 skip_mac_1 = 1;
Michael Chan58712ef2006-04-29 18:58:01 -07008375 }
Michael Chan986e0ae2007-05-05 12:10:20 -07008376 spin_lock_bh(&tp->lock);
8377 __tg3_set_mac_addr(tp, skip_mac_1);
8378 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008379
Michael Chanb9ec6c12006-07-25 16:37:27 -07008380 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008381}
8382
8383/* tp->lock is held. */
8384static void tg3_set_bdinfo(struct tg3 *tp, u32 bdinfo_addr,
8385 dma_addr_t mapping, u32 maxlen_flags,
8386 u32 nic_addr)
8387{
8388 tg3_write_mem(tp,
8389 (bdinfo_addr + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH),
8390 ((u64) mapping >> 32));
8391 tg3_write_mem(tp,
8392 (bdinfo_addr + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW),
8393 ((u64) mapping & 0xffffffff));
8394 tg3_write_mem(tp,
8395 (bdinfo_addr + TG3_BDINFO_MAXLEN_FLAGS),
8396 maxlen_flags);
8397
Joe Perches63c3a662011-04-26 08:12:10 +00008398 if (!tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008399 tg3_write_mem(tp,
8400 (bdinfo_addr + TG3_BDINFO_NIC_ADDR),
8401 nic_addr);
8402}
8403
Michael Chana489b6d2012-09-28 07:12:39 +00008404
8405static void tg3_coal_tx_init(struct tg3 *tp, struct ethtool_coalesce *ec)
David S. Miller15f98502005-05-18 22:49:26 -07008406{
Michael Chana489b6d2012-09-28 07:12:39 +00008407 int i = 0;
Matt Carlsonb6080e12009-09-01 13:12:00 +00008408
Joe Perches63c3a662011-04-26 08:12:10 +00008409 if (!tg3_flag(tp, ENABLE_TSS)) {
Matt Carlsonb6080e12009-09-01 13:12:00 +00008410 tw32(HOSTCC_TXCOL_TICKS, ec->tx_coalesce_usecs);
8411 tw32(HOSTCC_TXMAX_FRAMES, ec->tx_max_coalesced_frames);
8412 tw32(HOSTCC_TXCOAL_MAXF_INT, ec->tx_max_coalesced_frames_irq);
Matt Carlsonb6080e12009-09-01 13:12:00 +00008413 } else {
8414 tw32(HOSTCC_TXCOL_TICKS, 0);
8415 tw32(HOSTCC_TXMAX_FRAMES, 0);
8416 tw32(HOSTCC_TXCOAL_MAXF_INT, 0);
Michael Chana489b6d2012-09-28 07:12:39 +00008417
8418 for (; i < tp->txq_cnt; i++) {
8419 u32 reg;
8420
8421 reg = HOSTCC_TXCOL_TICKS_VEC1 + i * 0x18;
8422 tw32(reg, ec->tx_coalesce_usecs);
8423 reg = HOSTCC_TXMAX_FRAMES_VEC1 + i * 0x18;
8424 tw32(reg, ec->tx_max_coalesced_frames);
8425 reg = HOSTCC_TXCOAL_MAXF_INT_VEC1 + i * 0x18;
8426 tw32(reg, ec->tx_max_coalesced_frames_irq);
8427 }
Matt Carlson19cfaec2009-12-03 08:36:20 +00008428 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00008429
Michael Chana489b6d2012-09-28 07:12:39 +00008430 for (; i < tp->irq_max - 1; i++) {
8431 tw32(HOSTCC_TXCOL_TICKS_VEC1 + i * 0x18, 0);
8432 tw32(HOSTCC_TXMAX_FRAMES_VEC1 + i * 0x18, 0);
8433 tw32(HOSTCC_TXCOAL_MAXF_INT_VEC1 + i * 0x18, 0);
8434 }
8435}
8436
8437static void tg3_coal_rx_init(struct tg3 *tp, struct ethtool_coalesce *ec)
8438{
8439 int i = 0;
8440 u32 limit = tp->rxq_cnt;
8441
Joe Perches63c3a662011-04-26 08:12:10 +00008442 if (!tg3_flag(tp, ENABLE_RSS)) {
Matt Carlson19cfaec2009-12-03 08:36:20 +00008443 tw32(HOSTCC_RXCOL_TICKS, ec->rx_coalesce_usecs);
8444 tw32(HOSTCC_RXMAX_FRAMES, ec->rx_max_coalesced_frames);
8445 tw32(HOSTCC_RXCOAL_MAXF_INT, ec->rx_max_coalesced_frames_irq);
Michael Chana489b6d2012-09-28 07:12:39 +00008446 limit--;
Matt Carlson19cfaec2009-12-03 08:36:20 +00008447 } else {
Matt Carlsonb6080e12009-09-01 13:12:00 +00008448 tw32(HOSTCC_RXCOL_TICKS, 0);
8449 tw32(HOSTCC_RXMAX_FRAMES, 0);
8450 tw32(HOSTCC_RXCOAL_MAXF_INT, 0);
David S. Miller15f98502005-05-18 22:49:26 -07008451 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00008452
Michael Chana489b6d2012-09-28 07:12:39 +00008453 for (; i < limit; i++) {
8454 u32 reg;
8455
8456 reg = HOSTCC_RXCOL_TICKS_VEC1 + i * 0x18;
8457 tw32(reg, ec->rx_coalesce_usecs);
8458 reg = HOSTCC_RXMAX_FRAMES_VEC1 + i * 0x18;
8459 tw32(reg, ec->rx_max_coalesced_frames);
8460 reg = HOSTCC_RXCOAL_MAXF_INT_VEC1 + i * 0x18;
8461 tw32(reg, ec->rx_max_coalesced_frames_irq);
8462 }
8463
8464 for (; i < tp->irq_max - 1; i++) {
8465 tw32(HOSTCC_RXCOL_TICKS_VEC1 + i * 0x18, 0);
8466 tw32(HOSTCC_RXMAX_FRAMES_VEC1 + i * 0x18, 0);
8467 tw32(HOSTCC_RXCOAL_MAXF_INT_VEC1 + i * 0x18, 0);
8468 }
8469}
8470
8471static void __tg3_set_coalesce(struct tg3 *tp, struct ethtool_coalesce *ec)
8472{
8473 tg3_coal_tx_init(tp, ec);
8474 tg3_coal_rx_init(tp, ec);
8475
Joe Perches63c3a662011-04-26 08:12:10 +00008476 if (!tg3_flag(tp, 5705_PLUS)) {
David S. Miller15f98502005-05-18 22:49:26 -07008477 u32 val = ec->stats_block_coalesce_usecs;
8478
Matt Carlsonb6080e12009-09-01 13:12:00 +00008479 tw32(HOSTCC_RXCOAL_TICK_INT, ec->rx_coalesce_usecs_irq);
8480 tw32(HOSTCC_TXCOAL_TICK_INT, ec->tx_coalesce_usecs_irq);
8481
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00008482 if (!tp->link_up)
David S. Miller15f98502005-05-18 22:49:26 -07008483 val = 0;
8484
8485 tw32(HOSTCC_STAT_COAL_TICKS, val);
8486 }
8487}
Linus Torvalds1da177e2005-04-16 15:20:36 -07008488
8489/* tp->lock is held. */
Matt Carlson2d31eca2009-09-01 12:53:31 +00008490static void tg3_rings_reset(struct tg3 *tp)
8491{
8492 int i;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008493 u32 stblk, txrcb, rxrcb, limit;
Matt Carlson2d31eca2009-09-01 12:53:31 +00008494 struct tg3_napi *tnapi = &tp->napi[0];
8495
8496 /* Disable all transmit rings but the first. */
Joe Perches63c3a662011-04-26 08:12:10 +00008497 if (!tg3_flag(tp, 5705_PLUS))
Matt Carlson2d31eca2009-09-01 12:53:31 +00008498 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 16;
Joe Perches63c3a662011-04-26 08:12:10 +00008499 else if (tg3_flag(tp, 5717_PLUS))
Matt Carlson3d377282010-10-14 10:37:39 +00008500 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 4;
Matt Carlson55086ad2011-12-14 11:09:59 +00008501 else if (tg3_flag(tp, 57765_CLASS))
Matt Carlsonb703df62009-12-03 08:36:21 +00008502 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 2;
Matt Carlson2d31eca2009-09-01 12:53:31 +00008503 else
8504 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE;
8505
8506 for (txrcb = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE;
8507 txrcb < limit; txrcb += TG3_BDINFO_SIZE)
8508 tg3_write_mem(tp, txrcb + TG3_BDINFO_MAXLEN_FLAGS,
8509 BDINFO_FLAGS_DISABLED);
8510
8511
8512 /* Disable all receive return rings but the first. */
Joe Perches63c3a662011-04-26 08:12:10 +00008513 if (tg3_flag(tp, 5717_PLUS))
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00008514 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 17;
Joe Perches63c3a662011-04-26 08:12:10 +00008515 else if (!tg3_flag(tp, 5705_PLUS))
Matt Carlson2d31eca2009-09-01 12:53:31 +00008516 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 16;
Matt Carlsonb703df62009-12-03 08:36:21 +00008517 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
Matt Carlson55086ad2011-12-14 11:09:59 +00008518 tg3_flag(tp, 57765_CLASS))
Matt Carlson2d31eca2009-09-01 12:53:31 +00008519 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 4;
8520 else
8521 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE;
8522
8523 for (rxrcb = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE;
8524 rxrcb < limit; rxrcb += TG3_BDINFO_SIZE)
8525 tg3_write_mem(tp, rxrcb + TG3_BDINFO_MAXLEN_FLAGS,
8526 BDINFO_FLAGS_DISABLED);
8527
8528 /* Disable interrupts */
8529 tw32_mailbox_f(tp->napi[0].int_mbox, 1);
Matt Carlson0e6cf6a2011-06-13 13:38:55 +00008530 tp->napi[0].chk_msi_cnt = 0;
8531 tp->napi[0].last_rx_cons = 0;
8532 tp->napi[0].last_tx_cons = 0;
Matt Carlson2d31eca2009-09-01 12:53:31 +00008533
8534 /* Zero mailbox registers. */
Joe Perches63c3a662011-04-26 08:12:10 +00008535 if (tg3_flag(tp, SUPPORT_MSIX)) {
Matt Carlson6fd45cb2010-09-15 08:59:57 +00008536 for (i = 1; i < tp->irq_max; i++) {
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008537 tp->napi[i].tx_prod = 0;
8538 tp->napi[i].tx_cons = 0;
Joe Perches63c3a662011-04-26 08:12:10 +00008539 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlsonc2353a32010-01-20 16:58:08 +00008540 tw32_mailbox(tp->napi[i].prodmbox, 0);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008541 tw32_rx_mbox(tp->napi[i].consmbox, 0);
8542 tw32_mailbox_f(tp->napi[i].int_mbox, 1);
Matt Carlson7f230732011-08-31 11:44:48 +00008543 tp->napi[i].chk_msi_cnt = 0;
Matt Carlson0e6cf6a2011-06-13 13:38:55 +00008544 tp->napi[i].last_rx_cons = 0;
8545 tp->napi[i].last_tx_cons = 0;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008546 }
Joe Perches63c3a662011-04-26 08:12:10 +00008547 if (!tg3_flag(tp, ENABLE_TSS))
Matt Carlsonc2353a32010-01-20 16:58:08 +00008548 tw32_mailbox(tp->napi[0].prodmbox, 0);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008549 } else {
8550 tp->napi[0].tx_prod = 0;
8551 tp->napi[0].tx_cons = 0;
8552 tw32_mailbox(tp->napi[0].prodmbox, 0);
8553 tw32_rx_mbox(tp->napi[0].consmbox, 0);
8554 }
Matt Carlson2d31eca2009-09-01 12:53:31 +00008555
8556 /* Make sure the NIC-based send BD rings are disabled. */
Joe Perches63c3a662011-04-26 08:12:10 +00008557 if (!tg3_flag(tp, 5705_PLUS)) {
Matt Carlson2d31eca2009-09-01 12:53:31 +00008558 u32 mbox = MAILBOX_SNDNIC_PROD_IDX_0 + TG3_64BIT_REG_LOW;
8559 for (i = 0; i < 16; i++)
8560 tw32_tx_mbox(mbox + i * 8, 0);
8561 }
8562
8563 txrcb = NIC_SRAM_SEND_RCB;
8564 rxrcb = NIC_SRAM_RCV_RET_RCB;
8565
8566 /* Clear status block in ram. */
8567 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
8568
8569 /* Set status block DMA address */
8570 tw32(HOSTCC_STATUS_BLK_HOST_ADDR + TG3_64BIT_REG_HIGH,
8571 ((u64) tnapi->status_mapping >> 32));
8572 tw32(HOSTCC_STATUS_BLK_HOST_ADDR + TG3_64BIT_REG_LOW,
8573 ((u64) tnapi->status_mapping & 0xffffffff));
8574
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008575 if (tnapi->tx_ring) {
8576 tg3_set_bdinfo(tp, txrcb, tnapi->tx_desc_mapping,
8577 (TG3_TX_RING_SIZE <<
8578 BDINFO_FLAGS_MAXLEN_SHIFT),
8579 NIC_SRAM_TX_BUFFER_DESC);
8580 txrcb += TG3_BDINFO_SIZE;
8581 }
Matt Carlson2d31eca2009-09-01 12:53:31 +00008582
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008583 if (tnapi->rx_rcb) {
8584 tg3_set_bdinfo(tp, rxrcb, tnapi->rx_rcb_mapping,
Matt Carlson7cb32cf2010-09-30 10:34:36 +00008585 (tp->rx_ret_ring_mask + 1) <<
8586 BDINFO_FLAGS_MAXLEN_SHIFT, 0);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008587 rxrcb += TG3_BDINFO_SIZE;
8588 }
8589
8590 stblk = HOSTCC_STATBLCK_RING1;
8591
8592 for (i = 1, tnapi++; i < tp->irq_cnt; i++, tnapi++) {
8593 u64 mapping = (u64)tnapi->status_mapping;
8594 tw32(stblk + TG3_64BIT_REG_HIGH, mapping >> 32);
8595 tw32(stblk + TG3_64BIT_REG_LOW, mapping & 0xffffffff);
8596
8597 /* Clear status block in ram. */
8598 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
8599
Matt Carlson19cfaec2009-12-03 08:36:20 +00008600 if (tnapi->tx_ring) {
8601 tg3_set_bdinfo(tp, txrcb, tnapi->tx_desc_mapping,
8602 (TG3_TX_RING_SIZE <<
8603 BDINFO_FLAGS_MAXLEN_SHIFT),
8604 NIC_SRAM_TX_BUFFER_DESC);
8605 txrcb += TG3_BDINFO_SIZE;
8606 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008607
8608 tg3_set_bdinfo(tp, rxrcb, tnapi->rx_rcb_mapping,
Matt Carlson7cb32cf2010-09-30 10:34:36 +00008609 ((tp->rx_ret_ring_mask + 1) <<
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008610 BDINFO_FLAGS_MAXLEN_SHIFT), 0);
8611
8612 stblk += 8;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008613 rxrcb += TG3_BDINFO_SIZE;
8614 }
Matt Carlson2d31eca2009-09-01 12:53:31 +00008615}
8616
Matt Carlsoneb07a942011-04-20 07:57:36 +00008617static void tg3_setup_rxbd_thresholds(struct tg3 *tp)
8618{
8619 u32 val, bdcache_maxcnt, host_rep_thresh, nic_rep_thresh;
8620
Joe Perches63c3a662011-04-26 08:12:10 +00008621 if (!tg3_flag(tp, 5750_PLUS) ||
8622 tg3_flag(tp, 5780_CLASS) ||
Matt Carlsoneb07a942011-04-20 07:57:36 +00008623 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 ||
Matt Carlson513aa6e2011-11-21 15:01:18 +00008624 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752 ||
8625 tg3_flag(tp, 57765_PLUS))
Matt Carlsoneb07a942011-04-20 07:57:36 +00008626 bdcache_maxcnt = TG3_SRAM_RX_STD_BDCACHE_SIZE_5700;
8627 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
8628 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787)
8629 bdcache_maxcnt = TG3_SRAM_RX_STD_BDCACHE_SIZE_5755;
8630 else
8631 bdcache_maxcnt = TG3_SRAM_RX_STD_BDCACHE_SIZE_5906;
8632
8633 nic_rep_thresh = min(bdcache_maxcnt / 2, tp->rx_std_max_post);
8634 host_rep_thresh = max_t(u32, tp->rx_pending / 8, 1);
8635
8636 val = min(nic_rep_thresh, host_rep_thresh);
8637 tw32(RCVBDI_STD_THRESH, val);
8638
Joe Perches63c3a662011-04-26 08:12:10 +00008639 if (tg3_flag(tp, 57765_PLUS))
Matt Carlsoneb07a942011-04-20 07:57:36 +00008640 tw32(STD_REPLENISH_LWM, bdcache_maxcnt);
8641
Joe Perches63c3a662011-04-26 08:12:10 +00008642 if (!tg3_flag(tp, JUMBO_CAPABLE) || tg3_flag(tp, 5780_CLASS))
Matt Carlsoneb07a942011-04-20 07:57:36 +00008643 return;
8644
Matt Carlson513aa6e2011-11-21 15:01:18 +00008645 bdcache_maxcnt = TG3_SRAM_RX_JMB_BDCACHE_SIZE_5700;
Matt Carlsoneb07a942011-04-20 07:57:36 +00008646
8647 host_rep_thresh = max_t(u32, tp->rx_jumbo_pending / 8, 1);
8648
8649 val = min(bdcache_maxcnt / 2, host_rep_thresh);
8650 tw32(RCVBDI_JUMBO_THRESH, val);
8651
Joe Perches63c3a662011-04-26 08:12:10 +00008652 if (tg3_flag(tp, 57765_PLUS))
Matt Carlsoneb07a942011-04-20 07:57:36 +00008653 tw32(JMB_REPLENISH_LWM, bdcache_maxcnt);
8654}
8655
Matt Carlsonccd5ba92012-02-13 10:20:08 +00008656static inline u32 calc_crc(unsigned char *buf, int len)
8657{
8658 u32 reg;
8659 u32 tmp;
8660 int j, k;
8661
8662 reg = 0xffffffff;
8663
8664 for (j = 0; j < len; j++) {
8665 reg ^= buf[j];
8666
8667 for (k = 0; k < 8; k++) {
8668 tmp = reg & 0x01;
8669
8670 reg >>= 1;
8671
8672 if (tmp)
8673 reg ^= 0xedb88320;
8674 }
8675 }
8676
8677 return ~reg;
8678}
8679
8680static void tg3_set_multi(struct tg3 *tp, unsigned int accept_all)
8681{
8682 /* accept or reject all multicast frames */
8683 tw32(MAC_HASH_REG_0, accept_all ? 0xffffffff : 0);
8684 tw32(MAC_HASH_REG_1, accept_all ? 0xffffffff : 0);
8685 tw32(MAC_HASH_REG_2, accept_all ? 0xffffffff : 0);
8686 tw32(MAC_HASH_REG_3, accept_all ? 0xffffffff : 0);
8687}
8688
8689static void __tg3_set_rx_mode(struct net_device *dev)
8690{
8691 struct tg3 *tp = netdev_priv(dev);
8692 u32 rx_mode;
8693
8694 rx_mode = tp->rx_mode & ~(RX_MODE_PROMISC |
8695 RX_MODE_KEEP_VLAN_TAG);
8696
8697#if !defined(CONFIG_VLAN_8021Q) && !defined(CONFIG_VLAN_8021Q_MODULE)
8698 /* When ASF is in use, we always keep the RX_MODE_KEEP_VLAN_TAG
8699 * flag clear.
8700 */
8701 if (!tg3_flag(tp, ENABLE_ASF))
8702 rx_mode |= RX_MODE_KEEP_VLAN_TAG;
8703#endif
8704
8705 if (dev->flags & IFF_PROMISC) {
8706 /* Promiscuous mode. */
8707 rx_mode |= RX_MODE_PROMISC;
8708 } else if (dev->flags & IFF_ALLMULTI) {
8709 /* Accept all multicast. */
8710 tg3_set_multi(tp, 1);
8711 } else if (netdev_mc_empty(dev)) {
8712 /* Reject all multicast. */
8713 tg3_set_multi(tp, 0);
8714 } else {
8715 /* Accept one or more multicast(s). */
8716 struct netdev_hw_addr *ha;
8717 u32 mc_filter[4] = { 0, };
8718 u32 regidx;
8719 u32 bit;
8720 u32 crc;
8721
8722 netdev_for_each_mc_addr(ha, dev) {
8723 crc = calc_crc(ha->addr, ETH_ALEN);
8724 bit = ~crc & 0x7f;
8725 regidx = (bit & 0x60) >> 5;
8726 bit &= 0x1f;
8727 mc_filter[regidx] |= (1 << bit);
8728 }
8729
8730 tw32(MAC_HASH_REG_0, mc_filter[0]);
8731 tw32(MAC_HASH_REG_1, mc_filter[1]);
8732 tw32(MAC_HASH_REG_2, mc_filter[2]);
8733 tw32(MAC_HASH_REG_3, mc_filter[3]);
8734 }
8735
8736 if (rx_mode != tp->rx_mode) {
8737 tp->rx_mode = rx_mode;
8738 tw32_f(MAC_RX_MODE, rx_mode);
8739 udelay(10);
8740 }
8741}
8742
Michael Chan91024262012-09-28 07:12:38 +00008743static void tg3_rss_init_dflt_indir_tbl(struct tg3 *tp, u32 qcnt)
Matt Carlson90415472011-12-16 13:33:23 +00008744{
8745 int i;
8746
8747 for (i = 0; i < TG3_RSS_INDIR_TBL_SIZE; i++)
Michael Chan91024262012-09-28 07:12:38 +00008748 tp->rss_ind_tbl[i] = ethtool_rxfh_indir_default(i, qcnt);
Matt Carlson90415472011-12-16 13:33:23 +00008749}
8750
8751static void tg3_rss_check_indir_tbl(struct tg3 *tp)
Matt Carlsonbcebcc42011-12-14 11:10:01 +00008752{
8753 int i;
8754
8755 if (!tg3_flag(tp, SUPPORT_MSIX))
8756 return;
8757
Michael Chan0b3ba052012-11-14 14:44:29 +00008758 if (tp->rxq_cnt == 1) {
Matt Carlsonbcebcc42011-12-14 11:10:01 +00008759 memset(&tp->rss_ind_tbl[0], 0, sizeof(tp->rss_ind_tbl));
Matt Carlson90415472011-12-16 13:33:23 +00008760 return;
8761 }
8762
8763 /* Validate table against current IRQ count */
8764 for (i = 0; i < TG3_RSS_INDIR_TBL_SIZE; i++) {
Michael Chan0b3ba052012-11-14 14:44:29 +00008765 if (tp->rss_ind_tbl[i] >= tp->rxq_cnt)
Matt Carlson90415472011-12-16 13:33:23 +00008766 break;
8767 }
8768
8769 if (i != TG3_RSS_INDIR_TBL_SIZE)
Michael Chan91024262012-09-28 07:12:38 +00008770 tg3_rss_init_dflt_indir_tbl(tp, tp->rxq_cnt);
Matt Carlsonbcebcc42011-12-14 11:10:01 +00008771}
8772
Matt Carlson90415472011-12-16 13:33:23 +00008773static void tg3_rss_write_indir_tbl(struct tg3 *tp)
Matt Carlsonbcebcc42011-12-14 11:10:01 +00008774{
8775 int i = 0;
8776 u32 reg = MAC_RSS_INDIR_TBL_0;
8777
8778 while (i < TG3_RSS_INDIR_TBL_SIZE) {
8779 u32 val = tp->rss_ind_tbl[i];
8780 i++;
8781 for (; i % 8; i++) {
8782 val <<= 4;
8783 val |= tp->rss_ind_tbl[i];
8784 }
8785 tw32(reg, val);
8786 reg += 4;
8787 }
8788}
8789
Matt Carlson2d31eca2009-09-01 12:53:31 +00008790/* tp->lock is held. */
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07008791static int tg3_reset_hw(struct tg3 *tp, int reset_phy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008792{
8793 u32 val, rdmac_mode;
8794 int i, err, limit;
Matt Carlson8fea32b2010-09-15 08:59:58 +00008795 struct tg3_rx_prodring_set *tpr = &tp->napi[0].prodring;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008796
8797 tg3_disable_ints(tp);
8798
8799 tg3_stop_fw(tp);
8800
8801 tg3_write_sig_pre_reset(tp, RESET_KIND_INIT);
8802
Joe Perches63c3a662011-04-26 08:12:10 +00008803 if (tg3_flag(tp, INIT_COMPLETE))
Michael Chane6de8ad2005-05-05 14:42:41 -07008804 tg3_abort_hw(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008805
Matt Carlson699c0192010-12-06 08:28:51 +00008806 /* Enable MAC control of LPI */
8807 if (tp->phy_flags & TG3_PHYFLG_EEE_CAP) {
8808 tw32_f(TG3_CPMU_EEE_LNKIDL_CTRL,
8809 TG3_CPMU_EEE_LNKIDL_PCIE_NL0 |
8810 TG3_CPMU_EEE_LNKIDL_UART_IDL);
8811
8812 tw32_f(TG3_CPMU_EEE_CTRL,
8813 TG3_CPMU_EEE_CTRL_EXIT_20_1_US);
8814
Matt Carlsona386b902010-12-06 08:28:53 +00008815 val = TG3_CPMU_EEEMD_ERLY_L1_XIT_DET |
8816 TG3_CPMU_EEEMD_LPI_IN_TX |
8817 TG3_CPMU_EEEMD_LPI_IN_RX |
8818 TG3_CPMU_EEEMD_EEE_ENABLE;
8819
8820 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717)
8821 val |= TG3_CPMU_EEEMD_SND_IDX_DET_EN;
8822
Joe Perches63c3a662011-04-26 08:12:10 +00008823 if (tg3_flag(tp, ENABLE_APE))
Matt Carlsona386b902010-12-06 08:28:53 +00008824 val |= TG3_CPMU_EEEMD_APE_TX_DET_EN;
8825
8826 tw32_f(TG3_CPMU_EEE_MODE, val);
8827
8828 tw32_f(TG3_CPMU_EEE_DBTMR1,
8829 TG3_CPMU_DBTMR1_PCIEXIT_2047US |
8830 TG3_CPMU_DBTMR1_LNKIDLE_2047US);
8831
8832 tw32_f(TG3_CPMU_EEE_DBTMR2,
Matt Carlsond7f2ab22011-01-25 15:58:56 +00008833 TG3_CPMU_DBTMR2_APE_TX_2047US |
Matt Carlsona386b902010-12-06 08:28:53 +00008834 TG3_CPMU_DBTMR2_TXIDXEQ_2047US);
Matt Carlson699c0192010-12-06 08:28:51 +00008835 }
8836
Matt Carlson603f1172010-02-12 14:47:10 +00008837 if (reset_phy)
Michael Chand4d2c552006-03-20 17:47:20 -08008838 tg3_phy_reset(tp);
8839
Linus Torvalds1da177e2005-04-16 15:20:36 -07008840 err = tg3_chip_reset(tp);
8841 if (err)
8842 return err;
8843
8844 tg3_write_sig_legacy(tp, RESET_KIND_INIT);
8845
Matt Carlsonbcb37f62008-11-03 16:52:09 -08008846 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX) {
Matt Carlsond30cdd22007-10-07 23:28:35 -07008847 val = tr32(TG3_CPMU_CTRL);
8848 val &= ~(CPMU_CTRL_LINK_AWARE_MODE | CPMU_CTRL_LINK_IDLE_MODE);
8849 tw32(TG3_CPMU_CTRL, val);
Matt Carlson9acb9612007-11-12 21:10:06 -08008850
8851 val = tr32(TG3_CPMU_LSPD_10MB_CLK);
8852 val &= ~CPMU_LSPD_10MB_MACCLK_MASK;
8853 val |= CPMU_LSPD_10MB_MACCLK_6_25;
8854 tw32(TG3_CPMU_LSPD_10MB_CLK, val);
8855
8856 val = tr32(TG3_CPMU_LNK_AWARE_PWRMD);
8857 val &= ~CPMU_LNK_AWARE_MACCLK_MASK;
8858 val |= CPMU_LNK_AWARE_MACCLK_6_25;
8859 tw32(TG3_CPMU_LNK_AWARE_PWRMD, val);
8860
8861 val = tr32(TG3_CPMU_HST_ACC);
8862 val &= ~CPMU_HST_ACC_MACCLK_MASK;
8863 val |= CPMU_HST_ACC_MACCLK_6_25;
8864 tw32(TG3_CPMU_HST_ACC, val);
Matt Carlsond30cdd22007-10-07 23:28:35 -07008865 }
8866
Matt Carlson33466d92009-04-20 06:57:41 +00008867 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780) {
8868 val = tr32(PCIE_PWR_MGMT_THRESH) & ~PCIE_PWR_MGMT_L1_THRESH_MSK;
8869 val |= PCIE_PWR_MGMT_EXT_ASPM_TMR_EN |
8870 PCIE_PWR_MGMT_L1_THRESH_4MS;
8871 tw32(PCIE_PWR_MGMT_THRESH, val);
Matt Carlson521e6b92009-08-25 10:06:01 +00008872
8873 val = tr32(TG3_PCIE_EIDLE_DELAY) & ~TG3_PCIE_EIDLE_DELAY_MASK;
8874 tw32(TG3_PCIE_EIDLE_DELAY, val | TG3_PCIE_EIDLE_DELAY_13_CLKS);
8875
8876 tw32(TG3_CORR_ERR_STAT, TG3_CORR_ERR_STAT_CLEAR);
Matt Carlson33466d92009-04-20 06:57:41 +00008877
Matt Carlsonf40386c2009-11-02 14:24:02 +00008878 val = tr32(TG3_PCIE_LNKCTL) & ~TG3_PCIE_LNKCTL_L1_PLL_PD_EN;
8879 tw32(TG3_PCIE_LNKCTL, val | TG3_PCIE_LNKCTL_L1_PLL_PD_DIS);
Matt Carlson255ca312009-08-25 10:07:27 +00008880 }
8881
Joe Perches63c3a662011-04-26 08:12:10 +00008882 if (tg3_flag(tp, L1PLLPD_EN)) {
Matt Carlson614b0592010-01-20 16:58:02 +00008883 u32 grc_mode = tr32(GRC_MODE);
8884
8885 /* Access the lower 1K of PL PCIE block registers. */
8886 val = grc_mode & ~GRC_MODE_PCIE_PORT_MASK;
8887 tw32(GRC_MODE, val | GRC_MODE_PCIE_PL_SEL);
8888
8889 val = tr32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL1);
8890 tw32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL1,
8891 val | TG3_PCIE_PL_LO_PHYCTL1_L1PLLPD_EN);
8892
8893 tw32(GRC_MODE, grc_mode);
8894 }
8895
Matt Carlson55086ad2011-12-14 11:09:59 +00008896 if (tg3_flag(tp, 57765_CLASS)) {
Matt Carlson5093eed2010-11-24 08:31:45 +00008897 if (tp->pci_chip_rev_id == CHIPREV_ID_57765_A0) {
8898 u32 grc_mode = tr32(GRC_MODE);
Matt Carlsoncea46462010-04-12 06:58:24 +00008899
Matt Carlson5093eed2010-11-24 08:31:45 +00008900 /* Access the lower 1K of PL PCIE block registers. */
8901 val = grc_mode & ~GRC_MODE_PCIE_PORT_MASK;
8902 tw32(GRC_MODE, val | GRC_MODE_PCIE_PL_SEL);
Matt Carlsoncea46462010-04-12 06:58:24 +00008903
Matt Carlson5093eed2010-11-24 08:31:45 +00008904 val = tr32(TG3_PCIE_TLDLPL_PORT +
8905 TG3_PCIE_PL_LO_PHYCTL5);
8906 tw32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL5,
8907 val | TG3_PCIE_PL_LO_PHYCTL5_DIS_L2CLKREQ);
Matt Carlsoncea46462010-04-12 06:58:24 +00008908
Matt Carlson5093eed2010-11-24 08:31:45 +00008909 tw32(GRC_MODE, grc_mode);
8910 }
Matt Carlsona977dbe2010-04-12 06:58:26 +00008911
Matt Carlson1ff30a52011-05-19 12:12:46 +00008912 if (GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_57765_AX) {
8913 u32 grc_mode = tr32(GRC_MODE);
8914
8915 /* Access the lower 1K of DL PCIE block registers. */
8916 val = grc_mode & ~GRC_MODE_PCIE_PORT_MASK;
8917 tw32(GRC_MODE, val | GRC_MODE_PCIE_DL_SEL);
8918
8919 val = tr32(TG3_PCIE_TLDLPL_PORT +
8920 TG3_PCIE_DL_LO_FTSMAX);
8921 val &= ~TG3_PCIE_DL_LO_FTSMAX_MSK;
8922 tw32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_DL_LO_FTSMAX,
8923 val | TG3_PCIE_DL_LO_FTSMAX_VAL);
8924
8925 tw32(GRC_MODE, grc_mode);
8926 }
8927
Matt Carlsona977dbe2010-04-12 06:58:26 +00008928 val = tr32(TG3_CPMU_LSPD_10MB_CLK);
8929 val &= ~CPMU_LSPD_10MB_MACCLK_MASK;
8930 val |= CPMU_LSPD_10MB_MACCLK_6_25;
8931 tw32(TG3_CPMU_LSPD_10MB_CLK, val);
Matt Carlsoncea46462010-04-12 06:58:24 +00008932 }
8933
Linus Torvalds1da177e2005-04-16 15:20:36 -07008934 /* This works around an issue with Athlon chipsets on
8935 * B3 tigon3 silicon. This bit has no effect on any
8936 * other revision. But do not set this on PCI Express
Matt Carlson795d01c2007-10-07 23:28:17 -07008937 * chips and don't even touch the clocks if the CPMU is present.
Linus Torvalds1da177e2005-04-16 15:20:36 -07008938 */
Joe Perches63c3a662011-04-26 08:12:10 +00008939 if (!tg3_flag(tp, CPMU_PRESENT)) {
8940 if (!tg3_flag(tp, PCI_EXPRESS))
Matt Carlson795d01c2007-10-07 23:28:17 -07008941 tp->pci_clock_ctrl |= CLOCK_CTRL_DELAY_PCI_GRANT;
8942 tw32_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl);
8943 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008944
8945 if (tp->pci_chip_rev_id == CHIPREV_ID_5704_A0 &&
Joe Perches63c3a662011-04-26 08:12:10 +00008946 tg3_flag(tp, PCIX_MODE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008947 val = tr32(TG3PCI_PCISTATE);
8948 val |= PCISTATE_RETRY_SAME_DMA;
8949 tw32(TG3PCI_PCISTATE, val);
8950 }
8951
Joe Perches63c3a662011-04-26 08:12:10 +00008952 if (tg3_flag(tp, ENABLE_APE)) {
Matt Carlson0d3031d2007-10-10 18:02:43 -07008953 /* Allow reads and writes to the
8954 * APE register and memory space.
8955 */
8956 val = tr32(TG3PCI_PCISTATE);
8957 val |= PCISTATE_ALLOW_APE_CTLSPC_WR |
Matt Carlsonf92d9dc12010-06-05 17:24:30 +00008958 PCISTATE_ALLOW_APE_SHMEM_WR |
8959 PCISTATE_ALLOW_APE_PSPACE_WR;
Matt Carlson0d3031d2007-10-10 18:02:43 -07008960 tw32(TG3PCI_PCISTATE, val);
8961 }
8962
Linus Torvalds1da177e2005-04-16 15:20:36 -07008963 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5704_BX) {
8964 /* Enable some hw fixes. */
8965 val = tr32(TG3PCI_MSI_DATA);
8966 val |= (1 << 26) | (1 << 28) | (1 << 29);
8967 tw32(TG3PCI_MSI_DATA, val);
8968 }
8969
8970 /* Descriptor ring init may make accesses to the
8971 * NIC SRAM area to setup the TX descriptors, so we
8972 * can only do this after the hardware has been
8973 * successfully reset.
8974 */
Michael Chan32d8c572006-07-25 16:38:29 -07008975 err = tg3_init_rings(tp);
8976 if (err)
8977 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008978
Joe Perches63c3a662011-04-26 08:12:10 +00008979 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlsoncbf9ca62009-11-13 13:03:40 +00008980 val = tr32(TG3PCI_DMA_RW_CTRL) &
8981 ~DMA_RWCTRL_DIS_CACHE_ALIGNMENT;
Matt Carlson1a319022010-04-12 06:58:25 +00008982 if (tp->pci_chip_rev_id == CHIPREV_ID_57765_A0)
8983 val &= ~DMA_RWCTRL_CRDRDR_RDMA_MRRS_MSK;
Matt Carlson55086ad2011-12-14 11:09:59 +00008984 if (!tg3_flag(tp, 57765_CLASS) &&
Matt Carlson0aebff42011-04-25 12:42:45 +00008985 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717)
8986 val |= DMA_RWCTRL_TAGGED_STAT_WA;
Matt Carlsoncbf9ca62009-11-13 13:03:40 +00008987 tw32(TG3PCI_DMA_RW_CTRL, val | tp->dma_rwctrl);
8988 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5784 &&
8989 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5761) {
Matt Carlsond30cdd22007-10-07 23:28:35 -07008990 /* This value is determined during the probe time DMA
8991 * engine test, tg3_test_dma.
8992 */
8993 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
8994 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008995
8996 tp->grc_mode &= ~(GRC_MODE_HOST_SENDBDS |
8997 GRC_MODE_4X_NIC_SEND_RINGS |
8998 GRC_MODE_NO_TX_PHDR_CSUM |
8999 GRC_MODE_NO_RX_PHDR_CSUM);
9000 tp->grc_mode |= GRC_MODE_HOST_SENDBDS;
Michael Chand2d746f2006-04-06 21:45:39 -07009001
9002 /* Pseudo-header checksum is done by hardware logic and not
9003 * the offload processers, so make the chip do the pseudo-
9004 * header checksums on receive. For transmit it is more
9005 * convenient to do the pseudo-header checksum in software
9006 * as Linux does that on transmit for us in all cases.
9007 */
9008 tp->grc_mode |= GRC_MODE_NO_TX_PHDR_CSUM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009009
9010 tw32(GRC_MODE,
9011 tp->grc_mode |
9012 (GRC_MODE_IRQ_ON_MAC_ATTN | GRC_MODE_HOST_STACKUP));
9013
9014 /* Setup the timer prescalar register. Clock is always 66Mhz. */
9015 val = tr32(GRC_MISC_CFG);
9016 val &= ~0xff;
9017 val |= (65 << GRC_MISC_CFG_PRESCALAR_SHIFT);
9018 tw32(GRC_MISC_CFG, val);
9019
9020 /* Initialize MBUF/DESC pool. */
Joe Perches63c3a662011-04-26 08:12:10 +00009021 if (tg3_flag(tp, 5750_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009022 /* Do nothing. */
9023 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5705) {
9024 tw32(BUFMGR_MB_POOL_ADDR, NIC_SRAM_MBUF_POOL_BASE);
9025 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704)
9026 tw32(BUFMGR_MB_POOL_SIZE, NIC_SRAM_MBUF_POOL_SIZE64);
9027 else
9028 tw32(BUFMGR_MB_POOL_SIZE, NIC_SRAM_MBUF_POOL_SIZE96);
9029 tw32(BUFMGR_DMA_DESC_POOL_ADDR, NIC_SRAM_DMA_DESC_POOL_BASE);
9030 tw32(BUFMGR_DMA_DESC_POOL_SIZE, NIC_SRAM_DMA_DESC_POOL_SIZE);
Joe Perches63c3a662011-04-26 08:12:10 +00009031 } else if (tg3_flag(tp, TSO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009032 int fw_len;
9033
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08009034 fw_len = tp->fw_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009035 fw_len = (fw_len + (0x80 - 1)) & ~(0x80 - 1);
9036 tw32(BUFMGR_MB_POOL_ADDR,
9037 NIC_SRAM_MBUF_POOL_BASE5705 + fw_len);
9038 tw32(BUFMGR_MB_POOL_SIZE,
9039 NIC_SRAM_MBUF_POOL_SIZE5705 - fw_len - 0xa00);
9040 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009041
Michael Chan0f893dc2005-07-25 12:30:38 -07009042 if (tp->dev->mtu <= ETH_DATA_LEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009043 tw32(BUFMGR_MB_RDMA_LOW_WATER,
9044 tp->bufmgr_config.mbuf_read_dma_low_water);
9045 tw32(BUFMGR_MB_MACRX_LOW_WATER,
9046 tp->bufmgr_config.mbuf_mac_rx_low_water);
9047 tw32(BUFMGR_MB_HIGH_WATER,
9048 tp->bufmgr_config.mbuf_high_water);
9049 } else {
9050 tw32(BUFMGR_MB_RDMA_LOW_WATER,
9051 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo);
9052 tw32(BUFMGR_MB_MACRX_LOW_WATER,
9053 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo);
9054 tw32(BUFMGR_MB_HIGH_WATER,
9055 tp->bufmgr_config.mbuf_high_water_jumbo);
9056 }
9057 tw32(BUFMGR_DMA_LOW_WATER,
9058 tp->bufmgr_config.dma_low_water);
9059 tw32(BUFMGR_DMA_HIGH_WATER,
9060 tp->bufmgr_config.dma_high_water);
9061
Matt Carlsond309a462010-09-30 10:34:31 +00009062 val = BUFMGR_MODE_ENABLE | BUFMGR_MODE_ATTN_ENABLE;
9063 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
9064 val |= BUFMGR_MODE_NO_TX_UNDERRUN;
Matt Carlson4d958472011-04-20 07:57:35 +00009065 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
9066 tp->pci_chip_rev_id == CHIPREV_ID_5719_A0 ||
9067 tp->pci_chip_rev_id == CHIPREV_ID_5720_A0)
9068 val |= BUFMGR_MODE_MBLOW_ATTN_ENAB;
Matt Carlsond309a462010-09-30 10:34:31 +00009069 tw32(BUFMGR_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009070 for (i = 0; i < 2000; i++) {
9071 if (tr32(BUFMGR_MODE) & BUFMGR_MODE_ENABLE)
9072 break;
9073 udelay(10);
9074 }
9075 if (i >= 2000) {
Joe Perches05dbe002010-02-17 19:44:19 +00009076 netdev_err(tp->dev, "%s cannot enable BUFMGR\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009077 return -ENODEV;
9078 }
9079
Matt Carlsoneb07a942011-04-20 07:57:36 +00009080 if (tp->pci_chip_rev_id == CHIPREV_ID_5906_A1)
9081 tw32(ISO_PKT_TX, (tr32(ISO_PKT_TX) & ~0x3) | 0x2);
Michael Chanb5d37722006-09-27 16:06:21 -07009082
Matt Carlsoneb07a942011-04-20 07:57:36 +00009083 tg3_setup_rxbd_thresholds(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009084
9085 /* Initialize TG3_BDINFO's at:
9086 * RCVDBDI_STD_BD: standard eth size rx ring
9087 * RCVDBDI_JUMBO_BD: jumbo frame rx ring
9088 * RCVDBDI_MINI_BD: small frame rx ring (??? does not work)
9089 *
9090 * like so:
9091 * TG3_BDINFO_HOST_ADDR: high/low parts of DMA address of ring
9092 * TG3_BDINFO_MAXLEN_FLAGS: (rx max buffer size << 16) |
9093 * ring attribute flags
9094 * TG3_BDINFO_NIC_ADDR: location of descriptors in nic SRAM
9095 *
9096 * Standard receive ring @ NIC_SRAM_RX_BUFFER_DESC, 512 entries.
9097 * Jumbo receive ring @ NIC_SRAM_RX_JUMBO_BUFFER_DESC, 256 entries.
9098 *
9099 * The size of each ring is fixed in the firmware, but the location is
9100 * configurable.
9101 */
9102 tw32(RCVDBDI_STD_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH,
Matt Carlson21f581a2009-08-28 14:00:25 +00009103 ((u64) tpr->rx_std_mapping >> 32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07009104 tw32(RCVDBDI_STD_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW,
Matt Carlson21f581a2009-08-28 14:00:25 +00009105 ((u64) tpr->rx_std_mapping & 0xffffffff));
Joe Perches63c3a662011-04-26 08:12:10 +00009106 if (!tg3_flag(tp, 5717_PLUS))
Matt Carlson87668d32009-11-13 13:03:34 +00009107 tw32(RCVDBDI_STD_BD + TG3_BDINFO_NIC_ADDR,
9108 NIC_SRAM_RX_BUFFER_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009109
Matt Carlsonfdb72b32009-08-28 13:57:12 +00009110 /* Disable the mini ring */
Joe Perches63c3a662011-04-26 08:12:10 +00009111 if (!tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009112 tw32(RCVDBDI_MINI_BD + TG3_BDINFO_MAXLEN_FLAGS,
9113 BDINFO_FLAGS_DISABLED);
9114
Matt Carlsonfdb72b32009-08-28 13:57:12 +00009115 /* Program the jumbo buffer descriptor ring control
9116 * blocks on those devices that have them.
9117 */
Matt Carlsona0512942011-07-27 14:20:54 +00009118 if (tp->pci_chip_rev_id == CHIPREV_ID_5719_A0 ||
Joe Perches63c3a662011-04-26 08:12:10 +00009119 (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009120
Joe Perches63c3a662011-04-26 08:12:10 +00009121 if (tg3_flag(tp, JUMBO_RING_ENABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009122 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH,
Matt Carlson21f581a2009-08-28 14:00:25 +00009123 ((u64) tpr->rx_jmb_mapping >> 32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07009124 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW,
Matt Carlson21f581a2009-08-28 14:00:25 +00009125 ((u64) tpr->rx_jmb_mapping & 0xffffffff));
Matt Carlsonde9f5232011-04-05 14:22:43 +00009126 val = TG3_RX_JMB_RING_SIZE(tp) <<
9127 BDINFO_FLAGS_MAXLEN_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009128 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_MAXLEN_FLAGS,
Matt Carlsonde9f5232011-04-05 14:22:43 +00009129 val | BDINFO_FLAGS_USE_EXT_RECV);
Joe Perches63c3a662011-04-26 08:12:10 +00009130 if (!tg3_flag(tp, USE_JUMBO_BDFLAG) ||
Matt Carlson55086ad2011-12-14 11:09:59 +00009131 tg3_flag(tp, 57765_CLASS))
Matt Carlson87668d32009-11-13 13:03:34 +00009132 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_NIC_ADDR,
9133 NIC_SRAM_RX_JUMBO_BUFFER_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009134 } else {
9135 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_MAXLEN_FLAGS,
9136 BDINFO_FLAGS_DISABLED);
9137 }
9138
Joe Perches63c3a662011-04-26 08:12:10 +00009139 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlsonfa6b2aa2011-11-21 15:01:19 +00009140 val = TG3_RX_STD_RING_SIZE(tp);
Matt Carlson7cb32cf2010-09-30 10:34:36 +00009141 val <<= BDINFO_FLAGS_MAXLEN_SHIFT;
9142 val |= (TG3_RX_STD_DMA_SZ << 2);
9143 } else
Matt Carlson04380d42010-04-12 06:58:29 +00009144 val = TG3_RX_STD_DMA_SZ << BDINFO_FLAGS_MAXLEN_SHIFT;
Matt Carlsonfdb72b32009-08-28 13:57:12 +00009145 } else
Matt Carlsonde9f5232011-04-05 14:22:43 +00009146 val = TG3_RX_STD_MAX_SIZE_5700 << BDINFO_FLAGS_MAXLEN_SHIFT;
Matt Carlsonfdb72b32009-08-28 13:57:12 +00009147
9148 tw32(RCVDBDI_STD_BD + TG3_BDINFO_MAXLEN_FLAGS, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009149
Matt Carlson411da642009-11-13 13:03:46 +00009150 tpr->rx_std_prod_idx = tp->rx_pending;
Matt Carlson66711e62009-11-13 13:03:49 +00009151 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG, tpr->rx_std_prod_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009152
Joe Perches63c3a662011-04-26 08:12:10 +00009153 tpr->rx_jmb_prod_idx =
9154 tg3_flag(tp, JUMBO_RING_ENABLE) ? tp->rx_jumbo_pending : 0;
Matt Carlson66711e62009-11-13 13:03:49 +00009155 tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG, tpr->rx_jmb_prod_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009156
Matt Carlson2d31eca2009-09-01 12:53:31 +00009157 tg3_rings_reset(tp);
9158
Linus Torvalds1da177e2005-04-16 15:20:36 -07009159 /* Initialize MAC address and backoff seed. */
Michael Chan986e0ae2007-05-05 12:10:20 -07009160 __tg3_set_mac_addr(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009161
9162 /* MTU + ethernet header + FCS + optional VLAN tag */
Matt Carlsonf7b493e2009-02-25 14:21:52 +00009163 tw32(MAC_RX_MTU_SIZE,
9164 tp->dev->mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009165
9166 /* The slot time is changed by tg3_setup_phy if we
9167 * run at gigabit with half duplex.
9168 */
Matt Carlsonf2096f92011-04-05 14:22:48 +00009169 val = (2 << TX_LENGTHS_IPG_CRS_SHIFT) |
9170 (6 << TX_LENGTHS_IPG_SHIFT) |
9171 (32 << TX_LENGTHS_SLOT_TIME_SHIFT);
9172
9173 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
9174 val |= tr32(MAC_TX_LENGTHS) &
9175 (TX_LENGTHS_JMB_FRM_LEN_MSK |
9176 TX_LENGTHS_CNT_DWN_VAL_MSK);
9177
9178 tw32(MAC_TX_LENGTHS, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009179
9180 /* Receive rules. */
9181 tw32(MAC_RCV_RULE_CFG, RCV_RULE_CFG_DEFAULT_CLASS);
9182 tw32(RCVLPC_CONFIG, 0x0181);
9183
9184 /* Calculate RDMAC_MODE setting early, we need it to determine
9185 * the RCVLPC_STATE_ENABLE mask.
9186 */
9187 rdmac_mode = (RDMAC_MODE_ENABLE | RDMAC_MODE_TGTABORT_ENAB |
9188 RDMAC_MODE_MSTABORT_ENAB | RDMAC_MODE_PARITYERR_ENAB |
9189 RDMAC_MODE_ADDROFLOW_ENAB | RDMAC_MODE_FIFOOFLOW_ENAB |
9190 RDMAC_MODE_FIFOURUN_ENAB | RDMAC_MODE_FIFOOREAD_ENAB |
9191 RDMAC_MODE_LNGREAD_ENAB);
Michael Chan85e94ce2005-04-21 17:05:28 -07009192
Matt Carlsondeabaac2010-11-24 08:31:50 +00009193 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717)
Matt Carlson0339e4e2010-02-12 14:47:09 +00009194 rdmac_mode |= RDMAC_MODE_MULT_DMA_RD_DIS;
9195
Matt Carlson57e69832008-05-25 23:48:31 -07009196 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson321d32a2008-11-21 17:22:19 -08009197 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
9198 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
Matt Carlsond30cdd22007-10-07 23:28:35 -07009199 rdmac_mode |= RDMAC_MODE_BD_SBD_CRPT_ENAB |
9200 RDMAC_MODE_MBUF_RBD_CRPT_ENAB |
9201 RDMAC_MODE_MBUF_SBD_CRPT_ENAB;
9202
Matt Carlsonc5908932011-03-09 16:58:25 +00009203 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
9204 tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) {
Joe Perches63c3a662011-04-26 08:12:10 +00009205 if (tg3_flag(tp, TSO_CAPABLE) &&
Matt Carlsonc13e3712007-05-05 11:50:04 -07009206 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009207 rdmac_mode |= RDMAC_MODE_FIFO_SIZE_128;
9208 } else if (!(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH) &&
Joe Perches63c3a662011-04-26 08:12:10 +00009209 !tg3_flag(tp, IS_5788)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009210 rdmac_mode |= RDMAC_MODE_FIFO_LONG_BURST;
9211 }
9212 }
9213
Joe Perches63c3a662011-04-26 08:12:10 +00009214 if (tg3_flag(tp, PCI_EXPRESS))
Michael Chan85e94ce2005-04-21 17:05:28 -07009215 rdmac_mode |= RDMAC_MODE_FIFO_LONG_BURST;
9216
Joe Perches63c3a662011-04-26 08:12:10 +00009217 if (tg3_flag(tp, HW_TSO_1) ||
9218 tg3_flag(tp, HW_TSO_2) ||
9219 tg3_flag(tp, HW_TSO_3))
Matt Carlson027455a2008-12-21 20:19:30 -08009220 rdmac_mode |= RDMAC_MODE_IPV4_LSO_EN;
9221
Matt Carlson108a6c12011-05-19 12:12:47 +00009222 if (tg3_flag(tp, 57765_PLUS) ||
Matt Carlsone849cdc2009-11-13 13:03:38 +00009223 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Matt Carlson027455a2008-12-21 20:19:30 -08009224 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
9225 rdmac_mode |= RDMAC_MODE_IPV6_LSO_EN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009226
Matt Carlsonf2096f92011-04-05 14:22:48 +00009227 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
9228 rdmac_mode |= tr32(RDMAC_MODE) & RDMAC_MODE_H2BNC_VLAN_DET;
9229
Matt Carlson41a8a7e2010-09-15 08:59:53 +00009230 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
9231 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
9232 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
9233 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
Joe Perches63c3a662011-04-26 08:12:10 +00009234 tg3_flag(tp, 57765_PLUS)) {
Matt Carlson41a8a7e2010-09-15 08:59:53 +00009235 val = tr32(TG3_RDMA_RSRVCTRL_REG);
Michael Chan10ce95d2012-07-29 19:15:42 +00009236 if (tp->pci_chip_rev_id == CHIPREV_ID_5719_A0) {
Matt Carlsonb4495ed2011-01-25 15:58:47 +00009237 val &= ~(TG3_RDMA_RSRVCTRL_TXMRGN_MASK |
9238 TG3_RDMA_RSRVCTRL_FIFO_LWM_MASK |
9239 TG3_RDMA_RSRVCTRL_FIFO_HWM_MASK);
9240 val |= TG3_RDMA_RSRVCTRL_TXMRGN_320B |
9241 TG3_RDMA_RSRVCTRL_FIFO_LWM_1_5K |
9242 TG3_RDMA_RSRVCTRL_FIFO_HWM_1_5K;
Matt Carlsonb75cc0e2010-11-24 08:31:46 +00009243 }
Matt Carlson41a8a7e2010-09-15 08:59:53 +00009244 tw32(TG3_RDMA_RSRVCTRL_REG,
9245 val | TG3_RDMA_RSRVCTRL_FIFO_OFLW_FIX);
9246 }
9247
Matt Carlsond78b59f2011-04-05 14:22:46 +00009248 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
9249 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
Matt Carlsond309a462010-09-30 10:34:31 +00009250 val = tr32(TG3_LSO_RD_DMA_CRPTEN_CTRL);
9251 tw32(TG3_LSO_RD_DMA_CRPTEN_CTRL, val |
9252 TG3_LSO_RD_DMA_CRPTEN_CTRL_BLEN_BD_4K |
9253 TG3_LSO_RD_DMA_CRPTEN_CTRL_BLEN_LSO_4K);
9254 }
9255
Linus Torvalds1da177e2005-04-16 15:20:36 -07009256 /* Receive/send statistics. */
Joe Perches63c3a662011-04-26 08:12:10 +00009257 if (tg3_flag(tp, 5750_PLUS)) {
Michael Chan16613942006-06-29 20:15:13 -07009258 val = tr32(RCVLPC_STATS_ENABLE);
9259 val &= ~RCVLPC_STATSENAB_DACK_FIX;
9260 tw32(RCVLPC_STATS_ENABLE, val);
9261 } else if ((rdmac_mode & RDMAC_MODE_FIFO_SIZE_128) &&
Joe Perches63c3a662011-04-26 08:12:10 +00009262 tg3_flag(tp, TSO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009263 val = tr32(RCVLPC_STATS_ENABLE);
9264 val &= ~RCVLPC_STATSENAB_LNGBRST_RFIX;
9265 tw32(RCVLPC_STATS_ENABLE, val);
9266 } else {
9267 tw32(RCVLPC_STATS_ENABLE, 0xffffff);
9268 }
9269 tw32(RCVLPC_STATSCTRL, RCVLPC_STATSCTRL_ENABLE);
9270 tw32(SNDDATAI_STATSENAB, 0xffffff);
9271 tw32(SNDDATAI_STATSCTRL,
9272 (SNDDATAI_SCTRL_ENABLE |
9273 SNDDATAI_SCTRL_FASTUPD));
9274
9275 /* Setup host coalescing engine. */
9276 tw32(HOSTCC_MODE, 0);
9277 for (i = 0; i < 2000; i++) {
9278 if (!(tr32(HOSTCC_MODE) & HOSTCC_MODE_ENABLE))
9279 break;
9280 udelay(10);
9281 }
9282
Michael Chand244c892005-07-05 14:42:33 -07009283 __tg3_set_coalesce(tp, &tp->coal);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009284
Joe Perches63c3a662011-04-26 08:12:10 +00009285 if (!tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009286 /* Status/statistics block address. See tg3_timer,
9287 * the tg3_periodic_fetch_stats call there, and
9288 * tg3_get_stats to see how this works for 5705/5750 chips.
9289 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07009290 tw32(HOSTCC_STATS_BLK_HOST_ADDR + TG3_64BIT_REG_HIGH,
9291 ((u64) tp->stats_mapping >> 32));
9292 tw32(HOSTCC_STATS_BLK_HOST_ADDR + TG3_64BIT_REG_LOW,
9293 ((u64) tp->stats_mapping & 0xffffffff));
9294 tw32(HOSTCC_STATS_BLK_NIC_ADDR, NIC_SRAM_STATS_BLK);
Matt Carlson2d31eca2009-09-01 12:53:31 +00009295
Linus Torvalds1da177e2005-04-16 15:20:36 -07009296 tw32(HOSTCC_STATUS_BLK_NIC_ADDR, NIC_SRAM_STATUS_BLK);
Matt Carlson2d31eca2009-09-01 12:53:31 +00009297
9298 /* Clear statistics and status block memory areas */
9299 for (i = NIC_SRAM_STATS_BLK;
9300 i < NIC_SRAM_STATUS_BLK + TG3_HW_STATUS_SIZE;
9301 i += sizeof(u32)) {
9302 tg3_write_mem(tp, i, 0);
9303 udelay(40);
9304 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009305 }
9306
9307 tw32(HOSTCC_MODE, HOSTCC_MODE_ENABLE | tp->coalesce_mode);
9308
9309 tw32(RCVCC_MODE, RCVCC_MODE_ENABLE | RCVCC_MODE_ATTN_ENABLE);
9310 tw32(RCVLPC_MODE, RCVLPC_MODE_ENABLE);
Joe Perches63c3a662011-04-26 08:12:10 +00009311 if (!tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009312 tw32(RCVLSC_MODE, RCVLSC_MODE_ENABLE | RCVLSC_MODE_ATTN_ENABLE);
9313
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009314 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES) {
9315 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chanc94e3942005-09-27 12:12:42 -07009316 /* reset to prevent losing 1st rx packet intermittently */
9317 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
9318 udelay(10);
9319 }
9320
Matt Carlson3bda1252008-08-15 14:08:22 -07009321 tp->mac_mode |= MAC_MODE_TXSTAT_ENABLE | MAC_MODE_RXSTAT_ENABLE |
Matt Carlson9e975cc2011-07-20 10:20:50 +00009322 MAC_MODE_TDE_ENABLE | MAC_MODE_RDE_ENABLE |
9323 MAC_MODE_FHDE_ENABLE;
9324 if (tg3_flag(tp, ENABLE_APE))
9325 tp->mac_mode |= MAC_MODE_APE_TX_EN | MAC_MODE_APE_RX_EN;
Joe Perches63c3a662011-04-26 08:12:10 +00009326 if (!tg3_flag(tp, 5705_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009327 !(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07009328 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700)
9329 tp->mac_mode |= MAC_MODE_LINK_POLARITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009330 tw32_f(MAC_MODE, tp->mac_mode | MAC_MODE_RXSTAT_CLEAR | MAC_MODE_TXSTAT_CLEAR);
9331 udelay(40);
9332
Michael Chan314fba32005-04-21 17:07:04 -07009333 /* tp->grc_local_ctrl is partially set up during tg3_get_invariants().
Joe Perches63c3a662011-04-26 08:12:10 +00009334 * If TG3_FLAG_IS_NIC is zero, we should read the
Michael Chan314fba32005-04-21 17:07:04 -07009335 * register to preserve the GPIO settings for LOMs. The GPIOs,
9336 * whether used as inputs or outputs, are set by boot code after
9337 * reset.
9338 */
Joe Perches63c3a662011-04-26 08:12:10 +00009339 if (!tg3_flag(tp, IS_NIC)) {
Michael Chan314fba32005-04-21 17:07:04 -07009340 u32 gpio_mask;
9341
Michael Chan9d26e212006-12-07 00:21:14 -08009342 gpio_mask = GRC_LCLCTRL_GPIO_OE0 | GRC_LCLCTRL_GPIO_OE1 |
9343 GRC_LCLCTRL_GPIO_OE2 | GRC_LCLCTRL_GPIO_OUTPUT0 |
9344 GRC_LCLCTRL_GPIO_OUTPUT1 | GRC_LCLCTRL_GPIO_OUTPUT2;
Michael Chan3e7d83b2005-04-21 17:10:36 -07009345
9346 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752)
9347 gpio_mask |= GRC_LCLCTRL_GPIO_OE3 |
9348 GRC_LCLCTRL_GPIO_OUTPUT3;
9349
Michael Chanaf36e6b2006-03-23 01:28:06 -08009350 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755)
9351 gpio_mask |= GRC_LCLCTRL_GPIO_UART_SEL;
9352
Gary Zambranoaaf84462007-05-05 11:51:45 -07009353 tp->grc_local_ctrl &= ~gpio_mask;
Michael Chan314fba32005-04-21 17:07:04 -07009354 tp->grc_local_ctrl |= tr32(GRC_LOCAL_CTRL) & gpio_mask;
9355
9356 /* GPIO1 must be driven high for eeprom write protect */
Joe Perches63c3a662011-04-26 08:12:10 +00009357 if (tg3_flag(tp, EEPROM_WRITE_PROT))
Michael Chan9d26e212006-12-07 00:21:14 -08009358 tp->grc_local_ctrl |= (GRC_LCLCTRL_GPIO_OE1 |
9359 GRC_LCLCTRL_GPIO_OUTPUT1);
Michael Chan314fba32005-04-21 17:07:04 -07009360 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009361 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
9362 udelay(100);
9363
Matt Carlsonc3b50032012-01-17 15:27:23 +00009364 if (tg3_flag(tp, USING_MSIX)) {
Matt Carlsonbaf8a942009-09-01 13:13:00 +00009365 val = tr32(MSGINT_MODE);
Matt Carlsonc3b50032012-01-17 15:27:23 +00009366 val |= MSGINT_MODE_ENABLE;
9367 if (tp->irq_cnt > 1)
9368 val |= MSGINT_MODE_MULTIVEC_EN;
Matt Carlson5b39de92011-08-31 11:44:50 +00009369 if (!tg3_flag(tp, 1SHOT_MSI))
9370 val |= MSGINT_MODE_ONE_SHOT_DISABLE;
Matt Carlsonbaf8a942009-09-01 13:13:00 +00009371 tw32(MSGINT_MODE, val);
9372 }
9373
Joe Perches63c3a662011-04-26 08:12:10 +00009374 if (!tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009375 tw32_f(DMAC_MODE, DMAC_MODE_ENABLE);
9376 udelay(40);
9377 }
9378
9379 val = (WDMAC_MODE_ENABLE | WDMAC_MODE_TGTABORT_ENAB |
9380 WDMAC_MODE_MSTABORT_ENAB | WDMAC_MODE_PARITYERR_ENAB |
9381 WDMAC_MODE_ADDROFLOW_ENAB | WDMAC_MODE_FIFOOFLOW_ENAB |
9382 WDMAC_MODE_FIFOURUN_ENAB | WDMAC_MODE_FIFOOREAD_ENAB |
9383 WDMAC_MODE_LNGREAD_ENAB);
9384
Matt Carlsonc5908932011-03-09 16:58:25 +00009385 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
9386 tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) {
Joe Perches63c3a662011-04-26 08:12:10 +00009387 if (tg3_flag(tp, TSO_CAPABLE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07009388 (tp->pci_chip_rev_id == CHIPREV_ID_5705_A1 ||
9389 tp->pci_chip_rev_id == CHIPREV_ID_5705_A2)) {
9390 /* nothing */
9391 } else if (!(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH) &&
Joe Perches63c3a662011-04-26 08:12:10 +00009392 !tg3_flag(tp, IS_5788)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009393 val |= WDMAC_MODE_RX_ACCEL;
9394 }
9395 }
9396
Michael Chand9ab5ad2006-03-20 22:27:35 -08009397 /* Enable host coalescing bug fix */
Joe Perches63c3a662011-04-26 08:12:10 +00009398 if (tg3_flag(tp, 5755_PLUS))
Matt Carlsonf51f3562008-05-25 23:45:08 -07009399 val |= WDMAC_MODE_STATUS_TAG_FIX;
Michael Chand9ab5ad2006-03-20 22:27:35 -08009400
Matt Carlson788a0352009-11-02 14:26:03 +00009401 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
9402 val |= WDMAC_MODE_BURST_ALL_DATA;
9403
Linus Torvalds1da177e2005-04-16 15:20:36 -07009404 tw32_f(WDMAC_MODE, val);
9405 udelay(40);
9406
Joe Perches63c3a662011-04-26 08:12:10 +00009407 if (tg3_flag(tp, PCIX_MODE)) {
Matt Carlson9974a352007-10-07 23:27:28 -07009408 u16 pcix_cmd;
9409
9410 pci_read_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
9411 &pcix_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009412 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703) {
Matt Carlson9974a352007-10-07 23:27:28 -07009413 pcix_cmd &= ~PCI_X_CMD_MAX_READ;
9414 pcix_cmd |= PCI_X_CMD_READ_2K;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009415 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
Matt Carlson9974a352007-10-07 23:27:28 -07009416 pcix_cmd &= ~(PCI_X_CMD_MAX_SPLIT | PCI_X_CMD_MAX_READ);
9417 pcix_cmd |= PCI_X_CMD_READ_2K;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009418 }
Matt Carlson9974a352007-10-07 23:27:28 -07009419 pci_write_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
9420 pcix_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009421 }
9422
9423 tw32_f(RDMAC_MODE, rdmac_mode);
9424 udelay(40);
9425
Michael Chan091f0ea2012-07-29 19:15:43 +00009426 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719) {
9427 for (i = 0; i < TG3_NUM_RDMA_CHANNELS; i++) {
9428 if (tr32(TG3_RDMA_LENGTH + (i << 2)) > TG3_MAX_MTU(tp))
9429 break;
9430 }
9431 if (i < TG3_NUM_RDMA_CHANNELS) {
9432 val = tr32(TG3_LSO_RD_DMA_CRPTEN_CTRL);
9433 val |= TG3_LSO_RD_DMA_TX_LENGTH_WA;
9434 tw32(TG3_LSO_RD_DMA_CRPTEN_CTRL, val);
9435 tg3_flag_set(tp, 5719_RDMA_BUG);
9436 }
9437 }
9438
Linus Torvalds1da177e2005-04-16 15:20:36 -07009439 tw32(RCVDCC_MODE, RCVDCC_MODE_ENABLE | RCVDCC_MODE_ATTN_ENABLE);
Joe Perches63c3a662011-04-26 08:12:10 +00009440 if (!tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009441 tw32(MBFREE_MODE, MBFREE_MODE_ENABLE);
Matt Carlson9936bcf2007-10-10 18:03:07 -07009442
9443 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
9444 tw32(SNDDATAC_MODE,
9445 SNDDATAC_MODE_ENABLE | SNDDATAC_MODE_CDELAY);
9446 else
9447 tw32(SNDDATAC_MODE, SNDDATAC_MODE_ENABLE);
9448
Linus Torvalds1da177e2005-04-16 15:20:36 -07009449 tw32(SNDBDC_MODE, SNDBDC_MODE_ENABLE | SNDBDC_MODE_ATTN_ENABLE);
9450 tw32(RCVBDI_MODE, RCVBDI_MODE_ENABLE | RCVBDI_MODE_RCB_ATTN_ENAB);
Matt Carlson7cb32cf2010-09-30 10:34:36 +00009451 val = RCVDBDI_MODE_ENABLE | RCVDBDI_MODE_INV_RING_SZ;
Joe Perches63c3a662011-04-26 08:12:10 +00009452 if (tg3_flag(tp, LRG_PROD_RING_CAP))
Matt Carlson7cb32cf2010-09-30 10:34:36 +00009453 val |= RCVDBDI_MODE_LRG_RING_SZ;
9454 tw32(RCVDBDI_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009455 tw32(SNDDATAI_MODE, SNDDATAI_MODE_ENABLE);
Joe Perches63c3a662011-04-26 08:12:10 +00009456 if (tg3_flag(tp, HW_TSO_1) ||
9457 tg3_flag(tp, HW_TSO_2) ||
9458 tg3_flag(tp, HW_TSO_3))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009459 tw32(SNDDATAI_MODE, SNDDATAI_MODE_ENABLE | 0x8);
Matt Carlsonbaf8a942009-09-01 13:13:00 +00009460 val = SNDBDI_MODE_ENABLE | SNDBDI_MODE_ATTN_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00009461 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlsonbaf8a942009-09-01 13:13:00 +00009462 val |= SNDBDI_MODE_MULTI_TXQ_EN;
9463 tw32(SNDBDI_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009464 tw32(SNDBDS_MODE, SNDBDS_MODE_ENABLE | SNDBDS_MODE_ATTN_ENABLE);
9465
9466 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0) {
9467 err = tg3_load_5701_a0_firmware_fix(tp);
9468 if (err)
9469 return err;
9470 }
9471
Joe Perches63c3a662011-04-26 08:12:10 +00009472 if (tg3_flag(tp, TSO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009473 err = tg3_load_tso_firmware(tp);
9474 if (err)
9475 return err;
9476 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009477
9478 tp->tx_mode = TX_MODE_ENABLE;
Matt Carlsonf2096f92011-04-05 14:22:48 +00009479
Joe Perches63c3a662011-04-26 08:12:10 +00009480 if (tg3_flag(tp, 5755_PLUS) ||
Matt Carlsonb1d05212010-06-05 17:24:31 +00009481 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
9482 tp->tx_mode |= TX_MODE_MBUF_LOCKUP_FIX;
Matt Carlsonf2096f92011-04-05 14:22:48 +00009483
9484 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
9485 val = TX_MODE_JMB_FRM_LEN | TX_MODE_CNT_DN_MODE;
9486 tp->tx_mode &= ~val;
9487 tp->tx_mode |= tr32(MAC_TX_MODE) & val;
9488 }
9489
Linus Torvalds1da177e2005-04-16 15:20:36 -07009490 tw32_f(MAC_TX_MODE, tp->tx_mode);
9491 udelay(100);
9492
Joe Perches63c3a662011-04-26 08:12:10 +00009493 if (tg3_flag(tp, ENABLE_RSS)) {
Matt Carlsonbcebcc42011-12-14 11:10:01 +00009494 tg3_rss_write_indir_tbl(tp);
Matt Carlsonbaf8a942009-09-01 13:13:00 +00009495
9496 /* Setup the "secret" hash key. */
9497 tw32(MAC_RSS_HASH_KEY_0, 0x5f865437);
9498 tw32(MAC_RSS_HASH_KEY_1, 0xe4ac62cc);
9499 tw32(MAC_RSS_HASH_KEY_2, 0x50103a45);
9500 tw32(MAC_RSS_HASH_KEY_3, 0x36621985);
9501 tw32(MAC_RSS_HASH_KEY_4, 0xbf14c0e8);
9502 tw32(MAC_RSS_HASH_KEY_5, 0x1bc27a1e);
9503 tw32(MAC_RSS_HASH_KEY_6, 0x84f4b556);
9504 tw32(MAC_RSS_HASH_KEY_7, 0x094ea6fe);
9505 tw32(MAC_RSS_HASH_KEY_8, 0x7dda01e7);
9506 tw32(MAC_RSS_HASH_KEY_9, 0xc04d7481);
9507 }
9508
Linus Torvalds1da177e2005-04-16 15:20:36 -07009509 tp->rx_mode = RX_MODE_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00009510 if (tg3_flag(tp, 5755_PLUS))
Michael Chanaf36e6b2006-03-23 01:28:06 -08009511 tp->rx_mode |= RX_MODE_IPV6_CSUM_ENABLE;
9512
Joe Perches63c3a662011-04-26 08:12:10 +00009513 if (tg3_flag(tp, ENABLE_RSS))
Matt Carlsonbaf8a942009-09-01 13:13:00 +00009514 tp->rx_mode |= RX_MODE_RSS_ENABLE |
9515 RX_MODE_RSS_ITBL_HASH_BITS_7 |
9516 RX_MODE_RSS_IPV6_HASH_EN |
9517 RX_MODE_RSS_TCP_IPV6_HASH_EN |
9518 RX_MODE_RSS_IPV4_HASH_EN |
9519 RX_MODE_RSS_TCP_IPV4_HASH_EN;
9520
Linus Torvalds1da177e2005-04-16 15:20:36 -07009521 tw32_f(MAC_RX_MODE, tp->rx_mode);
9522 udelay(10);
9523
Linus Torvalds1da177e2005-04-16 15:20:36 -07009524 tw32(MAC_LED_CTRL, tp->led_ctrl);
9525
9526 tw32(MAC_MI_STAT, MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009527 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009528 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
9529 udelay(10);
9530 }
9531 tw32_f(MAC_RX_MODE, tp->rx_mode);
9532 udelay(10);
9533
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009534 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009535 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009536 !(tp->phy_flags & TG3_PHYFLG_SERDES_PREEMPHASIS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009537 /* Set drive transmission level to 1.2V */
9538 /* only if the signal pre-emphasis bit is not set */
9539 val = tr32(MAC_SERDES_CFG);
9540 val &= 0xfffff000;
9541 val |= 0x880;
9542 tw32(MAC_SERDES_CFG, val);
9543 }
9544 if (tp->pci_chip_rev_id == CHIPREV_ID_5703_A1)
9545 tw32(MAC_SERDES_CFG, 0x616000);
9546 }
9547
9548 /* Prevent chip from dropping frames when flow control
9549 * is enabled.
9550 */
Matt Carlson55086ad2011-12-14 11:09:59 +00009551 if (tg3_flag(tp, 57765_CLASS))
Matt Carlson666bc832010-01-20 16:58:03 +00009552 val = 1;
9553 else
9554 val = 2;
9555 tw32_f(MAC_LOW_WMARK_MAX_RX_FRAME, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009556
9557 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009558 (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009559 /* Use hardware link auto-negotiation */
Joe Perches63c3a662011-04-26 08:12:10 +00009560 tg3_flag_set(tp, HW_AUTONEG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009561 }
9562
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009563 if ((tp->phy_flags & TG3_PHYFLG_MII_SERDES) &&
Matt Carlson6ff6f812011-05-19 12:12:54 +00009564 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
Michael Chand4d2c552006-03-20 17:47:20 -08009565 u32 tmp;
9566
9567 tmp = tr32(SERDES_RX_CTRL);
9568 tw32(SERDES_RX_CTRL, tmp | SERDES_RX_SIG_DETECT);
9569 tp->grc_local_ctrl &= ~GRC_LCLCTRL_USE_EXT_SIG_DETECT;
9570 tp->grc_local_ctrl |= GRC_LCLCTRL_USE_SIG_DETECT;
9571 tw32(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
9572 }
9573
Joe Perches63c3a662011-04-26 08:12:10 +00009574 if (!tg3_flag(tp, USE_PHYLIB)) {
Matt Carlsonc6700ce2012-02-13 15:20:15 +00009575 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Matt Carlson800960682010-08-02 11:26:06 +00009576 tp->phy_flags &= ~TG3_PHYFLG_IS_LOW_POWER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009577
Matt Carlsondd477002008-05-25 23:45:58 -07009578 err = tg3_setup_phy(tp, 0);
9579 if (err)
9580 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009581
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009582 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
9583 !(tp->phy_flags & TG3_PHYFLG_IS_FET)) {
Matt Carlsondd477002008-05-25 23:45:58 -07009584 u32 tmp;
9585
9586 /* Clear CRC stats. */
9587 if (!tg3_readphy(tp, MII_TG3_TEST1, &tmp)) {
9588 tg3_writephy(tp, MII_TG3_TEST1,
9589 tmp | MII_TG3_TEST1_CRC_EN);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00009590 tg3_readphy(tp, MII_TG3_RXR_COUNTERS, &tmp);
Matt Carlsondd477002008-05-25 23:45:58 -07009591 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009592 }
9593 }
9594
9595 __tg3_set_rx_mode(tp->dev);
9596
9597 /* Initialize receive rules. */
9598 tw32(MAC_RCV_RULE_0, 0xc2000000 & RCV_RULE_DISABLE_MASK);
9599 tw32(MAC_RCV_VALUE_0, 0xffffffff & RCV_RULE_DISABLE_MASK);
9600 tw32(MAC_RCV_RULE_1, 0x86000004 & RCV_RULE_DISABLE_MASK);
9601 tw32(MAC_RCV_VALUE_1, 0xffffffff & RCV_RULE_DISABLE_MASK);
9602
Joe Perches63c3a662011-04-26 08:12:10 +00009603 if (tg3_flag(tp, 5705_PLUS) && !tg3_flag(tp, 5780_CLASS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009604 limit = 8;
9605 else
9606 limit = 16;
Joe Perches63c3a662011-04-26 08:12:10 +00009607 if (tg3_flag(tp, ENABLE_ASF))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009608 limit -= 4;
9609 switch (limit) {
9610 case 16:
9611 tw32(MAC_RCV_RULE_15, 0); tw32(MAC_RCV_VALUE_15, 0);
9612 case 15:
9613 tw32(MAC_RCV_RULE_14, 0); tw32(MAC_RCV_VALUE_14, 0);
9614 case 14:
9615 tw32(MAC_RCV_RULE_13, 0); tw32(MAC_RCV_VALUE_13, 0);
9616 case 13:
9617 tw32(MAC_RCV_RULE_12, 0); tw32(MAC_RCV_VALUE_12, 0);
9618 case 12:
9619 tw32(MAC_RCV_RULE_11, 0); tw32(MAC_RCV_VALUE_11, 0);
9620 case 11:
9621 tw32(MAC_RCV_RULE_10, 0); tw32(MAC_RCV_VALUE_10, 0);
9622 case 10:
9623 tw32(MAC_RCV_RULE_9, 0); tw32(MAC_RCV_VALUE_9, 0);
9624 case 9:
9625 tw32(MAC_RCV_RULE_8, 0); tw32(MAC_RCV_VALUE_8, 0);
9626 case 8:
9627 tw32(MAC_RCV_RULE_7, 0); tw32(MAC_RCV_VALUE_7, 0);
9628 case 7:
9629 tw32(MAC_RCV_RULE_6, 0); tw32(MAC_RCV_VALUE_6, 0);
9630 case 6:
9631 tw32(MAC_RCV_RULE_5, 0); tw32(MAC_RCV_VALUE_5, 0);
9632 case 5:
9633 tw32(MAC_RCV_RULE_4, 0); tw32(MAC_RCV_VALUE_4, 0);
9634 case 4:
9635 /* tw32(MAC_RCV_RULE_3, 0); tw32(MAC_RCV_VALUE_3, 0); */
9636 case 3:
9637 /* tw32(MAC_RCV_RULE_2, 0); tw32(MAC_RCV_VALUE_2, 0); */
9638 case 2:
9639 case 1:
9640
9641 default:
9642 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07009643 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009644
Joe Perches63c3a662011-04-26 08:12:10 +00009645 if (tg3_flag(tp, ENABLE_APE))
Matt Carlson9ce768e2007-10-11 19:49:11 -07009646 /* Write our heartbeat update interval to APE. */
9647 tg3_ape_write32(tp, TG3_APE_HOST_HEARTBEAT_INT_MS,
9648 APE_HOST_HEARTBEAT_INT_DISABLE);
Matt Carlson0d3031d2007-10-10 18:02:43 -07009649
Linus Torvalds1da177e2005-04-16 15:20:36 -07009650 tg3_write_sig_post_reset(tp, RESET_KIND_INIT);
9651
Linus Torvalds1da177e2005-04-16 15:20:36 -07009652 return 0;
9653}
9654
9655/* Called at device open time to get the chip ready for
9656 * packet processing. Invoked with tp->lock held.
9657 */
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07009658static int tg3_init_hw(struct tg3 *tp, int reset_phy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009659{
Linus Torvalds1da177e2005-04-16 15:20:36 -07009660 tg3_switch_clocks(tp);
9661
9662 tw32(TG3PCI_MEM_WIN_BASE_ADDR, 0);
9663
Matt Carlson2f751b62008-08-04 23:17:34 -07009664 return tg3_reset_hw(tp, reset_phy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009665}
9666
Michael Chanaed93e02012-07-16 16:24:02 +00009667static void tg3_sd_scan_scratchpad(struct tg3 *tp, struct tg3_ocir *ocir)
9668{
9669 int i;
9670
9671 for (i = 0; i < TG3_SD_NUM_RECS; i++, ocir++) {
9672 u32 off = i * TG3_OCIR_LEN, len = TG3_OCIR_LEN;
9673
9674 tg3_ape_scratchpad_read(tp, (u32 *) ocir, off, len);
9675 off += len;
9676
9677 if (ocir->signature != TG3_OCIR_SIG_MAGIC ||
9678 !(ocir->version_flags & TG3_OCIR_FLAG_ACTIVE))
9679 memset(ocir, 0, TG3_OCIR_LEN);
9680 }
9681}
9682
9683/* sysfs attributes for hwmon */
9684static ssize_t tg3_show_temp(struct device *dev,
9685 struct device_attribute *devattr, char *buf)
9686{
9687 struct pci_dev *pdev = to_pci_dev(dev);
9688 struct net_device *netdev = pci_get_drvdata(pdev);
9689 struct tg3 *tp = netdev_priv(netdev);
9690 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
9691 u32 temperature;
9692
9693 spin_lock_bh(&tp->lock);
9694 tg3_ape_scratchpad_read(tp, &temperature, attr->index,
9695 sizeof(temperature));
9696 spin_unlock_bh(&tp->lock);
9697 return sprintf(buf, "%u\n", temperature);
9698}
9699
9700
9701static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, tg3_show_temp, NULL,
9702 TG3_TEMP_SENSOR_OFFSET);
9703static SENSOR_DEVICE_ATTR(temp1_crit, S_IRUGO, tg3_show_temp, NULL,
9704 TG3_TEMP_CAUTION_OFFSET);
9705static SENSOR_DEVICE_ATTR(temp1_max, S_IRUGO, tg3_show_temp, NULL,
9706 TG3_TEMP_MAX_OFFSET);
9707
9708static struct attribute *tg3_attributes[] = {
9709 &sensor_dev_attr_temp1_input.dev_attr.attr,
9710 &sensor_dev_attr_temp1_crit.dev_attr.attr,
9711 &sensor_dev_attr_temp1_max.dev_attr.attr,
9712 NULL
9713};
9714
9715static const struct attribute_group tg3_group = {
9716 .attrs = tg3_attributes,
9717};
9718
Michael Chanaed93e02012-07-16 16:24:02 +00009719static void tg3_hwmon_close(struct tg3 *tp)
9720{
Michael Chanaed93e02012-07-16 16:24:02 +00009721 if (tp->hwmon_dev) {
9722 hwmon_device_unregister(tp->hwmon_dev);
9723 tp->hwmon_dev = NULL;
9724 sysfs_remove_group(&tp->pdev->dev.kobj, &tg3_group);
9725 }
Michael Chanaed93e02012-07-16 16:24:02 +00009726}
9727
9728static void tg3_hwmon_open(struct tg3 *tp)
9729{
Michael Chanaed93e02012-07-16 16:24:02 +00009730 int i, err;
9731 u32 size = 0;
9732 struct pci_dev *pdev = tp->pdev;
9733 struct tg3_ocir ocirs[TG3_SD_NUM_RECS];
9734
9735 tg3_sd_scan_scratchpad(tp, ocirs);
9736
9737 for (i = 0; i < TG3_SD_NUM_RECS; i++) {
9738 if (!ocirs[i].src_data_length)
9739 continue;
9740
9741 size += ocirs[i].src_hdr_length;
9742 size += ocirs[i].src_data_length;
9743 }
9744
9745 if (!size)
9746 return;
9747
9748 /* Register hwmon sysfs hooks */
9749 err = sysfs_create_group(&pdev->dev.kobj, &tg3_group);
9750 if (err) {
9751 dev_err(&pdev->dev, "Cannot create sysfs group, aborting\n");
9752 return;
9753 }
9754
9755 tp->hwmon_dev = hwmon_device_register(&pdev->dev);
9756 if (IS_ERR(tp->hwmon_dev)) {
9757 tp->hwmon_dev = NULL;
9758 dev_err(&pdev->dev, "Cannot register hwmon device, aborting\n");
9759 sysfs_remove_group(&pdev->dev.kobj, &tg3_group);
9760 }
Michael Chanaed93e02012-07-16 16:24:02 +00009761}
9762
9763
Linus Torvalds1da177e2005-04-16 15:20:36 -07009764#define TG3_STAT_ADD32(PSTAT, REG) \
9765do { u32 __val = tr32(REG); \
9766 (PSTAT)->low += __val; \
9767 if ((PSTAT)->low < __val) \
9768 (PSTAT)->high += 1; \
9769} while (0)
9770
9771static void tg3_periodic_fetch_stats(struct tg3 *tp)
9772{
9773 struct tg3_hw_stats *sp = tp->hw_stats;
9774
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00009775 if (!tp->link_up)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009776 return;
9777
9778 TG3_STAT_ADD32(&sp->tx_octets, MAC_TX_STATS_OCTETS);
9779 TG3_STAT_ADD32(&sp->tx_collisions, MAC_TX_STATS_COLLISIONS);
9780 TG3_STAT_ADD32(&sp->tx_xon_sent, MAC_TX_STATS_XON_SENT);
9781 TG3_STAT_ADD32(&sp->tx_xoff_sent, MAC_TX_STATS_XOFF_SENT);
9782 TG3_STAT_ADD32(&sp->tx_mac_errors, MAC_TX_STATS_MAC_ERRORS);
9783 TG3_STAT_ADD32(&sp->tx_single_collisions, MAC_TX_STATS_SINGLE_COLLISIONS);
9784 TG3_STAT_ADD32(&sp->tx_mult_collisions, MAC_TX_STATS_MULT_COLLISIONS);
9785 TG3_STAT_ADD32(&sp->tx_deferred, MAC_TX_STATS_DEFERRED);
9786 TG3_STAT_ADD32(&sp->tx_excessive_collisions, MAC_TX_STATS_EXCESSIVE_COL);
9787 TG3_STAT_ADD32(&sp->tx_late_collisions, MAC_TX_STATS_LATE_COL);
9788 TG3_STAT_ADD32(&sp->tx_ucast_packets, MAC_TX_STATS_UCAST);
9789 TG3_STAT_ADD32(&sp->tx_mcast_packets, MAC_TX_STATS_MCAST);
9790 TG3_STAT_ADD32(&sp->tx_bcast_packets, MAC_TX_STATS_BCAST);
Michael Chan091f0ea2012-07-29 19:15:43 +00009791 if (unlikely(tg3_flag(tp, 5719_RDMA_BUG) &&
9792 (sp->tx_ucast_packets.low + sp->tx_mcast_packets.low +
9793 sp->tx_bcast_packets.low) > TG3_NUM_RDMA_CHANNELS)) {
9794 u32 val;
9795
9796 val = tr32(TG3_LSO_RD_DMA_CRPTEN_CTRL);
9797 val &= ~TG3_LSO_RD_DMA_TX_LENGTH_WA;
9798 tw32(TG3_LSO_RD_DMA_CRPTEN_CTRL, val);
9799 tg3_flag_clear(tp, 5719_RDMA_BUG);
9800 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009801
9802 TG3_STAT_ADD32(&sp->rx_octets, MAC_RX_STATS_OCTETS);
9803 TG3_STAT_ADD32(&sp->rx_fragments, MAC_RX_STATS_FRAGMENTS);
9804 TG3_STAT_ADD32(&sp->rx_ucast_packets, MAC_RX_STATS_UCAST);
9805 TG3_STAT_ADD32(&sp->rx_mcast_packets, MAC_RX_STATS_MCAST);
9806 TG3_STAT_ADD32(&sp->rx_bcast_packets, MAC_RX_STATS_BCAST);
9807 TG3_STAT_ADD32(&sp->rx_fcs_errors, MAC_RX_STATS_FCS_ERRORS);
9808 TG3_STAT_ADD32(&sp->rx_align_errors, MAC_RX_STATS_ALIGN_ERRORS);
9809 TG3_STAT_ADD32(&sp->rx_xon_pause_rcvd, MAC_RX_STATS_XON_PAUSE_RECVD);
9810 TG3_STAT_ADD32(&sp->rx_xoff_pause_rcvd, MAC_RX_STATS_XOFF_PAUSE_RECVD);
9811 TG3_STAT_ADD32(&sp->rx_mac_ctrl_rcvd, MAC_RX_STATS_MAC_CTRL_RECVD);
9812 TG3_STAT_ADD32(&sp->rx_xoff_entered, MAC_RX_STATS_XOFF_ENTERED);
9813 TG3_STAT_ADD32(&sp->rx_frame_too_long_errors, MAC_RX_STATS_FRAME_TOO_LONG);
9814 TG3_STAT_ADD32(&sp->rx_jabbers, MAC_RX_STATS_JABBERS);
9815 TG3_STAT_ADD32(&sp->rx_undersize_packets, MAC_RX_STATS_UNDERSIZE);
Michael Chan463d3052006-05-22 16:36:27 -07009816
9817 TG3_STAT_ADD32(&sp->rxbds_empty, RCVLPC_NO_RCV_BD_CNT);
Matt Carlson310050f2011-05-19 12:12:55 +00009818 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717 &&
9819 tp->pci_chip_rev_id != CHIPREV_ID_5719_A0 &&
9820 tp->pci_chip_rev_id != CHIPREV_ID_5720_A0) {
Matt Carlson4d958472011-04-20 07:57:35 +00009821 TG3_STAT_ADD32(&sp->rx_discards, RCVLPC_IN_DISCARDS_CNT);
9822 } else {
9823 u32 val = tr32(HOSTCC_FLOW_ATTN);
9824 val = (val & HOSTCC_FLOW_ATTN_MBUF_LWM) ? 1 : 0;
9825 if (val) {
9826 tw32(HOSTCC_FLOW_ATTN, HOSTCC_FLOW_ATTN_MBUF_LWM);
9827 sp->rx_discards.low += val;
9828 if (sp->rx_discards.low < val)
9829 sp->rx_discards.high += 1;
9830 }
9831 sp->mbuf_lwm_thresh_hit = sp->rx_discards;
9832 }
Michael Chan463d3052006-05-22 16:36:27 -07009833 TG3_STAT_ADD32(&sp->rx_errors, RCVLPC_IN_ERRORS_CNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009834}
9835
Matt Carlson0e6cf6a2011-06-13 13:38:55 +00009836static void tg3_chk_missed_msi(struct tg3 *tp)
9837{
9838 u32 i;
9839
9840 for (i = 0; i < tp->irq_cnt; i++) {
9841 struct tg3_napi *tnapi = &tp->napi[i];
9842
9843 if (tg3_has_work(tnapi)) {
9844 if (tnapi->last_rx_cons == tnapi->rx_rcb_ptr &&
9845 tnapi->last_tx_cons == tnapi->tx_cons) {
9846 if (tnapi->chk_msi_cnt < 1) {
9847 tnapi->chk_msi_cnt++;
9848 return;
9849 }
Matt Carlson7f230732011-08-31 11:44:48 +00009850 tg3_msi(0, tnapi);
Matt Carlson0e6cf6a2011-06-13 13:38:55 +00009851 }
9852 }
9853 tnapi->chk_msi_cnt = 0;
9854 tnapi->last_rx_cons = tnapi->rx_rcb_ptr;
9855 tnapi->last_tx_cons = tnapi->tx_cons;
9856 }
9857}
9858
Linus Torvalds1da177e2005-04-16 15:20:36 -07009859static void tg3_timer(unsigned long __opaque)
9860{
9861 struct tg3 *tp = (struct tg3 *) __opaque;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009862
Matt Carlson5b190622011-11-04 09:15:04 +00009863 if (tp->irq_sync || tg3_flag(tp, RESET_TASK_PENDING))
Michael Chanf475f162006-03-27 23:20:14 -08009864 goto restart_timer;
9865
David S. Millerf47c11e2005-06-24 20:18:35 -07009866 spin_lock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009867
Matt Carlson0e6cf6a2011-06-13 13:38:55 +00009868 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
Matt Carlson55086ad2011-12-14 11:09:59 +00009869 tg3_flag(tp, 57765_CLASS))
Matt Carlson0e6cf6a2011-06-13 13:38:55 +00009870 tg3_chk_missed_msi(tp);
9871
Joe Perches63c3a662011-04-26 08:12:10 +00009872 if (!tg3_flag(tp, TAGGED_STATUS)) {
David S. Millerfac9b832005-05-18 22:46:34 -07009873 /* All of this garbage is because when using non-tagged
9874 * IRQ status the mailbox/status_block protocol the chip
9875 * uses with the cpu is race prone.
9876 */
Matt Carlson898a56f2009-08-28 14:02:40 +00009877 if (tp->napi[0].hw_status->status & SD_STATUS_UPDATED) {
David S. Millerfac9b832005-05-18 22:46:34 -07009878 tw32(GRC_LOCAL_CTRL,
9879 tp->grc_local_ctrl | GRC_LCLCTRL_SETINT);
9880 } else {
9881 tw32(HOSTCC_MODE, tp->coalesce_mode |
Matt Carlsonfd2ce372009-09-01 12:51:13 +00009882 HOSTCC_MODE_ENABLE | HOSTCC_MODE_NOW);
David S. Millerfac9b832005-05-18 22:46:34 -07009883 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009884
David S. Millerfac9b832005-05-18 22:46:34 -07009885 if (!(tr32(WDMAC_MODE) & WDMAC_MODE_ENABLE)) {
David S. Millerf47c11e2005-06-24 20:18:35 -07009886 spin_unlock(&tp->lock);
Matt Carlsondb219972011-11-04 09:15:03 +00009887 tg3_reset_task_schedule(tp);
Matt Carlson5b190622011-11-04 09:15:04 +00009888 goto restart_timer;
David S. Millerfac9b832005-05-18 22:46:34 -07009889 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009890 }
9891
Linus Torvalds1da177e2005-04-16 15:20:36 -07009892 /* This part only runs once per second. */
9893 if (!--tp->timer_counter) {
Joe Perches63c3a662011-04-26 08:12:10 +00009894 if (tg3_flag(tp, 5705_PLUS))
David S. Millerfac9b832005-05-18 22:46:34 -07009895 tg3_periodic_fetch_stats(tp);
9896
Matt Carlsonb0c59432011-05-19 12:12:48 +00009897 if (tp->setlpicnt && !--tp->setlpicnt)
9898 tg3_phy_eee_enable(tp);
Matt Carlson52b02d02010-10-14 10:37:41 +00009899
Joe Perches63c3a662011-04-26 08:12:10 +00009900 if (tg3_flag(tp, USE_LINKCHG_REG)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009901 u32 mac_stat;
9902 int phy_event;
9903
9904 mac_stat = tr32(MAC_STATUS);
9905
9906 phy_event = 0;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009907 if (tp->phy_flags & TG3_PHYFLG_USE_MI_INTERRUPT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009908 if (mac_stat & MAC_STATUS_MI_INTERRUPT)
9909 phy_event = 1;
9910 } else if (mac_stat & MAC_STATUS_LNKSTATE_CHANGED)
9911 phy_event = 1;
9912
9913 if (phy_event)
9914 tg3_setup_phy(tp, 0);
Joe Perches63c3a662011-04-26 08:12:10 +00009915 } else if (tg3_flag(tp, POLL_SERDES)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009916 u32 mac_stat = tr32(MAC_STATUS);
9917 int need_setup = 0;
9918
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00009919 if (tp->link_up &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07009920 (mac_stat & MAC_STATUS_LNKSTATE_CHANGED)) {
9921 need_setup = 1;
9922 }
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +00009923 if (!tp->link_up &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07009924 (mac_stat & (MAC_STATUS_PCS_SYNCED |
9925 MAC_STATUS_SIGNAL_DET))) {
9926 need_setup = 1;
9927 }
9928 if (need_setup) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07009929 if (!tp->serdes_counter) {
9930 tw32_f(MAC_MODE,
9931 (tp->mac_mode &
9932 ~MAC_MODE_PORT_MODE_MASK));
9933 udelay(40);
9934 tw32_f(MAC_MODE, tp->mac_mode);
9935 udelay(40);
9936 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009937 tg3_setup_phy(tp, 0);
9938 }
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009939 } else if ((tp->phy_flags & TG3_PHYFLG_MII_SERDES) &&
Joe Perches63c3a662011-04-26 08:12:10 +00009940 tg3_flag(tp, 5780_CLASS)) {
Michael Chan747e8f82005-07-25 12:33:22 -07009941 tg3_serdes_parallel_detect(tp);
Matt Carlson57d8b882010-06-05 17:24:35 +00009942 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009943
9944 tp->timer_counter = tp->timer_multiplier;
9945 }
9946
Michael Chan130b8e42006-09-27 16:00:40 -07009947 /* Heartbeat is only sent once every 2 seconds.
9948 *
9949 * The heartbeat is to tell the ASF firmware that the host
9950 * driver is still alive. In the event that the OS crashes,
9951 * ASF needs to reset the hardware to free up the FIFO space
9952 * that may be filled with rx packets destined for the host.
9953 * If the FIFO is full, ASF will no longer function properly.
9954 *
9955 * Unintended resets have been reported on real time kernels
9956 * where the timer doesn't run on time. Netpoll will also have
9957 * same problem.
9958 *
9959 * The new FWCMD_NICDRV_ALIVE3 command tells the ASF firmware
9960 * to check the ring condition when the heartbeat is expiring
9961 * before doing the reset. This will prevent most unintended
9962 * resets.
9963 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07009964 if (!--tp->asf_counter) {
Joe Perches63c3a662011-04-26 08:12:10 +00009965 if (tg3_flag(tp, ENABLE_ASF) && !tg3_flag(tp, ENABLE_APE)) {
Matt Carlson7c5026a2008-05-02 16:49:29 -07009966 tg3_wait_for_event_ack(tp);
9967
Michael Chanbbadf502006-04-06 21:46:34 -07009968 tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX,
Michael Chan130b8e42006-09-27 16:00:40 -07009969 FWCMD_NICDRV_ALIVE3);
Michael Chanbbadf502006-04-06 21:46:34 -07009970 tg3_write_mem(tp, NIC_SRAM_FW_CMD_LEN_MBOX, 4);
Matt Carlsonc6cdf432010-04-05 10:19:26 +00009971 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX,
9972 TG3_FW_UPDATE_TIMEOUT_SEC);
Matt Carlson4ba526c2008-08-15 14:10:04 -07009973
9974 tg3_generate_fw_event(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009975 }
9976 tp->asf_counter = tp->asf_multiplier;
9977 }
9978
David S. Millerf47c11e2005-06-24 20:18:35 -07009979 spin_unlock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009980
Michael Chanf475f162006-03-27 23:20:14 -08009981restart_timer:
Linus Torvalds1da177e2005-04-16 15:20:36 -07009982 tp->timer.expires = jiffies + tp->timer_offset;
9983 add_timer(&tp->timer);
9984}
9985
Bill Pemberton229b1ad2012-12-03 09:22:59 -05009986static void tg3_timer_init(struct tg3 *tp)
Matt Carlson21f76382012-02-22 12:35:21 +00009987{
9988 if (tg3_flag(tp, TAGGED_STATUS) &&
9989 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717 &&
9990 !tg3_flag(tp, 57765_CLASS))
9991 tp->timer_offset = HZ;
9992 else
9993 tp->timer_offset = HZ / 10;
9994
9995 BUG_ON(tp->timer_offset > HZ);
9996
9997 tp->timer_multiplier = (HZ / tp->timer_offset);
9998 tp->asf_multiplier = (HZ / tp->timer_offset) *
9999 TG3_FW_UPDATE_FREQ_SEC;
10000
10001 init_timer(&tp->timer);
10002 tp->timer.data = (unsigned long) tp;
10003 tp->timer.function = tg3_timer;
10004}
10005
10006static void tg3_timer_start(struct tg3 *tp)
10007{
10008 tp->asf_counter = tp->asf_multiplier;
10009 tp->timer_counter = tp->timer_multiplier;
10010
10011 tp->timer.expires = jiffies + tp->timer_offset;
10012 add_timer(&tp->timer);
10013}
10014
10015static void tg3_timer_stop(struct tg3 *tp)
10016{
10017 del_timer_sync(&tp->timer);
10018}
10019
10020/* Restart hardware after configuration changes, self-test, etc.
10021 * Invoked with tp->lock held.
10022 */
10023static int tg3_restart_hw(struct tg3 *tp, int reset_phy)
10024 __releases(tp->lock)
10025 __acquires(tp->lock)
10026{
10027 int err;
10028
10029 err = tg3_init_hw(tp, reset_phy);
10030 if (err) {
10031 netdev_err(tp->dev,
10032 "Failed to re-initialize device, aborting\n");
10033 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
10034 tg3_full_unlock(tp);
10035 tg3_timer_stop(tp);
10036 tp->irq_sync = 0;
10037 tg3_napi_enable(tp);
10038 dev_close(tp->dev);
10039 tg3_full_lock(tp, 0);
10040 }
10041 return err;
10042}
10043
10044static void tg3_reset_task(struct work_struct *work)
10045{
10046 struct tg3 *tp = container_of(work, struct tg3, reset_task);
10047 int err;
10048
10049 tg3_full_lock(tp, 0);
10050
10051 if (!netif_running(tp->dev)) {
10052 tg3_flag_clear(tp, RESET_TASK_PENDING);
10053 tg3_full_unlock(tp);
10054 return;
10055 }
10056
10057 tg3_full_unlock(tp);
10058
10059 tg3_phy_stop(tp);
10060
10061 tg3_netif_stop(tp);
10062
10063 tg3_full_lock(tp, 1);
10064
10065 if (tg3_flag(tp, TX_RECOVERY_PENDING)) {
10066 tp->write32_tx_mbox = tg3_write32_tx_mbox;
10067 tp->write32_rx_mbox = tg3_write_flush_reg32;
10068 tg3_flag_set(tp, MBOX_WRITE_REORDER);
10069 tg3_flag_clear(tp, TX_RECOVERY_PENDING);
10070 }
10071
10072 tg3_halt(tp, RESET_KIND_SHUTDOWN, 0);
10073 err = tg3_init_hw(tp, 1);
10074 if (err)
10075 goto out;
10076
10077 tg3_netif_start(tp);
10078
10079out:
10080 tg3_full_unlock(tp);
10081
10082 if (!err)
10083 tg3_phy_start(tp);
10084
10085 tg3_flag_clear(tp, RESET_TASK_PENDING);
10086}
10087
Matt Carlson4f125f42009-09-01 12:55:02 +000010088static int tg3_request_irq(struct tg3 *tp, int irq_num)
Michael Chanfcfa0a32006-03-20 22:28:41 -080010089{
David Howells7d12e782006-10-05 14:55:46 +010010090 irq_handler_t fn;
Michael Chanfcfa0a32006-03-20 22:28:41 -080010091 unsigned long flags;
Matt Carlson4f125f42009-09-01 12:55:02 +000010092 char *name;
10093 struct tg3_napi *tnapi = &tp->napi[irq_num];
10094
10095 if (tp->irq_cnt == 1)
10096 name = tp->dev->name;
10097 else {
10098 name = &tnapi->irq_lbl[0];
10099 snprintf(name, IFNAMSIZ, "%s-%d", tp->dev->name, irq_num);
10100 name[IFNAMSIZ-1] = 0;
10101 }
Michael Chanfcfa0a32006-03-20 22:28:41 -080010102
Joe Perches63c3a662011-04-26 08:12:10 +000010103 if (tg3_flag(tp, USING_MSI) || tg3_flag(tp, USING_MSIX)) {
Michael Chanfcfa0a32006-03-20 22:28:41 -080010104 fn = tg3_msi;
Joe Perches63c3a662011-04-26 08:12:10 +000010105 if (tg3_flag(tp, 1SHOT_MSI))
Michael Chanfcfa0a32006-03-20 22:28:41 -080010106 fn = tg3_msi_1shot;
Javier Martinez Canillasab392d22011-03-28 16:27:31 +000010107 flags = 0;
Michael Chanfcfa0a32006-03-20 22:28:41 -080010108 } else {
10109 fn = tg3_interrupt;
Joe Perches63c3a662011-04-26 08:12:10 +000010110 if (tg3_flag(tp, TAGGED_STATUS))
Michael Chanfcfa0a32006-03-20 22:28:41 -080010111 fn = tg3_interrupt_tagged;
Javier Martinez Canillasab392d22011-03-28 16:27:31 +000010112 flags = IRQF_SHARED;
Michael Chanfcfa0a32006-03-20 22:28:41 -080010113 }
Matt Carlson4f125f42009-09-01 12:55:02 +000010114
10115 return request_irq(tnapi->irq_vec, fn, flags, name, tnapi);
Michael Chanfcfa0a32006-03-20 22:28:41 -080010116}
10117
Michael Chan79381092005-04-21 17:13:59 -070010118static int tg3_test_interrupt(struct tg3 *tp)
10119{
Matt Carlson09943a12009-08-28 14:01:57 +000010120 struct tg3_napi *tnapi = &tp->napi[0];
Michael Chan79381092005-04-21 17:13:59 -070010121 struct net_device *dev = tp->dev;
Michael Chanb16250e2006-09-27 16:10:14 -070010122 int err, i, intr_ok = 0;
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000010123 u32 val;
Michael Chan79381092005-04-21 17:13:59 -070010124
Michael Chand4bc3922005-05-29 14:59:20 -070010125 if (!netif_running(dev))
10126 return -ENODEV;
10127
Michael Chan79381092005-04-21 17:13:59 -070010128 tg3_disable_ints(tp);
10129
Matt Carlson4f125f42009-09-01 12:55:02 +000010130 free_irq(tnapi->irq_vec, tnapi);
Michael Chan79381092005-04-21 17:13:59 -070010131
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000010132 /*
10133 * Turn off MSI one shot mode. Otherwise this test has no
10134 * observable way to know whether the interrupt was delivered.
10135 */
Matt Carlson3aa1cdf2011-07-20 10:20:55 +000010136 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000010137 val = tr32(MSGINT_MODE) | MSGINT_MODE_ONE_SHOT_DISABLE;
10138 tw32(MSGINT_MODE, val);
10139 }
10140
Matt Carlson4f125f42009-09-01 12:55:02 +000010141 err = request_irq(tnapi->irq_vec, tg3_test_isr,
Davidlohr Buesof274fd92012-02-22 03:06:54 +000010142 IRQF_SHARED, dev->name, tnapi);
Michael Chan79381092005-04-21 17:13:59 -070010143 if (err)
10144 return err;
10145
Matt Carlson898a56f2009-08-28 14:02:40 +000010146 tnapi->hw_status->status &= ~SD_STATUS_UPDATED;
Michael Chan79381092005-04-21 17:13:59 -070010147 tg3_enable_ints(tp);
10148
10149 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +000010150 tnapi->coal_now);
Michael Chan79381092005-04-21 17:13:59 -070010151
10152 for (i = 0; i < 5; i++) {
Michael Chanb16250e2006-09-27 16:10:14 -070010153 u32 int_mbox, misc_host_ctrl;
10154
Matt Carlson898a56f2009-08-28 14:02:40 +000010155 int_mbox = tr32_mailbox(tnapi->int_mbox);
Michael Chanb16250e2006-09-27 16:10:14 -070010156 misc_host_ctrl = tr32(TG3PCI_MISC_HOST_CTRL);
10157
10158 if ((int_mbox != 0) ||
10159 (misc_host_ctrl & MISC_HOST_CTRL_MASK_PCI_INT)) {
10160 intr_ok = 1;
Michael Chan79381092005-04-21 17:13:59 -070010161 break;
Michael Chanb16250e2006-09-27 16:10:14 -070010162 }
10163
Matt Carlson3aa1cdf2011-07-20 10:20:55 +000010164 if (tg3_flag(tp, 57765_PLUS) &&
10165 tnapi->hw_status->status_tag != tnapi->last_tag)
10166 tw32_mailbox_f(tnapi->int_mbox, tnapi->last_tag << 24);
10167
Michael Chan79381092005-04-21 17:13:59 -070010168 msleep(10);
10169 }
10170
10171 tg3_disable_ints(tp);
10172
Matt Carlson4f125f42009-09-01 12:55:02 +000010173 free_irq(tnapi->irq_vec, tnapi);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010174
Matt Carlson4f125f42009-09-01 12:55:02 +000010175 err = tg3_request_irq(tp, 0);
Michael Chan79381092005-04-21 17:13:59 -070010176
10177 if (err)
10178 return err;
10179
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000010180 if (intr_ok) {
10181 /* Reenable MSI one shot mode. */
Matt Carlson5b39de92011-08-31 11:44:50 +000010182 if (tg3_flag(tp, 57765_PLUS) && tg3_flag(tp, 1SHOT_MSI)) {
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000010183 val = tr32(MSGINT_MODE) & ~MSGINT_MODE_ONE_SHOT_DISABLE;
10184 tw32(MSGINT_MODE, val);
10185 }
Michael Chan79381092005-04-21 17:13:59 -070010186 return 0;
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000010187 }
Michael Chan79381092005-04-21 17:13:59 -070010188
10189 return -EIO;
10190}
10191
10192/* Returns 0 if MSI test succeeds or MSI test fails and INTx mode is
10193 * successfully restored
10194 */
10195static int tg3_test_msi(struct tg3 *tp)
10196{
Michael Chan79381092005-04-21 17:13:59 -070010197 int err;
10198 u16 pci_cmd;
10199
Joe Perches63c3a662011-04-26 08:12:10 +000010200 if (!tg3_flag(tp, USING_MSI))
Michael Chan79381092005-04-21 17:13:59 -070010201 return 0;
10202
10203 /* Turn off SERR reporting in case MSI terminates with Master
10204 * Abort.
10205 */
10206 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
10207 pci_write_config_word(tp->pdev, PCI_COMMAND,
10208 pci_cmd & ~PCI_COMMAND_SERR);
10209
10210 err = tg3_test_interrupt(tp);
10211
10212 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
10213
10214 if (!err)
10215 return 0;
10216
10217 /* other failures */
10218 if (err != -EIO)
10219 return err;
10220
10221 /* MSI test failed, go back to INTx mode */
Matt Carlson5129c3a2010-04-05 10:19:23 +000010222 netdev_warn(tp->dev, "No interrupt was generated using MSI. Switching "
10223 "to INTx mode. Please report this failure to the PCI "
10224 "maintainer and include system chipset information\n");
Michael Chan79381092005-04-21 17:13:59 -070010225
Matt Carlson4f125f42009-09-01 12:55:02 +000010226 free_irq(tp->napi[0].irq_vec, &tp->napi[0]);
Matt Carlson09943a12009-08-28 14:01:57 +000010227
Michael Chan79381092005-04-21 17:13:59 -070010228 pci_disable_msi(tp->pdev);
10229
Joe Perches63c3a662011-04-26 08:12:10 +000010230 tg3_flag_clear(tp, USING_MSI);
Andre Detschdc8bf1b2010-04-26 07:27:07 +000010231 tp->napi[0].irq_vec = tp->pdev->irq;
Michael Chan79381092005-04-21 17:13:59 -070010232
Matt Carlson4f125f42009-09-01 12:55:02 +000010233 err = tg3_request_irq(tp, 0);
Michael Chan79381092005-04-21 17:13:59 -070010234 if (err)
10235 return err;
10236
10237 /* Need to reset the chip because the MSI cycle may have terminated
10238 * with Master Abort.
10239 */
David S. Millerf47c11e2005-06-24 20:18:35 -070010240 tg3_full_lock(tp, 1);
Michael Chan79381092005-04-21 17:13:59 -070010241
Michael Chan944d9802005-05-29 14:57:48 -070010242 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Gary Zambrano8e7a22e2006-04-29 18:59:13 -070010243 err = tg3_init_hw(tp, 1);
Michael Chan79381092005-04-21 17:13:59 -070010244
David S. Millerf47c11e2005-06-24 20:18:35 -070010245 tg3_full_unlock(tp);
Michael Chan79381092005-04-21 17:13:59 -070010246
10247 if (err)
Matt Carlson4f125f42009-09-01 12:55:02 +000010248 free_irq(tp->napi[0].irq_vec, &tp->napi[0]);
Michael Chan79381092005-04-21 17:13:59 -070010249
10250 return err;
10251}
10252
Matt Carlson9e9fd122009-01-19 16:57:45 -080010253static int tg3_request_firmware(struct tg3 *tp)
10254{
10255 const __be32 *fw_data;
10256
10257 if (request_firmware(&tp->fw, tp->fw_needed, &tp->pdev->dev)) {
Joe Perches05dbe002010-02-17 19:44:19 +000010258 netdev_err(tp->dev, "Failed to load firmware \"%s\"\n",
10259 tp->fw_needed);
Matt Carlson9e9fd122009-01-19 16:57:45 -080010260 return -ENOENT;
10261 }
10262
10263 fw_data = (void *)tp->fw->data;
10264
10265 /* Firmware blob starts with version numbers, followed by
10266 * start address and _full_ length including BSS sections
10267 * (which must be longer than the actual data, of course
10268 */
10269
10270 tp->fw_len = be32_to_cpu(fw_data[2]); /* includes bss */
10271 if (tp->fw_len < (tp->fw->size - 12)) {
Joe Perches05dbe002010-02-17 19:44:19 +000010272 netdev_err(tp->dev, "bogus length %d in \"%s\"\n",
10273 tp->fw_len, tp->fw_needed);
Matt Carlson9e9fd122009-01-19 16:57:45 -080010274 release_firmware(tp->fw);
10275 tp->fw = NULL;
10276 return -EINVAL;
10277 }
10278
10279 /* We no longer need firmware; we have it. */
10280 tp->fw_needed = NULL;
10281 return 0;
10282}
10283
Michael Chan91024262012-09-28 07:12:38 +000010284static u32 tg3_irq_count(struct tg3 *tp)
Matt Carlson679563f2009-09-01 12:55:46 +000010285{
Michael Chan91024262012-09-28 07:12:38 +000010286 u32 irq_cnt = max(tp->rxq_cnt, tp->txq_cnt);
Matt Carlson679563f2009-09-01 12:55:46 +000010287
Michael Chan91024262012-09-28 07:12:38 +000010288 if (irq_cnt > 1) {
Matt Carlsonc3b50032012-01-17 15:27:23 +000010289 /* We want as many rx rings enabled as there are cpus.
10290 * In multiqueue MSI-X mode, the first MSI-X vector
10291 * only deals with link interrupts, etc, so we add
10292 * one to the number of vectors we are requesting.
10293 */
Michael Chan91024262012-09-28 07:12:38 +000010294 irq_cnt = min_t(unsigned, irq_cnt + 1, tp->irq_max);
Matt Carlsonc3b50032012-01-17 15:27:23 +000010295 }
Matt Carlson679563f2009-09-01 12:55:46 +000010296
Michael Chan91024262012-09-28 07:12:38 +000010297 return irq_cnt;
10298}
10299
10300static bool tg3_enable_msix(struct tg3 *tp)
10301{
10302 int i, rc;
Michael Chan86449942012-10-02 20:31:14 -070010303 struct msix_entry msix_ent[TG3_IRQ_MAX_VECS];
Michael Chan91024262012-09-28 07:12:38 +000010304
Michael Chan09681692012-09-28 07:12:42 +000010305 tp->txq_cnt = tp->txq_req;
10306 tp->rxq_cnt = tp->rxq_req;
10307 if (!tp->rxq_cnt)
10308 tp->rxq_cnt = netif_get_num_default_rss_queues();
Michael Chan91024262012-09-28 07:12:38 +000010309 if (tp->rxq_cnt > tp->rxq_max)
10310 tp->rxq_cnt = tp->rxq_max;
Michael Chancf6d6ea2012-09-28 07:12:43 +000010311
10312 /* Disable multiple TX rings by default. Simple round-robin hardware
10313 * scheduling of the TX rings can cause starvation of rings with
10314 * small packets when other rings have TSO or jumbo packets.
10315 */
10316 if (!tp->txq_req)
10317 tp->txq_cnt = 1;
Michael Chan91024262012-09-28 07:12:38 +000010318
10319 tp->irq_cnt = tg3_irq_count(tp);
10320
Matt Carlson679563f2009-09-01 12:55:46 +000010321 for (i = 0; i < tp->irq_max; i++) {
10322 msix_ent[i].entry = i;
10323 msix_ent[i].vector = 0;
10324 }
10325
10326 rc = pci_enable_msix(tp->pdev, msix_ent, tp->irq_cnt);
Matt Carlson2430b032010-06-05 17:24:34 +000010327 if (rc < 0) {
10328 return false;
10329 } else if (rc != 0) {
Matt Carlson679563f2009-09-01 12:55:46 +000010330 if (pci_enable_msix(tp->pdev, msix_ent, rc))
10331 return false;
Joe Perches05dbe002010-02-17 19:44:19 +000010332 netdev_notice(tp->dev, "Requested %d MSI-X vectors, received %d\n",
10333 tp->irq_cnt, rc);
Matt Carlson679563f2009-09-01 12:55:46 +000010334 tp->irq_cnt = rc;
Michael Chan49a359e2012-09-28 07:12:37 +000010335 tp->rxq_cnt = max(rc - 1, 1);
Michael Chan91024262012-09-28 07:12:38 +000010336 if (tp->txq_cnt)
10337 tp->txq_cnt = min(tp->rxq_cnt, tp->txq_max);
Matt Carlson679563f2009-09-01 12:55:46 +000010338 }
10339
10340 for (i = 0; i < tp->irq_max; i++)
10341 tp->napi[i].irq_vec = msix_ent[i].vector;
10342
Michael Chan49a359e2012-09-28 07:12:37 +000010343 if (netif_set_real_num_rx_queues(tp->dev, tp->rxq_cnt)) {
Ben Hutchings2ddaad32010-09-27 22:11:51 -070010344 pci_disable_msix(tp->pdev);
10345 return false;
10346 }
Matt Carlsonb92b9042010-11-24 08:31:51 +000010347
Michael Chan91024262012-09-28 07:12:38 +000010348 if (tp->irq_cnt == 1)
10349 return true;
Matt Carlsond78b59f2011-04-05 14:22:46 +000010350
Michael Chan91024262012-09-28 07:12:38 +000010351 tg3_flag_set(tp, ENABLE_RSS);
10352
10353 if (tp->txq_cnt > 1)
10354 tg3_flag_set(tp, ENABLE_TSS);
10355
10356 netif_set_real_num_tx_queues(tp->dev, tp->txq_cnt);
Matt Carlson2430b032010-06-05 17:24:34 +000010357
Matt Carlson679563f2009-09-01 12:55:46 +000010358 return true;
10359}
10360
Matt Carlson07b01732009-08-28 14:01:15 +000010361static void tg3_ints_init(struct tg3 *tp)
10362{
Joe Perches63c3a662011-04-26 08:12:10 +000010363 if ((tg3_flag(tp, SUPPORT_MSI) || tg3_flag(tp, SUPPORT_MSIX)) &&
10364 !tg3_flag(tp, TAGGED_STATUS)) {
Matt Carlson07b01732009-08-28 14:01:15 +000010365 /* All MSI supporting chips should support tagged
10366 * status. Assert that this is the case.
10367 */
Matt Carlson5129c3a2010-04-05 10:19:23 +000010368 netdev_warn(tp->dev,
10369 "MSI without TAGGED_STATUS? Not using MSI\n");
Matt Carlson679563f2009-09-01 12:55:46 +000010370 goto defcfg;
Matt Carlson07b01732009-08-28 14:01:15 +000010371 }
Matt Carlson4f125f42009-09-01 12:55:02 +000010372
Joe Perches63c3a662011-04-26 08:12:10 +000010373 if (tg3_flag(tp, SUPPORT_MSIX) && tg3_enable_msix(tp))
10374 tg3_flag_set(tp, USING_MSIX);
10375 else if (tg3_flag(tp, SUPPORT_MSI) && pci_enable_msi(tp->pdev) == 0)
10376 tg3_flag_set(tp, USING_MSI);
Matt Carlson679563f2009-09-01 12:55:46 +000010377
Joe Perches63c3a662011-04-26 08:12:10 +000010378 if (tg3_flag(tp, USING_MSI) || tg3_flag(tp, USING_MSIX)) {
Matt Carlson679563f2009-09-01 12:55:46 +000010379 u32 msi_mode = tr32(MSGINT_MODE);
Joe Perches63c3a662011-04-26 08:12:10 +000010380 if (tg3_flag(tp, USING_MSIX) && tp->irq_cnt > 1)
Matt Carlsonbaf8a942009-09-01 13:13:00 +000010381 msi_mode |= MSGINT_MODE_MULTIVEC_EN;
Matt Carlson5b39de92011-08-31 11:44:50 +000010382 if (!tg3_flag(tp, 1SHOT_MSI))
10383 msi_mode |= MSGINT_MODE_ONE_SHOT_DISABLE;
Matt Carlson679563f2009-09-01 12:55:46 +000010384 tw32(MSGINT_MODE, msi_mode | MSGINT_MODE_ENABLE);
10385 }
10386defcfg:
Joe Perches63c3a662011-04-26 08:12:10 +000010387 if (!tg3_flag(tp, USING_MSIX)) {
Matt Carlson679563f2009-09-01 12:55:46 +000010388 tp->irq_cnt = 1;
10389 tp->napi[0].irq_vec = tp->pdev->irq;
Michael Chan49a359e2012-09-28 07:12:37 +000010390 }
10391
10392 if (tp->irq_cnt == 1) {
10393 tp->txq_cnt = 1;
10394 tp->rxq_cnt = 1;
Ben Hutchings2ddaad32010-09-27 22:11:51 -070010395 netif_set_real_num_tx_queues(tp->dev, 1);
Matt Carlson85407882010-10-06 13:40:58 -070010396 netif_set_real_num_rx_queues(tp->dev, 1);
Matt Carlson679563f2009-09-01 12:55:46 +000010397 }
Matt Carlson07b01732009-08-28 14:01:15 +000010398}
10399
10400static void tg3_ints_fini(struct tg3 *tp)
10401{
Joe Perches63c3a662011-04-26 08:12:10 +000010402 if (tg3_flag(tp, USING_MSIX))
Matt Carlson679563f2009-09-01 12:55:46 +000010403 pci_disable_msix(tp->pdev);
Joe Perches63c3a662011-04-26 08:12:10 +000010404 else if (tg3_flag(tp, USING_MSI))
Matt Carlson679563f2009-09-01 12:55:46 +000010405 pci_disable_msi(tp->pdev);
Joe Perches63c3a662011-04-26 08:12:10 +000010406 tg3_flag_clear(tp, USING_MSI);
10407 tg3_flag_clear(tp, USING_MSIX);
10408 tg3_flag_clear(tp, ENABLE_RSS);
10409 tg3_flag_clear(tp, ENABLE_TSS);
Matt Carlson07b01732009-08-28 14:01:15 +000010410}
10411
Matt Carlsonbe947302012-12-03 19:36:57 +000010412static int tg3_start(struct tg3 *tp, bool reset_phy, bool test_irq,
10413 bool init)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010414{
Michael Chand8f4cd32012-09-28 07:12:40 +000010415 struct net_device *dev = tp->dev;
Matt Carlson4f125f42009-09-01 12:55:02 +000010416 int i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010417
Matt Carlson679563f2009-09-01 12:55:46 +000010418 /*
10419 * Setup interrupts first so we know how
10420 * many NAPI resources to allocate
10421 */
10422 tg3_ints_init(tp);
10423
Matt Carlson90415472011-12-16 13:33:23 +000010424 tg3_rss_check_indir_tbl(tp);
Matt Carlsonbcebcc42011-12-14 11:10:01 +000010425
Linus Torvalds1da177e2005-04-16 15:20:36 -070010426 /* The placement of this call is tied
10427 * to the setup and use of Host TX descriptors.
10428 */
10429 err = tg3_alloc_consistent(tp);
10430 if (err)
Matt Carlson679563f2009-09-01 12:55:46 +000010431 goto err_out1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010432
Matt Carlson66cfd1b2010-09-30 10:34:30 +000010433 tg3_napi_init(tp);
10434
Matt Carlsonfed97812009-09-01 13:10:19 +000010435 tg3_napi_enable(tp);
Stephen Hemmingerbea33482007-10-03 16:41:36 -070010436
Matt Carlson4f125f42009-09-01 12:55:02 +000010437 for (i = 0; i < tp->irq_cnt; i++) {
10438 struct tg3_napi *tnapi = &tp->napi[i];
10439 err = tg3_request_irq(tp, i);
10440 if (err) {
Matt Carlson5bc09182011-11-04 09:15:01 +000010441 for (i--; i >= 0; i--) {
10442 tnapi = &tp->napi[i];
Matt Carlson4f125f42009-09-01 12:55:02 +000010443 free_irq(tnapi->irq_vec, tnapi);
Matt Carlson5bc09182011-11-04 09:15:01 +000010444 }
10445 goto err_out2;
Matt Carlson4f125f42009-09-01 12:55:02 +000010446 }
10447 }
Matt Carlson07b01732009-08-28 14:01:15 +000010448
David S. Millerf47c11e2005-06-24 20:18:35 -070010449 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010450
Michael Chand8f4cd32012-09-28 07:12:40 +000010451 err = tg3_init_hw(tp, reset_phy);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010452 if (err) {
Michael Chan944d9802005-05-29 14:57:48 -070010453 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010454 tg3_free_rings(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010455 }
10456
David S. Millerf47c11e2005-06-24 20:18:35 -070010457 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010458
Matt Carlson07b01732009-08-28 14:01:15 +000010459 if (err)
Matt Carlson679563f2009-09-01 12:55:46 +000010460 goto err_out3;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010461
Michael Chand8f4cd32012-09-28 07:12:40 +000010462 if (test_irq && tg3_flag(tp, USING_MSI)) {
Michael Chan79381092005-04-21 17:13:59 -070010463 err = tg3_test_msi(tp);
David S. Millerfac9b832005-05-18 22:46:34 -070010464
Michael Chan79381092005-04-21 17:13:59 -070010465 if (err) {
David S. Millerf47c11e2005-06-24 20:18:35 -070010466 tg3_full_lock(tp, 0);
Michael Chan944d9802005-05-29 14:57:48 -070010467 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Michael Chan79381092005-04-21 17:13:59 -070010468 tg3_free_rings(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -070010469 tg3_full_unlock(tp);
Michael Chan79381092005-04-21 17:13:59 -070010470
Matt Carlson679563f2009-09-01 12:55:46 +000010471 goto err_out2;
Michael Chan79381092005-04-21 17:13:59 -070010472 }
Michael Chanfcfa0a32006-03-20 22:28:41 -080010473
Joe Perches63c3a662011-04-26 08:12:10 +000010474 if (!tg3_flag(tp, 57765_PLUS) && tg3_flag(tp, USING_MSI)) {
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000010475 u32 val = tr32(PCIE_TRANSACTION_CFG);
Michael Chanfcfa0a32006-03-20 22:28:41 -080010476
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000010477 tw32(PCIE_TRANSACTION_CFG,
10478 val | PCIE_TRANS_CFG_1SHOT_MSI);
Michael Chanfcfa0a32006-03-20 22:28:41 -080010479 }
Michael Chan79381092005-04-21 17:13:59 -070010480 }
10481
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010482 tg3_phy_start(tp);
10483
Michael Chanaed93e02012-07-16 16:24:02 +000010484 tg3_hwmon_open(tp);
10485
David S. Millerf47c11e2005-06-24 20:18:35 -070010486 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010487
Matt Carlson21f76382012-02-22 12:35:21 +000010488 tg3_timer_start(tp);
Joe Perches63c3a662011-04-26 08:12:10 +000010489 tg3_flag_set(tp, INIT_COMPLETE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010490 tg3_enable_ints(tp);
10491
Matt Carlsonbe947302012-12-03 19:36:57 +000010492 if (init)
10493 tg3_ptp_init(tp);
10494 else
10495 tg3_ptp_resume(tp);
10496
10497
David S. Millerf47c11e2005-06-24 20:18:35 -070010498 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010499
Matt Carlsonfe5f5782009-09-01 13:09:39 +000010500 netif_tx_start_all_queues(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010501
Mahesh Bandewar06c03c02011-05-08 06:51:48 +000010502 /*
10503 * Reset loopback feature if it was turned on while the device was down
10504 * make sure that it's installed properly now.
10505 */
10506 if (dev->features & NETIF_F_LOOPBACK)
10507 tg3_set_loopback(dev, dev->features);
10508
Linus Torvalds1da177e2005-04-16 15:20:36 -070010509 return 0;
Matt Carlson07b01732009-08-28 14:01:15 +000010510
Matt Carlson679563f2009-09-01 12:55:46 +000010511err_out3:
Matt Carlson4f125f42009-09-01 12:55:02 +000010512 for (i = tp->irq_cnt - 1; i >= 0; i--) {
10513 struct tg3_napi *tnapi = &tp->napi[i];
10514 free_irq(tnapi->irq_vec, tnapi);
10515 }
Matt Carlson07b01732009-08-28 14:01:15 +000010516
Matt Carlson679563f2009-09-01 12:55:46 +000010517err_out2:
Matt Carlsonfed97812009-09-01 13:10:19 +000010518 tg3_napi_disable(tp);
Matt Carlson66cfd1b2010-09-30 10:34:30 +000010519 tg3_napi_fini(tp);
Matt Carlson07b01732009-08-28 14:01:15 +000010520 tg3_free_consistent(tp);
Matt Carlson679563f2009-09-01 12:55:46 +000010521
10522err_out1:
10523 tg3_ints_fini(tp);
Michael Chand8f4cd32012-09-28 07:12:40 +000010524
Matt Carlson07b01732009-08-28 14:01:15 +000010525 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010526}
10527
Michael Chan65138592012-09-28 07:12:41 +000010528static void tg3_stop(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010529{
Matt Carlson4f125f42009-09-01 12:55:02 +000010530 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010531
Matt Carlsondb219972011-11-04 09:15:03 +000010532 tg3_reset_task_cancel(tp);
Nithin Nayak Sujirbd473da2012-11-05 14:26:30 +000010533 tg3_netif_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010534
Matt Carlson21f76382012-02-22 12:35:21 +000010535 tg3_timer_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010536
Michael Chanaed93e02012-07-16 16:24:02 +000010537 tg3_hwmon_close(tp);
10538
Matt Carlson24bb4fb2009-10-05 17:55:29 +000010539 tg3_phy_stop(tp);
10540
David S. Millerf47c11e2005-06-24 20:18:35 -070010541 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010542
10543 tg3_disable_ints(tp);
10544
Michael Chan944d9802005-05-29 14:57:48 -070010545 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010546 tg3_free_rings(tp);
Joe Perches63c3a662011-04-26 08:12:10 +000010547 tg3_flag_clear(tp, INIT_COMPLETE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010548
David S. Millerf47c11e2005-06-24 20:18:35 -070010549 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010550
Matt Carlson4f125f42009-09-01 12:55:02 +000010551 for (i = tp->irq_cnt - 1; i >= 0; i--) {
10552 struct tg3_napi *tnapi = &tp->napi[i];
10553 free_irq(tnapi->irq_vec, tnapi);
10554 }
Matt Carlson07b01732009-08-28 14:01:15 +000010555
10556 tg3_ints_fini(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010557
Matt Carlson66cfd1b2010-09-30 10:34:30 +000010558 tg3_napi_fini(tp);
10559
Linus Torvalds1da177e2005-04-16 15:20:36 -070010560 tg3_free_consistent(tp);
Michael Chan65138592012-09-28 07:12:41 +000010561}
10562
Michael Chand8f4cd32012-09-28 07:12:40 +000010563static int tg3_open(struct net_device *dev)
10564{
10565 struct tg3 *tp = netdev_priv(dev);
10566 int err;
10567
10568 if (tp->fw_needed) {
10569 err = tg3_request_firmware(tp);
10570 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0) {
10571 if (err)
10572 return err;
10573 } else if (err) {
10574 netdev_warn(tp->dev, "TSO capability disabled\n");
10575 tg3_flag_clear(tp, TSO_CAPABLE);
10576 } else if (!tg3_flag(tp, TSO_CAPABLE)) {
10577 netdev_notice(tp->dev, "TSO capability restored\n");
10578 tg3_flag_set(tp, TSO_CAPABLE);
10579 }
10580 }
10581
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +000010582 tg3_carrier_off(tp);
Michael Chand8f4cd32012-09-28 07:12:40 +000010583
10584 err = tg3_power_up(tp);
10585 if (err)
10586 return err;
10587
10588 tg3_full_lock(tp, 0);
10589
10590 tg3_disable_ints(tp);
10591 tg3_flag_clear(tp, INIT_COMPLETE);
10592
10593 tg3_full_unlock(tp);
10594
Matt Carlsonbe947302012-12-03 19:36:57 +000010595 err = tg3_start(tp, true, true, true);
Michael Chand8f4cd32012-09-28 07:12:40 +000010596 if (err) {
10597 tg3_frob_aux_power(tp, false);
10598 pci_set_power_state(tp->pdev, PCI_D3hot);
10599 }
Matt Carlsonbe947302012-12-03 19:36:57 +000010600
Linus Torvalds1da177e2005-04-16 15:20:36 -070010601 return err;
10602}
10603
10604static int tg3_close(struct net_device *dev)
10605{
Linus Torvalds1da177e2005-04-16 15:20:36 -070010606 struct tg3 *tp = netdev_priv(dev);
10607
Matt Carlsonbe947302012-12-03 19:36:57 +000010608 tg3_ptp_fini(tp);
10609
Michael Chan65138592012-09-28 07:12:41 +000010610 tg3_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010611
10612 /* Clear stats across close / open calls */
10613 memset(&tp->net_stats_prev, 0, sizeof(tp->net_stats_prev));
10614 memset(&tp->estats_prev, 0, sizeof(tp->estats_prev));
Linus Torvalds1da177e2005-04-16 15:20:36 -070010615
10616 tg3_power_down(tp);
10617
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +000010618 tg3_carrier_off(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010619
10620 return 0;
10621}
10622
10623static inline u64 get_stat64(tg3_stat64_t *val)
10624{
10625 return ((u64)val->high << 32) | ((u64)val->low);
10626}
10627
10628static u64 tg3_calc_crc_errors(struct tg3 *tp)
10629{
10630 struct tg3_hw_stats *hw_stats = tp->hw_stats;
10631
10632 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
10633 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
10634 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)) {
10635 u32 val;
10636
10637 if (!tg3_readphy(tp, MII_TG3_TEST1, &val)) {
10638 tg3_writephy(tp, MII_TG3_TEST1,
10639 val | MII_TG3_TEST1_CRC_EN);
10640 tg3_readphy(tp, MII_TG3_RXR_COUNTERS, &val);
10641 } else
10642 val = 0;
10643
10644 tp->phy_crc_errors += val;
10645
10646 return tp->phy_crc_errors;
10647 }
10648
10649 return get_stat64(&hw_stats->rx_fcs_errors);
10650}
10651
10652#define ESTAT_ADD(member) \
10653 estats->member = old_estats->member + \
10654 get_stat64(&hw_stats->member)
10655
10656static void tg3_get_estats(struct tg3 *tp, struct tg3_ethtool_stats *estats)
10657{
10658 struct tg3_ethtool_stats *old_estats = &tp->estats_prev;
10659 struct tg3_hw_stats *hw_stats = tp->hw_stats;
10660
10661 ESTAT_ADD(rx_octets);
10662 ESTAT_ADD(rx_fragments);
10663 ESTAT_ADD(rx_ucast_packets);
10664 ESTAT_ADD(rx_mcast_packets);
10665 ESTAT_ADD(rx_bcast_packets);
10666 ESTAT_ADD(rx_fcs_errors);
10667 ESTAT_ADD(rx_align_errors);
10668 ESTAT_ADD(rx_xon_pause_rcvd);
10669 ESTAT_ADD(rx_xoff_pause_rcvd);
10670 ESTAT_ADD(rx_mac_ctrl_rcvd);
10671 ESTAT_ADD(rx_xoff_entered);
10672 ESTAT_ADD(rx_frame_too_long_errors);
10673 ESTAT_ADD(rx_jabbers);
10674 ESTAT_ADD(rx_undersize_packets);
10675 ESTAT_ADD(rx_in_length_errors);
10676 ESTAT_ADD(rx_out_length_errors);
10677 ESTAT_ADD(rx_64_or_less_octet_packets);
10678 ESTAT_ADD(rx_65_to_127_octet_packets);
10679 ESTAT_ADD(rx_128_to_255_octet_packets);
10680 ESTAT_ADD(rx_256_to_511_octet_packets);
10681 ESTAT_ADD(rx_512_to_1023_octet_packets);
10682 ESTAT_ADD(rx_1024_to_1522_octet_packets);
10683 ESTAT_ADD(rx_1523_to_2047_octet_packets);
10684 ESTAT_ADD(rx_2048_to_4095_octet_packets);
10685 ESTAT_ADD(rx_4096_to_8191_octet_packets);
10686 ESTAT_ADD(rx_8192_to_9022_octet_packets);
10687
10688 ESTAT_ADD(tx_octets);
10689 ESTAT_ADD(tx_collisions);
10690 ESTAT_ADD(tx_xon_sent);
10691 ESTAT_ADD(tx_xoff_sent);
10692 ESTAT_ADD(tx_flow_control);
10693 ESTAT_ADD(tx_mac_errors);
10694 ESTAT_ADD(tx_single_collisions);
10695 ESTAT_ADD(tx_mult_collisions);
10696 ESTAT_ADD(tx_deferred);
10697 ESTAT_ADD(tx_excessive_collisions);
10698 ESTAT_ADD(tx_late_collisions);
10699 ESTAT_ADD(tx_collide_2times);
10700 ESTAT_ADD(tx_collide_3times);
10701 ESTAT_ADD(tx_collide_4times);
10702 ESTAT_ADD(tx_collide_5times);
10703 ESTAT_ADD(tx_collide_6times);
10704 ESTAT_ADD(tx_collide_7times);
10705 ESTAT_ADD(tx_collide_8times);
10706 ESTAT_ADD(tx_collide_9times);
10707 ESTAT_ADD(tx_collide_10times);
10708 ESTAT_ADD(tx_collide_11times);
10709 ESTAT_ADD(tx_collide_12times);
10710 ESTAT_ADD(tx_collide_13times);
10711 ESTAT_ADD(tx_collide_14times);
10712 ESTAT_ADD(tx_collide_15times);
10713 ESTAT_ADD(tx_ucast_packets);
10714 ESTAT_ADD(tx_mcast_packets);
10715 ESTAT_ADD(tx_bcast_packets);
10716 ESTAT_ADD(tx_carrier_sense_errors);
10717 ESTAT_ADD(tx_discards);
10718 ESTAT_ADD(tx_errors);
10719
10720 ESTAT_ADD(dma_writeq_full);
10721 ESTAT_ADD(dma_write_prioq_full);
10722 ESTAT_ADD(rxbds_empty);
10723 ESTAT_ADD(rx_discards);
10724 ESTAT_ADD(rx_errors);
10725 ESTAT_ADD(rx_threshold_hit);
10726
10727 ESTAT_ADD(dma_readq_full);
10728 ESTAT_ADD(dma_read_prioq_full);
10729 ESTAT_ADD(tx_comp_queue_full);
10730
10731 ESTAT_ADD(ring_set_send_prod_index);
10732 ESTAT_ADD(ring_status_update);
10733 ESTAT_ADD(nic_irqs);
10734 ESTAT_ADD(nic_avoided_irqs);
10735 ESTAT_ADD(nic_tx_threshold_hit);
10736
Matt Carlson4452d092011-05-19 12:12:51 +000010737 ESTAT_ADD(mbuf_lwm_thresh_hit);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010738}
10739
Matt Carlson65ec6982012-02-28 23:33:37 +000010740static void tg3_get_nstats(struct tg3 *tp, struct rtnl_link_stats64 *stats)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010741{
Eric Dumazet511d2222010-07-07 20:44:24 +000010742 struct rtnl_link_stats64 *old_stats = &tp->net_stats_prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010743 struct tg3_hw_stats *hw_stats = tp->hw_stats;
10744
Linus Torvalds1da177e2005-04-16 15:20:36 -070010745 stats->rx_packets = old_stats->rx_packets +
10746 get_stat64(&hw_stats->rx_ucast_packets) +
10747 get_stat64(&hw_stats->rx_mcast_packets) +
10748 get_stat64(&hw_stats->rx_bcast_packets);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010749
Linus Torvalds1da177e2005-04-16 15:20:36 -070010750 stats->tx_packets = old_stats->tx_packets +
10751 get_stat64(&hw_stats->tx_ucast_packets) +
10752 get_stat64(&hw_stats->tx_mcast_packets) +
10753 get_stat64(&hw_stats->tx_bcast_packets);
10754
10755 stats->rx_bytes = old_stats->rx_bytes +
10756 get_stat64(&hw_stats->rx_octets);
10757 stats->tx_bytes = old_stats->tx_bytes +
10758 get_stat64(&hw_stats->tx_octets);
10759
10760 stats->rx_errors = old_stats->rx_errors +
John W. Linville4f63b872005-09-12 14:43:18 -070010761 get_stat64(&hw_stats->rx_errors);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010762 stats->tx_errors = old_stats->tx_errors +
10763 get_stat64(&hw_stats->tx_errors) +
10764 get_stat64(&hw_stats->tx_mac_errors) +
10765 get_stat64(&hw_stats->tx_carrier_sense_errors) +
10766 get_stat64(&hw_stats->tx_discards);
10767
10768 stats->multicast = old_stats->multicast +
10769 get_stat64(&hw_stats->rx_mcast_packets);
10770 stats->collisions = old_stats->collisions +
10771 get_stat64(&hw_stats->tx_collisions);
10772
10773 stats->rx_length_errors = old_stats->rx_length_errors +
10774 get_stat64(&hw_stats->rx_frame_too_long_errors) +
10775 get_stat64(&hw_stats->rx_undersize_packets);
10776
10777 stats->rx_over_errors = old_stats->rx_over_errors +
10778 get_stat64(&hw_stats->rxbds_empty);
10779 stats->rx_frame_errors = old_stats->rx_frame_errors +
10780 get_stat64(&hw_stats->rx_align_errors);
10781 stats->tx_aborted_errors = old_stats->tx_aborted_errors +
10782 get_stat64(&hw_stats->tx_discards);
10783 stats->tx_carrier_errors = old_stats->tx_carrier_errors +
10784 get_stat64(&hw_stats->tx_carrier_sense_errors);
10785
10786 stats->rx_crc_errors = old_stats->rx_crc_errors +
Matt Carlson65ec6982012-02-28 23:33:37 +000010787 tg3_calc_crc_errors(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010788
John W. Linville4f63b872005-09-12 14:43:18 -070010789 stats->rx_missed_errors = old_stats->rx_missed_errors +
10790 get_stat64(&hw_stats->rx_discards);
10791
Eric Dumazetb0057c52010-10-10 19:55:52 +000010792 stats->rx_dropped = tp->rx_dropped;
Eric Dumazet48855432011-10-24 07:53:03 +000010793 stats->tx_dropped = tp->tx_dropped;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010794}
10795
Linus Torvalds1da177e2005-04-16 15:20:36 -070010796static int tg3_get_regs_len(struct net_device *dev)
10797{
Matt Carlson97bd8e42011-04-13 11:05:04 +000010798 return TG3_REG_BLK_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010799}
10800
10801static void tg3_get_regs(struct net_device *dev,
10802 struct ethtool_regs *regs, void *_p)
10803{
Linus Torvalds1da177e2005-04-16 15:20:36 -070010804 struct tg3 *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010805
10806 regs->version = 0;
10807
Matt Carlson97bd8e42011-04-13 11:05:04 +000010808 memset(_p, 0, TG3_REG_BLK_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010809
Matt Carlson800960682010-08-02 11:26:06 +000010810 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Michael Chanbc1c7562006-03-20 17:48:03 -080010811 return;
10812
David S. Millerf47c11e2005-06-24 20:18:35 -070010813 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010814
Matt Carlson97bd8e42011-04-13 11:05:04 +000010815 tg3_dump_legacy_regs(tp, (u32 *)_p);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010816
David S. Millerf47c11e2005-06-24 20:18:35 -070010817 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010818}
10819
10820static int tg3_get_eeprom_len(struct net_device *dev)
10821{
10822 struct tg3 *tp = netdev_priv(dev);
10823
10824 return tp->nvram_size;
10825}
10826
Linus Torvalds1da177e2005-04-16 15:20:36 -070010827static int tg3_get_eeprom(struct net_device *dev, struct ethtool_eeprom *eeprom, u8 *data)
10828{
10829 struct tg3 *tp = netdev_priv(dev);
10830 int ret;
10831 u8 *pd;
Al Virob9fc7dc2007-12-17 22:59:57 -080010832 u32 i, offset, len, b_offset, b_count;
Matt Carlsona9dc5292009-02-25 14:25:30 +000010833 __be32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010834
Joe Perches63c3a662011-04-26 08:12:10 +000010835 if (tg3_flag(tp, NO_NVRAM))
Matt Carlsondf259d82009-04-20 06:57:14 +000010836 return -EINVAL;
10837
Matt Carlson800960682010-08-02 11:26:06 +000010838 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Michael Chanbc1c7562006-03-20 17:48:03 -080010839 return -EAGAIN;
10840
Linus Torvalds1da177e2005-04-16 15:20:36 -070010841 offset = eeprom->offset;
10842 len = eeprom->len;
10843 eeprom->len = 0;
10844
10845 eeprom->magic = TG3_EEPROM_MAGIC;
10846
10847 if (offset & 3) {
10848 /* adjustments to start on required 4 byte boundary */
10849 b_offset = offset & 3;
10850 b_count = 4 - b_offset;
10851 if (b_count > len) {
10852 /* i.e. offset=1 len=2 */
10853 b_count = len;
10854 }
Matt Carlsona9dc5292009-02-25 14:25:30 +000010855 ret = tg3_nvram_read_be32(tp, offset-b_offset, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010856 if (ret)
10857 return ret;
Matt Carlsonbe98da62010-07-11 09:31:46 +000010858 memcpy(data, ((char *)&val) + b_offset, b_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010859 len -= b_count;
10860 offset += b_count;
Matt Carlsonc6cdf432010-04-05 10:19:26 +000010861 eeprom->len += b_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010862 }
10863
Lucas De Marchi25985ed2011-03-30 22:57:33 -030010864 /* read bytes up to the last 4 byte boundary */
Linus Torvalds1da177e2005-04-16 15:20:36 -070010865 pd = &data[eeprom->len];
10866 for (i = 0; i < (len - (len & 3)); i += 4) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000010867 ret = tg3_nvram_read_be32(tp, offset + i, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010868 if (ret) {
10869 eeprom->len += i;
10870 return ret;
10871 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010872 memcpy(pd + i, &val, 4);
10873 }
10874 eeprom->len += i;
10875
10876 if (len & 3) {
10877 /* read last bytes not ending on 4 byte boundary */
10878 pd = &data[eeprom->len];
10879 b_count = len & 3;
10880 b_offset = offset + len - b_count;
Matt Carlsona9dc5292009-02-25 14:25:30 +000010881 ret = tg3_nvram_read_be32(tp, b_offset, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010882 if (ret)
10883 return ret;
Al Virob9fc7dc2007-12-17 22:59:57 -080010884 memcpy(pd, &val, b_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010885 eeprom->len += b_count;
10886 }
10887 return 0;
10888}
10889
Linus Torvalds1da177e2005-04-16 15:20:36 -070010890static int tg3_set_eeprom(struct net_device *dev, struct ethtool_eeprom *eeprom, u8 *data)
10891{
10892 struct tg3 *tp = netdev_priv(dev);
10893 int ret;
Al Virob9fc7dc2007-12-17 22:59:57 -080010894 u32 offset, len, b_offset, odd_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010895 u8 *buf;
Matt Carlsona9dc5292009-02-25 14:25:30 +000010896 __be32 start, end;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010897
Matt Carlson800960682010-08-02 11:26:06 +000010898 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Michael Chanbc1c7562006-03-20 17:48:03 -080010899 return -EAGAIN;
10900
Joe Perches63c3a662011-04-26 08:12:10 +000010901 if (tg3_flag(tp, NO_NVRAM) ||
Matt Carlsondf259d82009-04-20 06:57:14 +000010902 eeprom->magic != TG3_EEPROM_MAGIC)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010903 return -EINVAL;
10904
10905 offset = eeprom->offset;
10906 len = eeprom->len;
10907
10908 if ((b_offset = (offset & 3))) {
10909 /* adjustments to start on required 4 byte boundary */
Matt Carlsona9dc5292009-02-25 14:25:30 +000010910 ret = tg3_nvram_read_be32(tp, offset-b_offset, &start);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010911 if (ret)
10912 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010913 len += b_offset;
10914 offset &= ~3;
Michael Chan1c8594b2005-04-21 17:12:46 -070010915 if (len < 4)
10916 len = 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010917 }
10918
10919 odd_len = 0;
Michael Chan1c8594b2005-04-21 17:12:46 -070010920 if (len & 3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010921 /* adjustments to end on required 4 byte boundary */
10922 odd_len = 1;
10923 len = (len + 3) & ~3;
Matt Carlsona9dc5292009-02-25 14:25:30 +000010924 ret = tg3_nvram_read_be32(tp, offset+len-4, &end);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010925 if (ret)
10926 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010927 }
10928
10929 buf = data;
10930 if (b_offset || odd_len) {
10931 buf = kmalloc(len, GFP_KERNEL);
Andy Gospodarekab0049b2007-09-06 20:42:14 +010010932 if (!buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010933 return -ENOMEM;
10934 if (b_offset)
10935 memcpy(buf, &start, 4);
10936 if (odd_len)
10937 memcpy(buf+len-4, &end, 4);
10938 memcpy(buf + b_offset, data, eeprom->len);
10939 }
10940
10941 ret = tg3_nvram_write_block(tp, offset, len, buf);
10942
10943 if (buf != data)
10944 kfree(buf);
10945
10946 return ret;
10947}
10948
10949static int tg3_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
10950{
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010951 struct tg3 *tp = netdev_priv(dev);
10952
Joe Perches63c3a662011-04-26 08:12:10 +000010953 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000010954 struct phy_device *phydev;
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010955 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010956 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000010957 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
10958 return phy_ethtool_gset(phydev, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010959 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010960
Linus Torvalds1da177e2005-04-16 15:20:36 -070010961 cmd->supported = (SUPPORTED_Autoneg);
10962
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010963 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010964 cmd->supported |= (SUPPORTED_1000baseT_Half |
10965 SUPPORTED_1000baseT_Full);
10966
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010967 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010968 cmd->supported |= (SUPPORTED_100baseT_Half |
10969 SUPPORTED_100baseT_Full |
10970 SUPPORTED_10baseT_Half |
10971 SUPPORTED_10baseT_Full |
Matt Carlson3bebab52007-11-12 21:22:40 -080010972 SUPPORTED_TP);
Karsten Keilef348142006-05-12 12:49:08 -070010973 cmd->port = PORT_TP;
10974 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010975 cmd->supported |= SUPPORTED_FIBRE;
Karsten Keilef348142006-05-12 12:49:08 -070010976 cmd->port = PORT_FIBRE;
10977 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010978
Linus Torvalds1da177e2005-04-16 15:20:36 -070010979 cmd->advertising = tp->link_config.advertising;
Matt Carlson5bb09772011-06-13 13:39:00 +000010980 if (tg3_flag(tp, PAUSE_AUTONEG)) {
10981 if (tp->link_config.flowctrl & FLOW_CTRL_RX) {
10982 if (tp->link_config.flowctrl & FLOW_CTRL_TX) {
10983 cmd->advertising |= ADVERTISED_Pause;
10984 } else {
10985 cmd->advertising |= ADVERTISED_Pause |
10986 ADVERTISED_Asym_Pause;
10987 }
10988 } else if (tp->link_config.flowctrl & FLOW_CTRL_TX) {
10989 cmd->advertising |= ADVERTISED_Asym_Pause;
10990 }
10991 }
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +000010992 if (netif_running(dev) && tp->link_up) {
David Decotigny70739492011-04-27 18:32:40 +000010993 ethtool_cmd_speed_set(cmd, tp->link_config.active_speed);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010994 cmd->duplex = tp->link_config.active_duplex;
Matt Carlson859edb22011-12-08 14:40:16 +000010995 cmd->lp_advertising = tp->link_config.rmt_adv;
Matt Carlsone348c5e2011-11-21 15:01:20 +000010996 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES)) {
10997 if (tp->phy_flags & TG3_PHYFLG_MDIX_STATE)
10998 cmd->eth_tp_mdix = ETH_TP_MDI_X;
10999 else
11000 cmd->eth_tp_mdix = ETH_TP_MDI;
11001 }
Matt Carlson64c22182010-10-14 10:37:44 +000011002 } else {
Matt Carlsone7405222012-02-13 15:20:16 +000011003 ethtool_cmd_speed_set(cmd, SPEED_UNKNOWN);
11004 cmd->duplex = DUPLEX_UNKNOWN;
Matt Carlsone348c5e2011-11-21 15:01:20 +000011005 cmd->eth_tp_mdix = ETH_TP_MDI_INVALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011006 }
Matt Carlson882e9792009-09-01 13:21:36 +000011007 cmd->phy_address = tp->phy_addr;
Matt Carlson7e5856b2009-02-25 14:23:01 +000011008 cmd->transceiver = XCVR_INTERNAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011009 cmd->autoneg = tp->link_config.autoneg;
11010 cmd->maxtxpkt = 0;
11011 cmd->maxrxpkt = 0;
11012 return 0;
11013}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011014
Linus Torvalds1da177e2005-04-16 15:20:36 -070011015static int tg3_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
11016{
11017 struct tg3 *tp = netdev_priv(dev);
David Decotigny25db0332011-04-27 18:32:39 +000011018 u32 speed = ethtool_cmd_speed(cmd);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011019
Joe Perches63c3a662011-04-26 08:12:10 +000011020 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000011021 struct phy_device *phydev;
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011022 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011023 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000011024 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
11025 return phy_ethtool_sset(phydev, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011026 }
11027
Matt Carlson7e5856b2009-02-25 14:23:01 +000011028 if (cmd->autoneg != AUTONEG_ENABLE &&
11029 cmd->autoneg != AUTONEG_DISABLE)
Michael Chan37ff2382005-10-26 15:49:51 -070011030 return -EINVAL;
Matt Carlson7e5856b2009-02-25 14:23:01 +000011031
11032 if (cmd->autoneg == AUTONEG_DISABLE &&
11033 cmd->duplex != DUPLEX_FULL &&
11034 cmd->duplex != DUPLEX_HALF)
Michael Chan37ff2382005-10-26 15:49:51 -070011035 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011036
Matt Carlson7e5856b2009-02-25 14:23:01 +000011037 if (cmd->autoneg == AUTONEG_ENABLE) {
11038 u32 mask = ADVERTISED_Autoneg |
11039 ADVERTISED_Pause |
11040 ADVERTISED_Asym_Pause;
11041
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011042 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
Matt Carlson7e5856b2009-02-25 14:23:01 +000011043 mask |= ADVERTISED_1000baseT_Half |
11044 ADVERTISED_1000baseT_Full;
11045
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011046 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
Matt Carlson7e5856b2009-02-25 14:23:01 +000011047 mask |= ADVERTISED_100baseT_Half |
11048 ADVERTISED_100baseT_Full |
11049 ADVERTISED_10baseT_Half |
11050 ADVERTISED_10baseT_Full |
11051 ADVERTISED_TP;
11052 else
11053 mask |= ADVERTISED_FIBRE;
11054
11055 if (cmd->advertising & ~mask)
11056 return -EINVAL;
11057
11058 mask &= (ADVERTISED_1000baseT_Half |
11059 ADVERTISED_1000baseT_Full |
11060 ADVERTISED_100baseT_Half |
11061 ADVERTISED_100baseT_Full |
11062 ADVERTISED_10baseT_Half |
11063 ADVERTISED_10baseT_Full);
11064
11065 cmd->advertising &= mask;
11066 } else {
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011067 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES) {
David Decotigny25db0332011-04-27 18:32:39 +000011068 if (speed != SPEED_1000)
Matt Carlson7e5856b2009-02-25 14:23:01 +000011069 return -EINVAL;
11070
11071 if (cmd->duplex != DUPLEX_FULL)
11072 return -EINVAL;
11073 } else {
David Decotigny25db0332011-04-27 18:32:39 +000011074 if (speed != SPEED_100 &&
11075 speed != SPEED_10)
Matt Carlson7e5856b2009-02-25 14:23:01 +000011076 return -EINVAL;
11077 }
11078 }
11079
David S. Millerf47c11e2005-06-24 20:18:35 -070011080 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011081
11082 tp->link_config.autoneg = cmd->autoneg;
11083 if (cmd->autoneg == AUTONEG_ENABLE) {
Andy Gospodarek405d8e52007-10-08 01:08:47 -070011084 tp->link_config.advertising = (cmd->advertising |
11085 ADVERTISED_Autoneg);
Matt Carlsone7405222012-02-13 15:20:16 +000011086 tp->link_config.speed = SPEED_UNKNOWN;
11087 tp->link_config.duplex = DUPLEX_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011088 } else {
11089 tp->link_config.advertising = 0;
David Decotigny25db0332011-04-27 18:32:39 +000011090 tp->link_config.speed = speed;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011091 tp->link_config.duplex = cmd->duplex;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011092 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011093
Linus Torvalds1da177e2005-04-16 15:20:36 -070011094 if (netif_running(dev))
11095 tg3_setup_phy(tp, 1);
11096
David S. Millerf47c11e2005-06-24 20:18:35 -070011097 tg3_full_unlock(tp);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011098
Linus Torvalds1da177e2005-04-16 15:20:36 -070011099 return 0;
11100}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011101
Linus Torvalds1da177e2005-04-16 15:20:36 -070011102static void tg3_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
11103{
11104 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011105
Rick Jones68aad782011-11-07 13:29:27 +000011106 strlcpy(info->driver, DRV_MODULE_NAME, sizeof(info->driver));
11107 strlcpy(info->version, DRV_MODULE_VERSION, sizeof(info->version));
11108 strlcpy(info->fw_version, tp->fw_ver, sizeof(info->fw_version));
11109 strlcpy(info->bus_info, pci_name(tp->pdev), sizeof(info->bus_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -070011110}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011111
Linus Torvalds1da177e2005-04-16 15:20:36 -070011112static void tg3_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
11113{
11114 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011115
Joe Perches63c3a662011-04-26 08:12:10 +000011116 if (tg3_flag(tp, WOL_CAP) && device_can_wakeup(&tp->pdev->dev))
Gary Zambranoa85feb82007-05-05 11:52:19 -070011117 wol->supported = WAKE_MAGIC;
11118 else
11119 wol->supported = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011120 wol->wolopts = 0;
Joe Perches63c3a662011-04-26 08:12:10 +000011121 if (tg3_flag(tp, WOL_ENABLE) && device_can_wakeup(&tp->pdev->dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -070011122 wol->wolopts = WAKE_MAGIC;
11123 memset(&wol->sopass, 0, sizeof(wol->sopass));
11124}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011125
Linus Torvalds1da177e2005-04-16 15:20:36 -070011126static int tg3_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
11127{
11128 struct tg3 *tp = netdev_priv(dev);
Rafael J. Wysocki12dac072008-07-30 16:37:33 -070011129 struct device *dp = &tp->pdev->dev;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011130
Linus Torvalds1da177e2005-04-16 15:20:36 -070011131 if (wol->wolopts & ~WAKE_MAGIC)
11132 return -EINVAL;
11133 if ((wol->wolopts & WAKE_MAGIC) &&
Joe Perches63c3a662011-04-26 08:12:10 +000011134 !(tg3_flag(tp, WOL_CAP) && device_can_wakeup(dp)))
Linus Torvalds1da177e2005-04-16 15:20:36 -070011135 return -EINVAL;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011136
Rafael J. Wysockif2dc0d12010-10-25 13:01:55 +000011137 device_set_wakeup_enable(dp, wol->wolopts & WAKE_MAGIC);
11138
David S. Millerf47c11e2005-06-24 20:18:35 -070011139 spin_lock_bh(&tp->lock);
Rafael J. Wysockif2dc0d12010-10-25 13:01:55 +000011140 if (device_may_wakeup(dp))
Joe Perches63c3a662011-04-26 08:12:10 +000011141 tg3_flag_set(tp, WOL_ENABLE);
Rafael J. Wysockif2dc0d12010-10-25 13:01:55 +000011142 else
Joe Perches63c3a662011-04-26 08:12:10 +000011143 tg3_flag_clear(tp, WOL_ENABLE);
David S. Millerf47c11e2005-06-24 20:18:35 -070011144 spin_unlock_bh(&tp->lock);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011145
Linus Torvalds1da177e2005-04-16 15:20:36 -070011146 return 0;
11147}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011148
Linus Torvalds1da177e2005-04-16 15:20:36 -070011149static u32 tg3_get_msglevel(struct net_device *dev)
11150{
11151 struct tg3 *tp = netdev_priv(dev);
11152 return tp->msg_enable;
11153}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011154
Linus Torvalds1da177e2005-04-16 15:20:36 -070011155static void tg3_set_msglevel(struct net_device *dev, u32 value)
11156{
11157 struct tg3 *tp = netdev_priv(dev);
11158 tp->msg_enable = value;
11159}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011160
Linus Torvalds1da177e2005-04-16 15:20:36 -070011161static int tg3_nway_reset(struct net_device *dev)
11162{
11163 struct tg3 *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011164 int r;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011165
Linus Torvalds1da177e2005-04-16 15:20:36 -070011166 if (!netif_running(dev))
11167 return -EAGAIN;
11168
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011169 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Michael Chanc94e3942005-09-27 12:12:42 -070011170 return -EINVAL;
11171
Joe Perches63c3a662011-04-26 08:12:10 +000011172 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011173 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011174 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000011175 r = phy_start_aneg(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011176 } else {
11177 u32 bmcr;
11178
11179 spin_lock_bh(&tp->lock);
11180 r = -EINVAL;
11181 tg3_readphy(tp, MII_BMCR, &bmcr);
11182 if (!tg3_readphy(tp, MII_BMCR, &bmcr) &&
11183 ((bmcr & BMCR_ANENABLE) ||
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011184 (tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT))) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011185 tg3_writephy(tp, MII_BMCR, bmcr | BMCR_ANRESTART |
11186 BMCR_ANENABLE);
11187 r = 0;
11188 }
11189 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011190 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011191
Linus Torvalds1da177e2005-04-16 15:20:36 -070011192 return r;
11193}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011194
Linus Torvalds1da177e2005-04-16 15:20:36 -070011195static void tg3_get_ringparam(struct net_device *dev, struct ethtool_ringparam *ering)
11196{
11197 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011198
Matt Carlson2c49a442010-09-30 10:34:35 +000011199 ering->rx_max_pending = tp->rx_std_ring_mask;
Joe Perches63c3a662011-04-26 08:12:10 +000011200 if (tg3_flag(tp, JUMBO_RING_ENABLE))
Matt Carlson2c49a442010-09-30 10:34:35 +000011201 ering->rx_jumbo_max_pending = tp->rx_jmb_ring_mask;
Michael Chan4f81c322006-03-20 21:33:42 -080011202 else
11203 ering->rx_jumbo_max_pending = 0;
11204
11205 ering->tx_max_pending = TG3_TX_RING_SIZE - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011206
11207 ering->rx_pending = tp->rx_pending;
Joe Perches63c3a662011-04-26 08:12:10 +000011208 if (tg3_flag(tp, JUMBO_RING_ENABLE))
Michael Chan4f81c322006-03-20 21:33:42 -080011209 ering->rx_jumbo_pending = tp->rx_jumbo_pending;
11210 else
11211 ering->rx_jumbo_pending = 0;
11212
Matt Carlsonf3f3f272009-08-28 14:03:21 +000011213 ering->tx_pending = tp->napi[0].tx_pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011214}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011215
Linus Torvalds1da177e2005-04-16 15:20:36 -070011216static int tg3_set_ringparam(struct net_device *dev, struct ethtool_ringparam *ering)
11217{
11218 struct tg3 *tp = netdev_priv(dev);
Matt Carlson646c9ed2009-09-01 12:58:41 +000011219 int i, irq_sync = 0, err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011220
Matt Carlson2c49a442010-09-30 10:34:35 +000011221 if ((ering->rx_pending > tp->rx_std_ring_mask) ||
11222 (ering->rx_jumbo_pending > tp->rx_jmb_ring_mask) ||
Michael Chanbc3a9252006-10-18 20:55:18 -070011223 (ering->tx_pending > TG3_TX_RING_SIZE - 1) ||
11224 (ering->tx_pending <= MAX_SKB_FRAGS) ||
Joe Perches63c3a662011-04-26 08:12:10 +000011225 (tg3_flag(tp, TSO_BUG) &&
Michael Chanbc3a9252006-10-18 20:55:18 -070011226 (ering->tx_pending <= (MAX_SKB_FRAGS * 3))))
Linus Torvalds1da177e2005-04-16 15:20:36 -070011227 return -EINVAL;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011228
Michael Chanbbe832c2005-06-24 20:20:04 -070011229 if (netif_running(dev)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011230 tg3_phy_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011231 tg3_netif_stop(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -070011232 irq_sync = 1;
11233 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070011234
Michael Chanbbe832c2005-06-24 20:20:04 -070011235 tg3_full_lock(tp, irq_sync);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011236
Linus Torvalds1da177e2005-04-16 15:20:36 -070011237 tp->rx_pending = ering->rx_pending;
11238
Joe Perches63c3a662011-04-26 08:12:10 +000011239 if (tg3_flag(tp, MAX_RXPEND_64) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070011240 tp->rx_pending > 63)
11241 tp->rx_pending = 63;
11242 tp->rx_jumbo_pending = ering->rx_jumbo_pending;
Matt Carlson646c9ed2009-09-01 12:58:41 +000011243
Matt Carlson6fd45cb2010-09-15 08:59:57 +000011244 for (i = 0; i < tp->irq_max; i++)
Matt Carlson646c9ed2009-09-01 12:58:41 +000011245 tp->napi[i].tx_pending = ering->tx_pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011246
11247 if (netif_running(dev)) {
Michael Chan944d9802005-05-29 14:57:48 -070011248 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Michael Chanb9ec6c12006-07-25 16:37:27 -070011249 err = tg3_restart_hw(tp, 1);
11250 if (!err)
11251 tg3_netif_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011252 }
11253
David S. Millerf47c11e2005-06-24 20:18:35 -070011254 tg3_full_unlock(tp);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011255
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011256 if (irq_sync && !err)
11257 tg3_phy_start(tp);
11258
Michael Chanb9ec6c12006-07-25 16:37:27 -070011259 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011260}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011261
Linus Torvalds1da177e2005-04-16 15:20:36 -070011262static void tg3_get_pauseparam(struct net_device *dev, struct ethtool_pauseparam *epause)
11263{
11264 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011265
Joe Perches63c3a662011-04-26 08:12:10 +000011266 epause->autoneg = !!tg3_flag(tp, PAUSE_AUTONEG);
Matt Carlson8d018622007-12-20 20:05:44 -080011267
Matt Carlson4a2db502011-12-08 14:40:17 +000011268 if (tp->link_config.flowctrl & FLOW_CTRL_RX)
Matt Carlson8d018622007-12-20 20:05:44 -080011269 epause->rx_pause = 1;
11270 else
11271 epause->rx_pause = 0;
11272
Matt Carlson4a2db502011-12-08 14:40:17 +000011273 if (tp->link_config.flowctrl & FLOW_CTRL_TX)
Matt Carlson8d018622007-12-20 20:05:44 -080011274 epause->tx_pause = 1;
11275 else
11276 epause->tx_pause = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011277}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011278
Linus Torvalds1da177e2005-04-16 15:20:36 -070011279static int tg3_set_pauseparam(struct net_device *dev, struct ethtool_pauseparam *epause)
11280{
11281 struct tg3 *tp = netdev_priv(dev);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011282 int err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011283
Joe Perches63c3a662011-04-26 08:12:10 +000011284 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson27121682010-02-17 15:16:57 +000011285 u32 newadv;
11286 struct phy_device *phydev;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011287
Matt Carlson27121682010-02-17 15:16:57 +000011288 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011289
Matt Carlson27121682010-02-17 15:16:57 +000011290 if (!(phydev->supported & SUPPORTED_Pause) ||
11291 (!(phydev->supported & SUPPORTED_Asym_Pause) &&
Nicolas Kaiser2259dca2010-10-07 23:29:27 +000011292 (epause->rx_pause != epause->tx_pause)))
Matt Carlson27121682010-02-17 15:16:57 +000011293 return -EINVAL;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011294
Matt Carlson27121682010-02-17 15:16:57 +000011295 tp->link_config.flowctrl = 0;
11296 if (epause->rx_pause) {
11297 tp->link_config.flowctrl |= FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011298
Matt Carlson27121682010-02-17 15:16:57 +000011299 if (epause->tx_pause) {
Steve Glendinninge18ce342008-12-16 02:00:00 -080011300 tp->link_config.flowctrl |= FLOW_CTRL_TX;
Matt Carlson27121682010-02-17 15:16:57 +000011301 newadv = ADVERTISED_Pause;
11302 } else
11303 newadv = ADVERTISED_Pause |
11304 ADVERTISED_Asym_Pause;
11305 } else if (epause->tx_pause) {
11306 tp->link_config.flowctrl |= FLOW_CTRL_TX;
11307 newadv = ADVERTISED_Asym_Pause;
11308 } else
11309 newadv = 0;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011310
Matt Carlson27121682010-02-17 15:16:57 +000011311 if (epause->autoneg)
Joe Perches63c3a662011-04-26 08:12:10 +000011312 tg3_flag_set(tp, PAUSE_AUTONEG);
Matt Carlson27121682010-02-17 15:16:57 +000011313 else
Joe Perches63c3a662011-04-26 08:12:10 +000011314 tg3_flag_clear(tp, PAUSE_AUTONEG);
Matt Carlson27121682010-02-17 15:16:57 +000011315
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011316 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) {
Matt Carlson27121682010-02-17 15:16:57 +000011317 u32 oldadv = phydev->advertising &
11318 (ADVERTISED_Pause | ADVERTISED_Asym_Pause);
11319 if (oldadv != newadv) {
11320 phydev->advertising &=
11321 ~(ADVERTISED_Pause |
11322 ADVERTISED_Asym_Pause);
11323 phydev->advertising |= newadv;
11324 if (phydev->autoneg) {
11325 /*
11326 * Always renegotiate the link to
11327 * inform our link partner of our
11328 * flow control settings, even if the
11329 * flow control is forced. Let
11330 * tg3_adjust_link() do the final
11331 * flow control setup.
11332 */
11333 return phy_start_aneg(phydev);
11334 }
11335 }
11336
11337 if (!epause->autoneg)
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011338 tg3_setup_flow_control(tp, 0, 0);
Matt Carlson27121682010-02-17 15:16:57 +000011339 } else {
Matt Carlsonc6700ce2012-02-13 15:20:15 +000011340 tp->link_config.advertising &=
Matt Carlson27121682010-02-17 15:16:57 +000011341 ~(ADVERTISED_Pause |
11342 ADVERTISED_Asym_Pause);
Matt Carlsonc6700ce2012-02-13 15:20:15 +000011343 tp->link_config.advertising |= newadv;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011344 }
11345 } else {
11346 int irq_sync = 0;
11347
11348 if (netif_running(dev)) {
11349 tg3_netif_stop(tp);
11350 irq_sync = 1;
11351 }
11352
11353 tg3_full_lock(tp, irq_sync);
11354
11355 if (epause->autoneg)
Joe Perches63c3a662011-04-26 08:12:10 +000011356 tg3_flag_set(tp, PAUSE_AUTONEG);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011357 else
Joe Perches63c3a662011-04-26 08:12:10 +000011358 tg3_flag_clear(tp, PAUSE_AUTONEG);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011359 if (epause->rx_pause)
Steve Glendinninge18ce342008-12-16 02:00:00 -080011360 tp->link_config.flowctrl |= FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011361 else
Steve Glendinninge18ce342008-12-16 02:00:00 -080011362 tp->link_config.flowctrl &= ~FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011363 if (epause->tx_pause)
Steve Glendinninge18ce342008-12-16 02:00:00 -080011364 tp->link_config.flowctrl |= FLOW_CTRL_TX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011365 else
Steve Glendinninge18ce342008-12-16 02:00:00 -080011366 tp->link_config.flowctrl &= ~FLOW_CTRL_TX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011367
11368 if (netif_running(dev)) {
11369 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
11370 err = tg3_restart_hw(tp, 1);
11371 if (!err)
11372 tg3_netif_start(tp);
11373 }
11374
11375 tg3_full_unlock(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -070011376 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070011377
Michael Chanb9ec6c12006-07-25 16:37:27 -070011378 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011379}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011380
Matt Carlsonde6f31e2010-04-12 06:58:30 +000011381static int tg3_get_sset_count(struct net_device *dev, int sset)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011382{
Jeff Garzikb9f2c042007-10-03 18:07:32 -070011383 switch (sset) {
11384 case ETH_SS_TEST:
11385 return TG3_NUM_TEST;
11386 case ETH_SS_STATS:
11387 return TG3_NUM_STATS;
11388 default:
11389 return -EOPNOTSUPP;
11390 }
Michael Chan4cafd3f2005-05-29 14:56:34 -070011391}
11392
Matt Carlson90415472011-12-16 13:33:23 +000011393static int tg3_get_rxnfc(struct net_device *dev, struct ethtool_rxnfc *info,
11394 u32 *rules __always_unused)
11395{
11396 struct tg3 *tp = netdev_priv(dev);
11397
11398 if (!tg3_flag(tp, SUPPORT_MSIX))
11399 return -EOPNOTSUPP;
11400
11401 switch (info->cmd) {
11402 case ETHTOOL_GRXRINGS:
11403 if (netif_running(tp->dev))
Michael Chan91024262012-09-28 07:12:38 +000011404 info->data = tp->rxq_cnt;
Matt Carlson90415472011-12-16 13:33:23 +000011405 else {
11406 info->data = num_online_cpus();
Michael Chan91024262012-09-28 07:12:38 +000011407 if (info->data > TG3_RSS_MAX_NUM_QS)
11408 info->data = TG3_RSS_MAX_NUM_QS;
Matt Carlson90415472011-12-16 13:33:23 +000011409 }
11410
11411 /* The first interrupt vector only
11412 * handles link interrupts.
11413 */
11414 info->data -= 1;
11415 return 0;
11416
11417 default:
11418 return -EOPNOTSUPP;
11419 }
11420}
11421
11422static u32 tg3_get_rxfh_indir_size(struct net_device *dev)
11423{
11424 u32 size = 0;
11425 struct tg3 *tp = netdev_priv(dev);
11426
11427 if (tg3_flag(tp, SUPPORT_MSIX))
11428 size = TG3_RSS_INDIR_TBL_SIZE;
11429
11430 return size;
11431}
11432
11433static int tg3_get_rxfh_indir(struct net_device *dev, u32 *indir)
11434{
11435 struct tg3 *tp = netdev_priv(dev);
11436 int i;
11437
11438 for (i = 0; i < TG3_RSS_INDIR_TBL_SIZE; i++)
11439 indir[i] = tp->rss_ind_tbl[i];
11440
11441 return 0;
11442}
11443
11444static int tg3_set_rxfh_indir(struct net_device *dev, const u32 *indir)
11445{
11446 struct tg3 *tp = netdev_priv(dev);
11447 size_t i;
11448
11449 for (i = 0; i < TG3_RSS_INDIR_TBL_SIZE; i++)
11450 tp->rss_ind_tbl[i] = indir[i];
11451
11452 if (!netif_running(dev) || !tg3_flag(tp, ENABLE_RSS))
11453 return 0;
11454
11455 /* It is legal to write the indirection
11456 * table while the device is running.
11457 */
11458 tg3_full_lock(tp, 0);
11459 tg3_rss_write_indir_tbl(tp);
11460 tg3_full_unlock(tp);
11461
11462 return 0;
11463}
11464
Michael Chan09681692012-09-28 07:12:42 +000011465static void tg3_get_channels(struct net_device *dev,
11466 struct ethtool_channels *channel)
11467{
11468 struct tg3 *tp = netdev_priv(dev);
11469 u32 deflt_qs = netif_get_num_default_rss_queues();
11470
11471 channel->max_rx = tp->rxq_max;
11472 channel->max_tx = tp->txq_max;
11473
11474 if (netif_running(dev)) {
11475 channel->rx_count = tp->rxq_cnt;
11476 channel->tx_count = tp->txq_cnt;
11477 } else {
11478 if (tp->rxq_req)
11479 channel->rx_count = tp->rxq_req;
11480 else
11481 channel->rx_count = min(deflt_qs, tp->rxq_max);
11482
11483 if (tp->txq_req)
11484 channel->tx_count = tp->txq_req;
11485 else
11486 channel->tx_count = min(deflt_qs, tp->txq_max);
11487 }
11488}
11489
11490static int tg3_set_channels(struct net_device *dev,
11491 struct ethtool_channels *channel)
11492{
11493 struct tg3 *tp = netdev_priv(dev);
11494
11495 if (!tg3_flag(tp, SUPPORT_MSIX))
11496 return -EOPNOTSUPP;
11497
11498 if (channel->rx_count > tp->rxq_max ||
11499 channel->tx_count > tp->txq_max)
11500 return -EINVAL;
11501
11502 tp->rxq_req = channel->rx_count;
11503 tp->txq_req = channel->tx_count;
11504
11505 if (!netif_running(dev))
11506 return 0;
11507
11508 tg3_stop(tp);
11509
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +000011510 tg3_carrier_off(tp);
Michael Chan09681692012-09-28 07:12:42 +000011511
Matt Carlsonbe947302012-12-03 19:36:57 +000011512 tg3_start(tp, true, false, false);
Michael Chan09681692012-09-28 07:12:42 +000011513
11514 return 0;
11515}
11516
Matt Carlsonde6f31e2010-04-12 06:58:30 +000011517static void tg3_get_strings(struct net_device *dev, u32 stringset, u8 *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011518{
11519 switch (stringset) {
11520 case ETH_SS_STATS:
11521 memcpy(buf, &ethtool_stats_keys, sizeof(ethtool_stats_keys));
11522 break;
Michael Chan4cafd3f2005-05-29 14:56:34 -070011523 case ETH_SS_TEST:
11524 memcpy(buf, &ethtool_test_keys, sizeof(ethtool_test_keys));
11525 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011526 default:
11527 WARN_ON(1); /* we need a WARN() */
11528 break;
11529 }
11530}
11531
stephen hemminger81b87092011-04-04 08:43:50 +000011532static int tg3_set_phys_id(struct net_device *dev,
11533 enum ethtool_phys_id_state state)
Michael Chan4009a932005-09-05 17:52:54 -070011534{
11535 struct tg3 *tp = netdev_priv(dev);
Michael Chan4009a932005-09-05 17:52:54 -070011536
11537 if (!netif_running(tp->dev))
11538 return -EAGAIN;
11539
stephen hemminger81b87092011-04-04 08:43:50 +000011540 switch (state) {
11541 case ETHTOOL_ID_ACTIVE:
Allan, Bruce Wfce55922011-04-13 13:09:10 +000011542 return 1; /* cycle on/off once per second */
Michael Chan4009a932005-09-05 17:52:54 -070011543
stephen hemminger81b87092011-04-04 08:43:50 +000011544 case ETHTOOL_ID_ON:
11545 tw32(MAC_LED_CTRL, LED_CTRL_LNKLED_OVERRIDE |
11546 LED_CTRL_1000MBPS_ON |
11547 LED_CTRL_100MBPS_ON |
11548 LED_CTRL_10MBPS_ON |
11549 LED_CTRL_TRAFFIC_OVERRIDE |
11550 LED_CTRL_TRAFFIC_BLINK |
11551 LED_CTRL_TRAFFIC_LED);
11552 break;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011553
stephen hemminger81b87092011-04-04 08:43:50 +000011554 case ETHTOOL_ID_OFF:
11555 tw32(MAC_LED_CTRL, LED_CTRL_LNKLED_OVERRIDE |
11556 LED_CTRL_TRAFFIC_OVERRIDE);
11557 break;
Michael Chan4009a932005-09-05 17:52:54 -070011558
stephen hemminger81b87092011-04-04 08:43:50 +000011559 case ETHTOOL_ID_INACTIVE:
11560 tw32(MAC_LED_CTRL, tp->led_ctrl);
11561 break;
Michael Chan4009a932005-09-05 17:52:54 -070011562 }
stephen hemminger81b87092011-04-04 08:43:50 +000011563
Michael Chan4009a932005-09-05 17:52:54 -070011564 return 0;
11565}
11566
Matt Carlsonde6f31e2010-04-12 06:58:30 +000011567static void tg3_get_ethtool_stats(struct net_device *dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -070011568 struct ethtool_stats *estats, u64 *tmp_stats)
11569{
11570 struct tg3 *tp = netdev_priv(dev);
Matt Carlson0e6c9da2011-12-08 14:40:13 +000011571
Matt Carlsonb546e462012-02-13 15:20:09 +000011572 if (tp->hw_stats)
11573 tg3_get_estats(tp, (struct tg3_ethtool_stats *)tmp_stats);
11574 else
11575 memset(tmp_stats, 0, sizeof(struct tg3_ethtool_stats));
Linus Torvalds1da177e2005-04-16 15:20:36 -070011576}
11577
Matt Carlson535a4902011-07-20 10:20:56 +000011578static __be32 *tg3_vpd_readblock(struct tg3 *tp, u32 *vpdlen)
Matt Carlsonc3e94502011-04-13 11:05:08 +000011579{
11580 int i;
11581 __be32 *buf;
11582 u32 offset = 0, len = 0;
11583 u32 magic, val;
11584
Joe Perches63c3a662011-04-26 08:12:10 +000011585 if (tg3_flag(tp, NO_NVRAM) || tg3_nvram_read(tp, 0, &magic))
Matt Carlsonc3e94502011-04-13 11:05:08 +000011586 return NULL;
11587
11588 if (magic == TG3_EEPROM_MAGIC) {
11589 for (offset = TG3_NVM_DIR_START;
11590 offset < TG3_NVM_DIR_END;
11591 offset += TG3_NVM_DIRENT_SIZE) {
11592 if (tg3_nvram_read(tp, offset, &val))
11593 return NULL;
11594
11595 if ((val >> TG3_NVM_DIRTYPE_SHIFT) ==
11596 TG3_NVM_DIRTYPE_EXTVPD)
11597 break;
11598 }
11599
11600 if (offset != TG3_NVM_DIR_END) {
11601 len = (val & TG3_NVM_DIRTYPE_LENMSK) * 4;
11602 if (tg3_nvram_read(tp, offset + 4, &offset))
11603 return NULL;
11604
11605 offset = tg3_nvram_logical_addr(tp, offset);
11606 }
11607 }
11608
11609 if (!offset || !len) {
11610 offset = TG3_NVM_VPD_OFF;
11611 len = TG3_NVM_VPD_LEN;
11612 }
11613
11614 buf = kmalloc(len, GFP_KERNEL);
11615 if (buf == NULL)
11616 return NULL;
11617
11618 if (magic == TG3_EEPROM_MAGIC) {
11619 for (i = 0; i < len; i += 4) {
11620 /* The data is in little-endian format in NVRAM.
11621 * Use the big-endian read routines to preserve
11622 * the byte order as it exists in NVRAM.
11623 */
11624 if (tg3_nvram_read_be32(tp, offset + i, &buf[i/4]))
11625 goto error;
11626 }
11627 } else {
11628 u8 *ptr;
11629 ssize_t cnt;
11630 unsigned int pos = 0;
11631
11632 ptr = (u8 *)&buf[0];
11633 for (i = 0; pos < len && i < 3; i++, pos += cnt, ptr += cnt) {
11634 cnt = pci_read_vpd(tp->pdev, pos,
11635 len - pos, ptr);
11636 if (cnt == -ETIMEDOUT || cnt == -EINTR)
11637 cnt = 0;
11638 else if (cnt < 0)
11639 goto error;
11640 }
11641 if (pos != len)
11642 goto error;
11643 }
11644
Matt Carlson535a4902011-07-20 10:20:56 +000011645 *vpdlen = len;
11646
Matt Carlsonc3e94502011-04-13 11:05:08 +000011647 return buf;
11648
11649error:
11650 kfree(buf);
11651 return NULL;
11652}
11653
Michael Chan566f86a2005-05-29 14:56:58 -070011654#define NVRAM_TEST_SIZE 0x100
Matt Carlsona5767de2007-11-12 21:10:58 -080011655#define NVRAM_SELFBOOT_FORMAT1_0_SIZE 0x14
11656#define NVRAM_SELFBOOT_FORMAT1_2_SIZE 0x18
11657#define NVRAM_SELFBOOT_FORMAT1_3_SIZE 0x1c
Matt Carlson727a6d92011-06-13 13:38:58 +000011658#define NVRAM_SELFBOOT_FORMAT1_4_SIZE 0x20
11659#define NVRAM_SELFBOOT_FORMAT1_5_SIZE 0x24
Matt Carlsonbda18fa2011-07-20 10:20:57 +000011660#define NVRAM_SELFBOOT_FORMAT1_6_SIZE 0x50
Michael Chanb16250e2006-09-27 16:10:14 -070011661#define NVRAM_SELFBOOT_HW_SIZE 0x20
11662#define NVRAM_SELFBOOT_DATA_SIZE 0x1c
Michael Chan566f86a2005-05-29 14:56:58 -070011663
11664static int tg3_test_nvram(struct tg3 *tp)
11665{
Matt Carlson535a4902011-07-20 10:20:56 +000011666 u32 csum, magic, len;
Matt Carlsona9dc5292009-02-25 14:25:30 +000011667 __be32 *buf;
Andy Gospodarekab0049b2007-09-06 20:42:14 +010011668 int i, j, k, err = 0, size;
Michael Chan566f86a2005-05-29 14:56:58 -070011669
Joe Perches63c3a662011-04-26 08:12:10 +000011670 if (tg3_flag(tp, NO_NVRAM))
Matt Carlsondf259d82009-04-20 06:57:14 +000011671 return 0;
11672
Matt Carlsone4f34112009-02-25 14:25:00 +000011673 if (tg3_nvram_read(tp, 0, &magic) != 0)
Michael Chan1b277772006-03-20 22:27:48 -080011674 return -EIO;
11675
Michael Chan1b277772006-03-20 22:27:48 -080011676 if (magic == TG3_EEPROM_MAGIC)
11677 size = NVRAM_TEST_SIZE;
Michael Chanb16250e2006-09-27 16:10:14 -070011678 else if ((magic & TG3_EEPROM_MAGIC_FW_MSK) == TG3_EEPROM_MAGIC_FW) {
Matt Carlsona5767de2007-11-12 21:10:58 -080011679 if ((magic & TG3_EEPROM_SB_FORMAT_MASK) ==
11680 TG3_EEPROM_SB_FORMAT_1) {
11681 switch (magic & TG3_EEPROM_SB_REVISION_MASK) {
11682 case TG3_EEPROM_SB_REVISION_0:
11683 size = NVRAM_SELFBOOT_FORMAT1_0_SIZE;
11684 break;
11685 case TG3_EEPROM_SB_REVISION_2:
11686 size = NVRAM_SELFBOOT_FORMAT1_2_SIZE;
11687 break;
11688 case TG3_EEPROM_SB_REVISION_3:
11689 size = NVRAM_SELFBOOT_FORMAT1_3_SIZE;
11690 break;
Matt Carlson727a6d92011-06-13 13:38:58 +000011691 case TG3_EEPROM_SB_REVISION_4:
11692 size = NVRAM_SELFBOOT_FORMAT1_4_SIZE;
11693 break;
11694 case TG3_EEPROM_SB_REVISION_5:
11695 size = NVRAM_SELFBOOT_FORMAT1_5_SIZE;
11696 break;
11697 case TG3_EEPROM_SB_REVISION_6:
11698 size = NVRAM_SELFBOOT_FORMAT1_6_SIZE;
11699 break;
Matt Carlsona5767de2007-11-12 21:10:58 -080011700 default:
Matt Carlson727a6d92011-06-13 13:38:58 +000011701 return -EIO;
Matt Carlsona5767de2007-11-12 21:10:58 -080011702 }
11703 } else
Michael Chan1b277772006-03-20 22:27:48 -080011704 return 0;
Michael Chanb16250e2006-09-27 16:10:14 -070011705 } else if ((magic & TG3_EEPROM_MAGIC_HW_MSK) == TG3_EEPROM_MAGIC_HW)
11706 size = NVRAM_SELFBOOT_HW_SIZE;
11707 else
Michael Chan1b277772006-03-20 22:27:48 -080011708 return -EIO;
11709
11710 buf = kmalloc(size, GFP_KERNEL);
Michael Chan566f86a2005-05-29 14:56:58 -070011711 if (buf == NULL)
11712 return -ENOMEM;
11713
Michael Chan1b277772006-03-20 22:27:48 -080011714 err = -EIO;
11715 for (i = 0, j = 0; i < size; i += 4, j++) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000011716 err = tg3_nvram_read_be32(tp, i, &buf[j]);
11717 if (err)
Michael Chan566f86a2005-05-29 14:56:58 -070011718 break;
Michael Chan566f86a2005-05-29 14:56:58 -070011719 }
Michael Chan1b277772006-03-20 22:27:48 -080011720 if (i < size)
Michael Chan566f86a2005-05-29 14:56:58 -070011721 goto out;
11722
Michael Chan1b277772006-03-20 22:27:48 -080011723 /* Selfboot format */
Matt Carlsona9dc5292009-02-25 14:25:30 +000011724 magic = be32_to_cpu(buf[0]);
Al Virob9fc7dc2007-12-17 22:59:57 -080011725 if ((magic & TG3_EEPROM_MAGIC_FW_MSK) ==
Michael Chanb16250e2006-09-27 16:10:14 -070011726 TG3_EEPROM_MAGIC_FW) {
Michael Chan1b277772006-03-20 22:27:48 -080011727 u8 *buf8 = (u8 *) buf, csum8 = 0;
11728
Al Virob9fc7dc2007-12-17 22:59:57 -080011729 if ((magic & TG3_EEPROM_SB_REVISION_MASK) ==
Matt Carlsona5767de2007-11-12 21:10:58 -080011730 TG3_EEPROM_SB_REVISION_2) {
11731 /* For rev 2, the csum doesn't include the MBA. */
11732 for (i = 0; i < TG3_EEPROM_SB_F1R2_MBA_OFF; i++)
11733 csum8 += buf8[i];
11734 for (i = TG3_EEPROM_SB_F1R2_MBA_OFF + 4; i < size; i++)
11735 csum8 += buf8[i];
11736 } else {
11737 for (i = 0; i < size; i++)
11738 csum8 += buf8[i];
11739 }
Michael Chan1b277772006-03-20 22:27:48 -080011740
Adrian Bunkad96b482006-04-05 22:21:04 -070011741 if (csum8 == 0) {
11742 err = 0;
11743 goto out;
11744 }
11745
11746 err = -EIO;
11747 goto out;
Michael Chan1b277772006-03-20 22:27:48 -080011748 }
Michael Chan566f86a2005-05-29 14:56:58 -070011749
Al Virob9fc7dc2007-12-17 22:59:57 -080011750 if ((magic & TG3_EEPROM_MAGIC_HW_MSK) ==
Michael Chanb16250e2006-09-27 16:10:14 -070011751 TG3_EEPROM_MAGIC_HW) {
11752 u8 data[NVRAM_SELFBOOT_DATA_SIZE];
Matt Carlsona9dc5292009-02-25 14:25:30 +000011753 u8 parity[NVRAM_SELFBOOT_DATA_SIZE];
Michael Chanb16250e2006-09-27 16:10:14 -070011754 u8 *buf8 = (u8 *) buf;
Michael Chanb16250e2006-09-27 16:10:14 -070011755
11756 /* Separate the parity bits and the data bytes. */
11757 for (i = 0, j = 0, k = 0; i < NVRAM_SELFBOOT_HW_SIZE; i++) {
11758 if ((i == 0) || (i == 8)) {
11759 int l;
11760 u8 msk;
11761
11762 for (l = 0, msk = 0x80; l < 7; l++, msk >>= 1)
11763 parity[k++] = buf8[i] & msk;
11764 i++;
Matt Carlson859a588792010-04-05 10:19:28 +000011765 } else if (i == 16) {
Michael Chanb16250e2006-09-27 16:10:14 -070011766 int l;
11767 u8 msk;
11768
11769 for (l = 0, msk = 0x20; l < 6; l++, msk >>= 1)
11770 parity[k++] = buf8[i] & msk;
11771 i++;
11772
11773 for (l = 0, msk = 0x80; l < 8; l++, msk >>= 1)
11774 parity[k++] = buf8[i] & msk;
11775 i++;
11776 }
11777 data[j++] = buf8[i];
11778 }
11779
11780 err = -EIO;
11781 for (i = 0; i < NVRAM_SELFBOOT_DATA_SIZE; i++) {
11782 u8 hw8 = hweight8(data[i]);
11783
11784 if ((hw8 & 0x1) && parity[i])
11785 goto out;
11786 else if (!(hw8 & 0x1) && !parity[i])
11787 goto out;
11788 }
11789 err = 0;
11790 goto out;
11791 }
11792
Matt Carlson01c3a392011-03-09 16:58:20 +000011793 err = -EIO;
11794
Michael Chan566f86a2005-05-29 14:56:58 -070011795 /* Bootstrap checksum at offset 0x10 */
11796 csum = calc_crc((unsigned char *) buf, 0x10);
Matt Carlson01c3a392011-03-09 16:58:20 +000011797 if (csum != le32_to_cpu(buf[0x10/4]))
Michael Chan566f86a2005-05-29 14:56:58 -070011798 goto out;
11799
11800 /* Manufacturing block starts at offset 0x74, checksum at 0xfc */
11801 csum = calc_crc((unsigned char *) &buf[0x74/4], 0x88);
Matt Carlson01c3a392011-03-09 16:58:20 +000011802 if (csum != le32_to_cpu(buf[0xfc/4]))
Matt Carlsona9dc5292009-02-25 14:25:30 +000011803 goto out;
Michael Chan566f86a2005-05-29 14:56:58 -070011804
Matt Carlsonc3e94502011-04-13 11:05:08 +000011805 kfree(buf);
11806
Matt Carlson535a4902011-07-20 10:20:56 +000011807 buf = tg3_vpd_readblock(tp, &len);
Matt Carlsonc3e94502011-04-13 11:05:08 +000011808 if (!buf)
11809 return -ENOMEM;
Matt Carlsond4894f32011-03-09 16:58:21 +000011810
Matt Carlson535a4902011-07-20 10:20:56 +000011811 i = pci_vpd_find_tag((u8 *)buf, 0, len, PCI_VPD_LRDT_RO_DATA);
Matt Carlsond4894f32011-03-09 16:58:21 +000011812 if (i > 0) {
11813 j = pci_vpd_lrdt_size(&((u8 *)buf)[i]);
11814 if (j < 0)
11815 goto out;
11816
Matt Carlson535a4902011-07-20 10:20:56 +000011817 if (i + PCI_VPD_LRDT_TAG_SIZE + j > len)
Matt Carlsond4894f32011-03-09 16:58:21 +000011818 goto out;
11819
11820 i += PCI_VPD_LRDT_TAG_SIZE;
11821 j = pci_vpd_find_info_keyword((u8 *)buf, i, j,
11822 PCI_VPD_RO_KEYWORD_CHKSUM);
11823 if (j > 0) {
11824 u8 csum8 = 0;
11825
11826 j += PCI_VPD_INFO_FLD_HDR_SIZE;
11827
11828 for (i = 0; i <= j; i++)
11829 csum8 += ((u8 *)buf)[i];
11830
11831 if (csum8)
11832 goto out;
11833 }
11834 }
11835
Michael Chan566f86a2005-05-29 14:56:58 -070011836 err = 0;
11837
11838out:
11839 kfree(buf);
11840 return err;
11841}
11842
Michael Chanca430072005-05-29 14:57:23 -070011843#define TG3_SERDES_TIMEOUT_SEC 2
11844#define TG3_COPPER_TIMEOUT_SEC 6
11845
11846static int tg3_test_link(struct tg3 *tp)
11847{
11848 int i, max;
11849
11850 if (!netif_running(tp->dev))
11851 return -ENODEV;
11852
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011853 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
Michael Chanca430072005-05-29 14:57:23 -070011854 max = TG3_SERDES_TIMEOUT_SEC;
11855 else
11856 max = TG3_COPPER_TIMEOUT_SEC;
11857
11858 for (i = 0; i < max; i++) {
Nithin Nayak Sujirf4a46d12012-11-14 14:44:27 +000011859 if (tp->link_up)
Michael Chanca430072005-05-29 14:57:23 -070011860 return 0;
11861
11862 if (msleep_interruptible(1000))
11863 break;
11864 }
11865
11866 return -EIO;
11867}
11868
Michael Chana71116d2005-05-29 14:58:11 -070011869/* Only test the commonly used registers */
David S. Miller30ca3e32006-03-20 23:02:36 -080011870static int tg3_test_registers(struct tg3 *tp)
Michael Chana71116d2005-05-29 14:58:11 -070011871{
Michael Chanb16250e2006-09-27 16:10:14 -070011872 int i, is_5705, is_5750;
Michael Chana71116d2005-05-29 14:58:11 -070011873 u32 offset, read_mask, write_mask, val, save_val, read_val;
11874 static struct {
11875 u16 offset;
11876 u16 flags;
11877#define TG3_FL_5705 0x1
11878#define TG3_FL_NOT_5705 0x2
11879#define TG3_FL_NOT_5788 0x4
Michael Chanb16250e2006-09-27 16:10:14 -070011880#define TG3_FL_NOT_5750 0x8
Michael Chana71116d2005-05-29 14:58:11 -070011881 u32 read_mask;
11882 u32 write_mask;
11883 } reg_tbl[] = {
11884 /* MAC Control Registers */
11885 { MAC_MODE, TG3_FL_NOT_5705,
11886 0x00000000, 0x00ef6f8c },
11887 { MAC_MODE, TG3_FL_5705,
11888 0x00000000, 0x01ef6b8c },
11889 { MAC_STATUS, TG3_FL_NOT_5705,
11890 0x03800107, 0x00000000 },
11891 { MAC_STATUS, TG3_FL_5705,
11892 0x03800100, 0x00000000 },
11893 { MAC_ADDR_0_HIGH, 0x0000,
11894 0x00000000, 0x0000ffff },
11895 { MAC_ADDR_0_LOW, 0x0000,
Matt Carlsonc6cdf432010-04-05 10:19:26 +000011896 0x00000000, 0xffffffff },
Michael Chana71116d2005-05-29 14:58:11 -070011897 { MAC_RX_MTU_SIZE, 0x0000,
11898 0x00000000, 0x0000ffff },
11899 { MAC_TX_MODE, 0x0000,
11900 0x00000000, 0x00000070 },
11901 { MAC_TX_LENGTHS, 0x0000,
11902 0x00000000, 0x00003fff },
11903 { MAC_RX_MODE, TG3_FL_NOT_5705,
11904 0x00000000, 0x000007fc },
11905 { MAC_RX_MODE, TG3_FL_5705,
11906 0x00000000, 0x000007dc },
11907 { MAC_HASH_REG_0, 0x0000,
11908 0x00000000, 0xffffffff },
11909 { MAC_HASH_REG_1, 0x0000,
11910 0x00000000, 0xffffffff },
11911 { MAC_HASH_REG_2, 0x0000,
11912 0x00000000, 0xffffffff },
11913 { MAC_HASH_REG_3, 0x0000,
11914 0x00000000, 0xffffffff },
11915
11916 /* Receive Data and Receive BD Initiator Control Registers. */
11917 { RCVDBDI_JUMBO_BD+0, TG3_FL_NOT_5705,
11918 0x00000000, 0xffffffff },
11919 { RCVDBDI_JUMBO_BD+4, TG3_FL_NOT_5705,
11920 0x00000000, 0xffffffff },
11921 { RCVDBDI_JUMBO_BD+8, TG3_FL_NOT_5705,
11922 0x00000000, 0x00000003 },
11923 { RCVDBDI_JUMBO_BD+0xc, TG3_FL_NOT_5705,
11924 0x00000000, 0xffffffff },
11925 { RCVDBDI_STD_BD+0, 0x0000,
11926 0x00000000, 0xffffffff },
11927 { RCVDBDI_STD_BD+4, 0x0000,
11928 0x00000000, 0xffffffff },
11929 { RCVDBDI_STD_BD+8, 0x0000,
11930 0x00000000, 0xffff0002 },
11931 { RCVDBDI_STD_BD+0xc, 0x0000,
11932 0x00000000, 0xffffffff },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011933
Michael Chana71116d2005-05-29 14:58:11 -070011934 /* Receive BD Initiator Control Registers. */
11935 { RCVBDI_STD_THRESH, TG3_FL_NOT_5705,
11936 0x00000000, 0xffffffff },
11937 { RCVBDI_STD_THRESH, TG3_FL_5705,
11938 0x00000000, 0x000003ff },
11939 { RCVBDI_JUMBO_THRESH, TG3_FL_NOT_5705,
11940 0x00000000, 0xffffffff },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011941
Michael Chana71116d2005-05-29 14:58:11 -070011942 /* Host Coalescing Control Registers. */
11943 { HOSTCC_MODE, TG3_FL_NOT_5705,
11944 0x00000000, 0x00000004 },
11945 { HOSTCC_MODE, TG3_FL_5705,
11946 0x00000000, 0x000000f6 },
11947 { HOSTCC_RXCOL_TICKS, TG3_FL_NOT_5705,
11948 0x00000000, 0xffffffff },
11949 { HOSTCC_RXCOL_TICKS, TG3_FL_5705,
11950 0x00000000, 0x000003ff },
11951 { HOSTCC_TXCOL_TICKS, TG3_FL_NOT_5705,
11952 0x00000000, 0xffffffff },
11953 { HOSTCC_TXCOL_TICKS, TG3_FL_5705,
11954 0x00000000, 0x000003ff },
11955 { HOSTCC_RXMAX_FRAMES, TG3_FL_NOT_5705,
11956 0x00000000, 0xffffffff },
11957 { HOSTCC_RXMAX_FRAMES, TG3_FL_5705 | TG3_FL_NOT_5788,
11958 0x00000000, 0x000000ff },
11959 { HOSTCC_TXMAX_FRAMES, TG3_FL_NOT_5705,
11960 0x00000000, 0xffffffff },
11961 { HOSTCC_TXMAX_FRAMES, TG3_FL_5705 | TG3_FL_NOT_5788,
11962 0x00000000, 0x000000ff },
11963 { HOSTCC_RXCOAL_TICK_INT, TG3_FL_NOT_5705,
11964 0x00000000, 0xffffffff },
11965 { HOSTCC_TXCOAL_TICK_INT, TG3_FL_NOT_5705,
11966 0x00000000, 0xffffffff },
11967 { HOSTCC_RXCOAL_MAXF_INT, TG3_FL_NOT_5705,
11968 0x00000000, 0xffffffff },
11969 { HOSTCC_RXCOAL_MAXF_INT, TG3_FL_5705 | TG3_FL_NOT_5788,
11970 0x00000000, 0x000000ff },
11971 { HOSTCC_TXCOAL_MAXF_INT, TG3_FL_NOT_5705,
11972 0x00000000, 0xffffffff },
11973 { HOSTCC_TXCOAL_MAXF_INT, TG3_FL_5705 | TG3_FL_NOT_5788,
11974 0x00000000, 0x000000ff },
11975 { HOSTCC_STAT_COAL_TICKS, TG3_FL_NOT_5705,
11976 0x00000000, 0xffffffff },
11977 { HOSTCC_STATS_BLK_HOST_ADDR, TG3_FL_NOT_5705,
11978 0x00000000, 0xffffffff },
11979 { HOSTCC_STATS_BLK_HOST_ADDR+4, TG3_FL_NOT_5705,
11980 0x00000000, 0xffffffff },
11981 { HOSTCC_STATUS_BLK_HOST_ADDR, 0x0000,
11982 0x00000000, 0xffffffff },
11983 { HOSTCC_STATUS_BLK_HOST_ADDR+4, 0x0000,
11984 0x00000000, 0xffffffff },
11985 { HOSTCC_STATS_BLK_NIC_ADDR, 0x0000,
11986 0xffffffff, 0x00000000 },
11987 { HOSTCC_STATUS_BLK_NIC_ADDR, 0x0000,
11988 0xffffffff, 0x00000000 },
11989
11990 /* Buffer Manager Control Registers. */
Michael Chanb16250e2006-09-27 16:10:14 -070011991 { BUFMGR_MB_POOL_ADDR, TG3_FL_NOT_5750,
Michael Chana71116d2005-05-29 14:58:11 -070011992 0x00000000, 0x007fff80 },
Michael Chanb16250e2006-09-27 16:10:14 -070011993 { BUFMGR_MB_POOL_SIZE, TG3_FL_NOT_5750,
Michael Chana71116d2005-05-29 14:58:11 -070011994 0x00000000, 0x007fffff },
11995 { BUFMGR_MB_RDMA_LOW_WATER, 0x0000,
11996 0x00000000, 0x0000003f },
11997 { BUFMGR_MB_MACRX_LOW_WATER, 0x0000,
11998 0x00000000, 0x000001ff },
11999 { BUFMGR_MB_HIGH_WATER, 0x0000,
12000 0x00000000, 0x000001ff },
12001 { BUFMGR_DMA_DESC_POOL_ADDR, TG3_FL_NOT_5705,
12002 0xffffffff, 0x00000000 },
12003 { BUFMGR_DMA_DESC_POOL_SIZE, TG3_FL_NOT_5705,
12004 0xffffffff, 0x00000000 },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012005
Michael Chana71116d2005-05-29 14:58:11 -070012006 /* Mailbox Registers */
12007 { GRCMBOX_RCVSTD_PROD_IDX+4, 0x0000,
12008 0x00000000, 0x000001ff },
12009 { GRCMBOX_RCVJUMBO_PROD_IDX+4, TG3_FL_NOT_5705,
12010 0x00000000, 0x000001ff },
12011 { GRCMBOX_RCVRET_CON_IDX_0+4, 0x0000,
12012 0x00000000, 0x000007ff },
12013 { GRCMBOX_SNDHOST_PROD_IDX_0+4, 0x0000,
12014 0x00000000, 0x000001ff },
12015
12016 { 0xffff, 0x0000, 0x00000000, 0x00000000 },
12017 };
12018
Michael Chanb16250e2006-09-27 16:10:14 -070012019 is_5705 = is_5750 = 0;
Joe Perches63c3a662011-04-26 08:12:10 +000012020 if (tg3_flag(tp, 5705_PLUS)) {
Michael Chana71116d2005-05-29 14:58:11 -070012021 is_5705 = 1;
Joe Perches63c3a662011-04-26 08:12:10 +000012022 if (tg3_flag(tp, 5750_PLUS))
Michael Chanb16250e2006-09-27 16:10:14 -070012023 is_5750 = 1;
12024 }
Michael Chana71116d2005-05-29 14:58:11 -070012025
12026 for (i = 0; reg_tbl[i].offset != 0xffff; i++) {
12027 if (is_5705 && (reg_tbl[i].flags & TG3_FL_NOT_5705))
12028 continue;
12029
12030 if (!is_5705 && (reg_tbl[i].flags & TG3_FL_5705))
12031 continue;
12032
Joe Perches63c3a662011-04-26 08:12:10 +000012033 if (tg3_flag(tp, IS_5788) &&
Michael Chana71116d2005-05-29 14:58:11 -070012034 (reg_tbl[i].flags & TG3_FL_NOT_5788))
12035 continue;
12036
Michael Chanb16250e2006-09-27 16:10:14 -070012037 if (is_5750 && (reg_tbl[i].flags & TG3_FL_NOT_5750))
12038 continue;
12039
Michael Chana71116d2005-05-29 14:58:11 -070012040 offset = (u32) reg_tbl[i].offset;
12041 read_mask = reg_tbl[i].read_mask;
12042 write_mask = reg_tbl[i].write_mask;
12043
12044 /* Save the original register content */
12045 save_val = tr32(offset);
12046
12047 /* Determine the read-only value. */
12048 read_val = save_val & read_mask;
12049
12050 /* Write zero to the register, then make sure the read-only bits
12051 * are not changed and the read/write bits are all zeros.
12052 */
12053 tw32(offset, 0);
12054
12055 val = tr32(offset);
12056
12057 /* Test the read-only and read/write bits. */
12058 if (((val & read_mask) != read_val) || (val & write_mask))
12059 goto out;
12060
12061 /* Write ones to all the bits defined by RdMask and WrMask, then
12062 * make sure the read-only bits are not changed and the
12063 * read/write bits are all ones.
12064 */
12065 tw32(offset, read_mask | write_mask);
12066
12067 val = tr32(offset);
12068
12069 /* Test the read-only bits. */
12070 if ((val & read_mask) != read_val)
12071 goto out;
12072
12073 /* Test the read/write bits. */
12074 if ((val & write_mask) != write_mask)
12075 goto out;
12076
12077 tw32(offset, save_val);
12078 }
12079
12080 return 0;
12081
12082out:
Michael Chan9f88f292006-12-07 00:22:54 -080012083 if (netif_msg_hw(tp))
Matt Carlson2445e462010-04-05 10:19:21 +000012084 netdev_err(tp->dev,
12085 "Register test failed at offset %x\n", offset);
Michael Chana71116d2005-05-29 14:58:11 -070012086 tw32(offset, save_val);
12087 return -EIO;
12088}
12089
Michael Chan7942e1d2005-05-29 14:58:36 -070012090static int tg3_do_mem_test(struct tg3 *tp, u32 offset, u32 len)
12091{
Arjan van de Venf71e1302006-03-03 21:33:57 -050012092 static const u32 test_pattern[] = { 0x00000000, 0xffffffff, 0xaa55a55a };
Michael Chan7942e1d2005-05-29 14:58:36 -070012093 int i;
12094 u32 j;
12095
Alejandro Martinez Ruize9edda62007-10-15 03:37:43 +020012096 for (i = 0; i < ARRAY_SIZE(test_pattern); i++) {
Michael Chan7942e1d2005-05-29 14:58:36 -070012097 for (j = 0; j < len; j += 4) {
12098 u32 val;
12099
12100 tg3_write_mem(tp, offset + j, test_pattern[i]);
12101 tg3_read_mem(tp, offset + j, &val);
12102 if (val != test_pattern[i])
12103 return -EIO;
12104 }
12105 }
12106 return 0;
12107}
12108
12109static int tg3_test_memory(struct tg3 *tp)
12110{
12111 static struct mem_entry {
12112 u32 offset;
12113 u32 len;
12114 } mem_tbl_570x[] = {
Michael Chan38690192005-12-19 16:27:28 -080012115 { 0x00000000, 0x00b50},
Michael Chan7942e1d2005-05-29 14:58:36 -070012116 { 0x00002000, 0x1c000},
12117 { 0xffffffff, 0x00000}
12118 }, mem_tbl_5705[] = {
12119 { 0x00000100, 0x0000c},
12120 { 0x00000200, 0x00008},
Michael Chan7942e1d2005-05-29 14:58:36 -070012121 { 0x00004000, 0x00800},
12122 { 0x00006000, 0x01000},
12123 { 0x00008000, 0x02000},
12124 { 0x00010000, 0x0e000},
12125 { 0xffffffff, 0x00000}
Michael Chan79f4d132006-03-20 22:28:57 -080012126 }, mem_tbl_5755[] = {
12127 { 0x00000200, 0x00008},
12128 { 0x00004000, 0x00800},
12129 { 0x00006000, 0x00800},
12130 { 0x00008000, 0x02000},
12131 { 0x00010000, 0x0c000},
12132 { 0xffffffff, 0x00000}
Michael Chanb16250e2006-09-27 16:10:14 -070012133 }, mem_tbl_5906[] = {
12134 { 0x00000200, 0x00008},
12135 { 0x00004000, 0x00400},
12136 { 0x00006000, 0x00400},
12137 { 0x00008000, 0x01000},
12138 { 0x00010000, 0x01000},
12139 { 0xffffffff, 0x00000}
Matt Carlson8b5a6c42010-01-20 16:58:06 +000012140 }, mem_tbl_5717[] = {
12141 { 0x00000200, 0x00008},
12142 { 0x00010000, 0x0a000},
12143 { 0x00020000, 0x13c00},
12144 { 0xffffffff, 0x00000}
12145 }, mem_tbl_57765[] = {
12146 { 0x00000200, 0x00008},
12147 { 0x00004000, 0x00800},
12148 { 0x00006000, 0x09800},
12149 { 0x00010000, 0x0a000},
12150 { 0xffffffff, 0x00000}
Michael Chan7942e1d2005-05-29 14:58:36 -070012151 };
12152 struct mem_entry *mem_tbl;
12153 int err = 0;
12154 int i;
12155
Joe Perches63c3a662011-04-26 08:12:10 +000012156 if (tg3_flag(tp, 5717_PLUS))
Matt Carlson8b5a6c42010-01-20 16:58:06 +000012157 mem_tbl = mem_tbl_5717;
Matt Carlson55086ad2011-12-14 11:09:59 +000012158 else if (tg3_flag(tp, 57765_CLASS))
Matt Carlson8b5a6c42010-01-20 16:58:06 +000012159 mem_tbl = mem_tbl_57765;
Joe Perches63c3a662011-04-26 08:12:10 +000012160 else if (tg3_flag(tp, 5755_PLUS))
Matt Carlson321d32a2008-11-21 17:22:19 -080012161 mem_tbl = mem_tbl_5755;
12162 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
12163 mem_tbl = mem_tbl_5906;
Joe Perches63c3a662011-04-26 08:12:10 +000012164 else if (tg3_flag(tp, 5705_PLUS))
Matt Carlson321d32a2008-11-21 17:22:19 -080012165 mem_tbl = mem_tbl_5705;
12166 else
Michael Chan7942e1d2005-05-29 14:58:36 -070012167 mem_tbl = mem_tbl_570x;
12168
12169 for (i = 0; mem_tbl[i].offset != 0xffffffff; i++) {
Matt Carlsonbe98da62010-07-11 09:31:46 +000012170 err = tg3_do_mem_test(tp, mem_tbl[i].offset, mem_tbl[i].len);
12171 if (err)
Michael Chan7942e1d2005-05-29 14:58:36 -070012172 break;
12173 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012174
Michael Chan7942e1d2005-05-29 14:58:36 -070012175 return err;
12176}
12177
Matt Carlsonbb158d62011-04-25 12:42:47 +000012178#define TG3_TSO_MSS 500
12179
12180#define TG3_TSO_IP_HDR_LEN 20
12181#define TG3_TSO_TCP_HDR_LEN 20
12182#define TG3_TSO_TCP_OPT_LEN 12
12183
12184static const u8 tg3_tso_header[] = {
121850x08, 0x00,
121860x45, 0x00, 0x00, 0x00,
121870x00, 0x00, 0x40, 0x00,
121880x40, 0x06, 0x00, 0x00,
121890x0a, 0x00, 0x00, 0x01,
121900x0a, 0x00, 0x00, 0x02,
121910x0d, 0x00, 0xe0, 0x00,
121920x00, 0x00, 0x01, 0x00,
121930x00, 0x00, 0x02, 0x00,
121940x80, 0x10, 0x10, 0x00,
121950x14, 0x09, 0x00, 0x00,
121960x01, 0x01, 0x08, 0x0a,
121970x11, 0x11, 0x11, 0x11,
121980x11, 0x11, 0x11, 0x11,
12199};
Michael Chan9f40dea2005-09-05 17:53:06 -070012200
Matt Carlson28a45952011-08-19 13:58:22 +000012201static int tg3_run_loopback(struct tg3 *tp, u32 pktsz, bool tso_loopback)
Michael Chanc76949a2005-05-29 14:58:59 -070012202{
Matt Carlson5e5a7f32011-08-19 13:58:21 +000012203 u32 rx_start_idx, rx_idx, tx_idx, opaque_key;
Matt Carlsonbb158d62011-04-25 12:42:47 +000012204 u32 base_flags = 0, mss = 0, desc_idx, coal_now, data_off, val;
Matt Carlson84b67b22011-07-27 14:20:52 +000012205 u32 budget;
Eric Dumazet9205fd92011-11-18 06:47:01 +000012206 struct sk_buff *skb;
12207 u8 *tx_data, *rx_data;
Michael Chanc76949a2005-05-29 14:58:59 -070012208 dma_addr_t map;
12209 int num_pkts, tx_len, rx_len, i, err;
12210 struct tg3_rx_buffer_desc *desc;
Matt Carlson898a56f2009-08-28 14:02:40 +000012211 struct tg3_napi *tnapi, *rnapi;
Matt Carlson8fea32b2010-09-15 08:59:58 +000012212 struct tg3_rx_prodring_set *tpr = &tp->napi[0].prodring;
Michael Chanc76949a2005-05-29 14:58:59 -070012213
Matt Carlsonc8873402010-02-12 14:47:11 +000012214 tnapi = &tp->napi[0];
12215 rnapi = &tp->napi[0];
Matt Carlson0c1d0e22009-09-01 13:16:33 +000012216 if (tp->irq_cnt > 1) {
Joe Perches63c3a662011-04-26 08:12:10 +000012217 if (tg3_flag(tp, ENABLE_RSS))
Matt Carlson1da85aa2010-09-30 10:34:34 +000012218 rnapi = &tp->napi[1];
Joe Perches63c3a662011-04-26 08:12:10 +000012219 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlsonc8873402010-02-12 14:47:11 +000012220 tnapi = &tp->napi[1];
Matt Carlson0c1d0e22009-09-01 13:16:33 +000012221 }
Matt Carlsonfd2ce372009-09-01 12:51:13 +000012222 coal_now = tnapi->coal_now | rnapi->coal_now;
Matt Carlson898a56f2009-08-28 14:02:40 +000012223
Michael Chanc76949a2005-05-29 14:58:59 -070012224 err = -EIO;
12225
Matt Carlson4852a862011-04-13 11:05:07 +000012226 tx_len = pktsz;
David S. Millera20e9c62006-07-31 22:38:16 -070012227 skb = netdev_alloc_skb(tp->dev, tx_len);
Jesper Juhla50bb7b2006-05-09 23:14:35 -070012228 if (!skb)
12229 return -ENOMEM;
12230
Michael Chanc76949a2005-05-29 14:58:59 -070012231 tx_data = skb_put(skb, tx_len);
12232 memcpy(tx_data, tp->dev->dev_addr, 6);
12233 memset(tx_data + 6, 0x0, 8);
12234
Matt Carlson4852a862011-04-13 11:05:07 +000012235 tw32(MAC_RX_MTU_SIZE, tx_len + ETH_FCS_LEN);
Michael Chanc76949a2005-05-29 14:58:59 -070012236
Matt Carlson28a45952011-08-19 13:58:22 +000012237 if (tso_loopback) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000012238 struct iphdr *iph = (struct iphdr *)&tx_data[ETH_HLEN];
12239
12240 u32 hdr_len = TG3_TSO_IP_HDR_LEN + TG3_TSO_TCP_HDR_LEN +
12241 TG3_TSO_TCP_OPT_LEN;
12242
12243 memcpy(tx_data + ETH_ALEN * 2, tg3_tso_header,
12244 sizeof(tg3_tso_header));
12245 mss = TG3_TSO_MSS;
12246
12247 val = tx_len - ETH_ALEN * 2 - sizeof(tg3_tso_header);
12248 num_pkts = DIV_ROUND_UP(val, TG3_TSO_MSS);
12249
12250 /* Set the total length field in the IP header */
12251 iph->tot_len = htons((u16)(mss + hdr_len));
12252
12253 base_flags = (TXD_FLAG_CPU_PRE_DMA |
12254 TXD_FLAG_CPU_POST_DMA);
12255
Joe Perches63c3a662011-04-26 08:12:10 +000012256 if (tg3_flag(tp, HW_TSO_1) ||
12257 tg3_flag(tp, HW_TSO_2) ||
12258 tg3_flag(tp, HW_TSO_3)) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000012259 struct tcphdr *th;
12260 val = ETH_HLEN + TG3_TSO_IP_HDR_LEN;
12261 th = (struct tcphdr *)&tx_data[val];
12262 th->check = 0;
12263 } else
12264 base_flags |= TXD_FLAG_TCPUDP_CSUM;
12265
Joe Perches63c3a662011-04-26 08:12:10 +000012266 if (tg3_flag(tp, HW_TSO_3)) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000012267 mss |= (hdr_len & 0xc) << 12;
12268 if (hdr_len & 0x10)
12269 base_flags |= 0x00000010;
12270 base_flags |= (hdr_len & 0x3e0) << 5;
Joe Perches63c3a662011-04-26 08:12:10 +000012271 } else if (tg3_flag(tp, HW_TSO_2))
Matt Carlsonbb158d62011-04-25 12:42:47 +000012272 mss |= hdr_len << 9;
Joe Perches63c3a662011-04-26 08:12:10 +000012273 else if (tg3_flag(tp, HW_TSO_1) ||
Matt Carlsonbb158d62011-04-25 12:42:47 +000012274 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
12275 mss |= (TG3_TSO_TCP_OPT_LEN << 9);
12276 } else {
12277 base_flags |= (TG3_TSO_TCP_OPT_LEN << 10);
12278 }
12279
12280 data_off = ETH_ALEN * 2 + sizeof(tg3_tso_header);
12281 } else {
12282 num_pkts = 1;
12283 data_off = ETH_HLEN;
Michael Chanc441b452012-03-04 14:48:13 +000012284
12285 if (tg3_flag(tp, USE_JUMBO_BDFLAG) &&
12286 tx_len > VLAN_ETH_FRAME_LEN)
12287 base_flags |= TXD_FLAG_JMB_PKT;
Matt Carlsonbb158d62011-04-25 12:42:47 +000012288 }
12289
12290 for (i = data_off; i < tx_len; i++)
Michael Chanc76949a2005-05-29 14:58:59 -070012291 tx_data[i] = (u8) (i & 0xff);
12292
Alexander Duyckf4188d82009-12-02 16:48:38 +000012293 map = pci_map_single(tp->pdev, skb->data, tx_len, PCI_DMA_TODEVICE);
12294 if (pci_dma_mapping_error(tp->pdev, map)) {
Matt Carlsona21771d2009-11-02 14:25:31 +000012295 dev_kfree_skb(skb);
12296 return -EIO;
12297 }
Michael Chanc76949a2005-05-29 14:58:59 -070012298
Matt Carlson0d681b22011-07-27 14:20:49 +000012299 val = tnapi->tx_prod;
12300 tnapi->tx_buffers[val].skb = skb;
12301 dma_unmap_addr_set(&tnapi->tx_buffers[val], mapping, map);
12302
Michael Chanc76949a2005-05-29 14:58:59 -070012303 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +000012304 rnapi->coal_now);
Michael Chanc76949a2005-05-29 14:58:59 -070012305
12306 udelay(10);
12307
Matt Carlson898a56f2009-08-28 14:02:40 +000012308 rx_start_idx = rnapi->hw_status->idx[0].rx_producer;
Michael Chanc76949a2005-05-29 14:58:59 -070012309
Matt Carlson84b67b22011-07-27 14:20:52 +000012310 budget = tg3_tx_avail(tnapi);
12311 if (tg3_tx_frag_set(tnapi, &val, &budget, map, tx_len,
Matt Carlsond1a3b732011-07-27 14:20:51 +000012312 base_flags | TXD_FLAG_END, mss, 0)) {
12313 tnapi->tx_buffers[val].skb = NULL;
12314 dev_kfree_skb(skb);
12315 return -EIO;
12316 }
Michael Chanc76949a2005-05-29 14:58:59 -070012317
Matt Carlsonf3f3f272009-08-28 14:03:21 +000012318 tnapi->tx_prod++;
Michael Chanc76949a2005-05-29 14:58:59 -070012319
Michael Chan6541b802012-03-04 14:48:14 +000012320 /* Sync BD data before updating mailbox */
12321 wmb();
12322
Matt Carlsonf3f3f272009-08-28 14:03:21 +000012323 tw32_tx_mbox(tnapi->prodmbox, tnapi->tx_prod);
12324 tr32_mailbox(tnapi->prodmbox);
Michael Chanc76949a2005-05-29 14:58:59 -070012325
12326 udelay(10);
12327
Matt Carlson303fc922009-11-02 14:27:34 +000012328 /* 350 usec to allow enough time on some 10/100 Mbps devices. */
12329 for (i = 0; i < 35; i++) {
Michael Chanc76949a2005-05-29 14:58:59 -070012330 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +000012331 coal_now);
Michael Chanc76949a2005-05-29 14:58:59 -070012332
12333 udelay(10);
12334
Matt Carlson898a56f2009-08-28 14:02:40 +000012335 tx_idx = tnapi->hw_status->idx[0].tx_consumer;
12336 rx_idx = rnapi->hw_status->idx[0].rx_producer;
Matt Carlsonf3f3f272009-08-28 14:03:21 +000012337 if ((tx_idx == tnapi->tx_prod) &&
Michael Chanc76949a2005-05-29 14:58:59 -070012338 (rx_idx == (rx_start_idx + num_pkts)))
12339 break;
12340 }
12341
Matt Carlsonba1142e2011-11-04 09:15:00 +000012342 tg3_tx_skb_unmap(tnapi, tnapi->tx_prod - 1, -1);
Michael Chanc76949a2005-05-29 14:58:59 -070012343 dev_kfree_skb(skb);
12344
Matt Carlsonf3f3f272009-08-28 14:03:21 +000012345 if (tx_idx != tnapi->tx_prod)
Michael Chanc76949a2005-05-29 14:58:59 -070012346 goto out;
12347
12348 if (rx_idx != rx_start_idx + num_pkts)
12349 goto out;
12350
Matt Carlsonbb158d62011-04-25 12:42:47 +000012351 val = data_off;
12352 while (rx_idx != rx_start_idx) {
12353 desc = &rnapi->rx_rcb[rx_start_idx++];
12354 desc_idx = desc->opaque & RXD_OPAQUE_INDEX_MASK;
12355 opaque_key = desc->opaque & RXD_OPAQUE_RING_MASK;
Michael Chanc76949a2005-05-29 14:58:59 -070012356
Matt Carlsonbb158d62011-04-25 12:42:47 +000012357 if ((desc->err_vlan & RXD_ERR_MASK) != 0 &&
12358 (desc->err_vlan != RXD_ERR_ODD_NIBBLE_RCVD_MII))
Matt Carlson4852a862011-04-13 11:05:07 +000012359 goto out;
Michael Chanc76949a2005-05-29 14:58:59 -070012360
Matt Carlsonbb158d62011-04-25 12:42:47 +000012361 rx_len = ((desc->idx_len & RXD_LEN_MASK) >> RXD_LEN_SHIFT)
12362 - ETH_FCS_LEN;
12363
Matt Carlson28a45952011-08-19 13:58:22 +000012364 if (!tso_loopback) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000012365 if (rx_len != tx_len)
12366 goto out;
12367
12368 if (pktsz <= TG3_RX_STD_DMA_SZ - ETH_FCS_LEN) {
12369 if (opaque_key != RXD_OPAQUE_RING_STD)
12370 goto out;
12371 } else {
12372 if (opaque_key != RXD_OPAQUE_RING_JUMBO)
12373 goto out;
12374 }
12375 } else if ((desc->type_flags & RXD_FLAG_TCPUDP_CSUM) &&
12376 (desc->ip_tcp_csum & RXD_TCPCSUM_MASK)
Matt Carlson54e0a672011-05-19 12:12:50 +000012377 >> RXD_TCPCSUM_SHIFT != 0xffff) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000012378 goto out;
12379 }
12380
12381 if (opaque_key == RXD_OPAQUE_RING_STD) {
Eric Dumazet9205fd92011-11-18 06:47:01 +000012382 rx_data = tpr->rx_std_buffers[desc_idx].data;
Matt Carlsonbb158d62011-04-25 12:42:47 +000012383 map = dma_unmap_addr(&tpr->rx_std_buffers[desc_idx],
12384 mapping);
12385 } else if (opaque_key == RXD_OPAQUE_RING_JUMBO) {
Eric Dumazet9205fd92011-11-18 06:47:01 +000012386 rx_data = tpr->rx_jmb_buffers[desc_idx].data;
Matt Carlsonbb158d62011-04-25 12:42:47 +000012387 map = dma_unmap_addr(&tpr->rx_jmb_buffers[desc_idx],
12388 mapping);
12389 } else
Matt Carlson4852a862011-04-13 11:05:07 +000012390 goto out;
12391
Matt Carlsonbb158d62011-04-25 12:42:47 +000012392 pci_dma_sync_single_for_cpu(tp->pdev, map, rx_len,
12393 PCI_DMA_FROMDEVICE);
12394
Eric Dumazet9205fd92011-11-18 06:47:01 +000012395 rx_data += TG3_RX_OFFSET(tp);
Matt Carlsonbb158d62011-04-25 12:42:47 +000012396 for (i = data_off; i < rx_len; i++, val++) {
Eric Dumazet9205fd92011-11-18 06:47:01 +000012397 if (*(rx_data + i) != (u8) (val & 0xff))
Matt Carlsonbb158d62011-04-25 12:42:47 +000012398 goto out;
12399 }
Matt Carlson4852a862011-04-13 11:05:07 +000012400 }
12401
Michael Chanc76949a2005-05-29 14:58:59 -070012402 err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012403
Eric Dumazet9205fd92011-11-18 06:47:01 +000012404 /* tg3_free_rings will unmap and free the rx_data */
Michael Chanc76949a2005-05-29 14:58:59 -070012405out:
12406 return err;
12407}
12408
Matt Carlson00c266b2011-04-25 12:42:46 +000012409#define TG3_STD_LOOPBACK_FAILED 1
12410#define TG3_JMB_LOOPBACK_FAILED 2
Matt Carlsonbb158d62011-04-25 12:42:47 +000012411#define TG3_TSO_LOOPBACK_FAILED 4
Matt Carlson28a45952011-08-19 13:58:22 +000012412#define TG3_LOOPBACK_FAILED \
12413 (TG3_STD_LOOPBACK_FAILED | \
12414 TG3_JMB_LOOPBACK_FAILED | \
12415 TG3_TSO_LOOPBACK_FAILED)
Matt Carlson00c266b2011-04-25 12:42:46 +000012416
Matt Carlson941ec902011-08-19 13:58:23 +000012417static int tg3_test_loopback(struct tg3 *tp, u64 *data, bool do_extlpbk)
Michael Chan9f40dea2005-09-05 17:53:06 -070012418{
Matt Carlson28a45952011-08-19 13:58:22 +000012419 int err = -EIO;
Matt Carlson2215e242011-08-19 13:58:19 +000012420 u32 eee_cap;
Michael Chanc441b452012-03-04 14:48:13 +000012421 u32 jmb_pkt_sz = 9000;
12422
12423 if (tp->dma_limit)
12424 jmb_pkt_sz = tp->dma_limit - ETH_HLEN;
Michael Chan9f40dea2005-09-05 17:53:06 -070012425
Matt Carlsonab789042011-01-25 15:58:54 +000012426 eee_cap = tp->phy_flags & TG3_PHYFLG_EEE_CAP;
12427 tp->phy_flags &= ~TG3_PHYFLG_EEE_CAP;
12428
Matt Carlson28a45952011-08-19 13:58:22 +000012429 if (!netif_running(tp->dev)) {
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000012430 data[TG3_MAC_LOOPB_TEST] = TG3_LOOPBACK_FAILED;
12431 data[TG3_PHY_LOOPB_TEST] = TG3_LOOPBACK_FAILED;
Matt Carlson941ec902011-08-19 13:58:23 +000012432 if (do_extlpbk)
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000012433 data[TG3_EXT_LOOPB_TEST] = TG3_LOOPBACK_FAILED;
Matt Carlson28a45952011-08-19 13:58:22 +000012434 goto done;
12435 }
12436
Michael Chanb9ec6c12006-07-25 16:37:27 -070012437 err = tg3_reset_hw(tp, 1);
Matt Carlsonab789042011-01-25 15:58:54 +000012438 if (err) {
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000012439 data[TG3_MAC_LOOPB_TEST] = TG3_LOOPBACK_FAILED;
12440 data[TG3_PHY_LOOPB_TEST] = TG3_LOOPBACK_FAILED;
Matt Carlson941ec902011-08-19 13:58:23 +000012441 if (do_extlpbk)
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000012442 data[TG3_EXT_LOOPB_TEST] = TG3_LOOPBACK_FAILED;
Matt Carlsonab789042011-01-25 15:58:54 +000012443 goto done;
12444 }
Michael Chan9f40dea2005-09-05 17:53:06 -070012445
Joe Perches63c3a662011-04-26 08:12:10 +000012446 if (tg3_flag(tp, ENABLE_RSS)) {
Matt Carlson4a85f092011-04-20 07:57:37 +000012447 int i;
12448
12449 /* Reroute all rx packets to the 1st queue */
12450 for (i = MAC_RSS_INDIR_TBL_0;
12451 i < MAC_RSS_INDIR_TBL_0 + TG3_RSS_INDIR_TBL_SIZE; i += 4)
12452 tw32(i, 0x0);
12453 }
12454
Matt Carlson6e01b202011-08-19 13:58:20 +000012455 /* HW errata - mac loopback fails in some cases on 5780.
12456 * Normal traffic and PHY loopback are not affected by
12457 * errata. Also, the MAC loopback test is deprecated for
12458 * all newer ASIC revisions.
12459 */
12460 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5780 &&
12461 !tg3_flag(tp, CPMU_PRESENT)) {
12462 tg3_mac_loopback(tp, true);
Matt Carlson9936bcf2007-10-10 18:03:07 -070012463
Matt Carlson28a45952011-08-19 13:58:22 +000012464 if (tg3_run_loopback(tp, ETH_FRAME_LEN, false))
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000012465 data[TG3_MAC_LOOPB_TEST] |= TG3_STD_LOOPBACK_FAILED;
Matt Carlson6e01b202011-08-19 13:58:20 +000012466
12467 if (tg3_flag(tp, JUMBO_RING_ENABLE) &&
Michael Chanc441b452012-03-04 14:48:13 +000012468 tg3_run_loopback(tp, jmb_pkt_sz + ETH_HLEN, false))
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000012469 data[TG3_MAC_LOOPB_TEST] |= TG3_JMB_LOOPBACK_FAILED;
Matt Carlson6e01b202011-08-19 13:58:20 +000012470
12471 tg3_mac_loopback(tp, false);
12472 }
Matt Carlson4852a862011-04-13 11:05:07 +000012473
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012474 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
Joe Perches63c3a662011-04-26 08:12:10 +000012475 !tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson5e5a7f32011-08-19 13:58:21 +000012476 int i;
12477
Matt Carlson941ec902011-08-19 13:58:23 +000012478 tg3_phy_lpbk_set(tp, 0, false);
Matt Carlson5e5a7f32011-08-19 13:58:21 +000012479
12480 /* Wait for link */
12481 for (i = 0; i < 100; i++) {
12482 if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
12483 break;
12484 mdelay(1);
12485 }
12486
Matt Carlson28a45952011-08-19 13:58:22 +000012487 if (tg3_run_loopback(tp, ETH_FRAME_LEN, false))
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000012488 data[TG3_PHY_LOOPB_TEST] |= TG3_STD_LOOPBACK_FAILED;
Joe Perches63c3a662011-04-26 08:12:10 +000012489 if (tg3_flag(tp, TSO_CAPABLE) &&
Matt Carlson28a45952011-08-19 13:58:22 +000012490 tg3_run_loopback(tp, ETH_FRAME_LEN, true))
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000012491 data[TG3_PHY_LOOPB_TEST] |= TG3_TSO_LOOPBACK_FAILED;
Joe Perches63c3a662011-04-26 08:12:10 +000012492 if (tg3_flag(tp, JUMBO_RING_ENABLE) &&
Michael Chanc441b452012-03-04 14:48:13 +000012493 tg3_run_loopback(tp, jmb_pkt_sz + ETH_HLEN, false))
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000012494 data[TG3_PHY_LOOPB_TEST] |= TG3_JMB_LOOPBACK_FAILED;
Michael Chan9f40dea2005-09-05 17:53:06 -070012495
Matt Carlson941ec902011-08-19 13:58:23 +000012496 if (do_extlpbk) {
12497 tg3_phy_lpbk_set(tp, 0, true);
12498
12499 /* All link indications report up, but the hardware
12500 * isn't really ready for about 20 msec. Double it
12501 * to be sure.
12502 */
12503 mdelay(40);
12504
12505 if (tg3_run_loopback(tp, ETH_FRAME_LEN, false))
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000012506 data[TG3_EXT_LOOPB_TEST] |=
12507 TG3_STD_LOOPBACK_FAILED;
Matt Carlson941ec902011-08-19 13:58:23 +000012508 if (tg3_flag(tp, TSO_CAPABLE) &&
12509 tg3_run_loopback(tp, ETH_FRAME_LEN, true))
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000012510 data[TG3_EXT_LOOPB_TEST] |=
12511 TG3_TSO_LOOPBACK_FAILED;
Matt Carlson941ec902011-08-19 13:58:23 +000012512 if (tg3_flag(tp, JUMBO_RING_ENABLE) &&
Michael Chanc441b452012-03-04 14:48:13 +000012513 tg3_run_loopback(tp, jmb_pkt_sz + ETH_HLEN, false))
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000012514 data[TG3_EXT_LOOPB_TEST] |=
12515 TG3_JMB_LOOPBACK_FAILED;
Matt Carlson941ec902011-08-19 13:58:23 +000012516 }
12517
Matt Carlson5e5a7f32011-08-19 13:58:21 +000012518 /* Re-enable gphy autopowerdown. */
12519 if (tp->phy_flags & TG3_PHYFLG_ENABLE_APD)
12520 tg3_phy_toggle_apd(tp, true);
12521 }
Matt Carlson6833c042008-11-21 17:18:59 -080012522
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000012523 err = (data[TG3_MAC_LOOPB_TEST] | data[TG3_PHY_LOOPB_TEST] |
12524 data[TG3_EXT_LOOPB_TEST]) ? -EIO : 0;
Matt Carlson28a45952011-08-19 13:58:22 +000012525
Matt Carlsonab789042011-01-25 15:58:54 +000012526done:
12527 tp->phy_flags |= eee_cap;
12528
Michael Chan9f40dea2005-09-05 17:53:06 -070012529 return err;
12530}
12531
Michael Chan4cafd3f2005-05-29 14:56:34 -070012532static void tg3_self_test(struct net_device *dev, struct ethtool_test *etest,
12533 u64 *data)
12534{
Michael Chan566f86a2005-05-29 14:56:58 -070012535 struct tg3 *tp = netdev_priv(dev);
Matt Carlson941ec902011-08-19 13:58:23 +000012536 bool doextlpbk = etest->flags & ETH_TEST_FL_EXTERNAL_LB;
Michael Chan566f86a2005-05-29 14:56:58 -070012537
Matt Carlsonbed98292011-07-13 09:27:29 +000012538 if ((tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) &&
12539 tg3_power_up(tp)) {
12540 etest->flags |= ETH_TEST_FL_FAILED;
12541 memset(data, 1, sizeof(u64) * TG3_NUM_TEST);
12542 return;
12543 }
Michael Chanbc1c7562006-03-20 17:48:03 -080012544
Michael Chan566f86a2005-05-29 14:56:58 -070012545 memset(data, 0, sizeof(u64) * TG3_NUM_TEST);
12546
12547 if (tg3_test_nvram(tp) != 0) {
12548 etest->flags |= ETH_TEST_FL_FAILED;
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000012549 data[TG3_NVRAM_TEST] = 1;
Michael Chan566f86a2005-05-29 14:56:58 -070012550 }
Matt Carlson941ec902011-08-19 13:58:23 +000012551 if (!doextlpbk && tg3_test_link(tp)) {
Michael Chanca430072005-05-29 14:57:23 -070012552 etest->flags |= ETH_TEST_FL_FAILED;
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000012553 data[TG3_LINK_TEST] = 1;
Michael Chanca430072005-05-29 14:57:23 -070012554 }
Michael Chana71116d2005-05-29 14:58:11 -070012555 if (etest->flags & ETH_TEST_FL_OFFLINE) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012556 int err, err2 = 0, irq_sync = 0;
Michael Chana71116d2005-05-29 14:58:11 -070012557
Michael Chanbbe832c2005-06-24 20:20:04 -070012558 if (netif_running(dev)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012559 tg3_phy_stop(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -070012560 tg3_netif_stop(tp);
12561 irq_sync = 1;
12562 }
12563
12564 tg3_full_lock(tp, irq_sync);
Michael Chana71116d2005-05-29 14:58:11 -070012565 tg3_halt(tp, RESET_KIND_SUSPEND, 1);
Michael Chanec41c7d2006-01-17 02:40:55 -080012566 err = tg3_nvram_lock(tp);
Michael Chana71116d2005-05-29 14:58:11 -070012567 tg3_halt_cpu(tp, RX_CPU_BASE);
Joe Perches63c3a662011-04-26 08:12:10 +000012568 if (!tg3_flag(tp, 5705_PLUS))
Michael Chana71116d2005-05-29 14:58:11 -070012569 tg3_halt_cpu(tp, TX_CPU_BASE);
Michael Chanec41c7d2006-01-17 02:40:55 -080012570 if (!err)
12571 tg3_nvram_unlock(tp);
Michael Chana71116d2005-05-29 14:58:11 -070012572
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012573 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
Michael Chand9ab5ad2006-03-20 22:27:35 -080012574 tg3_phy_reset(tp);
12575
Michael Chana71116d2005-05-29 14:58:11 -070012576 if (tg3_test_registers(tp) != 0) {
12577 etest->flags |= ETH_TEST_FL_FAILED;
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000012578 data[TG3_REGISTER_TEST] = 1;
Michael Chana71116d2005-05-29 14:58:11 -070012579 }
Matt Carlson28a45952011-08-19 13:58:22 +000012580
Michael Chan7942e1d2005-05-29 14:58:36 -070012581 if (tg3_test_memory(tp) != 0) {
12582 etest->flags |= ETH_TEST_FL_FAILED;
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000012583 data[TG3_MEMORY_TEST] = 1;
Michael Chan7942e1d2005-05-29 14:58:36 -070012584 }
Matt Carlson28a45952011-08-19 13:58:22 +000012585
Matt Carlson941ec902011-08-19 13:58:23 +000012586 if (doextlpbk)
12587 etest->flags |= ETH_TEST_FL_EXTERNAL_LB_DONE;
12588
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000012589 if (tg3_test_loopback(tp, data, doextlpbk))
Michael Chanc76949a2005-05-29 14:58:59 -070012590 etest->flags |= ETH_TEST_FL_FAILED;
Michael Chana71116d2005-05-29 14:58:11 -070012591
David S. Millerf47c11e2005-06-24 20:18:35 -070012592 tg3_full_unlock(tp);
12593
Michael Chand4bc3922005-05-29 14:59:20 -070012594 if (tg3_test_interrupt(tp) != 0) {
12595 etest->flags |= ETH_TEST_FL_FAILED;
Nithin Nayak Sujir93df8b82012-11-14 14:44:28 +000012596 data[TG3_INTERRUPT_TEST] = 1;
Michael Chand4bc3922005-05-29 14:59:20 -070012597 }
David S. Millerf47c11e2005-06-24 20:18:35 -070012598
12599 tg3_full_lock(tp, 0);
Michael Chand4bc3922005-05-29 14:59:20 -070012600
Michael Chana71116d2005-05-29 14:58:11 -070012601 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
12602 if (netif_running(dev)) {
Joe Perches63c3a662011-04-26 08:12:10 +000012603 tg3_flag_set(tp, INIT_COMPLETE);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012604 err2 = tg3_restart_hw(tp, 1);
12605 if (!err2)
Michael Chanb9ec6c12006-07-25 16:37:27 -070012606 tg3_netif_start(tp);
Michael Chana71116d2005-05-29 14:58:11 -070012607 }
David S. Millerf47c11e2005-06-24 20:18:35 -070012608
12609 tg3_full_unlock(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012610
12611 if (irq_sync && !err2)
12612 tg3_phy_start(tp);
Michael Chana71116d2005-05-29 14:58:11 -070012613 }
Matt Carlson800960682010-08-02 11:26:06 +000012614 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000012615 tg3_power_down(tp);
Michael Chanbc1c7562006-03-20 17:48:03 -080012616
Michael Chan4cafd3f2005-05-29 14:56:34 -070012617}
12618
Linus Torvalds1da177e2005-04-16 15:20:36 -070012619static int tg3_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
12620{
12621 struct mii_ioctl_data *data = if_mii(ifr);
12622 struct tg3 *tp = netdev_priv(dev);
12623 int err;
12624
Joe Perches63c3a662011-04-26 08:12:10 +000012625 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000012626 struct phy_device *phydev;
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012627 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012628 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000012629 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Richard Cochran28b04112010-07-17 08:48:55 +000012630 return phy_mii_ioctl(phydev, ifr, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012631 }
12632
Matt Carlson33f401a2010-04-05 10:19:27 +000012633 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012634 case SIOCGMIIPHY:
Matt Carlson882e9792009-09-01 13:21:36 +000012635 data->phy_id = tp->phy_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012636
12637 /* fallthru */
12638 case SIOCGMIIREG: {
12639 u32 mii_regval;
12640
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012641 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012642 break; /* We have no PHY */
12643
Matt Carlson34eea5a2011-04-20 07:57:38 +000012644 if (!netif_running(dev))
Michael Chanbc1c7562006-03-20 17:48:03 -080012645 return -EAGAIN;
12646
David S. Millerf47c11e2005-06-24 20:18:35 -070012647 spin_lock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012648 err = tg3_readphy(tp, data->reg_num & 0x1f, &mii_regval);
David S. Millerf47c11e2005-06-24 20:18:35 -070012649 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012650
12651 data->val_out = mii_regval;
12652
12653 return err;
12654 }
12655
12656 case SIOCSMIIREG:
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012657 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012658 break; /* We have no PHY */
12659
Matt Carlson34eea5a2011-04-20 07:57:38 +000012660 if (!netif_running(dev))
Michael Chanbc1c7562006-03-20 17:48:03 -080012661 return -EAGAIN;
12662
David S. Millerf47c11e2005-06-24 20:18:35 -070012663 spin_lock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012664 err = tg3_writephy(tp, data->reg_num & 0x1f, data->val_in);
David S. Millerf47c11e2005-06-24 20:18:35 -070012665 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012666
12667 return err;
12668
12669 default:
12670 /* do nothing */
12671 break;
12672 }
12673 return -EOPNOTSUPP;
12674}
12675
David S. Miller15f98502005-05-18 22:49:26 -070012676static int tg3_get_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
12677{
12678 struct tg3 *tp = netdev_priv(dev);
12679
12680 memcpy(ec, &tp->coal, sizeof(*ec));
12681 return 0;
12682}
12683
Michael Chand244c892005-07-05 14:42:33 -070012684static int tg3_set_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
12685{
12686 struct tg3 *tp = netdev_priv(dev);
12687 u32 max_rxcoal_tick_int = 0, max_txcoal_tick_int = 0;
12688 u32 max_stat_coal_ticks = 0, min_stat_coal_ticks = 0;
12689
Joe Perches63c3a662011-04-26 08:12:10 +000012690 if (!tg3_flag(tp, 5705_PLUS)) {
Michael Chand244c892005-07-05 14:42:33 -070012691 max_rxcoal_tick_int = MAX_RXCOAL_TICK_INT;
12692 max_txcoal_tick_int = MAX_TXCOAL_TICK_INT;
12693 max_stat_coal_ticks = MAX_STAT_COAL_TICKS;
12694 min_stat_coal_ticks = MIN_STAT_COAL_TICKS;
12695 }
12696
12697 if ((ec->rx_coalesce_usecs > MAX_RXCOL_TICKS) ||
12698 (ec->tx_coalesce_usecs > MAX_TXCOL_TICKS) ||
12699 (ec->rx_max_coalesced_frames > MAX_RXMAX_FRAMES) ||
12700 (ec->tx_max_coalesced_frames > MAX_TXMAX_FRAMES) ||
12701 (ec->rx_coalesce_usecs_irq > max_rxcoal_tick_int) ||
12702 (ec->tx_coalesce_usecs_irq > max_txcoal_tick_int) ||
12703 (ec->rx_max_coalesced_frames_irq > MAX_RXCOAL_MAXF_INT) ||
12704 (ec->tx_max_coalesced_frames_irq > MAX_TXCOAL_MAXF_INT) ||
12705 (ec->stats_block_coalesce_usecs > max_stat_coal_ticks) ||
12706 (ec->stats_block_coalesce_usecs < min_stat_coal_ticks))
12707 return -EINVAL;
12708
12709 /* No rx interrupts will be generated if both are zero */
12710 if ((ec->rx_coalesce_usecs == 0) &&
12711 (ec->rx_max_coalesced_frames == 0))
12712 return -EINVAL;
12713
12714 /* No tx interrupts will be generated if both are zero */
12715 if ((ec->tx_coalesce_usecs == 0) &&
12716 (ec->tx_max_coalesced_frames == 0))
12717 return -EINVAL;
12718
12719 /* Only copy relevant parameters, ignore all others. */
12720 tp->coal.rx_coalesce_usecs = ec->rx_coalesce_usecs;
12721 tp->coal.tx_coalesce_usecs = ec->tx_coalesce_usecs;
12722 tp->coal.rx_max_coalesced_frames = ec->rx_max_coalesced_frames;
12723 tp->coal.tx_max_coalesced_frames = ec->tx_max_coalesced_frames;
12724 tp->coal.rx_coalesce_usecs_irq = ec->rx_coalesce_usecs_irq;
12725 tp->coal.tx_coalesce_usecs_irq = ec->tx_coalesce_usecs_irq;
12726 tp->coal.rx_max_coalesced_frames_irq = ec->rx_max_coalesced_frames_irq;
12727 tp->coal.tx_max_coalesced_frames_irq = ec->tx_max_coalesced_frames_irq;
12728 tp->coal.stats_block_coalesce_usecs = ec->stats_block_coalesce_usecs;
12729
12730 if (netif_running(dev)) {
12731 tg3_full_lock(tp, 0);
12732 __tg3_set_coalesce(tp, &tp->coal);
12733 tg3_full_unlock(tp);
12734 }
12735 return 0;
12736}
12737
Jeff Garzik7282d492006-09-13 14:30:00 -040012738static const struct ethtool_ops tg3_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012739 .get_settings = tg3_get_settings,
12740 .set_settings = tg3_set_settings,
12741 .get_drvinfo = tg3_get_drvinfo,
12742 .get_regs_len = tg3_get_regs_len,
12743 .get_regs = tg3_get_regs,
12744 .get_wol = tg3_get_wol,
12745 .set_wol = tg3_set_wol,
12746 .get_msglevel = tg3_get_msglevel,
12747 .set_msglevel = tg3_set_msglevel,
12748 .nway_reset = tg3_nway_reset,
12749 .get_link = ethtool_op_get_link,
12750 .get_eeprom_len = tg3_get_eeprom_len,
12751 .get_eeprom = tg3_get_eeprom,
12752 .set_eeprom = tg3_set_eeprom,
12753 .get_ringparam = tg3_get_ringparam,
12754 .set_ringparam = tg3_set_ringparam,
12755 .get_pauseparam = tg3_get_pauseparam,
12756 .set_pauseparam = tg3_set_pauseparam,
Michael Chan4cafd3f2005-05-29 14:56:34 -070012757 .self_test = tg3_self_test,
Linus Torvalds1da177e2005-04-16 15:20:36 -070012758 .get_strings = tg3_get_strings,
stephen hemminger81b87092011-04-04 08:43:50 +000012759 .set_phys_id = tg3_set_phys_id,
Linus Torvalds1da177e2005-04-16 15:20:36 -070012760 .get_ethtool_stats = tg3_get_ethtool_stats,
David S. Miller15f98502005-05-18 22:49:26 -070012761 .get_coalesce = tg3_get_coalesce,
Michael Chand244c892005-07-05 14:42:33 -070012762 .set_coalesce = tg3_set_coalesce,
Jeff Garzikb9f2c042007-10-03 18:07:32 -070012763 .get_sset_count = tg3_get_sset_count,
Matt Carlson90415472011-12-16 13:33:23 +000012764 .get_rxnfc = tg3_get_rxnfc,
12765 .get_rxfh_indir_size = tg3_get_rxfh_indir_size,
12766 .get_rxfh_indir = tg3_get_rxfh_indir,
12767 .set_rxfh_indir = tg3_set_rxfh_indir,
Michael Chan09681692012-09-28 07:12:42 +000012768 .get_channels = tg3_get_channels,
12769 .set_channels = tg3_set_channels,
Richard Cochran3f847492012-04-03 22:59:39 +000012770 .get_ts_info = ethtool_op_get_ts_info,
Linus Torvalds1da177e2005-04-16 15:20:36 -070012771};
12772
David S. Millerb4017c52012-03-01 17:57:40 -050012773static struct rtnl_link_stats64 *tg3_get_stats64(struct net_device *dev,
12774 struct rtnl_link_stats64 *stats)
12775{
12776 struct tg3 *tp = netdev_priv(dev);
12777
David S. Millerb4017c52012-03-01 17:57:40 -050012778 spin_lock_bh(&tp->lock);
Michael Chan0f566b22012-07-29 19:15:44 +000012779 if (!tp->hw_stats) {
12780 spin_unlock_bh(&tp->lock);
12781 return &tp->net_stats_prev;
12782 }
12783
David S. Millerb4017c52012-03-01 17:57:40 -050012784 tg3_get_nstats(tp, stats);
12785 spin_unlock_bh(&tp->lock);
12786
12787 return stats;
12788}
12789
Matt Carlsonccd5ba92012-02-13 10:20:08 +000012790static void tg3_set_rx_mode(struct net_device *dev)
12791{
12792 struct tg3 *tp = netdev_priv(dev);
12793
12794 if (!netif_running(dev))
12795 return;
12796
12797 tg3_full_lock(tp, 0);
12798 __tg3_set_rx_mode(dev);
12799 tg3_full_unlock(tp);
12800}
12801
Matt Carlsonfaf16272012-02-13 10:20:07 +000012802static inline void tg3_set_mtu(struct net_device *dev, struct tg3 *tp,
12803 int new_mtu)
12804{
12805 dev->mtu = new_mtu;
12806
12807 if (new_mtu > ETH_DATA_LEN) {
12808 if (tg3_flag(tp, 5780_CLASS)) {
12809 netdev_update_features(dev);
12810 tg3_flag_clear(tp, TSO_CAPABLE);
12811 } else {
12812 tg3_flag_set(tp, JUMBO_RING_ENABLE);
12813 }
12814 } else {
12815 if (tg3_flag(tp, 5780_CLASS)) {
12816 tg3_flag_set(tp, TSO_CAPABLE);
12817 netdev_update_features(dev);
12818 }
12819 tg3_flag_clear(tp, JUMBO_RING_ENABLE);
12820 }
12821}
12822
12823static int tg3_change_mtu(struct net_device *dev, int new_mtu)
12824{
12825 struct tg3 *tp = netdev_priv(dev);
Michael Chan2fae5e32012-03-04 14:48:15 +000012826 int err, reset_phy = 0;
Matt Carlsonfaf16272012-02-13 10:20:07 +000012827
12828 if (new_mtu < TG3_MIN_MTU || new_mtu > TG3_MAX_MTU(tp))
12829 return -EINVAL;
12830
12831 if (!netif_running(dev)) {
12832 /* We'll just catch it later when the
12833 * device is up'd.
12834 */
12835 tg3_set_mtu(dev, tp, new_mtu);
12836 return 0;
12837 }
12838
12839 tg3_phy_stop(tp);
12840
12841 tg3_netif_stop(tp);
12842
12843 tg3_full_lock(tp, 1);
12844
12845 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
12846
12847 tg3_set_mtu(dev, tp, new_mtu);
12848
Michael Chan2fae5e32012-03-04 14:48:15 +000012849 /* Reset PHY, otherwise the read DMA engine will be in a mode that
12850 * breaks all requests to 256 bytes.
12851 */
12852 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57766)
12853 reset_phy = 1;
12854
12855 err = tg3_restart_hw(tp, reset_phy);
Matt Carlsonfaf16272012-02-13 10:20:07 +000012856
12857 if (!err)
12858 tg3_netif_start(tp);
12859
12860 tg3_full_unlock(tp);
12861
12862 if (!err)
12863 tg3_phy_start(tp);
12864
12865 return err;
12866}
12867
12868static const struct net_device_ops tg3_netdev_ops = {
12869 .ndo_open = tg3_open,
12870 .ndo_stop = tg3_close,
12871 .ndo_start_xmit = tg3_start_xmit,
12872 .ndo_get_stats64 = tg3_get_stats64,
12873 .ndo_validate_addr = eth_validate_addr,
12874 .ndo_set_rx_mode = tg3_set_rx_mode,
12875 .ndo_set_mac_address = tg3_set_mac_addr,
12876 .ndo_do_ioctl = tg3_ioctl,
12877 .ndo_tx_timeout = tg3_tx_timeout,
12878 .ndo_change_mtu = tg3_change_mtu,
12879 .ndo_fix_features = tg3_fix_features,
12880 .ndo_set_features = tg3_set_features,
12881#ifdef CONFIG_NET_POLL_CONTROLLER
12882 .ndo_poll_controller = tg3_poll_controller,
12883#endif
12884};
12885
Bill Pemberton229b1ad2012-12-03 09:22:59 -050012886static void tg3_get_eeprom_size(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012887{
Michael Chan1b277772006-03-20 22:27:48 -080012888 u32 cursize, val, magic;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012889
12890 tp->nvram_size = EEPROM_CHIP_SIZE;
12891
Matt Carlsone4f34112009-02-25 14:25:00 +000012892 if (tg3_nvram_read(tp, 0, &magic) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012893 return;
12894
Michael Chanb16250e2006-09-27 16:10:14 -070012895 if ((magic != TG3_EEPROM_MAGIC) &&
12896 ((magic & TG3_EEPROM_MAGIC_FW_MSK) != TG3_EEPROM_MAGIC_FW) &&
12897 ((magic & TG3_EEPROM_MAGIC_HW_MSK) != TG3_EEPROM_MAGIC_HW))
Linus Torvalds1da177e2005-04-16 15:20:36 -070012898 return;
12899
12900 /*
12901 * Size the chip by reading offsets at increasing powers of two.
12902 * When we encounter our validation signature, we know the addressing
12903 * has wrapped around, and thus have our chip size.
12904 */
Michael Chan1b277772006-03-20 22:27:48 -080012905 cursize = 0x10;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012906
12907 while (cursize < tp->nvram_size) {
Matt Carlsone4f34112009-02-25 14:25:00 +000012908 if (tg3_nvram_read(tp, cursize, &val) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012909 return;
12910
Michael Chan18201802006-03-20 22:29:15 -080012911 if (val == magic)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012912 break;
12913
12914 cursize <<= 1;
12915 }
12916
12917 tp->nvram_size = cursize;
12918}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012919
Bill Pemberton229b1ad2012-12-03 09:22:59 -050012920static void tg3_get_nvram_size(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012921{
12922 u32 val;
12923
Joe Perches63c3a662011-04-26 08:12:10 +000012924 if (tg3_flag(tp, NO_NVRAM) || tg3_nvram_read(tp, 0, &val) != 0)
Michael Chan1b277772006-03-20 22:27:48 -080012925 return;
12926
12927 /* Selfboot format */
Michael Chan18201802006-03-20 22:29:15 -080012928 if (val != TG3_EEPROM_MAGIC) {
Michael Chan1b277772006-03-20 22:27:48 -080012929 tg3_get_eeprom_size(tp);
12930 return;
12931 }
12932
Matt Carlson6d348f22009-02-25 14:25:52 +000012933 if (tg3_nvram_read(tp, 0xf0, &val) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012934 if (val != 0) {
Matt Carlson6d348f22009-02-25 14:25:52 +000012935 /* This is confusing. We want to operate on the
12936 * 16-bit value at offset 0xf2. The tg3_nvram_read()
12937 * call will read from NVRAM and byteswap the data
12938 * according to the byteswapping settings for all
12939 * other register accesses. This ensures the data we
12940 * want will always reside in the lower 16-bits.
12941 * However, the data in NVRAM is in LE format, which
12942 * means the data from the NVRAM read will always be
12943 * opposite the endianness of the CPU. The 16-bit
12944 * byteswap then brings the data to CPU endianness.
12945 */
12946 tp->nvram_size = swab16((u16)(val & 0x0000ffff)) * 1024;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012947 return;
12948 }
12949 }
Matt Carlsonfd1122a2008-05-02 16:48:36 -070012950 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012951}
12952
Bill Pemberton229b1ad2012-12-03 09:22:59 -050012953static void tg3_get_nvram_info(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012954{
12955 u32 nvcfg1;
12956
12957 nvcfg1 = tr32(NVRAM_CFG1);
12958 if (nvcfg1 & NVRAM_CFG1_FLASHIF_ENAB) {
Joe Perches63c3a662011-04-26 08:12:10 +000012959 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000012960 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012961 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
12962 tw32(NVRAM_CFG1, nvcfg1);
12963 }
12964
Matt Carlson6ff6f812011-05-19 12:12:54 +000012965 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 ||
Joe Perches63c3a662011-04-26 08:12:10 +000012966 tg3_flag(tp, 5780_CLASS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012967 switch (nvcfg1 & NVRAM_CFG1_VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000012968 case FLASH_VENDOR_ATMEL_FLASH_BUFFERED:
12969 tp->nvram_jedecnum = JEDEC_ATMEL;
12970 tp->nvram_pagesize = ATMEL_AT45DB0X1B_PAGE_SIZE;
Joe Perches63c3a662011-04-26 08:12:10 +000012971 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000012972 break;
12973 case FLASH_VENDOR_ATMEL_FLASH_UNBUFFERED:
12974 tp->nvram_jedecnum = JEDEC_ATMEL;
12975 tp->nvram_pagesize = ATMEL_AT25F512_PAGE_SIZE;
12976 break;
12977 case FLASH_VENDOR_ATMEL_EEPROM:
12978 tp->nvram_jedecnum = JEDEC_ATMEL;
12979 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
Joe Perches63c3a662011-04-26 08:12:10 +000012980 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000012981 break;
12982 case FLASH_VENDOR_ST:
12983 tp->nvram_jedecnum = JEDEC_ST;
12984 tp->nvram_pagesize = ST_M45PEX0_PAGE_SIZE;
Joe Perches63c3a662011-04-26 08:12:10 +000012985 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000012986 break;
12987 case FLASH_VENDOR_SAIFUN:
12988 tp->nvram_jedecnum = JEDEC_SAIFUN;
12989 tp->nvram_pagesize = SAIFUN_SA25F0XX_PAGE_SIZE;
12990 break;
12991 case FLASH_VENDOR_SST_SMALL:
12992 case FLASH_VENDOR_SST_LARGE:
12993 tp->nvram_jedecnum = JEDEC_SST;
12994 tp->nvram_pagesize = SST_25VF0X0_PAGE_SIZE;
12995 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012996 }
Matt Carlson8590a602009-08-28 12:29:16 +000012997 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012998 tp->nvram_jedecnum = JEDEC_ATMEL;
12999 tp->nvram_pagesize = ATMEL_AT45DB0X1B_PAGE_SIZE;
Joe Perches63c3a662011-04-26 08:12:10 +000013000 tg3_flag_set(tp, NVRAM_BUFFERED);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013001 }
13002}
13003
Bill Pemberton229b1ad2012-12-03 09:22:59 -050013004static void tg3_nvram_get_pagesize(struct tg3 *tp, u32 nvmcfg1)
Matt Carlsona1b950d2009-09-01 13:20:17 +000013005{
13006 switch (nvmcfg1 & NVRAM_CFG1_5752PAGE_SIZE_MASK) {
13007 case FLASH_5752PAGE_SIZE_256:
13008 tp->nvram_pagesize = 256;
13009 break;
13010 case FLASH_5752PAGE_SIZE_512:
13011 tp->nvram_pagesize = 512;
13012 break;
13013 case FLASH_5752PAGE_SIZE_1K:
13014 tp->nvram_pagesize = 1024;
13015 break;
13016 case FLASH_5752PAGE_SIZE_2K:
13017 tp->nvram_pagesize = 2048;
13018 break;
13019 case FLASH_5752PAGE_SIZE_4K:
13020 tp->nvram_pagesize = 4096;
13021 break;
13022 case FLASH_5752PAGE_SIZE_264:
13023 tp->nvram_pagesize = 264;
13024 break;
13025 case FLASH_5752PAGE_SIZE_528:
13026 tp->nvram_pagesize = 528;
13027 break;
13028 }
13029}
13030
Bill Pemberton229b1ad2012-12-03 09:22:59 -050013031static void tg3_get_5752_nvram_info(struct tg3 *tp)
Michael Chan361b4ac2005-04-21 17:11:21 -070013032{
13033 u32 nvcfg1;
13034
13035 nvcfg1 = tr32(NVRAM_CFG1);
13036
Michael Chane6af3012005-04-21 17:12:05 -070013037 /* NVRAM protection for TPM */
13038 if (nvcfg1 & (1 << 27))
Joe Perches63c3a662011-04-26 08:12:10 +000013039 tg3_flag_set(tp, PROTECTED_NVRAM);
Michael Chane6af3012005-04-21 17:12:05 -070013040
Michael Chan361b4ac2005-04-21 17:11:21 -070013041 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000013042 case FLASH_5752VENDOR_ATMEL_EEPROM_64KHZ:
13043 case FLASH_5752VENDOR_ATMEL_EEPROM_376KHZ:
13044 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000013045 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000013046 break;
13047 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
13048 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000013049 tg3_flag_set(tp, NVRAM_BUFFERED);
13050 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000013051 break;
13052 case FLASH_5752VENDOR_ST_M45PE10:
13053 case FLASH_5752VENDOR_ST_M45PE20:
13054 case FLASH_5752VENDOR_ST_M45PE40:
13055 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000013056 tg3_flag_set(tp, NVRAM_BUFFERED);
13057 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000013058 break;
Michael Chan361b4ac2005-04-21 17:11:21 -070013059 }
13060
Joe Perches63c3a662011-04-26 08:12:10 +000013061 if (tg3_flag(tp, FLASH)) {
Matt Carlsona1b950d2009-09-01 13:20:17 +000013062 tg3_nvram_get_pagesize(tp, nvcfg1);
Matt Carlson8590a602009-08-28 12:29:16 +000013063 } else {
Michael Chan361b4ac2005-04-21 17:11:21 -070013064 /* For eeprom, set pagesize to maximum eeprom size */
13065 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
13066
13067 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
13068 tw32(NVRAM_CFG1, nvcfg1);
13069 }
13070}
13071
Bill Pemberton229b1ad2012-12-03 09:22:59 -050013072static void tg3_get_5755_nvram_info(struct tg3 *tp)
Michael Chand3c7b882006-03-23 01:28:25 -080013073{
Matt Carlson989a9d22007-05-05 11:51:05 -070013074 u32 nvcfg1, protect = 0;
Michael Chand3c7b882006-03-23 01:28:25 -080013075
13076 nvcfg1 = tr32(NVRAM_CFG1);
13077
13078 /* NVRAM protection for TPM */
Matt Carlson989a9d22007-05-05 11:51:05 -070013079 if (nvcfg1 & (1 << 27)) {
Joe Perches63c3a662011-04-26 08:12:10 +000013080 tg3_flag_set(tp, PROTECTED_NVRAM);
Matt Carlson989a9d22007-05-05 11:51:05 -070013081 protect = 1;
13082 }
Michael Chand3c7b882006-03-23 01:28:25 -080013083
Matt Carlson989a9d22007-05-05 11:51:05 -070013084 nvcfg1 &= NVRAM_CFG1_5752VENDOR_MASK;
13085 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000013086 case FLASH_5755VENDOR_ATMEL_FLASH_1:
13087 case FLASH_5755VENDOR_ATMEL_FLASH_2:
13088 case FLASH_5755VENDOR_ATMEL_FLASH_3:
13089 case FLASH_5755VENDOR_ATMEL_FLASH_5:
13090 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000013091 tg3_flag_set(tp, NVRAM_BUFFERED);
13092 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000013093 tp->nvram_pagesize = 264;
13094 if (nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_1 ||
13095 nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_5)
13096 tp->nvram_size = (protect ? 0x3e200 :
13097 TG3_NVRAM_SIZE_512KB);
13098 else if (nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_2)
13099 tp->nvram_size = (protect ? 0x1f200 :
13100 TG3_NVRAM_SIZE_256KB);
13101 else
13102 tp->nvram_size = (protect ? 0x1f200 :
13103 TG3_NVRAM_SIZE_128KB);
13104 break;
13105 case FLASH_5752VENDOR_ST_M45PE10:
13106 case FLASH_5752VENDOR_ST_M45PE20:
13107 case FLASH_5752VENDOR_ST_M45PE40:
13108 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000013109 tg3_flag_set(tp, NVRAM_BUFFERED);
13110 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000013111 tp->nvram_pagesize = 256;
13112 if (nvcfg1 == FLASH_5752VENDOR_ST_M45PE10)
13113 tp->nvram_size = (protect ?
13114 TG3_NVRAM_SIZE_64KB :
13115 TG3_NVRAM_SIZE_128KB);
13116 else if (nvcfg1 == FLASH_5752VENDOR_ST_M45PE20)
13117 tp->nvram_size = (protect ?
13118 TG3_NVRAM_SIZE_64KB :
13119 TG3_NVRAM_SIZE_256KB);
13120 else
13121 tp->nvram_size = (protect ?
13122 TG3_NVRAM_SIZE_128KB :
13123 TG3_NVRAM_SIZE_512KB);
13124 break;
Michael Chand3c7b882006-03-23 01:28:25 -080013125 }
13126}
13127
Bill Pemberton229b1ad2012-12-03 09:22:59 -050013128static void tg3_get_5787_nvram_info(struct tg3 *tp)
Michael Chan1b277772006-03-20 22:27:48 -080013129{
13130 u32 nvcfg1;
13131
13132 nvcfg1 = tr32(NVRAM_CFG1);
13133
13134 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000013135 case FLASH_5787VENDOR_ATMEL_EEPROM_64KHZ:
13136 case FLASH_5787VENDOR_ATMEL_EEPROM_376KHZ:
13137 case FLASH_5787VENDOR_MICRO_EEPROM_64KHZ:
13138 case FLASH_5787VENDOR_MICRO_EEPROM_376KHZ:
13139 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000013140 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000013141 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
Michael Chan1b277772006-03-20 22:27:48 -080013142
Matt Carlson8590a602009-08-28 12:29:16 +000013143 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
13144 tw32(NVRAM_CFG1, nvcfg1);
13145 break;
13146 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
13147 case FLASH_5755VENDOR_ATMEL_FLASH_1:
13148 case FLASH_5755VENDOR_ATMEL_FLASH_2:
13149 case FLASH_5755VENDOR_ATMEL_FLASH_3:
13150 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000013151 tg3_flag_set(tp, NVRAM_BUFFERED);
13152 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000013153 tp->nvram_pagesize = 264;
13154 break;
13155 case FLASH_5752VENDOR_ST_M45PE10:
13156 case FLASH_5752VENDOR_ST_M45PE20:
13157 case FLASH_5752VENDOR_ST_M45PE40:
13158 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000013159 tg3_flag_set(tp, NVRAM_BUFFERED);
13160 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000013161 tp->nvram_pagesize = 256;
13162 break;
Michael Chan1b277772006-03-20 22:27:48 -080013163 }
13164}
13165
Bill Pemberton229b1ad2012-12-03 09:22:59 -050013166static void tg3_get_5761_nvram_info(struct tg3 *tp)
Matt Carlson6b91fa02007-10-10 18:01:09 -070013167{
13168 u32 nvcfg1, protect = 0;
13169
13170 nvcfg1 = tr32(NVRAM_CFG1);
13171
13172 /* NVRAM protection for TPM */
13173 if (nvcfg1 & (1 << 27)) {
Joe Perches63c3a662011-04-26 08:12:10 +000013174 tg3_flag_set(tp, PROTECTED_NVRAM);
Matt Carlson6b91fa02007-10-10 18:01:09 -070013175 protect = 1;
13176 }
13177
13178 nvcfg1 &= NVRAM_CFG1_5752VENDOR_MASK;
13179 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000013180 case FLASH_5761VENDOR_ATMEL_ADB021D:
13181 case FLASH_5761VENDOR_ATMEL_ADB041D:
13182 case FLASH_5761VENDOR_ATMEL_ADB081D:
13183 case FLASH_5761VENDOR_ATMEL_ADB161D:
13184 case FLASH_5761VENDOR_ATMEL_MDB021D:
13185 case FLASH_5761VENDOR_ATMEL_MDB041D:
13186 case FLASH_5761VENDOR_ATMEL_MDB081D:
13187 case FLASH_5761VENDOR_ATMEL_MDB161D:
13188 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000013189 tg3_flag_set(tp, NVRAM_BUFFERED);
13190 tg3_flag_set(tp, FLASH);
13191 tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
Matt Carlson8590a602009-08-28 12:29:16 +000013192 tp->nvram_pagesize = 256;
13193 break;
13194 case FLASH_5761VENDOR_ST_A_M45PE20:
13195 case FLASH_5761VENDOR_ST_A_M45PE40:
13196 case FLASH_5761VENDOR_ST_A_M45PE80:
13197 case FLASH_5761VENDOR_ST_A_M45PE16:
13198 case FLASH_5761VENDOR_ST_M_M45PE20:
13199 case FLASH_5761VENDOR_ST_M_M45PE40:
13200 case FLASH_5761VENDOR_ST_M_M45PE80:
13201 case FLASH_5761VENDOR_ST_M_M45PE16:
13202 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000013203 tg3_flag_set(tp, NVRAM_BUFFERED);
13204 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000013205 tp->nvram_pagesize = 256;
13206 break;
Matt Carlson6b91fa02007-10-10 18:01:09 -070013207 }
13208
13209 if (protect) {
13210 tp->nvram_size = tr32(NVRAM_ADDR_LOCKOUT);
13211 } else {
13212 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000013213 case FLASH_5761VENDOR_ATMEL_ADB161D:
13214 case FLASH_5761VENDOR_ATMEL_MDB161D:
13215 case FLASH_5761VENDOR_ST_A_M45PE16:
13216 case FLASH_5761VENDOR_ST_M_M45PE16:
13217 tp->nvram_size = TG3_NVRAM_SIZE_2MB;
13218 break;
13219 case FLASH_5761VENDOR_ATMEL_ADB081D:
13220 case FLASH_5761VENDOR_ATMEL_MDB081D:
13221 case FLASH_5761VENDOR_ST_A_M45PE80:
13222 case FLASH_5761VENDOR_ST_M_M45PE80:
13223 tp->nvram_size = TG3_NVRAM_SIZE_1MB;
13224 break;
13225 case FLASH_5761VENDOR_ATMEL_ADB041D:
13226 case FLASH_5761VENDOR_ATMEL_MDB041D:
13227 case FLASH_5761VENDOR_ST_A_M45PE40:
13228 case FLASH_5761VENDOR_ST_M_M45PE40:
13229 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
13230 break;
13231 case FLASH_5761VENDOR_ATMEL_ADB021D:
13232 case FLASH_5761VENDOR_ATMEL_MDB021D:
13233 case FLASH_5761VENDOR_ST_A_M45PE20:
13234 case FLASH_5761VENDOR_ST_M_M45PE20:
13235 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
13236 break;
Matt Carlson6b91fa02007-10-10 18:01:09 -070013237 }
13238 }
13239}
13240
Bill Pemberton229b1ad2012-12-03 09:22:59 -050013241static void tg3_get_5906_nvram_info(struct tg3 *tp)
Michael Chanb5d37722006-09-27 16:06:21 -070013242{
13243 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000013244 tg3_flag_set(tp, NVRAM_BUFFERED);
Michael Chanb5d37722006-09-27 16:06:21 -070013245 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
13246}
13247
Bill Pemberton229b1ad2012-12-03 09:22:59 -050013248static void tg3_get_57780_nvram_info(struct tg3 *tp)
Matt Carlson321d32a2008-11-21 17:22:19 -080013249{
13250 u32 nvcfg1;
13251
13252 nvcfg1 = tr32(NVRAM_CFG1);
13253
13254 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
13255 case FLASH_5787VENDOR_ATMEL_EEPROM_376KHZ:
13256 case FLASH_5787VENDOR_MICRO_EEPROM_376KHZ:
13257 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000013258 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson321d32a2008-11-21 17:22:19 -080013259 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
13260
13261 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
13262 tw32(NVRAM_CFG1, nvcfg1);
13263 return;
13264 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
13265 case FLASH_57780VENDOR_ATMEL_AT45DB011D:
13266 case FLASH_57780VENDOR_ATMEL_AT45DB011B:
13267 case FLASH_57780VENDOR_ATMEL_AT45DB021D:
13268 case FLASH_57780VENDOR_ATMEL_AT45DB021B:
13269 case FLASH_57780VENDOR_ATMEL_AT45DB041D:
13270 case FLASH_57780VENDOR_ATMEL_AT45DB041B:
13271 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000013272 tg3_flag_set(tp, NVRAM_BUFFERED);
13273 tg3_flag_set(tp, FLASH);
Matt Carlson321d32a2008-11-21 17:22:19 -080013274
13275 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
13276 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
13277 case FLASH_57780VENDOR_ATMEL_AT45DB011D:
13278 case FLASH_57780VENDOR_ATMEL_AT45DB011B:
13279 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
13280 break;
13281 case FLASH_57780VENDOR_ATMEL_AT45DB021D:
13282 case FLASH_57780VENDOR_ATMEL_AT45DB021B:
13283 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
13284 break;
13285 case FLASH_57780VENDOR_ATMEL_AT45DB041D:
13286 case FLASH_57780VENDOR_ATMEL_AT45DB041B:
13287 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
13288 break;
13289 }
13290 break;
13291 case FLASH_5752VENDOR_ST_M45PE10:
13292 case FLASH_5752VENDOR_ST_M45PE20:
13293 case FLASH_5752VENDOR_ST_M45PE40:
13294 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000013295 tg3_flag_set(tp, NVRAM_BUFFERED);
13296 tg3_flag_set(tp, FLASH);
Matt Carlson321d32a2008-11-21 17:22:19 -080013297
13298 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
13299 case FLASH_5752VENDOR_ST_M45PE10:
13300 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
13301 break;
13302 case FLASH_5752VENDOR_ST_M45PE20:
13303 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
13304 break;
13305 case FLASH_5752VENDOR_ST_M45PE40:
13306 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
13307 break;
13308 }
13309 break;
13310 default:
Joe Perches63c3a662011-04-26 08:12:10 +000013311 tg3_flag_set(tp, NO_NVRAM);
Matt Carlson321d32a2008-11-21 17:22:19 -080013312 return;
13313 }
13314
Matt Carlsona1b950d2009-09-01 13:20:17 +000013315 tg3_nvram_get_pagesize(tp, nvcfg1);
13316 if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
Joe Perches63c3a662011-04-26 08:12:10 +000013317 tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
Matt Carlsona1b950d2009-09-01 13:20:17 +000013318}
13319
13320
Bill Pemberton229b1ad2012-12-03 09:22:59 -050013321static void tg3_get_5717_nvram_info(struct tg3 *tp)
Matt Carlsona1b950d2009-09-01 13:20:17 +000013322{
13323 u32 nvcfg1;
13324
13325 nvcfg1 = tr32(NVRAM_CFG1);
13326
13327 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
13328 case FLASH_5717VENDOR_ATMEL_EEPROM:
13329 case FLASH_5717VENDOR_MICRO_EEPROM:
13330 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000013331 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlsona1b950d2009-09-01 13:20:17 +000013332 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
13333
13334 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
13335 tw32(NVRAM_CFG1, nvcfg1);
13336 return;
13337 case FLASH_5717VENDOR_ATMEL_MDB011D:
13338 case FLASH_5717VENDOR_ATMEL_ADB011B:
13339 case FLASH_5717VENDOR_ATMEL_ADB011D:
13340 case FLASH_5717VENDOR_ATMEL_MDB021D:
13341 case FLASH_5717VENDOR_ATMEL_ADB021B:
13342 case FLASH_5717VENDOR_ATMEL_ADB021D:
13343 case FLASH_5717VENDOR_ATMEL_45USPT:
13344 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000013345 tg3_flag_set(tp, NVRAM_BUFFERED);
13346 tg3_flag_set(tp, FLASH);
Matt Carlsona1b950d2009-09-01 13:20:17 +000013347
13348 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
13349 case FLASH_5717VENDOR_ATMEL_MDB021D:
Matt Carlson66ee33b2011-04-05 14:22:51 +000013350 /* Detect size with tg3_nvram_get_size() */
13351 break;
Matt Carlsona1b950d2009-09-01 13:20:17 +000013352 case FLASH_5717VENDOR_ATMEL_ADB021B:
13353 case FLASH_5717VENDOR_ATMEL_ADB021D:
13354 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
13355 break;
13356 default:
13357 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
13358 break;
13359 }
Matt Carlson321d32a2008-11-21 17:22:19 -080013360 break;
Matt Carlsona1b950d2009-09-01 13:20:17 +000013361 case FLASH_5717VENDOR_ST_M_M25PE10:
13362 case FLASH_5717VENDOR_ST_A_M25PE10:
13363 case FLASH_5717VENDOR_ST_M_M45PE10:
13364 case FLASH_5717VENDOR_ST_A_M45PE10:
13365 case FLASH_5717VENDOR_ST_M_M25PE20:
13366 case FLASH_5717VENDOR_ST_A_M25PE20:
13367 case FLASH_5717VENDOR_ST_M_M45PE20:
13368 case FLASH_5717VENDOR_ST_A_M45PE20:
13369 case FLASH_5717VENDOR_ST_25USPT:
13370 case FLASH_5717VENDOR_ST_45USPT:
13371 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000013372 tg3_flag_set(tp, NVRAM_BUFFERED);
13373 tg3_flag_set(tp, FLASH);
Matt Carlsona1b950d2009-09-01 13:20:17 +000013374
13375 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
13376 case FLASH_5717VENDOR_ST_M_M25PE20:
Matt Carlsona1b950d2009-09-01 13:20:17 +000013377 case FLASH_5717VENDOR_ST_M_M45PE20:
Matt Carlson66ee33b2011-04-05 14:22:51 +000013378 /* Detect size with tg3_nvram_get_size() */
13379 break;
13380 case FLASH_5717VENDOR_ST_A_M25PE20:
Matt Carlsona1b950d2009-09-01 13:20:17 +000013381 case FLASH_5717VENDOR_ST_A_M45PE20:
13382 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
13383 break;
13384 default:
13385 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
13386 break;
13387 }
Matt Carlson321d32a2008-11-21 17:22:19 -080013388 break;
Matt Carlsona1b950d2009-09-01 13:20:17 +000013389 default:
Joe Perches63c3a662011-04-26 08:12:10 +000013390 tg3_flag_set(tp, NO_NVRAM);
Matt Carlsona1b950d2009-09-01 13:20:17 +000013391 return;
Matt Carlson321d32a2008-11-21 17:22:19 -080013392 }
Matt Carlsona1b950d2009-09-01 13:20:17 +000013393
13394 tg3_nvram_get_pagesize(tp, nvcfg1);
13395 if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
Joe Perches63c3a662011-04-26 08:12:10 +000013396 tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
Matt Carlson321d32a2008-11-21 17:22:19 -080013397}
13398
Bill Pemberton229b1ad2012-12-03 09:22:59 -050013399static void tg3_get_5720_nvram_info(struct tg3 *tp)
Matt Carlson9b91b5f2011-04-05 14:22:47 +000013400{
13401 u32 nvcfg1, nvmpinstrp;
13402
13403 nvcfg1 = tr32(NVRAM_CFG1);
13404 nvmpinstrp = nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK;
13405
13406 switch (nvmpinstrp) {
13407 case FLASH_5720_EEPROM_HD:
13408 case FLASH_5720_EEPROM_LD:
13409 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000013410 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000013411
13412 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
13413 tw32(NVRAM_CFG1, nvcfg1);
13414 if (nvmpinstrp == FLASH_5720_EEPROM_HD)
13415 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
13416 else
13417 tp->nvram_pagesize = ATMEL_AT24C02_CHIP_SIZE;
13418 return;
13419 case FLASH_5720VENDOR_M_ATMEL_DB011D:
13420 case FLASH_5720VENDOR_A_ATMEL_DB011B:
13421 case FLASH_5720VENDOR_A_ATMEL_DB011D:
13422 case FLASH_5720VENDOR_M_ATMEL_DB021D:
13423 case FLASH_5720VENDOR_A_ATMEL_DB021B:
13424 case FLASH_5720VENDOR_A_ATMEL_DB021D:
13425 case FLASH_5720VENDOR_M_ATMEL_DB041D:
13426 case FLASH_5720VENDOR_A_ATMEL_DB041B:
13427 case FLASH_5720VENDOR_A_ATMEL_DB041D:
13428 case FLASH_5720VENDOR_M_ATMEL_DB081D:
13429 case FLASH_5720VENDOR_A_ATMEL_DB081D:
13430 case FLASH_5720VENDOR_ATMEL_45USPT:
13431 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000013432 tg3_flag_set(tp, NVRAM_BUFFERED);
13433 tg3_flag_set(tp, FLASH);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000013434
13435 switch (nvmpinstrp) {
13436 case FLASH_5720VENDOR_M_ATMEL_DB021D:
13437 case FLASH_5720VENDOR_A_ATMEL_DB021B:
13438 case FLASH_5720VENDOR_A_ATMEL_DB021D:
13439 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
13440 break;
13441 case FLASH_5720VENDOR_M_ATMEL_DB041D:
13442 case FLASH_5720VENDOR_A_ATMEL_DB041B:
13443 case FLASH_5720VENDOR_A_ATMEL_DB041D:
13444 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
13445 break;
13446 case FLASH_5720VENDOR_M_ATMEL_DB081D:
13447 case FLASH_5720VENDOR_A_ATMEL_DB081D:
13448 tp->nvram_size = TG3_NVRAM_SIZE_1MB;
13449 break;
13450 default:
13451 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
13452 break;
13453 }
13454 break;
13455 case FLASH_5720VENDOR_M_ST_M25PE10:
13456 case FLASH_5720VENDOR_M_ST_M45PE10:
13457 case FLASH_5720VENDOR_A_ST_M25PE10:
13458 case FLASH_5720VENDOR_A_ST_M45PE10:
13459 case FLASH_5720VENDOR_M_ST_M25PE20:
13460 case FLASH_5720VENDOR_M_ST_M45PE20:
13461 case FLASH_5720VENDOR_A_ST_M25PE20:
13462 case FLASH_5720VENDOR_A_ST_M45PE20:
13463 case FLASH_5720VENDOR_M_ST_M25PE40:
13464 case FLASH_5720VENDOR_M_ST_M45PE40:
13465 case FLASH_5720VENDOR_A_ST_M25PE40:
13466 case FLASH_5720VENDOR_A_ST_M45PE40:
13467 case FLASH_5720VENDOR_M_ST_M25PE80:
13468 case FLASH_5720VENDOR_M_ST_M45PE80:
13469 case FLASH_5720VENDOR_A_ST_M25PE80:
13470 case FLASH_5720VENDOR_A_ST_M45PE80:
13471 case FLASH_5720VENDOR_ST_25USPT:
13472 case FLASH_5720VENDOR_ST_45USPT:
13473 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000013474 tg3_flag_set(tp, NVRAM_BUFFERED);
13475 tg3_flag_set(tp, FLASH);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000013476
13477 switch (nvmpinstrp) {
13478 case FLASH_5720VENDOR_M_ST_M25PE20:
13479 case FLASH_5720VENDOR_M_ST_M45PE20:
13480 case FLASH_5720VENDOR_A_ST_M25PE20:
13481 case FLASH_5720VENDOR_A_ST_M45PE20:
13482 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
13483 break;
13484 case FLASH_5720VENDOR_M_ST_M25PE40:
13485 case FLASH_5720VENDOR_M_ST_M45PE40:
13486 case FLASH_5720VENDOR_A_ST_M25PE40:
13487 case FLASH_5720VENDOR_A_ST_M45PE40:
13488 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
13489 break;
13490 case FLASH_5720VENDOR_M_ST_M25PE80:
13491 case FLASH_5720VENDOR_M_ST_M45PE80:
13492 case FLASH_5720VENDOR_A_ST_M25PE80:
13493 case FLASH_5720VENDOR_A_ST_M45PE80:
13494 tp->nvram_size = TG3_NVRAM_SIZE_1MB;
13495 break;
13496 default:
13497 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
13498 break;
13499 }
13500 break;
13501 default:
Joe Perches63c3a662011-04-26 08:12:10 +000013502 tg3_flag_set(tp, NO_NVRAM);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000013503 return;
13504 }
13505
13506 tg3_nvram_get_pagesize(tp, nvcfg1);
13507 if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
Joe Perches63c3a662011-04-26 08:12:10 +000013508 tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000013509}
13510
Linus Torvalds1da177e2005-04-16 15:20:36 -070013511/* Chips other than 5700/5701 use the NVRAM for fetching info. */
Bill Pemberton229b1ad2012-12-03 09:22:59 -050013512static void tg3_nvram_init(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013513{
Linus Torvalds1da177e2005-04-16 15:20:36 -070013514 tw32_f(GRC_EEPROM_ADDR,
13515 (EEPROM_ADDR_FSM_RESET |
13516 (EEPROM_DEFAULT_CLOCK_PERIOD <<
13517 EEPROM_ADDR_CLKPERD_SHIFT)));
13518
Michael Chan9d57f012006-12-07 00:23:25 -080013519 msleep(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013520
13521 /* Enable seeprom accesses. */
13522 tw32_f(GRC_LOCAL_CTRL,
13523 tr32(GRC_LOCAL_CTRL) | GRC_LCLCTRL_AUTO_SEEPROM);
13524 udelay(100);
13525
13526 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
13527 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701) {
Joe Perches63c3a662011-04-26 08:12:10 +000013528 tg3_flag_set(tp, NVRAM);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013529
Michael Chanec41c7d2006-01-17 02:40:55 -080013530 if (tg3_nvram_lock(tp)) {
Matt Carlson5129c3a2010-04-05 10:19:23 +000013531 netdev_warn(tp->dev,
13532 "Cannot get nvram lock, %s failed\n",
Joe Perches05dbe002010-02-17 19:44:19 +000013533 __func__);
Michael Chanec41c7d2006-01-17 02:40:55 -080013534 return;
13535 }
Michael Chane6af3012005-04-21 17:12:05 -070013536 tg3_enable_nvram_access(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013537
Matt Carlson989a9d22007-05-05 11:51:05 -070013538 tp->nvram_size = 0;
13539
Michael Chan361b4ac2005-04-21 17:11:21 -070013540 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752)
13541 tg3_get_5752_nvram_info(tp);
Michael Chand3c7b882006-03-23 01:28:25 -080013542 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755)
13543 tg3_get_5755_nvram_info(tp);
Matt Carlsond30cdd22007-10-07 23:28:35 -070013544 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787 ||
Matt Carlson57e69832008-05-25 23:48:31 -070013545 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
13546 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
Michael Chan1b277772006-03-20 22:27:48 -080013547 tg3_get_5787_nvram_info(tp);
Matt Carlson6b91fa02007-10-10 18:01:09 -070013548 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
13549 tg3_get_5761_nvram_info(tp);
Michael Chanb5d37722006-09-27 16:06:21 -070013550 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
13551 tg3_get_5906_nvram_info(tp);
Matt Carlsonb703df62009-12-03 08:36:21 +000013552 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
Matt Carlson55086ad2011-12-14 11:09:59 +000013553 tg3_flag(tp, 57765_CLASS))
Matt Carlson321d32a2008-11-21 17:22:19 -080013554 tg3_get_57780_nvram_info(tp);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000013555 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
13556 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
Matt Carlsona1b950d2009-09-01 13:20:17 +000013557 tg3_get_5717_nvram_info(tp);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000013558 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
13559 tg3_get_5720_nvram_info(tp);
Michael Chan361b4ac2005-04-21 17:11:21 -070013560 else
13561 tg3_get_nvram_info(tp);
13562
Matt Carlson989a9d22007-05-05 11:51:05 -070013563 if (tp->nvram_size == 0)
13564 tg3_get_nvram_size(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013565
Michael Chane6af3012005-04-21 17:12:05 -070013566 tg3_disable_nvram_access(tp);
Michael Chan381291b2005-12-13 21:08:21 -080013567 tg3_nvram_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013568
13569 } else {
Joe Perches63c3a662011-04-26 08:12:10 +000013570 tg3_flag_clear(tp, NVRAM);
13571 tg3_flag_clear(tp, NVRAM_BUFFERED);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013572
13573 tg3_get_eeprom_size(tp);
13574 }
13575}
13576
Linus Torvalds1da177e2005-04-16 15:20:36 -070013577struct subsys_tbl_ent {
13578 u16 subsys_vendor, subsys_devid;
13579 u32 phy_id;
13580};
13581
Bill Pemberton229b1ad2012-12-03 09:22:59 -050013582static struct subsys_tbl_ent subsys_id_to_phy_id[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070013583 /* Broadcom boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000013584 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000013585 TG3PCI_SUBDEVICE_ID_BROADCOM_95700A6, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013586 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000013587 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A5, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013588 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000013589 TG3PCI_SUBDEVICE_ID_BROADCOM_95700T6, TG3_PHY_ID_BCM8002 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013590 { TG3PCI_SUBVENDOR_ID_BROADCOM,
13591 TG3PCI_SUBDEVICE_ID_BROADCOM_95700A9, 0 },
13592 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000013593 TG3PCI_SUBDEVICE_ID_BROADCOM_95701T1, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013594 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000013595 TG3PCI_SUBDEVICE_ID_BROADCOM_95701T8, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013596 { TG3PCI_SUBVENDOR_ID_BROADCOM,
13597 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A7, 0 },
13598 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000013599 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A10, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013600 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000013601 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A12, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013602 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000013603 TG3PCI_SUBDEVICE_ID_BROADCOM_95703AX1, TG3_PHY_ID_BCM5703 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013604 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000013605 TG3PCI_SUBDEVICE_ID_BROADCOM_95703AX2, TG3_PHY_ID_BCM5703 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070013606
13607 /* 3com boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000013608 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000013609 TG3PCI_SUBDEVICE_ID_3COM_3C996T, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013610 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000013611 TG3PCI_SUBDEVICE_ID_3COM_3C996BT, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013612 { TG3PCI_SUBVENDOR_ID_3COM,
13613 TG3PCI_SUBDEVICE_ID_3COM_3C996SX, 0 },
13614 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000013615 TG3PCI_SUBDEVICE_ID_3COM_3C1000T, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013616 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000013617 TG3PCI_SUBDEVICE_ID_3COM_3C940BR01, TG3_PHY_ID_BCM5701 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070013618
13619 /* DELL boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000013620 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000013621 TG3PCI_SUBDEVICE_ID_DELL_VIPER, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013622 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000013623 TG3PCI_SUBDEVICE_ID_DELL_JAGUAR, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013624 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000013625 TG3PCI_SUBDEVICE_ID_DELL_MERLOT, TG3_PHY_ID_BCM5411 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013626 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000013627 TG3PCI_SUBDEVICE_ID_DELL_SLIM_MERLOT, TG3_PHY_ID_BCM5411 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070013628
13629 /* Compaq boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000013630 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000013631 TG3PCI_SUBDEVICE_ID_COMPAQ_BANSHEE, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013632 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000013633 TG3PCI_SUBDEVICE_ID_COMPAQ_BANSHEE_2, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013634 { TG3PCI_SUBVENDOR_ID_COMPAQ,
13635 TG3PCI_SUBDEVICE_ID_COMPAQ_CHANGELING, 0 },
13636 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000013637 TG3PCI_SUBDEVICE_ID_COMPAQ_NC7780, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000013638 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000013639 TG3PCI_SUBDEVICE_ID_COMPAQ_NC7780_2, TG3_PHY_ID_BCM5701 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070013640
13641 /* IBM boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000013642 { TG3PCI_SUBVENDOR_ID_IBM,
13643 TG3PCI_SUBDEVICE_ID_IBM_5703SAX2, 0 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013644};
13645
Bill Pemberton229b1ad2012-12-03 09:22:59 -050013646static struct subsys_tbl_ent *tg3_lookup_by_subsys(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013647{
13648 int i;
13649
13650 for (i = 0; i < ARRAY_SIZE(subsys_id_to_phy_id); i++) {
13651 if ((subsys_id_to_phy_id[i].subsys_vendor ==
13652 tp->pdev->subsystem_vendor) &&
13653 (subsys_id_to_phy_id[i].subsys_devid ==
13654 tp->pdev->subsystem_device))
13655 return &subsys_id_to_phy_id[i];
13656 }
13657 return NULL;
13658}
13659
Bill Pemberton229b1ad2012-12-03 09:22:59 -050013660static void tg3_get_eeprom_hw_cfg(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013661{
Linus Torvalds1da177e2005-04-16 15:20:36 -070013662 u32 val;
David S. Millerf49639e2006-06-09 11:58:36 -070013663
Matt Carlson79eb6902010-02-17 15:17:03 +000013664 tp->phy_id = TG3_PHY_ID_INVALID;
Michael Chan7d0c41e2005-04-21 17:06:20 -070013665 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
13666
Gary Zambranoa85feb82007-05-05 11:52:19 -070013667 /* Assume an onboard device and WOL capable by default. */
Joe Perches63c3a662011-04-26 08:12:10 +000013668 tg3_flag_set(tp, EEPROM_WRITE_PROT);
13669 tg3_flag_set(tp, WOL_CAP);
David S. Miller72b845e2006-03-14 14:11:48 -080013670
Michael Chanb5d37722006-09-27 16:06:21 -070013671 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan9d26e212006-12-07 00:21:14 -080013672 if (!(tr32(PCIE_TRANSACTION_CFG) & PCIE_TRANS_CFG_LOM)) {
Joe Perches63c3a662011-04-26 08:12:10 +000013673 tg3_flag_clear(tp, EEPROM_WRITE_PROT);
13674 tg3_flag_set(tp, IS_NIC);
Michael Chan9d26e212006-12-07 00:21:14 -080013675 }
Matt Carlson0527ba32007-10-10 18:03:30 -070013676 val = tr32(VCPU_CFGSHDW);
13677 if (val & VCPU_CFGSHDW_ASPM_DBNC)
Joe Perches63c3a662011-04-26 08:12:10 +000013678 tg3_flag_set(tp, ASPM_WORKAROUND);
Matt Carlson0527ba32007-10-10 18:03:30 -070013679 if ((val & VCPU_CFGSHDW_WOL_ENABLE) &&
Rafael J. Wysocki6fdbab92011-04-28 11:02:15 +000013680 (val & VCPU_CFGSHDW_WOL_MAGPKT)) {
Joe Perches63c3a662011-04-26 08:12:10 +000013681 tg3_flag_set(tp, WOL_ENABLE);
Rafael J. Wysocki6fdbab92011-04-28 11:02:15 +000013682 device_set_wakeup_enable(&tp->pdev->dev, true);
13683 }
Matt Carlson05ac4cb2008-11-03 16:53:46 -080013684 goto done;
Michael Chanb5d37722006-09-27 16:06:21 -070013685 }
13686
Linus Torvalds1da177e2005-04-16 15:20:36 -070013687 tg3_read_mem(tp, NIC_SRAM_DATA_SIG, &val);
13688 if (val == NIC_SRAM_DATA_SIG_MAGIC) {
13689 u32 nic_cfg, led_cfg;
Matt Carlsona9daf362008-05-25 23:49:44 -070013690 u32 nic_phy_id, ver, cfg2 = 0, cfg4 = 0, eeprom_phy_id;
Michael Chan7d0c41e2005-04-21 17:06:20 -070013691 int eeprom_phy_serdes = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013692
13693 tg3_read_mem(tp, NIC_SRAM_DATA_CFG, &nic_cfg);
13694 tp->nic_sram_data_cfg = nic_cfg;
13695
13696 tg3_read_mem(tp, NIC_SRAM_DATA_VER, &ver);
13697 ver >>= NIC_SRAM_DATA_VER_SHIFT;
Matt Carlson6ff6f812011-05-19 12:12:54 +000013698 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
13699 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701 &&
13700 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5703 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070013701 (ver > 0) && (ver < 0x100))
13702 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_2, &cfg2);
13703
Matt Carlsona9daf362008-05-25 23:49:44 -070013704 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
13705 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_4, &cfg4);
13706
Linus Torvalds1da177e2005-04-16 15:20:36 -070013707 if ((nic_cfg & NIC_SRAM_DATA_CFG_PHY_TYPE_MASK) ==
13708 NIC_SRAM_DATA_CFG_PHY_TYPE_FIBER)
13709 eeprom_phy_serdes = 1;
13710
13711 tg3_read_mem(tp, NIC_SRAM_DATA_PHY_ID, &nic_phy_id);
13712 if (nic_phy_id != 0) {
13713 u32 id1 = nic_phy_id & NIC_SRAM_DATA_PHY_ID1_MASK;
13714 u32 id2 = nic_phy_id & NIC_SRAM_DATA_PHY_ID2_MASK;
13715
13716 eeprom_phy_id = (id1 >> 16) << 10;
13717 eeprom_phy_id |= (id2 & 0xfc00) << 16;
13718 eeprom_phy_id |= (id2 & 0x03ff) << 0;
13719 } else
13720 eeprom_phy_id = 0;
13721
Michael Chan7d0c41e2005-04-21 17:06:20 -070013722 tp->phy_id = eeprom_phy_id;
Michael Chan747e8f82005-07-25 12:33:22 -070013723 if (eeprom_phy_serdes) {
Joe Perches63c3a662011-04-26 08:12:10 +000013724 if (!tg3_flag(tp, 5705_PLUS))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013725 tp->phy_flags |= TG3_PHYFLG_PHY_SERDES;
Matt Carlsona50d0792010-06-05 17:24:37 +000013726 else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013727 tp->phy_flags |= TG3_PHYFLG_MII_SERDES;
Michael Chan747e8f82005-07-25 12:33:22 -070013728 }
Michael Chan7d0c41e2005-04-21 17:06:20 -070013729
Joe Perches63c3a662011-04-26 08:12:10 +000013730 if (tg3_flag(tp, 5750_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -070013731 led_cfg = cfg2 & (NIC_SRAM_DATA_CFG_LED_MODE_MASK |
13732 SHASTA_EXT_LED_MODE_MASK);
John W. Linvillecbf46852005-04-21 17:01:29 -070013733 else
Linus Torvalds1da177e2005-04-16 15:20:36 -070013734 led_cfg = nic_cfg & NIC_SRAM_DATA_CFG_LED_MODE_MASK;
13735
13736 switch (led_cfg) {
13737 default:
13738 case NIC_SRAM_DATA_CFG_LED_MODE_PHY_1:
13739 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
13740 break;
13741
13742 case NIC_SRAM_DATA_CFG_LED_MODE_PHY_2:
13743 tp->led_ctrl = LED_CTRL_MODE_PHY_2;
13744 break;
13745
13746 case NIC_SRAM_DATA_CFG_LED_MODE_MAC:
13747 tp->led_ctrl = LED_CTRL_MODE_MAC;
Michael Chan9ba27792005-06-06 15:16:20 -070013748
13749 /* Default to PHY_1_MODE if 0 (MAC_MODE) is
13750 * read on some older 5700/5701 bootcode.
13751 */
13752 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
13753 ASIC_REV_5700 ||
13754 GET_ASIC_REV(tp->pci_chip_rev_id) ==
13755 ASIC_REV_5701)
13756 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
13757
Linus Torvalds1da177e2005-04-16 15:20:36 -070013758 break;
13759
13760 case SHASTA_EXT_LED_SHARED:
13761 tp->led_ctrl = LED_CTRL_MODE_SHARED;
13762 if (tp->pci_chip_rev_id != CHIPREV_ID_5750_A0 &&
13763 tp->pci_chip_rev_id != CHIPREV_ID_5750_A1)
13764 tp->led_ctrl |= (LED_CTRL_MODE_PHY_1 |
13765 LED_CTRL_MODE_PHY_2);
13766 break;
13767
13768 case SHASTA_EXT_LED_MAC:
13769 tp->led_ctrl = LED_CTRL_MODE_SHASTA_MAC;
13770 break;
13771
13772 case SHASTA_EXT_LED_COMBO:
13773 tp->led_ctrl = LED_CTRL_MODE_COMBO;
13774 if (tp->pci_chip_rev_id != CHIPREV_ID_5750_A0)
13775 tp->led_ctrl |= (LED_CTRL_MODE_PHY_1 |
13776 LED_CTRL_MODE_PHY_2);
13777 break;
13778
Stephen Hemminger855e1112008-04-16 16:37:28 -070013779 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013780
13781 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
13782 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) &&
13783 tp->pdev->subsystem_vendor == PCI_VENDOR_ID_DELL)
13784 tp->led_ctrl = LED_CTRL_MODE_PHY_2;
13785
Matt Carlsonb2a5c192008-04-03 21:44:44 -070013786 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX)
13787 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
Matt Carlson5f608912007-11-12 21:17:07 -080013788
Michael Chan9d26e212006-12-07 00:21:14 -080013789 if (nic_cfg & NIC_SRAM_DATA_CFG_EEPROM_WP) {
Joe Perches63c3a662011-04-26 08:12:10 +000013790 tg3_flag_set(tp, EEPROM_WRITE_PROT);
Michael Chan9d26e212006-12-07 00:21:14 -080013791 if ((tp->pdev->subsystem_vendor ==
13792 PCI_VENDOR_ID_ARIMA) &&
13793 (tp->pdev->subsystem_device == 0x205a ||
13794 tp->pdev->subsystem_device == 0x2063))
Joe Perches63c3a662011-04-26 08:12:10 +000013795 tg3_flag_clear(tp, EEPROM_WRITE_PROT);
Michael Chan9d26e212006-12-07 00:21:14 -080013796 } else {
Joe Perches63c3a662011-04-26 08:12:10 +000013797 tg3_flag_clear(tp, EEPROM_WRITE_PROT);
13798 tg3_flag_set(tp, IS_NIC);
Michael Chan9d26e212006-12-07 00:21:14 -080013799 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013800
13801 if (nic_cfg & NIC_SRAM_DATA_CFG_ASF_ENABLE) {
Joe Perches63c3a662011-04-26 08:12:10 +000013802 tg3_flag_set(tp, ENABLE_ASF);
13803 if (tg3_flag(tp, 5750_PLUS))
13804 tg3_flag_set(tp, ASF_NEW_HANDSHAKE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013805 }
Matt Carlsonb2b98d42008-11-03 16:52:32 -080013806
13807 if ((nic_cfg & NIC_SRAM_DATA_CFG_APE_ENABLE) &&
Joe Perches63c3a662011-04-26 08:12:10 +000013808 tg3_flag(tp, 5750_PLUS))
13809 tg3_flag_set(tp, ENABLE_APE);
Matt Carlsonb2b98d42008-11-03 16:52:32 -080013810
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013811 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES &&
Gary Zambranoa85feb82007-05-05 11:52:19 -070013812 !(nic_cfg & NIC_SRAM_DATA_CFG_FIBER_WOL))
Joe Perches63c3a662011-04-26 08:12:10 +000013813 tg3_flag_clear(tp, WOL_CAP);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013814
Joe Perches63c3a662011-04-26 08:12:10 +000013815 if (tg3_flag(tp, WOL_CAP) &&
Rafael J. Wysocki6fdbab92011-04-28 11:02:15 +000013816 (nic_cfg & NIC_SRAM_DATA_CFG_WOL_ENABLE)) {
Joe Perches63c3a662011-04-26 08:12:10 +000013817 tg3_flag_set(tp, WOL_ENABLE);
Rafael J. Wysocki6fdbab92011-04-28 11:02:15 +000013818 device_set_wakeup_enable(&tp->pdev->dev, true);
13819 }
Matt Carlson0527ba32007-10-10 18:03:30 -070013820
Linus Torvalds1da177e2005-04-16 15:20:36 -070013821 if (cfg2 & (1 << 17))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013822 tp->phy_flags |= TG3_PHYFLG_CAPACITIVE_COUPLING;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013823
13824 /* serdes signal pre-emphasis in register 0x590 set by */
13825 /* bootcode if bit 18 is set */
13826 if (cfg2 & (1 << 18))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013827 tp->phy_flags |= TG3_PHYFLG_SERDES_PREEMPHASIS;
Matt Carlson8ed5d972007-05-07 00:25:49 -070013828
Joe Perches63c3a662011-04-26 08:12:10 +000013829 if ((tg3_flag(tp, 57765_PLUS) ||
13830 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
13831 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX)) &&
Matt Carlson6833c042008-11-21 17:18:59 -080013832 (cfg2 & NIC_SRAM_DATA_CFG_2_APD_EN))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013833 tp->phy_flags |= TG3_PHYFLG_ENABLE_APD;
Matt Carlson6833c042008-11-21 17:18:59 -080013834
Joe Perches63c3a662011-04-26 08:12:10 +000013835 if (tg3_flag(tp, PCI_EXPRESS) &&
Matt Carlson8c69b1e2010-08-02 11:26:00 +000013836 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
Joe Perches63c3a662011-04-26 08:12:10 +000013837 !tg3_flag(tp, 57765_PLUS)) {
Matt Carlson8ed5d972007-05-07 00:25:49 -070013838 u32 cfg3;
13839
13840 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_3, &cfg3);
13841 if (cfg3 & NIC_SRAM_ASPM_DEBOUNCE)
Joe Perches63c3a662011-04-26 08:12:10 +000013842 tg3_flag_set(tp, ASPM_WORKAROUND);
Matt Carlson8ed5d972007-05-07 00:25:49 -070013843 }
Matt Carlsona9daf362008-05-25 23:49:44 -070013844
Matt Carlson14417062010-02-17 15:16:59 +000013845 if (cfg4 & NIC_SRAM_RGMII_INBAND_DISABLE)
Joe Perches63c3a662011-04-26 08:12:10 +000013846 tg3_flag_set(tp, RGMII_INBAND_DISABLE);
Matt Carlsona9daf362008-05-25 23:49:44 -070013847 if (cfg4 & NIC_SRAM_RGMII_EXT_IBND_RX_EN)
Joe Perches63c3a662011-04-26 08:12:10 +000013848 tg3_flag_set(tp, RGMII_EXT_IBND_RX_EN);
Matt Carlsona9daf362008-05-25 23:49:44 -070013849 if (cfg4 & NIC_SRAM_RGMII_EXT_IBND_TX_EN)
Joe Perches63c3a662011-04-26 08:12:10 +000013850 tg3_flag_set(tp, RGMII_EXT_IBND_TX_EN);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013851 }
Matt Carlson05ac4cb2008-11-03 16:53:46 -080013852done:
Joe Perches63c3a662011-04-26 08:12:10 +000013853 if (tg3_flag(tp, WOL_CAP))
Rafael J. Wysocki43067ed2011-02-10 06:53:09 +000013854 device_set_wakeup_enable(&tp->pdev->dev,
Joe Perches63c3a662011-04-26 08:12:10 +000013855 tg3_flag(tp, WOL_ENABLE));
Rafael J. Wysocki43067ed2011-02-10 06:53:09 +000013856 else
13857 device_set_wakeup_capable(&tp->pdev->dev, false);
Michael Chan7d0c41e2005-04-21 17:06:20 -070013858}
13859
Bill Pemberton229b1ad2012-12-03 09:22:59 -050013860static int tg3_issue_otp_command(struct tg3 *tp, u32 cmd)
Matt Carlsonb2a5c192008-04-03 21:44:44 -070013861{
13862 int i;
13863 u32 val;
13864
13865 tw32(OTP_CTRL, cmd | OTP_CTRL_OTP_CMD_START);
13866 tw32(OTP_CTRL, cmd);
13867
13868 /* Wait for up to 1 ms for command to execute. */
13869 for (i = 0; i < 100; i++) {
13870 val = tr32(OTP_STATUS);
13871 if (val & OTP_STATUS_CMD_DONE)
13872 break;
13873 udelay(10);
13874 }
13875
13876 return (val & OTP_STATUS_CMD_DONE) ? 0 : -EBUSY;
13877}
13878
13879/* Read the gphy configuration from the OTP region of the chip. The gphy
13880 * configuration is a 32-bit value that straddles the alignment boundary.
13881 * We do two 32-bit reads and then shift and merge the results.
13882 */
Bill Pemberton229b1ad2012-12-03 09:22:59 -050013883static u32 tg3_read_otp_phycfg(struct tg3 *tp)
Matt Carlsonb2a5c192008-04-03 21:44:44 -070013884{
13885 u32 bhalf_otp, thalf_otp;
13886
13887 tw32(OTP_MODE, OTP_MODE_OTP_THRU_GRC);
13888
13889 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_INIT))
13890 return 0;
13891
13892 tw32(OTP_ADDRESS, OTP_ADDRESS_MAGIC1);
13893
13894 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_READ))
13895 return 0;
13896
13897 thalf_otp = tr32(OTP_READ_DATA);
13898
13899 tw32(OTP_ADDRESS, OTP_ADDRESS_MAGIC2);
13900
13901 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_READ))
13902 return 0;
13903
13904 bhalf_otp = tr32(OTP_READ_DATA);
13905
13906 return ((thalf_otp & 0x0000ffff) << 16) | (bhalf_otp >> 16);
13907}
13908
Bill Pemberton229b1ad2012-12-03 09:22:59 -050013909static void tg3_phy_init_link_config(struct tg3 *tp)
Matt Carlsone256f8a2011-03-09 16:58:24 +000013910{
Hiroaki SHIMODA202ff1c2011-11-22 04:05:41 +000013911 u32 adv = ADVERTISED_Autoneg;
Matt Carlsone256f8a2011-03-09 16:58:24 +000013912
13913 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
13914 adv |= ADVERTISED_1000baseT_Half |
13915 ADVERTISED_1000baseT_Full;
13916
13917 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
13918 adv |= ADVERTISED_100baseT_Half |
13919 ADVERTISED_100baseT_Full |
13920 ADVERTISED_10baseT_Half |
13921 ADVERTISED_10baseT_Full |
13922 ADVERTISED_TP;
13923 else
13924 adv |= ADVERTISED_FIBRE;
13925
13926 tp->link_config.advertising = adv;
Matt Carlsone7405222012-02-13 15:20:16 +000013927 tp->link_config.speed = SPEED_UNKNOWN;
13928 tp->link_config.duplex = DUPLEX_UNKNOWN;
Matt Carlsone256f8a2011-03-09 16:58:24 +000013929 tp->link_config.autoneg = AUTONEG_ENABLE;
Matt Carlsone7405222012-02-13 15:20:16 +000013930 tp->link_config.active_speed = SPEED_UNKNOWN;
13931 tp->link_config.active_duplex = DUPLEX_UNKNOWN;
Matt Carlson34655ad2012-02-22 12:35:18 +000013932
13933 tp->old_link = -1;
Matt Carlsone256f8a2011-03-09 16:58:24 +000013934}
13935
Bill Pemberton229b1ad2012-12-03 09:22:59 -050013936static int tg3_phy_probe(struct tg3 *tp)
Michael Chan7d0c41e2005-04-21 17:06:20 -070013937{
13938 u32 hw_phy_id_1, hw_phy_id_2;
13939 u32 hw_phy_id, hw_phy_id_masked;
13940 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013941
Matt Carlsone256f8a2011-03-09 16:58:24 +000013942 /* flow control autonegotiation is default behavior */
Joe Perches63c3a662011-04-26 08:12:10 +000013943 tg3_flag_set(tp, PAUSE_AUTONEG);
Matt Carlsone256f8a2011-03-09 16:58:24 +000013944 tp->link_config.flowctrl = FLOW_CTRL_TX | FLOW_CTRL_RX;
13945
Michael Chan8151ad52012-07-29 19:15:41 +000013946 if (tg3_flag(tp, ENABLE_APE)) {
13947 switch (tp->pci_fn) {
13948 case 0:
13949 tp->phy_ape_lock = TG3_APE_LOCK_PHY0;
13950 break;
13951 case 1:
13952 tp->phy_ape_lock = TG3_APE_LOCK_PHY1;
13953 break;
13954 case 2:
13955 tp->phy_ape_lock = TG3_APE_LOCK_PHY2;
13956 break;
13957 case 3:
13958 tp->phy_ape_lock = TG3_APE_LOCK_PHY3;
13959 break;
13960 }
13961 }
13962
Joe Perches63c3a662011-04-26 08:12:10 +000013963 if (tg3_flag(tp, USE_PHYLIB))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070013964 return tg3_phy_init(tp);
13965
Linus Torvalds1da177e2005-04-16 15:20:36 -070013966 /* Reading the PHY ID register can conflict with ASF
Nick Andrew877d0312009-01-26 11:06:57 +010013967 * firmware access to the PHY hardware.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013968 */
13969 err = 0;
Joe Perches63c3a662011-04-26 08:12:10 +000013970 if (tg3_flag(tp, ENABLE_ASF) || tg3_flag(tp, ENABLE_APE)) {
Matt Carlson79eb6902010-02-17 15:17:03 +000013971 hw_phy_id = hw_phy_id_masked = TG3_PHY_ID_INVALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013972 } else {
13973 /* Now read the physical PHY_ID from the chip and verify
13974 * that it is sane. If it doesn't look good, we fall back
13975 * to either the hard-coded table based PHY_ID and failing
13976 * that the value found in the eeprom area.
13977 */
13978 err |= tg3_readphy(tp, MII_PHYSID1, &hw_phy_id_1);
13979 err |= tg3_readphy(tp, MII_PHYSID2, &hw_phy_id_2);
13980
13981 hw_phy_id = (hw_phy_id_1 & 0xffff) << 10;
13982 hw_phy_id |= (hw_phy_id_2 & 0xfc00) << 16;
13983 hw_phy_id |= (hw_phy_id_2 & 0x03ff) << 0;
13984
Matt Carlson79eb6902010-02-17 15:17:03 +000013985 hw_phy_id_masked = hw_phy_id & TG3_PHY_ID_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013986 }
13987
Matt Carlson79eb6902010-02-17 15:17:03 +000013988 if (!err && TG3_KNOWN_PHY_ID(hw_phy_id_masked)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070013989 tp->phy_id = hw_phy_id;
Matt Carlson79eb6902010-02-17 15:17:03 +000013990 if (hw_phy_id_masked == TG3_PHY_ID_BCM8002)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013991 tp->phy_flags |= TG3_PHYFLG_PHY_SERDES;
Michael Chanda6b2d02005-08-19 12:54:29 -070013992 else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013993 tp->phy_flags &= ~TG3_PHYFLG_PHY_SERDES;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013994 } else {
Matt Carlson79eb6902010-02-17 15:17:03 +000013995 if (tp->phy_id != TG3_PHY_ID_INVALID) {
Michael Chan7d0c41e2005-04-21 17:06:20 -070013996 /* Do nothing, phy ID already set up in
13997 * tg3_get_eeprom_hw_cfg().
13998 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070013999 } else {
14000 struct subsys_tbl_ent *p;
14001
14002 /* No eeprom signature? Try the hardcoded
14003 * subsys device table.
14004 */
Matt Carlson24daf2b2010-02-17 15:17:02 +000014005 p = tg3_lookup_by_subsys(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014006 if (!p)
14007 return -ENODEV;
14008
14009 tp->phy_id = p->phy_id;
14010 if (!tp->phy_id ||
Matt Carlson79eb6902010-02-17 15:17:03 +000014011 tp->phy_id == TG3_PHY_ID_BCM8002)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014012 tp->phy_flags |= TG3_PHYFLG_PHY_SERDES;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014013 }
14014 }
14015
Matt Carlsona6b68da2010-12-06 08:28:52 +000014016 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES) &&
Matt Carlson5baa5e92011-07-20 10:20:53 +000014017 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
14018 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720 ||
14019 (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718 &&
Matt Carlsona6b68da2010-12-06 08:28:52 +000014020 tp->pci_chip_rev_id != CHIPREV_ID_5717_A0) ||
14021 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765 &&
14022 tp->pci_chip_rev_id != CHIPREV_ID_57765_A0)))
Matt Carlson52b02d02010-10-14 10:37:41 +000014023 tp->phy_flags |= TG3_PHYFLG_EEE_CAP;
14024
Matt Carlsone256f8a2011-03-09 16:58:24 +000014025 tg3_phy_init_link_config(tp);
14026
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014027 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES) &&
Joe Perches63c3a662011-04-26 08:12:10 +000014028 !tg3_flag(tp, ENABLE_APE) &&
14029 !tg3_flag(tp, ENABLE_ASF)) {
Matt Carlsone2bf73e2011-12-08 14:40:15 +000014030 u32 bmsr, dummy;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014031
14032 tg3_readphy(tp, MII_BMSR, &bmsr);
14033 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
14034 (bmsr & BMSR_LSTATUS))
14035 goto skip_phy_reset;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040014036
Linus Torvalds1da177e2005-04-16 15:20:36 -070014037 err = tg3_phy_reset(tp);
14038 if (err)
14039 return err;
14040
Matt Carlson42b64a42011-05-19 12:12:49 +000014041 tg3_phy_set_wirespeed(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014042
Matt Carlsone2bf73e2011-12-08 14:40:15 +000014043 if (!tg3_phy_copper_an_config_ok(tp, &dummy)) {
Matt Carlson42b64a42011-05-19 12:12:49 +000014044 tg3_phy_autoneg_cfg(tp, tp->link_config.advertising,
14045 tp->link_config.flowctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014046
14047 tg3_writephy(tp, MII_BMCR,
14048 BMCR_ANENABLE | BMCR_ANRESTART);
14049 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014050 }
14051
14052skip_phy_reset:
Matt Carlson79eb6902010-02-17 15:17:03 +000014053 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070014054 err = tg3_init_5401phy_dsp(tp);
14055 if (err)
14056 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014057
Linus Torvalds1da177e2005-04-16 15:20:36 -070014058 err = tg3_init_5401phy_dsp(tp);
14059 }
14060
Linus Torvalds1da177e2005-04-16 15:20:36 -070014061 return err;
14062}
14063
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014064static void tg3_read_vpd(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014065{
Matt Carlsona4a8bb12010-09-15 09:00:00 +000014066 u8 *vpd_data;
Matt Carlson4181b2c2010-02-26 14:04:45 +000014067 unsigned int block_end, rosize, len;
Matt Carlson535a4902011-07-20 10:20:56 +000014068 u32 vpdlen;
Matt Carlson184b8902010-04-05 10:19:25 +000014069 int j, i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014070
Matt Carlson535a4902011-07-20 10:20:56 +000014071 vpd_data = (u8 *)tg3_vpd_readblock(tp, &vpdlen);
Matt Carlsona4a8bb12010-09-15 09:00:00 +000014072 if (!vpd_data)
14073 goto out_no_vpd;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014074
Matt Carlson535a4902011-07-20 10:20:56 +000014075 i = pci_vpd_find_tag(vpd_data, 0, vpdlen, PCI_VPD_LRDT_RO_DATA);
Matt Carlson4181b2c2010-02-26 14:04:45 +000014076 if (i < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014077 goto out_not_found;
Matt Carlson4181b2c2010-02-26 14:04:45 +000014078
14079 rosize = pci_vpd_lrdt_size(&vpd_data[i]);
14080 block_end = i + PCI_VPD_LRDT_TAG_SIZE + rosize;
14081 i += PCI_VPD_LRDT_TAG_SIZE;
14082
Matt Carlson535a4902011-07-20 10:20:56 +000014083 if (block_end > vpdlen)
Matt Carlson4181b2c2010-02-26 14:04:45 +000014084 goto out_not_found;
14085
Matt Carlson184b8902010-04-05 10:19:25 +000014086 j = pci_vpd_find_info_keyword(vpd_data, i, rosize,
14087 PCI_VPD_RO_KEYWORD_MFR_ID);
14088 if (j > 0) {
14089 len = pci_vpd_info_field_size(&vpd_data[j]);
14090
14091 j += PCI_VPD_INFO_FLD_HDR_SIZE;
14092 if (j + len > block_end || len != 4 ||
14093 memcmp(&vpd_data[j], "1028", 4))
14094 goto partno;
14095
14096 j = pci_vpd_find_info_keyword(vpd_data, i, rosize,
14097 PCI_VPD_RO_KEYWORD_VENDOR0);
14098 if (j < 0)
14099 goto partno;
14100
14101 len = pci_vpd_info_field_size(&vpd_data[j]);
14102
14103 j += PCI_VPD_INFO_FLD_HDR_SIZE;
14104 if (j + len > block_end)
14105 goto partno;
14106
14107 memcpy(tp->fw_ver, &vpd_data[j], len);
Matt Carlson535a4902011-07-20 10:20:56 +000014108 strncat(tp->fw_ver, " bc ", vpdlen - len - 1);
Matt Carlson184b8902010-04-05 10:19:25 +000014109 }
14110
14111partno:
Matt Carlson4181b2c2010-02-26 14:04:45 +000014112 i = pci_vpd_find_info_keyword(vpd_data, i, rosize,
14113 PCI_VPD_RO_KEYWORD_PARTNO);
14114 if (i < 0)
14115 goto out_not_found;
14116
14117 len = pci_vpd_info_field_size(&vpd_data[i]);
14118
14119 i += PCI_VPD_INFO_FLD_HDR_SIZE;
14120 if (len > TG3_BPN_SIZE ||
Matt Carlson535a4902011-07-20 10:20:56 +000014121 (len + i) > vpdlen)
Matt Carlson4181b2c2010-02-26 14:04:45 +000014122 goto out_not_found;
14123
14124 memcpy(tp->board_part_number, &vpd_data[i], len);
14125
Linus Torvalds1da177e2005-04-16 15:20:36 -070014126out_not_found:
Matt Carlsona4a8bb12010-09-15 09:00:00 +000014127 kfree(vpd_data);
Matt Carlson37a949c2010-09-30 10:34:33 +000014128 if (tp->board_part_number[0])
Matt Carlsona4a8bb12010-09-15 09:00:00 +000014129 return;
14130
14131out_no_vpd:
Matt Carlson37a949c2010-09-30 10:34:33 +000014132 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717) {
Michael Chan79d49692012-11-05 14:26:29 +000014133 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717 ||
14134 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717_C)
Matt Carlson37a949c2010-09-30 10:34:33 +000014135 strcpy(tp->board_part_number, "BCM5717");
14136 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718)
14137 strcpy(tp->board_part_number, "BCM5718");
14138 else
14139 goto nomatch;
14140 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780) {
14141 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57780)
14142 strcpy(tp->board_part_number, "BCM57780");
14143 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57760)
14144 strcpy(tp->board_part_number, "BCM57760");
14145 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57790)
14146 strcpy(tp->board_part_number, "BCM57790");
14147 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57788)
14148 strcpy(tp->board_part_number, "BCM57788");
14149 else
14150 goto nomatch;
14151 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765) {
14152 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57761)
14153 strcpy(tp->board_part_number, "BCM57761");
14154 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57765)
14155 strcpy(tp->board_part_number, "BCM57765");
14156 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57781)
14157 strcpy(tp->board_part_number, "BCM57781");
14158 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57785)
14159 strcpy(tp->board_part_number, "BCM57785");
14160 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57791)
14161 strcpy(tp->board_part_number, "BCM57791");
14162 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57795)
14163 strcpy(tp->board_part_number, "BCM57795");
14164 else
14165 goto nomatch;
Matt Carlson55086ad2011-12-14 11:09:59 +000014166 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57766) {
14167 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57762)
14168 strcpy(tp->board_part_number, "BCM57762");
14169 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57766)
14170 strcpy(tp->board_part_number, "BCM57766");
14171 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57782)
14172 strcpy(tp->board_part_number, "BCM57782");
14173 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57786)
14174 strcpy(tp->board_part_number, "BCM57786");
14175 else
14176 goto nomatch;
Matt Carlson37a949c2010-09-30 10:34:33 +000014177 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chanb5d37722006-09-27 16:06:21 -070014178 strcpy(tp->board_part_number, "BCM95906");
Matt Carlson37a949c2010-09-30 10:34:33 +000014179 } else {
14180nomatch:
Michael Chanb5d37722006-09-27 16:06:21 -070014181 strcpy(tp->board_part_number, "none");
Matt Carlson37a949c2010-09-30 10:34:33 +000014182 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014183}
14184
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014185static int tg3_fw_img_is_valid(struct tg3 *tp, u32 offset)
Matt Carlson9c8a6202007-10-21 16:16:08 -070014186{
14187 u32 val;
14188
Matt Carlsone4f34112009-02-25 14:25:00 +000014189 if (tg3_nvram_read(tp, offset, &val) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070014190 (val & 0xfc000000) != 0x0c000000 ||
Matt Carlsone4f34112009-02-25 14:25:00 +000014191 tg3_nvram_read(tp, offset + 4, &val) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070014192 val != 0)
14193 return 0;
14194
14195 return 1;
14196}
14197
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014198static void tg3_read_bc_ver(struct tg3 *tp)
Matt Carlsonacd9c112009-02-25 14:26:33 +000014199{
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000014200 u32 val, offset, start, ver_offset;
Matt Carlson75f99362010-04-05 10:19:24 +000014201 int i, dst_off;
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000014202 bool newver = false;
Matt Carlsonacd9c112009-02-25 14:26:33 +000014203
14204 if (tg3_nvram_read(tp, 0xc, &offset) ||
14205 tg3_nvram_read(tp, 0x4, &start))
14206 return;
14207
14208 offset = tg3_nvram_logical_addr(tp, offset);
14209
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000014210 if (tg3_nvram_read(tp, offset, &val))
Matt Carlsonacd9c112009-02-25 14:26:33 +000014211 return;
14212
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000014213 if ((val & 0xfc000000) == 0x0c000000) {
14214 if (tg3_nvram_read(tp, offset + 4, &val))
Matt Carlsonacd9c112009-02-25 14:26:33 +000014215 return;
14216
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000014217 if (val == 0)
14218 newver = true;
14219 }
14220
Matt Carlson75f99362010-04-05 10:19:24 +000014221 dst_off = strlen(tp->fw_ver);
14222
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000014223 if (newver) {
Matt Carlson75f99362010-04-05 10:19:24 +000014224 if (TG3_VER_SIZE - dst_off < 16 ||
14225 tg3_nvram_read(tp, offset + 8, &ver_offset))
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000014226 return;
14227
14228 offset = offset + ver_offset - start;
14229 for (i = 0; i < 16; i += 4) {
14230 __be32 v;
14231 if (tg3_nvram_read_be32(tp, offset + i, &v))
14232 return;
14233
Matt Carlson75f99362010-04-05 10:19:24 +000014234 memcpy(tp->fw_ver + dst_off + i, &v, sizeof(v));
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000014235 }
14236 } else {
14237 u32 major, minor;
14238
14239 if (tg3_nvram_read(tp, TG3_NVM_PTREV_BCVER, &ver_offset))
14240 return;
14241
14242 major = (ver_offset & TG3_NVM_BCVER_MAJMSK) >>
14243 TG3_NVM_BCVER_MAJSFT;
14244 minor = ver_offset & TG3_NVM_BCVER_MINMSK;
Matt Carlson75f99362010-04-05 10:19:24 +000014245 snprintf(&tp->fw_ver[dst_off], TG3_VER_SIZE - dst_off,
14246 "v%d.%02d", major, minor);
Matt Carlsonacd9c112009-02-25 14:26:33 +000014247 }
14248}
14249
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014250static void tg3_read_hwsb_ver(struct tg3 *tp)
Matt Carlsona6f6cb12009-02-25 14:27:43 +000014251{
14252 u32 val, major, minor;
14253
14254 /* Use native endian representation */
14255 if (tg3_nvram_read(tp, TG3_NVM_HWSB_CFG1, &val))
14256 return;
14257
14258 major = (val & TG3_NVM_HWSB_CFG1_MAJMSK) >>
14259 TG3_NVM_HWSB_CFG1_MAJSFT;
14260 minor = (val & TG3_NVM_HWSB_CFG1_MINMSK) >>
14261 TG3_NVM_HWSB_CFG1_MINSFT;
14262
14263 snprintf(&tp->fw_ver[0], 32, "sb v%d.%02d", major, minor);
14264}
14265
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014266static void tg3_read_sb_ver(struct tg3 *tp, u32 val)
Matt Carlsondfe00d72008-11-21 17:19:41 -080014267{
14268 u32 offset, major, minor, build;
14269
Matt Carlson75f99362010-04-05 10:19:24 +000014270 strncat(tp->fw_ver, "sb", TG3_VER_SIZE - strlen(tp->fw_ver) - 1);
Matt Carlsondfe00d72008-11-21 17:19:41 -080014271
14272 if ((val & TG3_EEPROM_SB_FORMAT_MASK) != TG3_EEPROM_SB_FORMAT_1)
14273 return;
14274
14275 switch (val & TG3_EEPROM_SB_REVISION_MASK) {
14276 case TG3_EEPROM_SB_REVISION_0:
14277 offset = TG3_EEPROM_SB_F1R0_EDH_OFF;
14278 break;
14279 case TG3_EEPROM_SB_REVISION_2:
14280 offset = TG3_EEPROM_SB_F1R2_EDH_OFF;
14281 break;
14282 case TG3_EEPROM_SB_REVISION_3:
14283 offset = TG3_EEPROM_SB_F1R3_EDH_OFF;
14284 break;
Matt Carlsona4153d42010-02-17 15:16:56 +000014285 case TG3_EEPROM_SB_REVISION_4:
14286 offset = TG3_EEPROM_SB_F1R4_EDH_OFF;
14287 break;
14288 case TG3_EEPROM_SB_REVISION_5:
14289 offset = TG3_EEPROM_SB_F1R5_EDH_OFF;
14290 break;
Matt Carlsonbba226a2010-10-14 10:37:38 +000014291 case TG3_EEPROM_SB_REVISION_6:
14292 offset = TG3_EEPROM_SB_F1R6_EDH_OFF;
14293 break;
Matt Carlsondfe00d72008-11-21 17:19:41 -080014294 default:
14295 return;
14296 }
14297
Matt Carlsone4f34112009-02-25 14:25:00 +000014298 if (tg3_nvram_read(tp, offset, &val))
Matt Carlsondfe00d72008-11-21 17:19:41 -080014299 return;
14300
14301 build = (val & TG3_EEPROM_SB_EDH_BLD_MASK) >>
14302 TG3_EEPROM_SB_EDH_BLD_SHFT;
14303 major = (val & TG3_EEPROM_SB_EDH_MAJ_MASK) >>
14304 TG3_EEPROM_SB_EDH_MAJ_SHFT;
14305 minor = val & TG3_EEPROM_SB_EDH_MIN_MASK;
14306
14307 if (minor > 99 || build > 26)
14308 return;
14309
Matt Carlson75f99362010-04-05 10:19:24 +000014310 offset = strlen(tp->fw_ver);
14311 snprintf(&tp->fw_ver[offset], TG3_VER_SIZE - offset,
14312 " v%d.%02d", major, minor);
Matt Carlsondfe00d72008-11-21 17:19:41 -080014313
14314 if (build > 0) {
Matt Carlson75f99362010-04-05 10:19:24 +000014315 offset = strlen(tp->fw_ver);
14316 if (offset < TG3_VER_SIZE - 1)
14317 tp->fw_ver[offset] = 'a' + build - 1;
Matt Carlsondfe00d72008-11-21 17:19:41 -080014318 }
14319}
14320
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014321static void tg3_read_mgmtfw_ver(struct tg3 *tp)
Michael Chanc4e65752006-03-20 22:29:32 -080014322{
14323 u32 val, offset, start;
Matt Carlsonacd9c112009-02-25 14:26:33 +000014324 int i, vlen;
Matt Carlson9c8a6202007-10-21 16:16:08 -070014325
14326 for (offset = TG3_NVM_DIR_START;
14327 offset < TG3_NVM_DIR_END;
14328 offset += TG3_NVM_DIRENT_SIZE) {
Matt Carlsone4f34112009-02-25 14:25:00 +000014329 if (tg3_nvram_read(tp, offset, &val))
Matt Carlson9c8a6202007-10-21 16:16:08 -070014330 return;
14331
14332 if ((val >> TG3_NVM_DIRTYPE_SHIFT) == TG3_NVM_DIRTYPE_ASFINI)
14333 break;
14334 }
14335
14336 if (offset == TG3_NVM_DIR_END)
14337 return;
14338
Joe Perches63c3a662011-04-26 08:12:10 +000014339 if (!tg3_flag(tp, 5705_PLUS))
Matt Carlson9c8a6202007-10-21 16:16:08 -070014340 start = 0x08000000;
Matt Carlsone4f34112009-02-25 14:25:00 +000014341 else if (tg3_nvram_read(tp, offset - 4, &start))
Matt Carlson9c8a6202007-10-21 16:16:08 -070014342 return;
14343
Matt Carlsone4f34112009-02-25 14:25:00 +000014344 if (tg3_nvram_read(tp, offset + 4, &offset) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070014345 !tg3_fw_img_is_valid(tp, offset) ||
Matt Carlsone4f34112009-02-25 14:25:00 +000014346 tg3_nvram_read(tp, offset + 8, &val))
Matt Carlson9c8a6202007-10-21 16:16:08 -070014347 return;
14348
14349 offset += val - start;
14350
Matt Carlsonacd9c112009-02-25 14:26:33 +000014351 vlen = strlen(tp->fw_ver);
Matt Carlson9c8a6202007-10-21 16:16:08 -070014352
Matt Carlsonacd9c112009-02-25 14:26:33 +000014353 tp->fw_ver[vlen++] = ',';
14354 tp->fw_ver[vlen++] = ' ';
Matt Carlson9c8a6202007-10-21 16:16:08 -070014355
14356 for (i = 0; i < 4; i++) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000014357 __be32 v;
14358 if (tg3_nvram_read_be32(tp, offset, &v))
Matt Carlson9c8a6202007-10-21 16:16:08 -070014359 return;
14360
Al Virob9fc7dc2007-12-17 22:59:57 -080014361 offset += sizeof(v);
Matt Carlson9c8a6202007-10-21 16:16:08 -070014362
Matt Carlsonacd9c112009-02-25 14:26:33 +000014363 if (vlen > TG3_VER_SIZE - sizeof(v)) {
14364 memcpy(&tp->fw_ver[vlen], &v, TG3_VER_SIZE - vlen);
Matt Carlson9c8a6202007-10-21 16:16:08 -070014365 break;
14366 }
14367
Matt Carlsonacd9c112009-02-25 14:26:33 +000014368 memcpy(&tp->fw_ver[vlen], &v, sizeof(v));
14369 vlen += sizeof(v);
Matt Carlson9c8a6202007-10-21 16:16:08 -070014370 }
Matt Carlsonacd9c112009-02-25 14:26:33 +000014371}
14372
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014373static void tg3_probe_ncsi(struct tg3 *tp)
Matt Carlson7fd76442009-02-25 14:27:20 +000014374{
Matt Carlson7fd76442009-02-25 14:27:20 +000014375 u32 apedata;
Matt Carlson7fd76442009-02-25 14:27:20 +000014376
14377 apedata = tg3_ape_read32(tp, TG3_APE_SEG_SIG);
14378 if (apedata != APE_SEG_SIG_MAGIC)
14379 return;
14380
14381 apedata = tg3_ape_read32(tp, TG3_APE_FW_STATUS);
14382 if (!(apedata & APE_FW_STATUS_READY))
14383 return;
14384
Michael Chan165f4d12012-07-16 16:23:59 +000014385 if (tg3_ape_read32(tp, TG3_APE_FW_FEATURES) & TG3_APE_FW_FEATURE_NCSI)
14386 tg3_flag_set(tp, APE_HAS_NCSI);
14387}
14388
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014389static void tg3_read_dash_ver(struct tg3 *tp)
Michael Chan165f4d12012-07-16 16:23:59 +000014390{
14391 int vlen;
14392 u32 apedata;
14393 char *fwtype;
14394
Matt Carlson7fd76442009-02-25 14:27:20 +000014395 apedata = tg3_ape_read32(tp, TG3_APE_FW_VERSION);
14396
Michael Chan165f4d12012-07-16 16:23:59 +000014397 if (tg3_flag(tp, APE_HAS_NCSI))
Matt Carlsonecc79642010-08-02 11:26:01 +000014398 fwtype = "NCSI";
Michael Chan165f4d12012-07-16 16:23:59 +000014399 else
Matt Carlsonecc79642010-08-02 11:26:01 +000014400 fwtype = "DASH";
14401
Matt Carlson7fd76442009-02-25 14:27:20 +000014402 vlen = strlen(tp->fw_ver);
14403
Matt Carlsonecc79642010-08-02 11:26:01 +000014404 snprintf(&tp->fw_ver[vlen], TG3_VER_SIZE - vlen, " %s v%d.%d.%d.%d",
14405 fwtype,
Matt Carlson7fd76442009-02-25 14:27:20 +000014406 (apedata & APE_FW_VERSION_MAJMSK) >> APE_FW_VERSION_MAJSFT,
14407 (apedata & APE_FW_VERSION_MINMSK) >> APE_FW_VERSION_MINSFT,
14408 (apedata & APE_FW_VERSION_REVMSK) >> APE_FW_VERSION_REVSFT,
14409 (apedata & APE_FW_VERSION_BLDMSK));
14410}
14411
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014412static void tg3_read_fw_ver(struct tg3 *tp)
Matt Carlsonacd9c112009-02-25 14:26:33 +000014413{
14414 u32 val;
Matt Carlson75f99362010-04-05 10:19:24 +000014415 bool vpd_vers = false;
14416
14417 if (tp->fw_ver[0] != 0)
14418 vpd_vers = true;
Matt Carlsonacd9c112009-02-25 14:26:33 +000014419
Joe Perches63c3a662011-04-26 08:12:10 +000014420 if (tg3_flag(tp, NO_NVRAM)) {
Matt Carlson75f99362010-04-05 10:19:24 +000014421 strcat(tp->fw_ver, "sb");
Matt Carlsondf259d82009-04-20 06:57:14 +000014422 return;
14423 }
14424
Matt Carlsonacd9c112009-02-25 14:26:33 +000014425 if (tg3_nvram_read(tp, 0, &val))
14426 return;
14427
14428 if (val == TG3_EEPROM_MAGIC)
14429 tg3_read_bc_ver(tp);
14430 else if ((val & TG3_EEPROM_MAGIC_FW_MSK) == TG3_EEPROM_MAGIC_FW)
14431 tg3_read_sb_ver(tp, val);
Matt Carlsona6f6cb12009-02-25 14:27:43 +000014432 else if ((val & TG3_EEPROM_MAGIC_HW_MSK) == TG3_EEPROM_MAGIC_HW)
14433 tg3_read_hwsb_ver(tp);
Matt Carlsonacd9c112009-02-25 14:26:33 +000014434
Michael Chan165f4d12012-07-16 16:23:59 +000014435 if (tg3_flag(tp, ENABLE_ASF)) {
14436 if (tg3_flag(tp, ENABLE_APE)) {
14437 tg3_probe_ncsi(tp);
14438 if (!vpd_vers)
14439 tg3_read_dash_ver(tp);
14440 } else if (!vpd_vers) {
14441 tg3_read_mgmtfw_ver(tp);
14442 }
Matt Carlsonc9cab242011-07-13 09:27:27 +000014443 }
Matt Carlson9c8a6202007-10-21 16:16:08 -070014444
14445 tp->fw_ver[TG3_VER_SIZE - 1] = 0;
Michael Chanc4e65752006-03-20 22:29:32 -080014446}
14447
Matt Carlson7cb32cf2010-09-30 10:34:36 +000014448static inline u32 tg3_rx_ret_ring_size(struct tg3 *tp)
14449{
Joe Perches63c3a662011-04-26 08:12:10 +000014450 if (tg3_flag(tp, LRG_PROD_RING_CAP))
Matt Carlsonde9f5232011-04-05 14:22:43 +000014451 return TG3_RX_RET_MAX_SIZE_5717;
Joe Perches63c3a662011-04-26 08:12:10 +000014452 else if (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS))
Matt Carlsonde9f5232011-04-05 14:22:43 +000014453 return TG3_RX_RET_MAX_SIZE_5700;
Matt Carlson7cb32cf2010-09-30 10:34:36 +000014454 else
Matt Carlsonde9f5232011-04-05 14:22:43 +000014455 return TG3_RX_RET_MAX_SIZE_5705;
Matt Carlson7cb32cf2010-09-30 10:34:36 +000014456}
14457
Matt Carlson41434702011-03-09 16:58:22 +000014458static DEFINE_PCI_DEVICE_TABLE(tg3_write_reorder_chipsets) = {
Joe Perches895950c2010-12-21 02:16:08 -080014459 { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_FE_GATE_700C) },
14460 { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_8131_BRIDGE) },
14461 { PCI_DEVICE(PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_8385_0) },
14462 { },
14463};
14464
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014465static struct pci_dev *tg3_find_peer(struct tg3 *tp)
Matt Carlson16c7fa72012-02-13 10:20:10 +000014466{
14467 struct pci_dev *peer;
14468 unsigned int func, devnr = tp->pdev->devfn & ~7;
14469
14470 for (func = 0; func < 8; func++) {
14471 peer = pci_get_slot(tp->pdev->bus, devnr | func);
14472 if (peer && peer != tp->pdev)
14473 break;
14474 pci_dev_put(peer);
14475 }
14476 /* 5704 can be configured in single-port mode, set peer to
14477 * tp->pdev in that case.
14478 */
14479 if (!peer) {
14480 peer = tp->pdev;
14481 return peer;
14482 }
14483
14484 /*
14485 * We don't need to keep the refcount elevated; there's no way
14486 * to remove one half of this device without removing the other
14487 */
14488 pci_dev_put(peer);
14489
14490 return peer;
14491}
14492
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014493static void tg3_detect_asic_rev(struct tg3 *tp, u32 misc_ctrl_reg)
Matt Carlson42b123b2012-02-13 15:20:13 +000014494{
14495 tp->pci_chip_rev_id = misc_ctrl_reg >> MISC_HOST_CTRL_CHIPREV_SHIFT;
14496 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_USE_PROD_ID_REG) {
14497 u32 reg;
14498
14499 /* All devices that use the alternate
14500 * ASIC REV location have a CPMU.
14501 */
14502 tg3_flag_set(tp, CPMU_PRESENT);
14503
14504 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717 ||
Michael Chan79d49692012-11-05 14:26:29 +000014505 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717_C ||
Matt Carlson42b123b2012-02-13 15:20:13 +000014506 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718 ||
14507 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5719 ||
14508 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5720)
14509 reg = TG3PCI_GEN2_PRODID_ASICREV;
14510 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57781 ||
14511 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57785 ||
14512 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57761 ||
14513 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57765 ||
14514 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57791 ||
14515 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57795 ||
14516 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57762 ||
14517 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57766 ||
14518 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57782 ||
14519 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57786)
14520 reg = TG3PCI_GEN15_PRODID_ASICREV;
14521 else
14522 reg = TG3PCI_PRODID_ASICREV;
14523
14524 pci_read_config_dword(tp->pdev, reg, &tp->pci_chip_rev_id);
14525 }
14526
14527 /* Wrong chip ID in 5752 A0. This code can be removed later
14528 * as A0 is not in production.
14529 */
14530 if (tp->pci_chip_rev_id == CHIPREV_ID_5752_A0_HW)
14531 tp->pci_chip_rev_id = CHIPREV_ID_5752_A0;
14532
Michael Chan79d49692012-11-05 14:26:29 +000014533 if (tp->pci_chip_rev_id == CHIPREV_ID_5717_C0)
14534 tp->pci_chip_rev_id = CHIPREV_ID_5720_A0;
14535
Matt Carlson42b123b2012-02-13 15:20:13 +000014536 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
14537 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
14538 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
14539 tg3_flag_set(tp, 5717_PLUS);
14540
14541 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765 ||
14542 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57766)
14543 tg3_flag_set(tp, 57765_CLASS);
14544
14545 if (tg3_flag(tp, 57765_CLASS) || tg3_flag(tp, 5717_PLUS))
14546 tg3_flag_set(tp, 57765_PLUS);
14547
14548 /* Intentionally exclude ASIC_REV_5906 */
14549 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
14550 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787 ||
14551 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
14552 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
14553 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
14554 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
14555 tg3_flag(tp, 57765_PLUS))
14556 tg3_flag_set(tp, 5755_PLUS);
14557
14558 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780 ||
14559 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714)
14560 tg3_flag_set(tp, 5780_CLASS);
14561
14562 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 ||
14563 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752 ||
14564 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906 ||
14565 tg3_flag(tp, 5755_PLUS) ||
14566 tg3_flag(tp, 5780_CLASS))
14567 tg3_flag_set(tp, 5750_PLUS);
14568
14569 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 ||
14570 tg3_flag(tp, 5750_PLUS))
14571 tg3_flag_set(tp, 5705_PLUS);
14572}
14573
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +000014574static bool tg3_10_100_only_device(struct tg3 *tp,
14575 const struct pci_device_id *ent)
14576{
14577 u32 grc_misc_cfg = tr32(GRC_MISC_CFG) & GRC_MISC_CFG_BOARD_ID_MASK;
14578
14579 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 &&
14580 (grc_misc_cfg == 0x8000 || grc_misc_cfg == 0x4000)) ||
14581 (tp->phy_flags & TG3_PHYFLG_IS_FET))
14582 return true;
14583
14584 if (ent->driver_data & TG3_DRV_DATA_FLAG_10_100_ONLY) {
14585 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
14586 if (ent->driver_data & TG3_DRV_DATA_FLAG_5705_10_100)
14587 return true;
14588 } else {
14589 return true;
14590 }
14591 }
14592
14593 return false;
14594}
14595
Bill Pemberton229b1ad2012-12-03 09:22:59 -050014596static int tg3_get_invariants(struct tg3 *tp,
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +000014597 const struct pci_device_id *ent)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014598{
Linus Torvalds1da177e2005-04-16 15:20:36 -070014599 u32 misc_ctrl_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014600 u32 pci_state_reg, grc_misc_cfg;
14601 u32 val;
14602 u16 pci_cmd;
Matt Carlson5e7dfd02008-11-21 17:18:16 -080014603 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014604
Linus Torvalds1da177e2005-04-16 15:20:36 -070014605 /* Force memory write invalidate off. If we leave it on,
14606 * then on 5700_BX chips we have to enable a workaround.
14607 * The workaround is to set the TG3PCI_DMA_RW_CTRL boundary
14608 * to match the cacheline size. The Broadcom driver have this
14609 * workaround but turns MWI off all the times so never uses
14610 * it. This seems to suggest that the workaround is insufficient.
14611 */
14612 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
14613 pci_cmd &= ~PCI_COMMAND_INVALIDATE;
14614 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
14615
Matt Carlson16821282011-07-13 09:27:28 +000014616 /* Important! -- Make sure register accesses are byteswapped
14617 * correctly. Also, for those chips that require it, make
14618 * sure that indirect register accesses are enabled before
14619 * the first operation.
Linus Torvalds1da177e2005-04-16 15:20:36 -070014620 */
14621 pci_read_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
14622 &misc_ctrl_reg);
Matt Carlson16821282011-07-13 09:27:28 +000014623 tp->misc_host_ctrl |= (misc_ctrl_reg &
14624 MISC_HOST_CTRL_CHIPREV);
14625 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
14626 tp->misc_host_ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014627
Matt Carlson42b123b2012-02-13 15:20:13 +000014628 tg3_detect_asic_rev(tp, misc_ctrl_reg);
Michael Chanff645be2005-04-21 17:09:53 -070014629
Michael Chan68929142005-08-09 20:17:14 -070014630 /* If we have 5702/03 A1 or A2 on certain ICH chipsets,
14631 * we need to disable memory and use config. cycles
14632 * only to access all registers. The 5702/03 chips
14633 * can mistakenly decode the special cycles from the
14634 * ICH chipsets as memory write cycles, causing corruption
14635 * of register and memory space. Only certain ICH bridges
14636 * will drive special cycles with non-zero data during the
14637 * address phase which can fall within the 5703's address
14638 * range. This is not an ICH bug as the PCI spec allows
14639 * non-zero address during special cycles. However, only
14640 * these ICH bridges are known to drive non-zero addresses
14641 * during special cycles.
14642 *
14643 * Since special cycles do not cross PCI bridges, we only
14644 * enable this workaround if the 5703 is on the secondary
14645 * bus of these ICH bridges.
14646 */
14647 if ((tp->pci_chip_rev_id == CHIPREV_ID_5703_A1) ||
14648 (tp->pci_chip_rev_id == CHIPREV_ID_5703_A2)) {
14649 static struct tg3_dev_id {
14650 u32 vendor;
14651 u32 device;
14652 u32 rev;
14653 } ich_chipsets[] = {
14654 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AA_8,
14655 PCI_ANY_ID },
14656 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AB_8,
14657 PCI_ANY_ID },
14658 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_11,
14659 0xa },
14660 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_6,
14661 PCI_ANY_ID },
14662 { },
14663 };
14664 struct tg3_dev_id *pci_id = &ich_chipsets[0];
14665 struct pci_dev *bridge = NULL;
14666
14667 while (pci_id->vendor != 0) {
14668 bridge = pci_get_device(pci_id->vendor, pci_id->device,
14669 bridge);
14670 if (!bridge) {
14671 pci_id++;
14672 continue;
14673 }
14674 if (pci_id->rev != PCI_ANY_ID) {
Auke Kok44c10132007-06-08 15:46:36 -070014675 if (bridge->revision > pci_id->rev)
Michael Chan68929142005-08-09 20:17:14 -070014676 continue;
14677 }
14678 if (bridge->subordinate &&
14679 (bridge->subordinate->number ==
14680 tp->pdev->bus->number)) {
Joe Perches63c3a662011-04-26 08:12:10 +000014681 tg3_flag_set(tp, ICH_WORKAROUND);
Michael Chan68929142005-08-09 20:17:14 -070014682 pci_dev_put(bridge);
14683 break;
14684 }
14685 }
14686 }
14687
Matt Carlson6ff6f812011-05-19 12:12:54 +000014688 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
Matt Carlson41588ba2008-04-19 18:12:33 -070014689 static struct tg3_dev_id {
14690 u32 vendor;
14691 u32 device;
14692 } bridge_chipsets[] = {
14693 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PXH_0 },
14694 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PXH_1 },
14695 { },
14696 };
14697 struct tg3_dev_id *pci_id = &bridge_chipsets[0];
14698 struct pci_dev *bridge = NULL;
14699
14700 while (pci_id->vendor != 0) {
14701 bridge = pci_get_device(pci_id->vendor,
14702 pci_id->device,
14703 bridge);
14704 if (!bridge) {
14705 pci_id++;
14706 continue;
14707 }
14708 if (bridge->subordinate &&
14709 (bridge->subordinate->number <=
14710 tp->pdev->bus->number) &&
Yinghai Lub918c622012-05-17 18:51:11 -070014711 (bridge->subordinate->busn_res.end >=
Matt Carlson41588ba2008-04-19 18:12:33 -070014712 tp->pdev->bus->number)) {
Joe Perches63c3a662011-04-26 08:12:10 +000014713 tg3_flag_set(tp, 5701_DMA_BUG);
Matt Carlson41588ba2008-04-19 18:12:33 -070014714 pci_dev_put(bridge);
14715 break;
14716 }
14717 }
14718 }
14719
Michael Chan4a29cc22006-03-19 13:21:12 -080014720 /* The EPB bridge inside 5714, 5715, and 5780 cannot support
14721 * DMA addresses > 40-bit. This bridge may have other additional
14722 * 57xx devices behind it in some 4-port NIC designs for example.
14723 * Any tg3 device found behind the bridge will also need the 40-bit
14724 * DMA workaround.
14725 */
Matt Carlson42b123b2012-02-13 15:20:13 +000014726 if (tg3_flag(tp, 5780_CLASS)) {
Joe Perches63c3a662011-04-26 08:12:10 +000014727 tg3_flag_set(tp, 40BIT_DMA_BUG);
Michael Chan4cf78e42005-07-25 12:29:19 -070014728 tp->msi_cap = pci_find_capability(tp->pdev, PCI_CAP_ID_MSI);
Matt Carlson859a588792010-04-05 10:19:28 +000014729 } else {
Michael Chan4a29cc22006-03-19 13:21:12 -080014730 struct pci_dev *bridge = NULL;
14731
14732 do {
14733 bridge = pci_get_device(PCI_VENDOR_ID_SERVERWORKS,
14734 PCI_DEVICE_ID_SERVERWORKS_EPB,
14735 bridge);
14736 if (bridge && bridge->subordinate &&
14737 (bridge->subordinate->number <=
14738 tp->pdev->bus->number) &&
Yinghai Lub918c622012-05-17 18:51:11 -070014739 (bridge->subordinate->busn_res.end >=
Michael Chan4a29cc22006-03-19 13:21:12 -080014740 tp->pdev->bus->number)) {
Joe Perches63c3a662011-04-26 08:12:10 +000014741 tg3_flag_set(tp, 40BIT_DMA_BUG);
Michael Chan4a29cc22006-03-19 13:21:12 -080014742 pci_dev_put(bridge);
14743 break;
14744 }
14745 } while (bridge);
14746 }
Michael Chan4cf78e42005-07-25 12:29:19 -070014747
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000014748 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
Matt Carlson3a1e19d2011-07-13 09:27:32 +000014749 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714)
Michael Chan7544b092007-05-05 13:08:32 -070014750 tp->pdev_peer = tg3_find_peer(tp);
14751
Matt Carlson507399f2009-11-13 13:03:37 +000014752 /* Determine TSO capabilities */
Matt Carlsona0512942011-07-27 14:20:54 +000014753 if (tp->pci_chip_rev_id == CHIPREV_ID_5719_A0)
Matt Carlson4d163b72011-01-25 15:58:48 +000014754 ; /* Do nothing. HW bug. */
Joe Perches63c3a662011-04-26 08:12:10 +000014755 else if (tg3_flag(tp, 57765_PLUS))
14756 tg3_flag_set(tp, HW_TSO_3);
14757 else if (tg3_flag(tp, 5755_PLUS) ||
Matt Carlsone849cdc2009-11-13 13:03:38 +000014758 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
Joe Perches63c3a662011-04-26 08:12:10 +000014759 tg3_flag_set(tp, HW_TSO_2);
14760 else if (tg3_flag(tp, 5750_PLUS)) {
14761 tg3_flag_set(tp, HW_TSO_1);
14762 tg3_flag_set(tp, TSO_BUG);
Matt Carlson507399f2009-11-13 13:03:37 +000014763 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 &&
14764 tp->pci_chip_rev_id >= CHIPREV_ID_5750_C2)
Joe Perches63c3a662011-04-26 08:12:10 +000014765 tg3_flag_clear(tp, TSO_BUG);
Matt Carlson507399f2009-11-13 13:03:37 +000014766 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
14767 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701 &&
14768 tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) {
Joe Perches63c3a662011-04-26 08:12:10 +000014769 tg3_flag_set(tp, TSO_BUG);
Matt Carlson507399f2009-11-13 13:03:37 +000014770 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705)
14771 tp->fw_needed = FIRMWARE_TG3TSO5;
14772 else
14773 tp->fw_needed = FIRMWARE_TG3TSO;
14774 }
14775
Matt Carlsondabc5c62011-05-19 12:12:52 +000014776 /* Selectively allow TSO based on operating conditions */
Matt Carlson6ff6f812011-05-19 12:12:54 +000014777 if (tg3_flag(tp, HW_TSO_1) ||
14778 tg3_flag(tp, HW_TSO_2) ||
14779 tg3_flag(tp, HW_TSO_3) ||
Matt Carlsoncf9ecf42011-11-28 09:41:03 +000014780 tp->fw_needed) {
14781 /* For firmware TSO, assume ASF is disabled.
14782 * We'll disable TSO later if we discover ASF
14783 * is enabled in tg3_get_eeprom_hw_cfg().
14784 */
Matt Carlsondabc5c62011-05-19 12:12:52 +000014785 tg3_flag_set(tp, TSO_CAPABLE);
Matt Carlsoncf9ecf42011-11-28 09:41:03 +000014786 } else {
Matt Carlsondabc5c62011-05-19 12:12:52 +000014787 tg3_flag_clear(tp, TSO_CAPABLE);
14788 tg3_flag_clear(tp, TSO_BUG);
14789 tp->fw_needed = NULL;
14790 }
14791
14792 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0)
14793 tp->fw_needed = FIRMWARE_TG3;
14794
Matt Carlson507399f2009-11-13 13:03:37 +000014795 tp->irq_max = 1;
14796
Joe Perches63c3a662011-04-26 08:12:10 +000014797 if (tg3_flag(tp, 5750_PLUS)) {
14798 tg3_flag_set(tp, SUPPORT_MSI);
Michael Chan7544b092007-05-05 13:08:32 -070014799 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_AX ||
14800 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_BX ||
14801 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714 &&
14802 tp->pci_chip_rev_id <= CHIPREV_ID_5714_A2 &&
14803 tp->pdev_peer == tp->pdev))
Joe Perches63c3a662011-04-26 08:12:10 +000014804 tg3_flag_clear(tp, SUPPORT_MSI);
Michael Chan7544b092007-05-05 13:08:32 -070014805
Joe Perches63c3a662011-04-26 08:12:10 +000014806 if (tg3_flag(tp, 5755_PLUS) ||
Michael Chanb5d37722006-09-27 16:06:21 -070014807 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Joe Perches63c3a662011-04-26 08:12:10 +000014808 tg3_flag_set(tp, 1SHOT_MSI);
Michael Chan52c0fd82006-06-29 20:15:54 -070014809 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014810
Joe Perches63c3a662011-04-26 08:12:10 +000014811 if (tg3_flag(tp, 57765_PLUS)) {
14812 tg3_flag_set(tp, SUPPORT_MSIX);
Matt Carlson507399f2009-11-13 13:03:37 +000014813 tp->irq_max = TG3_IRQ_MAX_VECS;
14814 }
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000014815 }
Matt Carlson0e1406d2009-11-02 12:33:33 +000014816
Michael Chan91024262012-09-28 07:12:38 +000014817 tp->txq_max = 1;
14818 tp->rxq_max = 1;
14819 if (tp->irq_max > 1) {
14820 tp->rxq_max = TG3_RSS_MAX_NUM_QS;
14821 tg3_rss_init_dflt_indir_tbl(tp, TG3_RSS_MAX_NUM_QS);
14822
14823 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
14824 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
14825 tp->txq_max = tp->irq_max - 1;
14826 }
14827
Matt Carlsonb7abee62012-06-07 12:56:54 +000014828 if (tg3_flag(tp, 5755_PLUS) ||
14829 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
Joe Perches63c3a662011-04-26 08:12:10 +000014830 tg3_flag_set(tp, SHORT_DMA_BUG);
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000014831
Matt Carlsone31aa982011-07-27 14:20:53 +000014832 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
Matt Carlsona4cb4282011-12-14 11:09:58 +000014833 tp->dma_limit = TG3_TX_BD_DMA_MAX_4K;
Matt Carlsone31aa982011-07-27 14:20:53 +000014834
Matt Carlsonfa6b2aa2011-11-21 15:01:19 +000014835 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
14836 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
14837 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
Joe Perches63c3a662011-04-26 08:12:10 +000014838 tg3_flag_set(tp, LRG_PROD_RING_CAP);
Matt Carlsonde9f5232011-04-05 14:22:43 +000014839
Joe Perches63c3a662011-04-26 08:12:10 +000014840 if (tg3_flag(tp, 57765_PLUS) &&
Matt Carlsona0512942011-07-27 14:20:54 +000014841 tp->pci_chip_rev_id != CHIPREV_ID_5719_A0)
Joe Perches63c3a662011-04-26 08:12:10 +000014842 tg3_flag_set(tp, USE_JUMBO_BDFLAG);
Matt Carlsonb703df62009-12-03 08:36:21 +000014843
Joe Perches63c3a662011-04-26 08:12:10 +000014844 if (!tg3_flag(tp, 5705_PLUS) ||
14845 tg3_flag(tp, 5780_CLASS) ||
14846 tg3_flag(tp, USE_JUMBO_BDFLAG))
14847 tg3_flag_set(tp, JUMBO_CAPABLE);
Michael Chan0f893dc2005-07-25 12:30:38 -070014848
Matt Carlson52f44902008-11-21 17:17:04 -080014849 pci_read_config_dword(tp->pdev, TG3PCI_PCISTATE,
14850 &pci_state_reg);
14851
Jon Mason708ebb32011-06-27 12:56:50 +000014852 if (pci_is_pcie(tp->pdev)) {
Matt Carlson5e7dfd02008-11-21 17:18:16 -080014853 u16 lnkctl;
14854
Joe Perches63c3a662011-04-26 08:12:10 +000014855 tg3_flag_set(tp, PCI_EXPRESS);
Matt Carlson5f5c51e2007-11-12 21:19:37 -080014856
Jiang Liu0f49bfb2012-08-20 13:28:20 -060014857 pcie_capability_read_word(tp->pdev, PCI_EXP_LNKCTL, &lnkctl);
Matt Carlson5e7dfd02008-11-21 17:18:16 -080014858 if (lnkctl & PCI_EXP_LNKCTL_CLKREQ_EN) {
Matt Carlson7196cd62011-05-19 16:02:44 +000014859 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
14860 ASIC_REV_5906) {
Joe Perches63c3a662011-04-26 08:12:10 +000014861 tg3_flag_clear(tp, HW_TSO_2);
Matt Carlsondabc5c62011-05-19 12:12:52 +000014862 tg3_flag_clear(tp, TSO_CAPABLE);
Matt Carlson7196cd62011-05-19 16:02:44 +000014863 }
Matt Carlson5e7dfd02008-11-21 17:18:16 -080014864 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson321d32a2008-11-21 17:22:19 -080014865 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson9cf74eb2009-04-20 06:58:27 +000014866 tp->pci_chip_rev_id == CHIPREV_ID_57780_A0 ||
14867 tp->pci_chip_rev_id == CHIPREV_ID_57780_A1)
Joe Perches63c3a662011-04-26 08:12:10 +000014868 tg3_flag_set(tp, CLKREQ_BUG);
Matt Carlson614b0592010-01-20 16:58:02 +000014869 } else if (tp->pci_chip_rev_id == CHIPREV_ID_5717_A0) {
Joe Perches63c3a662011-04-26 08:12:10 +000014870 tg3_flag_set(tp, L1PLLPD_EN);
Michael Chanc7835a72006-11-15 21:14:42 -080014871 }
Matt Carlson52f44902008-11-21 17:17:04 -080014872 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785) {
Jon Mason708ebb32011-06-27 12:56:50 +000014873 /* BCM5785 devices are effectively PCIe devices, and should
14874 * follow PCIe codepaths, but do not have a PCIe capabilities
14875 * section.
Matt Carlson93a700a2011-08-31 11:44:54 +000014876 */
Joe Perches63c3a662011-04-26 08:12:10 +000014877 tg3_flag_set(tp, PCI_EXPRESS);
14878 } else if (!tg3_flag(tp, 5705_PLUS) ||
14879 tg3_flag(tp, 5780_CLASS)) {
Matt Carlson52f44902008-11-21 17:17:04 -080014880 tp->pcix_cap = pci_find_capability(tp->pdev, PCI_CAP_ID_PCIX);
14881 if (!tp->pcix_cap) {
Matt Carlson2445e462010-04-05 10:19:21 +000014882 dev_err(&tp->pdev->dev,
14883 "Cannot find PCI-X capability, aborting\n");
Matt Carlson52f44902008-11-21 17:17:04 -080014884 return -EIO;
14885 }
14886
14887 if (!(pci_state_reg & PCISTATE_CONV_PCI_MODE))
Joe Perches63c3a662011-04-26 08:12:10 +000014888 tg3_flag_set(tp, PCIX_MODE);
Matt Carlson52f44902008-11-21 17:17:04 -080014889 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014890
Michael Chan399de502005-10-03 14:02:39 -070014891 /* If we have an AMD 762 or VIA K8T800 chipset, write
14892 * reordering to the mailbox registers done by the host
14893 * controller can cause major troubles. We read back from
14894 * every mailbox register write to force the writes to be
14895 * posted to the chip in order.
14896 */
Matt Carlson41434702011-03-09 16:58:22 +000014897 if (pci_dev_present(tg3_write_reorder_chipsets) &&
Joe Perches63c3a662011-04-26 08:12:10 +000014898 !tg3_flag(tp, PCI_EXPRESS))
14899 tg3_flag_set(tp, MBOX_WRITE_REORDER);
Michael Chan399de502005-10-03 14:02:39 -070014900
Matt Carlson69fc4052008-12-21 20:19:57 -080014901 pci_read_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE,
14902 &tp->pci_cacheline_sz);
14903 pci_read_config_byte(tp->pdev, PCI_LATENCY_TIMER,
14904 &tp->pci_lat_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014905 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 &&
14906 tp->pci_lat_timer < 64) {
14907 tp->pci_lat_timer = 64;
Matt Carlson69fc4052008-12-21 20:19:57 -080014908 pci_write_config_byte(tp->pdev, PCI_LATENCY_TIMER,
14909 tp->pci_lat_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014910 }
14911
Matt Carlson16821282011-07-13 09:27:28 +000014912 /* Important! -- It is critical that the PCI-X hw workaround
14913 * situation is decided before the first MMIO register access.
14914 */
Matt Carlson52f44902008-11-21 17:17:04 -080014915 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5700_BX) {
14916 /* 5700 BX chips need to have their TX producer index
14917 * mailboxes written twice to workaround a bug.
14918 */
Joe Perches63c3a662011-04-26 08:12:10 +000014919 tg3_flag_set(tp, TXD_MBOX_HWBUG);
Matt Carlson9974a352007-10-07 23:27:28 -070014920
Matt Carlson52f44902008-11-21 17:17:04 -080014921 /* If we are in PCI-X mode, enable register write workaround.
Linus Torvalds1da177e2005-04-16 15:20:36 -070014922 *
14923 * The workaround is to use indirect register accesses
14924 * for all chip writes not to mailbox registers.
14925 */
Joe Perches63c3a662011-04-26 08:12:10 +000014926 if (tg3_flag(tp, PCIX_MODE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070014927 u32 pm_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014928
Joe Perches63c3a662011-04-26 08:12:10 +000014929 tg3_flag_set(tp, PCIX_TARGET_HWBUG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014930
14931 /* The chip can have it's power management PCI config
14932 * space registers clobbered due to this bug.
14933 * So explicitly force the chip into D0 here.
14934 */
Matt Carlson9974a352007-10-07 23:27:28 -070014935 pci_read_config_dword(tp->pdev,
14936 tp->pm_cap + PCI_PM_CTRL,
Linus Torvalds1da177e2005-04-16 15:20:36 -070014937 &pm_reg);
14938 pm_reg &= ~PCI_PM_CTRL_STATE_MASK;
14939 pm_reg |= PCI_PM_CTRL_PME_ENABLE | 0 /* D0 */;
Matt Carlson9974a352007-10-07 23:27:28 -070014940 pci_write_config_dword(tp->pdev,
14941 tp->pm_cap + PCI_PM_CTRL,
Linus Torvalds1da177e2005-04-16 15:20:36 -070014942 pm_reg);
14943
14944 /* Also, force SERR#/PERR# in PCI command. */
14945 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
14946 pci_cmd |= PCI_COMMAND_PARITY | PCI_COMMAND_SERR;
14947 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
14948 }
14949 }
14950
Linus Torvalds1da177e2005-04-16 15:20:36 -070014951 if ((pci_state_reg & PCISTATE_BUS_SPEED_HIGH) != 0)
Joe Perches63c3a662011-04-26 08:12:10 +000014952 tg3_flag_set(tp, PCI_HIGH_SPEED);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014953 if ((pci_state_reg & PCISTATE_BUS_32BIT) != 0)
Joe Perches63c3a662011-04-26 08:12:10 +000014954 tg3_flag_set(tp, PCI_32BIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014955
14956 /* Chip-specific fixup from Broadcom driver */
14957 if ((tp->pci_chip_rev_id == CHIPREV_ID_5704_A0) &&
14958 (!(pci_state_reg & PCISTATE_RETRY_SAME_DMA))) {
14959 pci_state_reg |= PCISTATE_RETRY_SAME_DMA;
14960 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE, pci_state_reg);
14961 }
14962
Michael Chan1ee582d2005-08-09 20:16:46 -070014963 /* Default fast path register access methods */
Michael Chan20094932005-08-09 20:16:32 -070014964 tp->read32 = tg3_read32;
Michael Chan1ee582d2005-08-09 20:16:46 -070014965 tp->write32 = tg3_write32;
Michael Chan09ee9292005-08-09 20:17:00 -070014966 tp->read32_mbox = tg3_read32;
Michael Chan20094932005-08-09 20:16:32 -070014967 tp->write32_mbox = tg3_write32;
Michael Chan1ee582d2005-08-09 20:16:46 -070014968 tp->write32_tx_mbox = tg3_write32;
14969 tp->write32_rx_mbox = tg3_write32;
14970
14971 /* Various workaround register access methods */
Joe Perches63c3a662011-04-26 08:12:10 +000014972 if (tg3_flag(tp, PCIX_TARGET_HWBUG))
Michael Chan1ee582d2005-08-09 20:16:46 -070014973 tp->write32 = tg3_write_indirect_reg32;
Matt Carlson98efd8a2007-05-05 12:47:25 -070014974 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701 ||
Joe Perches63c3a662011-04-26 08:12:10 +000014975 (tg3_flag(tp, PCI_EXPRESS) &&
Matt Carlson98efd8a2007-05-05 12:47:25 -070014976 tp->pci_chip_rev_id == CHIPREV_ID_5750_A0)) {
14977 /*
14978 * Back to back register writes can cause problems on these
14979 * chips, the workaround is to read back all reg writes
14980 * except those to mailbox regs.
14981 *
14982 * See tg3_write_indirect_reg32().
14983 */
Michael Chan1ee582d2005-08-09 20:16:46 -070014984 tp->write32 = tg3_write_flush_reg32;
Matt Carlson98efd8a2007-05-05 12:47:25 -070014985 }
14986
Joe Perches63c3a662011-04-26 08:12:10 +000014987 if (tg3_flag(tp, TXD_MBOX_HWBUG) || tg3_flag(tp, MBOX_WRITE_REORDER)) {
Michael Chan1ee582d2005-08-09 20:16:46 -070014988 tp->write32_tx_mbox = tg3_write32_tx_mbox;
Joe Perches63c3a662011-04-26 08:12:10 +000014989 if (tg3_flag(tp, MBOX_WRITE_REORDER))
Michael Chan1ee582d2005-08-09 20:16:46 -070014990 tp->write32_rx_mbox = tg3_write_flush_reg32;
14991 }
Michael Chan20094932005-08-09 20:16:32 -070014992
Joe Perches63c3a662011-04-26 08:12:10 +000014993 if (tg3_flag(tp, ICH_WORKAROUND)) {
Michael Chan68929142005-08-09 20:17:14 -070014994 tp->read32 = tg3_read_indirect_reg32;
14995 tp->write32 = tg3_write_indirect_reg32;
14996 tp->read32_mbox = tg3_read_indirect_mbox;
14997 tp->write32_mbox = tg3_write_indirect_mbox;
14998 tp->write32_tx_mbox = tg3_write_indirect_mbox;
14999 tp->write32_rx_mbox = tg3_write_indirect_mbox;
15000
15001 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070015002 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070015003
15004 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
15005 pci_cmd &= ~PCI_COMMAND_MEMORY;
15006 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
15007 }
Michael Chanb5d37722006-09-27 16:06:21 -070015008 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
15009 tp->read32_mbox = tg3_read32_mbox_5906;
15010 tp->write32_mbox = tg3_write32_mbox_5906;
15011 tp->write32_tx_mbox = tg3_write32_mbox_5906;
15012 tp->write32_rx_mbox = tg3_write32_mbox_5906;
15013 }
Michael Chan68929142005-08-09 20:17:14 -070015014
Michael Chanbbadf502006-04-06 21:46:34 -070015015 if (tp->write32 == tg3_write_indirect_reg32 ||
Joe Perches63c3a662011-04-26 08:12:10 +000015016 (tg3_flag(tp, PCIX_MODE) &&
Michael Chanbbadf502006-04-06 21:46:34 -070015017 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
David S. Millerf49639e2006-06-09 11:58:36 -070015018 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)))
Joe Perches63c3a662011-04-26 08:12:10 +000015019 tg3_flag_set(tp, SRAM_USE_CONFIG);
Michael Chanbbadf502006-04-06 21:46:34 -070015020
Matt Carlson16821282011-07-13 09:27:28 +000015021 /* The memory arbiter has to be enabled in order for SRAM accesses
15022 * to succeed. Normally on powerup the tg3 chip firmware will make
15023 * sure it is enabled, but other entities such as system netboot
15024 * code might disable it.
15025 */
15026 val = tr32(MEMARB_MODE);
15027 tw32(MEMARB_MODE, val | MEMARB_MODE_ENABLE);
15028
Matt Carlson9dc5e342011-11-04 09:15:02 +000015029 tp->pci_fn = PCI_FUNC(tp->pdev->devfn) & 3;
15030 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
15031 tg3_flag(tp, 5780_CLASS)) {
15032 if (tg3_flag(tp, PCIX_MODE)) {
15033 pci_read_config_dword(tp->pdev,
15034 tp->pcix_cap + PCI_X_STATUS,
15035 &val);
15036 tp->pci_fn = val & 0x7;
15037 }
15038 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717) {
15039 tg3_read_mem(tp, NIC_SRAM_CPMU_STATUS, &val);
15040 if ((val & NIC_SRAM_CPMUSTAT_SIG_MSK) ==
15041 NIC_SRAM_CPMUSTAT_SIG) {
15042 tp->pci_fn = val & TG3_CPMU_STATUS_FMSK_5717;
15043 tp->pci_fn = tp->pci_fn ? 1 : 0;
15044 }
15045 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
15046 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
15047 tg3_read_mem(tp, NIC_SRAM_CPMU_STATUS, &val);
15048 if ((val & NIC_SRAM_CPMUSTAT_SIG_MSK) ==
15049 NIC_SRAM_CPMUSTAT_SIG) {
15050 tp->pci_fn = (val & TG3_CPMU_STATUS_FMSK_5719) >>
15051 TG3_CPMU_STATUS_FSHFT_5719;
15052 }
Matt Carlson69f11c92011-07-13 09:27:30 +000015053 }
15054
Michael Chan7d0c41e2005-04-21 17:06:20 -070015055 /* Get eeprom hw config before calling tg3_set_power_state().
Joe Perches63c3a662011-04-26 08:12:10 +000015056 * In particular, the TG3_FLAG_IS_NIC flag must be
Michael Chan7d0c41e2005-04-21 17:06:20 -070015057 * determined before calling tg3_set_power_state() so that
15058 * we know whether or not to switch out of Vaux power.
15059 * When the flag is set, it means that GPIO1 is used for eeprom
15060 * write protect and also implies that it is a LOM where GPIOs
15061 * are not used to switch power.
Jeff Garzik6aa20a22006-09-13 13:24:59 -040015062 */
Michael Chan7d0c41e2005-04-21 17:06:20 -070015063 tg3_get_eeprom_hw_cfg(tp);
15064
Matt Carlsoncf9ecf42011-11-28 09:41:03 +000015065 if (tp->fw_needed && tg3_flag(tp, ENABLE_ASF)) {
15066 tg3_flag_clear(tp, TSO_CAPABLE);
15067 tg3_flag_clear(tp, TSO_BUG);
15068 tp->fw_needed = NULL;
15069 }
15070
Joe Perches63c3a662011-04-26 08:12:10 +000015071 if (tg3_flag(tp, ENABLE_APE)) {
Matt Carlson0d3031d2007-10-10 18:02:43 -070015072 /* Allow reads and writes to the
15073 * APE register and memory space.
15074 */
15075 pci_state_reg |= PCISTATE_ALLOW_APE_CTLSPC_WR |
Matt Carlsonf92d9dc12010-06-05 17:24:30 +000015076 PCISTATE_ALLOW_APE_SHMEM_WR |
15077 PCISTATE_ALLOW_APE_PSPACE_WR;
Matt Carlson0d3031d2007-10-10 18:02:43 -070015078 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE,
15079 pci_state_reg);
Matt Carlsonc9cab242011-07-13 09:27:27 +000015080
15081 tg3_ape_lock_init(tp);
Matt Carlson0d3031d2007-10-10 18:02:43 -070015082 }
15083
Matt Carlson16821282011-07-13 09:27:28 +000015084 /* Set up tp->grc_local_ctrl before calling
15085 * tg3_pwrsrc_switch_to_vmain(). GPIO1 driven high
15086 * will bring 5700's external PHY out of reset.
Michael Chan314fba32005-04-21 17:07:04 -070015087 * It is also used as eeprom write protect on LOMs.
15088 */
15089 tp->grc_local_ctrl = GRC_LCLCTRL_INT_ON_ATTN | GRC_LCLCTRL_AUTO_SEEPROM;
Matt Carlson6ff6f812011-05-19 12:12:54 +000015090 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
Joe Perches63c3a662011-04-26 08:12:10 +000015091 tg3_flag(tp, EEPROM_WRITE_PROT))
Michael Chan314fba32005-04-21 17:07:04 -070015092 tp->grc_local_ctrl |= (GRC_LCLCTRL_GPIO_OE1 |
15093 GRC_LCLCTRL_GPIO_OUTPUT1);
Michael Chan3e7d83b2005-04-21 17:10:36 -070015094 /* Unused GPIO3 must be driven as output on 5752 because there
15095 * are no pull-up resistors on unused GPIO pins.
15096 */
15097 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752)
15098 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE3;
Michael Chan314fba32005-04-21 17:07:04 -070015099
Matt Carlson321d32a2008-11-21 17:22:19 -080015100 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
Matt Carlsoncb4ed1f2010-01-20 16:58:09 +000015101 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
Matt Carlson55086ad2011-12-14 11:09:59 +000015102 tg3_flag(tp, 57765_CLASS))
Michael Chanaf36e6b2006-03-23 01:28:06 -080015103 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_UART_SEL;
15104
Matt Carlson8d519ab2009-04-20 06:58:01 +000015105 if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761 ||
15106 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761S) {
Matt Carlson5f0c4a32008-06-09 15:41:12 -070015107 /* Turn off the debug UART. */
15108 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_UART_SEL;
Joe Perches63c3a662011-04-26 08:12:10 +000015109 if (tg3_flag(tp, IS_NIC))
Matt Carlson5f0c4a32008-06-09 15:41:12 -070015110 /* Keep VMain power. */
15111 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE0 |
15112 GRC_LCLCTRL_GPIO_OUTPUT0;
15113 }
15114
Matt Carlson16821282011-07-13 09:27:28 +000015115 /* Switch out of Vaux if it is a NIC */
15116 tg3_pwrsrc_switch_to_vmain(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015117
Linus Torvalds1da177e2005-04-16 15:20:36 -070015118 /* Derive initial jumbo mode from MTU assigned in
15119 * ether_setup() via the alloc_etherdev() call
15120 */
Joe Perches63c3a662011-04-26 08:12:10 +000015121 if (tp->dev->mtu > ETH_DATA_LEN && !tg3_flag(tp, 5780_CLASS))
15122 tg3_flag_set(tp, JUMBO_RING_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015123
15124 /* Determine WakeOnLan speed to use. */
15125 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
15126 tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
15127 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0 ||
15128 tp->pci_chip_rev_id == CHIPREV_ID_5701_B2) {
Joe Perches63c3a662011-04-26 08:12:10 +000015129 tg3_flag_clear(tp, WOL_SPEED_100MB);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015130 } else {
Joe Perches63c3a662011-04-26 08:12:10 +000015131 tg3_flag_set(tp, WOL_SPEED_100MB);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015132 }
15133
Matt Carlson7f97a4b2009-08-25 10:10:03 +000015134 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015135 tp->phy_flags |= TG3_PHYFLG_IS_FET;
Matt Carlson7f97a4b2009-08-25 10:10:03 +000015136
Linus Torvalds1da177e2005-04-16 15:20:36 -070015137 /* A few boards don't want Ethernet@WireSpeed phy feature */
Matt Carlson6ff6f812011-05-19 12:12:54 +000015138 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
15139 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070015140 (tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) &&
Michael Chan747e8f82005-07-25 12:33:22 -070015141 (tp->pci_chip_rev_id != CHIPREV_ID_5705_A1)) ||
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015142 (tp->phy_flags & TG3_PHYFLG_IS_FET) ||
15143 (tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
15144 tp->phy_flags |= TG3_PHYFLG_NO_ETH_WIRE_SPEED;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015145
15146 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5703_AX ||
15147 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5704_AX)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015148 tp->phy_flags |= TG3_PHYFLG_ADC_BUG;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015149 if (tp->pci_chip_rev_id == CHIPREV_ID_5704_A0)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015150 tp->phy_flags |= TG3_PHYFLG_5704_A0_BUG;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015151
Joe Perches63c3a662011-04-26 08:12:10 +000015152 if (tg3_flag(tp, 5705_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015153 !(tp->phy_flags & TG3_PHYFLG_IS_FET) &&
Matt Carlson321d32a2008-11-21 17:22:19 -080015154 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000015155 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_57780 &&
Joe Perches63c3a662011-04-26 08:12:10 +000015156 !tg3_flag(tp, 57765_PLUS)) {
Michael Chanc424cb22006-04-29 18:56:34 -070015157 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
Matt Carlsond30cdd22007-10-07 23:28:35 -070015158 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787 ||
Matt Carlson9936bcf2007-10-10 18:03:07 -070015159 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
15160 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761) {
Michael Chand4011ad2007-02-13 12:17:25 -080015161 if (tp->pdev->device != PCI_DEVICE_ID_TIGON3_5756 &&
15162 tp->pdev->device != PCI_DEVICE_ID_TIGON3_5722)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015163 tp->phy_flags |= TG3_PHYFLG_JITTER_BUG;
Michael Chanc1d2a192007-01-08 19:57:20 -080015164 if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5755M)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015165 tp->phy_flags |= TG3_PHYFLG_ADJUST_TRIM;
Matt Carlson321d32a2008-11-21 17:22:19 -080015166 } else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015167 tp->phy_flags |= TG3_PHYFLG_BER_BUG;
Michael Chanc424cb22006-04-29 18:56:34 -070015168 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015169
Matt Carlsonb2a5c192008-04-03 21:44:44 -070015170 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
15171 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX) {
15172 tp->phy_otp = tg3_read_otp_phycfg(tp);
15173 if (tp->phy_otp == 0)
15174 tp->phy_otp = TG3_OTP_DEFAULT;
15175 }
15176
Joe Perches63c3a662011-04-26 08:12:10 +000015177 if (tg3_flag(tp, CPMU_PRESENT))
Matt Carlson8ef21422008-05-02 16:47:53 -070015178 tp->mi_mode = MAC_MI_MODE_500KHZ_CONST;
15179 else
15180 tp->mi_mode = MAC_MI_MODE_BASE;
15181
Linus Torvalds1da177e2005-04-16 15:20:36 -070015182 tp->coalesce_mode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015183 if (GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5700_AX &&
15184 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5700_BX)
15185 tp->coalesce_mode |= HOSTCC_MODE_32BYTE;
15186
Matt Carlson4d958472011-04-20 07:57:35 +000015187 /* Set these bits to enable statistics workaround. */
15188 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
15189 tp->pci_chip_rev_id == CHIPREV_ID_5719_A0 ||
15190 tp->pci_chip_rev_id == CHIPREV_ID_5720_A0) {
15191 tp->coalesce_mode |= HOSTCC_MODE_ATTN;
15192 tp->grc_mode |= GRC_MODE_IRQ_ON_FLOW_ATTN;
15193 }
15194
Matt Carlson321d32a2008-11-21 17:22:19 -080015195 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
15196 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
Joe Perches63c3a662011-04-26 08:12:10 +000015197 tg3_flag_set(tp, USE_PHYLIB);
Matt Carlson57e69832008-05-25 23:48:31 -070015198
Matt Carlson158d7ab2008-05-29 01:37:54 -070015199 err = tg3_mdio_init(tp);
15200 if (err)
15201 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015202
15203 /* Initialize data/descriptor byte/word swapping. */
15204 val = tr32(GRC_MODE);
Matt Carlsonf2096f92011-04-05 14:22:48 +000015205 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
15206 val &= (GRC_MODE_BYTE_SWAP_B2HRX_DATA |
15207 GRC_MODE_WORD_SWAP_B2HRX_DATA |
15208 GRC_MODE_B2HRX_ENABLE |
15209 GRC_MODE_HTX2B_ENABLE |
15210 GRC_MODE_HOST_STACKUP);
15211 else
15212 val &= GRC_MODE_HOST_STACKUP;
15213
Linus Torvalds1da177e2005-04-16 15:20:36 -070015214 tw32(GRC_MODE, val | tp->grc_mode);
15215
15216 tg3_switch_clocks(tp);
15217
15218 /* Clear this out for sanity. */
15219 tw32(TG3PCI_MEM_WIN_BASE_ADDR, 0);
15220
15221 pci_read_config_dword(tp->pdev, TG3PCI_PCISTATE,
15222 &pci_state_reg);
15223 if ((pci_state_reg & PCISTATE_CONV_PCI_MODE) == 0 &&
Joe Perches63c3a662011-04-26 08:12:10 +000015224 !tg3_flag(tp, PCIX_TARGET_HWBUG)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070015225 u32 chiprevid = GET_CHIP_REV_ID(tp->misc_host_ctrl);
15226
15227 if (chiprevid == CHIPREV_ID_5701_A0 ||
15228 chiprevid == CHIPREV_ID_5701_B0 ||
15229 chiprevid == CHIPREV_ID_5701_B2 ||
15230 chiprevid == CHIPREV_ID_5701_B5) {
15231 void __iomem *sram_base;
15232
15233 /* Write some dummy words into the SRAM status block
15234 * area, see if it reads back correctly. If the return
15235 * value is bad, force enable the PCIX workaround.
15236 */
15237 sram_base = tp->regs + NIC_SRAM_WIN_BASE + NIC_SRAM_STATS_BLK;
15238
15239 writel(0x00000000, sram_base);
15240 writel(0x00000000, sram_base + 4);
15241 writel(0xffffffff, sram_base + 4);
15242 if (readl(sram_base) != 0x00000000)
Joe Perches63c3a662011-04-26 08:12:10 +000015243 tg3_flag_set(tp, PCIX_TARGET_HWBUG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015244 }
15245 }
15246
15247 udelay(50);
15248 tg3_nvram_init(tp);
15249
15250 grc_misc_cfg = tr32(GRC_MISC_CFG);
15251 grc_misc_cfg &= GRC_MISC_CFG_BOARD_ID_MASK;
15252
Linus Torvalds1da177e2005-04-16 15:20:36 -070015253 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
15254 (grc_misc_cfg == GRC_MISC_CFG_BOARD_ID_5788 ||
15255 grc_misc_cfg == GRC_MISC_CFG_BOARD_ID_5788M))
Joe Perches63c3a662011-04-26 08:12:10 +000015256 tg3_flag_set(tp, IS_5788);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015257
Joe Perches63c3a662011-04-26 08:12:10 +000015258 if (!tg3_flag(tp, IS_5788) &&
Matt Carlson6ff6f812011-05-19 12:12:54 +000015259 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700)
Joe Perches63c3a662011-04-26 08:12:10 +000015260 tg3_flag_set(tp, TAGGED_STATUS);
15261 if (tg3_flag(tp, TAGGED_STATUS)) {
David S. Millerfac9b832005-05-18 22:46:34 -070015262 tp->coalesce_mode |= (HOSTCC_MODE_CLRTICK_RXBD |
15263 HOSTCC_MODE_CLRTICK_TXBD);
15264
15265 tp->misc_host_ctrl |= MISC_HOST_CTRL_TAGGED_STATUS;
15266 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
15267 tp->misc_host_ctrl);
15268 }
15269
Matt Carlson3bda1252008-08-15 14:08:22 -070015270 /* Preserve the APE MAC_MODE bits */
Joe Perches63c3a662011-04-26 08:12:10 +000015271 if (tg3_flag(tp, ENABLE_APE))
Matt Carlsond2394e6b2010-11-24 08:31:47 +000015272 tp->mac_mode = MAC_MODE_APE_TX_EN | MAC_MODE_APE_RX_EN;
Matt Carlson3bda1252008-08-15 14:08:22 -070015273 else
Matt Carlson6e01b202011-08-19 13:58:20 +000015274 tp->mac_mode = 0;
Matt Carlson3bda1252008-08-15 14:08:22 -070015275
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +000015276 if (tg3_10_100_only_device(tp, ent))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015277 tp->phy_flags |= TG3_PHYFLG_10_100_ONLY;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015278
15279 err = tg3_phy_probe(tp);
15280 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000015281 dev_err(&tp->pdev->dev, "phy probe failed, err %d\n", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015282 /* ... but do not return immediately ... */
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015283 tg3_mdio_fini(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015284 }
15285
Matt Carlson184b8902010-04-05 10:19:25 +000015286 tg3_read_vpd(tp);
Michael Chanc4e65752006-03-20 22:29:32 -080015287 tg3_read_fw_ver(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015288
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015289 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
15290 tp->phy_flags &= ~TG3_PHYFLG_USE_MI_INTERRUPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015291 } else {
15292 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015293 tp->phy_flags |= TG3_PHYFLG_USE_MI_INTERRUPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015294 else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015295 tp->phy_flags &= ~TG3_PHYFLG_USE_MI_INTERRUPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015296 }
15297
15298 /* 5700 {AX,BX} chips have a broken status block link
15299 * change bit implementation, so we must use the
15300 * status register in those cases.
15301 */
15302 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700)
Joe Perches63c3a662011-04-26 08:12:10 +000015303 tg3_flag_set(tp, USE_LINKCHG_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015304 else
Joe Perches63c3a662011-04-26 08:12:10 +000015305 tg3_flag_clear(tp, USE_LINKCHG_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015306
15307 /* The led_ctrl is set during tg3_phy_probe, here we might
15308 * have to force the link status polling mechanism based
15309 * upon subsystem IDs.
15310 */
15311 if (tp->pdev->subsystem_vendor == PCI_VENDOR_ID_DELL &&
Michael Chan007a880d2007-05-31 14:49:51 -070015312 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701 &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015313 !(tp->phy_flags & TG3_PHYFLG_PHY_SERDES)) {
15314 tp->phy_flags |= TG3_PHYFLG_USE_MI_INTERRUPT;
Joe Perches63c3a662011-04-26 08:12:10 +000015315 tg3_flag_set(tp, USE_LINKCHG_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015316 }
15317
15318 /* For all SERDES we poll the MAC status register. */
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015319 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Joe Perches63c3a662011-04-26 08:12:10 +000015320 tg3_flag_set(tp, POLL_SERDES);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015321 else
Joe Perches63c3a662011-04-26 08:12:10 +000015322 tg3_flag_clear(tp, POLL_SERDES);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015323
Eric Dumazet9205fd92011-11-18 06:47:01 +000015324 tp->rx_offset = NET_SKB_PAD + NET_IP_ALIGN;
Matt Carlsond2757fc2010-04-12 06:58:27 +000015325 tp->rx_copy_thresh = TG3_RX_COPY_THRESHOLD;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015326 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701 &&
Joe Perches63c3a662011-04-26 08:12:10 +000015327 tg3_flag(tp, PCIX_MODE)) {
Eric Dumazet9205fd92011-11-18 06:47:01 +000015328 tp->rx_offset = NET_SKB_PAD;
Matt Carlsond2757fc2010-04-12 06:58:27 +000015329#ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
Matt Carlson9dc7a112010-04-12 06:58:28 +000015330 tp->rx_copy_thresh = ~(u16)0;
Matt Carlsond2757fc2010-04-12 06:58:27 +000015331#endif
15332 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015333
Matt Carlson2c49a442010-09-30 10:34:35 +000015334 tp->rx_std_ring_mask = TG3_RX_STD_RING_SIZE(tp) - 1;
15335 tp->rx_jmb_ring_mask = TG3_RX_JMB_RING_SIZE(tp) - 1;
Matt Carlson7cb32cf2010-09-30 10:34:36 +000015336 tp->rx_ret_ring_mask = tg3_rx_ret_ring_size(tp) - 1;
15337
Matt Carlson2c49a442010-09-30 10:34:35 +000015338 tp->rx_std_max_post = tp->rx_std_ring_mask + 1;
Michael Chanf92905d2006-06-29 20:14:29 -070015339
15340 /* Increment the rx prod index on the rx std ring by at most
15341 * 8 for these chips to workaround hw errata.
15342 */
15343 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 ||
15344 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752 ||
15345 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755)
15346 tp->rx_std_max_post = 8;
15347
Joe Perches63c3a662011-04-26 08:12:10 +000015348 if (tg3_flag(tp, ASPM_WORKAROUND))
Matt Carlson8ed5d972007-05-07 00:25:49 -070015349 tp->pwrmgmt_thresh = tr32(PCIE_PWR_MGMT_THRESH) &
15350 PCIE_PWR_MGMT_L1_THRESH_MSK;
15351
Linus Torvalds1da177e2005-04-16 15:20:36 -070015352 return err;
15353}
15354
David S. Miller49b6e95f2007-03-29 01:38:42 -070015355#ifdef CONFIG_SPARC
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015356static int tg3_get_macaddr_sparc(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015357{
15358 struct net_device *dev = tp->dev;
15359 struct pci_dev *pdev = tp->pdev;
David S. Miller49b6e95f2007-03-29 01:38:42 -070015360 struct device_node *dp = pci_device_to_OF_node(pdev);
David S. Miller374d4ca2007-03-29 01:57:57 -070015361 const unsigned char *addr;
David S. Miller49b6e95f2007-03-29 01:38:42 -070015362 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015363
David S. Miller49b6e95f2007-03-29 01:38:42 -070015364 addr = of_get_property(dp, "local-mac-address", &len);
15365 if (addr && len == 6) {
15366 memcpy(dev->dev_addr, addr, 6);
15367 memcpy(dev->perm_addr, dev->dev_addr, 6);
15368 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015369 }
15370 return -ENODEV;
15371}
15372
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015373static int tg3_get_default_macaddr_sparc(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015374{
15375 struct net_device *dev = tp->dev;
15376
15377 memcpy(dev->dev_addr, idprom->id_ethaddr, 6);
John W. Linville2ff43692005-09-12 14:44:20 -070015378 memcpy(dev->perm_addr, idprom->id_ethaddr, 6);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015379 return 0;
15380}
15381#endif
15382
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015383static int tg3_get_device_address(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015384{
15385 struct net_device *dev = tp->dev;
15386 u32 hi, lo, mac_offset;
Michael Chan008652b2006-03-27 23:14:53 -080015387 int addr_ok = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015388
David S. Miller49b6e95f2007-03-29 01:38:42 -070015389#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070015390 if (!tg3_get_macaddr_sparc(tp))
15391 return 0;
15392#endif
15393
15394 mac_offset = 0x7c;
Matt Carlson6ff6f812011-05-19 12:12:54 +000015395 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
Joe Perches63c3a662011-04-26 08:12:10 +000015396 tg3_flag(tp, 5780_CLASS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070015397 if (tr32(TG3PCI_DUAL_MAC_CTRL) & DUAL_MAC_CTRL_ID)
15398 mac_offset = 0xcc;
15399 if (tg3_nvram_lock(tp))
15400 tw32_f(NVRAM_CMD, NVRAM_CMD_RESET);
15401 else
15402 tg3_nvram_unlock(tp);
Joe Perches63c3a662011-04-26 08:12:10 +000015403 } else if (tg3_flag(tp, 5717_PLUS)) {
Matt Carlson69f11c92011-07-13 09:27:30 +000015404 if (tp->pci_fn & 1)
Matt Carlsona1b950d2009-09-01 13:20:17 +000015405 mac_offset = 0xcc;
Matt Carlson69f11c92011-07-13 09:27:30 +000015406 if (tp->pci_fn > 1)
Matt Carlsona50d0792010-06-05 17:24:37 +000015407 mac_offset += 0x18c;
Matt Carlsona1b950d2009-09-01 13:20:17 +000015408 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
Michael Chanb5d37722006-09-27 16:06:21 -070015409 mac_offset = 0x10;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015410
15411 /* First try to get it from MAC address mailbox. */
15412 tg3_read_mem(tp, NIC_SRAM_MAC_ADDR_HIGH_MBOX, &hi);
15413 if ((hi >> 16) == 0x484b) {
15414 dev->dev_addr[0] = (hi >> 8) & 0xff;
15415 dev->dev_addr[1] = (hi >> 0) & 0xff;
15416
15417 tg3_read_mem(tp, NIC_SRAM_MAC_ADDR_LOW_MBOX, &lo);
15418 dev->dev_addr[2] = (lo >> 24) & 0xff;
15419 dev->dev_addr[3] = (lo >> 16) & 0xff;
15420 dev->dev_addr[4] = (lo >> 8) & 0xff;
15421 dev->dev_addr[5] = (lo >> 0) & 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015422
Michael Chan008652b2006-03-27 23:14:53 -080015423 /* Some old bootcode may report a 0 MAC address in SRAM */
15424 addr_ok = is_valid_ether_addr(&dev->dev_addr[0]);
15425 }
15426 if (!addr_ok) {
15427 /* Next, try NVRAM. */
Joe Perches63c3a662011-04-26 08:12:10 +000015428 if (!tg3_flag(tp, NO_NVRAM) &&
Matt Carlsondf259d82009-04-20 06:57:14 +000015429 !tg3_nvram_read_be32(tp, mac_offset + 0, &hi) &&
Matt Carlson6d348f22009-02-25 14:25:52 +000015430 !tg3_nvram_read_be32(tp, mac_offset + 4, &lo)) {
Matt Carlson62cedd12009-04-20 14:52:29 -070015431 memcpy(&dev->dev_addr[0], ((char *)&hi) + 2, 2);
15432 memcpy(&dev->dev_addr[2], (char *)&lo, sizeof(lo));
Michael Chan008652b2006-03-27 23:14:53 -080015433 }
15434 /* Finally just fetch it out of the MAC control regs. */
15435 else {
15436 hi = tr32(MAC_ADDR_0_HIGH);
15437 lo = tr32(MAC_ADDR_0_LOW);
15438
15439 dev->dev_addr[5] = lo & 0xff;
15440 dev->dev_addr[4] = (lo >> 8) & 0xff;
15441 dev->dev_addr[3] = (lo >> 16) & 0xff;
15442 dev->dev_addr[2] = (lo >> 24) & 0xff;
15443 dev->dev_addr[1] = hi & 0xff;
15444 dev->dev_addr[0] = (hi >> 8) & 0xff;
15445 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015446 }
15447
15448 if (!is_valid_ether_addr(&dev->dev_addr[0])) {
David S. Miller7582a332008-03-20 15:53:15 -070015449#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070015450 if (!tg3_get_default_macaddr_sparc(tp))
15451 return 0;
15452#endif
15453 return -EINVAL;
15454 }
John W. Linville2ff43692005-09-12 14:44:20 -070015455 memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015456 return 0;
15457}
15458
David S. Miller59e6b432005-05-18 22:50:10 -070015459#define BOUNDARY_SINGLE_CACHELINE 1
15460#define BOUNDARY_MULTI_CACHELINE 2
15461
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015462static u32 tg3_calc_dma_bndry(struct tg3 *tp, u32 val)
David S. Miller59e6b432005-05-18 22:50:10 -070015463{
15464 int cacheline_size;
15465 u8 byte;
15466 int goal;
15467
15468 pci_read_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE, &byte);
15469 if (byte == 0)
15470 cacheline_size = 1024;
15471 else
15472 cacheline_size = (int) byte * 4;
15473
15474 /* On 5703 and later chips, the boundary bits have no
15475 * effect.
15476 */
15477 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
15478 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701 &&
Joe Perches63c3a662011-04-26 08:12:10 +000015479 !tg3_flag(tp, PCI_EXPRESS))
David S. Miller59e6b432005-05-18 22:50:10 -070015480 goto out;
15481
15482#if defined(CONFIG_PPC64) || defined(CONFIG_IA64) || defined(CONFIG_PARISC)
15483 goal = BOUNDARY_MULTI_CACHELINE;
15484#else
15485#if defined(CONFIG_SPARC64) || defined(CONFIG_ALPHA)
15486 goal = BOUNDARY_SINGLE_CACHELINE;
15487#else
15488 goal = 0;
15489#endif
15490#endif
15491
Joe Perches63c3a662011-04-26 08:12:10 +000015492 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000015493 val = goal ? 0 : DMA_RWCTRL_DIS_CACHE_ALIGNMENT;
15494 goto out;
15495 }
15496
David S. Miller59e6b432005-05-18 22:50:10 -070015497 if (!goal)
15498 goto out;
15499
15500 /* PCI controllers on most RISC systems tend to disconnect
15501 * when a device tries to burst across a cache-line boundary.
15502 * Therefore, letting tg3 do so just wastes PCI bandwidth.
15503 *
15504 * Unfortunately, for PCI-E there are only limited
15505 * write-side controls for this, and thus for reads
15506 * we will still get the disconnects. We'll also waste
15507 * these PCI cycles for both read and write for chips
15508 * other than 5700 and 5701 which do not implement the
15509 * boundary bits.
15510 */
Joe Perches63c3a662011-04-26 08:12:10 +000015511 if (tg3_flag(tp, PCIX_MODE) && !tg3_flag(tp, PCI_EXPRESS)) {
David S. Miller59e6b432005-05-18 22:50:10 -070015512 switch (cacheline_size) {
15513 case 16:
15514 case 32:
15515 case 64:
15516 case 128:
15517 if (goal == BOUNDARY_SINGLE_CACHELINE) {
15518 val |= (DMA_RWCTRL_READ_BNDRY_128_PCIX |
15519 DMA_RWCTRL_WRITE_BNDRY_128_PCIX);
15520 } else {
15521 val |= (DMA_RWCTRL_READ_BNDRY_384_PCIX |
15522 DMA_RWCTRL_WRITE_BNDRY_384_PCIX);
15523 }
15524 break;
15525
15526 case 256:
15527 val |= (DMA_RWCTRL_READ_BNDRY_256_PCIX |
15528 DMA_RWCTRL_WRITE_BNDRY_256_PCIX);
15529 break;
15530
15531 default:
15532 val |= (DMA_RWCTRL_READ_BNDRY_384_PCIX |
15533 DMA_RWCTRL_WRITE_BNDRY_384_PCIX);
15534 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070015535 }
Joe Perches63c3a662011-04-26 08:12:10 +000015536 } else if (tg3_flag(tp, PCI_EXPRESS)) {
David S. Miller59e6b432005-05-18 22:50:10 -070015537 switch (cacheline_size) {
15538 case 16:
15539 case 32:
15540 case 64:
15541 if (goal == BOUNDARY_SINGLE_CACHELINE) {
15542 val &= ~DMA_RWCTRL_WRITE_BNDRY_DISAB_PCIE;
15543 val |= DMA_RWCTRL_WRITE_BNDRY_64_PCIE;
15544 break;
15545 }
15546 /* fallthrough */
15547 case 128:
15548 default:
15549 val &= ~DMA_RWCTRL_WRITE_BNDRY_DISAB_PCIE;
15550 val |= DMA_RWCTRL_WRITE_BNDRY_128_PCIE;
15551 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070015552 }
David S. Miller59e6b432005-05-18 22:50:10 -070015553 } else {
15554 switch (cacheline_size) {
15555 case 16:
15556 if (goal == BOUNDARY_SINGLE_CACHELINE) {
15557 val |= (DMA_RWCTRL_READ_BNDRY_16 |
15558 DMA_RWCTRL_WRITE_BNDRY_16);
15559 break;
15560 }
15561 /* fallthrough */
15562 case 32:
15563 if (goal == BOUNDARY_SINGLE_CACHELINE) {
15564 val |= (DMA_RWCTRL_READ_BNDRY_32 |
15565 DMA_RWCTRL_WRITE_BNDRY_32);
15566 break;
15567 }
15568 /* fallthrough */
15569 case 64:
15570 if (goal == BOUNDARY_SINGLE_CACHELINE) {
15571 val |= (DMA_RWCTRL_READ_BNDRY_64 |
15572 DMA_RWCTRL_WRITE_BNDRY_64);
15573 break;
15574 }
15575 /* fallthrough */
15576 case 128:
15577 if (goal == BOUNDARY_SINGLE_CACHELINE) {
15578 val |= (DMA_RWCTRL_READ_BNDRY_128 |
15579 DMA_RWCTRL_WRITE_BNDRY_128);
15580 break;
15581 }
15582 /* fallthrough */
15583 case 256:
15584 val |= (DMA_RWCTRL_READ_BNDRY_256 |
15585 DMA_RWCTRL_WRITE_BNDRY_256);
15586 break;
15587 case 512:
15588 val |= (DMA_RWCTRL_READ_BNDRY_512 |
15589 DMA_RWCTRL_WRITE_BNDRY_512);
15590 break;
15591 case 1024:
15592 default:
15593 val |= (DMA_RWCTRL_READ_BNDRY_1024 |
15594 DMA_RWCTRL_WRITE_BNDRY_1024);
15595 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070015596 }
David S. Miller59e6b432005-05-18 22:50:10 -070015597 }
15598
15599out:
15600 return val;
15601}
15602
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015603static int tg3_do_test_dma(struct tg3 *tp, u32 *buf, dma_addr_t buf_dma,
15604 int size, int to_device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015605{
15606 struct tg3_internal_buffer_desc test_desc;
15607 u32 sram_dma_descs;
15608 int i, ret;
15609
15610 sram_dma_descs = NIC_SRAM_DMA_DESC_POOL_BASE;
15611
15612 tw32(FTQ_RCVBD_COMP_FIFO_ENQDEQ, 0);
15613 tw32(FTQ_RCVDATA_COMP_FIFO_ENQDEQ, 0);
15614 tw32(RDMAC_STATUS, 0);
15615 tw32(WDMAC_STATUS, 0);
15616
15617 tw32(BUFMGR_MODE, 0);
15618 tw32(FTQ_RESET, 0);
15619
15620 test_desc.addr_hi = ((u64) buf_dma) >> 32;
15621 test_desc.addr_lo = buf_dma & 0xffffffff;
15622 test_desc.nic_mbuf = 0x00002100;
15623 test_desc.len = size;
15624
15625 /*
15626 * HP ZX1 was seeing test failures for 5701 cards running at 33Mhz
15627 * the *second* time the tg3 driver was getting loaded after an
15628 * initial scan.
15629 *
15630 * Broadcom tells me:
15631 * ...the DMA engine is connected to the GRC block and a DMA
15632 * reset may affect the GRC block in some unpredictable way...
15633 * The behavior of resets to individual blocks has not been tested.
15634 *
15635 * Broadcom noted the GRC reset will also reset all sub-components.
15636 */
15637 if (to_device) {
15638 test_desc.cqid_sqid = (13 << 8) | 2;
15639
15640 tw32_f(RDMAC_MODE, RDMAC_MODE_ENABLE);
15641 udelay(40);
15642 } else {
15643 test_desc.cqid_sqid = (16 << 8) | 7;
15644
15645 tw32_f(WDMAC_MODE, WDMAC_MODE_ENABLE);
15646 udelay(40);
15647 }
15648 test_desc.flags = 0x00000005;
15649
15650 for (i = 0; i < (sizeof(test_desc) / sizeof(u32)); i++) {
15651 u32 val;
15652
15653 val = *(((u32 *)&test_desc) + i);
15654 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR,
15655 sram_dma_descs + (i * sizeof(u32)));
15656 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_DATA, val);
15657 }
15658 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, 0);
15659
Matt Carlson859a588792010-04-05 10:19:28 +000015660 if (to_device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015661 tw32(FTQ_DMA_HIGH_READ_FIFO_ENQDEQ, sram_dma_descs);
Matt Carlson859a588792010-04-05 10:19:28 +000015662 else
Linus Torvalds1da177e2005-04-16 15:20:36 -070015663 tw32(FTQ_DMA_HIGH_WRITE_FIFO_ENQDEQ, sram_dma_descs);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015664
15665 ret = -ENODEV;
15666 for (i = 0; i < 40; i++) {
15667 u32 val;
15668
15669 if (to_device)
15670 val = tr32(FTQ_RCVBD_COMP_FIFO_ENQDEQ);
15671 else
15672 val = tr32(FTQ_RCVDATA_COMP_FIFO_ENQDEQ);
15673 if ((val & 0xffff) == sram_dma_descs) {
15674 ret = 0;
15675 break;
15676 }
15677
15678 udelay(100);
15679 }
15680
15681 return ret;
15682}
15683
David S. Millerded73402005-05-23 13:59:47 -070015684#define TEST_BUFFER_SIZE 0x2000
Linus Torvalds1da177e2005-04-16 15:20:36 -070015685
Matt Carlson41434702011-03-09 16:58:22 +000015686static DEFINE_PCI_DEVICE_TABLE(tg3_dma_wait_state_chipsets) = {
Joe Perches895950c2010-12-21 02:16:08 -080015687 { PCI_DEVICE(PCI_VENDOR_ID_APPLE, PCI_DEVICE_ID_APPLE_UNI_N_PCI15) },
15688 { },
15689};
15690
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015691static int tg3_test_dma(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015692{
15693 dma_addr_t buf_dma;
David S. Miller59e6b432005-05-18 22:50:10 -070015694 u32 *buf, saved_dma_rwctrl;
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000015695 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015696
Matt Carlson4bae65c2010-11-24 08:31:52 +000015697 buf = dma_alloc_coherent(&tp->pdev->dev, TEST_BUFFER_SIZE,
15698 &buf_dma, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015699 if (!buf) {
15700 ret = -ENOMEM;
15701 goto out_nofree;
15702 }
15703
15704 tp->dma_rwctrl = ((0x7 << DMA_RWCTRL_PCI_WRITE_CMD_SHIFT) |
15705 (0x6 << DMA_RWCTRL_PCI_READ_CMD_SHIFT));
15706
David S. Miller59e6b432005-05-18 22:50:10 -070015707 tp->dma_rwctrl = tg3_calc_dma_bndry(tp, tp->dma_rwctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015708
Joe Perches63c3a662011-04-26 08:12:10 +000015709 if (tg3_flag(tp, 57765_PLUS))
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000015710 goto out;
15711
Joe Perches63c3a662011-04-26 08:12:10 +000015712 if (tg3_flag(tp, PCI_EXPRESS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070015713 /* DMA read watermark not used on PCIE */
15714 tp->dma_rwctrl |= 0x00180000;
Joe Perches63c3a662011-04-26 08:12:10 +000015715 } else if (!tg3_flag(tp, PCIX_MODE)) {
Michael Chan85e94ce2005-04-21 17:05:28 -070015716 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 ||
15717 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015718 tp->dma_rwctrl |= 0x003f0000;
15719 else
15720 tp->dma_rwctrl |= 0x003f000f;
15721 } else {
15722 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
15723 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
15724 u32 ccval = (tr32(TG3PCI_CLOCK_CTRL) & 0x1f);
Michael Chan49afdeb2007-02-13 12:17:03 -080015725 u32 read_water = 0x7;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015726
Michael Chan4a29cc22006-03-19 13:21:12 -080015727 /* If the 5704 is behind the EPB bridge, we can
15728 * do the less restrictive ONE_DMA workaround for
15729 * better performance.
15730 */
Joe Perches63c3a662011-04-26 08:12:10 +000015731 if (tg3_flag(tp, 40BIT_DMA_BUG) &&
Michael Chan4a29cc22006-03-19 13:21:12 -080015732 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704)
15733 tp->dma_rwctrl |= 0x8000;
15734 else if (ccval == 0x6 || ccval == 0x7)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015735 tp->dma_rwctrl |= DMA_RWCTRL_ONE_DMA;
15736
Michael Chan49afdeb2007-02-13 12:17:03 -080015737 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703)
15738 read_water = 4;
David S. Miller59e6b432005-05-18 22:50:10 -070015739 /* Set bit 23 to enable PCIX hw bug fix */
Michael Chan49afdeb2007-02-13 12:17:03 -080015740 tp->dma_rwctrl |=
15741 (read_water << DMA_RWCTRL_READ_WATER_SHIFT) |
15742 (0x3 << DMA_RWCTRL_WRITE_WATER_SHIFT) |
15743 (1 << 23);
Michael Chan4cf78e42005-07-25 12:29:19 -070015744 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780) {
15745 /* 5780 always in PCIX mode */
15746 tp->dma_rwctrl |= 0x00144000;
Michael Chana4e2b342005-10-26 15:46:52 -070015747 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
15748 /* 5714 always in PCIX mode */
15749 tp->dma_rwctrl |= 0x00148000;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015750 } else {
15751 tp->dma_rwctrl |= 0x001b000f;
15752 }
15753 }
15754
15755 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
15756 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704)
15757 tp->dma_rwctrl &= 0xfffffff0;
15758
15759 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
15760 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
15761 /* Remove this if it causes problems for some boards. */
15762 tp->dma_rwctrl |= DMA_RWCTRL_USE_MEM_READ_MULT;
15763
15764 /* On 5700/5701 chips, we need to set this bit.
15765 * Otherwise the chip will issue cacheline transactions
15766 * to streamable DMA memory with not all the byte
15767 * enables turned on. This is an error on several
15768 * RISC PCI controllers, in particular sparc64.
15769 *
15770 * On 5703/5704 chips, this bit has been reassigned
15771 * a different meaning. In particular, it is used
15772 * on those chips to enable a PCI-X workaround.
15773 */
15774 tp->dma_rwctrl |= DMA_RWCTRL_ASSERT_ALL_BE;
15775 }
15776
15777 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
15778
15779#if 0
15780 /* Unneeded, already done by tg3_get_invariants. */
15781 tg3_switch_clocks(tp);
15782#endif
15783
Linus Torvalds1da177e2005-04-16 15:20:36 -070015784 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
15785 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701)
15786 goto out;
15787
David S. Miller59e6b432005-05-18 22:50:10 -070015788 /* It is best to perform DMA test with maximum write burst size
15789 * to expose the 5700/5701 write DMA bug.
15790 */
15791 saved_dma_rwctrl = tp->dma_rwctrl;
15792 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
15793 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
15794
Linus Torvalds1da177e2005-04-16 15:20:36 -070015795 while (1) {
15796 u32 *p = buf, i;
15797
15798 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++)
15799 p[i] = i;
15800
15801 /* Send the buffer to the chip. */
15802 ret = tg3_do_test_dma(tp, buf, buf_dma, TEST_BUFFER_SIZE, 1);
15803 if (ret) {
Matt Carlson2445e462010-04-05 10:19:21 +000015804 dev_err(&tp->pdev->dev,
15805 "%s: Buffer write failed. err = %d\n",
15806 __func__, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015807 break;
15808 }
15809
15810#if 0
15811 /* validate data reached card RAM correctly. */
15812 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++) {
15813 u32 val;
15814 tg3_read_mem(tp, 0x2100 + (i*4), &val);
15815 if (le32_to_cpu(val) != p[i]) {
Matt Carlson2445e462010-04-05 10:19:21 +000015816 dev_err(&tp->pdev->dev,
15817 "%s: Buffer corrupted on device! "
15818 "(%d != %d)\n", __func__, val, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015819 /* ret = -ENODEV here? */
15820 }
15821 p[i] = 0;
15822 }
15823#endif
15824 /* Now read it back. */
15825 ret = tg3_do_test_dma(tp, buf, buf_dma, TEST_BUFFER_SIZE, 0);
15826 if (ret) {
Matt Carlson5129c3a2010-04-05 10:19:23 +000015827 dev_err(&tp->pdev->dev, "%s: Buffer read failed. "
15828 "err = %d\n", __func__, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015829 break;
15830 }
15831
15832 /* Verify it. */
15833 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++) {
15834 if (p[i] == i)
15835 continue;
15836
David S. Miller59e6b432005-05-18 22:50:10 -070015837 if ((tp->dma_rwctrl & DMA_RWCTRL_WRITE_BNDRY_MASK) !=
15838 DMA_RWCTRL_WRITE_BNDRY_16) {
15839 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015840 tp->dma_rwctrl |= DMA_RWCTRL_WRITE_BNDRY_16;
15841 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
15842 break;
15843 } else {
Matt Carlson2445e462010-04-05 10:19:21 +000015844 dev_err(&tp->pdev->dev,
15845 "%s: Buffer corrupted on read back! "
15846 "(%d != %d)\n", __func__, p[i], i);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015847 ret = -ENODEV;
15848 goto out;
15849 }
15850 }
15851
15852 if (i == (TEST_BUFFER_SIZE / sizeof(u32))) {
15853 /* Success. */
15854 ret = 0;
15855 break;
15856 }
15857 }
David S. Miller59e6b432005-05-18 22:50:10 -070015858 if ((tp->dma_rwctrl & DMA_RWCTRL_WRITE_BNDRY_MASK) !=
15859 DMA_RWCTRL_WRITE_BNDRY_16) {
15860 /* DMA test passed without adjusting DMA boundary,
Michael Chan6d1cfba2005-06-08 14:13:14 -070015861 * now look for chipsets that are known to expose the
15862 * DMA bug without failing the test.
David S. Miller59e6b432005-05-18 22:50:10 -070015863 */
Matt Carlson41434702011-03-09 16:58:22 +000015864 if (pci_dev_present(tg3_dma_wait_state_chipsets)) {
Michael Chan6d1cfba2005-06-08 14:13:14 -070015865 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
15866 tp->dma_rwctrl |= DMA_RWCTRL_WRITE_BNDRY_16;
Matt Carlson859a588792010-04-05 10:19:28 +000015867 } else {
Michael Chan6d1cfba2005-06-08 14:13:14 -070015868 /* Safe to use the calculated DMA boundary. */
15869 tp->dma_rwctrl = saved_dma_rwctrl;
Matt Carlson859a588792010-04-05 10:19:28 +000015870 }
Michael Chan6d1cfba2005-06-08 14:13:14 -070015871
David S. Miller59e6b432005-05-18 22:50:10 -070015872 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
15873 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015874
15875out:
Matt Carlson4bae65c2010-11-24 08:31:52 +000015876 dma_free_coherent(&tp->pdev->dev, TEST_BUFFER_SIZE, buf, buf_dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015877out_nofree:
15878 return ret;
15879}
15880
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015881static void tg3_init_bufmgr_config(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015882{
Joe Perches63c3a662011-04-26 08:12:10 +000015883 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlson666bc832010-01-20 16:58:03 +000015884 tp->bufmgr_config.mbuf_read_dma_low_water =
15885 DEFAULT_MB_RDMA_LOW_WATER_5705;
15886 tp->bufmgr_config.mbuf_mac_rx_low_water =
15887 DEFAULT_MB_MACRX_LOW_WATER_57765;
15888 tp->bufmgr_config.mbuf_high_water =
15889 DEFAULT_MB_HIGH_WATER_57765;
15890
15891 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
15892 DEFAULT_MB_RDMA_LOW_WATER_5705;
15893 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
15894 DEFAULT_MB_MACRX_LOW_WATER_JUMBO_57765;
15895 tp->bufmgr_config.mbuf_high_water_jumbo =
15896 DEFAULT_MB_HIGH_WATER_JUMBO_57765;
Joe Perches63c3a662011-04-26 08:12:10 +000015897 } else if (tg3_flag(tp, 5705_PLUS)) {
Michael Chanfdfec1722005-07-25 12:31:48 -070015898 tp->bufmgr_config.mbuf_read_dma_low_water =
15899 DEFAULT_MB_RDMA_LOW_WATER_5705;
15900 tp->bufmgr_config.mbuf_mac_rx_low_water =
15901 DEFAULT_MB_MACRX_LOW_WATER_5705;
15902 tp->bufmgr_config.mbuf_high_water =
15903 DEFAULT_MB_HIGH_WATER_5705;
Michael Chanb5d37722006-09-27 16:06:21 -070015904 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
15905 tp->bufmgr_config.mbuf_mac_rx_low_water =
15906 DEFAULT_MB_MACRX_LOW_WATER_5906;
15907 tp->bufmgr_config.mbuf_high_water =
15908 DEFAULT_MB_HIGH_WATER_5906;
15909 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015910
Michael Chanfdfec1722005-07-25 12:31:48 -070015911 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
15912 DEFAULT_MB_RDMA_LOW_WATER_JUMBO_5780;
15913 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
15914 DEFAULT_MB_MACRX_LOW_WATER_JUMBO_5780;
15915 tp->bufmgr_config.mbuf_high_water_jumbo =
15916 DEFAULT_MB_HIGH_WATER_JUMBO_5780;
15917 } else {
15918 tp->bufmgr_config.mbuf_read_dma_low_water =
15919 DEFAULT_MB_RDMA_LOW_WATER;
15920 tp->bufmgr_config.mbuf_mac_rx_low_water =
15921 DEFAULT_MB_MACRX_LOW_WATER;
15922 tp->bufmgr_config.mbuf_high_water =
15923 DEFAULT_MB_HIGH_WATER;
15924
15925 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
15926 DEFAULT_MB_RDMA_LOW_WATER_JUMBO;
15927 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
15928 DEFAULT_MB_MACRX_LOW_WATER_JUMBO;
15929 tp->bufmgr_config.mbuf_high_water_jumbo =
15930 DEFAULT_MB_HIGH_WATER_JUMBO;
15931 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015932
15933 tp->bufmgr_config.dma_low_water = DEFAULT_DMA_LOW_WATER;
15934 tp->bufmgr_config.dma_high_water = DEFAULT_DMA_HIGH_WATER;
15935}
15936
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015937static char *tg3_phy_string(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015938{
Matt Carlson79eb6902010-02-17 15:17:03 +000015939 switch (tp->phy_id & TG3_PHY_ID_MASK) {
15940 case TG3_PHY_ID_BCM5400: return "5400";
15941 case TG3_PHY_ID_BCM5401: return "5401";
15942 case TG3_PHY_ID_BCM5411: return "5411";
15943 case TG3_PHY_ID_BCM5701: return "5701";
15944 case TG3_PHY_ID_BCM5703: return "5703";
15945 case TG3_PHY_ID_BCM5704: return "5704";
15946 case TG3_PHY_ID_BCM5705: return "5705";
15947 case TG3_PHY_ID_BCM5750: return "5750";
15948 case TG3_PHY_ID_BCM5752: return "5752";
15949 case TG3_PHY_ID_BCM5714: return "5714";
15950 case TG3_PHY_ID_BCM5780: return "5780";
15951 case TG3_PHY_ID_BCM5755: return "5755";
15952 case TG3_PHY_ID_BCM5787: return "5787";
15953 case TG3_PHY_ID_BCM5784: return "5784";
15954 case TG3_PHY_ID_BCM5756: return "5722/5756";
15955 case TG3_PHY_ID_BCM5906: return "5906";
15956 case TG3_PHY_ID_BCM5761: return "5761";
15957 case TG3_PHY_ID_BCM5718C: return "5718C";
15958 case TG3_PHY_ID_BCM5718S: return "5718S";
15959 case TG3_PHY_ID_BCM57765: return "57765";
Matt Carlson302b5002010-06-05 17:24:38 +000015960 case TG3_PHY_ID_BCM5719C: return "5719C";
Matt Carlson6418f2c2011-04-05 14:22:49 +000015961 case TG3_PHY_ID_BCM5720C: return "5720C";
Matt Carlson79eb6902010-02-17 15:17:03 +000015962 case TG3_PHY_ID_BCM8002: return "8002/serdes";
Linus Torvalds1da177e2005-04-16 15:20:36 -070015963 case 0: return "serdes";
15964 default: return "unknown";
Stephen Hemminger855e1112008-04-16 16:37:28 -070015965 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015966}
15967
Bill Pemberton229b1ad2012-12-03 09:22:59 -050015968static char *tg3_bus_string(struct tg3 *tp, char *str)
Michael Chanf9804dd2005-09-27 12:13:10 -070015969{
Joe Perches63c3a662011-04-26 08:12:10 +000015970 if (tg3_flag(tp, PCI_EXPRESS)) {
Michael Chanf9804dd2005-09-27 12:13:10 -070015971 strcpy(str, "PCI Express");
15972 return str;
Joe Perches63c3a662011-04-26 08:12:10 +000015973 } else if (tg3_flag(tp, PCIX_MODE)) {
Michael Chanf9804dd2005-09-27 12:13:10 -070015974 u32 clock_ctrl = tr32(TG3PCI_CLOCK_CTRL) & 0x1f;
15975
15976 strcpy(str, "PCIX:");
15977
15978 if ((clock_ctrl == 7) ||
15979 ((tr32(GRC_MISC_CFG) & GRC_MISC_CFG_BOARD_ID_MASK) ==
15980 GRC_MISC_CFG_BOARD_ID_5704CIOBE))
15981 strcat(str, "133MHz");
15982 else if (clock_ctrl == 0)
15983 strcat(str, "33MHz");
15984 else if (clock_ctrl == 2)
15985 strcat(str, "50MHz");
15986 else if (clock_ctrl == 4)
15987 strcat(str, "66MHz");
15988 else if (clock_ctrl == 6)
15989 strcat(str, "100MHz");
Michael Chanf9804dd2005-09-27 12:13:10 -070015990 } else {
15991 strcpy(str, "PCI:");
Joe Perches63c3a662011-04-26 08:12:10 +000015992 if (tg3_flag(tp, PCI_HIGH_SPEED))
Michael Chanf9804dd2005-09-27 12:13:10 -070015993 strcat(str, "66MHz");
15994 else
15995 strcat(str, "33MHz");
15996 }
Joe Perches63c3a662011-04-26 08:12:10 +000015997 if (tg3_flag(tp, PCI_32BIT))
Michael Chanf9804dd2005-09-27 12:13:10 -070015998 strcat(str, ":32-bit");
15999 else
16000 strcat(str, ":64-bit");
16001 return str;
16002}
16003
Bill Pemberton229b1ad2012-12-03 09:22:59 -050016004static void tg3_init_coal(struct tg3 *tp)
David S. Miller15f98502005-05-18 22:49:26 -070016005{
16006 struct ethtool_coalesce *ec = &tp->coal;
16007
16008 memset(ec, 0, sizeof(*ec));
16009 ec->cmd = ETHTOOL_GCOALESCE;
16010 ec->rx_coalesce_usecs = LOW_RXCOL_TICKS;
16011 ec->tx_coalesce_usecs = LOW_TXCOL_TICKS;
16012 ec->rx_max_coalesced_frames = LOW_RXMAX_FRAMES;
16013 ec->tx_max_coalesced_frames = LOW_TXMAX_FRAMES;
16014 ec->rx_coalesce_usecs_irq = DEFAULT_RXCOAL_TICK_INT;
16015 ec->tx_coalesce_usecs_irq = DEFAULT_TXCOAL_TICK_INT;
16016 ec->rx_max_coalesced_frames_irq = DEFAULT_RXCOAL_MAXF_INT;
16017 ec->tx_max_coalesced_frames_irq = DEFAULT_TXCOAL_MAXF_INT;
16018 ec->stats_block_coalesce_usecs = DEFAULT_STAT_COAL_TICKS;
16019
16020 if (tp->coalesce_mode & (HOSTCC_MODE_CLRTICK_RXBD |
16021 HOSTCC_MODE_CLRTICK_TXBD)) {
16022 ec->rx_coalesce_usecs = LOW_RXCOL_TICKS_CLRTCKS;
16023 ec->rx_coalesce_usecs_irq = DEFAULT_RXCOAL_TICK_INT_CLRTCKS;
16024 ec->tx_coalesce_usecs = LOW_TXCOL_TICKS_CLRTCKS;
16025 ec->tx_coalesce_usecs_irq = DEFAULT_TXCOAL_TICK_INT_CLRTCKS;
16026 }
Michael Chand244c892005-07-05 14:42:33 -070016027
Joe Perches63c3a662011-04-26 08:12:10 +000016028 if (tg3_flag(tp, 5705_PLUS)) {
Michael Chand244c892005-07-05 14:42:33 -070016029 ec->rx_coalesce_usecs_irq = 0;
16030 ec->tx_coalesce_usecs_irq = 0;
16031 ec->stats_block_coalesce_usecs = 0;
16032 }
David S. Miller15f98502005-05-18 22:49:26 -070016033}
16034
Bill Pemberton229b1ad2012-12-03 09:22:59 -050016035static int tg3_init_one(struct pci_dev *pdev,
Linus Torvalds1da177e2005-04-16 15:20:36 -070016036 const struct pci_device_id *ent)
16037{
Linus Torvalds1da177e2005-04-16 15:20:36 -070016038 struct net_device *dev;
16039 struct tg3 *tp;
Matt Carlson646c9ed2009-09-01 12:58:41 +000016040 int i, err, pm_cap;
16041 u32 sndmbx, rcvmbx, intmbx;
Michael Chanf9804dd2005-09-27 12:13:10 -070016042 char str[40];
Michael Chan72f2afb2006-03-06 19:28:35 -080016043 u64 dma_mask, persist_dma_mask;
Michał Mirosławc8f44af2011-11-15 15:29:55 +000016044 netdev_features_t features = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016045
Joe Perches05dbe002010-02-17 19:44:19 +000016046 printk_once(KERN_INFO "%s\n", version);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016047
16048 err = pci_enable_device(pdev);
16049 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000016050 dev_err(&pdev->dev, "Cannot enable PCI device, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070016051 return err;
16052 }
16053
Linus Torvalds1da177e2005-04-16 15:20:36 -070016054 err = pci_request_regions(pdev, DRV_MODULE_NAME);
16055 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000016056 dev_err(&pdev->dev, "Cannot obtain PCI resources, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070016057 goto err_out_disable_pdev;
16058 }
16059
16060 pci_set_master(pdev);
16061
16062 /* Find power-management capability. */
16063 pm_cap = pci_find_capability(pdev, PCI_CAP_ID_PM);
16064 if (pm_cap == 0) {
Matt Carlson2445e462010-04-05 10:19:21 +000016065 dev_err(&pdev->dev,
16066 "Cannot find Power Management capability, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070016067 err = -EIO;
16068 goto err_out_free_res;
16069 }
16070
Matt Carlson16821282011-07-13 09:27:28 +000016071 err = pci_set_power_state(pdev, PCI_D0);
16072 if (err) {
16073 dev_err(&pdev->dev, "Transition to D0 failed, aborting\n");
16074 goto err_out_free_res;
16075 }
16076
Matt Carlsonfe5f5782009-09-01 13:09:39 +000016077 dev = alloc_etherdev_mq(sizeof(*tp), TG3_IRQ_MAX_VECS);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016078 if (!dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070016079 err = -ENOMEM;
Matt Carlson16821282011-07-13 09:27:28 +000016080 goto err_out_power_down;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016081 }
16082
Linus Torvalds1da177e2005-04-16 15:20:36 -070016083 SET_NETDEV_DEV(dev, &pdev->dev);
16084
Linus Torvalds1da177e2005-04-16 15:20:36 -070016085 tp = netdev_priv(dev);
16086 tp->pdev = pdev;
16087 tp->dev = dev;
16088 tp->pm_cap = pm_cap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016089 tp->rx_mode = TG3_DEF_RX_MODE;
16090 tp->tx_mode = TG3_DEF_TX_MODE;
Matt Carlson8ef21422008-05-02 16:47:53 -070016091
Linus Torvalds1da177e2005-04-16 15:20:36 -070016092 if (tg3_debug > 0)
16093 tp->msg_enable = tg3_debug;
16094 else
16095 tp->msg_enable = TG3_DEF_MSG_ENABLE;
16096
16097 /* The word/byte swap controls here control register access byte
16098 * swapping. DMA data byte swapping is controlled in the GRC_MODE
16099 * setting below.
16100 */
16101 tp->misc_host_ctrl =
16102 MISC_HOST_CTRL_MASK_PCI_INT |
16103 MISC_HOST_CTRL_WORD_SWAP |
16104 MISC_HOST_CTRL_INDIR_ACCESS |
16105 MISC_HOST_CTRL_PCISTATE_RW;
16106
16107 /* The NONFRM (non-frame) byte/word swap controls take effect
16108 * on descriptor entries, anything which isn't packet data.
16109 *
16110 * The StrongARM chips on the board (one for tx, one for rx)
16111 * are running in big-endian mode.
16112 */
16113 tp->grc_mode = (GRC_MODE_WSWAP_DATA | GRC_MODE_BSWAP_DATA |
16114 GRC_MODE_WSWAP_NONFRM_DATA);
16115#ifdef __BIG_ENDIAN
16116 tp->grc_mode |= GRC_MODE_BSWAP_NONFRM_DATA;
16117#endif
16118 spin_lock_init(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016119 spin_lock_init(&tp->indirect_lock);
David Howellsc4028952006-11-22 14:57:56 +000016120 INIT_WORK(&tp->reset_task, tg3_reset_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016121
Matt Carlsond5fe4882008-11-21 17:20:32 -080016122 tp->regs = pci_ioremap_bar(pdev, BAR_0);
Andy Gospodarekab0049b2007-09-06 20:42:14 +010016123 if (!tp->regs) {
Matt Carlsonab96b242010-04-05 10:19:22 +000016124 dev_err(&pdev->dev, "Cannot map device registers, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070016125 err = -ENOMEM;
16126 goto err_out_free_dev;
16127 }
16128
Matt Carlsonc9cab242011-07-13 09:27:27 +000016129 if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761 ||
16130 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761E ||
16131 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761S ||
16132 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761SE ||
16133 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717 ||
Michael Chan79d49692012-11-05 14:26:29 +000016134 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717_C ||
Matt Carlsonc9cab242011-07-13 09:27:27 +000016135 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718 ||
16136 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5719 ||
16137 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5720) {
16138 tg3_flag_set(tp, ENABLE_APE);
16139 tp->aperegs = pci_ioremap_bar(pdev, BAR_2);
16140 if (!tp->aperegs) {
16141 dev_err(&pdev->dev,
16142 "Cannot map APE registers, aborting\n");
16143 err = -ENOMEM;
16144 goto err_out_iounmap;
16145 }
16146 }
16147
Linus Torvalds1da177e2005-04-16 15:20:36 -070016148 tp->rx_pending = TG3_DEF_RX_RING_PENDING;
16149 tp->rx_jumbo_pending = TG3_DEF_RX_JUMBO_RING_PENDING;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016150
Linus Torvalds1da177e2005-04-16 15:20:36 -070016151 dev->ethtool_ops = &tg3_ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016152 dev->watchdog_timeo = TG3_TX_TIMEOUT;
Matt Carlson2ffcc982011-05-19 12:12:44 +000016153 dev->netdev_ops = &tg3_netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016154 dev->irq = pdev->irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016155
Nithin Nayak Sujir3d567e02012-11-14 14:44:26 +000016156 err = tg3_get_invariants(tp, ent);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016157 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000016158 dev_err(&pdev->dev,
16159 "Problem fetching invariants of chip, aborting\n");
Matt Carlsonc9cab242011-07-13 09:27:27 +000016160 goto err_out_apeunmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016161 }
16162
Michael Chan4a29cc22006-03-19 13:21:12 -080016163 /* The EPB bridge inside 5714, 5715, and 5780 and any
16164 * device behind the EPB cannot support DMA addresses > 40-bit.
Michael Chan72f2afb2006-03-06 19:28:35 -080016165 * On 64-bit systems with IOMMU, use 40-bit dma_mask.
16166 * On 64-bit systems without IOMMU, use 64-bit dma_mask and
16167 * do DMA address check in tg3_start_xmit().
16168 */
Joe Perches63c3a662011-04-26 08:12:10 +000016169 if (tg3_flag(tp, IS_5788))
Yang Hongyang284901a2009-04-06 19:01:15 -070016170 persist_dma_mask = dma_mask = DMA_BIT_MASK(32);
Joe Perches63c3a662011-04-26 08:12:10 +000016171 else if (tg3_flag(tp, 40BIT_DMA_BUG)) {
Yang Hongyang50cf1562009-04-06 19:01:14 -070016172 persist_dma_mask = dma_mask = DMA_BIT_MASK(40);
Michael Chan72f2afb2006-03-06 19:28:35 -080016173#ifdef CONFIG_HIGHMEM
Yang Hongyang6a355282009-04-06 19:01:13 -070016174 dma_mask = DMA_BIT_MASK(64);
Michael Chan72f2afb2006-03-06 19:28:35 -080016175#endif
Michael Chan4a29cc22006-03-19 13:21:12 -080016176 } else
Yang Hongyang6a355282009-04-06 19:01:13 -070016177 persist_dma_mask = dma_mask = DMA_BIT_MASK(64);
Michael Chan72f2afb2006-03-06 19:28:35 -080016178
16179 /* Configure DMA attributes. */
Yang Hongyang284901a2009-04-06 19:01:15 -070016180 if (dma_mask > DMA_BIT_MASK(32)) {
Michael Chan72f2afb2006-03-06 19:28:35 -080016181 err = pci_set_dma_mask(pdev, dma_mask);
16182 if (!err) {
Matt Carlson0da06062011-05-19 12:12:53 +000016183 features |= NETIF_F_HIGHDMA;
Michael Chan72f2afb2006-03-06 19:28:35 -080016184 err = pci_set_consistent_dma_mask(pdev,
16185 persist_dma_mask);
16186 if (err < 0) {
Matt Carlsonab96b242010-04-05 10:19:22 +000016187 dev_err(&pdev->dev, "Unable to obtain 64 bit "
16188 "DMA for consistent allocations\n");
Matt Carlsonc9cab242011-07-13 09:27:27 +000016189 goto err_out_apeunmap;
Michael Chan72f2afb2006-03-06 19:28:35 -080016190 }
16191 }
16192 }
Yang Hongyang284901a2009-04-06 19:01:15 -070016193 if (err || dma_mask == DMA_BIT_MASK(32)) {
16194 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
Michael Chan72f2afb2006-03-06 19:28:35 -080016195 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000016196 dev_err(&pdev->dev,
16197 "No usable DMA configuration, aborting\n");
Matt Carlsonc9cab242011-07-13 09:27:27 +000016198 goto err_out_apeunmap;
Michael Chan72f2afb2006-03-06 19:28:35 -080016199 }
16200 }
16201
Michael Chanfdfec1722005-07-25 12:31:48 -070016202 tg3_init_bufmgr_config(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016203
Matt Carlson0da06062011-05-19 12:12:53 +000016204 features |= NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
16205
16206 /* 5700 B0 chips do not support checksumming correctly due
16207 * to hardware bugs.
16208 */
16209 if (tp->pci_chip_rev_id != CHIPREV_ID_5700_B0) {
16210 features |= NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_RXCSUM;
16211
16212 if (tg3_flag(tp, 5755_PLUS))
16213 features |= NETIF_F_IPV6_CSUM;
16214 }
16215
Michael Chan4e3a7aa2006-03-20 17:47:44 -080016216 /* TSO is on by default on chips that support hardware TSO.
16217 * Firmware TSO on older chips gives lower performance, so it
16218 * is off by default, but can be enabled using ethtool.
16219 */
Joe Perches63c3a662011-04-26 08:12:10 +000016220 if ((tg3_flag(tp, HW_TSO_1) ||
16221 tg3_flag(tp, HW_TSO_2) ||
16222 tg3_flag(tp, HW_TSO_3)) &&
Matt Carlson0da06062011-05-19 12:12:53 +000016223 (features & NETIF_F_IP_CSUM))
16224 features |= NETIF_F_TSO;
Joe Perches63c3a662011-04-26 08:12:10 +000016225 if (tg3_flag(tp, HW_TSO_2) || tg3_flag(tp, HW_TSO_3)) {
Matt Carlson0da06062011-05-19 12:12:53 +000016226 if (features & NETIF_F_IPV6_CSUM)
16227 features |= NETIF_F_TSO6;
Joe Perches63c3a662011-04-26 08:12:10 +000016228 if (tg3_flag(tp, HW_TSO_3) ||
Matt Carlsone849cdc2009-11-13 13:03:38 +000016229 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson57e69832008-05-25 23:48:31 -070016230 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
16231 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX) ||
Joe Perches63c3a662011-04-26 08:12:10 +000016232 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Michał Mirosławdc668912011-04-07 03:35:07 +000016233 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
Matt Carlson0da06062011-05-19 12:12:53 +000016234 features |= NETIF_F_TSO_ECN;
Michael Chanb0026622006-07-03 19:42:14 -070016235 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070016236
Matt Carlsond542fe22011-05-19 16:02:43 +000016237 dev->features |= features;
16238 dev->vlan_features |= features;
16239
Mahesh Bandewar06c03c02011-05-08 06:51:48 +000016240 /*
16241 * Add loopback capability only for a subset of devices that support
16242 * MAC-LOOPBACK. Eventually this need to be enhanced to allow INT-PHY
16243 * loopback for the remaining devices.
16244 */
16245 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5780 &&
16246 !tg3_flag(tp, CPMU_PRESENT))
16247 /* Add the loopback capability */
Matt Carlson0da06062011-05-19 12:12:53 +000016248 features |= NETIF_F_LOOPBACK;
16249
Matt Carlson0da06062011-05-19 12:12:53 +000016250 dev->hw_features |= features;
Mahesh Bandewar06c03c02011-05-08 06:51:48 +000016251
Linus Torvalds1da177e2005-04-16 15:20:36 -070016252 if (tp->pci_chip_rev_id == CHIPREV_ID_5705_A1 &&
Joe Perches63c3a662011-04-26 08:12:10 +000016253 !tg3_flag(tp, TSO_CAPABLE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070016254 !(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH)) {
Joe Perches63c3a662011-04-26 08:12:10 +000016255 tg3_flag_set(tp, MAX_RXPEND_64);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016256 tp->rx_pending = 63;
16257 }
16258
Linus Torvalds1da177e2005-04-16 15:20:36 -070016259 err = tg3_get_device_address(tp);
16260 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000016261 dev_err(&pdev->dev,
16262 "Could not obtain valid ethernet address, aborting\n");
Matt Carlsonc9cab242011-07-13 09:27:27 +000016263 goto err_out_apeunmap;
Matt Carlson0d3031d2007-10-10 18:02:43 -070016264 }
16265
Matt Carlsonc88864d2007-11-12 21:07:01 -080016266 /*
16267 * Reset chip in case UNDI or EFI driver did not shutdown
16268 * DMA self test will enable WDMAC and we'll see (spurious)
16269 * pending DMA on the PCI bus at that point.
16270 */
16271 if ((tr32(HOSTCC_MODE) & HOSTCC_MODE_ENABLE) ||
16272 (tr32(WDMAC_MODE) & WDMAC_MODE_ENABLE)) {
16273 tw32(MEMARB_MODE, MEMARB_MODE_ENABLE);
16274 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
16275 }
16276
16277 err = tg3_test_dma(tp);
16278 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000016279 dev_err(&pdev->dev, "DMA engine test failed, aborting\n");
Matt Carlsonc88864d2007-11-12 21:07:01 -080016280 goto err_out_apeunmap;
16281 }
16282
Matt Carlson78f90dc2009-11-13 13:03:42 +000016283 intmbx = MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW;
16284 rcvmbx = MAILBOX_RCVRET_CON_IDX_0 + TG3_64BIT_REG_LOW;
16285 sndmbx = MAILBOX_SNDHOST_PROD_IDX_0 + TG3_64BIT_REG_LOW;
Matt Carlson6fd45cb2010-09-15 08:59:57 +000016286 for (i = 0; i < tp->irq_max; i++) {
Matt Carlson78f90dc2009-11-13 13:03:42 +000016287 struct tg3_napi *tnapi = &tp->napi[i];
16288
16289 tnapi->tp = tp;
16290 tnapi->tx_pending = TG3_DEF_TX_RING_PENDING;
16291
16292 tnapi->int_mbox = intmbx;
Matt Carlson93a700a2011-08-31 11:44:54 +000016293 if (i <= 4)
Matt Carlson78f90dc2009-11-13 13:03:42 +000016294 intmbx += 0x8;
16295 else
16296 intmbx += 0x4;
16297
16298 tnapi->consmbox = rcvmbx;
16299 tnapi->prodmbox = sndmbx;
16300
Matt Carlson66cfd1b2010-09-30 10:34:30 +000016301 if (i)
Matt Carlson78f90dc2009-11-13 13:03:42 +000016302 tnapi->coal_now = HOSTCC_MODE_COAL_VEC1_NOW << (i - 1);
Matt Carlson66cfd1b2010-09-30 10:34:30 +000016303 else
Matt Carlson78f90dc2009-11-13 13:03:42 +000016304 tnapi->coal_now = HOSTCC_MODE_NOW;
Matt Carlson78f90dc2009-11-13 13:03:42 +000016305
Joe Perches63c3a662011-04-26 08:12:10 +000016306 if (!tg3_flag(tp, SUPPORT_MSIX))
Matt Carlson78f90dc2009-11-13 13:03:42 +000016307 break;
16308
16309 /*
16310 * If we support MSIX, we'll be using RSS. If we're using
16311 * RSS, the first vector only handles link interrupts and the
16312 * remaining vectors handle rx and tx interrupts. Reuse the
16313 * mailbox values for the next iteration. The values we setup
16314 * above are still useful for the single vectored mode.
16315 */
16316 if (!i)
16317 continue;
16318
16319 rcvmbx += 0x8;
16320
16321 if (sndmbx & 0x4)
16322 sndmbx -= 0x4;
16323 else
16324 sndmbx += 0xc;
16325 }
16326
Matt Carlsonc88864d2007-11-12 21:07:01 -080016327 tg3_init_coal(tp);
16328
Michael Chanc49a1562006-12-17 17:07:29 -080016329 pci_set_drvdata(pdev, dev);
16330
Matt Carlsoncd0d7222011-07-13 09:27:33 +000016331 if (tg3_flag(tp, 5717_PLUS)) {
16332 /* Resume a low-power mode */
16333 tg3_frob_aux_power(tp, false);
16334 }
16335
Matt Carlson21f76382012-02-22 12:35:21 +000016336 tg3_timer_init(tp);
16337
Linus Torvalds1da177e2005-04-16 15:20:36 -070016338 err = register_netdev(dev);
16339 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000016340 dev_err(&pdev->dev, "Cannot register net device, aborting\n");
Matt Carlson0d3031d2007-10-10 18:02:43 -070016341 goto err_out_apeunmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016342 }
16343
Joe Perches05dbe002010-02-17 19:44:19 +000016344 netdev_info(dev, "Tigon3 [partno(%s) rev %04x] (%s) MAC address %pM\n",
16345 tp->board_part_number,
16346 tp->pci_chip_rev_id,
16347 tg3_bus_string(tp, str),
16348 dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016349
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016350 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000016351 struct phy_device *phydev;
16352 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlson5129c3a2010-04-05 10:19:23 +000016353 netdev_info(dev,
16354 "attached PHY driver [%s] (mii_bus:phy_addr=%s)\n",
Joe Perches05dbe002010-02-17 19:44:19 +000016355 phydev->drv->name, dev_name(&phydev->dev));
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016356 } else {
16357 char *ethtype;
16358
16359 if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
16360 ethtype = "10/100Base-TX";
16361 else if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
16362 ethtype = "1000Base-SX";
16363 else
16364 ethtype = "10/100/1000Base-T";
16365
Matt Carlson5129c3a2010-04-05 10:19:23 +000016366 netdev_info(dev, "attached PHY is %s (%s Ethernet) "
Matt Carlson47007832011-04-20 07:57:43 +000016367 "(WireSpeed[%d], EEE[%d])\n",
16368 tg3_phy_string(tp), ethtype,
16369 (tp->phy_flags & TG3_PHYFLG_NO_ETH_WIRE_SPEED) == 0,
16370 (tp->phy_flags & TG3_PHYFLG_EEE_CAP) != 0);
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016371 }
Matt Carlsondf59c942008-11-03 16:52:56 -080016372
Joe Perches05dbe002010-02-17 19:44:19 +000016373 netdev_info(dev, "RXcsums[%d] LinkChgREG[%d] MIirq[%d] ASF[%d] TSOcap[%d]\n",
Michał Mirosławdc668912011-04-07 03:35:07 +000016374 (dev->features & NETIF_F_RXCSUM) != 0,
Joe Perches63c3a662011-04-26 08:12:10 +000016375 tg3_flag(tp, USE_LINKCHG_REG) != 0,
Matt Carlsonf07e9af2010-08-02 11:26:07 +000016376 (tp->phy_flags & TG3_PHYFLG_USE_MI_INTERRUPT) != 0,
Joe Perches63c3a662011-04-26 08:12:10 +000016377 tg3_flag(tp, ENABLE_ASF) != 0,
16378 tg3_flag(tp, TSO_CAPABLE) != 0);
Joe Perches05dbe002010-02-17 19:44:19 +000016379 netdev_info(dev, "dma_rwctrl[%08x] dma_mask[%d-bit]\n",
16380 tp->dma_rwctrl,
16381 pdev->dma_mask == DMA_BIT_MASK(32) ? 32 :
16382 ((u64)pdev->dma_mask) == DMA_BIT_MASK(40) ? 40 : 64);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016383
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000016384 pci_save_state(pdev);
16385
Linus Torvalds1da177e2005-04-16 15:20:36 -070016386 return 0;
16387
Matt Carlson0d3031d2007-10-10 18:02:43 -070016388err_out_apeunmap:
16389 if (tp->aperegs) {
16390 iounmap(tp->aperegs);
16391 tp->aperegs = NULL;
16392 }
16393
Linus Torvalds1da177e2005-04-16 15:20:36 -070016394err_out_iounmap:
Michael Chan68929142005-08-09 20:17:14 -070016395 if (tp->regs) {
16396 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070016397 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070016398 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070016399
16400err_out_free_dev:
16401 free_netdev(dev);
16402
Matt Carlson16821282011-07-13 09:27:28 +000016403err_out_power_down:
16404 pci_set_power_state(pdev, PCI_D3hot);
16405
Linus Torvalds1da177e2005-04-16 15:20:36 -070016406err_out_free_res:
16407 pci_release_regions(pdev);
16408
16409err_out_disable_pdev:
16410 pci_disable_device(pdev);
16411 pci_set_drvdata(pdev, NULL);
16412 return err;
16413}
16414
Bill Pemberton229b1ad2012-12-03 09:22:59 -050016415static void tg3_remove_one(struct pci_dev *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070016416{
16417 struct net_device *dev = pci_get_drvdata(pdev);
16418
16419 if (dev) {
16420 struct tg3 *tp = netdev_priv(dev);
16421
Jesper Juhle3c55302012-04-09 22:50:15 +020016422 release_firmware(tp->fw);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -080016423
Matt Carlsondb219972011-11-04 09:15:03 +000016424 tg3_reset_task_cancel(tp);
Matt Carlson158d7ab2008-05-29 01:37:54 -070016425
David S. Miller1805b2f2011-10-24 18:18:09 -040016426 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070016427 tg3_phy_fini(tp);
Matt Carlson158d7ab2008-05-29 01:37:54 -070016428 tg3_mdio_fini(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070016429 }
Matt Carlson158d7ab2008-05-29 01:37:54 -070016430
Linus Torvalds1da177e2005-04-16 15:20:36 -070016431 unregister_netdev(dev);
Matt Carlson0d3031d2007-10-10 18:02:43 -070016432 if (tp->aperegs) {
16433 iounmap(tp->aperegs);
16434 tp->aperegs = NULL;
16435 }
Michael Chan68929142005-08-09 20:17:14 -070016436 if (tp->regs) {
16437 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070016438 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070016439 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070016440 free_netdev(dev);
16441 pci_release_regions(pdev);
16442 pci_disable_device(pdev);
16443 pci_set_drvdata(pdev, NULL);
16444 }
16445}
16446
Eric Dumazetaa6027c2011-01-01 05:22:46 +000016447#ifdef CONFIG_PM_SLEEP
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000016448static int tg3_suspend(struct device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070016449{
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000016450 struct pci_dev *pdev = to_pci_dev(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016451 struct net_device *dev = pci_get_drvdata(pdev);
16452 struct tg3 *tp = netdev_priv(dev);
16453 int err;
16454
16455 if (!netif_running(dev))
16456 return 0;
16457
Matt Carlsondb219972011-11-04 09:15:03 +000016458 tg3_reset_task_cancel(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070016459 tg3_phy_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016460 tg3_netif_stop(tp);
16461
Matt Carlson21f76382012-02-22 12:35:21 +000016462 tg3_timer_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016463
David S. Millerf47c11e2005-06-24 20:18:35 -070016464 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016465 tg3_disable_ints(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -070016466 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016467
16468 netif_device_detach(dev);
16469
David S. Millerf47c11e2005-06-24 20:18:35 -070016470 tg3_full_lock(tp, 0);
Michael Chan944d9802005-05-29 14:57:48 -070016471 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Joe Perches63c3a662011-04-26 08:12:10 +000016472 tg3_flag_clear(tp, INIT_COMPLETE);
David S. Millerf47c11e2005-06-24 20:18:35 -070016473 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016474
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000016475 err = tg3_power_down_prepare(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016476 if (err) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070016477 int err2;
16478
David S. Millerf47c11e2005-06-24 20:18:35 -070016479 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016480
Joe Perches63c3a662011-04-26 08:12:10 +000016481 tg3_flag_set(tp, INIT_COMPLETE);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070016482 err2 = tg3_restart_hw(tp, 1);
16483 if (err2)
Michael Chanb9ec6c12006-07-25 16:37:27 -070016484 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016485
Matt Carlson21f76382012-02-22 12:35:21 +000016486 tg3_timer_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016487
16488 netif_device_attach(dev);
16489 tg3_netif_start(tp);
16490
Michael Chanb9ec6c12006-07-25 16:37:27 -070016491out:
David S. Millerf47c11e2005-06-24 20:18:35 -070016492 tg3_full_unlock(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070016493
16494 if (!err2)
16495 tg3_phy_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016496 }
16497
16498 return err;
16499}
16500
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000016501static int tg3_resume(struct device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070016502{
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000016503 struct pci_dev *pdev = to_pci_dev(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016504 struct net_device *dev = pci_get_drvdata(pdev);
16505 struct tg3 *tp = netdev_priv(dev);
16506 int err;
16507
16508 if (!netif_running(dev))
16509 return 0;
16510
Linus Torvalds1da177e2005-04-16 15:20:36 -070016511 netif_device_attach(dev);
16512
David S. Millerf47c11e2005-06-24 20:18:35 -070016513 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016514
Joe Perches63c3a662011-04-26 08:12:10 +000016515 tg3_flag_set(tp, INIT_COMPLETE);
Michael Chanb9ec6c12006-07-25 16:37:27 -070016516 err = tg3_restart_hw(tp, 1);
16517 if (err)
16518 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016519
Matt Carlson21f76382012-02-22 12:35:21 +000016520 tg3_timer_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016521
Linus Torvalds1da177e2005-04-16 15:20:36 -070016522 tg3_netif_start(tp);
16523
Michael Chanb9ec6c12006-07-25 16:37:27 -070016524out:
David S. Millerf47c11e2005-06-24 20:18:35 -070016525 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016526
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070016527 if (!err)
16528 tg3_phy_start(tp);
16529
Michael Chanb9ec6c12006-07-25 16:37:27 -070016530 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016531}
16532
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000016533static SIMPLE_DEV_PM_OPS(tg3_pm_ops, tg3_suspend, tg3_resume);
Eric Dumazetaa6027c2011-01-01 05:22:46 +000016534#define TG3_PM_OPS (&tg3_pm_ops)
16535
16536#else
16537
16538#define TG3_PM_OPS NULL
16539
16540#endif /* CONFIG_PM_SLEEP */
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000016541
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000016542/**
16543 * tg3_io_error_detected - called when PCI error is detected
16544 * @pdev: Pointer to PCI device
16545 * @state: The current pci connection state
16546 *
16547 * This function is called after a PCI bus error affecting
16548 * this device has been detected.
16549 */
16550static pci_ers_result_t tg3_io_error_detected(struct pci_dev *pdev,
16551 pci_channel_state_t state)
16552{
16553 struct net_device *netdev = pci_get_drvdata(pdev);
16554 struct tg3 *tp = netdev_priv(netdev);
16555 pci_ers_result_t err = PCI_ERS_RESULT_NEED_RESET;
16556
16557 netdev_info(netdev, "PCI I/O error detected\n");
16558
16559 rtnl_lock();
16560
16561 if (!netif_running(netdev))
16562 goto done;
16563
16564 tg3_phy_stop(tp);
16565
16566 tg3_netif_stop(tp);
16567
Matt Carlson21f76382012-02-22 12:35:21 +000016568 tg3_timer_stop(tp);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000016569
16570 /* Want to make sure that the reset task doesn't run */
Matt Carlsondb219972011-11-04 09:15:03 +000016571 tg3_reset_task_cancel(tp);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000016572
16573 netif_device_detach(netdev);
16574
16575 /* Clean up software state, even if MMIO is blocked */
16576 tg3_full_lock(tp, 0);
16577 tg3_halt(tp, RESET_KIND_SHUTDOWN, 0);
16578 tg3_full_unlock(tp);
16579
16580done:
16581 if (state == pci_channel_io_perm_failure)
16582 err = PCI_ERS_RESULT_DISCONNECT;
16583 else
16584 pci_disable_device(pdev);
16585
16586 rtnl_unlock();
16587
16588 return err;
16589}
16590
16591/**
16592 * tg3_io_slot_reset - called after the pci bus has been reset.
16593 * @pdev: Pointer to PCI device
16594 *
16595 * Restart the card from scratch, as if from a cold-boot.
16596 * At this point, the card has exprienced a hard reset,
16597 * followed by fixups by BIOS, and has its config space
16598 * set up identically to what it was at cold boot.
16599 */
16600static pci_ers_result_t tg3_io_slot_reset(struct pci_dev *pdev)
16601{
16602 struct net_device *netdev = pci_get_drvdata(pdev);
16603 struct tg3 *tp = netdev_priv(netdev);
16604 pci_ers_result_t rc = PCI_ERS_RESULT_DISCONNECT;
16605 int err;
16606
16607 rtnl_lock();
16608
16609 if (pci_enable_device(pdev)) {
16610 netdev_err(netdev, "Cannot re-enable PCI device after reset.\n");
16611 goto done;
16612 }
16613
16614 pci_set_master(pdev);
16615 pci_restore_state(pdev);
16616 pci_save_state(pdev);
16617
16618 if (!netif_running(netdev)) {
16619 rc = PCI_ERS_RESULT_RECOVERED;
16620 goto done;
16621 }
16622
16623 err = tg3_power_up(tp);
Matt Carlsonbed98292011-07-13 09:27:29 +000016624 if (err)
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000016625 goto done;
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000016626
16627 rc = PCI_ERS_RESULT_RECOVERED;
16628
16629done:
16630 rtnl_unlock();
16631
16632 return rc;
16633}
16634
16635/**
16636 * tg3_io_resume - called when traffic can start flowing again.
16637 * @pdev: Pointer to PCI device
16638 *
16639 * This callback is called when the error recovery driver tells
16640 * us that its OK to resume normal operation.
16641 */
16642static void tg3_io_resume(struct pci_dev *pdev)
16643{
16644 struct net_device *netdev = pci_get_drvdata(pdev);
16645 struct tg3 *tp = netdev_priv(netdev);
16646 int err;
16647
16648 rtnl_lock();
16649
16650 if (!netif_running(netdev))
16651 goto done;
16652
16653 tg3_full_lock(tp, 0);
Joe Perches63c3a662011-04-26 08:12:10 +000016654 tg3_flag_set(tp, INIT_COMPLETE);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000016655 err = tg3_restart_hw(tp, 1);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000016656 if (err) {
Nithin Nayak Sujir35763062012-12-03 19:36:56 +000016657 tg3_full_unlock(tp);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000016658 netdev_err(netdev, "Cannot restart hardware after reset.\n");
16659 goto done;
16660 }
16661
16662 netif_device_attach(netdev);
16663
Matt Carlson21f76382012-02-22 12:35:21 +000016664 tg3_timer_start(tp);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000016665
16666 tg3_netif_start(tp);
16667
Nithin Nayak Sujir35763062012-12-03 19:36:56 +000016668 tg3_full_unlock(tp);
16669
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000016670 tg3_phy_start(tp);
16671
16672done:
16673 rtnl_unlock();
16674}
16675
Stephen Hemminger3646f0e2012-09-07 09:33:15 -070016676static const struct pci_error_handlers tg3_err_handler = {
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000016677 .error_detected = tg3_io_error_detected,
16678 .slot_reset = tg3_io_slot_reset,
16679 .resume = tg3_io_resume
16680};
16681
Linus Torvalds1da177e2005-04-16 15:20:36 -070016682static struct pci_driver tg3_driver = {
16683 .name = DRV_MODULE_NAME,
16684 .id_table = tg3_pci_tbl,
16685 .probe = tg3_init_one,
Bill Pemberton229b1ad2012-12-03 09:22:59 -050016686 .remove = tg3_remove_one,
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000016687 .err_handler = &tg3_err_handler,
Eric Dumazetaa6027c2011-01-01 05:22:46 +000016688 .driver.pm = TG3_PM_OPS,
Linus Torvalds1da177e2005-04-16 15:20:36 -070016689};
16690
16691static int __init tg3_init(void)
16692{
Jeff Garzik29917622006-08-19 17:48:59 -040016693 return pci_register_driver(&tg3_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016694}
16695
16696static void __exit tg3_cleanup(void)
16697{
16698 pci_unregister_driver(&tg3_driver);
16699}
16700
16701module_init(tg3_init);
16702module_exit(tg3_cleanup);